diff options
author | jfrijters <jfrijters> | 2015-04-06 10:12:29 +0300 |
---|---|---|
committer | jfrijters <jfrijters> | 2015-04-06 10:12:29 +0300 |
commit | 9f39c83fb4671ce138c6e8092778b84d012bdf54 (patch) | |
tree | 779b9f38f94b712d881756d5d0ef856be1010888 | |
parent | ec3143fd38942c189fd59fb76bca773504db4d57 (diff) |
Fixed known custom attribute handling. They should be recognized by type name, not type identity.
-rw-r--r-- | reflect/Emit/CustomAttributeBuilder.cs | 80 | ||||
-rw-r--r-- | reflect/Emit/EventBuilder.cs | 3 | ||||
-rw-r--r-- | reflect/Emit/FieldBuilder.cs | 38 | ||||
-rw-r--r-- | reflect/Emit/MethodBuilder.cs | 43 | ||||
-rw-r--r-- | reflect/Emit/ParameterBuilder.cs | 37 | ||||
-rw-r--r-- | reflect/Emit/PropertyBuilder.cs | 3 | ||||
-rw-r--r-- | reflect/Emit/TypeBuilder.cs | 41 | ||||
-rw-r--r-- | reflect/TypeNameParser.cs | 15 | ||||
-rw-r--r-- | reflect/Universe.cs | 67 |
9 files changed, 168 insertions, 159 deletions
diff --git a/reflect/Emit/CustomAttributeBuilder.cs b/reflect/Emit/CustomAttributeBuilder.cs index 4a1a1435..93875248 100644 --- a/reflect/Emit/CustomAttributeBuilder.cs +++ b/reflect/Emit/CustomAttributeBuilder.cs @@ -668,5 +668,85 @@ namespace IKVM.Reflection.Emit bw.WriteCustomAttributeBlob(); return bb.ToArray(); } + + internal KnownCA KnownCA + { + get + { + TypeName typeName = con.DeclaringType.TypeName; + switch (typeName.Namespace) + { + case "System": + switch (typeName.Name) + { + case "SerializableAttribute": + return KnownCA.SerializableAttribute; + case "NonSerializedAttribute": + return KnownCA.NonSerializedAttribute; + } + break; + case "System.Runtime.CompilerServices": + switch (typeName.Name) + { + case "MethodImplAttribute": + return KnownCA.MethodImplAttribute; + case "SpecialNameAttribute": + return KnownCA.SpecialNameAttribute; + } + break; + case "System.Runtime.InteropServices": + switch (typeName.Name) + { + case "DllImportAttribute": + return KnownCA.DllImportAttribute; + case "ComImportAttribute": + return KnownCA.ComImportAttribute; + case "MarshalAsAttribute": + return KnownCA.MarshalAsAttribute; + case "PreserveSigAttribute": + return KnownCA.PreserveSigAttribute; + case "InAttribute": + return KnownCA.InAttribute; + case "OutAttribute": + return KnownCA.OutAttribute; + case "OptionalAttribute": + return KnownCA.OptionalAttribute; + case "StructLayoutAttribute": + return KnownCA.StructLayoutAttribute; + case "FieldOffsetAttribute": + return KnownCA.FieldOffsetAttribute; + } + break; + } + if (typeName.Matches("System.Security.SuppressUnmanagedCodeSecurityAttribute")) + { + return KnownCA.SuppressUnmanagedCodeSecurityAttribute; + } + return KnownCA.Unknown; + } + } + } + + // These are the pseudo-custom attributes that are recognized by name by the runtime (i.e. the type identity is not considered). + // The corresponding list in the runtime is at https://github.com/dotnet/coreclr/blob/1afe5ce4f45045d724a4e129df4b816655d486fb/src/md/compiler/custattr_emit.cpp#L38 + // Note that we only need to handle a subset of the types, since we don't need the ones that are only used for validation by the runtime. + enum KnownCA + { + Unknown, + DllImportAttribute, + ComImportAttribute, + SerializableAttribute, + NonSerializedAttribute, + MethodImplAttribute, + MarshalAsAttribute, + PreserveSigAttribute, + InAttribute, + OutAttribute, + OptionalAttribute, + StructLayoutAttribute, + FieldOffsetAttribute, + SpecialNameAttribute, + // the following is not part of the runtime known custom attributes, but we handle it here for efficiency and convenience + SuppressUnmanagedCodeSecurityAttribute, } } diff --git a/reflect/Emit/EventBuilder.cs b/reflect/Emit/EventBuilder.cs index 03242720..dd3c18e2 100644 --- a/reflect/Emit/EventBuilder.cs +++ b/reflect/Emit/EventBuilder.cs @@ -97,8 +97,7 @@ namespace IKVM.Reflection.Emit public void SetCustomAttribute(CustomAttributeBuilder customBuilder) { - Universe u = typeBuilder.ModuleBuilder.universe; - if (customBuilder.Constructor.DeclaringType == u.System_Runtime_CompilerServices_SpecialNameAttribute) + if (customBuilder.KnownCA == KnownCA.SpecialNameAttribute) { attributes |= EventAttributes.SpecialName; } diff --git a/reflect/Emit/FieldBuilder.cs b/reflect/Emit/FieldBuilder.cs index cbee210b..0ac911d9 100644 --- a/reflect/Emit/FieldBuilder.cs +++ b/reflect/Emit/FieldBuilder.cs @@ -124,28 +124,24 @@ namespace IKVM.Reflection.Emit public void SetCustomAttribute(CustomAttributeBuilder customBuilder) { - Universe u = this.Module.universe; - if (customBuilder.Constructor.DeclaringType == u.System_Runtime_InteropServices_FieldOffsetAttribute) + switch (customBuilder.KnownCA) { - customBuilder = customBuilder.DecodeBlob(this.Module.Assembly); - SetOffset((int)customBuilder.GetConstructorArgument(0)); - } - else if (customBuilder.Constructor.DeclaringType == u.System_Runtime_InteropServices_MarshalAsAttribute) - { - FieldMarshal.SetMarshalAsAttribute(typeBuilder.ModuleBuilder, pseudoToken, customBuilder); - attribs |= FieldAttributes.HasFieldMarshal; - } - else if (customBuilder.Constructor.DeclaringType == u.System_NonSerializedAttribute) - { - attribs |= FieldAttributes.NotSerialized; - } - else if (customBuilder.Constructor.DeclaringType == u.System_Runtime_CompilerServices_SpecialNameAttribute) - { - attribs |= FieldAttributes.SpecialName; - } - else - { - typeBuilder.ModuleBuilder.SetCustomAttribute(pseudoToken, customBuilder); + case KnownCA.FieldOffsetAttribute: + SetOffset((int)customBuilder.DecodeBlob(this.Module.Assembly).GetConstructorArgument(0)); + break; + case KnownCA.MarshalAsAttribute: + FieldMarshal.SetMarshalAsAttribute(typeBuilder.ModuleBuilder, pseudoToken, customBuilder); + attribs |= FieldAttributes.HasFieldMarshal; + break; + case KnownCA.NonSerializedAttribute: + attribs |= FieldAttributes.NotSerialized; + break; + case KnownCA.SpecialNameAttribute: + attribs |= FieldAttributes.SpecialName; + break; + default: + typeBuilder.ModuleBuilder.SetCustomAttribute(pseudoToken, customBuilder); + break; } } diff --git a/reflect/Emit/MethodBuilder.cs b/reflect/Emit/MethodBuilder.cs index b3602d7b..6bf20a2c 100644 --- a/reflect/Emit/MethodBuilder.cs +++ b/reflect/Emit/MethodBuilder.cs @@ -255,32 +255,27 @@ namespace IKVM.Reflection.Emit public void SetCustomAttribute(CustomAttributeBuilder customBuilder) { - Universe u = this.ModuleBuilder.universe; - Type type = customBuilder.Constructor.DeclaringType; - if (type == u.System_Runtime_InteropServices_DllImportAttribute) + switch (customBuilder.KnownCA) { - attributes |= MethodAttributes.PinvokeImpl; - SetDllImportPseudoCustomAttribute(customBuilder.DecodeBlob(this.Module.Assembly)); - } - else if (type == u.System_Runtime_CompilerServices_MethodImplAttribute) - { - SetMethodImplAttribute(customBuilder.DecodeBlob(this.Module.Assembly)); - } - else if (type == u.System_Runtime_InteropServices_PreserveSigAttribute) - { - implFlags |= MethodImplAttributes.PreserveSig; - } - else if (type == u.System_Runtime_CompilerServices_SpecialNameAttribute) - { - attributes |= MethodAttributes.SpecialName; - } - else - { - if (type == u.System_Security_SuppressUnmanagedCodeSecurityAttribute) - { + case KnownCA.DllImportAttribute: + SetDllImportPseudoCustomAttribute(customBuilder.DecodeBlob(this.Module.Assembly)); + attributes |= MethodAttributes.PinvokeImpl; + break; + case KnownCA.MethodImplAttribute: + SetMethodImplAttribute(customBuilder.DecodeBlob(this.Module.Assembly)); + break; + case KnownCA.PreserveSigAttribute: + implFlags |= MethodImplAttributes.PreserveSig; + break; + case KnownCA.SpecialNameAttribute: + attributes |= MethodAttributes.SpecialName; + break; + case KnownCA.SuppressUnmanagedCodeSecurityAttribute: attributes |= MethodAttributes.HasSecurity; - } - this.ModuleBuilder.SetCustomAttribute(pseudoToken, customBuilder); + goto default; + default: + this.ModuleBuilder.SetCustomAttribute(pseudoToken, customBuilder); + break; } } diff --git a/reflect/Emit/ParameterBuilder.cs b/reflect/Emit/ParameterBuilder.cs index 8b01efca..4626496a 100644 --- a/reflect/Emit/ParameterBuilder.cs +++ b/reflect/Emit/ParameterBuilder.cs @@ -96,27 +96,24 @@ namespace IKVM.Reflection.Emit public void SetCustomAttribute(CustomAttributeBuilder customAttributeBuilder) { - Universe u = moduleBuilder.universe; - if (customAttributeBuilder.Constructor.DeclaringType == u.System_Runtime_InteropServices_InAttribute) + switch (customAttributeBuilder.KnownCA) { - flags |= (short)ParameterAttributes.In; - } - else if (customAttributeBuilder.Constructor.DeclaringType == u.System_Runtime_InteropServices_OutAttribute) - { - flags |= (short)ParameterAttributes.Out; - } - else if (customAttributeBuilder.Constructor.DeclaringType == u.System_Runtime_InteropServices_OptionalAttribute) - { - flags |= (short)ParameterAttributes.Optional; - } - else if (customAttributeBuilder.Constructor.DeclaringType == u.System_Runtime_InteropServices_MarshalAsAttribute) - { - FieldMarshal.SetMarshalAsAttribute(moduleBuilder, PseudoToken, customAttributeBuilder); - flags |= (short)ParameterAttributes.HasFieldMarshal; - } - else - { - moduleBuilder.SetCustomAttribute(PseudoToken, customAttributeBuilder); + case KnownCA.InAttribute: + flags |= (short)ParameterAttributes.In; + break; + case KnownCA.OutAttribute: + flags |= (short)ParameterAttributes.Out; + break; + case KnownCA.OptionalAttribute: + flags |= (short)ParameterAttributes.Optional; + break; + case KnownCA.MarshalAsAttribute: + FieldMarshal.SetMarshalAsAttribute(moduleBuilder, PseudoToken, customAttributeBuilder); + flags |= (short)ParameterAttributes.HasFieldMarshal; + break; + default: + moduleBuilder.SetCustomAttribute(PseudoToken, customAttributeBuilder); + break; } } diff --git a/reflect/Emit/PropertyBuilder.cs b/reflect/Emit/PropertyBuilder.cs index 6e3c741c..76661d0b 100644 --- a/reflect/Emit/PropertyBuilder.cs +++ b/reflect/Emit/PropertyBuilder.cs @@ -94,8 +94,7 @@ namespace IKVM.Reflection.Emit public void SetCustomAttribute(CustomAttributeBuilder customBuilder) { - Universe u = typeBuilder.ModuleBuilder.universe; - if (customBuilder.Constructor.DeclaringType == u.System_Runtime_CompilerServices_SpecialNameAttribute) + if (customBuilder.KnownCA == KnownCA.SpecialNameAttribute) { attributes |= PropertyAttributes.SpecialName; } diff --git a/reflect/Emit/TypeBuilder.cs b/reflect/Emit/TypeBuilder.cs index 6f17cd73..7b852740 100644 --- a/reflect/Emit/TypeBuilder.cs +++ b/reflect/Emit/TypeBuilder.cs @@ -611,31 +611,26 @@ namespace IKVM.Reflection.Emit public void SetCustomAttribute(CustomAttributeBuilder customBuilder) { - Universe u = this.ModuleBuilder.universe; - Type type = customBuilder.Constructor.DeclaringType; - if (type == u.System_Runtime_InteropServices_StructLayoutAttribute) + switch (customBuilder.KnownCA) { - SetStructLayoutPseudoCustomAttribute(customBuilder.DecodeBlob(this.Assembly)); - } - else if (type == u.System_SerializableAttribute) - { - attribs |= TypeAttributes.Serializable; - } - else if (type == u.System_Runtime_InteropServices_ComImportAttribute) - { - attribs |= TypeAttributes.Import; - } - else if (type == u.System_Runtime_CompilerServices_SpecialNameAttribute) - { - attribs |= TypeAttributes.SpecialName; - } - else - { - if (type == u.System_Security_SuppressUnmanagedCodeSecurityAttribute) - { + case KnownCA.StructLayoutAttribute: + SetStructLayoutPseudoCustomAttribute(customBuilder.DecodeBlob(this.Assembly)); + break; + case KnownCA.SerializableAttribute: + attribs |= TypeAttributes.Serializable; + break; + case KnownCA.ComImportAttribute: + attribs |= TypeAttributes.Import; + break; + case KnownCA.SpecialNameAttribute: + attribs |= TypeAttributes.SpecialName; + break; + case KnownCA.SuppressUnmanagedCodeSecurityAttribute: attribs |= TypeAttributes.HasSecurity; - } - this.ModuleBuilder.SetCustomAttribute(token, customBuilder); + goto default; + default: + this.ModuleBuilder.SetCustomAttribute(token, customBuilder); + break; } } diff --git a/reflect/TypeNameParser.cs b/reflect/TypeNameParser.cs index db97373b..1d31952a 100644 --- a/reflect/TypeNameParser.cs +++ b/reflect/TypeNameParser.cs @@ -87,6 +87,21 @@ namespace IKVM.Reflection return this == other; } + internal bool Matches(string fullName) + { + if (ns == null) + { + return name == fullName; + } + if (ns.Length + 1 + name.Length == fullName.Length) + { + return fullName.StartsWith(ns, StringComparison.Ordinal) + && fullName[ns.Length] == '.' + && fullName.EndsWith(name, StringComparison.Ordinal); + } + return false; + } + internal TypeName ToLowerInvariant() { return new TypeName(ns == null ? null : ns.ToLowerInvariant(), name.ToLowerInvariant()); diff --git a/reflect/Universe.cs b/reflect/Universe.cs index 82fad202..65d25c10 100644 --- a/reflect/Universe.cs +++ b/reflect/Universe.cs @@ -136,8 +136,6 @@ namespace IKVM.Reflection DisableWindowsRuntimeProjection = 64, DecodeVersionInfoAttributeBlobs = 128, DeterministicOutput = 256, - - SupressReferenceTypeIdentityConversion = 1 << 20 } public sealed class Universe : IDisposable @@ -180,26 +178,16 @@ namespace IKVM.Reflection private Type typeof_System_DateTime; private Type typeof_System_DBNull; private Type typeof_System_Decimal; - private Type typeof_System_NonSerializedAttribute; - private Type typeof_System_SerializableAttribute; private Type typeof_System_AttributeUsageAttribute; private Type typeof_System_Runtime_InteropServices_DllImportAttribute; private Type typeof_System_Runtime_InteropServices_FieldOffsetAttribute; - private Type typeof_System_Runtime_InteropServices_InAttribute; private Type typeof_System_Runtime_InteropServices_MarshalAsAttribute; private Type typeof_System_Runtime_InteropServices_UnmanagedType; private Type typeof_System_Runtime_InteropServices_VarEnum; - private Type typeof_System_Runtime_InteropServices_OutAttribute; - private Type typeof_System_Runtime_InteropServices_StructLayoutAttribute; - private Type typeof_System_Runtime_InteropServices_OptionalAttribute; private Type typeof_System_Runtime_InteropServices_PreserveSigAttribute; private Type typeof_System_Runtime_InteropServices_CallingConvention; private Type typeof_System_Runtime_InteropServices_CharSet; - private Type typeof_System_Runtime_InteropServices_ComImportAttribute; private Type typeof_System_Runtime_CompilerServices_DecimalConstantAttribute; - private Type typeof_System_Runtime_CompilerServices_SpecialNameAttribute; - private Type typeof_System_Runtime_CompilerServices_MethodImplAttribute; - private Type typeof_System_Security_SuppressUnmanagedCodeSecurityAttribute; private Type typeof_System_Reflection_AssemblyCopyrightAttribute; private Type typeof_System_Reflection_AssemblyTrademarkAttribute; private Type typeof_System_Reflection_AssemblyProductAttribute; @@ -399,16 +387,6 @@ namespace IKVM.Reflection get { return typeof_System_Decimal ?? (typeof_System_Decimal = ImportMscorlibType("System", "Decimal")); } } - internal Type System_NonSerializedAttribute - { - get { return typeof_System_NonSerializedAttribute ?? (typeof_System_NonSerializedAttribute = ImportMscorlibType("System", "NonSerializedAttribute")); } - } - - internal Type System_SerializableAttribute - { - get { return typeof_System_SerializableAttribute ?? (typeof_System_SerializableAttribute = ImportMscorlibType("System", "SerializableAttribute")); } - } - internal Type System_AttributeUsageAttribute { get { return typeof_System_AttributeUsageAttribute ?? (typeof_System_AttributeUsageAttribute = ImportMscorlibType("System", "AttributeUsageAttribute")); } @@ -424,11 +402,6 @@ namespace IKVM.Reflection get { return typeof_System_Runtime_InteropServices_FieldOffsetAttribute ?? (typeof_System_Runtime_InteropServices_FieldOffsetAttribute = ImportMscorlibType("System.Runtime.InteropServices", "FieldOffsetAttribute")); } } - internal Type System_Runtime_InteropServices_InAttribute - { - get { return typeof_System_Runtime_InteropServices_InAttribute ?? (typeof_System_Runtime_InteropServices_InAttribute = ImportMscorlibType("System.Runtime.InteropServices", "InAttribute")); } - } - internal Type System_Runtime_InteropServices_MarshalAsAttribute { get { return typeof_System_Runtime_InteropServices_MarshalAsAttribute ?? (typeof_System_Runtime_InteropServices_MarshalAsAttribute = ImportMscorlibType("System.Runtime.InteropServices", "MarshalAsAttribute")); } @@ -444,21 +417,6 @@ namespace IKVM.Reflection get { return typeof_System_Runtime_InteropServices_VarEnum ?? (typeof_System_Runtime_InteropServices_VarEnum = ImportMscorlibType("System.Runtime.InteropServices", "VarEnum")); } } - internal Type System_Runtime_InteropServices_OutAttribute - { - get { return typeof_System_Runtime_InteropServices_OutAttribute ?? (typeof_System_Runtime_InteropServices_OutAttribute = ImportMscorlibType("System.Runtime.InteropServices", "OutAttribute")); } - } - - internal Type System_Runtime_InteropServices_StructLayoutAttribute - { - get { return typeof_System_Runtime_InteropServices_StructLayoutAttribute ?? (typeof_System_Runtime_InteropServices_StructLayoutAttribute = ImportMscorlibType("System.Runtime.InteropServices", "StructLayoutAttribute")); } - } - - internal Type System_Runtime_InteropServices_OptionalAttribute - { - get { return typeof_System_Runtime_InteropServices_OptionalAttribute ?? (typeof_System_Runtime_InteropServices_OptionalAttribute = ImportMscorlibType("System.Runtime.InteropServices", "OptionalAttribute")); } - } - internal Type System_Runtime_InteropServices_PreserveSigAttribute { get { return typeof_System_Runtime_InteropServices_PreserveSigAttribute ?? (typeof_System_Runtime_InteropServices_PreserveSigAttribute = ImportMscorlibType("System.Runtime.InteropServices", "PreserveSigAttribute")); } @@ -474,31 +432,11 @@ namespace IKVM.Reflection get { return typeof_System_Runtime_InteropServices_CharSet ?? (typeof_System_Runtime_InteropServices_CharSet = ImportMscorlibType("System.Runtime.InteropServices", "CharSet")); } } - internal Type System_Runtime_InteropServices_ComImportAttribute - { - get { return typeof_System_Runtime_InteropServices_ComImportAttribute ?? (typeof_System_Runtime_InteropServices_ComImportAttribute = ImportMscorlibType("System.Runtime.InteropServices", "ComImportAttribute")); } - } - internal Type System_Runtime_CompilerServices_DecimalConstantAttribute { get { return typeof_System_Runtime_CompilerServices_DecimalConstantAttribute ?? (typeof_System_Runtime_CompilerServices_DecimalConstantAttribute = ImportMscorlibType("System.Runtime.CompilerServices", "DecimalConstantAttribute")); } } - internal Type System_Runtime_CompilerServices_SpecialNameAttribute - { - get { return typeof_System_Runtime_CompilerServices_SpecialNameAttribute ?? (typeof_System_Runtime_CompilerServices_SpecialNameAttribute = ImportMscorlibType("System.Runtime.CompilerServices", "SpecialNameAttribute")); } - } - - internal Type System_Runtime_CompilerServices_MethodImplAttribute - { - get { return typeof_System_Runtime_CompilerServices_MethodImplAttribute ?? (typeof_System_Runtime_CompilerServices_MethodImplAttribute = ImportMscorlibType("System.Runtime.CompilerServices", "MethodImplAttribute")); } - } - - internal Type System_Security_SuppressUnmanagedCodeSecurityAttribute - { - get { return typeof_System_Security_SuppressUnmanagedCodeSecurityAttribute ?? (typeof_System_Security_SuppressUnmanagedCodeSecurityAttribute = ImportMscorlibType("System.Security", "SuppressUnmanagedCodeSecurityAttribute")); } - } - internal Type System_Reflection_AssemblyCopyrightAttribute { get { return typeof_System_Reflection_AssemblyCopyrightAttribute ?? (typeof_System_Reflection_AssemblyCopyrightAttribute = ImportMscorlibType("System.Reflection", "AssemblyCopyrightAttribute")); } @@ -1254,11 +1192,6 @@ namespace IKVM.Reflection get { return (options & UniverseOptions.DecodeVersionInfoAttributeBlobs) != 0; } } - internal bool SupressReferenceTypeIdentityConversion - { - get { return (options & UniverseOptions.SupressReferenceTypeIdentityConversion) != 0; } - } - internal bool Deterministic { get { return (options & UniverseOptions.DeterministicOutput) != 0; } |