}
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 (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 = ds.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 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)
{
- if (constraints != null)
- type = mb.DefineGenericParameter (name, constraints.Types);
+ 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
- type = mb.DefineGenericParameter (name, new Type [0]);
+ tb.SetGenericParameterConstraints (index, constraints.ResolveTypes (ec));
+ }
- return type;
+ 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 ()
args.Add (type);
}
+ public void Add (TypeArguments new_args)
+ {
+ args.AddRange (new_args.args);
+ }
+
public Type[] Arguments {
get {
return atypes;
atypes = new Type [count];
for (int i = 0; i < count; i++){
- Expression e = ((Expression)args [i]).ResolveAsTypeTerminal (ec);
+ 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.Type;
+ atypes [i] = e.ResolveType (ec);
+
+ if (atypes [i] == null)
+ ok = false;
}
return ok;
}
loc = l;
this.name = name;
this.args = args;
+
eclass = ExprClass.Type;
+ full_name = name + "<" + args.ToString () + ">";
+ }
+ public ConstructedType (string name, TypeParameter[] type_params, Location l)
+ {
+ 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 = args.Arguments [index];
+ Type atype = atypes [index];
Type ptype = gen_params [index];
//// FIXME
public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
{
+ if (gt != null)
+ return this;
+
//
// First, resolve the generic type.
//
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)
+ if (args.Resolve (ec) == false) {
+ Report.Error (-220, loc, "Failed to resolve constructed type `{0}'",
+ full_name);
return null;
+ }
gen_params = gt.GetGenericArguments ();
- atypes = args.Arguments;
+
+ 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} " +
return type;
}
+ public Expression GetMemberAccess (TypeExpr current_type)
+ {
+ return new GenericMemberAccess (current_type, name, args, loc);
+ }
+
public override bool CheckAccessLevel (DeclSpace ds)
{
return ds.CheckAccessLevel (gt);
public class GenericMethod : DeclSpace
{
- public GenericMethod (NamespaceEntry ns, TypeContainer parent, string name, Location l)
- : base (ns, parent, name, l)
+ public GenericMethod (NamespaceEntry ns, TypeContainer parent, string name,
+ Attributes attrs, Location l)
+ : base (ns, parent, name, attrs, l)
{ }
public override TypeBuilder DefineType ()
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