776fa34d7145d3b8068c93c3eeecaeac2c546c55
[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@gnu.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 Novell, Inc
13 //
14 // TODO:
15 //   (1) Figure out why error productions dont work.  `type-declaration' is a
16 //       great spot to put an `error' because you can reproduce it with this input:
17 //       "public X { }"
18 //
19 // Possible optimization:
20 //   Run memory profiler with parsing only, and consider dropping 
21 //   arraylists where not needed.   Some pieces can use linked lists.
22
23
24 using System.Text;
25 using System.IO;
26 using System;
27
28 namespace Mono.CSharp
29 {
30         using System.Collections;
31
32         /// <summary>
33         ///    The C# Parser
34         /// </summary>
35         public class CSharpParser
36         {
37                 [Flags]
38                 enum ParameterModifierType
39                 {
40                         Ref             = 1 << 1,
41                         Out             = 1 << 2,
42                         This    = 1 << 3,
43                         Params  = 1 << 4,
44                         Arglist = 1 << 5,
45                         DefaultValue = 1 << 6,
46                         
47                         All = Ref | Out | This | Params | Arglist | DefaultValue
48                 }
49         
50                 NamespaceEntry  current_namespace;
51                 TypeContainer   current_container;
52                 DeclSpace       current_class;
53         
54                 /// <summary>
55                 ///   Current block is used to add statements as we find
56                 ///   them.  
57                 /// </summary>
58                 Block      current_block;
59
60                 Delegate   current_delegate;
61                 
62                 GenericMethod current_generic_method;
63                 AnonymousMethodExpression current_anonymous_method;
64
65                 /// <summary>
66                 ///   This is used by the unary_expression code to resolve
67                 ///   a name against a parameter.  
68                 /// </summary>
69                 
70                 // FIXME: This is very ugly and it's very hard to reset it correctly
71                 // on all places, especially when some parameters are autogenerated.
72                 ParametersCompiled current_local_parameters;
73
74                 /// <summary>
75                 ///   Using during property parsing to describe the implicit
76                 ///   value parameter that is passed to the "set" and "get"accesor
77                 ///   methods (properties and indexers).
78                 /// </summary>
79                 FullNamedExpression implicit_value_parameter_type;
80                 ParametersCompiled indexer_parameters;
81
82                 /// <summary>
83                 ///   Hack to help create non-typed array initializer
84                 /// </summary>
85                 public static FullNamedExpression current_array_type;
86                 FullNamedExpression pushed_current_array_type;
87
88                 /// <summary>
89                 ///   Used to determine if we are parsing the get/set pair
90                 ///   of an indexer or a property
91                 /// </summmary>
92                 bool parsing_indexer;
93
94                 bool parsing_anonymous_method;
95
96                 ///
97                 /// An out-of-band stack.
98                 ///
99                 static Stack oob_stack;
100
101                 ///
102                 /// Switch stack.
103                 ///
104                 Stack switch_stack;
105
106                 ///
107                 /// Controls the verbosity of the errors produced by the parser
108                 ///
109                 static public int yacc_verbose_flag;
110
111                 /// 
112                 /// Used by the interactive shell, flags whether EOF was reached
113                 /// and an error was produced
114                 ///
115                 public bool UnexpectedEOF;
116
117                 ///
118                 /// The current file.
119                 ///
120                 CompilationUnit file;
121
122                 ///
123                 /// Temporary Xml documentation cache.
124                 /// For enum types, we need one more temporary store.
125                 ///
126                 string tmpComment;
127                 string enumTypeComment;
128                         
129                 /// Current attribute target
130                 string current_attr_target;
131                 
132                 /// assembly and module attribute definitions are enabled
133                 bool global_attrs_enabled = true;
134                 bool has_get, has_set;
135                 
136                 ParameterModifierType valid_param_mod;
137                 
138                 bool default_parameter_used;
139
140                 /// When using the interactive parser, this holds the
141                 /// resulting expression
142                 public object InteractiveResult;
143
144                 //
145                 // Keeps track of global data changes to undo on parser error
146                 //
147                 public Undo undo;
148                 
149                 // Stack<ToplevelBlock>
150                 Stack linq_clause_blocks;
151
152                 // A counter to create new class names in interactive mode
153                 static int class_count;
154                 
155                 CompilerContext compiler;
156                 
157                 //
158                 // Instead of allocating carrier array everytime we
159                 // share the bucket for very common constructs which can never
160                 // be recursive
161                 //
162                 static ArrayList parameters_bucket = new ArrayList (6);
163                 static ArrayList variables_bucket = new ArrayList (6);
164 %}
165
166 %token EOF
167 %token NONE   /* This token is never returned by our lexer */
168 %token ERROR            // This is used not by the parser, but by the tokenizer.
169                         // do not remove.
170
171 /*
172  *These are the C# keywords
173  */
174 %token FIRST_KEYWORD
175 %token ABSTRACT 
176 %token AS
177 %token ADD
178 %token BASE     
179 %token BOOL     
180 %token BREAK    
181 %token BYTE     
182 %token CASE     
183 %token CATCH    
184 %token CHAR     
185 %token CHECKED  
186 %token CLASS    
187 %token CONST    
188 %token CONTINUE 
189 %token DECIMAL  
190 %token DEFAULT  
191 %token DELEGATE 
192 %token DO       
193 %token DOUBLE   
194 %token ELSE     
195 %token ENUM     
196 %token EVENT    
197 %token EXPLICIT 
198 %token EXTERN   
199 %token FALSE    
200 %token FINALLY  
201 %token FIXED    
202 %token FLOAT    
203 %token FOR      
204 %token FOREACH  
205 %token GOTO     
206 %token IF       
207 %token IMPLICIT 
208 %token IN       
209 %token INT      
210 %token INTERFACE
211 %token INTERNAL 
212 %token IS       
213 %token LOCK     
214 %token LONG     
215 %token NAMESPACE
216 %token NEW      
217 %token NULL     
218 %token OBJECT   
219 %token OPERATOR 
220 %token OUT      
221 %token OVERRIDE 
222 %token PARAMS   
223 %token PRIVATE  
224 %token PROTECTED
225 %token PUBLIC   
226 %token READONLY 
227 %token REF      
228 %token RETURN   
229 %token REMOVE
230 %token SBYTE    
231 %token SEALED   
232 %token SHORT    
233 %token SIZEOF   
234 %token STACKALLOC
235 %token STATIC   
236 %token STRING   
237 %token STRUCT   
238 %token SWITCH   
239 %token THIS     
240 %token THROW    
241 %token TRUE     
242 %token TRY      
243 %token TYPEOF   
244 %token UINT     
245 %token ULONG    
246 %token UNCHECKED
247 %token UNSAFE   
248 %token USHORT   
249 %token USING    
250 %token VIRTUAL  
251 %token VOID     
252 %token VOLATILE
253 %token WHERE
254 %token WHILE    
255 %token ARGLIST
256 %token PARTIAL
257 %token ARROW
258 %token FROM
259 %token FROM_FIRST
260 %token JOIN
261 %token ON
262 %token EQUALS
263 %token SELECT
264 %token GROUP
265 %token BY
266 %token LET
267 %token ORDERBY
268 %token ASCENDING
269 %token DESCENDING
270 %token INTO
271 %token INTERR_NULLABLE
272 %token EXTERN_ALIAS
273
274 /* Generics <,> tokens */
275 %token OP_GENERICS_LT
276 %token OP_GENERICS_LT_DECL
277 %token OP_GENERICS_GT
278
279 /* C# keywords which are not really keywords */
280 %token GET
281 %token SET
282
283 %left LAST_KEYWORD
284
285 /* C# single character operators/punctuation. */
286 %token OPEN_BRACE
287 %token CLOSE_BRACE
288 %token OPEN_BRACKET
289 %token CLOSE_BRACKET
290 %token OPEN_PARENS
291 %token CLOSE_PARENS
292
293 %token DOT
294 %token COMMA
295 %token COLON
296 %token SEMICOLON
297 %token TILDE
298
299 %token PLUS
300 %token MINUS
301 %token BANG
302 %token ASSIGN
303 %token OP_LT
304 %token OP_GT
305 %token BITWISE_AND
306 %token BITWISE_OR
307 %token STAR
308 %token PERCENT
309 %token DIV
310 %token CARRET
311 %token INTERR
312
313 /* C# multi-character operators. */
314 %token DOUBLE_COLON
315 %token OP_INC
316 %token OP_DEC
317 %token OP_SHIFT_LEFT
318 %token OP_SHIFT_RIGHT
319 %token OP_LE
320 %token OP_GE
321 %token OP_EQ
322 %token OP_NE
323 %token OP_AND
324 %token OP_OR
325 %token OP_MULT_ASSIGN
326 %token OP_DIV_ASSIGN
327 %token OP_MOD_ASSIGN
328 %token OP_ADD_ASSIGN
329 %token OP_SUB_ASSIGN
330 %token OP_SHIFT_LEFT_ASSIGN
331 %token OP_SHIFT_RIGHT_ASSIGN
332 %token OP_AND_ASSIGN
333 %token OP_XOR_ASSIGN
334 %token OP_OR_ASSIGN
335 %token OP_PTR
336 %token OP_COALESCING
337
338 %token LITERAL
339
340 %token IDENTIFIER
341 %token OPEN_PARENS_LAMBDA
342 %token OPEN_PARENS_CAST
343 %token GENERIC_DIMENSION
344 %token DEFAULT_COLON
345
346 // Make the parser go into eval mode parsing (statements and compilation units).
347 %token EVAL_STATEMENT_PARSER
348 %token EVAL_COMPILATION_UNIT_PARSER
349 %token EVAL_USING_DECLARATIONS_UNIT_PARSER
350
351 // 
352 // This token is generated to trigger the completion engine at this point
353 //
354 %token GENERATE_COMPLETION
355
356 //
357 // This token is return repeatedly after the first GENERATE_COMPLETION
358 // token is produced and before the final EOF
359 //
360 %token COMPLETE_COMPLETION
361
362 /* Add precedence rules to solve dangling else s/r conflict */
363 %nonassoc IF
364 %nonassoc ELSE
365
366 /* Define the operator tokens and their precedences */
367 %right ASSIGN
368 %right OP_COALESCING
369 %right INTERR
370 %left OP_OR
371 %left OP_AND
372 %left BITWISE_OR
373 %left BITWISE_AND
374 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
375 %left PLUS MINUS
376 %left STAR DIV PERCENT
377 %right BANG CARRET UMINUS
378 %nonassoc OP_INC OP_DEC
379 %left OPEN_PARENS
380 %left OPEN_BRACKET OPEN_BRACE
381 %left DOT
382
383 %start compilation_unit
384 %%
385
386 compilation_unit
387         : outer_declarations opt_EOF
388         | outer_declarations global_attributes opt_EOF
389         | global_attributes opt_EOF
390         | opt_EOF /* allow empty files */
391         | interactive_parsing  { Lexer.CompleteOnEOF = false; } opt_EOF
392         ;
393
394 opt_EOF
395         : /* empty */
396           {
397                 Lexer.check_incorrect_doc_comment ();
398           }
399         | EOF
400           {
401                 Lexer.check_incorrect_doc_comment ();
402           }
403         ;
404
405 outer_declarations
406         : outer_declaration
407         | outer_declarations outer_declaration
408         ;
409  
410 outer_declaration
411         : extern_alias_directive
412         | using_directive 
413         | namespace_member_declaration
414         ;
415
416 extern_alias_directives
417         : extern_alias_directive
418         | extern_alias_directives extern_alias_directive
419         ;
420
421 extern_alias_directive
422         : EXTERN_ALIAS IDENTIFIER IDENTIFIER SEMICOLON
423           {
424                 var lt = (Tokenizer.LocatedToken) $2;
425                 string s = lt.Value;
426                 if (s != "alias"){
427                         syntax_error (lt.Location, "`alias' expected");
428                 } else if (RootContext.Version == LanguageVersion.ISO_1) {
429                         Report.FeatureIsNotAvailable (lt.Location, "external alias");
430                 } else {
431                         lt = (Tokenizer.LocatedToken) $3; 
432                         current_namespace.AddUsingExternalAlias (lt.Value, lt.Location, Report);
433                 }
434           }
435         | EXTERN_ALIAS error
436           {
437                 syntax_error (GetLocation ($1), "`alias' expected");   // TODO: better
438           }
439         ;
440  
441 using_directives
442         : using_directive 
443         | using_directives using_directive
444         ;
445
446 using_directive
447         : using_alias_directive
448           {
449                 if (RootContext.Documentation != null)
450                         Lexer.doc_state = XmlCommentState.Allowed;
451           }
452         | using_namespace_directive
453           {
454                 if (RootContext.Documentation != null)
455                         Lexer.doc_state = XmlCommentState.Allowed;
456           }
457         ;
458
459 using_alias_directive
460         : USING IDENTIFIER ASSIGN namespace_or_type_name SEMICOLON
461           {
462                 var lt = (Tokenizer.LocatedToken) $2;
463                 current_namespace.AddUsingAlias (lt.Value, (MemberName) $4, GetLocation ($1));
464           }
465         | USING error {
466                 CheckIdentifierToken (yyToken, GetLocation ($2));
467                 $$ = null;
468           }
469         ;
470
471 using_namespace_directive
472         : USING namespace_name SEMICOLON 
473           {
474                 current_namespace.AddUsing ((MemberName) $2, GetLocation ($1));
475           }
476         ;
477
478 //
479 // Strictly speaking, namespaces don't have attributes but
480 // we parse global attributes along with namespace declarations and then
481 // detach them
482 // 
483 namespace_declaration
484         : opt_attributes NAMESPACE qualified_identifier
485           {
486                 MemberName name = (MemberName) $3;
487
488                 if ($1 != null) {
489                         Report.Error(1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
490                 }
491
492                 current_namespace = new NamespaceEntry (
493                         current_namespace, file, name.GetName ());
494                 current_class = current_namespace.SlaveDeclSpace;
495                 current_container = current_class.PartialContainer;
496           } 
497           namespace_body opt_semicolon
498           { 
499                 current_namespace = current_namespace.Parent;
500                 current_class = current_namespace.SlaveDeclSpace;
501                 current_container = current_class.PartialContainer;
502           }
503         ;
504
505 qualified_identifier
506         : IDENTIFIER
507           {
508                 var lt = (Tokenizer.LocatedToken) $1;
509                 $$ = new MemberName (lt.Value, lt.Location);
510           }
511         | qualified_identifier DOT IDENTIFIER
512           {
513                 var lt = (Tokenizer.LocatedToken) $3;
514                 $$ = new MemberName ((MemberName) $1, lt.Value, lt.Location);           
515           }
516         | error
517           {
518                 syntax_error (lexer.Location, "`.' expected");
519                 $$ = new MemberName ("<invalid>", lexer.Location);
520           }
521         ;
522
523 opt_semicolon
524         : /* empty */
525         | SEMICOLON
526         ;
527
528 opt_comma
529         : /* empty */
530         | COMMA
531         ;
532
533 namespace_name
534         : namespace_or_type_name
535          {
536                 MemberName name = (MemberName) $1;
537
538                 if (name.TypeArguments != null)
539                         syntax_error (lexer.Location, "namespace name expected");
540
541                 $$ = name;
542           }
543         ;
544
545 namespace_body
546         : OPEN_BRACE
547           {
548                 if (RootContext.Documentation != null)
549                         Lexer.doc_state = XmlCommentState.Allowed;
550           }
551           namespace_body_body
552         ;
553         
554 namespace_body_body
555         : opt_extern_alias_directives
556           opt_using_directives
557           opt_namespace_member_declarations
558           CLOSE_BRACE
559         | error
560           {
561                 Report.Error (1518, lexer.Location, "Expected `class', `delegate', `enum', `interface', or `struct'");
562           }
563           CLOSE_BRACE
564         | opt_extern_alias_directives
565           opt_using_directives
566           opt_namespace_member_declarations
567           EOF
568           {
569                 Report.Error (1513, lexer.Location, "Expected `}'");
570           }
571         ;
572
573 opt_using_directives
574         : /* empty */
575         | using_directives
576         ;
577
578 opt_extern_alias_directives
579         : /* empty */
580         | extern_alias_directives
581         ;
582
583 opt_namespace_member_declarations
584         : /* empty */
585         | namespace_member_declarations
586         ;
587
588 namespace_member_declarations
589         : namespace_member_declaration
590         | namespace_member_declarations namespace_member_declaration
591         ;
592
593 namespace_member_declaration
594         : type_declaration
595           {
596                 if ($1 != null) {
597                         DeclSpace ds = (DeclSpace)$1;
598
599                         if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
600                                 Report.Error (1527, ds.Location, 
601                                 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
602                         }
603                 }
604                 current_namespace.DeclarationFound = true;
605           }
606         | namespace_declaration {
607                 current_namespace.DeclarationFound = true;
608           }
609
610         | field_declaration {
611                 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
612           }
613         | method_declaration {
614                 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
615           }
616         ;
617
618 type_declaration
619         : class_declaration             
620         | struct_declaration
621         | interface_declaration
622         | enum_declaration              
623         | delegate_declaration
624 //
625 // Enable this when we have handled all errors, because this acts as a generic fallback
626 //
627 //      | error {
628 //              Console.WriteLine ("Token=" + yyToken);
629 //              Report.Error (1518, GetLocation ($1), "Expected class, struct, interface, enum or delegate");
630 //        }
631         ;
632
633 //
634 // Attributes 17.2
635 //
636
637 global_attributes
638         : attribute_sections
639           {
640                 if ($1 != null) {
641                         Attributes attrs = (Attributes)$1;
642                         if (global_attrs_enabled) {
643                                 CodeGen.Assembly.AddAttributes (attrs.Attrs, current_namespace);
644                         } else {
645                                 foreach (Attribute a in attrs.Attrs) {
646                                         Report.Error (1730, a.Location, "Assembly and module attributes must precede all other elements except using clauses and extern alias declarations");
647                                 }
648                         }
649                 }
650                 $$ = $1;
651           }
652         ;
653
654 opt_attributes
655         : /* empty */ 
656           {
657                 global_attrs_enabled = false;
658                 $$ = null;
659       }
660         | attribute_sections
661           { 
662                 global_attrs_enabled = false;
663                 $$ = $1;
664           }
665     ;
666  
667
668 attribute_sections
669         : attribute_section
670           {
671                 if (current_attr_target != String.Empty) {
672                         ArrayList sect = (ArrayList) $1;
673
674                         if (global_attrs_enabled) {
675                                 if (current_attr_target == "module") {
676                                         current_container.Module.Compiled.AddAttributes (sect);
677                                         $$ = null;
678                                 } else if (current_attr_target != null && current_attr_target.Length > 0) {
679                                         CodeGen.Assembly.AddAttributes (sect, current_namespace);
680                                         $$ = null;
681                                 } else {
682                                         $$ = new Attributes (sect);
683                                 }
684                                 if ($$ == null) {
685                                         if (RootContext.Documentation != null) {
686                                                 Lexer.check_incorrect_doc_comment ();
687                                                 Lexer.doc_state =
688                                                         XmlCommentState.Allowed;
689                                         }
690                                 }
691                         } else {
692                                 $$ = new Attributes (sect);
693                         }               
694                 }
695                 else
696                         $$ = null;
697                 current_attr_target = null;
698           }
699         | attribute_sections attribute_section
700           {
701                 if (current_attr_target != String.Empty) {
702                         Attributes attrs = $1 as Attributes;
703                         ArrayList sect = (ArrayList) $2;
704
705                         if (global_attrs_enabled) {
706                                 if (current_attr_target == "module") {
707                                         current_container.Module.Compiled.AddAttributes (sect);
708                                         $$ = null;
709                                 } else if (current_attr_target == "assembly") {
710                                         CodeGen.Assembly.AddAttributes (sect, current_namespace);
711                                         $$ = null;
712                                 } else {
713                                         if (attrs == null)
714                                                 attrs = new Attributes (sect);
715                                         else
716                                                 attrs.AddAttributes (sect);                     
717                                 }
718                         } else {
719                                 if (attrs == null)
720                                         attrs = new Attributes (sect);
721                                 else
722                                         attrs.AddAttributes (sect);
723                         }               
724                         $$ = attrs;
725                 }
726                 else
727                         $$ = null;
728                 current_attr_target = null;
729           }
730         ;
731
732 attribute_section
733         : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
734           {
735                 $$ = $3;
736           }
737         | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
738           {
739                 $$ = $2;
740           }
741         ;
742  
743 attribute_target_specifier
744         : attribute_target COLON
745           {
746                 current_attr_target = (string)$1;
747                 $$ = $1;
748           }
749         ;
750
751 attribute_target
752         : IDENTIFIER
753           {
754                 var lt = (Tokenizer.LocatedToken) $1;
755                 $$ = CheckAttributeTarget (lt.Value, lt.Location);
756           }
757         | EVENT  { $$ = "event"; }
758         | RETURN { $$ = "return"; }
759         | error
760           {
761                 string name = GetTokenName (yyToken);
762                 $$ = CheckAttributeTarget (name, GetLocation ($1));
763           }
764         ;
765
766 attribute_list
767         : attribute
768           {
769                 $$ = new ArrayList (4) { $1 };
770           }
771         | attribute_list COMMA attribute
772           {
773                 ArrayList attrs = (ArrayList) $1;
774                 attrs.Add ($3);
775
776                 $$ = attrs;
777           }
778         ;
779
780 attribute
781         : attribute_name
782           {
783                 ++lexer.parsing_block;
784           }
785           opt_attribute_arguments
786           {
787                 --lexer.parsing_block;
788                 MemberName mname = (MemberName) $1;
789                 if (mname.IsGeneric) {
790                         Report.Error (404, lexer.Location,
791                                       "'<' unexpected: attributes cannot be generic");
792                 }
793
794                 Arguments [] arguments = (Arguments []) $3;
795                 ATypeNameExpression expr = mname.GetTypeExpression ();
796
797                 if (current_attr_target == String.Empty)
798                         $$ = null;
799                 else if (global_attrs_enabled && (current_attr_target == "assembly" || current_attr_target == "module"))
800                         // FIXME: supply "nameEscaped" parameter here.
801                         $$ = new GlobalAttribute (current_namespace, current_attr_target,
802                                                   expr, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
803                 else
804                         $$ = new Attribute (current_attr_target, expr, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
805           }
806         ;
807
808 attribute_name
809         : namespace_or_type_name  { /* reserved attribute name or identifier: 17.4 */ }
810         ;
811
812 opt_attribute_arguments
813         : /* empty */   { $$ = null; }
814         | OPEN_PARENS attribute_arguments CLOSE_PARENS
815           {
816                 $$ = $2;
817           }
818         ;
819
820
821 attribute_arguments
822         : /* empty */           { $$ = null; } 
823         | positional_or_named_argument
824           {
825                 Arguments a = new Arguments (4);
826                 a.Add ((Argument) $1);
827                 $$ = new Arguments [] { a, null };
828           }
829         | named_attribute_argument
830           {
831                 Arguments a = new Arguments (4);
832                 a.Add ((Argument) $1);  
833                 $$ = new Arguments [] { null, a };
834           }
835     | attribute_arguments COMMA positional_or_named_argument
836           {
837                 Arguments[] o = (Arguments[]) $1;
838                 if (o [1] != null) {
839                         Report.Error (1016, ((Argument) $3).Expr.Location, "Named attribute arguments must appear after the positional arguments");
840                         o [0] = new Arguments (4);
841                 }
842                 
843                 Arguments args = ((Arguments) o [0]);
844                 if (args.Count > 0 && !($3 is NamedArgument) && args [args.Count - 1] is NamedArgument)
845                         Error_NamedArgumentExpected ((NamedArgument) args [args.Count - 1]);
846                 
847                 args.Add ((Argument) $3);
848           }
849     | attribute_arguments COMMA named_attribute_argument
850           {
851                 Arguments[] o = (Arguments[]) $1;
852                 if (o [1] == null) {
853                         o [1] = new Arguments (4);
854                 }
855
856                 ((Arguments) o [1]).Add ((Argument) $3);
857           }
858     ;
859
860 positional_or_named_argument
861         : expression
862           {
863                 $$ = new Argument ((Expression) $1);
864           }
865         | named_argument
866         ;
867
868 named_attribute_argument
869         : IDENTIFIER ASSIGN expression
870           {
871                 var lt = (Tokenizer.LocatedToken) $1;
872                 $$ = new NamedArgument (lt.Value, lt.Location, (Expression) $3);          
873           }
874         ;
875         
876 named_argument
877         : IDENTIFIER COLON expression
878           {
879                 if (RootContext.Version <= LanguageVersion.V_3)
880                         Report.FeatureIsNotAvailable (GetLocation ($1), "named argument");
881                         
882                 var lt = (Tokenizer.LocatedToken) $1;
883                 $$ = new NamedArgument (lt.Value, lt.Location, (Expression) $3);
884           }       
885         ;       
886
887                   
888 class_body
889         :  OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
890         ;
891
892 opt_class_member_declarations
893         : /* empty */
894         | class_member_declarations
895         ;
896
897 class_member_declarations
898         : class_member_declaration
899         | class_member_declarations 
900           class_member_declaration
901         ;
902
903 class_member_declaration
904         : constant_declaration                  // done
905         | field_declaration                     // done
906         | method_declaration                    // done
907         | property_declaration                  // done
908         | event_declaration                     // done
909         | indexer_declaration                   // done
910         | operator_declaration                  // done
911         | constructor_declaration               // done
912         | destructor_declaration                // done
913         | type_declaration
914         | error
915           {
916                 Report.Error (1519, lexer.Location, "Unexpected symbol `{0}' in class, struct, or interface member declaration",
917                         GetSymbolName (yyToken));
918                 $$ = null;
919                 lexer.parsing_generic_declaration = false;
920           }
921         ;
922
923 struct_declaration
924         : opt_attributes
925           opt_modifiers
926           opt_partial
927           STRUCT
928           {
929                 lexer.ConstraintsParsing = true;
930           }
931           type_declaration_name
932           { 
933                 MemberName name = MakeName ((MemberName) $6);
934                 push_current_class (new Struct (current_namespace, current_class, name, (int) $2, (Attributes) $1), $3);
935           }
936           opt_class_base
937           opt_type_parameter_constraints_clauses
938           {
939                 lexer.ConstraintsParsing = false;
940
941                 current_class.SetParameterInfo ((ArrayList) $9);
942
943                 if (RootContext.Documentation != null)
944                         current_container.DocComment = Lexer.consume_doc_comment ();
945           }
946           struct_body
947           {
948                 --lexer.parsing_declaration;      
949                 if (RootContext.Documentation != null)
950                         Lexer.doc_state = XmlCommentState.Allowed;
951           }
952           opt_semicolon
953           {
954                 $$ = pop_current_class ();
955           }
956         | opt_attributes opt_modifiers opt_partial STRUCT error {
957                 CheckIdentifierToken (yyToken, GetLocation ($5));
958           }
959         ;
960
961 struct_body
962         : OPEN_BRACE
963           {
964                 if (RootContext.Documentation != null)
965                         Lexer.doc_state = XmlCommentState.Allowed;
966           }
967           opt_struct_member_declarations CLOSE_BRACE
968         ;
969
970 opt_struct_member_declarations
971         : /* empty */
972         | struct_member_declarations
973         ;
974
975 struct_member_declarations
976         : struct_member_declaration
977         | struct_member_declarations struct_member_declaration
978         ;
979
980 struct_member_declaration
981         : constant_declaration
982         | field_declaration
983         | method_declaration
984         | property_declaration
985         | event_declaration
986         | indexer_declaration
987         | operator_declaration
988         | constructor_declaration
989         | type_declaration
990
991         /*
992          * This is only included so we can flag error 575: 
993          * destructors only allowed on class types
994          */
995         | destructor_declaration 
996         ;
997
998 constant_declaration
999         : opt_attributes 
1000           opt_modifiers
1001           CONST
1002           type
1003           constant_declarators
1004           SEMICOLON
1005           {
1006                 int modflags = (int) $2;
1007                 foreach (VariableDeclaration constant in (ArrayList) $5){
1008                         Location l = constant.Location;
1009                         if ((modflags & Modifiers.STATIC) != 0) {
1010                                 Report.Error (504, l, "The constant `{0}' cannot be marked static", current_container.GetSignatureForError () + "." + (string) constant.identifier);
1011                                 continue;
1012                         }
1013
1014                         Const c = new Const (
1015                                 current_class, (FullNamedExpression) $4, (string) constant.identifier, 
1016                                 (Expression) constant.expression_or_array_initializer, modflags, 
1017                                 (Attributes) $1, l);
1018
1019                         if (RootContext.Documentation != null) {
1020                                 c.DocComment = Lexer.consume_doc_comment ();
1021                                 Lexer.doc_state = XmlCommentState.Allowed;
1022                         }
1023                         current_container.AddConstant (c);
1024                 }
1025           }
1026         ;
1027
1028 constant_declarators
1029         : constant_declarator 
1030           {
1031                 variables_bucket.Clear ();
1032                 if ($1 != null)
1033                         variables_bucket.Add ($1);
1034                 $$ = variables_bucket;
1035           }
1036         | constant_declarators COMMA constant_declarator
1037           {
1038                 if ($3 != null) {
1039                         ArrayList constants = (ArrayList) $1;
1040                         constants.Add ($3);
1041                 }
1042           }
1043         ;
1044
1045 constant_declarator
1046         : IDENTIFIER ASSIGN
1047           {
1048                 ++lexer.parsing_block;
1049           }     
1050           constant_initializer
1051           {
1052                 --lexer.parsing_block;
1053                 $$ = new VariableDeclaration ((Tokenizer.LocatedToken) $1, $4);
1054           }
1055         | IDENTIFIER
1056           {
1057                 // A const field requires a value to be provided
1058                 Report.Error (145, GetLocation ($1), "A const field requires a value to be provided");
1059                 $$ = null;
1060           }
1061         ;
1062         
1063 constant_initializer
1064         : constant_expression
1065         | array_initializer
1066         ;
1067
1068 field_declaration
1069         : opt_attributes
1070           opt_modifiers
1071           member_type
1072           variable_declarators
1073           SEMICOLON
1074           { 
1075                 FullNamedExpression type = (FullNamedExpression) $3;
1076                 if (type == TypeManager.system_void_expr)
1077                         Report.Error (670, GetLocation ($3), "Fields cannot have void type");
1078                 
1079                 int mod = (int) $2;
1080
1081                 current_array_type = null;
1082
1083                 foreach (VariableMemberDeclaration var in (ArrayList) $4){
1084                         Field field = new Field (current_class, type, mod, var.MemberName, (Attributes) $1);
1085
1086                         field.Initializer = var.expression_or_array_initializer;
1087
1088                         if (RootContext.Documentation != null) {
1089                                 field.DocComment = Lexer.consume_doc_comment ();
1090                                 Lexer.doc_state = XmlCommentState.Allowed;
1091                         }
1092                         current_container.AddField (field);
1093                         $$ = field; // FIXME: might be better if it points to the top item
1094                 }
1095           }
1096         | opt_attributes
1097           opt_modifiers
1098           FIXED
1099           member_type
1100           fixed_variable_declarators
1101           SEMICOLON
1102           { 
1103                         FullNamedExpression type = (FullNamedExpression) $4;
1104                         
1105                         int mod = (int) $2;
1106
1107                         current_array_type = null;
1108
1109                         foreach (VariableDeclaration var in (ArrayList) $5) {
1110                                 FixedField field = new FixedField (current_class, type, mod, var.identifier,
1111                                         (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
1112
1113                                 if (RootContext.Documentation != null) {
1114                                         field.DocComment = Lexer.consume_doc_comment ();
1115                                         Lexer.doc_state = XmlCommentState.Allowed;
1116                                 }
1117                                 current_container.AddField (field);
1118                                 $$ = field; // FIXME: might be better if it points to the top item
1119                         }
1120           }
1121         | opt_attributes
1122           opt_modifiers
1123           FIXED
1124           member_type
1125           error
1126           {
1127                 Report.Error (1641, GetLocation ($4), "A fixed size buffer field must have the array size specifier after the field name");
1128           }
1129         ;
1130
1131 fixed_variable_declarators
1132         : fixed_variable_declarator
1133           {
1134                 ArrayList decl = new ArrayList (2);
1135                 decl.Add ($1);
1136                 $$ = decl;
1137           }
1138         | fixed_variable_declarators COMMA fixed_variable_declarator
1139           {
1140                 ArrayList decls = (ArrayList) $1;
1141                 decls.Add ($3);
1142                 $$ = $1;
1143           }
1144         ;
1145
1146 fixed_variable_declarator
1147         : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
1148           {
1149                 $$ = new VariableDeclaration ((Tokenizer.LocatedToken) $1, $3);
1150           }
1151         | IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1152           {
1153                 Report.Error (443, lexer.Location, "Value or constant expected");
1154                 $$ = new VariableDeclaration ((Tokenizer.LocatedToken) $1, null);
1155           }
1156         ;
1157         
1158         
1159 local_variable_declarators      
1160         : local_variable_declarator 
1161           {
1162                 variables_bucket.Clear ();
1163                 if ($1 != null)
1164                         variables_bucket.Add ($1);
1165                 $$ = variables_bucket;
1166           }
1167         | local_variable_declarators COMMA local_variable_declarator
1168           {
1169                 ArrayList decls = (ArrayList) $1;
1170                 decls.Add ($3);
1171                 $$ = $1;
1172           }
1173         ;
1174         
1175 local_variable_declarator
1176         : IDENTIFIER ASSIGN local_variable_initializer
1177           {
1178                 $$ = new VariableDeclaration ((Tokenizer.LocatedToken) $1, $3);
1179           }
1180         | IDENTIFIER
1181           {
1182                 $$ = new VariableDeclaration ((Tokenizer.LocatedToken) $1, null);
1183           }
1184         | IDENTIFIER variable_bad_array
1185           {
1186                 $$ = null;
1187           }
1188         ;
1189
1190 local_variable_initializer
1191         : expression
1192         | array_initializer
1193         | STACKALLOC simple_type OPEN_BRACKET expression CLOSE_BRACKET
1194           {
1195                 $$ = new StackAlloc ((Expression) $2, (Expression) $4, GetLocation ($1));
1196           }
1197         | ARGLIST
1198           {
1199                 $$ = new ArglistAccess (GetLocation ($1));
1200           }
1201         | STACKALLOC simple_type
1202           {
1203                 Report.Error (1575, GetLocation ($1), "A stackalloc expression requires [] after type");
1204                 $$ = new StackAlloc ((Expression) $2, null, GetLocation ($1));          
1205           }
1206         ;
1207
1208 variable_declarators
1209         : variable_declarator 
1210           {
1211                 variables_bucket.Clear ();
1212                 if ($1 != null)
1213                         variables_bucket.Add ($1);
1214                 $$ = variables_bucket;
1215           }
1216         | variable_declarators COMMA variable_declarator
1217           {
1218                 ArrayList decls = (ArrayList) $1;
1219                 decls.Add ($3);
1220                 $$ = $1;
1221           }
1222         ;
1223
1224 variable_declarator
1225         : member_declaration_name ASSIGN
1226           {
1227                 ++lexer.parsing_block;
1228                 lexer.parsing_generic_declaration = false;
1229           }
1230           variable_initializer
1231           {
1232                 --lexer.parsing_block;
1233                 $$ = new VariableMemberDeclaration ((MemberName) $1, $4);
1234           }
1235         | member_declaration_name
1236           {
1237                 lexer.parsing_generic_declaration = false;
1238                 $$ = new VariableMemberDeclaration ((MemberName) $1, null);
1239           }
1240         | member_declaration_name variable_bad_array
1241           {
1242                 lexer.parsing_generic_declaration = false;        
1243                 $$ = null;
1244           }
1245         ;
1246         
1247 variable_bad_array
1248         : OPEN_BRACKET opt_expression CLOSE_BRACKET
1249           {
1250                 Report.Error (650, GetLocation ($1), "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
1251                         "To declare a fixed size buffer field, use the fixed keyword before the field type");
1252           }
1253         ;
1254
1255 variable_initializer
1256         : expression
1257         | array_initializer
1258         ;
1259
1260 method_declaration
1261         : method_header {
1262                 if (RootContext.Documentation != null)
1263                         Lexer.doc_state = XmlCommentState.NotAllowed;
1264           }
1265           method_body
1266           {
1267                 Method method = (Method) $1;
1268                 method.Block = (ToplevelBlock) $3;
1269                 current_container.AddMethod (method);
1270                 
1271                 if (current_container.Kind == Kind.Interface && method.Block != null) {
1272                         Report.Error (531, method.Location, "`{0}': interface members cannot have a definition", method.GetSignatureForError ());
1273                 }
1274
1275                 current_generic_method = null;
1276                 current_local_parameters = null;
1277
1278                 if (RootContext.Documentation != null)
1279                         Lexer.doc_state = XmlCommentState.Allowed;
1280           }
1281         ;
1282
1283 method_header
1284         : opt_attributes
1285           opt_modifiers
1286           member_type
1287           method_declaration_name OPEN_PARENS
1288           {
1289                 valid_param_mod = ParameterModifierType.All;
1290           }
1291           opt_formal_parameter_list CLOSE_PARENS
1292           {
1293                 lexer.ConstraintsParsing = true;
1294           }
1295           opt_type_parameter_constraints_clauses
1296           {
1297                 lexer.ConstraintsParsing = false;
1298                 valid_param_mod = 0;
1299                 MemberName name = (MemberName) $4;
1300                 current_local_parameters = (ParametersCompiled) $7;
1301
1302                 if ($10 != null && name.TypeArguments == null)
1303                         Report.Error (80, lexer.Location,
1304                                       "Constraints are not allowed on non-generic declarations");
1305
1306                 Method method;
1307
1308                 GenericMethod generic = null;
1309                 if (name.TypeArguments != null) {
1310                         generic = new GenericMethod (current_namespace, current_class, name,
1311                                                      (FullNamedExpression) $3, current_local_parameters);
1312
1313                         generic.SetParameterInfo ((ArrayList) $10);
1314                 }
1315
1316                 method = new Method (current_class, generic, (FullNamedExpression) $3, (int) $2,
1317                                      name, current_local_parameters, (Attributes) $1);
1318
1319                 current_generic_method = generic;
1320
1321                 if (RootContext.Documentation != null)
1322                         method.DocComment = Lexer.consume_doc_comment ();
1323
1324                 $$ = method;
1325           }
1326         | opt_attributes
1327           opt_modifiers
1328           PARTIAL
1329           VOID method_declaration_name
1330           OPEN_PARENS
1331           {
1332                 valid_param_mod = ParameterModifierType.All;
1333           }
1334           opt_formal_parameter_list CLOSE_PARENS 
1335           {
1336                 lexer.ConstraintsParsing = true;
1337           }
1338           opt_type_parameter_constraints_clauses
1339           {
1340                 lexer.ConstraintsParsing = false;
1341                 valid_param_mod = 0;
1342
1343                 MemberName name = (MemberName) $5;
1344                 current_local_parameters = (ParametersCompiled) $8;
1345
1346                 if ($10 != null && name.TypeArguments == null)
1347                         Report.Error (80, lexer.Location,
1348                                       "Constraints are not allowed on non-generic declarations");
1349
1350                 Method method;
1351                 GenericMethod generic = null;
1352                 if (name.TypeArguments != null) {
1353                         generic = new GenericMethod (current_namespace, current_class, name,
1354                                                      TypeManager.system_void_expr, current_local_parameters);
1355
1356                         generic.SetParameterInfo ((ArrayList) $11);
1357                 }
1358
1359                 int modifiers = (int) $2;
1360
1361
1362                 const int invalid_partial_mod = Modifiers.Accessibility | Modifiers.ABSTRACT | Modifiers.EXTERN |
1363                         Modifiers.NEW | Modifiers.OVERRIDE | Modifiers.SEALED | Modifiers.VIRTUAL;
1364
1365                 if ((modifiers & invalid_partial_mod) != 0) {
1366                         Report.Error (750, name.Location, "A partial method cannot define access modifier or " +
1367                         "any of abstract, extern, new, override, sealed, or virtual modifiers");
1368                         modifiers &= ~invalid_partial_mod;
1369                 }
1370
1371                 if ((current_class.ModFlags & Modifiers.PARTIAL) == 0) {
1372                         Report.Error (751, name.Location, "A partial method must be declared within a " +
1373                         "partial class or partial struct");
1374                 }
1375                 
1376                 modifiers |= Modifiers.PARTIAL | Modifiers.PRIVATE;
1377                 
1378                 method = new Method (current_class, generic, TypeManager.system_void_expr,
1379                                      modifiers, name, current_local_parameters, (Attributes) $1);
1380
1381                 current_generic_method = generic;
1382
1383                 if (RootContext.Documentation != null)
1384                         method.DocComment = Lexer.consume_doc_comment ();
1385
1386                 $$ = method;
1387           }
1388         | opt_attributes
1389           opt_modifiers
1390           member_type
1391           modifiers method_declaration_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1392           {
1393                 MemberName name = (MemberName) $5;
1394                 Report.Error (1585, name.Location, 
1395                         "Member modifier `{0}' must precede the member type and name", Modifiers.Name ((int) $4));
1396
1397                 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1398                                             0, name, (ParametersCompiled) $7, (Attributes) $1);
1399
1400                 current_local_parameters = (ParametersCompiled) $7;
1401
1402                 if (RootContext.Documentation != null)
1403                         method.DocComment = Lexer.consume_doc_comment ();
1404
1405                 $$ = method;
1406           }
1407         ;
1408
1409 method_body
1410         : block
1411         | SEMICOLON             { $$ = null; }
1412         ;
1413
1414 opt_formal_parameter_list
1415         : /* empty */                   { $$ = ParametersCompiled.EmptyReadOnlyParameters; }
1416         | formal_parameter_list
1417         ;
1418         
1419 formal_parameter_list
1420         : fixed_parameters
1421           { 
1422                 ArrayList pars_list = (ArrayList) $1;
1423
1424                 Parameter [] pars = new Parameter [pars_list.Count];
1425                 pars_list.CopyTo (pars);
1426
1427                 $$ = new ParametersCompiled (compiler, pars); 
1428           } 
1429         | fixed_parameters COMMA parameter_array
1430           {
1431                 ArrayList pars_list = (ArrayList) $1;
1432                 pars_list.Add ($3);
1433
1434                 Parameter [] pars = new Parameter [pars_list.Count];
1435                 pars_list.CopyTo (pars);
1436
1437                 $$ = new ParametersCompiled (compiler, pars); 
1438           }
1439         | fixed_parameters COMMA arglist_modifier
1440           {
1441                 ArrayList pars_list = (ArrayList) $1;
1442                 pars_list.Add (new ArglistParameter (GetLocation ($3)));
1443
1444                 Parameter [] pars = new Parameter [pars_list.Count];
1445                 pars_list.CopyTo (pars);
1446
1447                 $$ = new ParametersCompiled (compiler, pars, true);
1448           }
1449         | parameter_array COMMA error
1450           {
1451                 if ($1 != null)
1452                         Report.Error (231, ((Parameter) $1).Location, "A params parameter must be the last parameter in a formal parameter list");
1453
1454                 $$ = new ParametersCompiled (compiler, new Parameter[] { (Parameter) $1 } );                    
1455           }
1456         | fixed_parameters COMMA parameter_array COMMA error
1457           {
1458                 if ($3 != null)
1459                         Report.Error (231, ((Parameter) $3).Location, "A params parameter must be the last parameter in a formal parameter list");
1460                         
1461                 ArrayList pars_list = (ArrayList) $1;
1462                 pars_list.Add (new ArglistParameter (GetLocation ($3)));
1463
1464                 Parameter [] pars = new Parameter [pars_list.Count];
1465                 pars_list.CopyTo (pars);
1466
1467                 $$ = new ParametersCompiled (compiler, pars, true);
1468           }
1469         | arglist_modifier COMMA error
1470           {
1471                 Report.Error (257, GetLocation ($1), "An __arglist parameter must be the last parameter in a formal parameter list");
1472
1473                 $$ = new ParametersCompiled (compiler, new Parameter [] { new ArglistParameter (GetLocation ($1)) }, true);
1474           }
1475         | fixed_parameters COMMA ARGLIST COMMA error 
1476           {
1477                 Report.Error (257, GetLocation ($3), "An __arglist parameter must be the last parameter in a formal parameter list");
1478                 
1479                 ArrayList pars_list = (ArrayList) $1;
1480                 pars_list.Add (new ArglistParameter (GetLocation ($3)));
1481
1482                 Parameter [] pars = new Parameter [pars_list.Count];
1483                 pars_list.CopyTo (pars);
1484
1485                 $$ = new ParametersCompiled (compiler, pars, true);
1486           }
1487         | parameter_array 
1488           {
1489                 $$ = new ParametersCompiled (compiler, new Parameter[] { (Parameter) $1 } );
1490           }
1491         | arglist_modifier
1492           {
1493                 $$ = new ParametersCompiled (compiler, new Parameter [] { new ArglistParameter (GetLocation ($1)) }, true);
1494           }
1495         ;
1496
1497 fixed_parameters
1498         : fixed_parameter       
1499           {
1500                 parameters_bucket.Clear ();
1501                 Parameter p = (Parameter) $1;
1502                 parameters_bucket.Add (p);
1503                 
1504                 default_parameter_used = p.HasDefaultValue;
1505                 $$ = parameters_bucket;
1506           }
1507         | fixed_parameters COMMA fixed_parameter
1508           {
1509                 ArrayList pars = (ArrayList) $1;
1510                 Parameter p = (Parameter) $3;
1511                 if (p != null) {
1512                         if (p.HasExtensionMethodModifier)
1513                                 Report.Error (1100, p.Location, "The parameter modifier `this' can only be used on the first parameter");
1514                         else if (!p.HasDefaultValue && default_parameter_used)
1515                                 Report.Error (1737, p.Location, "Optional parameter cannot precede required parameters");
1516
1517                         default_parameter_used |= p.HasDefaultValue;
1518                         pars.Add (p);
1519                 }
1520                 $$ = $1;
1521           }
1522         ;
1523
1524 fixed_parameter
1525         : opt_attributes
1526           opt_parameter_modifier
1527           parameter_type
1528           IDENTIFIER
1529           {
1530                 var lt = (Tokenizer.LocatedToken) $4;
1531                 $$ = new Parameter ((FullNamedExpression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1532           }
1533         | opt_attributes
1534           opt_parameter_modifier
1535           parameter_type
1536           IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1537           {
1538                 var lt = (Tokenizer.LocatedToken) $4;
1539                 Report.Error (1552, lt.Location, "Array type specifier, [], must appear before parameter name");
1540                 $$ = new Parameter ((FullNamedExpression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1541           }
1542         | opt_attributes
1543           opt_parameter_modifier
1544           parameter_type
1545           error
1546           {
1547                 Location l = GetLocation ($4);
1548                 CheckIdentifierToken (yyToken, l);
1549                 $$ = new Parameter ((FullNamedExpression) $3, "NeedSomeGeneratorHere", (Parameter.Modifier) $2, (Attributes) $1, l);
1550           }
1551         | opt_attributes
1552           opt_parameter_modifier
1553           parameter_type
1554           IDENTIFIER
1555           ASSIGN
1556           constant_expression
1557           {
1558                 if (RootContext.Version <= LanguageVersion.V_3) {
1559                         Report.FeatureIsNotAvailable (GetLocation ($5), "optional parameter");
1560                 }
1561                 
1562                 Parameter.Modifier mod = (Parameter.Modifier) $2;
1563                 if (mod != Parameter.Modifier.NONE) {
1564                         switch (mod) {
1565                         case Parameter.Modifier.REF:
1566                         case Parameter.Modifier.OUT:
1567                                 Report.Error (1741, GetLocation ($2), "Cannot specify a default value for the `{0}' parameter",
1568                                         Parameter.GetModifierSignature (mod));
1569                                 break;
1570                                 
1571                         case Parameter.Modifier.This:
1572                                 Report.Error (1743, GetLocation ($2), "Cannot specify a default value for the `{0}' parameter",
1573                                         Parameter.GetModifierSignature (mod));
1574                                 break;
1575                         default:
1576                                 throw new NotImplementedException (mod.ToString ());
1577                         }
1578                                 
1579                         mod = Parameter.Modifier.NONE;
1580                 }
1581                 
1582                 if ((valid_param_mod & ParameterModifierType.DefaultValue) == 0)
1583                         Report.Error (1065, GetLocation ($6), "Optional parameter is not valid in this context");
1584                 
1585                 var lt = (Tokenizer.LocatedToken) $4;
1586                 $$ = new Parameter ((FullNamedExpression) $3, lt.Value, mod, (Attributes) $1, lt.Location);
1587                 if ($6 != null)
1588                         ((Parameter) $$).DefaultValue = (Expression) $6;
1589           }
1590         ;
1591
1592 opt_parameter_modifier
1593         : /* empty */           { $$ = Parameter.Modifier.NONE; }
1594         | parameter_modifiers
1595         ;
1596
1597 parameter_modifiers
1598         : parameter_modifier
1599           {
1600                 $$ = $1;
1601           }
1602         | parameter_modifiers parameter_modifier
1603           {
1604                 Parameter.Modifier p2 = (Parameter.Modifier)$2;
1605                 Parameter.Modifier mod = (Parameter.Modifier)$1 | p2;
1606                 if (((Parameter.Modifier)$1 & p2) == p2) {
1607                         Error_DuplicateParameterModifier (lexer.Location, p2);
1608                 } else {
1609                         switch (mod & ~Parameter.Modifier.This) {
1610                                 case Parameter.Modifier.REF:
1611                                         Report.Error (1101, lexer.Location, "The parameter modifiers `this' and `ref' cannot be used altogether");
1612                                         break;
1613                                 case Parameter.Modifier.OUT:
1614                                         Report.Error (1102, lexer.Location, "The parameter modifiers `this' and `out' cannot be used altogether");
1615                                         break;
1616                                 default:
1617                                         Report.Error (1108, lexer.Location, "A parameter cannot have specified more than one modifier");
1618                                         break;
1619                         }
1620                 }
1621                 $$ = mod;
1622           }
1623         ;
1624
1625 parameter_modifier
1626         : REF
1627           {
1628                 if ((valid_param_mod & ParameterModifierType.Ref) == 0)
1629                         Error_ParameterModifierNotValid ("ref", GetLocation ($1));
1630                         
1631                 $$ = Parameter.Modifier.REF;
1632           }
1633         | OUT
1634           {
1635                 if ((valid_param_mod & ParameterModifierType.Out) == 0)
1636                         Error_ParameterModifierNotValid ("out", GetLocation ($1));
1637           
1638                 $$ = Parameter.Modifier.OUT;
1639           }
1640         | THIS
1641           {
1642                 if ((valid_param_mod & ParameterModifierType.This) == 0)
1643                         Error_ParameterModifierNotValid ("this", GetLocation ($1));
1644
1645                 if (RootContext.Version <= LanguageVersion.ISO_2)
1646                         Report.FeatureIsNotAvailable (GetLocation ($1), "extension methods");
1647                                 
1648                 $$ = Parameter.Modifier.This;
1649           }
1650         ;
1651
1652 parameter_array
1653         : opt_attributes params_modifier type IDENTIFIER
1654           {
1655                 var lt = (Tokenizer.LocatedToken) $4;
1656                 $$ = new ParamsParameter ((FullNamedExpression) $3, lt.Value, (Attributes) $1, lt.Location);
1657           }
1658         | opt_attributes params_modifier type IDENTIFIER ASSIGN constant_expression
1659           {
1660                 Report.Error (1751, GetLocation ($2), "Cannot specify a default value for a parameter array");
1661                 
1662                 var lt = (Tokenizer.LocatedToken) $4;
1663                 $$ = new ParamsParameter ((FullNamedExpression) $3, lt.Value, (Attributes) $1, lt.Location);            
1664           }
1665         | opt_attributes params_modifier type error {
1666                 CheckIdentifierToken (yyToken, GetLocation ($4));
1667                 $$ = null;
1668           }
1669         ;
1670         
1671 params_modifier
1672         : PARAMS
1673           {
1674                 if ((valid_param_mod & ParameterModifierType.Params) == 0)
1675                         Report.Error (1670, (GetLocation ($1)), "The `params' modifier is not allowed in current context");
1676           }
1677         | PARAMS parameter_modifier
1678           {
1679                 Parameter.Modifier mod = (Parameter.Modifier)$2;
1680                 if ((mod & Parameter.Modifier.This) != 0) {
1681                         Report.Error (1104, GetLocation ($1), "The parameter modifiers `this' and `params' cannot be used altogether");
1682                 } else {
1683                         Report.Error (1611, GetLocation ($1), "The params parameter cannot be declared as ref or out");
1684                 }         
1685           }
1686         | PARAMS params_modifier
1687           {
1688                 Error_DuplicateParameterModifier (GetLocation ($1), Parameter.Modifier.PARAMS);
1689           }
1690         ;
1691         
1692 arglist_modifier
1693         : ARGLIST
1694           {
1695                 if ((valid_param_mod & ParameterModifierType.Arglist) == 0)
1696                         Report.Error (1669, GetLocation ($1), "__arglist is not valid in this context");
1697           }
1698         ;
1699         
1700 property_declaration
1701         : opt_attributes
1702           opt_modifiers
1703           member_type
1704           member_declaration_name
1705           {
1706                 if (RootContext.Documentation != null)
1707                         tmpComment = Lexer.consume_doc_comment ();
1708           }
1709           OPEN_BRACE 
1710           {
1711                 implicit_value_parameter_type = (FullNamedExpression) $3;
1712                 lexer.PropertyParsing = true;
1713           }
1714           accessor_declarations 
1715           {
1716                 lexer.PropertyParsing = false;
1717                 has_get = has_set = false;
1718           }
1719           CLOSE_BRACE
1720           { 
1721                 Property prop;
1722                 Accessors accessors = (Accessors) $8;
1723                 Accessor get_block = accessors != null ? accessors.get_or_add : null;
1724                 Accessor set_block = accessors != null ? accessors.set_or_remove : null;
1725                 bool order = accessors != null ? accessors.declared_in_reverse : false;
1726
1727                 MemberName name = (MemberName) $4;
1728                 FullNamedExpression ptype = (FullNamedExpression) $3;
1729
1730                 prop = new Property (current_class, ptype, (int) $2,
1731                                      name, (Attributes) $1, get_block, set_block, order, current_block);
1732
1733                 if (ptype == TypeManager.system_void_expr)
1734                         Report.Error (547, name.Location, "`{0}': property or indexer cannot have void type", prop.GetSignatureForError ());
1735                         
1736                 if (accessors == null)
1737                         Report.Error (548, prop.Location, "`{0}': property or indexer must have at least one accessor", prop.GetSignatureForError ());
1738
1739                 if (current_container.Kind == Kind.Interface) {
1740                         if (prop.Get.Block != null)
1741                                 Report.Error (531, prop.Location, "`{0}.get': interface members cannot have a definition", prop.GetSignatureForError ());
1742
1743                         if (prop.Set.Block != null)
1744                                 Report.Error (531, prop.Location, "`{0}.set': interface members cannot have a definition", prop.GetSignatureForError ());
1745                 }
1746
1747                 current_container.AddProperty (prop);
1748                 implicit_value_parameter_type = null;
1749
1750                 if (RootContext.Documentation != null)
1751                         prop.DocComment = ConsumeStoredComment ();
1752
1753           }
1754         ;
1755
1756 accessor_declarations
1757         : get_accessor_declaration
1758          {
1759                 $$ = new Accessors ((Accessor) $1, null);
1760          }
1761         | get_accessor_declaration accessor_declarations
1762          { 
1763                 Accessors accessors = (Accessors) $2;
1764                 accessors.get_or_add = (Accessor) $1;
1765                 $$ = accessors;
1766          }
1767         | set_accessor_declaration
1768          {
1769                 $$ = new Accessors (null, (Accessor) $1);
1770          }
1771         | set_accessor_declaration accessor_declarations
1772          { 
1773                 Accessors accessors = (Accessors) $2;
1774                 accessors.set_or_remove = (Accessor) $1;
1775                 accessors.declared_in_reverse = true;
1776                 $$ = accessors;
1777          }
1778         | error
1779           {
1780                 if (yyToken == Token.CLOSE_BRACE) {
1781                         $$ = null;
1782                 } else {
1783                         if (yyToken == Token.SEMICOLON)
1784                                 Report.Error (1597, lexer.Location, "Semicolon after method or accessor block is not valid");
1785                         else
1786                                 Report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1787
1788                         $$ = new Accessors (null, null);
1789                 }
1790           }
1791         ;
1792
1793 get_accessor_declaration
1794         : opt_attributes opt_modifiers GET
1795           {
1796                 // If this is not the case, then current_local_parameters has already
1797                 // been set in indexer_declaration
1798                 if (parsing_indexer == false)
1799                         current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters;
1800                 else 
1801                         current_local_parameters = indexer_parameters;
1802                 lexer.PropertyParsing = false;
1803           }
1804           accessor_body
1805           {
1806                 if (has_get) {
1807                         Report.Error (1007, GetLocation ($3), "Property accessor already defined");
1808                         break;
1809                 }
1810                 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, current_local_parameters, GetLocation ($3));
1811                 has_get = true;
1812                 current_local_parameters = null;
1813                 lexer.PropertyParsing = true;
1814
1815                 if (RootContext.Documentation != null)
1816                         if (Lexer.doc_state == XmlCommentState.Error)
1817                                 Lexer.doc_state = XmlCommentState.NotAllowed;
1818
1819                 $$ = accessor;
1820           }
1821         ;
1822
1823 set_accessor_declaration
1824         : opt_attributes opt_modifiers SET 
1825           {
1826                 Parameter implicit_value_parameter = new Parameter (
1827                         implicit_value_parameter_type, "value", 
1828                         Parameter.Modifier.NONE, null, GetLocation ($3));
1829
1830                 if (!parsing_indexer) {
1831                         current_local_parameters = new ParametersCompiled (compiler, new Parameter [] { implicit_value_parameter });
1832                 } else {
1833                         current_local_parameters = ParametersCompiled.MergeGenerated (compiler,
1834                                 indexer_parameters, true, implicit_value_parameter, null);
1835                 }
1836                 
1837                 lexer.PropertyParsing = false;
1838           }
1839           accessor_body
1840           {
1841                 if (has_set) {
1842                         Report.Error (1007, GetLocation ($3), "Property accessor already defined");
1843                         break;
1844                 }
1845                 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, current_local_parameters, GetLocation ($3));
1846                 has_set = true;
1847                 current_local_parameters = null;
1848                 lexer.PropertyParsing = true;
1849
1850                 if (RootContext.Documentation != null
1851                         && Lexer.doc_state == XmlCommentState.Error)
1852                         Lexer.doc_state = XmlCommentState.NotAllowed;
1853
1854                 $$ = accessor;
1855           }
1856         ;
1857
1858 accessor_body
1859         : block 
1860         | SEMICOLON
1861           {
1862                 $$ = null;
1863           }
1864         | error
1865           {
1866                 Error_SyntaxError (1043, yyToken, "Invalid accessor body");
1867                 $$ = null;
1868           }
1869         ;
1870
1871 interface_declaration
1872         : opt_attributes
1873           opt_modifiers
1874           opt_partial
1875           INTERFACE
1876           {
1877                 lexer.ConstraintsParsing = true;
1878           }
1879           type_declaration_name
1880           {
1881                 MemberName name = MakeName ((MemberName) $6);
1882                 push_current_class (new Interface (current_namespace, current_class, name, (int) $2, (Attributes) $1), $3);
1883           }
1884           opt_class_base
1885           opt_type_parameter_constraints_clauses
1886           {
1887                 lexer.ConstraintsParsing = false;
1888
1889                 current_class.SetParameterInfo ((ArrayList) $9);
1890
1891                 if (RootContext.Documentation != null) {
1892                         current_container.DocComment = Lexer.consume_doc_comment ();
1893                         Lexer.doc_state = XmlCommentState.Allowed;
1894                 }
1895           }
1896           interface_body
1897           {
1898                 --lexer.parsing_declaration;      
1899                 if (RootContext.Documentation != null)
1900                         Lexer.doc_state = XmlCommentState.Allowed;
1901           }
1902           opt_semicolon 
1903           {
1904                 $$ = pop_current_class ();
1905           }
1906         | opt_attributes opt_modifiers opt_partial INTERFACE error {
1907                 CheckIdentifierToken (yyToken, GetLocation ($5));
1908           }
1909         ;
1910
1911 interface_body
1912         : OPEN_BRACE
1913           opt_interface_member_declarations
1914           CLOSE_BRACE
1915         ;
1916
1917 opt_interface_member_declarations
1918         : /* empty */
1919         | interface_member_declarations
1920         ;
1921
1922 interface_member_declarations
1923         : interface_member_declaration
1924         | interface_member_declarations interface_member_declaration
1925         ;
1926
1927 interface_member_declaration
1928         : constant_declaration
1929           {
1930                 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1931           }
1932         | field_declaration
1933           {
1934                 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1935           }
1936         | method_declaration
1937         | property_declaration
1938         | event_declaration
1939         | indexer_declaration
1940         | operator_declaration
1941           {
1942                 Report.Error (567, GetLocation ($1), "Interfaces cannot contain operators");
1943           }
1944         | constructor_declaration
1945           {
1946                 Report.Error (526, GetLocation ($1), "Interfaces cannot contain contructors");
1947           }
1948         | type_declaration
1949           {
1950                 Report.Error (524, GetLocation ($1), "Interfaces cannot declare classes, structs, interfaces, delegates, or enumerations");
1951           }
1952         ;
1953
1954 operator_declaration
1955         : opt_attributes opt_modifiers operator_declarator 
1956           {
1957           }
1958           operator_body
1959           {
1960                 if ($3 == null)
1961                         break;
1962
1963                 OperatorDeclaration decl = (OperatorDeclaration) $3;
1964                 Operator op = new Operator (
1965                         current_class, decl.optype, decl.ret_type, (int) $2, 
1966                         current_local_parameters,
1967                         (ToplevelBlock) $5, (Attributes) $1, decl.location);
1968
1969                 if (RootContext.Documentation != null) {
1970                         op.DocComment = tmpComment;
1971                         Lexer.doc_state = XmlCommentState.Allowed;
1972                 }
1973
1974                 // Note again, checking is done in semantic analysis
1975                 current_container.AddOperator (op);
1976
1977                 current_local_parameters = null;
1978           }
1979         ;
1980
1981 operator_body 
1982         : block
1983         | SEMICOLON { $$ = null; }
1984         ; 
1985
1986 operator_type
1987         : type_expression_or_array
1988         | VOID
1989           {
1990                 Report.Error (590, GetLocation ($1), "User-defined operators cannot return void");
1991                 $$ = TypeManager.system_void_expr;              
1992           }
1993         ;
1994
1995 operator_declarator
1996         : operator_type OPERATOR overloadable_operator OPEN_PARENS
1997           {
1998                 valid_param_mod = ParameterModifierType.DefaultValue;
1999           }
2000           opt_formal_parameter_list CLOSE_PARENS
2001           {
2002                 valid_param_mod = 0;
2003
2004                 Location loc = GetLocation ($2);
2005                 Operator.OpType op = (Operator.OpType) $3;
2006                 current_local_parameters = (ParametersCompiled)$6;
2007                 
2008                 int p_count = current_local_parameters.Count;
2009                 if (p_count == 1) {
2010                         if (op == Operator.OpType.Addition)
2011                                 op = Operator.OpType.UnaryPlus;
2012                         else if (op == Operator.OpType.Subtraction)
2013                                 op = Operator.OpType.UnaryNegation;
2014                 }
2015                 
2016                 if (IsUnaryOperator (op)) {
2017                         if (p_count == 2) {
2018                                 Report.Error (1020, loc, "Overloadable binary operator expected");
2019                         } else if (p_count != 1) {
2020                                 Report.Error (1535, loc, "Overloaded unary operator `{0}' takes one parameter",
2021                                         Operator.GetName (op));
2022                         }
2023                 } else {
2024                         if (p_count > 2) {
2025                                 Report.Error (1534, loc, "Overloaded binary operator `{0}' takes two parameters",
2026                                         Operator.GetName (op));
2027                         } else if (p_count != 2) {
2028                                 Report.Error (1019, loc, "Overloadable unary operator expected");
2029                         }
2030                 }
2031                 
2032                 if (RootContext.Documentation != null) {
2033                         tmpComment = Lexer.consume_doc_comment ();
2034                         Lexer.doc_state = XmlCommentState.NotAllowed;
2035                 }
2036
2037                 $$ = new OperatorDeclaration (op, (FullNamedExpression) $1, loc);
2038           }
2039         | conversion_operator_declarator
2040         ;
2041
2042 overloadable_operator
2043 // Unary operators:
2044         : BANG   { $$ = Operator.OpType.LogicalNot; }
2045         | TILDE  { $$ = Operator.OpType.OnesComplement; }  
2046         | OP_INC { $$ = Operator.OpType.Increment; }
2047         | OP_DEC { $$ = Operator.OpType.Decrement; }
2048         | TRUE   { $$ = Operator.OpType.True; }
2049         | FALSE  { $$ = Operator.OpType.False; }
2050 // Unary and binary:
2051         | PLUS { $$ = Operator.OpType.Addition; }
2052         | MINUS { $$ = Operator.OpType.Subtraction; }
2053 // Binary:
2054         | STAR { $$ = Operator.OpType.Multiply; }
2055         | DIV {  $$ = Operator.OpType.Division; }
2056         | PERCENT { $$ = Operator.OpType.Modulus; }
2057         | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
2058         | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
2059         | CARRET { $$ = Operator.OpType.ExclusiveOr; }
2060         | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
2061         | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
2062         | OP_EQ { $$ = Operator.OpType.Equality; }
2063         | OP_NE { $$ = Operator.OpType.Inequality; }
2064         | OP_GT { $$ = Operator.OpType.GreaterThan; }
2065         | OP_LT { $$ = Operator.OpType.LessThan; }
2066         | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
2067         | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
2068         ;
2069
2070 conversion_operator_declarator
2071         : IMPLICIT OPERATOR type OPEN_PARENS
2072           {
2073                 valid_param_mod = ParameterModifierType.DefaultValue;
2074           }
2075           opt_formal_parameter_list CLOSE_PARENS
2076           {
2077                 valid_param_mod = 0;
2078
2079                 Location loc = GetLocation ($2);
2080                 current_local_parameters = (ParametersCompiled)$6;  
2081                   
2082                 if (RootContext.Documentation != null) {
2083                         tmpComment = Lexer.consume_doc_comment ();
2084                         Lexer.doc_state = XmlCommentState.NotAllowed;
2085                 }
2086
2087                 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (FullNamedExpression) $3, loc);
2088           }
2089         | EXPLICIT OPERATOR type OPEN_PARENS
2090           {
2091                 valid_param_mod = ParameterModifierType.DefaultValue;
2092           }
2093           opt_formal_parameter_list CLOSE_PARENS
2094           {
2095                 valid_param_mod = 0;
2096                 
2097                 Location loc = GetLocation ($2);
2098                 current_local_parameters = (ParametersCompiled)$6;  
2099                   
2100                 if (RootContext.Documentation != null) {
2101                         tmpComment = Lexer.consume_doc_comment ();
2102                         Lexer.doc_state = XmlCommentState.NotAllowed;
2103                 }
2104
2105                 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (FullNamedExpression) $3, loc);
2106           }
2107         | IMPLICIT error 
2108           {
2109                 Error_SyntaxError (yyToken);
2110                 current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters;
2111                 $$ = new OperatorDeclaration (Operator.OpType.Implicit, null, GetLocation ($1));
2112           }
2113         | EXPLICIT error 
2114           {
2115                 Error_SyntaxError (yyToken);
2116                 current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters;
2117                 $$ = new OperatorDeclaration (Operator.OpType.Explicit, null, GetLocation ($1));
2118           }
2119         ;
2120
2121 constructor_declaration
2122         : constructor_declarator
2123           constructor_body
2124           { 
2125                 Constructor c = (Constructor) $1;
2126                 c.Block = (ToplevelBlock) $2;
2127                 
2128                 if (RootContext.Documentation != null)
2129                         c.DocComment = ConsumeStoredComment ();
2130
2131                 current_container.AddConstructor (c);
2132
2133                 current_local_parameters = null;
2134                 if (RootContext.Documentation != null)
2135                         Lexer.doc_state = XmlCommentState.Allowed;
2136           }
2137         ;
2138
2139 constructor_declarator
2140         : opt_attributes
2141           opt_modifiers
2142           IDENTIFIER
2143           {
2144                 if (RootContext.Documentation != null) {
2145                         tmpComment = Lexer.consume_doc_comment ();
2146                         Lexer.doc_state = XmlCommentState.Allowed;
2147                 }
2148                 
2149                 valid_param_mod = ParameterModifierType.All;
2150           }
2151           OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2152           {
2153                 valid_param_mod = 0;
2154                 current_local_parameters = (ParametersCompiled) $6;  
2155                 
2156                 //
2157                 // start block here, so possible anonymous methods inside
2158                 // constructor initializer can get correct parent block
2159                 //
2160                 start_block (lexer.Location);
2161           }
2162           opt_constructor_initializer
2163           {
2164                 var lt = (Tokenizer.LocatedToken) $3;
2165                 int mods = (int) $2;
2166                 ConstructorInitializer ci = (ConstructorInitializer) $9;
2167
2168                 Constructor c = new Constructor (current_class, lt.Value, mods,
2169                         (Attributes) $1, current_local_parameters, ci, lt.Location);
2170                 
2171                 if (lt.Value != current_container.MemberName.Name) {
2172                         Report.Error (1520, c.Location, "Class, struct, or interface method must have a return type");
2173                 } else if ((mods & Modifiers.STATIC) != 0) {
2174                         if ((mods & Modifiers.Accessibility) != 0){
2175                                 Report.Error (515, c.Location,
2176                                         "`{0}': static constructor cannot have an access modifier",
2177                                         c.GetSignatureForError ());
2178                         }
2179                         if (ci != null) {
2180                                 Report.Error (514, c.Location,
2181                                         "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2182                                         c.GetSignatureForError ());
2183                         
2184                         }
2185                 }
2186                 
2187                 $$ = c;
2188           }
2189         ;
2190
2191 constructor_body
2192         : block_prepared
2193         | SEMICOLON             { current_block = null; $$ = null; }
2194         ;
2195
2196 opt_constructor_initializer
2197         : /* Empty */
2198         | constructor_initializer
2199         ;
2200
2201 constructor_initializer
2202         : COLON BASE OPEN_PARENS
2203           {
2204                 ++lexer.parsing_block;
2205           }
2206           opt_argument_list CLOSE_PARENS
2207           {
2208                 --lexer.parsing_block;
2209                 $$ = new ConstructorBaseInitializer ((Arguments) $5, GetLocation ($2));
2210           }
2211         | COLON THIS OPEN_PARENS
2212           {
2213                 ++lexer.parsing_block;
2214           }
2215           opt_argument_list CLOSE_PARENS
2216           {
2217                 --lexer.parsing_block;
2218                 $$ = new ConstructorThisInitializer ((Arguments) $5, GetLocation ($2));
2219           }
2220         | COLON error {
2221                 Report.Error (1018, GetLocation ($1), "Keyword `this' or `base' expected");
2222                 $$ = null;
2223           }
2224         ;
2225
2226 destructor_declaration
2227         : opt_attributes opt_modifiers TILDE 
2228           {
2229                 if (RootContext.Documentation != null) {
2230                         tmpComment = Lexer.consume_doc_comment ();
2231                         Lexer.doc_state = XmlCommentState.NotAllowed;
2232                 }
2233                 
2234                 current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters;
2235           }
2236           IDENTIFIER OPEN_PARENS CLOSE_PARENS method_body
2237           {
2238                 var lt = (Tokenizer.LocatedToken) $5;
2239                 if (lt.Value != current_container.MemberName.Name){
2240                         Report.Error (574, lt.Location, "Name of destructor must match name of class");
2241                 } else if (current_container.Kind != Kind.Class){
2242                         Report.Error (575, lt.Location, "Only class types can contain destructor");
2243                 } else {
2244                         Destructor d = new Destructor (current_class, (int) $2,
2245                                 ParametersCompiled.EmptyReadOnlyParameters, (Attributes) $1, lt.Location);
2246                         if (RootContext.Documentation != null)
2247                                 d.DocComment = ConsumeStoredComment ();
2248                   
2249                         d.Block = (ToplevelBlock) $8;
2250                         current_container.AddMethod (d);
2251                 }
2252
2253                 current_local_parameters = null;
2254           }
2255         ;
2256
2257 event_declaration
2258         : opt_attributes
2259           opt_modifiers
2260           EVENT type variable_declarators SEMICOLON
2261           {
2262                 current_array_type = null;
2263                 foreach (VariableMemberDeclaration var in (ArrayList) $5) {
2264
2265                         EventField e = new EventField (
2266                                 current_class, (FullNamedExpression) $4, (int) $2, var.MemberName, (Attributes) $1);
2267                                 
2268                         if (var.expression_or_array_initializer != null) {
2269                                 if (current_container.Kind == Kind.Interface) {
2270                                         Report.Error (68, e.Location, "`{0}': event in interface cannot have initializer", e.GetSignatureForError ());
2271                                 }
2272
2273                                 e.Initializer = var.expression_or_array_initializer;
2274                         }
2275                         
2276                         if (var.MemberName.Left != null) {
2277                                 Report.Error (71, e.Location,
2278                                         "`{0}': An explicit interface implementation of an event must use property syntax",
2279                                         e.GetSignatureForError ());
2280                         }
2281
2282                         current_container.AddEvent (e);
2283
2284                         if (RootContext.Documentation != null) {
2285                                 e.DocComment = Lexer.consume_doc_comment ();
2286                                 Lexer.doc_state = XmlCommentState.Allowed;
2287                         }
2288                 }
2289           }
2290         | opt_attributes
2291           opt_modifiers
2292           EVENT type member_declaration_name
2293           OPEN_BRACE
2294           {
2295                 implicit_value_parameter_type = (FullNamedExpression) $4;  
2296                 current_local_parameters = new ParametersCompiled (compiler,
2297                         new Parameter (implicit_value_parameter_type, "value", 
2298                         Parameter.Modifier.NONE, null, GetLocation ($3)));
2299
2300                 lexer.EventParsing = true;
2301           }
2302           event_accessor_declarations
2303           {
2304                 lexer.EventParsing = false;  
2305           }
2306           CLOSE_BRACE
2307           {
2308                 MemberName name = (MemberName) $5;
2309                 
2310                 if (current_container.Kind == Kind.Interface) {
2311                         Report.Error (69, GetLocation ($3), "Event in interface cannot have add or remove accessors");
2312                         $8 = new Accessors (null, null);
2313                 } else if ($8 == null) {
2314                         Report.Error (65, GetLocation ($3), "`{0}.{1}': event property must have both add and remove accessors",
2315                                 current_container.GetSignatureForError (), name.GetSignatureForError ());
2316                         $8 = new Accessors (null, null);
2317                 }
2318                 
2319                 Accessors accessors = (Accessors) $8;
2320
2321                 if (accessors.get_or_add == null || accessors.set_or_remove == null)
2322                         // CS0073 is already reported, so no CS0065 here.
2323                         $$ = null;
2324                 else {
2325                         Event e = new EventProperty (
2326                                 current_class, (FullNamedExpression) $4, (int) $2, name,
2327                                 (Attributes) $1, accessors.get_or_add, accessors.set_or_remove);
2328                         if (RootContext.Documentation != null) {
2329                                 e.DocComment = Lexer.consume_doc_comment ();
2330                                 Lexer.doc_state = XmlCommentState.Allowed;
2331                         }
2332
2333                         current_container.AddEvent (e);
2334                         implicit_value_parameter_type = null;
2335                 }
2336                 current_local_parameters = null;
2337           }
2338         | opt_attributes opt_modifiers EVENT type member_declaration_name error
2339           {
2340                 MemberName mn = (MemberName) $5;
2341                 if (mn.Left != null)
2342                         Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2343
2344                 if (RootContext.Documentation != null)
2345                         Lexer.doc_state = XmlCommentState.Allowed;
2346
2347                 Error_SyntaxError (yyToken);
2348                 $$ = null;
2349           }
2350         ;
2351
2352 event_accessor_declarations
2353         : add_accessor_declaration remove_accessor_declaration
2354           {
2355                 $$ = new Accessors ((Accessor) $1, (Accessor) $2);
2356           }
2357         | remove_accessor_declaration add_accessor_declaration
2358           {
2359                 Accessors accessors = new Accessors ((Accessor) $2, (Accessor) $1);
2360                 accessors.declared_in_reverse = true;
2361                 $$ = accessors;
2362           }     
2363         | add_accessor_declaration  { $$ = null; } 
2364         | remove_accessor_declaration { $$ = null; } 
2365         | error
2366           { 
2367                 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2368                 $$ = null;
2369           }
2370         | { $$ = null; }
2371         ;
2372
2373 add_accessor_declaration
2374         : opt_attributes ADD
2375           {
2376                 lexer.EventParsing = false;
2377           }
2378           block
2379           {
2380                 Accessor accessor = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, null, GetLocation ($2));
2381                 lexer.EventParsing = true;
2382                 $$ = accessor;
2383           }
2384         | opt_attributes ADD error {
2385                 Report.Error (73, GetLocation ($2), "An add or remove accessor must have a body");
2386                 $$ = null;
2387           }
2388         | opt_attributes modifiers ADD {
2389                 Report.Error (1609, GetLocation ($3), "Modifiers cannot be placed on event accessor declarations");
2390                 $$ = null;
2391           }
2392         ;
2393
2394 remove_accessor_declaration
2395         : opt_attributes REMOVE
2396           {
2397                 lexer.EventParsing = false;
2398           }
2399           block
2400           {
2401                 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, null, GetLocation ($2));
2402                 lexer.EventParsing = true;
2403           }
2404         | opt_attributes REMOVE error {
2405                 Report.Error (73, GetLocation ($2), "An add or remove accessor must have a body");
2406                 $$ = null;
2407           }
2408         | opt_attributes modifiers REMOVE {
2409                 Report.Error (1609, GetLocation ($3), "Modifiers cannot be placed on event accessor declarations");
2410                 $$ = null;
2411           }
2412         ;
2413
2414 indexer_declaration
2415         : opt_attributes opt_modifiers
2416           member_type indexer_declaration_name OPEN_BRACKET
2417           {
2418                 valid_param_mod = ParameterModifierType.Params | ParameterModifierType.DefaultValue;
2419           }
2420           opt_formal_parameter_list CLOSE_BRACKET
2421           OPEN_BRACE
2422           {
2423                 valid_param_mod = 0;
2424                 implicit_value_parameter_type = (FullNamedExpression) $3;
2425                 indexer_parameters = (ParametersCompiled) $7;
2426                 
2427                 if (indexer_parameters.IsEmpty) {
2428                         Report.Error (1551, GetLocation ($5), "Indexers must have at least one parameter");
2429                 }
2430
2431                 if (RootContext.Documentation != null) {
2432                         tmpComment = Lexer.consume_doc_comment ();
2433                         Lexer.doc_state = XmlCommentState.Allowed;
2434                 }
2435
2436                 lexer.PropertyParsing = true;
2437                 parsing_indexer  = true;
2438                 
2439           }
2440           accessor_declarations 
2441           {
2442                   lexer.PropertyParsing = false;
2443                   has_get = has_set = false;
2444                   parsing_indexer  = false;
2445           }
2446           CLOSE_BRACE
2447           { 
2448                 Accessors accessors = (Accessors) $11;
2449                 Accessor get_block = accessors != null ? accessors.get_or_add : null;
2450                 Accessor set_block = accessors != null ? accessors.set_or_remove : null;
2451                 bool order = accessors != null ? accessors.declared_in_reverse : false;
2452
2453                 Indexer indexer = new Indexer (current_class, (FullNamedExpression) $3,
2454                         (MemberName)$4, (int) $2, (ParametersCompiled) $7, (Attributes) $1,
2455                         get_block, set_block, order);
2456                                        
2457                 if ($3 == TypeManager.system_void_expr)
2458                         Report.Error (620, GetLocation ($3), "`{0}': indexer return type cannot be `void'", indexer.GetSignatureForError ());
2459                         
2460                 if (accessors == null)
2461                         Report.Error (548, indexer.Location, "`{0}': property or indexer must have at least one accessor", indexer.GetSignatureForError ());
2462
2463                 if (current_container.Kind == Kind.Interface) {
2464                         if (indexer.Get.Block != null)
2465                                 Report.Error (531, indexer.Location, "`{0}.get': interface members cannot have a definition", indexer.GetSignatureForError ());
2466
2467                         if (indexer.Set.Block != null)
2468                                 Report.Error (531, indexer.Location, "`{0}.set': interface members cannot have a definition", indexer.GetSignatureForError ());
2469                 }
2470
2471                 if (RootContext.Documentation != null)
2472                         indexer.DocComment = ConsumeStoredComment ();
2473
2474                 current_container.AddIndexer (indexer);
2475                 
2476                 current_local_parameters = null;
2477                 implicit_value_parameter_type = null;
2478                 indexer_parameters = null;
2479           }
2480         ;
2481
2482 enum_declaration
2483         : opt_attributes
2484           opt_modifiers
2485           ENUM type_declaration_name
2486           opt_enum_base {
2487                 if (RootContext.Documentation != null)
2488                         enumTypeComment = Lexer.consume_doc_comment ();
2489           }
2490           enum_body
2491           opt_semicolon
2492           {
2493                 MemberName name = (MemberName) $4;
2494                 if (name.IsGeneric) {
2495                         Report.Error (1675, name.Location, "Enums cannot have type parameters");
2496                 }
2497
2498                 name = MakeName (name);
2499                 Enum e = new Enum (current_namespace, current_class, (TypeExpr) $5, (int) $2,
2500                                    name, (Attributes) $1);
2501                 
2502                 if (RootContext.Documentation != null)
2503                         e.DocComment = enumTypeComment;
2504
2505
2506                 EnumMember em = null;
2507                 foreach (VariableDeclaration ev in (ArrayList) $7) {
2508                         em = new EnumMember (
2509                                 e, em, ev.identifier, (Expression) ev.expression_or_array_initializer,
2510                                 ev.OptAttributes, ev.Location);
2511
2512 //                      if (RootContext.Documentation != null)
2513                                 em.DocComment = ev.DocComment;
2514
2515                         e.AddEnumMember (em);
2516                 }
2517                 if (RootContext.EvalMode)
2518                         undo.AddTypeContainer (current_container, e);
2519
2520                 current_container.AddTypeContainer (e);
2521
2522                 $$ = e;
2523
2524           }
2525         ;
2526
2527 opt_enum_base
2528         : /* empty */
2529           {
2530                 $$ = TypeManager.system_int32_expr;
2531           }
2532         | COLON type
2533          {
2534                 if ($2 != TypeManager.system_int32_expr && $2 != TypeManager.system_uint32_expr &&
2535                         $2 != TypeManager.system_int64_expr && $2 != TypeManager.system_uint64_expr &&
2536                         $2 != TypeManager.system_int16_expr && $2 != TypeManager.system_uint16_expr &&
2537                         $2 != TypeManager.system_byte_expr && $2 != TypeManager.system_sbyte_expr) {
2538                         Enum.Error_1008 (GetLocation ($2), Report);
2539                         $2 = TypeManager.system_int32_expr;
2540                 }
2541          
2542                 $$ = $2;
2543          }
2544         | COLON error
2545          {
2546                 Error_TypeExpected (lexer.Location);
2547                 $$ = TypeManager.system_int32_expr;
2548          }
2549         ;
2550
2551 enum_body
2552         : OPEN_BRACE
2553           {
2554                 if (RootContext.Documentation != null)
2555                         Lexer.doc_state = XmlCommentState.Allowed;
2556           }
2557           opt_enum_member_declarations
2558           {
2559                 // here will be evaluated after CLOSE_BLACE is consumed.
2560                 if (RootContext.Documentation != null)
2561                         Lexer.doc_state = XmlCommentState.Allowed;
2562           }
2563           CLOSE_BRACE
2564           {
2565                 $$ = $3;
2566           }
2567         ;
2568
2569 opt_enum_member_declarations
2570         : /* empty */                   { $$ = new ArrayList (0); }
2571         | enum_member_declarations opt_comma { $$ = $1; }
2572         ;
2573
2574 enum_member_declarations
2575         : enum_member_declaration 
2576           {
2577                 ArrayList l = new ArrayList (4);
2578
2579                 l.Add ($1);
2580                 $$ = l;
2581           }
2582         | enum_member_declarations COMMA enum_member_declaration
2583           {
2584                 ArrayList l = (ArrayList) $1;
2585
2586                 l.Add ($3);
2587
2588                 $$ = l;
2589           }
2590         ;
2591
2592 enum_member_declaration
2593         : opt_attributes IDENTIFIER 
2594           {
2595                 VariableDeclaration vd = new VariableDeclaration (
2596                         (Tokenizer.LocatedToken) $2, null, (Attributes) $1);
2597
2598                 if (RootContext.Documentation != null) {
2599                         vd.DocComment = Lexer.consume_doc_comment ();
2600                         Lexer.doc_state = XmlCommentState.Allowed;
2601                 }
2602
2603                 $$ = vd;
2604           }
2605         | opt_attributes IDENTIFIER
2606           {
2607                 ++lexer.parsing_block;
2608                 if (RootContext.Documentation != null) {
2609                         tmpComment = Lexer.consume_doc_comment ();
2610                         Lexer.doc_state = XmlCommentState.NotAllowed;
2611                 }
2612           }
2613           ASSIGN constant_expression
2614           { 
2615                 --lexer.parsing_block;    
2616                 VariableDeclaration vd = new VariableDeclaration (
2617                         (Tokenizer.LocatedToken) $2, $5, (Attributes) $1);
2618
2619                 if (RootContext.Documentation != null)
2620                         vd.DocComment = ConsumeStoredComment ();
2621
2622                 $$ = vd;
2623           }
2624         ;
2625
2626 delegate_declaration
2627         : opt_attributes
2628           opt_modifiers
2629           DELEGATE
2630           member_type type_declaration_name
2631           OPEN_PARENS
2632           {
2633                 valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out | ParameterModifierType.Params | ParameterModifierType.DefaultValue;
2634           }
2635           opt_formal_parameter_list CLOSE_PARENS
2636           {
2637                 valid_param_mod = 0;
2638
2639                 MemberName name = MakeName ((MemberName) $5);
2640                 ParametersCompiled p = (ParametersCompiled) $8;
2641
2642                 Delegate del = new Delegate (current_namespace, current_class, (FullNamedExpression) $4,
2643                                              (int) $2, name, p, (Attributes) $1);
2644
2645                 if (RootContext.Documentation != null) {
2646                         del.DocComment = Lexer.consume_doc_comment ();
2647                         Lexer.doc_state = XmlCommentState.Allowed;
2648                 }
2649
2650                 current_container.AddDelegate (del);
2651                 current_delegate = del;
2652                 lexer.ConstraintsParsing = true;
2653           }
2654           opt_type_parameter_constraints_clauses
2655           {
2656                 lexer.ConstraintsParsing = false;
2657           }
2658           SEMICOLON
2659           {
2660                 current_delegate.SetParameterInfo ((ArrayList) $11);
2661                 $$ = current_delegate;
2662
2663                 current_delegate = null;
2664           }
2665         ;
2666
2667 opt_nullable
2668         : /* empty */
2669           {
2670                 $$ = null;
2671           }
2672         | INTERR_NULLABLE
2673           {
2674                 if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)        
2675                         Report.FeatureIsNotSupported (GetLocation ($1), "nullable types");
2676                 else if (RootContext.Version < LanguageVersion.ISO_2)
2677                         Report.FeatureIsNotAvailable (GetLocation ($1), "nullable types");
2678           
2679                 $$ = this;
2680           }
2681         ;
2682
2683 namespace_or_type_name
2684         : member_name
2685         | qualified_alias_member IDENTIFIER opt_type_argument_list
2686           {
2687                 var lt1 = (Tokenizer.LocatedToken) $1;
2688                 var lt2 = (Tokenizer.LocatedToken) $2;
2689                 
2690                 $$ = new MemberName (lt1.Value, lt2.Value, (TypeArguments) $3, lt1.Location);
2691           }
2692         ;
2693
2694 member_name
2695         : type_name
2696         | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list
2697           {
2698                 var lt = (Tokenizer.LocatedToken) $3;
2699                 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $4, lt.Location);
2700           }
2701         ;
2702
2703 type_name
2704         : IDENTIFIER opt_type_argument_list
2705           {
2706                 var lt = (Tokenizer.LocatedToken) $1;
2707                 $$ = new MemberName (lt.Value, (TypeArguments)$2, lt.Location);   
2708           }
2709         ;
2710         
2711 //
2712 // Generics arguments  (any type, without attributes)
2713 //
2714 opt_type_argument_list
2715         : /* empty */                { $$ = null; } 
2716         | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2717           {
2718                 if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)        
2719                         Report.FeatureIsNotSupported (GetLocation ($1), "generics");
2720                 else if (RootContext.Version < LanguageVersion.ISO_2)
2721                         Report.FeatureIsNotAvailable (GetLocation ($1), "generics");      
2722           
2723                 $$ = $2;
2724           }
2725         | OP_GENERICS_LT error
2726           {
2727                 Error_TypeExpected (lexer.Location);
2728                 $$ = new TypeArguments ();
2729           }
2730         ;
2731
2732 type_arguments
2733         : type
2734           {
2735                 TypeArguments type_args = new TypeArguments ();
2736                 type_args.Add ((FullNamedExpression) $1);
2737                 $$ = type_args;
2738           }
2739         | type_arguments COMMA type
2740           {
2741                 TypeArguments type_args = (TypeArguments) $1;
2742                 type_args.Add ((FullNamedExpression) $3);
2743                 $$ = type_args;
2744           }       
2745         ;
2746
2747 //
2748 // Generics parameters (identifiers only, with attributes), used in type or method declarations
2749 //
2750 type_declaration_name
2751         : IDENTIFIER
2752           {
2753                 lexer.parsing_generic_declaration = true;
2754           }
2755           opt_type_parameter_list
2756           {
2757                 lexer.parsing_generic_declaration = false;
2758                 var lt = (Tokenizer.LocatedToken) $1;
2759                 $$ = new MemberName (lt.Value, (TypeArguments)$3, lt.Location);   
2760           }
2761         ;
2762
2763 member_declaration_name
2764         : method_declaration_name
2765           {
2766                 MemberName mn = (MemberName)$1;
2767                 if (mn.TypeArguments != null)
2768                         syntax_error (mn.Location, string.Format ("Member `{0}' cannot declare type arguments",
2769                                 mn.GetSignatureForError ()));
2770           }
2771         ;
2772
2773 method_declaration_name
2774         : type_declaration_name
2775         | explicit_interface IDENTIFIER opt_type_parameter_list
2776           {
2777                 lexer.parsing_generic_declaration = false;        
2778                 var lt = (Tokenizer.LocatedToken) $2;
2779                 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $3, lt.Location);
2780           }
2781         ;
2782         
2783 indexer_declaration_name
2784         : THIS
2785           {
2786                 lexer.parsing_generic_declaration = false;        
2787                 $$ = new MemberName (TypeContainer.DefaultIndexerName, GetLocation ($1));
2788           }
2789         | explicit_interface THIS
2790           {
2791                 lexer.parsing_generic_declaration = false;
2792                 $$ = new MemberName ((MemberName) $1, TypeContainer.DefaultIndexerName, null, GetLocation ($1));
2793           }
2794         ;
2795
2796 explicit_interface
2797         : IDENTIFIER opt_type_argument_list DOT
2798           {
2799                 var lt = (Tokenizer.LocatedToken) $1;
2800                 $$ = new MemberName (lt.Value, (TypeArguments) $2, lt.Location);
2801           }
2802         | qualified_alias_member IDENTIFIER opt_type_argument_list DOT
2803           {
2804                 var lt1 = (Tokenizer.LocatedToken) $1;
2805                 var lt2 = (Tokenizer.LocatedToken) $2;
2806                 
2807                 $$ = new MemberName (lt1.Value, lt2.Value, (TypeArguments) $3, lt1.Location);
2808           }
2809         | explicit_interface IDENTIFIER opt_type_argument_list DOT
2810           {
2811                 var lt = (Tokenizer.LocatedToken) $2;
2812                 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $3, lt.Location);
2813           }
2814         ;
2815         
2816 opt_type_parameter_list
2817         : /* empty */                { $$ = null; } 
2818         | OP_GENERICS_LT_DECL type_parameters OP_GENERICS_GT
2819           {
2820                 if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)        
2821                         Report.FeatureIsNotSupported (GetLocation ($1), "generics");
2822                 else if (RootContext.Version < LanguageVersion.ISO_2)
2823                         Report.FeatureIsNotAvailable (GetLocation ($1), "generics");
2824           
2825                 $$ = $2;
2826           }
2827         ;
2828
2829 type_parameters
2830         : type_parameter
2831           {
2832                 TypeArguments type_args = new TypeArguments ();
2833                 type_args.Add ((FullNamedExpression)$1);
2834                 $$ = type_args;
2835           }
2836         | type_parameters COMMA type_parameter
2837           {
2838                 TypeArguments type_args = (TypeArguments) $1;
2839                 type_args.Add ((FullNamedExpression)$3);
2840                 $$ = type_args;
2841           }       
2842         ;
2843
2844 type_parameter
2845         : opt_attributes opt_type_parameter_variance IDENTIFIER
2846           {
2847                 var lt = (Tokenizer.LocatedToken)$3;
2848                 $$ = new TypeParameterName (lt.Value, (Attributes)$1, (Variance) $2, lt.Location);
2849           }
2850         | error
2851           {
2852                 if (GetTokenName (yyToken) == "type")
2853                         Report.Error (81, GetLocation ($1), "Type parameter declaration must be an identifier not a type");
2854                 else
2855                         Error_SyntaxError (yyToken);
2856                         
2857                 $$ = new TypeParameterName ("", null, lexer.Location);
2858           }
2859         ;
2860
2861 //
2862 // All types where void is allowed
2863 //
2864 type_and_void
2865         : type_expression_or_array
2866         | VOID
2867           {
2868                 $$ = TypeManager.system_void_expr;
2869           }
2870         ;
2871         
2872 member_type
2873         : type_and_void
2874           {
2875                 lexer.parsing_generic_declaration = true;
2876           }
2877         ;
2878
2879 //
2880 // A type which does not allow `void' to be used
2881 //
2882 type
2883         : type_expression_or_array
2884         | VOID
2885           {
2886                 Expression.Error_VoidInvalidInTheContext (GetLocation ($1), Report);
2887                 $$ = TypeManager.system_void_expr;
2888           }     
2889         ;
2890         
2891 simple_type
2892         : type_expression
2893         | VOID
2894           {
2895                 Expression.Error_VoidInvalidInTheContext (GetLocation ($1), Report);
2896                 $$ = TypeManager.system_void_expr;
2897           }     
2898         ;
2899         
2900 parameter_type
2901         : type_expression_or_array
2902         | VOID
2903           {
2904                 Report.Error (1536, GetLocation ($1), "Invalid parameter type `void'");
2905                 $$ = TypeManager.system_void_expr;
2906           }     
2907         ;
2908
2909 type_expression_or_array
2910         : type_expression
2911         | type_expression rank_specifiers
2912           {
2913                 string rank_specifiers = (string) $2;
2914                 $$ = current_array_type = new ComposedCast ((FullNamedExpression) $1, rank_specifiers);
2915           }
2916         ;
2917         
2918 type_expression
2919         : namespace_or_type_name opt_nullable
2920           {
2921                 MemberName name = (MemberName) $1;
2922
2923                 if ($2 != null) {
2924                         $$ = new ComposedCast (name.GetTypeExpression (), "?", lexer.Location);
2925                 } else {
2926                         if (name.Left == null && name.Name == "var")
2927                                 $$ = current_array_type = new VarExpr (name.Location);
2928                         else
2929                                 $$ = name.GetTypeExpression ();
2930                 }
2931           }
2932         | builtin_types opt_nullable
2933           {
2934                 if ($2 != null)
2935                         $$ = new ComposedCast ((FullNamedExpression) $1, "?", lexer.Location);
2936           }
2937         | type_expression STAR
2938           {
2939                 //
2940                 // Note that here only unmanaged types are allowed but we
2941                 // can't perform checks during this phase - we do it during
2942                 // semantic analysis.
2943                 //
2944                 $$ = new ComposedCast ((FullNamedExpression) $1, "*", Lexer.Location);
2945           }
2946         | VOID STAR
2947           {
2948                 $$ = new ComposedCast (TypeManager.system_void_expr, "*", GetLocation ($1));
2949           }     
2950         ;
2951
2952 type_list
2953         : base_type_name
2954           {
2955                 ArrayList types = new ArrayList (2);
2956                 types.Add ($1);
2957                 $$ = types;
2958           }
2959         | type_list COMMA base_type_name
2960           {
2961                 ArrayList types = (ArrayList) $1;
2962                 types.Add ($3);
2963                 $$ = types;
2964           }
2965         ;
2966
2967 base_type_name
2968         : type
2969           {
2970                 if ($1 is ComposedCast)
2971                         Report.Error (1521, GetLocation ($1), "Invalid base type `{0}'", ((ComposedCast)$1).GetSignatureForError ());
2972                 $$ = $1;
2973           }
2974         | error
2975           {
2976                 Error_TypeExpected (lexer.Location);
2977           }
2978         ;
2979         
2980 /*
2981  * replaces all the productions for isolating the various
2982  * simple types, but we need this to reuse it easily in variable_type
2983  */
2984 builtin_types
2985         : OBJECT        { $$ = TypeManager.system_object_expr; }
2986         | STRING        { $$ = TypeManager.system_string_expr; }
2987         | BOOL          { $$ = TypeManager.system_boolean_expr; }
2988         | DECIMAL       { $$ = TypeManager.system_decimal_expr; }
2989         | FLOAT         { $$ = TypeManager.system_single_expr; }
2990         | DOUBLE        { $$ = TypeManager.system_double_expr; }
2991         | integral_type
2992         ;
2993
2994 integral_type
2995         : SBYTE         { $$ = TypeManager.system_sbyte_expr; }
2996         | BYTE          { $$ = TypeManager.system_byte_expr; }
2997         | SHORT         { $$ = TypeManager.system_int16_expr; }
2998         | USHORT        { $$ = TypeManager.system_uint16_expr; }
2999         | INT           { $$ = TypeManager.system_int32_expr; }
3000         | UINT          { $$ = TypeManager.system_uint32_expr; }
3001         | LONG          { $$ = TypeManager.system_int64_expr; }
3002         | ULONG         { $$ = TypeManager.system_uint64_expr; }
3003         | CHAR          { $$ = TypeManager.system_char_expr; }
3004         ;
3005
3006 predefined_type
3007         : builtin_types
3008         | VOID
3009           {
3010                 $$ = TypeManager.system_void_expr;      
3011           }
3012         ;
3013
3014 //
3015 // Expressions, section 7.5
3016 //
3017
3018
3019 primary_expression
3020         : primary_expression_no_array_creation
3021         | array_creation_expression
3022         ;
3023
3024 primary_expression_no_array_creation
3025         : literal
3026         | IDENTIFIER opt_type_argument_list
3027           {
3028                 var lt = (Tokenizer.LocatedToken) $1;
3029                 $$ = new SimpleName (MemberName.MakeName (lt.Value, (TypeArguments)$2), (TypeArguments)$2, lt.Location);          
3030           }
3031         | IDENTIFIER GENERATE_COMPLETION {
3032                 var lt = (Tokenizer.LocatedToken) $1;
3033                $$ = new CompletionSimpleName (MemberName.MakeName (lt.Value, null), lt.Location);
3034           }
3035         | parenthesized_expression
3036         | default_value_expression
3037         | member_access
3038         | invocation_expression
3039         | element_access
3040         | this_access
3041         | base_access
3042         | post_increment_expression
3043         | post_decrement_expression
3044         | object_or_delegate_creation_expression
3045         | anonymous_type_expression
3046         | typeof_expression
3047         | sizeof_expression
3048         | checked_expression
3049         | unchecked_expression
3050         | pointer_member_access
3051         | anonymous_method_expression
3052         ;
3053
3054 literal
3055         : boolean_literal
3056         | LITERAL
3057         | NULL                  { $$ = new NullLiteral (GetLocation ($1)); }
3058         ;
3059
3060 boolean_literal
3061         : TRUE                  { $$ = new BoolLiteral (true, GetLocation ($1)); }
3062         | FALSE                 { $$ = new BoolLiteral (false, GetLocation ($1)); }
3063         ;
3064
3065
3066 //
3067 // Here is the trick, tokenizer may think that parens is a special but
3068 // parser is interested in open parens only, so we merge them.
3069 // Consider: if (a)foo ();
3070 //
3071 open_parens_any
3072         : OPEN_PARENS
3073         | OPEN_PARENS_CAST
3074         | OPEN_PARENS_LAMBDA
3075         ;
3076
3077 parenthesized_expression
3078         : OPEN_PARENS expression CLOSE_PARENS
3079           {
3080                 $$ = new ParenthesizedExpression ((Expression) $2);
3081           }
3082         | OPEN_PARENS expression COMPLETE_COMPLETION
3083           {
3084                 $$ = new ParenthesizedExpression ((Expression) $2);
3085           }
3086         ;
3087         
3088 member_access
3089         : primary_expression DOT IDENTIFIER opt_type_argument_list
3090           {
3091                 var lt = (Tokenizer.LocatedToken) $3;
3092                 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
3093           }
3094         | predefined_type DOT IDENTIFIER opt_type_argument_list
3095           {
3096                 var lt = (Tokenizer.LocatedToken) $3;
3097                 // TODO: Location is wrong as some predefined types doesn't hold a location
3098                 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
3099           }
3100         | qualified_alias_member IDENTIFIER opt_type_argument_list
3101           {
3102                 var lt1 = (Tokenizer.LocatedToken) $1;
3103                 var lt2 = (Tokenizer.LocatedToken) $2;
3104
3105                 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, (TypeArguments) $3, lt1.Location);
3106           }
3107         | primary_expression DOT GENERATE_COMPLETION {
3108                 $$ = new CompletionMemberAccess ((Expression) $1, null,GetLocation ($3));
3109           }
3110         | primary_expression DOT IDENTIFIER GENERATE_COMPLETION {
3111                 var lt = (Tokenizer.LocatedToken) $3;
3112                 $$ = new CompletionMemberAccess ((Expression) $1, lt.Value, lt.Location);
3113           }
3114         | predefined_type DOT GENERATE_COMPLETION
3115           {
3116                 // TODO: Location is wrong as some predefined types doesn't hold a location
3117                 $$ = new CompletionMemberAccess ((Expression) $1, null, lexer.Location);
3118           }
3119         | predefined_type DOT IDENTIFIER GENERATE_COMPLETION {
3120                 var lt = (Tokenizer.LocatedToken) $3;
3121                 $$ = new CompletionMemberAccess ((Expression) $1, lt.Value, lt.Location);
3122           }
3123         ;
3124
3125 invocation_expression
3126         : primary_expression open_parens_any opt_argument_list CLOSE_PARENS
3127           {
3128                 $$ = new Invocation ((Expression) $1, (Arguments) $3);
3129           }
3130         ;
3131
3132 opt_object_or_collection_initializer
3133         : /* empty */           { $$ = null; }
3134         | object_or_collection_initializer
3135         ;
3136
3137 object_or_collection_initializer
3138         : OPEN_BRACE opt_member_initializer_list close_brace_or_complete_completion
3139           {
3140                 if ($2 == null)
3141                         $$ = CollectionOrObjectInitializers.Empty;
3142                 else
3143                         $$ = new CollectionOrObjectInitializers ((ArrayList) $2, GetLocation ($1));
3144           }
3145         | OPEN_BRACE member_initializer_list COMMA CLOSE_BRACE
3146           {
3147                 $$ = new CollectionOrObjectInitializers ((ArrayList) $2, GetLocation ($1));
3148           }
3149         ;
3150
3151 opt_member_initializer_list
3152         : /* empty */           { $$ = null; }
3153         | member_initializer_list
3154         {
3155                 $$ = $1;
3156         }
3157         ;
3158
3159 member_initializer_list
3160         : member_initializer 
3161           {
3162                 ArrayList a = new ArrayList ();
3163                 a.Add ($1);
3164                 $$ = a;
3165           }
3166         | member_initializer_list COMMA member_initializer
3167           {
3168                 ArrayList a = (ArrayList)$1;
3169                 a.Add ($3);
3170                 $$ = a;
3171           }
3172         ;
3173
3174 member_initializer
3175         : IDENTIFIER ASSIGN initializer_value
3176           {
3177                 var lt = (Tokenizer.LocatedToken) $1;
3178                 $$ = new ElementInitializer (lt.Value, (Expression)$3, lt.Location);
3179           }
3180         | GENERATE_COMPLETION 
3181           {
3182                 $$ = new CompletionElementInitializer (null, GetLocation ($1));
3183           }
3184         | non_assignment_expression opt_COMPLETE_COMPLETION  {
3185                 CompletionSimpleName csn = $1 as CompletionSimpleName;
3186                 if (csn == null)
3187                         $$ = new CollectionElementInitializer ((Expression)$1);
3188                 else
3189                         $$ = new CompletionElementInitializer (csn.Prefix, csn.Location);
3190           }
3191         | OPEN_BRACE expression_list CLOSE_BRACE
3192           {
3193                 $$ = new CollectionElementInitializer ((ArrayList)$2, GetLocation ($1));
3194           }
3195         | OPEN_BRACE CLOSE_BRACE
3196           {
3197                 Report.Error (1920, GetLocation ($1), "An element initializer cannot be empty");
3198           }       
3199         ;
3200
3201 initializer_value
3202         : expression
3203         | object_or_collection_initializer
3204         ;
3205
3206 opt_argument_list
3207         : /* empty */           { $$ = null; }
3208         | argument_list
3209         ;
3210
3211 argument_list
3212         : argument_or_named_argument
3213           { 
3214                 Arguments list = new Arguments (4);
3215                 list.Add ((Argument) $1);
3216                 $$ = list;
3217           }
3218         | argument_list COMMA argument
3219           {
3220                 Arguments list = (Arguments) $1;
3221                 if (list [list.Count - 1] is NamedArgument)
3222                         Error_NamedArgumentExpected ((NamedArgument) list [list.Count - 1]);
3223                 
3224                 list.Add ((Argument) $3);
3225                 $$ = list;
3226           }
3227         | argument_list COMMA named_argument
3228           {
3229                 Arguments list = (Arguments) $1;
3230                 NamedArgument a = (NamedArgument) $3;
3231                 for (int i = 0; i < list.Count; ++i) {
3232                         NamedArgument na = list [i] as NamedArgument;
3233                         if (na != null && na.Name == a.Name)
3234                                 Report.Error (1740, na.Location, "Named argument `{0}' specified multiple times",
3235                                         na.Name);
3236                 }
3237                 
3238                 list.Add (a);
3239                 $$ = list;
3240           }
3241         | argument_list COMMA
3242           {
3243                 Report.Error (839, GetLocation ($2), "An argument is missing");
3244                 $$ = null;
3245           }
3246         | COMMA argument_or_named_argument
3247           {
3248                 Report.Error (839, GetLocation ($1), "An argument is missing");
3249                 $$ = null;
3250           }
3251         ;
3252
3253 argument
3254         : expression
3255           {
3256                 $$ = new Argument ((Expression) $1);
3257           }
3258         | non_simple_argument
3259         ;
3260
3261 argument_or_named_argument
3262         : argument
3263         | named_argument
3264         ;
3265
3266 non_simple_argument
3267         : REF variable_reference 
3268           { 
3269                 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
3270           }
3271         | OUT variable_reference 
3272           { 
3273                 $$ = new Argument ((Expression) $2, Argument.AType.Out);
3274           }
3275         | ARGLIST open_parens_any argument_list CLOSE_PARENS
3276           {
3277                 $$ = new Argument (new Arglist ((Arguments) $3, GetLocation ($1)));
3278           }
3279         | ARGLIST open_parens_any CLOSE_PARENS
3280           {
3281                 $$ = new Argument (new Arglist (GetLocation ($1)));
3282           }       
3283         | ARGLIST
3284           {
3285                 $$ = new Argument (new ArglistAccess (GetLocation ($1)));
3286           }
3287         ;
3288
3289 variable_reference
3290         : expression
3291         ;
3292
3293 element_access
3294         : primary_expression_no_array_creation OPEN_BRACKET expression_list_arguments CLOSE_BRACKET     
3295           {
3296                 $$ = new ElementAccess ((Expression) $1, (Arguments) $3);
3297           }
3298         | array_creation_expression OPEN_BRACKET expression_list_arguments CLOSE_BRACKET
3299           {
3300                 // LAMESPEC: Not allowed according to specification
3301                 $$ = new ElementAccess ((Expression) $1, (Arguments) $3);
3302           }     
3303         | primary_expression_no_array_creation rank_specifiers
3304           {
3305                 // So the super-trick is that primary_expression
3306                 // can only be either a SimpleName or a MemberAccess. 
3307                 // The MemberAccess case arises when you have a fully qualified type-name like :
3308                 // Foo.Bar.Blah i;
3309                 // SimpleName is when you have
3310                 // Blah i;
3311                   
3312                 Expression expr = (Expression) $1;  
3313                 if (expr is ComposedCast){
3314                         $$ = new ComposedCast ((ComposedCast)expr, (string) $2);
3315                 } else if (expr is ATypeNameExpression){
3316                         //
3317                         // So we extract the string corresponding to the SimpleName
3318                         // or MemberAccess
3319                         // 
3320                         $$ = new ComposedCast ((ATypeNameExpression)expr, (string) $2);
3321                 } else {
3322                         Error_ExpectingTypeName (expr);
3323                         $$ = TypeManager.system_object_expr;
3324                 }
3325                 
3326                 current_array_type = (FullNamedExpression)$$;
3327           }
3328         ;
3329
3330 expression_list
3331         : expression
3332           {
3333                 ArrayList list = new ArrayList (4);
3334                 list.Add ($1);
3335                 $$ = list;
3336           }
3337         | expression_list COMMA expression
3338           {
3339                 ArrayList list = (ArrayList) $1;
3340                 list.Add ($3);
3341                 $$ = list;
3342           }
3343         ;
3344         
3345 expression_list_arguments
3346         : expression_list_argument
3347           {
3348                 Arguments args = new Arguments (4);
3349                 args.Add ((Argument) $1);
3350                 $$ = args;
3351           }
3352         | expression_list_arguments COMMA expression_list_argument
3353           {
3354                 Arguments args = (Arguments) $1;
3355                 args.Add ((Argument) $3);
3356                 $$ = args;        
3357           }
3358         ;
3359         
3360 expression_list_argument
3361         : expression
3362           {
3363                 $$ = new Argument ((Expression) $1);
3364           }
3365         | named_argument
3366         ;
3367
3368 this_access
3369         : THIS
3370           {
3371                 $$ = new This (current_block, GetLocation ($1));
3372           }
3373         ;
3374
3375 base_access
3376         : BASE DOT IDENTIFIER opt_type_argument_list
3377           {
3378                 var lt = (Tokenizer.LocatedToken) $3;
3379                 $$ = new BaseAccess (lt.Value, (TypeArguments) $4, lt.Location);
3380           }
3381         | BASE OPEN_BRACKET expression_list_arguments CLOSE_BRACKET
3382           {
3383                 $$ = new BaseIndexerAccess ((Arguments) $3, GetLocation ($1));
3384           }
3385         | BASE error
3386           {
3387                 Error_SyntaxError (yyToken);
3388                 $$ = new BaseAccess (null, GetLocation ($2));
3389           }
3390         ;
3391
3392 post_increment_expression
3393         : primary_expression OP_INC
3394           {
3395                 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement, (Expression) $1);
3396           }
3397         ;
3398
3399 post_decrement_expression
3400         : primary_expression OP_DEC
3401           {
3402                 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement, (Expression) $1);
3403           }
3404         ;
3405
3406 object_or_delegate_creation_expression
3407         : new_expr_start open_parens_any opt_argument_list CLOSE_PARENS opt_object_or_collection_initializer
3408           {
3409                 if ($5 != null) {
3410                         if (RootContext.Version <= LanguageVersion.ISO_2)
3411                                 Report.FeatureIsNotAvailable (GetLocation ($1), "object initializers");
3412                                 
3413                         $$ = new NewInitialize ((Expression) $1, (Arguments) $3, (CollectionOrObjectInitializers) $5, GetLocation ($1));
3414                 }
3415                 else
3416                         $$ = new New ((Expression) $1, (Arguments) $3, GetLocation ($1));
3417           }
3418         | new_expr_start object_or_collection_initializer
3419           {
3420                 if (RootContext.Version <= LanguageVersion.ISO_2)
3421                         Report.FeatureIsNotAvailable (GetLocation ($1), "collection initializers");
3422           
3423                 $$ = new NewInitialize ((Expression) $1, null, (CollectionOrObjectInitializers) $2, GetLocation ($1));
3424           }
3425         ;
3426
3427 array_creation_expression
3428         : new_expr_start OPEN_BRACKET expression_list CLOSE_BRACKET 
3429           opt_rank_specifier    // shift/reduce on OPEN_BRACE
3430           opt_array_initializer
3431           {
3432                 $$ = new ArrayCreation ((FullNamedExpression) $1, (ArrayList) $3, (string) $5, (ArrayList) $6, GetLocation ($1));
3433           }
3434         | new_expr_start rank_specifiers opt_array_initializer
3435           {
3436                 if ($3 == null)
3437                         Report.Error (1586, GetLocation ($1), "Array creation must have array size or array initializer");
3438
3439                 $$ = new ArrayCreation ((FullNamedExpression) $1, (string) $2, (ArrayList) $3, GetLocation ($1));
3440           }
3441         | NEW rank_specifiers array_initializer
3442           {
3443                 if (RootContext.Version <= LanguageVersion.ISO_2)
3444                         Report.FeatureIsNotAvailable (GetLocation ($1), "implicitly typed arrays");
3445           
3446                 $$ = new ImplicitlyTypedArrayCreation ((string) $2, (ArrayList) $3, GetLocation ($1));
3447           }
3448         | new_expr_start error
3449           {
3450                 Report.Error (1526, GetLocation ($1), "A new expression requires () or [] after type");
3451                 $$ = new ArrayCreation ((FullNamedExpression) $1, "[]", null, GetLocation ($1));
3452           }
3453         ;
3454
3455 new_expr_start
3456         : NEW
3457           {
3458                 ++lexer.parsing_type;
3459           }
3460           simple_type
3461           {
3462                 --lexer.parsing_type;
3463                 $$ = $3;
3464           }
3465         ;
3466
3467 anonymous_type_expression
3468         : NEW OPEN_BRACE anonymous_type_parameters_opt_comma CLOSE_BRACE
3469           {
3470                 if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)        
3471                         Report.FeatureIsNotSupported (GetLocation ($1), "anonymous types");
3472                 else if (RootContext.Version <= LanguageVersion.ISO_2)
3473                         Report.FeatureIsNotAvailable (GetLocation ($1), "anonymous types");
3474
3475                 $$ = new NewAnonymousType ((ArrayList) $3, current_container, GetLocation ($1));
3476           }
3477         ;
3478
3479 anonymous_type_parameters_opt_comma
3480         : anonymous_type_parameters_opt
3481         | anonymous_type_parameters COMMA
3482         ;
3483
3484 anonymous_type_parameters_opt
3485         : { $$ = null; }
3486         | anonymous_type_parameters
3487         ;
3488
3489 anonymous_type_parameters
3490         : anonymous_type_parameter
3491           {
3492                 ArrayList a = new ArrayList (4);
3493                 a.Add ($1);
3494                 $$ = a;
3495           }
3496         | anonymous_type_parameters COMMA anonymous_type_parameter
3497           {
3498                 ArrayList a = (ArrayList) $1;
3499                 a.Add ($3);
3500                 $$ = a;
3501           }
3502         ;
3503
3504 anonymous_type_parameter
3505         : IDENTIFIER ASSIGN variable_initializer
3506           {
3507                 var lt = (Tokenizer.LocatedToken)$1;
3508                 $$ = new AnonymousTypeParameter ((Expression)$3, lt.Value, lt.Location);
3509           }
3510         | IDENTIFIER
3511           {
3512                 var lt = (Tokenizer.LocatedToken)$1;
3513                 $$ = new AnonymousTypeParameter (new SimpleName (lt.Value, lt.Location),
3514                         lt.Value, lt.Location);
3515           }
3516         | BASE DOT IDENTIFIER opt_type_argument_list
3517           {
3518                 var lt = (Tokenizer.LocatedToken) $3;
3519                 BaseAccess ba = new BaseAccess (lt.Value, (TypeArguments) $4, lt.Location);
3520                 $$ = new AnonymousTypeParameter (ba, lt.Value, lt.Location);            
3521           }       
3522         | member_access
3523           {
3524                 MemberAccess ma = (MemberAccess) $1;
3525                 $$ = new AnonymousTypeParameter (ma, ma.Name, ma.Location);
3526           }
3527         | error
3528           {
3529                 Report.Error (746, lexer.Location, "Invalid anonymous type member declarator. " +
3530                 "Anonymous type members must be a member assignment, simple name or member access expression");
3531           }
3532         ;
3533
3534 opt_rank_specifier
3535         : /* empty */
3536           {
3537                 $$ = "";
3538           }
3539         | rank_specifiers
3540           {
3541                 $$ = $1;
3542           }
3543         ;
3544
3545 opt_rank_specifier_or_nullable
3546         : opt_nullable
3547           {
3548                 if ($1 != null)
3549                         $$ = "?";
3550                 else
3551                         $$ = string.Empty;
3552           }
3553         | opt_nullable rank_specifiers
3554           {
3555                 if ($1 != null)
3556                         $$ = "?" + (string) $2;
3557                 else
3558                         $$ = $2;
3559           }
3560         ;
3561
3562 rank_specifiers
3563         : rank_specifier
3564         | rank_specifier rank_specifiers
3565           {
3566                 $$ = (string) $2 + (string) $1;
3567           }
3568         ;
3569
3570 rank_specifier
3571         : OPEN_BRACKET CLOSE_BRACKET
3572           {
3573                 $$ = "[]";
3574           }
3575         | OPEN_BRACKET dim_separators CLOSE_BRACKET
3576           {
3577                 $$ = "[" + (string) $2 + "]";
3578           }
3579         | OPEN_BRACKET error
3580           {
3581                 Error_SyntaxError (178, yyToken, "Invalid rank specifier");
3582                 $$ = "[]";
3583           }
3584         ;
3585
3586 dim_separators
3587         : COMMA
3588           {
3589                 $$ = ",";
3590           }
3591         | dim_separators COMMA
3592           {
3593                 $$ = (string) $1 + ",";
3594           }
3595         ;
3596
3597 opt_array_initializer
3598         : /* empty */
3599           {
3600                 $$ = null;
3601           }
3602         | array_initializer
3603           {
3604                 $$ = $1;
3605           }
3606         ;
3607
3608 array_initializer
3609         : OPEN_BRACE CLOSE_BRACE
3610           {
3611                 $$ = new ArrayList (0);
3612           }
3613         | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3614           {
3615                 $$ = (ArrayList) $2;
3616           }
3617         ;
3618
3619 variable_initializer_list
3620         : variable_initializer
3621           {
3622                 ArrayList list = new ArrayList (4);
3623                 list.Add ($1);
3624                 $$ = list;
3625           }
3626         | variable_initializer_list COMMA variable_initializer
3627           {
3628                 ArrayList list = (ArrayList) $1;
3629                 list.Add ($3);
3630                 $$ = list;
3631           }
3632         | error
3633           {
3634                 Error_SyntaxError (yyToken);
3635                 $$ = new ArrayList ();
3636           }
3637         ;
3638
3639 typeof_expression
3640         : TYPEOF
3641       {
3642                 pushed_current_array_type = current_array_type;
3643                 lexer.TypeOfParsing = true;
3644           }
3645           open_parens_any typeof_type_expression CLOSE_PARENS
3646           {
3647                 lexer.TypeOfParsing = false;
3648                 Expression type = (Expression)$4;
3649                 if (type == TypeManager.system_void_expr)
3650                         $$ = new TypeOfVoid (GetLocation ($1));
3651                 else
3652                         $$ = new TypeOf (type, GetLocation ($1));
3653                 current_array_type = pushed_current_array_type;
3654           }
3655         ;
3656         
3657 typeof_type_expression
3658         : type_and_void
3659         | unbound_type_name
3660         | error
3661          {
3662                 Error_TypeExpected (lexer.Location);
3663                 $$ = null;
3664          }
3665         ;
3666         
3667 unbound_type_name
3668         : IDENTIFIER generic_dimension
3669           {  
3670                 var lt = (Tokenizer.LocatedToken) $1;
3671
3672                 $$ = new SimpleName (MemberName.MakeName (lt.Value, (int)$2), lt.Location);
3673           }
3674         | qualified_alias_member IDENTIFIER generic_dimension
3675           {
3676                 var lt1 = (Tokenizer.LocatedToken) $1;
3677                 var lt2 = (Tokenizer.LocatedToken) $2;
3678
3679                 $$ = new QualifiedAliasMember (lt1.Value, MemberName.MakeName (lt2.Value, (int) $3), lt1.Location);
3680           }
3681         | unbound_type_name DOT IDENTIFIER
3682           {
3683                 var lt = (Tokenizer.LocatedToken) $3;
3684                 
3685                 $$ = new MemberAccess ((Expression) $1, lt.Value, lt.Location);         
3686           }
3687         | unbound_type_name DOT IDENTIFIER generic_dimension
3688           {
3689                 var lt = (Tokenizer.LocatedToken) $3;
3690                 
3691                 $$ = new MemberAccess ((Expression) $1, MemberName.MakeName (lt.Value, (int) $4), lt.Location);         
3692           }
3693         | namespace_or_type_name DOT IDENTIFIER generic_dimension
3694           {
3695                 var lt = (Tokenizer.LocatedToken) $3;
3696                 MemberName name = (MemberName) $1;
3697
3698                 $$ = new MemberAccess (name.GetTypeExpression (), MemberName.MakeName (lt.Value, (int) $4), lt.Location);               
3699           }
3700         ;
3701
3702 generic_dimension
3703         : GENERIC_DIMENSION
3704           {
3705                 if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)        
3706                         Report.FeatureIsNotSupported (GetLocation ($1), "generics");
3707                 else if (RootContext.Version < LanguageVersion.ISO_2)
3708                         Report.FeatureIsNotAvailable (GetLocation ($1), "generics");
3709
3710                 $$ = $1;
3711           }
3712         ;
3713         
3714 qualified_alias_member
3715         : IDENTIFIER DOUBLE_COLON
3716           {
3717                 var lt = (Tokenizer.LocatedToken) $1;
3718                 if (RootContext.Version == LanguageVersion.ISO_1)
3719                         Report.FeatureIsNotAvailable (lt.Location, "namespace alias qualifier");
3720
3721                 $$ = lt;                
3722           }
3723         ;
3724
3725 sizeof_expression
3726         : SIZEOF open_parens_any type CLOSE_PARENS { 
3727                 $$ = new SizeOf ((Expression) $3, GetLocation ($1));
3728           }
3729         ;
3730
3731 checked_expression
3732         : CHECKED open_parens_any expression CLOSE_PARENS
3733           {
3734                 $$ = new CheckedExpr ((Expression) $3, GetLocation ($1));
3735           }
3736         ;
3737
3738 unchecked_expression
3739         : UNCHECKED open_parens_any expression CLOSE_PARENS
3740           {
3741                 $$ = new UnCheckedExpr ((Expression) $3, GetLocation ($1));
3742           }
3743         ;
3744
3745 pointer_member_access 
3746         : primary_expression OP_PTR IDENTIFIER
3747           {
3748                 Expression deref;
3749                 var lt = (Tokenizer.LocatedToken) $3;
3750
3751                 deref = new Indirection ((Expression) $1, lt.Location);
3752                 $$ = new MemberAccess (deref, lt.Value);
3753           }
3754         ;
3755
3756 anonymous_method_expression
3757         : DELEGATE opt_anonymous_method_signature
3758           {
3759                 start_anonymous (false, (ParametersCompiled) $2, GetLocation ($1));
3760           }
3761           block
3762           {
3763                 $$ = end_anonymous ((ToplevelBlock) $4);
3764         }
3765         ;
3766
3767 opt_anonymous_method_signature
3768         : 
3769           {
3770                 $$ = ParametersCompiled.Undefined;
3771           } 
3772         | anonymous_method_signature
3773         ;
3774
3775 anonymous_method_signature
3776         : OPEN_PARENS
3777           {
3778                 valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out;
3779           }
3780           opt_formal_parameter_list CLOSE_PARENS
3781           {
3782                 valid_param_mod = 0;
3783                 $$ = $3;
3784           }
3785         ;
3786
3787 default_value_expression
3788         : DEFAULT open_parens_any type CLOSE_PARENS
3789           {
3790                 if (RootContext.Version < LanguageVersion.ISO_2)
3791                         Report.FeatureIsNotAvailable (GetLocation ($1), "default value expression");
3792
3793                 $$ = new DefaultValueExpression ((Expression) $3, GetLocation ($1));
3794           }
3795         ;
3796
3797 unary_expression
3798         : primary_expression
3799         | BANG prefixed_unary_expression
3800           {
3801                 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2);
3802           }
3803         | TILDE prefixed_unary_expression
3804           {
3805                 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2);
3806           }
3807         | cast_expression
3808         ;
3809
3810 cast_expression
3811         : OPEN_PARENS_CAST type CLOSE_PARENS prefixed_unary_expression
3812           {
3813                 $$ = new Cast ((FullNamedExpression) $2, (Expression) $4, GetLocation ($1));
3814           }
3815         | OPEN_PARENS predefined_type CLOSE_PARENS prefixed_unary_expression
3816           {
3817                 $$ = new Cast ((FullNamedExpression) $2, (Expression) $4, GetLocation ($1));
3818           }
3819         ;
3820
3821         //
3822         // The idea to split this out is from Rhys' grammar
3823         // to solve the problem with casts.
3824         //
3825 prefixed_unary_expression
3826         : unary_expression
3827         | PLUS prefixed_unary_expression
3828           { 
3829                 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2);
3830           } 
3831         | MINUS prefixed_unary_expression 
3832           { 
3833                 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2);
3834           }
3835         | OP_INC prefixed_unary_expression 
3836           {
3837                 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement, (Expression) $2);
3838           }
3839         | OP_DEC prefixed_unary_expression 
3840           {
3841                 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement, (Expression) $2);
3842           }
3843         | STAR prefixed_unary_expression
3844           {
3845                 $$ = new Indirection ((Expression) $2, GetLocation ($1));
3846           }
3847         | BITWISE_AND prefixed_unary_expression
3848           {
3849                 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2);
3850           }
3851         ;
3852
3853 multiplicative_expression
3854         : prefixed_unary_expression
3855         | multiplicative_expression STAR prefixed_unary_expression
3856           {
3857                 $$ = new Binary (Binary.Operator.Multiply, 
3858                                  (Expression) $1, (Expression) $3);
3859           }
3860         | multiplicative_expression DIV prefixed_unary_expression
3861           {
3862                 $$ = new Binary (Binary.Operator.Division, 
3863                                  (Expression) $1, (Expression) $3);
3864           }
3865         | multiplicative_expression PERCENT prefixed_unary_expression 
3866           {
3867                 $$ = new Binary (Binary.Operator.Modulus, 
3868                                  (Expression) $1, (Expression) $3);
3869           }
3870         ;
3871
3872 additive_expression
3873         : multiplicative_expression
3874         | additive_expression PLUS multiplicative_expression 
3875           {
3876                 $$ = new Binary (Binary.Operator.Addition, 
3877                                  (Expression) $1, (Expression) $3);
3878           }
3879         | additive_expression MINUS multiplicative_expression
3880           {
3881                 $$ = new Binary (Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3882           }
3883         | parenthesized_expression MINUS multiplicative_expression
3884           {
3885                 // Shift/Reduce conflict
3886                 $$ = new Binary (Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3887           }
3888         | additive_expression AS type
3889           {
3890                 $$ = new As ((Expression) $1, (Expression) $3, GetLocation ($2));
3891           }
3892         | additive_expression IS type
3893           {
3894                 $$ = new Is ((Expression) $1, (Expression) $3, GetLocation ($2));
3895           }       
3896         ;
3897
3898 shift_expression
3899         : additive_expression
3900         | shift_expression OP_SHIFT_LEFT additive_expression
3901           {
3902                 $$ = new Binary (Binary.Operator.LeftShift, 
3903                                  (Expression) $1, (Expression) $3);
3904           }
3905         | shift_expression OP_SHIFT_RIGHT additive_expression
3906           {
3907                 $$ = new Binary (Binary.Operator.RightShift, 
3908                                  (Expression) $1, (Expression) $3);
3909           }
3910         ; 
3911
3912 relational_expression
3913         : shift_expression
3914         | relational_expression OP_LT shift_expression
3915           {
3916                 $$ = new Binary (Binary.Operator.LessThan, 
3917                                  (Expression) $1, (Expression) $3);
3918           }
3919         | relational_expression OP_GT shift_expression
3920           {
3921                 $$ = new Binary (Binary.Operator.GreaterThan, 
3922                                  (Expression) $1, (Expression) $3);
3923           }
3924         | relational_expression OP_LE shift_expression
3925           {
3926                 $$ = new Binary (Binary.Operator.LessThanOrEqual, 
3927                                  (Expression) $1, (Expression) $3);
3928           }
3929         | relational_expression OP_GE shift_expression
3930           {
3931                 $$ = new Binary (Binary.Operator.GreaterThanOrEqual, 
3932                                  (Expression) $1, (Expression) $3);
3933           }
3934         ;
3935
3936 equality_expression
3937         : relational_expression
3938         | equality_expression OP_EQ relational_expression
3939           {
3940                 $$ = new Binary (Binary.Operator.Equality, 
3941                                  (Expression) $1, (Expression) $3);
3942           }
3943         | equality_expression OP_NE relational_expression
3944           {
3945                 $$ = new Binary (Binary.Operator.Inequality, 
3946                                  (Expression) $1, (Expression) $3);
3947           }
3948         ; 
3949
3950 and_expression
3951         : equality_expression
3952         | and_expression BITWISE_AND equality_expression
3953           {
3954                 $$ = new Binary (Binary.Operator.BitwiseAnd, 
3955                                  (Expression) $1, (Expression) $3);
3956           }
3957         ;
3958
3959 exclusive_or_expression
3960         : and_expression
3961         | exclusive_or_expression CARRET and_expression
3962           {
3963                 $$ = new Binary (Binary.Operator.ExclusiveOr, 
3964                                  (Expression) $1, (Expression) $3);
3965           }
3966         ;
3967
3968 inclusive_or_expression
3969         : exclusive_or_expression
3970         | inclusive_or_expression BITWISE_OR exclusive_or_expression
3971           {
3972                 $$ = new Binary (Binary.Operator.BitwiseOr, 
3973                                  (Expression) $1, (Expression) $3);
3974           }
3975         ;
3976
3977 conditional_and_expression
3978         : inclusive_or_expression
3979         | conditional_and_expression OP_AND inclusive_or_expression
3980           {
3981                 $$ = new Binary (Binary.Operator.LogicalAnd, 
3982                                  (Expression) $1, (Expression) $3);
3983           }
3984         ;
3985
3986 conditional_or_expression
3987         : conditional_and_expression
3988         | conditional_or_expression OP_OR conditional_and_expression
3989           {
3990                 $$ = new Binary (Binary.Operator.LogicalOr, 
3991                                  (Expression) $1, (Expression) $3);
3992           }
3993         ;
3994         
3995 null_coalescing_expression
3996         : conditional_or_expression
3997         | conditional_or_expression OP_COALESCING null_coalescing_expression
3998           {
3999                 if (RootContext.Version < LanguageVersion.ISO_2)
4000                         Report.FeatureIsNotAvailable (GetLocation ($2), "null coalescing operator");
4001                         
4002                 $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $3, GetLocation ($2));
4003           }
4004         ;
4005
4006 conditional_expression
4007         : null_coalescing_expression
4008         | null_coalescing_expression INTERR expression COLON expression 
4009           {
4010                 $$ = new Conditional (new BooleanExpression ((Expression) $1), (Expression) $3, (Expression) $5);
4011           }
4012         ;
4013
4014 assignment_expression
4015         : prefixed_unary_expression ASSIGN expression
4016           {
4017                 $$ = new SimpleAssign ((Expression) $1, (Expression) $3);
4018           }
4019         | prefixed_unary_expression OP_MULT_ASSIGN expression
4020           {
4021                 $$ = new CompoundAssign (
4022                         Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
4023           }
4024         | prefixed_unary_expression OP_DIV_ASSIGN expression
4025           {
4026                 $$ = new CompoundAssign (
4027                         Binary.Operator.Division, (Expression) $1, (Expression) $3);
4028           }
4029         | prefixed_unary_expression OP_MOD_ASSIGN expression
4030           {
4031                 $$ = new CompoundAssign (
4032                         Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
4033           }
4034         | prefixed_unary_expression OP_ADD_ASSIGN expression
4035           {
4036                 $$ = new CompoundAssign (
4037                         Binary.Operator.Addition, (Expression) $1, (Expression) $3);
4038           }
4039         | prefixed_unary_expression OP_SUB_ASSIGN expression
4040           {
4041                 $$ = new CompoundAssign (
4042                         Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
4043           }
4044         | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
4045           {
4046                 $$ = new CompoundAssign (
4047                         Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
4048           }
4049         | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
4050           {
4051                 $$ = new CompoundAssign (
4052                         Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
4053           }
4054         | prefixed_unary_expression OP_AND_ASSIGN expression
4055           {
4056                 $$ = new CompoundAssign (
4057                         Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
4058           }
4059         | prefixed_unary_expression OP_OR_ASSIGN expression
4060           {
4061                 $$ = new CompoundAssign (
4062                         Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
4063           }
4064         | prefixed_unary_expression OP_XOR_ASSIGN expression
4065           {
4066                 $$ = new CompoundAssign (
4067                         Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
4068           }
4069         ;
4070
4071 lambda_parameter_list
4072         : lambda_parameter
4073           {
4074                 ArrayList pars = new ArrayList (4);
4075                 pars.Add ($1);
4076
4077                 $$ = pars;
4078           }
4079         | lambda_parameter_list COMMA lambda_parameter
4080           {
4081                 ArrayList pars = (ArrayList) $1;
4082                 Parameter p = (Parameter)$3;
4083                 if (pars[0].GetType () != p.GetType ()) {
4084                         Report.Error (748, p.Location, "All lambda parameters must be typed either explicitly or implicitly");
4085                 }
4086                 
4087                 pars.Add (p);
4088                 $$ = pars;
4089           }
4090         ;
4091
4092 lambda_parameter
4093         : parameter_modifier parameter_type IDENTIFIER
4094           {
4095                 var lt = (Tokenizer.LocatedToken) $3;
4096
4097                 $$ = new Parameter ((FullNamedExpression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
4098           }
4099         | parameter_type IDENTIFIER
4100           {
4101                 var lt = (Tokenizer.LocatedToken) $2;
4102
4103                 $$ = new Parameter ((FullNamedExpression) $1, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
4104           }
4105         | IDENTIFIER
4106           {
4107                 var lt = (Tokenizer.LocatedToken) $1;
4108                 $$ = new ImplicitLambdaParameter (lt.Value, lt.Location);
4109           }
4110         ;
4111
4112 opt_lambda_parameter_list
4113         : /* empty */                   { $$ = ParametersCompiled.EmptyReadOnlyParameters; }
4114         | lambda_parameter_list         { 
4115                 ArrayList pars_list = (ArrayList) $1;
4116                 $$ = new ParametersCompiled (compiler, (Parameter[])pars_list.ToArray (typeof (Parameter)));
4117           }
4118         ;
4119
4120 lambda_expression_body
4121         : {
4122                 start_block (lexer.Location);
4123           }
4124           expression 
4125           {
4126                 Block b = end_block (lexer.Location);
4127                 b.AddStatement (new ContextualReturn ((Expression) $2));
4128                 $$ = b;
4129           } 
4130         | block { 
4131                 $$ = $1; 
4132           } 
4133         ;
4134
4135 lambda_expression
4136         : IDENTIFIER ARROW 
4137           {
4138                 var lt = (Tokenizer.LocatedToken) $1;
4139                 Parameter p = new ImplicitLambdaParameter (lt.Value, lt.Location);
4140                 start_anonymous (true, new ParametersCompiled (compiler, p), GetLocation ($1));
4141           }
4142           lambda_expression_body
4143           {
4144                 $$ = end_anonymous ((ToplevelBlock) $4);
4145           }
4146         | OPEN_PARENS_LAMBDA
4147           {
4148                 valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out;
4149           }
4150           opt_lambda_parameter_list CLOSE_PARENS ARROW 
4151           {
4152                 valid_param_mod = 0;
4153                 start_anonymous (true, (ParametersCompiled) $3, GetLocation ($1));
4154           }
4155           lambda_expression_body 
4156           {
4157                 $$ = end_anonymous ((ToplevelBlock) $7);
4158           }
4159         ;
4160
4161 expression
4162         : assignment_expression 
4163         | non_assignment_expression 
4164         ;
4165         
4166 non_assignment_expression
4167         : conditional_expression
4168         | lambda_expression
4169         | query_expression
4170         ;
4171
4172 constant_expression
4173         : expression
4174         ;
4175
4176 boolean_expression
4177         : expression
4178           {
4179                 $$ = new BooleanExpression ((Expression) $1);
4180           }
4181         ;
4182
4183 //
4184 // 10 classes
4185 //
4186 class_declaration
4187         : opt_attributes
4188           opt_modifiers
4189           opt_partial
4190           CLASS
4191           {
4192                 lexer.ConstraintsParsing = true;
4193           }
4194           type_declaration_name
4195           {
4196                 MemberName name = MakeName ((MemberName) $6);
4197                 push_current_class (new Class (current_namespace, current_class, name, (int) $2, (Attributes) $1), $3);
4198           }
4199           opt_class_base
4200           opt_type_parameter_constraints_clauses
4201           {
4202                 lexer.ConstraintsParsing = false;
4203
4204                 current_class.SetParameterInfo ((ArrayList) $9);
4205
4206                 if (RootContext.Documentation != null) {
4207                         current_container.DocComment = Lexer.consume_doc_comment ();
4208                         Lexer.doc_state = XmlCommentState.Allowed;
4209                 }
4210           }
4211           class_body
4212           {
4213                 --lexer.parsing_declaration;      
4214                 if (RootContext.Documentation != null)
4215                         Lexer.doc_state = XmlCommentState.Allowed;
4216           }
4217           opt_semicolon 
4218           {
4219                 $$ = pop_current_class ();
4220           }
4221         ;       
4222
4223 opt_partial
4224         : /* empty */
4225           { $$ = null; }
4226         | PARTIAL
4227           { $$ = $1; } // location
4228         ;
4229
4230 opt_modifiers
4231         : /* empty */           { $$ = (int) 0; }
4232         | modifiers
4233         ;
4234
4235 modifiers
4236         : modifier
4237         | modifiers modifier
4238           { 
4239                 int m1 = (int) $1;
4240                 int m2 = (int) $2;
4241
4242                 if ((m1 & m2) != 0) {
4243                         Location l = lexer.Location;
4244                         Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
4245                 }
4246                 $$ = (int) (m1 | m2);
4247           }
4248         ;
4249
4250 modifier
4251         : NEW
4252           {
4253                 $$ = Modifiers.NEW;
4254                 if (current_container == RootContext.ToplevelTypes)
4255                         Report.Error (1530, GetLocation ($1), "Keyword `new' is not allowed on namespace elements");
4256           }
4257         | PUBLIC                { $$ = Modifiers.PUBLIC; }
4258         | PROTECTED             { $$ = Modifiers.PROTECTED; }
4259         | INTERNAL              { $$ = Modifiers.INTERNAL; }
4260         | PRIVATE               { $$ = Modifiers.PRIVATE; }
4261         | ABSTRACT              { $$ = Modifiers.ABSTRACT; }
4262         | SEALED                { $$ = Modifiers.SEALED; }
4263         | STATIC                { $$ = Modifiers.STATIC; }
4264         | READONLY              { $$ = Modifiers.READONLY; }
4265         | VIRTUAL               { $$ = Modifiers.VIRTUAL; }
4266         | OVERRIDE              { $$ = Modifiers.OVERRIDE; }
4267         | EXTERN                { $$ = Modifiers.EXTERN; }
4268         | VOLATILE              { $$ = Modifiers.VOLATILE; }
4269         | UNSAFE                { $$ = Modifiers.UNSAFE; }
4270         ;
4271
4272 opt_class_base
4273         : /* empty */
4274         | class_base
4275         ;
4276
4277 class_base
4278         : COLON type_list       { current_container.AddBasesForPart (current_class, (ArrayList) $2); }
4279         ;
4280
4281 opt_type_parameter_constraints_clauses
4282         : /* empty */           { $$ = null; }
4283         | type_parameter_constraints_clauses 
4284           { $$ = $1; }
4285         ;
4286
4287 type_parameter_constraints_clauses
4288         : type_parameter_constraints_clause {
4289                 ArrayList constraints = new ArrayList (1);
4290                 constraints.Add ($1);
4291                 $$ = constraints;
4292           }
4293         | type_parameter_constraints_clauses type_parameter_constraints_clause {
4294                 ArrayList constraints = (ArrayList) $1;
4295                 Constraints new_constraint = (Constraints)$2;
4296
4297                 foreach (Constraints c in constraints) {
4298                         if (new_constraint.TypeParameter == c.TypeParameter) {
4299                                 Report.Error (409, new_constraint.Location, "A constraint clause has already been specified for type parameter `{0}'",
4300                                         new_constraint.TypeParameter);
4301                         }
4302                 }
4303
4304                 constraints.Add (new_constraint);
4305                 $$ = constraints;
4306           }
4307         ; 
4308
4309 type_parameter_constraints_clause
4310         : WHERE IDENTIFIER COLON type_parameter_constraints {
4311                 var lt = (Tokenizer.LocatedToken) $2;
4312                 $$ = new Constraints (lt.Value, (ArrayList) $4, lt.Location);
4313           }
4314         ; 
4315
4316 type_parameter_constraints
4317         : type_parameter_constraint {
4318                 ArrayList constraints = new ArrayList (1);
4319                 constraints.Add ($1);
4320                 $$ = constraints;
4321           }
4322         | type_parameter_constraints COMMA type_parameter_constraint {
4323                 ArrayList constraints = (ArrayList) $1;
4324
4325                 constraints.Add ($3);
4326                 $$ = constraints;
4327           }
4328         ;
4329
4330 type_parameter_constraint
4331         : type
4332         | NEW OPEN_PARENS CLOSE_PARENS {
4333                 $$ = SpecialConstraint.Constructor;
4334           }
4335         | CLASS {
4336                 $$ = SpecialConstraint.ReferenceType;
4337           }
4338         | STRUCT {
4339                 $$ = SpecialConstraint.ValueType;
4340           }
4341         ;
4342
4343 opt_type_parameter_variance
4344         : /* empty */
4345           {
4346                 $$ = Variance.None;
4347           }
4348         | type_parameter_variance
4349           {
4350                 if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)        
4351                         Report.FeatureIsNotSupported (lexer.Location, "generic type variance");
4352                 else if (RootContext.Version <= LanguageVersion.V_3)
4353                         Report.FeatureIsNotAvailable (lexer.Location, "generic type variance");
4354
4355                 $$ = $1;
4356           }
4357         ;
4358
4359 type_parameter_variance
4360         : OUT
4361           {
4362                 $$ = Variance.Covariant;
4363           }
4364         | IN
4365           {
4366                 $$ = Variance.Contravariant;
4367           }
4368         ;
4369
4370 //
4371 // Statements (8.2)
4372 //
4373
4374 //
4375 // A block is "contained" on the following places:
4376 //      method_body
4377 //      property_declaration as part of the accessor body (get/set)
4378 //      operator_declaration
4379 //      constructor_declaration
4380 //      destructor_declaration
4381 //      event_declaration as part of add_accessor_declaration or remove_accessor_declaration
4382 //      
4383 block
4384         : OPEN_BRACE  
4385           {
4386                 ++lexer.parsing_block;
4387                 start_block (GetLocation ($1));
4388           } 
4389           opt_statement_list block_end
4390           {
4391                 $$ = $4;
4392           }
4393         ;
4394
4395 block_end 
4396         : CLOSE_BRACE 
4397           {
4398                 --lexer.parsing_block;
4399                 $$ = end_block (GetLocation ($1));
4400           }
4401         | COMPLETE_COMPLETION
4402           {
4403                 --lexer.parsing_block;
4404                 $$ = end_block (lexer.Location);
4405           }
4406         ;
4407
4408
4409 block_prepared
4410         : OPEN_BRACE
4411           {
4412                 ++lexer.parsing_block;
4413                 current_block.StartLocation = GetLocation ($1);
4414           }
4415           opt_statement_list CLOSE_BRACE 
4416           {
4417                 --lexer.parsing_block;
4418                 $$ = end_block (GetLocation ($4));
4419           }
4420         ;
4421
4422 opt_statement_list
4423         : /* empty */
4424         | statement_list 
4425         ;
4426
4427 statement_list
4428         : statement
4429         | statement_list statement
4430         ;
4431
4432 statement
4433         : declaration_statement
4434           {
4435                 if ($1 != null && (Block) $1 != current_block){
4436                         current_block.AddStatement ((Statement) $1);
4437                         current_block = (Block) $1;
4438                 }
4439           }
4440         | valid_declaration_statement
4441           {
4442                 current_block.AddStatement ((Statement) $1);
4443           }
4444         | labeled_statement
4445         ;
4446
4447 //
4448 // The interactive_statement and its derivatives are only 
4449 // used to provide a special version of `expression_statement'
4450 // that has a side effect of assigning the expression to
4451 // $retval
4452 //
4453 interactive_statement_list
4454         : interactive_statement
4455         | interactive_statement_list interactive_statement
4456         ;
4457
4458 interactive_statement
4459         : declaration_statement
4460           {
4461                 if ($1 != null && (Block) $1 != current_block){
4462                         current_block.AddStatement ((Statement) $1);
4463                         current_block = (Block) $1;
4464                 }
4465           }
4466         | interactive_valid_declaration_statement
4467           {
4468                 current_block.AddStatement ((Statement) $1);
4469           }
4470         | labeled_statement
4471         ;
4472
4473 valid_declaration_statement
4474         : block
4475         | empty_statement
4476         | expression_statement
4477         | selection_statement
4478         | iteration_statement
4479         | jump_statement                  
4480         | try_statement
4481         | checked_statement
4482         | unchecked_statement
4483         | lock_statement
4484         | using_statement
4485         | unsafe_statement
4486         | fixed_statement
4487         ;
4488
4489 interactive_valid_declaration_statement
4490         : block
4491         | empty_statement
4492         | interactive_expression_statement
4493         | selection_statement
4494         | iteration_statement
4495         | jump_statement                  
4496         | try_statement
4497         | checked_statement
4498         | unchecked_statement
4499         | lock_statement
4500         | using_statement
4501         | unsafe_statement
4502         | fixed_statement
4503         ;
4504
4505 embedded_statement
4506         : valid_declaration_statement
4507         | declaration_statement
4508           {
4509                   Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4510                   $$ = null;
4511           }
4512         | labeled_statement
4513           {
4514                   Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4515                   $$ = null;
4516           }
4517         ;
4518
4519 empty_statement
4520         : SEMICOLON
4521           {
4522                   $$ = EmptyStatement.Value;
4523           }
4524         ;
4525
4526 labeled_statement
4527         : IDENTIFIER COLON 
4528           {
4529                 var lt = (Tokenizer.LocatedToken) $1;
4530                 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
4531
4532                 if (current_block.AddLabel (labeled))
4533                         current_block.AddStatement (labeled);
4534           }
4535           statement
4536         ;
4537
4538 declaration_statement
4539         : local_variable_declaration SEMICOLON
4540           {
4541                 current_array_type = null;
4542                 if ($1 != null){
4543                         DictionaryEntry de = (DictionaryEntry) $1;
4544                         Expression e = (Expression) de.Key;
4545
4546                         $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
4547                 }
4548           }
4549
4550         | local_constant_declaration SEMICOLON
4551           {
4552                 current_array_type = null;
4553                 if ($1 != null){
4554                         DictionaryEntry de = (DictionaryEntry) $1;
4555
4556                         $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
4557                 }
4558           }
4559         ;
4560
4561 /* 
4562  * The following is from Rhys' grammar:
4563  * > Types in local variable declarations must be recognized as 
4564  * > expressions to prevent reduce/reduce errors in the grammar.
4565  * > The expressions are converted into types during semantic analysis.
4566  */
4567 variable_type
4568         : primary_expression_no_array_creation opt_rank_specifier_or_nullable
4569           { 
4570                 // FIXME: Do something smart here regarding the composition of the type.
4571
4572                 // Ok, the above "primary_expression" is there to get rid of
4573                 // both reduce/reduce and shift/reduces in the grammar, it should
4574                 // really just be "type_name".  If you use type_name, a reduce/reduce
4575                 // creeps up.  If you use namespace_or_type_name (which is all we need
4576                 // really) two shift/reduces appear.
4577                 // 
4578
4579                 // So the super-trick is that primary_expression
4580                 // can only be either a SimpleName or a MemberAccess. 
4581                 // The MemberAccess case arises when you have a fully qualified type-name like :
4582                 // Foo.Bar.Blah i;
4583                 // SimpleName is when you have
4584                 // Blah i;
4585                 
4586                 Expression expr = (Expression) $1;
4587                 string rank_or_nullable = (string) $2;
4588                 
4589                 if (expr is ComposedCast){
4590                         $$ = new ComposedCast ((ComposedCast)expr, rank_or_nullable);
4591                 } else if (expr is ATypeNameExpression){
4592                         //
4593                         // So we extract the string corresponding to the SimpleName
4594                         // or MemberAccess
4595                         //
4596                         if (rank_or_nullable.Length == 0) {
4597                                 SimpleName sn = expr as SimpleName;
4598                                 if (sn != null && sn.Name == "var")
4599                                         $$ = current_array_type = new VarExpr (sn.Location);
4600                                 else
4601                                         $$ = $1;
4602                         } else {
4603                                 $$ = new ComposedCast ((ATypeNameExpression)expr, rank_or_nullable);
4604                         }
4605                 } else {
4606                         Error_ExpectingTypeName (expr);
4607                         $$ = TypeManager.system_object_expr;
4608                 }
4609           }
4610         | builtin_types opt_rank_specifier_or_nullable
4611           {
4612                 if ((string) $2 == "")
4613                         $$ = $1;
4614                 else
4615                         $$ = current_array_type = new ComposedCast ((FullNamedExpression) $1, (string) $2, lexer.Location);
4616           }
4617         | VOID opt_rank_specifier
4618           {
4619                 Expression.Error_VoidInvalidInTheContext (GetLocation ($1), Report);
4620                 $$ = TypeManager.system_void_expr;
4621           }
4622         ;
4623
4624 local_variable_pointer_type
4625         : primary_expression_no_array_creation STAR
4626           {
4627                 ATypeNameExpression expr = $1 as ATypeNameExpression;
4628
4629                 if (expr != null) {
4630                         $$ = new ComposedCast (expr, "*");
4631                 } else {
4632                         Error_ExpectingTypeName ((Expression)$1);
4633                         $$ = expr;
4634                 }
4635           }
4636         | builtin_types STAR
4637           {
4638                 $$ = new ComposedCast ((FullNamedExpression) $1, "*", GetLocation ($1));
4639           }
4640         | VOID STAR
4641           {
4642                 $$ = new ComposedCast (TypeManager.system_void_expr, "*", GetLocation ($1));
4643           }
4644         | local_variable_pointer_type STAR
4645           {
4646                 $$ = new ComposedCast ((FullNamedExpression) $1, "*");
4647           }
4648         ;
4649
4650 local_variable_type
4651         : variable_type
4652         | local_variable_pointer_type opt_rank_specifier
4653           {
4654                 if ($1 != null){
4655                         string rank = (string)$2;
4656
4657                         if (rank == "")
4658                                 $$ = $1;
4659                         else
4660                                 $$ = current_array_type = new ComposedCast ((FullNamedExpression) $1, rank);
4661                 } else {
4662                         $$ = null;
4663                 }
4664           }
4665         ;
4666
4667 local_variable_declaration
4668         : local_variable_type local_variable_declarators
4669           {
4670                 if ($1 != null) {
4671                         VarExpr ve = $1 as VarExpr;
4672                         if (ve != null) {
4673                                 if (((VariableDeclaration)((ArrayList)$2) [0]).expression_or_array_initializer == null)
4674                                         ve.VariableInitializersCount = 0;
4675                                 else
4676                                         ve.VariableInitializersCount = ((ArrayList)$2).Count;
4677                         }
4678                                 
4679                         $$ = new DictionaryEntry ($1, $2);
4680                 } else
4681                         $$ = null;
4682           }
4683         ;
4684
4685 local_constant_declaration
4686         : CONST variable_type constant_declarators
4687           {
4688                 if ($2 != null)
4689                         $$ = new DictionaryEntry ($2, $3);
4690                 else
4691                         $$ = null;
4692           }
4693         ;
4694
4695 expression_statement
4696         : statement_expression SEMICOLON { $$ = $1; }
4697         | statement_expression COMPLETE_COMPLETION { $$ = $1; }
4698         ;
4699
4700 interactive_expression_statement
4701         : interactive_statement_expression SEMICOLON { $$ = $1; }
4702         | interactive_statement_expression COMPLETE_COMPLETION { $$ = $1; }
4703         ;
4704
4705         //
4706         // We have to do the wrapping here and not in the case above,
4707         // because statement_expression is used for example in for_statement
4708         //
4709 statement_expression
4710         : expression
4711           {
4712                 ExpressionStatement s = $1 as ExpressionStatement;
4713                 if (s == null) {
4714                         Expression.Error_InvalidExpressionStatement (Report, GetLocation ($1));
4715                         s = EmptyExpressionStatement.Instance;
4716                 }
4717
4718                 $$ = new StatementExpression (s);
4719           }
4720         | error
4721           {
4722                 Error_SyntaxError (yyToken);
4723                 $$ = null;
4724           }
4725         ;
4726
4727 interactive_statement_expression
4728         : expression
4729           {
4730                 Expression expr = (Expression) $1;
4731                 ExpressionStatement s;
4732
4733                 s = new OptionalAssign (new SimpleName ("$retval", lexer.Location), expr, lexer.Location);
4734                 $$ = new StatementExpression (s);
4735           }
4736         | error
4737           {
4738                 Error_SyntaxError (yyToken);
4739                 $$ = null;
4740           }
4741         ;
4742         
4743 selection_statement
4744         : if_statement
4745         | switch_statement
4746         ; 
4747
4748 if_statement
4749         : IF open_parens_any boolean_expression CLOSE_PARENS 
4750           embedded_statement
4751           { 
4752                 Location l = GetLocation ($1);
4753
4754                 $$ = new If ((BooleanExpression) $3, (Statement) $5, l);
4755
4756                 // FIXME: location for warning should be loc property of $5.
4757                 if ($5 == EmptyStatement.Value)
4758                         Report.Warning (642, 3, l, "Possible mistaken empty statement");
4759
4760           }
4761         | IF open_parens_any boolean_expression CLOSE_PARENS
4762           embedded_statement ELSE embedded_statement
4763           {
4764                 Location l = GetLocation ($1);
4765
4766                 $$ = new If ((BooleanExpression) $3, (Statement) $5, (Statement) $7, l);
4767
4768                 // FIXME: location for warning should be loc property of $5 and $7.
4769                 if ($5 == EmptyStatement.Value)
4770                         Report.Warning (642, 3, l, "Possible mistaken empty statement");
4771                 if ($7 == EmptyStatement.Value)
4772                         Report.Warning (642, 3, l, "Possible mistaken empty statement");
4773           }
4774         ;
4775
4776 switch_statement
4777         : SWITCH open_parens_any
4778           { 
4779                 if (switch_stack == null)
4780                         switch_stack = new Stack (2);
4781                 switch_stack.Push (current_block);
4782           }
4783           expression CLOSE_PARENS 
4784           switch_block
4785           {
4786                 $$ = new Switch ((Expression) $4, (ArrayList) $6, GetLocation ($1));
4787                 current_block = (Block) switch_stack.Pop ();
4788           }
4789         ;
4790
4791 switch_block
4792         : OPEN_BRACE
4793           opt_switch_sections
4794           CLOSE_BRACE
4795           {
4796                 $$ = $2;
4797           }
4798         ;
4799
4800 opt_switch_sections
4801         : /* empty */           
4802           {
4803                 Report.Warning (1522, 1, lexer.Location, "Empty switch block"); 
4804                 $$ = new ArrayList ();
4805           }
4806         | switch_sections
4807         ;
4808
4809 switch_sections
4810         : switch_section 
4811           {
4812                 ArrayList sections = new ArrayList (4);
4813
4814                 sections.Add ($1);
4815                 $$ = sections;
4816           }
4817         | switch_sections switch_section
4818           {
4819                 ArrayList sections = (ArrayList) $1;
4820
4821                 sections.Add ($2);
4822                 $$ = sections;
4823           }
4824         ;
4825
4826 switch_section
4827         : switch_labels
4828           {
4829                 current_block = current_block.CreateSwitchBlock (lexer.Location);
4830           }
4831           statement_list 
4832           {
4833                 $$ = new SwitchSection ((ArrayList) $1, current_block.Explicit);
4834           }
4835         ;
4836
4837 switch_labels
4838         : switch_label 
4839           {
4840                 ArrayList labels = new ArrayList (4);
4841
4842                 labels.Add ($1);
4843                 $$ = labels;
4844           }
4845         | switch_labels switch_label 
4846           {
4847                 ArrayList labels = (ArrayList) ($1);
4848                 labels.Add ($2);
4849
4850                 $$ = labels;
4851           }
4852         ;
4853
4854 switch_label
4855         : CASE constant_expression COLON
4856          {
4857                 $$ = new SwitchLabel ((Expression) $2, GetLocation ($1));
4858          }
4859         | DEFAULT_COLON
4860           {
4861                 $$ = new SwitchLabel (null, GetLocation ($1));
4862           }
4863         ;
4864
4865 iteration_statement
4866         : while_statement
4867         | do_statement
4868         | for_statement
4869         | foreach_statement
4870         ;
4871
4872 while_statement
4873         : WHILE open_parens_any boolean_expression CLOSE_PARENS embedded_statement
4874           {
4875                 Location l = GetLocation ($1);
4876                 $$ = new While ((BooleanExpression) $3, (Statement) $5, l);
4877           }
4878         ;
4879
4880 do_statement
4881         : DO embedded_statement 
4882           WHILE open_parens_any boolean_expression CLOSE_PARENS SEMICOLON
4883           {
4884                 Location l = GetLocation ($1);
4885
4886                 $$ = new Do ((Statement) $2, (BooleanExpression) $5, l);
4887           }
4888         ;
4889
4890 for_statement
4891         : FOR open_parens_any opt_for_initializer SEMICOLON
4892           {
4893                 Location l = lexer.Location;
4894                 start_block (l);  
4895                 Block assign_block = current_block;
4896
4897                 if ($3 is DictionaryEntry){
4898                         DictionaryEntry de = (DictionaryEntry) $3;
4899                         
4900                         Expression type = (Expression) de.Key;
4901                         ArrayList var_declarators = (ArrayList) de.Value;
4902
4903                         foreach (VariableDeclaration decl in var_declarators){
4904
4905                                 LocalInfo vi;
4906
4907                                 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4908                                 if (vi == null)
4909                                         continue;
4910
4911                                 Expression expr = decl.expression_or_array_initializer;
4912                                         
4913                                 LocalVariableReference var;
4914                                 var = new LocalVariableReference (assign_block, decl.identifier, l);
4915
4916                                 if (expr != null) {
4917                                         Assign a = new SimpleAssign (var, expr, decl.Location);
4918                                         
4919                                         assign_block.AddStatement (new StatementExpression (a));
4920                                 }
4921                         }
4922                         
4923                         // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4924                         // This can be referred to as $5 below.
4925                         $$ = null;
4926                 } else {
4927                         $$ = $3;
4928                 }
4929           } 
4930           opt_for_condition SEMICOLON
4931           opt_for_iterator CLOSE_PARENS 
4932           embedded_statement
4933           {
4934                 Location l = GetLocation ($1);
4935
4936                 For f = new For ((Statement) $5, (BooleanExpression) $6, (Statement) $8, (Statement) $10, l);
4937
4938                 current_block.AddStatement (f);
4939
4940                 $$ = end_block (lexer.Location);
4941           }
4942         ;
4943
4944 opt_for_initializer
4945         : /* empty */           { $$ = EmptyStatement.Value; }
4946         | for_initializer       
4947         ;
4948
4949 for_initializer
4950         : local_variable_declaration
4951         | statement_expression_list
4952         ;
4953
4954 opt_for_condition
4955         : /* empty */           { $$ = null; }
4956         | boolean_expression
4957         ;
4958
4959 opt_for_iterator
4960         : /* empty */           { $$ = EmptyStatement.Value; }
4961         | for_iterator
4962         ;
4963
4964 for_iterator
4965         : statement_expression_list
4966         ;
4967
4968 statement_expression_list
4969         : statement_expression  
4970           {
4971                 // CHANGE: was `null'
4972                 Statement s = (Statement) $1;
4973                 Block b = new Block (current_block, s.loc, lexer.Location);   
4974
4975                 b.AddStatement (s);
4976                 $$ = b;
4977           }
4978         | statement_expression_list COMMA statement_expression
4979           {
4980                 Block b = (Block) $1;
4981
4982                 b.AddStatement ((Statement) $3);
4983                 $$ = $1;
4984           }
4985         ;
4986
4987 foreach_statement
4988         : FOREACH open_parens_any type IN expression CLOSE_PARENS
4989           {
4990                 Report.Error (230, GetLocation ($1), "Type and identifier are both required in a foreach statement");
4991                 $$ = null;
4992           }
4993         | FOREACH open_parens_any type IDENTIFIER IN
4994           expression CLOSE_PARENS 
4995           {
4996                 start_block (lexer.Location);
4997                 Block foreach_block = current_block;
4998
4999                 var lt = (Tokenizer.LocatedToken) $4;
5000                 Location l = lt.Location;
5001                 LocalInfo vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
5002                 if (vi != null) {
5003                         vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
5004
5005                         // Get a writable reference to this read-only variable.
5006                         //
5007                         // Note that the $$ here refers to the value of _this_ code block,
5008                         // not the value of the LHS non-terminal.  This can be referred to as $8 below.
5009                         $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
5010                 } else {
5011                         $$ = null;
5012                 }
5013           } 
5014           embedded_statement 
5015           {
5016                 LocalVariableReference v = (LocalVariableReference) $8;
5017                 Location l = GetLocation ($1);
5018
5019                 if (v != null) {
5020                         Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
5021                         current_block.AddStatement (f);
5022                 }
5023
5024                 $$ = end_block (lexer.Location);
5025           }
5026         ;
5027
5028 jump_statement
5029         : break_statement
5030         | continue_statement
5031         | goto_statement
5032         | return_statement
5033         | throw_statement
5034         | yield_statement
5035         ;
5036
5037 break_statement
5038         : BREAK SEMICOLON
5039           {
5040                 $$ = new Break (GetLocation ($1));
5041           }
5042         ;
5043
5044 continue_statement
5045         : CONTINUE SEMICOLON
5046           {
5047                 $$ = new Continue (GetLocation ($1));
5048           }
5049         ;
5050
5051 goto_statement
5052         : GOTO IDENTIFIER SEMICOLON 
5053           {
5054                 var lt = (Tokenizer.LocatedToken) $2;
5055                 $$ = new Goto (lt.Value, lt.Location);
5056           }
5057         | GOTO CASE constant_expression SEMICOLON
5058           {
5059                 $$ = new GotoCase ((Expression) $3, GetLocation ($1));
5060           }
5061         | GOTO DEFAULT SEMICOLON 
5062           {
5063                 $$ = new GotoDefault (GetLocation ($1));
5064           }
5065         ; 
5066
5067 return_statement
5068         : RETURN opt_expression SEMICOLON
5069           {
5070                 $$ = new Return ((Expression) $2, GetLocation ($1));
5071           }
5072         ;
5073
5074 throw_statement
5075         : THROW opt_expression SEMICOLON
5076           {
5077                 $$ = new Throw ((Expression) $2, GetLocation ($1));
5078           }
5079         ;
5080
5081 yield_statement 
5082         : IDENTIFIER RETURN expression SEMICOLON
5083           {
5084                 var lt = (Tokenizer.LocatedToken) $1;
5085                 string s = lt.Value;
5086                 if (s != "yield"){
5087                         Report.Error (1003, lt.Location, "; expected");
5088                         $$ = null;
5089                 }
5090                 if (RootContext.Version == LanguageVersion.ISO_1){
5091                         Report.FeatureIsNotAvailable (lt.Location, "yield statement");
5092                         $$ = null;
5093                 }
5094                 current_block.Toplevel.IsIterator = true;
5095                 $$ = new Yield ((Expression) $3, lt.Location); 
5096           }
5097         | IDENTIFIER RETURN SEMICOLON
5098           {
5099                 Report.Error (1627, GetLocation ($2), "Expression expected after yield return");
5100                 $$ = null;
5101           }
5102         | IDENTIFIER BREAK SEMICOLON
5103           {
5104                 var lt = (Tokenizer.LocatedToken) $1;
5105                 string s = lt.Value;
5106                 if (s != "yield"){
5107                         Report.Error (1003, lt.Location, "; expected");
5108                         $$ = null;
5109                 }
5110                 if (RootContext.Version == LanguageVersion.ISO_1){
5111                         Report.FeatureIsNotAvailable (lt.Location, "yield statement");
5112                         $$ = null;
5113                 }
5114                 
5115                 current_block.Toplevel.IsIterator = true;
5116                 $$ = new YieldBreak (lt.Location);
5117           }
5118         ;
5119
5120 opt_expression
5121         : /* empty */
5122         | expression
5123         ;
5124
5125 try_statement
5126         : TRY block catch_clauses
5127           {
5128                 $$ = new TryCatch ((Block) $2, (ArrayList) $3, GetLocation ($1), false);
5129           }
5130         | TRY block FINALLY block
5131           {
5132                 $$ = new TryFinally ((Statement) $2, (Block) $4, GetLocation ($1));
5133           }
5134         | TRY block catch_clauses FINALLY block
5135           {
5136                 $$ = new TryFinally (new TryCatch ((Block) $2, (ArrayList) $3, GetLocation ($1), true), (Block) $5, GetLocation ($1));
5137           }
5138         | TRY block error 
5139           {
5140                 Report.Error (1524, GetLocation ($1), "Expected catch or finally");
5141                 $$ = null;
5142           }
5143         ;
5144
5145 catch_clauses
5146         : catch_clause 
5147           {
5148                 ArrayList l = new ArrayList (4);
5149
5150                 l.Add ($1);
5151                 $$ = l;
5152           }
5153         | catch_clauses catch_clause
5154           {
5155                 ArrayList l = (ArrayList) $1;
5156                 
5157                 Catch c = (Catch) $2;
5158                 if (((Catch) l [0]).IsGeneral) {
5159                         Report.Error (1017, c.loc, "Try statement already has an empty catch block");
5160                 } else {
5161                         if (c.IsGeneral)
5162                                 l.Insert (0, $2);
5163                         else
5164                                 l.Add ($2);
5165                 }
5166                 
5167                 $$ = l;
5168           }
5169         ;
5170
5171 opt_identifier
5172         : /* empty */   { $$ = null; }
5173         | IDENTIFIER
5174         ;
5175
5176 catch_clause 
5177         : CATCH opt_catch_args 
5178           {
5179                 Expression type = null;
5180                 
5181                 if ($2 != null) {
5182                         DictionaryEntry cc = (DictionaryEntry) $2;
5183                         type = (Expression) cc.Key;
5184                         var lt = (Tokenizer.LocatedToken) cc.Value;
5185
5186                         if (lt != null){
5187                                 ArrayList one = new ArrayList (2);
5188
5189                                 one.Add (new VariableDeclaration (lt, null));
5190
5191                                 start_block (lexer.Location);
5192                                 current_block = declare_local_variables (type, one, lt.Location);
5193                         }
5194                 }
5195           } block {
5196                 Expression type = null;
5197                 string id = null;
5198                 Block var_block = null;
5199
5200                 if ($2 != null){
5201                         DictionaryEntry cc = (DictionaryEntry) $2;
5202                         type = (Expression) cc.Key;
5203                         var lt = (Tokenizer.LocatedToken) cc.Value;
5204
5205                         if (lt != null){
5206                                 id = lt.Value;
5207                                 var_block = end_block (lexer.Location);
5208                         }
5209                 }
5210
5211                 $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
5212           }
5213         ;
5214
5215 opt_catch_args
5216         : /* empty */ { $$ = null; }
5217         | catch_args
5218         ;         
5219
5220 catch_args 
5221         : open_parens_any type opt_identifier CLOSE_PARENS 
5222           {
5223                 $$ = new DictionaryEntry ($2, $3);
5224           }
5225         | open_parens_any CLOSE_PARENS 
5226           {
5227                 Report.Error (1015, GetLocation ($1), "A type that derives from `System.Exception', `object', or `string' expected");
5228                 $$ = null;
5229           }
5230         ;
5231
5232 checked_statement
5233         : CHECKED block
5234           {
5235                 $$ = new Checked ((Block) $2);
5236           }
5237         ;
5238
5239 unchecked_statement
5240         : UNCHECKED block
5241           {
5242                 $$ = new Unchecked ((Block) $2);
5243           }
5244         ;
5245
5246 unsafe_statement
5247         : UNSAFE 
5248           {
5249                 RootContext.CheckUnsafeOption (GetLocation ($1), Report);
5250           } block {
5251                 $$ = new Unsafe ((Block) $3);
5252           }
5253         ;
5254
5255 fixed_statement
5256         : FIXED open_parens_any 
5257           type_and_void fixed_pointer_declarators 
5258           CLOSE_PARENS
5259           {
5260                 ArrayList list = (ArrayList) $4;
5261                 Expression type = (Expression) $3;
5262                 Location l = GetLocation ($1);
5263                 int top = list.Count;
5264
5265                 start_block (lexer.Location);
5266
5267                 for (int i = 0; i < top; i++){
5268                         Pair p = (Pair) list [i];
5269                         LocalInfo v;
5270
5271                         v = current_block.AddVariable (type, (string) p.First, l);
5272                         if (v == null)
5273                                 continue;
5274
5275                         v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
5276                         v.Pinned = true;
5277                         p.First = v;
5278                         list [i] = p;
5279                 }
5280           }
5281           embedded_statement 
5282           {
5283                 Location l = GetLocation ($1);
5284
5285                 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
5286
5287                 current_block.AddStatement (f);
5288
5289                 $$ = end_block (lexer.Location);
5290           }
5291         ;
5292
5293 fixed_pointer_declarators
5294         : fixed_pointer_declarator      { 
5295                 ArrayList declarators = new ArrayList (4);
5296                 if ($1 != null)
5297                         declarators.Add ($1);
5298                 $$ = declarators;
5299           }
5300         | fixed_pointer_declarators COMMA fixed_pointer_declarator
5301           {
5302                 ArrayList declarators = (ArrayList) $1;
5303                 if ($3 != null)
5304                         declarators.Add ($3);
5305                 $$ = declarators;
5306           }
5307         ;
5308
5309 fixed_pointer_declarator
5310         : IDENTIFIER ASSIGN expression
5311           {
5312                 var lt = (Tokenizer.LocatedToken) $1;
5313                 // FIXME: keep location
5314                 $$ = new Pair (lt.Value, $3);
5315           }
5316         | IDENTIFIER
5317           {
5318                 Report.Error (210, ((Tokenizer.LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
5319                 $$ = null;
5320           }
5321         ;
5322
5323 lock_statement
5324         : LOCK open_parens_any expression CLOSE_PARENS 
5325           {
5326                 //
5327           } 
5328           embedded_statement
5329           {
5330                 $$ = new Lock ((Expression) $3, (Statement) $6, GetLocation ($1));
5331           }
5332         ;
5333
5334 using_statement
5335         : USING open_parens_any local_variable_declaration CLOSE_PARENS
5336           {
5337                 start_block (lexer.Location);
5338                 Block assign_block = current_block;
5339
5340                 DictionaryEntry de = (DictionaryEntry) $3;
5341                 Location l = GetLocation ($1);
5342
5343                 Expression type = (Expression) de.Key;
5344                 ArrayList var_declarators = (ArrayList) de.Value;
5345
5346                 Stack vars = new Stack ();
5347
5348                 foreach (VariableDeclaration decl in var_declarators) {
5349                         LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
5350                         if (vi == null)
5351                                 continue;
5352                         vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
5353
5354                         Expression expr = decl.expression_or_array_initializer;
5355                         if (expr == null) {
5356                                 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
5357                                 continue;
5358                         }
5359                         LocalVariableReference var;
5360
5361                         // Get a writable reference to this read-only variable.
5362                         var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
5363
5364                         // This is so that it is not a warning on using variables
5365                         vi.Used = true;
5366
5367                         vars.Push (new DictionaryEntry (var, expr));
5368
5369                         // Assign a = new SimpleAssign (var, expr, decl.Location);
5370                         // assign_block.AddStatement (new StatementExpression (a));
5371                 }
5372
5373                 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
5374                 // It can be referred to as $5 below.
5375                 $$ = vars;
5376           }
5377           embedded_statement
5378           {
5379                 Statement stmt = (Statement) $6;
5380                 Stack vars = (Stack) $5;
5381                 Location l = GetLocation ($1);
5382
5383                 while (vars.Count > 0) {
5384                           DictionaryEntry de = (DictionaryEntry) vars.Pop ();
5385                           stmt = new Using ((Expression) de.Key, (Expression) de.Value, stmt, l);
5386                 }
5387                 current_block.AddStatement (stmt);
5388                 $$ = end_block (lexer.Location);
5389           }
5390         | USING open_parens_any expression CLOSE_PARENS
5391           {
5392                 start_block (lexer.Location);
5393           }
5394           embedded_statement
5395           {
5396                 current_block.AddStatement (new UsingTemporary ((Expression) $3, (Statement) $6, GetLocation ($1)));
5397                 $$ = end_block (lexer.Location);
5398           }
5399         ; 
5400
5401
5402 // LINQ
5403
5404 query_expression
5405         : first_from_clause query_body
5406           {
5407                 lexer.query_parsing = false;
5408                         
5409                 Linq.AQueryClause from = $1 as Linq.AQueryClause;
5410                         
5411                 from.Tail.Next = (Linq.AQueryClause)$2;
5412                 $$ = from;
5413                 
5414                 current_block.SetEndLocation (lexer.Location);
5415                 current_block = current_block.Parent;
5416           }
5417         | nested_from_clause query_body
5418           {
5419                 Linq.AQueryClause from = $1 as Linq.AQueryClause;
5420                         
5421                 from.Tail.Next = (Linq.AQueryClause)$2;
5422                 $$ = from;
5423                 
5424                 current_block.SetEndLocation (lexer.Location);
5425                 current_block = current_block.Parent;
5426           }     
5427         ;
5428         
5429 first_from_clause
5430         : FROM_FIRST IDENTIFIER IN expression
5431           {
5432                 $$ = new Linq.QueryExpression (current_block, new Linq.QueryStartClause ((Expression)$4));
5433                 var lt = (Tokenizer.LocatedToken) $2;
5434                 current_block = new Linq.QueryBlock (compiler, current_block, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1));
5435           }
5436         | FROM_FIRST type IDENTIFIER IN expression
5437           {
5438                 var lt = (Tokenizer.LocatedToken) $3;
5439                 $$ = new Linq.QueryExpression (current_block, new Linq.Cast ((FullNamedExpression)$2, (Expression)$5));
5440                 current_block = new Linq.QueryBlock (compiler, current_block, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1));
5441           }
5442         ;
5443
5444 nested_from_clause
5445         : FROM IDENTIFIER IN expression
5446           {
5447                 $$ = new Linq.QueryExpression (current_block, new Linq.QueryStartClause ((Expression)$4));
5448                 var lt = (Tokenizer.LocatedToken) $2;
5449                 current_block = new Linq.QueryBlock (compiler, current_block, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1));
5450           }
5451         | FROM type IDENTIFIER IN expression
5452           {
5453                 $$ = new Linq.QueryExpression (current_block, new Linq.Cast ((FullNamedExpression)$2, (Expression)$5));
5454                 var lt = (Tokenizer.LocatedToken) $3;
5455                 current_block = new Linq.QueryBlock (compiler, current_block, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1));
5456           }
5457         ;
5458         
5459 from_clause
5460         : FROM IDENTIFIER IN
5461           {
5462                 current_block = new Linq.QueryBlock (compiler, current_block, GetLocation ($1));
5463           }
5464           expression
5465           {
5466                 var lt = (Tokenizer.LocatedToken) $2;
5467                 var sn = new SimpleMemberName (lt.Value, lt.Location);
5468                 $$ = new Linq.SelectMany (current_block.Toplevel, sn, (Expression)$5);
5469                 
5470                 current_block.SetEndLocation (lexer.Location);
5471                 current_block = current_block.Parent;
5472                 
5473                 ((Linq.QueryBlock)current_block).AddTransparentParameter (compiler, sn);
5474           }       
5475         | FROM type IDENTIFIER IN
5476           {
5477                 current_block = new Linq.QueryBlock (compiler, current_block, GetLocation ($1));
5478           }
5479           expression
5480           {
5481                 var lt = (Tokenizer.LocatedToken) $3;
5482                 var sn = new SimpleMemberName (lt.Value, lt.Location);
5483
5484                 FullNamedExpression type = (FullNamedExpression)$2;
5485                 
5486                 $$ = new Linq.SelectMany (current_block.Toplevel, sn, new Linq.Cast (type, (FullNamedExpression)$6));
5487                 
5488                 current_block.SetEndLocation (lexer.Location);
5489                 current_block = current_block.Parent;
5490                 
5491                 ((Linq.QueryBlock)current_block).AddTransparentParameter (compiler, sn);
5492           }
5493         ;       
5494
5495 query_body
5496         : opt_query_body_clauses select_or_group_clause opt_query_continuation
5497           {
5498                 Linq.AQueryClause head = (Linq.AQueryClause)$2;
5499                 
5500                 if ($3 != null)
5501                         head.Next = (Linq.AQueryClause)$3;
5502                                 
5503                 if ($1 != null) {
5504                         Linq.AQueryClause clause = (Linq.AQueryClause)$1;
5505                         clause.Tail.Next = head;
5506                         head = clause;
5507                 }
5508                 
5509                 $$ = head;
5510           }
5511         ;
5512         
5513 select_or_group_clause
5514         : SELECT
5515           {
5516                 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5517           }
5518           expression
5519           {
5520                 $$ = new Linq.Select (current_block.Toplevel, (Expression)$3, GetLocation ($1));
5521
5522                 current_block.SetEndLocation (lexer.Location);
5523                 current_block = current_block.Parent;
5524           }
5525         | GROUP
5526           {
5527                 if (linq_clause_blocks == null)
5528                         linq_clause_blocks = new Stack ();
5529                         
5530                 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5531                 linq_clause_blocks.Push (current_block);
5532           }
5533           expression
5534           {
5535                 current_block.SetEndLocation (lexer.Location);
5536                 current_block = current_block.Parent;
5537           
5538                 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5539           }
5540           BY expression
5541           {
5542                 $$ = new Linq.GroupBy (current_block.Toplevel, (Expression)$3, (ToplevelBlock) linq_clause_blocks.Pop (), (Expression)$6, GetLocation ($1));
5543                 
5544                 current_block.SetEndLocation (lexer.Location);
5545                 current_block = current_block.Parent;
5546           }
5547         ;
5548         
5549 opt_query_body_clauses
5550         : /* empty */
5551         | query_body_clauses
5552         ;
5553         
5554 query_body_clauses
5555         : query_body_clause
5556         | query_body_clauses query_body_clause
5557           {
5558                 ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$2;
5559                 $$ = $1;
5560           }
5561         ;
5562         
5563 query_body_clause
5564         : from_clause
5565         | let_clause
5566         | where_clause
5567         | join_clause
5568         | orderby_clause
5569         ;
5570         
5571 let_clause
5572         : LET IDENTIFIER ASSIGN 
5573           {
5574                 current_block = new Linq.QueryBlock (compiler, current_block, GetLocation ($1));
5575           }
5576           expression
5577           {
5578                 var lt = (Tokenizer.LocatedToken) $2;
5579                 var sn = new SimpleMemberName (lt.Value, lt.Location);
5580                 $$ = new Linq.Let (current_block.Toplevel, current_container, sn, (Expression)$5);
5581                 
5582                 current_block.SetEndLocation (lexer.Location);
5583                 current_block = current_block.Parent;
5584                 
5585                 ((Linq.QueryBlock)current_block).AddTransparentParameter (compiler, sn);
5586           }
5587         ;
5588
5589 where_clause
5590         : WHERE
5591           {
5592                 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5593           }
5594           boolean_expression
5595           {
5596                 $$ = new Linq.Where (current_block.Toplevel, (BooleanExpression)$3, GetLocation ($1));
5597
5598                 current_block.SetEndLocation (lexer.Location);
5599                 current_block = current_block.Parent;
5600           }
5601         ;
5602         
5603 join_clause
5604         : JOIN IDENTIFIER IN
5605           {
5606                 if (linq_clause_blocks == null)
5607                         linq_clause_blocks = new Stack ();
5608                         
5609                 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5610                 linq_clause_blocks.Push (current_block);
5611           }
5612           expression ON
5613           {
5614                 current_block.SetEndLocation (lexer.Location);
5615                 current_block = current_block.Parent;
5616
5617                 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5618                 linq_clause_blocks.Push (current_block);
5619           }
5620           expression EQUALS
5621           {
5622                 current_block.AddStatement (new ContextualReturn ((Expression) $8));
5623                 current_block.SetEndLocation (lexer.Location);
5624                 current_block = current_block.Parent;
5625
5626                 var lt = (Tokenizer.LocatedToken) $2;
5627                 current_block = new Linq.QueryBlock (compiler, current_block, new SimpleMemberName (lt.Value, lt.Location), lexer.Location);
5628           }
5629           expression opt_join_into
5630           {
5631                 var lt = (Tokenizer.LocatedToken) $2;
5632                 var sn = new SimpleMemberName (lt.Value, lt.Location);
5633                 SimpleMemberName sn2 = null;
5634                 
5635                 ToplevelBlock outer_selector = (ToplevelBlock) linq_clause_blocks.Pop ();
5636                 ToplevelBlock block = (ToplevelBlock) linq_clause_blocks.Pop ();
5637
5638                 if ($12 == null) {
5639                         $$ = new Linq.Join (block, sn, (Expression)$5, outer_selector, current_block.Toplevel, GetLocation ($1));
5640                 } else {
5641                         var lt2 = (Tokenizer.LocatedToken) $12;
5642                         sn2 = new SimpleMemberName (lt2.Value, lt2.Location);
5643                         $$ = new Linq.GroupJoin (block, sn, (Expression)$5, outer_selector, current_block.Toplevel,
5644                                 sn2, GetLocation ($1));
5645                 }
5646
5647                 current_block.AddStatement (new ContextualReturn ((Expression) $11));
5648                 current_block.SetEndLocation (lexer.Location);
5649                 current_block = current_block.Parent;
5650                         
5651                 if (sn2 == null)
5652                         ((Linq.QueryBlock)current_block).AddTransparentParameter (compiler, sn);
5653                 else
5654                         ((Linq.QueryBlock)current_block).AddTransparentParameter (compiler, sn2);
5655           }
5656         | JOIN type IDENTIFIER IN
5657           {
5658                 if (linq_clause_blocks == null)
5659                         linq_clause_blocks = new Stack ();
5660                         
5661                 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5662                 linq_clause_blocks.Push (current_block);
5663           }
5664           expression ON
5665           {
5666                 current_block.SetEndLocation (lexer.Location);
5667                 current_block = current_block.Parent;
5668
5669                 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5670                 linq_clause_blocks.Push (current_block);
5671           }
5672           expression EQUALS
5673           {
5674                 current_block.AddStatement (new ContextualReturn ((Expression) $9));
5675                 current_block.SetEndLocation (lexer.Location);
5676                 current_block = current_block.Parent;
5677
5678                 var lt = (Tokenizer.LocatedToken) $3;
5679                 current_block = new Linq.QueryBlock (compiler, current_block, new SimpleMemberName (lt.Value, lt.Location), lexer.Location);
5680           }
5681           expression opt_join_into
5682           {
5683                 var lt = (Tokenizer.LocatedToken) $3;
5684                 var sn = new SimpleMemberName (lt.Value, lt.Location);
5685                 SimpleMemberName sn2 = null;
5686                 ToplevelBlock outer_selector = (ToplevelBlock) linq_clause_blocks.Pop ();
5687                 ToplevelBlock block = (ToplevelBlock) linq_clause_blocks.Pop ();
5688                 
5689                 Linq.Cast cast = new Linq.Cast ((FullNamedExpression)$2, (Expression)$6);
5690                 if ($13 == null) {
5691                         $$ = new Linq.Join (block, sn, cast, outer_selector, current_block.Toplevel, GetLocation ($1));
5692                 } else {
5693                         var lt2 = (Tokenizer.LocatedToken) $13;
5694                         sn2 = new SimpleMemberName (lt2.Value, lt2.Location);
5695                         $$ = new Linq.GroupJoin (block, sn, cast, outer_selector, current_block.Toplevel,
5696                                 sn2, GetLocation ($1));
5697                 }
5698                 
5699                 current_block.AddStatement (new ContextualReturn ((Expression) $12));
5700                 current_block.SetEndLocation (lexer.Location);
5701                 current_block = current_block.Parent;
5702                         
5703                 if (sn2 == null)
5704                         ((Linq.QueryBlock)current_block).AddTransparentParameter (compiler, sn);
5705                 else
5706                         ((Linq.QueryBlock)current_block).AddTransparentParameter (compiler, sn2);
5707           }
5708         ;
5709         
5710 opt_join_into
5711         : /* empty */
5712         | INTO IDENTIFIER
5713           {
5714                 $$ = $2;
5715           }
5716         ;
5717         
5718 orderby_clause
5719         : ORDERBY
5720           {
5721                 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5722           }
5723           orderings
5724           {
5725                 current_block.SetEndLocation (lexer.Location);
5726                 current_block = current_block.Parent;
5727           
5728                 $$ = $3;
5729           }
5730         ;
5731         
5732 orderings
5733         : order_by
5734         | order_by COMMA
5735           {
5736                 current_block.SetEndLocation (lexer.Location);
5737                 current_block = current_block.Parent;
5738           
5739                 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5740           }
5741           orderings_then_by
5742           {
5743                 ((Linq.AQueryClause)$1).Next = (Linq.AQueryClause)$4;
5744                 $$ = $1;
5745           }
5746         ;
5747         
5748 orderings_then_by
5749         : then_by
5750         | orderings_then_by COMMA
5751          {
5752                 current_block.SetEndLocation (lexer.Location);
5753                 current_block = current_block.Parent;
5754           
5755                 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);   
5756          }
5757          then_by
5758          {
5759                 ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$3;
5760                 $$ = $1;
5761          }
5762         ;       
5763         
5764 order_by
5765         : expression
5766           {
5767                 $$ = new Linq.OrderByAscending (current_block.Toplevel, (Expression)$1);        
5768           }
5769         | expression ASCENDING
5770           {
5771                 $$ = new Linq.OrderByAscending (current_block.Toplevel, (Expression)$1);        
5772           }
5773         | expression DESCENDING
5774           {
5775                 $$ = new Linq.OrderByDescending (current_block.Toplevel, (Expression)$1);       
5776           }
5777         ;
5778
5779 then_by
5780         : expression
5781           {
5782                 $$ = new Linq.ThenByAscending (current_block.Toplevel, (Expression)$1); 
5783           }
5784         | expression ASCENDING
5785           {
5786                 $$ = new Linq.ThenByAscending (current_block.Toplevel, (Expression)$1); 
5787           }
5788         | expression DESCENDING
5789           {
5790                 $$ = new Linq.ThenByDescending (current_block.Toplevel, (Expression)$1);        
5791           }     
5792         ;
5793
5794
5795 opt_query_continuation
5796         : /* empty */
5797         | INTO IDENTIFIER
5798           {
5799                 // query continuation block is not linked with query block but with block
5800                 // before. This means each query can use same range variable names for
5801                 // different identifiers.
5802
5803                 current_block.SetEndLocation (GetLocation ($1));
5804                 current_block = current_block.Parent;
5805
5806                 var lt = (Tokenizer.LocatedToken) $2;
5807                 
5808                 current_block = new Linq.QueryBlock (compiler, current_block, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1));
5809           }
5810           query_body
5811           {
5812                 $$ = new Linq.QueryExpression (current_block, (Linq.AQueryClause)$4);
5813           }
5814         ;
5815         
5816 //
5817 // Support for using the compiler as an interactive parser
5818 //
5819 // The INTERACTIVE_PARSER token is first sent to parse our
5820 // productions;  If the result is a Statement, the parsing
5821 // is repeated, this time with INTERACTIVE_PARSE_WITH_BLOCK
5822 // to setup the blocks in advance.
5823 //
5824 // This setup is here so that in the future we can add 
5825 // support for other constructs (type parsing, namespaces, etc)
5826 // that do not require a block to be setup in advance
5827 //
5828
5829 interactive_parsing
5830         : EVAL_STATEMENT_PARSER EOF 
5831         | EVAL_USING_DECLARATIONS_UNIT_PARSER using_directives 
5832         | EVAL_STATEMENT_PARSER { 
5833                 Evaluator.LoadAliases (current_namespace);
5834
5835                 push_current_class (new Class (current_namespace, current_class, new MemberName ("Class" + class_count++),
5836                         Modifiers.PUBLIC, null), null);
5837
5838                 ArrayList baseclass_list = new ArrayList ();
5839                 baseclass_list.Add (new TypeExpression (Evaluator.InteractiveBaseClass, lexer.Location));
5840                 current_container.AddBasesForPart (current_class, baseclass_list);
5841
5842                 // (ref object retval)
5843                 Parameter [] mpar = new Parameter [1];
5844                 mpar [0] = new Parameter (TypeManager.system_object_expr, "$retval", Parameter.Modifier.REF, null, Location.Null);
5845
5846                 ParametersCompiled pars = new ParametersCompiled (compiler, mpar);
5847                 current_local_parameters = pars;
5848                 Method method = new Method (
5849                         current_class,
5850                         null, // generic
5851                         TypeManager.system_void_expr,
5852                         Modifiers.PUBLIC | Modifiers.STATIC,
5853                         new MemberName ("Host"),
5854                         pars,
5855                         null /* attributes */);
5856
5857                 oob_stack.Push (method);
5858                 ++lexer.parsing_block;
5859                 start_block (lexer.Location);
5860           }             
5861           interactive_statement_list opt_COMPLETE_COMPLETION
5862           {
5863                 --lexer.parsing_block;
5864                 Method method = (Method) oob_stack.Pop ();
5865
5866                 method.Block = (ToplevelBlock) end_block(lexer.Location);
5867                 current_container.AddMethod (method);
5868
5869                 --lexer.parsing_declaration;
5870                 InteractiveResult = pop_current_class ();
5871                 current_local_parameters = null;
5872           } 
5873         | EVAL_COMPILATION_UNIT_PARSER {
5874                 Evaluator.LoadAliases (current_namespace);
5875           }
5876           interactive_compilation_unit
5877         ;
5878
5879 interactive_compilation_unit
5880         : outer_declarations 
5881         | outer_declarations global_attributes 
5882         | global_attributes 
5883         | /* nothing */
5884         ;
5885
5886 opt_COMPLETE_COMPLETION
5887         : /* nothing */
5888         | COMPLETE_COMPLETION
5889         ;
5890
5891 close_brace_or_complete_completion
5892         : CLOSE_BRACE
5893         | COMPLETE_COMPLETION
5894         ;
5895 %%
5896
5897 // <summary>
5898 //   A class used to pass around variable declarations and constants
5899 // </summary>
5900 class VariableDeclaration {
5901         public string identifier;
5902         public Expression expression_or_array_initializer;
5903         public Location Location;
5904         public Attributes OptAttributes;
5905         public string DocComment;
5906
5907         public VariableDeclaration (Tokenizer.LocatedToken lt, object eoai, Attributes opt_attrs)
5908         {
5909                 this.identifier = lt.Value;
5910                 if (eoai is ArrayList) {
5911                         this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
5912                 } else {
5913                         this.expression_or_array_initializer = (Expression)eoai;
5914                 }
5915                 this.Location = lt.Location;
5916                 this.OptAttributes = opt_attrs;
5917         }
5918
5919         public VariableDeclaration (Tokenizer.LocatedToken lt, object eoai) : this (lt, eoai, null)
5920         {
5921         }
5922 }
5923
5924 class VariableMemberDeclaration
5925 {
5926         public readonly MemberName MemberName;
5927         public Expression expression_or_array_initializer;
5928         
5929         public VariableMemberDeclaration (MemberName mn, object initializer)
5930         {
5931                 MemberName = mn;
5932                 
5933                 if (initializer is ArrayList) {
5934                         this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)initializer, mn.Location);
5935                 } else {
5936                         this.expression_or_array_initializer = (Expression)initializer;
5937                 }
5938         }
5939 }
5940
5941
5942 // <summary>
5943 //  A class used to hold info about an operator declarator
5944 // </summary>
5945 struct OperatorDeclaration {
5946         public readonly Operator.OpType optype;
5947         public readonly FullNamedExpression ret_type;
5948         public readonly Location location;
5949
5950         public OperatorDeclaration (Operator.OpType op, FullNamedExpression ret_type, Location location)
5951         {
5952                 optype = op;
5953                 this.ret_type = ret_type;
5954                 this.location = location;
5955         }
5956 }
5957
5958 void Error_ExpectingTypeName (Expression expr)
5959 {
5960         if (expr is Invocation){
5961                 Report.Error (1002, expr.Location, "Expecting `;'");
5962         } else {
5963                 Expression.Error_InvalidExpressionStatement (Report, expr.Location);
5964         }
5965 }
5966
5967 void Error_ParameterModifierNotValid (string modifier, Location loc)
5968 {
5969         Report.Error (631, loc, "The parameter modifier `{0}' is not valid in this context",
5970                                       modifier);
5971 }
5972
5973 void Error_DuplicateParameterModifier (Location loc, Parameter.Modifier mod)
5974 {
5975         Report.Error (1107, loc, "Duplicate parameter modifier `{0}'",
5976                 Parameter.GetModifierSignature (mod));
5977 }
5978
5979 void Error_TypeExpected (Location loc)
5980 {
5981         Report.Error (1031, loc, "Type expected");
5982 }
5983
5984 void Error_NamedArgumentExpected (NamedArgument a)
5985 {
5986         Report.Error (1738, a.Location, "Named arguments must appear after the positional arguments");
5987 }
5988
5989 void push_current_class (TypeContainer tc, object partial_token)
5990 {
5991         if (RootContext.EvalMode){
5992                 tc.ModFlags = (tc.ModFlags & ~(Modifiers.PRIVATE|Modifiers.INTERNAL)) | Modifiers.PUBLIC;
5993                 undo.AddTypeContainer (current_container, tc);
5994         }
5995
5996         if (partial_token != null)
5997                 current_container = current_container.AddPartial (tc);
5998         else
5999                 current_container = current_container.AddTypeContainer (tc);
6000
6001         ++lexer.parsing_declaration;
6002         current_class = tc;
6003 }
6004
6005 DeclSpace pop_current_class ()
6006 {
6007         DeclSpace retval = current_class;
6008
6009         current_class = current_class.Parent;
6010         current_container = current_class.PartialContainer;
6011
6012         return retval;
6013 }
6014
6015 // <summary>
6016 //   Given the @class_name name, it creates a fully qualified name
6017 //   based on the containing declaration space
6018 // </summary>
6019 MemberName
6020 MakeName (MemberName class_name)
6021 {
6022         Namespace ns = current_namespace.NS;
6023
6024         if (current_container == RootContext.ToplevelTypes) {
6025                 if (ns.Name.Length != 0)
6026                         return new MemberName (ns.MemberName, class_name);
6027                 else
6028                         return class_name;
6029         } else {
6030                 return new MemberName (current_container.MemberName, class_name);
6031         }
6032 }
6033
6034 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
6035 {
6036         Block implicit_block;
6037
6038         //
6039         // If we are doing interactive editing, we want variable declarations
6040         // that are in the top block to be added instead to the class as 
6041         // static variables
6042         //
6043         if (RootContext.StatementMode){
6044                 bool hoist = true;
6045
6046                 for (Block b = current_block; b != null; b = b.Parent){
6047                         if (b is ExplicitBlock && !(b is ToplevelBlock)){
6048                                 // There has been an explicit block, we cant add to the class
6049                                 hoist = false;
6050                                 break;
6051                         }
6052                 }               
6053                 if (hoist){
6054                         //
6055                         // We can use "current_block" since we know there are no explicit blocks
6056                         //
6057                         foreach (VariableDeclaration decl in variable_declarators){
6058                                 // We can not use the super-handy f.Initializer, because
6059                                 // multiple lines would force code to be executed out of sync
6060                                 if (decl.expression_or_array_initializer != null){
6061                                         string id = "$" + decl.identifier;
6062                                         LocalInfo vi = current_block.AddVariable (type, id, decl.Location);                                     
6063
6064                                         // Avoid warning about this variable not being used.
6065                                         vi.Used = true;
6066
6067                                         LocalVariableReference var;
6068                                         var = new LocalVariableReferenceWithClassSideEffect (current_container, decl.identifier, current_block, id, vi, decl.Location);
6069                                         Assign assign = new SimpleAssign (var, decl.expression_or_array_initializer, decl.Location);
6070                                         current_block.AddStatement (new StatementExpression (assign));
6071                                         assign = new SimpleAssign (new SimpleName (decl.identifier, decl.Location), var);
6072                                         current_block.AddStatement (new StatementExpression (assign));
6073                                 } else {
6074                                         Field f = new Field (current_container, (FullNamedExpression) type, Modifiers.PUBLIC | Modifiers.STATIC,
6075                                                 new MemberName (decl.identifier, loc), null);
6076                                         current_container.AddField (f);
6077
6078                                         // Register the field to be visible later as a global variable
6079                                         Evaluator.QueueField (f);
6080                                 }
6081                         }
6082
6083                         return current_block;
6084                 }
6085         }
6086
6087         //
6088         // We use the `Used' property to check whether statements
6089         // have been added to the current block.  If so, we need
6090         // to create another block to contain the new declaration
6091         // otherwise, as an optimization, we use the same block to
6092         // add the declaration.
6093         //
6094         // FIXME: A further optimization is to check if the statements
6095         // that were added were added as part of the initialization
6096         // below.  In which case, no other statements have been executed
6097         // and we might be able to reduce the number of blocks for
6098         // situations like this:
6099         //
6100         // int j = 1;  int k = j + 1;
6101         //
6102         if (current_block.Used)
6103                 implicit_block = new Block (current_block, loc, lexer.Location);
6104         else
6105                 implicit_block = current_block;
6106
6107         foreach (VariableDeclaration decl in variable_declarators){
6108
6109                 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
6110                         if (decl.expression_or_array_initializer != null){
6111                                 Assign assign;
6112                                 Expression expr = decl.expression_or_array_initializer;
6113                                 
6114                                 var lvr = new LocalVariableReference (implicit_block, decl.identifier, loc);
6115
6116                                 assign = new SimpleAssign (lvr, expr, decl.Location);
6117
6118                                 implicit_block.AddStatement (new StatementExpression (assign));
6119                         }
6120                 }
6121         }
6122         
6123         return implicit_block;
6124 }
6125
6126 Block declare_local_constants (Expression type, ArrayList declarators)
6127 {
6128         Block implicit_block;
6129
6130         if (current_block.Used)
6131                 implicit_block = new Block (current_block);
6132         else
6133                 implicit_block = current_block;
6134
6135         foreach (VariableDeclaration decl in declarators){
6136                 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
6137         }
6138         
6139         return implicit_block;
6140 }
6141
6142 string CheckAttributeTarget (string a, Location l)
6143 {
6144         switch (a) {
6145         case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
6146                         return a;
6147         }
6148
6149         Report.Warning (658, 1, l,
6150                  "`{0}' is invalid attribute target. All attributes in this attribute section will be ignored", a);
6151         return string.Empty;
6152 }
6153
6154 static bool IsUnaryOperator (Operator.OpType op)
6155 {
6156         switch (op) {
6157                 
6158         case Operator.OpType.LogicalNot: 
6159         case Operator.OpType.OnesComplement: 
6160         case Operator.OpType.Increment:
6161         case Operator.OpType.Decrement:
6162         case Operator.OpType.True: 
6163         case Operator.OpType.False: 
6164         case Operator.OpType.UnaryPlus: 
6165         case Operator.OpType.UnaryNegation:
6166                 return true;
6167         }
6168         return false;
6169 }
6170
6171 void syntax_error (Location l, string msg)
6172 {
6173         Report.Error (1003, l, "Syntax error, " + msg);
6174 }
6175
6176 Tokenizer lexer;
6177
6178 public Tokenizer Lexer {
6179         get {
6180                 return lexer;
6181         }
6182 }                  
6183
6184 static CSharpParser ()
6185 {
6186         oob_stack = new Stack ();
6187 }
6188
6189 public CSharpParser (SeekableStreamReader reader, CompilationUnit file, CompilerContext ctx)
6190 {
6191         if (RootContext.EvalMode)
6192                 undo = new Undo ();
6193
6194         this.file = file;
6195         this.compiler = ctx;
6196         current_namespace = new NamespaceEntry (null, file, null);
6197         current_class = current_namespace.SlaveDeclSpace;
6198         current_container = current_class.PartialContainer; // == RootContest.ToplevelTypes
6199         oob_stack.Clear ();
6200         lexer = new Tokenizer (reader, file, ctx);
6201         
6202         use_global_stacks = true;
6203 }
6204
6205 public void parse ()
6206 {
6207         eof_token = Token.EOF;
6208         Tokenizer.LocatedToken.Initialize ();
6209         
6210         try {
6211                 if (yacc_verbose_flag > 1)
6212                         yyparse (lexer, new yydebug.yyDebugSimple ());
6213                 else
6214                         yyparse (lexer);
6215                         
6216                 Tokenizer tokenizer = lexer as Tokenizer;
6217                 tokenizer.cleanup ();           
6218         } catch (Exception e){
6219                 if (e is yyParser.yyUnexpectedEof)
6220                         UnexpectedEOF = true;
6221
6222                 if (e is yyParser.yyException)
6223                         Report.Error (-25, lexer.Location, "Parsing error");
6224                 else if (yacc_verbose_flag > 0)
6225                         throw;  // Used by compiler-tester to test internal errors
6226                 else 
6227                         Report.Error (589, lexer.Location, "Internal compiler error during parsing");
6228         }
6229
6230         if (RootContext.ToplevelTypes.NamespaceEntry != null)
6231                 throw new InternalErrorException ("who set it?");
6232 }
6233
6234 void CheckToken (int error, int yyToken, string msg, Location loc)
6235 {
6236         if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
6237                 Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, GetTokenName (yyToken));
6238         else
6239                 Report.Error (error, loc, msg);
6240 }
6241
6242 void CheckIdentifierToken (int yyToken, Location loc)
6243 {
6244         CheckToken (1041, yyToken, "Identifier expected", loc);
6245 }
6246
6247 string ConsumeStoredComment ()
6248 {
6249         string s = tmpComment;
6250         tmpComment = null;
6251         Lexer.doc_state = XmlCommentState.Allowed;
6252         return s;
6253 }
6254
6255 Location GetLocation (object obj)
6256 {
6257         if (obj is Tokenizer.LocatedToken)
6258                 return ((Tokenizer.LocatedToken) obj).Location;
6259         if (obj is MemberName)
6260                 return ((MemberName) obj).Location;
6261
6262         return lexer.Location;
6263 }
6264
6265 Report Report {
6266         get { return compiler.Report; }
6267 }
6268
6269 void start_block (Location loc)
6270 {
6271         if (current_block == null || parsing_anonymous_method) {
6272                 current_block = new ToplevelBlock (compiler, current_block, current_local_parameters, current_generic_method, loc);
6273                 parsing_anonymous_method = false;
6274         } else {
6275                 current_block = new ExplicitBlock (current_block, loc, Location.Null);
6276         }
6277 }
6278
6279 Block
6280 end_block (Location loc)
6281 {
6282         Block retval = current_block.Explicit;
6283         retval.SetEndLocation (loc);
6284         current_block = retval.Parent;
6285         return retval;
6286 }
6287
6288 void
6289 start_anonymous (bool lambda, ParametersCompiled parameters, Location loc)
6290 {
6291         if (RootContext.Version == LanguageVersion.ISO_1){
6292                 Report.FeatureIsNotAvailable (loc, "anonymous methods");
6293         }
6294
6295         oob_stack.Push (current_anonymous_method);
6296         oob_stack.Push (current_local_parameters);
6297
6298         current_local_parameters = parameters;
6299
6300         current_anonymous_method = lambda 
6301                 ? new LambdaExpression (loc) 
6302                 : new AnonymousMethodExpression (loc);
6303
6304         // Force the next block to be created as a ToplevelBlock
6305         parsing_anonymous_method = true;
6306 }
6307
6308 /*
6309  * Completes the anonymous method processing, if lambda_expr is null, this
6310  * means that we have a Statement instead of an Expression embedded 
6311  */
6312 AnonymousMethodExpression end_anonymous (ToplevelBlock anon_block)
6313 {
6314         AnonymousMethodExpression retval;
6315
6316         current_anonymous_method.Block = anon_block;
6317         retval = current_anonymous_method;
6318
6319         current_local_parameters = (ParametersCompiled) oob_stack.Pop ();
6320         current_anonymous_method = (AnonymousMethodExpression) oob_stack.Pop ();
6321
6322         return retval;
6323 }
6324
6325 public NamespaceEntry CurrentNamespace {
6326        get { 
6327            return current_namespace;
6328        }
6329 }
6330
6331
6332 void Error_SyntaxError (int token)
6333 {
6334         Error_SyntaxError (0, token, "Unexpected symbol");
6335 }
6336
6337 void Error_SyntaxError (int error_code, int token, string msg)
6338 {
6339         string symbol = GetSymbolName (token);
6340         string expecting = GetExpecting ();
6341         
6342         if (error_code == 0) {
6343                 if (expecting == "`)'")
6344                         error_code = 1026;
6345                 else
6346                         error_code = 1525;
6347         }
6348         
6349         if (expecting != null)
6350                 Report.Error (error_code, lexer.Location, "{2} `{0}', expecting {1}", 
6351                         symbol, expecting, msg);          
6352         else
6353                 Report.Error (error_code, lexer.Location, "{1} `{0}'", symbol, msg);
6354 }
6355
6356 string GetExpecting ()
6357 {
6358         int [] tokens = yyExpectingTokens (yyExpectingState);
6359         ArrayList names = new ArrayList (tokens.Length);
6360         bool has_type = false;
6361         bool has_identifier = false;
6362         for (int i = 0; i < tokens.Length; i++){
6363                 int token = tokens [i];
6364                 has_identifier |= token == Token.IDENTIFIER;
6365                 
6366                 string name = GetTokenName (token);
6367                 if (name == "<internal>")
6368                         continue;
6369                         
6370                 has_type |= name == "type";
6371                 if (names.Contains (name))
6372                         continue;
6373                 
6374                 names.Add (name);
6375         }
6376
6377         //
6378         // Too many tokens to enumerate
6379         //
6380         if (names.Count > 8)
6381                 return null;
6382
6383         if (has_type && has_identifier)
6384                 names.Remove ("identifier");
6385
6386         if (names.Count == 1)
6387                 return "`" + GetTokenName (tokens [0]) + "'";
6388         
6389         StringBuilder sb = new StringBuilder ();
6390         names.Sort ();
6391         int count = names.Count;
6392         for (int i = 0; i < count; i++){
6393                 bool last = i + 1 == count;
6394                 if (last)
6395                         sb.Append ("or ");
6396                 sb.Append ('`');
6397                 sb.Append (names [i]);
6398                 sb.Append (last ? "'" : count < 3 ? "' " : "', ");
6399         }
6400         return sb.ToString ();
6401 }
6402
6403
6404 string GetSymbolName (int token)
6405 {
6406         switch (token){
6407         case Token.LITERAL:
6408                 return ((Constant)lexer.Value).GetValue ().ToString ();
6409         case Token.IDENTIFIER:
6410                 return ((Tokenizer.LocatedToken)lexer.Value).Value;
6411
6412         case Token.BOOL:
6413                 return "bool";
6414         case Token.BYTE:
6415                 return "byte";
6416         case Token.CHAR:
6417                 return "char";
6418         case Token.VOID:
6419                 return "void";
6420         case Token.DECIMAL:
6421                 return "decimal";
6422         case Token.DOUBLE:
6423                 return "double";
6424         case Token.FLOAT:
6425                 return "float";
6426         case Token.INT:
6427                 return "int";
6428         case Token.LONG:
6429                 return "long";
6430         case Token.SBYTE:
6431                 return "sbyte";
6432         case Token.SHORT:
6433                 return "short";
6434         case Token.STRING:
6435                 return "string";
6436         case Token.UINT:
6437                 return "uint";
6438         case Token.ULONG:
6439                 return "ulong";
6440         case Token.USHORT:
6441                 return "ushort";
6442         case Token.OBJECT:
6443                 return "object";
6444                 
6445         case Token.PLUS:
6446                 return "+";
6447         case Token.UMINUS:
6448         case Token.MINUS:
6449                 return "-";
6450         case Token.BANG:
6451                 return "!";
6452         case Token.BITWISE_AND:
6453                 return "&";
6454         case Token.BITWISE_OR:
6455                 return "|";
6456         case Token.STAR:
6457                 return "*";
6458         case Token.PERCENT:
6459                 return "%";
6460         case Token.DIV:
6461                 return "/";
6462         case Token.CARRET:
6463                 return "^";
6464         case Token.OP_INC:
6465                 return "++";
6466         case Token.OP_DEC:
6467                 return "--";
6468         case Token.OP_SHIFT_LEFT:
6469                 return "<<";
6470         case Token.OP_SHIFT_RIGHT:
6471                 return ">>";
6472         case Token.OP_LT:
6473                 return "<";
6474         case Token.OP_GT:
6475                 return ">";
6476         case Token.OP_LE:
6477                 return "<=";
6478         case Token.OP_GE:
6479                 return ">=";
6480         case Token.OP_EQ:
6481                 return "==";
6482         case Token.OP_NE:
6483                 return "!=";
6484         case Token.OP_AND:
6485                 return "&&";
6486         case Token.OP_OR:
6487                 return "||";
6488         case Token.OP_PTR:
6489                 return "->";
6490         case Token.OP_COALESCING:       
6491                 return "??";
6492         case Token.OP_MULT_ASSIGN:
6493                 return "*=";
6494         case Token.OP_DIV_ASSIGN:
6495                 return "/=";
6496         case Token.OP_MOD_ASSIGN:
6497                 return "%=";
6498         case Token.OP_ADD_ASSIGN:
6499                 return "+=";
6500         case Token.OP_SUB_ASSIGN:
6501                 return "-=";
6502         case Token.OP_SHIFT_LEFT_ASSIGN:
6503                 return "<<=";
6504         case Token.OP_SHIFT_RIGHT_ASSIGN:
6505                 return ">>=";
6506         case Token.OP_AND_ASSIGN:
6507                 return "&=";
6508         case Token.OP_XOR_ASSIGN:
6509                 return "^=";
6510         case Token.OP_OR_ASSIGN:
6511                 return "|=";
6512         }
6513
6514         return GetTokenName (token);
6515 }
6516
6517 static string GetTokenName (int token)
6518 {
6519         switch (token){
6520         case Token.ABSTRACT:
6521                 return "abstract";
6522         case Token.AS:
6523                 return "as";
6524         case Token.ADD:
6525                 return "add";
6526         case Token.BASE:
6527                 return "base";
6528         case Token.BREAK:
6529                 return "break";
6530         case Token.CASE:
6531                 return "case";
6532         case Token.CATCH:
6533                 return "catch";
6534         case Token.CHECKED:
6535                 return "checked";
6536         case Token.CLASS:
6537                 return "class";
6538         case Token.CONST:
6539                 return "const";
6540         case Token.CONTINUE:
6541                 return "continue";
6542         case Token.DEFAULT:
6543                 return "default";
6544         case Token.DELEGATE:
6545                 return "delegate";
6546         case Token.DO:
6547                 return "do";
6548         case Token.ELSE:
6549                 return "else";
6550         case Token.ENUM:
6551                 return "enum";
6552         case Token.EVENT:
6553                 return "event";
6554         case Token.EXPLICIT:
6555                 return "explicit";
6556         case Token.EXTERN:
6557                 return "extern";
6558         case Token.FALSE:
6559                 return "false";
6560         case Token.FINALLY:
6561                 return "finally";
6562         case Token.FIXED:
6563                 return "fixed";
6564         case Token.FOR:
6565                 return "for";
6566         case Token.FOREACH:
6567                 return "foreach";
6568         case Token.GOTO:
6569                 return "goto";
6570         case Token.IF:
6571                 return "if";
6572         case Token.IMPLICIT:
6573                 return "implicit";
6574         case Token.IN:
6575                 return "in";
6576         case Token.INTERFACE:
6577                 return "interface";
6578         case Token.INTERNAL:
6579                 return "internal";
6580         case Token.IS:
6581                 return "is";
6582         case Token.LOCK:
6583                 return "lock";
6584         case Token.NAMESPACE:
6585                 return "namespace";
6586         case Token.NEW:
6587                 return "new";
6588         case Token.NULL:
6589                 return "null";
6590         case Token.OPERATOR:
6591                 return "operator";
6592         case Token.OUT:
6593                 return "out";
6594         case Token.OVERRIDE:
6595                 return "override";
6596         case Token.PARAMS:
6597                 return "params";
6598         case Token.PRIVATE:
6599                 return "private";
6600         case Token.PROTECTED:
6601                 return "protected";
6602         case Token.PUBLIC:
6603                 return "public";
6604         case Token.READONLY:
6605                 return "readonly";
6606         case Token.REF:
6607                 return "ref";
6608         case Token.RETURN:
6609                 return "return";
6610         case Token.REMOVE:
6611                 return "remove";
6612         case Token.SEALED:
6613                 return "sealed";
6614         case Token.SIZEOF:
6615                 return "sizeof";
6616         case Token.STACKALLOC:
6617                 return "stackalloc";
6618         case Token.STATIC:
6619                 return "static";
6620         case Token.STRUCT:
6621                 return "struct";
6622         case Token.SWITCH:
6623                 return "switch";
6624         case Token.THIS:
6625                 return "this";
6626         case Token.THROW:
6627                 return "throw";
6628         case Token.TRUE:
6629                 return "true";
6630         case Token.TRY:
6631                 return "try";
6632         case Token.TYPEOF:
6633                 return "typeof";
6634         case Token.UNCHECKED:
6635                 return "unchecked";
6636         case Token.UNSAFE:
6637                 return "unsafe";
6638         case Token.USING:
6639                 return "using";
6640         case Token.VIRTUAL:
6641                 return "virtual";
6642         case Token.VOLATILE:
6643                 return "volatile";
6644         case Token.WHERE:
6645                 return "where";
6646         case Token.WHILE:
6647                 return "while";
6648         case Token.ARGLIST:
6649                 return "__arglist";
6650         case Token.PARTIAL:
6651                 return "partial";
6652         case Token.ARROW:
6653                 return "=>";
6654         case Token.FROM:
6655         case Token.FROM_FIRST:
6656                 return "from";
6657         case Token.JOIN:
6658                 return "join";
6659         case Token.ON:
6660                 return "on";
6661         case Token.EQUALS:
6662                 return "equals";
6663         case Token.SELECT:
6664                 return "select";
6665         case Token.GROUP:
6666                 return "group";
6667         case Token.BY:
6668                 return "by";
6669         case Token.LET:
6670                 return "let";
6671         case Token.ORDERBY:
6672                 return "orderby";
6673         case Token.ASCENDING:
6674                 return "ascending";
6675         case Token.DESCENDING:
6676                 return "descending";
6677         case Token.INTO:
6678                 return "into";
6679         case Token.GET:
6680                 return "get";
6681         case Token.SET:
6682                 return "set";
6683         case Token.OPEN_BRACE:
6684                 return "{";
6685         case Token.CLOSE_BRACE:
6686                 return "}";
6687         case Token.OPEN_BRACKET:
6688                 return "[";
6689         case Token.CLOSE_BRACKET:
6690                 return "]";
6691         case Token.OPEN_PARENS_CAST:
6692         case Token.OPEN_PARENS_LAMBDA:
6693         case Token.OPEN_PARENS:
6694                 return "(";
6695         case Token.CLOSE_PARENS:
6696                 return ")";
6697         case Token.DOT:
6698                 return ".";
6699         case Token.COMMA:
6700                 return ",";
6701         case Token.DEFAULT_COLON:
6702                 return "default:";
6703         case Token.COLON:
6704                 return ":";
6705         case Token.SEMICOLON:
6706                 return ";";
6707         case Token.TILDE:
6708                 return "~";
6709                 
6710         case Token.PLUS:
6711         case Token.UMINUS:
6712         case Token.MINUS:
6713         case Token.BANG:
6714         case Token.OP_LT:
6715         case Token.OP_GT:
6716         case Token.BITWISE_AND:
6717         case Token.BITWISE_OR:
6718         case Token.STAR:
6719         case Token.PERCENT:
6720         case Token.DIV:
6721         case Token.CARRET:
6722         case Token.OP_INC:
6723         case Token.OP_DEC:
6724         case Token.OP_SHIFT_LEFT:
6725         case Token.OP_SHIFT_RIGHT:
6726         case Token.OP_LE:
6727         case Token.OP_GE:
6728         case Token.OP_EQ:
6729         case Token.OP_NE:
6730         case Token.OP_AND:
6731         case Token.OP_OR:
6732         case Token.OP_PTR:
6733         case Token.OP_COALESCING:       
6734         case Token.OP_MULT_ASSIGN:
6735         case Token.OP_DIV_ASSIGN:
6736         case Token.OP_MOD_ASSIGN:
6737         case Token.OP_ADD_ASSIGN:
6738         case Token.OP_SUB_ASSIGN:
6739         case Token.OP_SHIFT_LEFT_ASSIGN:
6740         case Token.OP_SHIFT_RIGHT_ASSIGN:
6741         case Token.OP_AND_ASSIGN:
6742         case Token.OP_XOR_ASSIGN:
6743         case Token.OP_OR_ASSIGN:
6744                 return "<operator>";
6745
6746         case Token.BOOL:
6747         case Token.BYTE:
6748         case Token.CHAR:
6749         case Token.VOID:
6750         case Token.DECIMAL:
6751         case Token.DOUBLE:
6752         case Token.FLOAT:
6753         case Token.INT:
6754         case Token.LONG:
6755         case Token.SBYTE:
6756         case Token.SHORT:
6757         case Token.STRING:
6758         case Token.UINT:
6759         case Token.ULONG:
6760         case Token.USHORT:
6761         case Token.OBJECT:
6762                 return "type";
6763         
6764         case Token.ASSIGN:
6765                 return "=";
6766         case Token.OP_GENERICS_LT:
6767         case Token.GENERIC_DIMENSION:
6768                 return "<";
6769         case Token.OP_GENERICS_GT:
6770                 return ">";
6771         case Token.INTERR:
6772         case Token.INTERR_NULLABLE:
6773                 return "?";
6774         case Token.DOUBLE_COLON:
6775                 return "::";
6776         case Token.LITERAL:
6777                 return "value";
6778         case Token.IDENTIFIER:
6779                 return "identifier";
6780
6781                 // All of these are internal.
6782         case Token.NONE:
6783         case Token.ERROR:
6784         case Token.FIRST_KEYWORD:
6785         case Token.EOF:
6786         case Token.EVAL_COMPILATION_UNIT_PARSER:
6787         case Token.EVAL_USING_DECLARATIONS_UNIT_PARSER:
6788         case Token.EVAL_STATEMENT_PARSER:
6789         case Token.LAST_KEYWORD:
6790         case Token.GENERATE_COMPLETION:
6791         case Token.COMPLETE_COMPLETION:
6792                 return "<internal>";
6793
6794                 // A bit more robust.
6795         default:
6796                 return yyNames [token];
6797         }
6798 }
6799
6800 /* end end end */
6801 }