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