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