diff options
author | Jo Shields <directhex@apebox.org> | 2014-02-19 22:12:43 +0000 |
---|---|---|
committer | Jo Shields <directhex@apebox.org> | 2014-02-19 22:12:43 +0000 |
commit | 9972bf87b4f27d9c8f358ef8414ac1ab957a2f0f (patch) | |
tree | 5bb230c1d698659115f918e243c1d4b0aa4c7f51 /external/ikvm/reflect/Emit | |
parent | d0a215f5626219ff7927f576588a777e5331c7be (diff) | |
download | mono-upstream/3.2.8+dfsg.tar.gz |
Imported Upstream version 3.2.8+dfsgupstream/3.2.8+dfsg
Diffstat (limited to 'external/ikvm/reflect/Emit')
-rw-r--r-- | external/ikvm/reflect/Emit/AssemblyBuilder.cs | 75 | ||||
-rw-r--r-- | external/ikvm/reflect/Emit/CustomAttributeBuilder.cs | 39 | ||||
-rw-r--r-- | external/ikvm/reflect/Emit/ExceptionHandler.cs | 2 | ||||
-rw-r--r-- | external/ikvm/reflect/Emit/ILGenerator.cs | 7 | ||||
-rw-r--r-- | external/ikvm/reflect/Emit/ModuleBuilder.cs | 48 | ||||
-rw-r--r-- | external/ikvm/reflect/Emit/TypeBuilder.cs | 56 |
6 files changed, 103 insertions, 124 deletions
diff --git a/external/ikvm/reflect/Emit/AssemblyBuilder.cs b/external/ikvm/reflect/Emit/AssemblyBuilder.cs index 910a6db8a7..7732c57ab3 100644 --- a/external/ikvm/reflect/Emit/AssemblyBuilder.cs +++ b/external/ikvm/reflect/Emit/AssemblyBuilder.cs @@ -1,5 +1,5 @@ /* - Copyright (C) 2008-2012 Jeroen Frijters + Copyright (C) 2008-2013 Jeroen Frijters This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages @@ -49,9 +49,6 @@ namespace IKVM.Reflection.Emit private StrongNameKeyPair keyPair; private byte[] publicKey; internal readonly string dir; - private readonly PermissionSet requiredPermissions; - private readonly PermissionSet optionalPermissions; - private readonly PermissionSet refusedPermissions; private PEFileKinds fileKind = PEFileKinds.Dll; private MethodInfo entryPoint; private VersionInfo versionInfo; @@ -66,7 +63,19 @@ namespace IKVM.Reflection.Emit private readonly List<Module> addedModules = new List<Module>(); private readonly List<CustomAttributeBuilder> customAttributes = new List<CustomAttributeBuilder>(); private readonly List<CustomAttributeBuilder> declarativeSecurity = new List<CustomAttributeBuilder>(); - private readonly List<Type> typeForwarders = new List<Type>(); + private readonly List<TypeForwarder> typeForwarders = new List<TypeForwarder>(); + + struct TypeForwarder + { + internal readonly Type Type; + internal readonly bool IncludeNested; + + internal TypeForwarder(Type type, bool includeNested) + { + this.Type = type; + this.IncludeNested = includeNested; + } + } private struct ResourceFile { @@ -76,7 +85,7 @@ namespace IKVM.Reflection.Emit internal ResourceWriter Writer; } - internal AssemblyBuilder(Universe universe, AssemblyName name, string dir, PermissionSet requiredPermissions, PermissionSet optionalPermissions, PermissionSet refusedPermissions) + internal AssemblyBuilder(Universe universe, AssemblyName name, string dir, IEnumerable<CustomAttributeBuilder> customAttributes) : base(universe) { this.name = name.Name; @@ -105,9 +114,10 @@ namespace IKVM.Reflection.Emit } } this.dir = dir ?? "."; - this.requiredPermissions = requiredPermissions; - this.optionalPermissions = optionalPermissions; - this.refusedPermissions = refusedPermissions; + if (customAttributes != null) + { + this.customAttributes.AddRange(customAttributes); + } if (universe.HasMscorlib && !universe.Mscorlib.__IsMissing && universe.Mscorlib.ImageRuntimeVersion != null) { this.imageRuntimeVersion = universe.Mscorlib.ImageRuntimeVersion; @@ -116,6 +126,7 @@ namespace IKVM.Reflection.Emit { this.imageRuntimeVersion = typeof(object).Assembly.ImageRuntimeVersion; } + universe.RegisterDynamicAssembly(this); } private void SetVersionHelper(Version version) @@ -266,7 +277,12 @@ namespace IKVM.Reflection.Emit public void __AddTypeForwarder(Type type) { - typeForwarders.Add(type); + __AddTypeForwarder(type, true); + } + + public void __AddTypeForwarder(Type type, bool includeNested) + { + typeForwarders.Add(new TypeForwarder(type, includeNested)); } public void SetEntryPoint(MethodInfo entryMethod) @@ -344,26 +360,7 @@ namespace IKVM.Reflection.Emit { assemblyRecord.Culture = manifestModule.Strings.Add(culture); } - int token = 0x20000000 + manifestModule.AssemblyTable.AddRecord(assemblyRecord); - -#pragma warning disable 618 - // this values are obsolete, but we already know that so we disable the warning - System.Security.Permissions.SecurityAction requestMinimum = System.Security.Permissions.SecurityAction.RequestMinimum; - System.Security.Permissions.SecurityAction requestOptional = System.Security.Permissions.SecurityAction.RequestOptional; - System.Security.Permissions.SecurityAction requestRefuse = System.Security.Permissions.SecurityAction.RequestRefuse; -#pragma warning restore 618 - if (requiredPermissions != null) - { - manifestModule.AddDeclarativeSecurity(token, requestMinimum, requiredPermissions); - } - if (optionalPermissions != null) - { - manifestModule.AddDeclarativeSecurity(token, requestOptional, optionalPermissions); - } - if (refusedPermissions != null) - { - manifestModule.AddDeclarativeSecurity(token, requestRefuse, refusedPermissions); - } + manifestModule.AssemblyTable.AddRecord(assemblyRecord); ResourceSection unmanagedResources = versionInfo != null || win32icon != null || win32manifest != null || win32resources != null ? new ResourceSection() @@ -409,9 +406,9 @@ namespace IKVM.Reflection.Emit manifestModule.AddDeclarativeSecurity(0x20000001, declarativeSecurity); - foreach (Type type in typeForwarders) + foreach (TypeForwarder fwd in typeForwarders) { - manifestModule.AddTypeForwarder(type); + manifestModule.AddTypeForwarder(fwd.Type, fwd.IncludeNested); } foreach (ResourceFile resfile in resourceFiles) @@ -703,14 +700,14 @@ namespace IKVM.Reflection.Emit { foreach (ModuleBuilder module in modules) { - if (module.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)) + if (module.Name.Equals(name, StringComparison.OrdinalIgnoreCase)) { return module; } } foreach (Module module in addedModules) { - if (module.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)) + if (module.Name.Equals(name, StringComparison.OrdinalIgnoreCase)) { return module; } @@ -745,6 +742,16 @@ namespace IKVM.Reflection.Emit get { return true; } } + public static AssemblyBuilder DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access) + { + return new Universe().DefineDynamicAssembly(name, access); + } + + public static AssemblyBuilder DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access, IEnumerable<CustomAttributeBuilder> assemblyAttributes) + { + return new Universe().DefineDynamicAssembly(name, access, assemblyAttributes); + } + internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType) { List<CustomAttributeData> list = new List<CustomAttributeData>(); diff --git a/external/ikvm/reflect/Emit/CustomAttributeBuilder.cs b/external/ikvm/reflect/Emit/CustomAttributeBuilder.cs index 2b9708b5c1..4a1a14359b 100644 --- a/external/ikvm/reflect/Emit/CustomAttributeBuilder.cs +++ b/external/ikvm/reflect/Emit/CustomAttributeBuilder.cs @@ -1,5 +1,5 @@ /* - Copyright (C) 2008-2011 Jeroen Frijters + Copyright (C) 2008-2013 Jeroen Frijters This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages @@ -32,6 +32,7 @@ namespace IKVM.Reflection.Emit { public sealed class CustomAttributeBuilder { + internal static readonly ConstructorInfo LegacyPermissionSet = new ConstructorBuilder(null); private readonly ConstructorInfo con; private readonly byte[] blob; private readonly object[] constructorArgs; @@ -460,11 +461,6 @@ namespace IKVM.Reflection.Emit } } - internal bool IsPseudoCustomAttribute - { - get { return con.DeclaringType.IsPseudoCustomAttribute; } - } - internal ConstructorInfo Constructor { get { return con; } @@ -537,18 +533,31 @@ namespace IKVM.Reflection.Emit return null; } - internal string GetLegacyDeclSecurity() + internal bool IsLegacyDeclSecurity { - if (con.DeclaringType == con.Module.universe.System_Security_Permissions_PermissionSetAttribute - && blob == null - && (namedFields == null || namedFields.Length == 0) - && namedProperties != null - && namedProperties.Length == 1 - && namedProperties[0].Name == "XML") + get { - return propertyValues[0] as string; + return ReferenceEquals(con, LegacyPermissionSet) + || (con.DeclaringType == con.Module.universe.System_Security_Permissions_PermissionSetAttribute + && blob == null + && (namedFields == null || namedFields.Length == 0) + && namedProperties != null + && namedProperties.Length == 1 + && namedProperties[0].Name == "XML" + && propertyValues[0] is string); + } + } + + internal int WriteLegacyDeclSecurityBlob(ModuleBuilder moduleBuilder) + { + if (blob != null) + { + return moduleBuilder.Blobs.Add(ByteBuffer.Wrap(blob)); + } + else + { + return moduleBuilder.Blobs.Add(ByteBuffer.Wrap(Encoding.Unicode.GetBytes((string)propertyValues[0]))); } - return null; } internal void WriteNamedArgumentsForDeclSecurity(ModuleBuilder moduleBuilder, ByteBuffer bb) diff --git a/external/ikvm/reflect/Emit/ExceptionHandler.cs b/external/ikvm/reflect/Emit/ExceptionHandler.cs index 40837a97a4..013f925917 100644 --- a/external/ikvm/reflect/Emit/ExceptionHandler.cs +++ b/external/ikvm/reflect/Emit/ExceptionHandler.cs @@ -100,7 +100,7 @@ namespace IKVM.Reflection.Emit public override bool Equals(object obj) { ExceptionHandler? other = obj as ExceptionHandler?; - return other != null && Equals(other); + return other != null && Equals(other.Value); } public override int GetHashCode() diff --git a/external/ikvm/reflect/Emit/ILGenerator.cs b/external/ikvm/reflect/Emit/ILGenerator.cs index b7be3f3a98..d7bb690462 100644 --- a/external/ikvm/reflect/Emit/ILGenerator.cs +++ b/external/ikvm/reflect/Emit/ILGenerator.cs @@ -794,8 +794,11 @@ namespace IKVM.Reflection.Emit { SignatureHelper sig = SignatureHelper.GetMethodSigHelper(moduleBuilder, callingConvention, returnType); sig.AddArguments(parameterTypes, null, null); - sig.AddSentinel(); - sig.AddArguments(optionalParameterTypes, null, null); + if (optionalParameterTypes != null && optionalParameterTypes.Length != 0) + { + sig.AddSentinel(); + sig.AddArguments(optionalParameterTypes, null, null); + } Emit(opc, sig); } diff --git a/external/ikvm/reflect/Emit/ModuleBuilder.cs b/external/ikvm/reflect/Emit/ModuleBuilder.cs index 7e0589297a..7d86ca4bc1 100644 --- a/external/ikvm/reflect/Emit/ModuleBuilder.cs +++ b/external/ikvm/reflect/Emit/ModuleBuilder.cs @@ -140,7 +140,7 @@ namespace IKVM.Reflection.Emit public override bool Equals(object obj) { MemberRefKey? other = obj as MemberRefKey?; - return other != null && Equals(other); + return other != null && Equals(other.Value); } public override int GetHashCode() @@ -180,7 +180,7 @@ namespace IKVM.Reflection.Emit public override bool Equals(object obj) { MethodSpecKey? other = obj as MethodSpecKey?; - return other != null && Equals(other); + return other != null && Equals(other.Value); } public override int GetHashCode() @@ -384,16 +384,16 @@ namespace IKVM.Reflection.Emit moduleType.CreateType(); } - internal void AddTypeForwarder(Type type) + internal void AddTypeForwarder(Type type, bool includeNested) { ExportType(type); - if (!type.__IsMissing) + if (includeNested && !type.__IsMissing) { foreach (Type nested in type.GetNestedTypes(BindingFlags.Public | BindingFlags.NonPublic)) { // we export all nested types (i.e. even the private ones) // (this behavior is the same as the C# compiler) - AddTypeForwarder(nested); + AddTypeForwarder(nested, true); } } } @@ -401,8 +401,11 @@ namespace IKVM.Reflection.Emit private int ExportType(Type type) { ExportedTypeTable.Record rec = new ExportedTypeTable.Record(); - // HACK we should *not* set the TypeDefId in this case, but 2.0 and 3.5 peverify gives a warning if it is missing (4.5 doesn't) - rec.TypeDefId = type.MetadataToken; + if (asm.ImageRuntimeVersion == "v2.0.50727") + { + // HACK we should *not* set the TypeDefId in this case, but 2.0 and 3.5 peverify gives a warning if it is missing (4.5 doesn't) + rec.TypeDefId = type.MetadataToken; + } rec.TypeName = this.Strings.Add(type.__Name); string ns = type.__Namespace; rec.TypeNamespace = ns == null ? 0 : this.Strings.Add(ns); @@ -431,7 +434,6 @@ namespace IKVM.Reflection.Emit internal void SetCustomAttribute(int token, CustomAttributeBuilder customBuilder) { - Debug.Assert(!customBuilder.IsPseudoCustomAttribute); CustomAttributeTable.Record rec = new CustomAttributeTable.Record(); rec.Parent = token; rec.Type = asm.IsWindowsRuntime ? customBuilder.Constructor.ImportTo(this) : GetConstructorToken(customBuilder.Constructor).Token; @@ -439,16 +441,21 @@ namespace IKVM.Reflection.Emit this.CustomAttribute.AddRecord(rec); } - internal void AddDeclarativeSecurity(int token, System.Security.Permissions.SecurityAction securityAction, System.Security.PermissionSet permissionSet) + private void AddDeclSecurityRecord(int token, int action, int blob) { DeclSecurityTable.Record rec = new DeclSecurityTable.Record(); - rec.Action = (short)securityAction; + rec.Action = (short)action; rec.Parent = token; - // like Ref.Emit, we're using the .NET 1.x xml format - rec.PermissionSet = this.Blobs.Add(ByteBuffer.Wrap(System.Text.Encoding.Unicode.GetBytes(permissionSet.ToXml().ToString()))); + rec.PermissionSet = blob; this.DeclSecurity.AddRecord(rec); } + internal void AddDeclarativeSecurity(int token, System.Security.Permissions.SecurityAction securityAction, System.Security.PermissionSet permissionSet) + { + // like Ref.Emit, we're using the .NET 1.x xml format + AddDeclSecurityRecord(token, (int)securityAction, this.Blobs.Add(ByteBuffer.Wrap(System.Text.Encoding.Unicode.GetBytes(permissionSet.ToXml().ToString())))); + } + internal void AddDeclarativeSecurity(int token, List<CustomAttributeBuilder> declarativeSecurity) { Dictionary<int, List<CustomAttributeBuilder>> ordered = new Dictionary<int, List<CustomAttributeBuilder>>(); @@ -464,6 +471,11 @@ namespace IKVM.Reflection.Emit { action = (int)cab.GetConstructorArgument(0); } + if (cab.IsLegacyDeclSecurity) + { + AddDeclSecurityRecord(token, action, cab.WriteLegacyDeclSecurityBlob(this)); + continue; + } List<CustomAttributeBuilder> list; if (!ordered.TryGetValue(action, out list)) { @@ -474,22 +486,12 @@ namespace IKVM.Reflection.Emit } foreach (KeyValuePair<int, List<CustomAttributeBuilder>> kv in ordered) { - DeclSecurityTable.Record rec = new DeclSecurityTable.Record(); - rec.Action = (short)kv.Key; - rec.Parent = token; - rec.PermissionSet = WriteDeclSecurityBlob(kv.Value); - this.DeclSecurity.AddRecord(rec); + AddDeclSecurityRecord(token, kv.Key, WriteDeclSecurityBlob(kv.Value)); } } private int WriteDeclSecurityBlob(List<CustomAttributeBuilder> list) { - string xml; - if (list.Count == 1 && (xml = list[0].GetLegacyDeclSecurity()) != null) - { - // write .NET 1.1 format - return this.Blobs.Add(ByteBuffer.Wrap(System.Text.Encoding.Unicode.GetBytes(xml))); - } ByteBuffer namedArgs = new ByteBuffer(100); ByteBuffer bb = new ByteBuffer(list.Count * 100); bb.Write((byte)'.'); diff --git a/external/ikvm/reflect/Emit/TypeBuilder.cs b/external/ikvm/reflect/Emit/TypeBuilder.cs index aa6d264290..30cde68c8e 100644 --- a/external/ikvm/reflect/Emit/TypeBuilder.cs +++ b/external/ikvm/reflect/Emit/TypeBuilder.cs @@ -571,12 +571,11 @@ namespace IKVM.Reflection.Emit { layout = (LayoutKind)val; } - StructLayoutAttribute attr = new StructLayoutAttribute(layout); - attr.Pack = (int?)customBuilder.GetFieldValue("Pack") ?? 0; - attr.Size = (int?)customBuilder.GetFieldValue("Size") ?? 0; - attr.CharSet = customBuilder.GetFieldValue<CharSet>("CharSet") ?? CharSet.None; + pack = (short)((int?)customBuilder.GetFieldValue("Pack") ?? 0); + size = (int?)customBuilder.GetFieldValue("Size") ?? 0; + CharSet charSet = customBuilder.GetFieldValue<CharSet>("CharSet") ?? CharSet.None; attribs &= ~TypeAttributes.LayoutMask; - switch (attr.Value) + switch (layout) { case LayoutKind.Auto: attribs |= TypeAttributes.AutoLayout; @@ -589,7 +588,7 @@ namespace IKVM.Reflection.Emit break; } attribs &= ~TypeAttributes.StringFormatMask; - switch (attr.CharSet) + switch (charSet) { case CharSet.None: case CharSet.Ansi: @@ -602,8 +601,6 @@ namespace IKVM.Reflection.Emit attribs |= TypeAttributes.UnicodeClass; break; } - pack = (short)attr.Pack; - size = attr.Size; hasLayout = pack != 0 || size != 0; } @@ -870,45 +867,6 @@ namespace IKVM.Reflection.Emit return methods; } - public override StructLayoutAttribute StructLayoutAttribute - { - get - { - LayoutKind layout; - switch (attribs & TypeAttributes.LayoutMask) - { - case TypeAttributes.ExplicitLayout: - layout = LayoutKind.Explicit; - break; - case TypeAttributes.SequentialLayout: - layout = LayoutKind.Sequential; - break; - default: - layout = LayoutKind.Auto; - break; - } - StructLayoutAttribute attr = new StructLayoutAttribute(layout); - attr.Pack = (ushort)pack; - attr.Size = size; - switch (attribs & TypeAttributes.StringFormatMask) - { - case TypeAttributes.AutoClass: - attr.CharSet = CharSet.Auto; - break; - case TypeAttributes.UnicodeClass: - attr.CharSet = CharSet.Unicode; - break; - case TypeAttributes.AnsiClass: - attr.CharSet = CharSet.Ansi; - break; - default: - attr.CharSet = CharSet.None; - break; - } - return attr; - } - } - public override Type DeclaringType { get { return owner as TypeBuilder; } @@ -1194,9 +1152,9 @@ namespace IKVM.Reflection.Emit get { return underlyingType.DeclaringType; } } - public override StructLayoutAttribute StructLayoutAttribute + public override bool __GetLayout(out int packingSize, out int typeSize) { - get { return underlyingType.StructLayoutAttribute; } + return underlyingType.__GetLayout(out packingSize, out typeSize); } public override Type[] GetGenericArguments() |