3 // cs-parser.jay: The Parser for the C# compiler
5 // Authors: Miguel de Icaza (miguel@gnu.org)
6 // Ravi Pratap (ravi@ximian.com)
7 // Marek Safar (marek.safar@gmail.com)
9 // Licensed under the terms of the GNU GPL
11 // (C) 2001 Ximian, Inc (http://www.ximian.com)
12 // (C) 2004 Novell, Inc
15 // (1) Figure out why error productions dont work. `type-declaration' is a
16 // great spot to put an `error' because you can reproduce it with this input:
19 // Possible optimization:
20 // Run memory profiler with parsing only, and consider dropping
21 // arraylists where not needed. Some pieces can use linked lists.
29 using System.Collections;
34 public class CSharpParser {
35 NamespaceEntry current_namespace;
36 TypeContainer current_container;
37 DeclSpace current_class;
39 IAnonymousHost anonymous_host;
42 /// Current block is used to add statements as we find
47 Delegate current_delegate;
49 AnonymousMethodExpression current_anonymous_method;
52 /// This is used by the unary_expression code to resolve
53 /// a name against a parameter.
55 Parameters current_local_parameters;
58 /// Using during property parsing to describe the implicit
59 /// value parameter that is passed to the "set" and "get"accesor
60 /// methods (properties and indexers).
62 Expression implicit_value_parameter_type;
63 Parameters indexer_parameters;
66 /// Hack to help create non-typed array initializer
68 public static Expression current_array_type;
69 Expression pushed_current_array_type;
72 /// Used to determine if we are parsing the get/set pair
73 /// of an indexer or a property
77 bool parsing_anonymous_method;
80 /// An out-of-band stack.
82 static Stack oob_stack;
89 static public int yacc_verbose_flag;
91 // Name of the file we are parsing
100 /// Temporary Xml documentation cache.
101 /// For enum types, we need one more temporary store.
104 string enumTypeComment;
106 /// Current attribute target
107 string current_attr_target;
109 /// assembly and module attribute definitions are enabled
110 bool global_attrs_enabled = true;
111 bool has_get, has_set;
116 %token NONE /* This token is never returned by our lexer */
117 %token ERROR // This is used not by the parser, but by the tokenizer.
121 *These are the C# keywords
208 /* C# keywords which are not really keywords */
214 /* C# single character operators/punctuation. */
215 %token OPEN_BRACE "{"
216 %token CLOSE_BRACE "}"
217 %token OPEN_BRACKET "["
218 %token CLOSE_BRACKET "]"
219 %token OPEN_PARENS "("
220 %token CLOSE_PARENS ")"
232 %token OP_GENERICS_LT "<"
234 %token OP_GENERICS_GT ">"
235 %token BITWISE_AND "&"
236 %token BITWISE_OR "|"
243 /* C# multi-character operators. */
244 %token DOUBLE_COLON "::"
247 %token OP_SHIFT_LEFT "<<"
248 %token OP_SHIFT_RIGHT ">>"
255 %token OP_MULT_ASSIGN "*="
256 %token OP_DIV_ASSIGN "/="
257 %token OP_MOD_ASSIGN "%="
258 %token OP_ADD_ASSIGN "+="
259 %token OP_SUB_ASSIGN "-="
260 %token OP_SHIFT_LEFT_ASSIGN "<<="
261 %token OP_SHIFT_RIGHT_ASSIGN ">>="
262 %token OP_AND_ASSIGN "&="
263 %token OP_XOR_ASSIGN "^="
264 %token OP_OR_ASSIGN "|="
268 %token LITERAL_INTEGER "int literal"
269 %token LITERAL_FLOAT "float literal"
270 %token LITERAL_DOUBLE "double literal"
271 %token LITERAL_DECIMAL "decimal literal"
272 %token LITERAL_CHARACTER "character literal"
273 %token LITERAL_STRING "string literal"
276 %token OPEN_PARENS_LAMBDA
277 %token CLOSE_PARENS_CAST
278 %token CLOSE_PARENS_NO_CAST
279 %token CLOSE_PARENS_OPEN_PARENS
280 %token CLOSE_PARENS_MINUS
282 /* Add precedence rules to solve dangling else s/r conflict */
291 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
293 %left STAR DIV PERCENT
294 %right BANG CARRET UMINUS
295 %nonassoc OP_INC OP_DEC
297 %left OPEN_BRACKET OPEN_BRACE
301 %start compilation_unit
305 : outer_declarations opt_EOF
306 | outer_declarations global_attributes opt_EOF
307 | global_attributes opt_EOF
308 | opt_EOF /* allow empty files */
314 Lexer.check_incorrect_doc_comment ();
318 Lexer.check_incorrect_doc_comment ();
324 | outer_declarations outer_declaration
328 : extern_alias_directive
330 | namespace_member_declaration
333 extern_alias_directives
334 : extern_alias_directive
335 | extern_alias_directives extern_alias_directive;
337 extern_alias_directive
338 : EXTERN IDENTIFIER IDENTIFIER SEMICOLON
340 LocatedToken lt = (LocatedToken) $2;
343 Report.Error (1003, lt.Location, "'alias' expected");
344 } else if (RootContext.Version == LanguageVersion.ISO_1) {
345 Report.FeatureIsNotISO1 (lt.Location, "external alias");
347 lt = (LocatedToken) $3;
348 current_namespace.UsingExternalAlias (lt.Value, lt.Location);
355 | using_directives using_directive
359 : using_alias_directive
361 if (RootContext.Documentation != null)
362 Lexer.doc_state = XmlCommentState.Allowed;
364 | using_namespace_directive
366 if (RootContext.Documentation != null)
367 Lexer.doc_state = XmlCommentState.Allowed;
371 using_alias_directive
372 : USING IDENTIFIER ASSIGN
373 namespace_or_type_name SEMICOLON
375 LocatedToken lt = (LocatedToken) $2;
376 current_namespace.UsingAlias (lt.Value, (MemberName) $4, (Location) $1);
379 CheckIdentifierToken (yyToken, GetLocation ($2));
383 using_namespace_directive
384 : USING namespace_name SEMICOLON
386 current_namespace.Using ((MemberName) $2, (Location) $1);
391 // Strictly speaking, namespaces don't have attributes but
392 // we parse global attributes along with namespace declarations and then
395 namespace_declaration
396 : opt_attributes NAMESPACE namespace_or_type_name
398 MemberName name = (MemberName) $3;
401 Report.Error(1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
404 current_namespace = new NamespaceEntry (
405 current_namespace, file, name.GetName ());
406 current_class = current_namespace.SlaveDeclSpace;
407 current_container = current_class.PartialContainer;
409 namespace_body opt_semicolon
411 current_namespace = current_namespace.Parent;
412 current_class = current_namespace.SlaveDeclSpace;
413 current_container = current_class.PartialContainer;
428 : namespace_or_type_name
434 if (RootContext.Documentation != null)
435 Lexer.doc_state = XmlCommentState.Allowed;
441 : opt_extern_alias_directives
443 opt_namespace_member_declarations
447 Report.Error (1518, lexer.Location, "Expected `class', `delegate', `enum', `interface', or `struct'");
450 | opt_extern_alias_directives
452 opt_namespace_member_declarations
455 Report.Error (1513, lexer.Location, "} expected");
464 opt_extern_alias_directives
466 | extern_alias_directives
469 opt_namespace_member_declarations
471 | namespace_member_declarations
474 namespace_member_declarations
475 : namespace_member_declaration
476 | namespace_member_declarations namespace_member_declaration
479 namespace_member_declaration
483 DeclSpace ds = (DeclSpace)$1;
485 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
486 Report.Error (1527, ds.Location,
487 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
490 current_namespace.DeclarationFound = true;
492 | namespace_declaration {
493 current_namespace.DeclarationFound = true;
496 | field_declaration {
497 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
499 | method_declaration {
500 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
507 | interface_declaration
509 | delegate_declaration
511 // Enable this when we have handled all errors, because this acts as a generic fallback
514 // Console.WriteLine ("Token=" + yyToken);
515 // Report.Error (1518, GetLocation ($1), "Expected class, struct, interface, enum or delegate");
527 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_attributes opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
1923 // TODO: wrong location
1924 if ((Parameter.Modifier)$6 != Parameter.Modifier.NONE)
1925 Error_ParameterModifierNotValid ((Location) $2);
1927 LocatedToken lt = (LocatedToken) $8;
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) $7;
1940 pars [0] = new Parameter (type, lt.Value, Parameter.Modifier.NONE, (Attributes) $5, 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_attributes opt_parameter_modifier type IDENTIFIER COMMA
1955 opt_attributes opt_parameter_modifier type IDENTIFIER
1958 // TODO: wrong location
1959 if ((Parameter.Modifier)$6 != Parameter.Modifier.NONE || (Parameter.Modifier)$11 != Parameter.Modifier.NONE)
1960 Error_ParameterModifierNotValid ((Location) $2);
1962 LocatedToken ltParam1 = (LocatedToken) $8;
1963 LocatedToken ltParam2 = (LocatedToken) $13;
1964 CheckBinaryOperator ((Operator.OpType) $3, (Location) $2);
1966 Parameter [] pars = new Parameter [2];
1968 Expression typeL = (Expression) $7;
1969 Expression typeR = (Expression) $12;
1971 pars [0] = new Parameter (typeL, ltParam1.Value, Parameter.Modifier.NONE, (Attributes) $5, ltParam1.Location);
1972 pars [1] = new Parameter (typeR, ltParam2.Value, Parameter.Modifier.NONE, (Attributes) $10, 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_attributes opt_parameter_modifier type IDENTIFIER COMMA
1989 opt_attributes opt_parameter_modifier type IDENTIFIER COMMA error
1991 Report.Error (1534, (Location) $2, "Overloaded binary operator `{0}' takes two parameters",
1992 Operator.GetName ((Operator.OpType) $3));
1995 | type OPERATOR overloadable_operator
1996 open_parens CLOSE_PARENS
1998 Report.Error (1535, (Location) $2, "Overloaded unary operator `{0}' takes one parameter",
1999 Operator.GetName ((Operator.OpType) $3));
2004 overloadable_operator
2006 : BANG { $$ = Operator.OpType.LogicalNot; }
2007 | TILDE { $$ = Operator.OpType.OnesComplement; }
2008 | OP_INC { $$ = Operator.OpType.Increment; }
2009 | OP_DEC { $$ = Operator.OpType.Decrement; }
2010 | TRUE { $$ = Operator.OpType.True; }
2011 | FALSE { $$ = Operator.OpType.False; }
2012 // Unary and binary:
2013 | PLUS { $$ = Operator.OpType.Addition; }
2014 | MINUS { $$ = Operator.OpType.Subtraction; }
2016 | STAR { $$ = Operator.OpType.Multiply; }
2017 | DIV { $$ = Operator.OpType.Division; }
2018 | PERCENT { $$ = Operator.OpType.Modulus; }
2019 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
2020 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
2021 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
2022 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
2023 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
2024 | OP_EQ { $$ = Operator.OpType.Equality; }
2025 | OP_NE { $$ = Operator.OpType.Inequality; }
2026 | OP_GT { $$ = Operator.OpType.GreaterThan; }
2027 | OP_LT { $$ = Operator.OpType.LessThan; }
2028 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
2029 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
2032 conversion_operator_declarator
2033 : IMPLICIT OPERATOR type open_parens opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
2035 // TODO: wrong location
2036 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
2037 Error_ParameterModifierNotValid (GetLocation ($4));
2039 LocatedToken lt = (LocatedToken) $7;
2040 Parameter [] pars = new Parameter [1];
2042 pars [0] = new Parameter ((Expression) $6, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2044 current_local_parameters = new Parameters (pars);
2046 if (RootContext.Documentation != null) {
2047 tmpComment = Lexer.consume_doc_comment ();
2048 Lexer.doc_state = XmlCommentState.NotAllowed;
2051 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $6, lt.Value,
2052 null, null, (Location) $2);
2054 | EXPLICIT OPERATOR type open_parens opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
2056 // TODO: wrong location
2057 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
2058 Error_ParameterModifierNotValid (GetLocation ($4));
2060 LocatedToken lt = (LocatedToken) $7;
2061 Parameter [] pars = new Parameter [1];
2063 pars [0] = new Parameter ((Expression) $6, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2065 current_local_parameters = new Parameters (pars);
2067 if (RootContext.Documentation != null) {
2068 tmpComment = Lexer.consume_doc_comment ();
2069 Lexer.doc_state = XmlCommentState.NotAllowed;
2072 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $6, lt.Value,
2073 null, null, (Location) $2);
2077 syntax_error ((Location) $1, "'operator' expected");
2081 syntax_error ((Location) $1, "'operator' expected");
2085 constructor_declaration
2088 constructor_declarator
2091 Constructor c = (Constructor) $3;
2092 c.Block = (ToplevelBlock) $4;
2093 c.OptAttributes = (Attributes) $1;
2094 c.ModFlags = (int) $2;
2096 if (RootContext.Documentation != null)
2097 c.DocComment = ConsumeStoredComment ();
2099 if (c.Name == current_container.Basename){
2100 if ((c.ModFlags & Modifiers.STATIC) != 0){
2101 if ((c.ModFlags & Modifiers.Accessibility) != 0){
2102 Report.Error (515, c.Location,
2103 "`{0}': access modifiers are not allowed on static constructors",
2104 c.GetSignatureForError ());
2107 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2109 if (c.Initializer != null){
2110 Report.Error (514, c.Location,
2111 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2112 c.GetSignatureForError ());
2115 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2118 // We let another layer check the validity of the constructor.
2119 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
2122 current_container.AddConstructor (c);
2124 current_local_parameters = null;
2125 if (RootContext.Documentation != null)
2126 Lexer.doc_state = XmlCommentState.Allowed;
2130 constructor_declarator
2131 : constructor_header
2135 | constructor_header constructor_initializer
2137 ((Constructor)$1).Initializer = (ConstructorInitializer) $2;
2145 if (RootContext.Documentation != null) {
2146 tmpComment = Lexer.consume_doc_comment ();
2147 Lexer.doc_state = XmlCommentState.Allowed;
2150 open_parens opt_formal_parameter_list CLOSE_PARENS
2152 LocatedToken lt = (LocatedToken) $1;
2153 current_local_parameters = (Parameters) $4;
2154 current_block = new ToplevelBlock (null, current_local_parameters, null, lt.Location);
2156 $$ = new Constructor (current_class, lt.Value, 0, current_local_parameters,
2159 anonymous_host = (IAnonymousHost) $$;
2165 | SEMICOLON { current_block = null; $$ = null; }
2168 constructor_initializer
2169 : COLON BASE open_parens opt_argument_list CLOSE_PARENS
2171 $$ = new ConstructorBaseInitializer ((ArrayList) $4, (Location) $2);
2173 | COLON THIS open_parens opt_argument_list CLOSE_PARENS
2175 $$ = new ConstructorThisInitializer ((ArrayList) $4, (Location) $2);
2178 Report.Error (1018, (Location) $1, "Keyword this or base expected");
2184 : /* EMPTY */ { $$ = 0; }
2185 | UNSAFE { $$ = Modifiers.UNSAFE; }
2186 | EXTERN { $$ = Modifiers.EXTERN; }
2189 destructor_declaration
2190 : opt_attributes opt_finalizer TILDE
2192 if (RootContext.Documentation != null) {
2193 tmpComment = Lexer.consume_doc_comment ();
2194 Lexer.doc_state = XmlCommentState.NotAllowed;
2197 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2199 LocatedToken lt = (LocatedToken) $5;
2200 if (lt.Value != current_container.Basename){
2201 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2202 } else if (current_container.Kind != Kind.Class){
2203 Report.Error (575, lt.Location, "Only class types can contain destructor");
2205 Location l = lt.Location;
2208 if (!RootContext.StdLib && current_container.Name == "System.Object")
2209 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2211 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2213 Method d = new Destructor (
2214 current_class, TypeManager.system_void_expr, m, "Finalize",
2215 Parameters.EmptyReadOnlyParameters, (Attributes) $1, l);
2216 if (RootContext.Documentation != null)
2217 d.DocComment = ConsumeStoredComment ();
2219 d.Block = (ToplevelBlock) $8;
2220 current_container.AddMethod (d);
2228 EVENT type variable_declarators SEMICOLON
2230 current_array_type = null;
2231 foreach (VariableDeclaration var in (ArrayList) $5) {
2233 MemberName name = new MemberName (var.identifier,
2236 EventField e = new EventField (
2237 current_class, (Expression) $4, (int) $2, false, name,
2240 e.Initializer = var.expression_or_array_initializer;
2242 current_container.AddEvent (e);
2244 if (RootContext.Documentation != null) {
2245 e.DocComment = Lexer.consume_doc_comment ();
2246 Lexer.doc_state = XmlCommentState.Allowed;
2252 EVENT type namespace_or_type_name
2255 implicit_value_parameter_type = (Expression) $4;
2256 lexer.EventParsing = true;
2258 event_accessor_declarations
2260 lexer.EventParsing = false;
2264 MemberName name = (MemberName) $5;
2267 Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2268 current_container.Name, name.ToString ());
2271 Accessors accessors = (Accessors) $8;
2272 if (accessors.get_or_add == null || accessors.set_or_remove == null)
2273 // CS0073 is already reported, so no CS0065 here.
2276 Event e = new EventProperty (
2277 current_class, (Expression) $4, (int) $2, false, name,
2278 (Attributes) $1, accessors.get_or_add, accessors.set_or_remove);
2279 if (RootContext.Documentation != null) {
2280 e.DocComment = Lexer.consume_doc_comment ();
2281 Lexer.doc_state = XmlCommentState.Allowed;
2284 current_container.AddEvent (e);
2285 implicit_value_parameter_type = null;
2289 | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
2290 MemberName mn = (MemberName) $5;
2292 if (mn.Left != null)
2293 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2295 Report.Error (71, mn.Location, "Event declaration should use property syntax");
2297 if (RootContext.Documentation != null)
2298 Lexer.doc_state = XmlCommentState.Allowed;
2302 event_accessor_declarations
2303 : add_accessor_declaration remove_accessor_declaration
2305 $$ = new Accessors ((Accessor) $1, (Accessor) $2);
2307 | remove_accessor_declaration add_accessor_declaration
2309 Accessors accessors = new Accessors ((Accessor) $2, (Accessor) $1);
2310 accessors.declared_in_reverse = true;
2313 | add_accessor_declaration { $$ = null; }
2314 | remove_accessor_declaration { $$ = null; }
2317 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2323 add_accessor_declaration
2324 : opt_attributes ADD
2326 Parameter [] args = new Parameter [1];
2327 Parameter implicit_value_parameter = new Parameter (
2328 implicit_value_parameter_type, "value",
2329 Parameter.Modifier.NONE, null, (Location) $2);
2331 args [0] = implicit_value_parameter;
2333 current_local_parameters = new Parameters (args);
2334 lexer.EventParsing = false;
2336 anonymous_host = SimpleAnonymousHost.GetSimple ();
2340 Accessor accessor = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2341 lexer.EventParsing = true;
2343 current_local_parameters = null;
2344 SimpleAnonymousHost.Simple.Propagate (accessor);
2345 anonymous_host = null;
2349 | opt_attributes ADD error {
2350 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2353 | opt_attributes modifiers ADD {
2354 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2359 remove_accessor_declaration
2360 : opt_attributes REMOVE
2362 Parameter [] args = new Parameter [1];
2363 Parameter implicit_value_parameter = new Parameter (
2364 implicit_value_parameter_type, "value",
2365 Parameter.Modifier.NONE, null, (Location) $2);
2367 args [0] = implicit_value_parameter;
2369 current_local_parameters = new Parameters (args);
2370 lexer.EventParsing = false;
2374 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2375 lexer.EventParsing = true;
2377 | opt_attributes REMOVE error {
2378 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2381 | opt_attributes modifiers REMOVE {
2382 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2388 : opt_attributes opt_modifiers indexer_declarator
2391 IndexerDeclaration decl = (IndexerDeclaration) $3;
2393 implicit_value_parameter_type = decl.type;
2395 lexer.PropertyParsing = true;
2396 parsing_indexer = true;
2398 indexer_parameters = decl.param_list;
2399 anonymous_host = SimpleAnonymousHost.GetSimple ();
2401 accessor_declarations
2403 lexer.PropertyParsing = false;
2404 has_get = has_set = false;
2405 parsing_indexer = false;
2412 // The signature is computed from the signature of the indexer. Look
2413 // at section 3.6 on the spec
2415 IndexerDeclaration decl = (IndexerDeclaration) $3;
2416 Location loc = decl.location;
2417 Accessors accessors = (Accessors) $6;
2418 Accessor get_block = accessors.get_or_add;
2419 Accessor set_block = accessors.set_or_remove;
2422 if (decl.interface_type != null)
2423 name = new MemberName (decl.interface_type, TypeContainer.DefaultIndexerName, loc);
2425 name = new MemberName (TypeContainer.DefaultIndexerName, loc);
2427 indexer = new Indexer (current_class, decl.type, name,
2428 (int) $2, false, decl.param_list, (Attributes) $1,
2429 get_block, set_block, accessors.declared_in_reverse);
2431 if (RootContext.Documentation != null)
2432 indexer.DocComment = ConsumeStoredComment ();
2434 current_container.AddIndexer (indexer);
2436 current_local_parameters = null;
2437 implicit_value_parameter_type = null;
2438 indexer_parameters = null;
2443 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2445 Parameters pars = (Parameters) $4;
2446 if (pars.HasArglist) {
2447 // "__arglist is not valid in this context"
2448 Report.Error (1669, (Location) $2, "__arglist is not valid in this context");
2449 } else if (pars.Empty){
2450 Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
2452 if (RootContext.Documentation != null) {
2453 tmpComment = Lexer.consume_doc_comment ();
2454 Lexer.doc_state = XmlCommentState.Allowed;
2457 $$ = new IndexerDeclaration ((Expression) $1, null, pars, (Location) $2);
2459 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2461 Parameters pars = (Parameters) $6;
2463 if (pars.HasArglist) {
2464 // "__arglist is not valid in this context"
2465 Report.Error (1669, (Location) $4, "__arglist is not valid in this context");
2466 } else if (pars.Empty){
2467 Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
2470 MemberName name = (MemberName) $2;
2471 $$ = new IndexerDeclaration ((Expression) $1, name, pars, (Location) $4);
2473 if (RootContext.Documentation != null) {
2474 tmpComment = Lexer.consume_doc_comment ();
2475 Lexer.doc_state = XmlCommentState.Allowed;
2485 if (RootContext.Documentation != null)
2486 enumTypeComment = Lexer.consume_doc_comment ();
2491 LocatedToken lt = (LocatedToken) $4;
2492 Location enum_location = lt.Location;
2494 MemberName name = MakeName (new MemberName (lt.Value, enum_location));
2495 Enum e = new Enum (current_namespace, current_class, (Expression) $5, (int) $2,
2496 name, (Attributes) $1);
2498 if (RootContext.Documentation != null)
2499 e.DocComment = enumTypeComment;
2502 EnumMember em = null;
2503 foreach (VariableDeclaration ev in (ArrayList) $7) {
2504 em = new EnumMember (
2505 e, em, ev.identifier, (Expression) ev.expression_or_array_initializer,
2506 ev.OptAttributes, ev.Location);
2508 // if (RootContext.Documentation != null)
2509 em.DocComment = ev.DocComment;
2511 e.AddEnumMember (em);
2514 current_container.AddTypeContainer (e);
2521 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2522 | COLON type { $$ = $2; }
2528 if (RootContext.Documentation != null)
2529 Lexer.doc_state = XmlCommentState.Allowed;
2531 opt_enum_member_declarations
2533 // here will be evaluated after CLOSE_BLACE is consumed.
2534 if (RootContext.Documentation != null)
2535 Lexer.doc_state = XmlCommentState.Allowed;
2543 opt_enum_member_declarations
2544 : /* empty */ { $$ = new ArrayList (4); }
2545 | enum_member_declarations opt_comma { $$ = $1; }
2548 enum_member_declarations
2549 : enum_member_declaration
2551 ArrayList l = new ArrayList (4);
2556 | enum_member_declarations COMMA enum_member_declaration
2558 ArrayList l = (ArrayList) $1;
2566 enum_member_declaration
2567 : opt_attributes IDENTIFIER
2569 VariableDeclaration vd = new VariableDeclaration (
2570 (LocatedToken) $2, null, (Attributes) $1);
2572 if (RootContext.Documentation != null) {
2573 vd.DocComment = Lexer.consume_doc_comment ();
2574 Lexer.doc_state = XmlCommentState.Allowed;
2579 | opt_attributes IDENTIFIER
2581 if (RootContext.Documentation != null) {
2582 tmpComment = Lexer.consume_doc_comment ();
2583 Lexer.doc_state = XmlCommentState.NotAllowed;
2588 VariableDeclaration vd = new VariableDeclaration (
2589 (LocatedToken) $2, $5, (Attributes) $1);
2591 if (RootContext.Documentation != null)
2592 vd.DocComment = ConsumeStoredComment ();
2598 delegate_declaration
2601 DELEGATE type member_name
2602 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2605 MemberName name = MakeName ((MemberName) $5);
2606 Parameters p = (Parameters) $7;
2608 // TODO: wrong location
2609 Report.Error (1669, name.Location, "__arglist is not valid in this context");
2612 Delegate del = new Delegate (current_namespace, current_class, (Expression) $4,
2613 (int) $2, name, p, (Attributes) $1);
2615 if (RootContext.Documentation != null) {
2616 del.DocComment = Lexer.consume_doc_comment ();
2617 Lexer.doc_state = XmlCommentState.Allowed;
2620 current_container.AddDelegate (del);
2625 namespace_or_type_name
2627 | IDENTIFIER DOUBLE_COLON IDENTIFIER {
2628 LocatedToken lt1 = (LocatedToken) $1;
2629 LocatedToken lt2 = (LocatedToken) $3;
2630 $$ = new MemberName (lt1.Value, lt2.Value, lt2.Location);
2632 | namespace_or_type_name DOT IDENTIFIER {
2633 LocatedToken lt = (LocatedToken) $3;
2634 $$ = new MemberName ((MemberName) $1, lt.Value);
2640 LocatedToken lt = (LocatedToken) $1;
2641 $$ = new MemberName (lt.Value, lt.Location);
2646 * Before you think of adding a return_type, notice that we have been
2647 * using two rules in the places where it matters (one rule using type
2648 * and another identical one that uses VOID as the return type). This
2649 * gets rid of a shift/reduce couple
2652 : namespace_or_type_name
2654 MemberName name = (MemberName) $1;
2655 $$ = name.GetTypeExpression ();
2667 // Note that here only unmanaged types are allowed but we
2668 // can't perform checks during this phase - we do it during
2669 // semantic analysis.
2671 $$ = new ComposedCast ((Expression) $1, "*", Lexer.Location);
2675 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2681 | non_expression_type rank_specifier
2683 Location loc = GetLocation ($1);
2685 loc = lexer.Location;
2686 $$ = new ComposedCast ((Expression) $1, (string) $2, loc);
2688 | non_expression_type STAR
2690 Location loc = GetLocation ($1);
2692 loc = lexer.Location;
2693 $$ = new ComposedCast ((Expression) $1, "*", loc);
2695 | expression rank_specifiers
2697 $$ = new ComposedCast ((Expression) $1, (string) $2);
2701 $$ = new ComposedCast ((Expression) $1, "*");
2705 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2706 // through this different path
2708 | multiplicative_expression STAR
2710 $$ = new ComposedCast ((Expression) $1, "*");
2717 ArrayList types = new ArrayList (4);
2722 | type_list COMMA type
2724 ArrayList types = (ArrayList) $1;
2732 * replaces all the productions for isolating the various
2733 * simple types, but we need this to reuse it easily in local_variable_type
2736 : OBJECT { $$ = TypeManager.system_object_expr; }
2737 | STRING { $$ = TypeManager.system_string_expr; }
2738 | BOOL { $$ = TypeManager.system_boolean_expr; }
2739 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2740 | FLOAT { $$ = TypeManager.system_single_expr; }
2741 | DOUBLE { $$ = TypeManager.system_double_expr; }
2746 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2747 | BYTE { $$ = TypeManager.system_byte_expr; }
2748 | SHORT { $$ = TypeManager.system_int16_expr; }
2749 | USHORT { $$ = TypeManager.system_uint16_expr; }
2750 | INT { $$ = TypeManager.system_int32_expr; }
2751 | UINT { $$ = TypeManager.system_uint32_expr; }
2752 | LONG { $$ = TypeManager.system_int64_expr; }
2753 | ULONG { $$ = TypeManager.system_uint64_expr; }
2754 | CHAR { $$ = TypeManager.system_char_expr; }
2755 | VOID { $$ = TypeManager.system_void_expr; }
2759 : type rank_specifiers
2761 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2);
2766 // Expressions, section 7.5
2775 MemberName mn = (MemberName) $1;
2776 $$ = mn.GetTypeExpression ();
2778 | IDENTIFIER DOUBLE_COLON IDENTIFIER
2780 LocatedToken lt1 = (LocatedToken) $1;
2781 LocatedToken lt2 = (LocatedToken) $3;
2782 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, lt2.Location);
2784 | parenthesized_expression
2786 | invocation_expression
2790 | post_increment_expression
2791 | post_decrement_expression
2795 | checked_expression
2796 | unchecked_expression
2797 | pointer_member_access
2798 | anonymous_method_expression
2805 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
2806 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); }
2807 | NULL { $$ = new NullLiteral (lexer.Location); }
2811 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
2812 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
2813 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
2818 object v = lexer.Value;
2821 $$ = new IntLiteral ((int) v, lexer.Location);
2822 } else if (v is uint)
2823 $$ = new UIntLiteral ((UInt32) v, lexer.Location);
2825 $$ = new LongLiteral ((Int64) v, lexer.Location);
2826 else if (v is ulong)
2827 $$ = new ULongLiteral ((UInt64) v, lexer.Location);
2829 Console.WriteLine ("OOPS. Unexpected result from scanner");
2834 : TRUE { $$ = new BoolLiteral (true, lexer.Location); }
2835 | FALSE { $$ = new BoolLiteral (false, lexer.Location); }
2838 parenthesized_expression_0
2839 : OPEN_PARENS expression CLOSE_PARENS
2842 lexer.Deambiguate_CloseParens ($$);
2843 // After this, the next token returned is one of
2844 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST (CLOSE_PARENS), CLOSE_PARENS_OPEN_PARENS
2845 // or CLOSE_PARENS_MINUS.
2847 | OPEN_PARENS expression error { CheckToken (1026, yyToken, "Expecting ')'", lexer.Location); }
2850 parenthesized_expression
2851 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2855 | parenthesized_expression_0 CLOSE_PARENS
2859 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2861 // If a parenthesized expression is followed by a minus, we need to wrap
2862 // the expression inside a ParenthesizedExpression for the CS0075 check
2863 // in Binary.DoResolve().
2864 $$ = new ParenthesizedExpression ((Expression) $1);
2869 : primary_expression DOT IDENTIFIER
2871 LocatedToken lt = (LocatedToken) $3;
2872 $$ = new MemberAccess ((Expression) $1, lt.Value);
2874 | predefined_type DOT IDENTIFIER
2876 LocatedToken lt = (LocatedToken) $3;
2877 // TODO: Location is wrong as some predefined types doesn't hold a location
2878 $$ = new MemberAccess ((Expression) $1, lt.Value, lt.Location);
2886 invocation_expression
2887 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2890 Report.Error (1, (Location) $2, "Parse error");
2892 $$ = new Invocation ((Expression) $1, (ArrayList) $3);
2894 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2896 $$ = new Invocation ((Expression) $1, new ArrayList ());
2898 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2900 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
2902 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2904 ArrayList args = new ArrayList (1);
2906 $$ = new Invocation ((Expression) $1, args);
2908 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2910 ArrayList args = ((ArrayList) $4);
2912 $$ = new Invocation ((Expression) $1, args);
2917 : /* empty */ { $$ = null; }
2924 ArrayList list = new ArrayList (4);
2928 | argument_list COMMA argument
2930 ArrayList list = (ArrayList) $1;
2934 | argument_list error {
2935 CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
2943 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2945 | non_simple_argument
2952 : REF variable_reference
2954 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2956 | OUT variable_reference
2958 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2960 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2962 ArrayList list = (ArrayList) $3;
2963 Argument[] args = new Argument [list.Count];
2964 list.CopyTo (args, 0);
2966 Expression expr = new Arglist (args, (Location) $1);
2967 $$ = new Argument (expr, Argument.AType.Expression);
2969 | ARGLIST OPEN_PARENS CLOSE_PARENS
2971 $$ = new Argument (new Arglist ((Location) $1), Argument.AType.Expression);
2975 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
2980 : expression { note ("section 5.4"); $$ = $1; }
2984 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2986 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
2988 | primary_expression rank_specifiers
2990 // So the super-trick is that primary_expression
2991 // can only be either a SimpleName or a MemberAccess.
2992 // The MemberAccess case arises when you have a fully qualified type-name like :
2994 // SimpleName is when you have
2997 Expression expr = (Expression) $1;
2998 if (expr is ComposedCast){
2999 $$ = new ComposedCast (expr, (string) $2);
3000 } else if (!(expr is SimpleName || expr is MemberAccess || expr is QualifiedAliasMember)){
3001 Error_ExpectingTypeName (expr);
3002 $$ = TypeManager.system_object_expr;
3005 // So we extract the string corresponding to the SimpleName
3008 $$ = new ComposedCast (expr, (string) $2);
3010 current_array_type = (Expression)$$;
3017 ArrayList list = new ArrayList (4);
3021 | expression_list COMMA expression
3023 ArrayList list = (ArrayList) $1;
3032 $$ = new This (current_block, (Location) $1);
3037 : BASE DOT IDENTIFIER
3039 LocatedToken lt = (LocatedToken) $3;
3040 $$ = new BaseAccess (lt.Value, lt.Location);
3042 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
3044 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
3047 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
3052 post_increment_expression
3053 : primary_expression OP_INC
3055 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
3056 (Expression) $1, (Location) $2);
3060 post_decrement_expression
3061 : primary_expression OP_DEC
3063 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
3064 (Expression) $1, (Location) $2);
3069 : object_or_delegate_creation_expression
3070 | array_creation_expression
3073 object_or_delegate_creation_expression
3074 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
3076 $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
3080 array_creation_expression
3081 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
3083 opt_array_initializer
3085 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
3087 | NEW type rank_specifiers array_initializer
3089 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, (Location) $1);
3093 Report.Error (1031, (Location) $1, "Type expected");
3098 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
3115 : rank_specifier opt_rank_specifier
3117 $$ = (string) $2 + (string) $1;
3122 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3124 $$ = "[" + (string) $2 + "]";
3144 | dim_separators COMMA
3146 $$ = (string) $1 + ",";
3150 opt_array_initializer
3162 : OPEN_BRACE CLOSE_BRACE
3164 ArrayList list = new ArrayList (4);
3167 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3169 $$ = (ArrayList) $2;
3173 variable_initializer_list
3174 : variable_initializer
3176 ArrayList list = new ArrayList (4);
3180 | variable_initializer_list COMMA variable_initializer
3182 ArrayList list = (ArrayList) $1;
3191 pushed_current_array_type = current_array_type;
3193 OPEN_PARENS type CLOSE_PARENS
3195 Expression type = (Expression)$4;
3196 if (type == TypeManager.system_void_expr)
3197 $$ = new TypeOfVoid ((Location) $1);
3199 $$ = new TypeOf (type, (Location) $1);
3200 current_array_type = pushed_current_array_type;
3205 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3206 $$ = new SizeOf ((Expression) $3, (Location) $1);
3211 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3213 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3217 unchecked_expression
3218 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3220 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3224 pointer_member_access
3225 : primary_expression OP_PTR IDENTIFIER
3228 LocatedToken lt = (LocatedToken) $3;
3230 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
3231 $$ = new MemberAccess (deref, lt.Value);
3235 anonymous_method_expression
3236 : DELEGATE opt_anonymous_method_signature
3238 start_anonymous (false, (Parameters) $2, (Location) $1);
3242 $$ = end_anonymous ((ToplevelBlock) $4, (Location) $1);
3246 opt_anonymous_method_signature
3247 : /* empty */ { $$ = null; }
3248 | anonymous_method_signature
3251 anonymous_method_signature
3252 : open_parens opt_anonymous_method_parameter_list CLOSE_PARENS
3255 $$ = Parameters.EmptyReadOnlyParameters;
3257 ArrayList par_list = (ArrayList) $2;
3258 Parameter [] pars = new Parameter [par_list.Count];
3259 par_list.CopyTo (pars);
3260 $$ = new Parameters (pars);
3265 opt_anonymous_method_parameter_list
3266 : /* empty */ { $$ = null; }
3267 | anonymous_method_parameter_list { $$ = $1; }
3270 anonymous_method_parameter_list
3271 : anonymous_method_parameter
3273 ArrayList a = new ArrayList (4);
3277 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3279 ArrayList a = (ArrayList) $1;
3285 anonymous_method_parameter
3286 : opt_parameter_modifier type IDENTIFIER {
3287 LocatedToken lt = (LocatedToken) $3;
3288 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3290 | PARAMS type IDENTIFIER {
3291 Report.Error (1670, ((LocatedToken) $3).Location, "The `params' modifier is not allowed in anonymous method declaration");
3297 : primary_expression
3298 | BANG prefixed_unary_expression
3300 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3302 | TILDE prefixed_unary_expression
3304 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3310 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3312 $$ = new Cast ((Expression) $1, (Expression) $3);
3314 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3316 $$ = new Cast ((Expression) $1, (Expression) $3);
3322 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3324 // TODO: wrong location
3325 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3330 // The idea to split this out is from Rhys' grammar
3331 // to solve the problem with casts.
3333 prefixed_unary_expression
3335 | PLUS prefixed_unary_expression
3337 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3339 | MINUS prefixed_unary_expression
3341 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3343 | OP_INC prefixed_unary_expression
3345 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3346 (Expression) $2, (Location) $1);
3348 | OP_DEC prefixed_unary_expression
3350 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3351 (Expression) $2, (Location) $1);
3353 | STAR prefixed_unary_expression
3355 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, (Location) $1);
3357 | BITWISE_AND prefixed_unary_expression
3359 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3363 pre_increment_expression
3364 : OP_INC prefixed_unary_expression
3366 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3367 (Expression) $2, (Location) $1);
3371 pre_decrement_expression
3372 : OP_DEC prefixed_unary_expression
3374 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3375 (Expression) $2, (Location) $1);
3379 multiplicative_expression
3380 : prefixed_unary_expression
3381 | multiplicative_expression STAR prefixed_unary_expression
3383 $$ = new Binary (Binary.Operator.Multiply,
3384 (Expression) $1, (Expression) $3);
3386 | multiplicative_expression DIV prefixed_unary_expression
3388 $$ = new Binary (Binary.Operator.Division,
3389 (Expression) $1, (Expression) $3);
3391 | multiplicative_expression PERCENT prefixed_unary_expression
3393 $$ = new Binary (Binary.Operator.Modulus,
3394 (Expression) $1, (Expression) $3);
3399 : multiplicative_expression
3400 | additive_expression PLUS multiplicative_expression
3402 $$ = new Binary (Binary.Operator.Addition,
3403 (Expression) $1, (Expression) $3);
3405 | additive_expression MINUS multiplicative_expression
3407 $$ = new Binary (Binary.Operator.Subtraction,
3408 (Expression) $1, (Expression) $3);
3413 : additive_expression
3414 | shift_expression OP_SHIFT_LEFT additive_expression
3416 $$ = new Binary (Binary.Operator.LeftShift,
3417 (Expression) $1, (Expression) $3);
3419 | shift_expression OP_SHIFT_RIGHT additive_expression
3421 $$ = new Binary (Binary.Operator.RightShift,
3422 (Expression) $1, (Expression) $3);
3426 relational_expression
3428 | relational_expression OP_LT shift_expression
3430 $$ = new Binary (Binary.Operator.LessThan,
3431 (Expression) $1, (Expression) $3);
3433 | relational_expression OP_GT shift_expression
3435 $$ = new Binary (Binary.Operator.GreaterThan,
3436 (Expression) $1, (Expression) $3);
3438 | relational_expression OP_LE shift_expression
3440 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3441 (Expression) $1, (Expression) $3);
3443 | relational_expression OP_GE shift_expression
3445 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3446 (Expression) $1, (Expression) $3);
3448 | relational_expression IS type
3450 $$ = new Is ((Expression) $1, (Expression) $3, (Location) $2);
3452 | relational_expression AS type
3454 $$ = new As ((Expression) $1, (Expression) $3, (Location) $2);
3459 : relational_expression
3460 | equality_expression OP_EQ relational_expression
3462 $$ = new Binary (Binary.Operator.Equality,
3463 (Expression) $1, (Expression) $3);
3465 | equality_expression OP_NE relational_expression
3467 $$ = new Binary (Binary.Operator.Inequality,
3468 (Expression) $1, (Expression) $3);
3473 : equality_expression
3474 | and_expression BITWISE_AND equality_expression
3476 $$ = new Binary (Binary.Operator.BitwiseAnd,
3477 (Expression) $1, (Expression) $3);
3481 exclusive_or_expression
3483 | exclusive_or_expression CARRET and_expression
3485 $$ = new Binary (Binary.Operator.ExclusiveOr,
3486 (Expression) $1, (Expression) $3);
3490 inclusive_or_expression
3491 : exclusive_or_expression
3492 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3494 $$ = new Binary (Binary.Operator.BitwiseOr,
3495 (Expression) $1, (Expression) $3);
3499 conditional_and_expression
3500 : inclusive_or_expression
3501 | conditional_and_expression OP_AND inclusive_or_expression
3503 $$ = new Binary (Binary.Operator.LogicalAnd,
3504 (Expression) $1, (Expression) $3);
3508 conditional_or_expression
3509 : conditional_and_expression
3510 | conditional_or_expression OP_OR conditional_and_expression
3512 $$ = new Binary (Binary.Operator.LogicalOr,
3513 (Expression) $1, (Expression) $3);
3517 conditional_expression
3518 : conditional_or_expression
3519 | conditional_or_expression INTERR expression COLON expression
3521 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
3525 assignment_expression
3526 : prefixed_unary_expression ASSIGN expression
3528 $$ = new Assign ((Expression) $1, (Expression) $3);
3530 | prefixed_unary_expression OP_MULT_ASSIGN expression
3532 $$ = new CompoundAssign (
3533 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
3535 | prefixed_unary_expression OP_DIV_ASSIGN expression
3537 $$ = new CompoundAssign (
3538 Binary.Operator.Division, (Expression) $1, (Expression) $3);
3540 | prefixed_unary_expression OP_MOD_ASSIGN expression
3542 $$ = new CompoundAssign (
3543 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
3545 | prefixed_unary_expression OP_ADD_ASSIGN expression
3547 $$ = new CompoundAssign (
3548 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
3550 | prefixed_unary_expression OP_SUB_ASSIGN expression
3552 $$ = new CompoundAssign (
3553 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3555 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3557 $$ = new CompoundAssign (
3558 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
3560 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3562 $$ = new CompoundAssign (
3563 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
3565 | prefixed_unary_expression OP_AND_ASSIGN expression
3567 $$ = new CompoundAssign (
3568 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
3570 | prefixed_unary_expression OP_OR_ASSIGN expression
3572 $$ = new CompoundAssign (
3573 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
3575 | prefixed_unary_expression OP_XOR_ASSIGN expression
3577 $$ = new CompoundAssign (
3578 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
3582 implicitly_typed_lambda_parameter_list
3584 LocatedToken lt = (LocatedToken) $1;
3585 ArrayList a = new ArrayList (4);
3587 a.Add (new Parameter ((Expression)null, lt.Value, Parameter.Modifier.NONE, null, lt.Location));
3590 | implicitly_typed_lambda_parameter_list COMMA IDENTIFIER {
3591 LocatedToken lt = (LocatedToken) $3;
3592 ArrayList a = (ArrayList) $1;
3593 a.Add (new Parameter ((Expression)null, lt.Value, Parameter.Modifier.NONE, null, lt.Location));
3598 explicitly_typed_lambda_parameter_list
3599 : explicitly_typed_lambda_parameter
3601 ArrayList pars = new ArrayList (4);
3606 | explicitly_typed_lambda_parameter_list COMMA explicitly_typed_lambda_parameter
3608 ArrayList pars = (ArrayList) $1;
3615 explicitly_typed_lambda_parameter
3616 : parameter_modifier type IDENTIFIER
3618 LocatedToken lt = (LocatedToken) $3;
3620 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3624 LocatedToken lt = (LocatedToken) $2;
3626 $$ = new Parameter ((Expression) $1, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
3630 lambda_parameter_list
3631 : implicitly_typed_lambda_parameter_list { $$ = $1; }
3632 | explicitly_typed_lambda_parameter_list { $$ = $1; }
3635 opt_lambda_parameter_list
3636 : /* empty */ { $$ = null; }
3637 | lambda_parameter_list {
3638 ArrayList pars_list = (ArrayList) $1;
3640 Parameter [] pars = new Parameter [pars_list.Count];
3641 pars_list.CopyTo (pars);
3642 $$ = new Parameters (pars);
3646 lambda_expression_body
3648 start_block (lexer.Location);
3652 Block b = end_block (lexer.Location);
3653 b.AddStatement (new ContextualReturn ((Expression) $2));
3664 Parameter [] pars = new Parameter [1];
3665 LocatedToken lt = (LocatedToken) $1;
3666 pars [0] = new Parameter ((Expression)null, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
3668 Parameters parameters = new Parameters (pars);
3670 start_anonymous (true, parameters, (Location) $2);
3672 lambda_expression_body
3674 $$ = end_anonymous ((ToplevelBlock) $4, (Location) $2);
3676 | OPEN_PARENS_LAMBDA opt_lambda_parameter_list CLOSE_PARENS ARROW
3678 start_anonymous (true, (Parameters) $2, (Location) $4);
3680 lambda_expression_body
3682 $$ = end_anonymous ((ToplevelBlock) $6, (Location) $4);
3687 : conditional_expression
3688 | assignment_expression
3709 MemberName name = MakeName ((MemberName) $5);
3710 int mod_flags = (int) $2;
3712 push_current_class (new Class (
3713 current_namespace, current_class, name,
3714 mod_flags, (Attributes) $1), $3);
3719 if (current_class.Name == "System.Object") {
3720 Report.Error (537, current_class.Location,
3721 "The class System.Object cannot have a base " +
3722 "class or implement an interface.");
3724 current_container.AddBasesForPart (current_class, (ArrayList) $7);
3727 if (RootContext.Documentation != null) {
3728 current_container.DocComment = Lexer.consume_doc_comment ();
3729 Lexer.doc_state = XmlCommentState.Allowed;
3734 if (RootContext.Documentation != null)
3735 Lexer.doc_state = XmlCommentState.Allowed;
3739 $$ = pop_current_class ();
3747 { $$ = $1; } // location
3751 : /* empty */ { $$ = (int) 0; }
3757 | modifiers modifier
3762 if ((m1 & m2) != 0) {
3763 Location l = lexer.Location;
3764 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
3766 $$ = (int) (m1 | m2);
3771 : NEW { $$ = Modifiers.NEW; }
3772 | PUBLIC { $$ = Modifiers.PUBLIC; }
3773 | PROTECTED { $$ = Modifiers.PROTECTED; }
3774 | INTERNAL { $$ = Modifiers.INTERNAL; }
3775 | PRIVATE { $$ = Modifiers.PRIVATE; }
3776 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3777 | SEALED { $$ = Modifiers.SEALED; }
3778 | STATIC { $$ = Modifiers.STATIC; }
3779 | READONLY { $$ = Modifiers.READONLY; }
3780 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3781 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3782 | EXTERN { $$ = Modifiers.EXTERN; }
3783 | VOLATILE { $$ = Modifiers.VOLATILE; }
3784 | UNSAFE { $$ = Modifiers.UNSAFE; }
3788 : /* empty */ { $$ = null; }
3789 | class_base { $$ = $1; }
3793 : COLON type_list { $$ = $2; }
3801 // A block is "contained" on the following places:
3803 // property_declaration as part of the accessor body (get/set)
3804 // operator_declaration
3805 // constructor_declaration
3806 // destructor_declaration
3807 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3812 start_block ((Location) $1);
3814 opt_statement_list CLOSE_BRACE
3816 $$ = end_block ((Location) $4);
3822 opt_statement_list CLOSE_BRACE
3824 $$ = end_block ((Location) $3);
3835 | statement_list statement
3839 : declaration_statement
3841 if ($1 != null && (Block) $1 != current_block){
3842 current_block.AddStatement ((Statement) $1);
3843 current_block = (Block) $1;
3846 | valid_declaration_statement
3848 current_block.AddStatement ((Statement) $1);
3853 valid_declaration_statement
3856 | expression_statement
3857 | selection_statement
3858 | iteration_statement
3862 | unchecked_statement
3870 : valid_declaration_statement
3871 | declaration_statement
3873 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3878 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3886 $$ = EmptyStatement.Value;
3893 LocatedToken lt = (LocatedToken) $1;
3894 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
3896 if (current_block.AddLabel (labeled))
3897 current_block.AddStatement (labeled);
3902 declaration_statement
3903 : local_variable_declaration SEMICOLON
3905 current_array_type = null;
3907 DictionaryEntry de = (DictionaryEntry) $1;
3908 Expression e = (Expression) de.Key;
3910 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
3914 | local_constant_declaration SEMICOLON
3916 current_array_type = null;
3918 DictionaryEntry de = (DictionaryEntry) $1;
3920 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3926 * The following is from Rhys' grammar:
3927 * > Types in local variable declarations must be recognized as
3928 * > expressions to prevent reduce/reduce errors in the grammar.
3929 * > The expressions are converted into types during semantic analysis.
3932 : primary_expression opt_rank_specifier
3934 // FIXME: Do something smart here regarding the composition of the type.
3936 // Ok, the above "primary_expression" is there to get rid of
3937 // both reduce/reduce and shift/reduces in the grammar, it should
3938 // really just be "type_name". If you use type_name, a reduce/reduce
3939 // creeps up. If you use namespace_or_type_name (which is all we need
3940 // really) two shift/reduces appear.
3943 // So the super-trick is that primary_expression
3944 // can only be either a SimpleName or a MemberAccess.
3945 // The MemberAccess case arises when you have a fully qualified type-name like :
3947 // SimpleName is when you have
3950 Expression expr = (Expression) $1;
3951 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3952 Error_ExpectingTypeName (expr);
3956 // So we extract the string corresponding to the SimpleName
3960 if ((string) $2 == "")
3963 $$ = new ComposedCast ((Expression) $1, (string) $2);
3966 | builtin_types opt_rank_specifier
3968 if ((string) $2 == "")
3971 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3975 local_variable_pointer_type
3976 : primary_expression STAR
3978 Expression expr = (Expression) $1;
3980 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3981 Error_ExpectingTypeName (expr);
3985 $$ = new ComposedCast ((Expression) $1, "*");
3987 | builtin_types STAR
3989 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3993 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
3995 | local_variable_pointer_type STAR
3997 $$ = new ComposedCast ((Expression) $1, "*");
4001 local_variable_declaration
4002 : local_variable_type variable_declarators
4005 $$ = new DictionaryEntry ($1, $2);
4009 | local_variable_pointer_type opt_rank_specifier variable_declarators
4014 if ((string) $2 == "")
4015 t = (Expression) $1;
4017 t = new ComposedCast ((Expression) $1, (string) $2);
4018 $$ = new DictionaryEntry (t, $3);
4024 local_constant_declaration
4025 : CONST local_variable_type constant_declarators
4028 $$ = new DictionaryEntry ($2, $3);
4034 expression_statement
4035 : statement_expression SEMICOLON { $$ = $1; }
4039 // We have to do the wrapping here and not in the case above,
4040 // because statement_expression is used for example in for_statement
4042 statement_expression
4045 Expression expr = (Expression) $1;
4046 ExpressionStatement s = expr as ExpressionStatement;
4048 expr.Error_InvalidExpressionStatement ();
4051 $$ = new StatementExpression (s);
4055 Report.Error (1002, GetLocation ($1), "Expecting `;'");
4060 object_creation_expression
4061 : object_or_delegate_creation_expression
4062 { note ("complain if this is a delegate maybe?"); }
4071 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
4074 Location l = (Location) $1;
4076 $$ = new If ((Expression) $3, (Statement) $5, l);
4078 // FIXME: location for warning should be loc property of $5.
4079 if ($5 == EmptyStatement.Value)
4080 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4083 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
4084 embedded_statement ELSE embedded_statement
4086 Location l = (Location) $1;
4088 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
4090 // FIXME: location for warning should be loc property of $5 and $7.
4091 if ($5 == EmptyStatement.Value)
4092 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4093 if ($7 == EmptyStatement.Value)
4094 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4099 : SWITCH OPEN_PARENS
4101 if (switch_stack == null)
4102 switch_stack = new Stack (2);
4103 switch_stack.Push (current_block);
4105 expression CLOSE_PARENS
4108 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
4109 current_block = (Block) switch_stack.Pop ();
4125 Report.Warning (1522, 1, lexer.Location, "Empty switch block");
4126 $$ = new ArrayList ();
4134 ArrayList sections = new ArrayList (4);
4139 | switch_sections switch_section
4141 ArrayList sections = (ArrayList) $1;
4151 current_block = current_block.CreateSwitchBlock (lexer.Location);
4155 $$ = new SwitchSection ((ArrayList) $1, current_block.Explicit);
4162 ArrayList labels = new ArrayList (4);
4167 | switch_labels switch_label
4169 ArrayList labels = (ArrayList) ($1);
4177 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
4178 | DEFAULT COLON { $$ = new SwitchLabel (null, (Location) $1); }
4181 1523, GetLocation ($1),
4182 "The keyword case or default must precede code in switch block");
4194 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
4196 Location l = (Location) $1;
4197 $$ = new While ((Expression) $3, (Statement) $5, l);
4202 : DO embedded_statement
4203 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
4205 Location l = (Location) $1;
4207 $$ = new Do ((Statement) $2, (Expression) $5, l);
4213 opt_for_initializer SEMICOLON
4215 Location l = lexer.Location;
4217 Block assign_block = current_block;
4219 if ($3 is DictionaryEntry){
4220 DictionaryEntry de = (DictionaryEntry) $3;
4222 Expression type = (Expression) de.Key;
4223 ArrayList var_declarators = (ArrayList) de.Value;
4225 foreach (VariableDeclaration decl in var_declarators){
4229 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4233 Expression expr = decl.expression_or_array_initializer;
4235 LocalVariableReference var;
4236 var = new LocalVariableReference (assign_block, decl.identifier, l);
4239 Assign a = new Assign (var, expr, decl.Location);
4241 assign_block.AddStatement (new StatementExpression (a));
4245 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4246 // This can be referred to as $5 below.
4252 opt_for_condition SEMICOLON
4253 opt_for_iterator CLOSE_PARENS
4256 Location l = (Location) $1;
4258 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4260 current_block.AddStatement (f);
4262 $$ = end_block (lexer.Location);
4267 : /* empty */ { $$ = EmptyStatement.Value; }
4272 : local_variable_declaration
4273 | statement_expression_list
4277 : /* empty */ { $$ = null; }
4278 | boolean_expression
4282 : /* empty */ { $$ = EmptyStatement.Value; }
4287 : statement_expression_list
4290 statement_expression_list
4291 : statement_expression
4293 // CHANGE: was `null'
4294 Statement s = (Statement) $1;
4295 Block b = new Block (current_block, s.loc, lexer.Location);
4300 | statement_expression_list COMMA statement_expression
4302 Block b = (Block) $1;
4304 b.AddStatement ((Statement) $3);
4310 : FOREACH open_parens type IN expression CLOSE_PARENS
4312 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4315 | FOREACH open_parens type IDENTIFIER IN
4316 expression CLOSE_PARENS
4318 start_block (lexer.Location);
4319 Block foreach_block = current_block;
4321 LocatedToken lt = (LocatedToken) $4;
4322 Location l = lt.Location;
4325 vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4327 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4329 // Get a writable reference to this read-only variable.
4331 // Note that the $$ here refers to the value of _this_ code block,
4332 // not the value of the LHS non-terminal. This can be referred to as $8 below.
4333 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
4340 LocalVariableReference v = (LocalVariableReference) $8;
4341 Location l = (Location) $1;
4344 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
4345 current_block.AddStatement (f);
4348 $$ = end_block (lexer.Location);
4354 | continue_statement
4364 $$ = new Break ((Location) $1);
4369 : CONTINUE SEMICOLON
4371 $$ = new Continue ((Location) $1);
4376 : GOTO IDENTIFIER SEMICOLON
4378 LocatedToken lt = (LocatedToken) $2;
4379 $$ = new Goto (lt.Value, lt.Location);
4381 | GOTO CASE constant_expression SEMICOLON
4383 $$ = new GotoCase ((Expression) $3, (Location) $1);
4385 | GOTO DEFAULT SEMICOLON
4387 $$ = new GotoDefault ((Location) $1);
4392 : RETURN opt_expression SEMICOLON
4394 $$ = new Return ((Expression) $2, (Location) $1);
4399 : THROW opt_expression SEMICOLON
4401 $$ = new Throw ((Expression) $2, (Location) $1);
4406 : IDENTIFIER RETURN expression SEMICOLON
4408 LocatedToken lt = (LocatedToken) $1;
4409 string s = lt.Value;
4411 Report.Error (1003, lt.Location, "; expected");
4414 if (RootContext.Version == LanguageVersion.ISO_1){
4415 Report.FeatureIsNotISO1 (lt.Location, "yield statement");
4418 if (anonymous_host == null){
4419 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4422 anonymous_host.SetYields ();
4423 $$ = new Yield ((Expression) $3, lt.Location);
4426 | IDENTIFIER RETURN SEMICOLON
4428 Report.Error (1627, (Location) $2, "Expression expected after yield return");
4431 | IDENTIFIER BREAK SEMICOLON
4433 LocatedToken lt = (LocatedToken) $1;
4434 string s = lt.Value;
4436 Report.Error (1003, lt.Location, "; expected");
4439 if (RootContext.Version == LanguageVersion.ISO_1){
4440 Report.FeatureIsNotISO1 (lt.Location, "yield statement");
4443 if (anonymous_host == null){
4444 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4447 anonymous_host.SetYields ();
4448 $$ = new YieldBreak (lt.Location);
4459 : TRY block catch_clauses
4463 ArrayList c = (ArrayList)$3;
4464 for (int i = 0; i < c.Count; ++i) {
4465 Catch cc = (Catch) c [i];
4467 if (i != c.Count - 1)
4468 Report.Error (1017, cc.loc, "Try statement already has an empty catch block");
4475 // Now s contains the list of specific catch clauses
4476 // and g contains the general one.
4478 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4480 | TRY block opt_catch_clauses FINALLY block
4483 ArrayList s = new ArrayList (4);
4484 ArrayList catch_list = (ArrayList) $3;
4486 if (catch_list != null){
4487 foreach (Catch cc in catch_list) {
4495 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4499 Report.Error (1524, (Location) $1, "Expected catch or finally");
4505 : /* empty */ { $$ = null; }
4512 ArrayList l = new ArrayList (4);
4517 | catch_clauses catch_clause
4519 ArrayList l = (ArrayList) $1;
4527 : /* empty */ { $$ = null; }
4532 : CATCH opt_catch_args
4534 Expression type = null;
4537 DictionaryEntry cc = (DictionaryEntry) $2;
4538 type = (Expression) cc.Key;
4539 LocatedToken lt = (LocatedToken) cc.Value;
4542 ArrayList one = new ArrayList (4);
4544 one.Add (new VariableDeclaration (lt, null));
4546 start_block (lexer.Location);
4547 current_block = declare_local_variables (type, one, lt.Location);
4551 Expression type = null;
4553 Block var_block = null;
4556 DictionaryEntry cc = (DictionaryEntry) $2;
4557 type = (Expression) cc.Key;
4558 LocatedToken lt = (LocatedToken) cc.Value;
4562 var_block = end_block (lexer.Location);
4566 $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
4571 : /* empty */ { $$ = null; }
4576 : open_parens type opt_identifier CLOSE_PARENS
4578 $$ = new DictionaryEntry ($2, $3);
4586 $$ = new Checked ((Block) $2);
4593 $$ = new Unchecked ((Block) $2);
4600 RootContext.CheckUnsafeOption ((Location) $1);
4602 $$ = new Unsafe ((Block) $3);
4608 type fixed_pointer_declarators
4611 ArrayList list = (ArrayList) $4;
4612 Expression type = (Expression) $3;
4613 Location l = (Location) $1;
4614 int top = list.Count;
4616 start_block (lexer.Location);
4617 Block assign_block = current_block;
4619 for (int i = 0; i < top; i++){
4620 Pair p = (Pair) list [i];
4623 v = current_block.AddVariable (type, (string) p.First, l);
4627 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4635 Location l = (Location) $1;
4637 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4639 current_block.AddStatement (f);
4641 $$ = end_block (lexer.Location);
4645 fixed_pointer_declarators
4646 : fixed_pointer_declarator {
4647 ArrayList declarators = new ArrayList (4);
4649 declarators.Add ($1);
4652 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4654 ArrayList declarators = (ArrayList) $1;
4656 declarators.Add ($3);
4661 fixed_pointer_declarator
4662 : IDENTIFIER ASSIGN expression
4664 LocatedToken lt = (LocatedToken) $1;
4665 // FIXME: keep location
4666 $$ = new Pair (lt.Value, $3);
4670 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
4676 : LOCK OPEN_PARENS expression CLOSE_PARENS
4682 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
4687 : USING open_parens resource_acquisition CLOSE_PARENS
4689 start_block (lexer.Location);
4690 Block assign_block = current_block;
4692 if ($3 is DictionaryEntry){
4693 DictionaryEntry de = (DictionaryEntry) $3;
4694 Location l = (Location) $1;
4696 Expression type = (Expression) de.Key;
4697 ArrayList var_declarators = (ArrayList) de.Value;
4699 ArrayList vars = new ArrayList (4);
4701 foreach (VariableDeclaration decl in var_declarators){
4703 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4706 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
4708 Expression expr = decl.expression_or_array_initializer;
4710 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4713 LocalVariableReference var;
4715 // Get a writable reference to this read-only variable.
4716 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4718 // This is so that it is not a warning on using variables
4721 vars.Add (new DictionaryEntry (var, expr));
4723 // Assign a = new Assign (var, expr, decl.Location);
4724 // assign_block.AddStatement (new StatementExpression (a));
4727 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4728 // It can be referred to as $5 below.
4729 $$ = new DictionaryEntry (type, vars);
4736 Using u = new Using ($5, (Statement) $6, (Location) $1);
4737 current_block.AddStatement (u);
4738 $$ = end_block (lexer.Location);
4742 resource_acquisition
4743 : local_variable_declaration
4750 // A class used to pass around variable declarations and constants
4752 public class VariableDeclaration {
4753 public string identifier;
4754 public Expression expression_or_array_initializer;
4755 public Location Location;
4756 public Attributes OptAttributes;
4757 public string DocComment;
4759 public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
4761 this.identifier = lt.Value;
4762 if (eoai is ArrayList) {
4763 if (CSharpParser.current_array_type == null)
4764 Report.Error (622, lt.Location,
4765 "Can only use array initializer expressions to assign to array types. Try using a new expression instead.");
4766 this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
4768 this.expression_or_array_initializer = (Expression)eoai;
4770 this.Location = lt.Location;
4771 this.OptAttributes = opt_attrs;
4774 public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
4780 // A class used to hold info about an indexer declarator
4782 public class IndexerDeclaration {
4783 public Expression type;
4784 public MemberName interface_type;
4785 public Parameters param_list;
4786 public Location location;
4788 public IndexerDeclaration (Expression type, MemberName interface_type,
4789 Parameters param_list, Location loc)
4792 this.interface_type = interface_type;
4793 this.param_list = param_list;
4794 this.location = loc;
4799 // We use this when we do not have an object in advance that is an IAnonymousHost
4801 public class SimpleAnonymousHost : IAnonymousHost {
4802 public static readonly SimpleAnonymousHost Simple = new SimpleAnonymousHost ();
4805 ArrayList anonymous_methods;
4807 public static SimpleAnonymousHost GetSimple () {
4808 Simple.yields = false;
4809 Simple.anonymous_methods = null;
4813 public void SetYields ()
4818 public void AddAnonymousMethod (AnonymousMethodExpression anonymous)
4820 if (anonymous_methods == null)
4821 anonymous_methods = new ArrayList ();
4822 anonymous_methods.Add (anonymous);
4825 public void Propagate (IAnonymousHost real_host)
4828 real_host.SetYields ();
4829 if (anonymous_methods != null) {
4830 foreach (AnonymousMethodExpression ame in anonymous_methods)
4831 real_host.AddAnonymousMethod (ame);
4837 // A class used to hold info about an operator declarator
4839 public class OperatorDeclaration {
4840 public Operator.OpType optype;
4841 public Expression ret_type, arg1type, arg2type;
4842 public string arg1name, arg2name;
4843 public Location location;
4845 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4846 Expression arg1type, string arg1name,
4847 Expression arg2type, string arg2name, Location location)
4850 this.ret_type = ret_type;
4851 this.arg1type = arg1type;
4852 this.arg1name = arg1name;
4853 this.arg2type = arg2type;
4854 this.arg2name = arg2name;
4855 this.location = location;
4860 void Error_ExpectingTypeName (Expression expr)
4862 if (expr is Invocation){
4863 Report.Error (1002, expr.Location, "Expecting `;'");
4865 expr.Error_InvalidExpressionStatement ();
4869 public static void Error_ParameterModifierNotValid (Location loc)
4871 Report.Error (631, loc, "The modifiers `ref' and `out' are not valid in this context");
4874 static void Error_DuplicateParameterModifier (Location loc, Parameter.Modifier mod)
4876 Report.Error (1107, loc, "Duplicate parameter modifier `{0}'",
4877 Parameter.GetModifierSignature (mod));
4880 void push_current_class (TypeContainer tc, object partial_token)
4882 if (partial_token != null)
4883 current_container = current_container.AddPartial (tc);
4885 current_container = current_container.AddTypeContainer (tc);
4889 DeclSpace pop_current_class ()
4891 DeclSpace retval = current_class;
4893 current_class = current_class.Parent;
4894 current_container = current_class.PartialContainer;
4900 // Given the @class_name name, it creates a fully qualified name
4901 // based on the containing declaration space
4904 MakeName (MemberName class_name)
4906 Namespace ns = current_namespace.NS;
4908 if (current_container.Name.Length == 0){
4909 if (ns.Name.Length != 0)
4910 return new MemberName (ns.MemberName, class_name);
4914 return new MemberName (current_container.MemberName, class_name);
4918 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4920 Block implicit_block;
4921 ArrayList inits = null;
4924 // We use the `Used' property to check whether statements
4925 // have been added to the current block. If so, we need
4926 // to create another block to contain the new declaration
4927 // otherwise, as an optimization, we use the same block to
4928 // add the declaration.
4930 // FIXME: A further optimization is to check if the statements
4931 // that were added were added as part of the initialization
4932 // below. In which case, no other statements have been executed
4933 // and we might be able to reduce the number of blocks for
4934 // situations like this:
4936 // int j = 1; int k = j + 1;
4938 if (current_block.Used)
4939 implicit_block = new Block (current_block, loc, Location.Null);
4941 implicit_block = current_block;
4943 foreach (VariableDeclaration decl in variable_declarators){
4945 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
4946 if (decl.expression_or_array_initializer != null){
4948 inits = new ArrayList (4);
4955 return implicit_block;
4957 foreach (VariableDeclaration decl in inits){
4959 Expression expr = decl.expression_or_array_initializer;
4961 LocalVariableReference var;
4962 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4964 assign = new Assign (var, expr, decl.Location);
4966 implicit_block.AddStatement (new StatementExpression (assign));
4969 return implicit_block;
4972 Block declare_local_constants (Expression type, ArrayList declarators)
4974 Block implicit_block;
4976 if (current_block.Used)
4977 implicit_block = new Block (current_block);
4979 implicit_block = current_block;
4981 foreach (VariableDeclaration decl in declarators){
4982 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
4985 return implicit_block;
4988 string CheckAttributeTarget (string a, Location l)
4991 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4995 Report.Warning (658, 1, l,
4996 "`{0}' is invalid attribute target. All attributes in this attribute section will be ignored", a);
4997 return string.Empty;
5000 void CheckUnaryOperator (Operator.OpType op, Location l)
5004 case Operator.OpType.LogicalNot:
5005 case Operator.OpType.OnesComplement:
5006 case Operator.OpType.Increment:
5007 case Operator.OpType.Decrement:
5008 case Operator.OpType.True:
5009 case Operator.OpType.False:
5010 case Operator.OpType.Addition:
5011 case Operator.OpType.Subtraction:
5016 Report.Error (1019, l, "Overloadable unary operator expected");
5022 void CheckBinaryOperator (Operator.OpType op, Location l)
5026 case Operator.OpType.Addition:
5027 case Operator.OpType.Subtraction:
5028 case Operator.OpType.Multiply:
5029 case Operator.OpType.Division:
5030 case Operator.OpType.Modulus:
5031 case Operator.OpType.BitwiseAnd:
5032 case Operator.OpType.BitwiseOr:
5033 case Operator.OpType.ExclusiveOr:
5034 case Operator.OpType.LeftShift:
5035 case Operator.OpType.RightShift:
5036 case Operator.OpType.Equality:
5037 case Operator.OpType.Inequality:
5038 case Operator.OpType.GreaterThan:
5039 case Operator.OpType.LessThan:
5040 case Operator.OpType.GreaterThanOrEqual:
5041 case Operator.OpType.LessThanOrEqual:
5045 Report.Error (1020, l, "Overloadable binary operator expected");
5051 void syntax_error (Location l, string msg)
5053 Report.Error (1003, l, "Syntax error, " + msg);
5056 void note (string s)
5058 // Used to put annotations
5063 public Tokenizer Lexer {
5069 static CSharpParser ()
5071 oob_stack = new Stack ();
5074 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
5076 this.name = file.Name;
5078 current_namespace = new NamespaceEntry (null, file, null);
5079 current_class = current_namespace.SlaveDeclSpace;
5080 current_container = current_class.PartialContainer; // == RootContest.ToplevelTypes
5082 lexer = new Tokenizer (reader, file, defines);
5085 public void parse ()
5087 int errors = Report.Errors;
5089 if (yacc_verbose_flag > 1)
5090 yyparse (lexer, new yydebug.yyDebugSimple ());
5093 Tokenizer tokenizer = lexer as Tokenizer;
5094 tokenizer.cleanup ();
5095 } catch (Exception e){
5097 // Removed for production use, use parser verbose to get the output.
5099 // Console.WriteLine (e);
5100 if (Report.Errors == errors)
5101 Report.Error (-25, lexer.Location, "Parsing error");
5102 if (yacc_verbose_flag > 0)
5103 Console.WriteLine (e);
5106 if (RootContext.ToplevelTypes.NamespaceEntry != null)
5107 throw new InternalErrorException ("who set it?");
5110 static void CheckToken (int error, int yyToken, string msg, Location loc)
5112 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
5113 Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ());
5115 Report.Error (error, loc, msg);
5118 void CheckIdentifierToken (int yyToken, Location loc)
5120 CheckToken (1041, yyToken, "Identifier expected", loc);
5123 string ConsumeStoredComment ()
5125 string s = tmpComment;
5127 Lexer.doc_state = XmlCommentState.Allowed;
5131 Location GetLocation (object obj)
5133 if (obj is MemberCore)
5134 return ((MemberCore) obj).Location;
5135 if (obj is MemberName)
5136 return ((MemberName) obj).Location;
5137 if (obj is LocatedToken)
5138 return ((LocatedToken) obj).Location;
5139 if (obj is Location)
5140 return (Location) obj;
5141 return lexer.Location;
5144 static GenericMethod current_generic_method = null;
5146 void start_block (Location loc)
5148 if (current_block == null || parsing_anonymous_method) {
5149 current_block = new ToplevelBlock (current_block, current_local_parameters, current_generic_method, loc);
5150 parsing_anonymous_method = false;
5152 current_block = new ExplicitBlock (current_block, loc, Location.Null);
5157 end_block (Location loc)
5159 Block retval = current_block.Explicit;
5160 retval.SetEndLocation (loc);
5161 current_block = retval.Parent;
5166 start_anonymous (bool lambda, Parameters parameters, Location loc)
5168 oob_stack.Push (current_anonymous_method);
5169 oob_stack.Push (current_local_parameters);
5171 current_local_parameters = parameters;
5173 ToplevelBlock top_current_block = current_block == null ? null : current_block.Toplevel;
5175 current_anonymous_method = lambda
5176 ? new LambdaExpression (
5177 current_anonymous_method, current_generic_method, current_container,
5178 parameters, top_current_block, loc)
5179 : new AnonymousMethodExpression (
5180 current_anonymous_method, current_generic_method, current_container,
5181 parameters, top_current_block, loc);
5183 // Force the next block to be created as a ToplevelBlock
5184 parsing_anonymous_method = true;
5188 * Completes the anonymous method processing, if lambda_expr is null, this
5189 * means that we have a Statement instead of an Expression embedded
5191 AnonymousMethodExpression
5192 end_anonymous (ToplevelBlock anon_block, Location loc)
5194 AnonymousMethodExpression retval;
5196 if (RootContext.Version == LanguageVersion.ISO_1){
5197 Report.FeatureIsNotISO1 (loc, "anonymous methods");
5200 current_anonymous_method.Block = anon_block;
5201 if ((anonymous_host != null) && (current_anonymous_method.Parent == null))
5202 anonymous_host.AddAnonymousMethod (current_anonymous_method);
5204 retval = current_anonymous_method;
5207 current_local_parameters = (Parameters) oob_stack.Pop ();
5208 current_anonymous_method = (AnonymousMethodExpression) oob_stack.Pop ();