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 @@ -284,6 +284,7 @@ internal Binding (BindingInfo bindingInfo, string name, ImmutableArray<string> @
declaration: enumDeclaration,
bindingType: BindingType.SmartEnum,
name: out name,
typeInfo: out typeInfo,
baseClass: out baseClass,
interfaces: out interfaces,
outerClasses: out outerClasses,
Expand Down Expand Up @@ -350,6 +351,7 @@ internal Binding (BindingInfo bindingInfo, string name, ImmutableArray<string> @
bindingType: classDeclaration.GetBindingType (context.SemanticModel),
name: out name,
baseClass: out baseClass,
typeInfo: out typeInfo,
interfaces: out interfaces,
outerClasses: out outerClasses,
namespaces: out namespaces,
Expand Down Expand Up @@ -392,6 +394,7 @@ internal Binding (BindingInfo bindingInfo, string name, ImmutableArray<string> @
declaration: interfaceDeclaration,
bindingType: BindingType.Protocol,
name: out name,
typeInfo: out typeInfo,
baseClass: out baseClass,
interfaces: out interfaces,
outerClasses: out outerClasses,
Expand Down
7 changes: 6 additions & 1 deletion src/rgen/Microsoft.Macios.Generator/DataModel/Binding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.Macios.Generator.Availability;
using Microsoft.Macios.Generator.Context;
using TypeInfo = Microsoft.Macios.Generator.DataModel.TypeInfo;

namespace Microsoft.Macios.Generator.DataModel;

Expand All @@ -28,6 +27,12 @@ readonly partial struct Binding {
/// </summary>
public string Name => name;

readonly TypeInfo typeInfo = TypeInfo.Default;
/// <summary>
/// Gets the type information for the named type that generated the code change.
/// </summary>
public TypeInfo TypeInfo => typeInfo;

readonly ImmutableArray<string> namespaces = ImmutableArray<string>.Empty;
/// <summary>
/// The namespace that contains the named type that generated the code change.
Expand Down
11 changes: 11 additions & 0 deletions src/rgen/Microsoft.Macios.Generator/DataModel/TypeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,16 @@ public bool IsDictionaryContainer {
init => isDictionaryContainer = value;
}

readonly bool isView = false;

/// <summary>
/// True if the type inherits from the UIView class.
/// </summary>
public bool IsView {
get => isView;
init => isView = value;
}

/// <summary>
/// True if the type represents a delegate.
/// </summary>
Expand Down Expand Up @@ -387,6 +397,7 @@ internal TypeInfo (ITypeSymbol symbol) : this (StructState.Initialized)
isNSObject: out isNSObject,
isNativeObject: out isINativeObject,
isDictionaryContainer: out isDictionaryContainer,
isView: out isView,
parents: out parents,
interfaces: out interfaces);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,34 @@ namespace Microsoft.Macios.Generator.Extensions;

static partial class SemanticModelExtensions {

/// <summary>
/// Extracts symbol data from a type declaration including binding information, type details, and availability.
/// </summary>
/// <param name="self">The semantic model to analyze.</param>
/// <param name="declaration">The type declaration syntax to process.</param>
/// <param name="bindingType">The type of binding to extract data for.</param>
/// <param name="name">When this method returns, contains the name of the symbol.</param>
/// <param name="baseClass">When this method returns, contains the base class name if present; otherwise, null.</param>
/// <param name="typeInfo">When this method returns, contains the type information for named types; otherwise, default.</param>
/// <param name="interfaces">When this method returns, contains the implemented interface names.</param>
/// <param name="outerClasses">When this method returns, contains the outer class hierarchy.</param>
/// <param name="namespaces">When this method returns, contains the namespace hierarchy.</param>
/// <param name="symbolAvailability">When this method returns, contains the platform availability information.</param>
/// <param name="bindingInfo">When this method returns, contains the binding-specific information based on the binding type.</param>
public static void GetSymbolData (this SemanticModel self, BaseTypeDeclarationSyntax declaration,
BindingType bindingType,
out string name,
out string? baseClass,
out TypeInfo typeInfo,
out ImmutableArray<string> interfaces,
out ImmutableArray<OuterClass> outerClasses,
out ImmutableArray<string> namespaces,
out SymbolAvailability symbolAvailability,
out BindingInfo bindingInfo)
{
var symbol = self.GetDeclaredSymbol (declaration);
// only named types have type info
typeInfo = (symbol is INamedTypeSymbol namedTypeSymbol) ? new (namedTypeSymbol) : TypeInfo.Default;
GetSymbolData (symbol, out name, out baseClass, out interfaces, out outerClasses, out namespaces, out symbolAvailability);
if (symbol is null)
bindingInfo = default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ static partial class TypeSymbolExtensions {
const string nativeObjectInterface = "ObjCRuntime.INativeObject";
const string nsObjectClass = "Foundation.NSObject";
const string dictionaryContainerClass = "Foundation.DictionaryContainer";
const string uiViewClass = "UIKit.UIView";
const string appKitViewClass = "AppKit.NSView";

/// <summary>
/// Retrieve a dictionary with the attribute data of all the attributes attached to a symbol. Because
Expand Down Expand Up @@ -419,17 +421,19 @@ internal static int GetValueTypeSize (this ITypeSymbol type, List<ITypeSymbol> f
/// <param name="symbol">The symbol whose inheritance we want to retrieve.</param>
/// <param name="isNativeObject">If the type implements the INativeObject interface.</param>
/// <param name="isDictionaryContainer">If the type inherits from Foundation.DictionaryContainer.</param>
/// <param name="isView">If the class represents a view.</param>
/// <param name="parents">An immutable array of the parents in order from closest to furthest.</param>
/// <param name="interfaces">All implemented interfaces by the type and its parents.</param>
/// <param name="isNSObject">If the type inherits from NSObject.</param>
public static void GetInheritance (
this ITypeSymbol symbol, out bool isNSObject, out bool isNativeObject, out bool isDictionaryContainer,
this ITypeSymbol symbol, out bool isNSObject, out bool isNativeObject, out bool isDictionaryContainer, out bool isView,
out ImmutableArray<string> parents,
out ImmutableArray<string> interfaces)
{
isNSObject = symbol.ToDisplayString ().Trim ('?') == nsObjectClass;
isNativeObject = false;
isDictionaryContainer = false;
isView = false;

// parents will be returned directly in a Immutable array via a builder since the order is important
// interfaces will use a hash set because we do not want duplicates.
Expand All @@ -443,6 +447,7 @@ public static void GetInheritance (
var parentName = currentType.ToDisplayString ().Trim ();
isNSObject |= parentName == nsObjectClass;
isDictionaryContainer |= parentName == dictionaryContainerClass;
isView |= parentName is uiViewClass or appKitViewClass;
parentsBuilder.Add (parentName);

// union with the current interfaces
Expand Down Expand Up @@ -497,6 +502,7 @@ public static bool IsWrapped (this ITypeSymbol symbol)
isNSObject: out bool isNSObject,
isNativeObject: out bool _,
isDictionaryContainer: out bool _,
isView: out bool _,
parents: out ImmutableArray<string> _,
interfaces: out ImmutableArray<string> _);
// either we are a NSObject or we are a subclass of it
Expand All @@ -514,6 +520,7 @@ public static bool IsINativeObject (this ITypeSymbol symbol)
isNSObject: out bool _,
isNativeObject: out bool isNativeObject,
isDictionaryContainer: out bool _,
isView: out bool _,
parents: out ImmutableArray<string> _,
interfaces: out ImmutableArray<string> _);
return isNativeObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ internal TypeInfo (ITypeSymbol symbol, Dictionary<string, List<AttributeData>> a
isNSObject: out isNSObject,
isNativeObject: out isINativeObject,
isDictionaryContainer: out isDictionaryContainer,
isView: out isView,
parents: out parents,
interfaces: out interfaces);
IsArray = symbol is IArrayTypeSymbol;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -719,13 +719,15 @@ internal void GetNameAndNamespaceTests (ApplePlatform platform, BindingType bind
declaration: declaration,
bindingType: bindingType,
name: out var name,
typeInfo: out var typeInfo,
baseClass: out var baseClass,
interfaces: out var interfaces,
outerClasses: out var outerClasses,
namespaces: out var @namespace,
symbolAvailability: out var symbolAvailability,
bindingInfo: out var bindingData);
Assert.Equal (expectedName, name);
Assert.False (typeInfo.IsNullOrDefault);
Assert.Equal (expectedBaseClass, baseClass);
Assert.Equal (expectedInterfaces, interfaces, interfacesComparer);
Assert.Equal (expectedOuterClasses, outerClasses, new OuterClassEqualityComparer ());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,12 @@ void GetInheritance (ApplePlatform platform, string inputText,
isNSObject: out var isNsObject,
isNativeObject: out var isNativeObject,
isDictionaryContainer: out var isDictionaryContainer,
isView: out var isUIView,
parents: out var parents,
interfaces: out var interfaces);
Assert.Equal (expectedIsNSObject, isNsObject);
Assert.Equal (expectedIsNativeObject, isNativeObject);
Assert.False (isUIView);
Assert.Equal (expectedParents, parents);
Assert.Equal (expectedInterfaces, interfaces);
Assert.Equal (expectedDictionaryContainer, isDictionaryContainer);
Expand Down