3 // cs-parser.jay: The Parser for the C# compiler
5 // Authors: Miguel de Icaza (miguel@gnu.org)
6 // Ravi Pratap (ravi@ximian.com)
7 // Marek Safar (marek.safar@gmail.com)
9 // Licensed under the terms of the GNU GPL
11 // (C) 2001 Ximian, Inc (http://www.ximian.com)
12 // (C) 2004 Novell, Inc
15 // (1) Figure out why error productions dont work. `type-declaration' is a
16 // great spot to put an `error' because you can reproduce it with this input:
19 // Possible optimization:
20 // Run memory profiler with parsing only, and consider dropping
21 // arraylists where not needed. Some pieces can use linked lists.
29 using System.Collections;
34 public class CSharpParser {
35 NamespaceEntry current_namespace;
36 TypeContainer current_container;
37 DeclSpace current_class;
39 IAnonymousHost anonymous_host;
42 /// Current block is used to add statements as we find
47 Delegate current_delegate;
49 AnonymousMethodExpression current_anonymous_method;
52 /// This is used by the unary_expression code to resolve
53 /// a name against a parameter.
55 Parameters current_local_parameters;
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).
62 Expression implicit_value_parameter_type;
63 Parameters indexer_parameters;
66 /// Hack to help create non-typed array initializer
68 public static Expression current_array_type;
69 Expression pushed_current_array_type;
72 /// Used to determine if we are parsing the get/set pair
73 /// of an indexer or a property
77 bool parsing_anonymous_method;
80 /// An out-of-band stack.
82 static Stack oob_stack;
89 static public int yacc_verbose_flag;
91 // Name of the file we are parsing
100 /// Temporary Xml documentation cache.
101 /// For enum types, we need one more temporary store.
104 string enumTypeComment;
106 /// Current attribute target
107 string current_attr_target;
109 /// assembly and module attribute definitions are enabled
110 bool global_attrs_enabled = true;
111 bool has_get, has_set;
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.
121 *These are the C# keywords
208 /* C# keywords which are not really keywords */
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 ")"
232 %token OP_GENERICS_LT "<"
234 %token OP_GENERICS_GT ">"
235 %token BITWISE_AND "&"
236 %token BITWISE_OR "|"
243 /* C# multi-character operators. */
244 %token DOUBLE_COLON "::"
247 %token OP_SHIFT_LEFT "<<"
248 %token OP_SHIFT_RIGHT ">>"
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 "|="
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"
276 %token OPEN_PARENS_LAMBDA
277 %token CLOSE_PARENS_CAST
278 %token CLOSE_PARENS_NO_CAST
279 %token CLOSE_PARENS_OPEN_PARENS
280 %token CLOSE_PARENS_MINUS
282 /* Add precedence rules to solve dangling else s/r conflict */
291 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
293 %left STAR DIV PERCENT
294 %right BANG CARRET UMINUS
295 %nonassoc OP_INC OP_DEC
297 %left OPEN_BRACKET OPEN_BRACE
301 %start compilation_unit
305 : outer_declarations opt_EOF
306 | outer_declarations global_attributes opt_EOF
307 | global_attributes opt_EOF
308 | opt_EOF /* allow empty files */
314 Lexer.check_incorrect_doc_comment ();
318 Lexer.check_incorrect_doc_comment ();
324 | outer_declarations outer_declaration
328 : extern_alias_directive
330 | namespace_member_declaration
333 extern_alias_directives
334 : extern_alias_directive
335 | extern_alias_directives extern_alias_directive;
337 extern_alias_directive
338 : EXTERN IDENTIFIER IDENTIFIER SEMICOLON
340 LocatedToken lt = (LocatedToken) $2;
343 Report.Error (1003, lt.Location, "'alias' expected");
344 } else if (RootContext.Version == LanguageVersion.ISO_1) {
345 Report.FeatureIsNotISO1 (lt.Location, "external alias");
347 lt = (LocatedToken) $3;
348 current_namespace.UsingExternalAlias (lt.Value, lt.Location);
355 | using_directives using_directive
359 : using_alias_directive
361 if (RootContext.Documentation != null)
362 Lexer.doc_state = XmlCommentState.Allowed;
364 | using_namespace_directive
366 if (RootContext.Documentation != null)
367 Lexer.doc_state = XmlCommentState.Allowed;
371 using_alias_directive
372 : USING IDENTIFIER ASSIGN
373 namespace_or_type_name SEMICOLON
375 LocatedToken lt = (LocatedToken) $2;
376 current_namespace.UsingAlias (lt.Value, (MemberName) $4, (Location) $1);
379 CheckIdentifierToken (yyToken, GetLocation ($2));
383 using_namespace_directive
384 : USING namespace_name SEMICOLON
386 current_namespace.Using ((MemberName) $2, (Location) $1);
391 // Strictly speaking, namespaces don't have attributes but
392 // we parse global attributes along with namespace declarations and then
395 namespace_declaration
396 : opt_attributes NAMESPACE namespace_or_type_name
398 MemberName name = (MemberName) $3;
401 Report.Error(1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
404 current_namespace = new NamespaceEntry (
405 current_namespace, file, name.GetName ());
406 current_class = current_namespace.SlaveDeclSpace;
407 current_container = current_class.PartialContainer;
409 namespace_body opt_semicolon
411 current_namespace = current_namespace.Parent;
412 current_class = current_namespace.SlaveDeclSpace;
413 current_container = current_class.PartialContainer;
428 : namespace_or_type_name
434 if (RootContext.Documentation != null)
435 Lexer.doc_state = XmlCommentState.Allowed;
441 : opt_extern_alias_directives
443 opt_namespace_member_declarations
447 Report.Error (1518, lexer.Location, "Expected `class', `delegate', `enum', `interface', or `struct'");
450 | opt_extern_alias_directives
452 opt_namespace_member_declarations
455 Report.Error (1513, lexer.Location, "} expected");
464 opt_extern_alias_directives
466 | extern_alias_directives
469 opt_namespace_member_declarations
471 | namespace_member_declarations
474 namespace_member_declarations
475 : namespace_member_declaration
476 | namespace_member_declarations namespace_member_declaration
479 namespace_member_declaration
483 DeclSpace ds = (DeclSpace)$1;
485 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
486 Report.Error (1527, ds.Location,
487 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
490 current_namespace.DeclarationFound = true;
492 | namespace_declaration {
493 current_namespace.DeclarationFound = true;
496 | field_declaration {
497 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
499 | method_declaration {
500 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
507 | interface_declaration
509 | delegate_declaration
511 // Enable this when we have handled all errors, because this acts as a generic fallback
514 // Console.WriteLine ("Token=" + yyToken);
515 // Report.Error (1518, GetLocation ($1), "Expected class, struct, interface, enum or delegate");
527 Attributes attrs = (Attributes)$1;
528 if (global_attrs_enabled) {
529 CodeGen.Assembly.AddAttributes (attrs.Attrs);
531 foreach (Attribute a in attrs.Attrs) {
532 Report.Error (1730, a.Location, "Assembly and module attributes must precede all other elements except using clauses and extern alias declarations");
543 global_attrs_enabled = false;
548 global_attrs_enabled = false;
557 if (current_attr_target != String.Empty) {
558 ArrayList sect = (ArrayList) $1;
560 if (global_attrs_enabled) {
561 if (current_attr_target == "module") {
562 CodeGen.Module.AddAttributes (sect);
564 } else if (current_attr_target != null && current_attr_target.Length > 0) {
565 CodeGen.Assembly.AddAttributes (sect);
568 $$ = new Attributes (sect);
571 if (RootContext.Documentation != null) {
572 Lexer.check_incorrect_doc_comment ();
574 XmlCommentState.Allowed;
578 $$ = new Attributes (sect);
583 current_attr_target = null;
585 | attribute_sections attribute_section
587 if (current_attr_target != String.Empty) {
588 Attributes attrs = $1 as Attributes;
589 ArrayList sect = (ArrayList) $2;
591 if (global_attrs_enabled) {
592 if (current_attr_target == "module") {
593 CodeGen.Module.AddAttributes (sect);
595 } else if (current_attr_target == "assembly") {
596 CodeGen.Assembly.AddAttributes (sect);
600 attrs = new Attributes (sect);
602 attrs.AddAttributes (sect);
606 attrs = new Attributes (sect);
608 attrs.AddAttributes (sect);
614 current_attr_target = null;
619 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
623 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
629 attribute_target_specifier
630 : attribute_target COLON
632 current_attr_target = (string)$1;
640 LocatedToken lt = (LocatedToken) $1;
641 $$ = CheckAttributeTarget (lt.Value, lt.Location);
643 | EVENT { $$ = "event"; }
644 | RETURN { $$ = "return"; }
647 string name = yyNames [yyToken].ToLower ();
648 $$ = CheckAttributeTarget (name, GetLocation ($1));
655 ArrayList attrs = new ArrayList (4);
661 | attribute_list COMMA attribute
663 ArrayList attrs = (ArrayList) $1;
671 : attribute_name opt_attribute_arguments
673 MemberName mname = (MemberName) $1;
674 object[] arguments = (object[]) $2;
675 MemberName left = mname.Left;
676 string identifier = mname.Name;
678 Expression left_expr = left == null ? null : left.GetTypeExpression ();
680 if (current_attr_target == String.Empty)
682 else if (global_attrs_enabled && (current_attr_target == "assembly" || current_attr_target == "module"))
683 // FIXME: supply "nameEscaped" parameter here.
684 $$ = new GlobalAttribute (current_namespace, current_attr_target,
685 left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
687 $$ = new Attribute (current_attr_target, left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
692 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
695 opt_attribute_arguments
696 : /* empty */ { $$ = null; }
697 | OPEN_PARENS attribute_arguments CLOSE_PARENS
705 : opt_positional_argument_list
710 $$ = new object [] { $1, null };
713 | positional_argument_list COMMA named_argument_list
715 $$ = new object[] { $1, $3 };
717 | named_argument_list
719 $$ = new object [] { null, $1 };
724 opt_positional_argument_list
725 : /* empty */ { $$ = null; }
726 | positional_argument_list
729 positional_argument_list
732 ArrayList args = new ArrayList (4);
733 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
737 | positional_argument_list COMMA expression
739 ArrayList args = (ArrayList) $1;
740 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
749 ArrayList args = new ArrayList (4);
754 | named_argument_list COMMA named_argument
756 ArrayList args = (ArrayList) $1;
761 | named_argument_list COMMA expression
763 Report.Error (1016, ((Expression) $3).Location, "Named attribute argument expected");
769 : IDENTIFIER ASSIGN expression
771 // FIXME: keep location
772 $$ = new DictionaryEntry (
773 ((LocatedToken) $1).Value,
774 new Argument ((Expression) $3, Argument.AType.Expression));
780 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
783 opt_class_member_declarations
785 | class_member_declarations
788 class_member_declarations
789 : class_member_declaration
790 | class_member_declarations
791 class_member_declaration
794 class_member_declaration
795 : constant_declaration // done
796 | field_declaration // done
797 | method_declaration // done
798 | property_declaration // done
799 | event_declaration // done
800 | indexer_declaration // done
801 | operator_declaration // done
802 | constructor_declaration // done
803 | destructor_declaration // done
816 MemberName name = MakeName ((MemberName) $6);
817 push_current_class (new Struct (current_namespace, current_class, name, (int) $2, (Attributes) $1), $3);
821 if (RootContext.Documentation != null)
822 current_container.DocComment = Lexer.consume_doc_comment ();
826 if (RootContext.Documentation != null)
827 Lexer.doc_state = XmlCommentState.Allowed;
831 $$ = pop_current_class ();
833 | opt_attributes opt_modifiers opt_partial STRUCT error {
834 CheckIdentifierToken (yyToken, GetLocation ($5));
841 if (RootContext.Documentation != null)
842 Lexer.doc_state = XmlCommentState.Allowed;
844 opt_struct_member_declarations CLOSE_BRACE
847 opt_struct_member_declarations
849 | struct_member_declarations
852 struct_member_declarations
853 : struct_member_declaration
854 | struct_member_declarations struct_member_declaration
857 struct_member_declaration
858 : constant_declaration
861 | property_declaration
863 | indexer_declaration
864 | operator_declaration
865 | constructor_declaration
869 * This is only included so we can flag error 575:
870 * destructors only allowed on class types
872 | destructor_declaration
883 int modflags = (int) $2;
884 foreach (VariableDeclaration constant in (ArrayList) $5){
885 Location l = constant.Location;
886 if ((modflags & Modifiers.STATIC) != 0) {
887 Report.Error (504, l, "The constant `{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
891 Const c = new Const (
892 current_class, (Expression) $4, (string) constant.identifier,
893 (Expression) constant.expression_or_array_initializer, modflags,
896 if (RootContext.Documentation != null) {
897 c.DocComment = Lexer.consume_doc_comment ();
898 Lexer.doc_state = XmlCommentState.Allowed;
900 current_container.AddConstant (c);
906 : constant_declarator
908 ArrayList constants = new ArrayList (4);
913 | constant_declarators COMMA constant_declarator
916 ArrayList constants = (ArrayList) $1;
923 : IDENTIFIER ASSIGN constant_expression
925 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
929 // A const field requires a value to be provided
930 Report.Error (145, ((LocatedToken) $1).Location, "A const field requires a value to be provided");
942 Expression type = (Expression) $3;
945 current_array_type = null;
947 foreach (VariableDeclaration var in (ArrayList) $4){
948 Field field = new Field (current_class, type, mod, var.identifier,
949 (Attributes) $1, var.Location);
951 field.Initializer = var.expression_or_array_initializer;
953 if (RootContext.Documentation != null) {
954 field.DocComment = Lexer.consume_doc_comment ();
955 Lexer.doc_state = XmlCommentState.Allowed;
957 current_container.AddField (field);
958 $$ = field; // FIXME: might be better if it points to the top item
965 fixed_variable_declarators
968 Expression type = (Expression) $4;
971 current_array_type = null;
973 foreach (VariableDeclaration var in (ArrayList) $5) {
974 FixedField field = new FixedField (current_class, type, mod, var.identifier,
975 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
977 if (RootContext.Documentation != null) {
978 field.DocComment = Lexer.consume_doc_comment ();
979 Lexer.doc_state = XmlCommentState.Allowed;
981 current_container.AddField (field);
982 $$ = field; // FIXME: might be better if it points to the top item
991 Report.Error (1641, GetLocation ($4), "A fixed size buffer field must have the array size specifier after the field name");
998 current_array_type = null;
999 Report.Error (670, (Location) $3, "Fields cannot have void type");
1003 fixed_variable_declarators
1004 : fixed_variable_declarator
1006 ArrayList decl = new ArrayList (2);
1010 | fixed_variable_declarators COMMA fixed_variable_declarator
1012 ArrayList decls = (ArrayList) $1;
1018 fixed_variable_declarator
1019 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
1021 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1023 | IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1025 Report.Error (443, lexer.Location, "Value or constant expected");
1026 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1030 variable_declarators
1031 : variable_declarator
1033 ArrayList decl = new ArrayList (4);
1038 | variable_declarators COMMA variable_declarator
1040 ArrayList decls = (ArrayList) $1;
1047 : IDENTIFIER ASSIGN variable_initializer
1049 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1053 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1055 | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
1057 Report.Error (650, ((LocatedToken) $1).Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
1058 "To declare a fixed size buffer field, use the fixed keyword before the field type");
1063 variable_initializer
1072 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
1074 $$ = new StackAlloc ((Expression) $2, (Expression) $4, (Location) $1);
1078 $$ = new ArglistAccess ((Location) $1);
1082 Report.Error (1575, (Location) $1, "A stackalloc expression requires [] after type");
1089 anonymous_host = (IAnonymousHost) $1;
1090 if (RootContext.Documentation != null)
1091 Lexer.doc_state = XmlCommentState.NotAllowed;
1095 Method method = (Method) $1;
1096 method.Block = (ToplevelBlock) $3;
1097 current_container.AddMethod (method);
1099 anonymous_host = null;
1100 current_local_parameters = null;
1102 if (RootContext.Documentation != null)
1103 Lexer.doc_state = XmlCommentState.Allowed;
1116 Report.Error (1585, lexer.Location,
1117 "Member modifier `{0}' must precede the member type and name",
1118 Modifiers.Name (i));
1127 // This rule is used to handle the cases where OPEN_PARENS_LAMBDA
1128 // is returned (type followed by an identifier), these are the
1129 // declarations (methods, overloads, constructors, etc) and a handful
1130 // of expressions ("using", "fixed") or "catch".
1134 | OPEN_PARENS_LAMBDA
1140 type namespace_or_type_name
1141 open_parens opt_formal_parameter_list CLOSE_PARENS
1143 MemberName name = (MemberName) $4;
1145 Method method = new Method (current_class, null, (Expression) $3, (int) $2,
1146 false, name, (Parameters) $6, (Attributes) $1);
1148 anonymous_host = method;
1149 current_local_parameters = (Parameters) $6;
1151 if (RootContext.Documentation != null)
1152 method.DocComment = Lexer.consume_doc_comment ();
1158 VOID namespace_or_type_name
1159 open_parens opt_formal_parameter_list CLOSE_PARENS
1161 MemberName name = (MemberName) $4;
1163 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1164 (int) $2, false, name, (Parameters) $6,
1167 anonymous_host = method;
1168 current_local_parameters = (Parameters) $6;
1170 if (RootContext.Documentation != null)
1171 method.DocComment = Lexer.consume_doc_comment ();
1178 modifiers namespace_or_type_name open_parens opt_formal_parameter_list CLOSE_PARENS
1180 MemberName name = (MemberName) $5;
1181 Report.Error (1585, name.Location,
1182 "Member modifier `{0}' must precede the member type and name", Modifiers.Name ((int) $4));
1184 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1185 0, false, name, (Parameters) $7, (Attributes) $1);
1187 current_local_parameters = (Parameters) $7;
1189 if (RootContext.Documentation != null)
1190 method.DocComment = Lexer.consume_doc_comment ();
1198 | SEMICOLON { $$ = null; }
1201 opt_formal_parameter_list
1202 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1203 | formal_parameter_list
1206 formal_parameter_list
1209 ArrayList pars_list = (ArrayList) $1;
1211 Parameter [] pars = new Parameter [pars_list.Count];
1212 pars_list.CopyTo (pars);
1214 $$ = new Parameters (pars);
1216 | fixed_parameters COMMA parameter_array
1218 ArrayList pars_list = (ArrayList) $1;
1221 Parameter [] pars = new Parameter [pars_list.Count];
1222 pars_list.CopyTo (pars);
1224 $$ = new Parameters (pars);
1226 | fixed_parameters COMMA ARGLIST
1228 ArrayList pars_list = (ArrayList) $1;
1229 //pars_list.Add (new ArglistParameter (GetLocation ($3)));
1231 Parameter [] pars = new Parameter [pars_list.Count];
1232 pars_list.CopyTo (pars);
1234 $$ = new Parameters (pars, true);
1236 | parameter_array COMMA error
1239 Report.Error (231, ((Parameter) $1).Location, "A params parameter must be the last parameter in a formal parameter list");
1242 | fixed_parameters COMMA parameter_array COMMA error
1245 Report.Error (231, ((Parameter) $3).Location, "A params parameter must be the last parameter in a formal parameter list");
1248 | ARGLIST COMMA error
1250 Report.Error (257, (Location) $1, "An __arglist parameter must be the last parameter in a formal parameter list");
1253 | fixed_parameters COMMA ARGLIST COMMA error
1255 Report.Error (257, (Location) $3, "An __arglist parameter must be the last parameter in a formal parameter list");
1260 $$ = new Parameters (new Parameter[] { (Parameter) $1 } );
1264 $$ = new Parameters (new Parameter[0], true);
1271 ArrayList pars = new ArrayList (4);
1276 | fixed_parameters COMMA fixed_parameter
1278 ArrayList pars = (ArrayList) $1;
1287 opt_parameter_modifier
1291 LocatedToken lt = (LocatedToken) $4;
1292 $$ = new Parameter ((Expression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1295 opt_parameter_modifier
1297 IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1299 LocatedToken lt = (LocatedToken) $4;
1300 Report.Error (1552, lt.Location, "Array type specifier, [], must appear before parameter name");
1304 opt_parameter_modifier
1307 Report.Error (1001, GetLocation ($3), "Identifier expected");
1311 opt_parameter_modifier
1314 CheckIdentifierToken (yyToken, GetLocation ($4));
1318 opt_parameter_modifier
1324 LocatedToken lt = (LocatedToken) $4;
1325 Report.Error (241, lt.Location, "Default parameter specifiers are not permitted");
1330 opt_parameter_modifier
1331 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1332 | parameter_modifiers
1336 : parameter_modifier
1340 | parameter_modifiers parameter_modifier
1342 Parameter.Modifier p2 = (Parameter.Modifier)$2;
1343 Parameter.Modifier mod = (Parameter.Modifier)$1 | p2;
1344 if (((Parameter.Modifier)$1 & p2) == p2) {
1345 Error_DuplicateParameterModifier (lexer.Location, p2);
1347 Report.Error (1108, lexer.Location, "A parameter cannot have specified more than one modifier");
1354 : REF { $$ = Parameter.Modifier.REF; }
1355 | OUT { $$ = Parameter.Modifier.OUT; }
1356 | THIS { $$ = Parameter.Modifier.This; }
1360 : opt_attributes PARAMS type IDENTIFIER
1362 LocatedToken lt = (LocatedToken) $4;
1363 $$ = new ParamsParameter ((Expression) $3, lt.Value, (Attributes) $1, lt.Location);
1365 | opt_attributes PARAMS PARAMS type IDENTIFIER
1367 Error_DuplicateParameterModifier (lexer.Location, Parameter.Modifier.PARAMS);
1370 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1372 Parameter.Modifier mod = (Parameter.Modifier)$3;
1373 if ((mod & Parameter.Modifier.This) != 0) {
1374 Report.Error (1104, lexer.Location, "The parameter modifiers `this' and `params' cannot be used altogether");
1376 Report.Error (1611, (Location) $2, "The params parameter cannot be declared as ref or out");
1380 | opt_attributes PARAMS type error {
1381 CheckIdentifierToken (yyToken, GetLocation ($4));
1386 property_declaration
1390 namespace_or_type_name
1392 if (RootContext.Documentation != null)
1393 tmpComment = Lexer.consume_doc_comment ();
1397 implicit_value_parameter_type = (Expression) $3;
1399 lexer.PropertyParsing = true;
1401 accessor_declarations
1403 lexer.PropertyParsing = false;
1404 has_get = has_set = false;
1412 Accessors accessors = (Accessors) $8;
1413 Accessor get_block = accessors.get_or_add;
1414 Accessor set_block = accessors.set_or_remove;
1416 MemberName name = (MemberName) $4;
1418 prop = new Property (current_class, (Expression) $3, (int) $2,
1419 false, name, (Attributes) $1, get_block, set_block, accessors.declared_in_reverse);
1421 current_container.AddProperty (prop);
1422 implicit_value_parameter_type = null;
1424 if (RootContext.Documentation != null)
1425 prop.DocComment = ConsumeStoredComment ();
1430 accessor_declarations
1431 : get_accessor_declaration
1433 $$ = new Accessors ((Accessor) $1, null);
1435 | get_accessor_declaration accessor_declarations
1437 Accessors accessors = (Accessors) $2;
1438 accessors.get_or_add = (Accessor) $1;
1441 | set_accessor_declaration
1443 $$ = new Accessors (null, (Accessor) $1);
1445 | set_accessor_declaration accessor_declarations
1447 Accessors accessors = (Accessors) $2;
1448 accessors.set_or_remove = (Accessor) $1;
1449 accessors.declared_in_reverse = true;
1454 Report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1459 get_accessor_declaration
1460 : opt_attributes opt_modifiers GET
1462 // If this is not the case, then current_local_parameters has already
1463 // been set in indexer_declaration
1464 if (parsing_indexer == false)
1465 current_local_parameters = null;
1467 current_local_parameters = indexer_parameters;
1468 lexer.PropertyParsing = false;
1470 anonymous_host = SimpleAnonymousHost.GetSimple ();
1475 Report.Error (1007, (Location) $3, "Property accessor already defined");
1478 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1480 current_local_parameters = null;
1481 lexer.PropertyParsing = true;
1483 SimpleAnonymousHost.Simple.Propagate (accessor);
1484 anonymous_host = null;
1486 if (RootContext.Documentation != null)
1487 if (Lexer.doc_state == XmlCommentState.Error)
1488 Lexer.doc_state = XmlCommentState.NotAllowed;
1494 set_accessor_declaration
1495 : opt_attributes opt_modifiers SET
1498 Parameter implicit_value_parameter = new Parameter (
1499 implicit_value_parameter_type, "value",
1500 Parameter.Modifier.NONE, null, (Location) $3);
1502 if (parsing_indexer == false) {
1503 args = new Parameter [1];
1504 args [0] = implicit_value_parameter;
1505 current_local_parameters = new Parameters (args);
1507 Parameter [] fpars = indexer_parameters.FixedParameters;
1510 int count = fpars.Length;
1512 args = new Parameter [count + 1];
1513 fpars.CopyTo (args, 0);
1514 args [count] = implicit_value_parameter;
1517 current_local_parameters = new Parameters (
1521 lexer.PropertyParsing = false;
1523 anonymous_host = SimpleAnonymousHost.GetSimple ();
1528 Report.Error (1007, ((LocatedToken) $3).Location, "Property accessor already defined");
1531 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1533 current_local_parameters = null;
1534 lexer.PropertyParsing = true;
1536 SimpleAnonymousHost.Simple.Propagate (accessor);
1537 anonymous_host = null;
1539 if (RootContext.Documentation != null
1540 && Lexer.doc_state == XmlCommentState.Error)
1541 Lexer.doc_state = XmlCommentState.NotAllowed;
1549 | SEMICOLON { $$ = null; }
1552 interface_declaration
1561 MemberName name = MakeName ((MemberName) $6);
1562 push_current_class (new Interface (current_namespace, current_class, name, (int) $2, (Attributes) $1), $3);
1566 if (RootContext.Documentation != null) {
1567 current_container.DocComment = Lexer.consume_doc_comment ();
1568 Lexer.doc_state = XmlCommentState.Allowed;
1573 if (RootContext.Documentation != null)
1574 Lexer.doc_state = XmlCommentState.Allowed;
1578 $$ = pop_current_class ();
1580 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1581 CheckIdentifierToken (yyToken, GetLocation ($5));
1587 opt_interface_member_declarations
1591 opt_interface_member_declarations
1593 | interface_member_declarations
1596 interface_member_declarations
1597 : interface_member_declaration
1598 | interface_member_declarations interface_member_declaration
1601 interface_member_declaration
1602 : interface_method_declaration
1607 Method m = (Method) $1;
1609 if (m.IsExplicitImpl)
1610 Report.Error (541, m.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1611 m.GetSignatureForError ());
1613 current_container.AddMethod (m);
1615 if (RootContext.Documentation != null)
1616 Lexer.doc_state = XmlCommentState.Allowed;
1618 | interface_property_declaration
1623 Property p = (Property) $1;
1625 if (p.IsExplicitImpl)
1626 Report.Error (541, p.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1627 p.GetSignatureForError ());
1629 current_container.AddProperty (p);
1631 if (RootContext.Documentation != null)
1632 Lexer.doc_state = XmlCommentState.Allowed;
1634 | interface_event_declaration
1637 Event e = (Event) $1;
1639 if (e.IsExplicitImpl)
1640 Report.Error (541, e.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1641 e.GetSignatureForError ());
1643 current_container.AddEvent (e);
1646 if (RootContext.Documentation != null)
1647 Lexer.doc_state = XmlCommentState.Allowed;
1649 | interface_indexer_declaration
1654 Indexer i = (Indexer) $1;
1656 if (i.IsExplicitImpl)
1657 Report.Error (541, i.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1658 i.GetSignatureForError ());
1660 current_container.AddIndexer (i);
1662 if (RootContext.Documentation != null)
1663 Lexer.doc_state = XmlCommentState.Allowed;
1665 | delegate_declaration
1668 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1669 ((MemberCore)$1).GetSignatureForError ());
1675 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1676 ((MemberCore)$1).GetSignatureForError ());
1679 | struct_declaration
1682 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1683 ((MemberCore)$1).GetSignatureForError ());
1689 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1690 ((MemberCore)$1).GetSignatureForError ());
1693 | interface_declaration
1696 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1697 ((MemberCore)$1).GetSignatureForError ());
1700 | constant_declaration
1702 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1710 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, GetLocation ($1));
1715 interface_method_declaration
1716 : opt_attributes opt_new type namespace_or_type_name
1717 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1720 $$ = new Method (current_class, null, (Expression) $3, (int) $2, true,
1721 (MemberName) $4, (Parameters) $6, (Attributes) $1);
1722 if (RootContext.Documentation != null)
1723 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1725 | opt_attributes opt_new type namespace_or_type_name
1726 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1727 OPEN_BRACE opt_statement_list CLOSE_BRACE
1729 $$ = new Method (current_class, null, (Expression) $3, (int) $2, true,
1730 (MemberName) $4, (Parameters) $6, (Attributes) $1);
1732 Report.Error (531, lexer.Location, "`{0}': interface members cannot have a definition",
1733 ((Method)$$).GetSignatureForError ());
1737 interface_property_declaration
1743 lexer.PropertyParsing = true;
1744 implicit_value_parameter_type = (Expression)$3;
1746 accessor_declarations
1748 has_get = has_set = false;
1749 lexer.PropertyParsing = false;
1750 implicit_value_parameter_type = null;
1754 LocatedToken lt = (LocatedToken) $4;
1755 MemberName name = new MemberName (lt.Value, lt.Location);
1757 if ($3 == TypeManager.system_void_expr) {
1758 Report.Error (547, lt.Location, "`{0}': property or indexer cannot have void type", lt.Value);
1764 p = new Property (current_class, (Expression) $3, (int) $2, true,
1765 name, (Attributes) $1,
1768 Report.Error (548, p.Location, "`{0}': property or indexer must have at least one accessor", p.GetSignatureForError ());
1772 Accessors accessor = (Accessors) $7;
1773 p = new Property (current_class, (Expression) $3, (int) $2, true,
1774 name, (Attributes) $1,
1775 accessor.get_or_add, accessor.set_or_remove, accessor.declared_in_reverse);
1777 if (accessor.get_or_add != null && accessor.get_or_add.Block != null) {
1778 Report.Error (531, p.Location, "`{0}.get': interface members cannot have a definition", p.GetSignatureForError ());
1783 if (accessor.set_or_remove != null && accessor.set_or_remove.Block != null) {
1784 Report.Error (531, p.Location, "`{0}.set': interface members cannot have a definition", p.GetSignatureForError ());
1789 if (RootContext.Documentation != null)
1790 p.DocComment = Lexer.consume_doc_comment ();
1797 CheckIdentifierToken (yyToken, GetLocation ($4));
1803 interface_event_declaration
1804 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1806 LocatedToken lt = (LocatedToken) $5;
1807 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1808 new MemberName (lt.Value, lt.Location),
1810 if (RootContext.Documentation != null)
1811 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1813 | opt_attributes opt_new EVENT type error {
1814 CheckIdentifierToken (yyToken, GetLocation ($5));
1817 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1818 LocatedToken lt = (LocatedToken) $5;
1819 Report.Error (68, lt.Location, "`{0}.{1}': event in interface cannot have initializer", current_container.Name, lt.Value);
1822 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1824 implicit_value_parameter_type = (Expression) $4;
1825 lexer.EventParsing = true;
1827 event_accessor_declarations
1829 lexer.EventParsing = false;
1830 implicit_value_parameter_type = null;
1833 Report.Error (69, (Location) $3, "Event in interface cannot have add or remove accessors");
1838 interface_indexer_declaration
1839 : opt_attributes opt_new type THIS
1840 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1843 lexer.PropertyParsing = true;
1844 implicit_value_parameter_type = (Expression)$3;
1846 accessor_declarations
1848 has_get = has_set = false;
1849 lexer.PropertyParsing = false;
1850 implicit_value_parameter_type = null;
1856 i = new Indexer (current_class, (Expression) $3,
1857 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1858 (int) $2, true, (Parameters) $6, (Attributes) $1,
1861 Report.Error (548, i.Location, "`{0}': property or indexer must have at least one accessor", i.GetSignatureForError ());
1865 Accessors accessors = (Accessors) $10;
1866 i = new Indexer (current_class, (Expression) $3,
1867 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1868 (int) $2, true, (Parameters) $6, (Attributes) $1,
1869 accessors.get_or_add, accessors.set_or_remove, accessors.declared_in_reverse);
1871 if (accessors.get_or_add != null && accessors.get_or_add.Block != null) {
1872 Report.Error (531, i.Location, "`{0}.get': interface members cannot have a definition", i.GetSignatureForError ());
1877 if (accessors.set_or_remove != null && accessors.set_or_remove.Block != null) {
1878 Report.Error (531, i.Location, "`{0}.set': interface members cannot have a definition", i.GetSignatureForError ());
1883 if (RootContext.Documentation != null)
1884 i.DocComment = ConsumeStoredComment ();
1890 operator_declaration
1891 : opt_attributes opt_modifiers operator_declarator
1893 anonymous_host = SimpleAnonymousHost.GetSimple ();
1900 OperatorDeclaration decl = (OperatorDeclaration) $3;
1902 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1904 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null, decl.location);
1905 if (decl.arg2type != null)
1906 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null, decl.location);
1908 Operator op = new Operator (
1909 current_class, decl.optype, decl.ret_type, (int) $2,
1910 new Parameters (param_list),
1911 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1913 if (RootContext.Documentation != null) {
1914 op.DocComment = tmpComment;
1915 Lexer.doc_state = XmlCommentState.Allowed;
1918 SimpleAnonymousHost.Simple.Propagate (op);
1919 anonymous_host = null;
1921 // Note again, checking is done in semantic analysis
1922 current_container.AddOperator (op);
1924 current_local_parameters = null;
1930 | SEMICOLON { $$ = null; }
1933 : type OPERATOR overloadable_operator
1934 open_parens opt_attributes opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
1936 // TODO: wrong location
1937 if ((Parameter.Modifier)$6 != Parameter.Modifier.NONE)
1938 Error_ParameterModifierNotValid ((Location) $2);
1940 LocatedToken lt = (LocatedToken) $8;
1941 Operator.OpType op = (Operator.OpType) $3;
1942 CheckUnaryOperator (op, lt.Location);
1944 if (op == Operator.OpType.Addition)
1945 op = Operator.OpType.UnaryPlus;
1947 if (op == Operator.OpType.Subtraction)
1948 op = Operator.OpType.UnaryNegation;
1950 Parameter [] pars = new Parameter [1];
1951 Expression type = (Expression) $7;
1953 pars [0] = new Parameter (type, lt.Value, Parameter.Modifier.NONE, (Attributes) $5, lt.Location);
1955 current_local_parameters = new Parameters (pars);
1957 if (RootContext.Documentation != null) {
1958 tmpComment = Lexer.consume_doc_comment ();
1959 Lexer.doc_state = XmlCommentState.NotAllowed;
1962 $$ = new OperatorDeclaration (op, (Expression) $1, type, lt.Value,
1963 null, null, (Location) $2);
1965 | type OPERATOR overloadable_operator
1967 opt_attributes opt_parameter_modifier type IDENTIFIER COMMA
1968 opt_attributes opt_parameter_modifier type IDENTIFIER
1971 // TODO: wrong location
1972 if ((Parameter.Modifier)$6 != Parameter.Modifier.NONE || (Parameter.Modifier)$11 != Parameter.Modifier.NONE)
1973 Error_ParameterModifierNotValid ((Location) $2);
1975 LocatedToken ltParam1 = (LocatedToken) $8;
1976 LocatedToken ltParam2 = (LocatedToken) $13;
1977 CheckBinaryOperator ((Operator.OpType) $3, (Location) $2);
1979 Parameter [] pars = new Parameter [2];
1981 Expression typeL = (Expression) $7;
1982 Expression typeR = (Expression) $12;
1984 pars [0] = new Parameter (typeL, ltParam1.Value, Parameter.Modifier.NONE, (Attributes) $5, ltParam1.Location);
1985 pars [1] = new Parameter (typeR, ltParam2.Value, Parameter.Modifier.NONE, (Attributes) $10, ltParam2.Location);
1987 current_local_parameters = new Parameters (pars);
1989 if (RootContext.Documentation != null) {
1990 tmpComment = Lexer.consume_doc_comment ();
1991 Lexer.doc_state = XmlCommentState.NotAllowed;
1994 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1995 typeL, ltParam1.Value,
1996 typeR, ltParam2.Value, (Location) $2);
1998 | conversion_operator_declarator
1999 | type OPERATOR overloadable_operator
2001 opt_attributes opt_parameter_modifier type IDENTIFIER COMMA
2002 opt_attributes opt_parameter_modifier type IDENTIFIER COMMA error
2004 Report.Error (1534, (Location) $2, "Overloaded binary operator `{0}' takes two parameters",
2005 Operator.GetName ((Operator.OpType) $3));
2008 | type OPERATOR overloadable_operator
2009 open_parens CLOSE_PARENS
2011 Report.Error (1535, (Location) $2, "Overloaded unary operator `{0}' takes one parameter",
2012 Operator.GetName ((Operator.OpType) $3));
2017 overloadable_operator
2019 : BANG { $$ = Operator.OpType.LogicalNot; }
2020 | TILDE { $$ = Operator.OpType.OnesComplement; }
2021 | OP_INC { $$ = Operator.OpType.Increment; }
2022 | OP_DEC { $$ = Operator.OpType.Decrement; }
2023 | TRUE { $$ = Operator.OpType.True; }
2024 | FALSE { $$ = Operator.OpType.False; }
2025 // Unary and binary:
2026 | PLUS { $$ = Operator.OpType.Addition; }
2027 | MINUS { $$ = Operator.OpType.Subtraction; }
2029 | STAR { $$ = Operator.OpType.Multiply; }
2030 | DIV { $$ = Operator.OpType.Division; }
2031 | PERCENT { $$ = Operator.OpType.Modulus; }
2032 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
2033 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
2034 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
2035 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
2036 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
2037 | OP_EQ { $$ = Operator.OpType.Equality; }
2038 | OP_NE { $$ = Operator.OpType.Inequality; }
2039 | OP_GT { $$ = Operator.OpType.GreaterThan; }
2040 | OP_LT { $$ = Operator.OpType.LessThan; }
2041 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
2042 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
2045 conversion_operator_declarator
2046 : IMPLICIT OPERATOR type open_parens opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
2048 // TODO: wrong location
2049 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
2050 Error_ParameterModifierNotValid (GetLocation ($4));
2052 LocatedToken lt = (LocatedToken) $7;
2053 Parameter [] pars = new Parameter [1];
2055 pars [0] = new Parameter ((Expression) $6, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2057 current_local_parameters = new Parameters (pars);
2059 if (RootContext.Documentation != null) {
2060 tmpComment = Lexer.consume_doc_comment ();
2061 Lexer.doc_state = XmlCommentState.NotAllowed;
2064 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $6, lt.Value,
2065 null, null, (Location) $2);
2067 | EXPLICIT OPERATOR type open_parens opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
2069 // TODO: wrong location
2070 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
2071 Error_ParameterModifierNotValid (GetLocation ($4));
2073 LocatedToken lt = (LocatedToken) $7;
2074 Parameter [] pars = new Parameter [1];
2076 pars [0] = new Parameter ((Expression) $6, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2078 current_local_parameters = new Parameters (pars);
2080 if (RootContext.Documentation != null) {
2081 tmpComment = Lexer.consume_doc_comment ();
2082 Lexer.doc_state = XmlCommentState.NotAllowed;
2085 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $6, lt.Value,
2086 null, null, (Location) $2);
2090 syntax_error ((Location) $1, "'operator' expected");
2094 syntax_error ((Location) $1, "'operator' expected");
2098 constructor_declaration
2101 constructor_declarator
2104 Constructor c = (Constructor) $3;
2105 c.Block = (ToplevelBlock) $4;
2106 c.OptAttributes = (Attributes) $1;
2107 c.ModFlags = (int) $2;
2109 if (RootContext.Documentation != null)
2110 c.DocComment = ConsumeStoredComment ();
2112 if (c.Name == current_container.Basename){
2113 if ((c.ModFlags & Modifiers.STATIC) != 0){
2114 if ((c.ModFlags & Modifiers.Accessibility) != 0){
2115 Report.Error (515, c.Location,
2116 "`{0}': access modifiers are not allowed on static constructors",
2117 c.GetSignatureForError ());
2120 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2122 if (c.Initializer != null){
2123 Report.Error (514, c.Location,
2124 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2125 c.GetSignatureForError ());
2128 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2131 // We let another layer check the validity of the constructor.
2132 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
2135 current_container.AddConstructor (c);
2137 current_local_parameters = null;
2138 if (RootContext.Documentation != null)
2139 Lexer.doc_state = XmlCommentState.Allowed;
2143 constructor_declarator
2144 : constructor_header
2148 | constructor_header constructor_initializer
2150 ((Constructor)$1).Initializer = (ConstructorInitializer) $2;
2158 if (RootContext.Documentation != null) {
2159 tmpComment = Lexer.consume_doc_comment ();
2160 Lexer.doc_state = XmlCommentState.Allowed;
2163 open_parens opt_formal_parameter_list CLOSE_PARENS
2165 LocatedToken lt = (LocatedToken) $1;
2166 current_local_parameters = (Parameters) $4;
2167 current_block = new ToplevelBlock (null, current_local_parameters, null, lt.Location);
2169 $$ = new Constructor (current_class, lt.Value, 0, current_local_parameters,
2172 anonymous_host = (IAnonymousHost) $$;
2178 | SEMICOLON { current_block = null; $$ = null; }
2181 constructor_initializer
2182 : COLON BASE open_parens opt_argument_list CLOSE_PARENS
2184 $$ = new ConstructorBaseInitializer ((ArrayList) $4, (Location) $2);
2186 | COLON THIS open_parens opt_argument_list CLOSE_PARENS
2188 $$ = new ConstructorThisInitializer ((ArrayList) $4, (Location) $2);
2191 Report.Error (1018, (Location) $1, "Keyword this or base expected");
2197 : /* EMPTY */ { $$ = 0; }
2198 | UNSAFE { $$ = Modifiers.UNSAFE; }
2199 | EXTERN { $$ = Modifiers.EXTERN; }
2202 destructor_declaration
2203 : opt_attributes opt_finalizer TILDE
2205 if (RootContext.Documentation != null) {
2206 tmpComment = Lexer.consume_doc_comment ();
2207 Lexer.doc_state = XmlCommentState.NotAllowed;
2210 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2212 LocatedToken lt = (LocatedToken) $5;
2213 if (lt.Value != current_container.Basename){
2214 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2215 } else if (current_container.Kind != Kind.Class){
2216 Report.Error (575, lt.Location, "Only class types can contain destructor");
2218 Location l = lt.Location;
2221 if (!RootContext.StdLib && current_container.Name == "System.Object")
2222 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2224 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2226 Method d = new Destructor (
2227 current_class, TypeManager.system_void_expr, m, "Finalize",
2228 Parameters.EmptyReadOnlyParameters, (Attributes) $1, l);
2229 if (RootContext.Documentation != null)
2230 d.DocComment = ConsumeStoredComment ();
2232 d.Block = (ToplevelBlock) $8;
2233 current_container.AddMethod (d);
2241 EVENT type variable_declarators SEMICOLON
2243 current_array_type = null;
2244 foreach (VariableDeclaration var in (ArrayList) $5) {
2246 MemberName name = new MemberName (var.identifier,
2249 EventField e = new EventField (
2250 current_class, (Expression) $4, (int) $2, false, name,
2253 e.Initializer = var.expression_or_array_initializer;
2255 current_container.AddEvent (e);
2257 if (RootContext.Documentation != null) {
2258 e.DocComment = Lexer.consume_doc_comment ();
2259 Lexer.doc_state = XmlCommentState.Allowed;
2265 EVENT type namespace_or_type_name
2268 implicit_value_parameter_type = (Expression) $4;
2269 lexer.EventParsing = true;
2271 event_accessor_declarations
2273 lexer.EventParsing = false;
2277 MemberName name = (MemberName) $5;
2280 Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2281 current_container.Name, name.ToString ());
2284 Accessors accessors = (Accessors) $8;
2285 if (accessors.get_or_add == null || accessors.set_or_remove == null)
2286 // CS0073 is already reported, so no CS0065 here.
2289 Event e = new EventProperty (
2290 current_class, (Expression) $4, (int) $2, false, name,
2291 (Attributes) $1, accessors.get_or_add, accessors.set_or_remove);
2292 if (RootContext.Documentation != null) {
2293 e.DocComment = Lexer.consume_doc_comment ();
2294 Lexer.doc_state = XmlCommentState.Allowed;
2297 current_container.AddEvent (e);
2298 implicit_value_parameter_type = null;
2302 | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
2303 MemberName mn = (MemberName) $5;
2305 if (mn.Left != null)
2306 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2308 Report.Error (71, mn.Location, "Event declaration should use property syntax");
2310 if (RootContext.Documentation != null)
2311 Lexer.doc_state = XmlCommentState.Allowed;
2315 event_accessor_declarations
2316 : add_accessor_declaration remove_accessor_declaration
2318 $$ = new Accessors ((Accessor) $1, (Accessor) $2);
2320 | remove_accessor_declaration add_accessor_declaration
2322 Accessors accessors = new Accessors ((Accessor) $2, (Accessor) $1);
2323 accessors.declared_in_reverse = true;
2326 | add_accessor_declaration { $$ = null; }
2327 | remove_accessor_declaration { $$ = null; }
2330 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2336 add_accessor_declaration
2337 : opt_attributes ADD
2339 Parameter [] args = new Parameter [1];
2340 Parameter implicit_value_parameter = new Parameter (
2341 implicit_value_parameter_type, "value",
2342 Parameter.Modifier.NONE, null, (Location) $2);
2344 args [0] = implicit_value_parameter;
2346 current_local_parameters = new Parameters (args);
2347 lexer.EventParsing = false;
2349 anonymous_host = SimpleAnonymousHost.GetSimple ();
2353 Accessor accessor = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2354 lexer.EventParsing = true;
2356 current_local_parameters = null;
2357 SimpleAnonymousHost.Simple.Propagate (accessor);
2358 anonymous_host = null;
2362 | opt_attributes ADD error {
2363 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2366 | opt_attributes modifiers ADD {
2367 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2372 remove_accessor_declaration
2373 : opt_attributes REMOVE
2375 Parameter [] args = new Parameter [1];
2376 Parameter implicit_value_parameter = new Parameter (
2377 implicit_value_parameter_type, "value",
2378 Parameter.Modifier.NONE, null, (Location) $2);
2380 args [0] = implicit_value_parameter;
2382 current_local_parameters = new Parameters (args);
2383 lexer.EventParsing = false;
2387 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2388 lexer.EventParsing = true;
2390 | opt_attributes REMOVE error {
2391 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2394 | opt_attributes modifiers REMOVE {
2395 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2401 : opt_attributes opt_modifiers indexer_declarator
2404 IndexerDeclaration decl = (IndexerDeclaration) $3;
2406 implicit_value_parameter_type = decl.type;
2408 lexer.PropertyParsing = true;
2409 parsing_indexer = true;
2411 indexer_parameters = decl.param_list;
2412 anonymous_host = SimpleAnonymousHost.GetSimple ();
2414 accessor_declarations
2416 lexer.PropertyParsing = false;
2417 has_get = has_set = false;
2418 parsing_indexer = false;
2425 // The signature is computed from the signature of the indexer. Look
2426 // at section 3.6 on the spec
2428 IndexerDeclaration decl = (IndexerDeclaration) $3;
2429 Location loc = decl.location;
2430 Accessors accessors = (Accessors) $6;
2431 Accessor get_block = accessors.get_or_add;
2432 Accessor set_block = accessors.set_or_remove;
2435 if (decl.interface_type != null)
2436 name = new MemberName (decl.interface_type, TypeContainer.DefaultIndexerName, loc);
2438 name = new MemberName (TypeContainer.DefaultIndexerName, loc);
2440 indexer = new Indexer (current_class, decl.type, name,
2441 (int) $2, false, decl.param_list, (Attributes) $1,
2442 get_block, set_block, accessors.declared_in_reverse);
2444 if (RootContext.Documentation != null)
2445 indexer.DocComment = ConsumeStoredComment ();
2447 current_container.AddIndexer (indexer);
2449 current_local_parameters = null;
2450 implicit_value_parameter_type = null;
2451 indexer_parameters = null;
2456 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2458 Parameters pars = (Parameters) $4;
2459 if (pars.HasArglist) {
2460 // "__arglist is not valid in this context"
2461 Report.Error (1669, (Location) $2, "__arglist is not valid in this context");
2462 } else if (pars.Empty){
2463 Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
2465 if (RootContext.Documentation != null) {
2466 tmpComment = Lexer.consume_doc_comment ();
2467 Lexer.doc_state = XmlCommentState.Allowed;
2470 $$ = new IndexerDeclaration ((Expression) $1, null, pars, (Location) $2);
2472 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2474 Parameters pars = (Parameters) $6;
2476 if (pars.HasArglist) {
2477 // "__arglist is not valid in this context"
2478 Report.Error (1669, (Location) $4, "__arglist is not valid in this context");
2479 } else if (pars.Empty){
2480 Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
2483 MemberName name = (MemberName) $2;
2484 $$ = new IndexerDeclaration ((Expression) $1, name, pars, (Location) $4);
2486 if (RootContext.Documentation != null) {
2487 tmpComment = Lexer.consume_doc_comment ();
2488 Lexer.doc_state = XmlCommentState.Allowed;
2498 if (RootContext.Documentation != null)
2499 enumTypeComment = Lexer.consume_doc_comment ();
2504 LocatedToken lt = (LocatedToken) $4;
2505 Location enum_location = lt.Location;
2507 MemberName name = MakeName (new MemberName (lt.Value, enum_location));
2508 Enum e = new Enum (current_namespace, current_class, (Expression) $5, (int) $2,
2509 name, (Attributes) $1);
2511 if (RootContext.Documentation != null)
2512 e.DocComment = enumTypeComment;
2515 EnumMember em = null;
2516 foreach (VariableDeclaration ev in (ArrayList) $7) {
2517 em = new EnumMember (
2518 e, em, ev.identifier, (Expression) ev.expression_or_array_initializer,
2519 ev.OptAttributes, ev.Location);
2521 // if (RootContext.Documentation != null)
2522 em.DocComment = ev.DocComment;
2524 e.AddEnumMember (em);
2527 current_container.AddTypeContainer (e);
2534 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2535 | COLON type { $$ = $2; }
2541 if (RootContext.Documentation != null)
2542 Lexer.doc_state = XmlCommentState.Allowed;
2544 opt_enum_member_declarations
2546 // here will be evaluated after CLOSE_BLACE is consumed.
2547 if (RootContext.Documentation != null)
2548 Lexer.doc_state = XmlCommentState.Allowed;
2556 opt_enum_member_declarations
2557 : /* empty */ { $$ = new ArrayList (4); }
2558 | enum_member_declarations opt_comma { $$ = $1; }
2561 enum_member_declarations
2562 : enum_member_declaration
2564 ArrayList l = new ArrayList (4);
2569 | enum_member_declarations COMMA enum_member_declaration
2571 ArrayList l = (ArrayList) $1;
2579 enum_member_declaration
2580 : opt_attributes IDENTIFIER
2582 VariableDeclaration vd = new VariableDeclaration (
2583 (LocatedToken) $2, null, (Attributes) $1);
2585 if (RootContext.Documentation != null) {
2586 vd.DocComment = Lexer.consume_doc_comment ();
2587 Lexer.doc_state = XmlCommentState.Allowed;
2592 | opt_attributes IDENTIFIER
2594 if (RootContext.Documentation != null) {
2595 tmpComment = Lexer.consume_doc_comment ();
2596 Lexer.doc_state = XmlCommentState.NotAllowed;
2601 VariableDeclaration vd = new VariableDeclaration (
2602 (LocatedToken) $2, $5, (Attributes) $1);
2604 if (RootContext.Documentation != null)
2605 vd.DocComment = ConsumeStoredComment ();
2611 delegate_declaration
2618 open_parens opt_formal_parameter_list CLOSE_PARENS
2620 MemberName name = MakeName ((MemberName) $6);
2621 Parameters p = (Parameters) $8;
2623 // TODO: wrong location
2624 Report.Error (1669, name.Location, "__arglist is not valid in this context");
2627 Delegate del = new Delegate (current_namespace, current_class, (Expression) $5,
2628 (int) $2, name, p, (Attributes) $1);
2630 if (RootContext.Documentation != null) {
2631 del.DocComment = Lexer.consume_doc_comment ();
2632 Lexer.doc_state = XmlCommentState.Allowed;
2635 current_container.AddDelegate (del);
2636 current_delegate = del;
2640 $$ = current_delegate;
2642 current_delegate = null;
2646 namespace_or_type_name
2648 | IDENTIFIER DOUBLE_COLON IDENTIFIER {
2649 LocatedToken lt1 = (LocatedToken) $1;
2650 LocatedToken lt2 = (LocatedToken) $3;
2651 $$ = new MemberName (lt1.Value, lt2.Value, lt2.Location);
2653 | namespace_or_type_name DOT IDENTIFIER {
2654 LocatedToken lt = (LocatedToken) $3;
2655 $$ = new MemberName ((MemberName) $1, lt.Value);
2661 LocatedToken lt = (LocatedToken) $1;
2662 $$ = new MemberName (lt.Value, lt.Location);
2671 * Before you think of adding a return_type, notice that we have been
2672 * using two rules in the places where it matters (one rule using type
2673 * and another identical one that uses VOID as the return type). This
2674 * gets rid of a shift/reduce couple
2677 : namespace_or_type_name
2679 MemberName name = (MemberName) $1;
2680 $$ = name.GetTypeExpression ();
2692 // Note that here only unmanaged types are allowed but we
2693 // can't perform checks during this phase - we do it during
2694 // semantic analysis.
2696 $$ = new ComposedCast ((Expression) $1, "*", Lexer.Location);
2700 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2706 | non_expression_type rank_specifier
2708 Location loc = GetLocation ($1);
2710 loc = lexer.Location;
2711 $$ = new ComposedCast ((Expression) $1, (string) $2, loc);
2713 | non_expression_type STAR
2715 Location loc = GetLocation ($1);
2717 loc = lexer.Location;
2718 $$ = new ComposedCast ((Expression) $1, "*", loc);
2720 | expression rank_specifiers
2722 $$ = new ComposedCast ((Expression) $1, (string) $2);
2726 $$ = new ComposedCast ((Expression) $1, "*");
2730 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2731 // through this different path
2733 | multiplicative_expression STAR
2735 $$ = new ComposedCast ((Expression) $1, "*");
2742 ArrayList types = new ArrayList (4);
2747 | type_list COMMA type
2749 ArrayList types = (ArrayList) $1;
2757 * replaces all the productions for isolating the various
2758 * simple types, but we need this to reuse it easily in local_variable_type
2761 : OBJECT { $$ = TypeManager.system_object_expr; }
2762 | STRING { $$ = TypeManager.system_string_expr; }
2763 | BOOL { $$ = TypeManager.system_boolean_expr; }
2764 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2765 | FLOAT { $$ = TypeManager.system_single_expr; }
2766 | DOUBLE { $$ = TypeManager.system_double_expr; }
2771 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2772 | BYTE { $$ = TypeManager.system_byte_expr; }
2773 | SHORT { $$ = TypeManager.system_int16_expr; }
2774 | USHORT { $$ = TypeManager.system_uint16_expr; }
2775 | INT { $$ = TypeManager.system_int32_expr; }
2776 | UINT { $$ = TypeManager.system_uint32_expr; }
2777 | LONG { $$ = TypeManager.system_int64_expr; }
2778 | ULONG { $$ = TypeManager.system_uint64_expr; }
2779 | CHAR { $$ = TypeManager.system_char_expr; }
2780 | VOID { $$ = TypeManager.system_void_expr; }
2784 : type rank_specifiers
2786 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2);
2791 // Expressions, section 7.5
2800 MemberName mn = (MemberName) $1;
2801 $$ = mn.GetTypeExpression ();
2803 | IDENTIFIER DOUBLE_COLON IDENTIFIER
2805 LocatedToken lt1 = (LocatedToken) $1;
2806 LocatedToken lt2 = (LocatedToken) $3;
2807 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, lt2.Location);
2809 | parenthesized_expression
2811 | invocation_expression
2815 | post_increment_expression
2816 | post_decrement_expression
2820 | checked_expression
2821 | unchecked_expression
2822 | pointer_member_access
2823 | anonymous_method_expression
2830 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
2831 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); }
2832 | NULL { $$ = new NullLiteral (lexer.Location); }
2836 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
2837 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
2838 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
2843 object v = lexer.Value;
2846 $$ = new IntLiteral ((int) v, lexer.Location);
2847 } else if (v is uint)
2848 $$ = new UIntLiteral ((UInt32) v, lexer.Location);
2850 $$ = new LongLiteral ((Int64) v, lexer.Location);
2851 else if (v is ulong)
2852 $$ = new ULongLiteral ((UInt64) v, lexer.Location);
2854 Console.WriteLine ("OOPS. Unexpected result from scanner");
2859 : TRUE { $$ = new BoolLiteral (true, lexer.Location); }
2860 | FALSE { $$ = new BoolLiteral (false, lexer.Location); }
2863 parenthesized_expression_0
2864 : OPEN_PARENS expression CLOSE_PARENS
2867 lexer.Deambiguate_CloseParens ($$);
2868 // After this, the next token returned is one of
2869 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST (CLOSE_PARENS), CLOSE_PARENS_OPEN_PARENS
2870 // or CLOSE_PARENS_MINUS.
2872 | OPEN_PARENS expression error { CheckToken (1026, yyToken, "Expecting ')'", lexer.Location); }
2875 parenthesized_expression
2876 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2880 | parenthesized_expression_0 CLOSE_PARENS
2884 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2886 // If a parenthesized expression is followed by a minus, we need to wrap
2887 // the expression inside a ParenthesizedExpression for the CS0075 check
2888 // in Binary.DoResolve().
2889 $$ = new ParenthesizedExpression ((Expression) $1);
2894 : primary_expression DOT IDENTIFIER
2896 LocatedToken lt = (LocatedToken) $3;
2897 $$ = new MemberAccess ((Expression) $1, lt.Value);
2899 | predefined_type DOT IDENTIFIER
2901 LocatedToken lt = (LocatedToken) $3;
2902 // TODO: Location is wrong as some predefined types doesn't hold a location
2903 $$ = new MemberAccess ((Expression) $1, lt.Value, lt.Location);
2911 invocation_expression
2912 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2915 Report.Error (1, (Location) $2, "Parse error");
2917 $$ = new Invocation ((Expression) $1, (ArrayList) $3);
2919 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2921 $$ = new Invocation ((Expression) $1, new ArrayList ());
2923 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2925 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
2927 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2929 ArrayList args = new ArrayList (1);
2931 $$ = new Invocation ((Expression) $1, args);
2933 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2935 ArrayList args = ((ArrayList) $4);
2937 $$ = new Invocation ((Expression) $1, args);
2942 : /* empty */ { $$ = null; }
2949 ArrayList list = new ArrayList (4);
2953 | argument_list COMMA argument
2955 ArrayList list = (ArrayList) $1;
2959 | argument_list error {
2960 CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
2968 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2970 | non_simple_argument
2977 : REF variable_reference
2979 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2981 | OUT variable_reference
2983 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2985 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2987 ArrayList list = (ArrayList) $3;
2988 Argument[] args = new Argument [list.Count];
2989 list.CopyTo (args, 0);
2991 Expression expr = new Arglist (args, (Location) $1);
2992 $$ = new Argument (expr, Argument.AType.Expression);
2994 | ARGLIST OPEN_PARENS CLOSE_PARENS
2996 $$ = new Argument (new Arglist ((Location) $1), Argument.AType.Expression);
3000 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
3005 : expression { note ("section 5.4"); $$ = $1; }
3009 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
3011 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
3013 | primary_expression rank_specifiers
3015 // So the super-trick is that primary_expression
3016 // can only be either a SimpleName or a MemberAccess.
3017 // The MemberAccess case arises when you have a fully qualified type-name like :
3019 // SimpleName is when you have
3022 Expression expr = (Expression) $1;
3023 if (expr is ComposedCast){
3024 $$ = new ComposedCast (expr, (string) $2);
3025 } else if (!(expr is SimpleName || expr is MemberAccess || expr is QualifiedAliasMember)){
3026 Error_ExpectingTypeName (expr);
3027 $$ = TypeManager.system_object_expr;
3030 // So we extract the string corresponding to the SimpleName
3033 $$ = new ComposedCast (expr, (string) $2);
3035 current_array_type = (Expression)$$;
3042 ArrayList list = new ArrayList (4);
3046 | expression_list COMMA expression
3048 ArrayList list = (ArrayList) $1;
3057 $$ = new This (current_block, (Location) $1);
3062 : BASE DOT IDENTIFIER
3064 LocatedToken lt = (LocatedToken) $3;
3065 $$ = new BaseAccess (lt.Value, lt.Location);
3067 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
3069 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
3072 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
3077 post_increment_expression
3078 : primary_expression OP_INC
3080 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
3081 (Expression) $1, (Location) $2);
3085 post_decrement_expression
3086 : primary_expression OP_DEC
3088 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
3089 (Expression) $1, (Location) $2);
3094 : object_or_delegate_creation_expression
3095 | array_creation_expression
3098 object_or_delegate_creation_expression
3099 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
3101 $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
3105 array_creation_expression
3106 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
3108 opt_array_initializer
3110 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
3112 | NEW type rank_specifiers array_initializer
3114 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, (Location) $1);
3118 Report.Error (1031, (Location) $1, "Type expected");
3123 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
3140 : rank_specifier opt_rank_specifier
3142 $$ = (string) $2 + (string) $1;
3147 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3149 $$ = "[" + (string) $2 + "]";
3169 | dim_separators COMMA
3171 $$ = (string) $1 + ",";
3175 opt_array_initializer
3187 : OPEN_BRACE CLOSE_BRACE
3189 ArrayList list = new ArrayList (4);
3192 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3194 $$ = (ArrayList) $2;
3198 variable_initializer_list
3199 : variable_initializer
3201 ArrayList list = new ArrayList (4);
3205 | variable_initializer_list COMMA variable_initializer
3207 ArrayList list = (ArrayList) $1;
3216 pushed_current_array_type = current_array_type;
3218 OPEN_PARENS type CLOSE_PARENS
3220 Expression type = (Expression)$4;
3221 if (type == TypeManager.system_void_expr)
3222 $$ = new TypeOfVoid ((Location) $1);
3224 $$ = new TypeOf (type, (Location) $1);
3225 current_array_type = pushed_current_array_type;
3230 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3231 $$ = new SizeOf ((Expression) $3, (Location) $1);
3236 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3238 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3242 unchecked_expression
3243 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3245 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3249 pointer_member_access
3250 : primary_expression OP_PTR IDENTIFIER
3253 LocatedToken lt = (LocatedToken) $3;
3255 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
3256 $$ = new MemberAccess (deref, lt.Value);
3260 anonymous_method_expression
3261 : DELEGATE opt_anonymous_method_signature
3263 start_anonymous (false, (Parameters) $2, (Location) $1);
3267 $$ = end_anonymous ((ToplevelBlock) $4, (Location) $1);
3271 opt_anonymous_method_signature
3272 : /* empty */ { $$ = null; }
3273 | anonymous_method_signature
3276 anonymous_method_signature
3277 : open_parens opt_anonymous_method_parameter_list CLOSE_PARENS
3280 $$ = Parameters.EmptyReadOnlyParameters;
3282 ArrayList par_list = (ArrayList) $2;
3283 Parameter [] pars = new Parameter [par_list.Count];
3284 par_list.CopyTo (pars);
3285 $$ = new Parameters (pars);
3290 opt_anonymous_method_parameter_list
3291 : /* empty */ { $$ = null; }
3292 | anonymous_method_parameter_list { $$ = $1; }
3295 anonymous_method_parameter_list
3296 : anonymous_method_parameter
3298 ArrayList a = new ArrayList (4);
3302 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3304 ArrayList a = (ArrayList) $1;
3310 anonymous_method_parameter
3311 : opt_parameter_modifier type IDENTIFIER {
3312 LocatedToken lt = (LocatedToken) $3;
3313 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3315 | PARAMS type IDENTIFIER {
3316 Report.Error (1670, ((LocatedToken) $3).Location, "The `params' modifier is not allowed in anonymous method declaration");
3322 : primary_expression
3323 | BANG prefixed_unary_expression
3325 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3327 | TILDE prefixed_unary_expression
3329 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3335 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3337 $$ = new Cast ((Expression) $1, (Expression) $3);
3339 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3341 $$ = new Cast ((Expression) $1, (Expression) $3);
3347 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3349 // TODO: wrong location
3350 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3355 // The idea to split this out is from Rhys' grammar
3356 // to solve the problem with casts.
3358 prefixed_unary_expression
3360 | PLUS prefixed_unary_expression
3362 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3364 | MINUS prefixed_unary_expression
3366 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3368 | OP_INC prefixed_unary_expression
3370 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3371 (Expression) $2, (Location) $1);
3373 | OP_DEC prefixed_unary_expression
3375 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3376 (Expression) $2, (Location) $1);
3378 | STAR prefixed_unary_expression
3380 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, (Location) $1);
3382 | BITWISE_AND prefixed_unary_expression
3384 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3388 pre_increment_expression
3389 : OP_INC prefixed_unary_expression
3391 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3392 (Expression) $2, (Location) $1);
3396 pre_decrement_expression
3397 : OP_DEC prefixed_unary_expression
3399 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3400 (Expression) $2, (Location) $1);
3404 multiplicative_expression
3405 : prefixed_unary_expression
3406 | multiplicative_expression STAR prefixed_unary_expression
3408 $$ = new Binary (Binary.Operator.Multiply,
3409 (Expression) $1, (Expression) $3);
3411 | multiplicative_expression DIV prefixed_unary_expression
3413 $$ = new Binary (Binary.Operator.Division,
3414 (Expression) $1, (Expression) $3);
3416 | multiplicative_expression PERCENT prefixed_unary_expression
3418 $$ = new Binary (Binary.Operator.Modulus,
3419 (Expression) $1, (Expression) $3);
3424 : multiplicative_expression
3425 | additive_expression PLUS multiplicative_expression
3427 $$ = new Binary (Binary.Operator.Addition,
3428 (Expression) $1, (Expression) $3);
3430 | additive_expression MINUS multiplicative_expression
3432 $$ = new Binary (Binary.Operator.Subtraction,
3433 (Expression) $1, (Expression) $3);
3438 : additive_expression
3439 | shift_expression OP_SHIFT_LEFT additive_expression
3441 $$ = new Binary (Binary.Operator.LeftShift,
3442 (Expression) $1, (Expression) $3);
3444 | shift_expression OP_SHIFT_RIGHT additive_expression
3446 $$ = new Binary (Binary.Operator.RightShift,
3447 (Expression) $1, (Expression) $3);
3451 relational_expression
3453 | relational_expression OP_LT shift_expression
3455 $$ = new Binary (Binary.Operator.LessThan,
3456 (Expression) $1, (Expression) $3);
3458 | relational_expression OP_GT shift_expression
3460 $$ = new Binary (Binary.Operator.GreaterThan,
3461 (Expression) $1, (Expression) $3);
3463 | relational_expression OP_LE shift_expression
3465 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3466 (Expression) $1, (Expression) $3);
3468 | relational_expression OP_GE shift_expression
3470 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3471 (Expression) $1, (Expression) $3);
3473 | relational_expression IS type
3475 $$ = new Is ((Expression) $1, (Expression) $3, (Location) $2);
3477 | relational_expression AS type
3479 $$ = new As ((Expression) $1, (Expression) $3, (Location) $2);
3484 : relational_expression
3485 | equality_expression OP_EQ relational_expression
3487 $$ = new Binary (Binary.Operator.Equality,
3488 (Expression) $1, (Expression) $3);
3490 | equality_expression OP_NE relational_expression
3492 $$ = new Binary (Binary.Operator.Inequality,
3493 (Expression) $1, (Expression) $3);
3498 : equality_expression
3499 | and_expression BITWISE_AND equality_expression
3501 $$ = new Binary (Binary.Operator.BitwiseAnd,
3502 (Expression) $1, (Expression) $3);
3506 exclusive_or_expression
3508 | exclusive_or_expression CARRET and_expression
3510 $$ = new Binary (Binary.Operator.ExclusiveOr,
3511 (Expression) $1, (Expression) $3);
3515 inclusive_or_expression
3516 : exclusive_or_expression
3517 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3519 $$ = new Binary (Binary.Operator.BitwiseOr,
3520 (Expression) $1, (Expression) $3);
3524 conditional_and_expression
3525 : inclusive_or_expression
3526 | conditional_and_expression OP_AND inclusive_or_expression
3528 $$ = new Binary (Binary.Operator.LogicalAnd,
3529 (Expression) $1, (Expression) $3);
3533 conditional_or_expression
3534 : conditional_and_expression
3535 | conditional_or_expression OP_OR conditional_and_expression
3537 $$ = new Binary (Binary.Operator.LogicalOr,
3538 (Expression) $1, (Expression) $3);
3542 conditional_expression
3543 : conditional_or_expression
3544 | conditional_or_expression INTERR expression COLON expression
3546 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
3550 assignment_expression
3551 : prefixed_unary_expression ASSIGN expression
3553 $$ = new Assign ((Expression) $1, (Expression) $3);
3555 | prefixed_unary_expression OP_MULT_ASSIGN expression
3557 $$ = new CompoundAssign (
3558 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
3560 | prefixed_unary_expression OP_DIV_ASSIGN expression
3562 $$ = new CompoundAssign (
3563 Binary.Operator.Division, (Expression) $1, (Expression) $3);
3565 | prefixed_unary_expression OP_MOD_ASSIGN expression
3567 $$ = new CompoundAssign (
3568 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
3570 | prefixed_unary_expression OP_ADD_ASSIGN expression
3572 $$ = new CompoundAssign (
3573 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
3575 | prefixed_unary_expression OP_SUB_ASSIGN expression
3577 $$ = new CompoundAssign (
3578 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3580 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3582 $$ = new CompoundAssign (
3583 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
3585 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3587 $$ = new CompoundAssign (
3588 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
3590 | prefixed_unary_expression OP_AND_ASSIGN expression
3592 $$ = new CompoundAssign (
3593 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
3595 | prefixed_unary_expression OP_OR_ASSIGN expression
3597 $$ = new CompoundAssign (
3598 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
3600 | prefixed_unary_expression OP_XOR_ASSIGN expression
3602 $$ = new CompoundAssign (
3603 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
3607 implicitly_typed_lambda_parameter_list
3609 LocatedToken lt = (LocatedToken) $1;
3610 ArrayList a = new ArrayList (4);
3612 a.Add (new Parameter ((Expression)null, lt.Value, Parameter.Modifier.NONE, null, lt.Location));
3615 | implicitly_typed_lambda_parameter_list COMMA IDENTIFIER {
3616 LocatedToken lt = (LocatedToken) $3;
3617 ArrayList a = (ArrayList) $1;
3618 a.Add (new Parameter ((Expression)null, lt.Value, Parameter.Modifier.NONE, null, lt.Location));
3623 explicitly_typed_lambda_parameter_list
3624 : explicitly_typed_lambda_parameter
3626 ArrayList pars = new ArrayList (4);
3631 | explicitly_typed_lambda_parameter_list COMMA explicitly_typed_lambda_parameter
3633 ArrayList pars = (ArrayList) $1;
3640 explicitly_typed_lambda_parameter
3641 : parameter_modifier type IDENTIFIER
3643 LocatedToken lt = (LocatedToken) $3;
3645 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3649 LocatedToken lt = (LocatedToken) $2;
3651 $$ = new Parameter ((Expression) $1, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
3655 lambda_parameter_list
3656 : implicitly_typed_lambda_parameter_list { $$ = $1; }
3657 | explicitly_typed_lambda_parameter_list { $$ = $1; }
3660 opt_lambda_parameter_list
3661 : /* empty */ { $$ = null; }
3662 | lambda_parameter_list {
3663 ArrayList pars_list = (ArrayList) $1;
3665 Parameter [] pars = new Parameter [pars_list.Count];
3666 pars_list.CopyTo (pars);
3667 $$ = new Parameters (pars);
3671 lambda_expression_body
3673 start_block (lexer.Location);
3677 Block b = end_block (lexer.Location);
3678 b.AddStatement (new ContextualReturn ((Expression) $2));
3689 Parameter [] pars = new Parameter [1];
3690 LocatedToken lt = (LocatedToken) $1;
3691 pars [0] = new Parameter ((Expression)null, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
3693 Parameters parameters = new Parameters (pars);
3695 start_anonymous (true, parameters, (Location) $2);
3697 lambda_expression_body
3699 $$ = end_anonymous ((ToplevelBlock) $4, (Location) $2);
3701 | OPEN_PARENS_LAMBDA opt_lambda_parameter_list CLOSE_PARENS ARROW
3703 start_anonymous (true, (Parameters) $2, (Location) $4);
3705 lambda_expression_body
3707 $$ = end_anonymous ((ToplevelBlock) $6, (Location) $4);
3712 : conditional_expression
3713 | assignment_expression
3737 MemberName name = MakeName ((MemberName) $6);
3738 push_current_class (new Class (current_namespace, current_class, name, (int) $2, (Attributes) $1), $3);
3742 if (RootContext.Documentation != null) {
3743 current_container.DocComment = Lexer.consume_doc_comment ();
3744 Lexer.doc_state = XmlCommentState.Allowed;
3749 if (RootContext.Documentation != null)
3750 Lexer.doc_state = XmlCommentState.Allowed;
3754 $$ = pop_current_class ();
3762 { $$ = $1; } // location
3766 : /* empty */ { $$ = (int) 0; }
3772 | modifiers modifier
3777 if ((m1 & m2) != 0) {
3778 Location l = lexer.Location;
3779 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
3781 $$ = (int) (m1 | m2);
3786 : NEW { $$ = Modifiers.NEW; }
3787 | PUBLIC { $$ = Modifiers.PUBLIC; }
3788 | PROTECTED { $$ = Modifiers.PROTECTED; }
3789 | INTERNAL { $$ = Modifiers.INTERNAL; }
3790 | PRIVATE { $$ = Modifiers.PRIVATE; }
3791 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3792 | SEALED { $$ = Modifiers.SEALED; }
3793 | STATIC { $$ = Modifiers.STATIC; }
3794 | READONLY { $$ = Modifiers.READONLY; }
3795 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3796 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3797 | EXTERN { $$ = Modifiers.EXTERN; }
3798 | VOLATILE { $$ = Modifiers.VOLATILE; }
3799 | UNSAFE { $$ = Modifiers.UNSAFE; }
3808 : COLON type_list { current_container.AddBasesForPart (current_class, (ArrayList) $2); }
3816 // A block is "contained" on the following places:
3818 // property_declaration as part of the accessor body (get/set)
3819 // operator_declaration
3820 // constructor_declaration
3821 // destructor_declaration
3822 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3827 ++lexer.parsing_block;
3828 start_block ((Location) $1);
3830 opt_statement_list CLOSE_BRACE
3832 --lexer.parsing_block;
3833 $$ = end_block ((Location) $4);
3840 ++lexer.parsing_block;
3842 opt_statement_list CLOSE_BRACE
3844 --lexer.parsing_block;
3845 $$ = end_block ((Location) $4);
3856 | statement_list statement
3860 : declaration_statement
3862 if ($1 != null && (Block) $1 != current_block){
3863 current_block.AddStatement ((Statement) $1);
3864 current_block = (Block) $1;
3867 | valid_declaration_statement
3869 current_block.AddStatement ((Statement) $1);
3874 valid_declaration_statement
3877 | expression_statement
3878 | selection_statement
3879 | iteration_statement
3883 | unchecked_statement
3891 : valid_declaration_statement
3892 | declaration_statement
3894 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3899 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3907 $$ = EmptyStatement.Value;
3914 LocatedToken lt = (LocatedToken) $1;
3915 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
3917 if (current_block.AddLabel (labeled))
3918 current_block.AddStatement (labeled);
3923 declaration_statement
3924 : local_variable_declaration SEMICOLON
3926 current_array_type = null;
3928 DictionaryEntry de = (DictionaryEntry) $1;
3929 Expression e = (Expression) de.Key;
3931 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
3935 | local_constant_declaration SEMICOLON
3937 current_array_type = null;
3939 DictionaryEntry de = (DictionaryEntry) $1;
3941 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3947 * The following is from Rhys' grammar:
3948 * > Types in local variable declarations must be recognized as
3949 * > expressions to prevent reduce/reduce errors in the grammar.
3950 * > The expressions are converted into types during semantic analysis.
3953 : primary_expression opt_rank_specifier
3955 // FIXME: Do something smart here regarding the composition of the type.
3957 // Ok, the above "primary_expression" is there to get rid of
3958 // both reduce/reduce and shift/reduces in the grammar, it should
3959 // really just be "type_name". If you use type_name, a reduce/reduce
3960 // creeps up. If you use namespace_or_type_name (which is all we need
3961 // really) two shift/reduces appear.
3964 // So the super-trick is that primary_expression
3965 // can only be either a SimpleName or a MemberAccess.
3966 // The MemberAccess case arises when you have a fully qualified type-name like :
3968 // SimpleName is when you have
3971 Expression expr = (Expression) $1;
3972 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3973 Error_ExpectingTypeName (expr);
3977 // So we extract the string corresponding to the SimpleName
3981 if ((string) $2 == "")
3984 $$ = new ComposedCast ((Expression) $1, (string) $2);
3987 | builtin_types opt_rank_specifier
3989 if ((string) $2 == "")
3992 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3996 local_variable_pointer_type
3997 : primary_expression STAR
3999 Expression expr = (Expression) $1;
4001 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
4002 Error_ExpectingTypeName (expr);
4006 $$ = new ComposedCast ((Expression) $1, "*");
4008 | builtin_types STAR
4010 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
4014 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
4016 | local_variable_pointer_type STAR
4018 $$ = new ComposedCast ((Expression) $1, "*");
4022 local_variable_declaration
4023 : local_variable_type variable_declarators
4026 $$ = new DictionaryEntry ($1, $2);
4030 | local_variable_pointer_type opt_rank_specifier variable_declarators
4035 if ((string) $2 == "")
4036 t = (Expression) $1;
4038 t = new ComposedCast ((Expression) $1, (string) $2);
4039 $$ = new DictionaryEntry (t, $3);
4045 local_constant_declaration
4046 : CONST local_variable_type constant_declarators
4049 $$ = new DictionaryEntry ($2, $3);
4055 expression_statement
4056 : statement_expression SEMICOLON { $$ = $1; }
4060 // We have to do the wrapping here and not in the case above,
4061 // because statement_expression is used for example in for_statement
4063 statement_expression
4066 Expression expr = (Expression) $1;
4067 ExpressionStatement s = expr as ExpressionStatement;
4069 expr.Error_InvalidExpressionStatement ();
4072 $$ = new StatementExpression (s);
4076 Report.Error (1002, GetLocation ($1), "Expecting `;'");
4081 object_creation_expression
4082 : object_or_delegate_creation_expression
4083 { note ("complain if this is a delegate maybe?"); }
4092 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
4095 Location l = (Location) $1;
4097 $$ = new If ((Expression) $3, (Statement) $5, l);
4099 // FIXME: location for warning should be loc property of $5.
4100 if ($5 == EmptyStatement.Value)
4101 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4104 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
4105 embedded_statement ELSE embedded_statement
4107 Location l = (Location) $1;
4109 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
4111 // FIXME: location for warning should be loc property of $5 and $7.
4112 if ($5 == EmptyStatement.Value)
4113 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4114 if ($7 == EmptyStatement.Value)
4115 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4120 : SWITCH OPEN_PARENS
4122 if (switch_stack == null)
4123 switch_stack = new Stack (2);
4124 switch_stack.Push (current_block);
4126 expression CLOSE_PARENS
4129 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
4130 current_block = (Block) switch_stack.Pop ();
4146 Report.Warning (1522, 1, lexer.Location, "Empty switch block");
4147 $$ = new ArrayList ();
4155 ArrayList sections = new ArrayList (4);
4160 | switch_sections switch_section
4162 ArrayList sections = (ArrayList) $1;
4172 current_block = current_block.CreateSwitchBlock (lexer.Location);
4176 $$ = new SwitchSection ((ArrayList) $1, current_block.Explicit);
4183 ArrayList labels = new ArrayList (4);
4188 | switch_labels switch_label
4190 ArrayList labels = (ArrayList) ($1);
4198 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
4199 | DEFAULT COLON { $$ = new SwitchLabel (null, (Location) $1); }
4202 1523, GetLocation ($1),
4203 "The keyword case or default must precede code in switch block");
4215 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
4217 Location l = (Location) $1;
4218 $$ = new While ((Expression) $3, (Statement) $5, l);
4223 : DO embedded_statement
4224 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
4226 Location l = (Location) $1;
4228 $$ = new Do ((Statement) $2, (Expression) $5, l);
4234 opt_for_initializer SEMICOLON
4236 Location l = lexer.Location;
4238 Block assign_block = current_block;
4240 if ($3 is DictionaryEntry){
4241 DictionaryEntry de = (DictionaryEntry) $3;
4243 Expression type = (Expression) de.Key;
4244 ArrayList var_declarators = (ArrayList) de.Value;
4246 foreach (VariableDeclaration decl in var_declarators){
4250 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4254 Expression expr = decl.expression_or_array_initializer;
4256 LocalVariableReference var;
4257 var = new LocalVariableReference (assign_block, decl.identifier, l);
4260 Assign a = new Assign (var, expr, decl.Location);
4262 assign_block.AddStatement (new StatementExpression (a));
4266 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4267 // This can be referred to as $5 below.
4273 opt_for_condition SEMICOLON
4274 opt_for_iterator CLOSE_PARENS
4277 Location l = (Location) $1;
4279 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4281 current_block.AddStatement (f);
4283 $$ = end_block (lexer.Location);
4288 : /* empty */ { $$ = EmptyStatement.Value; }
4293 : local_variable_declaration
4294 | statement_expression_list
4298 : /* empty */ { $$ = null; }
4299 | boolean_expression
4303 : /* empty */ { $$ = EmptyStatement.Value; }
4308 : statement_expression_list
4311 statement_expression_list
4312 : statement_expression
4314 // CHANGE: was `null'
4315 Statement s = (Statement) $1;
4316 Block b = new Block (current_block, s.loc, lexer.Location);
4321 | statement_expression_list COMMA statement_expression
4323 Block b = (Block) $1;
4325 b.AddStatement ((Statement) $3);
4331 : FOREACH open_parens type IN expression CLOSE_PARENS
4333 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4336 | FOREACH open_parens type IDENTIFIER IN
4337 expression CLOSE_PARENS
4339 start_block (lexer.Location);
4340 Block foreach_block = current_block;
4342 LocatedToken lt = (LocatedToken) $4;
4343 Location l = lt.Location;
4344 LocalInfo vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4346 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4348 // Get a writable reference to this read-only variable.
4350 // Note that the $$ here refers to the value of _this_ code block,
4351 // not the value of the LHS non-terminal. This can be referred to as $8 below.
4352 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
4359 LocalVariableReference v = (LocalVariableReference) $8;
4360 Location l = (Location) $1;
4363 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
4364 current_block.AddStatement (f);
4367 $$ = end_block (lexer.Location);
4373 | continue_statement
4383 $$ = new Break ((Location) $1);
4388 : CONTINUE SEMICOLON
4390 $$ = new Continue ((Location) $1);
4395 : GOTO IDENTIFIER SEMICOLON
4397 LocatedToken lt = (LocatedToken) $2;
4398 $$ = new Goto (lt.Value, lt.Location);
4400 | GOTO CASE constant_expression SEMICOLON
4402 $$ = new GotoCase ((Expression) $3, (Location) $1);
4404 | GOTO DEFAULT SEMICOLON
4406 $$ = new GotoDefault ((Location) $1);
4411 : RETURN opt_expression SEMICOLON
4413 $$ = new Return ((Expression) $2, (Location) $1);
4418 : THROW opt_expression SEMICOLON
4420 $$ = new Throw ((Expression) $2, (Location) $1);
4425 : IDENTIFIER RETURN expression SEMICOLON
4427 LocatedToken lt = (LocatedToken) $1;
4428 string s = lt.Value;
4430 Report.Error (1003, lt.Location, "; expected");
4433 if (RootContext.Version == LanguageVersion.ISO_1){
4434 Report.FeatureIsNotISO1 (lt.Location, "yield statement");
4437 if (anonymous_host == null){
4438 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4441 anonymous_host.SetYields ();
4442 $$ = new Yield ((Expression) $3, lt.Location);
4445 | IDENTIFIER RETURN SEMICOLON
4447 Report.Error (1627, (Location) $2, "Expression expected after yield return");
4450 | IDENTIFIER BREAK SEMICOLON
4452 LocatedToken lt = (LocatedToken) $1;
4453 string s = lt.Value;
4455 Report.Error (1003, lt.Location, "; expected");
4458 if (RootContext.Version == LanguageVersion.ISO_1){
4459 Report.FeatureIsNotISO1 (lt.Location, "yield statement");
4462 if (anonymous_host == null){
4463 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4466 anonymous_host.SetYields ();
4467 $$ = new YieldBreak (lt.Location);
4478 : TRY block catch_clauses
4482 ArrayList c = (ArrayList)$3;
4483 for (int i = 0; i < c.Count; ++i) {
4484 Catch cc = (Catch) c [i];
4486 if (i != c.Count - 1)
4487 Report.Error (1017, cc.loc, "Try statement already has an empty catch block");
4494 // Now s contains the list of specific catch clauses
4495 // and g contains the general one.
4497 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4499 | TRY block opt_catch_clauses FINALLY block
4502 ArrayList s = new ArrayList (4);
4503 ArrayList catch_list = (ArrayList) $3;
4505 if (catch_list != null){
4506 foreach (Catch cc in catch_list) {
4514 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4518 Report.Error (1524, (Location) $1, "Expected catch or finally");
4524 : /* empty */ { $$ = null; }
4531 ArrayList l = new ArrayList (4);
4536 | catch_clauses catch_clause
4538 ArrayList l = (ArrayList) $1;
4546 : /* empty */ { $$ = null; }
4551 : CATCH opt_catch_args
4553 Expression type = null;
4556 DictionaryEntry cc = (DictionaryEntry) $2;
4557 type = (Expression) cc.Key;
4558 LocatedToken lt = (LocatedToken) cc.Value;
4561 ArrayList one = new ArrayList (4);
4563 one.Add (new VariableDeclaration (lt, null));
4565 start_block (lexer.Location);
4566 current_block = declare_local_variables (type, one, lt.Location);
4570 Expression type = null;
4572 Block var_block = null;
4575 DictionaryEntry cc = (DictionaryEntry) $2;
4576 type = (Expression) cc.Key;
4577 LocatedToken lt = (LocatedToken) cc.Value;
4581 var_block = end_block (lexer.Location);
4585 $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
4590 : /* empty */ { $$ = null; }
4595 : open_parens type opt_identifier CLOSE_PARENS
4597 $$ = new DictionaryEntry ($2, $3);
4605 $$ = new Checked ((Block) $2);
4612 $$ = new Unchecked ((Block) $2);
4619 RootContext.CheckUnsafeOption ((Location) $1);
4621 $$ = new Unsafe ((Block) $3);
4627 type fixed_pointer_declarators
4630 ArrayList list = (ArrayList) $4;
4631 Expression type = (Expression) $3;
4632 Location l = (Location) $1;
4633 int top = list.Count;
4635 start_block (lexer.Location);
4637 for (int i = 0; i < top; i++){
4638 Pair p = (Pair) list [i];
4641 v = current_block.AddVariable (type, (string) p.First, l);
4645 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4653 Location l = (Location) $1;
4655 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4657 current_block.AddStatement (f);
4659 $$ = end_block (lexer.Location);
4663 fixed_pointer_declarators
4664 : fixed_pointer_declarator {
4665 ArrayList declarators = new ArrayList (4);
4667 declarators.Add ($1);
4670 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4672 ArrayList declarators = (ArrayList) $1;
4674 declarators.Add ($3);
4679 fixed_pointer_declarator
4680 : IDENTIFIER ASSIGN expression
4682 LocatedToken lt = (LocatedToken) $1;
4683 // FIXME: keep location
4684 $$ = new Pair (lt.Value, $3);
4688 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
4694 : LOCK OPEN_PARENS expression CLOSE_PARENS
4700 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
4705 : USING open_parens resource_acquisition CLOSE_PARENS
4707 start_block (lexer.Location);
4708 Block assign_block = current_block;
4710 if ($3 is DictionaryEntry){
4711 DictionaryEntry de = (DictionaryEntry) $3;
4712 Location l = (Location) $1;
4714 Expression type = (Expression) de.Key;
4715 ArrayList var_declarators = (ArrayList) de.Value;
4717 ArrayList vars = new ArrayList (4);
4719 foreach (VariableDeclaration decl in var_declarators){
4721 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4724 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
4726 Expression expr = decl.expression_or_array_initializer;
4728 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4731 LocalVariableReference var;
4733 // Get a writable reference to this read-only variable.
4734 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4736 // This is so that it is not a warning on using variables
4739 vars.Add (new DictionaryEntry (var, expr));
4741 // Assign a = new Assign (var, expr, decl.Location);
4742 // assign_block.AddStatement (new StatementExpression (a));
4745 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4746 // It can be referred to as $5 below.
4747 $$ = new DictionaryEntry (type, vars);
4754 Using u = new Using ($5, (Statement) $6, (Location) $1);
4755 current_block.AddStatement (u);
4756 $$ = end_block (lexer.Location);
4760 resource_acquisition
4761 : local_variable_declaration
4768 // A class used to pass around variable declarations and constants
4770 public class VariableDeclaration {
4771 public string identifier;
4772 public Expression expression_or_array_initializer;
4773 public Location Location;
4774 public Attributes OptAttributes;
4775 public string DocComment;
4777 public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
4779 this.identifier = lt.Value;
4780 if (eoai is ArrayList) {
4781 if (CSharpParser.current_array_type == null)
4782 Report.Error (622, lt.Location,
4783 "Can only use array initializer expressions to assign to array types. Try using a new expression instead.");
4784 this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
4786 this.expression_or_array_initializer = (Expression)eoai;
4788 this.Location = lt.Location;
4789 this.OptAttributes = opt_attrs;
4792 public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
4798 // A class used to hold info about an indexer declarator
4800 public class IndexerDeclaration {
4801 public Expression type;
4802 public MemberName interface_type;
4803 public Parameters param_list;
4804 public Location location;
4806 public IndexerDeclaration (Expression type, MemberName interface_type,
4807 Parameters param_list, Location loc)
4810 this.interface_type = interface_type;
4811 this.param_list = param_list;
4812 this.location = loc;
4817 // We use this when we do not have an object in advance that is an IAnonymousHost
4819 public class SimpleAnonymousHost : IAnonymousHost {
4820 public static readonly SimpleAnonymousHost Simple = new SimpleAnonymousHost ();
4823 ArrayList anonymous_methods;
4825 public static SimpleAnonymousHost GetSimple () {
4826 Simple.yields = false;
4827 Simple.anonymous_methods = null;
4831 public void SetYields ()
4836 public void AddAnonymousMethod (AnonymousMethodExpression anonymous)
4838 if (anonymous_methods == null)
4839 anonymous_methods = new ArrayList ();
4840 anonymous_methods.Add (anonymous);
4843 public void Propagate (IAnonymousHost real_host)
4846 real_host.SetYields ();
4847 if (anonymous_methods != null) {
4848 foreach (AnonymousMethodExpression ame in anonymous_methods)
4849 real_host.AddAnonymousMethod (ame);
4855 // A class used to hold info about an operator declarator
4857 public class OperatorDeclaration {
4858 public Operator.OpType optype;
4859 public Expression ret_type, arg1type, arg2type;
4860 public string arg1name, arg2name;
4861 public Location location;
4863 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4864 Expression arg1type, string arg1name,
4865 Expression arg2type, string arg2name, Location location)
4868 this.ret_type = ret_type;
4869 this.arg1type = arg1type;
4870 this.arg1name = arg1name;
4871 this.arg2type = arg2type;
4872 this.arg2name = arg2name;
4873 this.location = location;
4878 void Error_ExpectingTypeName (Expression expr)
4880 if (expr is Invocation){
4881 Report.Error (1002, expr.Location, "Expecting `;'");
4883 expr.Error_InvalidExpressionStatement ();
4887 public static void Error_ParameterModifierNotValid (Location loc)
4889 Report.Error (631, loc, "The modifiers `ref' and `out' are not valid in this context");
4892 static void Error_DuplicateParameterModifier (Location loc, Parameter.Modifier mod)
4894 Report.Error (1107, loc, "Duplicate parameter modifier `{0}'",
4895 Parameter.GetModifierSignature (mod));
4898 void push_current_class (TypeContainer tc, object partial_token)
4900 if (partial_token != null)
4901 current_container = current_container.AddPartial (tc);
4903 current_container = current_container.AddTypeContainer (tc);
4907 DeclSpace pop_current_class ()
4909 DeclSpace retval = current_class;
4911 current_class = current_class.Parent;
4912 current_container = current_class.PartialContainer;
4918 // Given the @class_name name, it creates a fully qualified name
4919 // based on the containing declaration space
4922 MakeName (MemberName class_name)
4924 Namespace ns = current_namespace.NS;
4926 if (current_container.Name.Length == 0){
4927 if (ns.Name.Length != 0)
4928 return new MemberName (ns.MemberName, class_name);
4932 return new MemberName (current_container.MemberName, class_name);
4936 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4938 Block implicit_block;
4939 ArrayList inits = null;
4942 // We use the `Used' property to check whether statements
4943 // have been added to the current block. If so, we need
4944 // to create another block to contain the new declaration
4945 // otherwise, as an optimization, we use the same block to
4946 // add the declaration.
4948 // FIXME: A further optimization is to check if the statements
4949 // that were added were added as part of the initialization
4950 // below. In which case, no other statements have been executed
4951 // and we might be able to reduce the number of blocks for
4952 // situations like this:
4954 // int j = 1; int k = j + 1;
4956 if (current_block.Used)
4957 implicit_block = new Block (current_block, loc, Location.Null);
4959 implicit_block = current_block;
4961 foreach (VariableDeclaration decl in variable_declarators){
4963 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
4964 if (decl.expression_or_array_initializer != null){
4966 inits = new ArrayList (4);
4973 return implicit_block;
4975 foreach (VariableDeclaration decl in inits){
4977 Expression expr = decl.expression_or_array_initializer;
4979 LocalVariableReference var;
4980 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4982 assign = new Assign (var, expr, decl.Location);
4984 implicit_block.AddStatement (new StatementExpression (assign));
4987 return implicit_block;
4990 Block declare_local_constants (Expression type, ArrayList declarators)
4992 Block implicit_block;
4994 if (current_block.Used)
4995 implicit_block = new Block (current_block);
4997 implicit_block = current_block;
4999 foreach (VariableDeclaration decl in declarators){
5000 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
5003 return implicit_block;
5006 string CheckAttributeTarget (string a, Location l)
5009 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
5013 Report.Warning (658, 1, l,
5014 "`{0}' is invalid attribute target. All attributes in this attribute section will be ignored", a);
5015 return string.Empty;
5018 void CheckUnaryOperator (Operator.OpType op, Location l)
5022 case Operator.OpType.LogicalNot:
5023 case Operator.OpType.OnesComplement:
5024 case Operator.OpType.Increment:
5025 case Operator.OpType.Decrement:
5026 case Operator.OpType.True:
5027 case Operator.OpType.False:
5028 case Operator.OpType.Addition:
5029 case Operator.OpType.Subtraction:
5034 Report.Error (1019, l, "Overloadable unary operator expected");
5040 void CheckBinaryOperator (Operator.OpType op, Location l)
5044 case Operator.OpType.Addition:
5045 case Operator.OpType.Subtraction:
5046 case Operator.OpType.Multiply:
5047 case Operator.OpType.Division:
5048 case Operator.OpType.Modulus:
5049 case Operator.OpType.BitwiseAnd:
5050 case Operator.OpType.BitwiseOr:
5051 case Operator.OpType.ExclusiveOr:
5052 case Operator.OpType.LeftShift:
5053 case Operator.OpType.RightShift:
5054 case Operator.OpType.Equality:
5055 case Operator.OpType.Inequality:
5056 case Operator.OpType.GreaterThan:
5057 case Operator.OpType.LessThan:
5058 case Operator.OpType.GreaterThanOrEqual:
5059 case Operator.OpType.LessThanOrEqual:
5063 Report.Error (1020, l, "Overloadable binary operator expected");
5069 void syntax_error (Location l, string msg)
5071 Report.Error (1003, l, "Syntax error, " + msg);
5074 void note (string s)
5076 // Used to put annotations
5081 public Tokenizer Lexer {
5087 static CSharpParser ()
5089 oob_stack = new Stack ();
5092 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
5094 this.name = file.Name;
5096 current_namespace = new NamespaceEntry (null, file, null);
5097 current_class = current_namespace.SlaveDeclSpace;
5098 current_container = current_class.PartialContainer; // == RootContest.ToplevelTypes
5100 lexer = new Tokenizer (reader, file, defines);
5103 public void parse ()
5105 int errors = Report.Errors;
5107 if (yacc_verbose_flag > 1)
5108 yyparse (lexer, new yydebug.yyDebugSimple ());
5111 Tokenizer tokenizer = lexer as Tokenizer;
5112 tokenizer.cleanup ();
5113 } catch (Exception e){
5115 // Removed for production use, use parser verbose to get the output.
5117 // Console.WriteLine (e);
5118 if (Report.Errors == errors)
5119 Report.Error (-25, lexer.Location, "Parsing error");
5120 if (yacc_verbose_flag > 0)
5121 Console.WriteLine (e);
5124 if (RootContext.ToplevelTypes.NamespaceEntry != null)
5125 throw new InternalErrorException ("who set it?");
5128 static void CheckToken (int error, int yyToken, string msg, Location loc)
5130 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
5131 Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ());
5133 Report.Error (error, loc, msg);
5136 void CheckIdentifierToken (int yyToken, Location loc)
5138 CheckToken (1041, yyToken, "Identifier expected", loc);
5141 string ConsumeStoredComment ()
5143 string s = tmpComment;
5145 Lexer.doc_state = XmlCommentState.Allowed;
5149 Location GetLocation (object obj)
5151 if (obj is MemberCore)
5152 return ((MemberCore) obj).Location;
5153 if (obj is MemberName)
5154 return ((MemberName) obj).Location;
5155 if (obj is LocatedToken)
5156 return ((LocatedToken) obj).Location;
5157 if (obj is Location)
5158 return (Location) obj;
5159 return lexer.Location;
5162 static GenericMethod current_generic_method = null;
5164 void start_block (Location loc)
5166 if (current_block == null || parsing_anonymous_method) {
5167 current_block = new ToplevelBlock (current_block, current_local_parameters, current_generic_method, loc);
5168 parsing_anonymous_method = false;
5170 current_block = new ExplicitBlock (current_block, loc, Location.Null);
5175 end_block (Location loc)
5177 Block retval = current_block.Explicit;
5178 retval.SetEndLocation (loc);
5179 current_block = retval.Parent;
5184 start_anonymous (bool lambda, Parameters parameters, Location loc)
5186 oob_stack.Push (current_anonymous_method);
5187 oob_stack.Push (current_local_parameters);
5189 current_local_parameters = parameters;
5191 ToplevelBlock top_current_block = current_block == null ? null : current_block.Toplevel;
5193 current_anonymous_method = lambda
5194 ? new LambdaExpression (
5195 current_anonymous_method, current_generic_method, current_container,
5196 parameters, top_current_block, loc)
5197 : new AnonymousMethodExpression (
5198 current_anonymous_method, current_generic_method, current_container,
5199 parameters, top_current_block, loc);
5201 // Force the next block to be created as a ToplevelBlock
5202 parsing_anonymous_method = true;
5206 * Completes the anonymous method processing, if lambda_expr is null, this
5207 * means that we have a Statement instead of an Expression embedded
5209 AnonymousMethodExpression
5210 end_anonymous (ToplevelBlock anon_block, Location loc)
5212 AnonymousMethodExpression retval;
5214 if (RootContext.Version == LanguageVersion.ISO_1){
5215 Report.FeatureIsNotISO1 (loc, "anonymous methods");
5218 current_anonymous_method.Block = anon_block;
5219 if ((anonymous_host != null) && (current_anonymous_method.Parent == null))
5220 anonymous_host.AddAnonymousMethod (current_anonymous_method);
5222 retval = current_anonymous_method;
5225 current_local_parameters = (Parameters) oob_stack.Pop ();
5226 current_anonymous_method = (AnonymousMethodExpression) oob_stack.Pop ();