Author: marek
Date: 2007-08-31 12:23:32 -0400 (Fri, 31 Aug 2007)
New Revision: 85140

Modified:
   trunk/mcs/gmcs/ChangeLog
   trunk/mcs/gmcs/cs-parser.jay
   trunk/mcs/gmcs/generic.cs
   trunk/mcs/gmcs/linq.cs
Log:
2007-08-31  Marek Safar  <[EMAIL PROTECTED]>

        * cs-parser.jay: Terminates query block correctly. Fixed orderby 
        chaining.
        
        * generic.cs (TypeInferenceContext): Better error resistance.
        
        * linq.cs: Implemented range variables, transparent identifiers and
        SelectMany clause.
        


Modified: trunk/mcs/gmcs/ChangeLog
===================================================================
--- trunk/mcs/gmcs/ChangeLog    2007-08-31 16:20:08 UTC (rev 85139)
+++ trunk/mcs/gmcs/ChangeLog    2007-08-31 16:23:32 UTC (rev 85140)
@@ -1,3 +1,13 @@
+2007-08-31  Marek Safar  <[EMAIL PROTECTED]>
+
+       * cs-parser.jay: Terminates query block correctly. Fixed orderby 
+       chaining.
+       
+       * generic.cs (TypeInferenceContext): Better error resistance.
+       
+       * linq.cs: Implemented range variables, transparent identifiers and
+       SelectMany clause.
+       
 2007-08-28  Marek Safar  <[EMAIL PROTECTED]>
 
        * cs-parser.jay: Anonymous type arguments can be simple name, syntax 
error

Modified: trunk/mcs/gmcs/cs-parser.jay
===================================================================
--- trunk/mcs/gmcs/cs-parser.jay        2007-08-31 16:20:08 UTC (rev 85139)
+++ trunk/mcs/gmcs/cs-parser.jay        2007-08-31 16:23:32 UTC (rev 85140)
@@ -5379,7 +5379,7 @@
        : from_clause query_body
          {
                lexer.QueryParsing = false;
-               $$ = new Linq.QueryExpression (current_container, 
current_block, (Expression)$1, (Linq.AQueryClause)$2); 
+               $$ = new Linq.QueryExpression (current_block, (Expression)$1, 
(Linq.AQueryClause)$2); 
                end_block (lexer.Location);
          }
        ;
@@ -5390,12 +5390,12 @@
                start_block (lexer.Location);
                LocatedToken lt = (LocatedToken) $2;
                
-               current_block.AddVariable (Linq.ImplicitArgument.Instance, 
lt.Value, lt.Location);
+               current_block.AddVariable 
(Linq.ImplicitQueryParameter.ImplicitType.Instance, lt.Value, lt.Location);
                $$ = $4;
          }
        | FROM type IDENTIFIER IN expression opt_join_clauses
          {
-               start_block (lexer.Location);
+               start_block (GetLocation ($1));
                LocatedToken lt = (LocatedToken) $3;
 
                Expression type = (Expression)$2;
@@ -5472,10 +5472,18 @@
 from_let_where_clauses
        : from_let_where_clause
        | from_let_where_clauses from_let_where_clause
+         {
+               ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$2;
+               $$ = $1;
+         }
        ;
        
 from_let_where_clause
        : from_clause
+         {
+               $$ = new Linq.SelectMany (current_block, (Expression)$1);
+               end_block (lexer.Location);             
+         }
        | let_clause
        | where_clause
        ;
@@ -5501,15 +5509,24 @@
        
 orderings
        : order_by
+       | order_by COMMA orderings_then_by
          {
+               ((Linq.AQueryClause)$1).Next = (Linq.AQueryClause)$3;
                $$ = $1;
          }
-       | order_by COMMA then_by
+       ;
+       
+orderings_then_by
+       : then_by
          {
-               ((Linq.AQueryClause)$1).Next = (Linq.AQueryClause)$3;
                $$ = $1;
          }
-       ;
+       | orderings_then_by COMMA then_by
+         {
+               ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$3;
+               $$ = $1;
+         }
+       ;       
        
 order_by
        : expression

Modified: trunk/mcs/gmcs/generic.cs
===================================================================
--- trunk/mcs/gmcs/generic.cs   2007-08-31 16:20:08 UTC (rev 85139)
+++ trunk/mcs/gmcs/generic.cs   2007-08-31 16:23:32 UTC (rev 85140)
@@ -2351,6 +2351,9 @@
                        if (i_args == null)
                                return false;
 
+                       if (i_args.Length == 0)
+                               return true;
+
                        method = ((MethodInfo) method).MakeGenericMethod 
(i_args);
                        return true;
                }
@@ -2597,6 +2600,9 @@
 
                        Type[] method_generic_args = method.GetGenericArguments 
();
                        TypeInferenceContext context = new TypeInferenceContext 
(method_generic_args);
+                       if (!context.UnfixedVariableExists)
+                               return Type.EmptyTypes;
+
                        if (!InferInPhases (ec, context, pd))
                                return null;
 
@@ -2708,10 +2714,18 @@
                        if (typeArguments.Length == 0)
                                throw new ArgumentException ("Empty generic 
arguments");
 
-                       unfixed_types = new Type[typeArguments.Length];
-                       Array.Copy (typeArguments, unfixed_types, 
unfixed_types.Length);
-                       bounds = new ArrayList[typeArguments.Length];
-                       fixed_types = new Type[typeArguments.Length];
+                       fixed_types = new Type [typeArguments.Length];
+                       for (int i = 0; i < typeArguments.Length; ++i) {
+                               if (typeArguments [i].IsGenericParameter) {
+                                       if (bounds == null) {
+                                               bounds = new ArrayList 
[typeArguments.Length];
+                                               unfixed_types = new Type 
[typeArguments.Length];
+                                       }
+                                       unfixed_types [i] = typeArguments [i];
+                               } else {
+                                       fixed_types [i] = typeArguments [i];
+                               }
+                       }
                }
 
                public Type[] InferredTypeArguments {
@@ -2841,7 +2855,8 @@
                                if (t == null)
                                        continue;
 
-                               if (!FixType (IsUnfixed (t))) {
+                               int idx = IsUnfixed (t);
+                               if (idx >= 0 && !FixType (idx)) {
                                        return false;
                                }
                        }
@@ -3054,6 +3069,9 @@
 
                public bool UnfixedVariableExists {
                        get {
+                               if (unfixed_types == null)
+                                       return false;
+
                                foreach (Type ut in unfixed_types)
                                        if (ut != null)
                                                return true;

Modified: trunk/mcs/gmcs/linq.cs
===================================================================
--- trunk/mcs/gmcs/linq.cs      2007-08-31 16:20:08 UTC (rev 85139)
+++ trunk/mcs/gmcs/linq.cs      2007-08-31 16:23:32 UTC (rev 85140)
@@ -20,59 +20,23 @@
        //
        //
 
-       public class QueryExpression : Expression
+       public class QueryExpression : ARangeVariableQueryClause
        {
-               public readonly Block Block;
-               public readonly TypeContainer Host;
-
-               AQueryClause query;
-               Expression from;
-
-               public QueryExpression (TypeContainer host, Block block, 
Expression from, AQueryClause query)
+               public QueryExpression (Block block, Expression from, 
AQueryClause query)
+                       : base (block, from, from.Location)
                {
-                       this.Host = host;
-                       this.Block = block;
-                       this.from = from;
-                       this.query = query;
+                       this.next = query;
                }
 
                public override Expression DoResolve (EmitContext ec)
                {
-                       from = from.DoResolve (ec);
-                       if (from == null)
-                               return null;
-
-                       ICollection values = Block.Variables.Values;
-                       if (values.Count != 1)
-                               throw new NotImplementedException ("Count != 
1");
-
-                       IEnumerator enumerator = values.GetEnumerator ();
-                       enumerator.MoveNext ();
-                       LocalInfo li = (LocalInfo)enumerator.Current;
-
-                       Parameter clause_parameter;
-                       if (li.Type == ImplicitArgument.Instance) {
-                               clause_parameter = new ImplicitLambdaParameter 
(li.Name, li.Location);
-                       } else {
-                               clause_parameter = new Parameter (li.Type, 
li.Name, Parameter.Modifier.NONE, null, li.Location);
-                       }
-
-                       Expression e = query.BuildQueryClause (ec, this, from, 
clause_parameter);
+                       Expression e = BuildQueryClause (ec, expr, null, null);
                        e = e.Resolve (ec);
-
-                       if (e == null)
-                               return null;
-
-                       // TODO: really ?
-                       if (li.VariableType == null)
-                               li.VariableType = ec.ReturnType;
-
                        return e;
                }
 
-               public override void Emit (EmitContext ec)
-               {
-                       throw new NotSupportedException ();
+               protected override string MethodName {
+                       get { throw new NotSupportedException (); }
                }
        }
 
@@ -107,8 +71,8 @@
 
        public abstract class AQueryClause : ALinqExpression
        {
-               public AQueryClause Next;
-               protected Expression expr;
+               public AQueryClause next;
+               /*protected*/ public Expression expr;
 
                protected AQueryClause (Expression expr, Location loc)
                {
@@ -126,41 +90,129 @@
                        throw new NotSupportedException ();
                }
 
-               public Expression BuildQueryClause (EmitContext ec, 
QueryExpression top, Expression from, Parameter parameter)
+               public virtual Expression BuildQueryClause (EmitContext ec, 
Expression from, Parameters parameters, TransparentIdentifiersScope ti)
                {
-                       Parameters parameters = new Parameters (parameter);
-                       LambdaExpression ame = new LambdaExpression (
-                               null, null, top.Host,
-                               parameters,
-                               top.Block, loc);
-                       ame.Block = new ToplevelBlock (parameters, loc);
-                       ame.Block.AddStatement (new Return (expr, loc));
+                       ArrayList args = new ArrayList (2);
+                       args.Add (new Argument (from));
+                       args.Add (new Argument (CreateSelector (ec, expr, 
parameters, ti)));
+                       expr = new Invocation (MethodGroup, args);
+                       if (next != null)
+                               return next.BuildQueryClause (ec, this, 
parameters, ti);
 
-                       expr = new Invocation (MethodGroup, CreateArguments 
(ame, from));
-                       if (Next != null)
-                               return Next.BuildQueryClause (ec, top, this, 
parameter);
-
                        return expr;
                }
-                                              
-               protected virtual ArrayList CreateArguments 
(AnonymousMethodExpression ame, Expression from)
+
+               protected LambdaExpression CreateSelector (EmitContext ec, 
Expression expr, Parameters parameters, TransparentIdentifiersScope ti)
                {
-                       ArrayList args = new ArrayList (2);
-                       args.Add (new Argument (from));
-                       args.Add (new Argument (ame));
-                       return args;
+                       LambdaExpression selector = new LambdaExpression (
+                               null, null, (TypeContainer) ec.DeclContainer, 
parameters, ec.CurrentBlock, loc);
+                       selector.Block = new SelectorBlock (ec.CurrentBlock, 
parameters, ti, loc);
+                       selector.Block.AddStatement (new Return (expr, loc));
+
+                       selector.CreateAnonymousHelpers ();
+                       selector.RootScope.DefineType ();
+
+                       return selector;
                }
 
+               public virtual AQueryClause Next {
+                       set {
+                               next = value;
+                       }
+               }
+
                public AQueryClause Tail {
                        get {
-                               return Next == null ? this : Next.Tail;
+                               return next == null ? this : next.Tail;
                        }
                }
        }
 
+       //
+       // A query clause with identifier (range variable)
+       //
+       public abstract class ARangeVariableQueryClause : AQueryClause
+       {
+               public readonly Block block;
+               Expression element_selector;
+
+               protected ARangeVariableQueryClause (Block block, Expression 
expr, Location loc)
+                       : base (expr, loc)
+               {
+                       this.block = block;
+               }
+
+               public override Expression BuildQueryClause (EmitContext ec, 
Expression from, Parameters parameters, TransparentIdentifiersScope ti)
+               {
+                       ICollection values = block.Variables.Values;
+                       if (values.Count != 1)
+                               throw new NotImplementedException ("Count != 
1");
+
+                       IEnumerator enumerator = values.GetEnumerator ();
+                       enumerator.MoveNext ();
+                       LocalInfo li = (LocalInfo) enumerator.Current;
+
+                       Parameters clause_parameter;
+                       if (li.Type == 
ImplicitQueryParameter.ImplicitType.Instance) {
+                               clause_parameter = new Parameters (
+                                       new ImplicitQueryParameter (li));
+                       } else {
+                               clause_parameter = new Parameters (
+                                       new Parameter (li.Type, li.Name, 
Parameter.Modifier.NONE, null, li.Location));
+                       }
+
+                       if (parameters == null)
+                               return next.BuildQueryClause (ec, expr, 
clause_parameter, ti);
+
+                       Parameters transparent_parameters = new Parameters (
+                               new Parameter[] { parameters [0], 
clause_parameter [0] });
+                       if (next != null) {
+                               ArrayList transp_args = new ArrayList (2);
+                               transp_args.Add (new AnonymousTypeParameter 
(transparent_parameters [0]));
+                               transp_args.Add (new AnonymousTypeParameter 
(transparent_parameters [1]));
+                               element_selector = new AnonymousTypeDeclaration 
(transp_args, (TypeContainer) ec.DeclContainer, loc);
+                       }
+
+                       ArrayList args = new ArrayList (3);
+                       args.Add (new Argument (from));
+                       args.Add (new Argument (CreateSelector (ec, expr, 
parameters, null)));
+                       args.Add (new Argument (CreateSelector (ec, 
element_selector, transparent_parameters, ti)));
+                       expr = new Invocation (MethodGroup, args);
+                       if (next != null) {
+                               TransparentParameter tp = new 
TransparentParameter (loc);
+                               parameters = new Parameters (tp);
+
+                               string[] identifiers;
+                               if (ti == null) {
+                                       identifiers = new string [] { 
transparent_parameters [0].Name, transparent_parameters [1].Name };
+                               } else {
+                                       identifiers = new string [] { 
transparent_parameters [1].Name };
+                               }
+
+                               return next.BuildQueryClause (ec, this, 
parameters,
+                                       new TransparentIdentifiersScope (ti, 
tp, identifiers));
+
+                       }
+
+                       return expr;
+               }
+
+               public override AQueryClause Next {
+                       set {
+                               element_selector = value.expr;
+
+                               // Can be optimized as SelectMany element 
selector
+                               if (value is Select)
+                                       return;
+
+                               next = value;
+                       }
+               }
+       }
+
        public class Cast : ALinqExpression
        {
-               readonly Expression expr;
+               Expression expr;
                readonly Expression cast_type;
 
                public Cast (Expression type, Expression expr, Location loc)
@@ -170,6 +222,13 @@
                        this.loc = loc;
                }
 
+               protected override void CloneTo (CloneContext clonectx, 
Expression target)
+               {
+                       // We don't have to clone cast type
+                       Cast t = (Cast) target;
+                       t.expr = expr.Clone (clonectx);
+               }
+
                protected override string MethodName {
                        get { return "Cast"; }
                }
@@ -200,20 +259,21 @@
                        this.element_selector = elementSelector;
                }
 
-               protected override ArrayList CreateArguments 
(AnonymousMethodExpression ame, Expression from)
+               public override Expression BuildQueryClause (EmitContext ec, 
Expression from, Parameters parameters, TransparentIdentifiersScope ti)
                {
-                       ArrayList args = base.CreateArguments (ame, from);
-                       
+                       ArrayList args = new ArrayList (3);
+                       args.Add (new Argument (from));
+                       args.Add (new Argument (CreateSelector (ec, expr, 
parameters, ti)));
+
                        // A query can be optimized when selector is not group 
by specific
-                       if (!element_selector.Equals (from)) {
-                               LambdaExpression am_element = new 
LambdaExpression (
-                                       null, null, ame.Host, ame.Parameters, 
ame.Container, loc);
-                               am_element.Block = new ToplevelBlock 
(ame.Parameters, loc);
-                               am_element.Block.AddStatement (new Return 
(element_selector, loc));
-                               
-                               args.Add (new Argument (am_element));
-                       }
-                       return args;
+                       if (!element_selector.Equals (from))
+                               args.Add (new Argument (CreateSelector (ec, 
element_selector, parameters, ti)));
+
+                       expr = new Invocation (MethodGroup, args);
+                       if (next != null)
+                               return next.BuildQueryClause (ec, this, 
parameters, ti);
+
+                       return expr;
                }
 
                protected override string MethodName {
@@ -233,6 +293,18 @@
                }
        }
 
+       public class SelectMany : ARangeVariableQueryClause
+       {
+               public SelectMany (Block block, Expression expr)
+                       : base (block, expr, expr.Location)
+               {
+               }
+
+               protected override string MethodName {
+                       get { return "SelectMany"; }
+               }
+       }
+
        public class Where : AQueryClause
        {
                public Where (Expression expr, Location loc)
@@ -293,25 +365,139 @@
                }
        }
 
+       class ImplicitQueryParameter : ImplicitLambdaParameter
+       {
+               public sealed class ImplicitType : Expression
+               {
+                       public static ImplicitType Instance = new ImplicitType 
();
+
+                       private ImplicitType ()
+                       {
+                       }
+
+                       protected override void CloneTo (CloneContext clonectx, 
Expression target)
+                       {
+                               // Nothing to clone
+                       }
+
+                       public override Expression DoResolve (EmitContext ec)
+                       {
+                               throw new NotSupportedException ();
+                       }
+
+                       public override void Emit (EmitContext ec)
+                       {
+                               throw new NotSupportedException ();
+                       }
+               }
+
+               readonly LocalInfo variable;
+
+               public ImplicitQueryParameter (LocalInfo variable)
+                       : base (variable.Name, variable.Location)
+               {
+                       this.variable = variable;
+               }
+
+               public override bool Resolve (IResolveContext ec)
+               {
+                       if (!base.Resolve (ec))
+                               return false;
+
+                       variable.VariableType = parameter_type;
+                       return true;
+               }
+       }
+
        //
-       // Only helper for implicit query type expression
+       // Transparent parameters are used to package up the intermediate 
results
+       // and pass them onto next clause
        //
-       public class ImplicitArgument : Expression
+       public class TransparentParameter : ImplicitLambdaParameter
        {
-               public static ImplicitArgument Instance = new ImplicitArgument 
();
+               static int counter;
+               const string ParameterNamePrefix = "<>__TranspIdent";
 
-               private ImplicitArgument ()
+               public TransparentParameter (Location loc)
+                       : base (ParameterNamePrefix + counter++, loc)
                {
                }
+       }
 
-               public override Expression DoResolve (EmitContext ec)
+       //
+       // Transparent identifiers are stored in nested anonymous types, each 
type can contain
+       // up to 2 identifiers or 1 identifier and parent type.
+       //
+       public class TransparentIdentifiersScope
+       {
+               readonly string [] identifiers;
+               readonly TransparentIdentifiersScope parent;
+               readonly TransparentParameter parameter;
+
+               public TransparentIdentifiersScope (TransparentIdentifiersScope 
parent,
+                       TransparentParameter parameter, string [] identifiers)
                {
-                       throw new NotImplementedException ();
+                       this.parent = parent;
+                       this.parameter = parameter;
+                       this.identifiers = identifiers;
                }
 
-               public override void Emit (EmitContext ec)
+               public MemberAccess GetIdentifier (string name)
                {
-                       throw new NotImplementedException ();
+                       TransparentIdentifiersScope ident = FindIdentifier 
(name);
+                       if (ident == null)
+                               return null;
+
+                       return new MemberAccess 
(CreateIndentifierNestingExpression (ident), name);
                }
+
+               TransparentIdentifiersScope FindIdentifier (string name)
+               {
+                       foreach (string s in identifiers) {
+                               if (s == name)
+                                       return this;
+                       }
+
+                       if (parent == null)
+                               return null;
+
+                       return parent.FindIdentifier (name);
+               }
+
+               Expression CreateIndentifierNestingExpression 
(TransparentIdentifiersScope end)
+               {
+                       Expression expr = new SimpleName (parameter.Name, 
parameter.Location);
+                       TransparentIdentifiersScope current = this;
+                       while (current != end)
+                       {
+                               current = current.parent;
+                               expr = new MemberAccess (expr, 
current.parameter.Name);
+                       }
+
+                       return expr;
+               }
        }
+
+       //
+       // Lambda expression block which contains transparent identifiers
+       //
+       class SelectorBlock : ToplevelBlock
+       {
+               readonly TransparentIdentifiersScope transparent_identifiers;
+
+               public SelectorBlock (Block block, Parameters parameters, 
+                       TransparentIdentifiersScope transparentIdentifiers, 
Location loc)
+                       : base (block, parameters, loc)
+               {
+                       this.transparent_identifiers = transparentIdentifiers;
+               }
+
+               public override Expression GetTransparentIdentifier (string 
name)
+               {
+                       if (transparent_identifiers == null)
+                               return null;
+
+                       return transparent_identifiers.GetIdentifier (name);
+               }
+       }
 }

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to