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