// (C) 2001 Ximian, Inc. http://www.ximian.com
//
+//
+// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
using System;
using System.Text;
using System.Reflection;
private EventBuilder[] events;
private CustomAttributeBuilder[] cattrs;
internal TypeBuilder[] subtypes;
- private TypeAttributes attrs;
+ internal TypeAttributes attrs;
private int table_idx;
private ModuleBuilder pmodule;
private int class_size;
private PackingSize packing_size;
-#if NET_2_0 | BOOTSTRAP_NET_2_0
+ 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;
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);
internal TypeBuilder (ModuleBuilder mb, TypeAttributes attr) {
this.parent = null;
this.attrs = attr;
- this.class_size = -1;
+ this.class_size = UnspecifiedTypeSize;
+ this.table_idx = 1;
fullname = this.tname = "<Module>";
this.nspace = "";
pmodule = mb;
public override Guid GUID {
get {
- throw not_supported ();
+ check_created ();
+ return created.GUID;
}
}
(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 */
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
CallingConventions callConvention, Type[] types,
ParameterModifier[] modifiers)
{
+ check_created ();
+
if (ctors == null)
return null;
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) {
return DefineConstructor (attributes, callingConvention, parameterTypes, null, null);
}
-#if NET_2_0 | BOOTSTRAP_NET_2_0
+#if NET_2_0 || BOOTSTRAP_NET_2_0
public
#else
internal
#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];
public ConstructorBuilder DefineDefaultConstructor (MethodAttributes attributes)
{
- ConstructorBuilder cb = DefineConstructor (attributes, CallingConventions.Standard, new Type [0]);
-
Type parent_type;
if (parent != null)
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;
}
return DefineMethod (name, attributes, callingConvention, returnType, null, null, parameterTypes, null, null);
}
-#if NET_2_0 | BOOTSTRAP_NET_2_0
+#if NET_2_0 || BOOTSTRAP_NET_2_0
public
#else
internal
#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)))
return DefinePInvokeMethod (name, dllName, entryName, attributes, callingConvention, returnType, null, null, parameterTypes, null, null, nativeCallConv, nativeCharSet);
}
-#if NET_2_0 | BOOTSTRAP_NET_2_0
+#if NET_2_0 || BOOTSTRAP_NET_2_0
public
#else
internal
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 (
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;
return DefineField (fieldName, type, null, null, attributes);
}
-#if NET_2_0 | BOOTSTRAP_NET_2_0
+#if NET_2_0 || BOOTSTRAP_NET_2_0
public
#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) {
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);
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.
//
// On classes, define a default constructor if not provided
//
- if (!(IsInterface || IsValueType) && (ctors == null) && (tname != "<Module>"))
+ if (!(IsInterface || IsValueType) && (ctors == null) && (tname != "<Module>") &&
+ (GetAttributeFlagsImpl () & TypeAttributes.Abstract | TypeAttributes.Sealed) != (TypeAttributes.Abstract | TypeAttributes.Sealed))
DefineDefaultConstructor (MethodAttributes.Public);
if (ctors != null){
}
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 */
}
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().
}
public override Type GetInterface (string name, bool ignoreCase) {
- throw not_supported ();
+ check_created ();
+ return created.GetInterface (name, ignoreCase);
}
public override Type[] GetInterfaces () {
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) {
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);
}
public override Type GetNestedType( string name, BindingFlags bindingAttr) {
- throw not_supported ();
+ check_created ();
+ return created.GetNestedType (name, bindingAttr);
}
public override Type[] GetNestedTypes (BindingFlags bindingAttr) {
bool match;
ArrayList result = new ArrayList ();
-
+
if (subtypes == null)
return Type.EmptyTypes;
foreach (TypeBuilder t in subtypes) {
}
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 ()
public override RuntimeTypeHandle TypeHandle {
get {
- throw not_supported ();
+ check_created ();
+ return created.TypeHandle;
}
}
int nnamed = (int)data [pos++];
nnamed |= ((int)data [pos++]) << 8;
for (int i = 0; i < nnamed; ++i) {
- byte named_type = data [pos++];
+ //byte named_type = data [pos++];
+ pos ++;
byte type = data [pos++];
- int len = CustomAttributeBuilder.decode_len (data, pos, out pos);
- string named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
+ int len;
+ string named_name;
+
+ if (type == 0x55) {
+ len = CustomAttributeBuilder.decode_len (data, pos, out pos);
+ //string named_typename =
+ CustomAttributeBuilder.string_from_bytes (data, pos, len);
+ pos += len;
+ // FIXME: Check that 'named_type' and 'named_typename' match, etc.
+ // See related code/FIXME in mono/mono/metadata/reflection.c
+ }
+
+ len = CustomAttributeBuilder.decode_len (data, pos, out pos);
+ named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
pos += len;
/* all the fields are integers in StructLayout */
int value = (int)data [pos++];
switch ((CharSet)value) {
case CharSet.None:
case CharSet.Ansi:
+ attrs &= ~(TypeAttributes.UnicodeClass | TypeAttributes.AutoClass);
break;
case CharSet.Unicode:
+ attrs &= ~TypeAttributes.AutoClass;
attrs |= TypeAttributes.UnicodeClass;
break;
case CharSet.Auto:
+ attrs &= ~TypeAttributes.UnicodeClass;
attrs |= TypeAttributes.AutoClass;
break;
default:
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) {
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++;
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
+ setup_internal_class (this);
}
internal int get_next_table_index (object obj, int table, bool inc) {
return pmodule.get_next_table_index (obj, table, inc);
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)
throw new ArgumentException (argName, "Illegal name.");
}
-#if NET_2_0 | BOOTSTRAP_NET_2_0
+ public override String ToString ()
+ {
+ return FullName;
+ }
+
+ [MonoTODO]
+ public override bool IsAssignableFrom (Type c)
+ {
+ return base.IsAssignableFrom (c);
+ }
+
+ [MonoTODO]
+ public override bool IsSubclassOf (Type c)
+ {
+ return base.IsSubclassOf (c);
+ }
+
+ [MonoTODO ("arrays")]
+ internal bool IsAssignableTo (Type c)
+ {
+ if (c == this)
+ return true;
+
+ if (c.IsInterface) {
+ if (interfaces == null)
+ return false;
+ foreach (Type t in interfaces)
+ if (c.IsAssignableFrom (t))
+ return true;
+ return false;
+ }
+
+ if (parent == null)
+ return c == typeof (object);
+ else
+ return c.IsAssignableFrom (parent);
+ }
+
+#if NET_2_0 || BOOTSTRAP_NET_2_0
+ public bool IsCreated () {
+ return is_created;
+ }
+
public override Type[] GetGenericArguments ()
{
if (generic_params != null)
public override Type GetGenericTypeDefinition ()
{
- setup_generic_class (this);
+ create_generic_class ();
return base.GetGenericTypeDefinition ();
}
}
}
- public GenericTypeParameterBuilder[] DefineGenericParameters (string[] names)
+ public GenericTypeParameterBuilder[] DefineGenericParameters (params string[] names)
{
+ setup_generic_class ();
+
generic_params = new GenericTypeParameterBuilder [names.Length];
for (int i = 0; i < names.Length; i++)
generic_params [i] = new GenericTypeParameterBuilder (