3 // cs-parser.jay: The Parser for the C# compiler
5 // Authors: Miguel de Icaza (miguel@gnu.org)
6 // Ravi Pratap (ravi@ximian.com)
7 // Marek Safar (marek.safar@gmail.com)
9 // Licensed under the terms of the GNU GPL
11 // (C) 2001 Ximian, Inc (http://www.ximian.com)
12 // (C) 2004 Novell, Inc
15 // (1) Figure out why error productions dont work. `type-declaration' is a
16 // great spot to put an `error' because you can reproduce it with this input:
19 // Possible optimization:
20 // Run memory profiler with parsing only, and consider dropping
21 // arraylists where not needed. Some pieces can use linked lists.
29 using System.Collections;
34 public class CSharpParser {
35 NamespaceEntry current_namespace;
36 TypeContainer current_container;
37 DeclSpace current_class;
39 IAnonymousHost anonymous_host;
42 /// Current block is used to add statements as we find
47 AnonymousMethodExpression current_anonymous_method;
50 /// This is used by the unary_expression code to resolve
51 /// a name against a parameter.
53 Parameters current_local_parameters;
56 /// Using during property parsing to describe the implicit
57 /// value parameter that is passed to the "set" and "get"accesor
58 /// methods (properties and indexers).
60 Expression implicit_value_parameter_type;
61 Parameters indexer_parameters;
64 /// Hack to help create non-typed array initializer
66 public static Expression current_array_type;
67 Expression pushed_current_array_type;
70 /// Used to determine if we are parsing the get/set pair
71 /// of an indexer or a property
75 bool parsing_anonymous_method;
78 /// An out-of-band stack.
80 static Stack oob_stack;
87 static public int yacc_verbose_flag;
89 // Name of the file we are parsing
98 /// Temporary Xml documentation cache.
99 /// For enum types, we need one more temporary store.
102 string enumTypeComment;
104 /// Current attribute target
105 string current_attr_target;
107 /// assembly and module attribute definitions are enabled
108 bool global_attrs_enabled = true;
109 bool has_get, has_set;
114 %token NONE /* This token is never returned by our lexer */
115 %token ERROR // This is used not by the parser, but by the tokenizer.
119 *These are the C# keywords
206 /* C# keywords which are not really keywords */
212 /* C# single character operators/punctuation. */
213 %token OPEN_BRACE "{"
214 %token CLOSE_BRACE "}"
215 %token OPEN_BRACKET "["
216 %token CLOSE_BRACKET "]"
217 %token OPEN_PARENS "("
218 %token CLOSE_PARENS ")"
230 %token OP_GENERICS_LT "<"
232 %token OP_GENERICS_GT ">"
233 %token BITWISE_AND "&"
234 %token BITWISE_OR "|"
241 /* C# multi-character operators. */
242 %token DOUBLE_COLON "::"
245 %token OP_SHIFT_LEFT "<<"
246 %token OP_SHIFT_RIGHT ">>"
253 %token OP_MULT_ASSIGN "*="
254 %token OP_DIV_ASSIGN "/="
255 %token OP_MOD_ASSIGN "%="
256 %token OP_ADD_ASSIGN "+="
257 %token OP_SUB_ASSIGN "-="
258 %token OP_SHIFT_LEFT_ASSIGN "<<="
259 %token OP_SHIFT_RIGHT_ASSIGN ">>="
260 %token OP_AND_ASSIGN "&="
261 %token OP_XOR_ASSIGN "^="
262 %token OP_OR_ASSIGN "|="
266 %token LITERAL_INTEGER "int literal"
267 %token LITERAL_FLOAT "float literal"
268 %token LITERAL_DOUBLE "double literal"
269 %token LITERAL_DECIMAL "decimal literal"
270 %token LITERAL_CHARACTER "character literal"
271 %token LITERAL_STRING "string literal"
274 %token OPEN_PARENS_LAMBDA
275 %token CLOSE_PARENS_CAST
276 %token CLOSE_PARENS_NO_CAST
277 %token CLOSE_PARENS_OPEN_PARENS
278 %token CLOSE_PARENS_MINUS
280 /* Add precedence rules to solve dangling else s/r conflict */
289 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
291 %left STAR DIV PERCENT
292 %right BANG CARRET UMINUS
293 %nonassoc OP_INC OP_DEC
295 %left OPEN_BRACKET OPEN_BRACE
299 %start compilation_unit
303 : outer_declarations opt_EOF
304 | outer_declarations global_attributes opt_EOF
305 | global_attributes opt_EOF
306 | opt_EOF /* allow empty files */
312 Lexer.check_incorrect_doc_comment ();
316 Lexer.check_incorrect_doc_comment ();
322 | outer_declarations outer_declaration
326 : extern_alias_directive
328 | namespace_member_declaration
331 extern_alias_directives
332 : extern_alias_directive
333 | extern_alias_directives extern_alias_directive;
335 extern_alias_directive
336 : EXTERN IDENTIFIER IDENTIFIER SEMICOLON
338 LocatedToken lt = (LocatedToken) $2;
341 Report.Error (1003, lt.Location, "'alias' expected");
342 } else if (RootContext.Version == LanguageVersion.ISO_1) {
343 Report.FeatureIsNotISO1 (lt.Location, "external alias");
345 lt = (LocatedToken) $3;
346 current_namespace.UsingExternalAlias (lt.Value, lt.Location);
353 | using_directives using_directive
357 : using_alias_directive
359 if (RootContext.Documentation != null)
360 Lexer.doc_state = XmlCommentState.Allowed;
362 | using_namespace_directive
364 if (RootContext.Documentation != null)
365 Lexer.doc_state = XmlCommentState.Allowed;
369 using_alias_directive
370 : USING IDENTIFIER ASSIGN
371 namespace_or_type_name SEMICOLON
373 LocatedToken lt = (LocatedToken) $2;
374 current_namespace.UsingAlias (lt.Value, (MemberName) $4, (Location) $1);
377 CheckIdentifierToken (yyToken, GetLocation ($2));
381 using_namespace_directive
382 : USING namespace_name SEMICOLON
384 current_namespace.Using ((MemberName) $2, (Location) $1);
389 // Strictly speaking, namespaces don't have attributes but
390 // we parse global attributes along with namespace declarations and then
393 namespace_declaration
394 : opt_attributes NAMESPACE namespace_or_type_name
396 MemberName name = (MemberName) $3;
399 Report.Error(1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
402 current_namespace = new NamespaceEntry (
403 current_namespace, file, name.GetName ());
404 current_class = current_namespace.SlaveDeclSpace;
405 current_container = current_class.PartialContainer;
407 namespace_body opt_semicolon
409 current_namespace = current_namespace.Parent;
410 current_class = current_namespace.SlaveDeclSpace;
411 current_container = current_class.PartialContainer;
426 : namespace_or_type_name
432 if (RootContext.Documentation != null)
433 Lexer.doc_state = XmlCommentState.Allowed;
439 : opt_extern_alias_directives
441 opt_namespace_member_declarations
445 Report.Error (1518, lexer.Location, "Expected `class', `delegate', `enum', `interface', or `struct'");
448 | opt_extern_alias_directives
450 opt_namespace_member_declarations
453 Report.Error (1513, lexer.Location, "} expected");
462 opt_extern_alias_directives
464 | extern_alias_directives
467 opt_namespace_member_declarations
469 | namespace_member_declarations
472 namespace_member_declarations
473 : namespace_member_declaration
474 | namespace_member_declarations namespace_member_declaration
477 namespace_member_declaration
481 DeclSpace ds = (DeclSpace)$1;
483 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
484 Report.Error (1527, ds.Location,
485 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
488 current_namespace.DeclarationFound = true;
490 | namespace_declaration {
491 current_namespace.DeclarationFound = true;
494 | field_declaration {
495 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
497 | method_declaration {
498 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
505 | interface_declaration
507 | delegate_declaration
509 // Enable this when we have handled all errors, because this acts as a generic fallback
512 // Console.WriteLine ("Token=" + yyToken);
513 // Report.Error (1518, GetLocation ($1), "Expected class, struct, interface, enum or delegate");
525 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
533 global_attrs_enabled = false;
538 global_attrs_enabled = false;
547 if (current_attr_target != String.Empty) {
548 ArrayList sect = (ArrayList) $1;
550 if (global_attrs_enabled) {
551 if (current_attr_target == "module") {
552 CodeGen.Module.AddAttributes (sect);
554 } else if (current_attr_target != null && current_attr_target.Length > 0) {
555 CodeGen.Assembly.AddAttributes (sect);
558 $$ = new Attributes (sect);
561 if (RootContext.Documentation != null) {
562 Lexer.check_incorrect_doc_comment ();
564 XmlCommentState.Allowed;
568 $$ = new Attributes (sect);
573 current_attr_target = null;
575 | attribute_sections attribute_section
577 if (current_attr_target != String.Empty) {
578 Attributes attrs = $1 as Attributes;
579 ArrayList sect = (ArrayList) $2;
581 if (global_attrs_enabled) {
582 if (current_attr_target == "module") {
583 CodeGen.Module.AddAttributes (sect);
585 } else if (current_attr_target == "assembly") {
586 CodeGen.Assembly.AddAttributes (sect);
590 attrs = new Attributes (sect);
592 attrs.AddAttributes (sect);
596 attrs = new Attributes (sect);
598 attrs.AddAttributes (sect);
604 current_attr_target = null;
609 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
613 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
619 attribute_target_specifier
620 : attribute_target COLON
622 current_attr_target = (string)$1;
630 LocatedToken lt = (LocatedToken) $1;
631 $$ = CheckAttributeTarget (lt.Value, lt.Location);
633 | EVENT { $$ = "event"; }
634 | RETURN { $$ = "return"; }
637 string name = yyNames [yyToken].ToLower ();
638 $$ = CheckAttributeTarget (name, GetLocation ($1));
645 ArrayList attrs = new ArrayList (4);
651 | attribute_list COMMA attribute
653 ArrayList attrs = (ArrayList) $1;
661 : attribute_name opt_attribute_arguments
663 MemberName mname = (MemberName) $1;
664 object[] arguments = (object[]) $2;
665 MemberName left = mname.Left;
666 string identifier = mname.Name;
668 Expression left_expr = left == null ? null : left.GetTypeExpression ();
670 if (current_attr_target == String.Empty)
672 else if (current_attr_target == "assembly" || current_attr_target == "module")
673 // FIXME: supply "nameEscaped" parameter here.
674 $$ = new GlobalAttribute (current_namespace, current_attr_target,
675 left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
677 $$ = new Attribute (current_attr_target, left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
682 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
685 opt_attribute_arguments
686 : /* empty */ { $$ = null; }
687 | OPEN_PARENS attribute_arguments CLOSE_PARENS
695 : opt_positional_argument_list
700 $$ = new object [] { $1, null };
703 | positional_argument_list COMMA named_argument_list
705 $$ = new object[] { $1, $3 };
707 | named_argument_list
709 $$ = new object [] { null, $1 };
714 opt_positional_argument_list
715 : /* empty */ { $$ = null; }
716 | positional_argument_list
719 positional_argument_list
722 ArrayList args = new ArrayList (4);
723 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
727 | positional_argument_list COMMA expression
729 ArrayList args = (ArrayList) $1;
730 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
739 ArrayList args = new ArrayList (4);
744 | named_argument_list COMMA named_argument
746 ArrayList args = (ArrayList) $1;
751 | named_argument_list COMMA expression
753 Report.Error (1016, ((Expression) $3).Location, "Named attribute argument expected");
759 : IDENTIFIER ASSIGN expression
761 // FIXME: keep location
762 $$ = new DictionaryEntry (
763 ((LocatedToken) $1).Value,
764 new Argument ((Expression) $3, Argument.AType.Expression));
770 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
773 opt_class_member_declarations
775 | class_member_declarations
778 class_member_declarations
779 : class_member_declaration
780 | class_member_declarations
781 class_member_declaration
784 class_member_declaration
785 : constant_declaration // done
786 | field_declaration // done
787 | method_declaration // done
788 | property_declaration // done
789 | event_declaration // done
790 | indexer_declaration // done
791 | operator_declaration // done
792 | constructor_declaration // done
793 | destructor_declaration // done
803 MemberName name = MakeName ((MemberName) $5);
804 push_current_class (new Struct (
805 current_namespace, current_class, name, (int) $2,
806 (Attributes) $1), $3);
812 current_container.AddBasesForPart (current_class, (ArrayList) $7);
814 if (RootContext.Documentation != null)
815 current_container.DocComment = Lexer.consume_doc_comment ();
819 if (RootContext.Documentation != null)
820 Lexer.doc_state = XmlCommentState.Allowed;
824 $$ = pop_current_class ();
826 | opt_attributes opt_modifiers opt_partial STRUCT error {
827 CheckIdentifierToken (yyToken, GetLocation ($5));
834 if (RootContext.Documentation != null)
835 Lexer.doc_state = XmlCommentState.Allowed;
837 opt_struct_member_declarations CLOSE_BRACE
840 opt_struct_member_declarations
842 | struct_member_declarations
845 struct_member_declarations
846 : struct_member_declaration
847 | struct_member_declarations struct_member_declaration
850 struct_member_declaration
851 : constant_declaration
854 | property_declaration
856 | indexer_declaration
857 | operator_declaration
858 | constructor_declaration
862 * This is only included so we can flag error 575:
863 * destructors only allowed on class types
865 | destructor_declaration
876 int modflags = (int) $2;
877 foreach (VariableDeclaration constant in (ArrayList) $5){
878 Location l = constant.Location;
879 if ((modflags & Modifiers.STATIC) != 0) {
880 Report.Error (504, l, "The constant `{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
884 Const c = new Const (
885 current_class, (Expression) $4, (string) constant.identifier,
886 (Expression) constant.expression_or_array_initializer, modflags,
889 if (RootContext.Documentation != null) {
890 c.DocComment = Lexer.consume_doc_comment ();
891 Lexer.doc_state = XmlCommentState.Allowed;
893 current_container.AddConstant (c);
899 : constant_declarator
901 ArrayList constants = new ArrayList (4);
906 | constant_declarators COMMA constant_declarator
909 ArrayList constants = (ArrayList) $1;
916 : IDENTIFIER ASSIGN constant_expression
918 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
922 // A const field requires a value to be provided
923 Report.Error (145, ((LocatedToken) $1).Location, "A const field requires a value to be provided");
935 Expression type = (Expression) $3;
938 current_array_type = null;
940 foreach (VariableDeclaration var in (ArrayList) $4){
941 Field field = new Field (current_class, type, mod, var.identifier,
942 (Attributes) $1, var.Location);
944 field.Initializer = var.expression_or_array_initializer;
946 if (RootContext.Documentation != null) {
947 field.DocComment = Lexer.consume_doc_comment ();
948 Lexer.doc_state = XmlCommentState.Allowed;
950 current_container.AddField (field);
951 $$ = field; // FIXME: might be better if it points to the top item
958 fixed_variable_declarators
961 Expression type = (Expression) $4;
964 current_array_type = null;
966 foreach (VariableDeclaration var in (ArrayList) $5) {
967 FixedField field = new FixedField (current_class, type, mod, var.identifier,
968 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
970 if (RootContext.Documentation != null) {
971 field.DocComment = Lexer.consume_doc_comment ();
972 Lexer.doc_state = XmlCommentState.Allowed;
974 current_container.AddField (field);
975 $$ = field; // FIXME: might be better if it points to the top item
984 Report.Error (1641, GetLocation ($4), "A fixed size buffer field must have the array size specifier after the field name");
991 current_array_type = null;
992 Report.Error (670, (Location) $3, "Fields cannot have void type");
996 fixed_variable_declarators
997 : fixed_variable_declarator
999 ArrayList decl = new ArrayList (2);
1003 | fixed_variable_declarators COMMA fixed_variable_declarator
1005 ArrayList decls = (ArrayList) $1;
1011 fixed_variable_declarator
1012 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
1014 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1016 | IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1018 Report.Error (443, lexer.Location, "Value or constant expected");
1019 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1023 variable_declarators
1024 : variable_declarator
1026 ArrayList decl = new ArrayList (4);
1031 | variable_declarators COMMA variable_declarator
1033 ArrayList decls = (ArrayList) $1;
1040 : IDENTIFIER ASSIGN variable_initializer
1042 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1046 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1048 | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
1050 Report.Error (650, ((LocatedToken) $1).Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
1051 "To declare a fixed size buffer field, use the fixed keyword before the field type");
1056 variable_initializer
1065 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
1067 $$ = new StackAlloc ((Expression) $2, (Expression) $4, (Location) $1);
1071 $$ = new ArglistAccess ((Location) $1);
1075 Report.Error (1575, (Location) $1, "A stackalloc expression requires [] after type");
1082 anonymous_host = (IAnonymousHost) $1;
1083 if (RootContext.Documentation != null)
1084 Lexer.doc_state = XmlCommentState.NotAllowed;
1088 Method method = (Method) $1;
1089 method.Block = (ToplevelBlock) $3;
1090 current_container.AddMethod (method);
1092 anonymous_host = null;
1093 current_local_parameters = null;
1095 if (RootContext.Documentation != null)
1096 Lexer.doc_state = XmlCommentState.Allowed;
1109 Report.Error (1585, lexer.Location,
1110 "Member modifier `{0}' must precede the member type and name",
1111 Modifiers.Name (i));
1120 // This rule is used to handle the cases where OPEN_PARENS_LAMBDA
1121 // is returned (type followed by an identifier), these are the
1122 // declarations (methods, overloads, constructors, etc) and a handful
1123 // of expressions ("using", "fixed") or "catch".
1127 | OPEN_PARENS_LAMBDA
1133 type namespace_or_type_name
1134 open_parens opt_formal_parameter_list CLOSE_PARENS
1136 MemberName name = (MemberName) $4;
1138 Method method = new Method (current_class, null, (Expression) $3, (int) $2,
1139 false, name, (Parameters) $6, (Attributes) $1);
1141 anonymous_host = method;
1142 current_local_parameters = (Parameters) $6;
1144 if (RootContext.Documentation != null)
1145 method.DocComment = Lexer.consume_doc_comment ();
1151 VOID namespace_or_type_name
1152 open_parens opt_formal_parameter_list CLOSE_PARENS
1154 MemberName name = (MemberName) $4;
1156 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1157 (int) $2, false, name, (Parameters) $6,
1160 anonymous_host = method;
1161 current_local_parameters = (Parameters) $6;
1163 if (RootContext.Documentation != null)
1164 method.DocComment = Lexer.consume_doc_comment ();
1171 modifiers namespace_or_type_name open_parens opt_formal_parameter_list CLOSE_PARENS
1173 MemberName name = (MemberName) $5;
1174 Report.Error (1585, name.Location,
1175 "Member modifier `{0}' must precede the member type and name", Modifiers.Name ((int) $4));
1177 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1178 0, false, name, (Parameters) $7, (Attributes) $1);
1180 current_local_parameters = (Parameters) $7;
1182 if (RootContext.Documentation != null)
1183 method.DocComment = Lexer.consume_doc_comment ();
1191 | SEMICOLON { $$ = null; }
1194 opt_formal_parameter_list
1195 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1196 | formal_parameter_list
1199 formal_parameter_list
1202 ArrayList pars_list = (ArrayList) $1;
1204 Parameter [] pars = new Parameter [pars_list.Count];
1205 pars_list.CopyTo (pars);
1207 $$ = new Parameters (pars);
1209 | fixed_parameters COMMA parameter_array
1211 ArrayList pars_list = (ArrayList) $1;
1214 Parameter [] pars = new Parameter [pars_list.Count];
1215 pars_list.CopyTo (pars);
1217 $$ = new Parameters (pars);
1219 | fixed_parameters COMMA ARGLIST
1221 ArrayList pars_list = (ArrayList) $1;
1222 //pars_list.Add (new ArglistParameter (GetLocation ($3)));
1224 Parameter [] pars = new Parameter [pars_list.Count];
1225 pars_list.CopyTo (pars);
1227 $$ = new Parameters (pars, true);
1229 | parameter_array COMMA error
1232 Report.Error (231, ((Parameter) $1).Location, "A params parameter must be the last parameter in a formal parameter list");
1235 | fixed_parameters COMMA parameter_array COMMA error
1238 Report.Error (231, ((Parameter) $3).Location, "A params parameter must be the last parameter in a formal parameter list");
1241 | ARGLIST COMMA error
1243 Report.Error (257, (Location) $1, "An __arglist parameter must be the last parameter in a formal parameter list");
1246 | fixed_parameters COMMA ARGLIST COMMA error
1248 Report.Error (257, (Location) $3, "An __arglist parameter must be the last parameter in a formal parameter list");
1253 $$ = new Parameters (new Parameter[] { (Parameter) $1 } );
1257 $$ = new Parameters (new Parameter[0], true);
1264 ArrayList pars = new ArrayList (4);
1269 | fixed_parameters COMMA fixed_parameter
1271 ArrayList pars = (ArrayList) $1;
1280 opt_parameter_modifier
1284 LocatedToken lt = (LocatedToken) $4;
1285 $$ = new Parameter ((Expression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1288 opt_parameter_modifier
1290 IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1292 LocatedToken lt = (LocatedToken) $4;
1293 Report.Error (1552, lt.Location, "Array type specifier, [], must appear before parameter name");
1297 opt_parameter_modifier
1300 Report.Error (1001, GetLocation ($3), "Identifier expected");
1304 opt_parameter_modifier
1307 CheckIdentifierToken (yyToken, GetLocation ($4));
1311 opt_parameter_modifier
1317 LocatedToken lt = (LocatedToken) $4;
1318 Report.Error (241, lt.Location, "Default parameter specifiers are not permitted");
1323 opt_parameter_modifier
1324 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1325 | parameter_modifiers
1329 : parameter_modifier
1333 | parameter_modifiers parameter_modifier
1335 Parameter.Modifier p2 = (Parameter.Modifier)$2;
1336 Parameter.Modifier mod = (Parameter.Modifier)$1 | p2;
1337 if (((Parameter.Modifier)$1 & p2) == p2) {
1338 Error_DuplicateParameterModifier (lexer.Location, p2);
1340 Report.Error (1108, lexer.Location, "A parameter cannot have specified more than one modifier");
1347 : REF { $$ = Parameter.Modifier.REF; }
1348 | OUT { $$ = Parameter.Modifier.OUT; }
1349 | THIS { $$ = Parameter.Modifier.This; }
1353 : opt_attributes PARAMS type IDENTIFIER
1355 LocatedToken lt = (LocatedToken) $4;
1356 $$ = new ParamsParameter ((Expression) $3, lt.Value, (Attributes) $1, lt.Location);
1358 | opt_attributes PARAMS PARAMS type IDENTIFIER
1360 Error_DuplicateParameterModifier (lexer.Location, Parameter.Modifier.PARAMS);
1363 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1365 Parameter.Modifier mod = (Parameter.Modifier)$3;
1366 if ((mod & Parameter.Modifier.This) != 0) {
1367 Report.Error (1104, lexer.Location, "The parameter modifiers `this' and `params' cannot be used altogether");
1369 Report.Error (1611, (Location) $2, "The params parameter cannot be declared as ref or out");
1373 | opt_attributes PARAMS type error {
1374 CheckIdentifierToken (yyToken, GetLocation ($4));
1379 property_declaration
1383 namespace_or_type_name
1385 if (RootContext.Documentation != null)
1386 tmpComment = Lexer.consume_doc_comment ();
1390 implicit_value_parameter_type = (Expression) $3;
1392 lexer.PropertyParsing = true;
1394 accessor_declarations
1396 lexer.PropertyParsing = false;
1397 has_get = has_set = false;
1405 Accessors accessors = (Accessors) $8;
1406 Accessor get_block = accessors.get_or_add;
1407 Accessor set_block = accessors.set_or_remove;
1409 MemberName name = (MemberName) $4;
1411 prop = new Property (current_class, (Expression) $3, (int) $2,
1412 false, name, (Attributes) $1, get_block, set_block, accessors.declared_in_reverse);
1414 current_container.AddProperty (prop);
1415 implicit_value_parameter_type = null;
1417 if (RootContext.Documentation != null)
1418 prop.DocComment = ConsumeStoredComment ();
1423 accessor_declarations
1424 : get_accessor_declaration
1426 $$ = new Accessors ((Accessor) $1, null);
1428 | get_accessor_declaration accessor_declarations
1430 Accessors accessors = (Accessors) $2;
1431 accessors.get_or_add = (Accessor) $1;
1434 | set_accessor_declaration
1436 $$ = new Accessors (null, (Accessor) $1);
1438 | set_accessor_declaration accessor_declarations
1440 Accessors accessors = (Accessors) $2;
1441 accessors.set_or_remove = (Accessor) $1;
1442 accessors.declared_in_reverse = true;
1447 Report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1452 get_accessor_declaration
1453 : opt_attributes opt_modifiers GET
1455 // If this is not the case, then current_local_parameters has already
1456 // been set in indexer_declaration
1457 if (parsing_indexer == false)
1458 current_local_parameters = null;
1460 current_local_parameters = indexer_parameters;
1461 lexer.PropertyParsing = false;
1463 anonymous_host = SimpleAnonymousHost.GetSimple ();
1468 Report.Error (1007, (Location) $3, "Property accessor already defined");
1471 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1473 current_local_parameters = null;
1474 lexer.PropertyParsing = true;
1476 SimpleAnonymousHost.Simple.Propagate (accessor);
1477 anonymous_host = null;
1479 if (RootContext.Documentation != null)
1480 if (Lexer.doc_state == XmlCommentState.Error)
1481 Lexer.doc_state = XmlCommentState.NotAllowed;
1487 set_accessor_declaration
1488 : opt_attributes opt_modifiers SET
1491 Parameter implicit_value_parameter = new Parameter (
1492 implicit_value_parameter_type, "value",
1493 Parameter.Modifier.NONE, null, (Location) $3);
1495 if (parsing_indexer == false) {
1496 args = new Parameter [1];
1497 args [0] = implicit_value_parameter;
1498 current_local_parameters = new Parameters (args);
1500 Parameter [] fpars = indexer_parameters.FixedParameters;
1503 int count = fpars.Length;
1505 args = new Parameter [count + 1];
1506 fpars.CopyTo (args, 0);
1507 args [count] = implicit_value_parameter;
1510 current_local_parameters = new Parameters (
1514 lexer.PropertyParsing = false;
1516 anonymous_host = SimpleAnonymousHost.GetSimple ();
1521 Report.Error (1007, ((LocatedToken) $3).Location, "Property accessor already defined");
1524 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1526 current_local_parameters = null;
1527 lexer.PropertyParsing = true;
1529 SimpleAnonymousHost.Simple.Propagate (accessor);
1530 anonymous_host = null;
1532 if (RootContext.Documentation != null
1533 && Lexer.doc_state == XmlCommentState.Error)
1534 Lexer.doc_state = XmlCommentState.NotAllowed;
1542 | SEMICOLON { $$ = null; }
1545 interface_declaration
1549 INTERFACE member_name
1551 MemberName name = MakeName ((MemberName) $5);
1553 push_current_class (new Interface (
1554 current_namespace, current_class, name, (int) $2,
1555 (Attributes) $1), $3);
1559 current_container.AddBasesForPart (current_class, (ArrayList) $7);
1561 if (RootContext.Documentation != null) {
1562 current_container.DocComment = Lexer.consume_doc_comment ();
1563 Lexer.doc_state = XmlCommentState.Allowed;
1568 if (RootContext.Documentation != null)
1569 Lexer.doc_state = XmlCommentState.Allowed;
1573 $$ = pop_current_class ();
1575 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1576 CheckIdentifierToken (yyToken, GetLocation ($5));
1582 opt_interface_member_declarations
1586 opt_interface_member_declarations
1588 | interface_member_declarations
1591 interface_member_declarations
1592 : interface_member_declaration
1593 | interface_member_declarations interface_member_declaration
1596 interface_member_declaration
1597 : interface_method_declaration
1602 Method m = (Method) $1;
1604 if (m.IsExplicitImpl)
1605 Report.Error (541, m.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1606 m.GetSignatureForError ());
1608 current_container.AddMethod (m);
1610 if (RootContext.Documentation != null)
1611 Lexer.doc_state = XmlCommentState.Allowed;
1613 | interface_property_declaration
1618 Property p = (Property) $1;
1620 if (p.IsExplicitImpl)
1621 Report.Error (541, p.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1622 p.GetSignatureForError ());
1624 current_container.AddProperty (p);
1626 if (RootContext.Documentation != null)
1627 Lexer.doc_state = XmlCommentState.Allowed;
1629 | interface_event_declaration
1632 Event e = (Event) $1;
1634 if (e.IsExplicitImpl)
1635 Report.Error (541, e.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1636 e.GetSignatureForError ());
1638 current_container.AddEvent (e);
1641 if (RootContext.Documentation != null)
1642 Lexer.doc_state = XmlCommentState.Allowed;
1644 | interface_indexer_declaration
1649 Indexer i = (Indexer) $1;
1651 if (i.IsExplicitImpl)
1652 Report.Error (541, i.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1653 i.GetSignatureForError ());
1655 current_container.AddIndexer (i);
1657 if (RootContext.Documentation != null)
1658 Lexer.doc_state = XmlCommentState.Allowed;
1660 | delegate_declaration
1663 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1664 ((MemberCore)$1).GetSignatureForError ());
1670 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1671 ((MemberCore)$1).GetSignatureForError ());
1674 | struct_declaration
1677 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1678 ((MemberCore)$1).GetSignatureForError ());
1684 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1685 ((MemberCore)$1).GetSignatureForError ());
1688 | interface_declaration
1691 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1692 ((MemberCore)$1).GetSignatureForError ());
1695 | constant_declaration
1697 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1705 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, GetLocation ($1));
1710 interface_method_declaration
1711 : opt_attributes opt_new type namespace_or_type_name
1712 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1715 $$ = new Method (current_class, null, (Expression) $3, (int) $2, true,
1716 (MemberName) $4, (Parameters) $6, (Attributes) $1);
1717 if (RootContext.Documentation != null)
1718 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1720 | opt_attributes opt_new type namespace_or_type_name
1721 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1722 OPEN_BRACE opt_statement_list CLOSE_BRACE
1724 $$ = new Method (current_class, null, (Expression) $3, (int) $2, true,
1725 (MemberName) $4, (Parameters) $6, (Attributes) $1);
1727 Report.Error (531, lexer.Location, "`{0}': interface members cannot have a definition",
1728 ((Method)$$).GetSignatureForError ());
1732 interface_property_declaration
1737 { lexer.PropertyParsing = true; }
1738 accessor_declarations
1740 has_get = has_set = false;
1741 lexer.PropertyParsing = false;
1745 LocatedToken lt = (LocatedToken) $4;
1746 MemberName name = new MemberName (lt.Value, lt.Location);
1748 if ($3 == TypeManager.system_void_expr) {
1749 Report.Error (547, lt.Location, "`{0}': property or indexer cannot have void type", lt.Value);
1755 p = new Property (current_class, (Expression) $3, (int) $2, true,
1756 name, (Attributes) $1,
1759 Report.Error (548, p.Location, "`{0}': property or indexer must have at least one accessor", p.GetSignatureForError ());
1763 Accessors accessor = (Accessors) $7;
1764 p = new Property (current_class, (Expression) $3, (int) $2, true,
1765 name, (Attributes) $1,
1766 accessor.get_or_add, accessor.set_or_remove, accessor.declared_in_reverse);
1768 if (accessor.get_or_add != null && accessor.get_or_add.Block != null) {
1769 Report.Error (531, p.Location, "`{0}.get': interface members cannot have a definition", p.GetSignatureForError ());
1774 if (accessor.set_or_remove != null && accessor.set_or_remove.Block != null) {
1775 Report.Error (531, p.Location, "`{0}.set': interface members cannot have a definition", p.GetSignatureForError ());
1780 if (RootContext.Documentation != null)
1781 p.DocComment = Lexer.consume_doc_comment ();
1788 CheckIdentifierToken (yyToken, GetLocation ($4));
1794 interface_event_declaration
1795 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1797 LocatedToken lt = (LocatedToken) $5;
1798 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1799 new MemberName (lt.Value, lt.Location),
1801 if (RootContext.Documentation != null)
1802 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1804 | opt_attributes opt_new EVENT type error {
1805 CheckIdentifierToken (yyToken, GetLocation ($5));
1808 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1809 LocatedToken lt = (LocatedToken) $5;
1810 Report.Error (68, lt.Location, "`{0}.{1}': event in interface cannot have initializer", current_container.Name, lt.Value);
1813 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1815 lexer.EventParsing = true;
1817 event_accessor_declarations
1819 lexer.EventParsing = false;
1822 Report.Error (69, (Location) $3, "Event in interface cannot have add or remove accessors");
1827 interface_indexer_declaration
1828 : opt_attributes opt_new type THIS
1829 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1831 { lexer.PropertyParsing = true; }
1832 accessor_declarations
1834 has_get = has_set = false;
1835 lexer.PropertyParsing = false;
1841 i = new Indexer (current_class, (Expression) $3,
1842 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1843 (int) $2, true, (Parameters) $6, (Attributes) $1,
1846 Report.Error (548, i.Location, "`{0}': property or indexer must have at least one accessor", i.GetSignatureForError ());
1850 Accessors accessors = (Accessors) $10;
1851 i = new Indexer (current_class, (Expression) $3,
1852 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1853 (int) $2, true, (Parameters) $6, (Attributes) $1,
1854 accessors.get_or_add, accessors.set_or_remove, accessors.declared_in_reverse);
1856 if (accessors.get_or_add != null && accessors.get_or_add.Block != null) {
1857 Report.Error (531, i.Location, "`{0}.get': interface members cannot have a definition", i.GetSignatureForError ());
1862 if (accessors.set_or_remove != null && accessors.set_or_remove.Block != null) {
1863 Report.Error (531, i.Location, "`{0}.set': interface members cannot have a definition", i.GetSignatureForError ());
1868 if (RootContext.Documentation != null)
1869 i.DocComment = ConsumeStoredComment ();
1875 operator_declaration
1876 : opt_attributes opt_modifiers operator_declarator
1878 anonymous_host = SimpleAnonymousHost.GetSimple ();
1885 OperatorDeclaration decl = (OperatorDeclaration) $3;
1887 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1889 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null, decl.location);
1890 if (decl.arg2type != null)
1891 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null, decl.location);
1893 Operator op = new Operator (
1894 current_class, decl.optype, decl.ret_type, (int) $2,
1895 new Parameters (param_list),
1896 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1898 if (RootContext.Documentation != null) {
1899 op.DocComment = tmpComment;
1900 Lexer.doc_state = XmlCommentState.Allowed;
1903 SimpleAnonymousHost.Simple.Propagate (op);
1904 anonymous_host = null;
1906 // Note again, checking is done in semantic analysis
1907 current_container.AddOperator (op);
1909 current_local_parameters = null;
1915 | SEMICOLON { $$ = null; }
1918 : type OPERATOR overloadable_operator
1919 open_parens opt_attributes opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
1921 // TODO: wrong location
1922 if ((Parameter.Modifier)$6 != Parameter.Modifier.NONE)
1923 Error_ParameterModifierNotValid ((Location) $2);
1925 LocatedToken lt = (LocatedToken) $8;
1926 Operator.OpType op = (Operator.OpType) $3;
1927 CheckUnaryOperator (op, lt.Location);
1929 if (op == Operator.OpType.Addition)
1930 op = Operator.OpType.UnaryPlus;
1932 if (op == Operator.OpType.Subtraction)
1933 op = Operator.OpType.UnaryNegation;
1935 Parameter [] pars = new Parameter [1];
1936 Expression type = (Expression) $7;
1938 pars [0] = new Parameter (type, lt.Value, Parameter.Modifier.NONE, (Attributes) $5, lt.Location);
1940 current_local_parameters = new Parameters (pars);
1942 if (RootContext.Documentation != null) {
1943 tmpComment = Lexer.consume_doc_comment ();
1944 Lexer.doc_state = XmlCommentState.NotAllowed;
1947 $$ = new OperatorDeclaration (op, (Expression) $1, type, lt.Value,
1948 null, null, (Location) $2);
1950 | type OPERATOR overloadable_operator
1952 opt_attributes opt_parameter_modifier type IDENTIFIER COMMA
1953 opt_attributes opt_parameter_modifier type IDENTIFIER
1956 // TODO: wrong location
1957 if ((Parameter.Modifier)$6 != Parameter.Modifier.NONE || (Parameter.Modifier)$11 != Parameter.Modifier.NONE)
1958 Error_ParameterModifierNotValid ((Location) $2);
1960 LocatedToken ltParam1 = (LocatedToken) $8;
1961 LocatedToken ltParam2 = (LocatedToken) $13;
1962 CheckBinaryOperator ((Operator.OpType) $3, (Location) $2);
1964 Parameter [] pars = new Parameter [2];
1966 Expression typeL = (Expression) $7;
1967 Expression typeR = (Expression) $12;
1969 pars [0] = new Parameter (typeL, ltParam1.Value, Parameter.Modifier.NONE, (Attributes) $5, ltParam1.Location);
1970 pars [1] = new Parameter (typeR, ltParam2.Value, Parameter.Modifier.NONE, (Attributes) $10, ltParam2.Location);
1972 current_local_parameters = new Parameters (pars);
1974 if (RootContext.Documentation != null) {
1975 tmpComment = Lexer.consume_doc_comment ();
1976 Lexer.doc_state = XmlCommentState.NotAllowed;
1979 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1980 typeL, ltParam1.Value,
1981 typeR, ltParam2.Value, (Location) $2);
1983 | conversion_operator_declarator
1984 | type OPERATOR overloadable_operator
1986 opt_attributes opt_parameter_modifier type IDENTIFIER COMMA
1987 opt_attributes opt_parameter_modifier type IDENTIFIER COMMA error
1989 Report.Error (1534, (Location) $2, "Overloaded binary operator `{0}' takes two parameters",
1990 Operator.GetName ((Operator.OpType) $3));
1993 | type OPERATOR overloadable_operator
1994 open_parens CLOSE_PARENS
1996 Report.Error (1535, (Location) $2, "Overloaded unary operator `{0}' takes one parameter",
1997 Operator.GetName ((Operator.OpType) $3));
2002 overloadable_operator
2004 : BANG { $$ = Operator.OpType.LogicalNot; }
2005 | TILDE { $$ = Operator.OpType.OnesComplement; }
2006 | OP_INC { $$ = Operator.OpType.Increment; }
2007 | OP_DEC { $$ = Operator.OpType.Decrement; }
2008 | TRUE { $$ = Operator.OpType.True; }
2009 | FALSE { $$ = Operator.OpType.False; }
2010 // Unary and binary:
2011 | PLUS { $$ = Operator.OpType.Addition; }
2012 | MINUS { $$ = Operator.OpType.Subtraction; }
2014 | STAR { $$ = Operator.OpType.Multiply; }
2015 | DIV { $$ = Operator.OpType.Division; }
2016 | PERCENT { $$ = Operator.OpType.Modulus; }
2017 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
2018 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
2019 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
2020 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
2021 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
2022 | OP_EQ { $$ = Operator.OpType.Equality; }
2023 | OP_NE { $$ = Operator.OpType.Inequality; }
2024 | OP_GT { $$ = Operator.OpType.GreaterThan; }
2025 | OP_LT { $$ = Operator.OpType.LessThan; }
2026 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
2027 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
2030 conversion_operator_declarator
2031 : IMPLICIT OPERATOR type open_parens opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
2033 // TODO: wrong location
2034 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
2035 Error_ParameterModifierNotValid (GetLocation ($4));
2037 LocatedToken lt = (LocatedToken) $7;
2038 Parameter [] pars = new Parameter [1];
2040 pars [0] = new Parameter ((Expression) $6, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2042 current_local_parameters = new Parameters (pars);
2044 if (RootContext.Documentation != null) {
2045 tmpComment = Lexer.consume_doc_comment ();
2046 Lexer.doc_state = XmlCommentState.NotAllowed;
2049 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $6, lt.Value,
2050 null, null, (Location) $2);
2052 | EXPLICIT OPERATOR type open_parens opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
2054 // TODO: wrong location
2055 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
2056 Error_ParameterModifierNotValid (GetLocation ($4));
2058 LocatedToken lt = (LocatedToken) $7;
2059 Parameter [] pars = new Parameter [1];
2061 pars [0] = new Parameter ((Expression) $6, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2063 current_local_parameters = new Parameters (pars);
2065 if (RootContext.Documentation != null) {
2066 tmpComment = Lexer.consume_doc_comment ();
2067 Lexer.doc_state = XmlCommentState.NotAllowed;
2070 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $6, lt.Value,
2071 null, null, (Location) $2);
2075 syntax_error ((Location) $1, "'operator' expected");
2079 syntax_error ((Location) $1, "'operator' expected");
2083 constructor_declaration
2086 constructor_declarator
2089 Constructor c = (Constructor) $3;
2090 c.Block = (ToplevelBlock) $4;
2091 c.OptAttributes = (Attributes) $1;
2092 c.ModFlags = (int) $2;
2094 if (RootContext.Documentation != null)
2095 c.DocComment = ConsumeStoredComment ();
2097 if (c.Name == current_container.Basename){
2098 if ((c.ModFlags & Modifiers.STATIC) != 0){
2099 if ((c.ModFlags & Modifiers.Accessibility) != 0){
2100 Report.Error (515, c.Location,
2101 "`{0}': access modifiers are not allowed on static constructors",
2102 c.GetSignatureForError ());
2105 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2107 if (c.Initializer != null){
2108 Report.Error (514, c.Location,
2109 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2110 c.GetSignatureForError ());
2113 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2116 // We let another layer check the validity of the constructor.
2117 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
2120 current_container.AddConstructor (c);
2122 current_local_parameters = null;
2123 if (RootContext.Documentation != null)
2124 Lexer.doc_state = XmlCommentState.Allowed;
2128 constructor_declarator
2129 : constructor_header
2133 | constructor_header constructor_initializer
2135 ((Constructor)$1).Initializer = (ConstructorInitializer) $2;
2143 if (RootContext.Documentation != null) {
2144 tmpComment = Lexer.consume_doc_comment ();
2145 Lexer.doc_state = XmlCommentState.Allowed;
2148 open_parens opt_formal_parameter_list CLOSE_PARENS
2150 LocatedToken lt = (LocatedToken) $1;
2151 current_local_parameters = (Parameters) $4;
2152 current_block = new ToplevelBlock (null, current_local_parameters, null, lt.Location);
2154 $$ = new Constructor (current_class, lt.Value, 0, current_local_parameters,
2157 anonymous_host = (IAnonymousHost) $$;
2163 | SEMICOLON { current_block = null; $$ = null; }
2166 constructor_initializer
2167 : COLON BASE open_parens opt_argument_list CLOSE_PARENS
2169 $$ = new ConstructorBaseInitializer ((ArrayList) $4, (Location) $2);
2171 | COLON THIS open_parens opt_argument_list CLOSE_PARENS
2173 $$ = new ConstructorThisInitializer ((ArrayList) $4, (Location) $2);
2176 Report.Error (1018, (Location) $1, "Keyword this or base expected");
2182 : /* EMPTY */ { $$ = 0; }
2183 | UNSAFE { $$ = Modifiers.UNSAFE; }
2184 | EXTERN { $$ = Modifiers.EXTERN; }
2187 destructor_declaration
2188 : opt_attributes opt_finalizer TILDE
2190 if (RootContext.Documentation != null) {
2191 tmpComment = Lexer.consume_doc_comment ();
2192 Lexer.doc_state = XmlCommentState.NotAllowed;
2195 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2197 LocatedToken lt = (LocatedToken) $5;
2198 if (lt.Value != current_container.Basename){
2199 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2200 } else if (current_container.Kind != Kind.Class){
2201 Report.Error (575, lt.Location, "Only class types can contain destructor");
2203 Location l = lt.Location;
2206 if (!RootContext.StdLib && current_container.Name == "System.Object")
2207 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2209 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2211 Method d = new Destructor (
2212 current_class, TypeManager.system_void_expr, m, "Finalize",
2213 Parameters.EmptyReadOnlyParameters, (Attributes) $1, l);
2214 if (RootContext.Documentation != null)
2215 d.DocComment = ConsumeStoredComment ();
2217 d.Block = (ToplevelBlock) $8;
2218 current_container.AddMethod (d);
2226 EVENT type variable_declarators SEMICOLON
2228 current_array_type = null;
2229 foreach (VariableDeclaration var in (ArrayList) $5) {
2231 MemberName name = new MemberName (var.identifier,
2234 EventField e = new EventField (
2235 current_class, (Expression) $4, (int) $2, false, name,
2238 e.Initializer = var.expression_or_array_initializer;
2240 current_container.AddEvent (e);
2242 if (RootContext.Documentation != null) {
2243 e.DocComment = Lexer.consume_doc_comment ();
2244 Lexer.doc_state = XmlCommentState.Allowed;
2250 EVENT type namespace_or_type_name
2253 implicit_value_parameter_type = (Expression) $4;
2254 lexer.EventParsing = true;
2256 event_accessor_declarations
2258 lexer.EventParsing = false;
2262 MemberName name = (MemberName) $5;
2265 Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2266 current_container.Name, name.ToString ());
2269 Accessors accessors = (Accessors) $8;
2270 if (accessors.get_or_add == null || accessors.set_or_remove == null)
2271 // CS0073 is already reported, so no CS0065 here.
2274 Event e = new EventProperty (
2275 current_class, (Expression) $4, (int) $2, false, name,
2276 (Attributes) $1, accessors.get_or_add, accessors.set_or_remove);
2277 if (RootContext.Documentation != null) {
2278 e.DocComment = Lexer.consume_doc_comment ();
2279 Lexer.doc_state = XmlCommentState.Allowed;
2282 current_container.AddEvent (e);
2283 implicit_value_parameter_type = null;
2287 | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
2288 MemberName mn = (MemberName) $5;
2290 if (mn.Left != null)
2291 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2293 Report.Error (71, mn.Location, "Event declaration should use property syntax");
2295 if (RootContext.Documentation != null)
2296 Lexer.doc_state = XmlCommentState.Allowed;
2300 event_accessor_declarations
2301 : add_accessor_declaration remove_accessor_declaration
2303 $$ = new Accessors ((Accessor) $1, (Accessor) $2);
2305 | remove_accessor_declaration add_accessor_declaration
2307 Accessors accessors = new Accessors ((Accessor) $2, (Accessor) $1);
2308 accessors.declared_in_reverse = true;
2311 | add_accessor_declaration { $$ = null; }
2312 | remove_accessor_declaration { $$ = null; }
2315 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2321 add_accessor_declaration
2322 : opt_attributes ADD
2324 Parameter [] args = new Parameter [1];
2325 Parameter implicit_value_parameter = new Parameter (
2326 implicit_value_parameter_type, "value",
2327 Parameter.Modifier.NONE, null, (Location) $2);
2329 args [0] = implicit_value_parameter;
2331 current_local_parameters = new Parameters (args);
2332 lexer.EventParsing = false;
2334 anonymous_host = SimpleAnonymousHost.GetSimple ();
2338 Accessor accessor = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2339 lexer.EventParsing = true;
2341 current_local_parameters = null;
2342 SimpleAnonymousHost.Simple.Propagate (accessor);
2343 anonymous_host = null;
2347 | opt_attributes ADD error {
2348 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2351 | opt_attributes modifiers ADD {
2352 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2357 remove_accessor_declaration
2358 : opt_attributes REMOVE
2360 Parameter [] args = new Parameter [1];
2361 Parameter implicit_value_parameter = new Parameter (
2362 implicit_value_parameter_type, "value",
2363 Parameter.Modifier.NONE, null, (Location) $2);
2365 args [0] = implicit_value_parameter;
2367 current_local_parameters = new Parameters (args);
2368 lexer.EventParsing = false;
2372 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2373 lexer.EventParsing = true;
2375 | opt_attributes REMOVE error {
2376 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2379 | opt_attributes modifiers REMOVE {
2380 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2386 : opt_attributes opt_modifiers indexer_declarator
2389 IndexerDeclaration decl = (IndexerDeclaration) $3;
2391 implicit_value_parameter_type = decl.type;
2393 lexer.PropertyParsing = true;
2394 parsing_indexer = true;
2396 indexer_parameters = decl.param_list;
2397 anonymous_host = SimpleAnonymousHost.GetSimple ();
2399 accessor_declarations
2401 lexer.PropertyParsing = false;
2402 has_get = has_set = false;
2403 parsing_indexer = false;
2410 // The signature is computed from the signature of the indexer. Look
2411 // at section 3.6 on the spec
2413 IndexerDeclaration decl = (IndexerDeclaration) $3;
2414 Location loc = decl.location;
2415 Accessors accessors = (Accessors) $6;
2416 Accessor get_block = accessors.get_or_add;
2417 Accessor set_block = accessors.set_or_remove;
2420 if (decl.interface_type != null)
2421 name = new MemberName (decl.interface_type, TypeContainer.DefaultIndexerName, loc);
2423 name = new MemberName (TypeContainer.DefaultIndexerName, loc);
2425 indexer = new Indexer (current_class, decl.type, name,
2426 (int) $2, false, decl.param_list, (Attributes) $1,
2427 get_block, set_block, accessors.declared_in_reverse);
2429 if (RootContext.Documentation != null)
2430 indexer.DocComment = ConsumeStoredComment ();
2432 current_container.AddIndexer (indexer);
2434 current_local_parameters = null;
2435 implicit_value_parameter_type = null;
2436 indexer_parameters = null;
2441 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2443 Parameters pars = (Parameters) $4;
2444 if (pars.HasArglist) {
2445 // "__arglist is not valid in this context"
2446 Report.Error (1669, (Location) $2, "__arglist is not valid in this context");
2447 } else if (pars.Empty){
2448 Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
2450 if (RootContext.Documentation != null) {
2451 tmpComment = Lexer.consume_doc_comment ();
2452 Lexer.doc_state = XmlCommentState.Allowed;
2455 $$ = new IndexerDeclaration ((Expression) $1, null, pars, (Location) $2);
2457 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2459 Parameters pars = (Parameters) $6;
2461 if (pars.HasArglist) {
2462 // "__arglist is not valid in this context"
2463 Report.Error (1669, (Location) $4, "__arglist is not valid in this context");
2464 } else if (pars.Empty){
2465 Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
2468 MemberName name = (MemberName) $2;
2469 $$ = new IndexerDeclaration ((Expression) $1, name, pars, (Location) $4);
2471 if (RootContext.Documentation != null) {
2472 tmpComment = Lexer.consume_doc_comment ();
2473 Lexer.doc_state = XmlCommentState.Allowed;
2483 if (RootContext.Documentation != null)
2484 enumTypeComment = Lexer.consume_doc_comment ();
2489 LocatedToken lt = (LocatedToken) $4;
2490 Location enum_location = lt.Location;
2492 MemberName name = MakeName (new MemberName (lt.Value, enum_location));
2493 Enum e = new Enum (current_namespace, current_class, (Expression) $5, (int) $2,
2494 name, (Attributes) $1);
2496 if (RootContext.Documentation != null)
2497 e.DocComment = enumTypeComment;
2500 EnumMember em = null;
2501 foreach (VariableDeclaration ev in (ArrayList) $7) {
2502 em = new EnumMember (
2503 e, em, ev.identifier, (Expression) ev.expression_or_array_initializer,
2504 ev.OptAttributes, ev.Location);
2506 // if (RootContext.Documentation != null)
2507 em.DocComment = ev.DocComment;
2509 e.AddEnumMember (em);
2512 current_container.AddTypeContainer (e);
2519 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2520 | COLON type { $$ = $2; }
2526 if (RootContext.Documentation != null)
2527 Lexer.doc_state = XmlCommentState.Allowed;
2529 opt_enum_member_declarations
2531 // here will be evaluated after CLOSE_BLACE is consumed.
2532 if (RootContext.Documentation != null)
2533 Lexer.doc_state = XmlCommentState.Allowed;
2541 opt_enum_member_declarations
2542 : /* empty */ { $$ = new ArrayList (4); }
2543 | enum_member_declarations opt_comma { $$ = $1; }
2546 enum_member_declarations
2547 : enum_member_declaration
2549 ArrayList l = new ArrayList (4);
2554 | enum_member_declarations COMMA enum_member_declaration
2556 ArrayList l = (ArrayList) $1;
2564 enum_member_declaration
2565 : opt_attributes IDENTIFIER
2567 VariableDeclaration vd = new VariableDeclaration (
2568 (LocatedToken) $2, null, (Attributes) $1);
2570 if (RootContext.Documentation != null) {
2571 vd.DocComment = Lexer.consume_doc_comment ();
2572 Lexer.doc_state = XmlCommentState.Allowed;
2577 | opt_attributes IDENTIFIER
2579 if (RootContext.Documentation != null) {
2580 tmpComment = Lexer.consume_doc_comment ();
2581 Lexer.doc_state = XmlCommentState.NotAllowed;
2586 VariableDeclaration vd = new VariableDeclaration (
2587 (LocatedToken) $2, $5, (Attributes) $1);
2589 if (RootContext.Documentation != null)
2590 vd.DocComment = ConsumeStoredComment ();
2596 delegate_declaration
2599 DELEGATE type member_name
2600 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2603 MemberName name = MakeName ((MemberName) $5);
2604 Parameters p = (Parameters) $7;
2606 // TODO: wrong location
2607 Report.Error (1669, name.Location, "__arglist is not valid in this context");
2610 Delegate del = new Delegate (current_namespace, current_class, (Expression) $4,
2611 (int) $2, name, p, (Attributes) $1);
2613 if (RootContext.Documentation != null) {
2614 del.DocComment = Lexer.consume_doc_comment ();
2615 Lexer.doc_state = XmlCommentState.Allowed;
2618 current_container.AddDelegate (del);
2623 namespace_or_type_name
2625 | IDENTIFIER DOUBLE_COLON IDENTIFIER {
2626 LocatedToken lt1 = (LocatedToken) $1;
2627 LocatedToken lt2 = (LocatedToken) $3;
2628 $$ = new MemberName (lt1.Value, lt2.Value, lt2.Location);
2630 | namespace_or_type_name DOT IDENTIFIER {
2631 LocatedToken lt = (LocatedToken) $3;
2632 $$ = new MemberName ((MemberName) $1, lt.Value);
2638 LocatedToken lt = (LocatedToken) $1;
2639 $$ = new MemberName (lt.Value, lt.Location);
2644 * Before you think of adding a return_type, notice that we have been
2645 * using two rules in the places where it matters (one rule using type
2646 * and another identical one that uses VOID as the return type). This
2647 * gets rid of a shift/reduce couple
2650 : namespace_or_type_name
2652 MemberName name = (MemberName) $1;
2653 $$ = name.GetTypeExpression ();
2665 // Note that here only unmanaged types are allowed but we
2666 // can't perform checks during this phase - we do it during
2667 // semantic analysis.
2669 $$ = new ComposedCast ((Expression) $1, "*", Lexer.Location);
2673 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2679 | non_expression_type rank_specifier
2681 Location loc = GetLocation ($1);
2683 loc = lexer.Location;
2684 $$ = new ComposedCast ((Expression) $1, (string) $2, loc);
2686 | non_expression_type STAR
2688 Location loc = GetLocation ($1);
2690 loc = lexer.Location;
2691 $$ = new ComposedCast ((Expression) $1, "*", loc);
2693 | expression rank_specifiers
2695 $$ = new ComposedCast ((Expression) $1, (string) $2);
2699 $$ = new ComposedCast ((Expression) $1, "*");
2703 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2704 // through this different path
2706 | multiplicative_expression STAR
2708 $$ = new ComposedCast ((Expression) $1, "*");
2715 ArrayList types = new ArrayList (4);
2720 | type_list COMMA type
2722 ArrayList types = (ArrayList) $1;
2730 * replaces all the productions for isolating the various
2731 * simple types, but we need this to reuse it easily in local_variable_type
2734 : OBJECT { $$ = TypeManager.system_object_expr; }
2735 | STRING { $$ = TypeManager.system_string_expr; }
2736 | BOOL { $$ = TypeManager.system_boolean_expr; }
2737 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2738 | FLOAT { $$ = TypeManager.system_single_expr; }
2739 | DOUBLE { $$ = TypeManager.system_double_expr; }
2744 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2745 | BYTE { $$ = TypeManager.system_byte_expr; }
2746 | SHORT { $$ = TypeManager.system_int16_expr; }
2747 | USHORT { $$ = TypeManager.system_uint16_expr; }
2748 | INT { $$ = TypeManager.system_int32_expr; }
2749 | UINT { $$ = TypeManager.system_uint32_expr; }
2750 | LONG { $$ = TypeManager.system_int64_expr; }
2751 | ULONG { $$ = TypeManager.system_uint64_expr; }
2752 | CHAR { $$ = TypeManager.system_char_expr; }
2753 | VOID { $$ = TypeManager.system_void_expr; }
2757 : type rank_specifiers
2759 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2);
2764 // Expressions, section 7.5
2773 MemberName mn = (MemberName) $1;
2774 $$ = mn.GetTypeExpression ();
2776 | IDENTIFIER DOUBLE_COLON IDENTIFIER
2778 LocatedToken lt1 = (LocatedToken) $1;
2779 LocatedToken lt2 = (LocatedToken) $3;
2780 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, lt2.Location);
2782 | parenthesized_expression
2784 | invocation_expression
2788 | post_increment_expression
2789 | post_decrement_expression
2793 | checked_expression
2794 | unchecked_expression
2795 | pointer_member_access
2796 | anonymous_method_expression
2803 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
2804 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); }
2805 | NULL { $$ = new NullLiteral (lexer.Location); }
2809 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
2810 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
2811 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
2816 object v = lexer.Value;
2819 $$ = new IntLiteral ((int) v, lexer.Location);
2820 } else if (v is uint)
2821 $$ = new UIntLiteral ((UInt32) v, lexer.Location);
2823 $$ = new LongLiteral ((Int64) v, lexer.Location);
2824 else if (v is ulong)
2825 $$ = new ULongLiteral ((UInt64) v, lexer.Location);
2827 Console.WriteLine ("OOPS. Unexpected result from scanner");
2832 : TRUE { $$ = new BoolLiteral (true, lexer.Location); }
2833 | FALSE { $$ = new BoolLiteral (false, lexer.Location); }
2836 parenthesized_expression_0
2837 : OPEN_PARENS expression CLOSE_PARENS
2840 lexer.Deambiguate_CloseParens ($$);
2841 // After this, the next token returned is one of
2842 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST (CLOSE_PARENS), CLOSE_PARENS_OPEN_PARENS
2843 // or CLOSE_PARENS_MINUS.
2845 | OPEN_PARENS expression error { CheckToken (1026, yyToken, "Expecting ')'", lexer.Location); }
2848 parenthesized_expression
2849 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2853 | parenthesized_expression_0 CLOSE_PARENS
2857 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2859 // If a parenthesized expression is followed by a minus, we need to wrap
2860 // the expression inside a ParenthesizedExpression for the CS0075 check
2861 // in Binary.DoResolve().
2862 $$ = new ParenthesizedExpression ((Expression) $1);
2867 : primary_expression DOT IDENTIFIER
2869 LocatedToken lt = (LocatedToken) $3;
2870 $$ = new MemberAccess ((Expression) $1, lt.Value);
2872 | predefined_type DOT IDENTIFIER
2874 LocatedToken lt = (LocatedToken) $3;
2875 // TODO: Location is wrong as some predefined types doesn't hold a location
2876 $$ = new MemberAccess ((Expression) $1, lt.Value, lt.Location);
2884 invocation_expression
2885 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2888 Report.Error (1, (Location) $2, "Parse error");
2890 $$ = new Invocation ((Expression) $1, (ArrayList) $3);
2892 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2894 $$ = new Invocation ((Expression) $1, new ArrayList ());
2896 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2898 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
2900 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2902 ArrayList args = new ArrayList (1);
2904 $$ = new Invocation ((Expression) $1, args);
2906 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2908 ArrayList args = ((ArrayList) $4);
2910 $$ = new Invocation ((Expression) $1, args);
2915 : /* empty */ { $$ = null; }
2922 ArrayList list = new ArrayList (4);
2926 | argument_list COMMA argument
2928 ArrayList list = (ArrayList) $1;
2932 | argument_list error {
2933 CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
2941 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2943 | non_simple_argument
2950 : REF variable_reference
2952 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2954 | OUT variable_reference
2956 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2958 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2960 ArrayList list = (ArrayList) $3;
2961 Argument[] args = new Argument [list.Count];
2962 list.CopyTo (args, 0);
2964 Expression expr = new Arglist (args, (Location) $1);
2965 $$ = new Argument (expr, Argument.AType.Expression);
2967 | ARGLIST OPEN_PARENS CLOSE_PARENS
2969 $$ = new Argument (new Arglist ((Location) $1), Argument.AType.Expression);
2973 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
2978 : expression { note ("section 5.4"); $$ = $1; }
2982 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2984 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
2986 | primary_expression rank_specifiers
2988 // So the super-trick is that primary_expression
2989 // can only be either a SimpleName or a MemberAccess.
2990 // The MemberAccess case arises when you have a fully qualified type-name like :
2992 // SimpleName is when you have
2995 Expression expr = (Expression) $1;
2996 if (expr is ComposedCast){
2997 $$ = new ComposedCast (expr, (string) $2);
2998 } else if (!(expr is SimpleName || expr is MemberAccess || expr is QualifiedAliasMember)){
2999 Error_ExpectingTypeName (expr);
3000 $$ = TypeManager.system_object_expr;
3003 // So we extract the string corresponding to the SimpleName
3006 $$ = new ComposedCast (expr, (string) $2);
3008 current_array_type = (Expression)$$;
3015 ArrayList list = new ArrayList (4);
3019 | expression_list COMMA expression
3021 ArrayList list = (ArrayList) $1;
3030 $$ = new This (current_block, (Location) $1);
3035 : BASE DOT IDENTIFIER
3037 LocatedToken lt = (LocatedToken) $3;
3038 $$ = new BaseAccess (lt.Value, lt.Location);
3040 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
3042 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
3045 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
3050 post_increment_expression
3051 : primary_expression OP_INC
3053 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
3054 (Expression) $1, (Location) $2);
3058 post_decrement_expression
3059 : primary_expression OP_DEC
3061 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
3062 (Expression) $1, (Location) $2);
3067 : object_or_delegate_creation_expression
3068 | array_creation_expression
3071 object_or_delegate_creation_expression
3072 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
3074 $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
3078 array_creation_expression
3079 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
3081 opt_array_initializer
3083 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
3085 | NEW type rank_specifiers array_initializer
3087 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, (Location) $1);
3091 Report.Error (1031, (Location) $1, "Type expected");
3096 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
3113 : rank_specifier opt_rank_specifier
3115 $$ = (string) $2 + (string) $1;
3120 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3122 $$ = "[" + (string) $2 + "]";
3142 | dim_separators COMMA
3144 $$ = (string) $1 + ",";
3148 opt_array_initializer
3160 : OPEN_BRACE CLOSE_BRACE
3162 ArrayList list = new ArrayList (4);
3165 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3167 $$ = (ArrayList) $2;
3171 variable_initializer_list
3172 : variable_initializer
3174 ArrayList list = new ArrayList (4);
3178 | variable_initializer_list COMMA variable_initializer
3180 ArrayList list = (ArrayList) $1;
3189 pushed_current_array_type = current_array_type;
3191 OPEN_PARENS type CLOSE_PARENS
3193 Expression type = (Expression)$4;
3194 if (type == TypeManager.system_void_expr)
3195 $$ = new TypeOfVoid ((Location) $1);
3197 $$ = new TypeOf (type, (Location) $1);
3198 current_array_type = pushed_current_array_type;
3203 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3204 $$ = new SizeOf ((Expression) $3, (Location) $1);
3209 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3211 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3215 unchecked_expression
3216 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3218 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3222 pointer_member_access
3223 : primary_expression OP_PTR IDENTIFIER
3226 LocatedToken lt = (LocatedToken) $3;
3228 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
3229 $$ = new MemberAccess (deref, lt.Value);
3233 anonymous_method_expression
3234 : DELEGATE opt_anonymous_method_signature
3236 start_anonymous (false, (Parameters) $2, (Location) $1);
3240 $$ = end_anonymous ((ToplevelBlock) $4, (Location) $1);
3244 opt_anonymous_method_signature
3245 : /* empty */ { $$ = null; }
3246 | anonymous_method_signature
3249 anonymous_method_signature
3250 : open_parens opt_anonymous_method_parameter_list CLOSE_PARENS
3253 $$ = Parameters.EmptyReadOnlyParameters;
3255 ArrayList par_list = (ArrayList) $2;
3256 Parameter [] pars = new Parameter [par_list.Count];
3257 par_list.CopyTo (pars);
3258 $$ = new Parameters (pars);
3263 opt_anonymous_method_parameter_list
3264 : /* empty */ { $$ = null; }
3265 | anonymous_method_parameter_list { $$ = $1; }
3268 anonymous_method_parameter_list
3269 : anonymous_method_parameter
3271 ArrayList a = new ArrayList (4);
3275 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3277 ArrayList a = (ArrayList) $1;
3283 anonymous_method_parameter
3284 : opt_parameter_modifier type IDENTIFIER {
3285 LocatedToken lt = (LocatedToken) $3;
3286 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3288 | PARAMS type IDENTIFIER {
3289 Report.Error (1670, ((LocatedToken) $3).Location, "The `params' modifier is not allowed in anonymous method declaration");
3295 : primary_expression
3296 | BANG prefixed_unary_expression
3298 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3300 | TILDE prefixed_unary_expression
3302 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3308 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3310 $$ = new Cast ((Expression) $1, (Expression) $3);
3312 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3314 $$ = new Cast ((Expression) $1, (Expression) $3);
3320 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3322 // TODO: wrong location
3323 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3328 // The idea to split this out is from Rhys' grammar
3329 // to solve the problem with casts.
3331 prefixed_unary_expression
3333 | PLUS prefixed_unary_expression
3335 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3337 | MINUS prefixed_unary_expression
3339 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3341 | OP_INC prefixed_unary_expression
3343 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3344 (Expression) $2, (Location) $1);
3346 | OP_DEC prefixed_unary_expression
3348 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3349 (Expression) $2, (Location) $1);
3351 | STAR prefixed_unary_expression
3353 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, (Location) $1);
3355 | BITWISE_AND prefixed_unary_expression
3357 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3361 pre_increment_expression
3362 : OP_INC prefixed_unary_expression
3364 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3365 (Expression) $2, (Location) $1);
3369 pre_decrement_expression
3370 : OP_DEC prefixed_unary_expression
3372 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3373 (Expression) $2, (Location) $1);
3377 multiplicative_expression
3378 : prefixed_unary_expression
3379 | multiplicative_expression STAR prefixed_unary_expression
3381 $$ = new Binary (Binary.Operator.Multiply,
3382 (Expression) $1, (Expression) $3);
3384 | multiplicative_expression DIV prefixed_unary_expression
3386 $$ = new Binary (Binary.Operator.Division,
3387 (Expression) $1, (Expression) $3);
3389 | multiplicative_expression PERCENT prefixed_unary_expression
3391 $$ = new Binary (Binary.Operator.Modulus,
3392 (Expression) $1, (Expression) $3);
3397 : multiplicative_expression
3398 | additive_expression PLUS multiplicative_expression
3400 $$ = new Binary (Binary.Operator.Addition,
3401 (Expression) $1, (Expression) $3);
3403 | additive_expression MINUS multiplicative_expression
3405 $$ = new Binary (Binary.Operator.Subtraction,
3406 (Expression) $1, (Expression) $3);
3411 : additive_expression
3412 | shift_expression OP_SHIFT_LEFT additive_expression
3414 $$ = new Binary (Binary.Operator.LeftShift,
3415 (Expression) $1, (Expression) $3);
3417 | shift_expression OP_SHIFT_RIGHT additive_expression
3419 $$ = new Binary (Binary.Operator.RightShift,
3420 (Expression) $1, (Expression) $3);
3424 relational_expression
3426 | relational_expression OP_LT shift_expression
3428 $$ = new Binary (Binary.Operator.LessThan,
3429 (Expression) $1, (Expression) $3);
3431 | relational_expression OP_GT shift_expression
3433 $$ = new Binary (Binary.Operator.GreaterThan,
3434 (Expression) $1, (Expression) $3);
3436 | relational_expression OP_LE shift_expression
3438 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3439 (Expression) $1, (Expression) $3);
3441 | relational_expression OP_GE shift_expression
3443 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3444 (Expression) $1, (Expression) $3);
3446 | relational_expression IS type
3448 $$ = new Is ((Expression) $1, (Expression) $3, (Location) $2);
3450 | relational_expression AS type
3452 $$ = new As ((Expression) $1, (Expression) $3, (Location) $2);
3457 : relational_expression
3458 | equality_expression OP_EQ relational_expression
3460 $$ = new Binary (Binary.Operator.Equality,
3461 (Expression) $1, (Expression) $3);
3463 | equality_expression OP_NE relational_expression
3465 $$ = new Binary (Binary.Operator.Inequality,
3466 (Expression) $1, (Expression) $3);
3471 : equality_expression
3472 | and_expression BITWISE_AND equality_expression
3474 $$ = new Binary (Binary.Operator.BitwiseAnd,
3475 (Expression) $1, (Expression) $3);
3479 exclusive_or_expression
3481 | exclusive_or_expression CARRET and_expression
3483 $$ = new Binary (Binary.Operator.ExclusiveOr,
3484 (Expression) $1, (Expression) $3);
3488 inclusive_or_expression
3489 : exclusive_or_expression
3490 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3492 $$ = new Binary (Binary.Operator.BitwiseOr,
3493 (Expression) $1, (Expression) $3);
3497 conditional_and_expression
3498 : inclusive_or_expression
3499 | conditional_and_expression OP_AND inclusive_or_expression
3501 $$ = new Binary (Binary.Operator.LogicalAnd,
3502 (Expression) $1, (Expression) $3);
3506 conditional_or_expression
3507 : conditional_and_expression
3508 | conditional_or_expression OP_OR conditional_and_expression
3510 $$ = new Binary (Binary.Operator.LogicalOr,
3511 (Expression) $1, (Expression) $3);
3515 conditional_expression
3516 : conditional_or_expression
3517 | conditional_or_expression INTERR expression COLON expression
3519 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
3523 assignment_expression
3524 : prefixed_unary_expression ASSIGN expression
3526 $$ = new Assign ((Expression) $1, (Expression) $3);
3528 | prefixed_unary_expression OP_MULT_ASSIGN expression
3530 $$ = new CompoundAssign (
3531 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
3533 | prefixed_unary_expression OP_DIV_ASSIGN expression
3535 $$ = new CompoundAssign (
3536 Binary.Operator.Division, (Expression) $1, (Expression) $3);
3538 | prefixed_unary_expression OP_MOD_ASSIGN expression
3540 $$ = new CompoundAssign (
3541 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
3543 | prefixed_unary_expression OP_ADD_ASSIGN expression
3545 $$ = new CompoundAssign (
3546 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
3548 | prefixed_unary_expression OP_SUB_ASSIGN expression
3550 $$ = new CompoundAssign (
3551 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3553 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3555 $$ = new CompoundAssign (
3556 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
3558 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3560 $$ = new CompoundAssign (
3561 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
3563 | prefixed_unary_expression OP_AND_ASSIGN expression
3565 $$ = new CompoundAssign (
3566 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
3568 | prefixed_unary_expression OP_OR_ASSIGN expression
3570 $$ = new CompoundAssign (
3571 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
3573 | prefixed_unary_expression OP_XOR_ASSIGN expression
3575 $$ = new CompoundAssign (
3576 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
3580 implicitly_typed_lambda_parameter_list
3582 LocatedToken lt = (LocatedToken) $1;
3583 ArrayList a = new ArrayList (4);
3585 a.Add (new Parameter ((Expression)null, lt.Value, Parameter.Modifier.NONE, null, lt.Location));
3588 | implicitly_typed_lambda_parameter_list COMMA IDENTIFIER {
3589 LocatedToken lt = (LocatedToken) $3;
3590 ArrayList a = (ArrayList) $1;
3591 a.Add (new Parameter ((Expression)null, lt.Value, Parameter.Modifier.NONE, null, lt.Location));
3596 explicitly_typed_lambda_parameter_list
3597 : explicitly_typed_lambda_parameter
3599 ArrayList pars = new ArrayList (4);
3604 | explicitly_typed_lambda_parameter_list COMMA explicitly_typed_lambda_parameter
3606 ArrayList pars = (ArrayList) $1;
3613 explicitly_typed_lambda_parameter
3614 : parameter_modifier type IDENTIFIER
3616 LocatedToken lt = (LocatedToken) $3;
3618 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3622 LocatedToken lt = (LocatedToken) $2;
3624 $$ = new Parameter ((Expression) $1, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
3628 lambda_parameter_list
3629 : implicitly_typed_lambda_parameter_list { $$ = $1; }
3630 | explicitly_typed_lambda_parameter_list { $$ = $1; }
3633 opt_lambda_parameter_list
3634 : /* empty */ { $$ = null; }
3635 | lambda_parameter_list {
3636 ArrayList pars_list = (ArrayList) $1;
3638 Parameter [] pars = new Parameter [pars_list.Count];
3639 pars_list.CopyTo (pars);
3640 $$ = new Parameters (pars);
3644 lambda_expression_body
3646 start_block (lexer.Location);
3650 Block b = end_block (lexer.Location);
3651 b.AddStatement (new ContextualReturn ((Expression) $2));
3662 Parameter [] pars = new Parameter [1];
3663 LocatedToken lt = (LocatedToken) $1;
3664 pars [0] = new Parameter ((Expression)null, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
3666 Parameters parameters = new Parameters (pars);
3668 start_anonymous (true, parameters, (Location) $2);
3670 lambda_expression_body
3672 $$ = end_anonymous ((ToplevelBlock) $4, (Location) $2);
3674 | OPEN_PARENS_LAMBDA opt_lambda_parameter_list CLOSE_PARENS ARROW
3676 start_anonymous (true, (Parameters) $2, (Location) $4);
3678 lambda_expression_body
3680 $$ = end_anonymous ((ToplevelBlock) $6, (Location) $4);
3685 : conditional_expression
3686 | assignment_expression
3707 MemberName name = MakeName ((MemberName) $5);
3708 int mod_flags = (int) $2;
3710 push_current_class (new Class (
3711 current_namespace, current_class, name,
3712 mod_flags, (Attributes) $1), $3);
3717 if (current_class.Name == "System.Object") {
3718 Report.Error (537, current_class.Location,
3719 "The class System.Object cannot have a base " +
3720 "class or implement an interface.");
3722 current_container.AddBasesForPart (current_class, (ArrayList) $7);
3725 if (RootContext.Documentation != null) {
3726 current_container.DocComment = Lexer.consume_doc_comment ();
3727 Lexer.doc_state = XmlCommentState.Allowed;
3732 if (RootContext.Documentation != null)
3733 Lexer.doc_state = XmlCommentState.Allowed;
3737 $$ = pop_current_class ();
3745 { $$ = $1; } // location
3749 : /* empty */ { $$ = (int) 0; }
3755 | modifiers modifier
3760 if ((m1 & m2) != 0) {
3761 Location l = lexer.Location;
3762 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
3764 $$ = (int) (m1 | m2);
3769 : NEW { $$ = Modifiers.NEW; }
3770 | PUBLIC { $$ = Modifiers.PUBLIC; }
3771 | PROTECTED { $$ = Modifiers.PROTECTED; }
3772 | INTERNAL { $$ = Modifiers.INTERNAL; }
3773 | PRIVATE { $$ = Modifiers.PRIVATE; }
3774 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3775 | SEALED { $$ = Modifiers.SEALED; }
3776 | STATIC { $$ = Modifiers.STATIC; }
3777 | READONLY { $$ = Modifiers.READONLY; }
3778 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3779 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3780 | EXTERN { $$ = Modifiers.EXTERN; }
3781 | VOLATILE { $$ = Modifiers.VOLATILE; }
3782 | UNSAFE { $$ = Modifiers.UNSAFE; }
3786 : /* empty */ { $$ = null; }
3787 | class_base { $$ = $1; }
3791 : COLON type_list { $$ = $2; }
3799 // A block is "contained" on the following places:
3801 // property_declaration as part of the accessor body (get/set)
3802 // operator_declaration
3803 // constructor_declaration
3804 // destructor_declaration
3805 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3810 start_block ((Location) $1);
3812 opt_statement_list CLOSE_BRACE
3814 $$ = end_block ((Location) $4);
3820 opt_statement_list CLOSE_BRACE
3822 $$ = end_block ((Location) $3);
3833 | statement_list statement
3837 : declaration_statement
3839 if ($1 != null && (Block) $1 != current_block){
3840 current_block.AddStatement ((Statement) $1);
3841 current_block = (Block) $1;
3844 | valid_declaration_statement
3846 current_block.AddStatement ((Statement) $1);
3851 valid_declaration_statement
3854 | expression_statement
3855 | selection_statement
3856 | iteration_statement
3860 | unchecked_statement
3868 : valid_declaration_statement
3869 | declaration_statement
3871 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3876 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3884 $$ = EmptyStatement.Value;
3891 LocatedToken lt = (LocatedToken) $1;
3892 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
3894 if (current_block.AddLabel (labeled))
3895 current_block.AddStatement (labeled);
3900 declaration_statement
3901 : local_variable_declaration SEMICOLON
3903 current_array_type = null;
3905 DictionaryEntry de = (DictionaryEntry) $1;
3906 Expression e = (Expression) de.Key;
3908 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
3912 | local_constant_declaration SEMICOLON
3914 current_array_type = null;
3916 DictionaryEntry de = (DictionaryEntry) $1;
3918 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3924 * The following is from Rhys' grammar:
3925 * > Types in local variable declarations must be recognized as
3926 * > expressions to prevent reduce/reduce errors in the grammar.
3927 * > The expressions are converted into types during semantic analysis.
3930 : primary_expression opt_rank_specifier
3932 // FIXME: Do something smart here regarding the composition of the type.
3934 // Ok, the above "primary_expression" is there to get rid of
3935 // both reduce/reduce and shift/reduces in the grammar, it should
3936 // really just be "type_name". If you use type_name, a reduce/reduce
3937 // creeps up. If you use namespace_or_type_name (which is all we need
3938 // really) two shift/reduces appear.
3941 // So the super-trick is that primary_expression
3942 // can only be either a SimpleName or a MemberAccess.
3943 // The MemberAccess case arises when you have a fully qualified type-name like :
3945 // SimpleName is when you have
3948 Expression expr = (Expression) $1;
3949 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3950 Error_ExpectingTypeName (expr);
3954 // So we extract the string corresponding to the SimpleName
3958 if ((string) $2 == "")
3961 $$ = new ComposedCast ((Expression) $1, (string) $2);
3964 | builtin_types opt_rank_specifier
3966 if ((string) $2 == "")
3969 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3973 local_variable_pointer_type
3974 : primary_expression STAR
3976 Expression expr = (Expression) $1;
3978 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3979 Error_ExpectingTypeName (expr);
3983 $$ = new ComposedCast ((Expression) $1, "*");
3985 | builtin_types STAR
3987 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3991 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
3993 | local_variable_pointer_type STAR
3995 $$ = new ComposedCast ((Expression) $1, "*");
3999 local_variable_declaration
4000 : local_variable_type variable_declarators
4003 $$ = new DictionaryEntry ($1, $2);
4007 | local_variable_pointer_type opt_rank_specifier variable_declarators
4012 if ((string) $2 == "")
4013 t = (Expression) $1;
4015 t = new ComposedCast ((Expression) $1, (string) $2);
4016 $$ = new DictionaryEntry (t, $3);
4022 local_constant_declaration
4023 : CONST local_variable_type constant_declarators
4026 $$ = new DictionaryEntry ($2, $3);
4032 expression_statement
4033 : statement_expression SEMICOLON { $$ = $1; }
4037 // We have to do the wrapping here and not in the case above,
4038 // because statement_expression is used for example in for_statement
4040 statement_expression
4043 Expression expr = (Expression) $1;
4044 ExpressionStatement s = expr as ExpressionStatement;
4046 expr.Error_InvalidExpressionStatement ();
4049 $$ = new StatementExpression (s);
4053 Report.Error (1002, GetLocation ($1), "Expecting `;'");
4058 object_creation_expression
4059 : object_or_delegate_creation_expression
4060 { note ("complain if this is a delegate maybe?"); }
4069 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
4072 Location l = (Location) $1;
4074 $$ = new If ((Expression) $3, (Statement) $5, l);
4076 // FIXME: location for warning should be loc property of $5.
4077 if ($5 == EmptyStatement.Value)
4078 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4081 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
4082 embedded_statement ELSE embedded_statement
4084 Location l = (Location) $1;
4086 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
4088 // FIXME: location for warning should be loc property of $5 and $7.
4089 if ($5 == EmptyStatement.Value)
4090 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4091 if ($7 == EmptyStatement.Value)
4092 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4097 : SWITCH OPEN_PARENS
4099 if (switch_stack == null)
4100 switch_stack = new Stack (2);
4101 switch_stack.Push (current_block);
4103 expression CLOSE_PARENS
4106 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
4107 current_block = (Block) switch_stack.Pop ();
4123 Report.Warning (1522, 1, lexer.Location, "Empty switch block");
4124 $$ = new ArrayList ();
4132 ArrayList sections = new ArrayList (4);
4137 | switch_sections switch_section
4139 ArrayList sections = (ArrayList) $1;
4149 current_block = current_block.CreateSwitchBlock (lexer.Location);
4153 $$ = new SwitchSection ((ArrayList) $1, current_block.Explicit);
4160 ArrayList labels = new ArrayList (4);
4165 | switch_labels switch_label
4167 ArrayList labels = (ArrayList) ($1);
4175 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
4176 | DEFAULT COLON { $$ = new SwitchLabel (null, (Location) $1); }
4179 1523, GetLocation ($1),
4180 "The keyword case or default must precede code in switch block");
4192 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
4194 Location l = (Location) $1;
4195 $$ = new While ((Expression) $3, (Statement) $5, l);
4200 : DO embedded_statement
4201 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
4203 Location l = (Location) $1;
4205 $$ = new Do ((Statement) $2, (Expression) $5, l);
4211 opt_for_initializer SEMICOLON
4213 Location l = lexer.Location;
4215 Block assign_block = current_block;
4217 if ($3 is DictionaryEntry){
4218 DictionaryEntry de = (DictionaryEntry) $3;
4220 Expression type = (Expression) de.Key;
4221 ArrayList var_declarators = (ArrayList) de.Value;
4223 foreach (VariableDeclaration decl in var_declarators){
4227 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4231 Expression expr = decl.expression_or_array_initializer;
4233 LocalVariableReference var;
4234 var = new LocalVariableReference (assign_block, decl.identifier, l);
4237 Assign a = new Assign (var, expr, decl.Location);
4239 assign_block.AddStatement (new StatementExpression (a));
4243 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4244 // This can be referred to as $5 below.
4250 opt_for_condition SEMICOLON
4251 opt_for_iterator CLOSE_PARENS
4254 Location l = (Location) $1;
4256 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4258 current_block.AddStatement (f);
4260 $$ = end_block (lexer.Location);
4265 : /* empty */ { $$ = EmptyStatement.Value; }
4270 : local_variable_declaration
4271 | statement_expression_list
4275 : /* empty */ { $$ = null; }
4276 | boolean_expression
4280 : /* empty */ { $$ = EmptyStatement.Value; }
4285 : statement_expression_list
4288 statement_expression_list
4289 : statement_expression
4291 // CHANGE: was `null'
4292 Statement s = (Statement) $1;
4293 Block b = new Block (current_block, s.loc, lexer.Location);
4298 | statement_expression_list COMMA statement_expression
4300 Block b = (Block) $1;
4302 b.AddStatement ((Statement) $3);
4308 : FOREACH open_parens type IN expression CLOSE_PARENS
4310 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4313 | FOREACH open_parens type IDENTIFIER IN
4314 expression CLOSE_PARENS
4316 start_block (lexer.Location);
4317 Block foreach_block = current_block;
4319 LocatedToken lt = (LocatedToken) $4;
4320 Location l = lt.Location;
4323 vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4325 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4327 // Get a writable reference to this read-only variable.
4329 // Note that the $$ here refers to the value of _this_ code block,
4330 // not the value of the LHS non-terminal. This can be referred to as $8 below.
4331 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
4338 LocalVariableReference v = (LocalVariableReference) $8;
4339 Location l = (Location) $1;
4342 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
4343 current_block.AddStatement (f);
4346 $$ = end_block (lexer.Location);
4352 | continue_statement
4362 $$ = new Break ((Location) $1);
4367 : CONTINUE SEMICOLON
4369 $$ = new Continue ((Location) $1);
4374 : GOTO IDENTIFIER SEMICOLON
4376 LocatedToken lt = (LocatedToken) $2;
4377 $$ = new Goto (lt.Value, lt.Location);
4379 | GOTO CASE constant_expression SEMICOLON
4381 $$ = new GotoCase ((Expression) $3, (Location) $1);
4383 | GOTO DEFAULT SEMICOLON
4385 $$ = new GotoDefault ((Location) $1);
4390 : RETURN opt_expression SEMICOLON
4392 $$ = new Return ((Expression) $2, (Location) $1);
4397 : THROW opt_expression SEMICOLON
4399 $$ = new Throw ((Expression) $2, (Location) $1);
4404 : IDENTIFIER RETURN expression SEMICOLON
4406 LocatedToken lt = (LocatedToken) $1;
4407 string s = lt.Value;
4409 Report.Error (1003, lt.Location, "; expected");
4412 if (RootContext.Version == LanguageVersion.ISO_1){
4413 Report.FeatureIsNotISO1 (lt.Location, "yield statement");
4416 if (anonymous_host == null){
4417 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4420 anonymous_host.SetYields ();
4421 $$ = new Yield ((Expression) $3, lt.Location);
4424 | IDENTIFIER RETURN SEMICOLON
4426 Report.Error (1627, (Location) $2, "Expression expected after yield return");
4429 | IDENTIFIER BREAK SEMICOLON
4431 LocatedToken lt = (LocatedToken) $1;
4432 string s = lt.Value;
4434 Report.Error (1003, lt.Location, "; expected");
4437 if (RootContext.Version == LanguageVersion.ISO_1){
4438 Report.FeatureIsNotISO1 (lt.Location, "yield statement");
4441 if (anonymous_host == null){
4442 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4445 anonymous_host.SetYields ();
4446 $$ = new YieldBreak (lt.Location);
4457 : TRY block catch_clauses
4461 ArrayList c = (ArrayList)$3;
4462 for (int i = 0; i < c.Count; ++i) {
4463 Catch cc = (Catch) c [i];
4465 if (i != c.Count - 1)
4466 Report.Error (1017, cc.loc, "Try statement already has an empty catch block");
4473 // Now s contains the list of specific catch clauses
4474 // and g contains the general one.
4476 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4478 | TRY block opt_catch_clauses FINALLY block
4481 ArrayList s = new ArrayList (4);
4482 ArrayList catch_list = (ArrayList) $3;
4484 if (catch_list != null){
4485 foreach (Catch cc in catch_list) {
4493 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4497 Report.Error (1524, (Location) $1, "Expected catch or finally");
4503 : /* empty */ { $$ = null; }
4510 ArrayList l = new ArrayList (4);
4515 | catch_clauses catch_clause
4517 ArrayList l = (ArrayList) $1;
4525 : /* empty */ { $$ = null; }
4530 : CATCH opt_catch_args
4532 Expression type = null;
4535 DictionaryEntry cc = (DictionaryEntry) $2;
4536 type = (Expression) cc.Key;
4537 LocatedToken lt = (LocatedToken) cc.Value;
4540 ArrayList one = new ArrayList (4);
4542 one.Add (new VariableDeclaration (lt, null));
4544 start_block (lexer.Location);
4545 current_block = declare_local_variables (type, one, lt.Location);
4549 Expression type = null;
4551 Block var_block = null;
4554 DictionaryEntry cc = (DictionaryEntry) $2;
4555 type = (Expression) cc.Key;
4556 LocatedToken lt = (LocatedToken) cc.Value;
4560 var_block = end_block (lexer.Location);
4564 $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
4569 : /* empty */ { $$ = null; }
4574 : open_parens type opt_identifier CLOSE_PARENS
4576 $$ = new DictionaryEntry ($2, $3);
4584 $$ = new Checked ((Block) $2);
4591 $$ = new Unchecked ((Block) $2);
4598 RootContext.CheckUnsafeOption ((Location) $1);
4600 $$ = new Unsafe ((Block) $3);
4606 type fixed_pointer_declarators
4609 ArrayList list = (ArrayList) $4;
4610 Expression type = (Expression) $3;
4611 Location l = (Location) $1;
4612 int top = list.Count;
4614 start_block (lexer.Location);
4616 for (int i = 0; i < top; i++){
4617 Pair p = (Pair) list [i];
4620 v = current_block.AddVariable (type, (string) p.First, l);
4624 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4632 Location l = (Location) $1;
4634 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4636 current_block.AddStatement (f);
4638 $$ = end_block (lexer.Location);
4642 fixed_pointer_declarators
4643 : fixed_pointer_declarator {
4644 ArrayList declarators = new ArrayList (4);
4646 declarators.Add ($1);
4649 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4651 ArrayList declarators = (ArrayList) $1;
4653 declarators.Add ($3);
4658 fixed_pointer_declarator
4659 : IDENTIFIER ASSIGN expression
4661 LocatedToken lt = (LocatedToken) $1;
4662 // FIXME: keep location
4663 $$ = new Pair (lt.Value, $3);
4667 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
4673 : LOCK OPEN_PARENS expression CLOSE_PARENS
4679 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
4684 : USING open_parens resource_acquisition CLOSE_PARENS
4686 start_block (lexer.Location);
4687 Block assign_block = current_block;
4689 if ($3 is DictionaryEntry){
4690 DictionaryEntry de = (DictionaryEntry) $3;
4691 Location l = (Location) $1;
4693 Expression type = (Expression) de.Key;
4694 ArrayList var_declarators = (ArrayList) de.Value;
4696 ArrayList vars = new ArrayList (4);
4698 foreach (VariableDeclaration decl in var_declarators){
4700 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4703 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
4705 Expression expr = decl.expression_or_array_initializer;
4707 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4710 LocalVariableReference var;
4712 // Get a writable reference to this read-only variable.
4713 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4715 // This is so that it is not a warning on using variables
4718 vars.Add (new DictionaryEntry (var, expr));
4720 // Assign a = new Assign (var, expr, decl.Location);
4721 // assign_block.AddStatement (new StatementExpression (a));
4724 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4725 // It can be referred to as $5 below.
4726 $$ = new DictionaryEntry (type, vars);
4733 Using u = new Using ($5, (Statement) $6, (Location) $1);
4734 current_block.AddStatement (u);
4735 $$ = end_block (lexer.Location);
4739 resource_acquisition
4740 : local_variable_declaration
4747 // A class used to pass around variable declarations and constants
4749 public class VariableDeclaration {
4750 public string identifier;
4751 public Expression expression_or_array_initializer;
4752 public Location Location;
4753 public Attributes OptAttributes;
4754 public string DocComment;
4756 public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
4758 this.identifier = lt.Value;
4759 if (eoai is ArrayList) {
4760 if (CSharpParser.current_array_type == null)
4761 Report.Error (622, lt.Location,
4762 "Can only use array initializer expressions to assign to array types. Try using a new expression instead.");
4763 this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
4765 this.expression_or_array_initializer = (Expression)eoai;
4767 this.Location = lt.Location;
4768 this.OptAttributes = opt_attrs;
4771 public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
4777 // A class used to hold info about an indexer declarator
4779 public class IndexerDeclaration {
4780 public Expression type;
4781 public MemberName interface_type;
4782 public Parameters param_list;
4783 public Location location;
4785 public IndexerDeclaration (Expression type, MemberName interface_type,
4786 Parameters param_list, Location loc)
4789 this.interface_type = interface_type;
4790 this.param_list = param_list;
4791 this.location = loc;
4796 // We use this when we do not have an object in advance that is an IAnonymousHost
4798 public class SimpleAnonymousHost : IAnonymousHost {
4799 public static readonly SimpleAnonymousHost Simple = new SimpleAnonymousHost ();
4802 ArrayList anonymous_methods;
4804 public static SimpleAnonymousHost GetSimple () {
4805 Simple.yields = false;
4806 Simple.anonymous_methods = null;
4810 public void SetYields ()
4815 public void AddAnonymousMethod (AnonymousMethodExpression anonymous)
4817 if (anonymous_methods == null)
4818 anonymous_methods = new ArrayList ();
4819 anonymous_methods.Add (anonymous);
4822 public void Propagate (IAnonymousHost real_host)
4825 real_host.SetYields ();
4826 if (anonymous_methods != null) {
4827 foreach (AnonymousMethodExpression ame in anonymous_methods)
4828 real_host.AddAnonymousMethod (ame);
4834 // A class used to hold info about an operator declarator
4836 public class OperatorDeclaration {
4837 public Operator.OpType optype;
4838 public Expression ret_type, arg1type, arg2type;
4839 public string arg1name, arg2name;
4840 public Location location;
4842 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4843 Expression arg1type, string arg1name,
4844 Expression arg2type, string arg2name, Location location)
4847 this.ret_type = ret_type;
4848 this.arg1type = arg1type;
4849 this.arg1name = arg1name;
4850 this.arg2type = arg2type;
4851 this.arg2name = arg2name;
4852 this.location = location;
4857 void Error_ExpectingTypeName (Expression expr)
4859 if (expr is Invocation){
4860 Report.Error (1002, expr.Location, "Expecting `;'");
4862 expr.Error_InvalidExpressionStatement ();
4866 public static void Error_ParameterModifierNotValid (Location loc)
4868 Report.Error (631, loc, "The modifiers `ref' and `out' are not valid in this context");
4871 static void Error_DuplicateParameterModifier (Location loc, Parameter.Modifier mod)
4873 Report.Error (1107, loc, "Duplicate parameter modifier `{0}'",
4874 Parameter.GetModifierSignature (mod));
4877 void push_current_class (TypeContainer tc, object partial_token)
4879 if (partial_token != null)
4880 current_container = current_container.AddPartial (tc);
4882 current_container = current_container.AddTypeContainer (tc);
4886 DeclSpace pop_current_class ()
4888 DeclSpace retval = current_class;
4890 current_class = current_class.Parent;
4891 current_container = current_class.PartialContainer;
4897 // Given the @class_name name, it creates a fully qualified name
4898 // based on the containing declaration space
4901 MakeName (MemberName class_name)
4903 Namespace ns = current_namespace.NS;
4905 if (current_container.Name.Length == 0){
4906 if (ns.Name.Length != 0)
4907 return new MemberName (ns.MemberName, class_name);
4911 return new MemberName (current_container.MemberName, class_name);
4915 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4917 Block implicit_block;
4918 ArrayList inits = null;
4921 // We use the `Used' property to check whether statements
4922 // have been added to the current block. If so, we need
4923 // to create another block to contain the new declaration
4924 // otherwise, as an optimization, we use the same block to
4925 // add the declaration.
4927 // FIXME: A further optimization is to check if the statements
4928 // that were added were added as part of the initialization
4929 // below. In which case, no other statements have been executed
4930 // and we might be able to reduce the number of blocks for
4931 // situations like this:
4933 // int j = 1; int k = j + 1;
4935 if (current_block.Used)
4936 implicit_block = new Block (current_block, loc, Location.Null);
4938 implicit_block = current_block;
4940 foreach (VariableDeclaration decl in variable_declarators){
4942 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
4943 if (decl.expression_or_array_initializer != null){
4945 inits = new ArrayList (4);
4952 return implicit_block;
4954 foreach (VariableDeclaration decl in inits){
4956 Expression expr = decl.expression_or_array_initializer;
4958 LocalVariableReference var;
4959 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4961 assign = new Assign (var, expr, decl.Location);
4963 implicit_block.AddStatement (new StatementExpression (assign));
4966 return implicit_block;
4969 Block declare_local_constants (Expression type, ArrayList declarators)
4971 Block implicit_block;
4973 if (current_block.Used)
4974 implicit_block = new Block (current_block);
4976 implicit_block = current_block;
4978 foreach (VariableDeclaration decl in declarators){
4979 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
4982 return implicit_block;
4985 string CheckAttributeTarget (string a, Location l)
4988 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4992 Report.Warning (658, 1, l,
4993 "`{0}' is invalid attribute target. All attributes in this attribute section will be ignored", a);
4994 return string.Empty;
4997 void CheckUnaryOperator (Operator.OpType op, Location l)
5001 case Operator.OpType.LogicalNot:
5002 case Operator.OpType.OnesComplement:
5003 case Operator.OpType.Increment:
5004 case Operator.OpType.Decrement:
5005 case Operator.OpType.True:
5006 case Operator.OpType.False:
5007 case Operator.OpType.Addition:
5008 case Operator.OpType.Subtraction:
5013 Report.Error (1019, l, "Overloadable unary operator expected");
5019 void CheckBinaryOperator (Operator.OpType op, Location l)
5023 case Operator.OpType.Addition:
5024 case Operator.OpType.Subtraction:
5025 case Operator.OpType.Multiply:
5026 case Operator.OpType.Division:
5027 case Operator.OpType.Modulus:
5028 case Operator.OpType.BitwiseAnd:
5029 case Operator.OpType.BitwiseOr:
5030 case Operator.OpType.ExclusiveOr:
5031 case Operator.OpType.LeftShift:
5032 case Operator.OpType.RightShift:
5033 case Operator.OpType.Equality:
5034 case Operator.OpType.Inequality:
5035 case Operator.OpType.GreaterThan:
5036 case Operator.OpType.LessThan:
5037 case Operator.OpType.GreaterThanOrEqual:
5038 case Operator.OpType.LessThanOrEqual:
5042 Report.Error (1020, l, "Overloadable binary operator expected");
5048 void syntax_error (Location l, string msg)
5050 Report.Error (1003, l, "Syntax error, " + msg);
5053 void note (string s)
5055 // Used to put annotations
5060 public Tokenizer Lexer {
5066 static CSharpParser ()
5068 oob_stack = new Stack ();
5071 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
5073 this.name = file.Name;
5075 current_namespace = new NamespaceEntry (null, file, null);
5076 current_class = current_namespace.SlaveDeclSpace;
5077 current_container = current_class.PartialContainer; // == RootContest.ToplevelTypes
5079 lexer = new Tokenizer (reader, file, defines);
5082 public void parse ()
5084 int errors = Report.Errors;
5086 if (yacc_verbose_flag > 1)
5087 yyparse (lexer, new yydebug.yyDebugSimple ());
5090 Tokenizer tokenizer = lexer as Tokenizer;
5091 tokenizer.cleanup ();
5092 } catch (Exception e){
5094 // Removed for production use, use parser verbose to get the output.
5096 // Console.WriteLine (e);
5097 if (Report.Errors == errors)
5098 Report.Error (-25, lexer.Location, "Parsing error");
5099 if (yacc_verbose_flag > 0)
5100 Console.WriteLine (e);
5103 if (RootContext.ToplevelTypes.NamespaceEntry != null)
5104 throw new InternalErrorException ("who set it?");
5107 static void CheckToken (int error, int yyToken, string msg, Location loc)
5109 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
5110 Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ());
5112 Report.Error (error, loc, msg);
5115 void CheckIdentifierToken (int yyToken, Location loc)
5117 CheckToken (1041, yyToken, "Identifier expected", loc);
5120 string ConsumeStoredComment ()
5122 string s = tmpComment;
5124 Lexer.doc_state = XmlCommentState.Allowed;
5128 Location GetLocation (object obj)
5130 if (obj is MemberCore)
5131 return ((MemberCore) obj).Location;
5132 if (obj is MemberName)
5133 return ((MemberName) obj).Location;
5134 if (obj is LocatedToken)
5135 return ((LocatedToken) obj).Location;
5136 if (obj is Location)
5137 return (Location) obj;
5138 return lexer.Location;
5141 static GenericMethod current_generic_method = null;
5143 void start_block (Location loc)
5145 if (current_block == null || parsing_anonymous_method) {
5146 current_block = new ToplevelBlock (current_block, current_local_parameters, current_generic_method, loc);
5147 parsing_anonymous_method = false;
5149 current_block = new ExplicitBlock (current_block, loc, Location.Null);
5154 end_block (Location loc)
5156 Block retval = current_block.Explicit;
5157 retval.SetEndLocation (loc);
5158 current_block = retval.Parent;
5163 start_anonymous (bool lambda, Parameters parameters, Location loc)
5165 oob_stack.Push (current_anonymous_method);
5166 oob_stack.Push (current_local_parameters);
5168 current_local_parameters = parameters;
5170 ToplevelBlock top_current_block = current_block == null ? null : current_block.Toplevel;
5172 current_anonymous_method = lambda
5173 ? new LambdaExpression (
5174 current_anonymous_method, current_generic_method, current_container,
5175 parameters, top_current_block, loc)
5176 : new AnonymousMethodExpression (
5177 current_anonymous_method, current_generic_method, current_container,
5178 parameters, top_current_block, loc);
5180 // Force the next block to be created as a ToplevelBlock
5181 parsing_anonymous_method = true;
5185 * Completes the anonymous method processing, if lambda_expr is null, this
5186 * means that we have a Statement instead of an Expression embedded
5188 AnonymousMethodExpression
5189 end_anonymous (ToplevelBlock anon_block, Location loc)
5191 AnonymousMethodExpression retval;
5193 if (RootContext.Version == LanguageVersion.ISO_1){
5194 Report.FeatureIsNotISO1 (loc, "anonymous methods");
5197 current_anonymous_method.Block = anon_block;
5198 if ((anonymous_host != null) && (current_anonymous_method.Parent == null))
5199 anonymous_host.AddAnonymousMethod (current_anonymous_method);
5201 retval = current_anonymous_method;
5204 current_local_parameters = (Parameters) oob_stack.Pop ();
5205 current_anonymous_method = (AnonymousMethodExpression) oob_stack.Pop ();