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