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