updated browser capabilities file
[mono.git] / mcs / gmcs / generic.cs
index 1d6668370eed574dae03451b329bb73134b1268b..b26083f895cd59c2f8715c0efa27bcd31f0e2ace 100644 (file)
@@ -48,28 +48,19 @@ 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 (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 " +
@@ -82,23 +73,22 @@ namespace Mono.CSharp {
                                        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;
@@ -106,6 +96,16 @@ namespace Mono.CSharp {
 
                        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;
+               }
        }
 
        //
@@ -158,22 +158,32 @@ namespace Mono.CSharp {
 
                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 ()
@@ -235,6 +245,11 @@ namespace Mono.CSharp {
                        args.Add (type);
                }
 
+               public void Add (TypeArguments new_args)
+               {
+                       args.AddRange (new_args.args);
+               }
+
                public Type[] Arguments {
                        get {
                                return atypes;
@@ -299,14 +314,43 @@ namespace Mono.CSharp {
                        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
@@ -341,6 +385,9 @@ namespace Mono.CSharp {
 
                public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
                {
+                       if (gt != null)
+                               return this;
+
                        //
                        // First, resolve the generic type.
                        //
@@ -361,6 +408,11 @@ namespace Mono.CSharp {
 
                public override Type ResolveType (EmitContext ec)
                {
+                       if (type != null)
+                               return type;
+                       if (DoResolveAsTypeStep (ec) == null)
+                               return null;
+
                        //
                        // Resolve the arguments.
                        //
@@ -371,7 +423,29 @@ namespace Mono.CSharp {
                        }
 
                        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} " +
@@ -392,6 +466,11 @@ namespace Mono.CSharp {
                        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);
@@ -433,8 +512,9 @@ namespace Mono.CSharp {
 
        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 ()
@@ -529,6 +609,28 @@ namespace Mono.CSharp {
                        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