Tue Aug 27 16:57:18 CEST 2002 Paolo Molaro <lupus@ximian.com>
[mono.git] / mcs / class / corlib / System.Reflection.Emit / TypeBuilder.cs
index 119b50a3eecdd6aaa245f32c872724c1d8a13e2c..be52f786da50a0aead8849e51988516094deee4e 100644 (file)
@@ -36,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;
@@ -76,13 +77,17 @@ namespace System.Reflection.Emit {
                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 {
                                if (fields != null) {
@@ -121,7 +126,7 @@ 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;}
                }
@@ -161,11 +166,11 @@ 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) {
@@ -293,7 +298,13 @@ namespace System.Reflection.Emit {
                        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 ();
@@ -304,6 +315,7 @@ namespace System.Reflection.Emit {
                                        ctor.fixup ();
                                }
                        }
+                       created = create_runtime_class (this);
 
                        return this;
                }
@@ -549,8 +561,7 @@ namespace System.Reflection.Emit {
                }
 
                protected override bool IsArrayImpl () {
-                       // FIXME
-                       return false;
+                       return type_is_subtype_of (this, typeof (System.Array), false);
                }
                protected override bool IsByRefImpl () {
                        // FIXME
@@ -568,35 +579,13 @@ 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; } }
 
-               private static int decode_len (byte[] data, int pos, out int rpos) {
-                       int len = 0;
-                       if ((data [pos] & 0x80) == 0) {
-                               len = (int)(data [pos++] & 0x7f);
-                       } else if ((data [pos] & 0x40) == 0) {
-                               len = ((data [pos] & 0x3f) << 8) + data [pos + 1];
-                               pos += 2;
-                       } else {
-                               len = ((data [pos] & 0x1f) << 24) + (data [pos + 1] << 16) + (data [pos + 2] << 8) + data [pos + 3];
-                               pos += 4;
-                       }
-                       rpos = pos;
-                       return len;
-               }
-
-               private static string string_from_bytes (byte[] data, int pos, int len) {
-                       char[] chars = new char [len];
-                       // FIXME: use a utf8 decoder here
-                       for (int i = 0; i < len; ++i)
-                               chars [i] = (char)data [pos + i];
-                       return new String (chars);
-               }
-
                public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
                        string attrname = customBuilder.Ctor.ReflectedType.FullName;
                        if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
@@ -626,9 +615,10 @@ namespace System.Reflection.Emit {
                                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 = decode_len (data, pos, out pos);
-                                       string named_name = string_from_bytes (data, pos, len);
+                                       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++];
@@ -662,6 +652,9 @@ namespace System.Reflection.Emit {
                                        }
                                }
                                return;
+                       } else if (attrname == "System.SerializableAttribute") {
+                               attrs |= TypeAttributes.Serializable;
+                               return;
                        }
                        if (cattrs != null) {
                                CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
@@ -696,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 ();