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