-
Notifications
You must be signed in to change notification settings - Fork 516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Rgen] Add extra data about the return type of a method. #21901
Changes from 1 commit
e7fc201
b933156
7c04e28
d8ad5c2
c5e0612
7f98f4a
ca7c54d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
using System; | ||
using System.Text; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.Macios.Generator.Extensions; | ||
|
||
namespace Microsoft.Macios.Generator.DataModel; | ||
|
||
/// <summary> | ||
/// Readonly structure that represents a change in a method return type. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand what is meant by "a change in" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Read https://github.com/dotnet/roslyn/blob/main/docs/features/incremental-generators.cookbook.md We get an event every time we get a code change. We calculate the data model and use that model to represent a code change. Use a comparer to invalidate the cache and move to generate if needed. In this case, this is the data model to represent a change in the method return type. |
||
/// </summary> | ||
readonly struct MethodReturnType : IEquatable<MethodReturnType> { | ||
|
||
/// <summary> | ||
/// Type of the parameter. | ||
/// </summary> | ||
public string Type { get; } | ||
|
||
/// <summary> | ||
/// True if the parameter is nullable.:w | ||
mandel-macaque marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// </summary> | ||
public bool IsNullable { get; } | ||
|
||
/// <summary> | ||
/// True if the parameter type is blittable. | ||
/// </summary> | ||
public bool IsBlittable { get; } | ||
|
||
/// <summary> | ||
/// Returns if the return type is a smart enum. | ||
/// </summary> | ||
public bool IsSmartEnum { get; } | ||
|
||
/// <summary> | ||
/// Returns if the return type is an array type. | ||
/// </summary> | ||
public bool IsArray { get; } | ||
|
||
/// <summary> | ||
/// Returns if the return type is a reference type. | ||
/// </summary> | ||
public bool IsReferenceType { get; } | ||
|
||
/// <summary> | ||
/// Returns if the return type is void. | ||
/// </summary> | ||
public bool IsVoid { get; } | ||
|
||
internal MethodReturnType (string type) | ||
{ | ||
Type = type; | ||
IsVoid = type == "void"; | ||
} | ||
|
||
internal MethodReturnType (string type, bool isNullable, bool isBlittable, bool isSmartEnum, bool isArray, | ||
bool isReferenceType) : this (type) | ||
{ | ||
IsNullable = isNullable; | ||
IsBlittable = isBlittable; | ||
IsSmartEnum = isSmartEnum; | ||
IsArray = isArray; | ||
IsReferenceType = isReferenceType; | ||
} | ||
|
||
internal MethodReturnType (ITypeSymbol symbol) : | ||
this ( | ||
symbol is IArrayTypeSymbol arrayTypeSymbol | ||
? arrayTypeSymbol.ElementType.ToDisplayString () | ||
: symbol.ToDisplayString ().Trim ('?', '[', ']')) | ||
{ | ||
IsNullable = symbol.NullableAnnotation == NullableAnnotation.Annotated; | ||
IsBlittable = symbol.IsBlittable (); | ||
IsSmartEnum = symbol.IsSmartEnum (); | ||
IsArray = symbol is IArrayTypeSymbol; | ||
IsReferenceType = symbol.IsReferenceType; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public bool Equals (MethodReturnType other) | ||
{ | ||
if (Type != other.Type) | ||
return false; | ||
if (IsNullable != other.IsNullable) | ||
return false; | ||
if (IsBlittable != other.IsBlittable) | ||
return false; | ||
if (IsSmartEnum != other.IsSmartEnum) | ||
return false; | ||
if (IsArray != other.IsArray) | ||
return false; | ||
if (IsReferenceType != other.IsReferenceType) | ||
return false; | ||
if (IsVoid != other.IsVoid) | ||
return false; | ||
|
||
return true; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override bool Equals (object? obj) | ||
{ | ||
return obj is MethodReturnType other && Equals (other); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override int GetHashCode () | ||
{ | ||
return HashCode.Combine (Type, IsNullable, IsBlittable, IsSmartEnum, IsArray, IsReferenceType, IsVoid); | ||
} | ||
|
||
public static bool operator == (MethodReturnType left, MethodReturnType right) | ||
{ | ||
return left.Equals (right); | ||
} | ||
|
||
public static bool operator != (MethodReturnType left, MethodReturnType right) | ||
{ | ||
return !left.Equals (right); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override string ToString () | ||
{ | ||
var sb = new StringBuilder ("{"); | ||
sb.Append ($"Type: {Type}, "); | ||
sb.Append ($"IsNullable: {IsNullable}, "); | ||
sb.Append ($"IsBlittable: {IsBlittable}, "); | ||
sb.Append ($"IsSmartEnum: {IsSmartEnum}, "); | ||
sb.Append ($"IsArray: {IsArray}, "); | ||
sb.Append ($"IsReferenceType: {IsReferenceType}, "); | ||
sb.Append ($"IsVoid : {IsVoid} }}"); | ||
return sb.ToString (); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs copyright notice There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same #21900 (review) |
||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.Macios.Generator.DataModel; | ||
|
||
class MethodReturnTypeComparer : IComparer<MethodReturnType> { | ||
|
||
/// <inheritdoc/> | ||
public int Compare (MethodReturnType x, MethodReturnType y) | ||
{ | ||
var returnTypeComparison = String.Compare (x.Type, y.Type, StringComparison.Ordinal); | ||
if (returnTypeComparison != 0) | ||
return returnTypeComparison; | ||
var isNullableComparison = x.IsNullable.CompareTo (y.IsNullable); | ||
if (isNullableComparison != 0) | ||
return isNullableComparison; | ||
var isBlittableComparison = x.IsBlittable.CompareTo (y.IsBlittable); | ||
if (isBlittableComparison != 0) | ||
return isBlittableComparison; | ||
var isSmartEnumComparison = x.IsSmartEnum.CompareTo (y.IsSmartEnum); | ||
if (isSmartEnumComparison != 0) | ||
return isSmartEnumComparison; | ||
var isArrayComparison = x.IsArray.CompareTo (y.IsArray); | ||
if (isArrayComparison != 0) | ||
return isArrayComparison; | ||
var isReferenceTypeComparison = x.IsReferenceType.CompareTo (y.IsReferenceType); | ||
if (isReferenceTypeComparison != 0) | ||
return isReferenceTypeComparison; | ||
return x.IsVoid.CompareTo (y.IsVoid); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs copyright notice
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #21900 (review)