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