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