3 // cs-parser.jay: The Parser for the C# compiler
5 // Authors: Miguel de Icaza (miguel@gnu.org)
6 // Ravi Pratap (ravi@ximian.com)
8 // Licensed under the terms of the GNU GPL
10 // (C) 2001 Ximian, Inc (http://www.ximian.com)
11 // (C) 2004 Novell, Inc
14 // (1) Figure out why error productions dont work. `type-declaration' is a
15 // great spot to put an `error' because you can reproduce it with this input:
18 // Possible optimization:
19 // Run memory profiler with parsing only, and consider dropping
20 // arraylists where not needed. Some pieces can use linked lists.
28 using System.Collections;
33 public class CSharpParser {
34 NamespaceEntry current_namespace;
35 TypeContainer current_container;
36 TypeContainer current_class;
38 IIteratorContainer iterator_container;
41 /// Current block is used to add statements as we find
44 Block current_block, top_current_block;
47 /// This is used by the unary_expression code to resolve
48 /// a name against a parameter.
50 Parameters current_local_parameters;
53 /// Using during property parsing to describe the implicit
54 /// value parameter that is passed to the "set" and "get"accesor
55 /// methods (properties and indexers).
57 Expression implicit_value_parameter_type;
58 Parameters indexer_parameters;
61 /// Hack to help create non-typed array initializer
63 public static Expression current_array_type;
66 /// Used to determine if we are parsing the get/set pair
67 /// of an indexer or a property
72 /// An out-of-band stack.
81 static public int yacc_verbose_flag;
83 // Name of the file we are parsing
92 /// Temporary Xml documentation cache.
93 /// For enum types, we need one more temporary store.
96 string enumTypeComment;
98 /// Current attribute target
99 string current_attr_target;
101 /// assembly and module attribute definitions are enabled
102 bool global_attrs_enabled = true;
104 bool has_get, has_set;
109 %token NONE /* This token is never returned by our lexer */
110 %token ERROR // This is used not by the parser, but by the tokenizer.
114 *These are the C# keywords
200 /* C# keywords which are not really keywords */
206 /* C# single character operators/punctuation. */
207 %token OPEN_BRACE "{"
208 %token CLOSE_BRACE "}"
209 %token OPEN_BRACKET "["
210 %token CLOSE_BRACKET "]"
211 %token OPEN_PARENS "("
212 %token CLOSE_PARENS ")"
225 %token BITWISE_AND "&"
226 %token BITWISE_OR "|"
233 /* C# multi-character operators. */
234 %token DOUBLE_COLON "::"
237 %token OP_SHIFT_LEFT "<<"
238 %token OP_SHIFT_RIGHT ">>"
245 %token OP_MULT_ASSIGN "*="
246 %token OP_DIV_ASSIGN "/="
247 %token OP_MOD_ASSIGN "%="
248 %token OP_ADD_ASSIGN "+="
249 %token OP_SUB_ASSIGN "-="
250 %token OP_SHIFT_LEFT_ASSIGN "<<="
251 %token OP_SHIFT_RIGHT_ASSIGN ">>="
252 %token OP_AND_ASSIGN "&="
253 %token OP_XOR_ASSIGN "^="
254 %token OP_OR_ASSIGN "|="
258 %token LITERAL_INTEGER "int literal"
259 %token LITERAL_FLOAT "float literal"
260 %token LITERAL_DOUBLE "double literal"
261 %token LITERAL_DECIMAL "decimal literal"
262 %token LITERAL_CHARACTER "character literal"
263 %token LITERAL_STRING "string literal"
266 %token CLOSE_PARENS_CAST
267 %token CLOSE_PARENS_NO_CAST
268 %token CLOSE_PARENS_OPEN_PARENS
269 %token CLOSE_PARENS_MINUS
271 /* Add precedence rules to solve dangling else s/r conflict */
280 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
282 %left STAR DIV PERCENT
283 %right BANG CARRET UMINUS
284 %nonassoc OP_INC OP_DEC
286 %left OPEN_BRACKET OPEN_BRACE
290 %start compilation_unit
294 : outer_declarations opt_EOF
295 | outer_declarations global_attributes opt_EOF
296 | global_attributes opt_EOF
297 | opt_EOF /* allow empty files */
303 Lexer.check_incorrect_doc_comment ();
307 Lexer.check_incorrect_doc_comment ();
313 | outer_declarations outer_declaration
317 : extern_alias_directive
319 | namespace_member_declaration
322 extern_alias_directives
323 : extern_alias_directive
324 | extern_alias_directives extern_alias_directive;
326 extern_alias_directive
327 : EXTERN IDENTIFIER IDENTIFIER SEMICOLON
329 LocatedToken lt = (LocatedToken) $2;
332 Report.Error (1003, lt.Location, "'alias' expected");
333 } else if (RootContext.Version == LanguageVersion.ISO_1) {
334 Report.FeatureIsNotStandardized (lt.Location, "external alias");
336 lt = (LocatedToken) $3;
337 current_namespace.UsingExternalAlias (lt.Value, lt.Location);
344 | using_directives using_directive
348 : using_alias_directive
350 if (RootContext.Documentation != null)
351 Lexer.doc_state = XmlCommentState.Allowed;
353 | using_namespace_directive
355 if (RootContext.Documentation != null)
356 Lexer.doc_state = XmlCommentState.Allowed;
360 using_alias_directive
361 : USING IDENTIFIER ASSIGN
362 namespace_or_type_name SEMICOLON
364 LocatedToken lt = (LocatedToken) $2;
365 current_namespace.UsingAlias (lt.Value, (MemberName) $4, (Location) $1);
368 CheckIdentifierToken (yyToken, GetLocation ($2));
372 using_namespace_directive
373 : USING namespace_name SEMICOLON
375 current_namespace.Using ((MemberName) $2, (Location) $1);
380 // Strictly speaking, namespaces don't have attributes but
381 // we parse global attributes along with namespace declarations and then
384 namespace_declaration
385 : opt_attributes NAMESPACE namespace_or_type_name
387 MemberName name = (MemberName) $3;
390 Report.Error(1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
393 current_namespace = new NamespaceEntry (
394 current_namespace, file, name.GetName ());
396 namespace_body opt_semicolon
398 current_namespace = current_namespace.Parent;
413 : namespace_or_type_name
419 if (RootContext.Documentation != null)
420 Lexer.doc_state = XmlCommentState.Allowed;
422 opt_extern_alias_directives
424 opt_namespace_member_declarations
433 opt_extern_alias_directives
435 | extern_alias_directives
438 opt_namespace_member_declarations
440 | namespace_member_declarations
443 namespace_member_declarations
444 : namespace_member_declaration
445 | namespace_member_declarations namespace_member_declaration
448 namespace_member_declaration
452 DeclSpace ds = (DeclSpace)$1;
454 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
455 Report.Error (1527, ds.Location,
456 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
459 current_namespace.DeclarationFound = true;
461 | namespace_declaration {
462 current_namespace.DeclarationFound = true;
465 | field_declaration {
466 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
468 | method_declaration {
469 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
476 | interface_declaration
478 | delegate_declaration
480 // Enable this when we have handled all errors, because this acts as a generic fallback
483 // Console.WriteLine ("Token=" + yyToken);
484 // Report.Error (1518, GetLocation ($1), "Expected class, struct, interface, enum or delegate");
496 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
504 global_attrs_enabled = false;
509 global_attrs_enabled = false;
518 ArrayList sect = (ArrayList) $1;
520 if (global_attrs_enabled) {
521 if (current_attr_target == "module") {
522 CodeGen.Module.AddAttributes (sect);
524 } else if (current_attr_target != null && current_attr_target.Length > 0) {
525 CodeGen.Assembly.AddAttributes (sect);
528 $$ = new Attributes (sect);
531 if (RootContext.Documentation != null) {
532 Lexer.check_incorrect_doc_comment ();
534 XmlCommentState.Allowed;
538 $$ = new Attributes (sect);
540 current_attr_target = null;
542 | attribute_sections attribute_section
544 Attributes attrs = $1 as Attributes;
545 ArrayList sect = (ArrayList) $2;
547 if (global_attrs_enabled) {
548 if (current_attr_target == "module") {
549 CodeGen.Module.AddAttributes (sect);
551 } else if (current_attr_target == "assembly") {
552 CodeGen.Assembly.AddAttributes (sect);
556 attrs = new Attributes (sect);
558 attrs.AddAttributes (sect);
562 attrs = new Attributes (sect);
564 attrs.AddAttributes (sect);
567 current_attr_target = null;
572 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
576 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
582 attribute_target_specifier
583 : attribute_target COLON
585 current_attr_target = (string)$1;
593 LocatedToken lt = (LocatedToken) $1;
594 CheckAttributeTarget (lt.Value, lt.Location);
595 $$ = lt.Value; // Location won't be required anymore.
597 | EVENT { $$ = "event"; }
598 | RETURN { $$ = "return"; }
604 ArrayList attrs = new ArrayList (4);
610 | attribute_list COMMA attribute
612 ArrayList attrs = (ArrayList) $1;
620 : attribute_name opt_attribute_arguments
622 MemberName mname = (MemberName) $1;
623 object[] arguments = (object[]) $2;
624 MemberName left = mname.Left;
625 string identifier = mname.Name;
627 Expression left_expr = left == null ? null : left.GetTypeExpression ();
629 if (current_attr_target == "assembly" || current_attr_target == "module")
630 // FIXME: supply "nameEscaped" parameter here.
631 $$ = new GlobalAttribute (current_namespace, current_attr_target,
632 left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
634 $$ = new Attribute (current_attr_target, left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
639 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
642 opt_attribute_arguments
643 : /* empty */ { $$ = null; }
644 | OPEN_PARENS attribute_arguments CLOSE_PARENS
652 : opt_positional_argument_list
657 $$ = new object [] { $1, null };
660 | positional_argument_list COMMA named_argument_list
662 $$ = new object[] { $1, $3 };
664 | named_argument_list
666 $$ = new object [] { null, $1 };
671 opt_positional_argument_list
672 : /* empty */ { $$ = null; }
673 | positional_argument_list
676 positional_argument_list
679 ArrayList args = new ArrayList (4);
680 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
684 | positional_argument_list COMMA expression
686 ArrayList args = (ArrayList) $1;
687 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
696 ArrayList args = new ArrayList (4);
701 | named_argument_list COMMA named_argument
703 ArrayList args = (ArrayList) $1;
708 | named_argument_list COMMA expression
710 Report.Error (1016, ((Expression) $3).Location, "Named attribute argument expected");
716 : IDENTIFIER ASSIGN expression
718 // FIXME: keep location
719 $$ = new DictionaryEntry (
720 ((LocatedToken) $1).Value,
721 new Argument ((Expression) $3, Argument.AType.Expression));
727 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
730 opt_class_member_declarations
732 | class_member_declarations
735 class_member_declarations
736 : class_member_declaration
737 | class_member_declarations
738 class_member_declaration
741 class_member_declaration
742 : constant_declaration // done
743 | field_declaration // done
744 | method_declaration // done
745 | property_declaration // done
746 | event_declaration // done
747 | indexer_declaration // done
748 | operator_declaration // done
749 | constructor_declaration // done
750 | destructor_declaration // done
760 MemberName name = MakeName ((MemberName) $5);
761 current_class = new Struct (
762 current_namespace, current_class, name, (int) $2,
766 current_container = current_container.AddPartial (current_class);
768 current_container.AddClassOrStruct (current_class);
769 current_container = current_class;
775 current_class.Bases = (ArrayList) $7;
777 if (RootContext.Documentation != null)
778 current_container.DocComment = Lexer.consume_doc_comment ();
782 if (RootContext.Documentation != null)
783 Lexer.doc_state = XmlCommentState.Allowed;
787 $$ = pop_current_class ();
789 | opt_attributes opt_modifiers opt_partial STRUCT error {
790 CheckIdentifierToken (yyToken, GetLocation ($5));
797 if (RootContext.Documentation != null)
798 Lexer.doc_state = XmlCommentState.Allowed;
800 opt_struct_member_declarations CLOSE_BRACE
803 opt_struct_member_declarations
805 | struct_member_declarations
808 struct_member_declarations
809 : struct_member_declaration
810 | struct_member_declarations struct_member_declaration
813 struct_member_declaration
814 : constant_declaration
817 | property_declaration
819 | indexer_declaration
820 | operator_declaration
821 | constructor_declaration
825 * This is only included so we can flag error 575:
826 * destructors only allowed on class types
828 | destructor_declaration
839 int modflags = (int) $2;
840 foreach (VariableDeclaration constant in (ArrayList) $5){
841 Location l = constant.Location;
842 if ((modflags & Modifiers.STATIC) != 0) {
843 Report.Error (504, l, "The constant `{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
847 Const c = new Const (
848 current_class, (Expression) $4, (string) constant.identifier,
849 (Expression) constant.expression_or_array_initializer, modflags,
852 if (RootContext.Documentation != null) {
853 c.DocComment = Lexer.consume_doc_comment ();
854 Lexer.doc_state = XmlCommentState.Allowed;
856 current_container.AddConstant (c);
862 : constant_declarator
864 ArrayList constants = new ArrayList (4);
869 | constant_declarators COMMA constant_declarator
872 ArrayList constants = (ArrayList) $1;
879 : IDENTIFIER ASSIGN constant_expression
881 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
885 // A const field requires a value to be provided
886 Report.Error (145, ((LocatedToken) $1).Location, "A const field requires a value to be provided");
898 Expression type = (Expression) $3;
901 current_array_type = null;
903 foreach (VariableDeclaration var in (ArrayList) $4){
904 Field field = new Field (current_class, type, mod, var.identifier,
905 (Attributes) $1, var.Location);
907 field.Initializer = var.expression_or_array_initializer;
909 if (RootContext.Documentation != null) {
910 field.DocComment = Lexer.consume_doc_comment ();
911 Lexer.doc_state = XmlCommentState.Allowed;
913 current_container.AddField (field);
914 $$ = field; // FIXME: might be better if it points to the top item
921 fixed_variable_declarators
924 Expression type = (Expression) $4;
927 current_array_type = null;
929 foreach (VariableDeclaration var in (ArrayList) $5) {
930 FixedField field = new FixedField (current_class, type, mod, var.identifier,
931 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
933 if (RootContext.Documentation != null) {
934 field.DocComment = Lexer.consume_doc_comment ();
935 Lexer.doc_state = XmlCommentState.Allowed;
937 current_container.AddField (field);
938 $$ = field; // FIXME: might be better if it points to the top item
946 current_array_type = null;
947 Report.Error (670, (Location) $3, "Fields cannot have void type");
951 fixed_variable_declarators
952 : fixed_variable_declarator
954 ArrayList decl = new ArrayList (2);
958 | fixed_variable_declarators COMMA fixed_variable_declarator
960 ArrayList decls = (ArrayList) $1;
966 fixed_variable_declarator
967 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
969 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
971 | IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
973 Report.Error (443, lexer.Location, "Value or constant expected");
974 $$ = new VariableDeclaration ((LocatedToken) $1, null);
979 : variable_declarator
981 ArrayList decl = new ArrayList (4);
986 | variable_declarators COMMA variable_declarator
988 ArrayList decls = (ArrayList) $1;
995 : IDENTIFIER ASSIGN variable_initializer
997 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1001 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1003 | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
1005 Report.Error (650, ((LocatedToken) $1).Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
1006 "To declare a fixed size buffer field, use the fixed keyword before the field type");
1011 variable_initializer
1020 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
1022 $$ = new StackAlloc ((Expression) $2, (Expression) $4, (Location) $1);
1026 Report.Error (1575, (Location) $1, "A stackalloc expression requires [] after type");
1033 iterator_container = (IIteratorContainer) $1;
1034 if (RootContext.Documentation != null)
1035 Lexer.doc_state = XmlCommentState.NotAllowed;
1039 Method method = (Method) $1;
1040 method.Block = (ToplevelBlock) $3;
1041 current_container.AddMethod (method);
1043 current_local_parameters = null;
1044 iterator_container = null;
1046 if (RootContext.Documentation != null)
1047 Lexer.doc_state = XmlCommentState.Allowed;
1060 Report.Error (1585, lexer.Location,
1061 "Member modifier `{0}' must precede the member type and name",
1062 Modifiers.Name (i));
1073 type namespace_or_type_name
1074 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1076 MemberName name = (MemberName) $4;
1078 Method method = new Method (current_class, (Expression) $3, (int) $2,
1079 false, name, (Parameters) $6, (Attributes) $1);
1081 current_local_parameters = (Parameters) $6;
1083 if (RootContext.Documentation != null)
1084 method.DocComment = Lexer.consume_doc_comment ();
1090 VOID namespace_or_type_name
1091 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1093 MemberName name = (MemberName) $4;
1095 Method method = new Method (current_class, TypeManager.system_void_expr,
1096 (int) $2, false, name, (Parameters) $6,
1099 current_local_parameters = (Parameters) $6;
1101 if (RootContext.Documentation != null)
1102 method.DocComment = Lexer.consume_doc_comment ();
1109 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1111 MemberName name = (MemberName) $5;
1112 Report.Error (1585, name.Location,
1113 "Member modifier `{0}' must precede the member type and name", Modifiers.Name ((int) $4));
1115 Method method = new Method (current_class, TypeManager.system_void_expr,
1116 0, false, name, (Parameters) $7, (Attributes) $1);
1118 current_local_parameters = (Parameters) $7;
1120 if (RootContext.Documentation != null)
1121 method.DocComment = Lexer.consume_doc_comment ();
1129 | SEMICOLON { $$ = null; }
1132 opt_formal_parameter_list
1133 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1134 | formal_parameter_list
1137 formal_parameter_list
1140 ArrayList pars_list = (ArrayList) $1;
1142 Parameter [] pars = new Parameter [pars_list.Count];
1143 pars_list.CopyTo (pars);
1145 $$ = new Parameters (pars);
1147 | fixed_parameters COMMA parameter_array
1149 ArrayList pars_list = (ArrayList) $1;
1152 Parameter [] pars = new Parameter [pars_list.Count];
1153 pars_list.CopyTo (pars);
1155 $$ = new Parameters (pars);
1157 | fixed_parameters COMMA ARGLIST
1159 ArrayList pars_list = (ArrayList) $1;
1160 //pars_list.Add (new ArglistParameter (GetLocation ($3)));
1162 Parameter [] pars = new Parameter [pars_list.Count];
1163 pars_list.CopyTo (pars);
1165 $$ = new Parameters (pars, true);
1167 | parameter_array COMMA error
1170 Report.Error (231, ((Parameter) $1).Location, "A params parameter must be the last parameter in a formal parameter list");
1173 | ARGLIST COMMA error
1175 Report.Error (257, (Location) $1, "An __arglist parameter must be the last parameter in a formal parameter list");
1180 $$ = new Parameters (new Parameter[] { (Parameter) $1 } );
1184 $$ = new Parameters (new Parameter[0], true);
1191 ArrayList pars = new ArrayList (4);
1196 | fixed_parameters COMMA fixed_parameter
1198 ArrayList pars = (ArrayList) $1;
1207 opt_parameter_modifier
1211 LocatedToken lt = (LocatedToken) $4;
1212 $$ = new Parameter ((Expression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1215 opt_parameter_modifier
1217 IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1219 LocatedToken lt = (LocatedToken) $4;
1220 Report.Error (1552, lt.Location, "Array type specifier, [], must appear before parameter name");
1224 opt_parameter_modifier
1227 Report.Error (1001, GetLocation ($3), "Identifier expected");
1231 opt_parameter_modifier
1234 CheckIdentifierToken (yyToken, GetLocation ($4));
1238 opt_parameter_modifier
1244 LocatedToken lt = (LocatedToken) $4;
1245 Report.Error (241, lt.Location, "Default parameter specifiers are not permitted");
1250 opt_parameter_modifier
1251 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1252 | parameter_modifier
1256 : REF { $$ = Parameter.Modifier.REF; }
1257 | OUT { $$ = Parameter.Modifier.OUT; }
1261 : opt_attributes PARAMS type IDENTIFIER
1263 LocatedToken lt = (LocatedToken) $4;
1264 $$ = new ParamsParameter ((Expression) $3, lt.Value, (Attributes) $1, lt.Location);
1265 note ("type must be a single-dimension array type");
1267 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1269 Report.Error (1611, (Location) $2, "The params parameter cannot be declared as ref or out");
1272 | opt_attributes PARAMS type error {
1273 CheckIdentifierToken (yyToken, GetLocation ($4));
1278 property_declaration
1282 namespace_or_type_name
1284 if (RootContext.Documentation != null)
1285 tmpComment = Lexer.consume_doc_comment ();
1289 implicit_value_parameter_type = (Expression) $3;
1291 lexer.PropertyParsing = true;
1293 accessor_declarations
1295 lexer.PropertyParsing = false;
1296 has_get = has_set = false;
1304 Pair pair = (Pair) $8;
1305 Accessor get_block = (Accessor) pair.First;
1306 Accessor set_block = (Accessor) pair.Second;
1308 MemberName name = (MemberName) $4;
1310 prop = new Property (current_class, (Expression) $3, (int) $2,
1311 false, name, (Attributes) $1, get_block, set_block);
1313 current_container.AddProperty (prop);
1314 implicit_value_parameter_type = null;
1316 if (RootContext.Documentation != null)
1317 prop.DocComment = ConsumeStoredComment ();
1322 accessor_declarations
1323 : get_accessor_declaration
1325 $$ = new Pair ($1, null);
1327 | get_accessor_declaration accessor_declarations
1329 Pair pair = (Pair) $2;
1333 | set_accessor_declaration
1335 $$ = new Pair (null, $1);
1337 | set_accessor_declaration accessor_declarations
1339 Pair pair = (Pair) $2;
1345 Report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1350 get_accessor_declaration
1351 : opt_attributes opt_modifiers GET
1353 // If this is not the case, then current_local_parameters has already
1354 // been set in indexer_declaration
1355 if (parsing_indexer == false)
1356 current_local_parameters = null;
1358 current_local_parameters = indexer_parameters;
1359 lexer.PropertyParsing = false;
1361 iterator_container = SimpleIteratorContainer.GetSimple ();
1366 Report.Error (1007, (Location) $3, "Property accessor already defined");
1369 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1371 current_local_parameters = null;
1372 lexer.PropertyParsing = true;
1374 if (SimpleIteratorContainer.Simple.Yields)
1375 accessor.SetYields ();
1377 iterator_container = null;
1379 if (RootContext.Documentation != null)
1380 if (Lexer.doc_state == XmlCommentState.Error)
1381 Lexer.doc_state = XmlCommentState.NotAllowed;
1387 set_accessor_declaration
1388 : opt_attributes opt_modifiers SET
1391 Parameter implicit_value_parameter = new Parameter (
1392 implicit_value_parameter_type, "value",
1393 Parameter.Modifier.NONE, null, (Location) $3);
1395 if (parsing_indexer == false) {
1396 args = new Parameter [1];
1397 args [0] = implicit_value_parameter;
1398 current_local_parameters = new Parameters (args);
1400 Parameter [] fpars = indexer_parameters.FixedParameters;
1403 int count = fpars.Length;
1405 args = new Parameter [count + 1];
1406 fpars.CopyTo (args, 0);
1407 args [count] = implicit_value_parameter;
1410 current_local_parameters = new Parameters (
1414 lexer.PropertyParsing = false;
1416 iterator_container = SimpleIteratorContainer.GetSimple ();
1421 Report.Error (1007, ((LocatedToken) $3).Location, "Property accessor already defined");
1424 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1426 current_local_parameters = null;
1427 lexer.PropertyParsing = true;
1429 if (SimpleIteratorContainer.Simple.Yields)
1430 accessor.SetYields ();
1432 iterator_container = null;
1434 if (RootContext.Documentation != null
1435 && Lexer.doc_state == XmlCommentState.Error)
1436 Lexer.doc_state = XmlCommentState.NotAllowed;
1444 | SEMICOLON { $$ = null; }
1447 interface_declaration
1451 INTERFACE member_name
1453 MemberName name = MakeName ((MemberName) $5);
1455 current_class = new Interface (
1456 current_namespace, current_class, name, (int) $2,
1460 current_container = current_container.AddPartial (current_class);
1462 current_container.AddInterface (current_class);
1463 current_container = current_class;
1468 current_class.Bases = (ArrayList) $7;
1470 if (RootContext.Documentation != null) {
1471 current_container.DocComment = Lexer.consume_doc_comment ();
1472 Lexer.doc_state = XmlCommentState.Allowed;
1477 if (RootContext.Documentation != null)
1478 Lexer.doc_state = XmlCommentState.Allowed;
1482 $$ = pop_current_class ();
1484 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1485 CheckIdentifierToken (yyToken, GetLocation ($5));
1491 opt_interface_member_declarations
1495 opt_interface_member_declarations
1497 | interface_member_declarations
1500 interface_member_declarations
1501 : interface_member_declaration
1502 | interface_member_declarations interface_member_declaration
1505 interface_member_declaration
1506 : interface_method_declaration
1511 Method m = (Method) $1;
1513 if (m.IsExplicitImpl)
1514 Report.Error (541, m.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1515 m.GetSignatureForError ());
1517 current_container.AddMethod (m);
1519 if (RootContext.Documentation != null)
1520 Lexer.doc_state = XmlCommentState.Allowed;
1522 | interface_property_declaration
1527 Property p = (Property) $1;
1529 if (p.IsExplicitImpl)
1530 Report.Error (541, p.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1531 p.GetSignatureForError ());
1533 current_container.AddProperty (p);
1535 if (RootContext.Documentation != null)
1536 Lexer.doc_state = XmlCommentState.Allowed;
1538 | interface_event_declaration
1541 Event e = (Event) $1;
1543 if (e.IsExplicitImpl)
1544 Report.Error (541, e.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1545 e.GetSignatureForError ());
1547 current_container.AddEvent (e);
1550 if (RootContext.Documentation != null)
1551 Lexer.doc_state = XmlCommentState.Allowed;
1553 | interface_indexer_declaration
1558 Indexer i = (Indexer) $1;
1560 if (i.IsExplicitImpl)
1561 Report.Error (541, i.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1562 i.GetSignatureForError ());
1564 current_container.AddIndexer (i);
1566 if (RootContext.Documentation != null)
1567 Lexer.doc_state = XmlCommentState.Allowed;
1569 | delegate_declaration
1572 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1573 ((MemberCore)$1).GetSignatureForError ());
1579 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1580 ((MemberCore)$1).GetSignatureForError ());
1583 | struct_declaration
1586 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1587 ((MemberCore)$1).GetSignatureForError ());
1593 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1594 ((MemberCore)$1).GetSignatureForError ());
1597 | interface_declaration
1600 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1601 ((MemberCore)$1).GetSignatureForError ());
1604 | constant_declaration
1606 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1614 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, GetLocation ($1));
1619 interface_method_declaration
1620 : opt_attributes opt_new type namespace_or_type_name
1621 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1624 $$ = new Method (current_class, (Expression) $3, (int) $2, true,
1625 (MemberName) $4, (Parameters) $6, (Attributes) $1);
1626 if (RootContext.Documentation != null)
1627 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1629 | opt_attributes opt_new type namespace_or_type_name
1630 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1631 OPEN_BRACE opt_statement_list CLOSE_BRACE
1633 $$ = new Method (current_class, (Expression) $3, (int) $2, true,
1634 (MemberName) $4, (Parameters) $6, (Attributes) $1);
1636 Report.Error (531, lexer.Location, "`{0}': interface members cannot have a definition",
1637 ((Method)$$).GetSignatureForError ());
1641 interface_property_declaration
1646 { lexer.PropertyParsing = true; }
1647 accessor_declarations
1649 has_get = has_set = false;
1650 lexer.PropertyParsing = false;
1654 LocatedToken lt = (LocatedToken) $4;
1655 MemberName name = new MemberName (lt.Value, lt.Location);
1657 if ($3 == TypeManager.system_void_expr) {
1658 Report.Error (547, lt.Location, "`{0}': property or indexer cannot have void type", lt.Value);
1664 p = new Property (current_class, (Expression) $3, (int) $2, true,
1665 name, (Attributes) $1,
1668 Report.Error (548, p.Location, "`{0}': property or indexer must have at least one accessor", p.GetSignatureForError ());
1672 Pair pair = (Pair) $7;
1673 p = new Property (current_class, (Expression) $3, (int) $2, true,
1674 name, (Attributes) $1,
1675 (Accessor)pair.First, (Accessor)pair.Second);
1677 if (pair.First != null && ((Accessor)(pair.First)).Block != null) {
1678 Report.Error (531, p.Location, "`{0}.get': interface members cannot have a definition", p.GetSignatureForError ());
1683 if (pair.Second != null && ((Accessor)(pair.Second)).Block != null) {
1684 Report.Error (531, p.Location, "`{0}.set': interface members cannot have a definition", p.GetSignatureForError ());
1689 if (RootContext.Documentation != null)
1690 p.DocComment = Lexer.consume_doc_comment ();
1697 CheckIdentifierToken (yyToken, GetLocation ($4));
1703 interface_event_declaration
1704 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1706 LocatedToken lt = (LocatedToken) $5;
1707 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1708 new MemberName (lt.Value, lt.Location),
1710 if (RootContext.Documentation != null)
1711 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1713 | opt_attributes opt_new EVENT type error {
1714 CheckIdentifierToken (yyToken, GetLocation ($5));
1717 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1718 LocatedToken lt = (LocatedToken) $5;
1719 Report.Error (68, lt.Location, "`{0}.{1}': event in interface cannot have initializer", current_container.Name, lt.Value);
1722 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1724 lexer.EventParsing = true;
1726 event_accessor_declarations
1728 lexer.EventParsing = false;
1731 Report.Error (69, (Location) $3, "Event in interface cannot have add or remove accessors");
1736 interface_indexer_declaration
1737 : opt_attributes opt_new type THIS
1738 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1740 { lexer.PropertyParsing = true; }
1741 accessor_declarations
1743 has_get = has_set = false;
1744 lexer.PropertyParsing = false;
1750 i = new Indexer (current_class, (Expression) $3,
1751 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1752 (int) $2, true, (Parameters) $6, (Attributes) $1,
1755 Report.Error (548, i.Location, "`{0}': property or indexer must have at least one accessor", i.GetSignatureForError ());
1759 Pair pair = (Pair) $10;
1760 i = new Indexer (current_class, (Expression) $3,
1761 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1762 (int) $2, true, (Parameters) $6, (Attributes) $1,
1763 (Accessor)pair.First, (Accessor)pair.Second);
1765 if (pair.First != null && ((Accessor)(pair.First)).Block != null) {
1766 Report.Error (531, i.Location, "`{0}.get': interface members cannot have a definition", i.GetSignatureForError ());
1771 if (pair.Second != null && ((Accessor)(pair.Second)).Block != null) {
1772 Report.Error (531, i.Location, "`{0}.set': interface members cannot have a definition", i.GetSignatureForError ());
1777 if (RootContext.Documentation != null)
1778 i.DocComment = ConsumeStoredComment ();
1784 operator_declaration
1785 : opt_attributes opt_modifiers operator_declarator
1787 iterator_container = SimpleIteratorContainer.GetSimple ();
1794 OperatorDeclaration decl = (OperatorDeclaration) $3;
1796 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1798 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null, decl.location);
1799 if (decl.arg2type != null)
1800 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null, decl.location);
1802 Operator op = new Operator (
1803 current_class, decl.optype, decl.ret_type, (int) $2,
1804 new Parameters (param_list),
1805 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1807 if (RootContext.Documentation != null) {
1808 op.DocComment = tmpComment;
1809 Lexer.doc_state = XmlCommentState.Allowed;
1812 if (SimpleIteratorContainer.Simple.Yields)
1815 // Note again, checking is done in semantic analysis
1816 current_container.AddOperator (op);
1818 current_local_parameters = null;
1819 iterator_container = null;
1825 | SEMICOLON { $$ = null; }
1828 : type OPERATOR overloadable_operator
1829 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1831 LocatedToken lt = (LocatedToken) $6;
1832 Operator.OpType op = (Operator.OpType) $3;
1833 CheckUnaryOperator (op, lt.Location);
1835 if (op == Operator.OpType.Addition)
1836 op = Operator.OpType.UnaryPlus;
1838 if (op == Operator.OpType.Subtraction)
1839 op = Operator.OpType.UnaryNegation;
1841 Parameter [] pars = new Parameter [1];
1842 Expression type = (Expression) $5;
1844 pars [0] = new Parameter (type, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1846 current_local_parameters = new Parameters (pars);
1848 if (RootContext.Documentation != null) {
1849 tmpComment = Lexer.consume_doc_comment ();
1850 Lexer.doc_state = XmlCommentState.NotAllowed;
1853 $$ = new OperatorDeclaration (op, (Expression) $1, type, lt.Value,
1854 null, null, (Location) $2);
1856 | type OPERATOR overloadable_operator
1858 type IDENTIFIER COMMA
1862 LocatedToken ltParam1 = (LocatedToken) $6;
1863 LocatedToken ltParam2 = (LocatedToken) $9;
1864 CheckBinaryOperator ((Operator.OpType) $3, (Location) $2);
1866 Parameter [] pars = new Parameter [2];
1868 Expression typeL = (Expression) $5;
1869 Expression typeR = (Expression) $8;
1871 pars [0] = new Parameter (typeL, ltParam1.Value, Parameter.Modifier.NONE, null, ltParam1.Location);
1872 pars [1] = new Parameter (typeR, ltParam2.Value, Parameter.Modifier.NONE, null, ltParam2.Location);
1874 current_local_parameters = new Parameters (pars);
1876 if (RootContext.Documentation != null) {
1877 tmpComment = Lexer.consume_doc_comment ();
1878 Lexer.doc_state = XmlCommentState.NotAllowed;
1881 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1882 typeL, ltParam1.Value,
1883 typeR, ltParam2.Value, (Location) $2);
1885 | conversion_operator_declarator
1886 | type OPERATOR overloadable_operator
1888 type IDENTIFIER COMMA
1889 type IDENTIFIER COMMA
1893 Report.Error (1534, (Location) $2, "Overloaded binary operator `{0}' takes two parameters",
1894 Operator.GetName ((Operator.OpType) $3));
1897 | type OPERATOR overloadable_operator
1898 OPEN_PARENS CLOSE_PARENS
1900 Report.Error (1535, (Location) $2, "Overloaded unary operator `{0}' takes one parameter",
1901 Operator.GetName ((Operator.OpType) $3));
1906 overloadable_operator
1908 : BANG { $$ = Operator.OpType.LogicalNot; }
1909 | TILDE { $$ = Operator.OpType.OnesComplement; }
1910 | OP_INC { $$ = Operator.OpType.Increment; }
1911 | OP_DEC { $$ = Operator.OpType.Decrement; }
1912 | TRUE { $$ = Operator.OpType.True; }
1913 | FALSE { $$ = Operator.OpType.False; }
1914 // Unary and binary:
1915 | PLUS { $$ = Operator.OpType.Addition; }
1916 | MINUS { $$ = Operator.OpType.Subtraction; }
1918 | STAR { $$ = Operator.OpType.Multiply; }
1919 | DIV { $$ = Operator.OpType.Division; }
1920 | PERCENT { $$ = Operator.OpType.Modulus; }
1921 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1922 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1923 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1924 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1925 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1926 | OP_EQ { $$ = Operator.OpType.Equality; }
1927 | OP_NE { $$ = Operator.OpType.Inequality; }
1928 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1929 | OP_LT { $$ = Operator.OpType.LessThan; }
1930 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1931 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1934 conversion_operator_declarator
1935 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1937 LocatedToken lt = (LocatedToken) $6;
1938 Parameter [] pars = new Parameter [1];
1940 pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1942 current_local_parameters = new Parameters (pars);
1944 if (RootContext.Documentation != null) {
1945 tmpComment = Lexer.consume_doc_comment ();
1946 Lexer.doc_state = XmlCommentState.NotAllowed;
1949 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, lt.Value,
1950 null, null, (Location) $2);
1952 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1954 LocatedToken lt = (LocatedToken) $6;
1955 Parameter [] pars = new Parameter [1];
1957 pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1959 current_local_parameters = new Parameters (pars);
1961 if (RootContext.Documentation != null) {
1962 tmpComment = Lexer.consume_doc_comment ();
1963 Lexer.doc_state = XmlCommentState.NotAllowed;
1966 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, lt.Value,
1967 null, null, (Location) $2);
1971 syntax_error ((Location) $1, "'operator' expected");
1975 syntax_error ((Location) $1, "'operator' expected");
1979 constructor_declaration
1982 constructor_declarator
1985 Constructor c = (Constructor) $3;
1986 c.Block = (ToplevelBlock) $4;
1987 c.OptAttributes = (Attributes) $1;
1988 c.ModFlags = (int) $2;
1990 if (RootContext.Documentation != null)
1991 c.DocComment = ConsumeStoredComment ();
1993 if (c.Name == current_container.Basename){
1994 if ((c.ModFlags & Modifiers.STATIC) != 0){
1995 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1996 Report.Error (515, c.Location,
1997 "`{0}': access modifiers are not allowed on static constructors",
1998 c.GetSignatureForError ());
2001 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2003 if (c.Initializer != null){
2004 Report.Error (514, c.Location,
2005 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2006 c.GetSignatureForError ());
2009 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2012 // We let another layer check the validity of the constructor.
2013 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
2016 current_container.AddConstructor (c);
2018 current_local_parameters = null;
2019 if (RootContext.Documentation != null)
2020 Lexer.doc_state = XmlCommentState.Allowed;
2024 constructor_declarator
2027 if (RootContext.Documentation != null) {
2028 tmpComment = Lexer.consume_doc_comment ();
2029 Lexer.doc_state = XmlCommentState.Allowed;
2032 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2034 current_local_parameters = (Parameters) $4;
2036 opt_constructor_initializer
2038 LocatedToken lt = (LocatedToken) $1;
2039 $$ = new Constructor (current_class, lt.Value, 0, (Parameters) $4,
2040 (ConstructorInitializer) $7, lt.Location);
2046 | SEMICOLON { $$ = null; }
2049 opt_constructor_initializer
2050 : /* empty */ { $$ = null; }
2051 | constructor_initializer
2054 constructor_initializer
2055 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
2057 $$ = new ConstructorBaseInitializer ((ArrayList) $4, (Location) $2);
2059 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
2061 $$ = new ConstructorThisInitializer ((ArrayList) $4, (Location) $2);
2064 Report.Error (1018, (Location) $1, "Keyword this or base expected");
2070 : /* EMPTY */ { $$ = 0; }
2071 | UNSAFE { $$ = Modifiers.UNSAFE; }
2072 | EXTERN { $$ = Modifiers.EXTERN; }
2075 destructor_declaration
2076 : opt_attributes opt_finalizer TILDE
2078 if (RootContext.Documentation != null) {
2079 tmpComment = Lexer.consume_doc_comment ();
2080 Lexer.doc_state = XmlCommentState.NotAllowed;
2083 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2085 LocatedToken lt = (LocatedToken) $5;
2086 if (lt.Value != current_container.Basename){
2087 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2088 } else if (current_container.Kind != Kind.Class){
2089 Report.Error (575, lt.Location, "Only class types can contain destructor");
2091 Location l = lt.Location;
2094 if (!RootContext.StdLib && current_container.Name == "System.Object")
2095 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2097 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2099 Method d = new Destructor (
2100 current_class, TypeManager.system_void_expr, m, "Finalize",
2101 Parameters.EmptyReadOnlyParameters, (Attributes) $1, l);
2102 if (RootContext.Documentation != null)
2103 d.DocComment = ConsumeStoredComment ();
2105 d.Block = (ToplevelBlock) $8;
2106 current_container.AddMethod (d);
2114 EVENT type variable_declarators SEMICOLON
2116 current_array_type = null;
2117 foreach (VariableDeclaration var in (ArrayList) $5) {
2119 MemberName name = new MemberName (var.identifier,
2122 EventField e = new EventField (
2123 current_class, (Expression) $4, (int) $2, false, name,
2126 e.Initializer = var.expression_or_array_initializer;
2128 current_container.AddEvent (e);
2130 if (RootContext.Documentation != null) {
2131 e.DocComment = Lexer.consume_doc_comment ();
2132 Lexer.doc_state = XmlCommentState.Allowed;
2138 EVENT type namespace_or_type_name
2141 implicit_value_parameter_type = (Expression) $4;
2142 lexer.EventParsing = true;
2144 event_accessor_declarations
2146 lexer.EventParsing = false;
2150 MemberName name = (MemberName) $5;
2153 Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2154 current_container.Name, name.ToString ());
2157 Pair pair = (Pair) $8;
2158 if (pair.First == null || pair.Second == null)
2159 // CS0073 is already reported, so no CS0065 here.
2162 Event e = new EventProperty (
2163 current_class, (Expression) $4, (int) $2, false, name,
2164 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second);
2165 if (RootContext.Documentation != null) {
2166 e.DocComment = Lexer.consume_doc_comment ();
2167 Lexer.doc_state = XmlCommentState.Allowed;
2170 current_container.AddEvent (e);
2171 implicit_value_parameter_type = null;
2175 | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
2176 MemberName mn = (MemberName) $5;
2178 if (mn.Left != null)
2179 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2181 Report.Error (71, mn.Location, "Event declaration should use property syntax");
2183 if (RootContext.Documentation != null)
2184 Lexer.doc_state = XmlCommentState.Allowed;
2188 event_accessor_declarations
2189 : add_accessor_declaration remove_accessor_declaration
2191 $$ = new Pair ($1, $2);
2193 | remove_accessor_declaration add_accessor_declaration
2195 $$ = new Pair ($2, $1);
2197 | add_accessor_declaration { $$ = null; }
2198 | remove_accessor_declaration { $$ = null; }
2201 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2207 add_accessor_declaration
2208 : opt_attributes ADD
2210 Parameter [] args = new Parameter [1];
2211 Parameter implicit_value_parameter = new Parameter (
2212 implicit_value_parameter_type, "value",
2213 Parameter.Modifier.NONE, null, (Location) $2);
2215 args [0] = implicit_value_parameter;
2217 current_local_parameters = new Parameters (args);
2218 lexer.EventParsing = false;
2222 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2223 lexer.EventParsing = true;
2225 | opt_attributes ADD error {
2226 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2229 | opt_attributes modifiers ADD {
2230 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2235 remove_accessor_declaration
2236 : opt_attributes REMOVE
2238 Parameter [] args = new Parameter [1];
2239 Parameter implicit_value_parameter = new Parameter (
2240 implicit_value_parameter_type, "value",
2241 Parameter.Modifier.NONE, null, (Location) $2);
2243 args [0] = implicit_value_parameter;
2245 current_local_parameters = new Parameters (args);
2246 lexer.EventParsing = false;
2250 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2251 lexer.EventParsing = true;
2253 | opt_attributes REMOVE error {
2254 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2257 | opt_attributes modifiers REMOVE {
2258 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2264 : opt_attributes opt_modifiers indexer_declarator
2267 IndexerDeclaration decl = (IndexerDeclaration) $3;
2269 implicit_value_parameter_type = decl.type;
2271 lexer.PropertyParsing = true;
2272 parsing_indexer = true;
2274 indexer_parameters = decl.param_list;
2276 accessor_declarations
2278 lexer.PropertyParsing = false;
2279 has_get = has_set = false;
2280 parsing_indexer = false;
2287 // The signature is computed from the signature of the indexer. Look
2288 // at section 3.6 on the spec
2290 IndexerDeclaration decl = (IndexerDeclaration) $3;
2291 Location loc = decl.location;
2292 Pair pair = (Pair) $6;
2293 Accessor get_block = (Accessor) pair.First;
2294 Accessor set_block = (Accessor) pair.Second;
2297 if (decl.interface_type != null)
2298 name = new MemberName (decl.interface_type, TypeContainer.DefaultIndexerName, loc);
2300 name = new MemberName (TypeContainer.DefaultIndexerName, loc);
2302 indexer = new Indexer (current_class, decl.type, name,
2303 (int) $2, false, decl.param_list, (Attributes) $1,
2304 get_block, set_block);
2306 if (RootContext.Documentation != null)
2307 indexer.DocComment = ConsumeStoredComment ();
2309 current_container.AddIndexer (indexer);
2311 current_local_parameters = null;
2312 implicit_value_parameter_type = null;
2313 indexer_parameters = null;
2318 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2320 Parameters pars = (Parameters) $4;
2321 if (pars.HasArglist) {
2322 // "__arglist is not valid in this context"
2323 Report.Error (1669, (Location) $2, "__arglist is not valid in this context");
2324 } else if (pars.Empty){
2325 Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
2327 if (RootContext.Documentation != null) {
2328 tmpComment = Lexer.consume_doc_comment ();
2329 Lexer.doc_state = XmlCommentState.Allowed;
2332 $$ = new IndexerDeclaration ((Expression) $1, null, pars, (Location) $2);
2334 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2336 Parameters pars = (Parameters) $6;
2338 if (pars.HasArglist) {
2339 // "__arglist is not valid in this context"
2340 Report.Error (1669, (Location) $4, "__arglist is not valid in this context");
2341 } else if (pars.Empty){
2342 Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
2345 MemberName name = (MemberName) $2;
2346 $$ = new IndexerDeclaration ((Expression) $1, name, pars, (Location) $4);
2348 if (RootContext.Documentation != null) {
2349 tmpComment = Lexer.consume_doc_comment ();
2350 Lexer.doc_state = XmlCommentState.Allowed;
2361 if (RootContext.Documentation != null)
2362 enumTypeComment = Lexer.consume_doc_comment ();
2367 LocatedToken lt = (LocatedToken) $5;
2368 Location enum_location = lt.Location;
2371 Report.Error (267, lt.Location, "The partial modifier can only appear immediately before `class', `struct' or `interface'");
2372 break; // assumes that the parser put us in a switch
2375 MemberName name = MakeName (new MemberName (lt.Value, enum_location));
2376 Enum e = new Enum (current_namespace, current_class, (Expression) $6, (int) $2,
2377 name, (Attributes) $1);
2379 if (RootContext.Documentation != null)
2380 e.DocComment = enumTypeComment;
2383 EnumMember em = null;
2384 foreach (VariableDeclaration ev in (ArrayList) $8) {
2385 em = new EnumMember (e, em, (Expression) ev.expression_or_array_initializer,
2386 new MemberName (ev.identifier, ev.Location), ev.OptAttributes);
2388 // if (RootContext.Documentation != null)
2389 em.DocComment = ev.DocComment;
2391 e.AddEnumMember (em);
2394 current_container.AddEnum (e);
2395 RootContext.Tree.RecordDecl (current_namespace.NS, name, e);
2402 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2403 | COLON type { $$ = $2; }
2409 if (RootContext.Documentation != null)
2410 Lexer.doc_state = XmlCommentState.Allowed;
2412 opt_enum_member_declarations
2414 // here will be evaluated after CLOSE_BLACE is consumed.
2415 if (RootContext.Documentation != null)
2416 Lexer.doc_state = XmlCommentState.Allowed;
2424 opt_enum_member_declarations
2425 : /* empty */ { $$ = new ArrayList (4); }
2426 | enum_member_declarations opt_comma { $$ = $1; }
2429 enum_member_declarations
2430 : enum_member_declaration
2432 ArrayList l = new ArrayList (4);
2437 | enum_member_declarations COMMA enum_member_declaration
2439 ArrayList l = (ArrayList) $1;
2447 enum_member_declaration
2448 : opt_attributes IDENTIFIER
2450 VariableDeclaration vd = new VariableDeclaration (
2451 (LocatedToken) $2, null, (Attributes) $1);
2453 if (RootContext.Documentation != null) {
2454 vd.DocComment = Lexer.consume_doc_comment ();
2455 Lexer.doc_state = XmlCommentState.Allowed;
2460 | opt_attributes IDENTIFIER
2462 if (RootContext.Documentation != null) {
2463 tmpComment = Lexer.consume_doc_comment ();
2464 Lexer.doc_state = XmlCommentState.NotAllowed;
2469 VariableDeclaration vd = new VariableDeclaration (
2470 (LocatedToken) $2, $5, (Attributes) $1);
2472 if (RootContext.Documentation != null)
2473 vd.DocComment = ConsumeStoredComment ();
2479 delegate_declaration
2482 DELEGATE type member_name
2483 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2486 MemberName name = MakeName ((MemberName) $5);
2487 Parameters p = (Parameters) $7;
2489 // TODO: wrong location
2490 Report.Error (1669, name.Location, "__arglist is not valid in this context");
2493 Delegate del = new Delegate (current_namespace, current_class, (Expression) $4,
2494 (int) $2, name, p, (Attributes) $1);
2496 if (RootContext.Documentation != null) {
2497 del.DocComment = Lexer.consume_doc_comment ();
2498 Lexer.doc_state = XmlCommentState.Allowed;
2501 current_container.AddDelegate (del);
2502 RootContext.Tree.RecordDecl (current_namespace.NS, name, del);
2507 namespace_or_type_name
2509 | IDENTIFIER DOUBLE_COLON IDENTIFIER {
2510 LocatedToken lt1 = (LocatedToken) $1;
2511 LocatedToken lt2 = (LocatedToken) $3;
2512 $$ = new MemberName (lt1.Value, lt2.Value, lt2.Location);
2514 | namespace_or_type_name DOT IDENTIFIER {
2515 LocatedToken lt = (LocatedToken) $3;
2516 $$ = new MemberName ((MemberName) $1, lt.Value);
2522 LocatedToken lt = (LocatedToken) $1;
2523 $$ = new MemberName (lt.Value, lt.Location);
2528 * Before you think of adding a return_type, notice that we have been
2529 * using two rules in the places where it matters (one rule using type
2530 * and another identical one that uses VOID as the return type). This
2531 * gets rid of a shift/reduce couple
2534 : namespace_or_type_name
2536 MemberName name = (MemberName) $1;
2537 $$ = name.GetTypeExpression ();
2549 // Note that here only unmanaged types are allowed but we
2550 // can't perform checks during this phase - we do it during
2551 // semantic analysis.
2553 $$ = new ComposedCast ((Expression) $1, "*", Lexer.Location);
2557 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2563 | non_expression_type rank_specifier
2565 Location loc = GetLocation ($1);
2567 loc = lexer.Location;
2568 $$ = new ComposedCast ((Expression) $1, (string) $2, loc);
2570 | non_expression_type STAR
2572 Location loc = GetLocation ($1);
2574 loc = lexer.Location;
2575 $$ = new ComposedCast ((Expression) $1, "*", loc);
2577 | expression rank_specifiers
2579 $$ = new ComposedCast ((Expression) $1, (string) $2);
2583 $$ = new ComposedCast ((Expression) $1, "*");
2587 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2588 // through this different path
2590 | multiplicative_expression STAR
2592 $$ = new ComposedCast ((Expression) $1, "*");
2599 ArrayList types = new ArrayList (4);
2604 | type_list COMMA type
2606 ArrayList types = (ArrayList) $1;
2614 * replaces all the productions for isolating the various
2615 * simple types, but we need this to reuse it easily in local_variable_type
2618 : OBJECT { $$ = TypeManager.system_object_expr; }
2619 | STRING { $$ = TypeManager.system_string_expr; }
2620 | BOOL { $$ = TypeManager.system_boolean_expr; }
2621 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2622 | FLOAT { $$ = TypeManager.system_single_expr; }
2623 | DOUBLE { $$ = TypeManager.system_double_expr; }
2628 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2629 | BYTE { $$ = TypeManager.system_byte_expr; }
2630 | SHORT { $$ = TypeManager.system_int16_expr; }
2631 | USHORT { $$ = TypeManager.system_uint16_expr; }
2632 | INT { $$ = TypeManager.system_int32_expr; }
2633 | UINT { $$ = TypeManager.system_uint32_expr; }
2634 | LONG { $$ = TypeManager.system_int64_expr; }
2635 | ULONG { $$ = TypeManager.system_uint64_expr; }
2636 | CHAR { $$ = TypeManager.system_char_expr; }
2637 | VOID { $$ = TypeManager.system_void_expr; }
2641 : type rank_specifiers
2643 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2);
2648 // Expressions, section 7.5
2658 MemberName mn = (MemberName) $1;
2659 $$ = mn.GetTypeExpression ();
2661 | IDENTIFIER DOUBLE_COLON IDENTIFIER
2663 LocatedToken lt1 = (LocatedToken) $1;
2664 LocatedToken lt2 = (LocatedToken) $3;
2665 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, lt2.Location);
2667 | parenthesized_expression
2669 | invocation_expression
2673 | post_increment_expression
2674 | post_decrement_expression
2678 | checked_expression
2679 | unchecked_expression
2680 | pointer_member_access
2681 | anonymous_method_expression
2688 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
2689 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); }
2690 | NULL { $$ = new NullLiteral (lexer.Location); }
2694 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
2695 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
2696 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
2701 object v = lexer.Value;
2704 $$ = new IntLiteral ((int) v, lexer.Location);
2705 } else if (v is uint)
2706 $$ = new UIntLiteral ((UInt32) v, lexer.Location);
2708 $$ = new LongLiteral ((Int64) v, lexer.Location);
2709 else if (v is ulong)
2710 $$ = new ULongLiteral ((UInt64) v, lexer.Location);
2712 Console.WriteLine ("OOPS. Unexpected result from scanner");
2717 : TRUE { $$ = new BoolLiteral (true, lexer.Location); }
2718 | FALSE { $$ = new BoolLiteral (false, lexer.Location); }
2721 parenthesized_expression_0
2722 : OPEN_PARENS expression CLOSE_PARENS
2725 lexer.Deambiguate_CloseParens ();
2726 // After this, the next token returned is one of
2727 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2728 // or CLOSE_PARENS_MINUS.
2730 | OPEN_PARENS expression error { CheckToken (1026, yyToken, "Expecting ')'", lexer.Location); }
2733 parenthesized_expression
2734 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2738 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2740 // If a parenthesized expression is followed by a minus, we need to wrap
2741 // the expression inside a ParenthesizedExpression for the CS0075 check
2742 // in Binary.DoResolve().
2743 $$ = new ParenthesizedExpression ((Expression) $1);
2748 : primary_expression DOT IDENTIFIER
2750 LocatedToken lt = (LocatedToken) $3;
2751 $$ = new MemberAccess ((Expression) $1, lt.Value);
2753 | predefined_type DOT IDENTIFIER
2755 LocatedToken lt = (LocatedToken) $3;
2756 $$ = new MemberAccess ((Expression) $1, lt.Value);
2764 invocation_expression
2765 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2768 Report.Error (1, (Location) $2, "Parse error");
2770 $$ = new Invocation ((Expression) $1, (ArrayList) $3);
2772 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2774 $$ = new Invocation ((Expression) $1, new ArrayList ());
2776 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2778 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
2780 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2782 ArrayList args = new ArrayList (1);
2784 $$ = new Invocation ((Expression) $1, args);
2786 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2788 ArrayList args = ((ArrayList) $4);
2790 $$ = new Invocation ((Expression) $1, args);
2795 : /* empty */ { $$ = null; }
2802 ArrayList list = new ArrayList (4);
2806 | argument_list COMMA argument
2808 ArrayList list = (ArrayList) $1;
2812 | argument_list error {
2813 CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
2821 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2823 | non_simple_argument
2830 : REF variable_reference
2832 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2834 | OUT variable_reference
2836 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2838 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2840 ArrayList list = (ArrayList) $3;
2841 Argument[] args = new Argument [list.Count];
2842 list.CopyTo (args, 0);
2844 Expression expr = new Arglist (args, (Location) $1);
2845 $$ = new Argument (expr, Argument.AType.Expression);
2849 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
2854 : expression { note ("section 5.4"); $$ = $1; }
2858 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2860 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
2862 | primary_expression rank_specifiers
2864 // So the super-trick is that primary_expression
2865 // can only be either a SimpleName or a MemberAccess.
2866 // The MemberAccess case arises when you have a fully qualified type-name like :
2868 // SimpleName is when you have
2871 Expression expr = (Expression) $1;
2872 if (expr is ComposedCast){
2873 $$ = new ComposedCast (expr, (string) $2);
2874 } else if (!(expr is SimpleName || expr is MemberAccess || expr is QualifiedAliasMember)){
2875 Error_ExpectingTypeName (expr);
2876 $$ = TypeManager.system_object_expr;
2879 // So we extract the string corresponding to the SimpleName
2882 $$ = new ComposedCast (expr, (string) $2);
2884 current_array_type = (Expression)$$;
2891 ArrayList list = new ArrayList (4);
2895 | expression_list COMMA expression
2897 ArrayList list = (ArrayList) $1;
2906 $$ = new This (current_block, (Location) $1);
2911 : BASE DOT IDENTIFIER
2913 LocatedToken lt = (LocatedToken) $3;
2914 $$ = new BaseAccess (lt.Value, lt.Location);
2916 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2918 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
2921 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
2926 post_increment_expression
2927 : primary_expression OP_INC
2929 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2930 (Expression) $1, (Location) $2);
2934 post_decrement_expression
2935 : primary_expression OP_DEC
2937 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2938 (Expression) $1, (Location) $2);
2943 : object_or_delegate_creation_expression
2944 | array_creation_expression
2947 object_or_delegate_creation_expression
2948 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2950 $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
2954 array_creation_expression
2955 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2957 opt_array_initializer
2959 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
2961 | NEW type rank_specifiers array_initializer
2963 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, (Location) $1);
2967 Report.Error (1031, (Location) $1, "Type expected");
2972 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
2989 : rank_specifier opt_rank_specifier
2991 $$ = (string) $2 + (string) $1;
2996 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2998 $$ = "[" + (string) $2 + "]";
3018 | dim_separators COMMA
3020 $$ = (string) $1 + ",";
3024 opt_array_initializer
3036 : OPEN_BRACE CLOSE_BRACE
3038 ArrayList list = new ArrayList (4);
3041 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3043 $$ = (ArrayList) $2;
3047 variable_initializer_list
3048 : variable_initializer
3050 ArrayList list = new ArrayList (4);
3054 | variable_initializer_list COMMA variable_initializer
3056 ArrayList list = (ArrayList) $1;
3063 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
3065 $$ = new TypeOfVoid ((Location) $1);
3067 | TYPEOF OPEN_PARENS type CLOSE_PARENS
3069 $$ = new TypeOf ((Expression) $3, (Location) $1);
3074 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3075 $$ = new SizeOf ((Expression) $3, (Location) $1);
3080 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3082 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3086 unchecked_expression
3087 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3089 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3093 pointer_member_access
3094 : primary_expression OP_PTR IDENTIFIER
3097 LocatedToken lt = (LocatedToken) $3;
3099 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
3100 $$ = new MemberAccess (deref, lt.Value);
3104 anonymous_method_expression
3105 : DELEGATE opt_anonymous_method_signature
3107 if (oob_stack == null)
3108 oob_stack = new Stack (6);
3110 oob_stack.Push (current_local_parameters);
3111 current_local_parameters = (Parameters)$2;
3113 // Force the next block to be created as a ToplevelBlock
3114 oob_stack.Push (current_block);
3115 oob_stack.Push (top_current_block);
3116 current_block = null;
3120 Location loc = (Location) $1;
3121 top_current_block = (Block) oob_stack.Pop ();
3122 current_block = (Block) oob_stack.Pop ();
3123 if (RootContext.Version == LanguageVersion.ISO_1){
3124 Report.FeatureIsNotStandardized (loc, "anonymous methods");
3127 ToplevelBlock anon_block = (ToplevelBlock) $4;
3129 anon_block.Parent = current_block;
3130 $$ = new AnonymousMethod (current_container, (Parameters) $2, (ToplevelBlock) top_current_block,
3133 current_local_parameters = (Parameters) oob_stack.Pop ();
3137 opt_anonymous_method_signature
3138 : /* empty */ { $$ = null; }
3139 | anonymous_method_signature
3142 anonymous_method_signature
3143 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3146 $$ = Parameters.EmptyReadOnlyParameters;
3148 ArrayList par_list = (ArrayList) $2;
3149 Parameter [] pars = new Parameter [par_list.Count];
3150 par_list.CopyTo (pars);
3151 $$ = new Parameters (pars);
3156 opt_anonymous_method_parameter_list
3157 : /* empty */ { $$ = null; }
3158 | anonymous_method_parameter_list { $$ = $1; }
3161 anonymous_method_parameter_list
3162 : anonymous_method_parameter
3164 ArrayList a = new ArrayList (4);
3168 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3170 ArrayList a = (ArrayList) $1;
3176 anonymous_method_parameter
3177 : opt_parameter_modifier type IDENTIFIER {
3178 LocatedToken lt = (LocatedToken) $3;
3179 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3181 | PARAMS type IDENTIFIER {
3182 Report.Error (1670, ((LocatedToken) $3).Location, "The `params' modifier is not allowed in anonymous method declaration");
3188 : primary_expression
3189 | BANG prefixed_unary_expression
3191 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3193 | TILDE prefixed_unary_expression
3195 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3201 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3203 $$ = new Cast ((Expression) $1, (Expression) $3);
3205 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3207 $$ = new Cast ((Expression) $1, (Expression) $3);
3213 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3215 // TODO: wrong location
3216 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3221 // The idea to split this out is from Rhys' grammar
3222 // to solve the problem with casts.
3224 prefixed_unary_expression
3226 | PLUS prefixed_unary_expression
3228 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3230 | MINUS prefixed_unary_expression
3232 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3234 | OP_INC prefixed_unary_expression
3236 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3237 (Expression) $2, (Location) $1);
3239 | OP_DEC prefixed_unary_expression
3241 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3242 (Expression) $2, (Location) $1);
3244 | STAR prefixed_unary_expression
3246 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, (Location) $1);
3248 | BITWISE_AND prefixed_unary_expression
3250 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3254 pre_increment_expression
3255 : OP_INC prefixed_unary_expression
3257 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3258 (Expression) $2, (Location) $1);
3262 pre_decrement_expression
3263 : OP_DEC prefixed_unary_expression
3265 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3266 (Expression) $2, (Location) $1);
3270 multiplicative_expression
3271 : prefixed_unary_expression
3272 | multiplicative_expression STAR prefixed_unary_expression
3274 $$ = new Binary (Binary.Operator.Multiply,
3275 (Expression) $1, (Expression) $3);
3277 | multiplicative_expression DIV prefixed_unary_expression
3279 $$ = new Binary (Binary.Operator.Division,
3280 (Expression) $1, (Expression) $3);
3282 | multiplicative_expression PERCENT prefixed_unary_expression
3284 $$ = new Binary (Binary.Operator.Modulus,
3285 (Expression) $1, (Expression) $3);
3290 : multiplicative_expression
3291 | additive_expression PLUS multiplicative_expression
3293 $$ = new Binary (Binary.Operator.Addition,
3294 (Expression) $1, (Expression) $3);
3296 | additive_expression MINUS multiplicative_expression
3298 $$ = new Binary (Binary.Operator.Subtraction,
3299 (Expression) $1, (Expression) $3);
3304 : additive_expression
3305 | shift_expression OP_SHIFT_LEFT additive_expression
3307 $$ = new Binary (Binary.Operator.LeftShift,
3308 (Expression) $1, (Expression) $3);
3310 | shift_expression OP_SHIFT_RIGHT additive_expression
3312 $$ = new Binary (Binary.Operator.RightShift,
3313 (Expression) $1, (Expression) $3);
3317 relational_expression
3319 | relational_expression OP_LT shift_expression
3321 $$ = new Binary (Binary.Operator.LessThan,
3322 (Expression) $1, (Expression) $3);
3324 | relational_expression OP_GT shift_expression
3326 $$ = new Binary (Binary.Operator.GreaterThan,
3327 (Expression) $1, (Expression) $3);
3329 | relational_expression OP_LE shift_expression
3331 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3332 (Expression) $1, (Expression) $3);
3334 | relational_expression OP_GE shift_expression
3336 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3337 (Expression) $1, (Expression) $3);
3339 | relational_expression IS type
3341 $$ = new Is ((Expression) $1, (Expression) $3, (Location) $2);
3343 | relational_expression AS type
3345 $$ = new As ((Expression) $1, (Expression) $3, (Location) $2);
3350 : relational_expression
3351 | equality_expression OP_EQ relational_expression
3353 $$ = new Binary (Binary.Operator.Equality,
3354 (Expression) $1, (Expression) $3);
3356 | equality_expression OP_NE relational_expression
3358 $$ = new Binary (Binary.Operator.Inequality,
3359 (Expression) $1, (Expression) $3);
3364 : equality_expression
3365 | and_expression BITWISE_AND equality_expression
3367 $$ = new Binary (Binary.Operator.BitwiseAnd,
3368 (Expression) $1, (Expression) $3);
3372 exclusive_or_expression
3374 | exclusive_or_expression CARRET and_expression
3376 $$ = new Binary (Binary.Operator.ExclusiveOr,
3377 (Expression) $1, (Expression) $3);
3381 inclusive_or_expression
3382 : exclusive_or_expression
3383 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3385 $$ = new Binary (Binary.Operator.BitwiseOr,
3386 (Expression) $1, (Expression) $3);
3390 conditional_and_expression
3391 : inclusive_or_expression
3392 | conditional_and_expression OP_AND inclusive_or_expression
3394 $$ = new Binary (Binary.Operator.LogicalAnd,
3395 (Expression) $1, (Expression) $3);
3399 conditional_or_expression
3400 : conditional_and_expression
3401 | conditional_or_expression OP_OR conditional_and_expression
3403 $$ = new Binary (Binary.Operator.LogicalOr,
3404 (Expression) $1, (Expression) $3);
3408 conditional_expression
3409 : conditional_or_expression
3410 | conditional_or_expression INTERR expression COLON expression
3412 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
3416 assignment_expression
3417 : prefixed_unary_expression ASSIGN expression
3419 $$ = new Assign ((Expression) $1, (Expression) $3);
3421 | prefixed_unary_expression OP_MULT_ASSIGN expression
3423 $$ = new CompoundAssign (
3424 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
3426 | prefixed_unary_expression OP_DIV_ASSIGN expression
3428 $$ = new CompoundAssign (
3429 Binary.Operator.Division, (Expression) $1, (Expression) $3);
3431 | prefixed_unary_expression OP_MOD_ASSIGN expression
3433 $$ = new CompoundAssign (
3434 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
3436 | prefixed_unary_expression OP_ADD_ASSIGN expression
3438 $$ = new CompoundAssign (
3439 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
3441 | prefixed_unary_expression OP_SUB_ASSIGN expression
3443 $$ = new CompoundAssign (
3444 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3446 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3448 $$ = new CompoundAssign (
3449 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
3451 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3453 $$ = new CompoundAssign (
3454 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
3456 | prefixed_unary_expression OP_AND_ASSIGN expression
3458 $$ = new CompoundAssign (
3459 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
3461 | prefixed_unary_expression OP_OR_ASSIGN expression
3463 $$ = new CompoundAssign (
3464 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
3466 | prefixed_unary_expression OP_XOR_ASSIGN expression
3468 $$ = new CompoundAssign (
3469 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
3474 : conditional_expression
3475 | assignment_expression
3495 MemberName name = MakeName ((MemberName) $5);
3496 int mod_flags = (int) $2;
3498 current_class = new Class (
3499 current_namespace, current_class, name,
3500 mod_flags, (Attributes) $1);
3503 current_container = current_container.AddPartial (current_class);
3505 current_container.AddClassOrStruct (current_class);
3506 current_container = current_class;
3512 if (current_class.Name == "System.Object") {
3513 Report.Error (537, current_class.Location,
3514 "The class System.Object cannot have a base " +
3515 "class or implement an interface.");
3517 current_class.Bases = (ArrayList) $7;
3520 if (RootContext.Documentation != null) {
3521 current_container.DocComment = Lexer.consume_doc_comment ();
3522 Lexer.doc_state = XmlCommentState.Allowed;
3527 if (RootContext.Documentation != null)
3528 Lexer.doc_state = XmlCommentState.Allowed;
3532 $$ = pop_current_class ();
3540 { $$ = $1; } // location
3544 : /* empty */ { $$ = (int) 0; }
3550 | modifiers modifier
3555 if ((m1 & m2) != 0) {
3556 Location l = lexer.Location;
3557 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
3559 $$ = (int) (m1 | m2);
3564 : NEW { $$ = Modifiers.NEW; }
3565 | PUBLIC { $$ = Modifiers.PUBLIC; }
3566 | PROTECTED { $$ = Modifiers.PROTECTED; }
3567 | INTERNAL { $$ = Modifiers.INTERNAL; }
3568 | PRIVATE { $$ = Modifiers.PRIVATE; }
3569 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3570 | SEALED { $$ = Modifiers.SEALED; }
3571 | STATIC { $$ = Modifiers.STATIC; }
3572 | READONLY { $$ = Modifiers.READONLY; }
3573 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3574 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3575 | EXTERN { $$ = Modifiers.EXTERN; }
3576 | VOLATILE { $$ = Modifiers.VOLATILE; }
3577 | UNSAFE { $$ = Modifiers.UNSAFE; }
3581 : /* empty */ { $$ = null; }
3582 | class_base { $$ = $1; }
3586 : COLON type_list { $$ = $2; }
3594 // A block is "contained" on the following places:
3596 // property_declaration as part of the accessor body (get/set)
3597 // operator_declaration
3598 // constructor_declaration
3599 // destructor_declaration
3600 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3605 if (current_block == null){
3606 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, (Location) $1);
3607 top_current_block = current_block;
3609 current_block = new Block (current_block, (Location) $1, Location.Null);
3612 opt_statement_list CLOSE_BRACE
3614 while (current_block.Implicit)
3615 current_block = current_block.Parent;
3617 current_block.SetEndLocation ((Location) $4);
3618 current_block = current_block.Parent;
3619 if (current_block == null)
3620 top_current_block = null;
3631 | statement_list statement
3635 : declaration_statement
3637 if ($1 != null && (Block) $1 != current_block){
3638 current_block.AddStatement ((Statement) $1);
3639 current_block = (Block) $1;
3642 | valid_declaration_statement
3644 current_block.AddStatement ((Statement) $1);
3649 valid_declaration_statement
3652 | expression_statement
3653 | selection_statement
3654 | iteration_statement
3658 | unchecked_statement
3666 : valid_declaration_statement
3667 | declaration_statement
3669 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3674 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3682 $$ = EmptyStatement.Value;
3689 LocatedToken lt = (LocatedToken) $1;
3690 LabeledStatement labeled = new LabeledStatement (lt.Location);
3692 if (current_block.AddLabel (lt.Value, labeled, lt.Location))
3693 current_block.AddStatement (labeled);
3698 declaration_statement
3699 : local_variable_declaration SEMICOLON
3701 current_array_type = null;
3703 DictionaryEntry de = (DictionaryEntry) $1;
3704 Expression e = (Expression) de.Key;
3706 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
3710 | local_constant_declaration SEMICOLON
3712 current_array_type = null;
3714 DictionaryEntry de = (DictionaryEntry) $1;
3716 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3722 * The following is from Rhys' grammar:
3723 * > Types in local variable declarations must be recognized as
3724 * > expressions to prevent reduce/reduce errors in the grammar.
3725 * > The expressions are converted into types during semantic analysis.
3728 : primary_expression opt_rank_specifier
3730 // FIXME: Do something smart here regarding the composition of the type.
3732 // Ok, the above "primary_expression" is there to get rid of
3733 // both reduce/reduce and shift/reduces in the grammar, it should
3734 // really just be "type_name". If you use type_name, a reduce/reduce
3735 // creeps up. If you use namespace_or_type_name (which is all we need
3736 // really) two shift/reduces appear.
3739 // So the super-trick is that primary_expression
3740 // can only be either a SimpleName or a MemberAccess.
3741 // The MemberAccess case arises when you have a fully qualified type-name like :
3743 // SimpleName is when you have
3746 Expression expr = (Expression) $1;
3747 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3748 Error_ExpectingTypeName (expr);
3752 // So we extract the string corresponding to the SimpleName
3756 if ((string) $2 == "")
3759 $$ = new ComposedCast ((Expression) $1, (string) $2);
3762 | builtin_types opt_rank_specifier
3764 if ((string) $2 == "")
3767 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3771 local_variable_pointer_type
3772 : primary_expression STAR
3774 Expression expr = (Expression) $1;
3776 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3777 Error_ExpectingTypeName (expr);
3781 $$ = new ComposedCast ((Expression) $1, "*");
3783 | builtin_types STAR
3785 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3789 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
3791 | local_variable_pointer_type STAR
3793 $$ = new ComposedCast ((Expression) $1, "*");
3797 local_variable_declaration
3798 : local_variable_type variable_declarators
3801 $$ = new DictionaryEntry ($1, $2);
3805 | local_variable_pointer_type opt_rank_specifier variable_declarators
3810 if ((string) $2 == "")
3811 t = (Expression) $1;
3813 t = new ComposedCast ((Expression) $1, (string) $2);
3814 $$ = new DictionaryEntry (t, $3);
3820 local_constant_declaration
3821 : CONST local_variable_type constant_declarators
3824 $$ = new DictionaryEntry ($2, $3);
3830 expression_statement
3831 : statement_expression SEMICOLON { $$ = $1; }
3835 // We have to do the wrapping here and not in the case above,
3836 // because statement_expression is used for example in for_statement
3838 statement_expression
3841 Expression expr = (Expression) $1;
3842 ExpressionStatement s = expr as ExpressionStatement;
3844 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
3847 $$ = new StatementExpression (s);
3851 Report.Error (1002, GetLocation ($1), "Expecting `;'");
3856 object_creation_expression
3857 : object_or_delegate_creation_expression
3858 { note ("complain if this is a delegate maybe?"); }
3867 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
3870 Location l = (Location) $1;
3872 $$ = new If ((Expression) $3, (Statement) $5, l);
3874 // FIXME: location for warning should be loc property of $5.
3875 if ($5 == EmptyStatement.Value)
3876 Report.Warning (642, 3, l, "Possible mistaken empty statement");
3879 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
3880 embedded_statement ELSE embedded_statement
3882 Location l = (Location) $1;
3884 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
3886 // FIXME: location for warning should be loc property of $5 and $7.
3887 if ($5 == EmptyStatement.Value)
3888 Report.Warning (642, 3, l, "Possible mistaken empty statement");
3889 if ($7 == EmptyStatement.Value)
3890 Report.Warning (642, 3, l, "Possible mistaken empty statement");
3895 : SWITCH OPEN_PARENS
3897 if (switch_stack == null)
3898 switch_stack = new Stack (2);
3899 switch_stack.Push (current_block);
3901 expression CLOSE_PARENS
3904 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
3905 current_block = (Block) switch_stack.Pop ();
3921 Report.Error (1522, lexer.Location, "Empty switch block");
3929 ArrayList sections = new ArrayList (4);
3934 | switch_sections switch_section
3936 ArrayList sections = (ArrayList) $1;
3946 current_block = current_block.CreateSwitchBlock (lexer.Location);
3950 Block topmost = current_block;
3952 while (topmost.Implicit)
3953 topmost = topmost.Parent;
3954 $$ = new SwitchSection ((ArrayList) $1, topmost);
3961 ArrayList labels = new ArrayList (4);
3966 | switch_labels switch_label
3968 ArrayList labels = (ArrayList) ($1);
3976 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
3977 | DEFAULT COLON { $$ = new SwitchLabel (null, (Location) $1); }
3980 1523, GetLocation ($1),
3981 "The keyword case or default must precede code in switch block");
3993 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
3995 Location l = (Location) $1;
3996 $$ = new While ((Expression) $3, (Statement) $5, l);
4001 : DO embedded_statement
4002 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
4004 Location l = (Location) $1;
4006 $$ = new Do ((Statement) $2, (Expression) $5, l);
4012 opt_for_initializer SEMICOLON
4014 Block assign_block = new Block (current_block);
4015 current_block = assign_block;
4017 if ($3 is DictionaryEntry){
4018 DictionaryEntry de = (DictionaryEntry) $3;
4020 Expression type = (Expression) de.Key;
4021 ArrayList var_declarators = (ArrayList) de.Value;
4023 foreach (VariableDeclaration decl in var_declarators){
4027 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4031 Location l = lexer.Location;
4032 Expression expr = decl.expression_or_array_initializer;
4034 LocalVariableReference var;
4035 var = new LocalVariableReference (assign_block, decl.identifier, l);
4038 Assign a = new Assign (var, expr, decl.Location);
4040 assign_block.AddStatement (new StatementExpression (a));
4044 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4045 // This can be referred to as $5 below.
4051 opt_for_condition SEMICOLON
4052 opt_for_iterator CLOSE_PARENS
4055 Location l = (Location) $1;
4057 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4059 current_block.AddStatement (f);
4060 while (current_block.Implicit)
4061 current_block = current_block.Parent;
4063 current_block = current_block.Parent;
4068 : /* empty */ { $$ = EmptyStatement.Value; }
4073 : local_variable_declaration
4074 | statement_expression_list
4078 : /* empty */ { $$ = null; }
4079 | boolean_expression
4083 : /* empty */ { $$ = EmptyStatement.Value; }
4088 : statement_expression_list
4091 statement_expression_list
4092 : statement_expression
4094 // CHANGE: was `null'
4095 Statement s = (Statement) $1;
4096 Block b = new Block (current_block, Block.Flags.Implicit, s.loc, lexer.Location);
4101 | statement_expression_list COMMA statement_expression
4103 Block b = (Block) $1;
4105 b.AddStatement ((Statement) $3);
4111 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4113 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4116 | FOREACH OPEN_PARENS type IDENTIFIER IN
4117 expression CLOSE_PARENS
4119 Block foreach_block = new Block (current_block);
4120 current_block = foreach_block;
4122 LocatedToken lt = (LocatedToken) $4;
4123 Location l = lt.Location;
4126 vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4128 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4130 // Get a writable reference to this read-only variable.
4132 // Note that the $$ here refers to the value of _this_ code block,
4133 // not the value of the LHS non-terminal. This can be referred to as $8 below.
4134 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
4141 LocalVariableReference v = (LocalVariableReference) $8;
4142 Location l = (Location) $1;
4145 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
4146 current_block.AddStatement (f);
4149 while (current_block.Implicit)
4150 current_block = current_block.Parent;
4152 current_block = current_block.Parent;
4158 | continue_statement
4168 $$ = new Break ((Location) $1);
4173 : CONTINUE SEMICOLON
4175 $$ = new Continue ((Location) $1);
4180 : GOTO IDENTIFIER SEMICOLON
4182 LocatedToken lt = (LocatedToken) $2;
4183 $$ = new Goto (lt.Value, lt.Location);
4185 | GOTO CASE constant_expression SEMICOLON
4187 $$ = new GotoCase ((Expression) $3, (Location) $1);
4189 | GOTO DEFAULT SEMICOLON
4191 $$ = new GotoDefault ((Location) $1);
4196 : RETURN opt_expression SEMICOLON
4198 $$ = new Return ((Expression) $2, (Location) $1);
4203 : THROW opt_expression SEMICOLON
4205 $$ = new Throw ((Expression) $2, (Location) $1);
4210 : IDENTIFIER RETURN expression SEMICOLON
4212 LocatedToken lt = (LocatedToken) $1;
4213 string s = lt.Value;
4215 Report.Error (1003, lt.Location, "; expected");
4218 if (RootContext.Version == LanguageVersion.ISO_1){
4219 Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4222 if (iterator_container == null){
4223 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4226 iterator_container.SetYields ();
4227 $$ = new Yield ((Expression) $3, lt.Location);
4230 | IDENTIFIER RETURN SEMICOLON
4232 Report.Error (1627, (Location) $2, "Expression expected after yield return");
4235 | IDENTIFIER BREAK SEMICOLON
4237 LocatedToken lt = (LocatedToken) $1;
4238 string s = lt.Value;
4240 Report.Error (1003, lt.Location, "; expected");
4243 if (RootContext.Version == LanguageVersion.ISO_1){
4244 Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4247 if (iterator_container == null){
4248 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4251 iterator_container.SetYields ();
4252 $$ = new YieldBreak (lt.Location);
4263 : TRY block catch_clauses
4267 ArrayList c = (ArrayList)$3;
4268 for (int i = 0; i < c.Count; ++i) {
4269 Catch cc = (Catch) c [i];
4271 if (i != c.Count - 1)
4272 Report.Error (1017, cc.loc, "Try statement already has an empty catch block");
4279 // Now s contains the list of specific catch clauses
4280 // and g contains the general one.
4282 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4284 | TRY block opt_catch_clauses FINALLY block
4287 ArrayList s = new ArrayList (4);
4288 ArrayList catch_list = (ArrayList) $3;
4290 if (catch_list != null){
4291 foreach (Catch cc in catch_list) {
4299 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4303 Report.Error (1524, (Location) $1, "Expected catch or finally");
4309 : /* empty */ { $$ = null; }
4316 ArrayList l = new ArrayList (4);
4321 | catch_clauses catch_clause
4323 ArrayList l = (ArrayList) $1;
4331 : /* empty */ { $$ = null; }
4336 : CATCH opt_catch_args
4338 Expression type = null;
4341 DictionaryEntry cc = (DictionaryEntry) $2;
4342 type = (Expression) cc.Key;
4343 LocatedToken lt = (LocatedToken) cc.Value;
4346 ArrayList one = new ArrayList (4);
4348 one.Add (new VariableDeclaration (lt, null));
4350 current_block = new Block (current_block);
4351 Block b = declare_local_variables (type, one, lt.Location);
4356 Expression type = null;
4358 Block var_block = null;
4361 DictionaryEntry cc = (DictionaryEntry) $2;
4362 type = (Expression) cc.Key;
4363 LocatedToken lt = (LocatedToken) cc.Value;
4367 while (current_block.Implicit)
4368 current_block = current_block.Parent;
4369 var_block = current_block;
4370 current_block = current_block.Parent;
4374 $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
4379 : /* empty */ { $$ = null; }
4384 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4386 $$ = new DictionaryEntry ($2, $3);
4394 $$ = new Checked ((Block) $2);
4401 $$ = new Unchecked ((Block) $2);
4408 RootContext.CheckUnsafeOption ((Location) $1);
4410 $$ = new Unsafe ((Block) $3);
4416 type fixed_pointer_declarators
4419 ArrayList list = (ArrayList) $4;
4420 Expression type = (Expression) $3;
4421 Location l = (Location) $1;
4422 int top = list.Count;
4424 Block assign_block = new Block (current_block);
4425 current_block = assign_block;
4427 for (int i = 0; i < top; i++){
4428 Pair p = (Pair) list [i];
4431 v = current_block.AddVariable (type, (string) p.First, l);
4435 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4443 Location l = (Location) $1;
4445 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4447 current_block.AddStatement (f);
4448 while (current_block.Implicit)
4449 current_block = current_block.Parent;
4451 current_block = current_block.Parent;
4455 fixed_pointer_declarators
4456 : fixed_pointer_declarator {
4457 ArrayList declarators = new ArrayList (4);
4459 declarators.Add ($1);
4462 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4464 ArrayList declarators = (ArrayList) $1;
4466 declarators.Add ($3);
4471 fixed_pointer_declarator
4472 : IDENTIFIER ASSIGN expression
4474 LocatedToken lt = (LocatedToken) $1;
4475 // FIXME: keep location
4476 $$ = new Pair (lt.Value, $3);
4480 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
4486 : LOCK OPEN_PARENS expression CLOSE_PARENS
4492 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
4497 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4499 Block assign_block = new Block (current_block);
4500 current_block = assign_block;
4502 if ($3 is DictionaryEntry){
4503 DictionaryEntry de = (DictionaryEntry) $3;
4504 Location l = (Location) $1;
4506 Expression type = (Expression) de.Key;
4507 ArrayList var_declarators = (ArrayList) de.Value;
4509 ArrayList vars = new ArrayList (4);
4511 foreach (VariableDeclaration decl in var_declarators){
4513 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4516 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
4518 Expression expr = decl.expression_or_array_initializer;
4520 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4523 LocalVariableReference var;
4525 // Get a writable reference to this read-only variable.
4526 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4528 // This is so that it is not a warning on using variables
4531 vars.Add (new DictionaryEntry (var, expr));
4533 // Assign a = new Assign (var, expr, decl.Location);
4534 // assign_block.AddStatement (new StatementExpression (a));
4537 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4538 // It can be referred to as $5 below.
4539 $$ = new DictionaryEntry (type, vars);
4546 Using u = new Using ($5, (Statement) $6, (Location) $1);
4547 current_block.AddStatement (u);
4548 while (current_block.Implicit)
4549 current_block = current_block.Parent;
4551 current_block = current_block.Parent;
4555 resource_acquisition
4556 : local_variable_declaration
4563 // A class used to pass around variable declarations and constants
4565 public class VariableDeclaration {
4566 public string identifier;
4567 public Expression expression_or_array_initializer;
4568 public Location Location;
4569 public Attributes OptAttributes;
4570 public string DocComment;
4572 public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
4574 this.identifier = lt.Value;
4575 if (eoai is ArrayList) {
4576 if (CSharpParser.current_array_type == null)
4577 Report.Error (622, lt.Location,
4578 "Can only use array initializer expressions to assign to array types. Try using a new expression instead.");
4579 this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
4581 this.expression_or_array_initializer = (Expression)eoai;
4583 this.Location = lt.Location;
4584 this.OptAttributes = opt_attrs;
4587 public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
4593 // A class used to hold info about an indexer declarator
4595 public class IndexerDeclaration {
4596 public Expression type;
4597 public MemberName interface_type;
4598 public Parameters param_list;
4599 public Location location;
4601 public IndexerDeclaration (Expression type, MemberName interface_type,
4602 Parameters param_list, Location loc)
4605 this.interface_type = interface_type;
4606 this.param_list = param_list;
4607 this.location = loc;
4612 // We use this when we do not have an object in advance that is an IIteratorContainer
4614 public class SimpleIteratorContainer : IIteratorContainer {
4617 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4622 public static SimpleIteratorContainer GetSimple () {
4623 Simple.Yields = false;
4627 public void SetYields () { Yields = true; }
4631 // A class used to hold info about an operator declarator
4633 public class OperatorDeclaration {
4634 public Operator.OpType optype;
4635 public Expression ret_type, arg1type, arg2type;
4636 public string arg1name, arg2name;
4637 public Location location;
4639 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4640 Expression arg1type, string arg1name,
4641 Expression arg2type, string arg2name, Location location)
4644 this.ret_type = ret_type;
4645 this.arg1type = arg1type;
4646 this.arg1name = arg1name;
4647 this.arg2type = arg2type;
4648 this.arg2name = arg2name;
4649 this.location = location;
4654 void Error_ExpectingTypeName (Expression expr)
4656 if (expr is Invocation){
4657 Report.Error (1002, expr.Location, "Expecting `;'");
4659 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
4663 TypeContainer pop_current_class ()
4665 TypeContainer retval = current_class;
4667 current_class = (TypeContainer) current_class.Parent;
4668 current_container = current_class.PartialContainer;
4674 // Given the @class_name name, it creates a fully qualified name
4675 // based on the containing declaration space
4678 MakeName (MemberName class_name)
4680 Namespace ns = current_namespace.NS;
4682 if (current_container.Name.Length == 0){
4683 if (ns.Name.Length != 0)
4684 return new MemberName (ns.MemberName, class_name);
4688 return new MemberName (current_container.MemberName, class_name);
4692 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4694 Block implicit_block;
4695 ArrayList inits = null;
4698 // We use the `Used' property to check whether statements
4699 // have been added to the current block. If so, we need
4700 // to create another block to contain the new declaration
4701 // otherwise, as an optimization, we use the same block to
4702 // add the declaration.
4704 // FIXME: A further optimization is to check if the statements
4705 // that were added were added as part of the initialization
4706 // below. In which case, no other statements have been executed
4707 // and we might be able to reduce the number of blocks for
4708 // situations like this:
4710 // int j = 1; int k = j + 1;
4712 if (current_block.Used)
4713 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4715 implicit_block = current_block;
4717 foreach (VariableDeclaration decl in variable_declarators){
4719 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
4720 if (decl.expression_or_array_initializer != null){
4722 inits = new ArrayList (4);
4729 return implicit_block;
4731 foreach (VariableDeclaration decl in inits){
4733 Expression expr = decl.expression_or_array_initializer;
4735 LocalVariableReference var;
4736 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4738 assign = new Assign (var, expr, decl.Location);
4740 implicit_block.AddStatement (new StatementExpression (assign));
4743 return implicit_block;
4746 Block declare_local_constants (Expression type, ArrayList declarators)
4748 Block implicit_block;
4750 if (current_block.Used)
4751 implicit_block = new Block (current_block, Block.Flags.Implicit);
4753 implicit_block = current_block;
4755 foreach (VariableDeclaration decl in declarators){
4756 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
4759 return implicit_block;
4762 void CheckAttributeTarget (string a, Location l)
4766 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4770 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4776 void CheckUnaryOperator (Operator.OpType op, Location l)
4780 case Operator.OpType.LogicalNot:
4781 case Operator.OpType.OnesComplement:
4782 case Operator.OpType.Increment:
4783 case Operator.OpType.Decrement:
4784 case Operator.OpType.True:
4785 case Operator.OpType.False:
4786 case Operator.OpType.Addition:
4787 case Operator.OpType.Subtraction:
4792 Report.Error (1019, l, "Overloadable unary operator expected");
4798 void CheckBinaryOperator (Operator.OpType op, Location l)
4802 case Operator.OpType.Addition:
4803 case Operator.OpType.Subtraction:
4804 case Operator.OpType.Multiply:
4805 case Operator.OpType.Division:
4806 case Operator.OpType.Modulus:
4807 case Operator.OpType.BitwiseAnd:
4808 case Operator.OpType.BitwiseOr:
4809 case Operator.OpType.ExclusiveOr:
4810 case Operator.OpType.LeftShift:
4811 case Operator.OpType.RightShift:
4812 case Operator.OpType.Equality:
4813 case Operator.OpType.Inequality:
4814 case Operator.OpType.GreaterThan:
4815 case Operator.OpType.LessThan:
4816 case Operator.OpType.GreaterThanOrEqual:
4817 case Operator.OpType.LessThanOrEqual:
4821 Report.Error (1020, l, "Overloadable binary operator expected");
4827 void syntax_error (Location l, string msg)
4829 Report.Error (1003, l, "Syntax error, " + msg);
4832 void note (string s)
4834 // Used to put annotations
4839 public Tokenizer Lexer {
4845 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4847 current_namespace = new NamespaceEntry (null, file, null);
4848 this.name = file.Name;
4850 current_container = RootContext.Tree.Types;
4851 // TODO: Make RootContext.Tree.Types a PartialContainer.
4852 current_class = current_container;
4853 current_container.NamespaceEntry = current_namespace;
4855 lexer = new Tokenizer (reader, file, defines);
4858 public void parse ()
4860 int errors = Report.Errors;
4862 if (yacc_verbose_flag > 1)
4863 yyparse (lexer, new yydebug.yyDebugSimple ());
4866 Tokenizer tokenizer = lexer as Tokenizer;
4867 tokenizer.cleanup ();
4868 } catch (Exception e){
4870 // Removed for production use, use parser verbose to get the output.
4872 // Console.WriteLine (e);
4873 if (Report.Errors == errors)
4874 Report.Error (-25, lexer.Location, "Parsing error");
4875 if (yacc_verbose_flag > 0)
4876 Console.WriteLine (e);
4879 RootContext.Tree.Types.NamespaceEntry = null;
4882 static void CheckToken (int error, int yyToken, string msg, Location loc)
4884 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
4885 Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ());
4887 Report.Error (error, loc, msg);
4890 void CheckIdentifierToken (int yyToken, Location loc)
4892 CheckToken (1041, yyToken, "Identifier expected", loc);
4895 string ConsumeStoredComment ()
4897 string s = tmpComment;
4899 Lexer.doc_state = XmlCommentState.Allowed;
4903 Location GetLocation (object obj)
4905 if (obj is MemberCore)
4906 return ((MemberCore) obj).Location;
4907 if (obj is MemberName)
4908 return ((MemberName) obj).Location;
4909 if (obj is LocatedToken)
4910 return ((LocatedToken) obj).Location;
4911 if (obj is Location)
4912 return (Location) obj;
4913 return lexer.Location;