Skip to content
Open
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
2 changes: 1 addition & 1 deletion Arch.AOT.SourceGenerator/Arch.AOT.SourceGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<PackageId>Arch.AOT.SourceGenerator</PackageId>
<Title>Arch.AOT.SourceGenerator</Title>
<Version>1.0.1</Version>
<Version>1.0.3-custom</Version>
<Authors>genaray</Authors>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<Description>A source generator for arch to support AOT. </Description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static StringBuilder AppendComponentTypes(this StringBuilder sb, IList<Co
sb.AppendLine(
$$"""
using System.Runtime.CompilerServices;
using Arch.Core.Utils;
using Arch.Core;

namespace Arch.AOT.SourceGenerator
{
Expand Down
71 changes: 60 additions & 11 deletions Arch.AOT.SourceGenerator/SourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,32 +108,81 @@ private void GenerateCode(SourceProductionContext productionContext, Compilation
var sb = new StringBuilder();
_componentTypes.Clear();

var openGenericTypes = new HashSet<INamedTypeSymbol>(SymbolEqualityComparer.Default);
var closedGenericTypes = new HashSet<INamedTypeSymbol>(SymbolEqualityComparer.Default);

foreach (var type in typeList)
{
// Get the symbol for the type.
var symbol = ModelExtensions.GetDeclaredSymbol(compilation.GetSemanticModel(type.SyntaxTree), type);

// If the symbol is not a type symbol, we can't do anything with it.
if (symbol is not ITypeSymbol typeSymbol)
if (symbol is not INamedTypeSymbol typeSymbol)
{
continue;
}

// Check if there are any fields in the type.
var hasZeroFields = true;
foreach (var member in typeSymbol.GetMembers())

// If the type is a generic Type definition (e.g. SomeType<T>) we don't want to register it, we want to register their concrete usages instead.
if (!IsClosedType(typeSymbol))
{
if (member is not IFieldSymbol) continue;

hasZeroFields = false;
break;
openGenericTypes.Add(typeSymbol);
continue;
}

var typeName = typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
_componentTypes.Add(new ComponentType(typeName, hasZeroFields, typeSymbol.IsValueType));
AddComponentType(typeSymbol);
}

foreach (var tree in compilation.SyntaxTrees)
{
var semanticModel = compilation.GetSemanticModel(tree);
var root = tree.GetRoot();

foreach (var node in root.DescendantNodes().OfType<TypeSyntax>())
{
if (semanticModel.GetSymbolInfo(node).Symbol is not INamedTypeSymbol typeSymbol)
continue;

// This is a constructed instance of a generic component
if (openGenericTypes.Contains(typeSymbol.OriginalDefinition) && IsClosedType(typeSymbol))
{
closedGenericTypes.Add(typeSymbol);
}
}
}

foreach (var typeSymbol in closedGenericTypes)
{
AddComponentType(typeSymbol);
}

sb.AppendComponentTypes(_componentTypes);
productionContext.AddSource("GeneratedComponentRegistry.g.cs",CSharpSyntaxTree.ParseText(sb.ToString()).GetRoot().NormalizeWhitespace().ToFullString());
}

private static bool IsClosedType(INamedTypeSymbol typeSymbol)
{
if (typeSymbol.IsUnboundGenericType) return false;
if (typeSymbol.IsGenericType)
{
return typeSymbol.TypeArguments.All(t => t.TypeKind != TypeKind.TypeParameter);
}

return true;
}

private void AddComponentType(INamedTypeSymbol typeSymbol)
{
// Check if there are any fields in the type.
var hasZeroFields = true;
foreach (var member in typeSymbol.GetMembers())
{
if (member is not IFieldSymbol) continue;

hasZeroFields = false;
break;
}

var typeName = typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
_componentTypes.Add(new ComponentType(typeName, hasZeroFields, typeSymbol.IsValueType));
}
}