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 (), name.Location);
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 if (!c.Parameters.Empty){
2010 Report.Error (132, c.Location,
2011 "`{0}': The static constructor must be parameterless", c.GetSignatureForError ());
2014 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2017 // We let another layer check the validity of the constructor.
2018 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
2021 current_container.AddConstructor (c);
2023 current_local_parameters = null;
2024 if (RootContext.Documentation != null)
2025 Lexer.doc_state = XmlCommentState.Allowed;
2029 constructor_declarator
2032 if (RootContext.Documentation != null) {
2033 tmpComment = Lexer.consume_doc_comment ();
2034 Lexer.doc_state = XmlCommentState.Allowed;
2037 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2039 current_local_parameters = (Parameters) $4;
2041 opt_constructor_initializer
2043 LocatedToken lt = (LocatedToken) $1;
2044 $$ = new Constructor (current_class, lt.Value, 0, (Parameters) $4,
2045 (ConstructorInitializer) $7, lt.Location);
2051 | SEMICOLON { $$ = null; }
2054 opt_constructor_initializer
2055 : /* empty */ { $$ = null; }
2056 | constructor_initializer
2059 constructor_initializer
2060 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
2062 $$ = new ConstructorBaseInitializer ((ArrayList) $4, (Location) $2);
2064 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
2066 $$ = new ConstructorThisInitializer ((ArrayList) $4, (Location) $2);
2069 Report.Error (1018, (Location) $1, "Keyword this or base expected");
2075 : /* EMPTY */ { $$ = 0; }
2076 | UNSAFE { $$ = Modifiers.UNSAFE; }
2077 | EXTERN { $$ = Modifiers.EXTERN; }
2080 destructor_declaration
2081 : opt_attributes opt_finalizer TILDE
2083 if (RootContext.Documentation != null) {
2084 tmpComment = Lexer.consume_doc_comment ();
2085 Lexer.doc_state = XmlCommentState.NotAllowed;
2088 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2090 LocatedToken lt = (LocatedToken) $5;
2091 if (lt.Value != current_container.Basename){
2092 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2093 } else if (current_container.Kind != Kind.Class){
2094 Report.Error (575, lt.Location, "Only class types can contain destructor");
2096 Location l = lt.Location;
2099 if (!RootContext.StdLib && current_container.Name == "System.Object")
2100 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2102 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2104 Method d = new Destructor (
2105 current_class, TypeManager.system_void_expr, m, "Finalize",
2106 Parameters.EmptyReadOnlyParameters, (Attributes) $1, l);
2107 if (RootContext.Documentation != null)
2108 d.DocComment = ConsumeStoredComment ();
2110 d.Block = (ToplevelBlock) $8;
2111 current_container.AddMethod (d);
2119 EVENT type variable_declarators SEMICOLON
2121 current_array_type = null;
2122 foreach (VariableDeclaration var in (ArrayList) $5) {
2124 MemberName name = new MemberName (var.identifier,
2127 EventField e = new EventField (
2128 current_class, (Expression) $4, (int) $2, false, name,
2131 e.Initializer = var.expression_or_array_initializer;
2133 current_container.AddEvent (e);
2135 if (RootContext.Documentation != null) {
2136 e.DocComment = Lexer.consume_doc_comment ();
2137 Lexer.doc_state = XmlCommentState.Allowed;
2143 EVENT type namespace_or_type_name
2146 implicit_value_parameter_type = (Expression) $4;
2147 lexer.EventParsing = true;
2149 event_accessor_declarations
2151 lexer.EventParsing = false;
2155 MemberName name = (MemberName) $5;
2158 Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2159 current_container.Name, name.ToString ());
2162 Pair pair = (Pair) $8;
2163 if (pair.First == null || pair.Second == null)
2164 // CS0073 is already reported, so no CS0065 here.
2167 Event e = new EventProperty (
2168 current_class, (Expression) $4, (int) $2, false, name,
2169 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second);
2170 if (RootContext.Documentation != null) {
2171 e.DocComment = Lexer.consume_doc_comment ();
2172 Lexer.doc_state = XmlCommentState.Allowed;
2175 current_container.AddEvent (e);
2176 implicit_value_parameter_type = null;
2180 | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
2181 MemberName mn = (MemberName) $5;
2183 if (mn.Left != null)
2184 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2186 Report.Error (71, mn.Location, "Event declaration should use property syntax");
2188 if (RootContext.Documentation != null)
2189 Lexer.doc_state = XmlCommentState.Allowed;
2193 event_accessor_declarations
2194 : add_accessor_declaration remove_accessor_declaration
2196 $$ = new Pair ($1, $2);
2198 | remove_accessor_declaration add_accessor_declaration
2200 $$ = new Pair ($2, $1);
2202 | add_accessor_declaration { $$ = null; }
2203 | remove_accessor_declaration { $$ = null; }
2206 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2212 add_accessor_declaration
2213 : opt_attributes ADD
2215 Parameter [] args = new Parameter [1];
2216 Parameter implicit_value_parameter = new Parameter (
2217 implicit_value_parameter_type, "value",
2218 Parameter.Modifier.NONE, null, (Location) $2);
2220 args [0] = implicit_value_parameter;
2222 current_local_parameters = new Parameters (args);
2223 lexer.EventParsing = false;
2227 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2228 lexer.EventParsing = true;
2230 | opt_attributes ADD error {
2231 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2234 | opt_attributes modifiers ADD {
2235 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2240 remove_accessor_declaration
2241 : opt_attributes REMOVE
2243 Parameter [] args = new Parameter [1];
2244 Parameter implicit_value_parameter = new Parameter (
2245 implicit_value_parameter_type, "value",
2246 Parameter.Modifier.NONE, null, (Location) $2);
2248 args [0] = implicit_value_parameter;
2250 current_local_parameters = new Parameters (args);
2251 lexer.EventParsing = false;
2255 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2256 lexer.EventParsing = true;
2258 | opt_attributes REMOVE error {
2259 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2262 | opt_attributes modifiers REMOVE {
2263 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2269 : opt_attributes opt_modifiers indexer_declarator
2272 IndexerDeclaration decl = (IndexerDeclaration) $3;
2274 implicit_value_parameter_type = decl.type;
2276 lexer.PropertyParsing = true;
2277 parsing_indexer = true;
2279 indexer_parameters = decl.param_list;
2281 accessor_declarations
2283 lexer.PropertyParsing = false;
2284 has_get = has_set = false;
2285 parsing_indexer = false;
2292 // The signature is computed from the signature of the indexer. Look
2293 // at section 3.6 on the spec
2295 IndexerDeclaration decl = (IndexerDeclaration) $3;
2296 Location loc = decl.location;
2297 Pair pair = (Pair) $6;
2298 Accessor get_block = (Accessor) pair.First;
2299 Accessor set_block = (Accessor) pair.Second;
2302 if (decl.interface_type != null)
2303 name = new MemberName (decl.interface_type, TypeContainer.DefaultIndexerName, loc);
2305 name = new MemberName (TypeContainer.DefaultIndexerName, loc);
2307 indexer = new Indexer (current_class, decl.type, name,
2308 (int) $2, false, decl.param_list, (Attributes) $1,
2309 get_block, set_block);
2311 if (RootContext.Documentation != null)
2312 indexer.DocComment = ConsumeStoredComment ();
2314 current_container.AddIndexer (indexer);
2316 current_local_parameters = null;
2317 implicit_value_parameter_type = null;
2318 indexer_parameters = null;
2323 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2325 Parameters pars = (Parameters) $4;
2326 if (pars.HasArglist) {
2327 // "__arglist is not valid in this context"
2328 Report.Error (1669, (Location) $2, "__arglist is not valid in this context");
2329 } else if (pars.Empty){
2330 Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
2332 if (RootContext.Documentation != null) {
2333 tmpComment = Lexer.consume_doc_comment ();
2334 Lexer.doc_state = XmlCommentState.Allowed;
2337 $$ = new IndexerDeclaration ((Expression) $1, null, pars, (Location) $2);
2339 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2341 Parameters pars = (Parameters) $6;
2343 if (pars.HasArglist) {
2344 // "__arglist is not valid in this context"
2345 Report.Error (1669, (Location) $4, "__arglist is not valid in this context");
2346 } else if (pars.Empty){
2347 Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
2350 MemberName name = (MemberName) $2;
2351 $$ = new IndexerDeclaration ((Expression) $1, name, pars, (Location) $4);
2353 if (RootContext.Documentation != null) {
2354 tmpComment = Lexer.consume_doc_comment ();
2355 Lexer.doc_state = XmlCommentState.Allowed;
2366 if (RootContext.Documentation != null)
2367 enumTypeComment = Lexer.consume_doc_comment ();
2372 LocatedToken lt = (LocatedToken) $5;
2373 Location enum_location = lt.Location;
2376 Report.Error (267, lt.Location, "The partial modifier can only appear immediately before `class', `struct' or `interface'");
2377 break; // assumes that the parser put us in a switch
2380 MemberName name = MakeName (new MemberName (lt.Value, enum_location));
2381 Enum e = new Enum (current_namespace, current_class, (Expression) $6, (int) $2,
2382 name, (Attributes) $1);
2384 if (RootContext.Documentation != null)
2385 e.DocComment = enumTypeComment;
2388 EnumMember em = null;
2389 foreach (VariableDeclaration ev in (ArrayList) $8) {
2390 em = new EnumMember (e, em, (Expression) ev.expression_or_array_initializer,
2391 new MemberName (ev.identifier, ev.Location), ev.OptAttributes);
2393 // if (RootContext.Documentation != null)
2394 em.DocComment = ev.DocComment;
2396 e.AddEnumMember (em);
2399 current_container.AddEnum (e);
2400 RootContext.Tree.RecordDecl (current_namespace.NS, name, e);
2407 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2408 | COLON type { $$ = $2; }
2414 if (RootContext.Documentation != null)
2415 Lexer.doc_state = XmlCommentState.Allowed;
2417 opt_enum_member_declarations
2419 // here will be evaluated after CLOSE_BLACE is consumed.
2420 if (RootContext.Documentation != null)
2421 Lexer.doc_state = XmlCommentState.Allowed;
2429 opt_enum_member_declarations
2430 : /* empty */ { $$ = new ArrayList (4); }
2431 | enum_member_declarations opt_comma { $$ = $1; }
2434 enum_member_declarations
2435 : enum_member_declaration
2437 ArrayList l = new ArrayList (4);
2442 | enum_member_declarations COMMA enum_member_declaration
2444 ArrayList l = (ArrayList) $1;
2452 enum_member_declaration
2453 : opt_attributes IDENTIFIER
2455 VariableDeclaration vd = new VariableDeclaration (
2456 (LocatedToken) $2, null, (Attributes) $1);
2458 if (RootContext.Documentation != null) {
2459 vd.DocComment = Lexer.consume_doc_comment ();
2460 Lexer.doc_state = XmlCommentState.Allowed;
2465 | opt_attributes IDENTIFIER
2467 if (RootContext.Documentation != null) {
2468 tmpComment = Lexer.consume_doc_comment ();
2469 Lexer.doc_state = XmlCommentState.NotAllowed;
2474 VariableDeclaration vd = new VariableDeclaration (
2475 (LocatedToken) $2, $5, (Attributes) $1);
2477 if (RootContext.Documentation != null)
2478 vd.DocComment = ConsumeStoredComment ();
2484 delegate_declaration
2487 DELEGATE type member_name
2488 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2491 MemberName name = MakeName ((MemberName) $5);
2492 Parameters p = (Parameters) $7;
2494 // TODO: wrong location
2495 Report.Error (1669, name.Location, "__arglist is not valid in this context");
2498 Delegate del = new Delegate (current_namespace, current_class, (Expression) $4,
2499 (int) $2, name, p, (Attributes) $1);
2501 if (RootContext.Documentation != null) {
2502 del.DocComment = Lexer.consume_doc_comment ();
2503 Lexer.doc_state = XmlCommentState.Allowed;
2506 current_container.AddDelegate (del);
2507 RootContext.Tree.RecordDecl (current_namespace.NS, name, del);
2512 namespace_or_type_name
2514 | IDENTIFIER DOUBLE_COLON IDENTIFIER {
2515 LocatedToken lt1 = (LocatedToken) $1;
2516 LocatedToken lt2 = (LocatedToken) $3;
2517 $$ = new MemberName (lt1.Value, lt2.Value, lt2.Location);
2519 | namespace_or_type_name DOT IDENTIFIER {
2520 LocatedToken lt = (LocatedToken) $3;
2521 $$ = new MemberName ((MemberName) $1, lt.Value);
2527 LocatedToken lt = (LocatedToken) $1;
2528 $$ = new MemberName (lt.Value, lt.Location);
2533 * Before you think of adding a return_type, notice that we have been
2534 * using two rules in the places where it matters (one rule using type
2535 * and another identical one that uses VOID as the return type). This
2536 * gets rid of a shift/reduce couple
2539 : namespace_or_type_name
2541 MemberName name = (MemberName) $1;
2542 $$ = name.GetTypeExpression ();
2554 // Note that here only unmanaged types are allowed but we
2555 // can't perform checks during this phase - we do it during
2556 // semantic analysis.
2558 $$ = new ComposedCast ((Expression) $1, "*", Lexer.Location);
2562 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2568 | non_expression_type rank_specifier
2570 Location loc = GetLocation ($1);
2572 loc = lexer.Location;
2573 $$ = new ComposedCast ((Expression) $1, (string) $2, loc);
2575 | non_expression_type STAR
2577 Location loc = GetLocation ($1);
2579 loc = lexer.Location;
2580 $$ = new ComposedCast ((Expression) $1, "*", loc);
2582 | expression rank_specifiers
2584 $$ = new ComposedCast ((Expression) $1, (string) $2);
2588 $$ = new ComposedCast ((Expression) $1, "*");
2592 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2593 // through this different path
2595 | multiplicative_expression STAR
2597 $$ = new ComposedCast ((Expression) $1, "*");
2604 ArrayList types = new ArrayList (4);
2609 | type_list COMMA type
2611 ArrayList types = (ArrayList) $1;
2619 * replaces all the productions for isolating the various
2620 * simple types, but we need this to reuse it easily in local_variable_type
2623 : OBJECT { $$ = TypeManager.system_object_expr; }
2624 | STRING { $$ = TypeManager.system_string_expr; }
2625 | BOOL { $$ = TypeManager.system_boolean_expr; }
2626 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2627 | FLOAT { $$ = TypeManager.system_single_expr; }
2628 | DOUBLE { $$ = TypeManager.system_double_expr; }
2633 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2634 | BYTE { $$ = TypeManager.system_byte_expr; }
2635 | SHORT { $$ = TypeManager.system_int16_expr; }
2636 | USHORT { $$ = TypeManager.system_uint16_expr; }
2637 | INT { $$ = TypeManager.system_int32_expr; }
2638 | UINT { $$ = TypeManager.system_uint32_expr; }
2639 | LONG { $$ = TypeManager.system_int64_expr; }
2640 | ULONG { $$ = TypeManager.system_uint64_expr; }
2641 | CHAR { $$ = TypeManager.system_char_expr; }
2642 | VOID { $$ = TypeManager.system_void_expr; }
2646 : type rank_specifiers
2648 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2);
2653 // Expressions, section 7.5
2663 MemberName mn = (MemberName) $1;
2664 $$ = mn.GetTypeExpression ();
2666 | IDENTIFIER DOUBLE_COLON IDENTIFIER
2668 LocatedToken lt1 = (LocatedToken) $1;
2669 LocatedToken lt2 = (LocatedToken) $3;
2670 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, lt2.Location);
2672 | parenthesized_expression
2674 | invocation_expression
2678 | post_increment_expression
2679 | post_decrement_expression
2683 | checked_expression
2684 | unchecked_expression
2685 | pointer_member_access
2686 | anonymous_method_expression
2693 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
2694 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); }
2695 | NULL { $$ = new NullLiteral (lexer.Location); }
2699 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
2700 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
2701 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
2706 object v = lexer.Value;
2709 $$ = new IntLiteral ((int) v, lexer.Location);
2710 } else if (v is uint)
2711 $$ = new UIntLiteral ((UInt32) v, lexer.Location);
2713 $$ = new LongLiteral ((Int64) v, lexer.Location);
2714 else if (v is ulong)
2715 $$ = new ULongLiteral ((UInt64) v, lexer.Location);
2717 Console.WriteLine ("OOPS. Unexpected result from scanner");
2722 : TRUE { $$ = new BoolLiteral (true, lexer.Location); }
2723 | FALSE { $$ = new BoolLiteral (false, lexer.Location); }
2726 parenthesized_expression_0
2727 : OPEN_PARENS expression CLOSE_PARENS
2730 lexer.Deambiguate_CloseParens ();
2731 // After this, the next token returned is one of
2732 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2733 // or CLOSE_PARENS_MINUS.
2735 | OPEN_PARENS expression error { CheckToken (1026, yyToken, "Expecting ')'", lexer.Location); }
2738 parenthesized_expression
2739 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2743 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2745 // If a parenthesized expression is followed by a minus, we need to wrap
2746 // the expression inside a ParenthesizedExpression for the CS0075 check
2747 // in Binary.DoResolve().
2748 $$ = new ParenthesizedExpression ((Expression) $1);
2753 : primary_expression DOT IDENTIFIER
2755 LocatedToken lt = (LocatedToken) $3;
2756 $$ = new MemberAccess ((Expression) $1, lt.Value, lt.Location);
2758 | predefined_type DOT IDENTIFIER
2760 LocatedToken lt = (LocatedToken) $3;
2761 $$ = new MemberAccess ((Expression) $1, lt.Value, lt.Location);
2769 invocation_expression
2770 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2773 Report.Error (1, (Location) $2, "Parse error");
2775 $$ = new Invocation ((Expression) $1, (ArrayList) $3);
2777 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2779 $$ = new Invocation ((Expression) $1, new ArrayList ());
2781 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2783 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
2785 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2787 ArrayList args = new ArrayList (1);
2789 $$ = new Invocation ((Expression) $1, args);
2791 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2793 ArrayList args = ((ArrayList) $4);
2795 $$ = new Invocation ((Expression) $1, args);
2800 : /* empty */ { $$ = null; }
2807 ArrayList list = new ArrayList (4);
2811 | argument_list COMMA argument
2813 ArrayList list = (ArrayList) $1;
2817 | argument_list error {
2818 CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
2826 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2828 | non_simple_argument
2835 : REF variable_reference
2837 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2839 | OUT variable_reference
2841 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2843 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2845 ArrayList list = (ArrayList) $3;
2846 Argument[] args = new Argument [list.Count];
2847 list.CopyTo (args, 0);
2849 Expression expr = new Arglist (args, (Location) $1);
2850 $$ = new Argument (expr, Argument.AType.Expression);
2854 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
2859 : expression { note ("section 5.4"); $$ = $1; }
2863 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2865 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
2867 | primary_expression rank_specifiers
2869 // So the super-trick is that primary_expression
2870 // can only be either a SimpleName or a MemberAccess.
2871 // The MemberAccess case arises when you have a fully qualified type-name like :
2873 // SimpleName is when you have
2876 Expression expr = (Expression) $1;
2877 if (expr is ComposedCast){
2878 $$ = new ComposedCast (expr, (string) $2);
2879 } else if (!(expr is SimpleName || expr is MemberAccess || expr is QualifiedAliasMember)){
2880 Error_ExpectingTypeName (expr);
2881 $$ = TypeManager.system_object_expr;
2884 // So we extract the string corresponding to the SimpleName
2887 $$ = new ComposedCast (expr, (string) $2);
2889 current_array_type = (Expression)$$;
2896 ArrayList list = new ArrayList (4);
2900 | expression_list COMMA expression
2902 ArrayList list = (ArrayList) $1;
2911 $$ = new This (current_block, (Location) $1);
2916 : BASE DOT IDENTIFIER
2918 LocatedToken lt = (LocatedToken) $3;
2919 $$ = new BaseAccess (lt.Value, lt.Location);
2921 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2923 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
2926 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
2931 post_increment_expression
2932 : primary_expression OP_INC
2934 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2935 (Expression) $1, (Location) $2);
2939 post_decrement_expression
2940 : primary_expression OP_DEC
2942 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2943 (Expression) $1, (Location) $2);
2948 : object_or_delegate_creation_expression
2949 | array_creation_expression
2952 object_or_delegate_creation_expression
2953 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2955 $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
2959 array_creation_expression
2960 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2962 opt_array_initializer
2964 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
2966 | NEW type rank_specifiers array_initializer
2968 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, (Location) $1);
2972 Report.Error (1031, (Location) $1, "Type expected");
2977 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
2994 : rank_specifier opt_rank_specifier
2996 $$ = (string) $2 + (string) $1;
3001 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3003 $$ = "[" + (string) $2 + "]";
3023 | dim_separators COMMA
3025 $$ = (string) $1 + ",";
3029 opt_array_initializer
3041 : OPEN_BRACE CLOSE_BRACE
3043 ArrayList list = new ArrayList (4);
3046 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3048 $$ = (ArrayList) $2;
3052 variable_initializer_list
3053 : variable_initializer
3055 ArrayList list = new ArrayList (4);
3059 | variable_initializer_list COMMA variable_initializer
3061 ArrayList list = (ArrayList) $1;
3068 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
3070 $$ = new TypeOfVoid ((Location) $1);
3072 | TYPEOF OPEN_PARENS type CLOSE_PARENS
3074 $$ = new TypeOf ((Expression) $3, (Location) $1);
3079 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3080 $$ = new SizeOf ((Expression) $3, (Location) $1);
3085 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3087 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3091 unchecked_expression
3092 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3094 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3098 pointer_member_access
3099 : primary_expression OP_PTR IDENTIFIER
3102 LocatedToken lt = (LocatedToken) $3;
3104 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
3105 $$ = new MemberAccess (deref, lt.Value, lt.Location);
3109 anonymous_method_expression
3110 : DELEGATE opt_anonymous_method_signature
3112 oob_stack.Push (current_local_parameters);
3113 current_local_parameters = (Parameters)$2;
3115 // Force the next block to be created as a ToplevelBlock
3116 oob_stack.Push (current_block);
3117 oob_stack.Push (top_current_block);
3118 current_block = null;
3122 Location loc = (Location) $1;
3123 top_current_block = (Block) oob_stack.Pop ();
3124 current_block = (Block) oob_stack.Pop ();
3125 if (RootContext.Version == LanguageVersion.ISO_1){
3126 Report.FeatureIsNotStandardized (loc, "anonymous methods");
3129 ToplevelBlock anon_block = (ToplevelBlock) $4;
3131 anon_block.Parent = current_block;
3132 $$ = new AnonymousMethod (current_container, (Parameters) $2, (ToplevelBlock) top_current_block,
3135 current_local_parameters = (Parameters) oob_stack.Pop ();
3139 opt_anonymous_method_signature
3140 : /* empty */ { $$ = null; }
3141 | anonymous_method_signature
3144 anonymous_method_signature
3145 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3148 $$ = Parameters.EmptyReadOnlyParameters;
3150 ArrayList par_list = (ArrayList) $2;
3151 Parameter [] pars = new Parameter [par_list.Count];
3152 par_list.CopyTo (pars);
3153 $$ = new Parameters (pars);
3158 opt_anonymous_method_parameter_list
3159 : /* empty */ { $$ = null; }
3160 | anonymous_method_parameter_list { $$ = $1; }
3163 anonymous_method_parameter_list
3164 : anonymous_method_parameter
3166 ArrayList a = new ArrayList (4);
3170 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3172 ArrayList a = (ArrayList) $1;
3178 anonymous_method_parameter
3179 : opt_parameter_modifier type IDENTIFIER {
3180 LocatedToken lt = (LocatedToken) $3;
3181 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3183 | PARAMS type IDENTIFIER {
3184 Report.Error (1670, ((LocatedToken) $3).Location, "The `params' modifier is not allowed in anonymous method declaration");
3190 : primary_expression
3191 | BANG prefixed_unary_expression
3193 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3195 | TILDE prefixed_unary_expression
3197 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3203 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3205 $$ = new Cast ((Expression) $1, (Expression) $3);
3207 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3209 $$ = new Cast ((Expression) $1, (Expression) $3);
3215 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3217 // TODO: wrong location
3218 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3223 // The idea to split this out is from Rhys' grammar
3224 // to solve the problem with casts.
3226 prefixed_unary_expression
3228 | PLUS prefixed_unary_expression
3230 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3232 | MINUS prefixed_unary_expression
3234 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3236 | OP_INC prefixed_unary_expression
3238 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3239 (Expression) $2, (Location) $1);
3241 | OP_DEC prefixed_unary_expression
3243 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3244 (Expression) $2, (Location) $1);
3246 | STAR prefixed_unary_expression
3248 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, (Location) $1);
3250 | BITWISE_AND prefixed_unary_expression
3252 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3256 pre_increment_expression
3257 : OP_INC prefixed_unary_expression
3259 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3260 (Expression) $2, (Location) $1);
3264 pre_decrement_expression
3265 : OP_DEC prefixed_unary_expression
3267 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3268 (Expression) $2, (Location) $1);
3272 multiplicative_expression
3273 : prefixed_unary_expression
3274 | multiplicative_expression STAR prefixed_unary_expression
3276 $$ = new Binary (Binary.Operator.Multiply,
3277 (Expression) $1, (Expression) $3);
3279 | multiplicative_expression DIV prefixed_unary_expression
3281 $$ = new Binary (Binary.Operator.Division,
3282 (Expression) $1, (Expression) $3);
3284 | multiplicative_expression PERCENT prefixed_unary_expression
3286 $$ = new Binary (Binary.Operator.Modulus,
3287 (Expression) $1, (Expression) $3);
3292 : multiplicative_expression
3293 | additive_expression PLUS multiplicative_expression
3295 $$ = new Binary (Binary.Operator.Addition,
3296 (Expression) $1, (Expression) $3);
3298 | additive_expression MINUS multiplicative_expression
3300 $$ = new Binary (Binary.Operator.Subtraction,
3301 (Expression) $1, (Expression) $3);
3306 : additive_expression
3307 | shift_expression OP_SHIFT_LEFT additive_expression
3309 $$ = new Binary (Binary.Operator.LeftShift,
3310 (Expression) $1, (Expression) $3);
3312 | shift_expression OP_SHIFT_RIGHT additive_expression
3314 $$ = new Binary (Binary.Operator.RightShift,
3315 (Expression) $1, (Expression) $3);
3319 relational_expression
3321 | relational_expression OP_LT shift_expression
3323 $$ = new Binary (Binary.Operator.LessThan,
3324 (Expression) $1, (Expression) $3);
3326 | relational_expression OP_GT shift_expression
3328 $$ = new Binary (Binary.Operator.GreaterThan,
3329 (Expression) $1, (Expression) $3);
3331 | relational_expression OP_LE shift_expression
3333 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3334 (Expression) $1, (Expression) $3);
3336 | relational_expression OP_GE shift_expression
3338 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3339 (Expression) $1, (Expression) $3);
3341 | relational_expression IS type
3343 $$ = new Is ((Expression) $1, (Expression) $3, (Location) $2);
3345 | relational_expression AS type
3347 $$ = new As ((Expression) $1, (Expression) $3, (Location) $2);
3352 : relational_expression
3353 | equality_expression OP_EQ relational_expression
3355 $$ = new Binary (Binary.Operator.Equality,
3356 (Expression) $1, (Expression) $3);
3358 | equality_expression OP_NE relational_expression
3360 $$ = new Binary (Binary.Operator.Inequality,
3361 (Expression) $1, (Expression) $3);
3366 : equality_expression
3367 | and_expression BITWISE_AND equality_expression
3369 $$ = new Binary (Binary.Operator.BitwiseAnd,
3370 (Expression) $1, (Expression) $3);
3374 exclusive_or_expression
3376 | exclusive_or_expression CARRET and_expression
3378 $$ = new Binary (Binary.Operator.ExclusiveOr,
3379 (Expression) $1, (Expression) $3);
3383 inclusive_or_expression
3384 : exclusive_or_expression
3385 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3387 $$ = new Binary (Binary.Operator.BitwiseOr,
3388 (Expression) $1, (Expression) $3);
3392 conditional_and_expression
3393 : inclusive_or_expression
3394 | conditional_and_expression OP_AND inclusive_or_expression
3396 $$ = new Binary (Binary.Operator.LogicalAnd,
3397 (Expression) $1, (Expression) $3);
3401 conditional_or_expression
3402 : conditional_and_expression
3403 | conditional_or_expression OP_OR conditional_and_expression
3405 $$ = new Binary (Binary.Operator.LogicalOr,
3406 (Expression) $1, (Expression) $3);
3410 conditional_expression
3411 : conditional_or_expression
3412 | conditional_or_expression INTERR expression COLON expression
3414 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
3418 assignment_expression
3419 : prefixed_unary_expression ASSIGN expression
3421 $$ = new Assign ((Expression) $1, (Expression) $3);
3423 | prefixed_unary_expression OP_MULT_ASSIGN expression
3425 $$ = new CompoundAssign (
3426 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
3428 | prefixed_unary_expression OP_DIV_ASSIGN expression
3430 $$ = new CompoundAssign (
3431 Binary.Operator.Division, (Expression) $1, (Expression) $3);
3433 | prefixed_unary_expression OP_MOD_ASSIGN expression
3435 $$ = new CompoundAssign (
3436 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
3438 | prefixed_unary_expression OP_ADD_ASSIGN expression
3440 $$ = new CompoundAssign (
3441 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
3443 | prefixed_unary_expression OP_SUB_ASSIGN expression
3445 $$ = new CompoundAssign (
3446 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3448 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3450 $$ = new CompoundAssign (
3451 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
3453 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3455 $$ = new CompoundAssign (
3456 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
3458 | prefixed_unary_expression OP_AND_ASSIGN expression
3460 $$ = new CompoundAssign (
3461 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
3463 | prefixed_unary_expression OP_OR_ASSIGN expression
3465 $$ = new CompoundAssign (
3466 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
3468 | prefixed_unary_expression OP_XOR_ASSIGN expression
3470 $$ = new CompoundAssign (
3471 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
3476 : conditional_expression
3477 | assignment_expression
3497 MemberName name = MakeName ((MemberName) $5);
3498 int mod_flags = (int) $2;
3500 current_class = new Class (
3501 current_namespace, current_class, name,
3502 mod_flags, (Attributes) $1);
3505 current_container = current_container.AddPartial (current_class);
3507 current_container.AddClassOrStruct (current_class);
3508 current_container = current_class;
3514 if (current_class.Name == "System.Object") {
3515 Report.Error (537, current_class.Location,
3516 "The class System.Object cannot have a base " +
3517 "class or implement an interface.");
3519 current_class.Bases = (ArrayList) $7;
3522 if (RootContext.Documentation != null) {
3523 current_container.DocComment = Lexer.consume_doc_comment ();
3524 Lexer.doc_state = XmlCommentState.Allowed;
3529 if (RootContext.Documentation != null)
3530 Lexer.doc_state = XmlCommentState.Allowed;
3534 $$ = pop_current_class ();
3542 { $$ = $1; } // location
3546 : /* empty */ { $$ = (int) 0; }
3552 | modifiers modifier
3557 if ((m1 & m2) != 0) {
3558 Location l = lexer.Location;
3559 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
3561 $$ = (int) (m1 | m2);
3566 : NEW { $$ = Modifiers.NEW; }
3567 | PUBLIC { $$ = Modifiers.PUBLIC; }
3568 | PROTECTED { $$ = Modifiers.PROTECTED; }
3569 | INTERNAL { $$ = Modifiers.INTERNAL; }
3570 | PRIVATE { $$ = Modifiers.PRIVATE; }
3571 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3572 | SEALED { $$ = Modifiers.SEALED; }
3573 | STATIC { $$ = Modifiers.STATIC; }
3574 | READONLY { $$ = Modifiers.READONLY; }
3575 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3576 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3577 | EXTERN { $$ = Modifiers.EXTERN; }
3578 | VOLATILE { $$ = Modifiers.VOLATILE; }
3579 | UNSAFE { $$ = Modifiers.UNSAFE; }
3583 : /* empty */ { $$ = null; }
3584 | class_base { $$ = $1; }
3588 : COLON type_list { $$ = $2; }
3596 // A block is "contained" on the following places:
3598 // property_declaration as part of the accessor body (get/set)
3599 // operator_declaration
3600 // constructor_declaration
3601 // destructor_declaration
3602 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3607 if (current_block == null){
3608 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, (Location) $1);
3609 top_current_block = current_block;
3611 current_block = new Block (current_block, (Location) $1, Location.Null);
3614 opt_statement_list CLOSE_BRACE
3616 while (current_block.Implicit)
3617 current_block = current_block.Parent;
3619 current_block.SetEndLocation ((Location) $4);
3620 current_block = current_block.Parent;
3621 if (current_block == null)
3622 top_current_block = null;
3633 | statement_list statement
3637 : declaration_statement
3639 if ($1 != null && (Block) $1 != current_block){
3640 current_block.AddStatement ((Statement) $1);
3641 current_block = (Block) $1;
3644 | valid_declaration_statement
3646 current_block.AddStatement ((Statement) $1);
3651 valid_declaration_statement
3654 | expression_statement
3655 | selection_statement
3656 | iteration_statement
3660 | unchecked_statement
3668 : valid_declaration_statement
3669 | declaration_statement
3671 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3676 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3684 $$ = EmptyStatement.Value;
3691 LocatedToken lt = (LocatedToken) $1;
3692 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
3694 if (current_block.AddLabel (lt.Value, labeled, lt.Location))
3695 current_block.AddStatement (labeled);
3700 declaration_statement
3701 : local_variable_declaration SEMICOLON
3703 current_array_type = null;
3705 DictionaryEntry de = (DictionaryEntry) $1;
3706 Expression e = (Expression) de.Key;
3708 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
3712 | local_constant_declaration SEMICOLON
3714 current_array_type = null;
3716 DictionaryEntry de = (DictionaryEntry) $1;
3718 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3724 * The following is from Rhys' grammar:
3725 * > Types in local variable declarations must be recognized as
3726 * > expressions to prevent reduce/reduce errors in the grammar.
3727 * > The expressions are converted into types during semantic analysis.
3730 : primary_expression opt_rank_specifier
3732 // FIXME: Do something smart here regarding the composition of the type.
3734 // Ok, the above "primary_expression" is there to get rid of
3735 // both reduce/reduce and shift/reduces in the grammar, it should
3736 // really just be "type_name". If you use type_name, a reduce/reduce
3737 // creeps up. If you use namespace_or_type_name (which is all we need
3738 // really) two shift/reduces appear.
3741 // So the super-trick is that primary_expression
3742 // can only be either a SimpleName or a MemberAccess.
3743 // The MemberAccess case arises when you have a fully qualified type-name like :
3745 // SimpleName is when you have
3748 Expression expr = (Expression) $1;
3749 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3750 Error_ExpectingTypeName (expr);
3754 // So we extract the string corresponding to the SimpleName
3758 if ((string) $2 == "")
3761 $$ = new ComposedCast ((Expression) $1, (string) $2);
3764 | builtin_types opt_rank_specifier
3766 if ((string) $2 == "")
3769 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3773 local_variable_pointer_type
3774 : primary_expression STAR
3776 Expression expr = (Expression) $1;
3778 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3779 Error_ExpectingTypeName (expr);
3783 $$ = new ComposedCast ((Expression) $1, "*");
3785 | builtin_types STAR
3787 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3791 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
3793 | local_variable_pointer_type STAR
3795 $$ = new ComposedCast ((Expression) $1, "*");
3799 local_variable_declaration
3800 : local_variable_type variable_declarators
3803 $$ = new DictionaryEntry ($1, $2);
3807 | local_variable_pointer_type opt_rank_specifier variable_declarators
3812 if ((string) $2 == "")
3813 t = (Expression) $1;
3815 t = new ComposedCast ((Expression) $1, (string) $2);
3816 $$ = new DictionaryEntry (t, $3);
3822 local_constant_declaration
3823 : CONST local_variable_type constant_declarators
3826 $$ = new DictionaryEntry ($2, $3);
3832 expression_statement
3833 : statement_expression SEMICOLON { $$ = $1; }
3837 // We have to do the wrapping here and not in the case above,
3838 // because statement_expression is used for example in for_statement
3840 statement_expression
3843 Expression expr = (Expression) $1;
3844 ExpressionStatement s = expr as ExpressionStatement;
3846 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
3849 $$ = new StatementExpression (s);
3853 Report.Error (1002, GetLocation ($1), "Expecting `;'");
3858 object_creation_expression
3859 : object_or_delegate_creation_expression
3860 { note ("complain if this is a delegate maybe?"); }
3869 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
3872 Location l = (Location) $1;
3874 $$ = new If ((Expression) $3, (Statement) $5, l);
3876 // FIXME: location for warning should be loc property of $5.
3877 if ($5 == EmptyStatement.Value)
3878 Report.Warning (642, 3, l, "Possible mistaken empty statement");
3881 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
3882 embedded_statement ELSE embedded_statement
3884 Location l = (Location) $1;
3886 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
3888 // FIXME: location for warning should be loc property of $5 and $7.
3889 if ($5 == EmptyStatement.Value)
3890 Report.Warning (642, 3, l, "Possible mistaken empty statement");
3891 if ($7 == EmptyStatement.Value)
3892 Report.Warning (642, 3, l, "Possible mistaken empty statement");
3897 : SWITCH OPEN_PARENS
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 == ""){
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, Location.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;
4854 oob_stack = new Stack ();
4855 switch_stack = new Stack ();
4857 lexer = new Tokenizer (reader, file, defines);
4860 public void parse ()
4862 int errors = Report.Errors;
4864 if (yacc_verbose_flag > 1)
4865 yyparse (lexer, new yydebug.yyDebugSimple ());
4868 Tokenizer tokenizer = lexer as Tokenizer;
4869 tokenizer.cleanup ();
4870 } catch (Exception e){
4872 // Removed for production use, use parser verbose to get the output.
4874 // Console.WriteLine (e);
4875 if (Report.Errors == errors)
4876 Report.Error (-25, lexer.Location, "Parsing error");
4877 if (yacc_verbose_flag > 0)
4878 Console.WriteLine (e);
4881 RootContext.Tree.Types.NamespaceEntry = null;
4884 void CheckToken (int error, int yyToken, string msg, Location loc)
4886 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
4887 Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ());
4889 Report.Error (error, loc, msg);
4892 void CheckIdentifierToken (int yyToken, Location loc)
4894 CheckToken (1041, yyToken, "Identifier expected", loc);
4897 string ConsumeStoredComment ()
4899 string s = tmpComment;
4901 Lexer.doc_state = XmlCommentState.Allowed;
4905 Location GetLocation (object obj)
4907 if (obj is MemberCore)
4908 return ((MemberCore) obj).Location;
4909 if (obj is MemberName)
4910 return ((MemberName) obj).Location;
4911 if (obj is LocatedToken)
4912 return ((LocatedToken) obj).Location;
4913 if (obj is Location)
4914 return (Location) obj;
4915 return lexer.Location;