diff options
author | jfrijters <jfrijters> | 2011-12-01 10:30:50 +0400 |
---|---|---|
committer | jfrijters <jfrijters> | 2011-12-01 10:30:50 +0400 |
commit | 3c49c20346609d4c397dff93d25bb68297301516 (patch) | |
tree | 926cbcef8a32139fb0cbaa3ad15e9690351a1379 /reflect/Emit | |
parent | ed23e4ca4ec0e5fc797e47e7dea004941e02e853 (diff) |
- Rewrote custom modifier handling to retain ordering.
- Added ConstructorInfo.__ReturnParameter to expose custom modifiers.
- Added __GetCustomModifiers() to various *Info types.
- Added CustomModifiers type to encapsulate a custom modifier sequence.
- Added CustomModifiersBuilder to create a CustomModifiers sequence.
- Marked a number of IKVM.Reflection specific methods Obsolete, because they are replaced with method that take CustomModifiers value(s).
Diffstat (limited to 'reflect/Emit')
-rw-r--r-- | reflect/Emit/ConstructorBuilder.cs | 8 | ||||
-rw-r--r-- | reflect/Emit/CustomModifiersBuilder.cs | 75 | ||||
-rw-r--r-- | reflect/Emit/FieldBuilder.cs | 4 | ||||
-rw-r--r-- | reflect/Emit/MethodBuilder.cs | 36 | ||||
-rw-r--r-- | reflect/Emit/ModuleBuilder.cs | 8 | ||||
-rw-r--r-- | reflect/Emit/SignatureHelper.cs | 29 | ||||
-rw-r--r-- | reflect/Emit/TypeBuilder.cs | 46 |
7 files changed, 142 insertions, 64 deletions
diff --git a/reflect/Emit/ConstructorBuilder.cs b/reflect/Emit/ConstructorBuilder.cs index 719ed33b..3ce26b02 100644 --- a/reflect/Emit/ConstructorBuilder.cs +++ b/reflect/Emit/ConstructorBuilder.cs @@ -1,5 +1,5 @@ /* - Copyright (C) 2008 Jeroen Frijters + 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 @@ -45,6 +45,12 @@ namespace IKVM.Reflection.Emit return methodBuilder.GetHashCode(); } + public void __SetSignature(Type returnType, CustomModifiers returnTypeCustomModifiers, Type[] parameterTypes, CustomModifiers[] parameterTypeCustomModifiers) + { + methodBuilder.__SetSignature(returnType, returnTypeCustomModifiers, parameterTypes, parameterTypeCustomModifiers); + } + + [Obsolete("Please use __SetSignature(Type, CustomModifiers, Type[], CustomModifiers[]) instead.")] public void __SetSignature(Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers) { methodBuilder.SetSignature(returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers); diff --git a/reflect/Emit/CustomModifiersBuilder.cs b/reflect/Emit/CustomModifiersBuilder.cs new file mode 100644 index 00000000..91f5b834 --- /dev/null +++ b/reflect/Emit/CustomModifiersBuilder.cs @@ -0,0 +1,75 @@ +/* + Copyright (C) 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 System.Collections.Generic; +using System.Text; + +namespace IKVM.Reflection.Emit +{ + public sealed class CustomModifiersBuilder + { + private readonly List<Item> list = new List<Item>(); + + internal struct Item + { + internal Type type; + internal bool required; + } + + public void AddRequired(Type type) + { + Item item; + item.type = type; + item.required = true; + list.Add(item); + } + + public void AddOptional(Type type) + { + Item item; + item.type = type; + item.required = false; + list.Add(item); + } + + // this adds the custom modifiers in the same order as the normal SRE APIs + // (the advantage over using the SRE APIs is that a CustomModifiers object is slightly more efficient, + // because unlike the Type arrays it doesn't need to be copied) + public void Add(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers) + { + foreach (CustomModifiers.Entry entry in CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers)) + { + Item item; + item.type = entry.Type; + item.required = entry.IsRequired; + list.Add(item); + } + } + + public CustomModifiers Create() + { + return new CustomModifiers(list); + } + } +} diff --git a/reflect/Emit/FieldBuilder.cs b/reflect/Emit/FieldBuilder.cs index 65973a32..3a31ec0c 100644 --- a/reflect/Emit/FieldBuilder.cs +++ b/reflect/Emit/FieldBuilder.cs @@ -38,13 +38,13 @@ namespace IKVM.Reflection.Emit private readonly int signature; private readonly FieldSignature fieldSig; - internal FieldBuilder(TypeBuilder type, string name, Type fieldType, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers, FieldAttributes attribs) + internal FieldBuilder(TypeBuilder type, string name, Type fieldType, CustomModifiers customModifiers, FieldAttributes attribs) { this.typeBuilder = type; this.name = name; this.pseudoToken = type.ModuleBuilder.AllocPseudoToken(); this.nameIndex = type.ModuleBuilder.Strings.Add(name); - this.fieldSig = FieldSignature.Create(fieldType, optionalCustomModifiers, requiredCustomModifiers); + this.fieldSig = FieldSignature.Create(fieldType, customModifiers); ByteBuffer sig = new ByteBuffer(5); fieldSig.WriteSig(this.typeBuilder.ModuleBuilder, sig); this.signature = this.typeBuilder.ModuleBuilder.Blobs.Add(sig); diff --git a/reflect/Emit/MethodBuilder.cs b/reflect/Emit/MethodBuilder.cs index f884fbf7..56c04b16 100644 --- a/reflect/Emit/MethodBuilder.cs +++ b/reflect/Emit/MethodBuilder.cs @@ -42,7 +42,7 @@ namespace IKVM.Reflection.Emit private int signature; private Type returnType; private Type[] parameterTypes; - private Type[][][] modifiers; // see PackedCustomModifiers + private PackedCustomModifiers customModifiers; private MethodAttributes attributes; private MethodImplAttributes implFlags; private ILGenerator ilgen; @@ -339,10 +339,20 @@ namespace IKVM.Reflection.Emit public void SetSignature(Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers) { + SetSignature(returnType, parameterTypes, PackedCustomModifiers.CreateFromExternal(returnTypeOptionalCustomModifiers, returnTypeRequiredCustomModifiers, + parameterTypeOptionalCustomModifiers, parameterTypeRequiredCustomModifiers, Util.NullSafeLength(parameterTypes))); + } + + public void __SetSignature(Type returnType, CustomModifiers returnTypeCustomModifiers, Type[] parameterTypes, CustomModifiers[] parameterTypeCustomModifiers) + { + SetSignature(returnType, parameterTypes, PackedCustomModifiers.CreateFromExternal(returnTypeCustomModifiers, parameterTypeCustomModifiers, Util.NullSafeLength(parameterTypes))); + } + + private void SetSignature(Type returnType, Type[] parameterTypes, PackedCustomModifiers customModifiers) + { this.returnType = returnType ?? this.Module.universe.System_Void; this.parameterTypes = Util.Copy(parameterTypes); - this.modifiers = PackedCustomModifiers.CreateFromExternal(returnTypeOptionalCustomModifiers, returnTypeRequiredCustomModifiers, - parameterTypeOptionalCustomModifiers, parameterTypeRequiredCustomModifiers, this.parameterTypes.Length); + this.customModifiers = customModifiers; } public GenericTypeParameterBuilder[] DefineGenericParameters(params string[] names) @@ -488,23 +498,9 @@ namespace IKVM.Reflection.Emit } } - private Type[] GetCustomModifiers(int optOrReq) - { - if (method.modifiers == null || method.modifiers[parameter + 1] == null) - { - return Type.EmptyTypes; - } - return Util.Copy(method.modifiers[parameter + 1][optOrReq]); - } - - public override Type[] GetOptionalCustomModifiers() - { - return GetCustomModifiers(0); - } - - public override Type[] GetRequiredCustomModifiers() + public override CustomModifiers __GetCustomModifiers() { - return GetCustomModifiers(1); + return method.customModifiers.GetParameterCustomModifiers(parameter); } public override MemberInfo Member @@ -671,7 +667,7 @@ namespace IKVM.Reflection.Emit { if (methodSignature == null) { - methodSignature = MethodSignature.MakeFromBuilder(returnType, parameterTypes, modifiers, callingConvention, gtpb == null ? 0 : gtpb.Length); + methodSignature = MethodSignature.MakeFromBuilder(returnType, parameterTypes, customModifiers, callingConvention, gtpb == null ? 0 : gtpb.Length); } return methodSignature; } diff --git a/reflect/Emit/ModuleBuilder.cs b/reflect/Emit/ModuleBuilder.cs index 770612e4..42a091f6 100644 --- a/reflect/Emit/ModuleBuilder.cs +++ b/reflect/Emit/ModuleBuilder.cs @@ -259,6 +259,12 @@ namespace IKVM.Reflection.Emit return new EnumBuilder(tb, fb); } + public FieldBuilder __DefineField(string name, Type type, CustomModifiers customModifiers, FieldAttributes attributes) + { + return moduleType.__DefineField(name, type, customModifiers, attributes); + } + + [Obsolete("Please use __DefineField(string, Type, CustomModifiers, FieldAttributes) instead.")] public FieldBuilder __DefineField(string name, Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers, FieldAttributes attributes) { return moduleType.DefineField(name, type, requiredCustomModifiers, optionalCustomModifiers, attributes); @@ -1627,7 +1633,7 @@ namespace IKVM.Reflection.Emit { if (methodSignature == null) { - methodSignature = MethodSignature.MakeFromBuilder(returnType, parameterTypes, null, callingConvention, 0); + methodSignature = MethodSignature.MakeFromBuilder(returnType, parameterTypes, new PackedCustomModifiers(), callingConvention, 0); } return methodSignature; } diff --git a/reflect/Emit/SignatureHelper.cs b/reflect/Emit/SignatureHelper.cs index 788c3708..f3d82002 100644 --- a/reflect/Emit/SignatureHelper.cs +++ b/reflect/Emit/SignatureHelper.cs @@ -1,5 +1,5 @@ /* - Copyright (C) 2008, 2010 Jeroen Frijters + 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 @@ -35,12 +35,10 @@ namespace IKVM.Reflection.Emit private readonly byte type; private readonly List<Type> args = new List<Type>(); private readonly List<LocalBuilder> locals = new List<LocalBuilder>(); - private readonly List<Type[]> requiredCustomModifiers = new List<Type[]>(); - private readonly List<Type[]> optionalCustomModifiers = new List<Type[]>(); + private readonly List<CustomModifiers> customModifiers = new List<CustomModifiers>(); private readonly List<Type> optionalArgs = new List<Type>(); private Type returnType; - private Type[] returnTypeRequiredCustomModifiers; - private Type[] returnTypeOptionalCustomModifiers; + private CustomModifiers returnTypeCustomModifiers; private CallingConventions callingConvention; private CallingConvention unmanagedCallConv; private bool unmanaged; @@ -86,8 +84,6 @@ namespace IKVM.Reflection.Emit { SignatureHelper sig = new SignatureHelper(mod as ModuleBuilder, Signature.PROPERTY); sig.returnType = returnType; - sig.returnTypeOptionalCustomModifiers = Type.EmptyTypes; - sig.returnTypeRequiredCustomModifiers = Type.EmptyTypes; foreach (Type type in parameterTypes) { sig.AddArgument(type); @@ -105,8 +101,7 @@ namespace IKVM.Reflection.Emit SignatureHelper sig = new SignatureHelper(mod as ModuleBuilder, Signature.PROPERTY); sig.callingConvention = callingConvention; sig.returnType = returnType; - sig.returnTypeOptionalCustomModifiers = requiredReturnTypeCustomModifiers; - sig.returnTypeRequiredCustomModifiers = optionalReturnTypeCustomModifiers; + sig.returnTypeCustomModifiers = CustomModifiers.FromReqOpt(requiredReturnTypeCustomModifiers, optionalReturnTypeCustomModifiers); sig.AddArguments(parameterTypes, requiredParameterTypeCustomModifiers, optionalParameterTypeCustomModifiers); return sig; } @@ -175,10 +170,10 @@ namespace IKVM.Reflection.Emit } break; case Signature.FIELD: - FieldSignature.Create(args[0], optionalCustomModifiers[0], requiredCustomModifiers[0]).WriteSig(module, bb); + FieldSignature.Create(args[0], customModifiers[0]).WriteSig(module, bb); break; case Signature.PROPERTY: - Signature.WritePropertySig(module, bb, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, args.ToArray(), requiredCustomModifiers.ToArray(), optionalCustomModifiers.ToArray()); + Signature.WritePropertySig(module, bb, callingConvention, returnType, returnTypeCustomModifiers, args.ToArray(), customModifiers.ToArray()); break; case Signature.LOCAL_SIG: Signature.WriteLocalVarSig(module, bb, locals); @@ -202,18 +197,19 @@ namespace IKVM.Reflection.Emit public void AddArgument(Type argument, bool pinned) { - AddArgument(argument, pinned, Type.EmptyTypes, Type.EmptyTypes); + __AddArgument(argument, pinned, new CustomModifiers()); } public void AddArgument(Type argument, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers) { - AddArgument(argument, false, requiredCustomModifiers, optionalCustomModifiers); + __AddArgument(argument, false, CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers)); } - private void AddArgument(Type argument, bool pinned, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers) + public void __AddArgument(Type argument, bool pinned, CustomModifiers customModifiers) { if (type == Signature.LOCAL_SIG) { + // TODO support custom modifiers in local sig locals.Add(new LocalBuilder(argument, 0, pinned)); } else if (optional) @@ -223,8 +219,7 @@ namespace IKVM.Reflection.Emit else { this.args.Add(argument); - this.requiredCustomModifiers.Add(requiredCustomModifiers); - this.optionalCustomModifiers.Add(optionalCustomModifiers); + this.customModifiers.Add(customModifiers); } } @@ -232,7 +227,7 @@ namespace IKVM.Reflection.Emit { for (int i = 0; i < arguments.Length; i++) { - AddArgument(arguments[i], false, requiredCustomModifiers[i], optionalCustomModifiers[i]); + __AddArgument(arguments[i], false, CustomModifiers.FromReqOpt(requiredCustomModifiers[i], optionalCustomModifiers[i])); } } } diff --git a/reflect/Emit/TypeBuilder.cs b/reflect/Emit/TypeBuilder.cs index b75757e4..bbc1edda 100644 --- a/reflect/Emit/TypeBuilder.cs +++ b/reflect/Emit/TypeBuilder.cs @@ -351,7 +351,12 @@ namespace IKVM.Reflection.Emit public FieldBuilder DefineField(string fieldName, Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers, FieldAttributes attributes) { - FieldBuilder fb = new FieldBuilder(this, fieldName, type, requiredCustomModifiers, optionalCustomModifiers, attributes); + return __DefineField(fieldName, type, CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers), attributes); + } + + public FieldBuilder __DefineField(string fieldName, Type type, CustomModifiers customModifiers, FieldAttributes attributes) + { + FieldBuilder fb = new FieldBuilder(this, fieldName, type, customModifiers, attributes); fields.Add(fb); return fb; } @@ -364,28 +369,33 @@ namespace IKVM.Reflection.Emit public PropertyBuilder DefineProperty(string name, PropertyAttributes attributes, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers) { - return DefinePropertyImpl(name, attributes, CallingConventions.Standard, true, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, - parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers); + return DefinePropertyImpl(name, attributes, CallingConventions.Standard, true, returnType, parameterTypes, + PackedCustomModifiers.CreateFromExternal(returnTypeOptionalCustomModifiers, returnTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers, parameterTypeRequiredCustomModifiers, Util.NullSafeLength(parameterTypes))); } public PropertyBuilder DefineProperty(string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers) { - return DefinePropertyImpl(name, attributes, callingConvention, false, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, - parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers); + return DefinePropertyImpl(name, attributes, callingConvention, false, returnType, parameterTypes, + PackedCustomModifiers.CreateFromExternal(returnTypeOptionalCustomModifiers, returnTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers, parameterTypeRequiredCustomModifiers, Util.NullSafeLength(parameterTypes))); + } + + public PropertyBuilder __DefineProperty(string name, PropertyAttributes attributes, CallingConventions callingConvention, + Type returnType, CustomModifiers returnTypeCustomModifiers, Type[] parameterTypes, CustomModifiers[] parameterTypeCustomModifiers) + { + return DefinePropertyImpl(name, attributes, callingConvention, false, returnType, parameterTypes, + PackedCustomModifiers.CreateFromExternal(returnTypeCustomModifiers, parameterTypeCustomModifiers, Util.NullSafeLength(parameterTypes))); } private PropertyBuilder DefinePropertyImpl(string name, PropertyAttributes attributes, CallingConventions callingConvention, bool patchCallingConvention, - Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, - Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers) + Type returnType, Type[] parameterTypes, PackedCustomModifiers customModifiers) { if (properties == null) { properties = new List<PropertyBuilder>(); } - PropertySignature sig = PropertySignature.Create(callingConvention, returnType, returnTypeOptionalCustomModifiers, returnTypeRequiredCustomModifiers, - parameterTypes, parameterTypeOptionalCustomModifiers, parameterTypeRequiredCustomModifiers); + PropertySignature sig = PropertySignature.Create(callingConvention, returnType, parameterTypes, customModifiers); PropertyBuilder pb = new PropertyBuilder(this, name, attributes, sig, patchCallingConvention); properties.Add(pb); return pb; @@ -628,14 +638,9 @@ namespace IKVM.Reflection.Emit return Util.Copy(gtpb); } - public override Type[][] __GetGenericArgumentsOptionalCustomModifiers() - { - return gtpb == null ? Empty<Type[]>.Array : Util.Copy(new Type[gtpb.Length][]); - } - - public override Type[][] __GetGenericArgumentsRequiredCustomModifiers() + public override CustomModifiers[] __GetGenericArgumentsCustomModifiers() { - return gtpb == null ? Empty<Type[]>.Array : Util.Copy(new Type[gtpb.Length][]); + return gtpb == null ? Empty<CustomModifiers>.Array : new CustomModifiers[gtpb.Length]; } internal override Type GetGenericTypeArgument(int index) @@ -1152,14 +1157,9 @@ namespace IKVM.Reflection.Emit return underlyingType.GetGenericTypeArgument(index); } - public override Type[][] __GetGenericArgumentsOptionalCustomModifiers() - { - return underlyingType.__GetGenericArgumentsOptionalCustomModifiers(); - } - - public override Type[][] __GetGenericArgumentsRequiredCustomModifiers() + public override CustomModifiers[] __GetGenericArgumentsCustomModifiers() { - return underlyingType.__GetGenericArgumentsRequiredCustomModifiers(); + return underlyingType.__GetGenericArgumentsCustomModifiers(); } public override bool IsGenericType |