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