3 // cs-parser.jay: The Parser for the C# compiler
5 // Authors: Miguel de Icaza (miguel@gnu.org)
6 // Ravi Pratap (ravi@ximian.com)
8 // Licensed under the terms of the GNU GPL
10 // (C) 2001 Ximian, Inc (http://www.ximian.com)
11 // (C) 2004 Novell, Inc
14 // (1) Figure out why error productions dont work. `type-declaration' is a
15 // great spot to put an `error' because you can reproduce it with this input:
18 // Possible optimization:
19 // Run memory profiler with parsing only, and consider dropping
20 // arraylists where not needed. Some pieces can use linked lists.
28 using System.Collections;
33 public class CSharpParser {
34 NamespaceEntry current_namespace;
35 TypeContainer current_container;
36 TypeContainer current_class;
38 IIteratorContainer iterator_container;
41 /// Current block is used to add statements as we find
44 Block current_block, top_current_block;
46 Delegate current_delegate;
49 /// This is used by the unary_expression code to resolve
50 /// a name against a parameter.
52 Parameters current_local_parameters;
55 /// Using during property parsing to describe the implicit
56 /// value parameter that is passed to the "set" and "get"accesor
57 /// methods (properties and indexers).
59 Expression implicit_value_parameter_type;
60 Parameters indexer_parameters;
63 /// Used to determine if we are parsing the get/set pair
64 /// of an indexer or a property
69 /// An out-of-band stack.
78 static public int yacc_verbose_flag;
80 // Name of the file we are parsing
89 /// Temporary Xml documentation cache.
90 /// For enum types, we need one more temporary store.
93 string enumTypeComment;
95 /// Current attribute target
96 string current_attr_target;
98 /// assembly and module attribute definitions are enabled
99 bool global_attrs_enabled = true;
100 bool has_get, has_set;
105 %token NONE /* This token is never returned by our lexer */
106 %token ERROR // This is used not by the parser, but by the tokenizer.
110 *These are the C# keywords
197 /* C# keywords which are not really keywords */
203 /* C# single character operators/punctuation. */
204 %token OPEN_BRACE "{"
205 %token CLOSE_BRACE "}"
206 %token OPEN_BRACKET "["
207 %token CLOSE_BRACKET "]"
208 %token OPEN_PARENS "("
209 %token CLOSE_PARENS ")"
221 %token OP_GENERICS_LT "<"
223 %token OP_GENERICS_GT ">"
224 %token BITWISE_AND "&"
225 %token BITWISE_OR "|"
232 /* C# multi-character operators. */
233 %token DOUBLE_COLON "::"
236 %token OP_SHIFT_LEFT "<<"
237 %token OP_SHIFT_RIGHT ">>"
244 %token OP_MULT_ASSIGN "*="
245 %token OP_DIV_ASSIGN "/="
246 %token OP_MOD_ASSIGN "%="
247 %token OP_ADD_ASSIGN "+="
248 %token OP_SUB_ASSIGN "-="
249 %token OP_SHIFT_LEFT_ASSIGN "<<="
250 %token OP_SHIFT_RIGHT_ASSIGN ">>="
251 %token OP_AND_ASSIGN "&="
252 %token OP_XOR_ASSIGN "^="
253 %token OP_OR_ASSIGN "|="
257 %token LITERAL_INTEGER "int literal"
258 %token LITERAL_FLOAT "float literal"
259 %token LITERAL_DOUBLE "double literal"
260 %token LITERAL_DECIMAL "decimal literal"
261 %token LITERAL_CHARACTER "character literal"
262 %token LITERAL_STRING "string literal"
265 %token CLOSE_PARENS_CAST
266 %token CLOSE_PARENS_NO_CAST
267 %token CLOSE_PARENS_OPEN_PARENS
268 %token CLOSE_PARENS_MINUS
269 %token DEFAULT_OPEN_PARENS
270 %token GENERIC_DIMENSION
272 /* Add precedence rules to solve dangling else s/r conflict */
281 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
283 %left STAR DIV PERCENT
284 %right BANG CARRET UMINUS
285 %nonassoc OP_INC OP_DEC
287 %left OPEN_BRACKET OPEN_BRACE
291 %start compilation_unit
295 : outer_declarations opt_EOF
296 | outer_declarations global_attributes opt_EOF
297 | global_attributes opt_EOF
298 | opt_EOF /* allow empty files */
304 Lexer.check_incorrect_doc_comment ();
308 Lexer.check_incorrect_doc_comment ();
314 | outer_declarations outer_declaration
319 | namespace_member_declaration
324 | using_directives using_directive
328 : using_alias_directive
330 if (RootContext.Documentation != null)
331 Lexer.doc_state = XmlCommentState.Allowed;
333 | using_namespace_directive
335 if (RootContext.Documentation != null)
336 Lexer.doc_state = XmlCommentState.Allowed;
340 using_alias_directive
341 : USING IDENTIFIER ASSIGN
342 namespace_or_type_name SEMICOLON
344 LocatedToken lt = (LocatedToken) $2;
345 current_namespace.UsingAlias (lt.Value, (MemberName) $4, (Location) $1);
348 CheckIdentifierToken (yyToken, GetLocation ($2));
352 using_namespace_directive
353 : USING namespace_name SEMICOLON
355 current_namespace.Using ((MemberName) $2, (Location) $1);
360 // Strictly speaking, namespaces don't have attributes but
361 // we parse global attributes along with namespace declarations and then
364 namespace_declaration
365 : opt_attributes NAMESPACE namespace_or_type_name
367 MemberName name = (MemberName) $3;
370 Report.Error(1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
373 if (name.TypeArguments != null)
374 syntax_error (lexer.Location, "namespace name expected");
376 current_namespace = new NamespaceEntry (
377 current_namespace, file, name.GetName (), name.Location);
379 namespace_body opt_semicolon
381 current_namespace = current_namespace.Parent;
396 : namespace_or_type_name {
397 MemberName name = (MemberName) $1;
399 if (name.TypeArguments != null)
400 syntax_error (lexer.Location, "namespace name expected");
409 if (RootContext.Documentation != null)
410 Lexer.doc_state = XmlCommentState.Allowed;
413 opt_namespace_member_declarations
422 opt_namespace_member_declarations
424 | namespace_member_declarations
427 namespace_member_declarations
428 : namespace_member_declaration
429 | namespace_member_declarations namespace_member_declaration
432 namespace_member_declaration
436 DeclSpace ds = (DeclSpace)$1;
438 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
439 Report.Error (1527, ds.Location,
440 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
443 current_namespace.DeclarationFound = true;
445 | namespace_declaration {
446 current_namespace.DeclarationFound = true;
449 | field_declaration {
450 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
452 | method_declaration {
453 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
460 | interface_declaration
462 | delegate_declaration
464 // Enable this when we have handled all errors, because this acts as a generic fallback
467 // Console.WriteLine ("Token=" + yyToken);
468 // Report.Error (1518, GetLocation ($1), "Expected class, struct, interface, enum or delegate");
480 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
488 global_attrs_enabled = false;
493 global_attrs_enabled = false;
502 ArrayList sect = (ArrayList) $1;
504 if (global_attrs_enabled) {
505 if (current_attr_target == "module") {
506 CodeGen.Module.AddAttributes (sect);
508 } else if (current_attr_target != null && current_attr_target.Length > 0) {
509 CodeGen.Assembly.AddAttributes (sect);
512 $$ = new Attributes (sect);
515 if (RootContext.Documentation != null) {
516 Lexer.check_incorrect_doc_comment ();
518 XmlCommentState.Allowed;
522 $$ = new Attributes (sect);
524 current_attr_target = null;
526 | attribute_sections attribute_section
528 Attributes attrs = $1 as Attributes;
529 ArrayList sect = (ArrayList) $2;
531 if (global_attrs_enabled) {
532 if (current_attr_target == "module") {
533 CodeGen.Module.AddAttributes (sect);
535 } else if (current_attr_target == "assembly") {
536 CodeGen.Assembly.AddAttributes (sect);
540 attrs = new Attributes (sect);
542 attrs.AddAttributes (sect);
546 attrs = new Attributes (sect);
548 attrs.AddAttributes (sect);
551 current_attr_target = null;
556 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
560 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
566 attribute_target_specifier
567 : attribute_target COLON
569 current_attr_target = (string)$1;
577 LocatedToken lt = (LocatedToken) $1;
578 CheckAttributeTarget (lt.Value, lt.Location);
579 $$ = lt.Value; // Location won't be required anymore.
581 | EVENT { $$ = "event"; }
582 | RETURN { $$ = "return"; }
588 ArrayList attrs = new ArrayList (4);
594 | attribute_list COMMA attribute
596 ArrayList attrs = (ArrayList) $1;
604 : attribute_name opt_attribute_arguments
606 MemberName mname = (MemberName) $1;
607 if (mname.IsGeneric) {
608 Report.Error (404, lexer.Location,
609 "'<' unexpected: attributes cannot be generic");
612 ArrayList arguments = (ArrayList) $2;
613 MemberName left = mname.Left;
614 string identifier = mname.Name;
616 Expression left_expr = left == null ? null : left.GetTypeExpression ();
618 if (current_attr_target == "assembly" || current_attr_target == "module")
619 // FIXME: supply "nameEscaped" parameter here.
620 $$ = new GlobalAttribute (current_class, current_attr_target,
621 left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
623 $$ = new Attribute (current_attr_target, left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
628 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
631 opt_attribute_arguments
632 : /* empty */ { $$ = null; }
633 | OPEN_PARENS attribute_arguments CLOSE_PARENS
641 : opt_positional_argument_list
646 ArrayList args = new ArrayList (4);
652 | positional_argument_list COMMA named_argument_list
654 ArrayList args = new ArrayList (4);
660 | named_argument_list
662 ArrayList args = new ArrayList (4);
671 opt_positional_argument_list
672 : /* empty */ { $$ = null; }
673 | positional_argument_list
676 positional_argument_list
679 ArrayList args = new ArrayList (4);
680 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
684 | positional_argument_list COMMA expression
686 ArrayList args = (ArrayList) $1;
687 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
696 ArrayList args = new ArrayList (4);
701 | named_argument_list COMMA named_argument
703 ArrayList args = (ArrayList) $1;
708 | named_argument_list COMMA expression
710 Report.Error (1016, ((Expression) $3).Location, "Named attribute argument expected");
716 : IDENTIFIER ASSIGN expression
718 // FIXME: keep location
719 $$ = new DictionaryEntry (
720 ((LocatedToken) $1).Value,
721 new Argument ((Expression) $3, Argument.AType.Expression));
727 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
730 opt_class_member_declarations
732 | class_member_declarations
735 class_member_declarations
736 : class_member_declaration
737 | class_member_declarations
738 class_member_declaration
741 class_member_declaration
742 : constant_declaration // done
743 | field_declaration // done
744 | method_declaration // done
745 | property_declaration // done
746 | event_declaration // done
747 | indexer_declaration // done
748 | operator_declaration // done
749 | constructor_declaration // done
750 | destructor_declaration // done
760 lexer.ConstraintsParsing = true;
764 MemberName name = MakeName ((MemberName) $6);
766 ClassPart part = PartialContainer.CreatePart (
767 current_namespace, current_class, name, (int) $2,
768 (Attributes) $1, Kind.Struct, (Location) $3);
770 current_container = part.PartialContainer;
771 current_class = part;
773 current_class = new Struct (
774 current_namespace, current_class, name, (int) $2,
777 current_container.AddClassOrStruct (current_class);
778 current_container = current_class;
779 RootContext.Tree.RecordDecl (current_namespace.NS, name, current_class);
783 opt_type_parameter_constraints_clauses
785 lexer.ConstraintsParsing = false;
788 current_class.Bases = (ArrayList) $8;
790 current_class.SetParameterInfo ((ArrayList) $9);
792 if (RootContext.Documentation != null)
793 current_class.DocComment = Lexer.consume_doc_comment ();
797 if (RootContext.Documentation != null)
798 Lexer.doc_state = XmlCommentState.Allowed;
802 $$ = pop_current_class ();
804 | opt_attributes opt_modifiers opt_partial STRUCT error {
805 CheckIdentifierToken (yyToken, GetLocation ($5));
812 if (RootContext.Documentation != null)
813 Lexer.doc_state = XmlCommentState.Allowed;
815 opt_struct_member_declarations CLOSE_BRACE
818 opt_struct_member_declarations
820 | struct_member_declarations
823 struct_member_declarations
824 : struct_member_declaration
825 | struct_member_declarations struct_member_declaration
828 struct_member_declaration
829 : constant_declaration
832 | property_declaration
834 | indexer_declaration
835 | operator_declaration
836 | constructor_declaration
840 * This is only included so we can flag error 575:
841 * destructors only allowed on class types
843 | destructor_declaration
854 int modflags = (int) $2;
855 foreach (VariableDeclaration constant in (ArrayList) $5){
856 Location l = constant.Location;
857 if ((modflags & Modifiers.STATIC) != 0) {
858 Report.Error (504, l, "The constant `{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
862 Const c = new Const (
863 current_class, (Expression) $4, (string) constant.identifier,
864 (Expression) constant.expression_or_array_initializer, modflags,
867 if (RootContext.Documentation != null) {
868 c.DocComment = Lexer.consume_doc_comment ();
869 Lexer.doc_state = XmlCommentState.Allowed;
871 current_container.AddConstant (c);
877 : constant_declarator
879 ArrayList constants = new ArrayList (4);
884 | constant_declarators COMMA constant_declarator
887 ArrayList constants = (ArrayList) $1;
894 : IDENTIFIER ASSIGN constant_expression
896 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
900 // A const field requires a value to be provided
901 Report.Error (145, ((LocatedToken) $1).Location, "A const field requires a value to be provided");
913 Expression type = (Expression) $3;
916 foreach (VariableDeclaration var in (ArrayList) $4){
917 Field field = new Field (current_class, type, mod, var.identifier,
918 var.expression_or_array_initializer,
919 (Attributes) $1, var.Location);
921 if (RootContext.Documentation != null) {
922 field.DocComment = Lexer.consume_doc_comment ();
923 Lexer.doc_state = XmlCommentState.Allowed;
925 current_container.AddField (field);
926 $$ = field; // FIXME: might be better if it points to the top item
933 fixed_variable_declarators
936 Expression type = (Expression) $4;
939 foreach (VariableDeclaration var in (ArrayList) $5) {
940 FixedField field = new FixedField (current_class, type, mod, var.identifier,
941 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
943 if (RootContext.Documentation != null) {
944 field.DocComment = Lexer.consume_doc_comment ();
945 Lexer.doc_state = XmlCommentState.Allowed;
947 current_container.AddField (field);
948 $$ = field; // FIXME: might be better if it points to the top item
956 Report.Error (670, (Location) $3, "Fields cannot have void type");
960 fixed_variable_declarators
961 : fixed_variable_declarator
963 ArrayList decl = new ArrayList (2);
967 | fixed_variable_declarators COMMA fixed_variable_declarator
969 ArrayList decls = (ArrayList) $1;
975 fixed_variable_declarator
976 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
978 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
980 | IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
982 Report.Error (443, lexer.Location, "Value or constant expected");
983 $$ = new VariableDeclaration ((LocatedToken) $1, null);
988 : variable_declarator
990 ArrayList decl = new ArrayList (4);
995 | variable_declarators COMMA variable_declarator
997 ArrayList decls = (ArrayList) $1;
1004 : IDENTIFIER ASSIGN variable_initializer
1006 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1010 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1012 | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
1014 Report.Error (650, ((LocatedToken) $1).Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
1015 "To declare a fixed size buffer field, use the fixed keyword before the field type");
1020 variable_initializer
1029 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
1031 $$ = new StackAlloc ((Expression) $2, (Expression) $4, (Location) $1);
1035 Report.Error (1575, (Location) $1, "A stackalloc expression requires [] after type");
1042 iterator_container = (IIteratorContainer) $1;
1043 if (RootContext.Documentation != null)
1044 Lexer.doc_state = XmlCommentState.NotAllowed;
1048 Method method = (Method) $1;
1049 method.Block = (ToplevelBlock) $3;
1050 current_container.AddMethod (method);
1052 current_local_parameters = null;
1053 iterator_container = null;
1055 if (RootContext.Documentation != null)
1056 Lexer.doc_state = XmlCommentState.Allowed;
1069 Report.Error (1585, lexer.Location,
1070 "Member modifier `{0}' must precede the member type and name",
1071 Modifiers.Name (i));
1082 type namespace_or_type_name
1083 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1085 lexer.ConstraintsParsing = true;
1087 opt_type_parameter_constraints_clauses
1089 lexer.ConstraintsParsing = false;
1091 MemberName name = (MemberName) $4;
1093 if ($9 != null && name.TypeArguments == null)
1094 Report.Error (80, lexer.Location,
1095 "Contraints are not allowed on non-generic declarations");
1099 GenericMethod generic = null;
1100 if (name.TypeArguments != null) {
1101 generic = new GenericMethod (current_namespace, current_class, name);
1103 generic.SetParameterInfo ((ArrayList) $9);
1106 method = new Method (current_class, generic, (Expression) $3, (int) $2, false,
1107 name, (Parameters) $6, (Attributes) $1);
1109 current_local_parameters = (Parameters) $6;
1111 if (RootContext.Documentation != null)
1112 method.DocComment = Lexer.consume_doc_comment ();
1118 VOID namespace_or_type_name
1119 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1121 lexer.ConstraintsParsing = true;
1123 opt_type_parameter_constraints_clauses
1125 lexer.ConstraintsParsing = false;
1127 MemberName name = (MemberName) $4;
1129 if ($9 != null && name.TypeArguments == null)
1130 Report.Error (80, lexer.Location,
1131 "Contraints are not allowed on non-generic declarations");
1134 GenericMethod generic = null;
1135 if (name.TypeArguments != null) {
1136 generic = new GenericMethod (current_namespace, current_class, name);
1138 generic.SetParameterInfo ((ArrayList) $9);
1141 method = new Method (current_class, generic, TypeManager.system_void_expr,
1142 (int) $2, false, name, (Parameters) $6, (Attributes) $1);
1144 current_local_parameters = (Parameters) $6;
1146 if (RootContext.Documentation != null)
1147 method.DocComment = Lexer.consume_doc_comment ();
1154 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1156 MemberName name = (MemberName) $5;
1157 Report.Error (1585, name.Location,
1158 "Member modifier `{0}' must precede the member type and name", Modifiers.Name ((int) $4));
1160 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1161 0, false, name, (Parameters) $6, (Attributes) $1);
1163 current_local_parameters = (Parameters) $6;
1165 if (RootContext.Documentation != null)
1166 method.DocComment = Lexer.consume_doc_comment ();
1174 | SEMICOLON { $$ = null; }
1177 opt_formal_parameter_list
1178 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1179 | formal_parameter_list
1182 formal_parameter_list
1185 ArrayList pars_list = (ArrayList) $1;
1187 Parameter [] pars = new Parameter [pars_list.Count];
1188 pars_list.CopyTo (pars);
1190 $$ = new Parameters (pars, null);
1192 | fixed_parameters COMMA parameter_array
1194 ArrayList pars_list = (ArrayList) $1;
1196 Parameter [] pars = new Parameter [pars_list.Count];
1197 pars_list.CopyTo (pars);
1199 $$ = new Parameters (pars, (Parameter) $3);
1201 | fixed_parameters COMMA ARGLIST
1203 ArrayList pars_list = (ArrayList) $1;
1205 Parameter [] pars = new Parameter [pars_list.Count];
1206 pars_list.CopyTo (pars);
1208 $$ = new Parameters (pars, true);
1210 | parameter_array COMMA fixed_parameters
1213 Report.Error (231, ((Parameter) $1).Location, "A params parameter must be the last parameter in a formal parameter list");
1216 | ARGLIST COMMA fixed_parameters
1218 Report.Error (257, (Location) $1, "An __arglist parameter must be the last parameter in a formal parameter list");
1223 $$ = new Parameters (null, (Parameter) $1);
1227 $$ = new Parameters (null, true);
1234 ArrayList pars = new ArrayList (4);
1239 | fixed_parameters COMMA fixed_parameter
1241 ArrayList pars = (ArrayList) $1;
1250 opt_parameter_modifier
1254 LocatedToken lt = (LocatedToken) $4;
1255 $$ = new Parameter ((Expression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1258 opt_parameter_modifier
1260 IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1262 LocatedToken lt = (LocatedToken) $4;
1263 Report.Error (1552, lt.Location, "Array type specifier, [], must appear before parameter name");
1267 opt_parameter_modifier
1270 Report.Error (1001, GetLocation ($3), "Identifier expected");
1274 opt_parameter_modifier
1277 CheckIdentifierToken (yyToken, GetLocation ($4));
1281 opt_parameter_modifier
1287 LocatedToken lt = (LocatedToken) $4;
1288 Report.Error (241, lt.Location, "Default parameter specifiers are not permitted");
1293 opt_parameter_modifier
1294 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1295 | parameter_modifier
1299 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1300 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1304 : opt_attributes PARAMS type IDENTIFIER
1306 LocatedToken lt = (LocatedToken) $4;
1307 $$ = new Parameter ((Expression) $3, lt.Value, Parameter.Modifier.PARAMS, (Attributes) $1, lt.Location);
1308 note ("type must be a single-dimension array type");
1310 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1312 Report.Error (1611, (Location) $2, "The params parameter cannot be declared as ref or out");
1315 | opt_attributes PARAMS type error {
1316 CheckIdentifierToken (yyToken, GetLocation ($4));
1321 property_declaration
1325 namespace_or_type_name
1327 if (RootContext.Documentation != null)
1328 tmpComment = Lexer.consume_doc_comment ();
1332 implicit_value_parameter_type = (Expression) $3;
1334 lexer.PropertyParsing = true;
1336 accessor_declarations
1338 lexer.PropertyParsing = false;
1339 has_get = has_set = false;
1347 Pair pair = (Pair) $8;
1348 Accessor get_block = (Accessor) pair.First;
1349 Accessor set_block = (Accessor) pair.Second;
1351 MemberName name = (MemberName) $4;
1353 if (name.TypeArguments != null)
1354 syntax_error (lexer.Location, "a property can't have type arguments");
1356 prop = new Property (current_class, (Expression) $3, (int) $2, false,
1357 name, (Attributes) $1, get_block, set_block);
1359 current_container.AddProperty (prop);
1360 implicit_value_parameter_type = null;
1362 if (RootContext.Documentation != null)
1363 prop.DocComment = ConsumeStoredComment ();
1368 accessor_declarations
1369 : get_accessor_declaration
1371 $$ = new Pair ($1, null);
1373 | get_accessor_declaration accessor_declarations
1375 Pair pair = (Pair) $2;
1379 | set_accessor_declaration
1381 $$ = new Pair (null, $1);
1383 | set_accessor_declaration accessor_declarations
1385 Pair pair = (Pair) $2;
1391 Report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1396 get_accessor_declaration
1397 : opt_attributes opt_modifiers GET
1399 // If this is not the case, then current_local_parameters has already
1400 // been set in indexer_declaration
1401 if (parsing_indexer == false)
1402 current_local_parameters = null;
1404 current_local_parameters = indexer_parameters;
1405 lexer.PropertyParsing = false;
1407 iterator_container = SimpleIteratorContainer.GetSimple ();
1412 Report.Error (1007, (Location) $3, "Property accessor already defined");
1415 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1417 current_local_parameters = null;
1418 lexer.PropertyParsing = true;
1420 if (SimpleIteratorContainer.Simple.Yields)
1421 accessor.SetYields ();
1423 iterator_container = null;
1425 if (RootContext.Documentation != null)
1426 if (Lexer.doc_state == XmlCommentState.Error)
1427 Lexer.doc_state = XmlCommentState.NotAllowed;
1433 set_accessor_declaration
1434 : opt_attributes opt_modifiers SET
1437 Parameter implicit_value_parameter = new Parameter (
1438 implicit_value_parameter_type, "value",
1439 Parameter.Modifier.NONE, null, (Location) $3);
1441 if (parsing_indexer == false) {
1442 args = new Parameter [1];
1443 args [0] = implicit_value_parameter;
1444 current_local_parameters = new Parameters (args, null);
1446 Parameter [] fpars = indexer_parameters.FixedParameters;
1449 int count = fpars.Length;
1451 args = new Parameter [count + 1];
1452 fpars.CopyTo (args, 0);
1453 args [count] = implicit_value_parameter;
1456 current_local_parameters = new Parameters (
1457 args, indexer_parameters.ArrayParameter);
1460 lexer.PropertyParsing = false;
1462 iterator_container = SimpleIteratorContainer.GetSimple ();
1467 Report.Error (1007, ((LocatedToken) $3).Location, "Property accessor already defined");
1470 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1472 current_local_parameters = null;
1473 lexer.PropertyParsing = true;
1475 if (SimpleIteratorContainer.Simple.Yields)
1476 accessor.SetYields ();
1478 iterator_container = null;
1480 if (RootContext.Documentation != null
1481 && Lexer.doc_state == XmlCommentState.Error)
1482 Lexer.doc_state = XmlCommentState.NotAllowed;
1490 | SEMICOLON { $$ = null; }
1493 interface_declaration
1499 lexer.ConstraintsParsing = true;
1503 MemberName name = MakeName ((MemberName) $6);
1506 ClassPart part = PartialContainer.CreatePart (
1507 current_namespace, current_class, name, (int) $2,
1508 (Attributes) $1, Kind.Interface, (Location) $3);
1510 current_container = part.PartialContainer;
1511 current_class = part;
1513 current_class = new Interface (
1514 current_namespace, current_class, name, (int) $2,
1517 current_container.AddInterface (current_class);
1518 current_container = current_class;
1519 RootContext.Tree.RecordDecl (current_namespace.NS, name, current_class);
1523 opt_type_parameter_constraints_clauses
1525 lexer.ConstraintsParsing = false;
1528 current_class.Bases = (ArrayList) $8;
1530 current_class.SetParameterInfo ((ArrayList) $9);
1532 if (RootContext.Documentation != null) {
1533 current_class.DocComment = Lexer.consume_doc_comment ();
1534 Lexer.doc_state = XmlCommentState.Allowed;
1539 if (RootContext.Documentation != null)
1540 Lexer.doc_state = XmlCommentState.Allowed;
1544 $$ = pop_current_class ();
1546 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1547 CheckIdentifierToken (yyToken, GetLocation ($5));
1553 opt_interface_member_declarations
1557 opt_interface_member_declarations
1559 | interface_member_declarations
1562 interface_member_declarations
1563 : interface_member_declaration
1564 | interface_member_declarations interface_member_declaration
1567 interface_member_declaration
1568 : interface_method_declaration
1573 Method m = (Method) $1;
1575 if (m.IsExplicitImpl)
1576 Report.Error (541, m.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1577 m.GetSignatureForError ());
1579 current_container.AddMethod (m);
1581 if (RootContext.Documentation != null)
1582 Lexer.doc_state = XmlCommentState.Allowed;
1584 | interface_property_declaration
1589 Property p = (Property) $1;
1591 if (p.IsExplicitImpl)
1592 Report.Error (541, p.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1593 p.GetSignatureForError ());
1595 current_container.AddProperty (p);
1597 if (RootContext.Documentation != null)
1598 Lexer.doc_state = XmlCommentState.Allowed;
1600 | interface_event_declaration
1603 Event e = (Event) $1;
1605 if (e.IsExplicitImpl)
1606 Report.Error (541, e.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1607 e.GetSignatureForError ());
1609 current_container.AddEvent (e);
1612 if (RootContext.Documentation != null)
1613 Lexer.doc_state = XmlCommentState.Allowed;
1615 | interface_indexer_declaration
1620 Indexer i = (Indexer) $1;
1622 if (i.IsExplicitImpl)
1623 Report.Error (541, i.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1624 i.GetSignatureForError ());
1626 current_container.AddIndexer (i);
1628 if (RootContext.Documentation != null)
1629 Lexer.doc_state = XmlCommentState.Allowed;
1631 | delegate_declaration
1634 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1635 ((MemberCore)$1).GetSignatureForError ());
1641 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1642 ((MemberCore)$1).GetSignatureForError ());
1645 | struct_declaration
1648 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1649 ((MemberCore)$1).GetSignatureForError ());
1655 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1656 ((MemberCore)$1).GetSignatureForError ());
1659 | interface_declaration
1662 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1663 ((MemberCore)$1).GetSignatureForError ());
1666 | constant_declaration
1668 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1676 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, GetLocation ($1));
1681 interface_method_declaration_body
1684 lexer.ConstraintsParsing = false;
1686 opt_statement_list CLOSE_BRACE
1688 Report.Error (531, lexer.Location,
1689 "'{0}': interface members cannot have a definition", $4);
1695 interface_method_declaration
1696 : opt_attributes opt_new type namespace_or_type_name
1697 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1699 lexer.ConstraintsParsing = true;
1701 opt_type_parameter_constraints_clauses
1702 interface_method_declaration_body
1704 lexer.ConstraintsParsing = false;
1706 MemberName name = (MemberName) $4;
1708 if ($9 != null && name.TypeArguments == null)
1709 Report.Error (80, lexer.Location,
1710 "Contraints are not allowed on non-generic declarations");
1712 GenericMethod generic = null;
1713 if (name.TypeArguments != null) {
1714 generic = new GenericMethod (current_namespace, current_class, name);
1716 generic.SetParameterInfo ((ArrayList) $9);
1719 $$ = new Method (current_class, generic, (Expression) $3, (int) $2, true, name,
1720 (Parameters) $6, (Attributes) $1);
1721 if (RootContext.Documentation != null)
1722 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1724 | opt_attributes opt_new VOID namespace_or_type_name
1725 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1727 lexer.ConstraintsParsing = true;
1729 opt_type_parameter_constraints_clauses
1730 interface_method_declaration_body
1732 lexer.ConstraintsParsing = false;
1734 MemberName name = (MemberName) $4;
1736 if ($9 != null && name.TypeArguments == null)
1737 Report.Error (80, lexer.Location,
1738 "Contraints are not allowed on non-generic declarations");
1740 GenericMethod generic = null;
1741 if (name.TypeArguments != null) {
1742 generic = new GenericMethod (current_namespace, current_class, name);
1744 generic.SetParameterInfo ((ArrayList) $9);
1747 $$ = new Method (current_class, generic, TypeManager.system_void_expr, (int) $2,
1748 true, name, (Parameters) $6, (Attributes) $1);
1749 if (RootContext.Documentation != null)
1750 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1754 interface_property_declaration
1759 { lexer.PropertyParsing = true; }
1760 accessor_declarations
1762 has_get = has_set = false;
1763 lexer.PropertyParsing = false;
1767 LocatedToken lt = (LocatedToken) $4;
1768 MemberName name = new MemberName (lt.Value, lt.Location);
1770 if ($3 == TypeManager.system_void_expr) {
1771 Report.Error (547, lt.Location, "`{0}': property or indexer cannot have void type", lt.Value);
1777 p = new Property (current_class, (Expression) $3, (int) $2, true,
1778 name, (Attributes) $1,
1781 Report.Error (548, p.Location, "`{0}': property or indexer must have at least one accessor", p.GetSignatureForError ());
1785 Pair pair = (Pair) $7;
1786 p = new Property (current_class, (Expression) $3, (int) $2, true,
1787 name, (Attributes) $1,
1788 (Accessor)pair.First, (Accessor)pair.Second);
1790 if (pair.First != null && ((Accessor)(pair.First)).Block != null) {
1791 Report.Error (531, p.Location, "`{0}.get': interface members cannot have a definition", p.GetSignatureForError ());
1796 if (pair.Second != null && ((Accessor)(pair.Second)).Block != null) {
1797 Report.Error (531, p.Location, "`{0}.set': interface members cannot have a definition", p.GetSignatureForError ());
1802 if (RootContext.Documentation != null)
1803 p.DocComment = Lexer.consume_doc_comment ();
1810 CheckIdentifierToken (yyToken, GetLocation ($4));
1816 interface_event_declaration
1817 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1819 LocatedToken lt = (LocatedToken) $5;
1820 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1821 new MemberName (lt.Value, lt.Location), null,
1823 if (RootContext.Documentation != null)
1824 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1826 | opt_attributes opt_new EVENT type error {
1827 CheckIdentifierToken (yyToken, GetLocation ($5));
1830 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1831 LocatedToken lt = (LocatedToken) $5;
1832 Report.Error (68, lt.Location, "`{0}.{1}': event in interface cannot have initializer", current_container.Name, lt.Value);
1835 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1837 lexer.EventParsing = true;
1839 event_accessor_declarations
1841 lexer.EventParsing = false;
1844 Report.Error (69, (Location) $3, "Event in interface cannot have add or remove accessors");
1849 interface_indexer_declaration
1850 : opt_attributes opt_new type THIS
1851 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1853 { lexer.PropertyParsing = true; }
1854 accessor_declarations
1856 has_get = has_set = false;
1857 lexer.PropertyParsing = false;
1863 i = new Indexer (current_class, (Expression) $3,
1864 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1865 (int) $2, true, (Parameters) $6, (Attributes) $1,
1868 Report.Error (548, i.Location, "`{0}': property or indexer must have at least one accessor", i.GetSignatureForError ());
1872 Pair pair = (Pair) $10;
1873 i = new Indexer (current_class, (Expression) $3,
1874 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1875 (int) $2, true, (Parameters) $6, (Attributes) $1,
1876 (Accessor)pair.First, (Accessor)pair.Second);
1878 if (pair.First != null && ((Accessor)(pair.First)).Block != null) {
1879 Report.Error (531, i.Location, "`{0}.get': interface members cannot have a definition", i.GetSignatureForError ());
1884 if (pair.Second != null && ((Accessor)(pair.Second)).Block != null) {
1885 Report.Error (531, i.Location, "`{0}.set': interface members cannot have a definition", i.GetSignatureForError ());
1890 if (RootContext.Documentation != null)
1891 i.DocComment = ConsumeStoredComment ();
1897 operator_declaration
1898 : opt_attributes opt_modifiers operator_declarator
1900 iterator_container = SimpleIteratorContainer.GetSimple ();
1907 OperatorDeclaration decl = (OperatorDeclaration) $3;
1909 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1911 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null, decl.location);
1912 if (decl.arg2type != null)
1913 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null, decl.location);
1915 Operator op = new Operator (
1916 current_class, decl.optype, decl.ret_type, (int) $2,
1917 new Parameters (param_list, null),
1918 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1920 if (RootContext.Documentation != null) {
1921 op.DocComment = tmpComment;
1922 Lexer.doc_state = XmlCommentState.Allowed;
1925 if (SimpleIteratorContainer.Simple.Yields)
1928 // Note again, checking is done in semantic analysis
1929 current_container.AddOperator (op);
1931 current_local_parameters = null;
1932 iterator_container = null;
1938 | SEMICOLON { $$ = null; }
1941 : type OPERATOR overloadable_operator
1942 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1944 LocatedToken lt = (LocatedToken) $6;
1945 Operator.OpType op = (Operator.OpType) $3;
1946 CheckUnaryOperator (op, lt.Location);
1948 if (op == Operator.OpType.Addition)
1949 op = Operator.OpType.UnaryPlus;
1951 if (op == Operator.OpType.Subtraction)
1952 op = Operator.OpType.UnaryNegation;
1954 Parameter [] pars = new Parameter [1];
1955 Expression type = (Expression) $5;
1957 pars [0] = new Parameter (type, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1959 current_local_parameters = new Parameters (pars, null);
1961 if (RootContext.Documentation != null) {
1962 tmpComment = Lexer.consume_doc_comment ();
1963 Lexer.doc_state = XmlCommentState.NotAllowed;
1966 $$ = new OperatorDeclaration (op, (Expression) $1, type, lt.Value,
1967 null, null, (Location) $2);
1969 | type OPERATOR overloadable_operator
1971 type IDENTIFIER COMMA
1975 LocatedToken ltParam1 = (LocatedToken) $6;
1976 LocatedToken ltParam2 = (LocatedToken) $9;
1977 CheckBinaryOperator ((Operator.OpType) $3, (Location) $2);
1979 Parameter [] pars = new Parameter [2];
1981 Expression typeL = (Expression) $5;
1982 Expression typeR = (Expression) $8;
1984 pars [0] = new Parameter (typeL, ltParam1.Value, Parameter.Modifier.NONE, null, ltParam1.Location);
1985 pars [1] = new Parameter (typeR, ltParam2.Value, Parameter.Modifier.NONE, null, ltParam2.Location);
1987 current_local_parameters = new Parameters (pars, null);
1989 if (RootContext.Documentation != null) {
1990 tmpComment = Lexer.consume_doc_comment ();
1991 Lexer.doc_state = XmlCommentState.NotAllowed;
1994 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1995 typeL, ltParam1.Value,
1996 typeR, ltParam2.Value, (Location) $2);
1998 | conversion_operator_declarator
1999 | type OPERATOR overloadable_operator
2001 type IDENTIFIER COMMA
2002 type IDENTIFIER COMMA
2006 Report.Error (1534, (Location) $2, "Overloaded binary operator `{0}' takes two parameters",
2007 Operator.GetName ((Operator.OpType) $3));
2010 | type OPERATOR overloadable_operator
2011 OPEN_PARENS CLOSE_PARENS
2013 Report.Error (1535, (Location) $2, "Overloaded unary operator `{0}' takes one parameter",
2014 Operator.GetName ((Operator.OpType) $3));
2019 overloadable_operator
2021 : BANG { $$ = Operator.OpType.LogicalNot; }
2022 | TILDE { $$ = Operator.OpType.OnesComplement; }
2023 | OP_INC { $$ = Operator.OpType.Increment; }
2024 | OP_DEC { $$ = Operator.OpType.Decrement; }
2025 | TRUE { $$ = Operator.OpType.True; }
2026 | FALSE { $$ = Operator.OpType.False; }
2027 // Unary and binary:
2028 | PLUS { $$ = Operator.OpType.Addition; }
2029 | MINUS { $$ = Operator.OpType.Subtraction; }
2031 | STAR { $$ = Operator.OpType.Multiply; }
2032 | DIV { $$ = Operator.OpType.Division; }
2033 | PERCENT { $$ = Operator.OpType.Modulus; }
2034 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
2035 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
2036 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
2037 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
2038 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
2039 | OP_EQ { $$ = Operator.OpType.Equality; }
2040 | OP_NE { $$ = Operator.OpType.Inequality; }
2041 | OP_GT { $$ = Operator.OpType.GreaterThan; }
2042 | OP_LT { $$ = Operator.OpType.LessThan; }
2043 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
2044 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
2047 conversion_operator_declarator
2048 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
2050 LocatedToken lt = (LocatedToken) $6;
2051 Parameter [] pars = new Parameter [1];
2053 pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2055 current_local_parameters = new Parameters (pars, null);
2057 if (RootContext.Documentation != null) {
2058 tmpComment = Lexer.consume_doc_comment ();
2059 Lexer.doc_state = XmlCommentState.NotAllowed;
2062 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, lt.Value,
2063 null, null, (Location) $2);
2065 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
2067 LocatedToken lt = (LocatedToken) $6;
2068 Parameter [] pars = new Parameter [1];
2070 pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2072 current_local_parameters = new Parameters (pars, null);
2074 if (RootContext.Documentation != null) {
2075 tmpComment = Lexer.consume_doc_comment ();
2076 Lexer.doc_state = XmlCommentState.NotAllowed;
2079 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, lt.Value,
2080 null, null, (Location) $2);
2084 syntax_error ((Location) $1, "'operator' expected");
2088 syntax_error ((Location) $1, "'operator' expected");
2092 constructor_declaration
2095 constructor_declarator
2098 Constructor c = (Constructor) $3;
2099 c.Block = (ToplevelBlock) $4;
2100 c.OptAttributes = (Attributes) $1;
2101 c.ModFlags = (int) $2;
2103 if (RootContext.Documentation != null)
2104 c.DocComment = ConsumeStoredComment ();
2106 if (c.Name == current_container.Basename){
2107 if ((c.ModFlags & Modifiers.STATIC) != 0){
2108 if ((c.ModFlags & Modifiers.Accessibility) != 0){
2109 Report.Error (515, c.Location,
2110 "`{0}': access modifiers are not allowed on static constructors",
2111 c.GetSignatureForError ());
2114 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2116 if (c.Initializer != null){
2117 Report.Error (514, c.Location,
2118 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2119 c.GetSignatureForError ());
2122 if (!c.Parameters.Empty){
2123 Report.Error (132, c.Location,
2124 "`{0}': The static constructor must be parameterless", c.GetSignatureForError ());
2127 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2130 // We let another layer check the validity of the constructor.
2131 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
2134 current_container.AddConstructor (c);
2136 current_local_parameters = null;
2137 if (RootContext.Documentation != null)
2138 Lexer.doc_state = XmlCommentState.Allowed;
2142 constructor_declarator
2145 if (RootContext.Documentation != null) {
2146 tmpComment = Lexer.consume_doc_comment ();
2147 Lexer.doc_state = XmlCommentState.Allowed;
2150 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2152 current_local_parameters = (Parameters) $4;
2154 opt_constructor_initializer
2156 LocatedToken lt = (LocatedToken) $1;
2157 $$ = new Constructor (current_class, lt.Value, 0, (Parameters) $4,
2158 (ConstructorInitializer) $7, lt.Location);
2164 | SEMICOLON { $$ = null; }
2167 opt_constructor_initializer
2168 : /* empty */ { $$ = null; }
2169 | constructor_initializer
2172 constructor_initializer
2173 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
2175 $$ = new ConstructorBaseInitializer ((ArrayList) $4, (Location) $2);
2177 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
2179 $$ = new ConstructorThisInitializer ((ArrayList) $4, (Location) $2);
2182 Report.Error (1018, (Location) $1, "Keyword this or base expected");
2188 : /* EMPTY */ { $$ = 0; }
2189 | UNSAFE { $$ = Modifiers.UNSAFE; }
2190 | EXTERN { $$ = Modifiers.EXTERN; }
2193 destructor_declaration
2194 : opt_attributes opt_finalizer TILDE
2196 if (RootContext.Documentation != null) {
2197 tmpComment = Lexer.consume_doc_comment ();
2198 Lexer.doc_state = XmlCommentState.NotAllowed;
2201 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2203 LocatedToken lt = (LocatedToken) $5;
2204 if (lt.Value != current_container.MemberName.Name){
2205 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2206 } else if (current_container.Kind != Kind.Class){
2207 Report.Error (575, lt.Location, "Only class types can contain destructor");
2209 Location l = lt.Location;
2212 if (!RootContext.StdLib && current_container.Name == "System.Object")
2213 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2215 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2217 Method d = new Destructor (
2218 current_class, TypeManager.system_void_expr, m, "Finalize",
2219 new Parameters (null, null), (Attributes) $1, l);
2220 if (RootContext.Documentation != null)
2221 d.DocComment = ConsumeStoredComment ();
2223 d.Block = (ToplevelBlock) $8;
2224 current_container.AddMethod (d);
2232 EVENT type variable_declarators SEMICOLON
2234 foreach (VariableDeclaration var in (ArrayList) $5) {
2236 MemberName name = new MemberName (var.identifier,
2239 Event e = new EventField (
2240 current_class, (Expression) $4, (int) $2, false, name,
2241 var.expression_or_array_initializer, (Attributes) $1);
2243 current_container.AddEvent (e);
2245 if (RootContext.Documentation != null) {
2246 e.DocComment = Lexer.consume_doc_comment ();
2247 Lexer.doc_state = XmlCommentState.Allowed;
2253 EVENT type namespace_or_type_name
2256 implicit_value_parameter_type = (Expression) $4;
2257 lexer.EventParsing = true;
2259 event_accessor_declarations
2261 lexer.EventParsing = false;
2265 MemberName name = (MemberName) $5;
2268 Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2269 current_container.Name, name);
2272 Pair pair = (Pair) $8;
2274 if (name.TypeArguments != null)
2275 syntax_error (lexer.Location, "an event can't have type arguments");
2277 if (pair.First == null || pair.Second == null)
2278 // CS0073 is already reported, so no CS0065 here.
2281 Event e = new EventProperty (
2282 current_class, (Expression) $4, (int) $2, false, name, null,
2283 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second);
2284 if (RootContext.Documentation != null) {
2285 e.DocComment = Lexer.consume_doc_comment ();
2286 Lexer.doc_state = XmlCommentState.Allowed;
2289 current_container.AddEvent (e);
2290 implicit_value_parameter_type = null;
2294 | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
2295 MemberName mn = (MemberName) $5;
2297 if (mn.Left != null)
2298 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2300 Report.Error (71, mn.Location, "Event declaration should use property syntax");
2302 if (RootContext.Documentation != null)
2303 Lexer.doc_state = XmlCommentState.Allowed;
2307 event_accessor_declarations
2308 : add_accessor_declaration remove_accessor_declaration
2310 $$ = new Pair ($1, $2);
2312 | remove_accessor_declaration add_accessor_declaration
2314 $$ = new Pair ($2, $1);
2316 | add_accessor_declaration { $$ = null; }
2317 | remove_accessor_declaration { $$ = null; }
2320 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2326 add_accessor_declaration
2327 : opt_attributes ADD
2329 Parameter [] args = new Parameter [1];
2330 Parameter implicit_value_parameter = new Parameter (
2331 implicit_value_parameter_type, "value",
2332 Parameter.Modifier.NONE, null, (Location) $2);
2334 args [0] = implicit_value_parameter;
2336 current_local_parameters = new Parameters (args, null);
2337 lexer.EventParsing = false;
2341 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2342 lexer.EventParsing = true;
2344 | opt_attributes ADD error {
2345 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2348 | opt_attributes modifiers ADD {
2349 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2354 remove_accessor_declaration
2355 : opt_attributes REMOVE
2357 Parameter [] args = new Parameter [1];
2358 Parameter implicit_value_parameter = new Parameter (
2359 implicit_value_parameter_type, "value",
2360 Parameter.Modifier.NONE, null, (Location) $2);
2362 args [0] = implicit_value_parameter;
2364 current_local_parameters = new Parameters (args, null);
2365 lexer.EventParsing = false;
2369 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2370 lexer.EventParsing = true;
2372 | opt_attributes REMOVE error {
2373 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2376 | opt_attributes modifiers REMOVE {
2377 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2383 : opt_attributes opt_modifiers indexer_declarator
2386 IndexerDeclaration decl = (IndexerDeclaration) $3;
2388 implicit_value_parameter_type = decl.type;
2390 lexer.PropertyParsing = true;
2391 parsing_indexer = true;
2393 indexer_parameters = decl.param_list;
2394 iterator_container = SimpleIteratorContainer.GetSimple ();
2396 accessor_declarations
2398 lexer.PropertyParsing = false;
2399 has_get = has_set = false;
2400 parsing_indexer = false;
2407 // The signature is computed from the signature of the indexer. Look
2408 // at section 3.6 on the spec
2410 IndexerDeclaration decl = (IndexerDeclaration) $3;
2411 Pair pair = (Pair) $6;
2412 Accessor get_block = (Accessor) pair.First;
2413 Accessor set_block = (Accessor) pair.Second;
2416 if (decl.interface_type != null)
2417 name = new MemberName (
2418 decl.interface_type, TypeContainer.DefaultIndexerName, decl.location);
2420 name = new MemberName (TypeContainer.DefaultIndexerName, decl.location);
2422 indexer = new Indexer (current_class, decl.type, name,
2423 (int) $2, false, decl.param_list, (Attributes) $1,
2424 get_block, set_block);
2426 if (RootContext.Documentation != null)
2427 indexer.DocComment = ConsumeStoredComment ();
2429 current_container.AddIndexer (indexer);
2431 current_local_parameters = null;
2432 implicit_value_parameter_type = null;
2433 indexer_parameters = null;
2438 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2440 Parameters pars = (Parameters) $4;
2441 if (pars.HasArglist) {
2442 // "__arglist is not valid in this context"
2443 Report.Error (1669, (Location) $2, "__arglist is not valid in this context");
2444 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2445 Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
2447 if (RootContext.Documentation != null) {
2448 tmpComment = Lexer.consume_doc_comment ();
2449 Lexer.doc_state = XmlCommentState.Allowed;
2452 $$ = new IndexerDeclaration ((Expression) $1, null, pars, (Location) $2);
2454 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2456 Parameters pars = (Parameters) $6;
2458 if (pars.HasArglist) {
2459 // "__arglist is not valid in this context"
2460 Report.Error (1669, (Location) $4, "__arglist is not valid in this context");
2461 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2462 Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
2465 MemberName name = (MemberName) $2;
2466 $$ = new IndexerDeclaration ((Expression) $1, name, pars, (Location) $4);
2468 if (RootContext.Documentation != null) {
2469 tmpComment = Lexer.consume_doc_comment ();
2470 Lexer.doc_state = XmlCommentState.Allowed;
2481 if (RootContext.Documentation != null)
2482 enumTypeComment = Lexer.consume_doc_comment ();
2487 LocatedToken lt = (LocatedToken) $5;
2488 Location enum_location = lt.Location;
2491 Report.Error (267, lt.Location, "The partial modifier can only appear immediately before `class', `struct' or `interface'");
2492 break; // assumes that the parser put us in a switch
2495 MemberName name = MakeName (new MemberName (lt.Value, enum_location));
2496 Enum e = new Enum (current_namespace, current_class, (Expression) $6, (int) $2,
2497 name, (Attributes) $1);
2499 if (RootContext.Documentation != null)
2500 e.DocComment = enumTypeComment;
2503 EnumMember em = null;
2504 foreach (VariableDeclaration ev in (ArrayList) $8) {
2505 em = new EnumMember (e, em, (Expression) ev.expression_or_array_initializer,
2506 new MemberName (ev.identifier, ev.Location), ev.OptAttributes);
2508 // if (RootContext.Documentation != null)
2509 em.DocComment = ev.DocComment;
2511 e.AddEnumMember (em);
2514 current_container.AddEnum (e);
2515 RootContext.Tree.RecordDecl (current_namespace.NS, name, e);
2522 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2523 | COLON type { $$ = $2; }
2529 if (RootContext.Documentation != null)
2530 Lexer.doc_state = XmlCommentState.Allowed;
2532 opt_enum_member_declarations
2534 // here will be evaluated after CLOSE_BLACE is consumed.
2535 if (RootContext.Documentation != null)
2536 Lexer.doc_state = XmlCommentState.Allowed;
2544 opt_enum_member_declarations
2545 : /* empty */ { $$ = new ArrayList (4); }
2546 | enum_member_declarations opt_comma { $$ = $1; }
2549 enum_member_declarations
2550 : enum_member_declaration
2552 ArrayList l = new ArrayList (4);
2557 | enum_member_declarations COMMA enum_member_declaration
2559 ArrayList l = (ArrayList) $1;
2567 enum_member_declaration
2568 : opt_attributes IDENTIFIER
2570 VariableDeclaration vd = new VariableDeclaration (
2571 (LocatedToken) $2, null, (Attributes) $1);
2573 if (RootContext.Documentation != null) {
2574 vd.DocComment = Lexer.consume_doc_comment ();
2575 Lexer.doc_state = XmlCommentState.Allowed;
2580 | opt_attributes IDENTIFIER
2582 if (RootContext.Documentation != null) {
2583 tmpComment = Lexer.consume_doc_comment ();
2584 Lexer.doc_state = XmlCommentState.NotAllowed;
2589 VariableDeclaration vd = new VariableDeclaration (
2590 (LocatedToken) $2, $5, (Attributes) $1);
2592 if (RootContext.Documentation != null)
2593 vd.DocComment = ConsumeStoredComment ();
2599 delegate_declaration
2604 lexer.ConstraintsParsing = true;
2607 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2609 MemberName name = MakeName ((MemberName) $6);
2610 Delegate del = new Delegate (current_namespace, current_class, (Expression) $5,
2611 (int) $2, name, (Parameters) $8, (Attributes) $1);
2613 if (RootContext.Documentation != null) {
2614 del.DocComment = Lexer.consume_doc_comment ();
2615 Lexer.doc_state = XmlCommentState.Allowed;
2618 current_container.AddDelegate (del);
2619 RootContext.Tree.RecordDecl (current_namespace.NS, name, del);
2621 current_delegate = del;
2623 opt_type_parameter_constraints_clauses
2625 lexer.ConstraintsParsing = false;
2629 current_delegate.SetParameterInfo ((ArrayList) $11);
2630 $$ = current_delegate;
2632 current_delegate = null;
2639 lexer.CheckNullable (false);
2644 lexer.CheckNullable (true);
2649 namespace_or_type_name
2651 | IDENTIFIER DOUBLE_COLON IDENTIFIER {
2652 LocatedToken lt1 = (LocatedToken) $1;
2653 LocatedToken lt2 = (LocatedToken) $3;
2654 $$ = new MemberName (lt1.Value, lt2.Value, lt2.Location);
2656 | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list {
2657 LocatedToken lt = (LocatedToken) $3;
2658 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $4, lt.Location);
2663 : IDENTIFIER opt_type_argument_list {
2664 LocatedToken lt = (LocatedToken) $1;
2665 $$ = new MemberName (lt.Value, (TypeArguments) $2, lt.Location);
2669 opt_type_argument_list
2670 : /* empty */ { $$ = null; }
2671 | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2674 if (RootContext.Version == LanguageVersion.ISO_1)
2675 Report.FeatureIsNotStandardized (lexer.Location, "generics");
2679 $$ = new TypeArguments ((int) $1, lexer.Location);
2680 if (RootContext.Version == LanguageVersion.ISO_1)
2681 Report.FeatureIsNotStandardized (lexer.Location, "generics");
2687 TypeArguments type_args = new TypeArguments (lexer.Location);
2688 type_args.Add ((Expression) $1);
2691 | type_arguments COMMA type {
2692 TypeArguments type_args = (TypeArguments) $1;
2693 type_args.Add ((Expression) $3);
2699 * Before you think of adding a return_type, notice that we have been
2700 * using two rules in the places where it matters (one rule using type
2701 * and another identical one that uses VOID as the return type). This
2702 * gets rid of a shift/reduce couple
2705 : namespace_or_type_name opt_nullable
2707 MemberName name = (MemberName) $1;
2708 $$ = name.GetTypeExpression ();
2711 $$ = new ComposedCast ((Expression) $$, "?", lexer.Location);
2713 | builtin_types opt_nullable
2716 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2726 // Note that here only unmanaged types are allowed but we
2727 // can't perform checks during this phase - we do it during
2728 // semantic analysis.
2730 $$ = new ComposedCast ((Expression) $1, "*", Lexer.Location);
2734 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2739 : builtin_types opt_nullable
2742 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2744 | non_expression_type rank_specifier
2746 Location loc = GetLocation ($1);
2748 loc = lexer.Location;
2749 $$ = new ComposedCast ((Expression) $1, (string) $2, loc);
2751 | non_expression_type STAR
2753 Location loc = GetLocation ($1);
2755 loc = lexer.Location;
2756 $$ = new ComposedCast ((Expression) $1, "*", loc);
2758 | expression rank_specifiers
2760 $$ = new ComposedCast ((Expression) $1, (string) $2);
2764 $$ = new ComposedCast ((Expression) $1, "*");
2768 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2769 // through this different path
2771 | multiplicative_expression STAR
2773 $$ = new ComposedCast ((Expression) $1, "*");
2780 ArrayList types = new ArrayList (4);
2785 | type_list COMMA type
2787 ArrayList types = (ArrayList) $1;
2795 * replaces all the productions for isolating the various
2796 * simple types, but we need this to reuse it easily in local_variable_type
2799 : OBJECT { $$ = TypeManager.system_object_expr; }
2800 | STRING { $$ = TypeManager.system_string_expr; }
2801 | BOOL { $$ = TypeManager.system_boolean_expr; }
2802 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2803 | FLOAT { $$ = TypeManager.system_single_expr; }
2804 | DOUBLE { $$ = TypeManager.system_double_expr; }
2809 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2810 | BYTE { $$ = TypeManager.system_byte_expr; }
2811 | SHORT { $$ = TypeManager.system_int16_expr; }
2812 | USHORT { $$ = TypeManager.system_uint16_expr; }
2813 | INT { $$ = TypeManager.system_int32_expr; }
2814 | UINT { $$ = TypeManager.system_uint32_expr; }
2815 | LONG { $$ = TypeManager.system_int64_expr; }
2816 | ULONG { $$ = TypeManager.system_uint64_expr; }
2817 | CHAR { $$ = TypeManager.system_char_expr; }
2818 | VOID { $$ = TypeManager.system_void_expr; }
2822 : type rank_specifiers opt_nullable
2824 string rank_specifiers = (string) $2;
2826 rank_specifiers += "?";
2828 $$ = new ComposedCast ((Expression) $1, rank_specifiers);
2833 // Expressions, section 7.5
2842 MemberName mn = (MemberName) $1;
2843 $$ = mn.GetTypeExpression ();
2845 | IDENTIFIER DOUBLE_COLON IDENTIFIER
2847 LocatedToken lt1 = (LocatedToken) $1;
2848 LocatedToken lt2 = (LocatedToken) $3;
2849 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, lt2.Location);
2851 | parenthesized_expression
2852 | default_value_expression
2854 | invocation_expression
2858 | post_increment_expression
2859 | post_decrement_expression
2863 | checked_expression
2864 | unchecked_expression
2865 | pointer_member_access
2866 | anonymous_method_expression
2873 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
2874 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); }
2875 | NULL { $$ = new NullLiteral (lexer.Location); }
2879 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
2880 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
2881 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
2886 object v = lexer.Value;
2889 $$ = new IntLiteral ((int) v, lexer.Location);
2890 } else if (v is uint)
2891 $$ = new UIntLiteral ((UInt32) v, lexer.Location);
2893 $$ = new LongLiteral ((Int64) v, lexer.Location);
2894 else if (v is ulong)
2895 $$ = new ULongLiteral ((UInt64) v, lexer.Location);
2897 Console.WriteLine ("OOPS. Unexpected result from scanner");
2902 : TRUE { $$ = new BoolLiteral (true, lexer.Location); }
2903 | FALSE { $$ = new BoolLiteral (false, lexer.Location); }
2906 parenthesized_expression_0
2907 : OPEN_PARENS expression CLOSE_PARENS
2910 lexer.Deambiguate_CloseParens ();
2911 // After this, the next token returned is one of
2912 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2913 // or CLOSE_PARENS_MINUS.
2917 parenthesized_expression
2918 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2922 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2924 // If a parenthesized expression is followed by a minus, we need to wrap
2925 // the expression inside a ParenthesizedExpression for the CS0075 check
2926 // in Binary.DoResolve().
2927 $$ = new ParenthesizedExpression ((Expression) $1);
2932 : primary_expression DOT IDENTIFIER opt_type_argument_list
2934 LocatedToken lt = (LocatedToken) $3;
2935 $$ = new MemberAccess ((Expression) $1, lt.Value,
2936 (TypeArguments) $4, lt.Location);
2938 | predefined_type DOT IDENTIFIER opt_type_argument_list
2940 LocatedToken lt = (LocatedToken) $3;
2941 $$ = new MemberAccess ((Expression) $1, lt.Value,
2942 (TypeArguments) $4, lt.Location);
2950 invocation_expression
2951 : primary_expression {
2952 $$ = lexer.Location;
2953 } OPEN_PARENS opt_argument_list CLOSE_PARENS
2956 Location l = (Location) $3;
2957 Report.Error (1, l, "Parse error");
2959 $$ = new Invocation ((Expression) $1, (ArrayList) $4);
2961 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2963 $$ = new Invocation ((Expression) $1, new ArrayList ());
2965 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2967 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
2969 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2971 ArrayList args = new ArrayList (1);
2973 $$ = new Invocation ((Expression) $1, args);
2975 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2977 ArrayList args = ((ArrayList) $4);
2979 $$ = new Invocation ((Expression) $1, args);
2984 : /* empty */ { $$ = null; }
2991 ArrayList list = new ArrayList (4);
2995 | argument_list COMMA argument
2997 ArrayList list = (ArrayList) $1;
3001 | argument_list error {
3002 CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
3010 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
3012 | non_simple_argument
3019 : REF variable_reference
3021 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
3023 | OUT variable_reference
3025 $$ = new Argument ((Expression) $2, Argument.AType.Out);
3027 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
3029 ArrayList list = (ArrayList) $3;
3030 Argument[] args = new Argument [list.Count];
3031 list.CopyTo (args, 0);
3033 Expression expr = new Arglist (args, (Location) $1);
3034 $$ = new Argument (expr, Argument.AType.Expression);
3038 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
3043 : expression { note ("section 5.4"); $$ = $1; }
3047 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
3049 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
3051 | primary_expression rank_specifiers
3053 // So the super-trick is that primary_expression
3054 // can only be either a SimpleName or a MemberAccess.
3055 // The MemberAccess case arises when you have a fully qualified type-name like :
3057 // SimpleName is when you have
3060 Expression expr = (Expression) $1;
3061 if (expr is ComposedCast){
3062 $$ = new ComposedCast (expr, (string) $2);
3063 } else if (!(expr is SimpleName || expr is MemberAccess || expr is ConstructedType || expr is QualifiedAliasMember)){
3064 Error_ExpectingTypeName (expr);
3065 $$ = TypeManager.system_object_expr;
3068 // So we extract the string corresponding to the SimpleName
3071 $$ = new ComposedCast (expr, (string) $2);
3079 ArrayList list = new ArrayList (4);
3083 | expression_list COMMA expression
3085 ArrayList list = (ArrayList) $1;
3094 $$ = new This (current_block, (Location) $1);
3099 : BASE DOT IDENTIFIER
3101 LocatedToken lt = (LocatedToken) $3;
3102 $$ = new BaseAccess (lt.Value, lt.Location);
3104 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
3106 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
3109 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
3114 post_increment_expression
3115 : primary_expression OP_INC
3117 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
3118 (Expression) $1, (Location) $2);
3122 post_decrement_expression
3123 : primary_expression OP_DEC
3125 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
3126 (Expression) $1, (Location) $2);
3131 : object_or_delegate_creation_expression
3132 | array_creation_expression
3135 object_or_delegate_creation_expression
3136 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
3138 $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
3142 array_creation_expression
3143 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
3145 opt_array_initializer
3147 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
3149 | NEW type rank_specifiers array_initializer
3151 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, (Location) $1);
3155 Report.Error (1031, (Location) $1, "Type expected");
3160 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
3176 opt_rank_specifier_or_nullable
3185 | opt_nullable rank_specifiers
3192 | opt_nullable rank_specifiers INTERR
3195 $$ = "?" + $2 + "?";
3202 : rank_specifier opt_rank_specifier
3204 $$ = (string) $2 + (string) $1;
3209 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3211 $$ = "[" + (string) $2 + "]";
3231 | dim_separators COMMA
3233 $$ = (string) $1 + ",";
3237 opt_array_initializer
3249 : OPEN_BRACE CLOSE_BRACE
3251 ArrayList list = new ArrayList (4);
3254 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3256 $$ = (ArrayList) $2;
3260 variable_initializer_list
3261 : variable_initializer
3263 ArrayList list = new ArrayList (4);
3267 | variable_initializer_list COMMA variable_initializer
3269 ArrayList list = (ArrayList) $1;
3275 void_pointer_expression
3276 : void_pointer_expression STAR
3278 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3282 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3287 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
3289 $$ = new TypeOfVoid ((Location) $1);
3291 | TYPEOF OPEN_PARENS void_pointer_expression CLOSE_PARENS
3293 $$ = new TypeOf ((Expression) $3, (Location) $1);
3295 | TYPEOF OPEN_PARENS
3297 lexer.TypeOfParsing = true;
3301 lexer.TypeOfParsing = false;
3302 $$ = new TypeOf ((Expression) $4, lexer.Location);
3307 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3308 $$ = new SizeOf ((Expression) $3, (Location) $1);
3313 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3315 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3319 unchecked_expression
3320 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3322 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3326 pointer_member_access
3327 : primary_expression OP_PTR IDENTIFIER
3330 LocatedToken lt = (LocatedToken) $3;
3332 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
3333 $$ = new MemberAccess (deref, lt.Value, lt.Location);
3337 anonymous_method_expression
3338 : DELEGATE opt_anonymous_method_signature
3340 oob_stack.Push (current_local_parameters);
3341 current_local_parameters = (Parameters)$2;
3343 // Force the next block to be created as a ToplevelBlock
3344 oob_stack.Push (current_block);
3345 oob_stack.Push (top_current_block);
3346 current_block = null;
3350 Location loc = (Location) $1;
3351 top_current_block = (Block) oob_stack.Pop ();
3352 current_block = (Block) oob_stack.Pop ();
3353 if (RootContext.Version == LanguageVersion.ISO_1){
3354 Report.FeatureIsNotStandardized (loc, "anonymous methods");
3357 ToplevelBlock anon_block = (ToplevelBlock) $4;
3359 anon_block.Parent = current_block;
3360 $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block,
3363 current_local_parameters = (Parameters) oob_stack.Pop ();
3367 opt_anonymous_method_signature
3368 : /* empty */ { $$ = null; }
3369 | anonymous_method_signature
3372 anonymous_method_signature
3373 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3376 $$ = Parameters.EmptyReadOnlyParameters;
3378 ArrayList par_list = (ArrayList) $2;
3379 Parameter [] pars = new Parameter [par_list.Count];
3380 par_list.CopyTo (pars);
3381 $$ = new Parameters (pars, null);
3386 opt_anonymous_method_parameter_list
3387 : /* empty */ { $$ = null; }
3388 | anonymous_method_parameter_list { $$ = $1; }
3391 anonymous_method_parameter_list
3392 : anonymous_method_parameter
3394 ArrayList a = new ArrayList (4);
3398 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3400 ArrayList a = (ArrayList) $1;
3406 anonymous_method_parameter
3407 : opt_parameter_modifier type IDENTIFIER {
3408 LocatedToken lt = (LocatedToken) $3;
3409 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3411 | PARAMS type IDENTIFIER {
3412 Report.Error (1670, ((LocatedToken) $3).Location, "The `params' modifier is not allowed in anonymous method declaration");
3417 default_value_expression
3418 : DEFAULT_OPEN_PARENS type CLOSE_PARENS
3420 $$ = new DefaultValueExpression ((Expression) $2, lexer.Location);
3425 : primary_expression
3426 | BANG prefixed_unary_expression
3428 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3430 | TILDE prefixed_unary_expression
3432 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3438 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3440 $$ = new Cast ((Expression) $1, (Expression) $3);
3442 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3444 $$ = new Cast ((Expression) $1, (Expression) $3);
3450 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3452 // TODO: wrong location
3453 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3458 // The idea to split this out is from Rhys' grammar
3459 // to solve the problem with casts.
3461 prefixed_unary_expression
3463 | PLUS prefixed_unary_expression
3465 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3467 | MINUS prefixed_unary_expression
3469 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3471 | OP_INC prefixed_unary_expression
3473 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3474 (Expression) $2, (Location) $1);
3476 | OP_DEC prefixed_unary_expression
3478 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3479 (Expression) $2, (Location) $1);
3481 | STAR prefixed_unary_expression
3483 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, (Location) $1);
3485 | BITWISE_AND prefixed_unary_expression
3487 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3491 pre_increment_expression
3492 : OP_INC prefixed_unary_expression
3494 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3495 (Expression) $2, (Location) $1);
3499 pre_decrement_expression
3500 : OP_DEC prefixed_unary_expression
3502 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3503 (Expression) $2, (Location) $1);
3507 multiplicative_expression
3508 : prefixed_unary_expression
3509 | multiplicative_expression STAR prefixed_unary_expression
3511 $$ = new Binary (Binary.Operator.Multiply,
3512 (Expression) $1, (Expression) $3);
3514 | multiplicative_expression DIV prefixed_unary_expression
3516 $$ = new Binary (Binary.Operator.Division,
3517 (Expression) $1, (Expression) $3);
3519 | multiplicative_expression PERCENT prefixed_unary_expression
3521 $$ = new Binary (Binary.Operator.Modulus,
3522 (Expression) $1, (Expression) $3);
3527 : multiplicative_expression
3528 | additive_expression PLUS multiplicative_expression
3530 $$ = new Binary (Binary.Operator.Addition,
3531 (Expression) $1, (Expression) $3);
3533 | additive_expression MINUS multiplicative_expression
3535 $$ = new Binary (Binary.Operator.Subtraction,
3536 (Expression) $1, (Expression) $3);
3541 : additive_expression
3542 | shift_expression OP_SHIFT_LEFT additive_expression
3544 $$ = new Binary (Binary.Operator.LeftShift,
3545 (Expression) $1, (Expression) $3);
3547 | shift_expression OP_SHIFT_RIGHT additive_expression
3549 $$ = new Binary (Binary.Operator.RightShift,
3550 (Expression) $1, (Expression) $3);
3561 lexer.PutbackNullable ();
3566 nullable_type_or_conditional
3569 if (((bool) $2) && ($1 is ComposedCast))
3570 $$ = ((ComposedCast) $1).RemoveNullable ();
3576 relational_expression
3578 | relational_expression OP_LT shift_expression
3580 $$ = new Binary (Binary.Operator.LessThan,
3581 (Expression) $1, (Expression) $3);
3583 | relational_expression OP_GT shift_expression
3585 $$ = new Binary (Binary.Operator.GreaterThan,
3586 (Expression) $1, (Expression) $3);
3588 | relational_expression OP_LE shift_expression
3590 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3591 (Expression) $1, (Expression) $3);
3593 | relational_expression OP_GE shift_expression
3595 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3596 (Expression) $1, (Expression) $3);
3598 | relational_expression IS
3601 } nullable_type_or_conditional
3603 $$ = new Is ((Expression) $1, (Expression) $4, (Location) $2);
3605 | relational_expression AS
3608 } nullable_type_or_conditional
3610 $$ = new As ((Expression) $1, (Expression) $4, (Location) $2);
3615 : relational_expression
3616 | equality_expression OP_EQ relational_expression
3618 $$ = new Binary (Binary.Operator.Equality,
3619 (Expression) $1, (Expression) $3);
3621 | equality_expression OP_NE relational_expression
3623 $$ = new Binary (Binary.Operator.Inequality,
3624 (Expression) $1, (Expression) $3);
3629 : equality_expression
3630 | and_expression BITWISE_AND equality_expression
3632 $$ = new Binary (Binary.Operator.BitwiseAnd,
3633 (Expression) $1, (Expression) $3);
3637 exclusive_or_expression
3639 | exclusive_or_expression CARRET and_expression
3641 $$ = new Binary (Binary.Operator.ExclusiveOr,
3642 (Expression) $1, (Expression) $3);
3646 inclusive_or_expression
3647 : exclusive_or_expression
3648 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3650 $$ = new Binary (Binary.Operator.BitwiseOr,
3651 (Expression) $1, (Expression) $3);
3655 conditional_and_expression
3656 : inclusive_or_expression
3657 | conditional_and_expression OP_AND inclusive_or_expression
3659 $$ = new Binary (Binary.Operator.LogicalAnd,
3660 (Expression) $1, (Expression) $3);
3664 conditional_or_expression
3665 : conditional_and_expression
3666 | conditional_or_expression OP_OR conditional_and_expression
3668 $$ = new Binary (Binary.Operator.LogicalOr,
3669 (Expression) $1, (Expression) $3);
3673 conditional_expression
3674 : conditional_or_expression
3675 | conditional_or_expression INTERR expression COLON expression
3677 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
3679 | conditional_or_expression INTERR INTERR expression
3681 $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $4, lexer.Location);
3683 // We'll be resolved into a `parenthesized_expression_0' later on.
3684 | conditional_or_expression INTERR CLOSE_PARENS
3686 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
3687 lexer.PutbackCloseParens ();
3691 assignment_expression
3692 : prefixed_unary_expression ASSIGN expression
3694 $$ = new Assign ((Expression) $1, (Expression) $3);
3696 | prefixed_unary_expression OP_MULT_ASSIGN expression
3698 $$ = new CompoundAssign (
3699 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
3701 | prefixed_unary_expression OP_DIV_ASSIGN expression
3703 $$ = new CompoundAssign (
3704 Binary.Operator.Division, (Expression) $1, (Expression) $3);
3706 | prefixed_unary_expression OP_MOD_ASSIGN expression
3708 $$ = new CompoundAssign (
3709 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
3711 | prefixed_unary_expression OP_ADD_ASSIGN expression
3713 $$ = new CompoundAssign (
3714 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
3716 | prefixed_unary_expression OP_SUB_ASSIGN expression
3718 $$ = new CompoundAssign (
3719 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3721 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3723 $$ = new CompoundAssign (
3724 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
3726 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3728 $$ = new CompoundAssign (
3729 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
3731 | prefixed_unary_expression OP_AND_ASSIGN expression
3733 $$ = new CompoundAssign (
3734 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
3736 | prefixed_unary_expression OP_OR_ASSIGN expression
3738 $$ = new CompoundAssign (
3739 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
3741 | prefixed_unary_expression OP_XOR_ASSIGN expression
3743 $$ = new CompoundAssign (
3744 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
3749 : conditional_expression
3750 | assignment_expression
3770 lexer.ConstraintsParsing = true;
3774 MemberName name = MakeName ((MemberName) $6);
3775 int mod_flags = (int) $2;
3778 ClassPart part = PartialContainer.CreatePart (
3779 current_namespace, current_class, name, mod_flags,
3780 (Attributes) $1, Kind.Class, (Location) $3);
3782 current_container = part.PartialContainer;
3783 current_class = part;
3785 if ((mod_flags & Modifiers.STATIC) != 0) {
3786 current_class = new StaticClass (
3787 current_namespace, current_class, name,
3788 mod_flags, (Attributes) $1);
3790 current_class = new Class (
3791 current_namespace, current_class, name,
3792 mod_flags, (Attributes) $1);
3795 current_container.AddClassOrStruct (current_class);
3796 current_container = current_class;
3797 RootContext.Tree.RecordDecl (current_namespace.NS, name, current_class);
3801 opt_type_parameter_constraints_clauses
3803 lexer.ConstraintsParsing = false;
3806 if (current_class.Name == "System.Object") {
3807 Report.Error (537, current_class.Location,
3808 "The class System.Object cannot have a base " +
3809 "class or implement an interface.");
3811 current_class.Bases = (ArrayList) $8;
3814 current_class.SetParameterInfo ((ArrayList) $9);
3816 if (RootContext.Documentation != null) {
3817 current_class.DocComment = Lexer.consume_doc_comment ();
3818 Lexer.doc_state = XmlCommentState.Allowed;
3823 if (RootContext.Documentation != null)
3824 Lexer.doc_state = XmlCommentState.Allowed;
3828 $$ = pop_current_class ();
3836 { $$ = $1; } // location
3840 : /* empty */ { $$ = (int) 0; }
3846 | modifiers modifier
3851 if ((m1 & m2) != 0) {
3852 Location l = lexer.Location;
3853 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
3855 $$ = (int) (m1 | m2);
3860 : NEW { $$ = Modifiers.NEW; }
3861 | PUBLIC { $$ = Modifiers.PUBLIC; }
3862 | PROTECTED { $$ = Modifiers.PROTECTED; }
3863 | INTERNAL { $$ = Modifiers.INTERNAL; }
3864 | PRIVATE { $$ = Modifiers.PRIVATE; }
3865 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3866 | SEALED { $$ = Modifiers.SEALED; }
3867 | STATIC { $$ = Modifiers.STATIC; }
3868 | READONLY { $$ = Modifiers.READONLY; }
3869 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3870 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3871 | EXTERN { $$ = Modifiers.EXTERN; }
3872 | VOLATILE { $$ = Modifiers.VOLATILE; }
3873 | UNSAFE { $$ = Modifiers.UNSAFE; }
3877 : /* empty */ { $$ = null; }
3878 | class_base { $$ = $1; }
3882 : COLON type_list { $$ = $2; }
3885 opt_type_parameter_constraints_clauses
3886 : /* empty */ { $$ = null; }
3887 | type_parameter_constraints_clauses
3891 type_parameter_constraints_clauses
3892 : type_parameter_constraints_clause {
3893 ArrayList constraints = new ArrayList (1);
3894 constraints.Add ($1);
3897 | type_parameter_constraints_clauses type_parameter_constraints_clause {
3898 ArrayList constraints = (ArrayList) $1;
3900 constraints.Add ($2);
3905 type_parameter_constraints_clause
3906 : WHERE IDENTIFIER COLON type_parameter_constraints {
3907 LocatedToken lt = (LocatedToken) $2;
3908 $$ = new Constraints (lt.Value, (ArrayList) $4, lt.Location);
3912 type_parameter_constraints
3913 : type_parameter_constraint {
3914 ArrayList constraints = new ArrayList (1);
3915 constraints.Add ($1);
3918 | type_parameter_constraints COMMA type_parameter_constraint {
3919 ArrayList constraints = (ArrayList) $1;
3921 constraints.Add ($3);
3926 type_parameter_constraint
3928 | NEW OPEN_PARENS CLOSE_PARENS {
3929 $$ = SpecialConstraint.Constructor;
3932 $$ = SpecialConstraint.ReferenceType;
3935 $$ = SpecialConstraint.ValueType;
3944 // A block is "contained" on the following places:
3946 // property_declaration as part of the accessor body (get/set)
3947 // operator_declaration
3948 // constructor_declaration
3949 // destructor_declaration
3950 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3955 if (current_block == null){
3956 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, (Location) $1);
3957 top_current_block = current_block;
3959 current_block = new Block (current_block, (Location) $1, Location.Null);
3962 opt_statement_list CLOSE_BRACE
3964 while (current_block.Implicit)
3965 current_block = current_block.Parent;
3967 current_block.SetEndLocation ((Location) $4);
3968 current_block = current_block.Parent;
3969 if (current_block == null)
3970 top_current_block = null;
3981 | statement_list statement
3985 : declaration_statement
3987 if ($1 != null && (Block) $1 != current_block){
3988 current_block.AddStatement ((Statement) $1);
3989 current_block = (Block) $1;
3992 | valid_declaration_statement
3994 current_block.AddStatement ((Statement) $1);
3999 valid_declaration_statement
4002 | expression_statement
4003 | selection_statement
4004 | iteration_statement
4008 | unchecked_statement
4016 : valid_declaration_statement
4017 | declaration_statement
4019 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4024 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4032 $$ = EmptyStatement.Value;
4039 LocatedToken lt = (LocatedToken) $1;
4040 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
4042 if (current_block.AddLabel (lt.Value, labeled, lt.Location))
4043 current_block.AddStatement (labeled);
4048 declaration_statement
4049 : local_variable_declaration SEMICOLON
4052 DictionaryEntry de = (DictionaryEntry) $1;
4053 Expression e = (Expression) de.Key;
4055 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
4059 | local_constant_declaration SEMICOLON
4062 DictionaryEntry de = (DictionaryEntry) $1;
4064 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
4070 * The following is from Rhys' grammar:
4071 * > Types in local variable declarations must be recognized as
4072 * > expressions to prevent reduce/reduce errors in the grammar.
4073 * > The expressions are converted into types during semantic analysis.
4076 : primary_expression opt_rank_specifier_or_nullable
4078 // FIXME: Do something smart here regarding the composition of the type.
4080 // Ok, the above "primary_expression" is there to get rid of
4081 // both reduce/reduce and shift/reduces in the grammar, it should
4082 // really just be "type_name". If you use type_name, a reduce/reduce
4083 // creeps up. If you use namespace_or_type_name (which is all we need
4084 // really) two shift/reduces appear.
4087 // So the super-trick is that primary_expression
4088 // can only be either a SimpleName or a MemberAccess.
4089 // The MemberAccess case arises when you have a fully qualified type-name like :
4091 // SimpleName is when you have
4094 Expression expr = (Expression) $1;
4095 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType || expr is QualifiedAliasMember)) {
4096 Error_ExpectingTypeName (expr);
4100 // So we extract the string corresponding to the SimpleName
4104 if ((string) $2 == "")
4107 $$ = new ComposedCast ((Expression) $1, (string) $2);
4110 | builtin_types opt_rank_specifier_or_nullable
4112 if ((string) $2 == "")
4115 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
4119 local_variable_pointer_type
4120 : primary_expression STAR
4122 Expression expr = (Expression) $1;
4124 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType || expr is QualifiedAliasMember)) {
4125 Error_ExpectingTypeName (expr);
4129 $$ = new ComposedCast ((Expression) $1, "*");
4131 | builtin_types STAR
4133 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
4137 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
4139 | local_variable_pointer_type STAR
4141 $$ = new ComposedCast ((Expression) $1, "*");
4145 local_variable_declaration
4146 : local_variable_type variable_declarators
4149 $$ = new DictionaryEntry ($1, $2);
4153 | local_variable_pointer_type opt_rank_specifier_or_nullable variable_declarators
4158 if ((string) $2 == "")
4159 t = (Expression) $1;
4161 t = new ComposedCast ((Expression) $1, (string) $2);
4162 $$ = new DictionaryEntry (t, $3);
4168 local_constant_declaration
4169 : CONST local_variable_type constant_declarators
4172 $$ = new DictionaryEntry ($2, $3);
4178 expression_statement
4179 : statement_expression SEMICOLON
4186 // We have to do the wrapping here and not in the case above,
4187 // because statement_expression is used for example in for_statement
4189 statement_expression
4190 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
4191 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
4192 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
4193 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
4194 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
4195 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
4196 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
4198 Report.Error (1002, GetLocation ($1), "Expecting `;'");
4203 object_creation_expression
4204 : object_or_delegate_creation_expression
4205 { note ("complain if this is a delegate maybe?"); }
4214 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
4217 Location l = (Location) $1;
4219 $$ = new If ((Expression) $3, (Statement) $5, l);
4221 if (RootContext.WarningLevel >= 3){
4222 // FIXME: location for warning should be loc property of $5.
4223 if ($5 == EmptyStatement.Value)
4224 Report.Warning (642, l, "Possible mistaken empty statement");
4228 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
4229 embedded_statement ELSE embedded_statement
4231 Location l = (Location) $1;
4233 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
4235 if (RootContext.WarningLevel >= 3){
4236 // FIXME: location for warning should be loc property of $5 and $7.
4237 if ($5 == EmptyStatement.Value)
4238 Report.Warning (642, l, "Possible mistaken empty statement");
4239 if ($7 == EmptyStatement.Value)
4240 Report.Warning (642, l, "Possible mistaken empty statement");
4246 : SWITCH OPEN_PARENS
4248 switch_stack.Push (current_block);
4250 expression CLOSE_PARENS
4253 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
4254 current_block = (Block) switch_stack.Pop ();
4270 Report.Error (1522, lexer.Location, "Empty switch block");
4278 ArrayList sections = new ArrayList (4);
4283 | switch_sections switch_section
4285 ArrayList sections = (ArrayList) $1;
4295 current_block = current_block.CreateSwitchBlock (lexer.Location);
4299 Block topmost = current_block;
4301 while (topmost.Implicit)
4302 topmost = topmost.Parent;
4303 $$ = new SwitchSection ((ArrayList) $1, topmost);
4310 ArrayList labels = new ArrayList (4);
4315 | switch_labels switch_label
4317 ArrayList labels = (ArrayList) ($1);
4325 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
4326 | DEFAULT COLON { $$ = new SwitchLabel (null, (Location) $2); }
4329 1523, GetLocation ($1),
4330 "The keyword case or default must precede code in switch block");
4342 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
4344 Location l = (Location) $1;
4345 $$ = new While ((Expression) $3, (Statement) $5, l);
4350 : DO embedded_statement
4351 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
4353 Location l = (Location) $1;
4355 $$ = new Do ((Statement) $2, (Expression) $5, l);
4361 opt_for_initializer SEMICOLON
4363 Block assign_block = new Block (current_block);
4364 current_block = assign_block;
4366 if ($3 is DictionaryEntry){
4367 DictionaryEntry de = (DictionaryEntry) $3;
4369 Expression type = (Expression) de.Key;
4370 ArrayList var_declarators = (ArrayList) de.Value;
4372 foreach (VariableDeclaration decl in var_declarators){
4376 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4380 Location l = lexer.Location;
4382 if (decl.expression_or_array_initializer is Expression){
4383 expr = (Expression) decl.expression_or_array_initializer;
4384 } else if (decl.expression_or_array_initializer == null) {
4387 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4388 expr = new ArrayCreation (type, "", init, decl.Location);
4391 LocalVariableReference var;
4392 var = new LocalVariableReference (assign_block, decl.identifier, l);
4395 Assign a = new Assign (var, expr, decl.Location);
4397 assign_block.AddStatement (new StatementExpression (a));
4401 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4402 // This can be referred to as $5 below.
4408 opt_for_condition SEMICOLON
4409 opt_for_iterator CLOSE_PARENS
4412 Location l = (Location) $1;
4414 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4416 current_block.AddStatement (f);
4417 while (current_block.Implicit)
4418 current_block = current_block.Parent;
4420 current_block = current_block.Parent;
4425 : /* empty */ { $$ = EmptyStatement.Value; }
4430 : local_variable_declaration
4431 | statement_expression_list
4435 : /* empty */ { $$ = null; }
4436 | boolean_expression
4440 : /* empty */ { $$ = EmptyStatement.Value; }
4445 : statement_expression_list
4448 statement_expression_list
4449 : statement_expression
4451 // CHANGE: was `null'
4452 Block b = new Block (current_block, Block.Flags.Implicit);
4454 b.AddStatement ((Statement) $1);
4457 | statement_expression_list COMMA statement_expression
4459 Block b = (Block) $1;
4461 b.AddStatement ((Statement) $3);
4467 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4469 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4472 | FOREACH OPEN_PARENS type IDENTIFIER IN
4473 expression CLOSE_PARENS
4475 Block foreach_block = new Block (current_block);
4476 current_block = foreach_block;
4478 LocatedToken lt = (LocatedToken) $4;
4479 Location l = lt.Location;
4482 vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4484 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4486 // Get a writable reference to this read-only variable.
4488 // Note that the $$ here refers to the value of _this_ code block,
4489 // not the value of the LHS non-terminal. This can be referred to as $8 below.
4490 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
4497 LocalVariableReference v = (LocalVariableReference) $8;
4498 Location l = (Location) $1;
4501 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
4502 current_block.AddStatement (f);
4505 while (current_block.Implicit)
4506 current_block = current_block.Parent;
4508 current_block = current_block.Parent;
4514 | continue_statement
4524 $$ = new Break ((Location) $1);
4529 : CONTINUE SEMICOLON
4531 $$ = new Continue ((Location) $1);
4536 : GOTO IDENTIFIER SEMICOLON
4538 LocatedToken lt = (LocatedToken) $2;
4539 $$ = new Goto (lt.Value, lt.Location);
4541 | GOTO CASE constant_expression SEMICOLON
4543 $$ = new GotoCase ((Expression) $3, (Location) $1);
4545 | GOTO DEFAULT SEMICOLON
4547 $$ = new GotoDefault ((Location) $1);
4552 : RETURN opt_expression SEMICOLON
4554 $$ = new Return ((Expression) $2, (Location) $1);
4559 : THROW opt_expression SEMICOLON
4561 $$ = new Throw ((Expression) $2, (Location) $1);
4566 : IDENTIFIER RETURN expression SEMICOLON
4568 LocatedToken lt = (LocatedToken) $1;
4569 string s = lt.Value;
4571 Report.Error (1003, lt.Location, "; expected");
4574 if (RootContext.Version == LanguageVersion.ISO_1){
4575 Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4578 if (iterator_container == null){
4579 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4582 iterator_container.SetYields ();
4583 $$ = new Yield ((Expression) $3, lt.Location);
4586 | IDENTIFIER RETURN SEMICOLON
4588 Report.Error (1627, (Location) $2, "Expression expected after yield return");
4591 | IDENTIFIER BREAK SEMICOLON
4593 LocatedToken lt = (LocatedToken) $1;
4594 string s = lt.Value;
4596 Report.Error (1003, lt.Location, "; expected");
4599 if (RootContext.Version == LanguageVersion.ISO_1){
4600 Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4603 if (iterator_container == null){
4604 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4607 iterator_container.SetYields ();
4608 $$ = new YieldBreak (lt.Location);
4619 : TRY block catch_clauses
4623 ArrayList c = (ArrayList)$3;
4624 for (int i = 0; i < c.Count; ++i) {
4625 Catch cc = (Catch) c [i];
4627 if (i != c.Count - 1)
4628 Report.Error (1017, cc.loc, "Try statement already has an empty catch block");
4635 // Now s contains the list of specific catch clauses
4636 // and g contains the general one.
4638 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4640 | TRY block opt_catch_clauses FINALLY block
4643 ArrayList s = new ArrayList (4);
4644 ArrayList catch_list = (ArrayList) $3;
4646 if (catch_list != null){
4647 foreach (Catch cc in catch_list) {
4655 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4659 Report.Error (1524, (Location) $1, "Expected catch or finally");
4665 : /* empty */ { $$ = null; }
4672 ArrayList l = new ArrayList (4);
4677 | catch_clauses catch_clause
4679 ArrayList l = (ArrayList) $1;
4687 : /* empty */ { $$ = null; }
4692 : CATCH opt_catch_args
4694 Expression type = null;
4697 DictionaryEntry cc = (DictionaryEntry) $2;
4698 type = (Expression) cc.Key;
4699 LocatedToken lt = (LocatedToken) cc.Value;
4702 ArrayList one = new ArrayList (4);
4704 one.Add (new VariableDeclaration (lt, null));
4706 current_block = new Block (current_block);
4707 Block b = declare_local_variables (type, one, lt.Location);
4712 Expression type = null;
4716 DictionaryEntry cc = (DictionaryEntry) $2;
4717 type = (Expression) cc.Key;
4718 LocatedToken lt = (LocatedToken) cc.Value;
4722 while (current_block.Implicit)
4723 current_block = current_block.Parent;
4724 current_block = current_block.Parent;
4728 $$ = new Catch (type, id, (Block) $4, ((Block) $4).loc);
4733 : /* empty */ { $$ = null; }
4738 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4740 $$ = new DictionaryEntry ($2, $3);
4748 $$ = new Checked ((Block) $2);
4755 $$ = new Unchecked ((Block) $2);
4762 RootContext.CheckUnsafeOption ((Location) $1);
4764 $$ = new Unsafe ((Block) $3);
4770 type fixed_pointer_declarators
4773 ArrayList list = (ArrayList) $4;
4774 Expression type = (Expression) $3;
4775 Location l = (Location) $1;
4776 int top = list.Count;
4778 Block assign_block = new Block (current_block);
4779 current_block = assign_block;
4781 for (int i = 0; i < top; i++){
4782 Pair p = (Pair) list [i];
4785 v = current_block.AddVariable (type, (string) p.First, l);
4789 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4797 Location l = (Location) $1;
4799 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4801 current_block.AddStatement (f);
4802 while (current_block.Implicit)
4803 current_block = current_block.Parent;
4805 current_block = current_block.Parent;
4809 fixed_pointer_declarators
4810 : fixed_pointer_declarator {
4811 ArrayList declarators = new ArrayList (4);
4813 declarators.Add ($1);
4816 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4818 ArrayList declarators = (ArrayList) $1;
4820 declarators.Add ($3);
4825 fixed_pointer_declarator
4826 : IDENTIFIER ASSIGN expression
4828 LocatedToken lt = (LocatedToken) $1;
4829 // FIXME: keep location
4830 $$ = new Pair (lt.Value, $3);
4834 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
4840 : LOCK OPEN_PARENS expression CLOSE_PARENS
4846 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
4851 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4853 Block assign_block = new Block (current_block);
4854 current_block = assign_block;
4856 if ($3 is DictionaryEntry){
4857 DictionaryEntry de = (DictionaryEntry) $3;
4858 Location l = (Location) $1;
4860 Expression type = (Expression) de.Key;
4861 ArrayList var_declarators = (ArrayList) de.Value;
4863 ArrayList vars = new ArrayList (4);
4865 foreach (VariableDeclaration decl in var_declarators){
4867 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4870 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
4873 if (decl.expression_or_array_initializer is Expression){
4874 expr = (Expression) decl.expression_or_array_initializer;
4876 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4878 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4881 expr = new ArrayCreation (type, "", init, decl.Location);
4884 LocalVariableReference var;
4886 // Get a writable reference to this read-only variable.
4887 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4889 // This is so that it is not a warning on using variables
4892 vars.Add (new DictionaryEntry (var, expr));
4894 // Assign a = new Assign (var, expr, decl.Location);
4895 // assign_block.AddStatement (new StatementExpression (a));
4898 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4899 // It can be referred to as $5 below.
4900 $$ = new DictionaryEntry (type, vars);
4907 Using u = new Using ($5, (Statement) $6, (Location) $1);
4908 current_block.AddStatement (u);
4909 while (current_block.Implicit)
4910 current_block = current_block.Parent;
4912 current_block = current_block.Parent;
4916 resource_acquisition
4917 : local_variable_declaration
4924 // A class used to pass around variable declarations and constants
4926 public class VariableDeclaration {
4927 public string identifier;
4928 public object expression_or_array_initializer;
4929 public Location Location;
4930 public Attributes OptAttributes;
4931 public string DocComment;
4933 public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
4935 this.identifier = lt.Value;
4936 this.expression_or_array_initializer = eoai;
4937 this.Location = lt.Location;
4938 this.OptAttributes = opt_attrs;
4941 public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
4947 // A class used to hold info about an indexer declarator
4949 public class IndexerDeclaration {
4950 public Expression type;
4951 public MemberName interface_type;
4952 public Parameters param_list;
4953 public Location location;
4955 public IndexerDeclaration (Expression type, MemberName interface_type,
4956 Parameters param_list, Location loc)
4959 this.interface_type = interface_type;
4960 this.param_list = param_list;
4961 this.location = loc;
4966 // We use this when we do not have an object in advance that is an IIteratorContainer
4968 public class SimpleIteratorContainer : IIteratorContainer {
4971 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4976 public static SimpleIteratorContainer GetSimple () {
4977 Simple.Yields = false;
4981 public void SetYields () { Yields = true; }
4985 // A class used to hold info about an operator declarator
4987 public class OperatorDeclaration {
4988 public Operator.OpType optype;
4989 public Expression ret_type, arg1type, arg2type;
4990 public string arg1name, arg2name;
4991 public Location location;
4993 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4994 Expression arg1type, string arg1name,
4995 Expression arg2type, string arg2name, Location location)
4998 this.ret_type = ret_type;
4999 this.arg1type = arg1type;
5000 this.arg1name = arg1name;
5001 this.arg2type = arg2type;
5002 this.arg2name = arg2name;
5003 this.location = location;
5008 void Error_ExpectingTypeName (Expression expr)
5010 if (expr is Invocation){
5011 Report.Error (1002, expr.Location, "Expecting `;'");
5013 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
5017 TypeContainer pop_current_class ()
5019 TypeContainer retval = current_class;
5021 current_class = current_class.Parent;
5022 current_container = current_container.Parent;
5024 if (current_class != current_container) {
5025 if (!(current_class is ClassPart) ||
5026 ((ClassPart) current_class).PartialContainer != current_container)
5027 throw new InternalErrorException ();
5028 } else if (current_container is ClassPart)
5029 current_container = ((ClassPart) current_class).PartialContainer;
5035 // Given the @class_name name, it creates a fully qualified name
5036 // based on the containing declaration space
5039 MakeName (MemberName class_name)
5041 Namespace ns = current_namespace.NS;
5043 if (current_container.Name == ""){
5045 return new MemberName (ns.MemberName, class_name);
5049 return new MemberName (current_container.MemberName, class_name);
5053 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
5055 Block implicit_block;
5056 ArrayList inits = null;
5059 // We use the `Used' property to check whether statements
5060 // have been added to the current block. If so, we need
5061 // to create another block to contain the new declaration
5062 // otherwise, as an optimization, we use the same block to
5063 // add the declaration.
5065 // FIXME: A further optimization is to check if the statements
5066 // that were added were added as part of the initialization
5067 // below. In which case, no other statements have been executed
5068 // and we might be able to reduce the number of blocks for
5069 // situations like this:
5071 // int j = 1; int k = j + 1;
5073 if (current_block.Used)
5074 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
5076 implicit_block = current_block;
5078 foreach (VariableDeclaration decl in variable_declarators){
5080 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
5081 if (decl.expression_or_array_initializer != null){
5083 inits = new ArrayList (4);
5090 return implicit_block;
5092 foreach (VariableDeclaration decl in inits){
5096 if (decl.expression_or_array_initializer is Expression){
5097 expr = (Expression) decl.expression_or_array_initializer;
5100 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
5102 expr = new ArrayCreation (type, "", init, decl.Location);
5105 LocalVariableReference var;
5106 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
5108 assign = new Assign (var, expr, decl.Location);
5110 implicit_block.AddStatement (new StatementExpression (assign));
5113 return implicit_block;
5116 Block declare_local_constants (Expression type, ArrayList declarators)
5118 Block implicit_block;
5120 if (current_block.Used)
5121 implicit_block = new Block (current_block, Block.Flags.Implicit);
5123 implicit_block = current_block;
5125 foreach (VariableDeclaration decl in declarators){
5126 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
5129 return implicit_block;
5132 void CheckAttributeTarget (string a, Location l)
5136 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
5140 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
5146 void CheckUnaryOperator (Operator.OpType op, Location l)
5150 case Operator.OpType.LogicalNot:
5151 case Operator.OpType.OnesComplement:
5152 case Operator.OpType.Increment:
5153 case Operator.OpType.Decrement:
5154 case Operator.OpType.True:
5155 case Operator.OpType.False:
5156 case Operator.OpType.Addition:
5157 case Operator.OpType.Subtraction:
5162 Report.Error (1019, l, "Overloadable unary operator expected");
5168 void CheckBinaryOperator (Operator.OpType op, Location l)
5172 case Operator.OpType.Addition:
5173 case Operator.OpType.Subtraction:
5174 case Operator.OpType.Multiply:
5175 case Operator.OpType.Division:
5176 case Operator.OpType.Modulus:
5177 case Operator.OpType.BitwiseAnd:
5178 case Operator.OpType.BitwiseOr:
5179 case Operator.OpType.ExclusiveOr:
5180 case Operator.OpType.LeftShift:
5181 case Operator.OpType.RightShift:
5182 case Operator.OpType.Equality:
5183 case Operator.OpType.Inequality:
5184 case Operator.OpType.GreaterThan:
5185 case Operator.OpType.LessThan:
5186 case Operator.OpType.GreaterThanOrEqual:
5187 case Operator.OpType.LessThanOrEqual:
5191 Report.Error (1020, l, "Overloadable binary operator expected");
5197 void syntax_error (Location l, string msg)
5199 Report.Error (1003, l, "Syntax error, " + msg);
5202 void note (string s)
5204 // Used to put annotations
5209 public Tokenizer Lexer {
5215 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
5217 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
5218 this.name = file.Name;
5220 current_container = RootContext.Tree.Types;
5221 // TODO: Make RootContext.Tree.Types a PartialContainer.
5222 current_class = current_container;
5223 current_container.NamespaceEntry = current_namespace;
5224 oob_stack = new Stack ();
5225 switch_stack = new Stack ();
5227 lexer = new Tokenizer (reader, file, defines);
5230 public void parse ()
5233 if (yacc_verbose_flag > 1)
5234 yyparse (lexer, new yydebug.yyDebugSimple ());
5237 Tokenizer tokenizer = lexer as Tokenizer;
5238 tokenizer.cleanup ();
5239 } catch (Exception e){
5241 // Removed for production use, use parser verbose to get the output.
5243 // Console.WriteLine (e);
5244 Report.Error (-25, lexer.Location, "Parsing error");
5245 if (yacc_verbose_flag > 0)
5246 Console.WriteLine (e);
5249 RootContext.Tree.Types.NamespaceEntry = null;
5252 void CheckToken (int error, int yyToken, string msg, Location loc)
5254 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
5255 Report.Error (error, loc, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
5258 Report.Error (error, loc, msg);
5261 void CheckIdentifierToken (int yyToken, Location loc)
5263 CheckToken (1041, yyToken, "Identifier expected", loc);
5266 string ConsumeStoredComment ()
5268 string s = tmpComment;
5270 Lexer.doc_state = XmlCommentState.Allowed;
5274 Location GetLocation (object obj)
5276 if (obj is MemberCore)
5277 return ((MemberCore) obj).Location;
5278 if (obj is MemberName)
5279 return ((MemberName) obj).Location;
5280 if (obj is LocatedToken)
5281 return ((LocatedToken) obj).Location;
5282 if (obj is Location)
5283 return (Location) obj;
5284 return lexer.Location;