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