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 DeclSpace current_class;
38 IAnonymousHost anonymous_host;
41 /// Current block is used to add statements as we find
44 Block current_block, top_current_block;
46 Delegate current_delegate;
48 AnonymousMethodExpression current_anonymous_method;
51 /// This is used by the unary_expression code to resolve
52 /// a name against a parameter.
54 Parameters current_local_parameters;
57 /// Using during property parsing to describe the implicit
58 /// value parameter that is passed to the "set" and "get"accesor
59 /// methods (properties and indexers).
61 Expression implicit_value_parameter_type;
62 Parameters indexer_parameters;
65 /// Hack to help create non-typed array initializer
67 public static Expression current_array_type;
68 Expression pushed_current_array_type;
71 /// Used to determine if we are parsing the get/set pair
72 /// of an indexer or a property
76 bool parsing_anonymous_method;
79 /// An out-of-band stack.
88 static public int yacc_verbose_flag;
90 // Name of the file we are parsing
99 /// Temporary Xml documentation cache.
100 /// For enum types, we need one more temporary store.
103 string enumTypeComment;
105 /// Current attribute target
106 string current_attr_target;
108 /// assembly and module attribute definitions are enabled
109 bool global_attrs_enabled = true;
110 bool has_get, has_set;
115 %token NONE /* This token is never returned by our lexer */
116 %token ERROR // This is used not by the parser, but by the tokenizer.
120 *These are the C# keywords
206 /* C# keywords which are not really keywords */
212 /* C# single character operators/punctuation. */
213 %token OPEN_BRACE "{"
214 %token CLOSE_BRACE "}"
215 %token OPEN_BRACKET "["
216 %token CLOSE_BRACKET "]"
217 %token OPEN_PARENS "("
218 %token CLOSE_PARENS ")"
231 %token BITWISE_AND "&"
232 %token BITWISE_OR "|"
239 /* C# multi-character operators. */
240 %token DOUBLE_COLON "::"
243 %token OP_SHIFT_LEFT "<<"
244 %token OP_SHIFT_RIGHT ">>"
251 %token OP_MULT_ASSIGN "*="
252 %token OP_DIV_ASSIGN "/="
253 %token OP_MOD_ASSIGN "%="
254 %token OP_ADD_ASSIGN "+="
255 %token OP_SUB_ASSIGN "-="
256 %token OP_SHIFT_LEFT_ASSIGN "<<="
257 %token OP_SHIFT_RIGHT_ASSIGN ">>="
258 %token OP_AND_ASSIGN "&="
259 %token OP_XOR_ASSIGN "^="
260 %token OP_OR_ASSIGN "|="
264 %token LITERAL_INTEGER "int literal"
265 %token LITERAL_FLOAT "float literal"
266 %token LITERAL_DOUBLE "double literal"
267 %token LITERAL_DECIMAL "decimal literal"
268 %token LITERAL_CHARACTER "character literal"
269 %token LITERAL_STRING "string literal"
272 %token CLOSE_PARENS_CAST
273 %token CLOSE_PARENS_NO_CAST
274 %token CLOSE_PARENS_OPEN_PARENS
275 %token CLOSE_PARENS_MINUS
277 /* Add precedence rules to solve dangling else s/r conflict */
286 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
288 %left STAR DIV PERCENT
289 %right BANG CARRET UMINUS
290 %nonassoc OP_INC OP_DEC
292 %left OPEN_BRACKET OPEN_BRACE
296 %start compilation_unit
300 : outer_declarations opt_EOF
301 | outer_declarations global_attributes opt_EOF
302 | global_attributes opt_EOF
303 | opt_EOF /* allow empty files */
309 Lexer.check_incorrect_doc_comment ();
313 Lexer.check_incorrect_doc_comment ();
319 | outer_declarations outer_declaration
323 : extern_alias_directive
325 | namespace_member_declaration
328 extern_alias_directives
329 : extern_alias_directive
330 | extern_alias_directives extern_alias_directive;
332 extern_alias_directive
333 : EXTERN IDENTIFIER IDENTIFIER SEMICOLON
335 LocatedToken lt = (LocatedToken) $2;
338 Report.Error (1003, lt.Location, "'alias' expected");
339 } else if (RootContext.Version == LanguageVersion.ISO_1) {
340 Report.FeatureIsNotStandardized (lt.Location, "external alias");
342 lt = (LocatedToken) $3;
343 current_namespace.UsingExternalAlias (lt.Value, lt.Location);
350 | using_directives using_directive
354 : using_alias_directive
356 if (RootContext.Documentation != null)
357 Lexer.doc_state = XmlCommentState.Allowed;
359 | using_namespace_directive
361 if (RootContext.Documentation != null)
362 Lexer.doc_state = XmlCommentState.Allowed;
366 using_alias_directive
367 : USING IDENTIFIER ASSIGN
368 namespace_or_type_name SEMICOLON
370 LocatedToken lt = (LocatedToken) $2;
371 current_namespace.UsingAlias (lt.Value, (MemberName) $4, (Location) $1);
374 CheckIdentifierToken (yyToken, GetLocation ($2));
378 using_namespace_directive
379 : USING namespace_name SEMICOLON
381 current_namespace.Using ((MemberName) $2, (Location) $1);
386 // Strictly speaking, namespaces don't have attributes but
387 // we parse global attributes along with namespace declarations and then
390 namespace_declaration
391 : opt_attributes NAMESPACE namespace_or_type_name
393 MemberName name = (MemberName) $3;
396 Report.Error(1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
399 current_namespace = new NamespaceEntry (
400 current_namespace, file, name.GetName ());
401 current_class = current_namespace.SlaveDeclSpace;
402 current_container = current_class.PartialContainer;
404 namespace_body opt_semicolon
406 current_namespace = current_namespace.Parent;
407 current_class = current_namespace.SlaveDeclSpace;
408 current_container = current_class.PartialContainer;
423 : namespace_or_type_name
429 if (RootContext.Documentation != null)
430 Lexer.doc_state = XmlCommentState.Allowed;
436 : opt_extern_alias_directives
438 opt_namespace_member_declarations
442 Report.Error (1518, lexer.Location, "Expected `class', `delegate', `enum', `interface', or `struct'");
445 | opt_extern_alias_directives
447 opt_namespace_member_declarations
450 Report.Error (1513, lexer.Location, "} expected");
459 opt_extern_alias_directives
461 | extern_alias_directives
464 opt_namespace_member_declarations
466 | namespace_member_declarations
469 namespace_member_declarations
470 : namespace_member_declaration
471 | namespace_member_declarations namespace_member_declaration
474 namespace_member_declaration
478 DeclSpace ds = (DeclSpace)$1;
480 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
481 Report.Error (1527, ds.Location,
482 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
485 current_namespace.DeclarationFound = true;
487 | namespace_declaration {
488 current_namespace.DeclarationFound = true;
491 | field_declaration {
492 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
494 | method_declaration {
495 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
502 | interface_declaration
504 | delegate_declaration
506 // Enable this when we have handled all errors, because this acts as a generic fallback
509 // Console.WriteLine ("Token=" + yyToken);
510 // Report.Error (1518, GetLocation ($1), "Expected class, struct, interface, enum or delegate");
522 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
530 global_attrs_enabled = false;
535 global_attrs_enabled = false;
544 ArrayList sect = (ArrayList) $1;
546 if (global_attrs_enabled) {
547 if (current_attr_target == "module") {
548 CodeGen.Module.AddAttributes (sect);
550 } else if (current_attr_target != null && current_attr_target.Length > 0) {
551 CodeGen.Assembly.AddAttributes (sect);
554 $$ = new Attributes (sect);
557 if (RootContext.Documentation != null) {
558 Lexer.check_incorrect_doc_comment ();
560 XmlCommentState.Allowed;
564 $$ = new Attributes (sect);
566 current_attr_target = null;
568 | attribute_sections attribute_section
570 Attributes attrs = $1 as Attributes;
571 ArrayList sect = (ArrayList) $2;
573 if (global_attrs_enabled) {
574 if (current_attr_target == "module") {
575 CodeGen.Module.AddAttributes (sect);
577 } else if (current_attr_target == "assembly") {
578 CodeGen.Assembly.AddAttributes (sect);
582 attrs = new Attributes (sect);
584 attrs.AddAttributes (sect);
588 attrs = new Attributes (sect);
590 attrs.AddAttributes (sect);
593 current_attr_target = null;
598 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
602 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
608 attribute_target_specifier
609 : attribute_target COLON
611 current_attr_target = (string)$1;
619 LocatedToken lt = (LocatedToken) $1;
620 CheckAttributeTarget (lt.Value, lt.Location);
621 $$ = lt.Value; // Location won't be required anymore.
623 | EVENT { $$ = "event"; }
624 | RETURN { $$ = "return"; }
630 ArrayList attrs = new ArrayList (4);
636 | attribute_list COMMA attribute
638 ArrayList attrs = (ArrayList) $1;
646 : attribute_name opt_attribute_arguments
648 MemberName mname = (MemberName) $1;
649 object[] arguments = (object[]) $2;
650 MemberName left = mname.Left;
651 string identifier = mname.Name;
653 Expression left_expr = left == null ? null : left.GetTypeExpression ();
655 if (current_attr_target == "assembly" || current_attr_target == "module")
656 // FIXME: supply "nameEscaped" parameter here.
657 $$ = new GlobalAttribute (current_namespace, current_attr_target,
658 left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
660 $$ = new Attribute (current_attr_target, left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
665 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
668 opt_attribute_arguments
669 : /* empty */ { $$ = null; }
670 | OPEN_PARENS attribute_arguments CLOSE_PARENS
678 : opt_positional_argument_list
683 $$ = new object [] { $1, null };
686 | positional_argument_list COMMA named_argument_list
688 $$ = new object[] { $1, $3 };
690 | named_argument_list
692 $$ = new object [] { null, $1 };
697 opt_positional_argument_list
698 : /* empty */ { $$ = null; }
699 | positional_argument_list
702 positional_argument_list
705 ArrayList args = new ArrayList (4);
706 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
710 | positional_argument_list COMMA expression
712 ArrayList args = (ArrayList) $1;
713 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
722 ArrayList args = new ArrayList (4);
727 | named_argument_list COMMA named_argument
729 ArrayList args = (ArrayList) $1;
734 | named_argument_list COMMA expression
736 Report.Error (1016, ((Expression) $3).Location, "Named attribute argument expected");
742 : IDENTIFIER ASSIGN expression
744 // FIXME: keep location
745 $$ = new DictionaryEntry (
746 ((LocatedToken) $1).Value,
747 new Argument ((Expression) $3, Argument.AType.Expression));
753 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
756 opt_class_member_declarations
758 | class_member_declarations
761 class_member_declarations
762 : class_member_declaration
763 | class_member_declarations
764 class_member_declaration
767 class_member_declaration
768 : constant_declaration // done
769 | field_declaration // done
770 | method_declaration // done
771 | property_declaration // done
772 | event_declaration // done
773 | indexer_declaration // done
774 | operator_declaration // done
775 | constructor_declaration // done
776 | destructor_declaration // done
786 MemberName name = MakeName ((MemberName) $5);
787 push_current_class (new Struct (
788 current_namespace, current_class, name, (int) $2,
789 (Attributes) $1), false, $3);
795 current_container.AddBasesForPart (current_class, (ArrayList) $7);
797 if (RootContext.Documentation != null)
798 current_container.DocComment = Lexer.consume_doc_comment ();
802 if (RootContext.Documentation != null)
803 Lexer.doc_state = XmlCommentState.Allowed;
807 $$ = pop_current_class ();
809 | opt_attributes opt_modifiers opt_partial STRUCT error {
810 CheckIdentifierToken (yyToken, GetLocation ($5));
817 if (RootContext.Documentation != null)
818 Lexer.doc_state = XmlCommentState.Allowed;
820 opt_struct_member_declarations CLOSE_BRACE
823 opt_struct_member_declarations
825 | struct_member_declarations
828 struct_member_declarations
829 : struct_member_declaration
830 | struct_member_declarations struct_member_declaration
833 struct_member_declaration
834 : constant_declaration
837 | property_declaration
839 | indexer_declaration
840 | operator_declaration
841 | constructor_declaration
845 * This is only included so we can flag error 575:
846 * destructors only allowed on class types
848 | destructor_declaration
859 int modflags = (int) $2;
860 foreach (VariableDeclaration constant in (ArrayList) $5){
861 Location l = constant.Location;
862 if ((modflags & Modifiers.STATIC) != 0) {
863 Report.Error (504, l, "The constant `{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
867 Const c = new Const (
868 current_class, (Expression) $4, (string) constant.identifier,
869 (Expression) constant.expression_or_array_initializer, modflags,
872 if (RootContext.Documentation != null) {
873 c.DocComment = Lexer.consume_doc_comment ();
874 Lexer.doc_state = XmlCommentState.Allowed;
876 current_container.AddConstant (c);
882 : constant_declarator
884 ArrayList constants = new ArrayList (4);
889 | constant_declarators COMMA constant_declarator
892 ArrayList constants = (ArrayList) $1;
899 : IDENTIFIER ASSIGN constant_expression
901 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
905 // A const field requires a value to be provided
906 Report.Error (145, ((LocatedToken) $1).Location, "A const field requires a value to be provided");
918 Expression type = (Expression) $3;
921 current_array_type = null;
923 foreach (VariableDeclaration var in (ArrayList) $4){
924 Field field = new Field (current_class, type, mod, var.identifier,
925 (Attributes) $1, var.Location);
927 field.Initializer = var.expression_or_array_initializer;
929 if (RootContext.Documentation != null) {
930 field.DocComment = Lexer.consume_doc_comment ();
931 Lexer.doc_state = XmlCommentState.Allowed;
933 current_container.AddField (field);
934 $$ = field; // FIXME: might be better if it points to the top item
941 fixed_variable_declarators
944 Expression type = (Expression) $4;
947 current_array_type = null;
949 foreach (VariableDeclaration var in (ArrayList) $5) {
950 FixedField field = new FixedField (current_class, type, mod, var.identifier,
951 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
953 if (RootContext.Documentation != null) {
954 field.DocComment = Lexer.consume_doc_comment ();
955 Lexer.doc_state = XmlCommentState.Allowed;
957 current_container.AddField (field);
958 $$ = field; // FIXME: might be better if it points to the top item
967 Report.Error (1641, GetLocation ($4), "A fixed size buffer field must have the array size specifier after the field name");
974 current_array_type = null;
975 Report.Error (670, (Location) $3, "Fields cannot have void type");
979 fixed_variable_declarators
980 : fixed_variable_declarator
982 ArrayList decl = new ArrayList (2);
986 | fixed_variable_declarators COMMA fixed_variable_declarator
988 ArrayList decls = (ArrayList) $1;
994 fixed_variable_declarator
995 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
997 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
999 | IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1001 Report.Error (443, lexer.Location, "Value or constant expected");
1002 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1006 variable_declarators
1007 : variable_declarator
1009 ArrayList decl = new ArrayList (4);
1014 | variable_declarators COMMA variable_declarator
1016 ArrayList decls = (ArrayList) $1;
1023 : IDENTIFIER ASSIGN variable_initializer
1025 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1029 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1031 | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
1033 Report.Error (650, ((LocatedToken) $1).Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
1034 "To declare a fixed size buffer field, use the fixed keyword before the field type");
1039 variable_initializer
1048 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
1050 $$ = new StackAlloc ((Expression) $2, (Expression) $4, (Location) $1);
1054 $$ = new ArglistAccess ((Location) $1);
1058 Report.Error (1575, (Location) $1, "A stackalloc expression requires [] after type");
1065 anonymous_host = (IAnonymousHost) $1;
1066 if (RootContext.Documentation != null)
1067 Lexer.doc_state = XmlCommentState.NotAllowed;
1071 Method method = (Method) $1;
1072 method.Block = (ToplevelBlock) $3;
1073 current_container.AddMethod (method);
1075 anonymous_host = null;
1076 current_local_parameters = null;
1078 if (RootContext.Documentation != null)
1079 Lexer.doc_state = XmlCommentState.Allowed;
1092 Report.Error (1585, lexer.Location,
1093 "Member modifier `{0}' must precede the member type and name",
1094 Modifiers.Name (i));
1105 type namespace_or_type_name
1106 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1108 MemberName name = (MemberName) $4;
1110 Method method = new Method (current_class, null, (Expression) $3, (int) $2,
1111 false, name, (Parameters) $6, (Attributes) $1);
1113 anonymous_host = method;
1114 current_local_parameters = (Parameters) $6;
1116 if (RootContext.Documentation != null)
1117 method.DocComment = Lexer.consume_doc_comment ();
1123 VOID namespace_or_type_name
1124 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1126 MemberName name = (MemberName) $4;
1128 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1129 (int) $2, false, name, (Parameters) $6,
1132 anonymous_host = method;
1133 current_local_parameters = (Parameters) $6;
1135 if (RootContext.Documentation != null)
1136 method.DocComment = Lexer.consume_doc_comment ();
1143 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1145 MemberName name = (MemberName) $5;
1146 Report.Error (1585, name.Location,
1147 "Member modifier `{0}' must precede the member type and name", Modifiers.Name ((int) $4));
1149 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1150 0, false, name, (Parameters) $7, (Attributes) $1);
1152 current_local_parameters = (Parameters) $7;
1154 if (RootContext.Documentation != null)
1155 method.DocComment = Lexer.consume_doc_comment ();
1163 | SEMICOLON { $$ = null; }
1166 opt_formal_parameter_list
1167 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1168 | formal_parameter_list
1171 formal_parameter_list
1174 ArrayList pars_list = (ArrayList) $1;
1176 Parameter [] pars = new Parameter [pars_list.Count];
1177 pars_list.CopyTo (pars);
1179 $$ = new Parameters (pars);
1181 | fixed_parameters COMMA parameter_array
1183 ArrayList pars_list = (ArrayList) $1;
1186 Parameter [] pars = new Parameter [pars_list.Count];
1187 pars_list.CopyTo (pars);
1189 $$ = new Parameters (pars);
1191 | fixed_parameters COMMA ARGLIST
1193 ArrayList pars_list = (ArrayList) $1;
1194 //pars_list.Add (new ArglistParameter (GetLocation ($3)));
1196 Parameter [] pars = new Parameter [pars_list.Count];
1197 pars_list.CopyTo (pars);
1199 $$ = new Parameters (pars, true);
1201 | parameter_array COMMA error
1204 Report.Error (231, ((Parameter) $1).Location, "A params parameter must be the last parameter in a formal parameter list");
1207 | fixed_parameters COMMA parameter_array COMMA error
1210 Report.Error (231, ((Parameter) $3).Location, "A params parameter must be the last parameter in a formal parameter list");
1213 | ARGLIST COMMA error
1215 Report.Error (257, (Location) $1, "An __arglist parameter must be the last parameter in a formal parameter list");
1218 | fixed_parameters COMMA ARGLIST COMMA error
1220 Report.Error (257, (Location) $3, "An __arglist parameter must be the last parameter in a formal parameter list");
1225 $$ = new Parameters (new Parameter[] { (Parameter) $1 } );
1229 $$ = new Parameters (new Parameter[0], true);
1236 ArrayList pars = new ArrayList (4);
1241 | fixed_parameters COMMA fixed_parameter
1243 ArrayList pars = (ArrayList) $1;
1252 opt_parameter_modifier
1256 LocatedToken lt = (LocatedToken) $4;
1257 $$ = new Parameter ((Expression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1260 opt_parameter_modifier
1262 IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1264 LocatedToken lt = (LocatedToken) $4;
1265 Report.Error (1552, lt.Location, "Array type specifier, [], must appear before parameter name");
1269 opt_parameter_modifier
1272 Report.Error (1001, GetLocation ($3), "Identifier expected");
1276 opt_parameter_modifier
1279 CheckIdentifierToken (yyToken, GetLocation ($4));
1283 opt_parameter_modifier
1289 LocatedToken lt = (LocatedToken) $4;
1290 Report.Error (241, lt.Location, "Default parameter specifiers are not permitted");
1295 opt_parameter_modifier
1296 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1297 | parameter_modifier
1301 : REF { $$ = Parameter.Modifier.REF; }
1302 | OUT { $$ = Parameter.Modifier.OUT; }
1306 : opt_attributes PARAMS type IDENTIFIER
1308 LocatedToken lt = (LocatedToken) $4;
1309 $$ = new ParamsParameter ((Expression) $3, lt.Value, (Attributes) $1, lt.Location);
1310 note ("type must be a single-dimension array type");
1312 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1314 Report.Error (1611, (Location) $2, "The params parameter cannot be declared as ref or out");
1317 | opt_attributes PARAMS type error {
1318 CheckIdentifierToken (yyToken, GetLocation ($4));
1323 property_declaration
1327 namespace_or_type_name
1329 if (RootContext.Documentation != null)
1330 tmpComment = Lexer.consume_doc_comment ();
1334 implicit_value_parameter_type = (Expression) $3;
1336 lexer.PropertyParsing = true;
1338 accessor_declarations
1340 lexer.PropertyParsing = false;
1341 has_get = has_set = false;
1349 Pair pair = (Pair) $8;
1350 Accessor get_block = (Accessor) pair.First;
1351 Accessor set_block = (Accessor) pair.Second;
1353 MemberName name = (MemberName) $4;
1355 prop = new Property (current_class, (Expression) $3, (int) $2,
1356 false, name, (Attributes) $1, get_block, set_block);
1358 current_container.AddProperty (prop);
1359 implicit_value_parameter_type = null;
1361 if (RootContext.Documentation != null)
1362 prop.DocComment = ConsumeStoredComment ();
1367 accessor_declarations
1368 : get_accessor_declaration
1370 $$ = new Pair ($1, null);
1372 | get_accessor_declaration accessor_declarations
1374 Pair pair = (Pair) $2;
1378 | set_accessor_declaration
1380 $$ = new Pair (null, $1);
1382 | set_accessor_declaration accessor_declarations
1384 Pair pair = (Pair) $2;
1390 Report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1395 get_accessor_declaration
1396 : opt_attributes opt_modifiers GET
1398 // If this is not the case, then current_local_parameters has already
1399 // been set in indexer_declaration
1400 if (parsing_indexer == false)
1401 current_local_parameters = null;
1403 current_local_parameters = indexer_parameters;
1404 lexer.PropertyParsing = false;
1406 anonymous_host = SimpleAnonymousHost.GetSimple ();
1411 Report.Error (1007, (Location) $3, "Property accessor already defined");
1414 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1416 current_local_parameters = null;
1417 lexer.PropertyParsing = true;
1419 SimpleAnonymousHost.Simple.Propagate (accessor);
1420 anonymous_host = null;
1422 if (RootContext.Documentation != null)
1423 if (Lexer.doc_state == XmlCommentState.Error)
1424 Lexer.doc_state = XmlCommentState.NotAllowed;
1430 set_accessor_declaration
1431 : opt_attributes opt_modifiers SET
1434 Parameter implicit_value_parameter = new Parameter (
1435 implicit_value_parameter_type, "value",
1436 Parameter.Modifier.NONE, null, (Location) $3);
1438 if (parsing_indexer == false) {
1439 args = new Parameter [1];
1440 args [0] = implicit_value_parameter;
1441 current_local_parameters = new Parameters (args);
1443 Parameter [] fpars = indexer_parameters.FixedParameters;
1446 int count = fpars.Length;
1448 args = new Parameter [count + 1];
1449 fpars.CopyTo (args, 0);
1450 args [count] = implicit_value_parameter;
1453 current_local_parameters = new Parameters (
1457 lexer.PropertyParsing = false;
1459 anonymous_host = SimpleAnonymousHost.GetSimple ();
1464 Report.Error (1007, ((LocatedToken) $3).Location, "Property accessor already defined");
1467 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1469 current_local_parameters = null;
1470 lexer.PropertyParsing = true;
1472 SimpleAnonymousHost.Simple.Propagate (accessor);
1473 anonymous_host = null;
1475 if (RootContext.Documentation != null
1476 && Lexer.doc_state == XmlCommentState.Error)
1477 Lexer.doc_state = XmlCommentState.NotAllowed;
1485 | SEMICOLON { $$ = null; }
1488 interface_declaration
1492 INTERFACE member_name
1494 MemberName name = MakeName ((MemberName) $5);
1496 push_current_class (new Interface (
1497 current_namespace, current_class, name, (int) $2,
1498 (Attributes) $1), true, $3);
1502 current_container.AddBasesForPart (current_class, (ArrayList) $7);
1504 if (RootContext.Documentation != null) {
1505 current_container.DocComment = Lexer.consume_doc_comment ();
1506 Lexer.doc_state = XmlCommentState.Allowed;
1511 if (RootContext.Documentation != null)
1512 Lexer.doc_state = XmlCommentState.Allowed;
1516 $$ = pop_current_class ();
1518 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1519 CheckIdentifierToken (yyToken, GetLocation ($5));
1525 opt_interface_member_declarations
1529 opt_interface_member_declarations
1531 | interface_member_declarations
1534 interface_member_declarations
1535 : interface_member_declaration
1536 | interface_member_declarations interface_member_declaration
1539 interface_member_declaration
1540 : interface_method_declaration
1545 Method m = (Method) $1;
1547 if (m.IsExplicitImpl)
1548 Report.Error (541, m.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1549 m.GetSignatureForError ());
1551 current_container.AddMethod (m);
1553 if (RootContext.Documentation != null)
1554 Lexer.doc_state = XmlCommentState.Allowed;
1556 | interface_property_declaration
1561 Property p = (Property) $1;
1563 if (p.IsExplicitImpl)
1564 Report.Error (541, p.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1565 p.GetSignatureForError ());
1567 current_container.AddProperty (p);
1569 if (RootContext.Documentation != null)
1570 Lexer.doc_state = XmlCommentState.Allowed;
1572 | interface_event_declaration
1575 Event e = (Event) $1;
1577 if (e.IsExplicitImpl)
1578 Report.Error (541, e.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1579 e.GetSignatureForError ());
1581 current_container.AddEvent (e);
1584 if (RootContext.Documentation != null)
1585 Lexer.doc_state = XmlCommentState.Allowed;
1587 | interface_indexer_declaration
1592 Indexer i = (Indexer) $1;
1594 if (i.IsExplicitImpl)
1595 Report.Error (541, i.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1596 i.GetSignatureForError ());
1598 current_container.AddIndexer (i);
1600 if (RootContext.Documentation != null)
1601 Lexer.doc_state = XmlCommentState.Allowed;
1603 | delegate_declaration
1606 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1607 ((MemberCore)$1).GetSignatureForError ());
1613 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1614 ((MemberCore)$1).GetSignatureForError ());
1617 | struct_declaration
1620 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1621 ((MemberCore)$1).GetSignatureForError ());
1627 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1628 ((MemberCore)$1).GetSignatureForError ());
1631 | interface_declaration
1634 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1635 ((MemberCore)$1).GetSignatureForError ());
1638 | constant_declaration
1640 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1648 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, GetLocation ($1));
1653 interface_method_declaration
1654 : opt_attributes opt_new type namespace_or_type_name
1655 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1658 $$ = new Method (current_class, null, (Expression) $3, (int) $2, true,
1659 (MemberName) $4, (Parameters) $6, (Attributes) $1);
1660 if (RootContext.Documentation != null)
1661 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1663 | opt_attributes opt_new type namespace_or_type_name
1664 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1665 OPEN_BRACE opt_statement_list CLOSE_BRACE
1667 $$ = new Method (current_class, null, (Expression) $3, (int) $2, true,
1668 (MemberName) $4, (Parameters) $6, (Attributes) $1);
1670 Report.Error (531, lexer.Location, "`{0}': interface members cannot have a definition",
1671 ((Method)$$).GetSignatureForError ());
1675 interface_property_declaration
1680 { lexer.PropertyParsing = true; }
1681 accessor_declarations
1683 has_get = has_set = false;
1684 lexer.PropertyParsing = false;
1688 LocatedToken lt = (LocatedToken) $4;
1689 MemberName name = new MemberName (lt.Value, lt.Location);
1691 if ($3 == TypeManager.system_void_expr) {
1692 Report.Error (547, lt.Location, "`{0}': property or indexer cannot have void type", lt.Value);
1698 p = new Property (current_class, (Expression) $3, (int) $2, true,
1699 name, (Attributes) $1,
1702 Report.Error (548, p.Location, "`{0}': property or indexer must have at least one accessor", p.GetSignatureForError ());
1706 Pair pair = (Pair) $7;
1707 p = new Property (current_class, (Expression) $3, (int) $2, true,
1708 name, (Attributes) $1,
1709 (Accessor)pair.First, (Accessor)pair.Second);
1711 if (pair.First != null && ((Accessor)(pair.First)).Block != null) {
1712 Report.Error (531, p.Location, "`{0}.get': interface members cannot have a definition", p.GetSignatureForError ());
1717 if (pair.Second != null && ((Accessor)(pair.Second)).Block != null) {
1718 Report.Error (531, p.Location, "`{0}.set': interface members cannot have a definition", p.GetSignatureForError ());
1723 if (RootContext.Documentation != null)
1724 p.DocComment = Lexer.consume_doc_comment ();
1731 CheckIdentifierToken (yyToken, GetLocation ($4));
1737 interface_event_declaration
1738 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1740 LocatedToken lt = (LocatedToken) $5;
1741 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1742 new MemberName (lt.Value, lt.Location),
1744 if (RootContext.Documentation != null)
1745 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1747 | opt_attributes opt_new EVENT type error {
1748 CheckIdentifierToken (yyToken, GetLocation ($5));
1751 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1752 LocatedToken lt = (LocatedToken) $5;
1753 Report.Error (68, lt.Location, "`{0}.{1}': event in interface cannot have initializer", current_container.Name, lt.Value);
1756 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1758 lexer.EventParsing = true;
1760 event_accessor_declarations
1762 lexer.EventParsing = false;
1765 Report.Error (69, (Location) $3, "Event in interface cannot have add or remove accessors");
1770 interface_indexer_declaration
1771 : opt_attributes opt_new type THIS
1772 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1774 { lexer.PropertyParsing = true; }
1775 accessor_declarations
1777 has_get = has_set = false;
1778 lexer.PropertyParsing = false;
1784 i = new Indexer (current_class, (Expression) $3,
1785 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1786 (int) $2, true, (Parameters) $6, (Attributes) $1,
1789 Report.Error (548, i.Location, "`{0}': property or indexer must have at least one accessor", i.GetSignatureForError ());
1793 Pair pair = (Pair) $10;
1794 i = new Indexer (current_class, (Expression) $3,
1795 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1796 (int) $2, true, (Parameters) $6, (Attributes) $1,
1797 (Accessor)pair.First, (Accessor)pair.Second);
1799 if (pair.First != null && ((Accessor)(pair.First)).Block != null) {
1800 Report.Error (531, i.Location, "`{0}.get': interface members cannot have a definition", i.GetSignatureForError ());
1805 if (pair.Second != null && ((Accessor)(pair.Second)).Block != null) {
1806 Report.Error (531, i.Location, "`{0}.set': interface members cannot have a definition", i.GetSignatureForError ());
1811 if (RootContext.Documentation != null)
1812 i.DocComment = ConsumeStoredComment ();
1818 operator_declaration
1819 : opt_attributes opt_modifiers operator_declarator
1821 anonymous_host = SimpleAnonymousHost.GetSimple ();
1828 OperatorDeclaration decl = (OperatorDeclaration) $3;
1830 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1832 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null, decl.location);
1833 if (decl.arg2type != null)
1834 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null, decl.location);
1836 Operator op = new Operator (
1837 current_class, decl.optype, decl.ret_type, (int) $2,
1838 new Parameters (param_list),
1839 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1841 if (RootContext.Documentation != null) {
1842 op.DocComment = tmpComment;
1843 Lexer.doc_state = XmlCommentState.Allowed;
1846 SimpleAnonymousHost.Simple.Propagate (op);
1847 anonymous_host = null;
1849 // Note again, checking is done in semantic analysis
1850 current_container.AddOperator (op);
1852 current_local_parameters = null;
1858 | SEMICOLON { $$ = null; }
1861 : type OPERATOR overloadable_operator
1862 OPEN_PARENS opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
1864 // TODO: wrong location
1865 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
1866 Error_ParameterModifierNotValid ((Location) $2);
1868 LocatedToken lt = (LocatedToken) $7;
1869 Operator.OpType op = (Operator.OpType) $3;
1870 CheckUnaryOperator (op, lt.Location);
1872 if (op == Operator.OpType.Addition)
1873 op = Operator.OpType.UnaryPlus;
1875 if (op == Operator.OpType.Subtraction)
1876 op = Operator.OpType.UnaryNegation;
1878 Parameter [] pars = new Parameter [1];
1879 Expression type = (Expression) $6;
1881 pars [0] = new Parameter (type, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1883 current_local_parameters = new Parameters (pars);
1885 if (RootContext.Documentation != null) {
1886 tmpComment = Lexer.consume_doc_comment ();
1887 Lexer.doc_state = XmlCommentState.NotAllowed;
1890 $$ = new OperatorDeclaration (op, (Expression) $1, type, lt.Value,
1891 null, null, (Location) $2);
1893 | type OPERATOR overloadable_operator
1895 opt_parameter_modifier type IDENTIFIER COMMA
1896 opt_parameter_modifier type IDENTIFIER
1899 // TODO: wrong location
1900 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE || (Parameter.Modifier)$9 != Parameter.Modifier.NONE)
1901 Error_ParameterModifierNotValid ((Location) $2);
1903 LocatedToken ltParam1 = (LocatedToken) $7;
1904 LocatedToken ltParam2 = (LocatedToken) $11;
1905 CheckBinaryOperator ((Operator.OpType) $3, (Location) $2);
1907 Parameter [] pars = new Parameter [2];
1909 Expression typeL = (Expression) $6;
1910 Expression typeR = (Expression) $10;
1912 pars [0] = new Parameter (typeL, ltParam1.Value, Parameter.Modifier.NONE, null, ltParam1.Location);
1913 pars [1] = new Parameter (typeR, ltParam2.Value, Parameter.Modifier.NONE, null, ltParam2.Location);
1915 current_local_parameters = new Parameters (pars);
1917 if (RootContext.Documentation != null) {
1918 tmpComment = Lexer.consume_doc_comment ();
1919 Lexer.doc_state = XmlCommentState.NotAllowed;
1922 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1923 typeL, ltParam1.Value,
1924 typeR, ltParam2.Value, (Location) $2);
1926 | conversion_operator_declarator
1927 | type OPERATOR overloadable_operator
1929 opt_parameter_modifier type IDENTIFIER COMMA
1930 opt_parameter_modifier type IDENTIFIER COMMA
1931 opt_parameter_modifier type IDENTIFIER
1934 Report.Error (1534, (Location) $2, "Overloaded binary operator `{0}' takes two parameters",
1935 Operator.GetName ((Operator.OpType) $3));
1938 | type OPERATOR overloadable_operator
1939 OPEN_PARENS CLOSE_PARENS
1941 Report.Error (1535, (Location) $2, "Overloaded unary operator `{0}' takes one parameter",
1942 Operator.GetName ((Operator.OpType) $3));
1947 overloadable_operator
1949 : BANG { $$ = Operator.OpType.LogicalNot; }
1950 | TILDE { $$ = Operator.OpType.OnesComplement; }
1951 | OP_INC { $$ = Operator.OpType.Increment; }
1952 | OP_DEC { $$ = Operator.OpType.Decrement; }
1953 | TRUE { $$ = Operator.OpType.True; }
1954 | FALSE { $$ = Operator.OpType.False; }
1955 // Unary and binary:
1956 | PLUS { $$ = Operator.OpType.Addition; }
1957 | MINUS { $$ = Operator.OpType.Subtraction; }
1959 | STAR { $$ = Operator.OpType.Multiply; }
1960 | DIV { $$ = Operator.OpType.Division; }
1961 | PERCENT { $$ = Operator.OpType.Modulus; }
1962 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1963 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1964 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1965 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1966 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1967 | OP_EQ { $$ = Operator.OpType.Equality; }
1968 | OP_NE { $$ = Operator.OpType.Inequality; }
1969 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1970 | OP_LT { $$ = Operator.OpType.LessThan; }
1971 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1972 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1975 conversion_operator_declarator
1976 : IMPLICIT OPERATOR type OPEN_PARENS opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
1978 // TODO: wrong location
1979 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
1980 Error_ParameterModifierNotValid (GetLocation ($4));
1982 LocatedToken lt = (LocatedToken) $7;
1983 Parameter [] pars = new Parameter [1];
1985 pars [0] = new Parameter ((Expression) $6, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1987 current_local_parameters = new Parameters (pars);
1989 if (RootContext.Documentation != null) {
1990 tmpComment = Lexer.consume_doc_comment ();
1991 Lexer.doc_state = XmlCommentState.NotAllowed;
1994 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $6, lt.Value,
1995 null, null, (Location) $2);
1997 | EXPLICIT OPERATOR type OPEN_PARENS opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
1999 // TODO: wrong location
2000 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
2001 Error_ParameterModifierNotValid (GetLocation ($4));
2003 LocatedToken lt = (LocatedToken) $7;
2004 Parameter [] pars = new Parameter [1];
2006 pars [0] = new Parameter ((Expression) $6, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2008 current_local_parameters = new Parameters (pars);
2010 if (RootContext.Documentation != null) {
2011 tmpComment = Lexer.consume_doc_comment ();
2012 Lexer.doc_state = XmlCommentState.NotAllowed;
2015 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $6, lt.Value,
2016 null, null, (Location) $2);
2020 syntax_error ((Location) $1, "'operator' expected");
2024 syntax_error ((Location) $1, "'operator' expected");
2028 constructor_declaration
2031 constructor_declarator
2034 Constructor c = (Constructor) $3;
2035 c.Block = (ToplevelBlock) $4;
2036 c.OptAttributes = (Attributes) $1;
2037 c.ModFlags = (int) $2;
2039 if (RootContext.Documentation != null)
2040 c.DocComment = ConsumeStoredComment ();
2042 if (c.Name == current_container.Basename){
2043 if ((c.ModFlags & Modifiers.STATIC) != 0){
2044 if ((c.ModFlags & Modifiers.Accessibility) != 0){
2045 Report.Error (515, c.Location,
2046 "`{0}': access modifiers are not allowed on static constructors",
2047 c.GetSignatureForError ());
2050 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2052 if (c.Initializer != null){
2053 Report.Error (514, c.Location,
2054 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2055 c.GetSignatureForError ());
2058 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2061 // We let another layer check the validity of the constructor.
2062 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
2065 current_container.AddConstructor (c);
2067 current_local_parameters = null;
2068 if (RootContext.Documentation != null)
2069 Lexer.doc_state = XmlCommentState.Allowed;
2073 constructor_declarator
2076 if (RootContext.Documentation != null) {
2077 tmpComment = Lexer.consume_doc_comment ();
2078 Lexer.doc_state = XmlCommentState.Allowed;
2081 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2083 current_local_parameters = (Parameters) $4;
2085 opt_constructor_initializer
2087 LocatedToken lt = (LocatedToken) $1;
2088 $$ = new Constructor (current_class, lt.Value, 0, (Parameters) $4,
2089 (ConstructorInitializer) $7, lt.Location);
2091 anonymous_host = (IAnonymousHost) $$;
2097 | SEMICOLON { $$ = null; }
2100 opt_constructor_initializer
2101 : /* empty */ { $$ = null; }
2102 | constructor_initializer
2105 constructor_initializer
2106 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
2108 $$ = new ConstructorBaseInitializer ((ArrayList) $4, (Location) $2);
2110 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
2112 $$ = new ConstructorThisInitializer ((ArrayList) $4, (Location) $2);
2115 Report.Error (1018, (Location) $1, "Keyword this or base expected");
2121 : /* EMPTY */ { $$ = 0; }
2122 | UNSAFE { $$ = Modifiers.UNSAFE; }
2123 | EXTERN { $$ = Modifiers.EXTERN; }
2126 destructor_declaration
2127 : opt_attributes opt_finalizer TILDE
2129 if (RootContext.Documentation != null) {
2130 tmpComment = Lexer.consume_doc_comment ();
2131 Lexer.doc_state = XmlCommentState.NotAllowed;
2134 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2136 LocatedToken lt = (LocatedToken) $5;
2137 if (lt.Value != current_container.Basename){
2138 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2139 } else if (current_container.Kind != Kind.Class){
2140 Report.Error (575, lt.Location, "Only class types can contain destructor");
2142 Location l = lt.Location;
2145 if (!RootContext.StdLib && current_container.Name == "System.Object")
2146 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2148 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2150 Method d = new Destructor (
2151 current_class, TypeManager.system_void_expr, m, "Finalize",
2152 Parameters.EmptyReadOnlyParameters, (Attributes) $1, l);
2153 if (RootContext.Documentation != null)
2154 d.DocComment = ConsumeStoredComment ();
2156 d.Block = (ToplevelBlock) $8;
2157 current_container.AddMethod (d);
2165 EVENT type variable_declarators SEMICOLON
2167 current_array_type = null;
2168 foreach (VariableDeclaration var in (ArrayList) $5) {
2170 MemberName name = new MemberName (var.identifier,
2173 EventField e = new EventField (
2174 current_class, (Expression) $4, (int) $2, false, name,
2177 e.Initializer = var.expression_or_array_initializer;
2179 current_container.AddEvent (e);
2181 if (RootContext.Documentation != null) {
2182 e.DocComment = Lexer.consume_doc_comment ();
2183 Lexer.doc_state = XmlCommentState.Allowed;
2189 EVENT type namespace_or_type_name
2192 implicit_value_parameter_type = (Expression) $4;
2193 lexer.EventParsing = true;
2195 event_accessor_declarations
2197 lexer.EventParsing = false;
2201 MemberName name = (MemberName) $5;
2204 Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2205 current_container.Name, name.ToString ());
2208 Pair pair = (Pair) $8;
2209 if (pair.First == null || pair.Second == null)
2210 // CS0073 is already reported, so no CS0065 here.
2213 Event e = new EventProperty (
2214 current_class, (Expression) $4, (int) $2, false, name,
2215 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second);
2216 if (RootContext.Documentation != null) {
2217 e.DocComment = Lexer.consume_doc_comment ();
2218 Lexer.doc_state = XmlCommentState.Allowed;
2221 current_container.AddEvent (e);
2222 implicit_value_parameter_type = null;
2226 | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
2227 MemberName mn = (MemberName) $5;
2229 if (mn.Left != null)
2230 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2232 Report.Error (71, mn.Location, "Event declaration should use property syntax");
2234 if (RootContext.Documentation != null)
2235 Lexer.doc_state = XmlCommentState.Allowed;
2239 event_accessor_declarations
2240 : add_accessor_declaration remove_accessor_declaration
2242 $$ = new Pair ($1, $2);
2244 | remove_accessor_declaration add_accessor_declaration
2246 $$ = new Pair ($2, $1);
2248 | add_accessor_declaration { $$ = null; }
2249 | remove_accessor_declaration { $$ = null; }
2252 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2258 add_accessor_declaration
2259 : opt_attributes ADD
2261 Parameter [] args = new Parameter [1];
2262 Parameter implicit_value_parameter = new Parameter (
2263 implicit_value_parameter_type, "value",
2264 Parameter.Modifier.NONE, null, (Location) $2);
2266 args [0] = implicit_value_parameter;
2268 current_local_parameters = new Parameters (args);
2269 lexer.EventParsing = false;
2273 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2274 lexer.EventParsing = true;
2276 | opt_attributes ADD error {
2277 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2280 | opt_attributes modifiers ADD {
2281 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2286 remove_accessor_declaration
2287 : opt_attributes REMOVE
2289 Parameter [] args = new Parameter [1];
2290 Parameter implicit_value_parameter = new Parameter (
2291 implicit_value_parameter_type, "value",
2292 Parameter.Modifier.NONE, null, (Location) $2);
2294 args [0] = implicit_value_parameter;
2296 current_local_parameters = new Parameters (args);
2297 lexer.EventParsing = false;
2301 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2302 lexer.EventParsing = true;
2304 | opt_attributes REMOVE error {
2305 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2308 | opt_attributes modifiers REMOVE {
2309 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2315 : opt_attributes opt_modifiers indexer_declarator
2318 IndexerDeclaration decl = (IndexerDeclaration) $3;
2320 implicit_value_parameter_type = decl.type;
2322 lexer.PropertyParsing = true;
2323 parsing_indexer = true;
2325 indexer_parameters = decl.param_list;
2326 anonymous_host = SimpleAnonymousHost.GetSimple ();
2328 accessor_declarations
2330 lexer.PropertyParsing = false;
2331 has_get = has_set = false;
2332 parsing_indexer = false;
2339 // The signature is computed from the signature of the indexer. Look
2340 // at section 3.6 on the spec
2342 IndexerDeclaration decl = (IndexerDeclaration) $3;
2343 Location loc = decl.location;
2344 Pair pair = (Pair) $6;
2345 Accessor get_block = (Accessor) pair.First;
2346 Accessor set_block = (Accessor) pair.Second;
2349 if (decl.interface_type != null)
2350 name = new MemberName (decl.interface_type, TypeContainer.DefaultIndexerName, loc);
2352 name = new MemberName (TypeContainer.DefaultIndexerName, loc);
2354 indexer = new Indexer (current_class, decl.type, name,
2355 (int) $2, false, decl.param_list, (Attributes) $1,
2356 get_block, set_block);
2358 if (RootContext.Documentation != null)
2359 indexer.DocComment = ConsumeStoredComment ();
2361 current_container.AddIndexer (indexer);
2363 current_local_parameters = null;
2364 implicit_value_parameter_type = null;
2365 indexer_parameters = null;
2370 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2372 Parameters pars = (Parameters) $4;
2373 if (pars.HasArglist) {
2374 // "__arglist is not valid in this context"
2375 Report.Error (1669, (Location) $2, "__arglist is not valid in this context");
2376 } else if (pars.Empty){
2377 Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
2379 if (RootContext.Documentation != null) {
2380 tmpComment = Lexer.consume_doc_comment ();
2381 Lexer.doc_state = XmlCommentState.Allowed;
2384 $$ = new IndexerDeclaration ((Expression) $1, null, pars, (Location) $2);
2386 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2388 Parameters pars = (Parameters) $6;
2390 if (pars.HasArglist) {
2391 // "__arglist is not valid in this context"
2392 Report.Error (1669, (Location) $4, "__arglist is not valid in this context");
2393 } else if (pars.Empty){
2394 Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
2397 MemberName name = (MemberName) $2;
2398 $$ = new IndexerDeclaration ((Expression) $1, name, pars, (Location) $4);
2400 if (RootContext.Documentation != null) {
2401 tmpComment = Lexer.consume_doc_comment ();
2402 Lexer.doc_state = XmlCommentState.Allowed;
2412 if (RootContext.Documentation != null)
2413 enumTypeComment = Lexer.consume_doc_comment ();
2418 LocatedToken lt = (LocatedToken) $4;
2419 Location enum_location = lt.Location;
2421 MemberName name = MakeName (new MemberName (lt.Value, enum_location));
2422 Enum e = new Enum (current_namespace, current_class, (Expression) $5, (int) $2,
2423 name, (Attributes) $1);
2425 if (RootContext.Documentation != null)
2426 e.DocComment = enumTypeComment;
2429 EnumMember em = null;
2430 foreach (VariableDeclaration ev in (ArrayList) $7) {
2431 em = new EnumMember (e, em, (Expression) ev.expression_or_array_initializer,
2432 new MemberName (ev.identifier, ev.Location), ev.OptAttributes);
2434 // if (RootContext.Documentation != null)
2435 em.DocComment = ev.DocComment;
2437 e.AddEnumMember (em);
2440 current_container.AddEnum (e);
2447 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2448 | COLON type { $$ = $2; }
2454 if (RootContext.Documentation != null)
2455 Lexer.doc_state = XmlCommentState.Allowed;
2457 opt_enum_member_declarations
2459 // here will be evaluated after CLOSE_BLACE is consumed.
2460 if (RootContext.Documentation != null)
2461 Lexer.doc_state = XmlCommentState.Allowed;
2469 opt_enum_member_declarations
2470 : /* empty */ { $$ = new ArrayList (4); }
2471 | enum_member_declarations opt_comma { $$ = $1; }
2474 enum_member_declarations
2475 : enum_member_declaration
2477 ArrayList l = new ArrayList (4);
2482 | enum_member_declarations COMMA enum_member_declaration
2484 ArrayList l = (ArrayList) $1;
2492 enum_member_declaration
2493 : opt_attributes IDENTIFIER
2495 VariableDeclaration vd = new VariableDeclaration (
2496 (LocatedToken) $2, null, (Attributes) $1);
2498 if (RootContext.Documentation != null) {
2499 vd.DocComment = Lexer.consume_doc_comment ();
2500 Lexer.doc_state = XmlCommentState.Allowed;
2505 | opt_attributes IDENTIFIER
2507 if (RootContext.Documentation != null) {
2508 tmpComment = Lexer.consume_doc_comment ();
2509 Lexer.doc_state = XmlCommentState.NotAllowed;
2514 VariableDeclaration vd = new VariableDeclaration (
2515 (LocatedToken) $2, $5, (Attributes) $1);
2517 if (RootContext.Documentation != null)
2518 vd.DocComment = ConsumeStoredComment ();
2524 delegate_declaration
2527 DELEGATE type member_name
2528 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2531 MemberName name = MakeName ((MemberName) $5);
2532 Parameters p = (Parameters) $7;
2534 // TODO: wrong location
2535 Report.Error (1669, name.Location, "__arglist is not valid in this context");
2538 Delegate del = new Delegate (current_namespace, current_class, (Expression) $4,
2539 (int) $2, name, p, (Attributes) $1);
2541 if (RootContext.Documentation != null) {
2542 del.DocComment = Lexer.consume_doc_comment ();
2543 Lexer.doc_state = XmlCommentState.Allowed;
2546 current_container.AddDelegate (del);
2551 namespace_or_type_name
2553 | IDENTIFIER DOUBLE_COLON IDENTIFIER {
2554 LocatedToken lt1 = (LocatedToken) $1;
2555 LocatedToken lt2 = (LocatedToken) $3;
2556 $$ = new MemberName (lt1.Value, lt2.Value, lt2.Location);
2558 | namespace_or_type_name DOT IDENTIFIER {
2559 LocatedToken lt = (LocatedToken) $3;
2560 $$ = new MemberName ((MemberName) $1, lt.Value);
2566 LocatedToken lt = (LocatedToken) $1;
2567 $$ = new MemberName (lt.Value, lt.Location);
2572 * Before you think of adding a return_type, notice that we have been
2573 * using two rules in the places where it matters (one rule using type
2574 * and another identical one that uses VOID as the return type). This
2575 * gets rid of a shift/reduce couple
2578 : namespace_or_type_name
2580 MemberName name = (MemberName) $1;
2581 $$ = name.GetTypeExpression ();
2593 // Note that here only unmanaged types are allowed but we
2594 // can't perform checks during this phase - we do it during
2595 // semantic analysis.
2597 $$ = new ComposedCast ((Expression) $1, "*", Lexer.Location);
2601 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2607 | non_expression_type rank_specifier
2609 Location loc = GetLocation ($1);
2611 loc = lexer.Location;
2612 $$ = new ComposedCast ((Expression) $1, (string) $2, loc);
2614 | non_expression_type STAR
2616 Location loc = GetLocation ($1);
2618 loc = lexer.Location;
2619 $$ = new ComposedCast ((Expression) $1, "*", loc);
2621 | expression rank_specifiers
2623 $$ = new ComposedCast ((Expression) $1, (string) $2);
2627 $$ = new ComposedCast ((Expression) $1, "*");
2631 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2632 // through this different path
2634 | multiplicative_expression STAR
2636 $$ = new ComposedCast ((Expression) $1, "*");
2643 ArrayList types = new ArrayList (4);
2648 | type_list COMMA type
2650 ArrayList types = (ArrayList) $1;
2658 * replaces all the productions for isolating the various
2659 * simple types, but we need this to reuse it easily in local_variable_type
2662 : OBJECT { $$ = TypeManager.system_object_expr; }
2663 | STRING { $$ = TypeManager.system_string_expr; }
2664 | BOOL { $$ = TypeManager.system_boolean_expr; }
2665 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2666 | FLOAT { $$ = TypeManager.system_single_expr; }
2667 | DOUBLE { $$ = TypeManager.system_double_expr; }
2672 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2673 | BYTE { $$ = TypeManager.system_byte_expr; }
2674 | SHORT { $$ = TypeManager.system_int16_expr; }
2675 | USHORT { $$ = TypeManager.system_uint16_expr; }
2676 | INT { $$ = TypeManager.system_int32_expr; }
2677 | UINT { $$ = TypeManager.system_uint32_expr; }
2678 | LONG { $$ = TypeManager.system_int64_expr; }
2679 | ULONG { $$ = TypeManager.system_uint64_expr; }
2680 | CHAR { $$ = TypeManager.system_char_expr; }
2681 | VOID { $$ = TypeManager.system_void_expr; }
2685 : type rank_specifiers
2687 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2);
2692 // Expressions, section 7.5
2702 MemberName mn = (MemberName) $1;
2703 $$ = mn.GetTypeExpression ();
2705 | IDENTIFIER DOUBLE_COLON IDENTIFIER
2707 LocatedToken lt1 = (LocatedToken) $1;
2708 LocatedToken lt2 = (LocatedToken) $3;
2709 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, lt2.Location);
2711 | parenthesized_expression
2713 | invocation_expression
2717 | post_increment_expression
2718 | post_decrement_expression
2722 | checked_expression
2723 | unchecked_expression
2724 | pointer_member_access
2725 | anonymous_method_expression
2732 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
2733 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); }
2734 | NULL { $$ = new NullLiteral (lexer.Location); }
2738 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
2739 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
2740 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
2745 object v = lexer.Value;
2748 $$ = new IntLiteral ((int) v, lexer.Location);
2749 } else if (v is uint)
2750 $$ = new UIntLiteral ((UInt32) v, lexer.Location);
2752 $$ = new LongLiteral ((Int64) v, lexer.Location);
2753 else if (v is ulong)
2754 $$ = new ULongLiteral ((UInt64) v, lexer.Location);
2756 Console.WriteLine ("OOPS. Unexpected result from scanner");
2761 : TRUE { $$ = new BoolLiteral (true, lexer.Location); }
2762 | FALSE { $$ = new BoolLiteral (false, lexer.Location); }
2765 parenthesized_expression_0
2766 : OPEN_PARENS expression CLOSE_PARENS
2769 lexer.Deambiguate_CloseParens ($$);
2770 // After this, the next token returned is one of
2771 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST (CLOSE_PARENS), CLOSE_PARENS_OPEN_PARENS
2772 // or CLOSE_PARENS_MINUS.
2774 | OPEN_PARENS expression error { CheckToken (1026, yyToken, "Expecting ')'", lexer.Location); }
2777 parenthesized_expression
2778 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2782 | parenthesized_expression_0 CLOSE_PARENS
2786 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2788 // If a parenthesized expression is followed by a minus, we need to wrap
2789 // the expression inside a ParenthesizedExpression for the CS0075 check
2790 // in Binary.DoResolve().
2791 $$ = new ParenthesizedExpression ((Expression) $1);
2796 : primary_expression DOT IDENTIFIER
2798 LocatedToken lt = (LocatedToken) $3;
2799 $$ = new MemberAccess ((Expression) $1, lt.Value);
2801 | predefined_type DOT IDENTIFIER
2803 LocatedToken lt = (LocatedToken) $3;
2804 // TODO: Location is wrong as some predefined types doesn't hold a location
2805 $$ = new MemberAccess ((Expression) $1, lt.Value, lt.Location);
2813 invocation_expression
2814 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2817 Report.Error (1, (Location) $2, "Parse error");
2819 $$ = new Invocation ((Expression) $1, (ArrayList) $3);
2821 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2823 $$ = new Invocation ((Expression) $1, new ArrayList ());
2825 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2827 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
2829 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2831 ArrayList args = new ArrayList (1);
2833 $$ = new Invocation ((Expression) $1, args);
2835 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2837 ArrayList args = ((ArrayList) $4);
2839 $$ = new Invocation ((Expression) $1, args);
2844 : /* empty */ { $$ = null; }
2851 ArrayList list = new ArrayList (4);
2855 | argument_list COMMA argument
2857 ArrayList list = (ArrayList) $1;
2861 | argument_list error {
2862 CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
2870 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2872 | non_simple_argument
2879 : REF variable_reference
2881 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2883 | OUT variable_reference
2885 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2887 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2889 ArrayList list = (ArrayList) $3;
2890 Argument[] args = new Argument [list.Count];
2891 list.CopyTo (args, 0);
2893 Expression expr = new Arglist (args, (Location) $1);
2894 $$ = new Argument (expr, Argument.AType.Expression);
2898 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
2903 : expression { note ("section 5.4"); $$ = $1; }
2907 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2909 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
2911 | primary_expression rank_specifiers
2913 // So the super-trick is that primary_expression
2914 // can only be either a SimpleName or a MemberAccess.
2915 // The MemberAccess case arises when you have a fully qualified type-name like :
2917 // SimpleName is when you have
2920 Expression expr = (Expression) $1;
2921 if (expr is ComposedCast){
2922 $$ = new ComposedCast (expr, (string) $2);
2923 } else if (!(expr is SimpleName || expr is MemberAccess || expr is QualifiedAliasMember)){
2924 Error_ExpectingTypeName (expr);
2925 $$ = TypeManager.system_object_expr;
2928 // So we extract the string corresponding to the SimpleName
2931 $$ = new ComposedCast (expr, (string) $2);
2933 current_array_type = (Expression)$$;
2940 ArrayList list = new ArrayList (4);
2944 | expression_list COMMA expression
2946 ArrayList list = (ArrayList) $1;
2955 $$ = new This (current_block, (Location) $1);
2960 : BASE DOT IDENTIFIER
2962 LocatedToken lt = (LocatedToken) $3;
2963 $$ = new BaseAccess (lt.Value, lt.Location);
2965 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2967 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
2970 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
2975 post_increment_expression
2976 : primary_expression OP_INC
2978 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2979 (Expression) $1, (Location) $2);
2983 post_decrement_expression
2984 : primary_expression OP_DEC
2986 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2987 (Expression) $1, (Location) $2);
2992 : object_or_delegate_creation_expression
2993 | array_creation_expression
2996 object_or_delegate_creation_expression
2997 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2999 $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
3003 array_creation_expression
3004 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
3006 opt_array_initializer
3008 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
3010 | NEW type rank_specifiers array_initializer
3012 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, (Location) $1);
3016 Report.Error (1031, (Location) $1, "Type expected");
3021 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
3038 : rank_specifier opt_rank_specifier
3040 $$ = (string) $2 + (string) $1;
3045 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3047 $$ = "[" + (string) $2 + "]";
3067 | dim_separators COMMA
3069 $$ = (string) $1 + ",";
3073 opt_array_initializer
3085 : OPEN_BRACE CLOSE_BRACE
3087 ArrayList list = new ArrayList (4);
3090 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3092 $$ = (ArrayList) $2;
3096 variable_initializer_list
3097 : variable_initializer
3099 ArrayList list = new ArrayList (4);
3103 | variable_initializer_list COMMA variable_initializer
3105 ArrayList list = (ArrayList) $1;
3114 pushed_current_array_type = current_array_type;
3116 OPEN_PARENS type CLOSE_PARENS
3118 Expression type = (Expression)$4;
3119 if (type == TypeManager.system_void_expr)
3120 $$ = new TypeOfVoid ((Location) $1);
3122 $$ = new TypeOf (type, (Location) $1);
3123 current_array_type = pushed_current_array_type;
3128 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3129 $$ = new SizeOf ((Expression) $3, (Location) $1);
3134 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3136 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3140 unchecked_expression
3141 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3143 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3147 pointer_member_access
3148 : primary_expression OP_PTR IDENTIFIER
3151 LocatedToken lt = (LocatedToken) $3;
3153 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
3154 $$ = new MemberAccess (deref, lt.Value);
3158 anonymous_method_expression
3159 : DELEGATE opt_anonymous_method_signature
3161 if (oob_stack == null)
3162 oob_stack = new Stack (6);
3164 oob_stack.Push (current_anonymous_method);
3165 oob_stack.Push (current_local_parameters);
3166 current_local_parameters = (Parameters)$2;
3168 // Force the next block to be created as a ToplevelBlock
3169 oob_stack.Push (current_block);
3170 oob_stack.Push (top_current_block);
3172 Location loc = (Location) $1;
3173 current_anonymous_method = new AnonymousMethodExpression (
3174 current_anonymous_method, null, current_container,
3175 (Parameters) $2, (ToplevelBlock) top_current_block, loc);
3177 parsing_anonymous_method = true;
3181 Location loc = (Location) $1;
3182 top_current_block = (Block) oob_stack.Pop ();
3183 current_block = (Block) oob_stack.Pop ();
3185 if (RootContext.Version == LanguageVersion.ISO_1){
3186 Report.FeatureIsNotStandardized (loc, "anonymous methods");
3189 ToplevelBlock anon_block = (ToplevelBlock) $4;
3191 anon_block.Parent = current_block;
3193 current_anonymous_method.Block = anon_block;
3194 if ((anonymous_host != null) && (current_anonymous_method.Parent == null))
3195 anonymous_host.AddAnonymousMethod (current_anonymous_method);
3197 $$ = current_anonymous_method;
3200 current_local_parameters = (Parameters) oob_stack.Pop ();
3201 current_anonymous_method = (AnonymousMethodExpression) oob_stack.Pop ();
3205 opt_anonymous_method_signature
3206 : /* empty */ { $$ = null; }
3207 | anonymous_method_signature
3210 anonymous_method_signature
3211 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3214 $$ = Parameters.EmptyReadOnlyParameters;
3216 ArrayList par_list = (ArrayList) $2;
3217 Parameter [] pars = new Parameter [par_list.Count];
3218 par_list.CopyTo (pars);
3219 $$ = new Parameters (pars);
3224 opt_anonymous_method_parameter_list
3225 : /* empty */ { $$ = null; }
3226 | anonymous_method_parameter_list { $$ = $1; }
3229 anonymous_method_parameter_list
3230 : anonymous_method_parameter
3232 ArrayList a = new ArrayList (4);
3236 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3238 ArrayList a = (ArrayList) $1;
3244 anonymous_method_parameter
3245 : opt_parameter_modifier type IDENTIFIER {
3246 LocatedToken lt = (LocatedToken) $3;
3247 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3249 | PARAMS type IDENTIFIER {
3250 Report.Error (1670, ((LocatedToken) $3).Location, "The `params' modifier is not allowed in anonymous method declaration");
3256 : primary_expression
3257 | BANG prefixed_unary_expression
3259 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3261 | TILDE prefixed_unary_expression
3263 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3269 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3271 $$ = new Cast ((Expression) $1, (Expression) $3);
3273 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3275 $$ = new Cast ((Expression) $1, (Expression) $3);
3281 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3283 // TODO: wrong location
3284 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3289 // The idea to split this out is from Rhys' grammar
3290 // to solve the problem with casts.
3292 prefixed_unary_expression
3294 | PLUS prefixed_unary_expression
3296 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3298 | MINUS prefixed_unary_expression
3300 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3302 | OP_INC prefixed_unary_expression
3304 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3305 (Expression) $2, (Location) $1);
3307 | OP_DEC prefixed_unary_expression
3309 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3310 (Expression) $2, (Location) $1);
3312 | STAR prefixed_unary_expression
3314 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, (Location) $1);
3316 | BITWISE_AND prefixed_unary_expression
3318 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3322 pre_increment_expression
3323 : OP_INC prefixed_unary_expression
3325 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3326 (Expression) $2, (Location) $1);
3330 pre_decrement_expression
3331 : OP_DEC prefixed_unary_expression
3333 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3334 (Expression) $2, (Location) $1);
3338 multiplicative_expression
3339 : prefixed_unary_expression
3340 | multiplicative_expression STAR prefixed_unary_expression
3342 $$ = new Binary (Binary.Operator.Multiply,
3343 (Expression) $1, (Expression) $3);
3345 | multiplicative_expression DIV prefixed_unary_expression
3347 $$ = new Binary (Binary.Operator.Division,
3348 (Expression) $1, (Expression) $3);
3350 | multiplicative_expression PERCENT prefixed_unary_expression
3352 $$ = new Binary (Binary.Operator.Modulus,
3353 (Expression) $1, (Expression) $3);
3358 : multiplicative_expression
3359 | additive_expression PLUS multiplicative_expression
3361 $$ = new Binary (Binary.Operator.Addition,
3362 (Expression) $1, (Expression) $3);
3364 | additive_expression MINUS multiplicative_expression
3366 $$ = new Binary (Binary.Operator.Subtraction,
3367 (Expression) $1, (Expression) $3);
3372 : additive_expression
3373 | shift_expression OP_SHIFT_LEFT additive_expression
3375 $$ = new Binary (Binary.Operator.LeftShift,
3376 (Expression) $1, (Expression) $3);
3378 | shift_expression OP_SHIFT_RIGHT additive_expression
3380 $$ = new Binary (Binary.Operator.RightShift,
3381 (Expression) $1, (Expression) $3);
3385 relational_expression
3387 | relational_expression OP_LT shift_expression
3389 $$ = new Binary (Binary.Operator.LessThan,
3390 (Expression) $1, (Expression) $3);
3392 | relational_expression OP_GT shift_expression
3394 $$ = new Binary (Binary.Operator.GreaterThan,
3395 (Expression) $1, (Expression) $3);
3397 | relational_expression OP_LE shift_expression
3399 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3400 (Expression) $1, (Expression) $3);
3402 | relational_expression OP_GE shift_expression
3404 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3405 (Expression) $1, (Expression) $3);
3407 | relational_expression IS type
3409 $$ = new Is ((Expression) $1, (Expression) $3, (Location) $2);
3411 | relational_expression AS type
3413 $$ = new As ((Expression) $1, (Expression) $3, (Location) $2);
3418 : relational_expression
3419 | equality_expression OP_EQ relational_expression
3421 $$ = new Binary (Binary.Operator.Equality,
3422 (Expression) $1, (Expression) $3);
3424 | equality_expression OP_NE relational_expression
3426 $$ = new Binary (Binary.Operator.Inequality,
3427 (Expression) $1, (Expression) $3);
3432 : equality_expression
3433 | and_expression BITWISE_AND equality_expression
3435 $$ = new Binary (Binary.Operator.BitwiseAnd,
3436 (Expression) $1, (Expression) $3);
3440 exclusive_or_expression
3442 | exclusive_or_expression CARRET and_expression
3444 $$ = new Binary (Binary.Operator.ExclusiveOr,
3445 (Expression) $1, (Expression) $3);
3449 inclusive_or_expression
3450 : exclusive_or_expression
3451 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3453 $$ = new Binary (Binary.Operator.BitwiseOr,
3454 (Expression) $1, (Expression) $3);
3458 conditional_and_expression
3459 : inclusive_or_expression
3460 | conditional_and_expression OP_AND inclusive_or_expression
3462 $$ = new Binary (Binary.Operator.LogicalAnd,
3463 (Expression) $1, (Expression) $3);
3467 conditional_or_expression
3468 : conditional_and_expression
3469 | conditional_or_expression OP_OR conditional_and_expression
3471 $$ = new Binary (Binary.Operator.LogicalOr,
3472 (Expression) $1, (Expression) $3);
3476 conditional_expression
3477 : conditional_or_expression
3478 | conditional_or_expression INTERR expression COLON expression
3480 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
3484 assignment_expression
3485 : prefixed_unary_expression ASSIGN expression
3487 $$ = new Assign ((Expression) $1, (Expression) $3);
3489 | prefixed_unary_expression OP_MULT_ASSIGN expression
3491 $$ = new CompoundAssign (
3492 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
3494 | prefixed_unary_expression OP_DIV_ASSIGN expression
3496 $$ = new CompoundAssign (
3497 Binary.Operator.Division, (Expression) $1, (Expression) $3);
3499 | prefixed_unary_expression OP_MOD_ASSIGN expression
3501 $$ = new CompoundAssign (
3502 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
3504 | prefixed_unary_expression OP_ADD_ASSIGN expression
3506 $$ = new CompoundAssign (
3507 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
3509 | prefixed_unary_expression OP_SUB_ASSIGN expression
3511 $$ = new CompoundAssign (
3512 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3514 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3516 $$ = new CompoundAssign (
3517 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
3519 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3521 $$ = new CompoundAssign (
3522 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
3524 | prefixed_unary_expression OP_AND_ASSIGN expression
3526 $$ = new CompoundAssign (
3527 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
3529 | prefixed_unary_expression OP_OR_ASSIGN expression
3531 $$ = new CompoundAssign (
3532 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
3534 | prefixed_unary_expression OP_XOR_ASSIGN expression
3536 $$ = new CompoundAssign (
3537 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
3542 : conditional_expression
3543 | assignment_expression
3563 MemberName name = MakeName ((MemberName) $5);
3564 int mod_flags = (int) $2;
3566 push_current_class (new Class (
3567 current_namespace, current_class, name,
3568 mod_flags, (Attributes) $1), false, $3);
3573 if (current_class.Name == "System.Object") {
3574 Report.Error (537, current_class.Location,
3575 "The class System.Object cannot have a base " +
3576 "class or implement an interface.");
3578 current_container.AddBasesForPart (current_class, (ArrayList) $7);
3581 if (RootContext.Documentation != null) {
3582 current_container.DocComment = Lexer.consume_doc_comment ();
3583 Lexer.doc_state = XmlCommentState.Allowed;
3588 if (RootContext.Documentation != null)
3589 Lexer.doc_state = XmlCommentState.Allowed;
3593 $$ = pop_current_class ();
3601 { $$ = $1; } // location
3605 : /* empty */ { $$ = (int) 0; }
3611 | modifiers modifier
3616 if ((m1 & m2) != 0) {
3617 Location l = lexer.Location;
3618 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
3620 $$ = (int) (m1 | m2);
3625 : NEW { $$ = Modifiers.NEW; }
3626 | PUBLIC { $$ = Modifiers.PUBLIC; }
3627 | PROTECTED { $$ = Modifiers.PROTECTED; }
3628 | INTERNAL { $$ = Modifiers.INTERNAL; }
3629 | PRIVATE { $$ = Modifiers.PRIVATE; }
3630 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3631 | SEALED { $$ = Modifiers.SEALED; }
3632 | STATIC { $$ = Modifiers.STATIC; }
3633 | READONLY { $$ = Modifiers.READONLY; }
3634 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3635 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3636 | EXTERN { $$ = Modifiers.EXTERN; }
3637 | VOLATILE { $$ = Modifiers.VOLATILE; }
3638 | UNSAFE { $$ = Modifiers.UNSAFE; }
3642 : /* empty */ { $$ = null; }
3643 | class_base { $$ = $1; }
3647 : COLON type_list { $$ = $2; }
3655 // A block is "contained" on the following places:
3657 // property_declaration as part of the accessor body (get/set)
3658 // operator_declaration
3659 // constructor_declaration
3660 // destructor_declaration
3661 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3666 if (parsing_anonymous_method) {
3667 top_current_block = new ToplevelBlock (
3668 current_block, current_local_parameters, null, (Location) $1);
3669 if (current_block != null)
3670 current_block.AddAnonymousChild ((ToplevelBlock) top_current_block);
3671 current_block = top_current_block;
3672 parsing_anonymous_method = false;
3673 } else if (current_block == null){
3674 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, (Location) $1);
3675 top_current_block = current_block;
3677 current_block = new Block (current_block, (Location) $1, Location.Null);
3680 opt_statement_list CLOSE_BRACE
3682 while (current_block.Implicit)
3683 current_block = current_block.Parent;
3685 current_block.SetEndLocation ((Location) $4);
3686 current_block = current_block.Parent;
3687 if (current_block == null)
3688 top_current_block = null;
3699 | statement_list statement
3703 : declaration_statement
3705 if ($1 != null && (Block) $1 != current_block){
3706 current_block.AddStatement ((Statement) $1);
3707 current_block = (Block) $1;
3710 | valid_declaration_statement
3712 current_block.AddStatement ((Statement) $1);
3717 valid_declaration_statement
3720 | expression_statement
3721 | selection_statement
3722 | iteration_statement
3726 | unchecked_statement
3734 : valid_declaration_statement
3735 | declaration_statement
3737 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3742 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3750 $$ = EmptyStatement.Value;
3757 LocatedToken lt = (LocatedToken) $1;
3758 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
3760 if (current_block.AddLabel (labeled))
3761 current_block.AddStatement (labeled);
3766 declaration_statement
3767 : local_variable_declaration SEMICOLON
3769 current_array_type = null;
3771 DictionaryEntry de = (DictionaryEntry) $1;
3772 Expression e = (Expression) de.Key;
3774 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
3778 | local_constant_declaration SEMICOLON
3780 current_array_type = null;
3782 DictionaryEntry de = (DictionaryEntry) $1;
3784 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3790 * The following is from Rhys' grammar:
3791 * > Types in local variable declarations must be recognized as
3792 * > expressions to prevent reduce/reduce errors in the grammar.
3793 * > The expressions are converted into types during semantic analysis.
3796 : primary_expression opt_rank_specifier
3798 // FIXME: Do something smart here regarding the composition of the type.
3800 // Ok, the above "primary_expression" is there to get rid of
3801 // both reduce/reduce and shift/reduces in the grammar, it should
3802 // really just be "type_name". If you use type_name, a reduce/reduce
3803 // creeps up. If you use namespace_or_type_name (which is all we need
3804 // really) two shift/reduces appear.
3807 // So the super-trick is that primary_expression
3808 // can only be either a SimpleName or a MemberAccess.
3809 // The MemberAccess case arises when you have a fully qualified type-name like :
3811 // SimpleName is when you have
3814 Expression expr = (Expression) $1;
3815 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3816 Error_ExpectingTypeName (expr);
3820 // So we extract the string corresponding to the SimpleName
3824 if ((string) $2 == "")
3827 $$ = new ComposedCast ((Expression) $1, (string) $2);
3830 | builtin_types opt_rank_specifier
3832 if ((string) $2 == "")
3835 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3839 local_variable_pointer_type
3840 : primary_expression STAR
3842 Expression expr = (Expression) $1;
3844 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3845 Error_ExpectingTypeName (expr);
3849 $$ = new ComposedCast ((Expression) $1, "*");
3851 | builtin_types STAR
3853 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3857 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
3859 | local_variable_pointer_type STAR
3861 $$ = new ComposedCast ((Expression) $1, "*");
3865 local_variable_declaration
3866 : local_variable_type variable_declarators
3869 $$ = new DictionaryEntry ($1, $2);
3873 | local_variable_pointer_type opt_rank_specifier variable_declarators
3878 if ((string) $2 == "")
3879 t = (Expression) $1;
3881 t = new ComposedCast ((Expression) $1, (string) $2);
3882 $$ = new DictionaryEntry (t, $3);
3888 local_constant_declaration
3889 : CONST local_variable_type constant_declarators
3892 $$ = new DictionaryEntry ($2, $3);
3898 expression_statement
3899 : statement_expression SEMICOLON { $$ = $1; }
3903 // We have to do the wrapping here and not in the case above,
3904 // because statement_expression is used for example in for_statement
3906 statement_expression
3909 Expression expr = (Expression) $1;
3910 ExpressionStatement s = expr as ExpressionStatement;
3912 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
3915 $$ = new StatementExpression (s);
3919 Report.Error (1002, GetLocation ($1), "Expecting `;'");
3924 object_creation_expression
3925 : object_or_delegate_creation_expression
3926 { note ("complain if this is a delegate maybe?"); }
3935 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
3938 Location l = (Location) $1;
3940 $$ = new If ((Expression) $3, (Statement) $5, l);
3942 // FIXME: location for warning should be loc property of $5.
3943 if ($5 == EmptyStatement.Value)
3944 Report.Warning (642, 3, l, "Possible mistaken empty statement");
3947 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
3948 embedded_statement ELSE embedded_statement
3950 Location l = (Location) $1;
3952 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
3954 // FIXME: location for warning should be loc property of $5 and $7.
3955 if ($5 == EmptyStatement.Value)
3956 Report.Warning (642, 3, l, "Possible mistaken empty statement");
3957 if ($7 == EmptyStatement.Value)
3958 Report.Warning (642, 3, l, "Possible mistaken empty statement");
3963 : SWITCH OPEN_PARENS
3965 if (switch_stack == null)
3966 switch_stack = new Stack (2);
3967 switch_stack.Push (current_block);
3969 expression CLOSE_PARENS
3972 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
3973 current_block = (Block) switch_stack.Pop ();
3989 Report.Warning (1522, 1, lexer.Location, "Empty switch block");
3990 $$ = new ArrayList ();
3998 ArrayList sections = new ArrayList (4);
4003 | switch_sections switch_section
4005 ArrayList sections = (ArrayList) $1;
4015 current_block = current_block.CreateSwitchBlock (lexer.Location);
4019 Block topmost = current_block;
4021 while (topmost.Implicit)
4022 topmost = topmost.Parent;
4023 $$ = new SwitchSection ((ArrayList) $1, topmost);
4030 ArrayList labels = new ArrayList (4);
4035 | switch_labels switch_label
4037 ArrayList labels = (ArrayList) ($1);
4045 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
4046 | DEFAULT COLON { $$ = new SwitchLabel (null, (Location) $1); }
4049 1523, GetLocation ($1),
4050 "The keyword case or default must precede code in switch block");
4062 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
4064 Location l = (Location) $1;
4065 $$ = new While ((Expression) $3, (Statement) $5, l);
4070 : DO embedded_statement
4071 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
4073 Location l = (Location) $1;
4075 $$ = new Do ((Statement) $2, (Expression) $5, l);
4081 opt_for_initializer SEMICOLON
4083 Block assign_block = new Block (current_block);
4084 current_block = assign_block;
4086 if ($3 is DictionaryEntry){
4087 DictionaryEntry de = (DictionaryEntry) $3;
4089 Expression type = (Expression) de.Key;
4090 ArrayList var_declarators = (ArrayList) de.Value;
4092 foreach (VariableDeclaration decl in var_declarators){
4096 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4100 Location l = lexer.Location;
4101 Expression expr = decl.expression_or_array_initializer;
4103 LocalVariableReference var;
4104 var = new LocalVariableReference (assign_block, decl.identifier, l);
4107 Assign a = new Assign (var, expr, decl.Location);
4109 assign_block.AddStatement (new StatementExpression (a));
4113 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4114 // This can be referred to as $5 below.
4120 opt_for_condition SEMICOLON
4121 opt_for_iterator CLOSE_PARENS
4124 Location l = (Location) $1;
4126 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4128 current_block.AddStatement (f);
4129 while (current_block.Implicit)
4130 current_block = current_block.Parent;
4132 current_block = current_block.Parent;
4137 : /* empty */ { $$ = EmptyStatement.Value; }
4142 : local_variable_declaration
4143 | statement_expression_list
4147 : /* empty */ { $$ = null; }
4148 | boolean_expression
4152 : /* empty */ { $$ = EmptyStatement.Value; }
4157 : statement_expression_list
4160 statement_expression_list
4161 : statement_expression
4163 // CHANGE: was `null'
4164 Statement s = (Statement) $1;
4165 Block b = new Block (current_block, Block.Flags.Implicit, s.loc, lexer.Location);
4170 | statement_expression_list COMMA statement_expression
4172 Block b = (Block) $1;
4174 b.AddStatement ((Statement) $3);
4180 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4182 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4185 | FOREACH OPEN_PARENS type IDENTIFIER IN
4186 expression CLOSE_PARENS
4188 Block foreach_block = new Block (current_block);
4189 current_block = foreach_block;
4191 LocatedToken lt = (LocatedToken) $4;
4192 Location l = lt.Location;
4195 vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4197 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4199 // Get a writable reference to this read-only variable.
4201 // Note that the $$ here refers to the value of _this_ code block,
4202 // not the value of the LHS non-terminal. This can be referred to as $8 below.
4203 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
4210 LocalVariableReference v = (LocalVariableReference) $8;
4211 Location l = (Location) $1;
4214 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
4215 current_block.AddStatement (f);
4218 while (current_block.Implicit)
4219 current_block = current_block.Parent;
4221 current_block = current_block.Parent;
4227 | continue_statement
4237 $$ = new Break ((Location) $1);
4242 : CONTINUE SEMICOLON
4244 $$ = new Continue ((Location) $1);
4249 : GOTO IDENTIFIER SEMICOLON
4251 LocatedToken lt = (LocatedToken) $2;
4252 $$ = new Goto (lt.Value, lt.Location);
4254 | GOTO CASE constant_expression SEMICOLON
4256 $$ = new GotoCase ((Expression) $3, (Location) $1);
4258 | GOTO DEFAULT SEMICOLON
4260 $$ = new GotoDefault ((Location) $1);
4265 : RETURN opt_expression SEMICOLON
4267 $$ = new Return ((Expression) $2, (Location) $1);
4272 : THROW opt_expression SEMICOLON
4274 $$ = new Throw ((Expression) $2, (Location) $1);
4279 : IDENTIFIER RETURN expression SEMICOLON
4281 LocatedToken lt = (LocatedToken) $1;
4282 string s = lt.Value;
4284 Report.Error (1003, lt.Location, "; expected");
4287 if (RootContext.Version == LanguageVersion.ISO_1){
4288 Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4291 if (anonymous_host == null){
4292 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4295 anonymous_host.SetYields ();
4296 $$ = new Yield ((Expression) $3, lt.Location);
4299 | IDENTIFIER RETURN SEMICOLON
4301 Report.Error (1627, (Location) $2, "Expression expected after yield return");
4304 | IDENTIFIER BREAK SEMICOLON
4306 LocatedToken lt = (LocatedToken) $1;
4307 string s = lt.Value;
4309 Report.Error (1003, lt.Location, "; expected");
4312 if (RootContext.Version == LanguageVersion.ISO_1){
4313 Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4316 if (anonymous_host == null){
4317 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4320 anonymous_host.SetYields ();
4321 $$ = new YieldBreak (lt.Location);
4332 : TRY block catch_clauses
4336 ArrayList c = (ArrayList)$3;
4337 for (int i = 0; i < c.Count; ++i) {
4338 Catch cc = (Catch) c [i];
4340 if (i != c.Count - 1)
4341 Report.Error (1017, cc.loc, "Try statement already has an empty catch block");
4348 // Now s contains the list of specific catch clauses
4349 // and g contains the general one.
4351 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4353 | TRY block opt_catch_clauses FINALLY block
4356 ArrayList s = new ArrayList (4);
4357 ArrayList catch_list = (ArrayList) $3;
4359 if (catch_list != null){
4360 foreach (Catch cc in catch_list) {
4368 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4372 Report.Error (1524, (Location) $1, "Expected catch or finally");
4378 : /* empty */ { $$ = null; }
4385 ArrayList l = new ArrayList (4);
4390 | catch_clauses catch_clause
4392 ArrayList l = (ArrayList) $1;
4400 : /* empty */ { $$ = null; }
4405 : CATCH opt_catch_args
4407 Expression type = null;
4410 DictionaryEntry cc = (DictionaryEntry) $2;
4411 type = (Expression) cc.Key;
4412 LocatedToken lt = (LocatedToken) cc.Value;
4415 ArrayList one = new ArrayList (4);
4417 one.Add (new VariableDeclaration (lt, null));
4419 current_block = new Block (current_block);
4420 Block b = declare_local_variables (type, one, lt.Location);
4425 Expression type = null;
4427 Block var_block = null;
4430 DictionaryEntry cc = (DictionaryEntry) $2;
4431 type = (Expression) cc.Key;
4432 LocatedToken lt = (LocatedToken) cc.Value;
4436 while (current_block.Implicit)
4437 current_block = current_block.Parent;
4438 var_block = current_block;
4439 current_block = current_block.Parent;
4443 $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
4448 : /* empty */ { $$ = null; }
4453 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4455 $$ = new DictionaryEntry ($2, $3);
4463 $$ = new Checked ((Block) $2);
4470 $$ = new Unchecked ((Block) $2);
4477 RootContext.CheckUnsafeOption ((Location) $1);
4479 $$ = new Unsafe ((Block) $3);
4485 type fixed_pointer_declarators
4488 ArrayList list = (ArrayList) $4;
4489 Expression type = (Expression) $3;
4490 Location l = (Location) $1;
4491 int top = list.Count;
4493 Block assign_block = new Block (current_block);
4494 current_block = assign_block;
4496 for (int i = 0; i < top; i++){
4497 Pair p = (Pair) list [i];
4500 v = current_block.AddVariable (type, (string) p.First, l);
4504 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4512 Location l = (Location) $1;
4514 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4516 current_block.AddStatement (f);
4517 while (current_block.Implicit)
4518 current_block = current_block.Parent;
4520 current_block = current_block.Parent;
4524 fixed_pointer_declarators
4525 : fixed_pointer_declarator {
4526 ArrayList declarators = new ArrayList (4);
4528 declarators.Add ($1);
4531 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4533 ArrayList declarators = (ArrayList) $1;
4535 declarators.Add ($3);
4540 fixed_pointer_declarator
4541 : IDENTIFIER ASSIGN expression
4543 LocatedToken lt = (LocatedToken) $1;
4544 // FIXME: keep location
4545 $$ = new Pair (lt.Value, $3);
4549 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
4555 : LOCK OPEN_PARENS expression CLOSE_PARENS
4561 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
4566 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4568 Block assign_block = new Block (current_block);
4569 current_block = assign_block;
4571 if ($3 is DictionaryEntry){
4572 DictionaryEntry de = (DictionaryEntry) $3;
4573 Location l = (Location) $1;
4575 Expression type = (Expression) de.Key;
4576 ArrayList var_declarators = (ArrayList) de.Value;
4578 ArrayList vars = new ArrayList (4);
4580 foreach (VariableDeclaration decl in var_declarators){
4582 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4585 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
4587 Expression expr = decl.expression_or_array_initializer;
4589 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4592 LocalVariableReference var;
4594 // Get a writable reference to this read-only variable.
4595 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4597 // This is so that it is not a warning on using variables
4600 vars.Add (new DictionaryEntry (var, expr));
4602 // Assign a = new Assign (var, expr, decl.Location);
4603 // assign_block.AddStatement (new StatementExpression (a));
4606 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4607 // It can be referred to as $5 below.
4608 $$ = new DictionaryEntry (type, vars);
4615 Using u = new Using ($5, (Statement) $6, (Location) $1);
4616 current_block.AddStatement (u);
4617 while (current_block.Implicit)
4618 current_block = current_block.Parent;
4620 current_block = current_block.Parent;
4624 resource_acquisition
4625 : local_variable_declaration
4632 // A class used to pass around variable declarations and constants
4634 public class VariableDeclaration {
4635 public string identifier;
4636 public Expression expression_or_array_initializer;
4637 public Location Location;
4638 public Attributes OptAttributes;
4639 public string DocComment;
4641 public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
4643 this.identifier = lt.Value;
4644 if (eoai is ArrayList) {
4645 if (CSharpParser.current_array_type == null)
4646 Report.Error (622, lt.Location,
4647 "Can only use array initializer expressions to assign to array types. Try using a new expression instead.");
4648 this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
4650 this.expression_or_array_initializer = (Expression)eoai;
4652 this.Location = lt.Location;
4653 this.OptAttributes = opt_attrs;
4656 public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
4662 // A class used to hold info about an indexer declarator
4664 public class IndexerDeclaration {
4665 public Expression type;
4666 public MemberName interface_type;
4667 public Parameters param_list;
4668 public Location location;
4670 public IndexerDeclaration (Expression type, MemberName interface_type,
4671 Parameters param_list, Location loc)
4674 this.interface_type = interface_type;
4675 this.param_list = param_list;
4676 this.location = loc;
4681 // We use this when we do not have an object in advance that is an IAnonymousHost
4683 public class SimpleAnonymousHost : IAnonymousHost {
4684 public static readonly SimpleAnonymousHost Simple = new SimpleAnonymousHost ();
4687 ArrayList anonymous_methods;
4689 public static SimpleAnonymousHost GetSimple () {
4690 Simple.yields = false;
4691 Simple.anonymous_methods = null;
4695 public void SetYields ()
4700 public void AddAnonymousMethod (AnonymousMethodExpression anonymous)
4702 if (anonymous_methods == null)
4703 anonymous_methods = new ArrayList ();
4704 anonymous_methods.Add (anonymous);
4707 public void Propagate (IAnonymousHost real_host)
4710 real_host.SetYields ();
4711 if (anonymous_methods != null) {
4712 foreach (AnonymousMethodExpression ame in anonymous_methods)
4713 real_host.AddAnonymousMethod (ame);
4719 // A class used to hold info about an operator declarator
4721 public class OperatorDeclaration {
4722 public Operator.OpType optype;
4723 public Expression ret_type, arg1type, arg2type;
4724 public string arg1name, arg2name;
4725 public Location location;
4727 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4728 Expression arg1type, string arg1name,
4729 Expression arg2type, string arg2name, Location location)
4732 this.ret_type = ret_type;
4733 this.arg1type = arg1type;
4734 this.arg1name = arg1name;
4735 this.arg2type = arg2type;
4736 this.arg2name = arg2name;
4737 this.location = location;
4742 void Error_ExpectingTypeName (Expression expr)
4744 if (expr is Invocation){
4745 Report.Error (1002, expr.Location, "Expecting `;'");
4747 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
4751 public static void Error_ParameterModifierNotValid (Location loc)
4753 Report.Error (631, loc, "The modifiers `ref' and `out' are not valid in this context");
4756 void push_current_class (TypeContainer tc, bool is_interface, object partial_token)
4758 if (partial_token != null)
4759 current_container = current_container.AddPartial (tc, is_interface);
4761 current_container = current_container.AddTypeContainer (tc, is_interface);
4765 DeclSpace pop_current_class ()
4767 DeclSpace retval = current_class;
4769 current_class = current_class.Parent;
4770 current_container = current_class.PartialContainer;
4776 // Given the @class_name name, it creates a fully qualified name
4777 // based on the containing declaration space
4780 MakeName (MemberName class_name)
4782 Namespace ns = current_namespace.NS;
4784 if (current_container.Name.Length == 0){
4785 if (ns.Name.Length != 0)
4786 return new MemberName (ns.MemberName, class_name);
4790 return new MemberName (current_container.MemberName, class_name);
4794 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4796 Block implicit_block;
4797 ArrayList inits = null;
4800 // We use the `Used' property to check whether statements
4801 // have been added to the current block. If so, we need
4802 // to create another block to contain the new declaration
4803 // otherwise, as an optimization, we use the same block to
4804 // add the declaration.
4806 // FIXME: A further optimization is to check if the statements
4807 // that were added were added as part of the initialization
4808 // below. In which case, no other statements have been executed
4809 // and we might be able to reduce the number of blocks for
4810 // situations like this:
4812 // int j = 1; int k = j + 1;
4814 if (current_block.Used)
4815 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4817 implicit_block = current_block;
4819 foreach (VariableDeclaration decl in variable_declarators){
4821 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
4822 if (decl.expression_or_array_initializer != null){
4824 inits = new ArrayList (4);
4831 return implicit_block;
4833 foreach (VariableDeclaration decl in inits){
4835 Expression expr = decl.expression_or_array_initializer;
4837 LocalVariableReference var;
4838 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4840 assign = new Assign (var, expr, decl.Location);
4842 implicit_block.AddStatement (new StatementExpression (assign));
4845 return implicit_block;
4848 Block declare_local_constants (Expression type, ArrayList declarators)
4850 Block implicit_block;
4852 if (current_block.Used)
4853 implicit_block = new Block (current_block, Block.Flags.Implicit);
4855 implicit_block = current_block;
4857 foreach (VariableDeclaration decl in declarators){
4858 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
4861 return implicit_block;
4864 void CheckAttributeTarget (string a, Location l)
4868 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4872 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4878 void CheckUnaryOperator (Operator.OpType op, Location l)
4882 case Operator.OpType.LogicalNot:
4883 case Operator.OpType.OnesComplement:
4884 case Operator.OpType.Increment:
4885 case Operator.OpType.Decrement:
4886 case Operator.OpType.True:
4887 case Operator.OpType.False:
4888 case Operator.OpType.Addition:
4889 case Operator.OpType.Subtraction:
4894 Report.Error (1019, l, "Overloadable unary operator expected");
4900 void CheckBinaryOperator (Operator.OpType op, Location l)
4904 case Operator.OpType.Addition:
4905 case Operator.OpType.Subtraction:
4906 case Operator.OpType.Multiply:
4907 case Operator.OpType.Division:
4908 case Operator.OpType.Modulus:
4909 case Operator.OpType.BitwiseAnd:
4910 case Operator.OpType.BitwiseOr:
4911 case Operator.OpType.ExclusiveOr:
4912 case Operator.OpType.LeftShift:
4913 case Operator.OpType.RightShift:
4914 case Operator.OpType.Equality:
4915 case Operator.OpType.Inequality:
4916 case Operator.OpType.GreaterThan:
4917 case Operator.OpType.LessThan:
4918 case Operator.OpType.GreaterThanOrEqual:
4919 case Operator.OpType.LessThanOrEqual:
4923 Report.Error (1020, l, "Overloadable binary operator expected");
4929 void syntax_error (Location l, string msg)
4931 Report.Error (1003, l, "Syntax error, " + msg);
4934 void note (string s)
4936 // Used to put annotations
4941 public Tokenizer Lexer {
4947 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4949 this.name = file.Name;
4951 current_namespace = new NamespaceEntry (null, file, null);
4952 current_class = current_namespace.SlaveDeclSpace;
4953 current_container = current_class.PartialContainer; // == RootContest.ToplevelTypes
4955 lexer = new Tokenizer (reader, file, defines);
4958 public void parse ()
4960 int errors = Report.Errors;
4962 if (yacc_verbose_flag > 1)
4963 yyparse (lexer, new yydebug.yyDebugSimple ());
4966 Tokenizer tokenizer = lexer as Tokenizer;
4967 tokenizer.cleanup ();
4968 } catch (Exception e){
4970 // Removed for production use, use parser verbose to get the output.
4972 // Console.WriteLine (e);
4973 if (Report.Errors == errors)
4974 Report.Error (-25, lexer.Location, "Parsing error");
4975 if (yacc_verbose_flag > 0)
4976 Console.WriteLine (e);
4979 if (RootContext.ToplevelTypes.NamespaceEntry != null)
4980 throw new InternalErrorException ("who set it?");
4983 static void CheckToken (int error, int yyToken, string msg, Location loc)
4985 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
4986 Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ());
4988 Report.Error (error, loc, msg);
4991 void CheckIdentifierToken (int yyToken, Location loc)
4993 CheckToken (1041, yyToken, "Identifier expected", loc);
4996 string ConsumeStoredComment ()
4998 string s = tmpComment;
5000 Lexer.doc_state = XmlCommentState.Allowed;
5004 Location GetLocation (object obj)
5006 if (obj is MemberCore)
5007 return ((MemberCore) obj).Location;
5008 if (obj is MemberName)
5009 return ((MemberName) obj).Location;
5010 if (obj is LocatedToken)
5011 return ((LocatedToken) obj).Location;
5012 if (obj is Location)
5013 return (Location) obj;
5014 return lexer.Location;