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.FeatureIsNotAvailable (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 int yield_method = c.ModFlags & Modifiers.METHOD_YIELDS;
2108 c.ModFlags = (int) $2;
2110 if (RootContext.Documentation != null)
2111 c.DocComment = ConsumeStoredComment ();
2113 if (c.Name == current_container.Basename){
2114 if ((c.ModFlags & Modifiers.STATIC) != 0){
2115 if ((c.ModFlags & Modifiers.Accessibility) != 0){
2116 Report.Error (515, c.Location,
2117 "`{0}': access modifiers are not allowed on static constructors",
2118 c.GetSignatureForError ());
2121 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2123 if (c.Initializer != null){
2124 Report.Error (514, c.Location,
2125 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2126 c.GetSignatureForError ());
2129 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2132 // We let another layer check the validity of the constructor.
2133 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
2136 c.ModFlags |= yield_method;
2137 current_container.AddConstructor (c);
2139 current_local_parameters = null;
2140 if (RootContext.Documentation != null)
2141 Lexer.doc_state = XmlCommentState.Allowed;
2145 constructor_declarator
2146 : constructor_header
2150 | constructor_header constructor_initializer
2152 ((Constructor)$1).Initializer = (ConstructorInitializer) $2;
2160 if (RootContext.Documentation != null) {
2161 tmpComment = Lexer.consume_doc_comment ();
2162 Lexer.doc_state = XmlCommentState.Allowed;
2165 open_parens opt_formal_parameter_list CLOSE_PARENS
2167 LocatedToken lt = (LocatedToken) $1;
2168 current_local_parameters = (Parameters) $4;
2169 current_block = new ToplevelBlock (null, current_local_parameters, null, lt.Location);
2171 $$ = new Constructor (current_class, lt.Value, 0, current_local_parameters,
2174 anonymous_host = (IAnonymousHost) $$;
2180 | SEMICOLON { current_block = null; $$ = null; }
2183 constructor_initializer
2184 : COLON BASE open_parens opt_argument_list CLOSE_PARENS
2186 $$ = new ConstructorBaseInitializer ((ArrayList) $4, (Location) $2);
2188 | COLON THIS open_parens opt_argument_list CLOSE_PARENS
2190 $$ = new ConstructorThisInitializer ((ArrayList) $4, (Location) $2);
2193 Report.Error (1018, (Location) $1, "Keyword this or base expected");
2199 : /* EMPTY */ { $$ = 0; }
2200 | UNSAFE { $$ = Modifiers.UNSAFE; }
2201 | EXTERN { $$ = Modifiers.EXTERN; }
2204 destructor_declaration
2205 : opt_attributes opt_finalizer TILDE
2207 if (RootContext.Documentation != null) {
2208 tmpComment = Lexer.consume_doc_comment ();
2209 Lexer.doc_state = XmlCommentState.NotAllowed;
2212 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2214 LocatedToken lt = (LocatedToken) $5;
2215 if (lt.Value != current_container.Basename){
2216 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2217 } else if (current_container.Kind != Kind.Class){
2218 Report.Error (575, lt.Location, "Only class types can contain destructor");
2220 Location l = lt.Location;
2223 if (!RootContext.StdLib && current_container.Name == "System.Object")
2224 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2226 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2228 Method d = new Destructor (
2229 current_class, TypeManager.system_void_expr, m, "Finalize",
2230 Parameters.EmptyReadOnlyParameters, (Attributes) $1, l);
2231 if (RootContext.Documentation != null)
2232 d.DocComment = ConsumeStoredComment ();
2234 d.Block = (ToplevelBlock) $8;
2235 current_container.AddMethod (d);
2243 EVENT type variable_declarators SEMICOLON
2245 current_array_type = null;
2246 foreach (VariableDeclaration var in (ArrayList) $5) {
2248 MemberName name = new MemberName (var.identifier,
2251 EventField e = new EventField (
2252 current_class, (Expression) $4, (int) $2, false, name,
2255 e.Initializer = var.expression_or_array_initializer;
2257 current_container.AddEvent (e);
2259 if (RootContext.Documentation != null) {
2260 e.DocComment = Lexer.consume_doc_comment ();
2261 Lexer.doc_state = XmlCommentState.Allowed;
2267 EVENT type namespace_or_type_name
2270 implicit_value_parameter_type = (Expression) $4;
2271 lexer.EventParsing = true;
2273 event_accessor_declarations
2275 lexer.EventParsing = false;
2279 MemberName name = (MemberName) $5;
2282 Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2283 current_container.Name, name.ToString ());
2286 Accessors accessors = (Accessors) $8;
2287 if (accessors.get_or_add == null || accessors.set_or_remove == null)
2288 // CS0073 is already reported, so no CS0065 here.
2291 Event e = new EventProperty (
2292 current_class, (Expression) $4, (int) $2, false, name,
2293 (Attributes) $1, accessors.get_or_add, accessors.set_or_remove);
2294 if (RootContext.Documentation != null) {
2295 e.DocComment = Lexer.consume_doc_comment ();
2296 Lexer.doc_state = XmlCommentState.Allowed;
2299 current_container.AddEvent (e);
2300 implicit_value_parameter_type = null;
2304 | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
2305 MemberName mn = (MemberName) $5;
2307 if (mn.Left != null)
2308 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2310 Report.Error (71, mn.Location, "Event declaration should use property syntax");
2312 if (RootContext.Documentation != null)
2313 Lexer.doc_state = XmlCommentState.Allowed;
2317 event_accessor_declarations
2318 : add_accessor_declaration remove_accessor_declaration
2320 $$ = new Accessors ((Accessor) $1, (Accessor) $2);
2322 | remove_accessor_declaration add_accessor_declaration
2324 Accessors accessors = new Accessors ((Accessor) $2, (Accessor) $1);
2325 accessors.declared_in_reverse = true;
2328 | add_accessor_declaration { $$ = null; }
2329 | remove_accessor_declaration { $$ = null; }
2332 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2338 add_accessor_declaration
2339 : opt_attributes ADD
2341 Parameter [] args = new Parameter [1];
2342 Parameter implicit_value_parameter = new Parameter (
2343 implicit_value_parameter_type, "value",
2344 Parameter.Modifier.NONE, null, (Location) $2);
2346 args [0] = implicit_value_parameter;
2348 current_local_parameters = new Parameters (args);
2349 lexer.EventParsing = false;
2351 anonymous_host = SimpleAnonymousHost.GetSimple ();
2355 Accessor accessor = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2356 lexer.EventParsing = true;
2358 current_local_parameters = null;
2359 SimpleAnonymousHost.Simple.Propagate (accessor);
2360 anonymous_host = null;
2364 | opt_attributes ADD error {
2365 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2368 | opt_attributes modifiers ADD {
2369 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2374 remove_accessor_declaration
2375 : opt_attributes REMOVE
2377 Parameter [] args = new Parameter [1];
2378 Parameter implicit_value_parameter = new Parameter (
2379 implicit_value_parameter_type, "value",
2380 Parameter.Modifier.NONE, null, (Location) $2);
2382 args [0] = implicit_value_parameter;
2384 current_local_parameters = new Parameters (args);
2385 lexer.EventParsing = false;
2389 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2390 lexer.EventParsing = true;
2392 | opt_attributes REMOVE error {
2393 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2396 | opt_attributes modifiers REMOVE {
2397 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2403 : opt_attributes opt_modifiers indexer_declarator
2406 IndexerDeclaration decl = (IndexerDeclaration) $3;
2408 implicit_value_parameter_type = decl.type;
2410 lexer.PropertyParsing = true;
2411 parsing_indexer = true;
2413 indexer_parameters = decl.param_list;
2414 anonymous_host = SimpleAnonymousHost.GetSimple ();
2416 accessor_declarations
2418 lexer.PropertyParsing = false;
2419 has_get = has_set = false;
2420 parsing_indexer = false;
2427 // The signature is computed from the signature of the indexer. Look
2428 // at section 3.6 on the spec
2430 IndexerDeclaration decl = (IndexerDeclaration) $3;
2431 Location loc = decl.location;
2432 Accessors accessors = (Accessors) $6;
2433 Accessor get_block = accessors.get_or_add;
2434 Accessor set_block = accessors.set_or_remove;
2437 if (decl.interface_type != null)
2438 name = new MemberName (decl.interface_type, TypeContainer.DefaultIndexerName, loc);
2440 name = new MemberName (TypeContainer.DefaultIndexerName, loc);
2442 indexer = new Indexer (current_class, decl.type, name,
2443 (int) $2, false, decl.param_list, (Attributes) $1,
2444 get_block, set_block, accessors.declared_in_reverse);
2446 if (RootContext.Documentation != null)
2447 indexer.DocComment = ConsumeStoredComment ();
2449 current_container.AddIndexer (indexer);
2451 current_local_parameters = null;
2452 implicit_value_parameter_type = null;
2453 indexer_parameters = null;
2458 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2460 Parameters pars = (Parameters) $4;
2461 if (pars.HasArglist) {
2462 // "__arglist is not valid in this context"
2463 Report.Error (1669, (Location) $2, "__arglist is not valid in this context");
2464 } else if (pars.Empty){
2465 Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
2467 if (RootContext.Documentation != null) {
2468 tmpComment = Lexer.consume_doc_comment ();
2469 Lexer.doc_state = XmlCommentState.Allowed;
2472 $$ = new IndexerDeclaration ((Expression) $1, null, pars, (Location) $2);
2474 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2476 Parameters pars = (Parameters) $6;
2478 if (pars.HasArglist) {
2479 // "__arglist is not valid in this context"
2480 Report.Error (1669, (Location) $4, "__arglist is not valid in this context");
2481 } else if (pars.Empty){
2482 Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
2485 MemberName name = (MemberName) $2;
2486 $$ = new IndexerDeclaration ((Expression) $1, name, pars, (Location) $4);
2488 if (RootContext.Documentation != null) {
2489 tmpComment = Lexer.consume_doc_comment ();
2490 Lexer.doc_state = XmlCommentState.Allowed;
2500 if (RootContext.Documentation != null)
2501 enumTypeComment = Lexer.consume_doc_comment ();
2506 LocatedToken lt = (LocatedToken) $4;
2507 Location enum_location = lt.Location;
2509 MemberName name = MakeName (new MemberName (lt.Value, enum_location));
2510 Enum e = new Enum (current_namespace, current_class, (Expression) $5, (int) $2,
2511 name, (Attributes) $1);
2513 if (RootContext.Documentation != null)
2514 e.DocComment = enumTypeComment;
2517 EnumMember em = null;
2518 foreach (VariableDeclaration ev in (ArrayList) $7) {
2519 em = new EnumMember (
2520 e, em, ev.identifier, (Expression) ev.expression_or_array_initializer,
2521 ev.OptAttributes, ev.Location);
2523 // if (RootContext.Documentation != null)
2524 em.DocComment = ev.DocComment;
2526 e.AddEnumMember (em);
2529 current_container.AddTypeContainer (e);
2536 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2537 | COLON type { $$ = $2; }
2543 if (RootContext.Documentation != null)
2544 Lexer.doc_state = XmlCommentState.Allowed;
2546 opt_enum_member_declarations
2548 // here will be evaluated after CLOSE_BLACE is consumed.
2549 if (RootContext.Documentation != null)
2550 Lexer.doc_state = XmlCommentState.Allowed;
2558 opt_enum_member_declarations
2559 : /* empty */ { $$ = new ArrayList (4); }
2560 | enum_member_declarations opt_comma { $$ = $1; }
2563 enum_member_declarations
2564 : enum_member_declaration
2566 ArrayList l = new ArrayList (4);
2571 | enum_member_declarations COMMA enum_member_declaration
2573 ArrayList l = (ArrayList) $1;
2581 enum_member_declaration
2582 : opt_attributes IDENTIFIER
2584 VariableDeclaration vd = new VariableDeclaration (
2585 (LocatedToken) $2, null, (Attributes) $1);
2587 if (RootContext.Documentation != null) {
2588 vd.DocComment = Lexer.consume_doc_comment ();
2589 Lexer.doc_state = XmlCommentState.Allowed;
2594 | opt_attributes IDENTIFIER
2596 if (RootContext.Documentation != null) {
2597 tmpComment = Lexer.consume_doc_comment ();
2598 Lexer.doc_state = XmlCommentState.NotAllowed;
2603 VariableDeclaration vd = new VariableDeclaration (
2604 (LocatedToken) $2, $5, (Attributes) $1);
2606 if (RootContext.Documentation != null)
2607 vd.DocComment = ConsumeStoredComment ();
2613 delegate_declaration
2620 open_parens opt_formal_parameter_list CLOSE_PARENS
2622 MemberName name = MakeName ((MemberName) $6);
2623 Parameters p = (Parameters) $8;
2625 // TODO: wrong location
2626 Report.Error (1669, name.Location, "__arglist is not valid in this context");
2629 Delegate del = new Delegate (current_namespace, current_class, (Expression) $5,
2630 (int) $2, name, p, (Attributes) $1);
2632 if (RootContext.Documentation != null) {
2633 del.DocComment = Lexer.consume_doc_comment ();
2634 Lexer.doc_state = XmlCommentState.Allowed;
2637 current_container.AddDelegate (del);
2638 current_delegate = del;
2642 $$ = current_delegate;
2644 current_delegate = null;
2648 namespace_or_type_name
2650 | IDENTIFIER DOUBLE_COLON IDENTIFIER {
2651 LocatedToken lt1 = (LocatedToken) $1;
2652 LocatedToken lt2 = (LocatedToken) $3;
2653 $$ = new MemberName (lt1.Value, lt2.Value, lt2.Location);
2655 | namespace_or_type_name DOT IDENTIFIER {
2656 LocatedToken lt = (LocatedToken) $3;
2657 $$ = new MemberName ((MemberName) $1, lt.Value);
2663 LocatedToken lt = (LocatedToken) $1;
2664 $$ = new MemberName (lt.Value, lt.Location);
2673 * Before you think of adding a return_type, notice that we have been
2674 * using two rules in the places where it matters (one rule using type
2675 * and another identical one that uses VOID as the return type). This
2676 * gets rid of a shift/reduce couple
2679 : namespace_or_type_name
2681 MemberName name = (MemberName) $1;
2682 $$ = name.GetTypeExpression ();
2694 // Note that here only unmanaged types are allowed but we
2695 // can't perform checks during this phase - we do it during
2696 // semantic analysis.
2698 $$ = new ComposedCast ((Expression) $1, "*", Lexer.Location);
2702 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2708 | non_expression_type rank_specifier
2710 Location loc = GetLocation ($1);
2712 loc = lexer.Location;
2713 $$ = new ComposedCast ((Expression) $1, (string) $2, loc);
2715 | non_expression_type STAR
2717 Location loc = GetLocation ($1);
2719 loc = lexer.Location;
2720 $$ = new ComposedCast ((Expression) $1, "*", loc);
2722 | expression rank_specifiers
2724 $$ = new ComposedCast ((Expression) $1, (string) $2);
2728 $$ = new ComposedCast ((Expression) $1, "*");
2732 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2733 // through this different path
2735 | multiplicative_expression STAR
2737 $$ = new ComposedCast ((Expression) $1, "*");
2744 ArrayList types = new ArrayList (4);
2749 | type_list COMMA type
2751 ArrayList types = (ArrayList) $1;
2759 * replaces all the productions for isolating the various
2760 * simple types, but we need this to reuse it easily in local_variable_type
2763 : OBJECT { $$ = TypeManager.system_object_expr; }
2764 | STRING { $$ = TypeManager.system_string_expr; }
2765 | BOOL { $$ = TypeManager.system_boolean_expr; }
2766 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2767 | FLOAT { $$ = TypeManager.system_single_expr; }
2768 | DOUBLE { $$ = TypeManager.system_double_expr; }
2773 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2774 | BYTE { $$ = TypeManager.system_byte_expr; }
2775 | SHORT { $$ = TypeManager.system_int16_expr; }
2776 | USHORT { $$ = TypeManager.system_uint16_expr; }
2777 | INT { $$ = TypeManager.system_int32_expr; }
2778 | UINT { $$ = TypeManager.system_uint32_expr; }
2779 | LONG { $$ = TypeManager.system_int64_expr; }
2780 | ULONG { $$ = TypeManager.system_uint64_expr; }
2781 | CHAR { $$ = TypeManager.system_char_expr; }
2782 | VOID { $$ = TypeManager.system_void_expr; }
2786 : type rank_specifiers
2788 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2);
2793 // Expressions, section 7.5
2802 MemberName mn = (MemberName) $1;
2803 $$ = mn.GetTypeExpression ();
2805 | IDENTIFIER DOUBLE_COLON IDENTIFIER
2807 LocatedToken lt1 = (LocatedToken) $1;
2808 LocatedToken lt2 = (LocatedToken) $3;
2809 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, lt2.Location);
2811 | parenthesized_expression
2813 | invocation_expression
2817 | post_increment_expression
2818 | post_decrement_expression
2822 | checked_expression
2823 | unchecked_expression
2824 | pointer_member_access
2825 | anonymous_method_expression
2832 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
2833 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); }
2834 | NULL { $$ = new NullLiteral (lexer.Location); }
2838 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
2839 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
2840 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
2845 object v = lexer.Value;
2848 $$ = new IntLiteral ((int) v, lexer.Location);
2849 } else if (v is uint)
2850 $$ = new UIntLiteral ((UInt32) v, lexer.Location);
2852 $$ = new LongLiteral ((Int64) v, lexer.Location);
2853 else if (v is ulong)
2854 $$ = new ULongLiteral ((UInt64) v, lexer.Location);
2856 Console.WriteLine ("OOPS. Unexpected result from scanner");
2861 : TRUE { $$ = new BoolLiteral (true, lexer.Location); }
2862 | FALSE { $$ = new BoolLiteral (false, lexer.Location); }
2865 parenthesized_expression_0
2866 : OPEN_PARENS expression CLOSE_PARENS
2869 lexer.Deambiguate_CloseParens ($$);
2870 // After this, the next token returned is one of
2871 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST (CLOSE_PARENS), CLOSE_PARENS_OPEN_PARENS
2872 // or CLOSE_PARENS_MINUS.
2874 | OPEN_PARENS expression error { CheckToken (1026, yyToken, "Expecting ')'", lexer.Location); }
2877 parenthesized_expression
2878 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2882 | parenthesized_expression_0 CLOSE_PARENS
2886 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2888 // If a parenthesized expression is followed by a minus, we need to wrap
2889 // the expression inside a ParenthesizedExpression for the CS0075 check
2890 // in Binary.DoResolve().
2891 $$ = new ParenthesizedExpression ((Expression) $1);
2896 : primary_expression DOT IDENTIFIER
2898 LocatedToken lt = (LocatedToken) $3;
2899 $$ = new MemberAccess ((Expression) $1, lt.Value);
2901 | predefined_type DOT IDENTIFIER
2903 LocatedToken lt = (LocatedToken) $3;
2904 // TODO: Location is wrong as some predefined types doesn't hold a location
2905 $$ = new MemberAccess ((Expression) $1, lt.Value, lt.Location);
2913 invocation_expression
2914 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2917 Report.Error (1, (Location) $2, "Parse error");
2919 $$ = new Invocation ((Expression) $1, (ArrayList) $3);
2921 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2923 $$ = new Invocation ((Expression) $1, new ArrayList ());
2925 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2927 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
2929 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2931 ArrayList args = new ArrayList (1);
2933 $$ = new Invocation ((Expression) $1, args);
2935 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2937 ArrayList args = ((ArrayList) $4);
2939 $$ = new Invocation ((Expression) $1, args);
2944 : /* empty */ { $$ = null; }
2951 ArrayList list = new ArrayList (4);
2955 | argument_list COMMA argument
2957 ArrayList list = (ArrayList) $1;
2961 | argument_list error {
2962 CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
2970 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2972 | non_simple_argument
2979 : REF variable_reference
2981 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2983 | OUT variable_reference
2985 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2987 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2989 ArrayList list = (ArrayList) $3;
2990 Argument[] args = new Argument [list.Count];
2991 list.CopyTo (args, 0);
2993 Expression expr = new Arglist (args, (Location) $1);
2994 $$ = new Argument (expr, Argument.AType.Expression);
2996 | ARGLIST OPEN_PARENS CLOSE_PARENS
2998 $$ = new Argument (new Arglist ((Location) $1), Argument.AType.Expression);
3002 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
3007 : expression { note ("section 5.4"); $$ = $1; }
3011 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
3013 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
3015 | primary_expression rank_specifiers
3017 // So the super-trick is that primary_expression
3018 // can only be either a SimpleName or a MemberAccess.
3019 // The MemberAccess case arises when you have a fully qualified type-name like :
3021 // SimpleName is when you have
3024 Expression expr = (Expression) $1;
3025 if (expr is ComposedCast){
3026 $$ = new ComposedCast (expr, (string) $2);
3027 } else if (!(expr is SimpleName || expr is MemberAccess || expr is QualifiedAliasMember)){
3028 Error_ExpectingTypeName (expr);
3029 $$ = TypeManager.system_object_expr;
3032 // So we extract the string corresponding to the SimpleName
3035 $$ = new ComposedCast (expr, (string) $2);
3037 current_array_type = (Expression)$$;
3044 ArrayList list = new ArrayList (4);
3048 | expression_list COMMA expression
3050 ArrayList list = (ArrayList) $1;
3059 $$ = new This (current_block, (Location) $1);
3064 : BASE DOT IDENTIFIER
3066 LocatedToken lt = (LocatedToken) $3;
3067 $$ = new BaseAccess (lt.Value, lt.Location);
3069 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
3071 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
3074 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
3079 post_increment_expression
3080 : primary_expression OP_INC
3082 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
3083 (Expression) $1, (Location) $2);
3087 post_decrement_expression
3088 : primary_expression OP_DEC
3090 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
3091 (Expression) $1, (Location) $2);
3096 : object_or_delegate_creation_expression
3097 | array_creation_expression
3100 object_or_delegate_creation_expression
3101 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
3103 $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
3107 array_creation_expression
3108 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
3110 opt_array_initializer
3112 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
3114 | NEW type rank_specifiers array_initializer
3116 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, (Location) $1);
3120 Report.Error (1031, (Location) $1, "Type expected");
3125 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
3142 : rank_specifier opt_rank_specifier
3144 $$ = (string) $2 + (string) $1;
3149 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3151 $$ = "[" + (string) $2 + "]";
3171 | dim_separators COMMA
3173 $$ = (string) $1 + ",";
3177 opt_array_initializer
3189 : OPEN_BRACE CLOSE_BRACE
3191 ArrayList list = new ArrayList (4);
3194 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3196 $$ = (ArrayList) $2;
3200 variable_initializer_list
3201 : variable_initializer
3203 ArrayList list = new ArrayList (4);
3207 | variable_initializer_list COMMA variable_initializer
3209 ArrayList list = (ArrayList) $1;
3218 pushed_current_array_type = current_array_type;
3220 OPEN_PARENS type CLOSE_PARENS
3222 Expression type = (Expression)$4;
3223 if (type == TypeManager.system_void_expr)
3224 $$ = new TypeOfVoid ((Location) $1);
3226 $$ = new TypeOf (type, (Location) $1);
3227 current_array_type = pushed_current_array_type;
3232 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3233 $$ = new SizeOf ((Expression) $3, (Location) $1);
3238 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3240 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3244 unchecked_expression
3245 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3247 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3251 pointer_member_access
3252 : primary_expression OP_PTR IDENTIFIER
3255 LocatedToken lt = (LocatedToken) $3;
3257 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
3258 $$ = new MemberAccess (deref, lt.Value);
3262 anonymous_method_expression
3263 : DELEGATE opt_anonymous_method_signature
3265 start_anonymous (false, (Parameters) $2, (Location) $1);
3269 $$ = end_anonymous ((ToplevelBlock) $4, (Location) $1);
3273 opt_anonymous_method_signature
3274 : /* empty */ { $$ = null; }
3275 | anonymous_method_signature
3278 anonymous_method_signature
3279 : open_parens opt_anonymous_method_parameter_list CLOSE_PARENS
3282 $$ = Parameters.EmptyReadOnlyParameters;
3284 ArrayList par_list = (ArrayList) $2;
3285 Parameter [] pars = new Parameter [par_list.Count];
3286 par_list.CopyTo (pars);
3287 $$ = new Parameters (pars);
3292 opt_anonymous_method_parameter_list
3293 : /* empty */ { $$ = null; }
3294 | anonymous_method_parameter_list { $$ = $1; }
3297 anonymous_method_parameter_list
3298 : anonymous_method_parameter
3300 ArrayList a = new ArrayList (4);
3304 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3306 ArrayList a = (ArrayList) $1;
3312 anonymous_method_parameter
3313 : opt_parameter_modifier type IDENTIFIER {
3314 LocatedToken lt = (LocatedToken) $3;
3315 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3317 | PARAMS type IDENTIFIER {
3318 Report.Error (1670, ((LocatedToken) $3).Location, "The `params' modifier is not allowed in anonymous method declaration");
3324 : primary_expression
3325 | BANG prefixed_unary_expression
3327 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3329 | TILDE prefixed_unary_expression
3331 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3337 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3339 $$ = new Cast ((Expression) $1, (Expression) $3);
3341 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3343 $$ = new Cast ((Expression) $1, (Expression) $3);
3349 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3351 // TODO: wrong location
3352 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3357 // The idea to split this out is from Rhys' grammar
3358 // to solve the problem with casts.
3360 prefixed_unary_expression
3362 | PLUS prefixed_unary_expression
3364 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3366 | MINUS prefixed_unary_expression
3368 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3370 | OP_INC prefixed_unary_expression
3372 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3373 (Expression) $2, (Location) $1);
3375 | OP_DEC prefixed_unary_expression
3377 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3378 (Expression) $2, (Location) $1);
3380 | STAR prefixed_unary_expression
3382 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, (Location) $1);
3384 | BITWISE_AND prefixed_unary_expression
3386 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3390 pre_increment_expression
3391 : OP_INC prefixed_unary_expression
3393 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3394 (Expression) $2, (Location) $1);
3398 pre_decrement_expression
3399 : OP_DEC prefixed_unary_expression
3401 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3402 (Expression) $2, (Location) $1);
3406 multiplicative_expression
3407 : prefixed_unary_expression
3408 | multiplicative_expression STAR prefixed_unary_expression
3410 $$ = new Binary (Binary.Operator.Multiply,
3411 (Expression) $1, (Expression) $3);
3413 | multiplicative_expression DIV prefixed_unary_expression
3415 $$ = new Binary (Binary.Operator.Division,
3416 (Expression) $1, (Expression) $3);
3418 | multiplicative_expression PERCENT prefixed_unary_expression
3420 $$ = new Binary (Binary.Operator.Modulus,
3421 (Expression) $1, (Expression) $3);
3426 : multiplicative_expression
3427 | additive_expression PLUS multiplicative_expression
3429 $$ = new Binary (Binary.Operator.Addition,
3430 (Expression) $1, (Expression) $3);
3432 | additive_expression MINUS multiplicative_expression
3434 $$ = new Binary (Binary.Operator.Subtraction,
3435 (Expression) $1, (Expression) $3);
3440 : additive_expression
3441 | shift_expression OP_SHIFT_LEFT additive_expression
3443 $$ = new Binary (Binary.Operator.LeftShift,
3444 (Expression) $1, (Expression) $3);
3446 | shift_expression OP_SHIFT_RIGHT additive_expression
3448 $$ = new Binary (Binary.Operator.RightShift,
3449 (Expression) $1, (Expression) $3);
3453 relational_expression
3455 | relational_expression OP_LT shift_expression
3457 $$ = new Binary (Binary.Operator.LessThan,
3458 (Expression) $1, (Expression) $3);
3460 | relational_expression OP_GT shift_expression
3462 $$ = new Binary (Binary.Operator.GreaterThan,
3463 (Expression) $1, (Expression) $3);
3465 | relational_expression OP_LE shift_expression
3467 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3468 (Expression) $1, (Expression) $3);
3470 | relational_expression OP_GE shift_expression
3472 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3473 (Expression) $1, (Expression) $3);
3475 | relational_expression IS type
3477 $$ = new Is ((Expression) $1, (Expression) $3, (Location) $2);
3479 | relational_expression AS type
3481 $$ = new As ((Expression) $1, (Expression) $3, (Location) $2);
3486 : relational_expression
3487 | equality_expression OP_EQ relational_expression
3489 $$ = new Binary (Binary.Operator.Equality,
3490 (Expression) $1, (Expression) $3);
3492 | equality_expression OP_NE relational_expression
3494 $$ = new Binary (Binary.Operator.Inequality,
3495 (Expression) $1, (Expression) $3);
3500 : equality_expression
3501 | and_expression BITWISE_AND equality_expression
3503 $$ = new Binary (Binary.Operator.BitwiseAnd,
3504 (Expression) $1, (Expression) $3);
3508 exclusive_or_expression
3510 | exclusive_or_expression CARRET and_expression
3512 $$ = new Binary (Binary.Operator.ExclusiveOr,
3513 (Expression) $1, (Expression) $3);
3517 inclusive_or_expression
3518 : exclusive_or_expression
3519 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3521 $$ = new Binary (Binary.Operator.BitwiseOr,
3522 (Expression) $1, (Expression) $3);
3526 conditional_and_expression
3527 : inclusive_or_expression
3528 | conditional_and_expression OP_AND inclusive_or_expression
3530 $$ = new Binary (Binary.Operator.LogicalAnd,
3531 (Expression) $1, (Expression) $3);
3535 conditional_or_expression
3536 : conditional_and_expression
3537 | conditional_or_expression OP_OR conditional_and_expression
3539 $$ = new Binary (Binary.Operator.LogicalOr,
3540 (Expression) $1, (Expression) $3);
3544 conditional_expression
3545 : conditional_or_expression
3546 | conditional_or_expression INTERR expression COLON expression
3548 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
3552 assignment_expression
3553 : prefixed_unary_expression ASSIGN expression
3555 $$ = new Assign ((Expression) $1, (Expression) $3);
3557 | prefixed_unary_expression OP_MULT_ASSIGN expression
3559 $$ = new CompoundAssign (
3560 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
3562 | prefixed_unary_expression OP_DIV_ASSIGN expression
3564 $$ = new CompoundAssign (
3565 Binary.Operator.Division, (Expression) $1, (Expression) $3);
3567 | prefixed_unary_expression OP_MOD_ASSIGN expression
3569 $$ = new CompoundAssign (
3570 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
3572 | prefixed_unary_expression OP_ADD_ASSIGN expression
3574 $$ = new CompoundAssign (
3575 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
3577 | prefixed_unary_expression OP_SUB_ASSIGN expression
3579 $$ = new CompoundAssign (
3580 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3582 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3584 $$ = new CompoundAssign (
3585 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
3587 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3589 $$ = new CompoundAssign (
3590 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
3592 | prefixed_unary_expression OP_AND_ASSIGN expression
3594 $$ = new CompoundAssign (
3595 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
3597 | prefixed_unary_expression OP_OR_ASSIGN expression
3599 $$ = new CompoundAssign (
3600 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
3602 | prefixed_unary_expression OP_XOR_ASSIGN expression
3604 $$ = new CompoundAssign (
3605 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
3609 implicitly_typed_lambda_parameter_list
3611 LocatedToken lt = (LocatedToken) $1;
3612 ArrayList a = new ArrayList (4);
3614 a.Add (new Parameter ((Expression)null, lt.Value, Parameter.Modifier.NONE, null, lt.Location));
3617 | implicitly_typed_lambda_parameter_list COMMA IDENTIFIER {
3618 LocatedToken lt = (LocatedToken) $3;
3619 ArrayList a = (ArrayList) $1;
3620 a.Add (new Parameter ((Expression)null, lt.Value, Parameter.Modifier.NONE, null, lt.Location));
3625 explicitly_typed_lambda_parameter_list
3626 : explicitly_typed_lambda_parameter
3628 ArrayList pars = new ArrayList (4);
3633 | explicitly_typed_lambda_parameter_list COMMA explicitly_typed_lambda_parameter
3635 ArrayList pars = (ArrayList) $1;
3642 explicitly_typed_lambda_parameter
3643 : parameter_modifier type IDENTIFIER
3645 LocatedToken lt = (LocatedToken) $3;
3647 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3651 LocatedToken lt = (LocatedToken) $2;
3653 $$ = new Parameter ((Expression) $1, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
3657 lambda_parameter_list
3658 : implicitly_typed_lambda_parameter_list { $$ = $1; }
3659 | explicitly_typed_lambda_parameter_list { $$ = $1; }
3662 opt_lambda_parameter_list
3663 : /* empty */ { $$ = null; }
3664 | lambda_parameter_list {
3665 ArrayList pars_list = (ArrayList) $1;
3667 Parameter [] pars = new Parameter [pars_list.Count];
3668 pars_list.CopyTo (pars);
3669 $$ = new Parameters (pars);
3673 lambda_expression_body
3675 start_block (lexer.Location);
3679 Block b = end_block (lexer.Location);
3680 b.AddStatement (new ContextualReturn ((Expression) $2));
3691 Parameter [] pars = new Parameter [1];
3692 LocatedToken lt = (LocatedToken) $1;
3693 pars [0] = new Parameter ((Expression)null, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
3695 Parameters parameters = new Parameters (pars);
3697 start_anonymous (true, parameters, (Location) $2);
3699 lambda_expression_body
3701 $$ = end_anonymous ((ToplevelBlock) $4, (Location) $2);
3703 | OPEN_PARENS_LAMBDA opt_lambda_parameter_list CLOSE_PARENS ARROW
3705 start_anonymous (true, (Parameters) $2, (Location) $4);
3707 lambda_expression_body
3709 $$ = end_anonymous ((ToplevelBlock) $6, (Location) $4);
3714 : conditional_expression
3715 | assignment_expression
3739 MemberName name = MakeName ((MemberName) $6);
3740 push_current_class (new Class (current_namespace, current_class, name, (int) $2, (Attributes) $1), $3);
3744 if (RootContext.Documentation != null) {
3745 current_container.DocComment = Lexer.consume_doc_comment ();
3746 Lexer.doc_state = XmlCommentState.Allowed;
3751 if (RootContext.Documentation != null)
3752 Lexer.doc_state = XmlCommentState.Allowed;
3756 $$ = pop_current_class ();
3764 { $$ = $1; } // location
3768 : /* empty */ { $$ = (int) 0; }
3774 | modifiers modifier
3779 if ((m1 & m2) != 0) {
3780 Location l = lexer.Location;
3781 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
3783 $$ = (int) (m1 | m2);
3788 : NEW { $$ = Modifiers.NEW; }
3789 | PUBLIC { $$ = Modifiers.PUBLIC; }
3790 | PROTECTED { $$ = Modifiers.PROTECTED; }
3791 | INTERNAL { $$ = Modifiers.INTERNAL; }
3792 | PRIVATE { $$ = Modifiers.PRIVATE; }
3793 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3794 | SEALED { $$ = Modifiers.SEALED; }
3795 | STATIC { $$ = Modifiers.STATIC; }
3796 | READONLY { $$ = Modifiers.READONLY; }
3797 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3798 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3799 | EXTERN { $$ = Modifiers.EXTERN; }
3800 | VOLATILE { $$ = Modifiers.VOLATILE; }
3801 | UNSAFE { $$ = Modifiers.UNSAFE; }
3810 : COLON type_list { current_container.AddBasesForPart (current_class, (ArrayList) $2); }
3818 // A block is "contained" on the following places:
3820 // property_declaration as part of the accessor body (get/set)
3821 // operator_declaration
3822 // constructor_declaration
3823 // destructor_declaration
3824 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3829 ++lexer.parsing_block;
3830 start_block ((Location) $1);
3832 opt_statement_list CLOSE_BRACE
3834 --lexer.parsing_block;
3835 $$ = end_block ((Location) $4);
3842 ++lexer.parsing_block;
3844 opt_statement_list CLOSE_BRACE
3846 --lexer.parsing_block;
3847 $$ = end_block ((Location) $4);
3858 | statement_list statement
3862 : declaration_statement
3864 if ($1 != null && (Block) $1 != current_block){
3865 current_block.AddStatement ((Statement) $1);
3866 current_block = (Block) $1;
3869 | valid_declaration_statement
3871 current_block.AddStatement ((Statement) $1);
3876 valid_declaration_statement
3879 | expression_statement
3880 | selection_statement
3881 | iteration_statement
3885 | unchecked_statement
3893 : valid_declaration_statement
3894 | declaration_statement
3896 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3901 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3909 $$ = EmptyStatement.Value;
3916 LocatedToken lt = (LocatedToken) $1;
3917 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
3919 if (current_block.AddLabel (labeled))
3920 current_block.AddStatement (labeled);
3925 declaration_statement
3926 : local_variable_declaration SEMICOLON
3928 current_array_type = null;
3930 DictionaryEntry de = (DictionaryEntry) $1;
3931 Expression e = (Expression) de.Key;
3933 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
3937 | local_constant_declaration SEMICOLON
3939 current_array_type = null;
3941 DictionaryEntry de = (DictionaryEntry) $1;
3943 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3949 * The following is from Rhys' grammar:
3950 * > Types in local variable declarations must be recognized as
3951 * > expressions to prevent reduce/reduce errors in the grammar.
3952 * > The expressions are converted into types during semantic analysis.
3955 : primary_expression opt_rank_specifier
3957 // FIXME: Do something smart here regarding the composition of the type.
3959 // Ok, the above "primary_expression" is there to get rid of
3960 // both reduce/reduce and shift/reduces in the grammar, it should
3961 // really just be "type_name". If you use type_name, a reduce/reduce
3962 // creeps up. If you use namespace_or_type_name (which is all we need
3963 // really) two shift/reduces appear.
3966 // So the super-trick is that primary_expression
3967 // can only be either a SimpleName or a MemberAccess.
3968 // The MemberAccess case arises when you have a fully qualified type-name like :
3970 // SimpleName is when you have
3973 Expression expr = (Expression) $1;
3974 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3975 Error_ExpectingTypeName (expr);
3979 // So we extract the string corresponding to the SimpleName
3983 if ((string) $2 == "")
3986 $$ = new ComposedCast ((Expression) $1, (string) $2);
3989 | builtin_types opt_rank_specifier
3991 if ((string) $2 == "")
3994 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3998 local_variable_pointer_type
3999 : primary_expression STAR
4001 Expression expr = (Expression) $1;
4003 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
4004 Error_ExpectingTypeName (expr);
4008 $$ = new ComposedCast ((Expression) $1, "*");
4010 | builtin_types STAR
4012 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
4016 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
4018 | local_variable_pointer_type STAR
4020 $$ = new ComposedCast ((Expression) $1, "*");
4024 local_variable_declaration
4025 : local_variable_type variable_declarators
4028 $$ = new DictionaryEntry ($1, $2);
4032 | local_variable_pointer_type opt_rank_specifier variable_declarators
4037 if ((string) $2 == "")
4038 t = (Expression) $1;
4040 t = new ComposedCast ((Expression) $1, (string) $2);
4041 $$ = new DictionaryEntry (t, $3);
4047 local_constant_declaration
4048 : CONST local_variable_type constant_declarators
4051 $$ = new DictionaryEntry ($2, $3);
4057 expression_statement
4058 : statement_expression SEMICOLON { $$ = $1; }
4062 // We have to do the wrapping here and not in the case above,
4063 // because statement_expression is used for example in for_statement
4065 statement_expression
4068 Expression expr = (Expression) $1;
4069 ExpressionStatement s = expr as ExpressionStatement;
4071 expr.Error_InvalidExpressionStatement ();
4074 $$ = new StatementExpression (s);
4078 Report.Error (1002, GetLocation ($1), "Expecting `;'");
4083 object_creation_expression
4084 : object_or_delegate_creation_expression
4085 { note ("complain if this is a delegate maybe?"); }
4094 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
4097 Location l = (Location) $1;
4099 $$ = new If ((Expression) $3, (Statement) $5, l);
4101 // FIXME: location for warning should be loc property of $5.
4102 if ($5 == EmptyStatement.Value)
4103 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4106 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
4107 embedded_statement ELSE embedded_statement
4109 Location l = (Location) $1;
4111 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
4113 // FIXME: location for warning should be loc property of $5 and $7.
4114 if ($5 == EmptyStatement.Value)
4115 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4116 if ($7 == EmptyStatement.Value)
4117 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4122 : SWITCH OPEN_PARENS
4124 if (switch_stack == null)
4125 switch_stack = new Stack (2);
4126 switch_stack.Push (current_block);
4128 expression CLOSE_PARENS
4131 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
4132 current_block = (Block) switch_stack.Pop ();
4148 Report.Warning (1522, 1, lexer.Location, "Empty switch block");
4149 $$ = new ArrayList ();
4157 ArrayList sections = new ArrayList (4);
4162 | switch_sections switch_section
4164 ArrayList sections = (ArrayList) $1;
4174 current_block = current_block.CreateSwitchBlock (lexer.Location);
4178 $$ = new SwitchSection ((ArrayList) $1, current_block.Explicit);
4185 ArrayList labels = new ArrayList (4);
4190 | switch_labels switch_label
4192 ArrayList labels = (ArrayList) ($1);
4200 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
4201 | DEFAULT COLON { $$ = new SwitchLabel (null, (Location) $1); }
4204 1523, GetLocation ($1),
4205 "The keyword case or default must precede code in switch block");
4217 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
4219 Location l = (Location) $1;
4220 $$ = new While ((Expression) $3, (Statement) $5, l);
4225 : DO embedded_statement
4226 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
4228 Location l = (Location) $1;
4230 $$ = new Do ((Statement) $2, (Expression) $5, l);
4236 opt_for_initializer SEMICOLON
4238 Location l = lexer.Location;
4240 Block assign_block = current_block;
4242 if ($3 is DictionaryEntry){
4243 DictionaryEntry de = (DictionaryEntry) $3;
4245 Expression type = (Expression) de.Key;
4246 ArrayList var_declarators = (ArrayList) de.Value;
4248 foreach (VariableDeclaration decl in var_declarators){
4252 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4256 Expression expr = decl.expression_or_array_initializer;
4258 LocalVariableReference var;
4259 var = new LocalVariableReference (assign_block, decl.identifier, l);
4262 Assign a = new Assign (var, expr, decl.Location);
4264 assign_block.AddStatement (new StatementExpression (a));
4268 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4269 // This can be referred to as $5 below.
4275 opt_for_condition SEMICOLON
4276 opt_for_iterator CLOSE_PARENS
4279 Location l = (Location) $1;
4281 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4283 current_block.AddStatement (f);
4285 $$ = end_block (lexer.Location);
4290 : /* empty */ { $$ = EmptyStatement.Value; }
4295 : local_variable_declaration
4296 | statement_expression_list
4300 : /* empty */ { $$ = null; }
4301 | boolean_expression
4305 : /* empty */ { $$ = EmptyStatement.Value; }
4310 : statement_expression_list
4313 statement_expression_list
4314 : statement_expression
4316 // CHANGE: was `null'
4317 Statement s = (Statement) $1;
4318 Block b = new Block (current_block, s.loc, lexer.Location);
4323 | statement_expression_list COMMA statement_expression
4325 Block b = (Block) $1;
4327 b.AddStatement ((Statement) $3);
4333 : FOREACH open_parens type IN expression CLOSE_PARENS
4335 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4338 | FOREACH open_parens type IDENTIFIER IN
4339 expression CLOSE_PARENS
4341 start_block (lexer.Location);
4342 Block foreach_block = current_block;
4344 LocatedToken lt = (LocatedToken) $4;
4345 Location l = lt.Location;
4346 LocalInfo vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4348 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4350 // Get a writable reference to this read-only variable.
4352 // Note that the $$ here refers to the value of _this_ code block,
4353 // not the value of the LHS non-terminal. This can be referred to as $8 below.
4354 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
4361 LocalVariableReference v = (LocalVariableReference) $8;
4362 Location l = (Location) $1;
4365 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
4366 current_block.AddStatement (f);
4369 $$ = end_block (lexer.Location);
4375 | continue_statement
4385 $$ = new Break ((Location) $1);
4390 : CONTINUE SEMICOLON
4392 $$ = new Continue ((Location) $1);
4397 : GOTO IDENTIFIER SEMICOLON
4399 LocatedToken lt = (LocatedToken) $2;
4400 $$ = new Goto (lt.Value, lt.Location);
4402 | GOTO CASE constant_expression SEMICOLON
4404 $$ = new GotoCase ((Expression) $3, (Location) $1);
4406 | GOTO DEFAULT SEMICOLON
4408 $$ = new GotoDefault ((Location) $1);
4413 : RETURN opt_expression SEMICOLON
4415 $$ = new Return ((Expression) $2, (Location) $1);
4420 : THROW opt_expression SEMICOLON
4422 $$ = new Throw ((Expression) $2, (Location) $1);
4427 : IDENTIFIER RETURN expression SEMICOLON
4429 LocatedToken lt = (LocatedToken) $1;
4430 string s = lt.Value;
4432 Report.Error (1003, lt.Location, "; expected");
4435 if (RootContext.Version == LanguageVersion.ISO_1){
4436 Report.FeatureIsNotAvailable (lt.Location, "yield statement");
4439 if (anonymous_host == null){
4440 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4443 anonymous_host.SetYields ();
4444 $$ = new Yield ((Expression) $3, lt.Location);
4447 | IDENTIFIER RETURN SEMICOLON
4449 Report.Error (1627, (Location) $2, "Expression expected after yield return");
4452 | IDENTIFIER BREAK SEMICOLON
4454 LocatedToken lt = (LocatedToken) $1;
4455 string s = lt.Value;
4457 Report.Error (1003, lt.Location, "; expected");
4460 if (RootContext.Version == LanguageVersion.ISO_1){
4461 Report.FeatureIsNotAvailable (lt.Location, "yield statement");
4464 if (anonymous_host == null){
4465 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4468 anonymous_host.SetYields ();
4469 $$ = new YieldBreak (lt.Location);
4480 : TRY block catch_clauses
4484 ArrayList c = (ArrayList)$3;
4485 for (int i = 0; i < c.Count; ++i) {
4486 Catch cc = (Catch) c [i];
4488 if (i != c.Count - 1)
4489 Report.Error (1017, cc.loc, "Try statement already has an empty catch block");
4496 // Now s contains the list of specific catch clauses
4497 // and g contains the general one.
4499 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4501 | TRY block opt_catch_clauses FINALLY block
4504 ArrayList s = new ArrayList (4);
4505 ArrayList catch_list = (ArrayList) $3;
4507 if (catch_list != null){
4508 foreach (Catch cc in catch_list) {
4516 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4520 Report.Error (1524, (Location) $1, "Expected catch or finally");
4526 : /* empty */ { $$ = null; }
4533 ArrayList l = new ArrayList (4);
4538 | catch_clauses catch_clause
4540 ArrayList l = (ArrayList) $1;
4548 : /* empty */ { $$ = null; }
4553 : CATCH opt_catch_args
4555 Expression type = null;
4558 DictionaryEntry cc = (DictionaryEntry) $2;
4559 type = (Expression) cc.Key;
4560 LocatedToken lt = (LocatedToken) cc.Value;
4563 ArrayList one = new ArrayList (4);
4565 one.Add (new VariableDeclaration (lt, null));
4567 start_block (lexer.Location);
4568 current_block = declare_local_variables (type, one, lt.Location);
4572 Expression type = null;
4574 Block var_block = null;
4577 DictionaryEntry cc = (DictionaryEntry) $2;
4578 type = (Expression) cc.Key;
4579 LocatedToken lt = (LocatedToken) cc.Value;
4583 var_block = end_block (lexer.Location);
4587 $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
4592 : /* empty */ { $$ = null; }
4597 : open_parens type opt_identifier CLOSE_PARENS
4599 $$ = new DictionaryEntry ($2, $3);
4607 $$ = new Checked ((Block) $2);
4614 $$ = new Unchecked ((Block) $2);
4621 RootContext.CheckUnsafeOption ((Location) $1);
4623 $$ = new Unsafe ((Block) $3);
4629 type fixed_pointer_declarators
4632 ArrayList list = (ArrayList) $4;
4633 Expression type = (Expression) $3;
4634 Location l = (Location) $1;
4635 int top = list.Count;
4637 start_block (lexer.Location);
4639 for (int i = 0; i < top; i++){
4640 Pair p = (Pair) list [i];
4643 v = current_block.AddVariable (type, (string) p.First, l);
4647 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4655 Location l = (Location) $1;
4657 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4659 current_block.AddStatement (f);
4661 $$ = end_block (lexer.Location);
4665 fixed_pointer_declarators
4666 : fixed_pointer_declarator {
4667 ArrayList declarators = new ArrayList (4);
4669 declarators.Add ($1);
4672 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4674 ArrayList declarators = (ArrayList) $1;
4676 declarators.Add ($3);
4681 fixed_pointer_declarator
4682 : IDENTIFIER ASSIGN expression
4684 LocatedToken lt = (LocatedToken) $1;
4685 // FIXME: keep location
4686 $$ = new Pair (lt.Value, $3);
4690 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
4696 : LOCK OPEN_PARENS expression CLOSE_PARENS
4702 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
4707 : USING open_parens resource_acquisition CLOSE_PARENS
4709 start_block (lexer.Location);
4710 Block assign_block = current_block;
4712 if ($3 is DictionaryEntry){
4713 DictionaryEntry de = (DictionaryEntry) $3;
4714 Location l = (Location) $1;
4716 Expression type = (Expression) de.Key;
4717 ArrayList var_declarators = (ArrayList) de.Value;
4719 ArrayList vars = new ArrayList (4);
4721 foreach (VariableDeclaration decl in var_declarators){
4723 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4726 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
4728 Expression expr = decl.expression_or_array_initializer;
4730 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4733 LocalVariableReference var;
4735 // Get a writable reference to this read-only variable.
4736 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4738 // This is so that it is not a warning on using variables
4741 vars.Add (new DictionaryEntry (var, expr));
4743 // Assign a = new Assign (var, expr, decl.Location);
4744 // assign_block.AddStatement (new StatementExpression (a));
4747 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4748 // It can be referred to as $5 below.
4749 $$ = new DictionaryEntry (type, vars);
4756 Using u = new Using ($5, (Statement) $6, (Location) $1);
4757 current_block.AddStatement (u);
4758 $$ = end_block (lexer.Location);
4762 resource_acquisition
4763 : local_variable_declaration
4770 // A class used to pass around variable declarations and constants
4772 public class VariableDeclaration {
4773 public string identifier;
4774 public Expression expression_or_array_initializer;
4775 public Location Location;
4776 public Attributes OptAttributes;
4777 public string DocComment;
4779 public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
4781 this.identifier = lt.Value;
4782 if (eoai is ArrayList) {
4783 if (CSharpParser.current_array_type == null)
4784 Report.Error (622, lt.Location,
4785 "Can only use array initializer expressions to assign to array types. Try using a new expression instead.");
4786 this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
4788 this.expression_or_array_initializer = (Expression)eoai;
4790 this.Location = lt.Location;
4791 this.OptAttributes = opt_attrs;
4794 public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
4800 // A class used to hold info about an indexer declarator
4802 public class IndexerDeclaration {
4803 public Expression type;
4804 public MemberName interface_type;
4805 public Parameters param_list;
4806 public Location location;
4808 public IndexerDeclaration (Expression type, MemberName interface_type,
4809 Parameters param_list, Location loc)
4812 this.interface_type = interface_type;
4813 this.param_list = param_list;
4814 this.location = loc;
4819 // We use this when we do not have an object in advance that is an IAnonymousHost
4821 public class SimpleAnonymousHost : IAnonymousHost {
4822 public static readonly SimpleAnonymousHost Simple = new SimpleAnonymousHost ();
4825 ArrayList anonymous_methods;
4827 public static SimpleAnonymousHost GetSimple () {
4828 Simple.yields = false;
4829 Simple.anonymous_methods = null;
4833 public void SetYields ()
4838 public void AddAnonymousMethod (AnonymousMethodExpression anonymous)
4840 if (anonymous_methods == null)
4841 anonymous_methods = new ArrayList ();
4842 anonymous_methods.Add (anonymous);
4845 public void Propagate (IAnonymousHost real_host)
4848 real_host.SetYields ();
4849 if (anonymous_methods != null) {
4850 foreach (AnonymousMethodExpression ame in anonymous_methods)
4851 real_host.AddAnonymousMethod (ame);
4857 // A class used to hold info about an operator declarator
4859 public class OperatorDeclaration {
4860 public Operator.OpType optype;
4861 public Expression ret_type, arg1type, arg2type;
4862 public string arg1name, arg2name;
4863 public Location location;
4865 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4866 Expression arg1type, string arg1name,
4867 Expression arg2type, string arg2name, Location location)
4870 this.ret_type = ret_type;
4871 this.arg1type = arg1type;
4872 this.arg1name = arg1name;
4873 this.arg2type = arg2type;
4874 this.arg2name = arg2name;
4875 this.location = location;
4880 void Error_ExpectingTypeName (Expression expr)
4882 if (expr is Invocation){
4883 Report.Error (1002, expr.Location, "Expecting `;'");
4885 expr.Error_InvalidExpressionStatement ();
4889 public static void Error_ParameterModifierNotValid (Location loc)
4891 Report.Error (631, loc, "The modifiers `ref' and `out' are not valid in this context");
4894 static void Error_DuplicateParameterModifier (Location loc, Parameter.Modifier mod)
4896 Report.Error (1107, loc, "Duplicate parameter modifier `{0}'",
4897 Parameter.GetModifierSignature (mod));
4900 void push_current_class (TypeContainer tc, object partial_token)
4902 if (partial_token != null)
4903 current_container = current_container.AddPartial (tc);
4905 current_container = current_container.AddTypeContainer (tc);
4909 DeclSpace pop_current_class ()
4911 DeclSpace retval = current_class;
4913 current_class = current_class.Parent;
4914 current_container = current_class.PartialContainer;
4920 // Given the @class_name name, it creates a fully qualified name
4921 // based on the containing declaration space
4924 MakeName (MemberName class_name)
4926 Namespace ns = current_namespace.NS;
4928 if (current_container.Name.Length == 0){
4929 if (ns.Name.Length != 0)
4930 return new MemberName (ns.MemberName, class_name);
4934 return new MemberName (current_container.MemberName, class_name);
4938 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4940 Block implicit_block;
4941 ArrayList inits = null;
4944 // We use the `Used' property to check whether statements
4945 // have been added to the current block. If so, we need
4946 // to create another block to contain the new declaration
4947 // otherwise, as an optimization, we use the same block to
4948 // add the declaration.
4950 // FIXME: A further optimization is to check if the statements
4951 // that were added were added as part of the initialization
4952 // below. In which case, no other statements have been executed
4953 // and we might be able to reduce the number of blocks for
4954 // situations like this:
4956 // int j = 1; int k = j + 1;
4958 if (current_block.Used)
4959 implicit_block = new Block (current_block, loc, Location.Null);
4961 implicit_block = current_block;
4963 foreach (VariableDeclaration decl in variable_declarators){
4965 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
4966 if (decl.expression_or_array_initializer != null){
4968 inits = new ArrayList (4);
4975 return implicit_block;
4977 foreach (VariableDeclaration decl in inits){
4979 Expression expr = decl.expression_or_array_initializer;
4981 LocalVariableReference var;
4982 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4984 assign = new Assign (var, expr, decl.Location);
4986 implicit_block.AddStatement (new StatementExpression (assign));
4989 return implicit_block;
4992 Block declare_local_constants (Expression type, ArrayList declarators)
4994 Block implicit_block;
4996 if (current_block.Used)
4997 implicit_block = new Block (current_block);
4999 implicit_block = current_block;
5001 foreach (VariableDeclaration decl in declarators){
5002 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
5005 return implicit_block;
5008 string CheckAttributeTarget (string a, Location l)
5011 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
5015 Report.Warning (658, 1, l,
5016 "`{0}' is invalid attribute target. All attributes in this attribute section will be ignored", a);
5017 return string.Empty;
5020 void CheckUnaryOperator (Operator.OpType op, Location l)
5024 case Operator.OpType.LogicalNot:
5025 case Operator.OpType.OnesComplement:
5026 case Operator.OpType.Increment:
5027 case Operator.OpType.Decrement:
5028 case Operator.OpType.True:
5029 case Operator.OpType.False:
5030 case Operator.OpType.Addition:
5031 case Operator.OpType.Subtraction:
5036 Report.Error (1019, l, "Overloadable unary operator expected");
5042 void CheckBinaryOperator (Operator.OpType op, Location l)
5046 case Operator.OpType.Addition:
5047 case Operator.OpType.Subtraction:
5048 case Operator.OpType.Multiply:
5049 case Operator.OpType.Division:
5050 case Operator.OpType.Modulus:
5051 case Operator.OpType.BitwiseAnd:
5052 case Operator.OpType.BitwiseOr:
5053 case Operator.OpType.ExclusiveOr:
5054 case Operator.OpType.LeftShift:
5055 case Operator.OpType.RightShift:
5056 case Operator.OpType.Equality:
5057 case Operator.OpType.Inequality:
5058 case Operator.OpType.GreaterThan:
5059 case Operator.OpType.LessThan:
5060 case Operator.OpType.GreaterThanOrEqual:
5061 case Operator.OpType.LessThanOrEqual:
5065 Report.Error (1020, l, "Overloadable binary operator expected");
5071 void syntax_error (Location l, string msg)
5073 Report.Error (1003, l, "Syntax error, " + msg);
5076 void note (string s)
5078 // Used to put annotations
5083 public Tokenizer Lexer {
5089 static CSharpParser ()
5091 oob_stack = new Stack ();
5094 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
5096 this.name = file.Name;
5098 current_namespace = new NamespaceEntry (null, file, null);
5099 current_class = current_namespace.SlaveDeclSpace;
5100 current_container = current_class.PartialContainer; // == RootContest.ToplevelTypes
5102 lexer = new Tokenizer (reader, file, defines);
5105 public void parse ()
5107 int errors = Report.Errors;
5109 if (yacc_verbose_flag > 1)
5110 yyparse (lexer, new yydebug.yyDebugSimple ());
5113 Tokenizer tokenizer = lexer as Tokenizer;
5114 tokenizer.cleanup ();
5115 } catch (Exception e){
5117 // Removed for production use, use parser verbose to get the output.
5119 // Console.WriteLine (e);
5120 if (Report.Errors == errors)
5121 Report.Error (-25, lexer.Location, "Parsing error");
5122 if (yacc_verbose_flag > 0)
5123 Console.WriteLine (e);
5126 if (RootContext.ToplevelTypes.NamespaceEntry != null)
5127 throw new InternalErrorException ("who set it?");
5130 static void CheckToken (int error, int yyToken, string msg, Location loc)
5132 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
5133 Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ());
5135 Report.Error (error, loc, msg);
5138 void CheckIdentifierToken (int yyToken, Location loc)
5140 CheckToken (1041, yyToken, "Identifier expected", loc);
5143 string ConsumeStoredComment ()
5145 string s = tmpComment;
5147 Lexer.doc_state = XmlCommentState.Allowed;
5151 Location GetLocation (object obj)
5153 if (obj is MemberCore)
5154 return ((MemberCore) obj).Location;
5155 if (obj is MemberName)
5156 return ((MemberName) obj).Location;
5157 if (obj is LocatedToken)
5158 return ((LocatedToken) obj).Location;
5159 if (obj is Location)
5160 return (Location) obj;
5161 return lexer.Location;
5164 static GenericMethod current_generic_method = null;
5166 void start_block (Location loc)
5168 if (current_block == null || parsing_anonymous_method) {
5169 current_block = new ToplevelBlock (current_block, current_local_parameters, current_generic_method, loc);
5170 parsing_anonymous_method = false;
5172 current_block = new ExplicitBlock (current_block, loc, Location.Null);
5177 end_block (Location loc)
5179 Block retval = current_block.Explicit;
5180 retval.SetEndLocation (loc);
5181 current_block = retval.Parent;
5186 start_anonymous (bool lambda, Parameters parameters, Location loc)
5188 oob_stack.Push (current_anonymous_method);
5189 oob_stack.Push (current_local_parameters);
5191 current_local_parameters = parameters;
5193 ToplevelBlock top_current_block = current_block == null ? null : current_block.Toplevel;
5195 current_anonymous_method = lambda
5196 ? new LambdaExpression (
5197 current_anonymous_method, current_generic_method, current_container,
5198 parameters, top_current_block, loc)
5199 : new AnonymousMethodExpression (
5200 current_anonymous_method, current_generic_method, current_container,
5201 parameters, top_current_block, loc);
5203 // Force the next block to be created as a ToplevelBlock
5204 parsing_anonymous_method = true;
5208 * Completes the anonymous method processing, if lambda_expr is null, this
5209 * means that we have a Statement instead of an Expression embedded
5211 AnonymousMethodExpression
5212 end_anonymous (ToplevelBlock anon_block, Location loc)
5214 AnonymousMethodExpression retval;
5216 if (RootContext.Version == LanguageVersion.ISO_1){
5217 Report.FeatureIsNotAvailable (loc, "anonymous methods");
5220 current_anonymous_method.Block = anon_block;
5221 if ((anonymous_host != null) && (current_anonymous_method.Parent == null))
5222 anonymous_host.AddAnonymousMethod (current_anonymous_method);
5224 retval = current_anonymous_method;
5227 current_local_parameters = (Parameters) oob_stack.Pop ();
5228 current_anonymous_method = (AnonymousMethodExpression) oob_stack.Pop ();