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
207 /* C# keywords which are not really keywords */
213 /* C# single character operators/punctuation. */
214 %token OPEN_BRACE "{"
215 %token CLOSE_BRACE "}"
216 %token OPEN_BRACKET "["
217 %token CLOSE_BRACKET "]"
218 %token OPEN_PARENS "("
219 %token CLOSE_PARENS ")"
231 %token OP_GENERICS_LT "<"
233 %token OP_GENERICS_GT ">"
234 %token BITWISE_AND "&"
235 %token BITWISE_OR "|"
242 /* C# multi-character operators. */
243 %token DOUBLE_COLON "::"
246 %token OP_SHIFT_LEFT "<<"
247 %token OP_SHIFT_RIGHT ">>"
254 %token OP_MULT_ASSIGN "*="
255 %token OP_DIV_ASSIGN "/="
256 %token OP_MOD_ASSIGN "%="
257 %token OP_ADD_ASSIGN "+="
258 %token OP_SUB_ASSIGN "-="
259 %token OP_SHIFT_LEFT_ASSIGN "<<="
260 %token OP_SHIFT_RIGHT_ASSIGN ">>="
261 %token OP_AND_ASSIGN "&="
262 %token OP_XOR_ASSIGN "^="
263 %token OP_OR_ASSIGN "|="
267 %token LITERAL_INTEGER "int literal"
268 %token LITERAL_FLOAT "float literal"
269 %token LITERAL_DOUBLE "double literal"
270 %token LITERAL_DECIMAL "decimal literal"
271 %token LITERAL_CHARACTER "character literal"
272 %token LITERAL_STRING "string literal"
275 %token OPEN_PARENS_LAMBDA
276 %token CLOSE_PARENS_CAST
277 %token CLOSE_PARENS_NO_CAST
278 %token CLOSE_PARENS_OPEN_PARENS
279 %token CLOSE_PARENS_MINUS
281 /* Add precedence rules to solve dangling else s/r conflict */
290 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
292 %left STAR DIV PERCENT
293 %right BANG CARRET UMINUS
294 %nonassoc OP_INC OP_DEC
296 %left OPEN_BRACKET OPEN_BRACE
300 %start compilation_unit
304 : outer_declarations opt_EOF
305 | outer_declarations global_attributes opt_EOF
306 | global_attributes opt_EOF
307 | opt_EOF /* allow empty files */
313 Lexer.check_incorrect_doc_comment ();
317 Lexer.check_incorrect_doc_comment ();
323 | outer_declarations outer_declaration
327 : extern_alias_directive
329 | namespace_member_declaration
332 extern_alias_directives
333 : extern_alias_directive
334 | extern_alias_directives extern_alias_directive;
336 extern_alias_directive
337 : EXTERN IDENTIFIER IDENTIFIER SEMICOLON
339 LocatedToken lt = (LocatedToken) $2;
342 Report.Error (1003, lt.Location, "'alias' expected");
343 } else if (RootContext.Version == LanguageVersion.ISO_1) {
344 Report.FeatureIsNotISO1 (lt.Location, "external alias");
346 lt = (LocatedToken) $3;
347 current_namespace.UsingExternalAlias (lt.Value, lt.Location);
354 | using_directives using_directive
358 : using_alias_directive
360 if (RootContext.Documentation != null)
361 Lexer.doc_state = XmlCommentState.Allowed;
363 | using_namespace_directive
365 if (RootContext.Documentation != null)
366 Lexer.doc_state = XmlCommentState.Allowed;
370 using_alias_directive
371 : USING IDENTIFIER ASSIGN
372 namespace_or_type_name SEMICOLON
374 LocatedToken lt = (LocatedToken) $2;
375 current_namespace.UsingAlias (lt.Value, (MemberName) $4, (Location) $1);
378 CheckIdentifierToken (yyToken, GetLocation ($2));
382 using_namespace_directive
383 : USING namespace_name SEMICOLON
385 current_namespace.Using ((MemberName) $2, (Location) $1);
390 // Strictly speaking, namespaces don't have attributes but
391 // we parse global attributes along with namespace declarations and then
394 namespace_declaration
395 : opt_attributes NAMESPACE namespace_or_type_name
397 MemberName name = (MemberName) $3;
400 Report.Error(1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
403 current_namespace = new NamespaceEntry (
404 current_namespace, file, name.GetName ());
405 current_class = current_namespace.SlaveDeclSpace;
406 current_container = current_class.PartialContainer;
408 namespace_body opt_semicolon
410 current_namespace = current_namespace.Parent;
411 current_class = current_namespace.SlaveDeclSpace;
412 current_container = current_class.PartialContainer;
427 : namespace_or_type_name
433 if (RootContext.Documentation != null)
434 Lexer.doc_state = XmlCommentState.Allowed;
440 : opt_extern_alias_directives
442 opt_namespace_member_declarations
446 Report.Error (1518, lexer.Location, "Expected `class', `delegate', `enum', `interface', or `struct'");
449 | opt_extern_alias_directives
451 opt_namespace_member_declarations
454 Report.Error (1513, lexer.Location, "} expected");
463 opt_extern_alias_directives
465 | extern_alias_directives
468 opt_namespace_member_declarations
470 | namespace_member_declarations
473 namespace_member_declarations
474 : namespace_member_declaration
475 | namespace_member_declarations namespace_member_declaration
478 namespace_member_declaration
482 DeclSpace ds = (DeclSpace)$1;
484 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
485 Report.Error (1527, ds.Location,
486 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
489 current_namespace.DeclarationFound = true;
491 | namespace_declaration {
492 current_namespace.DeclarationFound = true;
495 | field_declaration {
496 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
498 | method_declaration {
499 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
506 | interface_declaration
508 | delegate_declaration
510 // Enable this when we have handled all errors, because this acts as a generic fallback
513 // Console.WriteLine ("Token=" + yyToken);
514 // Report.Error (1518, GetLocation ($1), "Expected class, struct, interface, enum or delegate");
526 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
534 global_attrs_enabled = false;
539 global_attrs_enabled = false;
548 ArrayList sect = (ArrayList) $1;
550 if (global_attrs_enabled) {
551 if (current_attr_target == "module") {
552 CodeGen.Module.AddAttributes (sect);
554 } else if (current_attr_target != null && current_attr_target.Length > 0) {
555 CodeGen.Assembly.AddAttributes (sect);
558 $$ = new Attributes (sect);
561 if (RootContext.Documentation != null) {
562 Lexer.check_incorrect_doc_comment ();
564 XmlCommentState.Allowed;
568 $$ = new Attributes (sect);
570 current_attr_target = null;
572 | attribute_sections attribute_section
574 Attributes attrs = $1 as Attributes;
575 ArrayList sect = (ArrayList) $2;
577 if (global_attrs_enabled) {
578 if (current_attr_target == "module") {
579 CodeGen.Module.AddAttributes (sect);
581 } else if (current_attr_target == "assembly") {
582 CodeGen.Assembly.AddAttributes (sect);
586 attrs = new Attributes (sect);
588 attrs.AddAttributes (sect);
592 attrs = new Attributes (sect);
594 attrs.AddAttributes (sect);
597 current_attr_target = null;
602 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
606 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
612 attribute_target_specifier
613 : attribute_target COLON
615 current_attr_target = (string)$1;
623 LocatedToken lt = (LocatedToken) $1;
624 CheckAttributeTarget (lt.Value, lt.Location);
625 $$ = lt.Value; // Location won't be required anymore.
627 | EVENT { $$ = "event"; }
628 | RETURN { $$ = "return"; }
634 ArrayList attrs = new ArrayList (4);
640 | attribute_list COMMA attribute
642 ArrayList attrs = (ArrayList) $1;
650 : attribute_name opt_attribute_arguments
652 MemberName mname = (MemberName) $1;
653 object[] arguments = (object[]) $2;
654 MemberName left = mname.Left;
655 string identifier = mname.Name;
657 Expression left_expr = left == null ? null : left.GetTypeExpression ();
659 if (current_attr_target == "assembly" || current_attr_target == "module")
660 // FIXME: supply "nameEscaped" parameter here.
661 $$ = new GlobalAttribute (current_namespace, current_attr_target,
662 left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
664 $$ = new Attribute (current_attr_target, left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
669 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
672 opt_attribute_arguments
673 : /* empty */ { $$ = null; }
674 | OPEN_PARENS attribute_arguments CLOSE_PARENS
682 : opt_positional_argument_list
687 $$ = new object [] { $1, null };
690 | positional_argument_list COMMA named_argument_list
692 $$ = new object[] { $1, $3 };
694 | named_argument_list
696 $$ = new object [] { null, $1 };
701 opt_positional_argument_list
702 : /* empty */ { $$ = null; }
703 | positional_argument_list
706 positional_argument_list
709 ArrayList args = new ArrayList (4);
710 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
714 | positional_argument_list COMMA expression
716 ArrayList args = (ArrayList) $1;
717 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
726 ArrayList args = new ArrayList (4);
731 | named_argument_list COMMA named_argument
733 ArrayList args = (ArrayList) $1;
738 | named_argument_list COMMA expression
740 Report.Error (1016, ((Expression) $3).Location, "Named attribute argument expected");
746 : IDENTIFIER ASSIGN expression
748 // FIXME: keep location
749 $$ = new DictionaryEntry (
750 ((LocatedToken) $1).Value,
751 new Argument ((Expression) $3, Argument.AType.Expression));
757 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
760 opt_class_member_declarations
762 | class_member_declarations
765 class_member_declarations
766 : class_member_declaration
767 | class_member_declarations
768 class_member_declaration
771 class_member_declaration
772 : constant_declaration // done
773 | field_declaration // done
774 | method_declaration // done
775 | property_declaration // done
776 | event_declaration // done
777 | indexer_declaration // done
778 | operator_declaration // done
779 | constructor_declaration // done
780 | destructor_declaration // done
790 MemberName name = MakeName ((MemberName) $5);
791 push_current_class (new Struct (
792 current_namespace, current_class, name, (int) $2,
793 (Attributes) $1), false, $3);
799 current_container.AddBasesForPart (current_class, (ArrayList) $7);
801 if (RootContext.Documentation != null)
802 current_container.DocComment = Lexer.consume_doc_comment ();
806 if (RootContext.Documentation != null)
807 Lexer.doc_state = XmlCommentState.Allowed;
811 $$ = pop_current_class ();
813 | opt_attributes opt_modifiers opt_partial STRUCT error {
814 CheckIdentifierToken (yyToken, GetLocation ($5));
821 if (RootContext.Documentation != null)
822 Lexer.doc_state = XmlCommentState.Allowed;
824 opt_struct_member_declarations CLOSE_BRACE
827 opt_struct_member_declarations
829 | struct_member_declarations
832 struct_member_declarations
833 : struct_member_declaration
834 | struct_member_declarations struct_member_declaration
837 struct_member_declaration
838 : constant_declaration
841 | property_declaration
843 | indexer_declaration
844 | operator_declaration
845 | constructor_declaration
849 * This is only included so we can flag error 575:
850 * destructors only allowed on class types
852 | destructor_declaration
863 int modflags = (int) $2;
864 foreach (VariableDeclaration constant in (ArrayList) $5){
865 Location l = constant.Location;
866 if ((modflags & Modifiers.STATIC) != 0) {
867 Report.Error (504, l, "The constant `{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
871 Const c = new Const (
872 current_class, (Expression) $4, (string) constant.identifier,
873 (Expression) constant.expression_or_array_initializer, modflags,
876 if (RootContext.Documentation != null) {
877 c.DocComment = Lexer.consume_doc_comment ();
878 Lexer.doc_state = XmlCommentState.Allowed;
880 current_container.AddConstant (c);
886 : constant_declarator
888 ArrayList constants = new ArrayList (4);
893 | constant_declarators COMMA constant_declarator
896 ArrayList constants = (ArrayList) $1;
903 : IDENTIFIER ASSIGN constant_expression
905 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
909 // A const field requires a value to be provided
910 Report.Error (145, ((LocatedToken) $1).Location, "A const field requires a value to be provided");
922 Expression type = (Expression) $3;
925 current_array_type = null;
927 foreach (VariableDeclaration var in (ArrayList) $4){
928 Field field = new Field (current_class, type, mod, var.identifier,
929 (Attributes) $1, var.Location);
931 field.Initializer = var.expression_or_array_initializer;
933 if (RootContext.Documentation != null) {
934 field.DocComment = Lexer.consume_doc_comment ();
935 Lexer.doc_state = XmlCommentState.Allowed;
937 current_container.AddField (field);
938 $$ = field; // FIXME: might be better if it points to the top item
945 fixed_variable_declarators
948 Expression type = (Expression) $4;
951 current_array_type = null;
953 foreach (VariableDeclaration var in (ArrayList) $5) {
954 FixedField field = new FixedField (current_class, type, mod, var.identifier,
955 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
957 if (RootContext.Documentation != null) {
958 field.DocComment = Lexer.consume_doc_comment ();
959 Lexer.doc_state = XmlCommentState.Allowed;
961 current_container.AddField (field);
962 $$ = field; // FIXME: might be better if it points to the top item
971 Report.Error (1641, GetLocation ($4), "A fixed size buffer field must have the array size specifier after the field name");
978 current_array_type = null;
979 Report.Error (670, (Location) $3, "Fields cannot have void type");
983 fixed_variable_declarators
984 : fixed_variable_declarator
986 ArrayList decl = new ArrayList (2);
990 | fixed_variable_declarators COMMA fixed_variable_declarator
992 ArrayList decls = (ArrayList) $1;
998 fixed_variable_declarator
999 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
1001 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1003 | IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1005 Report.Error (443, lexer.Location, "Value or constant expected");
1006 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1010 variable_declarators
1011 : variable_declarator
1013 ArrayList decl = new ArrayList (4);
1018 | variable_declarators COMMA variable_declarator
1020 ArrayList decls = (ArrayList) $1;
1027 : IDENTIFIER ASSIGN variable_initializer
1029 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1033 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1035 | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
1037 Report.Error (650, ((LocatedToken) $1).Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
1038 "To declare a fixed size buffer field, use the fixed keyword before the field type");
1043 variable_initializer
1052 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
1054 $$ = new StackAlloc ((Expression) $2, (Expression) $4, (Location) $1);
1058 $$ = new ArglistAccess ((Location) $1);
1062 Report.Error (1575, (Location) $1, "A stackalloc expression requires [] after type");
1069 anonymous_host = (IAnonymousHost) $1;
1070 if (RootContext.Documentation != null)
1071 Lexer.doc_state = XmlCommentState.NotAllowed;
1075 Method method = (Method) $1;
1076 method.Block = (ToplevelBlock) $3;
1077 current_container.AddMethod (method);
1079 anonymous_host = null;
1080 current_local_parameters = null;
1082 if (RootContext.Documentation != null)
1083 Lexer.doc_state = XmlCommentState.Allowed;
1096 Report.Error (1585, lexer.Location,
1097 "Member modifier `{0}' must precede the member type and name",
1098 Modifiers.Name (i));
1109 type namespace_or_type_name
1110 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1112 MemberName name = (MemberName) $4;
1114 Method method = new Method (current_class, null, (Expression) $3, (int) $2,
1115 false, name, (Parameters) $6, (Attributes) $1);
1117 anonymous_host = method;
1118 current_local_parameters = (Parameters) $6;
1120 if (RootContext.Documentation != null)
1121 method.DocComment = Lexer.consume_doc_comment ();
1127 VOID namespace_or_type_name
1128 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1130 MemberName name = (MemberName) $4;
1132 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1133 (int) $2, false, name, (Parameters) $6,
1136 anonymous_host = method;
1137 current_local_parameters = (Parameters) $6;
1139 if (RootContext.Documentation != null)
1140 method.DocComment = Lexer.consume_doc_comment ();
1147 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1149 MemberName name = (MemberName) $5;
1150 Report.Error (1585, name.Location,
1151 "Member modifier `{0}' must precede the member type and name", Modifiers.Name ((int) $4));
1153 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1154 0, false, name, (Parameters) $7, (Attributes) $1);
1156 current_local_parameters = (Parameters) $7;
1158 if (RootContext.Documentation != null)
1159 method.DocComment = Lexer.consume_doc_comment ();
1167 | SEMICOLON { $$ = null; }
1170 opt_formal_parameter_list
1171 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1172 | formal_parameter_list
1175 formal_parameter_list
1178 ArrayList pars_list = (ArrayList) $1;
1180 Parameter [] pars = new Parameter [pars_list.Count];
1181 pars_list.CopyTo (pars);
1183 $$ = new Parameters (pars);
1185 | fixed_parameters COMMA parameter_array
1187 ArrayList pars_list = (ArrayList) $1;
1190 Parameter [] pars = new Parameter [pars_list.Count];
1191 pars_list.CopyTo (pars);
1193 $$ = new Parameters (pars);
1195 | fixed_parameters COMMA ARGLIST
1197 ArrayList pars_list = (ArrayList) $1;
1198 //pars_list.Add (new ArglistParameter (GetLocation ($3)));
1200 Parameter [] pars = new Parameter [pars_list.Count];
1201 pars_list.CopyTo (pars);
1203 $$ = new Parameters (pars, true);
1205 | parameter_array COMMA error
1208 Report.Error (231, ((Parameter) $1).Location, "A params parameter must be the last parameter in a formal parameter list");
1211 | fixed_parameters COMMA parameter_array COMMA error
1214 Report.Error (231, ((Parameter) $3).Location, "A params parameter must be the last parameter in a formal parameter list");
1217 | ARGLIST COMMA error
1219 Report.Error (257, (Location) $1, "An __arglist parameter must be the last parameter in a formal parameter list");
1222 | fixed_parameters COMMA ARGLIST COMMA error
1224 Report.Error (257, (Location) $3, "An __arglist parameter must be the last parameter in a formal parameter list");
1229 $$ = new Parameters (new Parameter[] { (Parameter) $1 } );
1233 $$ = new Parameters (new Parameter[0], true);
1240 ArrayList pars = new ArrayList (4);
1245 | fixed_parameters COMMA fixed_parameter
1247 ArrayList pars = (ArrayList) $1;
1256 opt_parameter_modifier
1260 LocatedToken lt = (LocatedToken) $4;
1261 $$ = new Parameter ((Expression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1264 opt_parameter_modifier
1266 IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1268 LocatedToken lt = (LocatedToken) $4;
1269 Report.Error (1552, lt.Location, "Array type specifier, [], must appear before parameter name");
1273 opt_parameter_modifier
1276 Report.Error (1001, GetLocation ($3), "Identifier expected");
1280 opt_parameter_modifier
1283 CheckIdentifierToken (yyToken, GetLocation ($4));
1287 opt_parameter_modifier
1293 LocatedToken lt = (LocatedToken) $4;
1294 Report.Error (241, lt.Location, "Default parameter specifiers are not permitted");
1299 opt_parameter_modifier
1300 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1301 | parameter_modifiers
1305 : parameter_modifier
1309 | parameter_modifiers parameter_modifier
1311 Report.Error (1108, lexer.Location, "A parameter cannot have specified more than one modifier");
1317 : REF { $$ = Parameter.Modifier.REF; }
1318 | OUT { $$ = Parameter.Modifier.OUT; }
1322 : opt_attributes PARAMS type IDENTIFIER
1324 LocatedToken lt = (LocatedToken) $4;
1325 $$ = new ParamsParameter ((Expression) $3, lt.Value, (Attributes) $1, lt.Location);
1327 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1329 Report.Error (1611, (Location) $2, "The params parameter cannot be declared as ref or out");
1332 | opt_attributes PARAMS type error {
1333 CheckIdentifierToken (yyToken, GetLocation ($4));
1338 property_declaration
1342 namespace_or_type_name
1344 if (RootContext.Documentation != null)
1345 tmpComment = Lexer.consume_doc_comment ();
1349 implicit_value_parameter_type = (Expression) $3;
1351 lexer.PropertyParsing = true;
1353 accessor_declarations
1355 lexer.PropertyParsing = false;
1356 has_get = has_set = false;
1364 Accessors accessors = (Accessors) $8;
1365 Accessor get_block = accessors.get_or_add;
1366 Accessor set_block = accessors.set_or_remove;
1368 MemberName name = (MemberName) $4;
1370 prop = new Property (current_class, (Expression) $3, (int) $2,
1371 false, name, (Attributes) $1, get_block, set_block, accessors.declared_in_reverse);
1373 current_container.AddProperty (prop);
1374 implicit_value_parameter_type = null;
1376 if (RootContext.Documentation != null)
1377 prop.DocComment = ConsumeStoredComment ();
1382 accessor_declarations
1383 : get_accessor_declaration
1385 $$ = new Accessors ((Accessor) $1, null);
1387 | get_accessor_declaration accessor_declarations
1389 Accessors accessors = (Accessors) $2;
1390 accessors.get_or_add = (Accessor) $1;
1393 | set_accessor_declaration
1395 $$ = new Accessors (null, (Accessor) $1);
1397 | set_accessor_declaration accessor_declarations
1399 Accessors accessors = (Accessors) $2;
1400 accessors.set_or_remove = (Accessor) $1;
1401 accessors.declared_in_reverse = true;
1406 Report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1411 get_accessor_declaration
1412 : opt_attributes opt_modifiers GET
1414 // If this is not the case, then current_local_parameters has already
1415 // been set in indexer_declaration
1416 if (parsing_indexer == false)
1417 current_local_parameters = null;
1419 current_local_parameters = indexer_parameters;
1420 lexer.PropertyParsing = false;
1422 anonymous_host = SimpleAnonymousHost.GetSimple ();
1427 Report.Error (1007, (Location) $3, "Property accessor already defined");
1430 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1432 current_local_parameters = null;
1433 lexer.PropertyParsing = true;
1435 SimpleAnonymousHost.Simple.Propagate (accessor);
1436 anonymous_host = null;
1438 if (RootContext.Documentation != null)
1439 if (Lexer.doc_state == XmlCommentState.Error)
1440 Lexer.doc_state = XmlCommentState.NotAllowed;
1446 set_accessor_declaration
1447 : opt_attributes opt_modifiers SET
1450 Parameter implicit_value_parameter = new Parameter (
1451 implicit_value_parameter_type, "value",
1452 Parameter.Modifier.NONE, null, (Location) $3);
1454 if (parsing_indexer == false) {
1455 args = new Parameter [1];
1456 args [0] = implicit_value_parameter;
1457 current_local_parameters = new Parameters (args);
1459 Parameter [] fpars = indexer_parameters.FixedParameters;
1462 int count = fpars.Length;
1464 args = new Parameter [count + 1];
1465 fpars.CopyTo (args, 0);
1466 args [count] = implicit_value_parameter;
1469 current_local_parameters = new Parameters (
1473 lexer.PropertyParsing = false;
1475 anonymous_host = SimpleAnonymousHost.GetSimple ();
1480 Report.Error (1007, ((LocatedToken) $3).Location, "Property accessor already defined");
1483 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1485 current_local_parameters = null;
1486 lexer.PropertyParsing = true;
1488 SimpleAnonymousHost.Simple.Propagate (accessor);
1489 anonymous_host = null;
1491 if (RootContext.Documentation != null
1492 && Lexer.doc_state == XmlCommentState.Error)
1493 Lexer.doc_state = XmlCommentState.NotAllowed;
1501 | SEMICOLON { $$ = null; }
1504 interface_declaration
1508 INTERFACE member_name
1510 MemberName name = MakeName ((MemberName) $5);
1512 push_current_class (new Interface (
1513 current_namespace, current_class, name, (int) $2,
1514 (Attributes) $1), true, $3);
1518 current_container.AddBasesForPart (current_class, (ArrayList) $7);
1520 if (RootContext.Documentation != null) {
1521 current_container.DocComment = Lexer.consume_doc_comment ();
1522 Lexer.doc_state = XmlCommentState.Allowed;
1527 if (RootContext.Documentation != null)
1528 Lexer.doc_state = XmlCommentState.Allowed;
1532 $$ = pop_current_class ();
1534 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1535 CheckIdentifierToken (yyToken, GetLocation ($5));
1541 opt_interface_member_declarations
1545 opt_interface_member_declarations
1547 | interface_member_declarations
1550 interface_member_declarations
1551 : interface_member_declaration
1552 | interface_member_declarations interface_member_declaration
1555 interface_member_declaration
1556 : interface_method_declaration
1561 Method m = (Method) $1;
1563 if (m.IsExplicitImpl)
1564 Report.Error (541, m.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1565 m.GetSignatureForError ());
1567 current_container.AddMethod (m);
1569 if (RootContext.Documentation != null)
1570 Lexer.doc_state = XmlCommentState.Allowed;
1572 | interface_property_declaration
1577 Property p = (Property) $1;
1579 if (p.IsExplicitImpl)
1580 Report.Error (541, p.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1581 p.GetSignatureForError ());
1583 current_container.AddProperty (p);
1585 if (RootContext.Documentation != null)
1586 Lexer.doc_state = XmlCommentState.Allowed;
1588 | interface_event_declaration
1591 Event e = (Event) $1;
1593 if (e.IsExplicitImpl)
1594 Report.Error (541, e.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1595 e.GetSignatureForError ());
1597 current_container.AddEvent (e);
1600 if (RootContext.Documentation != null)
1601 Lexer.doc_state = XmlCommentState.Allowed;
1603 | interface_indexer_declaration
1608 Indexer i = (Indexer) $1;
1610 if (i.IsExplicitImpl)
1611 Report.Error (541, i.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1612 i.GetSignatureForError ());
1614 current_container.AddIndexer (i);
1616 if (RootContext.Documentation != null)
1617 Lexer.doc_state = XmlCommentState.Allowed;
1619 | delegate_declaration
1622 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1623 ((MemberCore)$1).GetSignatureForError ());
1629 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1630 ((MemberCore)$1).GetSignatureForError ());
1633 | struct_declaration
1636 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1637 ((MemberCore)$1).GetSignatureForError ());
1643 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1644 ((MemberCore)$1).GetSignatureForError ());
1647 | interface_declaration
1650 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1651 ((MemberCore)$1).GetSignatureForError ());
1654 | constant_declaration
1656 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1664 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, GetLocation ($1));
1669 interface_method_declaration
1670 : opt_attributes opt_new type namespace_or_type_name
1671 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1674 $$ = new Method (current_class, null, (Expression) $3, (int) $2, true,
1675 (MemberName) $4, (Parameters) $6, (Attributes) $1);
1676 if (RootContext.Documentation != null)
1677 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1679 | opt_attributes opt_new type namespace_or_type_name
1680 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1681 OPEN_BRACE opt_statement_list CLOSE_BRACE
1683 $$ = new Method (current_class, null, (Expression) $3, (int) $2, true,
1684 (MemberName) $4, (Parameters) $6, (Attributes) $1);
1686 Report.Error (531, lexer.Location, "`{0}': interface members cannot have a definition",
1687 ((Method)$$).GetSignatureForError ());
1691 interface_property_declaration
1696 { lexer.PropertyParsing = true; }
1697 accessor_declarations
1699 has_get = has_set = false;
1700 lexer.PropertyParsing = false;
1704 LocatedToken lt = (LocatedToken) $4;
1705 MemberName name = new MemberName (lt.Value, lt.Location);
1707 if ($3 == TypeManager.system_void_expr) {
1708 Report.Error (547, lt.Location, "`{0}': property or indexer cannot have void type", lt.Value);
1714 p = new Property (current_class, (Expression) $3, (int) $2, true,
1715 name, (Attributes) $1,
1718 Report.Error (548, p.Location, "`{0}': property or indexer must have at least one accessor", p.GetSignatureForError ());
1722 Accessors accessor = (Accessors) $7;
1723 p = new Property (current_class, (Expression) $3, (int) $2, true,
1724 name, (Attributes) $1,
1725 accessor.get_or_add, accessor.set_or_remove, accessor.declared_in_reverse);
1727 if (accessor.get_or_add != null && accessor.get_or_add.Block != null) {
1728 Report.Error (531, p.Location, "`{0}.get': interface members cannot have a definition", p.GetSignatureForError ());
1733 if (accessor.set_or_remove != null && accessor.set_or_remove.Block != null) {
1734 Report.Error (531, p.Location, "`{0}.set': interface members cannot have a definition", p.GetSignatureForError ());
1739 if (RootContext.Documentation != null)
1740 p.DocComment = Lexer.consume_doc_comment ();
1747 CheckIdentifierToken (yyToken, GetLocation ($4));
1753 interface_event_declaration
1754 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1756 LocatedToken lt = (LocatedToken) $5;
1757 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1758 new MemberName (lt.Value, lt.Location),
1760 if (RootContext.Documentation != null)
1761 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1763 | opt_attributes opt_new EVENT type error {
1764 CheckIdentifierToken (yyToken, GetLocation ($5));
1767 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1768 LocatedToken lt = (LocatedToken) $5;
1769 Report.Error (68, lt.Location, "`{0}.{1}': event in interface cannot have initializer", current_container.Name, lt.Value);
1772 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1774 lexer.EventParsing = true;
1776 event_accessor_declarations
1778 lexer.EventParsing = false;
1781 Report.Error (69, (Location) $3, "Event in interface cannot have add or remove accessors");
1786 interface_indexer_declaration
1787 : opt_attributes opt_new type THIS
1788 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1790 { lexer.PropertyParsing = true; }
1791 accessor_declarations
1793 has_get = has_set = false;
1794 lexer.PropertyParsing = false;
1800 i = new Indexer (current_class, (Expression) $3,
1801 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1802 (int) $2, true, (Parameters) $6, (Attributes) $1,
1805 Report.Error (548, i.Location, "`{0}': property or indexer must have at least one accessor", i.GetSignatureForError ());
1809 Accessors accessors = (Accessors) $10;
1810 i = new Indexer (current_class, (Expression) $3,
1811 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1812 (int) $2, true, (Parameters) $6, (Attributes) $1,
1813 accessors.get_or_add, accessors.set_or_remove, accessors.declared_in_reverse);
1815 if (accessors.get_or_add != null && accessors.get_or_add.Block != null) {
1816 Report.Error (531, i.Location, "`{0}.get': interface members cannot have a definition", i.GetSignatureForError ());
1821 if (accessors.set_or_remove != null && accessors.set_or_remove.Block != null) {
1822 Report.Error (531, i.Location, "`{0}.set': interface members cannot have a definition", i.GetSignatureForError ());
1827 if (RootContext.Documentation != null)
1828 i.DocComment = ConsumeStoredComment ();
1834 operator_declaration
1835 : opt_attributes opt_modifiers operator_declarator
1837 anonymous_host = SimpleAnonymousHost.GetSimple ();
1844 OperatorDeclaration decl = (OperatorDeclaration) $3;
1846 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1848 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null, decl.location);
1849 if (decl.arg2type != null)
1850 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null, decl.location);
1852 Operator op = new Operator (
1853 current_class, decl.optype, decl.ret_type, (int) $2,
1854 new Parameters (param_list),
1855 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1857 if (RootContext.Documentation != null) {
1858 op.DocComment = tmpComment;
1859 Lexer.doc_state = XmlCommentState.Allowed;
1862 SimpleAnonymousHost.Simple.Propagate (op);
1863 anonymous_host = null;
1865 // Note again, checking is done in semantic analysis
1866 current_container.AddOperator (op);
1868 current_local_parameters = null;
1874 | SEMICOLON { $$ = null; }
1877 : type OPERATOR overloadable_operator
1878 OPEN_PARENS opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
1880 // TODO: wrong location
1881 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
1882 Error_ParameterModifierNotValid ((Location) $2);
1884 LocatedToken lt = (LocatedToken) $7;
1885 Operator.OpType op = (Operator.OpType) $3;
1886 CheckUnaryOperator (op, lt.Location);
1888 if (op == Operator.OpType.Addition)
1889 op = Operator.OpType.UnaryPlus;
1891 if (op == Operator.OpType.Subtraction)
1892 op = Operator.OpType.UnaryNegation;
1894 Parameter [] pars = new Parameter [1];
1895 Expression type = (Expression) $6;
1897 pars [0] = new Parameter (type, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1899 current_local_parameters = new Parameters (pars);
1901 if (RootContext.Documentation != null) {
1902 tmpComment = Lexer.consume_doc_comment ();
1903 Lexer.doc_state = XmlCommentState.NotAllowed;
1906 $$ = new OperatorDeclaration (op, (Expression) $1, type, lt.Value,
1907 null, null, (Location) $2);
1909 | type OPERATOR overloadable_operator
1911 opt_parameter_modifier type IDENTIFIER COMMA
1912 opt_parameter_modifier type IDENTIFIER
1915 // TODO: wrong location
1916 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE || (Parameter.Modifier)$9 != Parameter.Modifier.NONE)
1917 Error_ParameterModifierNotValid ((Location) $2);
1919 LocatedToken ltParam1 = (LocatedToken) $7;
1920 LocatedToken ltParam2 = (LocatedToken) $11;
1921 CheckBinaryOperator ((Operator.OpType) $3, (Location) $2);
1923 Parameter [] pars = new Parameter [2];
1925 Expression typeL = (Expression) $6;
1926 Expression typeR = (Expression) $10;
1928 pars [0] = new Parameter (typeL, ltParam1.Value, Parameter.Modifier.NONE, null, ltParam1.Location);
1929 pars [1] = new Parameter (typeR, ltParam2.Value, Parameter.Modifier.NONE, null, ltParam2.Location);
1931 current_local_parameters = new Parameters (pars);
1933 if (RootContext.Documentation != null) {
1934 tmpComment = Lexer.consume_doc_comment ();
1935 Lexer.doc_state = XmlCommentState.NotAllowed;
1938 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1939 typeL, ltParam1.Value,
1940 typeR, ltParam2.Value, (Location) $2);
1942 | conversion_operator_declarator
1943 | type OPERATOR overloadable_operator
1945 opt_parameter_modifier type IDENTIFIER COMMA
1946 opt_parameter_modifier type IDENTIFIER COMMA
1947 opt_parameter_modifier type IDENTIFIER
1950 Report.Error (1534, (Location) $2, "Overloaded binary operator `{0}' takes two parameters",
1951 Operator.GetName ((Operator.OpType) $3));
1954 | type OPERATOR overloadable_operator
1955 OPEN_PARENS CLOSE_PARENS
1957 Report.Error (1535, (Location) $2, "Overloaded unary operator `{0}' takes one parameter",
1958 Operator.GetName ((Operator.OpType) $3));
1963 overloadable_operator
1965 : BANG { $$ = Operator.OpType.LogicalNot; }
1966 | TILDE { $$ = Operator.OpType.OnesComplement; }
1967 | OP_INC { $$ = Operator.OpType.Increment; }
1968 | OP_DEC { $$ = Operator.OpType.Decrement; }
1969 | TRUE { $$ = Operator.OpType.True; }
1970 | FALSE { $$ = Operator.OpType.False; }
1971 // Unary and binary:
1972 | PLUS { $$ = Operator.OpType.Addition; }
1973 | MINUS { $$ = Operator.OpType.Subtraction; }
1975 | STAR { $$ = Operator.OpType.Multiply; }
1976 | DIV { $$ = Operator.OpType.Division; }
1977 | PERCENT { $$ = Operator.OpType.Modulus; }
1978 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1979 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1980 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1981 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1982 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1983 | OP_EQ { $$ = Operator.OpType.Equality; }
1984 | OP_NE { $$ = Operator.OpType.Inequality; }
1985 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1986 | OP_LT { $$ = Operator.OpType.LessThan; }
1987 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1988 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1991 conversion_operator_declarator
1992 : IMPLICIT OPERATOR type OPEN_PARENS opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
1994 // TODO: wrong location
1995 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
1996 Error_ParameterModifierNotValid (GetLocation ($4));
1998 LocatedToken lt = (LocatedToken) $7;
1999 Parameter [] pars = new Parameter [1];
2001 pars [0] = new Parameter ((Expression) $6, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2003 current_local_parameters = new Parameters (pars);
2005 if (RootContext.Documentation != null) {
2006 tmpComment = Lexer.consume_doc_comment ();
2007 Lexer.doc_state = XmlCommentState.NotAllowed;
2010 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $6, lt.Value,
2011 null, null, (Location) $2);
2013 | EXPLICIT OPERATOR type OPEN_PARENS opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
2015 // TODO: wrong location
2016 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
2017 Error_ParameterModifierNotValid (GetLocation ($4));
2019 LocatedToken lt = (LocatedToken) $7;
2020 Parameter [] pars = new Parameter [1];
2022 pars [0] = new Parameter ((Expression) $6, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2024 current_local_parameters = new Parameters (pars);
2026 if (RootContext.Documentation != null) {
2027 tmpComment = Lexer.consume_doc_comment ();
2028 Lexer.doc_state = XmlCommentState.NotAllowed;
2031 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $6, lt.Value,
2032 null, null, (Location) $2);
2036 syntax_error ((Location) $1, "'operator' expected");
2040 syntax_error ((Location) $1, "'operator' expected");
2044 constructor_declaration
2047 constructor_declarator
2050 Constructor c = (Constructor) $3;
2051 c.Block = (ToplevelBlock) $4;
2052 c.OptAttributes = (Attributes) $1;
2053 c.ModFlags = (int) $2;
2055 if (RootContext.Documentation != null)
2056 c.DocComment = ConsumeStoredComment ();
2058 if (c.Name == current_container.Basename){
2059 if ((c.ModFlags & Modifiers.STATIC) != 0){
2060 if ((c.ModFlags & Modifiers.Accessibility) != 0){
2061 Report.Error (515, c.Location,
2062 "`{0}': access modifiers are not allowed on static constructors",
2063 c.GetSignatureForError ());
2066 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2068 if (c.Initializer != null){
2069 Report.Error (514, c.Location,
2070 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2071 c.GetSignatureForError ());
2074 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2077 // We let another layer check the validity of the constructor.
2078 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
2081 current_container.AddConstructor (c);
2083 current_local_parameters = null;
2084 if (RootContext.Documentation != null)
2085 Lexer.doc_state = XmlCommentState.Allowed;
2089 constructor_declarator
2092 if (RootContext.Documentation != null) {
2093 tmpComment = Lexer.consume_doc_comment ();
2094 Lexer.doc_state = XmlCommentState.Allowed;
2097 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2099 current_local_parameters = (Parameters) $4;
2101 opt_constructor_initializer
2103 LocatedToken lt = (LocatedToken) $1;
2104 $$ = new Constructor (current_class, lt.Value, 0, (Parameters) $4,
2105 (ConstructorInitializer) $7, lt.Location);
2107 anonymous_host = (IAnonymousHost) $$;
2113 | SEMICOLON { $$ = null; }
2116 opt_constructor_initializer
2117 : /* empty */ { $$ = null; }
2118 | constructor_initializer
2121 constructor_initializer
2122 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
2124 $$ = new ConstructorBaseInitializer ((ArrayList) $4, (Location) $2);
2126 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
2128 $$ = new ConstructorThisInitializer ((ArrayList) $4, (Location) $2);
2131 Report.Error (1018, (Location) $1, "Keyword this or base expected");
2137 : /* EMPTY */ { $$ = 0; }
2138 | UNSAFE { $$ = Modifiers.UNSAFE; }
2139 | EXTERN { $$ = Modifiers.EXTERN; }
2142 destructor_declaration
2143 : opt_attributes opt_finalizer TILDE
2145 if (RootContext.Documentation != null) {
2146 tmpComment = Lexer.consume_doc_comment ();
2147 Lexer.doc_state = XmlCommentState.NotAllowed;
2150 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2152 LocatedToken lt = (LocatedToken) $5;
2153 if (lt.Value != current_container.Basename){
2154 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2155 } else if (current_container.Kind != Kind.Class){
2156 Report.Error (575, lt.Location, "Only class types can contain destructor");
2158 Location l = lt.Location;
2161 if (!RootContext.StdLib && current_container.Name == "System.Object")
2162 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2164 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2166 Method d = new Destructor (
2167 current_class, TypeManager.system_void_expr, m, "Finalize",
2168 Parameters.EmptyReadOnlyParameters, (Attributes) $1, l);
2169 if (RootContext.Documentation != null)
2170 d.DocComment = ConsumeStoredComment ();
2172 d.Block = (ToplevelBlock) $8;
2173 current_container.AddMethod (d);
2181 EVENT type variable_declarators SEMICOLON
2183 current_array_type = null;
2184 foreach (VariableDeclaration var in (ArrayList) $5) {
2186 MemberName name = new MemberName (var.identifier,
2189 EventField e = new EventField (
2190 current_class, (Expression) $4, (int) $2, false, name,
2193 e.Initializer = var.expression_or_array_initializer;
2195 current_container.AddEvent (e);
2197 if (RootContext.Documentation != null) {
2198 e.DocComment = Lexer.consume_doc_comment ();
2199 Lexer.doc_state = XmlCommentState.Allowed;
2205 EVENT type namespace_or_type_name
2208 implicit_value_parameter_type = (Expression) $4;
2209 lexer.EventParsing = true;
2211 event_accessor_declarations
2213 lexer.EventParsing = false;
2217 MemberName name = (MemberName) $5;
2220 Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2221 current_container.Name, name.ToString ());
2224 Accessors accessors = (Accessors) $8;
2225 if (accessors.get_or_add == null || accessors.set_or_remove == null)
2226 // CS0073 is already reported, so no CS0065 here.
2229 Event e = new EventProperty (
2230 current_class, (Expression) $4, (int) $2, false, name,
2231 (Attributes) $1, accessors.get_or_add, accessors.set_or_remove);
2232 if (RootContext.Documentation != null) {
2233 e.DocComment = Lexer.consume_doc_comment ();
2234 Lexer.doc_state = XmlCommentState.Allowed;
2237 current_container.AddEvent (e);
2238 implicit_value_parameter_type = null;
2242 | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
2243 MemberName mn = (MemberName) $5;
2245 if (mn.Left != null)
2246 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2248 Report.Error (71, mn.Location, "Event declaration should use property syntax");
2250 if (RootContext.Documentation != null)
2251 Lexer.doc_state = XmlCommentState.Allowed;
2255 event_accessor_declarations
2256 : add_accessor_declaration remove_accessor_declaration
2258 $$ = new Accessors ((Accessor) $1, (Accessor) $2);
2260 | remove_accessor_declaration add_accessor_declaration
2262 Accessors accessors = new Accessors ((Accessor) $2, (Accessor) $1);
2263 accessors.declared_in_reverse = true;
2266 | add_accessor_declaration { $$ = null; }
2267 | remove_accessor_declaration { $$ = null; }
2270 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2276 add_accessor_declaration
2277 : opt_attributes ADD
2279 Parameter [] args = new Parameter [1];
2280 Parameter implicit_value_parameter = new Parameter (
2281 implicit_value_parameter_type, "value",
2282 Parameter.Modifier.NONE, null, (Location) $2);
2284 args [0] = implicit_value_parameter;
2286 current_local_parameters = new Parameters (args);
2287 lexer.EventParsing = false;
2291 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2292 lexer.EventParsing = true;
2294 | opt_attributes ADD error {
2295 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2298 | opt_attributes modifiers ADD {
2299 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2304 remove_accessor_declaration
2305 : opt_attributes REMOVE
2307 Parameter [] args = new Parameter [1];
2308 Parameter implicit_value_parameter = new Parameter (
2309 implicit_value_parameter_type, "value",
2310 Parameter.Modifier.NONE, null, (Location) $2);
2312 args [0] = implicit_value_parameter;
2314 current_local_parameters = new Parameters (args);
2315 lexer.EventParsing = false;
2319 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2320 lexer.EventParsing = true;
2322 | opt_attributes REMOVE error {
2323 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2326 | opt_attributes modifiers REMOVE {
2327 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2333 : opt_attributes opt_modifiers indexer_declarator
2336 IndexerDeclaration decl = (IndexerDeclaration) $3;
2338 implicit_value_parameter_type = decl.type;
2340 lexer.PropertyParsing = true;
2341 parsing_indexer = true;
2343 indexer_parameters = decl.param_list;
2344 anonymous_host = SimpleAnonymousHost.GetSimple ();
2346 accessor_declarations
2348 lexer.PropertyParsing = false;
2349 has_get = has_set = false;
2350 parsing_indexer = false;
2357 // The signature is computed from the signature of the indexer. Look
2358 // at section 3.6 on the spec
2360 IndexerDeclaration decl = (IndexerDeclaration) $3;
2361 Location loc = decl.location;
2362 Accessors accessors = (Accessors) $6;
2363 Accessor get_block = accessors.get_or_add;
2364 Accessor set_block = accessors.set_or_remove;
2367 if (decl.interface_type != null)
2368 name = new MemberName (decl.interface_type, TypeContainer.DefaultIndexerName, loc);
2370 name = new MemberName (TypeContainer.DefaultIndexerName, loc);
2372 indexer = new Indexer (current_class, decl.type, name,
2373 (int) $2, false, decl.param_list, (Attributes) $1,
2374 get_block, set_block, accessors.declared_in_reverse);
2376 if (RootContext.Documentation != null)
2377 indexer.DocComment = ConsumeStoredComment ();
2379 current_container.AddIndexer (indexer);
2381 current_local_parameters = null;
2382 implicit_value_parameter_type = null;
2383 indexer_parameters = null;
2388 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2390 Parameters pars = (Parameters) $4;
2391 if (pars.HasArglist) {
2392 // "__arglist is not valid in this context"
2393 Report.Error (1669, (Location) $2, "__arglist is not valid in this context");
2394 } else if (pars.Empty){
2395 Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
2397 if (RootContext.Documentation != null) {
2398 tmpComment = Lexer.consume_doc_comment ();
2399 Lexer.doc_state = XmlCommentState.Allowed;
2402 $$ = new IndexerDeclaration ((Expression) $1, null, pars, (Location) $2);
2404 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2406 Parameters pars = (Parameters) $6;
2408 if (pars.HasArglist) {
2409 // "__arglist is not valid in this context"
2410 Report.Error (1669, (Location) $4, "__arglist is not valid in this context");
2411 } else if (pars.Empty){
2412 Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
2415 MemberName name = (MemberName) $2;
2416 $$ = new IndexerDeclaration ((Expression) $1, name, pars, (Location) $4);
2418 if (RootContext.Documentation != null) {
2419 tmpComment = Lexer.consume_doc_comment ();
2420 Lexer.doc_state = XmlCommentState.Allowed;
2430 if (RootContext.Documentation != null)
2431 enumTypeComment = Lexer.consume_doc_comment ();
2436 LocatedToken lt = (LocatedToken) $4;
2437 Location enum_location = lt.Location;
2439 MemberName name = MakeName (new MemberName (lt.Value, enum_location));
2440 Enum e = new Enum (current_namespace, current_class, (Expression) $5, (int) $2,
2441 name, (Attributes) $1);
2443 if (RootContext.Documentation != null)
2444 e.DocComment = enumTypeComment;
2447 EnumMember em = null;
2448 foreach (VariableDeclaration ev in (ArrayList) $7) {
2449 em = new EnumMember (e, em, (Expression) ev.expression_or_array_initializer,
2450 new MemberName (ev.identifier, ev.Location), ev.OptAttributes);
2452 // if (RootContext.Documentation != null)
2453 em.DocComment = ev.DocComment;
2455 e.AddEnumMember (em);
2458 current_container.AddEnum (e);
2465 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2466 | COLON type { $$ = $2; }
2472 if (RootContext.Documentation != null)
2473 Lexer.doc_state = XmlCommentState.Allowed;
2475 opt_enum_member_declarations
2477 // here will be evaluated after CLOSE_BLACE is consumed.
2478 if (RootContext.Documentation != null)
2479 Lexer.doc_state = XmlCommentState.Allowed;
2487 opt_enum_member_declarations
2488 : /* empty */ { $$ = new ArrayList (4); }
2489 | enum_member_declarations opt_comma { $$ = $1; }
2492 enum_member_declarations
2493 : enum_member_declaration
2495 ArrayList l = new ArrayList (4);
2500 | enum_member_declarations COMMA enum_member_declaration
2502 ArrayList l = (ArrayList) $1;
2510 enum_member_declaration
2511 : opt_attributes IDENTIFIER
2513 VariableDeclaration vd = new VariableDeclaration (
2514 (LocatedToken) $2, null, (Attributes) $1);
2516 if (RootContext.Documentation != null) {
2517 vd.DocComment = Lexer.consume_doc_comment ();
2518 Lexer.doc_state = XmlCommentState.Allowed;
2523 | opt_attributes IDENTIFIER
2525 if (RootContext.Documentation != null) {
2526 tmpComment = Lexer.consume_doc_comment ();
2527 Lexer.doc_state = XmlCommentState.NotAllowed;
2532 VariableDeclaration vd = new VariableDeclaration (
2533 (LocatedToken) $2, $5, (Attributes) $1);
2535 if (RootContext.Documentation != null)
2536 vd.DocComment = ConsumeStoredComment ();
2542 delegate_declaration
2545 DELEGATE type member_name
2546 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2549 MemberName name = MakeName ((MemberName) $5);
2550 Parameters p = (Parameters) $7;
2552 // TODO: wrong location
2553 Report.Error (1669, name.Location, "__arglist is not valid in this context");
2556 Delegate del = new Delegate (current_namespace, current_class, (Expression) $4,
2557 (int) $2, name, p, (Attributes) $1);
2559 if (RootContext.Documentation != null) {
2560 del.DocComment = Lexer.consume_doc_comment ();
2561 Lexer.doc_state = XmlCommentState.Allowed;
2564 current_container.AddDelegate (del);
2569 namespace_or_type_name
2571 | IDENTIFIER DOUBLE_COLON IDENTIFIER {
2572 LocatedToken lt1 = (LocatedToken) $1;
2573 LocatedToken lt2 = (LocatedToken) $3;
2574 $$ = new MemberName (lt1.Value, lt2.Value, lt2.Location);
2576 | namespace_or_type_name DOT IDENTIFIER {
2577 LocatedToken lt = (LocatedToken) $3;
2578 $$ = new MemberName ((MemberName) $1, lt.Value);
2584 LocatedToken lt = (LocatedToken) $1;
2585 $$ = new MemberName (lt.Value, lt.Location);
2590 * Before you think of adding a return_type, notice that we have been
2591 * using two rules in the places where it matters (one rule using type
2592 * and another identical one that uses VOID as the return type). This
2593 * gets rid of a shift/reduce couple
2596 : namespace_or_type_name
2598 MemberName name = (MemberName) $1;
2599 $$ = name.GetTypeExpression ();
2611 // Note that here only unmanaged types are allowed but we
2612 // can't perform checks during this phase - we do it during
2613 // semantic analysis.
2615 $$ = new ComposedCast ((Expression) $1, "*", Lexer.Location);
2619 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2625 | non_expression_type rank_specifier
2627 Location loc = GetLocation ($1);
2629 loc = lexer.Location;
2630 $$ = new ComposedCast ((Expression) $1, (string) $2, loc);
2632 | non_expression_type STAR
2634 Location loc = GetLocation ($1);
2636 loc = lexer.Location;
2637 $$ = new ComposedCast ((Expression) $1, "*", loc);
2639 | expression rank_specifiers
2641 $$ = new ComposedCast ((Expression) $1, (string) $2);
2645 $$ = new ComposedCast ((Expression) $1, "*");
2649 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2650 // through this different path
2652 | multiplicative_expression STAR
2654 $$ = new ComposedCast ((Expression) $1, "*");
2661 ArrayList types = new ArrayList (4);
2666 | type_list COMMA type
2668 ArrayList types = (ArrayList) $1;
2676 * replaces all the productions for isolating the various
2677 * simple types, but we need this to reuse it easily in local_variable_type
2680 : OBJECT { $$ = TypeManager.system_object_expr; }
2681 | STRING { $$ = TypeManager.system_string_expr; }
2682 | BOOL { $$ = TypeManager.system_boolean_expr; }
2683 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2684 | FLOAT { $$ = TypeManager.system_single_expr; }
2685 | DOUBLE { $$ = TypeManager.system_double_expr; }
2690 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2691 | BYTE { $$ = TypeManager.system_byte_expr; }
2692 | SHORT { $$ = TypeManager.system_int16_expr; }
2693 | USHORT { $$ = TypeManager.system_uint16_expr; }
2694 | INT { $$ = TypeManager.system_int32_expr; }
2695 | UINT { $$ = TypeManager.system_uint32_expr; }
2696 | LONG { $$ = TypeManager.system_int64_expr; }
2697 | ULONG { $$ = TypeManager.system_uint64_expr; }
2698 | CHAR { $$ = TypeManager.system_char_expr; }
2699 | VOID { $$ = TypeManager.system_void_expr; }
2703 : type rank_specifiers
2705 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2);
2710 // Expressions, section 7.5
2720 MemberName mn = (MemberName) $1;
2721 $$ = mn.GetTypeExpression ();
2723 | IDENTIFIER DOUBLE_COLON IDENTIFIER
2725 LocatedToken lt1 = (LocatedToken) $1;
2726 LocatedToken lt2 = (LocatedToken) $3;
2727 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, lt2.Location);
2729 | parenthesized_expression
2731 | invocation_expression
2735 | post_increment_expression
2736 | post_decrement_expression
2740 | checked_expression
2741 | unchecked_expression
2742 | pointer_member_access
2743 | anonymous_method_expression
2750 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
2751 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); }
2752 | NULL { $$ = new NullLiteral (lexer.Location); }
2756 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
2757 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
2758 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
2763 object v = lexer.Value;
2766 $$ = new IntLiteral ((int) v, lexer.Location);
2767 } else if (v is uint)
2768 $$ = new UIntLiteral ((UInt32) v, lexer.Location);
2770 $$ = new LongLiteral ((Int64) v, lexer.Location);
2771 else if (v is ulong)
2772 $$ = new ULongLiteral ((UInt64) v, lexer.Location);
2774 Console.WriteLine ("OOPS. Unexpected result from scanner");
2779 : TRUE { $$ = new BoolLiteral (true, lexer.Location); }
2780 | FALSE { $$ = new BoolLiteral (false, lexer.Location); }
2783 parenthesized_expression_0
2784 : OPEN_PARENS expression CLOSE_PARENS
2787 lexer.Deambiguate_CloseParens ($$);
2788 // After this, the next token returned is one of
2789 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST (CLOSE_PARENS), CLOSE_PARENS_OPEN_PARENS
2790 // or CLOSE_PARENS_MINUS.
2792 | OPEN_PARENS expression error { CheckToken (1026, yyToken, "Expecting ')'", lexer.Location); }
2795 parenthesized_expression
2796 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2800 | parenthesized_expression_0 CLOSE_PARENS
2804 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2806 // If a parenthesized expression is followed by a minus, we need to wrap
2807 // the expression inside a ParenthesizedExpression for the CS0075 check
2808 // in Binary.DoResolve().
2809 $$ = new ParenthesizedExpression ((Expression) $1);
2814 : primary_expression DOT IDENTIFIER
2816 LocatedToken lt = (LocatedToken) $3;
2817 $$ = new MemberAccess ((Expression) $1, lt.Value);
2819 | predefined_type DOT IDENTIFIER
2821 LocatedToken lt = (LocatedToken) $3;
2822 // TODO: Location is wrong as some predefined types doesn't hold a location
2823 $$ = new MemberAccess ((Expression) $1, lt.Value, lt.Location);
2831 invocation_expression
2832 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2835 Report.Error (1, (Location) $2, "Parse error");
2837 $$ = new Invocation ((Expression) $1, (ArrayList) $3);
2839 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2841 $$ = new Invocation ((Expression) $1, new ArrayList ());
2843 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2845 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
2847 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2849 ArrayList args = new ArrayList (1);
2851 $$ = new Invocation ((Expression) $1, args);
2853 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2855 ArrayList args = ((ArrayList) $4);
2857 $$ = new Invocation ((Expression) $1, args);
2862 : /* empty */ { $$ = null; }
2869 ArrayList list = new ArrayList (4);
2873 | argument_list COMMA argument
2875 ArrayList list = (ArrayList) $1;
2879 | argument_list error {
2880 CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
2888 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2890 | non_simple_argument
2897 : REF variable_reference
2899 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2901 | OUT variable_reference
2903 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2905 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2907 ArrayList list = (ArrayList) $3;
2908 Argument[] args = new Argument [list.Count];
2909 list.CopyTo (args, 0);
2911 Expression expr = new Arglist (args, (Location) $1);
2912 $$ = new Argument (expr, Argument.AType.Expression);
2916 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
2921 : expression { note ("section 5.4"); $$ = $1; }
2925 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2927 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
2929 | primary_expression rank_specifiers
2931 // So the super-trick is that primary_expression
2932 // can only be either a SimpleName or a MemberAccess.
2933 // The MemberAccess case arises when you have a fully qualified type-name like :
2935 // SimpleName is when you have
2938 Expression expr = (Expression) $1;
2939 if (expr is ComposedCast){
2940 $$ = new ComposedCast (expr, (string) $2);
2941 } else if (!(expr is SimpleName || expr is MemberAccess || expr is QualifiedAliasMember)){
2942 Error_ExpectingTypeName (expr);
2943 $$ = TypeManager.system_object_expr;
2946 // So we extract the string corresponding to the SimpleName
2949 $$ = new ComposedCast (expr, (string) $2);
2951 current_array_type = (Expression)$$;
2958 ArrayList list = new ArrayList (4);
2962 | expression_list COMMA expression
2964 ArrayList list = (ArrayList) $1;
2973 $$ = new This (current_block, (Location) $1);
2978 : BASE DOT IDENTIFIER
2980 LocatedToken lt = (LocatedToken) $3;
2981 $$ = new BaseAccess (lt.Value, lt.Location);
2983 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2985 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
2988 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
2993 post_increment_expression
2994 : primary_expression OP_INC
2996 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2997 (Expression) $1, (Location) $2);
3001 post_decrement_expression
3002 : primary_expression OP_DEC
3004 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
3005 (Expression) $1, (Location) $2);
3010 : object_or_delegate_creation_expression
3011 | array_creation_expression
3014 object_or_delegate_creation_expression
3015 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
3017 $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
3021 array_creation_expression
3022 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
3024 opt_array_initializer
3026 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
3028 | NEW type rank_specifiers array_initializer
3030 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, (Location) $1);
3034 Report.Error (1031, (Location) $1, "Type expected");
3039 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
3056 : rank_specifier opt_rank_specifier
3058 $$ = (string) $2 + (string) $1;
3063 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3065 $$ = "[" + (string) $2 + "]";
3085 | dim_separators COMMA
3087 $$ = (string) $1 + ",";
3091 opt_array_initializer
3103 : OPEN_BRACE CLOSE_BRACE
3105 ArrayList list = new ArrayList (4);
3108 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3110 $$ = (ArrayList) $2;
3114 variable_initializer_list
3115 : variable_initializer
3117 ArrayList list = new ArrayList (4);
3121 | variable_initializer_list COMMA variable_initializer
3123 ArrayList list = (ArrayList) $1;
3132 pushed_current_array_type = current_array_type;
3134 OPEN_PARENS type CLOSE_PARENS
3136 Expression type = (Expression)$4;
3137 if (type == TypeManager.system_void_expr)
3138 $$ = new TypeOfVoid ((Location) $1);
3140 $$ = new TypeOf (type, (Location) $1);
3141 current_array_type = pushed_current_array_type;
3146 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3147 $$ = new SizeOf ((Expression) $3, (Location) $1);
3152 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3154 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3158 unchecked_expression
3159 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3161 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3165 pointer_member_access
3166 : primary_expression OP_PTR IDENTIFIER
3169 LocatedToken lt = (LocatedToken) $3;
3171 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
3172 $$ = new MemberAccess (deref, lt.Value);
3176 anonymous_method_expression
3177 : DELEGATE opt_anonymous_method_signature
3179 if (oob_stack == null)
3180 oob_stack = new Stack (6);
3182 oob_stack.Push (current_anonymous_method);
3183 oob_stack.Push (current_local_parameters);
3184 current_local_parameters = (Parameters)$2;
3186 // Force the next block to be created as a ToplevelBlock
3187 oob_stack.Push (current_block);
3188 oob_stack.Push (top_current_block);
3190 Location loc = (Location) $1;
3191 current_anonymous_method = new AnonymousMethodExpression (
3192 current_anonymous_method, null, current_container,
3193 (Parameters) $2, (ToplevelBlock) top_current_block, loc);
3195 parsing_anonymous_method = true;
3199 Location loc = (Location) $1;
3200 top_current_block = (Block) oob_stack.Pop ();
3201 current_block = (Block) oob_stack.Pop ();
3203 if (RootContext.Version == LanguageVersion.ISO_1){
3204 Report.FeatureIsNotISO1 (loc, "anonymous methods");
3207 ToplevelBlock anon_block = (ToplevelBlock) $4;
3209 anon_block.Parent = current_block;
3211 current_anonymous_method.Block = anon_block;
3212 if ((anonymous_host != null) && (current_anonymous_method.Parent == null))
3213 anonymous_host.AddAnonymousMethod (current_anonymous_method);
3215 $$ = current_anonymous_method;
3218 current_local_parameters = (Parameters) oob_stack.Pop ();
3219 current_anonymous_method = (AnonymousMethodExpression) oob_stack.Pop ();
3223 opt_anonymous_method_signature
3224 : /* empty */ { $$ = null; }
3225 | anonymous_method_signature
3228 anonymous_method_signature
3229 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3232 $$ = Parameters.EmptyReadOnlyParameters;
3234 ArrayList par_list = (ArrayList) $2;
3235 Parameter [] pars = new Parameter [par_list.Count];
3236 par_list.CopyTo (pars);
3237 $$ = new Parameters (pars);
3242 opt_anonymous_method_parameter_list
3243 : /* empty */ { $$ = null; }
3244 | anonymous_method_parameter_list { $$ = $1; }
3247 anonymous_method_parameter_list
3248 : anonymous_method_parameter
3250 ArrayList a = new ArrayList (4);
3254 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3256 ArrayList a = (ArrayList) $1;
3262 anonymous_method_parameter
3263 : opt_parameter_modifier type IDENTIFIER {
3264 LocatedToken lt = (LocatedToken) $3;
3265 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3267 | PARAMS type IDENTIFIER {
3268 Report.Error (1670, ((LocatedToken) $3).Location, "The `params' modifier is not allowed in anonymous method declaration");
3274 : primary_expression
3275 | BANG prefixed_unary_expression
3277 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3279 | TILDE prefixed_unary_expression
3281 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3287 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3289 $$ = new Cast ((Expression) $1, (Expression) $3);
3291 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3293 $$ = new Cast ((Expression) $1, (Expression) $3);
3299 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3301 // TODO: wrong location
3302 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3307 // The idea to split this out is from Rhys' grammar
3308 // to solve the problem with casts.
3310 prefixed_unary_expression
3312 | PLUS prefixed_unary_expression
3314 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3316 | MINUS prefixed_unary_expression
3318 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3320 | OP_INC prefixed_unary_expression
3322 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3323 (Expression) $2, (Location) $1);
3325 | OP_DEC prefixed_unary_expression
3327 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3328 (Expression) $2, (Location) $1);
3330 | STAR prefixed_unary_expression
3332 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, (Location) $1);
3334 | BITWISE_AND prefixed_unary_expression
3336 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3340 pre_increment_expression
3341 : OP_INC prefixed_unary_expression
3343 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3344 (Expression) $2, (Location) $1);
3348 pre_decrement_expression
3349 : OP_DEC prefixed_unary_expression
3351 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3352 (Expression) $2, (Location) $1);
3356 multiplicative_expression
3357 : prefixed_unary_expression
3358 | multiplicative_expression STAR prefixed_unary_expression
3360 $$ = new Binary (Binary.Operator.Multiply,
3361 (Expression) $1, (Expression) $3);
3363 | multiplicative_expression DIV prefixed_unary_expression
3365 $$ = new Binary (Binary.Operator.Division,
3366 (Expression) $1, (Expression) $3);
3368 | multiplicative_expression PERCENT prefixed_unary_expression
3370 $$ = new Binary (Binary.Operator.Modulus,
3371 (Expression) $1, (Expression) $3);
3376 : multiplicative_expression
3377 | additive_expression PLUS multiplicative_expression
3379 $$ = new Binary (Binary.Operator.Addition,
3380 (Expression) $1, (Expression) $3);
3382 | additive_expression MINUS multiplicative_expression
3384 $$ = new Binary (Binary.Operator.Subtraction,
3385 (Expression) $1, (Expression) $3);
3390 : additive_expression
3391 | shift_expression OP_SHIFT_LEFT additive_expression
3393 $$ = new Binary (Binary.Operator.LeftShift,
3394 (Expression) $1, (Expression) $3);
3396 | shift_expression OP_SHIFT_RIGHT additive_expression
3398 $$ = new Binary (Binary.Operator.RightShift,
3399 (Expression) $1, (Expression) $3);
3403 relational_expression
3405 | relational_expression OP_LT shift_expression
3407 $$ = new Binary (Binary.Operator.LessThan,
3408 (Expression) $1, (Expression) $3);
3410 | relational_expression OP_GT shift_expression
3412 $$ = new Binary (Binary.Operator.GreaterThan,
3413 (Expression) $1, (Expression) $3);
3415 | relational_expression OP_LE shift_expression
3417 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3418 (Expression) $1, (Expression) $3);
3420 | relational_expression OP_GE shift_expression
3422 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3423 (Expression) $1, (Expression) $3);
3425 | relational_expression IS type
3427 $$ = new Is ((Expression) $1, (Expression) $3, (Location) $2);
3429 | relational_expression AS type
3431 $$ = new As ((Expression) $1, (Expression) $3, (Location) $2);
3436 : relational_expression
3437 | equality_expression OP_EQ relational_expression
3439 $$ = new Binary (Binary.Operator.Equality,
3440 (Expression) $1, (Expression) $3);
3442 | equality_expression OP_NE relational_expression
3444 $$ = new Binary (Binary.Operator.Inequality,
3445 (Expression) $1, (Expression) $3);
3450 : equality_expression
3451 | and_expression BITWISE_AND equality_expression
3453 $$ = new Binary (Binary.Operator.BitwiseAnd,
3454 (Expression) $1, (Expression) $3);
3458 exclusive_or_expression
3460 | exclusive_or_expression CARRET and_expression
3462 $$ = new Binary (Binary.Operator.ExclusiveOr,
3463 (Expression) $1, (Expression) $3);
3467 inclusive_or_expression
3468 : exclusive_or_expression
3469 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3471 $$ = new Binary (Binary.Operator.BitwiseOr,
3472 (Expression) $1, (Expression) $3);
3476 conditional_and_expression
3477 : inclusive_or_expression
3478 | conditional_and_expression OP_AND inclusive_or_expression
3480 $$ = new Binary (Binary.Operator.LogicalAnd,
3481 (Expression) $1, (Expression) $3);
3485 conditional_or_expression
3486 : conditional_and_expression
3487 | conditional_or_expression OP_OR conditional_and_expression
3489 $$ = new Binary (Binary.Operator.LogicalOr,
3490 (Expression) $1, (Expression) $3);
3494 conditional_expression
3495 : conditional_or_expression
3496 | conditional_or_expression INTERR expression COLON expression
3498 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
3502 assignment_expression
3503 : prefixed_unary_expression ASSIGN expression
3505 $$ = new Assign ((Expression) $1, (Expression) $3);
3507 | prefixed_unary_expression OP_MULT_ASSIGN expression
3509 $$ = new CompoundAssign (
3510 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
3512 | prefixed_unary_expression OP_DIV_ASSIGN expression
3514 $$ = new CompoundAssign (
3515 Binary.Operator.Division, (Expression) $1, (Expression) $3);
3517 | prefixed_unary_expression OP_MOD_ASSIGN expression
3519 $$ = new CompoundAssign (
3520 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
3522 | prefixed_unary_expression OP_ADD_ASSIGN expression
3524 $$ = new CompoundAssign (
3525 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
3527 | prefixed_unary_expression OP_SUB_ASSIGN expression
3529 $$ = new CompoundAssign (
3530 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3532 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3534 $$ = new CompoundAssign (
3535 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
3537 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3539 $$ = new CompoundAssign (
3540 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
3542 | prefixed_unary_expression OP_AND_ASSIGN expression
3544 $$ = new CompoundAssign (
3545 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
3547 | prefixed_unary_expression OP_OR_ASSIGN expression
3549 $$ = new CompoundAssign (
3550 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
3552 | prefixed_unary_expression OP_XOR_ASSIGN expression
3554 $$ = new CompoundAssign (
3555 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
3560 : conditional_expression
3561 | assignment_expression
3581 MemberName name = MakeName ((MemberName) $5);
3582 int mod_flags = (int) $2;
3584 push_current_class (new Class (
3585 current_namespace, current_class, name,
3586 mod_flags, (Attributes) $1), false, $3);
3591 if (current_class.Name == "System.Object") {
3592 Report.Error (537, current_class.Location,
3593 "The class System.Object cannot have a base " +
3594 "class or implement an interface.");
3596 current_container.AddBasesForPart (current_class, (ArrayList) $7);
3599 if (RootContext.Documentation != null) {
3600 current_container.DocComment = Lexer.consume_doc_comment ();
3601 Lexer.doc_state = XmlCommentState.Allowed;
3606 if (RootContext.Documentation != null)
3607 Lexer.doc_state = XmlCommentState.Allowed;
3611 $$ = pop_current_class ();
3619 { $$ = $1; } // location
3623 : /* empty */ { $$ = (int) 0; }
3629 | modifiers modifier
3634 if ((m1 & m2) != 0) {
3635 Location l = lexer.Location;
3636 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
3638 $$ = (int) (m1 | m2);
3643 : NEW { $$ = Modifiers.NEW; }
3644 | PUBLIC { $$ = Modifiers.PUBLIC; }
3645 | PROTECTED { $$ = Modifiers.PROTECTED; }
3646 | INTERNAL { $$ = Modifiers.INTERNAL; }
3647 | PRIVATE { $$ = Modifiers.PRIVATE; }
3648 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3649 | SEALED { $$ = Modifiers.SEALED; }
3650 | STATIC { $$ = Modifiers.STATIC; }
3651 | READONLY { $$ = Modifiers.READONLY; }
3652 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3653 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3654 | EXTERN { $$ = Modifiers.EXTERN; }
3655 | VOLATILE { $$ = Modifiers.VOLATILE; }
3656 | UNSAFE { $$ = Modifiers.UNSAFE; }
3660 : /* empty */ { $$ = null; }
3661 | class_base { $$ = $1; }
3665 : COLON type_list { $$ = $2; }
3673 // A block is "contained" on the following places:
3675 // property_declaration as part of the accessor body (get/set)
3676 // operator_declaration
3677 // constructor_declaration
3678 // destructor_declaration
3679 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3684 if (parsing_anonymous_method) {
3685 top_current_block = new ToplevelBlock (
3686 current_block, current_local_parameters, null, (Location) $1);
3687 if (current_block != null)
3688 current_block.AddAnonymousChild ((ToplevelBlock) top_current_block);
3689 current_block = top_current_block;
3690 parsing_anonymous_method = false;
3691 } else if (current_block == null){
3692 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, (Location) $1);
3693 top_current_block = current_block;
3695 current_block = new Block (current_block, (Location) $1, Location.Null);
3698 opt_statement_list CLOSE_BRACE
3700 while (current_block.Implicit)
3701 current_block = current_block.Parent;
3703 current_block.SetEndLocation ((Location) $4);
3704 current_block = current_block.Parent;
3705 if (current_block == null)
3706 top_current_block = null;
3717 | statement_list statement
3721 : declaration_statement
3723 if ($1 != null && (Block) $1 != current_block){
3724 current_block.AddStatement ((Statement) $1);
3725 current_block = (Block) $1;
3728 | valid_declaration_statement
3730 current_block.AddStatement ((Statement) $1);
3735 valid_declaration_statement
3738 | expression_statement
3739 | selection_statement
3740 | iteration_statement
3744 | unchecked_statement
3752 : valid_declaration_statement
3753 | declaration_statement
3755 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3760 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3768 $$ = EmptyStatement.Value;
3775 LocatedToken lt = (LocatedToken) $1;
3776 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
3778 if (current_block.AddLabel (labeled))
3779 current_block.AddStatement (labeled);
3784 declaration_statement
3785 : local_variable_declaration SEMICOLON
3787 current_array_type = null;
3789 DictionaryEntry de = (DictionaryEntry) $1;
3790 Expression e = (Expression) de.Key;
3792 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
3796 | local_constant_declaration SEMICOLON
3798 current_array_type = null;
3800 DictionaryEntry de = (DictionaryEntry) $1;
3802 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3808 * The following is from Rhys' grammar:
3809 * > Types in local variable declarations must be recognized as
3810 * > expressions to prevent reduce/reduce errors in the grammar.
3811 * > The expressions are converted into types during semantic analysis.
3814 : primary_expression opt_rank_specifier
3816 // FIXME: Do something smart here regarding the composition of the type.
3818 // Ok, the above "primary_expression" is there to get rid of
3819 // both reduce/reduce and shift/reduces in the grammar, it should
3820 // really just be "type_name". If you use type_name, a reduce/reduce
3821 // creeps up. If you use namespace_or_type_name (which is all we need
3822 // really) two shift/reduces appear.
3825 // So the super-trick is that primary_expression
3826 // can only be either a SimpleName or a MemberAccess.
3827 // The MemberAccess case arises when you have a fully qualified type-name like :
3829 // SimpleName is when you have
3832 Expression expr = (Expression) $1;
3833 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3834 Error_ExpectingTypeName (expr);
3838 // So we extract the string corresponding to the SimpleName
3842 if ((string) $2 == "")
3845 $$ = new ComposedCast ((Expression) $1, (string) $2);
3848 | builtin_types opt_rank_specifier
3850 if ((string) $2 == "")
3853 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3857 local_variable_pointer_type
3858 : primary_expression STAR
3860 Expression expr = (Expression) $1;
3862 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3863 Error_ExpectingTypeName (expr);
3867 $$ = new ComposedCast ((Expression) $1, "*");
3869 | builtin_types STAR
3871 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3875 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
3877 | local_variable_pointer_type STAR
3879 $$ = new ComposedCast ((Expression) $1, "*");
3883 local_variable_declaration
3884 : local_variable_type variable_declarators
3887 $$ = new DictionaryEntry ($1, $2);
3891 | local_variable_pointer_type opt_rank_specifier variable_declarators
3896 if ((string) $2 == "")
3897 t = (Expression) $1;
3899 t = new ComposedCast ((Expression) $1, (string) $2);
3900 $$ = new DictionaryEntry (t, $3);
3906 local_constant_declaration
3907 : CONST local_variable_type constant_declarators
3910 $$ = new DictionaryEntry ($2, $3);
3916 expression_statement
3917 : statement_expression SEMICOLON { $$ = $1; }
3921 // We have to do the wrapping here and not in the case above,
3922 // because statement_expression is used for example in for_statement
3924 statement_expression
3927 Expression expr = (Expression) $1;
3928 ExpressionStatement s = expr as ExpressionStatement;
3930 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
3933 $$ = new StatementExpression (s);
3937 Report.Error (1002, GetLocation ($1), "Expecting `;'");
3942 object_creation_expression
3943 : object_or_delegate_creation_expression
3944 { note ("complain if this is a delegate maybe?"); }
3953 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
3956 Location l = (Location) $1;
3958 $$ = new If ((Expression) $3, (Statement) $5, l);
3960 // FIXME: location for warning should be loc property of $5.
3961 if ($5 == EmptyStatement.Value)
3962 Report.Warning (642, 3, l, "Possible mistaken empty statement");
3965 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
3966 embedded_statement ELSE embedded_statement
3968 Location l = (Location) $1;
3970 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
3972 // FIXME: location for warning should be loc property of $5 and $7.
3973 if ($5 == EmptyStatement.Value)
3974 Report.Warning (642, 3, l, "Possible mistaken empty statement");
3975 if ($7 == EmptyStatement.Value)
3976 Report.Warning (642, 3, l, "Possible mistaken empty statement");
3981 : SWITCH OPEN_PARENS
3983 if (switch_stack == null)
3984 switch_stack = new Stack (2);
3985 switch_stack.Push (current_block);
3987 expression CLOSE_PARENS
3990 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
3991 current_block = (Block) switch_stack.Pop ();
4007 Report.Warning (1522, 1, lexer.Location, "Empty switch block");
4008 $$ = new ArrayList ();
4016 ArrayList sections = new ArrayList (4);
4021 | switch_sections switch_section
4023 ArrayList sections = (ArrayList) $1;
4033 current_block = current_block.CreateSwitchBlock (lexer.Location);
4037 Block topmost = current_block;
4039 while (topmost.Implicit)
4040 topmost = topmost.Parent;
4041 $$ = new SwitchSection ((ArrayList) $1, topmost);
4048 ArrayList labels = new ArrayList (4);
4053 | switch_labels switch_label
4055 ArrayList labels = (ArrayList) ($1);
4063 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
4064 | DEFAULT COLON { $$ = new SwitchLabel (null, (Location) $1); }
4067 1523, GetLocation ($1),
4068 "The keyword case or default must precede code in switch block");
4080 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
4082 Location l = (Location) $1;
4083 $$ = new While ((Expression) $3, (Statement) $5, l);
4088 : DO embedded_statement
4089 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
4091 Location l = (Location) $1;
4093 $$ = new Do ((Statement) $2, (Expression) $5, l);
4099 opt_for_initializer SEMICOLON
4101 Block assign_block = new Block (current_block);
4102 current_block = assign_block;
4104 if ($3 is DictionaryEntry){
4105 DictionaryEntry de = (DictionaryEntry) $3;
4107 Expression type = (Expression) de.Key;
4108 ArrayList var_declarators = (ArrayList) de.Value;
4110 foreach (VariableDeclaration decl in var_declarators){
4114 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4118 Location l = lexer.Location;
4119 Expression expr = decl.expression_or_array_initializer;
4121 LocalVariableReference var;
4122 var = new LocalVariableReference (assign_block, decl.identifier, l);
4125 Assign a = new Assign (var, expr, decl.Location);
4127 assign_block.AddStatement (new StatementExpression (a));
4131 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4132 // This can be referred to as $5 below.
4138 opt_for_condition SEMICOLON
4139 opt_for_iterator CLOSE_PARENS
4142 Location l = (Location) $1;
4144 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4146 current_block.AddStatement (f);
4147 while (current_block.Implicit)
4148 current_block = current_block.Parent;
4150 current_block = current_block.Parent;
4155 : /* empty */ { $$ = EmptyStatement.Value; }
4160 : local_variable_declaration
4161 | statement_expression_list
4165 : /* empty */ { $$ = null; }
4166 | boolean_expression
4170 : /* empty */ { $$ = EmptyStatement.Value; }
4175 : statement_expression_list
4178 statement_expression_list
4179 : statement_expression
4181 // CHANGE: was `null'
4182 Statement s = (Statement) $1;
4183 Block b = new Block (current_block, Block.Flags.Implicit, s.loc, lexer.Location);
4188 | statement_expression_list COMMA statement_expression
4190 Block b = (Block) $1;
4192 b.AddStatement ((Statement) $3);
4198 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4200 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4203 | FOREACH OPEN_PARENS type IDENTIFIER IN
4204 expression CLOSE_PARENS
4206 Block foreach_block = new Block (current_block);
4207 current_block = foreach_block;
4209 LocatedToken lt = (LocatedToken) $4;
4210 Location l = lt.Location;
4213 vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4215 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4217 // Get a writable reference to this read-only variable.
4219 // Note that the $$ here refers to the value of _this_ code block,
4220 // not the value of the LHS non-terminal. This can be referred to as $8 below.
4221 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
4228 LocalVariableReference v = (LocalVariableReference) $8;
4229 Location l = (Location) $1;
4232 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
4233 current_block.AddStatement (f);
4236 while (current_block.Implicit)
4237 current_block = current_block.Parent;
4239 current_block = current_block.Parent;
4245 | continue_statement
4255 $$ = new Break ((Location) $1);
4260 : CONTINUE SEMICOLON
4262 $$ = new Continue ((Location) $1);
4267 : GOTO IDENTIFIER SEMICOLON
4269 LocatedToken lt = (LocatedToken) $2;
4270 $$ = new Goto (lt.Value, lt.Location);
4272 | GOTO CASE constant_expression SEMICOLON
4274 $$ = new GotoCase ((Expression) $3, (Location) $1);
4276 | GOTO DEFAULT SEMICOLON
4278 $$ = new GotoDefault ((Location) $1);
4283 : RETURN opt_expression SEMICOLON
4285 $$ = new Return ((Expression) $2, (Location) $1);
4290 : THROW opt_expression SEMICOLON
4292 $$ = new Throw ((Expression) $2, (Location) $1);
4297 : IDENTIFIER RETURN expression SEMICOLON
4299 LocatedToken lt = (LocatedToken) $1;
4300 string s = lt.Value;
4302 Report.Error (1003, lt.Location, "; expected");
4305 if (RootContext.Version == LanguageVersion.ISO_1){
4306 Report.FeatureIsNotISO1 (lt.Location, "yield statement");
4309 if (anonymous_host == null){
4310 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4313 anonymous_host.SetYields ();
4314 $$ = new Yield ((Expression) $3, lt.Location);
4317 | IDENTIFIER RETURN SEMICOLON
4319 Report.Error (1627, (Location) $2, "Expression expected after yield return");
4322 | IDENTIFIER BREAK SEMICOLON
4324 LocatedToken lt = (LocatedToken) $1;
4325 string s = lt.Value;
4327 Report.Error (1003, lt.Location, "; expected");
4330 if (RootContext.Version == LanguageVersion.ISO_1){
4331 Report.FeatureIsNotISO1 (lt.Location, "yield statement");
4334 if (anonymous_host == null){
4335 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4338 anonymous_host.SetYields ();
4339 $$ = new YieldBreak (lt.Location);
4350 : TRY block catch_clauses
4354 ArrayList c = (ArrayList)$3;
4355 for (int i = 0; i < c.Count; ++i) {
4356 Catch cc = (Catch) c [i];
4358 if (i != c.Count - 1)
4359 Report.Error (1017, cc.loc, "Try statement already has an empty catch block");
4366 // Now s contains the list of specific catch clauses
4367 // and g contains the general one.
4369 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4371 | TRY block opt_catch_clauses FINALLY block
4374 ArrayList s = new ArrayList (4);
4375 ArrayList catch_list = (ArrayList) $3;
4377 if (catch_list != null){
4378 foreach (Catch cc in catch_list) {
4386 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4390 Report.Error (1524, (Location) $1, "Expected catch or finally");
4396 : /* empty */ { $$ = null; }
4403 ArrayList l = new ArrayList (4);
4408 | catch_clauses catch_clause
4410 ArrayList l = (ArrayList) $1;
4418 : /* empty */ { $$ = null; }
4423 : CATCH opt_catch_args
4425 Expression type = null;
4428 DictionaryEntry cc = (DictionaryEntry) $2;
4429 type = (Expression) cc.Key;
4430 LocatedToken lt = (LocatedToken) cc.Value;
4433 ArrayList one = new ArrayList (4);
4435 one.Add (new VariableDeclaration (lt, null));
4437 current_block = new Block (current_block);
4438 Block b = declare_local_variables (type, one, lt.Location);
4443 Expression type = null;
4445 Block var_block = null;
4448 DictionaryEntry cc = (DictionaryEntry) $2;
4449 type = (Expression) cc.Key;
4450 LocatedToken lt = (LocatedToken) cc.Value;
4454 while (current_block.Implicit)
4455 current_block = current_block.Parent;
4456 var_block = current_block;
4457 current_block = current_block.Parent;
4461 $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
4466 : /* empty */ { $$ = null; }
4471 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4473 $$ = new DictionaryEntry ($2, $3);
4481 $$ = new Checked ((Block) $2);
4488 $$ = new Unchecked ((Block) $2);
4495 RootContext.CheckUnsafeOption ((Location) $1);
4497 $$ = new Unsafe ((Block) $3);
4503 type fixed_pointer_declarators
4506 ArrayList list = (ArrayList) $4;
4507 Expression type = (Expression) $3;
4508 Location l = (Location) $1;
4509 int top = list.Count;
4511 Block assign_block = new Block (current_block);
4512 current_block = assign_block;
4514 for (int i = 0; i < top; i++){
4515 Pair p = (Pair) list [i];
4518 v = current_block.AddVariable (type, (string) p.First, l);
4522 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4530 Location l = (Location) $1;
4532 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4534 current_block.AddStatement (f);
4535 while (current_block.Implicit)
4536 current_block = current_block.Parent;
4538 current_block = current_block.Parent;
4542 fixed_pointer_declarators
4543 : fixed_pointer_declarator {
4544 ArrayList declarators = new ArrayList (4);
4546 declarators.Add ($1);
4549 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4551 ArrayList declarators = (ArrayList) $1;
4553 declarators.Add ($3);
4558 fixed_pointer_declarator
4559 : IDENTIFIER ASSIGN expression
4561 LocatedToken lt = (LocatedToken) $1;
4562 // FIXME: keep location
4563 $$ = new Pair (lt.Value, $3);
4567 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
4573 : LOCK OPEN_PARENS expression CLOSE_PARENS
4579 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
4584 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4586 Block assign_block = new Block (current_block);
4587 current_block = assign_block;
4589 if ($3 is DictionaryEntry){
4590 DictionaryEntry de = (DictionaryEntry) $3;
4591 Location l = (Location) $1;
4593 Expression type = (Expression) de.Key;
4594 ArrayList var_declarators = (ArrayList) de.Value;
4596 ArrayList vars = new ArrayList (4);
4598 foreach (VariableDeclaration decl in var_declarators){
4600 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4603 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
4605 Expression expr = decl.expression_or_array_initializer;
4607 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4610 LocalVariableReference var;
4612 // Get a writable reference to this read-only variable.
4613 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4615 // This is so that it is not a warning on using variables
4618 vars.Add (new DictionaryEntry (var, expr));
4620 // Assign a = new Assign (var, expr, decl.Location);
4621 // assign_block.AddStatement (new StatementExpression (a));
4624 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4625 // It can be referred to as $5 below.
4626 $$ = new DictionaryEntry (type, vars);
4633 Using u = new Using ($5, (Statement) $6, (Location) $1);
4634 current_block.AddStatement (u);
4635 while (current_block.Implicit)
4636 current_block = current_block.Parent;
4638 current_block = current_block.Parent;
4642 resource_acquisition
4643 : local_variable_declaration
4650 // A class used to pass around variable declarations and constants
4652 public class VariableDeclaration {
4653 public string identifier;
4654 public Expression expression_or_array_initializer;
4655 public Location Location;
4656 public Attributes OptAttributes;
4657 public string DocComment;
4659 public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
4661 this.identifier = lt.Value;
4662 if (eoai is ArrayList) {
4663 if (CSharpParser.current_array_type == null)
4664 Report.Error (622, lt.Location,
4665 "Can only use array initializer expressions to assign to array types. Try using a new expression instead.");
4666 this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
4668 this.expression_or_array_initializer = (Expression)eoai;
4670 this.Location = lt.Location;
4671 this.OptAttributes = opt_attrs;
4674 public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
4680 // A class used to hold info about an indexer declarator
4682 public class IndexerDeclaration {
4683 public Expression type;
4684 public MemberName interface_type;
4685 public Parameters param_list;
4686 public Location location;
4688 public IndexerDeclaration (Expression type, MemberName interface_type,
4689 Parameters param_list, Location loc)
4692 this.interface_type = interface_type;
4693 this.param_list = param_list;
4694 this.location = loc;
4699 // We use this when we do not have an object in advance that is an IAnonymousHost
4701 public class SimpleAnonymousHost : IAnonymousHost {
4702 public static readonly SimpleAnonymousHost Simple = new SimpleAnonymousHost ();
4705 ArrayList anonymous_methods;
4707 public static SimpleAnonymousHost GetSimple () {
4708 Simple.yields = false;
4709 Simple.anonymous_methods = null;
4713 public void SetYields ()
4718 public void AddAnonymousMethod (AnonymousMethodExpression anonymous)
4720 if (anonymous_methods == null)
4721 anonymous_methods = new ArrayList ();
4722 anonymous_methods.Add (anonymous);
4725 public void Propagate (IAnonymousHost real_host)
4728 real_host.SetYields ();
4729 if (anonymous_methods != null) {
4730 foreach (AnonymousMethodExpression ame in anonymous_methods)
4731 real_host.AddAnonymousMethod (ame);
4737 // A class used to hold info about an operator declarator
4739 public class OperatorDeclaration {
4740 public Operator.OpType optype;
4741 public Expression ret_type, arg1type, arg2type;
4742 public string arg1name, arg2name;
4743 public Location location;
4745 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4746 Expression arg1type, string arg1name,
4747 Expression arg2type, string arg2name, Location location)
4750 this.ret_type = ret_type;
4751 this.arg1type = arg1type;
4752 this.arg1name = arg1name;
4753 this.arg2type = arg2type;
4754 this.arg2name = arg2name;
4755 this.location = location;
4760 void Error_ExpectingTypeName (Expression expr)
4762 if (expr is Invocation){
4763 Report.Error (1002, expr.Location, "Expecting `;'");
4765 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
4769 public static void Error_ParameterModifierNotValid (Location loc)
4771 Report.Error (631, loc, "The modifiers `ref' and `out' are not valid in this context");
4774 void push_current_class (TypeContainer tc, bool is_interface, object partial_token)
4776 if (partial_token != null)
4777 current_container = current_container.AddPartial (tc, is_interface);
4779 current_container = current_container.AddTypeContainer (tc, is_interface);
4783 DeclSpace pop_current_class ()
4785 DeclSpace retval = current_class;
4787 current_class = current_class.Parent;
4788 current_container = current_class.PartialContainer;
4794 // Given the @class_name name, it creates a fully qualified name
4795 // based on the containing declaration space
4798 MakeName (MemberName class_name)
4800 Namespace ns = current_namespace.NS;
4802 if (current_container.Name.Length == 0){
4803 if (ns.Name.Length != 0)
4804 return new MemberName (ns.MemberName, class_name);
4808 return new MemberName (current_container.MemberName, class_name);
4812 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4814 Block implicit_block;
4815 ArrayList inits = null;
4818 // We use the `Used' property to check whether statements
4819 // have been added to the current block. If so, we need
4820 // to create another block to contain the new declaration
4821 // otherwise, as an optimization, we use the same block to
4822 // add the declaration.
4824 // FIXME: A further optimization is to check if the statements
4825 // that were added were added as part of the initialization
4826 // below. In which case, no other statements have been executed
4827 // and we might be able to reduce the number of blocks for
4828 // situations like this:
4830 // int j = 1; int k = j + 1;
4832 if (current_block.Used)
4833 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4835 implicit_block = current_block;
4837 foreach (VariableDeclaration decl in variable_declarators){
4839 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
4840 if (decl.expression_or_array_initializer != null){
4842 inits = new ArrayList (4);
4849 return implicit_block;
4851 foreach (VariableDeclaration decl in inits){
4853 Expression expr = decl.expression_or_array_initializer;
4855 LocalVariableReference var;
4856 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4858 assign = new Assign (var, expr, decl.Location);
4860 implicit_block.AddStatement (new StatementExpression (assign));
4863 return implicit_block;
4866 Block declare_local_constants (Expression type, ArrayList declarators)
4868 Block implicit_block;
4870 if (current_block.Used)
4871 implicit_block = new Block (current_block, Block.Flags.Implicit);
4873 implicit_block = current_block;
4875 foreach (VariableDeclaration decl in declarators){
4876 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
4879 return implicit_block;
4882 void CheckAttributeTarget (string a, Location l)
4886 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4890 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4896 void CheckUnaryOperator (Operator.OpType op, Location l)
4900 case Operator.OpType.LogicalNot:
4901 case Operator.OpType.OnesComplement:
4902 case Operator.OpType.Increment:
4903 case Operator.OpType.Decrement:
4904 case Operator.OpType.True:
4905 case Operator.OpType.False:
4906 case Operator.OpType.Addition:
4907 case Operator.OpType.Subtraction:
4912 Report.Error (1019, l, "Overloadable unary operator expected");
4918 void CheckBinaryOperator (Operator.OpType op, Location l)
4922 case Operator.OpType.Addition:
4923 case Operator.OpType.Subtraction:
4924 case Operator.OpType.Multiply:
4925 case Operator.OpType.Division:
4926 case Operator.OpType.Modulus:
4927 case Operator.OpType.BitwiseAnd:
4928 case Operator.OpType.BitwiseOr:
4929 case Operator.OpType.ExclusiveOr:
4930 case Operator.OpType.LeftShift:
4931 case Operator.OpType.RightShift:
4932 case Operator.OpType.Equality:
4933 case Operator.OpType.Inequality:
4934 case Operator.OpType.GreaterThan:
4935 case Operator.OpType.LessThan:
4936 case Operator.OpType.GreaterThanOrEqual:
4937 case Operator.OpType.LessThanOrEqual:
4941 Report.Error (1020, l, "Overloadable binary operator expected");
4947 void syntax_error (Location l, string msg)
4949 Report.Error (1003, l, "Syntax error, " + msg);
4952 void note (string s)
4954 // Used to put annotations
4959 public Tokenizer Lexer {
4965 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4967 this.name = file.Name;
4969 current_namespace = new NamespaceEntry (null, file, null);
4970 current_class = current_namespace.SlaveDeclSpace;
4971 current_container = current_class.PartialContainer; // == RootContest.ToplevelTypes
4973 lexer = new Tokenizer (reader, file, defines);
4976 public void parse ()
4978 int errors = Report.Errors;
4980 if (yacc_verbose_flag > 1)
4981 yyparse (lexer, new yydebug.yyDebugSimple ());
4984 Tokenizer tokenizer = lexer as Tokenizer;
4985 tokenizer.cleanup ();
4986 } catch (Exception e){
4988 // Removed for production use, use parser verbose to get the output.
4990 // Console.WriteLine (e);
4991 if (Report.Errors == errors)
4992 Report.Error (-25, lexer.Location, "Parsing error");
4993 if (yacc_verbose_flag > 0)
4994 Console.WriteLine (e);
4997 if (RootContext.ToplevelTypes.NamespaceEntry != null)
4998 throw new InternalErrorException ("who set it?");
5001 static void CheckToken (int error, int yyToken, string msg, Location loc)
5003 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
5004 Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ());
5006 Report.Error (error, loc, msg);
5009 void CheckIdentifierToken (int yyToken, Location loc)
5011 CheckToken (1041, yyToken, "Identifier expected", loc);
5014 string ConsumeStoredComment ()
5016 string s = tmpComment;
5018 Lexer.doc_state = XmlCommentState.Allowed;
5022 Location GetLocation (object obj)
5024 if (obj is MemberCore)
5025 return ((MemberCore) obj).Location;
5026 if (obj is MemberName)
5027 return ((MemberName) obj).Location;
5028 if (obj is LocatedToken)
5029 return ((LocatedToken) obj).Location;
5030 if (obj is Location)
5031 return (Location) obj;
5032 return lexer.Location;