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