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 Attributes attrs = (Attributes)$1;
526 if (global_attrs_enabled) {
527 CodeGen.Assembly.AddAttributes (attrs.Attrs);
529 foreach (Attribute a in attrs.Attrs) {
530 Report.Error (1730, a.Location, "Assembly and module attributes must precede all other elements except using clauses and extern alias declarations");
541 global_attrs_enabled = false;
546 global_attrs_enabled = false;
555 if (current_attr_target != String.Empty) {
556 ArrayList sect = (ArrayList) $1;
558 if (global_attrs_enabled) {
559 if (current_attr_target == "module") {
560 CodeGen.Module.AddAttributes (sect);
562 } else if (current_attr_target != null && current_attr_target.Length > 0) {
563 CodeGen.Assembly.AddAttributes (sect);
566 $$ = new Attributes (sect);
569 if (RootContext.Documentation != null) {
570 Lexer.check_incorrect_doc_comment ();
572 XmlCommentState.Allowed;
576 $$ = new Attributes (sect);
581 current_attr_target = null;
583 | attribute_sections attribute_section
585 if (current_attr_target != String.Empty) {
586 Attributes attrs = $1 as Attributes;
587 ArrayList sect = (ArrayList) $2;
589 if (global_attrs_enabled) {
590 if (current_attr_target == "module") {
591 CodeGen.Module.AddAttributes (sect);
593 } else if (current_attr_target == "assembly") {
594 CodeGen.Assembly.AddAttributes (sect);
598 attrs = new Attributes (sect);
600 attrs.AddAttributes (sect);
604 attrs = new Attributes (sect);
606 attrs.AddAttributes (sect);
612 current_attr_target = null;
617 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
621 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
627 attribute_target_specifier
628 : attribute_target COLON
630 current_attr_target = (string)$1;
638 LocatedToken lt = (LocatedToken) $1;
639 $$ = CheckAttributeTarget (lt.Value, lt.Location);
641 | EVENT { $$ = "event"; }
642 | RETURN { $$ = "return"; }
645 string name = yyNames [yyToken].ToLower ();
646 $$ = CheckAttributeTarget (name, GetLocation ($1));
653 ArrayList attrs = new ArrayList (4);
659 | attribute_list COMMA attribute
661 ArrayList attrs = (ArrayList) $1;
669 : attribute_name opt_attribute_arguments
671 MemberName mname = (MemberName) $1;
672 object[] arguments = (object[]) $2;
673 MemberName left = mname.Left;
674 string identifier = mname.Name;
676 Expression left_expr = left == null ? null : left.GetTypeExpression ();
678 if (current_attr_target == String.Empty)
680 else if (global_attrs_enabled && (current_attr_target == "assembly" || current_attr_target == "module"))
681 // FIXME: supply "nameEscaped" parameter here.
682 $$ = new GlobalAttribute (current_namespace, current_attr_target,
683 left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
685 $$ = new Attribute (current_attr_target, left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
690 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
693 opt_attribute_arguments
694 : /* empty */ { $$ = null; }
695 | OPEN_PARENS attribute_arguments CLOSE_PARENS
703 : opt_positional_argument_list
708 $$ = new object [] { $1, null };
711 | positional_argument_list COMMA named_argument_list
713 $$ = new object[] { $1, $3 };
715 | named_argument_list
717 $$ = new object [] { null, $1 };
722 opt_positional_argument_list
723 : /* empty */ { $$ = null; }
724 | positional_argument_list
727 positional_argument_list
730 ArrayList args = new ArrayList (4);
731 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
735 | positional_argument_list COMMA expression
737 ArrayList args = (ArrayList) $1;
738 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
747 ArrayList args = new ArrayList (4);
752 | named_argument_list COMMA named_argument
754 ArrayList args = (ArrayList) $1;
759 | named_argument_list COMMA expression
761 Report.Error (1016, ((Expression) $3).Location, "Named attribute argument expected");
767 : IDENTIFIER ASSIGN expression
769 // FIXME: keep location
770 $$ = new DictionaryEntry (
771 ((LocatedToken) $1).Value,
772 new Argument ((Expression) $3, Argument.AType.Expression));
778 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
781 opt_class_member_declarations
783 | class_member_declarations
786 class_member_declarations
787 : class_member_declaration
788 | class_member_declarations
789 class_member_declaration
792 class_member_declaration
793 : constant_declaration // done
794 | field_declaration // done
795 | method_declaration // done
796 | property_declaration // done
797 | event_declaration // done
798 | indexer_declaration // done
799 | operator_declaration // done
800 | constructor_declaration // done
801 | destructor_declaration // done
811 MemberName name = MakeName ((MemberName) $5);
812 push_current_class (new Struct (
813 current_namespace, current_class, name, (int) $2,
814 (Attributes) $1), $3);
820 current_container.AddBasesForPart (current_class, (ArrayList) $7);
822 if (RootContext.Documentation != null)
823 current_container.DocComment = Lexer.consume_doc_comment ();
827 if (RootContext.Documentation != null)
828 Lexer.doc_state = XmlCommentState.Allowed;
832 $$ = pop_current_class ();
834 | opt_attributes opt_modifiers opt_partial STRUCT error {
835 CheckIdentifierToken (yyToken, GetLocation ($5));
842 if (RootContext.Documentation != null)
843 Lexer.doc_state = XmlCommentState.Allowed;
845 opt_struct_member_declarations CLOSE_BRACE
848 opt_struct_member_declarations
850 | struct_member_declarations
853 struct_member_declarations
854 : struct_member_declaration
855 | struct_member_declarations struct_member_declaration
858 struct_member_declaration
859 : constant_declaration
862 | property_declaration
864 | indexer_declaration
865 | operator_declaration
866 | constructor_declaration
870 * This is only included so we can flag error 575:
871 * destructors only allowed on class types
873 | destructor_declaration
884 int modflags = (int) $2;
885 foreach (VariableDeclaration constant in (ArrayList) $5){
886 Location l = constant.Location;
887 if ((modflags & Modifiers.STATIC) != 0) {
888 Report.Error (504, l, "The constant `{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
892 Const c = new Const (
893 current_class, (Expression) $4, (string) constant.identifier,
894 (Expression) constant.expression_or_array_initializer, modflags,
897 if (RootContext.Documentation != null) {
898 c.DocComment = Lexer.consume_doc_comment ();
899 Lexer.doc_state = XmlCommentState.Allowed;
901 current_container.AddConstant (c);
907 : constant_declarator
909 ArrayList constants = new ArrayList (4);
914 | constant_declarators COMMA constant_declarator
917 ArrayList constants = (ArrayList) $1;
924 : IDENTIFIER ASSIGN constant_expression
926 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
930 // A const field requires a value to be provided
931 Report.Error (145, ((LocatedToken) $1).Location, "A const field requires a value to be provided");
943 Expression type = (Expression) $3;
946 current_array_type = null;
948 foreach (VariableDeclaration var in (ArrayList) $4){
949 Field field = new Field (current_class, type, mod, var.identifier,
950 (Attributes) $1, var.Location);
952 field.Initializer = var.expression_or_array_initializer;
954 if (RootContext.Documentation != null) {
955 field.DocComment = Lexer.consume_doc_comment ();
956 Lexer.doc_state = XmlCommentState.Allowed;
958 current_container.AddField (field);
959 $$ = field; // FIXME: might be better if it points to the top item
966 fixed_variable_declarators
969 Expression type = (Expression) $4;
972 current_array_type = null;
974 foreach (VariableDeclaration var in (ArrayList) $5) {
975 FixedField field = new FixedField (current_class, type, mod, var.identifier,
976 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
978 if (RootContext.Documentation != null) {
979 field.DocComment = Lexer.consume_doc_comment ();
980 Lexer.doc_state = XmlCommentState.Allowed;
982 current_container.AddField (field);
983 $$ = field; // FIXME: might be better if it points to the top item
992 Report.Error (1641, GetLocation ($4), "A fixed size buffer field must have the array size specifier after the field name");
999 current_array_type = null;
1000 Report.Error (670, (Location) $3, "Fields cannot have void type");
1004 fixed_variable_declarators
1005 : fixed_variable_declarator
1007 ArrayList decl = new ArrayList (2);
1011 | fixed_variable_declarators COMMA fixed_variable_declarator
1013 ArrayList decls = (ArrayList) $1;
1019 fixed_variable_declarator
1020 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
1022 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1024 | IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1026 Report.Error (443, lexer.Location, "Value or constant expected");
1027 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1031 variable_declarators
1032 : variable_declarator
1034 ArrayList decl = new ArrayList (4);
1039 | variable_declarators COMMA variable_declarator
1041 ArrayList decls = (ArrayList) $1;
1048 : IDENTIFIER ASSIGN variable_initializer
1050 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1054 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1056 | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
1058 Report.Error (650, ((LocatedToken) $1).Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
1059 "To declare a fixed size buffer field, use the fixed keyword before the field type");
1064 variable_initializer
1073 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
1075 $$ = new StackAlloc ((Expression) $2, (Expression) $4, (Location) $1);
1079 $$ = new ArglistAccess ((Location) $1);
1083 Report.Error (1575, (Location) $1, "A stackalloc expression requires [] after type");
1090 anonymous_host = (IAnonymousHost) $1;
1091 if (RootContext.Documentation != null)
1092 Lexer.doc_state = XmlCommentState.NotAllowed;
1096 Method method = (Method) $1;
1097 method.Block = (ToplevelBlock) $3;
1098 current_container.AddMethod (method);
1100 anonymous_host = null;
1101 current_local_parameters = null;
1103 if (RootContext.Documentation != null)
1104 Lexer.doc_state = XmlCommentState.Allowed;
1117 Report.Error (1585, lexer.Location,
1118 "Member modifier `{0}' must precede the member type and name",
1119 Modifiers.Name (i));
1128 // This rule is used to handle the cases where OPEN_PARENS_LAMBDA
1129 // is returned (type followed by an identifier), these are the
1130 // declarations (methods, overloads, constructors, etc) and a handful
1131 // of expressions ("using", "fixed") or "catch".
1135 | OPEN_PARENS_LAMBDA
1141 type namespace_or_type_name
1142 open_parens opt_formal_parameter_list CLOSE_PARENS
1144 MemberName name = (MemberName) $4;
1146 Method method = new Method (current_class, null, (Expression) $3, (int) $2,
1147 false, name, (Parameters) $6, (Attributes) $1);
1149 anonymous_host = method;
1150 current_local_parameters = (Parameters) $6;
1152 if (RootContext.Documentation != null)
1153 method.DocComment = Lexer.consume_doc_comment ();
1159 VOID namespace_or_type_name
1160 open_parens opt_formal_parameter_list CLOSE_PARENS
1162 MemberName name = (MemberName) $4;
1164 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1165 (int) $2, false, name, (Parameters) $6,
1168 anonymous_host = method;
1169 current_local_parameters = (Parameters) $6;
1171 if (RootContext.Documentation != null)
1172 method.DocComment = Lexer.consume_doc_comment ();
1179 modifiers namespace_or_type_name open_parens opt_formal_parameter_list CLOSE_PARENS
1181 MemberName name = (MemberName) $5;
1182 Report.Error (1585, name.Location,
1183 "Member modifier `{0}' must precede the member type and name", Modifiers.Name ((int) $4));
1185 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1186 0, false, name, (Parameters) $7, (Attributes) $1);
1188 current_local_parameters = (Parameters) $7;
1190 if (RootContext.Documentation != null)
1191 method.DocComment = Lexer.consume_doc_comment ();
1199 | SEMICOLON { $$ = null; }
1202 opt_formal_parameter_list
1203 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1204 | formal_parameter_list
1207 formal_parameter_list
1210 ArrayList pars_list = (ArrayList) $1;
1212 Parameter [] pars = new Parameter [pars_list.Count];
1213 pars_list.CopyTo (pars);
1215 $$ = new Parameters (pars);
1217 | fixed_parameters COMMA parameter_array
1219 ArrayList pars_list = (ArrayList) $1;
1222 Parameter [] pars = new Parameter [pars_list.Count];
1223 pars_list.CopyTo (pars);
1225 $$ = new Parameters (pars);
1227 | fixed_parameters COMMA ARGLIST
1229 ArrayList pars_list = (ArrayList) $1;
1230 //pars_list.Add (new ArglistParameter (GetLocation ($3)));
1232 Parameter [] pars = new Parameter [pars_list.Count];
1233 pars_list.CopyTo (pars);
1235 $$ = new Parameters (pars, true);
1237 | parameter_array COMMA error
1240 Report.Error (231, ((Parameter) $1).Location, "A params parameter must be the last parameter in a formal parameter list");
1243 | fixed_parameters COMMA parameter_array COMMA error
1246 Report.Error (231, ((Parameter) $3).Location, "A params parameter must be the last parameter in a formal parameter list");
1249 | ARGLIST COMMA error
1251 Report.Error (257, (Location) $1, "An __arglist parameter must be the last parameter in a formal parameter list");
1254 | fixed_parameters COMMA ARGLIST COMMA error
1256 Report.Error (257, (Location) $3, "An __arglist parameter must be the last parameter in a formal parameter list");
1261 $$ = new Parameters (new Parameter[] { (Parameter) $1 } );
1265 $$ = new Parameters (new Parameter[0], true);
1272 ArrayList pars = new ArrayList (4);
1277 | fixed_parameters COMMA fixed_parameter
1279 ArrayList pars = (ArrayList) $1;
1288 opt_parameter_modifier
1292 LocatedToken lt = (LocatedToken) $4;
1293 $$ = new Parameter ((Expression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1296 opt_parameter_modifier
1298 IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1300 LocatedToken lt = (LocatedToken) $4;
1301 Report.Error (1552, lt.Location, "Array type specifier, [], must appear before parameter name");
1305 opt_parameter_modifier
1308 Report.Error (1001, GetLocation ($3), "Identifier expected");
1312 opt_parameter_modifier
1315 CheckIdentifierToken (yyToken, GetLocation ($4));
1319 opt_parameter_modifier
1325 LocatedToken lt = (LocatedToken) $4;
1326 Report.Error (241, lt.Location, "Default parameter specifiers are not permitted");
1331 opt_parameter_modifier
1332 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1333 | parameter_modifiers
1337 : parameter_modifier
1341 | parameter_modifiers parameter_modifier
1343 Parameter.Modifier p2 = (Parameter.Modifier)$2;
1344 Parameter.Modifier mod = (Parameter.Modifier)$1 | p2;
1345 if (((Parameter.Modifier)$1 & p2) == p2) {
1346 Error_DuplicateParameterModifier (lexer.Location, p2);
1348 Report.Error (1108, lexer.Location, "A parameter cannot have specified more than one modifier");
1355 : REF { $$ = Parameter.Modifier.REF; }
1356 | OUT { $$ = Parameter.Modifier.OUT; }
1357 | THIS { $$ = Parameter.Modifier.This; }
1361 : opt_attributes PARAMS type IDENTIFIER
1363 LocatedToken lt = (LocatedToken) $4;
1364 $$ = new ParamsParameter ((Expression) $3, lt.Value, (Attributes) $1, lt.Location);
1366 | opt_attributes PARAMS PARAMS type IDENTIFIER
1368 Error_DuplicateParameterModifier (lexer.Location, Parameter.Modifier.PARAMS);
1371 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1373 Parameter.Modifier mod = (Parameter.Modifier)$3;
1374 if ((mod & Parameter.Modifier.This) != 0) {
1375 Report.Error (1104, lexer.Location, "The parameter modifiers `this' and `params' cannot be used altogether");
1377 Report.Error (1611, (Location) $2, "The params parameter cannot be declared as ref or out");
1381 | opt_attributes PARAMS type error {
1382 CheckIdentifierToken (yyToken, GetLocation ($4));
1387 property_declaration
1391 namespace_or_type_name
1393 if (RootContext.Documentation != null)
1394 tmpComment = Lexer.consume_doc_comment ();
1398 implicit_value_parameter_type = (Expression) $3;
1400 lexer.PropertyParsing = true;
1402 accessor_declarations
1404 lexer.PropertyParsing = false;
1405 has_get = has_set = false;
1413 Accessors accessors = (Accessors) $8;
1414 Accessor get_block = accessors.get_or_add;
1415 Accessor set_block = accessors.set_or_remove;
1417 MemberName name = (MemberName) $4;
1419 prop = new Property (current_class, (Expression) $3, (int) $2,
1420 false, name, (Attributes) $1, get_block, set_block, accessors.declared_in_reverse);
1422 current_container.AddProperty (prop);
1423 implicit_value_parameter_type = null;
1425 if (RootContext.Documentation != null)
1426 prop.DocComment = ConsumeStoredComment ();
1431 accessor_declarations
1432 : get_accessor_declaration
1434 $$ = new Accessors ((Accessor) $1, null);
1436 | get_accessor_declaration accessor_declarations
1438 Accessors accessors = (Accessors) $2;
1439 accessors.get_or_add = (Accessor) $1;
1442 | set_accessor_declaration
1444 $$ = new Accessors (null, (Accessor) $1);
1446 | set_accessor_declaration accessor_declarations
1448 Accessors accessors = (Accessors) $2;
1449 accessors.set_or_remove = (Accessor) $1;
1450 accessors.declared_in_reverse = true;
1455 Report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1460 get_accessor_declaration
1461 : opt_attributes opt_modifiers GET
1463 // If this is not the case, then current_local_parameters has already
1464 // been set in indexer_declaration
1465 if (parsing_indexer == false)
1466 current_local_parameters = null;
1468 current_local_parameters = indexer_parameters;
1469 lexer.PropertyParsing = false;
1471 anonymous_host = SimpleAnonymousHost.GetSimple ();
1476 Report.Error (1007, (Location) $3, "Property accessor already defined");
1479 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1481 current_local_parameters = null;
1482 lexer.PropertyParsing = true;
1484 SimpleAnonymousHost.Simple.Propagate (accessor);
1485 anonymous_host = null;
1487 if (RootContext.Documentation != null)
1488 if (Lexer.doc_state == XmlCommentState.Error)
1489 Lexer.doc_state = XmlCommentState.NotAllowed;
1495 set_accessor_declaration
1496 : opt_attributes opt_modifiers SET
1499 Parameter implicit_value_parameter = new Parameter (
1500 implicit_value_parameter_type, "value",
1501 Parameter.Modifier.NONE, null, (Location) $3);
1503 if (parsing_indexer == false) {
1504 args = new Parameter [1];
1505 args [0] = implicit_value_parameter;
1506 current_local_parameters = new Parameters (args);
1508 Parameter [] fpars = indexer_parameters.FixedParameters;
1511 int count = fpars.Length;
1513 args = new Parameter [count + 1];
1514 fpars.CopyTo (args, 0);
1515 args [count] = implicit_value_parameter;
1518 current_local_parameters = new Parameters (
1522 lexer.PropertyParsing = false;
1524 anonymous_host = SimpleAnonymousHost.GetSimple ();
1529 Report.Error (1007, ((LocatedToken) $3).Location, "Property accessor already defined");
1532 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1534 current_local_parameters = null;
1535 lexer.PropertyParsing = true;
1537 SimpleAnonymousHost.Simple.Propagate (accessor);
1538 anonymous_host = null;
1540 if (RootContext.Documentation != null
1541 && Lexer.doc_state == XmlCommentState.Error)
1542 Lexer.doc_state = XmlCommentState.NotAllowed;
1550 | SEMICOLON { $$ = null; }
1553 interface_declaration
1557 INTERFACE member_name
1559 MemberName name = MakeName ((MemberName) $5);
1561 push_current_class (new Interface (
1562 current_namespace, current_class, name, (int) $2,
1563 (Attributes) $1), $3);
1567 current_container.AddBasesForPart (current_class, (ArrayList) $7);
1569 if (RootContext.Documentation != null) {
1570 current_container.DocComment = Lexer.consume_doc_comment ();
1571 Lexer.doc_state = XmlCommentState.Allowed;
1576 if (RootContext.Documentation != null)
1577 Lexer.doc_state = XmlCommentState.Allowed;
1581 $$ = pop_current_class ();
1583 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1584 CheckIdentifierToken (yyToken, GetLocation ($5));
1590 opt_interface_member_declarations
1594 opt_interface_member_declarations
1596 | interface_member_declarations
1599 interface_member_declarations
1600 : interface_member_declaration
1601 | interface_member_declarations interface_member_declaration
1604 interface_member_declaration
1605 : interface_method_declaration
1610 Method m = (Method) $1;
1612 if (m.IsExplicitImpl)
1613 Report.Error (541, m.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1614 m.GetSignatureForError ());
1616 current_container.AddMethod (m);
1618 if (RootContext.Documentation != null)
1619 Lexer.doc_state = XmlCommentState.Allowed;
1621 | interface_property_declaration
1626 Property p = (Property) $1;
1628 if (p.IsExplicitImpl)
1629 Report.Error (541, p.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1630 p.GetSignatureForError ());
1632 current_container.AddProperty (p);
1634 if (RootContext.Documentation != null)
1635 Lexer.doc_state = XmlCommentState.Allowed;
1637 | interface_event_declaration
1640 Event e = (Event) $1;
1642 if (e.IsExplicitImpl)
1643 Report.Error (541, e.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1644 e.GetSignatureForError ());
1646 current_container.AddEvent (e);
1649 if (RootContext.Documentation != null)
1650 Lexer.doc_state = XmlCommentState.Allowed;
1652 | interface_indexer_declaration
1657 Indexer i = (Indexer) $1;
1659 if (i.IsExplicitImpl)
1660 Report.Error (541, i.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1661 i.GetSignatureForError ());
1663 current_container.AddIndexer (i);
1665 if (RootContext.Documentation != null)
1666 Lexer.doc_state = XmlCommentState.Allowed;
1668 | delegate_declaration
1671 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1672 ((MemberCore)$1).GetSignatureForError ());
1678 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1679 ((MemberCore)$1).GetSignatureForError ());
1682 | struct_declaration
1685 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1686 ((MemberCore)$1).GetSignatureForError ());
1692 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1693 ((MemberCore)$1).GetSignatureForError ());
1696 | interface_declaration
1699 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1700 ((MemberCore)$1).GetSignatureForError ());
1703 | constant_declaration
1705 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1713 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, GetLocation ($1));
1718 interface_method_declaration
1719 : opt_attributes opt_new type namespace_or_type_name
1720 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1723 $$ = new Method (current_class, null, (Expression) $3, (int) $2, true,
1724 (MemberName) $4, (Parameters) $6, (Attributes) $1);
1725 if (RootContext.Documentation != null)
1726 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1728 | opt_attributes opt_new type namespace_or_type_name
1729 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1730 OPEN_BRACE opt_statement_list CLOSE_BRACE
1732 $$ = new Method (current_class, null, (Expression) $3, (int) $2, true,
1733 (MemberName) $4, (Parameters) $6, (Attributes) $1);
1735 Report.Error (531, lexer.Location, "`{0}': interface members cannot have a definition",
1736 ((Method)$$).GetSignatureForError ());
1740 interface_property_declaration
1746 lexer.PropertyParsing = true;
1747 implicit_value_parameter_type = (Expression)$3;
1749 accessor_declarations
1751 has_get = has_set = false;
1752 lexer.PropertyParsing = false;
1753 implicit_value_parameter_type = null;
1757 LocatedToken lt = (LocatedToken) $4;
1758 MemberName name = new MemberName (lt.Value, lt.Location);
1760 if ($3 == TypeManager.system_void_expr) {
1761 Report.Error (547, lt.Location, "`{0}': property or indexer cannot have void type", lt.Value);
1767 p = new Property (current_class, (Expression) $3, (int) $2, true,
1768 name, (Attributes) $1,
1771 Report.Error (548, p.Location, "`{0}': property or indexer must have at least one accessor", p.GetSignatureForError ());
1775 Accessors accessor = (Accessors) $7;
1776 p = new Property (current_class, (Expression) $3, (int) $2, true,
1777 name, (Attributes) $1,
1778 accessor.get_or_add, accessor.set_or_remove, accessor.declared_in_reverse);
1780 if (accessor.get_or_add != null && accessor.get_or_add.Block != null) {
1781 Report.Error (531, p.Location, "`{0}.get': interface members cannot have a definition", p.GetSignatureForError ());
1786 if (accessor.set_or_remove != null && accessor.set_or_remove.Block != null) {
1787 Report.Error (531, p.Location, "`{0}.set': interface members cannot have a definition", p.GetSignatureForError ());
1792 if (RootContext.Documentation != null)
1793 p.DocComment = Lexer.consume_doc_comment ();
1800 CheckIdentifierToken (yyToken, GetLocation ($4));
1806 interface_event_declaration
1807 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1809 LocatedToken lt = (LocatedToken) $5;
1810 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1811 new MemberName (lt.Value, lt.Location),
1813 if (RootContext.Documentation != null)
1814 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1816 | opt_attributes opt_new EVENT type error {
1817 CheckIdentifierToken (yyToken, GetLocation ($5));
1820 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1821 LocatedToken lt = (LocatedToken) $5;
1822 Report.Error (68, lt.Location, "`{0}.{1}': event in interface cannot have initializer", current_container.Name, lt.Value);
1825 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1827 implicit_value_parameter_type = (Expression) $4;
1828 lexer.EventParsing = true;
1830 event_accessor_declarations
1832 lexer.EventParsing = false;
1833 implicit_value_parameter_type = null;
1836 Report.Error (69, (Location) $3, "Event in interface cannot have add or remove accessors");
1841 interface_indexer_declaration
1842 : opt_attributes opt_new type THIS
1843 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1846 lexer.PropertyParsing = true;
1847 implicit_value_parameter_type = (Expression)$3;
1849 accessor_declarations
1851 has_get = has_set = false;
1852 lexer.PropertyParsing = false;
1853 implicit_value_parameter_type = null;
1859 i = new Indexer (current_class, (Expression) $3,
1860 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1861 (int) $2, true, (Parameters) $6, (Attributes) $1,
1864 Report.Error (548, i.Location, "`{0}': property or indexer must have at least one accessor", i.GetSignatureForError ());
1868 Accessors accessors = (Accessors) $10;
1869 i = new Indexer (current_class, (Expression) $3,
1870 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1871 (int) $2, true, (Parameters) $6, (Attributes) $1,
1872 accessors.get_or_add, accessors.set_or_remove, accessors.declared_in_reverse);
1874 if (accessors.get_or_add != null && accessors.get_or_add.Block != null) {
1875 Report.Error (531, i.Location, "`{0}.get': interface members cannot have a definition", i.GetSignatureForError ());
1880 if (accessors.set_or_remove != null && accessors.set_or_remove.Block != null) {
1881 Report.Error (531, i.Location, "`{0}.set': interface members cannot have a definition", i.GetSignatureForError ());
1886 if (RootContext.Documentation != null)
1887 i.DocComment = ConsumeStoredComment ();
1893 operator_declaration
1894 : opt_attributes opt_modifiers operator_declarator
1896 anonymous_host = SimpleAnonymousHost.GetSimple ();
1903 OperatorDeclaration decl = (OperatorDeclaration) $3;
1905 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1907 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null, decl.location);
1908 if (decl.arg2type != null)
1909 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null, decl.location);
1911 Operator op = new Operator (
1912 current_class, decl.optype, decl.ret_type, (int) $2,
1913 new Parameters (param_list),
1914 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1916 if (RootContext.Documentation != null) {
1917 op.DocComment = tmpComment;
1918 Lexer.doc_state = XmlCommentState.Allowed;
1921 SimpleAnonymousHost.Simple.Propagate (op);
1922 anonymous_host = null;
1924 // Note again, checking is done in semantic analysis
1925 current_container.AddOperator (op);
1927 current_local_parameters = null;
1933 | SEMICOLON { $$ = null; }
1936 : type OPERATOR overloadable_operator
1937 open_parens opt_attributes opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
1939 // TODO: wrong location
1940 if ((Parameter.Modifier)$6 != Parameter.Modifier.NONE)
1941 Error_ParameterModifierNotValid ((Location) $2);
1943 LocatedToken lt = (LocatedToken) $8;
1944 Operator.OpType op = (Operator.OpType) $3;
1945 CheckUnaryOperator (op, lt.Location);
1947 if (op == Operator.OpType.Addition)
1948 op = Operator.OpType.UnaryPlus;
1950 if (op == Operator.OpType.Subtraction)
1951 op = Operator.OpType.UnaryNegation;
1953 Parameter [] pars = new Parameter [1];
1954 Expression type = (Expression) $7;
1956 pars [0] = new Parameter (type, lt.Value, Parameter.Modifier.NONE, (Attributes) $5, lt.Location);
1958 current_local_parameters = new Parameters (pars);
1960 if (RootContext.Documentation != null) {
1961 tmpComment = Lexer.consume_doc_comment ();
1962 Lexer.doc_state = XmlCommentState.NotAllowed;
1965 $$ = new OperatorDeclaration (op, (Expression) $1, type, lt.Value,
1966 null, null, (Location) $2);
1968 | type OPERATOR overloadable_operator
1970 opt_attributes opt_parameter_modifier type IDENTIFIER COMMA
1971 opt_attributes opt_parameter_modifier type IDENTIFIER
1974 // TODO: wrong location
1975 if ((Parameter.Modifier)$6 != Parameter.Modifier.NONE || (Parameter.Modifier)$11 != Parameter.Modifier.NONE)
1976 Error_ParameterModifierNotValid ((Location) $2);
1978 LocatedToken ltParam1 = (LocatedToken) $8;
1979 LocatedToken ltParam2 = (LocatedToken) $13;
1980 CheckBinaryOperator ((Operator.OpType) $3, (Location) $2);
1982 Parameter [] pars = new Parameter [2];
1984 Expression typeL = (Expression) $7;
1985 Expression typeR = (Expression) $12;
1987 pars [0] = new Parameter (typeL, ltParam1.Value, Parameter.Modifier.NONE, (Attributes) $5, ltParam1.Location);
1988 pars [1] = new Parameter (typeR, ltParam2.Value, Parameter.Modifier.NONE, (Attributes) $10, ltParam2.Location);
1990 current_local_parameters = new Parameters (pars);
1992 if (RootContext.Documentation != null) {
1993 tmpComment = Lexer.consume_doc_comment ();
1994 Lexer.doc_state = XmlCommentState.NotAllowed;
1997 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1998 typeL, ltParam1.Value,
1999 typeR, ltParam2.Value, (Location) $2);
2001 | conversion_operator_declarator
2002 | type OPERATOR overloadable_operator
2004 opt_attributes opt_parameter_modifier type IDENTIFIER COMMA
2005 opt_attributes opt_parameter_modifier type IDENTIFIER COMMA error
2007 Report.Error (1534, (Location) $2, "Overloaded binary operator `{0}' takes two parameters",
2008 Operator.GetName ((Operator.OpType) $3));
2011 | type OPERATOR overloadable_operator
2012 open_parens CLOSE_PARENS
2014 Report.Error (1535, (Location) $2, "Overloaded unary operator `{0}' takes one parameter",
2015 Operator.GetName ((Operator.OpType) $3));
2020 overloadable_operator
2022 : BANG { $$ = Operator.OpType.LogicalNot; }
2023 | TILDE { $$ = Operator.OpType.OnesComplement; }
2024 | OP_INC { $$ = Operator.OpType.Increment; }
2025 | OP_DEC { $$ = Operator.OpType.Decrement; }
2026 | TRUE { $$ = Operator.OpType.True; }
2027 | FALSE { $$ = Operator.OpType.False; }
2028 // Unary and binary:
2029 | PLUS { $$ = Operator.OpType.Addition; }
2030 | MINUS { $$ = Operator.OpType.Subtraction; }
2032 | STAR { $$ = Operator.OpType.Multiply; }
2033 | DIV { $$ = Operator.OpType.Division; }
2034 | PERCENT { $$ = Operator.OpType.Modulus; }
2035 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
2036 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
2037 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
2038 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
2039 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
2040 | OP_EQ { $$ = Operator.OpType.Equality; }
2041 | OP_NE { $$ = Operator.OpType.Inequality; }
2042 | OP_GT { $$ = Operator.OpType.GreaterThan; }
2043 | OP_LT { $$ = Operator.OpType.LessThan; }
2044 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
2045 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
2048 conversion_operator_declarator
2049 : IMPLICIT OPERATOR type open_parens opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
2051 // TODO: wrong location
2052 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
2053 Error_ParameterModifierNotValid (GetLocation ($4));
2055 LocatedToken lt = (LocatedToken) $7;
2056 Parameter [] pars = new Parameter [1];
2058 pars [0] = new Parameter ((Expression) $6, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2060 current_local_parameters = new Parameters (pars);
2062 if (RootContext.Documentation != null) {
2063 tmpComment = Lexer.consume_doc_comment ();
2064 Lexer.doc_state = XmlCommentState.NotAllowed;
2067 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $6, lt.Value,
2068 null, null, (Location) $2);
2070 | EXPLICIT OPERATOR type open_parens opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
2072 // TODO: wrong location
2073 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
2074 Error_ParameterModifierNotValid (GetLocation ($4));
2076 LocatedToken lt = (LocatedToken) $7;
2077 Parameter [] pars = new Parameter [1];
2079 pars [0] = new Parameter ((Expression) $6, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2081 current_local_parameters = new Parameters (pars);
2083 if (RootContext.Documentation != null) {
2084 tmpComment = Lexer.consume_doc_comment ();
2085 Lexer.doc_state = XmlCommentState.NotAllowed;
2088 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $6, lt.Value,
2089 null, null, (Location) $2);
2093 syntax_error ((Location) $1, "'operator' expected");
2097 syntax_error ((Location) $1, "'operator' expected");
2101 constructor_declaration
2104 constructor_declarator
2107 Constructor c = (Constructor) $3;
2108 c.Block = (ToplevelBlock) $4;
2109 c.OptAttributes = (Attributes) $1;
2110 c.ModFlags = (int) $2;
2112 if (RootContext.Documentation != null)
2113 c.DocComment = ConsumeStoredComment ();
2115 if (c.Name == current_container.Basename){
2116 if ((c.ModFlags & Modifiers.STATIC) != 0){
2117 if ((c.ModFlags & Modifiers.Accessibility) != 0){
2118 Report.Error (515, c.Location,
2119 "`{0}': access modifiers are not allowed on static constructors",
2120 c.GetSignatureForError ());
2123 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2125 if (c.Initializer != null){
2126 Report.Error (514, c.Location,
2127 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2128 c.GetSignatureForError ());
2131 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2134 // We let another layer check the validity of the constructor.
2135 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
2138 current_container.AddConstructor (c);
2140 current_local_parameters = null;
2141 if (RootContext.Documentation != null)
2142 Lexer.doc_state = XmlCommentState.Allowed;
2146 constructor_declarator
2147 : constructor_header
2151 | constructor_header constructor_initializer
2153 ((Constructor)$1).Initializer = (ConstructorInitializer) $2;
2161 if (RootContext.Documentation != null) {
2162 tmpComment = Lexer.consume_doc_comment ();
2163 Lexer.doc_state = XmlCommentState.Allowed;
2166 open_parens opt_formal_parameter_list CLOSE_PARENS
2168 LocatedToken lt = (LocatedToken) $1;
2169 current_local_parameters = (Parameters) $4;
2170 current_block = new ToplevelBlock (null, current_local_parameters, null, lt.Location);
2172 $$ = new Constructor (current_class, lt.Value, 0, current_local_parameters,
2175 anonymous_host = (IAnonymousHost) $$;
2181 | SEMICOLON { current_block = null; $$ = null; }
2184 constructor_initializer
2185 : COLON BASE open_parens opt_argument_list CLOSE_PARENS
2187 $$ = new ConstructorBaseInitializer ((ArrayList) $4, (Location) $2);
2189 | COLON THIS open_parens opt_argument_list CLOSE_PARENS
2191 $$ = new ConstructorThisInitializer ((ArrayList) $4, (Location) $2);
2194 Report.Error (1018, (Location) $1, "Keyword this or base expected");
2200 : /* EMPTY */ { $$ = 0; }
2201 | UNSAFE { $$ = Modifiers.UNSAFE; }
2202 | EXTERN { $$ = Modifiers.EXTERN; }
2205 destructor_declaration
2206 : opt_attributes opt_finalizer TILDE
2208 if (RootContext.Documentation != null) {
2209 tmpComment = Lexer.consume_doc_comment ();
2210 Lexer.doc_state = XmlCommentState.NotAllowed;
2213 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2215 LocatedToken lt = (LocatedToken) $5;
2216 if (lt.Value != current_container.Basename){
2217 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2218 } else if (current_container.Kind != Kind.Class){
2219 Report.Error (575, lt.Location, "Only class types can contain destructor");
2221 Location l = lt.Location;
2224 if (!RootContext.StdLib && current_container.Name == "System.Object")
2225 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2227 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2229 Method d = new Destructor (
2230 current_class, TypeManager.system_void_expr, m, "Finalize",
2231 Parameters.EmptyReadOnlyParameters, (Attributes) $1, l);
2232 if (RootContext.Documentation != null)
2233 d.DocComment = ConsumeStoredComment ();
2235 d.Block = (ToplevelBlock) $8;
2236 current_container.AddMethod (d);
2244 EVENT type variable_declarators SEMICOLON
2246 current_array_type = null;
2247 foreach (VariableDeclaration var in (ArrayList) $5) {
2249 MemberName name = new MemberName (var.identifier,
2252 EventField e = new EventField (
2253 current_class, (Expression) $4, (int) $2, false, name,
2256 e.Initializer = var.expression_or_array_initializer;
2258 current_container.AddEvent (e);
2260 if (RootContext.Documentation != null) {
2261 e.DocComment = Lexer.consume_doc_comment ();
2262 Lexer.doc_state = XmlCommentState.Allowed;
2268 EVENT type namespace_or_type_name
2271 implicit_value_parameter_type = (Expression) $4;
2272 lexer.EventParsing = true;
2274 event_accessor_declarations
2276 lexer.EventParsing = false;
2280 MemberName name = (MemberName) $5;
2283 Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2284 current_container.Name, name.ToString ());
2287 Accessors accessors = (Accessors) $8;
2288 if (accessors.get_or_add == null || accessors.set_or_remove == null)
2289 // CS0073 is already reported, so no CS0065 here.
2292 Event e = new EventProperty (
2293 current_class, (Expression) $4, (int) $2, false, name,
2294 (Attributes) $1, accessors.get_or_add, accessors.set_or_remove);
2295 if (RootContext.Documentation != null) {
2296 e.DocComment = Lexer.consume_doc_comment ();
2297 Lexer.doc_state = XmlCommentState.Allowed;
2300 current_container.AddEvent (e);
2301 implicit_value_parameter_type = null;
2305 | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
2306 MemberName mn = (MemberName) $5;
2308 if (mn.Left != null)
2309 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2311 Report.Error (71, mn.Location, "Event declaration should use property syntax");
2313 if (RootContext.Documentation != null)
2314 Lexer.doc_state = XmlCommentState.Allowed;
2318 event_accessor_declarations
2319 : add_accessor_declaration remove_accessor_declaration
2321 $$ = new Accessors ((Accessor) $1, (Accessor) $2);
2323 | remove_accessor_declaration add_accessor_declaration
2325 Accessors accessors = new Accessors ((Accessor) $2, (Accessor) $1);
2326 accessors.declared_in_reverse = true;
2329 | add_accessor_declaration { $$ = null; }
2330 | remove_accessor_declaration { $$ = null; }
2333 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2339 add_accessor_declaration
2340 : opt_attributes ADD
2342 Parameter [] args = new Parameter [1];
2343 Parameter implicit_value_parameter = new Parameter (
2344 implicit_value_parameter_type, "value",
2345 Parameter.Modifier.NONE, null, (Location) $2);
2347 args [0] = implicit_value_parameter;
2349 current_local_parameters = new Parameters (args);
2350 lexer.EventParsing = false;
2352 anonymous_host = SimpleAnonymousHost.GetSimple ();
2356 Accessor accessor = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2357 lexer.EventParsing = true;
2359 current_local_parameters = null;
2360 SimpleAnonymousHost.Simple.Propagate (accessor);
2361 anonymous_host = null;
2365 | opt_attributes ADD error {
2366 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2369 | opt_attributes modifiers ADD {
2370 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2375 remove_accessor_declaration
2376 : opt_attributes REMOVE
2378 Parameter [] args = new Parameter [1];
2379 Parameter implicit_value_parameter = new Parameter (
2380 implicit_value_parameter_type, "value",
2381 Parameter.Modifier.NONE, null, (Location) $2);
2383 args [0] = implicit_value_parameter;
2385 current_local_parameters = new Parameters (args);
2386 lexer.EventParsing = false;
2390 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2391 lexer.EventParsing = true;
2393 | opt_attributes REMOVE error {
2394 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2397 | opt_attributes modifiers REMOVE {
2398 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2404 : opt_attributes opt_modifiers indexer_declarator
2407 IndexerDeclaration decl = (IndexerDeclaration) $3;
2409 implicit_value_parameter_type = decl.type;
2411 lexer.PropertyParsing = true;
2412 parsing_indexer = true;
2414 indexer_parameters = decl.param_list;
2415 anonymous_host = SimpleAnonymousHost.GetSimple ();
2417 accessor_declarations
2419 lexer.PropertyParsing = false;
2420 has_get = has_set = false;
2421 parsing_indexer = false;
2428 // The signature is computed from the signature of the indexer. Look
2429 // at section 3.6 on the spec
2431 IndexerDeclaration decl = (IndexerDeclaration) $3;
2432 Location loc = decl.location;
2433 Accessors accessors = (Accessors) $6;
2434 Accessor get_block = accessors.get_or_add;
2435 Accessor set_block = accessors.set_or_remove;
2438 if (decl.interface_type != null)
2439 name = new MemberName (decl.interface_type, TypeContainer.DefaultIndexerName, loc);
2441 name = new MemberName (TypeContainer.DefaultIndexerName, loc);
2443 indexer = new Indexer (current_class, decl.type, name,
2444 (int) $2, false, decl.param_list, (Attributes) $1,
2445 get_block, set_block, accessors.declared_in_reverse);
2447 if (RootContext.Documentation != null)
2448 indexer.DocComment = ConsumeStoredComment ();
2450 current_container.AddIndexer (indexer);
2452 current_local_parameters = null;
2453 implicit_value_parameter_type = null;
2454 indexer_parameters = null;
2459 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2461 Parameters pars = (Parameters) $4;
2462 if (pars.HasArglist) {
2463 // "__arglist is not valid in this context"
2464 Report.Error (1669, (Location) $2, "__arglist is not valid in this context");
2465 } else if (pars.Empty){
2466 Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
2468 if (RootContext.Documentation != null) {
2469 tmpComment = Lexer.consume_doc_comment ();
2470 Lexer.doc_state = XmlCommentState.Allowed;
2473 $$ = new IndexerDeclaration ((Expression) $1, null, pars, (Location) $2);
2475 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2477 Parameters pars = (Parameters) $6;
2479 if (pars.HasArglist) {
2480 // "__arglist is not valid in this context"
2481 Report.Error (1669, (Location) $4, "__arglist is not valid in this context");
2482 } else if (pars.Empty){
2483 Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
2486 MemberName name = (MemberName) $2;
2487 $$ = new IndexerDeclaration ((Expression) $1, name, pars, (Location) $4);
2489 if (RootContext.Documentation != null) {
2490 tmpComment = Lexer.consume_doc_comment ();
2491 Lexer.doc_state = XmlCommentState.Allowed;
2501 if (RootContext.Documentation != null)
2502 enumTypeComment = Lexer.consume_doc_comment ();
2507 LocatedToken lt = (LocatedToken) $4;
2508 Location enum_location = lt.Location;
2510 MemberName name = MakeName (new MemberName (lt.Value, enum_location));
2511 Enum e = new Enum (current_namespace, current_class, (Expression) $5, (int) $2,
2512 name, (Attributes) $1);
2514 if (RootContext.Documentation != null)
2515 e.DocComment = enumTypeComment;
2518 EnumMember em = null;
2519 foreach (VariableDeclaration ev in (ArrayList) $7) {
2520 em = new EnumMember (
2521 e, em, ev.identifier, (Expression) ev.expression_or_array_initializer,
2522 ev.OptAttributes, ev.Location);
2524 // if (RootContext.Documentation != null)
2525 em.DocComment = ev.DocComment;
2527 e.AddEnumMember (em);
2530 current_container.AddTypeContainer (e);
2537 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2538 | COLON type { $$ = $2; }
2544 if (RootContext.Documentation != null)
2545 Lexer.doc_state = XmlCommentState.Allowed;
2547 opt_enum_member_declarations
2549 // here will be evaluated after CLOSE_BLACE is consumed.
2550 if (RootContext.Documentation != null)
2551 Lexer.doc_state = XmlCommentState.Allowed;
2559 opt_enum_member_declarations
2560 : /* empty */ { $$ = new ArrayList (4); }
2561 | enum_member_declarations opt_comma { $$ = $1; }
2564 enum_member_declarations
2565 : enum_member_declaration
2567 ArrayList l = new ArrayList (4);
2572 | enum_member_declarations COMMA enum_member_declaration
2574 ArrayList l = (ArrayList) $1;
2582 enum_member_declaration
2583 : opt_attributes IDENTIFIER
2585 VariableDeclaration vd = new VariableDeclaration (
2586 (LocatedToken) $2, null, (Attributes) $1);
2588 if (RootContext.Documentation != null) {
2589 vd.DocComment = Lexer.consume_doc_comment ();
2590 Lexer.doc_state = XmlCommentState.Allowed;
2595 | opt_attributes IDENTIFIER
2597 if (RootContext.Documentation != null) {
2598 tmpComment = Lexer.consume_doc_comment ();
2599 Lexer.doc_state = XmlCommentState.NotAllowed;
2604 VariableDeclaration vd = new VariableDeclaration (
2605 (LocatedToken) $2, $5, (Attributes) $1);
2607 if (RootContext.Documentation != null)
2608 vd.DocComment = ConsumeStoredComment ();
2614 delegate_declaration
2617 DELEGATE type member_name
2618 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2621 MemberName name = MakeName ((MemberName) $5);
2622 Parameters p = (Parameters) $7;
2624 // TODO: wrong location
2625 Report.Error (1669, name.Location, "__arglist is not valid in this context");
2628 Delegate del = new Delegate (current_namespace, current_class, (Expression) $4,
2629 (int) $2, name, p, (Attributes) $1);
2631 if (RootContext.Documentation != null) {
2632 del.DocComment = Lexer.consume_doc_comment ();
2633 Lexer.doc_state = XmlCommentState.Allowed;
2636 current_container.AddDelegate (del);
2641 namespace_or_type_name
2643 | IDENTIFIER DOUBLE_COLON IDENTIFIER {
2644 LocatedToken lt1 = (LocatedToken) $1;
2645 LocatedToken lt2 = (LocatedToken) $3;
2646 $$ = new MemberName (lt1.Value, lt2.Value, lt2.Location);
2648 | namespace_or_type_name DOT IDENTIFIER {
2649 LocatedToken lt = (LocatedToken) $3;
2650 $$ = new MemberName ((MemberName) $1, lt.Value);
2656 LocatedToken lt = (LocatedToken) $1;
2657 $$ = new MemberName (lt.Value, lt.Location);
2662 * Before you think of adding a return_type, notice that we have been
2663 * using two rules in the places where it matters (one rule using type
2664 * and another identical one that uses VOID as the return type). This
2665 * gets rid of a shift/reduce couple
2668 : namespace_or_type_name
2670 MemberName name = (MemberName) $1;
2671 $$ = name.GetTypeExpression ();
2683 // Note that here only unmanaged types are allowed but we
2684 // can't perform checks during this phase - we do it during
2685 // semantic analysis.
2687 $$ = new ComposedCast ((Expression) $1, "*", Lexer.Location);
2691 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2697 | non_expression_type rank_specifier
2699 Location loc = GetLocation ($1);
2701 loc = lexer.Location;
2702 $$ = new ComposedCast ((Expression) $1, (string) $2, loc);
2704 | non_expression_type STAR
2706 Location loc = GetLocation ($1);
2708 loc = lexer.Location;
2709 $$ = new ComposedCast ((Expression) $1, "*", loc);
2711 | expression rank_specifiers
2713 $$ = new ComposedCast ((Expression) $1, (string) $2);
2717 $$ = new ComposedCast ((Expression) $1, "*");
2721 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2722 // through this different path
2724 | multiplicative_expression STAR
2726 $$ = new ComposedCast ((Expression) $1, "*");
2733 ArrayList types = new ArrayList (4);
2738 | type_list COMMA type
2740 ArrayList types = (ArrayList) $1;
2748 * replaces all the productions for isolating the various
2749 * simple types, but we need this to reuse it easily in local_variable_type
2752 : OBJECT { $$ = TypeManager.system_object_expr; }
2753 | STRING { $$ = TypeManager.system_string_expr; }
2754 | BOOL { $$ = TypeManager.system_boolean_expr; }
2755 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2756 | FLOAT { $$ = TypeManager.system_single_expr; }
2757 | DOUBLE { $$ = TypeManager.system_double_expr; }
2762 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2763 | BYTE { $$ = TypeManager.system_byte_expr; }
2764 | SHORT { $$ = TypeManager.system_int16_expr; }
2765 | USHORT { $$ = TypeManager.system_uint16_expr; }
2766 | INT { $$ = TypeManager.system_int32_expr; }
2767 | UINT { $$ = TypeManager.system_uint32_expr; }
2768 | LONG { $$ = TypeManager.system_int64_expr; }
2769 | ULONG { $$ = TypeManager.system_uint64_expr; }
2770 | CHAR { $$ = TypeManager.system_char_expr; }
2771 | VOID { $$ = TypeManager.system_void_expr; }
2775 : type rank_specifiers
2777 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2);
2782 // Expressions, section 7.5
2791 MemberName mn = (MemberName) $1;
2792 $$ = mn.GetTypeExpression ();
2794 | IDENTIFIER DOUBLE_COLON IDENTIFIER
2796 LocatedToken lt1 = (LocatedToken) $1;
2797 LocatedToken lt2 = (LocatedToken) $3;
2798 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, lt2.Location);
2800 | parenthesized_expression
2802 | invocation_expression
2806 | post_increment_expression
2807 | post_decrement_expression
2811 | checked_expression
2812 | unchecked_expression
2813 | pointer_member_access
2814 | anonymous_method_expression
2821 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
2822 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); }
2823 | NULL { $$ = new NullLiteral (lexer.Location); }
2827 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
2828 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
2829 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
2834 object v = lexer.Value;
2837 $$ = new IntLiteral ((int) v, lexer.Location);
2838 } else if (v is uint)
2839 $$ = new UIntLiteral ((UInt32) v, lexer.Location);
2841 $$ = new LongLiteral ((Int64) v, lexer.Location);
2842 else if (v is ulong)
2843 $$ = new ULongLiteral ((UInt64) v, lexer.Location);
2845 Console.WriteLine ("OOPS. Unexpected result from scanner");
2850 : TRUE { $$ = new BoolLiteral (true, lexer.Location); }
2851 | FALSE { $$ = new BoolLiteral (false, lexer.Location); }
2854 parenthesized_expression_0
2855 : OPEN_PARENS expression CLOSE_PARENS
2858 lexer.Deambiguate_CloseParens ($$);
2859 // After this, the next token returned is one of
2860 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST (CLOSE_PARENS), CLOSE_PARENS_OPEN_PARENS
2861 // or CLOSE_PARENS_MINUS.
2863 | OPEN_PARENS expression error { CheckToken (1026, yyToken, "Expecting ')'", lexer.Location); }
2866 parenthesized_expression
2867 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2871 | parenthesized_expression_0 CLOSE_PARENS
2875 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2877 // If a parenthesized expression is followed by a minus, we need to wrap
2878 // the expression inside a ParenthesizedExpression for the CS0075 check
2879 // in Binary.DoResolve().
2880 $$ = new ParenthesizedExpression ((Expression) $1);
2885 : primary_expression DOT IDENTIFIER
2887 LocatedToken lt = (LocatedToken) $3;
2888 $$ = new MemberAccess ((Expression) $1, lt.Value);
2890 | predefined_type DOT IDENTIFIER
2892 LocatedToken lt = (LocatedToken) $3;
2893 // TODO: Location is wrong as some predefined types doesn't hold a location
2894 $$ = new MemberAccess ((Expression) $1, lt.Value, lt.Location);
2902 invocation_expression
2903 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2906 Report.Error (1, (Location) $2, "Parse error");
2908 $$ = new Invocation ((Expression) $1, (ArrayList) $3);
2910 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2912 $$ = new Invocation ((Expression) $1, new ArrayList ());
2914 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2916 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
2918 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2920 ArrayList args = new ArrayList (1);
2922 $$ = new Invocation ((Expression) $1, args);
2924 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2926 ArrayList args = ((ArrayList) $4);
2928 $$ = new Invocation ((Expression) $1, args);
2933 : /* empty */ { $$ = null; }
2940 ArrayList list = new ArrayList (4);
2944 | argument_list COMMA argument
2946 ArrayList list = (ArrayList) $1;
2950 | argument_list error {
2951 CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
2959 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2961 | non_simple_argument
2968 : REF variable_reference
2970 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2972 | OUT variable_reference
2974 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2976 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2978 ArrayList list = (ArrayList) $3;
2979 Argument[] args = new Argument [list.Count];
2980 list.CopyTo (args, 0);
2982 Expression expr = new Arglist (args, (Location) $1);
2983 $$ = new Argument (expr, Argument.AType.Expression);
2985 | ARGLIST OPEN_PARENS CLOSE_PARENS
2987 $$ = new Argument (new Arglist ((Location) $1), Argument.AType.Expression);
2991 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
2996 : expression { note ("section 5.4"); $$ = $1; }
3000 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
3002 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
3004 | primary_expression rank_specifiers
3006 // So the super-trick is that primary_expression
3007 // can only be either a SimpleName or a MemberAccess.
3008 // The MemberAccess case arises when you have a fully qualified type-name like :
3010 // SimpleName is when you have
3013 Expression expr = (Expression) $1;
3014 if (expr is ComposedCast){
3015 $$ = new ComposedCast (expr, (string) $2);
3016 } else if (!(expr is SimpleName || expr is MemberAccess || expr is QualifiedAliasMember)){
3017 Error_ExpectingTypeName (expr);
3018 $$ = TypeManager.system_object_expr;
3021 // So we extract the string corresponding to the SimpleName
3024 $$ = new ComposedCast (expr, (string) $2);
3026 current_array_type = (Expression)$$;
3033 ArrayList list = new ArrayList (4);
3037 | expression_list COMMA expression
3039 ArrayList list = (ArrayList) $1;
3048 $$ = new This (current_block, (Location) $1);
3053 : BASE DOT IDENTIFIER
3055 LocatedToken lt = (LocatedToken) $3;
3056 $$ = new BaseAccess (lt.Value, lt.Location);
3058 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
3060 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
3063 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
3068 post_increment_expression
3069 : primary_expression OP_INC
3071 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
3072 (Expression) $1, (Location) $2);
3076 post_decrement_expression
3077 : primary_expression OP_DEC
3079 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
3080 (Expression) $1, (Location) $2);
3085 : object_or_delegate_creation_expression
3086 | array_creation_expression
3089 object_or_delegate_creation_expression
3090 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
3092 $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
3096 array_creation_expression
3097 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
3099 opt_array_initializer
3101 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
3103 | NEW type rank_specifiers array_initializer
3105 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, (Location) $1);
3109 Report.Error (1031, (Location) $1, "Type expected");
3114 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
3131 : rank_specifier opt_rank_specifier
3133 $$ = (string) $2 + (string) $1;
3138 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3140 $$ = "[" + (string) $2 + "]";
3160 | dim_separators COMMA
3162 $$ = (string) $1 + ",";
3166 opt_array_initializer
3178 : OPEN_BRACE CLOSE_BRACE
3180 ArrayList list = new ArrayList (4);
3183 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3185 $$ = (ArrayList) $2;
3189 variable_initializer_list
3190 : variable_initializer
3192 ArrayList list = new ArrayList (4);
3196 | variable_initializer_list COMMA variable_initializer
3198 ArrayList list = (ArrayList) $1;
3207 pushed_current_array_type = current_array_type;
3209 OPEN_PARENS type CLOSE_PARENS
3211 Expression type = (Expression)$4;
3212 if (type == TypeManager.system_void_expr)
3213 $$ = new TypeOfVoid ((Location) $1);
3215 $$ = new TypeOf (type, (Location) $1);
3216 current_array_type = pushed_current_array_type;
3221 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3222 $$ = new SizeOf ((Expression) $3, (Location) $1);
3227 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3229 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3233 unchecked_expression
3234 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3236 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3240 pointer_member_access
3241 : primary_expression OP_PTR IDENTIFIER
3244 LocatedToken lt = (LocatedToken) $3;
3246 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
3247 $$ = new MemberAccess (deref, lt.Value);
3251 anonymous_method_expression
3252 : DELEGATE opt_anonymous_method_signature
3254 start_anonymous (false, (Parameters) $2, (Location) $1);
3258 $$ = end_anonymous ((ToplevelBlock) $4, (Location) $1);
3262 opt_anonymous_method_signature
3263 : /* empty */ { $$ = null; }
3264 | anonymous_method_signature
3267 anonymous_method_signature
3268 : open_parens opt_anonymous_method_parameter_list CLOSE_PARENS
3271 $$ = Parameters.EmptyReadOnlyParameters;
3273 ArrayList par_list = (ArrayList) $2;
3274 Parameter [] pars = new Parameter [par_list.Count];
3275 par_list.CopyTo (pars);
3276 $$ = new Parameters (pars);
3281 opt_anonymous_method_parameter_list
3282 : /* empty */ { $$ = null; }
3283 | anonymous_method_parameter_list { $$ = $1; }
3286 anonymous_method_parameter_list
3287 : anonymous_method_parameter
3289 ArrayList a = new ArrayList (4);
3293 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3295 ArrayList a = (ArrayList) $1;
3301 anonymous_method_parameter
3302 : opt_parameter_modifier type IDENTIFIER {
3303 LocatedToken lt = (LocatedToken) $3;
3304 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3306 | PARAMS type IDENTIFIER {
3307 Report.Error (1670, ((LocatedToken) $3).Location, "The `params' modifier is not allowed in anonymous method declaration");
3313 : primary_expression
3314 | BANG prefixed_unary_expression
3316 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3318 | TILDE prefixed_unary_expression
3320 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3326 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3328 $$ = new Cast ((Expression) $1, (Expression) $3);
3330 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3332 $$ = new Cast ((Expression) $1, (Expression) $3);
3338 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3340 // TODO: wrong location
3341 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3346 // The idea to split this out is from Rhys' grammar
3347 // to solve the problem with casts.
3349 prefixed_unary_expression
3351 | PLUS prefixed_unary_expression
3353 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3355 | MINUS prefixed_unary_expression
3357 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3359 | OP_INC prefixed_unary_expression
3361 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3362 (Expression) $2, (Location) $1);
3364 | OP_DEC prefixed_unary_expression
3366 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3367 (Expression) $2, (Location) $1);
3369 | STAR prefixed_unary_expression
3371 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, (Location) $1);
3373 | BITWISE_AND prefixed_unary_expression
3375 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3379 pre_increment_expression
3380 : OP_INC prefixed_unary_expression
3382 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3383 (Expression) $2, (Location) $1);
3387 pre_decrement_expression
3388 : OP_DEC prefixed_unary_expression
3390 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3391 (Expression) $2, (Location) $1);
3395 multiplicative_expression
3396 : prefixed_unary_expression
3397 | multiplicative_expression STAR prefixed_unary_expression
3399 $$ = new Binary (Binary.Operator.Multiply,
3400 (Expression) $1, (Expression) $3);
3402 | multiplicative_expression DIV prefixed_unary_expression
3404 $$ = new Binary (Binary.Operator.Division,
3405 (Expression) $1, (Expression) $3);
3407 | multiplicative_expression PERCENT prefixed_unary_expression
3409 $$ = new Binary (Binary.Operator.Modulus,
3410 (Expression) $1, (Expression) $3);
3415 : multiplicative_expression
3416 | additive_expression PLUS multiplicative_expression
3418 $$ = new Binary (Binary.Operator.Addition,
3419 (Expression) $1, (Expression) $3);
3421 | additive_expression MINUS multiplicative_expression
3423 $$ = new Binary (Binary.Operator.Subtraction,
3424 (Expression) $1, (Expression) $3);
3429 : additive_expression
3430 | shift_expression OP_SHIFT_LEFT additive_expression
3432 $$ = new Binary (Binary.Operator.LeftShift,
3433 (Expression) $1, (Expression) $3);
3435 | shift_expression OP_SHIFT_RIGHT additive_expression
3437 $$ = new Binary (Binary.Operator.RightShift,
3438 (Expression) $1, (Expression) $3);
3442 relational_expression
3444 | relational_expression OP_LT shift_expression
3446 $$ = new Binary (Binary.Operator.LessThan,
3447 (Expression) $1, (Expression) $3);
3449 | relational_expression OP_GT shift_expression
3451 $$ = new Binary (Binary.Operator.GreaterThan,
3452 (Expression) $1, (Expression) $3);
3454 | relational_expression OP_LE shift_expression
3456 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3457 (Expression) $1, (Expression) $3);
3459 | relational_expression OP_GE shift_expression
3461 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3462 (Expression) $1, (Expression) $3);
3464 | relational_expression IS type
3466 $$ = new Is ((Expression) $1, (Expression) $3, (Location) $2);
3468 | relational_expression AS type
3470 $$ = new As ((Expression) $1, (Expression) $3, (Location) $2);
3475 : relational_expression
3476 | equality_expression OP_EQ relational_expression
3478 $$ = new Binary (Binary.Operator.Equality,
3479 (Expression) $1, (Expression) $3);
3481 | equality_expression OP_NE relational_expression
3483 $$ = new Binary (Binary.Operator.Inequality,
3484 (Expression) $1, (Expression) $3);
3489 : equality_expression
3490 | and_expression BITWISE_AND equality_expression
3492 $$ = new Binary (Binary.Operator.BitwiseAnd,
3493 (Expression) $1, (Expression) $3);
3497 exclusive_or_expression
3499 | exclusive_or_expression CARRET and_expression
3501 $$ = new Binary (Binary.Operator.ExclusiveOr,
3502 (Expression) $1, (Expression) $3);
3506 inclusive_or_expression
3507 : exclusive_or_expression
3508 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3510 $$ = new Binary (Binary.Operator.BitwiseOr,
3511 (Expression) $1, (Expression) $3);
3515 conditional_and_expression
3516 : inclusive_or_expression
3517 | conditional_and_expression OP_AND inclusive_or_expression
3519 $$ = new Binary (Binary.Operator.LogicalAnd,
3520 (Expression) $1, (Expression) $3);
3524 conditional_or_expression
3525 : conditional_and_expression
3526 | conditional_or_expression OP_OR conditional_and_expression
3528 $$ = new Binary (Binary.Operator.LogicalOr,
3529 (Expression) $1, (Expression) $3);
3533 conditional_expression
3534 : conditional_or_expression
3535 | conditional_or_expression INTERR expression COLON expression
3537 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
3541 assignment_expression
3542 : prefixed_unary_expression ASSIGN expression
3544 $$ = new Assign ((Expression) $1, (Expression) $3);
3546 | prefixed_unary_expression OP_MULT_ASSIGN expression
3548 $$ = new CompoundAssign (
3549 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
3551 | prefixed_unary_expression OP_DIV_ASSIGN expression
3553 $$ = new CompoundAssign (
3554 Binary.Operator.Division, (Expression) $1, (Expression) $3);
3556 | prefixed_unary_expression OP_MOD_ASSIGN expression
3558 $$ = new CompoundAssign (
3559 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
3561 | prefixed_unary_expression OP_ADD_ASSIGN expression
3563 $$ = new CompoundAssign (
3564 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
3566 | prefixed_unary_expression OP_SUB_ASSIGN expression
3568 $$ = new CompoundAssign (
3569 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3571 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3573 $$ = new CompoundAssign (
3574 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
3576 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3578 $$ = new CompoundAssign (
3579 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
3581 | prefixed_unary_expression OP_AND_ASSIGN expression
3583 $$ = new CompoundAssign (
3584 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
3586 | prefixed_unary_expression OP_OR_ASSIGN expression
3588 $$ = new CompoundAssign (
3589 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
3591 | prefixed_unary_expression OP_XOR_ASSIGN expression
3593 $$ = new CompoundAssign (
3594 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
3598 implicitly_typed_lambda_parameter_list
3600 LocatedToken lt = (LocatedToken) $1;
3601 ArrayList a = new ArrayList (4);
3603 a.Add (new Parameter ((Expression)null, lt.Value, Parameter.Modifier.NONE, null, lt.Location));
3606 | implicitly_typed_lambda_parameter_list COMMA IDENTIFIER {
3607 LocatedToken lt = (LocatedToken) $3;
3608 ArrayList a = (ArrayList) $1;
3609 a.Add (new Parameter ((Expression)null, lt.Value, Parameter.Modifier.NONE, null, lt.Location));
3614 explicitly_typed_lambda_parameter_list
3615 : explicitly_typed_lambda_parameter
3617 ArrayList pars = new ArrayList (4);
3622 | explicitly_typed_lambda_parameter_list COMMA explicitly_typed_lambda_parameter
3624 ArrayList pars = (ArrayList) $1;
3631 explicitly_typed_lambda_parameter
3632 : parameter_modifier type IDENTIFIER
3634 LocatedToken lt = (LocatedToken) $3;
3636 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3640 LocatedToken lt = (LocatedToken) $2;
3642 $$ = new Parameter ((Expression) $1, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
3646 lambda_parameter_list
3647 : implicitly_typed_lambda_parameter_list { $$ = $1; }
3648 | explicitly_typed_lambda_parameter_list { $$ = $1; }
3651 opt_lambda_parameter_list
3652 : /* empty */ { $$ = null; }
3653 | lambda_parameter_list {
3654 ArrayList pars_list = (ArrayList) $1;
3656 Parameter [] pars = new Parameter [pars_list.Count];
3657 pars_list.CopyTo (pars);
3658 $$ = new Parameters (pars);
3662 lambda_expression_body
3664 start_block (lexer.Location);
3668 Block b = end_block (lexer.Location);
3669 b.AddStatement (new ContextualReturn ((Expression) $2));
3680 Parameter [] pars = new Parameter [1];
3681 LocatedToken lt = (LocatedToken) $1;
3682 pars [0] = new Parameter ((Expression)null, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
3684 Parameters parameters = new Parameters (pars);
3686 start_anonymous (true, parameters, (Location) $2);
3688 lambda_expression_body
3690 $$ = end_anonymous ((ToplevelBlock) $4, (Location) $2);
3692 | OPEN_PARENS_LAMBDA opt_lambda_parameter_list CLOSE_PARENS ARROW
3694 start_anonymous (true, (Parameters) $2, (Location) $4);
3696 lambda_expression_body
3698 $$ = end_anonymous ((ToplevelBlock) $6, (Location) $4);
3703 : conditional_expression
3704 | assignment_expression
3725 MemberName name = MakeName ((MemberName) $5);
3726 int mod_flags = (int) $2;
3728 push_current_class (new Class (
3729 current_namespace, current_class, name,
3730 mod_flags, (Attributes) $1), $3);
3735 if (current_class.Name == "System.Object") {
3736 Report.Error (537, current_class.Location,
3737 "The class System.Object cannot have a base " +
3738 "class or implement an interface.");
3740 current_container.AddBasesForPart (current_class, (ArrayList) $7);
3743 if (RootContext.Documentation != null) {
3744 current_container.DocComment = Lexer.consume_doc_comment ();
3745 Lexer.doc_state = XmlCommentState.Allowed;
3750 if (RootContext.Documentation != null)
3751 Lexer.doc_state = XmlCommentState.Allowed;
3755 $$ = pop_current_class ();
3763 { $$ = $1; } // location
3767 : /* empty */ { $$ = (int) 0; }
3773 | modifiers modifier
3778 if ((m1 & m2) != 0) {
3779 Location l = lexer.Location;
3780 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
3782 $$ = (int) (m1 | m2);
3787 : NEW { $$ = Modifiers.NEW; }
3788 | PUBLIC { $$ = Modifiers.PUBLIC; }
3789 | PROTECTED { $$ = Modifiers.PROTECTED; }
3790 | INTERNAL { $$ = Modifiers.INTERNAL; }
3791 | PRIVATE { $$ = Modifiers.PRIVATE; }
3792 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3793 | SEALED { $$ = Modifiers.SEALED; }
3794 | STATIC { $$ = Modifiers.STATIC; }
3795 | READONLY { $$ = Modifiers.READONLY; }
3796 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3797 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3798 | EXTERN { $$ = Modifiers.EXTERN; }
3799 | VOLATILE { $$ = Modifiers.VOLATILE; }
3800 | UNSAFE { $$ = Modifiers.UNSAFE; }
3804 : /* empty */ { $$ = null; }
3805 | class_base { $$ = $1; }
3809 : COLON type_list { $$ = $2; }
3817 // A block is "contained" on the following places:
3819 // property_declaration as part of the accessor body (get/set)
3820 // operator_declaration
3821 // constructor_declaration
3822 // destructor_declaration
3823 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3828 start_block ((Location) $1);
3830 opt_statement_list CLOSE_BRACE
3832 $$ = end_block ((Location) $4);
3838 opt_statement_list CLOSE_BRACE
3840 $$ = end_block ((Location) $3);
3851 | statement_list statement
3855 : declaration_statement
3857 if ($1 != null && (Block) $1 != current_block){
3858 current_block.AddStatement ((Statement) $1);
3859 current_block = (Block) $1;
3862 | valid_declaration_statement
3864 current_block.AddStatement ((Statement) $1);
3869 valid_declaration_statement
3872 | expression_statement
3873 | selection_statement
3874 | iteration_statement
3878 | unchecked_statement
3886 : valid_declaration_statement
3887 | declaration_statement
3889 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3894 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3902 $$ = EmptyStatement.Value;
3909 LocatedToken lt = (LocatedToken) $1;
3910 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
3912 if (current_block.AddLabel (labeled))
3913 current_block.AddStatement (labeled);
3918 declaration_statement
3919 : local_variable_declaration SEMICOLON
3921 current_array_type = null;
3923 DictionaryEntry de = (DictionaryEntry) $1;
3924 Expression e = (Expression) de.Key;
3926 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
3930 | local_constant_declaration SEMICOLON
3932 current_array_type = null;
3934 DictionaryEntry de = (DictionaryEntry) $1;
3936 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3942 * The following is from Rhys' grammar:
3943 * > Types in local variable declarations must be recognized as
3944 * > expressions to prevent reduce/reduce errors in the grammar.
3945 * > The expressions are converted into types during semantic analysis.
3948 : primary_expression opt_rank_specifier
3950 // FIXME: Do something smart here regarding the composition of the type.
3952 // Ok, the above "primary_expression" is there to get rid of
3953 // both reduce/reduce and shift/reduces in the grammar, it should
3954 // really just be "type_name". If you use type_name, a reduce/reduce
3955 // creeps up. If you use namespace_or_type_name (which is all we need
3956 // really) two shift/reduces appear.
3959 // So the super-trick is that primary_expression
3960 // can only be either a SimpleName or a MemberAccess.
3961 // The MemberAccess case arises when you have a fully qualified type-name like :
3963 // SimpleName is when you have
3966 Expression expr = (Expression) $1;
3967 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3968 Error_ExpectingTypeName (expr);
3972 // So we extract the string corresponding to the SimpleName
3976 if ((string) $2 == "")
3979 $$ = new ComposedCast ((Expression) $1, (string) $2);
3982 | builtin_types opt_rank_specifier
3984 if ((string) $2 == "")
3987 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3991 local_variable_pointer_type
3992 : primary_expression STAR
3994 Expression expr = (Expression) $1;
3996 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3997 Error_ExpectingTypeName (expr);
4001 $$ = new ComposedCast ((Expression) $1, "*");
4003 | builtin_types STAR
4005 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
4009 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
4011 | local_variable_pointer_type STAR
4013 $$ = new ComposedCast ((Expression) $1, "*");
4017 local_variable_declaration
4018 : local_variable_type variable_declarators
4021 $$ = new DictionaryEntry ($1, $2);
4025 | local_variable_pointer_type opt_rank_specifier variable_declarators
4030 if ((string) $2 == "")
4031 t = (Expression) $1;
4033 t = new ComposedCast ((Expression) $1, (string) $2);
4034 $$ = new DictionaryEntry (t, $3);
4040 local_constant_declaration
4041 : CONST local_variable_type constant_declarators
4044 $$ = new DictionaryEntry ($2, $3);
4050 expression_statement
4051 : statement_expression SEMICOLON { $$ = $1; }
4055 // We have to do the wrapping here and not in the case above,
4056 // because statement_expression is used for example in for_statement
4058 statement_expression
4061 Expression expr = (Expression) $1;
4062 ExpressionStatement s = expr as ExpressionStatement;
4064 expr.Error_InvalidExpressionStatement ();
4067 $$ = new StatementExpression (s);
4071 Report.Error (1002, GetLocation ($1), "Expecting `;'");
4076 object_creation_expression
4077 : object_or_delegate_creation_expression
4078 { note ("complain if this is a delegate maybe?"); }
4087 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
4090 Location l = (Location) $1;
4092 $$ = new If ((Expression) $3, (Statement) $5, l);
4094 // FIXME: location for warning should be loc property of $5.
4095 if ($5 == EmptyStatement.Value)
4096 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4099 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
4100 embedded_statement ELSE embedded_statement
4102 Location l = (Location) $1;
4104 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
4106 // FIXME: location for warning should be loc property of $5 and $7.
4107 if ($5 == EmptyStatement.Value)
4108 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4109 if ($7 == EmptyStatement.Value)
4110 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4115 : SWITCH OPEN_PARENS
4117 if (switch_stack == null)
4118 switch_stack = new Stack (2);
4119 switch_stack.Push (current_block);
4121 expression CLOSE_PARENS
4124 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
4125 current_block = (Block) switch_stack.Pop ();
4141 Report.Warning (1522, 1, lexer.Location, "Empty switch block");
4142 $$ = new ArrayList ();
4150 ArrayList sections = new ArrayList (4);
4155 | switch_sections switch_section
4157 ArrayList sections = (ArrayList) $1;
4167 current_block = current_block.CreateSwitchBlock (lexer.Location);
4171 $$ = new SwitchSection ((ArrayList) $1, current_block.Explicit);
4178 ArrayList labels = new ArrayList (4);
4183 | switch_labels switch_label
4185 ArrayList labels = (ArrayList) ($1);
4193 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
4194 | DEFAULT COLON { $$ = new SwitchLabel (null, (Location) $1); }
4197 1523, GetLocation ($1),
4198 "The keyword case or default must precede code in switch block");
4210 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
4212 Location l = (Location) $1;
4213 $$ = new While ((Expression) $3, (Statement) $5, l);
4218 : DO embedded_statement
4219 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
4221 Location l = (Location) $1;
4223 $$ = new Do ((Statement) $2, (Expression) $5, l);
4229 opt_for_initializer SEMICOLON
4231 Location l = lexer.Location;
4233 Block assign_block = current_block;
4235 if ($3 is DictionaryEntry){
4236 DictionaryEntry de = (DictionaryEntry) $3;
4238 Expression type = (Expression) de.Key;
4239 ArrayList var_declarators = (ArrayList) de.Value;
4241 foreach (VariableDeclaration decl in var_declarators){
4245 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4249 Expression expr = decl.expression_or_array_initializer;
4251 LocalVariableReference var;
4252 var = new LocalVariableReference (assign_block, decl.identifier, l);
4255 Assign a = new Assign (var, expr, decl.Location);
4257 assign_block.AddStatement (new StatementExpression (a));
4261 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4262 // This can be referred to as $5 below.
4268 opt_for_condition SEMICOLON
4269 opt_for_iterator CLOSE_PARENS
4272 Location l = (Location) $1;
4274 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4276 current_block.AddStatement (f);
4278 $$ = end_block (lexer.Location);
4283 : /* empty */ { $$ = EmptyStatement.Value; }
4288 : local_variable_declaration
4289 | statement_expression_list
4293 : /* empty */ { $$ = null; }
4294 | boolean_expression
4298 : /* empty */ { $$ = EmptyStatement.Value; }
4303 : statement_expression_list
4306 statement_expression_list
4307 : statement_expression
4309 // CHANGE: was `null'
4310 Statement s = (Statement) $1;
4311 Block b = new Block (current_block, s.loc, lexer.Location);
4316 | statement_expression_list COMMA statement_expression
4318 Block b = (Block) $1;
4320 b.AddStatement ((Statement) $3);
4326 : FOREACH open_parens type IN expression CLOSE_PARENS
4328 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4331 | FOREACH open_parens type IDENTIFIER IN
4332 expression CLOSE_PARENS
4334 start_block (lexer.Location);
4335 Block foreach_block = current_block;
4337 LocatedToken lt = (LocatedToken) $4;
4338 Location l = lt.Location;
4341 vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4343 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4345 // Get a writable reference to this read-only variable.
4347 // Note that the $$ here refers to the value of _this_ code block,
4348 // not the value of the LHS non-terminal. This can be referred to as $8 below.
4349 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
4356 LocalVariableReference v = (LocalVariableReference) $8;
4357 Location l = (Location) $1;
4360 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
4361 current_block.AddStatement (f);
4364 $$ = end_block (lexer.Location);
4370 | continue_statement
4380 $$ = new Break ((Location) $1);
4385 : CONTINUE SEMICOLON
4387 $$ = new Continue ((Location) $1);
4392 : GOTO IDENTIFIER SEMICOLON
4394 LocatedToken lt = (LocatedToken) $2;
4395 $$ = new Goto (lt.Value, lt.Location);
4397 | GOTO CASE constant_expression SEMICOLON
4399 $$ = new GotoCase ((Expression) $3, (Location) $1);
4401 | GOTO DEFAULT SEMICOLON
4403 $$ = new GotoDefault ((Location) $1);
4408 : RETURN opt_expression SEMICOLON
4410 $$ = new Return ((Expression) $2, (Location) $1);
4415 : THROW opt_expression SEMICOLON
4417 $$ = new Throw ((Expression) $2, (Location) $1);
4422 : IDENTIFIER RETURN expression SEMICOLON
4424 LocatedToken lt = (LocatedToken) $1;
4425 string s = lt.Value;
4427 Report.Error (1003, lt.Location, "; expected");
4430 if (RootContext.Version == LanguageVersion.ISO_1){
4431 Report.FeatureIsNotISO1 (lt.Location, "yield statement");
4434 if (anonymous_host == null){
4435 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4438 anonymous_host.SetYields ();
4439 $$ = new Yield ((Expression) $3, lt.Location);
4442 | IDENTIFIER RETURN SEMICOLON
4444 Report.Error (1627, (Location) $2, "Expression expected after yield return");
4447 | IDENTIFIER BREAK SEMICOLON
4449 LocatedToken lt = (LocatedToken) $1;
4450 string s = lt.Value;
4452 Report.Error (1003, lt.Location, "; expected");
4455 if (RootContext.Version == LanguageVersion.ISO_1){
4456 Report.FeatureIsNotISO1 (lt.Location, "yield statement");
4459 if (anonymous_host == null){
4460 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4463 anonymous_host.SetYields ();
4464 $$ = new YieldBreak (lt.Location);
4475 : TRY block catch_clauses
4479 ArrayList c = (ArrayList)$3;
4480 for (int i = 0; i < c.Count; ++i) {
4481 Catch cc = (Catch) c [i];
4483 if (i != c.Count - 1)
4484 Report.Error (1017, cc.loc, "Try statement already has an empty catch block");
4491 // Now s contains the list of specific catch clauses
4492 // and g contains the general one.
4494 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4496 | TRY block opt_catch_clauses FINALLY block
4499 ArrayList s = new ArrayList (4);
4500 ArrayList catch_list = (ArrayList) $3;
4502 if (catch_list != null){
4503 foreach (Catch cc in catch_list) {
4511 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4515 Report.Error (1524, (Location) $1, "Expected catch or finally");
4521 : /* empty */ { $$ = null; }
4528 ArrayList l = new ArrayList (4);
4533 | catch_clauses catch_clause
4535 ArrayList l = (ArrayList) $1;
4543 : /* empty */ { $$ = null; }
4548 : CATCH opt_catch_args
4550 Expression type = null;
4553 DictionaryEntry cc = (DictionaryEntry) $2;
4554 type = (Expression) cc.Key;
4555 LocatedToken lt = (LocatedToken) cc.Value;
4558 ArrayList one = new ArrayList (4);
4560 one.Add (new VariableDeclaration (lt, null));
4562 start_block (lexer.Location);
4563 current_block = declare_local_variables (type, one, lt.Location);
4567 Expression type = null;
4569 Block var_block = null;
4572 DictionaryEntry cc = (DictionaryEntry) $2;
4573 type = (Expression) cc.Key;
4574 LocatedToken lt = (LocatedToken) cc.Value;
4578 var_block = end_block (lexer.Location);
4582 $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
4587 : /* empty */ { $$ = null; }
4592 : open_parens type opt_identifier CLOSE_PARENS
4594 $$ = new DictionaryEntry ($2, $3);
4602 $$ = new Checked ((Block) $2);
4609 $$ = new Unchecked ((Block) $2);
4616 RootContext.CheckUnsafeOption ((Location) $1);
4618 $$ = new Unsafe ((Block) $3);
4624 type fixed_pointer_declarators
4627 ArrayList list = (ArrayList) $4;
4628 Expression type = (Expression) $3;
4629 Location l = (Location) $1;
4630 int top = list.Count;
4632 start_block (lexer.Location);
4634 for (int i = 0; i < top; i++){
4635 Pair p = (Pair) list [i];
4638 v = current_block.AddVariable (type, (string) p.First, l);
4642 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4650 Location l = (Location) $1;
4652 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4654 current_block.AddStatement (f);
4656 $$ = end_block (lexer.Location);
4660 fixed_pointer_declarators
4661 : fixed_pointer_declarator {
4662 ArrayList declarators = new ArrayList (4);
4664 declarators.Add ($1);
4667 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4669 ArrayList declarators = (ArrayList) $1;
4671 declarators.Add ($3);
4676 fixed_pointer_declarator
4677 : IDENTIFIER ASSIGN expression
4679 LocatedToken lt = (LocatedToken) $1;
4680 // FIXME: keep location
4681 $$ = new Pair (lt.Value, $3);
4685 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
4691 : LOCK OPEN_PARENS expression CLOSE_PARENS
4697 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
4702 : USING open_parens resource_acquisition CLOSE_PARENS
4704 start_block (lexer.Location);
4705 Block assign_block = current_block;
4707 if ($3 is DictionaryEntry){
4708 DictionaryEntry de = (DictionaryEntry) $3;
4709 Location l = (Location) $1;
4711 Expression type = (Expression) de.Key;
4712 ArrayList var_declarators = (ArrayList) de.Value;
4714 ArrayList vars = new ArrayList (4);
4716 foreach (VariableDeclaration decl in var_declarators){
4718 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4721 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
4723 Expression expr = decl.expression_or_array_initializer;
4725 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4728 LocalVariableReference var;
4730 // Get a writable reference to this read-only variable.
4731 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4733 // This is so that it is not a warning on using variables
4736 vars.Add (new DictionaryEntry (var, expr));
4738 // Assign a = new Assign (var, expr, decl.Location);
4739 // assign_block.AddStatement (new StatementExpression (a));
4742 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4743 // It can be referred to as $5 below.
4744 $$ = new DictionaryEntry (type, vars);
4751 Using u = new Using ($5, (Statement) $6, (Location) $1);
4752 current_block.AddStatement (u);
4753 $$ = end_block (lexer.Location);
4757 resource_acquisition
4758 : local_variable_declaration
4765 // A class used to pass around variable declarations and constants
4767 public class VariableDeclaration {
4768 public string identifier;
4769 public Expression expression_or_array_initializer;
4770 public Location Location;
4771 public Attributes OptAttributes;
4772 public string DocComment;
4774 public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
4776 this.identifier = lt.Value;
4777 if (eoai is ArrayList) {
4778 if (CSharpParser.current_array_type == null)
4779 Report.Error (622, lt.Location,
4780 "Can only use array initializer expressions to assign to array types. Try using a new expression instead.");
4781 this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
4783 this.expression_or_array_initializer = (Expression)eoai;
4785 this.Location = lt.Location;
4786 this.OptAttributes = opt_attrs;
4789 public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
4795 // A class used to hold info about an indexer declarator
4797 public class IndexerDeclaration {
4798 public Expression type;
4799 public MemberName interface_type;
4800 public Parameters param_list;
4801 public Location location;
4803 public IndexerDeclaration (Expression type, MemberName interface_type,
4804 Parameters param_list, Location loc)
4807 this.interface_type = interface_type;
4808 this.param_list = param_list;
4809 this.location = loc;
4814 // We use this when we do not have an object in advance that is an IAnonymousHost
4816 public class SimpleAnonymousHost : IAnonymousHost {
4817 public static readonly SimpleAnonymousHost Simple = new SimpleAnonymousHost ();
4820 ArrayList anonymous_methods;
4822 public static SimpleAnonymousHost GetSimple () {
4823 Simple.yields = false;
4824 Simple.anonymous_methods = null;
4828 public void SetYields ()
4833 public void AddAnonymousMethod (AnonymousMethodExpression anonymous)
4835 if (anonymous_methods == null)
4836 anonymous_methods = new ArrayList ();
4837 anonymous_methods.Add (anonymous);
4840 public void Propagate (IAnonymousHost real_host)
4843 real_host.SetYields ();
4844 if (anonymous_methods != null) {
4845 foreach (AnonymousMethodExpression ame in anonymous_methods)
4846 real_host.AddAnonymousMethod (ame);
4852 // A class used to hold info about an operator declarator
4854 public class OperatorDeclaration {
4855 public Operator.OpType optype;
4856 public Expression ret_type, arg1type, arg2type;
4857 public string arg1name, arg2name;
4858 public Location location;
4860 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4861 Expression arg1type, string arg1name,
4862 Expression arg2type, string arg2name, Location location)
4865 this.ret_type = ret_type;
4866 this.arg1type = arg1type;
4867 this.arg1name = arg1name;
4868 this.arg2type = arg2type;
4869 this.arg2name = arg2name;
4870 this.location = location;
4875 void Error_ExpectingTypeName (Expression expr)
4877 if (expr is Invocation){
4878 Report.Error (1002, expr.Location, "Expecting `;'");
4880 expr.Error_InvalidExpressionStatement ();
4884 public static void Error_ParameterModifierNotValid (Location loc)
4886 Report.Error (631, loc, "The modifiers `ref' and `out' are not valid in this context");
4889 static void Error_DuplicateParameterModifier (Location loc, Parameter.Modifier mod)
4891 Report.Error (1107, loc, "Duplicate parameter modifier `{0}'",
4892 Parameter.GetModifierSignature (mod));
4895 void push_current_class (TypeContainer tc, object partial_token)
4897 if (partial_token != null)
4898 current_container = current_container.AddPartial (tc);
4900 current_container = current_container.AddTypeContainer (tc);
4904 DeclSpace pop_current_class ()
4906 DeclSpace retval = current_class;
4908 current_class = current_class.Parent;
4909 current_container = current_class.PartialContainer;
4915 // Given the @class_name name, it creates a fully qualified name
4916 // based on the containing declaration space
4919 MakeName (MemberName class_name)
4921 Namespace ns = current_namespace.NS;
4923 if (current_container.Name.Length == 0){
4924 if (ns.Name.Length != 0)
4925 return new MemberName (ns.MemberName, class_name);
4929 return new MemberName (current_container.MemberName, class_name);
4933 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4935 Block implicit_block;
4936 ArrayList inits = null;
4939 // We use the `Used' property to check whether statements
4940 // have been added to the current block. If so, we need
4941 // to create another block to contain the new declaration
4942 // otherwise, as an optimization, we use the same block to
4943 // add the declaration.
4945 // FIXME: A further optimization is to check if the statements
4946 // that were added were added as part of the initialization
4947 // below. In which case, no other statements have been executed
4948 // and we might be able to reduce the number of blocks for
4949 // situations like this:
4951 // int j = 1; int k = j + 1;
4953 if (current_block.Used)
4954 implicit_block = new Block (current_block, loc, Location.Null);
4956 implicit_block = current_block;
4958 foreach (VariableDeclaration decl in variable_declarators){
4960 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
4961 if (decl.expression_or_array_initializer != null){
4963 inits = new ArrayList (4);
4970 return implicit_block;
4972 foreach (VariableDeclaration decl in inits){
4974 Expression expr = decl.expression_or_array_initializer;
4976 LocalVariableReference var;
4977 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4979 assign = new Assign (var, expr, decl.Location);
4981 implicit_block.AddStatement (new StatementExpression (assign));
4984 return implicit_block;
4987 Block declare_local_constants (Expression type, ArrayList declarators)
4989 Block implicit_block;
4991 if (current_block.Used)
4992 implicit_block = new Block (current_block);
4994 implicit_block = current_block;
4996 foreach (VariableDeclaration decl in declarators){
4997 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
5000 return implicit_block;
5003 string CheckAttributeTarget (string a, Location l)
5006 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
5010 Report.Warning (658, 1, l,
5011 "`{0}' is invalid attribute target. All attributes in this attribute section will be ignored", a);
5012 return string.Empty;
5015 void CheckUnaryOperator (Operator.OpType op, Location l)
5019 case Operator.OpType.LogicalNot:
5020 case Operator.OpType.OnesComplement:
5021 case Operator.OpType.Increment:
5022 case Operator.OpType.Decrement:
5023 case Operator.OpType.True:
5024 case Operator.OpType.False:
5025 case Operator.OpType.Addition:
5026 case Operator.OpType.Subtraction:
5031 Report.Error (1019, l, "Overloadable unary operator expected");
5037 void CheckBinaryOperator (Operator.OpType op, Location l)
5041 case Operator.OpType.Addition:
5042 case Operator.OpType.Subtraction:
5043 case Operator.OpType.Multiply:
5044 case Operator.OpType.Division:
5045 case Operator.OpType.Modulus:
5046 case Operator.OpType.BitwiseAnd:
5047 case Operator.OpType.BitwiseOr:
5048 case Operator.OpType.ExclusiveOr:
5049 case Operator.OpType.LeftShift:
5050 case Operator.OpType.RightShift:
5051 case Operator.OpType.Equality:
5052 case Operator.OpType.Inequality:
5053 case Operator.OpType.GreaterThan:
5054 case Operator.OpType.LessThan:
5055 case Operator.OpType.GreaterThanOrEqual:
5056 case Operator.OpType.LessThanOrEqual:
5060 Report.Error (1020, l, "Overloadable binary operator expected");
5066 void syntax_error (Location l, string msg)
5068 Report.Error (1003, l, "Syntax error, " + msg);
5071 void note (string s)
5073 // Used to put annotations
5078 public Tokenizer Lexer {
5084 static CSharpParser ()
5086 oob_stack = new Stack ();
5089 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
5091 this.name = file.Name;
5093 current_namespace = new NamespaceEntry (null, file, null);
5094 current_class = current_namespace.SlaveDeclSpace;
5095 current_container = current_class.PartialContainer; // == RootContest.ToplevelTypes
5097 lexer = new Tokenizer (reader, file, defines);
5100 public void parse ()
5102 int errors = Report.Errors;
5104 if (yacc_verbose_flag > 1)
5105 yyparse (lexer, new yydebug.yyDebugSimple ());
5108 Tokenizer tokenizer = lexer as Tokenizer;
5109 tokenizer.cleanup ();
5110 } catch (Exception e){
5112 // Removed for production use, use parser verbose to get the output.
5114 // Console.WriteLine (e);
5115 if (Report.Errors == errors)
5116 Report.Error (-25, lexer.Location, "Parsing error");
5117 if (yacc_verbose_flag > 0)
5118 Console.WriteLine (e);
5121 if (RootContext.ToplevelTypes.NamespaceEntry != null)
5122 throw new InternalErrorException ("who set it?");
5125 static void CheckToken (int error, int yyToken, string msg, Location loc)
5127 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
5128 Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ());
5130 Report.Error (error, loc, msg);
5133 void CheckIdentifierToken (int yyToken, Location loc)
5135 CheckToken (1041, yyToken, "Identifier expected", loc);
5138 string ConsumeStoredComment ()
5140 string s = tmpComment;
5142 Lexer.doc_state = XmlCommentState.Allowed;
5146 Location GetLocation (object obj)
5148 if (obj is MemberCore)
5149 return ((MemberCore) obj).Location;
5150 if (obj is MemberName)
5151 return ((MemberName) obj).Location;
5152 if (obj is LocatedToken)
5153 return ((LocatedToken) obj).Location;
5154 if (obj is Location)
5155 return (Location) obj;
5156 return lexer.Location;
5159 static GenericMethod current_generic_method = null;
5161 void start_block (Location loc)
5163 if (current_block == null || parsing_anonymous_method) {
5164 current_block = new ToplevelBlock (current_block, current_local_parameters, current_generic_method, loc);
5165 parsing_anonymous_method = false;
5167 current_block = new ExplicitBlock (current_block, loc, Location.Null);
5172 end_block (Location loc)
5174 Block retval = current_block.Explicit;
5175 retval.SetEndLocation (loc);
5176 current_block = retval.Parent;
5181 start_anonymous (bool lambda, Parameters parameters, Location loc)
5183 oob_stack.Push (current_anonymous_method);
5184 oob_stack.Push (current_local_parameters);
5186 current_local_parameters = parameters;
5188 ToplevelBlock top_current_block = current_block == null ? null : current_block.Toplevel;
5190 current_anonymous_method = lambda
5191 ? new LambdaExpression (
5192 current_anonymous_method, current_generic_method, current_container,
5193 parameters, top_current_block, loc)
5194 : new AnonymousMethodExpression (
5195 current_anonymous_method, current_generic_method, current_container,
5196 parameters, top_current_block, loc);
5198 // Force the next block to be created as a ToplevelBlock
5199 parsing_anonymous_method = true;
5203 * Completes the anonymous method processing, if lambda_expr is null, this
5204 * means that we have a Statement instead of an Expression embedded
5206 AnonymousMethodExpression
5207 end_anonymous (ToplevelBlock anon_block, Location loc)
5209 AnonymousMethodExpression retval;
5211 if (RootContext.Version == LanguageVersion.ISO_1){
5212 Report.FeatureIsNotISO1 (loc, "anonymous methods");
5215 current_anonymous_method.Block = anon_block;
5216 if ((anonymous_host != null) && (current_anonymous_method.Parent == null))
5217 anonymous_host.AddAnonymousMethod (current_anonymous_method);
5219 retval = current_anonymous_method;
5222 current_local_parameters = (Parameters) oob_stack.Pop ();
5223 current_anonymous_method = (AnonymousMethodExpression) oob_stack.Pop ();