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