**** Merged from HEAD ****
[mono.git] / mcs / gmcs / 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 //
8 // Licensed under the terms of the GNU GPL
9 //
10 // (C) 2001 Ximian, Inc (http://www.ximian.com)
11 //
12 // TODO:
13 //   (1) Figure out why error productions dont work.  `type-declaration' is a
14 //       great spot to put an `error' because you can reproduce it with this input:
15 //       "public X { }"
16 //
17 // Possible optimization:
18 //   Run memory profiler with parsing only, and consider dropping 
19 //   arraylists where not needed.   Some pieces can use linked lists.
20 //
21 using System.Text;
22 using System.IO;
23 using System;
24
25 namespace Mono.CSharp
26 {
27         using System.Collections;
28
29         /// <summary>
30         ///    The C# Parser
31         /// </summary>
32         public class CSharpParser {
33                 NamespaceEntry  current_namespace;
34                 TypeContainer   current_container;
35         
36                 IIteratorContainer iterator_container;
37
38                 /// <summary>
39                 ///   Current block is used to add statements as we find
40                 ///   them.  
41                 /// </summary>
42
43                 Block      current_block;
44
45                 /// <summary>
46                 ///   If true, creates a toplevel block in the block production
47                 ///   This is flagged by the delegate creation
48                 /// </summary>
49                 bool       create_toplevel_block;
50
51                 /// <summary>
52                 /// <summary>
53                 ///   Current interface is used by the various declaration
54                 ///   productions in the interface declaration to "add"
55                 ///   the interfaces as we find them.
56                 /// </summary>
57                 Interface  current_interface;
58
59                 /// <summary>
60                 ///   This is used by the unary_expression code to resolve
61                 ///   a name against a parameter.  
62                 /// </summary>
63                 Parameters current_local_parameters;
64
65                 /// <summary>
66                 ///   Using during property parsing to describe the implicit
67                 ///   value parameter that is passed to the "set" and "get"accesor
68                 ///   methods (properties and indexers).
69                 /// </summary>
70                 Expression implicit_value_parameter_type;
71                 Parameters indexer_parameters;
72
73                 /// <summary>
74                 ///   Used to determine if we are parsing the get/set pair
75                 ///   of an indexer or a property
76                 /// </summmary>
77                 bool  parsing_indexer;
78
79                 ///
80                 /// An out-of-band stack.
81                 ///
82                 Stack oob_stack;
83
84                 ///
85                 /// Switch stack.
86                 ///
87                 Stack switch_stack;
88
89                 public bool yacc_verbose_flag;
90
91                 // Name of the file we are parsing
92                 public string name;
93
94                 ///
95                 /// The current file.
96                 ///
97                 SourceFile file;
98 %}
99
100 %token EOF
101 %token NONE   /* This token is never returned by our lexer */
102 %token ERROR            // This is used not by the parser, but by the tokenizer.
103                         // do not remove.
104
105 /*
106  *These are the C# keywords
107  */
108 %token FIRST_KEYWORD
109 %token ABSTRACT 
110 %token AS
111 %token ADD
112 %token ASSEMBLY
113 %token BASE     
114 %token BOOL     
115 %token BREAK    
116 %token BYTE     
117 %token CASE     
118 %token CATCH    
119 %token CHAR     
120 %token CHECKED  
121 %token CLASS    
122 %token CONST    
123 %token CONTINUE 
124 %token DECIMAL  
125 %token DEFAULT  
126 %token DELEGATE 
127 %token DO       
128 %token DOUBLE   
129 %token ELSE     
130 %token ENUM     
131 %token EVENT    
132 %token EXPLICIT 
133 %token EXTERN   
134 %token FALSE    
135 %token FINALLY  
136 %token FIXED    
137 %token FLOAT    
138 %token FOR      
139 %token FOREACH  
140 %token GOTO     
141 %token IF       
142 %token IMPLICIT 
143 %token IN       
144 %token INT      
145 %token INTERFACE
146 %token INTERNAL 
147 %token IS       
148 %token LOCK     
149 %token LONG     
150 %token NAMESPACE
151 %token NEW      
152 %token NULL     
153 %token OBJECT   
154 %token OPERATOR 
155 %token OUT      
156 %token OVERRIDE 
157 %token PARAMS   
158 %token PRIVATE  
159 %token PROTECTED
160 %token PUBLIC   
161 %token READONLY 
162 %token REF      
163 %token RETURN   
164 %token REMOVE
165 %token SBYTE    
166 %token SEALED   
167 %token SHORT    
168 %token SIZEOF   
169 %token STACKALLOC
170 %token STATIC   
171 %token STRING   
172 %token STRUCT   
173 %token SWITCH   
174 %token THIS     
175 %token THROW    
176 %token TRUE     
177 %token TRY      
178 %token TYPEOF   
179 %token UINT     
180 %token ULONG    
181 %token UNCHECKED
182 %token UNSAFE   
183 %token USHORT   
184 %token USING    
185 %token VIRTUAL  
186 %token VOID     
187 %token VOLATILE
188 %token WHERE
189 %token WHILE    
190
191 /* v2 tokens */
192 %token YIELD
193
194 /* C# keywords which are not really keywords */
195 %token GET           "get"
196 %token SET           "set"
197
198 %left LAST_KEYWORD
199
200 /* C# single character operators/punctuation. */
201 %token OPEN_BRACE    "{"
202 %token CLOSE_BRACE   "}"
203 %token OPEN_BRACKET  "["
204 %token CLOSE_BRACKET "]"
205 %token OPEN_PARENS   "("
206 %token CLOSE_PARENS  ")"
207 %token DOT           "."
208 %token COMMA         ","
209 %token COLON         ":"
210 %token SEMICOLON     ";"
211 %token TILDE         "~"
212
213 %token PLUS           "+"
214 %token MINUS          "-"
215 %token BANG           "!"
216 %token ASSIGN         "="
217 %token OP_LT          "<"
218 %token OP_GENERICS_LT "<"
219 %token OP_GT          ">"
220 %token OP_GENERICS_GT ">"
221 %token BITWISE_AND    "&"
222 %token BITWISE_OR     "|"
223 %token STAR           "*"
224 %token PERCENT        "%"
225 %token DIV            "/"
226 %token CARRET         "^"
227 %token INTERR         "?"
228
229 /* C# multi-character operators. */
230 %token OP_INC                 "++"
231 %token OP_DEC                 "--"
232 %token OP_SHIFT_LEFT          "<<"
233 %token OP_SHIFT_RIGHT         ">>"
234 %token OP_LE                  "<="
235 %token OP_GE                  ">="
236 %token OP_EQ                  "=="
237 %token OP_NE                  "!="
238 %token OP_AND                 "&&"
239 %token OP_OR                  "||"
240 %token OP_MULT_ASSIGN         "*="
241 %token OP_DIV_ASSIGN          "/="
242 %token OP_MOD_ASSIGN          "%="
243 %token OP_ADD_ASSIGN          "+="
244 %token OP_SUB_ASSIGN          "-="
245 %token OP_SHIFT_LEFT_ASSIGN   "<<="
246 %token OP_SHIFT_RIGHT_ASSIGN  ">>="
247 %token OP_AND_ASSIGN          "&="
248 %token OP_XOR_ASSIGN          "^="
249 %token OP_OR_ASSIGN           "|="
250 %token OP_PTR                 "->"
251
252 /* Numbers */
253 %token LITERAL_INTEGER           "int literal"
254 %token LITERAL_FLOAT             "float literal"
255 %token LITERAL_DOUBLE            "double literal"
256 %token LITERAL_DECIMAL           "decimal literal"
257 %token LITERAL_CHARACTER         "character literal"
258 %token LITERAL_STRING            "string literal"
259
260 %token IDENTIFIER
261 %token CLOSE_PARENS_CAST
262 %token CLOSE_PARENS_NO_CAST
263 %token CLOSE_PARENS_OPEN_PARENS
264 %token CLOSE_PARENS_MINUS
265
266 /* Add precedence rules to solve dangling else s/r conflict */
267 %nonassoc LOWPREC
268 %nonassoc IF
269 %nonassoc ELSE
270 %right ASSIGN
271 %left OP_OR
272 %left OP_AND
273 %left BITWISE_OR
274 %left BITWISE_AND
275 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
276 %left PLUS MINUS
277 %left STAR DIV PERCENT
278 %right BANG CARRET UMINUS
279 %nonassoc OP_INC OP_DEC
280 %left OPEN_PARENS
281 %left OPEN_BRACKET OPEN_BRACE
282 %left DOT
283 %nonassoc HIGHPREC
284
285 %start compilation_unit
286 %%
287
288 compilation_unit
289         : outer_declarations opt_EOF
290         | outer_declarations attribute_sections opt_EOF
291         | attribute_sections opt_EOF
292         | opt_EOF /* allow empty files */
293         ;
294         
295 opt_EOF
296         : /* empty */
297         | EOF
298         ;
299
300 outer_declarations
301         : outer_declaration
302         | outer_declarations outer_declaration
303         ;
304  
305 outer_declaration
306         : using_directive
307         | namespace_member_declaration
308         ;
309   
310 using_directives
311         : using_directive 
312         | using_directives using_directive
313         ;
314
315 using_directive
316         : using_alias_directive
317         | using_namespace_directive
318         ;
319
320 using_alias_directive
321         : USING IDENTIFIER ASSIGN 
322           namespace_or_type_name SEMICOLON
323           {
324                   current_namespace.UsingAlias ((string) $2, (Expression) $4, lexer.Location);
325           }
326         | USING error {
327                 CheckIdentifierToken (yyToken);
328           }
329         ;
330
331 using_namespace_directive
332         : USING namespace_name SEMICOLON 
333           {
334                 current_namespace.Using ((string) $2, lexer.Location);
335           }
336         ;
337
338 //
339 // Strictly speaking, namespaces don't have attributes but
340 // we parse global attributes along with namespace declarations and then
341 // detach them
342 // 
343 namespace_declaration
344         : opt_attributes NAMESPACE qualified_identifier 
345         {
346                 Attributes attrs = (Attributes) $1;
347
348                 if (attrs != null) {
349                         foreach (AttributeSection asec in attrs.AttributeSections)
350                                 if (asec.Target == "assembly")
351                                         RootContext.AddGlobalAttributeSection (current_container, asec);
352                                 else
353                                         Report.Error(1518, Lexer.Location,
354                                         "Attributes cannot be applied to namespaces."
355                                         + " Expected class, delegate, enum, interface, or struct");
356                 }
357
358                 current_namespace = RootContext.Tree.RecordNamespace (current_namespace, file, $3.ToString (), lexer.Location);
359         } 
360           namespace_body opt_semicolon
361           { 
362                 current_namespace = current_namespace.Parent;
363           }
364         ;
365
366 opt_semicolon
367         : /* empty */
368         | SEMICOLON
369         ;
370
371 opt_comma
372         : /* empty */
373         | COMMA
374         ;
375
376 qualified_identifier
377         : IDENTIFIER { $$ = new QualifiedIdentifier ((string) $1); }
378         | qualified_identifier DOT IDENTIFIER { 
379                 $$ = new QualifiedIdentifier ((QualifiedIdentifier) $1, (string) $3);
380           }
381         ;
382
383 namespace_name
384         : qualified_identifier { $$ = ($1).ToString (); }
385         ;
386
387 namespace_body
388         : OPEN_BRACE
389           opt_using_directives
390           opt_namespace_member_declarations
391           CLOSE_BRACE
392           {
393           }
394         ;
395
396 opt_using_directives
397         : /* empty */
398         | using_directives
399         ;
400
401 opt_namespace_member_declarations
402         : /* empty */
403         | namespace_member_declarations
404         ;
405
406 namespace_member_declarations
407         : namespace_member_declaration
408         | namespace_member_declarations namespace_member_declaration
409         ;
410
411 namespace_member_declaration
412         : type_declaration
413           {
414                 string name = "";
415                 int mod_flags;
416
417                 if ($1 is Class){
418                         Class c = (Class) $1;
419                         mod_flags = c.ModFlags;
420                         name = c.Name;
421                 } else if ($1 is Struct){
422                         Struct s = (Struct) $1;
423                         mod_flags = s.ModFlags;
424                         name = s.Name;
425                 } else
426                         break;
427
428                 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
429                         Report.Error (
430                                 1527, lexer.Location, 
431                                 "Namespace elements cant be explicitly " +
432                                 "declared private or protected in `" + name + "'");
433                 }
434                 current_namespace.DeclarationFound = true;
435           }
436         | namespace_declaration {
437                 current_namespace.DeclarationFound = true;
438           }
439
440         | field_declaration {
441                 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
442           }
443         | method_declaration {
444                 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
445           }
446         ;
447
448 type_declaration
449         : class_declaration             
450         | struct_declaration            
451         | interface_declaration         
452         | enum_declaration              
453         | delegate_declaration
454 //
455 // Enable this when we have handled all errors, because this acts as a generic fallback
456 //
457 //      | error {
458 //              Console.WriteLine ("Token=" + yyToken);
459 //              Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
460 //        }
461         ;
462
463 //
464 // Attributes 17.2
465 //
466
467 opt_attributes
468         : /* empty */
469         | attribute_sections { $$ = $1; }
470         ;
471  
472 attribute_sections
473         : attribute_section
474           {
475                 AttributeSection sect = (AttributeSection) $1;
476
477                 if (sect.Target == "assembly") {
478                         RootContext.AddGlobalAttributeSection (current_container, sect);
479                         $$ = null;
480                 }
481                 else
482                         $$ = new Attributes (sect);
483           }
484         | attribute_sections attribute_section
485           {
486                 Attributes attrs = $1 as Attributes;
487                 AttributeSection sect = (AttributeSection) $2;
488
489                 if (sect.Target == "assembly") {
490                         RootContext.AddGlobalAttributeSection (current_container, sect);
491                 } else {
492                         if (attrs == null)
493                                 attrs = new Attributes (sect);
494                         else
495                         attrs.AddAttributeSection (sect);
496                 }
497                 
498                 $$ = attrs;
499           }
500         ;
501
502 attribute_section
503         : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
504           {
505                 string target = null;
506                 
507                 if ($2 != null)
508                         target = (string) $2;
509                 
510                 $$ = new AttributeSection (target, (ArrayList) $3);
511           }
512         | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
513           {
514                 $$ = new AttributeSection (null, (ArrayList) $2);
515           }
516         ;
517  
518 attribute_target_specifier
519         : attribute_target COLON
520           {
521                 $$ = $1;
522           }
523         ;
524
525 attribute_target
526         : IDENTIFIER
527           {
528                 CheckAttributeTarget ((string) $1);
529                 $$ = $1;
530           }
531         | EVENT  { $$ = "event"; }        
532         | RETURN { $$ = "return"; }
533         ;
534
535 attribute_list
536         : attribute
537           {
538                 ArrayList attrs = new ArrayList (4);
539                 attrs.Add ($1);
540
541                 $$ = attrs;
542                
543           }
544         | attribute_list COMMA attribute
545           {
546                 ArrayList attrs = (ArrayList) $1;
547                 attrs.Add ($3);
548
549                 $$ = attrs;
550           }
551         ;
552
553 attribute
554         : attribute_name
555           {
556                 $$ = lexer.Location;
557           }
558           opt_attribute_arguments
559           {
560                 //
561                 // Attributes need a string, not an expression: generic types will fail here.
562                 //
563                 $$ = new Attribute (((Expression) $1).ToString (), (ArrayList) $3, (Location) $2);
564           }
565         ;
566
567 attribute_name
568         : type_name  { /* reserved attribute name or identifier: 17.4 */ }
569         ;
570
571 opt_attribute_arguments
572         : /* empty */   { $$ = null; }
573         | OPEN_PARENS attribute_arguments CLOSE_PARENS
574           {
575                 $$ = $2;
576           }
577         ;
578
579
580 attribute_arguments
581         : opt_positional_argument_list
582           {
583                 if ($1 == null)
584                         $$ = null;
585                 else {
586                         ArrayList args = new ArrayList (4);
587                         args.Add ($1);
588                 
589                         $$ = args;
590                 }
591           }
592         | positional_argument_list COMMA named_argument_list
593           {
594                 ArrayList args = new ArrayList (4);
595                 args.Add ($1);
596                 args.Add ($3);
597
598                 $$ = args;
599           }
600         | named_argument_list
601           {
602                 ArrayList args = new ArrayList (4);
603                 args.Add (null);
604                 args.Add ($1);
605                 
606                 $$ = args;
607           }
608         ;
609
610
611 opt_positional_argument_list
612         : /* empty */           { $$ = null; } 
613         | positional_argument_list
614         ;
615
616 positional_argument_list
617         : expression
618           {
619                 ArrayList args = new ArrayList (4);
620                 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
621
622                 $$ = args;
623           }
624         | positional_argument_list COMMA expression
625          {
626                 ArrayList args = (ArrayList) $1;
627                 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
628
629                 $$ = args;
630          }
631         ;
632
633 named_argument_list
634         : named_argument
635           {
636                 ArrayList args = new ArrayList (4);
637                 args.Add ($1);
638
639                 $$ = args;
640           }
641         | named_argument_list COMMA named_argument
642           {       
643                 ArrayList args = (ArrayList) $1;
644                 args.Add ($3);
645
646                 $$ = args;
647           }
648         ;
649
650 named_argument
651         : IDENTIFIER ASSIGN expression
652           {
653                 $$ = new DictionaryEntry (
654                         (string) $1, 
655                         new Argument ((Expression) $3, Argument.AType.Expression));
656           }
657         ;
658
659                   
660 class_body
661         :  OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
662         ;
663
664 opt_class_member_declarations
665         : /* empty */
666         | class_member_declarations
667         ;
668
669 class_member_declarations
670         : class_member_declaration
671         | class_member_declarations 
672           class_member_declaration
673         ;
674
675 class_member_declaration
676         : constant_declaration                  // done
677         | field_declaration                     // done
678         | method_declaration                    // done
679         | property_declaration                  // done
680         | event_declaration                     // done
681         | indexer_declaration                   // done
682         | operator_declaration                  // done
683         | constructor_declaration               // done
684         | destructor_declaration                // done
685         | type_declaration
686         ;
687
688 struct_declaration
689         : opt_attributes
690           opt_modifiers
691           STRUCT IDENTIFIER
692           { 
693                 Struct new_struct;
694                 string full_struct_name = MakeName ((string) $4);
695
696                 new_struct = new Struct (current_namespace, current_container, full_struct_name,
697                                          (int) $2, (Attributes) $1, lexer.Location);
698                 current_container = new_struct;
699                 RootContext.Tree.RecordDecl (full_struct_name, new_struct);
700                 lexer.ConstraintsParsing = true;
701           }
702           opt_type_parameter_list
703           opt_class_base
704           opt_type_parameter_constraints_clauses
705           {
706                 lexer.ConstraintsParsing = false;
707           }
708           struct_body
709           opt_semicolon
710           {
711                 Struct new_struct = (Struct) current_container;
712
713                 if ($8 != null && $6 == null)
714                         Report.Error (-200, new_struct.Location,
715                                       "Type parameter constraints only valid if there is a type parameter list");
716                 if ($6 != null)
717                         CheckDef (new_struct.SetParameterInfo ((ArrayList) $6, (ArrayList) $8), new_struct.Name, new_struct.Location);
718
719                 if ($7 != null)
720                         new_struct.Bases = (ArrayList) $7;
721
722                 current_container = current_container.Parent;
723                 CheckDef (current_container.AddStruct (new_struct), new_struct.Name, new_struct.Location);
724                 $$ = new_struct;
725           }
726         | opt_attributes opt_modifiers STRUCT error {
727                 CheckIdentifierToken (yyToken);
728           }
729         ;
730
731 struct_body
732         : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
733         ;
734
735 opt_struct_member_declarations
736         : /* empty */
737         | struct_member_declarations
738         ;
739
740 struct_member_declarations
741         : struct_member_declaration
742         | struct_member_declarations struct_member_declaration
743         ;
744
745 struct_member_declaration
746         : constant_declaration
747         | field_declaration
748         | method_declaration
749         | property_declaration
750         | event_declaration
751         | indexer_declaration
752         | operator_declaration
753         | constructor_declaration
754         | type_declaration
755
756         /*
757          * This is only included so we can flag error 575: 
758          * destructors only allowed on class types
759          */
760         | destructor_declaration 
761         ;
762
763 constant_declaration
764         : opt_attributes 
765           opt_modifiers
766           CONST
767           type
768           constant_declarators
769           SEMICOLON
770           {
771                 foreach (VariableDeclaration constant in (ArrayList) $5){
772                         Location l = constant.Location;
773
774                         Const c = new Const (
775                                 (Expression) $4, (string) constant.identifier, 
776                                 (Expression) constant.expression_or_array_initializer, (int) $2, 
777                                 (Attributes) $1, l);
778
779                         CheckDef (current_container.AddConstant (c), c.Name, l);
780                 }
781           }
782         ;
783
784 constant_declarators
785         : constant_declarator 
786           {
787                 ArrayList constants = new ArrayList (4);
788                 constants.Add ($1);
789                 $$ = constants;
790           }
791         | constant_declarators COMMA constant_declarator
792           {
793                 ArrayList constants = (ArrayList) $1;
794
795                 constants.Add ($3);
796           }
797         ;
798
799 constant_declarator
800         : IDENTIFIER ASSIGN constant_expression
801           {
802                 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
803           }
804         ;
805
806 field_declaration
807         : opt_attributes
808           opt_modifiers
809           type 
810           variable_declarators
811           SEMICOLON
812           { 
813                 Expression type = (Expression) $3;
814                 int mod = (int) $2;
815
816                 foreach (VariableDeclaration var in (ArrayList) $4){
817                         Location l = var.Location;
818
819                         Field field = new Field (type, mod, var.identifier, 
820                                                  var.expression_or_array_initializer, 
821                                                  (Attributes) $1, l);
822
823                         CheckDef (current_container.AddField (field), field.Name, l);
824                 }
825           }
826         | opt_attributes
827           opt_modifiers
828           VOID  
829           variable_declarators
830           SEMICOLON {
831                 Report.Error (670, lexer.Location, "void type is not allowed for fields");
832           }
833         ;
834
835 variable_declarators
836         : variable_declarator 
837           {
838                 ArrayList decl = new ArrayList (4);
839                 decl.Add ($1);
840                 $$ = decl;
841           }
842         | variable_declarators COMMA variable_declarator
843           {
844                 ArrayList decls = (ArrayList) $1;
845                 decls.Add ($3);
846                 $$ = $1;
847           }
848         ;
849
850 variable_declarator
851         : IDENTIFIER ASSIGN variable_initializer
852           {
853                 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
854           }
855         | IDENTIFIER
856           {
857                 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
858           }
859         ;
860
861 variable_initializer
862         : expression
863           {
864                 $$ = $1;
865           }
866         | array_initializer
867           {
868                 $$ = $1;
869           }
870         | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
871           {
872                 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
873           }
874         ;
875
876 method_declaration
877         : method_header {
878                 iterator_container = (IIteratorContainer) $1;
879           }
880           method_body
881           {
882                 Method method = (Method) $1;
883                 Block b = (Block) $3;
884                 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
885
886                 if (b == null){
887                         if ((method.ModFlags & extern_abstract) == 0){
888                                 Report.Error (
889                                         501, lexer.Location,  current_container.MakeName (method.Name) +
890                                         "must declare a body because it is not marked abstract or extern");
891                         }
892                 } else {
893                         if ((method.ModFlags & Modifiers.EXTERN) != 0){
894                                 Report.Error (
895                                         179, lexer.Location, current_container.MakeName (method.Name) +
896                                         " is declared extern, but has a body");
897                         }
898                 }
899
900                 method.Block = (Block) $3;
901                 CheckDef (current_container.AddMethod (method), method.Name, method.Location);
902
903                 current_local_parameters = null;
904                 iterator_container = null;
905           }
906         ;
907
908 opt_error_modifier
909         : /* empty */
910         | modifiers 
911           {
912                 int m = (int) $1;
913                 int i = 1;
914
915                 while (m != 0){
916                         if ((i & m) != 0){
917                                 Report.Error (
918                                         1585, lexer.Location, "Member modifier `" + 
919                                         Modifiers.Name (i) + "' must precede member type and name");
920                         }
921                         m &= ~i;
922                         i = i << 1;
923                 }
924           }
925         ;
926
927 method_header
928         : opt_attributes
929           opt_modifiers
930           type
931           member_name
932           opt_type_parameter_list
933           OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS 
934           {
935                 lexer.ConstraintsParsing = true;
936           }
937           opt_type_parameter_constraints_clauses
938           {
939                 lexer.ConstraintsParsing = false;
940                 if ($10 != null && $5 == null)
941                         Report.Error (-200, lexer.Location,
942                                       "Type parameter constraints only valid if there is a type parameter list");
943
944                 Method method;
945                 GenericMethod generic = null;
946                 if ($5 != null) {
947                         generic = new GenericMethod (current_namespace, current_container,
948                                                      (string) $4, (Attributes) $1,
949                                                      lexer.Location);
950
951                         CheckDef (generic.SetParameterInfo ((ArrayList) $5, (ArrayList) $10), generic.Name, generic.Location);
952                         method = new Method (generic, (Expression) $3, (int) $2, (string) $4,
953                                              (Parameters) $7, (Attributes) $1, lexer.Location);
954                 } else
955                         method = new Method (current_container, (Expression) $3, (int) $2, (string) $4, 
956                                              (Parameters) $7, (Attributes) $1, lexer.Location);
957
958                 current_local_parameters = (Parameters) $7;
959
960                 $$ = method;
961           }
962         | opt_attributes
963           opt_modifiers
964           VOID
965           member_name
966           opt_type_parameter_list
967           OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS 
968           {
969                 lexer.ConstraintsParsing = true;
970           }
971           opt_type_parameter_constraints_clauses
972           {
973                 lexer.ConstraintsParsing = false;
974                 if ($10 != null && $5 == null)
975                         Report.Error (-200, lexer.Location,
976                                       "Type parameter constraints only valid if there is a type parameter list");
977
978                 Method method;
979                 GenericMethod generic = null;
980                 if ($5 != null) {
981                         generic = new GenericMethod (current_namespace, current_container,
982                                                      (string) $4, (Attributes) $1,
983                                                      lexer.Location);
984
985                         CheckDef (generic.SetParameterInfo ((ArrayList) $5, (ArrayList) $10), generic.Name, generic.Location);
986
987                         method = new Method (generic, TypeManager.system_void_expr, (int) $2,
988                                             (string) $4, (Parameters) $7, (Attributes) $1,
989                                             lexer.Location);
990                 } else
991                         method = new Method (current_container, TypeManager.system_void_expr, (int) $2,
992                                             (string) $4, (Parameters) $7, (Attributes) $1,
993                                             lexer.Location);
994
995                 current_local_parameters = (Parameters) $7;
996
997                 $$ = method;
998           }
999         | opt_attributes
1000           opt_modifiers
1001           type 
1002           modifiers member_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1003           {
1004                 Report.Error (1585, lexer.Location, 
1005                         String.Format ("Modifier {0} should appear before type", 
1006                                 Modifiers.Name ((int) $4)));
1007                 Method method = new Method (current_container, TypeManager.system_void_expr, 0,
1008                                             (string) $5, (Parameters) $7, (Attributes) $1,
1009                                             lexer.Location);
1010
1011                 current_local_parameters = (Parameters) $7;
1012                 $$ = method;
1013           }
1014         ;
1015
1016 method_body
1017         : block
1018         | SEMICOLON             { $$ = null; }
1019         ;
1020
1021 opt_formal_parameter_list
1022         : /* empty */                   { $$ = Parameters.EmptyReadOnlyParameters; }
1023         | formal_parameter_list
1024         ;
1025
1026 formal_parameter_list
1027         : fixed_parameters              
1028           { 
1029                 ArrayList pars_list = (ArrayList) $1;
1030
1031                 Parameter [] pars = new Parameter [pars_list.Count];
1032                 pars_list.CopyTo (pars);
1033
1034                 $$ = new Parameters (pars, null, lexer.Location); 
1035           } 
1036         | fixed_parameters COMMA parameter_array
1037           {
1038                 ArrayList pars_list = (ArrayList) $1;
1039
1040                 Parameter [] pars = new Parameter [pars_list.Count];
1041                 pars_list.CopyTo (pars);
1042
1043                 $$ = new Parameters (pars, (Parameter) $3, lexer.Location); 
1044           }
1045         | parameter_array 
1046           {
1047                 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
1048           }
1049         ;
1050
1051 fixed_parameters
1052         : fixed_parameter       
1053           {
1054                 ArrayList pars = new ArrayList (4);
1055
1056                 pars.Add ($1);
1057                 $$ = pars;
1058           }
1059         | fixed_parameters COMMA fixed_parameter
1060           {
1061                 ArrayList pars = (ArrayList) $1;
1062
1063                 pars.Add ($3);
1064                 $$ = $1;
1065           }
1066         ;
1067
1068 fixed_parameter
1069         : opt_attributes
1070           opt_parameter_modifier
1071           type
1072           IDENTIFIER
1073           {
1074                 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
1075           }
1076         | opt_attributes
1077           opt_parameter_modifier
1078           type
1079           error {
1080                 CheckIdentifierToken (yyToken);
1081                 $$ = null;
1082           }
1083         ;
1084
1085 opt_parameter_modifier
1086         : /* empty */           { $$ = Parameter.Modifier.NONE; }
1087         | parameter_modifier
1088         ;
1089
1090 parameter_modifier
1091         : REF                   { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1092         | OUT                   { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1093         ;
1094
1095 parameter_array
1096         : opt_attributes PARAMS type IDENTIFIER
1097           { 
1098                 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1099                 note ("type must be a single-dimension array type"); 
1100           }
1101         | opt_attributes PARAMS type error {
1102                 CheckIdentifierToken (yyToken);
1103                 $$ = null;
1104           }
1105         ;
1106
1107 member_name 
1108         : qualified_identifier { $$ = ($1).ToString (); } 
1109         ;
1110
1111 property_declaration
1112         : opt_attributes
1113           opt_modifiers
1114           type member_name
1115           OPEN_BRACE 
1116           {
1117                 implicit_value_parameter_type = (Expression) $3;
1118
1119                 lexer.PropertyParsing = true;
1120
1121                 $$ = lexer.Location;
1122           }
1123           accessor_declarations 
1124           {
1125                 lexer.PropertyParsing = false;
1126           }
1127           CLOSE_BRACE
1128           { 
1129                 Property prop;
1130                 Pair pair = (Pair) $7;
1131                 Accessor get_block = (Accessor) pair.First;
1132                 Accessor set_block = (Accessor) pair.Second;
1133
1134                 Location loc = (Location) $6;
1135                 prop = new Property (current_container, (Expression) $3, (string) $4, (int) $2,
1136                                      get_block, set_block, (Attributes) $1, loc);
1137                 
1138                 CheckDef (current_container.AddProperty (prop), prop.Name, loc);
1139                 implicit_value_parameter_type = null;
1140           }
1141         ;
1142
1143 accessor_declarations
1144         : get_accessor_declaration opt_set_accessor_declaration
1145           { 
1146                 $$ = new Pair ($1, $2);
1147           }
1148         | set_accessor_declaration opt_get_accessor_declaration
1149           {
1150                 $$ = new Pair ($2, $1);
1151           }
1152         ;
1153
1154 opt_get_accessor_declaration
1155         : /* empty */                   { $$ = null; }
1156         | get_accessor_declaration
1157         ;
1158
1159 opt_set_accessor_declaration
1160         : /* empty */                   { $$ = null; }
1161         | set_accessor_declaration
1162         ;
1163
1164 get_accessor_declaration
1165         : opt_attributes GET
1166           {
1167                 // If this is not the case, then current_local_parameters has already
1168                 // been set in indexer_declaration
1169                 if (parsing_indexer == false)
1170                         current_local_parameters = null;
1171                 else 
1172                         current_local_parameters = indexer_parameters;
1173                 lexer.PropertyParsing = false;
1174           }
1175           accessor_body
1176           {
1177                 $$ = new Accessor ((Block) $4, (Attributes) $1);
1178                 current_local_parameters = null;
1179                 lexer.PropertyParsing = true;
1180           }
1181         ;
1182
1183 set_accessor_declaration
1184         : opt_attributes SET 
1185           {
1186                 Parameter [] args;
1187                 Parameter implicit_value_parameter = new Parameter (
1188                         implicit_value_parameter_type, "value", 
1189                         Parameter.Modifier.NONE, null);
1190
1191                 if (parsing_indexer == false) {
1192                         args  = new Parameter [1];
1193                         args [0] = implicit_value_parameter;
1194                         current_local_parameters = new Parameters (args, null, lexer.Location);
1195                 } else {
1196                         Parameter [] fpars = indexer_parameters.FixedParameters;
1197
1198                         if (fpars != null){
1199                                 int count = fpars.Length;
1200
1201                                 args = new Parameter [count + 1];
1202                                 fpars.CopyTo (args, 0);
1203                                 args [count] = implicit_value_parameter;
1204                         } else 
1205                                 args = null;
1206                         current_local_parameters = new Parameters (
1207                                 args, indexer_parameters.ArrayParameter, lexer.Location);
1208                 }
1209                 
1210                 lexer.PropertyParsing = false;
1211           }
1212           accessor_body
1213           {
1214                 $$ = new Accessor ((Block) $4, (Attributes) $1);
1215                 current_local_parameters = null;
1216                 lexer.PropertyParsing = true;
1217           }
1218         ;
1219
1220 accessor_body
1221         : block 
1222         | SEMICOLON             { $$ = null; }
1223         ;
1224
1225 interface_declaration
1226         : opt_attributes
1227           opt_modifiers
1228           INTERFACE IDENTIFIER
1229           {
1230                 Interface new_interface;
1231                 string full_interface_name = MakeName ((string) $4);
1232
1233                 new_interface = new Interface (current_namespace, current_container, full_interface_name,
1234                                                (int) $2, (Attributes) $1, lexer.Location);
1235                 if (current_interface != null) {
1236                         Location l = lexer.Location;
1237                         Report.Error (-2, l, "Internal compiler error: interface inside interface");
1238                 }
1239                 current_interface = new_interface;
1240                 RootContext.Tree.RecordDecl (full_interface_name, new_interface);
1241                 lexer.ConstraintsParsing = true;
1242           }
1243           opt_type_parameter_list
1244           opt_interface_base
1245           opt_type_parameter_constraints_clauses
1246           {
1247                 lexer.ConstraintsParsing = false;
1248           }
1249           interface_body opt_semicolon
1250           { 
1251                 Interface new_interface = (Interface) current_interface;
1252
1253                 if ($8 != null && $6 == null)
1254                         Report.Error (-200, new_interface.Location,
1255                                       "Type parameter constraints only valid if there is a type parameter list");
1256
1257                 if ($6 != null)
1258                         CheckDef (new_interface.SetParameterInfo ((ArrayList) $6, (ArrayList) $8), new_interface.Name, new_interface.Location);
1259                 if ($7 != null)
1260                         new_interface.Bases = (ArrayList) $7;
1261
1262                 current_interface = null;
1263                 CheckDef (current_container.AddInterface (new_interface), 
1264                           new_interface.Name, new_interface.Location);
1265           }
1266         | opt_attributes opt_modifiers INTERFACE error {
1267                 CheckIdentifierToken (yyToken);
1268           }
1269         ;
1270
1271 opt_interface_base
1272         : /* empty */                     { $$ = null; }
1273         | interface_base
1274         ;
1275
1276 interface_base
1277         : COLON interface_type_list       { $$ = $2; }
1278         ;
1279
1280 interface_type_list
1281         : interface_type
1282           {
1283                 ArrayList interfaces = new ArrayList (4);
1284
1285                 interfaces.Add ($1);
1286                 $$ = interfaces;
1287           }
1288         | interface_type_list COMMA interface_type
1289           {
1290                 ArrayList interfaces = (ArrayList) $1;
1291                 interfaces.Add ($3);
1292                 $$ = interfaces;
1293           }
1294         ;
1295
1296 interface_body
1297         : OPEN_BRACE
1298           opt_interface_member_declarations
1299           CLOSE_BRACE
1300         ;
1301
1302 opt_interface_member_declarations
1303         : /* empty */
1304         | interface_member_declarations
1305         ;
1306
1307 interface_member_declarations
1308         : interface_member_declaration
1309         | interface_member_declarations interface_member_declaration
1310         ;
1311
1312 interface_member_declaration
1313         : interface_method_declaration          
1314           { 
1315                 InterfaceMethod m = (InterfaceMethod) $1;
1316
1317                 CheckDef (current_interface.AddMethod (m), m.Name, m.Location);
1318           }
1319         | interface_property_declaration        
1320           { 
1321                 InterfaceProperty p = (InterfaceProperty) $1;
1322
1323                 CheckDef (current_interface.AddProperty (p), p.Name, p.Location);
1324           }
1325         | interface_event_declaration 
1326           { 
1327                 if ($1 != null){
1328                 InterfaceEvent e = (InterfaceEvent) $1;
1329                 CheckDef (current_interface.AddEvent (e), e.Name, lexer.Location);
1330           }
1331           }
1332         | interface_indexer_declaration
1333           { 
1334                 InterfaceIndexer i = (InterfaceIndexer) $1;
1335
1336                 CheckDef (current_interface.AddIndexer (i), "indexer", i.Location);
1337           }
1338         ;
1339
1340 opt_new
1341         : opt_modifiers 
1342           {
1343                 int val = (int) $1;
1344                 
1345                 val = Modifiers.Check (Modifiers.NEW, val, 0, lexer.Location);
1346                 $$ = (bool) (val != 0);
1347           }
1348         ;
1349
1350 interface_method_declaration
1351         : opt_attributes opt_new type IDENTIFIER 
1352           OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1353           SEMICOLON
1354           {
1355                 $$ = new InterfaceMethod ((Expression) $3, (string) $4, (bool) $2, 
1356                                           (Parameters) $6, (Attributes) $1, lexer.Location);
1357           }
1358         | opt_attributes opt_new VOID IDENTIFIER 
1359           OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1360           SEMICOLON
1361           {
1362                 $$ = new InterfaceMethod (
1363                         TypeManager.system_void_expr, (string) $4, (bool) $2, (Parameters) $6, 
1364                                           (Attributes) $1, lexer.Location);
1365           }
1366         ;
1367
1368 interface_property_declaration
1369         : opt_attributes
1370           opt_new
1371           type IDENTIFIER 
1372           OPEN_BRACE 
1373           { lexer.PropertyParsing = true; }
1374           interface_accesors 
1375           { lexer.PropertyParsing = false; }
1376           CLOSE_BRACE
1377           {
1378                 int gs = (int) $7;
1379
1380                 $$ = new InterfaceProperty ((Expression) $3, (string) $4, (bool) $2, 
1381                                             (gs & 1) == 1, (gs & 2) == 2, (Attributes) $1,
1382                                             lexer.Location);
1383           }
1384         | opt_attributes
1385           opt_new
1386           type error {
1387                 CheckIdentifierToken (yyToken);
1388                 $$ = null;
1389           }
1390         ;
1391
1392 interface_accesors
1393         : opt_attributes GET SEMICOLON          { $$ = 1; }
1394         | opt_attributes SET SEMICOLON          { $$ = 2; }
1395         | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON 
1396           { $$ = 3; }
1397         | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1398           { $$ = 3; }
1399         ;
1400
1401 interface_event_declaration
1402         : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1403           {
1404                 $$ = new InterfaceEvent ((Expression) $4, (string) $5, (bool) $2, (Attributes) $1,
1405                                          lexer.Location);
1406           }
1407         | opt_attributes opt_new EVENT type error {
1408                 CheckIdentifierToken (yyToken);
1409                 $$ = null;
1410           }
1411         | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN  {
1412                 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1413                 $$ = null;
1414           }
1415         | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE event_accessor_declarations CLOSE_BRACE {
1416                 Report.Error (69, lexer.Location, "Event accessors not valid on interfaces");
1417                 $$ = null;
1418           }
1419         ;
1420
1421 interface_indexer_declaration 
1422         : opt_attributes opt_new type THIS 
1423           OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1424           OPEN_BRACE 
1425           { lexer.PropertyParsing = true; }
1426           interface_accesors 
1427           { lexer.PropertyParsing = false; }
1428           CLOSE_BRACE
1429           {
1430                 int a_flags = (int) $10;
1431
1432                 bool do_get = (a_flags & 1) == 1;
1433                 bool do_set = (a_flags & 2) == 2;
1434
1435                 $$ = new InterfaceIndexer ((Expression) $3, (Parameters) $6, do_get, do_set,
1436                                            (bool) $2, (Attributes) $1, lexer.Location);
1437           }
1438         ;
1439
1440 operator_declaration
1441         : opt_attributes opt_modifiers operator_declarator operator_body
1442           {
1443                 OperatorDeclaration decl = (OperatorDeclaration) $3;
1444                 
1445                 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
1446                                             decl.arg2type, decl.arg2name, (Block) $4, (Attributes) $1, decl.location);
1447
1448                 // Note again, checking is done in semantic analysis
1449                 current_container.AddOperator (op);
1450
1451                 current_local_parameters = null;
1452           }
1453         ;
1454
1455 operator_body 
1456         : block
1457         | SEMICOLON { $$ = null; }
1458         ; 
1459 operator_declarator
1460         : type OPERATOR overloadable_operator 
1461           OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1462         {
1463                 Operator.OpType op = (Operator.OpType) $3;
1464                 CheckUnaryOperator (op);
1465
1466                 if (op == Operator.OpType.Addition)
1467                         op = Operator.OpType.UnaryPlus;
1468
1469                 if (op == Operator.OpType.Subtraction)
1470                         op = Operator.OpType.UnaryNegation;
1471
1472                 Parameter [] pars = new Parameter [1];
1473
1474                 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1475
1476                 current_local_parameters = new Parameters (pars, null, lexer.Location);
1477
1478                 $$ = new OperatorDeclaration (op, (Expression) $1, (Expression) $5, (string) $6,
1479                                               null, null, lexer.Location);
1480         }
1481         | type OPERATOR overloadable_operator
1482           OPEN_PARENS 
1483                 type IDENTIFIER COMMA
1484                 type IDENTIFIER 
1485           CLOSE_PARENS
1486         {
1487                CheckBinaryOperator ((Operator.OpType) $3);
1488
1489                Parameter [] pars = new Parameter [2];
1490
1491                pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1492                pars [1] = new Parameter ((Expression) $8, (string) $9, Parameter.Modifier.NONE, null);
1493
1494                current_local_parameters = new Parameters (pars, null, lexer.Location);
1495                
1496                $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1, 
1497                                              (Expression) $5, (string) $6,
1498                                              (Expression) $8, (string) $9, lexer.Location);
1499         }
1500         | conversion_operator_declarator
1501         ;
1502
1503 overloadable_operator
1504 // Unary operators:
1505         : BANG   { $$ = Operator.OpType.LogicalNot; }
1506         | TILDE  { $$ = Operator.OpType.OnesComplement; }  
1507         | OP_INC { $$ = Operator.OpType.Increment; }
1508         | OP_DEC { $$ = Operator.OpType.Decrement; }
1509         | TRUE   { $$ = Operator.OpType.True; }
1510         | FALSE  { $$ = Operator.OpType.False; }
1511 // Unary and binary:
1512         | PLUS { $$ = Operator.OpType.Addition; }
1513         | MINUS { $$ = Operator.OpType.Subtraction; }
1514 // Binary:
1515         | STAR { $$ = Operator.OpType.Multiply; }
1516         | DIV {  $$ = Operator.OpType.Division; }
1517         | PERCENT { $$ = Operator.OpType.Modulus; }
1518         | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1519         | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1520         | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1521         | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1522         | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1523         | OP_EQ { $$ = Operator.OpType.Equality; }
1524         | OP_NE { $$ = Operator.OpType.Inequality; }
1525         | OP_GT { $$ = Operator.OpType.GreaterThan; }
1526         | OP_LT { $$ = Operator.OpType.LessThan; }
1527         | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1528         | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1529         ;
1530
1531 conversion_operator_declarator
1532         : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1533           {
1534                 Parameter [] pars = new Parameter [1];
1535
1536                 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1537
1538                 current_local_parameters = new Parameters (pars, null, lexer.Location);  
1539                   
1540                 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1541                                               null, null, lexer.Location);
1542           }
1543         | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1544           {
1545                 Parameter [] pars = new Parameter [1];
1546
1547                 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1548
1549                 current_local_parameters = new Parameters (pars, null, lexer.Location);  
1550                   
1551                 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1552                                               null, null, lexer.Location);
1553           }
1554         | IMPLICIT error 
1555           {
1556                 syntax_error (lexer.Location, "'operator' expected");
1557           }
1558         | EXPLICIT error 
1559           {
1560                 syntax_error (lexer.Location, "'operator' expected");
1561           }
1562         ;
1563
1564 constructor_declaration
1565         : opt_attributes
1566           opt_modifiers
1567           constructor_declarator
1568           constructor_body
1569           { 
1570                 Constructor c = (Constructor) $3;
1571                 c.Block = (Block) $4;
1572                 c.OptAttributes = (Attributes) $1;
1573                 c.ModFlags = (int) $2;
1574         
1575                 if (c.Name == current_container.Basename){
1576                         if ((c.ModFlags & Modifiers.STATIC) != 0){
1577                                 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1578                                         Report.Error (
1579                                                 515, c.Location, String.Format (
1580                                                 "`{0}.{1}': static constructor can not have access modifiers",
1581                                                 c.Name, current_container.Name));
1582                                 }
1583         
1584                                 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);   
1585         
1586                                 if (c.Initializer != null){
1587                                         Report.Error (
1588                                                 514, c.Location, 
1589                                                 "Static constructors can not have an explicit this or base " +
1590                                                 "constructor invocations");
1591                                 }
1592         
1593                                 if (!c.Parameters.Empty){
1594                                         Report.Error (
1595                                                 132, c.Location, "Static constructors should not have parameters");
1596                                 }
1597                         } else {
1598                                 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1599                         }
1600                 } else {
1601                         // We let another layer check the validity of the constructor.
1602                         Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1603                 }
1604
1605                 CheckDef (current_container.AddConstructor (c), c.Name, c.Location);
1606
1607                 current_local_parameters = null;
1608           }
1609         ;
1610
1611 constructor_declarator
1612         : IDENTIFIER 
1613           OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS 
1614           {
1615                 oob_stack.Push (lexer.Location);
1616
1617                 current_local_parameters = (Parameters) $3;
1618           }
1619           opt_constructor_initializer
1620           {
1621                 Location l = (Location) oob_stack.Pop ();
1622                 $$ = new Constructor (current_container, (string) $1, (Parameters) $3,
1623                                       (ConstructorInitializer) $6, l);
1624           }
1625         ;
1626
1627 constructor_body
1628         : block
1629         | SEMICOLON             { $$ = null; }
1630         ;
1631
1632 opt_constructor_initializer
1633         : /* empty */                   { $$ = null; }
1634         | constructor_initializer
1635         ;
1636
1637 constructor_initializer
1638         : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1639           {
1640                 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1641           }
1642         | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1643           {
1644                 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1645           }
1646         | COLON error {
1647                 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1648                 $$ = null;
1649           }
1650         ;
1651
1652 opt_finalizer
1653         : /* EMPTY */           { $$ = 0; }
1654         | UNSAFE                { $$ = Modifiers.UNSAFE; }
1655         | EXTERN                { $$ = Modifiers.EXTERN; }
1656         ;
1657         
1658 destructor_declaration
1659         : opt_attributes opt_finalizer TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1660           {
1661                 if ((string) $4 != current_container.Basename){
1662                         Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1663                 } else if (!(current_container is Class)){
1664                         Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1665                 } else {
1666                         Location l = lexer.Location;
1667
1668                         int m = (int) $2;
1669                         if (!RootContext.StdLib && current_container.Name == "System.Object")
1670                                 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
1671                         else
1672                                 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
1673                         
1674                         if ((m & Modifiers.UNSAFE) != 0){
1675                                 if (!RootContext.Unsafe){
1676                                         Report.Error (227, l,
1677                                               "Unsafe code requires the --unsafe command " +
1678                                               "line option to be specified");
1679                                 }
1680                         }
1681                         
1682                         Method d = new Destructor (
1683                                 current_container, TypeManager.system_void_expr, m, "Finalize", 
1684                                 new Parameters (null, null, l), (Attributes) $1, l);
1685                   
1686                         d.Block = (Block) $7;
1687                         CheckDef (current_container.AddMethod (d), d.Name, d.Location);
1688                 }
1689           }
1690         ;
1691
1692 event_declaration
1693         : opt_attributes
1694           opt_modifiers
1695           EVENT type variable_declarators SEMICOLON
1696           {
1697                 foreach (VariableDeclaration var in (ArrayList) $5) {
1698
1699                         Event e = new Event ((Expression) $4, var.identifier, 
1700                                              var.expression_or_array_initializer,
1701                                              (int) $2, null, null, (Attributes) $1, lexer.Location);
1702
1703                         CheckDef (current_container.AddEvent (e), e.Name, e.Location);
1704                                        
1705                 }
1706           }
1707         | opt_attributes
1708           opt_modifiers
1709           EVENT type member_name
1710           OPEN_BRACE
1711           {
1712                 implicit_value_parameter_type = (Expression) $4;  
1713                 lexer.EventParsing = true;
1714                 oob_stack.Push (lexer.Location);
1715           }
1716           event_accessor_declarations
1717           {
1718                 lexer.EventParsing = false;  
1719           }
1720           CLOSE_BRACE
1721           {
1722                 Location loc = (Location) oob_stack.Pop ();
1723
1724                 if ($8 == null){
1725                         Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
1726                         $$ = null;
1727                 } else {
1728                 Pair pair = (Pair) $8;
1729                 Accessor add_accessor = null;
1730                 Accessor rem_accessor = null;
1731
1732                 if (pair.First != null)
1733                         add_accessor = (Accessor) pair.First;
1734                 if (pair.Second != null)
1735                         rem_accessor = (Accessor) pair.Second;
1736                 
1737                 Event e = new Event ((Expression) $4, (string) $5, null, (int) $2, add_accessor, rem_accessor,
1738                                      (Attributes) $1, loc);
1739                 
1740                 CheckDef (current_container.AddEvent (e), e.Name, loc);
1741                 implicit_value_parameter_type = null;
1742           }
1743           }
1744         ;
1745
1746 event_accessor_declarations
1747         : add_accessor_declaration remove_accessor_declaration
1748         {
1749                 $$ = new Pair ($1, $2);
1750         }
1751         | remove_accessor_declaration add_accessor_declaration
1752         {
1753                 $$ = new Pair ($2, $1);
1754         }       
1755         | add_accessor_declaration  { $$ = null; } 
1756         | remove_accessor_declaration { $$ = null; } 
1757         ;
1758
1759 add_accessor_declaration
1760         : opt_attributes ADD
1761           {
1762                 Parameter [] args = new Parameter [1];
1763                 Parameter implicit_value_parameter = new Parameter (
1764                         implicit_value_parameter_type, "value", 
1765                         Parameter.Modifier.NONE, null);
1766
1767                 args [0] = implicit_value_parameter;
1768                 
1769                 current_local_parameters = new Parameters (args, null, lexer.Location);  
1770                 lexer.EventParsing = false;
1771           }
1772           block
1773           {
1774                 $$ = new Accessor ((Block) $4, (Attributes) $1);
1775                 lexer.EventParsing = true;
1776           }
1777         | opt_attributes ADD error {
1778                 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1779                 $$ = null;
1780           }
1781         ;
1782
1783 remove_accessor_declaration
1784         : opt_attributes REMOVE
1785           {
1786                 Parameter [] args = new Parameter [1];
1787                 Parameter implicit_value_parameter = new Parameter (
1788                         implicit_value_parameter_type, "value", 
1789                         Parameter.Modifier.NONE, null);
1790
1791                 args [0] = implicit_value_parameter;
1792                 
1793                 current_local_parameters = new Parameters (args, null, lexer.Location);  
1794                 lexer.EventParsing = false;
1795           }
1796           block
1797           {
1798                 $$ = new Accessor ((Block) $4, (Attributes) $1);
1799                 lexer.EventParsing = true;
1800           }
1801         | opt_attributes REMOVE error {
1802                 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1803                 $$ = null;
1804           }
1805         ;
1806
1807 indexer_declaration
1808         : opt_attributes opt_modifiers indexer_declarator 
1809           OPEN_BRACE 
1810           {
1811                 IndexerDeclaration decl = (IndexerDeclaration) $3;
1812
1813                 implicit_value_parameter_type = decl.type;
1814                 
1815                 lexer.PropertyParsing = true;
1816                 parsing_indexer  = true;
1817                 
1818                 indexer_parameters = decl.param_list;
1819                 oob_stack.Push (lexer.Location);
1820           }
1821           accessor_declarations 
1822           {
1823                   lexer.PropertyParsing = false;
1824                   parsing_indexer  = false;
1825           }
1826           CLOSE_BRACE
1827           { 
1828                 // The signature is computed from the signature of the indexer.  Look
1829                 // at section 3.6 on the spec
1830                 Location loc = (Location) oob_stack.Pop ();
1831                 Indexer indexer;
1832                 IndexerDeclaration decl = (IndexerDeclaration) $3;
1833                 Pair pair = (Pair) $6;
1834                 Accessor get_block = (Accessor) pair.First;
1835                 Accessor set_block = (Accessor) pair.Second;
1836
1837                 indexer = new Indexer (current_container, decl.type, decl.interface_type, (int) $2,
1838                                        decl.param_list, get_block, set_block, (Attributes) $1, loc);
1839
1840                 // Note that there is no equivalent of CheckDef for this case
1841                 // We shall handle this in semantic analysis
1842                 
1843                 current_container.AddIndexer (indexer);
1844                 
1845                 current_local_parameters = null;
1846                 implicit_value_parameter_type = null;
1847                 indexer_parameters = null;
1848           }
1849         ;
1850
1851 indexer_declarator
1852         : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1853           {
1854                 Parameters pars = (Parameters) $4;
1855
1856                 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1857                         Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1858                 }
1859
1860                 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
1861           }
1862         | type qualified_identifier DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1863           {
1864                 Parameters pars = (Parameters) $6;
1865
1866                 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1867                         Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1868                 }
1869                 $$ = new IndexerDeclaration ((Expression) $1, $2.ToString (), pars);
1870           }
1871         ;
1872
1873 enum_declaration
1874         : opt_attributes
1875           opt_modifiers
1876           ENUM IDENTIFIER 
1877           opt_enum_base
1878           enum_body
1879           opt_semicolon
1880           { 
1881                 Location enum_location = lexer.Location;
1882
1883                 string full_name = MakeName ((string) $4);
1884                 Enum e = new Enum (current_namespace, current_container, (Expression) $5, (int) $2,
1885                                    full_name, (Attributes) $1, enum_location);
1886                 
1887                 foreach (VariableDeclaration ev in (ArrayList) $6) {
1888                         Location loc = (Location) ev.Location;
1889
1890                         CheckDef (e.AddEnumMember (ev.identifier, 
1891                                                    (Expression) ev.expression_or_array_initializer,
1892                                                    loc, ev.OptAttributes),
1893                                   ev.identifier, loc);
1894                 }
1895
1896                 CheckDef (current_container.AddEnum (e), full_name, enum_location);
1897                 RootContext.Tree.RecordDecl (full_name, e);
1898
1899           }
1900         ;
1901
1902 opt_enum_base
1903         : /* empty */           { $$ = TypeManager.system_int32_expr; }
1904         | COLON type            { $$ = $2;   }
1905         ;
1906
1907 enum_body
1908         : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1909           {
1910                 $$ = $2;
1911           }
1912         ;
1913
1914 opt_enum_member_declarations
1915         : /* empty */                   { $$ = new ArrayList (4); }
1916         | enum_member_declarations opt_comma { $$ = $1; }
1917         ;
1918
1919 enum_member_declarations
1920         : enum_member_declaration 
1921           {
1922                 ArrayList l = new ArrayList (4);
1923
1924                 l.Add ($1);
1925                 $$ = l;
1926           }
1927         | enum_member_declarations COMMA enum_member_declaration
1928           {
1929                 ArrayList l = (ArrayList) $1;
1930
1931                 l.Add ($3);
1932
1933                 $$ = l;
1934           }
1935         ;
1936
1937 enum_member_declaration
1938         : opt_attributes IDENTIFIER 
1939           {
1940                 $$ = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
1941           }
1942         | opt_attributes IDENTIFIER
1943           {
1944                   $$ = lexer.Location;
1945           }
1946           ASSIGN expression
1947           { 
1948                 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
1949           }
1950         ;
1951
1952 delegate_declaration
1953         : opt_attributes
1954           opt_modifiers
1955           DELEGATE type   
1956           IDENTIFIER OPEN_PARENS 
1957           opt_formal_parameter_list
1958           CLOSE_PARENS 
1959           SEMICOLON
1960           {
1961                 Location l = lexer.Location;
1962                 Delegate del = new Delegate (current_namespace, current_container, (Expression) $4,
1963                                              (int) $2, MakeName ((string) $5), (Parameters) $7, 
1964                                              (Attributes) $1, l);
1965                   
1966                 CheckDef (current_container.AddDelegate (del), del.Name, l);
1967           }     
1968         | opt_attributes
1969           opt_modifiers
1970           DELEGATE VOID   
1971           IDENTIFIER OPEN_PARENS 
1972           opt_formal_parameter_list
1973           CLOSE_PARENS 
1974           SEMICOLON
1975           {
1976                 Location l = lexer.Location;
1977                 Delegate del = new Delegate (
1978                         current_namespace, current_container,
1979                         TypeManager.system_void_expr, (int) $2, MakeName ((string) $5), 
1980                         (Parameters) $7, (Attributes) $1, l);
1981
1982                 CheckDef (current_container.AddDelegate (del), del.Name, l);
1983           }
1984         ;
1985
1986 type_name
1987         : namespace_or_type_name
1988         ;
1989
1990 namespace_or_type_name
1991         : IDENTIFIER opt_type_argument_list { 
1992                 if ($2 == null)
1993                         $$ = new SimpleName ((string) $1, lexer.Location);
1994                 else 
1995                         $$ = new ConstructedType ((string) $1, (TypeArguments) $2, lexer.Location);
1996           }
1997         | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list
1998           {
1999                 Expression right;
2000
2001                 //
2002                 // Third argument will become an Expression, when we have sorted out
2003                 // the issues with SimpleName first
2004                 //
2005
2006                 if ($4 == null)
2007                         $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2008                 else
2009                         $$ = new GenericMemberAccess ((Expression) $1, (string) $3,
2010                                                       (TypeArguments) $4, lexer.Location);
2011           }
2012         | namespace_or_type_name DOT DEFAULT
2013           {
2014                 $$ = new DefaultValueExpression ((Expression) $1, lexer.Location);
2015           }
2016         ;
2017
2018 // 
2019 // TODO:
2020 //   Figure out what to do with the list 
2021 //
2022 opt_type_argument_list
2023         : /* empty */                { $$ = null; } 
2024         | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2025           {
2026                 $$ = $2;
2027           }
2028         ;
2029
2030 type_arguments
2031         : type {
2032                 TypeArguments type_args = new TypeArguments ();
2033                 type_args.Add ((Expression) $1);
2034                 $$ = type_args;
2035           }
2036         | type_arguments COMMA type {
2037                 TypeArguments type_args = (TypeArguments) $1;
2038                 type_args.Add ((Expression) $3);
2039                 $$ = type_args;
2040           }
2041         ;
2042         
2043 type_name_expression
2044         : IDENTIFIER { $$ = new SimpleName ((string) $1, lexer.Location);}
2045         | type_name_expression DOT IDENTIFIER { 
2046                 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2047           }
2048         ;
2049
2050 /* 
2051  * Before you think of adding a return_type, notice that we have been
2052  * using two rules in the places where it matters (one rule using type
2053  * and another identical one that uses VOID as the return type).  This
2054  * gets rid of a shift/reduce couple
2055  */
2056 type
2057         : type_name {   /* class_type */
2058           }
2059         | builtin_types
2060         | array_type
2061         | pointer_type    
2062         ;
2063
2064
2065 pointer_type
2066         : type STAR
2067           {
2068                 //
2069                 // Note that here only unmanaged types are allowed but we
2070                 // can't perform checks during this phase - we do it during
2071                 // semantic analysis.
2072                 //
2073                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2074           }
2075         | VOID STAR
2076           {
2077                 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2078           }
2079         ;
2080
2081 non_expression_type
2082         : builtin_types 
2083         | non_expression_type rank_specifier
2084           {
2085                 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2086           }
2087         | non_expression_type STAR
2088           {
2089                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2090           }
2091         | expression rank_specifiers 
2092           {
2093                 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2094           }
2095         | expression STAR 
2096           {
2097                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2098           }
2099         
2100         //
2101         // We need this because the parser will happily go and reduce IDENTIFIER STAR
2102         // through this different path
2103         //
2104         | multiplicative_expression STAR 
2105           {
2106                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2107           }
2108         ;
2109
2110 type_list
2111         : type
2112           {
2113                 ArrayList types = new ArrayList (4);
2114
2115                 types.Add ($1);
2116                 $$ = types;
2117           }
2118         | type_list COMMA type
2119           {
2120                 ArrayList types = (ArrayList) $1;
2121
2122                 types.Add ($3);
2123                 $$ = types;
2124           }
2125         ;
2126
2127 /*
2128  * replaces all the productions for isolating the various
2129  * simple types, but we need this to reuse it easily in local_variable_type
2130  */
2131 builtin_types
2132         : OBJECT        { $$ = TypeManager.system_object_expr; }
2133         | STRING        { $$ = TypeManager.system_string_expr; }
2134         | BOOL          { $$ = TypeManager.system_boolean_expr; }
2135         | DECIMAL       { $$ = TypeManager.system_decimal_expr; }
2136         | FLOAT         { $$ = TypeManager.system_single_expr; }
2137         | DOUBLE        { $$ = TypeManager.system_double_expr; }
2138         | integral_type
2139         ;
2140
2141 integral_type
2142         : SBYTE         { $$ = TypeManager.system_sbyte_expr; }
2143         | BYTE          { $$ = TypeManager.system_byte_expr; }
2144         | SHORT         { $$ = TypeManager.system_int16_expr; }
2145         | USHORT        { $$ = TypeManager.system_uint16_expr; }
2146         | INT           { $$ = TypeManager.system_int32_expr; }
2147         | UINT          { $$ = TypeManager.system_uint32_expr; }
2148         | LONG          { $$ = TypeManager.system_int64_expr; }
2149         | ULONG         { $$ = TypeManager.system_uint64_expr; }
2150         | CHAR          { $$ = TypeManager.system_char_expr; }
2151         | VOID          { $$ = TypeManager.system_void_expr; }
2152         ;
2153
2154 interface_type
2155         : type_name
2156         ;
2157
2158 array_type
2159         : type rank_specifiers
2160           {
2161                 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2162           }
2163         ;
2164
2165 //
2166 // Expressions, section 7.5
2167 //
2168 primary_expression
2169         : literal
2170           {
2171                 // 7.5.1: Literals
2172           }
2173  
2174         | type_name
2175           {
2176                 $$ = $1;
2177           }
2178         | parenthesized_expression
2179         | default_value_expression
2180         | member_access
2181         | invocation_expression
2182         | element_access
2183         | this_access
2184         | base_access
2185         | post_increment_expression
2186         | post_decrement_expression
2187         | new_expression
2188         | typeof_expression
2189         | sizeof_expression
2190         | checked_expression
2191         | unchecked_expression
2192         | pointer_member_access
2193         | anonymous_method_expression
2194         ;
2195
2196 literal
2197         : boolean_literal
2198         | integer_literal
2199         | real_literal
2200         | LITERAL_CHARACTER     { $$ = new CharLiteral ((char) lexer.Value); }
2201         | LITERAL_STRING        { $$ = new StringLiteral ((string) lexer.Value); }
2202         | NULL                  { $$ = NullLiteral.Null; }
2203         ;
2204
2205 real_literal
2206         : LITERAL_FLOAT         { $$ = new FloatLiteral ((float) lexer.Value); }
2207         | LITERAL_DOUBLE        { $$ = new DoubleLiteral ((double) lexer.Value); }
2208         | LITERAL_DECIMAL       { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2209         ;
2210
2211 integer_literal
2212         : LITERAL_INTEGER       { 
2213                 object v = lexer.Value;
2214
2215                 if (v is int){
2216                         int i = (int) v;
2217
2218                         if (i == 0)
2219                                 $$ = IntLiteral.Zero;
2220                         else if (i == 1)
2221                                 $$ = IntLiteral.One;
2222                         else
2223                                 $$ = new IntLiteral (i);
2224                 } else if (v is uint)
2225                         $$ = new UIntLiteral ((UInt32) v);
2226                 else if (v is long)
2227                         $$ = new LongLiteral ((Int64) v);
2228                 else if (v is ulong)
2229                         $$ = new ULongLiteral ((UInt64) v);
2230                 else
2231                         Console.WriteLine ("OOPS.  Unexpected result from scanner");
2232           }
2233         ;
2234
2235 boolean_literal
2236         : TRUE                  { $$ = new BoolLiteral (true); }
2237         | FALSE                 { $$ = new BoolLiteral (false); }
2238         ;
2239
2240 parenthesized_expression_0
2241         : OPEN_PARENS expression CLOSE_PARENS
2242           {
2243                 $$ = $2;
2244                 lexer.Deambiguate_CloseParens ();
2245                 // After this, the next token returned is one of
2246                 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2247                 // or CLOSE_PARENS_MINUS.
2248           }
2249         ;
2250
2251 parenthesized_expression
2252         : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2253           {
2254                 $$ = $1;
2255           }
2256         | parenthesized_expression_0 CLOSE_PARENS_MINUS
2257           {
2258                 // If a parenthesized expression is followed by a minus, we need to wrap
2259                 // the expression inside a ParenthesizedExpression for the CS0075 check
2260                 // in Binary.DoResolve().
2261                 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2262           }
2263         ;;
2264
2265 member_access
2266         : primary_expression DOT IDENTIFIER
2267           {
2268                 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2269           }
2270         | predefined_type DOT IDENTIFIER
2271           {
2272                 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2273           }
2274         ;
2275
2276 predefined_type
2277         : builtin_types
2278         ;
2279
2280 invocation_expression
2281         : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2282           {
2283                 if ($1 == null) {
2284                         Location l = lexer.Location;
2285                         Report.Error (1, l, "Parse error");
2286                 }
2287                 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2288           }
2289         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2290           {
2291                 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2292           }
2293         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2294           {
2295                 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2296           }
2297         ;
2298
2299 opt_argument_list
2300         : /* empty */           { $$ = null; }
2301         | argument_list
2302         ;
2303
2304 argument_list
2305         : argument              
2306           { 
2307                 ArrayList list = new ArrayList (4);
2308                 list.Add ($1);
2309                 $$ = list;
2310           }
2311         | argument_list COMMA argument
2312           {
2313                 ArrayList list = (ArrayList) $1;
2314                 list.Add ($3);
2315                 $$ = list;
2316           }
2317         | argument_list error {
2318                 CheckToken (1026, yyToken, ", or ) expected");
2319           }
2320         ;
2321
2322 argument
2323         : expression
2324           {
2325                 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2326           }
2327         | REF variable_reference 
2328           { 
2329                 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2330           }
2331         | OUT variable_reference 
2332           { 
2333                 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2334           }
2335         ;
2336
2337 variable_reference
2338         : expression { note ("section 5.4"); $$ = $1; }
2339         ;
2340
2341 element_access
2342         : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET 
2343           {
2344                 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2345           }
2346         | primary_expression rank_specifiers
2347           {
2348                 // So the super-trick is that primary_expression
2349                 // can only be either a SimpleName or a MemberAccess. 
2350                 // The MemberAccess case arises when you have a fully qualified type-name like :
2351                 // Foo.Bar.Blah i;
2352                 // SimpleName is when you have
2353                 // Blah i;
2354                   
2355                 Expression expr = (Expression) $1;  
2356                 if (expr is ComposedCast){
2357                         $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2358                 } else if (!(expr is SimpleName || expr is MemberAccess || expr is ConstructedType)){
2359                         Error_ExpectingTypeName (lexer.Location, expr);
2360                         $$ = TypeManager.system_object_expr;
2361                 } else {
2362                         //
2363                         // So we extract the string corresponding to the SimpleName
2364                         // or MemberAccess
2365                         // 
2366                         $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2367                 }
2368           }
2369         ;
2370
2371 expression_list
2372         : expression
2373           {
2374                 ArrayList list = new ArrayList (4);
2375                 list.Add ($1);
2376                 $$ = list;
2377           }
2378         | expression_list COMMA expression
2379           {
2380                 ArrayList list = (ArrayList) $1;
2381                 list.Add ($3);
2382                 $$ = list;
2383           }
2384         ;
2385
2386 this_access
2387         : THIS
2388           {
2389                 $$ = new This (current_block, lexer.Location);
2390           }
2391         ;
2392
2393 base_access
2394         : BASE DOT IDENTIFIER
2395           {
2396                 $$ = new BaseAccess ((string) $3, lexer.Location);
2397           }
2398         | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2399           {
2400                 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2401           }
2402         | BASE error {
2403                 Report.Error (175, "Use of keyword `base' is not valid in this context");
2404                 $$ = null;
2405           }
2406         ;
2407
2408 post_increment_expression
2409         : primary_expression OP_INC
2410           {
2411                 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2412                                        (Expression) $1, lexer.Location);
2413           }
2414         ;
2415
2416 post_decrement_expression
2417         : primary_expression OP_DEC
2418           {
2419                 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2420                                        (Expression) $1, lexer.Location);
2421           }
2422         ;
2423
2424 new_expression
2425         : object_or_delegate_creation_expression
2426         | array_creation_expression
2427         ;
2428
2429 object_or_delegate_creation_expression
2430         : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2431           {
2432                 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2433           }
2434         ;
2435
2436 array_creation_expression
2437         : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET 
2438           opt_rank_specifier
2439           opt_array_initializer
2440           {
2441                 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2442           }
2443         | NEW type rank_specifiers array_initializer
2444           {
2445                 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2446           }
2447         | NEW type error 
2448           {
2449                 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2450           }
2451         ;
2452
2453 opt_rank_specifier
2454         : /* empty */
2455           {
2456                   $$ = "";
2457           }
2458         | rank_specifiers
2459           {
2460                         $$ = $1;
2461           }
2462         ;
2463
2464 rank_specifiers
2465         : rank_specifier opt_rank_specifier
2466           {
2467                   $$ = (string) $2 + (string) $1;
2468           }
2469         ;
2470
2471 rank_specifier
2472         : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2473           {
2474                 $$ = "[" + (string) $2 + "]";
2475           }
2476         ;
2477
2478 opt_dim_separators
2479         : /* empty */
2480           {
2481                 $$ = "";
2482           }
2483         | dim_separators
2484           {
2485                   $$ = $1;
2486           }               
2487         ;
2488
2489 dim_separators
2490         : COMMA
2491           {
2492                 $$ = ",";
2493           }
2494         | dim_separators COMMA
2495           {
2496                 $$ = (string) $1 + ",";
2497           }
2498         ;
2499
2500 opt_array_initializer
2501         : /* empty */
2502           {
2503                 $$ = null;
2504           }
2505         | array_initializer
2506           {
2507                 $$ = $1;
2508           }
2509         ;
2510
2511 array_initializer
2512         : OPEN_BRACE CLOSE_BRACE
2513           {
2514                 ArrayList list = new ArrayList (4);
2515                 $$ = list;
2516           }
2517         | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2518           {
2519                 $$ = (ArrayList) $2;
2520           }
2521         ;
2522
2523 variable_initializer_list
2524         : variable_initializer
2525           {
2526                 ArrayList list = new ArrayList (4);
2527                 list.Add ($1);
2528                 $$ = list;
2529           }
2530         | variable_initializer_list COMMA variable_initializer
2531           {
2532                 ArrayList list = (ArrayList) $1;
2533                 list.Add ($3);
2534                 $$ = list;
2535           }
2536         ;
2537
2538 typeof_expression
2539         : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2540           {
2541                 $$ = new TypeOfVoid (lexer.Location);
2542           }
2543         | TYPEOF OPEN_PARENS type CLOSE_PARENS
2544           {
2545                 $$ = new TypeOf ((Expression) $3, lexer.Location);
2546           }
2547         ;
2548
2549 sizeof_expression
2550         : SIZEOF OPEN_PARENS type CLOSE_PARENS { 
2551                 $$ = new SizeOf ((Expression) $3, lexer.Location);
2552           }
2553         ;
2554
2555 checked_expression
2556         : CHECKED OPEN_PARENS expression CLOSE_PARENS
2557           {
2558                 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2559           }
2560         ;
2561
2562 unchecked_expression
2563         : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2564           {
2565                 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2566           }
2567         ;
2568
2569 pointer_member_access 
2570         : primary_expression OP_PTR IDENTIFIER
2571           {
2572                 Expression deref;
2573
2574                 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2575                 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2576           }
2577         ;
2578
2579 anonymous_method_expression
2580         : DELEGATE opt_anonymous_method_signature {
2581                 oob_stack.Push (current_local_parameters);
2582                 current_local_parameters = (Parameters)$2;
2583                 create_toplevel_block = true;
2584           } block {
2585                 create_toplevel_block = false;
2586                 if (!RootContext.V2){
2587                         Report.Error (-213, lexer.Location, "Anonymous methods are only supported in V2");
2588                         $$ = null;
2589                 } else 
2590                         $$ = new AnonymousMethod ((Parameters) $2, (Block) $4, lexer.Location);
2591                 current_local_parameters = (Parameters) oob_stack.Pop ();
2592           }
2593         ;
2594
2595 opt_anonymous_method_signature
2596         : /* empty */                   { $$ = Parameters.EmptyReadOnlyParameters; }
2597         | anonymous_method_signature
2598         ;
2599
2600 anonymous_method_signature
2601         : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS 
2602           {
2603                 if ($2 == null)
2604                         $$ = Parameters.EmptyReadOnlyParameters;
2605                 else {
2606                         ArrayList par_list = (ArrayList) $2;
2607                         Parameter [] pars = new Parameter [par_list.Count];
2608                         par_list.CopyTo (pars);
2609                         $$ = new Parameters (pars, null, lexer.Location);
2610                 }
2611           }
2612         ;
2613
2614 opt_anonymous_method_parameter_list
2615         : /* empty */   { $$ = null; } 
2616         | anonymous_method_parameter_list  { $$ = $1; }
2617         ;
2618
2619 anonymous_method_parameter_list
2620         : anonymous_method_parameter 
2621           {
2622                 ArrayList a = new ArrayList (4);
2623                 a.Add ($1);
2624                 $$ = a;
2625           }
2626         | anonymous_method_parameter_list COMMA anonymous_method_parameter 
2627           {
2628                 ArrayList a = (ArrayList) $1;
2629                 a.Add ($3);
2630                 $$ = a;
2631           }
2632         ; 
2633
2634 anonymous_method_parameter
2635         : opt_parameter_modifier type IDENTIFIER {
2636                 $$ = new Parameter ((Expression) $2, (string) $2, (Parameter.Modifier) $1, null);
2637           }
2638         ;
2639
2640 default_value_expression
2641         : primary_expression DOT DEFAULT
2642           {
2643                 $$ = new DefaultValueExpression ((Expression) $1, lexer.Location);
2644           }
2645         | predefined_type DOT DEFAULT
2646           {
2647                 $$ = new DefaultValueExpression ((Expression) $1, lexer.Location);
2648           }
2649         ;
2650
2651 unary_expression
2652         : primary_expression
2653         | BANG prefixed_unary_expression
2654           {
2655                 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2656           }
2657         | TILDE prefixed_unary_expression
2658           {
2659                 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2660           }
2661         | cast_expression
2662         ;
2663
2664 cast_list
2665         : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
2666           {
2667                 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2668           }
2669         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
2670           {
2671                 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2672           }     
2673         ;
2674
2675 cast_expression
2676         : cast_list
2677         | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2678           {
2679                 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2680           }
2681         ;
2682
2683         //
2684         // The idea to split this out is from Rhys' grammar
2685         // to solve the problem with casts.
2686         //
2687 prefixed_unary_expression
2688         : unary_expression
2689         | PLUS prefixed_unary_expression
2690           { 
2691                 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2692           } 
2693         | MINUS prefixed_unary_expression 
2694           { 
2695                 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2696           }
2697         | OP_INC prefixed_unary_expression 
2698           {
2699                 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2700                                        (Expression) $2, lexer.Location);
2701           }
2702         | OP_DEC prefixed_unary_expression 
2703           {
2704                 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2705                                        (Expression) $2, lexer.Location);
2706           }
2707         | STAR prefixed_unary_expression
2708           {
2709                 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2710           }
2711         | BITWISE_AND prefixed_unary_expression
2712           {
2713                 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2714           }
2715         ;
2716
2717 pre_increment_expression
2718         : OP_INC prefixed_unary_expression 
2719           {
2720                 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2721                                        (Expression) $2, lexer.Location);
2722           }
2723         ;
2724
2725 pre_decrement_expression
2726         : OP_DEC prefixed_unary_expression 
2727           {
2728                 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2729                                        (Expression) $2, lexer.Location);
2730           }
2731         ;
2732
2733 multiplicative_expression
2734         : prefixed_unary_expression
2735         | multiplicative_expression STAR prefixed_unary_expression
2736           {
2737                 $$ = new Binary (Binary.Operator.Multiply, 
2738                                  (Expression) $1, (Expression) $3, lexer.Location);
2739           }
2740         | multiplicative_expression DIV prefixed_unary_expression
2741           {
2742                 $$ = new Binary (Binary.Operator.Division, 
2743                                  (Expression) $1, (Expression) $3, lexer.Location);
2744           }
2745         | multiplicative_expression PERCENT prefixed_unary_expression 
2746           {
2747                 $$ = new Binary (Binary.Operator.Modulus, 
2748                                  (Expression) $1, (Expression) $3, lexer.Location);
2749           }
2750         ;
2751
2752 additive_expression
2753         : multiplicative_expression
2754         | additive_expression PLUS multiplicative_expression 
2755           {
2756                 $$ = new Binary (Binary.Operator.Addition, 
2757                                  (Expression) $1, (Expression) $3, lexer.Location);
2758           }
2759         | additive_expression MINUS multiplicative_expression
2760           {
2761                 $$ = new Binary (Binary.Operator.Subtraction, 
2762                                  (Expression) $1, (Expression) $3, lexer.Location);
2763           }
2764         ;
2765
2766 shift_expression
2767         : additive_expression
2768         | shift_expression OP_SHIFT_LEFT additive_expression
2769           {
2770                 $$ = new Binary (Binary.Operator.LeftShift, 
2771                                  (Expression) $1, (Expression) $3, lexer.Location);
2772           }
2773         | shift_expression OP_SHIFT_RIGHT additive_expression
2774           {
2775                 $$ = new Binary (Binary.Operator.RightShift, 
2776                                  (Expression) $1, (Expression) $3, lexer.Location);
2777           }
2778         ; 
2779
2780 relational_expression
2781         : shift_expression
2782         | relational_expression OP_LT shift_expression
2783           {
2784                 $$ = new Binary (Binary.Operator.LessThan, 
2785                                  (Expression) $1, (Expression) $3, lexer.Location);
2786           }
2787         | relational_expression OP_GT shift_expression
2788           {
2789                 $$ = new Binary (Binary.Operator.GreaterThan, 
2790                                  (Expression) $1, (Expression) $3, lexer.Location);
2791           }
2792         | relational_expression OP_LE shift_expression
2793           {
2794                 $$ = new Binary (Binary.Operator.LessThanOrEqual, 
2795                                  (Expression) $1, (Expression) $3, lexer.Location);
2796           }
2797         | relational_expression OP_GE shift_expression
2798           {
2799                 $$ = new Binary (Binary.Operator.GreaterThanOrEqual, 
2800                                  (Expression) $1, (Expression) $3, lexer.Location);
2801           }
2802         | relational_expression IS type
2803           {
2804                 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
2805           }
2806         | relational_expression AS type
2807           {
2808                 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
2809           }
2810         ;
2811
2812 equality_expression
2813         : relational_expression
2814         | equality_expression OP_EQ relational_expression
2815           {
2816                 $$ = new Binary (Binary.Operator.Equality, 
2817                                  (Expression) $1, (Expression) $3, lexer.Location);
2818           }
2819         | equality_expression OP_NE relational_expression
2820           {
2821                 $$ = new Binary (Binary.Operator.Inequality, 
2822                                  (Expression) $1, (Expression) $3, lexer.Location);
2823           }
2824         ; 
2825
2826 and_expression
2827         : equality_expression
2828         | and_expression BITWISE_AND equality_expression
2829           {
2830                 $$ = new Binary (Binary.Operator.BitwiseAnd, 
2831                                  (Expression) $1, (Expression) $3, lexer.Location);
2832           }
2833         ;
2834
2835 exclusive_or_expression
2836         : and_expression
2837         | exclusive_or_expression CARRET and_expression
2838           {
2839                 $$ = new Binary (Binary.Operator.ExclusiveOr, 
2840                                  (Expression) $1, (Expression) $3, lexer.Location);
2841           }
2842         ;
2843
2844 inclusive_or_expression
2845         : exclusive_or_expression
2846         | inclusive_or_expression BITWISE_OR exclusive_or_expression
2847           {
2848                 $$ = new Binary (Binary.Operator.BitwiseOr, 
2849                                  (Expression) $1, (Expression) $3, lexer.Location);
2850           }
2851         ;
2852
2853 conditional_and_expression
2854         : inclusive_or_expression
2855         | conditional_and_expression OP_AND inclusive_or_expression
2856           {
2857                 $$ = new Binary (Binary.Operator.LogicalAnd, 
2858                                  (Expression) $1, (Expression) $3, lexer.Location);
2859           }
2860         ;
2861
2862 conditional_or_expression
2863         : conditional_and_expression
2864         | conditional_or_expression OP_OR conditional_and_expression
2865           {
2866                 $$ = new Binary (Binary.Operator.LogicalOr, 
2867                                  (Expression) $1, (Expression) $3, lexer.Location);
2868           }
2869         ;
2870
2871 conditional_expression
2872         : conditional_or_expression
2873         | conditional_or_expression INTERR expression COLON expression 
2874           {
2875                 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2876           }
2877         ;
2878
2879 assignment_expression
2880         : prefixed_unary_expression ASSIGN expression
2881           {
2882                 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2883           }
2884         | prefixed_unary_expression OP_MULT_ASSIGN expression
2885           {
2886                 Location l = lexer.Location;
2887
2888                 $$ = new CompoundAssign (
2889                         Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
2890           }
2891         | prefixed_unary_expression OP_DIV_ASSIGN expression
2892           {
2893                 Location l = lexer.Location;
2894
2895                 $$ = new CompoundAssign (
2896                         Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
2897           }
2898         | prefixed_unary_expression OP_MOD_ASSIGN expression
2899           {
2900                 Location l = lexer.Location;
2901
2902                 $$ = new CompoundAssign (
2903                         Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
2904           }
2905         | prefixed_unary_expression OP_ADD_ASSIGN expression
2906           {
2907                 Location l = lexer.Location;
2908
2909                 $$ = new CompoundAssign (
2910                         Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
2911           }
2912         | prefixed_unary_expression OP_SUB_ASSIGN expression
2913           {
2914                 Location l = lexer.Location;
2915
2916                 $$ = new CompoundAssign (
2917                         Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
2918           }
2919         | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2920           {
2921                 Location l = lexer.Location;
2922
2923                 $$ = new CompoundAssign (
2924                         Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
2925           }
2926         | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2927           {
2928                 Location l = lexer.Location;
2929
2930                 $$ = new CompoundAssign (
2931                         Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
2932           }
2933         | prefixed_unary_expression OP_AND_ASSIGN expression
2934           {
2935                 Location l = lexer.Location;
2936
2937                 $$ = new CompoundAssign (
2938                         Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
2939           }
2940         | prefixed_unary_expression OP_OR_ASSIGN expression
2941           {
2942                 Location l = lexer.Location;
2943
2944                 $$ = new CompoundAssign (
2945                         Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
2946           }
2947         | prefixed_unary_expression OP_XOR_ASSIGN expression
2948           {
2949                 Location l = lexer.Location;
2950
2951                 $$ = new CompoundAssign (
2952                         Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
2953           }
2954         ;
2955
2956 expression
2957         : conditional_expression
2958         | assignment_expression
2959         ;
2960
2961 constant_expression
2962         : expression
2963         ;
2964
2965 boolean_expression
2966         : expression
2967         ;
2968
2969 //
2970 // 10 classes
2971 //
2972 class_declaration
2973         : opt_attributes
2974           opt_modifiers
2975           CLASS IDENTIFIER
2976           {
2977                 Class new_class;
2978                 string name;
2979
2980                 name = MakeName ((string) $4);
2981
2982                 new_class = new Class (current_namespace, current_container, name, (int) $2, 
2983                                        (Attributes) $1, lexer.Location);
2984                 current_container = new_class;
2985                 RootContext.Tree.RecordDecl (name, new_class);
2986
2987                 lexer.ConstraintsParsing = true;
2988           }
2989           opt_type_parameter_list
2990           opt_class_base
2991           opt_type_parameter_constraints_clauses
2992           {
2993                 lexer.ConstraintsParsing = false;
2994           }
2995           class_body 
2996           opt_semicolon 
2997           {
2998                 Class new_class = (Class) current_container;
2999
3000                 if ($8 != null && $6 == null)
3001                         Report.Error (-200, new_class.Location,
3002                                       "Type parameter constraints only valid if there is a type parameter list");
3003
3004                 if ($6 != null)
3005                         CheckDef (new_class.SetParameterInfo ((ArrayList) $6, (ArrayList) $8), new_class.Name, new_class.Location);
3006                 if ($7 != null) {
3007                         if (new_class.Name == "System.Object") {
3008                                 Report.Error (537, new_class.Location, "The class System.Object cannot have a base class or implement an interface.");
3009                         }
3010
3011                         new_class.Bases = (ArrayList) $7;
3012                 }
3013                 current_container = current_container.Parent;
3014                 CheckDef (current_container.AddClass (new_class), new_class.Name, new_class.Location);
3015
3016                 $$ = new_class;
3017           }
3018         | opt_attributes
3019           opt_modifiers
3020           CLASS IDENTIFIER
3021           {
3022                 lexer.ConstraintsParsing = true;
3023           }
3024           WHERE {
3025                 Report.Error (-200, lexer.Location,
3026                         "Type parameter constraints only valid if there is a type parameter list");
3027                 yyErrorFlag = 0;
3028                 $$ = null;
3029           }
3030         ;       
3031
3032 opt_modifiers
3033         : /* empty */           { $$ = (int) 0; }
3034         | modifiers
3035         ;
3036
3037 modifiers
3038         : modifier
3039         | modifiers modifier
3040           { 
3041                 int m1 = (int) $1;
3042                 int m2 = (int) $2;
3043
3044                 if ((m1 & m2) != 0) {
3045                         Location l = lexer.Location;
3046                         Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3047                 }
3048                 $$ = (int) (m1 | m2);
3049           }
3050         ;
3051
3052 modifier
3053         : NEW                   { $$ = Modifiers.NEW; }
3054         | PUBLIC                { $$ = Modifiers.PUBLIC; }
3055         | PROTECTED             { $$ = Modifiers.PROTECTED; }
3056         | INTERNAL              { $$ = Modifiers.INTERNAL; }
3057         | PRIVATE               { $$ = Modifiers.PRIVATE; }
3058         | ABSTRACT              { $$ = Modifiers.ABSTRACT; }
3059         | SEALED                { $$ = Modifiers.SEALED; }
3060         | STATIC                { $$ = Modifiers.STATIC; }
3061         | READONLY              { $$ = Modifiers.READONLY; }
3062         | VIRTUAL               { $$ = Modifiers.VIRTUAL; }
3063         | OVERRIDE              { $$ = Modifiers.OVERRIDE; }
3064         | EXTERN                { $$ = Modifiers.EXTERN; }
3065         | VOLATILE              { $$ = Modifiers.VOLATILE; }
3066         | UNSAFE                { $$ = Modifiers.UNSAFE; }
3067         ;
3068
3069 opt_class_base
3070         : /* empty */           { $$ = null; }
3071         | class_base            { $$ = $1;   }
3072         ;
3073
3074 class_base
3075         : COLON type_list { $$ = $2; }
3076         ;
3077
3078 opt_type_parameter_list
3079         : /* empty */           { $$ = null; }
3080         | type_parameter_list   { $$ = $1; }
3081         ;
3082
3083 type_parameter_list
3084         : OP_GENERICS_LT type_parameters OP_GENERICS_GT { $$ = $2; }
3085         ;
3086
3087 type_parameters
3088         : type_parameter { 
3089                 // 
3090                 // Do some profiling to find the optimal size, for now we
3091                 // assume most people will be generic on one type (saves space
3092                 //
3093                 ArrayList type_parameters = new ArrayList (1);
3094                 type_parameters.Add ($1);
3095                 $$ = type_parameters;
3096           }
3097         | type_parameters COMMA type_parameter {
3098                 ArrayList type_parameters = (ArrayList) $1;
3099
3100                 type_parameters.Add ($3);
3101                 $$ = type_parameters;
3102           }
3103         ;
3104
3105 type_parameter
3106         : IDENTIFIER
3107         ;
3108
3109 opt_type_parameter_constraints_clauses
3110         : /* empty */           { $$ = null; }
3111         | type_parameter_constraints_clauses 
3112           { $$ = $1; }
3113         ;
3114
3115 type_parameter_constraints_clauses
3116         : type_parameter_constraints_clause
3117         | type_parameter_constraints_clauses type_parameter_constraints_clause {
3118                 ArrayList constraints = (ArrayList) $1;
3119
3120                 constraints.Add ($2);
3121                 $$ = constraints;
3122           }
3123         ; 
3124
3125 type_parameter_constraints_clause
3126         : WHERE type_parameter COLON type_parameter_constraints {
3127                 ArrayList constraints = new ArrayList (1);
3128                 constraints.Add (new Constraints ((string) $2, (ArrayList) $4, lexer.Location));
3129                 $$ = constraints;
3130           }
3131         ; 
3132
3133 type_parameter_constraints
3134         : type_parameter_constraint {
3135                 ArrayList constraints = new ArrayList (1);
3136                 constraints.Add ($1);
3137                 $$ = constraints;
3138           }
3139         | type_parameter_constraints COMMA type_parameter_constraint {
3140                 ArrayList constraints = (ArrayList) $1;
3141
3142                 constraints.Add ($3);
3143                 $$ = constraints;
3144           }
3145         ;
3146
3147 type_parameter_constraint
3148         : type
3149         | NEW OPEN_PARENS CLOSE_PARENS {
3150                 $$ = true;
3151           }
3152         ;
3153
3154 //
3155 // Statements (8.2)
3156 //
3157
3158 //
3159 // A block is "contained" on the following places:
3160 //      method_body
3161 //      property_declaration as part of the accessor body (get/set)
3162 //      operator_declaration
3163 //      constructor_declaration
3164 //      destructor_declaration
3165 //      event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3166 //      
3167 block
3168         : OPEN_BRACE 
3169           {
3170                 if (current_block == null || create_toplevel_block){
3171                         current_block = new ToplevelBlock (current_local_parameters, lexer.Location);
3172                 } else {
3173                 current_block = new Block (current_block, current_local_parameters,
3174                                            lexer.Location, Location.Null);
3175                 }
3176           } 
3177           opt_statement_list CLOSE_BRACE 
3178           { 
3179                 while (current_block.Implicit)
3180                         current_block = current_block.Parent;
3181                 $$ = current_block;
3182                 current_block.SetEndLocation (lexer.Location);
3183                 current_block = current_block.Parent;
3184           }
3185         ;
3186
3187 opt_statement_list
3188         : /* empty */
3189         | statement_list 
3190         ;
3191
3192 statement_list
3193         : statement
3194         | statement_list statement
3195         ;
3196
3197 statement
3198         : declaration_statement
3199           {
3200                 if ($1 != null && (Block) $1 != current_block){
3201                         current_block.AddStatement ((Statement) $1);
3202                         current_block = (Block) $1;
3203                 }
3204           }
3205         | embedded_statement
3206           {
3207                 current_block.AddStatement ((Statement) $1);
3208           }
3209         | labeled_statement
3210         ;
3211
3212 embedded_statement
3213         : block
3214         | empty_statement
3215         | expression_statement
3216         | selection_statement
3217         | iteration_statement
3218         | jump_statement                  
3219         | try_statement
3220         | checked_statement
3221         | unchecked_statement
3222         | lock_statement
3223         | using_statement
3224         | unsafe_statement
3225         | fixed_statement
3226         ;
3227
3228 empty_statement
3229         : SEMICOLON
3230           {
3231                   $$ = EmptyStatement.Value;
3232           }
3233         ;
3234
3235 labeled_statement
3236         : IDENTIFIER COLON 
3237           {
3238                 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3239
3240                 if (!current_block.AddLabel ((string) $1, labeled)){
3241                         Location l = lexer.Location;
3242                         Report.Error (140, l, "The label '" + ((string) $1) + "' is a duplicate");
3243                 }       
3244                 current_block.AddStatement (labeled);
3245           }
3246           statement
3247         ;
3248
3249 declaration_statement
3250         : local_variable_declaration SEMICOLON
3251           {
3252                 if ($1 != null){
3253                         DictionaryEntry de = (DictionaryEntry) $1;
3254
3255                         $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3256                 }
3257           }
3258
3259         | local_constant_declaration SEMICOLON
3260           {
3261                 if ($1 != null){
3262                         DictionaryEntry de = (DictionaryEntry) $1;
3263
3264                         $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3265                 }
3266           }
3267         ;
3268
3269 /* 
3270  * The following is from Rhys' grammar:
3271  * > Types in local variable declarations must be recognized as 
3272  * > expressions to prevent reduce/reduce errors in the grammar.
3273  * > The expressions are converted into types during semantic analysis.
3274  */
3275 local_variable_type
3276         : primary_expression opt_rank_specifier
3277           { 
3278                 // FIXME: Do something smart here regarding the composition of the type.
3279
3280                 // Ok, the above "primary_expression" is there to get rid of
3281                 // both reduce/reduce and shift/reduces in the grammar, it should
3282                 // really just be "type_name".  If you use type_name, a reduce/reduce
3283                 // creeps up.  If you use qualified_identifier (which is all we need
3284                 // really) two shift/reduces appear.
3285                 // 
3286
3287                 // So the super-trick is that primary_expression
3288                 // can only be either a SimpleName or a MemberAccess. 
3289                 // The MemberAccess case arises when you have a fully qualified type-name like :
3290                 // Foo.Bar.Blah i;
3291                 // SimpleName is when you have
3292                 // Blah i;
3293                   
3294                 Expression expr = (Expression) $1;  
3295                 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType)) {
3296                         Error_ExpectingTypeName (lexer.Location, expr);
3297                         $$ = null;
3298                 } else {
3299                         //
3300                         // So we extract the string corresponding to the SimpleName
3301                         // or MemberAccess
3302                         // 
3303
3304                         if ((string) $2 == "")
3305                                 $$ = $1;
3306                         else
3307                                 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3308                 }
3309           }
3310         | builtin_types opt_rank_specifier
3311           {
3312                 if ((string) $2 == "")
3313                         $$ = $1;
3314                 else
3315                         $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3316           }
3317         ;
3318
3319 local_variable_pointer_type
3320         : primary_expression STAR
3321           {
3322                 Expression expr = (Expression) $1;  
3323                 Location l = lexer.Location;
3324
3325                 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType)) {
3326                         Error_ExpectingTypeName (l, expr);
3327
3328                         $$ = null;
3329                 } else 
3330                         $$ = new ComposedCast ((Expression) $1, "*", l);
3331           }
3332         | builtin_types STAR
3333           {
3334                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3335           }
3336         | VOID STAR
3337           {
3338                 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3339           }
3340         | local_variable_pointer_type STAR
3341           {
3342                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3343           }
3344         ;
3345
3346 local_variable_declaration
3347         : local_variable_type variable_declarators
3348           {
3349                 if ($1 != null)
3350                         $$ = new DictionaryEntry ($1, $2);
3351                 else
3352                         $$ = null;
3353           }
3354         | local_variable_pointer_type opt_rank_specifier variable_declarators
3355         {
3356                 if ($1 != null){
3357                         Expression t;
3358
3359                         if ((string) $2 == "")
3360                                 t = (Expression) $1;
3361                         else
3362                                 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3363                         $$ = new DictionaryEntry (t, $3);
3364                 } else 
3365                         $$ = null;
3366         }
3367         ;
3368
3369 local_constant_declaration
3370         : CONST local_variable_type constant_declarators
3371           {
3372                 if ($2 != null)
3373                         $$ = new DictionaryEntry ($2, $3);
3374                 else
3375                         $$ = null;
3376           }
3377         ;
3378
3379 expression_statement
3380         : statement_expression SEMICOLON
3381           {
3382                 $$ = $1;
3383           }
3384         ;
3385
3386         //
3387         // We have to do the wrapping here and not in the case above,
3388         // because statement_expression is used for example in for_statement
3389         //
3390 statement_expression
3391         : invocation_expression         { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3392         | object_creation_expression    { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3393         | assignment_expression         { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3394         | post_increment_expression     { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3395         | post_decrement_expression     { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3396         | pre_increment_expression      { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3397         | pre_decrement_expression      { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3398         | error {
3399                 Report.Error (1002, lexer.Location, "Expecting `;'");
3400                 $$ = null;
3401           }
3402         ;
3403
3404 object_creation_expression
3405         : object_or_delegate_creation_expression
3406           { note ("complain if this is a delegate maybe?"); } 
3407         ;
3408
3409 selection_statement
3410         : if_statement
3411         | switch_statement
3412         ; 
3413
3414 if_statement
3415         : if_statement_open if_statement_rest
3416           {
3417                 $$ = $2;
3418           }
3419         ;
3420
3421 if_statement_open
3422         : IF OPEN_PARENS 
3423           {
3424                 oob_stack.Push (lexer.Location);
3425           }
3426         ;
3427
3428 if_statement_rest
3429         : boolean_expression CLOSE_PARENS 
3430           embedded_statement
3431           { 
3432                 Location l = (Location) oob_stack.Pop ();
3433
3434                 $$ = new If ((Expression) $1, (Statement) $3, l);
3435
3436                 if (RootContext.WarningLevel >= 3){
3437                         if ($3 == EmptyStatement.Value)
3438                                 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3439                 }
3440
3441           }
3442         | boolean_expression CLOSE_PARENS
3443           embedded_statement ELSE embedded_statement
3444           {
3445                 Location l = (Location) oob_stack.Pop ();
3446
3447                 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
3448           }
3449         ;
3450
3451 switch_statement
3452         : SWITCH OPEN_PARENS 
3453           { 
3454                 oob_stack.Push (lexer.Location);
3455                 switch_stack.Push (current_block);
3456           }
3457           expression CLOSE_PARENS 
3458           switch_block
3459           {
3460                 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
3461                 current_block = (Block) switch_stack.Pop ();
3462           }
3463         ;
3464
3465 switch_block
3466         : OPEN_BRACE
3467           opt_switch_sections
3468           CLOSE_BRACE
3469           {
3470                 $$ = $2;
3471           }
3472         ;
3473
3474 opt_switch_sections
3475         : /* empty */           
3476           {
3477                 Report.Error (1522, lexer.Location, "Empty switch block"); 
3478           }
3479         | switch_sections
3480         ;
3481
3482 switch_sections
3483         : switch_section 
3484           {
3485                 ArrayList sections = new ArrayList (4);
3486
3487                 sections.Add ($1);
3488                 $$ = sections;
3489           }
3490         | switch_sections switch_section
3491           {
3492                 ArrayList sections = (ArrayList) $1;
3493
3494                 sections.Add ($2);
3495                 $$ = sections;
3496           }
3497         ;
3498
3499 switch_section
3500         : switch_labels
3501           {
3502                 current_block = current_block.CreateSwitchBlock (lexer.Location);
3503           }
3504           statement_list 
3505           {
3506                 Block topmost = current_block;
3507
3508                 while (topmost.Implicit)
3509                         topmost = topmost.Parent;
3510                 $$ = new SwitchSection ((ArrayList) $1, topmost);
3511           }
3512         ;
3513
3514 switch_labels
3515         : switch_label 
3516           {
3517                 ArrayList labels = new ArrayList (4);
3518
3519                 labels.Add ($1);
3520                 $$ = labels;
3521           }
3522         | switch_labels switch_label 
3523           {
3524                 ArrayList labels = (ArrayList) ($1);
3525                 labels.Add ($2);
3526
3527                 $$ = labels;
3528           }
3529         ;
3530
3531 switch_label
3532         : CASE constant_expression COLON        { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3533         | DEFAULT COLON                         { $$ = new SwitchLabel (null, lexer.Location); }
3534         | error {
3535                 Report.Error (
3536                         1523, lexer.Location, 
3537                         "The keyword case or default must precede code in switch block");
3538           }
3539         ;
3540
3541 iteration_statement
3542         : while_statement
3543         | do_statement
3544         | for_statement
3545         | foreach_statement
3546         ;
3547
3548 while_statement
3549         : WHILE OPEN_PARENS 
3550         {
3551                 oob_stack.Push (lexer.Location);
3552         }
3553         boolean_expression CLOSE_PARENS embedded_statement
3554         {
3555                 Location l = (Location) oob_stack.Pop ();
3556                 $$ = new While ((Expression) $4, (Statement) $6, l);
3557         
3558                 if (RootContext.WarningLevel >= 3){
3559                         if ($6 == EmptyStatement.Value)
3560                                 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3561                 }
3562         }
3563         ;
3564
3565 do_statement
3566         : DO embedded_statement 
3567           WHILE OPEN_PARENS {
3568                 oob_stack.Push (lexer.Location);
3569           }
3570           boolean_expression CLOSE_PARENS SEMICOLON
3571           {
3572                 Location l = (Location) oob_stack.Pop ();
3573
3574                 $$ = new Do ((Statement) $2, (Expression) $6, l);
3575           }
3576         ;
3577
3578 for_statement
3579         : FOR OPEN_PARENS 
3580           opt_for_initializer SEMICOLON
3581           {
3582                 Block assign_block = new Block (current_block);
3583                 current_block = assign_block;
3584
3585                 if ($3 is DictionaryEntry){
3586                         DictionaryEntry de = (DictionaryEntry) $3;
3587                         
3588                         Expression type = (Expression) de.Key;
3589                         ArrayList var_declarators = (ArrayList) de.Value;
3590
3591                         foreach (VariableDeclaration decl in var_declarators){
3592
3593                                 LocalInfo vi;
3594
3595                                 vi = current_block.AddVariable (
3596                                         type, decl.identifier, current_local_parameters, decl.Location);
3597                                 if (vi == null)
3598                                         continue;
3599
3600                                 Location l = lexer.Location;
3601                                 Expression expr;
3602                                 if (decl.expression_or_array_initializer is Expression){
3603                                         expr = (Expression) decl.expression_or_array_initializer;
3604                                 } else if (decl.expression_or_array_initializer == null) {
3605                                         expr = null;
3606                                 } else {
3607                                         ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3608                                         expr = new ArrayCreation (type, "", init, decl.Location);
3609                                 }
3610                                         
3611                                 LocalVariableReference var;
3612                                 var = new LocalVariableReference (assign_block, decl.identifier, l);
3613
3614                                 if (expr != null) {
3615                                         Assign a = new Assign (var, expr, decl.Location);
3616                                         
3617                                         assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3618                                 }
3619                         }
3620                         
3621                         $3 = null;
3622                 } 
3623                 oob_stack.Push (lexer.Location);
3624           } 
3625           opt_for_condition SEMICOLON
3626           opt_for_iterator CLOSE_PARENS 
3627           embedded_statement
3628           {
3629                 Location l = (Location) oob_stack.Pop ();
3630
3631                 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3632
3633                 if (RootContext.WarningLevel >= 3){
3634                         if ($10 == EmptyStatement.Value)
3635                                 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3636                 }
3637
3638                 current_block.AddStatement (f);
3639                 while (current_block.Implicit)
3640                         current_block = current_block.Parent;
3641                 $$ = current_block;
3642                 current_block = current_block.Parent;
3643           }
3644         ;
3645
3646 opt_for_initializer
3647         : /* empty */           { $$ = EmptyStatement.Value; }
3648         | for_initializer       
3649         ;
3650
3651 for_initializer
3652         : local_variable_declaration
3653         | statement_expression_list
3654         ;
3655
3656 opt_for_condition
3657         : /* empty */           { $$ = null; }
3658         | boolean_expression
3659         ;
3660
3661 opt_for_iterator
3662         : /* empty */           { $$ = EmptyStatement.Value; }
3663         | for_iterator
3664         ;
3665
3666 for_iterator
3667         : statement_expression_list
3668         ;
3669
3670 statement_expression_list
3671         : statement_expression  
3672           {
3673                 // CHANGE: was `null'
3674                 Block b = new Block (current_block, Block.Flags.Implicit);   
3675
3676                 b.AddStatement ((Statement) $1);
3677                 $$ = b;
3678           }
3679         | statement_expression_list COMMA statement_expression
3680           {
3681                 Block b = (Block) $1;
3682
3683                 b.AddStatement ((Statement) $3);
3684                 $$ = $1;
3685           }
3686         ;
3687
3688 foreach_statement
3689         : FOREACH OPEN_PARENS type IDENTIFIER IN 
3690           {
3691                 oob_stack.Push (lexer.Location);
3692           }
3693           expression CLOSE_PARENS 
3694           {
3695                 oob_stack.Push (current_block);
3696
3697                 Block foreach_block = new Block (current_block, Block.Flags.Implicit);
3698                 LocalVariableReference v = null;
3699                 Location l = lexer.Location;
3700                 LocalInfo vi;
3701
3702                 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3703                 if (vi != null) {
3704                         vi.ReadOnly = true;
3705
3706                         // Get a writable reference to this read-only variable.
3707                         v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3708                 }
3709                 current_block = foreach_block;
3710
3711                 oob_stack.Push (v);
3712                 oob_stack.Push (current_block);
3713           } 
3714           embedded_statement 
3715           {
3716                 Block foreach_block = (Block) oob_stack.Pop ();
3717                 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3718                 Block prev_block = (Block) oob_stack.Pop ();
3719                 Location l = (Location) oob_stack.Pop ();
3720
3721                 current_block = prev_block;
3722
3723                 if (v != null) {
3724                         Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3725                         foreach_block.AddStatement (f);
3726                 }
3727
3728                 $$ = foreach_block;
3729           }
3730         ;
3731
3732 jump_statement
3733         : break_statement
3734         | continue_statement
3735         | goto_statement
3736         | return_statement
3737         | throw_statement
3738         | yield_statement
3739         ;
3740
3741 break_statement
3742         : BREAK SEMICOLON
3743           {
3744                 $$ = new Break (lexer.Location);
3745           }
3746         ;
3747
3748 continue_statement
3749         : CONTINUE SEMICOLON
3750           {
3751                 $$ = new Continue (lexer.Location);
3752           }
3753         ;
3754
3755 goto_statement
3756         : GOTO IDENTIFIER SEMICOLON 
3757           {
3758                 $$ = new Goto (current_block, (string) $2, lexer.Location);
3759           }
3760         | GOTO CASE constant_expression SEMICOLON
3761           {
3762                 $$ = new GotoCase ((Expression) $3, lexer.Location);
3763           }
3764         | GOTO DEFAULT SEMICOLON 
3765           {
3766                 $$ = new GotoDefault (lexer.Location);
3767           }
3768         ; 
3769
3770 return_statement
3771         : RETURN opt_expression SEMICOLON
3772           {
3773                 $$ = new Return ((Expression) $2, lexer.Location);
3774           }
3775         ;
3776
3777 throw_statement
3778         : THROW opt_expression SEMICOLON
3779           {
3780                 $$ = new Throw ((Expression) $2, lexer.Location);
3781           }
3782         ;
3783
3784 yield_statement 
3785         : YIELD RETURN expression SEMICOLON
3786           {
3787                 if (iterator_container == null){
3788                         Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3789                         $$ = null;
3790                 } else {
3791                         iterator_container.SetYields ();
3792                         $$ = new Yield ((Expression) $3, lexer.Location); 
3793                 }
3794           }
3795         | YIELD expression SEMICOLON 
3796           {
3797                 if (iterator_container == null){
3798                         Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3799                         $$ = null;
3800                 } else {
3801                         iterator_container.SetYields ();
3802                         $$ = new Yield ((Expression) $2, lexer.Location);
3803                 }
3804           }
3805         | YIELD BREAK SEMICOLON
3806           {
3807                 if (iterator_container == null){
3808                         Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3809                         $$ = null;
3810                 } else {
3811                         iterator_container.SetYields ();
3812                         $$ = new YieldBreak (lexer.Location);
3813                 }
3814           }
3815         ;
3816
3817 opt_expression
3818         : /* empty */
3819         | expression
3820         ;
3821
3822 try_statement
3823         : TRY block catch_clauses 
3824         {
3825                 Catch g = null;
3826                 ArrayList s = new ArrayList (4);
3827                 
3828                 foreach (Catch cc in (ArrayList) $3) {
3829                         if (cc.IsGeneral)
3830                                 g = cc;
3831                         else
3832                                 s.Add (cc);
3833                 }
3834
3835                 // Now s contains the list of specific catch clauses
3836                 // and g contains the general one.
3837                 
3838                 $$ = new Try ((Block) $2, s, g, null, lexer.Location);
3839         }
3840         | TRY block opt_catch_clauses FINALLY block
3841           {
3842                 Catch g = null;
3843                 ArrayList s = new ArrayList (4);
3844                 ArrayList catch_list = (ArrayList) $3;
3845
3846                 if (catch_list != null){
3847                         foreach (Catch cc in catch_list) {
3848                                 if (cc.IsGeneral)
3849                                         g = cc;
3850                                 else
3851                                         s.Add (cc);
3852                         }
3853                 }
3854
3855                 $$ = new Try ((Block) $2, s, g, (Block) $5, lexer.Location);
3856           }
3857         | TRY block error 
3858           {
3859                 Report.Error (1524, lexer.Location, "Expected catch or finally");
3860           }
3861         ;
3862
3863 opt_catch_clauses
3864         : /* empty */  { $$ = null; }
3865         | catch_clauses
3866         ;
3867
3868 catch_clauses
3869         : catch_clause 
3870           {
3871                 ArrayList l = new ArrayList (4);
3872
3873                 l.Add ($1);
3874                 $$ = l;
3875           }
3876         | catch_clauses catch_clause
3877           {
3878                 ArrayList l = (ArrayList) $1;
3879
3880                 l.Add ($2);
3881                 $$ = l;
3882           }
3883         ;
3884
3885 opt_identifier
3886         : /* empty */   { $$ = null; }
3887         | IDENTIFIER
3888         ;
3889
3890 catch_clause 
3891         : CATCH opt_catch_args 
3892         {
3893                 Expression type = null;
3894                 string id = null;
3895                 
3896                 if ($2 != null) {
3897                         DictionaryEntry cc = (DictionaryEntry) $2;
3898                         type = (Expression) cc.Key;
3899                         id   = (string) cc.Value;
3900
3901                         if (id != null){
3902                                 ArrayList one = new ArrayList (4);
3903                                 Location loc = lexer.Location;
3904
3905                                 one.Add (new VariableDeclaration (id, null, loc));
3906
3907                                 $1 = current_block;
3908                                 current_block = new Block (current_block);
3909                                 Block b = declare_local_variables (type, one, loc);
3910                                 current_block = b;
3911                         }
3912                 }
3913         } block {
3914                 Expression type = null;
3915                 string id = null;
3916
3917                 if ($2 != null){
3918                         DictionaryEntry cc = (DictionaryEntry) $2;
3919                         type = (Expression) cc.Key;
3920                         id   = (string) cc.Value;
3921
3922                         if ($1 != null){
3923                                 //
3924                                 // FIXME: I can change this for an assignment.
3925                                 //
3926                                 while (current_block != (Block) $1)
3927                                         current_block = current_block.Parent;
3928                         }
3929                 }
3930
3931
3932                 $$ = new Catch (type, id , (Block) $4, lexer.Location);
3933         }
3934         ;
3935
3936 opt_catch_args
3937         : /* empty */ { $$ = null; }
3938         | catch_args
3939         ;         
3940
3941 catch_args 
3942         : OPEN_PARENS type opt_identifier CLOSE_PARENS 
3943         {
3944                 $$ = new DictionaryEntry ($2, $3);
3945         }
3946         ;
3947
3948 checked_statement
3949         : CHECKED block
3950           {
3951                 $$ = new Checked ((Block) $2);
3952           }
3953         ;
3954
3955 unchecked_statement
3956         : UNCHECKED block
3957           {
3958                 $$ = new Unchecked ((Block) $2);
3959           }
3960         ;
3961
3962 unsafe_statement
3963         : UNSAFE 
3964         {
3965                 if (!RootContext.Unsafe){
3966                         Report.Error (227, lexer.Location, 
3967                                 "Unsafe code can only be used if --unsafe is used");
3968                 }
3969         } block {
3970                 $$ = new Unsafe ((Block) $3);
3971         }
3972         ;
3973
3974 fixed_statement
3975         : FIXED OPEN_PARENS 
3976           type fixed_pointer_declarators 
3977           CLOSE_PARENS 
3978           {
3979                 Block assign_block = new Block (current_block, Block.Flags.Implicit);
3980                 ArrayList list = (ArrayList) $4;
3981                 Expression type = (Expression) $3;
3982                 Location l = lexer.Location;
3983                 int top = list.Count;
3984
3985                 for (int i = 0; i < top; i++){
3986                         Pair p = (Pair) list [i];
3987                         LocalInfo v;
3988
3989                         v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3990                         if (v == null)
3991                                 continue;
3992                         v.ReadOnly = true;
3993                         p.First = v;
3994                         list [i] = p;
3995                 }
3996                 current_block.AddStatement (assign_block);
3997                 current_block = assign_block;
3998                 oob_stack.Push (assign_block);
3999                 oob_stack.Push (l);
4000           }
4001           embedded_statement 
4002           {
4003                 Location l = (Location) oob_stack.Pop ();
4004                 oob_stack.Pop ();
4005
4006                 $$ = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4007           }
4008         ;
4009
4010 fixed_pointer_declarators
4011         : fixed_pointer_declarator      { 
4012                 ArrayList declarators = new ArrayList (4); 
4013                 declarators.Add ($1);
4014                 $$ = declarators;
4015           }
4016         | fixed_pointer_declarators COMMA fixed_pointer_declarator
4017           {
4018                 ArrayList declarators = (ArrayList) $1;
4019                 declarators.Add ($3);
4020                 $$ = declarators;
4021           }
4022         ;
4023
4024 fixed_pointer_declarator
4025         : IDENTIFIER ASSIGN expression
4026           {     
4027                 $$ = new Pair ($1, $3);
4028           }
4029         ;
4030
4031 lock_statement
4032         : LOCK OPEN_PARENS expression CLOSE_PARENS 
4033           {
4034                 //
4035           } 
4036           embedded_statement
4037           {
4038                 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4039           }
4040         ;
4041
4042 using_statement
4043         : USING OPEN_PARENS resource_acquisition CLOSE_PARENS 
4044           {
4045                 Block assign_block = new Block (current_block);
4046                 current_block = assign_block;
4047
4048                 oob_stack.Push (lexer.Location);
4049                 
4050                 if ($3 is DictionaryEntry){
4051                         DictionaryEntry de = (DictionaryEntry) $3;
4052                         Location l = lexer.Location;
4053
4054                         Expression type = (Expression) de.Key;
4055                         ArrayList var_declarators = (ArrayList) de.Value;
4056
4057                         ArrayList vars = new ArrayList (4);
4058
4059                         foreach (VariableDeclaration decl in var_declarators){
4060
4061                                 LocalInfo vi    = current_block.AddVariable (
4062                                         type, decl.identifier, 
4063                                         current_local_parameters, decl.Location);
4064                                 if (vi == null)
4065                                         continue;
4066                                 vi.ReadOnly = true;
4067
4068                                 Expression expr;
4069                                 if (decl.expression_or_array_initializer is Expression){
4070                                         expr = (Expression) decl.expression_or_array_initializer;
4071                                 } else {
4072                                         ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4073                                         
4074                                         expr = new ArrayCreation (type, "", init, decl.Location);
4075                                 }
4076
4077                                 LocalVariableReference var;
4078
4079                                 // Get a writable reference to this read-only variable.
4080                                 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4081
4082                                 // This is so that it is not a warning on using variables
4083                                 vi.Used = true;
4084
4085                                 vars.Add (new DictionaryEntry (var, expr));                             
4086
4087                                 // Assign a = new Assign (var, expr, decl.Location);
4088                                 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4089                         }
4090                         $3 = new DictionaryEntry (type, vars);
4091                  }
4092           } 
4093           embedded_statement
4094           {
4095                 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
4096                 current_block.AddStatement (u);
4097                 while (current_block.Implicit)
4098                         current_block = current_block.Parent;
4099                 $$ = current_block;
4100                 current_block = current_block.Parent;
4101           }
4102         ; 
4103
4104 resource_acquisition
4105         : local_variable_declaration
4106         | expression
4107         ;
4108
4109 %%
4110
4111 // <summary>
4112 //   A class used to pass around variable declarations and constants
4113 // </summary>
4114 public class VariableDeclaration {
4115         public string identifier;
4116         public object expression_or_array_initializer;
4117         public Location Location;
4118         public Attributes OptAttributes;
4119
4120         public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4121         {
4122                 this.identifier = id;
4123                 this.expression_or_array_initializer = eoai;
4124                 this.Location = l;
4125                 this.OptAttributes = opt_attrs;
4126         }
4127
4128         public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4129         {
4130         }
4131 }
4132
4133 // <summary>
4134 //   A class used to hold info about an indexer declarator
4135 // </summary>
4136 public class IndexerDeclaration {
4137         public Expression type;
4138         public string interface_type;
4139         public Parameters param_list;
4140
4141         public IndexerDeclaration (Expression type, string interface_type, Parameters param_list)
4142         {
4143                 this.type = type;
4144                 this.interface_type = interface_type;
4145                 this.param_list = param_list;
4146         }
4147 }
4148
4149 // <summary>
4150 //  A class used to hold info about an operator declarator
4151 // </summary>
4152 public class OperatorDeclaration {
4153         public Operator.OpType optype;
4154         public Expression ret_type, arg1type, arg2type;
4155         public string arg1name, arg2name;
4156         public Location location;
4157
4158         public OperatorDeclaration (Operator.OpType op, Expression ret_type, 
4159                                     Expression arg1type, string arg1name,
4160                                     Expression arg2type, string arg2name, Location location)
4161         {
4162                 optype = op;
4163                 this.ret_type = ret_type;
4164                 this.arg1type = arg1type;
4165                 this.arg1name = arg1name;
4166                 this.arg2type = arg2type;
4167                 this.arg2name = arg2name;
4168                 this.location = location;
4169         }
4170
4171 }
4172
4173 public class QualifiedIdentifier {
4174         public string s;
4175         public QualifiedIdentifier prev;
4176
4177         public QualifiedIdentifier (string s)
4178         {       
4179                 this.s = s;
4180                 prev = null;
4181         }
4182
4183         public QualifiedIdentifier (QualifiedIdentifier prev, string s)
4184         {
4185                 this.s = s;
4186                 this.prev = prev;
4187         }
4188
4189         public override string ToString ()
4190         {
4191                 if (prev == null)
4192                         return s;
4193
4194                 // Common cases
4195
4196                 if (prev.prev == null)
4197                         return prev.s + "." + s;
4198                 if (prev.prev.prev == null)
4199                         return prev.prev.s + "." + prev.s + "." + s;
4200
4201                 // Uncommon cases.
4202                 QualifiedIdentifier ptr = this.prev;
4203                 int count = 1;
4204                 while (ptr != null){
4205                         count++;
4206                         ptr = ptr.prev;
4207                 }
4208                 string [] ss = new string [count];
4209                 ptr = this;
4210                 for (int i = count; i-- > 0;){
4211                         ss [i] = ptr.s;
4212                         ptr = ptr.prev;
4213                 }
4214                 return String.Join (".", ss, 0, ss.Length);
4215         }
4216 }
4217
4218 void Error_ExpectingTypeName (Location l, Expression expr)
4219 {
4220         if (expr is Invocation){
4221                 Report.Error (1002, l, "; expected");
4222         } else {
4223                 Report.Error (-1, l, "Invalid Type definition");
4224         }
4225 }
4226
4227 // <summary>
4228 //   Given the @class_name name, it creates a fully qualified name
4229 //   based on the containing declaration space
4230 // </summary>
4231 string 
4232 MakeName (string class_name)
4233 {
4234         string ns = current_namespace.FullName;
4235         string container_name = current_container.Name;
4236
4237         if (container_name == ""){
4238                 if (ns != "")
4239                         return ns + "." + class_name;
4240                 else
4241                         return class_name;
4242         } else
4243                 return container_name + "." + class_name;
4244 }
4245
4246 // <summary>
4247 //   Used to report back to the user the result of a declaration
4248 //   in the current declaration space
4249 // </summary>
4250 void 
4251 CheckDef (DeclSpace.AdditionResult result, string name, Location l)
4252 {
4253         if (result == DeclSpace.AdditionResult.Success)
4254                 return;
4255
4256         switch (result){
4257         case DeclSpace.AdditionResult.NameExists:
4258                 Report.Error (102, l, "The container `" + current_container.Name + 
4259                                  "' already contains a definition for `"+
4260                                  name + "'");
4261                 break;
4262
4263
4264                 //
4265                 // This is handled only for static Constructors, because
4266                 // in reality we handle these by the semantic analysis later
4267                 //
4268         case DeclSpace.AdditionResult.MethodExists:
4269                 Report.Error (
4270                         111, l, "Class `"+current_container.Name+
4271                         "' already defines a member called '" + 
4272                         name + "' with the same parameter types (more than one default constructor)");
4273                 break;
4274
4275         case DeclSpace.AdditionResult.EnclosingClash:
4276                 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
4277                 break;
4278                 
4279         case DeclSpace.AdditionResult.NotAConstructor:
4280                 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
4281                 break;
4282
4283         case DeclSpace.AdditionResult.Error:
4284                 // Error has already been reported.
4285                 break;
4286         }
4287 }
4288
4289 void 
4290 CheckDef (bool result, string name, Location l)
4291 {
4292         if (result)
4293                 return;
4294         CheckDef (DeclSpace.AdditionResult.NameExists, name, l);
4295 }
4296
4297 Expression DecomposeQI (QualifiedIdentifier qi, Location loc)
4298 {
4299         Expression o;
4300
4301         if (qi.prev == null)
4302                 return new SimpleName (qi.s, loc);
4303         else {
4304                 o = DecomposeQI (qi.prev, loc);
4305                 return new MemberAccess (o, qi.s, loc);
4306         }
4307 }
4308
4309 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4310 {
4311         Block implicit_block;
4312         ArrayList inits = null;
4313
4314         //
4315         // We use the `Used' property to check whether statements
4316         // have been added to the current block.  If so, we need
4317         // to create another block to contain the new declaration
4318         // otherwise, as an optimization, we use the same block to
4319         // add the declaration.
4320         //
4321         // FIXME: A further optimization is to check if the statements
4322         // that were added were added as part of the initialization
4323         // below.  In which case, no other statements have been executed
4324         // and we might be able to reduce the number of blocks for
4325         // situations like this:
4326         //
4327         // int j = 1;  int k = j + 1;
4328         //
4329         if (current_block.Used) {
4330                 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4331                 implicit_block.AddChildVariableNames (current_block);
4332         } else
4333                 implicit_block = current_block;
4334
4335         foreach (VariableDeclaration decl in variable_declarators){
4336
4337                 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4338                         if (decl.expression_or_array_initializer != null){
4339                                 if (inits == null)
4340                                         inits = new ArrayList (4);
4341                                 inits.Add (decl);
4342                         }
4343                 }
4344         }
4345
4346         if (inits == null)
4347                 return implicit_block;
4348
4349         foreach (VariableDeclaration decl in inits){
4350                 Assign assign;
4351                 Expression expr;
4352                 
4353                 if (decl.expression_or_array_initializer is Expression){
4354                         expr = (Expression) decl.expression_or_array_initializer;
4355
4356                 } else {
4357                         ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4358                         
4359                         expr = new ArrayCreation (type, "", init, decl.Location);
4360                 }
4361
4362                 LocalVariableReference var;
4363                 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4364
4365                 assign = new Assign (var, expr, decl.Location);
4366
4367                 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4368         }
4369         
4370         return implicit_block;
4371 }
4372
4373 Block declare_local_constants (Expression type, ArrayList declarators)
4374 {
4375         Block implicit_block;
4376
4377         if (current_block.Used)
4378                 implicit_block = new Block (current_block, Block.Flags.Implicit);
4379         else
4380                 implicit_block = current_block;
4381
4382         foreach (VariableDeclaration decl in declarators){
4383                 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4384                                                   current_local_parameters, decl.Location);
4385         }
4386         
4387         return implicit_block;
4388 }
4389
4390 void CheckAttributeTarget (string a)
4391 {
4392         switch (a) {
4393
4394         case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
4395                 return;
4396                 
4397         default :
4398                 Location l = lexer.Location;
4399                 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4400                 break;
4401         }
4402
4403 }
4404
4405 void CheckUnaryOperator (Operator.OpType op)
4406 {
4407         switch (op) {
4408                 
4409         case Operator.OpType.LogicalNot: 
4410         case Operator.OpType.OnesComplement: 
4411         case Operator.OpType.Increment:
4412         case Operator.OpType.Decrement:
4413         case Operator.OpType.True: 
4414         case Operator.OpType.False: 
4415         case Operator.OpType.Addition: 
4416         case Operator.OpType.Subtraction:
4417                 
4418                 break;
4419                 
4420         default :
4421                 Location l = lexer.Location;
4422                 Report.Error (1019, l, "Overloadable unary operator expected"); 
4423                 break;
4424                 
4425         }
4426 }
4427
4428 void CheckBinaryOperator (Operator.OpType op)
4429 {
4430         switch (op) {
4431                 
4432         case Operator.OpType.Addition: 
4433         case Operator.OpType.Subtraction: 
4434         case Operator.OpType.Multiply:
4435         case Operator.OpType.Division:
4436         case Operator.OpType.Modulus: 
4437         case Operator.OpType.BitwiseAnd: 
4438         case Operator.OpType.BitwiseOr:
4439         case Operator.OpType.ExclusiveOr: 
4440         case Operator.OpType.LeftShift: 
4441         case Operator.OpType.RightShift:
4442         case Operator.OpType.Equality: 
4443         case Operator.OpType.Inequality:
4444         case Operator.OpType.GreaterThan: 
4445         case Operator.OpType.LessThan: 
4446         case Operator.OpType.GreaterThanOrEqual:
4447         case Operator.OpType.LessThanOrEqual:
4448                 break;
4449                 
4450         default :
4451                 Location l = lexer.Location;
4452                 Report.Error (1020, l, "Overloadable binary operator expected");
4453                 break;
4454         }
4455         
4456 }
4457
4458 void syntax_error (Location l, string msg)
4459 {
4460         Report.Error (1003, l, "Syntax error, " + msg);
4461 }
4462
4463 void output (string s)
4464 {
4465         Console.WriteLine (s);
4466 }
4467
4468 void note (string s)
4469 {
4470         // Used to put annotations
4471 }
4472
4473 Tokenizer lexer;
4474
4475 public Tokenizer Lexer {
4476         get {
4477                 return lexer;
4478         }
4479 }                  
4480
4481 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4482 {
4483         current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4484         this.name = file.Name;
4485         this.file = file;
4486         current_container = RootContext.Tree.Types;
4487         current_container.NamespaceEntry = current_namespace;
4488         oob_stack = new Stack ();
4489         switch_stack = new Stack ();
4490
4491         lexer = new Tokenizer (reader, file, defines);
4492 }
4493
4494 public void parse ()
4495 {
4496         try {
4497                 if (yacc_verbose_flag)
4498                         yyparse (lexer, new yydebug.yyDebugSimple ());
4499                 else
4500                         yyparse (lexer);
4501                 Tokenizer tokenizer = lexer as Tokenizer;
4502                 tokenizer.cleanup ();           
4503         } catch (Exception e){
4504                 // Please do not remove this, it is used during debugging
4505                 // of the grammar
4506                 //
4507                 Console.WriteLine (e);
4508                 Report.Error (-25, lexer.Location, "Parsing error");
4509                 if (Driver.parser_verbose)
4510                         Console.WriteLine (e);
4511         }
4512 }
4513
4514 void CheckToken (int error, int yyToken, string msg)
4515 {
4516         if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4517                 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4518                 return;
4519         }               
4520         Report.Error (error, lexer.Location, msg);
4521 }
4522
4523 void CheckIdentifierToken (int yyToken)
4524 {
4525         CheckToken (1041, yyToken, "Identifier expected");
4526 }
4527
4528 /* end end end */
4529 }