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