//
-// System.Reflection.Emit/TypeBuilder.cs
+// System.Reflection.Emit.TypeBuilder.cs
//
// Author:
// Paolo Molaro (lupus@ximian.com)
#endregion
string fullname;
bool createTypeCalled;
+ private Type underlying_type;
- public const int UnspecifiedTypeSize = 0;
+ public const int UnspecifiedTypeSize = 0;
protected override TypeAttributes GetAttributeFlagsImpl ()
{
this.packing_size = packing_size;
this.nesting_type = nesting_type;
+ check_name ("fullname", name);
+
if (parent == null && (attr & TypeAttributes.Interface) != 0 && (attr & TypeAttributes.Abstract) == 0)
throw new InvalidOperationException ("Interface must be declared abstract.");
}
pmodule = mb;
+ if (((attr & TypeAttributes.Interface) == 0) && (parent == null) && !IsCompilerContext)
+ this.parent = typeof (object);
+
// skip .<Module> ?
table_idx = mb.get_next_table_index (this, 0x02, true);
setup_internal_class (this);
public override string AssemblyQualifiedName {
get {
- return fullname + ", " + Assembly.GetName().FullName;
+ return fullname + ", " + Assembly.FullName;
}
}
public override Type UnderlyingSystemType {
get {
+ if (is_created)
+ return created.UnderlyingSystemType;
+
+ if (IsEnum && !IsCompilerContext) {
+ if (underlying_type != null)
+ return underlying_type;
+ throw new InvalidOperationException (
+ "Enumeration type is not defined.");
+ }
+
return this;
}
}
}
}
- [MonoTODO]
protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder,
CallingConventions callConvention, Type[] types,
ParameterModifier[] modifiers)
{
check_created ();
-
- if (ctors == null)
- return null;
- ConstructorBuilder found = null;
- int count = 0;
+ if (created == typeof (object)) {
+ /*
+ * This happens when building corlib. Calling created.GetConstructor
+ * would return constructors from the real mscorlib, instead of the
+ * newly built one.
+ */
+
+ if (ctors == null)
+ return null;
+
+ ConstructorBuilder found = null;
+ int count = 0;
- foreach (ConstructorBuilder cb in ctors) {
- if (callConvention != CallingConventions.Any && cb.CallingConvention != callConvention)
- continue;
- found = cb;
- count++;
- }
-
- if (count == 0)
- return null;
- if (types == null) {
- if (count > 1)
- throw new AmbiguousMatchException ();
- return found;
- }
- MethodBase[] match = new MethodBase [count];
- if (count == 1)
- match [0] = found;
- else {
- count = 0;
- foreach (ConstructorInfo m in ctors) {
- if (callConvention != CallingConventions.Any && m.CallingConvention != callConvention)
+ foreach (ConstructorBuilder cb in ctors) {
+ if (callConvention != CallingConventions.Any && cb.CallingConvention != callConvention)
continue;
- match [count++] = m;
+ found = cb;
+ count++;
+ }
+
+ if (count == 0)
+ return null;
+ if (types == null) {
+ if (count > 1)
+ throw new AmbiguousMatchException ();
+ return found;
+ }
+ MethodBase[] match = new MethodBase [count];
+ if (count == 1)
+ match [0] = found;
+ else {
+ count = 0;
+ foreach (ConstructorInfo m in ctors) {
+ if (callConvention != CallingConventions.Any && m.CallingConvention != callConvention)
+ continue;
+ match [count++] = m;
+ }
}
+ if (binder == null)
+ binder = Binder.DefaultBinder;
+ return (ConstructorInfo) binder.SelectMethod (bindingAttr, match,
+ types, modifiers);
}
- if (binder == null)
- binder = Binder.DefaultBinder;
- return (ConstructorInfo) binder.SelectMethod (bindingAttr, match,
- types, modifiers);
+
+ return created.GetConstructor (bindingAttr, binder,
+ callConvention, types, modifiers);
}
public override bool IsDefined (Type attributeType, bool inherit)
}
private TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces,
- PackingSize packsize, int typesize)
+ PackingSize packSize, int typeSize)
{
- check_name ("name", name);
// Visibility must be NestedXXX
/* This breaks mcs
if (((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.Public) ||
if (iface == null)
throw new ArgumentNullException ("interfaces");
- TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces, packsize, typesize, this);
+ TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces, packSize, typeSize, this);
res.fullname = res.GetFullName ();
pmodule.RegisterTypeName (res, res.fullname);
if (subtypes != null) {
return DefineNestedType (name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize);
}
- public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typesize)
+ public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typeSize)
{
- return DefineNestedType (name, attr, parent, null, PackingSize.Unspecified, typesize);
+ return DefineNestedType (name, attr, parent, null, PackingSize.Unspecified, typeSize);
}
- public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packsize)
+ public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packSize)
{
- return DefineNestedType (name, attr, parent, null, packsize, UnspecifiedTypeSize);
+ return DefineNestedType (name, attr, parent, null, packSize, UnspecifiedTypeSize);
}
#if NET_2_0
}
ConstructorBuilder cb = DefineConstructor (attributes,
- CallingConventions.Standard, new Type[0]);
+ CallingConventions.Standard, Type.EmptyTypes);
ILGenerator ig = cb.GetILGenerator ();
ig.Emit (OpCodes.Ldarg_0);
ig.Emit (OpCodes.Call, parent_constructor);
!((attributes & MethodAttributes.Abstract) != 0) ||
!((attributes & MethodAttributes.Virtual) != 0)) &&
!(((attributes & MethodAttributes.Static) != 0)))
- throw new ArgumentException ("attributes", "Interface method must be abstract and virtual.");
+ throw new ArgumentException ("Interface method must be abstract and virtual.");
if (returnType == null)
returnType = pmodule.assemblyb.corlib_void_type;
check_name ("dllName", dllName);
check_name ("entryName", entryName);
if ((attributes & MethodAttributes.Abstract) != 0)
- throw new ArgumentException ("attributes", "PInvoke methods must be static and native and cannot be abstract.");
+ throw new ArgumentException ("PInvoke methods must be static and native and cannot be abstract.");
if (IsInterface)
- throw new ArgumentException ("PInvoke methods cannot exist on interfaces.");
+ throw new ArgumentException ("PInvoke methods cannot exist on interfaces.");
check_not_created ();
MethodBuilder res
return DefineMethod (name, attributes, CallingConventions.Standard);
}
- public MethodBuilder DefineMethod (string name, MethodAttributes attributes, CallingConventions callConv)
+ public MethodBuilder DefineMethod (string name, MethodAttributes attributes, CallingConventions callingConvention)
{
- return DefineMethod (name, attributes, callConv, null, null);
+ return DefineMethod (name, attributes, callingConvention, null, null);
}
#endif
#else
internal
#endif
- FieldBuilder DefineField (string fieldName, Type type, Type[] requiredCustomAttributes, Type[] optionalCustomAttributes, FieldAttributes attributes)
+ FieldBuilder DefineField (string fieldName, Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers, FieldAttributes attributes)
{
check_name ("fieldName", fieldName);
if (type == typeof (void))
- throw new ArgumentException ("type", "Bad field type in defining field.");
+ throw new ArgumentException ("Bad field type in defining field.");
check_not_created ();
- FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes, requiredCustomAttributes, optionalCustomAttributes);
+ FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes, requiredCustomModifiers, optionalCustomModifiers);
if (fields != null) {
if (fields.Length == num_fields) {
FieldBuilder[] new_fields = new FieldBuilder [fields.Length * 2];
num_fields ++;
create_internal_class (this);
}
+
+ if (IsEnum && !IsCompilerContext) {
+ if (underlying_type == null && (attributes & FieldAttributes.Static) == 0)
+ underlying_type = type;
+ }
+
return res;
}
}
return false;
}
+
+ // Return whenever this type has a ctor defined using DefineMethod ()
+ private bool has_ctor_method () {
+ MethodAttributes ctor_attrs = MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
+
+ for (int i = 0; i < num_methods; ++i) {
+ MethodBuilder mb = (MethodBuilder)(methods[i]);
+
+ if (mb.Name == ConstructorInfo.ConstructorName && (mb.Attributes & ctor_attrs) == ctor_attrs)
+ return true;
+ }
+
+ return false;
+ }
public Type CreateType()
{
throw new TypeLoadException ("Could not load type '" + FullName + "' from assembly '" + Assembly + "' because it is an enum with methods.");
if (methods != null) {
- for (int i = 0; i < num_methods; ++i)
- ((MethodBuilder)(methods[i])).fixup ();
+ for (int i = 0; i < num_methods; ++i) {
+ MethodBuilder mb = (MethodBuilder)(methods[i]);
+ mb.check_override ();
+ mb.fixup ();
+ }
}
//
// On classes, define a default constructor if not provided
//
if (!(IsInterface || IsValueType) && (ctors == null) && (tname != "<Module>") &&
- (GetAttributeFlagsImpl () & TypeAttributes.Abstract | TypeAttributes.Sealed) != (TypeAttributes.Abstract | TypeAttributes.Sealed))
+ (GetAttributeFlagsImpl () & TypeAttributes.Abstract | TypeAttributes.Sealed) != (TypeAttributes.Abstract | TypeAttributes.Sealed) && !has_ctor_method ())
DefineDefaultConstructor (MethodAttributes.Public);
if (ctors != null){
}
symbolWriter.CloseNamespace ();
+
+ if (subtypes != null) {
+ for (int i = 0; i < subtypes.Length; ++i)
+ subtypes [i].GenerateDebugInfo (symbolWriter);
+ }
}
#if NET_2_0
#endif
public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
{
+ if (is_created)
+ return created.GetConstructors (bindingAttr);
+
if (ctors == null)
return new ConstructorInfo [0];
ArrayList l = new ArrayList ();
public override FieldInfo[] GetFields (BindingFlags bindingAttr)
{
-#if NET_2_0 || BOOTSTRAP_NET_2_0
- // FIXME: In the generic case, this leads to a crash
- // http://bugzilla.ximian.com/show_bug.cgi?id=82625
- if (!ContainsGenericParameters && (created != null))
- return created.GetFields (bindingAttr);
-#else
if (created != null)
return created.GetFields (bindingAttr);
-#endif
if (fields == null)
return new FieldInfo [0];
private MethodInfo[] GetMethodsByName (string name, BindingFlags bindingAttr, bool ignoreCase, Type reflected_type)
{
MethodInfo[] candidates;
+ bool match;
+ MethodAttributes mattrs;
+
if (((bindingAttr & BindingFlags.DeclaredOnly) == 0) && (parent != null)) {
- BindingFlags parentBindingAttr = bindingAttr & (~BindingFlags.Static);
- MethodInfo [] parent_methods = parent.GetMethods (parentBindingAttr);
- if (methods == null)
- candidates = parent_methods;
- else {
- candidates = new MethodInfo [methods.Length + parent_methods.Length];
- parent_methods.CopyTo (candidates, 0);
- methods.CopyTo (candidates, parent_methods.Length);
+ MethodInfo [] parent_methods = parent.GetMethods (bindingAttr);
+ ArrayList parent_candidates = new ArrayList (parent_methods.Length);
+
+ bool flatten = (bindingAttr & BindingFlags.FlattenHierarchy) != 0;
+
+ for (int i = 0; i < parent_methods.Length; i++) {
+ MethodInfo m = parent_methods [i];
+
+ mattrs = m.Attributes;
+
+ if (m.IsStatic && !flatten)
+ continue;
+
+ switch (mattrs & MethodAttributes.MemberAccessMask) {
+ case MethodAttributes.Public:
+ match = (bindingAttr & BindingFlags.Public) != 0;
+ break;
+ case MethodAttributes.Assembly:
+#if NET_2_0
+ match = (bindingAttr & BindingFlags.NonPublic) != 0;
+#else
+ match = false;
+#endif
+ break;
+ case MethodAttributes.Private:
+ match = false;
+ break;
+ default:
+ match = (bindingAttr & BindingFlags.NonPublic) != 0;
+ break;
+ }
+
+ if (match)
+ parent_candidates.Add (m);
+ }
+
+ if (methods == null) {
+ candidates = new MethodInfo [parent_candidates.Count];
+ parent_candidates.CopyTo (candidates);
+ } else {
+ candidates = new MethodInfo [methods.Length + parent_candidates.Count];
+ parent_candidates.CopyTo (candidates, 0);
+ methods.CopyTo (candidates, parent_candidates.Count);
}
}
else
return new MethodInfo [0];
ArrayList l = new ArrayList ();
- bool match;
- MethodAttributes mattrs;
foreach (MethodInfo c in candidates) {
if (c == null)
public override PropertyInfo[] GetProperties (BindingFlags bindingAttr)
{
+ if (is_created)
+ return created.GetProperties (bindingAttr);
+
if (properties == null)
return new PropertyInfo [0];
ArrayList l = new ArrayList ();
return false;
}
+ // FIXME: I doubt just removing this still works.
protected override bool IsValueTypeImpl ()
{
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_enum_type);
}
+#if NET_2_0
+ [MonoTODO]
+ public override Type MakeArrayType ()
+ {
+ return base.MakeArrayType ();
+ }
+
+ [MonoTODO]
+ public override Type MakeArrayType (int rank)
+ {
+ return base.MakeArrayType (rank);
+ }
+
+ [MonoTODO]
+ public override Type MakeByRefType ()
+ {
+ return base.MakeByRefType ();
+ }
+
+ [MonoTODO]
+ public override Type MakeGenericType (params Type [] typeArguments)
+ {
+ return base.MakeGenericType (typeArguments);
+ }
+
+ [MonoTODO]
+ public override Type MakePointerType ()
+ {
+ return base.MakePointerType ();
+ }
+#endif
+
public override RuntimeTypeHandle TypeHandle {
get {
check_created ();
{
check_name ("name", name);
if (eventtype == null)
- throw new ArgumentNullException ("eventtype");
+ throw new ArgumentNullException ("type");
check_not_created ();
EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
public FieldBuilder DefineInitializedData (string name, byte[] data, FieldAttributes attributes) {
if (data == null)
throw new ArgumentNullException ("data");
- if ((data.Length == 0) || (data.Length > 0x3f0000))
- throw new ArgumentException ("data", "Data size must be > 0 and < 0x3f0000");
FieldBuilder res = DefineUninitializedData (name, data.Length, attributes);
res.SetRVAData (data);
-
return res;
}
static int UnmanagedDataCount = 0;
public FieldBuilder DefineUninitializedData (string name, int size, FieldAttributes attributes)
- {
- check_name ("name", name);
+ {
+ if (name == null)
+ throw new ArgumentNullException ("name");
+ if (name.Length == 0)
+ throw new ArgumentException ("Empty name is not legal", "name");
if ((size <= 0) || (size > 0x3f0000))
- throw new ArgumentException ("size", "Data size must be > 0 and < 0x3f0000");
+ throw new ArgumentException ("Data size must be > 0 and < 0x3f0000");
check_not_created ();
string typeName = "$ArrayType$" + size;
return created.GetInterfaceMap (interfaceType);
}
+ internal bool IsCompilerContext {
+ get {
+ return pmodule.assemblyb.IsCompilerContext;
+ }
+ }
+
internal bool is_created {
get {
return created != null;
throw new ArgumentNullException (argName);
if (name.Length == 0)
throw new ArgumentException ("Empty name is not legal", argName);
- if (name.IndexOf ((char)0) != -1)
+ if (name [0] == ((char)0))
throw new ArgumentException ("Illegal name", argName);
}
return true;
if (c.IsInterface) {
+ if (parent != null && is_created) {
+ if (c.IsAssignableFrom (parent))
+ return true;
+ }
+
if (interfaces == null)
return false;
foreach (Type t in interfaces)
if (c.IsAssignableFrom (t))
return true;
- return false;
+ if (!is_created)
+ return false;
}
if (parent == null)
return generic_params;
}
- public static ConstructorInfo GetConstructor (Type instanciated, ConstructorInfo ctor)
+ public static ConstructorInfo GetConstructor (Type type, ConstructorInfo constructor)
{
- ConstructorInfo res = instanciated.GetConstructor (ctor);
+ ConstructorInfo res = type.GetConstructor (constructor);
if (res == null)
throw new System.Exception ("constructor not found");
else
return res;
}
- public static MethodInfo GetMethod (Type instanciated, MethodInfo meth)
+ static bool IsValidGetMethodType (Type type)
{
- MethodInfo res = instanciated.GetMethod (meth);
+ if (type is TypeBuilder || type is MonoGenericClass)
+ return true;
+ /*GetMethod() must work with TypeBuilders after CreateType() was called.*/
+ if (type.Module is ModuleBuilder)
+ return true;
+ if (type.IsGenericParameter)
+ return false;
+
+ Type[] inst = type.GetGenericArguments ();
+ if (inst == null)
+ return false;
+ for (int i = 0; i < inst.Length; ++i) {
+ if (IsValidGetMethodType (inst [i]))
+ return true;
+ }
+ return false;
+ }
+
+ public static MethodInfo GetMethod (Type type, MethodInfo method)
+ {
+ if (!IsValidGetMethodType (type))
+ throw new ArgumentException ("type is not TypeBuilder but " + type.GetType (), "type");
+
+ if (!type.IsGenericType)
+ throw new ArgumentException ("type is not a generic type", "type");
+
+ if (!method.DeclaringType.IsGenericTypeDefinition)
+ throw new ArgumentException ("method declaring type is not a generic type definition", "method");
+ if (method.DeclaringType != type.GetGenericTypeDefinition ())
+ throw new ArgumentException ("method declaring type is not the generic type definition of type", "method");
+
+ MethodInfo res = type.GetMethod (method);
if (res == null)
- throw new System.Exception ("method not found");
- else
- return res;
+ throw new ArgumentException (String.Format ("method {0} not found in type {1}", method.Name, type));
+
+ return res;
}
- public static FieldInfo GetField (Type instanciated, FieldInfo fld)
+ public static FieldInfo GetField (Type type, FieldInfo field)
{
- FieldInfo res = instanciated.GetField (fld);
+ FieldInfo res = type.GetField (field);
if (res == null)
throw new System.Exception ("field not found");
else