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