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 $$ = new ComposedCast ((Expression) $1, (string) $2);
2748 | non_expression_type STAR
2750 $$ = new ComposedCast ((Expression) $1, "*");
2752 | expression rank_specifiers
2754 $$ = new ComposedCast ((Expression) $1, (string) $2);
2758 $$ = new ComposedCast ((Expression) $1, "*");
2762 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2763 // through this different path
2765 | multiplicative_expression STAR
2767 $$ = new ComposedCast ((Expression) $1, "*");
2774 ArrayList types = new ArrayList (4);
2779 | type_list COMMA type
2781 ArrayList types = (ArrayList) $1;
2789 * replaces all the productions for isolating the various
2790 * simple types, but we need this to reuse it easily in local_variable_type
2793 : OBJECT { $$ = TypeManager.system_object_expr; }
2794 | STRING { $$ = TypeManager.system_string_expr; }
2795 | BOOL { $$ = TypeManager.system_boolean_expr; }
2796 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2797 | FLOAT { $$ = TypeManager.system_single_expr; }
2798 | DOUBLE { $$ = TypeManager.system_double_expr; }
2803 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2804 | BYTE { $$ = TypeManager.system_byte_expr; }
2805 | SHORT { $$ = TypeManager.system_int16_expr; }
2806 | USHORT { $$ = TypeManager.system_uint16_expr; }
2807 | INT { $$ = TypeManager.system_int32_expr; }
2808 | UINT { $$ = TypeManager.system_uint32_expr; }
2809 | LONG { $$ = TypeManager.system_int64_expr; }
2810 | ULONG { $$ = TypeManager.system_uint64_expr; }
2811 | CHAR { $$ = TypeManager.system_char_expr; }
2812 | VOID { $$ = TypeManager.system_void_expr; }
2816 : type rank_specifiers opt_nullable
2818 string rank_specifiers = (string) $2;
2820 rank_specifiers += "?";
2822 $$ = new ComposedCast ((Expression) $1, rank_specifiers);
2827 // Expressions, section 7.5
2836 MemberName mn = (MemberName) $1;
2837 $$ = mn.GetTypeExpression ();
2839 | IDENTIFIER DOUBLE_COLON IDENTIFIER
2841 LocatedToken lt1 = (LocatedToken) $1;
2842 LocatedToken lt2 = (LocatedToken) $3;
2843 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, lt2.Location);
2845 | parenthesized_expression
2846 | default_value_expression
2848 | invocation_expression
2852 | post_increment_expression
2853 | post_decrement_expression
2857 | checked_expression
2858 | unchecked_expression
2859 | pointer_member_access
2860 | anonymous_method_expression
2867 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
2868 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); }
2869 | NULL { $$ = new NullLiteral (lexer.Location); }
2873 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
2874 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
2875 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
2880 object v = lexer.Value;
2883 $$ = new IntLiteral ((int) v, lexer.Location);
2884 } else if (v is uint)
2885 $$ = new UIntLiteral ((UInt32) v, lexer.Location);
2887 $$ = new LongLiteral ((Int64) v, lexer.Location);
2888 else if (v is ulong)
2889 $$ = new ULongLiteral ((UInt64) v, lexer.Location);
2891 Console.WriteLine ("OOPS. Unexpected result from scanner");
2896 : TRUE { $$ = new BoolLiteral (true, lexer.Location); }
2897 | FALSE { $$ = new BoolLiteral (false, lexer.Location); }
2900 parenthesized_expression_0
2901 : OPEN_PARENS expression CLOSE_PARENS
2904 lexer.Deambiguate_CloseParens ();
2905 // After this, the next token returned is one of
2906 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2907 // or CLOSE_PARENS_MINUS.
2911 parenthesized_expression
2912 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2916 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2918 // If a parenthesized expression is followed by a minus, we need to wrap
2919 // the expression inside a ParenthesizedExpression for the CS0075 check
2920 // in Binary.DoResolve().
2921 $$ = new ParenthesizedExpression ((Expression) $1);
2926 : primary_expression DOT IDENTIFIER opt_type_argument_list
2928 LocatedToken lt = (LocatedToken) $3;
2929 $$ = new MemberAccess ((Expression) $1, lt.Value,
2930 (TypeArguments) $4, lt.Location);
2932 | predefined_type DOT IDENTIFIER opt_type_argument_list
2934 LocatedToken lt = (LocatedToken) $3;
2935 $$ = new MemberAccess ((Expression) $1, lt.Value,
2936 (TypeArguments) $4, lt.Location);
2944 invocation_expression
2945 : primary_expression {
2946 $$ = lexer.Location;
2947 } OPEN_PARENS opt_argument_list CLOSE_PARENS
2950 Location l = (Location) $3;
2951 Report.Error (1, l, "Parse error");
2953 $$ = new Invocation ((Expression) $1, (ArrayList) $4);
2955 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2957 $$ = new Invocation ((Expression) $1, new ArrayList ());
2959 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2961 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
2963 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2965 ArrayList args = new ArrayList (1);
2967 $$ = new Invocation ((Expression) $1, args);
2969 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2971 ArrayList args = ((ArrayList) $4);
2973 $$ = new Invocation ((Expression) $1, args);
2978 : /* empty */ { $$ = null; }
2985 ArrayList list = new ArrayList (4);
2989 | argument_list COMMA argument
2991 ArrayList list = (ArrayList) $1;
2995 | argument_list error {
2996 CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
3004 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
3006 | non_simple_argument
3013 : REF variable_reference
3015 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
3017 | OUT variable_reference
3019 $$ = new Argument ((Expression) $2, Argument.AType.Out);
3021 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
3023 ArrayList list = (ArrayList) $3;
3024 Argument[] args = new Argument [list.Count];
3025 list.CopyTo (args, 0);
3027 Expression expr = new Arglist (args, (Location) $1);
3028 $$ = new Argument (expr, Argument.AType.Expression);
3032 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
3037 : expression { note ("section 5.4"); $$ = $1; }
3041 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
3043 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
3045 | primary_expression rank_specifiers
3047 // So the super-trick is that primary_expression
3048 // can only be either a SimpleName or a MemberAccess.
3049 // The MemberAccess case arises when you have a fully qualified type-name like :
3051 // SimpleName is when you have
3054 Expression expr = (Expression) $1;
3055 if (expr is ComposedCast){
3056 $$ = new ComposedCast (expr, (string) $2);
3057 } else if (!(expr is SimpleName || expr is MemberAccess || expr is ConstructedType || expr is QualifiedAliasMember)){
3058 Error_ExpectingTypeName (expr);
3059 $$ = TypeManager.system_object_expr;
3062 // So we extract the string corresponding to the SimpleName
3065 $$ = new ComposedCast (expr, (string) $2);
3073 ArrayList list = new ArrayList (4);
3077 | expression_list COMMA expression
3079 ArrayList list = (ArrayList) $1;
3088 $$ = new This (current_block, (Location) $1);
3093 : BASE DOT IDENTIFIER
3095 LocatedToken lt = (LocatedToken) $3;
3096 $$ = new BaseAccess (lt.Value, lt.Location);
3098 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
3100 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
3103 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
3108 post_increment_expression
3109 : primary_expression OP_INC
3111 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
3112 (Expression) $1, (Location) $2);
3116 post_decrement_expression
3117 : primary_expression OP_DEC
3119 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
3120 (Expression) $1, (Location) $2);
3125 : object_or_delegate_creation_expression
3126 | array_creation_expression
3129 object_or_delegate_creation_expression
3130 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
3132 $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
3136 array_creation_expression
3137 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
3139 opt_array_initializer
3141 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
3143 | NEW type rank_specifiers array_initializer
3145 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, (Location) $1);
3149 Report.Error (1031, (Location) $1, "Type expected");
3154 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
3170 opt_rank_specifier_or_nullable
3179 | opt_nullable rank_specifiers
3186 | opt_nullable rank_specifiers INTERR
3189 $$ = "?" + $2 + "?";
3196 : rank_specifier opt_rank_specifier
3198 $$ = (string) $2 + (string) $1;
3203 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3205 $$ = "[" + (string) $2 + "]";
3225 | dim_separators COMMA
3227 $$ = (string) $1 + ",";
3231 opt_array_initializer
3243 : OPEN_BRACE CLOSE_BRACE
3245 ArrayList list = new ArrayList (4);
3248 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3250 $$ = (ArrayList) $2;
3254 variable_initializer_list
3255 : variable_initializer
3257 ArrayList list = new ArrayList (4);
3261 | variable_initializer_list COMMA variable_initializer
3263 ArrayList list = (ArrayList) $1;
3269 void_pointer_expression
3270 : void_pointer_expression STAR
3272 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3276 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3281 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
3283 $$ = new TypeOfVoid ((Location) $1);
3285 | TYPEOF OPEN_PARENS void_pointer_expression CLOSE_PARENS
3287 $$ = new TypeOf ((Expression) $3, (Location) $1);
3289 | TYPEOF OPEN_PARENS
3291 lexer.TypeOfParsing = true;
3295 lexer.TypeOfParsing = false;
3296 $$ = new TypeOf ((Expression) $4, lexer.Location);
3301 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3302 $$ = new SizeOf ((Expression) $3, (Location) $1);
3307 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3309 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3313 unchecked_expression
3314 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3316 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3320 pointer_member_access
3321 : primary_expression OP_PTR IDENTIFIER
3324 LocatedToken lt = (LocatedToken) $3;
3326 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
3327 $$ = new MemberAccess (deref, lt.Value, lt.Location);
3331 anonymous_method_expression
3332 : DELEGATE opt_anonymous_method_signature
3334 oob_stack.Push (current_local_parameters);
3335 current_local_parameters = (Parameters)$2;
3337 // Force the next block to be created as a ToplevelBlock
3338 oob_stack.Push (current_block);
3339 oob_stack.Push (top_current_block);
3340 current_block = null;
3344 Location loc = (Location) $1;
3345 top_current_block = (Block) oob_stack.Pop ();
3346 current_block = (Block) oob_stack.Pop ();
3347 if (RootContext.Version == LanguageVersion.ISO_1){
3348 Report.FeatureIsNotStandardized (loc, "anonymous methods");
3351 ToplevelBlock anon_block = (ToplevelBlock) $4;
3353 anon_block.Parent = current_block;
3354 $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block,
3357 current_local_parameters = (Parameters) oob_stack.Pop ();
3361 opt_anonymous_method_signature
3362 : /* empty */ { $$ = null; }
3363 | anonymous_method_signature
3366 anonymous_method_signature
3367 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3370 $$ = Parameters.EmptyReadOnlyParameters;
3372 ArrayList par_list = (ArrayList) $2;
3373 Parameter [] pars = new Parameter [par_list.Count];
3374 par_list.CopyTo (pars);
3375 $$ = new Parameters (pars, null);
3380 opt_anonymous_method_parameter_list
3381 : /* empty */ { $$ = null; }
3382 | anonymous_method_parameter_list { $$ = $1; }
3385 anonymous_method_parameter_list
3386 : anonymous_method_parameter
3388 ArrayList a = new ArrayList (4);
3392 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3394 ArrayList a = (ArrayList) $1;
3400 anonymous_method_parameter
3401 : opt_parameter_modifier type IDENTIFIER {
3402 LocatedToken lt = (LocatedToken) $3;
3403 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3405 | PARAMS type IDENTIFIER {
3406 Report.Error (1670, ((LocatedToken) $3).Location, "The `params' modifier is not allowed in anonymous method declaration");
3411 default_value_expression
3412 : DEFAULT_OPEN_PARENS type CLOSE_PARENS
3414 $$ = new DefaultValueExpression ((Expression) $2, lexer.Location);
3419 : primary_expression
3420 | BANG prefixed_unary_expression
3422 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3424 | TILDE prefixed_unary_expression
3426 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3432 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3434 $$ = new Cast ((Expression) $1, (Expression) $3);
3436 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3438 $$ = new Cast ((Expression) $1, (Expression) $3);
3444 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3446 // TODO: wrong location
3447 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3452 // The idea to split this out is from Rhys' grammar
3453 // to solve the problem with casts.
3455 prefixed_unary_expression
3457 | PLUS prefixed_unary_expression
3459 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3461 | MINUS prefixed_unary_expression
3463 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3465 | OP_INC prefixed_unary_expression
3467 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3468 (Expression) $2, (Location) $1);
3470 | OP_DEC prefixed_unary_expression
3472 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3473 (Expression) $2, (Location) $1);
3475 | STAR prefixed_unary_expression
3477 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, (Location) $1);
3479 | BITWISE_AND prefixed_unary_expression
3481 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3485 pre_increment_expression
3486 : OP_INC prefixed_unary_expression
3488 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3489 (Expression) $2, (Location) $1);
3493 pre_decrement_expression
3494 : OP_DEC prefixed_unary_expression
3496 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3497 (Expression) $2, (Location) $1);
3501 multiplicative_expression
3502 : prefixed_unary_expression
3503 | multiplicative_expression STAR prefixed_unary_expression
3505 $$ = new Binary (Binary.Operator.Multiply,
3506 (Expression) $1, (Expression) $3);
3508 | multiplicative_expression DIV prefixed_unary_expression
3510 $$ = new Binary (Binary.Operator.Division,
3511 (Expression) $1, (Expression) $3);
3513 | multiplicative_expression PERCENT prefixed_unary_expression
3515 $$ = new Binary (Binary.Operator.Modulus,
3516 (Expression) $1, (Expression) $3);
3521 : multiplicative_expression
3522 | additive_expression PLUS multiplicative_expression
3524 $$ = new Binary (Binary.Operator.Addition,
3525 (Expression) $1, (Expression) $3);
3527 | additive_expression MINUS multiplicative_expression
3529 $$ = new Binary (Binary.Operator.Subtraction,
3530 (Expression) $1, (Expression) $3);
3535 : additive_expression
3536 | shift_expression OP_SHIFT_LEFT additive_expression
3538 $$ = new Binary (Binary.Operator.LeftShift,
3539 (Expression) $1, (Expression) $3);
3541 | shift_expression OP_SHIFT_RIGHT additive_expression
3543 $$ = new Binary (Binary.Operator.RightShift,
3544 (Expression) $1, (Expression) $3);
3555 lexer.PutbackNullable ();
3560 nullable_type_or_conditional
3563 if (((bool) $2) && ($1 is ComposedCast))
3564 $$ = ((ComposedCast) $1).RemoveNullable ();
3570 relational_expression
3572 | relational_expression OP_LT shift_expression
3574 $$ = new Binary (Binary.Operator.LessThan,
3575 (Expression) $1, (Expression) $3);
3577 | relational_expression OP_GT shift_expression
3579 $$ = new Binary (Binary.Operator.GreaterThan,
3580 (Expression) $1, (Expression) $3);
3582 | relational_expression OP_LE shift_expression
3584 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3585 (Expression) $1, (Expression) $3);
3587 | relational_expression OP_GE shift_expression
3589 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3590 (Expression) $1, (Expression) $3);
3592 | relational_expression IS
3595 } nullable_type_or_conditional
3597 $$ = new Is ((Expression) $1, (Expression) $4, (Location) $2);
3599 | relational_expression AS
3602 } nullable_type_or_conditional
3604 $$ = new As ((Expression) $1, (Expression) $4, (Location) $2);
3609 : relational_expression
3610 | equality_expression OP_EQ relational_expression
3612 $$ = new Binary (Binary.Operator.Equality,
3613 (Expression) $1, (Expression) $3);
3615 | equality_expression OP_NE relational_expression
3617 $$ = new Binary (Binary.Operator.Inequality,
3618 (Expression) $1, (Expression) $3);
3623 : equality_expression
3624 | and_expression BITWISE_AND equality_expression
3626 $$ = new Binary (Binary.Operator.BitwiseAnd,
3627 (Expression) $1, (Expression) $3);
3631 exclusive_or_expression
3633 | exclusive_or_expression CARRET and_expression
3635 $$ = new Binary (Binary.Operator.ExclusiveOr,
3636 (Expression) $1, (Expression) $3);
3640 inclusive_or_expression
3641 : exclusive_or_expression
3642 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3644 $$ = new Binary (Binary.Operator.BitwiseOr,
3645 (Expression) $1, (Expression) $3);
3649 conditional_and_expression
3650 : inclusive_or_expression
3651 | conditional_and_expression OP_AND inclusive_or_expression
3653 $$ = new Binary (Binary.Operator.LogicalAnd,
3654 (Expression) $1, (Expression) $3);
3658 conditional_or_expression
3659 : conditional_and_expression
3660 | conditional_or_expression OP_OR conditional_and_expression
3662 $$ = new Binary (Binary.Operator.LogicalOr,
3663 (Expression) $1, (Expression) $3);
3667 conditional_expression
3668 : conditional_or_expression
3669 | conditional_or_expression INTERR expression COLON expression
3671 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
3673 | conditional_or_expression INTERR INTERR expression
3675 $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $4, lexer.Location);
3677 // We'll be resolved into a `parenthesized_expression_0' later on.
3678 | conditional_or_expression INTERR CLOSE_PARENS
3680 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
3681 lexer.PutbackCloseParens ();
3685 assignment_expression
3686 : prefixed_unary_expression ASSIGN expression
3688 $$ = new Assign ((Expression) $1, (Expression) $3);
3690 | prefixed_unary_expression OP_MULT_ASSIGN expression
3692 $$ = new CompoundAssign (
3693 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
3695 | prefixed_unary_expression OP_DIV_ASSIGN expression
3697 $$ = new CompoundAssign (
3698 Binary.Operator.Division, (Expression) $1, (Expression) $3);
3700 | prefixed_unary_expression OP_MOD_ASSIGN expression
3702 $$ = new CompoundAssign (
3703 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
3705 | prefixed_unary_expression OP_ADD_ASSIGN expression
3707 $$ = new CompoundAssign (
3708 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
3710 | prefixed_unary_expression OP_SUB_ASSIGN expression
3712 $$ = new CompoundAssign (
3713 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3715 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3717 $$ = new CompoundAssign (
3718 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
3720 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3722 $$ = new CompoundAssign (
3723 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
3725 | prefixed_unary_expression OP_AND_ASSIGN expression
3727 $$ = new CompoundAssign (
3728 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
3730 | prefixed_unary_expression OP_OR_ASSIGN expression
3732 $$ = new CompoundAssign (
3733 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
3735 | prefixed_unary_expression OP_XOR_ASSIGN expression
3737 $$ = new CompoundAssign (
3738 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
3743 : conditional_expression
3744 | assignment_expression
3764 lexer.ConstraintsParsing = true;
3768 MemberName name = MakeName ((MemberName) $6);
3769 int mod_flags = (int) $2;
3772 ClassPart part = PartialContainer.CreatePart (
3773 current_namespace, current_class, name, mod_flags,
3774 (Attributes) $1, Kind.Class, (Location) $3);
3776 current_container = part.PartialContainer;
3777 current_class = part;
3779 if ((mod_flags & Modifiers.STATIC) != 0) {
3780 current_class = new StaticClass (
3781 current_namespace, current_class, name,
3782 mod_flags, (Attributes) $1);
3784 current_class = new Class (
3785 current_namespace, current_class, name,
3786 mod_flags, (Attributes) $1);
3789 current_container.AddClassOrStruct (current_class);
3790 current_container = current_class;
3791 RootContext.Tree.RecordDecl (current_namespace.NS, name, current_class);
3795 opt_type_parameter_constraints_clauses
3797 lexer.ConstraintsParsing = false;
3800 if (current_class.Name == "System.Object") {
3801 Report.Error (537, current_class.Location,
3802 "The class System.Object cannot have a base " +
3803 "class or implement an interface.");
3805 current_class.Bases = (ArrayList) $8;
3808 current_class.SetParameterInfo ((ArrayList) $9);
3810 if (RootContext.Documentation != null) {
3811 current_class.DocComment = Lexer.consume_doc_comment ();
3812 Lexer.doc_state = XmlCommentState.Allowed;
3817 if (RootContext.Documentation != null)
3818 Lexer.doc_state = XmlCommentState.Allowed;
3822 $$ = pop_current_class ();
3830 { $$ = $1; } // location
3834 : /* empty */ { $$ = (int) 0; }
3840 | modifiers modifier
3845 if ((m1 & m2) != 0) {
3846 Location l = lexer.Location;
3847 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
3849 $$ = (int) (m1 | m2);
3854 : NEW { $$ = Modifiers.NEW; }
3855 | PUBLIC { $$ = Modifiers.PUBLIC; }
3856 | PROTECTED { $$ = Modifiers.PROTECTED; }
3857 | INTERNAL { $$ = Modifiers.INTERNAL; }
3858 | PRIVATE { $$ = Modifiers.PRIVATE; }
3859 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3860 | SEALED { $$ = Modifiers.SEALED; }
3861 | STATIC { $$ = Modifiers.STATIC; }
3862 | READONLY { $$ = Modifiers.READONLY; }
3863 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3864 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3865 | EXTERN { $$ = Modifiers.EXTERN; }
3866 | VOLATILE { $$ = Modifiers.VOLATILE; }
3867 | UNSAFE { $$ = Modifiers.UNSAFE; }
3871 : /* empty */ { $$ = null; }
3872 | class_base { $$ = $1; }
3876 : COLON type_list { $$ = $2; }
3879 opt_type_parameter_constraints_clauses
3880 : /* empty */ { $$ = null; }
3881 | type_parameter_constraints_clauses
3885 type_parameter_constraints_clauses
3886 : type_parameter_constraints_clause {
3887 ArrayList constraints = new ArrayList (1);
3888 constraints.Add ($1);
3891 | type_parameter_constraints_clauses type_parameter_constraints_clause {
3892 ArrayList constraints = (ArrayList) $1;
3894 constraints.Add ($2);
3899 type_parameter_constraints_clause
3900 : WHERE IDENTIFIER COLON type_parameter_constraints {
3901 LocatedToken lt = (LocatedToken) $2;
3902 $$ = new Constraints (lt.Value, (ArrayList) $4, lt.Location);
3906 type_parameter_constraints
3907 : type_parameter_constraint {
3908 ArrayList constraints = new ArrayList (1);
3909 constraints.Add ($1);
3912 | type_parameter_constraints COMMA type_parameter_constraint {
3913 ArrayList constraints = (ArrayList) $1;
3915 constraints.Add ($3);
3920 type_parameter_constraint
3922 | NEW OPEN_PARENS CLOSE_PARENS {
3923 $$ = SpecialConstraint.Constructor;
3926 $$ = SpecialConstraint.ReferenceType;
3929 $$ = SpecialConstraint.ValueType;
3938 // A block is "contained" on the following places:
3940 // property_declaration as part of the accessor body (get/set)
3941 // operator_declaration
3942 // constructor_declaration
3943 // destructor_declaration
3944 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3949 if (current_block == null){
3950 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, (Location) $1);
3951 top_current_block = current_block;
3953 current_block = new Block (current_block, (Location) $1, Location.Null);
3956 opt_statement_list CLOSE_BRACE
3958 while (current_block.Implicit)
3959 current_block = current_block.Parent;
3961 current_block.SetEndLocation ((Location) $4);
3962 current_block = current_block.Parent;
3963 if (current_block == null)
3964 top_current_block = null;
3975 | statement_list statement
3979 : declaration_statement
3981 if ($1 != null && (Block) $1 != current_block){
3982 current_block.AddStatement ((Statement) $1);
3983 current_block = (Block) $1;
3986 | valid_declaration_statement
3988 current_block.AddStatement ((Statement) $1);
3993 valid_declaration_statement
3996 | expression_statement
3997 | selection_statement
3998 | iteration_statement
4002 | unchecked_statement
4010 : valid_declaration_statement
4011 | declaration_statement
4013 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4018 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4026 $$ = EmptyStatement.Value;
4033 LocatedToken lt = (LocatedToken) $1;
4034 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
4036 if (current_block.AddLabel (lt.Value, labeled, lt.Location))
4037 current_block.AddStatement (labeled);
4042 declaration_statement
4043 : local_variable_declaration SEMICOLON
4046 DictionaryEntry de = (DictionaryEntry) $1;
4047 Expression e = (Expression) de.Key;
4049 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
4053 | local_constant_declaration SEMICOLON
4056 DictionaryEntry de = (DictionaryEntry) $1;
4058 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
4064 * The following is from Rhys' grammar:
4065 * > Types in local variable declarations must be recognized as
4066 * > expressions to prevent reduce/reduce errors in the grammar.
4067 * > The expressions are converted into types during semantic analysis.
4070 : primary_expression opt_rank_specifier_or_nullable
4072 // FIXME: Do something smart here regarding the composition of the type.
4074 // Ok, the above "primary_expression" is there to get rid of
4075 // both reduce/reduce and shift/reduces in the grammar, it should
4076 // really just be "type_name". If you use type_name, a reduce/reduce
4077 // creeps up. If you use namespace_or_type_name (which is all we need
4078 // really) two shift/reduces appear.
4081 // So the super-trick is that primary_expression
4082 // can only be either a SimpleName or a MemberAccess.
4083 // The MemberAccess case arises when you have a fully qualified type-name like :
4085 // SimpleName is when you have
4088 Expression expr = (Expression) $1;
4089 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType || expr is QualifiedAliasMember)) {
4090 Error_ExpectingTypeName (expr);
4094 // So we extract the string corresponding to the SimpleName
4098 if ((string) $2 == "")
4101 $$ = new ComposedCast ((Expression) $1, (string) $2);
4104 | builtin_types opt_rank_specifier_or_nullable
4106 if ((string) $2 == "")
4109 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
4113 local_variable_pointer_type
4114 : primary_expression STAR
4116 Expression expr = (Expression) $1;
4118 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType || expr is QualifiedAliasMember)) {
4119 Error_ExpectingTypeName (expr);
4123 $$ = new ComposedCast ((Expression) $1, "*");
4125 | builtin_types STAR
4127 $$ = new ComposedCast ((Expression) $1, "*");
4131 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
4133 | local_variable_pointer_type STAR
4135 $$ = new ComposedCast ((Expression) $1, "*");
4139 local_variable_declaration
4140 : local_variable_type variable_declarators
4143 $$ = new DictionaryEntry ($1, $2);
4147 | local_variable_pointer_type opt_rank_specifier_or_nullable variable_declarators
4152 if ((string) $2 == "")
4153 t = (Expression) $1;
4155 t = new ComposedCast ((Expression) $1, (string) $2);
4156 $$ = new DictionaryEntry (t, $3);
4162 local_constant_declaration
4163 : CONST local_variable_type constant_declarators
4166 $$ = new DictionaryEntry ($2, $3);
4172 expression_statement
4173 : statement_expression SEMICOLON
4180 // We have to do the wrapping here and not in the case above,
4181 // because statement_expression is used for example in for_statement
4183 statement_expression
4184 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
4185 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
4186 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
4187 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
4188 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
4189 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
4190 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
4192 Report.Error (1002, GetLocation ($1), "Expecting `;'");
4197 object_creation_expression
4198 : object_or_delegate_creation_expression
4199 { note ("complain if this is a delegate maybe?"); }
4208 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
4211 Location l = (Location) $1;
4213 $$ = new If ((Expression) $3, (Statement) $5, l);
4215 if (RootContext.WarningLevel >= 3){
4216 // FIXME: location for warning should be loc property of $5.
4217 if ($5 == EmptyStatement.Value)
4218 Report.Warning (642, l, "Possible mistaken empty statement");
4222 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
4223 embedded_statement ELSE embedded_statement
4225 Location l = (Location) $1;
4227 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
4229 if (RootContext.WarningLevel >= 3){
4230 // FIXME: location for warning should be loc property of $5 and $7.
4231 if ($5 == EmptyStatement.Value)
4232 Report.Warning (642, l, "Possible mistaken empty statement");
4233 if ($7 == EmptyStatement.Value)
4234 Report.Warning (642, l, "Possible mistaken empty statement");
4240 : SWITCH OPEN_PARENS
4242 switch_stack.Push (current_block);
4244 expression CLOSE_PARENS
4247 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
4248 current_block = (Block) switch_stack.Pop ();
4264 Report.Error (1522, lexer.Location, "Empty switch block");
4272 ArrayList sections = new ArrayList (4);
4277 | switch_sections switch_section
4279 ArrayList sections = (ArrayList) $1;
4289 current_block = current_block.CreateSwitchBlock (lexer.Location);
4293 Block topmost = current_block;
4295 while (topmost.Implicit)
4296 topmost = topmost.Parent;
4297 $$ = new SwitchSection ((ArrayList) $1, topmost);
4304 ArrayList labels = new ArrayList (4);
4309 | switch_labels switch_label
4311 ArrayList labels = (ArrayList) ($1);
4319 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
4320 | DEFAULT COLON { $$ = new SwitchLabel (null, (Location) $2); }
4323 1523, GetLocation ($1),
4324 "The keyword case or default must precede code in switch block");
4336 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
4338 Location l = (Location) $1;
4339 $$ = new While ((Expression) $3, (Statement) $5, l);
4344 : DO embedded_statement
4345 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
4347 Location l = (Location) $1;
4349 $$ = new Do ((Statement) $2, (Expression) $5, l);
4355 opt_for_initializer SEMICOLON
4357 Block assign_block = new Block (current_block);
4358 current_block = assign_block;
4360 if ($3 is DictionaryEntry){
4361 DictionaryEntry de = (DictionaryEntry) $3;
4363 Expression type = (Expression) de.Key;
4364 ArrayList var_declarators = (ArrayList) de.Value;
4366 foreach (VariableDeclaration decl in var_declarators){
4370 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4374 Location l = lexer.Location;
4376 if (decl.expression_or_array_initializer is Expression){
4377 expr = (Expression) decl.expression_or_array_initializer;
4378 } else if (decl.expression_or_array_initializer == null) {
4381 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4382 expr = new ArrayCreation (type, "", init, decl.Location);
4385 LocalVariableReference var;
4386 var = new LocalVariableReference (assign_block, decl.identifier, l);
4389 Assign a = new Assign (var, expr, decl.Location);
4391 assign_block.AddStatement (new StatementExpression (a));
4395 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4396 // This can be referred to as $5 below.
4402 opt_for_condition SEMICOLON
4403 opt_for_iterator CLOSE_PARENS
4406 Location l = (Location) $1;
4408 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4410 current_block.AddStatement (f);
4411 while (current_block.Implicit)
4412 current_block = current_block.Parent;
4414 current_block = current_block.Parent;
4419 : /* empty */ { $$ = EmptyStatement.Value; }
4424 : local_variable_declaration
4425 | statement_expression_list
4429 : /* empty */ { $$ = null; }
4430 | boolean_expression
4434 : /* empty */ { $$ = EmptyStatement.Value; }
4439 : statement_expression_list
4442 statement_expression_list
4443 : statement_expression
4445 // CHANGE: was `null'
4446 Block b = new Block (current_block, Block.Flags.Implicit);
4448 b.AddStatement ((Statement) $1);
4451 | statement_expression_list COMMA statement_expression
4453 Block b = (Block) $1;
4455 b.AddStatement ((Statement) $3);
4461 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4463 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4466 | FOREACH OPEN_PARENS type IDENTIFIER IN
4467 expression CLOSE_PARENS
4469 Block foreach_block = new Block (current_block);
4470 current_block = foreach_block;
4472 LocatedToken lt = (LocatedToken) $4;
4473 Location l = lt.Location;
4476 vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4478 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4480 // Get a writable reference to this read-only variable.
4482 // Note that the $$ here refers to the value of _this_ code block,
4483 // not the value of the LHS non-terminal. This can be referred to as $8 below.
4484 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
4491 LocalVariableReference v = (LocalVariableReference) $8;
4492 Location l = (Location) $1;
4495 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
4496 current_block.AddStatement (f);
4499 while (current_block.Implicit)
4500 current_block = current_block.Parent;
4502 current_block = current_block.Parent;
4508 | continue_statement
4518 $$ = new Break ((Location) $1);
4523 : CONTINUE SEMICOLON
4525 $$ = new Continue ((Location) $1);
4530 : GOTO IDENTIFIER SEMICOLON
4532 LocatedToken lt = (LocatedToken) $2;
4533 $$ = new Goto (lt.Value, lt.Location);
4535 | GOTO CASE constant_expression SEMICOLON
4537 $$ = new GotoCase ((Expression) $3, (Location) $1);
4539 | GOTO DEFAULT SEMICOLON
4541 $$ = new GotoDefault ((Location) $1);
4546 : RETURN opt_expression SEMICOLON
4548 $$ = new Return ((Expression) $2, (Location) $1);
4553 : THROW opt_expression SEMICOLON
4555 $$ = new Throw ((Expression) $2, (Location) $1);
4560 : IDENTIFIER RETURN expression SEMICOLON
4562 LocatedToken lt = (LocatedToken) $1;
4563 string s = lt.Value;
4565 Report.Error (1003, lt.Location, "; expected");
4568 if (RootContext.Version == LanguageVersion.ISO_1){
4569 Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4572 if (iterator_container == null){
4573 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4576 iterator_container.SetYields ();
4577 $$ = new Yield ((Expression) $3, lt.Location);
4580 | IDENTIFIER RETURN SEMICOLON
4582 Report.Error (1627, (Location) $2, "Expression expected after yield return");
4585 | IDENTIFIER BREAK SEMICOLON
4587 LocatedToken lt = (LocatedToken) $1;
4588 string s = lt.Value;
4590 Report.Error (1003, lt.Location, "; expected");
4593 if (RootContext.Version == LanguageVersion.ISO_1){
4594 Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4597 if (iterator_container == null){
4598 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4601 iterator_container.SetYields ();
4602 $$ = new YieldBreak (lt.Location);
4613 : TRY block catch_clauses
4617 ArrayList c = (ArrayList)$3;
4618 for (int i = 0; i < c.Count; ++i) {
4619 Catch cc = (Catch) c [i];
4621 if (i != c.Count - 1)
4622 Report.Error (1017, cc.loc, "Try statement already has an empty catch block");
4629 // Now s contains the list of specific catch clauses
4630 // and g contains the general one.
4632 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4634 | TRY block opt_catch_clauses FINALLY block
4637 ArrayList s = new ArrayList (4);
4638 ArrayList catch_list = (ArrayList) $3;
4640 if (catch_list != null){
4641 foreach (Catch cc in catch_list) {
4649 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4653 Report.Error (1524, (Location) $1, "Expected catch or finally");
4659 : /* empty */ { $$ = null; }
4666 ArrayList l = new ArrayList (4);
4671 | catch_clauses catch_clause
4673 ArrayList l = (ArrayList) $1;
4681 : /* empty */ { $$ = null; }
4686 : CATCH opt_catch_args
4688 Expression type = null;
4691 DictionaryEntry cc = (DictionaryEntry) $2;
4692 type = (Expression) cc.Key;
4693 LocatedToken lt = (LocatedToken) cc.Value;
4696 ArrayList one = new ArrayList (4);
4698 one.Add (new VariableDeclaration (lt, null));
4700 current_block = new Block (current_block);
4701 Block b = declare_local_variables (type, one, lt.Location);
4706 Expression type = null;
4710 DictionaryEntry cc = (DictionaryEntry) $2;
4711 type = (Expression) cc.Key;
4712 LocatedToken lt = (LocatedToken) cc.Value;
4716 while (current_block.Implicit)
4717 current_block = current_block.Parent;
4718 current_block = current_block.Parent;
4722 $$ = new Catch (type, id, (Block) $4, ((Block) $4).loc);
4727 : /* empty */ { $$ = null; }
4732 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4734 $$ = new DictionaryEntry ($2, $3);
4742 $$ = new Checked ((Block) $2);
4749 $$ = new Unchecked ((Block) $2);
4756 RootContext.CheckUnsafeOption ((Location) $1);
4758 $$ = new Unsafe ((Block) $3);
4764 type fixed_pointer_declarators
4767 ArrayList list = (ArrayList) $4;
4768 Expression type = (Expression) $3;
4769 Location l = (Location) $1;
4770 int top = list.Count;
4772 Block assign_block = new Block (current_block);
4773 current_block = assign_block;
4775 for (int i = 0; i < top; i++){
4776 Pair p = (Pair) list [i];
4779 v = current_block.AddVariable (type, (string) p.First, l);
4783 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4791 Location l = (Location) $1;
4793 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4795 current_block.AddStatement (f);
4796 while (current_block.Implicit)
4797 current_block = current_block.Parent;
4799 current_block = current_block.Parent;
4803 fixed_pointer_declarators
4804 : fixed_pointer_declarator {
4805 ArrayList declarators = new ArrayList (4);
4807 declarators.Add ($1);
4810 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4812 ArrayList declarators = (ArrayList) $1;
4814 declarators.Add ($3);
4819 fixed_pointer_declarator
4820 : IDENTIFIER ASSIGN expression
4822 LocatedToken lt = (LocatedToken) $1;
4823 // FIXME: keep location
4824 $$ = new Pair (lt.Value, $3);
4828 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
4834 : LOCK OPEN_PARENS expression CLOSE_PARENS
4840 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
4845 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4847 Block assign_block = new Block (current_block);
4848 current_block = assign_block;
4850 if ($3 is DictionaryEntry){
4851 DictionaryEntry de = (DictionaryEntry) $3;
4852 Location l = (Location) $1;
4854 Expression type = (Expression) de.Key;
4855 ArrayList var_declarators = (ArrayList) de.Value;
4857 ArrayList vars = new ArrayList (4);
4859 foreach (VariableDeclaration decl in var_declarators){
4861 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4864 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
4867 if (decl.expression_or_array_initializer is Expression){
4868 expr = (Expression) decl.expression_or_array_initializer;
4870 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4872 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4875 expr = new ArrayCreation (type, "", init, decl.Location);
4878 LocalVariableReference var;
4880 // Get a writable reference to this read-only variable.
4881 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4883 // This is so that it is not a warning on using variables
4886 vars.Add (new DictionaryEntry (var, expr));
4888 // Assign a = new Assign (var, expr, decl.Location);
4889 // assign_block.AddStatement (new StatementExpression (a));
4892 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4893 // It can be referred to as $5 below.
4894 $$ = new DictionaryEntry (type, vars);
4901 Using u = new Using ($5, (Statement) $6, (Location) $1);
4902 current_block.AddStatement (u);
4903 while (current_block.Implicit)
4904 current_block = current_block.Parent;
4906 current_block = current_block.Parent;
4910 resource_acquisition
4911 : local_variable_declaration
4918 // A class used to pass around variable declarations and constants
4920 public class VariableDeclaration {
4921 public string identifier;
4922 public object expression_or_array_initializer;
4923 public Location Location;
4924 public Attributes OptAttributes;
4925 public string DocComment;
4927 public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
4929 this.identifier = lt.Value;
4930 this.expression_or_array_initializer = eoai;
4931 this.Location = lt.Location;
4932 this.OptAttributes = opt_attrs;
4935 public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
4941 // A class used to hold info about an indexer declarator
4943 public class IndexerDeclaration {
4944 public Expression type;
4945 public MemberName interface_type;
4946 public Parameters param_list;
4947 public Location location;
4949 public IndexerDeclaration (Expression type, MemberName interface_type,
4950 Parameters param_list, Location loc)
4953 this.interface_type = interface_type;
4954 this.param_list = param_list;
4955 this.location = loc;
4960 // We use this when we do not have an object in advance that is an IIteratorContainer
4962 public class SimpleIteratorContainer : IIteratorContainer {
4965 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4970 public static SimpleIteratorContainer GetSimple () {
4971 Simple.Yields = false;
4975 public void SetYields () { Yields = true; }
4979 // A class used to hold info about an operator declarator
4981 public class OperatorDeclaration {
4982 public Operator.OpType optype;
4983 public Expression ret_type, arg1type, arg2type;
4984 public string arg1name, arg2name;
4985 public Location location;
4987 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4988 Expression arg1type, string arg1name,
4989 Expression arg2type, string arg2name, Location location)
4992 this.ret_type = ret_type;
4993 this.arg1type = arg1type;
4994 this.arg1name = arg1name;
4995 this.arg2type = arg2type;
4996 this.arg2name = arg2name;
4997 this.location = location;
5002 void Error_ExpectingTypeName (Expression expr)
5004 if (expr is Invocation){
5005 Report.Error (1002, expr.Location, "Expecting `;'");
5007 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
5011 TypeContainer pop_current_class ()
5013 TypeContainer retval = current_class;
5015 current_class = current_class.Parent;
5016 current_container = current_container.Parent;
5018 if (current_class != current_container) {
5019 if (!(current_class is ClassPart) ||
5020 ((ClassPart) current_class).PartialContainer != current_container)
5021 throw new InternalErrorException ();
5022 } else if (current_container is ClassPart)
5023 current_container = ((ClassPart) current_class).PartialContainer;
5029 // Given the @class_name name, it creates a fully qualified name
5030 // based on the containing declaration space
5033 MakeName (MemberName class_name)
5035 Namespace ns = current_namespace.NS;
5037 if (current_container.Name == ""){
5039 return new MemberName (ns.MemberName, class_name);
5043 return new MemberName (current_container.MemberName, class_name);
5047 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
5049 Block implicit_block;
5050 ArrayList inits = null;
5053 // We use the `Used' property to check whether statements
5054 // have been added to the current block. If so, we need
5055 // to create another block to contain the new declaration
5056 // otherwise, as an optimization, we use the same block to
5057 // add the declaration.
5059 // FIXME: A further optimization is to check if the statements
5060 // that were added were added as part of the initialization
5061 // below. In which case, no other statements have been executed
5062 // and we might be able to reduce the number of blocks for
5063 // situations like this:
5065 // int j = 1; int k = j + 1;
5067 if (current_block.Used)
5068 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
5070 implicit_block = current_block;
5072 foreach (VariableDeclaration decl in variable_declarators){
5074 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
5075 if (decl.expression_or_array_initializer != null){
5077 inits = new ArrayList (4);
5084 return implicit_block;
5086 foreach (VariableDeclaration decl in inits){
5090 if (decl.expression_or_array_initializer is Expression){
5091 expr = (Expression) decl.expression_or_array_initializer;
5094 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
5096 expr = new ArrayCreation (type, "", init, decl.Location);
5099 LocalVariableReference var;
5100 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
5102 assign = new Assign (var, expr, decl.Location);
5104 implicit_block.AddStatement (new StatementExpression (assign));
5107 return implicit_block;
5110 Block declare_local_constants (Expression type, ArrayList declarators)
5112 Block implicit_block;
5114 if (current_block.Used)
5115 implicit_block = new Block (current_block, Block.Flags.Implicit);
5117 implicit_block = current_block;
5119 foreach (VariableDeclaration decl in declarators){
5120 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
5123 return implicit_block;
5126 void CheckAttributeTarget (string a, Location l)
5130 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
5134 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
5140 void CheckUnaryOperator (Operator.OpType op, Location l)
5144 case Operator.OpType.LogicalNot:
5145 case Operator.OpType.OnesComplement:
5146 case Operator.OpType.Increment:
5147 case Operator.OpType.Decrement:
5148 case Operator.OpType.True:
5149 case Operator.OpType.False:
5150 case Operator.OpType.Addition:
5151 case Operator.OpType.Subtraction:
5156 Report.Error (1019, l, "Overloadable unary operator expected");
5162 void CheckBinaryOperator (Operator.OpType op, Location l)
5166 case Operator.OpType.Addition:
5167 case Operator.OpType.Subtraction:
5168 case Operator.OpType.Multiply:
5169 case Operator.OpType.Division:
5170 case Operator.OpType.Modulus:
5171 case Operator.OpType.BitwiseAnd:
5172 case Operator.OpType.BitwiseOr:
5173 case Operator.OpType.ExclusiveOr:
5174 case Operator.OpType.LeftShift:
5175 case Operator.OpType.RightShift:
5176 case Operator.OpType.Equality:
5177 case Operator.OpType.Inequality:
5178 case Operator.OpType.GreaterThan:
5179 case Operator.OpType.LessThan:
5180 case Operator.OpType.GreaterThanOrEqual:
5181 case Operator.OpType.LessThanOrEqual:
5185 Report.Error (1020, l, "Overloadable binary operator expected");
5191 void syntax_error (Location l, string msg)
5193 Report.Error (1003, l, "Syntax error, " + msg);
5196 void note (string s)
5198 // Used to put annotations
5203 public Tokenizer Lexer {
5209 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
5211 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
5212 this.name = file.Name;
5214 current_container = RootContext.Tree.Types;
5215 // TODO: Make RootContext.Tree.Types a PartialContainer.
5216 current_class = current_container;
5217 current_container.NamespaceEntry = current_namespace;
5218 oob_stack = new Stack ();
5219 switch_stack = new Stack ();
5221 lexer = new Tokenizer (reader, file, defines);
5224 public void parse ()
5227 if (yacc_verbose_flag > 1)
5228 yyparse (lexer, new yydebug.yyDebugSimple ());
5231 Tokenizer tokenizer = lexer as Tokenizer;
5232 tokenizer.cleanup ();
5233 } catch (Exception e){
5235 // Removed for production use, use parser verbose to get the output.
5237 // Console.WriteLine (e);
5238 Report.Error (-25, lexer.Location, "Parsing error");
5239 if (yacc_verbose_flag > 0)
5240 Console.WriteLine (e);
5243 RootContext.Tree.Types.NamespaceEntry = null;
5246 void CheckToken (int error, int yyToken, string msg, Location loc)
5248 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
5249 Report.Error (error, loc, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
5252 Report.Error (error, loc, msg);
5255 void CheckIdentifierToken (int yyToken, Location loc)
5257 CheckToken (1041, yyToken, "Identifier expected", loc);
5260 string ConsumeStoredComment ()
5262 string s = tmpComment;
5264 Lexer.doc_state = XmlCommentState.Allowed;
5268 Location GetLocation (object obj)
5270 if (obj is MemberCore)
5271 return ((MemberCore) obj).Location;
5272 if (obj is MemberName)
5273 return ((MemberName) obj).Location;
5274 if (obj is LocatedToken)
5275 return ((LocatedToken) obj).Location;
5276 if (obj is Location)
5277 return (Location) obj;
5278 return lexer.Location;