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