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