Update mcs/class/Commons.Xml.Relaxng/Commons.Xml.Relaxng/RelaxngPattern.cs
[mono.git] / mcs / class / IKVM.Reflection / Emit / TypeBuilder.cs
index be865c45c57045539c6be7ebd03817ae64f019f7..bbc1edda3e2370ebb91ee7bf003b43cd5c1ca396 100644 (file)
@@ -86,11 +86,6 @@ namespace IKVM.Reflection.Emit
                        get { return DeclaringType.Namespace; }
                }
 
-               public override Type UnderlyingSystemType
-               {
-                       get { return this; }
-               }
-
                public override string Name
                {
                        get { return name; }
@@ -226,7 +221,7 @@ namespace IKVM.Reflection.Emit
                private readonly ITypeOwner owner;
                private readonly int token;
                private int extends;
-               private Type baseType;
+               private Type lazyBaseType;              // (lazyBaseType == null && attribs & TypeAttributes.Interface) == 0) => BaseType == System.Object
                private readonly int typeName;
                private readonly int typeNameSpace;
                private readonly string ns;
@@ -236,28 +231,22 @@ namespace IKVM.Reflection.Emit
                private List<PropertyBuilder> properties;
                private List<EventBuilder> events;
                private TypeAttributes attribs;
-               private TypeFlags typeFlags;
                private GenericTypeParameterBuilder[] gtpb;
                private List<CustomAttributeBuilder> declarativeSecurity;
                private List<Type> interfaces;
+               private int size;
+               private short pack;
+               private bool hasLayout;
 
-               [Flags]
-               private enum TypeFlags
-               {
-                       IsGenericTypeDefinition = 1,
-                       HasNestedTypes = 2,
-                       Baked = 4,
-               }
-
-               internal TypeBuilder(ITypeOwner owner, string ns, string name, TypeAttributes attribs)
+               internal TypeBuilder(ITypeOwner owner, string ns, string name)
                {
                        this.owner = owner;
                        this.token = this.ModuleBuilder.TypeDef.AllocToken();
                        this.ns = ns;
                        this.name = name;
-                       this.attribs = attribs;
                        this.typeNameSpace = ns == null ? 0 : this.ModuleBuilder.Strings.Add(ns);
                        this.typeName = this.ModuleBuilder.Strings.Add(name);
+                       MarkEnumOrValueType(ns, name);
                }
 
                public ConstructorBuilder DefineDefaultConstructor(MethodAttributes attributes)
@@ -265,7 +254,7 @@ namespace IKVM.Reflection.Emit
                        ConstructorBuilder cb = DefineConstructor(attributes, CallingConventions.Standard, Type.EmptyTypes);
                        ILGenerator ilgen = cb.GetILGenerator();
                        ilgen.Emit(OpCodes.Ldarg_0);
-                       ilgen.Emit(OpCodes.Call, baseType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null));
+                       ilgen.Emit(OpCodes.Call, BaseType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null));
                        ilgen.Emit(OpCodes.Ret);
                        return cb;
                }
@@ -362,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;
                }
@@ -375,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;
@@ -426,9 +425,12 @@ namespace IKVM.Reflection.Emit
                public TypeBuilder DefineNestedType(string name, TypeAttributes attr, Type parent, Type[] interfaces)
                {
                        TypeBuilder tb = DefineNestedType(name, attr, parent);
-                       foreach (Type iface in interfaces)
+                       if (interfaces != null)
                        {
-                               tb.AddInterfaceImplementation(iface);
+                               foreach (Type iface in interfaces)
+                               {
+                                       tb.AddInterfaceImplementation(iface);
+                               }
                        }
                        return tb;
                }
@@ -457,20 +459,20 @@ namespace IKVM.Reflection.Emit
                                ns = name.Substring(0, lastdot);
                                name = name.Substring(lastdot + 1);
                        }
-                       TypeBuilder typeBuilder = __DefineNestedType(ns, name, attr);
-                       if (parent == null && (attr & TypeAttributes.Interface) == 0)
+                       TypeBuilder typeBuilder = __DefineNestedType(ns, name);
+                       typeBuilder.__SetAttributes(attr);
+                       typeBuilder.SetParent(parent);
+                       if (packSize != PackingSize.Unspecified || typeSize != 0)
                        {
-                               parent = this.ModuleBuilder.universe.System_Object;
+                               typeBuilder.__SetLayout((int)packSize, typeSize);
                        }
-                       typeBuilder.SetParent(parent);
-                       this.ModuleBuilder.SetPackingSizeAndTypeSize(typeBuilder, PackingSize.Unspecified, typeSize);
                        return typeBuilder;
                }
 
-               public TypeBuilder __DefineNestedType(string ns, string name, TypeAttributes attr)
+               public TypeBuilder __DefineNestedType(string ns, string name)
                {
                        this.typeFlags |= TypeFlags.HasNestedTypes;
-                       TypeBuilder typeBuilder = this.ModuleBuilder.DefineType(this, ns, name, attr);
+                       TypeBuilder typeBuilder = this.ModuleBuilder.DefineType(this, ns, name);
                        NestedClassTable.Record rec = new NestedClassTable.Record();
                        rec.NestedClass = typeBuilder.MetadataToken;
                        rec.EnclosingClass = this.MetadataToken;
@@ -480,7 +482,7 @@ namespace IKVM.Reflection.Emit
 
                public void SetParent(Type parent)
                {
-                       baseType = parent;
+                       lazyBaseType = parent;
                }
 
                public void AddInterfaceImplementation(Type interfaceType)
@@ -492,34 +494,33 @@ namespace IKVM.Reflection.Emit
                        interfaces.Add(interfaceType);
                }
 
+               public void __SetInterfaceImplementationCustomAttribute(Type interfaceType, CustomAttributeBuilder cab)
+               {
+                       this.ModuleBuilder.SetInterfaceImplementationCustomAttribute(this, interfaceType, cab);
+               }
+
                public int Size
                {
-                       get
-                       {
-                               for (int i = 0; i < this.ModuleBuilder.ClassLayout.records.Length; i++)
-                               {
-                                       if (this.ModuleBuilder.ClassLayout.records[i].Parent == token)
-                                       {
-                                               return this.ModuleBuilder.ClassLayout.records[i].ClassSize;
-                                       }
-                               }
-                               return 0;
-                       }
+                       get { return size; }
                }
 
                public PackingSize PackingSize
                {
-                       get
-                       {
-                               for (int i = 0; i < this.ModuleBuilder.ClassLayout.records.Length; i++)
-                               {
-                                       if (this.ModuleBuilder.ClassLayout.records[i].Parent == token)
-                                       {
-                                               return (PackingSize)this.ModuleBuilder.ClassLayout.records[i].PackingSize;
-                                       }
-                               }
-                               return PackingSize.Unspecified;
-                       }
+                       get { return (PackingSize)pack; }
+               }
+
+               public override bool __GetLayout(out int packingSize, out int typeSize)
+               {
+                       packingSize = this.pack;
+                       typeSize = this.size;
+                       return hasLayout;
+               }
+
+               public void __SetLayout(int packingSize, int typesize)
+               {
+                       this.pack = (short)packingSize;
+                       this.size = typesize;
+                       this.hasLayout = true;
                }
 
                private void SetStructLayoutPseudoCustomAttribute(CustomAttributeBuilder customBuilder)
@@ -534,18 +535,12 @@ namespace IKVM.Reflection.Emit
                        {
                                layout = (LayoutKind)val;
                        }
-                       int? pack = (int?)customBuilder.GetFieldValue("Pack");
-                       int? size = (int?)customBuilder.GetFieldValue("Size");
-                       if (pack.HasValue || size.HasValue)
-                       {
-                               ClassLayoutTable.Record rec = new ClassLayoutTable.Record();
-                               rec.PackingSize = (short)(pack ?? 0);
-                               rec.ClassSize = size ?? 0;
-                               rec.Parent = token;
-                               this.ModuleBuilder.ClassLayout.AddOrReplaceRecord(rec);
-                       }
+                       StructLayoutAttribute attr = new StructLayoutAttribute(layout);
+                       attr.Pack = (int?)customBuilder.GetFieldValue("Pack") ?? 0;
+                       attr.Size = (int?)customBuilder.GetFieldValue("Size") ?? 0;
+                       attr.CharSet = customBuilder.GetFieldValue<CharSet>("CharSet") ?? CharSet.None;
                        attribs &= ~TypeAttributes.LayoutMask;
-                       switch (layout)
+                       switch (attr.Value)
                        {
                                case LayoutKind.Auto:
                                        attribs |= TypeAttributes.AutoLayout;
@@ -557,9 +552,8 @@ namespace IKVM.Reflection.Emit
                                        attribs |= TypeAttributes.SequentialLayout;
                                        break;
                        }
-                       CharSet? charSet = customBuilder.GetFieldValue<CharSet>("CharSet");
                        attribs &= ~TypeAttributes.StringFormatMask;
-                       switch (charSet ?? CharSet.None)
+                       switch (attr.CharSet)
                        {
                                case CharSet.None:
                                case CharSet.Ansi:
@@ -572,6 +566,9 @@ namespace IKVM.Reflection.Emit
                                        attribs |= TypeAttributes.UnicodeClass;
                                        break;
                        }
+                       pack = (short)attr.Pack;
+                       size = attr.Size;
+                       hasLayout = pack != 0 || size != 0;
                }
 
                public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
@@ -641,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)
@@ -674,6 +666,14 @@ namespace IKVM.Reflection.Emit
                                throw new NotImplementedException();
                        }
                        typeFlags |= TypeFlags.Baked;
+                       if (hasLayout)
+                       {
+                               ClassLayoutTable.Record rec = new ClassLayoutTable.Record();
+                               rec.PackingSize = pack;
+                               rec.ClassSize = size;
+                               rec.Parent = token;
+                               this.ModuleBuilder.ClassLayout.AddRecord(rec);
+                       }
                        foreach (MethodBuilder mb in methods)
                        {
                                mb.Bake();
@@ -682,9 +682,13 @@ namespace IKVM.Reflection.Emit
                        {
                                this.ModuleBuilder.AddDeclarativeSecurity(token, declarativeSecurity);
                        }
-                       if (baseType != null)
+                       if (!IsModulePseudoType)
                        {
-                               extends = this.ModuleBuilder.GetTypeToken(baseType).Token;
+                               Type baseType = this.BaseType;
+                               if (baseType != null)
+                               {
+                                       extends = this.ModuleBuilder.GetTypeToken(baseType).Token;
+                               }
                        }
                        if (interfaces != null)
                        {
@@ -727,7 +731,18 @@ namespace IKVM.Reflection.Emit
 
                public override Type BaseType
                {
-                       get { return baseType; }
+                       get
+                       {
+                               if (lazyBaseType == null && !IsInterface)
+                               {
+                                       Type obj = Module.universe.System_Object;
+                                       if (this != obj)
+                                       {
+                                               lazyBaseType = obj;
+                                       }
+                               }
+                               return lazyBaseType;
+                       }
                }
 
                public override string FullName
@@ -812,20 +827,22 @@ namespace IKVM.Reflection.Emit
                {
                        get
                        {
-                               StructLayoutAttribute attr;
-                               if ((attribs & TypeAttributes.ExplicitLayout) != 0)
+                               LayoutKind layout;
+                               switch (attribs & TypeAttributes.LayoutMask)
                                {
-                                       attr = new StructLayoutAttribute(LayoutKind.Explicit);
-                                       attr.Pack = 8;
-                                       attr.Size = 0;
-                                       this.ModuleBuilder.ClassLayout.GetLayout(token, ref attr.Pack, ref attr.Size);
-                               }
-                               else
-                               {
-                                       attr = new StructLayoutAttribute((attribs & TypeAttributes.SequentialLayout) != 0 ? LayoutKind.Sequential : LayoutKind.Auto);
-                                       attr.Pack = 8;
-                                       attr.Size = 0;
+                                       case TypeAttributes.ExplicitLayout:
+                                               layout = LayoutKind.Explicit;
+                                               break;
+                                       case TypeAttributes.SequentialLayout:
+                                               layout = LayoutKind.Sequential;
+                                               break;
+                                       default:
+                                               layout = LayoutKind.Auto;
+                                               break;
                                }
+                               StructLayoutAttribute attr = new StructLayoutAttribute(layout);
+                               attr.Pack = (ushort)pack;
+                               attr.Size = size;
                                switch (attribs & TypeAttributes.StringFormatMask)
                                {
                                        case TypeAttributes.AutoClass:
@@ -837,6 +854,9 @@ namespace IKVM.Reflection.Emit
                                        case TypeAttributes.AnsiClass:
                                                attr.CharSet = CharSet.Ansi;
                                                break;
+                                       default:
+                                               attr.CharSet = CharSet.None;
+                                               break;
                                }
                                return attr;
                        }
@@ -1041,37 +1061,35 @@ namespace IKVM.Reflection.Emit
 
        sealed class BakedType : Type
        {
-               private readonly TypeBuilder typeBuilder;
-
                internal BakedType(TypeBuilder typeBuilder)
+                       : base(typeBuilder)
                {
-                       this.typeBuilder = typeBuilder;
                }
 
                public override string AssemblyQualifiedName
                {
-                       get { return typeBuilder.AssemblyQualifiedName; }
+                       get { return underlyingType.AssemblyQualifiedName; }
                }
 
                public override Type BaseType
                {
-                       get { return typeBuilder.BaseType; }
+                       get { return underlyingType.BaseType; }
                }
 
                public override string __Name
                {
-                       get { return typeBuilder.__Name; }
+                       get { return underlyingType.__Name; }
                }
 
                public override string __Namespace
                {
-                       get { return typeBuilder.__Namespace; }
+                       get { return underlyingType.__Namespace; }
                }
 
                public override string Name
                {
                        // we need to escape here, because TypeBuilder.Name does not escape
-                       get { return TypeNameParser.Escape(typeBuilder.__Name); }
+                       get { return TypeNameParser.Escape(underlyingType.__Name); }
                }
 
                public override string FullName
@@ -1081,108 +1099,97 @@ namespace IKVM.Reflection.Emit
 
                public override TypeAttributes Attributes
                {
-                       get { return typeBuilder.Attributes; }
+                       get { return underlyingType.Attributes; }
                }
 
                public override Type[] __GetDeclaredInterfaces()
                {
-                       return typeBuilder.__GetDeclaredInterfaces();
+                       return underlyingType.__GetDeclaredInterfaces();
                }
 
                public override MethodBase[] __GetDeclaredMethods()
                {
-                       return typeBuilder.__GetDeclaredMethods();
+                       return underlyingType.__GetDeclaredMethods();
                }
 
                public override __MethodImplMap __GetMethodImplMap()
                {
-                       return typeBuilder.__GetMethodImplMap();
+                       return underlyingType.__GetMethodImplMap();
                }
 
                public override FieldInfo[] __GetDeclaredFields()
                {
-                       return typeBuilder.__GetDeclaredFields();
+                       return underlyingType.__GetDeclaredFields();
                }
 
                public override EventInfo[] __GetDeclaredEvents()
                {
-                       return typeBuilder.__GetDeclaredEvents();
+                       return underlyingType.__GetDeclaredEvents();
                }
 
                public override PropertyInfo[] __GetDeclaredProperties()
                {
-                       return typeBuilder.__GetDeclaredProperties();
+                       return underlyingType.__GetDeclaredProperties();
                }
 
                public override Type[] __GetDeclaredTypes()
                {
-                       return typeBuilder.__GetDeclaredTypes();
+                       return underlyingType.__GetDeclaredTypes();
                }
 
                public override Type DeclaringType
                {
-                       get { return typeBuilder.DeclaringType; }
+                       get { return underlyingType.DeclaringType; }
                }
 
                public override StructLayoutAttribute StructLayoutAttribute
                {
-                       get { return typeBuilder.StructLayoutAttribute; }
-               }
-
-               public override Type UnderlyingSystemType
-               {
-                       // Type.Equals/GetHashCode relies on this
-                       get { return typeBuilder; }
+                       get { return underlyingType.StructLayoutAttribute; }
                }
 
                public override Type[] GetGenericArguments()
                {
-                       return typeBuilder.GetGenericArguments();
+                       return underlyingType.GetGenericArguments();
                }
 
                internal override Type GetGenericTypeArgument(int index)
                {
-                       return typeBuilder.GetGenericTypeArgument(index);
-               }
-
-               public override Type[][] __GetGenericArgumentsOptionalCustomModifiers()
-               {
-                       return typeBuilder.__GetGenericArgumentsOptionalCustomModifiers();
+                       return underlyingType.GetGenericTypeArgument(index);
                }
 
-               public override Type[][] __GetGenericArgumentsRequiredCustomModifiers()
+               public override CustomModifiers[] __GetGenericArgumentsCustomModifiers()
                {
-                       return typeBuilder.__GetGenericArgumentsRequiredCustomModifiers();
+                       return underlyingType.__GetGenericArgumentsCustomModifiers();
                }
 
                public override bool IsGenericType
                {
-                       get { return typeBuilder.IsGenericType; }
+                       get { return underlyingType.IsGenericType; }
                }
 
                public override bool IsGenericTypeDefinition
                {
-                       get { return typeBuilder.IsGenericTypeDefinition; }
+                       get { return underlyingType.IsGenericTypeDefinition; }
                }
 
                public override bool ContainsGenericParameters
                {
-                       get { return typeBuilder.ContainsGenericParameters; }
+                       get { return underlyingType.ContainsGenericParameters; }
                }
 
                public override int MetadataToken
                {
-                       get { return typeBuilder.MetadataToken; }
+                       get { return underlyingType.MetadataToken; }
                }
 
                public override Module Module
                {
-                       get { return typeBuilder.Module; }
+                       get { return underlyingType.Module; }
                }
 
                internal override int GetModuleBuilderToken()
                {
-                       return typeBuilder.GetModuleBuilderToken();
+                       return underlyingType.GetModuleBuilderToken();
                }
        }
 }