Tue Aug 27 16:57:18 CEST 2002 Paolo Molaro <lupus@ximian.com>
[mono.git] / mcs / class / corlib / System.Reflection.Emit / TypeBuilder.cs
index de6862e0825f0423b423bdc27616a535a4b2ce56..be52f786da50a0aead8849e51988516094deee4e 100644 (file)
@@ -22,11 +22,13 @@ namespace System.Reflection.Emit {
        private string tname;
        private string nspace;
        private Type parent;
+       private Type nesting_type;
        private Type[] interfaces;
        private MethodBuilder[] methods;
        private ConstructorBuilder[] ctors;
        private PropertyBuilder[] properties;
        private FieldBuilder[] fields;
+       private EventBuilder[] events;
        private CustomAttributeBuilder[] cattrs;
        internal TypeBuilder[] subtypes;
        private TypeAttributes attrs;
@@ -34,8 +36,9 @@ namespace System.Reflection.Emit {
        private ModuleBuilder pmodule;
        private int class_size;
        private PackingSize packing_size;
+       private Type created;
 
-       public const int UnspecifiedTypeSize = -1;
+       public const int UnspecifiedTypeSize = 0;
 
                protected override TypeAttributes GetAttributeFlagsImpl () {
                        return attrs;
@@ -44,11 +47,15 @@ namespace System.Reflection.Emit {
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                private extern void setup_internal_class (TypeBuilder tb);
                
-               internal TypeBuilder (ModuleBuilder mb, string name, TypeAttributes attr, Type parent, Type[] interfaces) {
+               [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               private extern void create_internal_class (TypeBuilder tb);
+               
+               internal TypeBuilder (ModuleBuilder mb, string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packing_size, int type_size) {
                        int sep_index;
                        this.parent = parent;
                        this.attrs = attr;
-                       packing_size = PackingSize.Unspecified;
+                       this.class_size = type_size;
+                       this.packing_size = packing_size;
                        sep_index = name.LastIndexOf('.');
                        if (sep_index != -1) {
                                this.tname = name.Substring (sep_index + 1);
@@ -63,26 +70,40 @@ namespace System.Reflection.Emit {
                        }
                        pmodule = mb;
                        // skip .<Module> ?
-                       table_idx = mb.get_next_table_index (0x02, true);
+                       table_idx = mb.get_next_table_index (this, 0x02, true);
                        setup_internal_class (this);
                }
 
                public override Assembly Assembly {
                        get {return pmodule.Assembly;}
                }
-               public override string AssemblyQualifiedName {get {return null;}}
+               public override string AssemblyQualifiedName {
+                       get {
+                               return FullName + ", " + Assembly.ToString();
+                       }
+               }
                public override Type BaseType {
                        get {
                                return parent;
                        }
                }
-               public override Type DeclaringType {get {return null;}}
+               public override Type DeclaringType {get {return nesting_type;}}
                public override Type UnderlyingSystemType {
-                       get {return null;}
+                       get {
+                               if (fields != null) {
+                                       foreach (FieldBuilder f in fields) {
+                                               if ((f.Attributes & FieldAttributes.Static) == 0)
+                                                       return f.FieldType;
+                                       }
+                               }
+                               throw new InvalidOperationException (String.Format ("typebuilder: {0}", this));
+                       }
                }
 
                public override string FullName {
                        get {
+                               if (nesting_type != null)
+                                       return String.Concat (nesting_type.FullName, "+", tname);
                                if ((nspace != null) && (nspace.Length > 0))
                                        return String.Concat (nspace, ".", tname);
                                return tname;
@@ -105,19 +126,29 @@ namespace System.Reflection.Emit {
                public PackingSize PackingSize {
                        get {return packing_size;}
                }
-               public override Type ReflectedType {get {return parent;}}
+               public override Type ReflectedType {get {return nesting_type;}}
                public override MemberTypes MemberType { 
                        get {return MemberTypes.TypeInfo;}
                }
 
+               [MonoTODO]
                public void AddDeclarativeSecurity( SecurityAction action, PermissionSet pset) {
                        throw new NotImplementedException ();
                }
 
                public void AddInterfaceImplementation( Type interfaceType) {
-                       throw new NotImplementedException ();
+                       if (interfaces != null) {
+                               Type[] ifnew = new Type [interfaces.Length + 1];
+                               interfaces.CopyTo (ifnew, 0);
+                               ifnew [interfaces.Length] = interfaceType;
+                               interfaces = ifnew;
+                       } else {
+                               interfaces = new Type [1];
+                               interfaces [0] = interfaceType;
+                       }
                }
 
+               [MonoTODO]
                protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) {
                        throw new NotImplementedException ();
                }
@@ -135,19 +166,20 @@ namespace System.Reflection.Emit {
                [MonoTODO]
                public TypeBuilder DefineNestedType (string name) {
                        // FIXME: LAMESPEC: what other attributes should we use here as default?
-                       return DefineNestedType (name, TypeAttributes.Public, typeof(object), null);
+                       return DefineNestedType (name, TypeAttributes.Public, pmodule.assemblyb.corlib_object_type, null);
                }
 
                public TypeBuilder DefineNestedType (string name, TypeAttributes attr) {
-                       return DefineNestedType (name, attr, typeof(object), null);
+                       return DefineNestedType (name, attr, pmodule.assemblyb.corlib_object_type, null);
                }
 
                public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent) {
                        return DefineNestedType (name, attr, parent, null);
                }
 
-               public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
-                       TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces);
+               private TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packsize, int typesize) {
+                       TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces, packsize, typesize);
+                       res.nesting_type = this;
                        if (subtypes != null) {
                                TypeBuilder[] new_types = new TypeBuilder [subtypes.Length + 1];
                                System.Array.Copy (subtypes, new_types, subtypes.Length);
@@ -160,16 +192,16 @@ namespace System.Reflection.Emit {
                        return res;
                }
 
-               public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typesize) {
-                       return DefineNestedType (name, attr, parent, null);
+               public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
+                       return DefineNestedType (name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize);
                }
 
-               public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packsize) {
-                       return DefineNestedType (name, attr, parent, null);
+               public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typesize) {
+                       return DefineNestedType (name, attr, parent, null, PackingSize.Unspecified, typesize);
                }
 
-               public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packsize, int typesize) {
-                       return DefineNestedType (name, attr, parent, null);
+               public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packsize) {
+                       return DefineNestedType (name, attr, parent, null, packsize, UnspecifiedTypeSize);
                }
 
                public ConstructorBuilder DefineConstructor( MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes) {
@@ -241,6 +273,7 @@ namespace System.Reflection.Emit {
                        } else {
                                fields = new FieldBuilder [1];
                                fields [0] = res;
+                               create_internal_class (this);
                        }
                        return res;
                }
@@ -260,11 +293,18 @@ namespace System.Reflection.Emit {
                        return res;
                }
 
+               [MonoTODO]
                public ConstructorBuilder DefineTypeInitializer() {
                        throw new NotImplementedException ();
                }
 
+               [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               private extern Type create_runtime_class (TypeBuilder tb);
+               
                public Type CreateType() {
+                       /* handle nesting_type */
+                       if (created != null)
+                               throw new InvalidOperationException ("type already created");
                        if (methods != null) {
                                foreach (MethodBuilder method in methods) {
                                        method.fixup ();
@@ -275,6 +315,8 @@ namespace System.Reflection.Emit {
                                        ctor.fixup ();
                                }
                        }
+                       created = create_runtime_class (this);
+
                        return this;
                }
 
@@ -316,6 +358,7 @@ namespace System.Reflection.Emit {
 
                public override Type GetElementType () { return null; }
 
+               [MonoTODO]
                public override EventInfo GetEvent (string name, BindingFlags bindingAttr) {
                        throw new NotImplementedException ();
                }
@@ -324,6 +367,7 @@ namespace System.Reflection.Emit {
                        return new EventInfo [0];
                }
 
+               [MonoTODO]
                public override FieldInfo GetField( string name, BindingFlags bindingAttr) {
                        //FIXME
                        throw new NotImplementedException ();
@@ -365,29 +409,22 @@ namespace System.Reflection.Emit {
                        return result;
                }
 
+               [MonoTODO]
                public override Type GetInterface (string name, bool ignoreCase) {
                        throw new NotImplementedException ();
                }
                
                public override Type[] GetInterfaces () {
                        if (interfaces != null) {
-                               Type [] pi;
-                               if (parent != null)
-                                       pi = parent.GetInterfaces ();
-                               else
-                                       pi = Type.EmptyTypes;
-                               Type[] ret = new Type [interfaces.Length + pi.Length];
+                               Type[] ret = new Type [interfaces.Length];
                                interfaces.CopyTo (ret, 0);
-                               pi.CopyTo (ret, interfaces.Length);
                                return ret;
                        } else {
-                               if (parent != null)
-                                       return parent.GetInterfaces ();
-                               else
-                                       return Type.EmptyTypes;
+                               return Type.EmptyTypes;
                        }
                }
 
+               [MonoTODO]
                public override MemberInfo[] GetMembers( BindingFlags bindingAttr) {
                        // FIXME
                        throw new NotImplementedException ();
@@ -429,22 +466,40 @@ namespace System.Reflection.Emit {
                        return result;
                }
 
+               [MonoTODO]
                protected override MethodInfo GetMethodImpl( string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) {
                        // FIXME
                        throw new NotImplementedException ();
-                       return null;
                }
                
+               [MonoTODO]
                public override Type GetNestedType( string name, BindingFlags bindingAttr) {
                        // FIXME
                        throw new NotImplementedException ();
-                       return null;
                }
 
                public override Type[] GetNestedTypes (BindingFlags bindingAttr) {
-                       // FIXME
-                       throw new NotImplementedException ();
-                       return null;
+                       bool match;
+                       ArrayList result = new ArrayList ();
+               
+                       if (subtypes == null)
+                               return Type.EmptyTypes;
+                       foreach (TypeBuilder t in subtypes) {
+                               match = false;
+                               if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
+                                       if ((bindingAttr & BindingFlags.Public) != 0)
+                                               match = true;
+                               } else {
+                                       if ((bindingAttr & BindingFlags.NonPublic) != 0)
+                                               match = true;
+                               }
+                               if (!match)
+                                       continue;
+                               result.Add (t);
+                       }
+                       Type[] r = new Type [result.Count];
+                       result.CopyTo (r);
+                       return r;
                }
 
                public override PropertyInfo[] GetProperties( BindingFlags bindingAttr) {
@@ -489,25 +544,24 @@ namespace System.Reflection.Emit {
                        return result;
                }
                
+               [MonoTODO]
                protected override PropertyInfo GetPropertyImpl( string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) {
                        // FIXME
                        throw new NotImplementedException ();
-                       return null;
                }
 
                protected override bool HasElementTypeImpl () {
                        return IsArrayImpl() || IsByRefImpl() || IsPointerImpl ();
                }
 
+               [MonoTODO]
                public override object InvokeMember( string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters) {
                        // FIXME
                        throw new NotImplementedException ();
-                       return null;
                }
 
                protected override bool IsArrayImpl () {
-                       // FIXME
-                       return false;
+                       return type_is_subtype_of (this, typeof (System.Array), false);
                }
                protected override bool IsByRefImpl () {
                        // FIXME
@@ -525,13 +579,83 @@ namespace System.Reflection.Emit {
                        return false;
                }
                protected override bool IsValueTypeImpl () {
-                       // test this one
-                       return type_is_subtype_of (this, typeof (System.ValueType), false);
+                       return ((type_is_subtype_of (this, pmodule.assemblyb.corlib_value_type, false) || type_is_subtype_of (this, typeof(System.ValueType), false)) &&
+                               this != pmodule.assemblyb.corlib_value_type &&
+                               this != pmodule.assemblyb.corlib_enum_type);
                }
                
                public override RuntimeTypeHandle TypeHandle { get { return _impl; } }
 
                public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
+                       string attrname = customBuilder.Ctor.ReflectedType.FullName;
+                       if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
+                               byte[] data = customBuilder.Data;
+                               int layout_kind; /* the (stupid) ctor takes a short or an int ... */
+                               layout_kind = (int)data [2];
+                               layout_kind |= ((int)data [3]) << 8;
+                               attrs &= ~TypeAttributes.LayoutMask;
+                               switch ((LayoutKind)layout_kind) {
+                               case LayoutKind.Auto:
+                                       attrs |= TypeAttributes.AutoLayout;
+                                       break;
+                               case LayoutKind.Explicit:
+                                       attrs |= TypeAttributes.ExplicitLayout;
+                                       break;
+                               case LayoutKind.Sequential:
+                                       attrs |= TypeAttributes.SequentialLayout;
+                                       break;
+                               default:
+                                       // we should ignore it since it can be any value anyway...
+                                       throw new Exception ("Error in customattr");
+                               }
+                               string first_type_name = customBuilder.Ctor.GetParameters()[0].ParameterType.FullName;
+                               int pos = 6;
+                               if (first_type_name == "System.Int16")
+                                       pos = 4;
+                               int nnamed = (int)data [pos++];
+                               nnamed |= ((int)data [pos++]) << 8;
+                               for (int i = 0; i < nnamed; ++i) {
+                                       byte named_type = data [pos++];
+                                       byte type = data [pos++];
+                                       int len = CustomAttributeBuilder.decode_len (data, pos, out pos);
+                                       string named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
+                                       pos += len;
+                                       /* all the fields are integers in StructLayout */
+                                       int value = (int)data [pos++];
+                                       value |= ((int)data [pos++]) << 8;
+                                       value |= ((int)data [pos++]) << 16;
+                                       value |= ((int)data [pos++]) << 24;
+                                       switch (named_name) {
+                                       case "CharSet":
+                                               switch ((CharSet)value) {
+                                               case CharSet.None:
+                                               case CharSet.Ansi:
+                                                       break;
+                                               case CharSet.Unicode:
+                                                       attrs |= TypeAttributes.UnicodeClass;
+                                                       break;
+                                               case CharSet.Auto:
+                                                       attrs |= TypeAttributes.AutoClass;
+                                                       break;
+                                               default:
+                                                       break; // error out...
+                                               }
+                                               break;
+                                       case "Pack":
+                                               packing_size = (PackingSize)value;
+                                               break;
+                                       case "Size":
+                                               class_size = value;
+                                               break;
+                                       default:
+                                               break; // error out...
+                                       }
+                               }
+                               return;
+                       } else if (attrname == "System.SerializableAttribute") {
+                               attrs |= TypeAttributes.Serializable;
+                               return;
+                       }
                        if (cattrs != null) {
                                CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
                                cattrs.CopyTo (new_array, 0);
@@ -547,7 +671,17 @@ namespace System.Reflection.Emit {
                }
 
                public EventBuilder DefineEvent( string name, EventAttributes attributes, Type eventtype) {
-                       throw new NotImplementedException ();
+                       EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
+                       if (events != null) {
+                               EventBuilder[] new_events = new EventBuilder [events.Length+1];
+                               System.Array.Copy (events, new_events, events.Length);
+                               new_events [events.Length] = res;
+                               events = new_events;
+                       } else {
+                               events = new EventBuilder [1];
+                               events [0] = res;
+                       }
+                       return res;
                }
 
                static int InitializedDataCount = 0;
@@ -555,7 +689,7 @@ namespace System.Reflection.Emit {
                public FieldBuilder DefineInitializedData( string name, byte[] data, FieldAttributes attributes) {
                        TypeBuilder datablobtype = pmodule.DefineType ("$ArrayType$"+InitializedDataCount.ToString(),
                                TypeAttributes.Public|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
-                               typeof (System.ValueType), PackingSize.Size1, data.Length);
+                               pmodule.assemblyb.corlib_value_type, PackingSize.Size1, data.Length);
                        datablobtype.packing_size = PackingSize.Size1;
                        datablobtype.class_size = data.Length;
                        datablobtype.CreateType ();
@@ -565,16 +699,21 @@ namespace System.Reflection.Emit {
                        return res;
                }
 
+               [MonoTODO]
                public FieldBuilder DefineUninitializedData( string name, int size, FieldAttributes attributes) {
                        throw new NotImplementedException ();
-                       return null;
                }
 
+               public TypeToken TypeToken {
+                       get {
+                               return new TypeToken (0x02000000 | table_idx);
+                       }
+               }
                public void SetParent (Type parentType) {
                        parent = parentType;
                }
-               internal int get_next_table_index (int table, bool inc) {
-                       return pmodule.get_next_table_index (table, inc);
+               internal int get_next_table_index (object obj, int table, bool inc) {
+                       return pmodule.get_next_table_index (obj, table, inc);
                }
 
        }