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