e6b9826d4d50fbd8e4c87bb5b16a26180f42eb61
[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                 $$ = new ComposedCast ((Expression) $1, (string) $2);
2747           }
2748         | non_expression_type STAR
2749           {
2750                 $$ = new ComposedCast ((Expression) $1, "*");
2751           }
2752         | expression rank_specifiers 
2753           {
2754                 $$ = new ComposedCast ((Expression) $1, (string) $2);
2755           }
2756         | expression STAR 
2757           {
2758                 $$ = new ComposedCast ((Expression) $1, "*");
2759           }
2760         
2761         //
2762         // We need this because the parser will happily go and reduce IDENTIFIER STAR
2763         // through this different path
2764         //
2765         | multiplicative_expression STAR 
2766           {
2767                 $$ = new ComposedCast ((Expression) $1, "*");
2768           }
2769         ;
2770
2771 type_list
2772         : type
2773           {
2774                 ArrayList types = new ArrayList (4);
2775
2776                 types.Add ($1);
2777                 $$ = types;
2778           }
2779         | type_list COMMA type
2780           {
2781                 ArrayList types = (ArrayList) $1;
2782
2783                 types.Add ($3);
2784                 $$ = types;
2785           }
2786         ;
2787
2788 /*
2789  * replaces all the productions for isolating the various
2790  * simple types, but we need this to reuse it easily in local_variable_type
2791  */
2792 builtin_types
2793         : OBJECT        { $$ = TypeManager.system_object_expr; }
2794         | STRING        { $$ = TypeManager.system_string_expr; }
2795         | BOOL          { $$ = TypeManager.system_boolean_expr; }
2796         | DECIMAL       { $$ = TypeManager.system_decimal_expr; }
2797         | FLOAT         { $$ = TypeManager.system_single_expr; }
2798         | DOUBLE        { $$ = TypeManager.system_double_expr; }
2799         | integral_type
2800         ;
2801
2802 integral_type
2803         : SBYTE         { $$ = TypeManager.system_sbyte_expr; }
2804         | BYTE          { $$ = TypeManager.system_byte_expr; }
2805         | SHORT         { $$ = TypeManager.system_int16_expr; }
2806         | USHORT        { $$ = TypeManager.system_uint16_expr; }
2807         | INT           { $$ = TypeManager.system_int32_expr; }
2808         | UINT          { $$ = TypeManager.system_uint32_expr; }
2809         | LONG          { $$ = TypeManager.system_int64_expr; }
2810         | ULONG         { $$ = TypeManager.system_uint64_expr; }
2811         | CHAR          { $$ = TypeManager.system_char_expr; }
2812         | VOID          { $$ = TypeManager.system_void_expr; }
2813         ;
2814
2815 array_type
2816         : type rank_specifiers opt_nullable
2817           {
2818                 string rank_specifiers = (string) $2;
2819                 if ((bool) $3)
2820                         rank_specifiers += "?";
2821
2822                 $$ = new ComposedCast ((Expression) $1, rank_specifiers);
2823           }
2824         ;
2825
2826 //
2827 // Expressions, section 7.5
2828 //
2829 primary_expression
2830         : literal
2831           {
2832                 // 7.5.1: Literals
2833           }
2834         | member_name
2835           {
2836                 MemberName mn = (MemberName) $1;
2837                 $$ = mn.GetTypeExpression ();
2838           }
2839         | IDENTIFIER DOUBLE_COLON IDENTIFIER
2840           {
2841                 LocatedToken lt1 = (LocatedToken) $1;
2842                 LocatedToken lt2 = (LocatedToken) $3;
2843                 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, lt2.Location);
2844           }
2845         | parenthesized_expression
2846         | default_value_expression
2847         | member_access
2848         | invocation_expression
2849         | element_access
2850         | this_access
2851         | base_access
2852         | post_increment_expression
2853         | post_decrement_expression
2854         | new_expression
2855         | typeof_expression
2856         | sizeof_expression
2857         | checked_expression
2858         | unchecked_expression
2859         | pointer_member_access
2860         | anonymous_method_expression
2861         ;
2862
2863 literal
2864         : boolean_literal
2865         | integer_literal
2866         | real_literal
2867         | LITERAL_CHARACTER     { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
2868         | LITERAL_STRING        { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); } 
2869         | NULL                  { $$ = new NullLiteral (lexer.Location); }
2870         ;
2871
2872 real_literal
2873         : LITERAL_FLOAT         { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
2874         | LITERAL_DOUBLE        { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
2875         | LITERAL_DECIMAL       { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
2876         ;
2877
2878 integer_literal
2879         : LITERAL_INTEGER       { 
2880                 object v = lexer.Value;
2881
2882                 if (v is int){
2883                         $$ = new IntLiteral ((int) v, lexer.Location);
2884                 } else if (v is uint)
2885                         $$ = new UIntLiteral ((UInt32) v, lexer.Location);
2886                 else if (v is long)
2887                         $$ = new LongLiteral ((Int64) v, lexer.Location);
2888                 else if (v is ulong)
2889                         $$ = new ULongLiteral ((UInt64) v, lexer.Location);
2890                 else
2891                         Console.WriteLine ("OOPS.  Unexpected result from scanner");
2892           }
2893         ;
2894
2895 boolean_literal
2896         : TRUE                  { $$ = new BoolLiteral (true, lexer.Location); }
2897         | FALSE                 { $$ = new BoolLiteral (false, lexer.Location); }
2898         ;
2899
2900 parenthesized_expression_0
2901         : OPEN_PARENS expression CLOSE_PARENS
2902           {
2903                 $$ = $2;
2904                 lexer.Deambiguate_CloseParens ();
2905                 // After this, the next token returned is one of
2906                 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2907                 // or CLOSE_PARENS_MINUS.
2908           }
2909         ;
2910
2911 parenthesized_expression
2912         : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2913           {
2914                 $$ = $1;
2915           }
2916         | parenthesized_expression_0 CLOSE_PARENS_MINUS
2917           {
2918                 // If a parenthesized expression is followed by a minus, we need to wrap
2919                 // the expression inside a ParenthesizedExpression for the CS0075 check
2920                 // in Binary.DoResolve().
2921                 $$ = new ParenthesizedExpression ((Expression) $1);
2922           }
2923         ;;
2924
2925 member_access
2926         : primary_expression DOT IDENTIFIER opt_type_argument_list
2927           {
2928                 LocatedToken lt = (LocatedToken) $3;
2929                 $$ = new MemberAccess ((Expression) $1, lt.Value,
2930                                        (TypeArguments) $4, lt.Location);
2931           }
2932         | predefined_type 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         ;
2939
2940 predefined_type
2941         : builtin_types
2942         ;
2943
2944 invocation_expression
2945         : primary_expression {
2946                 $$ = lexer.Location;
2947           } OPEN_PARENS opt_argument_list CLOSE_PARENS
2948           {
2949                 if ($1 == null) {
2950                         Location l = (Location) $3;
2951                         Report.Error (1, l, "Parse error");
2952                 }
2953                 $$ = new Invocation ((Expression) $1, (ArrayList) $4);
2954           }
2955         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2956           {
2957                 $$ = new Invocation ((Expression) $1, new ArrayList ());
2958           }
2959         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2960           {
2961                 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
2962           }
2963         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2964           {
2965                 ArrayList args = new ArrayList (1);
2966                 args.Add ($4);
2967                 $$ = new Invocation ((Expression) $1, args);
2968           }
2969         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2970           {
2971                 ArrayList args = ((ArrayList) $4);
2972                 args.Add ($6);
2973                 $$ = new Invocation ((Expression) $1, args);
2974           }
2975         ;
2976
2977 opt_argument_list
2978         : /* empty */           { $$ = null; }
2979         | argument_list
2980         ;
2981
2982 argument_list
2983         : argument              
2984           { 
2985                 ArrayList list = new ArrayList (4);
2986                 list.Add ($1);
2987                 $$ = list;
2988           }
2989         | argument_list COMMA argument
2990           {
2991                 ArrayList list = (ArrayList) $1;
2992                 list.Add ($3);
2993                 $$ = list;
2994           }
2995         | argument_list error {
2996                 CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
2997                 $$ = null;
2998           }
2999         ;
3000
3001 argument
3002         : expression
3003           {
3004                 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
3005           }
3006         | non_simple_argument
3007           {
3008                 $$ = $1;
3009           }
3010         ;
3011
3012 non_simple_argument
3013         : REF variable_reference 
3014           { 
3015                 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
3016           }
3017         | OUT variable_reference 
3018           { 
3019                 $$ = new Argument ((Expression) $2, Argument.AType.Out);
3020           }
3021         | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
3022           {
3023                 ArrayList list = (ArrayList) $3;
3024                 Argument[] args = new Argument [list.Count];
3025                 list.CopyTo (args, 0);
3026
3027                 Expression expr = new Arglist (args, (Location) $1);
3028                 $$ = new Argument (expr, Argument.AType.Expression);
3029           }
3030         | ARGLIST
3031           {
3032                 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
3033           }
3034         ;
3035
3036 variable_reference
3037         : expression { note ("section 5.4"); $$ = $1; }
3038         ;
3039
3040 element_access
3041         : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET 
3042           {
3043                 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
3044           }
3045         | primary_expression rank_specifiers
3046           {
3047                 // So the super-trick is that primary_expression
3048                 // can only be either a SimpleName or a MemberAccess. 
3049                 // The MemberAccess case arises when you have a fully qualified type-name like :
3050                 // Foo.Bar.Blah i;
3051                 // SimpleName is when you have
3052                 // Blah i;
3053                   
3054                 Expression expr = (Expression) $1;  
3055                 if (expr is ComposedCast){
3056                         $$ = new ComposedCast (expr, (string) $2);
3057                 } else if (!(expr is SimpleName || expr is MemberAccess || expr is ConstructedType || expr is QualifiedAliasMember)){
3058                         Error_ExpectingTypeName (expr);
3059                         $$ = TypeManager.system_object_expr;
3060                 } else {
3061                         //
3062                         // So we extract the string corresponding to the SimpleName
3063                         // or MemberAccess
3064                         // 
3065                         $$ = new ComposedCast (expr, (string) $2);
3066                 }
3067           }
3068         ;
3069
3070 expression_list
3071         : expression
3072           {
3073                 ArrayList list = new ArrayList (4);
3074                 list.Add ($1);
3075                 $$ = list;
3076           }
3077         | expression_list COMMA expression
3078           {
3079                 ArrayList list = (ArrayList) $1;
3080                 list.Add ($3);
3081                 $$ = list;
3082           }
3083         ;
3084
3085 this_access
3086         : THIS
3087           {
3088                 $$ = new This (current_block, (Location) $1);
3089           }
3090         ;
3091
3092 base_access
3093         : BASE DOT IDENTIFIER
3094           {
3095                 LocatedToken lt = (LocatedToken) $3;
3096                 $$ = new BaseAccess (lt.Value, lt.Location);
3097           }
3098         | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
3099           {
3100                 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
3101           }
3102         | BASE error {
3103                 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
3104                 $$ = null;
3105           }
3106         ;
3107
3108 post_increment_expression
3109         : primary_expression OP_INC
3110           {
3111                 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
3112                                        (Expression) $1, (Location) $2);
3113           }
3114         ;
3115
3116 post_decrement_expression
3117         : primary_expression OP_DEC
3118           {
3119                 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
3120                                        (Expression) $1, (Location) $2);
3121           }
3122         ;
3123
3124 new_expression
3125         : object_or_delegate_creation_expression
3126         | array_creation_expression
3127         ;
3128
3129 object_or_delegate_creation_expression
3130         : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
3131           {
3132                 $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
3133           }
3134         ;
3135
3136 array_creation_expression
3137         : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET 
3138           opt_rank_specifier
3139           opt_array_initializer
3140           {
3141                 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
3142           }
3143         | NEW type rank_specifiers array_initializer
3144           {
3145                 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, (Location) $1);
3146           }
3147         | NEW error
3148           {
3149                 Report.Error (1031, (Location) $1, "Type expected");
3150                 $$ = null;
3151           }          
3152         | NEW type error 
3153           {
3154                 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
3155                 $$ = null;
3156           }
3157         ;
3158
3159 opt_rank_specifier
3160         : /* empty */
3161           {
3162                   $$ = "";
3163           }
3164         | rank_specifiers
3165           {
3166                         $$ = $1;
3167           }
3168         ;
3169
3170 opt_rank_specifier_or_nullable
3171         : /* empty */
3172           {
3173                 $$ = "";
3174           }
3175         | INTERR
3176           {
3177                 $$ = "?";
3178           }
3179         | opt_nullable rank_specifiers
3180           {
3181                 if ((bool) $1)
3182                         $$ = "?" + $2;
3183                 else
3184                         $$ = $2;
3185           }
3186         | opt_nullable rank_specifiers INTERR
3187           {
3188                 if ((bool) $1)
3189                         $$ = "?" + $2 + "?";
3190                 else
3191                         $$ = $2 + "?";
3192           }
3193         ;
3194
3195 rank_specifiers
3196         : rank_specifier opt_rank_specifier
3197           {
3198                   $$ = (string) $2 + (string) $1;
3199           }
3200         ;
3201
3202 rank_specifier
3203         : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3204           {
3205                 $$ = "[" + (string) $2 + "]";
3206           }
3207         ;
3208
3209 opt_dim_separators
3210         : /* empty */
3211           {
3212                 $$ = "";
3213           }
3214         | dim_separators
3215           {
3216                   $$ = $1;
3217           }               
3218         ;
3219
3220 dim_separators
3221         : COMMA
3222           {
3223                 $$ = ",";
3224           }
3225         | dim_separators COMMA
3226           {
3227                 $$ = (string) $1 + ",";
3228           }
3229         ;
3230
3231 opt_array_initializer
3232         : /* empty */
3233           {
3234                 $$ = null;
3235           }
3236         | array_initializer
3237           {
3238                 $$ = $1;
3239           }
3240         ;
3241
3242 array_initializer
3243         : OPEN_BRACE CLOSE_BRACE
3244           {
3245                 ArrayList list = new ArrayList (4);
3246                 $$ = list;
3247           }
3248         | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3249           {
3250                 $$ = (ArrayList) $2;
3251           }
3252         ;
3253
3254 variable_initializer_list
3255         : variable_initializer
3256           {
3257                 ArrayList list = new ArrayList (4);
3258                 list.Add ($1);
3259                 $$ = list;
3260           }
3261         | variable_initializer_list COMMA variable_initializer
3262           {
3263                 ArrayList list = (ArrayList) $1;
3264                 list.Add ($3);
3265                 $$ = list;
3266           }
3267         ;
3268
3269 void_pointer_expression
3270         : void_pointer_expression STAR
3271           {
3272                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3273           }
3274         | VOID STAR
3275           {
3276                 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3277           }
3278         ;
3279
3280 typeof_expression
3281         : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
3282           {
3283                 $$ = new TypeOfVoid ((Location) $1);
3284           }
3285         | TYPEOF OPEN_PARENS void_pointer_expression CLOSE_PARENS
3286           {
3287                 $$ = new TypeOf ((Expression) $3, (Location) $1);
3288           }
3289         | TYPEOF OPEN_PARENS
3290           {
3291                 lexer.TypeOfParsing = true;
3292           }
3293           type CLOSE_PARENS
3294           {
3295                 lexer.TypeOfParsing = false;
3296                 $$ = new TypeOf ((Expression) $4, lexer.Location);
3297           }
3298         ;
3299
3300 sizeof_expression
3301         : SIZEOF OPEN_PARENS type CLOSE_PARENS { 
3302                 $$ = new SizeOf ((Expression) $3, (Location) $1);
3303           }
3304         ;
3305
3306 checked_expression
3307         : CHECKED OPEN_PARENS expression CLOSE_PARENS
3308           {
3309                 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3310           }
3311         ;
3312
3313 unchecked_expression
3314         : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3315           {
3316                 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3317           }
3318         ;
3319
3320 pointer_member_access 
3321         : primary_expression OP_PTR IDENTIFIER
3322           {
3323                 Expression deref;
3324                 LocatedToken lt = (LocatedToken) $3;
3325
3326                 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
3327                 $$ = new MemberAccess (deref, lt.Value, lt.Location);
3328           }
3329         ;
3330
3331 anonymous_method_expression
3332         : DELEGATE opt_anonymous_method_signature
3333           {
3334                 oob_stack.Push (current_local_parameters);
3335                 current_local_parameters = (Parameters)$2;
3336
3337                 // Force the next block to be created as a ToplevelBlock
3338                 oob_stack.Push (current_block);
3339                 oob_stack.Push (top_current_block);
3340                 current_block = null;
3341           } 
3342           block
3343           {
3344                 Location loc = (Location) $1;
3345                 top_current_block = (Block) oob_stack.Pop ();
3346                 current_block = (Block) oob_stack.Pop ();
3347                 if (RootContext.Version == LanguageVersion.ISO_1){
3348                         Report.FeatureIsNotStandardized (loc, "anonymous methods");
3349                         $$ = null;
3350                 } else  {
3351                         ToplevelBlock anon_block = (ToplevelBlock) $4;
3352
3353                         anon_block.Parent = current_block;
3354                         $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block, 
3355                                 anon_block, loc);
3356                 }
3357                         current_local_parameters = (Parameters) oob_stack.Pop ();
3358                 }
3359         ;
3360
3361 opt_anonymous_method_signature
3362         : /* empty */                   { $$ = null; } 
3363         | anonymous_method_signature
3364         ;
3365
3366 anonymous_method_signature
3367         : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS 
3368           {
3369                 if ($2 == null)
3370                         $$ = Parameters.EmptyReadOnlyParameters;
3371                 else {
3372                         ArrayList par_list = (ArrayList) $2;
3373                         Parameter [] pars = new Parameter [par_list.Count];
3374                         par_list.CopyTo (pars);
3375                         $$ = new Parameters (pars, null);
3376                 }
3377           }
3378         ;
3379
3380 opt_anonymous_method_parameter_list
3381         : /* empty */   { $$ = null; } 
3382         | anonymous_method_parameter_list  { $$ = $1; }
3383         ;
3384
3385 anonymous_method_parameter_list
3386         : anonymous_method_parameter 
3387           {
3388                 ArrayList a = new ArrayList (4);
3389                 a.Add ($1);
3390                 $$ = a;
3391           }
3392         | anonymous_method_parameter_list COMMA anonymous_method_parameter 
3393           {
3394                 ArrayList a = (ArrayList) $1;
3395                 a.Add ($3);
3396                 $$ = a;
3397           }
3398         ; 
3399
3400 anonymous_method_parameter
3401         : opt_parameter_modifier type IDENTIFIER {
3402                 LocatedToken lt = (LocatedToken) $3;
3403                 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3404           }
3405         | PARAMS type IDENTIFIER {
3406                 Report.Error (1670, ((LocatedToken) $3).Location, "The `params' modifier is not allowed in anonymous method declaration");
3407                 $$ = null;
3408           }
3409         ;
3410
3411 default_value_expression
3412         : DEFAULT_OPEN_PARENS type CLOSE_PARENS
3413           {
3414                 $$ = new DefaultValueExpression ((Expression) $2, lexer.Location);
3415           }
3416         ;
3417
3418 unary_expression
3419         : primary_expression
3420         | BANG prefixed_unary_expression
3421           {
3422                 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3423           }
3424         | TILDE prefixed_unary_expression
3425           {
3426                 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3427           }
3428         | cast_expression
3429         ;
3430
3431 cast_list
3432         : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3433           {
3434                 $$ = new Cast ((Expression) $1, (Expression) $3);
3435           }
3436         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3437           {
3438                 $$ = new Cast ((Expression) $1, (Expression) $3);
3439           }     
3440         ;
3441
3442 cast_expression
3443         : cast_list
3444         | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3445           {
3446                 // TODO: wrong location
3447                 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3448           }
3449         ;
3450
3451         //
3452         // The idea to split this out is from Rhys' grammar
3453         // to solve the problem with casts.
3454         //
3455 prefixed_unary_expression
3456         : unary_expression
3457         | PLUS prefixed_unary_expression
3458           { 
3459                 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3460           } 
3461         | MINUS prefixed_unary_expression 
3462           { 
3463                 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3464           }
3465         | OP_INC prefixed_unary_expression 
3466           {
3467                 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3468                                        (Expression) $2, (Location) $1);
3469           }
3470         | OP_DEC prefixed_unary_expression 
3471           {
3472                 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3473                                        (Expression) $2, (Location) $1);
3474           }
3475         | STAR prefixed_unary_expression
3476           {
3477                 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, (Location) $1);
3478           }
3479         | BITWISE_AND prefixed_unary_expression
3480           {
3481                 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3482           }
3483         ;
3484
3485 pre_increment_expression
3486         : OP_INC prefixed_unary_expression 
3487           {
3488                 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3489                                        (Expression) $2, (Location) $1);
3490           }
3491         ;
3492
3493 pre_decrement_expression
3494         : OP_DEC prefixed_unary_expression 
3495           {
3496                 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3497                                        (Expression) $2, (Location) $1);
3498           }
3499         ;
3500
3501 multiplicative_expression
3502         : prefixed_unary_expression
3503         | multiplicative_expression STAR prefixed_unary_expression
3504           {
3505                 $$ = new Binary (Binary.Operator.Multiply, 
3506                                  (Expression) $1, (Expression) $3);
3507           }
3508         | multiplicative_expression DIV prefixed_unary_expression
3509           {
3510                 $$ = new Binary (Binary.Operator.Division, 
3511                                  (Expression) $1, (Expression) $3);
3512           }
3513         | multiplicative_expression PERCENT prefixed_unary_expression 
3514           {
3515                 $$ = new Binary (Binary.Operator.Modulus, 
3516                                  (Expression) $1, (Expression) $3);
3517           }
3518         ;
3519
3520 additive_expression
3521         : multiplicative_expression
3522         | additive_expression PLUS multiplicative_expression 
3523           {
3524                 $$ = new Binary (Binary.Operator.Addition, 
3525                                  (Expression) $1, (Expression) $3);
3526           }
3527         | additive_expression MINUS multiplicative_expression
3528           {
3529                 $$ = new Binary (Binary.Operator.Subtraction, 
3530                                  (Expression) $1, (Expression) $3);
3531           }
3532         ;
3533
3534 shift_expression
3535         : additive_expression
3536         | shift_expression OP_SHIFT_LEFT additive_expression
3537           {
3538                 $$ = new Binary (Binary.Operator.LeftShift, 
3539                                  (Expression) $1, (Expression) $3);
3540           }
3541         | shift_expression OP_SHIFT_RIGHT additive_expression
3542           {
3543                 $$ = new Binary (Binary.Operator.RightShift, 
3544                                  (Expression) $1, (Expression) $3);
3545           }
3546         ; 
3547
3548 opt_error
3549         : /* empty */
3550           {
3551                 $$ = false;
3552           }
3553         | error
3554           {
3555                 lexer.PutbackNullable ();
3556                 $$ = true;
3557           }
3558         ;
3559
3560 nullable_type_or_conditional
3561         : type opt_error
3562           {
3563                 if (((bool) $2) && ($1 is ComposedCast))
3564                         $$ = ((ComposedCast) $1).RemoveNullable ();
3565                 else
3566                         $$ = $1;
3567           }
3568         ;
3569
3570 relational_expression
3571         : shift_expression
3572         | relational_expression OP_LT shift_expression
3573           {
3574                 $$ = new Binary (Binary.Operator.LessThan, 
3575                                  (Expression) $1, (Expression) $3);
3576           }
3577         | relational_expression OP_GT shift_expression
3578           {
3579                 $$ = new Binary (Binary.Operator.GreaterThan, 
3580                                  (Expression) $1, (Expression) $3);
3581           }
3582         | relational_expression OP_LE shift_expression
3583           {
3584                 $$ = new Binary (Binary.Operator.LessThanOrEqual, 
3585                                  (Expression) $1, (Expression) $3);
3586           }
3587         | relational_expression OP_GE shift_expression
3588           {
3589                 $$ = new Binary (Binary.Operator.GreaterThanOrEqual, 
3590                                  (Expression) $1, (Expression) $3);
3591           }
3592         | relational_expression IS
3593           {
3594                 yyErrorFlag = 3;
3595           } nullable_type_or_conditional
3596           {
3597                 $$ = new Is ((Expression) $1, (Expression) $4, (Location) $2);
3598           }
3599         | relational_expression AS
3600           {
3601                 yyErrorFlag = 3;
3602           } nullable_type_or_conditional
3603           {
3604                 $$ = new As ((Expression) $1, (Expression) $4, (Location) $2);
3605           }
3606         ;
3607
3608 equality_expression
3609         : relational_expression
3610         | equality_expression OP_EQ relational_expression
3611           {
3612                 $$ = new Binary (Binary.Operator.Equality, 
3613                                  (Expression) $1, (Expression) $3);
3614           }
3615         | equality_expression OP_NE relational_expression
3616           {
3617                 $$ = new Binary (Binary.Operator.Inequality, 
3618                                  (Expression) $1, (Expression) $3);
3619           }
3620         ; 
3621
3622 and_expression
3623         : equality_expression
3624         | and_expression BITWISE_AND equality_expression
3625           {
3626                 $$ = new Binary (Binary.Operator.BitwiseAnd, 
3627                                  (Expression) $1, (Expression) $3);
3628           }
3629         ;
3630
3631 exclusive_or_expression
3632         : and_expression
3633         | exclusive_or_expression CARRET and_expression
3634           {
3635                 $$ = new Binary (Binary.Operator.ExclusiveOr, 
3636                                  (Expression) $1, (Expression) $3);
3637           }
3638         ;
3639
3640 inclusive_or_expression
3641         : exclusive_or_expression
3642         | inclusive_or_expression BITWISE_OR exclusive_or_expression
3643           {
3644                 $$ = new Binary (Binary.Operator.BitwiseOr, 
3645                                  (Expression) $1, (Expression) $3);
3646           }
3647         ;
3648
3649 conditional_and_expression
3650         : inclusive_or_expression
3651         | conditional_and_expression OP_AND inclusive_or_expression
3652           {
3653                 $$ = new Binary (Binary.Operator.LogicalAnd, 
3654                                  (Expression) $1, (Expression) $3);
3655           }
3656         ;
3657
3658 conditional_or_expression
3659         : conditional_and_expression
3660         | conditional_or_expression OP_OR conditional_and_expression
3661           {
3662                 $$ = new Binary (Binary.Operator.LogicalOr, 
3663                                  (Expression) $1, (Expression) $3);
3664           }
3665         ;
3666
3667 conditional_expression
3668         : conditional_or_expression
3669         | conditional_or_expression INTERR expression COLON expression 
3670           {
3671                 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
3672           }
3673         | conditional_or_expression INTERR INTERR expression
3674           {
3675                 $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $4, lexer.Location);
3676           }
3677         // We'll be resolved into a `parenthesized_expression_0' later on.
3678         | conditional_or_expression INTERR CLOSE_PARENS
3679           {
3680                 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
3681                 lexer.PutbackCloseParens ();
3682           }
3683         ;
3684
3685 assignment_expression
3686         : prefixed_unary_expression ASSIGN expression
3687           {
3688                 $$ = new Assign ((Expression) $1, (Expression) $3);
3689           }
3690         | prefixed_unary_expression OP_MULT_ASSIGN expression
3691           {
3692                 $$ = new CompoundAssign (
3693                         Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
3694           }
3695         | prefixed_unary_expression OP_DIV_ASSIGN expression
3696           {
3697                 $$ = new CompoundAssign (
3698                         Binary.Operator.Division, (Expression) $1, (Expression) $3);
3699           }
3700         | prefixed_unary_expression OP_MOD_ASSIGN expression
3701           {
3702                 $$ = new CompoundAssign (
3703                         Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
3704           }
3705         | prefixed_unary_expression OP_ADD_ASSIGN expression
3706           {
3707                 $$ = new CompoundAssign (
3708                         Binary.Operator.Addition, (Expression) $1, (Expression) $3);
3709           }
3710         | prefixed_unary_expression OP_SUB_ASSIGN expression
3711           {
3712                 $$ = new CompoundAssign (
3713                         Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3714           }
3715         | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3716           {
3717                 $$ = new CompoundAssign (
3718                         Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
3719           }
3720         | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3721           {
3722                 $$ = new CompoundAssign (
3723                         Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
3724           }
3725         | prefixed_unary_expression OP_AND_ASSIGN expression
3726           {
3727                 $$ = new CompoundAssign (
3728                         Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
3729           }
3730         | prefixed_unary_expression OP_OR_ASSIGN expression
3731           {
3732                 $$ = new CompoundAssign (
3733                         Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
3734           }
3735         | prefixed_unary_expression OP_XOR_ASSIGN expression
3736           {
3737                 $$ = new CompoundAssign (
3738                         Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
3739           }
3740         ;
3741
3742 expression
3743         : conditional_expression
3744         | assignment_expression
3745         ;
3746
3747 constant_expression
3748         : expression
3749         ;
3750
3751 boolean_expression
3752         : expression
3753         ;
3754
3755 //
3756 // 10 classes
3757 //
3758 class_declaration
3759         : opt_attributes
3760           opt_modifiers
3761           opt_partial
3762           CLASS
3763           {
3764                 lexer.ConstraintsParsing = true;
3765           }
3766           member_name
3767           {
3768                 MemberName name = MakeName ((MemberName) $6);
3769                 int mod_flags = (int) $2;
3770
3771                 if ($3 != null) {
3772                         ClassPart part = PartialContainer.CreatePart (
3773                                 current_namespace, current_class, name, mod_flags,
3774                                 (Attributes) $1, Kind.Class, (Location) $3);
3775
3776                         current_container = part.PartialContainer;
3777                         current_class = part;
3778                 } else {
3779                         if ((mod_flags & Modifiers.STATIC) != 0) {
3780                                 current_class = new StaticClass (
3781                                         current_namespace, current_class, name,
3782                                         mod_flags, (Attributes) $1);
3783                         } else {
3784                                 current_class = new Class (
3785                                         current_namespace, current_class, name,
3786                                         mod_flags, (Attributes) $1);
3787                         }
3788
3789                         current_container.AddClassOrStruct (current_class);
3790                         current_container = current_class;
3791                         RootContext.Tree.RecordDecl (current_namespace.NS, name, current_class);
3792                 }
3793           }
3794           opt_class_base
3795           opt_type_parameter_constraints_clauses
3796           {
3797                 lexer.ConstraintsParsing = false;
3798
3799                 if ($8 != null) {
3800                         if (current_class.Name == "System.Object") {
3801                                 Report.Error (537, current_class.Location,
3802                                               "The class System.Object cannot have a base " +
3803                                               "class or implement an interface.");
3804                         }
3805                         current_class.Bases = (ArrayList) $8;
3806                 }
3807
3808                 current_class.SetParameterInfo ((ArrayList) $9);
3809
3810                 if (RootContext.Documentation != null) {
3811                         current_class.DocComment = Lexer.consume_doc_comment ();
3812                         Lexer.doc_state = XmlCommentState.Allowed;
3813                 }
3814           }
3815           class_body
3816           {
3817                 if (RootContext.Documentation != null)
3818                         Lexer.doc_state = XmlCommentState.Allowed;
3819           }
3820           opt_semicolon 
3821           {
3822                 $$ = pop_current_class ();
3823           }
3824         ;       
3825
3826 opt_partial
3827         : /* empty */
3828           { $$ = null; }
3829         | PARTIAL
3830           { $$ = $1; } // location
3831         ;
3832
3833 opt_modifiers
3834         : /* empty */           { $$ = (int) 0; }
3835         | modifiers
3836         ;
3837
3838 modifiers
3839         : modifier
3840         | modifiers modifier
3841           { 
3842                 int m1 = (int) $1;
3843                 int m2 = (int) $2;
3844
3845                 if ((m1 & m2) != 0) {
3846                         Location l = lexer.Location;
3847                         Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
3848                 }
3849                 $$ = (int) (m1 | m2);
3850           }
3851         ;
3852
3853 modifier
3854         : NEW                   { $$ = Modifiers.NEW; }
3855         | PUBLIC                { $$ = Modifiers.PUBLIC; }
3856         | PROTECTED             { $$ = Modifiers.PROTECTED; }
3857         | INTERNAL              { $$ = Modifiers.INTERNAL; }
3858         | PRIVATE               { $$ = Modifiers.PRIVATE; }
3859         | ABSTRACT              { $$ = Modifiers.ABSTRACT; }
3860         | SEALED                { $$ = Modifiers.SEALED; }
3861         | STATIC                { $$ = Modifiers.STATIC; }
3862         | READONLY              { $$ = Modifiers.READONLY; }
3863         | VIRTUAL               { $$ = Modifiers.VIRTUAL; }
3864         | OVERRIDE              { $$ = Modifiers.OVERRIDE; }
3865         | EXTERN                { $$ = Modifiers.EXTERN; }
3866         | VOLATILE              { $$ = Modifiers.VOLATILE; }
3867         | UNSAFE                { $$ = Modifiers.UNSAFE; }
3868         ;
3869
3870 opt_class_base
3871         : /* empty */           { $$ = null; }
3872         | class_base            { $$ = $1;   }
3873         ;
3874
3875 class_base
3876         : COLON type_list { $$ = $2; }
3877         ;
3878
3879 opt_type_parameter_constraints_clauses
3880         : /* empty */           { $$ = null; }
3881         | type_parameter_constraints_clauses 
3882           { $$ = $1; }
3883         ;
3884
3885 type_parameter_constraints_clauses
3886         : type_parameter_constraints_clause {
3887                 ArrayList constraints = new ArrayList (1);
3888                 constraints.Add ($1);
3889                 $$ = constraints;
3890           }
3891         | type_parameter_constraints_clauses type_parameter_constraints_clause {
3892                 ArrayList constraints = (ArrayList) $1;
3893
3894                 constraints.Add ($2);
3895                 $$ = constraints;
3896           }
3897         ; 
3898
3899 type_parameter_constraints_clause
3900         : WHERE IDENTIFIER COLON type_parameter_constraints {
3901                 LocatedToken lt = (LocatedToken) $2;
3902                 $$ = new Constraints (lt.Value, (ArrayList) $4, lt.Location);
3903           }
3904         ; 
3905
3906 type_parameter_constraints
3907         : type_parameter_constraint {
3908                 ArrayList constraints = new ArrayList (1);
3909                 constraints.Add ($1);
3910                 $$ = constraints;
3911           }
3912         | type_parameter_constraints COMMA type_parameter_constraint {
3913                 ArrayList constraints = (ArrayList) $1;
3914
3915                 constraints.Add ($3);
3916                 $$ = constraints;
3917           }
3918         ;
3919
3920 type_parameter_constraint
3921         : type
3922         | NEW OPEN_PARENS CLOSE_PARENS {
3923                 $$ = SpecialConstraint.Constructor;
3924           }
3925         | CLASS {
3926                 $$ = SpecialConstraint.ReferenceType;
3927           }
3928         | STRUCT {
3929                 $$ = SpecialConstraint.ValueType;
3930           }
3931         ;
3932
3933 //
3934 // Statements (8.2)
3935 //
3936
3937 //
3938 // A block is "contained" on the following places:
3939 //      method_body
3940 //      property_declaration as part of the accessor body (get/set)
3941 //      operator_declaration
3942 //      constructor_declaration
3943 //      destructor_declaration
3944 //      event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3945 //      
3946 block
3947         : OPEN_BRACE 
3948           {
3949                 if (current_block == null){
3950                         current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, (Location) $1);
3951                         top_current_block = current_block;
3952                 } else {
3953                         current_block = new Block (current_block, (Location) $1, Location.Null);
3954                 }
3955           } 
3956           opt_statement_list CLOSE_BRACE 
3957           { 
3958                 while (current_block.Implicit)
3959                         current_block = current_block.Parent;
3960                 $$ = current_block;
3961                 current_block.SetEndLocation ((Location) $4);
3962                 current_block = current_block.Parent;
3963                 if (current_block == null)
3964                         top_current_block = null;
3965           }
3966         ;
3967
3968 opt_statement_list
3969         : /* empty */
3970         | statement_list 
3971         ;
3972
3973 statement_list
3974         : statement
3975         | statement_list statement
3976         ;
3977
3978 statement
3979         : declaration_statement
3980           {
3981                 if ($1 != null && (Block) $1 != current_block){
3982                         current_block.AddStatement ((Statement) $1);
3983                         current_block = (Block) $1;
3984                 }
3985           }
3986         | valid_declaration_statement
3987           {
3988                 current_block.AddStatement ((Statement) $1);
3989           }
3990         | labeled_statement
3991         ;
3992
3993 valid_declaration_statement
3994         : block
3995         | empty_statement
3996         | expression_statement
3997         | selection_statement
3998         | iteration_statement
3999         | jump_statement                  
4000         | try_statement
4001         | checked_statement
4002         | unchecked_statement
4003         | lock_statement
4004         | using_statement
4005         | unsafe_statement
4006         | fixed_statement
4007         ;
4008
4009 embedded_statement
4010         : valid_declaration_statement
4011         | declaration_statement
4012           {
4013                   Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4014                   $$ = null;
4015           }
4016         | labeled_statement
4017           {
4018                   Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4019                   $$ = null;
4020           }
4021         ;
4022
4023 empty_statement
4024         : SEMICOLON
4025           {
4026                   $$ = EmptyStatement.Value;
4027           }
4028         ;
4029
4030 labeled_statement
4031         : IDENTIFIER COLON 
4032           {
4033                 LocatedToken lt = (LocatedToken) $1;
4034                 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
4035
4036                 if (current_block.AddLabel (lt.Value, labeled, lt.Location))
4037                         current_block.AddStatement (labeled);
4038           }
4039           statement
4040         ;
4041
4042 declaration_statement
4043         : local_variable_declaration SEMICOLON
4044           {
4045                 if ($1 != null){
4046                         DictionaryEntry de = (DictionaryEntry) $1;
4047                         Expression e = (Expression) de.Key;
4048
4049                         $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
4050                 }
4051           }
4052
4053         | local_constant_declaration SEMICOLON
4054           {
4055                 if ($1 != null){
4056                         DictionaryEntry de = (DictionaryEntry) $1;
4057
4058                         $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
4059                 }
4060           }
4061         ;
4062
4063 /* 
4064  * The following is from Rhys' grammar:
4065  * > Types in local variable declarations must be recognized as 
4066  * > expressions to prevent reduce/reduce errors in the grammar.
4067  * > The expressions are converted into types during semantic analysis.
4068  */
4069 local_variable_type
4070         : primary_expression opt_rank_specifier_or_nullable
4071           { 
4072                 // FIXME: Do something smart here regarding the composition of the type.
4073
4074                 // Ok, the above "primary_expression" is there to get rid of
4075                 // both reduce/reduce and shift/reduces in the grammar, it should
4076                 // really just be "type_name".  If you use type_name, a reduce/reduce
4077                 // creeps up.  If you use namespace_or_type_name (which is all we need
4078                 // really) two shift/reduces appear.
4079                 // 
4080
4081                 // So the super-trick is that primary_expression
4082                 // can only be either a SimpleName or a MemberAccess. 
4083                 // The MemberAccess case arises when you have a fully qualified type-name like :
4084                 // Foo.Bar.Blah i;
4085                 // SimpleName is when you have
4086                 // Blah i;
4087                   
4088                 Expression expr = (Expression) $1;  
4089                 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType || expr is QualifiedAliasMember)) {
4090                         Error_ExpectingTypeName (expr);
4091                         $$ = null;
4092                 } else {
4093                         //
4094                         // So we extract the string corresponding to the SimpleName
4095                         // or MemberAccess
4096                         // 
4097
4098                         if ((string) $2 == "")
4099                                 $$ = $1;
4100                         else
4101                                 $$ = new ComposedCast ((Expression) $1, (string) $2);
4102                 }
4103           }
4104         | builtin_types opt_rank_specifier_or_nullable
4105           {
4106                 if ((string) $2 == "")
4107                         $$ = $1;
4108                 else
4109                         $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
4110           }
4111         ;
4112
4113 local_variable_pointer_type
4114         : primary_expression STAR
4115           {
4116                 Expression expr = (Expression) $1;  
4117
4118                 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType || expr is QualifiedAliasMember)) {
4119                         Error_ExpectingTypeName (expr);
4120
4121                         $$ = null;
4122                 } else 
4123                         $$ = new ComposedCast ((Expression) $1, "*");
4124           }
4125         | builtin_types STAR
4126           {
4127                 $$ = new ComposedCast ((Expression) $1, "*");
4128           }
4129         | VOID STAR
4130           {
4131                 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
4132           }
4133         | local_variable_pointer_type STAR
4134           {
4135                 $$ = new ComposedCast ((Expression) $1, "*");
4136           }
4137         ;
4138
4139 local_variable_declaration
4140         : local_variable_type variable_declarators
4141           {
4142                 if ($1 != null)
4143                         $$ = new DictionaryEntry ($1, $2);
4144                 else
4145                         $$ = null;
4146           }
4147         | local_variable_pointer_type opt_rank_specifier_or_nullable variable_declarators
4148           {
4149                 if ($1 != null){
4150                         Expression t;
4151
4152                         if ((string) $2 == "")
4153                                 t = (Expression) $1;
4154                         else
4155                                 t = new ComposedCast ((Expression) $1, (string) $2);
4156                         $$ = new DictionaryEntry (t, $3);
4157                 } else 
4158                         $$ = null;
4159           }
4160         ;
4161
4162 local_constant_declaration
4163         : CONST local_variable_type constant_declarators
4164           {
4165                 if ($2 != null)
4166                         $$ = new DictionaryEntry ($2, $3);
4167                 else
4168                         $$ = null;
4169           }
4170         ;
4171
4172 expression_statement
4173         : statement_expression SEMICOLON
4174           {
4175                 $$ = $1;
4176           }
4177         ;
4178
4179         //
4180         // We have to do the wrapping here and not in the case above,
4181         // because statement_expression is used for example in for_statement
4182         //
4183 statement_expression
4184         : invocation_expression         { $$ = new StatementExpression ((ExpressionStatement) $1); }
4185         | object_creation_expression    { $$ = new StatementExpression ((ExpressionStatement) $1); }
4186         | assignment_expression         { $$ = new StatementExpression ((ExpressionStatement) $1); }
4187         | post_increment_expression     { $$ = new StatementExpression ((ExpressionStatement) $1); }
4188         | post_decrement_expression     { $$ = new StatementExpression ((ExpressionStatement) $1); }
4189         | pre_increment_expression      { $$ = new StatementExpression ((ExpressionStatement) $1); }
4190         | pre_decrement_expression      { $$ = new StatementExpression ((ExpressionStatement) $1); }
4191         | error {
4192                 Report.Error (1002, GetLocation ($1), "Expecting `;'");
4193                 $$ = null;
4194           }
4195         ;
4196
4197 object_creation_expression
4198         : object_or_delegate_creation_expression
4199           { note ("complain if this is a delegate maybe?"); } 
4200         ;
4201
4202 selection_statement
4203         : if_statement
4204         | switch_statement
4205         ; 
4206
4207 if_statement
4208         : IF OPEN_PARENS boolean_expression CLOSE_PARENS 
4209           embedded_statement
4210           { 
4211                 Location l = (Location) $1;
4212
4213                 $$ = new If ((Expression) $3, (Statement) $5, l);
4214
4215                 if (RootContext.WarningLevel >= 3){
4216                         // FIXME: location for warning should be loc property of $5.
4217                         if ($5 == EmptyStatement.Value)
4218                                 Report.Warning (642, l, "Possible mistaken empty statement");
4219                 }
4220
4221           }
4222         | IF OPEN_PARENS boolean_expression CLOSE_PARENS
4223           embedded_statement ELSE embedded_statement
4224           {
4225                 Location l = (Location) $1;
4226
4227                 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
4228
4229                 if (RootContext.WarningLevel >= 3){
4230                         // FIXME: location for warning should be loc property of $5 and $7.
4231                         if ($5 == EmptyStatement.Value)
4232                                 Report.Warning (642, l, "Possible mistaken empty statement");
4233                         if ($7 == EmptyStatement.Value)
4234                                 Report.Warning (642, l, "Possible mistaken empty statement");
4235                 }
4236           }
4237         ;
4238
4239 switch_statement
4240         : SWITCH OPEN_PARENS
4241           { 
4242                 switch_stack.Push (current_block);
4243           }
4244           expression CLOSE_PARENS 
4245           switch_block
4246           {
4247                 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
4248                 current_block = (Block) switch_stack.Pop ();
4249           }
4250         ;
4251
4252 switch_block
4253         : OPEN_BRACE
4254           opt_switch_sections
4255           CLOSE_BRACE
4256           {
4257                 $$ = $2;
4258           }
4259         ;
4260
4261 opt_switch_sections
4262         : /* empty */           
4263           {
4264                 Report.Error (1522, lexer.Location, "Empty switch block"); 
4265           }
4266         | switch_sections
4267         ;
4268
4269 switch_sections
4270         : switch_section 
4271           {
4272                 ArrayList sections = new ArrayList (4);
4273
4274                 sections.Add ($1);
4275                 $$ = sections;
4276           }
4277         | switch_sections switch_section
4278           {
4279                 ArrayList sections = (ArrayList) $1;
4280
4281                 sections.Add ($2);
4282                 $$ = sections;
4283           }
4284         ;
4285
4286 switch_section
4287         : switch_labels
4288           {
4289                 current_block = current_block.CreateSwitchBlock (lexer.Location);
4290           }
4291           statement_list 
4292           {
4293                 Block topmost = current_block;
4294
4295                 while (topmost.Implicit)
4296                         topmost = topmost.Parent;
4297                 $$ = new SwitchSection ((ArrayList) $1, topmost);
4298           }
4299         ;
4300
4301 switch_labels
4302         : switch_label 
4303           {
4304                 ArrayList labels = new ArrayList (4);
4305
4306                 labels.Add ($1);
4307                 $$ = labels;
4308           }
4309         | switch_labels switch_label 
4310           {
4311                 ArrayList labels = (ArrayList) ($1);
4312                 labels.Add ($2);
4313
4314                 $$ = labels;
4315           }
4316         ;
4317
4318 switch_label
4319         : CASE constant_expression COLON        { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
4320         | DEFAULT COLON                         { $$ = new SwitchLabel (null, (Location) $2); }
4321         | error {
4322                 Report.Error (
4323                         1523, GetLocation ($1), 
4324                         "The keyword case or default must precede code in switch block");
4325           }
4326         ;
4327
4328 iteration_statement
4329         : while_statement
4330         | do_statement
4331         | for_statement
4332         | foreach_statement
4333         ;
4334
4335 while_statement
4336         : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
4337           {
4338                 Location l = (Location) $1;
4339                 $$ = new While ((Expression) $3, (Statement) $5, l);
4340           }
4341         ;
4342
4343 do_statement
4344         : DO embedded_statement 
4345           WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
4346           {
4347                 Location l = (Location) $1;
4348
4349                 $$ = new Do ((Statement) $2, (Expression) $5, l);
4350           }
4351         ;
4352
4353 for_statement
4354         : FOR OPEN_PARENS 
4355           opt_for_initializer SEMICOLON
4356           {
4357                 Block assign_block = new Block (current_block);
4358                 current_block = assign_block;
4359
4360                 if ($3 is DictionaryEntry){
4361                         DictionaryEntry de = (DictionaryEntry) $3;
4362                         
4363                         Expression type = (Expression) de.Key;
4364                         ArrayList var_declarators = (ArrayList) de.Value;
4365
4366                         foreach (VariableDeclaration decl in var_declarators){
4367
4368                                 LocalInfo vi;
4369
4370                                 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4371                                 if (vi == null)
4372                                         continue;
4373
4374                                 Location l = lexer.Location;
4375                                 Expression expr;
4376                                 if (decl.expression_or_array_initializer is Expression){
4377                                         expr = (Expression) decl.expression_or_array_initializer;
4378                                 } else if (decl.expression_or_array_initializer == null) {
4379                                         expr = null;
4380                                 } else {
4381                                         ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4382                                         expr = new ArrayCreation (type, "", init, decl.Location);
4383                                 }
4384                                         
4385                                 LocalVariableReference var;
4386                                 var = new LocalVariableReference (assign_block, decl.identifier, l);
4387
4388                                 if (expr != null) {
4389                                         Assign a = new Assign (var, expr, decl.Location);
4390                                         
4391                                         assign_block.AddStatement (new StatementExpression (a));
4392                                 }
4393                         }
4394                         
4395                         // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4396                         // This can be referred to as $5 below.
4397                         $$ = null;
4398                 } else {
4399                         $$ = $3;
4400                 }
4401           } 
4402           opt_for_condition SEMICOLON
4403           opt_for_iterator CLOSE_PARENS 
4404           embedded_statement
4405           {
4406                 Location l = (Location) $1;
4407
4408                 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4409
4410                 current_block.AddStatement (f);
4411                 while (current_block.Implicit)
4412                         current_block = current_block.Parent;
4413                 $$ = current_block;
4414                 current_block = current_block.Parent;
4415           }
4416         ;
4417
4418 opt_for_initializer
4419         : /* empty */           { $$ = EmptyStatement.Value; }
4420         | for_initializer       
4421         ;
4422
4423 for_initializer
4424         : local_variable_declaration
4425         | statement_expression_list
4426         ;
4427
4428 opt_for_condition
4429         : /* empty */           { $$ = null; }
4430         | boolean_expression
4431         ;
4432
4433 opt_for_iterator
4434         : /* empty */           { $$ = EmptyStatement.Value; }
4435         | for_iterator
4436         ;
4437
4438 for_iterator
4439         : statement_expression_list
4440         ;
4441
4442 statement_expression_list
4443         : statement_expression  
4444           {
4445                 // CHANGE: was `null'
4446                 Block b = new Block (current_block, Block.Flags.Implicit);   
4447
4448                 b.AddStatement ((Statement) $1);
4449                 $$ = b;
4450           }
4451         | statement_expression_list COMMA statement_expression
4452           {
4453                 Block b = (Block) $1;
4454
4455                 b.AddStatement ((Statement) $3);
4456                 $$ = $1;
4457           }
4458         ;
4459
4460 foreach_statement
4461         : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4462           {
4463                 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4464                 $$ = null;
4465           }
4466         | FOREACH OPEN_PARENS type IDENTIFIER IN
4467           expression CLOSE_PARENS 
4468           {
4469                 Block foreach_block = new Block (current_block);
4470                 current_block = foreach_block;
4471
4472                 LocatedToken lt = (LocatedToken) $4;
4473                 Location l = lt.Location;
4474                 LocalInfo vi;
4475
4476                 vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4477                 if (vi != null) {
4478                         vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4479
4480                         // Get a writable reference to this read-only variable.
4481                         //
4482                         // Note that the $$ here refers to the value of _this_ code block,
4483                         // not the value of the LHS non-terminal.  This can be referred to as $8 below.
4484                         $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
4485                 } else {
4486                         $$ = null;
4487                 }
4488           } 
4489           embedded_statement 
4490           {
4491                 LocalVariableReference v = (LocalVariableReference) $8;
4492                 Location l = (Location) $1;
4493
4494                 if (v != null) {
4495                         Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
4496                         current_block.AddStatement (f);
4497                 }
4498
4499                 while (current_block.Implicit)
4500                           current_block = current_block.Parent;
4501                 $$ = current_block;
4502                 current_block = current_block.Parent;
4503           }
4504         ;
4505
4506 jump_statement
4507         : break_statement
4508         | continue_statement
4509         | goto_statement
4510         | return_statement
4511         | throw_statement
4512         | yield_statement
4513         ;
4514
4515 break_statement
4516         : BREAK SEMICOLON
4517           {
4518                 $$ = new Break ((Location) $1);
4519           }
4520         ;
4521
4522 continue_statement
4523         : CONTINUE SEMICOLON
4524           {
4525                 $$ = new Continue ((Location) $1);
4526           }
4527         ;
4528
4529 goto_statement
4530         : GOTO IDENTIFIER SEMICOLON 
4531           {
4532                 LocatedToken lt = (LocatedToken) $2;
4533                 $$ = new Goto (lt.Value, lt.Location);
4534           }
4535         | GOTO CASE constant_expression SEMICOLON
4536           {
4537                 $$ = new GotoCase ((Expression) $3, (Location) $1);
4538           }
4539         | GOTO DEFAULT SEMICOLON 
4540           {
4541                 $$ = new GotoDefault ((Location) $1);
4542           }
4543         ; 
4544
4545 return_statement
4546         : RETURN opt_expression SEMICOLON
4547           {
4548                 $$ = new Return ((Expression) $2, (Location) $1);
4549           }
4550         ;
4551
4552 throw_statement
4553         : THROW opt_expression SEMICOLON
4554           {
4555                 $$ = new Throw ((Expression) $2, (Location) $1);
4556           }
4557         ;
4558
4559 yield_statement 
4560         : IDENTIFIER RETURN expression SEMICOLON
4561           {
4562                 LocatedToken lt = (LocatedToken) $1;
4563                 string s = lt.Value;
4564                 if (s != "yield"){
4565                         Report.Error (1003, lt.Location, "; expected");
4566                         $$ = null;
4567                 }
4568                 if (RootContext.Version == LanguageVersion.ISO_1){
4569                         Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4570                         $$ = null;
4571                 }
4572                 if (iterator_container == null){
4573                         Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4574                         $$ = null;
4575                 } else {
4576                         iterator_container.SetYields ();
4577                         $$ = new Yield ((Expression) $3, lt.Location); 
4578                 }
4579           }
4580         | IDENTIFIER RETURN SEMICOLON
4581           {
4582                 Report.Error (1627, (Location) $2, "Expression expected after yield return");
4583                 $$ = null;
4584           }
4585         | IDENTIFIER BREAK SEMICOLON
4586           {
4587                 LocatedToken lt = (LocatedToken) $1;
4588                 string s = lt.Value;
4589                 if (s != "yield"){
4590                         Report.Error (1003, lt.Location, "; expected");
4591                         $$ = null;
4592                 }
4593                 if (RootContext.Version == LanguageVersion.ISO_1){
4594                         Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4595                         $$ = null;
4596                 }
4597                 if (iterator_container == null){
4598                         Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4599                         $$ = null;
4600                 } else {
4601                         iterator_container.SetYields ();
4602                         $$ = new YieldBreak (lt.Location);
4603                 }
4604           }
4605         ;
4606
4607 opt_expression
4608         : /* empty */
4609         | expression
4610         ;
4611
4612 try_statement
4613         : TRY block catch_clauses 
4614           {
4615                 Catch g = null;
4616                 
4617                 ArrayList c = (ArrayList)$3;
4618                 for (int i = 0; i < c.Count; ++i) {
4619                         Catch cc = (Catch) c [i];
4620                         if (cc.IsGeneral) {
4621                                 if (i != c.Count - 1)
4622                                         Report.Error (1017, cc.loc, "Try statement already has an empty catch block");
4623                                 g = cc;
4624                                 c.RemoveAt (i);
4625                                 i--;
4626                         }
4627                 }
4628
4629                 // Now s contains the list of specific catch clauses
4630                 // and g contains the general one.
4631                 
4632                 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4633           }
4634         | TRY block opt_catch_clauses FINALLY block
4635           {
4636                 Catch g = null;
4637                 ArrayList s = new ArrayList (4);
4638                 ArrayList catch_list = (ArrayList) $3;
4639
4640                 if (catch_list != null){
4641                         foreach (Catch cc in catch_list) {
4642                                 if (cc.IsGeneral)
4643                                         g = cc;
4644                                 else
4645                                         s.Add (cc);
4646                         }
4647                 }
4648
4649                 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4650           }
4651         | TRY block error 
4652           {
4653                 Report.Error (1524, (Location) $1, "Expected catch or finally");
4654                 $$ = null;
4655           }
4656         ;
4657
4658 opt_catch_clauses
4659         : /* empty */  { $$ = null; }
4660         | catch_clauses
4661         ;
4662
4663 catch_clauses
4664         : catch_clause 
4665           {
4666                 ArrayList l = new ArrayList (4);
4667
4668                 l.Add ($1);
4669                 $$ = l;
4670           }
4671         | catch_clauses catch_clause
4672           {
4673                 ArrayList l = (ArrayList) $1;
4674
4675                 l.Add ($2);
4676                 $$ = l;
4677           }
4678         ;
4679
4680 opt_identifier
4681         : /* empty */   { $$ = null; }
4682         | IDENTIFIER
4683         ;
4684
4685 catch_clause 
4686         : CATCH opt_catch_args 
4687           {
4688                 Expression type = null;
4689                 
4690                 if ($2 != null) {
4691                         DictionaryEntry cc = (DictionaryEntry) $2;
4692                         type = (Expression) cc.Key;
4693                         LocatedToken lt = (LocatedToken) cc.Value;
4694
4695                         if (lt != null){
4696                                 ArrayList one = new ArrayList (4);
4697
4698                                 one.Add (new VariableDeclaration (lt, null));
4699
4700                                 current_block = new Block (current_block);
4701                                 Block b = declare_local_variables (type, one, lt.Location);
4702                                 current_block = b;
4703                         }
4704                 }
4705           } block {
4706                 Expression type = null;
4707                 string id = null;
4708
4709                 if ($2 != null){
4710                         DictionaryEntry cc = (DictionaryEntry) $2;
4711                         type = (Expression) cc.Key;
4712                         LocatedToken lt = (LocatedToken) cc.Value;
4713
4714                         if (lt != null){
4715                                 id = lt.Value;
4716                                 while (current_block.Implicit)
4717                                         current_block = current_block.Parent;
4718                                 current_block = current_block.Parent;
4719                         }
4720                 }
4721
4722                 $$ = new Catch (type, id, (Block) $4, ((Block) $4).loc);
4723           }
4724         ;
4725
4726 opt_catch_args
4727         : /* empty */ { $$ = null; }
4728         | catch_args
4729         ;         
4730
4731 catch_args 
4732         : OPEN_PARENS type opt_identifier CLOSE_PARENS 
4733           {
4734                 $$ = new DictionaryEntry ($2, $3);
4735           }
4736         ;
4737
4738
4739 checked_statement
4740         : CHECKED block
4741           {
4742                 $$ = new Checked ((Block) $2);
4743           }
4744         ;
4745
4746 unchecked_statement
4747         : UNCHECKED block
4748           {
4749                 $$ = new Unchecked ((Block) $2);
4750           }
4751         ;
4752
4753 unsafe_statement
4754         : UNSAFE 
4755           {
4756                 RootContext.CheckUnsafeOption ((Location) $1);
4757           } block {
4758                 $$ = new Unsafe ((Block) $3);
4759           }
4760         ;
4761
4762 fixed_statement
4763         : FIXED OPEN_PARENS 
4764           type fixed_pointer_declarators 
4765           CLOSE_PARENS
4766           {
4767                 ArrayList list = (ArrayList) $4;
4768                 Expression type = (Expression) $3;
4769                 Location l = (Location) $1;
4770                 int top = list.Count;
4771
4772                 Block assign_block = new Block (current_block);
4773                 current_block = assign_block;
4774
4775                 for (int i = 0; i < top; i++){
4776                         Pair p = (Pair) list [i];
4777                         LocalInfo v;
4778
4779                         v = current_block.AddVariable (type, (string) p.First, l);
4780                         if (v == null)
4781                                 continue;
4782
4783                         v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4784                         v.Pinned = true;
4785                         p.First = v;
4786                         list [i] = p;
4787                 }
4788           }
4789           embedded_statement 
4790           {
4791                 Location l = (Location) $1;
4792
4793                 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4794
4795                 current_block.AddStatement (f);
4796                 while (current_block.Implicit)
4797                         current_block = current_block.Parent;
4798                 $$ = current_block;
4799                 current_block = current_block.Parent;
4800           }
4801         ;
4802
4803 fixed_pointer_declarators
4804         : fixed_pointer_declarator      { 
4805                 ArrayList declarators = new ArrayList (4);
4806                 if ($1 != null)
4807                         declarators.Add ($1);
4808                 $$ = declarators;
4809           }
4810         | fixed_pointer_declarators COMMA fixed_pointer_declarator
4811           {
4812                 ArrayList declarators = (ArrayList) $1;
4813                 if ($3 != null)
4814                         declarators.Add ($3);
4815                 $$ = declarators;
4816           }
4817         ;
4818
4819 fixed_pointer_declarator
4820         : IDENTIFIER ASSIGN expression
4821           {
4822                 LocatedToken lt = (LocatedToken) $1;
4823                 // FIXME: keep location
4824                 $$ = new Pair (lt.Value, $3);
4825           }
4826         | IDENTIFIER
4827           {
4828                 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
4829                 $$ = null;
4830           }
4831         ;
4832
4833 lock_statement
4834         : LOCK OPEN_PARENS expression CLOSE_PARENS 
4835           {
4836                 //
4837           } 
4838           embedded_statement
4839           {
4840                 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
4841           }
4842         ;
4843
4844 using_statement
4845         : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4846           {
4847                 Block assign_block = new Block (current_block);
4848                 current_block = assign_block;
4849
4850                 if ($3 is DictionaryEntry){
4851                         DictionaryEntry de = (DictionaryEntry) $3;
4852                         Location l = (Location) $1;
4853
4854                         Expression type = (Expression) de.Key;
4855                         ArrayList var_declarators = (ArrayList) de.Value;
4856
4857                         ArrayList vars = new ArrayList (4);
4858
4859                         foreach (VariableDeclaration decl in var_declarators){
4860
4861                                 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4862                                 if (vi == null)
4863                                         continue;
4864                                 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
4865
4866                                 Expression expr;
4867                                 if (decl.expression_or_array_initializer is Expression){
4868                                         expr = (Expression) decl.expression_or_array_initializer;
4869                                 } else {
4870                                         ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4871                                         if (init == null) {
4872                                                 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4873                                         }
4874                                         
4875                                         expr = new ArrayCreation (type, "", init, decl.Location);
4876                                 }
4877
4878                                 LocalVariableReference var;
4879
4880                                 // Get a writable reference to this read-only variable.
4881                                 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4882
4883                                 // This is so that it is not a warning on using variables
4884                                 vi.Used = true;
4885
4886                                 vars.Add (new DictionaryEntry (var, expr));                             
4887
4888                                 // Assign a = new Assign (var, expr, decl.Location);
4889                                 // assign_block.AddStatement (new StatementExpression (a));
4890                         }
4891
4892                         // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4893                         // It can be referred to as $5 below.
4894                         $$ = new DictionaryEntry (type, vars);
4895                  } else {
4896                         $$ = $3;
4897                  }
4898           } 
4899           embedded_statement
4900           {
4901                 Using u = new Using ($5, (Statement) $6, (Location) $1);
4902                 current_block.AddStatement (u);
4903                 while (current_block.Implicit)
4904                         current_block = current_block.Parent;
4905                 $$ = current_block;
4906                 current_block = current_block.Parent;
4907           }
4908         ; 
4909
4910 resource_acquisition
4911         : local_variable_declaration
4912         | expression
4913         ;
4914
4915 %%
4916
4917 // <summary>
4918 //   A class used to pass around variable declarations and constants
4919 // </summary>
4920 public class VariableDeclaration {
4921         public string identifier;
4922         public object expression_or_array_initializer;
4923         public Location Location;
4924         public Attributes OptAttributes;
4925         public string DocComment;
4926
4927         public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
4928         {
4929                 this.identifier = lt.Value;
4930                 this.expression_or_array_initializer = eoai;
4931                 this.Location = lt.Location;
4932                 this.OptAttributes = opt_attrs;
4933         }
4934
4935         public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
4936         {
4937         }
4938 }
4939
4940 // <summary>
4941 //   A class used to hold info about an indexer declarator
4942 // </summary>
4943 public class IndexerDeclaration {
4944         public Expression type;
4945         public MemberName interface_type;
4946         public Parameters param_list;
4947         public Location location;
4948
4949         public IndexerDeclaration (Expression type, MemberName interface_type,
4950                                    Parameters param_list, Location loc)
4951         {
4952                 this.type = type;
4953                 this.interface_type = interface_type;
4954                 this.param_list = param_list;
4955                 this.location = loc;
4956         }
4957 }
4958
4959 //
4960 // We use this when we do not have an object in advance that is an IIteratorContainer
4961 //
4962 public class SimpleIteratorContainer : IIteratorContainer {
4963         public bool Yields;
4964
4965         public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4966
4967         //
4968         // Reset and return
4969         //
4970         public static SimpleIteratorContainer GetSimple () { 
4971                 Simple.Yields = false;
4972                 return Simple;
4973         }
4974
4975         public void SetYields () { Yields = true; } 
4976 }
4977
4978 // <summary>
4979 //  A class used to hold info about an operator declarator
4980 // </summary>
4981 public class OperatorDeclaration {
4982         public Operator.OpType optype;
4983         public Expression ret_type, arg1type, arg2type;
4984         public string arg1name, arg2name;
4985         public Location location;
4986
4987         public OperatorDeclaration (Operator.OpType op, Expression ret_type, 
4988                                     Expression arg1type, string arg1name,
4989                                     Expression arg2type, string arg2name, Location location)
4990         {
4991                 optype = op;
4992                 this.ret_type = ret_type;
4993                 this.arg1type = arg1type;
4994                 this.arg1name = arg1name;
4995                 this.arg2type = arg2type;
4996                 this.arg2name = arg2name;
4997                 this.location = location;
4998         }
4999
5000 }
5001
5002 void Error_ExpectingTypeName (Expression expr)
5003 {
5004         if (expr is Invocation){
5005                 Report.Error (1002, expr.Location, "Expecting `;'");
5006         } else {
5007                 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
5008         }
5009 }
5010
5011 TypeContainer pop_current_class ()
5012 {
5013         TypeContainer retval = current_class;
5014
5015         current_class = current_class.Parent;
5016         current_container = current_container.Parent;
5017         
5018         if (current_class != current_container) {
5019                 if (!(current_class is ClassPart) ||
5020                     ((ClassPart) current_class).PartialContainer != current_container)
5021                         throw new InternalErrorException ();
5022         } else if (current_container is ClassPart)
5023                 current_container = ((ClassPart) current_class).PartialContainer;
5024
5025         return retval;
5026 }
5027
5028 // <summary>
5029 //   Given the @class_name name, it creates a fully qualified name
5030 //   based on the containing declaration space
5031 // </summary>
5032 MemberName
5033 MakeName (MemberName class_name)
5034 {
5035         Namespace ns = current_namespace.NS;
5036
5037         if (current_container.Name == ""){
5038                 if (ns.Name != "")
5039                         return new MemberName (ns.MemberName, class_name);
5040                 else
5041                         return class_name;
5042         } else {
5043                 return new MemberName (current_container.MemberName, class_name);
5044         }
5045 }
5046
5047 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
5048 {
5049         Block implicit_block;
5050         ArrayList inits = null;
5051
5052         //
5053         // We use the `Used' property to check whether statements
5054         // have been added to the current block.  If so, we need
5055         // to create another block to contain the new declaration
5056         // otherwise, as an optimization, we use the same block to
5057         // add the declaration.
5058         //
5059         // FIXME: A further optimization is to check if the statements
5060         // that were added were added as part of the initialization
5061         // below.  In which case, no other statements have been executed
5062         // and we might be able to reduce the number of blocks for
5063         // situations like this:
5064         //
5065         // int j = 1;  int k = j + 1;
5066         //
5067         if (current_block.Used)
5068                 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
5069         else
5070                 implicit_block = current_block;
5071
5072         foreach (VariableDeclaration decl in variable_declarators){
5073
5074                 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
5075                         if (decl.expression_or_array_initializer != null){
5076                                 if (inits == null)
5077                                         inits = new ArrayList (4);
5078                                 inits.Add (decl);
5079                         }
5080                 }
5081         }
5082
5083         if (inits == null)
5084                 return implicit_block;
5085
5086         foreach (VariableDeclaration decl in inits){
5087                 Assign assign;
5088                 Expression expr;
5089                 
5090                 if (decl.expression_or_array_initializer is Expression){
5091                         expr = (Expression) decl.expression_or_array_initializer;
5092
5093                 } else {
5094                         ArrayList init = (ArrayList) decl.expression_or_array_initializer;
5095                         
5096                         expr = new ArrayCreation (type, "", init, decl.Location);
5097                 }
5098
5099                 LocalVariableReference var;
5100                 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
5101
5102                 assign = new Assign (var, expr, decl.Location);
5103
5104                 implicit_block.AddStatement (new StatementExpression (assign));
5105         }
5106         
5107         return implicit_block;
5108 }
5109
5110 Block declare_local_constants (Expression type, ArrayList declarators)
5111 {
5112         Block implicit_block;
5113
5114         if (current_block.Used)
5115                 implicit_block = new Block (current_block, Block.Flags.Implicit);
5116         else
5117                 implicit_block = current_block;
5118
5119         foreach (VariableDeclaration decl in declarators){
5120                 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
5121         }
5122         
5123         return implicit_block;
5124 }
5125
5126 void CheckAttributeTarget (string a, Location l)
5127 {
5128         switch (a) {
5129
5130         case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
5131                 return;
5132                 
5133         default :
5134                 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
5135                 break;
5136         }
5137
5138 }
5139
5140 void CheckUnaryOperator (Operator.OpType op, Location l)
5141 {
5142         switch (op) {
5143                 
5144         case Operator.OpType.LogicalNot: 
5145         case Operator.OpType.OnesComplement: 
5146         case Operator.OpType.Increment:
5147         case Operator.OpType.Decrement:
5148         case Operator.OpType.True: 
5149         case Operator.OpType.False: 
5150         case Operator.OpType.Addition: 
5151         case Operator.OpType.Subtraction:
5152                 
5153                 break;
5154                 
5155         default :
5156                 Report.Error (1019, l, "Overloadable unary operator expected"); 
5157                 break;
5158                 
5159         }
5160 }
5161
5162 void CheckBinaryOperator (Operator.OpType op, Location l)
5163 {
5164         switch (op) {
5165                 
5166         case Operator.OpType.Addition: 
5167         case Operator.OpType.Subtraction: 
5168         case Operator.OpType.Multiply:
5169         case Operator.OpType.Division:
5170         case Operator.OpType.Modulus: 
5171         case Operator.OpType.BitwiseAnd: 
5172         case Operator.OpType.BitwiseOr:
5173         case Operator.OpType.ExclusiveOr: 
5174         case Operator.OpType.LeftShift: 
5175         case Operator.OpType.RightShift:
5176         case Operator.OpType.Equality: 
5177         case Operator.OpType.Inequality:
5178         case Operator.OpType.GreaterThan: 
5179         case Operator.OpType.LessThan: 
5180         case Operator.OpType.GreaterThanOrEqual:
5181         case Operator.OpType.LessThanOrEqual:
5182                 break;
5183                 
5184         default :
5185                 Report.Error (1020, l, "Overloadable binary operator expected");
5186                 break;
5187         }
5188         
5189 }
5190
5191 void syntax_error (Location l, string msg)
5192 {
5193         Report.Error (1003, l, "Syntax error, " + msg);
5194 }
5195
5196 void note (string s)
5197 {
5198         // Used to put annotations
5199 }
5200
5201 Tokenizer lexer;
5202
5203 public Tokenizer Lexer {
5204         get {
5205                 return lexer;
5206         }
5207 }                  
5208
5209 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
5210 {
5211         current_namespace = new NamespaceEntry (null, file, null, Location.Null);
5212         this.name = file.Name;
5213         this.file = file;
5214         current_container = RootContext.Tree.Types;
5215         // TODO: Make RootContext.Tree.Types a PartialContainer.
5216         current_class = current_container;
5217         current_container.NamespaceEntry = current_namespace;
5218         oob_stack = new Stack ();
5219         switch_stack = new Stack ();
5220
5221         lexer = new Tokenizer (reader, file, defines);
5222 }
5223
5224 public void parse ()
5225 {
5226         try {
5227                 if (yacc_verbose_flag > 1)
5228                         yyparse (lexer, new yydebug.yyDebugSimple ());
5229                 else
5230                         yyparse (lexer);
5231                 Tokenizer tokenizer = lexer as Tokenizer;
5232                 tokenizer.cleanup ();
5233         } catch (Exception e){
5234                 //
5235                 // Removed for production use, use parser verbose to get the output.
5236                 //
5237                 // Console.WriteLine (e);
5238                 Report.Error (-25, lexer.Location, "Parsing error");
5239                 if (yacc_verbose_flag > 0)
5240                         Console.WriteLine (e);
5241         }
5242
5243         RootContext.Tree.Types.NamespaceEntry = null;
5244 }
5245
5246 void CheckToken (int error, int yyToken, string msg, Location loc)
5247 {
5248         if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
5249                 Report.Error (error, loc, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
5250                 return;
5251         }               
5252         Report.Error (error, loc, msg);
5253 }
5254
5255 void CheckIdentifierToken (int yyToken, Location loc)
5256 {
5257         CheckToken (1041, yyToken, "Identifier expected", loc);
5258 }
5259
5260 string ConsumeStoredComment ()
5261 {
5262         string s = tmpComment;
5263         tmpComment = null;
5264         Lexer.doc_state = XmlCommentState.Allowed;
5265         return s;
5266 }
5267
5268 Location GetLocation (object obj)
5269 {
5270         if (obj is MemberCore)
5271                 return ((MemberCore) obj).Location;
5272         if (obj is MemberName)
5273                 return ((MemberName) obj).Location;
5274         if (obj is LocatedToken)
5275                 return ((LocatedToken) obj).Location;
5276         if (obj is Location)
5277                 return (Location) obj;
5278         return lexer.Location;
5279 }
5280
5281 /* end end end */
5282 }