updated browser capabilities file
[mono.git] / mcs / gmcs / generic.cs
index b54ab689fa9af415c6353755b7f6a6f17342e701..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 (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 " +
@@ -82,23 +73,22 @@ namespace Mono.CSharp {
                                        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;
@@ -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;
+               }
        }
 
        //
@@ -113,7 +113,6 @@ namespace Mono.CSharp {
        //
        public class TypeParameter {
                string name;
-               int index;
                Constraints constraints;
                Location loc;
                Type type;
@@ -125,12 +124,6 @@ namespace Mono.CSharp {
                        this.loc = loc;
                }
 
-               public TypeParameter (string name, int index, Constraints constraints, Location loc)
-                       : this (name, constraints, loc)
-               {
-                       this.index = index;
-               }
-
                public string Name {
                        get {
                                return name;
@@ -155,38 +148,42 @@ namespace Mono.CSharp {
                        }
                }
 
-               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 (index > 0)
-                               throw new InvalidOperationException ();
-
-                       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 (TypeBuilder tb)
+               public Type DefineMethod (MethodBuilder mb)
                {
-                       if (index == 0)
-                               throw new InvalidOperationException ();
+                       type = mb.DefineGenericParameter (name);
+                       return type;
+               }
 
-                       if (constraints != null)
-                               type = tb.DefineGenericParameter (name, index - 1, constraints.Types);
+               public void DefineType (EmitContext ec, TypeBuilder tb)
+               {
+                       int index = type.GenericParameterPosition;
+                       if (constraints == null)
+                               tb.SetGenericParameterConstraints (index, new Type [0]);
                        else
-                               type = tb.DefineGenericParameter (name, index - 1, 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 ()
@@ -203,7 +200,7 @@ namespace Mono.CSharp {
        public class TypeParameterExpr : TypeExpr {
                TypeParameter type_parameter;
 
-               public string Name {
+               public override string Name {
                        get {
                                return type_parameter.Name;
                        }
@@ -216,23 +213,17 @@ namespace Mono.CSharp {
                }
                
                public TypeParameterExpr (TypeParameter type_parameter, Location loc)
-                       : base (type_parameter.Type, loc)
                {
                        this.type_parameter = type_parameter;
                }
 
-               public override Expression ResolveAsTypeStep (EmitContext ec)
+               public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
                {
                        type = type_parameter.Type;
 
                        return this;
                }
 
-               public override string ToString ()
-               {
-                       return "TypeParameterExpr[" + type_parameter.Name + "]";
-               }
-
                public void Error_CannotUseAsUnmanagedType (Location loc)
                {
                        Report.Error (-203, loc, "Can not use type parameter as unamanged type");
@@ -254,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;
@@ -290,49 +286,71 @@ namespace Mono.CSharp {
                        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.Type;
+                               atypes [i] = e.ResolveType (ec);
+
+                               if (atypes [i] == null)
+                                       ok = false;
                        }
                        return ok;
                }
        }
        
-       public class ConstructedType : Expression {
-               Expression container_type;
+       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 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 override Expression DoResolve (EmitContext ec)
+               public ConstructedType (Type t, TypeParameter[] type_params, Location l)
+                       : this (t.Name, type_params, l)
                {
-                       if (args.Resolve (ec) == false)
-                               return null;
+                       gt = t.GetGenericTypeDefinition ();
+               }
 
-                       //
-                       // Pretend there are not type parameters, until we get GetType support
-                       //
-                       return new SimpleName (name, loc).DoResolve (ec);
+               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
@@ -365,22 +383,69 @@ namespace Mono.CSharp {
                        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 ();
-                       gen_params = gt.GetGenericParameters ();
-                       atypes = args.Arguments;
+                       if (resolved.Type == null) {
+                               Report.Error (-220, loc, "Failed to resolve constructed type `{0}'",
+                                             full_name);
+                               return null;
+                       }
+
+                       gt = resolved.Type.GetGenericTypeDefinition ();
+                       return this;
+               }
+
+               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} " +
@@ -389,9 +454,6 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       if (args.HasTypeArguments)
-                               return new TypeExpr (gt, loc);
-
                        for (int i = 0; i < gen_params.Length; i++) {
                                if (!CheckConstraints (i))
                                        return null;
@@ -400,28 +462,59 @@ namespace Mono.CSharp {
                        //
                        // 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 full_name;
+                       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, 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 ()
@@ -431,8 +524,14 @@ namespace Mono.CSharp {
 
                public override bool Define (TypeContainer parent)
                {
-                       foreach (TypeParameter type_param in TypeParameters)
-                               type_param.DefineMethod (parent.TypeBuilder);
+                       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;
                }
@@ -454,4 +553,118 @@ namespace Mono.CSharp {
                        }
                }
        }
+
+       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)
+               {
+                       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);
+               }
+       }
 }