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)
13 // (1) Figure out why error productions dont work. `type-declaration' is a
14 // great spot to put an `error' because you can reproduce it with this input:
17 // Possible optimization:
18 // Run memory profiler with parsing only, and consider dropping
19 // arraylists where not needed. Some pieces can use linked lists.
27 using System.Collections;
32 public class CSharpParser {
33 NamespaceEntry current_namespace;
34 TypeContainer current_container;
35 TypeContainer current_class;
37 IIteratorContainer iterator_container;
40 /// Current block is used to add statements as we find
46 /// If true, creates a toplevel block in the block production
47 /// This is flagged by the delegate creation
49 bool create_toplevel_block;
52 /// This is used by the unary_expression code to resolve
53 /// a name against a parameter.
55 Parameters current_local_parameters;
58 /// Using during property parsing to describe the implicit
59 /// value parameter that is passed to the "set" and "get"accesor
60 /// methods (properties and indexers).
62 Expression implicit_value_parameter_type;
63 Parameters indexer_parameters;
66 /// Used to determine if we are parsing the get/set pair
67 /// of an indexer or a property
72 /// An out-of-band stack.
81 public bool yacc_verbose_flag;
83 // Name of the file we are parsing
92 /// Current attribute target
93 string current_attr_target;
95 /// assembly and module attribute definition is enabled
96 bool global_attrs_enabled = true;
100 %token NONE /* This token is never returned by our lexer */
101 %token ERROR // This is used not by the parser, but by the tokenizer.
105 *These are the C# keywords
191 /* C# keywords which are not really keywords */
197 /* C# single character operators/punctuation. */
198 %token OPEN_BRACE "{"
199 %token CLOSE_BRACE "}"
200 %token OPEN_BRACKET "["
201 %token CLOSE_BRACKET "]"
202 %token OPEN_PARENS "("
203 %token CLOSE_PARENS ")"
216 %token BITWISE_AND "&"
217 %token BITWISE_OR "|"
224 /* C# multi-character operators. */
227 %token OP_SHIFT_LEFT "<<"
228 %token OP_SHIFT_RIGHT ">>"
235 %token OP_MULT_ASSIGN "*="
236 %token OP_DIV_ASSIGN "/="
237 %token OP_MOD_ASSIGN "%="
238 %token OP_ADD_ASSIGN "+="
239 %token OP_SUB_ASSIGN "-="
240 %token OP_SHIFT_LEFT_ASSIGN "<<="
241 %token OP_SHIFT_RIGHT_ASSIGN ">>="
242 %token OP_AND_ASSIGN "&="
243 %token OP_XOR_ASSIGN "^="
244 %token OP_OR_ASSIGN "|="
248 %token LITERAL_INTEGER "int literal"
249 %token LITERAL_FLOAT "float literal"
250 %token LITERAL_DOUBLE "double literal"
251 %token LITERAL_DECIMAL "decimal literal"
252 %token LITERAL_CHARACTER "character literal"
253 %token LITERAL_STRING "string literal"
256 %token CLOSE_PARENS_CAST
257 %token CLOSE_PARENS_NO_CAST
258 %token CLOSE_PARENS_OPEN_PARENS
259 %token CLOSE_PARENS_MINUS
261 /* Add precedence rules to solve dangling else s/r conflict */
270 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
272 %left STAR DIV PERCENT
273 %right BANG CARRET UMINUS
274 %nonassoc OP_INC OP_DEC
276 %left OPEN_BRACKET OPEN_BRACE
280 %start compilation_unit
284 : outer_declarations opt_EOF
285 | outer_declarations global_attributes opt_EOF
286 | global_attributes opt_EOF
287 | opt_EOF /* allow empty files */
297 | outer_declarations outer_declaration
302 | namespace_member_declaration
307 | using_directives using_directive
311 : using_alias_directive
312 | using_namespace_directive
315 using_alias_directive
316 : USING IDENTIFIER ASSIGN
317 namespace_or_type_name SEMICOLON
319 current_namespace.UsingAlias ((string) $2, (MemberName) $4, lexer.Location);
322 CheckIdentifierToken (yyToken);
326 using_namespace_directive
327 : USING namespace_name SEMICOLON
329 current_namespace.Using ((string) $2, lexer.Location);
334 // Strictly speaking, namespaces don't have attributes but
335 // we parse global attributes along with namespace declarations and then
338 namespace_declaration
339 : opt_attributes NAMESPACE namespace_or_type_name
342 Report.Error(1518, Lexer.Location, "Attributes cannot be applied to namespaces."
343 + " Expected class, delegate, enum, interface, or struct");
346 MemberName name = (MemberName) $3;
348 if ((current_namespace.Parent != null) && (name.Left != null)) {
349 Report.Error (134, lexer.Location,
350 "Cannot use qualified namespace names in nested " +
351 "namespace declarations");
354 current_namespace = new NamespaceEntry (
355 current_namespace, file, name.GetName (false), lexer.Location);
357 namespace_body opt_semicolon
359 current_namespace = current_namespace.Parent;
374 : namespace_or_type_name {
375 MemberName name = (MemberName) $1;
377 $$ = name.GetName (false);
384 opt_namespace_member_declarations
395 opt_namespace_member_declarations
397 | namespace_member_declarations
400 namespace_member_declarations
401 : namespace_member_declaration
402 | namespace_member_declarations namespace_member_declaration
405 namespace_member_declaration
412 Class c = (Class) $1;
413 mod_flags = c.ModFlags;
415 } else if ($1 is Struct){
416 Struct s = (Struct) $1;
417 mod_flags = s.ModFlags;
422 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
424 1527, lexer.Location,
425 "Namespace elements cant be explicitly " +
426 "declared private or protected in `" + name + "'");
428 current_namespace.DeclarationFound = true;
430 | namespace_declaration {
431 current_namespace.DeclarationFound = true;
434 | field_declaration {
435 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
437 | method_declaration {
438 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
445 | interface_declaration
447 | delegate_declaration
449 // Enable this when we have handled all errors, because this acts as a generic fallback
452 // Console.WriteLine ("Token=" + yyToken);
453 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
465 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
473 global_attrs_enabled = false;
478 global_attrs_enabled = false;
487 ArrayList sect = (ArrayList) $1;
489 if (global_attrs_enabled) {
490 if (current_attr_target == "module") {
491 CodeGen.Module.AddAttributes (sect);
493 } else if (current_attr_target == "assembly") {
494 CodeGen.Assembly.AddAttributes (sect);
497 $$ = new Attributes (sect);
500 $$ = new Attributes (sect);
502 current_attr_target = null;
504 | attribute_sections attribute_section
506 Attributes attrs = $1 as Attributes;
507 ArrayList sect = (ArrayList) $2;
509 if (global_attrs_enabled) {
510 if (current_attr_target == "module") {
511 CodeGen.Module.AddAttributes (sect);
513 } else if (current_attr_target == "assembly") {
514 CodeGen.Assembly.AddAttributes (sect);
518 attrs = new Attributes (sect);
520 attrs.AddAttributes (sect);
524 attrs = new Attributes (sect);
526 attrs.AddAttributes (sect);
529 current_attr_target = null;
534 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
538 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
544 attribute_target_specifier
545 : attribute_target COLON
547 current_attr_target = (string)$1;
555 CheckAttributeTarget ((string) $1);
558 | EVENT { $$ = "event"; }
559 | RETURN { $$ = "return"; }
565 ArrayList attrs = new ArrayList (4);
571 | attribute_list COMMA attribute
573 ArrayList attrs = (ArrayList) $1;
585 opt_attribute_arguments
587 if (current_attr_target == "assembly" || current_attr_target == "module")
588 $$ = new GlobalAttribute (current_container, current_attr_target, (string) ($1).ToString (), (ArrayList) $3, (Location) $2);
590 $$ = new Attribute (current_attr_target, (string) ($1).ToString (), (ArrayList) $3, (Location) $2);
595 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
598 opt_attribute_arguments
599 : /* empty */ { $$ = null; }
600 | OPEN_PARENS attribute_arguments CLOSE_PARENS
608 : opt_positional_argument_list
613 ArrayList args = new ArrayList (4);
619 | positional_argument_list COMMA named_argument_list
621 ArrayList args = new ArrayList (4);
627 | named_argument_list
629 ArrayList args = new ArrayList (4);
638 opt_positional_argument_list
639 : /* empty */ { $$ = null; }
640 | positional_argument_list
643 positional_argument_list
646 ArrayList args = new ArrayList (4);
647 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
651 | positional_argument_list COMMA expression
653 ArrayList args = (ArrayList) $1;
654 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
663 ArrayList args = new ArrayList (4);
668 | named_argument_list COMMA named_argument
670 ArrayList args = (ArrayList) $1;
678 : IDENTIFIER ASSIGN expression
680 $$ = new DictionaryEntry (
682 new Argument ((Expression) $3, Argument.AType.Expression));
688 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
691 opt_class_member_declarations
693 | class_member_declarations
696 class_member_declarations
697 : class_member_declaration
698 | class_member_declarations
699 class_member_declaration
702 class_member_declaration
703 : constant_declaration // done
704 | field_declaration // done
705 | method_declaration // done
706 | property_declaration // done
707 | event_declaration // done
708 | indexer_declaration // done
709 | operator_declaration // done
710 | constructor_declaration // done
711 | destructor_declaration // done
721 MemberName name = MakeName ((MemberName) $5);
722 bool partial = (bool) $3;
725 ClassPart part = PartialContainer.CreatePart (
726 current_namespace, current_container, name, (int) $2,
727 (Attributes) $1, Kind.Struct, lexer.Location);
729 current_container = part.PartialContainer;
730 current_class = part;
732 current_class = new Struct (
733 current_namespace, current_container, name, (int) $2,
734 (Attributes) $1, lexer.Location);
736 current_container = current_class;
737 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
743 current_class.Bases = (ArrayList) $7;
745 current_class.Register ();
752 current_container = current_container.Parent;
753 current_class = current_container;
755 | opt_attributes opt_modifiers opt_partial STRUCT error {
756 CheckIdentifierToken (yyToken);
761 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
764 opt_struct_member_declarations
766 | struct_member_declarations
769 struct_member_declarations
770 : struct_member_declaration
771 | struct_member_declarations struct_member_declaration
774 struct_member_declaration
775 : constant_declaration
778 | property_declaration
780 | indexer_declaration
781 | operator_declaration
782 | constructor_declaration
786 * This is only included so we can flag error 575:
787 * destructors only allowed on class types
789 | destructor_declaration
800 foreach (VariableDeclaration constant in (ArrayList) $5){
801 Location l = constant.Location;
803 Const c = new Const (
804 current_class, (Expression) $4, (string) constant.identifier,
805 (Expression) constant.expression_or_array_initializer, (int) $2,
808 current_container.AddConstant (c);
814 : constant_declarator
816 ArrayList constants = new ArrayList (4);
821 | constant_declarators COMMA constant_declarator
824 ArrayList constants = (ArrayList) $1;
831 : IDENTIFIER ASSIGN constant_expression
833 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
837 // A const field requires a value to be provided
838 Report.Error (145, lexer.Location, "A const field requires a value to be provided");
850 Expression type = (Expression) $3;
853 foreach (VariableDeclaration var in (ArrayList) $4){
854 Location l = var.Location;
856 Field field = new Field (current_class, type, mod, var.identifier,
857 var.expression_or_array_initializer,
860 current_container.AddField (field);
868 Report.Error (670, lexer.Location, "void type is not allowed for fields");
873 : variable_declarator
875 ArrayList decl = new ArrayList (4);
879 | variable_declarators COMMA variable_declarator
881 ArrayList decls = (ArrayList) $1;
888 : IDENTIFIER ASSIGN variable_initializer
890 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
894 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
907 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
909 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
915 iterator_container = (IIteratorContainer) $1;
919 Method method = (Method) $1;
920 Block b = (Block) $3;
921 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
924 if ((method.ModFlags & extern_abstract) == 0){
926 501, lexer.Location, current_container.MakeName (method.Name) +
927 "must declare a body because it is not marked abstract or extern");
930 if ((method.ModFlags & Modifiers.EXTERN) != 0){
932 179, lexer.Location, current_container.MakeName (method.Name) +
933 " is declared extern, but has a body");
937 method.Block = (Block) $3;
938 current_container.AddMethod (method);
940 current_local_parameters = null;
941 iterator_container = null;
955 1585, lexer.Location, "Member modifier `" +
956 Modifiers.Name (i) + "' must precede member type and name");
967 type namespace_or_type_name
968 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
970 MemberName name = (MemberName) $4;
972 Method method = new Method (current_class, (Expression) $3, (int) $2,
973 false, name, (Parameters) $6, (Attributes) $1,
976 current_local_parameters = (Parameters) $6;
982 VOID namespace_or_type_name
983 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
985 MemberName name = (MemberName) $4;
987 Method method = new Method (current_class, TypeManager.system_void_expr,
988 (int) $2, false, name, (Parameters) $6,
989 (Attributes) $1, lexer.Location);
991 current_local_parameters = (Parameters) $6;
997 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
999 Report.Error (1585, lexer.Location,
1000 String.Format ("Modifier {0} should appear before type",
1001 Modifiers.Name ((int) $4)));
1002 MemberName name = (MemberName) $4;
1004 Method method = new Method (current_class, TypeManager.system_void_expr,
1005 0, false, name, (Parameters) $7, (Attributes) $1,
1008 current_local_parameters = (Parameters) $7;
1015 | SEMICOLON { $$ = null; }
1018 opt_formal_parameter_list
1019 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1020 | formal_parameter_list
1023 formal_parameter_list
1026 ArrayList pars_list = (ArrayList) $1;
1028 Parameter [] pars = new Parameter [pars_list.Count];
1029 pars_list.CopyTo (pars);
1031 $$ = new Parameters (pars, null, lexer.Location);
1033 | fixed_parameters COMMA parameter_array
1035 ArrayList pars_list = (ArrayList) $1;
1037 Parameter [] pars = new Parameter [pars_list.Count];
1038 pars_list.CopyTo (pars);
1040 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
1042 | fixed_parameters COMMA ARGLIST
1044 ArrayList pars_list = (ArrayList) $1;
1046 Parameter [] pars = new Parameter [pars_list.Count];
1047 pars_list.CopyTo (pars);
1049 $$ = new Parameters (pars, true, lexer.Location);
1051 | parameter_array COMMA fixed_parameters
1053 Report.Error (231, lexer.Location, "A params parameter must be the last parameter in a formal parameter list");
1056 | ARGLIST COMMA fixed_parameters
1058 Report.Error (257, lexer.Location, "An __arglist parameter must be the last parameter in a formal parameter list");
1063 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
1067 $$ = new Parameters (null, true, lexer.Location);
1074 ArrayList pars = new ArrayList (4);
1079 | fixed_parameters COMMA fixed_parameter
1081 ArrayList pars = (ArrayList) $1;
1090 opt_parameter_modifier
1094 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
1097 opt_parameter_modifier
1100 CheckIdentifierToken (yyToken);
1104 opt_parameter_modifier
1110 Report.Error (241, lexer.Location, "Default parameter specifiers are not permitted");
1115 opt_parameter_modifier
1116 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1117 | parameter_modifier
1121 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1122 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1126 : opt_attributes PARAMS type IDENTIFIER
1128 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1129 note ("type must be a single-dimension array type");
1131 | opt_attributes PARAMS type error {
1132 CheckIdentifierToken (yyToken);
1137 property_declaration
1140 type namespace_or_type_name
1143 implicit_value_parameter_type = (Expression) $3;
1145 lexer.PropertyParsing = true;
1147 $$ = lexer.Location;
1149 iterator_container = SimpleIteratorContainer.GetSimple ();
1151 accessor_declarations
1153 lexer.PropertyParsing = false;
1158 Pair pair = (Pair) $7;
1159 Accessor get_block = (Accessor) pair.First;
1160 Accessor set_block = (Accessor) pair.Second;
1162 Location loc = (Location) $6;
1163 MemberName name = (MemberName) $4;
1165 prop = new Property (current_class, (Expression) $3, (int) $2, false,
1166 name, (Attributes) $1, get_block, set_block, loc);
1167 if (SimpleIteratorContainer.Simple.Yields)
1170 current_container.AddProperty (prop);
1171 implicit_value_parameter_type = null;
1172 iterator_container = null;
1176 accessor_declarations
1177 : get_accessor_declaration opt_set_accessor_declaration
1179 $$ = new Pair ($1, $2);
1181 | set_accessor_declaration opt_get_accessor_declaration
1183 $$ = new Pair ($2, $1);
1187 opt_get_accessor_declaration
1188 : /* empty */ { $$ = null; }
1189 | get_accessor_declaration
1192 opt_set_accessor_declaration
1193 : /* empty */ { $$ = null; }
1194 | set_accessor_declaration
1197 get_accessor_declaration
1198 : opt_attributes GET
1200 // If this is not the case, then current_local_parameters has already
1201 // been set in indexer_declaration
1202 if (parsing_indexer == false)
1203 current_local_parameters = null;
1205 current_local_parameters = indexer_parameters;
1206 lexer.PropertyParsing = false;
1210 $$ = new Accessor ((Block) $4, (Attributes) $1, lexer.Location);
1211 current_local_parameters = null;
1212 lexer.PropertyParsing = true;
1216 set_accessor_declaration
1217 : opt_attributes SET
1220 Parameter implicit_value_parameter = new Parameter (
1221 implicit_value_parameter_type, "value",
1222 Parameter.Modifier.NONE, null);
1224 if (parsing_indexer == false) {
1225 args = new Parameter [1];
1226 args [0] = implicit_value_parameter;
1227 current_local_parameters = new Parameters (args, null, lexer.Location);
1229 Parameter [] fpars = indexer_parameters.FixedParameters;
1232 int count = fpars.Length;
1234 args = new Parameter [count + 1];
1235 fpars.CopyTo (args, 0);
1236 args [count] = implicit_value_parameter;
1239 current_local_parameters = new Parameters (
1240 args, indexer_parameters.ArrayParameter, lexer.Location);
1243 lexer.PropertyParsing = false;
1247 $$ = new Accessor ((Block) $4, (Attributes) $1, lexer.Location);
1248 current_local_parameters = null;
1249 lexer.PropertyParsing = true;
1255 | SEMICOLON { $$ = null; }
1258 interface_declaration
1262 INTERFACE member_name
1264 MemberName name = MakeName ((MemberName) $5);
1265 bool partial = (bool) $3;
1268 ClassPart part = PartialContainer.CreatePart (
1269 current_namespace, current_container, name, (int) $2,
1270 (Attributes) $1, Kind.Interface, lexer.Location);
1272 current_container = part.PartialContainer;
1273 current_class = part;
1275 current_class = new Interface (
1276 current_namespace, current_container, name, (int) $2,
1277 (Attributes) $1, lexer.Location);
1279 current_container = current_class;
1280 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
1285 current_class.Bases = (ArrayList) $7;
1287 current_class.Register ();
1294 current_container = current_container.Parent;
1295 current_class = current_container;
1297 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1298 CheckIdentifierToken (yyToken);
1304 opt_interface_member_declarations
1308 opt_interface_member_declarations
1310 | interface_member_declarations
1313 interface_member_declarations
1314 : interface_member_declaration
1315 | interface_member_declarations interface_member_declaration
1318 interface_member_declaration
1319 : interface_method_declaration
1321 Method m = (Method) $1;
1323 current_container.AddMethod (m);
1325 | interface_property_declaration
1327 Property p = (Property) $1;
1329 current_container.AddProperty (p);
1331 | interface_event_declaration
1334 Event e = (Event) $1;
1335 current_container.AddEvent (e);
1338 | interface_indexer_declaration
1340 Indexer i = (Indexer) $1;
1342 current_container.AddIndexer (i);
1350 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, lexer.Location);
1355 interface_method_declaration
1356 : opt_attributes opt_new type namespace_or_type_name
1357 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1360 MemberName name = (MemberName) $4;
1362 $$ = new Method (current_class, (Expression) $3, (int) $2, true,
1363 name, (Parameters) $6, (Attributes) $1, lexer.Location);
1365 | opt_attributes opt_new VOID namespace_or_type_name
1366 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1369 MemberName name = (MemberName) $4;
1371 $$ = new Method (current_class, TypeManager.system_void_expr, (int) $2,
1372 true, name, (Parameters) $6, (Attributes) $1, lexer.Location);
1376 interface_property_declaration
1381 { lexer.PropertyParsing = true; }
1383 { lexer.PropertyParsing = false; }
1386 InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
1388 $$ = new Property (current_class, (Expression) $3, (int) $2, true,
1389 new MemberName ((string) $4), (Attributes) $1,
1390 pinfo.Get, pinfo.Set, lexer.Location);
1395 CheckIdentifierToken (yyToken);
1401 : opt_attributes GET SEMICOLON { $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, lexer.Location, lexer.Location); }
1402 | opt_attributes SET SEMICOLON { $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1, lexer.Location, lexer.Location); }
1403 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1404 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $3, lexer.Location, lexer.Location); }
1405 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1406 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $3, (Attributes) $1, lexer.Location, lexer.Location); }
1409 interface_event_declaration
1410 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1412 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1413 new MemberName ((string) $5), null,
1414 (Attributes) $1, lexer.Location);
1416 | opt_attributes opt_new EVENT type error {
1417 CheckIdentifierToken (yyToken);
1420 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1421 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1424 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE event_accessor_declarations CLOSE_BRACE {
1425 Report.Error (69, lexer.Location, "Event accessors not valid on interfaces");
1430 interface_indexer_declaration
1431 : opt_attributes opt_new type THIS
1432 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1434 { lexer.PropertyParsing = true; }
1436 { lexer.PropertyParsing = false; }
1439 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1441 $$ = new Indexer (current_class, (Expression) $3,
1442 new MemberName (TypeContainer.DefaultIndexerName),
1443 (int) $2, true, (Parameters) $6, (Attributes) $1,
1444 info.Get, info.Set, lexer.Location);
1448 operator_declaration
1449 : opt_attributes opt_modifiers operator_declarator
1451 iterator_container = SimpleIteratorContainer.GetSimple ();
1455 OperatorDeclaration decl = (OperatorDeclaration) $3;
1457 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1459 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null);
1460 if (decl.arg2type != null)
1461 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null);
1463 Operator op = new Operator (
1464 current_class, decl.optype, decl.ret_type, (int) $2,
1465 new Parameters (param_list, null, decl.location),
1466 (Block) $5, (Attributes) $1, decl.location);
1468 if (SimpleIteratorContainer.Simple.Yields)
1471 // Note again, checking is done in semantic analysis
1472 current_container.AddOperator (op);
1474 current_local_parameters = null;
1475 iterator_container = null;
1481 | SEMICOLON { $$ = null; }
1484 : type OPERATOR overloadable_operator
1485 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1487 Operator.OpType op = (Operator.OpType) $3;
1488 CheckUnaryOperator (op);
1490 if (op == Operator.OpType.Addition)
1491 op = Operator.OpType.UnaryPlus;
1493 if (op == Operator.OpType.Subtraction)
1494 op = Operator.OpType.UnaryNegation;
1496 Parameter [] pars = new Parameter [1];
1498 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1500 current_local_parameters = new Parameters (pars, null, lexer.Location);
1502 $$ = new OperatorDeclaration (op, (Expression) $1, (Expression) $5, (string) $6,
1503 null, null, lexer.Location);
1505 | type OPERATOR overloadable_operator
1507 type IDENTIFIER COMMA
1511 CheckBinaryOperator ((Operator.OpType) $3);
1513 Parameter [] pars = new Parameter [2];
1515 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1516 pars [1] = new Parameter ((Expression) $8, (string) $9, Parameter.Modifier.NONE, null);
1518 current_local_parameters = new Parameters (pars, null, lexer.Location);
1520 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1521 (Expression) $5, (string) $6,
1522 (Expression) $8, (string) $9, lexer.Location);
1524 | conversion_operator_declarator
1527 overloadable_operator
1529 : BANG { $$ = Operator.OpType.LogicalNot; }
1530 | TILDE { $$ = Operator.OpType.OnesComplement; }
1531 | OP_INC { $$ = Operator.OpType.Increment; }
1532 | OP_DEC { $$ = Operator.OpType.Decrement; }
1533 | TRUE { $$ = Operator.OpType.True; }
1534 | FALSE { $$ = Operator.OpType.False; }
1535 // Unary and binary:
1536 | PLUS { $$ = Operator.OpType.Addition; }
1537 | MINUS { $$ = Operator.OpType.Subtraction; }
1539 | STAR { $$ = Operator.OpType.Multiply; }
1540 | DIV { $$ = Operator.OpType.Division; }
1541 | PERCENT { $$ = Operator.OpType.Modulus; }
1542 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1543 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1544 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1545 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1546 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1547 | OP_EQ { $$ = Operator.OpType.Equality; }
1548 | OP_NE { $$ = Operator.OpType.Inequality; }
1549 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1550 | OP_LT { $$ = Operator.OpType.LessThan; }
1551 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1552 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1555 conversion_operator_declarator
1556 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1558 Parameter [] pars = new Parameter [1];
1560 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1562 current_local_parameters = new Parameters (pars, null, lexer.Location);
1564 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1565 null, null, lexer.Location);
1567 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1569 Parameter [] pars = new Parameter [1];
1571 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1573 current_local_parameters = new Parameters (pars, null, lexer.Location);
1575 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1576 null, null, lexer.Location);
1580 syntax_error (lexer.Location, "'operator' expected");
1584 syntax_error (lexer.Location, "'operator' expected");
1588 constructor_declaration
1591 constructor_declarator
1594 Constructor c = (Constructor) $3;
1595 c.Block = (Block) $4;
1596 c.OptAttributes = (Attributes) $1;
1597 c.ModFlags = (int) $2;
1599 if (c.Name == current_container.Basename){
1600 if ((c.ModFlags & Modifiers.STATIC) != 0){
1601 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1603 515, c.Location, String.Format (
1604 "`{0}.{1}': static constructor can not have access modifiers",
1605 c.Name, current_container.Name));
1608 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1610 if (c.Initializer != null){
1613 "Static constructors can not have an explicit this or base " +
1614 "constructor invocations");
1617 if (!c.Parameters.Empty){
1619 132, c.Location, "Static constructors should not have parameters");
1622 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1625 // We let another layer check the validity of the constructor.
1626 Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1629 current_container.AddConstructor (c);
1631 current_local_parameters = null;
1635 constructor_declarator
1637 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1639 oob_stack.Push (lexer.Location);
1641 current_local_parameters = (Parameters) $3;
1643 opt_constructor_initializer
1645 Location l = (Location) oob_stack.Pop ();
1646 $$ = new Constructor (current_class, (string) $1, 0, (Parameters) $3,
1647 (ConstructorInitializer) $6, l);
1653 | SEMICOLON { $$ = null; }
1656 opt_constructor_initializer
1657 : /* empty */ { $$ = null; }
1658 | constructor_initializer
1661 constructor_initializer
1662 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1664 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1666 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1668 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1671 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1677 : /* EMPTY */ { $$ = 0; }
1678 | UNSAFE { $$ = Modifiers.UNSAFE; }
1679 | EXTERN { $$ = Modifiers.EXTERN; }
1682 destructor_declaration
1683 : opt_attributes opt_finalizer TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1685 if ((string) $4 != current_container.Basename){
1686 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1687 } else if (!(current_container is Class)){
1688 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1690 Location l = lexer.Location;
1693 if (!RootContext.StdLib && current_container.Name == "System.Object")
1694 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
1696 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
1698 if ((m & Modifiers.UNSAFE) != 0){
1699 if (!RootContext.Unsafe){
1700 Report.Error (227, l,
1701 "Unsafe code requires the -unsafe command " +
1702 "line option to be specified");
1706 Method d = new Destructor (
1707 current_class, TypeManager.system_void_expr, m, "Finalize",
1708 new Parameters (null, null, l), (Attributes) $1, l);
1710 d.Block = (Block) $7;
1711 current_container.AddMethod (d);
1719 EVENT type variable_declarators SEMICOLON
1721 foreach (VariableDeclaration var in (ArrayList) $5) {
1723 MemberName name = new MemberName (var.identifier);
1725 Event e = new EventField (
1726 current_class, (Expression) $4, (int) $2, false, name,
1727 var.expression_or_array_initializer, (Attributes) $1,
1730 current_container.AddEvent (e);
1736 EVENT type namespace_or_type_name
1739 implicit_value_parameter_type = (Expression) $4;
1740 lexer.EventParsing = true;
1741 oob_stack.Push (lexer.Location);
1743 event_accessor_declarations
1745 lexer.EventParsing = false;
1749 Location loc = (Location) oob_stack.Pop ();
1752 Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
1755 Pair pair = (Pair) $8;
1757 MemberName name = (MemberName) $5;
1759 Event e = new EventProperty (
1760 current_class, (Expression) $4, (int) $2, false, name, null,
1761 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
1764 current_container.AddEvent (e);
1765 implicit_value_parameter_type = null;
1768 | opt_attributes opt_modifiers EVENT type namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS block {
1769 MemberName mn = (MemberName) $5;
1771 if (mn.Left != null)
1772 Report.Error (71, lexer.Location, "Explicit implementation of events requires property syntax");
1774 Report.Error (71, lexer.Location, "Event declaration should use property syntax");
1778 event_accessor_declarations
1779 : add_accessor_declaration remove_accessor_declaration
1781 $$ = new Pair ($1, $2);
1783 | remove_accessor_declaration add_accessor_declaration
1785 $$ = new Pair ($2, $1);
1787 | add_accessor_declaration { $$ = null; }
1788 | remove_accessor_declaration { $$ = null; }
1791 add_accessor_declaration
1792 : opt_attributes ADD
1794 Parameter [] args = new Parameter [1];
1795 Parameter implicit_value_parameter = new Parameter (
1796 implicit_value_parameter_type, "value",
1797 Parameter.Modifier.NONE, null);
1799 args [0] = implicit_value_parameter;
1801 current_local_parameters = new Parameters (args, null, lexer.Location);
1802 lexer.EventParsing = false;
1806 $$ = new Accessor ((Block) $4, (Attributes) $1, lexer.Location);
1807 lexer.EventParsing = true;
1809 | opt_attributes ADD error {
1810 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1815 remove_accessor_declaration
1816 : opt_attributes REMOVE
1818 Parameter [] args = new Parameter [1];
1819 Parameter implicit_value_parameter = new Parameter (
1820 implicit_value_parameter_type, "value",
1821 Parameter.Modifier.NONE, null);
1823 args [0] = implicit_value_parameter;
1825 current_local_parameters = new Parameters (args, null, lexer.Location);
1826 lexer.EventParsing = false;
1830 $$ = new Accessor ((Block) $4, (Attributes) $1, lexer.Location);
1831 lexer.EventParsing = true;
1833 | opt_attributes REMOVE error {
1834 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1840 : opt_attributes opt_modifiers indexer_declarator
1843 IndexerDeclaration decl = (IndexerDeclaration) $3;
1845 implicit_value_parameter_type = decl.type;
1847 lexer.PropertyParsing = true;
1848 parsing_indexer = true;
1850 indexer_parameters = decl.param_list;
1851 oob_stack.Push (lexer.Location);
1853 accessor_declarations
1855 lexer.PropertyParsing = false;
1856 parsing_indexer = false;
1860 // The signature is computed from the signature of the indexer. Look
1861 // at section 3.6 on the spec
1862 Location loc = (Location) oob_stack.Pop ();
1864 IndexerDeclaration decl = (IndexerDeclaration) $3;
1865 Pair pair = (Pair) $6;
1866 Accessor get_block = (Accessor) pair.First;
1867 Accessor set_block = (Accessor) pair.Second;
1870 if (decl.interface_type != null)
1871 name = new MemberName (decl.interface_type,
1872 TypeContainer.DefaultIndexerName);
1874 name = new MemberName (TypeContainer.DefaultIndexerName);
1876 indexer = new Indexer (current_class, decl.type, name,
1877 (int) $2, false, decl.param_list, (Attributes) $1,
1878 get_block, set_block, loc);
1880 current_container.AddIndexer (indexer);
1882 current_local_parameters = null;
1883 implicit_value_parameter_type = null;
1884 indexer_parameters = null;
1889 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1891 Parameters pars = (Parameters) $4;
1892 if (pars.HasArglist) {
1893 // "__arglist is not valid in this context"
1894 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
1895 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
1896 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1899 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
1901 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1903 Parameters pars = (Parameters) $6;
1905 if (pars.HasArglist) {
1906 // "__arglist is not valid in this context"
1907 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
1908 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
1909 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1911 MemberName name = (MemberName) $2;
1912 $$ = new IndexerDeclaration ((Expression) $1, name, pars);
1924 Location enum_location = lexer.Location;
1926 MemberName full_name = MakeName (new MemberName ((string) $4));
1927 Enum e = new Enum (current_namespace, current_container, (Expression) $5, (int) $2,
1928 full_name, (Attributes) $1, enum_location);
1930 foreach (VariableDeclaration ev in (ArrayList) $6) {
1931 e.AddEnumMember (ev.identifier,
1932 (Expression) ev.expression_or_array_initializer,
1933 ev.Location, ev.OptAttributes);
1936 string name = full_name.GetName (false);
1937 current_container.AddEnum (e);
1938 RootContext.Tree.RecordDecl (name, e);
1944 : /* empty */ { $$ = TypeManager.system_int32_expr; }
1945 | COLON type { $$ = $2; }
1949 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1955 opt_enum_member_declarations
1956 : /* empty */ { $$ = new ArrayList (4); }
1957 | enum_member_declarations opt_comma { $$ = $1; }
1960 enum_member_declarations
1961 : enum_member_declaration
1963 ArrayList l = new ArrayList (4);
1968 | enum_member_declarations COMMA enum_member_declaration
1970 ArrayList l = (ArrayList) $1;
1978 enum_member_declaration
1979 : opt_attributes IDENTIFIER
1981 $$ = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
1983 | opt_attributes IDENTIFIER
1985 $$ = lexer.Location;
1989 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
1993 delegate_declaration
1996 DELEGATE type member_name
1997 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2000 Location l = lexer.Location;
2001 MemberName name = MakeName ((MemberName) $5);
2002 Delegate del = new Delegate (current_namespace, current_container, (Expression) $4,
2003 (int) $2, name, (Parameters) $7, (Attributes) $1, l);
2005 current_container.AddDelegate (del);
2006 RootContext.Tree.RecordDecl (name.GetName (false), del);
2010 DELEGATE VOID member_name
2011 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2014 Location l = lexer.Location;
2015 MemberName name = MakeName ((MemberName) $5);
2016 Delegate del = new Delegate (
2017 current_namespace, current_container,
2018 TypeManager.system_void_expr, (int) $2, name,
2019 (Parameters) $7, (Attributes) $1, l);
2021 current_container.AddDelegate (del);
2022 RootContext.Tree.RecordDecl (name.GetName (false), del);
2026 namespace_or_type_name
2028 | namespace_or_type_name DOT IDENTIFIER {
2029 $$ = new MemberName ((MemberName) $1, (string) $3);
2035 $$ = new MemberName ((string) $1);
2040 * Before you think of adding a return_type, notice that we have been
2041 * using two rules in the places where it matters (one rule using type
2042 * and another identical one that uses VOID as the return type). This
2043 * gets rid of a shift/reduce couple
2046 : namespace_or_type_name
2048 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2060 // Note that here only unmanaged types are allowed but we
2061 // can't perform checks during this phase - we do it during
2062 // semantic analysis.
2064 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2068 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2074 | non_expression_type rank_specifier
2076 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2078 | non_expression_type STAR
2080 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2082 | expression rank_specifiers
2084 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2088 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2092 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2093 // through this different path
2095 | multiplicative_expression STAR
2097 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2104 ArrayList types = new ArrayList (4);
2109 | type_list COMMA type
2111 ArrayList types = (ArrayList) $1;
2119 * replaces all the productions for isolating the various
2120 * simple types, but we need this to reuse it easily in local_variable_type
2123 : OBJECT { $$ = TypeManager.system_object_expr; }
2124 | STRING { $$ = TypeManager.system_string_expr; }
2125 | BOOL { $$ = TypeManager.system_boolean_expr; }
2126 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2127 | FLOAT { $$ = TypeManager.system_single_expr; }
2128 | DOUBLE { $$ = TypeManager.system_double_expr; }
2133 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2134 | BYTE { $$ = TypeManager.system_byte_expr; }
2135 | SHORT { $$ = TypeManager.system_int16_expr; }
2136 | USHORT { $$ = TypeManager.system_uint16_expr; }
2137 | INT { $$ = TypeManager.system_int32_expr; }
2138 | UINT { $$ = TypeManager.system_uint32_expr; }
2139 | LONG { $$ = TypeManager.system_int64_expr; }
2140 | ULONG { $$ = TypeManager.system_uint64_expr; }
2141 | CHAR { $$ = TypeManager.system_char_expr; }
2142 | VOID { $$ = TypeManager.system_void_expr; }
2146 : type rank_specifiers
2148 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2153 // Expressions, section 7.5
2163 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2165 | parenthesized_expression
2167 | invocation_expression
2171 | post_increment_expression
2172 | post_decrement_expression
2176 | checked_expression
2177 | unchecked_expression
2178 | pointer_member_access
2179 | anonymous_method_expression
2186 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2187 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2188 | NULL { $$ = NullLiteral.Null; }
2192 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2193 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2194 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2199 object v = lexer.Value;
2205 $$ = IntLiteral.Zero;
2207 $$ = IntLiteral.One;
2209 $$ = new IntLiteral (i);
2210 } else if (v is uint)
2211 $$ = new UIntLiteral ((UInt32) v);
2213 $$ = new LongLiteral ((Int64) v);
2214 else if (v is ulong)
2215 $$ = new ULongLiteral ((UInt64) v);
2217 Console.WriteLine ("OOPS. Unexpected result from scanner");
2222 : TRUE { $$ = new BoolLiteral (true); }
2223 | FALSE { $$ = new BoolLiteral (false); }
2226 parenthesized_expression_0
2227 : OPEN_PARENS expression CLOSE_PARENS
2230 lexer.Deambiguate_CloseParens ();
2231 // After this, the next token returned is one of
2232 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2233 // or CLOSE_PARENS_MINUS.
2237 parenthesized_expression
2238 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2242 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2244 // If a parenthesized expression is followed by a minus, we need to wrap
2245 // the expression inside a ParenthesizedExpression for the CS0075 check
2246 // in Binary.DoResolve().
2247 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2252 : primary_expression DOT IDENTIFIER
2254 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2256 | predefined_type DOT IDENTIFIER
2258 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2266 invocation_expression
2267 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2270 Location l = lexer.Location;
2271 Report.Error (1, l, "Parse error");
2273 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2275 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2277 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2279 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2281 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2286 : /* empty */ { $$ = null; }
2293 ArrayList list = new ArrayList (4);
2297 | argument_list COMMA argument
2299 ArrayList list = (ArrayList) $1;
2303 | argument_list error {
2304 CheckToken (1026, yyToken, ", or ) expected");
2311 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2313 | REF variable_reference
2315 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2317 | OUT variable_reference
2319 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2321 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2323 ArrayList list = (ArrayList) $3;
2324 Argument[] args = new Argument [list.Count];
2325 list.CopyTo (args, 0);
2327 Expression expr = new Arglist (args, lexer.Location);
2328 $$ = new Argument (expr, Argument.AType.Expression);
2332 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2337 : expression { note ("section 5.4"); $$ = $1; }
2341 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2343 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2345 | primary_expression rank_specifiers
2347 // So the super-trick is that primary_expression
2348 // can only be either a SimpleName or a MemberAccess.
2349 // The MemberAccess case arises when you have a fully qualified type-name like :
2351 // SimpleName is when you have
2354 Expression expr = (Expression) $1;
2355 if (expr is ComposedCast){
2356 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2357 } else if (!(expr is SimpleName || expr is MemberAccess)){
2358 Error_ExpectingTypeName (lexer.Location, expr);
2359 $$ = TypeManager.system_object_expr;
2362 // So we extract the string corresponding to the SimpleName
2365 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2373 ArrayList list = new ArrayList (4);
2377 | expression_list COMMA expression
2379 ArrayList list = (ArrayList) $1;
2388 $$ = new This (current_block, lexer.Location);
2393 : BASE DOT IDENTIFIER
2395 $$ = new BaseAccess ((string) $3, lexer.Location);
2397 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2399 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2402 Report.Error (175, "Use of keyword `base' is not valid in this context");
2407 post_increment_expression
2408 : primary_expression OP_INC
2410 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2411 (Expression) $1, lexer.Location);
2415 post_decrement_expression
2416 : primary_expression OP_DEC
2418 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2419 (Expression) $1, lexer.Location);
2424 : object_or_delegate_creation_expression
2425 | array_creation_expression
2428 object_or_delegate_creation_expression
2429 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2431 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2435 array_creation_expression
2436 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2438 opt_array_initializer
2440 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2442 | NEW type rank_specifiers array_initializer
2444 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2448 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2464 : rank_specifier opt_rank_specifier
2466 $$ = (string) $2 + (string) $1;
2471 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2473 $$ = "[" + (string) $2 + "]";
2493 | dim_separators COMMA
2495 $$ = (string) $1 + ",";
2499 opt_array_initializer
2511 : OPEN_BRACE CLOSE_BRACE
2513 ArrayList list = new ArrayList (4);
2516 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2518 $$ = (ArrayList) $2;
2522 variable_initializer_list
2523 : variable_initializer
2525 ArrayList list = new ArrayList (4);
2529 | variable_initializer_list COMMA variable_initializer
2531 ArrayList list = (ArrayList) $1;
2538 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2540 $$ = new TypeOfVoid (lexer.Location);
2542 | TYPEOF OPEN_PARENS type CLOSE_PARENS
2544 $$ = new TypeOf ((Expression) $3, lexer.Location);
2549 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2550 $$ = new SizeOf ((Expression) $3, lexer.Location);
2555 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2557 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2561 unchecked_expression
2562 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2564 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2568 pointer_member_access
2569 : primary_expression OP_PTR IDENTIFIER
2573 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2574 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2578 anonymous_method_expression
2579 : DELEGATE opt_anonymous_method_signature {
2580 oob_stack.Push (current_local_parameters);
2581 current_local_parameters = (Parameters)$2;
2582 create_toplevel_block = true;
2585 Report.Error (-213, lexer.Location, "Anonymous methods are not supported in this branch");
2588 create_toplevel_block = false;
2589 if (RootContext.Version == LanguageVersion.ISO_1){
2590 Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
2593 $$ = new AnonymousMethod ((Parameters) $2, (Block) $4, lexer.Location);
2594 current_local_parameters = (Parameters) oob_stack.Pop ();
2599 opt_anonymous_method_signature
2600 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
2601 | anonymous_method_signature
2604 anonymous_method_signature
2605 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
2608 $$ = Parameters.EmptyReadOnlyParameters;
2610 ArrayList par_list = (ArrayList) $2;
2611 Parameter [] pars = new Parameter [par_list.Count];
2612 par_list.CopyTo (pars);
2613 $$ = new Parameters (pars, null, lexer.Location);
2618 opt_anonymous_method_parameter_list
2619 : /* empty */ { $$ = null; }
2620 | anonymous_method_parameter_list { $$ = $1; }
2623 anonymous_method_parameter_list
2624 : anonymous_method_parameter
2626 ArrayList a = new ArrayList (4);
2630 | anonymous_method_parameter_list COMMA anonymous_method_parameter
2632 ArrayList a = (ArrayList) $1;
2638 anonymous_method_parameter
2639 : opt_parameter_modifier type IDENTIFIER {
2640 $$ = new Parameter ((Expression) $2, (string) $2, (Parameter.Modifier) $1, null);
2645 : primary_expression
2646 | BANG prefixed_unary_expression
2648 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2650 | TILDE prefixed_unary_expression
2652 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2658 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
2660 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2662 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
2664 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2670 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2672 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2677 // The idea to split this out is from Rhys' grammar
2678 // to solve the problem with casts.
2680 prefixed_unary_expression
2682 | PLUS prefixed_unary_expression
2684 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2686 | MINUS prefixed_unary_expression
2688 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2690 | OP_INC prefixed_unary_expression
2692 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2693 (Expression) $2, lexer.Location);
2695 | OP_DEC prefixed_unary_expression
2697 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2698 (Expression) $2, lexer.Location);
2700 | STAR prefixed_unary_expression
2702 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2704 | BITWISE_AND prefixed_unary_expression
2706 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2710 pre_increment_expression
2711 : OP_INC prefixed_unary_expression
2713 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2714 (Expression) $2, lexer.Location);
2718 pre_decrement_expression
2719 : OP_DEC prefixed_unary_expression
2721 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2722 (Expression) $2, lexer.Location);
2726 multiplicative_expression
2727 : prefixed_unary_expression
2728 | multiplicative_expression STAR prefixed_unary_expression
2730 $$ = new Binary (Binary.Operator.Multiply,
2731 (Expression) $1, (Expression) $3, lexer.Location);
2733 | multiplicative_expression DIV prefixed_unary_expression
2735 $$ = new Binary (Binary.Operator.Division,
2736 (Expression) $1, (Expression) $3, lexer.Location);
2738 | multiplicative_expression PERCENT prefixed_unary_expression
2740 $$ = new Binary (Binary.Operator.Modulus,
2741 (Expression) $1, (Expression) $3, lexer.Location);
2746 : multiplicative_expression
2747 | additive_expression PLUS multiplicative_expression
2749 $$ = new Binary (Binary.Operator.Addition,
2750 (Expression) $1, (Expression) $3, lexer.Location);
2752 | additive_expression MINUS multiplicative_expression
2754 $$ = new Binary (Binary.Operator.Subtraction,
2755 (Expression) $1, (Expression) $3, lexer.Location);
2760 : additive_expression
2761 | shift_expression OP_SHIFT_LEFT additive_expression
2763 $$ = new Binary (Binary.Operator.LeftShift,
2764 (Expression) $1, (Expression) $3, lexer.Location);
2766 | shift_expression OP_SHIFT_RIGHT additive_expression
2768 $$ = new Binary (Binary.Operator.RightShift,
2769 (Expression) $1, (Expression) $3, lexer.Location);
2773 relational_expression
2775 | relational_expression OP_LT shift_expression
2777 $$ = new Binary (Binary.Operator.LessThan,
2778 (Expression) $1, (Expression) $3, lexer.Location);
2780 | relational_expression OP_GT shift_expression
2782 $$ = new Binary (Binary.Operator.GreaterThan,
2783 (Expression) $1, (Expression) $3, lexer.Location);
2785 | relational_expression OP_LE shift_expression
2787 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2788 (Expression) $1, (Expression) $3, lexer.Location);
2790 | relational_expression OP_GE shift_expression
2792 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2793 (Expression) $1, (Expression) $3, lexer.Location);
2795 | relational_expression IS type
2797 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
2799 | relational_expression AS type
2801 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
2806 : relational_expression
2807 | equality_expression OP_EQ relational_expression
2809 $$ = new Binary (Binary.Operator.Equality,
2810 (Expression) $1, (Expression) $3, lexer.Location);
2812 | equality_expression OP_NE relational_expression
2814 $$ = new Binary (Binary.Operator.Inequality,
2815 (Expression) $1, (Expression) $3, lexer.Location);
2820 : equality_expression
2821 | and_expression BITWISE_AND equality_expression
2823 $$ = new Binary (Binary.Operator.BitwiseAnd,
2824 (Expression) $1, (Expression) $3, lexer.Location);
2828 exclusive_or_expression
2830 | exclusive_or_expression CARRET and_expression
2832 $$ = new Binary (Binary.Operator.ExclusiveOr,
2833 (Expression) $1, (Expression) $3, lexer.Location);
2837 inclusive_or_expression
2838 : exclusive_or_expression
2839 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2841 $$ = new Binary (Binary.Operator.BitwiseOr,
2842 (Expression) $1, (Expression) $3, lexer.Location);
2846 conditional_and_expression
2847 : inclusive_or_expression
2848 | conditional_and_expression OP_AND inclusive_or_expression
2850 $$ = new Binary (Binary.Operator.LogicalAnd,
2851 (Expression) $1, (Expression) $3, lexer.Location);
2855 conditional_or_expression
2856 : conditional_and_expression
2857 | conditional_or_expression OP_OR conditional_and_expression
2859 $$ = new Binary (Binary.Operator.LogicalOr,
2860 (Expression) $1, (Expression) $3, lexer.Location);
2864 conditional_expression
2865 : conditional_or_expression
2866 | conditional_or_expression INTERR expression COLON expression
2868 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2872 assignment_expression
2873 : prefixed_unary_expression ASSIGN expression
2875 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2877 | prefixed_unary_expression OP_MULT_ASSIGN expression
2879 Location l = lexer.Location;
2881 $$ = new CompoundAssign (
2882 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
2884 | prefixed_unary_expression OP_DIV_ASSIGN expression
2886 Location l = lexer.Location;
2888 $$ = new CompoundAssign (
2889 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
2891 | prefixed_unary_expression OP_MOD_ASSIGN expression
2893 Location l = lexer.Location;
2895 $$ = new CompoundAssign (
2896 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
2898 | prefixed_unary_expression OP_ADD_ASSIGN expression
2900 Location l = lexer.Location;
2902 $$ = new CompoundAssign (
2903 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
2905 | prefixed_unary_expression OP_SUB_ASSIGN expression
2907 Location l = lexer.Location;
2909 $$ = new CompoundAssign (
2910 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
2912 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2914 Location l = lexer.Location;
2916 $$ = new CompoundAssign (
2917 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
2919 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2921 Location l = lexer.Location;
2923 $$ = new CompoundAssign (
2924 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
2926 | prefixed_unary_expression OP_AND_ASSIGN expression
2928 Location l = lexer.Location;
2930 $$ = new CompoundAssign (
2931 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
2933 | prefixed_unary_expression OP_OR_ASSIGN expression
2935 Location l = lexer.Location;
2937 $$ = new CompoundAssign (
2938 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
2940 | prefixed_unary_expression OP_XOR_ASSIGN expression
2942 Location l = lexer.Location;
2944 $$ = new CompoundAssign (
2945 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
2950 : conditional_expression
2951 | assignment_expression
2971 MemberName name = MakeName ((MemberName) $5);
2972 bool partial = (bool) $3;
2973 int mod_flags = (int) $2;
2976 ClassPart part = PartialContainer.CreatePart (
2977 current_namespace, current_container, name, mod_flags,
2978 (Attributes) $1, Kind.Class, lexer.Location);
2980 current_container = part.PartialContainer;
2981 current_class = part;
2983 if ((mod_flags & Modifiers.STATIC) != 0) {
2984 current_class = new StaticClass (
2985 current_namespace, current_container, name,
2986 mod_flags, (Attributes) $1, lexer.Location);
2988 current_class = new Class (
2989 current_namespace, current_container, name,
2990 mod_flags, (Attributes) $1, lexer.Location);
2993 current_container = current_class;
2994 RootContext.Tree.RecordDecl (name.GetName (false), current_class);
3000 if (current_class.Name == "System.Object") {
3001 Report.Error (537, current_class.Location,
3002 "The class System.Object cannot have a base " +
3003 "class or implement an interface.");
3005 current_class.Bases = (ArrayList) $7;
3008 current_class.Register ();
3015 current_container = current_container.Parent;
3016 current_class = current_container;
3022 { $$ = (bool) false; }
3024 { $$ = (bool) true; }
3028 : /* empty */ { $$ = (int) 0; }
3034 | modifiers modifier
3039 if ((m1 & m2) != 0) {
3040 Location l = lexer.Location;
3041 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3043 $$ = (int) (m1 | m2);
3048 : NEW { $$ = Modifiers.NEW; }
3049 | PUBLIC { $$ = Modifiers.PUBLIC; }
3050 | PROTECTED { $$ = Modifiers.PROTECTED; }
3051 | INTERNAL { $$ = Modifiers.INTERNAL; }
3052 | PRIVATE { $$ = Modifiers.PRIVATE; }
3053 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3054 | SEALED { $$ = Modifiers.SEALED; }
3055 | STATIC { $$ = Modifiers.STATIC; }
3056 | READONLY { $$ = Modifiers.READONLY; }
3057 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3058 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3059 | EXTERN { $$ = Modifiers.EXTERN; }
3060 | VOLATILE { $$ = Modifiers.VOLATILE; }
3061 | UNSAFE { $$ = Modifiers.UNSAFE; }
3065 : /* empty */ { $$ = null; }
3066 | class_base { $$ = $1; }
3070 : COLON type_list { $$ = $2; }
3078 // A block is "contained" on the following places:
3080 // property_declaration as part of the accessor body (get/set)
3081 // operator_declaration
3082 // constructor_declaration
3083 // destructor_declaration
3084 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3089 if (current_block == null || create_toplevel_block){
3090 current_block = new ToplevelBlock (current_local_parameters, lexer.Location);
3092 current_block = new Block (current_block, current_local_parameters,
3093 lexer.Location, Location.Null);
3096 opt_statement_list CLOSE_BRACE
3098 while (current_block.Implicit)
3099 current_block = current_block.Parent;
3101 current_block.SetEndLocation (lexer.Location);
3102 current_block = current_block.Parent;
3113 | statement_list statement
3117 : declaration_statement
3119 if ($1 != null && (Block) $1 != current_block){
3120 current_block.AddStatement ((Statement) $1);
3121 current_block = (Block) $1;
3124 | valid_declaration_statement
3126 current_block.AddStatement ((Statement) $1);
3131 valid_declaration_statement
3134 | expression_statement
3135 | selection_statement
3136 | iteration_statement
3140 | unchecked_statement
3148 : valid_declaration_statement
3149 | declaration_statement
3151 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3156 Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3164 $$ = EmptyStatement.Value;
3171 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3173 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3174 current_block.AddStatement (labeled);
3179 declaration_statement
3180 : local_variable_declaration SEMICOLON
3183 DictionaryEntry de = (DictionaryEntry) $1;
3185 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3189 | local_constant_declaration SEMICOLON
3192 DictionaryEntry de = (DictionaryEntry) $1;
3194 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3200 * The following is from Rhys' grammar:
3201 * > Types in local variable declarations must be recognized as
3202 * > expressions to prevent reduce/reduce errors in the grammar.
3203 * > The expressions are converted into types during semantic analysis.
3206 : primary_expression opt_rank_specifier
3208 // FIXME: Do something smart here regarding the composition of the type.
3210 // Ok, the above "primary_expression" is there to get rid of
3211 // both reduce/reduce and shift/reduces in the grammar, it should
3212 // really just be "type_name". If you use type_name, a reduce/reduce
3213 // creeps up. If you use namespace_or_type_name (which is all we need
3214 // really) two shift/reduces appear.
3217 // So the super-trick is that primary_expression
3218 // can only be either a SimpleName or a MemberAccess.
3219 // The MemberAccess case arises when you have a fully qualified type-name like :
3221 // SimpleName is when you have
3224 Expression expr = (Expression) $1;
3225 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3226 Error_ExpectingTypeName (lexer.Location, expr);
3230 // So we extract the string corresponding to the SimpleName
3234 if ((string) $2 == "")
3237 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3240 | builtin_types opt_rank_specifier
3242 if ((string) $2 == "")
3245 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3249 local_variable_pointer_type
3250 : primary_expression STAR
3252 Expression expr = (Expression) $1;
3253 Location l = lexer.Location;
3255 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3256 Error_ExpectingTypeName (l, expr);
3260 $$ = new ComposedCast ((Expression) $1, "*", l);
3262 | builtin_types STAR
3264 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3268 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3270 | local_variable_pointer_type STAR
3272 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3276 local_variable_declaration
3277 : local_variable_type variable_declarators
3280 $$ = new DictionaryEntry ($1, $2);
3284 | local_variable_pointer_type opt_rank_specifier variable_declarators
3289 if ((string) $2 == "")
3290 t = (Expression) $1;
3292 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3293 $$ = new DictionaryEntry (t, $3);
3299 local_constant_declaration
3300 : CONST local_variable_type constant_declarators
3303 $$ = new DictionaryEntry ($2, $3);
3309 expression_statement
3310 : statement_expression SEMICOLON
3317 // We have to do the wrapping here and not in the case above,
3318 // because statement_expression is used for example in for_statement
3320 statement_expression
3321 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3322 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3323 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3324 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3325 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3326 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3327 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3329 Report.Error (1002, lexer.Location, "Expecting `;'");
3334 object_creation_expression
3335 : object_or_delegate_creation_expression
3336 { note ("complain if this is a delegate maybe?"); }
3345 : if_statement_open if_statement_rest
3354 oob_stack.Push (lexer.Location);
3359 : boolean_expression CLOSE_PARENS
3362 Location l = (Location) oob_stack.Pop ();
3364 $$ = new If ((Expression) $1, (Statement) $3, l);
3366 if (RootContext.WarningLevel >= 3){
3367 if ($3 == EmptyStatement.Value)
3368 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3372 | boolean_expression CLOSE_PARENS
3373 embedded_statement ELSE embedded_statement
3375 Location l = (Location) oob_stack.Pop ();
3377 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
3382 : SWITCH OPEN_PARENS
3384 oob_stack.Push (lexer.Location);
3385 switch_stack.Push (current_block);
3387 expression CLOSE_PARENS
3390 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
3391 current_block = (Block) switch_stack.Pop ();
3407 Report.Error (1522, lexer.Location, "Empty switch block");
3415 ArrayList sections = new ArrayList (4);
3420 | switch_sections switch_section
3422 ArrayList sections = (ArrayList) $1;
3432 current_block = current_block.CreateSwitchBlock (lexer.Location);
3436 Block topmost = current_block;
3438 while (topmost.Implicit)
3439 topmost = topmost.Parent;
3440 $$ = new SwitchSection ((ArrayList) $1, topmost);
3447 ArrayList labels = new ArrayList (4);
3452 | switch_labels switch_label
3454 ArrayList labels = (ArrayList) ($1);
3462 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3463 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3466 1523, lexer.Location,
3467 "The keyword case or default must precede code in switch block");
3481 oob_stack.Push (lexer.Location);
3483 boolean_expression CLOSE_PARENS embedded_statement
3485 Location l = (Location) oob_stack.Pop ();
3486 $$ = new While ((Expression) $4, (Statement) $6, l);
3488 if (RootContext.WarningLevel >= 3){
3489 if ($6 == EmptyStatement.Value)
3490 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3496 : DO embedded_statement
3498 oob_stack.Push (lexer.Location);
3500 boolean_expression CLOSE_PARENS SEMICOLON
3502 Location l = (Location) oob_stack.Pop ();
3504 $$ = new Do ((Statement) $2, (Expression) $6, l);
3510 opt_for_initializer SEMICOLON
3512 Block assign_block = new Block (current_block);
3513 current_block = assign_block;
3515 if ($3 is DictionaryEntry){
3516 DictionaryEntry de = (DictionaryEntry) $3;
3518 Expression type = (Expression) de.Key;
3519 ArrayList var_declarators = (ArrayList) de.Value;
3521 foreach (VariableDeclaration decl in var_declarators){
3525 vi = current_block.AddVariable (
3526 type, decl.identifier, current_local_parameters, decl.Location);
3530 Location l = lexer.Location;
3532 if (decl.expression_or_array_initializer is Expression){
3533 expr = (Expression) decl.expression_or_array_initializer;
3534 } else if (decl.expression_or_array_initializer == null) {
3537 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3538 expr = new ArrayCreation (type, "", init, decl.Location);
3541 LocalVariableReference var;
3542 var = new LocalVariableReference (assign_block, decl.identifier, l);
3545 Assign a = new Assign (var, expr, decl.Location);
3547 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3553 oob_stack.Push (lexer.Location);
3555 opt_for_condition SEMICOLON
3556 opt_for_iterator CLOSE_PARENS
3559 Location l = (Location) oob_stack.Pop ();
3561 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3563 if (RootContext.WarningLevel >= 3){
3564 if ($10 == EmptyStatement.Value)
3565 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3568 current_block.AddStatement (f);
3569 while (current_block.Implicit)
3570 current_block = current_block.Parent;
3572 current_block = current_block.Parent;
3577 : /* empty */ { $$ = EmptyStatement.Value; }
3582 : local_variable_declaration
3583 | statement_expression_list
3587 : /* empty */ { $$ = null; }
3588 | boolean_expression
3592 : /* empty */ { $$ = EmptyStatement.Value; }
3597 : statement_expression_list
3600 statement_expression_list
3601 : statement_expression
3603 // CHANGE: was `null'
3604 Block b = new Block (current_block, Block.Flags.Implicit);
3606 b.AddStatement ((Statement) $1);
3609 | statement_expression_list COMMA statement_expression
3611 Block b = (Block) $1;
3613 b.AddStatement ((Statement) $3);
3619 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
3621 Report.Error (230, lexer.Location, "Type and identifier are both required in a foreach statement");
3624 | FOREACH OPEN_PARENS type IDENTIFIER IN
3626 oob_stack.Push (lexer.Location);
3628 expression CLOSE_PARENS
3630 oob_stack.Push (current_block);
3632 Block foreach_block = new Block (current_block);
3633 LocalVariableReference v = null;
3634 Location l = lexer.Location;
3637 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3641 // Get a writable reference to this read-only variable.
3642 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3644 current_block = foreach_block;
3647 oob_stack.Push (current_block);
3651 Block foreach_block = (Block) oob_stack.Pop ();
3652 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3653 Block prev_block = (Block) oob_stack.Pop ();
3654 Location l = (Location) oob_stack.Pop ();
3656 current_block = prev_block;
3659 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3660 foreach_block.AddStatement (f);
3669 | continue_statement
3679 $$ = new Break (lexer.Location);
3684 : CONTINUE SEMICOLON
3686 $$ = new Continue (lexer.Location);
3691 : GOTO IDENTIFIER SEMICOLON
3693 $$ = new Goto (current_block, (string) $2, lexer.Location);
3695 | GOTO CASE constant_expression SEMICOLON
3697 $$ = new GotoCase ((Expression) $3, lexer.Location);
3699 | GOTO DEFAULT SEMICOLON
3701 $$ = new GotoDefault (lexer.Location);
3706 : RETURN opt_expression SEMICOLON
3708 $$ = new Return ((Expression) $2, lexer.Location);
3713 : THROW opt_expression SEMICOLON
3715 $$ = new Throw ((Expression) $2, lexer.Location);
3720 : IDENTIFIER RETURN expression SEMICOLON
3722 string s = (string) $1;
3724 Report.Error (1003, lexer.Location, "; expected");
3727 if (RootContext.Version == LanguageVersion.ISO_1){
3728 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
3731 if (iterator_container == null){
3732 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3735 iterator_container.SetYields ();
3736 $$ = new Yield ((Expression) $3, lexer.Location);
3739 | IDENTIFIER BREAK SEMICOLON
3741 string s = (string) $1;
3743 Report.Error (1003, lexer.Location, "; expected");
3746 if (RootContext.Version == LanguageVersion.ISO_1){
3747 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
3750 if (iterator_container == null){
3751 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3754 iterator_container.SetYields ();
3755 $$ = new YieldBreak (lexer.Location);
3766 : TRY block catch_clauses
3769 ArrayList s = new ArrayList (4);
3771 foreach (Catch cc in (ArrayList) $3) {
3778 // Now s contains the list of specific catch clauses
3779 // and g contains the general one.
3781 $$ = new Try ((Block) $2, s, g, null, lexer.Location);
3783 | TRY block opt_catch_clauses FINALLY block
3786 ArrayList s = new ArrayList (4);
3787 ArrayList catch_list = (ArrayList) $3;
3789 if (catch_list != null){
3790 foreach (Catch cc in catch_list) {
3798 $$ = new Try ((Block) $2, s, g, (Block) $5, lexer.Location);
3802 Report.Error (1524, lexer.Location, "Expected catch or finally");
3807 : /* empty */ { $$ = null; }
3814 ArrayList l = new ArrayList (4);
3819 | catch_clauses catch_clause
3821 ArrayList l = (ArrayList) $1;
3829 : /* empty */ { $$ = null; }
3834 : CATCH opt_catch_args
3836 Expression type = null;
3840 DictionaryEntry cc = (DictionaryEntry) $2;
3841 type = (Expression) cc.Key;
3842 id = (string) cc.Value;
3845 ArrayList one = new ArrayList (4);
3846 Location loc = lexer.Location;
3848 one.Add (new VariableDeclaration (id, null, loc));
3851 current_block = new Block (current_block);
3852 Block b = declare_local_variables (type, one, loc);
3857 Expression type = null;
3861 DictionaryEntry cc = (DictionaryEntry) $2;
3862 type = (Expression) cc.Key;
3863 id = (string) cc.Value;
3867 // FIXME: I can change this for an assignment.
3869 while (current_block != (Block) $1)
3870 current_block = current_block.Parent;
3875 $$ = new Catch (type, id , (Block) $4, lexer.Location);
3880 : /* empty */ { $$ = null; }
3885 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3887 $$ = new DictionaryEntry ($2, $3);
3894 $$ = new Checked ((Block) $2);
3901 $$ = new Unchecked ((Block) $2);
3908 if (!RootContext.Unsafe){
3909 Report.Error (227, lexer.Location,
3910 "Unsafe code can only be used if --unsafe is used");
3913 $$ = new Unsafe ((Block) $3);
3919 type fixed_pointer_declarators
3922 ArrayList list = (ArrayList) $4;
3923 Expression type = (Expression) $3;
3924 Location l = lexer.Location;
3925 int top = list.Count;
3927 Block assign_block = new Block (current_block);
3928 current_block = assign_block;
3930 for (int i = 0; i < top; i++){
3931 Pair p = (Pair) list [i];
3934 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3948 Location l = (Location) oob_stack.Pop ();
3950 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
3952 if (RootContext.WarningLevel >= 3){
3953 if ($7 == EmptyStatement.Value)
3954 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3957 current_block.AddStatement (f);
3958 while (current_block.Implicit)
3959 current_block = current_block.Parent;
3961 current_block = current_block.Parent;
3965 fixed_pointer_declarators
3966 : fixed_pointer_declarator {
3967 ArrayList declarators = new ArrayList (4);
3969 declarators.Add ($1);
3972 | fixed_pointer_declarators COMMA fixed_pointer_declarator
3974 ArrayList declarators = (ArrayList) $1;
3976 declarators.Add ($3);
3981 fixed_pointer_declarator
3982 : IDENTIFIER ASSIGN expression
3984 $$ = new Pair ($1, $3);
3988 Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
3994 : LOCK OPEN_PARENS expression CLOSE_PARENS
4000 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4005 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4007 Block assign_block = new Block (current_block);
4008 current_block = assign_block;
4010 oob_stack.Push (lexer.Location);
4012 if ($3 is DictionaryEntry){
4013 DictionaryEntry de = (DictionaryEntry) $3;
4014 Location l = lexer.Location;
4016 Expression type = (Expression) de.Key;
4017 ArrayList var_declarators = (ArrayList) de.Value;
4019 ArrayList vars = new ArrayList (4);
4021 foreach (VariableDeclaration decl in var_declarators){
4023 LocalInfo vi = current_block.AddVariable (
4024 type, decl.identifier,
4025 current_local_parameters, decl.Location);
4031 if (decl.expression_or_array_initializer is Expression){
4032 expr = (Expression) decl.expression_or_array_initializer;
4034 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4036 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4039 expr = new ArrayCreation (type, "", init, decl.Location);
4042 LocalVariableReference var;
4044 // Get a writable reference to this read-only variable.
4045 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4047 // This is so that it is not a warning on using variables
4050 vars.Add (new DictionaryEntry (var, expr));
4052 // Assign a = new Assign (var, expr, decl.Location);
4053 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4055 $3 = new DictionaryEntry (type, vars);
4060 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
4061 current_block.AddStatement (u);
4062 while (current_block.Implicit)
4063 current_block = current_block.Parent;
4065 current_block = current_block.Parent;
4069 resource_acquisition
4070 : local_variable_declaration
4077 // A class used to pass around variable declarations and constants
4079 public class VariableDeclaration {
4080 public string identifier;
4081 public object expression_or_array_initializer;
4082 public Location Location;
4083 public Attributes OptAttributes;
4085 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4087 this.identifier = id;
4088 this.expression_or_array_initializer = eoai;
4090 this.OptAttributes = opt_attrs;
4093 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4099 /// Used to pass around interface property information
4101 public class InterfaceAccessorInfo {
4103 public readonly Accessor Get, Set;
4105 public InterfaceAccessorInfo (bool has_get, bool has_set,
4106 Attributes get_attrs, Attributes set_attrs, Location get_loc, Location set_loc)
4109 Get = new Accessor (null, get_attrs, get_loc);
4111 Set = new Accessor (null, set_attrs, set_loc);
4117 // A class used to hold info about an indexer declarator
4119 public class IndexerDeclaration {
4120 public Expression type;
4121 public MemberName interface_type;
4122 public Parameters param_list;
4124 public IndexerDeclaration (Expression type, MemberName interface_type,
4125 Parameters param_list)
4128 this.interface_type = interface_type;
4129 this.param_list = param_list;
4134 // We use this when we do not have an object in advance that is an IIteratorContainer
4136 public class SimpleIteratorContainer : IIteratorContainer {
4139 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4144 public static SimpleIteratorContainer GetSimple () {
4145 Simple.Yields = false;
4149 public void SetYields () { Yields = true; }
4153 // A class used to hold info about an operator declarator
4155 public class OperatorDeclaration {
4156 public Operator.OpType optype;
4157 public Expression ret_type, arg1type, arg2type;
4158 public string arg1name, arg2name;
4159 public Location location;
4161 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4162 Expression arg1type, string arg1name,
4163 Expression arg2type, string arg2name, Location location)
4166 this.ret_type = ret_type;
4167 this.arg1type = arg1type;
4168 this.arg1name = arg1name;
4169 this.arg2type = arg2type;
4170 this.arg2name = arg2name;
4171 this.location = location;
4176 void Error_ExpectingTypeName (Location l, Expression expr)
4178 if (expr is Invocation){
4179 Report.Error (1002, l, "; expected");
4181 Report.Error (-1, l, "Invalid Type definition");
4186 // Given the @class_name name, it creates a fully qualified name
4187 // based on the containing declaration space
4190 MakeName (MemberName class_name)
4192 string ns = current_namespace.FullName;
4194 if (current_container.Name == ""){
4196 return new MemberName (new MemberName (ns), class_name);
4200 return new MemberName (current_container.MemberName, class_name);
4204 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4206 Block implicit_block;
4207 ArrayList inits = null;
4210 // We use the `Used' property to check whether statements
4211 // have been added to the current block. If so, we need
4212 // to create another block to contain the new declaration
4213 // otherwise, as an optimization, we use the same block to
4214 // add the declaration.
4216 // FIXME: A further optimization is to check if the statements
4217 // that were added were added as part of the initialization
4218 // below. In which case, no other statements have been executed
4219 // and we might be able to reduce the number of blocks for
4220 // situations like this:
4222 // int j = 1; int k = j + 1;
4224 if (current_block.Used)
4225 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4227 implicit_block = current_block;
4229 foreach (VariableDeclaration decl in variable_declarators){
4231 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4232 if (decl.expression_or_array_initializer != null){
4234 inits = new ArrayList (4);
4241 return implicit_block;
4243 foreach (VariableDeclaration decl in inits){
4247 if (decl.expression_or_array_initializer is Expression){
4248 expr = (Expression) decl.expression_or_array_initializer;
4251 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4253 expr = new ArrayCreation (type, "", init, decl.Location);
4256 LocalVariableReference var;
4257 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4259 assign = new Assign (var, expr, decl.Location);
4261 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4264 return implicit_block;
4267 Block declare_local_constants (Expression type, ArrayList declarators)
4269 Block implicit_block;
4271 if (current_block.Used)
4272 implicit_block = new Block (current_block, Block.Flags.Implicit);
4274 implicit_block = current_block;
4276 foreach (VariableDeclaration decl in declarators){
4277 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4278 current_local_parameters, decl.Location);
4281 return implicit_block;
4284 void CheckAttributeTarget (string a)
4288 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4292 Location l = lexer.Location;
4293 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4299 void CheckUnaryOperator (Operator.OpType op)
4303 case Operator.OpType.LogicalNot:
4304 case Operator.OpType.OnesComplement:
4305 case Operator.OpType.Increment:
4306 case Operator.OpType.Decrement:
4307 case Operator.OpType.True:
4308 case Operator.OpType.False:
4309 case Operator.OpType.Addition:
4310 case Operator.OpType.Subtraction:
4315 Location l = lexer.Location;
4316 Report.Error (1019, l, "Overloadable unary operator expected");
4322 void CheckBinaryOperator (Operator.OpType op)
4326 case Operator.OpType.Addition:
4327 case Operator.OpType.Subtraction:
4328 case Operator.OpType.Multiply:
4329 case Operator.OpType.Division:
4330 case Operator.OpType.Modulus:
4331 case Operator.OpType.BitwiseAnd:
4332 case Operator.OpType.BitwiseOr:
4333 case Operator.OpType.ExclusiveOr:
4334 case Operator.OpType.LeftShift:
4335 case Operator.OpType.RightShift:
4336 case Operator.OpType.Equality:
4337 case Operator.OpType.Inequality:
4338 case Operator.OpType.GreaterThan:
4339 case Operator.OpType.LessThan:
4340 case Operator.OpType.GreaterThanOrEqual:
4341 case Operator.OpType.LessThanOrEqual:
4345 Location l = lexer.Location;
4346 Report.Error (1020, l, "Overloadable binary operator expected");
4352 void syntax_error (Location l, string msg)
4354 Report.Error (1003, l, "Syntax error, " + msg);
4357 void output (string s)
4359 Console.WriteLine (s);
4362 void note (string s)
4364 // Used to put annotations
4369 public Tokenizer Lexer {
4375 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4377 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4378 this.name = file.Name;
4380 current_container = RootContext.Tree.Types;
4381 current_container.NamespaceEntry = current_namespace;
4382 oob_stack = new Stack ();
4383 switch_stack = new Stack ();
4385 lexer = new Tokenizer (reader, file, defines);
4388 public void parse ()
4391 if (yacc_verbose_flag)
4392 yyparse (lexer, new yydebug.yyDebugSimple ());
4395 Tokenizer tokenizer = lexer as Tokenizer;
4396 tokenizer.cleanup ();
4397 } catch (Exception e){
4398 // Please do not remove this, it is used during debugging
4401 Console.WriteLine (e);
4402 Report.Error (-25, lexer.Location, "Parsing error");
4403 if (Driver.parser_verbose)
4404 Console.WriteLine (e);
4408 void CheckToken (int error, int yyToken, string msg)
4410 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4411 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4414 Report.Error (error, lexer.Location, msg);
4417 void CheckIdentifierToken (int yyToken)
4419 CheckToken (1041, yyToken, "Identifier expected");