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
31 changes: 10 additions & 21 deletions src/Orleans.CodeGenerator/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ namespace Orleans.CodeGenerator
{
public class CodeGeneratorOptions
{
public List<string> GenerateSerializerAttributes { get; } = new() { "Orleans.GenerateSerializerAttribute" };
public List<string> IdAttributes { get; } = new() { "Orleans.IdAttribute" };
public List<string> AliasAttributes { get; } = new() { "Orleans.AliasAttribute" };
public List<string> ImmutableAttributes { get; } = new() { "Orleans.ImmutableAttribute" };
public List<string> ConstructorAttributes { get; } = new() { "Orleans.OrleansConstructorAttribute", "Microsoft.Extensions.DependencyInjection.ActivatorUtilitiesConstructorAttribute" };
public const string IdAttribute = "Orleans.IdAttribute";
public const string AliasAttribute = "Orleans.AliasAttribute";
public const string ImmutableAttribute = "Orleans.ImmutableAttribute";
public static readonly IReadOnlyList<string> ConstructorAttributes = ["Orleans.OrleansConstructorAttribute", "Microsoft.Extensions.DependencyInjection.ActivatorUtilitiesConstructorAttribute"];
public GenerateFieldIds GenerateFieldIds { get; set; }
public bool GenerateCompatibilityInvokers { get; set; }
}
Expand Down Expand Up @@ -62,13 +61,11 @@ public CodeGenerator(Compilation compilation, CodeGeneratorOptions options)

public CompilationUnitSyntax GenerateCode(CancellationToken cancellationToken)
{
var referencedAssemblies = new HashSet<IAssemblySymbol>(SymbolEqualityComparer.Default);
var assembliesToExamine = new HashSet<IAssemblySymbol>(SymbolEqualityComparer.Default);
var compilationAsm = LibraryTypes.Compilation.Assembly;
ComputeAssembliesToExamine(compilationAsm, assembliesToExamine);

// Expand the set of referenced assemblies
referencedAssemblies.Add(compilationAsm);
MetadataModel.ApplicationParts.Add(compilationAsm.MetadataName);
foreach (var reference in LibraryTypes.Compilation.References)
{
Expand All @@ -77,11 +74,6 @@ public CompilationUnitSyntax GenerateCode(CancellationToken cancellationToken)
continue;
}

if (!referencedAssemblies.Add(asm))
{
continue;
}

if (asm.GetAttributes(LibraryTypes.ApplicationPartAttribute, out var attrs))
{
MetadataModel.ApplicationParts.Add(asm.MetadataName);
Expand All @@ -97,10 +89,10 @@ public CompilationUnitSyntax GenerateCode(CancellationToken cancellationToken)

foreach (var asm in assembliesToExamine)
{
var containingAssemblyAttributes = asm.GetAttributes();

foreach (var symbol in asm.GetDeclaredTypes())
{
var containingAssemblyAttributes = symbol.ContainingAssembly.GetAttributes();

if (GetWellKnownTypeId(symbol) is uint wellKnownTypeId)
{
MetadataModel.WellKnownTypeIds.Add((symbol.ToOpenTypeSyntax(), wellKnownTypeId));
Expand Down Expand Up @@ -283,7 +275,7 @@ GenerateFieldIds GetGenerateFieldIdsOptionFromType(INamedTypeSymbol t)
return GenerateFieldIds.None;
}

bool ShouldGenerateSerializer(INamedTypeSymbol t) => t.HasAnyAttribute(LibraryTypes.GenerateSerializerAttributes);
bool ShouldGenerateSerializer(INamedTypeSymbol t) => t.HasAttribute(LibraryTypes.GenerateSerializerAttribute);

bool ShouldIncludePrimaryConstructorParameters(INamedTypeSymbol t)
{
Expand All @@ -308,12 +300,9 @@ bool ShouldIncludePrimaryConstructorParameters(INamedTypeSymbol t)
return null;
}

foreach (var attr in LibraryTypes.GenerateSerializerAttributes)
if (TestGenerateSerializerAttribute(t, LibraryTypes.GenerateSerializerAttribute) is bool res)
{
if (TestGenerateSerializerAttribute(t, attr) is bool res)
{
return res;
}
return res;
}

// Default to true for records.
Expand Down Expand Up @@ -514,7 +503,7 @@ private static IEnumerable<IMemberDescription> GetDataMembers(FieldIdAssignmentH

internal static uint? GetId(LibraryTypes libraryTypes, ISymbol memberSymbol)
{
return memberSymbol.GetAnyAttribute(libraryTypes.IdAttributeTypes) is { } attr
return memberSymbol.GetAttribute(libraryTypes.IdAttributeType) is { } attr
? (uint)attr.ConstructorArguments.First().Value
: null;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Orleans.CodeGenerator/CopierGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ private List<GeneratedFieldDescription> GetFieldDescriptions(
private BaseCopierFieldDescription GetBaseTypeField(ISerializableTypeDescription serializableTypeDescription)
{
var baseType = serializableTypeDescription.BaseType;
if (baseType.HasAnyAttribute(LibraryTypes.GenerateSerializerAttributes)
if (baseType.HasAttribute(LibraryTypes.GenerateSerializerAttribute)
&& (SymbolEqualityComparer.Default.Equals(baseType.ContainingAssembly, LibraryTypes.Compilation.Assembly) || baseType.ContainingAssembly.HasAttribute(LibraryTypes.TypeManifestProviderAttribute))
&& baseType is not INamedTypeSymbol { IsGenericType: true })
{
Expand Down Expand Up @@ -302,14 +302,14 @@ public void GetCopierFieldDescriptions(IEnumerable<IMemberDescription> members,
if (SymbolEqualityComparer.Default.Equals(c.UnderlyingType, t))
goto skip;

if (member.Symbol.HasAnyAttribute(LibraryTypes.ImmutableAttributes))
if (member.Symbol.HasAttribute(LibraryTypes.ImmutableAttribute))
continue;

if (!uniqueTypes.Add(member))
continue;

TypeSyntax copierType;
if (t.HasAnyAttribute(LibraryTypes.GenerateSerializerAttributes)
if (t.HasAttribute(LibraryTypes.GenerateSerializerAttribute)
&& (SymbolEqualityComparer.Default.Equals(t.ContainingAssembly, LibraryTypes.Compilation.Assembly) || t.ContainingAssembly.HasAttribute(LibraryTypes.TypeManifestProviderAttribute))
&& t is not INamedTypeSymbol { IsGenericType: true, TypeArguments.Length: 0 })
{
Expand Down Expand Up @@ -693,4 +693,4 @@ public CopierFieldDescription(TypeSyntax fieldType, string fieldName, ITypeSymbo
}

}
}
}
4 changes: 2 additions & 2 deletions src/Orleans.CodeGenerator/FieldIdAssignmentHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public FieldIdAssignmentHelper(INamedTypeSymbol typeSymbol, ImmutableArray<IPara

public bool TryGetSymbolKey(ISymbol symbol, out (uint, bool) key) => _symbols.TryGetValue(symbol, out key);

private bool HasMemberWithIdAnnotation() => Array.Exists(_memberSymbols, member => member.HasAnyAttribute(_libraryTypes.IdAttributeTypes));
private bool HasMemberWithIdAnnotation() => Array.Exists(_memberSymbols, member => member.HasAttribute(_libraryTypes.IdAttributeType));

private IEnumerable<ISymbol> GetMembers(INamedTypeSymbol symbol)
{
Expand Down Expand Up @@ -194,4 +194,4 @@ bool GenerateFromProperties(IEnumerable<IPropertySymbol> properties)
return _symbols.Count > 0;
}
}
}
}
15 changes: 6 additions & 9 deletions src/Orleans.CodeGenerator/LibraryTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ private LibraryTypes(Compilation compilation, CodeGeneratorOptions options)
SerializationCallbacksAttribute = Type("Orleans.SerializationCallbacksAttribute");
IActivator_1 = Type("Orleans.Serialization.Activators.IActivator`1");
IBufferWriter = Type("System.Buffers.IBufferWriter`1");
IdAttributeTypes = options.IdAttributes.Select(Type).ToArray();
ConstructorAttributeTypes = options.ConstructorAttributes.Select(Type).ToArray();
IdAttributeType = Type(CodeGeneratorOptions.IdAttribute);
ConstructorAttributeTypes = CodeGeneratorOptions.ConstructorAttributes.Select(Type).ToArray();
AliasAttribute = Type("Orleans.AliasAttribute");
IInvokable = Type("Orleans.Serialization.Invocation.IInvokable");
InvokeMethodNameAttribute = Type("Orleans.InvokeMethodNameAttribute");
Expand Down Expand Up @@ -149,7 +149,7 @@ private LibraryTypes(Compilation compilation, CodeGeneratorOptions options)
new(compilation.GetSpecialType(SpecialType.System_Nullable_T), Type("Orleans.Serialization.Codecs.NullableCopier`1")),
};
Exception = Type("System.Exception");
ImmutableAttributes = options.ImmutableAttributes.Select(Type).ToArray();
ImmutableAttribute = Type(CodeGeneratorOptions.ImmutableAttribute);
TimeSpan = Type("System.TimeSpan");
_ipAddress = Type("System.Net.IPAddress");
_ipEndPoint = Type("System.Net.IPEndPoint");
Expand Down Expand Up @@ -185,7 +185,6 @@ private LibraryTypes(Compilation compilation, CodeGeneratorOptions options)
};

LanguageVersion = (compilation.SyntaxTrees.FirstOrDefault()?.Options as CSharpParseOptions)?.LanguageVersion;
GenerateSerializerAttributes = options.GenerateSerializerAttributes.Select(compilation.GetTypeByMetadataName).ToArray();

INamedTypeSymbol Type(string metadataName)
{
Expand Down Expand Up @@ -244,7 +243,7 @@ INamedTypeSymbol Type(string metadataName)
public INamedTypeSymbol ValueTypeGetter_2 { get; private set; }
public INamedTypeSymbol ValueTypeSetter_2 { get; private set; }
public INamedTypeSymbol Writer { get; private set; }
public INamedTypeSymbol[] IdAttributeTypes { get; private set; }
public INamedTypeSymbol IdAttributeType { get; private set; }
public INamedTypeSymbol[] ConstructorAttributeTypes { get; private set; }
public INamedTypeSymbol AliasAttribute { get; private set; }
public WellKnownCodecDescription[] StaticCodecs { get; private set; }
Expand Down Expand Up @@ -297,7 +296,7 @@ INamedTypeSymbol Type(string metadataName)
_half
}.Where(t => t is {}).ToArray()!;

public INamedTypeSymbol[] ImmutableAttributes { get; private set; }
public INamedTypeSymbol ImmutableAttribute { get; private set; }
Comment thread
ReubenBond marked this conversation as resolved.
public INamedTypeSymbol Exception { get; private set; }
public INamedTypeSymbol ApplicationPartAttribute { get; private set; }
public INamedTypeSymbol InvokeMethodNameAttribute { get; private set; }
Expand All @@ -315,8 +314,6 @@ INamedTypeSymbol Type(string metadataName)

public LanguageVersion? LanguageVersion { get; private set; }

public INamedTypeSymbol?[] GenerateSerializerAttributes { get; }

public bool IsShallowCopyable(ITypeSymbol type)
{
switch (type.SpecialType)
Expand Down Expand Up @@ -352,7 +349,7 @@ public bool IsShallowCopyable(ITypeSymbol type)
}
}

if (type.IsSealed && type.HasAnyAttribute(ImmutableAttributes))
if (type.IsSealed && type.HasAttribute(ImmutableAttribute))
{
return _shallowCopyableTypes[type] = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public bool IsEmptyConstructable

public bool IsUnsealedImmutable => !Type.IsSealed && IsImmutable;

public bool IsImmutable => Type.HasAnyAttribute(_libraryTypes.ImmutableAttributes);
public bool IsImmutable => Type.HasAttribute(_libraryTypes.ImmutableAttribute);

public bool IsExceptionType => Type.HasBaseType(_libraryTypes.Exception);

Expand Down Expand Up @@ -247,4 +247,4 @@ public ExpressionSyntax GetObjectCreationExpression()
}
}
}
}
}
21 changes: 1 addition & 20 deletions src/Orleans.CodeGenerator/OrleansSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,6 @@ public void Execute(GeneratorExecutionContext context)
}

var options = new CodeGeneratorOptions();
if (context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.orleans_immutableattributes", out var immutableAttributes) && immutableAttributes is { Length: > 0 })
{
options.ImmutableAttributes.AddRange(immutableAttributes.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList());
}

if (context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.orleans_aliasattributes", out var aliasAttributes) && aliasAttributes is { Length: > 0 })
{
options.AliasAttributes.AddRange(aliasAttributes.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList());
}

if (context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.orleans_idattributes", out var idAttributes) && idAttributes is { Length: > 0 })
{
options.IdAttributes.AddRange(idAttributes.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList());
}

if (context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.orleans_generateserializerattributes", out var generateSerializerAttributes) && generateSerializerAttributes is { Length: > 0 })
{
options.GenerateSerializerAttributes.AddRange(generateSerializerAttributes.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList());
}

if (context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.orleans_generatefieldids", out var generateFieldIds) && generateFieldIds is { Length: > 0 })
{
Expand Down Expand Up @@ -106,4 +87,4 @@ public void Initialize(GeneratorInitializationContext context)
}
}
}
#pragma warning restore RS1035 // Do not use APIs banned for analyzers
#pragma warning restore RS1035 // Do not use APIs banned for analyzers
10 changes: 5 additions & 5 deletions src/Orleans.CodeGenerator/SerializerGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ CodecFieldDescription GetCodecDescription(IMemberDescription member, int index)
{
var t = member.Type;
TypeSyntax codecType = null;
if (t.HasAnyAttribute(LibraryTypes.GenerateSerializerAttributes)
if (t.HasAttribute(LibraryTypes.GenerateSerializerAttribute)
&& (SymbolEqualityComparer.Default.Equals(t.ContainingAssembly, LibraryTypes.Compilation.Assembly) || t.ContainingAssembly.HasAttribute(LibraryTypes.TypeManifestProviderAttribute))
&& t is not INamedTypeSymbol { IsGenericType: true, TypeArguments.Length: 0 })
{
Expand Down Expand Up @@ -335,7 +335,7 @@ CodecFieldDescription GetCodecDescription(IMemberDescription member, int index)
private BaseCodecFieldDescription GetBaseTypeField(ISerializableTypeDescription serializableTypeDescription)
{
var baseType = serializableTypeDescription.BaseType;
if (baseType.HasAnyAttribute(LibraryTypes.GenerateSerializerAttributes)
if (baseType.HasAttribute(LibraryTypes.GenerateSerializerAttribute)
&& (SymbolEqualityComparer.Default.Equals(baseType.ContainingAssembly, LibraryTypes.Compilation.Assembly) || baseType.ContainingAssembly.HasAttribute(LibraryTypes.TypeManifestProviderAttribute))
&& baseType is not INamedTypeSymbol { IsGenericType: true })
{
Expand Down Expand Up @@ -1178,7 +1178,7 @@ public SerializableMethodMember(MethodParameterFieldDescription member)

private LibraryTypes LibraryTypes => _member.CodeGenerator.LibraryTypes;

public bool IsShallowCopyable => LibraryTypes.IsShallowCopyable(_member.Parameter.Type) || _member.Parameter.HasAnyAttribute(LibraryTypes.ImmutableAttributes);
public bool IsShallowCopyable => LibraryTypes.IsShallowCopyable(_member.Parameter.Type) || _member.Parameter.HasAttribute(LibraryTypes.ImmutableAttribute);

/// <summary>
/// Gets syntax representing the type of this field.
Expand Down Expand Up @@ -1237,8 +1237,8 @@ public SerializableMember(CodeGenerator codeGenerator, IMemberDescription member

public bool IsShallowCopyable =>
LibraryTypes.IsShallowCopyable(_member.Type)
|| Property is { } prop && prop.HasAnyAttribute(LibraryTypes.ImmutableAttributes)
|| _member.Symbol.HasAnyAttribute(LibraryTypes.ImmutableAttributes);
|| Property is { } prop && prop.HasAttribute(LibraryTypes.ImmutableAttribute)
|| _member.Symbol.HasAttribute(LibraryTypes.ImmutableAttribute);

public bool IsValueType => Type.IsValueType;

Expand Down
4 changes: 2 additions & 2 deletions src/Orleans.CodeGenerator/SyntaxGeneration/FSharpUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public FSharpUnionCaseFieldDescription(LibraryTypes libraryTypes, IFieldSymbol f

public uint FieldId { get; }

public bool IsShallowCopyable => _libraryTypes.IsShallowCopyable(Type) || _field.HasAnyAttribute(_libraryTypes.ImmutableAttributes);
public bool IsShallowCopyable => _libraryTypes.IsShallowCopyable(Type) || _field.HasAttribute(_libraryTypes.ImmutableAttribute);

public bool IsValueType => Type.IsValueType;

Expand Down Expand Up @@ -274,7 +274,7 @@ public FSharpRecordPropertyDescription(LibraryTypes libraryTypes, IPropertySymbo

public uint FieldId { get; }

public bool IsShallowCopyable => _libraryTypes.IsShallowCopyable(Type) || _property.HasAnyAttribute(_libraryTypes.ImmutableAttributes);
public bool IsShallowCopyable => _libraryTypes.IsShallowCopyable(Type) || _property.HasAttribute(_libraryTypes.ImmutableAttribute);

public bool IsValueType => Type.IsValueType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,14 @@
<CompilerVisibleProperty Include="Orleans_DesignTimeBuild" />
<CompilerVisibleProperty Include="Orleans_AttachDebugger" />
<CompilerVisibleProperty Include="Orleans_GenerateFieldIds" />
<CompilerVisibleProperty Include="Orleans_ImmutableAttributes" />
<CompilerVisibleProperty Include="Orleans_IdAttributes" />
<CompilerVisibleProperty Include="Orleans_AliasAttributes" />
<CompilerVisibleProperty Include="Orleans_GenerateSerializerAttributes" />
<CompilerVisibleProperty Include="Orleans_ConstructorAttributes" />
<CompilerVisibleProperty Include="OrleansGenerateCompatibilityInvokers" />
</ItemGroup>

<PropertyGroup>
<Orleans_DesignTimeBuild>$(DesignTimeBuild)</Orleans_DesignTimeBuild>
<Orleans_GenerateFieldIds>$(OrleansGenerateFieldIds)</Orleans_GenerateFieldIds>
<Orleans_ImmutableAttributes>$(OrleansImmutableAttributes)</Orleans_ImmutableAttributes>
<Orleans_IdAttributes>$(OrleansIdAttributes)</Orleans_IdAttributes>
<Orleans_AliasAttributes>$(OrleansAliasAttributes)</Orleans_AliasAttributes>
<Orleans_GenerateSerializerAttributes>$(OrleansGenerateSerializerAttributes)</Orleans_GenerateSerializerAttributes>
<Orleans_ConstructorAttributes>$(OrleansConstructorAttributes)</Orleans_ConstructorAttributes>
</PropertyGroup>

</Project>
</Project>
Loading
Loading