[runtime] Fix potential overflow when using mono_msec_ticks
[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                 $$ = new InterpolatedString ((StringLiteral) $1, (List<Expression>) $2, (StringLiteral) $3);
3355           }
3356         | INTERPOLATED_STRING_END
3357           {
3358                 $$ = new InterpolatedString ((StringLiteral) $1, null, null);
3359           }
3360         ;
3361
3362 interpolations
3363         : interpolation
3364           {
3365                 var list = new List<Expression> ();
3366                 list.Add ((InterpolatedStringInsert) $1);
3367                 $$ = list;
3368           }
3369         | interpolations INTERPOLATED_STRING interpolation
3370           {
3371                 var list = (List<Expression>) $1;
3372                 list.Add ((StringLiteral) $2);
3373                 list.Add ((InterpolatedStringInsert) $3);
3374                 $$ = list;
3375           }
3376         ;
3377
3378 interpolation
3379         : expression
3380           {
3381                 $$ = new InterpolatedStringInsert ((Expression) $1);
3382           }
3383         | expression COMMA expression
3384           {
3385                 $$ = new InterpolatedStringInsert ((Expression) $1) {
3386                         Alignment = (Expression)$3
3387                 };
3388           }
3389         | expression COLON
3390           {
3391                 lexer.parsing_interpolation_format = true;
3392           }
3393           LITERAL
3394           {
3395                 lexer.parsing_interpolation_format = false;
3396
3397                 $$ = new InterpolatedStringInsert ((Expression) $1) {
3398                         Format = (string)$4
3399                 };
3400           }
3401         | expression COMMA expression COLON
3402           {
3403                 lexer.parsing_interpolation_format = true;
3404           }
3405           LITERAL
3406           {
3407                 lexer.parsing_interpolation_format = false;
3408
3409                 $$ = new InterpolatedStringInsert ((Expression) $1) {
3410                         Alignment = (Expression)$3,
3411                         Format = (string) $6
3412                 };
3413           }
3414         ;
3415
3416
3417 //
3418 // Here is the trick, tokenizer may think that parens is a special but
3419 // parser is interested in open parens only, so we merge them.
3420 // Consider: if (a)foo ();
3421 //
3422 open_parens_any
3423         : OPEN_PARENS
3424         | OPEN_PARENS_CAST
3425         ;
3426
3427 // 
3428 // Use this production to accept closing parenthesis or 
3429 // performing completion
3430 //
3431 close_parens
3432         : CLOSE_PARENS
3433         | COMPLETE_COMPLETION
3434         ;
3435
3436
3437 parenthesized_expression
3438         : OPEN_PARENS expression CLOSE_PARENS
3439           {
3440                 $$ = new ParenthesizedExpression ((Expression) $2, GetLocation ($1));
3441                 lbag.AddLocation ($$, GetLocation ($1), GetLocation ($3));
3442           }
3443         | OPEN_PARENS expression COMPLETE_COMPLETION
3444           {
3445                 $$ = new ParenthesizedExpression ((Expression) $2, GetLocation ($1));
3446           }
3447         ;
3448
3449 member_access
3450         : primary_expression DOT identifier_inside_body opt_type_argument_list
3451           {
3452                 var lt = (LocatedToken) $3;
3453                 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
3454                 lbag.AddLocation ($$, GetLocation ($2));
3455           }
3456         | primary_expression DOT identifier_inside_body generic_dimension
3457           {
3458                 var lt = (LocatedToken) $3;
3459                 $$ = new MemberAccess ((Expression) $1, lt.Value, (int) $4, lt.Location);
3460                 lbag.AddLocation ($$, GetLocation ($2));
3461           }
3462         | primary_expression INTERR_OPERATOR DOT identifier_inside_body opt_type_argument_list
3463           {
3464                 if (lang_version < LanguageVersion.V_6)
3465                         FeatureIsNotAvailable (GetLocation ($2), "null propagating operator");
3466
3467                 var lt = (LocatedToken) $4;
3468                 $$ = new ConditionalMemberAccess ((Expression) $1, lt.Value, (TypeArguments) $5, lt.Location);
3469                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($3));
3470           }
3471         | builtin_types DOT identifier_inside_body opt_type_argument_list
3472           {
3473                 var lt = (LocatedToken) $3;
3474                 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
3475                 lbag.AddLocation ($$, GetLocation ($2));
3476           }
3477         | BASE DOT identifier_inside_body opt_type_argument_list
3478           {
3479                 var lt = (LocatedToken) $3;
3480                 $$ = new MemberAccess (new BaseThis (GetLocation ($1)), lt.Value, (TypeArguments) $4, lt.Location);
3481                 lbag.AddLocation ($$, GetLocation ($2));
3482           }
3483         | AWAIT DOT identifier_inside_body opt_type_argument_list
3484           {
3485                 var lt = (LocatedToken) $3;
3486                 $$ = new MemberAccess (new SimpleName ("await", ((LocatedToken) $1).Location), lt.Value, (TypeArguments) $4, lt.Location);
3487                 lbag.AddLocation ($$, GetLocation ($2));
3488           }
3489         | qualified_alias_member identifier_inside_body opt_type_argument_list
3490           {
3491                 var lt1 = (LocatedToken) $1;
3492                 var lt2 = (LocatedToken) $2;
3493
3494                 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, (TypeArguments) $3, lt1.Location);
3495                 lbag.AddLocation ($$, GetLocation ($2));
3496           }
3497         | qualified_alias_member identifier_inside_body generic_dimension
3498           {
3499                 var lt1 = (LocatedToken) $1;
3500                 var lt2 = (LocatedToken) $2;
3501
3502                 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, (int) $3, lt1.Location);
3503                 lbag.AddLocation ($$, GetLocation ($2));
3504           }
3505         | primary_expression DOT GENERATE_COMPLETION {
3506                 $$ = new CompletionMemberAccess ((Expression) $1, null,GetLocation ($3));
3507           }
3508         | primary_expression DOT IDENTIFIER GENERATE_COMPLETION {
3509                 var lt = (LocatedToken) $3;
3510                 $$ = new CompletionMemberAccess ((Expression) $1, lt.Value, lt.Location);
3511           }
3512         | builtin_types DOT GENERATE_COMPLETION
3513           {
3514                 $$ = new CompletionMemberAccess ((Expression) $1, null, lexer.Location);
3515           }
3516         | builtin_types DOT IDENTIFIER GENERATE_COMPLETION {
3517                 var lt = (LocatedToken) $3;
3518                 $$ = new CompletionMemberAccess ((Expression) $1, lt.Value, lt.Location);
3519           }
3520         ;
3521
3522 invocation_expression
3523         : primary_expression open_parens_any opt_argument_list close_parens
3524           {
3525                 $$ = new Invocation ((Expression) $1, (Arguments) $3);
3526                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
3527           }
3528         | primary_expression open_parens_any argument_list error
3529           {
3530                 Error_SyntaxError (yyToken);
3531
3532                 $$ = new Invocation ((Expression) $1, (Arguments) $3);
3533                 lbag.AddLocation ($$, GetLocation ($2));
3534           }
3535         | primary_expression open_parens_any error
3536           {
3537                 Error_SyntaxError (yyToken);
3538
3539                 $$ = new Invocation ((Expression) $1, null);
3540                 lbag.AddLocation ($$, GetLocation ($2));
3541           }
3542         ;
3543
3544 opt_object_or_collection_initializer
3545         : /* empty */           { $$ = null; }
3546         | object_or_collection_initializer
3547         ;
3548
3549 object_or_collection_initializer
3550         : OPEN_BRACE opt_member_initializer_list close_brace_or_complete_completion
3551           {
3552                 if ($2 == null) {
3553                         $$ = new CollectionOrObjectInitializers (GetLocation ($1));
3554                 } else {
3555                         $$ = new CollectionOrObjectInitializers ((List<Expression>) $2, GetLocation ($1));
3556                 }
3557                 lbag.AddLocation ($$, GetLocation ($3));
3558           }
3559         | OPEN_BRACE member_initializer_list COMMA CLOSE_BRACE
3560           {
3561                 $$ = new CollectionOrObjectInitializers ((List<Expression>) $2, GetLocation ($1));
3562                 lbag.AddLocation ($$, GetLocation ($3), GetLocation ($4));
3563           }
3564         ;
3565
3566 opt_member_initializer_list
3567         : /* empty */           { $$ = null; }
3568         | member_initializer_list
3569         {
3570                 $$ = $1;
3571         }
3572         ;
3573
3574 member_initializer_list
3575         : member_initializer
3576           {
3577                 var a = new List<Expression> ();
3578                 a.Add ((Expression) $1);
3579                 $$ = a;
3580           }
3581         | member_initializer_list COMMA member_initializer
3582           {
3583                 var a = (List<Expression>)$1;
3584                 a.Add ((Expression) $3);
3585                 $$ = a;
3586           }
3587         | member_initializer_list error {
3588                 Error_SyntaxError (yyToken);
3589                 $$ = $1;
3590           }
3591         ;
3592
3593 member_initializer
3594         : IDENTIFIER ASSIGN initializer_value
3595           {
3596                 var lt = (LocatedToken) $1;
3597                 $$ = new ElementInitializer (lt.Value, (Expression)$3, lt.Location);
3598                 lbag.AddLocation ($$, GetLocation ($2));
3599           }
3600         | AWAIT ASSIGN initializer_value
3601           {
3602                 var lt = (LocatedToken) Error_AwaitAsIdentifier ($1);
3603                 $$ = new ElementInitializer (lt.Value, (Expression)$3, lt.Location);
3604                 lbag.AddLocation ($$, GetLocation ($2));
3605           }
3606         | GENERATE_COMPLETION 
3607           {
3608                 $$ = new CompletionElementInitializer (null, GetLocation ($1));
3609           }
3610         | non_assignment_expression opt_COMPLETE_COMPLETION  {
3611                 CompletionSimpleName csn = $1 as CompletionSimpleName;
3612                 if (csn == null)
3613                         $$ = new CollectionElementInitializer ((Expression)$1);
3614                 else
3615                         $$ = new CompletionElementInitializer (csn.Prefix, csn.Location);
3616           }
3617         | OPEN_BRACE expression_list CLOSE_BRACE
3618           {
3619                 if ($2 == null)
3620                         $$ = new CollectionElementInitializer (GetLocation ($1));
3621                 else
3622                         $$ = new CollectionElementInitializer ((List<Expression>)$2, GetLocation ($1));
3623
3624                 lbag.AddLocation ($$, GetLocation ($3));
3625           }
3626         | OPEN_BRACKET_EXPR argument_list CLOSE_BRACKET ASSIGN initializer_value
3627           {
3628                 if (lang_version < LanguageVersion.V_6)
3629                         FeatureIsNotAvailable (GetLocation ($1), "dictionary initializer");
3630
3631                 $$ = new DictionaryElementInitializer ((Arguments)$2, (Expression) $5, GetLocation ($1));
3632                 lbag.AddLocation ($$, GetLocation ($3), GetLocation ($4));
3633           }
3634         | OPEN_BRACE CLOSE_BRACE
3635           {
3636                 report.Error (1920, GetLocation ($1), "An element initializer cannot be empty");
3637                 $$ = new CollectionElementInitializer (GetLocation ($1));
3638                 lbag.AddLocation ($$, GetLocation ($2));
3639           }
3640         ;
3641
3642 initializer_value
3643         : expression
3644         | object_or_collection_initializer
3645         ;
3646
3647 opt_argument_list
3648         : /* empty */           { $$ = null; }
3649         | argument_list
3650         ;
3651
3652 argument_list
3653         : argument_or_named_argument
3654           { 
3655                 Arguments list = new Arguments (4);
3656                 list.Add ((Argument) $1);
3657                 $$ = list;
3658           }
3659         | argument_list COMMA argument
3660           {
3661                 Arguments list = (Arguments) $1;
3662                 if (list [list.Count - 1] is NamedArgument)
3663                         Error_NamedArgumentExpected ((NamedArgument) list [list.Count - 1]);
3664                 
3665                 list.Add ((Argument) $3);
3666                 $$ = list;
3667           }
3668         | argument_list COMMA named_argument
3669           {
3670                 Arguments list = (Arguments) $1;
3671                 NamedArgument a = (NamedArgument) $3;
3672                 for (int i = 0; i < list.Count; ++i) {
3673                         NamedArgument na = list [i] as NamedArgument;
3674                         if (na != null && na.Name == a.Name)
3675                                 report.Error (1740, na.Location, "Named argument `{0}' specified multiple times",
3676                                         na.Name);
3677                 }
3678                 
3679                 list.Add (a);
3680                 $$ = list;
3681           }
3682         | argument_list COMMA error
3683           {
3684                 if (lexer.putback_char == -1)
3685                         lexer.putback (')'); // TODO: Wrong but what can I do
3686                 Error_SyntaxError (yyToken);
3687                 $$ = $1;
3688           }
3689         | COMMA error
3690           {
3691                 report.Error (839, GetLocation ($1), "An argument is missing");
3692                 $$ = null;
3693           }
3694         ;
3695
3696 argument
3697         : expression
3698           {
3699                 $$ = new Argument ((Expression) $1);
3700           }
3701         | non_simple_argument
3702         ;
3703
3704 argument_or_named_argument
3705         : argument
3706         | named_argument
3707         ;
3708
3709 non_simple_argument
3710         : REF variable_reference 
3711           { 
3712                 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
3713                 lbag.AddLocation ($$, GetLocation ($1));
3714           }
3715         | REF declaration_expression
3716           {
3717                 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
3718           }
3719         | OUT variable_reference 
3720           { 
3721                 $$ = new Argument ((Expression) $2, Argument.AType.Out);
3722                 lbag.AddLocation ($$, GetLocation ($1));
3723           }
3724         | OUT declaration_expression
3725           {
3726                 $$ = new Argument ((Expression) $2, Argument.AType.Out);
3727           }
3728         | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
3729           {
3730                 $$ = new Argument (new Arglist ((Arguments) $3, GetLocation ($1)));
3731                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
3732           }
3733         | ARGLIST OPEN_PARENS CLOSE_PARENS
3734           {
3735                 $$ = new Argument (new Arglist (GetLocation ($1)));
3736                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($3));
3737           }       
3738         ;
3739
3740 declaration_expression
3741         : OPEN_PARENS declaration_expression CLOSE_PARENS
3742           {
3743                 $$ = new ParenthesizedExpression ((Expression) $2, GetLocation ($1));
3744                 lbag.AddLocation ($$, GetLocation ($1), GetLocation ($3));
3745           }
3746 /*
3747         | CHECKED open_parens_any declaration_expression CLOSE_PARENS
3748           {
3749                 $$ = new CheckedExpr ((Expression) $3, GetLocation ($1));
3750                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
3751           }
3752         | UNCHECKED open_parens_any declaration_expression CLOSE_PARENS
3753           {
3754                 $$ = new UnCheckedExpr ((Expression) $3, GetLocation ($1));
3755                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
3756           }
3757 */
3758         | variable_type identifier_inside_body
3759           {
3760                 if (lang_version != LanguageVersion.Experimental)
3761                         FeatureIsNotAvailable (GetLocation ($1), "declaration expression");
3762
3763                 var lt = (LocatedToken) $2;
3764                 var lv = new LocalVariable (current_block, lt.Value, lt.Location);
3765                 current_block.AddLocalName (lv);
3766                 $$ = new DeclarationExpression ((FullNamedExpression) $1, lv);
3767           }
3768         | variable_type identifier_inside_body ASSIGN expression
3769           {
3770                 if (lang_version != LanguageVersion.Experimental)
3771                         FeatureIsNotAvailable (GetLocation ($1), "declaration expression");
3772
3773                 var lt = (LocatedToken) $2;
3774                 var lv = new LocalVariable (current_block, lt.Value, lt.Location);
3775                 current_block.AddLocalName (lv);
3776                 $$ = new DeclarationExpression ((FullNamedExpression) $1, lv) {
3777                         Initializer = (Expression) $4
3778                 };
3779           }
3780         ;
3781
3782 variable_reference
3783         : expression
3784         ;
3785
3786 element_access
3787         : primary_expression OPEN_BRACKET_EXPR expression_list_arguments CLOSE_BRACKET  
3788           {
3789                 $$ = new ElementAccess ((Expression) $1, (Arguments) $3, GetLocation ($2));
3790                 lbag.AddLocation ($$, GetLocation ($4));
3791           }
3792         | primary_expression INTERR_OPERATOR OPEN_BRACKET_EXPR expression_list_arguments CLOSE_BRACKET  
3793           {
3794                 if (lang_version < LanguageVersion.V_6)
3795                         FeatureIsNotAvailable (GetLocation ($2), "null propagating operator");
3796
3797                 $$ = new ElementAccess ((Expression) $1, (Arguments) $4, GetLocation ($3)) {
3798                         ConditionalAccess = true
3799                 };
3800
3801                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($5));
3802           }
3803         | primary_expression OPEN_BRACKET_EXPR expression_list_arguments error
3804           {
3805                 Error_SyntaxError (yyToken);
3806                 $$ = new ElementAccess ((Expression) $1, (Arguments) $3, GetLocation ($2));
3807           }
3808         | primary_expression OPEN_BRACKET_EXPR error
3809           {
3810                 Error_SyntaxError (yyToken);
3811                 $$ = new ElementAccess ((Expression) $1, null, GetLocation ($2));
3812           }
3813         ;
3814
3815 expression_list
3816         : expression_or_error
3817           {
3818                 var list = new List<Expression> (4);
3819                 list.Add ((Expression) $1);
3820                 $$ = list;
3821           }
3822         | expression_list COMMA expression_or_error
3823           {
3824                 var list = (List<Expression>) $1;
3825                 list.Add ((Expression) $3);
3826                 $$ = list;
3827           }
3828         ;
3829         
3830 expression_list_arguments
3831         : expression_list_argument
3832           {
3833                 Arguments args = new Arguments (4);
3834                 args.Add ((Argument) $1);
3835                 $$ = args;
3836           }
3837         | expression_list_arguments COMMA expression_list_argument
3838           {
3839                 Arguments args = (Arguments) $1;
3840                 if (args [args.Count - 1] is NamedArgument && !($3 is NamedArgument))
3841                         Error_NamedArgumentExpected ((NamedArgument) args [args.Count - 1]);
3842           
3843                 args.Add ((Argument) $3);
3844                 $$ = args;        
3845           }
3846         ;
3847         
3848 expression_list_argument
3849         : expression
3850           {
3851                 $$ = new Argument ((Expression) $1);
3852           }
3853         | named_argument
3854         ;
3855
3856 this_access
3857         : THIS
3858           {
3859                 $$ = new This (GetLocation ($1));
3860           }
3861         ;
3862
3863 base_access
3864         : BASE OPEN_BRACKET_EXPR expression_list_arguments CLOSE_BRACKET
3865           {
3866                 $$ = new ElementAccess (new BaseThis (GetLocation ($1)), (Arguments) $3, GetLocation ($2));
3867                 lbag.AddLocation ($$, GetLocation ($4));
3868           }
3869         | BASE OPEN_BRACKET error
3870           {
3871                 Error_SyntaxError (yyToken);
3872                 $$ = new ElementAccess (null, null, GetLocation ($2));
3873           }
3874         ;
3875
3876 post_increment_expression
3877         : primary_expression OP_INC
3878           {
3879                 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement, (Expression) $1, GetLocation ($2));
3880           }
3881         ;
3882
3883 post_decrement_expression
3884         : primary_expression OP_DEC
3885           {
3886                 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement, (Expression) $1, GetLocation ($2));
3887           }
3888         ;
3889         
3890 object_or_delegate_creation_expression
3891         : NEW new_expr_type open_parens_any opt_argument_list CLOSE_PARENS opt_object_or_collection_initializer
3892           {
3893                 if ($6 != null) {
3894                         if (lang_version <= LanguageVersion.ISO_2)
3895                                 FeatureIsNotAvailable (GetLocation ($1), "object initializers");
3896                                 
3897                         $$ = new NewInitialize ((FullNamedExpression) $2, (Arguments) $4, (CollectionOrObjectInitializers) $6, GetLocation ($1));
3898                 } else {
3899                         $$ = new New ((FullNamedExpression) $2, (Arguments) $4, GetLocation ($1));
3900                 }
3901                 
3902                 lbag.AddLocation ($$, GetLocation ($3), GetLocation ($5));
3903           }
3904         | NEW new_expr_type object_or_collection_initializer
3905           {
3906                 if (lang_version <= LanguageVersion.ISO_2)
3907                         FeatureIsNotAvailable (GetLocation ($1), "collection initializers");
3908           
3909                 $$ = new NewInitialize ((FullNamedExpression) $2, null, (CollectionOrObjectInitializers) $3, GetLocation ($1));
3910           }
3911         ;
3912
3913 array_creation_expression
3914         : NEW new_expr_type OPEN_BRACKET_EXPR expression_list CLOSE_BRACKET
3915           opt_rank_specifier
3916           opt_array_initializer
3917           {
3918                 $$ = new ArrayCreation ((FullNamedExpression) $2, (List<Expression>) $4,
3919                                 new ComposedTypeSpecifier (((List<Expression>) $4).Count, GetLocation ($3)) {
3920                                         Next = (ComposedTypeSpecifier) $6
3921                                 }, (ArrayInitializer) $7, GetLocation ($1));
3922                 lbag.AddLocation ($$, GetLocation ($3), GetLocation ($5));
3923           }
3924         | NEW new_expr_type rank_specifiers opt_array_initializer
3925           {
3926                 if ($4 == null)
3927                         report.Error (1586, GetLocation ($1), "Array creation must have array size or array initializer");
3928
3929                 $$ = new ArrayCreation ((FullNamedExpression) $2, (ComposedTypeSpecifier) $3, (ArrayInitializer) $4, GetLocation ($1));
3930           }
3931         | NEW rank_specifier array_initializer
3932           {
3933                 if (lang_version <= LanguageVersion.ISO_2)
3934                         FeatureIsNotAvailable (GetLocation ($1), "implicitly typed arrays");
3935           
3936                 $$ = new ImplicitlyTypedArrayCreation ((ComposedTypeSpecifier) $2, (ArrayInitializer) $3, GetLocation ($1));
3937           }
3938         | NEW new_expr_type OPEN_BRACKET CLOSE_BRACKET OPEN_BRACKET_EXPR error CLOSE_BRACKET
3939           {
3940                 report.Error (178, GetLocation ($6), "Invalid rank specifier, expecting `,' or `]'");
3941                 $$ = new ArrayCreation ((FullNamedExpression) $2, null, GetLocation ($1));
3942           }
3943         | NEW new_expr_type error
3944           {
3945                 Error_SyntaxError (yyToken);
3946                 // It can be any of new expression, create the most common one
3947                 $$ = new New ((FullNamedExpression) $2, null, GetLocation ($1));
3948           }
3949         ;
3950
3951 new_expr_type
3952         : {
3953                 ++lexer.parsing_type;
3954           }
3955           simple_type
3956           {
3957                 --lexer.parsing_type;
3958                 $$ = $2;
3959           }
3960         ;
3961
3962 anonymous_type_expression
3963         : NEW OPEN_BRACE anonymous_type_parameters_opt_comma CLOSE_BRACE
3964           {
3965                 if (lang_version <= LanguageVersion.ISO_2)
3966                         FeatureIsNotAvailable (GetLocation ($1), "anonymous types");
3967
3968                 $$ = new NewAnonymousType ((List<AnonymousTypeParameter>) $3, current_container, GetLocation ($1));
3969                 
3970                 // TODO: lbag comma location
3971                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
3972           }
3973         | NEW OPEN_BRACE GENERATE_COMPLETION
3974           {
3975                 $$ = new EmptyCompletion ();
3976           }
3977         ;
3978
3979 anonymous_type_parameters_opt_comma
3980         : anonymous_type_parameters_opt
3981         | anonymous_type_parameters COMMA
3982         ;
3983
3984 anonymous_type_parameters_opt
3985         : { $$ = null; }
3986         | anonymous_type_parameters
3987         ;
3988
3989 anonymous_type_parameters
3990         : anonymous_type_parameter
3991           {
3992                 var a = new List<AnonymousTypeParameter> (4);
3993                 a.Add ((AnonymousTypeParameter) $1);
3994                 $$ = a;
3995           }
3996         | anonymous_type_parameters COMMA anonymous_type_parameter
3997           {
3998                 var a = (List<AnonymousTypeParameter>) $1;
3999                 a.Add ((AnonymousTypeParameter) $3);
4000                 $$ = a;
4001           }
4002         | COMPLETE_COMPLETION
4003           {
4004                 $$ = new EmptyCompletion ();
4005           }
4006         | anonymous_type_parameter COMPLETE_COMPLETION
4007           {
4008                 $$ = $1;
4009           }
4010         ;
4011
4012 anonymous_type_parameter
4013         : identifier_inside_body ASSIGN variable_initializer
4014           {
4015                 var lt = (LocatedToken)$1;
4016                 $$ = new AnonymousTypeParameter ((Expression)$3, lt.Value, lt.Location);
4017                 lbag.AddLocation ($$, GetLocation ($2));
4018           }
4019         | identifier_inside_body
4020           {
4021                 var lt = (LocatedToken)$1;
4022                 $$ = new AnonymousTypeParameter (new SimpleName (lt.Value, lt.Location),
4023                         lt.Value, lt.Location);
4024           }
4025         | member_access
4026           {
4027                 MemberAccess ma = (MemberAccess) $1;
4028                 $$ = new AnonymousTypeParameter (ma, ma.Name, ma.Location);
4029           }
4030         | error
4031           {
4032                 report.Error (746, lexer.Location,
4033                         "Invalid anonymous type member declarator. Anonymous type members must be a member assignment, simple name or member access expression");
4034                 $$ = null;
4035           }
4036         ;
4037
4038 opt_rank_specifier
4039         : /* empty */
4040         | rank_specifiers
4041         ;
4042
4043 rank_specifiers
4044         : rank_specifier
4045         | rank_specifier rank_specifiers
4046           {
4047                 ((ComposedTypeSpecifier) $1).Next = (ComposedTypeSpecifier) $2;
4048                 $$ = $1;
4049           }
4050         ;
4051
4052 rank_specifier
4053         : OPEN_BRACKET CLOSE_BRACKET
4054           {
4055                 $$ = ComposedTypeSpecifier.CreateArrayDimension (1, GetLocation ($1));
4056                 lbag.AddLocation ($$, GetLocation ($2));
4057           }
4058         | OPEN_BRACKET dim_separators CLOSE_BRACKET
4059           {
4060                 $$ = ComposedTypeSpecifier.CreateArrayDimension ((int)$2, GetLocation ($1));
4061                 lbag.AddLocation ($$, GetLocation ($3));
4062           }
4063         ;
4064
4065 dim_separators
4066         : COMMA
4067           {
4068                 $$ = 2;
4069           }
4070         | dim_separators COMMA
4071           {
4072                 $$ = ((int) $1) + 1;
4073           }
4074         ;
4075
4076 opt_array_initializer
4077         : /* empty */
4078           {
4079                 $$ = null;
4080           }
4081         | array_initializer
4082           {
4083                 $$ = $1;
4084           }
4085         ;
4086
4087 array_initializer
4088         : OPEN_BRACE CLOSE_BRACE
4089           {
4090                 var ai = new ArrayInitializer (0, GetLocation ($1));
4091                 ai.VariableDeclaration = current_variable;
4092                 lbag.AddLocation (ai, GetLocation ($2));
4093                 $$ = ai;
4094           }
4095         | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
4096           {
4097                 var ai = new ArrayInitializer ((List<Expression>) $2, GetLocation ($1));
4098                 ai.VariableDeclaration = current_variable;
4099                 if ($3 != null) {
4100                         lbag.AddLocation (ai, GetLocation ($3), GetLocation ($4));
4101                 } else {
4102                         lbag.AddLocation (ai, GetLocation ($4));
4103                 }
4104                 $$ = ai;
4105           }
4106         ;
4107
4108 variable_initializer_list
4109         : variable_initializer
4110           {
4111                 var list = new List<Expression> (4);
4112                 list.Add ((Expression) $1);
4113                 $$ = list;
4114           }
4115         | variable_initializer_list COMMA variable_initializer
4116           {
4117                 var list = (List<Expression>) $1;
4118                 list.Add ((Expression) $3);
4119                 $$ = list;
4120           }
4121         ;
4122
4123 typeof_expression
4124         : TYPEOF open_parens_any typeof_type_expression CLOSE_PARENS
4125           {
4126                 $$ = new TypeOf ((FullNamedExpression) $3, GetLocation ($1));
4127                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
4128           }
4129         ;
4130         
4131 typeof_type_expression
4132         : type_and_void
4133         | error
4134          {
4135                 Error_TypeExpected (lexer.Location);
4136                 $$ = null;
4137          }
4138         ;
4139
4140 generic_dimension
4141         : GENERIC_DIMENSION
4142           {
4143                 if (lang_version < LanguageVersion.ISO_2)
4144                         FeatureIsNotAvailable (GetLocation ($1), "generics");
4145
4146                 $$ = $1;
4147           }
4148         ;
4149         
4150 qualified_alias_member
4151         : IDENTIFIER DOUBLE_COLON
4152           {
4153                 var lt = (LocatedToken) $1;
4154                 if (lang_version == LanguageVersion.ISO_1)
4155                         FeatureIsNotAvailable (lt.Location, "namespace alias qualifier");
4156
4157                 $$ = lt;                
4158           }
4159         ;
4160
4161 sizeof_expression
4162         : SIZEOF open_parens_any type CLOSE_PARENS
4163           { 
4164                 $$ = new SizeOf ((Expression) $3, GetLocation ($1));
4165                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
4166           }
4167         | SIZEOF open_parens_any type error
4168           {
4169                 Error_SyntaxError (yyToken);
4170
4171                 $$ = new SizeOf ((Expression) $3, GetLocation ($1));
4172                 lbag.AddLocation ($$, GetLocation ($2));
4173           }
4174         ;
4175
4176 checked_expression
4177         : CHECKED open_parens_any expression CLOSE_PARENS
4178           {
4179                 $$ = new CheckedExpr ((Expression) $3, GetLocation ($1));
4180                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
4181           }
4182         | CHECKED error
4183           {
4184                 Error_SyntaxError (yyToken);
4185
4186                 $$ = new CheckedExpr (null, GetLocation ($1));
4187           }
4188         ;
4189
4190 unchecked_expression
4191         : UNCHECKED open_parens_any expression CLOSE_PARENS
4192           {
4193                 $$ = new UnCheckedExpr ((Expression) $3, GetLocation ($1));
4194                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
4195           }
4196         | UNCHECKED error
4197           {
4198                 Error_SyntaxError (yyToken);
4199
4200                 $$ = new UnCheckedExpr (null, GetLocation ($1));
4201           }
4202         ;
4203
4204 pointer_member_access
4205         : primary_expression OP_PTR IDENTIFIER opt_type_argument_list
4206           {
4207                 var lt = (LocatedToken) $3;
4208                 $$ = new MemberAccess (new Indirection ((Expression) $1, GetLocation ($2)), lt.Value, (TypeArguments) $4, lt.Location);
4209           }
4210         ;
4211
4212 anonymous_method_expression
4213         : DELEGATE opt_anonymous_method_signature
4214           {
4215                 start_anonymous (false, (ParametersCompiled) $2, false, GetLocation ($1));
4216           }
4217           block
4218           {
4219                 $$ = end_anonymous ((ParametersBlock) $4);
4220           }
4221         | ASYNC DELEGATE opt_anonymous_method_signature
4222           {
4223                 start_anonymous (false, (ParametersCompiled) $3, true, GetLocation ($1));
4224           }
4225           block
4226           {
4227                 $$ = end_anonymous ((ParametersBlock) $5);
4228           }
4229         ;
4230
4231 opt_anonymous_method_signature
4232         : 
4233           {
4234                 $$ = ParametersCompiled.Undefined;
4235           } 
4236         | anonymous_method_signature
4237         ;
4238
4239 anonymous_method_signature
4240         : OPEN_PARENS
4241           {
4242                 valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out;
4243           }
4244           opt_formal_parameter_list CLOSE_PARENS
4245           {
4246                 valid_param_mod = 0;
4247                 $$ = $3;
4248           }
4249         ;
4250
4251 default_value_expression
4252         : DEFAULT open_parens_any type CLOSE_PARENS
4253           {
4254                 if (lang_version < LanguageVersion.ISO_2)
4255                         FeatureIsNotAvailable (GetLocation ($1), "default value expression");
4256
4257                 $$ = new DefaultValueExpression ((Expression) $3, GetLocation ($1));
4258                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
4259           }
4260         ;
4261
4262 unary_expression
4263         : primary_expression
4264         | BANG prefixed_unary_expression
4265           {
4266                 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, GetLocation ($1));
4267           }
4268         | TILDE prefixed_unary_expression
4269           {
4270                 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, GetLocation ($1));
4271           }
4272         | OPEN_PARENS_CAST type CLOSE_PARENS prefixed_unary_expression
4273           {
4274                 $$ = new Cast ((FullNamedExpression) $2, (Expression) $4, GetLocation ($1));
4275                 lbag.AddLocation ($$, GetLocation ($3));
4276           }
4277         | AWAIT prefixed_unary_expression
4278           {
4279                 if (!async_block) {
4280                          if (current_anonymous_method is LambdaExpression) {
4281                                 report.Error (4034, GetLocation ($1),
4282                                         "The `await' operator can only be used when its containing lambda expression is marked with the `async' modifier");
4283                         } else if (current_anonymous_method != null) {
4284                                 report.Error (4035, GetLocation ($1),
4285                                         "The `await' operator can only be used when its containing anonymous method is marked with the `async' modifier");
4286                         } else if (interactive_async != null) {
4287                                 current_block.Explicit.RegisterAsyncAwait ();
4288                                 interactive_async = true;
4289                         } else {
4290                                 report.Error (4033, GetLocation ($1),
4291                                         "The `await' operator can only be used when its containing method is marked with the `async' modifier");
4292                         }
4293                 } else {
4294                         current_block.Explicit.RegisterAsyncAwait ();
4295                 }
4296                 
4297                 $$ = new Await ((Expression) $2, GetLocation ($1));
4298           }
4299         | BANG error
4300           {
4301                 Error_SyntaxError (yyToken);
4302
4303                 $$ = new Unary (Unary.Operator.LogicalNot, null, GetLocation ($1));
4304           }
4305         | TILDE error
4306           {
4307                 Error_SyntaxError (yyToken);
4308
4309                 $$ = new Unary (Unary.Operator.OnesComplement, null, GetLocation ($1));
4310           }
4311         | OPEN_PARENS_CAST type CLOSE_PARENS error
4312           {
4313                 Error_SyntaxError (yyToken);
4314
4315                 $$ = new Cast ((FullNamedExpression) $2, null, GetLocation ($1));
4316                 lbag.AddLocation ($$, GetLocation ($3));
4317           }
4318         | AWAIT error
4319           {
4320                 Error_SyntaxError (yyToken);
4321
4322                 $$ = new Await (null, GetLocation ($1));
4323           }
4324         ;
4325
4326         //
4327         // The idea to split this out is from Rhys' grammar
4328         // to solve the problem with casts.
4329         //
4330 prefixed_unary_expression
4331         : unary_expression
4332         | PLUS prefixed_unary_expression
4333           { 
4334                 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, GetLocation ($1));
4335           } 
4336         | MINUS prefixed_unary_expression 
4337           { 
4338                 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, GetLocation ($1));
4339           }
4340         | OP_INC prefixed_unary_expression 
4341           {
4342                 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement, (Expression) $2, GetLocation ($1));
4343           }
4344         | OP_DEC prefixed_unary_expression 
4345           {
4346                 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement, (Expression) $2, GetLocation ($1));
4347           }
4348         | STAR prefixed_unary_expression
4349           {
4350                 $$ = new Indirection ((Expression) $2, GetLocation ($1));
4351           }
4352         | BITWISE_AND prefixed_unary_expression
4353           {
4354                 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, GetLocation ($1));
4355           }
4356         | PLUS error
4357           { 
4358                 Error_SyntaxError (yyToken);
4359
4360                 $$ = new Unary (Unary.Operator.UnaryPlus, null, GetLocation ($1));
4361           } 
4362         | MINUS error 
4363           { 
4364                 Error_SyntaxError (yyToken);
4365
4366                 $$ = new Unary (Unary.Operator.UnaryNegation, null, GetLocation ($1));
4367           }
4368         | OP_INC error 
4369           {
4370                 Error_SyntaxError (yyToken);
4371
4372                 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement, null, GetLocation ($1));
4373           }
4374         | OP_DEC error 
4375           {
4376                 Error_SyntaxError (yyToken);
4377
4378                 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement, null, GetLocation ($1));
4379           }
4380         | STAR error
4381           {
4382                 Error_SyntaxError (yyToken);
4383
4384                 $$ = new Indirection (null, GetLocation ($1));
4385           }
4386         | BITWISE_AND error
4387           {
4388                 Error_SyntaxError (yyToken);
4389
4390                 $$ = new Unary (Unary.Operator.AddressOf, null, GetLocation ($1));
4391           }
4392         ;
4393
4394 multiplicative_expression
4395         : prefixed_unary_expression
4396         | multiplicative_expression STAR prefixed_unary_expression
4397           {
4398                 $$ = new Binary (Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
4399                 lbag.AddLocation ($$, GetLocation ($2));
4400           }
4401         | multiplicative_expression DIV prefixed_unary_expression
4402           {
4403                 $$ = new Binary (Binary.Operator.Division, (Expression) $1, (Expression) $3);
4404                 lbag.AddLocation ($$, GetLocation ($2));
4405           }
4406         | multiplicative_expression PERCENT prefixed_unary_expression 
4407           {
4408                 $$ = new Binary (Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
4409                 lbag.AddLocation ($$, GetLocation ($2));
4410           }
4411         | multiplicative_expression STAR error
4412           {
4413                 Error_SyntaxError (yyToken);
4414
4415                 $$ = new Binary (Binary.Operator.Multiply, (Expression) $1, null);
4416                 lbag.AddLocation ($$, GetLocation ($2));
4417           }
4418         | multiplicative_expression DIV error
4419           {
4420                 Error_SyntaxError (yyToken);
4421
4422                 $$ = new Binary (Binary.Operator.Division, (Expression) $1, null);
4423                 lbag.AddLocation ($$, GetLocation ($2));
4424           }
4425         | multiplicative_expression PERCENT error 
4426           {
4427                 Error_SyntaxError (yyToken);
4428
4429                 $$ = new Binary (Binary.Operator.Modulus, (Expression) $1, null);
4430                 lbag.AddLocation ($$, GetLocation ($2));
4431           }
4432         ;
4433
4434 additive_expression
4435         : multiplicative_expression
4436         | additive_expression PLUS multiplicative_expression 
4437           {
4438                 $$ = new Binary (Binary.Operator.Addition, (Expression) $1, (Expression) $3);
4439                 lbag.AddLocation ($$, GetLocation ($2));
4440           }
4441         | additive_expression MINUS multiplicative_expression
4442           {
4443                 $$ = new Binary (Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
4444                 lbag.AddLocation ($$, GetLocation ($2));
4445           }
4446         | additive_expression PLUS error
4447           {
4448                 Error_SyntaxError (yyToken);
4449
4450                 $$ = new Binary (Binary.Operator.Addition, (Expression) $1, null);
4451                 lbag.AddLocation ($$, GetLocation ($2));
4452           }
4453         | additive_expression MINUS error
4454           {
4455                 Error_SyntaxError (yyToken);
4456
4457                 $$ = new Binary (Binary.Operator.Subtraction, (Expression) $1, null);
4458                 lbag.AddLocation ($$, GetLocation ($2));
4459           }
4460         | additive_expression AS type
4461           {
4462                 $$ = new As ((Expression) $1, (Expression) $3, GetLocation ($2));
4463           }
4464         | additive_expression IS pattern_type_expr opt_identifier
4465           {
4466                 var is_expr = new Is ((Expression) $1, (Expression) $3, GetLocation ($2));
4467                 if ($4 != null) {
4468                         if (lang_version != LanguageVersion.Experimental)
4469                                 FeatureIsNotAvailable (GetLocation ($4), "type pattern matching");
4470
4471                         var lt = (LocatedToken) $4;
4472                         is_expr.Variable = new LocalVariable (current_block, lt.Value, lt.Location);
4473                         current_block.AddLocalName (is_expr.Variable);
4474                 }
4475
4476                 $$ = is_expr;
4477           }
4478         | additive_expression IS pattern_expr
4479           {
4480                 var is_expr = new Is ((Expression) $1, (Expression) $3, GetLocation ($2));
4481                 if (lang_version != LanguageVersion.Experimental)
4482                         FeatureIsNotAvailable (GetLocation ($2), "pattern matching");
4483
4484                 $$ = is_expr;
4485           }
4486         | additive_expression AS error
4487           {
4488                 Error_SyntaxError (yyToken);
4489
4490                 $$ = new As ((Expression) $1, null, GetLocation ($2));
4491           }
4492         | additive_expression IS error
4493           {
4494                 Error_SyntaxError (yyToken);
4495
4496                 $$ = new Is ((Expression) $1, null, GetLocation ($2));
4497           }
4498         | AWAIT IS type
4499           {
4500                 var lt = (LocatedToken) $1;
4501                 $$ = new Is (new SimpleName (lt.Value, lt.Location), (Expression) $3, GetLocation ($2));
4502           }
4503         | AWAIT AS type
4504           {
4505                 var lt = (LocatedToken) $1;
4506                 $$ = new As (new SimpleName (lt.Value, lt.Location), (Expression) $3, GetLocation ($2));
4507           }
4508         ;
4509
4510 pattern_type_expr
4511         : variable_type
4512         ;
4513
4514 pattern_expr
4515         : literal
4516         | PLUS prefixed_unary_expression
4517           {
4518                 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, GetLocation ($1));
4519           }
4520         | MINUS prefixed_unary_expression
4521           {
4522                 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, GetLocation ($1));
4523           }
4524         | sizeof_expression
4525         | default_value_expression
4526         | OPEN_PARENS_CAST type CLOSE_PARENS prefixed_unary_expression
4527           {
4528                 $$ = new Cast ((FullNamedExpression) $2, (Expression) $4, GetLocation ($1));
4529                 lbag.AddLocation ($$, GetLocation ($3));
4530           }
4531         | STAR
4532           {
4533                 $$ = new WildcardPattern (GetLocation ($1));
4534           }
4535         | pattern_expr_invocation
4536         | pattern_property
4537         ;
4538
4539 pattern_expr_invocation
4540         : type_name_expression OPEN_PARENS opt_pattern_list CLOSE_PARENS
4541           {
4542                 $$ = new RecursivePattern ((ATypeNameExpression) $1, (Arguments) $3, GetLocation ($2));
4543           }
4544         ;
4545
4546 pattern_property
4547         : type_name_expression OPEN_BRACE pattern_property_list CLOSE_BRACE
4548           {
4549                 $$ = new PropertyPattern ((ATypeNameExpression) $1, (List<PropertyPatternMember>) $3, GetLocation ($2));
4550           }
4551         ;
4552
4553 pattern_property_list
4554         : pattern_property_entry
4555           {
4556                 var list = new List<PropertyPatternMember> ();
4557                 list.Add ((PropertyPatternMember) $1);
4558                 $$ = list;
4559           }
4560         | pattern_property_list COMMA pattern_property_entry
4561           {
4562                 var list = (List<PropertyPatternMember>) $1;
4563                 list.Add ((PropertyPatternMember) $3);
4564                 $$ = list;
4565           }
4566         ;
4567
4568 pattern_property_entry
4569         : identifier_inside_body IS pattern
4570           {
4571                 var lt = (LocatedToken) $1;
4572                 $$ = new PropertyPatternMember (lt.Value, (Expression) $3, lt.Location);
4573           }
4574         ;
4575
4576 pattern
4577         : pattern_expr
4578         | pattern_type_expr opt_identifier
4579           {
4580                 if ($2 != null) {
4581                         var lt = (LocatedToken) $2;
4582                         var variable = new LocalVariable (current_block, lt.Value, lt.Location);
4583                         current_block.AddLocalName (variable);
4584                 }
4585           }
4586         ;
4587
4588 opt_pattern_list
4589         : /* empty */
4590           {
4591                 $$ = new Arguments (0);
4592           }
4593         | pattern_list
4594         ;
4595
4596 pattern_list
4597         : pattern_argument
4598           {
4599                 Arguments args = new Arguments (4);
4600                 args.Add ((Argument) $1);
4601                 $$ = args;
4602           }
4603         | pattern_list COMMA pattern_argument
4604           {
4605                 Arguments args = (Arguments) $1;
4606                 if (args [args.Count - 1] is NamedArgument && !($3 is NamedArgument))
4607                         Error_NamedArgumentExpected ((NamedArgument) args [args.Count - 1]);
4608
4609                 args.Add ((Argument) $3);
4610                 $$ = args;
4611           }
4612         ;
4613
4614 pattern_argument
4615         : pattern
4616           {
4617                 $$ = new Argument ((Expression) $1);
4618           }
4619         | IDENTIFIER COLON pattern
4620           {
4621                 var lt = (LocatedToken) $1;
4622                 $$ = new NamedArgument (lt.Value, lt.Location, (Expression) $3);
4623           }
4624         ;
4625
4626 shift_expression
4627         : additive_expression
4628         | shift_expression OP_SHIFT_LEFT additive_expression
4629           {
4630                 $$ = new Binary (Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
4631                 lbag.AddLocation ($$, GetLocation ($2));
4632           }
4633         | shift_expression OP_SHIFT_RIGHT additive_expression
4634           {
4635                 $$ = new Binary (Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
4636                 lbag.AddLocation ($$, GetLocation ($2));
4637           }
4638         | shift_expression OP_SHIFT_LEFT error
4639           {
4640                 Error_SyntaxError (yyToken);
4641
4642                 $$ = new Binary (Binary.Operator.LeftShift, (Expression) $1, null);
4643                 lbag.AddLocation ($$, GetLocation ($2));
4644           }
4645         | shift_expression OP_SHIFT_RIGHT error
4646           {
4647                 Error_SyntaxError (yyToken);
4648
4649                 $$ = new Binary (Binary.Operator.RightShift, (Expression) $1, null);
4650                 lbag.AddLocation ($$, GetLocation ($2));
4651           }
4652         ; 
4653
4654 relational_expression
4655         : shift_expression
4656         | relational_expression OP_LT shift_expression
4657           {
4658                 $$ = new Binary (Binary.Operator.LessThan, (Expression) $1, (Expression) $3);
4659                 lbag.AddLocation ($$, GetLocation ($2));
4660           }
4661         | relational_expression OP_GT shift_expression
4662           {
4663                 $$ = new Binary (Binary.Operator.GreaterThan, (Expression) $1, (Expression) $3);
4664                 lbag.AddLocation ($$, GetLocation ($2));
4665           }
4666         | relational_expression OP_LE shift_expression
4667           {
4668                 $$ = new Binary (Binary.Operator.LessThanOrEqual, (Expression) $1, (Expression) $3);
4669                 lbag.AddLocation ($$, GetLocation ($2));
4670           }
4671         | relational_expression OP_GE shift_expression
4672           {
4673                 $$ = new Binary (Binary.Operator.GreaterThanOrEqual, (Expression) $1, (Expression) $3);
4674                 lbag.AddLocation ($$, GetLocation ($2));
4675           }
4676         | relational_expression OP_LT error
4677           {
4678                 Error_SyntaxError (yyToken);
4679
4680                 $$ = new Binary (Binary.Operator.LessThan, (Expression) $1, null);
4681                 lbag.AddLocation ($$, GetLocation ($2));
4682           }
4683         | relational_expression OP_GT error
4684           {
4685                 Error_SyntaxError (yyToken);
4686
4687                 $$ = new Binary (Binary.Operator.GreaterThan, (Expression) $1, null);
4688                 lbag.AddLocation ($$, GetLocation ($2));
4689           }
4690         | relational_expression OP_LE error
4691           {
4692                 Error_SyntaxError (yyToken);
4693
4694                 $$ = new Binary (Binary.Operator.LessThanOrEqual, (Expression) $1, null);
4695                 lbag.AddLocation ($$, GetLocation ($2));
4696           }
4697         | relational_expression OP_GE error
4698           {
4699                 Error_SyntaxError (yyToken);
4700
4701                 $$ = new Binary (Binary.Operator.GreaterThanOrEqual, (Expression) $1, null);
4702                 lbag.AddLocation ($$, GetLocation ($2));
4703           }
4704         ;
4705
4706 equality_expression
4707         : relational_expression
4708         | equality_expression OP_EQ relational_expression
4709           {
4710                 $$ = new Binary (Binary.Operator.Equality, (Expression) $1, (Expression) $3);
4711                 lbag.AddLocation ($$, GetLocation ($2));
4712           }
4713         | equality_expression OP_NE relational_expression
4714           {
4715                 $$ = new Binary (Binary.Operator.Inequality, (Expression) $1, (Expression) $3);
4716                 lbag.AddLocation ($$, GetLocation ($2));
4717           }
4718         | equality_expression OP_EQ error
4719           {
4720                 Error_SyntaxError (yyToken);
4721
4722                 $$ = new Binary (Binary.Operator.Equality, (Expression) $1, null);
4723                 lbag.AddLocation ($$, GetLocation ($2));
4724           }
4725         | equality_expression OP_NE error
4726           {
4727                 Error_SyntaxError (yyToken);
4728
4729                 $$ = new Binary (Binary.Operator.Inequality, (Expression) $1, null);
4730                 lbag.AddLocation ($$, GetLocation ($2));
4731           }
4732         ; 
4733
4734 and_expression
4735         : equality_expression
4736         | and_expression BITWISE_AND equality_expression
4737           {
4738                 $$ = new Binary (Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
4739                 lbag.AddLocation ($$, GetLocation ($2));
4740           }
4741         | and_expression BITWISE_AND error
4742           {
4743                 Error_SyntaxError (yyToken);
4744
4745                 $$ = new Binary (Binary.Operator.BitwiseAnd, (Expression) $1, null);
4746                 lbag.AddLocation ($$, GetLocation ($2));
4747           }
4748         ;
4749
4750 exclusive_or_expression
4751         : and_expression
4752         | exclusive_or_expression CARRET and_expression
4753           {
4754                 $$ = new Binary (Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
4755                 lbag.AddLocation ($$, GetLocation ($2));
4756           }
4757         | exclusive_or_expression CARRET error
4758           {
4759                 Error_SyntaxError (yyToken);
4760
4761                 $$ = new Binary (Binary.Operator.ExclusiveOr, (Expression) $1, null);
4762                 lbag.AddLocation ($$, GetLocation ($2));
4763           }
4764         ;
4765
4766 inclusive_or_expression
4767         : exclusive_or_expression
4768         | inclusive_or_expression BITWISE_OR exclusive_or_expression
4769           {
4770                 $$ = new Binary (Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
4771                 lbag.AddLocation ($$, GetLocation ($2));
4772           }
4773         | inclusive_or_expression BITWISE_OR error
4774           {
4775                 Error_SyntaxError (yyToken);
4776
4777                 $$ = new Binary (Binary.Operator.BitwiseOr, (Expression) $1, null);
4778                 lbag.AddLocation ($$, GetLocation ($2));
4779           }
4780         ;
4781
4782 conditional_and_expression
4783         : inclusive_or_expression
4784         | conditional_and_expression OP_AND inclusive_or_expression
4785           {
4786                 $$ = new Binary (Binary.Operator.LogicalAnd, (Expression) $1, (Expression) $3);
4787                 lbag.AddLocation ($$, GetLocation ($2));
4788           }
4789         | conditional_and_expression OP_AND error
4790           {
4791                 Error_SyntaxError (yyToken);
4792
4793                 $$ = new Binary (Binary.Operator.LogicalAnd, (Expression) $1, null);
4794                 lbag.AddLocation ($$, GetLocation ($2));
4795           }
4796         ;
4797
4798 conditional_or_expression
4799         : conditional_and_expression
4800         | conditional_or_expression OP_OR conditional_and_expression
4801           {
4802                 $$ = new Binary (Binary.Operator.LogicalOr, (Expression) $1, (Expression) $3);
4803                 lbag.AddLocation ($$, GetLocation ($2));
4804           }
4805         | conditional_or_expression OP_OR error
4806           {
4807                 Error_SyntaxError (yyToken);
4808
4809                 $$ = new Binary (Binary.Operator.LogicalOr, (Expression) $1, null);
4810                 lbag.AddLocation ($$, GetLocation ($2));
4811           }
4812         ;
4813         
4814 null_coalescing_expression
4815         : conditional_or_expression
4816         | conditional_or_expression OP_COALESCING null_coalescing_expression
4817           {
4818                 if (lang_version < LanguageVersion.ISO_2)
4819                         FeatureIsNotAvailable (GetLocation ($2), "null coalescing operator");
4820                         
4821                 $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $3);
4822                 lbag.AddLocation ($$, GetLocation ($2));
4823           }
4824         ;
4825
4826 conditional_expression
4827         : null_coalescing_expression
4828         | null_coalescing_expression INTERR expression COLON expression
4829           {
4830                 $$ = new Conditional (new BooleanExpression ((Expression) $1), (Expression) $3, (Expression) $5, GetLocation ($2));
4831                 lbag.AddLocation ($$, GetLocation ($4));
4832           }
4833         | null_coalescing_expression INTERR expression error
4834           {
4835                 Error_SyntaxError (yyToken);
4836
4837                 $$ = new Conditional (new BooleanExpression ((Expression) $1), (Expression) $3, null, GetLocation ($2));
4838           }
4839         | null_coalescing_expression INTERR expression COLON error
4840           {
4841                 Error_SyntaxError (yyToken);
4842
4843                 $$ = new Conditional (new BooleanExpression ((Expression) $1), (Expression) $3, null, GetLocation ($2));
4844                 lbag.AddLocation ($$, GetLocation ($4));
4845           }
4846         | null_coalescing_expression INTERR expression COLON CLOSE_BRACE
4847           {
4848                 Error_SyntaxError (Token.CLOSE_BRACE);
4849
4850                 $$ = new Conditional (new BooleanExpression ((Expression) $1), (Expression) $3, null, GetLocation ($2));
4851                 lbag.AddLocation ($$, GetLocation ($4));
4852                 lexer.putback ('}');
4853           }
4854         ;
4855
4856 assignment_expression
4857         : prefixed_unary_expression ASSIGN expression
4858           {
4859                 $$ = new SimpleAssign ((Expression) $1, (Expression) $3);
4860                 lbag.AddLocation ($$, GetLocation ($2));
4861           }
4862         | prefixed_unary_expression OP_MULT_ASSIGN expression
4863           {
4864                 $$ = new CompoundAssign (Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
4865                 lbag.AddLocation ($$, GetLocation ($2));
4866           }
4867         | prefixed_unary_expression OP_DIV_ASSIGN expression
4868           {
4869                 $$ = new CompoundAssign (Binary.Operator.Division, (Expression) $1, (Expression) $3);
4870                 lbag.AddLocation ($$, GetLocation ($2));
4871           }
4872         | prefixed_unary_expression OP_MOD_ASSIGN expression
4873           {
4874                 $$ = new CompoundAssign (Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
4875                 lbag.AddLocation ($$, GetLocation ($2));
4876           }
4877         | prefixed_unary_expression OP_ADD_ASSIGN expression
4878           {
4879                 $$ = new CompoundAssign (Binary.Operator.Addition, (Expression) $1, (Expression) $3);
4880                 lbag.AddLocation ($$, GetLocation ($2));
4881           }
4882         | prefixed_unary_expression OP_SUB_ASSIGN expression
4883           {
4884                 $$ = new CompoundAssign (Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
4885                 lbag.AddLocation ($$, GetLocation ($2));
4886           }
4887         | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
4888           {
4889                 $$ = new CompoundAssign (Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
4890                 lbag.AddLocation ($$, GetLocation ($2));
4891           }
4892         | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
4893           {
4894                 $$ = new CompoundAssign (Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
4895                 lbag.AddLocation ($$, GetLocation ($2));
4896           }
4897         | prefixed_unary_expression OP_AND_ASSIGN expression
4898           {
4899                 $$ = new CompoundAssign (Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
4900                 lbag.AddLocation ($$, GetLocation ($2));
4901           }
4902         | prefixed_unary_expression OP_OR_ASSIGN expression
4903           {
4904                 $$ = new CompoundAssign (Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
4905                 lbag.AddLocation ($$, GetLocation ($2));
4906           }
4907         | prefixed_unary_expression OP_XOR_ASSIGN expression
4908           {
4909                 $$ = new CompoundAssign (Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
4910                 lbag.AddLocation ($$, GetLocation ($2));
4911           }
4912         ;
4913
4914 lambda_parameter_list
4915         : lambda_parameter
4916           {
4917                 var pars = new List<Parameter> (4);
4918                 pars.Add ((Parameter) $1);
4919
4920                 $$ = pars;
4921           }
4922         | lambda_parameter_list COMMA lambda_parameter
4923           {
4924                 var pars = (List<Parameter>) $1;
4925                 Parameter p = (Parameter)$3;
4926                 if (pars[0].GetType () != p.GetType ()) {
4927                         report.Error (748, p.Location, "All lambda parameters must be typed either explicitly or implicitly");
4928                 }
4929                 
4930                 pars.Add (p);
4931                 $$ = pars;
4932           }
4933         ;
4934
4935 lambda_parameter
4936         : parameter_modifier parameter_type identifier_inside_body
4937           {
4938                 var lt = (LocatedToken) $3;
4939
4940                 $$ = new Parameter ((FullNamedExpression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
4941           }
4942         | parameter_type identifier_inside_body
4943           {
4944                 var lt = (LocatedToken) $2;
4945
4946                 $$ = new Parameter ((FullNamedExpression) $1, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
4947           }
4948         | IDENTIFIER
4949           {
4950                 var lt = (LocatedToken) $1;
4951                 $$ = new ImplicitLambdaParameter (lt.Value, lt.Location);
4952           }
4953         | AWAIT
4954           {
4955                 var lt = (LocatedToken) Error_AwaitAsIdentifier ($1);
4956                 $$ = new ImplicitLambdaParameter (lt.Value, lt.Location);
4957           }
4958         ;
4959
4960 opt_lambda_parameter_list
4961         : /* empty */                   { $$ = ParametersCompiled.EmptyReadOnlyParameters; }
4962         | lambda_parameter_list         { 
4963                 var pars_list = (List<Parameter>) $1;
4964                 $$ = new ParametersCompiled (pars_list.ToArray ());
4965           }
4966         ;
4967
4968 lambda_expression_body
4969         : {
4970                 start_block (Location.Null);
4971           }
4972           expression    // All expressions must handle error or current block won't be restored and breaking ast completely
4973           {
4974                 Block b = end_block (Location.Null);
4975                 b.IsCompilerGenerated = true;
4976                 b.AddStatement (new ContextualReturn ((Expression) $2));
4977                 $$ = b;
4978           } 
4979         | block
4980         | error
4981           {
4982                 // Handles only cases like foo = x.FirstOrDefault (l => );
4983                 // where we must restore current_variable
4984                 Block b = end_block (Location.Null);
4985                 b.IsCompilerGenerated = true;
4986
4987                 Error_SyntaxError (yyToken);
4988                 $$ = null;
4989           }
4990         ;
4991
4992 expression_or_error
4993         : expression
4994         | error
4995           {
4996                 Error_SyntaxError (yyToken);
4997                 $$ = null;
4998           }
4999         ;
5000         
5001 lambda_expression
5002         : IDENTIFIER ARROW 
5003           {
5004                 var lt = (LocatedToken) $1;     
5005                 Parameter p = new ImplicitLambdaParameter (lt.Value, lt.Location);
5006                 start_anonymous (true, new ParametersCompiled (p), false, lt.Location);
5007           }
5008           lambda_expression_body
5009           {
5010                 $$ = end_anonymous ((ParametersBlock) $4);
5011                 lbag.AddLocation ($$, GetLocation ($2));
5012           }
5013         | AWAIT ARROW
5014           {
5015                 var lt = (LocatedToken) Error_AwaitAsIdentifier ($1);
5016                 Parameter p = new ImplicitLambdaParameter (lt.Value, lt.Location);
5017                 start_anonymous (true, new ParametersCompiled (p), false, lt.Location);
5018           }
5019           lambda_expression_body
5020           {
5021                 $$ = end_anonymous ((ParametersBlock) $4);
5022                 lbag.AddLocation ($$, GetLocation ($2));
5023           }
5024         | ASYNC identifier_inside_body ARROW
5025           {
5026                 var lt = (LocatedToken) $2;
5027                 Parameter p = new ImplicitLambdaParameter (lt.Value, lt.Location);
5028                 start_anonymous (true, new ParametersCompiled (p), true, lt.Location);
5029           }
5030           lambda_expression_body
5031           {
5032                 $$ = end_anonymous ((ParametersBlock) $5);
5033                 lbag.AddLocation ($$, GetLocation ($1), GetLocation ($3));
5034           }
5035         | OPEN_PARENS_LAMBDA
5036           {
5037                 valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out;
5038           }
5039           opt_lambda_parameter_list CLOSE_PARENS ARROW 
5040           {
5041                 valid_param_mod = 0;
5042                 start_anonymous (true, (ParametersCompiled) $3, false, GetLocation ($1));
5043           }
5044           lambda_expression_body
5045           {
5046                 $$ = end_anonymous ((ParametersBlock) $7);
5047                 lbag.AddLocation ($$, GetLocation ($1), GetLocation ($4), GetLocation ($5));
5048           }
5049         | ASYNC OPEN_PARENS_LAMBDA
5050           {
5051                 valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out;          
5052           }
5053           opt_lambda_parameter_list CLOSE_PARENS ARROW 
5054           {
5055                 valid_param_mod = 0;
5056                 start_anonymous (true, (ParametersCompiled) $4, true, GetLocation ($1));
5057           }
5058           lambda_expression_body
5059           {
5060                 $$ = end_anonymous ((ParametersBlock) $8);
5061                 lbag.AddLocation ($$, GetLocation ($1), GetLocation ($2), GetLocation ($5), GetLocation ($6));
5062           }
5063         ;
5064
5065 expression
5066         : assignment_expression 
5067         | non_assignment_expression
5068         ;
5069         
5070 non_assignment_expression
5071         : conditional_expression
5072         | lambda_expression
5073         | query_expression
5074         | ARGLIST
5075           {
5076                 $$ = new ArglistAccess (GetLocation ($1));
5077           }
5078         ;
5079         
5080 undocumented_expressions
5081         : REFVALUE OPEN_PARENS non_assignment_expression COMMA type CLOSE_PARENS
5082           {
5083                 $$ = new RefValueExpr ((Expression) $3, (FullNamedExpression) $5, GetLocation ($1));
5084                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4), GetLocation ($6));
5085           }
5086         | REFTYPE open_parens_any expression CLOSE_PARENS
5087           {
5088                 $$ = new RefTypeExpr ((Expression) $3, GetLocation ($1));
5089                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
5090           }
5091         | MAKEREF open_parens_any expression CLOSE_PARENS
5092           {
5093                 $$ = new MakeRefExpr ((Expression) $3, GetLocation ($1));
5094                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));        
5095           }
5096         ;
5097
5098 constant_expression
5099         : expression
5100         ;
5101
5102 boolean_expression
5103         : expression
5104           {
5105                 $$ = new BooleanExpression ((Expression) $1);
5106           }
5107         ;
5108
5109 opt_primary_parameters
5110         : /* empty */
5111           {
5112                 $$ = null;
5113           }
5114         | primary_parameters
5115         ;
5116
5117 primary_parameters
5118         : OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
5119           {
5120                 $$ = $2;
5121
5122                 // Cannot use opt_formal_parameter_list because it can be shared instance for empty parameters
5123                 lbag.AppendToMember (current_container, GetLocation ($1), GetLocation ($3));
5124
5125                 if (lang_version != LanguageVersion.Experimental)
5126                         FeatureIsNotAvailable (GetLocation ($1), "primary constructor");
5127           }
5128         ;
5129
5130 opt_primary_parameters_with_class_base
5131         : /* empty */
5132           {
5133                 $$ = null;
5134           }
5135         | class_base
5136           {
5137                 $$ = null;
5138           }
5139         | primary_parameters
5140           {
5141                 $$ = $1;
5142           }
5143         | primary_parameters class_base
5144           {
5145                 $$ = $1;
5146           }
5147         | primary_parameters class_base OPEN_PARENS
5148           {
5149                 ++lexer.parsing_block;
5150                 current_type.PrimaryConstructorBaseArgumentsStart = GetLocation ($3);
5151           }
5152           opt_argument_list CLOSE_PARENS
5153           {
5154                 lbag.AppendToMember (current_container, GetLocation ($6));
5155                 current_type.PrimaryConstructorBaseArguments = (Arguments) $5;
5156                 --lexer.parsing_block;
5157
5158                 $$ = $1;
5159           }
5160         ;
5161
5162 //
5163 // 10 classes
5164 //
5165 class_declaration
5166         : opt_attributes
5167           opt_modifiers
5168           opt_partial
5169           CLASS
5170           {
5171           }
5172           type_declaration_name
5173           {
5174                 lexer.ConstraintsParsing = true;
5175
5176                 Class c = new Class (current_container, (MemberName) $6, (Modifiers) $2, (Attributes) $1);
5177                 if (((c.ModFlags & Modifiers.STATIC) != 0) && lang_version == LanguageVersion.ISO_1) {
5178                         FeatureIsNotAvailable (c.Location, "static classes");
5179                 }
5180                         
5181                 push_current_container (c, $3);
5182                 valid_param_mod = ParameterModifierType.PrimaryConstructor;
5183           }
5184           opt_primary_parameters_with_class_base
5185           opt_type_parameter_constraints_clauses
5186           {
5187                 valid_param_mod = 0;
5188                 lexer.ConstraintsParsing = false;
5189
5190                 if ($8 != null)
5191                         current_type.PrimaryConstructorParameters = (ParametersCompiled) $8;
5192
5193                 if ($9 != null)
5194                         current_container.SetConstraints ((List<Constraints>) $9);
5195                 lbag.AddMember (current_container, mod_locations, GetLocation ($4));
5196
5197                 if (doc_support) {
5198                         current_container.PartialContainer.DocComment = Lexer.consume_doc_comment ();
5199                         Lexer.doc_state = XmlCommentState.Allowed;
5200                 }
5201                 
5202                 lexer.parsing_modifiers = true;
5203           }
5204           OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
5205           {
5206                 --lexer.parsing_declaration;
5207                 if (doc_support)
5208                         Lexer.doc_state = XmlCommentState.Allowed;
5209           }
5210           opt_semicolon 
5211           {
5212                 if ($15 == null) {
5213                         lbag.AppendToMember (current_container, GetLocation ($11), GetLocation ($13));
5214                 } else {
5215                         lbag.AppendToMember (current_container, GetLocation ($11), GetLocation ($13), GetLocation ($15));
5216                 }
5217                 $$ = pop_current_class ();
5218           }
5219         ;       
5220
5221 opt_partial
5222         : /* empty */
5223           { $$ = null; }
5224         | PARTIAL
5225           { $$ = $1; } // location
5226         ;
5227
5228 opt_modifiers
5229         : /* empty */
5230           {
5231             mod_locations = null;
5232                 $$ = ModifierNone;
5233                 lexer.parsing_modifiers = false;
5234           }
5235         | modifiers
5236           {
5237                 lexer.parsing_modifiers = false;                
5238           }
5239         ;
5240
5241 modifiers
5242         : modifier
5243         | modifiers modifier
5244           { 
5245                 var m1 = (Modifiers) $1;
5246                 var m2 = (Modifiers) $2;
5247
5248                 if ((m1 & m2) != 0) {
5249                         report.Error (1004, lexer.Location - ModifiersExtensions.Name (m2).Length,
5250                                 "Duplicate `{0}' modifier", ModifiersExtensions.Name (m2));
5251                 } else if ((m2 & Modifiers.AccessibilityMask) != 0 && (m1 & Modifiers.AccessibilityMask) != 0 &&
5252                         ((m2 | m1 & Modifiers.AccessibilityMask) != (Modifiers.PROTECTED | Modifiers.INTERNAL))) {
5253                         report.Error (107, lexer.Location - ModifiersExtensions.Name (m2).Length,
5254                                 "More than one protection modifier specified");
5255                 }
5256                 
5257                 $$ = m1 | m2;
5258           }
5259         ;
5260
5261 modifier
5262         : NEW
5263           {
5264                 $$ = Modifiers.NEW;
5265                 StoreModifierLocation ($$, GetLocation ($1));
5266                 
5267                 if (current_container.Kind == MemberKind.Namespace)
5268                         report.Error (1530, GetLocation ($1), "Keyword `new' is not allowed on namespace elements");
5269           }
5270         | PUBLIC
5271           {
5272                 $$ = Modifiers.PUBLIC;
5273                 StoreModifierLocation ($$, GetLocation ($1));
5274           }
5275         | PROTECTED
5276           {
5277                 $$ = Modifiers.PROTECTED;
5278                 StoreModifierLocation ($$, GetLocation ($1));
5279           }
5280         | INTERNAL
5281           {
5282                 $$ = Modifiers.INTERNAL;
5283                 StoreModifierLocation ($$, GetLocation ($1));
5284           }
5285         | PRIVATE
5286           {
5287                 $$ = Modifiers.PRIVATE;
5288                 StoreModifierLocation ($$, GetLocation ($1));
5289           }
5290         | ABSTRACT
5291           {
5292                 $$ = Modifiers.ABSTRACT;
5293                 StoreModifierLocation ($$, GetLocation ($1));
5294           }
5295         | SEALED
5296           {
5297                 $$ = Modifiers.SEALED;
5298                 StoreModifierLocation ($$, GetLocation ($1));
5299           }
5300         | STATIC
5301           {
5302                 $$ = Modifiers.STATIC;
5303                 StoreModifierLocation ($$, GetLocation ($1));
5304           }
5305         | READONLY
5306           {
5307                 $$ = Modifiers.READONLY;
5308                 StoreModifierLocation ($$, GetLocation ($1));
5309           }
5310         | VIRTUAL
5311           {
5312                 $$ = Modifiers.VIRTUAL;
5313                 StoreModifierLocation ($$, GetLocation ($1));
5314           }
5315         | OVERRIDE
5316           {
5317                 $$ = Modifiers.OVERRIDE;
5318                 StoreModifierLocation ($$, GetLocation ($1));
5319           }
5320         | EXTERN
5321           {
5322                 $$ = Modifiers.EXTERN;
5323                 StoreModifierLocation ($$, GetLocation ($1));
5324           }
5325         | VOLATILE
5326           {
5327                 $$ = Modifiers.VOLATILE;
5328                 StoreModifierLocation ($$, GetLocation ($1));
5329           }
5330         | UNSAFE
5331           {
5332                 $$ = Modifiers.UNSAFE;
5333                 StoreModifierLocation ($$, GetLocation ($1));
5334                 if (!settings.Unsafe)
5335                         Error_UnsafeCodeNotAllowed (GetLocation ($1));
5336           }
5337         | ASYNC
5338           {
5339                 $$ = Modifiers.ASYNC;
5340                 StoreModifierLocation ($$, GetLocation ($1));
5341           }
5342         ;
5343         
5344 opt_class_base
5345         : /* empty */
5346         | class_base
5347         ;
5348
5349 class_base
5350         : COLON type_list
5351          {
5352                 current_type.SetBaseTypes ((List<FullNamedExpression>) $2);
5353          }
5354         | COLON type_list error
5355           {
5356                 Error_SyntaxError (yyToken);
5357
5358                 current_type.SetBaseTypes ((List<FullNamedExpression>) $2);
5359           }
5360         ;
5361
5362 opt_type_parameter_constraints_clauses
5363         : /* empty */
5364         | type_parameter_constraints_clauses 
5365           {
5366                 $$ = $1;
5367           }
5368         ;
5369
5370 type_parameter_constraints_clauses
5371         : type_parameter_constraints_clause
5372           {
5373                 var constraints = new List<Constraints> (1);
5374                 constraints.Add ((Constraints) $1);
5375                 $$ = constraints;
5376           }
5377         | type_parameter_constraints_clauses type_parameter_constraints_clause
5378           {
5379                 var constraints = (List<Constraints>) $1;
5380                 Constraints new_constraint = (Constraints)$2;
5381
5382                 foreach (Constraints c in constraints) {
5383                         if (new_constraint.TypeParameter.Value == c.TypeParameter.Value) {
5384                                 report.Error (409, new_constraint.Location,
5385                                         "A constraint clause has already been specified for type parameter `{0}'",
5386                                         new_constraint.TypeParameter.Value);
5387                         }
5388                 }
5389
5390                 constraints.Add (new_constraint);
5391                 $$ = constraints;
5392           }
5393         ; 
5394
5395 type_parameter_constraints_clause
5396         : WHERE IDENTIFIER COLON type_parameter_constraints
5397           {
5398                 var lt = (LocatedToken) $2;
5399                 $$ = new Constraints (new SimpleMemberName (lt.Value, lt.Location), (List<FullNamedExpression>) $4, GetLocation ($1));
5400                 lbag.AddLocation ($$, GetLocation ($3));
5401           }
5402         | WHERE IDENTIFIER error
5403           {
5404                 Error_SyntaxError (yyToken);
5405           
5406                 var lt = (LocatedToken) $2;
5407                 $$ = new Constraints (new SimpleMemberName (lt.Value, lt.Location), null, GetLocation ($1));
5408           }
5409         ; 
5410
5411 type_parameter_constraints
5412         : type_parameter_constraint
5413           {
5414                 var constraints = new List<FullNamedExpression> (1);
5415                 constraints.Add ((FullNamedExpression) $1);
5416                 $$ = constraints;
5417           }
5418         | type_parameter_constraints COMMA type_parameter_constraint
5419           {
5420                 var constraints = (List<FullNamedExpression>) $1;
5421                 var prev = constraints [constraints.Count - 1] as SpecialContraintExpr;
5422                 if (prev != null && (prev.Constraint & SpecialConstraint.Constructor) != 0) {                   
5423                         report.Error (401, GetLocation ($2), "The `new()' constraint must be the last constraint specified");
5424                 }
5425                 
5426                 prev = $3 as SpecialContraintExpr;
5427                 if (prev != null) {
5428                         if ((prev.Constraint & (SpecialConstraint.Class | SpecialConstraint.Struct)) != 0) {
5429                                 report.Error (449, prev.Location, "The `class' or `struct' constraint must be the first constraint specified");                 
5430                         } else {
5431                                 prev = constraints [0] as SpecialContraintExpr;
5432                                 if (prev != null && (prev.Constraint & SpecialConstraint.Struct) != 0) {                        
5433                                         report.Error (451, GetLocation ($3), "The `new()' constraint cannot be used with the `struct' constraint");
5434                                 }
5435                         }
5436                 }
5437
5438                 constraints.Add ((FullNamedExpression) $3);
5439                 $$ = constraints;
5440           }
5441         ;
5442
5443 type_parameter_constraint
5444         : type
5445           {
5446                 if ($1 is ComposedCast)
5447                         report.Error (706, GetLocation ($1), "Invalid constraint type `{0}'", ((ComposedCast)$1).GetSignatureForError ());
5448           
5449                 $$ = $1;
5450           }
5451         | NEW OPEN_PARENS CLOSE_PARENS
5452           {
5453                 $$ = new SpecialContraintExpr (SpecialConstraint.Constructor, GetLocation ($1));
5454                 lbag.AddLocation ($$, GetLocation ($2), GetLocation ($3));
5455           }
5456         | CLASS
5457           {
5458                 $$ = new SpecialContraintExpr (SpecialConstraint.Class, GetLocation ($1));
5459           }
5460         | STRUCT
5461           {
5462                 $$ = new SpecialContraintExpr (SpecialConstraint.Struct, GetLocation ($1));
5463           }
5464         ;
5465
5466 opt_type_parameter_variance
5467         : /* empty */
5468           {
5469                 $$ = null;
5470           }
5471         | type_parameter_variance
5472           {
5473                 if (lang_version <= LanguageVersion.V_3)
5474                         FeatureIsNotAvailable (lexer.Location, "generic type variance");
5475                 
5476                 $$ = $1;
5477           }
5478         ;
5479
5480 type_parameter_variance
5481         : OUT
5482           {
5483                 $$ = new VarianceDecl (Variance.Covariant, GetLocation ($1));
5484           }
5485         | IN
5486           {
5487                 $$ = new VarianceDecl (Variance.Contravariant, GetLocation ($1));
5488           }
5489         ;
5490
5491 //
5492 // Statements (8.2)
5493 //
5494
5495 //
5496 // A block is "contained" on the following places:
5497 //      method_body
5498 //      property_declaration as part of the accessor body (get/set)
5499 //      operator_declaration
5500 //      constructor_declaration
5501 //      destructor_declaration
5502 //      event_declaration as part of add_accessor_declaration or remove_accessor_declaration
5503 //      
5504 block
5505         : OPEN_BRACE  
5506           {
5507                 ++lexer.parsing_block;
5508                 start_block (GetLocation ($1));
5509           } 
5510           opt_statement_list block_end
5511           {
5512                 $$ = $4;
5513           }
5514         ;
5515
5516 block_end 
5517         : CLOSE_BRACE 
5518           {
5519                 --lexer.parsing_block;
5520                 $$ = end_block (GetLocation ($1));
5521           }
5522         | COMPLETE_COMPLETION
5523           {
5524                 --lexer.parsing_block;
5525                 $$ = end_block (lexer.Location);
5526           }
5527         ;
5528
5529
5530 block_prepared
5531         : OPEN_BRACE
5532           {
5533                 ++lexer.parsing_block;
5534                 current_block.StartLocation = GetLocation ($1);
5535           }
5536           opt_statement_list CLOSE_BRACE 
5537           {
5538                 --lexer.parsing_block;
5539                 $$ = end_block (GetLocation ($4));
5540           }
5541         ;
5542
5543 opt_statement_list
5544         : /* empty */
5545         | statement_list 
5546         ;
5547
5548 statement_list
5549         : statement
5550         | statement_list statement
5551         ;
5552
5553 statement
5554         : block_variable_declaration
5555           {
5556                 current_block.AddStatement ((Statement) $1);
5557           }
5558         | valid_declaration_statement
5559           {
5560                 current_block.AddStatement ((Statement) $1);
5561           }
5562         | labeled_statement
5563         | error
5564           {
5565                 Error_SyntaxError (yyToken);
5566                 $$ = null;
5567           }
5568         ;
5569
5570 //
5571 // The interactive_statement and its derivatives are only 
5572 // used to provide a special version of `expression_statement'
5573 // that has a side effect of assigning the expression to
5574 // $retval
5575 //
5576 interactive_statement_list
5577         : interactive_statement
5578         | interactive_statement_list interactive_statement
5579         ;
5580
5581 interactive_statement
5582         : block_variable_declaration
5583           {
5584                 current_block.AddStatement ((Statement) $1);
5585           }
5586         | interactive_valid_declaration_statement
5587           {
5588                 current_block.AddStatement ((Statement) $1);
5589           }
5590         | labeled_statement
5591         ;
5592
5593 valid_declaration_statement
5594         : block
5595         | empty_statement
5596         | expression_statement
5597         | selection_statement
5598         | iteration_statement
5599         | jump_statement                  
5600         | try_statement
5601         | checked_statement
5602         | unchecked_statement
5603         | lock_statement
5604         | using_statement
5605         | unsafe_statement
5606         | fixed_statement
5607         ;
5608
5609 interactive_valid_declaration_statement
5610         : block
5611         | empty_statement
5612         | interactive_expression_statement
5613         | selection_statement
5614         | iteration_statement
5615         | jump_statement                  
5616         | try_statement
5617         | checked_statement
5618         | unchecked_statement
5619         | lock_statement
5620         | using_statement
5621         | unsafe_statement
5622         | fixed_statement
5623         ;
5624
5625 embedded_statement
5626         : valid_declaration_statement
5627         | block_variable_declaration
5628           {
5629                   report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
5630                   $$ = null;
5631           }
5632         | labeled_statement
5633           {
5634                   report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
5635                   $$ = null;
5636           }
5637         | error
5638           {
5639                 Error_SyntaxError (yyToken);
5640                 $$ = new EmptyStatement (GetLocation ($1));
5641           }
5642         ;
5643
5644 empty_statement
5645         : SEMICOLON
5646           {
5647                 // Uses lexer.Location because semicolon location is not kept in quick mode
5648                 $$ = new EmptyStatement (lexer.Location);
5649           }
5650         ;
5651
5652 labeled_statement
5653         : identifier_inside_body COLON 
5654           {
5655                 var lt = (LocatedToken) $1;
5656                 LabeledStatement labeled = new LabeledStatement (lt.Value, current_block, lt.Location);
5657                 lbag.AddLocation (labeled, GetLocation ($2));
5658                 current_block.AddLabel (labeled);
5659                 current_block.AddStatement (labeled);
5660           }
5661           statement
5662         ;
5663
5664 variable_type
5665         : variable_type_simple
5666         | variable_type_simple rank_specifiers
5667           {
5668                 if ($1 is VarExpr)
5669                         $1 = new SimpleName ("var", ((VarExpr) $1).Location);
5670           
5671                 $$ = new ComposedCast ((FullNamedExpression) $1, (ComposedTypeSpecifier) $2);
5672           }
5673         ;
5674
5675 /* 
5676  * The following is from Rhys' grammar:
5677  * > Types in local variable declarations must be recognized as 
5678  * > expressions to prevent reduce/reduce errors in the grammar.
5679  * > The expressions are converted into types during semantic analysis.
5680  */
5681 variable_type_simple
5682         : type_name_expression opt_nullable
5683           { 
5684                 // Ok, the above "primary_expression" is there to get rid of
5685                 // both reduce/reduce and shift/reduces in the grammar, it should
5686                 // really just be "type_name".  If you use type_name, a reduce/reduce
5687                 // creeps up.  If you use namespace_or_type_name (which is all we need
5688                 // really) two shift/reduces appear.
5689                 // 
5690
5691                 // So the super-trick is that primary_expression
5692                 // can only be either a SimpleName or a MemberAccess. 
5693                 // The MemberAccess case arises when you have a fully qualified type-name like :
5694                 // Foo.Bar.Blah i;
5695                 // SimpleName is when you have
5696                 // Blah i;
5697                 
5698                 var expr = (ATypeNameExpression) $1;
5699                 if ($2 == null) {
5700                         if (expr.Name == "var" && expr is SimpleName)
5701                                 $$ = new VarExpr (expr.Location);
5702                         else
5703                                 $$ = $1;
5704                 } else {
5705                         $$ = new ComposedCast (expr, (ComposedTypeSpecifier) $2);
5706                 }
5707           }
5708         | type_name_expression pointer_stars
5709           {
5710                 var expr = (ATypeNameExpression) $1;
5711                 $$ = new ComposedCast (expr, (ComposedTypeSpecifier) $2);
5712           }
5713         | builtin_type_expression
5714         | void_invalid
5715         ;
5716         
5717 pointer_stars
5718         : pointer_star
5719         | pointer_star pointer_stars
5720           {
5721                 ((ComposedTypeSpecifier) $1).Next = (ComposedTypeSpecifier) $2;
5722                 $$ = $1;
5723           }       
5724         ;
5725
5726 pointer_star
5727         : STAR
5728           {
5729                 $$ = ComposedTypeSpecifier.CreatePointer (GetLocation ($1));
5730           }
5731         ;
5732
5733 identifier_inside_body
5734         : IDENTIFIER
5735         | AWAIT
5736           {
5737                 $$ = Error_AwaitAsIdentifier ($1);
5738           }
5739         ;
5740
5741 block_variable_declaration
5742         : variable_type identifier_inside_body
5743           {
5744                 var lt = (LocatedToken) $2;
5745                 var li = new LocalVariable (current_block, lt.Value, lt.Location);
5746                 current_block.AddLocalName (li);
5747                 current_variable = new BlockVariable ((FullNamedExpression) $1, li);
5748           }
5749           opt_local_variable_initializer opt_variable_declarators SEMICOLON
5750           {
5751                 $$ = current_variable;
5752                 current_variable = null;
5753                 if ($4 != null)
5754                         lbag.AddLocation ($$, PopLocation (), GetLocation ($6));
5755                 else
5756                         lbag.AddLocation ($$, GetLocation ($6));
5757           }
5758         | CONST variable_type identifier_inside_body
5759           {
5760                 var lt = (LocatedToken) $3;
5761                 var li = new LocalVariable (current_block, lt.Value, LocalVariable.Flags.Constant, lt.Location);
5762                 current_block.AddLocalName (li);
5763                 current_variable = new BlockConstant ((FullNamedExpression) $2, li);
5764           }
5765           const_variable_initializer opt_const_declarators SEMICOLON
5766           {
5767                 $$ = current_variable;
5768                 current_variable = null;
5769                 lbag.AddLocation ($$, GetLocation ($1), GetLocation ($7));
5770           }
5771         ;
5772
5773 opt_local_variable_initializer
5774         : /* empty */
5775         | ASSIGN block_variable_initializer
5776           {
5777                 current_variable.Initializer = (Expression) $2;
5778                 PushLocation (GetLocation ($1));
5779                 $$ = current_variable;
5780           }
5781         | error
5782           {
5783                 if (yyToken == Token.OPEN_BRACKET_EXPR) {
5784                         report.Error (650, lexer.Location,
5785                                 "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");
5786                 } else {
5787                         Error_SyntaxError (yyToken);
5788                 }
5789           }
5790         ;
5791
5792 opt_variable_declarators
5793         : /* empty */
5794         | variable_declarators
5795         ;
5796         
5797 opt_using_or_fixed_variable_declarators
5798         : /* empty */
5799         | variable_declarators
5800           {
5801                 foreach (var d in current_variable.Declarators) {
5802                         if (d.Initializer == null)
5803                                 Error_MissingInitializer (d.Variable.Location);
5804                 }
5805           }
5806         ;       
5807         
5808 variable_declarators
5809         : variable_declarator
5810         | variable_declarators variable_declarator
5811         ;
5812         
5813 variable_declarator
5814         : COMMA identifier_inside_body
5815           {
5816                 var lt = (LocatedToken) $2;       
5817                 var li = new LocalVariable (current_variable.Variable, lt.Value, lt.Location);
5818                 var d = new BlockVariableDeclarator (li, null);
5819                 current_variable.AddDeclarator (d);
5820                 current_block.AddLocalName (li);
5821                 lbag.AddLocation (d, GetLocation ($1));
5822           }
5823         | COMMA identifier_inside_body ASSIGN block_variable_initializer
5824           {
5825                 var lt = (LocatedToken) $2;       
5826                 var li = new LocalVariable (current_variable.Variable, lt.Value, lt.Location);
5827                 var d = new BlockVariableDeclarator (li, (Expression) $4);
5828                 current_variable.AddDeclarator (d);
5829                 current_block.AddLocalName (li);
5830                 lbag.AddLocation (d, GetLocation ($1), GetLocation ($3));
5831           }
5832         ;
5833         
5834 const_variable_initializer
5835         : /* empty */
5836           {
5837                 report.Error (145, lexer.Location, "A const field requires a value to be provided");
5838           }
5839         | ASSIGN constant_initializer_expr 
5840           {
5841                 current_variable.Initializer = (Expression) $2;
5842           }
5843         ;
5844         
5845 opt_const_declarators
5846         : /* empty */
5847         | const_declarators
5848         ;
5849         
5850 const_declarators
5851         : const_declarator
5852         | const_declarators const_declarator
5853         ;
5854         
5855 const_declarator
5856         : COMMA identifier_inside_body ASSIGN constant_initializer_expr
5857           {
5858                 var lt = (LocatedToken) $2;       
5859                 var li = new LocalVariable (current_block, lt.Value, LocalVariable.Flags.Constant, lt.Location);
5860                 var d = new BlockVariableDeclarator (li, (Expression) $4);
5861                 current_variable.AddDeclarator (d);
5862                 current_block.AddLocalName (li);
5863                 lbag.AddLocation (d, GetLocation ($1), GetLocation ($3));
5864           }
5865         ;
5866         
5867 block_variable_initializer
5868         : variable_initializer
5869         | STACKALLOC simple_type OPEN_BRACKET_EXPR expression CLOSE_BRACKET
5870           {
5871                 $$ = new StackAlloc ((Expression) $2, (Expression) $4, GetLocation ($1));
5872                 lbag.AddLocation ($$, GetLocation ($3), GetLocation ($5));
5873           }
5874         | STACKALLOC simple_type
5875           {
5876                 report.Error (1575, GetLocation ($1), "A stackalloc expression requires [] after type");
5877                 $$ = new StackAlloc ((Expression) $2, null, GetLocation ($1));          
5878           }
5879         ;
5880
5881 expression_statement
5882         : statement_expression SEMICOLON
5883           {
5884                 $$ = $1;
5885                 lbag.AddStatement ($$, GetLocation ($2));
5886           }
5887         | statement_expression COMPLETE_COMPLETION { $$ = $1; }
5888         | statement_expression CLOSE_BRACE
5889           {
5890                 $$ = $1;
5891                 report.Error (1002, GetLocation ($2), "; expected");
5892                 lexer.putback ('}');
5893           }
5894         ;
5895
5896 interactive_expression_statement
5897         : interactive_statement_expression SEMICOLON { $$ = $1; }
5898         | interactive_statement_expression COMPLETE_COMPLETION { $$ = $1; }
5899         ;
5900
5901         //
5902         // We have to do the wrapping here and not in the case above,
5903         // because statement_expression is used for example in for_statement
5904         //
5905 statement_expression
5906         : expression
5907           {
5908                 ExpressionStatement s = $1 as ExpressionStatement;
5909                 if (s == null) {
5910                         var expr = $1 as Expression;
5911                         $$ = new StatementErrorExpression (expr);
5912                 } else {
5913                         $$ = new StatementExpression (s);
5914                 }
5915           }
5916         ;
5917
5918 interactive_statement_expression
5919         : expression
5920           {
5921                 Expression expr = (Expression) $1;
5922                 $$ = new StatementExpression (new OptionalAssign (expr, lexer.Location));
5923           }
5924         | error
5925           {
5926                 Error_SyntaxError (yyToken);
5927                 $$ = new EmptyStatement (GetLocation ($1));
5928           }
5929         ;
5930         
5931 selection_statement
5932         : if_statement
5933         | switch_statement
5934         ; 
5935
5936 if_statement
5937         : IF open_parens_any boolean_expression CLOSE_PARENS 
5938           embedded_statement
5939           { 
5940                 if ($5 is EmptyStatement)
5941                         Warning_EmptyStatement (GetLocation ($5));
5942                 
5943                 $$ = new If ((BooleanExpression) $3, (Statement) $5, GetLocation ($1));
5944                 lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4));
5945           }
5946         | IF open_parens_any boolean_expression CLOSE_PARENS
5947           embedded_statement ELSE embedded_statement
5948           {
5949                 $$ = new If ((BooleanExpression) $3, (Statement) $5, (Statement) $7, GetLocation ($1));
5950                 lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4), GetLocation ($6));
5951                 
5952                 if ($5 is EmptyStatement)
5953                         Warning_EmptyStatement (GetLocation ($5));
5954                 if ($7 is EmptyStatement)
5955                         Warning_EmptyStatement (GetLocation ($7));
5956           }
5957         | IF open_parens_any boolean_expression error
5958           {
5959                 Error_SyntaxError (yyToken);
5960                 
5961                 $$ = new If ((BooleanExpression) $3, null, GetLocation ($1));
5962                 lbag.AddStatement ($$, GetLocation ($2));
5963           }
5964         ;
5965
5966 switch_statement
5967         : SWITCH open_parens_any expression CLOSE_PARENS OPEN_BRACE
5968           {
5969                 start_block (GetLocation ($5));
5970           }
5971           opt_switch_sections CLOSE_BRACE
5972           {
5973                 $$ = new Switch ((Expression) $3, (ExplicitBlock) current_block.Explicit, GetLocation ($1));    
5974                 end_block (GetLocation ($8));
5975                 lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4));
5976           }
5977         | SWITCH open_parens_any expression error
5978           {
5979                 Error_SyntaxError (yyToken);
5980           
5981                 $$ = new Switch ((Expression) $3, null, GetLocation ($1));      
5982                 lbag.AddStatement ($$, GetLocation ($2));
5983           }
5984         ;
5985
5986 opt_switch_sections
5987         : /* empty */           
5988       {
5989                 report.Warning (1522, 1, current_block.StartLocation, "Empty switch block"); 
5990           }
5991         | switch_sections
5992         ;
5993
5994 switch_sections
5995         : switch_section 
5996         | switch_sections switch_section
5997         | error
5998           {
5999                 Error_SyntaxError (yyToken);
6000           } 
6001         ;
6002
6003 switch_section
6004         : switch_labels statement_list 
6005         ;
6006
6007 switch_labels
6008         : switch_label 
6009           {
6010                 var label = (SwitchLabel) $1;
6011                 label.SectionStart = true;
6012                 current_block.AddStatement (label);
6013           }
6014         | switch_labels switch_label 
6015           {
6016                 current_block.AddStatement ((Statement) $2);
6017           }
6018         ;
6019
6020 switch_label
6021         : CASE constant_expression COLON
6022          {
6023                 $$ = new SwitchLabel ((Expression) $2, GetLocation ($1));
6024                 lbag.AddLocation ($$, GetLocation ($3));
6025          }
6026         | CASE constant_expression error
6027           {
6028                 Error_SyntaxError (yyToken);
6029                 $$ = new SwitchLabel ((Expression) $2, GetLocation ($1));
6030           }
6031 /*        
6032         | CASE pattern_expr_invocation COLON
6033           {
6034                 if (lang_version != LanguageVersion.Experimental)
6035                         FeatureIsNotAvailable (GetLocation ($2), "pattern matching");
6036
6037                 $$ = new SwitchLabel ((Expression) $2, GetLocation ($1)) {
6038                         PatternMatching = true
6039                 };
6040                 lbag.AddLocation ($$, GetLocation ($3));
6041           }
6042 */
6043         | DEFAULT_COLON
6044           {
6045                 $$ = new SwitchLabel (null, GetLocation ($1));
6046           }
6047         ;
6048
6049 iteration_statement
6050         : while_statement
6051         | do_statement
6052         | for_statement
6053         | foreach_statement
6054         ;
6055
6056 while_statement
6057         : WHILE open_parens_any boolean_expression CLOSE_PARENS embedded_statement
6058           {
6059                 if ($5 is EmptyStatement && lexer.peek_token () == Token.OPEN_BRACE)
6060                         Warning_EmptyStatement (GetLocation ($5));
6061           
6062                 $$ = new While ((BooleanExpression) $3, (Statement) $5, GetLocation ($1));
6063                 lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4));
6064           }
6065         | WHILE open_parens_any boolean_expression error
6066           {
6067                 Error_SyntaxError (yyToken);
6068                 
6069                 $$ = new While ((BooleanExpression) $3, null, GetLocation ($1));
6070                 lbag.AddStatement ($$, GetLocation ($2));
6071           }
6072         ;
6073
6074 do_statement
6075         : DO embedded_statement WHILE open_parens_any boolean_expression CLOSE_PARENS SEMICOLON
6076           {
6077                 $$ = new Do ((Statement) $2, (BooleanExpression) $5, GetLocation ($1), GetLocation ($3));
6078                 lbag.AddStatement ($$, GetLocation ($3), GetLocation ($4), GetLocation ($6), GetLocation ($7));
6079           }
6080         | DO embedded_statement error
6081           {
6082                 Error_SyntaxError (yyToken);
6083                 $$ = new Do ((Statement) $2, null, GetLocation ($1), Location.Null);
6084           }
6085         | DO embedded_statement WHILE open_parens_any boolean_expression error
6086           {
6087                 Error_SyntaxError (yyToken);
6088           
6089                 $$ = new Do ((Statement) $2, (BooleanExpression) $5, GetLocation ($1), GetLocation ($3));
6090                 lbag.AddStatement ($$, GetLocation ($3), GetLocation ($4));
6091           }
6092         ;
6093
6094 for_statement
6095         : FOR open_parens_any
6096           {
6097                 start_block (GetLocation ($2));
6098                 current_block.IsCompilerGenerated = true;
6099                 For f = new For (GetLocation ($1));
6100                 current_block.AddStatement (f);
6101                 $$ = f;
6102           }
6103           for_statement_cont
6104           {
6105                 $$ = $4;
6106           }
6107         ;
6108         
6109 // Has to use be extra rule to recover started block
6110 for_statement_cont
6111         : opt_for_initializer SEMICOLON
6112           {
6113                 ((For) $0).Initializer = (Statement) $1;
6114
6115                 // Pass the "For" object to the iterator_part4
6116                 oob_stack.Push ($0);
6117           }
6118           for_condition_and_iterator_part
6119           embedded_statement
6120           {
6121                 var locations = (Tuple<Location,Location>) $4;
6122                 oob_stack.Pop ();
6123                 if ($5 is EmptyStatement && lexer.peek_token () == Token.OPEN_BRACE)
6124                         Warning_EmptyStatement (GetLocation ($5));
6125           
6126                 For f = ((For) $0);
6127                 f.Statement = (Statement) $5;
6128                 lbag.AddStatement (f, current_block.StartLocation, GetLocation ($2), GetLocation (locations.Item1), GetLocation (locations.Item2));
6129
6130                 $$ = end_block (GetLocation ($2));
6131           }
6132         | error
6133           {
6134                 Error_SyntaxError (yyToken);
6135                 $$ = end_block (current_block.StartLocation);
6136           }
6137         ;
6138
6139 for_condition_and_iterator_part
6140         : opt_for_condition SEMICOLON
6141           {
6142                 For f = (For) oob_stack.Peek ();
6143                 f.Condition = (BooleanExpression) $1;
6144           }
6145           for_iterator_part {
6146                 $$ = new Tuple<Location,Location> (GetLocation ($2), (Location) $4);
6147           }
6148
6149         // Handle errors in the case of opt_for_condition being followed by
6150         // a close parenthesis
6151         | opt_for_condition close_parens_close_brace {
6152                 report.Error (1525, GetLocation ($2), "Unexpected symbol `}'");
6153                 For f = (For) oob_stack.Peek ();
6154                 f.Condition = (BooleanExpression) $1;
6155                 $$ = new Tuple<Location,Location> (GetLocation ($2), GetLocation ($2));
6156           }
6157         ;
6158
6159 for_iterator_part
6160         : opt_for_iterator CLOSE_PARENS {
6161                 For f = (For) oob_stack.Peek ();
6162                 f.Iterator = (Statement) $1;
6163                 $$ = GetLocation ($2);
6164           }
6165         | opt_for_iterator CLOSE_BRACE {
6166                 report.Error (1525, GetLocation ($2), "Unexpected symbol expected ')'");
6167                 For f = (For) oob_stack.Peek ();
6168                 f.Iterator = (Statement) $1;
6169                 $$ = GetLocation ($2);
6170           }
6171         ; 
6172
6173 close_parens_close_brace 
6174         : CLOSE_PARENS
6175         | CLOSE_BRACE { lexer.putback ('}'); }
6176         ;
6177
6178 opt_for_initializer
6179         : /* empty */           { $$ = new EmptyStatement (lexer.Location); }
6180         | for_initializer       
6181         ;
6182
6183 for_initializer
6184         : variable_type identifier_inside_body
6185           {
6186                 var lt = (LocatedToken) $2;
6187                 var li = new LocalVariable (current_block, lt.Value, lt.Location);
6188                 current_block.AddLocalName (li);
6189                 current_variable = new BlockVariable ((FullNamedExpression) $1, li);
6190           }
6191           opt_local_variable_initializer opt_variable_declarators
6192           {
6193                 $$ = current_variable;
6194                 if ($4 != null)
6195                         lbag.AddLocation (current_variable, PopLocation ());
6196
6197                 current_variable = null;
6198           }
6199         | statement_expression_list
6200         ;
6201
6202 opt_for_condition
6203         : /* empty */           { $$ = null; }
6204         | boolean_expression
6205         ;
6206
6207 opt_for_iterator
6208         : /* empty */           { $$ = new EmptyStatement (lexer.Location); }
6209         | for_iterator
6210         ;
6211
6212 for_iterator
6213         : statement_expression_list
6214         ;
6215
6216 statement_expression_list
6217         : statement_expression
6218         | statement_expression_list COMMA statement_expression
6219           {
6220                 var sl = $1 as StatementList;
6221                 if (sl == null) {
6222                         sl = new StatementList ((Statement) $1, (Statement) $3);
6223                         lbag.AddStatement (sl, GetLocation ($2));
6224                 } else {
6225                         sl.Add ((Statement) $3);
6226                         lbag.AppendTo (sl, GetLocation ($2));
6227                 }
6228                         
6229                 $$ = sl;
6230           }
6231         ;
6232
6233 foreach_statement
6234         : FOREACH open_parens_any type error
6235           {
6236                 report.Error (230, GetLocation ($1), "Type and identifier are both required in a foreach statement");
6237
6238                 start_block (GetLocation ($2));
6239                 current_block.IsCompilerGenerated = true;
6240                 
6241                 Foreach f = new Foreach ((Expression) $3, null, null, null, null, GetLocation ($1));
6242                 current_block.AddStatement (f);
6243                 
6244                 lbag.AddStatement (f, GetLocation ($2));
6245                 $$ = end_block (GetLocation ($4));
6246           }
6247         | FOREACH open_parens_any type identifier_inside_body error
6248           {
6249                 Error_SyntaxError (yyToken);
6250         
6251                 start_block (GetLocation ($2));
6252                 current_block.IsCompilerGenerated = true;
6253                 
6254                 var lt = (LocatedToken) $4;
6255                 var li = new LocalVariable (current_block, lt.Value, LocalVariable.Flags.ForeachVariable | LocalVariable.Flags.Used, lt.Location);
6256                 current_block.AddLocalName (li);
6257                 
6258                 Foreach f = new Foreach ((Expression) $3, li, null, null, null, GetLocation ($1));
6259                 current_block.AddStatement (f);
6260                 
6261                 lbag.AddStatement (f, GetLocation ($2));
6262                 $$ = end_block (GetLocation ($5));
6263           }
6264         | FOREACH open_parens_any type identifier_inside_body IN expression CLOSE_PARENS 
6265           {
6266                 start_block (GetLocation ($2));
6267                 current_block.IsCompilerGenerated = true;
6268                 
6269                 var lt = (LocatedToken) $4;
6270                 var li = new LocalVariable (current_block, lt.Value, LocalVariable.Flags.ForeachVariable | LocalVariable.Flags.Used, lt.Location);
6271                 current_block.AddLocalName (li);
6272                 $$ = li;
6273           } 
6274           embedded_statement
6275           {
6276                 if ($9 is EmptyStatement && lexer.peek_token () == Token.OPEN_BRACE)
6277                         Warning_EmptyStatement (GetLocation ($9));
6278                 
6279                 Foreach f = new Foreach ((Expression) $3, (LocalVariable) $8, (Expression) $6, (Statement) $9, current_block, GetLocation ($1));
6280                 lbag.AddStatement (f, GetLocation ($2), GetLocation ($5), GetLocation ($7));
6281                 end_block (GetLocation ($7));
6282                 
6283                 $$ = f;
6284           }
6285         ;
6286
6287 jump_statement
6288         : break_statement
6289         | continue_statement
6290         | goto_statement
6291         | return_statement
6292         | throw_statement
6293         | yield_statement
6294         ;
6295
6296 break_statement
6297         : BREAK SEMICOLON
6298           {
6299                 $$ = new Break (GetLocation ($1));
6300                 lbag.AddStatement ($$, GetLocation ($2));
6301           }
6302         ;
6303
6304 continue_statement
6305         : CONTINUE SEMICOLON
6306           {
6307                 $$ = new Continue (GetLocation ($1));
6308                 lbag.AddStatement ($$, GetLocation ($2));
6309           }
6310         | CONTINUE error
6311           {
6312                 Error_SyntaxError (yyToken);
6313                 $$ = new Continue (GetLocation ($1));
6314           }
6315         ;
6316
6317 goto_statement
6318         : GOTO identifier_inside_body SEMICOLON 
6319           {
6320                 var lt = (LocatedToken) $2;
6321                 $$ = new Goto (lt.Value, GetLocation ($1));
6322                 lbag.AddStatement ($$, GetLocation ($2), GetLocation ($3));
6323           }
6324         | GOTO CASE constant_expression SEMICOLON
6325           {
6326                 $$ = new GotoCase ((Expression) $3, GetLocation ($1));
6327                 lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4));
6328           }
6329         | GOTO DEFAULT SEMICOLON 
6330           {
6331                 $$ = new GotoDefault (GetLocation ($1));
6332                 lbag.AddStatement ($$, GetLocation ($2), GetLocation ($3));
6333           }
6334         ; 
6335
6336 return_statement
6337         : RETURN opt_expression SEMICOLON
6338           {
6339                 $$ = new Return ((Expression) $2, GetLocation ($1));
6340                 lbag.AddStatement ($$, GetLocation ($3));
6341           }
6342         | RETURN expression error
6343           {
6344                 Error_SyntaxError (yyToken);
6345                 $$ = new Return ((Expression) $2, GetLocation ($1));
6346           }
6347         | RETURN error
6348           {
6349                 Error_SyntaxError (yyToken);
6350                 $$ = new Return (null, GetLocation ($1));
6351           }
6352         ;
6353
6354 throw_statement
6355         : THROW opt_expression SEMICOLON
6356           {
6357                 $$ = new Throw ((Expression) $2, GetLocation ($1));
6358                 lbag.AddStatement ($$, GetLocation ($3));
6359           }
6360         | THROW expression error
6361           {
6362                 Error_SyntaxError (yyToken);
6363                 $$ = new Throw ((Expression) $2, GetLocation ($1));
6364           }
6365         | THROW error
6366           {
6367                 Error_SyntaxError (yyToken);
6368                 $$ = new Throw (null, GetLocation ($1));
6369           }
6370         ;
6371
6372 yield_statement 
6373         : identifier_inside_body RETURN opt_expression SEMICOLON
6374           {
6375                 var lt = (LocatedToken) $1;
6376                 string s = lt.Value;
6377                 if (s != "yield"){
6378                         report.Error (1003, lt.Location, "; expected");
6379                 } else if ($3 == null) {
6380                         report.Error (1627, GetLocation ($4), "Expression expected after yield return");
6381                 } else if (lang_version == LanguageVersion.ISO_1){
6382                         FeatureIsNotAvailable (lt.Location, "iterators");
6383                 }
6384                 
6385                 current_block.Explicit.RegisterIteratorYield ();
6386                 $$ = new Yield ((Expression) $3, lt.Location);
6387                 lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4));
6388           }
6389         | identifier_inside_body RETURN expression error
6390           {
6391                 Error_SyntaxError (yyToken);
6392
6393                 var lt = (LocatedToken) $1;
6394                 string s = lt.Value;
6395                 if (s != "yield"){
6396                         report.Error (1003, lt.Location, "; expected");
6397                 } else if ($3 == null) {
6398                         report.Error (1627, GetLocation ($4), "Expression expected after yield return");
6399                 } else if (lang_version == LanguageVersion.ISO_1){
6400                         FeatureIsNotAvailable (lt.Location, "iterators");
6401                 }
6402                 
6403                 current_block.Explicit.RegisterIteratorYield ();
6404                 $$ = new Yield ((Expression) $3, lt.Location);
6405                 lbag.AddStatement ($$, GetLocation ($2));
6406           }
6407         | identifier_inside_body BREAK SEMICOLON
6408           {
6409                 var lt = (LocatedToken) $1;
6410                 string s = lt.Value;
6411                 if (s != "yield"){
6412                         report.Error (1003, lt.Location, "; expected");
6413                 } else if (lang_version == LanguageVersion.ISO_1){
6414                         FeatureIsNotAvailable (lt.Location, "iterators");
6415                 }
6416                 
6417                 current_block.ParametersBlock.TopBlock.IsIterator = true;
6418                 $$ = new YieldBreak (lt.Location);
6419                 lbag.AddStatement ($$, GetLocation ($2), GetLocation ($3));
6420           }
6421         ;
6422
6423 opt_expression
6424         : /* empty */
6425         | expression
6426         ;
6427
6428 try_statement
6429         : TRY block catch_clauses
6430           {
6431                 $$ = new TryCatch ((Block) $2, (List<Catch>) $3, GetLocation ($1), false);
6432           }
6433         | TRY block FINALLY block
6434           {
6435                 $$ = new TryFinally ((Statement) $2, (ExplicitBlock) $4, GetLocation ($1));
6436                 lbag.AddStatement ($$, GetLocation ($3));
6437           }
6438         | TRY block catch_clauses FINALLY block
6439           {
6440                 $$ = new TryFinally (new TryCatch ((Block) $2, (List<Catch>) $3, Location.Null, true), (ExplicitBlock) $5, GetLocation ($1));
6441                 lbag.AddStatement ($$, GetLocation ($4));
6442           }
6443         | TRY block error
6444           {
6445                 Error_SyntaxError (1524, yyToken);
6446                 $$ = new TryCatch ((Block) $2, null, GetLocation ($1), false);
6447           }
6448         ;
6449
6450 catch_clauses
6451         : catch_clause 
6452           {
6453                 var l = new List<Catch> (2);
6454
6455                 l.Add ((Catch) $1);
6456                 $$ = l;
6457           }
6458         | catch_clauses catch_clause
6459           {
6460                 var l = (List<Catch>) $1;
6461                 
6462                 Catch c = (Catch) $2;
6463                 var prev_catch = l [l.Count - 1];
6464                 if (prev_catch.IsGeneral && prev_catch.Filter == null) {
6465                         report.Error (1017, c.loc, "Try statement already has an empty catch block");
6466                 }
6467                 
6468                 l.Add (c);
6469                 $$ = l;
6470           }
6471         ;
6472
6473 opt_identifier
6474         : /* empty */
6475         | identifier_inside_body
6476         ;
6477
6478 catch_clause 
6479         : CATCH opt_catch_filter block
6480           {
6481                 var c = new Catch ((ExplicitBlock) $3, GetLocation ($1));
6482                 c.Filter = (CatchFilterExpression) $2;
6483                 $$ = c;
6484           }
6485         | CATCH open_parens_any type opt_identifier CLOSE_PARENS
6486           {
6487                 start_block (GetLocation ($2));
6488                 var c = new Catch ((ExplicitBlock) current_block, GetLocation ($1));
6489                 c.TypeExpression = (FullNamedExpression) $3;
6490
6491                 if ($4 != null) {
6492                         var lt = (LocatedToken) $4;
6493                         c.Variable = new LocalVariable (current_block, lt.Value, lt.Location);
6494                         current_block.AddLocalName (c.Variable);
6495                 }
6496                 
6497                 lbag.AddLocation (c, GetLocation ($2), GetLocation ($5));
6498                 $$ = c;
6499                 lexer.parsing_catch_when = true;
6500           }
6501           opt_catch_filter_or_error
6502           {
6503                 ((Catch) $6).Filter = (CatchFilterExpression) $7;
6504                 $$ = $6;
6505           }
6506         | CATCH open_parens_any error
6507           {
6508                 if (yyToken == Token.CLOSE_PARENS) {
6509                         report.Error (1015, lexer.Location,
6510                                 "A type that derives from `System.Exception', `object', or `string' expected");
6511                 } else {
6512                         Error_SyntaxError (yyToken);
6513                 }
6514                 
6515                 $$ = new Catch (null, GetLocation ($1));
6516           }
6517         ;
6518
6519 opt_catch_filter_or_error
6520         : opt_catch_filter block_prepared
6521           {
6522                 $$ = $1;
6523           }
6524         | error
6525           {
6526                 end_block (Location.Null);
6527                 Error_SyntaxError (yyToken);
6528                 $$ = null;
6529           }
6530         ;
6531
6532 opt_catch_filter
6533         : {
6534                 lexer.parsing_catch_when = false;
6535           }
6536         | WHEN
6537           {
6538                 lexer.parsing_catch_when = false;
6539           }
6540           open_parens_any expression CLOSE_PARENS
6541           {
6542                 if (lang_version <= LanguageVersion.V_5)
6543                         FeatureIsNotAvailable (GetLocation ($1), "exception filter");
6544
6545                 $$ = new CatchFilterExpression ((Expression) $4, GetLocation ($1));
6546                 lbag.AddLocation ($$, GetLocation ($3), GetLocation ($5));
6547           }
6548         ;
6549
6550 checked_statement
6551         : CHECKED block
6552           {
6553                 $$ = new Checked ((Block) $2, GetLocation ($1));
6554           }
6555         ;
6556
6557 unchecked_statement
6558         : UNCHECKED block
6559           {
6560                 $$ = new Unchecked ((Block) $2, GetLocation ($1));
6561           }
6562         ;
6563
6564 unsafe_statement
6565         : UNSAFE
6566           {
6567                 if (!settings.Unsafe)
6568                         Error_UnsafeCodeNotAllowed (GetLocation ($1));
6569           } block {
6570                 $$ = new Unsafe ((Block) $3, GetLocation ($1));
6571           }
6572         ;
6573
6574 lock_statement
6575         : LOCK open_parens_any expression CLOSE_PARENS embedded_statement
6576           {
6577                 if ($5 is EmptyStatement && lexer.peek_token () == Token.OPEN_BRACE)
6578                         Warning_EmptyStatement (GetLocation ($5));
6579           
6580                 $$ = new Lock ((Expression) $3, (Statement) $5, GetLocation ($1));
6581                 lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4));
6582           }
6583         | LOCK open_parens_any expression error
6584           {
6585                 Error_SyntaxError (yyToken);
6586
6587                 $$ = new Lock ((Expression) $3, null, GetLocation ($1));
6588                 lbag.AddStatement ($$, GetLocation ($2));
6589           }
6590         ;
6591
6592 fixed_statement
6593         : FIXED open_parens_any variable_type identifier_inside_body
6594           {
6595             start_block (GetLocation ($2));
6596             
6597                 current_block.IsCompilerGenerated = true;
6598                 var lt = (LocatedToken) $4;
6599                 var li = new LocalVariable (current_block, lt.Value, LocalVariable.Flags.FixedVariable | LocalVariable.Flags.Used, lt.Location);
6600                 current_block.AddLocalName (li);
6601                 current_variable = new Fixed.VariableDeclaration ((FullNamedExpression) $3, li);
6602           }
6603           using_or_fixed_variable_initializer opt_using_or_fixed_variable_declarators CLOSE_PARENS
6604           {
6605                 $$ = current_variable;
6606                 current_variable = null;
6607           }
6608           embedded_statement
6609           {
6610                 if ($10 is EmptyStatement && lexer.peek_token () == Token.OPEN_BRACE)
6611                         Warning_EmptyStatement (GetLocation ($10));
6612           
6613                 Fixed f = new Fixed ((Fixed.VariableDeclaration) $9, (Statement) $10, GetLocation ($1));
6614                 current_block.AddStatement (f);
6615                 lbag.AddStatement (f, GetLocation ($2), GetLocation ($8));
6616                 $$ = end_block (GetLocation ($8));
6617           }
6618         ;
6619
6620 using_statement
6621         : USING open_parens_any variable_type identifier_inside_body
6622           {
6623             start_block (GetLocation ($2));
6624             
6625                 current_block.IsCompilerGenerated = true;
6626                 var lt = (LocatedToken) $4;
6627                 var li = new LocalVariable (current_block, lt.Value, LocalVariable.Flags.UsingVariable | LocalVariable.Flags.Used, lt.Location);
6628                 current_block.AddLocalName (li);
6629                 current_variable = new Using.VariableDeclaration ((FullNamedExpression) $3, li);
6630           }
6631           using_initialization CLOSE_PARENS
6632           {
6633                 $$ = current_variable;    
6634                 current_variable = null;
6635           }
6636           embedded_statement
6637           {
6638                 if ($9 is EmptyStatement && lexer.peek_token () == Token.OPEN_BRACE)
6639                         Warning_EmptyStatement (GetLocation ($9));
6640           
6641                 Using u = new Using ((Using.VariableDeclaration) $8, (Statement) $9, GetLocation ($1));
6642                 current_block.AddStatement (u);
6643                 $$ = end_block (GetLocation ($7));
6644           }
6645         | USING open_parens_any expression CLOSE_PARENS embedded_statement
6646           {
6647                 if ($5 is EmptyStatement && lexer.peek_token () == Token.OPEN_BRACE)
6648                         Warning_EmptyStatement (GetLocation ($5));
6649           
6650                 $$ = new Using ((Expression) $3, (Statement) $5, GetLocation ($1));
6651                 lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4));
6652           }
6653         | USING open_parens_any expression error
6654           {
6655                 Error_SyntaxError (yyToken);
6656                 
6657                 $$ = new Using ((Expression) $3, null, GetLocation ($1));
6658                 lbag.AddStatement ($$, GetLocation ($2));
6659           }
6660         ;
6661         
6662 using_initialization
6663         : using_or_fixed_variable_initializer opt_using_or_fixed_variable_declarators
6664         | error
6665           {
6666                 // It has to be here for the parent to safely restore artificial block
6667                 Error_SyntaxError (yyToken);
6668           }
6669         ;
6670         
6671 using_or_fixed_variable_initializer
6672         : /* empty */
6673           {
6674                 Error_MissingInitializer (lexer.Location);
6675           }
6676         | ASSIGN variable_initializer
6677           {
6678                 current_variable.Initializer = (Expression) $2;
6679                 $$ = current_variable;
6680           }
6681         ;
6682
6683
6684 // LINQ
6685
6686 query_expression
6687         : first_from_clause query_body 
6688           {
6689                 lexer.query_parsing = false;
6690                         
6691                 Linq.AQueryClause from = $1 as Linq.AQueryClause;
6692                         
6693                 from.Tail.Next = (Linq.AQueryClause)$2;
6694                 $$ = from;
6695                 
6696                 current_block.SetEndLocation (lexer.Location);
6697                 current_block = current_block.Parent;
6698           }
6699         | nested_from_clause query_body
6700           {
6701                 Linq.AQueryClause from = $1 as Linq.AQueryClause;
6702                         
6703                 from.Tail.Next = (Linq.AQueryClause)$2;
6704                 $$ = from;
6705                 
6706                 current_block.SetEndLocation (lexer.Location);
6707                 current_block = current_block.Parent;
6708           }     
6709
6710         // Bubble up COMPLETE_COMPLETION productions
6711         | first_from_clause COMPLETE_COMPLETION {
6712                 lexer.query_parsing = false;
6713                 $$ = $1;
6714
6715                 current_block.SetEndLocation (lexer.Location);
6716                 current_block = current_block.Parent;
6717           }
6718         | nested_from_clause COMPLETE_COMPLETION {
6719                 $$ = $1;
6720                 current_block.SetEndLocation (lexer.Location);
6721                 current_block = current_block.Parent;
6722           }
6723         ;
6724         
6725 first_from_clause
6726         : FROM_FIRST identifier_inside_body IN expression
6727           {
6728                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
6729           
6730                 var lt = (LocatedToken) $2;
6731                 var rv = new Linq.RangeVariable (lt.Value, lt.Location);
6732                 var clause = new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$4, rv, GetLocation ($1));
6733                 lbag.AddLocation (clause, GetLocation ($3));
6734                 $$ = new Linq.QueryExpression (clause);
6735           }
6736         | FROM_FIRST type identifier_inside_body IN expression
6737           {
6738                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
6739           
6740                 var lt = (LocatedToken) $3;
6741                 var rv = new Linq.RangeVariable (lt.Value, lt.Location);
6742                 var clause = new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$5, rv, GetLocation ($1)) {
6743                                 IdentifierType = (FullNamedExpression)$2
6744                 };
6745                 lbag.AddLocation (clause, GetLocation ($4));
6746                 $$ = new Linq.QueryExpression (clause);
6747           }
6748         ;
6749
6750 nested_from_clause
6751         : FROM identifier_inside_body IN expression
6752           {
6753                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
6754           
6755                 var lt = (LocatedToken) $2;
6756                 var rv = new Linq.RangeVariable (lt.Value, lt.Location);
6757                 var clause = new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$4, rv, GetLocation ($1));
6758                 lbag.AddLocation (clause, GetLocation ($3));
6759                 $$ = new Linq.QueryExpression (clause);
6760           }
6761         | FROM type identifier_inside_body IN expression
6762           {
6763                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
6764           
6765                 var lt = (LocatedToken) $3;
6766                 var rv = new Linq.RangeVariable (lt.Value, lt.Location);
6767                 var clause = new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$5, rv, GetLocation ($1)) {
6768                                 IdentifierType = (FullNamedExpression)$2
6769                 };
6770                 lbag.AddLocation (clause, GetLocation ($4));
6771                 $$ = new Linq.QueryExpression (clause);
6772           }
6773         ;
6774         
6775 from_clause
6776         : FROM identifier_inside_body IN
6777           {
6778                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
6779           }
6780           expression_or_error
6781           {
6782                 var lt = (LocatedToken) $2;
6783                 var sn = new Linq.RangeVariable (lt.Value, lt.Location);
6784                 $$ = new Linq.SelectMany ((Linq.QueryBlock)current_block, sn, (Expression)$5, GetLocation ($1));
6785                 
6786                 current_block.SetEndLocation (lexer.Location);
6787                 current_block = current_block.Parent;
6788                 
6789                 ((Linq.QueryBlock)current_block).AddRangeVariable (sn);
6790                 lbag.AddLocation ($$, GetLocation ($3));
6791           }       
6792         | FROM type identifier_inside_body IN
6793           {
6794                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
6795           }
6796           expression_or_error
6797           {
6798                 var lt = (LocatedToken) $3;
6799                 var sn = new Linq.RangeVariable (lt.Value, lt.Location);
6800
6801                 $$ = new Linq.SelectMany ((Linq.QueryBlock)current_block, sn, (Expression)$6, GetLocation ($1)) {
6802                         IdentifierType = (FullNamedExpression)$2
6803                 };
6804                 
6805                 current_block.SetEndLocation (lexer.Location);
6806                 current_block = current_block.Parent;
6807                 
6808                 ((Linq.QueryBlock)current_block).AddRangeVariable (sn);
6809                 
6810                 lbag.AddLocation ($$, GetLocation ($4));
6811           }
6812         ;       
6813
6814 query_body
6815         : query_body_clauses select_or_group_clause opt_query_continuation 
6816           {
6817                 Linq.AQueryClause head = (Linq.AQueryClause)$2;
6818                 
6819                 if ($3 != null)
6820                         head.Next = (Linq.AQueryClause)$3;
6821                                 
6822                 if ($1 != null) {
6823                         Linq.AQueryClause clause = (Linq.AQueryClause)$1;
6824                         clause.Tail.Next = head;
6825                         head = clause;
6826                 }
6827                 
6828                 $$ = head;
6829           }
6830         | select_or_group_clause opt_query_continuation
6831           {
6832                 Linq.AQueryClause head = (Linq.AQueryClause)$2;
6833
6834                 if ($1 != null) {
6835                         Linq.AQueryClause clause = (Linq.AQueryClause)$1;
6836                         clause.Tail.Next = head;
6837                         head = clause;
6838                 }
6839                 
6840                 $$ = head;
6841           }
6842         | query_body_clauses COMPLETE_COMPLETION
6843         | query_body_clauses error
6844           {
6845                 report.Error (742, GetLocation ($2), "Unexpected symbol `{0}'. A query body must end with select or group clause", GetSymbolName (yyToken));
6846                 $$ = $1;
6847           }
6848         | error
6849           {
6850                 Error_SyntaxError (yyToken);
6851                 $$ = null;
6852           }
6853         ;
6854         
6855 select_or_group_clause
6856         : SELECT
6857           {
6858                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
6859           }
6860           expression_or_error
6861           {
6862                 $$ = new Linq.Select ((Linq.QueryBlock)current_block, (Expression)$3, GetLocation ($1));
6863
6864                 current_block.SetEndLocation (lexer.Location);
6865                 current_block = current_block.Parent;
6866           }
6867         | GROUP
6868           {
6869                 if (linq_clause_blocks == null)
6870                         linq_clause_blocks = new Stack<Linq.QueryBlock> ();
6871                         
6872                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
6873                 linq_clause_blocks.Push ((Linq.QueryBlock)current_block);
6874           }
6875           expression_or_error
6876           {
6877                 current_block.SetEndLocation (lexer.Location);
6878                 current_block = current_block.Parent;
6879           
6880                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
6881           }
6882           by_expression
6883           {
6884                 var obj = (object[]) $5;
6885
6886                 $$ = new Linq.GroupBy ((Linq.QueryBlock)current_block, (Expression)$3, linq_clause_blocks.Pop (), (Expression)obj[0], GetLocation ($1));
6887                 lbag.AddLocation ($$, (Location) obj[1]);
6888                 
6889                 current_block.SetEndLocation (lexer.Location);
6890                 current_block = current_block.Parent;
6891           }
6892         ;
6893
6894 by_expression
6895         : BY expression_or_error
6896           {
6897                 $$ = new object[] { $2, GetLocation ($1) };
6898           }
6899         | error
6900           {
6901                 Error_SyntaxError (yyToken);
6902                 $$ = new object[2] { null, Location.Null };
6903           }
6904         ;
6905         
6906 query_body_clauses
6907         : query_body_clause
6908         | query_body_clauses query_body_clause
6909           {
6910                 ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$2;
6911                 $$ = $1;
6912           }
6913         ;
6914         
6915 query_body_clause
6916         : from_clause
6917         | let_clause 
6918         | where_clause
6919         | join_clause
6920         | orderby_clause
6921         ;
6922         
6923 let_clause
6924         : LET identifier_inside_body ASSIGN 
6925           {
6926                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
6927           }
6928           expression_or_error
6929           {
6930                 var lt = (LocatedToken) $2;
6931                 var sn = new Linq.RangeVariable (lt.Value, lt.Location);
6932                 $$ = new Linq.Let ((Linq.QueryBlock) current_block, sn, (Expression)$5, GetLocation ($1));
6933                 lbag.AddLocation ($$, GetLocation ($3));
6934                 
6935                 current_block.SetEndLocation (lexer.Location);
6936                 current_block = current_block.Parent;
6937                 
6938                 ((Linq.QueryBlock)current_block).AddRangeVariable (sn);
6939           }
6940         ;
6941
6942 where_clause
6943         : WHERE
6944           {
6945                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
6946           }
6947           expression_or_error
6948           {
6949                 $$ = new Linq.Where ((Linq.QueryBlock)current_block, (Expression)$3, GetLocation ($1));
6950
6951                 current_block.SetEndLocation (lexer.Location);
6952                 current_block = current_block.Parent;
6953           }
6954         ;
6955         
6956 join_clause
6957         : JOIN identifier_inside_body IN
6958           {
6959                 if (linq_clause_blocks == null)
6960                         linq_clause_blocks = new Stack<Linq.QueryBlock> ();
6961                         
6962                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
6963                 linq_clause_blocks.Push ((Linq.QueryBlock) current_block);
6964           }
6965           expression_or_error ON
6966           {
6967                 current_block.SetEndLocation (lexer.Location);
6968                 current_block = current_block.Parent;
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 EQUALS
6974           {
6975                 current_block.AddStatement (new ContextualReturn ((Expression) $8));
6976                 current_block.SetEndLocation (lexer.Location);
6977                 current_block = current_block.Parent;
6978
6979                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
6980           }
6981           expression_or_error opt_join_into
6982           {
6983                 current_block.AddStatement (new ContextualReturn ((Expression) $11));
6984                 current_block.SetEndLocation (lexer.Location);
6985           
6986                 var outer_selector = linq_clause_blocks.Pop ();
6987                 var block = linq_clause_blocks.Pop ();
6988
6989                 var lt = (LocatedToken) $2;     
6990                 var sn = new Linq.RangeVariable (lt.Value, lt.Location);
6991                 Linq.RangeVariable into;
6992                 
6993                 if ($12 == null) {
6994                         into = sn;
6995                         $$ = new Linq.Join (block, sn, (Expression)$5, outer_selector, (Linq.QueryBlock) current_block, GetLocation ($1));
6996                         lbag.AddLocation ($$, GetLocation ($3), GetLocation ($6), GetLocation ($9));
6997                 } else {
6998                         //
6999                         // Set equals right side parent to beginning of linq query, it is not accessible therefore cannot cause name collisions
7000                         //
7001                         var parent = block.Parent;
7002                         while (parent is Linq.QueryBlock) {
7003                                 parent = parent.Parent;
7004                         }
7005                         current_block.Parent = parent;
7006                         
7007                         ((Linq.QueryBlock)current_block).AddRangeVariable (sn);
7008                 
7009                         lt = (LocatedToken) $12;
7010                         into = new Linq.RangeVariable (lt.Value, lt.Location);
7011
7012                         $$ = new Linq.GroupJoin (block, sn, (Expression)$5, outer_selector, (Linq.QueryBlock) current_block, into, GetLocation ($1));   
7013                         lbag.AddLocation ($$, GetLocation ($3), GetLocation ($6), GetLocation ($9), GetLocation ($12));
7014                 }
7015
7016                 current_block = block.Parent;
7017                 ((Linq.QueryBlock)current_block).AddRangeVariable (into);
7018           }
7019         | JOIN type identifier_inside_body IN
7020           {
7021                 if (linq_clause_blocks == null)
7022                         linq_clause_blocks = new Stack<Linq.QueryBlock> ();
7023                         
7024                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
7025                 linq_clause_blocks.Push ((Linq.QueryBlock) current_block);
7026           }
7027           expression_or_error ON
7028           {
7029                 current_block.SetEndLocation (lexer.Location);
7030                 current_block = current_block.Parent;
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 EQUALS
7036           {
7037                 current_block.AddStatement (new ContextualReturn ((Expression) $9));
7038                 current_block.SetEndLocation (lexer.Location);
7039                 current_block = current_block.Parent;
7040
7041                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
7042           }
7043           expression_or_error opt_join_into
7044           {
7045                 current_block.AddStatement (new ContextualReturn ((Expression) $12));
7046                 current_block.SetEndLocation (lexer.Location);
7047           
7048                 var outer_selector = linq_clause_blocks.Pop ();
7049                 var block = linq_clause_blocks.Pop ();
7050                 
7051                 var lt = (LocatedToken) $3;
7052                 var sn = new Linq.RangeVariable (lt.Value, lt.Location);
7053                 Linq.RangeVariable into;
7054                 
7055                 if ($13 == null) {
7056                         into = sn;              
7057                         $$ = new Linq.Join (block, sn, (Expression)$6, outer_selector, (Linq.QueryBlock) current_block, GetLocation ($1)) {
7058                                 IdentifierType = (FullNamedExpression)$2
7059                         };
7060                         lbag.AddLocation ($$, GetLocation ($3), GetLocation ($6), GetLocation ($9));
7061                 } else {
7062                         //
7063                         // Set equals right side parent to beginning of linq query, it is not accessible therefore cannot cause name collisions
7064                         //
7065                         var parent = block.Parent;
7066                         while (parent is Linq.QueryBlock) {
7067                                 parent = parent.Parent;
7068                         }
7069                         current_block.Parent = parent;
7070                 
7071                         ((Linq.QueryBlock)current_block).AddRangeVariable (sn);
7072                 
7073                         lt = (LocatedToken) $13;
7074                         into = new Linq.RangeVariable (lt.Value, lt.Location); // TODO:
7075                         
7076                         $$ = new Linq.GroupJoin (block, sn, (Expression)$6, outer_selector, (Linq.QueryBlock) current_block, into, GetLocation ($1)) {
7077                                 IdentifierType = (FullNamedExpression)$2
7078                         };                      
7079                 }
7080                 
7081                 current_block = block.Parent;
7082                 ((Linq.QueryBlock)current_block).AddRangeVariable (into);               
7083           }
7084         ;
7085         
7086 opt_join_into
7087         : /* empty */
7088         | INTO identifier_inside_body
7089           {
7090                 $$ = $2;
7091           }
7092         ;
7093         
7094 orderby_clause
7095         : ORDERBY
7096           {
7097                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
7098           }
7099           orderings
7100           {
7101                 current_block.SetEndLocation (lexer.Location);
7102                 current_block = current_block.Parent;
7103           
7104                 $$ = $3;
7105           }
7106         ;
7107         
7108 orderings
7109         : order_by
7110         | order_by COMMA
7111           {
7112                 current_block.SetEndLocation (lexer.Location);
7113                 current_block = current_block.Parent;
7114           
7115                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
7116           }
7117           orderings_then_by
7118           {
7119                 ((Linq.AQueryClause)$1).Next = (Linq.AQueryClause)$4;
7120                 $$ = $1;
7121           }
7122         ;
7123         
7124 orderings_then_by
7125         : then_by
7126         | orderings_then_by COMMA
7127          {
7128                 current_block.SetEndLocation (lexer.Location);
7129                 current_block = current_block.Parent;
7130           
7131                 current_block = new Linq.QueryBlock ((Linq.QueryBlock) current_block, lexer.Location);   
7132          }
7133          then_by
7134          {
7135                 ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$4;
7136                 $$ = $1;
7137          }
7138         ;       
7139         
7140 order_by
7141         : expression
7142           {
7143                 $$ = new Linq.OrderByAscending ((Linq.QueryBlock) current_block, (Expression)$1);       
7144           }
7145         | expression ASCENDING
7146           {
7147                 $$ = new Linq.OrderByAscending ((Linq.QueryBlock) current_block, (Expression)$1);       
7148                 lbag.AddLocation ($$, GetLocation ($2));
7149           }
7150         | expression DESCENDING
7151           {
7152                 $$ = new Linq.OrderByDescending ((Linq.QueryBlock) current_block, (Expression)$1);      
7153                 lbag.AddLocation ($$, GetLocation ($2));
7154           }
7155         ;
7156
7157 then_by
7158         : expression
7159           {
7160                 $$ = new Linq.ThenByAscending ((Linq.QueryBlock) current_block, (Expression)$1);        
7161           }
7162         | expression ASCENDING
7163           {
7164                 $$ = new Linq.ThenByAscending ((Linq.QueryBlock) current_block, (Expression)$1);        
7165                 lbag.AddLocation ($$, GetLocation ($2));
7166           }
7167         | expression DESCENDING
7168           {
7169                 $$ = new Linq.ThenByDescending ((Linq.QueryBlock) current_block, (Expression)$1);       
7170                 lbag.AddLocation ($$, GetLocation ($2));
7171           }     
7172         ;
7173
7174
7175 opt_query_continuation
7176         : /* empty */
7177         | INTO identifier_inside_body
7178           {
7179                 // query continuation block is not linked with query block but with block
7180                 // before. This means each query can use same range variable names for
7181                 // different identifiers.
7182
7183                 current_block.SetEndLocation (GetLocation ($1));
7184                 current_block = current_block.Parent;
7185         
7186                 current_block = new Linq.QueryBlock (current_block, lexer.Location);
7187                 
7188                 if (linq_clause_blocks == null)
7189                         linq_clause_blocks = new Stack<Linq.QueryBlock> ();
7190                         
7191                 linq_clause_blocks.Push ((Linq.QueryBlock) current_block);              
7192           }
7193           query_body
7194           {
7195                 var current_block = linq_clause_blocks.Pop ();    
7196                 var lt = (LocatedToken) $2;
7197                 var rv = new Linq.RangeVariable (lt.Value, lt.Location);
7198                 $$ = new Linq.QueryStartClause ((Linq.QueryBlock)current_block, null, rv, GetLocation ($1)) {
7199                         next = (Linq.AQueryClause)$4
7200                 };
7201           }
7202         ;
7203         
7204 //
7205 // Support for using the compiler as an interactive parser
7206 //
7207 // The INTERACTIVE_PARSER token is first sent to parse our
7208 // productions;  If the result is a Statement, the parsing
7209 // is repeated, this time with INTERACTIVE_PARSE_WITH_BLOCK
7210 // to setup the blocks in advance.
7211 //
7212 // This setup is here so that in the future we can add 
7213 // support for other constructs (type parsing, namespaces, etc)
7214 // that do not require a block to be setup in advance
7215 //
7216
7217 interactive_parsing
7218         : EVAL_STATEMENT_PARSER EOF 
7219         | EVAL_USING_DECLARATIONS_UNIT_PARSER using_directives opt_COMPLETE_COMPLETION
7220         | EVAL_STATEMENT_PARSER
7221          { 
7222                 current_container = current_type = new Class (current_container, new MemberName ("<InteractiveExpressionClass>"), Modifiers.PUBLIC, null);
7223
7224                 // (ref object retval)
7225                 Parameter [] mpar = new Parameter [1];
7226                 mpar [0] = new Parameter (new TypeExpression (compiler.BuiltinTypes.Object, Location.Null), "$retval", Parameter.Modifier.REF, null, Location.Null);
7227
7228                 ParametersCompiled pars = new ParametersCompiled (mpar);
7229                 var mods = Modifiers.PUBLIC | Modifiers.STATIC;
7230                 if (settings.Unsafe)
7231                         mods |= Modifiers.UNSAFE;
7232
7233                 current_local_parameters = pars;
7234                 var method = new InteractiveMethod (
7235                         current_type,
7236                         new TypeExpression (compiler.BuiltinTypes.Void, Location.Null),
7237                         mods,
7238                         pars);
7239                         
7240                 current_type.AddMember (method);                        
7241                 oob_stack.Push (method);
7242
7243                 interactive_async = false;
7244
7245                 ++lexer.parsing_block;
7246                 start_block (lexer.Location);
7247           }             
7248           interactive_statement_list opt_COMPLETE_COMPLETION
7249           {
7250                 --lexer.parsing_block;
7251                 var method = (InteractiveMethod) oob_stack.Pop ();
7252                 method.Block = (ToplevelBlock) end_block(lexer.Location);
7253
7254                 if (interactive_async == true) {
7255                         method.ChangeToAsync ();
7256                 }
7257
7258                 InteractiveResult = (Class) pop_current_class ();
7259                 current_local_parameters = null;
7260           } 
7261         | EVAL_COMPILATION_UNIT_PARSER interactive_compilation_unit
7262         ;
7263
7264 interactive_compilation_unit
7265         : opt_extern_alias_directives opt_using_directives
7266         | opt_extern_alias_directives opt_using_directives namespace_or_type_declarations
7267         ;
7268
7269 opt_COMPLETE_COMPLETION
7270         : /* nothing */
7271         | COMPLETE_COMPLETION
7272         ;
7273
7274 close_brace_or_complete_completion
7275         : CLOSE_BRACE
7276         | COMPLETE_COMPLETION
7277         ;
7278         
7279 //
7280 // XML documentation code references micro parser
7281 //
7282 documentation_parsing
7283         : DOC_SEE doc_cref
7284           {
7285                 module.DocumentationBuilder.ParsedName = (MemberName) $2;
7286           }
7287         ;
7288
7289 doc_cref
7290         : doc_type_declaration_name opt_doc_method_sig
7291           {
7292                 module.DocumentationBuilder.ParsedParameters = (List<DocumentationParameter>)$2;
7293           }
7294         | builtin_types opt_doc_method_sig
7295           {
7296                 module.DocumentationBuilder.ParsedBuiltinType = (TypeExpression)$1;
7297                 module.DocumentationBuilder.ParsedParameters = (List<DocumentationParameter>)$2;
7298                 $$ = null;
7299           }
7300         | VOID opt_doc_method_sig
7301           {
7302                 module.DocumentationBuilder.ParsedBuiltinType = new TypeExpression (compiler.BuiltinTypes.Void, GetLocation ($1));
7303                 module.DocumentationBuilder.ParsedParameters = (List<DocumentationParameter>)$2;
7304                 $$ = null;
7305           }
7306         | builtin_types DOT IDENTIFIER opt_doc_method_sig
7307           {
7308                 module.DocumentationBuilder.ParsedBuiltinType = (TypeExpression)$1;
7309                 module.DocumentationBuilder.ParsedParameters = (List<DocumentationParameter>)$4;
7310                 var lt = (LocatedToken) $3;
7311                 $$ = new MemberName (lt.Value);
7312           }
7313         | doc_type_declaration_name DOT THIS
7314           {
7315                 $$ = new MemberName ((MemberName) $1, MemberCache.IndexerNameAlias, Location.Null);
7316           }
7317         | doc_type_declaration_name DOT THIS OPEN_BRACKET
7318           {
7319                 valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out;
7320           }
7321           opt_doc_parameters CLOSE_BRACKET
7322           {
7323                 module.DocumentationBuilder.ParsedParameters = (List<DocumentationParameter>)$6;
7324                 $$ = new MemberName ((MemberName) $1, MemberCache.IndexerNameAlias, Location.Null);
7325           }
7326         | EXPLICIT OPERATOR type opt_doc_method_sig
7327           {
7328                 var p = (List<DocumentationParameter>)$4 ?? new List<DocumentationParameter> (1);
7329                 p.Add (new DocumentationParameter ((FullNamedExpression) $3));
7330                 module.DocumentationBuilder.ParsedParameters = p;
7331                 module.DocumentationBuilder.ParsedOperator = Operator.OpType.Explicit;
7332                 $$ = null;
7333           }
7334         | IMPLICIT 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.Implicit;
7340                 $$ = null;
7341           }       
7342         | OPERATOR overloadable_operator opt_doc_method_sig
7343           {
7344                 var p = (List<DocumentationParameter>)$3;
7345                 module.DocumentationBuilder.ParsedParameters = p;
7346                 module.DocumentationBuilder.ParsedOperator = (Operator.OpType) $2;
7347                 $$ = null;
7348           }
7349         ;
7350         
7351 doc_type_declaration_name
7352         : type_declaration_name
7353         | doc_type_declaration_name DOT type_declaration_name
7354           {
7355                 $$ = new MemberName (((MemberName) $1), (MemberName) $3);
7356           }
7357         ;
7358         
7359 opt_doc_method_sig
7360         : /* empty */
7361         | OPEN_PARENS
7362           {
7363                 valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out;
7364           }
7365           opt_doc_parameters CLOSE_PARENS
7366           {
7367                 $$ = $3;
7368           }
7369         ;
7370         
7371 opt_doc_parameters
7372         : /* empty */
7373           {
7374                 $$ = new List<DocumentationParameter> (0);
7375           }
7376         | doc_parameters
7377         ;
7378         
7379 doc_parameters
7380         : doc_parameter
7381           {
7382                 var parameters = new List<DocumentationParameter> ();
7383                 parameters.Add ((DocumentationParameter) $1);
7384                 $$ = parameters;
7385           }
7386         | doc_parameters COMMA doc_parameter
7387           {
7388                 var parameters = $1 as List<DocumentationParameter>;
7389                 parameters.Add ((DocumentationParameter) $3);
7390                 $$ = parameters;
7391           }
7392         ;
7393         
7394 doc_parameter
7395         : opt_parameter_modifier parameter_type
7396           {
7397                 if ($1 != null)
7398                         $$ = new DocumentationParameter ((Parameter.Modifier) $1, (FullNamedExpression) $2);
7399                 else
7400                         $$ = new DocumentationParameter ((FullNamedExpression) $2);
7401           }
7402         ;
7403         
7404 %%
7405
7406 // <summary>
7407 //  A class used to hold info about an operator declarator
7408 // </summary>
7409 class OperatorDeclaration {
7410         public readonly Operator.OpType optype;
7411         public readonly FullNamedExpression ret_type;
7412         public readonly Location location;
7413
7414         public OperatorDeclaration (Operator.OpType op, FullNamedExpression ret_type, Location location)
7415         {
7416                 optype = op;
7417                 this.ret_type = ret_type;
7418                 this.location = location;
7419         }
7420 }
7421
7422 void Error_ExpectingTypeName (Expression expr)
7423 {
7424         if (expr is Invocation){
7425                 report.Error (1002, expr.Location, "Expecting `;'");
7426         } else {
7427                 expr.Error_InvalidExpressionStatement (report);
7428         }
7429 }
7430
7431 void Error_ParameterModifierNotValid (string modifier, Location loc)
7432 {
7433         report.Error (631, loc, "The parameter modifier `{0}' is not valid in this context",
7434                                       modifier);
7435 }
7436
7437 void Error_DuplicateParameterModifier (Location loc, Parameter.Modifier mod)
7438 {
7439         report.Error (1107, loc, "Duplicate parameter modifier `{0}'",
7440                 Parameter.GetModifierSignature (mod));
7441 }
7442
7443 void Error_TypeExpected (Location loc)
7444 {
7445         report.Error (1031, loc, "Type expected");
7446 }
7447
7448 void Error_UnsafeCodeNotAllowed (Location loc)
7449 {
7450         report.Error (227, loc, "Unsafe code requires the `unsafe' command line option to be specified");
7451 }
7452
7453 void Warning_EmptyStatement (Location loc)
7454 {
7455         report.Warning (642, 3, loc, "Possible mistaken empty statement");
7456 }
7457
7458 void Error_NamedArgumentExpected (NamedArgument a)
7459 {
7460         report.Error (1738, a.Location, "Named arguments must appear after the positional arguments");
7461 }
7462
7463 void Error_MissingInitializer (Location loc)
7464 {
7465         report.Error (210, loc, "You must provide an initializer in a fixed or using statement declaration");
7466 }
7467
7468 object Error_AwaitAsIdentifier (object token)
7469 {
7470         if (async_block) {
7471                 report.Error (4003, GetLocation (token), "`await' cannot be used as an identifier within an async method or lambda expression");
7472                 return new LocatedToken ("await", GetLocation (token));
7473         }
7474
7475         return token;
7476 }
7477
7478 void push_current_container (TypeDefinition tc, object partial_token)
7479 {
7480         if (module.Evaluator != null){
7481                 tc.Definition.Modifiers = tc.ModFlags = (tc.ModFlags & ~Modifiers.AccessibilityMask) | Modifiers.PUBLIC;
7482                 if (undo == null)
7483                         undo = new Undo ();
7484
7485                 undo.AddTypeContainer (current_container, tc);
7486         }
7487         
7488         if (partial_token != null)
7489                 current_container.AddPartial (tc);
7490         else
7491                 current_container.AddTypeContainer (tc);
7492                 
7493         ++lexer.parsing_declaration;
7494         current_container = tc;
7495         current_type = tc;
7496 }
7497
7498 TypeContainer pop_current_class ()
7499 {
7500         var retval = current_container;
7501
7502         current_container = current_container.Parent;
7503         current_type = current_type.Parent as TypeDefinition;
7504
7505         return retval;
7506 }
7507
7508 [System.Diagnostics.Conditional ("FULL_AST")]
7509 void StoreModifierLocation (object token, Location loc)
7510 {
7511         if (lbag == null)
7512                 return;
7513
7514         if (mod_locations == null)
7515                 mod_locations = new List<Tuple<Modifiers, Location>> ();
7516
7517         mod_locations.Add (Tuple.Create ((Modifiers) token, loc));
7518 }
7519
7520 [System.Diagnostics.Conditional ("FULL_AST")]
7521 void PushLocation (Location loc)
7522 {
7523         if (location_stack == null)
7524                 location_stack = new Stack<Location> ();
7525
7526         location_stack.Push (loc);
7527 }
7528
7529 Location PopLocation ()
7530 {
7531         if (location_stack == null)
7532                 return Location.Null;
7533
7534         return location_stack.Pop ();
7535 }
7536
7537 string CheckAttributeTarget (int token, string a, Location l)
7538 {
7539         switch (a) {
7540         case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
7541                         return a;
7542         }
7543
7544         if (!Tokenizer.IsValidIdentifier (a)) {
7545                 Error_SyntaxError (token);
7546         } else {
7547                 report.Warning (658, 1, l,
7548                          "`{0}' is invalid attribute target. All attributes in this attribute section will be ignored", a);
7549         }
7550
7551         return string.Empty;
7552 }
7553
7554 static bool IsUnaryOperator (Operator.OpType op)
7555 {
7556         switch (op) {
7557                 
7558         case Operator.OpType.LogicalNot: 
7559         case Operator.OpType.OnesComplement: 
7560         case Operator.OpType.Increment:
7561         case Operator.OpType.Decrement:
7562         case Operator.OpType.True: 
7563         case Operator.OpType.False: 
7564         case Operator.OpType.UnaryPlus: 
7565         case Operator.OpType.UnaryNegation:
7566                 return true;
7567         }
7568         return false;
7569 }
7570
7571 void syntax_error (Location l, string msg)
7572 {
7573         report.Error (1003, l, "Syntax error, " + msg);
7574 }
7575
7576 Tokenizer lexer;
7577
7578 public Tokenizer Lexer {
7579         get {
7580                 return lexer;
7581         }
7582 }                  
7583
7584 public CSharpParser (SeekableStreamReader reader, CompilationSourceFile file, ParserSession session)
7585         : this (reader, file, file.Compiler.Report, session)
7586 {
7587 }
7588
7589 public CSharpParser (SeekableStreamReader reader, CompilationSourceFile file, Report report, ParserSession session)
7590 {
7591         this.file = file;
7592         current_container = current_namespace = file;
7593         
7594         this.module = file.Module;
7595         this.compiler = file.Compiler;
7596         this.settings = compiler.Settings;
7597         this.report = report;
7598         
7599         lang_version = settings.Version;
7600         yacc_verbose_flag = settings.VerboseParserFlag;
7601         doc_support = settings.DocumentationFile != null;
7602         lexer = new Tokenizer (reader, file, session, report);
7603         oob_stack = new Stack<object> ();
7604         lbag = session.LocationsBag;
7605         use_global_stacks = session.UseJayGlobalArrays;
7606         parameters_bucket = session.ParametersStack;
7607 }
7608
7609 public void parse ()
7610 {
7611         eof_token = Token.EOF;
7612         
7613         try {
7614                 if (yacc_verbose_flag > 1)
7615                         yyparse (lexer, new yydebug.yyDebugSimple ());
7616                 else
7617                         yyparse (lexer);
7618                         
7619                 Tokenizer tokenizer = lexer as Tokenizer;
7620                 tokenizer.cleanup ();           
7621         } catch (Exception e){
7622                 if (e is yyParser.yyUnexpectedEof) {
7623                         Error_SyntaxError (yyToken);
7624                         UnexpectedEOF = true;
7625                         return;
7626                 }
7627                         
7628                 if (e is yyParser.yyException) {
7629                         if (report.Errors == 0)
7630                                 report.Error (-25, lexer.Location, "Parsing error");
7631                 } else {
7632                         // Used by compiler-tester to test internal errors
7633                         if (yacc_verbose_flag > 0 || e is FatalException)
7634                                 throw;
7635                 
7636                         report.Error (589, lexer.Location, "Internal compiler error during parsing" + e);
7637                 }
7638         }
7639 }
7640
7641 void CheckToken (int error, int yyToken, string msg, Location loc)
7642 {
7643         if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
7644                 report.Error (error, loc, "{0}: `{1}' is a keyword", msg, GetTokenName (yyToken));
7645         else
7646                 report.Error (error, loc, msg);
7647 }
7648
7649 string ConsumeStoredComment ()
7650 {
7651         string s = tmpComment;
7652         tmpComment = null;
7653         Lexer.doc_state = XmlCommentState.Allowed;
7654         return s;
7655 }
7656
7657 void FeatureIsNotAvailable (Location loc, string feature)
7658 {
7659         report.FeatureIsNotAvailable (compiler, loc, feature);
7660 }
7661
7662 Location GetLocation (object obj)
7663 {
7664         var lt = obj as LocatedToken;
7665         if (lt != null)
7666                 return lt.Location;
7667                 
7668         var mn = obj as MemberName;
7669         if (mn != null)
7670                 return mn.Location;
7671                 
7672         var expr = obj as Expression;
7673         if (expr != null)
7674                 return expr.Location;
7675
7676         return lexer.Location;
7677 }
7678
7679 void start_block (Location loc)
7680 {
7681         if (current_block == null) {
7682                 current_block = new ToplevelBlock (compiler, current_local_parameters, loc);
7683                 parsing_anonymous_method = false;
7684         } else if (parsing_anonymous_method) {
7685                 current_block = new ParametersBlock (current_block, current_local_parameters, loc);
7686                 parsing_anonymous_method = false;
7687         } else {
7688                 current_block = new ExplicitBlock (current_block, loc, Location.Null);
7689         }
7690 }
7691
7692 Block
7693 end_block (Location loc)
7694 {
7695         Block retval = current_block.Explicit;
7696         retval.SetEndLocation (loc);
7697         current_block = retval.Parent;
7698         return retval;
7699 }
7700
7701 void start_anonymous (bool isLambda, ParametersCompiled parameters, bool isAsync, Location loc)
7702 {
7703         oob_stack.Push (current_anonymous_method);
7704         oob_stack.Push (current_local_parameters);
7705         oob_stack.Push (current_variable);
7706         oob_stack.Push (async_block);
7707
7708         current_local_parameters = parameters;
7709         if (isLambda) {
7710                 if (lang_version <= LanguageVersion.ISO_2)
7711                         FeatureIsNotAvailable (loc, "lambda expressions");
7712
7713                 current_anonymous_method = new LambdaExpression (loc);
7714         } else {
7715                 if (lang_version == LanguageVersion.ISO_1)
7716                         FeatureIsNotAvailable (loc, "anonymous methods");
7717                         
7718                 current_anonymous_method = new AnonymousMethodExpression (loc);
7719         }
7720
7721         async_block = isAsync;
7722         // Force the next block to be created as a ToplevelBlock
7723         parsing_anonymous_method = true;
7724 }
7725
7726 /*
7727  * Completes the anonymous method processing, if lambda_expr is null, this
7728  * means that we have a Statement instead of an Expression embedded 
7729  */
7730 AnonymousMethodExpression end_anonymous (ParametersBlock anon_block)
7731 {
7732         AnonymousMethodExpression retval;
7733
7734         if (async_block)
7735                 anon_block.IsAsync = true;
7736
7737         current_anonymous_method.Block = anon_block;
7738         retval = current_anonymous_method;
7739
7740         async_block = (bool) oob_stack.Pop ();
7741         current_variable = (BlockVariable) oob_stack.Pop ();
7742         current_local_parameters = (ParametersCompiled) oob_stack.Pop ();
7743         current_anonymous_method = (AnonymousMethodExpression) oob_stack.Pop ();
7744
7745         return retval;
7746 }
7747
7748 void Error_SyntaxError (int token)
7749 {
7750         Error_SyntaxError (0, token);
7751 }
7752
7753 void Error_SyntaxError (int error_code, int token)
7754 {
7755         Error_SyntaxError (error_code, token, "Unexpected symbol");
7756 }
7757
7758 void Error_SyntaxError (int error_code, int token, string msg)
7759 {
7760         Lexer.CompleteOnEOF = false;
7761
7762         // An error message has been reported by tokenizer
7763         if (token == Token.ERROR)
7764                 return;
7765         
7766         // Avoid duplicit error message after unterminated string literals
7767         if (token == Token.LITERAL && lexer.Location.Column == 0)
7768                 return;
7769
7770         string symbol = GetSymbolName (token);
7771         string expecting = GetExpecting ();
7772         var loc = lexer.Location - symbol.Length;
7773         
7774         if (error_code == 0) {
7775                 if (expecting == "`identifier'") {
7776                         if (token > Token.FIRST_KEYWORD && token < Token.LAST_KEYWORD) {
7777                                 report.Error (1041, loc, "Identifier expected, `{0}' is a keyword", symbol);
7778                                 return;
7779                         }
7780                         
7781                         error_code = 1001;
7782                         expecting = "identifier";
7783                 } else if (expecting == "`)'") {
7784                         error_code = 1026;
7785                 } else {
7786                         error_code = 1525;
7787                 }
7788         }
7789         
7790         if (string.IsNullOrEmpty (expecting))
7791                 report.Error (error_code, loc, "{1} `{0}'", symbol, msg);
7792         else
7793                 report.Error (error_code, loc, "{2} `{0}', expecting {1}", symbol, expecting, msg);       
7794 }
7795
7796 string GetExpecting ()
7797 {
7798         int [] tokens = yyExpectingTokens (yyExpectingState);
7799         var names = new List<string> (tokens.Length);
7800         bool has_type = false;
7801         bool has_identifier = false;
7802         for (int i = 0; i < tokens.Length; i++){
7803                 int token = tokens [i];
7804                 has_identifier |= token == Token.IDENTIFIER;
7805                 
7806                 string name = GetTokenName (token);
7807                 if (name == "<internal>")
7808                         continue;
7809                         
7810                 has_type |= name == "type";
7811                 if (names.Contains (name))
7812                         continue;
7813                 
7814                 names.Add (name);
7815         }
7816
7817         //
7818         // Too many tokens to enumerate
7819         //
7820         if (names.Count > 8)
7821                 return null;
7822
7823         if (has_type && has_identifier)
7824                 names.Remove ("identifier");
7825
7826         if (names.Count == 1)
7827                 return "`" + GetTokenName (tokens [0]) + "'";
7828         
7829         StringBuilder sb = new StringBuilder ();
7830         names.Sort ();
7831         int count = names.Count;
7832         for (int i = 0; i < count; i++){
7833                 bool last = i + 1 == count;
7834                 if (last)
7835                         sb.Append ("or ");
7836                 sb.Append ('`');
7837                 sb.Append (names [i]);
7838                 sb.Append (last ? "'" : count < 3 ? "' " : "', ");
7839         }
7840         return sb.ToString ();
7841 }
7842
7843
7844 string GetSymbolName (int token)
7845 {
7846         switch (token){
7847         case Token.LITERAL:
7848                 return ((Constant)lexer.Value).GetValue ().ToString ();
7849         case Token.IDENTIFIER:
7850                 return ((LocatedToken)lexer.Value).Value;
7851
7852         case Token.BOOL:
7853                 return "bool";
7854         case Token.BYTE:
7855                 return "byte";
7856         case Token.CHAR:
7857                 return "char";
7858         case Token.VOID:
7859                 return "void";
7860         case Token.DECIMAL:
7861                 return "decimal";
7862         case Token.DOUBLE:
7863                 return "double";
7864         case Token.FLOAT:
7865                 return "float";
7866         case Token.INT:
7867                 return "int";
7868         case Token.LONG:
7869                 return "long";
7870         case Token.SBYTE:
7871                 return "sbyte";
7872         case Token.SHORT:
7873                 return "short";
7874         case Token.STRING:
7875                 return "string";
7876         case Token.UINT:
7877                 return "uint";
7878         case Token.ULONG:
7879                 return "ulong";
7880         case Token.USHORT:
7881                 return "ushort";
7882         case Token.OBJECT:
7883                 return "object";
7884                 
7885         case Token.PLUS:
7886                 return "+";
7887         case Token.UMINUS:
7888         case Token.MINUS:
7889                 return "-";
7890         case Token.BANG:
7891                 return "!";
7892         case Token.BITWISE_AND:
7893                 return "&";
7894         case Token.BITWISE_OR:
7895                 return "|";
7896         case Token.STAR:
7897                 return "*";
7898         case Token.PERCENT:
7899                 return "%";
7900         case Token.DIV:
7901                 return "/";
7902         case Token.CARRET:
7903                 return "^";
7904         case Token.OP_INC:
7905                 return "++";
7906         case Token.OP_DEC:
7907                 return "--";
7908         case Token.OP_SHIFT_LEFT:
7909                 return "<<";
7910         case Token.OP_SHIFT_RIGHT:
7911                 return ">>";
7912         case Token.OP_LT:
7913                 return "<";
7914         case Token.OP_GT:
7915                 return ">";
7916         case Token.OP_LE:
7917                 return "<=";
7918         case Token.OP_GE:
7919                 return ">=";
7920         case Token.OP_EQ:
7921                 return "==";
7922         case Token.OP_NE:
7923                 return "!=";
7924         case Token.OP_AND:
7925                 return "&&";
7926         case Token.OP_OR:
7927                 return "||";
7928         case Token.OP_PTR:
7929                 return "->";
7930         case Token.OP_COALESCING:       
7931                 return "??";
7932         case Token.OP_MULT_ASSIGN:
7933                 return "*=";
7934         case Token.OP_DIV_ASSIGN:
7935                 return "/=";
7936         case Token.OP_MOD_ASSIGN:
7937                 return "%=";
7938         case Token.OP_ADD_ASSIGN:
7939                 return "+=";
7940         case Token.OP_SUB_ASSIGN:
7941                 return "-=";
7942         case Token.OP_SHIFT_LEFT_ASSIGN:
7943                 return "<<=";
7944         case Token.OP_SHIFT_RIGHT_ASSIGN:
7945                 return ">>=";
7946         case Token.OP_AND_ASSIGN:
7947                 return "&=";
7948         case Token.OP_XOR_ASSIGN:
7949                 return "^=";
7950         case Token.OP_OR_ASSIGN:
7951                 return "|=";
7952         }
7953
7954         return GetTokenName (token);
7955 }
7956
7957 static string GetTokenName (int token)
7958 {
7959         switch (token){
7960         case Token.ABSTRACT:
7961                 return "abstract";
7962         case Token.AS:
7963                 return "as";
7964         case Token.ADD:
7965                 return "add";
7966         case Token.ASYNC:
7967                 return "async";
7968         case Token.BASE:
7969                 return "base";
7970         case Token.BREAK:
7971                 return "break";
7972         case Token.CASE:
7973                 return "case";
7974         case Token.CATCH:
7975                 return "catch";
7976         case Token.CHECKED:
7977                 return "checked";
7978         case Token.CLASS:
7979                 return "class";
7980         case Token.CONST:
7981                 return "const";
7982         case Token.CONTINUE:
7983                 return "continue";
7984         case Token.DEFAULT:
7985                 return "default";
7986         case Token.DELEGATE:
7987                 return "delegate";
7988         case Token.DO:
7989                 return "do";
7990         case Token.ELSE:
7991                 return "else";
7992         case Token.ENUM:
7993                 return "enum";
7994         case Token.EVENT:
7995                 return "event";
7996         case Token.EXPLICIT:
7997                 return "explicit";
7998         case Token.EXTERN:
7999         case Token.EXTERN_ALIAS:
8000                 return "extern";
8001         case Token.FALSE:
8002                 return "false";
8003         case Token.FINALLY:
8004                 return "finally";
8005         case Token.FIXED:
8006                 return "fixed";
8007         case Token.FOR:
8008                 return "for";
8009         case Token.FOREACH:
8010                 return "foreach";
8011         case Token.GOTO:
8012                 return "goto";
8013         case Token.IF:
8014                 return "if";
8015         case Token.IMPLICIT:
8016                 return "implicit";
8017         case Token.IN:
8018                 return "in";
8019         case Token.INTERFACE:
8020                 return "interface";
8021         case Token.INTERNAL:
8022                 return "internal";
8023         case Token.IS:
8024                 return "is";
8025         case Token.LOCK:
8026                 return "lock";
8027         case Token.NAMESPACE:
8028                 return "namespace";
8029         case Token.NEW:
8030                 return "new";
8031         case Token.NULL:
8032                 return "null";
8033         case Token.OPERATOR:
8034                 return "operator";
8035         case Token.OUT:
8036                 return "out";
8037         case Token.OVERRIDE:
8038                 return "override";
8039         case Token.PARAMS:
8040                 return "params";
8041         case Token.PRIVATE:
8042                 return "private";
8043         case Token.PROTECTED:
8044                 return "protected";
8045         case Token.PUBLIC:
8046                 return "public";
8047         case Token.READONLY:
8048                 return "readonly";
8049         case Token.REF:
8050                 return "ref";
8051         case Token.RETURN:
8052                 return "return";
8053         case Token.REMOVE:
8054                 return "remove";
8055         case Token.SEALED:
8056                 return "sealed";
8057         case Token.SIZEOF:
8058                 return "sizeof";
8059         case Token.STACKALLOC:
8060                 return "stackalloc";
8061         case Token.STATIC:
8062                 return "static";
8063         case Token.STRUCT:
8064                 return "struct";
8065         case Token.SWITCH:
8066                 return "switch";
8067         case Token.THIS:
8068                 return "this";
8069         case Token.THROW:
8070                 return "throw";
8071         case Token.TRUE:
8072                 return "true";
8073         case Token.TRY:
8074                 return "try";
8075         case Token.TYPEOF:
8076                 return "typeof";
8077         case Token.UNCHECKED:
8078                 return "unchecked";
8079         case Token.UNSAFE:
8080                 return "unsafe";
8081         case Token.USING:
8082                 return "using";
8083         case Token.VIRTUAL:
8084                 return "virtual";
8085         case Token.VOLATILE:
8086                 return "volatile";
8087         case Token.WHERE:
8088                 return "where";
8089         case Token.WHILE:
8090                 return "while";
8091         case Token.ARGLIST:
8092                 return "__arglist";
8093         case Token.REFVALUE:
8094                 return "__refvalue";
8095         case Token.REFTYPE:
8096                 return "__reftype";
8097         case Token.MAKEREF:
8098                 return "__makeref";
8099         case Token.PARTIAL:
8100                 return "partial";
8101         case Token.ARROW:
8102                 return "=>";
8103         case Token.FROM:
8104         case Token.FROM_FIRST:
8105                 return "from";
8106         case Token.JOIN:
8107                 return "join";
8108         case Token.ON:
8109                 return "on";
8110         case Token.EQUALS:
8111                 return "equals";
8112         case Token.SELECT:
8113                 return "select";
8114         case Token.GROUP:
8115                 return "group";
8116         case Token.BY:
8117                 return "by";
8118         case Token.LET:
8119                 return "let";
8120         case Token.ORDERBY:
8121                 return "orderby";
8122         case Token.ASCENDING:
8123                 return "ascending";
8124         case Token.DESCENDING:
8125                 return "descending";
8126         case Token.INTO:
8127                 return "into";
8128         case Token.GET:
8129                 return "get";
8130         case Token.SET:
8131                 return "set";
8132         case Token.OPEN_BRACE:
8133                 return "{";
8134         case Token.CLOSE_BRACE:
8135                 return "}";
8136         case Token.OPEN_BRACKET:
8137         case Token.OPEN_BRACKET_EXPR:
8138                 return "[";
8139         case Token.CLOSE_BRACKET:
8140                 return "]";
8141         case Token.OPEN_PARENS_CAST:
8142         case Token.OPEN_PARENS_LAMBDA:
8143         case Token.OPEN_PARENS:
8144                 return "(";
8145         case Token.CLOSE_PARENS:
8146                 return ")";
8147         case Token.DOT:
8148                 return ".";
8149         case Token.COMMA:
8150                 return ",";
8151         case Token.DEFAULT_COLON:
8152                 return "default:";
8153         case Token.COLON:
8154                 return ":";
8155         case Token.SEMICOLON:
8156                 return ";";
8157         case Token.TILDE:
8158                 return "~";
8159         case Token.WHEN:
8160                 return "when";
8161         case Token.INTERPOLATED_STRING_END:
8162                 return "}";
8163         case Token.INTERPOLATED_STRING:
8164                 return "${";
8165
8166         case Token.PLUS:
8167         case Token.UMINUS:
8168         case Token.MINUS:
8169         case Token.BANG:
8170         case Token.OP_LT:
8171         case Token.OP_GT:
8172         case Token.BITWISE_AND:
8173         case Token.BITWISE_OR:
8174         case Token.STAR:
8175         case Token.PERCENT:
8176         case Token.DIV:
8177         case Token.CARRET:
8178         case Token.OP_INC:
8179         case Token.OP_DEC:
8180         case Token.OP_SHIFT_LEFT:
8181         case Token.OP_SHIFT_RIGHT:
8182         case Token.OP_LE:
8183         case Token.OP_GE:
8184         case Token.OP_EQ:
8185         case Token.OP_NE:
8186         case Token.OP_AND:
8187         case Token.OP_OR:
8188         case Token.OP_PTR:
8189         case Token.OP_COALESCING:       
8190         case Token.OP_MULT_ASSIGN:
8191         case Token.OP_DIV_ASSIGN:
8192         case Token.OP_MOD_ASSIGN:
8193         case Token.OP_ADD_ASSIGN:
8194         case Token.OP_SUB_ASSIGN:
8195         case Token.OP_SHIFT_LEFT_ASSIGN:
8196         case Token.OP_SHIFT_RIGHT_ASSIGN:
8197         case Token.OP_AND_ASSIGN:
8198         case Token.OP_XOR_ASSIGN:
8199         case Token.OP_OR_ASSIGN:
8200         case Token.INTERR_OPERATOR:
8201                 return "<operator>";
8202
8203         case Token.BOOL:
8204         case Token.BYTE:
8205         case Token.CHAR:
8206         case Token.VOID:
8207         case Token.DECIMAL:
8208         case Token.DOUBLE:
8209         case Token.FLOAT:
8210         case Token.INT:
8211         case Token.LONG:
8212         case Token.SBYTE:
8213         case Token.SHORT:
8214         case Token.STRING:
8215         case Token.UINT:
8216         case Token.ULONG:
8217         case Token.USHORT:
8218         case Token.OBJECT:
8219                 return "type";
8220         
8221         case Token.ASSIGN:
8222                 return "=";
8223         case Token.OP_GENERICS_LT:
8224         case Token.GENERIC_DIMENSION:
8225                 return "<";
8226         case Token.OP_GENERICS_GT:
8227                 return ">";
8228         case Token.INTERR:
8229         case Token.INTERR_NULLABLE:
8230                 return "?";
8231         case Token.DOUBLE_COLON:
8232                 return "::";
8233         case Token.LITERAL:
8234                 return "value";
8235         case Token.IDENTIFIER:
8236         case Token.AWAIT:
8237                 return "identifier";
8238
8239         case Token.EOF:
8240                 return "end-of-file";
8241
8242                 // All of these are internal.
8243         case Token.NONE:
8244         case Token.ERROR:
8245         case Token.FIRST_KEYWORD:
8246         case Token.EVAL_COMPILATION_UNIT_PARSER:
8247         case Token.EVAL_USING_DECLARATIONS_UNIT_PARSER:
8248         case Token.EVAL_STATEMENT_PARSER:
8249         case Token.LAST_KEYWORD:
8250         case Token.GENERATE_COMPLETION:
8251         case Token.COMPLETE_COMPLETION:
8252                 return "<internal>";
8253
8254                 // A bit more robust.
8255         default:
8256                 return yyNames [token];
8257         }
8258 }
8259
8260 /* end end end */
8261 }