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