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