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