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