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 Parameter.Modifier p2 = (Parameter.Modifier)$2;
1312 Parameter.Modifier mod = (Parameter.Modifier)$1 | p2;
1313 if (((Parameter.Modifier)$1 & p2) == p2) {
1314 Error_DuplicateParameterModifier (lexer.Location, p2);
1316 Report.Error (1108, lexer.Location, "A parameter cannot have specified more than one modifier");
1323 : REF { $$ = Parameter.Modifier.REF; }
1324 | OUT { $$ = Parameter.Modifier.OUT; }
1328 : opt_attributes PARAMS type IDENTIFIER
1330 LocatedToken lt = (LocatedToken) $4;
1331 $$ = new ParamsParameter ((Expression) $3, lt.Value, (Attributes) $1, lt.Location);
1333 | opt_attributes PARAMS PARAMS type IDENTIFIER
1335 Error_DuplicateParameterModifier (lexer.Location, Parameter.Modifier.PARAMS);
1338 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1340 Report.Error (1611, (Location) $2, "The params parameter cannot be declared as ref or out");
1343 | opt_attributes PARAMS type error {
1344 CheckIdentifierToken (yyToken, GetLocation ($4));
1349 property_declaration
1353 namespace_or_type_name
1355 if (RootContext.Documentation != null)
1356 tmpComment = Lexer.consume_doc_comment ();
1360 implicit_value_parameter_type = (Expression) $3;
1362 lexer.PropertyParsing = true;
1364 accessor_declarations
1366 lexer.PropertyParsing = false;
1367 has_get = has_set = false;
1375 Accessors accessors = (Accessors) $8;
1376 Accessor get_block = accessors.get_or_add;
1377 Accessor set_block = accessors.set_or_remove;
1379 MemberName name = (MemberName) $4;
1381 prop = new Property (current_class, (Expression) $3, (int) $2,
1382 false, name, (Attributes) $1, get_block, set_block, accessors.declared_in_reverse);
1384 current_container.AddProperty (prop);
1385 implicit_value_parameter_type = null;
1387 if (RootContext.Documentation != null)
1388 prop.DocComment = ConsumeStoredComment ();
1393 accessor_declarations
1394 : get_accessor_declaration
1396 $$ = new Accessors ((Accessor) $1, null);
1398 | get_accessor_declaration accessor_declarations
1400 Accessors accessors = (Accessors) $2;
1401 accessors.get_or_add = (Accessor) $1;
1404 | set_accessor_declaration
1406 $$ = new Accessors (null, (Accessor) $1);
1408 | set_accessor_declaration accessor_declarations
1410 Accessors accessors = (Accessors) $2;
1411 accessors.set_or_remove = (Accessor) $1;
1412 accessors.declared_in_reverse = true;
1417 Report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1422 get_accessor_declaration
1423 : opt_attributes opt_modifiers GET
1425 // If this is not the case, then current_local_parameters has already
1426 // been set in indexer_declaration
1427 if (parsing_indexer == false)
1428 current_local_parameters = null;
1430 current_local_parameters = indexer_parameters;
1431 lexer.PropertyParsing = false;
1433 anonymous_host = SimpleAnonymousHost.GetSimple ();
1438 Report.Error (1007, (Location) $3, "Property accessor already defined");
1441 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1443 current_local_parameters = null;
1444 lexer.PropertyParsing = true;
1446 SimpleAnonymousHost.Simple.Propagate (accessor);
1447 anonymous_host = null;
1449 if (RootContext.Documentation != null)
1450 if (Lexer.doc_state == XmlCommentState.Error)
1451 Lexer.doc_state = XmlCommentState.NotAllowed;
1457 set_accessor_declaration
1458 : opt_attributes opt_modifiers SET
1461 Parameter implicit_value_parameter = new Parameter (
1462 implicit_value_parameter_type, "value",
1463 Parameter.Modifier.NONE, null, (Location) $3);
1465 if (parsing_indexer == false) {
1466 args = new Parameter [1];
1467 args [0] = implicit_value_parameter;
1468 current_local_parameters = new Parameters (args);
1470 Parameter [] fpars = indexer_parameters.FixedParameters;
1473 int count = fpars.Length;
1475 args = new Parameter [count + 1];
1476 fpars.CopyTo (args, 0);
1477 args [count] = implicit_value_parameter;
1480 current_local_parameters = new Parameters (
1484 lexer.PropertyParsing = false;
1486 anonymous_host = SimpleAnonymousHost.GetSimple ();
1491 Report.Error (1007, ((LocatedToken) $3).Location, "Property accessor already defined");
1494 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1496 current_local_parameters = null;
1497 lexer.PropertyParsing = true;
1499 SimpleAnonymousHost.Simple.Propagate (accessor);
1500 anonymous_host = null;
1502 if (RootContext.Documentation != null
1503 && Lexer.doc_state == XmlCommentState.Error)
1504 Lexer.doc_state = XmlCommentState.NotAllowed;
1512 | SEMICOLON { $$ = null; }
1515 interface_declaration
1519 INTERFACE member_name
1521 MemberName name = MakeName ((MemberName) $5);
1523 push_current_class (new Interface (
1524 current_namespace, current_class, name, (int) $2,
1525 (Attributes) $1), true, $3);
1529 current_container.AddBasesForPart (current_class, (ArrayList) $7);
1531 if (RootContext.Documentation != null) {
1532 current_container.DocComment = Lexer.consume_doc_comment ();
1533 Lexer.doc_state = XmlCommentState.Allowed;
1538 if (RootContext.Documentation != null)
1539 Lexer.doc_state = XmlCommentState.Allowed;
1543 $$ = pop_current_class ();
1545 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1546 CheckIdentifierToken (yyToken, GetLocation ($5));
1552 opt_interface_member_declarations
1556 opt_interface_member_declarations
1558 | interface_member_declarations
1561 interface_member_declarations
1562 : interface_member_declaration
1563 | interface_member_declarations interface_member_declaration
1566 interface_member_declaration
1567 : interface_method_declaration
1572 Method m = (Method) $1;
1574 if (m.IsExplicitImpl)
1575 Report.Error (541, m.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1576 m.GetSignatureForError ());
1578 current_container.AddMethod (m);
1580 if (RootContext.Documentation != null)
1581 Lexer.doc_state = XmlCommentState.Allowed;
1583 | interface_property_declaration
1588 Property p = (Property) $1;
1590 if (p.IsExplicitImpl)
1591 Report.Error (541, p.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1592 p.GetSignatureForError ());
1594 current_container.AddProperty (p);
1596 if (RootContext.Documentation != null)
1597 Lexer.doc_state = XmlCommentState.Allowed;
1599 | interface_event_declaration
1602 Event e = (Event) $1;
1604 if (e.IsExplicitImpl)
1605 Report.Error (541, e.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1606 e.GetSignatureForError ());
1608 current_container.AddEvent (e);
1611 if (RootContext.Documentation != null)
1612 Lexer.doc_state = XmlCommentState.Allowed;
1614 | interface_indexer_declaration
1619 Indexer i = (Indexer) $1;
1621 if (i.IsExplicitImpl)
1622 Report.Error (541, i.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1623 i.GetSignatureForError ());
1625 current_container.AddIndexer (i);
1627 if (RootContext.Documentation != null)
1628 Lexer.doc_state = XmlCommentState.Allowed;
1630 | delegate_declaration
1633 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1634 ((MemberCore)$1).GetSignatureForError ());
1640 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1641 ((MemberCore)$1).GetSignatureForError ());
1644 | struct_declaration
1647 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1648 ((MemberCore)$1).GetSignatureForError ());
1654 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1655 ((MemberCore)$1).GetSignatureForError ());
1658 | interface_declaration
1661 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1662 ((MemberCore)$1).GetSignatureForError ());
1665 | constant_declaration
1667 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1675 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, GetLocation ($1));
1680 interface_method_declaration
1681 : opt_attributes opt_new type namespace_or_type_name
1682 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1685 $$ = new Method (current_class, null, (Expression) $3, (int) $2, true,
1686 (MemberName) $4, (Parameters) $6, (Attributes) $1);
1687 if (RootContext.Documentation != null)
1688 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1690 | opt_attributes opt_new type namespace_or_type_name
1691 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1692 OPEN_BRACE opt_statement_list CLOSE_BRACE
1694 $$ = new Method (current_class, null, (Expression) $3, (int) $2, true,
1695 (MemberName) $4, (Parameters) $6, (Attributes) $1);
1697 Report.Error (531, lexer.Location, "`{0}': interface members cannot have a definition",
1698 ((Method)$$).GetSignatureForError ());
1702 interface_property_declaration
1707 { lexer.PropertyParsing = true; }
1708 accessor_declarations
1710 has_get = has_set = false;
1711 lexer.PropertyParsing = false;
1715 LocatedToken lt = (LocatedToken) $4;
1716 MemberName name = new MemberName (lt.Value, lt.Location);
1718 if ($3 == TypeManager.system_void_expr) {
1719 Report.Error (547, lt.Location, "`{0}': property or indexer cannot have void type", lt.Value);
1725 p = new Property (current_class, (Expression) $3, (int) $2, true,
1726 name, (Attributes) $1,
1729 Report.Error (548, p.Location, "`{0}': property or indexer must have at least one accessor", p.GetSignatureForError ());
1733 Accessors accessor = (Accessors) $7;
1734 p = new Property (current_class, (Expression) $3, (int) $2, true,
1735 name, (Attributes) $1,
1736 accessor.get_or_add, accessor.set_or_remove, accessor.declared_in_reverse);
1738 if (accessor.get_or_add != null && accessor.get_or_add.Block != null) {
1739 Report.Error (531, p.Location, "`{0}.get': interface members cannot have a definition", p.GetSignatureForError ());
1744 if (accessor.set_or_remove != null && accessor.set_or_remove.Block != null) {
1745 Report.Error (531, p.Location, "`{0}.set': interface members cannot have a definition", p.GetSignatureForError ());
1750 if (RootContext.Documentation != null)
1751 p.DocComment = Lexer.consume_doc_comment ();
1758 CheckIdentifierToken (yyToken, GetLocation ($4));
1764 interface_event_declaration
1765 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1767 LocatedToken lt = (LocatedToken) $5;
1768 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1769 new MemberName (lt.Value, lt.Location),
1771 if (RootContext.Documentation != null)
1772 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1774 | opt_attributes opt_new EVENT type error {
1775 CheckIdentifierToken (yyToken, GetLocation ($5));
1778 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1779 LocatedToken lt = (LocatedToken) $5;
1780 Report.Error (68, lt.Location, "`{0}.{1}': event in interface cannot have initializer", current_container.Name, lt.Value);
1783 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1785 lexer.EventParsing = true;
1787 event_accessor_declarations
1789 lexer.EventParsing = false;
1792 Report.Error (69, (Location) $3, "Event in interface cannot have add or remove accessors");
1797 interface_indexer_declaration
1798 : opt_attributes opt_new type THIS
1799 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1801 { lexer.PropertyParsing = true; }
1802 accessor_declarations
1804 has_get = has_set = false;
1805 lexer.PropertyParsing = false;
1811 i = new Indexer (current_class, (Expression) $3,
1812 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1813 (int) $2, true, (Parameters) $6, (Attributes) $1,
1816 Report.Error (548, i.Location, "`{0}': property or indexer must have at least one accessor", i.GetSignatureForError ());
1820 Accessors accessors = (Accessors) $10;
1821 i = new Indexer (current_class, (Expression) $3,
1822 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1823 (int) $2, true, (Parameters) $6, (Attributes) $1,
1824 accessors.get_or_add, accessors.set_or_remove, accessors.declared_in_reverse);
1826 if (accessors.get_or_add != null && accessors.get_or_add.Block != null) {
1827 Report.Error (531, i.Location, "`{0}.get': interface members cannot have a definition", i.GetSignatureForError ());
1832 if (accessors.set_or_remove != null && accessors.set_or_remove.Block != null) {
1833 Report.Error (531, i.Location, "`{0}.set': interface members cannot have a definition", i.GetSignatureForError ());
1838 if (RootContext.Documentation != null)
1839 i.DocComment = ConsumeStoredComment ();
1845 operator_declaration
1846 : opt_attributes opt_modifiers operator_declarator
1848 anonymous_host = SimpleAnonymousHost.GetSimple ();
1855 OperatorDeclaration decl = (OperatorDeclaration) $3;
1857 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1859 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null, decl.location);
1860 if (decl.arg2type != null)
1861 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null, decl.location);
1863 Operator op = new Operator (
1864 current_class, decl.optype, decl.ret_type, (int) $2,
1865 new Parameters (param_list),
1866 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1868 if (RootContext.Documentation != null) {
1869 op.DocComment = tmpComment;
1870 Lexer.doc_state = XmlCommentState.Allowed;
1873 SimpleAnonymousHost.Simple.Propagate (op);
1874 anonymous_host = null;
1876 // Note again, checking is done in semantic analysis
1877 current_container.AddOperator (op);
1879 current_local_parameters = null;
1885 | SEMICOLON { $$ = null; }
1888 : type OPERATOR overloadable_operator
1889 OPEN_PARENS opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
1891 // TODO: wrong location
1892 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
1893 Error_ParameterModifierNotValid ((Location) $2);
1895 LocatedToken lt = (LocatedToken) $7;
1896 Operator.OpType op = (Operator.OpType) $3;
1897 CheckUnaryOperator (op, lt.Location);
1899 if (op == Operator.OpType.Addition)
1900 op = Operator.OpType.UnaryPlus;
1902 if (op == Operator.OpType.Subtraction)
1903 op = Operator.OpType.UnaryNegation;
1905 Parameter [] pars = new Parameter [1];
1906 Expression type = (Expression) $6;
1908 pars [0] = new Parameter (type, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1910 current_local_parameters = new Parameters (pars);
1912 if (RootContext.Documentation != null) {
1913 tmpComment = Lexer.consume_doc_comment ();
1914 Lexer.doc_state = XmlCommentState.NotAllowed;
1917 $$ = new OperatorDeclaration (op, (Expression) $1, type, lt.Value,
1918 null, null, (Location) $2);
1920 | type OPERATOR overloadable_operator
1922 opt_parameter_modifier type IDENTIFIER COMMA
1923 opt_parameter_modifier type IDENTIFIER
1926 // TODO: wrong location
1927 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE || (Parameter.Modifier)$9 != Parameter.Modifier.NONE)
1928 Error_ParameterModifierNotValid ((Location) $2);
1930 LocatedToken ltParam1 = (LocatedToken) $7;
1931 LocatedToken ltParam2 = (LocatedToken) $11;
1932 CheckBinaryOperator ((Operator.OpType) $3, (Location) $2);
1934 Parameter [] pars = new Parameter [2];
1936 Expression typeL = (Expression) $6;
1937 Expression typeR = (Expression) $10;
1939 pars [0] = new Parameter (typeL, ltParam1.Value, Parameter.Modifier.NONE, null, ltParam1.Location);
1940 pars [1] = new Parameter (typeR, ltParam2.Value, Parameter.Modifier.NONE, null, ltParam2.Location);
1942 current_local_parameters = new Parameters (pars);
1944 if (RootContext.Documentation != null) {
1945 tmpComment = Lexer.consume_doc_comment ();
1946 Lexer.doc_state = XmlCommentState.NotAllowed;
1949 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1950 typeL, ltParam1.Value,
1951 typeR, ltParam2.Value, (Location) $2);
1953 | conversion_operator_declarator
1954 | type OPERATOR overloadable_operator
1956 opt_parameter_modifier type IDENTIFIER COMMA
1957 opt_parameter_modifier type IDENTIFIER COMMA
1958 opt_parameter_modifier type IDENTIFIER
1961 Report.Error (1534, (Location) $2, "Overloaded binary operator `{0}' takes two parameters",
1962 Operator.GetName ((Operator.OpType) $3));
1965 | type OPERATOR overloadable_operator
1966 OPEN_PARENS CLOSE_PARENS
1968 Report.Error (1535, (Location) $2, "Overloaded unary operator `{0}' takes one parameter",
1969 Operator.GetName ((Operator.OpType) $3));
1974 overloadable_operator
1976 : BANG { $$ = Operator.OpType.LogicalNot; }
1977 | TILDE { $$ = Operator.OpType.OnesComplement; }
1978 | OP_INC { $$ = Operator.OpType.Increment; }
1979 | OP_DEC { $$ = Operator.OpType.Decrement; }
1980 | TRUE { $$ = Operator.OpType.True; }
1981 | FALSE { $$ = Operator.OpType.False; }
1982 // Unary and binary:
1983 | PLUS { $$ = Operator.OpType.Addition; }
1984 | MINUS { $$ = Operator.OpType.Subtraction; }
1986 | STAR { $$ = Operator.OpType.Multiply; }
1987 | DIV { $$ = Operator.OpType.Division; }
1988 | PERCENT { $$ = Operator.OpType.Modulus; }
1989 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1990 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1991 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1992 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1993 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1994 | OP_EQ { $$ = Operator.OpType.Equality; }
1995 | OP_NE { $$ = Operator.OpType.Inequality; }
1996 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1997 | OP_LT { $$ = Operator.OpType.LessThan; }
1998 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1999 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
2002 conversion_operator_declarator
2003 : IMPLICIT OPERATOR type OPEN_PARENS opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
2005 // TODO: wrong location
2006 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
2007 Error_ParameterModifierNotValid (GetLocation ($4));
2009 LocatedToken lt = (LocatedToken) $7;
2010 Parameter [] pars = new Parameter [1];
2012 pars [0] = new Parameter ((Expression) $6, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2014 current_local_parameters = new Parameters (pars);
2016 if (RootContext.Documentation != null) {
2017 tmpComment = Lexer.consume_doc_comment ();
2018 Lexer.doc_state = XmlCommentState.NotAllowed;
2021 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $6, lt.Value,
2022 null, null, (Location) $2);
2024 | EXPLICIT OPERATOR type OPEN_PARENS opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
2026 // TODO: wrong location
2027 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
2028 Error_ParameterModifierNotValid (GetLocation ($4));
2030 LocatedToken lt = (LocatedToken) $7;
2031 Parameter [] pars = new Parameter [1];
2033 pars [0] = new Parameter ((Expression) $6, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2035 current_local_parameters = new Parameters (pars);
2037 if (RootContext.Documentation != null) {
2038 tmpComment = Lexer.consume_doc_comment ();
2039 Lexer.doc_state = XmlCommentState.NotAllowed;
2042 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $6, lt.Value,
2043 null, null, (Location) $2);
2047 syntax_error ((Location) $1, "'operator' expected");
2051 syntax_error ((Location) $1, "'operator' expected");
2055 constructor_declaration
2058 constructor_declarator
2061 Constructor c = (Constructor) $3;
2062 c.Block = (ToplevelBlock) $4;
2063 c.OptAttributes = (Attributes) $1;
2064 c.ModFlags = (int) $2;
2066 if (RootContext.Documentation != null)
2067 c.DocComment = ConsumeStoredComment ();
2069 if (c.Name == current_container.Basename){
2070 if ((c.ModFlags & Modifiers.STATIC) != 0){
2071 if ((c.ModFlags & Modifiers.Accessibility) != 0){
2072 Report.Error (515, c.Location,
2073 "`{0}': access modifiers are not allowed on static constructors",
2074 c.GetSignatureForError ());
2077 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2079 if (c.Initializer != null){
2080 Report.Error (514, c.Location,
2081 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2082 c.GetSignatureForError ());
2085 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2088 // We let another layer check the validity of the constructor.
2089 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
2092 current_container.AddConstructor (c);
2094 current_local_parameters = null;
2095 if (RootContext.Documentation != null)
2096 Lexer.doc_state = XmlCommentState.Allowed;
2100 constructor_declarator
2103 if (RootContext.Documentation != null) {
2104 tmpComment = Lexer.consume_doc_comment ();
2105 Lexer.doc_state = XmlCommentState.Allowed;
2108 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2110 current_local_parameters = (Parameters) $4;
2112 opt_constructor_initializer
2114 LocatedToken lt = (LocatedToken) $1;
2115 $$ = new Constructor (current_class, lt.Value, 0, (Parameters) $4,
2116 (ConstructorInitializer) $7, lt.Location);
2118 anonymous_host = (IAnonymousHost) $$;
2124 | SEMICOLON { $$ = null; }
2127 opt_constructor_initializer
2128 : /* empty */ { $$ = null; }
2129 | constructor_initializer
2132 constructor_initializer
2133 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
2135 $$ = new ConstructorBaseInitializer ((ArrayList) $4, (Location) $2);
2137 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
2139 $$ = new ConstructorThisInitializer ((ArrayList) $4, (Location) $2);
2142 Report.Error (1018, (Location) $1, "Keyword this or base expected");
2148 : /* EMPTY */ { $$ = 0; }
2149 | UNSAFE { $$ = Modifiers.UNSAFE; }
2150 | EXTERN { $$ = Modifiers.EXTERN; }
2153 destructor_declaration
2154 : opt_attributes opt_finalizer TILDE
2156 if (RootContext.Documentation != null) {
2157 tmpComment = Lexer.consume_doc_comment ();
2158 Lexer.doc_state = XmlCommentState.NotAllowed;
2161 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2163 LocatedToken lt = (LocatedToken) $5;
2164 if (lt.Value != current_container.Basename){
2165 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2166 } else if (current_container.Kind != Kind.Class){
2167 Report.Error (575, lt.Location, "Only class types can contain destructor");
2169 Location l = lt.Location;
2172 if (!RootContext.StdLib && current_container.Name == "System.Object")
2173 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2175 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2177 Method d = new Destructor (
2178 current_class, TypeManager.system_void_expr, m, "Finalize",
2179 Parameters.EmptyReadOnlyParameters, (Attributes) $1, l);
2180 if (RootContext.Documentation != null)
2181 d.DocComment = ConsumeStoredComment ();
2183 d.Block = (ToplevelBlock) $8;
2184 current_container.AddMethod (d);
2192 EVENT type variable_declarators SEMICOLON
2194 current_array_type = null;
2195 foreach (VariableDeclaration var in (ArrayList) $5) {
2197 MemberName name = new MemberName (var.identifier,
2200 EventField e = new EventField (
2201 current_class, (Expression) $4, (int) $2, false, name,
2204 e.Initializer = var.expression_or_array_initializer;
2206 current_container.AddEvent (e);
2208 if (RootContext.Documentation != null) {
2209 e.DocComment = Lexer.consume_doc_comment ();
2210 Lexer.doc_state = XmlCommentState.Allowed;
2216 EVENT type namespace_or_type_name
2219 implicit_value_parameter_type = (Expression) $4;
2220 lexer.EventParsing = true;
2222 event_accessor_declarations
2224 lexer.EventParsing = false;
2228 MemberName name = (MemberName) $5;
2231 Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2232 current_container.Name, name.ToString ());
2235 Accessors accessors = (Accessors) $8;
2236 if (accessors.get_or_add == null || accessors.set_or_remove == null)
2237 // CS0073 is already reported, so no CS0065 here.
2240 Event e = new EventProperty (
2241 current_class, (Expression) $4, (int) $2, false, name,
2242 (Attributes) $1, accessors.get_or_add, accessors.set_or_remove);
2243 if (RootContext.Documentation != null) {
2244 e.DocComment = Lexer.consume_doc_comment ();
2245 Lexer.doc_state = XmlCommentState.Allowed;
2248 current_container.AddEvent (e);
2249 implicit_value_parameter_type = null;
2253 | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
2254 MemberName mn = (MemberName) $5;
2256 if (mn.Left != null)
2257 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2259 Report.Error (71, mn.Location, "Event declaration should use property syntax");
2261 if (RootContext.Documentation != null)
2262 Lexer.doc_state = XmlCommentState.Allowed;
2266 event_accessor_declarations
2267 : add_accessor_declaration remove_accessor_declaration
2269 $$ = new Accessors ((Accessor) $1, (Accessor) $2);
2271 | remove_accessor_declaration add_accessor_declaration
2273 Accessors accessors = new Accessors ((Accessor) $2, (Accessor) $1);
2274 accessors.declared_in_reverse = true;
2277 | add_accessor_declaration { $$ = null; }
2278 | remove_accessor_declaration { $$ = null; }
2281 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2287 add_accessor_declaration
2288 : opt_attributes ADD
2290 Parameter [] args = new Parameter [1];
2291 Parameter implicit_value_parameter = new Parameter (
2292 implicit_value_parameter_type, "value",
2293 Parameter.Modifier.NONE, null, (Location) $2);
2295 args [0] = implicit_value_parameter;
2297 current_local_parameters = new Parameters (args);
2298 lexer.EventParsing = false;
2302 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2303 lexer.EventParsing = true;
2305 | opt_attributes ADD error {
2306 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2309 | opt_attributes modifiers ADD {
2310 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2315 remove_accessor_declaration
2316 : opt_attributes REMOVE
2318 Parameter [] args = new Parameter [1];
2319 Parameter implicit_value_parameter = new Parameter (
2320 implicit_value_parameter_type, "value",
2321 Parameter.Modifier.NONE, null, (Location) $2);
2323 args [0] = implicit_value_parameter;
2325 current_local_parameters = new Parameters (args);
2326 lexer.EventParsing = false;
2330 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2331 lexer.EventParsing = true;
2333 | opt_attributes REMOVE error {
2334 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2337 | opt_attributes modifiers REMOVE {
2338 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2344 : opt_attributes opt_modifiers indexer_declarator
2347 IndexerDeclaration decl = (IndexerDeclaration) $3;
2349 implicit_value_parameter_type = decl.type;
2351 lexer.PropertyParsing = true;
2352 parsing_indexer = true;
2354 indexer_parameters = decl.param_list;
2355 anonymous_host = SimpleAnonymousHost.GetSimple ();
2357 accessor_declarations
2359 lexer.PropertyParsing = false;
2360 has_get = has_set = false;
2361 parsing_indexer = false;
2368 // The signature is computed from the signature of the indexer. Look
2369 // at section 3.6 on the spec
2371 IndexerDeclaration decl = (IndexerDeclaration) $3;
2372 Location loc = decl.location;
2373 Accessors accessors = (Accessors) $6;
2374 Accessor get_block = accessors.get_or_add;
2375 Accessor set_block = accessors.set_or_remove;
2378 if (decl.interface_type != null)
2379 name = new MemberName (decl.interface_type, TypeContainer.DefaultIndexerName, loc);
2381 name = new MemberName (TypeContainer.DefaultIndexerName, loc);
2383 indexer = new Indexer (current_class, decl.type, name,
2384 (int) $2, false, decl.param_list, (Attributes) $1,
2385 get_block, set_block, accessors.declared_in_reverse);
2387 if (RootContext.Documentation != null)
2388 indexer.DocComment = ConsumeStoredComment ();
2390 current_container.AddIndexer (indexer);
2392 current_local_parameters = null;
2393 implicit_value_parameter_type = null;
2394 indexer_parameters = null;
2399 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2401 Parameters pars = (Parameters) $4;
2402 if (pars.HasArglist) {
2403 // "__arglist is not valid in this context"
2404 Report.Error (1669, (Location) $2, "__arglist is not valid in this context");
2405 } else if (pars.Empty){
2406 Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
2408 if (RootContext.Documentation != null) {
2409 tmpComment = Lexer.consume_doc_comment ();
2410 Lexer.doc_state = XmlCommentState.Allowed;
2413 $$ = new IndexerDeclaration ((Expression) $1, null, pars, (Location) $2);
2415 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2417 Parameters pars = (Parameters) $6;
2419 if (pars.HasArglist) {
2420 // "__arglist is not valid in this context"
2421 Report.Error (1669, (Location) $4, "__arglist is not valid in this context");
2422 } else if (pars.Empty){
2423 Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
2426 MemberName name = (MemberName) $2;
2427 $$ = new IndexerDeclaration ((Expression) $1, name, pars, (Location) $4);
2429 if (RootContext.Documentation != null) {
2430 tmpComment = Lexer.consume_doc_comment ();
2431 Lexer.doc_state = XmlCommentState.Allowed;
2441 if (RootContext.Documentation != null)
2442 enumTypeComment = Lexer.consume_doc_comment ();
2447 LocatedToken lt = (LocatedToken) $4;
2448 Location enum_location = lt.Location;
2450 MemberName name = MakeName (new MemberName (lt.Value, enum_location));
2451 Enum e = new Enum (current_namespace, current_class, (Expression) $5, (int) $2,
2452 name, (Attributes) $1);
2454 if (RootContext.Documentation != null)
2455 e.DocComment = enumTypeComment;
2458 EnumMember em = null;
2459 foreach (VariableDeclaration ev in (ArrayList) $7) {
2460 em = new EnumMember (e, em, (Expression) ev.expression_or_array_initializer,
2461 new MemberName (ev.identifier, ev.Location), ev.OptAttributes);
2463 // if (RootContext.Documentation != null)
2464 em.DocComment = ev.DocComment;
2466 e.AddEnumMember (em);
2469 current_container.AddEnum (e);
2476 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2477 | COLON type { $$ = $2; }
2483 if (RootContext.Documentation != null)
2484 Lexer.doc_state = XmlCommentState.Allowed;
2486 opt_enum_member_declarations
2488 // here will be evaluated after CLOSE_BLACE is consumed.
2489 if (RootContext.Documentation != null)
2490 Lexer.doc_state = XmlCommentState.Allowed;
2498 opt_enum_member_declarations
2499 : /* empty */ { $$ = new ArrayList (4); }
2500 | enum_member_declarations opt_comma { $$ = $1; }
2503 enum_member_declarations
2504 : enum_member_declaration
2506 ArrayList l = new ArrayList (4);
2511 | enum_member_declarations COMMA enum_member_declaration
2513 ArrayList l = (ArrayList) $1;
2521 enum_member_declaration
2522 : opt_attributes IDENTIFIER
2524 VariableDeclaration vd = new VariableDeclaration (
2525 (LocatedToken) $2, null, (Attributes) $1);
2527 if (RootContext.Documentation != null) {
2528 vd.DocComment = Lexer.consume_doc_comment ();
2529 Lexer.doc_state = XmlCommentState.Allowed;
2534 | opt_attributes IDENTIFIER
2536 if (RootContext.Documentation != null) {
2537 tmpComment = Lexer.consume_doc_comment ();
2538 Lexer.doc_state = XmlCommentState.NotAllowed;
2543 VariableDeclaration vd = new VariableDeclaration (
2544 (LocatedToken) $2, $5, (Attributes) $1);
2546 if (RootContext.Documentation != null)
2547 vd.DocComment = ConsumeStoredComment ();
2553 delegate_declaration
2556 DELEGATE type member_name
2557 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2560 MemberName name = MakeName ((MemberName) $5);
2561 Parameters p = (Parameters) $7;
2563 // TODO: wrong location
2564 Report.Error (1669, name.Location, "__arglist is not valid in this context");
2567 Delegate del = new Delegate (current_namespace, current_class, (Expression) $4,
2568 (int) $2, name, p, (Attributes) $1);
2570 if (RootContext.Documentation != null) {
2571 del.DocComment = Lexer.consume_doc_comment ();
2572 Lexer.doc_state = XmlCommentState.Allowed;
2575 current_container.AddDelegate (del);
2580 namespace_or_type_name
2582 | IDENTIFIER DOUBLE_COLON IDENTIFIER {
2583 LocatedToken lt1 = (LocatedToken) $1;
2584 LocatedToken lt2 = (LocatedToken) $3;
2585 $$ = new MemberName (lt1.Value, lt2.Value, lt2.Location);
2587 | namespace_or_type_name DOT IDENTIFIER {
2588 LocatedToken lt = (LocatedToken) $3;
2589 $$ = new MemberName ((MemberName) $1, lt.Value);
2595 LocatedToken lt = (LocatedToken) $1;
2596 $$ = new MemberName (lt.Value, lt.Location);
2601 * Before you think of adding a return_type, notice that we have been
2602 * using two rules in the places where it matters (one rule using type
2603 * and another identical one that uses VOID as the return type). This
2604 * gets rid of a shift/reduce couple
2607 : namespace_or_type_name
2609 MemberName name = (MemberName) $1;
2610 $$ = name.GetTypeExpression ();
2622 // Note that here only unmanaged types are allowed but we
2623 // can't perform checks during this phase - we do it during
2624 // semantic analysis.
2626 $$ = new ComposedCast ((Expression) $1, "*", Lexer.Location);
2630 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2636 | non_expression_type rank_specifier
2638 Location loc = GetLocation ($1);
2640 loc = lexer.Location;
2641 $$ = new ComposedCast ((Expression) $1, (string) $2, loc);
2643 | non_expression_type STAR
2645 Location loc = GetLocation ($1);
2647 loc = lexer.Location;
2648 $$ = new ComposedCast ((Expression) $1, "*", loc);
2650 | expression rank_specifiers
2652 $$ = new ComposedCast ((Expression) $1, (string) $2);
2656 $$ = new ComposedCast ((Expression) $1, "*");
2660 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2661 // through this different path
2663 | multiplicative_expression STAR
2665 $$ = new ComposedCast ((Expression) $1, "*");
2672 ArrayList types = new ArrayList (4);
2677 | type_list COMMA type
2679 ArrayList types = (ArrayList) $1;
2687 * replaces all the productions for isolating the various
2688 * simple types, but we need this to reuse it easily in local_variable_type
2691 : OBJECT { $$ = TypeManager.system_object_expr; }
2692 | STRING { $$ = TypeManager.system_string_expr; }
2693 | BOOL { $$ = TypeManager.system_boolean_expr; }
2694 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2695 | FLOAT { $$ = TypeManager.system_single_expr; }
2696 | DOUBLE { $$ = TypeManager.system_double_expr; }
2701 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2702 | BYTE { $$ = TypeManager.system_byte_expr; }
2703 | SHORT { $$ = TypeManager.system_int16_expr; }
2704 | USHORT { $$ = TypeManager.system_uint16_expr; }
2705 | INT { $$ = TypeManager.system_int32_expr; }
2706 | UINT { $$ = TypeManager.system_uint32_expr; }
2707 | LONG { $$ = TypeManager.system_int64_expr; }
2708 | ULONG { $$ = TypeManager.system_uint64_expr; }
2709 | CHAR { $$ = TypeManager.system_char_expr; }
2710 | VOID { $$ = TypeManager.system_void_expr; }
2714 : type rank_specifiers
2716 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2);
2721 // Expressions, section 7.5
2731 MemberName mn = (MemberName) $1;
2732 $$ = mn.GetTypeExpression ();
2734 | IDENTIFIER DOUBLE_COLON IDENTIFIER
2736 LocatedToken lt1 = (LocatedToken) $1;
2737 LocatedToken lt2 = (LocatedToken) $3;
2738 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, lt2.Location);
2740 | parenthesized_expression
2742 | invocation_expression
2746 | post_increment_expression
2747 | post_decrement_expression
2751 | checked_expression
2752 | unchecked_expression
2753 | pointer_member_access
2754 | anonymous_method_expression
2761 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
2762 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); }
2763 | NULL { $$ = new NullLiteral (lexer.Location); }
2767 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
2768 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
2769 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
2774 object v = lexer.Value;
2777 $$ = new IntLiteral ((int) v, lexer.Location);
2778 } else if (v is uint)
2779 $$ = new UIntLiteral ((UInt32) v, lexer.Location);
2781 $$ = new LongLiteral ((Int64) v, lexer.Location);
2782 else if (v is ulong)
2783 $$ = new ULongLiteral ((UInt64) v, lexer.Location);
2785 Console.WriteLine ("OOPS. Unexpected result from scanner");
2790 : TRUE { $$ = new BoolLiteral (true, lexer.Location); }
2791 | FALSE { $$ = new BoolLiteral (false, lexer.Location); }
2794 parenthesized_expression_0
2795 : OPEN_PARENS expression CLOSE_PARENS
2798 lexer.Deambiguate_CloseParens ($$);
2799 // After this, the next token returned is one of
2800 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST (CLOSE_PARENS), CLOSE_PARENS_OPEN_PARENS
2801 // or CLOSE_PARENS_MINUS.
2803 | OPEN_PARENS expression error { CheckToken (1026, yyToken, "Expecting ')'", lexer.Location); }
2806 parenthesized_expression
2807 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2811 | parenthesized_expression_0 CLOSE_PARENS
2815 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2817 // If a parenthesized expression is followed by a minus, we need to wrap
2818 // the expression inside a ParenthesizedExpression for the CS0075 check
2819 // in Binary.DoResolve().
2820 $$ = new ParenthesizedExpression ((Expression) $1);
2825 : primary_expression DOT IDENTIFIER
2827 LocatedToken lt = (LocatedToken) $3;
2828 $$ = new MemberAccess ((Expression) $1, lt.Value);
2830 | predefined_type DOT IDENTIFIER
2832 LocatedToken lt = (LocatedToken) $3;
2833 // TODO: Location is wrong as some predefined types doesn't hold a location
2834 $$ = new MemberAccess ((Expression) $1, lt.Value, lt.Location);
2842 invocation_expression
2843 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2846 Report.Error (1, (Location) $2, "Parse error");
2848 $$ = new Invocation ((Expression) $1, (ArrayList) $3);
2850 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2852 $$ = new Invocation ((Expression) $1, new ArrayList ());
2854 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2856 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
2858 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2860 ArrayList args = new ArrayList (1);
2862 $$ = new Invocation ((Expression) $1, args);
2864 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2866 ArrayList args = ((ArrayList) $4);
2868 $$ = new Invocation ((Expression) $1, args);
2873 : /* empty */ { $$ = null; }
2880 ArrayList list = new ArrayList (4);
2884 | argument_list COMMA argument
2886 ArrayList list = (ArrayList) $1;
2890 | argument_list error {
2891 CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
2899 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2901 | non_simple_argument
2908 : REF variable_reference
2910 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2912 | OUT variable_reference
2914 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2916 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2918 ArrayList list = (ArrayList) $3;
2919 Argument[] args = new Argument [list.Count];
2920 list.CopyTo (args, 0);
2922 Expression expr = new Arglist (args, (Location) $1);
2923 $$ = new Argument (expr, Argument.AType.Expression);
2927 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
2932 : expression { note ("section 5.4"); $$ = $1; }
2936 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2938 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
2940 | primary_expression rank_specifiers
2942 // So the super-trick is that primary_expression
2943 // can only be either a SimpleName or a MemberAccess.
2944 // The MemberAccess case arises when you have a fully qualified type-name like :
2946 // SimpleName is when you have
2949 Expression expr = (Expression) $1;
2950 if (expr is ComposedCast){
2951 $$ = new ComposedCast (expr, (string) $2);
2952 } else if (!(expr is SimpleName || expr is MemberAccess || expr is QualifiedAliasMember)){
2953 Error_ExpectingTypeName (expr);
2954 $$ = TypeManager.system_object_expr;
2957 // So we extract the string corresponding to the SimpleName
2960 $$ = new ComposedCast (expr, (string) $2);
2962 current_array_type = (Expression)$$;
2969 ArrayList list = new ArrayList (4);
2973 | expression_list COMMA expression
2975 ArrayList list = (ArrayList) $1;
2984 $$ = new This (current_block, (Location) $1);
2989 : BASE DOT IDENTIFIER
2991 LocatedToken lt = (LocatedToken) $3;
2992 $$ = new BaseAccess (lt.Value, lt.Location);
2994 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2996 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
2999 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
3004 post_increment_expression
3005 : primary_expression OP_INC
3007 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
3008 (Expression) $1, (Location) $2);
3012 post_decrement_expression
3013 : primary_expression OP_DEC
3015 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
3016 (Expression) $1, (Location) $2);
3021 : object_or_delegate_creation_expression
3022 | array_creation_expression
3025 object_or_delegate_creation_expression
3026 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
3028 $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
3032 array_creation_expression
3033 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
3035 opt_array_initializer
3037 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
3039 | NEW type rank_specifiers array_initializer
3041 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, (Location) $1);
3045 Report.Error (1031, (Location) $1, "Type expected");
3050 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
3067 : rank_specifier opt_rank_specifier
3069 $$ = (string) $2 + (string) $1;
3074 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3076 $$ = "[" + (string) $2 + "]";
3096 | dim_separators COMMA
3098 $$ = (string) $1 + ",";
3102 opt_array_initializer
3114 : OPEN_BRACE CLOSE_BRACE
3116 ArrayList list = new ArrayList (4);
3119 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3121 $$ = (ArrayList) $2;
3125 variable_initializer_list
3126 : variable_initializer
3128 ArrayList list = new ArrayList (4);
3132 | variable_initializer_list COMMA variable_initializer
3134 ArrayList list = (ArrayList) $1;
3143 pushed_current_array_type = current_array_type;
3145 OPEN_PARENS type CLOSE_PARENS
3147 Expression type = (Expression)$4;
3148 if (type == TypeManager.system_void_expr)
3149 $$ = new TypeOfVoid ((Location) $1);
3151 $$ = new TypeOf (type, (Location) $1);
3152 current_array_type = pushed_current_array_type;
3157 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3158 $$ = new SizeOf ((Expression) $3, (Location) $1);
3163 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3165 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3169 unchecked_expression
3170 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3172 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3176 pointer_member_access
3177 : primary_expression OP_PTR IDENTIFIER
3180 LocatedToken lt = (LocatedToken) $3;
3182 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
3183 $$ = new MemberAccess (deref, lt.Value);
3187 anonymous_method_expression
3188 : DELEGATE opt_anonymous_method_signature
3190 if (oob_stack == null)
3191 oob_stack = new Stack (6);
3193 oob_stack.Push (current_anonymous_method);
3194 oob_stack.Push (current_local_parameters);
3195 current_local_parameters = (Parameters)$2;
3197 // Force the next block to be created as a ToplevelBlock
3198 oob_stack.Push (current_block);
3199 oob_stack.Push (top_current_block);
3201 Location loc = (Location) $1;
3202 current_anonymous_method = new AnonymousMethodExpression (
3203 current_anonymous_method, null, current_container,
3204 (Parameters) $2, (ToplevelBlock) top_current_block, loc);
3206 parsing_anonymous_method = true;
3210 Location loc = (Location) $1;
3211 top_current_block = (Block) oob_stack.Pop ();
3212 current_block = (Block) oob_stack.Pop ();
3214 if (RootContext.Version == LanguageVersion.ISO_1){
3215 Report.FeatureIsNotISO1 (loc, "anonymous methods");
3218 ToplevelBlock anon_block = (ToplevelBlock) $4;
3220 anon_block.Parent = current_block;
3222 current_anonymous_method.Block = anon_block;
3223 if ((anonymous_host != null) && (current_anonymous_method.Parent == null))
3224 anonymous_host.AddAnonymousMethod (current_anonymous_method);
3226 $$ = current_anonymous_method;
3229 current_local_parameters = (Parameters) oob_stack.Pop ();
3230 current_anonymous_method = (AnonymousMethodExpression) oob_stack.Pop ();
3234 opt_anonymous_method_signature
3235 : /* empty */ { $$ = null; }
3236 | anonymous_method_signature
3239 anonymous_method_signature
3240 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3243 $$ = Parameters.EmptyReadOnlyParameters;
3245 ArrayList par_list = (ArrayList) $2;
3246 Parameter [] pars = new Parameter [par_list.Count];
3247 par_list.CopyTo (pars);
3248 $$ = new Parameters (pars);
3253 opt_anonymous_method_parameter_list
3254 : /* empty */ { $$ = null; }
3255 | anonymous_method_parameter_list { $$ = $1; }
3258 anonymous_method_parameter_list
3259 : anonymous_method_parameter
3261 ArrayList a = new ArrayList (4);
3265 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3267 ArrayList a = (ArrayList) $1;
3273 anonymous_method_parameter
3274 : opt_parameter_modifier type IDENTIFIER {
3275 LocatedToken lt = (LocatedToken) $3;
3276 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3278 | PARAMS type IDENTIFIER {
3279 Report.Error (1670, ((LocatedToken) $3).Location, "The `params' modifier is not allowed in anonymous method declaration");
3285 : primary_expression
3286 | BANG prefixed_unary_expression
3288 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3290 | TILDE prefixed_unary_expression
3292 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3298 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3300 $$ = new Cast ((Expression) $1, (Expression) $3);
3302 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3304 $$ = new Cast ((Expression) $1, (Expression) $3);
3310 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3312 // TODO: wrong location
3313 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3318 // The idea to split this out is from Rhys' grammar
3319 // to solve the problem with casts.
3321 prefixed_unary_expression
3323 | PLUS prefixed_unary_expression
3325 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3327 | MINUS prefixed_unary_expression
3329 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3331 | OP_INC prefixed_unary_expression
3333 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3334 (Expression) $2, (Location) $1);
3336 | OP_DEC prefixed_unary_expression
3338 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3339 (Expression) $2, (Location) $1);
3341 | STAR prefixed_unary_expression
3343 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, (Location) $1);
3345 | BITWISE_AND prefixed_unary_expression
3347 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3351 pre_increment_expression
3352 : OP_INC prefixed_unary_expression
3354 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3355 (Expression) $2, (Location) $1);
3359 pre_decrement_expression
3360 : OP_DEC prefixed_unary_expression
3362 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3363 (Expression) $2, (Location) $1);
3367 multiplicative_expression
3368 : prefixed_unary_expression
3369 | multiplicative_expression STAR prefixed_unary_expression
3371 $$ = new Binary (Binary.Operator.Multiply,
3372 (Expression) $1, (Expression) $3);
3374 | multiplicative_expression DIV prefixed_unary_expression
3376 $$ = new Binary (Binary.Operator.Division,
3377 (Expression) $1, (Expression) $3);
3379 | multiplicative_expression PERCENT prefixed_unary_expression
3381 $$ = new Binary (Binary.Operator.Modulus,
3382 (Expression) $1, (Expression) $3);
3387 : multiplicative_expression
3388 | additive_expression PLUS multiplicative_expression
3390 $$ = new Binary (Binary.Operator.Addition,
3391 (Expression) $1, (Expression) $3);
3393 | additive_expression MINUS multiplicative_expression
3395 $$ = new Binary (Binary.Operator.Subtraction,
3396 (Expression) $1, (Expression) $3);
3401 : additive_expression
3402 | shift_expression OP_SHIFT_LEFT additive_expression
3404 $$ = new Binary (Binary.Operator.LeftShift,
3405 (Expression) $1, (Expression) $3);
3407 | shift_expression OP_SHIFT_RIGHT additive_expression
3409 $$ = new Binary (Binary.Operator.RightShift,
3410 (Expression) $1, (Expression) $3);
3414 relational_expression
3416 | relational_expression OP_LT shift_expression
3418 $$ = new Binary (Binary.Operator.LessThan,
3419 (Expression) $1, (Expression) $3);
3421 | relational_expression OP_GT shift_expression
3423 $$ = new Binary (Binary.Operator.GreaterThan,
3424 (Expression) $1, (Expression) $3);
3426 | relational_expression OP_LE shift_expression
3428 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3429 (Expression) $1, (Expression) $3);
3431 | relational_expression OP_GE shift_expression
3433 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3434 (Expression) $1, (Expression) $3);
3436 | relational_expression IS type
3438 $$ = new Is ((Expression) $1, (Expression) $3, (Location) $2);
3440 | relational_expression AS type
3442 $$ = new As ((Expression) $1, (Expression) $3, (Location) $2);
3447 : relational_expression
3448 | equality_expression OP_EQ relational_expression
3450 $$ = new Binary (Binary.Operator.Equality,
3451 (Expression) $1, (Expression) $3);
3453 | equality_expression OP_NE relational_expression
3455 $$ = new Binary (Binary.Operator.Inequality,
3456 (Expression) $1, (Expression) $3);
3461 : equality_expression
3462 | and_expression BITWISE_AND equality_expression
3464 $$ = new Binary (Binary.Operator.BitwiseAnd,
3465 (Expression) $1, (Expression) $3);
3469 exclusive_or_expression
3471 | exclusive_or_expression CARRET and_expression
3473 $$ = new Binary (Binary.Operator.ExclusiveOr,
3474 (Expression) $1, (Expression) $3);
3478 inclusive_or_expression
3479 : exclusive_or_expression
3480 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3482 $$ = new Binary (Binary.Operator.BitwiseOr,
3483 (Expression) $1, (Expression) $3);
3487 conditional_and_expression
3488 : inclusive_or_expression
3489 | conditional_and_expression OP_AND inclusive_or_expression
3491 $$ = new Binary (Binary.Operator.LogicalAnd,
3492 (Expression) $1, (Expression) $3);
3496 conditional_or_expression
3497 : conditional_and_expression
3498 | conditional_or_expression OP_OR conditional_and_expression
3500 $$ = new Binary (Binary.Operator.LogicalOr,
3501 (Expression) $1, (Expression) $3);
3505 conditional_expression
3506 : conditional_or_expression
3507 | conditional_or_expression INTERR expression COLON expression
3509 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
3513 assignment_expression
3514 : prefixed_unary_expression ASSIGN expression
3516 $$ = new Assign ((Expression) $1, (Expression) $3);
3518 | prefixed_unary_expression OP_MULT_ASSIGN expression
3520 $$ = new CompoundAssign (
3521 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
3523 | prefixed_unary_expression OP_DIV_ASSIGN expression
3525 $$ = new CompoundAssign (
3526 Binary.Operator.Division, (Expression) $1, (Expression) $3);
3528 | prefixed_unary_expression OP_MOD_ASSIGN expression
3530 $$ = new CompoundAssign (
3531 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
3533 | prefixed_unary_expression OP_ADD_ASSIGN expression
3535 $$ = new CompoundAssign (
3536 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
3538 | prefixed_unary_expression OP_SUB_ASSIGN expression
3540 $$ = new CompoundAssign (
3541 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3543 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3545 $$ = new CompoundAssign (
3546 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
3548 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3550 $$ = new CompoundAssign (
3551 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
3553 | prefixed_unary_expression OP_AND_ASSIGN expression
3555 $$ = new CompoundAssign (
3556 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
3558 | prefixed_unary_expression OP_OR_ASSIGN expression
3560 $$ = new CompoundAssign (
3561 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
3563 | prefixed_unary_expression OP_XOR_ASSIGN expression
3565 $$ = new CompoundAssign (
3566 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
3571 : conditional_expression
3572 | assignment_expression
3592 MemberName name = MakeName ((MemberName) $5);
3593 int mod_flags = (int) $2;
3595 push_current_class (new Class (
3596 current_namespace, current_class, name,
3597 mod_flags, (Attributes) $1), false, $3);
3602 if (current_class.Name == "System.Object") {
3603 Report.Error (537, current_class.Location,
3604 "The class System.Object cannot have a base " +
3605 "class or implement an interface.");
3607 current_container.AddBasesForPart (current_class, (ArrayList) $7);
3610 if (RootContext.Documentation != null) {
3611 current_container.DocComment = Lexer.consume_doc_comment ();
3612 Lexer.doc_state = XmlCommentState.Allowed;
3617 if (RootContext.Documentation != null)
3618 Lexer.doc_state = XmlCommentState.Allowed;
3622 $$ = pop_current_class ();
3630 { $$ = $1; } // location
3634 : /* empty */ { $$ = (int) 0; }
3640 | modifiers modifier
3645 if ((m1 & m2) != 0) {
3646 Location l = lexer.Location;
3647 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
3649 $$ = (int) (m1 | m2);
3654 : NEW { $$ = Modifiers.NEW; }
3655 | PUBLIC { $$ = Modifiers.PUBLIC; }
3656 | PROTECTED { $$ = Modifiers.PROTECTED; }
3657 | INTERNAL { $$ = Modifiers.INTERNAL; }
3658 | PRIVATE { $$ = Modifiers.PRIVATE; }
3659 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3660 | SEALED { $$ = Modifiers.SEALED; }
3661 | STATIC { $$ = Modifiers.STATIC; }
3662 | READONLY { $$ = Modifiers.READONLY; }
3663 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3664 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3665 | EXTERN { $$ = Modifiers.EXTERN; }
3666 | VOLATILE { $$ = Modifiers.VOLATILE; }
3667 | UNSAFE { $$ = Modifiers.UNSAFE; }
3671 : /* empty */ { $$ = null; }
3672 | class_base { $$ = $1; }
3676 : COLON type_list { $$ = $2; }
3684 // A block is "contained" on the following places:
3686 // property_declaration as part of the accessor body (get/set)
3687 // operator_declaration
3688 // constructor_declaration
3689 // destructor_declaration
3690 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3695 if (parsing_anonymous_method) {
3696 top_current_block = new ToplevelBlock (
3697 current_block, current_local_parameters, null, (Location) $1);
3698 if (current_block != null)
3699 current_block.AddAnonymousChild ((ToplevelBlock) top_current_block);
3700 current_block = top_current_block;
3701 parsing_anonymous_method = false;
3702 } else if (current_block == null){
3703 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, (Location) $1);
3704 top_current_block = current_block;
3706 current_block = new Block (current_block, (Location) $1, Location.Null);
3709 opt_statement_list CLOSE_BRACE
3711 while (current_block.Implicit)
3712 current_block = current_block.Parent;
3714 current_block.SetEndLocation ((Location) $4);
3715 current_block = current_block.Parent;
3716 if (current_block == null)
3717 top_current_block = null;
3728 | statement_list statement
3732 : declaration_statement
3734 if ($1 != null && (Block) $1 != current_block){
3735 current_block.AddStatement ((Statement) $1);
3736 current_block = (Block) $1;
3739 | valid_declaration_statement
3741 current_block.AddStatement ((Statement) $1);
3746 valid_declaration_statement
3749 | expression_statement
3750 | selection_statement
3751 | iteration_statement
3755 | unchecked_statement
3763 : valid_declaration_statement
3764 | declaration_statement
3766 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3771 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3779 $$ = EmptyStatement.Value;
3786 LocatedToken lt = (LocatedToken) $1;
3787 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
3789 if (current_block.AddLabel (labeled))
3790 current_block.AddStatement (labeled);
3795 declaration_statement
3796 : local_variable_declaration SEMICOLON
3798 current_array_type = null;
3800 DictionaryEntry de = (DictionaryEntry) $1;
3801 Expression e = (Expression) de.Key;
3803 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
3807 | local_constant_declaration SEMICOLON
3809 current_array_type = null;
3811 DictionaryEntry de = (DictionaryEntry) $1;
3813 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3819 * The following is from Rhys' grammar:
3820 * > Types in local variable declarations must be recognized as
3821 * > expressions to prevent reduce/reduce errors in the grammar.
3822 * > The expressions are converted into types during semantic analysis.
3825 : primary_expression opt_rank_specifier
3827 // FIXME: Do something smart here regarding the composition of the type.
3829 // Ok, the above "primary_expression" is there to get rid of
3830 // both reduce/reduce and shift/reduces in the grammar, it should
3831 // really just be "type_name". If you use type_name, a reduce/reduce
3832 // creeps up. If you use namespace_or_type_name (which is all we need
3833 // really) two shift/reduces appear.
3836 // So the super-trick is that primary_expression
3837 // can only be either a SimpleName or a MemberAccess.
3838 // The MemberAccess case arises when you have a fully qualified type-name like :
3840 // SimpleName is when you have
3843 Expression expr = (Expression) $1;
3844 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3845 Error_ExpectingTypeName (expr);
3849 // So we extract the string corresponding to the SimpleName
3853 if ((string) $2 == "")
3856 $$ = new ComposedCast ((Expression) $1, (string) $2);
3859 | builtin_types opt_rank_specifier
3861 if ((string) $2 == "")
3864 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3868 local_variable_pointer_type
3869 : primary_expression STAR
3871 Expression expr = (Expression) $1;
3873 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3874 Error_ExpectingTypeName (expr);
3878 $$ = new ComposedCast ((Expression) $1, "*");
3880 | builtin_types STAR
3882 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3886 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
3888 | local_variable_pointer_type STAR
3890 $$ = new ComposedCast ((Expression) $1, "*");
3894 local_variable_declaration
3895 : local_variable_type variable_declarators
3898 $$ = new DictionaryEntry ($1, $2);
3902 | local_variable_pointer_type opt_rank_specifier variable_declarators
3907 if ((string) $2 == "")
3908 t = (Expression) $1;
3910 t = new ComposedCast ((Expression) $1, (string) $2);
3911 $$ = new DictionaryEntry (t, $3);
3917 local_constant_declaration
3918 : CONST local_variable_type constant_declarators
3921 $$ = new DictionaryEntry ($2, $3);
3927 expression_statement
3928 : statement_expression SEMICOLON { $$ = $1; }
3932 // We have to do the wrapping here and not in the case above,
3933 // because statement_expression is used for example in for_statement
3935 statement_expression
3938 Expression expr = (Expression) $1;
3939 ExpressionStatement s = expr as ExpressionStatement;
3941 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
3944 $$ = new StatementExpression (s);
3948 Report.Error (1002, GetLocation ($1), "Expecting `;'");
3953 object_creation_expression
3954 : object_or_delegate_creation_expression
3955 { note ("complain if this is a delegate maybe?"); }
3964 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
3967 Location l = (Location) $1;
3969 $$ = new If ((Expression) $3, (Statement) $5, l);
3971 // FIXME: location for warning should be loc property of $5.
3972 if ($5 == EmptyStatement.Value)
3973 Report.Warning (642, 3, l, "Possible mistaken empty statement");
3976 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
3977 embedded_statement ELSE embedded_statement
3979 Location l = (Location) $1;
3981 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
3983 // FIXME: location for warning should be loc property of $5 and $7.
3984 if ($5 == EmptyStatement.Value)
3985 Report.Warning (642, 3, l, "Possible mistaken empty statement");
3986 if ($7 == EmptyStatement.Value)
3987 Report.Warning (642, 3, l, "Possible mistaken empty statement");
3992 : SWITCH OPEN_PARENS
3994 if (switch_stack == null)
3995 switch_stack = new Stack (2);
3996 switch_stack.Push (current_block);
3998 expression CLOSE_PARENS
4001 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
4002 current_block = (Block) switch_stack.Pop ();
4018 Report.Warning (1522, 1, lexer.Location, "Empty switch block");
4019 $$ = new ArrayList ();
4027 ArrayList sections = new ArrayList (4);
4032 | switch_sections switch_section
4034 ArrayList sections = (ArrayList) $1;
4044 current_block = current_block.CreateSwitchBlock (lexer.Location);
4048 Block topmost = current_block;
4050 while (topmost.Implicit)
4051 topmost = topmost.Parent;
4052 $$ = new SwitchSection ((ArrayList) $1, topmost);
4059 ArrayList labels = new ArrayList (4);
4064 | switch_labels switch_label
4066 ArrayList labels = (ArrayList) ($1);
4074 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
4075 | DEFAULT COLON { $$ = new SwitchLabel (null, (Location) $1); }
4078 1523, GetLocation ($1),
4079 "The keyword case or default must precede code in switch block");
4091 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
4093 Location l = (Location) $1;
4094 $$ = new While ((Expression) $3, (Statement) $5, l);
4099 : DO embedded_statement
4100 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
4102 Location l = (Location) $1;
4104 $$ = new Do ((Statement) $2, (Expression) $5, l);
4110 opt_for_initializer SEMICOLON
4112 Block assign_block = new Block (current_block);
4113 current_block = assign_block;
4115 if ($3 is DictionaryEntry){
4116 DictionaryEntry de = (DictionaryEntry) $3;
4118 Expression type = (Expression) de.Key;
4119 ArrayList var_declarators = (ArrayList) de.Value;
4121 foreach (VariableDeclaration decl in var_declarators){
4125 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4129 Location l = lexer.Location;
4130 Expression expr = decl.expression_or_array_initializer;
4132 LocalVariableReference var;
4133 var = new LocalVariableReference (assign_block, decl.identifier, l);
4136 Assign a = new Assign (var, expr, decl.Location);
4138 assign_block.AddStatement (new StatementExpression (a));
4142 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4143 // This can be referred to as $5 below.
4149 opt_for_condition SEMICOLON
4150 opt_for_iterator CLOSE_PARENS
4153 Location l = (Location) $1;
4155 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4157 current_block.AddStatement (f);
4158 while (current_block.Implicit)
4159 current_block = current_block.Parent;
4161 current_block = current_block.Parent;
4166 : /* empty */ { $$ = EmptyStatement.Value; }
4171 : local_variable_declaration
4172 | statement_expression_list
4176 : /* empty */ { $$ = null; }
4177 | boolean_expression
4181 : /* empty */ { $$ = EmptyStatement.Value; }
4186 : statement_expression_list
4189 statement_expression_list
4190 : statement_expression
4192 // CHANGE: was `null'
4193 Statement s = (Statement) $1;
4194 Block b = new Block (current_block, Block.Flags.Implicit, s.loc, lexer.Location);
4199 | statement_expression_list COMMA statement_expression
4201 Block b = (Block) $1;
4203 b.AddStatement ((Statement) $3);
4209 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4211 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4214 | FOREACH OPEN_PARENS type IDENTIFIER IN
4215 expression CLOSE_PARENS
4217 Block foreach_block = new Block (current_block);
4218 current_block = foreach_block;
4220 LocatedToken lt = (LocatedToken) $4;
4221 Location l = lt.Location;
4224 vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4226 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4228 // Get a writable reference to this read-only variable.
4230 // Note that the $$ here refers to the value of _this_ code block,
4231 // not the value of the LHS non-terminal. This can be referred to as $8 below.
4232 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
4239 LocalVariableReference v = (LocalVariableReference) $8;
4240 Location l = (Location) $1;
4243 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
4244 current_block.AddStatement (f);
4247 while (current_block.Implicit)
4248 current_block = current_block.Parent;
4250 current_block = current_block.Parent;
4256 | continue_statement
4266 $$ = new Break ((Location) $1);
4271 : CONTINUE SEMICOLON
4273 $$ = new Continue ((Location) $1);
4278 : GOTO IDENTIFIER SEMICOLON
4280 LocatedToken lt = (LocatedToken) $2;
4281 $$ = new Goto (lt.Value, lt.Location);
4283 | GOTO CASE constant_expression SEMICOLON
4285 $$ = new GotoCase ((Expression) $3, (Location) $1);
4287 | GOTO DEFAULT SEMICOLON
4289 $$ = new GotoDefault ((Location) $1);
4294 : RETURN opt_expression SEMICOLON
4296 $$ = new Return ((Expression) $2, (Location) $1);
4301 : THROW opt_expression SEMICOLON
4303 $$ = new Throw ((Expression) $2, (Location) $1);
4308 : IDENTIFIER RETURN expression SEMICOLON
4310 LocatedToken lt = (LocatedToken) $1;
4311 string s = lt.Value;
4313 Report.Error (1003, lt.Location, "; expected");
4316 if (RootContext.Version == LanguageVersion.ISO_1){
4317 Report.FeatureIsNotISO1 (lt.Location, "yield statement");
4320 if (anonymous_host == null){
4321 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4324 anonymous_host.SetYields ();
4325 $$ = new Yield ((Expression) $3, lt.Location);
4328 | IDENTIFIER RETURN SEMICOLON
4330 Report.Error (1627, (Location) $2, "Expression expected after yield return");
4333 | IDENTIFIER BREAK SEMICOLON
4335 LocatedToken lt = (LocatedToken) $1;
4336 string s = lt.Value;
4338 Report.Error (1003, lt.Location, "; expected");
4341 if (RootContext.Version == LanguageVersion.ISO_1){
4342 Report.FeatureIsNotISO1 (lt.Location, "yield statement");
4345 if (anonymous_host == null){
4346 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4349 anonymous_host.SetYields ();
4350 $$ = new YieldBreak (lt.Location);
4361 : TRY block catch_clauses
4365 ArrayList c = (ArrayList)$3;
4366 for (int i = 0; i < c.Count; ++i) {
4367 Catch cc = (Catch) c [i];
4369 if (i != c.Count - 1)
4370 Report.Error (1017, cc.loc, "Try statement already has an empty catch block");
4377 // Now s contains the list of specific catch clauses
4378 // and g contains the general one.
4380 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4382 | TRY block opt_catch_clauses FINALLY block
4385 ArrayList s = new ArrayList (4);
4386 ArrayList catch_list = (ArrayList) $3;
4388 if (catch_list != null){
4389 foreach (Catch cc in catch_list) {
4397 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4401 Report.Error (1524, (Location) $1, "Expected catch or finally");
4407 : /* empty */ { $$ = null; }
4414 ArrayList l = new ArrayList (4);
4419 | catch_clauses catch_clause
4421 ArrayList l = (ArrayList) $1;
4429 : /* empty */ { $$ = null; }
4434 : CATCH opt_catch_args
4436 Expression type = null;
4439 DictionaryEntry cc = (DictionaryEntry) $2;
4440 type = (Expression) cc.Key;
4441 LocatedToken lt = (LocatedToken) cc.Value;
4444 ArrayList one = new ArrayList (4);
4446 one.Add (new VariableDeclaration (lt, null));
4448 current_block = new Block (current_block);
4449 Block b = declare_local_variables (type, one, lt.Location);
4454 Expression type = null;
4456 Block var_block = null;
4459 DictionaryEntry cc = (DictionaryEntry) $2;
4460 type = (Expression) cc.Key;
4461 LocatedToken lt = (LocatedToken) cc.Value;
4465 while (current_block.Implicit)
4466 current_block = current_block.Parent;
4467 var_block = current_block;
4468 current_block = current_block.Parent;
4472 $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
4477 : /* empty */ { $$ = null; }
4482 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4484 $$ = new DictionaryEntry ($2, $3);
4492 $$ = new Checked ((Block) $2);
4499 $$ = new Unchecked ((Block) $2);
4506 RootContext.CheckUnsafeOption ((Location) $1);
4508 $$ = new Unsafe ((Block) $3);
4514 type fixed_pointer_declarators
4517 ArrayList list = (ArrayList) $4;
4518 Expression type = (Expression) $3;
4519 Location l = (Location) $1;
4520 int top = list.Count;
4522 Block assign_block = new Block (current_block);
4523 current_block = assign_block;
4525 for (int i = 0; i < top; i++){
4526 Pair p = (Pair) list [i];
4529 v = current_block.AddVariable (type, (string) p.First, l);
4533 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4541 Location l = (Location) $1;
4543 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4545 current_block.AddStatement (f);
4546 while (current_block.Implicit)
4547 current_block = current_block.Parent;
4549 current_block = current_block.Parent;
4553 fixed_pointer_declarators
4554 : fixed_pointer_declarator {
4555 ArrayList declarators = new ArrayList (4);
4557 declarators.Add ($1);
4560 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4562 ArrayList declarators = (ArrayList) $1;
4564 declarators.Add ($3);
4569 fixed_pointer_declarator
4570 : IDENTIFIER ASSIGN expression
4572 LocatedToken lt = (LocatedToken) $1;
4573 // FIXME: keep location
4574 $$ = new Pair (lt.Value, $3);
4578 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
4584 : LOCK OPEN_PARENS expression CLOSE_PARENS
4590 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
4595 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4597 Block assign_block = new Block (current_block);
4598 current_block = assign_block;
4600 if ($3 is DictionaryEntry){
4601 DictionaryEntry de = (DictionaryEntry) $3;
4602 Location l = (Location) $1;
4604 Expression type = (Expression) de.Key;
4605 ArrayList var_declarators = (ArrayList) de.Value;
4607 ArrayList vars = new ArrayList (4);
4609 foreach (VariableDeclaration decl in var_declarators){
4611 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4614 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
4616 Expression expr = decl.expression_or_array_initializer;
4618 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4621 LocalVariableReference var;
4623 // Get a writable reference to this read-only variable.
4624 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4626 // This is so that it is not a warning on using variables
4629 vars.Add (new DictionaryEntry (var, expr));
4631 // Assign a = new Assign (var, expr, decl.Location);
4632 // assign_block.AddStatement (new StatementExpression (a));
4635 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4636 // It can be referred to as $5 below.
4637 $$ = new DictionaryEntry (type, vars);
4644 Using u = new Using ($5, (Statement) $6, (Location) $1);
4645 current_block.AddStatement (u);
4646 while (current_block.Implicit)
4647 current_block = current_block.Parent;
4649 current_block = current_block.Parent;
4653 resource_acquisition
4654 : local_variable_declaration
4661 // A class used to pass around variable declarations and constants
4663 public class VariableDeclaration {
4664 public string identifier;
4665 public Expression expression_or_array_initializer;
4666 public Location Location;
4667 public Attributes OptAttributes;
4668 public string DocComment;
4670 public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
4672 this.identifier = lt.Value;
4673 if (eoai is ArrayList) {
4674 if (CSharpParser.current_array_type == null)
4675 Report.Error (622, lt.Location,
4676 "Can only use array initializer expressions to assign to array types. Try using a new expression instead.");
4677 this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
4679 this.expression_or_array_initializer = (Expression)eoai;
4681 this.Location = lt.Location;
4682 this.OptAttributes = opt_attrs;
4685 public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
4691 // A class used to hold info about an indexer declarator
4693 public class IndexerDeclaration {
4694 public Expression type;
4695 public MemberName interface_type;
4696 public Parameters param_list;
4697 public Location location;
4699 public IndexerDeclaration (Expression type, MemberName interface_type,
4700 Parameters param_list, Location loc)
4703 this.interface_type = interface_type;
4704 this.param_list = param_list;
4705 this.location = loc;
4710 // We use this when we do not have an object in advance that is an IAnonymousHost
4712 public class SimpleAnonymousHost : IAnonymousHost {
4713 public static readonly SimpleAnonymousHost Simple = new SimpleAnonymousHost ();
4716 ArrayList anonymous_methods;
4718 public static SimpleAnonymousHost GetSimple () {
4719 Simple.yields = false;
4720 Simple.anonymous_methods = null;
4724 public void SetYields ()
4729 public void AddAnonymousMethod (AnonymousMethodExpression anonymous)
4731 if (anonymous_methods == null)
4732 anonymous_methods = new ArrayList ();
4733 anonymous_methods.Add (anonymous);
4736 public void Propagate (IAnonymousHost real_host)
4739 real_host.SetYields ();
4740 if (anonymous_methods != null) {
4741 foreach (AnonymousMethodExpression ame in anonymous_methods)
4742 real_host.AddAnonymousMethod (ame);
4748 // A class used to hold info about an operator declarator
4750 public class OperatorDeclaration {
4751 public Operator.OpType optype;
4752 public Expression ret_type, arg1type, arg2type;
4753 public string arg1name, arg2name;
4754 public Location location;
4756 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4757 Expression arg1type, string arg1name,
4758 Expression arg2type, string arg2name, Location location)
4761 this.ret_type = ret_type;
4762 this.arg1type = arg1type;
4763 this.arg1name = arg1name;
4764 this.arg2type = arg2type;
4765 this.arg2name = arg2name;
4766 this.location = location;
4771 void Error_ExpectingTypeName (Expression expr)
4773 if (expr is Invocation){
4774 Report.Error (1002, expr.Location, "Expecting `;'");
4776 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
4780 public static void Error_ParameterModifierNotValid (Location loc)
4782 Report.Error (631, loc, "The modifiers `ref' and `out' are not valid in this context");
4785 static void Error_DuplicateParameterModifier (Location loc, Parameter.Modifier mod)
4787 Report.Error (1107, loc, "Duplicate parameter modifier `{0}'",
4788 Parameter.GetModifierSignature (mod));
4791 void push_current_class (TypeContainer tc, bool is_interface, object partial_token)
4793 if (partial_token != null)
4794 current_container = current_container.AddPartial (tc, is_interface);
4796 current_container = current_container.AddTypeContainer (tc, is_interface);
4800 DeclSpace pop_current_class ()
4802 DeclSpace retval = current_class;
4804 current_class = current_class.Parent;
4805 current_container = current_class.PartialContainer;
4811 // Given the @class_name name, it creates a fully qualified name
4812 // based on the containing declaration space
4815 MakeName (MemberName class_name)
4817 Namespace ns = current_namespace.NS;
4819 if (current_container.Name.Length == 0){
4820 if (ns.Name.Length != 0)
4821 return new MemberName (ns.MemberName, class_name);
4825 return new MemberName (current_container.MemberName, class_name);
4829 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4831 Block implicit_block;
4832 ArrayList inits = null;
4835 // We use the `Used' property to check whether statements
4836 // have been added to the current block. If so, we need
4837 // to create another block to contain the new declaration
4838 // otherwise, as an optimization, we use the same block to
4839 // add the declaration.
4841 // FIXME: A further optimization is to check if the statements
4842 // that were added were added as part of the initialization
4843 // below. In which case, no other statements have been executed
4844 // and we might be able to reduce the number of blocks for
4845 // situations like this:
4847 // int j = 1; int k = j + 1;
4849 if (current_block.Used)
4850 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4852 implicit_block = current_block;
4854 foreach (VariableDeclaration decl in variable_declarators){
4856 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
4857 if (decl.expression_or_array_initializer != null){
4859 inits = new ArrayList (4);
4866 return implicit_block;
4868 foreach (VariableDeclaration decl in inits){
4870 Expression expr = decl.expression_or_array_initializer;
4872 LocalVariableReference var;
4873 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4875 assign = new Assign (var, expr, decl.Location);
4877 implicit_block.AddStatement (new StatementExpression (assign));
4880 return implicit_block;
4883 Block declare_local_constants (Expression type, ArrayList declarators)
4885 Block implicit_block;
4887 if (current_block.Used)
4888 implicit_block = new Block (current_block, Block.Flags.Implicit);
4890 implicit_block = current_block;
4892 foreach (VariableDeclaration decl in declarators){
4893 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
4896 return implicit_block;
4899 void CheckAttributeTarget (string a, Location l)
4903 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4907 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4913 void CheckUnaryOperator (Operator.OpType op, Location l)
4917 case Operator.OpType.LogicalNot:
4918 case Operator.OpType.OnesComplement:
4919 case Operator.OpType.Increment:
4920 case Operator.OpType.Decrement:
4921 case Operator.OpType.True:
4922 case Operator.OpType.False:
4923 case Operator.OpType.Addition:
4924 case Operator.OpType.Subtraction:
4929 Report.Error (1019, l, "Overloadable unary operator expected");
4935 void CheckBinaryOperator (Operator.OpType op, Location l)
4939 case Operator.OpType.Addition:
4940 case Operator.OpType.Subtraction:
4941 case Operator.OpType.Multiply:
4942 case Operator.OpType.Division:
4943 case Operator.OpType.Modulus:
4944 case Operator.OpType.BitwiseAnd:
4945 case Operator.OpType.BitwiseOr:
4946 case Operator.OpType.ExclusiveOr:
4947 case Operator.OpType.LeftShift:
4948 case Operator.OpType.RightShift:
4949 case Operator.OpType.Equality:
4950 case Operator.OpType.Inequality:
4951 case Operator.OpType.GreaterThan:
4952 case Operator.OpType.LessThan:
4953 case Operator.OpType.GreaterThanOrEqual:
4954 case Operator.OpType.LessThanOrEqual:
4958 Report.Error (1020, l, "Overloadable binary operator expected");
4964 void syntax_error (Location l, string msg)
4966 Report.Error (1003, l, "Syntax error, " + msg);
4969 void note (string s)
4971 // Used to put annotations
4976 public Tokenizer Lexer {
4982 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4984 this.name = file.Name;
4986 current_namespace = new NamespaceEntry (null, file, null);
4987 current_class = current_namespace.SlaveDeclSpace;
4988 current_container = current_class.PartialContainer; // == RootContest.ToplevelTypes
4990 lexer = new Tokenizer (reader, file, defines);
4993 public void parse ()
4995 int errors = Report.Errors;
4997 if (yacc_verbose_flag > 1)
4998 yyparse (lexer, new yydebug.yyDebugSimple ());
5001 Tokenizer tokenizer = lexer as Tokenizer;
5002 tokenizer.cleanup ();
5003 } catch (Exception e){
5005 // Removed for production use, use parser verbose to get the output.
5007 // Console.WriteLine (e);
5008 if (Report.Errors == errors)
5009 Report.Error (-25, lexer.Location, "Parsing error");
5010 if (yacc_verbose_flag > 0)
5011 Console.WriteLine (e);
5014 if (RootContext.ToplevelTypes.NamespaceEntry != null)
5015 throw new InternalErrorException ("who set it?");
5018 static void CheckToken (int error, int yyToken, string msg, Location loc)
5020 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
5021 Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ());
5023 Report.Error (error, loc, msg);
5026 void CheckIdentifierToken (int yyToken, Location loc)
5028 CheckToken (1041, yyToken, "Identifier expected", loc);
5031 string ConsumeStoredComment ()
5033 string s = tmpComment;
5035 Lexer.doc_state = XmlCommentState.Allowed;
5039 Location GetLocation (object obj)
5041 if (obj is MemberCore)
5042 return ((MemberCore) obj).Location;
5043 if (obj is MemberName)
5044 return ((MemberName) obj).Location;
5045 if (obj is LocatedToken)
5046 return ((LocatedToken) obj).Location;
5047 if (obj is Location)
5048 return (Location) obj;
5049 return lexer.Location;