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
45 Block current_block, top_current_block;
47 Delegate current_delegate;
49 AnonymousMethodExpression current_anonymous_method;
52 /// This is used by the unary_expression code to resolve
53 /// a name against a parameter.
55 Parameters current_local_parameters;
58 /// Using during property parsing to describe the implicit
59 /// value parameter that is passed to the "set" and "get"accesor
60 /// methods (properties and indexers).
62 Expression implicit_value_parameter_type;
63 Parameters indexer_parameters;
66 /// Hack to help create non-typed array initializer
68 public static Expression current_array_type;
69 Expression pushed_current_array_type;
72 /// Used to determine if we are parsing the get/set pair
73 /// of an indexer or a property
77 bool parsing_anonymous_method;
80 /// An out-of-band stack.
82 static Stack oob_stack;
89 static public int yacc_verbose_flag;
91 // Name of the file we are parsing
100 /// Temporary Xml documentation cache.
101 /// For enum types, we need one more temporary store.
104 string enumTypeComment;
106 /// Current attribute target
107 string current_attr_target;
109 /// assembly and module attribute definitions are enabled
110 bool global_attrs_enabled = true;
111 bool has_get, has_set;
116 %token NONE /* This token is never returned by our lexer */
117 %token ERROR // This is used not by the parser, but by the tokenizer.
121 *These are the C# keywords
208 /* C# keywords which are not really keywords */
214 /* C# single character operators/punctuation. */
215 %token OPEN_BRACE "{"
216 %token CLOSE_BRACE "}"
217 %token OPEN_BRACKET "["
218 %token CLOSE_BRACKET "]"
219 %token OPEN_PARENS "("
220 %token CLOSE_PARENS ")"
232 %token OP_GENERICS_LT "<"
234 %token OP_GENERICS_GT ">"
235 %token BITWISE_AND "&"
236 %token BITWISE_OR "|"
243 /* C# multi-character operators. */
244 %token DOUBLE_COLON "::"
247 %token OP_SHIFT_LEFT "<<"
248 %token OP_SHIFT_RIGHT ">>"
255 %token OP_MULT_ASSIGN "*="
256 %token OP_DIV_ASSIGN "/="
257 %token OP_MOD_ASSIGN "%="
258 %token OP_ADD_ASSIGN "+="
259 %token OP_SUB_ASSIGN "-="
260 %token OP_SHIFT_LEFT_ASSIGN "<<="
261 %token OP_SHIFT_RIGHT_ASSIGN ">>="
262 %token OP_AND_ASSIGN "&="
263 %token OP_XOR_ASSIGN "^="
264 %token OP_OR_ASSIGN "|="
268 %token LITERAL_INTEGER "int literal"
269 %token LITERAL_FLOAT "float literal"
270 %token LITERAL_DOUBLE "double literal"
271 %token LITERAL_DECIMAL "decimal literal"
272 %token LITERAL_CHARACTER "character literal"
273 %token LITERAL_STRING "string literal"
276 %token OPEN_PARENS_LAMBDA
277 %token CLOSE_PARENS_CAST
278 %token CLOSE_PARENS_NO_CAST
279 %token CLOSE_PARENS_OPEN_PARENS
280 %token CLOSE_PARENS_MINUS
282 /* Add precedence rules to solve dangling else s/r conflict */
291 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
293 %left STAR DIV PERCENT
294 %right BANG CARRET UMINUS
295 %nonassoc OP_INC OP_DEC
297 %left OPEN_BRACKET OPEN_BRACE
301 %start compilation_unit
305 : outer_declarations opt_EOF
306 | outer_declarations global_attributes opt_EOF
307 | global_attributes opt_EOF
308 | opt_EOF /* allow empty files */
314 Lexer.check_incorrect_doc_comment ();
318 Lexer.check_incorrect_doc_comment ();
324 | outer_declarations outer_declaration
328 : extern_alias_directive
330 | namespace_member_declaration
333 extern_alias_directives
334 : extern_alias_directive
335 | extern_alias_directives extern_alias_directive;
337 extern_alias_directive
338 : EXTERN IDENTIFIER IDENTIFIER SEMICOLON
340 LocatedToken lt = (LocatedToken) $2;
343 Report.Error (1003, lt.Location, "'alias' expected");
344 } else if (RootContext.Version == LanguageVersion.ISO_1) {
345 Report.FeatureIsNotISO1 (lt.Location, "external alias");
347 lt = (LocatedToken) $3;
348 current_namespace.UsingExternalAlias (lt.Value, lt.Location);
355 | using_directives using_directive
359 : using_alias_directive
361 if (RootContext.Documentation != null)
362 Lexer.doc_state = XmlCommentState.Allowed;
364 | using_namespace_directive
366 if (RootContext.Documentation != null)
367 Lexer.doc_state = XmlCommentState.Allowed;
371 using_alias_directive
372 : USING IDENTIFIER ASSIGN
373 namespace_or_type_name SEMICOLON
375 LocatedToken lt = (LocatedToken) $2;
376 current_namespace.UsingAlias (lt.Value, (MemberName) $4, (Location) $1);
379 CheckIdentifierToken (yyToken, GetLocation ($2));
383 using_namespace_directive
384 : USING namespace_name SEMICOLON
386 current_namespace.Using ((MemberName) $2, (Location) $1);
391 // Strictly speaking, namespaces don't have attributes but
392 // we parse global attributes along with namespace declarations and then
395 namespace_declaration
396 : opt_attributes NAMESPACE namespace_or_type_name
398 MemberName name = (MemberName) $3;
401 Report.Error(1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
404 current_namespace = new NamespaceEntry (
405 current_namespace, file, name.GetName ());
406 current_class = current_namespace.SlaveDeclSpace;
407 current_container = current_class.PartialContainer;
409 namespace_body opt_semicolon
411 current_namespace = current_namespace.Parent;
412 current_class = current_namespace.SlaveDeclSpace;
413 current_container = current_class.PartialContainer;
428 : namespace_or_type_name
434 if (RootContext.Documentation != null)
435 Lexer.doc_state = XmlCommentState.Allowed;
441 : opt_extern_alias_directives
443 opt_namespace_member_declarations
447 Report.Error (1518, lexer.Location, "Expected `class', `delegate', `enum', `interface', or `struct'");
450 | opt_extern_alias_directives
452 opt_namespace_member_declarations
455 Report.Error (1513, lexer.Location, "} expected");
464 opt_extern_alias_directives
466 | extern_alias_directives
469 opt_namespace_member_declarations
471 | namespace_member_declarations
474 namespace_member_declarations
475 : namespace_member_declaration
476 | namespace_member_declarations namespace_member_declaration
479 namespace_member_declaration
483 DeclSpace ds = (DeclSpace)$1;
485 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
486 Report.Error (1527, ds.Location,
487 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
490 current_namespace.DeclarationFound = true;
492 | namespace_declaration {
493 current_namespace.DeclarationFound = true;
496 | field_declaration {
497 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
499 | method_declaration {
500 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
507 | interface_declaration
509 | delegate_declaration
511 // Enable this when we have handled all errors, because this acts as a generic fallback
514 // Console.WriteLine ("Token=" + yyToken);
515 // Report.Error (1518, GetLocation ($1), "Expected class, struct, interface, enum or delegate");
527 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
535 global_attrs_enabled = false;
540 global_attrs_enabled = false;
549 if (current_attr_target != String.Empty) {
550 ArrayList sect = (ArrayList) $1;
552 if (global_attrs_enabled) {
553 if (current_attr_target == "module") {
554 CodeGen.Module.AddAttributes (sect);
556 } else if (current_attr_target != null && current_attr_target.Length > 0) {
557 CodeGen.Assembly.AddAttributes (sect);
560 $$ = new Attributes (sect);
563 if (RootContext.Documentation != null) {
564 Lexer.check_incorrect_doc_comment ();
566 XmlCommentState.Allowed;
570 $$ = new Attributes (sect);
575 current_attr_target = null;
577 | attribute_sections attribute_section
579 if (current_attr_target != String.Empty) {
580 Attributes attrs = $1 as Attributes;
581 ArrayList sect = (ArrayList) $2;
583 if (global_attrs_enabled) {
584 if (current_attr_target == "module") {
585 CodeGen.Module.AddAttributes (sect);
587 } else if (current_attr_target == "assembly") {
588 CodeGen.Assembly.AddAttributes (sect);
592 attrs = new Attributes (sect);
594 attrs.AddAttributes (sect);
598 attrs = new Attributes (sect);
600 attrs.AddAttributes (sect);
606 current_attr_target = null;
611 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
615 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
621 attribute_target_specifier
622 : attribute_target COLON
624 current_attr_target = (string)$1;
632 LocatedToken lt = (LocatedToken) $1;
633 $$ = CheckAttributeTarget (lt.Value, lt.Location);
635 | EVENT { $$ = "event"; }
636 | RETURN { $$ = "return"; }
639 string name = yyNames [yyToken].ToLower ();
640 $$ = CheckAttributeTarget (name, GetLocation ($1));
647 ArrayList attrs = new ArrayList (4);
653 | attribute_list COMMA attribute
655 ArrayList attrs = (ArrayList) $1;
663 : attribute_name opt_attribute_arguments
665 MemberName mname = (MemberName) $1;
666 object[] arguments = (object[]) $2;
667 MemberName left = mname.Left;
668 string identifier = mname.Name;
670 Expression left_expr = left == null ? null : left.GetTypeExpression ();
672 if (current_attr_target == String.Empty)
674 else if (current_attr_target == "assembly" || current_attr_target == "module")
675 // FIXME: supply "nameEscaped" parameter here.
676 $$ = new GlobalAttribute (current_namespace, current_attr_target,
677 left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
679 $$ = new Attribute (current_attr_target, left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
684 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
687 opt_attribute_arguments
688 : /* empty */ { $$ = null; }
689 | OPEN_PARENS attribute_arguments CLOSE_PARENS
697 : opt_positional_argument_list
702 $$ = new object [] { $1, null };
705 | positional_argument_list COMMA named_argument_list
707 $$ = new object[] { $1, $3 };
709 | named_argument_list
711 $$ = new object [] { null, $1 };
716 opt_positional_argument_list
717 : /* empty */ { $$ = null; }
718 | positional_argument_list
721 positional_argument_list
724 ArrayList args = new ArrayList (4);
725 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
729 | positional_argument_list COMMA expression
731 ArrayList args = (ArrayList) $1;
732 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
741 ArrayList args = new ArrayList (4);
746 | named_argument_list COMMA named_argument
748 ArrayList args = (ArrayList) $1;
753 | named_argument_list COMMA expression
755 Report.Error (1016, ((Expression) $3).Location, "Named attribute argument expected");
761 : IDENTIFIER ASSIGN expression
763 // FIXME: keep location
764 $$ = new DictionaryEntry (
765 ((LocatedToken) $1).Value,
766 new Argument ((Expression) $3, Argument.AType.Expression));
772 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
775 opt_class_member_declarations
777 | class_member_declarations
780 class_member_declarations
781 : class_member_declaration
782 | class_member_declarations
783 class_member_declaration
786 class_member_declaration
787 : constant_declaration // done
788 | field_declaration // done
789 | method_declaration // done
790 | property_declaration // done
791 | event_declaration // done
792 | indexer_declaration // done
793 | operator_declaration // done
794 | constructor_declaration // done
795 | destructor_declaration // done
805 MemberName name = MakeName ((MemberName) $5);
806 push_current_class (new Struct (
807 current_namespace, current_class, name, (int) $2,
808 (Attributes) $1), $3);
814 current_container.AddBasesForPart (current_class, (ArrayList) $7);
816 if (RootContext.Documentation != null)
817 current_container.DocComment = Lexer.consume_doc_comment ();
821 if (RootContext.Documentation != null)
822 Lexer.doc_state = XmlCommentState.Allowed;
826 $$ = pop_current_class ();
828 | opt_attributes opt_modifiers opt_partial STRUCT error {
829 CheckIdentifierToken (yyToken, GetLocation ($5));
836 if (RootContext.Documentation != null)
837 Lexer.doc_state = XmlCommentState.Allowed;
839 opt_struct_member_declarations CLOSE_BRACE
842 opt_struct_member_declarations
844 | struct_member_declarations
847 struct_member_declarations
848 : struct_member_declaration
849 | struct_member_declarations struct_member_declaration
852 struct_member_declaration
853 : constant_declaration
856 | property_declaration
858 | indexer_declaration
859 | operator_declaration
860 | constructor_declaration
864 * This is only included so we can flag error 575:
865 * destructors only allowed on class types
867 | destructor_declaration
878 int modflags = (int) $2;
879 foreach (VariableDeclaration constant in (ArrayList) $5){
880 Location l = constant.Location;
881 if ((modflags & Modifiers.STATIC) != 0) {
882 Report.Error (504, l, "The constant `{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
886 Const c = new Const (
887 current_class, (Expression) $4, (string) constant.identifier,
888 (Expression) constant.expression_or_array_initializer, modflags,
891 if (RootContext.Documentation != null) {
892 c.DocComment = Lexer.consume_doc_comment ();
893 Lexer.doc_state = XmlCommentState.Allowed;
895 current_container.AddConstant (c);
901 : constant_declarator
903 ArrayList constants = new ArrayList (4);
908 | constant_declarators COMMA constant_declarator
911 ArrayList constants = (ArrayList) $1;
918 : IDENTIFIER ASSIGN constant_expression
920 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
924 // A const field requires a value to be provided
925 Report.Error (145, ((LocatedToken) $1).Location, "A const field requires a value to be provided");
937 Expression type = (Expression) $3;
940 current_array_type = null;
942 foreach (VariableDeclaration var in (ArrayList) $4){
943 Field field = new Field (current_class, type, mod, var.identifier,
944 (Attributes) $1, var.Location);
946 field.Initializer = var.expression_or_array_initializer;
948 if (RootContext.Documentation != null) {
949 field.DocComment = Lexer.consume_doc_comment ();
950 Lexer.doc_state = XmlCommentState.Allowed;
952 current_container.AddField (field);
953 $$ = field; // FIXME: might be better if it points to the top item
960 fixed_variable_declarators
963 Expression type = (Expression) $4;
966 current_array_type = null;
968 foreach (VariableDeclaration var in (ArrayList) $5) {
969 FixedField field = new FixedField (current_class, type, mod, var.identifier,
970 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
972 if (RootContext.Documentation != null) {
973 field.DocComment = Lexer.consume_doc_comment ();
974 Lexer.doc_state = XmlCommentState.Allowed;
976 current_container.AddField (field);
977 $$ = field; // FIXME: might be better if it points to the top item
986 Report.Error (1641, GetLocation ($4), "A fixed size buffer field must have the array size specifier after the field name");
993 current_array_type = null;
994 Report.Error (670, (Location) $3, "Fields cannot have void type");
998 fixed_variable_declarators
999 : fixed_variable_declarator
1001 ArrayList decl = new ArrayList (2);
1005 | fixed_variable_declarators COMMA fixed_variable_declarator
1007 ArrayList decls = (ArrayList) $1;
1013 fixed_variable_declarator
1014 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
1016 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1018 | IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1020 Report.Error (443, lexer.Location, "Value or constant expected");
1021 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1025 variable_declarators
1026 : variable_declarator
1028 ArrayList decl = new ArrayList (4);
1033 | variable_declarators COMMA variable_declarator
1035 ArrayList decls = (ArrayList) $1;
1042 : IDENTIFIER ASSIGN variable_initializer
1044 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1048 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1050 | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
1052 Report.Error (650, ((LocatedToken) $1).Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
1053 "To declare a fixed size buffer field, use the fixed keyword before the field type");
1058 variable_initializer
1067 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
1069 $$ = new StackAlloc ((Expression) $2, (Expression) $4, (Location) $1);
1073 $$ = new ArglistAccess ((Location) $1);
1077 Report.Error (1575, (Location) $1, "A stackalloc expression requires [] after type");
1084 anonymous_host = (IAnonymousHost) $1;
1085 if (RootContext.Documentation != null)
1086 Lexer.doc_state = XmlCommentState.NotAllowed;
1090 Method method = (Method) $1;
1091 method.Block = (ToplevelBlock) $3;
1092 current_container.AddMethod (method);
1094 anonymous_host = null;
1095 current_local_parameters = null;
1097 if (RootContext.Documentation != null)
1098 Lexer.doc_state = XmlCommentState.Allowed;
1111 Report.Error (1585, lexer.Location,
1112 "Member modifier `{0}' must precede the member type and name",
1113 Modifiers.Name (i));
1122 // This rule is used to handle the cases where OPEN_PARENS_LAMBDA
1123 // is returned (type followed by an identifier), these are the
1124 // declarations (methods, overloads, constructors, etc) and a handful
1125 // of expressions ("using", "fixed") or "catch".
1129 | OPEN_PARENS_LAMBDA
1135 type namespace_or_type_name
1136 open_parens opt_formal_parameter_list CLOSE_PARENS
1138 MemberName name = (MemberName) $4;
1140 Method method = new Method (current_class, null, (Expression) $3, (int) $2,
1141 false, name, (Parameters) $6, (Attributes) $1);
1143 anonymous_host = method;
1144 current_local_parameters = (Parameters) $6;
1146 if (RootContext.Documentation != null)
1147 method.DocComment = Lexer.consume_doc_comment ();
1153 VOID namespace_or_type_name
1154 open_parens opt_formal_parameter_list CLOSE_PARENS
1156 MemberName name = (MemberName) $4;
1158 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1159 (int) $2, false, name, (Parameters) $6,
1162 anonymous_host = method;
1163 current_local_parameters = (Parameters) $6;
1165 if (RootContext.Documentation != null)
1166 method.DocComment = Lexer.consume_doc_comment ();
1173 modifiers namespace_or_type_name open_parens opt_formal_parameter_list CLOSE_PARENS
1175 MemberName name = (MemberName) $5;
1176 Report.Error (1585, name.Location,
1177 "Member modifier `{0}' must precede the member type and name", Modifiers.Name ((int) $4));
1179 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1180 0, false, name, (Parameters) $7, (Attributes) $1);
1182 current_local_parameters = (Parameters) $7;
1184 if (RootContext.Documentation != null)
1185 method.DocComment = Lexer.consume_doc_comment ();
1193 | SEMICOLON { $$ = null; }
1196 opt_formal_parameter_list
1197 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1198 | formal_parameter_list
1201 formal_parameter_list
1204 ArrayList pars_list = (ArrayList) $1;
1206 Parameter [] pars = new Parameter [pars_list.Count];
1207 pars_list.CopyTo (pars);
1209 $$ = new Parameters (pars);
1211 | fixed_parameters COMMA parameter_array
1213 ArrayList pars_list = (ArrayList) $1;
1216 Parameter [] pars = new Parameter [pars_list.Count];
1217 pars_list.CopyTo (pars);
1219 $$ = new Parameters (pars);
1221 | fixed_parameters COMMA ARGLIST
1223 ArrayList pars_list = (ArrayList) $1;
1224 //pars_list.Add (new ArglistParameter (GetLocation ($3)));
1226 Parameter [] pars = new Parameter [pars_list.Count];
1227 pars_list.CopyTo (pars);
1229 $$ = new Parameters (pars, true);
1231 | parameter_array COMMA error
1234 Report.Error (231, ((Parameter) $1).Location, "A params parameter must be the last parameter in a formal parameter list");
1237 | fixed_parameters COMMA parameter_array COMMA error
1240 Report.Error (231, ((Parameter) $3).Location, "A params parameter must be the last parameter in a formal parameter list");
1243 | ARGLIST COMMA error
1245 Report.Error (257, (Location) $1, "An __arglist parameter must be the last parameter in a formal parameter list");
1248 | fixed_parameters COMMA ARGLIST COMMA error
1250 Report.Error (257, (Location) $3, "An __arglist parameter must be the last parameter in a formal parameter list");
1255 $$ = new Parameters (new Parameter[] { (Parameter) $1 } );
1259 $$ = new Parameters (new Parameter[0], true);
1266 ArrayList pars = new ArrayList (4);
1271 | fixed_parameters COMMA fixed_parameter
1273 ArrayList pars = (ArrayList) $1;
1282 opt_parameter_modifier
1286 LocatedToken lt = (LocatedToken) $4;
1287 $$ = new Parameter ((Expression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1290 opt_parameter_modifier
1292 IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1294 LocatedToken lt = (LocatedToken) $4;
1295 Report.Error (1552, lt.Location, "Array type specifier, [], must appear before parameter name");
1299 opt_parameter_modifier
1302 Report.Error (1001, GetLocation ($3), "Identifier expected");
1306 opt_parameter_modifier
1309 CheckIdentifierToken (yyToken, GetLocation ($4));
1313 opt_parameter_modifier
1319 LocatedToken lt = (LocatedToken) $4;
1320 Report.Error (241, lt.Location, "Default parameter specifiers are not permitted");
1325 opt_parameter_modifier
1326 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1327 | parameter_modifiers
1331 : parameter_modifier
1335 | parameter_modifiers parameter_modifier
1337 Parameter.Modifier p2 = (Parameter.Modifier)$2;
1338 Parameter.Modifier mod = (Parameter.Modifier)$1 | p2;
1339 if (((Parameter.Modifier)$1 & p2) == p2) {
1340 Error_DuplicateParameterModifier (lexer.Location, p2);
1342 Report.Error (1108, lexer.Location, "A parameter cannot have specified more than one modifier");
1349 : REF { $$ = Parameter.Modifier.REF; }
1350 | OUT { $$ = Parameter.Modifier.OUT; }
1351 | THIS { $$ = Parameter.Modifier.This; }
1355 : opt_attributes PARAMS type IDENTIFIER
1357 LocatedToken lt = (LocatedToken) $4;
1358 $$ = new ParamsParameter ((Expression) $3, lt.Value, (Attributes) $1, lt.Location);
1360 | opt_attributes PARAMS PARAMS type IDENTIFIER
1362 Error_DuplicateParameterModifier (lexer.Location, Parameter.Modifier.PARAMS);
1365 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1367 Parameter.Modifier mod = (Parameter.Modifier)$3;
1368 if ((mod & Parameter.Modifier.This) != 0) {
1369 Report.Error (1104, lexer.Location, "The parameter modifiers `this' and `params' cannot be used altogether");
1371 Report.Error (1611, (Location) $2, "The params parameter cannot be declared as ref or out");
1375 | opt_attributes PARAMS type error {
1376 CheckIdentifierToken (yyToken, GetLocation ($4));
1381 property_declaration
1385 namespace_or_type_name
1387 if (RootContext.Documentation != null)
1388 tmpComment = Lexer.consume_doc_comment ();
1392 implicit_value_parameter_type = (Expression) $3;
1394 lexer.PropertyParsing = true;
1396 accessor_declarations
1398 lexer.PropertyParsing = false;
1399 has_get = has_set = false;
1407 Accessors accessors = (Accessors) $8;
1408 Accessor get_block = accessors.get_or_add;
1409 Accessor set_block = accessors.set_or_remove;
1411 MemberName name = (MemberName) $4;
1413 prop = new Property (current_class, (Expression) $3, (int) $2,
1414 false, name, (Attributes) $1, get_block, set_block, accessors.declared_in_reverse);
1416 current_container.AddProperty (prop);
1417 implicit_value_parameter_type = null;
1419 if (RootContext.Documentation != null)
1420 prop.DocComment = ConsumeStoredComment ();
1425 accessor_declarations
1426 : get_accessor_declaration
1428 $$ = new Accessors ((Accessor) $1, null);
1430 | get_accessor_declaration accessor_declarations
1432 Accessors accessors = (Accessors) $2;
1433 accessors.get_or_add = (Accessor) $1;
1436 | set_accessor_declaration
1438 $$ = new Accessors (null, (Accessor) $1);
1440 | set_accessor_declaration accessor_declarations
1442 Accessors accessors = (Accessors) $2;
1443 accessors.set_or_remove = (Accessor) $1;
1444 accessors.declared_in_reverse = true;
1449 Report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1454 get_accessor_declaration
1455 : opt_attributes opt_modifiers GET
1457 // If this is not the case, then current_local_parameters has already
1458 // been set in indexer_declaration
1459 if (parsing_indexer == false)
1460 current_local_parameters = null;
1462 current_local_parameters = indexer_parameters;
1463 lexer.PropertyParsing = false;
1465 anonymous_host = SimpleAnonymousHost.GetSimple ();
1470 Report.Error (1007, (Location) $3, "Property accessor already defined");
1473 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1475 current_local_parameters = null;
1476 lexer.PropertyParsing = true;
1478 SimpleAnonymousHost.Simple.Propagate (accessor);
1479 anonymous_host = null;
1481 if (RootContext.Documentation != null)
1482 if (Lexer.doc_state == XmlCommentState.Error)
1483 Lexer.doc_state = XmlCommentState.NotAllowed;
1489 set_accessor_declaration
1490 : opt_attributes opt_modifiers SET
1493 Parameter implicit_value_parameter = new Parameter (
1494 implicit_value_parameter_type, "value",
1495 Parameter.Modifier.NONE, null, (Location) $3);
1497 if (parsing_indexer == false) {
1498 args = new Parameter [1];
1499 args [0] = implicit_value_parameter;
1500 current_local_parameters = new Parameters (args);
1502 Parameter [] fpars = indexer_parameters.FixedParameters;
1505 int count = fpars.Length;
1507 args = new Parameter [count + 1];
1508 fpars.CopyTo (args, 0);
1509 args [count] = implicit_value_parameter;
1512 current_local_parameters = new Parameters (
1516 lexer.PropertyParsing = false;
1518 anonymous_host = SimpleAnonymousHost.GetSimple ();
1523 Report.Error (1007, ((LocatedToken) $3).Location, "Property accessor already defined");
1526 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1528 current_local_parameters = null;
1529 lexer.PropertyParsing = true;
1531 SimpleAnonymousHost.Simple.Propagate (accessor);
1532 anonymous_host = null;
1534 if (RootContext.Documentation != null
1535 && Lexer.doc_state == XmlCommentState.Error)
1536 Lexer.doc_state = XmlCommentState.NotAllowed;
1544 | SEMICOLON { $$ = null; }
1547 interface_declaration
1551 INTERFACE member_name
1553 MemberName name = MakeName ((MemberName) $5);
1555 push_current_class (new Interface (
1556 current_namespace, current_class, name, (int) $2,
1557 (Attributes) $1), $3);
1561 current_container.AddBasesForPart (current_class, (ArrayList) $7);
1563 if (RootContext.Documentation != null) {
1564 current_container.DocComment = Lexer.consume_doc_comment ();
1565 Lexer.doc_state = XmlCommentState.Allowed;
1570 if (RootContext.Documentation != null)
1571 Lexer.doc_state = XmlCommentState.Allowed;
1575 $$ = pop_current_class ();
1577 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1578 CheckIdentifierToken (yyToken, GetLocation ($5));
1584 opt_interface_member_declarations
1588 opt_interface_member_declarations
1590 | interface_member_declarations
1593 interface_member_declarations
1594 : interface_member_declaration
1595 | interface_member_declarations interface_member_declaration
1598 interface_member_declaration
1599 : interface_method_declaration
1604 Method m = (Method) $1;
1606 if (m.IsExplicitImpl)
1607 Report.Error (541, m.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1608 m.GetSignatureForError ());
1610 current_container.AddMethod (m);
1612 if (RootContext.Documentation != null)
1613 Lexer.doc_state = XmlCommentState.Allowed;
1615 | interface_property_declaration
1620 Property p = (Property) $1;
1622 if (p.IsExplicitImpl)
1623 Report.Error (541, p.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1624 p.GetSignatureForError ());
1626 current_container.AddProperty (p);
1628 if (RootContext.Documentation != null)
1629 Lexer.doc_state = XmlCommentState.Allowed;
1631 | interface_event_declaration
1634 Event e = (Event) $1;
1636 if (e.IsExplicitImpl)
1637 Report.Error (541, e.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1638 e.GetSignatureForError ());
1640 current_container.AddEvent (e);
1643 if (RootContext.Documentation != null)
1644 Lexer.doc_state = XmlCommentState.Allowed;
1646 | interface_indexer_declaration
1651 Indexer i = (Indexer) $1;
1653 if (i.IsExplicitImpl)
1654 Report.Error (541, i.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1655 i.GetSignatureForError ());
1657 current_container.AddIndexer (i);
1659 if (RootContext.Documentation != null)
1660 Lexer.doc_state = XmlCommentState.Allowed;
1662 | delegate_declaration
1665 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1666 ((MemberCore)$1).GetSignatureForError ());
1672 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1673 ((MemberCore)$1).GetSignatureForError ());
1676 | struct_declaration
1679 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1680 ((MemberCore)$1).GetSignatureForError ());
1686 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1687 ((MemberCore)$1).GetSignatureForError ());
1690 | interface_declaration
1693 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1694 ((MemberCore)$1).GetSignatureForError ());
1697 | constant_declaration
1699 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1707 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, GetLocation ($1));
1712 interface_method_declaration
1713 : opt_attributes opt_new type namespace_or_type_name
1714 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1717 $$ = new Method (current_class, null, (Expression) $3, (int) $2, true,
1718 (MemberName) $4, (Parameters) $6, (Attributes) $1);
1719 if (RootContext.Documentation != null)
1720 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1722 | opt_attributes opt_new type namespace_or_type_name
1723 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1724 OPEN_BRACE opt_statement_list CLOSE_BRACE
1726 $$ = new Method (current_class, null, (Expression) $3, (int) $2, true,
1727 (MemberName) $4, (Parameters) $6, (Attributes) $1);
1729 Report.Error (531, lexer.Location, "`{0}': interface members cannot have a definition",
1730 ((Method)$$).GetSignatureForError ());
1734 interface_property_declaration
1739 { lexer.PropertyParsing = true; }
1740 accessor_declarations
1742 has_get = has_set = false;
1743 lexer.PropertyParsing = false;
1747 LocatedToken lt = (LocatedToken) $4;
1748 MemberName name = new MemberName (lt.Value, lt.Location);
1750 if ($3 == TypeManager.system_void_expr) {
1751 Report.Error (547, lt.Location, "`{0}': property or indexer cannot have void type", lt.Value);
1757 p = new Property (current_class, (Expression) $3, (int) $2, true,
1758 name, (Attributes) $1,
1761 Report.Error (548, p.Location, "`{0}': property or indexer must have at least one accessor", p.GetSignatureForError ());
1765 Accessors accessor = (Accessors) $7;
1766 p = new Property (current_class, (Expression) $3, (int) $2, true,
1767 name, (Attributes) $1,
1768 accessor.get_or_add, accessor.set_or_remove, accessor.declared_in_reverse);
1770 if (accessor.get_or_add != null && accessor.get_or_add.Block != null) {
1771 Report.Error (531, p.Location, "`{0}.get': interface members cannot have a definition", p.GetSignatureForError ());
1776 if (accessor.set_or_remove != null && accessor.set_or_remove.Block != null) {
1777 Report.Error (531, p.Location, "`{0}.set': interface members cannot have a definition", p.GetSignatureForError ());
1782 if (RootContext.Documentation != null)
1783 p.DocComment = Lexer.consume_doc_comment ();
1790 CheckIdentifierToken (yyToken, GetLocation ($4));
1796 interface_event_declaration
1797 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1799 LocatedToken lt = (LocatedToken) $5;
1800 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1801 new MemberName (lt.Value, lt.Location),
1803 if (RootContext.Documentation != null)
1804 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1806 | opt_attributes opt_new EVENT type error {
1807 CheckIdentifierToken (yyToken, GetLocation ($5));
1810 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1811 LocatedToken lt = (LocatedToken) $5;
1812 Report.Error (68, lt.Location, "`{0}.{1}': event in interface cannot have initializer", current_container.Name, lt.Value);
1815 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1817 lexer.EventParsing = true;
1819 event_accessor_declarations
1821 lexer.EventParsing = false;
1824 Report.Error (69, (Location) $3, "Event in interface cannot have add or remove accessors");
1829 interface_indexer_declaration
1830 : opt_attributes opt_new type THIS
1831 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1833 { lexer.PropertyParsing = true; }
1834 accessor_declarations
1836 has_get = has_set = false;
1837 lexer.PropertyParsing = false;
1843 i = new Indexer (current_class, (Expression) $3,
1844 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1845 (int) $2, true, (Parameters) $6, (Attributes) $1,
1848 Report.Error (548, i.Location, "`{0}': property or indexer must have at least one accessor", i.GetSignatureForError ());
1852 Accessors accessors = (Accessors) $10;
1853 i = new Indexer (current_class, (Expression) $3,
1854 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1855 (int) $2, true, (Parameters) $6, (Attributes) $1,
1856 accessors.get_or_add, accessors.set_or_remove, accessors.declared_in_reverse);
1858 if (accessors.get_or_add != null && accessors.get_or_add.Block != null) {
1859 Report.Error (531, i.Location, "`{0}.get': interface members cannot have a definition", i.GetSignatureForError ());
1864 if (accessors.set_or_remove != null && accessors.set_or_remove.Block != null) {
1865 Report.Error (531, i.Location, "`{0}.set': interface members cannot have a definition", i.GetSignatureForError ());
1870 if (RootContext.Documentation != null)
1871 i.DocComment = ConsumeStoredComment ();
1877 operator_declaration
1878 : opt_attributes opt_modifiers operator_declarator
1880 anonymous_host = SimpleAnonymousHost.GetSimple ();
1887 OperatorDeclaration decl = (OperatorDeclaration) $3;
1889 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1891 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null, decl.location);
1892 if (decl.arg2type != null)
1893 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null, decl.location);
1895 Operator op = new Operator (
1896 current_class, decl.optype, decl.ret_type, (int) $2,
1897 new Parameters (param_list),
1898 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1900 if (RootContext.Documentation != null) {
1901 op.DocComment = tmpComment;
1902 Lexer.doc_state = XmlCommentState.Allowed;
1905 SimpleAnonymousHost.Simple.Propagate (op);
1906 anonymous_host = null;
1908 // Note again, checking is done in semantic analysis
1909 current_container.AddOperator (op);
1911 current_local_parameters = null;
1917 | SEMICOLON { $$ = null; }
1920 : type OPERATOR overloadable_operator
1921 open_parens opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
1923 // TODO: wrong location
1924 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
1925 Error_ParameterModifierNotValid ((Location) $2);
1927 LocatedToken lt = (LocatedToken) $7;
1928 Operator.OpType op = (Operator.OpType) $3;
1929 CheckUnaryOperator (op, lt.Location);
1931 if (op == Operator.OpType.Addition)
1932 op = Operator.OpType.UnaryPlus;
1934 if (op == Operator.OpType.Subtraction)
1935 op = Operator.OpType.UnaryNegation;
1937 Parameter [] pars = new Parameter [1];
1938 Expression type = (Expression) $6;
1940 pars [0] = new Parameter (type, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1942 current_local_parameters = new Parameters (pars);
1944 if (RootContext.Documentation != null) {
1945 tmpComment = Lexer.consume_doc_comment ();
1946 Lexer.doc_state = XmlCommentState.NotAllowed;
1949 $$ = new OperatorDeclaration (op, (Expression) $1, type, lt.Value,
1950 null, null, (Location) $2);
1952 | type OPERATOR overloadable_operator
1954 opt_parameter_modifier type IDENTIFIER COMMA
1955 opt_parameter_modifier type IDENTIFIER
1958 // TODO: wrong location
1959 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE || (Parameter.Modifier)$9 != Parameter.Modifier.NONE)
1960 Error_ParameterModifierNotValid ((Location) $2);
1962 LocatedToken ltParam1 = (LocatedToken) $7;
1963 LocatedToken ltParam2 = (LocatedToken) $11;
1964 CheckBinaryOperator ((Operator.OpType) $3, (Location) $2);
1966 Parameter [] pars = new Parameter [2];
1968 Expression typeL = (Expression) $6;
1969 Expression typeR = (Expression) $10;
1971 pars [0] = new Parameter (typeL, ltParam1.Value, Parameter.Modifier.NONE, null, ltParam1.Location);
1972 pars [1] = new Parameter (typeR, ltParam2.Value, Parameter.Modifier.NONE, null, ltParam2.Location);
1974 current_local_parameters = new Parameters (pars);
1976 if (RootContext.Documentation != null) {
1977 tmpComment = Lexer.consume_doc_comment ();
1978 Lexer.doc_state = XmlCommentState.NotAllowed;
1981 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1982 typeL, ltParam1.Value,
1983 typeR, ltParam2.Value, (Location) $2);
1985 | conversion_operator_declarator
1986 | type OPERATOR overloadable_operator
1988 opt_parameter_modifier type IDENTIFIER COMMA
1989 opt_parameter_modifier type IDENTIFIER COMMA
1990 opt_parameter_modifier type IDENTIFIER
1993 Report.Error (1534, (Location) $2, "Overloaded binary operator `{0}' takes two parameters",
1994 Operator.GetName ((Operator.OpType) $3));
1997 | type OPERATOR overloadable_operator
1998 open_parens CLOSE_PARENS
2000 Report.Error (1535, (Location) $2, "Overloaded unary operator `{0}' takes one parameter",
2001 Operator.GetName ((Operator.OpType) $3));
2006 overloadable_operator
2008 : BANG { $$ = Operator.OpType.LogicalNot; }
2009 | TILDE { $$ = Operator.OpType.OnesComplement; }
2010 | OP_INC { $$ = Operator.OpType.Increment; }
2011 | OP_DEC { $$ = Operator.OpType.Decrement; }
2012 | TRUE { $$ = Operator.OpType.True; }
2013 | FALSE { $$ = Operator.OpType.False; }
2014 // Unary and binary:
2015 | PLUS { $$ = Operator.OpType.Addition; }
2016 | MINUS { $$ = Operator.OpType.Subtraction; }
2018 | STAR { $$ = Operator.OpType.Multiply; }
2019 | DIV { $$ = Operator.OpType.Division; }
2020 | PERCENT { $$ = Operator.OpType.Modulus; }
2021 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
2022 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
2023 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
2024 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
2025 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
2026 | OP_EQ { $$ = Operator.OpType.Equality; }
2027 | OP_NE { $$ = Operator.OpType.Inequality; }
2028 | OP_GT { $$ = Operator.OpType.GreaterThan; }
2029 | OP_LT { $$ = Operator.OpType.LessThan; }
2030 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
2031 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
2034 conversion_operator_declarator
2035 : IMPLICIT OPERATOR type open_parens opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
2037 // TODO: wrong location
2038 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
2039 Error_ParameterModifierNotValid (GetLocation ($4));
2041 LocatedToken lt = (LocatedToken) $7;
2042 Parameter [] pars = new Parameter [1];
2044 pars [0] = new Parameter ((Expression) $6, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2046 current_local_parameters = new Parameters (pars);
2048 if (RootContext.Documentation != null) {
2049 tmpComment = Lexer.consume_doc_comment ();
2050 Lexer.doc_state = XmlCommentState.NotAllowed;
2053 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $6, lt.Value,
2054 null, null, (Location) $2);
2056 | EXPLICIT OPERATOR type open_parens opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
2058 // TODO: wrong location
2059 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
2060 Error_ParameterModifierNotValid (GetLocation ($4));
2062 LocatedToken lt = (LocatedToken) $7;
2063 Parameter [] pars = new Parameter [1];
2065 pars [0] = new Parameter ((Expression) $6, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2067 current_local_parameters = new Parameters (pars);
2069 if (RootContext.Documentation != null) {
2070 tmpComment = Lexer.consume_doc_comment ();
2071 Lexer.doc_state = XmlCommentState.NotAllowed;
2074 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $6, lt.Value,
2075 null, null, (Location) $2);
2079 syntax_error ((Location) $1, "'operator' expected");
2083 syntax_error ((Location) $1, "'operator' expected");
2087 constructor_declaration
2090 constructor_declarator
2093 Constructor c = (Constructor) $3;
2094 c.Block = (ToplevelBlock) $4;
2095 c.OptAttributes = (Attributes) $1;
2096 c.ModFlags = (int) $2;
2098 if (RootContext.Documentation != null)
2099 c.DocComment = ConsumeStoredComment ();
2101 if (c.Name == current_container.Basename){
2102 if ((c.ModFlags & Modifiers.STATIC) != 0){
2103 if ((c.ModFlags & Modifiers.Accessibility) != 0){
2104 Report.Error (515, c.Location,
2105 "`{0}': access modifiers are not allowed on static constructors",
2106 c.GetSignatureForError ());
2109 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2111 if (c.Initializer != null){
2112 Report.Error (514, c.Location,
2113 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2114 c.GetSignatureForError ());
2117 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2120 // We let another layer check the validity of the constructor.
2121 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
2124 current_container.AddConstructor (c);
2126 current_local_parameters = null;
2127 if (RootContext.Documentation != null)
2128 Lexer.doc_state = XmlCommentState.Allowed;
2132 constructor_declarator
2133 : constructor_header
2137 | constructor_header constructor_initializer
2139 ((Constructor)$1).Initializer = (ConstructorInitializer) $2;
2147 if (RootContext.Documentation != null) {
2148 tmpComment = Lexer.consume_doc_comment ();
2149 Lexer.doc_state = XmlCommentState.Allowed;
2152 open_parens opt_formal_parameter_list CLOSE_PARENS
2154 LocatedToken lt = (LocatedToken) $1;
2155 current_local_parameters = (Parameters) $4;
2156 current_block = top_current_block = new ToplevelBlock (null,
2157 current_local_parameters, null, lt.Location);
2159 $$ = new Constructor (current_class, lt.Value, 0, current_local_parameters,
2162 anonymous_host = (IAnonymousHost) $$;
2168 | SEMICOLON { current_block = top_current_block = null; $$ = null; }
2171 constructor_initializer
2172 : COLON BASE open_parens opt_argument_list CLOSE_PARENS
2174 $$ = new ConstructorBaseInitializer ((ArrayList) $4, (Location) $2);
2176 | COLON THIS open_parens opt_argument_list CLOSE_PARENS
2178 $$ = new ConstructorThisInitializer ((ArrayList) $4, (Location) $2);
2181 Report.Error (1018, (Location) $1, "Keyword this or base expected");
2187 : /* EMPTY */ { $$ = 0; }
2188 | UNSAFE { $$ = Modifiers.UNSAFE; }
2189 | EXTERN { $$ = Modifiers.EXTERN; }
2192 destructor_declaration
2193 : opt_attributes opt_finalizer TILDE
2195 if (RootContext.Documentation != null) {
2196 tmpComment = Lexer.consume_doc_comment ();
2197 Lexer.doc_state = XmlCommentState.NotAllowed;
2200 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2202 LocatedToken lt = (LocatedToken) $5;
2203 if (lt.Value != current_container.Basename){
2204 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2205 } else if (current_container.Kind != Kind.Class){
2206 Report.Error (575, lt.Location, "Only class types can contain destructor");
2208 Location l = lt.Location;
2211 if (!RootContext.StdLib && current_container.Name == "System.Object")
2212 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2214 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2216 Method d = new Destructor (
2217 current_class, TypeManager.system_void_expr, m, "Finalize",
2218 Parameters.EmptyReadOnlyParameters, (Attributes) $1, l);
2219 if (RootContext.Documentation != null)
2220 d.DocComment = ConsumeStoredComment ();
2222 d.Block = (ToplevelBlock) $8;
2223 current_container.AddMethod (d);
2231 EVENT type variable_declarators SEMICOLON
2233 current_array_type = null;
2234 foreach (VariableDeclaration var in (ArrayList) $5) {
2236 MemberName name = new MemberName (var.identifier,
2239 EventField e = new EventField (
2240 current_class, (Expression) $4, (int) $2, false, name,
2243 e.Initializer = var.expression_or_array_initializer;
2245 current_container.AddEvent (e);
2247 if (RootContext.Documentation != null) {
2248 e.DocComment = Lexer.consume_doc_comment ();
2249 Lexer.doc_state = XmlCommentState.Allowed;
2255 EVENT type namespace_or_type_name
2258 implicit_value_parameter_type = (Expression) $4;
2259 lexer.EventParsing = true;
2261 event_accessor_declarations
2263 lexer.EventParsing = false;
2267 MemberName name = (MemberName) $5;
2270 Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2271 current_container.Name, name.ToString ());
2274 Accessors accessors = (Accessors) $8;
2275 if (accessors.get_or_add == null || accessors.set_or_remove == null)
2276 // CS0073 is already reported, so no CS0065 here.
2279 Event e = new EventProperty (
2280 current_class, (Expression) $4, (int) $2, false, name,
2281 (Attributes) $1, accessors.get_or_add, accessors.set_or_remove);
2282 if (RootContext.Documentation != null) {
2283 e.DocComment = Lexer.consume_doc_comment ();
2284 Lexer.doc_state = XmlCommentState.Allowed;
2287 current_container.AddEvent (e);
2288 implicit_value_parameter_type = null;
2292 | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
2293 MemberName mn = (MemberName) $5;
2295 if (mn.Left != null)
2296 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2298 Report.Error (71, mn.Location, "Event declaration should use property syntax");
2300 if (RootContext.Documentation != null)
2301 Lexer.doc_state = XmlCommentState.Allowed;
2305 event_accessor_declarations
2306 : add_accessor_declaration remove_accessor_declaration
2308 $$ = new Accessors ((Accessor) $1, (Accessor) $2);
2310 | remove_accessor_declaration add_accessor_declaration
2312 Accessors accessors = new Accessors ((Accessor) $2, (Accessor) $1);
2313 accessors.declared_in_reverse = true;
2316 | add_accessor_declaration { $$ = null; }
2317 | remove_accessor_declaration { $$ = null; }
2320 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2326 add_accessor_declaration
2327 : opt_attributes ADD
2329 Parameter [] args = new Parameter [1];
2330 Parameter implicit_value_parameter = new Parameter (
2331 implicit_value_parameter_type, "value",
2332 Parameter.Modifier.NONE, null, (Location) $2);
2334 args [0] = implicit_value_parameter;
2336 current_local_parameters = new Parameters (args);
2337 lexer.EventParsing = false;
2339 anonymous_host = SimpleAnonymousHost.GetSimple ();
2343 Accessor accessor = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2344 lexer.EventParsing = true;
2346 current_local_parameters = null;
2347 SimpleAnonymousHost.Simple.Propagate (accessor);
2348 anonymous_host = null;
2352 | opt_attributes ADD error {
2353 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2356 | opt_attributes modifiers ADD {
2357 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2362 remove_accessor_declaration
2363 : opt_attributes REMOVE
2365 Parameter [] args = new Parameter [1];
2366 Parameter implicit_value_parameter = new Parameter (
2367 implicit_value_parameter_type, "value",
2368 Parameter.Modifier.NONE, null, (Location) $2);
2370 args [0] = implicit_value_parameter;
2372 current_local_parameters = new Parameters (args);
2373 lexer.EventParsing = false;
2377 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2378 lexer.EventParsing = true;
2380 | opt_attributes REMOVE error {
2381 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2384 | opt_attributes modifiers REMOVE {
2385 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2391 : opt_attributes opt_modifiers indexer_declarator
2394 IndexerDeclaration decl = (IndexerDeclaration) $3;
2396 implicit_value_parameter_type = decl.type;
2398 lexer.PropertyParsing = true;
2399 parsing_indexer = true;
2401 indexer_parameters = decl.param_list;
2402 anonymous_host = SimpleAnonymousHost.GetSimple ();
2404 accessor_declarations
2406 lexer.PropertyParsing = false;
2407 has_get = has_set = false;
2408 parsing_indexer = false;
2415 // The signature is computed from the signature of the indexer. Look
2416 // at section 3.6 on the spec
2418 IndexerDeclaration decl = (IndexerDeclaration) $3;
2419 Location loc = decl.location;
2420 Accessors accessors = (Accessors) $6;
2421 Accessor get_block = accessors.get_or_add;
2422 Accessor set_block = accessors.set_or_remove;
2425 if (decl.interface_type != null)
2426 name = new MemberName (decl.interface_type, TypeContainer.DefaultIndexerName, loc);
2428 name = new MemberName (TypeContainer.DefaultIndexerName, loc);
2430 indexer = new Indexer (current_class, decl.type, name,
2431 (int) $2, false, decl.param_list, (Attributes) $1,
2432 get_block, set_block, accessors.declared_in_reverse);
2434 if (RootContext.Documentation != null)
2435 indexer.DocComment = ConsumeStoredComment ();
2437 current_container.AddIndexer (indexer);
2439 current_local_parameters = null;
2440 implicit_value_parameter_type = null;
2441 indexer_parameters = null;
2446 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2448 Parameters pars = (Parameters) $4;
2449 if (pars.HasArglist) {
2450 // "__arglist is not valid in this context"
2451 Report.Error (1669, (Location) $2, "__arglist is not valid in this context");
2452 } else if (pars.Empty){
2453 Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
2455 if (RootContext.Documentation != null) {
2456 tmpComment = Lexer.consume_doc_comment ();
2457 Lexer.doc_state = XmlCommentState.Allowed;
2460 $$ = new IndexerDeclaration ((Expression) $1, null, pars, (Location) $2);
2462 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2464 Parameters pars = (Parameters) $6;
2466 if (pars.HasArglist) {
2467 // "__arglist is not valid in this context"
2468 Report.Error (1669, (Location) $4, "__arglist is not valid in this context");
2469 } else if (pars.Empty){
2470 Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
2473 MemberName name = (MemberName) $2;
2474 $$ = new IndexerDeclaration ((Expression) $1, name, pars, (Location) $4);
2476 if (RootContext.Documentation != null) {
2477 tmpComment = Lexer.consume_doc_comment ();
2478 Lexer.doc_state = XmlCommentState.Allowed;
2488 if (RootContext.Documentation != null)
2489 enumTypeComment = Lexer.consume_doc_comment ();
2494 LocatedToken lt = (LocatedToken) $4;
2495 Location enum_location = lt.Location;
2497 MemberName name = MakeName (new MemberName (lt.Value, enum_location));
2498 Enum e = new Enum (current_namespace, current_class, (Expression) $5, (int) $2,
2499 name, (Attributes) $1);
2501 if (RootContext.Documentation != null)
2502 e.DocComment = enumTypeComment;
2505 EnumMember em = null;
2506 foreach (VariableDeclaration ev in (ArrayList) $7) {
2507 em = new EnumMember (
2508 e, em, ev.identifier, (Expression) ev.expression_or_array_initializer,
2509 ev.OptAttributes, ev.Location);
2511 // if (RootContext.Documentation != null)
2512 em.DocComment = ev.DocComment;
2514 e.AddEnumMember (em);
2517 current_container.AddTypeContainer (e);
2524 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2525 | COLON type { $$ = $2; }
2531 if (RootContext.Documentation != null)
2532 Lexer.doc_state = XmlCommentState.Allowed;
2534 opt_enum_member_declarations
2536 // here will be evaluated after CLOSE_BLACE is consumed.
2537 if (RootContext.Documentation != null)
2538 Lexer.doc_state = XmlCommentState.Allowed;
2546 opt_enum_member_declarations
2547 : /* empty */ { $$ = new ArrayList (4); }
2548 | enum_member_declarations opt_comma { $$ = $1; }
2551 enum_member_declarations
2552 : enum_member_declaration
2554 ArrayList l = new ArrayList (4);
2559 | enum_member_declarations COMMA enum_member_declaration
2561 ArrayList l = (ArrayList) $1;
2569 enum_member_declaration
2570 : opt_attributes IDENTIFIER
2572 VariableDeclaration vd = new VariableDeclaration (
2573 (LocatedToken) $2, null, (Attributes) $1);
2575 if (RootContext.Documentation != null) {
2576 vd.DocComment = Lexer.consume_doc_comment ();
2577 Lexer.doc_state = XmlCommentState.Allowed;
2582 | opt_attributes IDENTIFIER
2584 if (RootContext.Documentation != null) {
2585 tmpComment = Lexer.consume_doc_comment ();
2586 Lexer.doc_state = XmlCommentState.NotAllowed;
2591 VariableDeclaration vd = new VariableDeclaration (
2592 (LocatedToken) $2, $5, (Attributes) $1);
2594 if (RootContext.Documentation != null)
2595 vd.DocComment = ConsumeStoredComment ();
2601 delegate_declaration
2604 DELEGATE type member_name
2605 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2608 MemberName name = MakeName ((MemberName) $5);
2609 Parameters p = (Parameters) $7;
2611 // TODO: wrong location
2612 Report.Error (1669, name.Location, "__arglist is not valid in this context");
2615 Delegate del = new Delegate (current_namespace, current_class, (Expression) $4,
2616 (int) $2, name, p, (Attributes) $1);
2618 if (RootContext.Documentation != null) {
2619 del.DocComment = Lexer.consume_doc_comment ();
2620 Lexer.doc_state = XmlCommentState.Allowed;
2623 current_container.AddDelegate (del);
2628 namespace_or_type_name
2630 | IDENTIFIER DOUBLE_COLON IDENTIFIER {
2631 LocatedToken lt1 = (LocatedToken) $1;
2632 LocatedToken lt2 = (LocatedToken) $3;
2633 $$ = new MemberName (lt1.Value, lt2.Value, lt2.Location);
2635 | namespace_or_type_name DOT IDENTIFIER {
2636 LocatedToken lt = (LocatedToken) $3;
2637 $$ = new MemberName ((MemberName) $1, lt.Value);
2643 LocatedToken lt = (LocatedToken) $1;
2644 $$ = new MemberName (lt.Value, lt.Location);
2649 * Before you think of adding a return_type, notice that we have been
2650 * using two rules in the places where it matters (one rule using type
2651 * and another identical one that uses VOID as the return type). This
2652 * gets rid of a shift/reduce couple
2655 : namespace_or_type_name
2657 MemberName name = (MemberName) $1;
2658 $$ = name.GetTypeExpression ();
2670 // Note that here only unmanaged types are allowed but we
2671 // can't perform checks during this phase - we do it during
2672 // semantic analysis.
2674 $$ = new ComposedCast ((Expression) $1, "*", Lexer.Location);
2678 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2684 | non_expression_type rank_specifier
2686 Location loc = GetLocation ($1);
2688 loc = lexer.Location;
2689 $$ = new ComposedCast ((Expression) $1, (string) $2, loc);
2691 | non_expression_type STAR
2693 Location loc = GetLocation ($1);
2695 loc = lexer.Location;
2696 $$ = new ComposedCast ((Expression) $1, "*", loc);
2698 | expression rank_specifiers
2700 $$ = new ComposedCast ((Expression) $1, (string) $2);
2704 $$ = new ComposedCast ((Expression) $1, "*");
2708 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2709 // through this different path
2711 | multiplicative_expression STAR
2713 $$ = new ComposedCast ((Expression) $1, "*");
2720 ArrayList types = new ArrayList (4);
2725 | type_list COMMA type
2727 ArrayList types = (ArrayList) $1;
2735 * replaces all the productions for isolating the various
2736 * simple types, but we need this to reuse it easily in local_variable_type
2739 : OBJECT { $$ = TypeManager.system_object_expr; }
2740 | STRING { $$ = TypeManager.system_string_expr; }
2741 | BOOL { $$ = TypeManager.system_boolean_expr; }
2742 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2743 | FLOAT { $$ = TypeManager.system_single_expr; }
2744 | DOUBLE { $$ = TypeManager.system_double_expr; }
2749 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2750 | BYTE { $$ = TypeManager.system_byte_expr; }
2751 | SHORT { $$ = TypeManager.system_int16_expr; }
2752 | USHORT { $$ = TypeManager.system_uint16_expr; }
2753 | INT { $$ = TypeManager.system_int32_expr; }
2754 | UINT { $$ = TypeManager.system_uint32_expr; }
2755 | LONG { $$ = TypeManager.system_int64_expr; }
2756 | ULONG { $$ = TypeManager.system_uint64_expr; }
2757 | CHAR { $$ = TypeManager.system_char_expr; }
2758 | VOID { $$ = TypeManager.system_void_expr; }
2762 : type rank_specifiers
2764 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2);
2769 // Expressions, section 7.5
2778 MemberName mn = (MemberName) $1;
2779 $$ = mn.GetTypeExpression ();
2781 | IDENTIFIER DOUBLE_COLON IDENTIFIER
2783 LocatedToken lt1 = (LocatedToken) $1;
2784 LocatedToken lt2 = (LocatedToken) $3;
2785 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, lt2.Location);
2787 | parenthesized_expression
2789 | invocation_expression
2793 | post_increment_expression
2794 | post_decrement_expression
2798 | checked_expression
2799 | unchecked_expression
2800 | pointer_member_access
2801 | anonymous_method_expression
2808 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
2809 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); }
2810 | NULL { $$ = new NullLiteral (lexer.Location); }
2814 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
2815 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
2816 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
2821 object v = lexer.Value;
2824 $$ = new IntLiteral ((int) v, lexer.Location);
2825 } else if (v is uint)
2826 $$ = new UIntLiteral ((UInt32) v, lexer.Location);
2828 $$ = new LongLiteral ((Int64) v, lexer.Location);
2829 else if (v is ulong)
2830 $$ = new ULongLiteral ((UInt64) v, lexer.Location);
2832 Console.WriteLine ("OOPS. Unexpected result from scanner");
2837 : TRUE { $$ = new BoolLiteral (true, lexer.Location); }
2838 | FALSE { $$ = new BoolLiteral (false, lexer.Location); }
2841 parenthesized_expression_0
2842 : OPEN_PARENS expression CLOSE_PARENS
2845 lexer.Deambiguate_CloseParens ($$);
2846 // After this, the next token returned is one of
2847 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST (CLOSE_PARENS), CLOSE_PARENS_OPEN_PARENS
2848 // or CLOSE_PARENS_MINUS.
2850 | OPEN_PARENS expression error { CheckToken (1026, yyToken, "Expecting ')'", lexer.Location); }
2853 parenthesized_expression
2854 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2858 | parenthesized_expression_0 CLOSE_PARENS
2862 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2864 // If a parenthesized expression is followed by a minus, we need to wrap
2865 // the expression inside a ParenthesizedExpression for the CS0075 check
2866 // in Binary.DoResolve().
2867 $$ = new ParenthesizedExpression ((Expression) $1);
2872 : primary_expression DOT IDENTIFIER
2874 LocatedToken lt = (LocatedToken) $3;
2875 $$ = new MemberAccess ((Expression) $1, lt.Value);
2877 | predefined_type DOT IDENTIFIER
2879 LocatedToken lt = (LocatedToken) $3;
2880 // TODO: Location is wrong as some predefined types doesn't hold a location
2881 $$ = new MemberAccess ((Expression) $1, lt.Value, lt.Location);
2889 invocation_expression
2890 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2893 Report.Error (1, (Location) $2, "Parse error");
2895 $$ = new Invocation ((Expression) $1, (ArrayList) $3);
2897 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2899 $$ = new Invocation ((Expression) $1, new ArrayList ());
2901 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2903 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
2905 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2907 ArrayList args = new ArrayList (1);
2909 $$ = new Invocation ((Expression) $1, args);
2911 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2913 ArrayList args = ((ArrayList) $4);
2915 $$ = new Invocation ((Expression) $1, args);
2920 : /* empty */ { $$ = null; }
2927 ArrayList list = new ArrayList (4);
2931 | argument_list COMMA argument
2933 ArrayList list = (ArrayList) $1;
2937 | argument_list error {
2938 CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
2946 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2948 | non_simple_argument
2955 : REF variable_reference
2957 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2959 | OUT variable_reference
2961 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2963 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2965 ArrayList list = (ArrayList) $3;
2966 Argument[] args = new Argument [list.Count];
2967 list.CopyTo (args, 0);
2969 Expression expr = new Arglist (args, (Location) $1);
2970 $$ = new Argument (expr, Argument.AType.Expression);
2972 | ARGLIST OPEN_PARENS CLOSE_PARENS
2974 $$ = new Argument (new Arglist ((Location) $1), Argument.AType.Expression);
2978 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
2983 : expression { note ("section 5.4"); $$ = $1; }
2987 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2989 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
2991 | primary_expression rank_specifiers
2993 // So the super-trick is that primary_expression
2994 // can only be either a SimpleName or a MemberAccess.
2995 // The MemberAccess case arises when you have a fully qualified type-name like :
2997 // SimpleName is when you have
3000 Expression expr = (Expression) $1;
3001 if (expr is ComposedCast){
3002 $$ = new ComposedCast (expr, (string) $2);
3003 } else if (!(expr is SimpleName || expr is MemberAccess || expr is QualifiedAliasMember)){
3004 Error_ExpectingTypeName (expr);
3005 $$ = TypeManager.system_object_expr;
3008 // So we extract the string corresponding to the SimpleName
3011 $$ = new ComposedCast (expr, (string) $2);
3013 current_array_type = (Expression)$$;
3020 ArrayList list = new ArrayList (4);
3024 | expression_list COMMA expression
3026 ArrayList list = (ArrayList) $1;
3035 $$ = new This (current_block, (Location) $1);
3040 : BASE DOT IDENTIFIER
3042 LocatedToken lt = (LocatedToken) $3;
3043 $$ = new BaseAccess (lt.Value, lt.Location);
3045 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
3047 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
3050 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
3055 post_increment_expression
3056 : primary_expression OP_INC
3058 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
3059 (Expression) $1, (Location) $2);
3063 post_decrement_expression
3064 : primary_expression OP_DEC
3066 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
3067 (Expression) $1, (Location) $2);
3072 : object_or_delegate_creation_expression
3073 | array_creation_expression
3076 object_or_delegate_creation_expression
3077 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
3079 $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
3083 array_creation_expression
3084 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
3086 opt_array_initializer
3088 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
3090 | NEW type rank_specifiers array_initializer
3092 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, (Location) $1);
3096 Report.Error (1031, (Location) $1, "Type expected");
3101 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
3118 : rank_specifier opt_rank_specifier
3120 $$ = (string) $2 + (string) $1;
3125 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3127 $$ = "[" + (string) $2 + "]";
3147 | dim_separators COMMA
3149 $$ = (string) $1 + ",";
3153 opt_array_initializer
3165 : OPEN_BRACE CLOSE_BRACE
3167 ArrayList list = new ArrayList (4);
3170 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3172 $$ = (ArrayList) $2;
3176 variable_initializer_list
3177 : variable_initializer
3179 ArrayList list = new ArrayList (4);
3183 | variable_initializer_list COMMA variable_initializer
3185 ArrayList list = (ArrayList) $1;
3194 pushed_current_array_type = current_array_type;
3196 OPEN_PARENS type CLOSE_PARENS
3198 Expression type = (Expression)$4;
3199 if (type == TypeManager.system_void_expr)
3200 $$ = new TypeOfVoid ((Location) $1);
3202 $$ = new TypeOf (type, (Location) $1);
3203 current_array_type = pushed_current_array_type;
3208 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3209 $$ = new SizeOf ((Expression) $3, (Location) $1);
3214 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3216 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3220 unchecked_expression
3221 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3223 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3227 pointer_member_access
3228 : primary_expression OP_PTR IDENTIFIER
3231 LocatedToken lt = (LocatedToken) $3;
3233 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
3234 $$ = new MemberAccess (deref, lt.Value);
3238 anonymous_method_expression
3239 : DELEGATE opt_anonymous_method_signature
3241 start_anonymous (false, (Parameters) $2, (Location) $1);
3245 $$ = end_anonymous ((ToplevelBlock) $4, (Location) $1);
3249 opt_anonymous_method_signature
3250 : /* empty */ { $$ = null; }
3251 | anonymous_method_signature
3254 anonymous_method_signature
3255 : open_parens opt_anonymous_method_parameter_list CLOSE_PARENS
3258 $$ = Parameters.EmptyReadOnlyParameters;
3260 ArrayList par_list = (ArrayList) $2;
3261 Parameter [] pars = new Parameter [par_list.Count];
3262 par_list.CopyTo (pars);
3263 $$ = new Parameters (pars);
3268 opt_anonymous_method_parameter_list
3269 : /* empty */ { $$ = null; }
3270 | anonymous_method_parameter_list { $$ = $1; }
3273 anonymous_method_parameter_list
3274 : anonymous_method_parameter
3276 ArrayList a = new ArrayList (4);
3280 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3282 ArrayList a = (ArrayList) $1;
3288 anonymous_method_parameter
3289 : opt_parameter_modifier type IDENTIFIER {
3290 LocatedToken lt = (LocatedToken) $3;
3291 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3293 | PARAMS type IDENTIFIER {
3294 Report.Error (1670, ((LocatedToken) $3).Location, "The `params' modifier is not allowed in anonymous method declaration");
3300 : primary_expression
3301 | BANG prefixed_unary_expression
3303 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3305 | TILDE prefixed_unary_expression
3307 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3313 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3315 $$ = new Cast ((Expression) $1, (Expression) $3);
3317 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3319 $$ = new Cast ((Expression) $1, (Expression) $3);
3325 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3327 // TODO: wrong location
3328 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3333 // The idea to split this out is from Rhys' grammar
3334 // to solve the problem with casts.
3336 prefixed_unary_expression
3338 | PLUS prefixed_unary_expression
3340 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3342 | MINUS prefixed_unary_expression
3344 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3346 | OP_INC prefixed_unary_expression
3348 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3349 (Expression) $2, (Location) $1);
3351 | OP_DEC prefixed_unary_expression
3353 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3354 (Expression) $2, (Location) $1);
3356 | STAR prefixed_unary_expression
3358 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, (Location) $1);
3360 | BITWISE_AND prefixed_unary_expression
3362 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3366 pre_increment_expression
3367 : OP_INC prefixed_unary_expression
3369 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3370 (Expression) $2, (Location) $1);
3374 pre_decrement_expression
3375 : OP_DEC prefixed_unary_expression
3377 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3378 (Expression) $2, (Location) $1);
3382 multiplicative_expression
3383 : prefixed_unary_expression
3384 | multiplicative_expression STAR prefixed_unary_expression
3386 $$ = new Binary (Binary.Operator.Multiply,
3387 (Expression) $1, (Expression) $3);
3389 | multiplicative_expression DIV prefixed_unary_expression
3391 $$ = new Binary (Binary.Operator.Division,
3392 (Expression) $1, (Expression) $3);
3394 | multiplicative_expression PERCENT prefixed_unary_expression
3396 $$ = new Binary (Binary.Operator.Modulus,
3397 (Expression) $1, (Expression) $3);
3402 : multiplicative_expression
3403 | additive_expression PLUS multiplicative_expression
3405 $$ = new Binary (Binary.Operator.Addition,
3406 (Expression) $1, (Expression) $3);
3408 | additive_expression MINUS multiplicative_expression
3410 $$ = new Binary (Binary.Operator.Subtraction,
3411 (Expression) $1, (Expression) $3);
3416 : additive_expression
3417 | shift_expression OP_SHIFT_LEFT additive_expression
3419 $$ = new Binary (Binary.Operator.LeftShift,
3420 (Expression) $1, (Expression) $3);
3422 | shift_expression OP_SHIFT_RIGHT additive_expression
3424 $$ = new Binary (Binary.Operator.RightShift,
3425 (Expression) $1, (Expression) $3);
3429 relational_expression
3431 | relational_expression OP_LT shift_expression
3433 $$ = new Binary (Binary.Operator.LessThan,
3434 (Expression) $1, (Expression) $3);
3436 | relational_expression OP_GT shift_expression
3438 $$ = new Binary (Binary.Operator.GreaterThan,
3439 (Expression) $1, (Expression) $3);
3441 | relational_expression OP_LE shift_expression
3443 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3444 (Expression) $1, (Expression) $3);
3446 | relational_expression OP_GE shift_expression
3448 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3449 (Expression) $1, (Expression) $3);
3451 | relational_expression IS type
3453 $$ = new Is ((Expression) $1, (Expression) $3, (Location) $2);
3455 | relational_expression AS type
3457 $$ = new As ((Expression) $1, (Expression) $3, (Location) $2);
3462 : relational_expression
3463 | equality_expression OP_EQ relational_expression
3465 $$ = new Binary (Binary.Operator.Equality,
3466 (Expression) $1, (Expression) $3);
3468 | equality_expression OP_NE relational_expression
3470 $$ = new Binary (Binary.Operator.Inequality,
3471 (Expression) $1, (Expression) $3);
3476 : equality_expression
3477 | and_expression BITWISE_AND equality_expression
3479 $$ = new Binary (Binary.Operator.BitwiseAnd,
3480 (Expression) $1, (Expression) $3);
3484 exclusive_or_expression
3486 | exclusive_or_expression CARRET and_expression
3488 $$ = new Binary (Binary.Operator.ExclusiveOr,
3489 (Expression) $1, (Expression) $3);
3493 inclusive_or_expression
3494 : exclusive_or_expression
3495 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3497 $$ = new Binary (Binary.Operator.BitwiseOr,
3498 (Expression) $1, (Expression) $3);
3502 conditional_and_expression
3503 : inclusive_or_expression
3504 | conditional_and_expression OP_AND inclusive_or_expression
3506 $$ = new Binary (Binary.Operator.LogicalAnd,
3507 (Expression) $1, (Expression) $3);
3511 conditional_or_expression
3512 : conditional_and_expression
3513 | conditional_or_expression OP_OR conditional_and_expression
3515 $$ = new Binary (Binary.Operator.LogicalOr,
3516 (Expression) $1, (Expression) $3);
3520 conditional_expression
3521 : conditional_or_expression
3522 | conditional_or_expression INTERR expression COLON expression
3524 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
3528 assignment_expression
3529 : prefixed_unary_expression ASSIGN expression
3531 $$ = new Assign ((Expression) $1, (Expression) $3);
3533 | prefixed_unary_expression OP_MULT_ASSIGN expression
3535 $$ = new CompoundAssign (
3536 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
3538 | prefixed_unary_expression OP_DIV_ASSIGN expression
3540 $$ = new CompoundAssign (
3541 Binary.Operator.Division, (Expression) $1, (Expression) $3);
3543 | prefixed_unary_expression OP_MOD_ASSIGN expression
3545 $$ = new CompoundAssign (
3546 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
3548 | prefixed_unary_expression OP_ADD_ASSIGN expression
3550 $$ = new CompoundAssign (
3551 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
3553 | prefixed_unary_expression OP_SUB_ASSIGN expression
3555 $$ = new CompoundAssign (
3556 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3558 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3560 $$ = new CompoundAssign (
3561 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
3563 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3565 $$ = new CompoundAssign (
3566 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
3568 | prefixed_unary_expression OP_AND_ASSIGN expression
3570 $$ = new CompoundAssign (
3571 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
3573 | prefixed_unary_expression OP_OR_ASSIGN expression
3575 $$ = new CompoundAssign (
3576 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
3578 | prefixed_unary_expression OP_XOR_ASSIGN expression
3580 $$ = new CompoundAssign (
3581 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
3585 implicitly_typed_lambda_parameter_list
3587 LocatedToken lt = (LocatedToken) $1;
3588 ArrayList a = new ArrayList (4);
3590 a.Add (new Parameter ((Expression)null, lt.Value, Parameter.Modifier.NONE, null, lt.Location));
3593 | implicitly_typed_lambda_parameter_list COMMA IDENTIFIER {
3594 LocatedToken lt = (LocatedToken) $3;
3595 ArrayList a = (ArrayList) $1;
3596 a.Add (new Parameter ((Expression)null, lt.Value, Parameter.Modifier.NONE, null, lt.Location));
3601 explicitly_typed_lambda_parameter_list
3602 : explicitly_typed_lambda_parameter
3604 ArrayList pars = new ArrayList (4);
3609 | explicitly_typed_lambda_parameter_list COMMA explicitly_typed_lambda_parameter
3611 ArrayList pars = (ArrayList) $1;
3618 explicitly_typed_lambda_parameter
3619 : parameter_modifier type IDENTIFIER
3621 LocatedToken lt = (LocatedToken) $3;
3623 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3627 LocatedToken lt = (LocatedToken) $2;
3629 $$ = new Parameter ((Expression) $1, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
3633 lambda_parameter_list
3634 : implicitly_typed_lambda_parameter_list { $$ = $1; }
3635 | explicitly_typed_lambda_parameter_list { $$ = $1; }
3638 opt_lambda_parameter_list
3639 : /* empty */ { $$ = null; }
3640 | lambda_parameter_list {
3641 ArrayList pars_list = (ArrayList) $1;
3643 Parameter [] pars = new Parameter [pars_list.Count];
3644 pars_list.CopyTo (pars);
3645 $$ = new Parameters (pars);
3649 lambda_expression_body
3651 start_block (lexer.Location);
3655 Block b = end_block (lexer.Location);
3656 b.AddStatement (new ContextualReturn ((Expression) $2));
3667 Parameter [] pars = new Parameter [1];
3668 LocatedToken lt = (LocatedToken) $1;
3669 pars [0] = new Parameter ((Expression)null, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
3671 Parameters parameters = new Parameters (pars);
3673 start_anonymous (true, parameters, (Location) $2);
3675 lambda_expression_body
3677 $$ = end_anonymous ((ToplevelBlock) $4, (Location) $2);
3679 | OPEN_PARENS_LAMBDA opt_lambda_parameter_list CLOSE_PARENS ARROW
3681 start_anonymous (true, (Parameters) $2, (Location) $4);
3683 lambda_expression_body
3685 $$ = end_anonymous ((ToplevelBlock) $6, (Location) $4);
3690 : conditional_expression
3691 | assignment_expression
3712 MemberName name = MakeName ((MemberName) $5);
3713 int mod_flags = (int) $2;
3715 push_current_class (new Class (
3716 current_namespace, current_class, name,
3717 mod_flags, (Attributes) $1), $3);
3722 if (current_class.Name == "System.Object") {
3723 Report.Error (537, current_class.Location,
3724 "The class System.Object cannot have a base " +
3725 "class or implement an interface.");
3727 current_container.AddBasesForPart (current_class, (ArrayList) $7);
3730 if (RootContext.Documentation != null) {
3731 current_container.DocComment = Lexer.consume_doc_comment ();
3732 Lexer.doc_state = XmlCommentState.Allowed;
3737 if (RootContext.Documentation != null)
3738 Lexer.doc_state = XmlCommentState.Allowed;
3742 $$ = pop_current_class ();
3750 { $$ = $1; } // location
3754 : /* empty */ { $$ = (int) 0; }
3760 | modifiers modifier
3765 if ((m1 & m2) != 0) {
3766 Location l = lexer.Location;
3767 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
3769 $$ = (int) (m1 | m2);
3774 : NEW { $$ = Modifiers.NEW; }
3775 | PUBLIC { $$ = Modifiers.PUBLIC; }
3776 | PROTECTED { $$ = Modifiers.PROTECTED; }
3777 | INTERNAL { $$ = Modifiers.INTERNAL; }
3778 | PRIVATE { $$ = Modifiers.PRIVATE; }
3779 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3780 | SEALED { $$ = Modifiers.SEALED; }
3781 | STATIC { $$ = Modifiers.STATIC; }
3782 | READONLY { $$ = Modifiers.READONLY; }
3783 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3784 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3785 | EXTERN { $$ = Modifiers.EXTERN; }
3786 | VOLATILE { $$ = Modifiers.VOLATILE; }
3787 | UNSAFE { $$ = Modifiers.UNSAFE; }
3791 : /* empty */ { $$ = null; }
3792 | class_base { $$ = $1; }
3796 : COLON type_list { $$ = $2; }
3804 // A block is "contained" on the following places:
3806 // property_declaration as part of the accessor body (get/set)
3807 // operator_declaration
3808 // constructor_declaration
3809 // destructor_declaration
3810 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3815 start_block ((Location) $1);
3817 opt_statement_list CLOSE_BRACE
3819 $$ = end_block ((Location) $4);
3825 opt_statement_list CLOSE_BRACE
3827 $$ = end_block ((Location) $3);
3838 | statement_list statement
3842 : declaration_statement
3844 if ($1 != null && (Block) $1 != current_block){
3845 current_block.AddStatement ((Statement) $1);
3846 current_block = (Block) $1;
3849 | valid_declaration_statement
3851 current_block.AddStatement ((Statement) $1);
3856 valid_declaration_statement
3859 | expression_statement
3860 | selection_statement
3861 | iteration_statement
3865 | unchecked_statement
3873 : valid_declaration_statement
3874 | declaration_statement
3876 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3881 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3889 $$ = EmptyStatement.Value;
3896 LocatedToken lt = (LocatedToken) $1;
3897 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
3899 if (current_block.AddLabel (labeled))
3900 current_block.AddStatement (labeled);
3905 declaration_statement
3906 : local_variable_declaration SEMICOLON
3908 current_array_type = null;
3910 DictionaryEntry de = (DictionaryEntry) $1;
3911 Expression e = (Expression) de.Key;
3913 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
3917 | local_constant_declaration SEMICOLON
3919 current_array_type = null;
3921 DictionaryEntry de = (DictionaryEntry) $1;
3923 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3929 * The following is from Rhys' grammar:
3930 * > Types in local variable declarations must be recognized as
3931 * > expressions to prevent reduce/reduce errors in the grammar.
3932 * > The expressions are converted into types during semantic analysis.
3935 : primary_expression opt_rank_specifier
3937 // FIXME: Do something smart here regarding the composition of the type.
3939 // Ok, the above "primary_expression" is there to get rid of
3940 // both reduce/reduce and shift/reduces in the grammar, it should
3941 // really just be "type_name". If you use type_name, a reduce/reduce
3942 // creeps up. If you use namespace_or_type_name (which is all we need
3943 // really) two shift/reduces appear.
3946 // So the super-trick is that primary_expression
3947 // can only be either a SimpleName or a MemberAccess.
3948 // The MemberAccess case arises when you have a fully qualified type-name like :
3950 // SimpleName is when you have
3953 Expression expr = (Expression) $1;
3954 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3955 Error_ExpectingTypeName (expr);
3959 // So we extract the string corresponding to the SimpleName
3963 if ((string) $2 == "")
3966 $$ = new ComposedCast ((Expression) $1, (string) $2);
3969 | builtin_types opt_rank_specifier
3971 if ((string) $2 == "")
3974 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3978 local_variable_pointer_type
3979 : primary_expression STAR
3981 Expression expr = (Expression) $1;
3983 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3984 Error_ExpectingTypeName (expr);
3988 $$ = new ComposedCast ((Expression) $1, "*");
3990 | builtin_types STAR
3992 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3996 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
3998 | local_variable_pointer_type STAR
4000 $$ = new ComposedCast ((Expression) $1, "*");
4004 local_variable_declaration
4005 : local_variable_type variable_declarators
4008 $$ = new DictionaryEntry ($1, $2);
4012 | local_variable_pointer_type opt_rank_specifier variable_declarators
4017 if ((string) $2 == "")
4018 t = (Expression) $1;
4020 t = new ComposedCast ((Expression) $1, (string) $2);
4021 $$ = new DictionaryEntry (t, $3);
4027 local_constant_declaration
4028 : CONST local_variable_type constant_declarators
4031 $$ = new DictionaryEntry ($2, $3);
4037 expression_statement
4038 : statement_expression SEMICOLON { $$ = $1; }
4042 // We have to do the wrapping here and not in the case above,
4043 // because statement_expression is used for example in for_statement
4045 statement_expression
4048 Expression expr = (Expression) $1;
4049 ExpressionStatement s = expr as ExpressionStatement;
4051 expr.Error_InvalidExpressionStatement ();
4054 $$ = new StatementExpression (s);
4058 Report.Error (1002, GetLocation ($1), "Expecting `;'");
4063 object_creation_expression
4064 : object_or_delegate_creation_expression
4065 { note ("complain if this is a delegate maybe?"); }
4074 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
4077 Location l = (Location) $1;
4079 $$ = new If ((Expression) $3, (Statement) $5, l);
4081 // FIXME: location for warning should be loc property of $5.
4082 if ($5 == EmptyStatement.Value)
4083 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4086 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
4087 embedded_statement ELSE embedded_statement
4089 Location l = (Location) $1;
4091 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
4093 // FIXME: location for warning should be loc property of $5 and $7.
4094 if ($5 == EmptyStatement.Value)
4095 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4096 if ($7 == EmptyStatement.Value)
4097 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4102 : SWITCH OPEN_PARENS
4104 if (switch_stack == null)
4105 switch_stack = new Stack (2);
4106 switch_stack.Push (current_block);
4108 expression CLOSE_PARENS
4111 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
4112 current_block = (Block) switch_stack.Pop ();
4128 Report.Warning (1522, 1, lexer.Location, "Empty switch block");
4129 $$ = new ArrayList ();
4137 ArrayList sections = new ArrayList (4);
4142 | switch_sections switch_section
4144 ArrayList sections = (ArrayList) $1;
4154 current_block = current_block.CreateSwitchBlock (lexer.Location);
4158 Block topmost = current_block;
4160 while (topmost.Implicit)
4161 topmost = topmost.Parent;
4162 $$ = new SwitchSection ((ArrayList) $1, topmost);
4169 ArrayList labels = new ArrayList (4);
4174 | switch_labels switch_label
4176 ArrayList labels = (ArrayList) ($1);
4184 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
4185 | DEFAULT COLON { $$ = new SwitchLabel (null, (Location) $1); }
4188 1523, GetLocation ($1),
4189 "The keyword case or default must precede code in switch block");
4201 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
4203 Location l = (Location) $1;
4204 $$ = new While ((Expression) $3, (Statement) $5, l);
4209 : DO embedded_statement
4210 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
4212 Location l = (Location) $1;
4214 $$ = new Do ((Statement) $2, (Expression) $5, l);
4220 opt_for_initializer SEMICOLON
4222 Block assign_block = new Block (current_block);
4223 current_block = assign_block;
4225 if ($3 is DictionaryEntry){
4226 DictionaryEntry de = (DictionaryEntry) $3;
4228 Expression type = (Expression) de.Key;
4229 ArrayList var_declarators = (ArrayList) de.Value;
4231 foreach (VariableDeclaration decl in var_declarators){
4235 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4239 Location l = lexer.Location;
4240 Expression expr = decl.expression_or_array_initializer;
4242 LocalVariableReference var;
4243 var = new LocalVariableReference (assign_block, decl.identifier, l);
4246 Assign a = new Assign (var, expr, decl.Location);
4248 assign_block.AddStatement (new StatementExpression (a));
4252 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4253 // This can be referred to as $5 below.
4259 opt_for_condition SEMICOLON
4260 opt_for_iterator CLOSE_PARENS
4263 Location l = (Location) $1;
4265 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4267 current_block.AddStatement (f);
4268 while (current_block.Implicit)
4269 current_block = current_block.Parent;
4271 current_block = current_block.Parent;
4276 : /* empty */ { $$ = EmptyStatement.Value; }
4281 : local_variable_declaration
4282 | statement_expression_list
4286 : /* empty */ { $$ = null; }
4287 | boolean_expression
4291 : /* empty */ { $$ = EmptyStatement.Value; }
4296 : statement_expression_list
4299 statement_expression_list
4300 : statement_expression
4302 // CHANGE: was `null'
4303 Statement s = (Statement) $1;
4304 Block b = new Block (current_block, Block.Flags.Implicit, s.loc, lexer.Location);
4309 | statement_expression_list COMMA statement_expression
4311 Block b = (Block) $1;
4313 b.AddStatement ((Statement) $3);
4319 : FOREACH open_parens type IN expression CLOSE_PARENS
4321 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4324 | FOREACH open_parens type IDENTIFIER IN
4325 expression CLOSE_PARENS
4327 Block foreach_block = new Block (current_block);
4328 current_block = foreach_block;
4330 LocatedToken lt = (LocatedToken) $4;
4331 Location l = lt.Location;
4334 vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4336 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4338 // Get a writable reference to this read-only variable.
4340 // Note that the $$ here refers to the value of _this_ code block,
4341 // not the value of the LHS non-terminal. This can be referred to as $8 below.
4342 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
4349 LocalVariableReference v = (LocalVariableReference) $8;
4350 Location l = (Location) $1;
4353 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
4354 current_block.AddStatement (f);
4357 while (current_block.Implicit)
4358 current_block = current_block.Parent;
4360 current_block = current_block.Parent;
4366 | continue_statement
4376 $$ = new Break ((Location) $1);
4381 : CONTINUE SEMICOLON
4383 $$ = new Continue ((Location) $1);
4388 : GOTO IDENTIFIER SEMICOLON
4390 LocatedToken lt = (LocatedToken) $2;
4391 $$ = new Goto (lt.Value, lt.Location);
4393 | GOTO CASE constant_expression SEMICOLON
4395 $$ = new GotoCase ((Expression) $3, (Location) $1);
4397 | GOTO DEFAULT SEMICOLON
4399 $$ = new GotoDefault ((Location) $1);
4404 : RETURN opt_expression SEMICOLON
4406 $$ = new Return ((Expression) $2, (Location) $1);
4411 : THROW opt_expression SEMICOLON
4413 $$ = new Throw ((Expression) $2, (Location) $1);
4418 : IDENTIFIER RETURN expression SEMICOLON
4420 LocatedToken lt = (LocatedToken) $1;
4421 string s = lt.Value;
4423 Report.Error (1003, lt.Location, "; expected");
4426 if (RootContext.Version == LanguageVersion.ISO_1){
4427 Report.FeatureIsNotISO1 (lt.Location, "yield statement");
4430 if (anonymous_host == null){
4431 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4434 anonymous_host.SetYields ();
4435 $$ = new Yield ((Expression) $3, lt.Location);
4438 | IDENTIFIER RETURN SEMICOLON
4440 Report.Error (1627, (Location) $2, "Expression expected after yield return");
4443 | IDENTIFIER BREAK SEMICOLON
4445 LocatedToken lt = (LocatedToken) $1;
4446 string s = lt.Value;
4448 Report.Error (1003, lt.Location, "; expected");
4451 if (RootContext.Version == LanguageVersion.ISO_1){
4452 Report.FeatureIsNotISO1 (lt.Location, "yield statement");
4455 if (anonymous_host == null){
4456 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4459 anonymous_host.SetYields ();
4460 $$ = new YieldBreak (lt.Location);
4471 : TRY block catch_clauses
4475 ArrayList c = (ArrayList)$3;
4476 for (int i = 0; i < c.Count; ++i) {
4477 Catch cc = (Catch) c [i];
4479 if (i != c.Count - 1)
4480 Report.Error (1017, cc.loc, "Try statement already has an empty catch block");
4487 // Now s contains the list of specific catch clauses
4488 // and g contains the general one.
4490 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4492 | TRY block opt_catch_clauses FINALLY block
4495 ArrayList s = new ArrayList (4);
4496 ArrayList catch_list = (ArrayList) $3;
4498 if (catch_list != null){
4499 foreach (Catch cc in catch_list) {
4507 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4511 Report.Error (1524, (Location) $1, "Expected catch or finally");
4517 : /* empty */ { $$ = null; }
4524 ArrayList l = new ArrayList (4);
4529 | catch_clauses catch_clause
4531 ArrayList l = (ArrayList) $1;
4539 : /* empty */ { $$ = null; }
4544 : CATCH opt_catch_args
4546 Expression type = null;
4549 DictionaryEntry cc = (DictionaryEntry) $2;
4550 type = (Expression) cc.Key;
4551 LocatedToken lt = (LocatedToken) cc.Value;
4554 ArrayList one = new ArrayList (4);
4556 one.Add (new VariableDeclaration (lt, null));
4558 current_block = new Block (current_block);
4559 Block b = declare_local_variables (type, one, lt.Location);
4564 Expression type = null;
4566 Block var_block = null;
4569 DictionaryEntry cc = (DictionaryEntry) $2;
4570 type = (Expression) cc.Key;
4571 LocatedToken lt = (LocatedToken) cc.Value;
4575 while (current_block.Implicit)
4576 current_block = current_block.Parent;
4577 var_block = current_block;
4578 current_block = current_block.Parent;
4582 $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
4587 : /* empty */ { $$ = null; }
4592 : open_parens type opt_identifier CLOSE_PARENS
4594 $$ = new DictionaryEntry ($2, $3);
4602 $$ = new Checked ((Block) $2);
4609 $$ = new Unchecked ((Block) $2);
4616 RootContext.CheckUnsafeOption ((Location) $1);
4618 $$ = new Unsafe ((Block) $3);
4624 type fixed_pointer_declarators
4627 ArrayList list = (ArrayList) $4;
4628 Expression type = (Expression) $3;
4629 Location l = (Location) $1;
4630 int top = list.Count;
4632 Block assign_block = new Block (current_block);
4633 current_block = assign_block;
4635 for (int i = 0; i < top; i++){
4636 Pair p = (Pair) list [i];
4639 v = current_block.AddVariable (type, (string) p.First, l);
4643 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4651 Location l = (Location) $1;
4653 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4655 current_block.AddStatement (f);
4656 while (current_block.Implicit)
4657 current_block = current_block.Parent;
4659 current_block = current_block.Parent;
4663 fixed_pointer_declarators
4664 : fixed_pointer_declarator {
4665 ArrayList declarators = new ArrayList (4);
4667 declarators.Add ($1);
4670 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4672 ArrayList declarators = (ArrayList) $1;
4674 declarators.Add ($3);
4679 fixed_pointer_declarator
4680 : IDENTIFIER ASSIGN expression
4682 LocatedToken lt = (LocatedToken) $1;
4683 // FIXME: keep location
4684 $$ = new Pair (lt.Value, $3);
4688 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
4694 : LOCK OPEN_PARENS expression CLOSE_PARENS
4700 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
4705 : USING open_parens resource_acquisition CLOSE_PARENS
4707 Block assign_block = new Block (current_block);
4708 current_block = assign_block;
4710 if ($3 is DictionaryEntry){
4711 DictionaryEntry de = (DictionaryEntry) $3;
4712 Location l = (Location) $1;
4714 Expression type = (Expression) de.Key;
4715 ArrayList var_declarators = (ArrayList) de.Value;
4717 ArrayList vars = new ArrayList (4);
4719 foreach (VariableDeclaration decl in var_declarators){
4721 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4724 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
4726 Expression expr = decl.expression_or_array_initializer;
4728 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4731 LocalVariableReference var;
4733 // Get a writable reference to this read-only variable.
4734 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4736 // This is so that it is not a warning on using variables
4739 vars.Add (new DictionaryEntry (var, expr));
4741 // Assign a = new Assign (var, expr, decl.Location);
4742 // assign_block.AddStatement (new StatementExpression (a));
4745 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4746 // It can be referred to as $5 below.
4747 $$ = new DictionaryEntry (type, vars);
4754 Using u = new Using ($5, (Statement) $6, (Location) $1);
4755 current_block.AddStatement (u);
4756 while (current_block.Implicit)
4757 current_block = current_block.Parent;
4759 current_block = current_block.Parent;
4763 resource_acquisition
4764 : local_variable_declaration
4771 // A class used to pass around variable declarations and constants
4773 public class VariableDeclaration {
4774 public string identifier;
4775 public Expression expression_or_array_initializer;
4776 public Location Location;
4777 public Attributes OptAttributes;
4778 public string DocComment;
4780 public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
4782 this.identifier = lt.Value;
4783 if (eoai is ArrayList) {
4784 if (CSharpParser.current_array_type == null)
4785 Report.Error (622, lt.Location,
4786 "Can only use array initializer expressions to assign to array types. Try using a new expression instead.");
4787 this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
4789 this.expression_or_array_initializer = (Expression)eoai;
4791 this.Location = lt.Location;
4792 this.OptAttributes = opt_attrs;
4795 public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
4801 // A class used to hold info about an indexer declarator
4803 public class IndexerDeclaration {
4804 public Expression type;
4805 public MemberName interface_type;
4806 public Parameters param_list;
4807 public Location location;
4809 public IndexerDeclaration (Expression type, MemberName interface_type,
4810 Parameters param_list, Location loc)
4813 this.interface_type = interface_type;
4814 this.param_list = param_list;
4815 this.location = loc;
4820 // We use this when we do not have an object in advance that is an IAnonymousHost
4822 public class SimpleAnonymousHost : IAnonymousHost {
4823 public static readonly SimpleAnonymousHost Simple = new SimpleAnonymousHost ();
4826 ArrayList anonymous_methods;
4828 public static SimpleAnonymousHost GetSimple () {
4829 Simple.yields = false;
4830 Simple.anonymous_methods = null;
4834 public void SetYields ()
4839 public void AddAnonymousMethod (AnonymousMethodExpression anonymous)
4841 if (anonymous_methods == null)
4842 anonymous_methods = new ArrayList ();
4843 anonymous_methods.Add (anonymous);
4846 public void Propagate (IAnonymousHost real_host)
4849 real_host.SetYields ();
4850 if (anonymous_methods != null) {
4851 foreach (AnonymousMethodExpression ame in anonymous_methods)
4852 real_host.AddAnonymousMethod (ame);
4858 // A class used to hold info about an operator declarator
4860 public class OperatorDeclaration {
4861 public Operator.OpType optype;
4862 public Expression ret_type, arg1type, arg2type;
4863 public string arg1name, arg2name;
4864 public Location location;
4866 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4867 Expression arg1type, string arg1name,
4868 Expression arg2type, string arg2name, Location location)
4871 this.ret_type = ret_type;
4872 this.arg1type = arg1type;
4873 this.arg1name = arg1name;
4874 this.arg2type = arg2type;
4875 this.arg2name = arg2name;
4876 this.location = location;
4881 void Error_ExpectingTypeName (Expression expr)
4883 if (expr is Invocation){
4884 Report.Error (1002, expr.Location, "Expecting `;'");
4886 expr.Error_InvalidExpressionStatement ();
4890 public static void Error_ParameterModifierNotValid (Location loc)
4892 Report.Error (631, loc, "The modifiers `ref' and `out' are not valid in this context");
4895 static void Error_DuplicateParameterModifier (Location loc, Parameter.Modifier mod)
4897 Report.Error (1107, loc, "Duplicate parameter modifier `{0}'",
4898 Parameter.GetModifierSignature (mod));
4901 void push_current_class (TypeContainer tc, object partial_token)
4903 if (partial_token != null)
4904 current_container = current_container.AddPartial (tc);
4906 current_container = current_container.AddTypeContainer (tc);
4910 DeclSpace pop_current_class ()
4912 DeclSpace retval = current_class;
4914 current_class = current_class.Parent;
4915 current_container = current_class.PartialContainer;
4921 // Given the @class_name name, it creates a fully qualified name
4922 // based on the containing declaration space
4925 MakeName (MemberName class_name)
4927 Namespace ns = current_namespace.NS;
4929 if (current_container.Name.Length == 0){
4930 if (ns.Name.Length != 0)
4931 return new MemberName (ns.MemberName, class_name);
4935 return new MemberName (current_container.MemberName, class_name);
4939 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4941 Block implicit_block;
4942 ArrayList inits = null;
4945 // We use the `Used' property to check whether statements
4946 // have been added to the current block. If so, we need
4947 // to create another block to contain the new declaration
4948 // otherwise, as an optimization, we use the same block to
4949 // add the declaration.
4951 // FIXME: A further optimization is to check if the statements
4952 // that were added were added as part of the initialization
4953 // below. In which case, no other statements have been executed
4954 // and we might be able to reduce the number of blocks for
4955 // situations like this:
4957 // int j = 1; int k = j + 1;
4959 if (current_block.Used)
4960 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4962 implicit_block = current_block;
4964 foreach (VariableDeclaration decl in variable_declarators){
4966 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
4967 if (decl.expression_or_array_initializer != null){
4969 inits = new ArrayList (4);
4976 return implicit_block;
4978 foreach (VariableDeclaration decl in inits){
4980 Expression expr = decl.expression_or_array_initializer;
4982 LocalVariableReference var;
4983 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4985 assign = new Assign (var, expr, decl.Location);
4987 implicit_block.AddStatement (new StatementExpression (assign));
4990 return implicit_block;
4993 Block declare_local_constants (Expression type, ArrayList declarators)
4995 Block implicit_block;
4997 if (current_block.Used)
4998 implicit_block = new Block (current_block, Block.Flags.Implicit);
5000 implicit_block = current_block;
5002 foreach (VariableDeclaration decl in declarators){
5003 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
5006 return implicit_block;
5009 string CheckAttributeTarget (string a, Location l)
5012 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
5016 Report.Warning (658, 1, l,
5017 "`{0}' is invalid attribute target. All attributes in this attribute section will be ignored", a);
5018 return string.Empty;
5021 void CheckUnaryOperator (Operator.OpType op, Location l)
5025 case Operator.OpType.LogicalNot:
5026 case Operator.OpType.OnesComplement:
5027 case Operator.OpType.Increment:
5028 case Operator.OpType.Decrement:
5029 case Operator.OpType.True:
5030 case Operator.OpType.False:
5031 case Operator.OpType.Addition:
5032 case Operator.OpType.Subtraction:
5037 Report.Error (1019, l, "Overloadable unary operator expected");
5043 void CheckBinaryOperator (Operator.OpType op, Location l)
5047 case Operator.OpType.Addition:
5048 case Operator.OpType.Subtraction:
5049 case Operator.OpType.Multiply:
5050 case Operator.OpType.Division:
5051 case Operator.OpType.Modulus:
5052 case Operator.OpType.BitwiseAnd:
5053 case Operator.OpType.BitwiseOr:
5054 case Operator.OpType.ExclusiveOr:
5055 case Operator.OpType.LeftShift:
5056 case Operator.OpType.RightShift:
5057 case Operator.OpType.Equality:
5058 case Operator.OpType.Inequality:
5059 case Operator.OpType.GreaterThan:
5060 case Operator.OpType.LessThan:
5061 case Operator.OpType.GreaterThanOrEqual:
5062 case Operator.OpType.LessThanOrEqual:
5066 Report.Error (1020, l, "Overloadable binary operator expected");
5072 void syntax_error (Location l, string msg)
5074 Report.Error (1003, l, "Syntax error, " + msg);
5077 void note (string s)
5079 // Used to put annotations
5084 public Tokenizer Lexer {
5090 static CSharpParser ()
5092 oob_stack = new Stack ();
5095 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
5097 this.name = file.Name;
5099 current_namespace = new NamespaceEntry (null, file, null);
5100 current_class = current_namespace.SlaveDeclSpace;
5101 current_container = current_class.PartialContainer; // == RootContest.ToplevelTypes
5103 lexer = new Tokenizer (reader, file, defines);
5106 public void parse ()
5108 int errors = Report.Errors;
5110 if (yacc_verbose_flag > 1)
5111 yyparse (lexer, new yydebug.yyDebugSimple ());
5114 Tokenizer tokenizer = lexer as Tokenizer;
5115 tokenizer.cleanup ();
5116 } catch (Exception e){
5118 // Removed for production use, use parser verbose to get the output.
5120 // Console.WriteLine (e);
5121 if (Report.Errors == errors)
5122 Report.Error (-25, lexer.Location, "Parsing error");
5123 if (yacc_verbose_flag > 0)
5124 Console.WriteLine (e);
5127 if (RootContext.ToplevelTypes.NamespaceEntry != null)
5128 throw new InternalErrorException ("who set it?");
5131 static void CheckToken (int error, int yyToken, string msg, Location loc)
5133 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
5134 Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ());
5136 Report.Error (error, loc, msg);
5139 void CheckIdentifierToken (int yyToken, Location loc)
5141 CheckToken (1041, yyToken, "Identifier expected", loc);
5144 string ConsumeStoredComment ()
5146 string s = tmpComment;
5148 Lexer.doc_state = XmlCommentState.Allowed;
5152 Location GetLocation (object obj)
5154 if (obj is MemberCore)
5155 return ((MemberCore) obj).Location;
5156 if (obj is MemberName)
5157 return ((MemberName) obj).Location;
5158 if (obj is LocatedToken)
5159 return ((LocatedToken) obj).Location;
5160 if (obj is Location)
5161 return (Location) obj;
5162 return lexer.Location;
5165 static GenericMethod current_generic_method = null;
5167 void start_block (Location loc)
5169 if (parsing_anonymous_method) {
5170 top_current_block = new ToplevelBlock (
5171 current_block, current_local_parameters, current_generic_method, loc);
5172 if (current_block != null)
5173 current_block.AddAnonymousChild ((ToplevelBlock) top_current_block);
5174 current_block = top_current_block;
5175 parsing_anonymous_method = false;
5176 } else if (current_block == null) {
5177 current_block = new ToplevelBlock (
5178 (ToplevelBlock) top_current_block, current_local_parameters,
5179 current_generic_method, loc);
5180 top_current_block = current_block;
5182 current_block = new Block (current_block, loc, Location.Null);
5187 end_block (Location loc)
5191 while (current_block.Implicit)
5192 current_block = current_block.Parent;
5193 retval = current_block;
5194 current_block.SetEndLocation (loc);
5195 current_block = current_block.Parent;
5196 if (current_block == null)
5197 top_current_block = null;
5202 start_anonymous (bool lambda, Parameters parameters, Location loc)
5204 oob_stack.Push (current_anonymous_method);
5205 oob_stack.Push (current_local_parameters);
5207 current_local_parameters = parameters;
5209 // Force the next block to be created as a ToplevelBlock
5210 oob_stack.Push (current_block);
5211 oob_stack.Push (top_current_block);
5213 current_anonymous_method = lambda
5214 ? new LambdaExpression (
5215 current_anonymous_method, current_generic_method, current_container,
5216 parameters, (ToplevelBlock) top_current_block, loc)
5217 : new AnonymousMethodExpression (
5218 current_anonymous_method, current_generic_method, current_container,
5219 parameters, (ToplevelBlock) top_current_block, loc);
5221 parsing_anonymous_method = true;
5225 * Completes the anonymous method processing, if lambda_expr is null, this
5226 * means that we have a Statement instead of an Expression embedded
5228 AnonymousMethodExpression
5229 end_anonymous (ToplevelBlock anon_block, Location loc)
5231 AnonymousMethodExpression retval;
5233 top_current_block = (Block) oob_stack.Pop ();
5234 current_block = (Block) oob_stack.Pop ();
5236 if (RootContext.Version == LanguageVersion.ISO_1){
5237 Report.FeatureIsNotISO1 (loc, "anonymous methods");
5240 anon_block.Parent = current_block;
5242 current_anonymous_method.Block = anon_block;
5243 if ((anonymous_host != null) && (current_anonymous_method.Parent == null))
5244 anonymous_host.AddAnonymousMethod (current_anonymous_method);
5246 retval = current_anonymous_method;
5249 current_local_parameters = (Parameters) oob_stack.Pop ();
5250 current_anonymous_method = (AnonymousMethodExpression) oob_stack.Pop ();