Merge pull request #3269 from alexanderkyte/silence_zero_len
[mono.git] / mcs / mcs / cs-parser.jay
1 %{
2 //
3 // cs-parser.jay: The Parser for the C# compiler
4 //
5 // Authors: Miguel de Icaza (miguel@gnome.org)
6 //          Ravi Pratap     (ravi@ximian.com)
7 //          Marek Safar     (marek.safar@gmail.com)
8 //
9 // Dual Licensed under the terms of the GNU GPL and the MIT X11 license
10 //
11 // (C) 2001 Ximian, Inc (http://www.ximian.com)
12 // (C) 2004-2011 Novell, Inc
13 // Copyright 2011-2012 Xamarin Inc.
14 //
15
16 using System.Text;
17 using System.IO;
18 using System;
19 using System.Collections.Generic;
20
21 namespace Mono.CSharp
22 {
23         /// <summary>
24         ///    The C# Parser
25         /// </summary>
26         public class CSharpParser
27         {
28                 [Flags]
29                 enum ParameterModifierType
30                 {
31                         Ref             = 1 << 1,
32                         Out             = 1 << 2,
33                         This    = 1 << 3,
34                         Params  = 1 << 4,
35                         Arglist = 1 << 5,
36                         DefaultValue = 1 << 6,
37                         
38                         All = Ref | Out | This | Params | Arglist | DefaultValue,
39                         PrimaryConstructor = Ref | Out | Params | DefaultValue
40                 }
41                 
42                 static readonly object ModifierNone = 0;
43         
44                 NamespaceContainer current_namespace;
45                 TypeContainer current_container;
46                 TypeDefinition current_type;
47                 PropertyBase current_property;
48                 EventProperty current_event;
49                 EventField current_event_field;
50                 FieldBase current_field;
51         
52                 /// <summary>
53                 ///   Current block is used to add statements as we find
54                 ///   them.  
55                 /// </summary>
56                 Block      current_block;
57                 
58                 BlockVariable current_variable;
59
60                 Delegate   current_delegate;
61                 
62                 AnonymousMethodExpression current_anonymous_method;
63
64                 /// <summary>
65                 ///   This is used by the unary_expression code to resolve
66                 ///   a name against a parameter.  
67                 /// </summary>
68                 
69                 // FIXME: This is very ugly and it's very hard to reset it correctly
70                 // on all places, especially when some parameters are autogenerated.
71                 ParametersCompiled current_local_parameters;
72
73                 bool parsing_anonymous_method;
74                 
75                 bool async_block;
76
77                 ///
78                 /// An out-of-band stack.
79                 ///
80                 Stack<object> oob_stack;
81
82                 ///
83                 /// Controls the verbosity of the errors produced by the parser
84                 ///
85                 int yacc_verbose_flag;
86
87                 /// 
88                 /// Used by the interactive shell, flags whether EOF was reached
89                 /// and an error was produced
90                 ///
91                 public bool UnexpectedEOF;
92
93                 ///
94                 /// The current file.
95                 ///
96                 readonly CompilationSourceFile file;
97
98                 ///
99                 /// Temporary Xml documentation cache.
100                 /// For enum types, we need one more temporary store.
101                 ///
102                 string tmpComment;
103                 string enumTypeComment;
104                         
105                 /// Current attribute target
106                 string current_attr_target;
107                 
108                 ParameterModifierType valid_param_mod;
109                 
110                 bool default_parameter_used;
111
112                 /// When using the interactive parser, this holds the
113                 /// resulting expression
114                 public Class InteractiveResult;
115
116                 //
117                 // Keeps track of global data changes to undo on parser error
118                 //
119                 public Undo undo;
120
121                 bool? interactive_async;
122                 
123                 Stack<Linq.QueryBlock> linq_clause_blocks;
124
125                 ModuleContainer module;
126                 
127                 readonly CompilerContext compiler;
128                 readonly LanguageVersion lang_version;
129                 readonly bool doc_support;
130                 readonly CompilerSettings settings;
131                 readonly Report report;
132                 
133                 //
134                 // Instead of allocating carrier array everytime we
135                 // share the bucket for very common constructs which can never
136                 // be recursive
137                 //
138                 List<Parameter> parameters_bucket;
139                 
140                 //
141                 // Full AST support members
142                 //
143                 LocationsBag lbag;
144                 List<Tuple<Modifiers, Location>> mod_locations;
145                 Stack<Location> location_stack;
146 %}
147
148 %token EOF
149 %token NONE   /* This token is never returned by our lexer */
150 %token ERROR            // This is used not by the parser, but by the tokenizer.
151                         // do not remove.
152
153 /*
154  *These are the C# keywords
155  */
156 %token FIRST_KEYWORD
157 %token ABSTRACT 
158 %token AS
159 %token ADD
160 %token BASE     
161 %token BOOL     
162 %token BREAK    
163 %token BYTE     
164 %token CASE     
165 %token CATCH    
166 %token CHAR     
167 %token CHECKED  
168 %token CLASS    
169 %token CONST    
170 %token CONTINUE 
171 %token DECIMAL  
172 %token DEFAULT  
173 %token DELEGATE 
174 %token DO       
175 %token DOUBLE   
176 %token ELSE     
177 %token ENUM     
178 %token EVENT    
179 %token EXPLICIT 
180 %token EXTERN   
181 %token FALSE    
182 %token FINALLY  
183 %token FIXED    
184 %token FLOAT    
185 %token FOR      
186 %token FOREACH  
187 %token GOTO     
188 %token IF       
189 %token IMPLICIT 
190 %token IN       
191 %token INT      
192 %token INTERFACE
193 %token INTERNAL 
194 %token IS       
195 %token LOCK     
196 %token LONG     
197 %token NAMESPACE
198 %token NEW      
199 %token NULL     
200 %token OBJECT   
201 %token OPERATOR 
202 %token OUT      
203 %token OVERRIDE 
204 %token PARAMS   
205 %token PRIVATE  
206 %token PROTECTED
207 %token PUBLIC   
208 %token READONLY 
209 %token REF      
210 %token RETURN   
211 %token REMOVE
212 %token SBYTE    
213 %token SEALED   
214 %token SHORT    
215 %token SIZEOF   
216 %token STACKALLOC
217 %token STATIC   
218 %token STRING   
219 %token STRUCT   
220 %token SWITCH   
221 %token THIS     
222 %token THROW    
223 %token TRUE     
224 %token TRY      
225 %token TYPEOF   
226 %token UINT     
227 %token ULONG    
228 %token UNCHECKED
229 %token UNSAFE   
230 %token USHORT   
231 %token USING    
232 %token VIRTUAL  
233 %token VOID     
234 %token VOLATILE
235 %token WHERE
236 %token WHILE    
237 %token ARGLIST
238 %token PARTIAL
239 %token ARROW
240 %token FROM
241 %token FROM_FIRST
242 %token JOIN
243 %token ON
244 %token EQUALS
245 %token SELECT
246 %token GROUP
247 %token BY
248 %token LET
249 %token ORDERBY
250 %token ASCENDING
251 %token DESCENDING
252 %token INTO
253 %token INTERR_NULLABLE
254 %token EXTERN_ALIAS
255 %token REFVALUE
256 %token REFTYPE
257 %token MAKEREF
258 %token ASYNC
259 %token AWAIT
260 %token INTERR_OPERATOR
261 %token WHEN
262 %token INTERPOLATED_STRING
263 %token INTERPOLATED_STRING_END
264
265 /* C# keywords which are not really keywords */
266 %token GET
267 %token SET
268
269 %left LAST_KEYWORD
270
271 /* C# single character operators/punctuation. */
272 %token OPEN_BRACE
273 %token CLOSE_BRACE
274 %token OPEN_BRACKET
275 %token CLOSE_BRACKET
276 %token OPEN_PARENS
277 %token CLOSE_PARENS
278
279 %token DOT
280 %token COMMA
281 %token COLON
282 %token SEMICOLON
283 %token TILDE
284
285 %token PLUS
286 %token MINUS
287 %token BANG
288 %token ASSIGN
289 %token OP_LT
290 %token OP_GT
291 %token BITWISE_AND
292 %token BITWISE_OR
293 %token STAR
294 %token PERCENT
295 %token DIV
296 %token CARRET
297 %token INTERR
298
299 /* C# multi-character operators. */
300 %token DOUBLE_COLON
301 %token OP_INC
302 %token OP_DEC
303 %token OP_SHIFT_LEFT
304 %token OP_SHIFT_RIGHT
305 %token OP_LE
306 %token OP_GE
307 %token OP_EQ
308 %token OP_NE
309 %token OP_AND
310 %token OP_OR
311 %token OP_MULT_ASSIGN
312 %token OP_DIV_ASSIGN
313 %token OP_MOD_ASSIGN
314 %token OP_ADD_ASSIGN
315 %token OP_SUB_ASSIGN
316 %token OP_SHIFT_LEFT_ASSIGN
317 %token OP_SHIFT_RIGHT_ASSIGN
318 %token OP_AND_ASSIGN
319 %token OP_XOR_ASSIGN
320 %token OP_OR_ASSIGN
321 %token OP_PTR
322 %token OP_COALESCING
323
324 /* Generics <,> tokens */
325 %token OP_GENERICS_LT
326 %token OP_GENERICS_LT_DECL
327 %token OP_GENERICS_GT
328
329 %token LITERAL
330
331 %token IDENTIFIER
332 %token OPEN_PARENS_LAMBDA
333 %token OPEN_PARENS_CAST
334 %token GENERIC_DIMENSION
335 %token DEFAULT_COLON
336 %token OPEN_BRACKET_EXPR
337
338 // Make the parser go into eval mode parsing (statements and compilation units).
339 %token EVAL_STATEMENT_PARSER
340 %token EVAL_COMPILATION_UNIT_PARSER
341 %token EVAL_USING_DECLARATIONS_UNIT_PARSER
342
343 %token DOC_SEE
344
345 // 
346 // This token is generated to trigger the completion engine at this point
347 //
348 %token GENERATE_COMPLETION
349
350 //
351 // This token is return repeatedly after the first GENERATE_COMPLETION
352 // token is produced and before the final EOF
353 //
354 %token COMPLETE_COMPLETION
355
356 /* Add precedence rules to solve dangling else s/r conflict */
357 %nonassoc IF
358 %nonassoc ELSE
359
360 /* Define the operator tokens and their precedences */
361 %right ASSIGN
362 %right OP_COALESCING
363 %right INTERR
364 %left OP_OR
365 %left OP_AND
366 %left BITWISE_OR
367 %left BITWISE_AND
368 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
369 %left PLUS MINUS
370 %left STAR DIV PERCENT
371 %right BANG CARRET UMINUS
372 %nonassoc OP_INC OP_DEC
373 %left OPEN_PARENS
374 %left OPEN_BRACKET OPEN_BRACE
375 %left DOT
376
377 %start compilation_unit
378 %%
379
380 compilation_unit
381         : outer_declaration opt_EOF
382           {
383                 Lexer.check_incorrect_doc_comment ();
384           }
385         | interactive_parsing  { Lexer.CompleteOnEOF = false; } opt_EOF
386         | documentation_parsing
387         ;
388         
389 outer_declaration
390         : opt_extern_alias_directives opt_using_directives
391         | opt_extern_alias_directives opt_using_directives namespace_or_type_declarations opt_attributes
392           {
393                 if ($4 != null) {
394                         Attributes attrs = (Attributes) $4;
395                         report.Error (1730, attrs.Attrs [0].Location,
396                                 "Assembly and module attributes must precede all other elements except using clauses and extern alias declarations");
397
398                         current_namespace.UnattachedAttributes = attrs;
399                 }
400           }
401         | opt_extern_alias_directives opt_using_directives attribute_sections
402           {
403                 Attributes attrs = (Attributes) $3;
404                 if (attrs != null) {
405                         foreach (var a in attrs.Attrs) {
406                                 if (a.ExplicitTarget == "assembly" || a.ExplicitTarget == "module")
407                                         continue;
408
409                                 if (a.ExplicitTarget == null)
410                                         report.Error (-1671, a.Location, "Global attributes must have attribute target specified");
411                         }
412                 }
413
414                 module.AddAttributes ((Attributes) $3, current_namespace);
415           }
416         | error
417           {
418                 if (yyToken == Token.EXTERN_ALIAS)
419                         report.Error (439, lexer.Location, "An extern alias declaration must precede all other elements");
420                 else
421                         Error_SyntaxError (yyToken);
422           }
423         ;
424         
425 opt_EOF
426         : /* empty */
427         | EOF
428         ;
429
430 extern_alias_directives
431         : extern_alias_directive
432         | extern_alias_directives extern_alias_directive
433         ;
434
435 extern_alias_directive
436         : EXTERN_ALIAS IDENTIFIER IDENTIFIER SEMICOLON
437           {
438                 var lt = (LocatedToken) $2;
439                 string s = lt.Value;
440                 if (s != "alias") {
441                         syntax_error (lt.Location, "`alias' expected");
442                 } else {
443                         if (lang_version == LanguageVersion.ISO_1)
444                                 FeatureIsNotAvailable (lt.Location, "external alias");
445
446                         lt = (LocatedToken) $3;
447                         if (lt.Value == QualifiedAliasMember.GlobalAlias) {
448                                 RootNamespace.Error_GlobalNamespaceRedefined (report, lt.Location);
449                         }
450                         
451                         var na = new UsingExternAlias (new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1));
452                         current_namespace.AddUsing (na);
453                         
454                         lbag.AddLocation (na, GetLocation ($2), GetLocation ($4));
455                 }
456           }
457         | EXTERN_ALIAS error
458           {
459                 Error_SyntaxError (yyToken);
460           }
461         ;
462  
463 using_directives
464         : using_directive 
465         | using_directives using_directive
466         ;
467
468 using_directive
469         : using_namespace
470           {
471                 if (doc_support)
472                         Lexer.doc_state = XmlCommentState.Allowed;
473           }
474         ;
475
476 using_namespace
477         : USING opt_static namespace_or_type_expr SEMICOLON
478           {
479                 UsingClause uc;
480                 if ($2 != null) {
481                         if (lang_version <= LanguageVersion.V_5)
482                                 FeatureIsNotAvailable (GetLocation ($2), "using static");
483
484                         uc = new UsingType ((ATypeNameExpression) $3, GetLocation ($1));
485                         lbag.AddLocation (uc, GetLocation ($2), GetLocation ($4));
486                 } else {
487                         uc = new UsingNamespace ((ATypeNameExpression) $3, GetLocation ($1));
488                         lbag.AddLocation (uc, GetLocation ($4));
489                 }
490
491                 current_namespace.AddUsing (uc);
492           }
493         | USING opt_static IDENTIFIER ASSIGN namespace_or_type_expr SEMICOLON
494           {
495                 var lt = (LocatedToken) $3;
496                 if (lang_version != LanguageVersion.ISO_1 && lt.Value == "global") {
497                         report.Warning (440, 2, lt.Location,
498                          "An alias named `global' will not be used when resolving `global::'. The global namespace will be used instead");
499                 }
500
501                 if ($2 != null) {
502                         report.Error (8085, GetLocation ($2), "A `using static' directive cannot be used to declare an alias");
503                 }
504
505                 var un = new UsingAliasNamespace (new SimpleMemberName (lt.Value, lt.Location), (ATypeNameExpression) $5, GetLocation ($1));
506                 current_namespace.AddUsing (un);
507                 
508                 lbag.AddLocation (un, GetLocation ($4), GetLocation ($6));
509           }
510         | USING error
511          {
512                 Error_SyntaxError (yyToken);
513                 $$ = null;
514          }
515         ;
516
517 opt_static
518         :
519         | STATIC
520         ;
521
522 //
523 // Strictly speaking, namespaces don't have attributes but
524 // we parse global attributes along with namespace declarations and then
525 // detach them
526 // 
527 namespace_declaration
528         : opt_attributes NAMESPACE namespace_name
529           {
530                 Attributes attrs = (Attributes) $1;
531                 var name = (MemberName) $3;
532                 if (attrs != null) {
533                         bool valid_global_attrs = true;
534                         if ((current_namespace.DeclarationFound || current_namespace != file)) {
535                                 valid_global_attrs = false;
536                         } else {
537                                 foreach (var a in attrs.Attrs) {
538                                         if (a.ExplicitTarget == "assembly" || a.ExplicitTarget == "module")
539                                                 continue;
540                                                 
541                                         valid_global_attrs = false;
542                                         break;
543                                 }
544                         }
545                         
546                         if (!valid_global_attrs)
547                                 report.Error (1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
548                 }
549         
550                 module.AddAttributes (attrs, current_namespace);
551                 
552                 var ns = new NamespaceContainer (name, current_namespace);
553                 current_namespace.AddTypeContainer (ns);
554                 current_container = current_namespace = ns;
555           }
556           OPEN_BRACE
557           {
558                 if (doc_support)
559                         Lexer.doc_state = XmlCommentState.Allowed;
560           }
561           opt_extern_alias_directives opt_using_directives opt_namespace_or_type_declarations CLOSE_BRACE opt_semicolon_error
562           {
563                 if ($11 != null)
564                         lbag.AddLocation (current_container, GetLocation ($2), GetLocation ($5), GetLocation ($10), GetLocation ($11));
565                 else
566                         lbag.AddLocation (current_container, GetLocation ($2), GetLocation ($5), GetLocation ($10));
567           
568                 current_container = current_namespace = current_namespace.Parent;
569           }
570         | opt_attributes NAMESPACE namespace_name
571           {
572                 report.Error (1514, lexer.Location, "Unexpected symbol `{0}', expecting `.' or `{{'", GetSymbolName (yyToken));
573
574                 var name = (MemberName) $3;             
575                 var ns = new NamespaceContainer (name, current_namespace);
576                 lbag.AddLocation (ns, GetLocation ($2));
577                 current_namespace.AddTypeContainer (ns);
578           }
579         ;
580
581 opt_semicolon_error
582         : /* empty */
583         | SEMICOLON
584         | error
585           {
586                 Error_SyntaxError (yyToken);
587                 $$ = null;
588           }
589         ;
590
591 namespace_name
592         : IDENTIFIER
593           {
594                 var lt = (LocatedToken) $1;
595                 $$ = new MemberName (lt.Value, lt.Location);
596           }
597         | namespace_name DOT IDENTIFIER
598           {
599                 var lt = (LocatedToken) $3;
600                 $$ = new MemberName ((MemberName) $1, lt.Value, lt.Location);           
601                 lbag.AddLocation ($$, GetLocation ($2));
602           }
603         | error
604           {
605                 Error_SyntaxError (yyToken);
606                 $$ = new MemberName ("<invalid>", lexer.Location);
607           }
608         ;
609
610 opt_semicolon
611         : /* empty */
612         | SEMICOLON
613         ;
614
615 opt_comma
616         : /* empty */
617         | COMMA
618         ;
619
620 opt_using_directives
621         : /* empty */
622         | using_directives
623         ;
624
625 opt_extern_alias_directives
626         : /* empty */
627         | extern_alias_directives
628         ;
629
630 opt_namespace_or_type_declarations
631         : /* empty */
632         | namespace_or_type_declarations
633         ;
634
635 namespace_or_type_declarations
636         : namespace_or_type_declaration
637         | namespace_or_type_declarations namespace_or_type_declaration
638         ;
639
640 namespace_or_type_declaration
641         : type_declaration
642           {
643                 if ($1 != null) {
644                         TypeContainer ds = (TypeContainer)$1;
645
646                         if ((ds.ModFlags & (Modifiers.PRIVATE | Modifiers.PROTECTED)) != 0){
647                                 report.Error (1527, ds.Location, 
648                                 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
649                         }
650
651                         // Here is a trick, for explicit attributes we don't know where they belong to until
652                         // we parse succeeding declaration hence we parse them as normal and re-attach them
653                         // when we know whether they are global (assembly:, module:) or local (type:).
654                         if (ds.OptAttributes != null) {
655                                 ds.OptAttributes.ConvertGlobalAttributes (ds, current_namespace, !current_namespace.DeclarationFound && current_namespace == file);
656                         }
657                 }
658                 current_namespace.DeclarationFound = true;
659           }
660         | namespace_declaration
661           {
662                 current_namespace.DeclarationFound = true;
663           }
664         | attribute_sections CLOSE_BRACE {
665                 current_namespace.UnattachedAttributes = (Attributes) $1;
666                 report.Error (1518, lexer.Location, "Attributes must be attached to class, delegate, enum, interface or struct");
667                 lexer.putback ('}');
668           }
669         ;
670
671 type_declaration
672         : class_declaration             
673         | struct_declaration
674         | interface_declaration
675         | enum_declaration              
676         | delegate_declaration
677 //
678 // Enable this when we have handled all errors, because this acts as a generic fallback
679 //
680 //      | error {
681 //              Console.WriteLine ("Token=" + yyToken);
682 //              report.Error (1518, GetLocation ($1), "Expected class, struct, interface, enum or delegate");
683 //        }
684         ;
685
686 //
687 // Attributes
688 //
689
690 opt_attributes
691         : /* empty */ 
692         | attribute_sections
693     ;
694  
695 attribute_sections
696         : attribute_section
697           {
698                 var sect = (List<Attribute>) $1;
699                 $$ = new Attributes (sect);
700           }
701         | attribute_sections attribute_section
702           {
703                 Attributes attrs = $1 as Attributes;
704                 var sect = (List<Attribute>) $2;
705                 if (attrs == null)
706                         attrs = new Attributes (sect);
707                 else if (sect != null)
708                         attrs.AddAttributes (sect);
709                 $$ = attrs;
710           }
711         ;
712         
713 attribute_section
714         : OPEN_BRACKET
715           {
716                 PushLocation (GetLocation ($1));
717                 lexer.parsing_attribute_section = true;
718           }
719           attribute_section_cont
720           {
721                 lexer.parsing_attribute_section = false;
722                 $$ = $3;
723           }
724         ;       
725         
726 attribute_section_cont
727         : attribute_target COLON
728           {
729                 current_attr_target = (string) $1;
730                 if (current_attr_target == "assembly" || current_attr_target == "module") {
731                         Lexer.check_incorrect_doc_comment ();
732                 }
733           }
734           attribute_list opt_comma CLOSE_BRACKET
735           {
736                 // when attribute target is invalid
737                 if (current_attr_target == string.Empty)
738                         $$ = new List<Attribute> (0);
739                 else
740                         $$ = $4;
741
742                 lbag.InsertLocation ($$, 0, PopLocation ());
743                 if ($5 != null) {
744                         lbag.AddLocation ($$, GetLocation ($2), GetLocation ($5), GetLocation ($6));
745                 } else {
746                         lbag.AddLocation ($$, GetLocation ($2), GetLocation ($6));
747                 }
748
749                 current_attr_target = null;
750                 lexer.parsing_attribute_section = false;
751           }
752         | attribute_list opt_comma CLOSE_BRACKET
753           {
754                 $$ = $1;
755
756                 lbag.InsertLocation ($$, 0, PopLocation ());
757                 if ($2 != null) {
758                         lbag.AddLocation ($$, GetLocation($2), GetLocation ($3));
759                 } else {
760                         lbag.AddLocation ($$, GetLocation($3));
761                 }
762           }
763         | IDENTIFIER error
764           {
765                 Error_SyntaxError (yyToken);
766
767                 var lt = (LocatedToken) $1;
768                 var tne = new SimpleName (lt.Value, null, lt.Location);
769
770                 $$ = new List<Attribute> () {
771                         new Attribute (null, tne, null, GetLocation ($1), false)
772                 };
773           }
774         | error
775           {
776                 if (CheckAttributeTarget (yyToken, GetTokenName (yyToken), GetLocation ($1)).Length > 0)
777                         Error_SyntaxError (yyToken);
778
779                 $$ = null;
780           }
781         ;       
782
783 attribute_target
784         : IDENTIFIER
785           {
786                 var lt = (LocatedToken) $1;
787                 $$ = CheckAttributeTarget (yyToken, lt.Value, lt.Location);
788           }
789         | EVENT  { $$ = "event"; }
790         | RETURN { $$ = "return"; }
791         ;
792
793 attribute_list
794         : attribute
795           {
796                 $$ = new List<Attribute> (4) { (Attribute) $1 };
797           }
798         | attribute_list COMMA attribute
799           {
800                 var attrs = (List<Attribute>) $1;
801                 if (attrs != null) {
802                         attrs.Add ((Attribute) $3);
803                         lbag.AppendTo (attrs, GetLocation ($2));
804                 }
805
806                 $$ = attrs;
807           }
808         ;
809
810 attribute
811         : attribute_name
812           {
813                 ++lexer.parsing_block;
814           }
815           opt_attribute_arguments
816           {
817                 --lexer.parsing_block;
818                 
819                 var tne = (ATypeNameExpression) $1;
820                 if (tne.HasTypeArguments) {
821                         report.Error (404, tne.Location, "Attributes cannot be generic");
822                 }
823
824                 $$ = new Attribute (current_attr_target, tne, (Arguments[]) $3, GetLocation ($1), lexer.IsEscapedIdentifier (tne));
825           }
826         ;
827
828 attribute_name
829         : namespace_or_type_expr
830         ;
831
832 opt_attribute_arguments
833         : /* empty */   { $$ = null; }
834         | OPEN_PARENS attribute_arguments CLOSE_PARENS
835           {
836                 $$ = $2;
837           }
838         ;
839
840
841 attribute_arguments
842         : /* empty */           { $$ = null; } 
843         | positional_or_named_argument
844           {
845                 Arguments a = new Arguments (4);
846                 a.Add ((Argument) $1);
847                 $$ = new Arguments [] { a, null };
848           }
849         | named_attribute_argument
850           {
851                 Arguments a = new Arguments (4);
852                 a.Add ((Argument) $1);  
853                 $$ = new Arguments [] { null, a };
854           }
855     | attribute_arguments COMMA positional_or_named_argument
856           {
857                 Arguments[] o = (Arguments[]) $1;
858                 if (o [1] != null) {
859                         report.Error (1016, ((Argument) $3).Expr.Location, "Named attribute arguments must appear after the positional arguments");
860                         o [0] = new Arguments (4);
861                 }
862                 
863                 Arguments args = ((Arguments) o [0]);
864                 if (args.Count > 0 && !($3 is NamedArgument) && args [args.Count - 1] is NamedArgument)
865                         Error_NamedArgumentExpected ((NamedArgument) args [args.Count - 1]);
866                 
867                 args.Add ((Argument) $3);
868           }
869     | attribute_arguments COMMA named_attribute_argument
870           {
871                 Arguments[] o = (Arguments[]) $1;
872                 if (o [1] == null) {
873                         o [1] = new Arguments (4);
874                 }
875
876                 ((Arguments) o [1]).Add ((Argument) $3);
877           }
878     ;
879
880 positional_or_named_argument
881         : expression
882           {
883                 $$ = new Argument ((Expression) $1);
884           }
885         | named_argument
886         | error
887           {
888                 Error_SyntaxError (yyToken);
889                 $$ = null;
890           }
891         ;
892
893 named_attribute_argument
894         : IDENTIFIER ASSIGN
895           {
896                 ++lexer.parsing_block;
897           }
898           expression
899           {
900                 --lexer.parsing_block;
901                 var lt = (LocatedToken) $1;
902                 $$ = new NamedArgument (lt.Value, lt.Location, (Expression) $4);          
903                 lbag.AddLocation ($$, GetLocation($2));
904           }
905         ;
906         
907 named_argument
908         : identifier_inside_body COLON opt_named_modifier named_argument_expr
909           {
910                 if (lang_version <= LanguageVersion.V_3)
911                         FeatureIsNotAvailable (GetLocation ($1), "named argument");
912                         
913                 // Avoid boxing in common case (no modifier)
914                 var arg_mod = $3 == null ? Argument.AType.None : (Argument.AType) $3;
915                         
916                 var lt = (LocatedToken) $1;
917                 $$ = new NamedArgument (lt.Value, lt.Location, (Expression) $4, arg_mod);
918                 lbag.AddLocation ($$, GetLocation($2));
919           }
920         ;
921
922 named_argument_expr
923         : expression_or_error
924 //      | declaration_expression
925         ;
926         
927 opt_named_modifier
928         : /* empty */   { $$ = null; }
929         | REF
930           { 
931                 $$ = Argument.AType.Ref;
932           }
933         | OUT
934           { 
935                 $$ = Argument.AType.Out;
936           }
937         ;
938                   
939 opt_class_member_declarations
940         : /* empty */
941         | class_member_declarations
942         ;
943
944 class_member_declarations
945         : class_member_declaration
946           {
947                 lexer.parsing_modifiers = true;
948                 lexer.parsing_block = 0;
949           }
950         | class_member_declarations class_member_declaration
951           {
952                 lexer.parsing_modifiers = true;
953                 lexer.parsing_block = 0;
954           }
955         ;
956         
957 class_member_declaration
958         : constant_declaration
959         | field_declaration
960         | method_declaration
961         | property_declaration
962         | event_declaration
963         | indexer_declaration
964         | operator_declaration
965         | constructor_declaration
966         | primary_constructor_body
967         | destructor_declaration
968         | type_declaration
969         | attributes_without_members
970         | incomplete_member
971         | error
972           {
973                 report.Error (1519, lexer.Location, "Unexpected symbol `{0}' in class, struct, or interface member declaration",
974                         GetSymbolName (yyToken));
975                 $$ = null;
976                 lexer.parsing_generic_declaration = false;
977           }     
978         ;
979
980 primary_constructor_body
981         : OPEN_BRACE
982           {
983                 current_local_parameters = current_type.PrimaryConstructorParameters;
984                 if (current_local_parameters == null) {
985                         report.Error (9010, GetLocation ($1), "Primary constructor body is not allowed");
986                         current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters;
987                 }
988
989                 ++lexer.parsing_block;
990                 start_block (GetLocation ($1));
991           }
992           opt_statement_list block_end
993           {
994                 current_local_parameters = null;
995                 var t = current_type as ClassOrStruct;
996                 if (t != null) {
997                         var b = (ToplevelBlock) $4;
998                         if (t.PrimaryConstructorBlock != null) {
999                                 report.Error (8041, b.StartLocation, "Primary constructor already has a body");
1000                         } else {
1001                                 t.PrimaryConstructorBlock = b;
1002                         }
1003                 }
1004           }
1005         ;
1006
1007 struct_declaration
1008         : opt_attributes
1009           opt_modifiers
1010           opt_partial
1011           STRUCT
1012           {
1013           }
1014           type_declaration_name
1015           { 
1016                 lexer.ConstraintsParsing = true;
1017                 valid_param_mod = ParameterModifierType.PrimaryConstructor;
1018                 push_current_container (new Struct (current_container, (MemberName) $6, (Modifiers) $2, (Attributes) $1), $3);
1019           }
1020           opt_primary_parameters
1021           opt_class_base
1022           opt_type_parameter_constraints_clauses
1023           {
1024                 valid_param_mod = 0;
1025                 lexer.ConstraintsParsing = false;
1026
1027                 if ($8 != null)
1028                         current_type.PrimaryConstructorParameters = (ParametersCompiled) $8;
1029
1030                 if ($10 != null)
1031                         current_container.SetConstraints ((List<Constraints>) $10);
1032
1033                 if (doc_support)
1034                         current_container.PartialContainer.DocComment = Lexer.consume_doc_comment ();
1035
1036                 lbag.AddMember (current_container, mod_locations, GetLocation ($4));
1037                 
1038                 lexer.parsing_modifiers = true;
1039           }
1040           OPEN_BRACE
1041           {
1042                 if (doc_support)
1043                         Lexer.doc_state = XmlCommentState.Allowed;
1044           }
1045           opt_class_member_declarations CLOSE_BRACE
1046           {
1047                 --lexer.parsing_declaration;
1048                 if (doc_support)
1049                         Lexer.doc_state = XmlCommentState.Allowed;
1050           }
1051           opt_semicolon
1052           {
1053                 if ($16 == null) {
1054                         lbag.AppendToMember (current_container, GetLocation ($12), GetLocation ($15));
1055                 } else {
1056                         lbag.AppendToMember (current_container, GetLocation ($12), GetLocation ($15), GetLocation ($17));
1057                 }
1058                 $$ = pop_current_class ();
1059           }
1060         | opt_attributes opt_modifiers opt_partial STRUCT error
1061           {
1062                 Error_SyntaxError (yyToken);
1063           }
1064         ;
1065         
1066 constant_declaration
1067         : opt_attributes 
1068           opt_modifiers
1069           CONST type IDENTIFIER
1070           {
1071                 var lt = (LocatedToken) $5;
1072                 var mod = (Modifiers) $2;
1073                 current_field = new Const (current_type, (FullNamedExpression) $4, mod, new MemberName (lt.Value, lt.Location), (Attributes) $1);
1074                 current_type.AddMember (current_field);
1075                 
1076                 if ((mod & Modifiers.STATIC) != 0) {
1077                         report.Error (504, current_field.Location, "The constant `{0}' cannot be marked static", current_field.GetSignatureForError ());
1078                 }
1079                 
1080                 $$ = current_field;
1081           }
1082           constant_initializer opt_constant_declarators SEMICOLON
1083           {
1084                 if (doc_support) {
1085                         current_field.DocComment = Lexer.consume_doc_comment ();
1086                         Lexer.doc_state = XmlCommentState.Allowed;
1087                 }
1088                 
1089                 current_field.Initializer = (ConstInitializer) $7;
1090                 lbag.AddMember (current_field, mod_locations, GetLocation ($3), GetLocation ($9));
1091                 current_field = null;
1092           }
1093         | opt_attributes 
1094           opt_modifiers
1095           CONST type error
1096           {
1097                 Error_SyntaxError (yyToken);
1098
1099                 current_type.AddMember (new Const (current_type, (FullNamedExpression) $4, (Modifiers) $2, MemberName.Null, (Attributes) $1));
1100           }     
1101         ;
1102         
1103 opt_constant_declarators
1104         : /* empty */
1105         | constant_declarators
1106         ;
1107         
1108 constant_declarators
1109         : constant_declarator
1110           {
1111                 current_field.AddDeclarator ((FieldDeclarator) $1);
1112           }
1113         | constant_declarators constant_declarator
1114           {
1115                 current_field.AddDeclarator ((FieldDeclarator) $2);
1116           }
1117         ;
1118         
1119 constant_declarator
1120         : COMMA IDENTIFIER constant_initializer
1121           {
1122                 var lt = (LocatedToken) $2;
1123                 $$ = new FieldDeclarator (new SimpleMemberName (lt.Value, lt.Location), (ConstInitializer) $3);
1124                 lbag.AddLocation ($$, GetLocation ($1));
1125           }
1126         ;               
1127
1128 constant_initializer
1129         : ASSIGN
1130           {
1131                 ++lexer.parsing_block;
1132           }
1133           constant_initializer_expr
1134           {
1135                 --lexer.parsing_block;
1136                 $$ = new ConstInitializer (current_field, (Expression) $3, GetLocation ($1));
1137           }
1138         | error
1139           {
1140                 report.Error (145, lexer.Location, "A const field requires a value to be provided");
1141                 $$ = null;
1142           }       
1143         ;
1144         
1145 constant_initializer_expr
1146         : constant_expression
1147         | array_initializer
1148         ;
1149
1150 field_declaration
1151         : opt_attributes
1152           opt_modifiers
1153           member_type IDENTIFIER
1154           {
1155                 lexer.parsing_generic_declaration = false;
1156
1157                 FullNamedExpression type = (FullNamedExpression) $3;
1158                 if (type.Type != null && type.Type.Kind == MemberKind.Void)
1159                         report.Error (670, GetLocation ($3), "Fields cannot have void type");
1160                         
1161                 var lt = (LocatedToken) $4;
1162                 current_field = new Field (current_type, type, (Modifiers) $2, new MemberName (lt.Value, lt.Location), (Attributes) $1);
1163                 current_type.AddField (current_field);
1164                 $$ = current_field;
1165           }
1166           opt_field_initializer
1167           opt_field_declarators
1168           SEMICOLON
1169           { 
1170                 if (doc_support) {
1171                         current_field.DocComment = Lexer.consume_doc_comment ();
1172                         Lexer.doc_state = XmlCommentState.Allowed;
1173                 }
1174                         
1175                 lbag.AddMember (current_field, mod_locations, GetLocation ($8));
1176                 $$ = current_field;
1177                 current_field = null;
1178           }
1179         | opt_attributes
1180           opt_modifiers
1181           FIXED simple_type IDENTIFIER
1182           { 
1183                 if (lang_version < LanguageVersion.ISO_2)
1184                         FeatureIsNotAvailable (GetLocation ($3), "fixed size buffers");
1185
1186                 var lt = (LocatedToken) $5;
1187                 current_field = new FixedField (current_type, (FullNamedExpression) $4, (Modifiers) $2,
1188                         new MemberName (lt.Value, lt.Location), (Attributes) $1);
1189                         
1190                 current_type.AddField (current_field);
1191           }
1192           fixed_field_size opt_fixed_field_declarators SEMICOLON
1193           {
1194                 if (doc_support) {
1195                         current_field.DocComment = Lexer.consume_doc_comment ();
1196                         Lexer.doc_state = XmlCommentState.Allowed;
1197             }
1198
1199                 current_field.Initializer = (ConstInitializer) $7;          
1200                 lbag.AddMember (current_field, mod_locations, GetLocation ($9));
1201                 $$ = current_field;
1202             current_field = null;
1203           }
1204         | opt_attributes
1205           opt_modifiers
1206           FIXED simple_type error
1207           SEMICOLON
1208           {
1209                 report.Error (1641, GetLocation ($5), "A fixed size buffer field must have the array size specifier after the field name");
1210           }
1211         ;
1212         
1213 opt_field_initializer
1214         : /* empty */
1215         | ASSIGN
1216           {
1217                 ++lexer.parsing_block;
1218                 current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters;
1219                 start_block (GetLocation ($1));
1220           }
1221           variable_initializer
1222           {
1223                 --lexer.parsing_block;
1224                 current_field.Initializer = (Expression) $3;
1225                 lbag.AppendToMember (current_field, GetLocation ($1));
1226                 end_block (lexer.Location);
1227                 current_local_parameters = null;
1228           }
1229         ;
1230         
1231 opt_field_declarators
1232         : /* empty */
1233         | field_declarators
1234         ;
1235         
1236 field_declarators
1237         : field_declarator
1238           {
1239                 current_field.AddDeclarator ((FieldDeclarator) $1);
1240           }
1241         | field_declarators field_declarator
1242           {
1243                 current_field.AddDeclarator ((FieldDeclarator) $2);
1244           }
1245         ;
1246         
1247 field_declarator
1248         : COMMA IDENTIFIER
1249           {
1250                 var lt = (LocatedToken) $2;
1251                 $$ = new FieldDeclarator (new SimpleMemberName (lt.Value, lt.Location), null);
1252                 lbag.AddLocation ($$, GetLocation ($1));
1253           }
1254         | COMMA IDENTIFIER ASSIGN
1255           {
1256                 ++lexer.parsing_block;
1257           }
1258           variable_initializer
1259           {
1260                 --lexer.parsing_block;
1261                 var lt = (LocatedToken) $2;       
1262                 $$ = new FieldDeclarator (new SimpleMemberName (lt.Value, lt.Location), (Expression) $5);
1263                 lbag.AddLocation ($$, GetLocation ($1), GetLocation ($3));
1264           }
1265         ;       
1266
1267 opt_fixed_field_declarators
1268         : /* empty */
1269         | fixed_field_declarators
1270         ;
1271         
1272 fixed_field_declarators
1273         : fixed_field_declarator
1274           {
1275                 current_field.AddDeclarator ((FieldDeclarator) $1);
1276           }
1277         | fixed_field_declarators fixed_field_declarator
1278           {
1279                 current_field.AddDeclarator ((FieldDeclarator) $2);
1280           }
1281         ;
1282         
1283 fixed_field_declarator
1284         : COMMA IDENTIFIER fixed_field_size
1285           {
1286                 var lt = (LocatedToken) $2;       
1287                 $$ = new FieldDeclarator (new SimpleMemberName (lt.Value, lt.Location), (ConstInitializer) $3);
1288                 lbag.AddLocation ($$, GetLocation ($1));
1289           }
1290         ;
1291
1292 fixed_field_size
1293         : OPEN_BRACKET
1294           {
1295                 ++lexer.parsing_block;
1296           }
1297           expression CLOSE_BRACKET
1298           {
1299                 --lexer.parsing_block;
1300                 $$ = new ConstInitializer (current_field, (Expression) $3, GetLocation ($1));
1301                 lbag.AddLocation ($$, GetLocation ($4));
1302           }
1303         | OPEN_BRACKET error
1304           {
1305                 report.Error (443, lexer.Location, "Value or constant expected");
1306                 $$ = null;
1307           }       
1308         ;
1309
1310 variable_initializer
1311         : expression
1312         | array_initializer
1313         | error
1314           {
1315                 // It has to be here for the parent to safely restore artificial block
1316                 Error_SyntaxError (yyToken);
1317                 $$ = null;
1318           }
1319         ;
1320
1321 method_declaration
1322         : method_header
1323           {
1324                 if (doc_support)
1325                         Lexer.doc_state = XmlCommentState.NotAllowed;
1326
1327                 // Was added earlier in the case of body being eof for full ast
1328           }
1329           method_body_expression_block
1330           {
1331                 Method method = (Method) $1;
1332                 method.Block = (ToplevelBlock) $3;
1333                 async_block = false;
1334                 
1335                 if (method.Block == null) {
1336                         method.ParameterInfo.CheckParameters (method);
1337
1338                         if ((method.ModFlags & Modifiers.ASYNC) != 0) {
1339                                 report.Error (1994, method.Location, "`{0}': The async modifier can only be used with methods that have a body",
1340                                         method.GetSignatureForError ());
1341                         }
1342                 } else {
1343                         if (current_container.Kind == MemberKind.Interface) {
1344                                 report.Error (531, method.Location, "`{0}': interface members cannot have a definition",
1345                                         method.GetSignatureForError ());
1346                         }
1347                 }
1348
1349                 current_local_parameters = null;
1350
1351                 if (doc_support)
1352                         Lexer.doc_state = XmlCommentState.Allowed;
1353           }
1354         ;
1355
1356 method_header
1357         : opt_attributes
1358           opt_modifiers
1359           member_type
1360           method_declaration_name OPEN_PARENS
1361           {
1362                 valid_param_mod = ParameterModifierType.All;
1363           }
1364           opt_formal_parameter_list CLOSE_PARENS
1365           {
1366                 valid_param_mod = 0;
1367                 MemberName name = (MemberName) $4;
1368                 current_local_parameters = (ParametersCompiled) $7;
1369
1370                 var method = Method.Create (current_type, (FullNamedExpression) $3, (Modifiers) $2,
1371                                      name, current_local_parameters, (Attributes) $1);
1372
1373                 current_type.AddMember (method);
1374
1375                 async_block = (method.ModFlags & Modifiers.ASYNC) != 0;
1376
1377                 if (doc_support)
1378                         method.DocComment = Lexer.consume_doc_comment ();
1379
1380                 lbag.AddMember (method, mod_locations, GetLocation ($5), GetLocation ($8));
1381
1382                 $$ = method;
1383
1384                 lexer.ConstraintsParsing = true;
1385           }
1386           opt_type_parameter_constraints_clauses
1387           {
1388                 lexer.ConstraintsParsing = false;
1389
1390                 if ($10 != null) {
1391                         var method = (Method) $9;
1392                         method.SetConstraints ((List<Constraints>) $10);
1393                 }
1394
1395                 $$ = $9;
1396           }
1397         | opt_attributes
1398           opt_modifiers
1399           PARTIAL
1400           VOID
1401           {
1402                 lexer.parsing_generic_declaration = true;
1403           }
1404           method_declaration_name
1405           OPEN_PARENS
1406           {
1407                 lexer.parsing_generic_declaration = false;
1408                 valid_param_mod = ParameterModifierType.All;
1409           }
1410           opt_formal_parameter_list CLOSE_PARENS 
1411           {
1412                 lexer.ConstraintsParsing = true;
1413           }
1414           opt_type_parameter_constraints_clauses
1415           {
1416                 lexer.ConstraintsParsing = false;
1417                 valid_param_mod = 0;
1418
1419                 MemberName name = (MemberName) $6;
1420                 current_local_parameters = (ParametersCompiled) $9;
1421
1422                 var modifiers = (Modifiers) $2;
1423                 modifiers |= Modifiers.PARTIAL;
1424
1425                 var method = Method.Create (current_type, new TypeExpression (compiler.BuiltinTypes.Void, GetLocation ($4)),
1426                                      modifiers, name, current_local_parameters, (Attributes) $1);
1427
1428                 current_type.AddMember (method);
1429
1430                 async_block = (method.ModFlags & Modifiers.ASYNC) != 0;
1431
1432                 if ($12 != null)
1433                         method.SetConstraints ((List<Constraints>) $12);
1434
1435                 if (doc_support)
1436                         method.DocComment = Lexer.consume_doc_comment ();
1437
1438                 StoreModifierLocation (Modifiers.PARTIAL, GetLocation ($3));
1439                 lbag.AddMember (method, mod_locations, GetLocation ($7), GetLocation ($10));
1440                 $$ = method;
1441           }
1442         | opt_attributes
1443           opt_modifiers
1444           member_type
1445           modifiers method_declaration_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1446           {
1447                 MemberName name = (MemberName) $5;
1448                 report.Error (1585, name.Location, 
1449                         "Member modifier `{0}' must precede the member type and name", ModifiersExtensions.Name ((Modifiers) $4));
1450
1451                 var method = Method.Create (current_type, (FullNamedExpression) $3,
1452                                             0, name, (ParametersCompiled) $7, (Attributes) $1);
1453
1454                 current_type.AddMember (method);
1455
1456                 current_local_parameters = (ParametersCompiled) $7;
1457
1458                 if (doc_support)
1459                         method.DocComment = Lexer.consume_doc_comment ();
1460
1461                 $$ = method;
1462           }
1463         | opt_attributes
1464           opt_modifiers
1465           member_type
1466           method_declaration_name error
1467           {
1468                 Error_SyntaxError (yyToken);
1469                 current_local_parameters = ParametersCompiled.Undefined;
1470
1471                 MemberName name = (MemberName) $4;
1472                 var method = Method.Create (current_type, (FullNamedExpression) $3, (Modifiers) $2,
1473                                                                         name, current_local_parameters, (Attributes) $1);
1474
1475                 current_type.AddMember (method);
1476
1477                 if (doc_support)
1478                         method.DocComment = Lexer.consume_doc_comment ();
1479
1480                 $$ = method;
1481           }
1482         ;
1483
1484 method_body_expression_block
1485         : method_body
1486         | expression_block
1487         ;
1488
1489 method_body
1490         : block
1491         | SEMICOLON             { $$ = null; }
1492         ;
1493
1494 expression_block
1495         : ARROW
1496          {
1497                 if (lang_version < LanguageVersion.V_6) {
1498                         FeatureIsNotAvailable (GetLocation ($1), "expression bodied members");
1499                 }
1500
1501                 ++lexer.parsing_block;
1502                 start_block (GetLocation ($1));
1503          }
1504          expression SEMICOLON
1505          {
1506                 lexer.parsing_block = 0;
1507                 current_block.AddStatement (new ContextualReturn ((Expression) $3));
1508                 var b = end_block (GetLocation ($4));
1509                 b.IsCompilerGenerated = true;
1510                 $$ = b;
1511          }
1512         ;
1513
1514 opt_formal_parameter_list
1515         : /* empty */                   { $$ = ParametersCompiled.EmptyReadOnlyParameters; }
1516         | formal_parameter_list
1517         ;
1518         
1519 formal_parameter_list
1520         : fixed_parameters
1521           {
1522                 var pars_list = (List<Parameter>) $1;
1523                 $$ = new ParametersCompiled (pars_list.ToArray ());
1524           } 
1525         | fixed_parameters COMMA parameter_array
1526           {
1527                 var pars_list = (List<Parameter>) $1;
1528                 pars_list.Add ((Parameter) $3);
1529
1530                 $$ = new ParametersCompiled (pars_list.ToArray ()); 
1531           }
1532         | fixed_parameters COMMA arglist_modifier
1533           {
1534                 var pars_list = (List<Parameter>) $1;
1535                 pars_list.Add (new ArglistParameter (GetLocation ($3)));
1536                 $$ = new ParametersCompiled (pars_list.ToArray (), true);
1537           }
1538         | parameter_array COMMA error
1539           {
1540                 if ($1 != null)
1541                         report.Error (231, ((Parameter) $1).Location, "A params parameter must be the last parameter in a formal parameter list");
1542
1543                 $$ = new ParametersCompiled (new Parameter[] { (Parameter) $1 } );                      
1544           }
1545         | fixed_parameters COMMA parameter_array COMMA error
1546           {
1547                 if ($3 != null)
1548                         report.Error (231, ((Parameter) $3).Location, "A params parameter must be the last parameter in a formal parameter list");
1549
1550                 var pars_list = (List<Parameter>) $1;
1551                 pars_list.Add (new ArglistParameter (GetLocation ($3)));
1552
1553                 $$ = new ParametersCompiled (pars_list.ToArray (), true);
1554           }
1555         | arglist_modifier COMMA error
1556           {
1557                 report.Error (257, GetLocation ($1), "An __arglist parameter must be the last parameter in a formal parameter list");
1558
1559                 $$ = new ParametersCompiled (new Parameter [] { new ArglistParameter (GetLocation ($1)) }, true);
1560           }
1561         | fixed_parameters COMMA ARGLIST COMMA error 
1562           {
1563                 report.Error (257, GetLocation ($3), "An __arglist parameter must be the last parameter in a formal parameter list");
1564
1565                 var pars_list = (List<Parameter>) $1;
1566                 pars_list.Add (new ArglistParameter (GetLocation ($3)));
1567
1568                 $$ = new ParametersCompiled (pars_list.ToArray (), true);
1569           }
1570         | parameter_array 
1571           {
1572                 $$ = new ParametersCompiled (new Parameter[] { (Parameter) $1 } );
1573           }
1574         | arglist_modifier
1575           {
1576                 $$ = new ParametersCompiled (new Parameter [] { new ArglistParameter (GetLocation ($1)) }, true);
1577           }
1578         | error
1579           {
1580                 Error_SyntaxError (yyToken);
1581                 $$ = ParametersCompiled.EmptyReadOnlyParameters;
1582           }
1583         ;
1584
1585 fixed_parameters
1586         : fixed_parameter       
1587           {
1588                 parameters_bucket.Clear ();
1589                 Parameter p = (Parameter) $1;
1590                 parameters_bucket.Add (p);
1591                 
1592                 default_parameter_used = p.HasDefaultValue;
1593                 $$ = parameters_bucket;
1594           }
1595         | fixed_parameters COMMA fixed_parameter
1596           {
1597                 var pars = (List<Parameter>) $1;
1598                 Parameter p = (Parameter) $3;
1599                 if (p != null) {
1600                         if (p.HasExtensionMethodModifier)
1601                                 report.Error (1100, p.Location, "The parameter modifier `this' can only be used on the first parameter");
1602                         else if (!p.HasDefaultValue && default_parameter_used)
1603                                 report.Error (1737, p.Location, "Optional parameter cannot precede required parameters");
1604
1605                         default_parameter_used |= p.HasDefaultValue;
1606                         pars.Add (p);
1607                         
1608                         lbag.AddLocation (p, GetLocation ($2));
1609                 }
1610                 
1611                 $$ = $1;
1612           }
1613         ;
1614
1615 fixed_parameter
1616         : opt_attributes
1617           opt_parameter_modifier
1618           parameter_type
1619           identifier_inside_body
1620           {
1621                 var lt = (LocatedToken) $4;
1622                 $$ = new Parameter ((FullNamedExpression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1623           }
1624         | opt_attributes
1625           opt_parameter_modifier
1626           parameter_type
1627           identifier_inside_body OPEN_BRACKET CLOSE_BRACKET
1628           {
1629                 var lt = (LocatedToken) $4;
1630                 report.Error (1552, lt.Location, "Array type specifier, [], must appear before parameter name");
1631                 $$ = new Parameter ((FullNamedExpression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1632           }
1633         | attribute_sections error
1634           {
1635                 Error_SyntaxError (yyToken);
1636                 Location l = GetLocation ($2);
1637                 $$ = new Parameter (null, null, Parameter.Modifier.NONE, (Attributes) $1, l);
1638           }
1639         | opt_attributes
1640           opt_parameter_modifier
1641           parameter_type
1642           error
1643           {
1644                 Error_SyntaxError (yyToken);
1645                 Location l = GetLocation ($4);
1646                 $$ = new Parameter ((FullNamedExpression) $3, null, (Parameter.Modifier) $2, (Attributes) $1, l);
1647           }
1648         | opt_attributes
1649           opt_parameter_modifier
1650           parameter_type
1651           identifier_inside_body
1652           ASSIGN
1653           {
1654                 ++lexer.parsing_block;
1655           }
1656           constant_expression
1657           {
1658                 --lexer.parsing_block;
1659                 if (lang_version <= LanguageVersion.V_3) {
1660                         FeatureIsNotAvailable (GetLocation ($5), "optional parameter");
1661                 }
1662                 
1663                 Parameter.Modifier mod = (Parameter.Modifier) $2;
1664                 if (mod != Parameter.Modifier.NONE) {
1665                         switch (mod) {
1666                         case Parameter.Modifier.REF:
1667                         case Parameter.Modifier.OUT:
1668                                 report.Error (1741, GetLocation ($2), "Cannot specify a default value for the `{0}' parameter",
1669                                         Parameter.GetModifierSignature (mod));
1670                                 break;
1671                                 
1672                         case Parameter.Modifier.This:
1673                                 report.Error (1743, GetLocation ($2), "Cannot specify a default value for the `{0}' parameter",
1674                                         Parameter.GetModifierSignature (mod));
1675                                 break;
1676                         default:
1677                                 throw new NotImplementedException (mod.ToString ());
1678                         }
1679                                 
1680                         mod = Parameter.Modifier.NONE;
1681                 }
1682                 
1683                 if ((valid_param_mod & ParameterModifierType.DefaultValue) == 0)
1684                         report.Error (1065, GetLocation ($5), "Optional parameter is not valid in this context");
1685                 
1686                 var lt = (LocatedToken) $4;
1687                 $$ = new Parameter ((FullNamedExpression) $3, lt.Value, mod, (Attributes) $1, lt.Location);
1688                 lbag.AddLocation ($$, GetLocation ($5));
1689                 
1690                 if ($7 != null)
1691                         ((Parameter) $$).DefaultValue = new DefaultParameterValueExpression ((Expression) $7);
1692           }
1693         ;
1694
1695 opt_parameter_modifier
1696         : /* empty */           { $$ = Parameter.Modifier.NONE; }
1697         | parameter_modifiers
1698         ;
1699
1700 parameter_modifiers
1701         : parameter_modifier
1702           {
1703                 $$ = $1;
1704           }
1705         | parameter_modifiers parameter_modifier
1706           {
1707                 Parameter.Modifier p2 = (Parameter.Modifier)$2;
1708                 Parameter.Modifier mod = (Parameter.Modifier)$1 | p2;
1709                 if (((Parameter.Modifier)$1 & p2) == p2) {
1710                         Error_DuplicateParameterModifier (lexer.Location, p2);
1711                 } else {
1712                         switch (mod & ~Parameter.Modifier.This) {
1713                                 case Parameter.Modifier.REF:
1714                                         report.Error (1101, lexer.Location, "The parameter modifiers `this' and `ref' cannot be used altogether");
1715                                         break;
1716                                 case Parameter.Modifier.OUT:
1717                                         report.Error (1102, lexer.Location, "The parameter modifiers `this' and `out' cannot be used altogether");
1718                                         break;
1719                                 default:
1720                                         report.Error (1108, lexer.Location, "A parameter cannot have specified more than one modifier");
1721                                         break;
1722                         }
1723                 }
1724                 $$ = mod;
1725           }
1726         ;
1727
1728 parameter_modifier
1729         : REF
1730           {
1731                 if ((valid_param_mod & ParameterModifierType.Ref) == 0)
1732                         Error_ParameterModifierNotValid ("ref", GetLocation ($1));
1733                         
1734                 $$ = Parameter.Modifier.REF;
1735           }
1736         | OUT
1737           {
1738                 if ((valid_param_mod & ParameterModifierType.Out) == 0)
1739                         Error_ParameterModifierNotValid ("out", GetLocation ($1));
1740           
1741                 $$ = Parameter.Modifier.OUT;
1742           }
1743         | THIS
1744           {
1745                 if ((valid_param_mod & ParameterModifierType.This) == 0)
1746                         Error_ParameterModifierNotValid ("this", GetLocation ($1));
1747
1748                 if (lang_version <= LanguageVersion.ISO_2)
1749                         FeatureIsNotAvailable (GetLocation ($1), "extension methods");
1750                                 
1751                 $$ = Parameter.Modifier.This;
1752           }
1753         ;
1754
1755 parameter_array
1756         : opt_attributes params_modifier type IDENTIFIER
1757           {
1758                 var lt = (LocatedToken) $4;
1759                 $$ = new ParamsParameter ((FullNamedExpression) $3, lt.Value, (Attributes) $1, lt.Location);
1760           }
1761         | opt_attributes params_modifier type IDENTIFIER ASSIGN constant_expression
1762           {
1763                 report.Error (1751, GetLocation ($2), "Cannot specify a default value for a parameter array");
1764                 
1765                 var lt = (LocatedToken) $4;
1766                 $$ = new ParamsParameter ((FullNamedExpression) $3, lt.Value, (Attributes) $1, lt.Location);            
1767           }
1768         | opt_attributes params_modifier type error
1769           {
1770                 Error_SyntaxError (yyToken);
1771
1772                 $$ = new ParamsParameter ((FullNamedExpression) $3, null, (Attributes) $1, Location.Null);
1773           }
1774         ;
1775         
1776 params_modifier
1777         : PARAMS
1778           {
1779                 if ((valid_param_mod & ParameterModifierType.Params) == 0)
1780                         report.Error (1670, (GetLocation ($1)), "The `params' modifier is not allowed in current context");
1781           }
1782         | PARAMS parameter_modifier
1783           {
1784                 Parameter.Modifier mod = (Parameter.Modifier)$2;
1785                 if ((mod & Parameter.Modifier.This) != 0) {
1786                         report.Error (1104, GetLocation ($1), "The parameter modifiers `this' and `params' cannot be used altogether");
1787                 } else {
1788                         report.Error (1611, GetLocation ($1), "The params parameter cannot be declared as ref or out");
1789                 }         
1790           }
1791         | PARAMS params_modifier
1792           {
1793                 Error_DuplicateParameterModifier (GetLocation ($1), Parameter.Modifier.PARAMS);
1794           }
1795         ;
1796         
1797 arglist_modifier
1798         : ARGLIST
1799           {
1800                 if ((valid_param_mod & ParameterModifierType.Arglist) == 0)
1801                         report.Error (1669, GetLocation ($1), "__arglist is not valid in this context");
1802           }
1803         ;
1804
1805 property_declaration
1806         : opt_attributes
1807           opt_modifiers
1808           member_type
1809           member_declaration_name
1810           {
1811                 lexer.parsing_generic_declaration = false;
1812                 if (doc_support)
1813                         tmpComment = Lexer.consume_doc_comment ();
1814           }
1815           OPEN_BRACE
1816           {
1817                 var type = (FullNamedExpression) $3;
1818                 current_property = new Property (current_type, type, (Modifiers) $2,
1819                         (MemberName) $4, (Attributes) $1);
1820                         
1821                 if (type.Type != null && type.Type.Kind == MemberKind.Void)
1822                         report.Error (547, GetLocation ($3), "`{0}': property or indexer cannot have void type", current_property.GetSignatureForError ());                                     
1823                         
1824                 current_type.AddMember (current_property);
1825                 lbag.AddMember (current_property, mod_locations, GetLocation ($6));
1826                 
1827                 lexer.PropertyParsing = true;
1828           }
1829           accessor_declarations 
1830           {
1831                 lexer.PropertyParsing = false;
1832
1833                 if (doc_support)
1834                         current_property.DocComment = ConsumeStoredComment ();
1835           }
1836           CLOSE_BRACE
1837           {
1838                 lbag.AppendToMember (current_property, GetLocation ($10));
1839                 lexer.parsing_modifiers = true;
1840           }
1841           opt_property_initializer
1842           {
1843                 current_property = null;
1844           }
1845         | opt_attributes
1846           opt_modifiers
1847           member_type
1848           member_declaration_name
1849           {
1850                 lexer.parsing_generic_declaration = false;
1851                 if (doc_support)
1852                         tmpComment = Lexer.consume_doc_comment ();
1853                 current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters;
1854           }
1855           expression_block
1856           {
1857                 var type = (FullNamedExpression) $3;
1858                 var property = new Property (current_type, type, (Modifiers) $2,
1859                         (MemberName) $4, (Attributes) $1);
1860
1861                 property.Get = new Property.GetMethod (property, Modifiers.COMPILER_GENERATED, null, property.Location);
1862                 property.Get.Block = (ToplevelBlock) $6;
1863
1864                 if (current_container.Kind == MemberKind.Interface) {
1865                         report.Error (531, property.Get.Block.StartLocation,
1866                                 "`{0}': interface members cannot have a definition", property.GetSignatureForError ());
1867                 }
1868
1869                 if (type.Type != null && type.Type.Kind == MemberKind.Void)
1870                         report.Error (547, GetLocation ($3), "`{0}': property or indexer cannot have void type", property.GetSignatureForError ());
1871
1872                 if (doc_support)
1873                         property.DocComment = ConsumeStoredComment ();
1874
1875                 current_type.AddMember (property);
1876
1877                 current_local_parameters = null;
1878           }
1879         ;
1880
1881 opt_property_initializer
1882         : /* empty */
1883         | ASSIGN
1884           {
1885                 ++lexer.parsing_block;
1886                 current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters;
1887                 start_block (GetLocation ($1));
1888           }
1889           property_initializer SEMICOLON
1890           {
1891                 --lexer.parsing_block;
1892                 ((Property)current_property).Initializer = (Expression) $3;
1893                 lbag.AppendToMember (current_property, GetLocation ($1), GetLocation ($4));
1894                 end_block (GetLocation ($4));
1895                 current_local_parameters = null;
1896
1897                 if (doc_support)
1898                         Lexer.doc_state = XmlCommentState.Allowed;
1899           }
1900         ;
1901
1902 property_initializer
1903         : expression
1904         | array_initializer
1905         ;
1906
1907 indexer_declaration
1908         : opt_attributes opt_modifiers
1909           member_type indexer_declaration_name OPEN_BRACKET
1910           {
1911                 valid_param_mod = ParameterModifierType.Params | ParameterModifierType.DefaultValue;
1912           }
1913           opt_formal_parameter_list CLOSE_BRACKET 
1914           {
1915                 valid_param_mod = 0;
1916                 var type = (FullNamedExpression) $3;
1917                 Indexer indexer = new Indexer (current_type, type, (MemberName) $4, (Modifiers) $2, (ParametersCompiled) $7, (Attributes) $1);
1918                         
1919                 current_property = indexer;
1920
1921                 current_type.AddIndexer (indexer);
1922                 lbag.AddMember (current_property, mod_locations, GetLocation ($5), GetLocation ($8));
1923                 
1924                 if (type.Type != null && type.Type.Kind == MemberKind.Void)
1925                         report.Error (620, GetLocation ($3), "`{0}': indexer return type cannot be `void'", indexer.GetSignatureForError ());           
1926
1927                 if (indexer.ParameterInfo.IsEmpty) {
1928                         report.Error (1551, GetLocation ($5), "Indexers must have at least one parameter");
1929                 }
1930
1931                 if (doc_support) {
1932                         tmpComment = Lexer.consume_doc_comment ();
1933                         Lexer.doc_state = XmlCommentState.Allowed;
1934                 }
1935
1936                 lexer.PropertyParsing = true;
1937                 current_local_parameters = (ParametersCompiled) $7;
1938           }
1939           indexer_body
1940           {
1941                 lexer.PropertyParsing = false;
1942                 current_local_parameters = null;
1943
1944                 if (current_property.AccessorFirst != null && current_property.AccessorFirst.Block == null)
1945                         ((Indexer) current_property).ParameterInfo.CheckParameters (current_property);
1946           
1947                 if (doc_support)
1948                         current_property.DocComment = ConsumeStoredComment ();
1949                         
1950                 current_property = null;                
1951           }
1952         ;
1953
1954 indexer_body
1955         : OPEN_BRACE accessor_declarations CLOSE_BRACE
1956           {
1957                 lbag.AppendToMember (current_property, GetLocation ($1), GetLocation ($3));
1958           }
1959         | expression_block
1960           {
1961                 current_property.Get = new Indexer.GetIndexerMethod (current_property, Modifiers.COMPILER_GENERATED, current_local_parameters, null, current_property.Location);
1962                 current_property.Get.Block = (ToplevelBlock) $1;
1963           }
1964         ;
1965
1966 accessor_declarations
1967         : get_accessor_declaration
1968         | get_accessor_declaration accessor_declarations
1969         | set_accessor_declaration
1970         | set_accessor_declaration accessor_declarations
1971         | error
1972           {
1973                 if (yyToken == Token.CLOSE_BRACE) {
1974                         report.Error (548, lexer.Location, "`{0}': property or indexer must have at least one accessor", current_property.GetSignatureForError ());
1975                 } else {
1976                         if (yyToken == Token.SEMICOLON)
1977                                 report.Error (1597, lexer.Location, "Semicolon after method or accessor block is not valid");
1978                         else
1979                                 report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1980                 }
1981           }
1982         ;
1983
1984 get_accessor_declaration
1985         : opt_attributes opt_modifiers GET
1986           {
1987                 if ($2 != ModifierNone && lang_version == LanguageVersion.ISO_1) {
1988                         FeatureIsNotAvailable (GetLocation ($2), "access modifiers on properties");
1989                 }
1990           
1991                 if (current_property.Get != null) {
1992                         report.Error (1007, GetLocation ($3), "Property accessor already defined");
1993                 }
1994                 
1995                 if (current_property is Indexer) {
1996                         current_property.Get = new Indexer.GetIndexerMethod (current_property, (Modifiers) $2, ((Indexer)current_property).ParameterInfo.Clone (),
1997                                 (Attributes) $1, GetLocation ($3));
1998                 } else {
1999                         current_property.Get = new Property.GetMethod (current_property,
2000                                 (Modifiers) $2, (Attributes) $1, GetLocation ($3));
2001                 }       
2002           
2003                 current_local_parameters = current_property.Get.ParameterInfo;    
2004                 lbag.AddMember (current_property.Get, mod_locations);
2005                 lexer.PropertyParsing = false;
2006           }
2007           accessor_body
2008           {
2009                 if ($5 != null) {
2010                         current_property.Get.Block = (ToplevelBlock) $5;                        
2011                 
2012                         if (current_container.Kind == MemberKind.Interface) {
2013                                 report.Error (531, current_property.Get.Block.StartLocation,
2014                                         "`{0}': interface members cannot have a definition", current_property.Get.GetSignatureForError ());
2015                         }               
2016                 }
2017           
2018                 current_local_parameters = null;
2019                 lexer.PropertyParsing = true;
2020
2021                 if (doc_support)
2022                         if (Lexer.doc_state == XmlCommentState.Error)
2023                                 Lexer.doc_state = XmlCommentState.NotAllowed;
2024           }
2025         ;
2026
2027 set_accessor_declaration
2028         : opt_attributes opt_modifiers SET 
2029           {
2030                 if ($2 != ModifierNone && lang_version == LanguageVersion.ISO_1) {
2031                         FeatureIsNotAvailable (GetLocation ($2), "access modifiers on properties");
2032                 }
2033                 
2034                 if (current_property.Set != null) {
2035                         report.Error (1007, GetLocation ($3), "Property accessor already defined");
2036                 }
2037           
2038                 if (current_property is Indexer) {
2039                         current_property.Set = new Indexer.SetIndexerMethod (current_property, (Modifiers) $2,
2040                                 ParametersCompiled.MergeGenerated (compiler,
2041                                 ((Indexer)current_property).ParameterInfo, true, new Parameter (
2042                                         current_property.TypeExpression, "value", Parameter.Modifier.NONE, null, GetLocation ($3)),
2043                                         null),
2044                                 (Attributes) $1, GetLocation ($3));
2045                 } else {
2046                         current_property.Set = new Property.SetMethod (current_property, (Modifiers) $2, 
2047                                 ParametersCompiled.CreateImplicitParameter (current_property.TypeExpression, GetLocation ($3)),
2048                                 (Attributes) $1, GetLocation ($3));
2049                 }
2050                 
2051                 current_local_parameters = current_property.Set.ParameterInfo;  
2052                 lbag.AddMember (current_property.Set, mod_locations);
2053                 lexer.PropertyParsing = false;
2054           }
2055           accessor_body
2056           {
2057                 if ($5 != null) {               
2058                         current_property.Set.Block = (ToplevelBlock) $5;
2059                 
2060                         if (current_container.Kind == MemberKind.Interface) {
2061                                 report.Error (531, current_property.Set.Block.StartLocation,
2062                                         "`{0}': interface members cannot have a definition", current_property.Set.GetSignatureForError ());
2063                         }
2064                 }
2065                 
2066                 current_local_parameters = null;
2067                 lexer.PropertyParsing = true;
2068
2069                 if (doc_support
2070                         && Lexer.doc_state == XmlCommentState.Error)
2071                         Lexer.doc_state = XmlCommentState.NotAllowed;
2072           }
2073         ;
2074
2075 accessor_body
2076         : block 
2077         | SEMICOLON
2078           {
2079                 // TODO: lbag
2080                 $$ = null;
2081           }
2082         | error
2083           {
2084                 Error_SyntaxError (1043, yyToken, "Invalid accessor body");
2085                 $$ = null;
2086           }
2087         ;
2088
2089 interface_declaration
2090         : opt_attributes
2091           opt_modifiers
2092           opt_partial
2093           INTERFACE
2094           {
2095           }
2096           type_declaration_name
2097           {
2098                 lexer.ConstraintsParsing = true;
2099                 push_current_container (new Interface (current_container, (MemberName) $6, (Modifiers) $2, (Attributes) $1), $3);
2100                 lbag.AddMember (current_container, mod_locations, GetLocation ($4));            
2101           }
2102           opt_class_base
2103           opt_type_parameter_constraints_clauses
2104           {
2105                 lexer.ConstraintsParsing = false;
2106
2107                 if ($9 != null)
2108                         current_container.SetConstraints ((List<Constraints>) $9);
2109
2110                 if (doc_support) {
2111                         current_container.PartialContainer.DocComment = Lexer.consume_doc_comment ();
2112                         Lexer.doc_state = XmlCommentState.Allowed;
2113                 }
2114                 
2115                 lexer.parsing_modifiers = true;
2116           }
2117           OPEN_BRACE opt_interface_member_declarations CLOSE_BRACE
2118           {
2119                 --lexer.parsing_declaration;      
2120                 if (doc_support)
2121                         Lexer.doc_state = XmlCommentState.Allowed;
2122           }
2123           opt_semicolon 
2124           {
2125                 if ($15 == null) {
2126                         lbag.AppendToMember (current_container, GetLocation ($11), GetLocation ($13));
2127                 } else {
2128                         lbag.AppendToMember (current_container, GetLocation ($11), GetLocation ($13), GetLocation ($15));
2129                 }
2130                 $$ = pop_current_class ();
2131           }
2132         | opt_attributes opt_modifiers opt_partial INTERFACE error
2133           {
2134                 Error_SyntaxError (yyToken);      
2135           }
2136         ;
2137
2138 opt_interface_member_declarations
2139         : /* empty */
2140         | interface_member_declarations
2141         ;
2142
2143 interface_member_declarations
2144         : interface_member_declaration
2145           {
2146                 lexer.parsing_modifiers = true;
2147                 lexer.parsing_block = 0;
2148           }
2149         | interface_member_declarations interface_member_declaration
2150           {
2151                 lexer.parsing_modifiers = true;
2152                 lexer.parsing_block = 0;
2153           }
2154         ;
2155
2156 interface_member_declaration
2157         : constant_declaration
2158           {
2159                 report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
2160           }
2161         | field_declaration
2162           {
2163                 report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
2164           }
2165         | method_declaration
2166         | property_declaration
2167         | event_declaration
2168         | indexer_declaration
2169         | operator_declaration
2170           {
2171                 report.Error (567, GetLocation ($1), "Interfaces cannot contain operators");
2172           }
2173         | constructor_declaration
2174           {
2175                 report.Error (526, GetLocation ($1), "Interfaces cannot contain contructors");
2176           }
2177         | type_declaration
2178           {
2179                 report.Error (524, GetLocation ($1), "Interfaces cannot declare classes, structs, interfaces, delegates, or enumerations");
2180           }
2181         ;
2182
2183 operator_declaration
2184         : opt_attributes opt_modifiers operator_declarator 
2185           {
2186           }
2187           method_body_expression_block
2188           {
2189                 OperatorDeclaration decl = (OperatorDeclaration) $3;
2190                 if (decl != null) {
2191                         Operator op = new Operator (
2192                                 current_type, decl.optype, decl.ret_type, (Modifiers) $2, 
2193                                 current_local_parameters,
2194                                 (ToplevelBlock) $5, (Attributes) $1, decl.location);
2195                                 
2196                         if (op.Block == null)
2197                                 op.ParameterInfo.CheckParameters (op);
2198
2199                         if (doc_support) {
2200                                 op.DocComment = tmpComment;
2201                                 Lexer.doc_state = XmlCommentState.Allowed;
2202                         }
2203
2204                         // Note again, checking is done in semantic analysis
2205                         current_type.AddOperator (op);
2206
2207                         lbag.AddMember (op, mod_locations, lbag.GetLocations (decl));
2208                 }
2209                 
2210                 current_local_parameters = null;
2211           }
2212         ;
2213
2214 operator_type
2215         : type_expression_or_array
2216         | VOID
2217           {
2218                 report.Error (590, GetLocation ($1), "User-defined operators cannot return void");
2219                 $$ = new TypeExpression (compiler.BuiltinTypes.Void, GetLocation ($1));
2220           }
2221         ;
2222
2223 operator_declarator
2224         : operator_type OPERATOR overloadable_operator OPEN_PARENS
2225           {
2226                 valid_param_mod = ParameterModifierType.DefaultValue;
2227                 if ((Operator.OpType) $3 == Operator.OpType.Is)
2228                         valid_param_mod |= ParameterModifierType.Out;
2229           }
2230           opt_formal_parameter_list CLOSE_PARENS
2231           {
2232                 valid_param_mod = 0;
2233
2234                 Location loc = GetLocation ($2);
2235                 Operator.OpType op = (Operator.OpType) $3;
2236                 current_local_parameters = (ParametersCompiled)$6;
2237                 
2238                 int p_count = current_local_parameters.Count;
2239                 if (p_count == 1) {
2240                         if (op == Operator.OpType.Addition)
2241                                 op = Operator.OpType.UnaryPlus;
2242                         else if (op == Operator.OpType.Subtraction)
2243                                 op = Operator.OpType.UnaryNegation;
2244                 }
2245                 
2246                 if (IsUnaryOperator (op)) {
2247                         if (p_count == 2) {
2248                                 report.Error (1020, loc, "Overloadable binary operator expected");
2249                         } else if (p_count != 1) {
2250                                 report.Error (1535, loc, "Overloaded unary operator `{0}' takes one parameter",
2251                                         Operator.GetName (op));
2252                         }
2253                 } else if (op == Operator.OpType.Is) {
2254                         // TODO: Special checks for is operator
2255                 } else {
2256                         if (p_count == 1) {
2257                                 report.Error (1019, loc, "Overloadable unary operator expected");
2258                         } else if (p_count != 2) {
2259                                 report.Error (1534, loc, "Overloaded binary operator `{0}' takes two parameters",
2260                                         Operator.GetName (op));
2261                         }
2262                 }
2263                 
2264                 if (doc_support) {
2265                         tmpComment = Lexer.consume_doc_comment ();
2266                         Lexer.doc_state = XmlCommentState.NotAllowed;
2267                 }
2268
2269                 $$ = new OperatorDeclaration (op, (FullNamedExpression) $1, loc);
2270                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($3), GetLocation ($4), GetLocation ($7));
2271           }
2272         | conversion_operator_declarator
2273         ;
2274
2275 overloadable_operator
2276 // Unary operators:
2277         : BANG   { $$ = Operator.OpType.LogicalNot; }
2278         | TILDE  { $$ = Operator.OpType.OnesComplement; }  
2279         | OP_INC { $$ = Operator.OpType.Increment; }
2280         | OP_DEC { $$ = Operator.OpType.Decrement; }
2281         | TRUE   { $$ = Operator.OpType.True; }
2282         | FALSE  { $$ = Operator.OpType.False; }
2283 // Unary and binary:
2284         | PLUS { $$ = Operator.OpType.Addition; }
2285         | MINUS { $$ = Operator.OpType.Subtraction; }
2286 // Binary:
2287         | STAR { $$ = Operator.OpType.Multiply; }
2288         | DIV {  $$ = Operator.OpType.Division; }
2289         | PERCENT { $$ = Operator.OpType.Modulus; }
2290         | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
2291         | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
2292         | CARRET { $$ = Operator.OpType.ExclusiveOr; }
2293         | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
2294         | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
2295         | OP_EQ { $$ = Operator.OpType.Equality; }
2296         | OP_NE { $$ = Operator.OpType.Inequality; }
2297         | OP_GT { $$ = Operator.OpType.GreaterThan; }
2298         | OP_LT { $$ = Operator.OpType.LessThan; }
2299         | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
2300         | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
2301         | IS
2302           {
2303                 if (lang_version != LanguageVersion.Experimental)
2304                         FeatureIsNotAvailable (GetLocation ($1), "is user operator");
2305
2306                 $$ = Operator.OpType.Is;
2307           }
2308         ;
2309
2310 conversion_operator_declarator
2311         : IMPLICIT OPERATOR type OPEN_PARENS
2312           {
2313                 valid_param_mod = ParameterModifierType.DefaultValue;
2314           }
2315           opt_formal_parameter_list CLOSE_PARENS
2316           {
2317                 valid_param_mod = 0;
2318
2319                 Location loc = GetLocation ($2);
2320                 current_local_parameters = (ParametersCompiled)$6;  
2321
2322                 if (current_local_parameters.Count != 1) {
2323                         report.Error (1535, loc, "Overloaded unary operator `implicit' takes one parameter");
2324                 }
2325
2326                 if (doc_support) {
2327                         tmpComment = Lexer.consume_doc_comment ();
2328                         Lexer.doc_state = XmlCommentState.NotAllowed;
2329                 }
2330
2331                 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (FullNamedExpression) $3, loc);
2332                 lbag.AddLocation ($$, GetLocation ($1), GetLocation ($2), GetLocation ($4), GetLocation ($7));
2333           }
2334         | EXPLICIT OPERATOR type OPEN_PARENS
2335           {
2336                 valid_param_mod = ParameterModifierType.DefaultValue;
2337           }
2338           opt_formal_parameter_list CLOSE_PARENS
2339           {
2340                 valid_param_mod = 0;
2341                 
2342                 Location loc = GetLocation ($2);
2343                 current_local_parameters = (ParametersCompiled)$6;  
2344
2345                 if (current_local_parameters.Count != 1) {
2346                         report.Error (1535, loc, "Overloaded unary operator `explicit' takes one parameter");
2347                 }
2348
2349                 if (doc_support) {
2350                         tmpComment = Lexer.consume_doc_comment ();
2351                         Lexer.doc_state = XmlCommentState.NotAllowed;
2352                 }
2353
2354                 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (FullNamedExpression) $3, loc);
2355                 lbag.AddLocation ($$, GetLocation ($1), GetLocation ($2), GetLocation ($4), GetLocation ($7));
2356           }
2357         | IMPLICIT error 
2358           {
2359                 Error_SyntaxError (yyToken);
2360                 current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters;
2361                 $$ = new OperatorDeclaration (Operator.OpType.Implicit, null, GetLocation ($1));
2362           }
2363         | EXPLICIT error 
2364           {
2365                 Error_SyntaxError (yyToken);
2366                 current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters;
2367                 $$ = new OperatorDeclaration (Operator.OpType.Explicit, null, GetLocation ($1));
2368           }
2369         ;
2370
2371 constructor_declaration
2372         : constructor_declarator
2373           constructor_body
2374           { 
2375                 Constructor c = (Constructor) $1;
2376                 c.Block = (ToplevelBlock) $2;
2377                 
2378                 if (doc_support)
2379                         c.DocComment = ConsumeStoredComment ();
2380
2381                 current_local_parameters = null;
2382                 if (doc_support)
2383                         Lexer.doc_state = XmlCommentState.Allowed;
2384           }
2385         ;
2386
2387 constructor_declarator
2388         : opt_attributes
2389           opt_modifiers
2390           IDENTIFIER
2391           {
2392                 if (doc_support) {
2393                         tmpComment = Lexer.consume_doc_comment ();
2394                         Lexer.doc_state = XmlCommentState.Allowed;
2395                 }
2396                 
2397                 valid_param_mod = ParameterModifierType.All;
2398           }
2399           OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2400           {
2401                 valid_param_mod = 0;
2402                 current_local_parameters = (ParametersCompiled) $6;
2403                 
2404                 var lt = (LocatedToken) $3;
2405                 var mods = (Modifiers) $2;
2406                 var c = new Constructor (current_type, lt.Value, mods, (Attributes) $1, current_local_parameters, lt.Location);
2407
2408                 if (lt.Value != current_container.MemberName.Name) {
2409                         report.Error (1520, c.Location, "Class, struct, or interface method must have a return type");
2410                 } else if ((mods & Modifiers.STATIC) != 0) {
2411                         if (!current_local_parameters.IsEmpty) {
2412                                 report.Error (132, c.Location, "`{0}': The static constructor must be parameterless",
2413                                         c.GetSignatureForError ());
2414                         }
2415
2416                         if ((mods & Modifiers.AccessibilityMask) != 0){
2417                                 report.Error (515, c.Location,
2418                                         "`{0}': static constructor cannot have an access modifier",
2419                                         c.GetSignatureForError ());
2420                         }
2421                 } else {
2422                         if (current_type.Kind == MemberKind.Struct && current_local_parameters.IsEmpty) {
2423                                 report.Error (568, c.Location, "Structs cannot contain explicit parameterless constructors");
2424                         }
2425                 }
2426
2427                 current_type.AddConstructor (c);
2428                 lbag.AddMember (c, mod_locations, GetLocation ($5), GetLocation ($7));
2429                 $$ = c;
2430
2431                 //
2432                 // start block here, so possible anonymous methods inside
2433                 // constructor initializer can get correct parent block
2434                 //
2435                 start_block (lexer.Location);
2436           }
2437           opt_constructor_initializer
2438           {
2439                 if ($9 != null) {
2440                         var c = (Constructor) $8;
2441                         c.Initializer = (ConstructorInitializer) $9;
2442                         
2443                         if (c.IsStatic) {
2444                                 report.Error (514, c.Location,
2445                                         "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2446                                         c.GetSignatureForError ());
2447                         }
2448                 }
2449
2450                 $$ = $8;
2451           }
2452         ;
2453
2454 constructor_body
2455         : block_prepared
2456         | SEMICOLON             { current_block = null; $$ = null; }
2457         ;
2458
2459 opt_constructor_initializer
2460         : /* Empty */
2461         | constructor_initializer
2462         ;
2463
2464 constructor_initializer
2465         : COLON BASE OPEN_PARENS
2466           {
2467                 ++lexer.parsing_block;
2468           }
2469           opt_argument_list CLOSE_PARENS
2470           {
2471                 --lexer.parsing_block;
2472                 $$ = new ConstructorBaseInitializer ((Arguments) $5, GetLocation ($2));
2473                 lbag.AddLocation ($$, GetLocation ($1), GetLocation ($3), GetLocation ($6));
2474           }
2475         | COLON THIS OPEN_PARENS
2476           {
2477                 ++lexer.parsing_block;
2478           }
2479           opt_argument_list CLOSE_PARENS
2480           {
2481                 --lexer.parsing_block;
2482                 $$ = new ConstructorThisInitializer ((Arguments) $5, GetLocation ($2));
2483                 lbag.AddLocation ($$, GetLocation ($1), GetLocation ($3), GetLocation ($6));
2484           }
2485         | COLON error
2486           {
2487                 Error_SyntaxError (yyToken);      
2488                 $$ = new ConstructorThisInitializer (null, GetLocation ($2));
2489                 lbag.AddLocation ($$, GetLocation ($1));
2490           }
2491         | error
2492           {
2493                 Error_SyntaxError (yyToken);
2494                 $$ = null;
2495           }
2496         ;
2497
2498 destructor_declaration
2499         : opt_attributes opt_modifiers TILDE 
2500           {
2501                 if (doc_support) {
2502                         tmpComment = Lexer.consume_doc_comment ();
2503                         Lexer.doc_state = XmlCommentState.NotAllowed;
2504                 }
2505                 
2506                 current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters;
2507           }
2508           IDENTIFIER OPEN_PARENS CLOSE_PARENS method_body
2509           {
2510                 var lt = (LocatedToken) $5;
2511                 if (lt.Value != current_container.MemberName.Name){
2512                         report.Error (574, lt.Location, "Name of destructor must match name of class");
2513                 } else if (current_container.Kind != MemberKind.Class){
2514                         report.Error (575, lt.Location, "Only class types can contain destructor");
2515                 }
2516                 
2517                 Destructor d = new Destructor (current_type, (Modifiers) $2,
2518                         ParametersCompiled.EmptyReadOnlyParameters, (Attributes) $1, lt.Location);
2519                 if (doc_support)
2520                         d.DocComment = ConsumeStoredComment ();
2521                   
2522                 d.Block = (ToplevelBlock) $8;
2523                 current_type.AddMember (d);
2524                 lbag.AddMember (d, mod_locations, GetLocation ($3), GetLocation ($6), GetLocation ($7));
2525
2526                 current_local_parameters = null;
2527           }
2528         ;
2529
2530 event_declaration
2531         : opt_attributes
2532           opt_modifiers
2533           EVENT type member_declaration_name
2534           {
2535                 current_event_field = new EventField (current_type, (FullNamedExpression) $4, (Modifiers) $2, (MemberName) $5, (Attributes) $1);
2536                 current_type.AddMember (current_event_field);
2537                 
2538                 if (current_event_field.MemberName.ExplicitInterface != null) {
2539                         report.Error (71, current_event_field.Location, "`{0}': An explicit interface implementation of an event must use property syntax",
2540                         current_event_field.GetSignatureForError ());
2541                 }
2542                 
2543                 $$ = current_event_field;
2544           }
2545           opt_event_initializer
2546           opt_event_declarators
2547           SEMICOLON
2548           {
2549                 if (doc_support) {
2550                         current_event_field.DocComment = Lexer.consume_doc_comment ();
2551                         Lexer.doc_state = XmlCommentState.Allowed;
2552                 }
2553                 
2554                 lbag.AddMember (current_event_field, mod_locations, GetLocation ($3), GetLocation ($9));
2555                 current_event_field = null;
2556           }
2557         | opt_attributes
2558           opt_modifiers
2559           EVENT type member_declaration_name
2560           OPEN_BRACE
2561           {
2562                 current_event = new EventProperty (current_type, (FullNamedExpression) $4, (Modifiers) $2, (MemberName) $5, (Attributes) $1);
2563                 current_type.AddMember (current_event);
2564                 lbag.AddMember (current_event, mod_locations, GetLocation ($3), GetLocation ($6));
2565                 
2566                 lexer.EventParsing = true;
2567           }
2568           event_accessor_declarations
2569           {
2570                 if (current_container.Kind == MemberKind.Interface)
2571                         report.Error (69, GetLocation ($6), "Event in interface cannot have add or remove accessors");
2572           
2573                 lexer.EventParsing = false;
2574           }
2575           CLOSE_BRACE
2576           {
2577                 if (doc_support) {
2578                         current_event.DocComment = Lexer.consume_doc_comment ();
2579                         Lexer.doc_state = XmlCommentState.Allowed;
2580                 }
2581                 
2582                 lbag.AppendToMember (current_event, GetLocation ($9));
2583                 current_event = null;   
2584                 current_local_parameters = null;
2585           }
2586         | opt_attributes
2587           opt_modifiers
2588           EVENT type error
2589           {
2590                 Error_SyntaxError (yyToken);
2591
2592                 current_type.AddMember (new EventField (current_type, (FullNamedExpression) $4, (Modifiers) $2, MemberName.Null, (Attributes) $1));
2593           }
2594         ;
2595         
2596 opt_event_initializer
2597         : /* empty */
2598         | ASSIGN
2599           {
2600                 ++lexer.parsing_block;
2601           }
2602           event_variable_initializer
2603           {
2604                 --lexer.parsing_block;
2605                 current_event_field.Initializer = (Expression) $3;
2606           }
2607         ;
2608         
2609 opt_event_declarators
2610         : /* empty */
2611         | event_declarators
2612         ;
2613         
2614 event_declarators
2615         : event_declarator
2616           {
2617                 current_event_field.AddDeclarator ((FieldDeclarator) $1);
2618           }
2619         | event_declarators event_declarator
2620           {
2621                 current_event_field.AddDeclarator ((FieldDeclarator) $2);
2622           }
2623         ;
2624         
2625 event_declarator
2626         : COMMA IDENTIFIER
2627           {
2628                 var lt = (LocatedToken) $2;
2629                 $$ = new FieldDeclarator (new SimpleMemberName (lt.Value, lt.Location), null);
2630                 lbag.AddLocation ($$, GetLocation ($1));
2631           }
2632         | COMMA IDENTIFIER ASSIGN
2633           {
2634                 ++lexer.parsing_block;
2635           }
2636           event_variable_initializer
2637           {
2638                 --lexer.parsing_block;
2639                 var lt = (LocatedToken) $2;       
2640                 $$ = new FieldDeclarator (new SimpleMemberName (lt.Value, lt.Location), (Expression) $5);
2641                 lbag.AddLocation ($$, GetLocation ($1), GetLocation ($3));
2642           }
2643         ;
2644         
2645 event_variable_initializer
2646         : {
2647                 if (current_container.Kind == MemberKind.Interface) {
2648                         report.Error (68, lexer.Location, "`{0}': event in interface cannot have an initializer",
2649                                 current_event_field.GetSignatureForError ());
2650                 }
2651                 
2652                 if ((current_event_field.ModFlags & Modifiers.ABSTRACT) != 0) {
2653                         report.Error (74, lexer.Location, "`{0}': abstract event cannot have an initializer",
2654                                 current_event_field.GetSignatureForError ());
2655                 }               
2656           }
2657           variable_initializer
2658           {
2659                 $$ = $2;
2660           }
2661         ;
2662         
2663 event_accessor_declarations
2664         : add_accessor_declaration remove_accessor_declaration
2665         | remove_accessor_declaration add_accessor_declaration
2666         | add_accessor_declaration
2667           {
2668                 report.Error (65, lexer.Location, "`{0}': event property must have both add and remove accessors",
2669                         current_event.GetSignatureForError ());
2670           } 
2671         | remove_accessor_declaration
2672           {
2673                 report.Error (65, lexer.Location, "`{0}': event property must have both add and remove accessors",
2674                         current_event.GetSignatureForError ());
2675           }     
2676         | error
2677           { 
2678                 report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2679                 $$ = null;
2680           }
2681         ;
2682
2683 add_accessor_declaration
2684         : opt_attributes opt_modifiers ADD
2685           {
2686                 if ($2 != ModifierNone) {
2687                         report.Error (1609, GetLocation ($2), "Modifiers cannot be placed on event accessor declarations");
2688                 }
2689                 
2690                 current_event.Add = new EventProperty.AddDelegateMethod (current_event, (Attributes) $1, GetLocation ($3));
2691                 current_local_parameters = current_event.Add.ParameterInfo;
2692                 
2693                 lbag.AddMember (current_event.Add, mod_locations);
2694                 lexer.EventParsing = false;             
2695           }
2696           event_accessor_block
2697           {
2698                 lexer.EventParsing = true;
2699           
2700                 current_event.Add.Block = (ToplevelBlock) $5;
2701                 
2702                 if (current_container.Kind == MemberKind.Interface) {
2703                         report.Error (531, current_event.Add.Block.StartLocation,
2704                                 "`{0}': interface members cannot have a definition", current_event.Add.GetSignatureForError ());
2705                 }
2706                 
2707                 current_local_parameters = null;
2708           }
2709         ;
2710         
2711 remove_accessor_declaration
2712         : opt_attributes opt_modifiers REMOVE
2713           {
2714                 if ($2 != ModifierNone) {
2715                         report.Error (1609, GetLocation ($2), "Modifiers cannot be placed on event accessor declarations");
2716                 }
2717                 
2718                 current_event.Remove = new EventProperty.RemoveDelegateMethod (current_event, (Attributes) $1, GetLocation ($3));
2719                 current_local_parameters = current_event.Remove.ParameterInfo;
2720
2721                 lbag.AddMember (current_event.Remove, mod_locations);
2722                 lexer.EventParsing = false;             
2723           }
2724           event_accessor_block
2725           {
2726                 lexer.EventParsing = true;
2727           
2728                 current_event.Remove.Block = (ToplevelBlock) $5;
2729                 
2730                 if (current_container.Kind == MemberKind.Interface) {
2731                         report.Error (531, current_event.Remove.Block.StartLocation,
2732                                 "`{0}': interface members cannot have a definition", current_event.Remove.GetSignatureForError ());
2733                 }
2734                 
2735                 current_local_parameters = null;
2736           }
2737         ;
2738
2739 event_accessor_block
2740         : opt_semicolon
2741           {
2742                 report.Error (73, lexer.Location, "An add or remove accessor must have a body");
2743                 $$ = null;
2744           }
2745         | block;
2746         ;
2747
2748 attributes_without_members
2749         : attribute_sections CLOSE_BRACE
2750           {
2751                 current_type.UnattachedAttributes = (Attributes) $1;
2752                 report.Error (1519, GetLocation ($1), "An attribute is missing member declaration");
2753                 lexer.putback ('}');
2754           }
2755         ;
2756
2757 // For full ast try to recover incomplete ambiguous member
2758 // declaration in form on class X { public int }
2759 incomplete_member
2760         : opt_attributes opt_modifiers member_type CLOSE_BRACE
2761           {
2762                 report.Error (1519, lexer.Location, "Unexpected symbol `}' in class, struct, or interface member declaration");
2763  
2764                 lexer.putback ('}');
2765
2766                 lexer.parsing_generic_declaration = false;
2767                 FullNamedExpression type = (FullNamedExpression) $3;
2768                 current_field = new Field (current_type, type, (Modifiers) $2, MemberName.Null, (Attributes) $1);
2769                 current_type.AddField (current_field);
2770                 $$ = current_field;
2771           }
2772         ;
2773           
2774 enum_declaration
2775         : opt_attributes
2776           opt_modifiers
2777           ENUM type_declaration_name
2778           opt_enum_base
2779           {
2780                 if (doc_support)
2781                         enumTypeComment = Lexer.consume_doc_comment ();
2782           }
2783           OPEN_BRACE
2784           {
2785                 if (doc_support)
2786                         Lexer.doc_state = XmlCommentState.Allowed;
2787
2788                 MemberName name = (MemberName) $4;
2789                 if (name.IsGeneric) {
2790                         report.Error (1675, name.Location, "Enums cannot have type parameters");
2791                 }
2792                 
2793                 push_current_container (new Enum (current_container, (FullNamedExpression) $5, (Modifiers) $2, name, (Attributes) $1), null);
2794           }
2795           opt_enum_member_declarations
2796           {
2797                 lexer.parsing_modifiers = true;
2798           
2799                 // here will be evaluated after CLOSE_BLACE is consumed.
2800                 if (doc_support)
2801                         Lexer.doc_state = XmlCommentState.Allowed;
2802           }
2803           CLOSE_BRACE opt_semicolon
2804           {
2805                 if (doc_support)
2806                         current_container.DocComment = enumTypeComment;
2807                         
2808                 --lexer.parsing_declaration;
2809
2810 //                      if (doc_support)
2811 //                              em.DocComment = ev.DocComment;
2812
2813                 lbag.AddMember (current_container, mod_locations, GetLocation ($3), GetLocation ($7), GetLocation ($11));
2814                 $$ = pop_current_class ();
2815           }
2816         ;
2817
2818 opt_enum_base
2819         : /* empty */
2820         | COLON type
2821          {
2822                 $$ = $2;
2823          }
2824         | COLON error
2825          {
2826                 Error_TypeExpected (GetLocation ($1));
2827                 $$ = null;
2828          }
2829         ;
2830
2831 opt_enum_member_declarations
2832         : /* empty */
2833         | enum_member_declarations
2834         | enum_member_declarations COMMA
2835           {
2836                 lbag.AddLocation ($1, GetLocation ($2));
2837           }
2838         ;
2839
2840 enum_member_declarations
2841         : enum_member_declaration
2842         | enum_member_declarations COMMA enum_member_declaration
2843           {
2844                 lbag.AddLocation ($1, GetLocation ($2));
2845                 $$ = $3;
2846           }
2847         ;
2848
2849 enum_member_declaration
2850         : opt_attributes IDENTIFIER
2851           {
2852                 var lt = (LocatedToken) $2;
2853                 var em = new EnumMember ((Enum) current_type, new MemberName (lt.Value, lt.Location), (Attributes) $1);
2854                 ((Enum) current_type).AddEnumMember (em);
2855
2856                 if (doc_support) {
2857                         em.DocComment = Lexer.consume_doc_comment ();
2858                         Lexer.doc_state = XmlCommentState.Allowed;
2859                 }
2860
2861                 $$ = em;
2862           }
2863         | opt_attributes IDENTIFIER
2864           {
2865                 ++lexer.parsing_block;
2866                 if (doc_support) {
2867                         tmpComment = Lexer.consume_doc_comment ();
2868                         Lexer.doc_state = XmlCommentState.NotAllowed;
2869                 }
2870           }
2871           ASSIGN constant_expression
2872           { 
2873                 --lexer.parsing_block;
2874                 
2875                 var lt = (LocatedToken) $2;
2876                 var em = new EnumMember ((Enum) current_type, new MemberName (lt.Value, lt.Location), (Attributes) $1);
2877                 em.Initializer = new ConstInitializer (em, (Expression) $5, GetLocation ($4));
2878                 ((Enum) current_type).AddEnumMember (em);
2879                 
2880                 if (doc_support)
2881                         em.DocComment = ConsumeStoredComment ();
2882
2883                 $$ = em;
2884           }
2885         | opt_attributes IDENTIFIER error
2886           {
2887                 Error_SyntaxError (yyToken);
2888           
2889                 var lt = (LocatedToken) $2;
2890                 var em = new EnumMember ((Enum) current_type, new MemberName (lt.Value, lt.Location), (Attributes) $1);
2891                 ((Enum) current_type).AddEnumMember (em);
2892
2893                 if (doc_support) {
2894                         em.DocComment = Lexer.consume_doc_comment ();
2895                         Lexer.doc_state = XmlCommentState.Allowed;
2896                 }
2897
2898                 $$ = em;
2899           }
2900         | attributes_without_members
2901         ;
2902
2903 delegate_declaration
2904         : opt_attributes
2905           opt_modifiers
2906           DELEGATE
2907           member_type type_declaration_name
2908           OPEN_PARENS
2909           {
2910                 valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out | ParameterModifierType.Params | ParameterModifierType.DefaultValue;
2911           }
2912           opt_formal_parameter_list CLOSE_PARENS
2913           {
2914                 valid_param_mod = 0;
2915
2916                 ParametersCompiled p = (ParametersCompiled) $8;
2917
2918                 Delegate del = new Delegate (current_container, (FullNamedExpression) $4, (Modifiers) $2, (MemberName) $5, p, (Attributes) $1);
2919
2920                 p.CheckParameters (del);
2921
2922                 current_container.AddTypeContainer (del);
2923
2924                 current_delegate = del;
2925                 lexer.ConstraintsParsing = true;
2926           }
2927           opt_type_parameter_constraints_clauses
2928           {
2929                 lexer.ConstraintsParsing = false;
2930           }
2931           SEMICOLON
2932           {
2933                 if (doc_support) {
2934                         current_delegate.DocComment = Lexer.consume_doc_comment ();
2935                         Lexer.doc_state = XmlCommentState.Allowed;
2936                 }
2937           
2938                 if ($11 != null)
2939                         current_delegate.SetConstraints ((List<Constraints>) $11);
2940                 lbag.AddMember (current_delegate, mod_locations, GetLocation ($3), GetLocation ($6), GetLocation ($9), GetLocation ($13));
2941
2942                 $$ = current_delegate;
2943
2944                 current_delegate = null;
2945           }
2946         ;
2947
2948 opt_nullable
2949         : /* empty */
2950         | INTERR_NULLABLE
2951           {
2952                 if (lang_version < LanguageVersion.ISO_2)
2953                         FeatureIsNotAvailable (GetLocation ($1), "nullable types");
2954           
2955                 $$ = ComposedTypeSpecifier.CreateNullable (GetLocation ($1));
2956           }
2957         ;
2958
2959 namespace_or_type_expr
2960         : member_name
2961         | qualified_alias_member IDENTIFIER opt_type_argument_list
2962           {
2963                 var lt1 = (LocatedToken) $1;
2964                 var lt2 = (LocatedToken) $2;
2965                 
2966                 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, (TypeArguments) $3, lt1.Location);
2967                 lbag.AddLocation ($$, GetLocation ($2));
2968           }
2969         | qualified_alias_member IDENTIFIER generic_dimension
2970           {
2971                 var lt1 = (LocatedToken) $1;
2972                 var lt2 = (LocatedToken) $2;
2973
2974                 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, (int) $3, lt1.Location);
2975                 lbag.AddLocation ($$, GetLocation ($2));
2976           }
2977         ;
2978
2979 member_name
2980         : simple_name_expr
2981         | namespace_or_type_expr DOT IDENTIFIER opt_type_argument_list
2982           {
2983                 var lt = (LocatedToken) $3;
2984                 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
2985                 lbag.AddLocation ($$, GetLocation ($2));
2986           }
2987         | namespace_or_type_expr DOT IDENTIFIER generic_dimension
2988           {
2989                 var lt = (LocatedToken) $3;
2990                 $$ = new MemberAccess ((Expression) $1, lt.Value, (int) $4, lt.Location);
2991                 lbag.AddLocation ($$, GetLocation ($2));
2992           }
2993         ;
2994
2995 simple_name_expr
2996         : IDENTIFIER opt_type_argument_list
2997           {
2998                 var lt = (LocatedToken) $1;
2999                 $$ = new SimpleName (lt.Value, (TypeArguments)$2, lt.Location);
3000           }
3001         | IDENTIFIER generic_dimension
3002           {  
3003                 var lt = (LocatedToken) $1;
3004                 $$ = new SimpleName (lt.Value, (int) $2, lt.Location);
3005           }
3006         ;
3007
3008 //
3009 // Generics arguments  (any type, without attributes)
3010 //
3011 opt_type_argument_list
3012         : /* empty */
3013         | OP_GENERICS_LT type_arguments OP_GENERICS_GT
3014           {
3015                 if (lang_version < LanguageVersion.ISO_2)
3016                         FeatureIsNotAvailable (GetLocation ($1), "generics");     
3017           
3018                 $$ = $2;
3019           }
3020         | OP_GENERICS_LT error
3021           {
3022                 Error_TypeExpected (lexer.Location);
3023                 $$ = new TypeArguments ();
3024           }
3025         ;
3026
3027 type_arguments
3028         : type
3029           {
3030                 TypeArguments type_args = new TypeArguments ();
3031                 type_args.Add ((FullNamedExpression) $1);
3032                 $$ = type_args;
3033           }
3034         | type_arguments COMMA type
3035           {
3036                 TypeArguments type_args = (TypeArguments) $1;
3037                 type_args.Add ((FullNamedExpression) $3);
3038                 $$ = type_args;
3039           }       
3040         ;
3041
3042 //
3043 // Generics parameters (identifiers only, with attributes), used in type or method declarations
3044 //
3045 type_declaration_name
3046         : IDENTIFIER
3047           {
3048                 lexer.parsing_generic_declaration = true;
3049           }
3050           opt_type_parameter_list
3051           {
3052                 lexer.parsing_generic_declaration = false;
3053                 var lt = (LocatedToken) $1;
3054                 $$ = new MemberName (lt.Value, (TypeParameters)$3, lt.Location);
3055           }
3056         ;
3057
3058 member_declaration_name
3059         : method_declaration_name
3060           {
3061                 MemberName mn = (MemberName)$1;
3062                 if (mn.TypeParameters != null)
3063                         syntax_error (mn.Location, string.Format ("Member `{0}' cannot declare type arguments",
3064                                 mn.GetSignatureForError ()));
3065           }
3066         ;
3067
3068 method_declaration_name
3069         : type_declaration_name
3070         | explicit_interface IDENTIFIER opt_type_parameter_list
3071           {
3072                 lexer.parsing_generic_declaration = false;        
3073                 var lt = (LocatedToken) $2;
3074                 $$ = new MemberName (lt.Value, (TypeParameters) $3, (ATypeNameExpression) $1, lt.Location);
3075           }
3076         ;
3077         
3078 indexer_declaration_name
3079         : THIS
3080           {
3081                 lexer.parsing_generic_declaration = false;        
3082                 $$ = new MemberName (TypeDefinition.DefaultIndexerName, GetLocation ($1));
3083           }
3084         | explicit_interface THIS
3085           {
3086                 lexer.parsing_generic_declaration = false;
3087                 $$ = new MemberName (TypeDefinition.DefaultIndexerName, null, (ATypeNameExpression) $1, GetLocation ($2));
3088           }
3089         ;
3090
3091 explicit_interface
3092         : IDENTIFIER opt_type_argument_list DOT
3093           {
3094                 var lt = (LocatedToken) $1;
3095                 $$ = new SimpleName (lt.Value, (TypeArguments) $2, lt.Location);
3096                 lbag.AddLocation ($$, GetLocation ($3));
3097           }
3098         | qualified_alias_member IDENTIFIER opt_type_argument_list DOT
3099           {
3100                 var lt1 = (LocatedToken) $1;
3101                 var lt2 = (LocatedToken) $2;
3102
3103                 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, (TypeArguments) $3, lt1.Location);
3104                 lbag.AddLocation ($$, GetLocation ($4));
3105           }
3106         | explicit_interface IDENTIFIER opt_type_argument_list DOT
3107           {
3108                 var lt = (LocatedToken) $2;
3109                 $$ = new MemberAccess ((ATypeNameExpression) $1, lt.Value, (TypeArguments) $3, lt.Location);
3110                 lbag.AddLocation ($$, GetLocation ($4));
3111           }
3112         ;
3113         
3114 opt_type_parameter_list
3115         : /* empty */
3116         | OP_GENERICS_LT_DECL type_parameters OP_GENERICS_GT
3117           {
3118                 if (lang_version < LanguageVersion.ISO_2)
3119                         FeatureIsNotAvailable (GetLocation ($1), "generics");
3120           
3121                 $$ = $2;
3122                 lbag.AddLocation ($$, GetLocation ($1), GetLocation ($3));
3123           }
3124         ;
3125
3126 type_parameters
3127         : type_parameter
3128           {
3129                 var tparams = new TypeParameters ();
3130                 tparams.Add ((TypeParameter)$1);
3131                 $$ = tparams;
3132           }
3133         | type_parameters COMMA type_parameter
3134           {
3135                 var tparams = (TypeParameters) $1;
3136                 tparams.Add ((TypeParameter)$3);
3137                 $$ = tparams;
3138                 lbag.AddLocation ($3, GetLocation ($3));
3139           }       
3140         ;
3141
3142 type_parameter
3143         : opt_attributes opt_type_parameter_variance IDENTIFIER
3144           {
3145                 var lt = (LocatedToken)$3;
3146                 $$ = new TypeParameter (new MemberName (lt.Value, lt.Location), (Attributes)$1, (VarianceDecl) $2);
3147           }
3148         | error
3149           {
3150                 if (GetTokenName (yyToken) == "type")
3151                         report.Error (81, GetLocation ($1), "Type parameter declaration must be an identifier not a type");
3152                 else
3153                         Error_SyntaxError (yyToken);
3154                         
3155                 $$ = new TypeParameter (MemberName.Null, null, null);
3156           }
3157         ;
3158
3159 //
3160 // All types where void is allowed
3161 //
3162 type_and_void
3163         : type_expression_or_array
3164         | VOID
3165           {
3166                 $$ = new TypeExpression (compiler.BuiltinTypes.Void, GetLocation ($1));
3167           }
3168         ;
3169         
3170 member_type
3171         : type_and_void
3172           {
3173                 lexer.parsing_generic_declaration = true;
3174           }
3175         ;
3176         
3177 //
3178 // A type which does not allow `void' to be used
3179 //
3180 type
3181         : type_expression_or_array
3182         | void_invalid
3183         ;
3184         
3185 simple_type
3186         : type_expression
3187         | void_invalid
3188         ;
3189         
3190 parameter_type
3191         : type_expression_or_array
3192         | VOID
3193           {
3194                 report.Error (1536, GetLocation ($1), "Invalid parameter type `void'");
3195                 $$ = new TypeExpression (compiler.BuiltinTypes.Void, GetLocation ($1));
3196           }
3197         ;
3198
3199 type_expression_or_array
3200         : type_expression
3201         | type_expression rank_specifiers
3202           {
3203                 $$ = new ComposedCast ((FullNamedExpression) $1, (ComposedTypeSpecifier) $2);
3204           }
3205         ;
3206         
3207 type_expression
3208         : namespace_or_type_expr opt_nullable
3209           {
3210                 if ($2 != null) {
3211                         $$ = new ComposedCast ((ATypeNameExpression) $1, (ComposedTypeSpecifier) $2);
3212                 } else {
3213                         var sn = $1 as SimpleName;
3214                         if (sn != null && sn.Name == "var")
3215                                 $$ = new VarExpr (sn.Location);
3216                         else
3217                                 $$ = $1;
3218                 }
3219           }
3220         | namespace_or_type_expr pointer_stars
3221           {
3222                 $$ = new ComposedCast ((ATypeNameExpression) $1, (ComposedTypeSpecifier) $2);
3223           }
3224         | builtin_type_expression
3225         ;
3226
3227 void_invalid
3228         : VOID
3229           {
3230                 Expression.Error_VoidInvalidInTheContext (GetLocation ($1), report);
3231                 $$ = new TypeExpression (compiler.BuiltinTypes.Void, GetLocation ($1));
3232           }
3233         ;
3234
3235 builtin_type_expression
3236         : builtin_types opt_nullable
3237           {
3238                 if ($2 != null)
3239                         $$ = new ComposedCast ((FullNamedExpression) $1, (ComposedTypeSpecifier) $2);
3240           }
3241         | builtin_types pointer_stars
3242           {
3243                 $$ = new ComposedCast ((FullNamedExpression) $1, (ComposedTypeSpecifier) $2);
3244           }
3245         | VOID pointer_stars
3246           {
3247                 $$ = new ComposedCast (new TypeExpression (compiler.BuiltinTypes.Void, GetLocation ($1)), (ComposedTypeSpecifier) $2);
3248           }
3249         ;
3250
3251 type_list
3252         : base_type_name
3253           {
3254                 var types = new List<FullNamedExpression> (2);
3255                 types.Add ((FullNamedExpression) $1);
3256                 $$ = types;
3257           }
3258         | type_list COMMA base_type_name
3259           {
3260                 var types = (List<FullNamedExpression>) $1;
3261                 types.Add ((FullNamedExpression) $3);
3262                 $$ = types;
3263           }
3264         ;
3265
3266 base_type_name
3267         : type
3268           {
3269                 if ($1 is ComposedCast) {
3270                         report.Error (1521, GetLocation ($1), "Invalid base type `{0}'", ((ComposedCast)$1).GetSignatureForError ());
3271                 }
3272                 $$ = $1;
3273           }
3274         ;
3275         
3276 /*
3277  * replaces all the productions for isolating the various
3278  * simple types, but we need this to reuse it easily in variable_type
3279  */
3280 builtin_types
3281         : OBJECT        { $$ = new TypeExpression (compiler.BuiltinTypes.Object, GetLocation ($1)); }
3282         | STRING        { $$ = new TypeExpression (compiler.BuiltinTypes.String, GetLocation ($1)); }
3283         | BOOL          { $$ = new TypeExpression (compiler.BuiltinTypes.Bool, GetLocation ($1)); }
3284         | DECIMAL       { $$ = new TypeExpression (compiler.BuiltinTypes.Decimal, GetLocation ($1)); }
3285         | FLOAT         { $$ = new TypeExpression (compiler.BuiltinTypes.Float, GetLocation ($1)); }
3286         | DOUBLE        { $$ = new TypeExpression (compiler.BuiltinTypes.Double, GetLocation ($1)); }
3287         | integral_type
3288         ;
3289
3290 integral_type
3291         : SBYTE         { $$ = new TypeExpression (compiler.BuiltinTypes.SByte, GetLocation ($1)); }
3292         | BYTE          { $$ = new TypeExpression (compiler.BuiltinTypes.Byte, GetLocation ($1)); }
3293         | SHORT         { $$ = new TypeExpression (compiler.BuiltinTypes.Short, GetLocation ($1)); }
3294         | USHORT        { $$ = new TypeExpression (compiler.BuiltinTypes.UShort, GetLocation ($1)); }
3295         | INT           { $$ = new TypeExpression (compiler.BuiltinTypes.Int, GetLocation ($1)); }
3296         | UINT          { $$ = new TypeExpression (compiler.BuiltinTypes.UInt, GetLocation ($1)); }
3297         | LONG          { $$ = new TypeExpression (compiler.BuiltinTypes.Long, GetLocation ($1)); }
3298         | ULONG         { $$ = new TypeExpression (compiler.BuiltinTypes.ULong, GetLocation ($1)); }
3299         | CHAR          { $$ = new TypeExpression (compiler.BuiltinTypes.Char, GetLocation ($1)); }
3300         ;
3301
3302 //
3303 // Expressions, section 7.5
3304 //
3305
3306
3307 primary_expression
3308         : type_name_expression
3309         | literal
3310         | array_creation_expression
3311         | parenthesized_expression
3312         | default_value_expression
3313         | invocation_expression
3314         | element_access
3315         | this_access
3316         | base_access
3317         | post_increment_expression
3318         | post_decrement_expression
3319         | object_or_delegate_creation_expression
3320         | anonymous_type_expression
3321         | typeof_expression
3322         | sizeof_expression
3323         | checked_expression
3324         | unchecked_expression
3325         | pointer_member_access
3326         | anonymous_method_expression
3327         | undocumented_expressions
3328         | interpolated_string
3329         ;
3330
3331 type_name_expression
3332         : simple_name_expr
3333         | IDENTIFIER GENERATE_COMPLETION {
3334                 var lt = (LocatedToken) $1;
3335                $$ = new CompletionSimpleName (MemberName.MakeName (lt.Value, null), lt.Location);
3336           }
3337         | member_access
3338         ;
3339
3340 literal
3341         : boolean_literal
3342         | LITERAL
3343         | NULL                  { $$ = new NullLiteral (GetLocation ($1)); }
3344         ;
3345
3346 boolean_literal
3347         : TRUE                  { $$ = new BoolLiteral (compiler.BuiltinTypes, true, GetLocation ($1)); }
3348         | FALSE                 { $$ = new BoolLiteral (compiler.BuiltinTypes, false, GetLocation ($1)); }
3349         ;
3350
3351 interpolated_string
3352         : INTERPOLATED_STRING interpolations INTERPOLATED_STRING_END
3353           {
3354                 if (lang_version < LanguageVersion.V_6)
3355                         FeatureIsNotAvailable (GetLocation ($1), "interpolated strings");
3356
3357                 $$ = new InterpolatedString ((StringLiteral) $1, (List<Expression>) $2, (StringLiteral) $3);
3358           }
3359         | INTERPOLATED_STRING_END
3360           {
3361                 if (lang_version < LanguageVersion.V_6)
3362                         FeatureIsNotAvailable (GetLocation ($1), "interpolated strings");
3363
3364                 $$ = new InterpolatedString ((StringLiteral) $1, null, null);
3365           }
3366         ;
3367
3368 interpolations
3369         : interpolation
3370           {
3371                 var list = new List<Expression> ();
3372                 list.Add ((InterpolatedStringInsert) $1);
3373                 $$ = list;
3374           }
3375         | interpolations INTERPOLATED_STRING interpolation
3376           {
3377                 var list = (List<Expression>) $1;
3378                 list.Add ((StringLiteral) $2);
3379                 list.Add ((InterpolatedStringInsert) $3);
3380                 $$ = list;
3381           }
3382         ;
3383
3384 interpolation
3385         : expression
3386           {
3387                 $$ = new InterpolatedStringInsert ((Expression) $1);
3388           }
3389         | expression COMMA expression
3390           {
3391                 $$ = new InterpolatedStringInsert ((Expression) $1) {
3392                         Alignment = (Expression)$3
3393                 };
3394           }
3395         | expression COLON
3396           {
3397                 lexer.parsing_interpolation_format = true;
3398           }
3399           LITERAL
3400           {
3401                 lexer.parsing_interpolation_format = false;
3402
3403                 $$ = new InterpolatedStringInsert ((Expression) $1) {
3404                         Format = (string)$4
3405                 };
3406           }
3407         | expression COMMA expression COLON
3408           {
3409                 lexer.parsing_interpolation_format = true;
3410           }
3411           LITERAL
3412           {
3413                 lexer.parsing_interpolation_format = false;
3414
3415                 $$ = new InterpolatedStringInsert ((Expression) $1) {
3416                         Alignment = (Expression)$3,
3417                         Format = (string) $6
3418                 };
3419           }
3420         ;
3421
3422
3423 //
3424 // Here is the trick, tokenizer may think that parens is a special but
3425 // parser is interested in open parens only, so we merge them.
3426 // Consider: if (a)foo ();
3427 //
3428 open_parens_any
3429         : OPEN_PARENS
3430         | OPEN_PARENS_CAST
3431         ;
3432
3433 // 
3434 // Use this production to accept closing parenthesis or 
3435 // performing completion
3436 //
3437 close_parens
3438         : CLOSE_PARENS
3439         | COMPLETE_COMPLETION
3440         ;
3441
3442
3443 parenthesized_expression
3444         : OPEN_PARENS expression CLOSE_PARENS
3445           {
3446                 $$ = new ParenthesizedExpression ((Expression) $2, GetLocation ($1));
3447                 lbag.AddLocation ($$, GetLocation ($1), GetLocation ($3));
3448           }
3449         | OPEN_PARENS expression COMPLETE_COMPLETION
3450           {
3451                 $$ = new ParenthesizedExpression ((Expression) $2, GetLocation ($1));
3452           }
3453         ;
3454
3455 member_access
3456         : primary_expression DOT identifier_inside_body opt_type_argument_list
3457           {
3458                 var lt = (LocatedToken) $3;
3459                 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
3460                 lbag.AddLocation ($$, GetLocation ($2));
3461           }
3462         | primary_expression DOT identifier_inside_body generic_dimension
3463           {
3464                 var lt = (LocatedToken) $3;
3465                 $$ = new MemberAccess ((Expression) $1, lt.Value, (int) $4, lt.Location);
3466                 lbag.AddLocation ($$, GetLocation ($2));
3467           }
3468         | primary_expression INTERR_OPERATOR DOT identifier_inside_body opt_type_argument_list
3469           {
3470                 if (lang_version < LanguageVersion.V_6)
3471                         FeatureIsNotAvailable (GetLocation ($2), "null propagating operator");
3472
3473                 var lt = (LocatedToken) $4;
3474                 $$ = new ConditionalMemberAccess ((Expression) $1, lt.Value, (TypeArguments) $5, lt.Location);
3475                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($3));
3476           }
3477         | builtin_types DOT identifier_inside_body opt_type_argument_list
3478           {
3479                 var lt = (LocatedToken) $3;
3480                 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
3481                 lbag.AddLocation ($$, GetLocation ($2));
3482           }
3483         | BASE DOT identifier_inside_body opt_type_argument_list
3484           {
3485                 var lt = (LocatedToken) $3;
3486                 $$ = new MemberAccess (new BaseThis (GetLocation ($1)), lt.Value, (TypeArguments) $4, lt.Location);
3487                 lbag.AddLocation ($$, GetLocation ($2));
3488           }
3489         | AWAIT DOT identifier_inside_body opt_type_argument_list
3490           {
3491                 var lt = (LocatedToken) $3;
3492                 $$ = new MemberAccess (new SimpleName ("await", ((LocatedToken) $1).Location), lt.Value, (TypeArguments) $4, lt.Location);
3493                 lbag.AddLocation ($$, GetLocation ($2));
3494           }
3495         | qualified_alias_member identifier_inside_body opt_type_argument_list
3496           {
3497                 var lt1 = (LocatedToken) $1;
3498                 var lt2 = (LocatedToken) $2;
3499
3500                 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, (TypeArguments) $3, lt1.Location);
3501                 lbag.AddLocation ($$, GetLocation ($2));
3502           }
3503         | qualified_alias_member identifier_inside_body generic_dimension
3504           {
3505                 var lt1 = (LocatedToken) $1;
3506                 var lt2 = (LocatedToken) $2;
3507
3508                 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, (int) $3, lt1.Location);
3509                 lbag.AddLocation ($$, GetLocation ($2));
3510           }
3511         | primary_expression DOT GENERATE_COMPLETION {
3512                 $$ = new CompletionMemberAccess ((Expression) $1, null,GetLocation ($3));
3513           }
3514         | primary_expression DOT IDENTIFIER GENERATE_COMPLETION {
3515                 var lt = (LocatedToken) $3;
3516                 $$ = new CompletionMemberAccess ((Expression) $1, lt.Value, lt.Location);
3517           }
3518         | builtin_types DOT GENERATE_COMPLETION
3519           {
3520                 $$ = new CompletionMemberAccess ((Expression) $1, null, lexer.Location);
3521           }
3522         | builtin_types DOT IDENTIFIER GENERATE_COMPLETION {
3523                 var lt = (LocatedToken) $3;
3524                 $$ = new CompletionMemberAccess ((Expression) $1, lt.Value, lt.Location);
3525           }
3526         ;
3527
3528 invocation_expression
3529         : primary_expression open_parens_any opt_argument_list close_parens
3530           {
3531                 $$ = new Invocation ((Expression) $1, (Arguments) $3);
3532                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
3533           }
3534         | primary_expression open_parens_any argument_list error
3535           {
3536                 Error_SyntaxError (yyToken);
3537
3538                 $$ = new Invocation ((Expression) $1, (Arguments) $3);
3539                 lbag.AddLocation ($$, GetLocation ($2));
3540           }
3541         | primary_expression open_parens_any error
3542           {
3543                 Error_SyntaxError (yyToken);
3544
3545                 $$ = new Invocation ((Expression) $1, null);
3546                 lbag.AddLocation ($$, GetLocation ($2));
3547           }
3548         ;
3549
3550 opt_object_or_collection_initializer
3551         : /* empty */           { $$ = null; }
3552         | object_or_collection_initializer
3553         ;
3554
3555 object_or_collection_initializer
3556         : OPEN_BRACE opt_member_initializer_list close_brace_or_complete_completion
3557           {
3558                 if ($2 == null) {
3559                         $$ = new CollectionOrObjectInitializers (GetLocation ($1));
3560                 } else {
3561                         $$ = new CollectionOrObjectInitializers ((List<Expression>) $2, GetLocation ($1));
3562                 }
3563                 lbag.AddLocation ($$, GetLocation ($3));
3564           }
3565         | OPEN_BRACE member_initializer_list COMMA CLOSE_BRACE
3566           {
3567                 $$ = new CollectionOrObjectInitializers ((List<Expression>) $2, GetLocation ($1));
3568                 lbag.AddLocation ($$, GetLocation ($3), GetLocation ($4));
3569           }
3570         ;
3571
3572 opt_member_initializer_list
3573         : /* empty */           { $$ = null; }
3574         | member_initializer_list
3575         {
3576                 $$ = $1;
3577         }
3578         ;
3579
3580 member_initializer_list
3581         : member_initializer
3582           {
3583                 var a = new List<Expression> ();
3584                 a.Add ((Expression) $1);
3585                 $$ = a;
3586           }
3587         | member_initializer_list COMMA member_initializer
3588           {
3589                 var a = (List<Expression>)$1;
3590                 a.Add ((Expression) $3);
3591                 $$ = a;
3592           }
3593         | member_initializer_list error {
3594                 Error_SyntaxError (yyToken);
3595                 $$ = $1;
3596           }
3597         ;
3598
3599 member_initializer
3600         : IDENTIFIER ASSIGN initializer_value
3601           {
3602                 var lt = (LocatedToken) $1;
3603                 $$ = new ElementInitializer (lt.Value, (Expression)$3, lt.Location);
3604                 lbag.AddLocation ($$, GetLocation ($2));
3605           }
3606         | AWAIT ASSIGN initializer_value
3607           {
3608                 var lt = (LocatedToken) Error_AwaitAsIdentifier ($1);
3609                 $$ = new ElementInitializer (lt.Value, (Expression)$3, lt.Location);
3610                 lbag.AddLocation ($$, GetLocation ($2));
3611           }
3612         | GENERATE_COMPLETION 
3613           {
3614                 $$ = new CompletionElementInitializer (null, GetLocation ($1));
3615           }
3616         | non_assignment_expression opt_COMPLETE_COMPLETION  {
3617                 CompletionSimpleName csn = $1 as CompletionSimpleName;
3618                 if (csn == null)
3619                         $$ = new CollectionElementInitializer ((Expression)$1);
3620                 else
3621                         $$ = new CompletionElementInitializer (csn.Prefix, csn.Location);
3622           }
3623         | OPEN_BRACE expression_list CLOSE_BRACE
3624           {
3625                 if ($2 == null)
3626                         $$ = new CollectionElementInitializer (GetLocation ($1));
3627                 else
3628                         $$ = new CollectionElementInitializer ((List<Expression>)$2, GetLocation ($1));
3629
3630                 lbag.AddLocation ($$, GetLocation ($3));
3631           }
3632         | OPEN_BRACKET_EXPR argument_list CLOSE_BRACKET ASSIGN initializer_value
3633           {
3634                 if (lang_version < LanguageVersion.V_6)
3635                         FeatureIsNotAvailable (GetLocation ($1), "dictionary initializer");
3636
3637                 $$ = new DictionaryElementInitializer ((Arguments)$2, (Expression) $5, GetLocation ($1));
3638                 lbag.AddLocation ($$, GetLocation ($3), GetLocation ($4));
3639           }
3640         | OPEN_BRACE CLOSE_BRACE
3641           {
3642                 report.Error (1920, GetLocation ($1), "An element initializer cannot be empty");
3643                 $$ = new CollectionElementInitializer (GetLocation ($1));
3644                 lbag.AddLocation ($$, GetLocation ($2));
3645           }
3646         ;
3647
3648 initializer_value
3649         : expression
3650         | object_or_collection_initializer
3651         ;
3652
3653 opt_argument_list
3654         : /* empty */           { $$ = null; }
3655         | argument_list
3656         ;
3657
3658 argument_list
3659         : argument_or_named_argument
3660           { 
3661                 Arguments list = new Arguments (4);
3662                 list.Add ((Argument) $1);
3663                 $$ = list;
3664           }
3665         | argument_list COMMA argument
3666           {
3667                 Arguments list = (Arguments) $1;
3668                 if (list [list.Count - 1] is NamedArgument)
3669                         Error_NamedArgumentExpected ((NamedArgument) list [list.Count - 1]);
3670                 
3671                 list.Add ((Argument) $3);
3672                 $$ = list;
3673           }
3674         | argument_list COMMA named_argument
3675           {
3676                 Arguments list = (Arguments) $1;
3677                 NamedArgument a = (NamedArgument) $3;
3678                 for (int i = 0; i < list.Count; ++i) {
3679                         NamedArgument na = list [i] as NamedArgument;
3680                         if (na != null && na.Name == a.Name)
3681                                 report.Error (1740, na.Location, "Named argument `{0}' specified multiple times",
3682                                         na.Name);
3683                 }
3684                 
3685                 list.Add (a);
3686                 $$ = list;
3687           }
3688         | argument_list COMMA error
3689           {
3690                 if (lexer.putback_char == -1)
3691                         lexer.putback (')'); // TODO: Wrong but what can I do
3692                 Error_SyntaxError (yyToken);
3693                 $$ = $1;
3694           }
3695         | COMMA error
3696           {
3697                 report.Error (839, GetLocation ($1), "An argument is missing");
3698                 $$ = null;
3699           }
3700         ;
3701
3702 argument
3703         : expression
3704           {
3705                 $$ = new Argument ((Expression) $1);
3706           }
3707         | non_simple_argument
3708         ;
3709
3710 argument_or_named_argument
3711         : argument
3712         | named_argument
3713         ;
3714
3715 non_simple_argument
3716         : REF variable_reference 
3717           { 
3718                 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
3719                 lbag.AddLocation ($$, GetLocation ($1));
3720           }
3721         | REF declaration_expression
3722           {
3723                 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
3724           }
3725         | OUT variable_reference 
3726           { 
3727                 $$ = new Argument ((Expression) $2, Argument.AType.Out);
3728                 lbag.AddLocation ($$, GetLocation ($1));
3729           }
3730         | OUT declaration_expression
3731           {
3732                 $$ = new Argument ((Expression) $2, Argument.AType.Out);
3733           }
3734         | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
3735           {
3736                 $$ = new Argument (new Arglist ((Arguments) $3, GetLocation ($1)));
3737                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
3738           }
3739         | ARGLIST OPEN_PARENS CLOSE_PARENS
3740           {
3741                 $$ = new Argument (new Arglist (GetLocation ($1)));
3742                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($3));
3743           }       
3744         ;
3745
3746 declaration_expression
3747         : OPEN_PARENS declaration_expression CLOSE_PARENS
3748           {
3749                 $$ = new ParenthesizedExpression ((Expression) $2, GetLocation ($1));
3750                 lbag.AddLocation ($$, GetLocation ($1), GetLocation ($3));
3751           }
3752 /*
3753         | CHECKED open_parens_any declaration_expression CLOSE_PARENS
3754           {
3755                 $$ = new CheckedExpr ((Expression) $3, GetLocation ($1));
3756                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
3757           }
3758         | UNCHECKED open_parens_any declaration_expression CLOSE_PARENS
3759           {
3760                 $$ = new UnCheckedExpr ((Expression) $3, GetLocation ($1));
3761                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
3762           }
3763 */
3764         | variable_type identifier_inside_body
3765           {
3766                 if (lang_version != LanguageVersion.Experimental)
3767                         FeatureIsNotAvailable (GetLocation ($1), "declaration expression");
3768
3769                 var lt = (LocatedToken) $2;
3770                 var lv = new LocalVariable (current_block, lt.Value, lt.Location);
3771                 current_block.AddLocalName (lv);
3772                 $$ = new DeclarationExpression ((FullNamedExpression) $1, lv);
3773           }
3774         | variable_type identifier_inside_body ASSIGN expression
3775           {
3776                 if (lang_version != LanguageVersion.Experimental)
3777                         FeatureIsNotAvailable (GetLocation ($1), "declaration expression");
3778
3779                 var lt = (LocatedToken) $2;
3780                 var lv = new LocalVariable (current_block, lt.Value, lt.Location);
3781                 current_block.AddLocalName (lv);
3782                 $$ = new DeclarationExpression ((FullNamedExpression) $1, lv) {
3783                         Initializer = (Expression) $4
3784                 };
3785           }
3786         ;
3787
3788 variable_reference
3789         : expression
3790         ;
3791
3792 element_access
3793         : primary_expression OPEN_BRACKET_EXPR expression_list_arguments CLOSE_BRACKET  
3794           {
3795                 $$ = new ElementAccess ((Expression) $1, (Arguments) $3, GetLocation ($2));
3796                 lbag.AddLocation ($$, GetLocation ($4));
3797           }
3798         | primary_expression INTERR_OPERATOR OPEN_BRACKET_EXPR expression_list_arguments CLOSE_BRACKET  
3799           {
3800                 if (lang_version < LanguageVersion.V_6)
3801                         FeatureIsNotAvailable (GetLocation ($2), "null propagating operator");
3802
3803                 $$ = new ElementAccess ((Expression) $1, (Arguments) $4, GetLocation ($3)) {
3804                         ConditionalAccess = true
3805                 };
3806
3807                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($5));
3808           }
3809         | primary_expression OPEN_BRACKET_EXPR expression_list_arguments error
3810           {
3811                 Error_SyntaxError (yyToken);
3812                 $$ = new ElementAccess ((Expression) $1, (Arguments) $3, GetLocation ($2));
3813           }
3814         | primary_expression OPEN_BRACKET_EXPR error
3815           {
3816                 Error_SyntaxError (yyToken);
3817                 $$ = new ElementAccess ((Expression) $1, null, GetLocation ($2));
3818           }
3819         ;
3820
3821 expression_list
3822         : expression_or_error
3823           {
3824                 var list = new List<Expression> (4);
3825                 list.Add ((Expression) $1);
3826                 $$ = list;
3827           }
3828         | expression_list COMMA expression_or_error
3829           {
3830                 var list = (List<Expression>) $1;
3831                 list.Add ((Expression) $3);
3832                 $$ = list;
3833           }
3834         ;
3835         
3836 expression_list_arguments
3837         : expression_list_argument
3838           {
3839                 Arguments args = new Arguments (4);
3840                 args.Add ((Argument) $1);
3841                 $$ = args;
3842           }
3843         | expression_list_arguments COMMA expression_list_argument
3844           {
3845                 Arguments args = (Arguments) $1;
3846                 if (args [args.Count - 1] is NamedArgument && !($3 is NamedArgument))
3847                         Error_NamedArgumentExpected ((NamedArgument) args [args.Count - 1]);
3848           
3849                 args.Add ((Argument) $3);
3850                 $$ = args;        
3851           }
3852         ;
3853         
3854 expression_list_argument
3855         : expression
3856           {
3857                 $$ = new Argument ((Expression) $1);
3858           }
3859         | named_argument
3860         ;
3861
3862 this_access
3863         : THIS
3864           {
3865                 $$ = new This (GetLocation ($1));
3866           }
3867         ;
3868
3869 base_access
3870         : BASE OPEN_BRACKET_EXPR expression_list_arguments CLOSE_BRACKET
3871           {
3872                 $$ = new ElementAccess (new BaseThis (GetLocation ($1)), (Arguments) $3, GetLocation ($2));
3873                 lbag.AddLocation ($$, GetLocation ($4));
3874           }
3875         | BASE OPEN_BRACKET error
3876           {
3877                 Error_SyntaxError (yyToken);
3878                 $$ = new ElementAccess (null, null, GetLocation ($2));
3879           }
3880         ;
3881
3882 post_increment_expression
3883         : primary_expression OP_INC
3884           {
3885                 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement, (Expression) $1, GetLocation ($2));
3886           }
3887         ;
3888
3889 post_decrement_expression
3890         : primary_expression OP_DEC
3891           {
3892                 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement, (Expression) $1, GetLocation ($2));
3893           }
3894         ;
3895         
3896 object_or_delegate_creation_expression
3897         : NEW new_expr_type open_parens_any opt_argument_list CLOSE_PARENS opt_object_or_collection_initializer
3898           {
3899                 if ($6 != null) {
3900                         if (lang_version <= LanguageVersion.ISO_2)
3901                                 FeatureIsNotAvailable (GetLocation ($1), "object initializers");
3902                                 
3903                         $$ = new NewInitialize ((FullNamedExpression) $2, (Arguments) $4, (CollectionOrObjectInitializers) $6, GetLocation ($1));
3904                 } else {
3905                         $$ = new New ((FullNamedExpression) $2, (Arguments) $4, GetLocation ($1));
3906                 }
3907                 
3908                 lbag.AddLocation ($$, GetLocation ($3), GetLocation ($5));
3909           }
3910         | NEW new_expr_type object_or_collection_initializer
3911           {
3912                 if (lang_version <= LanguageVersion.ISO_2)
3913                         FeatureIsNotAvailable (GetLocation ($1), "collection initializers");
3914           
3915                 $$ = new NewInitialize ((FullNamedExpression) $2, null, (CollectionOrObjectInitializers) $3, GetLocation ($1));
3916           }
3917         ;
3918
3919 array_creation_expression
3920         : NEW new_expr_type OPEN_BRACKET_EXPR expression_list CLOSE_BRACKET
3921           opt_rank_specifier
3922           opt_array_initializer
3923           {
3924                 $$ = new ArrayCreation ((FullNamedExpression) $2, (List<Expression>) $4,
3925                                 new ComposedTypeSpecifier (((List<Expression>) $4).Count, GetLocation ($3)) {
3926                                         Next = (ComposedTypeSpecifier) $6
3927                                 }, (ArrayInitializer) $7, GetLocation ($1));
3928                 lbag.AddLocation ($$, GetLocation ($3), GetLocation ($5));
3929           }
3930         | NEW new_expr_type rank_specifiers opt_array_initializer
3931           {
3932                 if ($4 == null)
3933                         report.Error (1586, GetLocation ($1), "Array creation must have array size or array initializer");
3934
3935                 $$ = new ArrayCreation ((FullNamedExpression) $2, (ComposedTypeSpecifier) $3, (ArrayInitializer) $4, GetLocation ($1)) {
3936                         NoEmptyInterpolation = true
3937                 };
3938           }
3939         | NEW rank_specifier array_initializer
3940           {
3941                 if (lang_version <= LanguageVersion.ISO_2)
3942                         FeatureIsNotAvailable (GetLocation ($1), "implicitly typed arrays");
3943           
3944                 $$ = new ImplicitlyTypedArrayCreation ((ComposedTypeSpecifier) $2, (ArrayInitializer) $3, GetLocation ($1));
3945           }
3946         | NEW new_expr_type OPEN_BRACKET CLOSE_BRACKET OPEN_BRACKET_EXPR error CLOSE_BRACKET
3947           {
3948                 report.Error (178, GetLocation ($6), "Invalid rank specifier, expecting `,' or `]'");
3949                 $$ = new ArrayCreation ((FullNamedExpression) $2, null, GetLocation ($1));
3950           }
3951         | NEW new_expr_type error
3952           {
3953                 Error_SyntaxError (yyToken);
3954                 // It can be any of new expression, create the most common one
3955                 $$ = new New ((FullNamedExpression) $2, null, GetLocation ($1));
3956           }
3957         ;
3958
3959 new_expr_type
3960         : {
3961                 ++lexer.parsing_type;
3962           }
3963           simple_type
3964           {
3965                 --lexer.parsing_type;
3966                 $$ = $2;
3967           }
3968         ;
3969
3970 anonymous_type_expression
3971         : NEW OPEN_BRACE anonymous_type_parameters_opt_comma CLOSE_BRACE
3972           {
3973                 if (lang_version <= LanguageVersion.ISO_2)
3974                         FeatureIsNotAvailable (GetLocation ($1), "anonymous types");
3975
3976                 $$ = new NewAnonymousType ((List<AnonymousTypeParameter>) $3, current_container, GetLocation ($1));
3977                 
3978                 // TODO: lbag comma location
3979                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
3980           }
3981         | NEW OPEN_BRACE GENERATE_COMPLETION
3982           {
3983                 $$ = new EmptyCompletion ();
3984           }
3985         ;
3986
3987 anonymous_type_parameters_opt_comma
3988         : anonymous_type_parameters_opt
3989         | anonymous_type_parameters COMMA
3990         ;
3991
3992 anonymous_type_parameters_opt
3993         : { $$ = null; }
3994         | anonymous_type_parameters
3995         ;
3996
3997 anonymous_type_parameters
3998         : anonymous_type_parameter
3999           {
4000                 var a = new List<AnonymousTypeParameter> (4);
4001                 a.Add ((AnonymousTypeParameter) $1);
4002                 $$ = a;
4003           }
4004         | anonymous_type_parameters COMMA anonymous_type_parameter
4005           {
4006                 var a = (List<AnonymousTypeParameter>) $1;
4007                 a.Add ((AnonymousTypeParameter) $3);
4008                 $$ = a;
4009           }
4010         | COMPLETE_COMPLETION
4011           {
4012                 $$ = new EmptyCompletion ();
4013           }
4014         | anonymous_type_parameter COMPLETE_COMPLETION
4015           {
4016                 $$ = $1;
4017           }
4018         ;
4019
4020 anonymous_type_parameter
4021         : identifier_inside_body ASSIGN variable_initializer
4022           {
4023                 var lt = (LocatedToken)$1;
4024                 $$ = new AnonymousTypeParameter ((Expression)$3, lt.Value, lt.Location);
4025                 lbag.AddLocation ($$, GetLocation ($2));
4026           }
4027         | identifier_inside_body
4028           {
4029                 var lt = (LocatedToken)$1;
4030                 $$ = new AnonymousTypeParameter (new SimpleName (lt.Value, lt.Location),
4031                         lt.Value, lt.Location);
4032           }
4033         | member_access
4034           {
4035                 MemberAccess ma = (MemberAccess) $1;
4036                 $$ = new AnonymousTypeParameter (ma, ma.Name, ma.Location);
4037           }
4038         | error
4039           {
4040                 report.Error (746, lexer.Location,
4041                         "Invalid anonymous type member declarator. Anonymous type members must be a member assignment, simple name or member access expression");
4042                 $$ = null;
4043           }
4044         ;
4045
4046 opt_rank_specifier
4047         : /* empty */
4048         | rank_specifiers
4049         ;
4050
4051 rank_specifiers
4052         : rank_specifier
4053         | rank_specifier rank_specifiers
4054           {
4055                 ((ComposedTypeSpecifier) $1).Next = (ComposedTypeSpecifier) $2;
4056                 $$ = $1;
4057           }
4058         ;
4059
4060 rank_specifier
4061         : OPEN_BRACKET CLOSE_BRACKET
4062           {
4063                 $$ = ComposedTypeSpecifier.CreateArrayDimension (1, GetLocation ($1));
4064                 lbag.AddLocation ($$, GetLocation ($2));
4065           }
4066         | OPEN_BRACKET dim_separators CLOSE_BRACKET
4067           {
4068                 $$ = ComposedTypeSpecifier.CreateArrayDimension ((int)$2, GetLocation ($1));
4069                 lbag.AddLocation ($$, GetLocation ($3));
4070           }
4071         ;
4072
4073 dim_separators
4074         : COMMA
4075           {
4076                 $$ = 2;
4077           }
4078         | dim_separators COMMA
4079           {
4080                 $$ = ((int) $1) + 1;
4081           }
4082         ;
4083
4084 opt_array_initializer
4085         : /* empty */
4086           {
4087                 $$ = null;
4088           }
4089         | array_initializer
4090           {
4091                 $$ = $1;
4092           }
4093         ;
4094
4095 array_initializer
4096         : OPEN_BRACE CLOSE_BRACE
4097           {
4098                 var ai = new ArrayInitializer (0, GetLocation ($1));
4099                 ai.VariableDeclaration = current_variable;
4100                 lbag.AddLocation (ai, GetLocation ($2));
4101                 $$ = ai;
4102           }
4103         | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
4104           {
4105                 var ai = new ArrayInitializer ((List<Expression>) $2, GetLocation ($1));
4106                 ai.VariableDeclaration = current_variable;
4107                 if ($3 != null) {
4108                         lbag.AddLocation (ai, GetLocation ($3), GetLocation ($4));
4109                 } else {
4110                         lbag.AddLocation (ai, GetLocation ($4));
4111                 }
4112                 $$ = ai;
4113           }
4114         ;
4115
4116 variable_initializer_list
4117         : variable_initializer
4118           {
4119                 var list = new List<Expression> (4);
4120                 list.Add ((Expression) $1);
4121                 $$ = list;
4122           }
4123         | variable_initializer_list COMMA variable_initializer
4124           {
4125                 var list = (List<Expression>) $1;
4126                 list.Add ((Expression) $3);
4127                 $$ = list;
4128           }
4129         ;
4130
4131 typeof_expression
4132         : TYPEOF open_parens_any typeof_type_expression CLOSE_PARENS
4133           {
4134                 $$ = new TypeOf ((FullNamedExpression) $3, GetLocation ($1));
4135                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
4136           }
4137         ;
4138         
4139 typeof_type_expression
4140         : type_and_void
4141         | error
4142          {
4143                 Error_TypeExpected (lexer.Location);
4144                 $$ = null;
4145          }
4146         ;
4147
4148 generic_dimension
4149         : GENERIC_DIMENSION
4150           {
4151                 if (lang_version < LanguageVersion.ISO_2)
4152                         FeatureIsNotAvailable (GetLocation ($1), "generics");
4153
4154                 $$ = $1;
4155           }
4156         ;
4157         
4158 qualified_alias_member
4159         : IDENTIFIER DOUBLE_COLON
4160           {
4161                 var lt = (LocatedToken) $1;
4162                 if (lang_version == LanguageVersion.ISO_1)
4163                         FeatureIsNotAvailable (lt.Location, "namespace alias qualifier");
4164
4165                 $$ = lt;                
4166           }
4167         ;
4168
4169 sizeof_expression
4170         : SIZEOF open_parens_any type CLOSE_PARENS
4171           { 
4172                 $$ = new SizeOf ((Expression) $3, GetLocation ($1));
4173                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
4174           }
4175         | SIZEOF open_parens_any type error
4176           {
4177                 Error_SyntaxError (yyToken);
4178
4179                 $$ = new SizeOf ((Expression) $3, GetLocation ($1));
4180                 lbag.AddLocation ($$, GetLocation ($2));
4181           }
4182         ;
4183
4184 checked_expression
4185         : CHECKED open_parens_any expression CLOSE_PARENS
4186           {
4187                 $$ = new CheckedExpr ((Expression) $3, GetLocation ($1));
4188                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
4189           }
4190         | CHECKED error
4191           {
4192                 Error_SyntaxError (yyToken);
4193
4194                 $$ = new CheckedExpr (null, GetLocation ($1));
4195           }
4196         ;
4197
4198 unchecked_expression
4199         : UNCHECKED open_parens_any expression CLOSE_PARENS
4200           {
4201                 $$ = new UnCheckedExpr ((Expression) $3, GetLocation ($1));
4202                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
4203           }
4204         | UNCHECKED error
4205           {
4206                 Error_SyntaxError (yyToken);
4207
4208                 $$ = new UnCheckedExpr (null, GetLocation ($1));
4209           }
4210         ;
4211
4212 pointer_member_access
4213         : primary_expression OP_PTR IDENTIFIER opt_type_argument_list
4214           {
4215                 var lt = (LocatedToken) $3;
4216                 $$ = new MemberAccess (new Indirection ((Expression) $1, GetLocation ($2)), lt.Value, (TypeArguments) $4, lt.Location);
4217           }
4218         ;
4219
4220 anonymous_method_expression
4221         : DELEGATE opt_anonymous_method_signature
4222           {
4223                 start_anonymous (false, (ParametersCompiled) $2, false, GetLocation ($1));
4224           }
4225           block
4226           {
4227                 $$ = end_anonymous ((ParametersBlock) $4);
4228           }
4229         | ASYNC DELEGATE opt_anonymous_method_signature
4230           {
4231                 start_anonymous (false, (ParametersCompiled) $3, true, GetLocation ($1));
4232           }
4233           block
4234           {
4235                 $$ = end_anonymous ((ParametersBlock) $5);
4236           }
4237         ;
4238
4239 opt_anonymous_method_signature
4240         : 
4241           {
4242                 $$ = ParametersCompiled.Undefined;
4243           } 
4244         | anonymous_method_signature
4245         ;
4246
4247 anonymous_method_signature
4248         : OPEN_PARENS
4249           {
4250                 valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out;
4251           }
4252           opt_formal_parameter_list CLOSE_PARENS
4253           {
4254                 valid_param_mod = 0;
4255                 $$ = $3;
4256           }
4257         ;
4258
4259 default_value_expression
4260         : DEFAULT open_parens_any type CLOSE_PARENS
4261           {
4262                 if (lang_version < LanguageVersion.ISO_2)
4263                         FeatureIsNotAvailable (GetLocation ($1), "default value expression");
4264
4265                 $$ = new DefaultValueExpression ((Expression) $3, GetLocation ($1));
4266                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
4267           }
4268         ;
4269
4270 unary_expression
4271         : primary_expression
4272         | BANG prefixed_unary_expression
4273           {
4274                 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, GetLocation ($1));
4275           }
4276         | TILDE prefixed_unary_expression
4277           {
4278                 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, GetLocation ($1));
4279           }
4280         | OPEN_PARENS_CAST type CLOSE_PARENS prefixed_unary_expression
4281           {
4282                 $$ = new Cast ((FullNamedExpression) $2, (Expression) $4, GetLocation ($1));
4283                 lbag.AddLocation ($$, GetLocation ($3));
4284           }
4285         | AWAIT prefixed_unary_expression
4286           {
4287                 if (!async_block) {
4288                          if (current_anonymous_method is LambdaExpression) {
4289                                 report.Error (4034, GetLocation ($1),
4290                                         "The `await' operator can only be used when its containing lambda expression is marked with the `async' modifier");
4291                         } else if (current_anonymous_method != null) {
4292                                 report.Error (4035, GetLocation ($1),
4293                                         "The `await' operator can only be used when its containing anonymous method is marked with the `async' modifier");
4294                         } else if (interactive_async != null) {
4295                                 current_block.Explicit.RegisterAsyncAwait ();
4296                                 interactive_async = true;
4297                         } else {
4298                                 report.Error (4033, GetLocation ($1),
4299                                         "The `await' operator can only be used when its containing method is marked with the `async' modifier");
4300                         }
4301                 } else {
4302                         current_block.Explicit.RegisterAsyncAwait ();
4303                 }
4304                 
4305                 $$ = new Await ((Expression) $2, GetLocation ($1));
4306           }
4307         | BANG error
4308           {
4309                 Error_SyntaxError (yyToken);
4310
4311                 $$ = new Unary (Unary.Operator.LogicalNot, null, GetLocation ($1));
4312           }
4313         | TILDE error
4314           {
4315                 Error_SyntaxError (yyToken);
4316
4317                 $$ = new Unary (Unary.Operator.OnesComplement, null, GetLocation ($1));
4318           }
4319         | OPEN_PARENS_CAST type CLOSE_PARENS error
4320           {
4321                 Error_SyntaxError (yyToken);
4322
4323                 $$ = new Cast ((FullNamedExpression) $2, null, GetLocation ($1));
4324                 lbag.AddLocation ($$, GetLocation ($3));
4325           }
4326         | AWAIT error
4327           {
4328                 Error_SyntaxError (yyToken);
4329
4330                 $$ = new Await (null, GetLocation ($1));
4331           }
4332         ;
4333
4334         //
4335         // The idea to split this out is from Rhys' grammar
4336         // to solve the problem with casts.
4337         //
4338 prefixed_unary_expression
4339         : unary_expression
4340         | PLUS prefixed_unary_expression
4341           { 
4342                 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, GetLocation ($1));
4343           } 
4344         | MINUS prefixed_unary_expression 
4345           { 
4346                 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, GetLocation ($1));
4347           }
4348         | OP_INC prefixed_unary_expression 
4349           {
4350                 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement, (Expression) $2, GetLocation ($1));
4351           }
4352         | OP_DEC prefixed_unary_expression 
4353           {
4354                 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement, (Expression) $2, GetLocation ($1));
4355           }
4356         | STAR prefixed_unary_expression
4357           {
4358                 $$ = new Indirection ((Expression) $2, GetLocation ($1));
4359           }
4360         | BITWISE_AND prefixed_unary_expression
4361           {
4362                 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, GetLocation ($1));
4363           }
4364         | PLUS error
4365           { 
4366                 Error_SyntaxError (yyToken);
4367
4368                 $$ = new Unary (Unary.Operator.UnaryPlus, null, GetLocation ($1));
4369           } 
4370         | MINUS error 
4371           { 
4372                 Error_SyntaxError (yyToken);
4373
4374                 $$ = new Unary (Unary.Operator.UnaryNegation, null, GetLocation ($1));
4375           }
4376         | OP_INC error 
4377           {
4378                 Error_SyntaxError (yyToken);
4379
4380                 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement, null, GetLocation ($1));
4381           }
4382         | OP_DEC error 
4383           {
4384                 Error_SyntaxError (yyToken);
4385
4386                 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement, null, GetLocation ($1));
4387           }
4388         | STAR error
4389           {
4390                 Error_SyntaxError (yyToken);
4391
4392                 $$ = new Indirection (null, GetLocation ($1));
4393           }
4394         | BITWISE_AND error
4395           {
4396                 Error_SyntaxError (yyToken);
4397
4398                 $$ = new Unary (Unary.Operator.AddressOf, null, GetLocation ($1));
4399           }
4400         ;
4401
4402 multiplicative_expression
4403         : prefixed_unary_expression
4404         | multiplicative_expression STAR prefixed_unary_expression
4405           {
4406                 $$ = new Binary (Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
4407                 lbag.AddLocation ($$, GetLocation ($2));
4408           }
4409         | multiplicative_expression DIV prefixed_unary_expression
4410           {
4411                 $$ = new Binary (Binary.Operator.Division, (Expression) $1, (Expression) $3);
4412                 lbag.AddLocation ($$, GetLocation ($2));
4413           }
4414         | multiplicative_expression PERCENT prefixed_unary_expression 
4415           {
4416                 $$ = new Binary (Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
4417                 lbag.AddLocation ($$, GetLocation ($2));
4418           }
4419         | multiplicative_expression STAR error
4420           {
4421                 Error_SyntaxError (yyToken);
4422
4423                 $$ = new Binary (Binary.Operator.Multiply, (Expression) $1, null);
4424                 lbag.AddLocation ($$, GetLocation ($2));
4425           }
4426         | multiplicative_expression DIV error
4427           {
4428                 Error_SyntaxError (yyToken);
4429
4430                 $$ = new Binary (Binary.Operator.Division, (Expression) $1, null);
4431                 lbag.AddLocation ($$, GetLocation ($2));
4432           }
4433         | multiplicative_expression PERCENT error 
4434           {
4435                 Error_SyntaxError (yyToken);
4436
4437                 $$ = new Binary (Binary.Operator.Modulus, (Expression) $1, null);
4438                 lbag.AddLocation ($$, GetLocation ($2));
4439           }
4440         ;
4441
4442 additive_expression
4443         : multiplicative_expression
4444         | additive_expression PLUS multiplicative_expression 
4445           {
4446                 $$ = new Binary (Binary.Operator.Addition, (Expression) $1, (Expression) $3);
4447                 lbag.AddLocation ($$, GetLocation ($2));
4448           }
4449         | additive_expression MINUS multiplicative_expression
4450           {
4451                 $$ = new Binary (Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
4452                 lbag.AddLocation ($$, GetLocation ($2));
4453           }
4454         | additive_expression PLUS error
4455           {
4456                 Error_SyntaxError (yyToken);
4457
4458                 $$ = new Binary (Binary.Operator.Addition, (Expression) $1, null);
4459                 lbag.AddLocation ($$, GetLocation ($2));
4460           }
4461         | additive_expression MINUS error
4462           {
4463                 Error_SyntaxError (yyToken);
4464
4465                 $$ = new Binary (Binary.Operator.Subtraction, (Expression) $1, null);
4466                 lbag.AddLocation ($$, GetLocation ($2));
4467           }
4468         | additive_expression AS type
4469           {
4470                 $$ = new As ((Expression) $1, (Expression) $3, GetLocation ($2));
4471           }
4472         | additive_expression IS pattern_type_expr opt_identifier
4473           {
4474                 var is_expr = new Is ((Expression) $1, (Expression) $3, GetLocation ($2));
4475                 if ($4 != null) {
4476                         if (lang_version != LanguageVersion.Experimental)
4477                                 FeatureIsNotAvailable (GetLocation ($4), "type pattern matching");
4478
4479                         var lt = (LocatedToken) $4;
4480                         is_expr.Variable = new LocalVariable (current_block, lt.Value, lt.Location);
4481                         current_block.AddLocalName (is_expr.Variable);
4482                 }
4483
4484                 $$ = is_expr;
4485           }
4486         | additive_expression IS pattern_expr
4487           {
4488                 var is_expr = new Is ((Expression) $1, (Expression) $3, GetLocation ($2));
4489                 if (lang_version != LanguageVersion.Experimental)
4490                         FeatureIsNotAvailable (GetLocation ($2), "pattern matching");
4491
4492                 $$ = is_expr;
4493           }
4494         | additive_expression AS error
4495           {
4496                 Error_SyntaxError (yyToken);
4497
4498                 $$ = new As ((Expression) $1, null, GetLocation ($2));
4499           }
4500         | additive_expression IS error
4501           {
4502                 Error_SyntaxError (yyToken);
4503
4504                 $$ = new Is ((Expression) $1, null, GetLocation ($2));
4505           }
4506         | AWAIT IS type
4507           {
4508                 var lt = (LocatedToken) $1;
4509                 $$ = new Is (new SimpleName (lt.Value, lt.Location), (Expression) $3, GetLocation ($2));
4510           }
4511         | AWAIT AS type
4512           {
4513                 var lt = (LocatedToken) $1;
4514                 $$ = new As (new SimpleName (lt.Value, lt.Location), (Expression) $3, GetLocation ($2));
4515           }
4516         ;
4517
4518 pattern_type_expr
4519         : variable_type
4520         ;
4521
4522 pattern_expr
4523         : literal
4524         | PLUS prefixed_unary_expression
4525           {
4526                 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, GetLocation ($1));
4527           }
4528         | MINUS prefixed_unary_expression
4529           {
4530                 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, GetLocation ($1));
4531           }
4532         | sizeof_expression
4533         | default_value_expression
4534         | OPEN_PARENS_CAST type CLOSE_PARENS prefixed_unary_expression
4535           {
4536                 $$ = new Cast ((FullNamedExpression) $2, (Expression) $4, GetLocation ($1));
4537                 lbag.AddLocation ($$, GetLocation ($3));
4538           }
4539         | STAR
4540           {
4541                 $$ = new WildcardPattern (GetLocation ($1));
4542           }
4543         | pattern_expr_invocation
4544         | pattern_property
4545         ;
4546
4547 pattern_expr_invocation
4548         : type_name_expression OPEN_PARENS opt_pattern_list CLOSE_PARENS
4549           {
4550                 $$ = new RecursivePattern ((ATypeNameExpression) $1, (Arguments) $3, GetLocation ($2));
4551           }
4552         ;
4553
4554 pattern_property
4555         : type_name_expression OPEN_BRACE pattern_property_list CLOSE_BRACE
4556           {
4557                 $$ = new PropertyPattern ((ATypeNameExpression) $1, (List<PropertyPatternMember>) $3, GetLocation ($2));
4558           }
4559         ;
4560
4561 pattern_property_list
4562         : pattern_property_entry
4563           {
4564                 var list = new List<PropertyPatternMember> ();
4565                 list.Add ((PropertyPatternMember) $1);
4566                 $$ = list;
4567           }
4568         | pattern_property_list COMMA pattern_property_entry
4569           {
4570                 var list = (List<PropertyPatternMember>) $1;
4571                 list.Add ((PropertyPatternMember) $3);
4572                 $$ = list;
4573           }
4574         ;
4575
4576 pattern_property_entry
4577         : identifier_inside_body IS pattern
4578           {
4579                 var lt = (LocatedToken) $1;
4580                 $$ = new PropertyPatternMember (lt.Value, (Expression) $3, lt.Location);
4581           }
4582         ;
4583
4584 pattern
4585         : pattern_expr
4586         | pattern_type_expr opt_identifier
4587           {
4588                 if ($2 != null) {
4589                         var lt = (LocatedToken) $2;
4590                         var variable = new LocalVariable (current_block, lt.Value, lt.Location);
4591                         current_block.AddLocalName (variable);
4592                 }
4593           }
4594         ;
4595
4596 opt_pattern_list
4597         : /* empty */
4598           {
4599                 $$ = new Arguments (0);
4600           }
4601         | pattern_list
4602         ;
4603
4604 pattern_list
4605         : pattern_argument
4606           {
4607                 Arguments args = new Arguments (4);
4608                 args.Add ((Argument) $1);
4609                 $$ = args;
4610           }
4611         | pattern_list COMMA pattern_argument
4612           {
4613                 Arguments args = (Arguments) $1;
4614                 if (args [args.Count - 1] is NamedArgument && !($3 is NamedArgument))
4615                         Error_NamedArgumentExpected ((NamedArgument) args [args.Count - 1]);
4616
4617                 args.Add ((Argument) $3);
4618                 $$ = args;
4619           }
4620         ;
4621
4622 pattern_argument
4623         : pattern
4624           {
4625                 $$ = new Argument ((Expression) $1);
4626           }
4627         | IDENTIFIER COLON pattern
4628           {
4629                 var lt = (LocatedToken) $1;
4630                 $$ = new NamedArgument (lt.Value, lt.Location, (Expression) $3);
4631           }
4632         ;
4633
4634 shift_expression
4635         : additive_expression
4636         | shift_expression OP_SHIFT_LEFT additive_expression
4637           {
4638                 $$ = new Binary (Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
4639                 lbag.AddLocation ($$, GetLocation ($2));
4640           }
4641         | shift_expression OP_SHIFT_RIGHT additive_expression
4642           {
4643                 $$ = new Binary (Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
4644                 lbag.AddLocation ($$, GetLocation ($2));
4645           }
4646         | shift_expression OP_SHIFT_LEFT error
4647           {
4648                 Error_SyntaxError (yyToken);
4649
4650                 $$ = new Binary (Binary.Operator.LeftShift, (Expression) $1, null);
4651                 lbag.AddLocation ($$, GetLocation ($2));
4652           }
4653         | shift_expression OP_SHIFT_RIGHT error
4654           {
4655                 Error_SyntaxError (yyToken);
4656
4657                 $$ = new Binary (Binary.Operator.RightShift, (Expression) $1, null);
4658                 lbag.AddLocation ($$, GetLocation ($2));
4659           }
4660         ; 
4661
4662 relational_expression
4663         : shift_expression
4664         | relational_expression OP_LT shift_expression
4665           {
4666                 $$ = new Binary (Binary.Operator.LessThan, (Expression) $1, (Expression) $3);
4667                 lbag.AddLocation ($$, GetLocation ($2));
4668           }
4669         | relational_expression OP_GT shift_expression
4670           {
4671                 $$ = new Binary (Binary.Operator.GreaterThan, (Expression) $1, (Expression) $3);
4672                 lbag.AddLocation ($$, GetLocation ($2));
4673           }
4674         | relational_expression OP_LE shift_expression
4675           {
4676                 $$ = new Binary (Binary.Operator.LessThanOrEqual, (Expression) $1, (Expression) $3);
4677                 lbag.AddLocation ($$, GetLocation ($2));
4678           }
4679         | relational_expression OP_GE shift_expression
4680           {
4681                 $$ = new Binary (Binary.Operator.GreaterThanOrEqual, (Expression) $1, (Expression) $3);
4682                 lbag.AddLocation ($$, GetLocation ($2));
4683           }
4684         | relational_expression OP_LT error
4685           {
4686                 Error_SyntaxError (yyToken);
4687
4688                 $$ = new Binary (Binary.Operator.LessThan, (Expression) $1, null);
4689                 lbag.AddLocation ($$, GetLocation ($2));
4690           }
4691         | relational_expression OP_GT error
4692           {
4693                 Error_SyntaxError (yyToken);
4694
4695                 $$ = new Binary (Binary.Operator.GreaterThan, (Expression) $1, null);
4696                 lbag.AddLocation ($$, GetLocation ($2));
4697           }
4698         | relational_expression OP_LE error
4699           {
4700                 Error_SyntaxError (yyToken);
4701
4702                 $$ = new Binary (Binary.Operator.LessThanOrEqual, (Expression) $1, null);
4703                 lbag.AddLocation ($$, GetLocation ($2));
4704           }
4705         | relational_expression OP_GE error
4706           {
4707                 Error_SyntaxError (yyToken);
4708
4709                 $$ = new Binary (Binary.Operator.GreaterThanOrEqual, (Expression) $1, null);
4710                 lbag.AddLocation ($$, GetLocation ($2));
4711           }
4712         ;
4713
4714 equality_expression
4715         : relational_expression
4716         | equality_expression OP_EQ relational_expression
4717           {
4718                 $$ = new Binary (Binary.Operator.Equality, (Expression) $1, (Expression) $3);
4719                 lbag.AddLocation ($$, GetLocation ($2));
4720           }
4721         | equality_expression OP_NE relational_expression
4722           {
4723                 $$ = new Binary (Binary.Operator.Inequality, (Expression) $1, (Expression) $3);
4724                 lbag.AddLocation ($$, GetLocation ($2));
4725           }
4726         | equality_expression OP_EQ error
4727           {
4728                 Error_SyntaxError (yyToken);
4729
4730                 $$ = new Binary (Binary.Operator.Equality, (Expression) $1, null);
4731                 lbag.AddLocation ($$, GetLocation ($2));
4732           }
4733         | equality_expression OP_NE error
4734           {
4735                 Error_SyntaxError (yyToken);
4736
4737                 $$ = new Binary (Binary.Operator.Inequality, (Expression) $1, null);
4738                 lbag.AddLocation ($$, GetLocation ($2));
4739           }
4740         ; 
4741
4742 and_expression
4743         : equality_expression
4744         | and_expression BITWISE_AND equality_expression
4745           {
4746                 $$ = new Binary (Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
4747                 lbag.AddLocation ($$, GetLocation ($2));
4748           }
4749         | and_expression BITWISE_AND error
4750           {
4751                 Error_SyntaxError (yyToken);
4752
4753                 $$ = new Binary (Binary.Operator.BitwiseAnd, (Expression) $1, null);
4754                 lbag.AddLocation ($$, GetLocation ($2));
4755           }
4756         ;
4757
4758 exclusive_or_expression
4759         : and_expression
4760         | exclusive_or_expression CARRET and_expression
4761           {
4762                 $$ = new Binary (Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
4763                 lbag.AddLocation ($$, GetLocation ($2));
4764           }
4765         | exclusive_or_expression CARRET error
4766           {
4767                 Error_SyntaxError (yyToken);
4768
4769                 $$ = new Binary (Binary.Operator.ExclusiveOr, (Expression) $1, null);
4770                 lbag.AddLocation ($$, GetLocation ($2));
4771           }
4772         ;
4773
4774 inclusive_or_expression
4775         : exclusive_or_expression
4776         | inclusive_or_expression BITWISE_OR exclusive_or_expression
4777           {
4778                 $$ = new Binary (Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
4779                 lbag.AddLocation ($$, GetLocation ($2));
4780           }
4781         | inclusive_or_expression BITWISE_OR error
4782           {
4783                 Error_SyntaxError (yyToken);
4784
4785                 $$ = new Binary (Binary.Operator.BitwiseOr, (Expression) $1, null);
4786                 lbag.AddLocation ($$, GetLocation ($2));
4787           }
4788         ;
4789
4790 conditional_and_expression
4791         : inclusive_or_expression
4792         | conditional_and_expression OP_AND inclusive_or_expression
4793           {
4794                 $$ = new Binary (Binary.Operator.LogicalAnd, (Expression) $1, (Expression) $3);
4795                 lbag.AddLocation ($$, GetLocation ($2));
4796           }
4797         | conditional_and_expression OP_AND error
4798           {
4799                 Error_SyntaxError (yyToken);
4800
4801                 $$ = new Binary (Binary.Operator.LogicalAnd, (Expression) $1, null);
4802                 lbag.AddLocation ($$, GetLocation ($2));
4803           }
4804         ;
4805
4806 conditional_or_expression
4807         : conditional_and_expression
4808         | conditional_or_expression OP_OR conditional_and_expression
4809           {
4810                 $$ = new Binary (Binary.Operator.LogicalOr, (Expression) $1, (Expression) $3);
4811                 lbag.AddLocation ($$, GetLocation ($2));
4812           }
4813         | conditional_or_expression OP_OR error
4814           {
4815                 Error_SyntaxError (yyToken);
4816
4817                 $$ = new Binary (Binary.Operator.LogicalOr, (Expression) $1, null);
4818                 lbag.AddLocation ($$, GetLocation ($2));
4819           }
4820         ;
4821         
4822 null_coalescing_expression
4823         : conditional_or_expression
4824         | conditional_or_expression OP_COALESCING null_coalescing_expression
4825           {
4826                 if (lang_version < LanguageVersion.ISO_2)
4827                         FeatureIsNotAvailable (GetLocation ($2), "null coalescing operator");
4828                         
4829                 $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $3);
4830                 lbag.AddLocation ($$, GetLocation ($2));
4831           }
4832         ;
4833
4834 conditional_expression
4835         : null_coalescing_expression
4836         | null_coalescing_expression INTERR expression COLON expression
4837           {
4838                 $$ = new Conditional (new BooleanExpression ((Expression) $1), (Expression) $3, (Expression) $5, GetLocation ($2));
4839                 lbag.AddLocation ($$, GetLocation ($4));
4840           }
4841         | null_coalescing_expression INTERR expression error
4842           {
4843                 Error_SyntaxError (yyToken);
4844
4845                 $$ = new Conditional (new BooleanExpression ((Expression) $1), (Expression) $3, null, GetLocation ($2));
4846           }
4847         | null_coalescing_expression INTERR expression COLON error
4848           {
4849                 Error_SyntaxError (yyToken);
4850
4851                 $$ = new Conditional (new BooleanExpression ((Expression) $1), (Expression) $3, null, GetLocation ($2));
4852                 lbag.AddLocation ($$, GetLocation ($4));
4853           }
4854         | null_coalescing_expression INTERR expression COLON CLOSE_BRACE
4855           {
4856                 Error_SyntaxError (Token.CLOSE_BRACE);
4857
4858                 $$ = new Conditional (new BooleanExpression ((Expression) $1), (Expression) $3, null, GetLocation ($2));
4859                 lbag.AddLocation ($$, GetLocation ($4));
4860                 lexer.putback ('}');
4861           }
4862         ;
4863
4864 assignment_expression
4865         : prefixed_unary_expression ASSIGN expression
4866           {
4867                 $$ = new SimpleAssign ((Expression) $1, (Expression) $3);
4868                 lbag.AddLocation ($$, GetLocation ($2));
4869           }
4870         | prefixed_unary_expression OP_MULT_ASSIGN expression
4871           {
4872                 $$ = new CompoundAssign (Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
4873                 lbag.AddLocation ($$, GetLocation ($2));
4874           }
4875         | prefixed_unary_expression OP_DIV_ASSIGN expression
4876           {
4877                 $$ = new CompoundAssign (Binary.Operator.Division, (Expression) $1, (Expression) $3);
4878                 lbag.AddLocation ($$, GetLocation ($2));
4879           }
4880         | prefixed_unary_expression OP_MOD_ASSIGN expression
4881           {
4882                 $$ = new CompoundAssign (Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
4883                 lbag.AddLocation ($$, GetLocation ($2));
4884           }
4885         | prefixed_unary_expression OP_ADD_ASSIGN expression
4886           {
4887                 $$ = new CompoundAssign (Binary.Operator.Addition, (Expression) $1, (Expression) $3);
4888                 lbag.AddLocation ($$, GetLocation ($2));
4889           }
4890         | prefixed_unary_expression OP_SUB_ASSIGN expression
4891           {
4892                 $$ = new CompoundAssign (Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
4893                 lbag.AddLocation ($$, GetLocation ($2));
4894           }
4895         | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
4896           {
4897                 $$ = new CompoundAssign (Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
4898                 lbag.AddLocation ($$, GetLocation ($2));
4899           }
4900         | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
4901           {
4902                 $$ = new CompoundAssign (Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
4903                 lbag.AddLocation ($$, GetLocation ($2));
4904           }
4905         | prefixed_unary_expression OP_AND_ASSIGN expression
4906           {
4907                 $$ = new CompoundAssign (Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
4908                 lbag.AddLocation ($$, GetLocation ($2));
4909           }
4910         | prefixed_unary_expression OP_OR_ASSIGN expression
4911           {
4912                 $$ = new CompoundAssign (Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
4913                 lbag.AddLocation ($$, GetLocation ($2));
4914           }
4915         | prefixed_unary_expression OP_XOR_ASSIGN expression
4916           {
4917                 $$ = new CompoundAssign (Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
4918                 lbag.AddLocation ($$, GetLocation ($2));
4919           }
4920         ;
4921
4922 lambda_parameter_list
4923         : lambda_parameter
4924           {
4925                 var pars = new List<Parameter> (4);
4926                 pars.Add ((Parameter) $1);
4927
4928                 $$ = pars;
4929           }
4930         | lambda_parameter_list COMMA lambda_parameter
4931           {
4932                 var pars = (List<Parameter>) $1;
4933                 Parameter p = (Parameter)$3;
4934                 if (pars[0].GetType () != p.GetType ()) {
4935                         report.Error (748, p.Location, "All lambda parameters must be typed either explicitly or implicitly");
4936                 }
4937                 
4938                 pars.Add (p);
4939                 $$ = pars;
4940           }
4941         ;
4942
4943 lambda_parameter
4944         : parameter_modifier parameter_type identifier_inside_body
4945           {
4946                 var lt = (LocatedToken) $3;
4947
4948                 $$ = new Parameter ((FullNamedExpression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
4949           }
4950         | parameter_type identifier_inside_body
4951           {
4952                 var lt = (LocatedToken) $2;
4953
4954                 $$ = new Parameter ((FullNamedExpression) $1, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
4955           }
4956         | IDENTIFIER
4957           {
4958                 var lt = (LocatedToken) $1;
4959                 $$ = new ImplicitLambdaParameter (lt.Value, lt.Location);
4960           }
4961         | AWAIT
4962           {
4963                 var lt = (LocatedToken) Error_AwaitAsIdentifier ($1);
4964                 $$ = new ImplicitLambdaParameter (lt.Value, lt.Location);
4965           }
4966         ;
4967
4968 opt_lambda_parameter_list
4969         : /* empty */                   { $$ = ParametersCompiled.EmptyReadOnlyParameters; }
4970         | lambda_parameter_list         { 
4971                 var pars_list = (List<Parameter>) $1;
4972                 $$ = new ParametersCompiled (pars_list.ToArray ());
4973           }
4974         ;
4975
4976 lambda_expression_body
4977         : {
4978                 start_block (Location.Null);
4979           }
4980           expression    // All expressions must handle error or current block won't be restored and breaking ast completely
4981           {
4982                 Block b = end_block (Location.Null);
4983                 b.IsCompilerGenerated = true;
4984                 b.AddStatement (new ContextualReturn ((Expression) $2));
4985                 $$ = b;
4986           } 
4987         | block
4988         | error
4989           {
4990                 // Handles only cases like foo = x.FirstOrDefault (l => );
4991                 // where we must restore current_variable
4992                 Block b = end_block (Location.Null);
4993                 b.IsCompilerGenerated = true;
4994
4995                 Error_SyntaxError (yyToken);
4996                 $$ = null;
4997           }
4998         ;
4999
5000 expression_or_error
5001         : expression
5002         | error
5003           {
5004                 Error_SyntaxError (yyToken);
5005                 $$ = null;
5006           }
5007         ;
5008         
5009 lambda_expression
5010         : IDENTIFIER ARROW 
5011           {
5012                 var lt = (LocatedToken) $1;     
5013                 Parameter p = new ImplicitLambdaParameter (lt.Value, lt.Location);
5014                 start_anonymous (true, new ParametersCompiled (p), false, lt.Location);
5015           }
5016           lambda_expression_body
5017           {
5018                 $$ = end_anonymous ((ParametersBlock) $4);
5019                 lbag.AddLocation ($$, GetLocation ($2));
5020           }
5021         | AWAIT ARROW
5022           {
5023                 var lt = (LocatedToken) Error_AwaitAsIdentifier ($1);
5024                 Parameter p = new ImplicitLambdaParameter (lt.Value, lt.Location);
5025                 start_anonymous (true, new ParametersCompiled (p), false, lt.Location);
5026           }
5027           lambda_expression_body
5028           {
5029                 $$ = end_anonymous ((ParametersBlock) $4);
5030                 lbag.AddLocation ($$, GetLocation ($2));
5031           }
5032         | ASYNC identifier_inside_body ARROW
5033           {
5034                 var lt = (LocatedToken) $2;
5035                 Parameter p = new ImplicitLambdaParameter (lt.Value, lt.Location);
5036                 start_anonymous (true, new ParametersCompiled (p), true, lt.Location);
5037           }
5038           lambda_expression_body
5039           {
5040                 $$ = end_anonymous ((ParametersBlock) $5);
5041                 lbag.AddLocation ($$, GetLocation ($1), GetLocation ($3));
5042           }
5043         | OPEN_PARENS_LAMBDA
5044           {
5045                 valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out;
5046           }
5047           opt_lambda_parameter_list CLOSE_PARENS ARROW 
5048           {
5049                 valid_param_mod = 0;
5050                 start_anonymous (true, (ParametersCompiled) $3, false, GetLocation ($1));
5051           }
5052           lambda_expression_body
5053           {
5054                 $$ = end_anonymous ((ParametersBlock) $7);
5055                 lbag.AddLocation ($$, GetLocation ($1), GetLocation ($4), GetLocation ($5));
5056           }
5057         | ASYNC OPEN_PARENS_LAMBDA
5058           {
5059                 valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out;          
5060           }
5061           opt_lambda_parameter_list CLOSE_PARENS ARROW 
5062           {
5063                 valid_param_mod = 0;
5064                 start_anonymous (true, (ParametersCompiled) $4, true, GetLocation ($1));
5065           }
5066           lambda_expression_body
5067           {
5068                 $$ = end_anonymous ((ParametersBlock) $8);
5069                 lbag.AddLocation ($$, GetLocation ($1), GetLocation ($2), GetLocation ($5), GetLocation ($6));
5070           }
5071         ;
5072
5073 expression
5074         : assignment_expression 
5075         | non_assignment_expression
5076         ;
5077         
5078 non_assignment_expression
5079         : conditional_expression
5080         | lambda_expression
5081         | query_expression
5082         | ARGLIST
5083           {
5084                 $$ = new ArglistAccess (GetLocation ($1));
5085           }
5086         ;
5087         
5088 undocumented_expressions
5089         : REFVALUE OPEN_PARENS non_assignment_expression COMMA type CLOSE_PARENS
5090           {
5091                 $$ = new RefValueExpr ((Expression) $3, (FullNamedExpression) $5, GetLocation ($1));
5092                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4), GetLocation ($6));
5093           }
5094         | REFTYPE open_parens_any expression CLOSE_PARENS
5095           {
5096                 $$ = new RefTypeExpr ((Expression) $3, GetLocation ($1));
5097                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
5098           }
5099         | MAKEREF open_parens_any expression CLOSE_PARENS
5100           {
5101                 $$ = new MakeRefExpr ((Expression) $3, GetLocation ($1));
5102                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));        
5103           }
5104         ;
5105
5106 constant_expression
5107         : expression
5108         ;
5109
5110 boolean_expression
5111         : expression
5112           {
5113                 $$ = new BooleanExpression ((Expression) $1);
5114           }
5115         ;
5116
5117 opt_primary_parameters
5118         : /* empty */
5119           {
5120                 $$ = null;
5121           }
5122         | primary_parameters
5123         ;
5124
5125 primary_parameters
5126         : OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
5127           {
5128                 $$ = $2;
5129
5130                 // Cannot use opt_formal_parameter_list because it can be shared instance for empty parameters
5131                 lbag.AppendToMember (current_container, GetLocation ($1), GetLocation ($3));
5132
5133                 if (lang_version != LanguageVersion.Experimental)
5134                         FeatureIsNotAvailable (GetLocation ($1), "primary constructor");
5135           }
5136         ;
5137
5138 opt_primary_parameters_with_class_base
5139         : /* empty */
5140           {
5141                 $$ = null;
5142           }
5143         | class_base
5144           {
5145                 $$ = null;
5146           }
5147         | primary_parameters
5148           {
5149                 $$ = $1;
5150           }
5151         | primary_parameters class_base
5152           {
5153                 $$ = $1;
5154           }
5155         | primary_parameters class_base OPEN_PARENS
5156           {
5157                 ++lexer.parsing_block;
5158                 current_type.PrimaryConstructorBaseArgumentsStart = GetLocation ($3);
5159           }
5160           opt_argument_list CLOSE_PARENS
5161           {
5162                 lbag.AppendToMember (current_container, GetLocation ($6));
5163                 current_type.PrimaryConstructorBaseArguments = (Arguments) $5;
5164                 --lexer.parsing_block;
5165
5166                 $$ = $1;
5167           }
5168         ;
5169
5170 //
5171 // 10 classes
5172 //
5173 class_declaration
5174         : opt_attributes
5175           opt_modifiers
5176           opt_partial
5177           CLASS
5178           {
5179           }
5180           type_declaration_name
5181           {
5182                 lexer.ConstraintsParsing = true;
5183
5184                 Class c = new Class (current_container, (MemberName) $6, (Modifiers) $2, (Attributes) $1);
5185                 if (((c.ModFlags & Modifiers.STATIC) != 0) && lang_version == LanguageVersion.ISO_1) {
5186                         FeatureIsNotAvailable (c.Location, "static classes");
5187                 }
5188                         
5189                 push_current_container (c, $3);
5190                 valid_param_mod = ParameterModifierType.PrimaryConstructor;
5191           }
5192           opt_primary_parameters_with_class_base
5193           opt_type_parameter_constraints_clauses
5194           {
5195                 valid_param_mod = 0;
5196                 lexer.ConstraintsParsing = false;
5197
5198                 if ($8 != null)
5199                         current_type.PrimaryConstructorParameters = (ParametersCompiled) $8;
5200
5201                 if ($9 != null)
5202                         current_container.SetConstraints ((List<Constraints>) $9);
5203                 lbag.AddMember (current_container, mod_locations, GetLocation ($4));
5204
5205                 if (doc_support) {
5206                         current_container.PartialContainer.DocComment = Lexer.consume_doc_comment ();
5207                         Lexer.doc_state = XmlCommentState.Allowed;
5208                 }
5209                 
5210                 lexer.parsing_modifiers = true;
5211           }
5212           OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
5213           {
5214                 --lexer.parsing_declaration;
5215                 if (doc_support)
5216                         Lexer.doc_state = XmlCommentState.Allowed;
5217           }
5218           opt_semicolon 
5219           {
5220                 if ($15 == null) {
5221                         lbag.AppendToMember (current_container, GetLocation ($11), GetLocation ($13));
5222                 } else {
5223                         lbag.AppendToMember (current_container, GetLocation ($11), GetLocation ($13), GetLocation ($15));
5224                 }
5225                 $$ = pop_current_class ();
5226           }
5227         ;       
5228
5229 opt_partial
5230         : /* empty */
5231           { $$ = null; }
5232         | PARTIAL
5233           { $$ = $1; } // location
5234         ;
5235
5236 opt_modifiers
5237         : /* empty */
5238           {
5239             mod_locations = null;
5240                 $$ = ModifierNone;
5241                 lexer.parsing_modifiers = false;
5242           }
5243         | modifiers
5244           {
5245                 lexer.parsing_modifiers = false;                
5246           }
5247         ;
5248
5249 modifiers
5250         : modifier
5251         | modifiers modifier
5252           { 
5253                 var m1 = (Modifiers) $1;
5254                 var m2 = (Modifiers) $2;
5255
5256                 if ((m1 & m2) != 0) {
5257                         report.Error (1004, lexer.Location - ModifiersExtensions.Name (m2).Length,
5258                                 "Duplicate `{0}' modifier", ModifiersExtensions.Name (m2));
5259                 } else if ((m2 & Modifiers.AccessibilityMask) != 0 && (m1 & Modifiers.AccessibilityMask) != 0 &&
5260                         ((m2 | m1 & Modifiers.AccessibilityMask) != (Modifiers.PROTECTED | Modifiers.INTERNAL))) {
5261                         report.Error (107, lexer.Location - ModifiersExtensions.Name (m2).Length,
5262                                 "More than one protection modifier specified");
5263                 }
5264                 
5265                 $$ = m1 | m2;
5266           }
5267         ;
5268
5269 modifier
5270         : NEW
5271           {
5272                 $$ = Modifiers.NEW;
5273                 StoreModifierLocation ($$, GetLocation ($1));
5274                 
5275                 if (current_container.Kind == MemberKind.Namespace)
5276                         report.Error (1530, GetLocation ($1), "Keyword `new' is not allowed on namespace elements");
5277           }
5278         | PUBLIC
5279           {
5280                 $$ = Modifiers.PUBLIC;
5281                 StoreModifierLocation ($$, GetLocation ($1));
5282           }
5283         | PROTECTED
5284           {
5285                 $$ = Modifiers.PROTECTED;
5286                 StoreModifierLocation ($$, GetLocation ($1));
5287           }
5288         | INTERNAL
5289           {
5290                 $$ = Modifiers.INTERNAL;
5291                 StoreModifierLocation ($$, GetLocation ($1));
5292           }
5293         | PRIVATE
5294           {
5295                 $$ = Modifiers.PRIVATE;
5296                 StoreModifierLocation ($$, GetLocation ($1));
5297           }
5298         | ABSTRACT
5299           {
5300                 $$ = Modifiers.ABSTRACT;
5301                 StoreModifierLocation ($$, GetLocation ($1));
5302           }
5303         | SEALED
5304           {
5305                 $$ = Modifiers.SEALED;
5306                 StoreModifierLocation ($$, GetLocation ($1));
5307           }
5308         | STATIC
5309           {
5310                 $$ = Modifiers.STATIC;
5311                 StoreModifierLocation ($$, GetLocation ($1));
5312           }
5313         | READONLY
5314           {
5315                 $$ = Modifiers.READONLY;
5316                 StoreModifierLocation ($$, GetLocation ($1));
5317           }
5318         | VIRTUAL
5319           {
5320                 $$ = Modifiers.VIRTUAL;
5321                 StoreModifierLocation ($$, GetLocation ($1));
5322           }
5323         | OVERRIDE
5324           {
5325                 $$ = Modifiers.OVERRIDE;
5326                 StoreModifierLocation ($$, GetLocation ($1));
5327           }
5328         | EXTERN
5329           {
5330                 $$ = Modifiers.EXTERN;
5331                 StoreModifierLocation ($$, GetLocation ($1));
5332           }
5333         | VOLATILE
5334           {
5335                 $$ = Modifiers.VOLATILE;
5336                 StoreModifierLocation ($$, GetLocation ($1));
5337           }
5338         | UNSAFE
5339           {
5340                 $$ = Modifiers.UNSAFE;
5341                 StoreModifierLocation ($$, GetLocation ($1));
5342                 if (!settings.Unsafe)
5343                         Error_UnsafeCodeNotAllowed (GetLocation ($1));
5344           }
5345         | ASYNC
5346           {
5347                 $$ = Modifiers.ASYNC;
5348                 StoreModifierLocation ($$, GetLocation ($1));
5349           }
5350         ;
5351         
5352 opt_class_base
5353         : /* empty */
5354         | class_base
5355         ;
5356
5357 class_base
5358         : COLON type_list
5359          {
5360                 current_type.SetBaseTypes ((List<FullNamedExpression>) $2);
5361          }
5362         | COLON type_list error
5363           {
5364                 Error_SyntaxError (yyToken);
5365
5366                 current_type.SetBaseTypes ((List<FullNamedExpression>) $2);
5367           }
5368         ;
5369
5370 opt_type_parameter_constraints_clauses
5371         : /* empty */
5372         | type_parameter_constraints_clauses 
5373           {
5374                 $$ = $1;
5375           }
5376         ;
5377
5378 type_parameter_constraints_clauses
5379         : type_parameter_constraints_clause
5380           {
5381                 var constraints = new List<Constraints> (1);
5382                 constraints.Add ((Constraints) $1);
5383                 $$ = constraints;
5384           }
5385         | type_parameter_constraints_clauses type_parameter_constraints_clause
5386           {
5387                 var constraints = (List<Constraints>) $1;
5388                 Constraints new_constraint = (Constraints)$2;
5389
5390                 foreach (Constraints c in constraints) {
5391                         if (new_constraint.TypeParameter.Value == c.TypeParameter.Value) {
5392                                 report.Error (409, new_constraint.Location,
5393                                         "A constraint clause has already been specified for type parameter `{0}'",
5394                                         new_constraint.TypeParameter.Value);
5395                         }
5396                 }
5397
5398                 constraints.Add (new_constraint);
5399                 $$ = constraints;
5400           }
5401         ; 
5402
5403 type_parameter_constraints_clause
5404         : WHERE IDENTIFIER COLON type_parameter_constraints
5405           {
5406                 var lt = (LocatedToken) $2;
5407                 $$ = new Constraints (new SimpleMemberName (lt.Value, lt.Location), (List<FullNamedExpression>) $4, GetLocation ($1));
5408                 lbag.AddLocation ($$, GetLocation ($3));
5409           }
5410         | WHERE IDENTIFIER error
5411           {
5412                 Error_SyntaxError (yyToken);
5413           
5414                 var lt = (LocatedToken) $2;
5415                 $$ = new Constraints (new SimpleMemberName (lt.Value, lt.Location), null, GetLocation ($1));
5416           }
5417         ; 
5418
5419 type_parameter_constraints
5420         : type_parameter_constraint
5421           {
5422                 var constraints = new List<FullNamedExpression> (1);
5423                 constraints.Add ((FullNamedExpression) $1);
5424                 $$ = constraints;
5425           }
5426         | type_parameter_constraints COMMA type_parameter_constraint
5427           {
5428                 var constraints = (List<FullNamedExpression>) $1;
5429                 var prev = constraints [constraints.Count - 1] as SpecialContraintExpr;
5430                 if (prev != null && (prev.Constraint & SpecialConstraint.Constructor) != 0) {                   
5431                         report.Error (401, GetLocation ($2), "The `new()' constraint must be the last constraint specified");
5432                 }
5433                 
5434                 prev = $3 as SpecialContraintExpr;
5435                 if (prev != null) {
5436                         if ((prev.Constraint & (SpecialConstraint.Class | SpecialConstraint.Struct)) != 0) {
5437                                 report.Error (449, prev.Location, "The `class' or `struct' constraint must be the first constraint specified");                 
5438                         } else {
5439                                 prev = constraints [0] as SpecialContraintExpr;
5440                                 if (prev != null && (prev.Constraint & SpecialConstraint.Struct) != 0) {                        
5441                                         report.Error (451, GetLocation ($3), "The `new()' constraint cannot be used with the `struct' constraint");
5442                                 }
5443                         }
5444                 }
5445
5446                 constraints.Add ((FullNamedExpression) $3);
5447                 $$ = constraints;
5448           }
5449         ;
5450
5451 type_parameter_constraint
5452         : type
5453           {
5454                 if ($1 is ComposedCast)
5455                         report.Error (706, GetLocation ($1), "Invalid constraint type `{0}'", ((ComposedCast)$1).GetSignatureForError ());
5456           
5457                 $$ = $1;
5458           }
5459         | NEW OPEN_PARENS CLOSE_PARENS
5460           {
5461                 $$ = new SpecialContraintExpr (SpecialConstraint.Constructor, GetLocation ($1));
5462                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($3));
5463           }
5464         | CLASS
5465           {
5466                 $$ = new SpecialContraintExpr (SpecialConstraint.Class, GetLocation ($1));
5467           }
5468         | STRUCT
5469           {
5470                 $$ = new SpecialContraintExpr (SpecialConstraint.Struct, GetLocation ($1));
5471           }
5472         ;
5473
5474 opt_type_parameter_variance
5475         : /* empty */
5476           {
5477                 $$ = null;
5478           }
5479         | type_parameter_variance
5480           {
5481                 if (lang_version <= LanguageVersion.V_3)
5482                         FeatureIsNotAvailable (lexer.Location, "generic type variance");
5483                 
5484                 $$ = $1;
5485           }
5486         ;
5487
5488 type_parameter_variance
5489         : OUT
5490           {
5491                 $$ = new VarianceDecl (Variance.Covariant, GetLocation ($1));
5492           }
5493         | IN
5494           {
5495                 $$ = new VarianceDecl (Variance.Contravariant, GetLocation ($1));
5496           }
5497         ;
5498
5499 //
5500 // Statements (8.2)
5501 //
5502
5503 //
5504 // A block is "contained" on the following places:
5505 //      method_body
5506 //      property_declaration as part of the accessor body (get/set)
5507 //      operator_declaration
5508 //      constructor_declaration
5509 //      destructor_declaration
5510 //      event_declaration as part of add_accessor_declaration or remove_accessor_declaration
5511 //      
5512 block
5513         : OPEN_BRACE  
5514           {
5515                 ++lexer.parsing_block;
5516                 start_block (GetLocation ($1));
5517           } 
5518           opt_statement_list block_end
5519           {
5520                 $$ = $4;
5521           }
5522         ;
5523
5524 block_end 
5525         : CLOSE_BRACE 
5526           {
5527                 --lexer.parsing_block;
5528                 $$ = end_block (GetLocation ($1));
5529           }
5530         | COMPLETE_COMPLETION
5531           {
5532                 --lexer.parsing_block;
5533                 $$ = end_block (lexer.Location);
5534           }
5535         ;
5536
5537
5538 block_prepared
5539         : OPEN_BRACE
5540           {
5541                 ++lexer.parsing_block;
5542                 current_block.StartLocation = GetLocation ($1);
5543           }
5544           opt_statement_list CLOSE_BRACE 
5545           {
5546                 --lexer.parsing_block;
5547                 $$ = end_block (GetLocation ($4));
5548           }
5549         ;
5550
5551 opt_statement_list
5552         : /* empty */
5553         | statement_list 
5554         ;
5555
5556 statement_list
5557         : statement
5558         | statement_list statement
5559         ;
5560
5561 statement
5562         : block_variable_declaration
5563           {
5564                 current_block.AddStatement ((Statement) $1);
5565           }
5566         | valid_declaration_statement
5567           {
5568                 current_block.AddStatement ((Statement) $1);
5569           }
5570         | labeled_statement
5571         | error
5572           {
5573                 Error_SyntaxError (yyToken);
5574                 $$ = null;
5575           }
5576         ;
5577
5578 //
5579 // The interactive_statement and its derivatives are only 
5580 // used to provide a special version of `expression_statement'
5581 // that has a side effect of assigning the expression to
5582 // $retval
5583 //
5584 interactive_statement_list
5585         : interactive_statement
5586         | interactive_statement_list interactive_statement
5587         ;
5588
5589 interactive_statement
5590         : block_variable_declaration
5591           {
5592                 current_block.AddStatement ((Statement) $1);
5593           }
5594         | interactive_valid_declaration_statement
5595           {
5596                 current_block.AddStatement ((Statement) $1);
5597           }
5598         | labeled_statement
5599         ;
5600
5601 valid_declaration_statement
5602         : block
5603         | empty_statement
5604         | expression_statement
5605         | selection_statement
5606         | iteration_statement
5607         | jump_statement                  
5608         | try_statement
5609         | checked_statement
5610         | unchecked_statement
5611         | lock_statement
5612         | using_statement
5613         | unsafe_statement
5614         | fixed_statement
5615         ;
5616
5617 interactive_valid_declaration_statement
5618         : block
5619         | empty_statement
5620         | interactive_expression_statement
5621         | selection_statement
5622         | iteration_statement
5623         | jump_statement                  
5624         | try_statement
5625         | checked_statement
5626         | unchecked_statement
5627         | lock_statement
5628         | using_statement
5629         | unsafe_statement
5630         | fixed_statement
5631         ;
5632
5633 embedded_statement
5634         : valid_declaration_statement
5635         | block_variable_declaration
5636           {
5637                   report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
5638                   $$ = null;
5639           }
5640         | labeled_statement
5641           {
5642                   report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
5643                   $$ = null;
5644           }
5645         | error
5646           {
5647                 Error_SyntaxError (yyToken);
5648                 $$ = new EmptyStatement (GetLocation ($1));
5649           }
5650         ;
5651
5652 empty_statement
5653         : SEMICOLON
5654           {
5655                 // Uses lexer.Location because semicolon location is not kept in quick mode
5656                 $$ = new EmptyStatement (lexer.Location);
5657           }
5658         ;
5659
5660 labeled_statement
5661         : identifier_inside_body COLON 
5662           {
5663                 var lt = (LocatedToken) $1;
5664                 LabeledStatement labeled = new LabeledStatement (lt.Value, current_block, lt.Location);
5665                 lbag.AddLocation (labeled, GetLocation ($2));
5666                 current_block.AddLabel (labeled);
5667                 current_block.AddStatement (labeled);
5668           }
5669           statement
5670         ;
5671
5672 variable_type
5673         : variable_type_simple
5674         | variable_type_simple rank_specifiers
5675           {
5676                 if ($1 is VarExpr)
5677                         $1 = new SimpleName ("var", ((VarExpr) $1).Location);
5678           
5679                 $$ = new ComposedCast ((FullNamedExpression) $1, (ComposedTypeSpecifier) $2);
5680           }
5681         ;
5682
5683 /* 
5684  * The following is from Rhys' grammar:
5685  * > Types in local variable declarations must be recognized as 
5686  * > expressions to prevent reduce/reduce errors in the grammar.
5687  * > The expressions are converted into types during semantic analysis.
5688  */
5689 variable_type_simple
5690         : type_name_expression opt_nullable
5691           { 
5692                 // Ok, the above "primary_expression" is there to get rid of
5693                 // both reduce/reduce and shift/reduces in the grammar, it should
5694                 // really just be "type_name".  If you use type_name, a reduce/reduce
5695                 // creeps up.  If you use namespace_or_type_name (which is all we need
5696                 // really) two shift/reduces appear.
5697                 // 
5698
5699                 // So the super-trick is that primary_expression
5700                 // can only be either a SimpleName or a MemberAccess. 
5701                 // The MemberAccess case arises when you have a fully qualified type-name like :
5702                 // Foo.Bar.Blah i;
5703                 // SimpleName is when you have
5704                 // Blah i;
5705                 
5706                 var expr = (ATypeNameExpression) $1;
5707                 if ($2 == null) {
5708                         if (expr.Name == "var" && expr is SimpleName)
5709                                 $$ = new VarExpr (expr.Location);
5710                         else
5711                                 $$ = $1;
5712                 } else {
5713                         $$ = new ComposedCast (expr, (ComposedTypeSpecifier) $2);
5714                 }
5715           }
5716         | type_name_expression pointer_stars
5717           {
5718                 var expr = (ATypeNameExpression) $1;
5719                 $$ = new ComposedCast (expr, (ComposedTypeSpecifier) $2);
5720           }
5721         | builtin_type_expression
5722         | void_invalid
5723         ;
5724         
5725 pointer_stars
5726         : pointer_star
5727         | pointer_star pointer_stars
5728           {
5729                 ((ComposedTypeSpecifier) $1).Next = (ComposedTypeSpecifier) $2;
5730                 $$ = $1;
5731           }       
5732         ;
5733
5734 pointer_star
5735         : STAR
5736           {
5737                 $$ = ComposedTypeSpecifier.CreatePointer (GetLocation ($1));
5738           }
5739         ;
5740
5741 identifier_inside_body
5742         : IDENTIFIER
5743         | AWAIT
5744           {
5745                 $$ = Error_AwaitAsIdentifier ($1);
5746           }
5747         ;
5748
5749 block_variable_declaration
5750         : variable_type identifier_inside_body
5751           {
5752                 var lt = (LocatedToken) $2;
5753                 var li = new LocalVariable (current_block, lt.Value, lt.Location);
5754                 current_block.AddLocalName (li);
5755                 current_variable = new BlockVariable ((FullNamedExpression) $1, li);
5756           }
5757           opt_local_variable_initializer opt_variable_declarators SEMICOLON
5758           {
5759                 $$ = current_variable;
5760                 current_variable = null;
5761                 if ($4 != null)
5762                         lbag.AddLocation ($$, PopLocation (), GetLocation ($6));
5763                 else
5764                         lbag.AddLocation ($$, GetLocation ($6));
5765           }
5766         | CONST variable_type identifier_inside_body
5767           {
5768                 var lt = (LocatedToken) $3;
5769                 var li = new LocalVariable (current_block, lt.Value, LocalVariable.Flags.Constant, lt.Location);
5770                 current_block.AddLocalName (li);
5771                 current_variable = new BlockConstant ((FullNamedExpression) $2, li);
5772           }
5773           const_variable_initializer opt_const_declarators SEMICOLON
5774           {
5775                 $$ = current_variable;
5776                 current_variable = null;
5777                 lbag.AddLocation ($$, GetLocation ($1), GetLocation ($7));
5778           }
5779         ;
5780
5781 opt_local_variable_initializer
5782         : /* empty */
5783         | ASSIGN block_variable_initializer
5784           {
5785                 current_variable.Initializer = (Expression) $2;
5786                 PushLocation (GetLocation ($1));
5787                 $$ = current_variable;
5788           }
5789         | error
5790           {
5791                 if (yyToken == Token.OPEN_BRACKET_EXPR) {
5792                         report.Error (650, lexer.Location,
5793                                 "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type");
5794                 } else {
5795                         Error_SyntaxError (yyToken);
5796                 }
5797           }
5798         ;
5799
5800 opt_variable_declarators
5801         : /* empty */
5802         | variable_declarators
5803         ;
5804         
5805 opt_using_or_fixed_variable_declarators
5806         : /* empty */
5807         | variable_declarators
5808           {
5809                 foreach (var d in current_variable.Declarators) {
5810                         if (d.Initializer == null)
5811                                 Error_MissingInitializer (d.Variable.Location);
5812                 }
5813           }
5814         ;       
5815         
5816 variable_declarators
5817         : variable_declarator
5818         | variable_declarators variable_declarator
5819         ;
5820         
5821 variable_declarator
5822         : COMMA identifier_inside_body
5823           {
5824                 var lt = (LocatedToken) $2;       
5825                 var li = new LocalVariable (current_variable.Variable, lt.Value, lt.Location);
5826                 var d = new BlockVariableDeclarator (li, null);
5827                 current_variable.AddDeclarator (d);
5828                 current_block.AddLocalName (li);
5829                 lbag.AddLocation (d, GetLocation ($1));
5830           }
5831         | COMMA identifier_inside_body ASSIGN block_variable_initializer
5832           {
5833                 var lt = (LocatedToken) $2;       
5834                 var li = new LocalVariable (current_variable.Variable, lt.Value, lt.Location);
5835                 var d = new BlockVariableDeclarator (li, (Expression) $4);
5836                 current_variable.AddDeclarator (d);
5837                 current_block.AddLocalName (li);
5838                 lbag.AddLocation (d, GetLocation ($1), GetLocation ($3));
5839           }
5840         ;
5841         
5842 const_variable_initializer
5843         : /* empty */
5844           {
5845                 report.Error (145, lexer.Location, "A const field requires a value to be provided");
5846           }
5847         | ASSIGN constant_initializer_expr 
5848           {
5849                 current_variable.Initializer = (Expression) $2;
5850           }
5851         ;
5852         
5853 opt_const_declarators
5854         : /* empty */
5855         | const_declarators
5856         ;
5857         
5858 const_declarators
5859         : const_declarator
5860         | const_declarators const_declarator
5861         ;
5862         
5863 const_declarator
5864         : COMMA identifier_inside_body ASSIGN constant_initializer_expr
5865           {
5866                 var lt = (LocatedToken) $2;       
5867                 var li = new LocalVariable (current_block, lt.Value, LocalVariable.Flags.Constant, lt.Location);
5868                 var d = new BlockVariableDeclarator (li, (Expression) $4);
5869                 current_variable.AddDeclarator (d);
5870                 current_block.AddLocalName (li);
5871                 lbag.AddLocation (d, GetLocation ($1), GetLocation ($3));
5872           }
5873         ;
5874         
5875 block_variable_initializer
5876         : variable_initializer
5877         | STACKALLOC simple_type OPEN_BRACKET_EXPR expression CLOSE_BRACKET
5878           {
5879                 $$ = new StackAlloc ((Expression) $2, (Expression) $4, GetLocation ($1));
5880                 lbag.AddLocation ($$, GetLocation ($3), GetLocation ($5));
5881           }
5882         | STACKALLOC simple_type
5883           {
5884                 report.Error (1575, GetLocation ($1), "A stackalloc expression requires [] after type");
5885                 $$ = new StackAlloc ((Expression) $2, null, GetLocation ($1));          
5886           }
5887         ;
5888
5889 expression_statement
5890         : statement_expression SEMICOLON
5891           {
5892                 $$ = $1;
5893                 lbag.AddStatement ($$, GetLocation ($2));
5894           }
5895         | statement_expression COMPLETE_COMPLETION { $$ = $1; }
5896         | statement_expression CLOSE_BRACE
5897           {
5898                 $$ = $1;
5899                 report.Error (1002, GetLocation ($2), "; expected");
5900                 lexer.putback ('}');
5901           }
5902         ;
5903
5904 interactive_expression_statement
5905         : interactive_statement_expression SEMICOLON { $$ = $1; }
5906         | interactive_statement_expression COMPLETE_COMPLETION { $$ = $1; }
5907         ;
5908
5909         //
5910         // We have to do the wrapping here and not in the case above,
5911         // because statement_expression is used for example in for_statement
5912         //
5913 statement_expression
5914         : expression
5915           {
5916                 ExpressionStatement s = $1 as ExpressionStatement;
5917                 if (s == null) {
5918                         var expr = $1 as Expression;
5919                         $$ = new StatementErrorExpression (expr);
5920                 } else {
5921                         $$ = new StatementExpression (s);
5922                 }
5923           }
5924         ;
5925
5926 interactive_statement_expression
5927         : expression
5928           {
5929                 Expression expr = (Expression) $1;
5930                 $$ = new StatementExpression (new OptionalAssign (expr, lexer.Location));
5931           }
5932         | error
5933           {
5934                 Error_SyntaxError (yyToken);
5935                 $$ = new EmptyStatement (GetLocation ($1));
5936           }
5937         ;
5938         
5939 selection_statement
5940         : if_statement
5941         | switch_statement
5942         ; 
5943
5944 if_statement
5945         : IF open_parens_any boolean_expression CLOSE_PARENS 
5946           embedded_statement
5947           { 
5948                 if ($5 is EmptyStatement)
5949                         Warning_EmptyStatement (GetLocation ($5));
5950                 
5951                 $$ = new If ((BooleanExpression) $3, (Statement) $5, GetLocation ($1));
5952                 lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4));
5953           }
5954         | IF open_parens_any boolean_expression CLOSE_PARENS
5955           embedded_statement ELSE embedded_statement
5956           {
5957                 $$ = new If ((BooleanExpression) $3, (Statement) $5, (Statement) $7, GetLocation ($1));
5958                 lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4), GetLocation ($6));
5959                 
5960                 if ($5 is EmptyStatement)
5961                         Warning_EmptyStatement (GetLocation ($5));
5962                 if ($7 is EmptyStatement)
5963                         Warning_EmptyStatement (GetLocation ($7));
5964           }
5965         | IF open_parens_any boolean_expression error
5966           {
5967                 Error_SyntaxError (yyToken);
5968                 
5969                 $$ = new If ((BooleanExpression) $3, null, GetLocation ($1));
5970                 lbag.AddStatement ($$, GetLocation ($2));
5971           }
5972         ;
5973
5974 switch_statement
5975         : SWITCH open_parens_any expression CLOSE_PARENS OPEN_BRACE
5976           {
5977                 start_block (GetLocation ($5));
5978           }
5979           opt_switch_sections CLOSE_BRACE
5980           {
5981                 $$ = new Switch ((Expression) $3, (ExplicitBlock) current_block.Explicit, GetLocation ($1));    
5982                 end_block (GetLocation ($8));
5983                 lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4));
5984           }
5985         | SWITCH open_parens_any expression error
5986           {
5987                 Error_SyntaxError (yyToken);
5988           
5989                 $$ = new Switch ((Expression) $3, null, GetLocation ($1));      
5990                 lbag.AddStatement ($$, GetLocation ($2));
5991           }
5992         ;
5993
5994 opt_switch_sections
5995         : /* empty */           
5996       {
5997                 report.Warning (1522, 1, current_block.StartLocation, "Empty switch block"); 
5998           }
5999         | switch_sections
6000         ;
6001
6002 switch_sections
6003         : switch_section 
6004         | switch_sections switch_section
6005         | error
6006           {
6007                 Error_SyntaxError (yyToken);
6008           } 
6009         ;
6010
6011 switch_section
6012         : switch_labels statement_list 
6013         ;
6014
6015 switch_labels
6016         : switch_label 
6017           {
6018                 var label = (SwitchLabel) $1;
6019                 label.SectionStart = true;
6020                 current_block.AddStatement (label);
6021           }
6022         | switch_labels switch_label 
6023           {
6024                 current_block.AddStatement ((Statement) $2);
6025           }
6026         ;
6027
6028 switch_label
6029         : CASE constant_expression COLON
6030          {
6031                 $$ = new SwitchLabel ((Expression) $2, GetLocation ($1));
6032                 lbag.AddLocation ($$, GetLocation ($3));
6033          }
6034         | CASE constant_expression error
6035           {
6036                 Error_SyntaxError (yyToken);
6037                 $$ = new SwitchLabel ((Expression) $2, GetLocation ($1));
6038           }
6039 /*        
6040         | CASE pattern_expr_invocation COLON
6041           {
6042                 if (lang_version != LanguageVersion.Experimental)
6043                         FeatureIsNotAvailable (GetLocation ($2), "pattern matching");
6044
6045                 $$ = new SwitchLabel ((Expression) $2, GetLocation ($1)) {
6046                         PatternMatching = true
6047                 };
6048                 lbag.AddLocation ($$, GetLocation ($3));
6049           }
6050 */
6051         | DEFAULT_COLON
6052           {
6053                 $$ = new SwitchLabel (null, GetLocation ($1));
6054           }
6055         ;
6056
6057 iteration_statement
6058         : while_statement
6059         | do_statement
6060         | for_statement
6061         | foreach_statement
6062         ;
6063
6064 while_statement
6065         : WHILE open_parens_any boolean_expression CLOSE_PARENS embedded_statement
6066           {
6067                 if ($5 is EmptyStatement && lexer.peek_token () == Token.OPEN_BRACE)
6068                         Warning_EmptyStatement (GetLocation ($5));
6069           
6070                 $$ = new While ((BooleanExpression) $3, (Statement) $5, GetLocation ($1));
6071                 lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4));
6072           }
6073         | WHILE open_parens_any boolean_expression error
6074           {
6075                 Error_SyntaxError (yyToken);
6076                 
6077                 $$ = new While ((BooleanExpression) $3, null, GetLocation ($1));
6078                 lbag.AddStatement ($$, GetLocation ($2));
6079           }
6080         ;
6081
6082 do_statement
6083         : DO embedded_statement WHILE open_parens_any boolean_expression CLOSE_PARENS SEMICOLON
6084           {
6085                 $$ = new Do ((Statement) $2, (BooleanExpression) $5, GetLocation ($1), GetLocation ($3));
6086                 lbag.AddStatement ($$, GetLocation ($3), GetLocation ($4), GetLocation ($6), GetLocation ($7));
6087           }
6088         | DO embedded_statement error
6089           {
6090                 Error_SyntaxError (yyToken);
6091                 $$ = new Do ((Statement) $2, null, GetLocation ($1), Location.Null);
6092           }
6093         | DO embedded_statement WHILE open_parens_any boolean_expression error
6094           {
6095                 Error_SyntaxError (yyToken);
6096           
6097                 $$ = new Do ((Statement) $2, (BooleanExpression) $5, GetLocation ($1), GetLocation ($3));
6098                 lbag.AddStatement ($$, GetLocation ($3), GetLocation ($4));
6099           }
6100         ;
6101
6102 for_statement
6103         : FOR open_parens_any
6104           {
6105                 start_block (GetLocation ($2));
6106                 current_block.IsCompilerGenerated = true;
6107                 For f = new For (GetLocation ($1));
6108                 current_block.AddStatement (f);
6109                 $$ = f;
6110           }
6111           for_statement_cont
6112           {
6113                 $$ = $4;
6114           }
6115         ;
6116         
6117 // Has to use be extra rule to recover started block
6118 for_statement_cont
6119         : opt_for_initializer SEMICOLON
6120           {
6121                 ((For) $0).Initializer = (Statement) $1;
6122
6123                 // Pass the "For" object to the iterator_part4
6124                 oob_stack.Push ($0);
6125           }
6126           for_condition_and_iterator_part
6127           embedded_statement
6128           {
6129                 var locations = (Tuple<Location,Location>) $4;
6130                 oob_stack.Pop ();
6131                 if ($5 is EmptyStatement && lexer.peek_token () == Token.OPEN_BRACE)
6132                         Warning_EmptyStatement (GetLocation ($5));
6133           
6134                 For f = ((For) $0);
6135                 f.Statement = (Statement) $5;
6136                 lbag.AddStatement (f, current_block.StartLocation, GetLocation ($2), GetLocation (locations.Item1), GetLocation (locations.Item2));
6137
6138                 $$ = end_block (GetLocation ($2));
6139           }
6140         | error
6141           {
6142                 Error_SyntaxError (yyToken);
6143                 $$ = end_block (current_block.StartLocation);
6144           }
6145         ;
6146
6147 for_condition_and_iterator_part
6148         : opt_for_condition SEMICOLON
6149           {
6150                 For f = (For) oob_stack.Peek ();
6151                 f.Condition = (BooleanExpression) $1;
6152           }
6153           for_iterator_part {
6154                 $$ = new Tuple<Location,Location> (GetLocation ($2), (Location) $4);
6155           }
6156
6157         // Handle errors in the case of opt_for_condition being followed by
6158         // a close parenthesis
6159         | opt_for_condition close_parens_close_brace {
6160                 report.Error (1525, GetLocation ($2), "Unexpected symbol `}'");
6161                 For f = (For) oob_stack.Peek ();
6162                 f.Condition = (BooleanExpression) $1;
6163                 $$ = new Tuple<Location,Location> (GetLocation ($2), GetLocation ($2));
6164           }
6165         ;
6166
6167 for_iterator_part
6168         : opt_for_iterator CLOSE_PARENS {
6169                 For f = (For) oob_stack.Peek ();
6170                 f.Iterator = (Statement) $1;
6171                 $$ = GetLocation ($2);
6172           }
6173         | opt_for_iterator CLOSE_BRACE {
6174                 report.Error (1525, GetLocation ($2), "Unexpected symbol expected ')'");
6175                 For f = (For) oob_stack.Peek ();
6176                 f.Iterator = (Statement) $1;
6177                 $$ = GetLocation ($2);
6178           }
6179         ; 
6180
6181 close_parens_close_brace 
6182         : CLOSE_PARENS
6183         | CLOSE_BRACE { lexer.putback ('}'); }
6184         ;
6185
6186 opt_for_initializer
6187         : /* empty */           { $$ = new EmptyStatement (lexer.Location); }
6188         | for_initializer       
6189         ;
6190
6191 for_initializer
6192         : variable_type identifier_inside_body
6193           {
6194                 var lt = (LocatedToken) $2;
6195                 var li = new LocalVariable (current_block, lt.Value, lt.Location);
6196                 current_block.AddLocalName (li);
6197                 current_variable = new BlockVariable ((FullNamedExpression) $1, li);
6198           }
6199           opt_local_variable_initializer opt_variable_declarators
6200           {
6201                 $$ = current_variable;
6202                 if ($4 != null)
6203                         lbag.AddLocation (current_variable, PopLocation ());
6204
6205                 current_variable = null;
6206           }
6207         | statement_expression_list
6208         ;
6209
6210 opt_for_condition
6211         : /* empty */           { $$ = null; }
6212         | boolean_expression
6213         ;
6214
6215 opt_for_iterator
6216         : /* empty */           { $$ = new EmptyStatement (lexer.Location); }
6217         | for_iterator
6218         ;
6219
6220 for_iterator
6221         : statement_expression_list
6222         ;
6223
6224 statement_expression_list
6225         : statement_expression
6226         | statement_expression_list COMMA statement_expression
6227           {
6228                 var sl = $1 as StatementList;
6229                 if (sl == null) {
6230                         sl = new StatementList ((Statement) $1, (Statement) $3);
6231                         lbag.AddStatement (sl, GetLocation ($2));
6232                 } else {
6233                         sl.Add ((Statement) $3);
6234                         lbag.AppendTo (sl, GetLocation ($2));
6235                 }
6236                         
6237                 $$ = sl;
6238           }
6239         ;
6240
6241 foreach_statement
6242         : FOREACH open_parens_any type error
6243           {
6244                 report.Error (230, GetLocation ($1), "Type and identifier are both required in a foreach statement");
6245
6246                 start_block (GetLocation ($2));
6247                 current_block.IsCompilerGenerated = true;
6248                 
6249                 Foreach f = new Foreach ((Expression) $3, null, null, null, null, GetLocation ($1));
6250                 current_block.AddStatement (f);
6251                 
6252                 lbag.AddStatement (f, GetLocation ($2));
6253                 $$ = end_block (GetLocation ($4));
6254           }
6255         | FOREACH open_parens_any type identifier_inside_body error
6256           {
6257                 Error_SyntaxError (yyToken);
6258         
6259                 start_block (GetLocation ($2));
6260                 current_block.IsCompilerGenerated = true;
6261                 
6262                 var lt = (LocatedToken) $4;
6263                 var li = new LocalVariable (current_block, lt.Value, LocalVariable.Flags.ForeachVariable | LocalVariable.Flags.Used, lt.Location);
6264                 current_block.AddLocalName (li);
6265                 
6266                 Foreach f = new Foreach ((Expression) $3, li, null, null, null, GetLocation ($1));
6267                 current_block.AddStatement (f);
6268                 
6269                 lbag.AddStatement (f, GetLocation ($2));
6270                 $$ = end_block (GetLocation ($5));
6271           }
6272         | FOREACH open_parens_any type identifier_inside_body IN expression CLOSE_PARENS 
6273           {
6274                 start_block (GetLocation ($2));
6275                 current_block.IsCompilerGenerated = true;
6276                 
6277                 var lt = (LocatedToken) $4;
6278                 var li = new LocalVariable (current_block, lt.Value, LocalVariable.Flags.ForeachVariable | LocalVariable.Flags.Used, lt.Location);
6279                 current_block.AddLocalName (li);
6280                 $$ = li;
6281           } 
6282           embedded_statement
6283           {
6284                 if ($9 is EmptyStatement && lexer.peek_token () == Token.OPEN_BRACE)
6285                         Warning_EmptyStatement (GetLocation ($9));
6286                 
6287                 Foreach f = new Foreach ((Expression) $3, (LocalVariable) $8, (Expression) $6, (Statement) $9, current_block, GetLocation ($1));
6288                 lbag.AddStatement (f, GetLocation ($2), GetLocation ($5), GetLocation ($7));
6289                 end_block (GetLocation ($7));
6290                 
6291                 $$ = f;
6292           }
6293         ;
6294
6295 jump_statement
6296         : break_statement
6297         | continue_statement
6298         | goto_statement
6299         | return_statement
6300         | throw_statement
6301         | yield_statement
6302         ;
6303
6304 break_statement
6305         : BREAK SEMICOLON
6306           {
6307                 $$ = new Break (GetLocation ($1));
6308                 lbag.AddStatement ($$, GetLocation ($2));
6309           }
6310         ;
6311
6312 continue_statement
6313         : CONTINUE SEMICOLON
6314           {
6315                 $$ = new Continue (GetLocation ($1));
6316                 lbag.AddStatement ($$, GetLocation ($2));
6317           }
6318         | CONTINUE error
6319           {
6320                 Error_SyntaxError (yyToken);
6321                 $$ = new Continue (GetLocation ($1));
6322           }
6323         ;
6324
6325 goto_statement
6326         : GOTO identifier_inside_body SEMICOLON 
6327           {
6328                 var lt = (LocatedToken) $2;
6329                 $$ = new Goto (lt.Value, GetLocation ($1));
6330                 lbag.AddStatement ($$, GetLocation ($2), GetLocation ($3));
6331           }
6332         | GOTO CASE constant_expression SEMICOLON
6333           {
6334                 $$ = new GotoCase ((Expression) $3, GetLocation ($1));
6335                 lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4));
6336           }
6337         | GOTO DEFAULT SEMICOLON 
6338           {
6339                 $$ = new GotoDefault (GetLocation ($1));
6340                 lbag.AddStatement ($$, GetLocation ($2), GetLocation ($3));
6341           }
6342         ; 
6343
6344 return_statement
6345         : RETURN opt_expression SEMICOLON
6346           {
6347                 $$ = new Return ((Expression) $2, GetLocation ($1));
6348                 lbag.AddStatement ($$, GetLocation ($3));
6349           }
6350         | RETURN expression error
6351           {
6352                 Error_SyntaxError (yyToken);
6353                 $$ = new Return ((Expression) $2, GetLocation ($1));
6354           }
6355         | RETURN error
6356           {
6357                 Error_SyntaxError (yyToken);
6358                 $$ = new Return (null, GetLocation ($1));
6359           }
6360         ;
6361
6362 throw_statement
6363         : THROW opt_expression SEMICOLON
6364           {
6365                 $$ = new Throw ((Expression) $2, GetLocation ($1));
6366                 lbag.AddStatement ($$, GetLocation ($3));
6367           }
6368         | THROW expression error
6369           {
6370                 Error_SyntaxError (yyToken);
6371                 $$ = new Throw ((Expression) $2, GetLocation ($1));
6372           }
6373         | THROW error
6374           {
6375                 Error_SyntaxError (yyToken);
6376                 $$ = new Throw (null, GetLocation ($1));
6377           }
6378         ;
6379
6380 yield_statement 
6381         : identifier_inside_body RETURN opt_expression SEMICOLON
6382           {
6383                 var lt = (LocatedToken) $1;
6384                 string s = lt.Value;
6385                 if (s != "yield"){
6386                         report.Error (1003, lt.Location, "; expected");
6387                 } else if ($3 == null) {
6388                         report.Error (1627, GetLocation ($4), "Expression expected after yield return");
6389                 } else if (lang_version == LanguageVersion.ISO_1){
6390                         FeatureIsNotAvailable (lt.Location, "iterators");
6391                 }
6392                 
6393                 current_block.Explicit.RegisterIteratorYield ();
6394                 $$ = new Yield ((Expression) $3, lt.Location);
6395                 lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4));
6396           }
6397         | identifier_inside_body RETURN expression error
6398           {
6399                 Error_SyntaxError (yyToken);
6400
6401                 var lt = (LocatedToken) $1;
6402                 string s = lt.Value;
6403                 if (s != "yield"){
6404                         report.Error (1003, lt.Location, "; expected");
6405                 } else if ($3 == null) {
6406                         report.Error (1627, GetLocation ($4), "Expression expected after yield return");
6407                 } else if (lang_version == LanguageVersion.ISO_1){
6408                         FeatureIsNotAvailable (lt.Location, "iterators");
6409                 }
6410                 
6411                 current_block.Explicit.RegisterIteratorYield ();
6412                 $$ = new Yield ((Expression) $3, lt.Location);
6413                 lbag.AddStatement ($$, GetLocation ($2));
6414           }
6415         | identifier_inside_body BREAK SEMICOLON
6416           {
6417                 var lt = (LocatedToken) $1;
6418                 string s = lt.Value;
6419                 if (s != "yield"){
6420                         report.Error (1003, lt.Location, "; expected");
6421                 } else if (lang_version == LanguageVersion.ISO_1){
6422                         FeatureIsNotAvailable (lt.Location, "iterators");
6423                 }
6424                 
6425                 current_block.ParametersBlock.TopBlock.IsIterator = true;
6426                 $$ = new YieldBreak (lt.Location);
6427                 lbag.AddStatement ($$, GetLocation ($2), GetLocation ($3));
6428           }
6429         ;
6430
6431 opt_expression
6432         : /* empty */
6433         | expression
6434         ;
6435
6436 try_statement
6437         : TRY block catch_clauses
6438           {
6439                 $$ = new TryCatch ((Block) $2, (List<Catch>) $3, GetLocation ($1), false);
6440           }
6441         | TRY block FINALLY block
6442           {
6443                 $$ = new TryFinally ((Statement) $2, (ExplicitBlock) $4, GetLocation ($1));
6444                 lbag.AddStatement ($$, GetLocation ($3));
6445           }
6446         | TRY block catch_clauses FINALLY block
6447           {
6448                 $$ = new TryFinally (new TryCatch ((Block) $2, (List<Catch>) $3, Location.Null, true), (ExplicitBlock) $5, GetLocation ($1));
6449                 lbag.AddStatement ($$, GetLocation ($4));
6450           }
6451         | TRY block error
6452           {
6453                 Error_SyntaxError (1524, yyToken);
6454                 $$ = new TryCatch ((Block) $2, null, GetLocation ($1), false);
6455           }
6456         ;
6457
6458 catch_clauses
6459         : catch_clause 
6460           {
6461                 var l = new List<Catch> (2);
6462
6463                 l.Add ((Catch) $1);
6464                 $$ = l;
6465           }
6466         | catch_clauses catch_clause
6467           {
6468                 var l = (List<Catch>) $1;
6469                 
6470                 Catch c = (Catch) $2;
6471                 var prev_catch = l [l.Count - 1];
6472                 if (prev_catch.IsGeneral && prev_catch.Filter == null) {
6473                         report.Error (1017, c.loc, "Try statement already has an empty catch block");
6474                 }
6475                 
6476                 l.Add (c);
6477                 $$ = l;
6478           }
6479         ;
6480
6481 opt_identifier
6482         : /* empty */
6483         | identifier_inside_body
6484         ;
6485
6486 catch_clause 
6487         : CATCH opt_catch_filter block
6488           {
6489                 var c = new Catch ((ExplicitBlock) $3, GetLocation ($1));
6490                 c.Filter = (CatchFilterExpression) $2;
6491                 $$ = c;
6492           }
6493         | CATCH open_parens_any type opt_identifier CLOSE_PARENS
6494           {
6495                 start_block (GetLocation ($2));
6496                 var c = new Catch ((ExplicitBlock) current_block, GetLocation ($1));
6497                 c.TypeExpression = (FullNamedExpression) $3;
6498
6499                 if ($4 != null) {
6500                         var lt = (LocatedToken) $4;
6501                         c.Variable = new LocalVariable (current_block, lt.Value, lt.Location);
6502                         current_block.AddLocalName (c.Variable);
6503                 }
6504                 
6505                 lbag.AddLocation (c, GetLocation ($2), GetLocation ($5));
6506                 $$ = c;
6507                 lexer.parsing_catch_when = true;
6508           }
6509           opt_catch_filter_or_error
6510           {
6511                 ((Catch) $6).Filter = (CatchFilterExpression) $7;
6512                 $$ = $6;
6513           }
6514         | CATCH open_parens_any error
6515           {
6516                 if (yyToken == Token.CLOSE_PARENS) {
6517                         report.Error (1015, lexer.Location,
6518                                 "A type that derives from `System.Exception', `object', or `string' expected");
6519                 } else {
6520                         Error_SyntaxError (yyToken);
6521                 }
6522                 
6523                 $$ = new Catch (null, GetLocation ($1));
6524           }
6525         ;
6526
6527 opt_catch_filter_or_error
6528         : opt_catch_filter block_prepared
6529           {
6530                 $$ = $1;
6531           }
6532         | error
6533           {
6534                 end_block (Location.Null);
6535                 Error_SyntaxError (yyToken);
6536                 $$ = null;
6537           }
6538         ;
6539
6540 opt_catch_filter
6541         : {
6542                 lexer.parsing_catch_when = false;
6543           }
6544         | WHEN
6545           {
6546                 lexer.parsing_catch_when = false;
6547           }
6548           open_parens_any expression CLOSE_PARENS
6549           {
6550                 if (lang_version <= LanguageVersion.V_5)
6551                         FeatureIsNotAvailable (GetLocation ($1), "exception filter");
6552
6553                 $$ = new CatchFilterExpression ((Expression) $4, GetLocation ($1));
6554                 lbag.AddLocation ($$, GetLocation ($3), GetLocation ($5));
6555           }
6556         ;
6557
6558 checked_statement
6559         : CHECKED block
6560           {
6561                 $$ = new Checked ((Block) $2, GetLocation ($1));
6562           }
6563         ;
6564
6565 unchecked_statement
6566         : UNCHECKED block
6567           {
6568                 $$ = new Unchecked ((Block) $2, GetLocation ($1));
6569           }
6570         ;
6571
6572 unsafe_statement
6573         : UNSAFE
6574           {
6575                 if (!settings.Unsafe)
6576                         Error_UnsafeCodeNotAllowed (GetLocation ($1));
6577           } block {
6578                 $$ = new Unsafe ((Block) $3, GetLocation ($1));
6579           }
6580         ;
6581
6582 lock_statement
6583         : LOCK open_parens_any expression CLOSE_PARENS embedded_statement
6584           {
6585                 if ($5 is EmptyStatement && lexer.peek_token () == Token.OPEN_BRACE)
6586                         Warning_EmptyStatement (GetLocation ($5));
6587           
6588                 $$ = new Lock ((Expression) $3, (Statement) $5, GetLocation ($1));
6589                 lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4));
6590           }
6591         | LOCK open_parens_any expression error
6592           {
6593                 Error_SyntaxError (yyToken);
6594
6595                 $$ = new Lock ((Expression) $3, null, GetLocation ($1));
6596                 lbag.AddStatement ($$, GetLocation ($2));
6597           }
6598         ;
6599
6600 fixed_statement
6601         : FIXED open_parens_any variable_type identifier_inside_body
6602           {
6603             start_block (GetLocation ($2));
6604             
6605                 current_block.IsCompilerGenerated = true;
6606                 var lt = (LocatedToken) $4;
6607                 var li = new LocalVariable (current_block, lt.Value, LocalVariable.Flags.FixedVariable | LocalVariable.Flags.Used, lt.Location);
6608                 current_block.AddLocalName (li);
6609                 current_variable = new Fixed.VariableDeclaration ((FullNamedExpression) $3, li);
6610           }
6611           using_or_fixed_variable_initializer opt_using_or_fixed_variable_declarators CLOSE_PARENS
6612           {
6613                 $$ = current_variable;
6614                 current_variable = null;
6615           }
6616           embedded_statement
6617           {
6618                 if ($10 is EmptyStatement && lexer.peek_token () == Token.OPEN_BRACE)
6619                         Warning_EmptyStatement (GetLocation ($10));
6620           
6621                 Fixed f = new Fixed ((Fixed.VariableDeclaration) $9, (Statement) $10, GetLocation ($1));
6622                 current_block.AddStatement (f);
6623                 lbag.AddStatement (f, GetLocation ($2), GetLocation ($8));
6624                 $$ = end_block (GetLocation ($8));
6625           }
6626         ;
6627
6628 using_statement
6629         : USING open_parens_any variable_type identifier_inside_body
6630           {
6631             start_block (GetLocation ($2));
6632             
6633                 current_block.IsCompilerGenerated = true;
6634                 var lt = (LocatedToken) $4;
6635                 var li = new LocalVariable (current_block, lt.Value, LocalVariable.Flags.UsingVariable | LocalVariable.Flags.Used, lt.Location);
6636                 current_block.AddLocalName (li);
6637                 current_variable = new Using.VariableDeclaration ((FullNamedExpression) $3, li);
6638           }
6639           using_initialization CLOSE_PARENS
6640           {
6641                 $$ = current_variable;    
6642                 current_variable = null;
6643           }
6644           embedded_statement
6645           {
6646                 if ($9 is EmptyStatement && lexer.peek_token () == Token.OPEN_BRACE)
6647                         Warning_EmptyStatement (GetLocation ($9));
6648           
6649                 Using u = new Using ((Using.VariableDeclaration) $8, (Statement) $9, GetLocation ($1));
6650                 current_block.AddStatement (u);
6651                 $$ = end_block (GetLocation ($7));
6652           }
6653         | USING open_parens_any expression CLOSE_PARENS embedded_statement
6654           {
6655                 if ($5 is EmptyStatement && lexer.peek_token () == Token.OPEN_BRACE)
6656                         Warning_EmptyStatement (GetLocation ($5));
6657           
6658                 $$ = new Using ((Expression) $3, (Statement) $5, GetLocation ($1));
6659                 lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4));
6660           }
6661         | USING open_parens_any expression error
6662           {
6663                 Error_SyntaxError (yyToken);
6664                 
6665                 $$ = new Using ((Expression) $3, null, GetLocation ($1));
6666                 lbag.AddStatement ($$, GetLocation ($2));
6667           }
6668         ;
6669         
6670 using_initialization
6671         : using_or_fixed_variable_initializer opt_using_or_fixed_variable_declarators
6672         | error
6673           {
6674                 // It has to be here for the parent to safely restore artificial block
6675                 Error_SyntaxError (yyToken);
6676           }
6677         ;
6678         
6679 using_or_fixed_variable_initializer
6680         : /* empty */
6681           {
6682                 Error_MissingInitializer (lexer.Location);
6683           }
6684         | ASSIGN variable_initializer
6685           {
6686                 current_variable.Initializer = (Expression) $2;
6687                 $$ = current_variable;
6688           }
6689         ;
6690
6691
6692 // LINQ
6693
6694 query_expression
6695         : first_from_clause query_body 
6696           {
6697                 lexer.query_parsing = false;
6698                         
6699                 Linq.AQueryClause from = $1 as Linq.AQueryClause;
6700                         
6701                 from.Tail.Next = (Linq.AQueryClause)$2;
6702                 $$ = from;
6703                 
6704                 current_block.SetEndLocation (lexer.Location);
6705                 current_block = current_block.Parent;
6706           }
6707         | nested_from_clause query_body
6708           {
6709                 Linq.AQueryClause from = $1 as Linq.AQueryClause;
6710                         
6711                 from.Tail.Next = (Linq.AQueryClause)$2;
6712                 $$ = from;
6713                 
6714                 current_block.SetEndLocation (lexer.Location);
6715                 current_block = current_block.Parent;
6716           }     
6717
6718         // Bubble up COMPLETE_COMPLETION productions
6719         | first_from_clause COMPLETE_COMPLETION {
6720                 lexer.query_parsing = false;
6721                 $$ = $1;
6722
6723                 current_block.SetEndLocation (lexer.Location);
6724                 current_block = current_block.Parent;
6725           }
6726         | nested_from_clause COMPLETE_COMPLETION {
6727                 $$ = $1;
6728                 current_block.SetEndLocation (lexer.Location);
6729                 current_block = current_block.Parent;
6730           }
6731         ;
6732         
6733 first_from_clause
6734         : FROM_FIRST identifier_inside_body IN expression
6735           {
6736                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
6737           
6738                 var lt = (LocatedToken) $2;
6739                 var rv = new Linq.RangeVariable (lt.Value, lt.Location);
6740                 var clause = new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$4, rv, GetLocation ($1));
6741                 lbag.AddLocation (clause, GetLocation ($3));
6742                 $$ = new Linq.QueryExpression (clause);
6743           }
6744         | FROM_FIRST type identifier_inside_body IN expression
6745           {
6746                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
6747           
6748                 var lt = (LocatedToken) $3;
6749                 var rv = new Linq.RangeVariable (lt.Value, lt.Location);
6750                 var clause = new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$5, rv, GetLocation ($1)) {
6751                                 IdentifierType = (FullNamedExpression)$2
6752                 };
6753                 lbag.AddLocation (clause, GetLocation ($4));
6754                 $$ = new Linq.QueryExpression (clause);
6755           }
6756         ;
6757
6758 nested_from_clause
6759         : FROM identifier_inside_body IN expression
6760           {
6761                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
6762           
6763                 var lt = (LocatedToken) $2;
6764                 var rv = new Linq.RangeVariable (lt.Value, lt.Location);
6765                 var clause = new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$4, rv, GetLocation ($1));
6766                 lbag.AddLocation (clause, GetLocation ($3));
6767                 $$ = new Linq.QueryExpression (clause);
6768           }
6769         | FROM type identifier_inside_body IN expression
6770           {
6771                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
6772           
6773                 var lt = (LocatedToken) $3;
6774                 var rv = new Linq.RangeVariable (lt.Value, lt.Location);
6775                 var clause = new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$5, rv, GetLocation ($1)) {
6776                                 IdentifierType = (FullNamedExpression)$2
6777                 };
6778                 lbag.AddLocation (clause, GetLocation ($4));
6779                 $$ = new Linq.QueryExpression (clause);
6780           }
6781         ;
6782         
6783 from_clause
6784         : FROM identifier_inside_body IN
6785           {
6786                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
6787           }
6788           expression_or_error
6789           {
6790                 var lt = (LocatedToken) $2;
6791                 var sn = new Linq.RangeVariable (lt.Value, lt.Location);
6792                 $$ = new Linq.SelectMany ((Linq.QueryBlock)current_block, sn, (Expression)$5, GetLocation ($1));
6793                 
6794                 current_block.SetEndLocation (lexer.Location);
6795                 current_block = current_block.Parent;
6796                 
6797                 ((Linq.QueryBlock)current_block).AddRangeVariable (sn);
6798                 lbag.AddLocation ($$, GetLocation ($3));
6799           }       
6800         | FROM type identifier_inside_body IN
6801           {
6802                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
6803           }
6804           expression_or_error
6805           {
6806                 var lt = (LocatedToken) $3;
6807                 var sn = new Linq.RangeVariable (lt.Value, lt.Location);
6808
6809                 $$ = new Linq.SelectMany ((Linq.QueryBlock)current_block, sn, (Expression)$6, GetLocation ($1)) {
6810                         IdentifierType = (FullNamedExpression)$2
6811                 };
6812                 
6813                 current_block.SetEndLocation (lexer.Location);
6814                 current_block = current_block.Parent;
6815                 
6816                 ((Linq.QueryBlock)current_block).AddRangeVariable (sn);
6817                 
6818                 lbag.AddLocation ($$, GetLocation ($4));
6819           }
6820         ;       
6821
6822 query_body
6823         : query_body_clauses select_or_group_clause opt_query_continuation 
6824           {
6825                 Linq.AQueryClause head = (Linq.AQueryClause)$2;
6826                 
6827                 if ($3 != null)
6828                         head.Next = (Linq.AQueryClause)$3;
6829                                 
6830                 if ($1 != null) {
6831                         Linq.AQueryClause clause = (Linq.AQueryClause)$1;
6832                         clause.Tail.Next = head;
6833                         head = clause;
6834                 }
6835                 
6836                 $$ = head;
6837           }
6838         | select_or_group_clause opt_query_continuation
6839           {
6840                 Linq.AQueryClause head = (Linq.AQueryClause)$2;
6841
6842                 if ($1 != null) {
6843                         Linq.AQueryClause clause = (Linq.AQueryClause)$1;
6844                         clause.Tail.Next = head;
6845                         head = clause;
6846                 }
6847                 
6848                 $$ = head;
6849           }
6850         | query_body_clauses COMPLETE_COMPLETION
6851         | query_body_clauses error
6852           {
6853                 report.Error (742, GetLocation ($2), "Unexpected symbol `{0}'. A query body must end with select or group clause", GetSymbolName (yyToken));
6854                 $$ = $1;
6855           }
6856         | error
6857           {
6858                 Error_SyntaxError (yyToken);
6859                 $$ = null;
6860           }
6861         ;
6862         
6863 select_or_group_clause
6864         : SELECT
6865           {
6866                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
6867           }
6868           expression_or_error
6869           {
6870                 $$ = new Linq.Select ((Linq.QueryBlock)current_block, (Expression)$3, GetLocation ($1));
6871
6872                 current_block.SetEndLocation (lexer.Location);
6873                 current_block = current_block.Parent;
6874           }
6875         | GROUP
6876           {
6877                 if (linq_clause_blocks == null)
6878                         linq_clause_blocks = new Stack<Linq.QueryBlock> ();
6879                         
6880                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
6881                 linq_clause_blocks.Push ((Linq.QueryBlock)current_block);
6882           }
6883           expression_or_error
6884           {
6885                 current_block.SetEndLocation (lexer.Location);
6886                 current_block = current_block.Parent;
6887           
6888                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
6889           }
6890           by_expression
6891           {
6892                 var obj = (object[]) $5;
6893
6894                 $$ = new Linq.GroupBy ((Linq.QueryBlock)current_block, (Expression)$3, linq_clause_blocks.Pop (), (Expression)obj[0], GetLocation ($1));
6895                 lbag.AddLocation ($$, (Location) obj[1]);
6896                 
6897                 current_block.SetEndLocation (lexer.Location);
6898                 current_block = current_block.Parent;
6899           }
6900         ;
6901
6902 by_expression
6903         : BY expression_or_error
6904           {
6905                 $$ = new object[] { $2, GetLocation ($1) };
6906           }
6907         | error
6908           {
6909                 Error_SyntaxError (yyToken);
6910                 $$ = new object[2] { null, Location.Null };
6911           }
6912         ;
6913         
6914 query_body_clauses
6915         : query_body_clause
6916         | query_body_clauses query_body_clause
6917           {
6918                 ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$2;
6919                 $$ = $1;
6920           }
6921         ;
6922         
6923 query_body_clause
6924         : from_clause
6925         | let_clause 
6926         | where_clause
6927         | join_clause
6928         | orderby_clause
6929         ;
6930         
6931 let_clause
6932         : LET identifier_inside_body ASSIGN 
6933           {
6934                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
6935           }
6936           expression_or_error
6937           {
6938                 var lt = (LocatedToken) $2;
6939                 var sn = new Linq.RangeVariable (lt.Value, lt.Location);
6940                 $$ = new Linq.Let ((Linq.QueryBlock) current_block, sn, (Expression)$5, GetLocation ($1));
6941                 lbag.AddLocation ($$, GetLocation ($3));
6942                 
6943                 current_block.SetEndLocation (lexer.Location);
6944                 current_block = current_block.Parent;
6945                 
6946                 ((Linq.QueryBlock)current_block).AddRangeVariable (sn);
6947           }
6948         ;
6949
6950 where_clause
6951         : WHERE
6952           {
6953                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
6954           }
6955           expression_or_error
6956           {
6957                 $$ = new Linq.Where ((Linq.QueryBlock)current_block, (Expression)$3, GetLocation ($1));
6958
6959                 current_block.SetEndLocation (lexer.Location);
6960                 current_block = current_block.Parent;
6961           }
6962         ;
6963         
6964 join_clause
6965         : JOIN identifier_inside_body IN
6966           {
6967                 if (linq_clause_blocks == null)
6968                         linq_clause_blocks = new Stack<Linq.QueryBlock> ();
6969                         
6970                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
6971                 linq_clause_blocks.Push ((Linq.QueryBlock) current_block);
6972           }
6973           expression_or_error ON
6974           {
6975                 current_block.SetEndLocation (lexer.Location);
6976                 current_block = current_block.Parent;
6977
6978                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
6979                 linq_clause_blocks.Push ((Linq.QueryBlock) current_block);
6980           }
6981           expression_or_error EQUALS
6982           {
6983                 current_block.AddStatement (new ContextualReturn ((Expression) $8));
6984                 current_block.SetEndLocation (lexer.Location);
6985                 current_block = current_block.Parent;
6986
6987                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
6988           }
6989           expression_or_error opt_join_into
6990           {
6991                 current_block.AddStatement (new ContextualReturn ((Expression) $11));
6992                 current_block.SetEndLocation (lexer.Location);
6993           
6994                 var outer_selector = linq_clause_blocks.Pop ();
6995                 var block = linq_clause_blocks.Pop ();
6996
6997                 var lt = (LocatedToken) $2;     
6998                 var sn = new Linq.RangeVariable (lt.Value, lt.Location);
6999                 Linq.RangeVariable into;
7000                 
7001                 if ($12 == null) {
7002                         into = sn;
7003                         $$ = new Linq.Join (block, sn, (Expression)$5, outer_selector, (Linq.QueryBlock) current_block, GetLocation ($1));
7004                         lbag.AddLocation ($$, GetLocation ($3), GetLocation ($6), GetLocation ($9));
7005                 } else {
7006                         //
7007                         // Set equals right side parent to beginning of linq query, it is not accessible therefore cannot cause name collisions
7008                         //
7009                         var parent = block.Parent;
7010                         while (parent is Linq.QueryBlock) {
7011                                 parent = parent.Parent;
7012                         }
7013                         current_block.Parent = parent;
7014                         
7015                         ((Linq.QueryBlock)current_block).AddRangeVariable (sn);
7016                 
7017                         lt = (LocatedToken) $12;
7018                         into = new Linq.RangeVariable (lt.Value, lt.Location);
7019
7020                         $$ = new Linq.GroupJoin (block, sn, (Expression)$5, outer_selector, (Linq.QueryBlock) current_block, into, GetLocation ($1));   
7021                         lbag.AddLocation ($$, GetLocation ($3), GetLocation ($6), GetLocation ($9), GetLocation ($12));
7022                 }
7023
7024                 current_block = block.Parent;
7025                 ((Linq.QueryBlock)current_block).AddRangeVariable (into);
7026           }
7027         | JOIN type identifier_inside_body IN
7028           {
7029                 if (linq_clause_blocks == null)
7030                         linq_clause_blocks = new Stack<Linq.QueryBlock> ();
7031                         
7032                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
7033                 linq_clause_blocks.Push ((Linq.QueryBlock) current_block);
7034           }
7035           expression_or_error ON
7036           {
7037                 current_block.SetEndLocation (lexer.Location);
7038                 current_block = current_block.Parent;
7039
7040                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
7041                 linq_clause_blocks.Push ((Linq.QueryBlock) current_block);
7042           }
7043           expression_or_error EQUALS
7044           {
7045                 current_block.AddStatement (new ContextualReturn ((Expression) $9));
7046                 current_block.SetEndLocation (lexer.Location);
7047                 current_block = current_block.Parent;
7048
7049                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
7050           }
7051           expression_or_error opt_join_into
7052           {
7053                 current_block.AddStatement (new ContextualReturn ((Expression) $12));
7054                 current_block.SetEndLocation (lexer.Location);
7055           
7056                 var outer_selector = linq_clause_blocks.Pop ();
7057                 var block = linq_clause_blocks.Pop ();
7058                 
7059                 var lt = (LocatedToken) $3;
7060                 var sn = new Linq.RangeVariable (lt.Value, lt.Location);
7061                 Linq.RangeVariable into;
7062                 
7063                 if ($13 == null) {
7064                         into = sn;              
7065                         $$ = new Linq.Join (block, sn, (Expression)$6, outer_selector, (Linq.QueryBlock) current_block, GetLocation ($1)) {
7066                                 IdentifierType = (FullNamedExpression)$2
7067                         };
7068                         lbag.AddLocation ($$, GetLocation ($3), GetLocation ($6), GetLocation ($9));
7069                 } else {
7070                         //
7071                         // Set equals right side parent to beginning of linq query, it is not accessible therefore cannot cause name collisions
7072                         //
7073                         var parent = block.Parent;
7074                         while (parent is Linq.QueryBlock) {
7075                                 parent = parent.Parent;
7076                         }
7077                         current_block.Parent = parent;
7078                 
7079                         ((Linq.QueryBlock)current_block).AddRangeVariable (sn);
7080                 
7081                         lt = (LocatedToken) $13;
7082                         into = new Linq.RangeVariable (lt.Value, lt.Location); // TODO:
7083                         
7084                         $$ = new Linq.GroupJoin (block, sn, (Expression)$6, outer_selector, (Linq.QueryBlock) current_block, into, GetLocation ($1)) {
7085                                 IdentifierType = (FullNamedExpression)$2
7086                         };                      
7087                 }
7088                 
7089                 current_block = block.Parent;
7090                 ((Linq.QueryBlock)current_block).AddRangeVariable (into);               
7091           }
7092         ;
7093         
7094 opt_join_into
7095         : /* empty */
7096         | INTO identifier_inside_body
7097           {
7098                 $$ = $2;
7099           }
7100         ;
7101         
7102 orderby_clause
7103         : ORDERBY
7104           {
7105                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
7106           }
7107           orderings
7108           {
7109                 current_block.SetEndLocation (lexer.Location);
7110                 current_block = current_block.Parent;
7111           
7112                 $$ = $3;
7113           }
7114         ;
7115         
7116 orderings
7117         : order_by
7118         | order_by COMMA
7119           {
7120                 current_block.SetEndLocation (lexer.Location);
7121                 current_block = current_block.Parent;
7122           
7123                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
7124           }
7125           orderings_then_by
7126           {
7127                 ((Linq.AQueryClause)$1).Next = (Linq.AQueryClause)$4;
7128                 $$ = $1;
7129           }
7130         ;
7131         
7132 orderings_then_by
7133         : then_by
7134         | orderings_then_by COMMA
7135          {
7136                 current_block.SetEndLocation (lexer.Location);
7137                 current_block = current_block.Parent;
7138           
7139                 current_block = new Linq.QueryBlock ((Linq.QueryBlock) current_block, lexer.Location);   
7140          }
7141          then_by
7142          {
7143                 ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$4;
7144                 $$ = $1;
7145          }
7146         ;       
7147         
7148 order_by
7149         : expression
7150           {
7151                 $$ = new Linq.OrderByAscending ((Linq.QueryBlock) current_block, (Expression)$1);       
7152           }
7153         | expression ASCENDING
7154           {
7155                 $$ = new Linq.OrderByAscending ((Linq.QueryBlock) current_block, (Expression)$1);       
7156                 lbag.AddLocation ($$, GetLocation ($2));
7157           }
7158         | expression DESCENDING
7159           {
7160                 $$ = new Linq.OrderByDescending ((Linq.QueryBlock) current_block, (Expression)$1);      
7161                 lbag.AddLocation ($$, GetLocation ($2));
7162           }
7163         ;
7164
7165 then_by
7166         : expression
7167           {
7168                 $$ = new Linq.ThenByAscending ((Linq.QueryBlock) current_block, (Expression)$1);        
7169           }
7170         | expression ASCENDING
7171           {
7172                 $$ = new Linq.ThenByAscending ((Linq.QueryBlock) current_block, (Expression)$1);        
7173                 lbag.AddLocation ($$, GetLocation ($2));
7174           }
7175         | expression DESCENDING
7176           {
7177                 $$ = new Linq.ThenByDescending ((Linq.QueryBlock) current_block, (Expression)$1);       
7178                 lbag.AddLocation ($$, GetLocation ($2));
7179           }     
7180         ;
7181
7182
7183 opt_query_continuation
7184         : /* empty */
7185         | INTO identifier_inside_body
7186           {
7187                 // query continuation block is not linked with query block but with block
7188                 // before. This means each query can use same range variable names for
7189                 // different identifiers.
7190
7191                 current_block.SetEndLocation (GetLocation ($1));
7192                 current_block = current_block.Parent;
7193         
7194                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
7195                 
7196                 if (linq_clause_blocks == null)
7197                         linq_clause_blocks = new Stack<Linq.QueryBlock> ();
7198                         
7199                 linq_clause_blocks.Push ((Linq.QueryBlock) current_block);              
7200           }
7201           query_body
7202           {
7203                 var current_block = linq_clause_blocks.Pop ();    
7204                 var lt = (LocatedToken) $2;
7205                 var rv = new Linq.RangeVariable (lt.Value, lt.Location);
7206                 $$ = new Linq.QueryStartClause ((Linq.QueryBlock)current_block, null, rv, GetLocation ($1)) {
7207                         next = (Linq.AQueryClause)$4
7208                 };
7209           }
7210         ;
7211         
7212 //
7213 // Support for using the compiler as an interactive parser
7214 //
7215 // The INTERACTIVE_PARSER token is first sent to parse our
7216 // productions;  If the result is a Statement, the parsing
7217 // is repeated, this time with INTERACTIVE_PARSE_WITH_BLOCK
7218 // to setup the blocks in advance.
7219 //
7220 // This setup is here so that in the future we can add 
7221 // support for other constructs (type parsing, namespaces, etc)
7222 // that do not require a block to be setup in advance
7223 //
7224
7225 interactive_parsing
7226         : EVAL_STATEMENT_PARSER EOF 
7227         | EVAL_USING_DECLARATIONS_UNIT_PARSER using_directives opt_COMPLETE_COMPLETION
7228         | EVAL_STATEMENT_PARSER
7229          { 
7230                 current_container = current_type = new Class (current_container, new MemberName ("<InteractiveExpressionClass>"), Modifiers.PUBLIC, null);
7231
7232                 // (ref object retval)
7233                 Parameter [] mpar = new Parameter [1];
7234                 mpar [0] = new Parameter (new TypeExpression (compiler.BuiltinTypes.Object, Location.Null), "$retval", Parameter.Modifier.REF, null, Location.Null);
7235
7236                 ParametersCompiled pars = new ParametersCompiled (mpar);
7237                 var mods = Modifiers.PUBLIC | Modifiers.STATIC;
7238                 if (settings.Unsafe)
7239                         mods |= Modifiers.UNSAFE;
7240
7241                 current_local_parameters = pars;
7242                 var method = new InteractiveMethod (
7243                         current_type,
7244                         new TypeExpression (compiler.BuiltinTypes.Void, Location.Null),
7245                         mods,
7246                         pars);
7247                         
7248                 current_type.AddMember (method);                        
7249                 oob_stack.Push (method);
7250
7251                 interactive_async = false;
7252
7253                 ++lexer.parsing_block;
7254                 start_block (lexer.Location);
7255           }             
7256           interactive_statement_list opt_COMPLETE_COMPLETION
7257           {
7258                 --lexer.parsing_block;
7259                 var method = (InteractiveMethod) oob_stack.Pop ();
7260                 method.Block = (ToplevelBlock) end_block(lexer.Location);
7261
7262                 if (interactive_async == true) {
7263                         method.ChangeToAsync ();
7264                 }
7265
7266                 InteractiveResult = (Class) pop_current_class ();
7267                 current_local_parameters = null;
7268           } 
7269         | EVAL_COMPILATION_UNIT_PARSER interactive_compilation_unit
7270         ;
7271
7272 interactive_compilation_unit
7273         : opt_extern_alias_directives opt_using_directives
7274         | opt_extern_alias_directives opt_using_directives namespace_or_type_declarations
7275         ;
7276
7277 opt_COMPLETE_COMPLETION
7278         : /* nothing */
7279         | COMPLETE_COMPLETION
7280         ;
7281
7282 close_brace_or_complete_completion
7283         : CLOSE_BRACE
7284         | COMPLETE_COMPLETION
7285         ;
7286         
7287 //
7288 // XML documentation code references micro parser
7289 //
7290 documentation_parsing
7291         : DOC_SEE doc_cref
7292           {
7293                 module.DocumentationBuilder.ParsedName = (MemberName) $2;
7294           }
7295         ;
7296
7297 doc_cref
7298         : doc_type_declaration_name opt_doc_method_sig
7299           {
7300                 module.DocumentationBuilder.ParsedParameters = (List<DocumentationParameter>)$2;
7301           }
7302         | builtin_types opt_doc_method_sig
7303           {
7304                 module.DocumentationBuilder.ParsedBuiltinType = (TypeExpression)$1;
7305                 module.DocumentationBuilder.ParsedParameters = (List<DocumentationParameter>)$2;
7306                 $$ = null;
7307           }
7308         | VOID opt_doc_method_sig
7309           {
7310                 module.DocumentationBuilder.ParsedBuiltinType = new TypeExpression (compiler.BuiltinTypes.Void, GetLocation ($1));
7311                 module.DocumentationBuilder.ParsedParameters = (List<DocumentationParameter>)$2;
7312                 $$ = null;
7313           }
7314         | builtin_types DOT IDENTIFIER opt_doc_method_sig
7315           {
7316                 module.DocumentationBuilder.ParsedBuiltinType = (TypeExpression)$1;
7317                 module.DocumentationBuilder.ParsedParameters = (List<DocumentationParameter>)$4;
7318                 var lt = (LocatedToken) $3;
7319                 $$ = new MemberName (lt.Value);
7320           }
7321         | doc_type_declaration_name DOT THIS
7322           {
7323                 $$ = new MemberName ((MemberName) $1, MemberCache.IndexerNameAlias, Location.Null);
7324           }
7325         | doc_type_declaration_name DOT THIS OPEN_BRACKET
7326           {
7327                 valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out;
7328           }
7329           opt_doc_parameters CLOSE_BRACKET
7330           {
7331                 module.DocumentationBuilder.ParsedParameters = (List<DocumentationParameter>)$6;
7332                 $$ = new MemberName ((MemberName) $1, MemberCache.IndexerNameAlias, Location.Null);
7333           }
7334         | EXPLICIT OPERATOR type opt_doc_method_sig
7335           {
7336                 var p = (List<DocumentationParameter>)$4 ?? new List<DocumentationParameter> (1);
7337                 p.Add (new DocumentationParameter ((FullNamedExpression) $3));
7338                 module.DocumentationBuilder.ParsedParameters = p;
7339                 module.DocumentationBuilder.ParsedOperator = Operator.OpType.Explicit;
7340                 $$ = null;
7341           }
7342         | IMPLICIT OPERATOR type opt_doc_method_sig
7343           {
7344                 var p = (List<DocumentationParameter>)$4 ?? new List<DocumentationParameter> (1);
7345                 p.Add (new DocumentationParameter ((FullNamedExpression) $3));
7346                 module.DocumentationBuilder.ParsedParameters = p;
7347                 module.DocumentationBuilder.ParsedOperator = Operator.OpType.Implicit;
7348                 $$ = null;
7349           }       
7350         | OPERATOR overloadable_operator opt_doc_method_sig
7351           {
7352                 var p = (List<DocumentationParameter>)$3;
7353                 module.DocumentationBuilder.ParsedParameters = p;
7354                 module.DocumentationBuilder.ParsedOperator = (Operator.OpType) $2;
7355                 $$ = null;
7356           }
7357         ;
7358         
7359 doc_type_declaration_name
7360         : type_declaration_name
7361         | doc_type_declaration_name DOT type_declaration_name
7362           {
7363                 $$ = new MemberName (((MemberName) $1), (MemberName) $3);
7364           }
7365         ;
7366         
7367 opt_doc_method_sig
7368         : /* empty */
7369         | OPEN_PARENS
7370           {
7371                 valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out;
7372           }
7373           opt_doc_parameters CLOSE_PARENS
7374           {
7375                 $$ = $3;
7376           }
7377         ;
7378         
7379 opt_doc_parameters
7380         : /* empty */
7381           {
7382                 $$ = new List<DocumentationParameter> (0);
7383           }
7384         | doc_parameters
7385         ;
7386         
7387 doc_parameters
7388         : doc_parameter
7389           {
7390                 var parameters = new List<DocumentationParameter> ();
7391                 parameters.Add ((DocumentationParameter) $1);
7392                 $$ = parameters;
7393           }
7394         | doc_parameters COMMA doc_parameter
7395           {
7396                 var parameters = $1 as List<DocumentationParameter>;
7397                 parameters.Add ((DocumentationParameter) $3);
7398                 $$ = parameters;
7399           }
7400         ;
7401         
7402 doc_parameter
7403         : opt_parameter_modifier parameter_type
7404           {
7405                 if ($1 != null)
7406                         $$ = new DocumentationParameter ((Parameter.Modifier) $1, (FullNamedExpression) $2);
7407                 else
7408                         $$ = new DocumentationParameter ((FullNamedExpression) $2);
7409           }
7410         ;
7411         
7412 %%
7413
7414 // <summary>
7415 //  A class used to hold info about an operator declarator
7416 // </summary>
7417 class OperatorDeclaration {
7418         public readonly Operator.OpType optype;
7419         public readonly FullNamedExpression ret_type;
7420         public readonly Location location;
7421
7422         public OperatorDeclaration (Operator.OpType op, FullNamedExpression ret_type, Location location)
7423         {
7424                 optype = op;
7425                 this.ret_type = ret_type;
7426                 this.location = location;
7427         }
7428 }
7429
7430 void Error_ExpectingTypeName (Expression expr)
7431 {
7432         if (expr is Invocation){
7433                 report.Error (1002, expr.Location, "Expecting `;'");
7434         } else {
7435                 expr.Error_InvalidExpressionStatement (report);
7436         }
7437 }
7438
7439 void Error_ParameterModifierNotValid (string modifier, Location loc)
7440 {
7441         report.Error (631, loc, "The parameter modifier `{0}' is not valid in this context",
7442                                       modifier);
7443 }
7444
7445 void Error_DuplicateParameterModifier (Location loc, Parameter.Modifier mod)
7446 {
7447         report.Error (1107, loc, "Duplicate parameter modifier `{0}'",
7448                 Parameter.GetModifierSignature (mod));
7449 }
7450
7451 void Error_TypeExpected (Location loc)
7452 {
7453         report.Error (1031, loc, "Type expected");
7454 }
7455
7456 void Error_UnsafeCodeNotAllowed (Location loc)
7457 {
7458         report.Error (227, loc, "Unsafe code requires the `unsafe' command line option to be specified");
7459 }
7460
7461 void Warning_EmptyStatement (Location loc)
7462 {
7463         report.Warning (642, 3, loc, "Possible mistaken empty statement");
7464 }
7465
7466 void Error_NamedArgumentExpected (NamedArgument a)
7467 {
7468         report.Error (1738, a.Location, "Named arguments must appear after the positional arguments");
7469 }
7470
7471 void Error_MissingInitializer (Location loc)
7472 {
7473         report.Error (210, loc, "You must provide an initializer in a fixed or using statement declaration");
7474 }
7475
7476 object Error_AwaitAsIdentifier (object token)
7477 {
7478         if (async_block) {
7479                 report.Error (4003, GetLocation (token), "`await' cannot be used as an identifier within an async method or lambda expression");
7480                 return new LocatedToken ("await", GetLocation (token));
7481         }
7482
7483         return token;
7484 }
7485
7486 void push_current_container (TypeDefinition tc, object partial_token)
7487 {
7488         if (module.Evaluator != null){
7489                 tc.Definition.Modifiers = tc.ModFlags = (tc.ModFlags & ~Modifiers.AccessibilityMask) | Modifiers.PUBLIC;
7490                 if (undo == null)
7491                         undo = new Undo ();
7492
7493                 undo.AddTypeContainer (current_container, tc);
7494         }
7495         
7496         if (partial_token != null)
7497                 current_container.AddPartial (tc);
7498         else
7499                 current_container.AddTypeContainer (tc);
7500                 
7501         ++lexer.parsing_declaration;
7502         current_container = tc;
7503         current_type = tc;
7504 }
7505
7506 TypeContainer pop_current_class ()
7507 {
7508         var retval = current_container;
7509
7510         current_container = current_container.Parent;
7511         current_type = current_type.Parent as TypeDefinition;
7512
7513         return retval;
7514 }
7515
7516 [System.Diagnostics.Conditional ("FULL_AST")]
7517 void StoreModifierLocation (object token, Location loc)
7518 {
7519         if (lbag == null)
7520                 return;
7521
7522         if (mod_locations == null)
7523                 mod_locations = new List<Tuple<Modifiers, Location>> ();
7524
7525         mod_locations.Add (Tuple.Create ((Modifiers) token, loc));
7526 }
7527
7528 [System.Diagnostics.Conditional ("FULL_AST")]
7529 void PushLocation (Location loc)
7530 {
7531         if (location_stack == null)
7532                 location_stack = new Stack<Location> ();
7533
7534         location_stack.Push (loc);
7535 }
7536
7537 Location PopLocation ()
7538 {
7539         if (location_stack == null)
7540                 return Location.Null;
7541
7542         return location_stack.Pop ();
7543 }
7544
7545 string CheckAttributeTarget (int token, string a, Location l)
7546 {
7547         switch (a) {
7548         case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
7549                         return a;
7550         }
7551
7552         if (!Tokenizer.IsValidIdentifier (a)) {
7553                 Error_SyntaxError (token);
7554         } else {
7555                 report.Warning (658, 1, l,
7556                          "`{0}' is invalid attribute target. All attributes in this attribute section will be ignored", a);
7557         }
7558
7559         return string.Empty;
7560 }
7561
7562 static bool IsUnaryOperator (Operator.OpType op)
7563 {
7564         switch (op) {
7565                 
7566         case Operator.OpType.LogicalNot: 
7567         case Operator.OpType.OnesComplement: 
7568         case Operator.OpType.Increment:
7569         case Operator.OpType.Decrement:
7570         case Operator.OpType.True: 
7571         case Operator.OpType.False: 
7572         case Operator.OpType.UnaryPlus: 
7573         case Operator.OpType.UnaryNegation:
7574                 return true;
7575         }
7576         return false;
7577 }
7578
7579 void syntax_error (Location l, string msg)
7580 {
7581         report.Error (1003, l, "Syntax error, " + msg);
7582 }
7583
7584 Tokenizer lexer;
7585
7586 public Tokenizer Lexer {
7587         get {
7588                 return lexer;
7589         }
7590 }                  
7591
7592 public CSharpParser (SeekableStreamReader reader, CompilationSourceFile file, ParserSession session)
7593         : this (reader, file, file.Compiler.Report, session)
7594 {
7595 }
7596
7597 public CSharpParser (SeekableStreamReader reader, CompilationSourceFile file, Report report, ParserSession session)
7598 {
7599         this.file = file;
7600         current_container = current_namespace = file;
7601         
7602         this.module = file.Module;
7603         this.compiler = file.Compiler;
7604         this.settings = compiler.Settings;
7605         this.report = report;
7606         
7607         lang_version = settings.Version;
7608         yacc_verbose_flag = settings.VerboseParserFlag;
7609         doc_support = settings.DocumentationFile != null;
7610         lexer = new Tokenizer (reader, file, session, report);
7611         oob_stack = new Stack<object> ();
7612         lbag = session.LocationsBag;
7613         use_global_stacks = session.UseJayGlobalArrays;
7614         parameters_bucket = session.ParametersStack;
7615 }
7616
7617 public void parse ()
7618 {
7619         eof_token = Token.EOF;
7620         
7621         try {
7622                 if (yacc_verbose_flag > 1)
7623                         yyparse (lexer, new yydebug.yyDebugSimple ());
7624                 else
7625                         yyparse (lexer);
7626                         
7627                 Tokenizer tokenizer = lexer as Tokenizer;
7628                 tokenizer.cleanup ();           
7629         } catch (Exception e){
7630                 if (e is yyParser.yyUnexpectedEof) {
7631                         Error_SyntaxError (yyToken);
7632                         UnexpectedEOF = true;
7633                         return;
7634                 }
7635                         
7636                 if (e is yyParser.yyException) {
7637                         if (report.Errors == 0)
7638                                 report.Error (-25, lexer.Location, "Parsing error");
7639                 } else {
7640                         // Used by compiler-tester to test internal errors
7641                         if (yacc_verbose_flag > 0 || e is FatalException)
7642                                 throw;
7643                 
7644                         report.Error (589, lexer.Location, "Internal compiler error during parsing" + e);
7645                 }
7646         }
7647 }
7648
7649 void CheckToken (int error, int yyToken, string msg, Location loc)
7650 {
7651         if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
7652                 report.Error (error, loc, "{0}: `{1}' is a keyword", msg, GetTokenName (yyToken));
7653         else
7654                 report.Error (error, loc, msg);
7655 }
7656
7657 string ConsumeStoredComment ()
7658 {
7659         string s = tmpComment;
7660         tmpComment = null;
7661         Lexer.doc_state = XmlCommentState.Allowed;
7662         return s;
7663 }
7664
7665 void FeatureIsNotAvailable (Location loc, string feature)
7666 {
7667         report.FeatureIsNotAvailable (compiler, loc, feature);
7668 }
7669
7670 Location GetLocation (object obj)
7671 {
7672         var lt = obj as LocatedToken;
7673         if (lt != null)
7674                 return lt.Location;
7675                 
7676         var mn = obj as MemberName;
7677         if (mn != null)
7678                 return mn.Location;
7679                 
7680         var expr = obj as Expression;
7681         if (expr != null)
7682                 return expr.Location;
7683
7684         return lexer.Location;
7685 }
7686
7687 void start_block (Location loc)
7688 {
7689         if (current_block == null) {
7690                 current_block = new ToplevelBlock (compiler, current_local_parameters, loc);
7691                 parsing_anonymous_method = false;
7692         } else if (parsing_anonymous_method) {
7693                 current_block = new ParametersBlock (current_block, current_local_parameters, loc);
7694                 parsing_anonymous_method = false;
7695         } else {
7696                 current_block = new ExplicitBlock (current_block, loc, Location.Null);
7697         }
7698 }
7699
7700 Block
7701 end_block (Location loc)
7702 {
7703         Block retval = current_block.Explicit;
7704         retval.SetEndLocation (loc);
7705         current_block = retval.Parent;
7706         return retval;
7707 }
7708
7709 void start_anonymous (bool isLambda, ParametersCompiled parameters, bool isAsync, Location loc)
7710 {
7711         oob_stack.Push (current_anonymous_method);
7712         oob_stack.Push (current_local_parameters);
7713         oob_stack.Push (current_variable);
7714         oob_stack.Push (async_block);
7715
7716         current_local_parameters = parameters;
7717         if (isLambda) {
7718                 if (lang_version <= LanguageVersion.ISO_2)
7719                         FeatureIsNotAvailable (loc, "lambda expressions");
7720
7721                 current_anonymous_method = new LambdaExpression (loc);
7722         } else {
7723                 if (lang_version == LanguageVersion.ISO_1)
7724                         FeatureIsNotAvailable (loc, "anonymous methods");
7725                         
7726                 current_anonymous_method = new AnonymousMethodExpression (loc);
7727         }
7728
7729         async_block = isAsync;
7730         // Force the next block to be created as a ToplevelBlock
7731         parsing_anonymous_method = true;
7732 }
7733
7734 /*
7735  * Completes the anonymous method processing, if lambda_expr is null, this
7736  * means that we have a Statement instead of an Expression embedded 
7737  */
7738 AnonymousMethodExpression end_anonymous (ParametersBlock anon_block)
7739 {
7740         AnonymousMethodExpression retval;
7741
7742         if (async_block)
7743                 anon_block.IsAsync = true;
7744
7745         current_anonymous_method.Block = anon_block;
7746         retval = current_anonymous_method;
7747
7748         async_block = (bool) oob_stack.Pop ();
7749         current_variable = (BlockVariable) oob_stack.Pop ();
7750         current_local_parameters = (ParametersCompiled) oob_stack.Pop ();
7751         current_anonymous_method = (AnonymousMethodExpression) oob_stack.Pop ();
7752
7753         return retval;
7754 }
7755
7756 void Error_SyntaxError (int token)
7757 {
7758         Error_SyntaxError (0, token);
7759 }
7760
7761 void Error_SyntaxError (int error_code, int token)
7762 {
7763         Error_SyntaxError (error_code, token, "Unexpected symbol");
7764 }
7765
7766 void Error_SyntaxError (int error_code, int token, string msg)
7767 {
7768         Lexer.CompleteOnEOF = false;
7769
7770         // An error message has been reported by tokenizer
7771         if (token == Token.ERROR)
7772                 return;
7773         
7774         // Avoid duplicit error message after unterminated string literals
7775         if (token == Token.LITERAL && lexer.Location.Column == 0)
7776                 return;
7777
7778         string symbol = GetSymbolName (token);
7779         string expecting = GetExpecting ();
7780         var loc = lexer.Location - symbol.Length;
7781         
7782         if (error_code == 0) {
7783                 if (expecting == "`identifier'") {
7784                         if (token > Token.FIRST_KEYWORD && token < Token.LAST_KEYWORD) {
7785                                 report.Error (1041, loc, "Identifier expected, `{0}' is a keyword", symbol);
7786                                 return;
7787                         }
7788                         
7789                         error_code = 1001;
7790                         expecting = "identifier";
7791                 } else if (expecting == "`)'") {
7792                         error_code = 1026;
7793                 } else {
7794                         error_code = 1525;
7795                 }
7796         }
7797         
7798         if (string.IsNullOrEmpty (expecting))
7799                 report.Error (error_code, loc, "{1} `{0}'", symbol, msg);
7800         else
7801                 report.Error (error_code, loc, "{2} `{0}', expecting {1}", symbol, expecting, msg);       
7802 }
7803
7804 string GetExpecting ()
7805 {
7806         int [] tokens = yyExpectingTokens (yyExpectingState);
7807         var names = new List<string> (tokens.Length);
7808         bool has_type = false;
7809         bool has_identifier = false;
7810         for (int i = 0; i < tokens.Length; i++){
7811                 int token = tokens [i];
7812                 has_identifier |= token == Token.IDENTIFIER;
7813                 
7814                 string name = GetTokenName (token);
7815                 if (name == "<internal>")
7816                         continue;
7817                         
7818                 has_type |= name == "type";
7819                 if (names.Contains (name))
7820                         continue;
7821                 
7822                 names.Add (name);
7823         }
7824
7825         //
7826         // Too many tokens to enumerate
7827         //
7828         if (names.Count > 8)
7829                 return null;
7830
7831         if (has_type && has_identifier)
7832                 names.Remove ("identifier");
7833
7834         if (names.Count == 1)
7835                 return "`" + GetTokenName (tokens [0]) + "'";
7836         
7837         StringBuilder sb = new StringBuilder ();
7838         names.Sort ();
7839         int count = names.Count;
7840         for (int i = 0; i < count; i++){
7841                 bool last = i + 1 == count;
7842                 if (last)
7843                         sb.Append ("or ");
7844                 sb.Append ('`');
7845                 sb.Append (names [i]);
7846                 sb.Append (last ? "'" : count < 3 ? "' " : "', ");
7847         }
7848         return sb.ToString ();
7849 }
7850
7851
7852 string GetSymbolName (int token)
7853 {
7854         switch (token){
7855         case Token.LITERAL:
7856                 return ((Constant)lexer.Value).GetValue ().ToString ();
7857         case Token.IDENTIFIER:
7858                 return ((LocatedToken)lexer.Value).Value;
7859
7860         case Token.BOOL:
7861                 return "bool";
7862         case Token.BYTE:
7863                 return "byte";
7864         case Token.CHAR:
7865                 return "char";
7866         case Token.VOID:
7867                 return "void";
7868         case Token.DECIMAL:
7869                 return "decimal";
7870         case Token.DOUBLE:
7871                 return "double";
7872         case Token.FLOAT:
7873                 return "float";
7874         case Token.INT:
7875                 return "int";
7876         case Token.LONG:
7877                 return "long";
7878         case Token.SBYTE:
7879                 return "sbyte";
7880         case Token.SHORT:
7881                 return "short";
7882         case Token.STRING:
7883                 return "string";
7884         case Token.UINT:
7885                 return "uint";
7886         case Token.ULONG:
7887                 return "ulong";
7888         case Token.USHORT:
7889                 return "ushort";
7890         case Token.OBJECT:
7891                 return "object";
7892                 
7893         case Token.PLUS:
7894                 return "+";
7895         case Token.UMINUS:
7896         case Token.MINUS:
7897                 return "-";
7898         case Token.BANG:
7899                 return "!";
7900         case Token.BITWISE_AND:
7901                 return "&";
7902         case Token.BITWISE_OR:
7903                 return "|";
7904         case Token.STAR:
7905                 return "*";
7906         case Token.PERCENT:
7907                 return "%";
7908         case Token.DIV:
7909                 return "/";
7910         case Token.CARRET:
7911                 return "^";
7912         case Token.OP_INC:
7913                 return "++";
7914         case Token.OP_DEC:
7915                 return "--";
7916         case Token.OP_SHIFT_LEFT:
7917                 return "<<";
7918         case Token.OP_SHIFT_RIGHT:
7919                 return ">>";
7920         case Token.OP_LT:
7921                 return "<";
7922         case Token.OP_GT:
7923                 return ">";
7924         case Token.OP_LE:
7925                 return "<=";
7926         case Token.OP_GE:
7927                 return ">=";
7928         case Token.OP_EQ:
7929                 return "==";
7930         case Token.OP_NE:
7931                 return "!=";
7932         case Token.OP_AND:
7933                 return "&&";
7934         case Token.OP_OR:
7935                 return "||";
7936         case Token.OP_PTR:
7937                 return "->";
7938         case Token.OP_COALESCING:       
7939                 return "??";
7940         case Token.OP_MULT_ASSIGN:
7941                 return "*=";
7942         case Token.OP_DIV_ASSIGN:
7943                 return "/=";
7944         case Token.OP_MOD_ASSIGN:
7945                 return "%=";
7946         case Token.OP_ADD_ASSIGN:
7947                 return "+=";
7948         case Token.OP_SUB_ASSIGN:
7949                 return "-=";
7950         case Token.OP_SHIFT_LEFT_ASSIGN:
7951                 return "<<=";
7952         case Token.OP_SHIFT_RIGHT_ASSIGN:
7953                 return ">>=";
7954         case Token.OP_AND_ASSIGN:
7955                 return "&=";
7956         case Token.OP_XOR_ASSIGN:
7957                 return "^=";
7958         case Token.OP_OR_ASSIGN:
7959                 return "|=";
7960         }
7961
7962         return GetTokenName (token);
7963 }
7964
7965 static string GetTokenName (int token)
7966 {
7967         switch (token){
7968         case Token.ABSTRACT:
7969                 return "abstract";
7970         case Token.AS:
7971                 return "as";
7972         case Token.ADD:
7973                 return "add";
7974         case Token.ASYNC:
7975                 return "async";
7976         case Token.BASE:
7977                 return "base";
7978         case Token.BREAK:
7979                 return "break";
7980         case Token.CASE:
7981                 return "case";
7982         case Token.CATCH:
7983                 return "catch";
7984         case Token.CHECKED:
7985                 return "checked";
7986         case Token.CLASS:
7987                 return "class";
7988         case Token.CONST:
7989                 return "const";
7990         case Token.CONTINUE:
7991                 return "continue";
7992         case Token.DEFAULT:
7993                 return "default";
7994         case Token.DELEGATE:
7995                 return "delegate";
7996         case Token.DO:
7997                 return "do";
7998         case Token.ELSE:
7999                 return "else";
8000         case Token.ENUM:
8001                 return "enum";
8002         case Token.EVENT:
8003                 return "event";
8004         case Token.EXPLICIT:
8005                 return "explicit";
8006         case Token.EXTERN:
8007         case Token.EXTERN_ALIAS:
8008                 return "extern";
8009         case Token.FALSE:
8010                 return "false";
8011         case Token.FINALLY:
8012                 return "finally";
8013         case Token.FIXED:
8014                 return "fixed";
8015         case Token.FOR:
8016                 return "for";
8017         case Token.FOREACH:
8018                 return "foreach";
8019         case Token.GOTO:
8020                 return "goto";
8021         case Token.IF:
8022                 return "if";
8023         case Token.IMPLICIT:
8024                 return "implicit";
8025         case Token.IN:
8026                 return "in";
8027         case Token.INTERFACE:
8028                 return "interface";
8029         case Token.INTERNAL:
8030                 return "internal";
8031         case Token.IS:
8032                 return "is";
8033         case Token.LOCK:
8034                 return "lock";
8035         case Token.NAMESPACE:
8036                 return "namespace";
8037         case Token.NEW:
8038                 return "new";
8039         case Token.NULL:
8040                 return "null";
8041         case Token.OPERATOR:
8042                 return "operator";
8043         case Token.OUT:
8044                 return "out";
8045         case Token.OVERRIDE:
8046                 return "override";
8047         case Token.PARAMS:
8048                 return "params";
8049         case Token.PRIVATE:
8050                 return "private";
8051         case Token.PROTECTED:
8052                 return "protected";
8053         case Token.PUBLIC:
8054                 return "public";
8055         case Token.READONLY:
8056                 return "readonly";
8057         case Token.REF:
8058                 return "ref";
8059         case Token.RETURN:
8060                 return "return";
8061         case Token.REMOVE:
8062                 return "remove";
8063         case Token.SEALED:
8064                 return "sealed";
8065         case Token.SIZEOF:
8066                 return "sizeof";
8067         case Token.STACKALLOC:
8068                 return "stackalloc";
8069         case Token.STATIC:
8070                 return "static";
8071         case Token.STRUCT:
8072                 return "struct";
8073         case Token.SWITCH:
8074                 return "switch";
8075         case Token.THIS:
8076                 return "this";
8077         case Token.THROW:
8078                 return "throw";
8079         case Token.TRUE:
8080                 return "true";
8081         case Token.TRY:
8082                 return "try";
8083         case Token.TYPEOF:
8084                 return "typeof";
8085         case Token.UNCHECKED:
8086                 return "unchecked";
8087         case Token.UNSAFE:
8088                 return "unsafe";
8089         case Token.USING:
8090                 return "using";
8091         case Token.VIRTUAL:
8092                 return "virtual";
8093         case Token.VOLATILE:
8094                 return "volatile";
8095         case Token.WHERE:
8096                 return "where";
8097         case Token.WHILE:
8098                 return "while";
8099         case Token.ARGLIST:
8100                 return "__arglist";
8101         case Token.REFVALUE:
8102                 return "__refvalue";
8103         case Token.REFTYPE:
8104                 return "__reftype";
8105         case Token.MAKEREF:
8106                 return "__makeref";
8107         case Token.PARTIAL:
8108                 return "partial";
8109         case Token.ARROW:
8110                 return "=>";
8111         case Token.FROM:
8112         case Token.FROM_FIRST:
8113                 return "from";
8114         case Token.JOIN:
8115                 return "join";
8116         case Token.ON:
8117                 return "on";
8118         case Token.EQUALS:
8119                 return "equals";
8120         case Token.SELECT:
8121                 return "select";
8122         case Token.GROUP:
8123                 return "group";
8124         case Token.BY:
8125                 return "by";
8126         case Token.LET:
8127                 return "let";
8128         case Token.ORDERBY:
8129                 return "orderby";
8130         case Token.ASCENDING:
8131                 return "ascending";
8132         case Token.DESCENDING:
8133                 return "descending";
8134         case Token.INTO:
8135                 return "into";
8136         case Token.GET:
8137                 return "get";
8138         case Token.SET:
8139                 return "set";
8140         case Token.OPEN_BRACE:
8141                 return "{";
8142         case Token.CLOSE_BRACE:
8143                 return "}";
8144         case Token.OPEN_BRACKET:
8145         case Token.OPEN_BRACKET_EXPR:
8146                 return "[";
8147         case Token.CLOSE_BRACKET:
8148                 return "]";
8149         case Token.OPEN_PARENS_CAST:
8150         case Token.OPEN_PARENS_LAMBDA:
8151         case Token.OPEN_PARENS:
8152                 return "(";
8153         case Token.CLOSE_PARENS:
8154                 return ")";
8155         case Token.DOT:
8156                 return ".";
8157         case Token.COMMA:
8158                 return ",";
8159         case Token.DEFAULT_COLON:
8160                 return "default:";
8161         case Token.COLON:
8162                 return ":";
8163         case Token.SEMICOLON:
8164                 return ";";
8165         case Token.TILDE:
8166                 return "~";
8167         case Token.WHEN:
8168                 return "when";
8169         case Token.INTERPOLATED_STRING_END:
8170                 return "}";
8171         case Token.INTERPOLATED_STRING:
8172                 return "${";
8173
8174         case Token.PLUS:
8175         case Token.UMINUS:
8176         case Token.MINUS:
8177         case Token.BANG:
8178         case Token.OP_LT:
8179         case Token.OP_GT:
8180         case Token.BITWISE_AND:
8181         case Token.BITWISE_OR:
8182         case Token.STAR:
8183         case Token.PERCENT:
8184         case Token.DIV:
8185         case Token.CARRET:
8186         case Token.OP_INC:
8187         case Token.OP_DEC:
8188         case Token.OP_SHIFT_LEFT:
8189         case Token.OP_SHIFT_RIGHT:
8190         case Token.OP_LE:
8191         case Token.OP_GE:
8192         case Token.OP_EQ:
8193         case Token.OP_NE:
8194         case Token.OP_AND:
8195         case Token.OP_OR:
8196         case Token.OP_PTR:
8197         case Token.OP_COALESCING:       
8198         case Token.OP_MULT_ASSIGN:
8199         case Token.OP_DIV_ASSIGN:
8200         case Token.OP_MOD_ASSIGN:
8201         case Token.OP_ADD_ASSIGN:
8202         case Token.OP_SUB_ASSIGN:
8203         case Token.OP_SHIFT_LEFT_ASSIGN:
8204         case Token.OP_SHIFT_RIGHT_ASSIGN:
8205         case Token.OP_AND_ASSIGN:
8206         case Token.OP_XOR_ASSIGN:
8207         case Token.OP_OR_ASSIGN:
8208         case Token.INTERR_OPERATOR:
8209                 return "<operator>";
8210
8211         case Token.BOOL:
8212         case Token.BYTE:
8213         case Token.CHAR:
8214         case Token.VOID:
8215         case Token.DECIMAL:
8216         case Token.DOUBLE:
8217         case Token.FLOAT:
8218         case Token.INT:
8219         case Token.LONG:
8220         case Token.SBYTE:
8221         case Token.SHORT:
8222         case Token.STRING:
8223         case Token.UINT:
8224         case Token.ULONG:
8225         case Token.USHORT:
8226         case Token.OBJECT:
8227                 return "type";
8228         
8229         case Token.ASSIGN:
8230                 return "=";
8231         case Token.OP_GENERICS_LT:
8232         case Token.GENERIC_DIMENSION:
8233                 return "<";
8234         case Token.OP_GENERICS_GT:
8235                 return ">";
8236         case Token.INTERR:
8237         case Token.INTERR_NULLABLE:
8238                 return "?";
8239         case Token.DOUBLE_COLON:
8240                 return "::";
8241         case Token.LITERAL:
8242                 return "value";
8243         case Token.IDENTIFIER:
8244         case Token.AWAIT:
8245                 return "identifier";
8246
8247         case Token.EOF:
8248                 return "end-of-file";
8249
8250                 // All of these are internal.
8251         case Token.NONE:
8252         case Token.ERROR:
8253         case Token.FIRST_KEYWORD:
8254         case Token.EVAL_COMPILATION_UNIT_PARSER:
8255         case Token.EVAL_USING_DECLARATIONS_UNIT_PARSER:
8256         case Token.EVAL_STATEMENT_PARSER:
8257         case Token.LAST_KEYWORD:
8258         case Token.GENERATE_COMPLETION:
8259         case Token.COMPLETE_COMPLETION:
8260                 return "<internal>";
8261
8262                 // A bit more robust.
8263         default:
8264                 return yyNames [token];
8265         }
8266 }
8267
8268 /* end end end */
8269 }