Skip to content

Decorate all generated items with CompilerGeneratedAttribute #104

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

Merged
merged 4 commits into from
Sep 14, 2024
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
3 changes: 3 additions & 0 deletions src/XamlX.IL.Cecil/CecilTypeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public IXamlMethodBuilder<IXamlILEmitter> DefineMethod(IXamlType returnType, IEn
def.Overrides.Add(Definition.Module.ImportReference(((CecilMethod) overrideMethod).Reference));
def.Body.InitLocals = true;
Definition.Methods.Add(def);
TypeSystem.AddCompilerGeneratedAttribute(def);
var rv = new CecilMethod(_builderTypeResolveContext, def);
((List<CecilMethod>)Methods).Add(rv);
return rv;
Expand Down Expand Up @@ -137,6 +138,7 @@ public IXamlTypeBuilder<IXamlILEmitter> DefineSubType(IXamlType baseType, string
var td = new TypeDefinition("", name, attrs, GetReference(baseType));

Definition.NestedTypes.Add(td);
TypeSystem.AddCompilerGeneratedAttribute(td);
return new CecilTypeBuilder(_builderTypeResolveContext, (CecilAssembly?)Assembly, td);
}

Expand All @@ -156,6 +158,7 @@ public IXamlTypeBuilder<IXamlILEmitter> DefineDelegateSubType(string name, XamlV
var builder = new TypeDefinition("", name, attrs, GetReference(TypeSystem.GetType("System.MulticastDelegate")));

Definition.NestedTypes.Add(builder);
TypeSystem.AddCompilerGeneratedAttribute(builder);

var ctor = new MethodDefinition(".ctor", MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName | MethodAttributes.HideBySig, GetReference(returnType));
ctor.ImplAttributes = MethodImplAttributes.Managed | MethodImplAttributes.Runtime;
Expand Down
39 changes: 37 additions & 2 deletions src/XamlX.IL.Cecil/CecilTypeSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Cecil.Rocks;
using XamlX.IL;

namespace XamlX.TypeSystem
Expand All @@ -17,6 +18,8 @@ partial class CecilTypeSystem : IXamlTypeSystem, IAssemblyResolver
private readonly Dictionary<string, CecilAssembly> _assemblyCache = new(StringComparer.Ordinal);
private readonly Dictionary<AssemblyDefinition, CecilAssembly> _assemblyDic = new();
private readonly CustomMetadataResolver _resolver;
private readonly TypeDefinition _compilerGeneratedAttribute;
private readonly MethodDefinition _compilerGeneratedAttributeConstructor;

public void Dispose()
{
Expand Down Expand Up @@ -70,7 +73,10 @@ public CecilTypeSystem(IEnumerable<string> paths, string? targetPath = null)
TargetAssembly = wrapped;
TargetAssemblyDefinition = asm;
}
}
}

_compilerGeneratedAttribute = GetTypeReference(FindType("System.Runtime.CompilerServices.CompilerGeneratedAttribute")!).Resolve();
_compilerGeneratedAttributeConstructor = _compilerGeneratedAttribute.GetConstructors().Single();
}

internal CecilTypeResolveContext RootTypeResolveContext { get; }
Expand Down Expand Up @@ -132,11 +138,40 @@ interface ITypeReference
TypeReference Reference { get; }
}

public IXamlTypeBuilder<IXamlILEmitter> CreateTypeBuilder(TypeDefinition def)
public IXamlTypeBuilder<IXamlILEmitter> CreateTypeBuilder(TypeDefinition def, bool compilerGeneratedType = true)
{
if (compilerGeneratedType)
{
AddCompilerGeneratedAttribute(def);
}

return new CecilTypeBuilder(RootTypeResolveContext, FindAsm(def.Module.Assembly), def);
}

public void AddCompilerGeneratedAttribute(MemberReference member)
{
if (member is not ICustomAttributeProvider { CustomAttributes: { } attributes } )
{
throw new ArgumentException($"Member '{member}' does not support custom attributes", nameof(member));
}

if (member is not TypeDefinition && member.DeclaringType.Resolve().CustomAttributes.Any(IsCompilerGeneratedAttribute))
{
return; // declaring type is already decorated
}

if (!attributes.Any(IsCompilerGeneratedAttribute))
{
if (member.Module == null)
{
throw new ArgumentException("Member has not yet been added to a module.", nameof(member));
}
attributes.Add(new(member.Module.Assembly.MainModule.ImportReference(_compilerGeneratedAttributeConstructor)));
}
}

private bool IsCompilerGeneratedAttribute(CustomAttribute attribute) => attribute.AttributeType.Resolve() == _compilerGeneratedAttribute;

public AssemblyDefinition GetAssembly(IXamlAssembly asm)
=> ((CecilAssembly) asm).Assembly;
}
Expand Down