3 // cs-parser.jay: The Parser for the C# compiler
5 // Authors: Miguel de Icaza (miguel@gnu.org)
6 // Ravi Pratap (ravi@ximian.com)
8 // Licensed under the terms of the GNU GPL
10 // (C) 2001 Ximian, Inc (http://www.ximian.com)
11 // (C) 2004 Novell, Inc
14 // (1) Figure out why error productions dont work. `type-declaration' is a
15 // great spot to put an `error' because you can reproduce it with this input:
18 // Possible optimization:
19 // Run memory profiler with parsing only, and consider dropping
20 // arraylists where not needed. Some pieces can use linked lists.
28 using System.Collections;
33 public class CSharpParser {
34 NamespaceEntry current_namespace;
35 TypeContainer current_container;
36 DeclSpace current_class;
38 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;
66 Expression pushed_current_array_type;
69 /// Used to determine if we are parsing the get/set pair
70 /// of an indexer or a property
75 /// An out-of-band stack.
84 static public int yacc_verbose_flag;
86 // Name of the file we are parsing
95 /// Temporary Xml documentation cache.
96 /// For enum types, we need one more temporary store.
99 string enumTypeComment;
101 /// Current attribute target
102 string current_attr_target;
104 /// assembly and module attribute definitions are enabled
105 bool global_attrs_enabled = true;
106 bool has_get, has_set;
111 %token NONE /* This token is never returned by our lexer */
112 %token ERROR // This is used not by the parser, but by the tokenizer.
116 *These are the C# keywords
203 /* C# keywords which are not really keywords */
209 /* C# single character operators/punctuation. */
210 %token OPEN_BRACE "{"
211 %token CLOSE_BRACE "}"
212 %token OPEN_BRACKET "["
213 %token CLOSE_BRACKET "]"
214 %token OPEN_PARENS "("
215 %token CLOSE_PARENS ")"
227 %token OP_GENERICS_LT "<"
229 %token OP_GENERICS_GT ">"
230 %token BITWISE_AND "&"
231 %token BITWISE_OR "|"
238 /* C# multi-character operators. */
239 %token DOUBLE_COLON "::"
242 %token OP_SHIFT_LEFT "<<"
243 %token OP_SHIFT_RIGHT ">>"
250 %token OP_MULT_ASSIGN "*="
251 %token OP_DIV_ASSIGN "/="
252 %token OP_MOD_ASSIGN "%="
253 %token OP_ADD_ASSIGN "+="
254 %token OP_SUB_ASSIGN "-="
255 %token OP_SHIFT_LEFT_ASSIGN "<<="
256 %token OP_SHIFT_RIGHT_ASSIGN ">>="
257 %token OP_AND_ASSIGN "&="
258 %token OP_XOR_ASSIGN "^="
259 %token OP_OR_ASSIGN "|="
263 %token LITERAL_INTEGER "int literal"
264 %token LITERAL_FLOAT "float literal"
265 %token LITERAL_DOUBLE "double literal"
266 %token LITERAL_DECIMAL "decimal literal"
267 %token LITERAL_CHARACTER "character literal"
268 %token LITERAL_STRING "string literal"
271 %token CLOSE_PARENS_CAST
272 %token CLOSE_PARENS_NO_CAST
273 %token CLOSE_PARENS_OPEN_PARENS
274 %token CLOSE_PARENS_MINUS
275 %token DEFAULT_OPEN_PARENS
276 %token GENERIC_DIMENSION
278 /* Add precedence rules to solve dangling else s/r conflict */
287 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
289 %left STAR DIV PERCENT
290 %right BANG CARRET UMINUS
291 %nonassoc OP_INC OP_DEC
293 %left OPEN_BRACKET OPEN_BRACE
297 %start compilation_unit
301 : outer_declarations opt_EOF
302 | outer_declarations global_attributes opt_EOF
303 | global_attributes opt_EOF
304 | opt_EOF /* allow empty files */
310 Lexer.check_incorrect_doc_comment ();
314 Lexer.check_incorrect_doc_comment ();
320 | outer_declarations outer_declaration
324 : extern_alias_directive
326 | namespace_member_declaration
329 extern_alias_directives
330 : extern_alias_directive
331 | extern_alias_directives extern_alias_directive;
333 extern_alias_directive
334 : EXTERN IDENTIFIER IDENTIFIER SEMICOLON
336 LocatedToken lt = (LocatedToken) $2;
339 Report.Error (1003, lt.Location, "'alias' expected");
340 } else if (RootContext.Version == LanguageVersion.ISO_1) {
341 Report.FeatureIsNotStandardized (lt.Location, "external alias");
343 lt = (LocatedToken) $3;
344 current_namespace.UsingExternalAlias (lt.Value, lt.Location);
351 | using_directives using_directive
355 : using_alias_directive
357 if (RootContext.Documentation != null)
358 Lexer.doc_state = XmlCommentState.Allowed;
360 | using_namespace_directive
362 if (RootContext.Documentation != null)
363 Lexer.doc_state = XmlCommentState.Allowed;
367 using_alias_directive
368 : USING IDENTIFIER ASSIGN
369 namespace_or_type_name SEMICOLON
371 LocatedToken lt = (LocatedToken) $2;
372 current_namespace.UsingAlias (lt.Value, (MemberName) $4, (Location) $1);
375 CheckIdentifierToken (yyToken, GetLocation ($2));
379 using_namespace_directive
380 : USING namespace_name SEMICOLON
382 current_namespace.Using ((MemberName) $2, (Location) $1);
387 // Strictly speaking, namespaces don't have attributes but
388 // we parse global attributes along with namespace declarations and then
391 namespace_declaration
392 : opt_attributes NAMESPACE namespace_or_type_name
394 MemberName name = (MemberName) $3;
397 Report.Error(1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
400 if (name.TypeArguments != null)
401 syntax_error (lexer.Location, "namespace name expected");
403 current_namespace = new NamespaceEntry (
404 current_namespace, file, name.GetName ());
405 current_class = current_namespace.SlaveDeclSpace;
406 current_container = current_class.PartialContainer;
408 namespace_body opt_semicolon
410 current_namespace = current_namespace.Parent;
411 current_class = current_namespace.SlaveDeclSpace;
412 current_container = current_class.PartialContainer;
427 : namespace_or_type_name {
428 MemberName name = (MemberName) $1;
430 if (name.TypeArguments != null)
431 syntax_error (lexer.Location, "namespace name expected");
440 if (RootContext.Documentation != null)
441 Lexer.doc_state = XmlCommentState.Allowed;
443 opt_extern_alias_directives
445 opt_namespace_member_declarations
454 opt_extern_alias_directives
456 | extern_alias_directives
459 opt_namespace_member_declarations
461 | namespace_member_declarations
464 namespace_member_declarations
465 : namespace_member_declaration
466 | namespace_member_declarations namespace_member_declaration
469 namespace_member_declaration
473 DeclSpace ds = (DeclSpace)$1;
475 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
476 Report.Error (1527, ds.Location,
477 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
480 current_namespace.DeclarationFound = true;
482 | namespace_declaration {
483 current_namespace.DeclarationFound = true;
486 | field_declaration {
487 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
489 | method_declaration {
490 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
497 | interface_declaration
499 | delegate_declaration
501 // Enable this when we have handled all errors, because this acts as a generic fallback
504 // Console.WriteLine ("Token=" + yyToken);
505 // Report.Error (1518, GetLocation ($1), "Expected class, struct, interface, enum or delegate");
517 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
525 global_attrs_enabled = false;
530 global_attrs_enabled = false;
539 ArrayList sect = (ArrayList) $1;
541 if (global_attrs_enabled) {
542 if (current_attr_target == "module") {
543 CodeGen.Module.AddAttributes (sect);
545 } else if (current_attr_target != null && current_attr_target.Length > 0) {
546 CodeGen.Assembly.AddAttributes (sect);
549 $$ = new Attributes (sect);
552 if (RootContext.Documentation != null) {
553 Lexer.check_incorrect_doc_comment ();
555 XmlCommentState.Allowed;
559 $$ = new Attributes (sect);
561 current_attr_target = null;
563 | attribute_sections attribute_section
565 Attributes attrs = $1 as Attributes;
566 ArrayList sect = (ArrayList) $2;
568 if (global_attrs_enabled) {
569 if (current_attr_target == "module") {
570 CodeGen.Module.AddAttributes (sect);
572 } else if (current_attr_target == "assembly") {
573 CodeGen.Assembly.AddAttributes (sect);
577 attrs = new Attributes (sect);
579 attrs.AddAttributes (sect);
583 attrs = new Attributes (sect);
585 attrs.AddAttributes (sect);
588 current_attr_target = null;
593 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
597 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
603 attribute_target_specifier
604 : attribute_target COLON
606 current_attr_target = (string)$1;
614 LocatedToken lt = (LocatedToken) $1;
615 CheckAttributeTarget (lt.Value, lt.Location);
616 $$ = lt.Value; // Location won't be required anymore.
618 | EVENT { $$ = "event"; }
619 | RETURN { $$ = "return"; }
625 ArrayList attrs = new ArrayList (4);
631 | attribute_list COMMA attribute
633 ArrayList attrs = (ArrayList) $1;
641 : attribute_name opt_attribute_arguments
643 MemberName mname = (MemberName) $1;
644 if (mname.IsGeneric) {
645 Report.Error (404, lexer.Location,
646 "'<' unexpected: attributes cannot be generic");
649 object [] arguments = (object []) $2;
650 MemberName left = mname.Left;
651 string identifier = mname.Name;
653 Expression left_expr = left == null ? null : left.GetTypeExpression ();
655 if (current_attr_target == "assembly" || current_attr_target == "module")
656 // FIXME: supply "nameEscaped" parameter here.
657 $$ = new GlobalAttribute (current_namespace, current_attr_target,
658 left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
660 $$ = new Attribute (current_attr_target, left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
665 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
668 opt_attribute_arguments
669 : /* empty */ { $$ = null; }
670 | OPEN_PARENS attribute_arguments CLOSE_PARENS
678 : opt_positional_argument_list
683 $$ = new object [] { $1, null };
686 | positional_argument_list COMMA named_argument_list
688 $$ = new object[] { $1, $3 };
690 | named_argument_list
692 $$ = new object [] { null, $1 };
697 opt_positional_argument_list
698 : /* empty */ { $$ = null; }
699 | positional_argument_list
702 positional_argument_list
705 ArrayList args = new ArrayList (4);
706 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
710 | positional_argument_list COMMA expression
712 ArrayList args = (ArrayList) $1;
713 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
722 ArrayList args = new ArrayList (4);
727 | named_argument_list COMMA named_argument
729 ArrayList args = (ArrayList) $1;
734 | named_argument_list COMMA expression
736 Report.Error (1016, ((Expression) $3).Location, "Named attribute argument expected");
742 : IDENTIFIER ASSIGN expression
744 // FIXME: keep location
745 $$ = new DictionaryEntry (
746 ((LocatedToken) $1).Value,
747 new Argument ((Expression) $3, Argument.AType.Expression));
753 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
756 opt_class_member_declarations
758 | class_member_declarations
761 class_member_declarations
762 : class_member_declaration
763 | class_member_declarations
764 class_member_declaration
767 class_member_declaration
768 : constant_declaration // done
769 | field_declaration // done
770 | method_declaration // done
771 | property_declaration // done
772 | event_declaration // done
773 | indexer_declaration // done
774 | operator_declaration // done
775 | constructor_declaration // done
776 | destructor_declaration // done
786 lexer.ConstraintsParsing = true;
790 MemberName name = MakeName ((MemberName) $6);
791 push_current_class (new Struct (
792 current_namespace, current_class, name, (int) $2,
793 (Attributes) $1), false, $3);
796 opt_type_parameter_constraints_clauses
798 lexer.ConstraintsParsing = false;
801 current_container.AddBasesForPart (current_class, (ArrayList) $8);
803 current_class.SetParameterInfo ((ArrayList) $9);
805 if (RootContext.Documentation != null)
806 current_container.DocComment = Lexer.consume_doc_comment ();
810 if (RootContext.Documentation != null)
811 Lexer.doc_state = XmlCommentState.Allowed;
815 $$ = pop_current_class ();
817 | opt_attributes opt_modifiers opt_partial STRUCT error {
818 CheckIdentifierToken (yyToken, GetLocation ($5));
825 if (RootContext.Documentation != null)
826 Lexer.doc_state = XmlCommentState.Allowed;
828 opt_struct_member_declarations CLOSE_BRACE
831 opt_struct_member_declarations
833 | struct_member_declarations
836 struct_member_declarations
837 : struct_member_declaration
838 | struct_member_declarations struct_member_declaration
841 struct_member_declaration
842 : constant_declaration
845 | property_declaration
847 | indexer_declaration
848 | operator_declaration
849 | constructor_declaration
853 * This is only included so we can flag error 575:
854 * destructors only allowed on class types
856 | destructor_declaration
867 int modflags = (int) $2;
868 foreach (VariableDeclaration constant in (ArrayList) $5){
869 Location l = constant.Location;
870 if ((modflags & Modifiers.STATIC) != 0) {
871 Report.Error (504, l, "The constant `{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
875 Const c = new Const (
876 current_class, (Expression) $4, (string) constant.identifier,
877 (Expression) constant.expression_or_array_initializer, modflags,
880 if (RootContext.Documentation != null) {
881 c.DocComment = Lexer.consume_doc_comment ();
882 Lexer.doc_state = XmlCommentState.Allowed;
884 current_container.AddConstant (c);
890 : constant_declarator
892 ArrayList constants = new ArrayList (4);
897 | constant_declarators COMMA constant_declarator
900 ArrayList constants = (ArrayList) $1;
907 : IDENTIFIER ASSIGN constant_expression
909 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
913 // A const field requires a value to be provided
914 Report.Error (145, ((LocatedToken) $1).Location, "A const field requires a value to be provided");
926 Expression type = (Expression) $3;
929 current_array_type = null;
931 foreach (VariableDeclaration var in (ArrayList) $4){
932 Field field = new Field (current_class, type, mod, var.identifier,
933 (Attributes) $1, var.Location);
935 field.Initializer = var.expression_or_array_initializer;
937 if (RootContext.Documentation != null) {
938 field.DocComment = Lexer.consume_doc_comment ();
939 Lexer.doc_state = XmlCommentState.Allowed;
941 current_container.AddField (field);
942 $$ = field; // FIXME: might be better if it points to the top item
949 fixed_variable_declarators
952 Expression type = (Expression) $4;
955 current_array_type = null;
957 foreach (VariableDeclaration var in (ArrayList) $5) {
958 FixedField field = new FixedField (current_class, type, mod, var.identifier,
959 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
961 if (RootContext.Documentation != null) {
962 field.DocComment = Lexer.consume_doc_comment ();
963 Lexer.doc_state = XmlCommentState.Allowed;
965 current_container.AddField (field);
966 $$ = field; // FIXME: might be better if it points to the top item
974 current_array_type = null;
975 Report.Error (670, (Location) $3, "Fields cannot have void type");
979 fixed_variable_declarators
980 : fixed_variable_declarator
982 ArrayList decl = new ArrayList (2);
986 | fixed_variable_declarators COMMA fixed_variable_declarator
988 ArrayList decls = (ArrayList) $1;
994 fixed_variable_declarator
995 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
997 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
999 | IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1001 Report.Error (443, lexer.Location, "Value or constant expected");
1002 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1006 variable_declarators
1007 : variable_declarator
1009 ArrayList decl = new ArrayList (4);
1014 | variable_declarators COMMA variable_declarator
1016 ArrayList decls = (ArrayList) $1;
1023 : IDENTIFIER ASSIGN variable_initializer
1025 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1029 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1031 | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
1033 Report.Error (650, ((LocatedToken) $1).Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
1034 "To declare a fixed size buffer field, use the fixed keyword before the field type");
1039 variable_initializer
1048 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
1050 $$ = new StackAlloc ((Expression) $2, (Expression) $4, (Location) $1);
1054 $$ = new ArglistAccess ((Location) $1);
1058 Report.Error (1575, (Location) $1, "A stackalloc expression requires [] after type");
1065 iterator_container = (IIteratorContainer) $1;
1066 if (RootContext.Documentation != null)
1067 Lexer.doc_state = XmlCommentState.NotAllowed;
1071 Method method = (Method) $1;
1072 method.Block = (ToplevelBlock) $3;
1073 current_container.AddMethod (method);
1075 current_local_parameters = null;
1076 iterator_container = null;
1078 if (RootContext.Documentation != null)
1079 Lexer.doc_state = XmlCommentState.Allowed;
1092 Report.Error (1585, lexer.Location,
1093 "Member modifier `{0}' must precede the member type and name",
1094 Modifiers.Name (i));
1105 type namespace_or_type_name
1106 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1108 lexer.ConstraintsParsing = true;
1110 opt_type_parameter_constraints_clauses
1112 lexer.ConstraintsParsing = false;
1114 MemberName name = (MemberName) $4;
1116 if ($9 != null && name.TypeArguments == null)
1117 Report.Error (80, lexer.Location,
1118 "Constraints are not allowed on non-generic declarations");
1122 GenericMethod generic = null;
1123 if (name.TypeArguments != null) {
1124 generic = new GenericMethod (current_namespace, current_class, name,
1125 (Expression) $3, (Parameters) $6);
1127 generic.SetParameterInfo ((ArrayList) $9);
1130 method = new Method (current_class, generic, (Expression) $3, (int) $2, false,
1131 name, (Parameters) $6, (Attributes) $1);
1133 current_local_parameters = (Parameters) $6;
1135 if (RootContext.Documentation != null)
1136 method.DocComment = Lexer.consume_doc_comment ();
1142 VOID namespace_or_type_name
1143 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1145 lexer.ConstraintsParsing = true;
1147 opt_type_parameter_constraints_clauses
1149 lexer.ConstraintsParsing = false;
1151 MemberName name = (MemberName) $4;
1153 if ($9 != null && name.TypeArguments == null)
1154 Report.Error (80, lexer.Location,
1155 "Constraints are not allowed on non-generic declarations");
1158 GenericMethod generic = null;
1159 if (name.TypeArguments != null) {
1160 generic = new GenericMethod (current_namespace, current_class, name,
1161 TypeManager.system_void_expr, (Parameters) $6);
1163 generic.SetParameterInfo ((ArrayList) $9);
1166 method = new Method (current_class, generic, TypeManager.system_void_expr,
1167 (int) $2, false, name, (Parameters) $6, (Attributes) $1);
1169 current_local_parameters = (Parameters) $6;
1171 if (RootContext.Documentation != null)
1172 method.DocComment = Lexer.consume_doc_comment ();
1179 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1181 MemberName name = (MemberName) $5;
1182 Report.Error (1585, name.Location,
1183 "Member modifier `{0}' must precede the member type and name", Modifiers.Name ((int) $4));
1185 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1186 0, false, name, (Parameters) $6, (Attributes) $1);
1188 current_local_parameters = (Parameters) $6;
1190 if (RootContext.Documentation != null)
1191 method.DocComment = Lexer.consume_doc_comment ();
1199 | SEMICOLON { $$ = null; }
1202 opt_formal_parameter_list
1203 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1204 | formal_parameter_list
1207 formal_parameter_list
1210 ArrayList pars_list = (ArrayList) $1;
1212 Parameter [] pars = new Parameter [pars_list.Count];
1213 pars_list.CopyTo (pars);
1215 $$ = new Parameters (pars);
1217 | fixed_parameters COMMA parameter_array
1219 ArrayList pars_list = (ArrayList) $1;
1222 Parameter [] pars = new Parameter [pars_list.Count];
1223 pars_list.CopyTo (pars);
1225 $$ = new Parameters (pars);
1227 | fixed_parameters COMMA ARGLIST
1229 ArrayList pars_list = (ArrayList) $1;
1230 //pars_list.Add (new ArglistParameter (GetLocation ($3)));
1232 Parameter [] pars = new Parameter [pars_list.Count];
1233 pars_list.CopyTo (pars);
1235 $$ = new Parameters (pars, true);
1237 | parameter_array COMMA error
1240 Report.Error (231, ((Parameter) $1).Location, "A params parameter must be the last parameter in a formal parameter list");
1243 | fixed_parameters COMMA parameter_array COMMA error
1246 Report.Error (231, ((Parameter) $3).Location, "A params parameter must be the last parameter in a formal parameter list");
1249 | ARGLIST COMMA error
1251 Report.Error (257, (Location) $1, "An __arglist parameter must be the last parameter in a formal parameter list");
1254 | fixed_parameters COMMA ARGLIST COMMA error
1256 Report.Error (257, (Location) $3, "An __arglist parameter must be the last parameter in a formal parameter list");
1261 $$ = new Parameters (new Parameter[] { (Parameter) $1 } );
1265 $$ = new Parameters (new Parameter[0], true);
1272 ArrayList pars = new ArrayList (4);
1277 | fixed_parameters COMMA fixed_parameter
1279 ArrayList pars = (ArrayList) $1;
1288 opt_parameter_modifier
1292 LocatedToken lt = (LocatedToken) $4;
1293 $$ = new Parameter ((Expression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1296 opt_parameter_modifier
1298 IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1300 LocatedToken lt = (LocatedToken) $4;
1301 Report.Error (1552, lt.Location, "Array type specifier, [], must appear before parameter name");
1305 opt_parameter_modifier
1308 Report.Error (1001, GetLocation ($3), "Identifier expected");
1312 opt_parameter_modifier
1315 CheckIdentifierToken (yyToken, GetLocation ($4));
1319 opt_parameter_modifier
1325 LocatedToken lt = (LocatedToken) $4;
1326 Report.Error (241, lt.Location, "Default parameter specifiers are not permitted");
1331 opt_parameter_modifier
1332 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1333 | parameter_modifier
1337 : REF { $$ = Parameter.Modifier.REF; }
1338 | OUT { $$ = Parameter.Modifier.OUT; }
1342 : opt_attributes PARAMS type IDENTIFIER
1344 LocatedToken lt = (LocatedToken) $4;
1345 $$ = new ParamsParameter ((Expression) $3, lt.Value, (Attributes) $1, lt.Location);
1346 note ("type must be a single-dimension array type");
1348 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1350 Report.Error (1611, (Location) $2, "The params parameter cannot be declared as ref or out");
1353 | opt_attributes PARAMS type error {
1354 CheckIdentifierToken (yyToken, GetLocation ($4));
1359 property_declaration
1363 namespace_or_type_name
1365 if (RootContext.Documentation != null)
1366 tmpComment = Lexer.consume_doc_comment ();
1370 implicit_value_parameter_type = (Expression) $3;
1372 lexer.PropertyParsing = true;
1374 accessor_declarations
1376 lexer.PropertyParsing = false;
1377 has_get = has_set = false;
1385 Pair pair = (Pair) $8;
1386 Accessor get_block = (Accessor) pair.First;
1387 Accessor set_block = (Accessor) pair.Second;
1389 MemberName name = (MemberName) $4;
1391 if (name.TypeArguments != null)
1392 syntax_error (lexer.Location, "a property can't have type arguments");
1394 prop = new Property (current_class, (Expression) $3, (int) $2, false,
1395 name, (Attributes) $1, get_block, set_block);
1397 current_container.AddProperty (prop);
1398 implicit_value_parameter_type = null;
1400 if (RootContext.Documentation != null)
1401 prop.DocComment = ConsumeStoredComment ();
1406 accessor_declarations
1407 : get_accessor_declaration
1409 $$ = new Pair ($1, null);
1411 | get_accessor_declaration accessor_declarations
1413 Pair pair = (Pair) $2;
1417 | set_accessor_declaration
1419 $$ = new Pair (null, $1);
1421 | set_accessor_declaration accessor_declarations
1423 Pair pair = (Pair) $2;
1429 Report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1434 get_accessor_declaration
1435 : opt_attributes opt_modifiers GET
1437 // If this is not the case, then current_local_parameters has already
1438 // been set in indexer_declaration
1439 if (parsing_indexer == false)
1440 current_local_parameters = null;
1442 current_local_parameters = indexer_parameters;
1443 lexer.PropertyParsing = false;
1445 iterator_container = SimpleIteratorContainer.GetSimple ();
1450 Report.Error (1007, (Location) $3, "Property accessor already defined");
1453 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1455 current_local_parameters = null;
1456 lexer.PropertyParsing = true;
1458 if (SimpleIteratorContainer.Simple.Yields)
1459 accessor.SetYields ();
1461 iterator_container = null;
1463 if (RootContext.Documentation != null)
1464 if (Lexer.doc_state == XmlCommentState.Error)
1465 Lexer.doc_state = XmlCommentState.NotAllowed;
1471 set_accessor_declaration
1472 : opt_attributes opt_modifiers SET
1475 Parameter implicit_value_parameter = new Parameter (
1476 implicit_value_parameter_type, "value",
1477 Parameter.Modifier.NONE, null, (Location) $3);
1479 if (parsing_indexer == false) {
1480 args = new Parameter [1];
1481 args [0] = implicit_value_parameter;
1482 current_local_parameters = new Parameters (args);
1484 Parameter [] fpars = indexer_parameters.FixedParameters;
1487 int count = fpars.Length;
1489 args = new Parameter [count + 1];
1490 fpars.CopyTo (args, 0);
1491 args [count] = implicit_value_parameter;
1494 current_local_parameters = new Parameters (
1498 lexer.PropertyParsing = false;
1500 iterator_container = SimpleIteratorContainer.GetSimple ();
1505 Report.Error (1007, ((LocatedToken) $3).Location, "Property accessor already defined");
1508 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1510 current_local_parameters = null;
1511 lexer.PropertyParsing = true;
1513 if (SimpleIteratorContainer.Simple.Yields)
1514 accessor.SetYields ();
1516 iterator_container = null;
1518 if (RootContext.Documentation != null
1519 && Lexer.doc_state == XmlCommentState.Error)
1520 Lexer.doc_state = XmlCommentState.NotAllowed;
1528 | SEMICOLON { $$ = null; }
1531 interface_declaration
1537 lexer.ConstraintsParsing = true;
1541 MemberName name = MakeName ((MemberName) $6);
1543 push_current_class (new Interface (
1544 current_namespace, current_class, name, (int) $2,
1545 (Attributes) $1), true, $3);
1548 opt_type_parameter_constraints_clauses
1550 lexer.ConstraintsParsing = false;
1553 current_container.AddBasesForPart (current_class, (ArrayList) $8);
1555 current_class.SetParameterInfo ((ArrayList) $9);
1557 if (RootContext.Documentation != null) {
1558 current_container.DocComment = Lexer.consume_doc_comment ();
1559 Lexer.doc_state = XmlCommentState.Allowed;
1564 if (RootContext.Documentation != null)
1565 Lexer.doc_state = XmlCommentState.Allowed;
1569 $$ = pop_current_class ();
1571 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1572 CheckIdentifierToken (yyToken, GetLocation ($5));
1578 opt_interface_member_declarations
1582 opt_interface_member_declarations
1584 | interface_member_declarations
1587 interface_member_declarations
1588 : interface_member_declaration
1589 | interface_member_declarations interface_member_declaration
1592 interface_member_declaration
1593 : interface_method_declaration
1598 Method m = (Method) $1;
1600 if (m.IsExplicitImpl)
1601 Report.Error (541, m.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1602 m.GetSignatureForError ());
1604 current_container.AddMethod (m);
1606 if (RootContext.Documentation != null)
1607 Lexer.doc_state = XmlCommentState.Allowed;
1609 | interface_property_declaration
1614 Property p = (Property) $1;
1616 if (p.IsExplicitImpl)
1617 Report.Error (541, p.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1618 p.GetSignatureForError ());
1620 current_container.AddProperty (p);
1622 if (RootContext.Documentation != null)
1623 Lexer.doc_state = XmlCommentState.Allowed;
1625 | interface_event_declaration
1628 Event e = (Event) $1;
1630 if (e.IsExplicitImpl)
1631 Report.Error (541, e.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1632 e.GetSignatureForError ());
1634 current_container.AddEvent (e);
1637 if (RootContext.Documentation != null)
1638 Lexer.doc_state = XmlCommentState.Allowed;
1640 | interface_indexer_declaration
1645 Indexer i = (Indexer) $1;
1647 if (i.IsExplicitImpl)
1648 Report.Error (541, i.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1649 i.GetSignatureForError ());
1651 current_container.AddIndexer (i);
1653 if (RootContext.Documentation != null)
1654 Lexer.doc_state = XmlCommentState.Allowed;
1656 | delegate_declaration
1659 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1660 ((MemberCore)$1).GetSignatureForError ());
1666 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1667 ((MemberCore)$1).GetSignatureForError ());
1670 | struct_declaration
1673 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1674 ((MemberCore)$1).GetSignatureForError ());
1680 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1681 ((MemberCore)$1).GetSignatureForError ());
1684 | interface_declaration
1687 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1688 ((MemberCore)$1).GetSignatureForError ());
1691 | constant_declaration
1693 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1701 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, GetLocation ($1));
1706 interface_method_declaration_body
1709 lexer.ConstraintsParsing = false;
1711 opt_statement_list CLOSE_BRACE
1713 Report.Error (531, lexer.Location,
1714 "'{0}': interface members cannot have a definition", ((MemberName) $-1).ToString ());
1720 interface_method_declaration
1721 : opt_attributes opt_new type namespace_or_type_name
1722 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1724 lexer.ConstraintsParsing = true;
1726 opt_type_parameter_constraints_clauses
1728 // Refer to the name as $-1 in interface_method_declaration_body
1731 interface_method_declaration_body
1733 lexer.ConstraintsParsing = false;
1735 MemberName name = (MemberName) $4;
1737 if ($9 != null && name.TypeArguments == null)
1738 Report.Error (80, lexer.Location,
1739 "Constraints are not allowed on non-generic declarations");
1741 GenericMethod generic = null;
1742 if (name.TypeArguments != null) {
1743 generic = new GenericMethod (current_namespace, current_class, name,
1744 (Expression) $3, (Parameters) $6);
1746 generic.SetParameterInfo ((ArrayList) $9);
1749 $$ = new Method (current_class, generic, (Expression) $3, (int) $2, true, name,
1750 (Parameters) $6, (Attributes) $1);
1751 if (RootContext.Documentation != null)
1752 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1754 | opt_attributes opt_new VOID namespace_or_type_name
1755 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1757 lexer.ConstraintsParsing = true;
1759 opt_type_parameter_constraints_clauses
1763 interface_method_declaration_body
1765 lexer.ConstraintsParsing = false;
1767 MemberName name = (MemberName) $4;
1769 if ($9 != null && name.TypeArguments == null)
1770 Report.Error (80, lexer.Location,
1771 "Constraints are not allowed on non-generic declarations");
1773 GenericMethod generic = null;
1774 if (name.TypeArguments != null) {
1775 generic = new GenericMethod (current_namespace, current_class, name,
1776 TypeManager.system_void_expr, (Parameters) $6);
1778 generic.SetParameterInfo ((ArrayList) $9);
1781 $$ = new Method (current_class, generic, TypeManager.system_void_expr, (int) $2,
1782 true, name, (Parameters) $6, (Attributes) $1);
1783 if (RootContext.Documentation != null)
1784 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1788 interface_property_declaration
1793 { lexer.PropertyParsing = true; }
1794 accessor_declarations
1796 has_get = has_set = false;
1797 lexer.PropertyParsing = false;
1801 LocatedToken lt = (LocatedToken) $4;
1802 MemberName name = new MemberName (lt.Value, lt.Location);
1804 if ($3 == TypeManager.system_void_expr) {
1805 Report.Error (547, lt.Location, "`{0}': property or indexer cannot have void type", lt.Value);
1811 p = new Property (current_class, (Expression) $3, (int) $2, true,
1812 name, (Attributes) $1,
1815 Report.Error (548, p.Location, "`{0}': property or indexer must have at least one accessor", p.GetSignatureForError ());
1819 Pair pair = (Pair) $7;
1820 p = new Property (current_class, (Expression) $3, (int) $2, true,
1821 name, (Attributes) $1,
1822 (Accessor)pair.First, (Accessor)pair.Second);
1824 if (pair.First != null && ((Accessor)(pair.First)).Block != null) {
1825 Report.Error (531, p.Location, "`{0}.get': interface members cannot have a definition", p.GetSignatureForError ());
1830 if (pair.Second != null && ((Accessor)(pair.Second)).Block != null) {
1831 Report.Error (531, p.Location, "`{0}.set': interface members cannot have a definition", p.GetSignatureForError ());
1836 if (RootContext.Documentation != null)
1837 p.DocComment = Lexer.consume_doc_comment ();
1844 CheckIdentifierToken (yyToken, GetLocation ($4));
1850 interface_event_declaration
1851 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1853 LocatedToken lt = (LocatedToken) $5;
1854 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1855 new MemberName (lt.Value, lt.Location),
1857 if (RootContext.Documentation != null)
1858 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1860 | opt_attributes opt_new EVENT type error {
1861 CheckIdentifierToken (yyToken, GetLocation ($5));
1864 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1865 LocatedToken lt = (LocatedToken) $5;
1866 Report.Error (68, lt.Location, "`{0}.{1}': event in interface cannot have initializer", current_container.Name, lt.Value);
1869 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1871 lexer.EventParsing = true;
1873 event_accessor_declarations
1875 lexer.EventParsing = false;
1878 Report.Error (69, (Location) $3, "Event in interface cannot have add or remove accessors");
1883 interface_indexer_declaration
1884 : opt_attributes opt_new type THIS
1885 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1887 { lexer.PropertyParsing = true; }
1888 accessor_declarations
1890 has_get = has_set = false;
1891 lexer.PropertyParsing = false;
1897 i = new Indexer (current_class, (Expression) $3,
1898 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1899 (int) $2, true, (Parameters) $6, (Attributes) $1,
1902 Report.Error (548, i.Location, "`{0}': property or indexer must have at least one accessor", i.GetSignatureForError ());
1906 Pair pair = (Pair) $10;
1907 i = new Indexer (current_class, (Expression) $3,
1908 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1909 (int) $2, true, (Parameters) $6, (Attributes) $1,
1910 (Accessor)pair.First, (Accessor)pair.Second);
1912 if (pair.First != null && ((Accessor)(pair.First)).Block != null) {
1913 Report.Error (531, i.Location, "`{0}.get': interface members cannot have a definition", i.GetSignatureForError ());
1918 if (pair.Second != null && ((Accessor)(pair.Second)).Block != null) {
1919 Report.Error (531, i.Location, "`{0}.set': interface members cannot have a definition", i.GetSignatureForError ());
1924 if (RootContext.Documentation != null)
1925 i.DocComment = ConsumeStoredComment ();
1931 operator_declaration
1932 : opt_attributes opt_modifiers operator_declarator
1934 iterator_container = SimpleIteratorContainer.GetSimple ();
1941 OperatorDeclaration decl = (OperatorDeclaration) $3;
1943 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1945 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null, decl.location);
1946 if (decl.arg2type != null)
1947 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null, decl.location);
1949 Operator op = new Operator (
1950 current_class, decl.optype, decl.ret_type, (int) $2,
1951 new Parameters (param_list),
1952 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1954 if (RootContext.Documentation != null) {
1955 op.DocComment = tmpComment;
1956 Lexer.doc_state = XmlCommentState.Allowed;
1959 if (SimpleIteratorContainer.Simple.Yields)
1962 // Note again, checking is done in semantic analysis
1963 current_container.AddOperator (op);
1965 current_local_parameters = null;
1966 iterator_container = null;
1972 | SEMICOLON { $$ = null; }
1975 : type OPERATOR overloadable_operator
1976 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1978 LocatedToken lt = (LocatedToken) $6;
1979 Operator.OpType op = (Operator.OpType) $3;
1980 CheckUnaryOperator (op, lt.Location);
1982 if (op == Operator.OpType.Addition)
1983 op = Operator.OpType.UnaryPlus;
1985 if (op == Operator.OpType.Subtraction)
1986 op = Operator.OpType.UnaryNegation;
1988 Parameter [] pars = new Parameter [1];
1989 Expression type = (Expression) $5;
1991 pars [0] = new Parameter (type, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1993 current_local_parameters = new Parameters (pars);
1995 if (RootContext.Documentation != null) {
1996 tmpComment = Lexer.consume_doc_comment ();
1997 Lexer.doc_state = XmlCommentState.NotAllowed;
2000 $$ = new OperatorDeclaration (op, (Expression) $1, type, lt.Value,
2001 null, null, (Location) $2);
2003 | type OPERATOR overloadable_operator
2005 type IDENTIFIER COMMA
2009 LocatedToken ltParam1 = (LocatedToken) $6;
2010 LocatedToken ltParam2 = (LocatedToken) $9;
2011 CheckBinaryOperator ((Operator.OpType) $3, (Location) $2);
2013 Parameter [] pars = new Parameter [2];
2015 Expression typeL = (Expression) $5;
2016 Expression typeR = (Expression) $8;
2018 pars [0] = new Parameter (typeL, ltParam1.Value, Parameter.Modifier.NONE, null, ltParam1.Location);
2019 pars [1] = new Parameter (typeR, ltParam2.Value, Parameter.Modifier.NONE, null, ltParam2.Location);
2021 current_local_parameters = new Parameters (pars);
2023 if (RootContext.Documentation != null) {
2024 tmpComment = Lexer.consume_doc_comment ();
2025 Lexer.doc_state = XmlCommentState.NotAllowed;
2028 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
2029 typeL, ltParam1.Value,
2030 typeR, ltParam2.Value, (Location) $2);
2032 | conversion_operator_declarator
2033 | type OPERATOR overloadable_operator
2035 type IDENTIFIER COMMA
2036 type IDENTIFIER COMMA
2040 Report.Error (1534, (Location) $2, "Overloaded binary operator `{0}' takes two parameters",
2041 Operator.GetName ((Operator.OpType) $3));
2044 | type OPERATOR overloadable_operator
2045 OPEN_PARENS CLOSE_PARENS
2047 Report.Error (1535, (Location) $2, "Overloaded unary operator `{0}' takes one parameter",
2048 Operator.GetName ((Operator.OpType) $3));
2053 overloadable_operator
2055 : BANG { $$ = Operator.OpType.LogicalNot; }
2056 | TILDE { $$ = Operator.OpType.OnesComplement; }
2057 | OP_INC { $$ = Operator.OpType.Increment; }
2058 | OP_DEC { $$ = Operator.OpType.Decrement; }
2059 | TRUE { $$ = Operator.OpType.True; }
2060 | FALSE { $$ = Operator.OpType.False; }
2061 // Unary and binary:
2062 | PLUS { $$ = Operator.OpType.Addition; }
2063 | MINUS { $$ = Operator.OpType.Subtraction; }
2065 | STAR { $$ = Operator.OpType.Multiply; }
2066 | DIV { $$ = Operator.OpType.Division; }
2067 | PERCENT { $$ = Operator.OpType.Modulus; }
2068 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
2069 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
2070 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
2071 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
2072 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
2073 | OP_EQ { $$ = Operator.OpType.Equality; }
2074 | OP_NE { $$ = Operator.OpType.Inequality; }
2075 | OP_GT { $$ = Operator.OpType.GreaterThan; }
2076 | OP_LT { $$ = Operator.OpType.LessThan; }
2077 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
2078 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
2081 conversion_operator_declarator
2082 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
2084 LocatedToken lt = (LocatedToken) $6;
2085 Parameter [] pars = new Parameter [1];
2087 pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2089 current_local_parameters = new Parameters (pars);
2091 if (RootContext.Documentation != null) {
2092 tmpComment = Lexer.consume_doc_comment ();
2093 Lexer.doc_state = XmlCommentState.NotAllowed;
2096 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, lt.Value,
2097 null, null, (Location) $2);
2099 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
2101 LocatedToken lt = (LocatedToken) $6;
2102 Parameter [] pars = new Parameter [1];
2104 pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2106 current_local_parameters = new Parameters (pars);
2108 if (RootContext.Documentation != null) {
2109 tmpComment = Lexer.consume_doc_comment ();
2110 Lexer.doc_state = XmlCommentState.NotAllowed;
2113 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, lt.Value,
2114 null, null, (Location) $2);
2118 syntax_error ((Location) $1, "'operator' expected");
2122 syntax_error ((Location) $1, "'operator' expected");
2126 constructor_declaration
2129 constructor_declarator
2132 Constructor c = (Constructor) $3;
2133 c.Block = (ToplevelBlock) $4;
2134 c.OptAttributes = (Attributes) $1;
2135 c.ModFlags = (int) $2;
2137 if (RootContext.Documentation != null)
2138 c.DocComment = ConsumeStoredComment ();
2140 if (c.Name == current_container.Basename){
2141 if ((c.ModFlags & Modifiers.STATIC) != 0){
2142 if ((c.ModFlags & Modifiers.Accessibility) != 0){
2143 Report.Error (515, c.Location,
2144 "`{0}': access modifiers are not allowed on static constructors",
2145 c.GetSignatureForError ());
2148 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2150 if (c.Initializer != null){
2151 Report.Error (514, c.Location,
2152 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2153 c.GetSignatureForError ());
2156 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2159 // We let another layer check the validity of the constructor.
2160 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
2163 current_container.AddConstructor (c);
2165 current_local_parameters = null;
2166 if (RootContext.Documentation != null)
2167 Lexer.doc_state = XmlCommentState.Allowed;
2171 constructor_declarator
2174 if (RootContext.Documentation != null) {
2175 tmpComment = Lexer.consume_doc_comment ();
2176 Lexer.doc_state = XmlCommentState.Allowed;
2179 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2181 current_local_parameters = (Parameters) $4;
2183 opt_constructor_initializer
2185 LocatedToken lt = (LocatedToken) $1;
2186 $$ = new Constructor (current_class, lt.Value, 0, (Parameters) $4,
2187 (ConstructorInitializer) $7, lt.Location);
2193 | SEMICOLON { $$ = null; }
2196 opt_constructor_initializer
2197 : /* empty */ { $$ = null; }
2198 | constructor_initializer
2201 constructor_initializer
2202 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
2204 $$ = new ConstructorBaseInitializer ((ArrayList) $4, (Location) $2);
2206 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
2208 $$ = new ConstructorThisInitializer ((ArrayList) $4, (Location) $2);
2211 Report.Error (1018, (Location) $1, "Keyword this or base expected");
2217 : /* EMPTY */ { $$ = 0; }
2218 | UNSAFE { $$ = Modifiers.UNSAFE; }
2219 | EXTERN { $$ = Modifiers.EXTERN; }
2222 destructor_declaration
2223 : opt_attributes opt_finalizer TILDE
2225 if (RootContext.Documentation != null) {
2226 tmpComment = Lexer.consume_doc_comment ();
2227 Lexer.doc_state = XmlCommentState.NotAllowed;
2230 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2232 LocatedToken lt = (LocatedToken) $5;
2233 if (lt.Value != current_container.MemberName.Name){
2234 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2235 } else if (current_container.Kind != Kind.Class){
2236 Report.Error (575, lt.Location, "Only class types can contain destructor");
2238 Location l = lt.Location;
2241 if (!RootContext.StdLib && current_container.Name == "System.Object")
2242 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2244 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2246 Method d = new Destructor (
2247 current_class, TypeManager.system_void_expr, m, "Finalize",
2248 Parameters.EmptyReadOnlyParameters, (Attributes) $1, l);
2249 if (RootContext.Documentation != null)
2250 d.DocComment = ConsumeStoredComment ();
2252 d.Block = (ToplevelBlock) $8;
2253 current_container.AddMethod (d);
2261 EVENT type variable_declarators SEMICOLON
2263 current_array_type = null;
2264 foreach (VariableDeclaration var in (ArrayList) $5) {
2266 MemberName name = new MemberName (var.identifier,
2269 EventField e = new EventField (
2270 current_class, (Expression) $4, (int) $2, false, name,
2273 e.Initializer = var.expression_or_array_initializer;
2275 current_container.AddEvent (e);
2277 if (RootContext.Documentation != null) {
2278 e.DocComment = Lexer.consume_doc_comment ();
2279 Lexer.doc_state = XmlCommentState.Allowed;
2285 EVENT type namespace_or_type_name
2288 implicit_value_parameter_type = (Expression) $4;
2289 lexer.EventParsing = true;
2291 event_accessor_declarations
2293 lexer.EventParsing = false;
2297 MemberName name = (MemberName) $5;
2300 Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2301 current_container.Name, name.ToString ());
2304 Pair pair = (Pair) $8;
2306 if (name.TypeArguments != null)
2307 syntax_error (lexer.Location, "an event can't have type arguments");
2309 if (pair.First == null || pair.Second == null)
2310 // CS0073 is already reported, so no CS0065 here.
2313 Event e = new EventProperty (
2314 current_class, (Expression) $4, (int) $2, false, name,
2315 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second);
2316 if (RootContext.Documentation != null) {
2317 e.DocComment = Lexer.consume_doc_comment ();
2318 Lexer.doc_state = XmlCommentState.Allowed;
2321 current_container.AddEvent (e);
2322 implicit_value_parameter_type = null;
2326 | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
2327 MemberName mn = (MemberName) $5;
2329 if (mn.Left != null)
2330 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2332 Report.Error (71, mn.Location, "Event declaration should use property syntax");
2334 if (RootContext.Documentation != null)
2335 Lexer.doc_state = XmlCommentState.Allowed;
2339 event_accessor_declarations
2340 : add_accessor_declaration remove_accessor_declaration
2342 $$ = new Pair ($1, $2);
2344 | remove_accessor_declaration add_accessor_declaration
2346 $$ = new Pair ($2, $1);
2348 | add_accessor_declaration { $$ = null; }
2349 | remove_accessor_declaration { $$ = null; }
2352 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2358 add_accessor_declaration
2359 : opt_attributes ADD
2361 Parameter [] args = new Parameter [1];
2362 Parameter implicit_value_parameter = new Parameter (
2363 implicit_value_parameter_type, "value",
2364 Parameter.Modifier.NONE, null, (Location) $2);
2366 args [0] = implicit_value_parameter;
2368 current_local_parameters = new Parameters (args);
2369 lexer.EventParsing = false;
2373 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2374 lexer.EventParsing = true;
2376 | opt_attributes ADD error {
2377 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2380 | opt_attributes modifiers ADD {
2381 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2386 remove_accessor_declaration
2387 : opt_attributes REMOVE
2389 Parameter [] args = new Parameter [1];
2390 Parameter implicit_value_parameter = new Parameter (
2391 implicit_value_parameter_type, "value",
2392 Parameter.Modifier.NONE, null, (Location) $2);
2394 args [0] = implicit_value_parameter;
2396 current_local_parameters = new Parameters (args);
2397 lexer.EventParsing = false;
2401 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2402 lexer.EventParsing = true;
2404 | opt_attributes REMOVE error {
2405 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2408 | opt_attributes modifiers REMOVE {
2409 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2415 : opt_attributes opt_modifiers indexer_declarator
2418 IndexerDeclaration decl = (IndexerDeclaration) $3;
2420 implicit_value_parameter_type = decl.type;
2422 lexer.PropertyParsing = true;
2423 parsing_indexer = true;
2425 indexer_parameters = decl.param_list;
2426 iterator_container = SimpleIteratorContainer.GetSimple ();
2428 accessor_declarations
2430 lexer.PropertyParsing = false;
2431 has_get = has_set = false;
2432 parsing_indexer = false;
2439 // The signature is computed from the signature of the indexer. Look
2440 // at section 3.6 on the spec
2442 IndexerDeclaration decl = (IndexerDeclaration) $3;
2443 Pair pair = (Pair) $6;
2444 Accessor get_block = (Accessor) pair.First;
2445 Accessor set_block = (Accessor) pair.Second;
2448 if (decl.interface_type != null)
2449 name = new MemberName (
2450 decl.interface_type, TypeContainer.DefaultIndexerName, decl.location);
2452 name = new MemberName (TypeContainer.DefaultIndexerName, decl.location);
2454 indexer = new Indexer (current_class, decl.type, name,
2455 (int) $2, false, decl.param_list, (Attributes) $1,
2456 get_block, set_block);
2458 if (RootContext.Documentation != null)
2459 indexer.DocComment = ConsumeStoredComment ();
2461 current_container.AddIndexer (indexer);
2463 current_local_parameters = null;
2464 implicit_value_parameter_type = null;
2465 indexer_parameters = null;
2470 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2472 Parameters pars = (Parameters) $4;
2473 if (pars.HasArglist) {
2474 // "__arglist is not valid in this context"
2475 Report.Error (1669, (Location) $2, "__arglist is not valid in this context");
2476 } else if (pars.Empty){
2477 Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
2479 if (RootContext.Documentation != null) {
2480 tmpComment = Lexer.consume_doc_comment ();
2481 Lexer.doc_state = XmlCommentState.Allowed;
2484 $$ = new IndexerDeclaration ((Expression) $1, null, pars, (Location) $2);
2486 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2488 Parameters pars = (Parameters) $6;
2490 if (pars.HasArglist) {
2491 // "__arglist is not valid in this context"
2492 Report.Error (1669, (Location) $4, "__arglist is not valid in this context");
2493 } else if (pars.Empty){
2494 Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
2497 MemberName name = (MemberName) $2;
2498 $$ = new IndexerDeclaration ((Expression) $1, name, pars, (Location) $4);
2500 if (RootContext.Documentation != null) {
2501 tmpComment = Lexer.consume_doc_comment ();
2502 Lexer.doc_state = XmlCommentState.Allowed;
2513 if (RootContext.Documentation != null)
2514 enumTypeComment = Lexer.consume_doc_comment ();
2519 LocatedToken lt = (LocatedToken) $5;
2520 Location enum_location = lt.Location;
2523 Report.Error (267, lt.Location, "The partial modifier can only appear immediately before `class', `struct' or `interface'");
2524 break; // assumes that the parser put us in a switch
2527 MemberName name = MakeName (new MemberName (lt.Value, enum_location));
2528 Enum e = new Enum (current_namespace, current_class, (Expression) $6, (int) $2,
2529 name, (Attributes) $1);
2531 if (RootContext.Documentation != null)
2532 e.DocComment = enumTypeComment;
2535 EnumMember em = null;
2536 foreach (VariableDeclaration ev in (ArrayList) $8) {
2537 em = new EnumMember (e, em, (Expression) ev.expression_or_array_initializer,
2538 new MemberName (ev.identifier, ev.Location), ev.OptAttributes);
2540 // if (RootContext.Documentation != null)
2541 em.DocComment = ev.DocComment;
2543 e.AddEnumMember (em);
2546 current_container.AddEnum (e);
2553 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2554 | COLON type { $$ = $2; }
2560 if (RootContext.Documentation != null)
2561 Lexer.doc_state = XmlCommentState.Allowed;
2563 opt_enum_member_declarations
2565 // here will be evaluated after CLOSE_BLACE is consumed.
2566 if (RootContext.Documentation != null)
2567 Lexer.doc_state = XmlCommentState.Allowed;
2575 opt_enum_member_declarations
2576 : /* empty */ { $$ = new ArrayList (4); }
2577 | enum_member_declarations opt_comma { $$ = $1; }
2580 enum_member_declarations
2581 : enum_member_declaration
2583 ArrayList l = new ArrayList (4);
2588 | enum_member_declarations COMMA enum_member_declaration
2590 ArrayList l = (ArrayList) $1;
2598 enum_member_declaration
2599 : opt_attributes IDENTIFIER
2601 VariableDeclaration vd = new VariableDeclaration (
2602 (LocatedToken) $2, null, (Attributes) $1);
2604 if (RootContext.Documentation != null) {
2605 vd.DocComment = Lexer.consume_doc_comment ();
2606 Lexer.doc_state = XmlCommentState.Allowed;
2611 | opt_attributes IDENTIFIER
2613 if (RootContext.Documentation != null) {
2614 tmpComment = Lexer.consume_doc_comment ();
2615 Lexer.doc_state = XmlCommentState.NotAllowed;
2620 VariableDeclaration vd = new VariableDeclaration (
2621 (LocatedToken) $2, $5, (Attributes) $1);
2623 if (RootContext.Documentation != null)
2624 vd.DocComment = ConsumeStoredComment ();
2630 delegate_declaration
2635 lexer.ConstraintsParsing = true;
2638 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2640 MemberName name = MakeName ((MemberName) $6);
2641 Parameters p = (Parameters) $8;
2643 // TODO: wrong location
2644 Report.Error (1669, name.Location, "__arglist is not valid in this context");
2647 Delegate del = new Delegate (current_namespace, current_class, (Expression) $5,
2648 (int) $2, name, p, (Attributes) $1);
2650 if (RootContext.Documentation != null) {
2651 del.DocComment = Lexer.consume_doc_comment ();
2652 Lexer.doc_state = XmlCommentState.Allowed;
2655 current_container.AddDelegate (del);
2656 current_delegate = del;
2658 opt_type_parameter_constraints_clauses
2660 lexer.ConstraintsParsing = false;
2664 current_delegate.SetParameterInfo ((ArrayList) $11);
2665 $$ = current_delegate;
2667 current_delegate = null;
2674 lexer.CheckNullable (false);
2679 lexer.CheckNullable (true);
2684 namespace_or_type_name
2686 | IDENTIFIER DOUBLE_COLON IDENTIFIER {
2687 LocatedToken lt1 = (LocatedToken) $1;
2688 LocatedToken lt2 = (LocatedToken) $3;
2689 $$ = new MemberName (lt1.Value, lt2.Value, lt2.Location);
2691 | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list {
2692 LocatedToken lt = (LocatedToken) $3;
2693 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $4, lt.Location);
2698 : IDENTIFIER opt_type_argument_list {
2699 LocatedToken lt = (LocatedToken) $1;
2700 $$ = new MemberName (lt.Value, (TypeArguments) $2, lt.Location);
2704 opt_type_argument_list
2705 : /* empty */ { $$ = null; }
2706 | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2709 if (RootContext.Version == LanguageVersion.ISO_1)
2710 Report.FeatureIsNotStandardized (lexer.Location, "generics");
2714 $$ = new TypeArguments ((int) $1, lexer.Location);
2715 if (RootContext.Version == LanguageVersion.ISO_1)
2716 Report.FeatureIsNotStandardized (lexer.Location, "generics");
2721 : opt_attributes type {
2722 TypeArguments type_args = new TypeArguments (lexer.Location);
2724 SimpleName sn = $2 as SimpleName;
2726 Report.Error (1031, lexer.Location, "Type expected");
2728 $2 = new TypeParameterName (sn.Name, (Attributes) $1, lexer.Location);
2730 type_args.Add ((Expression) $2);
2733 | type_arguments COMMA opt_attributes type {
2734 TypeArguments type_args = (TypeArguments) $1;
2736 SimpleName sn = $4 as SimpleName;
2738 Report.Error (1031, lexer.Location, "Type expected");
2740 $4 = new TypeParameterName (sn.Name, (Attributes) $3, lexer.Location);
2742 type_args.Add ((Expression) $4);
2749 * Before you think of adding a return_type, notice that we have been
2750 * using two rules in the places where it matters (one rule using type
2751 * and another identical one that uses VOID as the return type). This
2752 * gets rid of a shift/reduce couple
2755 : namespace_or_type_name opt_nullable
2757 MemberName name = (MemberName) $1;
2758 $$ = name.GetTypeExpression ();
2761 $$ = new ComposedCast ((Expression) $$, "?", lexer.Location);
2763 | builtin_types opt_nullable
2766 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2776 // Note that here only unmanaged types are allowed but we
2777 // can't perform checks during this phase - we do it during
2778 // semantic analysis.
2780 $$ = new ComposedCast ((Expression) $1, "*", Lexer.Location);
2784 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2789 : builtin_types opt_nullable
2792 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2794 | non_expression_type rank_specifier
2796 Location loc = GetLocation ($1);
2798 loc = lexer.Location;
2799 $$ = new ComposedCast ((Expression) $1, (string) $2, loc);
2801 | non_expression_type STAR
2803 Location loc = GetLocation ($1);
2805 loc = lexer.Location;
2806 $$ = new ComposedCast ((Expression) $1, "*", loc);
2808 | expression rank_specifiers
2810 $$ = new ComposedCast ((Expression) $1, (string) $2);
2814 $$ = new ComposedCast ((Expression) $1, "*");
2818 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2819 // through this different path
2821 | multiplicative_expression STAR
2823 $$ = new ComposedCast ((Expression) $1, "*");
2830 ArrayList types = new ArrayList (4);
2835 | type_list COMMA type
2837 ArrayList types = (ArrayList) $1;
2845 * replaces all the productions for isolating the various
2846 * simple types, but we need this to reuse it easily in local_variable_type
2849 : OBJECT { $$ = TypeManager.system_object_expr; }
2850 | STRING { $$ = TypeManager.system_string_expr; }
2851 | BOOL { $$ = TypeManager.system_boolean_expr; }
2852 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2853 | FLOAT { $$ = TypeManager.system_single_expr; }
2854 | DOUBLE { $$ = TypeManager.system_double_expr; }
2859 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2860 | BYTE { $$ = TypeManager.system_byte_expr; }
2861 | SHORT { $$ = TypeManager.system_int16_expr; }
2862 | USHORT { $$ = TypeManager.system_uint16_expr; }
2863 | INT { $$ = TypeManager.system_int32_expr; }
2864 | UINT { $$ = TypeManager.system_uint32_expr; }
2865 | LONG { $$ = TypeManager.system_int64_expr; }
2866 | ULONG { $$ = TypeManager.system_uint64_expr; }
2867 | CHAR { $$ = TypeManager.system_char_expr; }
2868 | VOID { $$ = TypeManager.system_void_expr; }
2872 : type rank_specifiers opt_nullable
2874 string rank_specifiers = (string) $2;
2876 rank_specifiers += "?";
2878 $$ = current_array_type = new ComposedCast ((Expression) $1, rank_specifiers);
2883 // Expressions, section 7.5
2892 MemberName mn = (MemberName) $1;
2893 $$ = mn.GetTypeExpression ();
2895 | IDENTIFIER DOUBLE_COLON IDENTIFIER
2897 LocatedToken lt1 = (LocatedToken) $1;
2898 LocatedToken lt2 = (LocatedToken) $3;
2899 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, lt2.Location);
2901 | parenthesized_expression
2902 | default_value_expression
2904 | invocation_expression
2908 | post_increment_expression
2909 | post_decrement_expression
2913 | checked_expression
2914 | unchecked_expression
2915 | pointer_member_access
2916 | anonymous_method_expression
2923 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
2924 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); }
2925 | NULL { $$ = new NullLiteral (lexer.Location); }
2929 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
2930 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
2931 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
2936 object v = lexer.Value;
2939 $$ = new IntLiteral ((int) v, lexer.Location);
2940 } else if (v is uint)
2941 $$ = new UIntLiteral ((UInt32) v, lexer.Location);
2943 $$ = new LongLiteral ((Int64) v, lexer.Location);
2944 else if (v is ulong)
2945 $$ = new ULongLiteral ((UInt64) v, lexer.Location);
2947 Console.WriteLine ("OOPS. Unexpected result from scanner");
2952 : TRUE { $$ = new BoolLiteral (true, lexer.Location); }
2953 | FALSE { $$ = new BoolLiteral (false, lexer.Location); }
2956 parenthesized_expression_0
2957 : OPEN_PARENS expression CLOSE_PARENS
2960 lexer.Deambiguate_CloseParens ();
2961 // After this, the next token returned is one of
2962 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2963 // or CLOSE_PARENS_MINUS.
2965 | OPEN_PARENS expression error { CheckToken (1026, yyToken, "Expecting ')'", lexer.Location); }
2968 parenthesized_expression
2969 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2973 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2975 // If a parenthesized expression is followed by a minus, we need to wrap
2976 // the expression inside a ParenthesizedExpression for the CS0075 check
2977 // in Binary.DoResolve().
2978 $$ = new ParenthesizedExpression ((Expression) $1);
2983 : primary_expression DOT IDENTIFIER opt_type_argument_list
2985 LocatedToken lt = (LocatedToken) $3;
2986 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
2988 | predefined_type DOT IDENTIFIER opt_type_argument_list
2990 LocatedToken lt = (LocatedToken) $3;
2991 // TODO: Location is wrong as some predefined types doesn't hold a location
2992 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
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;
3326 pushed_current_array_type = current_array_type;
3327 lexer.TypeOfParsing = true;
3329 OPEN_PARENS type CLOSE_PARENS
3331 lexer.TypeOfParsing = false;
3332 Expression type = (Expression)$4;
3333 if (type == TypeManager.system_void_expr)
3334 $$ = new TypeOfVoid ((Location) $1);
3336 $$ = new TypeOf (type, (Location) $1);
3337 current_array_type = pushed_current_array_type;
3342 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3343 $$ = new SizeOf ((Expression) $3, (Location) $1);
3348 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3350 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3354 unchecked_expression
3355 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3357 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3361 pointer_member_access
3362 : primary_expression OP_PTR IDENTIFIER
3365 LocatedToken lt = (LocatedToken) $3;
3367 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
3368 $$ = new MemberAccess (deref, lt.Value);
3372 anonymous_method_expression
3373 : DELEGATE opt_anonymous_method_signature
3375 if (oob_stack == null)
3376 oob_stack = new Stack (6);
3378 oob_stack.Push (current_local_parameters);
3379 current_local_parameters = (Parameters)$2;
3381 // Force the next block to be created as a ToplevelBlock
3382 oob_stack.Push (current_block);
3383 oob_stack.Push (top_current_block);
3384 current_block = null;
3388 Location loc = (Location) $1;
3389 top_current_block = (Block) oob_stack.Pop ();
3390 current_block = (Block) oob_stack.Pop ();
3391 if (RootContext.Version == LanguageVersion.ISO_1){
3392 Report.FeatureIsNotStandardized (loc, "anonymous methods");
3395 ToplevelBlock anon_block = (ToplevelBlock) $4;
3397 anon_block.Parent = current_block;
3398 $$ = new AnonymousMethod (current_container, (Parameters) $2, (ToplevelBlock) top_current_block,
3401 current_local_parameters = (Parameters) oob_stack.Pop ();
3405 opt_anonymous_method_signature
3406 : /* empty */ { $$ = null; }
3407 | anonymous_method_signature
3410 anonymous_method_signature
3411 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3414 $$ = Parameters.EmptyReadOnlyParameters;
3416 ArrayList par_list = (ArrayList) $2;
3417 Parameter [] pars = new Parameter [par_list.Count];
3418 par_list.CopyTo (pars);
3419 $$ = new Parameters (pars);
3424 opt_anonymous_method_parameter_list
3425 : /* empty */ { $$ = null; }
3426 | anonymous_method_parameter_list { $$ = $1; }
3429 anonymous_method_parameter_list
3430 : anonymous_method_parameter
3432 ArrayList a = new ArrayList (4);
3436 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3438 ArrayList a = (ArrayList) $1;
3444 anonymous_method_parameter
3445 : opt_parameter_modifier type IDENTIFIER {
3446 LocatedToken lt = (LocatedToken) $3;
3447 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3449 | PARAMS type IDENTIFIER {
3450 Report.Error (1670, ((LocatedToken) $3).Location, "The `params' modifier is not allowed in anonymous method declaration");
3455 default_value_expression
3456 : DEFAULT_OPEN_PARENS type CLOSE_PARENS
3458 $$ = new DefaultValueExpression ((Expression) $2, lexer.Location);
3463 : primary_expression
3464 | BANG prefixed_unary_expression
3466 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3468 | TILDE prefixed_unary_expression
3470 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3476 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3478 $$ = new Cast ((Expression) $1, (Expression) $3);
3480 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3482 $$ = new Cast ((Expression) $1, (Expression) $3);
3488 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3490 // TODO: wrong location
3491 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3496 // The idea to split this out is from Rhys' grammar
3497 // to solve the problem with casts.
3499 prefixed_unary_expression
3501 | PLUS prefixed_unary_expression
3503 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3505 | MINUS prefixed_unary_expression
3507 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3509 | OP_INC prefixed_unary_expression
3511 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3512 (Expression) $2, (Location) $1);
3514 | OP_DEC prefixed_unary_expression
3516 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3517 (Expression) $2, (Location) $1);
3519 | STAR prefixed_unary_expression
3521 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, (Location) $1);
3523 | BITWISE_AND prefixed_unary_expression
3525 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3529 pre_increment_expression
3530 : OP_INC prefixed_unary_expression
3532 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3533 (Expression) $2, (Location) $1);
3537 pre_decrement_expression
3538 : OP_DEC prefixed_unary_expression
3540 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3541 (Expression) $2, (Location) $1);
3545 multiplicative_expression
3546 : prefixed_unary_expression
3547 | multiplicative_expression STAR prefixed_unary_expression
3549 $$ = new Binary (Binary.Operator.Multiply,
3550 (Expression) $1, (Expression) $3);
3552 | multiplicative_expression DIV prefixed_unary_expression
3554 $$ = new Binary (Binary.Operator.Division,
3555 (Expression) $1, (Expression) $3);
3557 | multiplicative_expression PERCENT prefixed_unary_expression
3559 $$ = new Binary (Binary.Operator.Modulus,
3560 (Expression) $1, (Expression) $3);
3565 : multiplicative_expression
3566 | additive_expression PLUS multiplicative_expression
3568 $$ = new Binary (Binary.Operator.Addition,
3569 (Expression) $1, (Expression) $3);
3571 | additive_expression MINUS multiplicative_expression
3573 $$ = new Binary (Binary.Operator.Subtraction,
3574 (Expression) $1, (Expression) $3);
3579 : additive_expression
3580 | shift_expression OP_SHIFT_LEFT additive_expression
3582 $$ = new Binary (Binary.Operator.LeftShift,
3583 (Expression) $1, (Expression) $3);
3585 | shift_expression OP_SHIFT_RIGHT additive_expression
3587 $$ = new Binary (Binary.Operator.RightShift,
3588 (Expression) $1, (Expression) $3);
3599 lexer.PutbackNullable ();
3604 nullable_type_or_conditional
3607 if (((bool) $2) && ($1 is ComposedCast))
3608 $$ = ((ComposedCast) $1).RemoveNullable ();
3614 relational_expression
3616 | relational_expression OP_LT shift_expression
3618 $$ = new Binary (Binary.Operator.LessThan,
3619 (Expression) $1, (Expression) $3);
3621 | relational_expression OP_GT shift_expression
3623 $$ = new Binary (Binary.Operator.GreaterThan,
3624 (Expression) $1, (Expression) $3);
3626 | relational_expression OP_LE shift_expression
3628 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3629 (Expression) $1, (Expression) $3);
3631 | relational_expression OP_GE shift_expression
3633 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3634 (Expression) $1, (Expression) $3);
3636 | relational_expression IS
3639 } nullable_type_or_conditional
3641 $$ = new Is ((Expression) $1, (Expression) $4, (Location) $2);
3643 | relational_expression AS
3646 } nullable_type_or_conditional
3648 $$ = new As ((Expression) $1, (Expression) $4, (Location) $2);
3653 : relational_expression
3654 | equality_expression OP_EQ relational_expression
3656 $$ = new Binary (Binary.Operator.Equality,
3657 (Expression) $1, (Expression) $3);
3659 | equality_expression OP_NE relational_expression
3661 $$ = new Binary (Binary.Operator.Inequality,
3662 (Expression) $1, (Expression) $3);
3667 : equality_expression
3668 | and_expression BITWISE_AND equality_expression
3670 $$ = new Binary (Binary.Operator.BitwiseAnd,
3671 (Expression) $1, (Expression) $3);
3675 exclusive_or_expression
3677 | exclusive_or_expression CARRET and_expression
3679 $$ = new Binary (Binary.Operator.ExclusiveOr,
3680 (Expression) $1, (Expression) $3);
3684 inclusive_or_expression
3685 : exclusive_or_expression
3686 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3688 $$ = new Binary (Binary.Operator.BitwiseOr,
3689 (Expression) $1, (Expression) $3);
3693 conditional_and_expression
3694 : inclusive_or_expression
3695 | conditional_and_expression OP_AND inclusive_or_expression
3697 $$ = new Binary (Binary.Operator.LogicalAnd,
3698 (Expression) $1, (Expression) $3);
3702 conditional_or_expression
3703 : conditional_and_expression
3704 | conditional_or_expression OP_OR conditional_and_expression
3706 $$ = new Binary (Binary.Operator.LogicalOr,
3707 (Expression) $1, (Expression) $3);
3711 conditional_expression
3712 : conditional_or_expression
3713 | conditional_or_expression INTERR expression COLON expression
3715 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
3717 | conditional_or_expression INTERR INTERR expression
3719 $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $4, lexer.Location);
3721 // We'll be resolved into a `parenthesized_expression_0' later on.
3722 | conditional_or_expression INTERR CLOSE_PARENS
3724 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
3725 lexer.PutbackCloseParens ();
3729 assignment_expression
3730 : prefixed_unary_expression ASSIGN expression
3732 $$ = new Assign ((Expression) $1, (Expression) $3);
3734 | prefixed_unary_expression OP_MULT_ASSIGN expression
3736 $$ = new CompoundAssign (
3737 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
3739 | prefixed_unary_expression OP_DIV_ASSIGN expression
3741 $$ = new CompoundAssign (
3742 Binary.Operator.Division, (Expression) $1, (Expression) $3);
3744 | prefixed_unary_expression OP_MOD_ASSIGN expression
3746 $$ = new CompoundAssign (
3747 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
3749 | prefixed_unary_expression OP_ADD_ASSIGN expression
3751 $$ = new CompoundAssign (
3752 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
3754 | prefixed_unary_expression OP_SUB_ASSIGN expression
3756 $$ = new CompoundAssign (
3757 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3759 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3761 $$ = new CompoundAssign (
3762 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
3764 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3766 $$ = new CompoundAssign (
3767 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
3769 | prefixed_unary_expression OP_AND_ASSIGN expression
3771 $$ = new CompoundAssign (
3772 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
3774 | prefixed_unary_expression OP_OR_ASSIGN expression
3776 $$ = new CompoundAssign (
3777 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
3779 | prefixed_unary_expression OP_XOR_ASSIGN expression
3781 $$ = new CompoundAssign (
3782 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
3787 : conditional_expression
3788 | assignment_expression
3808 lexer.ConstraintsParsing = true;
3812 MemberName name = MakeName ((MemberName) $6);
3813 int mod_flags = (int) $2;
3815 push_current_class (new Class (
3816 current_namespace, current_class, name,
3817 mod_flags, (Attributes) $1), false, $3);
3820 opt_type_parameter_constraints_clauses
3822 lexer.ConstraintsParsing = false;
3825 if (current_class.Name == "System.Object") {
3826 Report.Error (537, current_class.Location,
3827 "The class System.Object cannot have a base " +
3828 "class or implement an interface.");
3830 current_container.AddBasesForPart (current_class, (ArrayList) $8);
3833 current_class.SetParameterInfo ((ArrayList) $9);
3835 if (RootContext.Documentation != null) {
3836 current_container.DocComment = Lexer.consume_doc_comment ();
3837 Lexer.doc_state = XmlCommentState.Allowed;
3842 if (RootContext.Documentation != null)
3843 Lexer.doc_state = XmlCommentState.Allowed;
3847 $$ = pop_current_class ();
3855 { $$ = $1; } // location
3859 : /* empty */ { $$ = (int) 0; }
3865 | modifiers modifier
3870 if ((m1 & m2) != 0) {
3871 Location l = lexer.Location;
3872 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
3874 $$ = (int) (m1 | m2);
3879 : NEW { $$ = Modifiers.NEW; }
3880 | PUBLIC { $$ = Modifiers.PUBLIC; }
3881 | PROTECTED { $$ = Modifiers.PROTECTED; }
3882 | INTERNAL { $$ = Modifiers.INTERNAL; }
3883 | PRIVATE { $$ = Modifiers.PRIVATE; }
3884 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3885 | SEALED { $$ = Modifiers.SEALED; }
3886 | STATIC { $$ = Modifiers.STATIC; }
3887 | READONLY { $$ = Modifiers.READONLY; }
3888 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3889 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3890 | EXTERN { $$ = Modifiers.EXTERN; }
3891 | VOLATILE { $$ = Modifiers.VOLATILE; }
3892 | UNSAFE { $$ = Modifiers.UNSAFE; }
3896 : /* empty */ { $$ = null; }
3897 | class_base { $$ = $1; }
3901 : COLON type_list { $$ = $2; }
3904 opt_type_parameter_constraints_clauses
3905 : /* empty */ { $$ = null; }
3906 | type_parameter_constraints_clauses
3910 type_parameter_constraints_clauses
3911 : type_parameter_constraints_clause {
3912 ArrayList constraints = new ArrayList (1);
3913 constraints.Add ($1);
3916 | type_parameter_constraints_clauses type_parameter_constraints_clause {
3917 ArrayList constraints = (ArrayList) $1;
3918 Constraints new_constraint = (Constraints)$2;
3920 foreach (Constraints c in constraints) {
3921 if (new_constraint.TypeParameter == c.TypeParameter) {
3922 Report.Error (409, new_constraint.Location, "A constraint clause has already been specified for type parameter `{0}'",
3923 new_constraint.TypeParameter);
3927 constraints.Add (new_constraint);
3932 type_parameter_constraints_clause
3933 : WHERE IDENTIFIER COLON type_parameter_constraints {
3934 LocatedToken lt = (LocatedToken) $2;
3935 $$ = new Constraints (lt.Value, (ArrayList) $4, lt.Location);
3939 type_parameter_constraints
3940 : type_parameter_constraint {
3941 ArrayList constraints = new ArrayList (1);
3942 constraints.Add ($1);
3945 | type_parameter_constraints COMMA type_parameter_constraint {
3946 ArrayList constraints = (ArrayList) $1;
3948 constraints.Add ($3);
3953 type_parameter_constraint
3955 | NEW OPEN_PARENS CLOSE_PARENS {
3956 $$ = SpecialConstraint.Constructor;
3959 $$ = SpecialConstraint.ReferenceType;
3962 $$ = SpecialConstraint.ValueType;
3971 // A block is "contained" on the following places:
3973 // property_declaration as part of the accessor body (get/set)
3974 // operator_declaration
3975 // constructor_declaration
3976 // destructor_declaration
3977 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3982 if (current_block == null){
3983 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, (Location) $1);
3984 top_current_block = current_block;
3986 current_block = new Block (current_block, (Location) $1, Location.Null);
3989 opt_statement_list CLOSE_BRACE
3991 while (current_block.Implicit)
3992 current_block = current_block.Parent;
3994 current_block.SetEndLocation ((Location) $4);
3995 current_block = current_block.Parent;
3996 if (current_block == null)
3997 top_current_block = null;
4008 | statement_list statement
4012 : declaration_statement
4014 if ($1 != null && (Block) $1 != current_block){
4015 current_block.AddStatement ((Statement) $1);
4016 current_block = (Block) $1;
4019 | valid_declaration_statement
4021 current_block.AddStatement ((Statement) $1);
4026 valid_declaration_statement
4029 | expression_statement
4030 | selection_statement
4031 | iteration_statement
4035 | unchecked_statement
4043 : valid_declaration_statement
4044 | declaration_statement
4046 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4051 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4059 $$ = EmptyStatement.Value;
4066 LocatedToken lt = (LocatedToken) $1;
4067 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
4069 if (current_block.AddLabel (labeled))
4070 current_block.AddStatement (labeled);
4075 declaration_statement
4076 : local_variable_declaration SEMICOLON
4078 current_array_type = null;
4080 DictionaryEntry de = (DictionaryEntry) $1;
4081 Expression e = (Expression) de.Key;
4083 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
4087 | local_constant_declaration SEMICOLON
4089 current_array_type = null;
4091 DictionaryEntry de = (DictionaryEntry) $1;
4093 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
4099 * The following is from Rhys' grammar:
4100 * > Types in local variable declarations must be recognized as
4101 * > expressions to prevent reduce/reduce errors in the grammar.
4102 * > The expressions are converted into types during semantic analysis.
4105 : primary_expression opt_rank_specifier_or_nullable
4107 // FIXME: Do something smart here regarding the composition of the type.
4109 // Ok, the above "primary_expression" is there to get rid of
4110 // both reduce/reduce and shift/reduces in the grammar, it should
4111 // really just be "type_name". If you use type_name, a reduce/reduce
4112 // creeps up. If you use namespace_or_type_name (which is all we need
4113 // really) two shift/reduces appear.
4116 // So the super-trick is that primary_expression
4117 // can only be either a SimpleName or a MemberAccess.
4118 // The MemberAccess case arises when you have a fully qualified type-name like :
4120 // SimpleName is when you have
4123 Expression expr = (Expression) $1;
4124 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType || expr is QualifiedAliasMember)) {
4125 Error_ExpectingTypeName (expr);
4129 // So we extract the string corresponding to the SimpleName
4133 if ((string) $2 == "")
4136 $$ = new ComposedCast ((Expression) $1, (string) $2);
4139 | builtin_types opt_rank_specifier_or_nullable
4141 if ((string) $2 == "")
4144 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
4148 local_variable_pointer_type
4149 : primary_expression STAR
4151 Expression expr = (Expression) $1;
4153 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType || expr is QualifiedAliasMember)) {
4154 Error_ExpectingTypeName (expr);
4158 $$ = new ComposedCast ((Expression) $1, "*");
4160 | builtin_types STAR
4162 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
4166 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
4168 | local_variable_pointer_type STAR
4170 $$ = new ComposedCast ((Expression) $1, "*");
4174 local_variable_declaration
4175 : local_variable_type variable_declarators
4178 $$ = new DictionaryEntry ($1, $2);
4182 | local_variable_pointer_type opt_rank_specifier_or_nullable variable_declarators
4187 if ((string) $2 == "")
4188 t = (Expression) $1;
4190 t = new ComposedCast ((Expression) $1, (string) $2);
4191 $$ = new DictionaryEntry (t, $3);
4197 local_constant_declaration
4198 : CONST local_variable_type constant_declarators
4201 $$ = new DictionaryEntry ($2, $3);
4207 expression_statement
4208 : statement_expression SEMICOLON { $$ = $1; }
4212 // We have to do the wrapping here and not in the case above,
4213 // because statement_expression is used for example in for_statement
4215 statement_expression
4218 Expression expr = (Expression) $1;
4219 ExpressionStatement s = expr as ExpressionStatement;
4221 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
4224 $$ = new StatementExpression (s);
4228 Report.Error (1002, GetLocation ($1), "Expecting `;'");
4233 object_creation_expression
4234 : object_or_delegate_creation_expression
4235 { note ("complain if this is a delegate maybe?"); }
4244 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
4247 Location l = (Location) $1;
4249 $$ = new If ((Expression) $3, (Statement) $5, l);
4251 // FIXME: location for warning should be loc property of $5.
4252 if ($5 == EmptyStatement.Value)
4253 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4256 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
4257 embedded_statement ELSE embedded_statement
4259 Location l = (Location) $1;
4261 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
4263 // FIXME: location for warning should be loc property of $5 and $7.
4264 if ($5 == EmptyStatement.Value)
4265 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4266 if ($7 == EmptyStatement.Value)
4267 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4272 : SWITCH OPEN_PARENS
4274 if (switch_stack == null)
4275 switch_stack = new Stack (2);
4276 switch_stack.Push (current_block);
4278 expression CLOSE_PARENS
4281 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
4282 current_block = (Block) switch_stack.Pop ();
4298 Report.Warning (1522, 1, lexer.Location, "Empty switch block");
4299 $$ = new ArrayList ();
4307 ArrayList sections = new ArrayList (4);
4312 | switch_sections switch_section
4314 ArrayList sections = (ArrayList) $1;
4324 current_block = current_block.CreateSwitchBlock (lexer.Location);
4328 Block topmost = current_block;
4330 while (topmost.Implicit)
4331 topmost = topmost.Parent;
4332 $$ = new SwitchSection ((ArrayList) $1, topmost);
4339 ArrayList labels = new ArrayList (4);
4344 | switch_labels switch_label
4346 ArrayList labels = (ArrayList) ($1);
4354 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
4355 | DEFAULT COLON { $$ = new SwitchLabel (null, (Location) $2); }
4358 1523, GetLocation ($1),
4359 "The keyword case or default must precede code in switch block");
4371 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
4373 Location l = (Location) $1;
4374 $$ = new While ((Expression) $3, (Statement) $5, l);
4379 : DO embedded_statement
4380 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
4382 Location l = (Location) $1;
4384 $$ = new Do ((Statement) $2, (Expression) $5, l);
4390 opt_for_initializer SEMICOLON
4392 Block assign_block = new Block (current_block);
4393 current_block = assign_block;
4395 if ($3 is DictionaryEntry){
4396 DictionaryEntry de = (DictionaryEntry) $3;
4398 Expression type = (Expression) de.Key;
4399 ArrayList var_declarators = (ArrayList) de.Value;
4401 foreach (VariableDeclaration decl in var_declarators){
4405 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4409 Location l = lexer.Location;
4410 Expression expr = decl.expression_or_array_initializer;
4412 LocalVariableReference var;
4413 var = new LocalVariableReference (assign_block, decl.identifier, l);
4416 Assign a = new Assign (var, expr, decl.Location);
4418 assign_block.AddStatement (new StatementExpression (a));
4422 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4423 // This can be referred to as $5 below.
4429 opt_for_condition SEMICOLON
4430 opt_for_iterator CLOSE_PARENS
4433 Location l = (Location) $1;
4435 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4437 current_block.AddStatement (f);
4438 while (current_block.Implicit)
4439 current_block = current_block.Parent;
4441 current_block = current_block.Parent;
4446 : /* empty */ { $$ = EmptyStatement.Value; }
4451 : local_variable_declaration
4452 | statement_expression_list
4456 : /* empty */ { $$ = null; }
4457 | boolean_expression
4461 : /* empty */ { $$ = EmptyStatement.Value; }
4466 : statement_expression_list
4469 statement_expression_list
4470 : statement_expression
4472 // CHANGE: was `null'
4473 Statement s = (Statement) $1;
4474 Block b = new Block (current_block, Block.Flags.Implicit, s.loc, lexer.Location);
4479 | statement_expression_list COMMA statement_expression
4481 Block b = (Block) $1;
4483 b.AddStatement ((Statement) $3);
4489 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4491 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4494 | FOREACH OPEN_PARENS type IDENTIFIER IN
4495 expression CLOSE_PARENS
4497 Block foreach_block = new Block (current_block);
4498 current_block = foreach_block;
4500 LocatedToken lt = (LocatedToken) $4;
4501 Location l = lt.Location;
4504 vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4506 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4508 // Get a writable reference to this read-only variable.
4510 // Note that the $$ here refers to the value of _this_ code block,
4511 // not the value of the LHS non-terminal. This can be referred to as $8 below.
4512 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
4519 LocalVariableReference v = (LocalVariableReference) $8;
4520 Location l = (Location) $1;
4523 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
4524 current_block.AddStatement (f);
4527 while (current_block.Implicit)
4528 current_block = current_block.Parent;
4530 current_block = current_block.Parent;
4536 | continue_statement
4546 $$ = new Break ((Location) $1);
4551 : CONTINUE SEMICOLON
4553 $$ = new Continue ((Location) $1);
4558 : GOTO IDENTIFIER SEMICOLON
4560 LocatedToken lt = (LocatedToken) $2;
4561 $$ = new Goto (lt.Value, lt.Location);
4563 | GOTO CASE constant_expression SEMICOLON
4565 $$ = new GotoCase ((Expression) $3, (Location) $1);
4567 | GOTO DEFAULT SEMICOLON
4569 $$ = new GotoDefault ((Location) $1);
4574 : RETURN opt_expression SEMICOLON
4576 $$ = new Return ((Expression) $2, (Location) $1);
4581 : THROW opt_expression SEMICOLON
4583 $$ = new Throw ((Expression) $2, (Location) $1);
4588 : IDENTIFIER RETURN expression SEMICOLON
4590 LocatedToken lt = (LocatedToken) $1;
4591 string s = lt.Value;
4593 Report.Error (1003, lt.Location, "; expected");
4596 if (RootContext.Version == LanguageVersion.ISO_1){
4597 Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4600 if (iterator_container == null){
4601 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4604 iterator_container.SetYields ();
4605 $$ = new Yield ((Expression) $3, lt.Location);
4608 | IDENTIFIER RETURN SEMICOLON
4610 Report.Error (1627, (Location) $2, "Expression expected after yield return");
4613 | IDENTIFIER BREAK SEMICOLON
4615 LocatedToken lt = (LocatedToken) $1;
4616 string s = lt.Value;
4618 Report.Error (1003, lt.Location, "; expected");
4621 if (RootContext.Version == LanguageVersion.ISO_1){
4622 Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4625 if (iterator_container == null){
4626 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4629 iterator_container.SetYields ();
4630 $$ = new YieldBreak (lt.Location);
4641 : TRY block catch_clauses
4645 ArrayList c = (ArrayList)$3;
4646 for (int i = 0; i < c.Count; ++i) {
4647 Catch cc = (Catch) c [i];
4649 if (i != c.Count - 1)
4650 Report.Error (1017, cc.loc, "Try statement already has an empty catch block");
4657 // Now s contains the list of specific catch clauses
4658 // and g contains the general one.
4660 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4662 | TRY block opt_catch_clauses FINALLY block
4665 ArrayList s = new ArrayList (4);
4666 ArrayList catch_list = (ArrayList) $3;
4668 if (catch_list != null){
4669 foreach (Catch cc in catch_list) {
4677 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4681 Report.Error (1524, (Location) $1, "Expected catch or finally");
4687 : /* empty */ { $$ = null; }
4694 ArrayList l = new ArrayList (4);
4699 | catch_clauses catch_clause
4701 ArrayList l = (ArrayList) $1;
4709 : /* empty */ { $$ = null; }
4714 : CATCH opt_catch_args
4716 Expression type = null;
4719 DictionaryEntry cc = (DictionaryEntry) $2;
4720 type = (Expression) cc.Key;
4721 LocatedToken lt = (LocatedToken) cc.Value;
4724 ArrayList one = new ArrayList (4);
4726 one.Add (new VariableDeclaration (lt, null));
4728 current_block = new Block (current_block);
4729 Block b = declare_local_variables (type, one, lt.Location);
4734 Expression type = null;
4736 Block var_block = null;
4739 DictionaryEntry cc = (DictionaryEntry) $2;
4740 type = (Expression) cc.Key;
4741 LocatedToken lt = (LocatedToken) cc.Value;
4745 while (current_block.Implicit)
4746 current_block = current_block.Parent;
4747 var_block = current_block;
4748 current_block = current_block.Parent;
4752 $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
4757 : /* empty */ { $$ = null; }
4762 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4764 $$ = new DictionaryEntry ($2, $3);
4772 $$ = new Checked ((Block) $2);
4779 $$ = new Unchecked ((Block) $2);
4786 RootContext.CheckUnsafeOption ((Location) $1);
4788 $$ = new Unsafe ((Block) $3);
4794 type fixed_pointer_declarators
4797 ArrayList list = (ArrayList) $4;
4798 Expression type = (Expression) $3;
4799 Location l = (Location) $1;
4800 int top = list.Count;
4802 Block assign_block = new Block (current_block);
4803 current_block = assign_block;
4805 for (int i = 0; i < top; i++){
4806 Pair p = (Pair) list [i];
4809 v = current_block.AddVariable (type, (string) p.First, l);
4813 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4821 Location l = (Location) $1;
4823 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4825 current_block.AddStatement (f);
4826 while (current_block.Implicit)
4827 current_block = current_block.Parent;
4829 current_block = current_block.Parent;
4833 fixed_pointer_declarators
4834 : fixed_pointer_declarator {
4835 ArrayList declarators = new ArrayList (4);
4837 declarators.Add ($1);
4840 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4842 ArrayList declarators = (ArrayList) $1;
4844 declarators.Add ($3);
4849 fixed_pointer_declarator
4850 : IDENTIFIER ASSIGN expression
4852 LocatedToken lt = (LocatedToken) $1;
4853 // FIXME: keep location
4854 $$ = new Pair (lt.Value, $3);
4858 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
4864 : LOCK OPEN_PARENS expression CLOSE_PARENS
4870 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
4875 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4877 Block assign_block = new Block (current_block);
4878 current_block = assign_block;
4880 if ($3 is DictionaryEntry){
4881 DictionaryEntry de = (DictionaryEntry) $3;
4882 Location l = (Location) $1;
4884 Expression type = (Expression) de.Key;
4885 ArrayList var_declarators = (ArrayList) de.Value;
4887 ArrayList vars = new ArrayList (4);
4889 foreach (VariableDeclaration decl in var_declarators){
4891 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4894 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
4896 Expression expr = decl.expression_or_array_initializer;
4898 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4901 LocalVariableReference var;
4903 // Get a writable reference to this read-only variable.
4904 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4906 // This is so that it is not a warning on using variables
4909 vars.Add (new DictionaryEntry (var, expr));
4911 // Assign a = new Assign (var, expr, decl.Location);
4912 // assign_block.AddStatement (new StatementExpression (a));
4915 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4916 // It can be referred to as $5 below.
4917 $$ = new DictionaryEntry (type, vars);
4924 Using u = new Using ($5, (Statement) $6, (Location) $1);
4925 current_block.AddStatement (u);
4926 while (current_block.Implicit)
4927 current_block = current_block.Parent;
4929 current_block = current_block.Parent;
4933 resource_acquisition
4934 : local_variable_declaration
4941 // A class used to pass around variable declarations and constants
4943 public class VariableDeclaration {
4944 public string identifier;
4945 public Expression expression_or_array_initializer;
4946 public Location Location;
4947 public Attributes OptAttributes;
4948 public string DocComment;
4950 public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
4952 this.identifier = lt.Value;
4953 if (eoai is ArrayList) {
4954 if (CSharpParser.current_array_type == null)
4955 Report.Error (622, lt.Location,
4956 "Can only use array initializer expressions to assign to array types. Try using a new expression instead.");
4957 this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
4959 this.expression_or_array_initializer = (Expression)eoai;
4961 this.Location = lt.Location;
4962 this.OptAttributes = opt_attrs;
4965 public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
4971 // A class used to hold info about an indexer declarator
4973 public class IndexerDeclaration {
4974 public Expression type;
4975 public MemberName interface_type;
4976 public Parameters param_list;
4977 public Location location;
4979 public IndexerDeclaration (Expression type, MemberName interface_type,
4980 Parameters param_list, Location loc)
4983 this.interface_type = interface_type;
4984 this.param_list = param_list;
4985 this.location = loc;
4990 // We use this when we do not have an object in advance that is an IIteratorContainer
4992 public class SimpleIteratorContainer : IIteratorContainer {
4995 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
5000 public static SimpleIteratorContainer GetSimple () {
5001 Simple.Yields = false;
5005 public void SetYields () { Yields = true; }
5009 // A class used to hold info about an operator declarator
5011 public class OperatorDeclaration {
5012 public Operator.OpType optype;
5013 public Expression ret_type, arg1type, arg2type;
5014 public string arg1name, arg2name;
5015 public Location location;
5017 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
5018 Expression arg1type, string arg1name,
5019 Expression arg2type, string arg2name, Location location)
5022 this.ret_type = ret_type;
5023 this.arg1type = arg1type;
5024 this.arg1name = arg1name;
5025 this.arg2type = arg2type;
5026 this.arg2name = arg2name;
5027 this.location = location;
5032 void Error_ExpectingTypeName (Expression expr)
5034 if (expr is Invocation){
5035 Report.Error (1002, expr.Location, "Expecting `;'");
5037 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
5041 void push_current_class (TypeContainer tc, bool is_interface, object partial_token)
5043 if (partial_token != null)
5044 current_container = current_container.AddPartial (tc, is_interface);
5046 current_container = current_container.AddTypeContainer (tc, is_interface);
5050 DeclSpace pop_current_class ()
5052 DeclSpace retval = current_class;
5054 current_class = current_class.Parent;
5055 current_container = current_class.PartialContainer;
5061 // Given the @class_name name, it creates a fully qualified name
5062 // based on the containing declaration space
5065 MakeName (MemberName class_name)
5067 Namespace ns = current_namespace.NS;
5069 if (current_container.Name.Length == 0){
5070 if (ns.Name.Length != 0)
5071 return new MemberName (ns.MemberName, class_name);
5075 return new MemberName (current_container.MemberName, class_name);
5079 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
5081 Block implicit_block;
5082 ArrayList inits = null;
5085 // We use the `Used' property to check whether statements
5086 // have been added to the current block. If so, we need
5087 // to create another block to contain the new declaration
5088 // otherwise, as an optimization, we use the same block to
5089 // add the declaration.
5091 // FIXME: A further optimization is to check if the statements
5092 // that were added were added as part of the initialization
5093 // below. In which case, no other statements have been executed
5094 // and we might be able to reduce the number of blocks for
5095 // situations like this:
5097 // int j = 1; int k = j + 1;
5099 if (current_block.Used)
5100 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
5102 implicit_block = current_block;
5104 foreach (VariableDeclaration decl in variable_declarators){
5106 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
5107 if (decl.expression_or_array_initializer != null){
5109 inits = new ArrayList (4);
5116 return implicit_block;
5118 foreach (VariableDeclaration decl in inits){
5120 Expression expr = decl.expression_or_array_initializer;
5122 LocalVariableReference var;
5123 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
5125 assign = new Assign (var, expr, decl.Location);
5127 implicit_block.AddStatement (new StatementExpression (assign));
5130 return implicit_block;
5133 Block declare_local_constants (Expression type, ArrayList declarators)
5135 Block implicit_block;
5137 if (current_block.Used)
5138 implicit_block = new Block (current_block, Block.Flags.Implicit);
5140 implicit_block = current_block;
5142 foreach (VariableDeclaration decl in declarators){
5143 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
5146 return implicit_block;
5149 void CheckAttributeTarget (string a, Location l)
5153 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
5157 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
5163 void CheckUnaryOperator (Operator.OpType op, Location l)
5167 case Operator.OpType.LogicalNot:
5168 case Operator.OpType.OnesComplement:
5169 case Operator.OpType.Increment:
5170 case Operator.OpType.Decrement:
5171 case Operator.OpType.True:
5172 case Operator.OpType.False:
5173 case Operator.OpType.Addition:
5174 case Operator.OpType.Subtraction:
5179 Report.Error (1019, l, "Overloadable unary operator expected");
5185 void CheckBinaryOperator (Operator.OpType op, Location l)
5189 case Operator.OpType.Addition:
5190 case Operator.OpType.Subtraction:
5191 case Operator.OpType.Multiply:
5192 case Operator.OpType.Division:
5193 case Operator.OpType.Modulus:
5194 case Operator.OpType.BitwiseAnd:
5195 case Operator.OpType.BitwiseOr:
5196 case Operator.OpType.ExclusiveOr:
5197 case Operator.OpType.LeftShift:
5198 case Operator.OpType.RightShift:
5199 case Operator.OpType.Equality:
5200 case Operator.OpType.Inequality:
5201 case Operator.OpType.GreaterThan:
5202 case Operator.OpType.LessThan:
5203 case Operator.OpType.GreaterThanOrEqual:
5204 case Operator.OpType.LessThanOrEqual:
5208 Report.Error (1020, l, "Overloadable binary operator expected");
5214 void syntax_error (Location l, string msg)
5216 Report.Error (1003, l, "Syntax error, " + msg);
5219 void note (string s)
5221 // Used to put annotations
5226 public Tokenizer Lexer {
5232 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
5234 this.name = file.Name;
5236 current_namespace = new NamespaceEntry (null, file, null);
5237 current_class = current_namespace.SlaveDeclSpace;
5238 current_container = current_class.PartialContainer; // == RootContest.ToplevelTypes
5240 lexer = new Tokenizer (reader, file, defines);
5243 public void parse ()
5245 int errors = Report.Errors;
5247 if (yacc_verbose_flag > 1)
5248 yyparse (lexer, new yydebug.yyDebugSimple ());
5251 Tokenizer tokenizer = lexer as Tokenizer;
5252 tokenizer.cleanup ();
5253 } catch (Exception e){
5255 // Removed for production use, use parser verbose to get the output.
5257 // Console.WriteLine (e);
5258 if (Report.Errors == errors)
5259 Report.Error (-25, lexer.Location, "Parsing error");
5260 if (yacc_verbose_flag > 0)
5261 Console.WriteLine (e);
5264 if (RootContext.ToplevelTypes.NamespaceEntry != null)
5265 throw new InternalErrorException ("who set it?");
5268 static void CheckToken (int error, int yyToken, string msg, Location loc)
5270 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
5271 Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ());
5273 Report.Error (error, loc, msg);
5276 void CheckIdentifierToken (int yyToken, Location loc)
5278 CheckToken (1041, yyToken, "Identifier expected", loc);
5281 string ConsumeStoredComment ()
5283 string s = tmpComment;
5285 Lexer.doc_state = XmlCommentState.Allowed;
5289 Location GetLocation (object obj)
5291 if (obj is MemberCore)
5292 return ((MemberCore) obj).Location;
5293 if (obj is MemberName)
5294 return ((MemberName) obj).Location;
5295 if (obj is LocatedToken)
5296 return ((LocatedToken) obj).Location;
5297 if (obj is Location)
5298 return (Location) obj;
5299 return lexer.Location;