*** Merged revisions from mcs: 52039
[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                                       "Contraints 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                                       "Contraints 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", $4);
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           interface_method_declaration_body
1734           {
1735                 lexer.ConstraintsParsing = false;
1736
1737                 MemberName name = (MemberName) $4;
1738
1739                 if ($9 != null && name.TypeArguments == null)
1740                         Report.Error (80, lexer.Location,
1741                                       "Contraints are not allowed on non-generic declarations");
1742
1743                 GenericMethod generic = null;
1744                 if (name.TypeArguments != null) {
1745                         generic = new GenericMethod (current_namespace, current_class, name,
1746                                                      (Expression) $3, (Parameters) $6);
1747
1748                         generic.SetParameterInfo ((ArrayList) $9);
1749                 }
1750
1751                 $$ = new Method (current_class, generic, (Expression) $3, (int) $2, true, name,
1752                                  (Parameters) $6, (Attributes) $1);
1753                 if (RootContext.Documentation != null)
1754                         ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1755           }
1756         | opt_attributes opt_new VOID namespace_or_type_name
1757           OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1758           {
1759                 lexer.ConstraintsParsing = true;
1760           }
1761           opt_type_parameter_constraints_clauses
1762           interface_method_declaration_body
1763           {
1764                 lexer.ConstraintsParsing = false;
1765
1766                 MemberName name = (MemberName) $4;
1767
1768                 if ($9 != null && name.TypeArguments == null)
1769                         Report.Error (80, lexer.Location,
1770                                       "Contraints are not allowed on non-generic declarations");
1771
1772                 GenericMethod generic = null;
1773                 if (name.TypeArguments != null) {
1774                         generic = new GenericMethod (current_namespace, current_class, name,
1775                                                      TypeManager.system_void_expr, (Parameters) $6);
1776
1777                         generic.SetParameterInfo ((ArrayList) $9);
1778                 }
1779
1780                 $$ = new Method (current_class, generic, TypeManager.system_void_expr, (int) $2,
1781                                  true, name, (Parameters) $6, (Attributes) $1);
1782                 if (RootContext.Documentation != null)
1783                         ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1784           }
1785         ;
1786
1787 interface_property_declaration
1788         : opt_attributes
1789           opt_new
1790           type IDENTIFIER 
1791           OPEN_BRACE 
1792           { lexer.PropertyParsing = true; }
1793           accessor_declarations 
1794           {
1795                 has_get = has_set = false; 
1796                 lexer.PropertyParsing = false;
1797           }
1798           CLOSE_BRACE
1799           {
1800                 LocatedToken lt = (LocatedToken) $4;
1801                 MemberName name = new MemberName (lt.Value, lt.Location);
1802
1803                 if ($3 == TypeManager.system_void_expr) {
1804                         Report.Error (547, lt.Location, "`{0}': property or indexer cannot have void type", lt.Value);
1805                         break;
1806                 }
1807
1808                 Property p = null;
1809                 if ($7 == null) {
1810                         p = new Property (current_class, (Expression) $3, (int) $2, true,
1811                                    name, (Attributes) $1,
1812                                    null, null);
1813
1814                         Report.Error (548, p.Location, "`{0}': property or indexer must have at least one accessor", p.GetSignatureForError ());
1815                         break;
1816                 }
1817
1818                 Pair pair = (Pair) $7;
1819                 p = new Property (current_class, (Expression) $3, (int) $2, true,
1820                                    name, (Attributes) $1,
1821                                    (Accessor)pair.First, (Accessor)pair.Second);
1822
1823                 if (pair.First != null && ((Accessor)(pair.First)).Block != null) {
1824                         Report.Error (531, p.Location, "`{0}.get': interface members cannot have a definition", p.GetSignatureForError ());
1825                         $$ = null;
1826                         break;
1827                 }
1828
1829                 if (pair.Second != null && ((Accessor)(pair.Second)).Block != null) {
1830                         Report.Error (531, p.Location, "`{0}.set': interface members cannot have a definition", p.GetSignatureForError ());
1831                         $$ = null;
1832                         break;
1833                 }
1834
1835                 if (RootContext.Documentation != null)
1836                         p.DocComment = Lexer.consume_doc_comment ();
1837
1838                 $$ = p;
1839           }
1840         | opt_attributes
1841           opt_new
1842           type error {
1843                 CheckIdentifierToken (yyToken, GetLocation ($4));
1844                 $$ = null;
1845           }
1846         ;
1847
1848
1849 interface_event_declaration
1850         : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1851           {
1852                 LocatedToken lt = (LocatedToken) $5;
1853                 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1854                                      new MemberName (lt.Value, lt.Location), null,
1855                                      (Attributes) $1);
1856                 if (RootContext.Documentation != null)
1857                         ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1858           }
1859         | opt_attributes opt_new EVENT type error {
1860                 CheckIdentifierToken (yyToken, GetLocation ($5));
1861                 $$ = null;
1862           }
1863         | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN  {
1864                 LocatedToken lt = (LocatedToken) $5;
1865                 Report.Error (68, lt.Location, "`{0}.{1}': event in interface cannot have initializer", current_container.Name, lt.Value);
1866                 $$ = null;
1867           }
1868         | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1869           {
1870                 lexer.EventParsing = true;
1871           }
1872           event_accessor_declarations
1873           {
1874                 lexer.EventParsing = false;
1875           }
1876           CLOSE_BRACE {
1877                 Report.Error (69, (Location) $3, "Event in interface cannot have add or remove accessors");
1878                 $$ = null;
1879           }
1880         ;
1881
1882 interface_indexer_declaration 
1883         : opt_attributes opt_new type THIS 
1884           OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1885           OPEN_BRACE 
1886           { lexer.PropertyParsing = true; }
1887           accessor_declarations 
1888           { 
1889                 has_get = has_set = false;
1890                 lexer.PropertyParsing = false;
1891           }
1892           CLOSE_BRACE
1893           {
1894                 Indexer i = null;
1895                 if ($10 == null) {
1896                         i = new Indexer (current_class, (Expression) $3,
1897                                   new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1898                                   (int) $2, true, (Parameters) $6, (Attributes) $1,
1899                                   null, null);
1900
1901                         Report.Error (548, i.Location, "`{0}': property or indexer must have at least one accessor", i.GetSignatureForError ());
1902                         break;
1903                 }
1904
1905                 Pair pair = (Pair) $10;
1906                 i = new Indexer (current_class, (Expression) $3,
1907                                   new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1908                                   (int) $2, true, (Parameters) $6, (Attributes) $1,
1909                                    (Accessor)pair.First, (Accessor)pair.Second);
1910
1911                 if (pair.First != null && ((Accessor)(pair.First)).Block != null) {
1912                         Report.Error (531, i.Location, "`{0}.get': interface members cannot have a definition", i.GetSignatureForError ());
1913                         $$ = null;
1914                         break;
1915                 }
1916
1917                 if (pair.Second != null && ((Accessor)(pair.Second)).Block != null) {
1918                         Report.Error (531, i.Location, "`{0}.set': interface members cannot have a definition", i.GetSignatureForError ());
1919                         $$ = null;
1920                         break;
1921                 }
1922
1923                 if (RootContext.Documentation != null)
1924                         i.DocComment = ConsumeStoredComment ();
1925
1926                 $$ = i;
1927           }
1928         ;
1929
1930 operator_declaration
1931         : opt_attributes opt_modifiers operator_declarator 
1932           {
1933                 iterator_container = SimpleIteratorContainer.GetSimple ();
1934           }
1935           operator_body
1936           {
1937                 if ($3 == null)
1938                         break;
1939
1940                 OperatorDeclaration decl = (OperatorDeclaration) $3;
1941                 
1942                 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1943
1944                 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null, decl.location);
1945                 if (decl.arg2type != null)
1946                         param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null, decl.location);
1947
1948                 Operator op = new Operator (
1949                         current_class, decl.optype, decl.ret_type, (int) $2, 
1950                         new Parameters (param_list, null),
1951                         (ToplevelBlock) $5, (Attributes) $1, decl.location);
1952
1953                 if (RootContext.Documentation != null) {
1954                         op.DocComment = tmpComment;
1955                         Lexer.doc_state = XmlCommentState.Allowed;
1956                 }
1957
1958                 if (SimpleIteratorContainer.Simple.Yields)
1959                         op.SetYields ();
1960
1961                 // Note again, checking is done in semantic analysis
1962                 current_container.AddOperator (op);
1963
1964                 current_local_parameters = null;
1965                 iterator_container = null;
1966           }
1967         ;
1968
1969 operator_body 
1970         : block
1971         | SEMICOLON { $$ = null; }
1972         ; 
1973 operator_declarator
1974         : type OPERATOR overloadable_operator 
1975           OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1976           {
1977                 LocatedToken lt = (LocatedToken) $6;
1978                 Operator.OpType op = (Operator.OpType) $3;
1979                 CheckUnaryOperator (op, lt.Location);
1980
1981                 if (op == Operator.OpType.Addition)
1982                         op = Operator.OpType.UnaryPlus;
1983
1984                 if (op == Operator.OpType.Subtraction)
1985                         op = Operator.OpType.UnaryNegation;
1986
1987                 Parameter [] pars = new Parameter [1];
1988                 Expression type = (Expression) $5;
1989
1990                 pars [0] = new Parameter (type, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1991
1992                 current_local_parameters = new Parameters (pars, null);
1993
1994                 if (RootContext.Documentation != null) {
1995                         tmpComment = Lexer.consume_doc_comment ();
1996                         Lexer.doc_state = XmlCommentState.NotAllowed;
1997                 }
1998
1999                 $$ = new OperatorDeclaration (op, (Expression) $1, type, lt.Value,
2000                                               null, null, (Location) $2);
2001           }
2002         | type OPERATOR overloadable_operator
2003           OPEN_PARENS 
2004                 type IDENTIFIER COMMA
2005                 type IDENTIFIER 
2006           CLOSE_PARENS
2007           {
2008                 LocatedToken ltParam1 = (LocatedToken) $6;
2009                 LocatedToken ltParam2 = (LocatedToken) $9;
2010                 CheckBinaryOperator ((Operator.OpType) $3, (Location) $2);
2011
2012                 Parameter [] pars = new Parameter [2];
2013
2014                 Expression typeL = (Expression) $5;
2015                 Expression typeR = (Expression) $8;
2016
2017                pars [0] = new Parameter (typeL, ltParam1.Value, Parameter.Modifier.NONE, null, ltParam1.Location);
2018                pars [1] = new Parameter (typeR, ltParam2.Value, Parameter.Modifier.NONE, null, ltParam2.Location);
2019
2020                current_local_parameters = new Parameters (pars, null);
2021
2022                 if (RootContext.Documentation != null) {
2023                         tmpComment = Lexer.consume_doc_comment ();
2024                         Lexer.doc_state = XmlCommentState.NotAllowed;
2025                 }
2026                
2027                $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1, 
2028                                              typeL, ltParam1.Value,
2029                                              typeR, ltParam2.Value, (Location) $2);
2030           }
2031         | conversion_operator_declarator
2032         | type OPERATOR overloadable_operator
2033           OPEN_PARENS 
2034                 type IDENTIFIER COMMA
2035                 type IDENTIFIER COMMA
2036                 type IDENTIFIER
2037           CLOSE_PARENS
2038           {
2039                 Report.Error (1534, (Location) $2, "Overloaded binary operator `{0}' takes two parameters",
2040                         Operator.GetName ((Operator.OpType) $3));
2041                 $$ = null;
2042           }
2043         | type OPERATOR overloadable_operator 
2044           OPEN_PARENS CLOSE_PARENS
2045           {
2046                 Report.Error (1535, (Location) $2, "Overloaded unary operator `{0}' takes one parameter",
2047                         Operator.GetName ((Operator.OpType) $3));
2048                 $$ = null;
2049           }
2050         ;
2051
2052 overloadable_operator
2053 // Unary operators:
2054         : BANG   { $$ = Operator.OpType.LogicalNot; }
2055         | TILDE  { $$ = Operator.OpType.OnesComplement; }  
2056         | OP_INC { $$ = Operator.OpType.Increment; }
2057         | OP_DEC { $$ = Operator.OpType.Decrement; }
2058         | TRUE   { $$ = Operator.OpType.True; }
2059         | FALSE  { $$ = Operator.OpType.False; }
2060 // Unary and binary:
2061         | PLUS { $$ = Operator.OpType.Addition; }
2062         | MINUS { $$ = Operator.OpType.Subtraction; }
2063 // Binary:
2064         | STAR { $$ = Operator.OpType.Multiply; }
2065         | DIV {  $$ = Operator.OpType.Division; }
2066         | PERCENT { $$ = Operator.OpType.Modulus; }
2067         | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
2068         | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
2069         | CARRET { $$ = Operator.OpType.ExclusiveOr; }
2070         | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
2071         | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
2072         | OP_EQ { $$ = Operator.OpType.Equality; }
2073         | OP_NE { $$ = Operator.OpType.Inequality; }
2074         | OP_GT { $$ = Operator.OpType.GreaterThan; }
2075         | OP_LT { $$ = Operator.OpType.LessThan; }
2076         | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
2077         | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
2078         ;
2079
2080 conversion_operator_declarator
2081         : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
2082           {
2083                 LocatedToken lt = (LocatedToken) $6;
2084                 Parameter [] pars = new Parameter [1];
2085
2086                 pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2087
2088                 current_local_parameters = new Parameters (pars, null);  
2089                   
2090                 if (RootContext.Documentation != null) {
2091                         tmpComment = Lexer.consume_doc_comment ();
2092                         Lexer.doc_state = XmlCommentState.NotAllowed;
2093                 }
2094
2095                 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, lt.Value,
2096                                               null, null, (Location) $2);
2097           }
2098         | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
2099           {
2100                 LocatedToken lt = (LocatedToken) $6;
2101                 Parameter [] pars = new Parameter [1];
2102
2103                 pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2104
2105                 current_local_parameters = new Parameters (pars, null);  
2106                   
2107                 if (RootContext.Documentation != null) {
2108                         tmpComment = Lexer.consume_doc_comment ();
2109                         Lexer.doc_state = XmlCommentState.NotAllowed;
2110                 }
2111
2112                 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, lt.Value,
2113                                               null, null, (Location) $2);
2114           }
2115         | IMPLICIT error 
2116           {
2117                 syntax_error ((Location) $1, "'operator' expected");
2118           }
2119         | EXPLICIT error 
2120           {
2121                 syntax_error ((Location) $1, "'operator' expected");
2122           }
2123         ;
2124
2125 constructor_declaration
2126         : opt_attributes
2127           opt_modifiers
2128           constructor_declarator
2129           constructor_body
2130           { 
2131                 Constructor c = (Constructor) $3;
2132                 c.Block = (ToplevelBlock) $4;
2133                 c.OptAttributes = (Attributes) $1;
2134                 c.ModFlags = (int) $2;
2135         
2136                 if (RootContext.Documentation != null)
2137                         c.DocComment = ConsumeStoredComment ();
2138
2139                 if (c.Name == current_container.Basename){
2140                         if ((c.ModFlags & Modifiers.STATIC) != 0){
2141                                 if ((c.ModFlags & Modifiers.Accessibility) != 0){
2142                                         Report.Error (515, c.Location,
2143                                                 "`{0}': access modifiers are not allowed on static constructors",
2144                                                 c.GetSignatureForError ());
2145                                 }
2146         
2147                                 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);   
2148         
2149                                 if (c.Initializer != null){
2150                                         Report.Error (514, c.Location,
2151                                                 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2152                                                 c.GetSignatureForError ());
2153                                 }
2154         
2155                                 if (!c.Parameters.Empty){
2156                                         Report.Error (132, c.Location,
2157                                                 "`{0}': The static constructor must be parameterless", c.GetSignatureForError ());
2158                                 }
2159                         } else {
2160                                 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2161                         }
2162                 } else {
2163                         // We let another layer check the validity of the constructor.
2164                         //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
2165                 }
2166
2167                 current_container.AddConstructor (c);
2168
2169                 current_local_parameters = null;
2170                 if (RootContext.Documentation != null)
2171                         Lexer.doc_state = XmlCommentState.Allowed;
2172           }
2173         ;
2174
2175 constructor_declarator
2176         : IDENTIFIER
2177           {
2178                 if (RootContext.Documentation != null) {
2179                         tmpComment = Lexer.consume_doc_comment ();
2180                         Lexer.doc_state = XmlCommentState.Allowed;
2181                 }
2182           }
2183           OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2184           {
2185                 current_local_parameters = (Parameters) $4;
2186           }
2187           opt_constructor_initializer
2188           {
2189                 LocatedToken lt = (LocatedToken) $1;
2190                 $$ = new Constructor (current_class, lt.Value, 0, (Parameters) $4,
2191                                       (ConstructorInitializer) $7, lt.Location);
2192           }
2193         ;
2194
2195 constructor_body
2196         : block
2197         | SEMICOLON             { $$ = null; }
2198         ;
2199
2200 opt_constructor_initializer
2201         : /* empty */                   { $$ = null; }
2202         | constructor_initializer
2203         ;
2204
2205 constructor_initializer
2206         : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
2207           {
2208                 $$ = new ConstructorBaseInitializer ((ArrayList) $4, (Location) $2);
2209           }
2210         | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
2211           {
2212                 $$ = new ConstructorThisInitializer ((ArrayList) $4, (Location) $2);
2213           }
2214         | COLON error {
2215                 Report.Error (1018, (Location) $1, "Keyword this or base expected");
2216                 $$ = null;
2217           }
2218         ;
2219
2220 opt_finalizer
2221         : /* EMPTY */           { $$ = 0; }
2222         | UNSAFE                { $$ = Modifiers.UNSAFE; }
2223         | EXTERN                { $$ = Modifiers.EXTERN; }
2224         ;
2225         
2226 destructor_declaration
2227         : opt_attributes opt_finalizer TILDE 
2228           {
2229                 if (RootContext.Documentation != null) {
2230                         tmpComment = Lexer.consume_doc_comment ();
2231                         Lexer.doc_state = XmlCommentState.NotAllowed;
2232                 }
2233           }
2234           IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2235           {
2236                 LocatedToken lt = (LocatedToken) $5;
2237                 if (lt.Value != current_container.MemberName.Name){
2238                         Report.Error (574, lt.Location, "Name of destructor must match name of class");
2239                 } else if (current_container.Kind != Kind.Class){
2240                         Report.Error (575, lt.Location, "Only class types can contain destructor");
2241                 } else {
2242                         Location l = lt.Location;
2243
2244                         int m = (int) $2;
2245                         if (!RootContext.StdLib && current_container.Name == "System.Object")
2246                                 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2247                         else
2248                                 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2249                         
2250                         Method d = new Destructor (
2251                                 current_class, TypeManager.system_void_expr, m, "Finalize", 
2252                                 new Parameters (null, null), (Attributes) $1, l);
2253                         if (RootContext.Documentation != null)
2254                                 d.DocComment = ConsumeStoredComment ();
2255                   
2256                         d.Block = (ToplevelBlock) $8;
2257                         current_container.AddMethod (d);
2258                 }
2259           }
2260         ;
2261
2262 event_declaration
2263         : opt_attributes
2264           opt_modifiers
2265           EVENT type variable_declarators SEMICOLON
2266           {
2267                 foreach (VariableDeclaration var in (ArrayList) $5) {
2268
2269                         MemberName name = new MemberName (var.identifier,
2270                                 var.Location);
2271
2272                         Event e = new EventField (
2273                                 current_class, (Expression) $4, (int) $2, false, name,
2274                                 var.expression_or_array_initializer, (Attributes) $1);
2275
2276                         current_container.AddEvent (e);
2277
2278                         if (RootContext.Documentation != null) {
2279                                 e.DocComment = Lexer.consume_doc_comment ();
2280                                 Lexer.doc_state = XmlCommentState.Allowed;
2281                         }
2282                 }
2283           }
2284         | opt_attributes
2285           opt_modifiers
2286           EVENT type namespace_or_type_name
2287           OPEN_BRACE
2288           {
2289                 implicit_value_parameter_type = (Expression) $4;  
2290                 lexer.EventParsing = true;
2291           }
2292           event_accessor_declarations
2293           {
2294                 lexer.EventParsing = false;  
2295           }
2296           CLOSE_BRACE
2297           {
2298                 MemberName name = (MemberName) $5;
2299
2300                 if ($8 == null){
2301                         Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2302                                 current_container.Name, name);
2303                         $$ = null;
2304                 } else {
2305                         Pair pair = (Pair) $8;
2306                         
2307                         if (name.TypeArguments != null)
2308                                 syntax_error (lexer.Location, "an event can't have type arguments");
2309
2310                         if (pair.First == null || pair.Second == null)
2311                                 // CS0073 is already reported, so no CS0065 here.
2312                                 $$ = null;
2313                         else {
2314                                 Event e = new EventProperty (
2315                                         current_class, (Expression) $4, (int) $2, false, name, null,
2316                                         (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second);
2317                                 if (RootContext.Documentation != null) {
2318                                         e.DocComment = Lexer.consume_doc_comment ();
2319                                         Lexer.doc_state = XmlCommentState.Allowed;
2320                                 }
2321
2322                                 current_container.AddEvent (e);
2323                                 implicit_value_parameter_type = null;
2324                         }
2325                 }
2326           }
2327         | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
2328                 MemberName mn = (MemberName) $5;
2329
2330                 if (mn.Left != null)
2331                         Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2332                 else 
2333                         Report.Error (71, mn.Location, "Event declaration should use property syntax");
2334
2335                 if (RootContext.Documentation != null)
2336                         Lexer.doc_state = XmlCommentState.Allowed;
2337           }
2338         ;
2339
2340 event_accessor_declarations
2341         : add_accessor_declaration remove_accessor_declaration
2342           {
2343                 $$ = new Pair ($1, $2);
2344           }
2345         | remove_accessor_declaration add_accessor_declaration
2346           {
2347                 $$ = new Pair ($2, $1);
2348           }     
2349         | add_accessor_declaration  { $$ = null; } 
2350         | remove_accessor_declaration { $$ = null; } 
2351         | error
2352           { 
2353                 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2354                 $$ = null;
2355           }
2356         | { $$ = null; }
2357         ;
2358
2359 add_accessor_declaration
2360         : opt_attributes ADD
2361           {
2362                 Parameter [] args = new Parameter [1];
2363                 Parameter implicit_value_parameter = new Parameter (
2364                         implicit_value_parameter_type, "value", 
2365                         Parameter.Modifier.NONE, null, (Location) $2);
2366
2367                 args [0] = implicit_value_parameter;
2368                 
2369                 current_local_parameters = new Parameters (args, null);  
2370                 lexer.EventParsing = false;
2371           }
2372           block
2373           {
2374                 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2375                 lexer.EventParsing = true;
2376           }
2377         | opt_attributes ADD error {
2378                 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2379                 $$ = null;
2380           }
2381         | opt_attributes modifiers ADD {
2382                 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2383                 $$ = null;
2384           }
2385         ;
2386
2387 remove_accessor_declaration
2388         : opt_attributes REMOVE
2389           {
2390                 Parameter [] args = new Parameter [1];
2391                 Parameter implicit_value_parameter = new Parameter (
2392                         implicit_value_parameter_type, "value", 
2393                         Parameter.Modifier.NONE, null, (Location) $2);
2394
2395                 args [0] = implicit_value_parameter;
2396                 
2397                 current_local_parameters = new Parameters (args, null);  
2398                 lexer.EventParsing = false;
2399           }
2400           block
2401           {
2402                 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2403                 lexer.EventParsing = true;
2404           }
2405         | opt_attributes REMOVE error {
2406                 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2407                 $$ = null;
2408           }
2409         | opt_attributes modifiers REMOVE {
2410                 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2411                 $$ = null;
2412           }
2413         ;
2414
2415 indexer_declaration
2416         : opt_attributes opt_modifiers indexer_declarator 
2417           OPEN_BRACE
2418           {
2419                 IndexerDeclaration decl = (IndexerDeclaration) $3;
2420
2421                 implicit_value_parameter_type = decl.type;
2422                 
2423                 lexer.PropertyParsing = true;
2424                 parsing_indexer  = true;
2425                 
2426                 indexer_parameters = decl.param_list;
2427                 iterator_container = SimpleIteratorContainer.GetSimple ();
2428           }
2429           accessor_declarations 
2430           {
2431                   lexer.PropertyParsing = false;
2432                   has_get = has_set = false;
2433                   parsing_indexer  = false;
2434           }
2435           CLOSE_BRACE
2436           { 
2437                 if ($6 == null)
2438                         break;
2439
2440                 // The signature is computed from the signature of the indexer.  Look
2441                 // at section 3.6 on the spec
2442                 Indexer indexer;
2443                 IndexerDeclaration decl = (IndexerDeclaration) $3;
2444                 Pair pair = (Pair) $6;
2445                 Accessor get_block = (Accessor) pair.First;
2446                 Accessor set_block = (Accessor) pair.Second;
2447
2448                 MemberName name;
2449                 if (decl.interface_type != null)
2450                         name = new MemberName (
2451                                 decl.interface_type, TypeContainer.DefaultIndexerName, decl.location);
2452                 else
2453                         name = new MemberName (TypeContainer.DefaultIndexerName, decl.location);
2454
2455                 indexer = new Indexer (current_class, decl.type, name,
2456                                        (int) $2, false, decl.param_list, (Attributes) $1,
2457                                        get_block, set_block);
2458
2459                 if (RootContext.Documentation != null)
2460                         indexer.DocComment = ConsumeStoredComment ();
2461
2462                 current_container.AddIndexer (indexer);
2463                 
2464                 current_local_parameters = null;
2465                 implicit_value_parameter_type = null;
2466                 indexer_parameters = null;
2467           }
2468         ;
2469
2470 indexer_declarator
2471         : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2472           {
2473                 Parameters pars = (Parameters) $4;
2474                 if (pars.HasArglist) {
2475                         // "__arglist is not valid in this context"
2476                         Report.Error (1669, (Location) $2, "__arglist is not valid in this context");
2477                 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2478                         Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
2479                 }
2480                 if (RootContext.Documentation != null) {
2481                         tmpComment = Lexer.consume_doc_comment ();
2482                         Lexer.doc_state = XmlCommentState.Allowed;
2483                 }
2484
2485                 $$ = new IndexerDeclaration ((Expression) $1, null, pars, (Location) $2);
2486           }
2487         | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2488           {
2489                 Parameters pars = (Parameters) $6;
2490
2491                 if (pars.HasArglist) {
2492                         // "__arglist is not valid in this context"
2493                         Report.Error (1669, (Location) $4, "__arglist is not valid in this context");
2494                 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2495                         Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
2496                 }
2497
2498                 MemberName name = (MemberName) $2;
2499                 $$ = new IndexerDeclaration ((Expression) $1, name, pars, (Location) $4);
2500
2501                 if (RootContext.Documentation != null) {
2502                         tmpComment = Lexer.consume_doc_comment ();
2503                         Lexer.doc_state = XmlCommentState.Allowed;
2504                 }
2505           }
2506         ;
2507
2508 enum_declaration
2509         : opt_attributes
2510           opt_modifiers
2511           opt_partial
2512           ENUM IDENTIFIER 
2513           opt_enum_base {
2514                 if (RootContext.Documentation != null)
2515                         enumTypeComment = Lexer.consume_doc_comment ();
2516           }
2517           enum_body
2518           opt_semicolon
2519           {
2520                 LocatedToken lt = (LocatedToken) $5;
2521                 Location enum_location = lt.Location;
2522
2523                 if ($3 != null) {
2524                         Report.Error (267, lt.Location, "The partial modifier can only appear immediately before `class', `struct' or `interface'");
2525                         break;  // assumes that the parser put us in a switch
2526                 }
2527
2528                 MemberName name = MakeName (new MemberName (lt.Value, enum_location));
2529                 Enum e = new Enum (current_namespace, current_class, (Expression) $6, (int) $2,
2530                                    name, (Attributes) $1);
2531                 
2532                 if (RootContext.Documentation != null)
2533                         e.DocComment = enumTypeComment;
2534
2535
2536                 EnumMember em = null;
2537                 foreach (VariableDeclaration ev in (ArrayList) $8) {
2538                         em = new EnumMember (e, em, (Expression) ev.expression_or_array_initializer,
2539                                 new MemberName (ev.identifier, ev.Location), ev.OptAttributes);
2540
2541 //                      if (RootContext.Documentation != null)
2542                                 em.DocComment = ev.DocComment;
2543
2544                         e.AddEnumMember (em);
2545                 }
2546
2547                 current_container.AddEnum (e);
2548                 RootContext.Tree.RecordDecl (current_namespace.NS, name, e);
2549                 $$ = e;
2550
2551           }
2552         ;
2553
2554 opt_enum_base
2555         : /* empty */           { $$ = TypeManager.system_int32_expr; }
2556         | COLON type            { $$ = $2;   }
2557         ;
2558
2559 enum_body
2560         : OPEN_BRACE
2561           {
2562                 if (RootContext.Documentation != null)
2563                         Lexer.doc_state = XmlCommentState.Allowed;
2564           }
2565           opt_enum_member_declarations
2566           {
2567                 // here will be evaluated after CLOSE_BLACE is consumed.
2568                 if (RootContext.Documentation != null)
2569                         Lexer.doc_state = XmlCommentState.Allowed;
2570           }
2571           CLOSE_BRACE
2572           {
2573                 $$ = $3;
2574           }
2575         ;
2576
2577 opt_enum_member_declarations
2578         : /* empty */                   { $$ = new ArrayList (4); }
2579         | enum_member_declarations opt_comma { $$ = $1; }
2580         ;
2581
2582 enum_member_declarations
2583         : enum_member_declaration 
2584           {
2585                 ArrayList l = new ArrayList (4);
2586
2587                 l.Add ($1);
2588                 $$ = l;
2589           }
2590         | enum_member_declarations COMMA enum_member_declaration
2591           {
2592                 ArrayList l = (ArrayList) $1;
2593
2594                 l.Add ($3);
2595
2596                 $$ = l;
2597           }
2598         ;
2599
2600 enum_member_declaration
2601         : opt_attributes IDENTIFIER 
2602           {
2603                 VariableDeclaration vd = new VariableDeclaration (
2604                         (LocatedToken) $2, null, (Attributes) $1);
2605
2606                 if (RootContext.Documentation != null) {
2607                         vd.DocComment = Lexer.consume_doc_comment ();
2608                         Lexer.doc_state = XmlCommentState.Allowed;
2609                 }
2610
2611                 $$ = vd;
2612           }
2613         | opt_attributes IDENTIFIER
2614           {
2615                 if (RootContext.Documentation != null) {
2616                         tmpComment = Lexer.consume_doc_comment ();
2617                         Lexer.doc_state = XmlCommentState.NotAllowed;
2618                 }
2619           }
2620           ASSIGN expression
2621           { 
2622                 VariableDeclaration vd = new VariableDeclaration (
2623                         (LocatedToken) $2, $5, (Attributes) $1);
2624
2625                 if (RootContext.Documentation != null)
2626                         vd.DocComment = ConsumeStoredComment ();
2627
2628                 $$ = vd;
2629           }
2630         ;
2631
2632 delegate_declaration
2633         : opt_attributes
2634           opt_modifiers
2635           DELEGATE
2636           {
2637                 lexer.ConstraintsParsing = true;
2638           }
2639           type member_name
2640           OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2641           {
2642                 MemberName name = MakeName ((MemberName) $6);
2643                 Delegate del = new Delegate (current_namespace, current_class, (Expression) $5,
2644                                              (int) $2, name, (Parameters) $8, (Attributes) $1);
2645
2646                 if (RootContext.Documentation != null) {
2647                         del.DocComment = Lexer.consume_doc_comment ();
2648                         Lexer.doc_state = XmlCommentState.Allowed;
2649                 }
2650
2651                 current_container.AddDelegate (del);
2652                 RootContext.Tree.RecordDecl (current_namespace.NS, name, del);
2653
2654                 current_delegate = del;
2655           }
2656           opt_type_parameter_constraints_clauses
2657           {
2658                 lexer.ConstraintsParsing = false;
2659           }
2660           SEMICOLON
2661           {
2662                 current_delegate.SetParameterInfo ((ArrayList) $11);
2663                 $$ = current_delegate;
2664
2665                 current_delegate = null;
2666           }
2667         ;
2668
2669 opt_nullable
2670         : /* empty */
2671           {
2672                 lexer.CheckNullable (false);
2673                 $$ = false;
2674           }
2675         | INTERR
2676           {
2677                 lexer.CheckNullable (true);
2678                 $$ = true;
2679           }
2680         ;
2681
2682 namespace_or_type_name
2683         : member_name
2684         | IDENTIFIER DOUBLE_COLON IDENTIFIER {
2685                 LocatedToken lt1 = (LocatedToken) $1;
2686                 LocatedToken lt2 = (LocatedToken) $3;
2687                 $$ = new MemberName (lt1.Value, lt2.Value, lt2.Location);
2688           }
2689         | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list {
2690                 LocatedToken lt = (LocatedToken) $3;
2691                 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $4, lt.Location);
2692           }
2693         ;
2694
2695 member_name
2696         : IDENTIFIER opt_type_argument_list {
2697                 LocatedToken lt = (LocatedToken) $1;
2698                 $$ = new MemberName (lt.Value, (TypeArguments) $2, lt.Location);
2699           }
2700         ;
2701
2702 opt_type_argument_list
2703         : /* empty */                { $$ = null; } 
2704         | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2705           {
2706                 $$ = $2;
2707                 if (RootContext.Version == LanguageVersion.ISO_1)
2708                         Report.FeatureIsNotStandardized (lexer.Location, "generics");
2709           }
2710         | GENERIC_DIMENSION
2711           {
2712                 $$ = new TypeArguments ((int) $1, lexer.Location);
2713                 if (RootContext.Version == LanguageVersion.ISO_1)
2714                         Report.FeatureIsNotStandardized (lexer.Location, "generics");
2715           }
2716         ;
2717
2718 type_arguments
2719         : type {
2720                 TypeArguments type_args = new TypeArguments (lexer.Location);
2721                 type_args.Add ((Expression) $1);
2722                 $$ = type_args;
2723           }
2724         | type_arguments COMMA type {
2725                 TypeArguments type_args = (TypeArguments) $1;
2726                 type_args.Add ((Expression) $3);
2727                 $$ = type_args;
2728           }
2729         ;
2730         
2731 /* 
2732  * Before you think of adding a return_type, notice that we have been
2733  * using two rules in the places where it matters (one rule using type
2734  * and another identical one that uses VOID as the return type).  This
2735  * gets rid of a shift/reduce couple
2736  */
2737 type
2738         : namespace_or_type_name opt_nullable
2739           {
2740                 MemberName name = (MemberName) $1;
2741                 $$ = name.GetTypeExpression ();
2742
2743                 if ((bool) $2)
2744                         $$ = new ComposedCast ((Expression) $$, "?", lexer.Location);
2745           }
2746         | builtin_types opt_nullable
2747           {
2748                 if ((bool) $2)
2749                         $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2750           }
2751         | array_type
2752         | pointer_type
2753         ;
2754
2755 pointer_type
2756         : type STAR
2757           {
2758                 //
2759                 // Note that here only unmanaged types are allowed but we
2760                 // can't perform checks during this phase - we do it during
2761                 // semantic analysis.
2762                 //
2763                 $$ = new ComposedCast ((Expression) $1, "*", Lexer.Location);
2764           }
2765         | VOID STAR
2766           {
2767                 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2768           }
2769         ;
2770
2771 non_expression_type
2772         : builtin_types opt_nullable
2773           {
2774                 if ((bool) $2)
2775                         $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2776           }
2777         | non_expression_type rank_specifier
2778           {
2779                 Location loc = GetLocation ($1);
2780                 if (loc.IsNull)
2781                         loc = lexer.Location;
2782                 $$ = new ComposedCast ((Expression) $1, (string) $2, loc);
2783           }
2784         | non_expression_type STAR
2785           {
2786                 Location loc = GetLocation ($1);
2787                 if (loc.IsNull)
2788                         loc = lexer.Location;
2789                 $$ = new ComposedCast ((Expression) $1, "*", loc);
2790           }
2791         | expression rank_specifiers 
2792           {
2793                 $$ = new ComposedCast ((Expression) $1, (string) $2);
2794           }
2795         | expression STAR 
2796           {
2797                 $$ = new ComposedCast ((Expression) $1, "*");
2798           }
2799         
2800         //
2801         // We need this because the parser will happily go and reduce IDENTIFIER STAR
2802         // through this different path
2803         //
2804         | multiplicative_expression STAR 
2805           {
2806                 $$ = new ComposedCast ((Expression) $1, "*");
2807           }
2808         ;
2809
2810 type_list
2811         : type
2812           {
2813                 ArrayList types = new ArrayList (4);
2814
2815                 types.Add ($1);
2816                 $$ = types;
2817           }
2818         | type_list COMMA type
2819           {
2820                 ArrayList types = (ArrayList) $1;
2821
2822                 types.Add ($3);
2823                 $$ = types;
2824           }
2825         ;
2826
2827 /*
2828  * replaces all the productions for isolating the various
2829  * simple types, but we need this to reuse it easily in local_variable_type
2830  */
2831 builtin_types
2832         : OBJECT        { $$ = TypeManager.system_object_expr; }
2833         | STRING        { $$ = TypeManager.system_string_expr; }
2834         | BOOL          { $$ = TypeManager.system_boolean_expr; }
2835         | DECIMAL       { $$ = TypeManager.system_decimal_expr; }
2836         | FLOAT         { $$ = TypeManager.system_single_expr; }
2837         | DOUBLE        { $$ = TypeManager.system_double_expr; }
2838         | integral_type
2839         ;
2840
2841 integral_type
2842         : SBYTE         { $$ = TypeManager.system_sbyte_expr; }
2843         | BYTE          { $$ = TypeManager.system_byte_expr; }
2844         | SHORT         { $$ = TypeManager.system_int16_expr; }
2845         | USHORT        { $$ = TypeManager.system_uint16_expr; }
2846         | INT           { $$ = TypeManager.system_int32_expr; }
2847         | UINT          { $$ = TypeManager.system_uint32_expr; }
2848         | LONG          { $$ = TypeManager.system_int64_expr; }
2849         | ULONG         { $$ = TypeManager.system_uint64_expr; }
2850         | CHAR          { $$ = TypeManager.system_char_expr; }
2851         | VOID          { $$ = TypeManager.system_void_expr; }
2852         ;
2853
2854 array_type
2855         : type rank_specifiers opt_nullable
2856           {
2857                 string rank_specifiers = (string) $2;
2858                 if ((bool) $3)
2859                         rank_specifiers += "?";
2860
2861                 $$ = new ComposedCast ((Expression) $1, rank_specifiers);
2862           }
2863         ;
2864
2865 //
2866 // Expressions, section 7.5
2867 //
2868 primary_expression
2869         : literal
2870           {
2871                 // 7.5.1: Literals
2872           }
2873         | member_name
2874           {
2875                 MemberName mn = (MemberName) $1;
2876                 $$ = mn.GetTypeExpression ();
2877           }
2878         | IDENTIFIER DOUBLE_COLON IDENTIFIER
2879           {
2880                 LocatedToken lt1 = (LocatedToken) $1;
2881                 LocatedToken lt2 = (LocatedToken) $3;
2882                 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, lt2.Location);
2883           }
2884         | parenthesized_expression
2885         | default_value_expression
2886         | member_access
2887         | invocation_expression
2888         | element_access
2889         | this_access
2890         | base_access
2891         | post_increment_expression
2892         | post_decrement_expression
2893         | new_expression
2894         | typeof_expression
2895         | sizeof_expression
2896         | checked_expression
2897         | unchecked_expression
2898         | pointer_member_access
2899         | anonymous_method_expression
2900         ;
2901
2902 literal
2903         : boolean_literal
2904         | integer_literal
2905         | real_literal
2906         | LITERAL_CHARACTER     { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
2907         | LITERAL_STRING        { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); } 
2908         | NULL                  { $$ = new NullLiteral (lexer.Location); }
2909         ;
2910
2911 real_literal
2912         : LITERAL_FLOAT         { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
2913         | LITERAL_DOUBLE        { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
2914         | LITERAL_DECIMAL       { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
2915         ;
2916
2917 integer_literal
2918         : LITERAL_INTEGER       { 
2919                 object v = lexer.Value;
2920
2921                 if (v is int){
2922                         $$ = new IntLiteral ((int) v, lexer.Location);
2923                 } else if (v is uint)
2924                         $$ = new UIntLiteral ((UInt32) v, lexer.Location);
2925                 else if (v is long)
2926                         $$ = new LongLiteral ((Int64) v, lexer.Location);
2927                 else if (v is ulong)
2928                         $$ = new ULongLiteral ((UInt64) v, lexer.Location);
2929                 else
2930                         Console.WriteLine ("OOPS.  Unexpected result from scanner");
2931           }
2932         ;
2933
2934 boolean_literal
2935         : TRUE                  { $$ = new BoolLiteral (true, lexer.Location); }
2936         | FALSE                 { $$ = new BoolLiteral (false, lexer.Location); }
2937         ;
2938
2939 parenthesized_expression_0
2940         : OPEN_PARENS expression CLOSE_PARENS
2941           {
2942                 $$ = $2;
2943                 lexer.Deambiguate_CloseParens ();
2944                 // After this, the next token returned is one of
2945                 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2946                 // or CLOSE_PARENS_MINUS.
2947           }
2948         ;
2949
2950 parenthesized_expression
2951         : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2952           {
2953                 $$ = $1;
2954           }
2955         | parenthesized_expression_0 CLOSE_PARENS_MINUS
2956           {
2957                 // If a parenthesized expression is followed by a minus, we need to wrap
2958                 // the expression inside a ParenthesizedExpression for the CS0075 check
2959                 // in Binary.DoResolve().
2960                 $$ = new ParenthesizedExpression ((Expression) $1);
2961           }
2962         ;;
2963
2964 member_access
2965         : primary_expression DOT IDENTIFIER opt_type_argument_list
2966           {
2967                 LocatedToken lt = (LocatedToken) $3;
2968                 $$ = new MemberAccess ((Expression) $1, lt.Value,
2969                                        (TypeArguments) $4, lt.Location);
2970           }
2971         | predefined_type DOT IDENTIFIER opt_type_argument_list
2972           {
2973                 LocatedToken lt = (LocatedToken) $3;
2974                 $$ = new MemberAccess ((Expression) $1, lt.Value,
2975                                        (TypeArguments) $4, lt.Location);
2976           }
2977         ;
2978
2979 predefined_type
2980         : builtin_types
2981         ;
2982
2983 invocation_expression
2984         : primary_expression {
2985                 $$ = lexer.Location;
2986           } OPEN_PARENS opt_argument_list CLOSE_PARENS
2987           {
2988                 if ($1 == null) {
2989                         Location l = (Location) $3;
2990                         Report.Error (1, l, "Parse error");
2991                 }
2992                 $$ = new Invocation ((Expression) $1, (ArrayList) $4);
2993           }
2994         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2995           {
2996                 $$ = new Invocation ((Expression) $1, new ArrayList ());
2997           }
2998         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2999           {
3000                 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
3001           }
3002         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
3003           {
3004                 ArrayList args = new ArrayList (1);
3005                 args.Add ($4);
3006                 $$ = new Invocation ((Expression) $1, args);
3007           }
3008         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
3009           {
3010                 ArrayList args = ((ArrayList) $4);
3011                 args.Add ($6);
3012                 $$ = new Invocation ((Expression) $1, args);
3013           }
3014         ;
3015
3016 opt_argument_list
3017         : /* empty */           { $$ = null; }
3018         | argument_list
3019         ;
3020
3021 argument_list
3022         : argument              
3023           { 
3024                 ArrayList list = new ArrayList (4);
3025                 list.Add ($1);
3026                 $$ = list;
3027           }
3028         | argument_list COMMA argument
3029           {
3030                 ArrayList list = (ArrayList) $1;
3031                 list.Add ($3);
3032                 $$ = list;
3033           }
3034         | argument_list error {
3035                 CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
3036                 $$ = null;
3037           }
3038         ;
3039
3040 argument
3041         : expression
3042           {
3043                 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
3044           }
3045         | non_simple_argument
3046           {
3047                 $$ = $1;
3048           }
3049         ;
3050
3051 non_simple_argument
3052         : REF variable_reference 
3053           { 
3054                 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
3055           }
3056         | OUT variable_reference 
3057           { 
3058                 $$ = new Argument ((Expression) $2, Argument.AType.Out);
3059           }
3060         | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
3061           {
3062                 ArrayList list = (ArrayList) $3;
3063                 Argument[] args = new Argument [list.Count];
3064                 list.CopyTo (args, 0);
3065
3066                 Expression expr = new Arglist (args, (Location) $1);
3067                 $$ = new Argument (expr, Argument.AType.Expression);
3068           }
3069         | ARGLIST
3070           {
3071                 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
3072           }
3073         ;
3074
3075 variable_reference
3076         : expression { note ("section 5.4"); $$ = $1; }
3077         ;
3078
3079 element_access
3080         : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET 
3081           {
3082                 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
3083           }
3084         | primary_expression rank_specifiers
3085           {
3086                 // So the super-trick is that primary_expression
3087                 // can only be either a SimpleName or a MemberAccess. 
3088                 // The MemberAccess case arises when you have a fully qualified type-name like :
3089                 // Foo.Bar.Blah i;
3090                 // SimpleName is when you have
3091                 // Blah i;
3092                   
3093                 Expression expr = (Expression) $1;  
3094                 if (expr is ComposedCast){
3095                         $$ = new ComposedCast (expr, (string) $2);
3096                 } else if (!(expr is SimpleName || expr is MemberAccess || expr is ConstructedType || expr is QualifiedAliasMember)){
3097                         Error_ExpectingTypeName (expr);
3098                         $$ = TypeManager.system_object_expr;
3099                 } else {
3100                         //
3101                         // So we extract the string corresponding to the SimpleName
3102                         // or MemberAccess
3103                         // 
3104                         $$ = new ComposedCast (expr, (string) $2);
3105                 }
3106           }
3107         ;
3108
3109 expression_list
3110         : expression
3111           {
3112                 ArrayList list = new ArrayList (4);
3113                 list.Add ($1);
3114                 $$ = list;
3115           }
3116         | expression_list COMMA expression
3117           {
3118                 ArrayList list = (ArrayList) $1;
3119                 list.Add ($3);
3120                 $$ = list;
3121           }
3122         ;
3123
3124 this_access
3125         : THIS
3126           {
3127                 $$ = new This (current_block, (Location) $1);
3128           }
3129         ;
3130
3131 base_access
3132         : BASE DOT IDENTIFIER
3133           {
3134                 LocatedToken lt = (LocatedToken) $3;
3135                 $$ = new BaseAccess (lt.Value, lt.Location);
3136           }
3137         | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
3138           {
3139                 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
3140           }
3141         | BASE error {
3142                 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
3143                 $$ = null;
3144           }
3145         ;
3146
3147 post_increment_expression
3148         : primary_expression OP_INC
3149           {
3150                 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
3151                                        (Expression) $1, (Location) $2);
3152           }
3153         ;
3154
3155 post_decrement_expression
3156         : primary_expression OP_DEC
3157           {
3158                 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
3159                                        (Expression) $1, (Location) $2);
3160           }
3161         ;
3162
3163 new_expression
3164         : object_or_delegate_creation_expression
3165         | array_creation_expression
3166         ;
3167
3168 object_or_delegate_creation_expression
3169         : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
3170           {
3171                 $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
3172           }
3173         ;
3174
3175 array_creation_expression
3176         : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET 
3177           opt_rank_specifier
3178           opt_array_initializer
3179           {
3180                 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
3181           }
3182         | NEW type rank_specifiers array_initializer
3183           {
3184                 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, (Location) $1);
3185           }
3186         | NEW error
3187           {
3188                 Report.Error (1031, (Location) $1, "Type expected");
3189                 $$ = null;
3190           }          
3191         | NEW type error 
3192           {
3193                 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
3194                 $$ = null;
3195           }
3196         ;
3197
3198 opt_rank_specifier
3199         : /* empty */
3200           {
3201                   $$ = "";
3202           }
3203         | rank_specifiers
3204           {
3205                         $$ = $1;
3206           }
3207         ;
3208
3209 opt_rank_specifier_or_nullable
3210         : /* empty */
3211           {
3212                 $$ = "";
3213           }
3214         | INTERR
3215           {
3216                 $$ = "?";
3217           }
3218         | opt_nullable rank_specifiers
3219           {
3220                 if ((bool) $1)
3221                         $$ = "?" + $2;
3222                 else
3223                         $$ = $2;
3224           }
3225         | opt_nullable rank_specifiers INTERR
3226           {
3227                 if ((bool) $1)
3228                         $$ = "?" + $2 + "?";
3229                 else
3230                         $$ = $2 + "?";
3231           }
3232         ;
3233
3234 rank_specifiers
3235         : rank_specifier opt_rank_specifier
3236           {
3237                   $$ = (string) $2 + (string) $1;
3238           }
3239         ;
3240
3241 rank_specifier
3242         : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3243           {
3244                 $$ = "[" + (string) $2 + "]";
3245           }
3246         ;
3247
3248 opt_dim_separators
3249         : /* empty */
3250           {
3251                 $$ = "";
3252           }
3253         | dim_separators
3254           {
3255                   $$ = $1;
3256           }               
3257         ;
3258
3259 dim_separators
3260         : COMMA
3261           {
3262                 $$ = ",";
3263           }
3264         | dim_separators COMMA
3265           {
3266                 $$ = (string) $1 + ",";
3267           }
3268         ;
3269
3270 opt_array_initializer
3271         : /* empty */
3272           {
3273                 $$ = null;
3274           }
3275         | array_initializer
3276           {
3277                 $$ = $1;
3278           }
3279         ;
3280
3281 array_initializer
3282         : OPEN_BRACE CLOSE_BRACE
3283           {
3284                 ArrayList list = new ArrayList (4);
3285                 $$ = list;
3286           }
3287         | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3288           {
3289                 $$ = (ArrayList) $2;
3290           }
3291         ;
3292
3293 variable_initializer_list
3294         : variable_initializer
3295           {
3296                 ArrayList list = new ArrayList (4);
3297                 list.Add ($1);
3298                 $$ = list;
3299           }
3300         | variable_initializer_list COMMA variable_initializer
3301           {
3302                 ArrayList list = (ArrayList) $1;
3303                 list.Add ($3);
3304                 $$ = list;
3305           }
3306         ;
3307
3308 void_pointer_expression
3309         : void_pointer_expression STAR
3310           {
3311                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3312           }
3313         | VOID STAR
3314           {
3315                 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3316           }
3317         ;
3318
3319 typeof_expression
3320         : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
3321           {
3322                 $$ = new TypeOfVoid ((Location) $1);
3323           }
3324         | TYPEOF OPEN_PARENS void_pointer_expression CLOSE_PARENS
3325           {
3326                 $$ = new TypeOf ((Expression) $3, (Location) $1);
3327           }
3328         | TYPEOF OPEN_PARENS
3329           {
3330                 lexer.TypeOfParsing = true;
3331           }
3332           type CLOSE_PARENS
3333           {
3334                 lexer.TypeOfParsing = false;
3335                 $$ = new TypeOf ((Expression) $4, lexer.Location);
3336           }
3337         ;
3338
3339 sizeof_expression
3340         : SIZEOF OPEN_PARENS type CLOSE_PARENS { 
3341                 $$ = new SizeOf ((Expression) $3, (Location) $1);
3342           }
3343         ;
3344
3345 checked_expression
3346         : CHECKED OPEN_PARENS expression CLOSE_PARENS
3347           {
3348                 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3349           }
3350         ;
3351
3352 unchecked_expression
3353         : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3354           {
3355                 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3356           }
3357         ;
3358
3359 pointer_member_access 
3360         : primary_expression OP_PTR IDENTIFIER
3361           {
3362                 Expression deref;
3363                 LocatedToken lt = (LocatedToken) $3;
3364
3365                 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
3366                 $$ = new MemberAccess (deref, lt.Value, lt.Location);
3367           }
3368         ;
3369
3370 anonymous_method_expression
3371         : DELEGATE opt_anonymous_method_signature
3372           {
3373                 oob_stack.Push (current_local_parameters);
3374                 current_local_parameters = (Parameters)$2;
3375
3376                 // Force the next block to be created as a ToplevelBlock
3377                 oob_stack.Push (current_block);
3378                 oob_stack.Push (top_current_block);
3379                 current_block = null;
3380           } 
3381           block
3382           {
3383                 Location loc = (Location) $1;
3384                 top_current_block = (Block) oob_stack.Pop ();
3385                 current_block = (Block) oob_stack.Pop ();
3386                 if (RootContext.Version == LanguageVersion.ISO_1){
3387                         Report.FeatureIsNotStandardized (loc, "anonymous methods");
3388                         $$ = null;
3389                 } else  {
3390                         ToplevelBlock anon_block = (ToplevelBlock) $4;
3391
3392                         anon_block.Parent = current_block;
3393                         $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block, 
3394                                 anon_block, loc);
3395                 }
3396                         current_local_parameters = (Parameters) oob_stack.Pop ();
3397                 }
3398         ;
3399
3400 opt_anonymous_method_signature
3401         : /* empty */                   { $$ = null; } 
3402         | anonymous_method_signature
3403         ;
3404
3405 anonymous_method_signature
3406         : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS 
3407           {
3408                 if ($2 == null)
3409                         $$ = Parameters.EmptyReadOnlyParameters;
3410                 else {
3411                         ArrayList par_list = (ArrayList) $2;
3412                         Parameter [] pars = new Parameter [par_list.Count];
3413                         par_list.CopyTo (pars);
3414                         $$ = new Parameters (pars, null);
3415                 }
3416           }
3417         ;
3418
3419 opt_anonymous_method_parameter_list
3420         : /* empty */   { $$ = null; } 
3421         | anonymous_method_parameter_list  { $$ = $1; }
3422         ;
3423
3424 anonymous_method_parameter_list
3425         : anonymous_method_parameter 
3426           {
3427                 ArrayList a = new ArrayList (4);
3428                 a.Add ($1);
3429                 $$ = a;
3430           }
3431         | anonymous_method_parameter_list COMMA anonymous_method_parameter 
3432           {
3433                 ArrayList a = (ArrayList) $1;
3434                 a.Add ($3);
3435                 $$ = a;
3436           }
3437         ; 
3438
3439 anonymous_method_parameter
3440         : opt_parameter_modifier type IDENTIFIER {
3441                 LocatedToken lt = (LocatedToken) $3;
3442                 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3443           }
3444         | PARAMS type IDENTIFIER {
3445                 Report.Error (1670, ((LocatedToken) $3).Location, "The `params' modifier is not allowed in anonymous method declaration");
3446                 $$ = null;
3447           }
3448         ;
3449
3450 default_value_expression
3451         : DEFAULT_OPEN_PARENS type CLOSE_PARENS
3452           {
3453                 $$ = new DefaultValueExpression ((Expression) $2, lexer.Location);
3454           }
3455         ;
3456
3457 unary_expression
3458         : primary_expression
3459         | BANG prefixed_unary_expression
3460           {
3461                 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3462           }
3463         | TILDE prefixed_unary_expression
3464           {
3465                 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3466           }
3467         | cast_expression
3468         ;
3469
3470 cast_list
3471         : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3472           {
3473                 $$ = new Cast ((Expression) $1, (Expression) $3);
3474           }
3475         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3476           {
3477                 $$ = new Cast ((Expression) $1, (Expression) $3);
3478           }     
3479         ;
3480
3481 cast_expression
3482         : cast_list
3483         | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3484           {
3485                 // TODO: wrong location
3486                 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3487           }
3488         ;
3489
3490         //
3491         // The idea to split this out is from Rhys' grammar
3492         // to solve the problem with casts.
3493         //
3494 prefixed_unary_expression
3495         : unary_expression
3496         | PLUS prefixed_unary_expression
3497           { 
3498                 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3499           } 
3500         | MINUS prefixed_unary_expression 
3501           { 
3502                 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3503           }
3504         | OP_INC prefixed_unary_expression 
3505           {
3506                 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3507                                        (Expression) $2, (Location) $1);
3508           }
3509         | OP_DEC prefixed_unary_expression 
3510           {
3511                 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3512                                        (Expression) $2, (Location) $1);
3513           }
3514         | STAR prefixed_unary_expression
3515           {
3516                 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, (Location) $1);
3517           }
3518         | BITWISE_AND prefixed_unary_expression
3519           {
3520                 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3521           }
3522         ;
3523
3524 pre_increment_expression
3525         : OP_INC prefixed_unary_expression 
3526           {
3527                 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3528                                        (Expression) $2, (Location) $1);
3529           }
3530         ;
3531
3532 pre_decrement_expression
3533         : OP_DEC prefixed_unary_expression 
3534           {
3535                 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3536                                        (Expression) $2, (Location) $1);
3537           }
3538         ;
3539
3540 multiplicative_expression
3541         : prefixed_unary_expression
3542         | multiplicative_expression STAR prefixed_unary_expression
3543           {
3544                 $$ = new Binary (Binary.Operator.Multiply, 
3545                                  (Expression) $1, (Expression) $3);
3546           }
3547         | multiplicative_expression DIV prefixed_unary_expression
3548           {
3549                 $$ = new Binary (Binary.Operator.Division, 
3550                                  (Expression) $1, (Expression) $3);
3551           }
3552         | multiplicative_expression PERCENT prefixed_unary_expression 
3553           {
3554                 $$ = new Binary (Binary.Operator.Modulus, 
3555                                  (Expression) $1, (Expression) $3);
3556           }
3557         ;
3558
3559 additive_expression
3560         : multiplicative_expression
3561         | additive_expression PLUS multiplicative_expression 
3562           {
3563                 $$ = new Binary (Binary.Operator.Addition, 
3564                                  (Expression) $1, (Expression) $3);
3565           }
3566         | additive_expression MINUS multiplicative_expression
3567           {
3568                 $$ = new Binary (Binary.Operator.Subtraction, 
3569                                  (Expression) $1, (Expression) $3);
3570           }
3571         ;
3572
3573 shift_expression
3574         : additive_expression
3575         | shift_expression OP_SHIFT_LEFT additive_expression
3576           {
3577                 $$ = new Binary (Binary.Operator.LeftShift, 
3578                                  (Expression) $1, (Expression) $3);
3579           }
3580         | shift_expression OP_SHIFT_RIGHT additive_expression
3581           {
3582                 $$ = new Binary (Binary.Operator.RightShift, 
3583                                  (Expression) $1, (Expression) $3);
3584           }
3585         ; 
3586
3587 opt_error
3588         : /* empty */
3589           {
3590                 $$ = false;
3591           }
3592         | error
3593           {
3594                 lexer.PutbackNullable ();
3595                 $$ = true;
3596           }
3597         ;
3598
3599 nullable_type_or_conditional
3600         : type opt_error
3601           {
3602                 if (((bool) $2) && ($1 is ComposedCast))
3603                         $$ = ((ComposedCast) $1).RemoveNullable ();
3604                 else
3605                         $$ = $1;
3606           }
3607         ;
3608
3609 relational_expression
3610         : shift_expression
3611         | relational_expression OP_LT shift_expression
3612           {
3613                 $$ = new Binary (Binary.Operator.LessThan, 
3614                                  (Expression) $1, (Expression) $3);
3615           }
3616         | relational_expression OP_GT shift_expression
3617           {
3618                 $$ = new Binary (Binary.Operator.GreaterThan, 
3619                                  (Expression) $1, (Expression) $3);
3620           }
3621         | relational_expression OP_LE shift_expression
3622           {
3623                 $$ = new Binary (Binary.Operator.LessThanOrEqual, 
3624                                  (Expression) $1, (Expression) $3);
3625           }
3626         | relational_expression OP_GE shift_expression
3627           {
3628                 $$ = new Binary (Binary.Operator.GreaterThanOrEqual, 
3629                                  (Expression) $1, (Expression) $3);
3630           }
3631         | relational_expression IS
3632           {
3633                 yyErrorFlag = 3;
3634           } nullable_type_or_conditional
3635           {
3636                 $$ = new Is ((Expression) $1, (Expression) $4, (Location) $2);
3637           }
3638         | relational_expression AS
3639           {
3640                 yyErrorFlag = 3;
3641           } nullable_type_or_conditional
3642           {
3643                 $$ = new As ((Expression) $1, (Expression) $4, (Location) $2);
3644           }
3645         ;
3646
3647 equality_expression
3648         : relational_expression
3649         | equality_expression OP_EQ relational_expression
3650           {
3651                 $$ = new Binary (Binary.Operator.Equality, 
3652                                  (Expression) $1, (Expression) $3);
3653           }
3654         | equality_expression OP_NE relational_expression
3655           {
3656                 $$ = new Binary (Binary.Operator.Inequality, 
3657                                  (Expression) $1, (Expression) $3);
3658           }
3659         ; 
3660
3661 and_expression
3662         : equality_expression
3663         | and_expression BITWISE_AND equality_expression
3664           {
3665                 $$ = new Binary (Binary.Operator.BitwiseAnd, 
3666                                  (Expression) $1, (Expression) $3);
3667           }
3668         ;
3669
3670 exclusive_or_expression
3671         : and_expression
3672         | exclusive_or_expression CARRET and_expression
3673           {
3674                 $$ = new Binary (Binary.Operator.ExclusiveOr, 
3675                                  (Expression) $1, (Expression) $3);
3676           }
3677         ;
3678
3679 inclusive_or_expression
3680         : exclusive_or_expression
3681         | inclusive_or_expression BITWISE_OR exclusive_or_expression
3682           {
3683                 $$ = new Binary (Binary.Operator.BitwiseOr, 
3684                                  (Expression) $1, (Expression) $3);
3685           }
3686         ;
3687
3688 conditional_and_expression
3689         : inclusive_or_expression
3690         | conditional_and_expression OP_AND inclusive_or_expression
3691           {
3692                 $$ = new Binary (Binary.Operator.LogicalAnd, 
3693                                  (Expression) $1, (Expression) $3);
3694           }
3695         ;
3696
3697 conditional_or_expression
3698         : conditional_and_expression
3699         | conditional_or_expression OP_OR conditional_and_expression
3700           {
3701                 $$ = new Binary (Binary.Operator.LogicalOr, 
3702                                  (Expression) $1, (Expression) $3);
3703           }
3704         ;
3705
3706 conditional_expression
3707         : conditional_or_expression
3708         | conditional_or_expression INTERR expression COLON expression 
3709           {
3710                 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
3711           }
3712         | conditional_or_expression INTERR INTERR expression
3713           {
3714                 $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $4, lexer.Location);
3715           }
3716         // We'll be resolved into a `parenthesized_expression_0' later on.
3717         | conditional_or_expression INTERR CLOSE_PARENS
3718           {
3719                 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
3720                 lexer.PutbackCloseParens ();
3721           }
3722         ;
3723
3724 assignment_expression
3725         : prefixed_unary_expression ASSIGN expression
3726           {
3727                 $$ = new Assign ((Expression) $1, (Expression) $3);
3728           }
3729         | prefixed_unary_expression OP_MULT_ASSIGN expression
3730           {
3731                 $$ = new CompoundAssign (
3732                         Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
3733           }
3734         | prefixed_unary_expression OP_DIV_ASSIGN expression
3735           {
3736                 $$ = new CompoundAssign (
3737                         Binary.Operator.Division, (Expression) $1, (Expression) $3);
3738           }
3739         | prefixed_unary_expression OP_MOD_ASSIGN expression
3740           {
3741                 $$ = new CompoundAssign (
3742                         Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
3743           }
3744         | prefixed_unary_expression OP_ADD_ASSIGN expression
3745           {
3746                 $$ = new CompoundAssign (
3747                         Binary.Operator.Addition, (Expression) $1, (Expression) $3);
3748           }
3749         | prefixed_unary_expression OP_SUB_ASSIGN expression
3750           {
3751                 $$ = new CompoundAssign (
3752                         Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3753           }
3754         | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3755           {
3756                 $$ = new CompoundAssign (
3757                         Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
3758           }
3759         | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3760           {
3761                 $$ = new CompoundAssign (
3762                         Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
3763           }
3764         | prefixed_unary_expression OP_AND_ASSIGN expression
3765           {
3766                 $$ = new CompoundAssign (
3767                         Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
3768           }
3769         | prefixed_unary_expression OP_OR_ASSIGN expression
3770           {
3771                 $$ = new CompoundAssign (
3772                         Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
3773           }
3774         | prefixed_unary_expression OP_XOR_ASSIGN expression
3775           {
3776                 $$ = new CompoundAssign (
3777                         Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
3778           }
3779         ;
3780
3781 expression
3782         : conditional_expression
3783         | assignment_expression
3784         ;
3785
3786 constant_expression
3787         : expression
3788         ;
3789
3790 boolean_expression
3791         : expression
3792         ;
3793
3794 //
3795 // 10 classes
3796 //
3797 class_declaration
3798         : opt_attributes
3799           opt_modifiers
3800           opt_partial
3801           CLASS
3802           {
3803                 lexer.ConstraintsParsing = true;
3804           }
3805           member_name
3806           {
3807                 MemberName name = MakeName ((MemberName) $6);
3808                 int mod_flags = (int) $2;
3809
3810                 if ($3 != null) {
3811                         ClassPart part = PartialContainer.CreatePart (
3812                                 current_namespace, current_class, name, mod_flags,
3813                                 (Attributes) $1, Kind.Class, (Location) $3);
3814
3815                         current_container = part.PartialContainer;
3816                         current_class = part;
3817                 } else {
3818                         if ((mod_flags & Modifiers.STATIC) != 0) {
3819                                 current_class = new StaticClass (
3820                                         current_namespace, current_class, name,
3821                                         mod_flags, (Attributes) $1);
3822                         } else {
3823                                 current_class = new Class (
3824                                         current_namespace, current_class, name,
3825                                         mod_flags, (Attributes) $1);
3826                         }
3827
3828                         current_container.AddClassOrStruct (current_class);
3829                         current_container = current_class;
3830                         RootContext.Tree.RecordDecl (current_namespace.NS, name, current_class);
3831                 }
3832           }
3833           opt_class_base
3834           opt_type_parameter_constraints_clauses
3835           {
3836                 lexer.ConstraintsParsing = false;
3837
3838                 if ($8 != null) {
3839                         if (current_class.Name == "System.Object") {
3840                                 Report.Error (537, current_class.Location,
3841                                               "The class System.Object cannot have a base " +
3842                                               "class or implement an interface.");
3843                         }
3844                         current_class.Bases = (ArrayList) $8;
3845                 }
3846
3847                 current_class.SetParameterInfo ((ArrayList) $9);
3848
3849                 if (RootContext.Documentation != null) {
3850                         current_class.DocComment = Lexer.consume_doc_comment ();
3851                         Lexer.doc_state = XmlCommentState.Allowed;
3852                 }
3853           }
3854           class_body
3855           {
3856                 if (RootContext.Documentation != null)
3857                         Lexer.doc_state = XmlCommentState.Allowed;
3858           }
3859           opt_semicolon 
3860           {
3861                 $$ = pop_current_class ();
3862           }
3863         ;       
3864
3865 opt_partial
3866         : /* empty */
3867           { $$ = null; }
3868         | PARTIAL
3869           { $$ = $1; } // location
3870         ;
3871
3872 opt_modifiers
3873         : /* empty */           { $$ = (int) 0; }
3874         | modifiers
3875         ;
3876
3877 modifiers
3878         : modifier
3879         | modifiers modifier
3880           { 
3881                 int m1 = (int) $1;
3882                 int m2 = (int) $2;
3883
3884                 if ((m1 & m2) != 0) {
3885                         Location l = lexer.Location;
3886                         Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
3887                 }
3888                 $$ = (int) (m1 | m2);
3889           }
3890         ;
3891
3892 modifier
3893         : NEW                   { $$ = Modifiers.NEW; }
3894         | PUBLIC                { $$ = Modifiers.PUBLIC; }
3895         | PROTECTED             { $$ = Modifiers.PROTECTED; }
3896         | INTERNAL              { $$ = Modifiers.INTERNAL; }
3897         | PRIVATE               { $$ = Modifiers.PRIVATE; }
3898         | ABSTRACT              { $$ = Modifiers.ABSTRACT; }
3899         | SEALED                { $$ = Modifiers.SEALED; }
3900         | STATIC                { $$ = Modifiers.STATIC; }
3901         | READONLY              { $$ = Modifiers.READONLY; }
3902         | VIRTUAL               { $$ = Modifiers.VIRTUAL; }
3903         | OVERRIDE              { $$ = Modifiers.OVERRIDE; }
3904         | EXTERN                { $$ = Modifiers.EXTERN; }
3905         | VOLATILE              { $$ = Modifiers.VOLATILE; }
3906         | UNSAFE                { $$ = Modifiers.UNSAFE; }
3907         ;
3908
3909 opt_class_base
3910         : /* empty */           { $$ = null; }
3911         | class_base            { $$ = $1;   }
3912         ;
3913
3914 class_base
3915         : COLON type_list { $$ = $2; }
3916         ;
3917
3918 opt_type_parameter_constraints_clauses
3919         : /* empty */           { $$ = null; }
3920         | type_parameter_constraints_clauses 
3921           { $$ = $1; }
3922         ;
3923
3924 type_parameter_constraints_clauses
3925         : type_parameter_constraints_clause {
3926                 ArrayList constraints = new ArrayList (1);
3927                 constraints.Add ($1);
3928                 $$ = constraints;
3929           }
3930         | type_parameter_constraints_clauses type_parameter_constraints_clause {
3931                 ArrayList constraints = (ArrayList) $1;
3932
3933                 constraints.Add ($2);
3934                 $$ = constraints;
3935           }
3936         ; 
3937
3938 type_parameter_constraints_clause
3939         : WHERE IDENTIFIER COLON type_parameter_constraints {
3940                 LocatedToken lt = (LocatedToken) $2;
3941                 $$ = new Constraints (lt.Value, (ArrayList) $4, lt.Location);
3942           }
3943         ; 
3944
3945 type_parameter_constraints
3946         : type_parameter_constraint {
3947                 ArrayList constraints = new ArrayList (1);
3948                 constraints.Add ($1);
3949                 $$ = constraints;
3950           }
3951         | type_parameter_constraints COMMA type_parameter_constraint {
3952                 ArrayList constraints = (ArrayList) $1;
3953
3954                 constraints.Add ($3);
3955                 $$ = constraints;
3956           }
3957         ;
3958
3959 type_parameter_constraint
3960         : type
3961         | NEW OPEN_PARENS CLOSE_PARENS {
3962                 $$ = SpecialConstraint.Constructor;
3963           }
3964         | CLASS {
3965                 $$ = SpecialConstraint.ReferenceType;
3966           }
3967         | STRUCT {
3968                 $$ = SpecialConstraint.ValueType;
3969           }
3970         ;
3971
3972 //
3973 // Statements (8.2)
3974 //
3975
3976 //
3977 // A block is "contained" on the following places:
3978 //      method_body
3979 //      property_declaration as part of the accessor body (get/set)
3980 //      operator_declaration
3981 //      constructor_declaration
3982 //      destructor_declaration
3983 //      event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3984 //      
3985 block
3986         : OPEN_BRACE 
3987           {
3988                 if (current_block == null){
3989                         current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, (Location) $1);
3990                         top_current_block = current_block;
3991                 } else {
3992                         current_block = new Block (current_block, (Location) $1, Location.Null);
3993                 }
3994           } 
3995           opt_statement_list CLOSE_BRACE 
3996           { 
3997                 while (current_block.Implicit)
3998                         current_block = current_block.Parent;
3999                 $$ = current_block;
4000                 current_block.SetEndLocation ((Location) $4);
4001                 current_block = current_block.Parent;
4002                 if (current_block == null)
4003                         top_current_block = null;
4004           }
4005         ;
4006
4007 opt_statement_list
4008         : /* empty */
4009         | statement_list 
4010         ;
4011
4012 statement_list
4013         : statement
4014         | statement_list statement
4015         ;
4016
4017 statement
4018         : declaration_statement
4019           {
4020                 if ($1 != null && (Block) $1 != current_block){
4021                         current_block.AddStatement ((Statement) $1);
4022                         current_block = (Block) $1;
4023                 }
4024           }
4025         | valid_declaration_statement
4026           {
4027                 current_block.AddStatement ((Statement) $1);
4028           }
4029         | labeled_statement
4030         ;
4031
4032 valid_declaration_statement
4033         : block
4034         | empty_statement
4035         | expression_statement
4036         | selection_statement
4037         | iteration_statement
4038         | jump_statement                  
4039         | try_statement
4040         | checked_statement
4041         | unchecked_statement
4042         | lock_statement
4043         | using_statement
4044         | unsafe_statement
4045         | fixed_statement
4046         ;
4047
4048 embedded_statement
4049         : valid_declaration_statement
4050         | declaration_statement
4051           {
4052                   Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4053                   $$ = null;
4054           }
4055         | labeled_statement
4056           {
4057                   Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4058                   $$ = null;
4059           }
4060         ;
4061
4062 empty_statement
4063         : SEMICOLON
4064           {
4065                   $$ = EmptyStatement.Value;
4066           }
4067         ;
4068
4069 labeled_statement
4070         : IDENTIFIER COLON 
4071           {
4072                 LocatedToken lt = (LocatedToken) $1;
4073                 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
4074
4075                 if (current_block.AddLabel (lt.Value, labeled, lt.Location))
4076                         current_block.AddStatement (labeled);
4077           }
4078           statement
4079         ;
4080
4081 declaration_statement
4082         : local_variable_declaration SEMICOLON
4083           {
4084                 if ($1 != null){
4085                         DictionaryEntry de = (DictionaryEntry) $1;
4086                         Expression e = (Expression) de.Key;
4087
4088                         $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
4089                 }
4090           }
4091
4092         | local_constant_declaration SEMICOLON
4093           {
4094                 if ($1 != null){
4095                         DictionaryEntry de = (DictionaryEntry) $1;
4096
4097                         $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
4098                 }
4099           }
4100         ;
4101
4102 /* 
4103  * The following is from Rhys' grammar:
4104  * > Types in local variable declarations must be recognized as 
4105  * > expressions to prevent reduce/reduce errors in the grammar.
4106  * > The expressions are converted into types during semantic analysis.
4107  */
4108 local_variable_type
4109         : primary_expression opt_rank_specifier_or_nullable
4110           { 
4111                 // FIXME: Do something smart here regarding the composition of the type.
4112
4113                 // Ok, the above "primary_expression" is there to get rid of
4114                 // both reduce/reduce and shift/reduces in the grammar, it should
4115                 // really just be "type_name".  If you use type_name, a reduce/reduce
4116                 // creeps up.  If you use namespace_or_type_name (which is all we need
4117                 // really) two shift/reduces appear.
4118                 // 
4119
4120                 // So the super-trick is that primary_expression
4121                 // can only be either a SimpleName or a MemberAccess. 
4122                 // The MemberAccess case arises when you have a fully qualified type-name like :
4123                 // Foo.Bar.Blah i;
4124                 // SimpleName is when you have
4125                 // Blah i;
4126                   
4127                 Expression expr = (Expression) $1;  
4128                 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType || expr is QualifiedAliasMember)) {
4129                         Error_ExpectingTypeName (expr);
4130                         $$ = null;
4131                 } else {
4132                         //
4133                         // So we extract the string corresponding to the SimpleName
4134                         // or MemberAccess
4135                         // 
4136
4137                         if ((string) $2 == "")
4138                                 $$ = $1;
4139                         else
4140                                 $$ = new ComposedCast ((Expression) $1, (string) $2);
4141                 }
4142           }
4143         | builtin_types opt_rank_specifier_or_nullable
4144           {
4145                 if ((string) $2 == "")
4146                         $$ = $1;
4147                 else
4148                         $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
4149           }
4150         ;
4151
4152 local_variable_pointer_type
4153         : primary_expression STAR
4154           {
4155                 Expression expr = (Expression) $1;  
4156
4157                 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType || expr is QualifiedAliasMember)) {
4158                         Error_ExpectingTypeName (expr);
4159
4160                         $$ = null;
4161                 } else 
4162                         $$ = new ComposedCast ((Expression) $1, "*");
4163           }
4164         | builtin_types STAR
4165           {
4166                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
4167           }
4168         | VOID STAR
4169           {
4170                 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
4171           }
4172         | local_variable_pointer_type STAR
4173           {
4174                 $$ = new ComposedCast ((Expression) $1, "*");
4175           }
4176         ;
4177
4178 local_variable_declaration
4179         : local_variable_type variable_declarators
4180           {
4181                 if ($1 != null)
4182                         $$ = new DictionaryEntry ($1, $2);
4183                 else
4184                         $$ = null;
4185           }
4186         | local_variable_pointer_type opt_rank_specifier_or_nullable variable_declarators
4187           {
4188                 if ($1 != null){
4189                         Expression t;
4190
4191                         if ((string) $2 == "")
4192                                 t = (Expression) $1;
4193                         else
4194                                 t = new ComposedCast ((Expression) $1, (string) $2);
4195                         $$ = new DictionaryEntry (t, $3);
4196                 } else 
4197                         $$ = null;
4198           }
4199         ;
4200
4201 local_constant_declaration
4202         : CONST local_variable_type constant_declarators
4203           {
4204                 if ($2 != null)
4205                         $$ = new DictionaryEntry ($2, $3);
4206                 else
4207                         $$ = null;
4208           }
4209         ;
4210
4211 expression_statement
4212         : statement_expression SEMICOLON
4213           {
4214                 $$ = $1;
4215           }
4216         ;
4217
4218         //
4219         // We have to do the wrapping here and not in the case above,
4220         // because statement_expression is used for example in for_statement
4221         //
4222 statement_expression
4223         : invocation_expression         { $$ = new StatementExpression ((ExpressionStatement) $1); }
4224         | object_creation_expression    { $$ = new StatementExpression ((ExpressionStatement) $1); }
4225         | assignment_expression         { $$ = new StatementExpression ((ExpressionStatement) $1); }
4226         | post_increment_expression     { $$ = new StatementExpression ((ExpressionStatement) $1); }
4227         | post_decrement_expression     { $$ = new StatementExpression ((ExpressionStatement) $1); }
4228         | pre_increment_expression      { $$ = new StatementExpression ((ExpressionStatement) $1); }
4229         | pre_decrement_expression      { $$ = new StatementExpression ((ExpressionStatement) $1); }
4230         | error {
4231                 Report.Error (1002, GetLocation ($1), "Expecting `;'");
4232                 $$ = null;
4233           }
4234         ;
4235
4236 object_creation_expression
4237         : object_or_delegate_creation_expression
4238           { note ("complain if this is a delegate maybe?"); } 
4239         ;
4240
4241 selection_statement
4242         : if_statement
4243         | switch_statement
4244         ; 
4245
4246 if_statement
4247         : IF OPEN_PARENS boolean_expression CLOSE_PARENS 
4248           embedded_statement
4249           { 
4250                 Location l = (Location) $1;
4251
4252                 $$ = new If ((Expression) $3, (Statement) $5, l);
4253
4254                 if (RootContext.WarningLevel >= 3){
4255                         // FIXME: location for warning should be loc property of $5.
4256                         if ($5 == EmptyStatement.Value)
4257                                 Report.Warning (642, l, "Possible mistaken empty statement");
4258                 }
4259
4260           }
4261         | IF OPEN_PARENS boolean_expression CLOSE_PARENS
4262           embedded_statement ELSE embedded_statement
4263           {
4264                 Location l = (Location) $1;
4265
4266                 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
4267
4268                 if (RootContext.WarningLevel >= 3){
4269                         // FIXME: location for warning should be loc property of $5 and $7.
4270                         if ($5 == EmptyStatement.Value)
4271                                 Report.Warning (642, l, "Possible mistaken empty statement");
4272                         if ($7 == EmptyStatement.Value)
4273                                 Report.Warning (642, l, "Possible mistaken empty statement");
4274                 }
4275           }
4276         ;
4277
4278 switch_statement
4279         : SWITCH OPEN_PARENS
4280           { 
4281                 switch_stack.Push (current_block);
4282           }
4283           expression CLOSE_PARENS 
4284           switch_block
4285           {
4286                 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
4287                 current_block = (Block) switch_stack.Pop ();
4288           }
4289         ;
4290
4291 switch_block
4292         : OPEN_BRACE
4293           opt_switch_sections
4294           CLOSE_BRACE
4295           {
4296                 $$ = $2;
4297           }
4298         ;
4299
4300 opt_switch_sections
4301         : /* empty */           
4302           {
4303                 Report.Error (1522, lexer.Location, "Empty switch block"); 
4304           }
4305         | switch_sections
4306         ;
4307
4308 switch_sections
4309         : switch_section 
4310           {
4311                 ArrayList sections = new ArrayList (4);
4312
4313                 sections.Add ($1);
4314                 $$ = sections;
4315           }
4316         | switch_sections switch_section
4317           {
4318                 ArrayList sections = (ArrayList) $1;
4319
4320                 sections.Add ($2);
4321                 $$ = sections;
4322           }
4323         ;
4324
4325 switch_section
4326         : switch_labels
4327           {
4328                 current_block = current_block.CreateSwitchBlock (lexer.Location);
4329           }
4330           statement_list 
4331           {
4332                 Block topmost = current_block;
4333
4334                 while (topmost.Implicit)
4335                         topmost = topmost.Parent;
4336                 $$ = new SwitchSection ((ArrayList) $1, topmost);
4337           }
4338         ;
4339
4340 switch_labels
4341         : switch_label 
4342           {
4343                 ArrayList labels = new ArrayList (4);
4344
4345                 labels.Add ($1);
4346                 $$ = labels;
4347           }
4348         | switch_labels switch_label 
4349           {
4350                 ArrayList labels = (ArrayList) ($1);
4351                 labels.Add ($2);
4352
4353                 $$ = labels;
4354           }
4355         ;
4356
4357 switch_label
4358         : CASE constant_expression COLON        { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
4359         | DEFAULT COLON                         { $$ = new SwitchLabel (null, (Location) $2); }
4360         | error {
4361                 Report.Error (
4362                         1523, GetLocation ($1), 
4363                         "The keyword case or default must precede code in switch block");
4364           }
4365         ;
4366
4367 iteration_statement
4368         : while_statement
4369         | do_statement
4370         | for_statement
4371         | foreach_statement
4372         ;
4373
4374 while_statement
4375         : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
4376           {
4377                 Location l = (Location) $1;
4378                 $$ = new While ((Expression) $3, (Statement) $5, l);
4379           }
4380         ;
4381
4382 do_statement
4383         : DO embedded_statement 
4384           WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
4385           {
4386                 Location l = (Location) $1;
4387
4388                 $$ = new Do ((Statement) $2, (Expression) $5, l);
4389           }
4390         ;
4391
4392 for_statement
4393         : FOR OPEN_PARENS 
4394           opt_for_initializer SEMICOLON
4395           {
4396                 Block assign_block = new Block (current_block);
4397                 current_block = assign_block;
4398
4399                 if ($3 is DictionaryEntry){
4400                         DictionaryEntry de = (DictionaryEntry) $3;
4401                         
4402                         Expression type = (Expression) de.Key;
4403                         ArrayList var_declarators = (ArrayList) de.Value;
4404
4405                         foreach (VariableDeclaration decl in var_declarators){
4406
4407                                 LocalInfo vi;
4408
4409                                 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4410                                 if (vi == null)
4411                                         continue;
4412
4413                                 Location l = lexer.Location;
4414                                 Expression expr;
4415                                 if (decl.expression_or_array_initializer is Expression){
4416                                         expr = (Expression) decl.expression_or_array_initializer;
4417                                 } else if (decl.expression_or_array_initializer == null) {
4418                                         expr = null;
4419                                 } else {
4420                                         ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4421                                         expr = new ArrayCreation (type, "", init, decl.Location);
4422                                 }
4423                                         
4424                                 LocalVariableReference var;
4425                                 var = new LocalVariableReference (assign_block, decl.identifier, l);
4426
4427                                 if (expr != null) {
4428                                         Assign a = new Assign (var, expr, decl.Location);
4429                                         
4430                                         assign_block.AddStatement (new StatementExpression (a));
4431                                 }
4432                         }
4433                         
4434                         // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4435                         // This can be referred to as $5 below.
4436                         $$ = null;
4437                 } else {
4438                         $$ = $3;
4439                 }
4440           } 
4441           opt_for_condition SEMICOLON
4442           opt_for_iterator CLOSE_PARENS 
4443           embedded_statement
4444           {
4445                 Location l = (Location) $1;
4446
4447                 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4448
4449                 current_block.AddStatement (f);
4450                 while (current_block.Implicit)
4451                         current_block = current_block.Parent;
4452                 $$ = current_block;
4453                 current_block = current_block.Parent;
4454           }
4455         ;
4456
4457 opt_for_initializer
4458         : /* empty */           { $$ = EmptyStatement.Value; }
4459         | for_initializer       
4460         ;
4461
4462 for_initializer
4463         : local_variable_declaration
4464         | statement_expression_list
4465         ;
4466
4467 opt_for_condition
4468         : /* empty */           { $$ = null; }
4469         | boolean_expression
4470         ;
4471
4472 opt_for_iterator
4473         : /* empty */           { $$ = EmptyStatement.Value; }
4474         | for_iterator
4475         ;
4476
4477 for_iterator
4478         : statement_expression_list
4479         ;
4480
4481 statement_expression_list
4482         : statement_expression  
4483           {
4484                 // CHANGE: was `null'
4485                 Block b = new Block (current_block, Block.Flags.Implicit);   
4486
4487                 b.AddStatement ((Statement) $1);
4488                 $$ = b;
4489           }
4490         | statement_expression_list COMMA statement_expression
4491           {
4492                 Block b = (Block) $1;
4493
4494                 b.AddStatement ((Statement) $3);
4495                 $$ = $1;
4496           }
4497         ;
4498
4499 foreach_statement
4500         : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4501           {
4502                 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4503                 $$ = null;
4504           }
4505         | FOREACH OPEN_PARENS type IDENTIFIER IN
4506           expression CLOSE_PARENS 
4507           {
4508                 Block foreach_block = new Block (current_block);
4509                 current_block = foreach_block;
4510
4511                 LocatedToken lt = (LocatedToken) $4;
4512                 Location l = lt.Location;
4513                 LocalInfo vi;
4514
4515                 vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4516                 if (vi != null) {
4517                         vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4518
4519                         // Get a writable reference to this read-only variable.
4520                         //
4521                         // Note that the $$ here refers to the value of _this_ code block,
4522                         // not the value of the LHS non-terminal.  This can be referred to as $8 below.
4523                         $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
4524                 } else {
4525                         $$ = null;
4526                 }
4527           } 
4528           embedded_statement 
4529           {
4530                 LocalVariableReference v = (LocalVariableReference) $8;
4531                 Location l = (Location) $1;
4532
4533                 if (v != null) {
4534                         Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
4535                         current_block.AddStatement (f);
4536                 }
4537
4538                 while (current_block.Implicit)
4539                           current_block = current_block.Parent;
4540                 $$ = current_block;
4541                 current_block = current_block.Parent;
4542           }
4543         ;
4544
4545 jump_statement
4546         : break_statement
4547         | continue_statement
4548         | goto_statement
4549         | return_statement
4550         | throw_statement
4551         | yield_statement
4552         ;
4553
4554 break_statement
4555         : BREAK SEMICOLON
4556           {
4557                 $$ = new Break ((Location) $1);
4558           }
4559         ;
4560
4561 continue_statement
4562         : CONTINUE SEMICOLON
4563           {
4564                 $$ = new Continue ((Location) $1);
4565           }
4566         ;
4567
4568 goto_statement
4569         : GOTO IDENTIFIER SEMICOLON 
4570           {
4571                 LocatedToken lt = (LocatedToken) $2;
4572                 $$ = new Goto (lt.Value, lt.Location);
4573           }
4574         | GOTO CASE constant_expression SEMICOLON
4575           {
4576                 $$ = new GotoCase ((Expression) $3, (Location) $1);
4577           }
4578         | GOTO DEFAULT SEMICOLON 
4579           {
4580                 $$ = new GotoDefault ((Location) $1);
4581           }
4582         ; 
4583
4584 return_statement
4585         : RETURN opt_expression SEMICOLON
4586           {
4587                 $$ = new Return ((Expression) $2, (Location) $1);
4588           }
4589         ;
4590
4591 throw_statement
4592         : THROW opt_expression SEMICOLON
4593           {
4594                 $$ = new Throw ((Expression) $2, (Location) $1);
4595           }
4596         ;
4597
4598 yield_statement 
4599         : IDENTIFIER RETURN expression SEMICOLON
4600           {
4601                 LocatedToken lt = (LocatedToken) $1;
4602                 string s = lt.Value;
4603                 if (s != "yield"){
4604                         Report.Error (1003, lt.Location, "; expected");
4605                         $$ = null;
4606                 }
4607                 if (RootContext.Version == LanguageVersion.ISO_1){
4608                         Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4609                         $$ = null;
4610                 }
4611                 if (iterator_container == null){
4612                         Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4613                         $$ = null;
4614                 } else {
4615                         iterator_container.SetYields ();
4616                         $$ = new Yield ((Expression) $3, lt.Location); 
4617                 }
4618           }
4619         | IDENTIFIER RETURN SEMICOLON
4620           {
4621                 Report.Error (1627, (Location) $2, "Expression expected after yield return");
4622                 $$ = null;
4623           }
4624         | IDENTIFIER BREAK SEMICOLON
4625           {
4626                 LocatedToken lt = (LocatedToken) $1;
4627                 string s = lt.Value;
4628                 if (s != "yield"){
4629                         Report.Error (1003, lt.Location, "; expected");
4630                         $$ = null;
4631                 }
4632                 if (RootContext.Version == LanguageVersion.ISO_1){
4633                         Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4634                         $$ = null;
4635                 }
4636                 if (iterator_container == null){
4637                         Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4638                         $$ = null;
4639                 } else {
4640                         iterator_container.SetYields ();
4641                         $$ = new YieldBreak (lt.Location);
4642                 }
4643           }
4644         ;
4645
4646 opt_expression
4647         : /* empty */
4648         | expression
4649         ;
4650
4651 try_statement
4652         : TRY block catch_clauses 
4653           {
4654                 Catch g = null;
4655                 
4656                 ArrayList c = (ArrayList)$3;
4657                 for (int i = 0; i < c.Count; ++i) {
4658                         Catch cc = (Catch) c [i];
4659                         if (cc.IsGeneral) {
4660                                 if (i != c.Count - 1)
4661                                         Report.Error (1017, cc.loc, "Try statement already has an empty catch block");
4662                                 g = cc;
4663                                 c.RemoveAt (i);
4664                                 i--;
4665                         }
4666                 }
4667
4668                 // Now s contains the list of specific catch clauses
4669                 // and g contains the general one.
4670                 
4671                 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4672           }
4673         | TRY block opt_catch_clauses FINALLY block
4674           {
4675                 Catch g = null;
4676                 ArrayList s = new ArrayList (4);
4677                 ArrayList catch_list = (ArrayList) $3;
4678
4679                 if (catch_list != null){
4680                         foreach (Catch cc in catch_list) {
4681                                 if (cc.IsGeneral)
4682                                         g = cc;
4683                                 else
4684                                         s.Add (cc);
4685                         }
4686                 }
4687
4688                 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4689           }
4690         | TRY block error 
4691           {
4692                 Report.Error (1524, (Location) $1, "Expected catch or finally");
4693                 $$ = null;
4694           }
4695         ;
4696
4697 opt_catch_clauses
4698         : /* empty */  { $$ = null; }
4699         | catch_clauses
4700         ;
4701
4702 catch_clauses
4703         : catch_clause 
4704           {
4705                 ArrayList l = new ArrayList (4);
4706
4707                 l.Add ($1);
4708                 $$ = l;
4709           }
4710         | catch_clauses catch_clause
4711           {
4712                 ArrayList l = (ArrayList) $1;
4713
4714                 l.Add ($2);
4715                 $$ = l;
4716           }
4717         ;
4718
4719 opt_identifier
4720         : /* empty */   { $$ = null; }
4721         | IDENTIFIER
4722         ;
4723
4724 catch_clause 
4725         : CATCH opt_catch_args 
4726           {
4727                 Expression type = null;
4728                 
4729                 if ($2 != null) {
4730                         DictionaryEntry cc = (DictionaryEntry) $2;
4731                         type = (Expression) cc.Key;
4732                         LocatedToken lt = (LocatedToken) cc.Value;
4733
4734                         if (lt != null){
4735                                 ArrayList one = new ArrayList (4);
4736
4737                                 one.Add (new VariableDeclaration (lt, null));
4738
4739                                 current_block = new Block (current_block);
4740                                 Block b = declare_local_variables (type, one, lt.Location);
4741                                 current_block = b;
4742                         }
4743                 }
4744           } block {
4745                 Expression type = null;
4746                 string id = null;
4747
4748                 if ($2 != null){
4749                         DictionaryEntry cc = (DictionaryEntry) $2;
4750                         type = (Expression) cc.Key;
4751                         LocatedToken lt = (LocatedToken) cc.Value;
4752
4753                         if (lt != null){
4754                                 id = lt.Value;
4755                                 while (current_block.Implicit)
4756                                         current_block = current_block.Parent;
4757                                 current_block = current_block.Parent;
4758                         }
4759                 }
4760
4761                 $$ = new Catch (type, id, (Block) $4, ((Block) $4).loc);
4762           }
4763         ;
4764
4765 opt_catch_args
4766         : /* empty */ { $$ = null; }
4767         | catch_args
4768         ;         
4769
4770 catch_args 
4771         : OPEN_PARENS type opt_identifier CLOSE_PARENS 
4772           {
4773                 $$ = new DictionaryEntry ($2, $3);
4774           }
4775         ;
4776
4777
4778 checked_statement
4779         : CHECKED block
4780           {
4781                 $$ = new Checked ((Block) $2);
4782           }
4783         ;
4784
4785 unchecked_statement
4786         : UNCHECKED block
4787           {
4788                 $$ = new Unchecked ((Block) $2);
4789           }
4790         ;
4791
4792 unsafe_statement
4793         : UNSAFE 
4794           {
4795                 RootContext.CheckUnsafeOption ((Location) $1);
4796           } block {
4797                 $$ = new Unsafe ((Block) $3);
4798           }
4799         ;
4800
4801 fixed_statement
4802         : FIXED OPEN_PARENS 
4803           type fixed_pointer_declarators 
4804           CLOSE_PARENS
4805           {
4806                 ArrayList list = (ArrayList) $4;
4807                 Expression type = (Expression) $3;
4808                 Location l = (Location) $1;
4809                 int top = list.Count;
4810
4811                 Block assign_block = new Block (current_block);
4812                 current_block = assign_block;
4813
4814                 for (int i = 0; i < top; i++){
4815                         Pair p = (Pair) list [i];
4816                         LocalInfo v;
4817
4818                         v = current_block.AddVariable (type, (string) p.First, l);
4819                         if (v == null)
4820                                 continue;
4821
4822                         v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4823                         v.Pinned = true;
4824                         p.First = v;
4825                         list [i] = p;
4826                 }
4827           }
4828           embedded_statement 
4829           {
4830                 Location l = (Location) $1;
4831
4832                 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4833
4834                 current_block.AddStatement (f);
4835                 while (current_block.Implicit)
4836                         current_block = current_block.Parent;
4837                 $$ = current_block;
4838                 current_block = current_block.Parent;
4839           }
4840         ;
4841
4842 fixed_pointer_declarators
4843         : fixed_pointer_declarator      { 
4844                 ArrayList declarators = new ArrayList (4);
4845                 if ($1 != null)
4846                         declarators.Add ($1);
4847                 $$ = declarators;
4848           }
4849         | fixed_pointer_declarators COMMA fixed_pointer_declarator
4850           {
4851                 ArrayList declarators = (ArrayList) $1;
4852                 if ($3 != null)
4853                         declarators.Add ($3);
4854                 $$ = declarators;
4855           }
4856         ;
4857
4858 fixed_pointer_declarator
4859         : IDENTIFIER ASSIGN expression
4860           {
4861                 LocatedToken lt = (LocatedToken) $1;
4862                 // FIXME: keep location
4863                 $$ = new Pair (lt.Value, $3);
4864           }
4865         | IDENTIFIER
4866           {
4867                 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
4868                 $$ = null;
4869           }
4870         ;
4871
4872 lock_statement
4873         : LOCK OPEN_PARENS expression CLOSE_PARENS 
4874           {
4875                 //
4876           } 
4877           embedded_statement
4878           {
4879                 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
4880           }
4881         ;
4882
4883 using_statement
4884         : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4885           {
4886                 Block assign_block = new Block (current_block);
4887                 current_block = assign_block;
4888
4889                 if ($3 is DictionaryEntry){
4890                         DictionaryEntry de = (DictionaryEntry) $3;
4891                         Location l = (Location) $1;
4892
4893                         Expression type = (Expression) de.Key;
4894                         ArrayList var_declarators = (ArrayList) de.Value;
4895
4896                         ArrayList vars = new ArrayList (4);
4897
4898                         foreach (VariableDeclaration decl in var_declarators){
4899
4900                                 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4901                                 if (vi == null)
4902                                         continue;
4903                                 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
4904
4905                                 Expression expr;
4906                                 if (decl.expression_or_array_initializer is Expression){
4907                                         expr = (Expression) decl.expression_or_array_initializer;
4908                                 } else {
4909                                         ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4910                                         if (init == null) {
4911                                                 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4912                                         }
4913                                         
4914                                         expr = new ArrayCreation (type, "", init, decl.Location);
4915                                 }
4916
4917                                 LocalVariableReference var;
4918
4919                                 // Get a writable reference to this read-only variable.
4920                                 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4921
4922                                 // This is so that it is not a warning on using variables
4923                                 vi.Used = true;
4924
4925                                 vars.Add (new DictionaryEntry (var, expr));                             
4926
4927                                 // Assign a = new Assign (var, expr, decl.Location);
4928                                 // assign_block.AddStatement (new StatementExpression (a));
4929                         }
4930
4931                         // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4932                         // It can be referred to as $5 below.
4933                         $$ = new DictionaryEntry (type, vars);
4934                  } else {
4935                         $$ = $3;
4936                  }
4937           } 
4938           embedded_statement
4939           {
4940                 Using u = new Using ($5, (Statement) $6, (Location) $1);
4941                 current_block.AddStatement (u);
4942                 while (current_block.Implicit)
4943                         current_block = current_block.Parent;
4944                 $$ = current_block;
4945                 current_block = current_block.Parent;
4946           }
4947         ; 
4948
4949 resource_acquisition
4950         : local_variable_declaration
4951         | expression
4952         ;
4953
4954 %%
4955
4956 // <summary>
4957 //   A class used to pass around variable declarations and constants
4958 // </summary>
4959 public class VariableDeclaration {
4960         public string identifier;
4961         public object expression_or_array_initializer;
4962         public Location Location;
4963         public Attributes OptAttributes;
4964         public string DocComment;
4965
4966         public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
4967         {
4968                 this.identifier = lt.Value;
4969                 this.expression_or_array_initializer = eoai;
4970                 this.Location = lt.Location;
4971                 this.OptAttributes = opt_attrs;
4972         }
4973
4974         public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
4975         {
4976         }
4977 }
4978
4979 // <summary>
4980 //   A class used to hold info about an indexer declarator
4981 // </summary>
4982 public class IndexerDeclaration {
4983         public Expression type;
4984         public MemberName interface_type;
4985         public Parameters param_list;
4986         public Location location;
4987
4988         public IndexerDeclaration (Expression type, MemberName interface_type,
4989                                    Parameters param_list, Location loc)
4990         {
4991                 this.type = type;
4992                 this.interface_type = interface_type;
4993                 this.param_list = param_list;
4994                 this.location = loc;
4995         }
4996 }
4997
4998 //
4999 // We use this when we do not have an object in advance that is an IIteratorContainer
5000 //
5001 public class SimpleIteratorContainer : IIteratorContainer {
5002         public bool Yields;
5003
5004         public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
5005
5006         //
5007         // Reset and return
5008         //
5009         public static SimpleIteratorContainer GetSimple () { 
5010                 Simple.Yields = false;
5011                 return Simple;
5012         }
5013
5014         public void SetYields () { Yields = true; } 
5015 }
5016
5017 // <summary>
5018 //  A class used to hold info about an operator declarator
5019 // </summary>
5020 public class OperatorDeclaration {
5021         public Operator.OpType optype;
5022         public Expression ret_type, arg1type, arg2type;
5023         public string arg1name, arg2name;
5024         public Location location;
5025
5026         public OperatorDeclaration (Operator.OpType op, Expression ret_type, 
5027                                     Expression arg1type, string arg1name,
5028                                     Expression arg2type, string arg2name, Location location)
5029         {
5030                 optype = op;
5031                 this.ret_type = ret_type;
5032                 this.arg1type = arg1type;
5033                 this.arg1name = arg1name;
5034                 this.arg2type = arg2type;
5035                 this.arg2name = arg2name;
5036                 this.location = location;
5037         }
5038
5039 }
5040
5041 void Error_ExpectingTypeName (Expression expr)
5042 {
5043         if (expr is Invocation){
5044                 Report.Error (1002, expr.Location, "Expecting `;'");
5045         } else {
5046                 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
5047         }
5048 }
5049
5050 TypeContainer pop_current_class ()
5051 {
5052         TypeContainer retval = current_class;
5053
5054         current_class = current_class.Parent;
5055         current_container = current_container.Parent;
5056         
5057         if (current_class != current_container) {
5058                 if (!(current_class is ClassPart) ||
5059                     ((ClassPart) current_class).PartialContainer != current_container)
5060                         throw new InternalErrorException ();
5061         } else if (current_container is ClassPart)
5062                 current_container = ((ClassPart) current_class).PartialContainer;
5063
5064         return retval;
5065 }
5066
5067 // <summary>
5068 //   Given the @class_name name, it creates a fully qualified name
5069 //   based on the containing declaration space
5070 // </summary>
5071 MemberName
5072 MakeName (MemberName class_name)
5073 {
5074         Namespace ns = current_namespace.NS;
5075
5076         if (current_container.Name == ""){
5077                 if (ns.Name != "")
5078                         return new MemberName (ns.MemberName, class_name);
5079                 else
5080                         return class_name;
5081         } else {
5082                 return new MemberName (current_container.MemberName, class_name);
5083         }
5084 }
5085
5086 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
5087 {
5088         Block implicit_block;
5089         ArrayList inits = null;
5090
5091         //
5092         // We use the `Used' property to check whether statements
5093         // have been added to the current block.  If so, we need
5094         // to create another block to contain the new declaration
5095         // otherwise, as an optimization, we use the same block to
5096         // add the declaration.
5097         //
5098         // FIXME: A further optimization is to check if the statements
5099         // that were added were added as part of the initialization
5100         // below.  In which case, no other statements have been executed
5101         // and we might be able to reduce the number of blocks for
5102         // situations like this:
5103         //
5104         // int j = 1;  int k = j + 1;
5105         //
5106         if (current_block.Used)
5107                 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
5108         else
5109                 implicit_block = current_block;
5110
5111         foreach (VariableDeclaration decl in variable_declarators){
5112
5113                 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
5114                         if (decl.expression_or_array_initializer != null){
5115                                 if (inits == null)
5116                                         inits = new ArrayList (4);
5117                                 inits.Add (decl);
5118                         }
5119                 }
5120         }
5121
5122         if (inits == null)
5123                 return implicit_block;
5124
5125         foreach (VariableDeclaration decl in inits){
5126                 Assign assign;
5127                 Expression expr;
5128                 
5129                 if (decl.expression_or_array_initializer is Expression){
5130                         expr = (Expression) decl.expression_or_array_initializer;
5131
5132                 } else {
5133                         ArrayList init = (ArrayList) decl.expression_or_array_initializer;
5134                         
5135                         expr = new ArrayCreation (type, "", init, decl.Location);
5136                 }
5137
5138                 LocalVariableReference var;
5139                 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
5140
5141                 assign = new Assign (var, expr, decl.Location);
5142
5143                 implicit_block.AddStatement (new StatementExpression (assign));
5144         }
5145         
5146         return implicit_block;
5147 }
5148
5149 Block declare_local_constants (Expression type, ArrayList declarators)
5150 {
5151         Block implicit_block;
5152
5153         if (current_block.Used)
5154                 implicit_block = new Block (current_block, Block.Flags.Implicit);
5155         else
5156                 implicit_block = current_block;
5157
5158         foreach (VariableDeclaration decl in declarators){
5159                 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
5160         }
5161         
5162         return implicit_block;
5163 }
5164
5165 void CheckAttributeTarget (string a, Location l)
5166 {
5167         switch (a) {
5168
5169         case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
5170                 return;
5171                 
5172         default :
5173                 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
5174                 break;
5175         }
5176
5177 }
5178
5179 void CheckUnaryOperator (Operator.OpType op, Location l)
5180 {
5181         switch (op) {
5182                 
5183         case Operator.OpType.LogicalNot: 
5184         case Operator.OpType.OnesComplement: 
5185         case Operator.OpType.Increment:
5186         case Operator.OpType.Decrement:
5187         case Operator.OpType.True: 
5188         case Operator.OpType.False: 
5189         case Operator.OpType.Addition: 
5190         case Operator.OpType.Subtraction:
5191                 
5192                 break;
5193                 
5194         default :
5195                 Report.Error (1019, l, "Overloadable unary operator expected"); 
5196                 break;
5197                 
5198         }
5199 }
5200
5201 void CheckBinaryOperator (Operator.OpType op, Location l)
5202 {
5203         switch (op) {
5204                 
5205         case Operator.OpType.Addition: 
5206         case Operator.OpType.Subtraction: 
5207         case Operator.OpType.Multiply:
5208         case Operator.OpType.Division:
5209         case Operator.OpType.Modulus: 
5210         case Operator.OpType.BitwiseAnd: 
5211         case Operator.OpType.BitwiseOr:
5212         case Operator.OpType.ExclusiveOr: 
5213         case Operator.OpType.LeftShift: 
5214         case Operator.OpType.RightShift:
5215         case Operator.OpType.Equality: 
5216         case Operator.OpType.Inequality:
5217         case Operator.OpType.GreaterThan: 
5218         case Operator.OpType.LessThan: 
5219         case Operator.OpType.GreaterThanOrEqual:
5220         case Operator.OpType.LessThanOrEqual:
5221                 break;
5222                 
5223         default :
5224                 Report.Error (1020, l, "Overloadable binary operator expected");
5225                 break;
5226         }
5227         
5228 }
5229
5230 void syntax_error (Location l, string msg)
5231 {
5232         Report.Error (1003, l, "Syntax error, " + msg);
5233 }
5234
5235 void note (string s)
5236 {
5237         // Used to put annotations
5238 }
5239
5240 Tokenizer lexer;
5241
5242 public Tokenizer Lexer {
5243         get {
5244                 return lexer;
5245         }
5246 }                  
5247
5248 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
5249 {
5250         current_namespace = new NamespaceEntry (null, file, null, Location.Null);
5251         this.name = file.Name;
5252         this.file = file;
5253         current_container = RootContext.Tree.Types;
5254         // TODO: Make RootContext.Tree.Types a PartialContainer.
5255         current_class = current_container;
5256         current_container.NamespaceEntry = current_namespace;
5257         oob_stack = new Stack ();
5258         switch_stack = new Stack ();
5259
5260         lexer = new Tokenizer (reader, file, defines);
5261 }
5262
5263 public void parse ()
5264 {
5265         try {
5266                 if (yacc_verbose_flag > 1)
5267                         yyparse (lexer, new yydebug.yyDebugSimple ());
5268                 else
5269                         yyparse (lexer);
5270                 Tokenizer tokenizer = lexer as Tokenizer;
5271                 tokenizer.cleanup ();
5272         } catch (Exception e){
5273                 //
5274                 // Removed for production use, use parser verbose to get the output.
5275                 //
5276                 // Console.WriteLine (e);
5277                 Report.Error (-25, lexer.Location, "Parsing error");
5278                 if (yacc_verbose_flag > 0)
5279                         Console.WriteLine (e);
5280         }
5281
5282         RootContext.Tree.Types.NamespaceEntry = null;
5283 }
5284
5285 void CheckToken (int error, int yyToken, string msg, Location loc)
5286 {
5287         if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
5288                 Report.Error (error, loc, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
5289                 return;
5290         }               
5291         Report.Error (error, loc, msg);
5292 }
5293
5294 void CheckIdentifierToken (int yyToken, Location loc)
5295 {
5296         CheckToken (1041, yyToken, "Identifier expected", loc);
5297 }
5298
5299 string ConsumeStoredComment ()
5300 {
5301         string s = tmpComment;
5302         tmpComment = null;
5303         Lexer.doc_state = XmlCommentState.Allowed;
5304         return s;
5305 }
5306
5307 Location GetLocation (object obj)
5308 {
5309         if (obj is MemberCore)
5310                 return ((MemberCore) obj).Location;
5311         if (obj is MemberName)
5312                 return ((MemberName) obj).Location;
5313         if (obj is LocatedToken)
5314                 return ((LocatedToken) obj).Location;
5315         if (obj is Location)
5316                 return (Location) obj;
5317         return lexer.Location;
5318 }
5319
5320 /* end end end */
5321 }