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 definition is enabled
99 bool global_attrs_enabled = true;
103 %token NONE /* This token is never returned by our lexer */
104 %token ERROR // This is used not by the parser, but by the tokenizer.
108 *These are the C# keywords
195 /* C# keywords which are not really keywords */
201 /* C# single character operators/punctuation. */
202 %token OPEN_BRACE "{"
203 %token CLOSE_BRACE "}"
204 %token OPEN_BRACKET "["
205 %token CLOSE_BRACKET "]"
206 %token OPEN_PARENS "("
207 %token CLOSE_PARENS ")"
219 %token OP_GENERICS_LT "<"
221 %token OP_GENERICS_GT ">"
222 %token BITWISE_AND "&"
223 %token BITWISE_OR "|"
230 /* C# multi-character operators. */
233 %token OP_SHIFT_LEFT "<<"
234 %token OP_SHIFT_RIGHT ">>"
241 %token OP_MULT_ASSIGN "*="
242 %token OP_DIV_ASSIGN "/="
243 %token OP_MOD_ASSIGN "%="
244 %token OP_ADD_ASSIGN "+="
245 %token OP_SUB_ASSIGN "-="
246 %token OP_SHIFT_LEFT_ASSIGN "<<="
247 %token OP_SHIFT_RIGHT_ASSIGN ">>="
248 %token OP_AND_ASSIGN "&="
249 %token OP_XOR_ASSIGN "^="
250 %token OP_OR_ASSIGN "|="
254 %token LITERAL_INTEGER "int literal"
255 %token LITERAL_FLOAT "float literal"
256 %token LITERAL_DOUBLE "double literal"
257 %token LITERAL_DECIMAL "decimal literal"
258 %token LITERAL_CHARACTER "character literal"
259 %token LITERAL_STRING "string literal"
262 %token CLOSE_PARENS_CAST
263 %token CLOSE_PARENS_NO_CAST
264 %token CLOSE_PARENS_OPEN_PARENS
265 %token CLOSE_PARENS_MINUS
266 %token DEFAULT_OPEN_PARENS
267 %token GENERIC_DIMENSION
269 /* Add precedence rules to solve dangling else s/r conflict */
278 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
280 %left STAR DIV PERCENT
281 %right BANG CARRET UMINUS
282 %nonassoc OP_INC OP_DEC
284 %left OPEN_BRACKET OPEN_BRACE
288 %start compilation_unit
292 : outer_declarations opt_EOF
293 | outer_declarations global_attributes opt_EOF
294 | global_attributes opt_EOF
295 | opt_EOF /* allow empty files */
301 Lexer.check_incorrect_doc_comment ();
305 Lexer.check_incorrect_doc_comment ();
311 | outer_declarations outer_declaration
316 | namespace_member_declaration
321 | using_directives using_directive
325 : using_alias_directive
327 if (RootContext.Documentation != null)
328 Lexer.doc_state = XmlCommentState.Allowed;
330 | using_namespace_directive
332 if (RootContext.Documentation != null)
333 Lexer.doc_state = XmlCommentState.Allowed;
337 using_alias_directive
338 : USING IDENTIFIER ASSIGN
339 namespace_or_type_name SEMICOLON
341 MemberName name = (MemberName) $4;
342 current_namespace.UsingAlias ((string) $2, name.GetTypeExpression (lexer.Location), lexer.Location);
345 CheckIdentifierToken (yyToken);
349 using_namespace_directive
350 : USING namespace_name SEMICOLON
352 MemberName ns_name = (MemberName) $2;
353 current_namespace.Using (ns_name.GetTypeExpression (lexer.Location), lexer.Location);
358 // Strictly speaking, namespaces don't have attributes but
359 // we parse global attributes along with namespace declarations and then
362 namespace_declaration
363 : opt_attributes NAMESPACE namespace_or_type_name
366 Report.Error(1518, Lexer.Location, "Attributes cannot be applied to namespaces."
367 + " Expected class, delegate, enum, interface, or struct");
370 MemberName name = (MemberName) $3;
372 if (name.TypeArguments != null)
373 syntax_error (lexer.Location, "namespace name expected");
374 else if ((current_namespace.Parent != null) && (name.Left != null)) {
375 Report.Error (134, lexer.Location,
376 "Cannot use qualified namespace names in nested " +
377 "namespace declarations");
380 current_namespace = new NamespaceEntry (
381 current_namespace, file, name.GetName (), lexer.Location);
383 namespace_body opt_semicolon
385 current_namespace = current_namespace.Parent;
400 : namespace_or_type_name {
401 MemberName name = (MemberName) $1;
403 if (name.TypeArguments != null)
404 syntax_error (lexer.Location, "namespace name expected");
413 if (RootContext.Documentation != null)
414 Lexer.doc_state = XmlCommentState.Allowed;
417 opt_namespace_member_declarations
428 opt_namespace_member_declarations
430 | namespace_member_declarations
433 namespace_member_declarations
434 : namespace_member_declaration
435 | namespace_member_declarations namespace_member_declaration
438 namespace_member_declaration
445 Class c = (Class) $1;
446 mod_flags = c.ModFlags;
448 } else if ($1 is Struct){
449 Struct s = (Struct) $1;
450 mod_flags = s.ModFlags;
455 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
457 1527, lexer.Location,
458 "Namespace elements cant be explicitly " +
459 "declared private or protected in `" + name + "'");
461 current_namespace.DeclarationFound = true;
463 | namespace_declaration {
464 current_namespace.DeclarationFound = true;
467 | field_declaration {
468 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
470 | method_declaration {
471 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
478 | interface_declaration
480 | delegate_declaration
482 // Enable this when we have handled all errors, because this acts as a generic fallback
485 // Console.WriteLine ("Token=" + yyToken);
486 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
498 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
506 global_attrs_enabled = false;
511 global_attrs_enabled = false;
520 ArrayList sect = (ArrayList) $1;
522 if (global_attrs_enabled) {
523 if (current_attr_target == "module") {
524 CodeGen.Module.AddAttributes (sect);
526 } else if (current_attr_target == "assembly") {
527 CodeGen.Assembly.AddAttributes (sect);
530 $$ = new Attributes (sect);
533 if (RootContext.Documentation != null) {
534 Lexer.check_incorrect_doc_comment ();
536 XmlCommentState.Allowed;
540 $$ = new Attributes (sect);
542 current_attr_target = null;
544 | attribute_sections attribute_section
546 Attributes attrs = $1 as Attributes;
547 ArrayList sect = (ArrayList) $2;
549 if (global_attrs_enabled) {
550 if (current_attr_target == "module") {
551 CodeGen.Module.AddAttributes (sect);
553 } else if (current_attr_target == "assembly") {
554 CodeGen.Assembly.AddAttributes (sect);
558 attrs = new Attributes (sect);
560 attrs.AddAttributes (sect);
564 attrs = new Attributes (sect);
566 attrs.AddAttributes (sect);
569 current_attr_target = null;
574 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
578 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
584 attribute_target_specifier
585 : attribute_target COLON
587 current_attr_target = (string)$1;
595 CheckAttributeTarget ((string) $1);
598 | EVENT { $$ = "event"; }
599 | RETURN { $$ = "return"; }
605 ArrayList attrs = new ArrayList (4);
611 | attribute_list COMMA attribute
613 ArrayList attrs = (ArrayList) $1;
625 opt_attribute_arguments
627 Location loc = (Location) $2;
628 MemberName mname = (MemberName) $1;
629 if (mname.IsGeneric) {
630 Report.Error (404, lexer.Location,
631 "'<' unexpected: attributes cannot be generic");
634 MemberName left = mname.Left;
635 string identifier = mname.Name;
637 Expression left_expr = left == null ? null : left.GetTypeExpression (loc);
639 if (current_attr_target == "assembly" || current_attr_target == "module")
640 $$ = new GlobalAttribute (current_container, current_attr_target,
641 left_expr, identifier, (ArrayList) $3, loc);
643 $$ = new Attribute (current_attr_target, left_expr, identifier, (ArrayList) $3, loc);
648 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
651 opt_attribute_arguments
652 : /* empty */ { $$ = null; }
653 | OPEN_PARENS attribute_arguments CLOSE_PARENS
661 : opt_positional_argument_list
666 ArrayList args = new ArrayList (4);
672 | positional_argument_list COMMA named_argument_list
674 ArrayList args = new ArrayList (4);
680 | named_argument_list
682 ArrayList args = new ArrayList (4);
691 opt_positional_argument_list
692 : /* empty */ { $$ = null; }
693 | positional_argument_list
696 positional_argument_list
699 ArrayList args = new ArrayList (4);
700 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
704 | positional_argument_list COMMA expression
706 ArrayList args = (ArrayList) $1;
707 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
716 ArrayList args = new ArrayList (4);
721 | named_argument_list COMMA named_argument
723 ArrayList args = (ArrayList) $1;
728 | named_argument_list COMMA expression
730 Report.Error (1016, lexer.Location, "Named attribute argument expected");
736 : IDENTIFIER ASSIGN expression
738 $$ = new DictionaryEntry (
740 new Argument ((Expression) $3, Argument.AType.Expression));
746 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
749 opt_class_member_declarations
751 | class_member_declarations
754 class_member_declarations
755 : class_member_declaration
756 | class_member_declarations
757 class_member_declaration
760 class_member_declaration
761 : constant_declaration // done
762 | field_declaration // done
763 | method_declaration // done
764 | property_declaration // done
765 | event_declaration // done
766 | indexer_declaration // done
767 | operator_declaration // done
768 | constructor_declaration // done
769 | destructor_declaration // done
779 MemberName name = MakeName ((MemberName) $5);
780 bool partial = (bool) $3;
783 ClassPart part = PartialContainer.CreatePart (
784 current_namespace, current_container, name, (int) $2,
785 (Attributes) $1, Kind.Struct, lexer.Location);
787 current_container = part.PartialContainer;
788 current_class = part;
790 current_class = new Struct (
791 current_namespace, current_container, name, (int) $2,
792 (Attributes) $1, lexer.Location);
794 current_container = current_class;
795 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
798 lexer.ConstraintsParsing = true;
801 opt_type_parameter_constraints_clauses
803 lexer.ConstraintsParsing = false;
806 current_class.Bases = (ArrayList) $7;
808 current_class.SetParameterInfo ((ArrayList) $8);
810 if (RootContext.Documentation != null)
811 current_class.DocComment = Lexer.consume_doc_comment ();
813 current_class.Register ();
817 if (RootContext.Documentation != null)
818 Lexer.doc_state = XmlCommentState.Allowed;
824 current_container = current_container.Parent;
825 current_class = current_container;
827 | opt_attributes opt_modifiers opt_partial STRUCT error {
828 CheckIdentifierToken (yyToken);
835 if (RootContext.Documentation != null)
836 Lexer.doc_state = XmlCommentState.Allowed;
838 opt_struct_member_declarations CLOSE_BRACE
841 opt_struct_member_declarations
843 | struct_member_declarations
846 struct_member_declarations
847 : struct_member_declaration
848 | struct_member_declarations struct_member_declaration
851 struct_member_declaration
852 : constant_declaration
855 | property_declaration
857 | indexer_declaration
858 | operator_declaration
859 | constructor_declaration
863 * This is only included so we can flag error 575:
864 * destructors only allowed on class types
866 | destructor_declaration
877 int modflags = (int) $2;
878 foreach (VariableDeclaration constant in (ArrayList) $5){
879 Location l = constant.Location;
880 if ((modflags & Modifiers.STATIC) != 0) {
881 Report.Error (504, l, "The constant '{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
885 Const c = new Const (
886 current_class, (Expression) $4, (string) constant.identifier,
887 (Expression) constant.expression_or_array_initializer, modflags,
890 if (RootContext.Documentation != null) {
891 c.DocComment = Lexer.consume_doc_comment ();
892 Lexer.doc_state = XmlCommentState.Allowed;
894 current_container.AddConstant (c);
900 : constant_declarator
902 ArrayList constants = new ArrayList (4);
907 | constant_declarators COMMA constant_declarator
910 ArrayList constants = (ArrayList) $1;
917 : IDENTIFIER ASSIGN constant_expression
919 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
923 // A const field requires a value to be provided
924 Report.Error (145, lexer.Location, "A const field requires a value to be provided");
936 Expression type = (Expression) $3;
939 foreach (VariableDeclaration var in (ArrayList) $4){
940 Field field = new Field (current_class, type, mod, var.identifier,
941 var.expression_or_array_initializer,
942 (Attributes) $1, var.Location);
944 if (RootContext.Documentation != null) {
945 field.DocComment = Lexer.consume_doc_comment ();
946 Lexer.doc_state = XmlCommentState.Allowed;
948 current_container.AddField (field);
955 fixed_variable_declarators
958 Expression type = (Expression) $4;
961 foreach (VariableDeclaration var in (ArrayList) $5) {
962 FixedField field = new FixedField (current_class, type, mod, var.identifier,
963 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
965 if (RootContext.Documentation != null) {
966 field.DocComment = Lexer.consume_doc_comment ();
967 Lexer.doc_state = XmlCommentState.Allowed;
969 current_container.AddField (field);
977 Report.Error (670, lexer.Location, "void type is not allowed for fields");
981 fixed_variable_declarators
982 : fixed_variable_declarator
984 ArrayList decl = new ArrayList (2);
988 | fixed_variable_declarators COMMA fixed_variable_declarator
990 ArrayList decls = (ArrayList) $1;
996 fixed_variable_declarator
997 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
999 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
1003 variable_declarators
1004 : variable_declarator
1006 ArrayList decl = new ArrayList (4);
1010 | variable_declarators COMMA variable_declarator
1012 ArrayList decls = (ArrayList) $1;
1019 : IDENTIFIER ASSIGN variable_initializer
1021 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
1025 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
1029 variable_initializer
1038 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
1040 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
1044 Report.Error (1575, lexer.Location, "A stackalloc expression requires [] after type");
1051 iterator_container = (IIteratorContainer) $1;
1052 if (RootContext.Documentation != null)
1053 Lexer.doc_state = XmlCommentState.NotAllowed;
1057 Method method = (Method) $1;
1058 Block b = (Block) $3;
1059 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
1062 if ((method.ModFlags & extern_abstract) == 0){
1064 501, lexer.Location, current_container.MakeName (method.Name) +
1065 "must declare a body because it is not marked abstract or extern");
1068 if ((method.ModFlags & Modifiers.EXTERN) != 0){
1070 179, lexer.Location, current_container.MakeName (method.Name) +
1071 " is declared extern, but has a body");
1075 method.Block = (ToplevelBlock) $3;
1076 current_container.AddMethod (method);
1078 current_local_parameters = null;
1079 iterator_container = null;
1081 if (RootContext.Documentation != null)
1082 Lexer.doc_state = XmlCommentState.Allowed;
1089 type namespace_or_type_name
1090 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1092 lexer.ConstraintsParsing = true;
1094 opt_type_parameter_constraints_clauses
1096 lexer.ConstraintsParsing = false;
1098 MemberName name = (MemberName) $4;
1100 if ($9 != null && name.TypeArguments == null)
1101 Report.Error (80, lexer.Location,
1102 "Contraints are not allowed on non-generic declarations");
1106 GenericMethod generic = null;
1107 if (name.TypeArguments != null) {
1108 generic = new GenericMethod (current_namespace, current_class,
1109 name, lexer.Location);
1111 generic.SetParameterInfo ((ArrayList) $9);
1114 method = new Method (current_class, generic, (Expression) $3, (int) $2, false,
1115 name, (Parameters) $6, (Attributes) $1, lexer.Location);
1117 current_local_parameters = (Parameters) $6;
1119 if (RootContext.Documentation != null)
1120 method.DocComment = Lexer.consume_doc_comment ();
1126 VOID namespace_or_type_name
1127 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1129 lexer.ConstraintsParsing = true;
1131 opt_type_parameter_constraints_clauses
1133 lexer.ConstraintsParsing = false;
1135 MemberName name = (MemberName) $4;
1137 if ($9 != null && name.TypeArguments == null)
1138 Report.Error (80, lexer.Location,
1139 "Contraints are not allowed on non-generic declarations");
1142 GenericMethod generic = null;
1143 if (name.TypeArguments != null) {
1144 generic = new GenericMethod (current_namespace, current_class,
1145 name, lexer.Location);
1147 generic.SetParameterInfo ((ArrayList) $9);
1150 method = new Method (current_class, generic, TypeManager.system_void_expr,
1151 (int) $2, false, name, (Parameters) $6, (Attributes) $1,
1154 current_local_parameters = (Parameters) $6;
1156 if (RootContext.Documentation != null)
1157 method.DocComment = Lexer.consume_doc_comment ();
1164 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1166 Report.Error (1585, lexer.Location,
1167 String.Format ("Modifier {0} should appear before type",
1168 Modifiers.Name ((int) $4)));
1169 MemberName name = (MemberName) $4;
1171 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1172 0, false, name, (Parameters) $6, (Attributes) $1,
1175 current_local_parameters = (Parameters) $6;
1177 if (RootContext.Documentation != null)
1178 method.DocComment = Lexer.consume_doc_comment ();
1186 | SEMICOLON { $$ = null; }
1189 opt_formal_parameter_list
1190 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1191 | formal_parameter_list
1194 formal_parameter_list
1197 ArrayList pars_list = (ArrayList) $1;
1199 Parameter [] pars = new Parameter [pars_list.Count];
1200 pars_list.CopyTo (pars);
1202 $$ = new Parameters (pars, null, lexer.Location);
1204 | fixed_parameters COMMA parameter_array
1206 ArrayList pars_list = (ArrayList) $1;
1208 Parameter [] pars = new Parameter [pars_list.Count];
1209 pars_list.CopyTo (pars);
1211 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
1213 | fixed_parameters COMMA ARGLIST
1215 ArrayList pars_list = (ArrayList) $1;
1217 Parameter [] pars = new Parameter [pars_list.Count];
1218 pars_list.CopyTo (pars);
1220 $$ = new Parameters (pars, true, lexer.Location);
1224 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
1228 $$ = new Parameters (null, true, lexer.Location);
1235 ArrayList pars = new ArrayList (4);
1240 | fixed_parameters COMMA fixed_parameter
1242 ArrayList pars = (ArrayList) $1;
1251 opt_parameter_modifier
1255 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
1258 opt_parameter_modifier
1261 Report.Error (1001, lexer.Location, "Identifier expected");
1265 opt_parameter_modifier
1268 CheckIdentifierToken (yyToken);
1272 opt_parameter_modifier
1278 Report.Error (241, lexer.Location, "Default parameter specifiers are not permitted");
1283 opt_parameter_modifier
1284 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1285 | parameter_modifier
1289 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1290 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1294 : opt_attributes PARAMS type IDENTIFIER
1296 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1297 note ("type must be a single-dimension array type");
1299 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1301 Report.Error (1611, lexer.Location, "The params parameter cannot be declared as ref or out");
1304 | opt_attributes PARAMS type error {
1305 CheckIdentifierToken (yyToken);
1310 property_declaration
1314 namespace_or_type_name
1316 if (RootContext.Documentation != null)
1317 tmpComment = Lexer.consume_doc_comment ();
1321 implicit_value_parameter_type = (Expression) $3;
1323 lexer.PropertyParsing = true;
1325 $$ = lexer.Location;
1327 iterator_container = SimpleIteratorContainer.GetSimple ();
1329 accessor_declarations
1331 lexer.PropertyParsing = false;
1336 Pair pair = (Pair) $8;
1337 Accessor get_block = (Accessor) pair.First;
1338 Accessor set_block = (Accessor) pair.Second;
1340 Location loc = (Location) $7;
1341 MemberName name = (MemberName) $4;
1343 if (name.TypeArguments != null)
1344 syntax_error (lexer.Location, "a property can't have type arguments");
1346 prop = new Property (current_container, (Expression) $3, (int) $2, false,
1347 name, (Attributes) $1, get_block, set_block, loc);
1348 if (SimpleIteratorContainer.Simple.Yields)
1351 current_container.AddProperty (prop);
1352 implicit_value_parameter_type = null;
1353 iterator_container = null;
1355 if (RootContext.Documentation != null)
1356 prop.DocComment = ConsumeStoredComment ();
1361 accessor_declarations
1362 : get_accessor_declaration opt_set_accessor_declaration
1364 $$ = new Pair ($1, $2);
1366 | set_accessor_declaration opt_get_accessor_declaration
1368 $$ = new Pair ($2, $1);
1372 opt_get_accessor_declaration
1373 : /* empty */ { $$ = null; }
1374 | get_accessor_declaration
1377 opt_set_accessor_declaration
1378 : /* empty */ { $$ = null; }
1379 | set_accessor_declaration
1382 get_accessor_declaration
1383 : opt_attributes opt_modifiers GET
1385 // If this is not the case, then current_local_parameters has already
1386 // been set in indexer_declaration
1387 if (parsing_indexer == false)
1388 current_local_parameters = null;
1390 current_local_parameters = indexer_parameters;
1391 lexer.PropertyParsing = false;
1395 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1396 current_local_parameters = null;
1397 lexer.PropertyParsing = true;
1399 if (RootContext.Documentation != null)
1400 if (Lexer.doc_state == XmlCommentState.Error)
1401 Lexer.doc_state = XmlCommentState.NotAllowed;
1405 set_accessor_declaration
1406 : opt_attributes opt_modifiers SET
1409 Parameter implicit_value_parameter = new Parameter (
1410 implicit_value_parameter_type, "value",
1411 Parameter.Modifier.NONE, null);
1413 if (parsing_indexer == false) {
1414 args = new Parameter [1];
1415 args [0] = implicit_value_parameter;
1416 current_local_parameters = new Parameters (args, null, lexer.Location);
1418 Parameter [] fpars = indexer_parameters.FixedParameters;
1421 int count = fpars.Length;
1423 args = new Parameter [count + 1];
1424 fpars.CopyTo (args, 0);
1425 args [count] = implicit_value_parameter;
1428 current_local_parameters = new Parameters (
1429 args, indexer_parameters.ArrayParameter, lexer.Location);
1432 lexer.PropertyParsing = false;
1436 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1437 current_local_parameters = null;
1438 lexer.PropertyParsing = true;
1440 if (RootContext.Documentation != null
1441 && Lexer.doc_state == XmlCommentState.Error)
1442 Lexer.doc_state = XmlCommentState.NotAllowed;
1448 | SEMICOLON { $$ = null; }
1451 interface_declaration
1455 INTERFACE member_name
1457 MemberName name = MakeName ((MemberName) $5);
1458 bool partial = (bool) $3;
1461 ClassPart part = PartialContainer.CreatePart (
1462 current_namespace, current_container, name, (int) $2,
1463 (Attributes) $1, Kind.Interface, lexer.Location);
1465 current_container = part.PartialContainer;
1466 current_class = part;
1468 current_class = new Interface (
1469 current_namespace, current_container, name, (int) $2,
1470 (Attributes) $1, lexer.Location);
1472 current_container = current_class;
1473 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
1476 lexer.ConstraintsParsing = true;
1479 opt_type_parameter_constraints_clauses
1481 lexer.ConstraintsParsing = false;
1484 current_class.Bases = (ArrayList) $7;
1486 current_class.SetParameterInfo ((ArrayList) $8);
1488 if (RootContext.Documentation != null) {
1489 current_class.DocComment = Lexer.consume_doc_comment ();
1490 Lexer.doc_state = XmlCommentState.Allowed;
1493 current_class.Register ();
1497 if (RootContext.Documentation != null)
1498 Lexer.doc_state = XmlCommentState.Allowed;
1504 current_container = current_container.Parent;
1505 current_class = current_container;
1507 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1508 CheckIdentifierToken (yyToken);
1514 opt_interface_member_declarations
1518 opt_interface_member_declarations
1520 | interface_member_declarations
1523 interface_member_declarations
1524 : interface_member_declaration
1525 | interface_member_declarations interface_member_declaration
1528 interface_member_declaration
1529 : interface_method_declaration
1531 Method m = (Method) $1;
1533 if (m.IsExplicitImpl)
1534 Report.Error (541, lexer.Location,
1535 "Explicit interface declaration can only be declared in a class or struct");
1537 current_container.AddMethod (m);
1539 if (RootContext.Documentation != null)
1540 Lexer.doc_state = XmlCommentState.Allowed;
1542 | interface_property_declaration
1544 Property p = (Property) $1;
1546 if (p.IsExplicitImpl)
1547 Report.Error (541, lexer.Location,
1548 "Explicit interface declaration can only be declared in a class or struct");
1550 current_container.AddProperty (p);
1552 if (RootContext.Documentation != null)
1553 Lexer.doc_state = XmlCommentState.Allowed;
1555 | interface_event_declaration
1558 Event e = (Event) $1;
1560 if (e.IsExplicitImpl)
1561 Report.Error (541, lexer.Location,
1562 "Explicit interface declaration can only be declared in a class or struct");
1564 current_container.AddEvent (e);
1567 if (RootContext.Documentation != null)
1568 Lexer.doc_state = XmlCommentState.Allowed;
1570 | interface_indexer_declaration
1572 Indexer i = (Indexer) $1;
1574 if (i.IsExplicitImpl)
1575 Report.Error (541, lexer.Location,
1576 "Explicit interface declaration can only be declared in a class or struct");
1578 current_container.AddIndexer (i);
1580 if (RootContext.Documentation != null)
1581 Lexer.doc_state = XmlCommentState.Allowed;
1583 | delegate_declaration
1585 Report.Error (524, lexer.Location, "Interfaces can not declare delegates");
1589 Report.Error (524, lexer.Location, "Interfaces can not declare classes");
1591 | struct_declaration
1593 Report.Error (524, lexer.Location, "Interfaces can not declare structures");
1597 Report.Error (524, lexer.Location, "Interfaces can not declare enumerations");
1599 | interface_declaration
1601 Report.Error (524, lexer.Location, "Interfaces can not declare interfaces");
1609 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, lexer.Location);
1614 interface_method_declaration
1615 : opt_attributes opt_new type namespace_or_type_name
1616 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1618 lexer.ConstraintsParsing = true;
1620 opt_type_parameter_constraints_clauses
1623 lexer.ConstraintsParsing = false;
1625 MemberName name = (MemberName) $4;
1627 if ($9 != null && name.TypeArguments == null)
1628 Report.Error (80, lexer.Location,
1629 "Contraints are not allowed on non-generic declarations");
1631 GenericMethod generic = null;
1632 if (name.TypeArguments != null) {
1633 generic = new GenericMethod (current_namespace, current_class,
1634 name, lexer.Location);
1636 generic.SetParameterInfo ((ArrayList) $9);
1639 $$ = new Method (current_class, generic, (Expression) $3, (int) $2, true, name,
1640 (Parameters) $6, (Attributes) $1, lexer.Location);
1641 if (RootContext.Documentation != null)
1642 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1644 | opt_attributes opt_new VOID namespace_or_type_name
1645 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1647 lexer.ConstraintsParsing = true;
1649 opt_type_parameter_constraints_clauses
1652 lexer.ConstraintsParsing = false;
1654 MemberName name = (MemberName) $4;
1656 if ($9 != null && name.TypeArguments == null)
1657 Report.Error (80, lexer.Location,
1658 "Contraints are not allowed on non-generic declarations");
1660 GenericMethod generic = null;
1661 if (name.TypeArguments != null) {
1662 generic = new GenericMethod (current_namespace, current_class,
1663 name, lexer.Location);
1665 generic.SetParameterInfo ((ArrayList) $9);
1668 $$ = new Method (current_class, generic, TypeManager.system_void_expr, (int) $2,
1669 true, name, (Parameters) $6, (Attributes) $1, lexer.Location);
1670 if (RootContext.Documentation != null)
1671 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1675 interface_property_declaration
1680 { lexer.PropertyParsing = true; }
1682 { lexer.PropertyParsing = false; }
1685 InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
1687 $$ = new Property (current_class, (Expression) $3, (int) $2, true,
1688 new MemberName ((string) $4), (Attributes) $1,
1689 pinfo.Get, pinfo.Set, lexer.Location);
1690 if (RootContext.Documentation != null)
1691 ((Property) $$).DocComment = Lexer.consume_doc_comment ();
1696 CheckIdentifierToken (yyToken);
1702 : opt_attributes opt_modifiers GET SEMICOLON
1703 { $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, (int) $2, 0, lexer.Location, lexer.Location); }
1704 | opt_attributes opt_modifiers SET SEMICOLON
1705 { $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1, 0, (int) $2, lexer.Location, lexer.Location); }
1706 | opt_attributes opt_modifiers GET SEMICOLON opt_attributes opt_modifiers SET SEMICOLON
1707 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $5, (int) $2, (int) $6, lexer.Location, lexer.Location); }
1708 | opt_attributes opt_modifiers SET SEMICOLON opt_attributes opt_modifiers GET SEMICOLON
1709 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $5, (Attributes) $1, (int) $6, (int) $2, lexer.Location, lexer.Location); }
1712 interface_event_declaration
1713 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1715 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1716 new MemberName ((string) $5), null,
1717 (Attributes) $1, lexer.Location);
1718 if (RootContext.Documentation != null)
1719 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1721 | opt_attributes opt_new EVENT type error {
1722 CheckIdentifierToken (yyToken);
1725 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1726 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1729 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE event_accessor_declarations CLOSE_BRACE {
1730 Report.Error (69, lexer.Location, "Event accessors not valid on interfaces");
1735 interface_indexer_declaration
1736 : opt_attributes opt_new type THIS
1737 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1739 { lexer.PropertyParsing = true; }
1741 { lexer.PropertyParsing = false; }
1744 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1746 $$ = new Indexer (current_class, (Expression) $3,
1747 new MemberName (TypeContainer.DefaultIndexerName),
1748 (int) $2, true, (Parameters) $6, (Attributes) $1,
1749 info.Get, info.Set, lexer.Location);
1750 if (RootContext.Documentation != null)
1751 ((Indexer) $$).DocComment = ConsumeStoredComment ();
1755 operator_declaration
1756 : opt_attributes opt_modifiers operator_declarator
1758 iterator_container = SimpleIteratorContainer.GetSimple ();
1762 OperatorDeclaration decl = (OperatorDeclaration) $3;
1764 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1766 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null);
1767 if (decl.arg2type != null)
1768 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null);
1770 Operator op = new Operator (
1771 current_class, decl.optype, decl.ret_type, (int) $2,
1772 new Parameters (param_list, null, decl.location),
1773 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1775 if (RootContext.Documentation != null) {
1776 op.DocComment = tmpComment;
1777 Lexer.doc_state = XmlCommentState.Allowed;
1780 if (SimpleIteratorContainer.Simple.Yields)
1783 // Note again, checking is done in semantic analysis
1784 current_container.AddOperator (op);
1786 current_local_parameters = null;
1787 iterator_container = null;
1793 | SEMICOLON { $$ = null; }
1796 : type OPERATOR overloadable_operator
1797 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1799 Operator.OpType op = (Operator.OpType) $3;
1800 CheckUnaryOperator (op);
1802 if (op == Operator.OpType.Addition)
1803 op = Operator.OpType.UnaryPlus;
1805 if (op == Operator.OpType.Subtraction)
1806 op = Operator.OpType.UnaryNegation;
1808 Parameter [] pars = new Parameter [1];
1809 Expression type = (Expression) $5;
1811 pars [0] = new Parameter (type, (string) $6, Parameter.Modifier.NONE, null);
1813 current_local_parameters = new Parameters (pars, null, lexer.Location);
1815 if (RootContext.Documentation != null) {
1816 tmpComment = Lexer.consume_doc_comment ();
1817 Lexer.doc_state = XmlCommentState.NotAllowed;
1820 $$ = new OperatorDeclaration (op, (Expression) $1, type, (string) $6,
1821 null, null, lexer.Location);
1823 | type OPERATOR overloadable_operator
1825 type IDENTIFIER COMMA
1829 CheckBinaryOperator ((Operator.OpType) $3);
1831 Parameter [] pars = new Parameter [2];
1833 Expression typeL = (Expression) $5;
1834 Expression typeR = (Expression) $8;
1836 pars [0] = new Parameter (typeL, (string) $6, Parameter.Modifier.NONE, null);
1837 pars [1] = new Parameter (typeR, (string) $9, Parameter.Modifier.NONE, null);
1839 current_local_parameters = new Parameters (pars, null, lexer.Location);
1841 if (RootContext.Documentation != null) {
1842 tmpComment = Lexer.consume_doc_comment ();
1843 Lexer.doc_state = XmlCommentState.NotAllowed;
1846 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1848 typeR, (string) $9, lexer.Location);
1850 | conversion_operator_declarator
1853 overloadable_operator
1855 : BANG { $$ = Operator.OpType.LogicalNot; }
1856 | TILDE { $$ = Operator.OpType.OnesComplement; }
1857 | OP_INC { $$ = Operator.OpType.Increment; }
1858 | OP_DEC { $$ = Operator.OpType.Decrement; }
1859 | TRUE { $$ = Operator.OpType.True; }
1860 | FALSE { $$ = Operator.OpType.False; }
1861 // Unary and binary:
1862 | PLUS { $$ = Operator.OpType.Addition; }
1863 | MINUS { $$ = Operator.OpType.Subtraction; }
1865 | STAR { $$ = Operator.OpType.Multiply; }
1866 | DIV { $$ = Operator.OpType.Division; }
1867 | PERCENT { $$ = Operator.OpType.Modulus; }
1868 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1869 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1870 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1871 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1872 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1873 | OP_EQ { $$ = Operator.OpType.Equality; }
1874 | OP_NE { $$ = Operator.OpType.Inequality; }
1875 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1876 | OP_LT { $$ = Operator.OpType.LessThan; }
1877 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1878 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1881 conversion_operator_declarator
1882 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1884 Parameter [] pars = new Parameter [1];
1886 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1888 current_local_parameters = new Parameters (pars, null, lexer.Location);
1890 if (RootContext.Documentation != null) {
1891 tmpComment = Lexer.consume_doc_comment ();
1892 Lexer.doc_state = XmlCommentState.NotAllowed;
1895 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1896 null, null, lexer.Location);
1898 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1900 Parameter [] pars = new Parameter [1];
1902 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1904 current_local_parameters = new Parameters (pars, null, lexer.Location);
1906 if (RootContext.Documentation != null) {
1907 tmpComment = Lexer.consume_doc_comment ();
1908 Lexer.doc_state = XmlCommentState.NotAllowed;
1911 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1912 null, null, lexer.Location);
1916 syntax_error (lexer.Location, "'operator' expected");
1920 syntax_error (lexer.Location, "'operator' expected");
1924 constructor_declaration
1927 constructor_declarator
1930 Constructor c = (Constructor) $3;
1931 c.Block = (ToplevelBlock) $4;
1932 c.OptAttributes = (Attributes) $1;
1933 c.ModFlags = (int) $2;
1935 if (RootContext.Documentation != null)
1936 c.DocComment = ConsumeStoredComment ();
1938 if (c.Name == current_container.Basename){
1939 if ((c.ModFlags & Modifiers.STATIC) != 0){
1940 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1942 515, c.Location, String.Format (
1943 "`{0}.{1}': static constructor can not have access modifiers",
1944 c.Name, current_container.Name));
1947 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1949 if (c.Initializer != null){
1952 "Static constructors can not have an explicit this or base " +
1953 "constructor invocations");
1956 if (!c.Parameters.Empty){
1958 132, c.Location, "Static constructors should not have parameters");
1961 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1964 // We let another layer check the validity of the constructor.
1965 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1968 current_container.AddConstructor (c);
1970 current_local_parameters = null;
1971 if (RootContext.Documentation != null)
1972 Lexer.doc_state = XmlCommentState.Allowed;
1976 constructor_declarator
1979 if (RootContext.Documentation != null) {
1980 tmpComment = Lexer.consume_doc_comment ();
1981 Lexer.doc_state = XmlCommentState.Allowed;
1984 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS _mark_
1986 current_local_parameters = (Parameters) $4;
1988 opt_constructor_initializer
1990 $$ = new Constructor (current_class, (string) $1, 0, (Parameters) $4,
1991 (ConstructorInitializer) $8, (Location) $6);
1997 | SEMICOLON { $$ = null; }
2000 opt_constructor_initializer
2001 : /* empty */ { $$ = null; }
2002 | constructor_initializer
2005 constructor_initializer
2006 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
2008 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
2010 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
2012 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
2015 Report.Error (1018, lexer.Location, "Keyword this or base expected");
2021 : /* EMPTY */ { $$ = 0; }
2022 | UNSAFE { $$ = Modifiers.UNSAFE; }
2023 | EXTERN { $$ = Modifiers.EXTERN; }
2026 destructor_declaration
2027 : opt_attributes opt_finalizer TILDE
2029 if (RootContext.Documentation != null) {
2030 tmpComment = Lexer.consume_doc_comment ();
2031 Lexer.doc_state = XmlCommentState.NotAllowed;
2034 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2036 if ((string) $5 != current_container.Basename){
2037 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
2038 } else if (current_container.Kind != Kind.Class){
2039 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
2041 Location l = lexer.Location;
2044 if (!RootContext.StdLib && current_container.Name == "System.Object")
2045 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2047 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2049 if ((m & Modifiers.UNSAFE) != 0){
2050 if (!RootContext.Unsafe){
2051 Report.Error (227, l,
2052 "Unsafe code requires the -unsafe command " +
2053 "line option to be specified");
2057 Method d = new Destructor (
2058 current_class, TypeManager.system_void_expr, m, "Finalize",
2059 new Parameters (null, null, l), (Attributes) $1, l);
2060 if (RootContext.Documentation != null)
2061 d.DocComment = ConsumeStoredComment ();
2063 d.Block = (ToplevelBlock) $8;
2064 current_container.AddMethod (d);
2072 EVENT type variable_declarators SEMICOLON
2074 foreach (VariableDeclaration var in (ArrayList) $5) {
2076 MemberName name = new MemberName (var.identifier);
2078 Event e = new EventField (
2079 current_class, (Expression) $4, (int) $2, false, name,
2080 var.expression_or_array_initializer, (Attributes) $1,
2083 current_container.AddEvent (e);
2085 if (RootContext.Documentation != null) {
2086 e.DocComment = Lexer.consume_doc_comment ();
2087 Lexer.doc_state = XmlCommentState.Allowed;
2093 EVENT type namespace_or_type_name
2096 implicit_value_parameter_type = (Expression) $4;
2097 lexer.EventParsing = true;
2099 event_accessor_declarations
2101 lexer.EventParsing = false;
2105 Location loc = (Location) $7;
2108 Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
2111 Pair pair = (Pair) $9;
2113 MemberName name = (MemberName) $5;
2115 if (name.TypeArguments != null)
2116 syntax_error (lexer.Location, "an event can't have type arguments");
2118 Event e = new EventProperty (
2119 current_class, (Expression) $4, (int) $2, false, name, null,
2120 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
2122 if (RootContext.Documentation != null) {
2123 e.DocComment = Lexer.consume_doc_comment ();
2124 Lexer.doc_state = XmlCommentState.Allowed;
2127 current_container.AddEvent (e);
2128 implicit_value_parameter_type = null;
2131 | opt_attributes opt_modifiers EVENT type namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS block {
2132 MemberName mn = (MemberName) $5;
2134 if (mn.Left != null)
2135 Report.Error (71, lexer.Location, "Explicit implementation of events requires property syntax");
2137 Report.Error (71, lexer.Location, "Event declaration should use property syntax");
2139 if (RootContext.Documentation != null)
2140 Lexer.doc_state = XmlCommentState.Allowed;
2144 event_accessor_declarations
2145 : add_accessor_declaration remove_accessor_declaration
2147 $$ = new Pair ($1, $2);
2149 | remove_accessor_declaration add_accessor_declaration
2151 $$ = new Pair ($2, $1);
2153 | add_accessor_declaration { $$ = null; }
2154 | remove_accessor_declaration { $$ = null; }
2157 Report.Error (1055, lexer.Location, "An add or remove accessor expected");
2163 add_accessor_declaration
2164 : opt_attributes ADD
2166 Parameter [] args = new Parameter [1];
2167 Parameter implicit_value_parameter = new Parameter (
2168 implicit_value_parameter_type, "value",
2169 Parameter.Modifier.NONE, null);
2171 args [0] = implicit_value_parameter;
2173 current_local_parameters = new Parameters (args, null, lexer.Location);
2174 lexer.EventParsing = false;
2178 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2179 lexer.EventParsing = true;
2181 | opt_attributes ADD error {
2182 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2187 remove_accessor_declaration
2188 : opt_attributes REMOVE
2190 Parameter [] args = new Parameter [1];
2191 Parameter implicit_value_parameter = new Parameter (
2192 implicit_value_parameter_type, "value",
2193 Parameter.Modifier.NONE, null);
2195 args [0] = implicit_value_parameter;
2197 current_local_parameters = new Parameters (args, null, lexer.Location);
2198 lexer.EventParsing = false;
2202 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2203 lexer.EventParsing = true;
2205 | opt_attributes REMOVE error {
2206 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2212 : opt_attributes opt_modifiers indexer_declarator
2215 IndexerDeclaration decl = (IndexerDeclaration) $3;
2217 implicit_value_parameter_type = decl.type;
2219 lexer.PropertyParsing = true;
2220 parsing_indexer = true;
2222 indexer_parameters = decl.param_list;
2224 accessor_declarations
2226 lexer.PropertyParsing = false;
2227 parsing_indexer = false;
2231 // The signature is computed from the signature of the indexer. Look
2232 // at section 3.6 on the spec
2233 Location loc = (Location) $5;
2235 IndexerDeclaration decl = (IndexerDeclaration) $3;
2236 Pair pair = (Pair) $7;
2237 Accessor get_block = (Accessor) pair.First;
2238 Accessor set_block = (Accessor) pair.Second;
2241 if (decl.interface_type != null)
2242 name = new MemberName (decl.interface_type,
2243 TypeContainer.DefaultIndexerName, null);
2245 name = new MemberName (TypeContainer.DefaultIndexerName);
2247 indexer = new Indexer (current_class, decl.type, name,
2248 (int) $2, false, decl.param_list, (Attributes) $1,
2249 get_block, set_block, loc);
2250 if (RootContext.Documentation != null)
2251 indexer.DocComment = ConsumeStoredComment ();
2253 current_container.AddIndexer (indexer);
2255 current_local_parameters = null;
2256 implicit_value_parameter_type = null;
2257 indexer_parameters = null;
2262 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2264 Parameters pars = (Parameters) $4;
2265 if (pars.HasArglist) {
2266 // "__arglist is not valid in this context"
2267 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2268 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2269 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2271 if (RootContext.Documentation != null) {
2272 tmpComment = Lexer.consume_doc_comment ();
2273 Lexer.doc_state = XmlCommentState.Allowed;
2276 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
2278 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2280 Parameters pars = (Parameters) $6;
2282 if (pars.HasArglist) {
2283 // "__arglist is not valid in this context"
2284 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2285 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2286 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2289 MemberName name = (MemberName) $2;
2291 $$ = new IndexerDeclaration ((Expression) $1, name, pars);
2293 if (RootContext.Documentation != null) {
2294 tmpComment = Lexer.consume_doc_comment ();
2295 Lexer.doc_state = XmlCommentState.Allowed;
2305 if (RootContext.Documentation != null)
2306 enumTypeComment = Lexer.consume_doc_comment ();
2311 Location enum_location = lexer.Location;
2313 MemberName full_name = MakeName (new MemberName ((string) $4));
2314 Enum e = new Enum (current_namespace, current_container, (Expression) $5, (int) $2,
2315 full_name, (Attributes) $1, enum_location);
2317 if (RootContext.Documentation != null)
2318 e.DocComment = enumTypeComment;
2320 foreach (VariableDeclaration ev in (ArrayList) $7) {
2321 e.AddEnumMember (ev.identifier,
2322 (Expression) ev.expression_or_array_initializer,
2323 ev.Location, ev.OptAttributes,
2327 string name = full_name.GetName ();
2328 current_container.AddEnum (e);
2329 RootContext.Tree.RecordDecl (name, e);
2335 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2336 | COLON type { $$ = $2; }
2342 if (RootContext.Documentation != null)
2343 Lexer.doc_state = XmlCommentState.Allowed;
2345 opt_enum_member_declarations
2347 // here will be evaluated after CLOSE_BLACE is consumed.
2348 if (RootContext.Documentation != null)
2349 Lexer.doc_state = XmlCommentState.Allowed;
2357 opt_enum_member_declarations
2358 : /* empty */ { $$ = new ArrayList (4); }
2359 | enum_member_declarations opt_comma { $$ = $1; }
2362 enum_member_declarations
2363 : enum_member_declaration
2365 ArrayList l = new ArrayList (4);
2370 | enum_member_declarations COMMA enum_member_declaration
2372 ArrayList l = (ArrayList) $1;
2380 enum_member_declaration
2381 : opt_attributes IDENTIFIER
2383 VariableDeclaration vd = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
2385 if (RootContext.Documentation != null) {
2386 vd.DocComment = Lexer.consume_doc_comment ();
2387 Lexer.doc_state = XmlCommentState.Allowed;
2392 | opt_attributes IDENTIFIER
2394 $$ = lexer.Location;
2395 if (RootContext.Documentation != null) {
2396 tmpComment = Lexer.consume_doc_comment ();
2397 Lexer.doc_state = XmlCommentState.NotAllowed;
2402 VariableDeclaration vd = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
2404 if (RootContext.Documentation != null)
2405 vd.DocComment = ConsumeStoredComment ();
2411 delegate_declaration
2414 DELEGATE type member_name
2415 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2417 Location l = lexer.Location;
2418 MemberName name = MakeName ((MemberName) $5);
2419 Delegate del = new Delegate (current_namespace, current_container, (Expression) $4,
2420 (int) $2, name, (Parameters) $7, (Attributes) $1, l);
2422 if (RootContext.Documentation != null) {
2423 del.DocComment = Lexer.consume_doc_comment ();
2424 Lexer.doc_state = XmlCommentState.Allowed;
2427 current_container.AddDelegate (del);
2428 RootContext.Tree.RecordDecl (name.GetName (true), del);
2430 current_delegate = del;
2432 lexer.ConstraintsParsing = true;
2434 opt_type_parameter_constraints_clauses
2436 lexer.ConstraintsParsing = false;
2440 current_delegate.SetParameterInfo ((ArrayList) $9);
2442 current_delegate = null;
2446 DELEGATE VOID member_name
2447 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2449 Location l = lexer.Location;
2450 MemberName name = MakeName ((MemberName) $5);
2451 Delegate del = new Delegate (
2452 current_namespace, current_container,
2453 TypeManager.system_void_expr, (int) $2, name,
2454 (Parameters) $7, (Attributes) $1, l);
2456 if (RootContext.Documentation != null) {
2457 del.DocComment = Lexer.consume_doc_comment ();
2458 Lexer.doc_state = XmlCommentState.Allowed;
2461 current_container.AddDelegate (del);
2462 RootContext.Tree.RecordDecl (name.GetName (true), del);
2464 current_delegate = del;
2466 lexer.ConstraintsParsing = true;
2468 opt_type_parameter_constraints_clauses
2470 lexer.ConstraintsParsing = false;
2474 current_delegate.SetParameterInfo ((ArrayList) $9);
2476 current_delegate = null;
2483 lexer.CheckNullable (false);
2488 lexer.CheckNullable (true);
2493 namespace_or_type_name
2495 | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list {
2496 $$ = new MemberName ((MemberName) $1, (string) $3, (TypeArguments) $4);
2501 : IDENTIFIER opt_type_argument_list {
2502 $$ = new MemberName ((string) $1, (TypeArguments) $2);
2506 opt_type_argument_list
2507 : /* empty */ { $$ = null; }
2508 | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2514 $$ = new TypeArguments ((int) $1, lexer.Location);
2520 TypeArguments type_args = new TypeArguments (lexer.Location);
2521 type_args.Add ((Expression) $1);
2524 | type_arguments COMMA type {
2525 TypeArguments type_args = (TypeArguments) $1;
2526 type_args.Add ((Expression) $3);
2532 * Before you think of adding a return_type, notice that we have been
2533 * using two rules in the places where it matters (one rule using type
2534 * and another identical one that uses VOID as the return type). This
2535 * gets rid of a shift/reduce couple
2538 : namespace_or_type_name opt_nullable
2540 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2543 $$ = new ComposedCast ((Expression) $$, "?", lexer.Location);
2545 | builtin_types opt_nullable
2548 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2558 // Note that here only unmanaged types are allowed but we
2559 // can't perform checks during this phase - we do it during
2560 // semantic analysis.
2562 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2566 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2571 : builtin_types opt_nullable
2574 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2576 | non_expression_type rank_specifier
2578 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2580 | non_expression_type STAR
2582 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2584 | expression rank_specifiers
2586 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2590 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2594 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2595 // through this different path
2597 | multiplicative_expression STAR
2599 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2606 ArrayList types = new ArrayList (4);
2611 | type_list COMMA type
2613 ArrayList types = (ArrayList) $1;
2621 * replaces all the productions for isolating the various
2622 * simple types, but we need this to reuse it easily in local_variable_type
2625 : OBJECT { $$ = TypeManager.system_object_expr; }
2626 | STRING { $$ = TypeManager.system_string_expr; }
2627 | BOOL { $$ = TypeManager.system_boolean_expr; }
2628 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2629 | FLOAT { $$ = TypeManager.system_single_expr; }
2630 | DOUBLE { $$ = TypeManager.system_double_expr; }
2635 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2636 | BYTE { $$ = TypeManager.system_byte_expr; }
2637 | SHORT { $$ = TypeManager.system_int16_expr; }
2638 | USHORT { $$ = TypeManager.system_uint16_expr; }
2639 | INT { $$ = TypeManager.system_int32_expr; }
2640 | UINT { $$ = TypeManager.system_uint32_expr; }
2641 | LONG { $$ = TypeManager.system_int64_expr; }
2642 | ULONG { $$ = TypeManager.system_uint64_expr; }
2643 | CHAR { $$ = TypeManager.system_char_expr; }
2644 | VOID { $$ = TypeManager.system_void_expr; }
2648 : type rank_specifiers opt_nullable
2650 string rank_specifiers = (string) $2;
2652 rank_specifiers += "?";
2654 $$ = new ComposedCast ((Expression) $1, rank_specifiers, lexer.Location);
2659 // Expressions, section 7.5
2668 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2670 | parenthesized_expression
2671 | default_value_expression
2673 | invocation_expression
2677 | post_increment_expression
2678 | post_decrement_expression
2682 | checked_expression
2683 | unchecked_expression
2684 | pointer_member_access
2685 | anonymous_method_expression
2692 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2693 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2694 | NULL { $$ = NullLiteral.Null; }
2698 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2699 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2700 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2705 object v = lexer.Value;
2711 $$ = IntLiteral.Zero;
2713 $$ = IntLiteral.One;
2715 $$ = new IntLiteral (i);
2716 } else if (v is uint)
2717 $$ = new UIntLiteral ((UInt32) v);
2719 $$ = new LongLiteral ((Int64) v);
2720 else if (v is ulong)
2721 $$ = new ULongLiteral ((UInt64) v);
2723 Console.WriteLine ("OOPS. Unexpected result from scanner");
2728 : TRUE { $$ = new BoolLiteral (true); }
2729 | FALSE { $$ = new BoolLiteral (false); }
2732 parenthesized_expression_0
2733 : OPEN_PARENS expression CLOSE_PARENS
2736 lexer.Deambiguate_CloseParens ();
2737 // After this, the next token returned is one of
2738 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2739 // or CLOSE_PARENS_MINUS.
2743 parenthesized_expression
2744 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2748 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2750 // If a parenthesized expression is followed by a minus, we need to wrap
2751 // the expression inside a ParenthesizedExpression for the CS0075 check
2752 // in Binary.DoResolve().
2753 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2758 : primary_expression DOT IDENTIFIER opt_type_argument_list
2760 $$ = new MemberAccess ((Expression) $1, (string) $3,
2761 (TypeArguments) $4, lexer.Location);
2763 | predefined_type DOT IDENTIFIER opt_type_argument_list
2765 $$ = new MemberAccess ((Expression) $1, (string) $3,
2766 (TypeArguments) $4, lexer.Location);
2774 invocation_expression
2775 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2778 Location l = lexer.Location;
2779 Report.Error (1, l, "Parse error");
2781 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2783 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2785 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2787 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2789 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2791 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2793 ArrayList args = new ArrayList (1);
2795 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2797 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2799 ArrayList args = ((ArrayList) $4);
2801 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2806 : /* empty */ { $$ = null; }
2813 ArrayList list = new ArrayList (4);
2817 | argument_list COMMA argument
2819 ArrayList list = (ArrayList) $1;
2823 | argument_list error {
2824 CheckToken (1026, yyToken, ", or ) expected");
2831 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2833 | non_simple_argument
2840 : REF variable_reference
2842 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2844 | OUT variable_reference
2846 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2848 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2850 ArrayList list = (ArrayList) $3;
2851 Argument[] args = new Argument [list.Count];
2852 list.CopyTo (args, 0);
2854 Expression expr = new Arglist (args, lexer.Location);
2855 $$ = new Argument (expr, Argument.AType.Expression);
2859 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2864 : expression { note ("section 5.4"); $$ = $1; }
2868 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2870 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2872 | primary_expression rank_specifiers
2874 // So the super-trick is that primary_expression
2875 // can only be either a SimpleName or a MemberAccess.
2876 // The MemberAccess case arises when you have a fully qualified type-name like :
2878 // SimpleName is when you have
2881 Expression expr = (Expression) $1;
2882 if (expr is ComposedCast){
2883 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2884 } else if (!(expr is SimpleName || expr is MemberAccess || expr is ConstructedType)){
2885 Error_ExpectingTypeName (lexer.Location, expr);
2886 $$ = TypeManager.system_object_expr;
2889 // So we extract the string corresponding to the SimpleName
2892 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2900 ArrayList list = new ArrayList (4);
2904 | expression_list COMMA expression
2906 ArrayList list = (ArrayList) $1;
2915 $$ = new This (current_block, lexer.Location);
2920 : BASE DOT IDENTIFIER
2922 $$ = new BaseAccess ((string) $3, lexer.Location);
2924 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2926 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2929 Report.Error (175, lexer.Location, "Use of keyword `base' is not valid in this context");
2934 post_increment_expression
2935 : primary_expression OP_INC
2937 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2938 (Expression) $1, lexer.Location);
2942 post_decrement_expression
2943 : primary_expression OP_DEC
2945 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2946 (Expression) $1, lexer.Location);
2951 : object_or_delegate_creation_expression
2952 | array_creation_expression
2955 object_or_delegate_creation_expression
2956 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2958 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2962 array_creation_expression
2963 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2965 opt_array_initializer
2967 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2969 | NEW type rank_specifiers array_initializer
2971 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2975 Report.Error (1031, lexer.Location, "Type expected");
2980 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2995 opt_rank_specifier_or_nullable
3004 | opt_nullable rank_specifiers
3011 | opt_nullable rank_specifiers INTERR
3014 $$ = "?" + $2 + "?";
3021 : rank_specifier opt_rank_specifier
3023 $$ = (string) $2 + (string) $1;
3028 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3030 $$ = "[" + (string) $2 + "]";
3050 | dim_separators COMMA
3052 $$ = (string) $1 + ",";
3056 opt_array_initializer
3068 : OPEN_BRACE CLOSE_BRACE
3070 ArrayList list = new ArrayList (4);
3073 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3075 $$ = (ArrayList) $2;
3079 variable_initializer_list
3080 : variable_initializer
3082 ArrayList list = new ArrayList (4);
3086 | variable_initializer_list COMMA variable_initializer
3088 ArrayList list = (ArrayList) $1;
3094 void_pointer_expression
3095 : void_pointer_expression STAR
3097 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3101 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3106 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
3108 $$ = new TypeOfVoid (lexer.Location);
3110 | TYPEOF OPEN_PARENS void_pointer_expression CLOSE_PARENS
3112 $$ = new TypeOf ((Expression) $3, lexer.Location);
3114 | TYPEOF OPEN_PARENS
3116 lexer.TypeOfParsing = true;
3120 lexer.TypeOfParsing = false;
3121 $$ = new TypeOf ((Expression) $4, lexer.Location);
3126 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3127 $$ = new SizeOf ((Expression) $3, lexer.Location);
3132 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3134 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
3138 unchecked_expression
3139 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3141 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
3145 pointer_member_access
3146 : primary_expression OP_PTR IDENTIFIER
3150 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
3151 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
3155 anonymous_method_expression
3156 : DELEGATE opt_anonymous_method_signature _mark_
3158 oob_stack.Push (current_local_parameters);
3159 current_local_parameters = (Parameters)$2;
3161 // Force the next block to be created as a ToplevelBlock
3162 oob_stack.Push (current_block);
3163 oob_stack.Push (top_current_block);
3164 current_block = null;
3168 Location loc = (Location) $3;
3169 top_current_block = (Block) oob_stack.Pop ();
3170 current_block = (Block) oob_stack.Pop ();
3171 if (RootContext.Version == LanguageVersion.ISO_1){
3172 Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
3175 ToplevelBlock anon_block = (ToplevelBlock) $5;
3177 anon_block.Parent = current_block;
3178 $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block,
3181 current_local_parameters = (Parameters) oob_stack.Pop ();
3185 opt_anonymous_method_signature
3186 : /* empty */ { $$ = null; }
3187 | anonymous_method_signature
3190 anonymous_method_signature
3191 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3194 $$ = Parameters.EmptyReadOnlyParameters;
3196 ArrayList par_list = (ArrayList) $2;
3197 Parameter [] pars = new Parameter [par_list.Count];
3198 par_list.CopyTo (pars);
3199 $$ = new Parameters (pars, null, lexer.Location);
3204 opt_anonymous_method_parameter_list
3205 : /* empty */ { $$ = null; }
3206 | anonymous_method_parameter_list { $$ = $1; }
3209 anonymous_method_parameter_list
3210 : anonymous_method_parameter
3212 ArrayList a = new ArrayList (4);
3216 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3218 ArrayList a = (ArrayList) $1;
3224 anonymous_method_parameter
3225 : opt_parameter_modifier type IDENTIFIER {
3226 $$ = new Parameter ((Expression) $2, (string) $3, (Parameter.Modifier) $1, null);
3228 | PARAMS type IDENTIFIER {
3229 Report.Error (-221, lexer.Location, "params modifier not allowed in anonymous method declaration");
3234 default_value_expression
3235 : DEFAULT_OPEN_PARENS type CLOSE_PARENS
3237 $$ = new DefaultValueExpression ((Expression) $2, lexer.Location);
3242 : primary_expression
3243 | BANG prefixed_unary_expression
3245 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
3247 | TILDE prefixed_unary_expression
3249 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
3255 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3257 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3259 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3261 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3267 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3269 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3274 // The idea to split this out is from Rhys' grammar
3275 // to solve the problem with casts.
3277 prefixed_unary_expression
3279 | PLUS prefixed_unary_expression
3281 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
3283 | MINUS prefixed_unary_expression
3285 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
3287 | OP_INC prefixed_unary_expression
3289 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3290 (Expression) $2, lexer.Location);
3292 | OP_DEC prefixed_unary_expression
3294 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3295 (Expression) $2, lexer.Location);
3297 | STAR prefixed_unary_expression
3299 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
3301 | BITWISE_AND prefixed_unary_expression
3303 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
3307 pre_increment_expression
3308 : OP_INC prefixed_unary_expression
3310 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3311 (Expression) $2, lexer.Location);
3315 pre_decrement_expression
3316 : OP_DEC prefixed_unary_expression
3318 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3319 (Expression) $2, lexer.Location);
3323 multiplicative_expression
3324 : prefixed_unary_expression
3325 | multiplicative_expression STAR prefixed_unary_expression
3327 $$ = new Binary (Binary.Operator.Multiply,
3328 (Expression) $1, (Expression) $3, lexer.Location);
3330 | multiplicative_expression DIV prefixed_unary_expression
3332 $$ = new Binary (Binary.Operator.Division,
3333 (Expression) $1, (Expression) $3, lexer.Location);
3335 | multiplicative_expression PERCENT prefixed_unary_expression
3337 $$ = new Binary (Binary.Operator.Modulus,
3338 (Expression) $1, (Expression) $3, lexer.Location);
3343 : multiplicative_expression
3344 | additive_expression PLUS multiplicative_expression
3346 $$ = new Binary (Binary.Operator.Addition,
3347 (Expression) $1, (Expression) $3, lexer.Location);
3349 | additive_expression MINUS multiplicative_expression
3351 $$ = new Binary (Binary.Operator.Subtraction,
3352 (Expression) $1, (Expression) $3, lexer.Location);
3357 : additive_expression
3358 | shift_expression OP_SHIFT_LEFT additive_expression
3360 $$ = new Binary (Binary.Operator.LeftShift,
3361 (Expression) $1, (Expression) $3, lexer.Location);
3363 | shift_expression OP_SHIFT_RIGHT additive_expression
3365 $$ = new Binary (Binary.Operator.RightShift,
3366 (Expression) $1, (Expression) $3, lexer.Location);
3374 lexer.PutbackNullable ();
3378 nullable_type_or_conditional
3385 relational_expression
3387 | relational_expression OP_LT shift_expression
3389 $$ = new Binary (Binary.Operator.LessThan,
3390 (Expression) $1, (Expression) $3, lexer.Location);
3392 | relational_expression OP_GT shift_expression
3394 $$ = new Binary (Binary.Operator.GreaterThan,
3395 (Expression) $1, (Expression) $3, lexer.Location);
3397 | relational_expression OP_LE shift_expression
3399 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3400 (Expression) $1, (Expression) $3, lexer.Location);
3402 | relational_expression OP_GE shift_expression
3404 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3405 (Expression) $1, (Expression) $3, lexer.Location);
3407 | relational_expression IS
3410 } nullable_type_or_conditional
3412 $$ = new Is ((Expression) $1, (Expression) $4, lexer.Location);
3414 | relational_expression AS
3417 } nullable_type_or_conditional
3419 $$ = new As ((Expression) $1, (Expression) $4, lexer.Location);
3424 : relational_expression
3425 | equality_expression OP_EQ relational_expression
3427 $$ = new Binary (Binary.Operator.Equality,
3428 (Expression) $1, (Expression) $3, lexer.Location);
3430 | equality_expression OP_NE relational_expression
3432 $$ = new Binary (Binary.Operator.Inequality,
3433 (Expression) $1, (Expression) $3, lexer.Location);
3438 : equality_expression
3439 | and_expression BITWISE_AND equality_expression
3441 $$ = new Binary (Binary.Operator.BitwiseAnd,
3442 (Expression) $1, (Expression) $3, lexer.Location);
3446 exclusive_or_expression
3448 | exclusive_or_expression CARRET and_expression
3450 $$ = new Binary (Binary.Operator.ExclusiveOr,
3451 (Expression) $1, (Expression) $3, lexer.Location);
3455 inclusive_or_expression
3456 : exclusive_or_expression
3457 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3459 $$ = new Binary (Binary.Operator.BitwiseOr,
3460 (Expression) $1, (Expression) $3, lexer.Location);
3464 conditional_and_expression
3465 : inclusive_or_expression
3466 | conditional_and_expression OP_AND inclusive_or_expression
3468 $$ = new Binary (Binary.Operator.LogicalAnd,
3469 (Expression) $1, (Expression) $3, lexer.Location);
3473 conditional_or_expression
3474 : conditional_and_expression
3475 | conditional_or_expression OP_OR conditional_and_expression
3477 $$ = new Binary (Binary.Operator.LogicalOr,
3478 (Expression) $1, (Expression) $3, lexer.Location);
3482 conditional_expression
3483 : conditional_or_expression
3484 | conditional_or_expression INTERR expression COLON expression
3486 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
3488 | conditional_or_expression INTERR INTERR expression
3490 $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $4, lexer.Location);
3492 // We'll be resolved into a `parenthesized_expression_0' later on.
3493 | conditional_or_expression INTERR CLOSE_PARENS
3495 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
3496 lexer.PutbackCloseParens ();
3500 assignment_expression
3501 : prefixed_unary_expression ASSIGN expression
3503 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
3505 | prefixed_unary_expression OP_MULT_ASSIGN expression
3507 Location l = lexer.Location;
3509 $$ = new CompoundAssign (
3510 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
3512 | prefixed_unary_expression OP_DIV_ASSIGN expression
3514 Location l = lexer.Location;
3516 $$ = new CompoundAssign (
3517 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
3519 | prefixed_unary_expression OP_MOD_ASSIGN expression
3521 Location l = lexer.Location;
3523 $$ = new CompoundAssign (
3524 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
3526 | prefixed_unary_expression OP_ADD_ASSIGN expression
3528 Location l = lexer.Location;
3530 $$ = new CompoundAssign (
3531 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
3533 | prefixed_unary_expression OP_SUB_ASSIGN expression
3535 Location l = lexer.Location;
3537 $$ = new CompoundAssign (
3538 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
3540 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3542 Location l = lexer.Location;
3544 $$ = new CompoundAssign (
3545 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
3547 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3549 Location l = lexer.Location;
3551 $$ = new CompoundAssign (
3552 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
3554 | prefixed_unary_expression OP_AND_ASSIGN expression
3556 Location l = lexer.Location;
3558 $$ = new CompoundAssign (
3559 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
3561 | prefixed_unary_expression OP_OR_ASSIGN expression
3563 Location l = lexer.Location;
3565 $$ = new CompoundAssign (
3566 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
3568 | prefixed_unary_expression OP_XOR_ASSIGN expression
3570 Location l = lexer.Location;
3572 $$ = new CompoundAssign (
3573 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
3578 : conditional_expression
3579 | assignment_expression
3599 MemberName name = MakeName ((MemberName) $5);
3600 bool partial = (bool) $3;
3601 int mod_flags = (int) $2;
3604 ClassPart part = PartialContainer.CreatePart (
3605 current_namespace, current_container, name, mod_flags,
3606 (Attributes) $1, Kind.Class, lexer.Location);
3608 current_container = part.PartialContainer;
3609 current_class = part;
3611 if ((mod_flags & Modifiers.STATIC) != 0) {
3612 current_class = new StaticClass (
3613 current_namespace, current_container, name,
3614 mod_flags, (Attributes) $1, lexer.Location);
3616 current_class = new Class (
3617 current_namespace, current_container, name,
3618 mod_flags, (Attributes) $1, lexer.Location);
3621 current_container = current_class;
3622 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
3625 lexer.ConstraintsParsing = true;
3628 opt_type_parameter_constraints_clauses
3630 lexer.ConstraintsParsing = false;
3633 if (current_class.Name == "System.Object") {
3634 Report.Error (537, current_class.Location,
3635 "The class System.Object cannot have a base " +
3636 "class or implement an interface.");
3638 current_class.Bases = (ArrayList) $7;
3641 current_class.SetParameterInfo ((ArrayList) $8);
3643 if (RootContext.Documentation != null) {
3644 current_class.DocComment = Lexer.consume_doc_comment ();
3645 Lexer.doc_state = XmlCommentState.Allowed;
3648 current_class.Register ();
3652 if (RootContext.Documentation != null)
3653 Lexer.doc_state = XmlCommentState.Allowed;
3659 current_container = current_container.Parent;
3660 current_class = current_container;
3666 { $$ = (bool) false; }
3668 { $$ = (bool) true; }
3672 : /* empty */ { $$ = (int) 0; }
3678 | modifiers modifier
3683 if ((m1 & m2) != 0) {
3684 Location l = lexer.Location;
3685 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3687 $$ = (int) (m1 | m2);
3692 : NEW { $$ = Modifiers.NEW; }
3693 | PUBLIC { $$ = Modifiers.PUBLIC; }
3694 | PROTECTED { $$ = Modifiers.PROTECTED; }
3695 | INTERNAL { $$ = Modifiers.INTERNAL; }
3696 | PRIVATE { $$ = Modifiers.PRIVATE; }
3697 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3698 | SEALED { $$ = Modifiers.SEALED; }
3699 | STATIC { $$ = Modifiers.STATIC; }
3700 | READONLY { $$ = Modifiers.READONLY; }
3701 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3702 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3703 | EXTERN { $$ = Modifiers.EXTERN; }
3704 | VOLATILE { $$ = Modifiers.VOLATILE; }
3705 | UNSAFE { $$ = Modifiers.UNSAFE; }
3709 : /* empty */ { $$ = null; }
3710 | class_base { $$ = $1; }
3714 : COLON type_list { $$ = $2; }
3717 opt_type_parameter_constraints_clauses
3718 : /* empty */ { $$ = null; }
3719 | type_parameter_constraints_clauses
3723 type_parameter_constraints_clauses
3724 : type_parameter_constraints_clause {
3725 ArrayList constraints = new ArrayList (1);
3726 constraints.Add ($1);
3729 | type_parameter_constraints_clauses type_parameter_constraints_clause {
3730 ArrayList constraints = (ArrayList) $1;
3732 constraints.Add ($2);
3737 type_parameter_constraints_clause
3738 : WHERE IDENTIFIER COLON type_parameter_constraints {
3739 $$ = new Constraints ((string) $2, (ArrayList) $4, lexer.Location);
3743 type_parameter_constraints
3744 : type_parameter_constraint {
3745 ArrayList constraints = new ArrayList (1);
3746 constraints.Add ($1);
3749 | type_parameter_constraints COMMA type_parameter_constraint {
3750 ArrayList constraints = (ArrayList) $1;
3752 constraints.Add ($3);
3757 type_parameter_constraint
3759 | NEW OPEN_PARENS CLOSE_PARENS {
3760 $$ = SpecialConstraint.Constructor;
3763 $$ = SpecialConstraint.ReferenceType;
3766 $$ = SpecialConstraint.ValueType;
3775 // A block is "contained" on the following places:
3777 // property_declaration as part of the accessor body (get/set)
3778 // operator_declaration
3779 // constructor_declaration
3780 // destructor_declaration
3781 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3786 if (current_block == null){
3787 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, lexer.Location);
3788 top_current_block = current_block;
3790 current_block = new Block (current_block, current_local_parameters,
3791 lexer.Location, Location.Null);
3794 opt_statement_list CLOSE_BRACE
3796 while (current_block.Implicit)
3797 current_block = current_block.Parent;
3799 current_block.SetEndLocation (lexer.Location);
3800 current_block = current_block.Parent;
3801 if (current_block == null)
3802 top_current_block = null;
3813 | statement_list statement
3817 : declaration_statement
3819 if ($1 != null && (Block) $1 != current_block){
3820 current_block.AddStatement ((Statement) $1);
3821 current_block = (Block) $1;
3824 | valid_declaration_statement
3826 current_block.AddStatement ((Statement) $1);
3831 valid_declaration_statement
3834 | expression_statement
3835 | selection_statement
3836 | iteration_statement
3840 | unchecked_statement
3848 : valid_declaration_statement
3849 | declaration_statement
3851 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3856 Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3864 $$ = EmptyStatement.Value;
3871 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3873 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3874 current_block.AddStatement (labeled);
3879 declaration_statement
3880 : local_variable_declaration SEMICOLON
3883 DictionaryEntry de = (DictionaryEntry) $1;
3885 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3889 | local_constant_declaration SEMICOLON
3892 DictionaryEntry de = (DictionaryEntry) $1;
3894 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3900 * The following is from Rhys' grammar:
3901 * > Types in local variable declarations must be recognized as
3902 * > expressions to prevent reduce/reduce errors in the grammar.
3903 * > The expressions are converted into types during semantic analysis.
3906 : primary_expression opt_rank_specifier_or_nullable
3908 // FIXME: Do something smart here regarding the composition of the type.
3910 // Ok, the above "primary_expression" is there to get rid of
3911 // both reduce/reduce and shift/reduces in the grammar, it should
3912 // really just be "type_name". If you use type_name, a reduce/reduce
3913 // creeps up. If you use namespace_or_type_name (which is all we need
3914 // really) two shift/reduces appear.
3917 // So the super-trick is that primary_expression
3918 // can only be either a SimpleName or a MemberAccess.
3919 // The MemberAccess case arises when you have a fully qualified type-name like :
3921 // SimpleName is when you have
3924 Expression expr = (Expression) $1;
3925 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType)) {
3926 Error_ExpectingTypeName (lexer.Location, expr);
3930 // So we extract the string corresponding to the SimpleName
3934 if ((string) $2 == "")
3937 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3940 | builtin_types opt_rank_specifier_or_nullable
3942 if ((string) $2 == "")
3945 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3949 local_variable_pointer_type
3950 : primary_expression STAR
3952 Expression expr = (Expression) $1;
3953 Location l = lexer.Location;
3955 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType)) {
3956 Error_ExpectingTypeName (l, expr);
3960 $$ = new ComposedCast ((Expression) $1, "*", l);
3962 | builtin_types STAR
3964 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3968 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3970 | local_variable_pointer_type STAR
3972 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3976 local_variable_declaration
3977 : local_variable_type variable_declarators
3980 $$ = new DictionaryEntry ($1, $2);
3984 | local_variable_pointer_type opt_rank_specifier_or_nullable variable_declarators
3989 if ((string) $2 == "")
3990 t = (Expression) $1;
3992 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3993 $$ = new DictionaryEntry (t, $3);
3999 local_constant_declaration
4000 : CONST local_variable_type constant_declarators
4003 $$ = new DictionaryEntry ($2, $3);
4009 expression_statement
4010 : statement_expression SEMICOLON
4017 // We have to do the wrapping here and not in the case above,
4018 // because statement_expression is used for example in for_statement
4020 statement_expression
4021 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4022 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4023 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4024 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4025 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4026 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4027 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4029 Report.Error (1002, lexer.Location, "Expecting `;'");
4034 object_creation_expression
4035 : object_or_delegate_creation_expression
4036 { note ("complain if this is a delegate maybe?"); }
4045 : IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS
4048 Location l = (Location) $3;
4050 $$ = new If ((Expression) $4, (Statement) $6, l);
4052 if (RootContext.WarningLevel >= 4){
4053 if ($6 == EmptyStatement.Value)
4054 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4058 | IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS
4059 embedded_statement ELSE embedded_statement
4061 Location l = (Location) $3;
4063 $$ = new If ((Expression) $4, (Statement) $6, (Statement) $8, l);
4068 : SWITCH OPEN_PARENS _mark_
4070 switch_stack.Push (current_block);
4072 expression CLOSE_PARENS
4075 $$ = new Switch ((Expression) $5, (ArrayList) $7, (Location) $3);
4076 current_block = (Block) switch_stack.Pop ();
4092 Report.Error (1522, lexer.Location, "Empty switch block");
4100 ArrayList sections = new ArrayList (4);
4105 | switch_sections switch_section
4107 ArrayList sections = (ArrayList) $1;
4117 current_block = current_block.CreateSwitchBlock (lexer.Location);
4121 Block topmost = current_block;
4123 while (topmost.Implicit)
4124 topmost = topmost.Parent;
4125 $$ = new SwitchSection ((ArrayList) $1, topmost);
4132 ArrayList labels = new ArrayList (4);
4137 | switch_labels switch_label
4139 ArrayList labels = (ArrayList) ($1);
4147 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
4148 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
4151 1523, lexer.Location,
4152 "The keyword case or default must precede code in switch block");
4164 : WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS embedded_statement
4166 Location l = (Location) $3;
4167 $$ = new While ((Expression) $4, (Statement) $6, l);
4169 if (RootContext.WarningLevel >= 4){
4170 if ($6 == EmptyStatement.Value)
4171 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4177 : DO embedded_statement
4178 WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS SEMICOLON
4180 Location l = (Location) $5;
4182 $$ = new Do ((Statement) $2, (Expression) $6, l);
4188 opt_for_initializer SEMICOLON _mark_
4190 Block assign_block = new Block (current_block);
4191 current_block = assign_block;
4193 if ($3 is DictionaryEntry){
4194 DictionaryEntry de = (DictionaryEntry) $3;
4196 Expression type = (Expression) de.Key;
4197 ArrayList var_declarators = (ArrayList) de.Value;
4199 foreach (VariableDeclaration decl in var_declarators){
4203 vi = current_block.AddVariable (
4204 type, decl.identifier, current_local_parameters, decl.Location);
4208 Location l = lexer.Location;
4210 if (decl.expression_or_array_initializer is Expression){
4211 expr = (Expression) decl.expression_or_array_initializer;
4212 } else if (decl.expression_or_array_initializer == null) {
4215 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4216 expr = new ArrayCreation (type, "", init, decl.Location);
4219 LocalVariableReference var;
4220 var = new LocalVariableReference (assign_block, decl.identifier, l);
4223 Assign a = new Assign (var, expr, decl.Location);
4225 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4229 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4230 // This can be referred to as $6 below.
4236 opt_for_condition SEMICOLON
4237 opt_for_iterator CLOSE_PARENS
4240 Location l = (Location) $5;
4242 For f = new For ((Statement) $6, (Expression) $7, (Statement) $9, (Statement) $11, l);
4244 if (RootContext.WarningLevel >= 4){
4245 if ($11 == EmptyStatement.Value)
4246 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4249 current_block.AddStatement (f);
4250 while (current_block.Implicit)
4251 current_block = current_block.Parent;
4253 current_block = current_block.Parent;
4258 : /* empty */ { $$ = EmptyStatement.Value; }
4263 : local_variable_declaration
4264 | statement_expression_list
4268 : /* empty */ { $$ = null; }
4269 | boolean_expression
4273 : /* empty */ { $$ = EmptyStatement.Value; }
4278 : statement_expression_list
4281 statement_expression_list
4282 : statement_expression
4284 // CHANGE: was `null'
4285 Block b = new Block (current_block, Block.Flags.Implicit);
4287 b.AddStatement ((Statement) $1);
4290 | statement_expression_list COMMA statement_expression
4292 Block b = (Block) $1;
4294 b.AddStatement ((Statement) $3);
4300 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4302 Report.Error (230, lexer.Location, "Type and identifier are both required in a foreach statement");
4305 | FOREACH OPEN_PARENS type IDENTIFIER IN _mark_
4306 expression CLOSE_PARENS
4308 Block foreach_block = new Block (current_block);
4309 current_block = foreach_block;
4311 Location l = lexer.Location;
4314 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
4318 // Get a writable reference to this read-only variable.
4320 // Note that the $$ here refers to the value of _this_ code block,
4321 // not the value of the LHS non-terminal. This can be referred to as $9 below.
4322 $$ = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
4329 LocalVariableReference v = (LocalVariableReference) $9;
4330 Location l = (Location) $6;
4333 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
4334 current_block.AddStatement (f);
4337 while (current_block.Implicit)
4338 current_block = current_block.Parent;
4340 current_block = current_block.Parent;
4346 | continue_statement
4356 $$ = new Break (lexer.Location);
4361 : CONTINUE SEMICOLON
4363 $$ = new Continue (lexer.Location);
4368 : GOTO IDENTIFIER SEMICOLON
4370 $$ = new Goto (current_block, (string) $2, lexer.Location);
4372 | GOTO CASE constant_expression SEMICOLON
4374 $$ = new GotoCase ((Expression) $3, lexer.Location);
4376 | GOTO DEFAULT SEMICOLON
4378 $$ = new GotoDefault (lexer.Location);
4383 : RETURN opt_expression SEMICOLON
4385 $$ = new Return ((Expression) $2, lexer.Location);
4390 : THROW opt_expression SEMICOLON
4392 $$ = new Throw ((Expression) $2, lexer.Location);
4397 : IDENTIFIER RETURN expression SEMICOLON
4399 string s = (string) $1;
4401 Report.Error (1003, lexer.Location, "; expected");
4404 if (RootContext.Version == LanguageVersion.ISO_1){
4405 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4408 if (iterator_container == null){
4409 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4412 iterator_container.SetYields ();
4413 $$ = new Yield ((Expression) $3, lexer.Location);
4416 | IDENTIFIER BREAK SEMICOLON
4418 string s = (string) $1;
4420 Report.Error (1003, lexer.Location, "; expected");
4423 if (RootContext.Version == LanguageVersion.ISO_1){
4424 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4427 if (iterator_container == null){
4428 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4431 iterator_container.SetYields ();
4432 $$ = new YieldBreak (lexer.Location);
4443 : TRY block catch_clauses
4447 ArrayList c = (ArrayList)$3;
4448 for (int i = 0; i < c.Count; ++i) {
4449 Catch cc = (Catch) c [i];
4451 if (i != c.Count - 1)
4452 Report.Error (1017, cc.loc, "Empty catch block must be the last in a series of catch blocks");
4459 // Now s contains the list of specific catch clauses
4460 // and g contains the general one.
4462 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4464 | TRY block opt_catch_clauses FINALLY block
4467 ArrayList s = new ArrayList (4);
4468 ArrayList catch_list = (ArrayList) $3;
4470 if (catch_list != null){
4471 foreach (Catch cc in catch_list) {
4479 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4483 Report.Error (1524, lexer.Location, "Expected catch or finally");
4488 : /* empty */ { $$ = null; }
4495 ArrayList l = new ArrayList (4);
4500 | catch_clauses catch_clause
4502 ArrayList l = (ArrayList) $1;
4510 : /* empty */ { $$ = null; }
4515 : CATCH opt_catch_args
4517 Expression type = null;
4521 DictionaryEntry cc = (DictionaryEntry) $2;
4522 type = (Expression) cc.Key;
4523 id = (string) cc.Value;
4526 ArrayList one = new ArrayList (4);
4527 Location loc = lexer.Location;
4529 one.Add (new VariableDeclaration (id, null, loc));
4531 current_block = new Block (current_block);
4532 Block b = declare_local_variables (type, one, loc);
4537 Expression type = null;
4541 DictionaryEntry cc = (DictionaryEntry) $2;
4542 type = (Expression) cc.Key;
4543 id = (string) cc.Value;
4546 while (current_block.Implicit)
4547 current_block = current_block.Parent;
4548 current_block = current_block.Parent;
4552 $$ = new Catch (type, id, (Block) $4, ((Block) $4).loc);
4557 : /* empty */ { $$ = null; }
4562 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4564 $$ = new DictionaryEntry ($2, $3);
4571 $$ = new Checked ((Block) $2);
4578 $$ = new Unchecked ((Block) $2);
4585 if (!RootContext.Unsafe){
4586 Report.Error (227, lexer.Location,
4587 "Unsafe code can only be used if --unsafe is used");
4590 $$ = new Unsafe ((Block) $3);
4596 type fixed_pointer_declarators
4599 ArrayList list = (ArrayList) $4;
4600 Expression type = (Expression) $3;
4601 Location l = lexer.Location;
4602 int top = list.Count;
4604 Block assign_block = new Block (current_block);
4605 current_block = assign_block;
4607 for (int i = 0; i < top; i++){
4608 Pair p = (Pair) list [i];
4611 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
4623 Location l = (Location) $6;
4625 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $8, l);
4627 if (RootContext.WarningLevel >= 4){
4628 if ($8 == EmptyStatement.Value)
4629 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4632 current_block.AddStatement (f);
4633 while (current_block.Implicit)
4634 current_block = current_block.Parent;
4636 current_block = current_block.Parent;
4640 fixed_pointer_declarators
4641 : fixed_pointer_declarator {
4642 ArrayList declarators = new ArrayList (4);
4644 declarators.Add ($1);
4647 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4649 ArrayList declarators = (ArrayList) $1;
4651 declarators.Add ($3);
4656 fixed_pointer_declarator
4657 : IDENTIFIER ASSIGN expression
4659 $$ = new Pair ($1, $3);
4663 Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
4669 : LOCK OPEN_PARENS expression CLOSE_PARENS
4675 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4680 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS _mark_
4682 Block assign_block = new Block (current_block);
4683 current_block = assign_block;
4685 if ($3 is DictionaryEntry){
4686 DictionaryEntry de = (DictionaryEntry) $3;
4687 Location l = lexer.Location;
4689 Expression type = (Expression) de.Key;
4690 ArrayList var_declarators = (ArrayList) de.Value;
4692 ArrayList vars = new ArrayList (4);
4694 foreach (VariableDeclaration decl in var_declarators){
4696 LocalInfo vi = current_block.AddVariable (
4697 type, decl.identifier,
4698 current_local_parameters, decl.Location);
4704 if (decl.expression_or_array_initializer is Expression){
4705 expr = (Expression) decl.expression_or_array_initializer;
4707 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4709 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4712 expr = new ArrayCreation (type, "", init, decl.Location);
4715 LocalVariableReference var;
4717 // Get a writable reference to this read-only variable.
4718 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4720 // This is so that it is not a warning on using variables
4723 vars.Add (new DictionaryEntry (var, expr));
4725 // Assign a = new Assign (var, expr, decl.Location);
4726 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4729 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4730 // It can be referred to as $6 below.
4731 $$ = new DictionaryEntry (type, vars);
4738 Using u = new Using ($6, (Statement) $7, (Location) $5);
4739 current_block.AddStatement (u);
4740 while (current_block.Implicit)
4741 current_block = current_block.Parent;
4743 current_block = current_block.Parent;
4747 resource_acquisition
4748 : local_variable_declaration
4752 // Utility rule to save location information
4755 { $$ = lexer.Location; }
4760 // A class used to pass around variable declarations and constants
4762 public class VariableDeclaration {
4763 public string identifier;
4764 public object expression_or_array_initializer;
4765 public Location Location;
4766 public Attributes OptAttributes;
4767 public string DocComment;
4769 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4771 this.identifier = id;
4772 this.expression_or_array_initializer = eoai;
4774 this.OptAttributes = opt_attrs;
4777 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4783 /// Used to pass around interface property information
4785 public class InterfaceAccessorInfo {
4787 public readonly Accessor Get, Set;
4789 public InterfaceAccessorInfo (bool has_get, bool has_set,
4790 Attributes get_attrs, Attributes set_attrs, int get_mod, int set_mod, Location get_loc, Location set_loc)
4793 Report.Error (275, get_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4795 Report.Error (275, set_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4798 Get = new Accessor (null, 0, get_attrs, get_loc);
4800 Set = new Accessor (null, 0, set_attrs, set_loc);
4807 // A class used to hold info about an indexer declarator
4809 public class IndexerDeclaration {
4810 public Expression type;
4811 public MemberName interface_type;
4812 public Parameters param_list;
4814 public IndexerDeclaration (Expression type, MemberName interface_type,
4815 Parameters param_list)
4818 this.interface_type = interface_type;
4819 this.param_list = param_list;
4824 // We use this when we do not have an object in advance that is an IIteratorContainer
4826 public class SimpleIteratorContainer : IIteratorContainer {
4829 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4834 public static SimpleIteratorContainer GetSimple () {
4835 Simple.Yields = false;
4839 public void SetYields () { Yields = true; }
4843 // A class used to hold info about an operator declarator
4845 public class OperatorDeclaration {
4846 public Operator.OpType optype;
4847 public Expression ret_type, arg1type, arg2type;
4848 public string arg1name, arg2name;
4849 public Location location;
4851 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4852 Expression arg1type, string arg1name,
4853 Expression arg2type, string arg2name, Location location)
4856 this.ret_type = ret_type;
4857 this.arg1type = arg1type;
4858 this.arg1name = arg1name;
4859 this.arg2type = arg2type;
4860 this.arg2name = arg2name;
4861 this.location = location;
4866 void Error_ExpectingTypeName (Location l, Expression expr)
4868 if (expr is Invocation){
4869 Report.Error (1002, l, "; expected");
4871 Report.Error (-1, l, "Invalid Type definition");
4876 // Given the @class_name name, it creates a fully qualified name
4877 // based on the containing declaration space
4880 MakeName (MemberName class_name)
4882 string ns = current_namespace.FullName;
4884 if (current_container.Name == ""){
4886 return new MemberName (new MemberName (ns), class_name);
4890 return new MemberName (current_container.MemberName, class_name);
4894 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4896 Block implicit_block;
4897 ArrayList inits = null;
4900 // We use the `Used' property to check whether statements
4901 // have been added to the current block. If so, we need
4902 // to create another block to contain the new declaration
4903 // otherwise, as an optimization, we use the same block to
4904 // add the declaration.
4906 // FIXME: A further optimization is to check if the statements
4907 // that were added were added as part of the initialization
4908 // below. In which case, no other statements have been executed
4909 // and we might be able to reduce the number of blocks for
4910 // situations like this:
4912 // int j = 1; int k = j + 1;
4914 if (current_block.Used)
4915 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4917 implicit_block = current_block;
4919 foreach (VariableDeclaration decl in variable_declarators){
4921 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4922 if (decl.expression_or_array_initializer != null){
4924 inits = new ArrayList (4);
4931 return implicit_block;
4933 foreach (VariableDeclaration decl in inits){
4937 if (decl.expression_or_array_initializer is Expression){
4938 expr = (Expression) decl.expression_or_array_initializer;
4941 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4943 expr = new ArrayCreation (type, "", init, decl.Location);
4946 LocalVariableReference var;
4947 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4949 assign = new Assign (var, expr, decl.Location);
4951 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4954 return implicit_block;
4957 Block declare_local_constants (Expression type, ArrayList declarators)
4959 Block implicit_block;
4961 if (current_block.Used)
4962 implicit_block = new Block (current_block, Block.Flags.Implicit);
4964 implicit_block = current_block;
4966 foreach (VariableDeclaration decl in declarators){
4967 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4968 current_local_parameters, decl.Location);
4971 return implicit_block;
4974 void CheckAttributeTarget (string a)
4978 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4982 Location l = lexer.Location;
4983 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4989 void CheckUnaryOperator (Operator.OpType op)
4993 case Operator.OpType.LogicalNot:
4994 case Operator.OpType.OnesComplement:
4995 case Operator.OpType.Increment:
4996 case Operator.OpType.Decrement:
4997 case Operator.OpType.True:
4998 case Operator.OpType.False:
4999 case Operator.OpType.Addition:
5000 case Operator.OpType.Subtraction:
5005 Location l = lexer.Location;
5006 Report.Error (1019, l, "Overloadable unary operator expected");
5012 void CheckBinaryOperator (Operator.OpType op)
5016 case Operator.OpType.Addition:
5017 case Operator.OpType.Subtraction:
5018 case Operator.OpType.Multiply:
5019 case Operator.OpType.Division:
5020 case Operator.OpType.Modulus:
5021 case Operator.OpType.BitwiseAnd:
5022 case Operator.OpType.BitwiseOr:
5023 case Operator.OpType.ExclusiveOr:
5024 case Operator.OpType.LeftShift:
5025 case Operator.OpType.RightShift:
5026 case Operator.OpType.Equality:
5027 case Operator.OpType.Inequality:
5028 case Operator.OpType.GreaterThan:
5029 case Operator.OpType.LessThan:
5030 case Operator.OpType.GreaterThanOrEqual:
5031 case Operator.OpType.LessThanOrEqual:
5035 Location l = lexer.Location;
5036 Report.Error (1020, l, "Overloadable binary operator expected");
5042 void syntax_error (Location l, string msg)
5044 Report.Error (1003, l, "Syntax error, " + msg);
5047 void output (string s)
5049 Console.WriteLine (s);
5052 void note (string s)
5054 // Used to put annotations
5059 public Tokenizer Lexer {
5065 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
5067 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
5068 this.name = file.Name;
5070 current_container = RootContext.Tree.Types;
5071 current_container.NamespaceEntry = current_namespace;
5072 oob_stack = new Stack ();
5073 switch_stack = new Stack ();
5075 lexer = new Tokenizer (reader, file, defines);
5078 public void parse ()
5081 if (yacc_verbose_flag > 1)
5082 yyparse (lexer, new yydebug.yyDebugSimple ());
5085 Tokenizer tokenizer = lexer as Tokenizer;
5086 tokenizer.cleanup ();
5088 } catch (Exception e){
5090 // Removed for production use, use parser verbose to get the output.
5092 // Console.WriteLine (e);
5093 Report.Error (-25, lexer.Location, "Parsing error");
5094 if (yacc_verbose_flag > 0)
5095 Console.WriteLine (e);
5100 void CheckToken (int error, int yyToken, string msg)
5102 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
5103 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
5106 Report.Error (error, lexer.Location, msg);
5109 void CheckIdentifierToken (int yyToken)
5111 CheckToken (1041, yyToken, "Identifier expected");
5114 string ConsumeStoredComment ()
5116 string s = tmpComment;
5118 Lexer.doc_state = XmlCommentState.Allowed;