diff --git a/src/IKVM.Reflection/Emit/BakedType.cs b/src/IKVM.Reflection/Emit/BakedType.cs new file mode 100644 index 0000000000..51751dbbf0 --- /dev/null +++ b/src/IKVM.Reflection/Emit/BakedType.cs @@ -0,0 +1,164 @@ +/* + Copyright (C) 2008-2011 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 + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. + + Jeroen Frijters + jeroen@frijters.net + +*/ +namespace IKVM.Reflection.Emit +{ + sealed class BakedType : TypeInfo + { + internal BakedType(TypeBuilder typeBuilder) + : base(typeBuilder) + { + } + + public override string AssemblyQualifiedName + { + get { return underlyingType.AssemblyQualifiedName; } + } + + public override Type BaseType + { + get { return underlyingType.BaseType; } + } + + internal override TypeName TypeName + { + get { return underlyingType.TypeName; } + } + + public override string Name + { + // we need to escape here, because TypeBuilder.Name does not escape + get { return TypeNameParser.Escape(underlyingType.__Name); } + } + + public override string FullName + { + get { return GetFullName(); } + } + + public override TypeAttributes Attributes + { + get { return underlyingType.Attributes; } + } + + public override Type[] __GetDeclaredInterfaces() + { + return underlyingType.__GetDeclaredInterfaces(); + } + + public override MethodBase[] __GetDeclaredMethods() + { + return underlyingType.__GetDeclaredMethods(); + } + + public override __MethodImplMap __GetMethodImplMap() + { + return underlyingType.__GetMethodImplMap(); + } + + public override FieldInfo[] __GetDeclaredFields() + { + return underlyingType.__GetDeclaredFields(); + } + + public override EventInfo[] __GetDeclaredEvents() + { + return underlyingType.__GetDeclaredEvents(); + } + + public override PropertyInfo[] __GetDeclaredProperties() + { + return underlyingType.__GetDeclaredProperties(); + } + + public override Type[] __GetDeclaredTypes() + { + return underlyingType.__GetDeclaredTypes(); + } + + public override Type DeclaringType + { + get { return underlyingType.DeclaringType; } + } + + public override bool __GetLayout(out int packingSize, out int typeSize) + { + return underlyingType.__GetLayout(out packingSize, out typeSize); + } + + public override Type[] GetGenericArguments() + { + return underlyingType.GetGenericArguments(); + } + + internal override Type GetGenericTypeArgument(int index) + { + return underlyingType.GetGenericTypeArgument(index); + } + + public override CustomModifiers[] __GetGenericArgumentsCustomModifiers() + { + return underlyingType.__GetGenericArgumentsCustomModifiers(); + } + + public override bool IsGenericType + { + get { return underlyingType.IsGenericType; } + } + + public override bool IsGenericTypeDefinition + { + get { return underlyingType.IsGenericTypeDefinition; } + } + + public override bool ContainsGenericParameters + { + get { return underlyingType.ContainsGenericParameters; } + } + + public override int MetadataToken + { + get { return underlyingType.MetadataToken; } + } + + public override Module Module + { + get { return underlyingType.Module; } + } + + internal override int GetModuleBuilderToken() + { + return underlyingType.GetModuleBuilderToken(); + } + + internal override bool IsBaked + { + get { return true; } + } + + protected override bool IsValueTypeImpl + { + get { return underlyingType.IsValueType; } + } + } +} diff --git a/src/IKVM.Reflection/Emit/GenericTypeParameterBuilder.cs b/src/IKVM.Reflection/Emit/GenericTypeParameterBuilder.cs new file mode 100644 index 0000000000..6f7aa6ce01 --- /dev/null +++ b/src/IKVM.Reflection/Emit/GenericTypeParameterBuilder.cs @@ -0,0 +1,256 @@ +/* + Copyright (C) 2008-2011 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 + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. + + Jeroen Frijters + jeroen@frijters.net + +*/ +using System; + +using IKVM.Reflection.Metadata; +using IKVM.Reflection.Writer; + +namespace IKVM.Reflection.Emit +{ + public sealed class GenericTypeParameterBuilder : TypeInfo + { + private readonly string name; + private readonly TypeBuilder type; + private readonly MethodBuilder method; + private readonly int paramPseudoIndex; + private readonly int position; + private int typeToken; + private Type baseType; + private GenericParameterAttributes attr; + + internal GenericTypeParameterBuilder(string name, TypeBuilder type, int position) + : this(name, type, null, position, Signature.ELEMENT_TYPE_VAR) + { + } + + internal GenericTypeParameterBuilder(string name, MethodBuilder method, int position) + : this(name, null, method, position, Signature.ELEMENT_TYPE_MVAR) + { + } + + private GenericTypeParameterBuilder(string name, TypeBuilder type, MethodBuilder method, int position, byte sigElementType) + : base(sigElementType) + { + this.name = name; + this.type = type; + this.method = method; + this.position = position; + GenericParamTable.Record rec = new GenericParamTable.Record(); + rec.Number = (short)position; + rec.Flags = 0; + rec.Owner = type != null ? type.MetadataToken : method.MetadataToken; + rec.Name = this.ModuleBuilder.Strings.Add(name); + this.paramPseudoIndex = this.ModuleBuilder.GenericParam.AddRecord(rec); + } + + public override string AssemblyQualifiedName + { + get { return null; } + } + + protected override bool IsValueTypeImpl + { + get { return (this.GenericParameterAttributes & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0; } + } + + public override Type BaseType + { + get { return baseType; } + } + + public override Type[] __GetDeclaredInterfaces() + { + throw new NotImplementedException(); + } + + public override TypeAttributes Attributes + { + get { return TypeAttributes.Public; } + } + + public override string Namespace + { + get { return DeclaringType.Namespace; } + } + + public override string Name + { + get { return name; } + } + + public override string FullName + { + get { return null; } + } + + public override string ToString() + { + return this.Name; + } + + private ModuleBuilder ModuleBuilder + { + get { return type != null ? type.ModuleBuilder : method.ModuleBuilder; } + } + + public override Module Module + { + get { return ModuleBuilder; } + } + + public override int GenericParameterPosition + { + get { return position; } + } + + public override Type DeclaringType + { + get { return type; } + } + + public override MethodBase DeclaringMethod + { + get { return method; } + } + + public override Type[] GetGenericParameterConstraints() + { + throw new NotImplementedException(); + } + + public override CustomModifiers[] __GetGenericParameterConstraintCustomModifiers() + { + throw new NotImplementedException(); + } + + public override GenericParameterAttributes GenericParameterAttributes + { + get + { + CheckBaked(); + return attr; + } + } + + internal override void CheckBaked() + { + if (type != null) + { + type.CheckBaked(); + } + else + { + method.CheckBaked(); + } + } + + private void AddConstraint(Type type) + { + GenericParamConstraintTable.Record rec = new GenericParamConstraintTable.Record(); + rec.Owner = paramPseudoIndex; + rec.Constraint = this.ModuleBuilder.GetTypeTokenForMemberRef(type); + this.ModuleBuilder.GenericParamConstraint.AddRecord(rec); + } + + public void SetBaseTypeConstraint(Type baseTypeConstraint) + { + this.baseType = baseTypeConstraint; + AddConstraint(baseTypeConstraint); + } + + public void SetInterfaceConstraints(params Type[] interfaceConstraints) + { + foreach (Type type in interfaceConstraints) + { + AddConstraint(type); + } + } + + public void SetGenericParameterAttributes(GenericParameterAttributes genericParameterAttributes) + { + this.attr = genericParameterAttributes; + // for now we'll back patch the table + this.ModuleBuilder.GenericParam.PatchAttribute(paramPseudoIndex, genericParameterAttributes); + } + + public void SetCustomAttribute(CustomAttributeBuilder customBuilder) + { + this.ModuleBuilder.SetCustomAttribute((GenericParamTable.Index << 24) | paramPseudoIndex, customBuilder); + } + + public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) + { + SetCustomAttribute(new CustomAttributeBuilder(con, binaryAttribute)); + } + + public override int MetadataToken + { + get + { + CheckBaked(); + return (GenericParamTable.Index << 24) | paramPseudoIndex; + } + } + + internal override int GetModuleBuilderToken() + { + if (typeToken == 0) + { + ByteBuffer spec = new ByteBuffer(5); + Signature.WriteTypeSpec(this.ModuleBuilder, spec, this); + typeToken = 0x1B000000 | this.ModuleBuilder.TypeSpec.AddRecord(this.ModuleBuilder.Blobs.Add(spec)); + } + return typeToken; + } + + internal override Type BindTypeParameters(IGenericBinder binder) + { + if (type != null) + { + return binder.BindTypeParameter(this); + } + else + { + return binder.BindMethodParameter(this); + } + } + + internal override int GetCurrentToken() + { + if (this.ModuleBuilder.IsSaved) + { + return (GenericParamTable.Index << 24) | this.Module.GenericParam.GetIndexFixup()[paramPseudoIndex - 1] + 1; + } + else + { + return (GenericParamTable.Index << 24) | paramPseudoIndex; + } + } + + internal override bool IsBaked + { + get { return ((MemberInfo)type ?? method).IsBaked; } + } + } +} diff --git a/src/IKVM.Reflection/Emit/MethodBuilder.cs b/src/IKVM.Reflection/Emit/MethodBuilder.cs index 0a6ce547dc..bc25421576 100644 --- a/src/IKVM.Reflection/Emit/MethodBuilder.cs +++ b/src/IKVM.Reflection/Emit/MethodBuilder.cs @@ -32,8 +32,10 @@ Jeroen Frijters namespace IKVM.Reflection.Emit { + public sealed class MethodBuilder : MethodInfo { + private readonly TypeBuilder typeBuilder; private readonly string name; private readonly int pseudoToken; @@ -773,4 +775,5 @@ internal override bool IsBaked get { return typeBuilder.IsBaked; } } } + } diff --git a/src/IKVM.Reflection/Emit/ParameterBuilder.cs b/src/IKVM.Reflection/Emit/ParameterBuilder.cs index 4626496a96..9d1fe637ef 100644 --- a/src/IKVM.Reflection/Emit/ParameterBuilder.cs +++ b/src/IKVM.Reflection/Emit/ParameterBuilder.cs @@ -21,14 +21,14 @@ Jeroen Frijters jeroen@frijters.net */ -using System; using IKVM.Reflection.Writer; -using IKVM.Reflection.Metadata; namespace IKVM.Reflection.Emit { - public sealed class ParameterBuilder + + public sealed class ParameterBuilder { + private readonly ModuleBuilder moduleBuilder; private short flags; private readonly short sequence; @@ -138,4 +138,5 @@ internal void FixupToken(int parameterToken) } } } + } diff --git a/src/IKVM.Reflection/Emit/PropertyBuilder.cs b/src/IKVM.Reflection/Emit/PropertyBuilder.cs index 76661d0bbe..9a10d18fa3 100644 --- a/src/IKVM.Reflection/Emit/PropertyBuilder.cs +++ b/src/IKVM.Reflection/Emit/PropertyBuilder.cs @@ -23,14 +23,15 @@ Jeroen Frijters */ using System; using System.Collections.Generic; -using System.Runtime.CompilerServices; -using IKVM.Reflection.Writer; + using IKVM.Reflection.Metadata; namespace IKVM.Reflection.Emit { - public sealed class PropertyBuilder : PropertyInfo + + public sealed class PropertyBuilder : PropertyInfo { + private readonly TypeBuilder typeBuilder; private readonly string name; private PropertyAttributes attributes; @@ -284,4 +285,5 @@ internal override int GetCurrentToken() } } } + } diff --git a/src/IKVM.Reflection/Emit/SignatureHelper.cs b/src/IKVM.Reflection/Emit/SignatureHelper.cs index 64dc18d280..caa1df77aa 100644 --- a/src/IKVM.Reflection/Emit/SignatureHelper.cs +++ b/src/IKVM.Reflection/Emit/SignatureHelper.cs @@ -24,13 +24,15 @@ Jeroen Frijters using System; using System.Collections.Generic; using System.Runtime.InteropServices; -using IKVM.Reflection; + using IKVM.Reflection.Writer; namespace IKVM.Reflection.Emit { - public abstract class SignatureHelper + + public abstract class SignatureHelper { + protected readonly byte type; protected ushort paramCount; @@ -314,4 +316,5 @@ public void AddArguments(Type[] arguments, Type[][] requiredCustomModifiers, Typ } } } + } diff --git a/src/IKVM.Reflection/Emit/TypeBuilder.cs b/src/IKVM.Reflection/Emit/TypeBuilder.cs index 1234ea5d2b..f63e467d9d 100644 --- a/src/IKVM.Reflection/Emit/TypeBuilder.cs +++ b/src/IKVM.Reflection/Emit/TypeBuilder.cs @@ -23,241 +23,16 @@ Jeroen Frijters */ using System; using System.Collections.Generic; -using System.Diagnostics; using System.Runtime.InteropServices; + using IKVM.Reflection.Impl; using IKVM.Reflection.Metadata; using IKVM.Reflection.Writer; namespace IKVM.Reflection.Emit { - public sealed class GenericTypeParameterBuilder : TypeInfo - { - private readonly string name; - private readonly TypeBuilder type; - private readonly MethodBuilder method; - private readonly int paramPseudoIndex; - private readonly int position; - private int typeToken; - private Type baseType; - private GenericParameterAttributes attr; - - internal GenericTypeParameterBuilder(string name, TypeBuilder type, int position) - : this(name, type, null, position, Signature.ELEMENT_TYPE_VAR) - { - } - - internal GenericTypeParameterBuilder(string name, MethodBuilder method, int position) - : this(name, null, method, position, Signature.ELEMENT_TYPE_MVAR) - { - } - - private GenericTypeParameterBuilder(string name, TypeBuilder type, MethodBuilder method, int position, byte sigElementType) - : base(sigElementType) - { - this.name = name; - this.type = type; - this.method = method; - this.position = position; - GenericParamTable.Record rec = new GenericParamTable.Record(); - rec.Number = (short)position; - rec.Flags = 0; - rec.Owner = type != null ? type.MetadataToken : method.MetadataToken; - rec.Name = this.ModuleBuilder.Strings.Add(name); - this.paramPseudoIndex = this.ModuleBuilder.GenericParam.AddRecord(rec); - } - - public override string AssemblyQualifiedName - { - get { return null; } - } - - protected override bool IsValueTypeImpl - { - get { return (this.GenericParameterAttributes & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0; } - } - - public override Type BaseType - { - get { return baseType; } - } - - public override Type[] __GetDeclaredInterfaces() - { - throw new NotImplementedException(); - } - - public override TypeAttributes Attributes - { - get { return TypeAttributes.Public; } - } - public override string Namespace - { - get { return DeclaringType.Namespace; } - } - - public override string Name - { - get { return name; } - } - - public override string FullName - { - get { return null; } - } - - public override string ToString() - { - return this.Name; - } - - private ModuleBuilder ModuleBuilder - { - get { return type != null ? type.ModuleBuilder : method.ModuleBuilder; } - } - - public override Module Module - { - get { return ModuleBuilder; } - } - - public override int GenericParameterPosition - { - get { return position; } - } - - public override Type DeclaringType - { - get { return type; } - } - - public override MethodBase DeclaringMethod - { - get { return method; } - } - - public override Type[] GetGenericParameterConstraints() - { - throw new NotImplementedException(); - } - - public override CustomModifiers[] __GetGenericParameterConstraintCustomModifiers() - { - throw new NotImplementedException(); - } - - public override GenericParameterAttributes GenericParameterAttributes - { - get - { - CheckBaked(); - return attr; - } - } - - internal override void CheckBaked() - { - if (type != null) - { - type.CheckBaked(); - } - else - { - method.CheckBaked(); - } - } - - private void AddConstraint(Type type) - { - GenericParamConstraintTable.Record rec = new GenericParamConstraintTable.Record(); - rec.Owner = paramPseudoIndex; - rec.Constraint = this.ModuleBuilder.GetTypeTokenForMemberRef(type); - this.ModuleBuilder.GenericParamConstraint.AddRecord(rec); - } - - public void SetBaseTypeConstraint(Type baseTypeConstraint) - { - this.baseType = baseTypeConstraint; - AddConstraint(baseTypeConstraint); - } - - public void SetInterfaceConstraints(params Type[] interfaceConstraints) - { - foreach (Type type in interfaceConstraints) - { - AddConstraint(type); - } - } - - public void SetGenericParameterAttributes(GenericParameterAttributes genericParameterAttributes) - { - this.attr = genericParameterAttributes; - // for now we'll back patch the table - this.ModuleBuilder.GenericParam.PatchAttribute(paramPseudoIndex, genericParameterAttributes); - } - - public void SetCustomAttribute(CustomAttributeBuilder customBuilder) - { - this.ModuleBuilder.SetCustomAttribute((GenericParamTable.Index << 24) | paramPseudoIndex, customBuilder); - } - - public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) - { - SetCustomAttribute(new CustomAttributeBuilder(con, binaryAttribute)); - } - - public override int MetadataToken - { - get - { - CheckBaked(); - return (GenericParamTable.Index << 24) | paramPseudoIndex; - } - } - - internal override int GetModuleBuilderToken() - { - if (typeToken == 0) - { - ByteBuffer spec = new ByteBuffer(5); - Signature.WriteTypeSpec(this.ModuleBuilder, spec, this); - typeToken = 0x1B000000 | this.ModuleBuilder.TypeSpec.AddRecord(this.ModuleBuilder.Blobs.Add(spec)); - } - return typeToken; - } - - internal override Type BindTypeParameters(IGenericBinder binder) - { - if (type != null) - { - return binder.BindTypeParameter(this); - } - else - { - return binder.BindMethodParameter(this); - } - } - - internal override int GetCurrentToken() - { - if (this.ModuleBuilder.IsSaved) - { - return (GenericParamTable.Index << 24) | this.Module.GenericParam.GetIndexFixup()[paramPseudoIndex - 1] + 1; - } - else - { - return (GenericParamTable.Index << 24) | paramPseudoIndex; - } - } - - internal override bool IsBaked - { - get { return ((MemberInfo)type ?? method).IsBaked; } - } - } - - public sealed class TypeBuilder : TypeInfo, ITypeOwner + public sealed class TypeBuilder : TypeInfo, ITypeOwner { public const int UnspecifiedTypeSize = 0; private readonly ITypeOwner owner; @@ -1094,142 +869,4 @@ protected override bool IsValueTypeImpl } } - sealed class BakedType : TypeInfo - { - internal BakedType(TypeBuilder typeBuilder) - : base(typeBuilder) - { - } - - public override string AssemblyQualifiedName - { - get { return underlyingType.AssemblyQualifiedName; } - } - - public override Type BaseType - { - get { return underlyingType.BaseType; } - } - - internal override TypeName TypeName - { - get { return underlyingType.TypeName; } - } - - public override string Name - { - // we need to escape here, because TypeBuilder.Name does not escape - get { return TypeNameParser.Escape(underlyingType.__Name); } - } - - public override string FullName - { - get { return GetFullName(); } - } - - public override TypeAttributes Attributes - { - get { return underlyingType.Attributes; } - } - - public override Type[] __GetDeclaredInterfaces() - { - return underlyingType.__GetDeclaredInterfaces(); - } - - public override MethodBase[] __GetDeclaredMethods() - { - return underlyingType.__GetDeclaredMethods(); - } - - public override __MethodImplMap __GetMethodImplMap() - { - return underlyingType.__GetMethodImplMap(); - } - - public override FieldInfo[] __GetDeclaredFields() - { - return underlyingType.__GetDeclaredFields(); - } - - public override EventInfo[] __GetDeclaredEvents() - { - return underlyingType.__GetDeclaredEvents(); - } - - public override PropertyInfo[] __GetDeclaredProperties() - { - return underlyingType.__GetDeclaredProperties(); - } - - public override Type[] __GetDeclaredTypes() - { - return underlyingType.__GetDeclaredTypes(); - } - - public override Type DeclaringType - { - get { return underlyingType.DeclaringType; } - } - - public override bool __GetLayout(out int packingSize, out int typeSize) - { - return underlyingType.__GetLayout(out packingSize, out typeSize); - } - - public override Type[] GetGenericArguments() - { - return underlyingType.GetGenericArguments(); - } - - internal override Type GetGenericTypeArgument(int index) - { - return underlyingType.GetGenericTypeArgument(index); - } - - public override CustomModifiers[] __GetGenericArgumentsCustomModifiers() - { - return underlyingType.__GetGenericArgumentsCustomModifiers(); - } - - public override bool IsGenericType - { - get { return underlyingType.IsGenericType; } - } - - public override bool IsGenericTypeDefinition - { - get { return underlyingType.IsGenericTypeDefinition; } - } - - public override bool ContainsGenericParameters - { - get { return underlyingType.ContainsGenericParameters; } - } - - public override int MetadataToken - { - get { return underlyingType.MetadataToken; } - } - - public override Module Module - { - get { return underlyingType.Module; } - } - - internal override int GetModuleBuilderToken() - { - return underlyingType.GetModuleBuilderToken(); - } - - internal override bool IsBaked - { - get { return true; } - } - - protected override bool IsValueTypeImpl - { - get { return underlyingType.IsValueType; } - } - } } diff --git a/src/IKVM.Reflection/Emit/UnmanagedExport.cs b/src/IKVM.Reflection/Emit/UnmanagedExport.cs index b01e5ce2b4..263dde42d8 100644 --- a/src/IKVM.Reflection/Emit/UnmanagedExport.cs +++ b/src/IKVM.Reflection/Emit/UnmanagedExport.cs @@ -24,6 +24,7 @@ Jeroen Frijters namespace IKVM.Reflection.Emit { + struct UnmanagedExport { internal string name; @@ -31,4 +32,5 @@ struct UnmanagedExport internal RelativeVirtualAddress rva; internal MethodBuilder mb; } + }