X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;ds=sidebyside;f=mcs%2Fclass%2Fcorlib%2FSystem.Reflection.Emit%2FTypeBuilder.cs;h=6bf253e6c0b613e56fcd8214a16ae30ee5432b58;hb=19460c4ca7c710bf80a7121c76e5b5ef8fc48c8c;hp=edcf429d9cc55167a63c75ed876ab94c314b6d44;hpb=e1b7e8c457247c6a89c805cf1ce3eb98b8a74d36;p=mono.git diff --git a/mcs/class/corlib/System.Reflection.Emit/TypeBuilder.cs b/mcs/class/corlib/System.Reflection.Emit/TypeBuilder.cs index edcf429d9cc..6bf253e6c0b 100644 --- a/mcs/class/corlib/System.Reflection.Emit/TypeBuilder.cs +++ b/mcs/class/corlib/System.Reflection.Emit/TypeBuilder.cs @@ -64,14 +64,15 @@ namespace System.Reflection.Emit { private ModuleBuilder pmodule; private int class_size; private PackingSize packing_size; + private IntPtr generic_container; #if NET_2_0 || BOOTSTRAP_NET_2_0 private GenericTypeParameterBuilder[] generic_params; #else private Object generic_params; /* so offsets don't change */ #endif private RefEmitPermissionSet[] permissions; - #endregion private Type created; + #endregion string fullname; public const int UnspecifiedTypeSize = 0; @@ -87,7 +88,10 @@ namespace System.Reflection.Emit { private extern void create_internal_class (TypeBuilder tb); [MethodImplAttribute(MethodImplOptions.InternalCall)] - private extern void setup_generic_class (TypeBuilder tb); + private extern void setup_generic_class (); + + [MethodImplAttribute(MethodImplOptions.InternalCall)] + private extern void create_generic_class (); [MethodImplAttribute(MethodImplOptions.InternalCall)] private extern EventInfo get_event_info (EventBuilder eb); @@ -95,9 +99,9 @@ namespace System.Reflection.Emit { internal TypeBuilder (ModuleBuilder mb, TypeAttributes attr) { this.parent = null; this.attrs = attr; - this.class_size = 0; - fullname = this.tname = ""; + this.class_size = UnspecifiedTypeSize; this.table_idx = 1; + fullname = this.tname = ""; this.nspace = ""; pmodule = mb; setup_internal_class (this); @@ -192,7 +196,8 @@ namespace System.Reflection.Emit { public override Guid GUID { get { - throw not_supported (); + check_created (); + return created.GUID; } } @@ -221,8 +226,7 @@ namespace System.Reflection.Emit { (action == SecurityAction.RequestRefuse)) throw new ArgumentException ("Request* values are not permitted", "action"); - if (is_created) - throw not_after_created (); + check_not_created (); if (permissions != null) { /* Check duplicate actions */ @@ -244,8 +248,7 @@ namespace System.Reflection.Emit { public void AddInterfaceImplementation( Type interfaceType) { if (interfaceType == null) throw new ArgumentNullException ("interfaceType"); - if (is_created) - throw not_after_created (); + check_not_created (); if (interfaces != null) { // Check for duplicates @@ -315,12 +318,16 @@ namespace System.Reflection.Emit { public override object[] GetCustomAttributes(bool inherit) { - throw not_supported (); + check_created (); + + return created.GetCustomAttributes (inherit); } public override object[] GetCustomAttributes(Type attributeType, bool inherit) { - throw not_supported (); + check_created (); + + return created.GetCustomAttributes (attributeType, inherit); } public TypeBuilder DefineNestedType (string name) { @@ -388,8 +395,7 @@ namespace System.Reflection.Emit { #endif ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers) { - if (is_created) - throw not_after_created (); + check_not_created (); ConstructorBuilder cb = new ConstructorBuilder (this, attributes, callingConvention, parameterTypes, requiredCustomModifiers, optionalCustomModifiers); if (ctors != null) { ConstructorBuilder[] new_ctors = new ConstructorBuilder [ctors.Length+1]; @@ -405,8 +411,6 @@ namespace System.Reflection.Emit { public ConstructorBuilder DefineDefaultConstructor (MethodAttributes attributes) { - ConstructorBuilder cb = DefineConstructor (attributes, CallingConventions.Standard, new Type [0]); - Type parent_type; if (parent != null) @@ -418,12 +422,18 @@ namespace System.Reflection.Emit { parent_type.GetConstructor ( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null); + if (parent_constructor == null) { + throw new NotSupportedException ("Parent does" + + " not have a default constructor." + + " The default constructor must be" + + " explicitly defined."); + } + ConstructorBuilder cb = DefineConstructor (attributes, + CallingConventions.Standard, new Type[0]); ILGenerator ig = cb.GetILGenerator (); - if (parent_constructor != null){ - ig.Emit (OpCodes.Ldarg_0); - ig.Emit (OpCodes.Call, parent_constructor); - } + ig.Emit (OpCodes.Ldarg_0); + ig.Emit (OpCodes.Call, parent_constructor); ig.Emit (OpCodes.Ret); return cb; } @@ -457,8 +467,7 @@ namespace System.Reflection.Emit { #endif MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers) { check_name ("name", name); - if (is_created) - throw not_after_created (); + check_not_created (); if (IsInterface && ( !((attributes & MethodAttributes.Abstract) != 0) || !((attributes & MethodAttributes.Virtual) != 0))) @@ -500,8 +509,7 @@ namespace System.Reflection.Emit { throw new ArgumentException ("attributes", "PInvoke methods must be static and native and cannot be abstract."); if (IsInterface) throw new ArgumentException ("PInvoke methods cannot exist on interfaces."); - if (is_created) - throw not_after_created (); + check_not_created (); MethodBuilder res = new MethodBuilder ( @@ -533,8 +541,7 @@ namespace System.Reflection.Emit { throw new ArgumentNullException ("methodInfoBody"); if (methodInfoDeclaration == null) throw new ArgumentNullException ("methodInfoDeclaration"); - if (is_created) - throw not_after_created (); + check_not_created (); if (methodInfoBody is MethodBuilder) { MethodBuilder mb = (MethodBuilder)methodInfoBody; @@ -551,12 +558,11 @@ namespace System.Reflection.Emit { #else internal #endif - FieldBuilder DefineField( string fieldName, Type type, Type[] requiredCustomAttributes, Type[] optionalCustomAttributes, FieldAttributes attributes) { + FieldBuilder DefineField (string fieldName, Type type, Type[] requiredCustomAttributes, Type[] optionalCustomAttributes, FieldAttributes attributes) { check_name ("fieldName", fieldName); if (type == typeof (void)) throw new ArgumentException ("type", "Bad field type in defining field."); - if (is_created) - throw not_after_created (); + check_not_created (); FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes, requiredCustomAttributes, optionalCustomAttributes); if (fields != null) { @@ -582,8 +588,7 @@ namespace System.Reflection.Emit { foreach (Type param in parameterTypes) if (param == null) throw new ArgumentNullException ("parameterTypes"); - if (is_created) - throw not_after_created (); + check_not_created (); PropertyBuilder res = new PropertyBuilder (this, name, attributes, returnType, parameterTypes); @@ -618,8 +623,8 @@ namespace System.Reflection.Emit { public Type CreateType() { /* handle nesting_type */ - if (is_created) - throw not_after_created (); + if (created != null) + return created; // Fire TypeResolve events for fields whose type is an unfinished // value type. @@ -651,7 +656,8 @@ namespace System.Reflection.Emit { // // On classes, define a default constructor if not provided // - if (!(IsInterface || IsValueType) && (ctors == null) && (tname != "")) + if (!(IsInterface || IsValueType) && (ctors == null) && (tname != "") && + (GetAttributeFlagsImpl () & TypeAttributes.Abstract | TypeAttributes.Sealed) != (TypeAttributes.Abstract | TypeAttributes.Sealed)) DefineDefaultConstructor (MethodAttributes.Public); if (ctors != null){ @@ -703,11 +709,13 @@ namespace System.Reflection.Emit { } public override Type GetElementType () { - throw not_supported (); + check_created (); + return created.GetElementType (); } public override EventInfo GetEvent (string name, BindingFlags bindingAttr) { - throw not_supported (); + check_created (); + return created.GetEvent (name, bindingAttr); } /* Needed to keep signature compatibility with MS.NET */ @@ -717,9 +725,13 @@ namespace System.Reflection.Emit { } public override EventInfo[] GetEvents (BindingFlags bindingAttr) { - // FIXME: Under MS.NET, this throws a NotImplementedException - // But mcs calls this method. How can that be? - return new EventInfo [0]; + /* FIXME: mcs calls this + check_created (); + */ + if (!is_created) + return new EventInfo [0]; + else + return created.GetEvents (bindingAttr); } // This is only used from MonoGenericInst.initialize(). @@ -846,7 +858,8 @@ namespace System.Reflection.Emit { } public override Type GetInterface (string name, bool ignoreCase) { - throw not_supported (); + check_created (); + return created.GetInterface (name, ignoreCase); } public override Type[] GetInterfaces () { @@ -861,11 +874,13 @@ namespace System.Reflection.Emit { public override MemberInfo[] GetMember (string name, MemberTypes type, BindingFlags bindingAttr) { - throw not_supported (); + check_created (); + return created.GetMember (name, type, bindingAttr); } public override MemberInfo[] GetMembers (BindingFlags bindingAttr) { - throw not_supported (); + check_created (); + return created.GetMembers (bindingAttr); } private MethodInfo[] GetMethodsByName (string name, BindingFlags bindingAttr, bool ignoreCase, Type reflected_type) { @@ -935,9 +950,7 @@ namespace System.Reflection.Emit { CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) { - if (!is_created) - /* MS.Net throws this exception if the type is unfinished... */ - throw not_supported (); + check_created (); bool ignoreCase = ((bindingAttr & BindingFlags.IgnoreCase) != 0); MethodInfo[] methods = GetMethodsByName (name, bindingAttr, ignoreCase, this); @@ -982,7 +995,8 @@ namespace System.Reflection.Emit { } public override Type GetNestedType( string name, BindingFlags bindingAttr) { - throw not_supported (); + check_created (); + return created.GetNestedType (name, bindingAttr); } public override Type[] GetNestedTypes (BindingFlags bindingAttr) { @@ -1056,14 +1070,13 @@ namespace System.Reflection.Emit { } protected override bool HasElementTypeImpl () { - // According to the MSDN docs, this is supported for TypeBuilders, - // but in reality, it is not - throw not_supported (); - // return IsArrayImpl() || IsByRefImpl() || IsPointerImpl (); + check_created (); + return created.HasElementType; } public override object InvokeMember( string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters) { - throw not_supported (); + check_created (); + return created.InvokeMember (name, invokeAttr, binder, target, args, modifiers, culture, namedParameters); } protected override bool IsArrayImpl () @@ -1094,7 +1107,8 @@ namespace System.Reflection.Emit { public override RuntimeTypeHandle TypeHandle { get { - throw not_supported (); + check_created (); + return created.TypeHandle; } } @@ -1200,8 +1214,7 @@ namespace System.Reflection.Emit { check_name ("name", name); if (eventtype == null) throw new ArgumentNullException ("eventtype"); - if (is_created) - throw not_after_created (); + check_not_created (); EventBuilder res = new EventBuilder (this, name, attributes, eventtype); if (events != null) { @@ -1234,8 +1247,7 @@ namespace System.Reflection.Emit { check_name ("name", name); if ((size <= 0) || (size > 0x3f0000)) throw new ArgumentException ("size", "Data size must be > 0 and < 0x3f0000"); - if (is_created) - throw not_after_created (); + check_not_created (); string s = "$ArrayType$"+UnmanagedDataCount.ToString(); UnmanagedDataCount++; @@ -1254,8 +1266,7 @@ namespace System.Reflection.Emit { public void SetParent (Type parentType) { if (parentType == null) throw new ArgumentNullException ("parentType"); - if (is_created) - throw not_after_created (); + check_not_created (); parent = parentType; // will just set the parent-related bits if called a second time @@ -1284,9 +1295,16 @@ namespace System.Reflection.Emit { return new NotSupportedException ("The invoked member is not supported in a dynamic module."); } - private Exception not_after_created () + private void check_not_created () + { + if (is_created) + throw new InvalidOperationException ("Unable to change after type has been created."); + } + + private void check_created () { - return new InvalidOperationException ("Unable to change after type has been created."); + if (!is_created) + throw not_supported (); } private void check_name (string argName, string name) @@ -1317,6 +1335,10 @@ namespace System.Reflection.Emit { } #if NET_2_0 || BOOTSTRAP_NET_2_0 + public bool IsCreated () { + return is_created; + } + public override Type[] GetGenericArguments () { if (generic_params != null) @@ -1327,7 +1349,7 @@ namespace System.Reflection.Emit { public override Type GetGenericTypeDefinition () { - setup_generic_class (this); + create_generic_class (); return base.GetGenericTypeDefinition (); } @@ -1363,6 +1385,8 @@ namespace System.Reflection.Emit { public GenericTypeParameterBuilder[] DefineGenericParameters (string[] names) { + setup_generic_class (); + generic_params = new GenericTypeParameterBuilder [names.Length]; for (int i = 0; i < names.Length; i++) generic_params [i] = new GenericTypeParameterBuilder (