3 // cs-parser.jay: The Parser for the C# compiler
5 // Authors: Miguel de Icaza (miguel@gnu.org)
6 // Ravi Pratap (ravi@ximian.com)
8 // Licensed under the terms of the GNU GPL
10 // (C) 2001 Ximian, Inc (http://www.ximian.com)
11 // (C) 2004 Novell, Inc
14 // (1) Figure out why error productions dont work. `type-declaration' is a
15 // great spot to put an `error' because you can reproduce it with this input:
18 // Possible optimization:
19 // Run memory profiler with parsing only, and consider dropping
20 // arraylists where not needed. Some pieces can use linked lists.
28 using System.Collections;
33 public class CSharpParser {
34 NamespaceEntry current_namespace;
35 TypeContainer current_container;
36 TypeContainer current_class;
38 IIteratorContainer iterator_container;
41 /// Current block is used to add statements as we find
44 Block current_block, top_current_block;
47 /// This is used by the unary_expression code to resolve
48 /// a name against a parameter.
50 Parameters current_local_parameters;
53 /// Using during property parsing to describe the implicit
54 /// value parameter that is passed to the "set" and "get"accesor
55 /// methods (properties and indexers).
57 Expression implicit_value_parameter_type;
58 Parameters indexer_parameters;
61 /// Hack to help create non-typed array initializer
63 public static Expression current_array_type;
66 /// Used to determine if we are parsing the get/set pair
67 /// of an indexer or a property
72 /// An out-of-band stack.
81 static public int yacc_verbose_flag;
83 // Name of the file we are parsing
92 /// Temporary Xml documentation cache.
93 /// For enum types, we need one more temporary store.
96 string enumTypeComment;
98 /// Current attribute target
99 string current_attr_target;
101 /// assembly and module attribute definitions are enabled
102 bool global_attrs_enabled = true;
104 bool has_get, has_set;
109 %token NONE /* This token is never returned by our lexer */
110 %token ERROR // This is used not by the parser, but by the tokenizer.
114 *These are the C# keywords
200 /* C# keywords which are not really keywords */
206 /* C# single character operators/punctuation. */
207 %token OPEN_BRACE "{"
208 %token CLOSE_BRACE "}"
209 %token OPEN_BRACKET "["
210 %token CLOSE_BRACKET "]"
211 %token OPEN_PARENS "("
212 %token CLOSE_PARENS ")"
225 %token BITWISE_AND "&"
226 %token BITWISE_OR "|"
233 /* C# multi-character operators. */
234 %token DOUBLE_COLON "::"
237 %token OP_SHIFT_LEFT "<<"
238 %token OP_SHIFT_RIGHT ">>"
245 %token OP_MULT_ASSIGN "*="
246 %token OP_DIV_ASSIGN "/="
247 %token OP_MOD_ASSIGN "%="
248 %token OP_ADD_ASSIGN "+="
249 %token OP_SUB_ASSIGN "-="
250 %token OP_SHIFT_LEFT_ASSIGN "<<="
251 %token OP_SHIFT_RIGHT_ASSIGN ">>="
252 %token OP_AND_ASSIGN "&="
253 %token OP_XOR_ASSIGN "^="
254 %token OP_OR_ASSIGN "|="
258 %token LITERAL_INTEGER "int literal"
259 %token LITERAL_FLOAT "float literal"
260 %token LITERAL_DOUBLE "double literal"
261 %token LITERAL_DECIMAL "decimal literal"
262 %token LITERAL_CHARACTER "character literal"
263 %token LITERAL_STRING "string literal"
266 %token CLOSE_PARENS_CAST
267 %token CLOSE_PARENS_NO_CAST
268 %token CLOSE_PARENS_OPEN_PARENS
269 %token CLOSE_PARENS_MINUS
271 /* Add precedence rules to solve dangling else s/r conflict */
280 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
282 %left STAR DIV PERCENT
283 %right BANG CARRET UMINUS
284 %nonassoc OP_INC OP_DEC
286 %left OPEN_BRACKET OPEN_BRACE
290 %start compilation_unit
294 : outer_declarations opt_EOF
295 | outer_declarations global_attributes opt_EOF
296 | global_attributes opt_EOF
297 | opt_EOF /* allow empty files */
303 Lexer.check_incorrect_doc_comment ();
307 Lexer.check_incorrect_doc_comment ();
313 | outer_declarations outer_declaration
317 : extern_alias_directive
319 | namespace_member_declaration
322 extern_alias_directives
323 : extern_alias_directive
324 | extern_alias_directives extern_alias_directive;
326 extern_alias_directive
327 : EXTERN IDENTIFIER IDENTIFIER SEMICOLON
329 LocatedToken lt = (LocatedToken) $2;
332 Report.Error (1003, lt.Location, "'alias' expected");
333 } else if (RootContext.Version == LanguageVersion.ISO_1) {
334 Report.FeatureIsNotStandardized (lt.Location, "external alias");
336 lt = (LocatedToken) $3;
337 current_namespace.UsingExternalAlias (lt.Value, lt.Location);
344 | using_directives using_directive
348 : using_alias_directive
350 if (RootContext.Documentation != null)
351 Lexer.doc_state = XmlCommentState.Allowed;
353 | using_namespace_directive
355 if (RootContext.Documentation != null)
356 Lexer.doc_state = XmlCommentState.Allowed;
360 using_alias_directive
361 : USING IDENTIFIER ASSIGN
362 namespace_or_type_name SEMICOLON
364 LocatedToken lt = (LocatedToken) $2;
365 current_namespace.UsingAlias (lt.Value, (MemberName) $4, (Location) $1);
368 CheckIdentifierToken (yyToken, GetLocation ($2));
372 using_namespace_directive
373 : USING namespace_name SEMICOLON
375 current_namespace.Using ((MemberName) $2, (Location) $1);
380 // Strictly speaking, namespaces don't have attributes but
381 // we parse global attributes along with namespace declarations and then
384 namespace_declaration
385 : opt_attributes NAMESPACE namespace_or_type_name
387 MemberName name = (MemberName) $3;
390 Report.Error(1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
393 current_namespace = new NamespaceEntry (
394 current_namespace, file, name.GetName ());
396 namespace_body opt_semicolon
398 current_namespace = current_namespace.Parent;
413 : namespace_or_type_name
419 if (RootContext.Documentation != null)
420 Lexer.doc_state = XmlCommentState.Allowed;
422 opt_extern_alias_directives
424 opt_namespace_member_declarations
433 opt_extern_alias_directives
435 | extern_alias_directives
438 opt_namespace_member_declarations
440 | namespace_member_declarations
443 namespace_member_declarations
444 : namespace_member_declaration
445 | namespace_member_declarations namespace_member_declaration
448 namespace_member_declaration
452 DeclSpace ds = (DeclSpace)$1;
454 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
455 Report.Error (1527, ds.Location,
456 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
459 current_namespace.DeclarationFound = true;
461 | namespace_declaration {
462 current_namespace.DeclarationFound = true;
465 | field_declaration {
466 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
468 | method_declaration {
469 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
476 | interface_declaration
478 | delegate_declaration
480 // Enable this when we have handled all errors, because this acts as a generic fallback
483 // Console.WriteLine ("Token=" + yyToken);
484 // Report.Error (1518, GetLocation ($1), "Expected class, struct, interface, enum or delegate");
496 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
504 global_attrs_enabled = false;
509 global_attrs_enabled = false;
518 ArrayList sect = (ArrayList) $1;
520 if (global_attrs_enabled) {
521 if (current_attr_target == "module") {
522 CodeGen.Module.AddAttributes (sect);
524 } else if (current_attr_target != null && current_attr_target.Length > 0) {
525 CodeGen.Assembly.AddAttributes (sect);
528 $$ = new Attributes (sect);
531 if (RootContext.Documentation != null) {
532 Lexer.check_incorrect_doc_comment ();
534 XmlCommentState.Allowed;
538 $$ = new Attributes (sect);
540 current_attr_target = null;
542 | attribute_sections attribute_section
544 Attributes attrs = $1 as Attributes;
545 ArrayList sect = (ArrayList) $2;
547 if (global_attrs_enabled) {
548 if (current_attr_target == "module") {
549 CodeGen.Module.AddAttributes (sect);
551 } else if (current_attr_target == "assembly") {
552 CodeGen.Assembly.AddAttributes (sect);
556 attrs = new Attributes (sect);
558 attrs.AddAttributes (sect);
562 attrs = new Attributes (sect);
564 attrs.AddAttributes (sect);
567 current_attr_target = null;
572 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
576 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
582 attribute_target_specifier
583 : attribute_target COLON
585 current_attr_target = (string)$1;
593 LocatedToken lt = (LocatedToken) $1;
594 CheckAttributeTarget (lt.Value, lt.Location);
595 $$ = lt.Value; // Location won't be required anymore.
597 | EVENT { $$ = "event"; }
598 | RETURN { $$ = "return"; }
604 ArrayList attrs = new ArrayList (4);
610 | attribute_list COMMA attribute
612 ArrayList attrs = (ArrayList) $1;
620 : attribute_name opt_attribute_arguments
622 MemberName mname = (MemberName) $1;
623 object[] arguments = (object[]) $2;
624 MemberName left = mname.Left;
625 string identifier = mname.Name;
627 Expression left_expr = left == null ? null : left.GetTypeExpression ();
629 if (current_attr_target == "assembly" || current_attr_target == "module")
630 // FIXME: supply "nameEscaped" parameter here.
631 $$ = new GlobalAttribute (current_namespace, current_attr_target,
632 left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
634 $$ = new Attribute (current_attr_target, left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
639 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
642 opt_attribute_arguments
643 : /* empty */ { $$ = null; }
644 | OPEN_PARENS attribute_arguments CLOSE_PARENS
652 : opt_positional_argument_list
657 $$ = new object [] { $1, null };
660 | positional_argument_list COMMA named_argument_list
662 $$ = new object[] { $1, $3 };
664 | named_argument_list
666 $$ = new object [] { null, $1 };
671 opt_positional_argument_list
672 : /* empty */ { $$ = null; }
673 | positional_argument_list
676 positional_argument_list
679 ArrayList args = new ArrayList (4);
680 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
684 | positional_argument_list COMMA expression
686 ArrayList args = (ArrayList) $1;
687 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
696 ArrayList args = new ArrayList (4);
701 | named_argument_list COMMA named_argument
703 ArrayList args = (ArrayList) $1;
708 | named_argument_list COMMA expression
710 Report.Error (1016, ((Expression) $3).Location, "Named attribute argument expected");
716 : IDENTIFIER ASSIGN expression
718 // FIXME: keep location
719 $$ = new DictionaryEntry (
720 ((LocatedToken) $1).Value,
721 new Argument ((Expression) $3, Argument.AType.Expression));
727 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
730 opt_class_member_declarations
732 | class_member_declarations
735 class_member_declarations
736 : class_member_declaration
737 | class_member_declarations
738 class_member_declaration
741 class_member_declaration
742 : constant_declaration // done
743 | field_declaration // done
744 | method_declaration // done
745 | property_declaration // done
746 | event_declaration // done
747 | indexer_declaration // done
748 | operator_declaration // done
749 | constructor_declaration // done
750 | destructor_declaration // done
760 MemberName name = MakeName ((MemberName) $5);
761 current_class = new Struct (
762 current_namespace, current_class, name, (int) $2,
766 current_container = current_container.AddPartial (current_class);
768 current_container.AddClassOrStruct (current_class);
769 current_container = current_class;
775 current_class.Bases = (ArrayList) $7;
777 if (RootContext.Documentation != null)
778 current_container.DocComment = Lexer.consume_doc_comment ();
782 if (RootContext.Documentation != null)
783 Lexer.doc_state = XmlCommentState.Allowed;
787 $$ = pop_current_class ();
789 | opt_attributes opt_modifiers opt_partial STRUCT error {
790 CheckIdentifierToken (yyToken, GetLocation ($5));
797 if (RootContext.Documentation != null)
798 Lexer.doc_state = XmlCommentState.Allowed;
800 opt_struct_member_declarations CLOSE_BRACE
803 opt_struct_member_declarations
805 | struct_member_declarations
808 struct_member_declarations
809 : struct_member_declaration
810 | struct_member_declarations struct_member_declaration
813 struct_member_declaration
814 : constant_declaration
817 | property_declaration
819 | indexer_declaration
820 | operator_declaration
821 | constructor_declaration
825 * This is only included so we can flag error 575:
826 * destructors only allowed on class types
828 | destructor_declaration
839 int modflags = (int) $2;
840 foreach (VariableDeclaration constant in (ArrayList) $5){
841 Location l = constant.Location;
842 if ((modflags & Modifiers.STATIC) != 0) {
843 Report.Error (504, l, "The constant `{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
847 Const c = new Const (
848 current_class, (Expression) $4, (string) constant.identifier,
849 (Expression) constant.expression_or_array_initializer, modflags,
852 if (RootContext.Documentation != null) {
853 c.DocComment = Lexer.consume_doc_comment ();
854 Lexer.doc_state = XmlCommentState.Allowed;
856 current_container.AddConstant (c);
862 : constant_declarator
864 ArrayList constants = new ArrayList (4);
869 | constant_declarators COMMA constant_declarator
872 ArrayList constants = (ArrayList) $1;
879 : IDENTIFIER ASSIGN constant_expression
881 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
885 // A const field requires a value to be provided
886 Report.Error (145, ((LocatedToken) $1).Location, "A const field requires a value to be provided");
898 Expression type = (Expression) $3;
901 current_array_type = null;
903 foreach (VariableDeclaration var in (ArrayList) $4){
904 Field field = new Field (current_class, type, mod, var.identifier,
905 (Attributes) $1, var.Location);
907 field.Initializer = var.expression_or_array_initializer;
909 if (RootContext.Documentation != null) {
910 field.DocComment = Lexer.consume_doc_comment ();
911 Lexer.doc_state = XmlCommentState.Allowed;
913 current_container.AddField (field);
914 $$ = field; // FIXME: might be better if it points to the top item
921 fixed_variable_declarators
924 Expression type = (Expression) $4;
927 current_array_type = null;
929 foreach (VariableDeclaration var in (ArrayList) $5) {
930 FixedField field = new FixedField (current_class, type, mod, var.identifier,
931 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
933 if (RootContext.Documentation != null) {
934 field.DocComment = Lexer.consume_doc_comment ();
935 Lexer.doc_state = XmlCommentState.Allowed;
937 current_container.AddField (field);
938 $$ = field; // FIXME: might be better if it points to the top item
946 current_array_type = null;
947 Report.Error (670, (Location) $3, "Fields cannot have void type");
951 fixed_variable_declarators
952 : fixed_variable_declarator
954 ArrayList decl = new ArrayList (2);
958 | fixed_variable_declarators COMMA fixed_variable_declarator
960 ArrayList decls = (ArrayList) $1;
966 fixed_variable_declarator
967 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
969 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
971 | IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
973 Report.Error (443, lexer.Location, "Value or constant expected");
974 $$ = new VariableDeclaration ((LocatedToken) $1, null);
979 : variable_declarator
981 ArrayList decl = new ArrayList (4);
986 | variable_declarators COMMA variable_declarator
988 ArrayList decls = (ArrayList) $1;
995 : IDENTIFIER ASSIGN variable_initializer
997 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1001 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1003 | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
1005 Report.Error (650, ((LocatedToken) $1).Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
1006 "To declare a fixed size buffer field, use the fixed keyword before the field type");
1011 variable_initializer
1020 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
1022 $$ = new StackAlloc ((Expression) $2, (Expression) $4, (Location) $1);
1026 $$ = new ArglistAccess ((Location) $1);
1030 Report.Error (1575, (Location) $1, "A stackalloc expression requires [] after type");
1037 iterator_container = (IIteratorContainer) $1;
1038 if (RootContext.Documentation != null)
1039 Lexer.doc_state = XmlCommentState.NotAllowed;
1043 Method method = (Method) $1;
1044 method.Block = (ToplevelBlock) $3;
1045 current_container.AddMethod (method);
1047 current_local_parameters = null;
1048 iterator_container = null;
1050 if (RootContext.Documentation != null)
1051 Lexer.doc_state = XmlCommentState.Allowed;
1064 Report.Error (1585, lexer.Location,
1065 "Member modifier `{0}' must precede the member type and name",
1066 Modifiers.Name (i));
1077 type namespace_or_type_name
1078 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1080 MemberName name = (MemberName) $4;
1082 Method method = new Method (current_class, (Expression) $3, (int) $2,
1083 false, name, (Parameters) $6, (Attributes) $1);
1085 current_local_parameters = (Parameters) $6;
1087 if (RootContext.Documentation != null)
1088 method.DocComment = Lexer.consume_doc_comment ();
1094 VOID namespace_or_type_name
1095 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1097 MemberName name = (MemberName) $4;
1099 Method method = new Method (current_class, TypeManager.system_void_expr,
1100 (int) $2, false, name, (Parameters) $6,
1103 current_local_parameters = (Parameters) $6;
1105 if (RootContext.Documentation != null)
1106 method.DocComment = Lexer.consume_doc_comment ();
1113 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1115 MemberName name = (MemberName) $5;
1116 Report.Error (1585, name.Location,
1117 "Member modifier `{0}' must precede the member type and name", Modifiers.Name ((int) $4));
1119 Method method = new Method (current_class, TypeManager.system_void_expr,
1120 0, false, name, (Parameters) $7, (Attributes) $1);
1122 current_local_parameters = (Parameters) $7;
1124 if (RootContext.Documentation != null)
1125 method.DocComment = Lexer.consume_doc_comment ();
1133 | SEMICOLON { $$ = null; }
1136 opt_formal_parameter_list
1137 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1138 | formal_parameter_list
1141 formal_parameter_list
1144 ArrayList pars_list = (ArrayList) $1;
1146 Parameter [] pars = new Parameter [pars_list.Count];
1147 pars_list.CopyTo (pars);
1149 $$ = new Parameters (pars);
1151 | fixed_parameters COMMA parameter_array
1153 ArrayList pars_list = (ArrayList) $1;
1156 Parameter [] pars = new Parameter [pars_list.Count];
1157 pars_list.CopyTo (pars);
1159 $$ = new Parameters (pars);
1161 | fixed_parameters COMMA ARGLIST
1163 ArrayList pars_list = (ArrayList) $1;
1164 //pars_list.Add (new ArglistParameter (GetLocation ($3)));
1166 Parameter [] pars = new Parameter [pars_list.Count];
1167 pars_list.CopyTo (pars);
1169 $$ = new Parameters (pars, true);
1171 | parameter_array COMMA error
1174 Report.Error (231, ((Parameter) $1).Location, "A params parameter must be the last parameter in a formal parameter list");
1177 | fixed_parameters COMMA parameter_array COMMA error
1180 Report.Error (231, ((Parameter) $3).Location, "A params parameter must be the last parameter in a formal parameter list");
1183 | ARGLIST COMMA error
1185 Report.Error (257, (Location) $1, "An __arglist parameter must be the last parameter in a formal parameter list");
1188 | fixed_parameters COMMA ARGLIST COMMA error
1190 Report.Error (257, (Location) $3, "An __arglist parameter must be the last parameter in a formal parameter list");
1195 $$ = new Parameters (new Parameter[] { (Parameter) $1 } );
1199 $$ = new Parameters (new Parameter[0], true);
1206 ArrayList pars = new ArrayList (4);
1211 | fixed_parameters COMMA fixed_parameter
1213 ArrayList pars = (ArrayList) $1;
1222 opt_parameter_modifier
1226 LocatedToken lt = (LocatedToken) $4;
1227 $$ = new Parameter ((Expression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1230 opt_parameter_modifier
1232 IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1234 LocatedToken lt = (LocatedToken) $4;
1235 Report.Error (1552, lt.Location, "Array type specifier, [], must appear before parameter name");
1239 opt_parameter_modifier
1242 Report.Error (1001, GetLocation ($3), "Identifier expected");
1246 opt_parameter_modifier
1249 CheckIdentifierToken (yyToken, GetLocation ($4));
1253 opt_parameter_modifier
1259 LocatedToken lt = (LocatedToken) $4;
1260 Report.Error (241, lt.Location, "Default parameter specifiers are not permitted");
1265 opt_parameter_modifier
1266 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1267 | parameter_modifier
1271 : REF { $$ = Parameter.Modifier.REF; }
1272 | OUT { $$ = Parameter.Modifier.OUT; }
1276 : opt_attributes PARAMS type IDENTIFIER
1278 LocatedToken lt = (LocatedToken) $4;
1279 $$ = new ParamsParameter ((Expression) $3, lt.Value, (Attributes) $1, lt.Location);
1280 note ("type must be a single-dimension array type");
1282 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1284 Report.Error (1611, (Location) $2, "The params parameter cannot be declared as ref or out");
1287 | opt_attributes PARAMS type error {
1288 CheckIdentifierToken (yyToken, GetLocation ($4));
1293 property_declaration
1297 namespace_or_type_name
1299 if (RootContext.Documentation != null)
1300 tmpComment = Lexer.consume_doc_comment ();
1304 implicit_value_parameter_type = (Expression) $3;
1306 lexer.PropertyParsing = true;
1308 accessor_declarations
1310 lexer.PropertyParsing = false;
1311 has_get = has_set = false;
1319 Pair pair = (Pair) $8;
1320 Accessor get_block = (Accessor) pair.First;
1321 Accessor set_block = (Accessor) pair.Second;
1323 MemberName name = (MemberName) $4;
1325 prop = new Property (current_class, (Expression) $3, (int) $2,
1326 false, name, (Attributes) $1, get_block, set_block);
1328 current_container.AddProperty (prop);
1329 implicit_value_parameter_type = null;
1331 if (RootContext.Documentation != null)
1332 prop.DocComment = ConsumeStoredComment ();
1337 accessor_declarations
1338 : get_accessor_declaration
1340 $$ = new Pair ($1, null);
1342 | get_accessor_declaration accessor_declarations
1344 Pair pair = (Pair) $2;
1348 | set_accessor_declaration
1350 $$ = new Pair (null, $1);
1352 | set_accessor_declaration accessor_declarations
1354 Pair pair = (Pair) $2;
1360 Report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1365 get_accessor_declaration
1366 : opt_attributes opt_modifiers GET
1368 // If this is not the case, then current_local_parameters has already
1369 // been set in indexer_declaration
1370 if (parsing_indexer == false)
1371 current_local_parameters = null;
1373 current_local_parameters = indexer_parameters;
1374 lexer.PropertyParsing = false;
1376 iterator_container = SimpleIteratorContainer.GetSimple ();
1381 Report.Error (1007, (Location) $3, "Property accessor already defined");
1384 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1386 current_local_parameters = null;
1387 lexer.PropertyParsing = true;
1389 if (SimpleIteratorContainer.Simple.Yields)
1390 accessor.SetYields ();
1392 iterator_container = null;
1394 if (RootContext.Documentation != null)
1395 if (Lexer.doc_state == XmlCommentState.Error)
1396 Lexer.doc_state = XmlCommentState.NotAllowed;
1402 set_accessor_declaration
1403 : opt_attributes opt_modifiers SET
1406 Parameter implicit_value_parameter = new Parameter (
1407 implicit_value_parameter_type, "value",
1408 Parameter.Modifier.NONE, null, (Location) $3);
1410 if (parsing_indexer == false) {
1411 args = new Parameter [1];
1412 args [0] = implicit_value_parameter;
1413 current_local_parameters = new Parameters (args);
1415 Parameter [] fpars = indexer_parameters.FixedParameters;
1418 int count = fpars.Length;
1420 args = new Parameter [count + 1];
1421 fpars.CopyTo (args, 0);
1422 args [count] = implicit_value_parameter;
1425 current_local_parameters = new Parameters (
1429 lexer.PropertyParsing = false;
1431 iterator_container = SimpleIteratorContainer.GetSimple ();
1436 Report.Error (1007, ((LocatedToken) $3).Location, "Property accessor already defined");
1439 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1441 current_local_parameters = null;
1442 lexer.PropertyParsing = true;
1444 if (SimpleIteratorContainer.Simple.Yields)
1445 accessor.SetYields ();
1447 iterator_container = null;
1449 if (RootContext.Documentation != null
1450 && Lexer.doc_state == XmlCommentState.Error)
1451 Lexer.doc_state = XmlCommentState.NotAllowed;
1459 | SEMICOLON { $$ = null; }
1462 interface_declaration
1466 INTERFACE member_name
1468 MemberName name = MakeName ((MemberName) $5);
1470 current_class = new Interface (
1471 current_namespace, current_class, name, (int) $2,
1475 current_container = current_container.AddPartial (current_class);
1477 current_container.AddInterface (current_class);
1478 current_container = current_class;
1483 current_class.Bases = (ArrayList) $7;
1485 if (RootContext.Documentation != null) {
1486 current_container.DocComment = Lexer.consume_doc_comment ();
1487 Lexer.doc_state = XmlCommentState.Allowed;
1492 if (RootContext.Documentation != null)
1493 Lexer.doc_state = XmlCommentState.Allowed;
1497 $$ = pop_current_class ();
1499 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1500 CheckIdentifierToken (yyToken, GetLocation ($5));
1506 opt_interface_member_declarations
1510 opt_interface_member_declarations
1512 | interface_member_declarations
1515 interface_member_declarations
1516 : interface_member_declaration
1517 | interface_member_declarations interface_member_declaration
1520 interface_member_declaration
1521 : interface_method_declaration
1526 Method m = (Method) $1;
1528 if (m.IsExplicitImpl)
1529 Report.Error (541, m.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1530 m.GetSignatureForError ());
1532 current_container.AddMethod (m);
1534 if (RootContext.Documentation != null)
1535 Lexer.doc_state = XmlCommentState.Allowed;
1537 | interface_property_declaration
1542 Property p = (Property) $1;
1544 if (p.IsExplicitImpl)
1545 Report.Error (541, p.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1546 p.GetSignatureForError ());
1548 current_container.AddProperty (p);
1550 if (RootContext.Documentation != null)
1551 Lexer.doc_state = XmlCommentState.Allowed;
1553 | interface_event_declaration
1556 Event e = (Event) $1;
1558 if (e.IsExplicitImpl)
1559 Report.Error (541, e.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1560 e.GetSignatureForError ());
1562 current_container.AddEvent (e);
1565 if (RootContext.Documentation != null)
1566 Lexer.doc_state = XmlCommentState.Allowed;
1568 | interface_indexer_declaration
1573 Indexer i = (Indexer) $1;
1575 if (i.IsExplicitImpl)
1576 Report.Error (541, i.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1577 i.GetSignatureForError ());
1579 current_container.AddIndexer (i);
1581 if (RootContext.Documentation != null)
1582 Lexer.doc_state = XmlCommentState.Allowed;
1584 | delegate_declaration
1587 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1588 ((MemberCore)$1).GetSignatureForError ());
1594 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1595 ((MemberCore)$1).GetSignatureForError ());
1598 | struct_declaration
1601 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1602 ((MemberCore)$1).GetSignatureForError ());
1608 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1609 ((MemberCore)$1).GetSignatureForError ());
1612 | interface_declaration
1615 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1616 ((MemberCore)$1).GetSignatureForError ());
1619 | constant_declaration
1621 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1629 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, GetLocation ($1));
1634 interface_method_declaration
1635 : opt_attributes opt_new type namespace_or_type_name
1636 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1639 $$ = new Method (current_class, (Expression) $3, (int) $2, true,
1640 (MemberName) $4, (Parameters) $6, (Attributes) $1);
1641 if (RootContext.Documentation != null)
1642 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1644 | opt_attributes opt_new type namespace_or_type_name
1645 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1646 OPEN_BRACE opt_statement_list CLOSE_BRACE
1648 $$ = new Method (current_class, (Expression) $3, (int) $2, true,
1649 (MemberName) $4, (Parameters) $6, (Attributes) $1);
1651 Report.Error (531, lexer.Location, "`{0}': interface members cannot have a definition",
1652 ((Method)$$).GetSignatureForError ());
1656 interface_property_declaration
1661 { lexer.PropertyParsing = true; }
1662 accessor_declarations
1664 has_get = has_set = false;
1665 lexer.PropertyParsing = false;
1669 LocatedToken lt = (LocatedToken) $4;
1670 MemberName name = new MemberName (lt.Value, lt.Location);
1672 if ($3 == TypeManager.system_void_expr) {
1673 Report.Error (547, lt.Location, "`{0}': property or indexer cannot have void type", lt.Value);
1679 p = new Property (current_class, (Expression) $3, (int) $2, true,
1680 name, (Attributes) $1,
1683 Report.Error (548, p.Location, "`{0}': property or indexer must have at least one accessor", p.GetSignatureForError ());
1687 Pair pair = (Pair) $7;
1688 p = new Property (current_class, (Expression) $3, (int) $2, true,
1689 name, (Attributes) $1,
1690 (Accessor)pair.First, (Accessor)pair.Second);
1692 if (pair.First != null && ((Accessor)(pair.First)).Block != null) {
1693 Report.Error (531, p.Location, "`{0}.get': interface members cannot have a definition", p.GetSignatureForError ());
1698 if (pair.Second != null && ((Accessor)(pair.Second)).Block != null) {
1699 Report.Error (531, p.Location, "`{0}.set': interface members cannot have a definition", p.GetSignatureForError ());
1704 if (RootContext.Documentation != null)
1705 p.DocComment = Lexer.consume_doc_comment ();
1712 CheckIdentifierToken (yyToken, GetLocation ($4));
1718 interface_event_declaration
1719 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1721 LocatedToken lt = (LocatedToken) $5;
1722 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1723 new MemberName (lt.Value, lt.Location),
1725 if (RootContext.Documentation != null)
1726 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1728 | opt_attributes opt_new EVENT type error {
1729 CheckIdentifierToken (yyToken, GetLocation ($5));
1732 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1733 LocatedToken lt = (LocatedToken) $5;
1734 Report.Error (68, lt.Location, "`{0}.{1}': event in interface cannot have initializer", current_container.Name, lt.Value);
1737 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1739 lexer.EventParsing = true;
1741 event_accessor_declarations
1743 lexer.EventParsing = false;
1746 Report.Error (69, (Location) $3, "Event in interface cannot have add or remove accessors");
1751 interface_indexer_declaration
1752 : opt_attributes opt_new type THIS
1753 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1755 { lexer.PropertyParsing = true; }
1756 accessor_declarations
1758 has_get = has_set = false;
1759 lexer.PropertyParsing = false;
1765 i = new Indexer (current_class, (Expression) $3,
1766 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1767 (int) $2, true, (Parameters) $6, (Attributes) $1,
1770 Report.Error (548, i.Location, "`{0}': property or indexer must have at least one accessor", i.GetSignatureForError ());
1774 Pair pair = (Pair) $10;
1775 i = new Indexer (current_class, (Expression) $3,
1776 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1777 (int) $2, true, (Parameters) $6, (Attributes) $1,
1778 (Accessor)pair.First, (Accessor)pair.Second);
1780 if (pair.First != null && ((Accessor)(pair.First)).Block != null) {
1781 Report.Error (531, i.Location, "`{0}.get': interface members cannot have a definition", i.GetSignatureForError ());
1786 if (pair.Second != null && ((Accessor)(pair.Second)).Block != null) {
1787 Report.Error (531, i.Location, "`{0}.set': interface members cannot have a definition", i.GetSignatureForError ());
1792 if (RootContext.Documentation != null)
1793 i.DocComment = ConsumeStoredComment ();
1799 operator_declaration
1800 : opt_attributes opt_modifiers operator_declarator
1802 iterator_container = SimpleIteratorContainer.GetSimple ();
1809 OperatorDeclaration decl = (OperatorDeclaration) $3;
1811 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1813 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null, decl.location);
1814 if (decl.arg2type != null)
1815 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null, decl.location);
1817 Operator op = new Operator (
1818 current_class, decl.optype, decl.ret_type, (int) $2,
1819 new Parameters (param_list),
1820 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1822 if (RootContext.Documentation != null) {
1823 op.DocComment = tmpComment;
1824 Lexer.doc_state = XmlCommentState.Allowed;
1827 if (SimpleIteratorContainer.Simple.Yields)
1830 // Note again, checking is done in semantic analysis
1831 current_container.AddOperator (op);
1833 current_local_parameters = null;
1834 iterator_container = null;
1840 | SEMICOLON { $$ = null; }
1843 : type OPERATOR overloadable_operator
1844 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1846 LocatedToken lt = (LocatedToken) $6;
1847 Operator.OpType op = (Operator.OpType) $3;
1848 CheckUnaryOperator (op, lt.Location);
1850 if (op == Operator.OpType.Addition)
1851 op = Operator.OpType.UnaryPlus;
1853 if (op == Operator.OpType.Subtraction)
1854 op = Operator.OpType.UnaryNegation;
1856 Parameter [] pars = new Parameter [1];
1857 Expression type = (Expression) $5;
1859 pars [0] = new Parameter (type, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1861 current_local_parameters = new Parameters (pars);
1863 if (RootContext.Documentation != null) {
1864 tmpComment = Lexer.consume_doc_comment ();
1865 Lexer.doc_state = XmlCommentState.NotAllowed;
1868 $$ = new OperatorDeclaration (op, (Expression) $1, type, lt.Value,
1869 null, null, (Location) $2);
1871 | type OPERATOR overloadable_operator
1873 type IDENTIFIER COMMA
1877 LocatedToken ltParam1 = (LocatedToken) $6;
1878 LocatedToken ltParam2 = (LocatedToken) $9;
1879 CheckBinaryOperator ((Operator.OpType) $3, (Location) $2);
1881 Parameter [] pars = new Parameter [2];
1883 Expression typeL = (Expression) $5;
1884 Expression typeR = (Expression) $8;
1886 pars [0] = new Parameter (typeL, ltParam1.Value, Parameter.Modifier.NONE, null, ltParam1.Location);
1887 pars [1] = new Parameter (typeR, ltParam2.Value, Parameter.Modifier.NONE, null, ltParam2.Location);
1889 current_local_parameters = new Parameters (pars);
1891 if (RootContext.Documentation != null) {
1892 tmpComment = Lexer.consume_doc_comment ();
1893 Lexer.doc_state = XmlCommentState.NotAllowed;
1896 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1897 typeL, ltParam1.Value,
1898 typeR, ltParam2.Value, (Location) $2);
1900 | conversion_operator_declarator
1901 | type OPERATOR overloadable_operator
1903 type IDENTIFIER COMMA
1904 type IDENTIFIER COMMA
1908 Report.Error (1534, (Location) $2, "Overloaded binary operator `{0}' takes two parameters",
1909 Operator.GetName ((Operator.OpType) $3));
1912 | type OPERATOR overloadable_operator
1913 OPEN_PARENS CLOSE_PARENS
1915 Report.Error (1535, (Location) $2, "Overloaded unary operator `{0}' takes one parameter",
1916 Operator.GetName ((Operator.OpType) $3));
1921 overloadable_operator
1923 : BANG { $$ = Operator.OpType.LogicalNot; }
1924 | TILDE { $$ = Operator.OpType.OnesComplement; }
1925 | OP_INC { $$ = Operator.OpType.Increment; }
1926 | OP_DEC { $$ = Operator.OpType.Decrement; }
1927 | TRUE { $$ = Operator.OpType.True; }
1928 | FALSE { $$ = Operator.OpType.False; }
1929 // Unary and binary:
1930 | PLUS { $$ = Operator.OpType.Addition; }
1931 | MINUS { $$ = Operator.OpType.Subtraction; }
1933 | STAR { $$ = Operator.OpType.Multiply; }
1934 | DIV { $$ = Operator.OpType.Division; }
1935 | PERCENT { $$ = Operator.OpType.Modulus; }
1936 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1937 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1938 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1939 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1940 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1941 | OP_EQ { $$ = Operator.OpType.Equality; }
1942 | OP_NE { $$ = Operator.OpType.Inequality; }
1943 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1944 | OP_LT { $$ = Operator.OpType.LessThan; }
1945 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1946 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1949 conversion_operator_declarator
1950 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1952 LocatedToken lt = (LocatedToken) $6;
1953 Parameter [] pars = new Parameter [1];
1955 pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1957 current_local_parameters = new Parameters (pars);
1959 if (RootContext.Documentation != null) {
1960 tmpComment = Lexer.consume_doc_comment ();
1961 Lexer.doc_state = XmlCommentState.NotAllowed;
1964 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, lt.Value,
1965 null, null, (Location) $2);
1967 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1969 LocatedToken lt = (LocatedToken) $6;
1970 Parameter [] pars = new Parameter [1];
1972 pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1974 current_local_parameters = new Parameters (pars);
1976 if (RootContext.Documentation != null) {
1977 tmpComment = Lexer.consume_doc_comment ();
1978 Lexer.doc_state = XmlCommentState.NotAllowed;
1981 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, lt.Value,
1982 null, null, (Location) $2);
1986 syntax_error ((Location) $1, "'operator' expected");
1990 syntax_error ((Location) $1, "'operator' expected");
1994 constructor_declaration
1997 constructor_declarator
2000 Constructor c = (Constructor) $3;
2001 c.Block = (ToplevelBlock) $4;
2002 c.OptAttributes = (Attributes) $1;
2003 c.ModFlags = (int) $2;
2005 if (RootContext.Documentation != null)
2006 c.DocComment = ConsumeStoredComment ();
2008 if (c.Name == current_container.Basename){
2009 if ((c.ModFlags & Modifiers.STATIC) != 0){
2010 if ((c.ModFlags & Modifiers.Accessibility) != 0){
2011 Report.Error (515, c.Location,
2012 "`{0}': access modifiers are not allowed on static constructors",
2013 c.GetSignatureForError ());
2016 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2018 if (c.Initializer != null){
2019 Report.Error (514, c.Location,
2020 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2021 c.GetSignatureForError ());
2024 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2027 // We let another layer check the validity of the constructor.
2028 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
2031 current_container.AddConstructor (c);
2033 current_local_parameters = null;
2034 if (RootContext.Documentation != null)
2035 Lexer.doc_state = XmlCommentState.Allowed;
2039 constructor_declarator
2042 if (RootContext.Documentation != null) {
2043 tmpComment = Lexer.consume_doc_comment ();
2044 Lexer.doc_state = XmlCommentState.Allowed;
2047 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2049 current_local_parameters = (Parameters) $4;
2051 opt_constructor_initializer
2053 LocatedToken lt = (LocatedToken) $1;
2054 $$ = new Constructor (current_class, lt.Value, 0, (Parameters) $4,
2055 (ConstructorInitializer) $7, lt.Location);
2061 | SEMICOLON { $$ = null; }
2064 opt_constructor_initializer
2065 : /* empty */ { $$ = null; }
2066 | constructor_initializer
2069 constructor_initializer
2070 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
2072 $$ = new ConstructorBaseInitializer ((ArrayList) $4, (Location) $2);
2074 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
2076 $$ = new ConstructorThisInitializer ((ArrayList) $4, (Location) $2);
2079 Report.Error (1018, (Location) $1, "Keyword this or base expected");
2085 : /* EMPTY */ { $$ = 0; }
2086 | UNSAFE { $$ = Modifiers.UNSAFE; }
2087 | EXTERN { $$ = Modifiers.EXTERN; }
2090 destructor_declaration
2091 : opt_attributes opt_finalizer TILDE
2093 if (RootContext.Documentation != null) {
2094 tmpComment = Lexer.consume_doc_comment ();
2095 Lexer.doc_state = XmlCommentState.NotAllowed;
2098 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2100 LocatedToken lt = (LocatedToken) $5;
2101 if (lt.Value != current_container.Basename){
2102 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2103 } else if (current_container.Kind != Kind.Class){
2104 Report.Error (575, lt.Location, "Only class types can contain destructor");
2106 Location l = lt.Location;
2109 if (!RootContext.StdLib && current_container.Name == "System.Object")
2110 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2112 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2114 Method d = new Destructor (
2115 current_class, TypeManager.system_void_expr, m, "Finalize",
2116 Parameters.EmptyReadOnlyParameters, (Attributes) $1, l);
2117 if (RootContext.Documentation != null)
2118 d.DocComment = ConsumeStoredComment ();
2120 d.Block = (ToplevelBlock) $8;
2121 current_container.AddMethod (d);
2129 EVENT type variable_declarators SEMICOLON
2131 current_array_type = null;
2132 foreach (VariableDeclaration var in (ArrayList) $5) {
2134 MemberName name = new MemberName (var.identifier,
2137 EventField e = new EventField (
2138 current_class, (Expression) $4, (int) $2, false, name,
2141 e.Initializer = var.expression_or_array_initializer;
2143 current_container.AddEvent (e);
2145 if (RootContext.Documentation != null) {
2146 e.DocComment = Lexer.consume_doc_comment ();
2147 Lexer.doc_state = XmlCommentState.Allowed;
2153 EVENT type namespace_or_type_name
2156 implicit_value_parameter_type = (Expression) $4;
2157 lexer.EventParsing = true;
2159 event_accessor_declarations
2161 lexer.EventParsing = false;
2165 MemberName name = (MemberName) $5;
2168 Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2169 current_container.Name, name.ToString ());
2172 Pair pair = (Pair) $8;
2173 if (pair.First == null || pair.Second == null)
2174 // CS0073 is already reported, so no CS0065 here.
2177 Event e = new EventProperty (
2178 current_class, (Expression) $4, (int) $2, false, name,
2179 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second);
2180 if (RootContext.Documentation != null) {
2181 e.DocComment = Lexer.consume_doc_comment ();
2182 Lexer.doc_state = XmlCommentState.Allowed;
2185 current_container.AddEvent (e);
2186 implicit_value_parameter_type = null;
2190 | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
2191 MemberName mn = (MemberName) $5;
2193 if (mn.Left != null)
2194 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2196 Report.Error (71, mn.Location, "Event declaration should use property syntax");
2198 if (RootContext.Documentation != null)
2199 Lexer.doc_state = XmlCommentState.Allowed;
2203 event_accessor_declarations
2204 : add_accessor_declaration remove_accessor_declaration
2206 $$ = new Pair ($1, $2);
2208 | remove_accessor_declaration add_accessor_declaration
2210 $$ = new Pair ($2, $1);
2212 | add_accessor_declaration { $$ = null; }
2213 | remove_accessor_declaration { $$ = null; }
2216 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2222 add_accessor_declaration
2223 : opt_attributes ADD
2225 Parameter [] args = new Parameter [1];
2226 Parameter implicit_value_parameter = new Parameter (
2227 implicit_value_parameter_type, "value",
2228 Parameter.Modifier.NONE, null, (Location) $2);
2230 args [0] = implicit_value_parameter;
2232 current_local_parameters = new Parameters (args);
2233 lexer.EventParsing = false;
2237 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2238 lexer.EventParsing = true;
2240 | opt_attributes ADD error {
2241 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2244 | opt_attributes modifiers ADD {
2245 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2250 remove_accessor_declaration
2251 : opt_attributes REMOVE
2253 Parameter [] args = new Parameter [1];
2254 Parameter implicit_value_parameter = new Parameter (
2255 implicit_value_parameter_type, "value",
2256 Parameter.Modifier.NONE, null, (Location) $2);
2258 args [0] = implicit_value_parameter;
2260 current_local_parameters = new Parameters (args);
2261 lexer.EventParsing = false;
2265 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2266 lexer.EventParsing = true;
2268 | opt_attributes REMOVE error {
2269 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2272 | opt_attributes modifiers REMOVE {
2273 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2279 : opt_attributes opt_modifiers indexer_declarator
2282 IndexerDeclaration decl = (IndexerDeclaration) $3;
2284 implicit_value_parameter_type = decl.type;
2286 lexer.PropertyParsing = true;
2287 parsing_indexer = true;
2289 indexer_parameters = decl.param_list;
2291 accessor_declarations
2293 lexer.PropertyParsing = false;
2294 has_get = has_set = false;
2295 parsing_indexer = false;
2302 // The signature is computed from the signature of the indexer. Look
2303 // at section 3.6 on the spec
2305 IndexerDeclaration decl = (IndexerDeclaration) $3;
2306 Location loc = decl.location;
2307 Pair pair = (Pair) $6;
2308 Accessor get_block = (Accessor) pair.First;
2309 Accessor set_block = (Accessor) pair.Second;
2312 if (decl.interface_type != null)
2313 name = new MemberName (decl.interface_type, TypeContainer.DefaultIndexerName, loc);
2315 name = new MemberName (TypeContainer.DefaultIndexerName, loc);
2317 indexer = new Indexer (current_class, decl.type, name,
2318 (int) $2, false, decl.param_list, (Attributes) $1,
2319 get_block, set_block);
2321 if (RootContext.Documentation != null)
2322 indexer.DocComment = ConsumeStoredComment ();
2324 current_container.AddIndexer (indexer);
2326 current_local_parameters = null;
2327 implicit_value_parameter_type = null;
2328 indexer_parameters = null;
2333 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2335 Parameters pars = (Parameters) $4;
2336 if (pars.HasArglist) {
2337 // "__arglist is not valid in this context"
2338 Report.Error (1669, (Location) $2, "__arglist is not valid in this context");
2339 } else if (pars.Empty){
2340 Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
2342 if (RootContext.Documentation != null) {
2343 tmpComment = Lexer.consume_doc_comment ();
2344 Lexer.doc_state = XmlCommentState.Allowed;
2347 $$ = new IndexerDeclaration ((Expression) $1, null, pars, (Location) $2);
2349 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2351 Parameters pars = (Parameters) $6;
2353 if (pars.HasArglist) {
2354 // "__arglist is not valid in this context"
2355 Report.Error (1669, (Location) $4, "__arglist is not valid in this context");
2356 } else if (pars.Empty){
2357 Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
2360 MemberName name = (MemberName) $2;
2361 $$ = new IndexerDeclaration ((Expression) $1, name, pars, (Location) $4);
2363 if (RootContext.Documentation != null) {
2364 tmpComment = Lexer.consume_doc_comment ();
2365 Lexer.doc_state = XmlCommentState.Allowed;
2376 if (RootContext.Documentation != null)
2377 enumTypeComment = Lexer.consume_doc_comment ();
2382 LocatedToken lt = (LocatedToken) $5;
2383 Location enum_location = lt.Location;
2386 Report.Error (267, lt.Location, "The partial modifier can only appear immediately before `class', `struct' or `interface'");
2387 break; // assumes that the parser put us in a switch
2390 MemberName name = MakeName (new MemberName (lt.Value, enum_location));
2391 Enum e = new Enum (current_namespace, current_class, (Expression) $6, (int) $2,
2392 name, (Attributes) $1);
2394 if (RootContext.Documentation != null)
2395 e.DocComment = enumTypeComment;
2398 EnumMember em = null;
2399 foreach (VariableDeclaration ev in (ArrayList) $8) {
2400 em = new EnumMember (e, em, (Expression) ev.expression_or_array_initializer,
2401 new MemberName (ev.identifier, ev.Location), ev.OptAttributes);
2403 // if (RootContext.Documentation != null)
2404 em.DocComment = ev.DocComment;
2406 e.AddEnumMember (em);
2409 current_container.AddEnum (e);
2410 RootContext.Tree.RecordDecl (current_namespace.NS, name, e);
2417 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2418 | COLON type { $$ = $2; }
2424 if (RootContext.Documentation != null)
2425 Lexer.doc_state = XmlCommentState.Allowed;
2427 opt_enum_member_declarations
2429 // here will be evaluated after CLOSE_BLACE is consumed.
2430 if (RootContext.Documentation != null)
2431 Lexer.doc_state = XmlCommentState.Allowed;
2439 opt_enum_member_declarations
2440 : /* empty */ { $$ = new ArrayList (4); }
2441 | enum_member_declarations opt_comma { $$ = $1; }
2444 enum_member_declarations
2445 : enum_member_declaration
2447 ArrayList l = new ArrayList (4);
2452 | enum_member_declarations COMMA enum_member_declaration
2454 ArrayList l = (ArrayList) $1;
2462 enum_member_declaration
2463 : opt_attributes IDENTIFIER
2465 VariableDeclaration vd = new VariableDeclaration (
2466 (LocatedToken) $2, null, (Attributes) $1);
2468 if (RootContext.Documentation != null) {
2469 vd.DocComment = Lexer.consume_doc_comment ();
2470 Lexer.doc_state = XmlCommentState.Allowed;
2475 | opt_attributes IDENTIFIER
2477 if (RootContext.Documentation != null) {
2478 tmpComment = Lexer.consume_doc_comment ();
2479 Lexer.doc_state = XmlCommentState.NotAllowed;
2484 VariableDeclaration vd = new VariableDeclaration (
2485 (LocatedToken) $2, $5, (Attributes) $1);
2487 if (RootContext.Documentation != null)
2488 vd.DocComment = ConsumeStoredComment ();
2494 delegate_declaration
2497 DELEGATE type member_name
2498 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2501 MemberName name = MakeName ((MemberName) $5);
2502 Parameters p = (Parameters) $7;
2504 // TODO: wrong location
2505 Report.Error (1669, name.Location, "__arglist is not valid in this context");
2508 Delegate del = new Delegate (current_namespace, current_class, (Expression) $4,
2509 (int) $2, name, p, (Attributes) $1);
2511 if (RootContext.Documentation != null) {
2512 del.DocComment = Lexer.consume_doc_comment ();
2513 Lexer.doc_state = XmlCommentState.Allowed;
2516 current_container.AddDelegate (del);
2517 RootContext.Tree.RecordDecl (current_namespace.NS, name, del);
2522 namespace_or_type_name
2524 | IDENTIFIER DOUBLE_COLON IDENTIFIER {
2525 LocatedToken lt1 = (LocatedToken) $1;
2526 LocatedToken lt2 = (LocatedToken) $3;
2527 $$ = new MemberName (lt1.Value, lt2.Value, lt2.Location);
2529 | namespace_or_type_name DOT IDENTIFIER {
2530 LocatedToken lt = (LocatedToken) $3;
2531 $$ = new MemberName ((MemberName) $1, lt.Value);
2537 LocatedToken lt = (LocatedToken) $1;
2538 $$ = new MemberName (lt.Value, lt.Location);
2543 * Before you think of adding a return_type, notice that we have been
2544 * using two rules in the places where it matters (one rule using type
2545 * and another identical one that uses VOID as the return type). This
2546 * gets rid of a shift/reduce couple
2549 : namespace_or_type_name
2551 MemberName name = (MemberName) $1;
2552 $$ = name.GetTypeExpression ();
2564 // Note that here only unmanaged types are allowed but we
2565 // can't perform checks during this phase - we do it during
2566 // semantic analysis.
2568 $$ = new ComposedCast ((Expression) $1, "*", Lexer.Location);
2572 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2578 | non_expression_type rank_specifier
2580 Location loc = GetLocation ($1);
2582 loc = lexer.Location;
2583 $$ = new ComposedCast ((Expression) $1, (string) $2, loc);
2585 | non_expression_type STAR
2587 Location loc = GetLocation ($1);
2589 loc = lexer.Location;
2590 $$ = new ComposedCast ((Expression) $1, "*", loc);
2592 | expression rank_specifiers
2594 $$ = new ComposedCast ((Expression) $1, (string) $2);
2598 $$ = new ComposedCast ((Expression) $1, "*");
2602 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2603 // through this different path
2605 | multiplicative_expression STAR
2607 $$ = new ComposedCast ((Expression) $1, "*");
2614 ArrayList types = new ArrayList (4);
2619 | type_list COMMA type
2621 ArrayList types = (ArrayList) $1;
2629 * replaces all the productions for isolating the various
2630 * simple types, but we need this to reuse it easily in local_variable_type
2633 : OBJECT { $$ = TypeManager.system_object_expr; }
2634 | STRING { $$ = TypeManager.system_string_expr; }
2635 | BOOL { $$ = TypeManager.system_boolean_expr; }
2636 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2637 | FLOAT { $$ = TypeManager.system_single_expr; }
2638 | DOUBLE { $$ = TypeManager.system_double_expr; }
2643 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2644 | BYTE { $$ = TypeManager.system_byte_expr; }
2645 | SHORT { $$ = TypeManager.system_int16_expr; }
2646 | USHORT { $$ = TypeManager.system_uint16_expr; }
2647 | INT { $$ = TypeManager.system_int32_expr; }
2648 | UINT { $$ = TypeManager.system_uint32_expr; }
2649 | LONG { $$ = TypeManager.system_int64_expr; }
2650 | ULONG { $$ = TypeManager.system_uint64_expr; }
2651 | CHAR { $$ = TypeManager.system_char_expr; }
2652 | VOID { $$ = TypeManager.system_void_expr; }
2656 : type rank_specifiers
2658 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2);
2663 // Expressions, section 7.5
2673 MemberName mn = (MemberName) $1;
2674 $$ = mn.GetTypeExpression ();
2676 | IDENTIFIER DOUBLE_COLON IDENTIFIER
2678 LocatedToken lt1 = (LocatedToken) $1;
2679 LocatedToken lt2 = (LocatedToken) $3;
2680 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, lt2.Location);
2682 | parenthesized_expression
2684 | invocation_expression
2688 | post_increment_expression
2689 | post_decrement_expression
2693 | checked_expression
2694 | unchecked_expression
2695 | pointer_member_access
2696 | anonymous_method_expression
2703 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
2704 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); }
2705 | NULL { $$ = new NullLiteral (lexer.Location); }
2709 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
2710 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
2711 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
2716 object v = lexer.Value;
2719 $$ = new IntLiteral ((int) v, lexer.Location);
2720 } else if (v is uint)
2721 $$ = new UIntLiteral ((UInt32) v, lexer.Location);
2723 $$ = new LongLiteral ((Int64) v, lexer.Location);
2724 else if (v is ulong)
2725 $$ = new ULongLiteral ((UInt64) v, lexer.Location);
2727 Console.WriteLine ("OOPS. Unexpected result from scanner");
2732 : TRUE { $$ = new BoolLiteral (true, lexer.Location); }
2733 | FALSE { $$ = new BoolLiteral (false, lexer.Location); }
2736 parenthesized_expression_0
2737 : OPEN_PARENS expression CLOSE_PARENS
2740 lexer.Deambiguate_CloseParens ();
2741 // After this, the next token returned is one of
2742 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2743 // or CLOSE_PARENS_MINUS.
2745 | OPEN_PARENS expression error { CheckToken (1026, yyToken, "Expecting ')'", lexer.Location); }
2748 parenthesized_expression
2749 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2753 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2755 // If a parenthesized expression is followed by a minus, we need to wrap
2756 // the expression inside a ParenthesizedExpression for the CS0075 check
2757 // in Binary.DoResolve().
2758 $$ = new ParenthesizedExpression ((Expression) $1);
2763 : primary_expression DOT IDENTIFIER
2765 LocatedToken lt = (LocatedToken) $3;
2766 $$ = new MemberAccess ((Expression) $1, lt.Value);
2768 | predefined_type DOT IDENTIFIER
2770 LocatedToken lt = (LocatedToken) $3;
2771 $$ = new MemberAccess ((Expression) $1, lt.Value);
2779 invocation_expression
2780 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2783 Report.Error (1, (Location) $2, "Parse error");
2785 $$ = new Invocation ((Expression) $1, (ArrayList) $3);
2787 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2789 $$ = new Invocation ((Expression) $1, new ArrayList ());
2791 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2793 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
2795 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2797 ArrayList args = new ArrayList (1);
2799 $$ = new Invocation ((Expression) $1, args);
2801 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2803 ArrayList args = ((ArrayList) $4);
2805 $$ = new Invocation ((Expression) $1, args);
2810 : /* empty */ { $$ = null; }
2817 ArrayList list = new ArrayList (4);
2821 | argument_list COMMA argument
2823 ArrayList list = (ArrayList) $1;
2827 | argument_list error {
2828 CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
2836 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2838 | non_simple_argument
2845 : REF variable_reference
2847 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2849 | OUT variable_reference
2851 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2853 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2855 ArrayList list = (ArrayList) $3;
2856 Argument[] args = new Argument [list.Count];
2857 list.CopyTo (args, 0);
2859 Expression expr = new Arglist (args, (Location) $1);
2860 $$ = new Argument (expr, Argument.AType.Expression);
2864 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
2869 : expression { note ("section 5.4"); $$ = $1; }
2873 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2875 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
2877 | primary_expression rank_specifiers
2879 // So the super-trick is that primary_expression
2880 // can only be either a SimpleName or a MemberAccess.
2881 // The MemberAccess case arises when you have a fully qualified type-name like :
2883 // SimpleName is when you have
2886 Expression expr = (Expression) $1;
2887 if (expr is ComposedCast){
2888 $$ = new ComposedCast (expr, (string) $2);
2889 } else if (!(expr is SimpleName || expr is MemberAccess || expr is QualifiedAliasMember)){
2890 Error_ExpectingTypeName (expr);
2891 $$ = TypeManager.system_object_expr;
2894 // So we extract the string corresponding to the SimpleName
2897 $$ = new ComposedCast (expr, (string) $2);
2899 current_array_type = (Expression)$$;
2906 ArrayList list = new ArrayList (4);
2910 | expression_list COMMA expression
2912 ArrayList list = (ArrayList) $1;
2921 $$ = new This (current_block, (Location) $1);
2926 : BASE DOT IDENTIFIER
2928 LocatedToken lt = (LocatedToken) $3;
2929 $$ = new BaseAccess (lt.Value, lt.Location);
2931 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2933 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
2936 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
2941 post_increment_expression
2942 : primary_expression OP_INC
2944 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2945 (Expression) $1, (Location) $2);
2949 post_decrement_expression
2950 : primary_expression OP_DEC
2952 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2953 (Expression) $1, (Location) $2);
2958 : object_or_delegate_creation_expression
2959 | array_creation_expression
2962 object_or_delegate_creation_expression
2963 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2965 $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
2969 array_creation_expression
2970 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2972 opt_array_initializer
2974 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
2976 | NEW type rank_specifiers array_initializer
2978 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, (Location) $1);
2982 Report.Error (1031, (Location) $1, "Type expected");
2987 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
3004 : rank_specifier opt_rank_specifier
3006 $$ = (string) $2 + (string) $1;
3011 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3013 $$ = "[" + (string) $2 + "]";
3033 | dim_separators COMMA
3035 $$ = (string) $1 + ",";
3039 opt_array_initializer
3051 : OPEN_BRACE CLOSE_BRACE
3053 ArrayList list = new ArrayList (4);
3056 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3058 $$ = (ArrayList) $2;
3062 variable_initializer_list
3063 : variable_initializer
3065 ArrayList list = new ArrayList (4);
3069 | variable_initializer_list COMMA variable_initializer
3071 ArrayList list = (ArrayList) $1;
3078 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
3080 $$ = new TypeOfVoid ((Location) $1);
3082 | TYPEOF OPEN_PARENS type CLOSE_PARENS
3084 $$ = new TypeOf ((Expression) $3, (Location) $1);
3089 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3090 $$ = new SizeOf ((Expression) $3, (Location) $1);
3095 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3097 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3101 unchecked_expression
3102 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3104 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3108 pointer_member_access
3109 : primary_expression OP_PTR IDENTIFIER
3112 LocatedToken lt = (LocatedToken) $3;
3114 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
3115 $$ = new MemberAccess (deref, lt.Value);
3119 anonymous_method_expression
3120 : DELEGATE opt_anonymous_method_signature
3122 if (oob_stack == null)
3123 oob_stack = new Stack (6);
3125 oob_stack.Push (current_local_parameters);
3126 current_local_parameters = (Parameters)$2;
3128 // Force the next block to be created as a ToplevelBlock
3129 oob_stack.Push (current_block);
3130 oob_stack.Push (top_current_block);
3131 current_block = null;
3135 Location loc = (Location) $1;
3136 top_current_block = (Block) oob_stack.Pop ();
3137 current_block = (Block) oob_stack.Pop ();
3138 if (RootContext.Version == LanguageVersion.ISO_1){
3139 Report.FeatureIsNotStandardized (loc, "anonymous methods");
3142 ToplevelBlock anon_block = (ToplevelBlock) $4;
3144 anon_block.Parent = current_block;
3145 $$ = new AnonymousMethod (current_container, (Parameters) $2, (ToplevelBlock) top_current_block,
3148 current_local_parameters = (Parameters) oob_stack.Pop ();
3152 opt_anonymous_method_signature
3153 : /* empty */ { $$ = null; }
3154 | anonymous_method_signature
3157 anonymous_method_signature
3158 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3161 $$ = Parameters.EmptyReadOnlyParameters;
3163 ArrayList par_list = (ArrayList) $2;
3164 Parameter [] pars = new Parameter [par_list.Count];
3165 par_list.CopyTo (pars);
3166 $$ = new Parameters (pars);
3171 opt_anonymous_method_parameter_list
3172 : /* empty */ { $$ = null; }
3173 | anonymous_method_parameter_list { $$ = $1; }
3176 anonymous_method_parameter_list
3177 : anonymous_method_parameter
3179 ArrayList a = new ArrayList (4);
3183 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3185 ArrayList a = (ArrayList) $1;
3191 anonymous_method_parameter
3192 : opt_parameter_modifier type IDENTIFIER {
3193 LocatedToken lt = (LocatedToken) $3;
3194 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3196 | PARAMS type IDENTIFIER {
3197 Report.Error (1670, ((LocatedToken) $3).Location, "The `params' modifier is not allowed in anonymous method declaration");
3203 : primary_expression
3204 | BANG prefixed_unary_expression
3206 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3208 | TILDE prefixed_unary_expression
3210 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3216 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3218 $$ = new Cast ((Expression) $1, (Expression) $3);
3220 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3222 $$ = new Cast ((Expression) $1, (Expression) $3);
3228 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3230 // TODO: wrong location
3231 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3236 // The idea to split this out is from Rhys' grammar
3237 // to solve the problem with casts.
3239 prefixed_unary_expression
3241 | PLUS prefixed_unary_expression
3243 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3245 | MINUS prefixed_unary_expression
3247 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3249 | OP_INC prefixed_unary_expression
3251 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3252 (Expression) $2, (Location) $1);
3254 | OP_DEC prefixed_unary_expression
3256 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3257 (Expression) $2, (Location) $1);
3259 | STAR prefixed_unary_expression
3261 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, (Location) $1);
3263 | BITWISE_AND prefixed_unary_expression
3265 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3269 pre_increment_expression
3270 : OP_INC prefixed_unary_expression
3272 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3273 (Expression) $2, (Location) $1);
3277 pre_decrement_expression
3278 : OP_DEC prefixed_unary_expression
3280 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3281 (Expression) $2, (Location) $1);
3285 multiplicative_expression
3286 : prefixed_unary_expression
3287 | multiplicative_expression STAR prefixed_unary_expression
3289 $$ = new Binary (Binary.Operator.Multiply,
3290 (Expression) $1, (Expression) $3);
3292 | multiplicative_expression DIV prefixed_unary_expression
3294 $$ = new Binary (Binary.Operator.Division,
3295 (Expression) $1, (Expression) $3);
3297 | multiplicative_expression PERCENT prefixed_unary_expression
3299 $$ = new Binary (Binary.Operator.Modulus,
3300 (Expression) $1, (Expression) $3);
3305 : multiplicative_expression
3306 | additive_expression PLUS multiplicative_expression
3308 $$ = new Binary (Binary.Operator.Addition,
3309 (Expression) $1, (Expression) $3);
3311 | additive_expression MINUS multiplicative_expression
3313 $$ = new Binary (Binary.Operator.Subtraction,
3314 (Expression) $1, (Expression) $3);
3319 : additive_expression
3320 | shift_expression OP_SHIFT_LEFT additive_expression
3322 $$ = new Binary (Binary.Operator.LeftShift,
3323 (Expression) $1, (Expression) $3);
3325 | shift_expression OP_SHIFT_RIGHT additive_expression
3327 $$ = new Binary (Binary.Operator.RightShift,
3328 (Expression) $1, (Expression) $3);
3332 relational_expression
3334 | relational_expression OP_LT shift_expression
3336 $$ = new Binary (Binary.Operator.LessThan,
3337 (Expression) $1, (Expression) $3);
3339 | relational_expression OP_GT shift_expression
3341 $$ = new Binary (Binary.Operator.GreaterThan,
3342 (Expression) $1, (Expression) $3);
3344 | relational_expression OP_LE shift_expression
3346 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3347 (Expression) $1, (Expression) $3);
3349 | relational_expression OP_GE shift_expression
3351 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3352 (Expression) $1, (Expression) $3);
3354 | relational_expression IS type
3356 $$ = new Is ((Expression) $1, (Expression) $3, (Location) $2);
3358 | relational_expression AS type
3360 $$ = new As ((Expression) $1, (Expression) $3, (Location) $2);
3365 : relational_expression
3366 | equality_expression OP_EQ relational_expression
3368 $$ = new Binary (Binary.Operator.Equality,
3369 (Expression) $1, (Expression) $3);
3371 | equality_expression OP_NE relational_expression
3373 $$ = new Binary (Binary.Operator.Inequality,
3374 (Expression) $1, (Expression) $3);
3379 : equality_expression
3380 | and_expression BITWISE_AND equality_expression
3382 $$ = new Binary (Binary.Operator.BitwiseAnd,
3383 (Expression) $1, (Expression) $3);
3387 exclusive_or_expression
3389 | exclusive_or_expression CARRET and_expression
3391 $$ = new Binary (Binary.Operator.ExclusiveOr,
3392 (Expression) $1, (Expression) $3);
3396 inclusive_or_expression
3397 : exclusive_or_expression
3398 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3400 $$ = new Binary (Binary.Operator.BitwiseOr,
3401 (Expression) $1, (Expression) $3);
3405 conditional_and_expression
3406 : inclusive_or_expression
3407 | conditional_and_expression OP_AND inclusive_or_expression
3409 $$ = new Binary (Binary.Operator.LogicalAnd,
3410 (Expression) $1, (Expression) $3);
3414 conditional_or_expression
3415 : conditional_and_expression
3416 | conditional_or_expression OP_OR conditional_and_expression
3418 $$ = new Binary (Binary.Operator.LogicalOr,
3419 (Expression) $1, (Expression) $3);
3423 conditional_expression
3424 : conditional_or_expression
3425 | conditional_or_expression INTERR expression COLON expression
3427 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
3431 assignment_expression
3432 : prefixed_unary_expression ASSIGN expression
3434 $$ = new Assign ((Expression) $1, (Expression) $3);
3436 | prefixed_unary_expression OP_MULT_ASSIGN expression
3438 $$ = new CompoundAssign (
3439 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
3441 | prefixed_unary_expression OP_DIV_ASSIGN expression
3443 $$ = new CompoundAssign (
3444 Binary.Operator.Division, (Expression) $1, (Expression) $3);
3446 | prefixed_unary_expression OP_MOD_ASSIGN expression
3448 $$ = new CompoundAssign (
3449 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
3451 | prefixed_unary_expression OP_ADD_ASSIGN expression
3453 $$ = new CompoundAssign (
3454 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
3456 | prefixed_unary_expression OP_SUB_ASSIGN expression
3458 $$ = new CompoundAssign (
3459 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3461 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3463 $$ = new CompoundAssign (
3464 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
3466 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3468 $$ = new CompoundAssign (
3469 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
3471 | prefixed_unary_expression OP_AND_ASSIGN expression
3473 $$ = new CompoundAssign (
3474 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
3476 | prefixed_unary_expression OP_OR_ASSIGN expression
3478 $$ = new CompoundAssign (
3479 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
3481 | prefixed_unary_expression OP_XOR_ASSIGN expression
3483 $$ = new CompoundAssign (
3484 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
3489 : conditional_expression
3490 | assignment_expression
3510 MemberName name = MakeName ((MemberName) $5);
3511 int mod_flags = (int) $2;
3513 current_class = new Class (
3514 current_namespace, current_class, name,
3515 mod_flags, (Attributes) $1);
3518 current_container = current_container.AddPartial (current_class);
3520 current_container.AddClassOrStruct (current_class);
3521 current_container = current_class;
3527 if (current_class.Name == "System.Object") {
3528 Report.Error (537, current_class.Location,
3529 "The class System.Object cannot have a base " +
3530 "class or implement an interface.");
3532 current_class.Bases = (ArrayList) $7;
3535 if (RootContext.Documentation != null) {
3536 current_container.DocComment = Lexer.consume_doc_comment ();
3537 Lexer.doc_state = XmlCommentState.Allowed;
3542 if (RootContext.Documentation != null)
3543 Lexer.doc_state = XmlCommentState.Allowed;
3547 $$ = pop_current_class ();
3555 { $$ = $1; } // location
3559 : /* empty */ { $$ = (int) 0; }
3565 | modifiers modifier
3570 if ((m1 & m2) != 0) {
3571 Location l = lexer.Location;
3572 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
3574 $$ = (int) (m1 | m2);
3579 : NEW { $$ = Modifiers.NEW; }
3580 | PUBLIC { $$ = Modifiers.PUBLIC; }
3581 | PROTECTED { $$ = Modifiers.PROTECTED; }
3582 | INTERNAL { $$ = Modifiers.INTERNAL; }
3583 | PRIVATE { $$ = Modifiers.PRIVATE; }
3584 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3585 | SEALED { $$ = Modifiers.SEALED; }
3586 | STATIC { $$ = Modifiers.STATIC; }
3587 | READONLY { $$ = Modifiers.READONLY; }
3588 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3589 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3590 | EXTERN { $$ = Modifiers.EXTERN; }
3591 | VOLATILE { $$ = Modifiers.VOLATILE; }
3592 | UNSAFE { $$ = Modifiers.UNSAFE; }
3596 : /* empty */ { $$ = null; }
3597 | class_base { $$ = $1; }
3601 : COLON type_list { $$ = $2; }
3609 // A block is "contained" on the following places:
3611 // property_declaration as part of the accessor body (get/set)
3612 // operator_declaration
3613 // constructor_declaration
3614 // destructor_declaration
3615 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3620 if (current_block == null){
3621 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, (Location) $1);
3622 top_current_block = current_block;
3624 current_block = new Block (current_block, (Location) $1, Location.Null);
3627 opt_statement_list CLOSE_BRACE
3629 while (current_block.Implicit)
3630 current_block = current_block.Parent;
3632 current_block.SetEndLocation ((Location) $4);
3633 current_block = current_block.Parent;
3634 if (current_block == null)
3635 top_current_block = null;
3646 | statement_list statement
3650 : declaration_statement
3652 if ($1 != null && (Block) $1 != current_block){
3653 current_block.AddStatement ((Statement) $1);
3654 current_block = (Block) $1;
3657 | valid_declaration_statement
3659 current_block.AddStatement ((Statement) $1);
3664 valid_declaration_statement
3667 | expression_statement
3668 | selection_statement
3669 | iteration_statement
3673 | unchecked_statement
3681 : valid_declaration_statement
3682 | declaration_statement
3684 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3689 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3697 $$ = EmptyStatement.Value;
3704 LocatedToken lt = (LocatedToken) $1;
3705 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
3707 if (current_block.AddLabel (labeled))
3708 current_block.AddStatement (labeled);
3713 declaration_statement
3714 : local_variable_declaration SEMICOLON
3716 current_array_type = null;
3718 DictionaryEntry de = (DictionaryEntry) $1;
3719 Expression e = (Expression) de.Key;
3721 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
3725 | local_constant_declaration SEMICOLON
3727 current_array_type = null;
3729 DictionaryEntry de = (DictionaryEntry) $1;
3731 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3737 * The following is from Rhys' grammar:
3738 * > Types in local variable declarations must be recognized as
3739 * > expressions to prevent reduce/reduce errors in the grammar.
3740 * > The expressions are converted into types during semantic analysis.
3743 : primary_expression opt_rank_specifier
3745 // FIXME: Do something smart here regarding the composition of the type.
3747 // Ok, the above "primary_expression" is there to get rid of
3748 // both reduce/reduce and shift/reduces in the grammar, it should
3749 // really just be "type_name". If you use type_name, a reduce/reduce
3750 // creeps up. If you use namespace_or_type_name (which is all we need
3751 // really) two shift/reduces appear.
3754 // So the super-trick is that primary_expression
3755 // can only be either a SimpleName or a MemberAccess.
3756 // The MemberAccess case arises when you have a fully qualified type-name like :
3758 // SimpleName is when you have
3761 Expression expr = (Expression) $1;
3762 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3763 Error_ExpectingTypeName (expr);
3767 // So we extract the string corresponding to the SimpleName
3771 if ((string) $2 == "")
3774 $$ = new ComposedCast ((Expression) $1, (string) $2);
3777 | builtin_types opt_rank_specifier
3779 if ((string) $2 == "")
3782 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3786 local_variable_pointer_type
3787 : primary_expression STAR
3789 Expression expr = (Expression) $1;
3791 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3792 Error_ExpectingTypeName (expr);
3796 $$ = new ComposedCast ((Expression) $1, "*");
3798 | builtin_types STAR
3800 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3804 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
3806 | local_variable_pointer_type STAR
3808 $$ = new ComposedCast ((Expression) $1, "*");
3812 local_variable_declaration
3813 : local_variable_type variable_declarators
3816 $$ = new DictionaryEntry ($1, $2);
3820 | local_variable_pointer_type opt_rank_specifier variable_declarators
3825 if ((string) $2 == "")
3826 t = (Expression) $1;
3828 t = new ComposedCast ((Expression) $1, (string) $2);
3829 $$ = new DictionaryEntry (t, $3);
3835 local_constant_declaration
3836 : CONST local_variable_type constant_declarators
3839 $$ = new DictionaryEntry ($2, $3);
3845 expression_statement
3846 : statement_expression SEMICOLON { $$ = $1; }
3850 // We have to do the wrapping here and not in the case above,
3851 // because statement_expression is used for example in for_statement
3853 statement_expression
3856 Expression expr = (Expression) $1;
3857 ExpressionStatement s = expr as ExpressionStatement;
3859 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
3862 $$ = new StatementExpression (s);
3866 Report.Error (1002, GetLocation ($1), "Expecting `;'");
3871 object_creation_expression
3872 : object_or_delegate_creation_expression
3873 { note ("complain if this is a delegate maybe?"); }
3882 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
3885 Location l = (Location) $1;
3887 $$ = new If ((Expression) $3, (Statement) $5, l);
3889 // FIXME: location for warning should be loc property of $5.
3890 if ($5 == EmptyStatement.Value)
3891 Report.Warning (642, 3, l, "Possible mistaken empty statement");
3894 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
3895 embedded_statement ELSE embedded_statement
3897 Location l = (Location) $1;
3899 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
3901 // FIXME: location for warning should be loc property of $5 and $7.
3902 if ($5 == EmptyStatement.Value)
3903 Report.Warning (642, 3, l, "Possible mistaken empty statement");
3904 if ($7 == EmptyStatement.Value)
3905 Report.Warning (642, 3, l, "Possible mistaken empty statement");
3910 : SWITCH OPEN_PARENS
3912 if (switch_stack == null)
3913 switch_stack = new Stack (2);
3914 switch_stack.Push (current_block);
3916 expression CLOSE_PARENS
3919 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
3920 current_block = (Block) switch_stack.Pop ();
3936 Report.Error (1522, lexer.Location, "Empty switch block");
3944 ArrayList sections = new ArrayList (4);
3949 | switch_sections switch_section
3951 ArrayList sections = (ArrayList) $1;
3961 current_block = current_block.CreateSwitchBlock (lexer.Location);
3965 Block topmost = current_block;
3967 while (topmost.Implicit)
3968 topmost = topmost.Parent;
3969 $$ = new SwitchSection ((ArrayList) $1, topmost);
3976 ArrayList labels = new ArrayList (4);
3981 | switch_labels switch_label
3983 ArrayList labels = (ArrayList) ($1);
3991 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
3992 | DEFAULT COLON { $$ = new SwitchLabel (null, (Location) $1); }
3995 1523, GetLocation ($1),
3996 "The keyword case or default must precede code in switch block");
4008 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
4010 Location l = (Location) $1;
4011 $$ = new While ((Expression) $3, (Statement) $5, l);
4016 : DO embedded_statement
4017 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
4019 Location l = (Location) $1;
4021 $$ = new Do ((Statement) $2, (Expression) $5, l);
4027 opt_for_initializer SEMICOLON
4029 Block assign_block = new Block (current_block);
4030 current_block = assign_block;
4032 if ($3 is DictionaryEntry){
4033 DictionaryEntry de = (DictionaryEntry) $3;
4035 Expression type = (Expression) de.Key;
4036 ArrayList var_declarators = (ArrayList) de.Value;
4038 foreach (VariableDeclaration decl in var_declarators){
4042 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4046 Location l = lexer.Location;
4047 Expression expr = decl.expression_or_array_initializer;
4049 LocalVariableReference var;
4050 var = new LocalVariableReference (assign_block, decl.identifier, l);
4053 Assign a = new Assign (var, expr, decl.Location);
4055 assign_block.AddStatement (new StatementExpression (a));
4059 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4060 // This can be referred to as $5 below.
4066 opt_for_condition SEMICOLON
4067 opt_for_iterator CLOSE_PARENS
4070 Location l = (Location) $1;
4072 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4074 current_block.AddStatement (f);
4075 while (current_block.Implicit)
4076 current_block = current_block.Parent;
4078 current_block = current_block.Parent;
4083 : /* empty */ { $$ = EmptyStatement.Value; }
4088 : local_variable_declaration
4089 | statement_expression_list
4093 : /* empty */ { $$ = null; }
4094 | boolean_expression
4098 : /* empty */ { $$ = EmptyStatement.Value; }
4103 : statement_expression_list
4106 statement_expression_list
4107 : statement_expression
4109 // CHANGE: was `null'
4110 Statement s = (Statement) $1;
4111 Block b = new Block (current_block, Block.Flags.Implicit, s.loc, lexer.Location);
4116 | statement_expression_list COMMA statement_expression
4118 Block b = (Block) $1;
4120 b.AddStatement ((Statement) $3);
4126 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4128 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4131 | FOREACH OPEN_PARENS type IDENTIFIER IN
4132 expression CLOSE_PARENS
4134 Block foreach_block = new Block (current_block);
4135 current_block = foreach_block;
4137 LocatedToken lt = (LocatedToken) $4;
4138 Location l = lt.Location;
4141 vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4143 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4145 // Get a writable reference to this read-only variable.
4147 // Note that the $$ here refers to the value of _this_ code block,
4148 // not the value of the LHS non-terminal. This can be referred to as $8 below.
4149 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
4156 LocalVariableReference v = (LocalVariableReference) $8;
4157 Location l = (Location) $1;
4160 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
4161 current_block.AddStatement (f);
4164 while (current_block.Implicit)
4165 current_block = current_block.Parent;
4167 current_block = current_block.Parent;
4173 | continue_statement
4183 $$ = new Break ((Location) $1);
4188 : CONTINUE SEMICOLON
4190 $$ = new Continue ((Location) $1);
4195 : GOTO IDENTIFIER SEMICOLON
4197 LocatedToken lt = (LocatedToken) $2;
4198 $$ = new Goto (lt.Value, lt.Location);
4200 | GOTO CASE constant_expression SEMICOLON
4202 $$ = new GotoCase ((Expression) $3, (Location) $1);
4204 | GOTO DEFAULT SEMICOLON
4206 $$ = new GotoDefault ((Location) $1);
4211 : RETURN opt_expression SEMICOLON
4213 $$ = new Return ((Expression) $2, (Location) $1);
4218 : THROW opt_expression SEMICOLON
4220 $$ = new Throw ((Expression) $2, (Location) $1);
4225 : IDENTIFIER RETURN expression SEMICOLON
4227 LocatedToken lt = (LocatedToken) $1;
4228 string s = lt.Value;
4230 Report.Error (1003, lt.Location, "; expected");
4233 if (RootContext.Version == LanguageVersion.ISO_1){
4234 Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4237 if (iterator_container == null){
4238 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4241 iterator_container.SetYields ();
4242 $$ = new Yield ((Expression) $3, lt.Location);
4245 | IDENTIFIER RETURN SEMICOLON
4247 Report.Error (1627, (Location) $2, "Expression expected after yield return");
4250 | IDENTIFIER BREAK SEMICOLON
4252 LocatedToken lt = (LocatedToken) $1;
4253 string s = lt.Value;
4255 Report.Error (1003, lt.Location, "; expected");
4258 if (RootContext.Version == LanguageVersion.ISO_1){
4259 Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4262 if (iterator_container == null){
4263 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4266 iterator_container.SetYields ();
4267 $$ = new YieldBreak (lt.Location);
4278 : TRY block catch_clauses
4282 ArrayList c = (ArrayList)$3;
4283 for (int i = 0; i < c.Count; ++i) {
4284 Catch cc = (Catch) c [i];
4286 if (i != c.Count - 1)
4287 Report.Error (1017, cc.loc, "Try statement already has an empty catch block");
4294 // Now s contains the list of specific catch clauses
4295 // and g contains the general one.
4297 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4299 | TRY block opt_catch_clauses FINALLY block
4302 ArrayList s = new ArrayList (4);
4303 ArrayList catch_list = (ArrayList) $3;
4305 if (catch_list != null){
4306 foreach (Catch cc in catch_list) {
4314 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4318 Report.Error (1524, (Location) $1, "Expected catch or finally");
4324 : /* empty */ { $$ = null; }
4331 ArrayList l = new ArrayList (4);
4336 | catch_clauses catch_clause
4338 ArrayList l = (ArrayList) $1;
4346 : /* empty */ { $$ = null; }
4351 : CATCH opt_catch_args
4353 Expression type = null;
4356 DictionaryEntry cc = (DictionaryEntry) $2;
4357 type = (Expression) cc.Key;
4358 LocatedToken lt = (LocatedToken) cc.Value;
4361 ArrayList one = new ArrayList (4);
4363 one.Add (new VariableDeclaration (lt, null));
4365 current_block = new Block (current_block);
4366 Block b = declare_local_variables (type, one, lt.Location);
4371 Expression type = null;
4373 Block var_block = null;
4376 DictionaryEntry cc = (DictionaryEntry) $2;
4377 type = (Expression) cc.Key;
4378 LocatedToken lt = (LocatedToken) cc.Value;
4382 while (current_block.Implicit)
4383 current_block = current_block.Parent;
4384 var_block = current_block;
4385 current_block = current_block.Parent;
4389 $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
4394 : /* empty */ { $$ = null; }
4399 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4401 $$ = new DictionaryEntry ($2, $3);
4409 $$ = new Checked ((Block) $2);
4416 $$ = new Unchecked ((Block) $2);
4423 RootContext.CheckUnsafeOption ((Location) $1);
4425 $$ = new Unsafe ((Block) $3);
4431 type fixed_pointer_declarators
4434 ArrayList list = (ArrayList) $4;
4435 Expression type = (Expression) $3;
4436 Location l = (Location) $1;
4437 int top = list.Count;
4439 Block assign_block = new Block (current_block);
4440 current_block = assign_block;
4442 for (int i = 0; i < top; i++){
4443 Pair p = (Pair) list [i];
4446 v = current_block.AddVariable (type, (string) p.First, l);
4450 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4458 Location l = (Location) $1;
4460 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4462 current_block.AddStatement (f);
4463 while (current_block.Implicit)
4464 current_block = current_block.Parent;
4466 current_block = current_block.Parent;
4470 fixed_pointer_declarators
4471 : fixed_pointer_declarator {
4472 ArrayList declarators = new ArrayList (4);
4474 declarators.Add ($1);
4477 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4479 ArrayList declarators = (ArrayList) $1;
4481 declarators.Add ($3);
4486 fixed_pointer_declarator
4487 : IDENTIFIER ASSIGN expression
4489 LocatedToken lt = (LocatedToken) $1;
4490 // FIXME: keep location
4491 $$ = new Pair (lt.Value, $3);
4495 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
4501 : LOCK OPEN_PARENS expression CLOSE_PARENS
4507 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
4512 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4514 Block assign_block = new Block (current_block);
4515 current_block = assign_block;
4517 if ($3 is DictionaryEntry){
4518 DictionaryEntry de = (DictionaryEntry) $3;
4519 Location l = (Location) $1;
4521 Expression type = (Expression) de.Key;
4522 ArrayList var_declarators = (ArrayList) de.Value;
4524 ArrayList vars = new ArrayList (4);
4526 foreach (VariableDeclaration decl in var_declarators){
4528 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4531 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
4533 Expression expr = decl.expression_or_array_initializer;
4535 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4538 LocalVariableReference var;
4540 // Get a writable reference to this read-only variable.
4541 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4543 // This is so that it is not a warning on using variables
4546 vars.Add (new DictionaryEntry (var, expr));
4548 // Assign a = new Assign (var, expr, decl.Location);
4549 // assign_block.AddStatement (new StatementExpression (a));
4552 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4553 // It can be referred to as $5 below.
4554 $$ = new DictionaryEntry (type, vars);
4561 Using u = new Using ($5, (Statement) $6, (Location) $1);
4562 current_block.AddStatement (u);
4563 while (current_block.Implicit)
4564 current_block = current_block.Parent;
4566 current_block = current_block.Parent;
4570 resource_acquisition
4571 : local_variable_declaration
4578 // A class used to pass around variable declarations and constants
4580 public class VariableDeclaration {
4581 public string identifier;
4582 public Expression expression_or_array_initializer;
4583 public Location Location;
4584 public Attributes OptAttributes;
4585 public string DocComment;
4587 public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
4589 this.identifier = lt.Value;
4590 if (eoai is ArrayList) {
4591 if (CSharpParser.current_array_type == null)
4592 Report.Error (622, lt.Location,
4593 "Can only use array initializer expressions to assign to array types. Try using a new expression instead.");
4594 this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
4596 this.expression_or_array_initializer = (Expression)eoai;
4598 this.Location = lt.Location;
4599 this.OptAttributes = opt_attrs;
4602 public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
4608 // A class used to hold info about an indexer declarator
4610 public class IndexerDeclaration {
4611 public Expression type;
4612 public MemberName interface_type;
4613 public Parameters param_list;
4614 public Location location;
4616 public IndexerDeclaration (Expression type, MemberName interface_type,
4617 Parameters param_list, Location loc)
4620 this.interface_type = interface_type;
4621 this.param_list = param_list;
4622 this.location = loc;
4627 // We use this when we do not have an object in advance that is an IIteratorContainer
4629 public class SimpleIteratorContainer : IIteratorContainer {
4632 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4637 public static SimpleIteratorContainer GetSimple () {
4638 Simple.Yields = false;
4642 public void SetYields () { Yields = true; }
4646 // A class used to hold info about an operator declarator
4648 public class OperatorDeclaration {
4649 public Operator.OpType optype;
4650 public Expression ret_type, arg1type, arg2type;
4651 public string arg1name, arg2name;
4652 public Location location;
4654 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4655 Expression arg1type, string arg1name,
4656 Expression arg2type, string arg2name, Location location)
4659 this.ret_type = ret_type;
4660 this.arg1type = arg1type;
4661 this.arg1name = arg1name;
4662 this.arg2type = arg2type;
4663 this.arg2name = arg2name;
4664 this.location = location;
4669 void Error_ExpectingTypeName (Expression expr)
4671 if (expr is Invocation){
4672 Report.Error (1002, expr.Location, "Expecting `;'");
4674 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
4678 TypeContainer pop_current_class ()
4680 TypeContainer retval = current_class;
4682 current_class = (TypeContainer) current_class.Parent;
4683 current_container = current_class.PartialContainer;
4689 // Given the @class_name name, it creates a fully qualified name
4690 // based on the containing declaration space
4693 MakeName (MemberName class_name)
4695 Namespace ns = current_namespace.NS;
4697 if (current_container.Name.Length == 0){
4698 if (ns.Name.Length != 0)
4699 return new MemberName (ns.MemberName, class_name);
4703 return new MemberName (current_container.MemberName, class_name);
4707 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4709 Block implicit_block;
4710 ArrayList inits = null;
4713 // We use the `Used' property to check whether statements
4714 // have been added to the current block. If so, we need
4715 // to create another block to contain the new declaration
4716 // otherwise, as an optimization, we use the same block to
4717 // add the declaration.
4719 // FIXME: A further optimization is to check if the statements
4720 // that were added were added as part of the initialization
4721 // below. In which case, no other statements have been executed
4722 // and we might be able to reduce the number of blocks for
4723 // situations like this:
4725 // int j = 1; int k = j + 1;
4727 if (current_block.Used)
4728 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4730 implicit_block = current_block;
4732 foreach (VariableDeclaration decl in variable_declarators){
4734 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
4735 if (decl.expression_or_array_initializer != null){
4737 inits = new ArrayList (4);
4744 return implicit_block;
4746 foreach (VariableDeclaration decl in inits){
4748 Expression expr = decl.expression_or_array_initializer;
4750 LocalVariableReference var;
4751 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4753 assign = new Assign (var, expr, decl.Location);
4755 implicit_block.AddStatement (new StatementExpression (assign));
4758 return implicit_block;
4761 Block declare_local_constants (Expression type, ArrayList declarators)
4763 Block implicit_block;
4765 if (current_block.Used)
4766 implicit_block = new Block (current_block, Block.Flags.Implicit);
4768 implicit_block = current_block;
4770 foreach (VariableDeclaration decl in declarators){
4771 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
4774 return implicit_block;
4777 void CheckAttributeTarget (string a, Location l)
4781 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4785 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4791 void CheckUnaryOperator (Operator.OpType op, Location l)
4795 case Operator.OpType.LogicalNot:
4796 case Operator.OpType.OnesComplement:
4797 case Operator.OpType.Increment:
4798 case Operator.OpType.Decrement:
4799 case Operator.OpType.True:
4800 case Operator.OpType.False:
4801 case Operator.OpType.Addition:
4802 case Operator.OpType.Subtraction:
4807 Report.Error (1019, l, "Overloadable unary operator expected");
4813 void CheckBinaryOperator (Operator.OpType op, Location l)
4817 case Operator.OpType.Addition:
4818 case Operator.OpType.Subtraction:
4819 case Operator.OpType.Multiply:
4820 case Operator.OpType.Division:
4821 case Operator.OpType.Modulus:
4822 case Operator.OpType.BitwiseAnd:
4823 case Operator.OpType.BitwiseOr:
4824 case Operator.OpType.ExclusiveOr:
4825 case Operator.OpType.LeftShift:
4826 case Operator.OpType.RightShift:
4827 case Operator.OpType.Equality:
4828 case Operator.OpType.Inequality:
4829 case Operator.OpType.GreaterThan:
4830 case Operator.OpType.LessThan:
4831 case Operator.OpType.GreaterThanOrEqual:
4832 case Operator.OpType.LessThanOrEqual:
4836 Report.Error (1020, l, "Overloadable binary operator expected");
4842 void syntax_error (Location l, string msg)
4844 Report.Error (1003, l, "Syntax error, " + msg);
4847 void note (string s)
4849 // Used to put annotations
4854 public Tokenizer Lexer {
4860 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4862 current_namespace = new NamespaceEntry (null, file, null);
4863 this.name = file.Name;
4865 current_container = RootContext.Tree.Types;
4866 // TODO: Make RootContext.Tree.Types a PartialContainer.
4867 current_class = current_container;
4868 current_container.NamespaceEntry = current_namespace;
4870 lexer = new Tokenizer (reader, file, defines);
4873 public void parse ()
4875 int errors = Report.Errors;
4877 if (yacc_verbose_flag > 1)
4878 yyparse (lexer, new yydebug.yyDebugSimple ());
4881 Tokenizer tokenizer = lexer as Tokenizer;
4882 tokenizer.cleanup ();
4883 } catch (Exception e){
4885 // Removed for production use, use parser verbose to get the output.
4887 // Console.WriteLine (e);
4888 if (Report.Errors == errors)
4889 Report.Error (-25, lexer.Location, "Parsing error");
4890 if (yacc_verbose_flag > 0)
4891 Console.WriteLine (e);
4894 RootContext.Tree.Types.NamespaceEntry = null;
4897 static void CheckToken (int error, int yyToken, string msg, Location loc)
4899 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
4900 Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ());
4902 Report.Error (error, loc, msg);
4905 void CheckIdentifierToken (int yyToken, Location loc)
4907 CheckToken (1041, yyToken, "Identifier expected", loc);
4910 string ConsumeStoredComment ()
4912 string s = tmpComment;
4914 Lexer.doc_state = XmlCommentState.Allowed;
4918 Location GetLocation (object obj)
4920 if (obj is MemberCore)
4921 return ((MemberCore) obj).Location;
4922 if (obj is MemberName)
4923 return ((MemberName) obj).Location;
4924 if (obj is LocatedToken)
4925 return ((LocatedToken) obj).Location;
4926 if (obj is Location)
4927 return (Location) obj;
4928 return lexer.Location;