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 | ARGLIST COMMA error
1247 Report.Error (257, (Location) $1, "An __arglist parameter must be the last parameter in a formal parameter list");
1252 $$ = new Parameters (new Parameter[] { (Parameter) $1 } );
1256 $$ = new Parameters (new Parameter[0], true);
1263 ArrayList pars = new ArrayList (4);
1268 | fixed_parameters COMMA fixed_parameter
1270 ArrayList pars = (ArrayList) $1;
1279 opt_parameter_modifier
1283 LocatedToken lt = (LocatedToken) $4;
1284 $$ = new Parameter ((Expression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1287 opt_parameter_modifier
1289 IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1291 LocatedToken lt = (LocatedToken) $4;
1292 Report.Error (1552, lt.Location, "Array type specifier, [], must appear before parameter name");
1296 opt_parameter_modifier
1299 Report.Error (1001, GetLocation ($3), "Identifier expected");
1303 opt_parameter_modifier
1306 CheckIdentifierToken (yyToken, GetLocation ($4));
1310 opt_parameter_modifier
1316 LocatedToken lt = (LocatedToken) $4;
1317 Report.Error (241, lt.Location, "Default parameter specifiers are not permitted");
1322 opt_parameter_modifier
1323 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1324 | parameter_modifier
1328 : REF { $$ = Parameter.Modifier.REF; }
1329 | OUT { $$ = Parameter.Modifier.OUT; }
1333 : opt_attributes PARAMS type IDENTIFIER
1335 LocatedToken lt = (LocatedToken) $4;
1336 $$ = new ParamsParameter ((Expression) $3, lt.Value, (Attributes) $1, lt.Location);
1337 note ("type must be a single-dimension array type");
1339 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1341 Report.Error (1611, (Location) $2, "The params parameter cannot be declared as ref or out");
1344 | opt_attributes PARAMS type error {
1345 CheckIdentifierToken (yyToken, GetLocation ($4));
1350 property_declaration
1354 namespace_or_type_name
1356 if (RootContext.Documentation != null)
1357 tmpComment = Lexer.consume_doc_comment ();
1361 implicit_value_parameter_type = (Expression) $3;
1363 lexer.PropertyParsing = true;
1365 accessor_declarations
1367 lexer.PropertyParsing = false;
1368 has_get = has_set = false;
1376 Pair pair = (Pair) $8;
1377 Accessor get_block = (Accessor) pair.First;
1378 Accessor set_block = (Accessor) pair.Second;
1380 MemberName name = (MemberName) $4;
1382 if (name.TypeArguments != null)
1383 syntax_error (lexer.Location, "a property can't have type arguments");
1385 prop = new Property (current_class, (Expression) $3, (int) $2, false,
1386 name, (Attributes) $1, get_block, set_block);
1388 current_container.AddProperty (prop);
1389 implicit_value_parameter_type = null;
1391 if (RootContext.Documentation != null)
1392 prop.DocComment = ConsumeStoredComment ();
1397 accessor_declarations
1398 : get_accessor_declaration
1400 $$ = new Pair ($1, null);
1402 | get_accessor_declaration accessor_declarations
1404 Pair pair = (Pair) $2;
1408 | set_accessor_declaration
1410 $$ = new Pair (null, $1);
1412 | set_accessor_declaration accessor_declarations
1414 Pair pair = (Pair) $2;
1420 Report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1425 get_accessor_declaration
1426 : opt_attributes opt_modifiers GET
1428 // If this is not the case, then current_local_parameters has already
1429 // been set in indexer_declaration
1430 if (parsing_indexer == false)
1431 current_local_parameters = null;
1433 current_local_parameters = indexer_parameters;
1434 lexer.PropertyParsing = false;
1436 iterator_container = SimpleIteratorContainer.GetSimple ();
1441 Report.Error (1007, (Location) $3, "Property accessor already defined");
1444 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1446 current_local_parameters = null;
1447 lexer.PropertyParsing = true;
1449 if (SimpleIteratorContainer.Simple.Yields)
1450 accessor.SetYields ();
1452 iterator_container = null;
1454 if (RootContext.Documentation != null)
1455 if (Lexer.doc_state == XmlCommentState.Error)
1456 Lexer.doc_state = XmlCommentState.NotAllowed;
1462 set_accessor_declaration
1463 : opt_attributes opt_modifiers SET
1466 Parameter implicit_value_parameter = new Parameter (
1467 implicit_value_parameter_type, "value",
1468 Parameter.Modifier.NONE, null, (Location) $3);
1470 if (parsing_indexer == false) {
1471 args = new Parameter [1];
1472 args [0] = implicit_value_parameter;
1473 current_local_parameters = new Parameters (args);
1475 Parameter [] fpars = indexer_parameters.FixedParameters;
1478 int count = fpars.Length;
1480 args = new Parameter [count + 1];
1481 fpars.CopyTo (args, 0);
1482 args [count] = implicit_value_parameter;
1485 current_local_parameters = new Parameters (
1489 lexer.PropertyParsing = false;
1491 iterator_container = SimpleIteratorContainer.GetSimple ();
1496 Report.Error (1007, ((LocatedToken) $3).Location, "Property accessor already defined");
1499 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1501 current_local_parameters = null;
1502 lexer.PropertyParsing = true;
1504 if (SimpleIteratorContainer.Simple.Yields)
1505 accessor.SetYields ();
1507 iterator_container = null;
1509 if (RootContext.Documentation != null
1510 && Lexer.doc_state == XmlCommentState.Error)
1511 Lexer.doc_state = XmlCommentState.NotAllowed;
1519 | SEMICOLON { $$ = null; }
1522 interface_declaration
1528 lexer.ConstraintsParsing = true;
1532 MemberName name = MakeName ((MemberName) $6);
1534 current_class = new Interface (
1535 current_namespace, current_class, name, (int) $2,
1539 current_container = current_container.AddPartial (current_class);
1541 current_container.AddInterface (current_class);
1542 current_container = current_class;
1546 opt_type_parameter_constraints_clauses
1548 lexer.ConstraintsParsing = false;
1551 current_class.Bases = (ArrayList) $8;
1553 current_class.SetParameterInfo ((ArrayList) $9);
1555 if (RootContext.Documentation != null) {
1556 current_container.DocComment = Lexer.consume_doc_comment ();
1557 Lexer.doc_state = XmlCommentState.Allowed;
1562 if (RootContext.Documentation != null)
1563 Lexer.doc_state = XmlCommentState.Allowed;
1567 $$ = pop_current_class ();
1569 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1570 CheckIdentifierToken (yyToken, GetLocation ($5));
1576 opt_interface_member_declarations
1580 opt_interface_member_declarations
1582 | interface_member_declarations
1585 interface_member_declarations
1586 : interface_member_declaration
1587 | interface_member_declarations interface_member_declaration
1590 interface_member_declaration
1591 : interface_method_declaration
1596 Method m = (Method) $1;
1598 if (m.IsExplicitImpl)
1599 Report.Error (541, m.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1600 m.GetSignatureForError ());
1602 current_container.AddMethod (m);
1604 if (RootContext.Documentation != null)
1605 Lexer.doc_state = XmlCommentState.Allowed;
1607 | interface_property_declaration
1612 Property p = (Property) $1;
1614 if (p.IsExplicitImpl)
1615 Report.Error (541, p.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1616 p.GetSignatureForError ());
1618 current_container.AddProperty (p);
1620 if (RootContext.Documentation != null)
1621 Lexer.doc_state = XmlCommentState.Allowed;
1623 | interface_event_declaration
1626 Event e = (Event) $1;
1628 if (e.IsExplicitImpl)
1629 Report.Error (541, e.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1630 e.GetSignatureForError ());
1632 current_container.AddEvent (e);
1635 if (RootContext.Documentation != null)
1636 Lexer.doc_state = XmlCommentState.Allowed;
1638 | interface_indexer_declaration
1643 Indexer i = (Indexer) $1;
1645 if (i.IsExplicitImpl)
1646 Report.Error (541, i.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1647 i.GetSignatureForError ());
1649 current_container.AddIndexer (i);
1651 if (RootContext.Documentation != null)
1652 Lexer.doc_state = XmlCommentState.Allowed;
1654 | delegate_declaration
1657 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1658 ((MemberCore)$1).GetSignatureForError ());
1664 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1665 ((MemberCore)$1).GetSignatureForError ());
1668 | struct_declaration
1671 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1672 ((MemberCore)$1).GetSignatureForError ());
1678 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1679 ((MemberCore)$1).GetSignatureForError ());
1682 | interface_declaration
1685 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1686 ((MemberCore)$1).GetSignatureForError ());
1689 | constant_declaration
1691 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1699 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, GetLocation ($1));
1704 interface_method_declaration_body
1707 lexer.ConstraintsParsing = false;
1709 opt_statement_list CLOSE_BRACE
1711 Report.Error (531, lexer.Location,
1712 "'{0}': interface members cannot have a definition", ((MemberName) $-1).ToString ());
1718 interface_method_declaration
1719 : opt_attributes opt_new type namespace_or_type_name
1720 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1722 lexer.ConstraintsParsing = true;
1724 opt_type_parameter_constraints_clauses
1726 // Refer to the name as $-1 in interface_method_declaration_body
1729 interface_method_declaration_body
1731 lexer.ConstraintsParsing = false;
1733 MemberName name = (MemberName) $4;
1735 if ($9 != null && name.TypeArguments == null)
1736 Report.Error (80, lexer.Location,
1737 "Constraints are not allowed on non-generic declarations");
1739 GenericMethod generic = null;
1740 if (name.TypeArguments != null) {
1741 generic = new GenericMethod (current_namespace, current_class, name,
1742 (Expression) $3, (Parameters) $6);
1744 generic.SetParameterInfo ((ArrayList) $9);
1747 $$ = new Method (current_class, generic, (Expression) $3, (int) $2, true, name,
1748 (Parameters) $6, (Attributes) $1);
1749 if (RootContext.Documentation != null)
1750 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1752 | opt_attributes opt_new VOID namespace_or_type_name
1753 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1755 lexer.ConstraintsParsing = true;
1757 opt_type_parameter_constraints_clauses
1761 interface_method_declaration_body
1763 lexer.ConstraintsParsing = false;
1765 MemberName name = (MemberName) $4;
1767 if ($9 != null && name.TypeArguments == null)
1768 Report.Error (80, lexer.Location,
1769 "Constraints are not allowed on non-generic declarations");
1771 GenericMethod generic = null;
1772 if (name.TypeArguments != null) {
1773 generic = new GenericMethod (current_namespace, current_class, name,
1774 TypeManager.system_void_expr, (Parameters) $6);
1776 generic.SetParameterInfo ((ArrayList) $9);
1779 $$ = new Method (current_class, generic, TypeManager.system_void_expr, (int) $2,
1780 true, name, (Parameters) $6, (Attributes) $1);
1781 if (RootContext.Documentation != null)
1782 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1786 interface_property_declaration
1791 { lexer.PropertyParsing = true; }
1792 accessor_declarations
1794 has_get = has_set = false;
1795 lexer.PropertyParsing = false;
1799 LocatedToken lt = (LocatedToken) $4;
1800 MemberName name = new MemberName (lt.Value, lt.Location);
1802 if ($3 == TypeManager.system_void_expr) {
1803 Report.Error (547, lt.Location, "`{0}': property or indexer cannot have void type", lt.Value);
1809 p = new Property (current_class, (Expression) $3, (int) $2, true,
1810 name, (Attributes) $1,
1813 Report.Error (548, p.Location, "`{0}': property or indexer must have at least one accessor", p.GetSignatureForError ());
1817 Pair pair = (Pair) $7;
1818 p = new Property (current_class, (Expression) $3, (int) $2, true,
1819 name, (Attributes) $1,
1820 (Accessor)pair.First, (Accessor)pair.Second);
1822 if (pair.First != null && ((Accessor)(pair.First)).Block != null) {
1823 Report.Error (531, p.Location, "`{0}.get': interface members cannot have a definition", p.GetSignatureForError ());
1828 if (pair.Second != null && ((Accessor)(pair.Second)).Block != null) {
1829 Report.Error (531, p.Location, "`{0}.set': interface members cannot have a definition", p.GetSignatureForError ());
1834 if (RootContext.Documentation != null)
1835 p.DocComment = Lexer.consume_doc_comment ();
1842 CheckIdentifierToken (yyToken, GetLocation ($4));
1848 interface_event_declaration
1849 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1851 LocatedToken lt = (LocatedToken) $5;
1852 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1853 new MemberName (lt.Value, lt.Location),
1855 if (RootContext.Documentation != null)
1856 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1858 | opt_attributes opt_new EVENT type error {
1859 CheckIdentifierToken (yyToken, GetLocation ($5));
1862 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1863 LocatedToken lt = (LocatedToken) $5;
1864 Report.Error (68, lt.Location, "`{0}.{1}': event in interface cannot have initializer", current_container.Name, lt.Value);
1867 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1869 lexer.EventParsing = true;
1871 event_accessor_declarations
1873 lexer.EventParsing = false;
1876 Report.Error (69, (Location) $3, "Event in interface cannot have add or remove accessors");
1881 interface_indexer_declaration
1882 : opt_attributes opt_new type THIS
1883 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1885 { lexer.PropertyParsing = true; }
1886 accessor_declarations
1888 has_get = has_set = false;
1889 lexer.PropertyParsing = false;
1895 i = new Indexer (current_class, (Expression) $3,
1896 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1897 (int) $2, true, (Parameters) $6, (Attributes) $1,
1900 Report.Error (548, i.Location, "`{0}': property or indexer must have at least one accessor", i.GetSignatureForError ());
1904 Pair pair = (Pair) $10;
1905 i = new Indexer (current_class, (Expression) $3,
1906 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1907 (int) $2, true, (Parameters) $6, (Attributes) $1,
1908 (Accessor)pair.First, (Accessor)pair.Second);
1910 if (pair.First != null && ((Accessor)(pair.First)).Block != null) {
1911 Report.Error (531, i.Location, "`{0}.get': interface members cannot have a definition", i.GetSignatureForError ());
1916 if (pair.Second != null && ((Accessor)(pair.Second)).Block != null) {
1917 Report.Error (531, i.Location, "`{0}.set': interface members cannot have a definition", i.GetSignatureForError ());
1922 if (RootContext.Documentation != null)
1923 i.DocComment = ConsumeStoredComment ();
1929 operator_declaration
1930 : opt_attributes opt_modifiers operator_declarator
1932 iterator_container = SimpleIteratorContainer.GetSimple ();
1939 OperatorDeclaration decl = (OperatorDeclaration) $3;
1941 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1943 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null, decl.location);
1944 if (decl.arg2type != null)
1945 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null, decl.location);
1947 Operator op = new Operator (
1948 current_class, decl.optype, decl.ret_type, (int) $2,
1949 new Parameters (param_list),
1950 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1952 if (RootContext.Documentation != null) {
1953 op.DocComment = tmpComment;
1954 Lexer.doc_state = XmlCommentState.Allowed;
1957 if (SimpleIteratorContainer.Simple.Yields)
1960 // Note again, checking is done in semantic analysis
1961 current_container.AddOperator (op);
1963 current_local_parameters = null;
1964 iterator_container = null;
1970 | SEMICOLON { $$ = null; }
1973 : type OPERATOR overloadable_operator
1974 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1976 LocatedToken lt = (LocatedToken) $6;
1977 Operator.OpType op = (Operator.OpType) $3;
1978 CheckUnaryOperator (op, lt.Location);
1980 if (op == Operator.OpType.Addition)
1981 op = Operator.OpType.UnaryPlus;
1983 if (op == Operator.OpType.Subtraction)
1984 op = Operator.OpType.UnaryNegation;
1986 Parameter [] pars = new Parameter [1];
1987 Expression type = (Expression) $5;
1989 pars [0] = new Parameter (type, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1991 current_local_parameters = new Parameters (pars);
1993 if (RootContext.Documentation != null) {
1994 tmpComment = Lexer.consume_doc_comment ();
1995 Lexer.doc_state = XmlCommentState.NotAllowed;
1998 $$ = new OperatorDeclaration (op, (Expression) $1, type, lt.Value,
1999 null, null, (Location) $2);
2001 | type OPERATOR overloadable_operator
2003 type IDENTIFIER COMMA
2007 LocatedToken ltParam1 = (LocatedToken) $6;
2008 LocatedToken ltParam2 = (LocatedToken) $9;
2009 CheckBinaryOperator ((Operator.OpType) $3, (Location) $2);
2011 Parameter [] pars = new Parameter [2];
2013 Expression typeL = (Expression) $5;
2014 Expression typeR = (Expression) $8;
2016 pars [0] = new Parameter (typeL, ltParam1.Value, Parameter.Modifier.NONE, null, ltParam1.Location);
2017 pars [1] = new Parameter (typeR, ltParam2.Value, Parameter.Modifier.NONE, null, ltParam2.Location);
2019 current_local_parameters = new Parameters (pars);
2021 if (RootContext.Documentation != null) {
2022 tmpComment = Lexer.consume_doc_comment ();
2023 Lexer.doc_state = XmlCommentState.NotAllowed;
2026 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
2027 typeL, ltParam1.Value,
2028 typeR, ltParam2.Value, (Location) $2);
2030 | conversion_operator_declarator
2031 | type OPERATOR overloadable_operator
2033 type IDENTIFIER COMMA
2034 type IDENTIFIER COMMA
2038 Report.Error (1534, (Location) $2, "Overloaded binary operator `{0}' takes two parameters",
2039 Operator.GetName ((Operator.OpType) $3));
2042 | type OPERATOR overloadable_operator
2043 OPEN_PARENS CLOSE_PARENS
2045 Report.Error (1535, (Location) $2, "Overloaded unary operator `{0}' takes one parameter",
2046 Operator.GetName ((Operator.OpType) $3));
2051 overloadable_operator
2053 : BANG { $$ = Operator.OpType.LogicalNot; }
2054 | TILDE { $$ = Operator.OpType.OnesComplement; }
2055 | OP_INC { $$ = Operator.OpType.Increment; }
2056 | OP_DEC { $$ = Operator.OpType.Decrement; }
2057 | TRUE { $$ = Operator.OpType.True; }
2058 | FALSE { $$ = Operator.OpType.False; }
2059 // Unary and binary:
2060 | PLUS { $$ = Operator.OpType.Addition; }
2061 | MINUS { $$ = Operator.OpType.Subtraction; }
2063 | STAR { $$ = Operator.OpType.Multiply; }
2064 | DIV { $$ = Operator.OpType.Division; }
2065 | PERCENT { $$ = Operator.OpType.Modulus; }
2066 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
2067 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
2068 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
2069 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
2070 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
2071 | OP_EQ { $$ = Operator.OpType.Equality; }
2072 | OP_NE { $$ = Operator.OpType.Inequality; }
2073 | OP_GT { $$ = Operator.OpType.GreaterThan; }
2074 | OP_LT { $$ = Operator.OpType.LessThan; }
2075 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
2076 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
2079 conversion_operator_declarator
2080 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
2082 LocatedToken lt = (LocatedToken) $6;
2083 Parameter [] pars = new Parameter [1];
2085 pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2087 current_local_parameters = new Parameters (pars);
2089 if (RootContext.Documentation != null) {
2090 tmpComment = Lexer.consume_doc_comment ();
2091 Lexer.doc_state = XmlCommentState.NotAllowed;
2094 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, lt.Value,
2095 null, null, (Location) $2);
2097 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
2099 LocatedToken lt = (LocatedToken) $6;
2100 Parameter [] pars = new Parameter [1];
2102 pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2104 current_local_parameters = new Parameters (pars);
2106 if (RootContext.Documentation != null) {
2107 tmpComment = Lexer.consume_doc_comment ();
2108 Lexer.doc_state = XmlCommentState.NotAllowed;
2111 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, lt.Value,
2112 null, null, (Location) $2);
2116 syntax_error ((Location) $1, "'operator' expected");
2120 syntax_error ((Location) $1, "'operator' expected");
2124 constructor_declaration
2127 constructor_declarator
2130 Constructor c = (Constructor) $3;
2131 c.Block = (ToplevelBlock) $4;
2132 c.OptAttributes = (Attributes) $1;
2133 c.ModFlags = (int) $2;
2135 if (RootContext.Documentation != null)
2136 c.DocComment = ConsumeStoredComment ();
2138 if (c.Name == current_container.Basename){
2139 if ((c.ModFlags & Modifiers.STATIC) != 0){
2140 if ((c.ModFlags & Modifiers.Accessibility) != 0){
2141 Report.Error (515, c.Location,
2142 "`{0}': access modifiers are not allowed on static constructors",
2143 c.GetSignatureForError ());
2146 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2148 if (c.Initializer != null){
2149 Report.Error (514, c.Location,
2150 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2151 c.GetSignatureForError ());
2154 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2157 // We let another layer check the validity of the constructor.
2158 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
2161 current_container.AddConstructor (c);
2163 current_local_parameters = null;
2164 if (RootContext.Documentation != null)
2165 Lexer.doc_state = XmlCommentState.Allowed;
2169 constructor_declarator
2172 if (RootContext.Documentation != null) {
2173 tmpComment = Lexer.consume_doc_comment ();
2174 Lexer.doc_state = XmlCommentState.Allowed;
2177 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2179 current_local_parameters = (Parameters) $4;
2181 opt_constructor_initializer
2183 LocatedToken lt = (LocatedToken) $1;
2184 $$ = new Constructor (current_class, lt.Value, 0, (Parameters) $4,
2185 (ConstructorInitializer) $7, lt.Location);
2191 | SEMICOLON { $$ = null; }
2194 opt_constructor_initializer
2195 : /* empty */ { $$ = null; }
2196 | constructor_initializer
2199 constructor_initializer
2200 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
2202 $$ = new ConstructorBaseInitializer ((ArrayList) $4, (Location) $2);
2204 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
2206 $$ = new ConstructorThisInitializer ((ArrayList) $4, (Location) $2);
2209 Report.Error (1018, (Location) $1, "Keyword this or base expected");
2215 : /* EMPTY */ { $$ = 0; }
2216 | UNSAFE { $$ = Modifiers.UNSAFE; }
2217 | EXTERN { $$ = Modifiers.EXTERN; }
2220 destructor_declaration
2221 : opt_attributes opt_finalizer TILDE
2223 if (RootContext.Documentation != null) {
2224 tmpComment = Lexer.consume_doc_comment ();
2225 Lexer.doc_state = XmlCommentState.NotAllowed;
2228 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2230 LocatedToken lt = (LocatedToken) $5;
2231 if (lt.Value != current_container.MemberName.Name){
2232 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2233 } else if (current_container.Kind != Kind.Class){
2234 Report.Error (575, lt.Location, "Only class types can contain destructor");
2236 Location l = lt.Location;
2239 if (!RootContext.StdLib && current_container.Name == "System.Object")
2240 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2242 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2244 Method d = new Destructor (
2245 current_class, TypeManager.system_void_expr, m, "Finalize",
2246 Parameters.EmptyReadOnlyParameters, (Attributes) $1, l);
2247 if (RootContext.Documentation != null)
2248 d.DocComment = ConsumeStoredComment ();
2250 d.Block = (ToplevelBlock) $8;
2251 current_container.AddMethod (d);
2259 EVENT type variable_declarators SEMICOLON
2261 current_array_type = null;
2262 foreach (VariableDeclaration var in (ArrayList) $5) {
2264 MemberName name = new MemberName (var.identifier,
2267 EventField e = new EventField (
2268 current_class, (Expression) $4, (int) $2, false, name,
2271 e.Initializer = var.expression_or_array_initializer;
2273 current_container.AddEvent (e);
2275 if (RootContext.Documentation != null) {
2276 e.DocComment = Lexer.consume_doc_comment ();
2277 Lexer.doc_state = XmlCommentState.Allowed;
2283 EVENT type namespace_or_type_name
2286 implicit_value_parameter_type = (Expression) $4;
2287 lexer.EventParsing = true;
2289 event_accessor_declarations
2291 lexer.EventParsing = false;
2295 MemberName name = (MemberName) $5;
2298 Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2299 current_container.Name, name.ToString ());
2302 Pair pair = (Pair) $8;
2304 if (name.TypeArguments != null)
2305 syntax_error (lexer.Location, "an event can't have type arguments");
2307 if (pair.First == null || pair.Second == null)
2308 // CS0073 is already reported, so no CS0065 here.
2311 Event e = new EventProperty (
2312 current_class, (Expression) $4, (int) $2, false, name,
2313 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second);
2314 if (RootContext.Documentation != null) {
2315 e.DocComment = Lexer.consume_doc_comment ();
2316 Lexer.doc_state = XmlCommentState.Allowed;
2319 current_container.AddEvent (e);
2320 implicit_value_parameter_type = null;
2324 | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
2325 MemberName mn = (MemberName) $5;
2327 if (mn.Left != null)
2328 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2330 Report.Error (71, mn.Location, "Event declaration should use property syntax");
2332 if (RootContext.Documentation != null)
2333 Lexer.doc_state = XmlCommentState.Allowed;
2337 event_accessor_declarations
2338 : add_accessor_declaration remove_accessor_declaration
2340 $$ = new Pair ($1, $2);
2342 | remove_accessor_declaration add_accessor_declaration
2344 $$ = new Pair ($2, $1);
2346 | add_accessor_declaration { $$ = null; }
2347 | remove_accessor_declaration { $$ = null; }
2350 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2356 add_accessor_declaration
2357 : opt_attributes ADD
2359 Parameter [] args = new Parameter [1];
2360 Parameter implicit_value_parameter = new Parameter (
2361 implicit_value_parameter_type, "value",
2362 Parameter.Modifier.NONE, null, (Location) $2);
2364 args [0] = implicit_value_parameter;
2366 current_local_parameters = new Parameters (args);
2367 lexer.EventParsing = false;
2371 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2372 lexer.EventParsing = true;
2374 | opt_attributes ADD error {
2375 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2378 | opt_attributes modifiers ADD {
2379 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2384 remove_accessor_declaration
2385 : opt_attributes REMOVE
2387 Parameter [] args = new Parameter [1];
2388 Parameter implicit_value_parameter = new Parameter (
2389 implicit_value_parameter_type, "value",
2390 Parameter.Modifier.NONE, null, (Location) $2);
2392 args [0] = implicit_value_parameter;
2394 current_local_parameters = new Parameters (args);
2395 lexer.EventParsing = false;
2399 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2400 lexer.EventParsing = true;
2402 | opt_attributes REMOVE error {
2403 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2406 | opt_attributes modifiers REMOVE {
2407 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2413 : opt_attributes opt_modifiers indexer_declarator
2416 IndexerDeclaration decl = (IndexerDeclaration) $3;
2418 implicit_value_parameter_type = decl.type;
2420 lexer.PropertyParsing = true;
2421 parsing_indexer = true;
2423 indexer_parameters = decl.param_list;
2424 iterator_container = SimpleIteratorContainer.GetSimple ();
2426 accessor_declarations
2428 lexer.PropertyParsing = false;
2429 has_get = has_set = false;
2430 parsing_indexer = false;
2437 // The signature is computed from the signature of the indexer. Look
2438 // at section 3.6 on the spec
2440 IndexerDeclaration decl = (IndexerDeclaration) $3;
2441 Pair pair = (Pair) $6;
2442 Accessor get_block = (Accessor) pair.First;
2443 Accessor set_block = (Accessor) pair.Second;
2446 if (decl.interface_type != null)
2447 name = new MemberName (
2448 decl.interface_type, TypeContainer.DefaultIndexerName, decl.location);
2450 name = new MemberName (TypeContainer.DefaultIndexerName, decl.location);
2452 indexer = new Indexer (current_class, decl.type, name,
2453 (int) $2, false, decl.param_list, (Attributes) $1,
2454 get_block, set_block);
2456 if (RootContext.Documentation != null)
2457 indexer.DocComment = ConsumeStoredComment ();
2459 current_container.AddIndexer (indexer);
2461 current_local_parameters = null;
2462 implicit_value_parameter_type = null;
2463 indexer_parameters = null;
2468 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2470 Parameters pars = (Parameters) $4;
2471 if (pars.HasArglist) {
2472 // "__arglist is not valid in this context"
2473 Report.Error (1669, (Location) $2, "__arglist is not valid in this context");
2474 } else if (pars.Empty){
2475 Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
2477 if (RootContext.Documentation != null) {
2478 tmpComment = Lexer.consume_doc_comment ();
2479 Lexer.doc_state = XmlCommentState.Allowed;
2482 $$ = new IndexerDeclaration ((Expression) $1, null, pars, (Location) $2);
2484 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2486 Parameters pars = (Parameters) $6;
2488 if (pars.HasArglist) {
2489 // "__arglist is not valid in this context"
2490 Report.Error (1669, (Location) $4, "__arglist is not valid in this context");
2491 } else if (pars.Empty){
2492 Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
2495 MemberName name = (MemberName) $2;
2496 $$ = new IndexerDeclaration ((Expression) $1, name, pars, (Location) $4);
2498 if (RootContext.Documentation != null) {
2499 tmpComment = Lexer.consume_doc_comment ();
2500 Lexer.doc_state = XmlCommentState.Allowed;
2511 if (RootContext.Documentation != null)
2512 enumTypeComment = Lexer.consume_doc_comment ();
2517 LocatedToken lt = (LocatedToken) $5;
2518 Location enum_location = lt.Location;
2521 Report.Error (267, lt.Location, "The partial modifier can only appear immediately before `class', `struct' or `interface'");
2522 break; // assumes that the parser put us in a switch
2525 MemberName name = MakeName (new MemberName (lt.Value, enum_location));
2526 Enum e = new Enum (current_namespace, current_class, (Expression) $6, (int) $2,
2527 name, (Attributes) $1);
2529 if (RootContext.Documentation != null)
2530 e.DocComment = enumTypeComment;
2533 EnumMember em = null;
2534 foreach (VariableDeclaration ev in (ArrayList) $8) {
2535 em = new EnumMember (e, em, (Expression) ev.expression_or_array_initializer,
2536 new MemberName (ev.identifier, ev.Location), ev.OptAttributes);
2538 // if (RootContext.Documentation != null)
2539 em.DocComment = ev.DocComment;
2541 e.AddEnumMember (em);
2544 current_container.AddEnum (e);
2545 RootContext.Tree.RecordDecl (current_namespace.NS, name, e);
2552 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2553 | COLON type { $$ = $2; }
2559 if (RootContext.Documentation != null)
2560 Lexer.doc_state = XmlCommentState.Allowed;
2562 opt_enum_member_declarations
2564 // here will be evaluated after CLOSE_BLACE is consumed.
2565 if (RootContext.Documentation != null)
2566 Lexer.doc_state = XmlCommentState.Allowed;
2574 opt_enum_member_declarations
2575 : /* empty */ { $$ = new ArrayList (4); }
2576 | enum_member_declarations opt_comma { $$ = $1; }
2579 enum_member_declarations
2580 : enum_member_declaration
2582 ArrayList l = new ArrayList (4);
2587 | enum_member_declarations COMMA enum_member_declaration
2589 ArrayList l = (ArrayList) $1;
2597 enum_member_declaration
2598 : opt_attributes IDENTIFIER
2600 VariableDeclaration vd = new VariableDeclaration (
2601 (LocatedToken) $2, null, (Attributes) $1);
2603 if (RootContext.Documentation != null) {
2604 vd.DocComment = Lexer.consume_doc_comment ();
2605 Lexer.doc_state = XmlCommentState.Allowed;
2610 | opt_attributes IDENTIFIER
2612 if (RootContext.Documentation != null) {
2613 tmpComment = Lexer.consume_doc_comment ();
2614 Lexer.doc_state = XmlCommentState.NotAllowed;
2619 VariableDeclaration vd = new VariableDeclaration (
2620 (LocatedToken) $2, $5, (Attributes) $1);
2622 if (RootContext.Documentation != null)
2623 vd.DocComment = ConsumeStoredComment ();
2629 delegate_declaration
2634 lexer.ConstraintsParsing = true;
2637 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2639 MemberName name = MakeName ((MemberName) $6);
2640 Parameters p = (Parameters) $8;
2642 // TODO: wrong location
2643 Report.Error (1669, name.Location, "__arglist is not valid in this context");
2646 Delegate del = new Delegate (current_namespace, current_class, (Expression) $5,
2647 (int) $2, name, p, (Attributes) $1);
2649 if (RootContext.Documentation != null) {
2650 del.DocComment = Lexer.consume_doc_comment ();
2651 Lexer.doc_state = XmlCommentState.Allowed;
2654 current_container.AddDelegate (del);
2655 RootContext.Tree.RecordDecl (current_namespace.NS, name, del);
2657 current_delegate = del;
2659 opt_type_parameter_constraints_clauses
2661 lexer.ConstraintsParsing = false;
2665 current_delegate.SetParameterInfo ((ArrayList) $11);
2666 $$ = current_delegate;
2668 current_delegate = null;
2675 lexer.CheckNullable (false);
2680 lexer.CheckNullable (true);
2685 namespace_or_type_name
2687 | IDENTIFIER DOUBLE_COLON IDENTIFIER {
2688 LocatedToken lt1 = (LocatedToken) $1;
2689 LocatedToken lt2 = (LocatedToken) $3;
2690 $$ = new MemberName (lt1.Value, lt2.Value, lt2.Location);
2692 | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list {
2693 LocatedToken lt = (LocatedToken) $3;
2694 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $4, lt.Location);
2699 : IDENTIFIER opt_type_argument_list {
2700 LocatedToken lt = (LocatedToken) $1;
2701 $$ = new MemberName (lt.Value, (TypeArguments) $2, lt.Location);
2705 opt_type_argument_list
2706 : /* empty */ { $$ = null; }
2707 | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2710 if (RootContext.Version == LanguageVersion.ISO_1)
2711 Report.FeatureIsNotStandardized (lexer.Location, "generics");
2715 $$ = new TypeArguments ((int) $1, lexer.Location);
2716 if (RootContext.Version == LanguageVersion.ISO_1)
2717 Report.FeatureIsNotStandardized (lexer.Location, "generics");
2722 : opt_attributes type {
2723 TypeArguments type_args = new TypeArguments (lexer.Location);
2725 SimpleName sn = $2 as SimpleName;
2727 Report.Error (1031, lexer.Location, "Type expected");
2729 $2 = new TypeParameterName (sn.Name, (Attributes) $1, lexer.Location);
2731 type_args.Add ((Expression) $2);
2734 | type_arguments COMMA opt_attributes type {
2735 TypeArguments type_args = (TypeArguments) $1;
2737 SimpleName sn = $4 as SimpleName;
2739 Report.Error (1031, lexer.Location, "Type expected");
2741 $4 = new TypeParameterName (sn.Name, (Attributes) $3, lexer.Location);
2743 type_args.Add ((Expression) $4);
2750 * Before you think of adding a return_type, notice that we have been
2751 * using two rules in the places where it matters (one rule using type
2752 * and another identical one that uses VOID as the return type). This
2753 * gets rid of a shift/reduce couple
2756 : namespace_or_type_name opt_nullable
2758 MemberName name = (MemberName) $1;
2759 $$ = name.GetTypeExpression ();
2762 $$ = new ComposedCast ((Expression) $$, "?", lexer.Location);
2764 | builtin_types opt_nullable
2767 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2777 // Note that here only unmanaged types are allowed but we
2778 // can't perform checks during this phase - we do it during
2779 // semantic analysis.
2781 $$ = new ComposedCast ((Expression) $1, "*", Lexer.Location);
2785 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2790 : builtin_types opt_nullable
2793 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2795 | non_expression_type rank_specifier
2797 Location loc = GetLocation ($1);
2799 loc = lexer.Location;
2800 $$ = new ComposedCast ((Expression) $1, (string) $2, loc);
2802 | non_expression_type STAR
2804 Location loc = GetLocation ($1);
2806 loc = lexer.Location;
2807 $$ = new ComposedCast ((Expression) $1, "*", loc);
2809 | expression rank_specifiers
2811 $$ = new ComposedCast ((Expression) $1, (string) $2);
2815 $$ = new ComposedCast ((Expression) $1, "*");
2819 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2820 // through this different path
2822 | multiplicative_expression STAR
2824 $$ = new ComposedCast ((Expression) $1, "*");
2831 ArrayList types = new ArrayList (4);
2836 | type_list COMMA type
2838 ArrayList types = (ArrayList) $1;
2846 * replaces all the productions for isolating the various
2847 * simple types, but we need this to reuse it easily in local_variable_type
2850 : OBJECT { $$ = TypeManager.system_object_expr; }
2851 | STRING { $$ = TypeManager.system_string_expr; }
2852 | BOOL { $$ = TypeManager.system_boolean_expr; }
2853 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2854 | FLOAT { $$ = TypeManager.system_single_expr; }
2855 | DOUBLE { $$ = TypeManager.system_double_expr; }
2860 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2861 | BYTE { $$ = TypeManager.system_byte_expr; }
2862 | SHORT { $$ = TypeManager.system_int16_expr; }
2863 | USHORT { $$ = TypeManager.system_uint16_expr; }
2864 | INT { $$ = TypeManager.system_int32_expr; }
2865 | UINT { $$ = TypeManager.system_uint32_expr; }
2866 | LONG { $$ = TypeManager.system_int64_expr; }
2867 | ULONG { $$ = TypeManager.system_uint64_expr; }
2868 | CHAR { $$ = TypeManager.system_char_expr; }
2869 | VOID { $$ = TypeManager.system_void_expr; }
2873 : type rank_specifiers opt_nullable
2875 string rank_specifiers = (string) $2;
2877 rank_specifiers += "?";
2879 $$ = current_array_type = new ComposedCast ((Expression) $1, rank_specifiers);
2884 // Expressions, section 7.5
2893 MemberName mn = (MemberName) $1;
2894 $$ = mn.GetTypeExpression ();
2896 | IDENTIFIER DOUBLE_COLON IDENTIFIER
2898 LocatedToken lt1 = (LocatedToken) $1;
2899 LocatedToken lt2 = (LocatedToken) $3;
2900 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, lt2.Location);
2902 | parenthesized_expression
2903 | default_value_expression
2905 | invocation_expression
2909 | post_increment_expression
2910 | post_decrement_expression
2914 | checked_expression
2915 | unchecked_expression
2916 | pointer_member_access
2917 | anonymous_method_expression
2924 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
2925 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); }
2926 | NULL { $$ = new NullLiteral (lexer.Location); }
2930 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
2931 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
2932 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
2937 object v = lexer.Value;
2940 $$ = new IntLiteral ((int) v, lexer.Location);
2941 } else if (v is uint)
2942 $$ = new UIntLiteral ((UInt32) v, lexer.Location);
2944 $$ = new LongLiteral ((Int64) v, lexer.Location);
2945 else if (v is ulong)
2946 $$ = new ULongLiteral ((UInt64) v, lexer.Location);
2948 Console.WriteLine ("OOPS. Unexpected result from scanner");
2953 : TRUE { $$ = new BoolLiteral (true, lexer.Location); }
2954 | FALSE { $$ = new BoolLiteral (false, lexer.Location); }
2957 parenthesized_expression_0
2958 : OPEN_PARENS expression CLOSE_PARENS
2961 lexer.Deambiguate_CloseParens ();
2962 // After this, the next token returned is one of
2963 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2964 // or CLOSE_PARENS_MINUS.
2966 | OPEN_PARENS expression error { CheckToken (1026, yyToken, "Expecting ')'", lexer.Location); }
2969 parenthesized_expression
2970 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2974 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2976 // If a parenthesized expression is followed by a minus, we need to wrap
2977 // the expression inside a ParenthesizedExpression for the CS0075 check
2978 // in Binary.DoResolve().
2979 $$ = new ParenthesizedExpression ((Expression) $1);
2984 : primary_expression DOT IDENTIFIER opt_type_argument_list
2986 LocatedToken lt = (LocatedToken) $3;
2987 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4);
2989 | predefined_type DOT IDENTIFIER opt_type_argument_list
2991 LocatedToken lt = (LocatedToken) $3;
2992 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4);
3000 invocation_expression
3001 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
3004 Report.Error (1, (Location) $2, "Parse error");
3006 $$ = new Invocation ((Expression) $1, (ArrayList) $3);
3008 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
3010 $$ = new Invocation ((Expression) $1, new ArrayList ());
3012 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
3014 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
3016 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
3018 ArrayList args = new ArrayList (1);
3020 $$ = new Invocation ((Expression) $1, args);
3022 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
3024 ArrayList args = ((ArrayList) $4);
3026 $$ = new Invocation ((Expression) $1, args);
3031 : /* empty */ { $$ = null; }
3038 ArrayList list = new ArrayList (4);
3042 | argument_list COMMA argument
3044 ArrayList list = (ArrayList) $1;
3048 | argument_list error {
3049 CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
3057 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
3059 | non_simple_argument
3066 : REF variable_reference
3068 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
3070 | OUT variable_reference
3072 $$ = new Argument ((Expression) $2, Argument.AType.Out);
3074 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
3076 ArrayList list = (ArrayList) $3;
3077 Argument[] args = new Argument [list.Count];
3078 list.CopyTo (args, 0);
3080 Expression expr = new Arglist (args, (Location) $1);
3081 $$ = new Argument (expr, Argument.AType.Expression);
3085 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
3090 : expression { note ("section 5.4"); $$ = $1; }
3094 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
3096 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
3098 | primary_expression rank_specifiers
3100 // So the super-trick is that primary_expression
3101 // can only be either a SimpleName or a MemberAccess.
3102 // The MemberAccess case arises when you have a fully qualified type-name like :
3104 // SimpleName is when you have
3107 Expression expr = (Expression) $1;
3108 if (expr is ComposedCast){
3109 $$ = new ComposedCast (expr, (string) $2);
3110 } else if (!(expr is SimpleName || expr is MemberAccess || expr is ConstructedType || expr is QualifiedAliasMember)){
3111 Error_ExpectingTypeName (expr);
3112 $$ = TypeManager.system_object_expr;
3115 // So we extract the string corresponding to the SimpleName
3118 $$ = new ComposedCast (expr, (string) $2);
3120 current_array_type = (Expression)$$;
3127 ArrayList list = new ArrayList (4);
3131 | expression_list COMMA expression
3133 ArrayList list = (ArrayList) $1;
3142 $$ = new This (current_block, (Location) $1);
3147 : BASE DOT IDENTIFIER opt_type_argument_list
3149 LocatedToken lt = (LocatedToken) $3;
3150 $$ = new BaseAccess (lt.Value, (TypeArguments) $4, lt.Location);
3152 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
3154 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
3157 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
3162 post_increment_expression
3163 : primary_expression OP_INC
3165 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
3166 (Expression) $1, (Location) $2);
3170 post_decrement_expression
3171 : primary_expression OP_DEC
3173 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
3174 (Expression) $1, (Location) $2);
3179 : object_or_delegate_creation_expression
3180 | array_creation_expression
3183 object_or_delegate_creation_expression
3184 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
3186 $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
3190 array_creation_expression
3191 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
3193 opt_array_initializer
3195 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
3197 | NEW type rank_specifiers array_initializer
3199 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, (Location) $1);
3203 Report.Error (1031, (Location) $1, "Type expected");
3208 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
3224 opt_rank_specifier_or_nullable
3233 | opt_nullable rank_specifiers
3240 | opt_nullable rank_specifiers INTERR
3243 $$ = "?" + $2 + "?";
3250 : rank_specifier opt_rank_specifier
3252 $$ = (string) $2 + (string) $1;
3257 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3259 $$ = "[" + (string) $2 + "]";
3279 | dim_separators COMMA
3281 $$ = (string) $1 + ",";
3285 opt_array_initializer
3297 : OPEN_BRACE CLOSE_BRACE
3299 ArrayList list = new ArrayList (4);
3302 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3304 $$ = (ArrayList) $2;
3308 variable_initializer_list
3309 : variable_initializer
3311 ArrayList list = new ArrayList (4);
3315 | variable_initializer_list COMMA variable_initializer
3317 ArrayList list = (ArrayList) $1;
3323 void_pointer_expression
3324 : void_pointer_expression STAR
3326 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3330 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3335 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
3337 $$ = new TypeOfVoid ((Location) $1);
3339 | TYPEOF OPEN_PARENS void_pointer_expression CLOSE_PARENS
3341 $$ = new TypeOf ((Expression) $3, (Location) $1);
3343 | TYPEOF OPEN_PARENS
3345 lexer.TypeOfParsing = true;
3349 lexer.TypeOfParsing = false;
3350 $$ = new TypeOf ((Expression) $4, lexer.Location);
3355 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3356 $$ = new SizeOf ((Expression) $3, (Location) $1);
3361 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3363 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3367 unchecked_expression
3368 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3370 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3374 pointer_member_access
3375 : primary_expression OP_PTR IDENTIFIER
3378 LocatedToken lt = (LocatedToken) $3;
3380 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
3381 $$ = new MemberAccess (deref, lt.Value);
3385 anonymous_method_expression
3386 : DELEGATE opt_anonymous_method_signature
3388 if (oob_stack == null)
3389 oob_stack = new Stack (6);
3391 oob_stack.Push (current_local_parameters);
3392 current_local_parameters = (Parameters)$2;
3394 // Force the next block to be created as a ToplevelBlock
3395 oob_stack.Push (current_block);
3396 oob_stack.Push (top_current_block);
3397 current_block = null;
3401 Location loc = (Location) $1;
3402 top_current_block = (Block) oob_stack.Pop ();
3403 current_block = (Block) oob_stack.Pop ();
3404 if (RootContext.Version == LanguageVersion.ISO_1){
3405 Report.FeatureIsNotStandardized (loc, "anonymous methods");
3408 ToplevelBlock anon_block = (ToplevelBlock) $4;
3410 anon_block.Parent = current_block;
3411 $$ = new AnonymousMethod (current_container, (Parameters) $2, (ToplevelBlock) top_current_block,
3414 current_local_parameters = (Parameters) oob_stack.Pop ();
3418 opt_anonymous_method_signature
3419 : /* empty */ { $$ = null; }
3420 | anonymous_method_signature
3423 anonymous_method_signature
3424 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3427 $$ = Parameters.EmptyReadOnlyParameters;
3429 ArrayList par_list = (ArrayList) $2;
3430 Parameter [] pars = new Parameter [par_list.Count];
3431 par_list.CopyTo (pars);
3432 $$ = new Parameters (pars);
3437 opt_anonymous_method_parameter_list
3438 : /* empty */ { $$ = null; }
3439 | anonymous_method_parameter_list { $$ = $1; }
3442 anonymous_method_parameter_list
3443 : anonymous_method_parameter
3445 ArrayList a = new ArrayList (4);
3449 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3451 ArrayList a = (ArrayList) $1;
3457 anonymous_method_parameter
3458 : opt_parameter_modifier type IDENTIFIER {
3459 LocatedToken lt = (LocatedToken) $3;
3460 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3462 | PARAMS type IDENTIFIER {
3463 Report.Error (1670, ((LocatedToken) $3).Location, "The `params' modifier is not allowed in anonymous method declaration");
3468 default_value_expression
3469 : DEFAULT_OPEN_PARENS type CLOSE_PARENS
3471 $$ = new DefaultValueExpression ((Expression) $2, lexer.Location);
3476 : primary_expression
3477 | BANG prefixed_unary_expression
3479 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3481 | TILDE prefixed_unary_expression
3483 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3489 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3491 $$ = new Cast ((Expression) $1, (Expression) $3);
3493 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3495 $$ = new Cast ((Expression) $1, (Expression) $3);
3501 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3503 // TODO: wrong location
3504 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3509 // The idea to split this out is from Rhys' grammar
3510 // to solve the problem with casts.
3512 prefixed_unary_expression
3514 | PLUS prefixed_unary_expression
3516 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3518 | MINUS prefixed_unary_expression
3520 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3522 | OP_INC prefixed_unary_expression
3524 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3525 (Expression) $2, (Location) $1);
3527 | OP_DEC prefixed_unary_expression
3529 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3530 (Expression) $2, (Location) $1);
3532 | STAR prefixed_unary_expression
3534 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, (Location) $1);
3536 | BITWISE_AND prefixed_unary_expression
3538 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3542 pre_increment_expression
3543 : OP_INC prefixed_unary_expression
3545 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3546 (Expression) $2, (Location) $1);
3550 pre_decrement_expression
3551 : OP_DEC prefixed_unary_expression
3553 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3554 (Expression) $2, (Location) $1);
3558 multiplicative_expression
3559 : prefixed_unary_expression
3560 | multiplicative_expression STAR prefixed_unary_expression
3562 $$ = new Binary (Binary.Operator.Multiply,
3563 (Expression) $1, (Expression) $3);
3565 | multiplicative_expression DIV prefixed_unary_expression
3567 $$ = new Binary (Binary.Operator.Division,
3568 (Expression) $1, (Expression) $3);
3570 | multiplicative_expression PERCENT prefixed_unary_expression
3572 $$ = new Binary (Binary.Operator.Modulus,
3573 (Expression) $1, (Expression) $3);
3578 : multiplicative_expression
3579 | additive_expression PLUS multiplicative_expression
3581 $$ = new Binary (Binary.Operator.Addition,
3582 (Expression) $1, (Expression) $3);
3584 | additive_expression MINUS multiplicative_expression
3586 $$ = new Binary (Binary.Operator.Subtraction,
3587 (Expression) $1, (Expression) $3);
3592 : additive_expression
3593 | shift_expression OP_SHIFT_LEFT additive_expression
3595 $$ = new Binary (Binary.Operator.LeftShift,
3596 (Expression) $1, (Expression) $3);
3598 | shift_expression OP_SHIFT_RIGHT additive_expression
3600 $$ = new Binary (Binary.Operator.RightShift,
3601 (Expression) $1, (Expression) $3);
3612 lexer.PutbackNullable ();
3617 nullable_type_or_conditional
3620 if (((bool) $2) && ($1 is ComposedCast))
3621 $$ = ((ComposedCast) $1).RemoveNullable ();
3627 relational_expression
3629 | relational_expression OP_LT shift_expression
3631 $$ = new Binary (Binary.Operator.LessThan,
3632 (Expression) $1, (Expression) $3);
3634 | relational_expression OP_GT shift_expression
3636 $$ = new Binary (Binary.Operator.GreaterThan,
3637 (Expression) $1, (Expression) $3);
3639 | relational_expression OP_LE shift_expression
3641 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3642 (Expression) $1, (Expression) $3);
3644 | relational_expression OP_GE shift_expression
3646 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3647 (Expression) $1, (Expression) $3);
3649 | relational_expression IS
3652 } nullable_type_or_conditional
3654 $$ = new Is ((Expression) $1, (Expression) $4, (Location) $2);
3656 | relational_expression AS
3659 } nullable_type_or_conditional
3661 $$ = new As ((Expression) $1, (Expression) $4, (Location) $2);
3666 : relational_expression
3667 | equality_expression OP_EQ relational_expression
3669 $$ = new Binary (Binary.Operator.Equality,
3670 (Expression) $1, (Expression) $3);
3672 | equality_expression OP_NE relational_expression
3674 $$ = new Binary (Binary.Operator.Inequality,
3675 (Expression) $1, (Expression) $3);
3680 : equality_expression
3681 | and_expression BITWISE_AND equality_expression
3683 $$ = new Binary (Binary.Operator.BitwiseAnd,
3684 (Expression) $1, (Expression) $3);
3688 exclusive_or_expression
3690 | exclusive_or_expression CARRET and_expression
3692 $$ = new Binary (Binary.Operator.ExclusiveOr,
3693 (Expression) $1, (Expression) $3);
3697 inclusive_or_expression
3698 : exclusive_or_expression
3699 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3701 $$ = new Binary (Binary.Operator.BitwiseOr,
3702 (Expression) $1, (Expression) $3);
3706 conditional_and_expression
3707 : inclusive_or_expression
3708 | conditional_and_expression OP_AND inclusive_or_expression
3710 $$ = new Binary (Binary.Operator.LogicalAnd,
3711 (Expression) $1, (Expression) $3);
3715 conditional_or_expression
3716 : conditional_and_expression
3717 | conditional_or_expression OP_OR conditional_and_expression
3719 $$ = new Binary (Binary.Operator.LogicalOr,
3720 (Expression) $1, (Expression) $3);
3724 conditional_expression
3725 : conditional_or_expression
3726 | conditional_or_expression INTERR expression COLON expression
3728 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
3730 | conditional_or_expression INTERR INTERR expression
3732 $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $4, lexer.Location);
3734 // We'll be resolved into a `parenthesized_expression_0' later on.
3735 | conditional_or_expression INTERR CLOSE_PARENS
3737 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
3738 lexer.PutbackCloseParens ();
3742 assignment_expression
3743 : prefixed_unary_expression ASSIGN expression
3745 $$ = new Assign ((Expression) $1, (Expression) $3);
3747 | prefixed_unary_expression OP_MULT_ASSIGN expression
3749 $$ = new CompoundAssign (
3750 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
3752 | prefixed_unary_expression OP_DIV_ASSIGN expression
3754 $$ = new CompoundAssign (
3755 Binary.Operator.Division, (Expression) $1, (Expression) $3);
3757 | prefixed_unary_expression OP_MOD_ASSIGN expression
3759 $$ = new CompoundAssign (
3760 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
3762 | prefixed_unary_expression OP_ADD_ASSIGN expression
3764 $$ = new CompoundAssign (
3765 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
3767 | prefixed_unary_expression OP_SUB_ASSIGN expression
3769 $$ = new CompoundAssign (
3770 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3772 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3774 $$ = new CompoundAssign (
3775 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
3777 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3779 $$ = new CompoundAssign (
3780 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
3782 | prefixed_unary_expression OP_AND_ASSIGN expression
3784 $$ = new CompoundAssign (
3785 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
3787 | prefixed_unary_expression OP_OR_ASSIGN expression
3789 $$ = new CompoundAssign (
3790 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
3792 | prefixed_unary_expression OP_XOR_ASSIGN expression
3794 $$ = new CompoundAssign (
3795 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
3800 : conditional_expression
3801 | assignment_expression
3821 lexer.ConstraintsParsing = true;
3825 MemberName name = MakeName ((MemberName) $6);
3826 int mod_flags = (int) $2;
3828 current_class = new Class (
3829 current_namespace, current_class, name,
3830 mod_flags, (Attributes) $1);
3833 current_container = current_container.AddPartial (current_class);
3835 current_container.AddClassOrStruct (current_class);
3836 current_container = current_class;
3840 opt_type_parameter_constraints_clauses
3842 lexer.ConstraintsParsing = false;
3845 if (current_class.Name == "System.Object") {
3846 Report.Error (537, current_class.Location,
3847 "The class System.Object cannot have a base " +
3848 "class or implement an interface.");
3850 current_class.Bases = (ArrayList) $8;
3853 current_class.SetParameterInfo ((ArrayList) $9);
3855 if (RootContext.Documentation != null) {
3856 current_container.DocComment = Lexer.consume_doc_comment ();
3857 Lexer.doc_state = XmlCommentState.Allowed;
3862 if (RootContext.Documentation != null)
3863 Lexer.doc_state = XmlCommentState.Allowed;
3867 $$ = pop_current_class ();
3875 { $$ = $1; } // location
3879 : /* empty */ { $$ = (int) 0; }
3885 | modifiers modifier
3890 if ((m1 & m2) != 0) {
3891 Location l = lexer.Location;
3892 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
3894 $$ = (int) (m1 | m2);
3899 : NEW { $$ = Modifiers.NEW; }
3900 | PUBLIC { $$ = Modifiers.PUBLIC; }
3901 | PROTECTED { $$ = Modifiers.PROTECTED; }
3902 | INTERNAL { $$ = Modifiers.INTERNAL; }
3903 | PRIVATE { $$ = Modifiers.PRIVATE; }
3904 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3905 | SEALED { $$ = Modifiers.SEALED; }
3906 | STATIC { $$ = Modifiers.STATIC; }
3907 | READONLY { $$ = Modifiers.READONLY; }
3908 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3909 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3910 | EXTERN { $$ = Modifiers.EXTERN; }
3911 | VOLATILE { $$ = Modifiers.VOLATILE; }
3912 | UNSAFE { $$ = Modifiers.UNSAFE; }
3916 : /* empty */ { $$ = null; }
3917 | class_base { $$ = $1; }
3921 : COLON type_list { $$ = $2; }
3924 opt_type_parameter_constraints_clauses
3925 : /* empty */ { $$ = null; }
3926 | type_parameter_constraints_clauses
3930 type_parameter_constraints_clauses
3931 : type_parameter_constraints_clause {
3932 ArrayList constraints = new ArrayList (1);
3933 constraints.Add ($1);
3936 | type_parameter_constraints_clauses type_parameter_constraints_clause {
3937 ArrayList constraints = (ArrayList) $1;
3939 constraints.Add ($2);
3944 type_parameter_constraints_clause
3945 : WHERE IDENTIFIER COLON type_parameter_constraints {
3946 LocatedToken lt = (LocatedToken) $2;
3947 $$ = new Constraints (lt.Value, (ArrayList) $4, lt.Location);
3951 type_parameter_constraints
3952 : type_parameter_constraint {
3953 ArrayList constraints = new ArrayList (1);
3954 constraints.Add ($1);
3957 | type_parameter_constraints COMMA type_parameter_constraint {
3958 ArrayList constraints = (ArrayList) $1;
3960 constraints.Add ($3);
3965 type_parameter_constraint
3967 | NEW OPEN_PARENS CLOSE_PARENS {
3968 $$ = SpecialConstraint.Constructor;
3971 $$ = SpecialConstraint.ReferenceType;
3974 $$ = SpecialConstraint.ValueType;
3983 // A block is "contained" on the following places:
3985 // property_declaration as part of the accessor body (get/set)
3986 // operator_declaration
3987 // constructor_declaration
3988 // destructor_declaration
3989 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3994 if (current_block == null){
3995 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, (Location) $1);
3996 top_current_block = current_block;
3998 current_block = new Block (current_block, (Location) $1, Location.Null);
4001 opt_statement_list CLOSE_BRACE
4003 while (current_block.Implicit)
4004 current_block = current_block.Parent;
4006 current_block.SetEndLocation ((Location) $4);
4007 current_block = current_block.Parent;
4008 if (current_block == null)
4009 top_current_block = null;
4020 | statement_list statement
4024 : declaration_statement
4026 if ($1 != null && (Block) $1 != current_block){
4027 current_block.AddStatement ((Statement) $1);
4028 current_block = (Block) $1;
4031 | valid_declaration_statement
4033 current_block.AddStatement ((Statement) $1);
4038 valid_declaration_statement
4041 | expression_statement
4042 | selection_statement
4043 | iteration_statement
4047 | unchecked_statement
4055 : valid_declaration_statement
4056 | declaration_statement
4058 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4063 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4071 $$ = EmptyStatement.Value;
4078 LocatedToken lt = (LocatedToken) $1;
4079 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
4081 if (current_block.AddLabel (labeled))
4082 current_block.AddStatement (labeled);
4087 declaration_statement
4088 : local_variable_declaration SEMICOLON
4090 current_array_type = null;
4092 DictionaryEntry de = (DictionaryEntry) $1;
4093 Expression e = (Expression) de.Key;
4095 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
4099 | local_constant_declaration SEMICOLON
4101 current_array_type = null;
4103 DictionaryEntry de = (DictionaryEntry) $1;
4105 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
4111 * The following is from Rhys' grammar:
4112 * > Types in local variable declarations must be recognized as
4113 * > expressions to prevent reduce/reduce errors in the grammar.
4114 * > The expressions are converted into types during semantic analysis.
4117 : primary_expression opt_rank_specifier_or_nullable
4119 // FIXME: Do something smart here regarding the composition of the type.
4121 // Ok, the above "primary_expression" is there to get rid of
4122 // both reduce/reduce and shift/reduces in the grammar, it should
4123 // really just be "type_name". If you use type_name, a reduce/reduce
4124 // creeps up. If you use namespace_or_type_name (which is all we need
4125 // really) two shift/reduces appear.
4128 // So the super-trick is that primary_expression
4129 // can only be either a SimpleName or a MemberAccess.
4130 // The MemberAccess case arises when you have a fully qualified type-name like :
4132 // SimpleName is when you have
4135 Expression expr = (Expression) $1;
4136 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType || expr is QualifiedAliasMember)) {
4137 Error_ExpectingTypeName (expr);
4141 // So we extract the string corresponding to the SimpleName
4145 if ((string) $2 == "")
4148 $$ = new ComposedCast ((Expression) $1, (string) $2);
4151 | builtin_types opt_rank_specifier_or_nullable
4153 if ((string) $2 == "")
4156 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
4160 local_variable_pointer_type
4161 : primary_expression STAR
4163 Expression expr = (Expression) $1;
4165 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType || expr is QualifiedAliasMember)) {
4166 Error_ExpectingTypeName (expr);
4170 $$ = new ComposedCast ((Expression) $1, "*");
4172 | builtin_types STAR
4174 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
4178 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
4180 | local_variable_pointer_type STAR
4182 $$ = new ComposedCast ((Expression) $1, "*");
4186 local_variable_declaration
4187 : local_variable_type variable_declarators
4190 $$ = new DictionaryEntry ($1, $2);
4194 | local_variable_pointer_type opt_rank_specifier_or_nullable variable_declarators
4199 if ((string) $2 == "")
4200 t = (Expression) $1;
4202 t = new ComposedCast ((Expression) $1, (string) $2);
4203 $$ = new DictionaryEntry (t, $3);
4209 local_constant_declaration
4210 : CONST local_variable_type constant_declarators
4213 $$ = new DictionaryEntry ($2, $3);
4219 expression_statement
4220 : statement_expression SEMICOLON { $$ = $1; }
4224 // We have to do the wrapping here and not in the case above,
4225 // because statement_expression is used for example in for_statement
4227 statement_expression
4230 Expression expr = (Expression) $1;
4231 ExpressionStatement s = expr as ExpressionStatement;
4233 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
4236 $$ = new StatementExpression (s);
4240 Report.Error (1002, GetLocation ($1), "Expecting `;'");
4245 object_creation_expression
4246 : object_or_delegate_creation_expression
4247 { note ("complain if this is a delegate maybe?"); }
4256 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
4259 Location l = (Location) $1;
4261 $$ = new If ((Expression) $3, (Statement) $5, l);
4263 // FIXME: location for warning should be loc property of $5.
4264 if ($5 == EmptyStatement.Value)
4265 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4268 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
4269 embedded_statement ELSE embedded_statement
4271 Location l = (Location) $1;
4273 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
4275 // FIXME: location for warning should be loc property of $5 and $7.
4276 if ($5 == EmptyStatement.Value)
4277 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4278 if ($7 == EmptyStatement.Value)
4279 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4284 : SWITCH OPEN_PARENS
4286 if (switch_stack == null)
4287 switch_stack = new Stack (2);
4288 switch_stack.Push (current_block);
4290 expression CLOSE_PARENS
4293 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
4294 current_block = (Block) switch_stack.Pop ();
4310 Report.Error (1522, lexer.Location, "Empty switch block");
4318 ArrayList sections = new ArrayList (4);
4323 | switch_sections switch_section
4325 ArrayList sections = (ArrayList) $1;
4335 current_block = current_block.CreateSwitchBlock (lexer.Location);
4339 Block topmost = current_block;
4341 while (topmost.Implicit)
4342 topmost = topmost.Parent;
4343 $$ = new SwitchSection ((ArrayList) $1, topmost);
4350 ArrayList labels = new ArrayList (4);
4355 | switch_labels switch_label
4357 ArrayList labels = (ArrayList) ($1);
4365 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
4366 | DEFAULT COLON { $$ = new SwitchLabel (null, (Location) $2); }
4369 1523, GetLocation ($1),
4370 "The keyword case or default must precede code in switch block");
4382 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
4384 Location l = (Location) $1;
4385 $$ = new While ((Expression) $3, (Statement) $5, l);
4390 : DO embedded_statement
4391 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
4393 Location l = (Location) $1;
4395 $$ = new Do ((Statement) $2, (Expression) $5, l);
4401 opt_for_initializer SEMICOLON
4403 Block assign_block = new Block (current_block);
4404 current_block = assign_block;
4406 if ($3 is DictionaryEntry){
4407 DictionaryEntry de = (DictionaryEntry) $3;
4409 Expression type = (Expression) de.Key;
4410 ArrayList var_declarators = (ArrayList) de.Value;
4412 foreach (VariableDeclaration decl in var_declarators){
4416 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4420 Location l = lexer.Location;
4421 Expression expr = decl.expression_or_array_initializer;
4423 LocalVariableReference var;
4424 var = new LocalVariableReference (assign_block, decl.identifier, l);
4427 Assign a = new Assign (var, expr, decl.Location);
4429 assign_block.AddStatement (new StatementExpression (a));
4433 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4434 // This can be referred to as $5 below.
4440 opt_for_condition SEMICOLON
4441 opt_for_iterator CLOSE_PARENS
4444 Location l = (Location) $1;
4446 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4448 current_block.AddStatement (f);
4449 while (current_block.Implicit)
4450 current_block = current_block.Parent;
4452 current_block = current_block.Parent;
4457 : /* empty */ { $$ = EmptyStatement.Value; }
4462 : local_variable_declaration
4463 | statement_expression_list
4467 : /* empty */ { $$ = null; }
4468 | boolean_expression
4472 : /* empty */ { $$ = EmptyStatement.Value; }
4477 : statement_expression_list
4480 statement_expression_list
4481 : statement_expression
4483 // CHANGE: was `null'
4484 Statement s = (Statement) $1;
4485 Block b = new Block (current_block, Block.Flags.Implicit, s.loc, lexer.Location);
4490 | statement_expression_list COMMA statement_expression
4492 Block b = (Block) $1;
4494 b.AddStatement ((Statement) $3);
4500 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4502 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4505 | FOREACH OPEN_PARENS type IDENTIFIER IN
4506 expression CLOSE_PARENS
4508 Block foreach_block = new Block (current_block);
4509 current_block = foreach_block;
4511 LocatedToken lt = (LocatedToken) $4;
4512 Location l = lt.Location;
4515 vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4517 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4519 // Get a writable reference to this read-only variable.
4521 // Note that the $$ here refers to the value of _this_ code block,
4522 // not the value of the LHS non-terminal. This can be referred to as $8 below.
4523 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
4530 LocalVariableReference v = (LocalVariableReference) $8;
4531 Location l = (Location) $1;
4534 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
4535 current_block.AddStatement (f);
4538 while (current_block.Implicit)
4539 current_block = current_block.Parent;
4541 current_block = current_block.Parent;
4547 | continue_statement
4557 $$ = new Break ((Location) $1);
4562 : CONTINUE SEMICOLON
4564 $$ = new Continue ((Location) $1);
4569 : GOTO IDENTIFIER SEMICOLON
4571 LocatedToken lt = (LocatedToken) $2;
4572 $$ = new Goto (lt.Value, lt.Location);
4574 | GOTO CASE constant_expression SEMICOLON
4576 $$ = new GotoCase ((Expression) $3, (Location) $1);
4578 | GOTO DEFAULT SEMICOLON
4580 $$ = new GotoDefault ((Location) $1);
4585 : RETURN opt_expression SEMICOLON
4587 $$ = new Return ((Expression) $2, (Location) $1);
4592 : THROW opt_expression SEMICOLON
4594 $$ = new Throw ((Expression) $2, (Location) $1);
4599 : IDENTIFIER RETURN expression SEMICOLON
4601 LocatedToken lt = (LocatedToken) $1;
4602 string s = lt.Value;
4604 Report.Error (1003, lt.Location, "; expected");
4607 if (RootContext.Version == LanguageVersion.ISO_1){
4608 Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4611 if (iterator_container == null){
4612 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4615 iterator_container.SetYields ();
4616 $$ = new Yield ((Expression) $3, lt.Location);
4619 | IDENTIFIER RETURN SEMICOLON
4621 Report.Error (1627, (Location) $2, "Expression expected after yield return");
4624 | IDENTIFIER BREAK SEMICOLON
4626 LocatedToken lt = (LocatedToken) $1;
4627 string s = lt.Value;
4629 Report.Error (1003, lt.Location, "; expected");
4632 if (RootContext.Version == LanguageVersion.ISO_1){
4633 Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4636 if (iterator_container == null){
4637 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4640 iterator_container.SetYields ();
4641 $$ = new YieldBreak (lt.Location);
4652 : TRY block catch_clauses
4656 ArrayList c = (ArrayList)$3;
4657 for (int i = 0; i < c.Count; ++i) {
4658 Catch cc = (Catch) c [i];
4660 if (i != c.Count - 1)
4661 Report.Error (1017, cc.loc, "Try statement already has an empty catch block");
4668 // Now s contains the list of specific catch clauses
4669 // and g contains the general one.
4671 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4673 | TRY block opt_catch_clauses FINALLY block
4676 ArrayList s = new ArrayList (4);
4677 ArrayList catch_list = (ArrayList) $3;
4679 if (catch_list != null){
4680 foreach (Catch cc in catch_list) {
4688 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4692 Report.Error (1524, (Location) $1, "Expected catch or finally");
4698 : /* empty */ { $$ = null; }
4705 ArrayList l = new ArrayList (4);
4710 | catch_clauses catch_clause
4712 ArrayList l = (ArrayList) $1;
4720 : /* empty */ { $$ = null; }
4725 : CATCH opt_catch_args
4727 Expression type = null;
4730 DictionaryEntry cc = (DictionaryEntry) $2;
4731 type = (Expression) cc.Key;
4732 LocatedToken lt = (LocatedToken) cc.Value;
4735 ArrayList one = new ArrayList (4);
4737 one.Add (new VariableDeclaration (lt, null));
4739 current_block = new Block (current_block);
4740 Block b = declare_local_variables (type, one, lt.Location);
4745 Expression type = null;
4747 Block var_block = null;
4750 DictionaryEntry cc = (DictionaryEntry) $2;
4751 type = (Expression) cc.Key;
4752 LocatedToken lt = (LocatedToken) cc.Value;
4756 while (current_block.Implicit)
4757 current_block = current_block.Parent;
4758 var_block = current_block;
4759 current_block = current_block.Parent;
4763 $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
4768 : /* empty */ { $$ = null; }
4773 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4775 $$ = new DictionaryEntry ($2, $3);
4783 $$ = new Checked ((Block) $2);
4790 $$ = new Unchecked ((Block) $2);
4797 RootContext.CheckUnsafeOption ((Location) $1);
4799 $$ = new Unsafe ((Block) $3);
4805 type fixed_pointer_declarators
4808 ArrayList list = (ArrayList) $4;
4809 Expression type = (Expression) $3;
4810 Location l = (Location) $1;
4811 int top = list.Count;
4813 Block assign_block = new Block (current_block);
4814 current_block = assign_block;
4816 for (int i = 0; i < top; i++){
4817 Pair p = (Pair) list [i];
4820 v = current_block.AddVariable (type, (string) p.First, l);
4824 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4832 Location l = (Location) $1;
4834 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4836 current_block.AddStatement (f);
4837 while (current_block.Implicit)
4838 current_block = current_block.Parent;
4840 current_block = current_block.Parent;
4844 fixed_pointer_declarators
4845 : fixed_pointer_declarator {
4846 ArrayList declarators = new ArrayList (4);
4848 declarators.Add ($1);
4851 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4853 ArrayList declarators = (ArrayList) $1;
4855 declarators.Add ($3);
4860 fixed_pointer_declarator
4861 : IDENTIFIER ASSIGN expression
4863 LocatedToken lt = (LocatedToken) $1;
4864 // FIXME: keep location
4865 $$ = new Pair (lt.Value, $3);
4869 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
4875 : LOCK OPEN_PARENS expression CLOSE_PARENS
4881 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
4886 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4888 Block assign_block = new Block (current_block);
4889 current_block = assign_block;
4891 if ($3 is DictionaryEntry){
4892 DictionaryEntry de = (DictionaryEntry) $3;
4893 Location l = (Location) $1;
4895 Expression type = (Expression) de.Key;
4896 ArrayList var_declarators = (ArrayList) de.Value;
4898 ArrayList vars = new ArrayList (4);
4900 foreach (VariableDeclaration decl in var_declarators){
4902 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4905 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
4907 Expression expr = decl.expression_or_array_initializer;
4909 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4912 LocalVariableReference var;
4914 // Get a writable reference to this read-only variable.
4915 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4917 // This is so that it is not a warning on using variables
4920 vars.Add (new DictionaryEntry (var, expr));
4922 // Assign a = new Assign (var, expr, decl.Location);
4923 // assign_block.AddStatement (new StatementExpression (a));
4926 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4927 // It can be referred to as $5 below.
4928 $$ = new DictionaryEntry (type, vars);
4935 Using u = new Using ($5, (Statement) $6, (Location) $1);
4936 current_block.AddStatement (u);
4937 while (current_block.Implicit)
4938 current_block = current_block.Parent;
4940 current_block = current_block.Parent;
4944 resource_acquisition
4945 : local_variable_declaration
4952 // A class used to pass around variable declarations and constants
4954 public class VariableDeclaration {
4955 public string identifier;
4956 public Expression expression_or_array_initializer;
4957 public Location Location;
4958 public Attributes OptAttributes;
4959 public string DocComment;
4961 public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
4963 this.identifier = lt.Value;
4964 if (eoai is ArrayList) {
4965 if (CSharpParser.current_array_type == null)
4966 Report.Error (622, lt.Location,
4967 "Can only use array initializer expressions to assign to array types. Try using a new expression instead.");
4968 this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
4970 this.expression_or_array_initializer = (Expression)eoai;
4972 this.Location = lt.Location;
4973 this.OptAttributes = opt_attrs;
4976 public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
4982 // A class used to hold info about an indexer declarator
4984 public class IndexerDeclaration {
4985 public Expression type;
4986 public MemberName interface_type;
4987 public Parameters param_list;
4988 public Location location;
4990 public IndexerDeclaration (Expression type, MemberName interface_type,
4991 Parameters param_list, Location loc)
4994 this.interface_type = interface_type;
4995 this.param_list = param_list;
4996 this.location = loc;
5001 // We use this when we do not have an object in advance that is an IIteratorContainer
5003 public class SimpleIteratorContainer : IIteratorContainer {
5006 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
5011 public static SimpleIteratorContainer GetSimple () {
5012 Simple.Yields = false;
5016 public void SetYields () { Yields = true; }
5020 // A class used to hold info about an operator declarator
5022 public class OperatorDeclaration {
5023 public Operator.OpType optype;
5024 public Expression ret_type, arg1type, arg2type;
5025 public string arg1name, arg2name;
5026 public Location location;
5028 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
5029 Expression arg1type, string arg1name,
5030 Expression arg2type, string arg2name, Location location)
5033 this.ret_type = ret_type;
5034 this.arg1type = arg1type;
5035 this.arg1name = arg1name;
5036 this.arg2type = arg2type;
5037 this.arg2name = arg2name;
5038 this.location = location;
5043 void Error_ExpectingTypeName (Expression expr)
5045 if (expr is Invocation){
5046 Report.Error (1002, expr.Location, "Expecting `;'");
5048 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
5052 TypeContainer pop_current_class ()
5054 TypeContainer retval = current_class;
5056 current_class = (TypeContainer) current_class.Parent;
5057 current_container = current_class.PartialContainer;
5063 // Given the @class_name name, it creates a fully qualified name
5064 // based on the containing declaration space
5067 MakeName (MemberName class_name)
5069 Namespace ns = current_namespace.NS;
5071 if (current_container.Name.Length == 0){
5072 if (ns.Name.Length != 0)
5073 return new MemberName (ns.MemberName, class_name);
5077 return new MemberName (current_container.MemberName, class_name);
5081 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
5083 Block implicit_block;
5084 ArrayList inits = null;
5087 // We use the `Used' property to check whether statements
5088 // have been added to the current block. If so, we need
5089 // to create another block to contain the new declaration
5090 // otherwise, as an optimization, we use the same block to
5091 // add the declaration.
5093 // FIXME: A further optimization is to check if the statements
5094 // that were added were added as part of the initialization
5095 // below. In which case, no other statements have been executed
5096 // and we might be able to reduce the number of blocks for
5097 // situations like this:
5099 // int j = 1; int k = j + 1;
5101 if (current_block.Used)
5102 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
5104 implicit_block = current_block;
5106 foreach (VariableDeclaration decl in variable_declarators){
5108 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
5109 if (decl.expression_or_array_initializer != null){
5111 inits = new ArrayList (4);
5118 return implicit_block;
5120 foreach (VariableDeclaration decl in inits){
5122 Expression expr = decl.expression_or_array_initializer;
5124 LocalVariableReference var;
5125 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
5127 assign = new Assign (var, expr, decl.Location);
5129 implicit_block.AddStatement (new StatementExpression (assign));
5132 return implicit_block;
5135 Block declare_local_constants (Expression type, ArrayList declarators)
5137 Block implicit_block;
5139 if (current_block.Used)
5140 implicit_block = new Block (current_block, Block.Flags.Implicit);
5142 implicit_block = current_block;
5144 foreach (VariableDeclaration decl in declarators){
5145 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
5148 return implicit_block;
5151 void CheckAttributeTarget (string a, Location l)
5155 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
5159 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
5165 void CheckUnaryOperator (Operator.OpType op, Location l)
5169 case Operator.OpType.LogicalNot:
5170 case Operator.OpType.OnesComplement:
5171 case Operator.OpType.Increment:
5172 case Operator.OpType.Decrement:
5173 case Operator.OpType.True:
5174 case Operator.OpType.False:
5175 case Operator.OpType.Addition:
5176 case Operator.OpType.Subtraction:
5181 Report.Error (1019, l, "Overloadable unary operator expected");
5187 void CheckBinaryOperator (Operator.OpType op, Location l)
5191 case Operator.OpType.Addition:
5192 case Operator.OpType.Subtraction:
5193 case Operator.OpType.Multiply:
5194 case Operator.OpType.Division:
5195 case Operator.OpType.Modulus:
5196 case Operator.OpType.BitwiseAnd:
5197 case Operator.OpType.BitwiseOr:
5198 case Operator.OpType.ExclusiveOr:
5199 case Operator.OpType.LeftShift:
5200 case Operator.OpType.RightShift:
5201 case Operator.OpType.Equality:
5202 case Operator.OpType.Inequality:
5203 case Operator.OpType.GreaterThan:
5204 case Operator.OpType.LessThan:
5205 case Operator.OpType.GreaterThanOrEqual:
5206 case Operator.OpType.LessThanOrEqual:
5210 Report.Error (1020, l, "Overloadable binary operator expected");
5216 void syntax_error (Location l, string msg)
5218 Report.Error (1003, l, "Syntax error, " + msg);
5221 void note (string s)
5223 // Used to put annotations
5228 public Tokenizer Lexer {
5234 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
5236 current_namespace = new NamespaceEntry (null, file, null);
5237 this.name = file.Name;
5239 current_container = RootContext.Tree.Types;
5240 // TODO: Make RootContext.Tree.Types a PartialContainer.
5241 current_class = current_container;
5242 current_container.NamespaceEntry = current_namespace;
5244 lexer = new Tokenizer (reader, file, defines);
5247 public void parse ()
5249 int errors = Report.Errors;
5251 if (yacc_verbose_flag > 1)
5252 yyparse (lexer, new yydebug.yyDebugSimple ());
5255 Tokenizer tokenizer = lexer as Tokenizer;
5256 tokenizer.cleanup ();
5257 } catch (Exception e){
5259 // Removed for production use, use parser verbose to get the output.
5261 // Console.WriteLine (e);
5262 if (Report.Errors == errors)
5263 Report.Error (-25, lexer.Location, "Parsing error");
5264 if (yacc_verbose_flag > 0)
5265 Console.WriteLine (e);
5268 RootContext.Tree.Types.NamespaceEntry = null;
5271 static void CheckToken (int error, int yyToken, string msg, Location loc)
5273 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
5274 Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ());
5276 Report.Error (error, loc, msg);
5279 void CheckIdentifierToken (int yyToken, Location loc)
5281 CheckToken (1041, yyToken, "Identifier expected", loc);
5284 string ConsumeStoredComment ()
5286 string s = tmpComment;
5288 Lexer.doc_state = XmlCommentState.Allowed;
5292 Location GetLocation (object obj)
5294 if (obj is MemberCore)
5295 return ((MemberCore) obj).Location;
5296 if (obj is MemberName)
5297 return ((MemberName) obj).Location;
5298 if (obj is LocatedToken)
5299 return ((LocatedToken) obj).Location;
5300 if (obj is Location)
5301 return (Location) obj;
5302 return lexer.Location;