3 // cs-parser.jay: The Parser for the C# compiler
5 // Authors: Miguel de Icaza (miguel@gnu.org)
6 // Ravi Pratap (ravi@ximian.com)
8 // Licensed under the terms of the GNU GPL
10 // (C) 2001 Ximian, Inc (http://www.ximian.com)
11 // (C) 2004 Novell, Inc
14 // (1) Figure out why error productions dont work. `type-declaration' is a
15 // great spot to put an `error' because you can reproduce it with this input:
18 // Possible optimization:
19 // Run memory profiler with parsing only, and consider dropping
20 // arraylists where not needed. Some pieces can use linked lists.
28 using System.Collections;
33 public class CSharpParser {
34 NamespaceEntry current_namespace;
35 TypeContainer current_container;
36 TypeContainer current_class;
38 IIteratorContainer iterator_container;
41 /// Current block is used to add statements as we find
44 Block current_block, top_current_block;
46 Delegate current_delegate;
49 /// This is used by the unary_expression code to resolve
50 /// a name against a parameter.
52 Parameters current_local_parameters;
55 /// Using during property parsing to describe the implicit
56 /// value parameter that is passed to the "set" and "get"accesor
57 /// methods (properties and indexers).
59 Expression implicit_value_parameter_type;
60 Parameters indexer_parameters;
63 /// Hack to help create non-typed array initializer
65 public static Expression current_array_type;
68 /// Used to determine if we are parsing the get/set pair
69 /// of an indexer or a property
74 /// An out-of-band stack.
83 static public int yacc_verbose_flag;
85 // Name of the file we are parsing
94 /// Temporary Xml documentation cache.
95 /// For enum types, we need one more temporary store.
98 string enumTypeComment;
100 /// Current attribute target
101 string current_attr_target;
103 /// assembly and module attribute definitions are enabled
104 bool global_attrs_enabled = true;
105 bool has_get, has_set;
110 %token NONE /* This token is never returned by our lexer */
111 %token ERROR // This is used not by the parser, but by the tokenizer.
115 *These are the C# keywords
202 /* C# keywords which are not really keywords */
208 /* C# single character operators/punctuation. */
209 %token OPEN_BRACE "{"
210 %token CLOSE_BRACE "}"
211 %token OPEN_BRACKET "["
212 %token CLOSE_BRACKET "]"
213 %token OPEN_PARENS "("
214 %token CLOSE_PARENS ")"
226 %token OP_GENERICS_LT "<"
228 %token OP_GENERICS_GT ">"
229 %token BITWISE_AND "&"
230 %token BITWISE_OR "|"
237 /* C# multi-character operators. */
238 %token DOUBLE_COLON "::"
241 %token OP_SHIFT_LEFT "<<"
242 %token OP_SHIFT_RIGHT ">>"
249 %token OP_MULT_ASSIGN "*="
250 %token OP_DIV_ASSIGN "/="
251 %token OP_MOD_ASSIGN "%="
252 %token OP_ADD_ASSIGN "+="
253 %token OP_SUB_ASSIGN "-="
254 %token OP_SHIFT_LEFT_ASSIGN "<<="
255 %token OP_SHIFT_RIGHT_ASSIGN ">>="
256 %token OP_AND_ASSIGN "&="
257 %token OP_XOR_ASSIGN "^="
258 %token OP_OR_ASSIGN "|="
262 %token LITERAL_INTEGER "int literal"
263 %token LITERAL_FLOAT "float literal"
264 %token LITERAL_DOUBLE "double literal"
265 %token LITERAL_DECIMAL "decimal literal"
266 %token LITERAL_CHARACTER "character literal"
267 %token LITERAL_STRING "string literal"
270 %token CLOSE_PARENS_CAST
271 %token CLOSE_PARENS_NO_CAST
272 %token CLOSE_PARENS_OPEN_PARENS
273 %token CLOSE_PARENS_MINUS
274 %token DEFAULT_OPEN_PARENS
275 %token GENERIC_DIMENSION
277 /* Add precedence rules to solve dangling else s/r conflict */
286 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
288 %left STAR DIV PERCENT
289 %right BANG CARRET UMINUS
290 %nonassoc OP_INC OP_DEC
292 %left OPEN_BRACKET OPEN_BRACE
296 %start compilation_unit
300 : outer_declarations opt_EOF
301 | outer_declarations global_attributes opt_EOF
302 | global_attributes opt_EOF
303 | opt_EOF /* allow empty files */
309 Lexer.check_incorrect_doc_comment ();
313 Lexer.check_incorrect_doc_comment ();
319 | outer_declarations outer_declaration
323 : extern_alias_directive
325 | namespace_member_declaration
328 extern_alias_directives
329 : extern_alias_directive
330 | extern_alias_directives extern_alias_directive;
332 extern_alias_directive
333 : EXTERN IDENTIFIER IDENTIFIER SEMICOLON
335 LocatedToken lt = (LocatedToken) $2;
338 Report.Error (1003, lt.Location, "'alias' expected");
339 } else if (RootContext.Version == LanguageVersion.ISO_1) {
340 Report.FeatureIsNotStandardized (lt.Location, "external alias");
342 lt = (LocatedToken) $3;
343 current_namespace.UsingExternalAlias (lt.Value, lt.Location);
350 | using_directives using_directive
354 : using_alias_directive
356 if (RootContext.Documentation != null)
357 Lexer.doc_state = XmlCommentState.Allowed;
359 | using_namespace_directive
361 if (RootContext.Documentation != null)
362 Lexer.doc_state = XmlCommentState.Allowed;
366 using_alias_directive
367 : USING IDENTIFIER ASSIGN
368 namespace_or_type_name SEMICOLON
370 LocatedToken lt = (LocatedToken) $2;
371 current_namespace.UsingAlias (lt.Value, (MemberName) $4, (Location) $1);
374 CheckIdentifierToken (yyToken, GetLocation ($2));
378 using_namespace_directive
379 : USING namespace_name SEMICOLON
381 current_namespace.Using ((MemberName) $2, (Location) $1);
386 // Strictly speaking, namespaces don't have attributes but
387 // we parse global attributes along with namespace declarations and then
390 namespace_declaration
391 : opt_attributes NAMESPACE namespace_or_type_name
393 MemberName name = (MemberName) $3;
396 Report.Error(1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
399 if (name.TypeArguments != null)
400 syntax_error (lexer.Location, "namespace name expected");
402 current_namespace = new NamespaceEntry (
403 current_namespace, file, name.GetName ());
405 namespace_body opt_semicolon
407 current_namespace = current_namespace.Parent;
422 : namespace_or_type_name {
423 MemberName name = (MemberName) $1;
425 if (name.TypeArguments != null)
426 syntax_error (lexer.Location, "namespace name expected");
435 if (RootContext.Documentation != null)
436 Lexer.doc_state = XmlCommentState.Allowed;
438 opt_extern_alias_directives
440 opt_namespace_member_declarations
449 opt_extern_alias_directives
451 | extern_alias_directives
454 opt_namespace_member_declarations
456 | namespace_member_declarations
459 namespace_member_declarations
460 : namespace_member_declaration
461 | namespace_member_declarations namespace_member_declaration
464 namespace_member_declaration
468 DeclSpace ds = (DeclSpace)$1;
470 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
471 Report.Error (1527, ds.Location,
472 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
475 current_namespace.DeclarationFound = true;
477 | namespace_declaration {
478 current_namespace.DeclarationFound = true;
481 | field_declaration {
482 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
484 | method_declaration {
485 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
492 | interface_declaration
494 | delegate_declaration
496 // Enable this when we have handled all errors, because this acts as a generic fallback
499 // Console.WriteLine ("Token=" + yyToken);
500 // Report.Error (1518, GetLocation ($1), "Expected class, struct, interface, enum or delegate");
512 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
520 global_attrs_enabled = false;
525 global_attrs_enabled = false;
534 ArrayList sect = (ArrayList) $1;
536 if (global_attrs_enabled) {
537 if (current_attr_target == "module") {
538 CodeGen.Module.AddAttributes (sect);
540 } else if (current_attr_target != null && current_attr_target.Length > 0) {
541 CodeGen.Assembly.AddAttributes (sect);
544 $$ = new Attributes (sect);
547 if (RootContext.Documentation != null) {
548 Lexer.check_incorrect_doc_comment ();
550 XmlCommentState.Allowed;
554 $$ = new Attributes (sect);
556 current_attr_target = null;
558 | attribute_sections attribute_section
560 Attributes attrs = $1 as Attributes;
561 ArrayList sect = (ArrayList) $2;
563 if (global_attrs_enabled) {
564 if (current_attr_target == "module") {
565 CodeGen.Module.AddAttributes (sect);
567 } else if (current_attr_target == "assembly") {
568 CodeGen.Assembly.AddAttributes (sect);
572 attrs = new Attributes (sect);
574 attrs.AddAttributes (sect);
578 attrs = new Attributes (sect);
580 attrs.AddAttributes (sect);
583 current_attr_target = null;
588 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
592 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
598 attribute_target_specifier
599 : attribute_target COLON
601 current_attr_target = (string)$1;
609 LocatedToken lt = (LocatedToken) $1;
610 CheckAttributeTarget (lt.Value, lt.Location);
611 $$ = lt.Value; // Location won't be required anymore.
613 | EVENT { $$ = "event"; }
614 | RETURN { $$ = "return"; }
620 ArrayList attrs = new ArrayList (4);
626 | attribute_list COMMA attribute
628 ArrayList attrs = (ArrayList) $1;
636 : attribute_name opt_attribute_arguments
638 MemberName mname = (MemberName) $1;
639 if (mname.IsGeneric) {
640 Report.Error (404, lexer.Location,
641 "'<' unexpected: attributes cannot be generic");
644 object [] arguments = (object []) $2;
645 MemberName left = mname.Left;
646 string identifier = mname.Name;
648 Expression left_expr = left == null ? null : left.GetTypeExpression ();
650 if (current_attr_target == "assembly" || current_attr_target == "module")
651 // FIXME: supply "nameEscaped" parameter here.
652 $$ = new GlobalAttribute (current_namespace, current_attr_target,
653 left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
655 $$ = new Attribute (current_attr_target, left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
660 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
663 opt_attribute_arguments
664 : /* empty */ { $$ = null; }
665 | OPEN_PARENS attribute_arguments CLOSE_PARENS
673 : opt_positional_argument_list
678 $$ = new object [] { $1, null };
681 | positional_argument_list COMMA named_argument_list
683 $$ = new object[] { $1, $3 };
685 | named_argument_list
687 $$ = new object [] { null, $1 };
692 opt_positional_argument_list
693 : /* empty */ { $$ = null; }
694 | positional_argument_list
697 positional_argument_list
700 ArrayList args = new ArrayList (4);
701 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
705 | positional_argument_list COMMA expression
707 ArrayList args = (ArrayList) $1;
708 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
717 ArrayList args = new ArrayList (4);
722 | named_argument_list COMMA named_argument
724 ArrayList args = (ArrayList) $1;
729 | named_argument_list COMMA expression
731 Report.Error (1016, ((Expression) $3).Location, "Named attribute argument expected");
737 : IDENTIFIER ASSIGN expression
739 // FIXME: keep location
740 $$ = new DictionaryEntry (
741 ((LocatedToken) $1).Value,
742 new Argument ((Expression) $3, Argument.AType.Expression));
748 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
751 opt_class_member_declarations
753 | class_member_declarations
756 class_member_declarations
757 : class_member_declaration
758 | class_member_declarations
759 class_member_declaration
762 class_member_declaration
763 : constant_declaration // done
764 | field_declaration // done
765 | method_declaration // done
766 | property_declaration // done
767 | event_declaration // done
768 | indexer_declaration // done
769 | operator_declaration // done
770 | constructor_declaration // done
771 | destructor_declaration // done
781 lexer.ConstraintsParsing = true;
785 MemberName name = MakeName ((MemberName) $6);
786 current_class = new Struct (
787 current_namespace, current_class, name, (int) $2,
791 current_container = current_container.AddPartial (current_class);
793 current_container.AddClassOrStruct (current_class);
794 current_container = current_class;
798 opt_type_parameter_constraints_clauses
800 lexer.ConstraintsParsing = false;
803 current_class.Bases = (ArrayList) $8;
805 current_class.SetParameterInfo ((ArrayList) $9);
807 if (RootContext.Documentation != null)
808 current_container.DocComment = Lexer.consume_doc_comment ();
812 if (RootContext.Documentation != null)
813 Lexer.doc_state = XmlCommentState.Allowed;
817 $$ = pop_current_class ();
819 | opt_attributes opt_modifiers opt_partial STRUCT error {
820 CheckIdentifierToken (yyToken, GetLocation ($5));
827 if (RootContext.Documentation != null)
828 Lexer.doc_state = XmlCommentState.Allowed;
830 opt_struct_member_declarations CLOSE_BRACE
833 opt_struct_member_declarations
835 | struct_member_declarations
838 struct_member_declarations
839 : struct_member_declaration
840 | struct_member_declarations struct_member_declaration
843 struct_member_declaration
844 : constant_declaration
847 | property_declaration
849 | indexer_declaration
850 | operator_declaration
851 | constructor_declaration
855 * This is only included so we can flag error 575:
856 * destructors only allowed on class types
858 | destructor_declaration
869 int modflags = (int) $2;
870 foreach (VariableDeclaration constant in (ArrayList) $5){
871 Location l = constant.Location;
872 if ((modflags & Modifiers.STATIC) != 0) {
873 Report.Error (504, l, "The constant `{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
877 Const c = new Const (
878 current_class, (Expression) $4, (string) constant.identifier,
879 (Expression) constant.expression_or_array_initializer, modflags,
882 if (RootContext.Documentation != null) {
883 c.DocComment = Lexer.consume_doc_comment ();
884 Lexer.doc_state = XmlCommentState.Allowed;
886 current_container.AddConstant (c);
892 : constant_declarator
894 ArrayList constants = new ArrayList (4);
899 | constant_declarators COMMA constant_declarator
902 ArrayList constants = (ArrayList) $1;
909 : IDENTIFIER ASSIGN constant_expression
911 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
915 // A const field requires a value to be provided
916 Report.Error (145, ((LocatedToken) $1).Location, "A const field requires a value to be provided");
928 Expression type = (Expression) $3;
931 current_array_type = null;
933 foreach (VariableDeclaration var in (ArrayList) $4){
934 Field field = new Field (current_class, type, mod, var.identifier,
935 (Attributes) $1, var.Location);
937 field.Initializer = var.expression_or_array_initializer;
939 if (RootContext.Documentation != null) {
940 field.DocComment = Lexer.consume_doc_comment ();
941 Lexer.doc_state = XmlCommentState.Allowed;
943 current_container.AddField (field);
944 $$ = field; // FIXME: might be better if it points to the top item
951 fixed_variable_declarators
954 Expression type = (Expression) $4;
957 current_array_type = null;
959 foreach (VariableDeclaration var in (ArrayList) $5) {
960 FixedField field = new FixedField (current_class, type, mod, var.identifier,
961 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
963 if (RootContext.Documentation != null) {
964 field.DocComment = Lexer.consume_doc_comment ();
965 Lexer.doc_state = XmlCommentState.Allowed;
967 current_container.AddField (field);
968 $$ = field; // FIXME: might be better if it points to the top item
976 current_array_type = null;
977 Report.Error (670, (Location) $3, "Fields cannot have void type");
981 fixed_variable_declarators
982 : fixed_variable_declarator
984 ArrayList decl = new ArrayList (2);
988 | fixed_variable_declarators COMMA fixed_variable_declarator
990 ArrayList decls = (ArrayList) $1;
996 fixed_variable_declarator
997 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
999 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1001 | IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1003 Report.Error (443, lexer.Location, "Value or constant expected");
1004 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1008 variable_declarators
1009 : variable_declarator
1011 ArrayList decl = new ArrayList (4);
1016 | variable_declarators COMMA variable_declarator
1018 ArrayList decls = (ArrayList) $1;
1025 : IDENTIFIER ASSIGN variable_initializer
1027 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1031 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1033 | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
1035 Report.Error (650, ((LocatedToken) $1).Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
1036 "To declare a fixed size buffer field, use the fixed keyword before the field type");
1041 variable_initializer
1050 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
1052 $$ = new StackAlloc ((Expression) $2, (Expression) $4, (Location) $1);
1056 $$ = new ArglistAccess ((Location) $1);
1060 Report.Error (1575, (Location) $1, "A stackalloc expression requires [] after type");
1067 iterator_container = (IIteratorContainer) $1;
1068 if (RootContext.Documentation != null)
1069 Lexer.doc_state = XmlCommentState.NotAllowed;
1073 Method method = (Method) $1;
1074 method.Block = (ToplevelBlock) $3;
1075 current_container.AddMethod (method);
1077 current_local_parameters = null;
1078 iterator_container = null;
1080 if (RootContext.Documentation != null)
1081 Lexer.doc_state = XmlCommentState.Allowed;
1094 Report.Error (1585, lexer.Location,
1095 "Member modifier `{0}' must precede the member type and name",
1096 Modifiers.Name (i));
1107 type namespace_or_type_name
1108 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1110 lexer.ConstraintsParsing = true;
1112 opt_type_parameter_constraints_clauses
1114 lexer.ConstraintsParsing = false;
1116 MemberName name = (MemberName) $4;
1118 if ($9 != null && name.TypeArguments == null)
1119 Report.Error (80, lexer.Location,
1120 "Constraints are not allowed on non-generic declarations");
1124 GenericMethod generic = null;
1125 if (name.TypeArguments != null) {
1126 generic = new GenericMethod (current_namespace, current_class, name,
1127 (Expression) $3, (Parameters) $6);
1129 generic.SetParameterInfo ((ArrayList) $9);
1132 method = new Method (current_class, generic, (Expression) $3, (int) $2, false,
1133 name, (Parameters) $6, (Attributes) $1);
1135 current_local_parameters = (Parameters) $6;
1137 if (RootContext.Documentation != null)
1138 method.DocComment = Lexer.consume_doc_comment ();
1144 VOID namespace_or_type_name
1145 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1147 lexer.ConstraintsParsing = true;
1149 opt_type_parameter_constraints_clauses
1151 lexer.ConstraintsParsing = false;
1153 MemberName name = (MemberName) $4;
1155 if ($9 != null && name.TypeArguments == null)
1156 Report.Error (80, lexer.Location,
1157 "Constraints are not allowed on non-generic declarations");
1160 GenericMethod generic = null;
1161 if (name.TypeArguments != null) {
1162 generic = new GenericMethod (current_namespace, current_class, name,
1163 TypeManager.system_void_expr, (Parameters) $6);
1165 generic.SetParameterInfo ((ArrayList) $9);
1168 method = new Method (current_class, generic, TypeManager.system_void_expr,
1169 (int) $2, false, name, (Parameters) $6, (Attributes) $1);
1171 current_local_parameters = (Parameters) $6;
1173 if (RootContext.Documentation != null)
1174 method.DocComment = Lexer.consume_doc_comment ();
1181 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1183 MemberName name = (MemberName) $5;
1184 Report.Error (1585, name.Location,
1185 "Member modifier `{0}' must precede the member type and name", Modifiers.Name ((int) $4));
1187 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1188 0, false, name, (Parameters) $6, (Attributes) $1);
1190 current_local_parameters = (Parameters) $6;
1192 if (RootContext.Documentation != null)
1193 method.DocComment = Lexer.consume_doc_comment ();
1201 | SEMICOLON { $$ = null; }
1204 opt_formal_parameter_list
1205 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1206 | formal_parameter_list
1209 formal_parameter_list
1212 ArrayList pars_list = (ArrayList) $1;
1214 Parameter [] pars = new Parameter [pars_list.Count];
1215 pars_list.CopyTo (pars);
1217 $$ = new Parameters (pars);
1219 | fixed_parameters COMMA parameter_array
1221 ArrayList pars_list = (ArrayList) $1;
1224 Parameter [] pars = new Parameter [pars_list.Count];
1225 pars_list.CopyTo (pars);
1227 $$ = new Parameters (pars);
1229 | fixed_parameters COMMA ARGLIST
1231 ArrayList pars_list = (ArrayList) $1;
1232 //pars_list.Add (new ArglistParameter (GetLocation ($3)));
1234 Parameter [] pars = new Parameter [pars_list.Count];
1235 pars_list.CopyTo (pars);
1237 $$ = new Parameters (pars, true);
1239 | parameter_array COMMA error
1242 Report.Error (231, ((Parameter) $1).Location, "A params parameter must be the last parameter in a formal parameter list");
1245 | fixed_parameters COMMA parameter_array COMMA error
1248 Report.Error (231, ((Parameter) $3).Location, "A params parameter must be the last parameter in a formal parameter list");
1251 | ARGLIST COMMA error
1253 Report.Error (257, (Location) $1, "An __arglist parameter must be the last parameter in a formal parameter list");
1256 | fixed_parameters COMMA ARGLIST COMMA error
1258 Report.Error (257, (Location) $3, "An __arglist parameter must be the last parameter in a formal parameter list");
1263 $$ = new Parameters (new Parameter[] { (Parameter) $1 } );
1267 $$ = new Parameters (new Parameter[0], true);
1274 ArrayList pars = new ArrayList (4);
1279 | fixed_parameters COMMA fixed_parameter
1281 ArrayList pars = (ArrayList) $1;
1290 opt_parameter_modifier
1294 LocatedToken lt = (LocatedToken) $4;
1295 $$ = new Parameter ((Expression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1298 opt_parameter_modifier
1300 IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1302 LocatedToken lt = (LocatedToken) $4;
1303 Report.Error (1552, lt.Location, "Array type specifier, [], must appear before parameter name");
1307 opt_parameter_modifier
1310 Report.Error (1001, GetLocation ($3), "Identifier expected");
1314 opt_parameter_modifier
1317 CheckIdentifierToken (yyToken, GetLocation ($4));
1321 opt_parameter_modifier
1327 LocatedToken lt = (LocatedToken) $4;
1328 Report.Error (241, lt.Location, "Default parameter specifiers are not permitted");
1333 opt_parameter_modifier
1334 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1335 | parameter_modifier
1339 : REF { $$ = Parameter.Modifier.REF; }
1340 | OUT { $$ = Parameter.Modifier.OUT; }
1344 : opt_attributes PARAMS type IDENTIFIER
1346 LocatedToken lt = (LocatedToken) $4;
1347 $$ = new ParamsParameter ((Expression) $3, lt.Value, (Attributes) $1, lt.Location);
1348 note ("type must be a single-dimension array type");
1350 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1352 Report.Error (1611, (Location) $2, "The params parameter cannot be declared as ref or out");
1355 | opt_attributes PARAMS type error {
1356 CheckIdentifierToken (yyToken, GetLocation ($4));
1361 property_declaration
1365 namespace_or_type_name
1367 if (RootContext.Documentation != null)
1368 tmpComment = Lexer.consume_doc_comment ();
1372 implicit_value_parameter_type = (Expression) $3;
1374 lexer.PropertyParsing = true;
1376 accessor_declarations
1378 lexer.PropertyParsing = false;
1379 has_get = has_set = false;
1387 Pair pair = (Pair) $8;
1388 Accessor get_block = (Accessor) pair.First;
1389 Accessor set_block = (Accessor) pair.Second;
1391 MemberName name = (MemberName) $4;
1393 if (name.TypeArguments != null)
1394 syntax_error (lexer.Location, "a property can't have type arguments");
1396 prop = new Property (current_class, (Expression) $3, (int) $2, false,
1397 name, (Attributes) $1, get_block, set_block);
1399 current_container.AddProperty (prop);
1400 implicit_value_parameter_type = null;
1402 if (RootContext.Documentation != null)
1403 prop.DocComment = ConsumeStoredComment ();
1408 accessor_declarations
1409 : get_accessor_declaration
1411 $$ = new Pair ($1, null);
1413 | get_accessor_declaration accessor_declarations
1415 Pair pair = (Pair) $2;
1419 | set_accessor_declaration
1421 $$ = new Pair (null, $1);
1423 | set_accessor_declaration accessor_declarations
1425 Pair pair = (Pair) $2;
1431 Report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1436 get_accessor_declaration
1437 : opt_attributes opt_modifiers GET
1439 // If this is not the case, then current_local_parameters has already
1440 // been set in indexer_declaration
1441 if (parsing_indexer == false)
1442 current_local_parameters = null;
1444 current_local_parameters = indexer_parameters;
1445 lexer.PropertyParsing = false;
1447 iterator_container = SimpleIteratorContainer.GetSimple ();
1452 Report.Error (1007, (Location) $3, "Property accessor already defined");
1455 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1457 current_local_parameters = null;
1458 lexer.PropertyParsing = true;
1460 if (SimpleIteratorContainer.Simple.Yields)
1461 accessor.SetYields ();
1463 iterator_container = null;
1465 if (RootContext.Documentation != null)
1466 if (Lexer.doc_state == XmlCommentState.Error)
1467 Lexer.doc_state = XmlCommentState.NotAllowed;
1473 set_accessor_declaration
1474 : opt_attributes opt_modifiers SET
1477 Parameter implicit_value_parameter = new Parameter (
1478 implicit_value_parameter_type, "value",
1479 Parameter.Modifier.NONE, null, (Location) $3);
1481 if (parsing_indexer == false) {
1482 args = new Parameter [1];
1483 args [0] = implicit_value_parameter;
1484 current_local_parameters = new Parameters (args);
1486 Parameter [] fpars = indexer_parameters.FixedParameters;
1489 int count = fpars.Length;
1491 args = new Parameter [count + 1];
1492 fpars.CopyTo (args, 0);
1493 args [count] = implicit_value_parameter;
1496 current_local_parameters = new Parameters (
1500 lexer.PropertyParsing = false;
1502 iterator_container = SimpleIteratorContainer.GetSimple ();
1507 Report.Error (1007, ((LocatedToken) $3).Location, "Property accessor already defined");
1510 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1512 current_local_parameters = null;
1513 lexer.PropertyParsing = true;
1515 if (SimpleIteratorContainer.Simple.Yields)
1516 accessor.SetYields ();
1518 iterator_container = null;
1520 if (RootContext.Documentation != null
1521 && Lexer.doc_state == XmlCommentState.Error)
1522 Lexer.doc_state = XmlCommentState.NotAllowed;
1530 | SEMICOLON { $$ = null; }
1533 interface_declaration
1539 lexer.ConstraintsParsing = true;
1543 MemberName name = MakeName ((MemberName) $6);
1545 current_class = new Interface (
1546 current_namespace, current_class, name, (int) $2,
1550 current_container = current_container.AddPartial (current_class);
1552 current_container.AddInterface (current_class);
1553 current_container = current_class;
1557 opt_type_parameter_constraints_clauses
1559 lexer.ConstraintsParsing = false;
1562 current_class.Bases = (ArrayList) $8;
1564 current_class.SetParameterInfo ((ArrayList) $9);
1566 if (RootContext.Documentation != null) {
1567 current_container.DocComment = Lexer.consume_doc_comment ();
1568 Lexer.doc_state = XmlCommentState.Allowed;
1573 if (RootContext.Documentation != null)
1574 Lexer.doc_state = XmlCommentState.Allowed;
1578 $$ = pop_current_class ();
1580 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1581 CheckIdentifierToken (yyToken, GetLocation ($5));
1587 opt_interface_member_declarations
1591 opt_interface_member_declarations
1593 | interface_member_declarations
1596 interface_member_declarations
1597 : interface_member_declaration
1598 | interface_member_declarations interface_member_declaration
1601 interface_member_declaration
1602 : interface_method_declaration
1607 Method m = (Method) $1;
1609 if (m.IsExplicitImpl)
1610 Report.Error (541, m.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1611 m.GetSignatureForError ());
1613 current_container.AddMethod (m);
1615 if (RootContext.Documentation != null)
1616 Lexer.doc_state = XmlCommentState.Allowed;
1618 | interface_property_declaration
1623 Property p = (Property) $1;
1625 if (p.IsExplicitImpl)
1626 Report.Error (541, p.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1627 p.GetSignatureForError ());
1629 current_container.AddProperty (p);
1631 if (RootContext.Documentation != null)
1632 Lexer.doc_state = XmlCommentState.Allowed;
1634 | interface_event_declaration
1637 Event e = (Event) $1;
1639 if (e.IsExplicitImpl)
1640 Report.Error (541, e.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1641 e.GetSignatureForError ());
1643 current_container.AddEvent (e);
1646 if (RootContext.Documentation != null)
1647 Lexer.doc_state = XmlCommentState.Allowed;
1649 | interface_indexer_declaration
1654 Indexer i = (Indexer) $1;
1656 if (i.IsExplicitImpl)
1657 Report.Error (541, i.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1658 i.GetSignatureForError ());
1660 current_container.AddIndexer (i);
1662 if (RootContext.Documentation != null)
1663 Lexer.doc_state = XmlCommentState.Allowed;
1665 | delegate_declaration
1668 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1669 ((MemberCore)$1).GetSignatureForError ());
1675 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1676 ((MemberCore)$1).GetSignatureForError ());
1679 | struct_declaration
1682 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1683 ((MemberCore)$1).GetSignatureForError ());
1689 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1690 ((MemberCore)$1).GetSignatureForError ());
1693 | interface_declaration
1696 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1697 ((MemberCore)$1).GetSignatureForError ());
1700 | constant_declaration
1702 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1710 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, GetLocation ($1));
1715 interface_method_declaration_body
1718 lexer.ConstraintsParsing = false;
1720 opt_statement_list CLOSE_BRACE
1722 Report.Error (531, lexer.Location,
1723 "'{0}': interface members cannot have a definition", ((MemberName) $-1).ToString ());
1729 interface_method_declaration
1730 : opt_attributes opt_new type namespace_or_type_name
1731 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1733 lexer.ConstraintsParsing = true;
1735 opt_type_parameter_constraints_clauses
1737 // Refer to the name as $-1 in interface_method_declaration_body
1740 interface_method_declaration_body
1742 lexer.ConstraintsParsing = false;
1744 MemberName name = (MemberName) $4;
1746 if ($9 != null && name.TypeArguments == null)
1747 Report.Error (80, lexer.Location,
1748 "Constraints are not allowed on non-generic declarations");
1750 GenericMethod generic = null;
1751 if (name.TypeArguments != null) {
1752 generic = new GenericMethod (current_namespace, current_class, name,
1753 (Expression) $3, (Parameters) $6);
1755 generic.SetParameterInfo ((ArrayList) $9);
1758 $$ = new Method (current_class, generic, (Expression) $3, (int) $2, true, name,
1759 (Parameters) $6, (Attributes) $1);
1760 if (RootContext.Documentation != null)
1761 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1763 | opt_attributes opt_new VOID namespace_or_type_name
1764 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1766 lexer.ConstraintsParsing = true;
1768 opt_type_parameter_constraints_clauses
1772 interface_method_declaration_body
1774 lexer.ConstraintsParsing = false;
1776 MemberName name = (MemberName) $4;
1778 if ($9 != null && name.TypeArguments == null)
1779 Report.Error (80, lexer.Location,
1780 "Constraints are not allowed on non-generic declarations");
1782 GenericMethod generic = null;
1783 if (name.TypeArguments != null) {
1784 generic = new GenericMethod (current_namespace, current_class, name,
1785 TypeManager.system_void_expr, (Parameters) $6);
1787 generic.SetParameterInfo ((ArrayList) $9);
1790 $$ = new Method (current_class, generic, TypeManager.system_void_expr, (int) $2,
1791 true, name, (Parameters) $6, (Attributes) $1);
1792 if (RootContext.Documentation != null)
1793 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1797 interface_property_declaration
1802 { lexer.PropertyParsing = true; }
1803 accessor_declarations
1805 has_get = has_set = false;
1806 lexer.PropertyParsing = false;
1810 LocatedToken lt = (LocatedToken) $4;
1811 MemberName name = new MemberName (lt.Value, lt.Location);
1813 if ($3 == TypeManager.system_void_expr) {
1814 Report.Error (547, lt.Location, "`{0}': property or indexer cannot have void type", lt.Value);
1820 p = new Property (current_class, (Expression) $3, (int) $2, true,
1821 name, (Attributes) $1,
1824 Report.Error (548, p.Location, "`{0}': property or indexer must have at least one accessor", p.GetSignatureForError ());
1828 Pair pair = (Pair) $7;
1829 p = new Property (current_class, (Expression) $3, (int) $2, true,
1830 name, (Attributes) $1,
1831 (Accessor)pair.First, (Accessor)pair.Second);
1833 if (pair.First != null && ((Accessor)(pair.First)).Block != null) {
1834 Report.Error (531, p.Location, "`{0}.get': interface members cannot have a definition", p.GetSignatureForError ());
1839 if (pair.Second != null && ((Accessor)(pair.Second)).Block != null) {
1840 Report.Error (531, p.Location, "`{0}.set': interface members cannot have a definition", p.GetSignatureForError ());
1845 if (RootContext.Documentation != null)
1846 p.DocComment = Lexer.consume_doc_comment ();
1853 CheckIdentifierToken (yyToken, GetLocation ($4));
1859 interface_event_declaration
1860 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1862 LocatedToken lt = (LocatedToken) $5;
1863 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1864 new MemberName (lt.Value, lt.Location),
1866 if (RootContext.Documentation != null)
1867 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1869 | opt_attributes opt_new EVENT type error {
1870 CheckIdentifierToken (yyToken, GetLocation ($5));
1873 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1874 LocatedToken lt = (LocatedToken) $5;
1875 Report.Error (68, lt.Location, "`{0}.{1}': event in interface cannot have initializer", current_container.Name, lt.Value);
1878 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1880 lexer.EventParsing = true;
1882 event_accessor_declarations
1884 lexer.EventParsing = false;
1887 Report.Error (69, (Location) $3, "Event in interface cannot have add or remove accessors");
1892 interface_indexer_declaration
1893 : opt_attributes opt_new type THIS
1894 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1896 { lexer.PropertyParsing = true; }
1897 accessor_declarations
1899 has_get = has_set = false;
1900 lexer.PropertyParsing = false;
1906 i = new Indexer (current_class, (Expression) $3,
1907 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1908 (int) $2, true, (Parameters) $6, (Attributes) $1,
1911 Report.Error (548, i.Location, "`{0}': property or indexer must have at least one accessor", i.GetSignatureForError ());
1915 Pair pair = (Pair) $10;
1916 i = new Indexer (current_class, (Expression) $3,
1917 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1918 (int) $2, true, (Parameters) $6, (Attributes) $1,
1919 (Accessor)pair.First, (Accessor)pair.Second);
1921 if (pair.First != null && ((Accessor)(pair.First)).Block != null) {
1922 Report.Error (531, i.Location, "`{0}.get': interface members cannot have a definition", i.GetSignatureForError ());
1927 if (pair.Second != null && ((Accessor)(pair.Second)).Block != null) {
1928 Report.Error (531, i.Location, "`{0}.set': interface members cannot have a definition", i.GetSignatureForError ());
1933 if (RootContext.Documentation != null)
1934 i.DocComment = ConsumeStoredComment ();
1940 operator_declaration
1941 : opt_attributes opt_modifiers operator_declarator
1943 iterator_container = SimpleIteratorContainer.GetSimple ();
1950 OperatorDeclaration decl = (OperatorDeclaration) $3;
1952 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1954 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null, decl.location);
1955 if (decl.arg2type != null)
1956 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null, decl.location);
1958 Operator op = new Operator (
1959 current_class, decl.optype, decl.ret_type, (int) $2,
1960 new Parameters (param_list),
1961 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1963 if (RootContext.Documentation != null) {
1964 op.DocComment = tmpComment;
1965 Lexer.doc_state = XmlCommentState.Allowed;
1968 if (SimpleIteratorContainer.Simple.Yields)
1971 // Note again, checking is done in semantic analysis
1972 current_container.AddOperator (op);
1974 current_local_parameters = null;
1975 iterator_container = null;
1981 | SEMICOLON { $$ = null; }
1984 : type OPERATOR overloadable_operator
1985 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1987 LocatedToken lt = (LocatedToken) $6;
1988 Operator.OpType op = (Operator.OpType) $3;
1989 CheckUnaryOperator (op, lt.Location);
1991 if (op == Operator.OpType.Addition)
1992 op = Operator.OpType.UnaryPlus;
1994 if (op == Operator.OpType.Subtraction)
1995 op = Operator.OpType.UnaryNegation;
1997 Parameter [] pars = new Parameter [1];
1998 Expression type = (Expression) $5;
2000 pars [0] = new Parameter (type, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2002 current_local_parameters = new Parameters (pars);
2004 if (RootContext.Documentation != null) {
2005 tmpComment = Lexer.consume_doc_comment ();
2006 Lexer.doc_state = XmlCommentState.NotAllowed;
2009 $$ = new OperatorDeclaration (op, (Expression) $1, type, lt.Value,
2010 null, null, (Location) $2);
2012 | type OPERATOR overloadable_operator
2014 type IDENTIFIER COMMA
2018 LocatedToken ltParam1 = (LocatedToken) $6;
2019 LocatedToken ltParam2 = (LocatedToken) $9;
2020 CheckBinaryOperator ((Operator.OpType) $3, (Location) $2);
2022 Parameter [] pars = new Parameter [2];
2024 Expression typeL = (Expression) $5;
2025 Expression typeR = (Expression) $8;
2027 pars [0] = new Parameter (typeL, ltParam1.Value, Parameter.Modifier.NONE, null, ltParam1.Location);
2028 pars [1] = new Parameter (typeR, ltParam2.Value, Parameter.Modifier.NONE, null, ltParam2.Location);
2030 current_local_parameters = new Parameters (pars);
2032 if (RootContext.Documentation != null) {
2033 tmpComment = Lexer.consume_doc_comment ();
2034 Lexer.doc_state = XmlCommentState.NotAllowed;
2037 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
2038 typeL, ltParam1.Value,
2039 typeR, ltParam2.Value, (Location) $2);
2041 | conversion_operator_declarator
2042 | type OPERATOR overloadable_operator
2044 type IDENTIFIER COMMA
2045 type IDENTIFIER COMMA
2049 Report.Error (1534, (Location) $2, "Overloaded binary operator `{0}' takes two parameters",
2050 Operator.GetName ((Operator.OpType) $3));
2053 | type OPERATOR overloadable_operator
2054 OPEN_PARENS CLOSE_PARENS
2056 Report.Error (1535, (Location) $2, "Overloaded unary operator `{0}' takes one parameter",
2057 Operator.GetName ((Operator.OpType) $3));
2062 overloadable_operator
2064 : BANG { $$ = Operator.OpType.LogicalNot; }
2065 | TILDE { $$ = Operator.OpType.OnesComplement; }
2066 | OP_INC { $$ = Operator.OpType.Increment; }
2067 | OP_DEC { $$ = Operator.OpType.Decrement; }
2068 | TRUE { $$ = Operator.OpType.True; }
2069 | FALSE { $$ = Operator.OpType.False; }
2070 // Unary and binary:
2071 | PLUS { $$ = Operator.OpType.Addition; }
2072 | MINUS { $$ = Operator.OpType.Subtraction; }
2074 | STAR { $$ = Operator.OpType.Multiply; }
2075 | DIV { $$ = Operator.OpType.Division; }
2076 | PERCENT { $$ = Operator.OpType.Modulus; }
2077 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
2078 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
2079 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
2080 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
2081 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
2082 | OP_EQ { $$ = Operator.OpType.Equality; }
2083 | OP_NE { $$ = Operator.OpType.Inequality; }
2084 | OP_GT { $$ = Operator.OpType.GreaterThan; }
2085 | OP_LT { $$ = Operator.OpType.LessThan; }
2086 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
2087 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
2090 conversion_operator_declarator
2091 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
2093 LocatedToken lt = (LocatedToken) $6;
2094 Parameter [] pars = new Parameter [1];
2096 pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2098 current_local_parameters = new Parameters (pars);
2100 if (RootContext.Documentation != null) {
2101 tmpComment = Lexer.consume_doc_comment ();
2102 Lexer.doc_state = XmlCommentState.NotAllowed;
2105 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, lt.Value,
2106 null, null, (Location) $2);
2108 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
2110 LocatedToken lt = (LocatedToken) $6;
2111 Parameter [] pars = new Parameter [1];
2113 pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2115 current_local_parameters = new Parameters (pars);
2117 if (RootContext.Documentation != null) {
2118 tmpComment = Lexer.consume_doc_comment ();
2119 Lexer.doc_state = XmlCommentState.NotAllowed;
2122 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, lt.Value,
2123 null, null, (Location) $2);
2127 syntax_error ((Location) $1, "'operator' expected");
2131 syntax_error ((Location) $1, "'operator' expected");
2135 constructor_declaration
2138 constructor_declarator
2141 Constructor c = (Constructor) $3;
2142 c.Block = (ToplevelBlock) $4;
2143 c.OptAttributes = (Attributes) $1;
2144 c.ModFlags = (int) $2;
2146 if (RootContext.Documentation != null)
2147 c.DocComment = ConsumeStoredComment ();
2149 if (c.Name == current_container.Basename){
2150 if ((c.ModFlags & Modifiers.STATIC) != 0){
2151 if ((c.ModFlags & Modifiers.Accessibility) != 0){
2152 Report.Error (515, c.Location,
2153 "`{0}': access modifiers are not allowed on static constructors",
2154 c.GetSignatureForError ());
2157 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2159 if (c.Initializer != null){
2160 Report.Error (514, c.Location,
2161 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2162 c.GetSignatureForError ());
2165 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2168 // We let another layer check the validity of the constructor.
2169 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
2172 current_container.AddConstructor (c);
2174 current_local_parameters = null;
2175 if (RootContext.Documentation != null)
2176 Lexer.doc_state = XmlCommentState.Allowed;
2180 constructor_declarator
2183 if (RootContext.Documentation != null) {
2184 tmpComment = Lexer.consume_doc_comment ();
2185 Lexer.doc_state = XmlCommentState.Allowed;
2188 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2190 current_local_parameters = (Parameters) $4;
2192 opt_constructor_initializer
2194 LocatedToken lt = (LocatedToken) $1;
2195 $$ = new Constructor (current_class, lt.Value, 0, (Parameters) $4,
2196 (ConstructorInitializer) $7, lt.Location);
2202 | SEMICOLON { $$ = null; }
2205 opt_constructor_initializer
2206 : /* empty */ { $$ = null; }
2207 | constructor_initializer
2210 constructor_initializer
2211 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
2213 $$ = new ConstructorBaseInitializer ((ArrayList) $4, (Location) $2);
2215 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
2217 $$ = new ConstructorThisInitializer ((ArrayList) $4, (Location) $2);
2220 Report.Error (1018, (Location) $1, "Keyword this or base expected");
2226 : /* EMPTY */ { $$ = 0; }
2227 | UNSAFE { $$ = Modifiers.UNSAFE; }
2228 | EXTERN { $$ = Modifiers.EXTERN; }
2231 destructor_declaration
2232 : opt_attributes opt_finalizer TILDE
2234 if (RootContext.Documentation != null) {
2235 tmpComment = Lexer.consume_doc_comment ();
2236 Lexer.doc_state = XmlCommentState.NotAllowed;
2239 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2241 LocatedToken lt = (LocatedToken) $5;
2242 if (lt.Value != current_container.MemberName.Name){
2243 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2244 } else if (current_container.Kind != Kind.Class){
2245 Report.Error (575, lt.Location, "Only class types can contain destructor");
2247 Location l = lt.Location;
2250 if (!RootContext.StdLib && current_container.Name == "System.Object")
2251 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2253 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2255 Method d = new Destructor (
2256 current_class, TypeManager.system_void_expr, m, "Finalize",
2257 Parameters.EmptyReadOnlyParameters, (Attributes) $1, l);
2258 if (RootContext.Documentation != null)
2259 d.DocComment = ConsumeStoredComment ();
2261 d.Block = (ToplevelBlock) $8;
2262 current_container.AddMethod (d);
2270 EVENT type variable_declarators SEMICOLON
2272 current_array_type = null;
2273 foreach (VariableDeclaration var in (ArrayList) $5) {
2275 MemberName name = new MemberName (var.identifier,
2278 EventField e = new EventField (
2279 current_class, (Expression) $4, (int) $2, false, name,
2282 e.Initializer = var.expression_or_array_initializer;
2284 current_container.AddEvent (e);
2286 if (RootContext.Documentation != null) {
2287 e.DocComment = Lexer.consume_doc_comment ();
2288 Lexer.doc_state = XmlCommentState.Allowed;
2294 EVENT type namespace_or_type_name
2297 implicit_value_parameter_type = (Expression) $4;
2298 lexer.EventParsing = true;
2300 event_accessor_declarations
2302 lexer.EventParsing = false;
2306 MemberName name = (MemberName) $5;
2309 Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2310 current_container.Name, name.ToString ());
2313 Pair pair = (Pair) $8;
2315 if (name.TypeArguments != null)
2316 syntax_error (lexer.Location, "an event can't have type arguments");
2318 if (pair.First == null || pair.Second == null)
2319 // CS0073 is already reported, so no CS0065 here.
2322 Event e = new EventProperty (
2323 current_class, (Expression) $4, (int) $2, false, name,
2324 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second);
2325 if (RootContext.Documentation != null) {
2326 e.DocComment = Lexer.consume_doc_comment ();
2327 Lexer.doc_state = XmlCommentState.Allowed;
2330 current_container.AddEvent (e);
2331 implicit_value_parameter_type = null;
2335 | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
2336 MemberName mn = (MemberName) $5;
2338 if (mn.Left != null)
2339 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2341 Report.Error (71, mn.Location, "Event declaration should use property syntax");
2343 if (RootContext.Documentation != null)
2344 Lexer.doc_state = XmlCommentState.Allowed;
2348 event_accessor_declarations
2349 : add_accessor_declaration remove_accessor_declaration
2351 $$ = new Pair ($1, $2);
2353 | remove_accessor_declaration add_accessor_declaration
2355 $$ = new Pair ($2, $1);
2357 | add_accessor_declaration { $$ = null; }
2358 | remove_accessor_declaration { $$ = null; }
2361 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2367 add_accessor_declaration
2368 : opt_attributes ADD
2370 Parameter [] args = new Parameter [1];
2371 Parameter implicit_value_parameter = new Parameter (
2372 implicit_value_parameter_type, "value",
2373 Parameter.Modifier.NONE, null, (Location) $2);
2375 args [0] = implicit_value_parameter;
2377 current_local_parameters = new Parameters (args);
2378 lexer.EventParsing = false;
2382 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2383 lexer.EventParsing = true;
2385 | opt_attributes ADD error {
2386 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2389 | opt_attributes modifiers ADD {
2390 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2395 remove_accessor_declaration
2396 : opt_attributes REMOVE
2398 Parameter [] args = new Parameter [1];
2399 Parameter implicit_value_parameter = new Parameter (
2400 implicit_value_parameter_type, "value",
2401 Parameter.Modifier.NONE, null, (Location) $2);
2403 args [0] = implicit_value_parameter;
2405 current_local_parameters = new Parameters (args);
2406 lexer.EventParsing = false;
2410 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2411 lexer.EventParsing = true;
2413 | opt_attributes REMOVE error {
2414 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2417 | opt_attributes modifiers REMOVE {
2418 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2424 : opt_attributes opt_modifiers indexer_declarator
2427 IndexerDeclaration decl = (IndexerDeclaration) $3;
2429 implicit_value_parameter_type = decl.type;
2431 lexer.PropertyParsing = true;
2432 parsing_indexer = true;
2434 indexer_parameters = decl.param_list;
2435 iterator_container = SimpleIteratorContainer.GetSimple ();
2437 accessor_declarations
2439 lexer.PropertyParsing = false;
2440 has_get = has_set = false;
2441 parsing_indexer = false;
2448 // The signature is computed from the signature of the indexer. Look
2449 // at section 3.6 on the spec
2451 IndexerDeclaration decl = (IndexerDeclaration) $3;
2452 Pair pair = (Pair) $6;
2453 Accessor get_block = (Accessor) pair.First;
2454 Accessor set_block = (Accessor) pair.Second;
2457 if (decl.interface_type != null)
2458 name = new MemberName (
2459 decl.interface_type, TypeContainer.DefaultIndexerName, decl.location);
2461 name = new MemberName (TypeContainer.DefaultIndexerName, decl.location);
2463 indexer = new Indexer (current_class, decl.type, name,
2464 (int) $2, false, decl.param_list, (Attributes) $1,
2465 get_block, set_block);
2467 if (RootContext.Documentation != null)
2468 indexer.DocComment = ConsumeStoredComment ();
2470 current_container.AddIndexer (indexer);
2472 current_local_parameters = null;
2473 implicit_value_parameter_type = null;
2474 indexer_parameters = null;
2479 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2481 Parameters pars = (Parameters) $4;
2482 if (pars.HasArglist) {
2483 // "__arglist is not valid in this context"
2484 Report.Error (1669, (Location) $2, "__arglist is not valid in this context");
2485 } else if (pars.Empty){
2486 Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
2488 if (RootContext.Documentation != null) {
2489 tmpComment = Lexer.consume_doc_comment ();
2490 Lexer.doc_state = XmlCommentState.Allowed;
2493 $$ = new IndexerDeclaration ((Expression) $1, null, pars, (Location) $2);
2495 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2497 Parameters pars = (Parameters) $6;
2499 if (pars.HasArglist) {
2500 // "__arglist is not valid in this context"
2501 Report.Error (1669, (Location) $4, "__arglist is not valid in this context");
2502 } else if (pars.Empty){
2503 Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
2506 MemberName name = (MemberName) $2;
2507 $$ = new IndexerDeclaration ((Expression) $1, name, pars, (Location) $4);
2509 if (RootContext.Documentation != null) {
2510 tmpComment = Lexer.consume_doc_comment ();
2511 Lexer.doc_state = XmlCommentState.Allowed;
2522 if (RootContext.Documentation != null)
2523 enumTypeComment = Lexer.consume_doc_comment ();
2528 LocatedToken lt = (LocatedToken) $5;
2529 Location enum_location = lt.Location;
2532 Report.Error (267, lt.Location, "The partial modifier can only appear immediately before `class', `struct' or `interface'");
2533 break; // assumes that the parser put us in a switch
2536 MemberName name = MakeName (new MemberName (lt.Value, enum_location));
2537 Enum e = new Enum (current_namespace, current_class, (Expression) $6, (int) $2,
2538 name, (Attributes) $1);
2540 if (RootContext.Documentation != null)
2541 e.DocComment = enumTypeComment;
2544 EnumMember em = null;
2545 foreach (VariableDeclaration ev in (ArrayList) $8) {
2546 em = new EnumMember (e, em, (Expression) ev.expression_or_array_initializer,
2547 new MemberName (ev.identifier, ev.Location), ev.OptAttributes);
2549 // if (RootContext.Documentation != null)
2550 em.DocComment = ev.DocComment;
2552 e.AddEnumMember (em);
2555 current_container.AddEnum (e);
2562 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2563 | COLON type { $$ = $2; }
2569 if (RootContext.Documentation != null)
2570 Lexer.doc_state = XmlCommentState.Allowed;
2572 opt_enum_member_declarations
2574 // here will be evaluated after CLOSE_BLACE is consumed.
2575 if (RootContext.Documentation != null)
2576 Lexer.doc_state = XmlCommentState.Allowed;
2584 opt_enum_member_declarations
2585 : /* empty */ { $$ = new ArrayList (4); }
2586 | enum_member_declarations opt_comma { $$ = $1; }
2589 enum_member_declarations
2590 : enum_member_declaration
2592 ArrayList l = new ArrayList (4);
2597 | enum_member_declarations COMMA enum_member_declaration
2599 ArrayList l = (ArrayList) $1;
2607 enum_member_declaration
2608 : opt_attributes IDENTIFIER
2610 VariableDeclaration vd = new VariableDeclaration (
2611 (LocatedToken) $2, null, (Attributes) $1);
2613 if (RootContext.Documentation != null) {
2614 vd.DocComment = Lexer.consume_doc_comment ();
2615 Lexer.doc_state = XmlCommentState.Allowed;
2620 | opt_attributes IDENTIFIER
2622 if (RootContext.Documentation != null) {
2623 tmpComment = Lexer.consume_doc_comment ();
2624 Lexer.doc_state = XmlCommentState.NotAllowed;
2629 VariableDeclaration vd = new VariableDeclaration (
2630 (LocatedToken) $2, $5, (Attributes) $1);
2632 if (RootContext.Documentation != null)
2633 vd.DocComment = ConsumeStoredComment ();
2639 delegate_declaration
2644 lexer.ConstraintsParsing = true;
2647 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2649 MemberName name = MakeName ((MemberName) $6);
2650 Parameters p = (Parameters) $8;
2652 // TODO: wrong location
2653 Report.Error (1669, name.Location, "__arglist is not valid in this context");
2656 Delegate del = new Delegate (current_namespace, current_class, (Expression) $5,
2657 (int) $2, name, p, (Attributes) $1);
2659 if (RootContext.Documentation != null) {
2660 del.DocComment = Lexer.consume_doc_comment ();
2661 Lexer.doc_state = XmlCommentState.Allowed;
2664 current_container.AddDelegate (del);
2665 current_delegate = del;
2667 opt_type_parameter_constraints_clauses
2669 lexer.ConstraintsParsing = false;
2673 current_delegate.SetParameterInfo ((ArrayList) $11);
2674 $$ = current_delegate;
2676 current_delegate = null;
2683 lexer.CheckNullable (false);
2688 lexer.CheckNullable (true);
2693 namespace_or_type_name
2695 | IDENTIFIER DOUBLE_COLON IDENTIFIER {
2696 LocatedToken lt1 = (LocatedToken) $1;
2697 LocatedToken lt2 = (LocatedToken) $3;
2698 $$ = new MemberName (lt1.Value, lt2.Value, lt2.Location);
2700 | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list {
2701 LocatedToken lt = (LocatedToken) $3;
2702 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $4, lt.Location);
2707 : IDENTIFIER opt_type_argument_list {
2708 LocatedToken lt = (LocatedToken) $1;
2709 $$ = new MemberName (lt.Value, (TypeArguments) $2, lt.Location);
2713 opt_type_argument_list
2714 : /* empty */ { $$ = null; }
2715 | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2718 if (RootContext.Version == LanguageVersion.ISO_1)
2719 Report.FeatureIsNotStandardized (lexer.Location, "generics");
2723 $$ = new TypeArguments ((int) $1, lexer.Location);
2724 if (RootContext.Version == LanguageVersion.ISO_1)
2725 Report.FeatureIsNotStandardized (lexer.Location, "generics");
2730 : opt_attributes type {
2731 TypeArguments type_args = new TypeArguments (lexer.Location);
2733 SimpleName sn = $2 as SimpleName;
2735 Report.Error (1031, lexer.Location, "Type expected");
2737 $2 = new TypeParameterName (sn.Name, (Attributes) $1, lexer.Location);
2739 type_args.Add ((Expression) $2);
2742 | type_arguments COMMA opt_attributes type {
2743 TypeArguments type_args = (TypeArguments) $1;
2745 SimpleName sn = $4 as SimpleName;
2747 Report.Error (1031, lexer.Location, "Type expected");
2749 $4 = new TypeParameterName (sn.Name, (Attributes) $3, lexer.Location);
2751 type_args.Add ((Expression) $4);
2758 * Before you think of adding a return_type, notice that we have been
2759 * using two rules in the places where it matters (one rule using type
2760 * and another identical one that uses VOID as the return type). This
2761 * gets rid of a shift/reduce couple
2764 : namespace_or_type_name opt_nullable
2766 MemberName name = (MemberName) $1;
2767 $$ = name.GetTypeExpression ();
2770 $$ = new ComposedCast ((Expression) $$, "?", lexer.Location);
2772 | builtin_types opt_nullable
2775 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2785 // Note that here only unmanaged types are allowed but we
2786 // can't perform checks during this phase - we do it during
2787 // semantic analysis.
2789 $$ = new ComposedCast ((Expression) $1, "*", Lexer.Location);
2793 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2798 : builtin_types opt_nullable
2801 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2803 | non_expression_type rank_specifier
2805 Location loc = GetLocation ($1);
2807 loc = lexer.Location;
2808 $$ = new ComposedCast ((Expression) $1, (string) $2, loc);
2810 | non_expression_type STAR
2812 Location loc = GetLocation ($1);
2814 loc = lexer.Location;
2815 $$ = new ComposedCast ((Expression) $1, "*", loc);
2817 | expression rank_specifiers
2819 $$ = new ComposedCast ((Expression) $1, (string) $2);
2823 $$ = new ComposedCast ((Expression) $1, "*");
2827 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2828 // through this different path
2830 | multiplicative_expression STAR
2832 $$ = new ComposedCast ((Expression) $1, "*");
2839 ArrayList types = new ArrayList (4);
2844 | type_list COMMA type
2846 ArrayList types = (ArrayList) $1;
2854 * replaces all the productions for isolating the various
2855 * simple types, but we need this to reuse it easily in local_variable_type
2858 : OBJECT { $$ = TypeManager.system_object_expr; }
2859 | STRING { $$ = TypeManager.system_string_expr; }
2860 | BOOL { $$ = TypeManager.system_boolean_expr; }
2861 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2862 | FLOAT { $$ = TypeManager.system_single_expr; }
2863 | DOUBLE { $$ = TypeManager.system_double_expr; }
2868 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2869 | BYTE { $$ = TypeManager.system_byte_expr; }
2870 | SHORT { $$ = TypeManager.system_int16_expr; }
2871 | USHORT { $$ = TypeManager.system_uint16_expr; }
2872 | INT { $$ = TypeManager.system_int32_expr; }
2873 | UINT { $$ = TypeManager.system_uint32_expr; }
2874 | LONG { $$ = TypeManager.system_int64_expr; }
2875 | ULONG { $$ = TypeManager.system_uint64_expr; }
2876 | CHAR { $$ = TypeManager.system_char_expr; }
2877 | VOID { $$ = TypeManager.system_void_expr; }
2881 : type rank_specifiers opt_nullable
2883 string rank_specifiers = (string) $2;
2885 rank_specifiers += "?";
2887 $$ = current_array_type = new ComposedCast ((Expression) $1, rank_specifiers);
2892 // Expressions, section 7.5
2901 MemberName mn = (MemberName) $1;
2902 $$ = mn.GetTypeExpression ();
2904 | IDENTIFIER DOUBLE_COLON IDENTIFIER
2906 LocatedToken lt1 = (LocatedToken) $1;
2907 LocatedToken lt2 = (LocatedToken) $3;
2908 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, lt2.Location);
2910 | parenthesized_expression
2911 | default_value_expression
2913 | invocation_expression
2917 | post_increment_expression
2918 | post_decrement_expression
2922 | checked_expression
2923 | unchecked_expression
2924 | pointer_member_access
2925 | anonymous_method_expression
2932 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
2933 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); }
2934 | NULL { $$ = new NullLiteral (lexer.Location); }
2938 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
2939 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
2940 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
2945 object v = lexer.Value;
2948 $$ = new IntLiteral ((int) v, lexer.Location);
2949 } else if (v is uint)
2950 $$ = new UIntLiteral ((UInt32) v, lexer.Location);
2952 $$ = new LongLiteral ((Int64) v, lexer.Location);
2953 else if (v is ulong)
2954 $$ = new ULongLiteral ((UInt64) v, lexer.Location);
2956 Console.WriteLine ("OOPS. Unexpected result from scanner");
2961 : TRUE { $$ = new BoolLiteral (true, lexer.Location); }
2962 | FALSE { $$ = new BoolLiteral (false, lexer.Location); }
2965 parenthesized_expression_0
2966 : OPEN_PARENS expression CLOSE_PARENS
2969 lexer.Deambiguate_CloseParens ();
2970 // After this, the next token returned is one of
2971 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2972 // or CLOSE_PARENS_MINUS.
2974 | OPEN_PARENS expression error { CheckToken (1026, yyToken, "Expecting ')'", lexer.Location); }
2977 parenthesized_expression
2978 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2982 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2984 // If a parenthesized expression is followed by a minus, we need to wrap
2985 // the expression inside a ParenthesizedExpression for the CS0075 check
2986 // in Binary.DoResolve().
2987 $$ = new ParenthesizedExpression ((Expression) $1);
2992 : primary_expression DOT IDENTIFIER opt_type_argument_list
2994 LocatedToken lt = (LocatedToken) $3;
2995 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4);
2997 | predefined_type DOT IDENTIFIER opt_type_argument_list
2999 LocatedToken lt = (LocatedToken) $3;
3000 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4);
3008 invocation_expression
3009 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
3012 Report.Error (1, (Location) $2, "Parse error");
3014 $$ = new Invocation ((Expression) $1, (ArrayList) $3);
3016 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
3018 $$ = new Invocation ((Expression) $1, new ArrayList ());
3020 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
3022 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
3024 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
3026 ArrayList args = new ArrayList (1);
3028 $$ = new Invocation ((Expression) $1, args);
3030 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
3032 ArrayList args = ((ArrayList) $4);
3034 $$ = new Invocation ((Expression) $1, args);
3039 : /* empty */ { $$ = null; }
3046 ArrayList list = new ArrayList (4);
3050 | argument_list COMMA argument
3052 ArrayList list = (ArrayList) $1;
3056 | argument_list error {
3057 CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
3065 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
3067 | non_simple_argument
3074 : REF variable_reference
3076 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
3078 | OUT variable_reference
3080 $$ = new Argument ((Expression) $2, Argument.AType.Out);
3082 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
3084 ArrayList list = (ArrayList) $3;
3085 Argument[] args = new Argument [list.Count];
3086 list.CopyTo (args, 0);
3088 Expression expr = new Arglist (args, (Location) $1);
3089 $$ = new Argument (expr, Argument.AType.Expression);
3093 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
3098 : expression { note ("section 5.4"); $$ = $1; }
3102 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
3104 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
3106 | primary_expression rank_specifiers
3108 // So the super-trick is that primary_expression
3109 // can only be either a SimpleName or a MemberAccess.
3110 // The MemberAccess case arises when you have a fully qualified type-name like :
3112 // SimpleName is when you have
3115 Expression expr = (Expression) $1;
3116 if (expr is ComposedCast){
3117 $$ = new ComposedCast (expr, (string) $2);
3118 } else if (!(expr is SimpleName || expr is MemberAccess || expr is ConstructedType || expr is QualifiedAliasMember)){
3119 Error_ExpectingTypeName (expr);
3120 $$ = TypeManager.system_object_expr;
3123 // So we extract the string corresponding to the SimpleName
3126 $$ = new ComposedCast (expr, (string) $2);
3128 current_array_type = (Expression)$$;
3135 ArrayList list = new ArrayList (4);
3139 | expression_list COMMA expression
3141 ArrayList list = (ArrayList) $1;
3150 $$ = new This (current_block, (Location) $1);
3155 : BASE DOT IDENTIFIER opt_type_argument_list
3157 LocatedToken lt = (LocatedToken) $3;
3158 $$ = new BaseAccess (lt.Value, (TypeArguments) $4, lt.Location);
3160 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
3162 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
3165 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
3170 post_increment_expression
3171 : primary_expression OP_INC
3173 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
3174 (Expression) $1, (Location) $2);
3178 post_decrement_expression
3179 : primary_expression OP_DEC
3181 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
3182 (Expression) $1, (Location) $2);
3187 : object_or_delegate_creation_expression
3188 | array_creation_expression
3191 object_or_delegate_creation_expression
3192 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
3194 $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
3198 array_creation_expression
3199 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
3201 opt_array_initializer
3203 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
3205 | NEW type rank_specifiers array_initializer
3207 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, (Location) $1);
3211 Report.Error (1031, (Location) $1, "Type expected");
3216 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
3232 opt_rank_specifier_or_nullable
3241 | opt_nullable rank_specifiers
3248 | opt_nullable rank_specifiers INTERR
3251 $$ = "?" + $2 + "?";
3258 : rank_specifier opt_rank_specifier
3260 $$ = (string) $2 + (string) $1;
3265 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3267 $$ = "[" + (string) $2 + "]";
3287 | dim_separators COMMA
3289 $$ = (string) $1 + ",";
3293 opt_array_initializer
3305 : OPEN_BRACE CLOSE_BRACE
3307 ArrayList list = new ArrayList (4);
3310 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3312 $$ = (ArrayList) $2;
3316 variable_initializer_list
3317 : variable_initializer
3319 ArrayList list = new ArrayList (4);
3323 | variable_initializer_list COMMA variable_initializer
3325 ArrayList list = (ArrayList) $1;
3331 void_pointer_expression
3332 : void_pointer_expression STAR
3334 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3338 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3343 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
3345 $$ = new TypeOfVoid ((Location) $1);
3347 | TYPEOF OPEN_PARENS void_pointer_expression CLOSE_PARENS
3349 $$ = new TypeOf ((Expression) $3, (Location) $1);
3351 | TYPEOF OPEN_PARENS
3353 lexer.TypeOfParsing = true;
3357 lexer.TypeOfParsing = false;
3358 $$ = new TypeOf ((Expression) $4, lexer.Location);
3363 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3364 $$ = new SizeOf ((Expression) $3, (Location) $1);
3369 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3371 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3375 unchecked_expression
3376 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3378 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3382 pointer_member_access
3383 : primary_expression OP_PTR IDENTIFIER
3386 LocatedToken lt = (LocatedToken) $3;
3388 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
3389 $$ = new MemberAccess (deref, lt.Value);
3393 anonymous_method_expression
3394 : DELEGATE opt_anonymous_method_signature
3396 if (oob_stack == null)
3397 oob_stack = new Stack (6);
3399 oob_stack.Push (current_local_parameters);
3400 current_local_parameters = (Parameters)$2;
3402 // Force the next block to be created as a ToplevelBlock
3403 oob_stack.Push (current_block);
3404 oob_stack.Push (top_current_block);
3405 current_block = null;
3409 Location loc = (Location) $1;
3410 top_current_block = (Block) oob_stack.Pop ();
3411 current_block = (Block) oob_stack.Pop ();
3412 if (RootContext.Version == LanguageVersion.ISO_1){
3413 Report.FeatureIsNotStandardized (loc, "anonymous methods");
3416 ToplevelBlock anon_block = (ToplevelBlock) $4;
3418 anon_block.Parent = current_block;
3419 $$ = new AnonymousMethod (current_container, (Parameters) $2, (ToplevelBlock) top_current_block,
3422 current_local_parameters = (Parameters) oob_stack.Pop ();
3426 opt_anonymous_method_signature
3427 : /* empty */ { $$ = null; }
3428 | anonymous_method_signature
3431 anonymous_method_signature
3432 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3435 $$ = Parameters.EmptyReadOnlyParameters;
3437 ArrayList par_list = (ArrayList) $2;
3438 Parameter [] pars = new Parameter [par_list.Count];
3439 par_list.CopyTo (pars);
3440 $$ = new Parameters (pars);
3445 opt_anonymous_method_parameter_list
3446 : /* empty */ { $$ = null; }
3447 | anonymous_method_parameter_list { $$ = $1; }
3450 anonymous_method_parameter_list
3451 : anonymous_method_parameter
3453 ArrayList a = new ArrayList (4);
3457 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3459 ArrayList a = (ArrayList) $1;
3465 anonymous_method_parameter
3466 : opt_parameter_modifier type IDENTIFIER {
3467 LocatedToken lt = (LocatedToken) $3;
3468 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3470 | PARAMS type IDENTIFIER {
3471 Report.Error (1670, ((LocatedToken) $3).Location, "The `params' modifier is not allowed in anonymous method declaration");
3476 default_value_expression
3477 : DEFAULT_OPEN_PARENS type CLOSE_PARENS
3479 $$ = new DefaultValueExpression ((Expression) $2, lexer.Location);
3484 : primary_expression
3485 | BANG prefixed_unary_expression
3487 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3489 | TILDE prefixed_unary_expression
3491 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3497 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3499 $$ = new Cast ((Expression) $1, (Expression) $3);
3501 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3503 $$ = new Cast ((Expression) $1, (Expression) $3);
3509 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3511 // TODO: wrong location
3512 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3517 // The idea to split this out is from Rhys' grammar
3518 // to solve the problem with casts.
3520 prefixed_unary_expression
3522 | PLUS prefixed_unary_expression
3524 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3526 | MINUS prefixed_unary_expression
3528 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3530 | OP_INC prefixed_unary_expression
3532 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3533 (Expression) $2, (Location) $1);
3535 | OP_DEC prefixed_unary_expression
3537 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3538 (Expression) $2, (Location) $1);
3540 | STAR prefixed_unary_expression
3542 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, (Location) $1);
3544 | BITWISE_AND prefixed_unary_expression
3546 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3550 pre_increment_expression
3551 : OP_INC prefixed_unary_expression
3553 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3554 (Expression) $2, (Location) $1);
3558 pre_decrement_expression
3559 : OP_DEC prefixed_unary_expression
3561 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3562 (Expression) $2, (Location) $1);
3566 multiplicative_expression
3567 : prefixed_unary_expression
3568 | multiplicative_expression STAR prefixed_unary_expression
3570 $$ = new Binary (Binary.Operator.Multiply,
3571 (Expression) $1, (Expression) $3);
3573 | multiplicative_expression DIV prefixed_unary_expression
3575 $$ = new Binary (Binary.Operator.Division,
3576 (Expression) $1, (Expression) $3);
3578 | multiplicative_expression PERCENT prefixed_unary_expression
3580 $$ = new Binary (Binary.Operator.Modulus,
3581 (Expression) $1, (Expression) $3);
3586 : multiplicative_expression
3587 | additive_expression PLUS multiplicative_expression
3589 $$ = new Binary (Binary.Operator.Addition,
3590 (Expression) $1, (Expression) $3);
3592 | additive_expression MINUS multiplicative_expression
3594 $$ = new Binary (Binary.Operator.Subtraction,
3595 (Expression) $1, (Expression) $3);
3600 : additive_expression
3601 | shift_expression OP_SHIFT_LEFT additive_expression
3603 $$ = new Binary (Binary.Operator.LeftShift,
3604 (Expression) $1, (Expression) $3);
3606 | shift_expression OP_SHIFT_RIGHT additive_expression
3608 $$ = new Binary (Binary.Operator.RightShift,
3609 (Expression) $1, (Expression) $3);
3620 lexer.PutbackNullable ();
3625 nullable_type_or_conditional
3628 if (((bool) $2) && ($1 is ComposedCast))
3629 $$ = ((ComposedCast) $1).RemoveNullable ();
3635 relational_expression
3637 | relational_expression OP_LT shift_expression
3639 $$ = new Binary (Binary.Operator.LessThan,
3640 (Expression) $1, (Expression) $3);
3642 | relational_expression OP_GT shift_expression
3644 $$ = new Binary (Binary.Operator.GreaterThan,
3645 (Expression) $1, (Expression) $3);
3647 | relational_expression OP_LE shift_expression
3649 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3650 (Expression) $1, (Expression) $3);
3652 | relational_expression OP_GE shift_expression
3654 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3655 (Expression) $1, (Expression) $3);
3657 | relational_expression IS
3660 } nullable_type_or_conditional
3662 $$ = new Is ((Expression) $1, (Expression) $4, (Location) $2);
3664 | relational_expression AS
3667 } nullable_type_or_conditional
3669 $$ = new As ((Expression) $1, (Expression) $4, (Location) $2);
3674 : relational_expression
3675 | equality_expression OP_EQ relational_expression
3677 $$ = new Binary (Binary.Operator.Equality,
3678 (Expression) $1, (Expression) $3);
3680 | equality_expression OP_NE relational_expression
3682 $$ = new Binary (Binary.Operator.Inequality,
3683 (Expression) $1, (Expression) $3);
3688 : equality_expression
3689 | and_expression BITWISE_AND equality_expression
3691 $$ = new Binary (Binary.Operator.BitwiseAnd,
3692 (Expression) $1, (Expression) $3);
3696 exclusive_or_expression
3698 | exclusive_or_expression CARRET and_expression
3700 $$ = new Binary (Binary.Operator.ExclusiveOr,
3701 (Expression) $1, (Expression) $3);
3705 inclusive_or_expression
3706 : exclusive_or_expression
3707 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3709 $$ = new Binary (Binary.Operator.BitwiseOr,
3710 (Expression) $1, (Expression) $3);
3714 conditional_and_expression
3715 : inclusive_or_expression
3716 | conditional_and_expression OP_AND inclusive_or_expression
3718 $$ = new Binary (Binary.Operator.LogicalAnd,
3719 (Expression) $1, (Expression) $3);
3723 conditional_or_expression
3724 : conditional_and_expression
3725 | conditional_or_expression OP_OR conditional_and_expression
3727 $$ = new Binary (Binary.Operator.LogicalOr,
3728 (Expression) $1, (Expression) $3);
3732 conditional_expression
3733 : conditional_or_expression
3734 | conditional_or_expression INTERR expression COLON expression
3736 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
3738 | conditional_or_expression INTERR INTERR expression
3740 $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $4, lexer.Location);
3742 // We'll be resolved into a `parenthesized_expression_0' later on.
3743 | conditional_or_expression INTERR CLOSE_PARENS
3745 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
3746 lexer.PutbackCloseParens ();
3750 assignment_expression
3751 : prefixed_unary_expression ASSIGN expression
3753 $$ = new Assign ((Expression) $1, (Expression) $3);
3755 | prefixed_unary_expression OP_MULT_ASSIGN expression
3757 $$ = new CompoundAssign (
3758 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
3760 | prefixed_unary_expression OP_DIV_ASSIGN expression
3762 $$ = new CompoundAssign (
3763 Binary.Operator.Division, (Expression) $1, (Expression) $3);
3765 | prefixed_unary_expression OP_MOD_ASSIGN expression
3767 $$ = new CompoundAssign (
3768 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
3770 | prefixed_unary_expression OP_ADD_ASSIGN expression
3772 $$ = new CompoundAssign (
3773 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
3775 | prefixed_unary_expression OP_SUB_ASSIGN expression
3777 $$ = new CompoundAssign (
3778 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3780 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3782 $$ = new CompoundAssign (
3783 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
3785 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3787 $$ = new CompoundAssign (
3788 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
3790 | prefixed_unary_expression OP_AND_ASSIGN expression
3792 $$ = new CompoundAssign (
3793 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
3795 | prefixed_unary_expression OP_OR_ASSIGN expression
3797 $$ = new CompoundAssign (
3798 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
3800 | prefixed_unary_expression OP_XOR_ASSIGN expression
3802 $$ = new CompoundAssign (
3803 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
3808 : conditional_expression
3809 | assignment_expression
3829 lexer.ConstraintsParsing = true;
3833 MemberName name = MakeName ((MemberName) $6);
3834 int mod_flags = (int) $2;
3836 current_class = new Class (
3837 current_namespace, current_class, name,
3838 mod_flags, (Attributes) $1);
3841 current_container = current_container.AddPartial (current_class);
3843 current_container.AddClassOrStruct (current_class);
3844 current_container = current_class;
3848 opt_type_parameter_constraints_clauses
3850 lexer.ConstraintsParsing = false;
3853 if (current_class.Name == "System.Object") {
3854 Report.Error (537, current_class.Location,
3855 "The class System.Object cannot have a base " +
3856 "class or implement an interface.");
3858 current_class.Bases = (ArrayList) $8;
3861 current_class.SetParameterInfo ((ArrayList) $9);
3863 if (RootContext.Documentation != null) {
3864 current_container.DocComment = Lexer.consume_doc_comment ();
3865 Lexer.doc_state = XmlCommentState.Allowed;
3870 if (RootContext.Documentation != null)
3871 Lexer.doc_state = XmlCommentState.Allowed;
3875 $$ = pop_current_class ();
3883 { $$ = $1; } // location
3887 : /* empty */ { $$ = (int) 0; }
3893 | modifiers modifier
3898 if ((m1 & m2) != 0) {
3899 Location l = lexer.Location;
3900 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
3902 $$ = (int) (m1 | m2);
3907 : NEW { $$ = Modifiers.NEW; }
3908 | PUBLIC { $$ = Modifiers.PUBLIC; }
3909 | PROTECTED { $$ = Modifiers.PROTECTED; }
3910 | INTERNAL { $$ = Modifiers.INTERNAL; }
3911 | PRIVATE { $$ = Modifiers.PRIVATE; }
3912 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3913 | SEALED { $$ = Modifiers.SEALED; }
3914 | STATIC { $$ = Modifiers.STATIC; }
3915 | READONLY { $$ = Modifiers.READONLY; }
3916 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3917 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3918 | EXTERN { $$ = Modifiers.EXTERN; }
3919 | VOLATILE { $$ = Modifiers.VOLATILE; }
3920 | UNSAFE { $$ = Modifiers.UNSAFE; }
3924 : /* empty */ { $$ = null; }
3925 | class_base { $$ = $1; }
3929 : COLON type_list { $$ = $2; }
3932 opt_type_parameter_constraints_clauses
3933 : /* empty */ { $$ = null; }
3934 | type_parameter_constraints_clauses
3938 type_parameter_constraints_clauses
3939 : type_parameter_constraints_clause {
3940 ArrayList constraints = new ArrayList (1);
3941 constraints.Add ($1);
3944 | type_parameter_constraints_clauses type_parameter_constraints_clause {
3945 ArrayList constraints = (ArrayList) $1;
3947 constraints.Add ($2);
3952 type_parameter_constraints_clause
3953 : WHERE IDENTIFIER COLON type_parameter_constraints {
3954 LocatedToken lt = (LocatedToken) $2;
3955 $$ = new Constraints (lt.Value, (ArrayList) $4, lt.Location);
3959 type_parameter_constraints
3960 : type_parameter_constraint {
3961 ArrayList constraints = new ArrayList (1);
3962 constraints.Add ($1);
3965 | type_parameter_constraints COMMA type_parameter_constraint {
3966 ArrayList constraints = (ArrayList) $1;
3968 constraints.Add ($3);
3973 type_parameter_constraint
3975 | NEW OPEN_PARENS CLOSE_PARENS {
3976 $$ = SpecialConstraint.Constructor;
3979 $$ = SpecialConstraint.ReferenceType;
3982 $$ = SpecialConstraint.ValueType;
3991 // A block is "contained" on the following places:
3993 // property_declaration as part of the accessor body (get/set)
3994 // operator_declaration
3995 // constructor_declaration
3996 // destructor_declaration
3997 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
4002 if (current_block == null){
4003 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, (Location) $1);
4004 top_current_block = current_block;
4006 current_block = new Block (current_block, (Location) $1, Location.Null);
4009 opt_statement_list CLOSE_BRACE
4011 while (current_block.Implicit)
4012 current_block = current_block.Parent;
4014 current_block.SetEndLocation ((Location) $4);
4015 current_block = current_block.Parent;
4016 if (current_block == null)
4017 top_current_block = null;
4028 | statement_list statement
4032 : declaration_statement
4034 if ($1 != null && (Block) $1 != current_block){
4035 current_block.AddStatement ((Statement) $1);
4036 current_block = (Block) $1;
4039 | valid_declaration_statement
4041 current_block.AddStatement ((Statement) $1);
4046 valid_declaration_statement
4049 | expression_statement
4050 | selection_statement
4051 | iteration_statement
4055 | unchecked_statement
4063 : valid_declaration_statement
4064 | declaration_statement
4066 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4071 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4079 $$ = EmptyStatement.Value;
4086 LocatedToken lt = (LocatedToken) $1;
4087 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
4089 if (current_block.AddLabel (labeled))
4090 current_block.AddStatement (labeled);
4095 declaration_statement
4096 : local_variable_declaration SEMICOLON
4098 current_array_type = null;
4100 DictionaryEntry de = (DictionaryEntry) $1;
4101 Expression e = (Expression) de.Key;
4103 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
4107 | local_constant_declaration SEMICOLON
4109 current_array_type = null;
4111 DictionaryEntry de = (DictionaryEntry) $1;
4113 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
4119 * The following is from Rhys' grammar:
4120 * > Types in local variable declarations must be recognized as
4121 * > expressions to prevent reduce/reduce errors in the grammar.
4122 * > The expressions are converted into types during semantic analysis.
4125 : primary_expression opt_rank_specifier_or_nullable
4127 // FIXME: Do something smart here regarding the composition of the type.
4129 // Ok, the above "primary_expression" is there to get rid of
4130 // both reduce/reduce and shift/reduces in the grammar, it should
4131 // really just be "type_name". If you use type_name, a reduce/reduce
4132 // creeps up. If you use namespace_or_type_name (which is all we need
4133 // really) two shift/reduces appear.
4136 // So the super-trick is that primary_expression
4137 // can only be either a SimpleName or a MemberAccess.
4138 // The MemberAccess case arises when you have a fully qualified type-name like :
4140 // SimpleName is when you have
4143 Expression expr = (Expression) $1;
4144 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType || expr is QualifiedAliasMember)) {
4145 Error_ExpectingTypeName (expr);
4149 // So we extract the string corresponding to the SimpleName
4153 if ((string) $2 == "")
4156 $$ = new ComposedCast ((Expression) $1, (string) $2);
4159 | builtin_types opt_rank_specifier_or_nullable
4161 if ((string) $2 == "")
4164 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
4168 local_variable_pointer_type
4169 : primary_expression STAR
4171 Expression expr = (Expression) $1;
4173 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType || expr is QualifiedAliasMember)) {
4174 Error_ExpectingTypeName (expr);
4178 $$ = new ComposedCast ((Expression) $1, "*");
4180 | builtin_types STAR
4182 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
4186 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
4188 | local_variable_pointer_type STAR
4190 $$ = new ComposedCast ((Expression) $1, "*");
4194 local_variable_declaration
4195 : local_variable_type variable_declarators
4198 $$ = new DictionaryEntry ($1, $2);
4202 | local_variable_pointer_type opt_rank_specifier_or_nullable variable_declarators
4207 if ((string) $2 == "")
4208 t = (Expression) $1;
4210 t = new ComposedCast ((Expression) $1, (string) $2);
4211 $$ = new DictionaryEntry (t, $3);
4217 local_constant_declaration
4218 : CONST local_variable_type constant_declarators
4221 $$ = new DictionaryEntry ($2, $3);
4227 expression_statement
4228 : statement_expression SEMICOLON { $$ = $1; }
4232 // We have to do the wrapping here and not in the case above,
4233 // because statement_expression is used for example in for_statement
4235 statement_expression
4238 Expression expr = (Expression) $1;
4239 ExpressionStatement s = expr as ExpressionStatement;
4241 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
4244 $$ = new StatementExpression (s);
4248 Report.Error (1002, GetLocation ($1), "Expecting `;'");
4253 object_creation_expression
4254 : object_or_delegate_creation_expression
4255 { note ("complain if this is a delegate maybe?"); }
4264 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
4267 Location l = (Location) $1;
4269 $$ = new If ((Expression) $3, (Statement) $5, l);
4271 // FIXME: location for warning should be loc property of $5.
4272 if ($5 == EmptyStatement.Value)
4273 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4276 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
4277 embedded_statement ELSE embedded_statement
4279 Location l = (Location) $1;
4281 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
4283 // FIXME: location for warning should be loc property of $5 and $7.
4284 if ($5 == EmptyStatement.Value)
4285 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4286 if ($7 == EmptyStatement.Value)
4287 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4292 : SWITCH OPEN_PARENS
4294 if (switch_stack == null)
4295 switch_stack = new Stack (2);
4296 switch_stack.Push (current_block);
4298 expression CLOSE_PARENS
4301 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
4302 current_block = (Block) switch_stack.Pop ();
4318 Report.Error (1522, lexer.Location, "Empty switch block");
4326 ArrayList sections = new ArrayList (4);
4331 | switch_sections switch_section
4333 ArrayList sections = (ArrayList) $1;
4343 current_block = current_block.CreateSwitchBlock (lexer.Location);
4347 Block topmost = current_block;
4349 while (topmost.Implicit)
4350 topmost = topmost.Parent;
4351 $$ = new SwitchSection ((ArrayList) $1, topmost);
4358 ArrayList labels = new ArrayList (4);
4363 | switch_labels switch_label
4365 ArrayList labels = (ArrayList) ($1);
4373 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
4374 | DEFAULT COLON { $$ = new SwitchLabel (null, (Location) $2); }
4377 1523, GetLocation ($1),
4378 "The keyword case or default must precede code in switch block");
4390 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
4392 Location l = (Location) $1;
4393 $$ = new While ((Expression) $3, (Statement) $5, l);
4398 : DO embedded_statement
4399 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
4401 Location l = (Location) $1;
4403 $$ = new Do ((Statement) $2, (Expression) $5, l);
4409 opt_for_initializer SEMICOLON
4411 Block assign_block = new Block (current_block);
4412 current_block = assign_block;
4414 if ($3 is DictionaryEntry){
4415 DictionaryEntry de = (DictionaryEntry) $3;
4417 Expression type = (Expression) de.Key;
4418 ArrayList var_declarators = (ArrayList) de.Value;
4420 foreach (VariableDeclaration decl in var_declarators){
4424 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4428 Location l = lexer.Location;
4429 Expression expr = decl.expression_or_array_initializer;
4431 LocalVariableReference var;
4432 var = new LocalVariableReference (assign_block, decl.identifier, l);
4435 Assign a = new Assign (var, expr, decl.Location);
4437 assign_block.AddStatement (new StatementExpression (a));
4441 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4442 // This can be referred to as $5 below.
4448 opt_for_condition SEMICOLON
4449 opt_for_iterator CLOSE_PARENS
4452 Location l = (Location) $1;
4454 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4456 current_block.AddStatement (f);
4457 while (current_block.Implicit)
4458 current_block = current_block.Parent;
4460 current_block = current_block.Parent;
4465 : /* empty */ { $$ = EmptyStatement.Value; }
4470 : local_variable_declaration
4471 | statement_expression_list
4475 : /* empty */ { $$ = null; }
4476 | boolean_expression
4480 : /* empty */ { $$ = EmptyStatement.Value; }
4485 : statement_expression_list
4488 statement_expression_list
4489 : statement_expression
4491 // CHANGE: was `null'
4492 Statement s = (Statement) $1;
4493 Block b = new Block (current_block, Block.Flags.Implicit, s.loc, lexer.Location);
4498 | statement_expression_list COMMA statement_expression
4500 Block b = (Block) $1;
4502 b.AddStatement ((Statement) $3);
4508 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4510 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4513 | FOREACH OPEN_PARENS type IDENTIFIER IN
4514 expression CLOSE_PARENS
4516 Block foreach_block = new Block (current_block);
4517 current_block = foreach_block;
4519 LocatedToken lt = (LocatedToken) $4;
4520 Location l = lt.Location;
4523 vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4525 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4527 // Get a writable reference to this read-only variable.
4529 // Note that the $$ here refers to the value of _this_ code block,
4530 // not the value of the LHS non-terminal. This can be referred to as $8 below.
4531 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
4538 LocalVariableReference v = (LocalVariableReference) $8;
4539 Location l = (Location) $1;
4542 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
4543 current_block.AddStatement (f);
4546 while (current_block.Implicit)
4547 current_block = current_block.Parent;
4549 current_block = current_block.Parent;
4555 | continue_statement
4565 $$ = new Break ((Location) $1);
4570 : CONTINUE SEMICOLON
4572 $$ = new Continue ((Location) $1);
4577 : GOTO IDENTIFIER SEMICOLON
4579 LocatedToken lt = (LocatedToken) $2;
4580 $$ = new Goto (lt.Value, lt.Location);
4582 | GOTO CASE constant_expression SEMICOLON
4584 $$ = new GotoCase ((Expression) $3, (Location) $1);
4586 | GOTO DEFAULT SEMICOLON
4588 $$ = new GotoDefault ((Location) $1);
4593 : RETURN opt_expression SEMICOLON
4595 $$ = new Return ((Expression) $2, (Location) $1);
4600 : THROW opt_expression SEMICOLON
4602 $$ = new Throw ((Expression) $2, (Location) $1);
4607 : IDENTIFIER RETURN expression SEMICOLON
4609 LocatedToken lt = (LocatedToken) $1;
4610 string s = lt.Value;
4612 Report.Error (1003, lt.Location, "; expected");
4615 if (RootContext.Version == LanguageVersion.ISO_1){
4616 Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4619 if (iterator_container == null){
4620 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4623 iterator_container.SetYields ();
4624 $$ = new Yield ((Expression) $3, lt.Location);
4627 | IDENTIFIER RETURN SEMICOLON
4629 Report.Error (1627, (Location) $2, "Expression expected after yield return");
4632 | IDENTIFIER BREAK SEMICOLON
4634 LocatedToken lt = (LocatedToken) $1;
4635 string s = lt.Value;
4637 Report.Error (1003, lt.Location, "; expected");
4640 if (RootContext.Version == LanguageVersion.ISO_1){
4641 Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4644 if (iterator_container == null){
4645 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4648 iterator_container.SetYields ();
4649 $$ = new YieldBreak (lt.Location);
4660 : TRY block catch_clauses
4664 ArrayList c = (ArrayList)$3;
4665 for (int i = 0; i < c.Count; ++i) {
4666 Catch cc = (Catch) c [i];
4668 if (i != c.Count - 1)
4669 Report.Error (1017, cc.loc, "Try statement already has an empty catch block");
4676 // Now s contains the list of specific catch clauses
4677 // and g contains the general one.
4679 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4681 | TRY block opt_catch_clauses FINALLY block
4684 ArrayList s = new ArrayList (4);
4685 ArrayList catch_list = (ArrayList) $3;
4687 if (catch_list != null){
4688 foreach (Catch cc in catch_list) {
4696 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4700 Report.Error (1524, (Location) $1, "Expected catch or finally");
4706 : /* empty */ { $$ = null; }
4713 ArrayList l = new ArrayList (4);
4718 | catch_clauses catch_clause
4720 ArrayList l = (ArrayList) $1;
4728 : /* empty */ { $$ = null; }
4733 : CATCH opt_catch_args
4735 Expression type = null;
4738 DictionaryEntry cc = (DictionaryEntry) $2;
4739 type = (Expression) cc.Key;
4740 LocatedToken lt = (LocatedToken) cc.Value;
4743 ArrayList one = new ArrayList (4);
4745 one.Add (new VariableDeclaration (lt, null));
4747 current_block = new Block (current_block);
4748 Block b = declare_local_variables (type, one, lt.Location);
4753 Expression type = null;
4755 Block var_block = null;
4758 DictionaryEntry cc = (DictionaryEntry) $2;
4759 type = (Expression) cc.Key;
4760 LocatedToken lt = (LocatedToken) cc.Value;
4764 while (current_block.Implicit)
4765 current_block = current_block.Parent;
4766 var_block = current_block;
4767 current_block = current_block.Parent;
4771 $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
4776 : /* empty */ { $$ = null; }
4781 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4783 $$ = new DictionaryEntry ($2, $3);
4791 $$ = new Checked ((Block) $2);
4798 $$ = new Unchecked ((Block) $2);
4805 RootContext.CheckUnsafeOption ((Location) $1);
4807 $$ = new Unsafe ((Block) $3);
4813 type fixed_pointer_declarators
4816 ArrayList list = (ArrayList) $4;
4817 Expression type = (Expression) $3;
4818 Location l = (Location) $1;
4819 int top = list.Count;
4821 Block assign_block = new Block (current_block);
4822 current_block = assign_block;
4824 for (int i = 0; i < top; i++){
4825 Pair p = (Pair) list [i];
4828 v = current_block.AddVariable (type, (string) p.First, l);
4832 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4840 Location l = (Location) $1;
4842 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4844 current_block.AddStatement (f);
4845 while (current_block.Implicit)
4846 current_block = current_block.Parent;
4848 current_block = current_block.Parent;
4852 fixed_pointer_declarators
4853 : fixed_pointer_declarator {
4854 ArrayList declarators = new ArrayList (4);
4856 declarators.Add ($1);
4859 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4861 ArrayList declarators = (ArrayList) $1;
4863 declarators.Add ($3);
4868 fixed_pointer_declarator
4869 : IDENTIFIER ASSIGN expression
4871 LocatedToken lt = (LocatedToken) $1;
4872 // FIXME: keep location
4873 $$ = new Pair (lt.Value, $3);
4877 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
4883 : LOCK OPEN_PARENS expression CLOSE_PARENS
4889 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
4894 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4896 Block assign_block = new Block (current_block);
4897 current_block = assign_block;
4899 if ($3 is DictionaryEntry){
4900 DictionaryEntry de = (DictionaryEntry) $3;
4901 Location l = (Location) $1;
4903 Expression type = (Expression) de.Key;
4904 ArrayList var_declarators = (ArrayList) de.Value;
4906 ArrayList vars = new ArrayList (4);
4908 foreach (VariableDeclaration decl in var_declarators){
4910 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4913 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
4915 Expression expr = decl.expression_or_array_initializer;
4917 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4920 LocalVariableReference var;
4922 // Get a writable reference to this read-only variable.
4923 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4925 // This is so that it is not a warning on using variables
4928 vars.Add (new DictionaryEntry (var, expr));
4930 // Assign a = new Assign (var, expr, decl.Location);
4931 // assign_block.AddStatement (new StatementExpression (a));
4934 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4935 // It can be referred to as $5 below.
4936 $$ = new DictionaryEntry (type, vars);
4943 Using u = new Using ($5, (Statement) $6, (Location) $1);
4944 current_block.AddStatement (u);
4945 while (current_block.Implicit)
4946 current_block = current_block.Parent;
4948 current_block = current_block.Parent;
4952 resource_acquisition
4953 : local_variable_declaration
4960 // A class used to pass around variable declarations and constants
4962 public class VariableDeclaration {
4963 public string identifier;
4964 public Expression expression_or_array_initializer;
4965 public Location Location;
4966 public Attributes OptAttributes;
4967 public string DocComment;
4969 public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
4971 this.identifier = lt.Value;
4972 if (eoai is ArrayList) {
4973 if (CSharpParser.current_array_type == null)
4974 Report.Error (622, lt.Location,
4975 "Can only use array initializer expressions to assign to array types. Try using a new expression instead.");
4976 this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
4978 this.expression_or_array_initializer = (Expression)eoai;
4980 this.Location = lt.Location;
4981 this.OptAttributes = opt_attrs;
4984 public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
4990 // A class used to hold info about an indexer declarator
4992 public class IndexerDeclaration {
4993 public Expression type;
4994 public MemberName interface_type;
4995 public Parameters param_list;
4996 public Location location;
4998 public IndexerDeclaration (Expression type, MemberName interface_type,
4999 Parameters param_list, Location loc)
5002 this.interface_type = interface_type;
5003 this.param_list = param_list;
5004 this.location = loc;
5009 // We use this when we do not have an object in advance that is an IIteratorContainer
5011 public class SimpleIteratorContainer : IIteratorContainer {
5014 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
5019 public static SimpleIteratorContainer GetSimple () {
5020 Simple.Yields = false;
5024 public void SetYields () { Yields = true; }
5028 // A class used to hold info about an operator declarator
5030 public class OperatorDeclaration {
5031 public Operator.OpType optype;
5032 public Expression ret_type, arg1type, arg2type;
5033 public string arg1name, arg2name;
5034 public Location location;
5036 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
5037 Expression arg1type, string arg1name,
5038 Expression arg2type, string arg2name, Location location)
5041 this.ret_type = ret_type;
5042 this.arg1type = arg1type;
5043 this.arg1name = arg1name;
5044 this.arg2type = arg2type;
5045 this.arg2name = arg2name;
5046 this.location = location;
5051 void Error_ExpectingTypeName (Expression expr)
5053 if (expr is Invocation){
5054 Report.Error (1002, expr.Location, "Expecting `;'");
5056 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
5060 TypeContainer pop_current_class ()
5062 TypeContainer retval = current_class;
5064 current_class = (TypeContainer) current_class.Parent;
5065 current_container = current_class.PartialContainer;
5071 // Given the @class_name name, it creates a fully qualified name
5072 // based on the containing declaration space
5075 MakeName (MemberName class_name)
5077 Namespace ns = current_namespace.NS;
5079 if (current_container.Name.Length == 0){
5080 if (ns.Name.Length != 0)
5081 return new MemberName (ns.MemberName, class_name);
5085 return new MemberName (current_container.MemberName, class_name);
5089 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
5091 Block implicit_block;
5092 ArrayList inits = null;
5095 // We use the `Used' property to check whether statements
5096 // have been added to the current block. If so, we need
5097 // to create another block to contain the new declaration
5098 // otherwise, as an optimization, we use the same block to
5099 // add the declaration.
5101 // FIXME: A further optimization is to check if the statements
5102 // that were added were added as part of the initialization
5103 // below. In which case, no other statements have been executed
5104 // and we might be able to reduce the number of blocks for
5105 // situations like this:
5107 // int j = 1; int k = j + 1;
5109 if (current_block.Used)
5110 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
5112 implicit_block = current_block;
5114 foreach (VariableDeclaration decl in variable_declarators){
5116 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
5117 if (decl.expression_or_array_initializer != null){
5119 inits = new ArrayList (4);
5126 return implicit_block;
5128 foreach (VariableDeclaration decl in inits){
5130 Expression expr = decl.expression_or_array_initializer;
5132 LocalVariableReference var;
5133 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
5135 assign = new Assign (var, expr, decl.Location);
5137 implicit_block.AddStatement (new StatementExpression (assign));
5140 return implicit_block;
5143 Block declare_local_constants (Expression type, ArrayList declarators)
5145 Block implicit_block;
5147 if (current_block.Used)
5148 implicit_block = new Block (current_block, Block.Flags.Implicit);
5150 implicit_block = current_block;
5152 foreach (VariableDeclaration decl in declarators){
5153 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
5156 return implicit_block;
5159 void CheckAttributeTarget (string a, Location l)
5163 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
5167 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
5173 void CheckUnaryOperator (Operator.OpType op, Location l)
5177 case Operator.OpType.LogicalNot:
5178 case Operator.OpType.OnesComplement:
5179 case Operator.OpType.Increment:
5180 case Operator.OpType.Decrement:
5181 case Operator.OpType.True:
5182 case Operator.OpType.False:
5183 case Operator.OpType.Addition:
5184 case Operator.OpType.Subtraction:
5189 Report.Error (1019, l, "Overloadable unary operator expected");
5195 void CheckBinaryOperator (Operator.OpType op, Location l)
5199 case Operator.OpType.Addition:
5200 case Operator.OpType.Subtraction:
5201 case Operator.OpType.Multiply:
5202 case Operator.OpType.Division:
5203 case Operator.OpType.Modulus:
5204 case Operator.OpType.BitwiseAnd:
5205 case Operator.OpType.BitwiseOr:
5206 case Operator.OpType.ExclusiveOr:
5207 case Operator.OpType.LeftShift:
5208 case Operator.OpType.RightShift:
5209 case Operator.OpType.Equality:
5210 case Operator.OpType.Inequality:
5211 case Operator.OpType.GreaterThan:
5212 case Operator.OpType.LessThan:
5213 case Operator.OpType.GreaterThanOrEqual:
5214 case Operator.OpType.LessThanOrEqual:
5218 Report.Error (1020, l, "Overloadable binary operator expected");
5224 void syntax_error (Location l, string msg)
5226 Report.Error (1003, l, "Syntax error, " + msg);
5229 void note (string s)
5231 // Used to put annotations
5236 public Tokenizer Lexer {
5242 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
5244 current_namespace = new NamespaceEntry (null, file, null);
5245 this.name = file.Name;
5247 current_container = RootContext.Tree.Types;
5248 // TODO: Make RootContext.Tree.Types a PartialContainer.
5249 current_class = current_container;
5250 current_container.NamespaceEntry = current_namespace;
5252 lexer = new Tokenizer (reader, file, defines);
5255 public void parse ()
5257 int errors = Report.Errors;
5259 if (yacc_verbose_flag > 1)
5260 yyparse (lexer, new yydebug.yyDebugSimple ());
5263 Tokenizer tokenizer = lexer as Tokenizer;
5264 tokenizer.cleanup ();
5265 } catch (Exception e){
5267 // Removed for production use, use parser verbose to get the output.
5269 // Console.WriteLine (e);
5270 if (Report.Errors == errors)
5271 Report.Error (-25, lexer.Location, "Parsing error");
5272 if (yacc_verbose_flag > 0)
5273 Console.WriteLine (e);
5276 RootContext.Tree.Types.NamespaceEntry = null;
5279 static void CheckToken (int error, int yyToken, string msg, Location loc)
5281 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
5282 Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ());
5284 Report.Error (error, loc, msg);
5287 void CheckIdentifierToken (int yyToken, Location loc)
5289 CheckToken (1041, yyToken, "Identifier expected", loc);
5292 string ConsumeStoredComment ()
5294 string s = tmpComment;
5296 Lexer.doc_state = XmlCommentState.Allowed;
5300 Location GetLocation (object obj)
5302 if (obj is MemberCore)
5303 return ((MemberCore) obj).Location;
5304 if (obj is MemberName)
5305 return ((MemberName) obj).Location;
5306 if (obj is LocatedToken)
5307 return ((LocatedToken) obj).Location;
5308 if (obj is Location)
5309 return (Location) obj;
5310 return lexer.Location;