In mcs:
[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         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2567           {
2568                 ArrayList args = new ArrayList (1);
2569                 args.Add ($4);
2570                 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2571           }
2572         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2573           {
2574                 ArrayList args = ((ArrayList) $4);
2575                 args.Add ($6);
2576                 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2577           }
2578         ;
2579
2580 opt_argument_list
2581         : /* empty */           { $$ = null; }
2582         | argument_list
2583         ;
2584
2585 argument_list
2586         : argument
2587           { 
2588                 ArrayList list = new ArrayList (4);
2589                 list.Add ($1);
2590                 $$ = list;
2591           }
2592         | argument_list COMMA argument
2593           {
2594                 ArrayList list = (ArrayList) $1;
2595                 list.Add ($3);
2596                 $$ = list;
2597           }
2598         | argument_list error {
2599                 CheckToken (1026, yyToken, ", or ) expected");
2600           }
2601         ;
2602
2603 argument
2604         : expression
2605           {
2606                 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2607           }
2608         | non_simple_argument
2609           {
2610                 $$ = $1;
2611           }
2612         ;
2613
2614 non_simple_argument
2615         : REF variable_reference 
2616           { 
2617                 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2618           }
2619         | OUT variable_reference 
2620           { 
2621                 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2622           }
2623         | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2624           {
2625                 ArrayList list = (ArrayList) $3;
2626                 Argument[] args = new Argument [list.Count];
2627                 list.CopyTo (args, 0);
2628
2629                 Expression expr = new Arglist (args, lexer.Location);
2630                 $$ = new Argument (expr, Argument.AType.Expression);
2631           }
2632         | ARGLIST
2633           {
2634                 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2635           }
2636         ;
2637
2638 variable_reference
2639         : expression { note ("section 5.4"); $$ = $1; }
2640         ;
2641
2642 element_access
2643         : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET 
2644           {
2645                 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2646           }
2647         | primary_expression rank_specifiers
2648           {
2649                 // So the super-trick is that primary_expression
2650                 // can only be either a SimpleName or a MemberAccess. 
2651                 // The MemberAccess case arises when you have a fully qualified type-name like :
2652                 // Foo.Bar.Blah i;
2653                 // SimpleName is when you have
2654                 // Blah i;
2655                   
2656                 Expression expr = (Expression) $1;  
2657                 if (expr is ComposedCast){
2658                         $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2659                 } else if (!(expr is SimpleName || expr is MemberAccess)){
2660                         Error_ExpectingTypeName (lexer.Location, expr);
2661                         $$ = TypeManager.system_object_expr;
2662                 } else {
2663                         //
2664                         // So we extract the string corresponding to the SimpleName
2665                         // or MemberAccess
2666                         // 
2667                         $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2668                 }
2669           }
2670         ;
2671
2672 expression_list
2673         : expression
2674           {
2675                 ArrayList list = new ArrayList (4);
2676                 list.Add ($1);
2677                 $$ = list;
2678           }
2679         | expression_list COMMA expression
2680           {
2681                 ArrayList list = (ArrayList) $1;
2682                 list.Add ($3);
2683                 $$ = list;
2684           }
2685         ;
2686
2687 this_access
2688         : THIS
2689           {
2690                 $$ = new This (current_block, lexer.Location);
2691           }
2692         ;
2693
2694 base_access
2695         : BASE DOT IDENTIFIER
2696           {
2697                 $$ = new BaseAccess ((string) $3, lexer.Location);
2698           }
2699         | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2700           {
2701                 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2702           }
2703         | BASE error {
2704                 Report.Error (175, lexer.Location, "Use of keyword `base' is not valid in this context");
2705                 $$ = null;
2706           }
2707         ;
2708
2709 post_increment_expression
2710         : primary_expression OP_INC
2711           {
2712                 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2713                                        (Expression) $1, lexer.Location);
2714           }
2715         ;
2716
2717 post_decrement_expression
2718         : primary_expression OP_DEC
2719           {
2720                 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2721                                        (Expression) $1, lexer.Location);
2722           }
2723         ;
2724
2725 new_expression
2726         : object_or_delegate_creation_expression
2727         | array_creation_expression
2728         ;
2729
2730 object_or_delegate_creation_expression
2731         : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2732           {
2733                 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2734           }
2735         ;
2736
2737 array_creation_expression
2738         : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET 
2739           opt_rank_specifier
2740           opt_array_initializer
2741           {
2742                 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2743           }
2744         | NEW type rank_specifiers array_initializer
2745           {
2746                 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2747           }
2748         | NEW error
2749           {
2750                 Report.Error (1031, lexer.Location, "Type expected");
2751                 $$ = null;
2752           }          
2753         | NEW type error 
2754           {
2755                 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2756           }
2757         ;
2758
2759 opt_rank_specifier
2760         : /* empty */
2761           {
2762                   $$ = "";
2763           }
2764         | rank_specifiers
2765           {
2766                         $$ = $1;
2767           }
2768         ;
2769
2770 rank_specifiers
2771         : rank_specifier opt_rank_specifier
2772           {
2773                   $$ = (string) $2 + (string) $1;
2774           }
2775         ;
2776
2777 rank_specifier
2778         : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2779           {
2780                 $$ = "[" + (string) $2 + "]";
2781           }
2782         ;
2783
2784 opt_dim_separators
2785         : /* empty */
2786           {
2787                 $$ = "";
2788           }
2789         | dim_separators
2790           {
2791                   $$ = $1;
2792           }               
2793         ;
2794
2795 dim_separators
2796         : COMMA
2797           {
2798                 $$ = ",";
2799           }
2800         | dim_separators COMMA
2801           {
2802                 $$ = (string) $1 + ",";
2803           }
2804         ;
2805
2806 opt_array_initializer
2807         : /* empty */
2808           {
2809                 $$ = null;
2810           }
2811         | array_initializer
2812           {
2813                 $$ = $1;
2814           }
2815         ;
2816
2817 array_initializer
2818         : OPEN_BRACE CLOSE_BRACE
2819           {
2820                 ArrayList list = new ArrayList (4);
2821                 $$ = list;
2822           }
2823         | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2824           {
2825                 $$ = (ArrayList) $2;
2826           }
2827         ;
2828
2829 variable_initializer_list
2830         : variable_initializer
2831           {
2832                 ArrayList list = new ArrayList (4);
2833                 list.Add ($1);
2834                 $$ = list;
2835           }
2836         | variable_initializer_list COMMA variable_initializer
2837           {
2838                 ArrayList list = (ArrayList) $1;
2839                 list.Add ($3);
2840                 $$ = list;
2841           }
2842         ;
2843
2844 typeof_expression
2845         : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2846           {
2847                 $$ = new TypeOfVoid (lexer.Location);
2848           }
2849         | TYPEOF OPEN_PARENS type CLOSE_PARENS
2850           {
2851                 $$ = new TypeOf ((Expression) $3, lexer.Location);
2852           }
2853         ;
2854
2855 sizeof_expression
2856         : SIZEOF OPEN_PARENS type CLOSE_PARENS { 
2857                 $$ = new SizeOf ((Expression) $3, lexer.Location);
2858           }
2859         ;
2860
2861 checked_expression
2862         : CHECKED OPEN_PARENS expression CLOSE_PARENS
2863           {
2864                 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2865           }
2866         ;
2867
2868 unchecked_expression
2869         : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2870           {
2871                 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2872           }
2873         ;
2874
2875 pointer_member_access 
2876         : primary_expression OP_PTR IDENTIFIER
2877           {
2878                 Expression deref;
2879
2880                 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2881                 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2882           }
2883         ;
2884
2885 anonymous_method_expression
2886         : DELEGATE opt_anonymous_method_signature {
2887                 oob_stack.Push (current_local_parameters);
2888                 current_local_parameters = (Parameters)$2;
2889
2890                 // Force the next block to be created as a ToplevelBlock
2891                 oob_stack.Push (current_block);
2892                 oob_stack.Push (top_current_block);
2893                 oob_stack.Push (lexer.Location);
2894                 current_block = null;
2895           } block {
2896                 Location loc = (Location) oob_stack.Pop ();
2897                 top_current_block = (Block) oob_stack.Pop ();
2898                 current_block = (Block) oob_stack.Pop ();
2899                 if (RootContext.Version == LanguageVersion.ISO_1){
2900                         Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
2901                         $$ = null;
2902                 } else  {
2903                         ToplevelBlock anon_block = (ToplevelBlock) $4;
2904
2905                         anon_block.Parent = current_block;
2906                         $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block, 
2907                                 anon_block, loc);
2908                 }
2909                 current_local_parameters = (Parameters) oob_stack.Pop ();
2910           }
2911         ;
2912
2913 opt_anonymous_method_signature
2914         : /* empty */                   { $$ = null; } 
2915         | anonymous_method_signature
2916         ;
2917
2918 anonymous_method_signature
2919         : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS 
2920           {
2921                 if ($2 == null)
2922                         $$ = Parameters.EmptyReadOnlyParameters;
2923                 else {
2924                         ArrayList par_list = (ArrayList) $2;
2925                         Parameter [] pars = new Parameter [par_list.Count];
2926                         par_list.CopyTo (pars);
2927                         $$ = new Parameters (pars, null, lexer.Location);
2928                 }
2929           }
2930         ;
2931
2932 opt_anonymous_method_parameter_list
2933         : /* empty */                      { $$ = null; } 
2934         | anonymous_method_parameter_list  { $$ = $1; }
2935         ;
2936
2937 anonymous_method_parameter_list
2938         : anonymous_method_parameter 
2939           {
2940                 ArrayList a = new ArrayList (4);
2941                 a.Add ($1);
2942                 $$ = a;
2943           }
2944         | anonymous_method_parameter_list COMMA anonymous_method_parameter 
2945           {
2946                 ArrayList a = (ArrayList) $1;
2947                 a.Add ($3);
2948                 $$ = a;
2949           }
2950         ; 
2951
2952 anonymous_method_parameter
2953         : opt_parameter_modifier type IDENTIFIER {
2954                 $$ = new Parameter ((Expression) $2, (string) $3, (Parameter.Modifier) $1, null);
2955           }
2956         | PARAMS type IDENTIFIER {
2957                 Report.Error (-221, lexer.Location, "params modifier not allowed in anonymous method declaration");
2958                 $$ = null;
2959           }
2960         ;
2961
2962 unary_expression
2963         : primary_expression
2964         | BANG prefixed_unary_expression
2965           {
2966                 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2967           }
2968         | TILDE prefixed_unary_expression
2969           {
2970                 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2971           }
2972         | cast_expression
2973         ;
2974
2975 cast_list
2976         : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
2977           {
2978                 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2979           }
2980         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
2981           {
2982                 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2983           }     
2984         ;
2985
2986 cast_expression
2987         : cast_list
2988         | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2989           {
2990                 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2991           }
2992         ;
2993
2994         //
2995         // The idea to split this out is from Rhys' grammar
2996         // to solve the problem with casts.
2997         //
2998 prefixed_unary_expression
2999         : unary_expression
3000         | PLUS prefixed_unary_expression
3001           { 
3002                 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
3003           } 
3004         | MINUS prefixed_unary_expression 
3005           { 
3006                 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
3007           }
3008         | OP_INC prefixed_unary_expression 
3009           {
3010                 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3011                                        (Expression) $2, lexer.Location);
3012           }
3013         | OP_DEC prefixed_unary_expression 
3014           {
3015                 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3016                                        (Expression) $2, lexer.Location);
3017           }
3018         | STAR prefixed_unary_expression
3019           {
3020                 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
3021           }
3022         | BITWISE_AND prefixed_unary_expression
3023           {
3024                 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
3025           }
3026         ;
3027
3028 pre_increment_expression
3029         : OP_INC prefixed_unary_expression 
3030           {
3031                 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3032                                        (Expression) $2, lexer.Location);
3033           }
3034         ;
3035
3036 pre_decrement_expression
3037         : OP_DEC prefixed_unary_expression 
3038           {
3039                 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3040                                        (Expression) $2, lexer.Location);
3041           }
3042         ;
3043
3044 multiplicative_expression
3045         : prefixed_unary_expression
3046         | multiplicative_expression STAR prefixed_unary_expression
3047           {
3048                 $$ = new Binary (Binary.Operator.Multiply, 
3049                                  (Expression) $1, (Expression) $3, lexer.Location);
3050           }
3051         | multiplicative_expression DIV prefixed_unary_expression
3052           {
3053                 $$ = new Binary (Binary.Operator.Division, 
3054                                  (Expression) $1, (Expression) $3, lexer.Location);
3055           }
3056         | multiplicative_expression PERCENT prefixed_unary_expression 
3057           {
3058                 $$ = new Binary (Binary.Operator.Modulus, 
3059                                  (Expression) $1, (Expression) $3, lexer.Location);
3060           }
3061         ;
3062
3063 additive_expression
3064         : multiplicative_expression
3065         | additive_expression PLUS multiplicative_expression 
3066           {
3067                 $$ = new Binary (Binary.Operator.Addition, 
3068                                  (Expression) $1, (Expression) $3, lexer.Location);
3069           }
3070         | additive_expression MINUS multiplicative_expression
3071           {
3072                 $$ = new Binary (Binary.Operator.Subtraction, 
3073                                  (Expression) $1, (Expression) $3, lexer.Location);
3074           }
3075         ;
3076
3077 shift_expression
3078         : additive_expression
3079         | shift_expression OP_SHIFT_LEFT additive_expression
3080           {
3081                 $$ = new Binary (Binary.Operator.LeftShift, 
3082                                  (Expression) $1, (Expression) $3, lexer.Location);
3083           }
3084         | shift_expression OP_SHIFT_RIGHT additive_expression
3085           {
3086                 $$ = new Binary (Binary.Operator.RightShift, 
3087                                  (Expression) $1, (Expression) $3, lexer.Location);
3088           }
3089         ; 
3090
3091 relational_expression
3092         : shift_expression
3093         | relational_expression OP_LT shift_expression
3094           {
3095                 $$ = new Binary (Binary.Operator.LessThan, 
3096                                  (Expression) $1, (Expression) $3, lexer.Location);
3097           }
3098         | relational_expression OP_GT shift_expression
3099           {
3100                 $$ = new Binary (Binary.Operator.GreaterThan, 
3101                                  (Expression) $1, (Expression) $3, lexer.Location);
3102           }
3103         | relational_expression OP_LE shift_expression
3104           {
3105                 $$ = new Binary (Binary.Operator.LessThanOrEqual, 
3106                                  (Expression) $1, (Expression) $3, lexer.Location);
3107           }
3108         | relational_expression OP_GE shift_expression
3109           {
3110                 $$ = new Binary (Binary.Operator.GreaterThanOrEqual, 
3111                                  (Expression) $1, (Expression) $3, lexer.Location);
3112           }
3113         | relational_expression IS type
3114           {
3115                 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
3116           }
3117         | relational_expression AS type
3118           {
3119                 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
3120           }
3121         ;
3122
3123 equality_expression
3124         : relational_expression
3125         | equality_expression OP_EQ relational_expression
3126           {
3127                 $$ = new Binary (Binary.Operator.Equality, 
3128                                  (Expression) $1, (Expression) $3, lexer.Location);
3129           }
3130         | equality_expression OP_NE relational_expression
3131           {
3132                 $$ = new Binary (Binary.Operator.Inequality, 
3133                                  (Expression) $1, (Expression) $3, lexer.Location);
3134           }
3135         ; 
3136
3137 and_expression
3138         : equality_expression
3139         | and_expression BITWISE_AND equality_expression
3140           {
3141                 $$ = new Binary (Binary.Operator.BitwiseAnd, 
3142                                  (Expression) $1, (Expression) $3, lexer.Location);
3143           }
3144         ;
3145
3146 exclusive_or_expression
3147         : and_expression
3148         | exclusive_or_expression CARRET and_expression
3149           {
3150                 $$ = new Binary (Binary.Operator.ExclusiveOr, 
3151                                  (Expression) $1, (Expression) $3, lexer.Location);
3152           }
3153         ;
3154
3155 inclusive_or_expression
3156         : exclusive_or_expression
3157         | inclusive_or_expression BITWISE_OR exclusive_or_expression
3158           {
3159                 $$ = new Binary (Binary.Operator.BitwiseOr, 
3160                                  (Expression) $1, (Expression) $3, lexer.Location);
3161           }
3162         ;
3163
3164 conditional_and_expression
3165         : inclusive_or_expression
3166         | conditional_and_expression OP_AND inclusive_or_expression
3167           {
3168                 $$ = new Binary (Binary.Operator.LogicalAnd, 
3169                                  (Expression) $1, (Expression) $3, lexer.Location);
3170           }
3171         ;
3172
3173 conditional_or_expression
3174         : conditional_and_expression
3175         | conditional_or_expression OP_OR conditional_and_expression
3176           {
3177                 $$ = new Binary (Binary.Operator.LogicalOr, 
3178                                  (Expression) $1, (Expression) $3, lexer.Location);
3179           }
3180         ;
3181
3182 conditional_expression
3183         : conditional_or_expression
3184         | conditional_or_expression INTERR expression COLON expression 
3185           {
3186                 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
3187           }
3188         ;
3189
3190 assignment_expression
3191         : prefixed_unary_expression ASSIGN expression
3192           {
3193                 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
3194           }
3195         | prefixed_unary_expression OP_MULT_ASSIGN expression
3196           {
3197                 Location l = lexer.Location;
3198
3199                 $$ = new CompoundAssign (
3200                         Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
3201           }
3202         | prefixed_unary_expression OP_DIV_ASSIGN expression
3203           {
3204                 Location l = lexer.Location;
3205
3206                 $$ = new CompoundAssign (
3207                         Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
3208           }
3209         | prefixed_unary_expression OP_MOD_ASSIGN expression
3210           {
3211                 Location l = lexer.Location;
3212
3213                 $$ = new CompoundAssign (
3214                         Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
3215           }
3216         | prefixed_unary_expression OP_ADD_ASSIGN expression
3217           {
3218                 Location l = lexer.Location;
3219
3220                 $$ = new CompoundAssign (
3221                         Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
3222           }
3223         | prefixed_unary_expression OP_SUB_ASSIGN expression
3224           {
3225                 Location l = lexer.Location;
3226
3227                 $$ = new CompoundAssign (
3228                         Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
3229           }
3230         | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3231           {
3232                 Location l = lexer.Location;
3233
3234                 $$ = new CompoundAssign (
3235                         Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
3236           }
3237         | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3238           {
3239                 Location l = lexer.Location;
3240
3241                 $$ = new CompoundAssign (
3242                         Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
3243           }
3244         | prefixed_unary_expression OP_AND_ASSIGN expression
3245           {
3246                 Location l = lexer.Location;
3247
3248                 $$ = new CompoundAssign (
3249                         Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
3250           }
3251         | prefixed_unary_expression OP_OR_ASSIGN expression
3252           {
3253                 Location l = lexer.Location;
3254
3255                 $$ = new CompoundAssign (
3256                         Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
3257           }
3258         | prefixed_unary_expression OP_XOR_ASSIGN expression
3259           {
3260                 Location l = lexer.Location;
3261
3262                 $$ = new CompoundAssign (
3263                         Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
3264           }
3265         ;
3266
3267 expression
3268         : conditional_expression
3269         | assignment_expression
3270         ;
3271
3272 constant_expression
3273         : expression
3274         ;
3275
3276 boolean_expression
3277         : expression
3278         ;
3279
3280 //
3281 // 10 classes
3282 //
3283 class_declaration
3284         : opt_attributes
3285           opt_modifiers
3286           opt_partial
3287           CLASS member_name
3288           {
3289                 MemberName name = MakeName ((MemberName) $5);
3290                 bool partial = (bool) $3;
3291                 int mod_flags = (int) $2;
3292
3293                 if (partial) {
3294                         ClassPart part = PartialContainer.CreatePart (
3295                                 current_namespace, current_container, name, mod_flags,
3296                                 (Attributes) $1, Kind.Class, lexer.Location);
3297
3298                         current_container = part.PartialContainer;
3299                         current_class = part;
3300                 } else {
3301                         if ((mod_flags & Modifiers.STATIC) != 0) {
3302                                 current_class = new StaticClass (
3303                                         current_namespace, current_container, name,
3304                                         mod_flags, (Attributes) $1, lexer.Location);
3305                         } else {
3306                                 current_class = new Class (
3307                                         current_namespace, current_container, name,
3308                                         mod_flags, (Attributes) $1, lexer.Location);
3309                         }
3310
3311                         current_container = current_class;
3312                         RootContext.Tree.RecordDecl (name.GetName (true), current_class);
3313                 }
3314           }
3315           opt_class_base
3316           {
3317                 if ($7 != null) {
3318                         if (current_class.Name == "System.Object") {
3319                                 Report.Error (537, current_class.Location,
3320                                               "The class System.Object cannot have a base " +
3321                                               "class or implement an interface.");
3322                         }
3323                         current_class.Bases = (ArrayList) $7;
3324                 }
3325
3326                 if (RootContext.Documentation != null) {
3327                         current_class.DocComment = Lexer.consume_doc_comment ();
3328                         Lexer.doc_state = XmlCommentState.Allowed;
3329                 }
3330
3331                 current_class.Register ();
3332           }
3333           class_body
3334           {
3335                 if (RootContext.Documentation != null)
3336                         Lexer.doc_state = XmlCommentState.Allowed;
3337           }
3338           opt_semicolon 
3339           {
3340                 $$ = current_class;
3341
3342                 current_container = current_container.Parent;
3343                 current_class = current_container;
3344           }
3345         ;       
3346
3347 opt_partial
3348         : /* empty */
3349           { $$ = (bool) false; }
3350         | PARTIAL
3351           { $$ = (bool) true; }
3352         ;
3353
3354 opt_modifiers
3355         : /* empty */           { $$ = (int) 0; }
3356         | modifiers
3357         ;
3358
3359 modifiers
3360         : modifier
3361         | modifiers modifier
3362           { 
3363                 int m1 = (int) $1;
3364                 int m2 = (int) $2;
3365
3366                 if ((m1 & m2) != 0) {
3367                         Location l = lexer.Location;
3368                         Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3369                 }
3370                 $$ = (int) (m1 | m2);
3371           }
3372         ;
3373
3374 modifier
3375         : NEW                   { $$ = Modifiers.NEW; }
3376         | PUBLIC                { $$ = Modifiers.PUBLIC; }
3377         | PROTECTED             { $$ = Modifiers.PROTECTED; }
3378         | INTERNAL              { $$ = Modifiers.INTERNAL; }
3379         | PRIVATE               { $$ = Modifiers.PRIVATE; }
3380         | ABSTRACT              { $$ = Modifiers.ABSTRACT; }
3381         | SEALED                { $$ = Modifiers.SEALED; }
3382         | STATIC                { $$ = Modifiers.STATIC; }
3383         | READONLY              { $$ = Modifiers.READONLY; }
3384         | VIRTUAL               { $$ = Modifiers.VIRTUAL; }
3385         | OVERRIDE              { $$ = Modifiers.OVERRIDE; }
3386         | EXTERN                { $$ = Modifiers.EXTERN; }
3387         | VOLATILE              { $$ = Modifiers.VOLATILE; }
3388         | UNSAFE                { $$ = Modifiers.UNSAFE; }
3389         ;
3390
3391 opt_class_base
3392         : /* empty */           { $$ = null; }
3393         | class_base            { $$ = $1;   }
3394         ;
3395
3396 class_base
3397         : COLON type_list { $$ = $2; }
3398         ;
3399
3400 //
3401 // Statements (8.2)
3402 //
3403
3404 //
3405 // A block is "contained" on the following places:
3406 //      method_body
3407 //      property_declaration as part of the accessor body (get/set)
3408 //      operator_declaration
3409 //      constructor_declaration
3410 //      destructor_declaration
3411 //      event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3412 //      
3413 block
3414         : OPEN_BRACE 
3415           {
3416                 if (current_block == null){
3417                         current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, lexer.Location);
3418                         top_current_block = current_block;
3419                 } else {
3420                 current_block = new Block (current_block, current_local_parameters,
3421                                            lexer.Location, Location.Null);
3422                 }
3423           } 
3424           opt_statement_list CLOSE_BRACE 
3425           { 
3426                 while (current_block.Implicit)
3427                         current_block = current_block.Parent;
3428                 $$ = current_block;
3429                 current_block.SetEndLocation (lexer.Location);
3430                 current_block = current_block.Parent;
3431                 if (current_block == null)
3432                         top_current_block = null;
3433           }
3434         ;
3435
3436 opt_statement_list
3437         : /* empty */
3438         | statement_list 
3439         ;
3440
3441 statement_list
3442         : statement
3443         | statement_list statement
3444         ;
3445
3446 statement
3447         : declaration_statement
3448           {
3449                 if ($1 != null && (Block) $1 != current_block){
3450                         current_block.AddStatement ((Statement) $1);
3451                         current_block = (Block) $1;
3452                 }
3453           }
3454         | valid_declaration_statement
3455           {
3456                 current_block.AddStatement ((Statement) $1);
3457           }
3458         | labeled_statement
3459         ;
3460
3461 valid_declaration_statement
3462         : block
3463         | empty_statement
3464         | expression_statement
3465         | selection_statement
3466         | iteration_statement
3467         | jump_statement                  
3468         | try_statement
3469         | checked_statement
3470         | unchecked_statement
3471         | lock_statement
3472         | using_statement
3473         | unsafe_statement
3474         | fixed_statement
3475         ;
3476
3477 embedded_statement
3478         : valid_declaration_statement
3479         | declaration_statement
3480           {
3481                   Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3482                   $$ = null;
3483           }
3484         | labeled_statement
3485           {
3486                   Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3487                   $$ = null;
3488           }
3489         ;
3490
3491 empty_statement
3492         : SEMICOLON
3493           {
3494                   $$ = EmptyStatement.Value;
3495           }
3496         ;
3497
3498 labeled_statement
3499         : IDENTIFIER COLON 
3500           {
3501                 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3502
3503                 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3504                         current_block.AddStatement (labeled);
3505           }
3506           statement
3507         ;
3508
3509 declaration_statement
3510         : local_variable_declaration SEMICOLON
3511           {
3512                 if ($1 != null){
3513                         DictionaryEntry de = (DictionaryEntry) $1;
3514
3515                         $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3516                 }
3517           }
3518
3519         | local_constant_declaration SEMICOLON
3520           {
3521                 if ($1 != null){
3522                         DictionaryEntry de = (DictionaryEntry) $1;
3523
3524                         $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3525                 }
3526           }
3527         ;
3528
3529 /* 
3530  * The following is from Rhys' grammar:
3531  * > Types in local variable declarations must be recognized as 
3532  * > expressions to prevent reduce/reduce errors in the grammar.
3533  * > The expressions are converted into types during semantic analysis.
3534  */
3535 local_variable_type
3536         : primary_expression opt_rank_specifier
3537           { 
3538                 // FIXME: Do something smart here regarding the composition of the type.
3539
3540                 // Ok, the above "primary_expression" is there to get rid of
3541                 // both reduce/reduce and shift/reduces in the grammar, it should
3542                 // really just be "type_name".  If you use type_name, a reduce/reduce
3543                 // creeps up.  If you use namespace_or_type_name (which is all we need
3544                 // really) two shift/reduces appear.
3545                 // 
3546
3547                 // So the super-trick is that primary_expression
3548                 // can only be either a SimpleName or a MemberAccess. 
3549                 // The MemberAccess case arises when you have a fully qualified type-name like :
3550                 // Foo.Bar.Blah i;
3551                 // SimpleName is when you have
3552                 // Blah i;
3553                   
3554                 Expression expr = (Expression) $1;  
3555                 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3556                         Error_ExpectingTypeName (lexer.Location, expr);
3557                         $$ = null;
3558                 } else {
3559                         //
3560                         // So we extract the string corresponding to the SimpleName
3561                         // or MemberAccess
3562                         // 
3563
3564                         if ((string) $2 == "")
3565                                 $$ = $1;
3566                         else
3567                                 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3568                 }
3569           }
3570         | builtin_types opt_rank_specifier
3571           {
3572                 if ((string) $2 == "")
3573                         $$ = $1;
3574                 else
3575                         $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3576           }
3577         ;
3578
3579 local_variable_pointer_type
3580         : primary_expression STAR
3581           {
3582                 Expression expr = (Expression) $1;  
3583                 Location l = lexer.Location;
3584
3585                 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3586                         Error_ExpectingTypeName (l, expr);
3587
3588                         $$ = null;
3589                 } else 
3590                         $$ = new ComposedCast ((Expression) $1, "*", l);
3591           }
3592         | builtin_types STAR
3593           {
3594                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3595           }
3596         | VOID STAR
3597           {
3598                 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3599           }
3600         | local_variable_pointer_type STAR
3601           {
3602                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3603           }
3604         ;
3605
3606 local_variable_declaration
3607         : local_variable_type variable_declarators
3608           {
3609                 if ($1 != null)
3610                         $$ = new DictionaryEntry ($1, $2);
3611                 else
3612                         $$ = null;
3613           }
3614         | local_variable_pointer_type opt_rank_specifier variable_declarators
3615         {
3616                 if ($1 != null){
3617                         Expression t;
3618
3619                         if ((string) $2 == "")
3620                                 t = (Expression) $1;
3621                         else
3622                                 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3623                         $$ = new DictionaryEntry (t, $3);
3624                 } else 
3625                         $$ = null;
3626         }
3627         ;
3628
3629 local_constant_declaration
3630         : CONST local_variable_type constant_declarators
3631           {
3632                 if ($2 != null)
3633                         $$ = new DictionaryEntry ($2, $3);
3634                 else
3635                         $$ = null;
3636           }
3637         ;
3638
3639 expression_statement
3640         : statement_expression SEMICOLON
3641           {
3642                 $$ = $1;
3643           }
3644         ;
3645
3646         //
3647         // We have to do the wrapping here and not in the case above,
3648         // because statement_expression is used for example in for_statement
3649         //
3650 statement_expression
3651         : invocation_expression         { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3652         | object_creation_expression    { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3653         | assignment_expression         { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3654         | post_increment_expression     { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3655         | post_decrement_expression     { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3656         | pre_increment_expression      { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3657         | pre_decrement_expression      { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3658         | error {
3659                 Report.Error (1002, lexer.Location, "Expecting `;'");
3660                 $$ = null;
3661           }
3662         ;
3663
3664 object_creation_expression
3665         : object_or_delegate_creation_expression
3666           { note ("complain if this is a delegate maybe?"); } 
3667         ;
3668
3669 selection_statement
3670         : if_statement
3671         | switch_statement
3672         ; 
3673
3674 if_statement
3675         : if_statement_open if_statement_rest
3676           {
3677                 $$ = $2;
3678           }
3679         ;
3680
3681 if_statement_open
3682         : IF OPEN_PARENS 
3683           {
3684                 oob_stack.Push (lexer.Location);
3685           }
3686         ;
3687
3688 if_statement_rest
3689         : boolean_expression CLOSE_PARENS 
3690           embedded_statement
3691           { 
3692                 Location l = (Location) oob_stack.Pop ();
3693
3694                 $$ = new If ((Expression) $1, (Statement) $3, l);
3695
3696                 if (RootContext.WarningLevel >= 4){
3697                         if ($3 == EmptyStatement.Value)
3698                                 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3699                 }
3700
3701           }
3702         | boolean_expression CLOSE_PARENS
3703           embedded_statement ELSE embedded_statement
3704           {
3705                 Location l = (Location) oob_stack.Pop ();
3706
3707                 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
3708           }
3709         ;
3710
3711 switch_statement
3712         : SWITCH OPEN_PARENS 
3713           { 
3714                 oob_stack.Push (lexer.Location);
3715                 switch_stack.Push (current_block);
3716           }
3717           expression CLOSE_PARENS 
3718           switch_block
3719           {
3720                 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
3721                 current_block = (Block) switch_stack.Pop ();
3722           }
3723         ;
3724
3725 switch_block
3726         : OPEN_BRACE
3727           opt_switch_sections
3728           CLOSE_BRACE
3729           {
3730                 $$ = $2;
3731           }
3732         ;
3733
3734 opt_switch_sections
3735         : /* empty */           
3736           {
3737                 Report.Error (1522, lexer.Location, "Empty switch block"); 
3738           }
3739         | switch_sections
3740         ;
3741
3742 switch_sections
3743         : switch_section 
3744           {
3745                 ArrayList sections = new ArrayList (4);
3746
3747                 sections.Add ($1);
3748                 $$ = sections;
3749           }
3750         | switch_sections switch_section
3751           {
3752                 ArrayList sections = (ArrayList) $1;
3753
3754                 sections.Add ($2);
3755                 $$ = sections;
3756           }
3757         ;
3758
3759 switch_section
3760         : switch_labels
3761           {
3762                 current_block = current_block.CreateSwitchBlock (lexer.Location);
3763           }
3764           statement_list 
3765           {
3766                 Block topmost = current_block;
3767
3768                 while (topmost.Implicit)
3769                         topmost = topmost.Parent;
3770                 $$ = new SwitchSection ((ArrayList) $1, topmost);
3771           }
3772         ;
3773
3774 switch_labels
3775         : switch_label 
3776           {
3777                 ArrayList labels = new ArrayList (4);
3778
3779                 labels.Add ($1);
3780                 $$ = labels;
3781           }
3782         | switch_labels switch_label 
3783           {
3784                 ArrayList labels = (ArrayList) ($1);
3785                 labels.Add ($2);
3786
3787                 $$ = labels;
3788           }
3789         ;
3790
3791 switch_label
3792         : CASE constant_expression COLON        { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3793         | DEFAULT COLON                         { $$ = new SwitchLabel (null, lexer.Location); }
3794         | error {
3795                 Report.Error (
3796                         1523, lexer.Location, 
3797                         "The keyword case or default must precede code in switch block");
3798           }
3799         ;
3800
3801 iteration_statement
3802         : while_statement
3803         | do_statement
3804         | for_statement
3805         | foreach_statement
3806         ;
3807
3808 while_statement
3809         : WHILE OPEN_PARENS 
3810         {
3811                 oob_stack.Push (lexer.Location);
3812         }
3813         boolean_expression CLOSE_PARENS embedded_statement
3814         {
3815                 Location l = (Location) oob_stack.Pop ();
3816                 $$ = new While ((Expression) $4, (Statement) $6, l);
3817         
3818                 if (RootContext.WarningLevel >= 4){
3819                         if ($6 == EmptyStatement.Value)
3820                                 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3821                 }
3822         }
3823         ;
3824
3825 do_statement
3826         : DO embedded_statement 
3827           WHILE OPEN_PARENS {
3828                 oob_stack.Push (lexer.Location);
3829           }
3830           boolean_expression CLOSE_PARENS SEMICOLON
3831           {
3832                 Location l = (Location) oob_stack.Pop ();
3833
3834                 $$ = new Do ((Statement) $2, (Expression) $6, l);
3835           }
3836         ;
3837
3838 for_statement
3839         : FOR OPEN_PARENS 
3840           opt_for_initializer SEMICOLON
3841           {
3842                 Block assign_block = new Block (current_block);
3843                 current_block = assign_block;
3844
3845                 if ($3 is DictionaryEntry){
3846                         DictionaryEntry de = (DictionaryEntry) $3;
3847                         
3848                         Expression type = (Expression) de.Key;
3849                         ArrayList var_declarators = (ArrayList) de.Value;
3850
3851                         foreach (VariableDeclaration decl in var_declarators){
3852
3853                                 LocalInfo vi;
3854
3855                                 vi = current_block.AddVariable (
3856                                         type, decl.identifier, current_local_parameters, decl.Location);
3857                                 if (vi == null)
3858                                         continue;
3859
3860                                 Location l = lexer.Location;
3861                                 Expression expr;
3862                                 if (decl.expression_or_array_initializer is Expression){
3863                                         expr = (Expression) decl.expression_or_array_initializer;
3864                                 } else if (decl.expression_or_array_initializer == null) {
3865                                         expr = null;
3866                                 } else {
3867                                         ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3868                                         expr = new ArrayCreation (type, "", init, decl.Location);
3869                                 }
3870                                         
3871                                 LocalVariableReference var;
3872                                 var = new LocalVariableReference (assign_block, decl.identifier, l);
3873
3874                                 if (expr != null) {
3875                                         Assign a = new Assign (var, expr, decl.Location);
3876                                         
3877                                         assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3878                                 }
3879                         }
3880                         
3881                         $3 = null;
3882                 } 
3883                 oob_stack.Push (lexer.Location);
3884           } 
3885           opt_for_condition SEMICOLON
3886           opt_for_iterator CLOSE_PARENS 
3887           embedded_statement
3888           {
3889                 Location l = (Location) oob_stack.Pop ();
3890
3891                 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3892
3893                 if (RootContext.WarningLevel >= 4){
3894                         if ($10 == EmptyStatement.Value)
3895                                 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3896                 }
3897
3898                 current_block.AddStatement (f);
3899                 while (current_block.Implicit)
3900                         current_block = current_block.Parent;
3901                 $$ = current_block;
3902                 current_block = current_block.Parent;
3903           }
3904         ;
3905
3906 opt_for_initializer
3907         : /* empty */           { $$ = EmptyStatement.Value; }
3908         | for_initializer       
3909         ;
3910
3911 for_initializer
3912         : local_variable_declaration
3913         | statement_expression_list
3914         ;
3915
3916 opt_for_condition
3917         : /* empty */           { $$ = null; }
3918         | boolean_expression
3919         ;
3920
3921 opt_for_iterator
3922         : /* empty */           { $$ = EmptyStatement.Value; }
3923         | for_iterator
3924         ;
3925
3926 for_iterator
3927         : statement_expression_list
3928         ;
3929
3930 statement_expression_list
3931         : statement_expression  
3932           {
3933                 // CHANGE: was `null'
3934                 Block b = new Block (current_block, Block.Flags.Implicit);   
3935
3936                 b.AddStatement ((Statement) $1);
3937                 $$ = b;
3938           }
3939         | statement_expression_list COMMA statement_expression
3940           {
3941                 Block b = (Block) $1;
3942
3943                 b.AddStatement ((Statement) $3);
3944                 $$ = $1;
3945           }
3946         ;
3947
3948 foreach_statement
3949         : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
3950         {
3951                 Report.Error (230, lexer.Location, "Type and identifier are both required in a foreach statement");
3952                 $$ = null;
3953         }
3954         | FOREACH OPEN_PARENS type IDENTIFIER IN 
3955           {
3956                 oob_stack.Push (lexer.Location);
3957           }
3958           expression CLOSE_PARENS 
3959           {
3960                 oob_stack.Push (current_block);
3961
3962                 Block foreach_block = new Block (current_block);
3963                 LocalVariableReference v = null;
3964                 Location l = lexer.Location;
3965                 LocalInfo vi;
3966
3967                 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3968                 if (vi != null) {
3969                         vi.ReadOnly = true;
3970
3971                         // Get a writable reference to this read-only variable.
3972                         v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3973                 }
3974                 current_block = foreach_block;
3975
3976                 oob_stack.Push (v);
3977                 oob_stack.Push (current_block);
3978           } 
3979           embedded_statement 
3980           {
3981                 Block foreach_block = (Block) oob_stack.Pop ();
3982                 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3983                 Block prev_block = (Block) oob_stack.Pop ();
3984                 Location l = (Location) oob_stack.Pop ();
3985
3986                 current_block = prev_block;
3987
3988                 if (v != null) {
3989                         Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3990                         foreach_block.AddStatement (f);
3991                 }
3992
3993                 $$ = foreach_block;
3994           }
3995         ;
3996
3997 jump_statement
3998         : break_statement
3999         | continue_statement
4000         | goto_statement
4001         | return_statement
4002         | throw_statement
4003         | yield_statement
4004         ;
4005
4006 break_statement
4007         : BREAK SEMICOLON
4008           {
4009                 $$ = new Break (lexer.Location);
4010           }
4011         ;
4012
4013 continue_statement
4014         : CONTINUE SEMICOLON
4015           {
4016                 $$ = new Continue (lexer.Location);
4017           }
4018         ;
4019
4020 goto_statement
4021         : GOTO IDENTIFIER SEMICOLON 
4022           {
4023                 $$ = new Goto (current_block, (string) $2, lexer.Location);
4024           }
4025         | GOTO CASE constant_expression SEMICOLON
4026           {
4027                 $$ = new GotoCase ((Expression) $3, lexer.Location);
4028           }
4029         | GOTO DEFAULT SEMICOLON 
4030           {
4031                 $$ = new GotoDefault (lexer.Location);
4032           }
4033         ; 
4034
4035 return_statement
4036         : RETURN opt_expression SEMICOLON
4037           {
4038                 $$ = new Return ((Expression) $2, lexer.Location);
4039           }
4040         ;
4041
4042 throw_statement
4043         : THROW opt_expression SEMICOLON
4044           {
4045                 $$ = new Throw ((Expression) $2, lexer.Location);
4046           }
4047         ;
4048
4049 yield_statement 
4050         : IDENTIFIER RETURN expression 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 Yield ((Expression) $3, lexer.Location); 
4067                 }
4068           }
4069         | IDENTIFIER BREAK SEMICOLON
4070           {
4071                 string s = (string) $1;
4072                 if (s != "yield"){
4073                         Report.Error (1003, lexer.Location, "; expected");
4074                         $$ = null;
4075                 }
4076                 if (RootContext.Version == LanguageVersion.ISO_1){
4077                         Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4078                         $$ = null;
4079                 }
4080                 if (iterator_container == null){
4081                         Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4082                         $$ = null;
4083                 } else {
4084                         iterator_container.SetYields ();
4085                         $$ = new YieldBreak (lexer.Location);
4086                 }
4087           }
4088         ;
4089
4090 opt_expression
4091         : /* empty */
4092         | expression
4093         ;
4094
4095 try_statement
4096         : TRY block catch_clauses 
4097         {
4098                 Catch g = null;
4099                 
4100                 ArrayList c = (ArrayList)$3;
4101                 for (int i = 0; i < c.Count; ++i) {
4102                         Catch cc = (Catch) c [i];
4103                         if (cc.IsGeneral) {
4104                                 if (i != c.Count - 1)
4105                                         Report.Error (1017, cc.loc, "Empty catch block must be the last in a series of catch blocks");
4106                                 g = cc;
4107                                 c.RemoveAt (i);
4108                                 i--;
4109                         }
4110                 }
4111
4112                 // Now s contains the list of specific catch clauses
4113                 // and g contains the general one.
4114                 
4115                 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4116         }
4117         | TRY block opt_catch_clauses FINALLY block
4118           {
4119                 Catch g = null;
4120                 ArrayList s = new ArrayList (4);
4121                 ArrayList catch_list = (ArrayList) $3;
4122
4123                 if (catch_list != null){
4124                         foreach (Catch cc in catch_list) {
4125                                 if (cc.IsGeneral)
4126                                         g = cc;
4127                                 else
4128                                         s.Add (cc);
4129                         }
4130                 }
4131
4132                 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4133           }
4134         | TRY block error 
4135           {
4136                 Report.Error (1524, lexer.Location, "Expected catch or finally");
4137           }
4138         ;
4139
4140 opt_catch_clauses
4141         : /* empty */  { $$ = null; }
4142         | catch_clauses
4143         ;
4144
4145 catch_clauses
4146         : catch_clause 
4147           {
4148                 ArrayList l = new ArrayList (4);
4149
4150                 l.Add ($1);
4151                 $$ = l;
4152           }
4153         | catch_clauses catch_clause
4154           {
4155                 ArrayList l = (ArrayList) $1;
4156
4157                 l.Add ($2);
4158                 $$ = l;
4159           }
4160         ;
4161
4162 opt_identifier
4163         : /* empty */   { $$ = null; }
4164         | IDENTIFIER
4165         ;
4166
4167 catch_clause 
4168         : CATCH opt_catch_args 
4169         {
4170                 Expression type = null;
4171                 string id = null;
4172                 
4173                 if ($2 != null) {
4174                         DictionaryEntry cc = (DictionaryEntry) $2;
4175                         type = (Expression) cc.Key;
4176                         id   = (string) cc.Value;
4177
4178                         if (id != null){
4179                                 ArrayList one = new ArrayList (4);
4180                                 Location loc = lexer.Location;
4181
4182                                 one.Add (new VariableDeclaration (id, null, loc));
4183
4184                                 $1 = current_block;
4185                                 current_block = new Block (current_block);
4186                                 Block b = declare_local_variables (type, one, loc);
4187                                 current_block = b;
4188                         }
4189                 }
4190         } block {
4191                 Expression type = null;
4192                 string id = null;
4193
4194                 if ($2 != null){
4195                         DictionaryEntry cc = (DictionaryEntry) $2;
4196                         type = (Expression) cc.Key;
4197                         id   = (string) cc.Value;
4198
4199                         if ($1 != null){
4200                                 //
4201                                 // FIXME: I can change this for an assignment.
4202                                 //
4203                                 while (current_block != (Block) $1)
4204                                         current_block = current_block.Parent;
4205                         }
4206                 }
4207
4208
4209                 $$ = new Catch (type, id , (Block) $4, ((Block) $4).loc);
4210         }
4211         ;
4212
4213 opt_catch_args
4214         : /* empty */ { $$ = null; }
4215         | catch_args
4216         ;         
4217
4218 catch_args 
4219         : OPEN_PARENS type opt_identifier CLOSE_PARENS 
4220         {
4221                 $$ = new DictionaryEntry ($2, $3);
4222         }
4223         ;
4224
4225 checked_statement
4226         : CHECKED block
4227           {
4228                 $$ = new Checked ((Block) $2);
4229           }
4230         ;
4231
4232 unchecked_statement
4233         : UNCHECKED block
4234           {
4235                 $$ = new Unchecked ((Block) $2);
4236           }
4237         ;
4238
4239 unsafe_statement
4240         : UNSAFE 
4241         {
4242                 if (!RootContext.Unsafe){
4243                         Report.Error (227, lexer.Location, 
4244                                 "Unsafe code can only be used if --unsafe is used");
4245                 }
4246         } block {
4247                 $$ = new Unsafe ((Block) $3);
4248         }
4249         ;
4250
4251 fixed_statement
4252         : FIXED OPEN_PARENS 
4253           type fixed_pointer_declarators 
4254           CLOSE_PARENS 
4255           {
4256                 ArrayList list = (ArrayList) $4;
4257                 Expression type = (Expression) $3;
4258                 Location l = lexer.Location;
4259                 int top = list.Count;
4260
4261                 Block assign_block = new Block (current_block);
4262                 current_block = assign_block;
4263
4264                 for (int i = 0; i < top; i++){
4265                         Pair p = (Pair) list [i];
4266                         LocalInfo v;
4267
4268                         v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
4269                         if (v == null)
4270                                 continue;
4271
4272                         v.ReadOnly = true;
4273                         v.Pinned = true;
4274                         p.First = v;
4275                         list [i] = p;
4276                 }
4277
4278                 oob_stack.Push (l);
4279           }
4280           embedded_statement 
4281           {
4282                 Location l = (Location) oob_stack.Pop ();
4283
4284                 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4285
4286                 if (RootContext.WarningLevel >= 4){
4287                         if ($7 == EmptyStatement.Value)
4288                                 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4289                 }
4290
4291                 current_block.AddStatement (f);
4292                 while (current_block.Implicit)
4293                         current_block = current_block.Parent;
4294                 $$ = current_block;
4295                 current_block = current_block.Parent;
4296           }
4297         ;
4298
4299 fixed_pointer_declarators
4300         : fixed_pointer_declarator      { 
4301                 ArrayList declarators = new ArrayList (4);
4302                 if ($1 != null)
4303                         declarators.Add ($1);
4304                 $$ = declarators;
4305           }
4306         | fixed_pointer_declarators COMMA fixed_pointer_declarator
4307           {
4308                 ArrayList declarators = (ArrayList) $1;
4309                 if ($3 != null)
4310                         declarators.Add ($3);
4311                 $$ = declarators;
4312           }
4313         ;
4314
4315 fixed_pointer_declarator
4316         : IDENTIFIER ASSIGN expression
4317           {     
4318                 $$ = new Pair ($1, $3);
4319           }
4320         | IDENTIFIER
4321           {
4322                 Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
4323                 $$ = null;
4324           }
4325         ;
4326
4327 lock_statement
4328         : LOCK OPEN_PARENS expression CLOSE_PARENS 
4329           {
4330                 //
4331           } 
4332           embedded_statement
4333           {
4334                 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4335           }
4336         ;
4337
4338 using_statement
4339         : USING OPEN_PARENS resource_acquisition CLOSE_PARENS 
4340           {
4341                 Block assign_block = new Block (current_block);
4342                 current_block = assign_block;
4343
4344                 oob_stack.Push (lexer.Location);
4345                 
4346                 if ($3 is DictionaryEntry){
4347                         DictionaryEntry de = (DictionaryEntry) $3;
4348                         Location l = lexer.Location;
4349
4350                         Expression type = (Expression) de.Key;
4351                         ArrayList var_declarators = (ArrayList) de.Value;
4352
4353                         ArrayList vars = new ArrayList (4);
4354
4355                         foreach (VariableDeclaration decl in var_declarators){
4356
4357                                 LocalInfo vi    = current_block.AddVariable (
4358                                         type, decl.identifier, 
4359                                         current_local_parameters, decl.Location);
4360                                 if (vi == null)
4361                                         continue;
4362                                 vi.ReadOnly = true;
4363
4364                                 Expression expr;
4365                                 if (decl.expression_or_array_initializer is Expression){
4366                                         expr = (Expression) decl.expression_or_array_initializer;
4367                                 } else {
4368                                         ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4369                                         if (init == null) {
4370                                                 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4371                                         }
4372                                         
4373                                         expr = new ArrayCreation (type, "", init, decl.Location);
4374                                 }
4375
4376                                 LocalVariableReference var;
4377
4378                                 // Get a writable reference to this read-only variable.
4379                                 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4380
4381                                 // This is so that it is not a warning on using variables
4382                                 vi.Used = true;
4383
4384                                 vars.Add (new DictionaryEntry (var, expr));                             
4385
4386                                 // Assign a = new Assign (var, expr, decl.Location);
4387                                 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4388                         }
4389                         $3 = new DictionaryEntry (type, vars);
4390                  }
4391           } 
4392           embedded_statement
4393           {
4394                 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
4395                 current_block.AddStatement (u);
4396                 while (current_block.Implicit)
4397                         current_block = current_block.Parent;
4398                 $$ = current_block;
4399                 current_block = current_block.Parent;
4400           }
4401         ; 
4402
4403 resource_acquisition
4404         : local_variable_declaration
4405         | expression
4406         ;
4407
4408 %%
4409
4410 // <summary>
4411 //   A class used to pass around variable declarations and constants
4412 // </summary>
4413 public class VariableDeclaration {
4414         public string identifier;
4415         public object expression_or_array_initializer;
4416         public Location Location;
4417         public Attributes OptAttributes;
4418         public string DocComment;
4419
4420         public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4421         {
4422                 this.identifier = id;
4423                 this.expression_or_array_initializer = eoai;
4424                 this.Location = l;
4425                 this.OptAttributes = opt_attrs;
4426         }
4427
4428         public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4429         {
4430         }
4431 }
4432
4433 /// <summary>
4434 ///  Used to pass around interface property information
4435 /// </summary>
4436 public class InterfaceAccessorInfo {
4437
4438         public readonly Accessor Get, Set;
4439
4440         public InterfaceAccessorInfo (bool has_get, bool has_set,
4441                                       Attributes get_attrs, Attributes set_attrs, int get_mod, int set_mod, Location get_loc, Location set_loc)
4442         {
4443                 if (get_mod != 0)
4444                         Report.Error (275, get_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4445                 if (set_mod != 0)
4446                         Report.Error (275, set_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4447                         
4448                 if (has_get)
4449                         Get = new Accessor (null, 0, get_attrs, get_loc);
4450                 if (has_set)
4451                         Set = new Accessor (null, 0, set_attrs, set_loc);
4452         }
4453
4454 }
4455
4456
4457 // <summary>
4458 //   A class used to hold info about an indexer declarator
4459 // </summary>
4460 public class IndexerDeclaration {
4461         public Expression type;
4462         public MemberName interface_type;
4463         public Parameters param_list;
4464
4465         public IndexerDeclaration (Expression type, MemberName interface_type,
4466                                    Parameters param_list)
4467         {
4468                 this.type = type;
4469                 this.interface_type = interface_type;
4470                 this.param_list = param_list;
4471         }
4472 }
4473
4474 //
4475 // We use this when we do not have an object in advance that is an IIteratorContainer
4476 //
4477 public class SimpleIteratorContainer : IIteratorContainer {
4478         public bool Yields;
4479
4480         public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4481
4482         //
4483         // Reset and return
4484         //
4485         public static SimpleIteratorContainer GetSimple () { 
4486                 Simple.Yields = false;
4487                 return Simple;
4488         }
4489
4490         public void SetYields () { Yields = true; } 
4491 }
4492
4493 // <summary>
4494 //  A class used to hold info about an operator declarator
4495 // </summary>
4496 public class OperatorDeclaration {
4497         public Operator.OpType optype;
4498         public Expression ret_type, arg1type, arg2type;
4499         public string arg1name, arg2name;
4500         public Location location;
4501
4502         public OperatorDeclaration (Operator.OpType op, Expression ret_type, 
4503                                     Expression arg1type, string arg1name,
4504                                     Expression arg2type, string arg2name, Location location)
4505         {
4506                 optype = op;
4507                 this.ret_type = ret_type;
4508                 this.arg1type = arg1type;
4509                 this.arg1name = arg1name;
4510                 this.arg2type = arg2type;
4511                 this.arg2name = arg2name;
4512                 this.location = location;
4513         }
4514
4515 }
4516
4517 void Error_ExpectingTypeName (Location l, Expression expr)
4518 {
4519         if (expr is Invocation){
4520                 Report.Error (1002, l, "; expected");
4521         } else {
4522                 Report.Error (-1, l, "Invalid Type definition");
4523         }
4524 }
4525
4526 // <summary>
4527 //   Given the @class_name name, it creates a fully qualified name
4528 //   based on the containing declaration space
4529 // </summary>
4530 MemberName
4531 MakeName (MemberName class_name)
4532 {
4533         string ns = current_namespace.FullName;
4534
4535         if (current_container.Name == ""){
4536                 if (ns != "")
4537                         return new MemberName (new MemberName (ns), class_name);
4538                 else
4539                         return class_name;
4540         } else {
4541                 return new MemberName (current_container.MemberName, class_name);
4542         }
4543 }
4544
4545 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4546 {
4547         Block implicit_block;
4548         ArrayList inits = null;
4549
4550         //
4551         // We use the `Used' property to check whether statements
4552         // have been added to the current block.  If so, we need
4553         // to create another block to contain the new declaration
4554         // otherwise, as an optimization, we use the same block to
4555         // add the declaration.
4556         //
4557         // FIXME: A further optimization is to check if the statements
4558         // that were added were added as part of the initialization
4559         // below.  In which case, no other statements have been executed
4560         // and we might be able to reduce the number of blocks for
4561         // situations like this:
4562         //
4563         // int j = 1;  int k = j + 1;
4564         //
4565         if (current_block.Used)
4566                 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4567         else
4568                 implicit_block = current_block;
4569
4570         foreach (VariableDeclaration decl in variable_declarators){
4571
4572                 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4573                         if (decl.expression_or_array_initializer != null){
4574                                 if (inits == null)
4575                                         inits = new ArrayList (4);
4576                                 inits.Add (decl);
4577                         }
4578                 }
4579         }
4580
4581         if (inits == null)
4582                 return implicit_block;
4583
4584         foreach (VariableDeclaration decl in inits){
4585                 Assign assign;
4586                 Expression expr;
4587                 
4588                 if (decl.expression_or_array_initializer is Expression){
4589                         expr = (Expression) decl.expression_or_array_initializer;
4590
4591                 } else {
4592                         ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4593                         
4594                         expr = new ArrayCreation (type, "", init, decl.Location);
4595                 }
4596
4597                 LocalVariableReference var;
4598                 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4599
4600                 assign = new Assign (var, expr, decl.Location);
4601
4602                 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4603         }
4604         
4605         return implicit_block;
4606 }
4607
4608 Block declare_local_constants (Expression type, ArrayList declarators)
4609 {
4610         Block implicit_block;
4611
4612         if (current_block.Used)
4613                 implicit_block = new Block (current_block, Block.Flags.Implicit);
4614         else
4615                 implicit_block = current_block;
4616
4617         foreach (VariableDeclaration decl in declarators){
4618                 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4619                                                   current_local_parameters, decl.Location);
4620         }
4621         
4622         return implicit_block;
4623 }
4624
4625 void CheckAttributeTarget (string a)
4626 {
4627         switch (a) {
4628
4629         case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4630                 return;
4631                 
4632         default :
4633                 Location l = lexer.Location;
4634                 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4635                 break;
4636         }
4637
4638 }
4639
4640 void CheckUnaryOperator (Operator.OpType op)
4641 {
4642         switch (op) {
4643                 
4644         case Operator.OpType.LogicalNot: 
4645         case Operator.OpType.OnesComplement: 
4646         case Operator.OpType.Increment:
4647         case Operator.OpType.Decrement:
4648         case Operator.OpType.True: 
4649         case Operator.OpType.False: 
4650         case Operator.OpType.Addition: 
4651         case Operator.OpType.Subtraction:
4652                 
4653                 break;
4654                 
4655         default :
4656                 Location l = lexer.Location;
4657                 Report.Error (1019, l, "Overloadable unary operator expected"); 
4658                 break;
4659                 
4660         }
4661 }
4662
4663 void CheckBinaryOperator (Operator.OpType op)
4664 {
4665         switch (op) {
4666                 
4667         case Operator.OpType.Addition: 
4668         case Operator.OpType.Subtraction: 
4669         case Operator.OpType.Multiply:
4670         case Operator.OpType.Division:
4671         case Operator.OpType.Modulus: 
4672         case Operator.OpType.BitwiseAnd: 
4673         case Operator.OpType.BitwiseOr:
4674         case Operator.OpType.ExclusiveOr: 
4675         case Operator.OpType.LeftShift: 
4676         case Operator.OpType.RightShift:
4677         case Operator.OpType.Equality: 
4678         case Operator.OpType.Inequality:
4679         case Operator.OpType.GreaterThan: 
4680         case Operator.OpType.LessThan: 
4681         case Operator.OpType.GreaterThanOrEqual:
4682         case Operator.OpType.LessThanOrEqual:
4683                 break;
4684                 
4685         default :
4686                 Location l = lexer.Location;
4687                 Report.Error (1020, l, "Overloadable binary operator expected");
4688                 break;
4689         }
4690         
4691 }
4692
4693 void syntax_error (Location l, string msg)
4694 {
4695         Report.Error (1003, l, "Syntax error, " + msg);
4696 }
4697
4698 void output (string s)
4699 {
4700         Console.WriteLine (s);
4701 }
4702
4703 void note (string s)
4704 {
4705         // Used to put annotations
4706 }
4707
4708 Tokenizer lexer;
4709
4710 public Tokenizer Lexer {
4711         get {
4712                 return lexer;
4713         }
4714 }                  
4715
4716 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4717 {
4718         current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4719         this.name = file.Name;
4720         this.file = file;
4721         current_container = RootContext.Tree.Types;
4722         current_container.NamespaceEntry = current_namespace;
4723         oob_stack = new Stack ();
4724         switch_stack = new Stack ();
4725
4726         lexer = new Tokenizer (reader, file, defines);
4727 }
4728
4729 public void parse ()
4730 {
4731         try {
4732                 if (yacc_verbose_flag > 1)
4733                         yyparse (lexer, new yydebug.yyDebugSimple ());
4734                 else
4735                         yyparse (lexer);
4736                 Tokenizer tokenizer = lexer as Tokenizer;
4737                 tokenizer.cleanup ();           
4738         } catch (Exception e){
4739                 // 
4740                 // Removed for production use, use parser verbose to get the output.
4741                 //
4742                 // Console.WriteLine (e);
4743                 Report.Error (-25, lexer.Location, "Parsing error");
4744                 if (yacc_verbose_flag > 0)
4745                         Console.WriteLine (e);
4746         }
4747 }
4748
4749 void CheckToken (int error, int yyToken, string msg)
4750 {
4751         if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4752                 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4753                 return;
4754         }               
4755         Report.Error (error, lexer.Location, msg);
4756 }
4757
4758 void CheckIdentifierToken (int yyToken)
4759 {
4760         CheckToken (1041, yyToken, "Identifier expected");
4761 }
4762
4763 string ConsumeStoredComment ()
4764 {
4765         string s = tmpComment;
4766         tmpComment = null;
4767         Lexer.doc_state = XmlCommentState.Allowed;
4768         return s;
4769 }
4770
4771 /* end end end */
4772 }