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