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