2010-06-02 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / cs-parser.jay
index fd493a2e4ed255968e632716e68b0933a1db94f3..adccfdf4da46cdb75f6113233ba148aefd1cf8d9 100644 (file)
@@ -54,7 +54,6 @@ namespace Mono.CSharp
 
                Delegate   current_delegate;
                
-               GenericMethod current_generic_method;
                AnonymousMethodExpression current_anonymous_method;
 
                /// <summary>
@@ -135,7 +134,7 @@ namespace Mono.CSharp
                //
                public Undo undo;
                
-               Stack<Block> linq_clause_blocks;
+               Stack<Linq.QueryBlock> linq_clause_blocks;
 
                // A counter to create new class names in interactive mode
                static int class_count;
@@ -1273,7 +1272,6 @@ method_declaration
                        Report.Error (531, method.Location, "`{0}': interface members cannot have a definition", method.GetSignatureForError ());
                }
 
-               current_generic_method = null;
                current_local_parameters = null;
 
                if (RootContext.Documentation != null)
@@ -1300,24 +1298,25 @@ method_header
                MemberName name = (MemberName) $4;
                current_local_parameters = (ParametersCompiled) $7;
 
-               if ($10 != null && name.TypeArguments == null)
-                       Report.Error (80, lexer.Location,
-                                     "Constraints are not allowed on non-generic declarations");
-
-               Method method;
-
                GenericMethod generic = null;
                if (name.TypeArguments != null) {
                        generic = new GenericMethod (current_namespace, current_class, name,
                                                     (FullNamedExpression) $3, current_local_parameters);
 
                        generic.SetParameterInfo ((List<Constraints>) $10);
+               } else if ($10 != null) {
+                       Report.Error (80, GetLocation ($10),
+                               "Constraints are not allowed on non-generic declarations");
                }
 
-               method = new Method (current_class, generic, (FullNamedExpression) $3, (Modifiers) $2,
+               Method method = new Method (current_class, generic, (FullNamedExpression) $3, (Modifiers) $2,
                                     name, current_local_parameters, (Attributes) $1);
-
-               current_generic_method = generic;
+                                    
+               if ($10 != null && ((method.ModFlags & Modifiers.OVERRIDE) != 0 || method.IsExplicitImpl)) {
+                       Report.Error (460, method.Location,
+                               "`{0}': Cannot specify constraints for overrides and explicit interface implementation methods",
+                               method.GetSignatureForError ());
+               }
 
                if (RootContext.Documentation != null)
                        method.DocComment = Lexer.consume_doc_comment ();
@@ -1379,8 +1378,6 @@ method_header
                method = new Method (current_class, generic, TypeManager.system_void_expr,
                                     modifiers, name, current_local_parameters, (Attributes) $1);
 
-               current_generic_method = generic;
-
                if (RootContext.Documentation != null)
                        method.DocComment = Lexer.consume_doc_comment ();
 
@@ -2523,7 +2520,7 @@ opt_enum_base
         }
        | COLON error
         {
-               Error_TypeExpected (lexer.Location);
+               Error_TypeExpected (GetLocation ($1));
                $$ = TypeManager.system_int32_expr;
         }
        ;
@@ -2944,8 +2941,9 @@ type_list
 base_type_name
        : type
          {
-               if ($1 is ComposedCast)
+               if ($1 is ComposedCast) {
                        Report.Error (1521, GetLocation ($1), "Invalid base type `{0}'", ((ComposedCast)$1).GetSignatureForError ());
+               }
                $$ = $1;
          }
        | error
@@ -2981,14 +2979,6 @@ integral_type
        | CHAR          { $$ = TypeManager.system_char_expr; }
        ;
 
-predefined_type
-       : builtin_types
-       | VOID
-         {
-               $$ = TypeManager.system_void_expr;      
-         }
-       ;
-
 //
 // Expressions, section 7.5
 //
@@ -3004,7 +2994,7 @@ primary_expression_no_array_creation
        | IDENTIFIER opt_type_argument_list
          {
                var lt = (Tokenizer.LocatedToken) $1;
-               $$ = new SimpleName (MemberName.MakeName (lt.Value, (TypeArguments)$2), (TypeArguments)$2, lt.Location);          
+               $$ = new SimpleName (lt.Value, (TypeArguments)$2, lt.Location);   
          }
        | IDENTIFIER GENERATE_COMPLETION {
                var lt = (Tokenizer.LocatedToken) $1;
@@ -3049,7 +3039,6 @@ boolean_literal
 open_parens_any
        : OPEN_PARENS
        | OPEN_PARENS_CAST
-       | OPEN_PARENS_LAMBDA
        ;
 
 // 
@@ -3079,7 +3068,7 @@ member_access
                var lt = (Tokenizer.LocatedToken) $3;
                $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
          }
-       | predefined_type DOT IDENTIFIER opt_type_argument_list
+       | builtin_types DOT IDENTIFIER opt_type_argument_list
          {
                var lt = (Tokenizer.LocatedToken) $3;
                // TODO: Location is wrong as some predefined types doesn't hold a location
@@ -3099,12 +3088,12 @@ member_access
                var lt = (Tokenizer.LocatedToken) $3;
                $$ = new CompletionMemberAccess ((Expression) $1, lt.Value, lt.Location);
          }
-       | predefined_type DOT GENERATE_COMPLETION
+       | builtin_types DOT GENERATE_COMPLETION
          {
                // TODO: Location is wrong as some predefined types doesn't hold a location
                $$ = new CompletionMemberAccess ((Expression) $1, null, lexer.Location);
          }
-       | predefined_type DOT IDENTIFIER GENERATE_COMPLETION {
+       | builtin_types DOT IDENTIFIER GENERATE_COMPLETION {
                var lt = (Tokenizer.LocatedToken) $3;
                $$ = new CompletionMemberAccess ((Expression) $1, lt.Value, lt.Location);
          }
@@ -3157,6 +3146,10 @@ member_initializer_list
                a.Add ((Expression) $3);
                $$ = a;
          }
+       | member_initializer_list error {
+               Error_SyntaxError (yyToken);
+               $$ = $1;
+         }
        ;
 
 member_initializer
@@ -3178,7 +3171,10 @@ member_initializer
          }
        | OPEN_BRACE expression_list CLOSE_BRACE
          {
-               $$ = new CollectionElementInitializer ((List<Expression>)$2, GetLocation ($1));
+               if ($2 == null)
+                       $$ = null;
+               else
+                       $$ = new CollectionElementInitializer ((List<Expression>)$2, GetLocation ($1));
          }
        | OPEN_BRACE CLOSE_BRACE
          {
@@ -3230,12 +3226,12 @@ argument_list
        | argument_list COMMA
          {
                Report.Error (839, GetLocation ($2), "An argument is missing");
-               $$ = null;
+               $$ = $1;
          }
        | COMMA argument_or_named_argument
          {
                Report.Error (839, GetLocation ($1), "An argument is missing");
-               $$ = null;
+               $$ = $1;
          }
        ;
 
@@ -3261,11 +3257,11 @@ non_simple_argument
          { 
                $$ = new Argument ((Expression) $2, Argument.AType.Out);
          }
-       | ARGLIST open_parens_any argument_list CLOSE_PARENS
+       | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
          {
                $$ = new Argument (new Arglist ((Arguments) $3, GetLocation ($1)));
          }
-       | ARGLIST open_parens_any CLOSE_PARENS
+       | ARGLIST OPEN_PARENS CLOSE_PARENS
          {
                $$ = new Argument (new Arglist (GetLocation ($1)));
          }       
@@ -3327,6 +3323,10 @@ expression_list
                list.Add ((Expression) $3);
                $$ = list;
          }
+       | expression_list error {
+               Error_SyntaxError (yyToken);
+               $$ = $1;
+         }
        ;
        
 expression_list_arguments
@@ -3355,7 +3355,7 @@ expression_list_argument
 this_access
        : THIS
          {
-               $$ = new This (current_block, GetLocation ($1));
+               $$ = new This (GetLocation ($1));
          }
        ;
 
@@ -3379,14 +3379,14 @@ base_access
 post_increment_expression
        : primary_expression OP_INC
          {
-               $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement, (Expression) $1);
+               $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement, (Expression) $1, GetLocation ($2));
          }
        ;
 
 post_decrement_expression
        : primary_expression OP_DEC
          {
-               $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement, (Expression) $1);
+               $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement, (Expression) $1, GetLocation ($2));
          }
        ;
 
@@ -3551,7 +3551,7 @@ rank_specifiers
        : rank_specifier
        | rank_specifier rank_specifiers
          {
-               $$ = (string) $2 + (string) $1;
+               $$ = (string) $1 + (string) $2;
          }
        ;
 
@@ -3655,14 +3655,14 @@ unbound_type_name
          {  
                var lt = (Tokenizer.LocatedToken) $1;
 
-               $$ = new SimpleName (MemberName.MakeName (lt.Value, (int)$2), lt.Location);
+               $$ = new SimpleName (lt.Value, (int) $2, lt.Location);
          }
        | qualified_alias_member IDENTIFIER generic_dimension
          {
                var lt1 = (Tokenizer.LocatedToken) $1;
                var lt2 = (Tokenizer.LocatedToken) $2;
 
-               $$ = new QualifiedAliasMember (lt1.Value, MemberName.MakeName (lt2.Value, (int) $3), lt1.Location);
+               $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, (int) $3, lt1.Location);
          }
        | unbound_type_name DOT IDENTIFIER
          {
@@ -3674,14 +3674,14 @@ unbound_type_name
          {
                var lt = (Tokenizer.LocatedToken) $3;
                
-               $$ = new MemberAccess ((Expression) $1, MemberName.MakeName (lt.Value, (int) $4), lt.Location);         
+               $$ = new MemberAccess ((Expression) $1, lt.Value, (int) $4, lt.Location);               
          }
        | namespace_or_type_name DOT IDENTIFIER generic_dimension
          {
                var lt = (Tokenizer.LocatedToken) $3;
                MemberName name = (MemberName) $1;
 
-               $$ = new MemberAccess (name.GetTypeExpression (), MemberName.MakeName (lt.Value, (int) $4), lt.Location);               
+               $$ = new MemberAccess (name.GetTypeExpression (), lt.Value, (int) $4, lt.Location);             
          }
        ;
 
@@ -3784,11 +3784,11 @@ unary_expression
        : primary_expression
        | BANG prefixed_unary_expression
          {
-               $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2);
+               $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, GetLocation ($1));
          }
        | TILDE prefixed_unary_expression
          {
-               $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2);
+               $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, GetLocation ($1));
          }
        | cast_expression
        ;
@@ -3798,7 +3798,7 @@ cast_expression
          {
                $$ = new Cast ((FullNamedExpression) $2, (Expression) $4, GetLocation ($1));
          }
-       | OPEN_PARENS predefined_type CLOSE_PARENS prefixed_unary_expression
+       | OPEN_PARENS builtin_types CLOSE_PARENS prefixed_unary_expression
          {
                $$ = new Cast ((FullNamedExpression) $2, (Expression) $4, GetLocation ($1));
          }
@@ -3812,19 +3812,19 @@ prefixed_unary_expression
        : unary_expression
        | PLUS prefixed_unary_expression
          { 
-               $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2);
+               $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, GetLocation ($1));
          } 
        | MINUS prefixed_unary_expression 
          { 
-               $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2);
+               $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, GetLocation ($1));
          }
        | OP_INC prefixed_unary_expression 
          {
-               $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement, (Expression) $2);
+               $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement, (Expression) $2, GetLocation ($1));
          }
        | OP_DEC prefixed_unary_expression 
          {
-               $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement, (Expression) $2);
+               $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement, (Expression) $2, GetLocation ($1));
          }
        | STAR prefixed_unary_expression
          {
@@ -3832,7 +3832,7 @@ prefixed_unary_expression
          }
        | BITWISE_AND prefixed_unary_expression
          {
-               $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2);
+               $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, GetLocation ($1));
          }
        ;
 
@@ -3841,17 +3841,17 @@ multiplicative_expression
        | multiplicative_expression STAR prefixed_unary_expression
          {
                $$ = new Binary (Binary.Operator.Multiply, 
-                                (Expression) $1, (Expression) $3);
+                                (Expression) $1, (Expression) $3, GetLocation ($2));
          }
        | multiplicative_expression DIV prefixed_unary_expression
          {
                $$ = new Binary (Binary.Operator.Division, 
-                                (Expression) $1, (Expression) $3);
+                                (Expression) $1, (Expression) $3, GetLocation ($2));
          }
        | multiplicative_expression PERCENT prefixed_unary_expression 
          {
                $$ = new Binary (Binary.Operator.Modulus, 
-                                (Expression) $1, (Expression) $3);
+                                (Expression) $1, (Expression) $3, GetLocation ($2));
          }
        ;
 
@@ -3860,16 +3860,16 @@ additive_expression
        | additive_expression PLUS multiplicative_expression 
          {
                $$ = new Binary (Binary.Operator.Addition, 
-                                (Expression) $1, (Expression) $3);
+                                (Expression) $1, (Expression) $3, GetLocation ($2));
          }
        | additive_expression MINUS multiplicative_expression
          {
-               $$ = new Binary (Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
+               $$ = new Binary (Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, GetLocation ($2));
          }
        | parenthesized_expression MINUS multiplicative_expression
          {
                // Shift/Reduce conflict
-               $$ = new Binary (Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
+               $$ = new Binary (Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, GetLocation ($2));
          }
        | additive_expression AS type
          {
@@ -3886,12 +3886,12 @@ shift_expression
        | shift_expression OP_SHIFT_LEFT additive_expression
          {
                $$ = new Binary (Binary.Operator.LeftShift, 
-                                (Expression) $1, (Expression) $3);
+                                (Expression) $1, (Expression) $3, GetLocation ($2));
          }
        | shift_expression OP_SHIFT_RIGHT additive_expression
          {
                $$ = new Binary (Binary.Operator.RightShift, 
-                                (Expression) $1, (Expression) $3);
+                                (Expression) $1, (Expression) $3, GetLocation ($2));
          }
        ; 
 
@@ -3900,22 +3900,22 @@ relational_expression
        | relational_expression OP_LT shift_expression
          {
                $$ = new Binary (Binary.Operator.LessThan, 
-                                (Expression) $1, (Expression) $3);
+                                (Expression) $1, (Expression) $3, GetLocation ($2));
          }
        | relational_expression OP_GT shift_expression
          {
                $$ = new Binary (Binary.Operator.GreaterThan, 
-                                (Expression) $1, (Expression) $3);
+                                (Expression) $1, (Expression) $3, GetLocation ($2));
          }
        | relational_expression OP_LE shift_expression
          {
                $$ = new Binary (Binary.Operator.LessThanOrEqual, 
-                                (Expression) $1, (Expression) $3);
+                                (Expression) $1, (Expression) $3, GetLocation ($2));
          }
        | relational_expression OP_GE shift_expression
          {
                $$ = new Binary (Binary.Operator.GreaterThanOrEqual, 
-                                (Expression) $1, (Expression) $3);
+                                (Expression) $1, (Expression) $3, GetLocation ($2));
          }
        ;
 
@@ -3924,12 +3924,12 @@ equality_expression
        | equality_expression OP_EQ relational_expression
          {
                $$ = new Binary (Binary.Operator.Equality, 
-                                (Expression) $1, (Expression) $3);
+                                (Expression) $1, (Expression) $3, GetLocation ($2));
          }
        | equality_expression OP_NE relational_expression
          {
                $$ = new Binary (Binary.Operator.Inequality, 
-                                (Expression) $1, (Expression) $3);
+                                (Expression) $1, (Expression) $3, GetLocation ($2));
          }
        ; 
 
@@ -3938,7 +3938,7 @@ and_expression
        | and_expression BITWISE_AND equality_expression
          {
                $$ = new Binary (Binary.Operator.BitwiseAnd, 
-                                (Expression) $1, (Expression) $3);
+                                (Expression) $1, (Expression) $3, GetLocation ($2));
          }
        ;
 
@@ -3947,7 +3947,7 @@ exclusive_or_expression
        | exclusive_or_expression CARRET and_expression
          {
                $$ = new Binary (Binary.Operator.ExclusiveOr, 
-                                (Expression) $1, (Expression) $3);
+                                (Expression) $1, (Expression) $3, GetLocation ($2));
          }
        ;
 
@@ -3956,7 +3956,7 @@ inclusive_or_expression
        | inclusive_or_expression BITWISE_OR exclusive_or_expression
          {
                $$ = new Binary (Binary.Operator.BitwiseOr, 
-                                (Expression) $1, (Expression) $3);
+                                (Expression) $1, (Expression) $3, GetLocation ($2));
          }
        ;
 
@@ -3965,7 +3965,7 @@ conditional_and_expression
        | conditional_and_expression OP_AND inclusive_or_expression
          {
                $$ = new Binary (Binary.Operator.LogicalAnd, 
-                                (Expression) $1, (Expression) $3);
+                                (Expression) $1, (Expression) $3, GetLocation ($2));
          }
        ;
 
@@ -3974,7 +3974,7 @@ conditional_or_expression
        | conditional_or_expression OP_OR conditional_and_expression
          {
                $$ = new Binary (Binary.Operator.LogicalOr, 
-                                (Expression) $1, (Expression) $3);
+                                (Expression) $1, (Expression) $3, GetLocation ($2));
          }
        ;
        
@@ -4131,6 +4131,9 @@ lambda_expression
          }
        | OPEN_PARENS_LAMBDA
          {
+               if (RootContext.Version <= LanguageVersion.ISO_2)
+                       Report.FeatureIsNotAvailable (GetLocation ($1), "lambda expressions");
+         
                valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out;
          }
          opt_lambda_parameter_list CLOSE_PARENS ARROW 
@@ -4343,6 +4346,9 @@ type_parameter_constraints
 type_parameter_constraint
        : type
          {
+               if ($1 is ComposedCast)
+                       Report.Error (706, GetLocation ($1), "Invalid constraint type `{0}'", ((ComposedCast)$1).GetSignatureForError ());
+         
                $$ = $1;
          }
        | NEW OPEN_PARENS CLOSE_PARENS
@@ -4538,7 +4544,7 @@ embedded_statement
 empty_statement
        : SEMICOLON
          {
-                 $$ = EmptyStatement.Value;
+               $$ = new EmptyStatement (GetLocation ($1));
          }
        ;
 
@@ -4751,7 +4757,7 @@ interactive_statement_expression
        | error
          {
                Error_SyntaxError (yyToken);
-               $$ = EmptyStatement.Value;
+               $$ = new EmptyStatement (GetLocation ($1));
          }
        ;
        
@@ -4764,27 +4770,20 @@ if_statement
        : IF open_parens_any boolean_expression CLOSE_PARENS 
          embedded_statement
          { 
-               Location l = GetLocation ($1);
-
-               $$ = new If ((BooleanExpression) $3, (Statement) $5, l);
-
-               // FIXME: location for warning should be loc property of $5.
-               if ($5 == EmptyStatement.Value)
-                       Report.Warning (642, 3, l, "Possible mistaken empty statement");
-
+               if ($5 is EmptyStatement)
+                       Report.Warning (642, 3, GetLocation ($5), "Possible mistaken empty statement");
+               
+               $$ = new If ((BooleanExpression) $3, (Statement) $5, GetLocation ($1));
          }
        | IF open_parens_any boolean_expression CLOSE_PARENS
          embedded_statement ELSE embedded_statement
          {
-               Location l = GetLocation ($1);
-
-               $$ = new If ((BooleanExpression) $3, (Statement) $5, (Statement) $7, l);
+               $$ = new If ((BooleanExpression) $3, (Statement) $5, (Statement) $7, GetLocation ($1));
 
-               // FIXME: location for warning should be loc property of $5 and $7.
-               if ($5 == EmptyStatement.Value)
-                       Report.Warning (642, 3, l, "Possible mistaken empty statement");
-               if ($7 == EmptyStatement.Value)
-                       Report.Warning (642, 3, l, "Possible mistaken empty statement");
+               if ($5 is EmptyStatement)
+                       Report.Warning (642, 3, GetLocation ($5), "Possible mistaken empty statement");
+               if ($7 is EmptyStatement)
+                       Report.Warning (642, 3, GetLocation ($7), "Possible mistaken empty statement");
          }
        ;
 
@@ -4956,7 +4955,7 @@ for_statement
        ;
 
 opt_for_initializer
-       : /* empty */           { $$ = EmptyStatement.Value; }
+       : /* empty */           { $$ = new EmptyStatement (lexer.Location); }
        | for_initializer       
        ;
 
@@ -4971,7 +4970,7 @@ opt_for_condition
        ;
 
 opt_for_iterator
-       : /* empty */           { $$ = EmptyStatement.Value; }
+       : /* empty */           { $$ = new EmptyStatement (lexer.Location); }
        | for_iterator
        ;
 
@@ -5439,66 +5438,78 @@ query_expression
 first_from_clause
        : FROM_FIRST IDENTIFIER IN expression
          {
-               $$ = new Linq.QueryExpression (current_block, new Linq.QueryStartClause ((Expression)$4));
-               var lt = (Tokenizer.LocatedToken) $2;
-               current_block = new Linq.QueryBlock (compiler, current_block, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1));
+               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
+         
+               var lt = (Tokenizer.LocatedToken) $2;     
+               $$ = new Linq.QueryExpression (new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$4, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1)));
          }
        | FROM_FIRST type IDENTIFIER IN expression
          {
+               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
+         
                var lt = (Tokenizer.LocatedToken) $3;
-               $$ = new Linq.QueryExpression (current_block, new Linq.Cast ((FullNamedExpression)$2, (Expression)$5));
-               current_block = new Linq.QueryBlock (compiler, current_block, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1));
+               $$ = new Linq.QueryExpression (
+                       new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$5, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1)) {
+                               IdentifierType = (FullNamedExpression)$2
+                       }
+               );
          }
        ;
 
 nested_from_clause
        : FROM IDENTIFIER IN expression
          {
-               $$ = new Linq.QueryExpression (current_block, new Linq.QueryStartClause ((Expression)$4));
-               var lt = (Tokenizer.LocatedToken) $2;
-               current_block = new Linq.QueryBlock (compiler, current_block, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1));
+               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
+         
+               var lt = (Tokenizer.LocatedToken) $2;     
+               $$ = new Linq.QueryExpression (new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$4, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1)));
          }
        | FROM type IDENTIFIER IN expression
          {
-               $$ = new Linq.QueryExpression (current_block, new Linq.Cast ((FullNamedExpression)$2, (Expression)$5));
+               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
+         
                var lt = (Tokenizer.LocatedToken) $3;
-               current_block = new Linq.QueryBlock (compiler, current_block, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1));
+               $$ = new Linq.QueryExpression (
+                       new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$5, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1)) {
+                               IdentifierType = (FullNamedExpression)$2
+                       }
+               );
          }
        ;
        
 from_clause
        : FROM IDENTIFIER IN
          {
-               current_block = new Linq.QueryBlock (compiler, current_block, GetLocation ($1));
+               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
          }
          expression
          {
                var lt = (Tokenizer.LocatedToken) $2;
                var sn = new SimpleMemberName (lt.Value, lt.Location);
-               $$ = new Linq.SelectMany (current_block.Toplevel, sn, (Expression)$5);
+               $$ = new Linq.SelectMany ((Linq.QueryBlock)current_block, sn, (Expression)$5, GetLocation ($1));
                
                current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
                
-               ((Linq.QueryBlock)current_block).AddTransparentParameter (compiler, sn);
+               ((Linq.QueryBlock)current_block).AddRangeVariable (sn);
          }       
        | FROM type IDENTIFIER IN
          {
-               current_block = new Linq.QueryBlock (compiler, current_block, GetLocation ($1));
+               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
          }
          expression
          {
                var lt = (Tokenizer.LocatedToken) $3;
                var sn = new SimpleMemberName (lt.Value, lt.Location);
 
-               FullNamedExpression type = (FullNamedExpression)$2;
-               
-               $$ = new Linq.SelectMany (current_block.Toplevel, sn, new Linq.Cast (type, (FullNamedExpression)$6));
+               $$ = new Linq.SelectMany ((Linq.QueryBlock)current_block, sn, (Expression)$6, GetLocation ($1)) {
+                       IdentifierType = (FullNamedExpression)$2
+               };
                
                current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
                
-               ((Linq.QueryBlock)current_block).AddTransparentParameter (compiler, sn);
+               ((Linq.QueryBlock)current_block).AddRangeVariable (sn);
          }
        ;       
 
@@ -5528,7 +5539,7 @@ select_or_group_clause
          }
          expression
          {
-               $$ = new Linq.Select (current_block.Toplevel, (Expression)$3, GetLocation ($1));
+               $$ = new Linq.Select ((Linq.QueryBlock)current_block, (Expression)$3, GetLocation ($1));
 
                current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
@@ -5536,10 +5547,10 @@ select_or_group_clause
        | GROUP
          {
                if (linq_clause_blocks == null)
-                       linq_clause_blocks = new Stack<Block> ();
+                       linq_clause_blocks = new Stack<Linq.QueryBlock> ();
                        
                current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
-               linq_clause_blocks.Push (current_block);
+               linq_clause_blocks.Push ((Linq.QueryBlock)current_block);
          }
          expression
          {
@@ -5550,7 +5561,7 @@ select_or_group_clause
          }
          BY expression
          {
-               $$ = new Linq.GroupBy (current_block.Toplevel, (Expression)$3, (ToplevelBlock) linq_clause_blocks.Pop (), (Expression)$6, GetLocation ($1));
+               $$ = new Linq.GroupBy ((Linq.QueryBlock)current_block, (Expression)$3, linq_clause_blocks.Pop (), (Expression)$6, GetLocation ($1));
                
                current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
@@ -5582,18 +5593,18 @@ query_body_clause
 let_clause
        : LET IDENTIFIER ASSIGN 
          {
-               current_block = new Linq.QueryBlock (compiler, current_block, GetLocation ($1));
+               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
          }
          expression
          {
                var lt = (Tokenizer.LocatedToken) $2;
                var sn = new SimpleMemberName (lt.Value, lt.Location);
-               $$ = new Linq.Let (current_block.Toplevel, current_container, sn, (Expression)$5);
+               $$ = new Linq.Let ((Linq.QueryBlock) current_block, sn, (Expression)$5, GetLocation ($1));
                
                current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
                
-               ((Linq.QueryBlock)current_block).AddTransparentParameter (compiler, sn);
+               ((Linq.QueryBlock)current_block).AddRangeVariable (sn);
          }
        ;
 
@@ -5604,7 +5615,7 @@ where_clause
          }
          boolean_expression
          {
-               $$ = new Linq.Where (current_block.Toplevel, (BooleanExpression)$3, GetLocation ($1));
+               $$ = new Linq.Where ((Linq.QueryBlock)current_block, (BooleanExpression)$3, GetLocation ($1));
 
                current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
@@ -5615,10 +5626,10 @@ join_clause
        : JOIN IDENTIFIER IN
          {
                if (linq_clause_blocks == null)
-                       linq_clause_blocks = new Stack<Block> ();
+                       linq_clause_blocks = new Stack<Linq.QueryBlock> ();
                        
                current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
-               linq_clause_blocks.Push (current_block);
+               linq_clause_blocks.Push ((Linq.QueryBlock) current_block);
          }
          expression ON
          {
@@ -5626,7 +5637,7 @@ join_clause
                current_block = current_block.Parent;
 
                current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
-               linq_clause_blocks.Push (current_block);
+               linq_clause_blocks.Push ((Linq.QueryBlock) current_block);
          }
          expression EQUALS
          {
@@ -5634,8 +5645,7 @@ join_clause
                current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
 
-               var lt = (Tokenizer.LocatedToken) $2;
-               current_block = new Linq.QueryBlock (compiler, current_block, new SimpleMemberName (lt.Value, lt.Location), lexer.Location);
+               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
          }
          expression opt_join_into
          {
@@ -5643,15 +5653,15 @@ join_clause
                var sn = new SimpleMemberName (lt.Value, lt.Location);
                SimpleMemberName sn2 = null;
                
-               ToplevelBlock outer_selector = (ToplevelBlock) linq_clause_blocks.Pop ();
-               ToplevelBlock block = (ToplevelBlock) linq_clause_blocks.Pop ();
+               var outer_selector = linq_clause_blocks.Pop ();
+               var block = linq_clause_blocks.Pop ();
 
                if ($12 == null) {
-                       $$ = new Linq.Join (block, sn, (Expression)$5, outer_selector, current_block.Toplevel, GetLocation ($1));
+                       $$ = new Linq.Join (block, sn, (Expression)$5, outer_selector, (Linq.QueryBlock) current_block, GetLocation ($1));
                } else {
                        var lt2 = (Tokenizer.LocatedToken) $12;
                        sn2 = new SimpleMemberName (lt2.Value, lt2.Location);
-                       $$ = new Linq.GroupJoin (block, sn, (Expression)$5, outer_selector, current_block.Toplevel,
+                       $$ = new Linq.GroupJoin (block, sn, (Expression)$5, outer_selector, (Linq.QueryBlock) current_block,
                                sn2, GetLocation ($1));
                }
 
@@ -5659,18 +5669,15 @@ join_clause
                current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
                        
-               if (sn2 == null)
-                       ((Linq.QueryBlock)current_block).AddTransparentParameter (compiler, sn);
-               else
-                       ((Linq.QueryBlock)current_block).AddTransparentParameter (compiler, sn2);
+               ((Linq.QueryBlock)current_block).AddRangeVariable (sn2 ?? sn);
          }
        | JOIN type IDENTIFIER IN
          {
                if (linq_clause_blocks == null)
-                       linq_clause_blocks = new Stack<Block> ();
+                       linq_clause_blocks = new Stack<Linq.QueryBlock> ();
                        
                current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
-               linq_clause_blocks.Push (current_block);
+               linq_clause_blocks.Push ((Linq.QueryBlock) current_block);
          }
          expression ON
          {
@@ -5678,7 +5685,7 @@ join_clause
                current_block = current_block.Parent;
 
                current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
-               linq_clause_blocks.Push (current_block);
+               linq_clause_blocks.Push ((Linq.QueryBlock) current_block);
          }
          expression EQUALS
          {
@@ -5686,35 +5693,33 @@ join_clause
                current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
 
-               var lt = (Tokenizer.LocatedToken) $3;
-               current_block = new Linq.QueryBlock (compiler, current_block, new SimpleMemberName (lt.Value, lt.Location), lexer.Location);
+               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
          }
          expression opt_join_into
          {
                var lt = (Tokenizer.LocatedToken) $3;
                var sn = new SimpleMemberName (lt.Value, lt.Location);
                SimpleMemberName sn2 = null;
-               ToplevelBlock outer_selector = (ToplevelBlock) linq_clause_blocks.Pop ();
-               ToplevelBlock block = (ToplevelBlock) linq_clause_blocks.Pop ();
+               var outer_selector = linq_clause_blocks.Pop ();
+               var block = linq_clause_blocks.Pop ();
                
-               Linq.Cast cast = new Linq.Cast ((FullNamedExpression)$2, (Expression)$6);
                if ($13 == null) {
-                       $$ = new Linq.Join (block, sn, cast, outer_selector, current_block.Toplevel, GetLocation ($1));
+                       $$ = new Linq.Join (block, sn, (Expression)$6, outer_selector, (Linq.QueryBlock) current_block, GetLocation ($1)) {
+                               IdentifierType = (FullNamedExpression)$2
+                       };
                } else {
                        var lt2 = (Tokenizer.LocatedToken) $13;
                        sn2 = new SimpleMemberName (lt2.Value, lt2.Location);
-                       $$ = new Linq.GroupJoin (block, sn, cast, outer_selector, current_block.Toplevel,
-                               sn2, GetLocation ($1));
+                       $$ = new Linq.GroupJoin (block, sn, (Expression)$6, outer_selector, (Linq.QueryBlock) current_block, sn2, GetLocation ($1)) {
+                               IdentifierType = (FullNamedExpression)$2
+                       };                      
                }
                
                current_block.AddStatement (new ContextualReturn ((Expression) $12));
                current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
                        
-               if (sn2 == null)
-                       ((Linq.QueryBlock)current_block).AddTransparentParameter (compiler, sn);
-               else
-                       ((Linq.QueryBlock)current_block).AddTransparentParameter (compiler, sn2);
+               ((Linq.QueryBlock)current_block).AddRangeVariable (sn2 ?? sn);
          }
        ;
        
@@ -5763,11 +5768,11 @@ orderings_then_by
                current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
          
-               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);   
+               current_block = new Linq.QueryBlock (compiler, (Linq.QueryBlock) current_block, lexer.Location);         
         }
         then_by
         {
-               ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$3;
+               ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$4;
                $$ = $1;
         }
        ;       
@@ -5775,30 +5780,30 @@ orderings_then_by
 order_by
        : expression
          {
-               $$ = new Linq.OrderByAscending (current_block.Toplevel, (Expression)$1);        
+               $$ = new Linq.OrderByAscending ((Linq.QueryBlock) current_block, (Expression)$1);       
          }
        | expression ASCENDING
          {
-               $$ = new Linq.OrderByAscending (current_block.Toplevel, (Expression)$1);        
+               $$ = new Linq.OrderByAscending ((Linq.QueryBlock) current_block, (Expression)$1);       
          }
        | expression DESCENDING
          {
-               $$ = new Linq.OrderByDescending (current_block.Toplevel, (Expression)$1);       
+               $$ = new Linq.OrderByDescending ((Linq.QueryBlock) current_block, (Expression)$1);      
          }
        ;
 
 then_by
        : expression
          {
-               $$ = new Linq.ThenByAscending (current_block.Toplevel, (Expression)$1); 
+               $$ = new Linq.ThenByAscending ((Linq.QueryBlock) current_block, (Expression)$1);        
          }
        | expression ASCENDING
          {
-               $$ = new Linq.ThenByAscending (current_block.Toplevel, (Expression)$1); 
+               $$ = new Linq.ThenByAscending ((Linq.QueryBlock) current_block, (Expression)$1);        
          }
        | expression DESCENDING
          {
-               $$ = new Linq.ThenByDescending (current_block.Toplevel, (Expression)$1);        
+               $$ = new Linq.ThenByDescending ((Linq.QueryBlock) current_block, (Expression)$1);       
          }     
        ;
 
@@ -5813,14 +5818,21 @@ opt_query_continuation
 
                current_block.SetEndLocation (GetLocation ($1));
                current_block = current_block.Parent;
-
-               var lt = (Tokenizer.LocatedToken) $2;
+       
+               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
                
-               current_block = new Linq.QueryBlock (compiler, current_block, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1));
+               if (linq_clause_blocks == null)
+                       linq_clause_blocks = new Stack<Linq.QueryBlock> ();
+                       
+               linq_clause_blocks.Push ((Linq.QueryBlock) current_block);              
          }
          query_body
          {
-               $$ = new Linq.QueryExpression (current_block, (Linq.AQueryClause)$4);
+               var current_block = linq_clause_blocks.Pop ();    
+               var lt = (Tokenizer.LocatedToken) $2;
+               $$ = new Linq.QueryStartClause ((Linq.QueryBlock)current_block, null, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1)) {
+                       next = (Linq.AQueryClause)$4
+               };
          }
        ;
        
@@ -6282,6 +6294,9 @@ Location GetLocation (object obj)
        if (obj is MemberName)
                return ((MemberName) obj).Location;
 
+//     if (obj is Expression)
+//             return ((Expression) obj).Location;             
+
        return lexer.Location;
 }
 
@@ -6292,7 +6307,7 @@ Report Report {
 void start_block (Location loc)
 {
        if (current_block == null || parsing_anonymous_method) {
-               current_block = new ToplevelBlock (compiler, current_block, current_local_parameters, current_generic_method, loc);
+               current_block = new ToplevelBlock (compiler, current_block, current_local_parameters, loc);
                parsing_anonymous_method = false;
        } else {
                current_block = new ExplicitBlock (current_block, loc, Location.Null);