2004-11-18 Marek Safar <marek.safar@seznam.cz>
[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 GET SEMICOLON          { $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, lexer.Location, lexer.Location); }
1427         | opt_attributes SET SEMICOLON          { $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1, lexer.Location, lexer.Location); }
1428         | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON 
1429           { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $3, lexer.Location, lexer.Location); }
1430         | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1431           { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $3, (Attributes) $1, lexer.Location, lexer.Location); }
1432         ;
1433
1434 interface_event_declaration
1435         : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1436           {
1437                 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1438                                      new MemberName ((string) $5), null,
1439                                      (Attributes) $1, lexer.Location);
1440           }
1441         | opt_attributes opt_new EVENT type error {
1442                 CheckIdentifierToken (yyToken);
1443                 $$ = null;
1444           }
1445         | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN  {
1446                 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1447                 $$ = null;
1448           }
1449         | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE event_accessor_declarations CLOSE_BRACE {
1450                 Report.Error (69, lexer.Location, "Event accessors not valid on interfaces");
1451                 $$ = null;
1452           }
1453         ;
1454
1455 interface_indexer_declaration 
1456         : opt_attributes opt_new type THIS 
1457           OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1458           OPEN_BRACE 
1459           { lexer.PropertyParsing = true; }
1460           interface_accessors 
1461           { lexer.PropertyParsing = false; }
1462           CLOSE_BRACE
1463           {
1464                 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1465
1466                 $$ = new Indexer (current_class, (Expression) $3,
1467                                   new MemberName (TypeContainer.DefaultIndexerName),
1468                                   (int) $2, true, (Parameters) $6, (Attributes) $1,
1469                                   info.Get, info.Set, lexer.Location);
1470           }
1471         ;
1472
1473 operator_declaration
1474         : opt_attributes opt_modifiers operator_declarator 
1475           {
1476                 iterator_container = SimpleIteratorContainer.GetSimple ();
1477           }
1478           operator_body
1479           {
1480                 OperatorDeclaration decl = (OperatorDeclaration) $3;
1481                 
1482                 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1483
1484                 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null);
1485                 if (decl.arg2type != null)
1486                         param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null);
1487
1488                 Operator op = new Operator (
1489                         current_class, decl.optype, decl.ret_type, (int) $2, 
1490                         new Parameters (param_list, null, decl.location),
1491                         (ToplevelBlock) $5, (Attributes) $1, decl.location);
1492
1493                 if (SimpleIteratorContainer.Simple.Yields)
1494                         op.SetYields ();
1495
1496                 // Note again, checking is done in semantic analysis
1497                 current_container.AddOperator (op);
1498
1499                 current_local_parameters = null;
1500                 iterator_container = null;
1501           }
1502         ;
1503
1504 operator_body 
1505         : block
1506         | SEMICOLON { $$ = null; }
1507         ; 
1508 operator_declarator
1509         : type OPERATOR overloadable_operator 
1510           OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1511         {
1512                 Operator.OpType op = (Operator.OpType) $3;
1513                 CheckUnaryOperator (op);
1514
1515                 if (op == Operator.OpType.Addition)
1516                         op = Operator.OpType.UnaryPlus;
1517
1518                 if (op == Operator.OpType.Subtraction)
1519                         op = Operator.OpType.UnaryNegation;
1520
1521                 Parameter [] pars = new Parameter [1];
1522
1523                 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1524
1525                 current_local_parameters = new Parameters (pars, null, lexer.Location);
1526
1527                 $$ = new OperatorDeclaration (op, (Expression) $1, (Expression) $5, (string) $6,
1528                                               null, null, lexer.Location);
1529         }
1530         | type OPERATOR overloadable_operator
1531           OPEN_PARENS 
1532                 type IDENTIFIER COMMA
1533                 type IDENTIFIER 
1534           CLOSE_PARENS
1535         {
1536                CheckBinaryOperator ((Operator.OpType) $3);
1537
1538                Parameter [] pars = new Parameter [2];
1539
1540                pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1541                pars [1] = new Parameter ((Expression) $8, (string) $9, Parameter.Modifier.NONE, null);
1542
1543                current_local_parameters = new Parameters (pars, null, lexer.Location);
1544                
1545                $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1, 
1546                                              (Expression) $5, (string) $6,
1547                                              (Expression) $8, (string) $9, lexer.Location);
1548         }
1549         | conversion_operator_declarator
1550         ;
1551
1552 overloadable_operator
1553 // Unary operators:
1554         : BANG   { $$ = Operator.OpType.LogicalNot; }
1555         | TILDE  { $$ = Operator.OpType.OnesComplement; }  
1556         | OP_INC { $$ = Operator.OpType.Increment; }
1557         | OP_DEC { $$ = Operator.OpType.Decrement; }
1558         | TRUE   { $$ = Operator.OpType.True; }
1559         | FALSE  { $$ = Operator.OpType.False; }
1560 // Unary and binary:
1561         | PLUS { $$ = Operator.OpType.Addition; }
1562         | MINUS { $$ = Operator.OpType.Subtraction; }
1563 // Binary:
1564         | STAR { $$ = Operator.OpType.Multiply; }
1565         | DIV {  $$ = Operator.OpType.Division; }
1566         | PERCENT { $$ = Operator.OpType.Modulus; }
1567         | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1568         | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1569         | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1570         | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1571         | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1572         | OP_EQ { $$ = Operator.OpType.Equality; }
1573         | OP_NE { $$ = Operator.OpType.Inequality; }
1574         | OP_GT { $$ = Operator.OpType.GreaterThan; }
1575         | OP_LT { $$ = Operator.OpType.LessThan; }
1576         | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1577         | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1578         ;
1579
1580 conversion_operator_declarator
1581         : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1582           {
1583                 Parameter [] pars = new Parameter [1];
1584
1585                 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1586
1587                 current_local_parameters = new Parameters (pars, null, lexer.Location);  
1588                   
1589                 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1590                                               null, null, lexer.Location);
1591           }
1592         | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1593           {
1594                 Parameter [] pars = new Parameter [1];
1595
1596                 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1597
1598                 current_local_parameters = new Parameters (pars, null, lexer.Location);  
1599                   
1600                 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1601                                               null, null, lexer.Location);
1602           }
1603         | IMPLICIT error 
1604           {
1605                 syntax_error (lexer.Location, "'operator' expected");
1606           }
1607         | EXPLICIT error 
1608           {
1609                 syntax_error (lexer.Location, "'operator' expected");
1610           }
1611         ;
1612
1613 constructor_declaration
1614         : opt_attributes
1615           opt_modifiers
1616           constructor_declarator
1617           constructor_body
1618           { 
1619                 Constructor c = (Constructor) $3;
1620                 c.Block = (ToplevelBlock) $4;
1621                 c.OptAttributes = (Attributes) $1;
1622                 c.ModFlags = (int) $2;
1623         
1624                 if (c.Name == current_container.Basename){
1625                         if ((c.ModFlags & Modifiers.STATIC) != 0){
1626                                 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1627                                         Report.Error (
1628                                                 515, c.Location, String.Format (
1629                                                 "`{0}.{1}': static constructor can not have access modifiers",
1630                                                 c.Name, current_container.Name));
1631                                 }
1632         
1633                                 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);   
1634         
1635                                 if (c.Initializer != null){
1636                                         Report.Error (
1637                                                 514, c.Location, 
1638                                                 "Static constructors can not have an explicit this or base " +
1639                                                 "constructor invocations");
1640                                 }
1641         
1642                                 if (!c.Parameters.Empty){
1643                                         Report.Error (
1644                                                 132, c.Location, "Static constructors should not have parameters");
1645                                 }
1646                         } else {
1647                                 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1648                         }
1649                 } else {
1650                         // We let another layer check the validity of the constructor.
1651                         //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1652                 }
1653
1654                 current_container.AddConstructor (c);
1655
1656                 current_local_parameters = null;
1657           }
1658         ;
1659
1660 constructor_declarator
1661         : IDENTIFIER 
1662           OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS 
1663           {
1664                 oob_stack.Push (lexer.Location);
1665
1666                 current_local_parameters = (Parameters) $3;
1667           }
1668           opt_constructor_initializer
1669           {
1670                 Location l = (Location) oob_stack.Pop ();
1671                 $$ = new Constructor (current_class, (string) $1, 0, (Parameters) $3,
1672                                       (ConstructorInitializer) $6, l);
1673           }
1674         ;
1675
1676 constructor_body
1677         : block
1678         | SEMICOLON             { $$ = null; }
1679         ;
1680
1681 opt_constructor_initializer
1682         : /* empty */                   { $$ = null; }
1683         | constructor_initializer
1684         ;
1685
1686 constructor_initializer
1687         : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1688           {
1689                 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1690           }
1691         | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1692           {
1693                 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1694           }
1695         | COLON error {
1696                 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1697                 $$ = null;
1698           }
1699         ;
1700
1701 opt_finalizer
1702         : /* EMPTY */           { $$ = 0; }
1703         | UNSAFE                { $$ = Modifiers.UNSAFE; }
1704         | EXTERN                { $$ = Modifiers.EXTERN; }
1705         ;
1706         
1707 destructor_declaration
1708         : opt_attributes opt_finalizer TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1709           {
1710                 if ((string) $4 != current_container.Basename){
1711                         Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1712                 } else if (!(current_container is Class)){
1713                         Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1714                 } else {
1715                         Location l = lexer.Location;
1716
1717                         int m = (int) $2;
1718                         if (!RootContext.StdLib && current_container.Name == "System.Object")
1719                                 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
1720                         else
1721                                 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
1722                         
1723                         if ((m & Modifiers.UNSAFE) != 0){
1724                                 if (!RootContext.Unsafe){
1725                                         Report.Error (227, l,
1726                                               "Unsafe code requires the -unsafe command " +
1727                                               "line option to be specified");
1728                                 }
1729                         }
1730                         
1731                         Method d = new Destructor (
1732                                 current_class, TypeManager.system_void_expr, m, "Finalize", 
1733                                 new Parameters (null, null, l), (Attributes) $1, l);
1734                   
1735                         d.Block = (ToplevelBlock) $7;
1736                         current_container.AddMethod (d);
1737                 }
1738           }
1739         ;
1740
1741 event_declaration
1742         : opt_attributes
1743           opt_modifiers
1744           EVENT type variable_declarators SEMICOLON
1745           {
1746                 foreach (VariableDeclaration var in (ArrayList) $5) {
1747
1748                         MemberName name = new MemberName (var.identifier);
1749
1750                         Event e = new EventField (
1751                                 current_class, (Expression) $4, (int) $2, false, name,
1752                                 var.expression_or_array_initializer, (Attributes) $1,
1753                                 lexer.Location);
1754
1755                         current_container.AddEvent (e);
1756                                        
1757                 }
1758           }
1759         | opt_attributes
1760           opt_modifiers
1761           EVENT type namespace_or_type_name
1762           OPEN_BRACE
1763           {
1764                 implicit_value_parameter_type = (Expression) $4;  
1765                 lexer.EventParsing = true;
1766                 oob_stack.Push (lexer.Location);
1767           }
1768           event_accessor_declarations
1769           {
1770                 lexer.EventParsing = false;  
1771           }
1772           CLOSE_BRACE
1773           {
1774                 Location loc = (Location) oob_stack.Pop ();
1775
1776                 if ($8 == null){
1777                         Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
1778                         $$ = null;
1779                 } else {
1780                         Pair pair = (Pair) $8;
1781                         
1782                         MemberName name = (MemberName) $5;
1783
1784                         Event e = new EventProperty (
1785                                 current_class, (Expression) $4, (int) $2, false, name, null,
1786                                 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
1787                                 loc);
1788                         
1789                         current_container.AddEvent (e);
1790                         implicit_value_parameter_type = null;
1791                 }
1792           }
1793         | opt_attributes opt_modifiers EVENT type namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS block {
1794                 MemberName mn = (MemberName) $5;
1795
1796                 if (mn.Left != null)
1797                         Report.Error (71, lexer.Location, "Explicit implementation of events requires property syntax");
1798                 else 
1799                         Report.Error (71, lexer.Location, "Event declaration should use property syntax");
1800           }
1801         ;
1802
1803 event_accessor_declarations
1804         : add_accessor_declaration remove_accessor_declaration
1805         {
1806                 $$ = new Pair ($1, $2);
1807         }
1808         | remove_accessor_declaration add_accessor_declaration
1809         {
1810                 $$ = new Pair ($2, $1);
1811         }       
1812         | add_accessor_declaration  { $$ = null; } 
1813         | remove_accessor_declaration { $$ = null; } 
1814         | error
1815         { 
1816                 Report.Error (1055, lexer.Location, "An add or remove accessor expected");
1817                 $$ = null;
1818         }
1819         | { $$ = null; }
1820         ;
1821
1822 add_accessor_declaration
1823         : opt_attributes ADD
1824           {
1825                 Parameter [] args = new Parameter [1];
1826                 Parameter implicit_value_parameter = new Parameter (
1827                         implicit_value_parameter_type, "value", 
1828                         Parameter.Modifier.NONE, null);
1829
1830                 args [0] = implicit_value_parameter;
1831                 
1832                 current_local_parameters = new Parameters (args, null, lexer.Location);  
1833                 lexer.EventParsing = false;
1834           }
1835           block
1836           {
1837                 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
1838                 lexer.EventParsing = true;
1839           }
1840         | opt_attributes ADD error {
1841                 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1842                 $$ = null;
1843           }
1844         ;
1845
1846 remove_accessor_declaration
1847         : opt_attributes REMOVE
1848           {
1849                 Parameter [] args = new Parameter [1];
1850                 Parameter implicit_value_parameter = new Parameter (
1851                         implicit_value_parameter_type, "value", 
1852                         Parameter.Modifier.NONE, null);
1853
1854                 args [0] = implicit_value_parameter;
1855                 
1856                 current_local_parameters = new Parameters (args, null, lexer.Location);  
1857                 lexer.EventParsing = false;
1858           }
1859           block
1860           {
1861                 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
1862                 lexer.EventParsing = true;
1863           }
1864         | opt_attributes REMOVE error {
1865                 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1866                 $$ = null;
1867           }
1868         ;
1869
1870 indexer_declaration
1871         : opt_attributes opt_modifiers indexer_declarator 
1872           OPEN_BRACE 
1873           {
1874                 IndexerDeclaration decl = (IndexerDeclaration) $3;
1875
1876                 implicit_value_parameter_type = decl.type;
1877                 
1878                 lexer.PropertyParsing = true;
1879                 parsing_indexer  = true;
1880                 
1881                 indexer_parameters = decl.param_list;
1882                 oob_stack.Push (lexer.Location);
1883           }
1884           accessor_declarations 
1885           {
1886                   lexer.PropertyParsing = false;
1887                   parsing_indexer  = false;
1888           }
1889           CLOSE_BRACE
1890           { 
1891                 // The signature is computed from the signature of the indexer.  Look
1892                 // at section 3.6 on the spec
1893                 Location loc = (Location) oob_stack.Pop ();
1894                 Indexer indexer;
1895                 IndexerDeclaration decl = (IndexerDeclaration) $3;
1896                 Pair pair = (Pair) $6;
1897                 Accessor get_block = (Accessor) pair.First;
1898                 Accessor set_block = (Accessor) pair.Second;
1899
1900                 MemberName name;
1901                 if (decl.interface_type != null)
1902                         name = new MemberName (decl.interface_type,
1903                                                TypeContainer.DefaultIndexerName);
1904                 else
1905                         name = new MemberName (TypeContainer.DefaultIndexerName);
1906
1907                 indexer = new Indexer (current_class, decl.type, name,
1908                                        (int) $2, false, decl.param_list, (Attributes) $1,
1909                                        get_block, set_block, loc);
1910
1911                 current_container.AddIndexer (indexer);
1912                 
1913                 current_local_parameters = null;
1914                 implicit_value_parameter_type = null;
1915                 indexer_parameters = null;
1916           }
1917         ;
1918
1919 indexer_declarator
1920         : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1921           {
1922                 Parameters pars = (Parameters) $4;
1923                 if (pars.HasArglist) {
1924                         // "__arglist is not valid in this context"
1925                         Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
1926                 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
1927                         Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1928                 }
1929
1930                 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
1931           }
1932         | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1933           {
1934                 Parameters pars = (Parameters) $6;
1935
1936                 if (pars.HasArglist) {
1937                         // "__arglist is not valid in this context"
1938                         Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
1939                 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
1940                         Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1941                 }
1942                 MemberName name = (MemberName) $2;
1943                 $$ = new IndexerDeclaration ((Expression) $1, name, pars);
1944           }
1945         ;
1946
1947 enum_declaration
1948         : opt_attributes
1949           opt_modifiers
1950           ENUM IDENTIFIER 
1951           opt_enum_base
1952           enum_body
1953           opt_semicolon
1954           { 
1955                 Location enum_location = lexer.Location;
1956
1957                 MemberName full_name = MakeName (new MemberName ((string) $4));
1958                 Enum e = new Enum (current_namespace, current_container, (Expression) $5, (int) $2,
1959                                    full_name, (Attributes) $1, enum_location);
1960                 
1961                 foreach (VariableDeclaration ev in (ArrayList) $6) {
1962                         e.AddEnumMember (ev.identifier, 
1963                                          (Expression) ev.expression_or_array_initializer,
1964                                          ev.Location, ev.OptAttributes);
1965                 }
1966
1967                 string name = full_name.GetName ();
1968                 current_container.AddEnum (e);
1969                 RootContext.Tree.RecordDecl (name, e);
1970
1971           }
1972         ;
1973
1974 opt_enum_base
1975         : /* empty */           { $$ = TypeManager.system_int32_expr; }
1976         | COLON type            { $$ = $2;   }
1977         ;
1978
1979 enum_body
1980         : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1981           {
1982                 $$ = $2;
1983           }
1984         ;
1985
1986 opt_enum_member_declarations
1987         : /* empty */                   { $$ = new ArrayList (4); }
1988         | enum_member_declarations opt_comma { $$ = $1; }
1989         ;
1990
1991 enum_member_declarations
1992         : enum_member_declaration 
1993           {
1994                 ArrayList l = new ArrayList (4);
1995
1996                 l.Add ($1);
1997                 $$ = l;
1998           }
1999         | enum_member_declarations COMMA enum_member_declaration
2000           {
2001                 ArrayList l = (ArrayList) $1;
2002
2003                 l.Add ($3);
2004
2005                 $$ = l;
2006           }
2007         ;
2008
2009 enum_member_declaration
2010         : opt_attributes IDENTIFIER 
2011           {
2012                 $$ = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
2013           }
2014         | opt_attributes IDENTIFIER
2015           {
2016                   $$ = lexer.Location;
2017           }
2018           ASSIGN expression
2019           { 
2020                 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
2021           }
2022         ;
2023
2024 delegate_declaration
2025         : opt_attributes
2026           opt_modifiers
2027           DELEGATE type member_name
2028           OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS 
2029           SEMICOLON
2030           {
2031                 Location l = lexer.Location;
2032                 MemberName name = MakeName ((MemberName) $5);
2033                 Delegate del = new Delegate (current_namespace, current_container, (Expression) $4,
2034                                              (int) $2, name, (Parameters) $7, (Attributes) $1, l);
2035
2036                 current_container.AddDelegate (del);
2037                 RootContext.Tree.RecordDecl (name.GetName (true), del);
2038           }     
2039         | opt_attributes
2040           opt_modifiers
2041           DELEGATE VOID member_name
2042           OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS 
2043           SEMICOLON
2044           {
2045                 Location l = lexer.Location;
2046                 MemberName name = MakeName ((MemberName) $5);
2047                 Delegate del = new Delegate (
2048                         current_namespace, current_container,
2049                         TypeManager.system_void_expr, (int) $2, name,
2050                         (Parameters) $7, (Attributes) $1, l);
2051
2052                 current_container.AddDelegate (del);
2053                 RootContext.Tree.RecordDecl (name.GetName (true), del);
2054           }
2055         ;
2056
2057 namespace_or_type_name
2058         : member_name
2059         | namespace_or_type_name DOT IDENTIFIER {
2060                 $$ = new MemberName ((MemberName) $1, (string) $3);
2061           }
2062         ;
2063
2064 member_name
2065         : IDENTIFIER {
2066                 $$ = new MemberName ((string) $1);
2067           }
2068         ;
2069
2070 /* 
2071  * Before you think of adding a return_type, notice that we have been
2072  * using two rules in the places where it matters (one rule using type
2073  * and another identical one that uses VOID as the return type).  This
2074  * gets rid of a shift/reduce couple
2075  */
2076 type
2077         : namespace_or_type_name
2078           {
2079                 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2080           }
2081         | builtin_types
2082         | array_type
2083         | pointer_type    
2084         ;
2085
2086
2087 pointer_type
2088         : type STAR
2089           {
2090                 //
2091                 // Note that here only unmanaged types are allowed but we
2092                 // can't perform checks during this phase - we do it during
2093                 // semantic analysis.
2094                 //
2095                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2096           }
2097         | VOID STAR
2098           {
2099                 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2100           }
2101         ;
2102
2103 non_expression_type
2104         : builtin_types 
2105         | non_expression_type rank_specifier
2106           {
2107                 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2108           }
2109         | non_expression_type STAR
2110           {
2111                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2112           }
2113         | expression rank_specifiers 
2114           {
2115                 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2116           }
2117         | expression STAR 
2118           {
2119                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2120           }
2121         
2122         //
2123         // We need this because the parser will happily go and reduce IDENTIFIER STAR
2124         // through this different path
2125         //
2126         | multiplicative_expression STAR 
2127           {
2128                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2129           }
2130         ;
2131
2132 type_list
2133         : type
2134           {
2135                 ArrayList types = new ArrayList (4);
2136
2137                 types.Add ($1);
2138                 $$ = types;
2139           }
2140         | type_list COMMA type
2141           {
2142                 ArrayList types = (ArrayList) $1;
2143
2144                 types.Add ($3);
2145                 $$ = types;
2146           }
2147         ;
2148
2149 /*
2150  * replaces all the productions for isolating the various
2151  * simple types, but we need this to reuse it easily in local_variable_type
2152  */
2153 builtin_types
2154         : OBJECT        { $$ = TypeManager.system_object_expr; }
2155         | STRING        { $$ = TypeManager.system_string_expr; }
2156         | BOOL          { $$ = TypeManager.system_boolean_expr; }
2157         | DECIMAL       { $$ = TypeManager.system_decimal_expr; }
2158         | FLOAT         { $$ = TypeManager.system_single_expr; }
2159         | DOUBLE        { $$ = TypeManager.system_double_expr; }
2160         | integral_type
2161         ;
2162
2163 integral_type
2164         : SBYTE         { $$ = TypeManager.system_sbyte_expr; }
2165         | BYTE          { $$ = TypeManager.system_byte_expr; }
2166         | SHORT         { $$ = TypeManager.system_int16_expr; }
2167         | USHORT        { $$ = TypeManager.system_uint16_expr; }
2168         | INT           { $$ = TypeManager.system_int32_expr; }
2169         | UINT          { $$ = TypeManager.system_uint32_expr; }
2170         | LONG          { $$ = TypeManager.system_int64_expr; }
2171         | ULONG         { $$ = TypeManager.system_uint64_expr; }
2172         | CHAR          { $$ = TypeManager.system_char_expr; }
2173         | VOID          { $$ = TypeManager.system_void_expr; }
2174         ;
2175
2176 array_type
2177         : type rank_specifiers
2178           {
2179                 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2180           }
2181         ;
2182
2183 //
2184 // Expressions, section 7.5
2185 //
2186 primary_expression
2187         : literal
2188           {
2189                 // 7.5.1: Literals
2190           }
2191  
2192         | member_name
2193           {
2194                 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2195           }
2196         | parenthesized_expression
2197         | member_access
2198         | invocation_expression
2199         | element_access
2200         | this_access
2201         | base_access
2202         | post_increment_expression
2203         | post_decrement_expression
2204         | new_expression
2205         | typeof_expression
2206         | sizeof_expression
2207         | checked_expression
2208         | unchecked_expression
2209         | pointer_member_access
2210         | anonymous_method_expression
2211         ;
2212
2213 literal
2214         : boolean_literal
2215         | integer_literal
2216         | real_literal
2217         | LITERAL_CHARACTER     { $$ = new CharLiteral ((char) lexer.Value); }
2218         | LITERAL_STRING        { $$ = new StringLiteral ((string) lexer.Value); }
2219         | NULL                  { $$ = NullLiteral.Null; }
2220         ;
2221
2222 real_literal
2223         : LITERAL_FLOAT         { $$ = new FloatLiteral ((float) lexer.Value); }
2224         | LITERAL_DOUBLE        { $$ = new DoubleLiteral ((double) lexer.Value); }
2225         | LITERAL_DECIMAL       { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2226         ;
2227
2228 integer_literal
2229         : LITERAL_INTEGER       { 
2230                 object v = lexer.Value;
2231
2232                 if (v is int){
2233                         int i = (int) v;
2234
2235                         if (i == 0)
2236                                 $$ = IntLiteral.Zero;
2237                         else if (i == 1)
2238                                 $$ = IntLiteral.One;
2239                         else
2240                                 $$ = new IntLiteral (i);
2241                 } else if (v is uint)
2242                         $$ = new UIntLiteral ((UInt32) v);
2243                 else if (v is long)
2244                         $$ = new LongLiteral ((Int64) v);
2245                 else if (v is ulong)
2246                         $$ = new ULongLiteral ((UInt64) v);
2247                 else
2248                         Console.WriteLine ("OOPS.  Unexpected result from scanner");
2249           }
2250         ;
2251
2252 boolean_literal
2253         : TRUE                  { $$ = new BoolLiteral (true); }
2254         | FALSE                 { $$ = new BoolLiteral (false); }
2255         ;
2256
2257 parenthesized_expression_0
2258         : OPEN_PARENS expression CLOSE_PARENS
2259           {
2260                 $$ = $2;
2261                 lexer.Deambiguate_CloseParens ();
2262                 // After this, the next token returned is one of
2263                 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2264                 // or CLOSE_PARENS_MINUS.
2265           }
2266         ;
2267
2268 parenthesized_expression
2269         : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2270           {
2271                 $$ = $1;
2272           }
2273         | parenthesized_expression_0 CLOSE_PARENS_MINUS
2274           {
2275                 // If a parenthesized expression is followed by a minus, we need to wrap
2276                 // the expression inside a ParenthesizedExpression for the CS0075 check
2277                 // in Binary.DoResolve().
2278                 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2279           }
2280         ;;
2281
2282 member_access
2283         : primary_expression DOT IDENTIFIER
2284           {
2285                 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2286           }
2287         | predefined_type DOT IDENTIFIER
2288           {
2289                 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2290           }
2291         ;
2292
2293 predefined_type
2294         : builtin_types
2295         ;
2296
2297 invocation_expression
2298         : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2299           {
2300                 if ($1 == null) {
2301                         Location l = lexer.Location;
2302                         Report.Error (1, l, "Parse error");
2303                 }
2304                 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2305           }
2306         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2307           {
2308                 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2309           }
2310         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2311           {
2312                 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2313           }
2314         ;
2315
2316 opt_argument_list
2317         : /* empty */           { $$ = null; }
2318         | argument_list
2319         ;
2320
2321 argument_list
2322         : argument
2323           { 
2324                 ArrayList list = new ArrayList (4);
2325                 list.Add ($1);
2326                 $$ = list;
2327           }
2328         | argument_list COMMA argument
2329           {
2330                 ArrayList list = (ArrayList) $1;
2331                 list.Add ($3);
2332                 $$ = list;
2333           }
2334         | argument_list error {
2335                 CheckToken (1026, yyToken, ", or ) expected");
2336           }
2337         ;
2338
2339 argument
2340         : expression
2341           {
2342                 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2343           }
2344         | REF variable_reference 
2345           { 
2346                 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2347           }
2348         | OUT variable_reference 
2349           { 
2350                 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2351           }
2352         | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2353           {
2354                 ArrayList list = (ArrayList) $3;
2355                 Argument[] args = new Argument [list.Count];
2356                 list.CopyTo (args, 0);
2357
2358                 Expression expr = new Arglist (args, lexer.Location);
2359                 $$ = new Argument (expr, Argument.AType.Expression);
2360           }
2361         | ARGLIST
2362           {
2363                 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2364           }
2365         ;
2366
2367 variable_reference
2368         : expression { note ("section 5.4"); $$ = $1; }
2369         ;
2370
2371 element_access
2372         : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET 
2373           {
2374                 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2375           }
2376         | primary_expression rank_specifiers
2377           {
2378                 // So the super-trick is that primary_expression
2379                 // can only be either a SimpleName or a MemberAccess. 
2380                 // The MemberAccess case arises when you have a fully qualified type-name like :
2381                 // Foo.Bar.Blah i;
2382                 // SimpleName is when you have
2383                 // Blah i;
2384                   
2385                 Expression expr = (Expression) $1;  
2386                 if (expr is ComposedCast){
2387                         $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2388                 } else if (!(expr is SimpleName || expr is MemberAccess)){
2389                         Error_ExpectingTypeName (lexer.Location, expr);
2390                         $$ = TypeManager.system_object_expr;
2391                 } else {
2392                         //
2393                         // So we extract the string corresponding to the SimpleName
2394                         // or MemberAccess
2395                         // 
2396                         $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2397                 }
2398           }
2399         ;
2400
2401 expression_list
2402         : expression
2403           {
2404                 ArrayList list = new ArrayList (4);
2405                 list.Add ($1);
2406                 $$ = list;
2407           }
2408         | expression_list COMMA expression
2409           {
2410                 ArrayList list = (ArrayList) $1;
2411                 list.Add ($3);
2412                 $$ = list;
2413           }
2414         ;
2415
2416 this_access
2417         : THIS
2418           {
2419                 $$ = new This (current_block, lexer.Location);
2420           }
2421         ;
2422
2423 base_access
2424         : BASE DOT IDENTIFIER
2425           {
2426                 $$ = new BaseAccess ((string) $3, lexer.Location);
2427           }
2428         | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2429           {
2430                 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2431           }
2432         | BASE error {
2433                 Report.Error (175, lexer.Location, "Use of keyword `base' is not valid in this context");
2434                 $$ = null;
2435           }
2436         ;
2437
2438 post_increment_expression
2439         : primary_expression OP_INC
2440           {
2441                 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2442                                        (Expression) $1, lexer.Location);
2443           }
2444         ;
2445
2446 post_decrement_expression
2447         : primary_expression OP_DEC
2448           {
2449                 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2450                                        (Expression) $1, lexer.Location);
2451           }
2452         ;
2453
2454 new_expression
2455         : object_or_delegate_creation_expression
2456         | array_creation_expression
2457         ;
2458
2459 object_or_delegate_creation_expression
2460         : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2461           {
2462                 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2463           }
2464         ;
2465
2466 array_creation_expression
2467         : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET 
2468           opt_rank_specifier
2469           opt_array_initializer
2470           {
2471                 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2472           }
2473         | NEW type rank_specifiers array_initializer
2474           {
2475                 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2476           }
2477         | NEW error
2478           {
2479                 Report.Error (1031, lexer.Location, "Type expected");
2480                 $$ = null;
2481           }          
2482         | NEW type error 
2483           {
2484                 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2485           }
2486         ;
2487
2488 opt_rank_specifier
2489         : /* empty */
2490           {
2491                   $$ = "";
2492           }
2493         | rank_specifiers
2494           {
2495                         $$ = $1;
2496           }
2497         ;
2498
2499 rank_specifiers
2500         : rank_specifier opt_rank_specifier
2501           {
2502                   $$ = (string) $2 + (string) $1;
2503           }
2504         ;
2505
2506 rank_specifier
2507         : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2508           {
2509                 $$ = "[" + (string) $2 + "]";
2510           }
2511         ;
2512
2513 opt_dim_separators
2514         : /* empty */
2515           {
2516                 $$ = "";
2517           }
2518         | dim_separators
2519           {
2520                   $$ = $1;
2521           }               
2522         ;
2523
2524 dim_separators
2525         : COMMA
2526           {
2527                 $$ = ",";
2528           }
2529         | dim_separators COMMA
2530           {
2531                 $$ = (string) $1 + ",";
2532           }
2533         ;
2534
2535 opt_array_initializer
2536         : /* empty */
2537           {
2538                 $$ = null;
2539           }
2540         | array_initializer
2541           {
2542                 $$ = $1;
2543           }
2544         ;
2545
2546 array_initializer
2547         : OPEN_BRACE CLOSE_BRACE
2548           {
2549                 ArrayList list = new ArrayList (4);
2550                 $$ = list;
2551           }
2552         | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2553           {
2554                 $$ = (ArrayList) $2;
2555           }
2556         ;
2557
2558 variable_initializer_list
2559         : variable_initializer
2560           {
2561                 ArrayList list = new ArrayList (4);
2562                 list.Add ($1);
2563                 $$ = list;
2564           }
2565         | variable_initializer_list COMMA variable_initializer
2566           {
2567                 ArrayList list = (ArrayList) $1;
2568                 list.Add ($3);
2569                 $$ = list;
2570           }
2571         ;
2572
2573 typeof_expression
2574         : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2575           {
2576                 $$ = new TypeOfVoid (lexer.Location);
2577           }
2578         | TYPEOF OPEN_PARENS type CLOSE_PARENS
2579           {
2580                 $$ = new TypeOf ((Expression) $3, lexer.Location);
2581           }
2582         ;
2583
2584 sizeof_expression
2585         : SIZEOF OPEN_PARENS type CLOSE_PARENS { 
2586                 $$ = new SizeOf ((Expression) $3, lexer.Location);
2587           }
2588         ;
2589
2590 checked_expression
2591         : CHECKED OPEN_PARENS expression CLOSE_PARENS
2592           {
2593                 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2594           }
2595         ;
2596
2597 unchecked_expression
2598         : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2599           {
2600                 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2601           }
2602         ;
2603
2604 pointer_member_access 
2605         : primary_expression OP_PTR IDENTIFIER
2606           {
2607                 Expression deref;
2608
2609                 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2610                 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2611           }
2612         ;
2613
2614 anonymous_method_expression
2615         : DELEGATE opt_anonymous_method_signature {
2616                 oob_stack.Push (current_local_parameters);
2617                 current_local_parameters = (Parameters)$2;
2618
2619                 // Force the next block to be created as a ToplevelBlock
2620                 oob_stack.Push (current_block);
2621                 oob_stack.Push (top_current_block);
2622                 oob_stack.Push (lexer.Location);
2623                 current_block = null;
2624           } block {
2625                 Location loc = (Location) oob_stack.Pop ();
2626                 top_current_block = (Block) oob_stack.Pop ();
2627                 current_block = (Block) oob_stack.Pop ();
2628                 if (RootContext.Version == LanguageVersion.ISO_1){
2629                         Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
2630                         $$ = null;
2631                 } else  {
2632                         ToplevelBlock anon_block = (ToplevelBlock) $4;
2633
2634                         anon_block.Parent = current_block;
2635                         $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block, 
2636                                 anon_block, loc);
2637                 }
2638                 current_local_parameters = (Parameters) oob_stack.Pop ();
2639           }
2640         ;
2641
2642 opt_anonymous_method_signature
2643         : /* empty */                   { $$ = null; } 
2644         | anonymous_method_signature
2645         ;
2646
2647 anonymous_method_signature
2648         : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS 
2649           {
2650                 if ($2 == null)
2651                         $$ = Parameters.EmptyReadOnlyParameters;
2652                 else {
2653                         ArrayList par_list = (ArrayList) $2;
2654                         Parameter [] pars = new Parameter [par_list.Count];
2655                         par_list.CopyTo (pars);
2656                         $$ = new Parameters (pars, null, lexer.Location);
2657                 }
2658           }
2659         ;
2660
2661 opt_anonymous_method_parameter_list
2662         : /* empty */                      { $$ = null; } 
2663         | anonymous_method_parameter_list  { $$ = $1; }
2664         ;
2665
2666 anonymous_method_parameter_list
2667         : anonymous_method_parameter 
2668           {
2669                 ArrayList a = new ArrayList (4);
2670                 a.Add ($1);
2671                 $$ = a;
2672           }
2673         | anonymous_method_parameter_list COMMA anonymous_method_parameter 
2674           {
2675                 ArrayList a = (ArrayList) $1;
2676                 a.Add ($3);
2677                 $$ = a;
2678           }
2679         ; 
2680
2681 anonymous_method_parameter
2682         : opt_parameter_modifier type IDENTIFIER {
2683                 $$ = new Parameter ((Expression) $2, (string) $3, (Parameter.Modifier) $1, null);
2684           }
2685         | PARAMS type IDENTIFIER {
2686                 Report.Error (-221, lexer.Location, "params modifier not allowed in anonymous method declaration");
2687                 $$ = null;
2688           }
2689         ;
2690
2691 unary_expression
2692         : primary_expression
2693         | BANG prefixed_unary_expression
2694           {
2695                 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2696           }
2697         | TILDE prefixed_unary_expression
2698           {
2699                 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2700           }
2701         | cast_expression
2702         ;
2703
2704 cast_list
2705         : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
2706           {
2707                 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2708           }
2709         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
2710           {
2711                 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2712           }     
2713         ;
2714
2715 cast_expression
2716         : cast_list
2717         | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2718           {
2719                 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2720           }
2721         ;
2722
2723         //
2724         // The idea to split this out is from Rhys' grammar
2725         // to solve the problem with casts.
2726         //
2727 prefixed_unary_expression
2728         : unary_expression
2729         | PLUS prefixed_unary_expression
2730           { 
2731                 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2732           } 
2733         | MINUS prefixed_unary_expression 
2734           { 
2735                 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2736           }
2737         | OP_INC prefixed_unary_expression 
2738           {
2739                 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2740                                        (Expression) $2, lexer.Location);
2741           }
2742         | OP_DEC prefixed_unary_expression 
2743           {
2744                 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2745                                        (Expression) $2, lexer.Location);
2746           }
2747         | STAR prefixed_unary_expression
2748           {
2749                 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2750           }
2751         | BITWISE_AND prefixed_unary_expression
2752           {
2753                 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2754           }
2755         ;
2756
2757 pre_increment_expression
2758         : OP_INC prefixed_unary_expression 
2759           {
2760                 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2761                                        (Expression) $2, lexer.Location);
2762           }
2763         ;
2764
2765 pre_decrement_expression
2766         : OP_DEC prefixed_unary_expression 
2767           {
2768                 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2769                                        (Expression) $2, lexer.Location);
2770           }
2771         ;
2772
2773 multiplicative_expression
2774         : prefixed_unary_expression
2775         | multiplicative_expression STAR prefixed_unary_expression
2776           {
2777                 $$ = new Binary (Binary.Operator.Multiply, 
2778                                  (Expression) $1, (Expression) $3, lexer.Location);
2779           }
2780         | multiplicative_expression DIV prefixed_unary_expression
2781           {
2782                 $$ = new Binary (Binary.Operator.Division, 
2783                                  (Expression) $1, (Expression) $3, lexer.Location);
2784           }
2785         | multiplicative_expression PERCENT prefixed_unary_expression 
2786           {
2787                 $$ = new Binary (Binary.Operator.Modulus, 
2788                                  (Expression) $1, (Expression) $3, lexer.Location);
2789           }
2790         ;
2791
2792 additive_expression
2793         : multiplicative_expression
2794         | additive_expression PLUS multiplicative_expression 
2795           {
2796                 $$ = new Binary (Binary.Operator.Addition, 
2797                                  (Expression) $1, (Expression) $3, lexer.Location);
2798           }
2799         | additive_expression MINUS multiplicative_expression
2800           {
2801                 $$ = new Binary (Binary.Operator.Subtraction, 
2802                                  (Expression) $1, (Expression) $3, lexer.Location);
2803           }
2804         ;
2805
2806 shift_expression
2807         : additive_expression
2808         | shift_expression OP_SHIFT_LEFT additive_expression
2809           {
2810                 $$ = new Binary (Binary.Operator.LeftShift, 
2811                                  (Expression) $1, (Expression) $3, lexer.Location);
2812           }
2813         | shift_expression OP_SHIFT_RIGHT additive_expression
2814           {
2815                 $$ = new Binary (Binary.Operator.RightShift, 
2816                                  (Expression) $1, (Expression) $3, lexer.Location);
2817           }
2818         ; 
2819
2820 relational_expression
2821         : shift_expression
2822         | relational_expression OP_LT shift_expression
2823           {
2824                 $$ = new Binary (Binary.Operator.LessThan, 
2825                                  (Expression) $1, (Expression) $3, lexer.Location);
2826           }
2827         | relational_expression OP_GT shift_expression
2828           {
2829                 $$ = new Binary (Binary.Operator.GreaterThan, 
2830                                  (Expression) $1, (Expression) $3, lexer.Location);
2831           }
2832         | relational_expression OP_LE shift_expression
2833           {
2834                 $$ = new Binary (Binary.Operator.LessThanOrEqual, 
2835                                  (Expression) $1, (Expression) $3, lexer.Location);
2836           }
2837         | relational_expression OP_GE shift_expression
2838           {
2839                 $$ = new Binary (Binary.Operator.GreaterThanOrEqual, 
2840                                  (Expression) $1, (Expression) $3, lexer.Location);
2841           }
2842         | relational_expression IS type
2843           {
2844                 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
2845           }
2846         | relational_expression AS type
2847           {
2848                 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
2849           }
2850         ;
2851
2852 equality_expression
2853         : relational_expression
2854         | equality_expression OP_EQ relational_expression
2855           {
2856                 $$ = new Binary (Binary.Operator.Equality, 
2857                                  (Expression) $1, (Expression) $3, lexer.Location);
2858           }
2859         | equality_expression OP_NE relational_expression
2860           {
2861                 $$ = new Binary (Binary.Operator.Inequality, 
2862                                  (Expression) $1, (Expression) $3, lexer.Location);
2863           }
2864         ; 
2865
2866 and_expression
2867         : equality_expression
2868         | and_expression BITWISE_AND equality_expression
2869           {
2870                 $$ = new Binary (Binary.Operator.BitwiseAnd, 
2871                                  (Expression) $1, (Expression) $3, lexer.Location);
2872           }
2873         ;
2874
2875 exclusive_or_expression
2876         : and_expression
2877         | exclusive_or_expression CARRET and_expression
2878           {
2879                 $$ = new Binary (Binary.Operator.ExclusiveOr, 
2880                                  (Expression) $1, (Expression) $3, lexer.Location);
2881           }
2882         ;
2883
2884 inclusive_or_expression
2885         : exclusive_or_expression
2886         | inclusive_or_expression BITWISE_OR exclusive_or_expression
2887           {
2888                 $$ = new Binary (Binary.Operator.BitwiseOr, 
2889                                  (Expression) $1, (Expression) $3, lexer.Location);
2890           }
2891         ;
2892
2893 conditional_and_expression
2894         : inclusive_or_expression
2895         | conditional_and_expression OP_AND inclusive_or_expression
2896           {
2897                 $$ = new Binary (Binary.Operator.LogicalAnd, 
2898                                  (Expression) $1, (Expression) $3, lexer.Location);
2899           }
2900         ;
2901
2902 conditional_or_expression
2903         : conditional_and_expression
2904         | conditional_or_expression OP_OR conditional_and_expression
2905           {
2906                 $$ = new Binary (Binary.Operator.LogicalOr, 
2907                                  (Expression) $1, (Expression) $3, lexer.Location);
2908           }
2909         ;
2910
2911 conditional_expression
2912         : conditional_or_expression
2913         | conditional_or_expression INTERR expression COLON expression 
2914           {
2915                 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2916           }
2917         ;
2918
2919 assignment_expression
2920         : prefixed_unary_expression ASSIGN expression
2921           {
2922                 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2923           }
2924         | prefixed_unary_expression OP_MULT_ASSIGN expression
2925           {
2926                 Location l = lexer.Location;
2927
2928                 $$ = new CompoundAssign (
2929                         Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
2930           }
2931         | prefixed_unary_expression OP_DIV_ASSIGN expression
2932           {
2933                 Location l = lexer.Location;
2934
2935                 $$ = new CompoundAssign (
2936                         Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
2937           }
2938         | prefixed_unary_expression OP_MOD_ASSIGN expression
2939           {
2940                 Location l = lexer.Location;
2941
2942                 $$ = new CompoundAssign (
2943                         Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
2944           }
2945         | prefixed_unary_expression OP_ADD_ASSIGN expression
2946           {
2947                 Location l = lexer.Location;
2948
2949                 $$ = new CompoundAssign (
2950                         Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
2951           }
2952         | prefixed_unary_expression OP_SUB_ASSIGN expression
2953           {
2954                 Location l = lexer.Location;
2955
2956                 $$ = new CompoundAssign (
2957                         Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
2958           }
2959         | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2960           {
2961                 Location l = lexer.Location;
2962
2963                 $$ = new CompoundAssign (
2964                         Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
2965           }
2966         | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2967           {
2968                 Location l = lexer.Location;
2969
2970                 $$ = new CompoundAssign (
2971                         Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
2972           }
2973         | prefixed_unary_expression OP_AND_ASSIGN expression
2974           {
2975                 Location l = lexer.Location;
2976
2977                 $$ = new CompoundAssign (
2978                         Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
2979           }
2980         | prefixed_unary_expression OP_OR_ASSIGN expression
2981           {
2982                 Location l = lexer.Location;
2983
2984                 $$ = new CompoundAssign (
2985                         Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
2986           }
2987         | prefixed_unary_expression OP_XOR_ASSIGN expression
2988           {
2989                 Location l = lexer.Location;
2990
2991                 $$ = new CompoundAssign (
2992                         Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
2993           }
2994         ;
2995
2996 expression
2997         : conditional_expression
2998         | assignment_expression
2999         ;
3000
3001 constant_expression
3002         : expression
3003         ;
3004
3005 boolean_expression
3006         : expression
3007         ;
3008
3009 //
3010 // 10 classes
3011 //
3012 class_declaration
3013         : opt_attributes
3014           opt_modifiers
3015           opt_partial
3016           CLASS member_name
3017           {
3018                 MemberName name = MakeName ((MemberName) $5);
3019                 bool partial = (bool) $3;
3020                 int mod_flags = (int) $2;
3021
3022                 if (partial) {
3023                         ClassPart part = PartialContainer.CreatePart (
3024                                 current_namespace, current_container, name, mod_flags,
3025                                 (Attributes) $1, Kind.Class, lexer.Location);
3026
3027                         current_container = part.PartialContainer;
3028                         current_class = part;
3029                 } else {
3030                         if ((mod_flags & Modifiers.STATIC) != 0) {
3031                                 current_class = new StaticClass (
3032                                         current_namespace, current_container, name,
3033                                         mod_flags, (Attributes) $1, lexer.Location);
3034                         } else {
3035                                 current_class = new Class (
3036                                         current_namespace, current_container, name,
3037                                         mod_flags, (Attributes) $1, lexer.Location);
3038                         }
3039
3040                         current_container = current_class;
3041                         RootContext.Tree.RecordDecl (name.GetName (true), current_class);
3042                 }
3043           }
3044           opt_class_base
3045           {
3046                 if ($7 != null) {
3047                         if (current_class.Name == "System.Object") {
3048                                 Report.Error (537, current_class.Location,
3049                                               "The class System.Object cannot have a base " +
3050                                               "class or implement an interface.");
3051                         }
3052                         current_class.Bases = (ArrayList) $7;
3053                 }
3054
3055                 current_class.Register ();
3056           }
3057           class_body 
3058           opt_semicolon 
3059           {
3060                 $$ = current_class;
3061
3062                 current_container = current_container.Parent;
3063                 current_class = current_container;
3064           }
3065         ;       
3066
3067 opt_partial
3068         : /* empty */
3069           { $$ = (bool) false; }
3070         | PARTIAL
3071           { $$ = (bool) true; }
3072         ;
3073
3074 opt_modifiers
3075         : /* empty */           { $$ = (int) 0; }
3076         | modifiers
3077         ;
3078
3079 modifiers
3080         : modifier
3081         | modifiers modifier
3082           { 
3083                 int m1 = (int) $1;
3084                 int m2 = (int) $2;
3085
3086                 if ((m1 & m2) != 0) {
3087                         Location l = lexer.Location;
3088                         Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3089                 }
3090                 $$ = (int) (m1 | m2);
3091           }
3092         ;
3093
3094 modifier
3095         : NEW                   { $$ = Modifiers.NEW; }
3096         | PUBLIC                { $$ = Modifiers.PUBLIC; }
3097         | PROTECTED             { $$ = Modifiers.PROTECTED; }
3098         | INTERNAL              { $$ = Modifiers.INTERNAL; }
3099         | PRIVATE               { $$ = Modifiers.PRIVATE; }
3100         | ABSTRACT              { $$ = Modifiers.ABSTRACT; }
3101         | SEALED                { $$ = Modifiers.SEALED; }
3102         | STATIC                { $$ = Modifiers.STATIC; }
3103         | READONLY              { $$ = Modifiers.READONLY; }
3104         | VIRTUAL               { $$ = Modifiers.VIRTUAL; }
3105         | OVERRIDE              { $$ = Modifiers.OVERRIDE; }
3106         | EXTERN                { $$ = Modifiers.EXTERN; }
3107         | VOLATILE              { $$ = Modifiers.VOLATILE; }
3108         | UNSAFE                { $$ = Modifiers.UNSAFE; }
3109         ;
3110
3111 opt_class_base
3112         : /* empty */           { $$ = null; }
3113         | class_base            { $$ = $1;   }
3114         ;
3115
3116 class_base
3117         : COLON type_list { $$ = $2; }
3118         ;
3119
3120 //
3121 // Statements (8.2)
3122 //
3123
3124 //
3125 // A block is "contained" on the following places:
3126 //      method_body
3127 //      property_declaration as part of the accessor body (get/set)
3128 //      operator_declaration
3129 //      constructor_declaration
3130 //      destructor_declaration
3131 //      event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3132 //      
3133 block
3134         : OPEN_BRACE 
3135           {
3136                 if (current_block == null){
3137                         current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, lexer.Location);
3138                         top_current_block = current_block;
3139                 } else {
3140                 current_block = new Block (current_block, current_local_parameters,
3141                                            lexer.Location, Location.Null);
3142                 }
3143           } 
3144           opt_statement_list CLOSE_BRACE 
3145           { 
3146                 while (current_block.Implicit)
3147                         current_block = current_block.Parent;
3148                 $$ = current_block;
3149                 current_block.SetEndLocation (lexer.Location);
3150                 current_block = current_block.Parent;
3151                 if (current_block == null)
3152                         top_current_block = null;
3153           }
3154         ;
3155
3156 opt_statement_list
3157         : /* empty */
3158         | statement_list 
3159         ;
3160
3161 statement_list
3162         : statement
3163         | statement_list statement
3164         ;
3165
3166 statement
3167         : declaration_statement
3168           {
3169                 if ($1 != null && (Block) $1 != current_block){
3170                         current_block.AddStatement ((Statement) $1);
3171                         current_block = (Block) $1;
3172                 }
3173           }
3174         | valid_declaration_statement
3175           {
3176                 current_block.AddStatement ((Statement) $1);
3177           }
3178         | labeled_statement
3179         ;
3180
3181 valid_declaration_statement
3182         : block
3183         | empty_statement
3184         | expression_statement
3185         | selection_statement
3186         | iteration_statement
3187         | jump_statement                  
3188         | try_statement
3189         | checked_statement
3190         | unchecked_statement
3191         | lock_statement
3192         | using_statement
3193         | unsafe_statement
3194         | fixed_statement
3195         ;
3196
3197 embedded_statement
3198         : valid_declaration_statement
3199         | declaration_statement
3200           {
3201                   Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3202                   $$ = null;
3203           }
3204         | labeled_statement
3205           {
3206                   Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3207                   $$ = null;
3208           }
3209         ;
3210
3211 empty_statement
3212         : SEMICOLON
3213           {
3214                   $$ = EmptyStatement.Value;
3215           }
3216         ;
3217
3218 labeled_statement
3219         : IDENTIFIER COLON 
3220           {
3221                 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3222
3223                 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3224                         current_block.AddStatement (labeled);
3225           }
3226           statement
3227         ;
3228
3229 declaration_statement
3230         : local_variable_declaration SEMICOLON
3231           {
3232                 if ($1 != null){
3233                         DictionaryEntry de = (DictionaryEntry) $1;
3234
3235                         $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3236                 }
3237           }
3238
3239         | local_constant_declaration SEMICOLON
3240           {
3241                 if ($1 != null){
3242                         DictionaryEntry de = (DictionaryEntry) $1;
3243
3244                         $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3245                 }
3246           }
3247         ;
3248
3249 /* 
3250  * The following is from Rhys' grammar:
3251  * > Types in local variable declarations must be recognized as 
3252  * > expressions to prevent reduce/reduce errors in the grammar.
3253  * > The expressions are converted into types during semantic analysis.
3254  */
3255 local_variable_type
3256         : primary_expression opt_rank_specifier
3257           { 
3258                 // FIXME: Do something smart here regarding the composition of the type.
3259
3260                 // Ok, the above "primary_expression" is there to get rid of
3261                 // both reduce/reduce and shift/reduces in the grammar, it should
3262                 // really just be "type_name".  If you use type_name, a reduce/reduce
3263                 // creeps up.  If you use namespace_or_type_name (which is all we need
3264                 // really) two shift/reduces appear.
3265                 // 
3266
3267                 // So the super-trick is that primary_expression
3268                 // can only be either a SimpleName or a MemberAccess. 
3269                 // The MemberAccess case arises when you have a fully qualified type-name like :
3270                 // Foo.Bar.Blah i;
3271                 // SimpleName is when you have
3272                 // Blah i;
3273                   
3274                 Expression expr = (Expression) $1;  
3275                 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3276                         Error_ExpectingTypeName (lexer.Location, expr);
3277                         $$ = null;
3278                 } else {
3279                         //
3280                         // So we extract the string corresponding to the SimpleName
3281                         // or MemberAccess
3282                         // 
3283
3284                         if ((string) $2 == "")
3285                                 $$ = $1;
3286                         else
3287                                 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3288                 }
3289           }
3290         | builtin_types opt_rank_specifier
3291           {
3292                 if ((string) $2 == "")
3293                         $$ = $1;
3294                 else
3295                         $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3296           }
3297         ;
3298
3299 local_variable_pointer_type
3300         : primary_expression STAR
3301           {
3302                 Expression expr = (Expression) $1;  
3303                 Location l = lexer.Location;
3304
3305                 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3306                         Error_ExpectingTypeName (l, expr);
3307
3308                         $$ = null;
3309                 } else 
3310                         $$ = new ComposedCast ((Expression) $1, "*", l);
3311           }
3312         | builtin_types STAR
3313           {
3314                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3315           }
3316         | VOID STAR
3317           {
3318                 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3319           }
3320         | local_variable_pointer_type STAR
3321           {
3322                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3323           }
3324         ;
3325
3326 local_variable_declaration
3327         : local_variable_type variable_declarators
3328           {
3329                 if ($1 != null)
3330                         $$ = new DictionaryEntry ($1, $2);
3331                 else
3332                         $$ = null;
3333           }
3334         | local_variable_pointer_type opt_rank_specifier variable_declarators
3335         {
3336                 if ($1 != null){
3337                         Expression t;
3338
3339                         if ((string) $2 == "")
3340                                 t = (Expression) $1;
3341                         else
3342                                 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3343                         $$ = new DictionaryEntry (t, $3);
3344                 } else 
3345                         $$ = null;
3346         }
3347         ;
3348
3349 local_constant_declaration
3350         : CONST local_variable_type constant_declarators
3351           {
3352                 if ($2 != null)
3353                         $$ = new DictionaryEntry ($2, $3);
3354                 else
3355                         $$ = null;
3356           }
3357         ;
3358
3359 expression_statement
3360         : statement_expression SEMICOLON
3361           {
3362                 $$ = $1;
3363           }
3364         ;
3365
3366         //
3367         // We have to do the wrapping here and not in the case above,
3368         // because statement_expression is used for example in for_statement
3369         //
3370 statement_expression
3371         : invocation_expression         { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3372         | object_creation_expression    { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3373         | assignment_expression         { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3374         | post_increment_expression     { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3375         | post_decrement_expression     { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3376         | pre_increment_expression      { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3377         | pre_decrement_expression      { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3378         | error {
3379                 Report.Error (1002, lexer.Location, "Expecting `;'");
3380                 $$ = null;
3381           }
3382         ;
3383
3384 object_creation_expression
3385         : object_or_delegate_creation_expression
3386           { note ("complain if this is a delegate maybe?"); } 
3387         ;
3388
3389 selection_statement
3390         : if_statement
3391         | switch_statement
3392         ; 
3393
3394 if_statement
3395         : if_statement_open if_statement_rest
3396           {
3397                 $$ = $2;
3398           }
3399         ;
3400
3401 if_statement_open
3402         : IF OPEN_PARENS 
3403           {
3404                 oob_stack.Push (lexer.Location);
3405           }
3406         ;
3407
3408 if_statement_rest
3409         : boolean_expression CLOSE_PARENS 
3410           embedded_statement
3411           { 
3412                 Location l = (Location) oob_stack.Pop ();
3413
3414                 $$ = new If ((Expression) $1, (Statement) $3, l);
3415
3416                 if (RootContext.WarningLevel >= 3){
3417                         if ($3 == EmptyStatement.Value)
3418                                 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3419                 }
3420
3421           }
3422         | boolean_expression CLOSE_PARENS
3423           embedded_statement ELSE embedded_statement
3424           {
3425                 Location l = (Location) oob_stack.Pop ();
3426
3427                 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
3428           }
3429         ;
3430
3431 switch_statement
3432         : SWITCH OPEN_PARENS 
3433           { 
3434                 oob_stack.Push (lexer.Location);
3435                 switch_stack.Push (current_block);
3436           }
3437           expression CLOSE_PARENS 
3438           switch_block
3439           {
3440                 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
3441                 current_block = (Block) switch_stack.Pop ();
3442           }
3443         ;
3444
3445 switch_block
3446         : OPEN_BRACE
3447           opt_switch_sections
3448           CLOSE_BRACE
3449           {
3450                 $$ = $2;
3451           }
3452         ;
3453
3454 opt_switch_sections
3455         : /* empty */           
3456           {
3457                 Report.Error (1522, lexer.Location, "Empty switch block"); 
3458           }
3459         | switch_sections
3460         ;
3461
3462 switch_sections
3463         : switch_section 
3464           {
3465                 ArrayList sections = new ArrayList (4);
3466
3467                 sections.Add ($1);
3468                 $$ = sections;
3469           }
3470         | switch_sections switch_section
3471           {
3472                 ArrayList sections = (ArrayList) $1;
3473
3474                 sections.Add ($2);
3475                 $$ = sections;
3476           }
3477         ;
3478
3479 switch_section
3480         : switch_labels
3481           {
3482                 current_block = current_block.CreateSwitchBlock (lexer.Location);
3483           }
3484           statement_list 
3485           {
3486                 Block topmost = current_block;
3487
3488                 while (topmost.Implicit)
3489                         topmost = topmost.Parent;
3490                 $$ = new SwitchSection ((ArrayList) $1, topmost);
3491           }
3492         ;
3493
3494 switch_labels
3495         : switch_label 
3496           {
3497                 ArrayList labels = new ArrayList (4);
3498
3499                 labels.Add ($1);
3500                 $$ = labels;
3501           }
3502         | switch_labels switch_label 
3503           {
3504                 ArrayList labels = (ArrayList) ($1);
3505                 labels.Add ($2);
3506
3507                 $$ = labels;
3508           }
3509         ;
3510
3511 switch_label
3512         : CASE constant_expression COLON        { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3513         | DEFAULT COLON                         { $$ = new SwitchLabel (null, lexer.Location); }
3514         | error {
3515                 Report.Error (
3516                         1523, lexer.Location, 
3517                         "The keyword case or default must precede code in switch block");
3518           }
3519         ;
3520
3521 iteration_statement
3522         : while_statement
3523         | do_statement
3524         | for_statement
3525         | foreach_statement
3526         ;
3527
3528 while_statement
3529         : WHILE OPEN_PARENS 
3530         {
3531                 oob_stack.Push (lexer.Location);
3532         }
3533         boolean_expression CLOSE_PARENS embedded_statement
3534         {
3535                 Location l = (Location) oob_stack.Pop ();
3536                 $$ = new While ((Expression) $4, (Statement) $6, l);
3537         
3538                 if (RootContext.WarningLevel >= 3){
3539                         if ($6 == EmptyStatement.Value)
3540                                 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3541                 }
3542         }
3543         ;
3544
3545 do_statement
3546         : DO embedded_statement 
3547           WHILE OPEN_PARENS {
3548                 oob_stack.Push (lexer.Location);
3549           }
3550           boolean_expression CLOSE_PARENS SEMICOLON
3551           {
3552                 Location l = (Location) oob_stack.Pop ();
3553
3554                 $$ = new Do ((Statement) $2, (Expression) $6, l);
3555           }
3556         ;
3557
3558 for_statement
3559         : FOR OPEN_PARENS 
3560           opt_for_initializer SEMICOLON
3561           {
3562                 Block assign_block = new Block (current_block);
3563                 current_block = assign_block;
3564
3565                 if ($3 is DictionaryEntry){
3566                         DictionaryEntry de = (DictionaryEntry) $3;
3567                         
3568                         Expression type = (Expression) de.Key;
3569                         ArrayList var_declarators = (ArrayList) de.Value;
3570
3571                         foreach (VariableDeclaration decl in var_declarators){
3572
3573                                 LocalInfo vi;
3574
3575                                 vi = current_block.AddVariable (
3576                                         type, decl.identifier, current_local_parameters, decl.Location);
3577                                 if (vi == null)
3578                                         continue;
3579
3580                                 Location l = lexer.Location;
3581                                 Expression expr;
3582                                 if (decl.expression_or_array_initializer is Expression){
3583                                         expr = (Expression) decl.expression_or_array_initializer;
3584                                 } else if (decl.expression_or_array_initializer == null) {
3585                                         expr = null;
3586                                 } else {
3587                                         ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3588                                         expr = new ArrayCreation (type, "", init, decl.Location);
3589                                 }
3590                                         
3591                                 LocalVariableReference var;
3592                                 var = new LocalVariableReference (assign_block, decl.identifier, l);
3593
3594                                 if (expr != null) {
3595                                         Assign a = new Assign (var, expr, decl.Location);
3596                                         
3597                                         assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3598                                 }
3599                         }
3600                         
3601                         $3 = null;
3602                 } 
3603                 oob_stack.Push (lexer.Location);
3604           } 
3605           opt_for_condition SEMICOLON
3606           opt_for_iterator CLOSE_PARENS 
3607           embedded_statement
3608           {
3609                 Location l = (Location) oob_stack.Pop ();
3610
3611                 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3612
3613                 if (RootContext.WarningLevel >= 3){
3614                         if ($10 == EmptyStatement.Value)
3615                                 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3616                 }
3617
3618                 current_block.AddStatement (f);
3619                 while (current_block.Implicit)
3620                         current_block = current_block.Parent;
3621                 $$ = current_block;
3622                 current_block = current_block.Parent;
3623           }
3624         ;
3625
3626 opt_for_initializer
3627         : /* empty */           { $$ = EmptyStatement.Value; }
3628         | for_initializer       
3629         ;
3630
3631 for_initializer
3632         : local_variable_declaration
3633         | statement_expression_list
3634         ;
3635
3636 opt_for_condition
3637         : /* empty */           { $$ = null; }
3638         | boolean_expression
3639         ;
3640
3641 opt_for_iterator
3642         : /* empty */           { $$ = EmptyStatement.Value; }
3643         | for_iterator
3644         ;
3645
3646 for_iterator
3647         : statement_expression_list
3648         ;
3649
3650 statement_expression_list
3651         : statement_expression  
3652           {
3653                 // CHANGE: was `null'
3654                 Block b = new Block (current_block, Block.Flags.Implicit);   
3655
3656                 b.AddStatement ((Statement) $1);
3657                 $$ = b;
3658           }
3659         | statement_expression_list COMMA statement_expression
3660           {
3661                 Block b = (Block) $1;
3662
3663                 b.AddStatement ((Statement) $3);
3664                 $$ = $1;
3665           }
3666         ;
3667
3668 foreach_statement
3669         : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
3670         {
3671                 Report.Error (230, lexer.Location, "Type and identifier are both required in a foreach statement");
3672                 $$ = null;
3673         }
3674         | FOREACH OPEN_PARENS type IDENTIFIER IN 
3675           {
3676                 oob_stack.Push (lexer.Location);
3677           }
3678           expression CLOSE_PARENS 
3679           {
3680                 oob_stack.Push (current_block);
3681
3682                 Block foreach_block = new Block (current_block);
3683                 LocalVariableReference v = null;
3684                 Location l = lexer.Location;
3685                 LocalInfo vi;
3686
3687                 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3688                 if (vi != null) {
3689                         vi.ReadOnly = true;
3690
3691                         // Get a writable reference to this read-only variable.
3692                         v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3693                 }
3694                 current_block = foreach_block;
3695
3696                 oob_stack.Push (v);
3697                 oob_stack.Push (current_block);
3698           } 
3699           embedded_statement 
3700           {
3701                 Block foreach_block = (Block) oob_stack.Pop ();
3702                 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3703                 Block prev_block = (Block) oob_stack.Pop ();
3704                 Location l = (Location) oob_stack.Pop ();
3705
3706                 current_block = prev_block;
3707
3708                 if (v != null) {
3709                         Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3710                         foreach_block.AddStatement (f);
3711                 }
3712
3713                 $$ = foreach_block;
3714           }
3715         ;
3716
3717 jump_statement
3718         : break_statement
3719         | continue_statement
3720         | goto_statement
3721         | return_statement
3722         | throw_statement
3723         | yield_statement
3724         ;
3725
3726 break_statement
3727         : BREAK SEMICOLON
3728           {
3729                 $$ = new Break (lexer.Location);
3730           }
3731         ;
3732
3733 continue_statement
3734         : CONTINUE SEMICOLON
3735           {
3736                 $$ = new Continue (lexer.Location);
3737           }
3738         ;
3739
3740 goto_statement
3741         : GOTO IDENTIFIER SEMICOLON 
3742           {
3743                 $$ = new Goto (current_block, (string) $2, lexer.Location);
3744           }
3745         | GOTO CASE constant_expression SEMICOLON
3746           {
3747                 $$ = new GotoCase ((Expression) $3, lexer.Location);
3748           }
3749         | GOTO DEFAULT SEMICOLON 
3750           {
3751                 $$ = new GotoDefault (lexer.Location);
3752           }
3753         ; 
3754
3755 return_statement
3756         : RETURN opt_expression SEMICOLON
3757           {
3758                 $$ = new Return ((Expression) $2, lexer.Location);
3759           }
3760         ;
3761
3762 throw_statement
3763         : THROW opt_expression SEMICOLON
3764           {
3765                 $$ = new Throw ((Expression) $2, lexer.Location);
3766           }
3767         ;
3768
3769 yield_statement 
3770         : IDENTIFIER RETURN expression SEMICOLON
3771           {
3772                 string s = (string) $1;
3773                 if (s != "yield"){
3774                         Report.Error (1003, lexer.Location, "; expected");
3775                         $$ = null;
3776                 }
3777                 if (RootContext.Version == LanguageVersion.ISO_1){
3778                         Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
3779                         $$ = null;
3780                 }
3781                 if (iterator_container == null){
3782                         Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3783                         $$ = null;
3784                 } else {
3785                         iterator_container.SetYields ();
3786                         $$ = new Yield ((Expression) $3, lexer.Location); 
3787                 }
3788           }
3789         | IDENTIFIER BREAK SEMICOLON
3790           {
3791                 string s = (string) $1;
3792                 if (s != "yield"){
3793                         Report.Error (1003, lexer.Location, "; expected");
3794                         $$ = null;
3795                 }
3796                 if (RootContext.Version == LanguageVersion.ISO_1){
3797                         Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
3798                         $$ = null;
3799                 }
3800                 if (iterator_container == null){
3801                         Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3802                         $$ = null;
3803                 } else {
3804                         iterator_container.SetYields ();
3805                         $$ = new YieldBreak (lexer.Location);
3806                 }
3807           }
3808         ;
3809
3810 opt_expression
3811         : /* empty */
3812         | expression
3813         ;
3814
3815 try_statement
3816         : TRY block catch_clauses 
3817         {
3818                 Catch g = null;
3819                 
3820                 ArrayList c = (ArrayList)$3;
3821                 for (int i = 0; i < c.Count; ++i) {
3822                         Catch cc = (Catch) c [i];
3823                         if (cc.IsGeneral) {
3824                                 if (i != c.Count - 1)
3825                                         Report.Error (1017, cc.loc, "Empty catch block must be the last in a series of catch blocks");
3826                                 g = cc;
3827                                 c.RemoveAt (i);
3828                                 i--;
3829                         }
3830                 }
3831
3832                 // Now s contains the list of specific catch clauses
3833                 // and g contains the general one.
3834                 
3835                 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
3836         }
3837         | TRY block opt_catch_clauses FINALLY block
3838           {
3839                 Catch g = null;
3840                 ArrayList s = new ArrayList (4);
3841                 ArrayList catch_list = (ArrayList) $3;
3842
3843                 if (catch_list != null){
3844                         foreach (Catch cc in catch_list) {
3845                                 if (cc.IsGeneral)
3846                                         g = cc;
3847                                 else
3848                                         s.Add (cc);
3849                         }
3850                 }
3851
3852                 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
3853           }
3854         | TRY block error 
3855           {
3856                 Report.Error (1524, lexer.Location, "Expected catch or finally");
3857           }
3858         ;
3859
3860 opt_catch_clauses
3861         : /* empty */  { $$ = null; }
3862         | catch_clauses
3863         ;
3864
3865 catch_clauses
3866         : catch_clause 
3867           {
3868                 ArrayList l = new ArrayList (4);
3869
3870                 l.Add ($1);
3871                 $$ = l;
3872           }
3873         | catch_clauses catch_clause
3874           {
3875                 ArrayList l = (ArrayList) $1;
3876
3877                 l.Add ($2);
3878                 $$ = l;
3879           }
3880         ;
3881
3882 opt_identifier
3883         : /* empty */   { $$ = null; }
3884         | IDENTIFIER
3885         ;
3886
3887 catch_clause 
3888         : CATCH opt_catch_args 
3889         {
3890                 Expression type = null;
3891                 string id = null;
3892                 
3893                 if ($2 != null) {
3894                         DictionaryEntry cc = (DictionaryEntry) $2;
3895                         type = (Expression) cc.Key;
3896                         id   = (string) cc.Value;
3897
3898                         if (id != null){
3899                                 ArrayList one = new ArrayList (4);
3900                                 Location loc = lexer.Location;
3901
3902                                 one.Add (new VariableDeclaration (id, null, loc));
3903
3904                                 $1 = current_block;
3905                                 current_block = new Block (current_block);
3906                                 Block b = declare_local_variables (type, one, loc);
3907                                 current_block = b;
3908                         }
3909                 }
3910         } block {
3911                 Expression type = null;
3912                 string id = null;
3913
3914                 if ($2 != null){
3915                         DictionaryEntry cc = (DictionaryEntry) $2;
3916                         type = (Expression) cc.Key;
3917                         id   = (string) cc.Value;
3918
3919                         if ($1 != null){
3920                                 //
3921                                 // FIXME: I can change this for an assignment.
3922                                 //
3923                                 while (current_block != (Block) $1)
3924                                         current_block = current_block.Parent;
3925                         }
3926                 }
3927
3928
3929                 $$ = new Catch (type, id , (Block) $4, ((Block) $4).loc);
3930         }
3931         ;
3932
3933 opt_catch_args
3934         : /* empty */ { $$ = null; }
3935         | catch_args
3936         ;         
3937
3938 catch_args 
3939         : OPEN_PARENS type opt_identifier CLOSE_PARENS 
3940         {
3941                 $$ = new DictionaryEntry ($2, $3);
3942         }
3943         ;
3944
3945 checked_statement
3946         : CHECKED block
3947           {
3948                 $$ = new Checked ((Block) $2);
3949           }
3950         ;
3951
3952 unchecked_statement
3953         : UNCHECKED block
3954           {
3955                 $$ = new Unchecked ((Block) $2);
3956           }
3957         ;
3958
3959 unsafe_statement
3960         : UNSAFE 
3961         {
3962                 if (!RootContext.Unsafe){
3963                         Report.Error (227, lexer.Location, 
3964                                 "Unsafe code can only be used if --unsafe is used");
3965                 }
3966         } block {
3967                 $$ = new Unsafe ((Block) $3);
3968         }
3969         ;
3970
3971 fixed_statement
3972         : FIXED OPEN_PARENS 
3973           type fixed_pointer_declarators 
3974           CLOSE_PARENS 
3975           {
3976                 ArrayList list = (ArrayList) $4;
3977                 Expression type = (Expression) $3;
3978                 Location l = lexer.Location;
3979                 int top = list.Count;
3980
3981                 Block assign_block = new Block (current_block);
3982                 current_block = assign_block;
3983
3984                 for (int i = 0; i < top; i++){
3985                         Pair p = (Pair) list [i];
3986                         LocalInfo v;
3987
3988                         v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3989                         if (v == null)
3990                                 continue;
3991
3992                         v.ReadOnly = true;
3993                         v.Pinned = true;
3994                         p.First = v;
3995                         list [i] = p;
3996                 }
3997
3998                 oob_stack.Push (l);
3999           }
4000           embedded_statement 
4001           {
4002                 Location l = (Location) oob_stack.Pop ();
4003
4004                 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4005
4006                 if (RootContext.WarningLevel >= 3){
4007                         if ($7 == EmptyStatement.Value)
4008                                 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4009                 }
4010
4011                 current_block.AddStatement (f);
4012                 while (current_block.Implicit)
4013                         current_block = current_block.Parent;
4014                 $$ = current_block;
4015                 current_block = current_block.Parent;
4016           }
4017         ;
4018
4019 fixed_pointer_declarators
4020         : fixed_pointer_declarator      { 
4021                 ArrayList declarators = new ArrayList (4);
4022                 if ($1 != null)
4023                         declarators.Add ($1);
4024                 $$ = declarators;
4025           }
4026         | fixed_pointer_declarators COMMA fixed_pointer_declarator
4027           {
4028                 ArrayList declarators = (ArrayList) $1;
4029                 if ($3 != null)
4030                         declarators.Add ($3);
4031                 $$ = declarators;
4032           }
4033         ;
4034
4035 fixed_pointer_declarator
4036         : IDENTIFIER ASSIGN expression
4037           {     
4038                 $$ = new Pair ($1, $3);
4039           }
4040         | IDENTIFIER
4041           {
4042                 Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
4043                 $$ = null;
4044           }
4045         ;
4046
4047 lock_statement
4048         : LOCK OPEN_PARENS expression CLOSE_PARENS 
4049           {
4050                 //
4051           } 
4052           embedded_statement
4053           {
4054                 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4055           }
4056         ;
4057
4058 using_statement
4059         : USING OPEN_PARENS resource_acquisition CLOSE_PARENS 
4060           {
4061                 Block assign_block = new Block (current_block);
4062                 current_block = assign_block;
4063
4064                 oob_stack.Push (lexer.Location);
4065                 
4066                 if ($3 is DictionaryEntry){
4067                         DictionaryEntry de = (DictionaryEntry) $3;
4068                         Location l = lexer.Location;
4069
4070                         Expression type = (Expression) de.Key;
4071                         ArrayList var_declarators = (ArrayList) de.Value;
4072
4073                         ArrayList vars = new ArrayList (4);
4074
4075                         foreach (VariableDeclaration decl in var_declarators){
4076
4077                                 LocalInfo vi    = current_block.AddVariable (
4078                                         type, decl.identifier, 
4079                                         current_local_parameters, decl.Location);
4080                                 if (vi == null)
4081                                         continue;
4082                                 vi.ReadOnly = true;
4083
4084                                 Expression expr;
4085                                 if (decl.expression_or_array_initializer is Expression){
4086                                         expr = (Expression) decl.expression_or_array_initializer;
4087                                 } else {
4088                                         ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4089                                         if (init == null) {
4090                                                 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4091                                         }
4092                                         
4093                                         expr = new ArrayCreation (type, "", init, decl.Location);
4094                                 }
4095
4096                                 LocalVariableReference var;
4097
4098                                 // Get a writable reference to this read-only variable.
4099                                 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4100
4101                                 // This is so that it is not a warning on using variables
4102                                 vi.Used = true;
4103
4104                                 vars.Add (new DictionaryEntry (var, expr));                             
4105
4106                                 // Assign a = new Assign (var, expr, decl.Location);
4107                                 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4108                         }
4109                         $3 = new DictionaryEntry (type, vars);
4110                  }
4111           } 
4112           embedded_statement
4113           {
4114                 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
4115                 current_block.AddStatement (u);
4116                 while (current_block.Implicit)
4117                         current_block = current_block.Parent;
4118                 $$ = current_block;
4119                 current_block = current_block.Parent;
4120           }
4121         ; 
4122
4123 resource_acquisition
4124         : local_variable_declaration
4125         | expression
4126         ;
4127
4128 %%
4129
4130 // <summary>
4131 //   A class used to pass around variable declarations and constants
4132 // </summary>
4133 public class VariableDeclaration {
4134         public string identifier;
4135         public object expression_or_array_initializer;
4136         public Location Location;
4137         public Attributes OptAttributes;
4138
4139         public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4140         {
4141                 this.identifier = id;
4142                 this.expression_or_array_initializer = eoai;
4143                 this.Location = l;
4144                 this.OptAttributes = opt_attrs;
4145         }
4146
4147         public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4148         {
4149         }
4150 }
4151
4152 /// <summary>
4153 ///  Used to pass around interface property information
4154 /// </summary>
4155 public class InterfaceAccessorInfo {
4156
4157         public readonly Accessor Get, Set;
4158
4159         public InterfaceAccessorInfo (bool has_get, bool has_set,
4160                                       Attributes get_attrs, Attributes set_attrs, Location get_loc, Location set_loc)
4161         {
4162                 if (has_get)
4163                         Get = new Accessor (null, 0, get_attrs, get_loc);
4164                 if (has_set)
4165                         Set = new Accessor (null, 0, set_attrs, set_loc);
4166         }
4167 }
4168
4169
4170 // <summary>
4171 //   A class used to hold info about an indexer declarator
4172 // </summary>
4173 public class IndexerDeclaration {
4174         public Expression type;
4175         public MemberName interface_type;
4176         public Parameters param_list;
4177
4178         public IndexerDeclaration (Expression type, MemberName interface_type,
4179                                    Parameters param_list)
4180         {
4181                 this.type = type;
4182                 this.interface_type = interface_type;
4183                 this.param_list = param_list;
4184         }
4185 }
4186
4187 //
4188 // We use this when we do not have an object in advance that is an IIteratorContainer
4189 //
4190 public class SimpleIteratorContainer : IIteratorContainer {
4191         public bool Yields;
4192
4193         public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4194
4195         //
4196         // Reset and return
4197         //
4198         public static SimpleIteratorContainer GetSimple () { 
4199                 Simple.Yields = false;
4200                 return Simple;
4201         }
4202
4203         public void SetYields () { Yields = true; } 
4204 }
4205
4206 // <summary>
4207 //  A class used to hold info about an operator declarator
4208 // </summary>
4209 public class OperatorDeclaration {
4210         public Operator.OpType optype;
4211         public Expression ret_type, arg1type, arg2type;
4212         public string arg1name, arg2name;
4213         public Location location;
4214
4215         public OperatorDeclaration (Operator.OpType op, Expression ret_type, 
4216                                     Expression arg1type, string arg1name,
4217                                     Expression arg2type, string arg2name, Location location)
4218         {
4219                 optype = op;
4220                 this.ret_type = ret_type;
4221                 this.arg1type = arg1type;
4222                 this.arg1name = arg1name;
4223                 this.arg2type = arg2type;
4224                 this.arg2name = arg2name;
4225                 this.location = location;
4226         }
4227
4228 }
4229
4230 void Error_ExpectingTypeName (Location l, Expression expr)
4231 {
4232         if (expr is Invocation){
4233                 Report.Error (1002, l, "; expected");
4234         } else {
4235                 Report.Error (-1, l, "Invalid Type definition");
4236         }
4237 }
4238
4239 // <summary>
4240 //   Given the @class_name name, it creates a fully qualified name
4241 //   based on the containing declaration space
4242 // </summary>
4243 MemberName
4244 MakeName (MemberName class_name)
4245 {
4246         string ns = current_namespace.FullName;
4247
4248         if (current_container.Name == ""){
4249                 if (ns != "")
4250                         return new MemberName (new MemberName (ns), class_name);
4251                 else
4252                         return class_name;
4253         } else {
4254                 return new MemberName (current_container.MemberName, class_name);
4255         }
4256 }
4257
4258 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4259 {
4260         Block implicit_block;
4261         ArrayList inits = null;
4262
4263         //
4264         // We use the `Used' property to check whether statements
4265         // have been added to the current block.  If so, we need
4266         // to create another block to contain the new declaration
4267         // otherwise, as an optimization, we use the same block to
4268         // add the declaration.
4269         //
4270         // FIXME: A further optimization is to check if the statements
4271         // that were added were added as part of the initialization
4272         // below.  In which case, no other statements have been executed
4273         // and we might be able to reduce the number of blocks for
4274         // situations like this:
4275         //
4276         // int j = 1;  int k = j + 1;
4277         //
4278         if (current_block.Used)
4279                 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4280         else
4281                 implicit_block = current_block;
4282
4283         foreach (VariableDeclaration decl in variable_declarators){
4284
4285                 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4286                         if (decl.expression_or_array_initializer != null){
4287                                 if (inits == null)
4288                                         inits = new ArrayList (4);
4289                                 inits.Add (decl);
4290                         }
4291                 }
4292         }
4293
4294         if (inits == null)
4295                 return implicit_block;
4296
4297         foreach (VariableDeclaration decl in inits){
4298                 Assign assign;
4299                 Expression expr;
4300                 
4301                 if (decl.expression_or_array_initializer is Expression){
4302                         expr = (Expression) decl.expression_or_array_initializer;
4303
4304                 } else {
4305                         ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4306                         
4307                         expr = new ArrayCreation (type, "", init, decl.Location);
4308                 }
4309
4310                 LocalVariableReference var;
4311                 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4312
4313                 assign = new Assign (var, expr, decl.Location);
4314
4315                 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4316         }
4317         
4318         return implicit_block;
4319 }
4320
4321 Block declare_local_constants (Expression type, ArrayList declarators)
4322 {
4323         Block implicit_block;
4324
4325         if (current_block.Used)
4326                 implicit_block = new Block (current_block, Block.Flags.Implicit);
4327         else
4328                 implicit_block = current_block;
4329
4330         foreach (VariableDeclaration decl in declarators){
4331                 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4332                                                   current_local_parameters, decl.Location);
4333         }
4334         
4335         return implicit_block;
4336 }
4337
4338 void CheckAttributeTarget (string a)
4339 {
4340         switch (a) {
4341
4342         case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4343                 return;
4344                 
4345         default :
4346                 Location l = lexer.Location;
4347                 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4348                 break;
4349         }
4350
4351 }
4352
4353 void CheckUnaryOperator (Operator.OpType op)
4354 {
4355         switch (op) {
4356                 
4357         case Operator.OpType.LogicalNot: 
4358         case Operator.OpType.OnesComplement: 
4359         case Operator.OpType.Increment:
4360         case Operator.OpType.Decrement:
4361         case Operator.OpType.True: 
4362         case Operator.OpType.False: 
4363         case Operator.OpType.Addition: 
4364         case Operator.OpType.Subtraction:
4365                 
4366                 break;
4367                 
4368         default :
4369                 Location l = lexer.Location;
4370                 Report.Error (1019, l, "Overloadable unary operator expected"); 
4371                 break;
4372                 
4373         }
4374 }
4375
4376 void CheckBinaryOperator (Operator.OpType op)
4377 {
4378         switch (op) {
4379                 
4380         case Operator.OpType.Addition: 
4381         case Operator.OpType.Subtraction: 
4382         case Operator.OpType.Multiply:
4383         case Operator.OpType.Division:
4384         case Operator.OpType.Modulus: 
4385         case Operator.OpType.BitwiseAnd: 
4386         case Operator.OpType.BitwiseOr:
4387         case Operator.OpType.ExclusiveOr: 
4388         case Operator.OpType.LeftShift: 
4389         case Operator.OpType.RightShift:
4390         case Operator.OpType.Equality: 
4391         case Operator.OpType.Inequality:
4392         case Operator.OpType.GreaterThan: 
4393         case Operator.OpType.LessThan: 
4394         case Operator.OpType.GreaterThanOrEqual:
4395         case Operator.OpType.LessThanOrEqual:
4396                 break;
4397                 
4398         default :
4399                 Location l = lexer.Location;
4400                 Report.Error (1020, l, "Overloadable binary operator expected");
4401                 break;
4402         }
4403         
4404 }
4405
4406 void syntax_error (Location l, string msg)
4407 {
4408         Report.Error (1003, l, "Syntax error, " + msg);
4409 }
4410
4411 void output (string s)
4412 {
4413         Console.WriteLine (s);
4414 }
4415
4416 void note (string s)
4417 {
4418         // Used to put annotations
4419 }
4420
4421 Tokenizer lexer;
4422
4423 public Tokenizer Lexer {
4424         get {
4425                 return lexer;
4426         }
4427 }                  
4428
4429 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4430 {
4431         current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4432         this.name = file.Name;
4433         this.file = file;
4434         current_container = RootContext.Tree.Types;
4435         current_container.NamespaceEntry = current_namespace;
4436         oob_stack = new Stack ();
4437         switch_stack = new Stack ();
4438
4439         lexer = new Tokenizer (reader, file, defines);
4440 }
4441
4442 public void parse ()
4443 {
4444         try {
4445                 if (yacc_verbose_flag)
4446                         yyparse (lexer, new yydebug.yyDebugSimple ());
4447                 else
4448                         yyparse (lexer);
4449                 Tokenizer tokenizer = lexer as Tokenizer;
4450                 tokenizer.cleanup ();           
4451         } catch (Exception e){
4452                 // 
4453                 // Removed for production use, use parser verbose to get the output.
4454                 //
4455                 // Console.WriteLine (e);
4456                 Report.Error (-25, lexer.Location, "Parsing error");
4457                 if (Driver.parser_verbose)
4458                         Console.WriteLine (e);
4459         }
4460 }
4461
4462 void CheckToken (int error, int yyToken, string msg)
4463 {
4464         if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4465                 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4466                 return;
4467         }               
4468         Report.Error (error, lexer.Location, msg);
4469 }
4470
4471 void CheckIdentifierToken (int yyToken)
4472 {
4473         CheckToken (1041, yyToken, "Identifier expected");
4474 }
4475
4476 /* end end end */
4477 }