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