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