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