New tests.
[mono.git] / mcs / mcs / linq.cs
index 44c7434d255cfbe38c34fa8cb14647126d6a6296..420dae4bf832e7c4fcc18125e7c51c3f31bb855b 100644 (file)
@@ -10,7 +10,7 @@
 
 using System;
 using System.Reflection;
-using System.Collections;
+using System.Collections.Generic;
 
 namespace Mono.CSharp.Linq
 {
@@ -33,12 +33,13 @@ namespace Mono.CSharp.Linq
                        return next.BuildQueryClause (ec, lSide);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        int counter = QueryBlock.TransparentParameter.Counter;
 
                        Expression e = BuildQueryClause (ec, null);
-                       e = e.Resolve (ec);
+                       if (e != null)
+                               e = e.Resolve (ec);
 
                        //
                        // Reset counter in probing mode to ensure that all transparent
@@ -69,8 +70,8 @@ namespace Mono.CSharp.Linq
                        {
                        }
 
-                       protected override Expression Error_MemberLookupFailed (ResolveContext ec, Type container_type, Type qualifier_type,
-                               Type queried_type, string name, string class_name, MemberTypes mt, BindingFlags bf)
+                       protected override Expression Error_MemberLookupFailed (ResolveContext ec, TypeSpec container_type, TypeSpec qualifier_type,
+                               TypeSpec queried_type, string name, int arity, string class_name, MemberKind mt, BindingRestriction bf)
                        {
                                ec.Report.Error (1935, loc, "An implementation of `{0}' query expression pattern could not be found. " +
                                        "Are you missing `System.Linq' using directive or `System.Core.dll' assembly reference?",
@@ -93,27 +94,27 @@ namespace Mono.CSharp.Linq
                                return rmg;
                        }
 
-                       public bool AmbiguousCall (ResolveContext ec, MethodBase ambiguous)
+                       public bool AmbiguousCall (ResolveContext ec, MethodSpec ambiguous)
                        {
-                               ec.Report.SymbolRelatedToPreviousError ((MethodInfo) mg);
+                               ec.Report.SymbolRelatedToPreviousError (mg.BestCandidate);
                                ec.Report.SymbolRelatedToPreviousError (ambiguous);
                                ec.Report.Error (1940, loc, "Ambiguous implementation of the query pattern `{0}' for source type `{1}'",
                                        mg.Name, mg.InstanceExpression.GetSignatureForError ());
                                return true;
                        }
 
-                       public bool NoExactMatch (ResolveContext ec, MethodBase method)
+                       public bool NoExactMatch (ResolveContext ec, MethodSpec method)
                        {
-                               AParametersCollection pd = TypeManager.GetParameterData (method);
-                               Type source_type = pd.ExtensionMethodType;
+                               var pd = method.Parameters;
+                               TypeSpec source_type = pd.ExtensionMethodType;
                                if (source_type != null) {
                                        Argument a = arguments [0];
 
                                        if (TypeManager.IsGenericType (source_type) && TypeManager.ContainsGenericParameters (source_type)) {
-                                               TypeInferenceContext tic = new TypeInferenceContext (TypeManager.GetTypeArguments (source_type));
+                                               TypeInferenceContext tic = new TypeInferenceContext (source_type.TypeArguments);
                                                tic.OutputTypeInference (ec, a.Expr, source_type);
                                                if (tic.FixAllTypes (ec)) {
-                                                       source_type = TypeManager.DropGenericTypeArguments (source_type).MakeGenericType (tic.InferredTypeArguments);
+                                                       source_type = source_type.GetDefinition ().MakeGenericType (tic.InferredTypeArguments);
                                                }
                                        }
 
@@ -124,7 +125,7 @@ namespace Mono.CSharp.Linq
                                        }
                                }
 
-                               if (!TypeManager.IsGenericMethod (method))
+                               if (!method.IsGeneric)
                                        return false;
 
                                if (mg.Name == "SelectMany") {
@@ -165,9 +166,9 @@ namespace Mono.CSharp.Linq
                                t.next = (AQueryClause) next.Clone (clonectx);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       return expr.DoResolve (ec);
+                       return expr.Resolve (ec);
                }
 
                public virtual Expression BuildQueryClause (ResolveContext ec, Expression lSide)
@@ -233,7 +234,7 @@ namespace Mono.CSharp.Linq
        {
                sealed class RangeAnonymousTypeParameter : AnonymousTypeParameter
                {
-                       public RangeAnonymousTypeParameter (Expression initializer, LocatedToken parameter)
+                       public RangeAnonymousTypeParameter (Expression initializer, SimpleMemberName parameter)
                                : base (initializer, parameter.Value, parameter.Location)
                        {
                        }
@@ -250,12 +251,12 @@ namespace Mono.CSharp.Linq
                {
                }
 
-               protected static Expression CreateRangeVariableType (ToplevelBlock block, IMemberContext context, LocatedToken name, Expression init)
+               protected static Expression CreateRangeVariableType (ToplevelBlock block, TypeContainer container, SimpleMemberName name, Expression init)
                {
-                       ArrayList args = new ArrayList (2);
+                       var args = new List<AnonymousTypeParameter> (2);
                        args.Add (new AnonymousTypeParameter (block.Parameters [0]));
                        args.Add (new RangeAnonymousTypeParameter (init, name));
-                       return new NewAnonymousType (args, context.CurrentTypeDefinition, name.Location);
+                       return new NewAnonymousType (args, container, name.Location);
                }
        }
 
@@ -268,10 +269,21 @@ namespace Mono.CSharp.Linq
 
                public override Expression BuildQueryClause (ResolveContext ec, Expression lSide)
                {
+                       expr = expr.Resolve (ec);
+                       if (expr == null)
+                               return null;
+
+                       if (expr.Type == InternalType.Dynamic || expr.Type == TypeManager.void_type) {
+                               ec.Report.Error (1979, expr.Location,
+                                       "Query expression with a source or join sequence of type `{0}' is not allowed",
+                                       TypeManager.CSharpName (expr.Type));
+                               return null;
+                       }
+
                        return next.BuildQueryClause (ec, expr);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        Expression e = BuildQueryClause (ec, null);
                        return e.Resolve (ec);
@@ -354,10 +366,10 @@ namespace Mono.CSharp.Linq
 
        class Join : ARangeVariableQueryClause
        {
-               readonly LocatedToken lt;
+               readonly SimpleMemberName lt;
                ToplevelBlock inner_selector, outer_selector;
 
-               public Join (ToplevelBlock block, LocatedToken lt, Expression inner, ToplevelBlock outerSelector, ToplevelBlock innerSelector, Location loc)
+               public Join (ToplevelBlock block, SimpleMemberName lt, Expression inner, ToplevelBlock outerSelector, ToplevelBlock innerSelector, Location loc)
                        : base (block, inner)
                {
                        this.lt = lt;
@@ -380,7 +392,7 @@ namespace Mono.CSharp.Linq
                        args.Add (new Argument (lambda));
 
                        Expression result_selector_expr;
-                       LocatedToken into_variable = GetIntoVariable ();
+                       SimpleMemberName into_variable = GetIntoVariable ();
                        //
                        // When select follows use is as result selector
                        //
@@ -388,7 +400,7 @@ namespace Mono.CSharp.Linq
                                result_selector_expr = next.Expr;
                                next = next.next;
                        } else {
-                               result_selector_expr = CreateRangeVariableType (block, ec.MemberContext, into_variable,
+                               result_selector_expr = CreateRangeVariableType (block, ec.MemberContext.CurrentMemberDefinition.Parent, into_variable,
                                        new SimpleName (into_variable.Value, into_variable.Location));
                        }
 
@@ -399,7 +411,7 @@ namespace Mono.CSharp.Linq
                        args.Add (new Argument (result_selector));
                }
 
-               protected virtual LocatedToken GetIntoVariable ()
+               protected virtual SimpleMemberName GetIntoVariable ()
                {
                        return lt;
                }
@@ -419,16 +431,16 @@ namespace Mono.CSharp.Linq
 
        class GroupJoin : Join
        {
-               readonly LocatedToken into;
+               readonly SimpleMemberName into;
 
-               public GroupJoin (ToplevelBlock block, LocatedToken lt, Expression inner,
-                       ToplevelBlock outerSelector, ToplevelBlock innerSelector, LocatedToken into, Location loc)
+               public GroupJoin (ToplevelBlock block, SimpleMemberName lt, Expression inner,
+                       ToplevelBlock outerSelector, ToplevelBlock innerSelector, SimpleMemberName into, Location loc)
                        : base (block, lt, inner, outerSelector, innerSelector, loc)
                {
                        this.into = into;
                }
 
-               protected override LocatedToken GetIntoVariable ()
+               protected override SimpleMemberName GetIntoVariable ()
                {
                        return into;
                }
@@ -440,7 +452,7 @@ namespace Mono.CSharp.Linq
 
        class Let : ARangeVariableQueryClause
        {
-               public Let (ToplevelBlock block, TypeContainer container, LocatedToken identifier, Expression expr)
+               public Let (ToplevelBlock block, TypeContainer container, SimpleMemberName identifier, Expression expr)
                        : base (block, CreateRangeVariableType (block, container, identifier, expr))
                {
                }
@@ -478,9 +490,9 @@ namespace Mono.CSharp.Linq
 
        class SelectMany : ARangeVariableQueryClause
        {
-               LocatedToken lt;
+               SimpleMemberName lt;
 
-               public SelectMany (ToplevelBlock block, LocatedToken lt, Expression expr)
+               public SelectMany (ToplevelBlock block, SimpleMemberName lt, Expression expr)
                        : base (block, expr)
                {
                        this.lt = lt;
@@ -498,7 +510,7 @@ namespace Mono.CSharp.Linq
                                result_selector_expr = next.Expr;
                                next = next.next;
                        } else {
-                               result_selector_expr = CreateRangeVariableType (block, ec.MemberContext, lt, new SimpleName (lt.Value, lt.Location));
+                               result_selector_expr = CreateRangeVariableType (block, ec.MemberContext.CurrentMemberDefinition.Parent, lt, new SimpleName (lt.Value, lt.Location));
                        }
 
                        LambdaExpression result_selector = new LambdaExpression (lt.Location);
@@ -590,14 +602,14 @@ namespace Mono.CSharp.Linq
                        public readonly ParametersCompiled Parent;
                        public readonly string Identifier;
 
-                       public TransparentParameter (ParametersCompiled parent, LocatedToken identifier)
+                       public TransparentParameter (ParametersCompiled parent, SimpleMemberName identifier)
                                : base (ParameterNamePrefix + Counter++, identifier.Location)
                        {
                                Parent = parent;
                                Identifier = identifier.Value;
                        }
 
-                       public static void Reset ()
+                       public new static void Reset ()
                        {
                                Counter = 0;
                        }
@@ -611,15 +623,15 @@ namespace Mono.CSharp.Linq
                        }
                }
 
-               public QueryBlock (CompilerContext ctx, Block parent, LocatedToken lt, Location start)
-                       : base (ctx, parent, new ParametersCompiled (new ImplicitQueryParameter (lt.Value, lt.Location)), start)
+               public QueryBlock (CompilerContext ctx, Block parent, SimpleMemberName lt, Location start)
+                       : base (ctx, parent, new ParametersCompiled (ctx, new ImplicitQueryParameter (lt.Value, lt.Location)), start)
                {
                        if (parent != null)
                                base.CheckParentConflictName (parent.Toplevel, lt.Value, lt.Location);
                }
 
-               public QueryBlock (CompilerContext ctx, Block parent, ParametersCompiled parameters, LocatedToken lt, Location start)
-                       : base (ctx, parent, new ParametersCompiled (parameters [0].Clone (), new ImplicitQueryParameter (lt.Value, lt.Location)), start)
+               public QueryBlock (CompilerContext ctx, Block parent, ParametersCompiled parameters, SimpleMemberName lt, Location start)
+                       : base (ctx, parent, new ParametersCompiled (ctx, parameters [0].Clone (), new ImplicitQueryParameter (lt.Value, lt.Location)), start)
                {
                }
 
@@ -628,11 +640,11 @@ namespace Mono.CSharp.Linq
                {
                }
 
-               public void AddTransparentParameter (LocatedToken name)
+               public void AddTransparentParameter (CompilerContext ctx, SimpleMemberName name)
                {
                        base.CheckParentConflictName (this, name.Value, name.Location);
 
-                       parameters = new ParametersCompiled (new TransparentParameter (parameters, name));
+                       parameters = new ParametersCompiled (ctx, new TransparentParameter (parameters, name));
                }
 
                protected override bool CheckParentConflictName (ToplevelBlock block, string name, Location l)