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