// (C) 2003 Ximian, Inc.
//
using System;
-using System.Collections;
+using System.Reflection;
using System.Reflection.Emit;
+using System.Globalization;
+using System.Collections;
using System.Text;
namespace Mono.CSharp {
}
bool has_ctor_constraint;
- Type class_constraint;
+ TypeExpr class_constraint;
ArrayList iface_constraints;
- Type[] constraint_types;
+ TypeExpr[] constraint_types;
int num_constraints;
public bool HasConstructorConstraint {
get { return has_ctor_constraint; }
}
- public Type[] Types {
- get { return constraint_types; }
- }
-
- public bool Resolve (TypeContainer tc)
+ public bool Resolve (DeclSpace ds)
{
iface_constraints = new ArrayList ();
- if (constraints == null) {
- constraint_types = new Type [0];
- return true;
- }
-
foreach (object obj in constraints) {
if (has_ctor_constraint) {
Error ("can only use one constructor constraint and " +
continue;
}
- Expression expr = tc.ResolveTypeExpr ((Expression) obj, false, loc);
+ TypeExpr expr = ds.ResolveTypeExpr ((Expression) obj, false, loc);
if (expr == null)
return false;
- Type etype = expr.Type;
- if (etype.IsInterface)
- iface_constraints.Add (etype);
+ if (expr.IsInterface)
+ iface_constraints.Add (expr);
else if (class_constraint != null) {
Error ("can have at most one class constraint.");
return false;
} else
- class_constraint = etype;
+ class_constraint = expr;
num_constraints++;
}
- constraint_types = new Type [num_constraints];
+ constraint_types = new TypeExpr [num_constraints];
int pos = 0;
if (class_constraint != null)
constraint_types [pos++] = class_constraint;
return true;
}
+
+ public Type[] ResolveTypes (EmitContext ec)
+ {
+ Type [] types = new Type [constraint_types.Length];
+
+ for (int i = 0; i < constraint_types.Length; i++)
+ types [i] = constraint_types [i].ResolveType (ec);
+
+ return types;
+ }
}
//
}
}
- public bool Resolve (TypeContainer tc)
+ public bool Resolve (DeclSpace ds)
{
if (constraints != null)
- return constraints.Resolve (tc);
+ return constraints.Resolve (ds);
return true;
}
public Type Define (TypeBuilder tb)
{
- if (constraints != null)
- type = tb.DefineGenericParameter (name, constraints.Types);
- else
- type = tb.DefineGenericParameter (name, new Type [0]);
+ type = tb.DefineGenericParameter (name);
+ return type;
+ }
+ public Type DefineMethod (MethodBuilder mb)
+ {
+ type = mb.DefineGenericParameter (name);
return type;
}
+ public void DefineType (EmitContext ec, TypeBuilder tb)
+ {
+ int index = type.GenericParameterPosition;
+ if (constraints == null)
+ tb.SetGenericParameterConstraints (index, new Type [0]);
+ else
+ tb.SetGenericParameterConstraints (index, constraints.ResolveTypes (ec));
+ }
+
+ public void DefineType (EmitContext ec, MethodBuilder mb)
+ {
+ int index = type.GenericParameterPosition;
+ if (constraints == null)
+ mb.SetGenericParameterConstraints (index, new Type [0]);
+ else
+ mb.SetGenericParameterConstraints (index, constraints.ResolveTypes (ec));
+ }
+
public override string ToString ()
{
return "TypeParameter[" + name + "]";
public class TypeParameterExpr : TypeExpr {
TypeParameter type_parameter;
- public string Name {
+ public override string Name {
get {
return type_parameter.Name;
}
}
public TypeParameterExpr (TypeParameter type_parameter, Location loc)
- : base (type_parameter.Type, loc)
{
this.type_parameter = type_parameter;
}
- public override string ToString ()
+ public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
{
- return "TypeParameterExpr[" + type_parameter.Name + "]";
+ type = type_parameter.Type;
+
+ return this;
}
public void Error_CannotUseAsUnmanagedType (Location loc)
public class TypeArguments {
ArrayList args;
+ Type[] atypes;
+ bool has_type_args;
public TypeArguments ()
{
args.Add (type);
}
+ public void Add (TypeArguments new_args)
+ {
+ args.AddRange (new_args.args);
+ }
+
public Type[] Arguments {
get {
- Type[] retval = new Type [args.Count];
- for (int i = 0; i < args.Count; i++)
- retval [i] = ((TypeExpr) args [i]).Type;
+ return atypes;
+ }
+ }
- return retval;
+ public bool HasTypeArguments {
+ get {
+ return has_type_args;
}
}
{
int count = args.Count;
bool ok = true;
+
+ atypes = new Type [count];
for (int i = 0; i < count; i++){
- Expression e = ((Expression)args [i]).ResolveAsTypeTerminal (ec);
- if (e == null)
+ TypeExpr e = ((Expression)args [i]).ResolveAsTypeTerminal (ec);
+ if (e == null) {
ok = false;
+ continue;
+ }
+ if (e is TypeParameterExpr)
+ has_type_args = true;
args [i] = e;
+ atypes [i] = e.ResolveType (ec);
+
+ if (atypes [i] == null)
+ ok = false;
}
return ok;
}
}
- public class ConstructedType : Expression {
- Expression container_type;
- string name;
+ public class ConstructedType : TypeExpr {
+ string name, full_name;
TypeArguments args;
+ Type[] gen_params, atypes;
+ Type gt;
public ConstructedType (string name, TypeArguments args, Location l)
{
loc = l;
- this.container_type = container_type;
this.name = name;
this.args = args;
+
eclass = ExprClass.Type;
+ full_name = name + "<" + args.ToString () + ">";
}
- public override Expression DoResolve (EmitContext ec)
+ public ConstructedType (string name, TypeParameter[] type_params, Location l)
{
- if (args.Resolve (ec) == false)
- return null;
+ loc = l;
+ this.name = name;
+
+ args = new TypeArguments ();
+ foreach (TypeParameter type_param in type_params)
+ args.Add (new TypeParameterExpr (type_param, l));
+
+ eclass = ExprClass.Type;
+ full_name = name + "<" + args.ToString () + ">";
+ }
+
+ public ConstructedType (Type t, TypeParameter[] type_params, Location l)
+ : this (t.Name, type_params, l)
+ {
+ gt = t.GetGenericTypeDefinition ();
+ }
+
+ public ConstructedType (Type t, TypeArguments args, Location l)
+ : this (t.Name, args, l)
+ {
+ gt = t.GetGenericTypeDefinition ();
+ }
+
+ public TypeArguments TypeArguments {
+ get { return args; }
+ }
+
+ protected bool CheckConstraints (int index)
+ {
+ Type atype = atypes [index];
+ Type ptype = gen_params [index];
+
+ //// FIXME
+ return true;
//
- // Pretend there are not type parameters, until we get GetType support
+ // First, check parent class.
//
- return new SimpleName (name, loc).DoResolve (ec);
+ if ((ptype.BaseType != atype.BaseType) &&
+ !atype.BaseType.IsSubclassOf (ptype.BaseType)) {
+ Report.Error (-219, loc, "Cannot create constructed type `{0}': " +
+ "type argument `{1}' must derive from `{2}'.",
+ full_name, atype, ptype.BaseType);
+ return false;
+ }
+
+ //
+ // Now, check the interfaces.
+ //
+ foreach (Type itype in ptype.GetInterfaces ()) {
+ if (TypeManager.ImplementsInterface (atype, itype))
+ continue;
+
+ Report.Error (-219, loc, "Cannot create constructed type `{0}: " +
+ "type argument `{1}' must implement interface `{2}'.",
+ full_name, atype, itype);
+ return false;
+ }
+
+ return true;
}
- public override Expression ResolveAsTypeStep (EmitContext ec)
+ public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
{
- if (args.Resolve (ec) == false)
- return null;
+ if (gt != null)
+ return this;
//
// First, resolve the generic type.
//
SimpleName sn = new SimpleName (name, loc);
- Expression resolved = sn.ResolveAsTypeStep (ec);
+ TypeExpr resolved = sn.ResolveAsTypeTerminal (ec);
if (resolved == null)
return null;
- Type gt = resolved.Type.GetGenericTypeDefinition ();
+ if (resolved.Type == null) {
+ Report.Error (-220, loc, "Failed to resolve constructed type `{0}'",
+ full_name);
+ return null;
+ }
+
+ gt = resolved.Type.GetGenericTypeDefinition ();
+ return this;
+ }
- Type[] gen_params = gt.GetGenericParameters ();
- if (args.Arguments.Length != gen_params.Length) {
- Report.Error (-217, loc, "Generic type `" + gt.Name + "' takes " +
- gen_params.Length + " type parameters, but specified " +
- args.Arguments.Length + ".");
+ public override Type ResolveType (EmitContext ec)
+ {
+ if (type != null)
+ return type;
+ if (DoResolveAsTypeStep (ec) == null)
return null;
+
+ //
+ // Resolve the arguments.
+ //
+ if (args.Resolve (ec) == false) {
+ Report.Error (-220, loc, "Failed to resolve constructed type `{0}'",
+ full_name);
+ return null;
+ }
+
+ gen_params = gt.GetGenericArguments ();
+
+ DeclSpace decl = ec.DeclSpace, parent = null;
+ while (decl != null) {
+ if (TypeManager.IsNestedChildOf (gt, decl.TypeBuilder)) {
+ parent = decl;
+ break;
+ }
+ decl = decl.Parent;
+ }
+
+ if (parent != null) {
+ TypeParameter[] pparams = parent.TypeParameters;
+
+ atypes = new Type [args.Arguments.Length + pparams.Length];
+ for (int i = 0; i < pparams.Length; i++) {
+ TypeParameter pparam = pparams [i];
+ if (pparam.Type == null)
+ throw new Exception ();
+ atypes [i] = pparam.Type;
+ }
+ args.Arguments.CopyTo (atypes, pparams.Length);
+ } else
+ atypes = args.Arguments;
+
+ if (atypes.Length != gen_params.Length) {
+ Report.Error (-217, loc, "Generic type `{0}' takes {1} " +
+ "type parameters, but specified {2}.", gt.Name,
+ gen_params.Length, atypes.Length);
+ return null;
+ }
+
+ for (int i = 0; i < gen_params.Length; i++) {
+ if (!CheckConstraints (i))
+ return null;
}
//
// Now bind the parameters.
//
- Type ntype = gt.BindGenericParameters (args.Arguments);
- return new TypeExpr (ntype, loc);
+ type = gt.BindGenericParameters (atypes);
+ return type;
}
-
- public override void Emit (EmitContext ec)
+
+ public Expression GetMemberAccess (TypeExpr current_type)
{
- //
- // Never reached for now
- //
- throw new Exception ("IMPLEMENT ME");
+ return new GenericMemberAccess (current_type, name, args, loc);
}
- public override string ToString ()
+ public override bool CheckAccessLevel (DeclSpace ds)
+ {
+ return ds.CheckAccessLevel (gt);
+ }
+
+ public override bool AsAccessible (DeclSpace ds, int flags)
+ {
+ return ds.AsAccessible (gt, flags);
+ }
+
+ public override bool IsClass {
+ get { return gt.IsClass; }
+ }
+
+ public override bool IsValueType {
+ get { return gt.IsValueType; }
+ }
+
+ public override bool IsInterface {
+ get { return gt.IsInterface; }
+ }
+
+ public override bool IsSealed {
+ get { return gt.IsSealed; }
+ }
+
+ public override TypeExpr[] GetInterfaces ()
+ {
+ TypeExpr[] ifaces = TypeManager.GetInterfaces (gt);
+ return ifaces;
+ }
+
+ public override string Name {
+ get {
+ return full_name;
+ }
+ }
+ }
+
+ public class GenericMethod : DeclSpace
+ {
+ public GenericMethod (NamespaceEntry ns, TypeContainer parent, string name,
+ Attributes attrs, Location l)
+ : base (ns, parent, name, attrs, l)
+ { }
+
+ public override TypeBuilder DefineType ()
+ {
+ throw new Exception ();
+ }
+
+ public override bool Define (TypeContainer parent)
+ {
+ return true;
+ }
+
+ public bool Define (MethodBuilder mb)
+ {
+ Type[] gen_params = new Type [TypeParameters.Length];
+ for (int i = 0; i < TypeParameters.Length; i++)
+ gen_params [i] = TypeParameters [i].DefineMethod (mb);
+
+ return true;
+ }
+
+ public override bool DefineMembers (TypeContainer parent)
+ {
+ return true;
+ }
+
+ public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
+ MemberFilter filter, object criteria)
+ {
+ throw new Exception ();
+ }
+
+ public override MemberCache MemberCache {
+ get {
+ throw new Exception ();
+ }
+ }
+ }
+
+ public class GenericMemberAccess : MemberAccess
+ {
+ TypeArguments args;
+
+ public GenericMemberAccess (Expression expr, string id, TypeArguments args, Location loc)
+ : base (expr, id, loc)
+ {
+ this.args = args;
+ }
+
+ public override Expression DoResolve (EmitContext ec, Expression right_side,
+ ResolveFlags flags)
+ {
+ Expression expr = base.DoResolve (ec, right_side, flags);
+ if (expr == null)
+ return null;
+
+ MethodGroupExpr mg = expr as MethodGroupExpr;
+ if (mg == null) {
+ Report.Error (-220, loc, "Member `{0}' has type arguments, but did " +
+ "not resolve as a method group.", Identifier);
+ return null;
+ }
+
+ if (args.Resolve (ec) == false)
+ return null;
+
+ Type[] atypes = args.Arguments;
+
+ ArrayList list = new ArrayList ();
+
+ foreach (MethodBase method in mg.Methods) {
+ MethodInfo mi = method as MethodInfo;
+ if (mi == null)
+ continue;
+
+ Type[] gen_params = mi.GetGenericArguments ();
+
+ if (atypes.Length != gen_params.Length) {
+ Report.Error (-217, loc, "Generic method `{0}' takes {1} " +
+ "type parameters, but specified {2}.", mi.Name,
+ gen_params.Length, atypes.Length);
+ continue;
+ }
+
+ list.Add (mi.BindGenericParameters (args.Arguments));
+ }
+
+ MethodInfo[] methods = new MethodInfo [list.Count];
+ list.CopyTo (methods, 0);
+
+ MethodGroupExpr new_mg = new MethodGroupExpr (methods, mg.Location);
+ new_mg.InstanceExpression = mg.InstanceExpression;
+ return new_mg;
+ }
+
+ public override Expression ResolveAsTypeStep (EmitContext ec)
+ {
+ ConstructedType cexpr = expr as ConstructedType;
+ if (cexpr == null)
+ throw new Exception ();
+
+ expr = base.ResolveAsTypeStep (ec);
+ if (expr == null)
+ return null;
+
+ Type t = ((TypeExpr) expr).ResolveType (ec);
+ if (t == null)
+ return null;
+
+ TypeArguments new_args = new TypeArguments ();
+ new_args.Add (cexpr.TypeArguments);
+ new_args.Add (args);
+
+ ConstructedType ctype = new ConstructedType (t, new_args, loc);
+ return ctype.ResolveAsTypeStep (ec);
+ }
+ }
+
+ public class DefaultValueExpression : Expression
+ {
+ Expression expr;
+ LocalTemporary temp_storage;
+
+ public DefaultValueExpression (Expression expr, Location loc)
+ {
+ this.expr = expr;
+ this.loc = loc;
+ }
+
+ public override Expression DoResolve (EmitContext ec)
+ {
+ type = ec.DeclSpace.ResolveType (expr, false, loc);
+ if (type == null)
+ return null;
+
+ if (type.IsGenericParameter || TypeManager.IsValueType (type))
+ temp_storage = new LocalTemporary (ec, type);
+
+ eclass = ExprClass.Variable;
+ return this;
+ }
+
+ public override void Emit (EmitContext ec)
{
- return name + "<" + args.ToString () + ">";
+ if (temp_storage != null) {
+ temp_storage.AddressOf (ec, AddressOp.LoadStore);
+ ec.ig.Emit (OpCodes.Initobj, type);
+ temp_storage.Emit (ec);
+ } else
+ ec.ig.Emit (OpCodes.Ldnull);
}
}
}