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