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