2007-10-17 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / gmcs / cs-parser.jay
1 %{
2 //
3 // cs-parser.jay: The Parser for the C# compiler
4 //
5 // Authors: Miguel de Icaza (miguel@gnu.org)
6 //          Ravi Pratap     (ravi@ximian.com)
7 //          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
1453           {
1454                 if (RootContext.Version <= LanguageVersion.ISO_2)
1455                         Report.FeatureIsNotISO1 (GetLocation ($1), "extension methods");
1456                 $$ = Parameter.Modifier.This;
1457           }
1458         ;
1459
1460 parameter_array
1461         : opt_attributes PARAMS type IDENTIFIER
1462           { 
1463                 LocatedToken lt = (LocatedToken) $4;
1464                 $$ = new ParamsParameter ((Expression) $3, lt.Value, (Attributes) $1, lt.Location);
1465           }
1466         | opt_attributes PARAMS PARAMS type IDENTIFIER 
1467           {
1468                 Error_DuplicateParameterModifier (lexer.Location, Parameter.Modifier.PARAMS);
1469                 $$ = null;
1470           }
1471         | opt_attributes PARAMS parameter_modifier type IDENTIFIER 
1472           {
1473                 Parameter.Modifier mod = (Parameter.Modifier)$3;
1474                 if ((mod & Parameter.Modifier.This) != 0) {
1475                         Report.Error (1104, lexer.Location, "The parameter modifiers `this' and `params' cannot be used altogether");
1476                 } else {
1477                         Report.Error (1611, (Location) $2, "The params parameter cannot be declared as ref or out");
1478                 }
1479                 $$ = null;
1480           }
1481         | opt_attributes PARAMS type error {
1482                 CheckIdentifierToken (yyToken, GetLocation ($4));
1483                 $$ = null;
1484           }
1485         ;
1486
1487 property_declaration
1488         : opt_attributes
1489           opt_modifiers
1490           type
1491           namespace_or_type_name
1492           {
1493                 if (RootContext.Documentation != null)
1494                         tmpComment = Lexer.consume_doc_comment ();
1495           }
1496           OPEN_BRACE 
1497           {
1498                 implicit_value_parameter_type = (Expression) $3;
1499
1500                 lexer.PropertyParsing = true;
1501           }
1502           accessor_declarations 
1503           {
1504                 lexer.PropertyParsing = false;
1505                 has_get = has_set = false;
1506           }
1507           CLOSE_BRACE
1508           { 
1509                 if ($8 == null)
1510                         break;
1511
1512                 Property prop;
1513                 Accessors accessors = (Accessors) $8;
1514                 Accessor get_block = accessors.get_or_add;
1515                 Accessor set_block = accessors.set_or_remove;
1516
1517                 MemberName name = (MemberName) $4;
1518
1519                 if (name.TypeArguments != null)
1520                         syntax_error (lexer.Location, "a property can't have type arguments");
1521
1522                 prop = new Property (current_class, (Expression) $3, (int) $2, false,
1523                                      name, (Attributes) $1, get_block, set_block, accessors.declared_in_reverse, current_block);
1524
1525                 current_container.AddProperty (prop);
1526                 implicit_value_parameter_type = null;
1527
1528                 if (RootContext.Documentation != null)
1529                         prop.DocComment = ConsumeStoredComment ();
1530
1531           }
1532         ;
1533
1534 accessor_declarations
1535         : get_accessor_declaration
1536          {
1537                 $$ = new Accessors ((Accessor) $1, null);
1538          }
1539         | get_accessor_declaration accessor_declarations
1540          { 
1541                 Accessors accessors = (Accessors) $2;
1542                 accessors.get_or_add = (Accessor) $1;
1543                 $$ = accessors;
1544          }
1545         | set_accessor_declaration
1546          {
1547                 $$ = new Accessors (null, (Accessor) $1);
1548          }
1549         | set_accessor_declaration accessor_declarations
1550          { 
1551                 Accessors accessors = (Accessors) $2;
1552                 accessors.set_or_remove = (Accessor) $1;
1553                 accessors.declared_in_reverse = true;
1554                 $$ = accessors;
1555          }
1556         | error
1557           {
1558                 Report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1559                 $$ = null;
1560           }
1561         ;
1562
1563 get_accessor_declaration
1564         : opt_attributes opt_modifiers GET
1565           {
1566                 // If this is not the case, then current_local_parameters has already
1567                 // been set in indexer_declaration
1568                 if (parsing_indexer == false)
1569                         current_local_parameters = null;
1570                 else 
1571                         current_local_parameters = indexer_parameters;
1572                 lexer.PropertyParsing = false;
1573
1574                 anonymous_host = SimpleAnonymousHost.GetSimple ();
1575           }
1576           accessor_body
1577           {
1578                 if (has_get) {
1579                         Report.Error (1007, (Location) $3, "Property accessor already defined");
1580                         break;
1581                 }
1582                 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1583                 has_get = true;
1584                 current_local_parameters = null;
1585                 lexer.PropertyParsing = true;
1586
1587                 SimpleAnonymousHost.Simple.Propagate (accessor);
1588                 anonymous_host = null;
1589
1590                 if (RootContext.Documentation != null)
1591                         if (Lexer.doc_state == XmlCommentState.Error)
1592                                 Lexer.doc_state = XmlCommentState.NotAllowed;
1593
1594                 $$ = accessor;
1595           }
1596         ;
1597
1598 set_accessor_declaration
1599         : opt_attributes opt_modifiers SET 
1600           {
1601                 Parameter [] args;
1602                 Parameter implicit_value_parameter = new Parameter (
1603                         implicit_value_parameter_type, "value", 
1604                         Parameter.Modifier.NONE, null, (Location) $3);
1605
1606                 if (parsing_indexer == false) {
1607                         args  = new Parameter [1];
1608                         args [0] = implicit_value_parameter;
1609                         current_local_parameters = new Parameters (args);
1610                 } else {
1611                         Parameter [] fpars = indexer_parameters.FixedParameters;
1612
1613                         if (fpars != null){
1614                                 int count = fpars.Length;
1615
1616                                 args = new Parameter [count + 1];
1617                                 fpars.CopyTo (args, 0);
1618                                 args [count] = implicit_value_parameter;
1619                         } else 
1620                                 args = null;
1621                         current_local_parameters = new Parameters (
1622                                 args);
1623                 }
1624                 
1625                 lexer.PropertyParsing = false;
1626
1627                 anonymous_host = SimpleAnonymousHost.GetSimple ();
1628           }
1629           accessor_body
1630           {
1631                 if (has_set) {
1632                         Report.Error (1007, ((LocatedToken) $3).Location, "Property accessor already defined");
1633                         break;
1634                 }
1635                 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1636                 has_set = true;
1637                 current_local_parameters = null;
1638                 lexer.PropertyParsing = true;
1639
1640                 SimpleAnonymousHost.Simple.Propagate (accessor);
1641                 anonymous_host = null;
1642
1643                 if (RootContext.Documentation != null
1644                         && Lexer.doc_state == XmlCommentState.Error)
1645                         Lexer.doc_state = XmlCommentState.NotAllowed;
1646
1647                 $$ = accessor;
1648           }
1649         ;
1650
1651 accessor_body
1652         : block 
1653         | SEMICOLON             { $$ = null; }
1654         ;
1655
1656 interface_declaration
1657         : opt_attributes
1658           opt_modifiers
1659           opt_partial
1660           INTERFACE
1661           {
1662                 lexer.ConstraintsParsing = true;
1663           }
1664           type_name
1665           {
1666                 MemberName name = MakeName ((MemberName) $6);
1667                 push_current_class (new Interface (current_namespace, current_class, name, (int) $2, (Attributes) $1), $3);
1668           }
1669           opt_class_base
1670           opt_type_parameter_constraints_clauses
1671           {
1672                 lexer.ConstraintsParsing = false;
1673
1674                 current_class.SetParameterInfo ((ArrayList) $9);
1675
1676                 if (RootContext.Documentation != null) {
1677                         current_container.DocComment = Lexer.consume_doc_comment ();
1678                         Lexer.doc_state = XmlCommentState.Allowed;
1679                 }
1680           }
1681           interface_body
1682           {
1683                 if (RootContext.Documentation != null)
1684                         Lexer.doc_state = XmlCommentState.Allowed;
1685           }
1686           opt_semicolon 
1687           {
1688                 $$ = pop_current_class ();
1689           }
1690         | opt_attributes opt_modifiers opt_partial INTERFACE error {
1691                 CheckIdentifierToken (yyToken, GetLocation ($5));
1692           }
1693         ;
1694
1695 interface_body
1696         : OPEN_BRACE
1697           opt_interface_member_declarations
1698           CLOSE_BRACE
1699         ;
1700
1701 opt_interface_member_declarations
1702         : /* empty */
1703         | interface_member_declarations
1704         ;
1705
1706 interface_member_declarations
1707         : interface_member_declaration
1708         | interface_member_declarations interface_member_declaration
1709         ;
1710
1711 interface_member_declaration
1712         : interface_method_declaration          
1713           { 
1714                 if ($1 == null)
1715                         break;
1716
1717                 Method m = (Method) $1;
1718
1719                 if (m.IsExplicitImpl)
1720                         Report.Error (541, m.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1721                                 m.GetSignatureForError ());
1722
1723                 current_container.AddMethod (m);
1724
1725                 if (RootContext.Documentation != null)
1726                         Lexer.doc_state = XmlCommentState.Allowed;
1727           }
1728         | interface_property_declaration        
1729           { 
1730                 if ($1 == null)
1731                         break;
1732
1733                 Property p = (Property) $1;
1734
1735                 if (p.IsExplicitImpl)
1736                         Report.Error (541, p.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1737                                 p.GetSignatureForError ());
1738
1739                 current_container.AddProperty (p);
1740
1741                 if (RootContext.Documentation != null)
1742                         Lexer.doc_state = XmlCommentState.Allowed;
1743           }
1744         | interface_event_declaration 
1745           { 
1746                 if ($1 != null){
1747                         Event e = (Event) $1;
1748
1749                         if (e.IsExplicitImpl)
1750                         Report.Error (541, e.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1751                                 e.GetSignatureForError ());
1752                         
1753                         current_container.AddEvent (e);
1754                 }
1755
1756                 if (RootContext.Documentation != null)
1757                         Lexer.doc_state = XmlCommentState.Allowed;
1758           }
1759         | interface_indexer_declaration
1760           { 
1761                 if ($1 == null)
1762                         break;
1763
1764                 Indexer i = (Indexer) $1;
1765
1766                 if (i.IsExplicitImpl)
1767                         Report.Error (541, i.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1768                                 i.GetSignatureForError ());
1769
1770                 current_container.AddIndexer (i);
1771
1772                 if (RootContext.Documentation != null)
1773                         Lexer.doc_state = XmlCommentState.Allowed;
1774           }
1775         | delegate_declaration
1776           {
1777                 if ($1 != null) {
1778                         Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1779                                 ((MemberCore)$1).GetSignatureForError ());
1780                 }
1781           }
1782         | class_declaration
1783           {
1784                 if ($1 != null) {
1785                         Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1786                                 ((MemberCore)$1).GetSignatureForError ());
1787                 }
1788           }
1789         | struct_declaration
1790           {
1791                 if ($1 != null) {
1792                         Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1793                                 ((MemberCore)$1).GetSignatureForError ());
1794                 }
1795           }
1796         | enum_declaration 
1797           {
1798                 if ($1 != null) {
1799                         Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1800                                 ((MemberCore)$1).GetSignatureForError ());
1801                 }
1802           }
1803         | interface_declaration 
1804           {
1805                 if ($1 != null) {
1806                         Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1807                                 ((MemberCore)$1).GetSignatureForError ());
1808                 }
1809           } 
1810         | constant_declaration
1811           {
1812                 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1813           }
1814         ;
1815
1816 opt_new
1817         : opt_modifiers 
1818           {
1819                 int val = (int) $1;
1820                 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, GetLocation ($1));
1821                 $$ = val;
1822           }
1823         ;
1824
1825 interface_method_declaration_body
1826         : OPEN_BRACE 
1827           {
1828                 lexer.ConstraintsParsing = false;
1829           }
1830           opt_statement_list CLOSE_BRACE
1831           {
1832                 Report.Error (531, lexer.Location,
1833                               "'{0}': interface members cannot have a definition", ((MemberName) $-1).ToString ());
1834                 $$ = null;
1835           }
1836         | SEMICOLON
1837         ;
1838
1839 interface_method_declaration
1840         : opt_attributes opt_new type namespace_or_type_name
1841           open_parens opt_formal_parameter_list CLOSE_PARENS
1842           {
1843                 lexer.ConstraintsParsing = true;
1844           }
1845           opt_type_parameter_constraints_clauses
1846           {
1847                 // Refer to the name as $-1 in interface_method_declaration_body          
1848                 $$ = $4;
1849           }
1850           interface_method_declaration_body
1851           {
1852                 lexer.ConstraintsParsing = false;
1853
1854                 MemberName name = (MemberName) $4;
1855
1856                 if ($9 != null && name.TypeArguments == null)
1857                         Report.Error (80, lexer.Location,
1858                                       "Constraints are not allowed on non-generic declarations");
1859
1860                 GenericMethod generic = null;
1861                 if (name.TypeArguments != null) {
1862                         generic = new GenericMethod (current_namespace, current_class, name,
1863                                                      (Expression) $3, (Parameters) $6);
1864
1865                         generic.SetParameterInfo ((ArrayList) $9);
1866                 }
1867
1868                 $$ = new Method (current_class, generic, (Expression) $3, (int) $2, true, name,
1869                                  (Parameters) $6, (Attributes) $1);
1870                 if (RootContext.Documentation != null)
1871                         ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1872           }
1873         | opt_attributes opt_new VOID namespace_or_type_name
1874           open_parens opt_formal_parameter_list CLOSE_PARENS
1875           {
1876                 lexer.ConstraintsParsing = true;
1877           }
1878           opt_type_parameter_constraints_clauses
1879           {
1880                 $$ = $4;
1881           }
1882           interface_method_declaration_body
1883           {
1884                 lexer.ConstraintsParsing = false;
1885
1886                 MemberName name = (MemberName) $4;
1887
1888                 if ($9 != null && name.TypeArguments == null)
1889                         Report.Error (80, lexer.Location,
1890                                       "Constraints are not allowed on non-generic declarations");
1891
1892                 GenericMethod generic = null;
1893                 if (name.TypeArguments != null) {
1894                         generic = new GenericMethod (current_namespace, current_class, name,
1895                                                      TypeManager.system_void_expr, (Parameters) $6);
1896
1897                         generic.SetParameterInfo ((ArrayList) $9);
1898                 }
1899
1900                 $$ = new Method (current_class, generic, TypeManager.system_void_expr, (int) $2,
1901                                  true, name, (Parameters) $6, (Attributes) $1);
1902                 if (RootContext.Documentation != null)
1903                         ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1904           }
1905         ;
1906
1907 interface_property_declaration
1908         : opt_attributes
1909           opt_new
1910           type IDENTIFIER 
1911           OPEN_BRACE 
1912           {
1913                 lexer.PropertyParsing = true;
1914                 implicit_value_parameter_type = (Expression)$3;
1915           }
1916           accessor_declarations 
1917           {
1918                 has_get = has_set = false; 
1919                 lexer.PropertyParsing = false;
1920                 implicit_value_parameter_type = null;
1921           }
1922           CLOSE_BRACE
1923           {
1924                 LocatedToken lt = (LocatedToken) $4;
1925                 MemberName name = new MemberName (lt.Value, lt.Location);
1926
1927                 if ($3 == TypeManager.system_void_expr) {
1928                         Report.Error (547, lt.Location, "`{0}': property or indexer cannot have void type", lt.Value);
1929                         break;
1930                 }
1931
1932                 Property p = null;
1933                 if ($7 == null) {
1934                         p = new Property (current_class, (Expression) $3, (int) $2, true,
1935                                    name, (Attributes) $1,
1936                                    null, null, false);
1937
1938                         Report.Error (548, p.Location, "`{0}': property or indexer must have at least one accessor", p.GetSignatureForError ());
1939                         break;
1940                 }
1941
1942                 Accessors accessor = (Accessors) $7;
1943                 p = new Property (current_class, (Expression) $3, (int) $2, true,
1944                                    name, (Attributes) $1,
1945                                    accessor.get_or_add, accessor.set_or_remove, accessor.declared_in_reverse);
1946
1947                 if (accessor.get_or_add != null && accessor.get_or_add.Block != null) {
1948                         Report.Error (531, p.Location, "`{0}.get': interface members cannot have a definition", p.GetSignatureForError ());
1949                         $$ = null;
1950                         break;
1951                 }
1952
1953                 if (accessor.set_or_remove != null && accessor.set_or_remove.Block != null) {
1954                         Report.Error (531, p.Location, "`{0}.set': interface members cannot have a definition", p.GetSignatureForError ());
1955                         $$ = null;
1956                         break;
1957                 }
1958
1959                 if (RootContext.Documentation != null)
1960                         p.DocComment = Lexer.consume_doc_comment ();
1961
1962                 $$ = p;
1963           }
1964         | opt_attributes
1965           opt_new
1966           type error {
1967                 CheckIdentifierToken (yyToken, GetLocation ($4));
1968                 $$ = null;
1969           }
1970         ;
1971
1972
1973 interface_event_declaration
1974         : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1975           {
1976                 LocatedToken lt = (LocatedToken) $5;
1977                 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1978                                      new MemberName (lt.Value, lt.Location),
1979                                      (Attributes) $1);
1980                 if (RootContext.Documentation != null)
1981                         ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1982           }
1983         | opt_attributes opt_new EVENT type error {
1984                 CheckIdentifierToken (yyToken, GetLocation ($5));
1985                 $$ = null;
1986           }
1987         | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN  {
1988                 LocatedToken lt = (LocatedToken) $5;
1989                 Report.Error (68, lt.Location, "`{0}.{1}': event in interface cannot have initializer", current_container.Name, lt.Value);
1990                 $$ = null;
1991           }
1992         | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1993           {
1994                 implicit_value_parameter_type = (Expression) $4;
1995                 lexer.EventParsing = true;
1996           }
1997           event_accessor_declarations
1998           {
1999                 lexer.EventParsing = false;
2000                 implicit_value_parameter_type = null;
2001           }
2002           CLOSE_BRACE {
2003                 Report.Error (69, (Location) $3, "Event in interface cannot have add or remove accessors");
2004                 $$ = null;
2005           }
2006         ;
2007
2008 interface_indexer_declaration 
2009         : opt_attributes opt_new type THIS 
2010           OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
2011           OPEN_BRACE
2012           {
2013                 lexer.PropertyParsing = true;
2014                 implicit_value_parameter_type = (Expression)$3;
2015           }
2016           accessor_declarations 
2017           { 
2018                 has_get = has_set = false;
2019                 lexer.PropertyParsing = false;
2020                 implicit_value_parameter_type = null;
2021           }
2022           CLOSE_BRACE
2023           {
2024                 Indexer i = null;
2025                 if ($10 == null) {
2026                         i = new Indexer (current_class, (Expression) $3,
2027                                   new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
2028                                   (int) $2, true, (Parameters) $6, (Attributes) $1,
2029                                   null, null, false);
2030
2031                         Report.Error (548, i.Location, "`{0}': property or indexer must have at least one accessor", i.GetSignatureForError ());
2032                         break;
2033                 }
2034
2035                 Accessors accessors = (Accessors) $10;
2036                 i = new Indexer (current_class, (Expression) $3,
2037                                   new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
2038                                   (int) $2, true, (Parameters) $6, (Attributes) $1,
2039                                    accessors.get_or_add, accessors.set_or_remove, accessors.declared_in_reverse);
2040
2041                 if (accessors.get_or_add != null && accessors.get_or_add.Block != null) {
2042                         Report.Error (531, i.Location, "`{0}.get': interface members cannot have a definition", i.GetSignatureForError ());
2043                         $$ = null;
2044                         break;
2045                 }
2046
2047                 if (accessors.set_or_remove != null && accessors.set_or_remove.Block != null) {
2048                         Report.Error (531, i.Location, "`{0}.set': interface members cannot have a definition", i.GetSignatureForError ());
2049                         $$ = null;
2050                         break;
2051                 }
2052
2053                 if (RootContext.Documentation != null)
2054                         i.DocComment = ConsumeStoredComment ();
2055
2056                 $$ = i;
2057           }
2058         ;
2059
2060 operator_declaration
2061         : opt_attributes opt_modifiers operator_declarator 
2062           {
2063                 anonymous_host = SimpleAnonymousHost.GetSimple ();
2064           }
2065           operator_body
2066           {
2067                 if ($3 == null)
2068                         break;
2069
2070                 OperatorDeclaration decl = (OperatorDeclaration) $3;
2071                 
2072                 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
2073
2074                 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null, decl.location);
2075                 if (decl.arg2type != null)
2076                         param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null, decl.location);
2077
2078                 Operator op = new Operator (
2079                         current_class, decl.optype, decl.ret_type, (int) $2, 
2080                         new Parameters (param_list),
2081                         (ToplevelBlock) $5, (Attributes) $1, decl.location);
2082
2083                 if (RootContext.Documentation != null) {
2084                         op.DocComment = tmpComment;
2085                         Lexer.doc_state = XmlCommentState.Allowed;
2086                 }
2087
2088                 SimpleAnonymousHost.Simple.Propagate (op);
2089                 anonymous_host = null;
2090
2091                 // Note again, checking is done in semantic analysis
2092                 current_container.AddOperator (op);
2093
2094                 current_local_parameters = null;
2095           }
2096         ;
2097
2098 operator_body 
2099         : block
2100         | SEMICOLON { $$ = null; }
2101         ; 
2102 operator_declarator
2103         : type OPERATOR overloadable_operator 
2104           open_parens opt_attributes opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
2105           {
2106                 // TODO: wrong location
2107                 if ((Parameter.Modifier)$6 != Parameter.Modifier.NONE)
2108                         Error_ParameterModifierNotValid ((Location) $2);
2109           
2110                 LocatedToken lt = (LocatedToken) $8;
2111                 Operator.OpType op = (Operator.OpType) $3;
2112                 CheckUnaryOperator (op, lt.Location);
2113
2114                 if (op == Operator.OpType.Addition)
2115                         op = Operator.OpType.UnaryPlus;
2116
2117                 if (op == Operator.OpType.Subtraction)
2118                         op = Operator.OpType.UnaryNegation;
2119
2120                 Parameter [] pars = new Parameter [1];
2121                 Expression type = (Expression) $7;
2122
2123                 pars [0] = new Parameter (type, lt.Value, Parameter.Modifier.NONE, (Attributes) $5, lt.Location);
2124
2125                 current_local_parameters = new Parameters (pars);
2126
2127                 if (RootContext.Documentation != null) {
2128                         tmpComment = Lexer.consume_doc_comment ();
2129                         Lexer.doc_state = XmlCommentState.NotAllowed;
2130                 }
2131
2132                 $$ = new OperatorDeclaration (op, (Expression) $1, type, lt.Value,
2133                                               null, null, (Location) $2);
2134           }
2135         | type OPERATOR overloadable_operator
2136           open_parens 
2137                 opt_attributes opt_parameter_modifier type IDENTIFIER COMMA
2138                 opt_attributes opt_parameter_modifier type IDENTIFIER 
2139           CLOSE_PARENS
2140           {
2141                 // TODO: wrong location
2142                 if ((Parameter.Modifier)$6 != Parameter.Modifier.NONE || (Parameter.Modifier)$11 != Parameter.Modifier.NONE)
2143                         Error_ParameterModifierNotValid ((Location) $2);
2144
2145                 LocatedToken ltParam1 = (LocatedToken) $8;
2146                 LocatedToken ltParam2 = (LocatedToken) $13;
2147                 CheckBinaryOperator ((Operator.OpType) $3, (Location) $2);
2148
2149                 Parameter [] pars = new Parameter [2];
2150
2151                 Expression typeL = (Expression) $7;
2152                 Expression typeR = (Expression) $12;
2153
2154                pars [0] = new Parameter (typeL, ltParam1.Value, Parameter.Modifier.NONE, (Attributes) $5, ltParam1.Location);
2155                pars [1] = new Parameter (typeR, ltParam2.Value, Parameter.Modifier.NONE, (Attributes) $10, ltParam2.Location);
2156
2157                current_local_parameters = new Parameters (pars);
2158
2159                 if (RootContext.Documentation != null) {
2160                         tmpComment = Lexer.consume_doc_comment ();
2161                         Lexer.doc_state = XmlCommentState.NotAllowed;
2162                 }
2163                
2164                $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1, 
2165                                              typeL, ltParam1.Value,
2166                                              typeR, ltParam2.Value, (Location) $2);
2167           }
2168         | conversion_operator_declarator
2169         | type OPERATOR overloadable_operator
2170           open_parens 
2171                 opt_attributes opt_parameter_modifier type IDENTIFIER COMMA
2172                 opt_attributes opt_parameter_modifier type IDENTIFIER COMMA error
2173           {
2174                 Report.Error (1534, (Location) $2, "Overloaded binary operator `{0}' takes two parameters",
2175                         Operator.GetName ((Operator.OpType) $3));
2176                 $$ = null;
2177           }
2178         | type OPERATOR overloadable_operator 
2179           open_parens CLOSE_PARENS
2180           {
2181                 Report.Error (1535, (Location) $2, "Overloaded unary operator `{0}' takes one parameter",
2182                         Operator.GetName ((Operator.OpType) $3));
2183                 $$ = null;
2184           }
2185         ;
2186
2187 overloadable_operator
2188 // Unary operators:
2189         : BANG   { $$ = Operator.OpType.LogicalNot; }
2190         | TILDE  { $$ = Operator.OpType.OnesComplement; }  
2191         | OP_INC { $$ = Operator.OpType.Increment; }
2192         | OP_DEC { $$ = Operator.OpType.Decrement; }
2193         | TRUE   { $$ = Operator.OpType.True; }
2194         | FALSE  { $$ = Operator.OpType.False; }
2195 // Unary and binary:
2196         | PLUS { $$ = Operator.OpType.Addition; }
2197         | MINUS { $$ = Operator.OpType.Subtraction; }
2198 // Binary:
2199         | STAR { $$ = Operator.OpType.Multiply; }
2200         | DIV {  $$ = Operator.OpType.Division; }
2201         | PERCENT { $$ = Operator.OpType.Modulus; }
2202         | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
2203         | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
2204         | CARRET { $$ = Operator.OpType.ExclusiveOr; }
2205         | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
2206         | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
2207         | OP_EQ { $$ = Operator.OpType.Equality; }
2208         | OP_NE { $$ = Operator.OpType.Inequality; }
2209         | OP_GT { $$ = Operator.OpType.GreaterThan; }
2210         | OP_LT { $$ = Operator.OpType.LessThan; }
2211         | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
2212         | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
2213         ;
2214
2215 conversion_operator_declarator
2216         : IMPLICIT OPERATOR type open_parens opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
2217           {
2218                 // TODO: wrong location
2219                 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
2220                         Error_ParameterModifierNotValid (GetLocation ($4));
2221
2222                 LocatedToken lt = (LocatedToken) $7;
2223                 Parameter [] pars = new Parameter [1];
2224
2225                 pars [0] = new Parameter ((Expression) $6, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2226
2227                 current_local_parameters = new Parameters (pars);  
2228                   
2229                 if (RootContext.Documentation != null) {
2230                         tmpComment = Lexer.consume_doc_comment ();
2231                         Lexer.doc_state = XmlCommentState.NotAllowed;
2232                 }
2233
2234                 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $6, lt.Value,
2235                                               null, null, (Location) $2);
2236           }
2237         | EXPLICIT OPERATOR type open_parens opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
2238           {
2239                 // TODO: wrong location
2240                 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
2241                         Error_ParameterModifierNotValid (GetLocation ($4));
2242           
2243                 LocatedToken lt = (LocatedToken) $7;
2244                 Parameter [] pars = new Parameter [1];
2245
2246                 pars [0] = new Parameter ((Expression) $6, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2247
2248                 current_local_parameters = new Parameters (pars);  
2249                   
2250                 if (RootContext.Documentation != null) {
2251                         tmpComment = Lexer.consume_doc_comment ();
2252                         Lexer.doc_state = XmlCommentState.NotAllowed;
2253                 }
2254
2255                 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $6, lt.Value,
2256                                               null, null, (Location) $2);
2257           }
2258         | IMPLICIT error 
2259           {
2260                 syntax_error ((Location) $1, "'operator' expected");
2261           }
2262         | EXPLICIT error 
2263           {
2264                 syntax_error ((Location) $1, "'operator' expected");
2265           }
2266         ;
2267
2268 constructor_declaration
2269         : opt_attributes
2270           opt_modifiers
2271           constructor_declarator
2272           constructor_body
2273           { 
2274                 Constructor c = (Constructor) $3;
2275                 c.Block = (ToplevelBlock) $4;
2276                 c.OptAttributes = (Attributes) $1;
2277                 c.ModFlags = (int) $2;
2278         
2279                 if (RootContext.Documentation != null)
2280                         c.DocComment = ConsumeStoredComment ();
2281
2282                 if (c.Name == current_container.Basename){
2283                         if ((c.ModFlags & Modifiers.STATIC) != 0){
2284                                 if ((c.ModFlags & Modifiers.Accessibility) != 0){
2285                                         Report.Error (515, c.Location,
2286                                                 "`{0}': access modifiers are not allowed on static constructors",
2287                                                 c.GetSignatureForError ());
2288                                 }
2289         
2290                                 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);   
2291         
2292                                 if (c.Initializer != null){
2293                                         Report.Error (514, c.Location,
2294                                                 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2295                                                 c.GetSignatureForError ());
2296                                 }
2297                         } else {
2298                                 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2299                         }
2300                 } else {
2301                         // We let another layer check the validity of the constructor.
2302                         //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
2303                 }
2304
2305                 current_container.AddConstructor (c);
2306
2307                 current_local_parameters = null;
2308                 if (RootContext.Documentation != null)
2309                         Lexer.doc_state = XmlCommentState.Allowed;
2310           }
2311         ;
2312
2313 constructor_declarator
2314         : constructor_header
2315           {
2316                 $$ = $1;
2317           }
2318         | constructor_header constructor_initializer
2319           {
2320                 ((Constructor)$1).Initializer = (ConstructorInitializer) $2;
2321                 $$ = $1;
2322           }
2323         ;
2324
2325 constructor_header
2326         : IDENTIFIER
2327           {
2328                 if (RootContext.Documentation != null) {
2329                         tmpComment = Lexer.consume_doc_comment ();
2330                         Lexer.doc_state = XmlCommentState.Allowed;
2331                 }
2332           }
2333           open_parens opt_formal_parameter_list CLOSE_PARENS
2334           {
2335                 LocatedToken lt = (LocatedToken) $1;
2336                 current_local_parameters = (Parameters) $4;
2337                 current_block = new ToplevelBlock (null, current_local_parameters, null, lt.Location);
2338
2339                 $$ = new Constructor (current_class, lt.Value, 0, current_local_parameters,
2340                                       null, lt.Location);
2341
2342                 anonymous_host = (IAnonymousHost) $$;
2343           }
2344         ;
2345
2346 constructor_body
2347         : block_prepared
2348         | SEMICOLON             { current_block = null; $$ = null; }
2349         ;
2350
2351 constructor_initializer
2352         : COLON BASE open_parens opt_argument_list CLOSE_PARENS
2353           {
2354                 $$ = new ConstructorBaseInitializer ((ArrayList) $4, (Location) $2);
2355           }
2356         | COLON THIS open_parens opt_argument_list CLOSE_PARENS
2357           {
2358                 $$ = new ConstructorThisInitializer ((ArrayList) $4, (Location) $2);
2359           }
2360         | COLON error {
2361                 Report.Error (1018, (Location) $1, "Keyword this or base expected");
2362                 $$ = null;
2363           }
2364         ;
2365
2366 opt_finalizer
2367         : /* EMPTY */           { $$ = 0; }
2368         | UNSAFE                { $$ = Modifiers.UNSAFE; }
2369         | EXTERN                { $$ = Modifiers.EXTERN; }
2370         ;
2371         
2372 destructor_declaration
2373         : opt_attributes opt_finalizer TILDE 
2374           {
2375                 if (RootContext.Documentation != null) {
2376                         tmpComment = Lexer.consume_doc_comment ();
2377                         Lexer.doc_state = XmlCommentState.NotAllowed;
2378                 }
2379           }
2380           IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2381           {
2382                 LocatedToken lt = (LocatedToken) $5;
2383                 if (lt.Value != current_container.MemberName.Name){
2384                         Report.Error (574, lt.Location, "Name of destructor must match name of class");
2385                 } else if (current_container.Kind != Kind.Class){
2386                         Report.Error (575, lt.Location, "Only class types can contain destructor");
2387                 } else {
2388                         Location l = lt.Location;
2389
2390                         int m = (int) $2;
2391                         if (!RootContext.StdLib && current_container.Name == "System.Object")
2392                                 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2393                         else
2394                                 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2395                         
2396                         Method d = new Destructor (
2397                                 current_class, TypeManager.system_void_expr, m, "Finalize", 
2398                                 Parameters.EmptyReadOnlyParameters, (Attributes) $1, l);
2399                         if (RootContext.Documentation != null)
2400                                 d.DocComment = ConsumeStoredComment ();
2401                   
2402                         d.Block = (ToplevelBlock) $8;
2403                         current_container.AddMethod (d);
2404                 }
2405           }
2406         ;
2407
2408 event_declaration
2409         : opt_attributes
2410           opt_modifiers
2411           EVENT type variable_declarators SEMICOLON
2412           {
2413                 current_array_type = null;
2414                 foreach (VariableDeclaration var in (ArrayList) $5) {
2415
2416                         MemberName name = new MemberName (var.identifier,
2417                                 var.Location);
2418
2419                         EventField e = new EventField (
2420                                 current_class, (Expression) $4, (int) $2, false, name,
2421                                 (Attributes) $1);
2422
2423                         e.Initializer = var.expression_or_array_initializer;
2424
2425                         current_container.AddEvent (e);
2426
2427                         if (RootContext.Documentation != null) {
2428                                 e.DocComment = Lexer.consume_doc_comment ();
2429                                 Lexer.doc_state = XmlCommentState.Allowed;
2430                         }
2431                 }
2432           }
2433         | opt_attributes
2434           opt_modifiers
2435           EVENT type namespace_or_type_name
2436           OPEN_BRACE
2437           {
2438                 implicit_value_parameter_type = (Expression) $4;  
2439                 lexer.EventParsing = true;
2440           }
2441           event_accessor_declarations
2442           {
2443                 lexer.EventParsing = false;  
2444           }
2445           CLOSE_BRACE
2446           {
2447                 MemberName name = (MemberName) $5;
2448
2449                 if ($8 == null){
2450                         Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2451                                 current_container.Name, name.ToString ());
2452                         $$ = null;
2453                 } else {
2454                         Accessors accessors = (Accessors) $8;
2455                         
2456                         if (name.TypeArguments != null)
2457                                 syntax_error (lexer.Location, "an event can't have type arguments");
2458
2459                         if (accessors.get_or_add == null || accessors.set_or_remove == null)
2460                                 // CS0073 is already reported, so no CS0065 here.
2461                                 $$ = null;
2462                         else {
2463                                 Event e = new EventProperty (
2464                                         current_class, (Expression) $4, (int) $2, false, name,
2465                                         (Attributes) $1, accessors.get_or_add, accessors.set_or_remove);
2466                                 if (RootContext.Documentation != null) {
2467                                         e.DocComment = Lexer.consume_doc_comment ();
2468                                         Lexer.doc_state = XmlCommentState.Allowed;
2469                                 }
2470
2471                                 current_container.AddEvent (e);
2472                                 implicit_value_parameter_type = null;
2473                         }
2474                 }
2475           }
2476         | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
2477                 MemberName mn = (MemberName) $5;
2478
2479                 if (mn.Left != null)
2480                         Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2481                 else 
2482                         Report.Error (71, mn.Location, "Event declaration should use property syntax");
2483
2484                 if (RootContext.Documentation != null)
2485                         Lexer.doc_state = XmlCommentState.Allowed;
2486           }
2487         ;
2488
2489 event_accessor_declarations
2490         : add_accessor_declaration remove_accessor_declaration
2491           {
2492                 $$ = new Accessors ((Accessor) $1, (Accessor) $2);
2493           }
2494         | remove_accessor_declaration add_accessor_declaration
2495           {
2496                 Accessors accessors = new Accessors ((Accessor) $2, (Accessor) $1);
2497                 accessors.declared_in_reverse = true;
2498                 $$ = accessors;
2499           }     
2500         | add_accessor_declaration  { $$ = null; } 
2501         | remove_accessor_declaration { $$ = null; } 
2502         | error
2503           { 
2504                 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2505                 $$ = null;
2506           }
2507         | { $$ = null; }
2508         ;
2509
2510 add_accessor_declaration
2511         : opt_attributes ADD
2512           {
2513                 Parameter [] args = new Parameter [1];
2514                 Parameter implicit_value_parameter = new Parameter (
2515                         implicit_value_parameter_type, "value", 
2516                         Parameter.Modifier.NONE, null, (Location) $2);
2517
2518                 args [0] = implicit_value_parameter;
2519                 
2520                 current_local_parameters = new Parameters (args);  
2521                 lexer.EventParsing = false;
2522                 
2523                 anonymous_host = SimpleAnonymousHost.GetSimple ();
2524           }
2525           block
2526           {
2527                 Accessor accessor = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2528                 lexer.EventParsing = true;
2529                 
2530                 current_local_parameters = null;
2531                 SimpleAnonymousHost.Simple.Propagate (accessor);
2532                 anonymous_host = null;
2533                 
2534                 $$ = accessor;
2535           }
2536         | opt_attributes ADD error {
2537                 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2538                 $$ = null;
2539           }
2540         | opt_attributes modifiers ADD {
2541                 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2542                 $$ = null;
2543           }
2544         ;
2545
2546 remove_accessor_declaration
2547         : opt_attributes REMOVE
2548           {
2549                 Parameter [] args = new Parameter [1];
2550                 Parameter implicit_value_parameter = new Parameter (
2551                         implicit_value_parameter_type, "value", 
2552                         Parameter.Modifier.NONE, null, (Location) $2);
2553
2554                 args [0] = implicit_value_parameter;
2555                 
2556                 current_local_parameters = new Parameters (args);  
2557                 lexer.EventParsing = false;
2558           }
2559           block
2560           {
2561                 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2562                 lexer.EventParsing = true;
2563           }
2564         | opt_attributes REMOVE error {
2565                 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2566                 $$ = null;
2567           }
2568         | opt_attributes modifiers REMOVE {
2569                 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2570                 $$ = null;
2571           }
2572         ;
2573
2574 indexer_declaration
2575         : opt_attributes opt_modifiers indexer_declarator 
2576           OPEN_BRACE
2577           {
2578                 IndexerDeclaration decl = (IndexerDeclaration) $3;
2579
2580                 implicit_value_parameter_type = decl.type;
2581                 
2582                 lexer.PropertyParsing = true;
2583                 parsing_indexer  = true;
2584                 
2585                 indexer_parameters = decl.param_list;
2586                 anonymous_host = SimpleAnonymousHost.GetSimple ();
2587           }
2588           accessor_declarations 
2589           {
2590                   lexer.PropertyParsing = false;
2591                   has_get = has_set = false;
2592                   parsing_indexer  = false;
2593           }
2594           CLOSE_BRACE
2595           { 
2596                 if ($6 == null)
2597                         break;
2598
2599                 // The signature is computed from the signature of the indexer.  Look
2600                 // at section 3.6 on the spec
2601                 Indexer indexer;
2602                 IndexerDeclaration decl = (IndexerDeclaration) $3;
2603                 Location loc = decl.location;
2604                 Accessors accessors = (Accessors) $6;
2605                 Accessor get_block = accessors.get_or_add;
2606                 Accessor set_block = accessors.set_or_remove;
2607
2608                 MemberName name;
2609                 if (decl.interface_type != null)
2610                         name = new MemberName (decl.interface_type, TypeContainer.DefaultIndexerName, loc);
2611                 else
2612                         name = new MemberName (TypeContainer.DefaultIndexerName, loc);
2613
2614                 indexer = new Indexer (current_class, decl.type, name,
2615                                        (int) $2, false, decl.param_list, (Attributes) $1,
2616                                        get_block, set_block, accessors.declared_in_reverse);
2617
2618                 if (RootContext.Documentation != null)
2619                         indexer.DocComment = ConsumeStoredComment ();
2620
2621                 current_container.AddIndexer (indexer);
2622                 
2623                 current_local_parameters = null;
2624                 implicit_value_parameter_type = null;
2625                 indexer_parameters = null;
2626           }
2627         ;
2628
2629 indexer_declarator
2630         : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2631           {
2632                 Parameters pars = (Parameters) $4;
2633                 if (pars.HasArglist) {
2634                         // "__arglist is not valid in this context"
2635                         Report.Error (1669, (Location) $2, "__arglist is not valid in this context");
2636                 } else if (pars.Empty){
2637                         Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
2638                 }
2639                 if (RootContext.Documentation != null) {
2640                         tmpComment = Lexer.consume_doc_comment ();
2641                         Lexer.doc_state = XmlCommentState.Allowed;
2642                 }
2643
2644                 $$ = new IndexerDeclaration ((Expression) $1, null, pars, (Location) $2);
2645           }
2646         | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2647           {
2648                 Parameters pars = (Parameters) $6;
2649
2650                 if (pars.HasArglist) {
2651                         // "__arglist is not valid in this context"
2652                         Report.Error (1669, (Location) $4, "__arglist is not valid in this context");
2653                 } else if (pars.Empty){
2654                         Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
2655                 }
2656
2657                 MemberName name = (MemberName) $2;
2658                 $$ = new IndexerDeclaration ((Expression) $1, name, pars, (Location) $4);
2659
2660                 if (RootContext.Documentation != null) {
2661                         tmpComment = Lexer.consume_doc_comment ();
2662                         Lexer.doc_state = XmlCommentState.Allowed;
2663                 }
2664           }
2665         ;
2666
2667 enum_declaration
2668         : opt_attributes
2669           opt_modifiers
2670           ENUM IDENTIFIER 
2671           opt_enum_base {
2672                 if (RootContext.Documentation != null)
2673                         enumTypeComment = Lexer.consume_doc_comment ();
2674           }
2675           enum_body
2676           opt_semicolon
2677           {
2678                 LocatedToken lt = (LocatedToken) $4;
2679                 Location enum_location = lt.Location;
2680
2681                 MemberName name = MakeName (new MemberName (lt.Value, enum_location));
2682                 Enum e = new Enum (current_namespace, current_class, (Expression) $5, (int) $2,
2683                                    name, (Attributes) $1);
2684                 
2685                 if (RootContext.Documentation != null)
2686                         e.DocComment = enumTypeComment;
2687
2688
2689                 EnumMember em = null;
2690                 foreach (VariableDeclaration ev in (ArrayList) $7) {
2691                         em = new EnumMember (
2692                                 e, em, ev.identifier, (Expression) ev.expression_or_array_initializer,
2693                                 ev.OptAttributes, ev.Location);
2694
2695 //                      if (RootContext.Documentation != null)
2696                                 em.DocComment = ev.DocComment;
2697
2698                         e.AddEnumMember (em);
2699                 }
2700
2701                 current_container.AddTypeContainer (e);
2702                 $$ = e;
2703
2704           }
2705         ;
2706
2707 opt_enum_base
2708         : /* empty */           { $$ = TypeManager.system_int32_expr; }
2709         | COLON type            { $$ = $2;   }
2710         ;
2711
2712 enum_body
2713         : OPEN_BRACE
2714           {
2715                 if (RootContext.Documentation != null)
2716                         Lexer.doc_state = XmlCommentState.Allowed;
2717           }
2718           opt_enum_member_declarations
2719           {
2720                 // here will be evaluated after CLOSE_BLACE is consumed.
2721                 if (RootContext.Documentation != null)
2722                         Lexer.doc_state = XmlCommentState.Allowed;
2723           }
2724           CLOSE_BRACE
2725           {
2726                 $$ = $3;
2727           }
2728         ;
2729
2730 opt_enum_member_declarations
2731         : /* empty */                   { $$ = new ArrayList (4); }
2732         | enum_member_declarations opt_comma { $$ = $1; }
2733         ;
2734
2735 enum_member_declarations
2736         : enum_member_declaration 
2737           {
2738                 ArrayList l = new ArrayList (4);
2739
2740                 l.Add ($1);
2741                 $$ = l;
2742           }
2743         | enum_member_declarations COMMA enum_member_declaration
2744           {
2745                 ArrayList l = (ArrayList) $1;
2746
2747                 l.Add ($3);
2748
2749                 $$ = l;
2750           }
2751         ;
2752
2753 enum_member_declaration
2754         : opt_attributes IDENTIFIER 
2755           {
2756                 VariableDeclaration vd = new VariableDeclaration (
2757                         (LocatedToken) $2, null, (Attributes) $1);
2758
2759                 if (RootContext.Documentation != null) {
2760                         vd.DocComment = Lexer.consume_doc_comment ();
2761                         Lexer.doc_state = XmlCommentState.Allowed;
2762                 }
2763
2764                 $$ = vd;
2765           }
2766         | opt_attributes IDENTIFIER
2767           {
2768                 if (RootContext.Documentation != null) {
2769                         tmpComment = Lexer.consume_doc_comment ();
2770                         Lexer.doc_state = XmlCommentState.NotAllowed;
2771                 }
2772           }
2773           ASSIGN expression
2774           { 
2775                 VariableDeclaration vd = new VariableDeclaration (
2776                         (LocatedToken) $2, $5, (Attributes) $1);
2777
2778                 if (RootContext.Documentation != null)
2779                         vd.DocComment = ConsumeStoredComment ();
2780
2781                 $$ = vd;
2782           }
2783         ;
2784
2785 delegate_declaration
2786         : opt_attributes
2787           opt_modifiers
2788           DELEGATE
2789           {
2790                 lexer.ConstraintsParsing = true;
2791           }
2792           type type_name
2793           open_parens opt_formal_parameter_list CLOSE_PARENS
2794           {
2795                 MemberName name = MakeName ((MemberName) $6);
2796                 Parameters p = (Parameters) $8;
2797                 if (p.HasArglist) {
2798                         // TODO: wrong location
2799                         Report.Error (1669, name.Location, "__arglist is not valid in this context");
2800                 }
2801
2802                 Delegate del = new Delegate (current_namespace, current_class, (Expression) $5,
2803                                              (int) $2, name, p, (Attributes) $1);
2804
2805                 if (RootContext.Documentation != null) {
2806                         del.DocComment = Lexer.consume_doc_comment ();
2807                         Lexer.doc_state = XmlCommentState.Allowed;
2808                 }
2809
2810                 current_container.AddDelegate (del);
2811                 current_delegate = del;
2812           }
2813           opt_type_parameter_constraints_clauses
2814           {
2815                 lexer.ConstraintsParsing = false;
2816           }
2817           SEMICOLON
2818           {
2819                 current_delegate.SetParameterInfo ((ArrayList) $11);
2820                 $$ = current_delegate;
2821
2822                 current_delegate = null;
2823           }
2824         ;
2825
2826 opt_nullable
2827         : /* empty */
2828           {
2829                 lexer.CheckNullable (false);
2830                 $$ = false;
2831           }
2832         | INTERR
2833           {
2834                 lexer.CheckNullable (true);
2835                 $$ = true;
2836           }
2837         ;
2838
2839 namespace_or_type_name
2840         : IDENTIFIER opt_type_argument_list
2841           {
2842                 LocatedToken lt = (LocatedToken) $1;
2843                 $$ = new MemberName (lt.Value, (TypeArguments) $2, lt.Location);
2844           }
2845         | IDENTIFIER DOUBLE_COLON IDENTIFIER {
2846                 LocatedToken lt1 = (LocatedToken) $1;
2847                 LocatedToken lt2 = (LocatedToken) $3;
2848                 $$ = new MemberName (lt1.Value, lt2.Value, lt2.Location);
2849           }
2850         | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list {
2851                 LocatedToken lt = (LocatedToken) $3;
2852                 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $4, lt.Location);
2853           }
2854         ;
2855
2856 member_name
2857         : IDENTIFIER opt_type_parameter_list
2858           {
2859                 LocatedToken lt = (LocatedToken) $1;
2860                 $$ = new MemberName (lt.Value, (TypeArguments) $2, lt.Location);
2861           }
2862         | namespace_or_type_name DOT IDENTIFIER opt_type_parameter_list 
2863           {
2864                 LocatedToken lt = (LocatedToken) $3;
2865                 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $4, lt.Location);
2866           }
2867         ;
2868
2869 type_name
2870         : IDENTIFIER opt_type_parameter_list
2871           {
2872                 LocatedToken lt = (LocatedToken) $1;
2873                 $$ = new MemberName (lt.Value, (TypeArguments)$2, lt.Location);   
2874           }
2875         ;
2876
2877 //
2878 // Generics arguments  (any type, without attributes)
2879 //
2880
2881 opt_type_argument_list
2882         : /* empty */                { $$ = null; } 
2883         | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2884           {
2885                 if (RootContext.Version == LanguageVersion.ISO_1)
2886                         Report.FeatureIsNotISO1 (lexer.Location, "generics");     
2887           
2888                 $$ = $2;
2889           }
2890         ;
2891
2892 //
2893 // Generics parameters (identifiers only, with attributes), used in type, method declarations
2894 //
2895
2896 opt_type_parameter_list
2897         : /* empty */                { $$ = null; } 
2898         | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2899           {
2900                 if (RootContext.Version == LanguageVersion.ISO_1)
2901                         Report.FeatureIsNotISO1 (lexer.Location, "generics");
2902           
2903                 $$ = $2;
2904           }
2905         ;
2906
2907 type_arguments
2908         : type_argument
2909           {
2910                 TypeArguments type_args = new TypeArguments (lexer.Location);
2911                 type_args.Add ((Expression) $1);
2912                 $$ = type_args;
2913           }
2914         | type_arguments COMMA type_argument
2915           {
2916                 TypeArguments type_args = (TypeArguments) $1;
2917                 type_args.Add ((Expression) $3);
2918                 $$ = type_args;
2919           }       
2920         ;
2921
2922 type_argument
2923         : type
2924           {
2925                 $$ = $1;
2926           }
2927         | attribute_sections type
2928           {
2929                 SimpleName sn = $2 as SimpleName;
2930                 if (sn == null)
2931                         Error_TypeExpected (GetLocation ($2));
2932                 else
2933                         $2 = new TypeParameterName (sn.Name, (Attributes) $1, lexer.Location);
2934                 $$ = $2;          
2935           }
2936         ;
2937         
2938 /* 
2939  * Before you think of adding a return_type, notice that we have been
2940  * using two rules in the places where it matters (one rule using type
2941  * and another identical one that uses VOID as the return type).  This
2942  * gets rid of a shift/reduce couple
2943  */
2944 type
2945         : namespace_or_type_name opt_nullable
2946           {
2947                 MemberName name = (MemberName) $1;
2948                 $$ = name.GetTypeExpression ();
2949
2950                 if ((bool) $2)
2951                         $$ = new ComposedCast ((Expression) $$, "?", lexer.Location);
2952           }
2953         | builtin_types opt_nullable
2954           {
2955                 if ((bool) $2)
2956                         $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2957           }
2958         | array_type
2959         | pointer_type
2960         ;
2961
2962 pointer_type
2963         : type STAR
2964           {
2965                 //
2966                 // Note that here only unmanaged types are allowed but we
2967                 // can't perform checks during this phase - we do it during
2968                 // semantic analysis.
2969                 //
2970                 $$ = new ComposedCast ((Expression) $1, "*", Lexer.Location);
2971           }
2972         | VOID STAR
2973           {
2974                 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2975           }
2976         ;
2977
2978 non_expression_type
2979         : builtin_types opt_nullable
2980           {
2981                 if ((bool) $2)
2982                         $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2983           }
2984         | non_expression_type rank_specifier
2985           {
2986                 Location loc = GetLocation ($1);
2987                 if (loc.IsNull)
2988                         loc = lexer.Location;
2989                 $$ = new ComposedCast ((Expression) $1, (string) $2, loc);
2990           }
2991         | non_expression_type STAR
2992           {
2993                 Location loc = GetLocation ($1);
2994                 if (loc.IsNull)
2995                         loc = lexer.Location;
2996                 $$ = new ComposedCast ((Expression) $1, "*", loc);
2997           }
2998         | expression rank_specifiers 
2999           {
3000                 $$ = new ComposedCast ((Expression) $1, (string) $2);
3001           }
3002         | expression STAR 
3003           {
3004                 $$ = new ComposedCast ((Expression) $1, "*");
3005           }
3006         
3007         //
3008         // We need this because the parser will happily go and reduce IDENTIFIER STAR
3009         // through this different path
3010         //
3011         | multiplicative_expression STAR 
3012           {
3013                 $$ = new ComposedCast ((Expression) $1, "*");
3014           }
3015         ;
3016
3017 type_list
3018         : type
3019           {
3020                 ArrayList types = new ArrayList (4);
3021
3022                 types.Add ($1);
3023                 $$ = types;
3024           }
3025         | type_list COMMA type
3026           {
3027                 ArrayList types = (ArrayList) $1;
3028
3029                 types.Add ($3);
3030                 $$ = types;
3031           }
3032         ;
3033
3034 /*
3035  * replaces all the productions for isolating the various
3036  * simple types, but we need this to reuse it easily in local_variable_type
3037  */
3038 builtin_types
3039         : OBJECT        { $$ = TypeManager.system_object_expr; }
3040         | STRING        { $$ = TypeManager.system_string_expr; }
3041         | BOOL          { $$ = TypeManager.system_boolean_expr; }
3042         | DECIMAL       { $$ = TypeManager.system_decimal_expr; }
3043         | FLOAT         { $$ = TypeManager.system_single_expr; }
3044         | DOUBLE        { $$ = TypeManager.system_double_expr; }
3045         | integral_type
3046         ;
3047
3048 integral_type
3049         : SBYTE         { $$ = TypeManager.system_sbyte_expr; }
3050         | BYTE          { $$ = TypeManager.system_byte_expr; }
3051         | SHORT         { $$ = TypeManager.system_int16_expr; }
3052         | USHORT        { $$ = TypeManager.system_uint16_expr; }
3053         | INT           { $$ = TypeManager.system_int32_expr; }
3054         | UINT          { $$ = TypeManager.system_uint32_expr; }
3055         | LONG          { $$ = TypeManager.system_int64_expr; }
3056         | ULONG         { $$ = TypeManager.system_uint64_expr; }
3057         | CHAR          { $$ = TypeManager.system_char_expr; }
3058         | VOID          { $$ = TypeManager.system_void_expr; }
3059         ;
3060
3061 array_type
3062         : type rank_specifiers opt_nullable
3063           {
3064                 string rank_specifiers = (string) $2;
3065                 if ((bool) $3)
3066                         rank_specifiers += "?";
3067
3068                 $$ = current_array_type = new ComposedCast ((Expression) $1, rank_specifiers);
3069           }
3070         ;
3071
3072 //
3073 // Expressions, section 7.5
3074 //
3075 primary_expression
3076         : literal
3077           {
3078                 // 7.5.1: Literals
3079           }
3080         | type_name
3081           {
3082                 MemberName mn = (MemberName) $1;
3083                 $$ = mn.GetTypeExpression ();
3084           }
3085         | IDENTIFIER DOUBLE_COLON IDENTIFIER
3086           {
3087                 LocatedToken lt1 = (LocatedToken) $1;
3088                 LocatedToken lt2 = (LocatedToken) $3;
3089                 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, lt2.Location);
3090           }
3091         | parenthesized_expression
3092         | default_value_expression
3093         | member_access
3094         | invocation_expression
3095         | element_access
3096         | this_access
3097         | base_access
3098         | post_increment_expression
3099         | post_decrement_expression
3100         | new_expression
3101         | typeof_expression
3102         | sizeof_expression
3103         | checked_expression
3104         | unchecked_expression
3105         | pointer_member_access
3106         | anonymous_method_expression
3107         ;
3108
3109 literal
3110         : boolean_literal
3111         | integer_literal
3112         | real_literal
3113         | LITERAL_CHARACTER     { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
3114         | LITERAL_STRING        { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); } 
3115         | NULL                  { $$ = new NullLiteral (lexer.Location); }
3116         ;
3117
3118 real_literal
3119         : LITERAL_FLOAT         { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
3120         | LITERAL_DOUBLE        { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
3121         | LITERAL_DECIMAL       { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
3122         ;
3123
3124 integer_literal
3125         : LITERAL_INTEGER       { 
3126                 object v = lexer.Value;
3127
3128                 if (v is int){
3129                         $$ = new IntLiteral ((int) v, lexer.Location);
3130                 } else if (v is uint)
3131                         $$ = new UIntLiteral ((UInt32) v, lexer.Location);
3132                 else if (v is long)
3133                         $$ = new LongLiteral ((Int64) v, lexer.Location);
3134                 else if (v is ulong)
3135                         $$ = new ULongLiteral ((UInt64) v, lexer.Location);
3136                 else
3137                         Console.WriteLine ("OOPS.  Unexpected result from scanner");
3138           }
3139         ;
3140
3141 boolean_literal
3142         : TRUE                  { $$ = new BoolLiteral (true, lexer.Location); }
3143         | FALSE                 { $$ = new BoolLiteral (false, lexer.Location); }
3144         ;
3145
3146 parenthesized_expression_0
3147         : OPEN_PARENS expression CLOSE_PARENS
3148           {
3149                 $$ = $2;
3150                 lexer.Deambiguate_CloseParens ($$);
3151                 // After this, the next token returned is one of
3152                 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST (CLOSE_PARENS), CLOSE_PARENS_OPEN_PARENS
3153                 // or CLOSE_PARENS_MINUS.
3154           }
3155         | OPEN_PARENS expression error { CheckToken (1026, yyToken, "Expecting ')'", lexer.Location); }
3156         ;
3157
3158 parenthesized_expression
3159         : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
3160           {
3161                 $$ = $1;
3162           }  
3163         | parenthesized_expression_0 CLOSE_PARENS
3164           {
3165                 $$ = $1;
3166           }       
3167         | parenthesized_expression_0 CLOSE_PARENS_MINUS
3168           {
3169                 // If a parenthesized expression is followed by a minus, we need to wrap
3170                 // the expression inside a ParenthesizedExpression for the CS0075 check
3171                 // in Binary.DoResolve().
3172                 $$ = new ParenthesizedExpression ((Expression) $1);
3173           }
3174         ;
3175
3176 member_access
3177         : primary_expression DOT IDENTIFIER opt_type_argument_list
3178           {
3179                 LocatedToken lt = (LocatedToken) $3;
3180                 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
3181           }
3182         | predefined_type DOT IDENTIFIER opt_type_argument_list
3183           {
3184                 LocatedToken lt = (LocatedToken) $3;
3185                 // TODO: Location is wrong as some predefined types doesn't hold a location
3186                 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
3187           }
3188         ;
3189
3190 predefined_type
3191         : builtin_types
3192         ;
3193
3194 invocation_expression
3195         : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
3196           {
3197                 if ($1 == null)
3198                         Report.Error (1, (Location) $2, "Parse error");
3199                 else
3200                         $$ = new Invocation ((Expression) $1, (ArrayList) $3);
3201           }
3202         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
3203           {
3204                 $$ = new Invocation ((Expression) $1, new ArrayList ());
3205           }
3206         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
3207           {
3208                 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
3209           }
3210         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
3211           {
3212                 ArrayList args = new ArrayList (1);
3213                 args.Add ($4);
3214                 $$ = new Invocation ((Expression) $1, args);
3215           }
3216         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
3217           {
3218                 ArrayList args = ((ArrayList) $4);
3219                 args.Add ($6);
3220                 $$ = new Invocation ((Expression) $1, args);
3221           }
3222         ;
3223
3224 opt_object_or_collection_initializer
3225         : /* empty */           { $$ = null; }
3226         | object_or_collection_initializer
3227         ;
3228
3229 object_or_collection_initializer
3230         : OPEN_BRACE opt_member_initializer_list CLOSE_BRACE
3231           {
3232                 if ($2 == null)
3233                   $$ = CollectionOrObjectInitializers.Empty;
3234                 else
3235                   $$ = new CollectionOrObjectInitializers ((ArrayList) $2, GetLocation ($1));
3236           }
3237         | OPEN_BRACE member_initializer_list COMMA CLOSE_BRACE
3238           {
3239                 $$ = new CollectionOrObjectInitializers ((ArrayList) $2, GetLocation ($1));
3240           }
3241         ;
3242
3243 opt_member_initializer_list
3244         : /* empty */           { $$ = null; }
3245         | member_initializer_list
3246         {
3247                 $$ = $1;
3248         }
3249         ;
3250
3251 member_initializer_list
3252         : member_initializer
3253           {
3254                 ArrayList a = new ArrayList ();
3255                 a.Add ($1);
3256                 $$ = a;
3257           }
3258         | member_initializer_list COMMA member_initializer
3259           {
3260                 ArrayList a = (ArrayList)$1;
3261                 a.Add ($3);
3262                 $$ = a;
3263           }
3264         ;
3265
3266 member_initializer
3267         : IDENTIFIER ASSIGN initializer_value
3268           {
3269                 LocatedToken lt = $1 as LocatedToken;
3270                 $$ = new ElementInitializer (lt.Value, (Expression)$3, lt.Location);
3271           }
3272         | non_assignment_expression
3273           {
3274                 $$ = new CollectionElementInitializer ((Expression)$1);
3275           }
3276         | OPEN_BRACE expression_list CLOSE_BRACE
3277           {
3278                 $$ = new CollectionElementInitializer ((ArrayList)$2, GetLocation ($1));
3279           }
3280         | OPEN_BRACE CLOSE_BRACE
3281           {
3282                 Report.Error (1920, GetLocation ($1), "An element initializer cannot be empty");
3283           }       
3284         ;
3285
3286 initializer_value
3287         : expression
3288         | object_or_collection_initializer
3289         ;
3290
3291 opt_argument_list
3292         : /* empty */           { $$ = null; }
3293         | argument_list
3294         ;
3295
3296 argument_list
3297         : argument
3298           { 
3299                 ArrayList list = new ArrayList (4);
3300                 list.Add ($1);
3301                 $$ = list;
3302           }
3303         | argument_list COMMA argument
3304           {
3305                 ArrayList list = (ArrayList) $1;
3306                 list.Add ($3);
3307                 $$ = list;
3308           }
3309         | argument_list error {
3310                 CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
3311                 $$ = null;
3312           }
3313         ;
3314
3315 argument
3316         : expression
3317           {
3318                 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
3319           }
3320         | non_simple_argument
3321           {
3322                 $$ = $1;
3323           }
3324         ;
3325
3326 non_simple_argument
3327         : REF variable_reference 
3328           { 
3329                 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
3330           }
3331         | OUT variable_reference 
3332           { 
3333                 $$ = new Argument ((Expression) $2, Argument.AType.Out);
3334           }
3335         | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
3336           {
3337                 ArrayList list = (ArrayList) $3;
3338                 Argument[] args = new Argument [list.Count];
3339                 list.CopyTo (args, 0);
3340
3341                 Expression expr = new Arglist (args, (Location) $1);
3342                 $$ = new Argument (expr, Argument.AType.Expression);
3343           }
3344         | ARGLIST OPEN_PARENS CLOSE_PARENS
3345           {
3346                 $$ = new Argument (new Arglist ((Location) $1), Argument.AType.Expression);
3347           }       
3348         | ARGLIST
3349           {
3350                 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
3351           }
3352         ;
3353
3354 variable_reference
3355         : expression { note ("section 5.4"); $$ = $1; }
3356         ;
3357
3358 element_access
3359         : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET 
3360           {
3361                 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
3362           }
3363         | primary_expression rank_specifiers
3364           {
3365                 // So the super-trick is that primary_expression
3366                 // can only be either a SimpleName or a MemberAccess. 
3367                 // The MemberAccess case arises when you have a fully qualified type-name like :
3368                 // Foo.Bar.Blah i;
3369                 // SimpleName is when you have
3370                 // Blah i;
3371                   
3372                 Expression expr = (Expression) $1;  
3373                 if (expr is ComposedCast){
3374                         $$ = new ComposedCast (expr, (string) $2);
3375                 } else if (!(expr is SimpleName || expr is MemberAccess || expr is ConstructedType || expr is QualifiedAliasMember)){
3376                         Error_ExpectingTypeName (expr);
3377                         $$ = TypeManager.system_object_expr;
3378                 } else {
3379                         //
3380                         // So we extract the string corresponding to the SimpleName
3381                         // or MemberAccess
3382                         // 
3383                         $$ = new ComposedCast (expr, (string) $2);
3384                 }
3385                 current_array_type = (Expression)$$;
3386           }
3387         ;
3388
3389 expression_list
3390         : expression
3391           {
3392                 ArrayList list = new ArrayList (4);
3393                 list.Add ($1);
3394                 $$ = list;
3395           }
3396         | expression_list COMMA expression
3397           {
3398                 ArrayList list = (ArrayList) $1;
3399                 list.Add ($3);
3400                 $$ = list;
3401           }
3402         ;
3403
3404 this_access
3405         : THIS
3406           {
3407                 $$ = new This (current_block, (Location) $1);
3408           }
3409         ;
3410
3411 base_access
3412         : BASE DOT IDENTIFIER opt_type_argument_list
3413           {
3414                 LocatedToken lt = (LocatedToken) $3;
3415                 $$ = new BaseAccess (lt.Value, (TypeArguments) $4, lt.Location);
3416           }
3417         | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
3418           {
3419                 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
3420           }
3421         | BASE error {
3422                 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
3423                 $$ = null;
3424           }
3425         ;
3426
3427 post_increment_expression
3428         : primary_expression OP_INC
3429           {
3430                 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
3431                                        (Expression) $1, (Location) $2);
3432           }
3433         ;
3434
3435 post_decrement_expression
3436         : primary_expression OP_DEC
3437           {
3438                 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
3439                                        (Expression) $1, (Location) $2);
3440           }
3441         ;
3442
3443 new_expression
3444         : object_or_delegate_creation_expression
3445         | array_creation_expression
3446         | anonymous_type_expression
3447         ;
3448
3449 object_or_delegate_creation_expression
3450         : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS opt_object_or_collection_initializer
3451           {
3452                 if ($6 != null) {
3453                         if (RootContext.Version <= LanguageVersion.ISO_2)
3454                                 Report.FeatureIsNotISO1 (GetLocation ($1), "object initializers");
3455                                 
3456                         $$ = new NewInitialize ((Expression) $2, (ArrayList) $4, (CollectionOrObjectInitializers) $6, (Location) $1);
3457                 }
3458                 else
3459                         $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
3460           }
3461         | NEW type object_or_collection_initializer
3462           {
3463                 if (RootContext.Version <= LanguageVersion.ISO_2)
3464                         Report.FeatureIsNotISO1 (GetLocation ($1), "collection initializers");
3465           
3466                 $$ = new NewInitialize ((Expression) $2, null, (CollectionOrObjectInitializers) $3, (Location) $1);
3467           }
3468         ;
3469
3470 array_creation_expression
3471         : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET 
3472           opt_rank_specifier
3473           opt_array_initializer
3474           {
3475                 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
3476           }
3477         | NEW type rank_specifiers array_initializer
3478           {
3479                 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, (Location) $1);
3480           }
3481         | NEW rank_specifiers array_initializer
3482           {
3483                 $$ = new ImplicitlyTypedArrayCreation ((string) $2, (ArrayList) $3, (Location) $1);
3484           }
3485         | NEW error
3486           {
3487                 Report.Error (1031, (Location) $1, "Type expected");
3488                 $$ = null;
3489           }          
3490         | NEW type error 
3491           {
3492                 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
3493                 $$ = null;
3494           }
3495         ;
3496
3497 anonymous_type_expression
3498         : NEW OPEN_BRACE anonymous_type_parameters CLOSE_BRACE
3499           {
3500                 if (RootContext.Version <= LanguageVersion.ISO_2)
3501                         Report.FeatureIsNotISO1 (GetLocation ($1), "anonymous types");
3502
3503                 $$ = new AnonymousTypeDeclaration ((ArrayList) $3, current_container, GetLocation ($1));
3504           }
3505         ;
3506
3507 anonymous_type_parameters
3508         : { $$ = null; }
3509         | anonymous_type_parameter
3510           {
3511                 ArrayList a = new ArrayList (4);
3512                 a.Add ($1);
3513                 $$ = a;
3514           }
3515         | anonymous_type_parameters COMMA anonymous_type_parameter
3516           {
3517                 ArrayList a = (ArrayList) $1;
3518                 a.Add ($3);
3519                 $$ = a;
3520           }
3521         ;
3522
3523 anonymous_type_parameter
3524         : IDENTIFIER ASSIGN variable_initializer
3525           {
3526                 LocatedToken lt = (LocatedToken)$1;
3527                 $$ = new AnonymousTypeParameter ((Expression)$3, lt.Value, lt.Location);
3528           }
3529         | IDENTIFIER
3530           {
3531                 LocatedToken lt = (LocatedToken)$1;
3532                 $$ = new AnonymousTypeParameter (new SimpleName (lt.Value, lt.Location),
3533                         lt.Value, lt.Location);
3534           }
3535         | member_access
3536           {
3537                 MemberAccess ma = (MemberAccess) $1;
3538                 $$ = new AnonymousTypeParameter (ma, ma.Identifier, ma.Location);
3539           }
3540         | error
3541           {
3542                 Report.Error (746, lexer.Location, "Invalid anonymous type member declarator. " +
3543                 "Anonymous type members must be a member assignment, simple name or member access expression");
3544           }
3545         ;
3546
3547 opt_rank_specifier
3548         : /* empty */
3549           {
3550                   $$ = "";
3551           }
3552         | rank_specifiers
3553           {
3554                         $$ = $1;
3555           }
3556         ;
3557
3558 opt_rank_specifier_or_nullable
3559         : /* empty */
3560           {
3561                 $$ = "";
3562           }
3563         | INTERR
3564           {
3565                 $$ = "?";
3566           }
3567         | opt_nullable rank_specifiers
3568           {
3569                 if ((bool) $1)
3570                         $$ = "?" + $2;
3571                 else
3572                         $$ = $2;
3573           }
3574         | opt_nullable rank_specifiers INTERR
3575           {
3576                 if ((bool) $1)
3577                         $$ = "?" + $2 + "?";
3578                 else
3579                         $$ = $2 + "?";
3580           }
3581         ;
3582
3583 rank_specifiers
3584         : rank_specifier opt_rank_specifier
3585           {
3586                   $$ = (string) $2 + (string) $1;
3587           }
3588         ;
3589
3590 rank_specifier
3591         : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3592           {
3593                 $$ = "[" + (string) $2 + "]";
3594           }
3595         ;
3596
3597 opt_dim_separators
3598         : /* empty */
3599           {
3600                 $$ = "";
3601           }
3602         | dim_separators
3603           {
3604                   $$ = $1;
3605           }               
3606         ;
3607
3608 dim_separators
3609         : COMMA
3610           {
3611                 $$ = ",";
3612           }
3613         | dim_separators COMMA
3614           {
3615                 $$ = (string) $1 + ",";
3616           }
3617         ;
3618
3619 opt_array_initializer
3620         : /* empty */
3621           {
3622                 $$ = null;
3623           }
3624         | array_initializer
3625           {
3626                 $$ = $1;
3627           }
3628         ;
3629
3630 array_initializer
3631         : OPEN_BRACE CLOSE_BRACE
3632           {
3633                 ArrayList list = new ArrayList (4);
3634                 $$ = list;
3635           }
3636         | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3637           {
3638                 $$ = (ArrayList) $2;
3639           }
3640         ;
3641
3642 variable_initializer_list
3643         : variable_initializer
3644           {
3645                 ArrayList list = new ArrayList (4);
3646                 list.Add ($1);
3647                 $$ = list;
3648           }
3649         | variable_initializer_list COMMA variable_initializer
3650           {
3651                 ArrayList list = (ArrayList) $1;
3652                 list.Add ($3);
3653                 $$ = list;
3654           }
3655         ;
3656
3657 typeof_expression
3658         : TYPEOF
3659       {
3660                 pushed_current_array_type = current_array_type;
3661                 lexer.TypeOfParsing = true;
3662           }
3663           OPEN_PARENS typeof_type_expression CLOSE_PARENS
3664           {
3665                 lexer.TypeOfParsing = false;
3666                 Expression type = (Expression)$4;
3667                 if (type == TypeManager.system_void_expr)
3668                         $$ = new TypeOfVoid ((Location) $1);
3669                 else
3670                         $$ = new TypeOf (type, (Location) $1);
3671                 current_array_type = pushed_current_array_type;
3672           }
3673         ;
3674         
3675 typeof_type_expression
3676         : type
3677           {
3678                 $$ = $1;
3679           }
3680         | unbound_type_name
3681           {
3682                 $$ = new UnboundTypeExpression ((MemberName)$1, lexer.Location);
3683           }
3684         ;
3685         
3686 unbound_type_name
3687         : IDENTIFIER GENERIC_DIMENSION
3688           {
3689                 if (RootContext.Version == LanguageVersion.ISO_1)
3690                         Report.FeatureIsNotISO1 (lexer.Location, "generics");
3691           
3692                 LocatedToken lt = (LocatedToken) $1;
3693                 TypeArguments ta = new TypeArguments ((int)$2, lt.Location);
3694
3695                 $$ = new MemberName (lt.Value, ta, lt.Location);
3696           }
3697         | IDENTIFIER DOUBLE_COLON IDENTIFIER GENERIC_DIMENSION
3698           {
3699                 LocatedToken lt = (LocatedToken) $1;
3700                 MemberName left = new MemberName (lt.Value, lt.Location);
3701                 lt = (LocatedToken) $3;
3702                 TypeArguments ta = new TypeArguments ((int)$4, lt.Location);
3703                 
3704                 $$ = new MemberName (left, lt.Value, ta, lt.Location);
3705           }
3706         | unbound_type_name DOT IDENTIFIER GENERIC_DIMENSION
3707           {
3708                 LocatedToken lt = (LocatedToken) $3;
3709                 TypeArguments ta = new TypeArguments ((int)$4, lt.Location);
3710                 
3711                 $$ = new MemberName ((MemberName)$1, lt.Value, ta, lt.Location);
3712           }
3713         | namespace_or_type_name DOT IDENTIFIER GENERIC_DIMENSION
3714           {
3715                 LocatedToken lt = (LocatedToken) $3;
3716                 TypeArguments ta = new TypeArguments ((int)$4, lt.Location);
3717                 
3718                 $$ = new MemberName ((MemberName)$1, lt.Value, ta, lt.Location);
3719           }
3720         ;
3721
3722
3723 sizeof_expression
3724         : SIZEOF OPEN_PARENS type CLOSE_PARENS { 
3725                 $$ = new SizeOf ((Expression) $3, (Location) $1);
3726           }
3727         ;
3728
3729 checked_expression
3730         : CHECKED OPEN_PARENS expression CLOSE_PARENS
3731           {
3732                 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3733           }
3734         ;
3735
3736 unchecked_expression
3737         : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3738           {
3739                 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3740           }
3741         ;
3742
3743 pointer_member_access 
3744         : primary_expression OP_PTR IDENTIFIER
3745           {
3746                 Expression deref;
3747                 LocatedToken lt = (LocatedToken) $3;
3748
3749                 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
3750                 $$ = new MemberAccess (deref, lt.Value);
3751           }
3752         ;
3753
3754 anonymous_method_expression
3755         : DELEGATE opt_anonymous_method_signature
3756           {
3757                 start_anonymous (false, (Parameters) $2, (Location) $1);
3758           }
3759           block
3760           {
3761                 $$ = end_anonymous ((ToplevelBlock) $4, (Location) $1);
3762         }
3763         ;
3764
3765 opt_anonymous_method_signature
3766         : /* empty */                   { $$ = null; } 
3767         | anonymous_method_signature
3768         ;
3769
3770 anonymous_method_signature
3771         : open_parens opt_anonymous_method_parameter_list CLOSE_PARENS 
3772           {
3773                 if ($2 == null)
3774                         $$ = Parameters.EmptyReadOnlyParameters;
3775                 else {
3776                         ArrayList par_list = (ArrayList) $2;
3777                         Parameter [] pars = new Parameter [par_list.Count];
3778                         par_list.CopyTo (pars);
3779                         $$ = new Parameters (pars);
3780                 }
3781           }
3782         ;
3783
3784 opt_anonymous_method_parameter_list
3785         : /* empty */                      { $$ = null; } 
3786         | anonymous_method_parameter_list  { $$ = $1; }
3787         ;
3788
3789 anonymous_method_parameter_list
3790         : anonymous_method_parameter 
3791           {
3792                 ArrayList a = new ArrayList (4);
3793                 a.Add ($1);
3794                 $$ = a;
3795           }
3796         | anonymous_method_parameter_list COMMA anonymous_method_parameter 
3797           {
3798                 ArrayList a = (ArrayList) $1;
3799                 a.Add ($3);
3800                 $$ = a;
3801           }
3802         ; 
3803
3804 anonymous_method_parameter
3805         : opt_parameter_modifier type IDENTIFIER {
3806                 LocatedToken lt = (LocatedToken) $3;
3807                 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3808           }
3809         | PARAMS type IDENTIFIER {
3810                 Report.Error (1670, ((LocatedToken) $3).Location, "The `params' modifier is not allowed in anonymous method declaration");
3811                 $$ = null;
3812           }
3813         ;
3814
3815 default_value_expression
3816         : DEFAULT_OPEN_PARENS type CLOSE_PARENS
3817           {
3818                 $$ = new DefaultValueExpression ((Expression) $2, lexer.Location);
3819           }
3820         ;
3821
3822 unary_expression
3823         : primary_expression
3824         | BANG prefixed_unary_expression
3825           {
3826                 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3827           }
3828         | TILDE prefixed_unary_expression
3829           {
3830                 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3831           }
3832         | cast_expression
3833         ;
3834
3835 cast_list
3836         : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3837           {
3838                 $$ = new Cast ((Expression) $1, (Expression) $3);
3839           }
3840         | parenthesized_expression_0 CLOSE_PARENS_NO_CAST default_value_expression
3841           {
3842                 $$ = new Cast ((Expression) $1, (Expression) $3);
3843           }
3844         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3845           {
3846                 $$ = new Cast ((Expression) $1, (Expression) $3);
3847           }     
3848         ;
3849
3850 cast_expression
3851         : cast_list
3852         | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3853           {
3854                 // TODO: wrong location
3855                 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3856           }
3857         ;
3858
3859         //
3860         // The idea to split this out is from Rhys' grammar
3861         // to solve the problem with casts.
3862         //
3863 prefixed_unary_expression
3864         : unary_expression
3865         | PLUS prefixed_unary_expression
3866           { 
3867                 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3868           } 
3869         | MINUS prefixed_unary_expression 
3870           { 
3871                 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3872           }
3873         | OP_INC prefixed_unary_expression 
3874           {
3875                 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3876                                        (Expression) $2, (Location) $1);
3877           }
3878         | OP_DEC prefixed_unary_expression 
3879           {
3880                 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3881                                        (Expression) $2, (Location) $1);
3882           }
3883         | STAR prefixed_unary_expression
3884           {
3885                 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, (Location) $1);
3886           }
3887         | BITWISE_AND prefixed_unary_expression
3888           {
3889                 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3890           }
3891         ;
3892
3893 pre_increment_expression
3894         : OP_INC prefixed_unary_expression 
3895           {
3896                 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3897                                        (Expression) $2, (Location) $1);
3898           }
3899         ;
3900
3901 pre_decrement_expression
3902         : OP_DEC prefixed_unary_expression 
3903           {
3904                 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3905                                        (Expression) $2, (Location) $1);
3906           }
3907         ;
3908
3909 multiplicative_expression
3910         : prefixed_unary_expression
3911         | multiplicative_expression STAR prefixed_unary_expression
3912           {
3913                 $$ = new Binary (Binary.Operator.Multiply, 
3914                                  (Expression) $1, (Expression) $3);
3915           }
3916         | multiplicative_expression DIV prefixed_unary_expression
3917           {
3918                 $$ = new Binary (Binary.Operator.Division, 
3919                                  (Expression) $1, (Expression) $3);
3920           }
3921         | multiplicative_expression PERCENT prefixed_unary_expression 
3922           {
3923                 $$ = new Binary (Binary.Operator.Modulus, 
3924                                  (Expression) $1, (Expression) $3);
3925           }
3926         ;
3927
3928 additive_expression
3929         : multiplicative_expression
3930         | additive_expression PLUS multiplicative_expression 
3931           {
3932                 $$ = new Binary (Binary.Operator.Addition, 
3933                                  (Expression) $1, (Expression) $3);
3934           }
3935         | additive_expression MINUS multiplicative_expression
3936           {
3937                 $$ = new Binary (Binary.Operator.Subtraction, 
3938                                  (Expression) $1, (Expression) $3);
3939           }
3940         ;
3941
3942 shift_expression
3943         : additive_expression
3944         | shift_expression OP_SHIFT_LEFT additive_expression
3945           {
3946                 $$ = new Binary (Binary.Operator.LeftShift, 
3947                                  (Expression) $1, (Expression) $3);
3948           }
3949         | shift_expression OP_SHIFT_RIGHT additive_expression
3950           {
3951                 $$ = new Binary (Binary.Operator.RightShift, 
3952                                  (Expression) $1, (Expression) $3);
3953           }
3954         ; 
3955
3956 opt_error
3957         : /* empty */
3958           {
3959                 $$ = false;
3960           }
3961         | error
3962           {
3963                 lexer.PutbackNullable ();
3964                 $$ = true;
3965           }
3966         ;
3967
3968 nullable_type_or_conditional
3969         : type opt_error
3970           {
3971                 if (((bool) $2) && ($1 is ComposedCast))
3972                         $$ = ((ComposedCast) $1).RemoveNullable ();
3973                 else
3974                         $$ = $1;
3975           }
3976         ;
3977
3978 relational_expression
3979         : shift_expression
3980         | relational_expression OP_LT shift_expression
3981           {
3982                 $$ = new Binary (Binary.Operator.LessThan, 
3983                                  (Expression) $1, (Expression) $3);
3984           }
3985         | relational_expression OP_GT shift_expression
3986           {
3987                 $$ = new Binary (Binary.Operator.GreaterThan, 
3988                                  (Expression) $1, (Expression) $3);
3989           }
3990         | relational_expression OP_LE shift_expression
3991           {
3992                 $$ = new Binary (Binary.Operator.LessThanOrEqual, 
3993                                  (Expression) $1, (Expression) $3);
3994           }
3995         | relational_expression OP_GE shift_expression
3996           {
3997                 $$ = new Binary (Binary.Operator.GreaterThanOrEqual, 
3998                                  (Expression) $1, (Expression) $3);
3999           }
4000         | relational_expression IS
4001           {
4002                 yyErrorFlag = 3;
4003           } nullable_type_or_conditional
4004           {
4005                 $$ = new Is ((Expression) $1, (Expression) $4, (Location) $2);
4006           }
4007         | relational_expression AS
4008           {
4009                 yyErrorFlag = 3;
4010           } nullable_type_or_conditional
4011           {
4012                 $$ = new As ((Expression) $1, (Expression) $4, (Location) $2);
4013           }
4014         ;
4015
4016 equality_expression
4017         : relational_expression
4018         | equality_expression OP_EQ relational_expression
4019           {
4020                 $$ = new Binary (Binary.Operator.Equality, 
4021                                  (Expression) $1, (Expression) $3);
4022           }
4023         | equality_expression OP_NE relational_expression
4024           {
4025                 $$ = new Binary (Binary.Operator.Inequality, 
4026                                  (Expression) $1, (Expression) $3);
4027           }
4028         ; 
4029
4030 and_expression
4031         : equality_expression
4032         | and_expression BITWISE_AND equality_expression
4033           {
4034                 $$ = new Binary (Binary.Operator.BitwiseAnd, 
4035                                  (Expression) $1, (Expression) $3);
4036           }
4037         ;
4038
4039 exclusive_or_expression
4040         : and_expression
4041         | exclusive_or_expression CARRET and_expression
4042           {
4043                 $$ = new Binary (Binary.Operator.ExclusiveOr, 
4044                                  (Expression) $1, (Expression) $3);
4045           }
4046         ;
4047
4048 inclusive_or_expression
4049         : exclusive_or_expression
4050         | inclusive_or_expression BITWISE_OR exclusive_or_expression
4051           {
4052                 $$ = new Binary (Binary.Operator.BitwiseOr, 
4053                                  (Expression) $1, (Expression) $3);
4054           }
4055         ;
4056
4057 conditional_and_expression
4058         : inclusive_or_expression
4059         | conditional_and_expression OP_AND inclusive_or_expression
4060           {
4061                 $$ = new Binary (Binary.Operator.LogicalAnd, 
4062                                  (Expression) $1, (Expression) $3);
4063           }
4064         ;
4065
4066 conditional_or_expression
4067         : conditional_and_expression
4068         | conditional_or_expression OP_OR conditional_and_expression
4069           {
4070                 $$ = new Binary (Binary.Operator.LogicalOr, 
4071                                  (Expression) $1, (Expression) $3);
4072           }
4073         ;
4074
4075 conditional_expression
4076         : conditional_or_expression
4077         | conditional_or_expression INTERR expression COLON expression 
4078           {
4079                 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
4080           }
4081         | conditional_or_expression OP_COALESCING expression
4082           {
4083                 $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $3, lexer.Location);
4084           }
4085         // We'll be resolved into a `parenthesized_expression_0' later on.
4086         | conditional_or_expression INTERR CLOSE_PARENS
4087           {
4088                 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
4089                 lexer.PutbackCloseParens ();
4090           }
4091         ;
4092
4093 assignment_expression
4094         : prefixed_unary_expression ASSIGN expression
4095           {
4096                 $$ = new Assign ((Expression) $1, (Expression) $3);
4097           }
4098         | prefixed_unary_expression OP_MULT_ASSIGN expression
4099           {
4100                 $$ = new CompoundAssign (
4101                         Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
4102           }
4103         | prefixed_unary_expression OP_DIV_ASSIGN expression
4104           {
4105                 $$ = new CompoundAssign (
4106                         Binary.Operator.Division, (Expression) $1, (Expression) $3);
4107           }
4108         | prefixed_unary_expression OP_MOD_ASSIGN expression
4109           {
4110                 $$ = new CompoundAssign (
4111                         Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
4112           }
4113         | prefixed_unary_expression OP_ADD_ASSIGN expression
4114           {
4115                 $$ = new CompoundAssign (
4116                         Binary.Operator.Addition, (Expression) $1, (Expression) $3);
4117           }
4118         | prefixed_unary_expression OP_SUB_ASSIGN expression
4119           {
4120                 $$ = new CompoundAssign (
4121                         Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
4122           }
4123         | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
4124           {
4125                 $$ = new CompoundAssign (
4126                         Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
4127           }
4128         | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
4129           {
4130                 $$ = new CompoundAssign (
4131                         Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
4132           }
4133         | prefixed_unary_expression OP_AND_ASSIGN expression
4134           {
4135                 $$ = new CompoundAssign (
4136                         Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
4137           }
4138         | prefixed_unary_expression OP_OR_ASSIGN expression
4139           {
4140                 $$ = new CompoundAssign (
4141                         Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
4142           }
4143         | prefixed_unary_expression OP_XOR_ASSIGN expression
4144           {
4145                 $$ = new CompoundAssign (
4146                         Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
4147           }
4148         ;
4149
4150 lambda_parameter_list
4151         : lambda_parameter
4152           {
4153                 ArrayList pars = new ArrayList (4);
4154                 pars.Add ($1);
4155
4156                 $$ = pars;
4157           }
4158         | lambda_parameter_list COMMA lambda_parameter
4159           {
4160                 ArrayList pars = (ArrayList) $1;
4161                 Parameter p = (Parameter)$3;
4162                 if (pars[0].GetType () != p.GetType ()) {
4163                         Report.Error (748, p.Location, "All lambda parameters must be typed either explicitly or implicitly");
4164                 }
4165                 
4166                 pars.Add (p);
4167                 $$ = pars;
4168           }
4169         ;
4170
4171 lambda_parameter
4172         : parameter_modifier type IDENTIFIER
4173           {
4174                 LocatedToken lt = (LocatedToken) $3;
4175
4176                 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
4177           }
4178         | type IDENTIFIER
4179           {
4180                 LocatedToken lt = (LocatedToken) $2;
4181
4182                 $$ = new Parameter ((Expression) $1, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
4183           }
4184         | IDENTIFIER
4185           {
4186                 LocatedToken lt = (LocatedToken) $1;
4187                 $$ = new ImplicitLambdaParameter (lt.Value, lt.Location);
4188           }
4189         ;
4190
4191 opt_lambda_parameter_list
4192         : /* empty */                   { $$ = Parameters.EmptyReadOnlyParameters; }
4193         | lambda_parameter_list         { 
4194                 ArrayList pars_list = (ArrayList) $1;
4195                 $$ = new Parameters ((Parameter[])pars_list.ToArray (typeof (Parameter)));
4196           }
4197         ;
4198
4199 lambda_expression_body
4200         : {
4201                 start_block (lexer.Location);
4202           }
4203           expression 
4204           {
4205                 Block b = end_block (lexer.Location);
4206                 b.AddStatement (new ContextualReturn ((Expression) $2));
4207                 $$ = b;
4208           } 
4209         | block { 
4210                 $$ = $1; 
4211           } 
4212         ;
4213
4214 lambda_expression
4215         : IDENTIFIER ARROW 
4216           {
4217                 LocatedToken lt = (LocatedToken) $1;
4218                 Parameter p = new ImplicitLambdaParameter (lt.Value, lt.Location);
4219                 start_anonymous (true, new Parameters (p), (Location) $2);
4220           }
4221           lambda_expression_body
4222           {
4223                 $$ = end_anonymous ((ToplevelBlock) $4, (Location) $2);
4224           }
4225         | OPEN_PARENS_LAMBDA opt_lambda_parameter_list CLOSE_PARENS ARROW 
4226           {
4227                 start_anonymous (true, (Parameters) $2, (Location) $4);
4228           }
4229           lambda_expression_body 
4230           {
4231                 $$ = end_anonymous ((ToplevelBlock) $6, (Location) $4);
4232           }
4233         ;
4234
4235 expression
4236         : assignment_expression
4237         | non_assignment_expression
4238         ;
4239         
4240 non_assignment_expression
4241         : conditional_expression
4242         | lambda_expression
4243         | query_expression
4244         ;
4245
4246 constant_expression
4247         : expression
4248         ;
4249
4250 boolean_expression
4251         : expression
4252         ;
4253
4254 //
4255 // 10 classes
4256 //
4257 class_declaration
4258         : opt_attributes
4259           opt_modifiers
4260           opt_partial
4261           CLASS
4262           {
4263                 lexer.ConstraintsParsing = true;
4264           }
4265           type_name
4266           {
4267                 MemberName name = MakeName ((MemberName) $6);
4268                 push_current_class (new Class (current_namespace, current_class, name, (int) $2, (Attributes) $1), $3);
4269           }
4270           opt_class_base
4271           opt_type_parameter_constraints_clauses
4272           {
4273                 lexer.ConstraintsParsing = false;
4274
4275                 current_class.SetParameterInfo ((ArrayList) $9);
4276
4277                 if (RootContext.Documentation != null) {
4278                         current_container.DocComment = Lexer.consume_doc_comment ();
4279                         Lexer.doc_state = XmlCommentState.Allowed;
4280                 }
4281           }
4282           class_body
4283           {
4284                 if (RootContext.Documentation != null)
4285                         Lexer.doc_state = XmlCommentState.Allowed;
4286           }
4287           opt_semicolon 
4288           {
4289                 $$ = pop_current_class ();
4290           }
4291         ;       
4292
4293 opt_partial
4294         : /* empty */
4295           { $$ = null; }
4296         | PARTIAL
4297           { $$ = $1; } // location
4298         ;
4299
4300 opt_modifiers
4301         : /* empty */           { $$ = (int) 0; }
4302         | modifiers
4303         ;
4304
4305 modifiers
4306         : modifier
4307         | modifiers modifier
4308           { 
4309                 int m1 = (int) $1;
4310                 int m2 = (int) $2;
4311
4312                 if ((m1 & m2) != 0) {
4313                         Location l = lexer.Location;
4314                         Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
4315                 }
4316                 $$ = (int) (m1 | m2);
4317           }
4318         ;
4319
4320 modifier
4321         : NEW                   { $$ = Modifiers.NEW; }
4322         | PUBLIC                { $$ = Modifiers.PUBLIC; }
4323         | PROTECTED             { $$ = Modifiers.PROTECTED; }
4324         | INTERNAL              { $$ = Modifiers.INTERNAL; }
4325         | PRIVATE               { $$ = Modifiers.PRIVATE; }
4326         | ABSTRACT              { $$ = Modifiers.ABSTRACT; }
4327         | SEALED                { $$ = Modifiers.SEALED; }
4328         | STATIC                { $$ = Modifiers.STATIC; }
4329         | READONLY              { $$ = Modifiers.READONLY; }
4330         | VIRTUAL               { $$ = Modifiers.VIRTUAL; }
4331         | OVERRIDE              { $$ = Modifiers.OVERRIDE; }
4332         | EXTERN                { $$ = Modifiers.EXTERN; }
4333         | VOLATILE              { $$ = Modifiers.VOLATILE; }
4334         | UNSAFE                { $$ = Modifiers.UNSAFE; }
4335         ;
4336
4337 opt_class_base
4338         : /* empty */
4339         | class_base
4340         ;
4341
4342 class_base
4343         : COLON type_list       { current_container.AddBasesForPart (current_class, (ArrayList) $2); }
4344         ;
4345
4346 opt_type_parameter_constraints_clauses
4347         : /* empty */           { $$ = null; }
4348         | type_parameter_constraints_clauses 
4349           { $$ = $1; }
4350         ;
4351
4352 type_parameter_constraints_clauses
4353         : type_parameter_constraints_clause {
4354                 ArrayList constraints = new ArrayList (1);
4355                 constraints.Add ($1);
4356                 $$ = constraints;
4357           }
4358         | type_parameter_constraints_clauses type_parameter_constraints_clause {
4359                 ArrayList constraints = (ArrayList) $1;
4360                 Constraints new_constraint = (Constraints)$2;
4361
4362                 foreach (Constraints c in constraints) {
4363                         if (new_constraint.TypeParameter == c.TypeParameter) {
4364                                 Report.Error (409, new_constraint.Location, "A constraint clause has already been specified for type parameter `{0}'",
4365                                         new_constraint.TypeParameter);
4366                         }
4367                 }
4368
4369                 constraints.Add (new_constraint);
4370                 $$ = constraints;
4371           }
4372         ; 
4373
4374 type_parameter_constraints_clause
4375         : WHERE IDENTIFIER COLON type_parameter_constraints {
4376                 LocatedToken lt = (LocatedToken) $2;
4377                 $$ = new Constraints (lt.Value, (ArrayList) $4, lt.Location);
4378           }
4379         ; 
4380
4381 type_parameter_constraints
4382         : type_parameter_constraint {
4383                 ArrayList constraints = new ArrayList (1);
4384                 constraints.Add ($1);
4385                 $$ = constraints;
4386           }
4387         | type_parameter_constraints COMMA type_parameter_constraint {
4388                 ArrayList constraints = (ArrayList) $1;
4389
4390                 constraints.Add ($3);
4391                 $$ = constraints;
4392           }
4393         ;
4394
4395 type_parameter_constraint
4396         : type
4397         | NEW OPEN_PARENS CLOSE_PARENS {
4398                 $$ = SpecialConstraint.Constructor;
4399           }
4400         | CLASS {
4401                 $$ = SpecialConstraint.ReferenceType;
4402           }
4403         | STRUCT {
4404                 $$ = SpecialConstraint.ValueType;
4405           }
4406         ;
4407
4408 //
4409 // Statements (8.2)
4410 //
4411
4412 //
4413 // A block is "contained" on the following places:
4414 //      method_body
4415 //      property_declaration as part of the accessor body (get/set)
4416 //      operator_declaration
4417 //      constructor_declaration
4418 //      destructor_declaration
4419 //      event_declaration as part of add_accessor_declaration or remove_accessor_declaration
4420 //      
4421 block
4422         : OPEN_BRACE  
4423           {
4424                 ++lexer.parsing_block;
4425                 start_block ((Location) $1);
4426           } 
4427           opt_statement_list CLOSE_BRACE 
4428           {
4429                 --lexer.parsing_block;
4430                 $$ = end_block ((Location) $4);
4431           }
4432         ;
4433
4434 block_prepared
4435         : OPEN_BRACE
4436           {
4437                 ++lexer.parsing_block;
4438           }
4439           opt_statement_list CLOSE_BRACE 
4440           {
4441                 --lexer.parsing_block;
4442                 $$ = end_block ((Location) $4);
4443           }
4444         ;
4445
4446 opt_statement_list
4447         : /* empty */
4448         | statement_list 
4449         ;
4450
4451 statement_list
4452         : statement
4453         | statement_list statement
4454         ;
4455
4456 statement
4457         : declaration_statement
4458           {
4459                 if ($1 != null && (Block) $1 != current_block){
4460                         current_block.AddStatement ((Statement) $1);
4461                         current_block = (Block) $1;
4462                 }
4463           }
4464         | valid_declaration_statement
4465           {
4466                 current_block.AddStatement ((Statement) $1);
4467           }
4468         | labeled_statement
4469         ;
4470
4471 valid_declaration_statement
4472         : block
4473         | empty_statement
4474         | expression_statement
4475         | selection_statement
4476         | iteration_statement
4477         | jump_statement                  
4478         | try_statement
4479         | checked_statement
4480         | unchecked_statement
4481         | lock_statement
4482         | using_statement
4483         | unsafe_statement
4484         | fixed_statement
4485         ;
4486
4487 embedded_statement
4488         : valid_declaration_statement
4489         | declaration_statement
4490           {
4491                   Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4492                   $$ = null;
4493           }
4494         | labeled_statement
4495           {
4496                   Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4497                   $$ = null;
4498           }
4499         ;
4500
4501 empty_statement
4502         : SEMICOLON
4503           {
4504                   $$ = EmptyStatement.Value;
4505           }
4506         ;
4507
4508 labeled_statement
4509         : IDENTIFIER COLON 
4510           {
4511                 LocatedToken lt = (LocatedToken) $1;
4512                 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
4513
4514                 if (current_block.AddLabel (labeled))
4515                         current_block.AddStatement (labeled);
4516           }
4517           statement
4518         ;
4519
4520 declaration_statement
4521         : local_variable_declaration SEMICOLON
4522           {
4523                 current_array_type = null;
4524                 if ($1 != null){
4525                         DictionaryEntry de = (DictionaryEntry) $1;
4526                         Expression e = (Expression) de.Key;
4527
4528                         $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
4529                 }
4530           }
4531
4532         | local_constant_declaration SEMICOLON
4533           {
4534                 current_array_type = null;
4535                 if ($1 != null){
4536                         DictionaryEntry de = (DictionaryEntry) $1;
4537
4538                         $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
4539                 }
4540           }
4541         ;
4542
4543 /* 
4544  * The following is from Rhys' grammar:
4545  * > Types in local variable declarations must be recognized as 
4546  * > expressions to prevent reduce/reduce errors in the grammar.
4547  * > The expressions are converted into types during semantic analysis.
4548  */
4549 local_variable_type
4550         : primary_expression opt_rank_specifier_or_nullable
4551           { 
4552                 // FIXME: Do something smart here regarding the composition of the type.
4553
4554                 // Ok, the above "primary_expression" is there to get rid of
4555                 // both reduce/reduce and shift/reduces in the grammar, it should
4556                 // really just be "type_name".  If you use type_name, a reduce/reduce
4557                 // creeps up.  If you use namespace_or_type_name (which is all we need
4558                 // really) two shift/reduces appear.
4559                 // 
4560
4561                 // So the super-trick is that primary_expression
4562                 // can only be either a SimpleName or a MemberAccess. 
4563                 // The MemberAccess case arises when you have a fully qualified type-name like :
4564                 // Foo.Bar.Blah i;
4565                 // SimpleName is when you have
4566                 // Blah i;
4567                   
4568                 Expression expr = (Expression) $1;  
4569                 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType || expr is QualifiedAliasMember)) {
4570                         Error_ExpectingTypeName (expr);
4571                         $$ = null;
4572                 } else {
4573                         //
4574                         // So we extract the string corresponding to the SimpleName
4575                         // or MemberAccess
4576                         // 
4577
4578                         if ((string) $2 == "")
4579                                 $$ = $1;
4580                         else
4581                                 $$ = new ComposedCast ((Expression) $1, (string) $2);
4582                 }
4583           }
4584         | builtin_types opt_rank_specifier_or_nullable
4585           {
4586                 if ((string) $2 == "")
4587                         $$ = $1;
4588                 else
4589                         $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
4590           }
4591         ;
4592
4593 local_variable_pointer_type
4594         : primary_expression STAR
4595           {
4596                 Expression expr = (Expression) $1;  
4597
4598                 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType || expr is QualifiedAliasMember)) {
4599                         Error_ExpectingTypeName (expr);
4600
4601                         $$ = null;
4602                 } else 
4603                         $$ = new ComposedCast ((Expression) $1, "*");
4604           }
4605         | builtin_types STAR
4606           {
4607                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
4608           }
4609         | VOID STAR
4610           {
4611                 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
4612           }
4613         | local_variable_pointer_type STAR
4614           {
4615                 $$ = new ComposedCast ((Expression) $1, "*");
4616           }
4617         ;
4618
4619 local_variable_declaration
4620         : local_variable_type variable_declarators
4621           {
4622                 if ($1 != null) {
4623                         VarExpr ve = $1 as VarExpr;
4624                         if (ve != null)
4625                                 ve.VariableInitializer = (ArrayList)$2;
4626                                 
4627                         $$ = new DictionaryEntry ($1, $2);
4628                 } else
4629                         $$ = null;
4630           }
4631         | local_variable_pointer_type opt_rank_specifier_or_nullable variable_declarators
4632           {
4633                 if ($1 != null){
4634                         Expression t;
4635
4636                         if ((string) $2 == "")
4637                                 t = (Expression) $1;
4638                         else
4639                                 t = new ComposedCast ((Expression) $1, (string) $2);
4640                         $$ = new DictionaryEntry (t, $3);
4641                 } else 
4642                         $$ = null;
4643           }
4644         ;
4645
4646 local_constant_declaration
4647         : CONST local_variable_type constant_declarators
4648           {
4649                 if ($2 != null)
4650                         $$ = new DictionaryEntry ($2, $3);
4651                 else
4652                         $$ = null;
4653           }
4654         ;
4655
4656 expression_statement
4657         : statement_expression SEMICOLON { $$ = $1; }
4658         ;
4659
4660         //
4661         // We have to do the wrapping here and not in the case above,
4662         // because statement_expression is used for example in for_statement
4663         //
4664 statement_expression
4665         : expression
4666           {
4667                 Expression expr = (Expression) $1;
4668                 ExpressionStatement s = expr as ExpressionStatement;
4669                 if (s == null) {
4670                         Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
4671                         $$ = null;
4672                 }
4673                 $$ = new StatementExpression (s);
4674           }
4675         | error
4676           {
4677                 Report.Error (1002, GetLocation ($1), "Expecting `;'");
4678                 $$ = null;
4679           }
4680         ;
4681
4682 object_creation_expression
4683         : object_or_delegate_creation_expression
4684           { note ("complain if this is a delegate maybe?"); } 
4685         ;
4686
4687 selection_statement
4688         : if_statement
4689         | switch_statement
4690         ; 
4691
4692 if_statement
4693         : IF OPEN_PARENS boolean_expression CLOSE_PARENS 
4694           embedded_statement
4695           { 
4696                 Location l = (Location) $1;
4697
4698                 $$ = new If ((Expression) $3, (Statement) $5, l);
4699
4700                 // FIXME: location for warning should be loc property of $5.
4701                 if ($5 == EmptyStatement.Value)
4702                         Report.Warning (642, 3, l, "Possible mistaken empty statement");
4703
4704           }
4705         | IF OPEN_PARENS boolean_expression CLOSE_PARENS
4706           embedded_statement ELSE embedded_statement
4707           {
4708                 Location l = (Location) $1;
4709
4710                 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
4711
4712                 // FIXME: location for warning should be loc property of $5 and $7.
4713                 if ($5 == EmptyStatement.Value)
4714                         Report.Warning (642, 3, l, "Possible mistaken empty statement");
4715                 if ($7 == EmptyStatement.Value)
4716                         Report.Warning (642, 3, l, "Possible mistaken empty statement");
4717           }
4718         ;
4719
4720 switch_statement
4721         : SWITCH OPEN_PARENS
4722           { 
4723                 if (switch_stack == null)
4724                         switch_stack = new Stack (2);
4725                 switch_stack.Push (current_block);
4726           }
4727           expression CLOSE_PARENS 
4728           switch_block
4729           {
4730                 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
4731                 current_block = (Block) switch_stack.Pop ();
4732           }
4733         ;
4734
4735 switch_block
4736         : OPEN_BRACE
4737           opt_switch_sections
4738           CLOSE_BRACE
4739           {
4740                 $$ = $2;
4741           }
4742         ;
4743
4744 opt_switch_sections
4745         : /* empty */           
4746           {
4747                 Report.Warning (1522, 1, lexer.Location, "Empty switch block"); 
4748                 $$ = new ArrayList ();
4749           }
4750         | switch_sections
4751         ;
4752
4753 switch_sections
4754         : switch_section 
4755           {
4756                 ArrayList sections = new ArrayList (4);
4757
4758                 sections.Add ($1);
4759                 $$ = sections;
4760           }
4761         | switch_sections switch_section
4762           {
4763                 ArrayList sections = (ArrayList) $1;
4764
4765                 sections.Add ($2);
4766                 $$ = sections;
4767           }
4768         ;
4769
4770 switch_section
4771         : switch_labels
4772           {
4773                 current_block = current_block.CreateSwitchBlock (lexer.Location);
4774           }
4775           statement_list 
4776           {
4777                 $$ = new SwitchSection ((ArrayList) $1, current_block.Explicit);
4778           }
4779         ;
4780
4781 switch_labels
4782         : switch_label 
4783           {
4784                 ArrayList labels = new ArrayList (4);
4785
4786                 labels.Add ($1);
4787                 $$ = labels;
4788           }
4789         | switch_labels switch_label 
4790           {
4791                 ArrayList labels = (ArrayList) ($1);
4792                 labels.Add ($2);
4793
4794                 $$ = labels;
4795           }
4796         ;
4797
4798 switch_label
4799         : CASE constant_expression COLON        { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
4800         | DEFAULT_COLON                         { $$ = new SwitchLabel (null, (Location) $1); }
4801         | error {
4802                 Report.Error (
4803                         1523, GetLocation ($1), 
4804                         "The keyword case or default must precede code in switch block");
4805           }
4806         ;
4807
4808 iteration_statement
4809         : while_statement
4810         | do_statement
4811         | for_statement
4812         | foreach_statement
4813         ;
4814
4815 while_statement
4816         : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
4817           {
4818                 Location l = (Location) $1;
4819                 $$ = new While ((Expression) $3, (Statement) $5, l);
4820           }
4821         ;
4822
4823 do_statement
4824         : DO embedded_statement 
4825           WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
4826           {
4827                 Location l = (Location) $1;
4828
4829                 $$ = new Do ((Statement) $2, (Expression) $5, l);
4830           }
4831         ;
4832
4833 for_statement
4834         : FOR open_parens 
4835           opt_for_initializer SEMICOLON
4836           {
4837                 Location l = lexer.Location;
4838                 start_block (l);  
4839                 Block assign_block = current_block;
4840
4841                 if ($3 is DictionaryEntry){
4842                         DictionaryEntry de = (DictionaryEntry) $3;
4843                         
4844                         Expression type = (Expression) de.Key;
4845                         ArrayList var_declarators = (ArrayList) de.Value;
4846
4847                         foreach (VariableDeclaration decl in var_declarators){
4848
4849                                 LocalInfo vi;
4850
4851                                 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4852                                 if (vi == null)
4853                                         continue;
4854
4855                                 Expression expr = decl.expression_or_array_initializer;
4856                                         
4857                                 LocalVariableReference var;
4858                                 var = new LocalVariableReference (assign_block, decl.identifier, l);
4859
4860                                 if (expr != null) {
4861                                         Assign a = new Assign (var, expr, decl.Location);
4862                                         
4863                                         assign_block.AddStatement (new StatementExpression (a));
4864                                 }
4865                         }
4866                         
4867                         // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4868                         // This can be referred to as $5 below.
4869                         $$ = null;
4870                 } else {
4871                         $$ = $3;
4872                 }
4873           } 
4874           opt_for_condition SEMICOLON
4875           opt_for_iterator CLOSE_PARENS 
4876           embedded_statement
4877           {
4878                 Location l = (Location) $1;
4879
4880                 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4881
4882                 current_block.AddStatement (f);
4883
4884                 $$ = end_block (lexer.Location);
4885           }
4886         ;
4887
4888 opt_for_initializer
4889         : /* empty */           { $$ = EmptyStatement.Value; }
4890         | for_initializer       
4891         ;
4892
4893 for_initializer
4894         : local_variable_declaration
4895         | statement_expression_list
4896         ;
4897
4898 opt_for_condition
4899         : /* empty */           { $$ = null; }
4900         | boolean_expression
4901         ;
4902
4903 opt_for_iterator
4904         : /* empty */           { $$ = EmptyStatement.Value; }
4905         | for_iterator
4906         ;
4907
4908 for_iterator
4909         : statement_expression_list
4910         ;
4911
4912 statement_expression_list
4913         : statement_expression  
4914           {
4915                 // CHANGE: was `null'
4916                 Statement s = (Statement) $1;
4917                 Block b = new Block (current_block, s.loc, lexer.Location);   
4918
4919                 b.AddStatement (s);
4920                 $$ = b;
4921           }
4922         | statement_expression_list COMMA statement_expression
4923           {
4924                 Block b = (Block) $1;
4925
4926                 b.AddStatement ((Statement) $3);
4927                 $$ = $1;
4928           }
4929         ;
4930
4931 foreach_statement
4932         : FOREACH open_parens type IN expression CLOSE_PARENS
4933           {
4934                 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4935                 $$ = null;
4936           }
4937         | FOREACH open_parens type IDENTIFIER IN
4938           expression CLOSE_PARENS 
4939           {
4940                 start_block (lexer.Location);
4941                 Block foreach_block = current_block;
4942
4943                 LocatedToken lt = (LocatedToken) $4;
4944                 Location l = lt.Location;
4945                 LocalInfo vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4946                 if (vi != null) {
4947                         vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4948
4949                         // Get a writable reference to this read-only variable.
4950                         //
4951                         // Note that the $$ here refers to the value of _this_ code block,
4952                         // not the value of the LHS non-terminal.  This can be referred to as $8 below.
4953                         $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
4954                 } else {
4955                         $$ = null;
4956                 }
4957           } 
4958           embedded_statement 
4959           {
4960                 LocalVariableReference v = (LocalVariableReference) $8;
4961                 Location l = (Location) $1;
4962
4963                 if (v != null) {
4964                         Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
4965                         current_block.AddStatement (f);
4966                 }
4967
4968                 $$ = end_block (lexer.Location);
4969           }
4970         ;
4971
4972 jump_statement
4973         : break_statement
4974         | continue_statement
4975         | goto_statement
4976         | return_statement
4977         | throw_statement
4978         | yield_statement
4979         ;
4980
4981 break_statement
4982         : BREAK SEMICOLON
4983           {
4984                 $$ = new Break ((Location) $1);
4985           }
4986         ;
4987
4988 continue_statement
4989         : CONTINUE SEMICOLON
4990           {
4991                 $$ = new Continue ((Location) $1);
4992           }
4993         ;
4994
4995 goto_statement
4996         : GOTO IDENTIFIER SEMICOLON 
4997           {
4998                 LocatedToken lt = (LocatedToken) $2;
4999                 $$ = new Goto (lt.Value, lt.Location);
5000           }
5001         | GOTO CASE constant_expression SEMICOLON
5002           {
5003                 $$ = new GotoCase ((Expression) $3, (Location) $1);
5004           }
5005         | GOTO DEFAULT SEMICOLON 
5006           {
5007                 $$ = new GotoDefault ((Location) $1);
5008           }
5009         ; 
5010
5011 return_statement
5012         : RETURN opt_expression SEMICOLON
5013           {
5014                 $$ = new Return ((Expression) $2, (Location) $1);
5015           }
5016         ;
5017
5018 throw_statement
5019         : THROW opt_expression SEMICOLON
5020           {
5021                 $$ = new Throw ((Expression) $2, (Location) $1);
5022           }
5023         ;
5024
5025 yield_statement 
5026         : IDENTIFIER RETURN expression SEMICOLON
5027           {
5028                 LocatedToken lt = (LocatedToken) $1;
5029                 string s = lt.Value;
5030                 if (s != "yield"){
5031                         Report.Error (1003, lt.Location, "; expected");
5032                         $$ = null;
5033                 }
5034                 if (RootContext.Version == LanguageVersion.ISO_1){
5035                         Report.FeatureIsNotISO1 (lt.Location, "yield statement");
5036                         $$ = null;
5037                 }
5038                 if (anonymous_host == null){
5039                         Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
5040                         $$ = null;
5041                 } else {
5042                         anonymous_host.SetYields ();
5043                         $$ = new Yield ((Expression) $3, lt.Location); 
5044                 }
5045           }
5046         | IDENTIFIER RETURN SEMICOLON
5047           {
5048                 Report.Error (1627, (Location) $2, "Expression expected after yield return");
5049                 $$ = null;
5050           }
5051         | IDENTIFIER BREAK SEMICOLON
5052           {
5053                 LocatedToken lt = (LocatedToken) $1;
5054                 string s = lt.Value;
5055                 if (s != "yield"){
5056                         Report.Error (1003, lt.Location, "; expected");
5057                         $$ = null;
5058                 }
5059                 if (RootContext.Version == LanguageVersion.ISO_1){
5060                         Report.FeatureIsNotISO1 (lt.Location, "yield statement");
5061                         $$ = null;
5062                 }
5063                 if (anonymous_host == null){
5064                         Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
5065                         $$ = null;
5066                 } else {
5067                         anonymous_host.SetYields ();
5068                         $$ = new YieldBreak (lt.Location);
5069                 }
5070           }
5071         ;
5072
5073 opt_expression
5074         : /* empty */
5075         | expression
5076         ;
5077
5078 try_statement
5079         : TRY block catch_clauses 
5080           {
5081                 Catch g = null;
5082                 
5083                 ArrayList c = (ArrayList)$3;
5084                 for (int i = 0; i < c.Count; ++i) {
5085                         Catch cc = (Catch) c [i];
5086                         if (cc.IsGeneral) {
5087                                 if (i != c.Count - 1)
5088                                         Report.Error (1017, cc.loc, "Try statement already has an empty catch block");
5089                                 g = cc;
5090                                 c.RemoveAt (i);
5091                                 i--;
5092                         }
5093                 }
5094
5095                 // Now s contains the list of specific catch clauses
5096                 // and g contains the general one.
5097                 
5098                 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
5099           }
5100         | TRY block opt_catch_clauses FINALLY block
5101           {
5102                 Catch g = null;
5103                 ArrayList s = new ArrayList (4);
5104                 ArrayList catch_list = (ArrayList) $3;
5105
5106                 if (catch_list != null){
5107                         foreach (Catch cc in catch_list) {
5108                                 if (cc.IsGeneral)
5109                                         g = cc;
5110                                 else
5111                                         s.Add (cc);
5112                         }
5113                 }
5114
5115                 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
5116           }
5117         | TRY block error 
5118           {
5119                 Report.Error (1524, (Location) $1, "Expected catch or finally");
5120                 $$ = null;
5121           }
5122         ;
5123
5124 opt_catch_clauses
5125         : /* empty */  { $$ = null; }
5126         | catch_clauses
5127         ;
5128
5129 catch_clauses
5130         : catch_clause 
5131           {
5132                 ArrayList l = new ArrayList (4);
5133
5134                 l.Add ($1);
5135                 $$ = l;
5136           }
5137         | catch_clauses catch_clause
5138           {
5139                 ArrayList l = (ArrayList) $1;
5140
5141                 l.Add ($2);
5142                 $$ = l;
5143           }
5144         ;
5145
5146 opt_identifier
5147         : /* empty */   { $$ = null; }
5148         | IDENTIFIER
5149         ;
5150
5151 catch_clause 
5152         : CATCH opt_catch_args 
5153           {
5154                 Expression type = null;
5155                 
5156                 if ($2 != null) {
5157                         DictionaryEntry cc = (DictionaryEntry) $2;
5158                         type = (Expression) cc.Key;
5159                         LocatedToken lt = (LocatedToken) cc.Value;
5160
5161                         if (lt != null){
5162                                 ArrayList one = new ArrayList (4);
5163
5164                                 one.Add (new VariableDeclaration (lt, null));
5165
5166                                 start_block (lexer.Location);
5167                                 current_block = declare_local_variables (type, one, lt.Location);
5168                         }
5169                 }
5170           } block {
5171                 Expression type = null;
5172                 string id = null;
5173                 Block var_block = null;
5174
5175                 if ($2 != null){
5176                         DictionaryEntry cc = (DictionaryEntry) $2;
5177                         type = (Expression) cc.Key;
5178                         LocatedToken lt = (LocatedToken) cc.Value;
5179
5180                         if (lt != null){
5181                                 id = lt.Value;
5182                                 var_block = end_block (lexer.Location);
5183                         }
5184                 }
5185
5186                 $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
5187           }
5188         ;
5189
5190 opt_catch_args
5191         : /* empty */ { $$ = null; }
5192         | catch_args
5193         ;         
5194
5195 catch_args 
5196         : open_parens type opt_identifier CLOSE_PARENS 
5197           {
5198                 $$ = new DictionaryEntry ($2, $3);
5199           }
5200         ;
5201
5202
5203 checked_statement
5204         : CHECKED block
5205           {
5206                 $$ = new Checked ((Block) $2);
5207           }
5208         ;
5209
5210 unchecked_statement
5211         : UNCHECKED block
5212           {
5213                 $$ = new Unchecked ((Block) $2);
5214           }
5215         ;
5216
5217 unsafe_statement
5218         : UNSAFE 
5219           {
5220                 RootContext.CheckUnsafeOption ((Location) $1);
5221           } block {
5222                 $$ = new Unsafe ((Block) $3);
5223           }
5224         ;
5225
5226 fixed_statement
5227         : FIXED open_parens 
5228           type fixed_pointer_declarators 
5229           CLOSE_PARENS
5230           {
5231                 ArrayList list = (ArrayList) $4;
5232                 Expression type = (Expression) $3;
5233                 Location l = (Location) $1;
5234                 int top = list.Count;
5235
5236                 if (type is VarExpr)
5237                         Report.Error (821, GetLocation ($3), "A fixed statement cannot use an implicitly typed local variable");
5238
5239                 start_block (lexer.Location);
5240
5241                 for (int i = 0; i < top; i++){
5242                         Pair p = (Pair) list [i];
5243                         LocalInfo v;
5244
5245                         v = current_block.AddVariable (type, (string) p.First, l);
5246                         if (v == null)
5247                                 continue;
5248
5249                         v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
5250                         v.Pinned = true;
5251                         p.First = v;
5252                         list [i] = p;
5253                 }
5254           }
5255           embedded_statement 
5256           {
5257                 Location l = (Location) $1;
5258
5259                 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
5260
5261                 current_block.AddStatement (f);
5262
5263                 $$ = end_block (lexer.Location);
5264           }
5265         ;
5266
5267 fixed_pointer_declarators
5268         : fixed_pointer_declarator      { 
5269                 ArrayList declarators = new ArrayList (4);
5270                 if ($1 != null)
5271                         declarators.Add ($1);
5272                 $$ = declarators;
5273           }
5274         | fixed_pointer_declarators COMMA fixed_pointer_declarator
5275           {
5276                 ArrayList declarators = (ArrayList) $1;
5277                 if ($3 != null)
5278                         declarators.Add ($3);
5279                 $$ = declarators;
5280           }
5281         ;
5282
5283 fixed_pointer_declarator
5284         : IDENTIFIER ASSIGN expression
5285           {
5286                 LocatedToken lt = (LocatedToken) $1;
5287                 // FIXME: keep location
5288                 $$ = new Pair (lt.Value, $3);
5289           }
5290         | IDENTIFIER
5291           {
5292                 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
5293                 $$ = null;
5294           }
5295         ;
5296
5297 lock_statement
5298         : LOCK OPEN_PARENS expression CLOSE_PARENS 
5299           {
5300                 //
5301           } 
5302           embedded_statement
5303           {
5304                 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
5305           }
5306         ;
5307
5308 using_statement
5309         : USING open_parens resource_acquisition CLOSE_PARENS
5310           {
5311                 start_block (lexer.Location);
5312                 Block assign_block = current_block;
5313
5314                 if ($3 is DictionaryEntry){
5315                         DictionaryEntry de = (DictionaryEntry) $3;
5316                         Location l = (Location) $1;
5317
5318                         Expression type = (Expression) de.Key;
5319                         ArrayList var_declarators = (ArrayList) de.Value;
5320
5321                         ArrayList vars = new ArrayList (4);
5322
5323                         foreach (VariableDeclaration decl in var_declarators){
5324
5325                                 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
5326                                 if (vi == null)
5327                                         continue;
5328                                 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
5329
5330                                 Expression expr = decl.expression_or_array_initializer;
5331                                 if (expr == null) {
5332                                         Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
5333                                 }
5334
5335                                 LocalVariableReference var;
5336
5337                                 // Get a writable reference to this read-only variable.
5338                                 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
5339
5340                                 // This is so that it is not a warning on using variables
5341                                 vi.Used = true;
5342
5343                                 vars.Add (new DictionaryEntry (var, expr));                             
5344
5345                                 // Assign a = new Assign (var, expr, decl.Location);
5346                                 // assign_block.AddStatement (new StatementExpression (a));
5347                         }
5348
5349                         // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
5350                         // It can be referred to as $5 below.
5351                         $$ = new DictionaryEntry (type, vars);
5352                  } else {
5353                         $$ = $3;
5354                  }
5355           } 
5356           embedded_statement
5357           {
5358                 Using u = new Using ($5, (Statement) $6, (Location) $1);
5359                 current_block.AddStatement (u);
5360                 $$ = end_block (lexer.Location);
5361           }
5362         ; 
5363
5364 resource_acquisition
5365         : local_variable_declaration
5366         | expression
5367         ;
5368
5369
5370 // LINQ
5371
5372 query_expression
5373         : first_from_clause  query_body
5374           {
5375                 lexer.QueryParsing = false;
5376
5377                 Linq.AQueryClause from = $1 as Linq.AQueryClause;
5378                         
5379                 from.Tail.Next = (Linq.AQueryClause)$2;
5380                 $$ = from;
5381                 
5382                 current_block.SetEndLocation (lexer.Location);
5383                 current_block = current_block.Parent;
5384           }     
5385         ;
5386         
5387 first_from_clause
5388         : FROM IDENTIFIER IN expression
5389           {
5390                 current_block = new Linq.QueryBlock (current_block, GetLocation ($1));
5391                 LocatedToken lt = (LocatedToken) $2;
5392                 
5393                 current_block.AddVariable (Linq.ImplicitQueryParameter.ImplicitType.Instance, lt.Value, lt.Location);
5394                 $$ = new Linq.QueryExpression (lt, new Linq.QueryStartClause ((Expression)$4));
5395           }
5396         | FROM type IDENTIFIER IN expression
5397           {
5398                 current_block = new Linq.QueryBlock (current_block, GetLocation ($1));
5399                 LocatedToken lt = (LocatedToken) $3;
5400
5401                 Expression type = (Expression)$2;
5402                 current_block.AddVariable (type, lt.Value, lt.Location);
5403                 $$ = new Linq.QueryExpression (lt, new Linq.Cast (type, (Expression)$5));
5404           }
5405         ;
5406         
5407 from_clause
5408         : FROM IDENTIFIER IN expression
5409           {
5410                 LocatedToken lt = (LocatedToken) $2;
5411                 
5412                 current_block.AddVariable (Linq.ImplicitQueryParameter.ImplicitType.Instance,
5413                         lt.Value, lt.Location);
5414                         
5415                 $$ = new Linq.SelectMany (lt, (Expression)$4);                  
5416           }
5417         | FROM type IDENTIFIER IN expression
5418           {
5419                 LocatedToken lt = (LocatedToken) $3;
5420
5421                 Expression type = (Expression)$2;
5422                 current_block.AddVariable (type, lt.Value, lt.Location);
5423                 $$ = new Linq.SelectMany (lt, new Linq.Cast (type, (Expression)$5));
5424           }
5425         ;       
5426
5427 query_body
5428         : opt_query_body_clauses select_or_group_clause opt_query_continuation
5429           {
5430                 Linq.AQueryClause head = (Linq.AQueryClause)$2;
5431                 
5432                 if ($3 != null)
5433                         head.Next = (Linq.AQueryClause)$3;
5434                                 
5435                 if ($1 != null) {
5436                         Linq.AQueryClause clause = (Linq.AQueryClause)$1;
5437                         clause.Tail.Next = head;
5438                         head = clause;
5439                 }
5440                 
5441                 $$ = head;
5442           }
5443         ;
5444         
5445 select_or_group_clause
5446         : SELECT expression
5447           {
5448                 $$ = new Linq.Select ((Expression)$2, GetLocation ($1));
5449           }
5450         | GROUP expression BY expression
5451           {
5452             $$ = new Linq.GroupBy ((Expression)$2, (Expression)$4, GetLocation ($1));
5453           }
5454         ;
5455         
5456 opt_query_body_clauses
5457         : /* empty */
5458         | query_body_clauses
5459         ;
5460         
5461 query_body_clauses
5462         : query_body_clause
5463         | query_body_clauses query_body_clause
5464           {
5465                 ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$2;
5466                 $$ = $1;
5467           }
5468         ;
5469         
5470 query_body_clause
5471         : from_clause
5472         | let_clause
5473         | where_clause
5474         | join_clause
5475         | orderby_clause
5476         ;
5477         
5478 let_clause
5479         : LET IDENTIFIER ASSIGN expression
5480           {
5481                 LocatedToken lt = (LocatedToken) $2;
5482                 current_block.AddVariable (Linq.ImplicitQueryParameter.ImplicitType.Instance,
5483                         lt.Value, lt.Location);   
5484                 $$ = new Linq.Let (lt, (Expression)$4, GetLocation ($1));
5485           }
5486         ;
5487
5488 where_clause
5489         : WHERE boolean_expression
5490           {
5491                 $$ = new Linq.Where ((Expression)$2, GetLocation ($1));
5492           }
5493         ;
5494         
5495 join_clause
5496         : JOIN IDENTIFIER IN expression ON expression EQUALS expression opt_join_into
5497           {
5498                 Location loc = GetLocation ($1);
5499                 LocatedToken lt = (LocatedToken) $2;
5500                 current_block.AddVariable (Linq.ImplicitQueryParameter.ImplicitType.Instance,
5501                         lt.Value, lt.Location);
5502                 
5503                 if ($9 == null) {
5504                         $$ = new Linq.Join (lt, (Expression)$4, (Expression)$6,
5505                                 (Expression)$8, loc);
5506                 } else {
5507                         LocatedToken lt_into = (LocatedToken) $9;
5508                         $$ = new Linq.GroupJoin (lt, (Expression)$4, (Expression)$6,
5509                                 (Expression)$8, lt_into, loc);
5510                 }
5511           }
5512         | JOIN type IDENTIFIER IN expression ON expression EQUALS expression opt_join_into
5513           {
5514                 Location loc = GetLocation ($1);
5515                 LocatedToken lt = (LocatedToken) $3;
5516                 current_block.AddVariable ((Expression)$2, lt.Value, lt.Location);
5517                 
5518                 Linq.Cast cast = new Linq.Cast ((Expression)$2, (Expression)$5);
5519                 if ($10 == null) {
5520                         $$ = new Linq.Join (lt, cast, (Expression)$7,
5521                                 (Expression)$9, loc);
5522                 } else {
5523                         LocatedToken lt_into = (LocatedToken) $10;
5524                         $$ = new Linq.GroupJoin (lt, cast, (Expression)$7,
5525                                 (Expression)$9, lt_into, loc);
5526                 }
5527           }
5528         ;
5529         
5530 opt_join_into
5531         : /* empty */
5532         | INTO IDENTIFIER
5533           {
5534                 $$ = $2;
5535           }
5536         ;
5537         
5538 orderby_clause
5539         : ORDERBY orderings
5540           {
5541                 $$ = $2;
5542           }
5543         ;
5544         
5545 orderings
5546         : order_by
5547         | order_by COMMA orderings_then_by
5548           {
5549                 ((Linq.AQueryClause)$1).Next = (Linq.AQueryClause)$3;
5550                 $$ = $1;
5551           }
5552         ;
5553         
5554 orderings_then_by
5555         : then_by
5556           {
5557                 $$ = $1;
5558           }
5559         | orderings_then_by COMMA then_by
5560           {
5561                 ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$3;
5562                 $$ = $1;
5563           }
5564         ;       
5565         
5566 order_by
5567         : expression
5568           {
5569                 $$ = new Linq.OrderByAscending ((Expression)$1);        
5570           }
5571         | expression ASCENDING
5572           {
5573                 $$ = new Linq.OrderByAscending ((Expression)$1);        
5574           }
5575         | expression DESCENDING
5576           {
5577                 $$ = new Linq.OrderByDescending ((Expression)$1);       
5578           }
5579         ;
5580
5581 then_by
5582         : expression
5583           {
5584                 $$ = new Linq.ThenByAscending ((Expression)$1); 
5585           }
5586         | expression ASCENDING
5587           {
5588                 $$ = new Linq.ThenByAscending ((Expression)$1); 
5589           }
5590         | expression DESCENDING
5591           {
5592                 $$ = new Linq.ThenByDescending ((Expression)$1);        
5593           }     
5594         ;
5595
5596
5597 opt_query_continuation
5598         : /* empty */
5599         | INTO IDENTIFIER
5600           {
5601                 // query continuation block is not linked with query block but with block
5602                 // before. This means each query can use same range variable names for
5603                 // different identifiers.
5604
5605                 current_block.SetEndLocation (GetLocation ($1));
5606                 current_block = current_block.Parent;
5607                 current_block = new Linq.QueryBlock (current_block, GetLocation ($1));
5608                 
5609                 LocatedToken lt = (LocatedToken) $2;
5610                 current_block.AddVariable (Linq.ImplicitQueryParameter.ImplicitType.Instance,
5611                         lt.Value, lt.Location);
5612           }
5613           query_body
5614           {
5615                 $$ = new Linq.QueryExpression ((LocatedToken) $2,
5616                         (Linq.AQueryClause)$4);
5617           }
5618         ;
5619         
5620 %%
5621
5622 // <summary>
5623 //   A class used to pass around variable declarations and constants
5624 // </summary>
5625 public class VariableDeclaration {
5626         public string identifier;
5627         public Expression expression_or_array_initializer;
5628         public Location Location;
5629         public Attributes OptAttributes;
5630         public string DocComment;
5631
5632         public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
5633         {
5634                 this.identifier = lt.Value;
5635                 if (eoai is ArrayList) {
5636                         this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
5637                 } else {
5638                         this.expression_or_array_initializer = (Expression)eoai;
5639                 }
5640                 this.Location = lt.Location;
5641                 this.OptAttributes = opt_attrs;
5642         }
5643
5644         public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
5645         {
5646         }
5647 }
5648
5649 // <summary>
5650 //   A class used to hold info about an indexer declarator
5651 // </summary>
5652 public class IndexerDeclaration {
5653         public Expression type;
5654         public MemberName interface_type;
5655         public Parameters param_list;
5656         public Location location;
5657
5658         public IndexerDeclaration (Expression type, MemberName interface_type,
5659                                    Parameters param_list, Location loc)
5660         {
5661                 this.type = type;
5662                 this.interface_type = interface_type;
5663                 this.param_list = param_list;
5664                 this.location = loc;
5665         }
5666 }
5667
5668 //
5669 // We use this when we do not have an object in advance that is an IAnonymousHost
5670 //
5671 public class SimpleAnonymousHost : IAnonymousHost {
5672         public static readonly SimpleAnonymousHost Simple = new SimpleAnonymousHost ();
5673
5674         bool yields;
5675         ArrayList anonymous_methods;
5676
5677         public static SimpleAnonymousHost GetSimple () {
5678                 Simple.yields = false;
5679                 Simple.anonymous_methods = null;
5680                 return Simple;
5681         }
5682
5683         public void SetYields ()
5684         {
5685                 yields = true;
5686         }
5687
5688         public void AddAnonymousMethod (AnonymousMethodExpression anonymous)
5689         {
5690                 if (anonymous_methods == null)
5691                         anonymous_methods = new ArrayList ();
5692                 anonymous_methods.Add (anonymous);
5693         }
5694
5695         public void Propagate (IAnonymousHost real_host)
5696         {
5697                 if (yields)
5698                         real_host.SetYields ();
5699                 if (anonymous_methods != null) {
5700                         foreach (AnonymousMethodExpression ame in anonymous_methods)
5701                                 real_host.AddAnonymousMethod (ame);
5702                 }
5703         }
5704 }
5705
5706 // <summary>
5707 //  A class used to hold info about an operator declarator
5708 // </summary>
5709 public class OperatorDeclaration {
5710         public Operator.OpType optype;
5711         public Expression ret_type, arg1type, arg2type;
5712         public string arg1name, arg2name;
5713         public Location location;
5714
5715         public OperatorDeclaration (Operator.OpType op, Expression ret_type, 
5716                                     Expression arg1type, string arg1name,
5717                                     Expression arg2type, string arg2name, Location location)
5718         {
5719                 optype = op;
5720                 this.ret_type = ret_type;
5721                 this.arg1type = arg1type;
5722                 this.arg1name = arg1name;
5723                 this.arg2type = arg2type;
5724                 this.arg2name = arg2name;
5725                 this.location = location;
5726         }
5727
5728 }
5729
5730 void Error_ExpectingTypeName (Expression expr)
5731 {
5732         if (expr is Invocation){
5733                 Report.Error (1002, expr.Location, "Expecting `;'");
5734         } else {
5735                 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
5736         }
5737 }
5738
5739 public static void Error_ParameterModifierNotValid (Location loc)
5740 {
5741         Report.Error (631, loc, "The modifiers `ref' and `out' are not valid in this context");
5742 }
5743
5744 static void Error_DuplicateParameterModifier (Location loc, Parameter.Modifier mod)
5745 {
5746         Report.Error (1107, loc, "Duplicate parameter modifier `{0}'",
5747                 Parameter.GetModifierSignature (mod));
5748 }
5749
5750 static void Error_TypeExpected (Location loc)
5751 {
5752         Report.Error (1031, loc, "Type expected");
5753 }
5754
5755 void push_current_class (TypeContainer tc, object partial_token)
5756 {
5757         if (partial_token != null)
5758                 current_container = current_container.AddPartial (tc);
5759         else
5760                 current_container = current_container.AddTypeContainer (tc);
5761         current_class = tc;
5762 }
5763
5764 DeclSpace pop_current_class ()
5765 {
5766         DeclSpace retval = current_class;
5767
5768         current_class = current_class.Parent;
5769         current_container = current_class.PartialContainer;
5770
5771         return retval;
5772 }
5773
5774 // <summary>
5775 //   Given the @class_name name, it creates a fully qualified name
5776 //   based on the containing declaration space
5777 // </summary>
5778 MemberName
5779 MakeName (MemberName class_name)
5780 {
5781         Namespace ns = current_namespace.NS;
5782
5783         if (current_container.Name.Length == 0){
5784                 if (ns.Name.Length != 0)
5785                         return new MemberName (ns.MemberName, class_name);
5786                 else
5787                         return class_name;
5788         } else {
5789                 return new MemberName (current_container.MemberName, class_name);
5790         }
5791 }
5792
5793 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
5794 {
5795         Block implicit_block;
5796         ArrayList inits = null;
5797
5798         //
5799         // We use the `Used' property to check whether statements
5800         // have been added to the current block.  If so, we need
5801         // to create another block to contain the new declaration
5802         // otherwise, as an optimization, we use the same block to
5803         // add the declaration.
5804         //
5805         // FIXME: A further optimization is to check if the statements
5806         // that were added were added as part of the initialization
5807         // below.  In which case, no other statements have been executed
5808         // and we might be able to reduce the number of blocks for
5809         // situations like this:
5810         //
5811         // int j = 1;  int k = j + 1;
5812         //
5813         if (current_block.Used)
5814                 implicit_block = new Block (current_block, loc, Location.Null);
5815         else
5816                 implicit_block = current_block;
5817
5818         foreach (VariableDeclaration decl in variable_declarators){
5819
5820                 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
5821                         if (decl.expression_or_array_initializer != null){
5822                                 if (inits == null)
5823                                         inits = new ArrayList (4);
5824                                 inits.Add (decl);
5825                         }
5826                 }
5827         }
5828
5829         if (inits == null)
5830                 return implicit_block;
5831
5832         foreach (VariableDeclaration decl in inits){
5833                 Assign assign;
5834                 Expression expr = decl.expression_or_array_initializer;
5835                 
5836                 LocalVariableReference var;
5837                 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
5838
5839                 assign = new Assign (var, expr, decl.Location);
5840
5841                 implicit_block.AddStatement (new StatementExpression (assign));
5842         }
5843         
5844         return implicit_block;
5845 }
5846
5847 Block declare_local_constants (Expression type, ArrayList declarators)
5848 {
5849         Block implicit_block;
5850
5851         if (current_block.Used)
5852                 implicit_block = new Block (current_block);
5853         else
5854                 implicit_block = current_block;
5855
5856         foreach (VariableDeclaration decl in declarators){
5857                 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
5858         }
5859         
5860         return implicit_block;
5861 }
5862
5863 string CheckAttributeTarget (string a, Location l)
5864 {
5865         switch (a) {
5866         case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
5867                         return a;
5868         }
5869
5870         Report.Warning (658, 1, l,
5871                  "`{0}' is invalid attribute target. All attributes in this attribute section will be ignored", a);
5872         return string.Empty;
5873 }
5874
5875 void CheckUnaryOperator (Operator.OpType op, Location l)
5876 {
5877         switch (op) {
5878                 
5879         case Operator.OpType.LogicalNot: 
5880         case Operator.OpType.OnesComplement: 
5881         case Operator.OpType.Increment:
5882         case Operator.OpType.Decrement:
5883         case Operator.OpType.True: 
5884         case Operator.OpType.False: 
5885         case Operator.OpType.Addition: 
5886         case Operator.OpType.Subtraction:
5887                 
5888                 break;
5889                 
5890         default :
5891                 Report.Error (1019, l, "Overloadable unary operator expected"); 
5892                 break;
5893                 
5894         }
5895 }
5896
5897 void CheckBinaryOperator (Operator.OpType op, Location l)
5898 {
5899         switch (op) {
5900                 
5901         case Operator.OpType.Addition: 
5902         case Operator.OpType.Subtraction: 
5903         case Operator.OpType.Multiply:
5904         case Operator.OpType.Division:
5905         case Operator.OpType.Modulus: 
5906         case Operator.OpType.BitwiseAnd: 
5907         case Operator.OpType.BitwiseOr:
5908         case Operator.OpType.ExclusiveOr: 
5909         case Operator.OpType.LeftShift: 
5910         case Operator.OpType.RightShift:
5911         case Operator.OpType.Equality: 
5912         case Operator.OpType.Inequality:
5913         case Operator.OpType.GreaterThan: 
5914         case Operator.OpType.LessThan: 
5915         case Operator.OpType.GreaterThanOrEqual:
5916         case Operator.OpType.LessThanOrEqual:
5917                 break;
5918                 
5919         default :
5920                 Report.Error (1020, l, "Overloadable binary operator expected");
5921                 break;
5922         }
5923         
5924 }
5925
5926 void syntax_error (Location l, string msg)
5927 {
5928         Report.Error (1003, l, "Syntax error, " + msg);
5929 }
5930
5931 void note (string s)
5932 {
5933         // Used to put annotations
5934 }
5935
5936 Tokenizer lexer;
5937
5938 public Tokenizer Lexer {
5939         get {
5940                 return lexer;
5941         }
5942 }                  
5943
5944 static CSharpParser ()
5945 {
5946         oob_stack = new Stack ();
5947 }
5948
5949 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
5950 {
5951         this.name = file.Name;
5952         this.file = file;
5953         current_namespace = new NamespaceEntry (null, file, null);
5954         current_class = current_namespace.SlaveDeclSpace;
5955         current_container = current_class.PartialContainer; // == RootContest.ToplevelTypes
5956         oob_stack.Clear ();
5957         lexer = new Tokenizer (reader, file, defines);
5958 }
5959
5960 public void parse ()
5961 {
5962         int errors = Report.Errors;
5963         try {
5964                 if (yacc_verbose_flag > 1)
5965                         yyparse (lexer, new yydebug.yyDebugSimple ());
5966                 else
5967                         yyparse (lexer);
5968                 Tokenizer tokenizer = lexer as Tokenizer;
5969                 tokenizer.cleanup ();
5970         } catch (Exception e){
5971                 //
5972                 // Removed for production use, use parser verbose to get the output.
5973                 //
5974                 // Console.WriteLine (e);
5975                 if (Report.Errors == errors)
5976                         Report.Error (-25, lexer.Location, "Parsing error");
5977                 if (yacc_verbose_flag > 0)
5978                         Console.WriteLine (e);
5979         }
5980
5981         if (RootContext.ToplevelTypes.NamespaceEntry != null)
5982                 throw new InternalErrorException ("who set it?");
5983 }
5984
5985 static void CheckToken (int error, int yyToken, string msg, Location loc)
5986 {
5987         if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
5988                 Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ());
5989         else
5990                 Report.Error (error, loc, msg);
5991 }
5992
5993 void CheckIdentifierToken (int yyToken, Location loc)
5994 {
5995         CheckToken (1041, yyToken, "Identifier expected", loc);
5996 }
5997
5998 string ConsumeStoredComment ()
5999 {
6000         string s = tmpComment;
6001         tmpComment = null;
6002         Lexer.doc_state = XmlCommentState.Allowed;
6003         return s;
6004 }
6005
6006 Location GetLocation (object obj)
6007 {
6008         if (obj is MemberCore)
6009                 return ((MemberCore) obj).Location;
6010         if (obj is MemberName)
6011                 return ((MemberName) obj).Location;
6012         if (obj is LocatedToken)
6013                 return ((LocatedToken) obj).Location;
6014         if (obj is Location)
6015                 return (Location) obj;
6016         return lexer.Location;
6017 }
6018
6019 void start_block (Location loc)
6020 {
6021         if (current_block == null || parsing_anonymous_method) {
6022                 current_block = new ToplevelBlock (current_block, current_local_parameters, current_generic_method, loc);
6023                 parsing_anonymous_method = false;
6024         } else {
6025                 current_block = new ExplicitBlock (current_block, loc, Location.Null);
6026         }
6027 }
6028
6029 Block
6030 end_block (Location loc)
6031 {
6032         Block retval = current_block.Explicit;
6033         retval.SetEndLocation (loc);
6034         current_block = retval.Parent;
6035         return retval;
6036 }
6037
6038 void
6039 start_anonymous (bool lambda, Parameters parameters, Location loc)
6040 {
6041         oob_stack.Push (current_anonymous_method);
6042         oob_stack.Push (current_local_parameters);
6043
6044         current_local_parameters = parameters;
6045
6046         ToplevelBlock top_current_block = current_block == null ? null : current_block.Toplevel;
6047
6048         current_anonymous_method = lambda 
6049                 ? new LambdaExpression (
6050                         current_anonymous_method, current_generic_method, current_container,
6051                         parameters, top_current_block, loc) 
6052                 : new AnonymousMethodExpression (
6053                         current_anonymous_method, current_generic_method, current_container,
6054                         parameters, top_current_block, loc);
6055
6056         // Force the next block to be created as a ToplevelBlock
6057         parsing_anonymous_method = true;
6058 }
6059
6060 /*
6061  * Completes the anonymous method processing, if lambda_expr is null, this
6062  * means that we have a Statement instead of an Expression embedded 
6063  */
6064 AnonymousMethodExpression 
6065 end_anonymous (ToplevelBlock anon_block, Location loc)
6066 {
6067         AnonymousMethodExpression retval;
6068
6069         if (RootContext.Version == LanguageVersion.ISO_1){
6070                 Report.FeatureIsNotISO1 (loc, "anonymous methods");
6071                 retval = null;
6072         } else  {
6073                 current_anonymous_method.Block = anon_block;
6074                 if ((anonymous_host != null) && (current_anonymous_method.Parent == null))
6075                         anonymous_host.AddAnonymousMethod (current_anonymous_method);
6076
6077                 retval = current_anonymous_method;
6078         }
6079
6080         current_local_parameters = (Parameters) oob_stack.Pop ();
6081         current_anonymous_method = (AnonymousMethodExpression) oob_stack.Pop ();
6082
6083         return retval;
6084 }
6085
6086 /* end end end */
6087 }