Tue Aug 27 16:57:18 CEST 2002 Paolo Molaro <lupus@ximian.com>
[mono.git] / mcs / class / corlib / System.Reflection.Emit / TypeBuilder.cs
index 1013a8b83bf5ee96c52e15c57778ecde744962a5..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;
@@ -86,7 +87,7 @@ namespace System.Reflection.Emit {
                                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) {
@@ -125,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;}
                }
@@ -297,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 ();
@@ -308,6 +315,7 @@ namespace System.Reflection.Emit {
                                        ctor.fixup ();
                                }
                        }
+                       created = create_runtime_class (this);
 
                        return this;
                }
@@ -571,41 +579,13 @@ namespace System.Reflection.Emit {
                        return false;
                }
                protected override bool IsValueTypeImpl () {
-                       //Console.WriteLine ("is value type: {0}: {1} ({2}) base: {3}", AssemblyQualifiedName, type_is_subtype_of (this, pmodule.assemblyb.corlib_value_type, false), pmodule.assemblyb.corlib_value_type.AssemblyQualifiedName, BaseType != null?BaseType.AssemblyQualifiedName: "");
-                       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 != typeof (System.Enum); /* FIXME: handle a TypeBuilder System.Enum */
-
-/*                     return type_is_subtype_of (this, typeof(System.ValueType), false) &&
-                               this != typeof(System.ValueType) &&
-                               this != typeof (System.Enum);*/
+                       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") {
@@ -635,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++];