Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
if (type is null)
return null;

// Extension methods are generated for the operation methods a type
// declares, not for those it inherits.
return new ExtensionGenerationModel(
type,
new EquatableArray<OperationMethodModel>(operationType.Methods.Where(m => SupportedKinds.Contains(m.Kind))),
new EquatableArray<OperationMethodModel>(operationType.Methods.Where(m => !m.IsInherited && SupportedKinds.Contains(m.Kind))),
options);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,18 @@ private static void AnalyzeType(SymbolAnalysisContext context, Dictionary<INamed
var (_, collisions) = DataPortalOperationsBuilder.SelectNamedDispatchMethods(model.Methods);
foreach (var collision in collisions)
{
// A collision between two inherited methods is reported on the type
// declaring them. One that involves a method the type declares is
// reported at that method, even when the other method wins.
var declared = !collision.Loser.IsInherited ? collision.Loser
: !collision.Winner.IsInherited ? collision.Winner
: null;
if (declared is null)
continue;

context.ReportDiagnostic(Diagnostic.Create(
collision.IsAmbiguous ? DataPortalOperationsDiagnostics.AmbiguousOperationName : DataPortalOperationsDiagnostics.DuplicateOperationName,
collision.Loser.Location?.ToLocation(context.Compilation),
declared.Location?.ToLocation(context.Compilation),
collision.Winner.MethodDisplay, collision.Loser.MethodDisplay, model.Type.TypeName, collision.Winner.OperationName));
}
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,14 @@ internal static IncrementalValueProvider<EquatableArray<OperationTypeModel>> Get
.Select(static (pair, _) => pair.Left.AddRange(pair.Right));
}

// Inherited operation methods are added once per type, rather than
// for each operation method the type declares.
return all
.Select(static (entries, ct) => OperationDiscovery.GroupByType(entries, ct))
.WithTrackingName(TrackingNames.GroupOperationTypes);
.WithTrackingName(TrackingNames.GroupOperationTypes)
.Combine(context.CompilationProvider)
.Select(static (pair, ct) => OperationDiscovery.AddInheritedMethods(pair.Left, pair.Right, ct))
.WithTrackingName(TrackingNames.InheritedOperationMethods);
}

private static IncrementalValueProvider<ImmutableArray<OperationMethodEntry>> GetOperationMethods(IncrementalGeneratorInitializationContext context, string kind)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,34 @@ internal sealed record OperationMethodModel

public LocationInfo? Location { get; init; }

/// <summary>
/// How far up the inheritance chain the method is declared: 0 for a
/// method declared by the type, 1 for one inherited from its base type,
/// and so on. Like reflection-based dispatch, a method closer to the
/// type is preferred.
/// </summary>
public int InheritanceDepth { get; init; }

/// <summary>
/// The generated code calls the inherited method as <c>base.Method(...)</c>
/// because the type declares another member with the same name.
/// </summary>
public bool CallThroughBase { get; init; }

/// <summary>
/// The method is declared by a base type, not by the type itself. An
/// inherited method the type overrides without repeating the operation
/// attribute has an <see cref="InheritanceDepth"/> of 0 but is still
/// inherited.
/// </summary>
public bool IsInherited { get; init; }

/// <summary>
/// The method takes a single <c>object[]</c> parameter, which receives
/// the whole criteria array.
/// </summary>
public bool TakesCriteriaArray => Parameters.Count == 1 && Parameters[0].IsObjectArray;

public IEnumerable<OperationParameterModel> CriteriaParameters => Parameters.Where(p => !p.IsInjected);

public int CriteriaCount => Parameters.Count(p => !p.IsInjected);
Expand All @@ -61,7 +89,24 @@ internal sealed record OperationMethodModel
/// </summary>
public bool CanDispatch =>
!Parameters.Any(p => p.RefKindPrefix is "ref " or "out ")
&& !(Parameters.Count == 1 && Parameters[0].IsObjectArray);
&& !TakesCriteriaArray;

/// <summary>
/// The generated code can call the method through the type's internal
/// IDataPortalOperations interface. A method taking a private or
/// protected nested type cannot be named by the interface, and an
/// inherited method may be inaccessible or hidden by another member of
/// the same name. Such a method is left out of the interface and of
/// generated dispatch, and is called through reflection, but it still
/// competes with the other methods when generated dispatch selects one.
/// </summary>
public bool CanCall { get; init; } = true;

/// <summary>
/// The generated code can name the criteria parameter types, for type
/// checks that keep another method from capturing a call.
/// </summary>
public bool CanNameCriteriaTypes { get; init; } = true;

/// <summary>
/// The method can take part in name-based dispatch. When criteria
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@ internal enum TypeVisibility
Private
}

/// <summary>
/// The kind of a parameter type, used to decide whether one criteria value
/// can match two parameter types.
/// </summary>
internal enum CriteriaTypeCategory
{
/// <summary>object, dynamic, or a type the generator cannot reason about.</summary>
Unknown,
TypeParameter,
Interface,
Array,
/// <summary>A value type or a sealed class.</summary>
Sealed,
/// <summary>A class that can have subclasses.</summary>
Class
}

/// <summary>
/// A parameter of a data portal operation method.
/// </summary>
Expand Down Expand Up @@ -71,5 +88,17 @@ internal sealed record OperationParameterModel
public string? DefaultValueExpression { get; init; }

public TypeVisibility Visibility { get; init; }

/// <summary>The kind of the pattern type.</summary>
public CriteriaTypeCategory Category { get; init; }

/// <summary>
/// The pattern types of the base classes and interfaces of the pattern
/// type, which a value of this type also matches.
/// </summary>
public EquatableArray<string> AssignableTo { get; init; }

/// <summary>For an array, its element type is a value type.</summary>
public bool HasValueTypeElements { get; init; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,17 @@ internal sealed record OperationTypeHeader
internal sealed record OperationMethodEntry(OperationTypeHeader Type, OperationMethodModel Method);

/// <summary>
/// A type and all of its data portal operation methods.
/// A type and all of its data portal operation methods: those it
/// declares, followed by those it inherits, nearest base type first.
/// </summary>
internal sealed record OperationTypeModel(OperationTypeHeader Type, EquatableArray<OperationMethodModel> Methods);
internal sealed record OperationTypeModel(OperationTypeHeader Type, EquatableArray<OperationMethodModel> Methods)
{
/// <summary>
/// Operation kinds for which reflection-based dispatch can see an
/// operation method that is not in <see cref="Methods"/>, such as an
/// inherited method the generated code cannot call. Generated dispatch
/// must not assume it knows every candidate method of these kinds.
/// </summary>
public EquatableArray<string> UnresolvedKinds { get; init; }
}
}
Loading
Loading