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