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 (), 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 ();
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 MemberName mname = (MemberName) $1;
588 string name = mname.GetName ();
589 if (current_attr_target == "assembly" || current_attr_target == "module")
590 $$ = new GlobalAttribute (current_container, current_attr_target,
591 name, (ArrayList) $3, (Location) $2);
593 $$ = new Attribute (current_attr_target, name, (ArrayList) $3,
599 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
602 opt_attribute_arguments
603 : /* empty */ { $$ = null; }
604 | OPEN_PARENS attribute_arguments CLOSE_PARENS
612 : opt_positional_argument_list
617 ArrayList args = new ArrayList (4);
623 | positional_argument_list COMMA named_argument_list
625 ArrayList args = new ArrayList (4);
631 | named_argument_list
633 ArrayList args = new ArrayList (4);
642 opt_positional_argument_list
643 : /* empty */ { $$ = null; }
644 | positional_argument_list
647 positional_argument_list
650 ArrayList args = new ArrayList (4);
651 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
655 | positional_argument_list COMMA expression
657 ArrayList args = (ArrayList) $1;
658 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
667 ArrayList args = new ArrayList (4);
672 | named_argument_list COMMA named_argument
674 ArrayList args = (ArrayList) $1;
682 : IDENTIFIER ASSIGN expression
684 $$ = new DictionaryEntry (
686 new Argument ((Expression) $3, Argument.AType.Expression));
692 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
695 opt_class_member_declarations
697 | class_member_declarations
700 class_member_declarations
701 : class_member_declaration
702 | class_member_declarations
703 class_member_declaration
706 class_member_declaration
707 : constant_declaration // done
708 | field_declaration // done
709 | method_declaration // done
710 | property_declaration // done
711 | event_declaration // done
712 | indexer_declaration // done
713 | operator_declaration // done
714 | constructor_declaration // done
715 | destructor_declaration // done
725 MemberName name = MakeName ((MemberName) $5);
726 bool partial = (bool) $3;
729 ClassPart part = PartialContainer.CreatePart (
730 current_namespace, current_container, name, (int) $2,
731 (Attributes) $1, Kind.Struct, lexer.Location);
733 current_container = part.PartialContainer;
734 current_class = part;
736 current_class = new Struct (
737 current_namespace, current_container, name, (int) $2,
738 (Attributes) $1, lexer.Location);
740 current_container = current_class;
741 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
747 current_class.Bases = (ArrayList) $7;
749 current_class.Register ();
756 current_container = current_container.Parent;
757 current_class = current_container;
759 | opt_attributes opt_modifiers opt_partial STRUCT error {
760 CheckIdentifierToken (yyToken);
765 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
768 opt_struct_member_declarations
770 | struct_member_declarations
773 struct_member_declarations
774 : struct_member_declaration
775 | struct_member_declarations struct_member_declaration
778 struct_member_declaration
779 : constant_declaration
782 | property_declaration
784 | indexer_declaration
785 | operator_declaration
786 | constructor_declaration
790 * This is only included so we can flag error 575:
791 * destructors only allowed on class types
793 | destructor_declaration
804 foreach (VariableDeclaration constant in (ArrayList) $5){
805 Location l = constant.Location;
807 Const c = new Const (
808 current_class, (Expression) $4, (string) constant.identifier,
809 (Expression) constant.expression_or_array_initializer, (int) $2,
812 current_container.AddConstant (c);
818 : constant_declarator
820 ArrayList constants = new ArrayList (4);
825 | constant_declarators COMMA constant_declarator
828 ArrayList constants = (ArrayList) $1;
835 : IDENTIFIER ASSIGN constant_expression
837 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
841 // A const field requires a value to be provided
842 Report.Error (145, lexer.Location, "A const field requires a value to be provided");
854 Expression type = (Expression) $3;
857 foreach (VariableDeclaration var in (ArrayList) $4){
858 Location l = var.Location;
860 Field field = new Field (current_class, type, mod, var.identifier,
861 var.expression_or_array_initializer,
864 current_container.AddField (field);
872 Report.Error (670, lexer.Location, "void type is not allowed for fields");
877 : variable_declarator
879 ArrayList decl = new ArrayList (4);
883 | variable_declarators COMMA variable_declarator
885 ArrayList decls = (ArrayList) $1;
892 : IDENTIFIER ASSIGN variable_initializer
894 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
898 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
911 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
913 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
919 iterator_container = (IIteratorContainer) $1;
923 Method method = (Method) $1;
924 Block b = (Block) $3;
925 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
928 if ((method.ModFlags & extern_abstract) == 0){
930 501, lexer.Location, current_container.MakeName (method.Name) +
931 "must declare a body because it is not marked abstract or extern");
934 if ((method.ModFlags & Modifiers.EXTERN) != 0){
936 179, lexer.Location, current_container.MakeName (method.Name) +
937 " is declared extern, but has a body");
941 method.Block = (Block) $3;
942 current_container.AddMethod (method);
944 current_local_parameters = null;
945 iterator_container = null;
959 1585, lexer.Location, "Member modifier `" +
960 Modifiers.Name (i) + "' must precede member type and name");
971 type namespace_or_type_name
972 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
974 MemberName name = (MemberName) $4;
976 Method method = new Method (current_class, (Expression) $3, (int) $2,
977 false, name, (Parameters) $6, (Attributes) $1,
980 current_local_parameters = (Parameters) $6;
986 VOID namespace_or_type_name
987 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
989 MemberName name = (MemberName) $4;
991 Method method = new Method (current_class, TypeManager.system_void_expr,
992 (int) $2, false, name, (Parameters) $6,
993 (Attributes) $1, lexer.Location);
995 current_local_parameters = (Parameters) $6;
1001 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1003 Report.Error (1585, lexer.Location,
1004 String.Format ("Modifier {0} should appear before type",
1005 Modifiers.Name ((int) $4)));
1006 MemberName name = (MemberName) $4;
1008 Method method = new Method (current_class, TypeManager.system_void_expr,
1009 0, false, name, (Parameters) $7, (Attributes) $1,
1012 current_local_parameters = (Parameters) $7;
1019 | SEMICOLON { $$ = null; }
1022 opt_formal_parameter_list
1023 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1024 | formal_parameter_list
1027 formal_parameter_list
1030 ArrayList pars_list = (ArrayList) $1;
1032 Parameter [] pars = new Parameter [pars_list.Count];
1033 pars_list.CopyTo (pars);
1035 $$ = new Parameters (pars, null, lexer.Location);
1037 | fixed_parameters COMMA parameter_array
1039 ArrayList pars_list = (ArrayList) $1;
1041 Parameter [] pars = new Parameter [pars_list.Count];
1042 pars_list.CopyTo (pars);
1044 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
1046 | fixed_parameters COMMA ARGLIST
1048 ArrayList pars_list = (ArrayList) $1;
1050 Parameter [] pars = new Parameter [pars_list.Count];
1051 pars_list.CopyTo (pars);
1053 $$ = new Parameters (pars, true, lexer.Location);
1055 | parameter_array COMMA fixed_parameters
1057 Report.Error (231, lexer.Location, "A params parameter must be the last parameter in a formal parameter list");
1060 | ARGLIST COMMA fixed_parameters
1062 Report.Error (257, lexer.Location, "An __arglist parameter must be the last parameter in a formal parameter list");
1067 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
1071 $$ = new Parameters (null, true, lexer.Location);
1078 ArrayList pars = new ArrayList (4);
1083 | fixed_parameters COMMA fixed_parameter
1085 ArrayList pars = (ArrayList) $1;
1094 opt_parameter_modifier
1098 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
1101 opt_parameter_modifier
1104 CheckIdentifierToken (yyToken);
1108 opt_parameter_modifier
1114 Report.Error (241, lexer.Location, "Default parameter specifiers are not permitted");
1119 opt_parameter_modifier
1120 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1121 | parameter_modifier
1125 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1126 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1130 : opt_attributes PARAMS type IDENTIFIER
1132 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1133 note ("type must be a single-dimension array type");
1135 | opt_attributes PARAMS type error {
1136 CheckIdentifierToken (yyToken);
1141 property_declaration
1144 type namespace_or_type_name
1147 implicit_value_parameter_type = (Expression) $3;
1149 lexer.PropertyParsing = true;
1151 $$ = lexer.Location;
1153 iterator_container = SimpleIteratorContainer.GetSimple ();
1155 accessor_declarations
1157 lexer.PropertyParsing = false;
1162 Pair pair = (Pair) $7;
1163 Accessor get_block = (Accessor) pair.First;
1164 Accessor set_block = (Accessor) pair.Second;
1166 Location loc = (Location) $6;
1167 MemberName name = (MemberName) $4;
1169 prop = new Property (current_class, (Expression) $3, (int) $2, false,
1170 name, (Attributes) $1, get_block, set_block, loc);
1171 if (SimpleIteratorContainer.Simple.Yields)
1174 current_container.AddProperty (prop);
1175 implicit_value_parameter_type = null;
1176 iterator_container = null;
1180 accessor_declarations
1181 : get_accessor_declaration opt_set_accessor_declaration
1183 $$ = new Pair ($1, $2);
1185 | set_accessor_declaration opt_get_accessor_declaration
1187 $$ = new Pair ($2, $1);
1191 opt_get_accessor_declaration
1192 : /* empty */ { $$ = null; }
1193 | get_accessor_declaration
1196 opt_set_accessor_declaration
1197 : /* empty */ { $$ = null; }
1198 | set_accessor_declaration
1201 get_accessor_declaration
1202 : opt_attributes GET
1204 // If this is not the case, then current_local_parameters has already
1205 // been set in indexer_declaration
1206 if (parsing_indexer == false)
1207 current_local_parameters = null;
1209 current_local_parameters = indexer_parameters;
1210 lexer.PropertyParsing = false;
1214 $$ = new Accessor ((Block) $4, (Attributes) $1, lexer.Location);
1215 current_local_parameters = null;
1216 lexer.PropertyParsing = true;
1220 set_accessor_declaration
1221 : opt_attributes SET
1224 Parameter implicit_value_parameter = new Parameter (
1225 implicit_value_parameter_type, "value",
1226 Parameter.Modifier.NONE, null);
1228 if (parsing_indexer == false) {
1229 args = new Parameter [1];
1230 args [0] = implicit_value_parameter;
1231 current_local_parameters = new Parameters (args, null, lexer.Location);
1233 Parameter [] fpars = indexer_parameters.FixedParameters;
1236 int count = fpars.Length;
1238 args = new Parameter [count + 1];
1239 fpars.CopyTo (args, 0);
1240 args [count] = implicit_value_parameter;
1243 current_local_parameters = new Parameters (
1244 args, indexer_parameters.ArrayParameter, lexer.Location);
1247 lexer.PropertyParsing = false;
1251 $$ = new Accessor ((Block) $4, (Attributes) $1, lexer.Location);
1252 current_local_parameters = null;
1253 lexer.PropertyParsing = true;
1259 | SEMICOLON { $$ = null; }
1262 interface_declaration
1266 INTERFACE member_name
1268 MemberName name = MakeName ((MemberName) $5);
1269 bool partial = (bool) $3;
1272 ClassPart part = PartialContainer.CreatePart (
1273 current_namespace, current_container, name, (int) $2,
1274 (Attributes) $1, Kind.Interface, lexer.Location);
1276 current_container = part.PartialContainer;
1277 current_class = part;
1279 current_class = new Interface (
1280 current_namespace, current_container, name, (int) $2,
1281 (Attributes) $1, lexer.Location);
1283 current_container = current_class;
1284 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
1289 current_class.Bases = (ArrayList) $7;
1291 current_class.Register ();
1298 current_container = current_container.Parent;
1299 current_class = current_container;
1301 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1302 CheckIdentifierToken (yyToken);
1308 opt_interface_member_declarations
1312 opt_interface_member_declarations
1314 | interface_member_declarations
1317 interface_member_declarations
1318 : interface_member_declaration
1319 | interface_member_declarations interface_member_declaration
1322 interface_member_declaration
1323 : interface_method_declaration
1325 Method m = (Method) $1;
1327 current_container.AddMethod (m);
1329 | interface_property_declaration
1331 Property p = (Property) $1;
1333 current_container.AddProperty (p);
1335 | interface_event_declaration
1338 Event e = (Event) $1;
1339 current_container.AddEvent (e);
1342 | interface_indexer_declaration
1344 Indexer i = (Indexer) $1;
1346 current_container.AddIndexer (i);
1354 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, lexer.Location);
1359 interface_method_declaration
1360 : opt_attributes opt_new type namespace_or_type_name
1361 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1364 MemberName name = (MemberName) $4;
1366 $$ = new Method (current_class, (Expression) $3, (int) $2, true,
1367 name, (Parameters) $6, (Attributes) $1, lexer.Location);
1369 | opt_attributes opt_new VOID namespace_or_type_name
1370 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1373 MemberName name = (MemberName) $4;
1375 $$ = new Method (current_class, TypeManager.system_void_expr, (int) $2,
1376 true, name, (Parameters) $6, (Attributes) $1, lexer.Location);
1380 interface_property_declaration
1385 { lexer.PropertyParsing = true; }
1387 { lexer.PropertyParsing = false; }
1390 InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
1392 $$ = new Property (current_class, (Expression) $3, (int) $2, true,
1393 new MemberName ((string) $4), (Attributes) $1,
1394 pinfo.Get, pinfo.Set, lexer.Location);
1399 CheckIdentifierToken (yyToken);
1405 : opt_attributes GET SEMICOLON { $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, lexer.Location, lexer.Location); }
1406 | opt_attributes SET SEMICOLON { $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1, lexer.Location, lexer.Location); }
1407 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1408 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $3, lexer.Location, lexer.Location); }
1409 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1410 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $3, (Attributes) $1, lexer.Location, lexer.Location); }
1413 interface_event_declaration
1414 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1416 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1417 new MemberName ((string) $5), null,
1418 (Attributes) $1, lexer.Location);
1420 | opt_attributes opt_new EVENT type error {
1421 CheckIdentifierToken (yyToken);
1424 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1425 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1428 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE event_accessor_declarations CLOSE_BRACE {
1429 Report.Error (69, lexer.Location, "Event accessors not valid on interfaces");
1434 interface_indexer_declaration
1435 : opt_attributes opt_new type THIS
1436 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1438 { lexer.PropertyParsing = true; }
1440 { lexer.PropertyParsing = false; }
1443 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1445 $$ = new Indexer (current_class, (Expression) $3,
1446 new MemberName (TypeContainer.DefaultIndexerName),
1447 (int) $2, true, (Parameters) $6, (Attributes) $1,
1448 info.Get, info.Set, lexer.Location);
1452 operator_declaration
1453 : opt_attributes opt_modifiers operator_declarator
1455 iterator_container = SimpleIteratorContainer.GetSimple ();
1459 OperatorDeclaration decl = (OperatorDeclaration) $3;
1461 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1463 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null);
1464 if (decl.arg2type != null)
1465 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null);
1467 Operator op = new Operator (
1468 current_class, decl.optype, decl.ret_type, (int) $2,
1469 new Parameters (param_list, null, decl.location),
1470 (Block) $5, (Attributes) $1, decl.location);
1472 if (SimpleIteratorContainer.Simple.Yields)
1475 // Note again, checking is done in semantic analysis
1476 current_container.AddOperator (op);
1478 current_local_parameters = null;
1479 iterator_container = null;
1485 | SEMICOLON { $$ = null; }
1488 : type OPERATOR overloadable_operator
1489 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1491 Operator.OpType op = (Operator.OpType) $3;
1492 CheckUnaryOperator (op);
1494 if (op == Operator.OpType.Addition)
1495 op = Operator.OpType.UnaryPlus;
1497 if (op == Operator.OpType.Subtraction)
1498 op = Operator.OpType.UnaryNegation;
1500 Parameter [] pars = new Parameter [1];
1502 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1504 current_local_parameters = new Parameters (pars, null, lexer.Location);
1506 $$ = new OperatorDeclaration (op, (Expression) $1, (Expression) $5, (string) $6,
1507 null, null, lexer.Location);
1509 | type OPERATOR overloadable_operator
1511 type IDENTIFIER COMMA
1515 CheckBinaryOperator ((Operator.OpType) $3);
1517 Parameter [] pars = new Parameter [2];
1519 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1520 pars [1] = new Parameter ((Expression) $8, (string) $9, Parameter.Modifier.NONE, null);
1522 current_local_parameters = new Parameters (pars, null, lexer.Location);
1524 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1525 (Expression) $5, (string) $6,
1526 (Expression) $8, (string) $9, lexer.Location);
1528 | conversion_operator_declarator
1531 overloadable_operator
1533 : BANG { $$ = Operator.OpType.LogicalNot; }
1534 | TILDE { $$ = Operator.OpType.OnesComplement; }
1535 | OP_INC { $$ = Operator.OpType.Increment; }
1536 | OP_DEC { $$ = Operator.OpType.Decrement; }
1537 | TRUE { $$ = Operator.OpType.True; }
1538 | FALSE { $$ = Operator.OpType.False; }
1539 // Unary and binary:
1540 | PLUS { $$ = Operator.OpType.Addition; }
1541 | MINUS { $$ = Operator.OpType.Subtraction; }
1543 | STAR { $$ = Operator.OpType.Multiply; }
1544 | DIV { $$ = Operator.OpType.Division; }
1545 | PERCENT { $$ = Operator.OpType.Modulus; }
1546 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1547 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1548 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1549 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1550 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1551 | OP_EQ { $$ = Operator.OpType.Equality; }
1552 | OP_NE { $$ = Operator.OpType.Inequality; }
1553 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1554 | OP_LT { $$ = Operator.OpType.LessThan; }
1555 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1556 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1559 conversion_operator_declarator
1560 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1562 Parameter [] pars = new Parameter [1];
1564 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1566 current_local_parameters = new Parameters (pars, null, lexer.Location);
1568 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1569 null, null, lexer.Location);
1571 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1573 Parameter [] pars = new Parameter [1];
1575 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1577 current_local_parameters = new Parameters (pars, null, lexer.Location);
1579 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1580 null, null, lexer.Location);
1584 syntax_error (lexer.Location, "'operator' expected");
1588 syntax_error (lexer.Location, "'operator' expected");
1592 constructor_declaration
1595 constructor_declarator
1598 Constructor c = (Constructor) $3;
1599 c.Block = (Block) $4;
1600 c.OptAttributes = (Attributes) $1;
1601 c.ModFlags = (int) $2;
1603 if (c.Name == current_container.Basename){
1604 if ((c.ModFlags & Modifiers.STATIC) != 0){
1605 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1607 515, c.Location, String.Format (
1608 "`{0}.{1}': static constructor can not have access modifiers",
1609 c.Name, current_container.Name));
1612 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1614 if (c.Initializer != null){
1617 "Static constructors can not have an explicit this or base " +
1618 "constructor invocations");
1621 if (!c.Parameters.Empty){
1623 132, c.Location, "Static constructors should not have parameters");
1626 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1629 // We let another layer check the validity of the constructor.
1630 Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1633 current_container.AddConstructor (c);
1635 current_local_parameters = null;
1639 constructor_declarator
1641 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1643 oob_stack.Push (lexer.Location);
1645 current_local_parameters = (Parameters) $3;
1647 opt_constructor_initializer
1649 Location l = (Location) oob_stack.Pop ();
1650 $$ = new Constructor (current_class, (string) $1, 0, (Parameters) $3,
1651 (ConstructorInitializer) $6, l);
1657 | SEMICOLON { $$ = null; }
1660 opt_constructor_initializer
1661 : /* empty */ { $$ = null; }
1662 | constructor_initializer
1665 constructor_initializer
1666 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1668 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1670 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1672 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1675 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1681 : /* EMPTY */ { $$ = 0; }
1682 | UNSAFE { $$ = Modifiers.UNSAFE; }
1683 | EXTERN { $$ = Modifiers.EXTERN; }
1686 destructor_declaration
1687 : opt_attributes opt_finalizer TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1689 if ((string) $4 != current_container.Basename){
1690 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1691 } else if (!(current_container is Class)){
1692 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1694 Location l = lexer.Location;
1697 if (!RootContext.StdLib && current_container.Name == "System.Object")
1698 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
1700 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
1702 if ((m & Modifiers.UNSAFE) != 0){
1703 if (!RootContext.Unsafe){
1704 Report.Error (227, l,
1705 "Unsafe code requires the -unsafe command " +
1706 "line option to be specified");
1710 Method d = new Destructor (
1711 current_class, TypeManager.system_void_expr, m, "Finalize",
1712 new Parameters (null, null, l), (Attributes) $1, l);
1714 d.Block = (Block) $7;
1715 current_container.AddMethod (d);
1723 EVENT type variable_declarators SEMICOLON
1725 foreach (VariableDeclaration var in (ArrayList) $5) {
1727 MemberName name = new MemberName (var.identifier);
1729 Event e = new EventField (
1730 current_class, (Expression) $4, (int) $2, false, name,
1731 var.expression_or_array_initializer, (Attributes) $1,
1734 current_container.AddEvent (e);
1740 EVENT type namespace_or_type_name
1743 implicit_value_parameter_type = (Expression) $4;
1744 lexer.EventParsing = true;
1745 oob_stack.Push (lexer.Location);
1747 event_accessor_declarations
1749 lexer.EventParsing = false;
1753 Location loc = (Location) oob_stack.Pop ();
1756 Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
1759 Pair pair = (Pair) $8;
1761 MemberName name = (MemberName) $5;
1763 Event e = new EventProperty (
1764 current_class, (Expression) $4, (int) $2, false, name, null,
1765 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
1768 current_container.AddEvent (e);
1769 implicit_value_parameter_type = null;
1772 | opt_attributes opt_modifiers EVENT type namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS block {
1773 MemberName mn = (MemberName) $5;
1775 if (mn.Left != null)
1776 Report.Error (71, lexer.Location, "Explicit implementation of events requires property syntax");
1778 Report.Error (71, lexer.Location, "Event declaration should use property syntax");
1782 event_accessor_declarations
1783 : add_accessor_declaration remove_accessor_declaration
1785 $$ = new Pair ($1, $2);
1787 | remove_accessor_declaration add_accessor_declaration
1789 $$ = new Pair ($2, $1);
1791 | add_accessor_declaration { $$ = null; }
1792 | remove_accessor_declaration { $$ = null; }
1795 add_accessor_declaration
1796 : opt_attributes ADD
1798 Parameter [] args = new Parameter [1];
1799 Parameter implicit_value_parameter = new Parameter (
1800 implicit_value_parameter_type, "value",
1801 Parameter.Modifier.NONE, null);
1803 args [0] = implicit_value_parameter;
1805 current_local_parameters = new Parameters (args, null, lexer.Location);
1806 lexer.EventParsing = false;
1810 $$ = new Accessor ((Block) $4, (Attributes) $1, lexer.Location);
1811 lexer.EventParsing = true;
1813 | opt_attributes ADD error {
1814 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1819 remove_accessor_declaration
1820 : opt_attributes REMOVE
1822 Parameter [] args = new Parameter [1];
1823 Parameter implicit_value_parameter = new Parameter (
1824 implicit_value_parameter_type, "value",
1825 Parameter.Modifier.NONE, null);
1827 args [0] = implicit_value_parameter;
1829 current_local_parameters = new Parameters (args, null, lexer.Location);
1830 lexer.EventParsing = false;
1834 $$ = new Accessor ((Block) $4, (Attributes) $1, lexer.Location);
1835 lexer.EventParsing = true;
1837 | opt_attributes REMOVE error {
1838 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1844 : opt_attributes opt_modifiers indexer_declarator
1847 IndexerDeclaration decl = (IndexerDeclaration) $3;
1849 implicit_value_parameter_type = decl.type;
1851 lexer.PropertyParsing = true;
1852 parsing_indexer = true;
1854 indexer_parameters = decl.param_list;
1855 oob_stack.Push (lexer.Location);
1857 accessor_declarations
1859 lexer.PropertyParsing = false;
1860 parsing_indexer = false;
1864 // The signature is computed from the signature of the indexer. Look
1865 // at section 3.6 on the spec
1866 Location loc = (Location) oob_stack.Pop ();
1868 IndexerDeclaration decl = (IndexerDeclaration) $3;
1869 Pair pair = (Pair) $6;
1870 Accessor get_block = (Accessor) pair.First;
1871 Accessor set_block = (Accessor) pair.Second;
1874 if (decl.interface_type != null)
1875 name = new MemberName (decl.interface_type,
1876 TypeContainer.DefaultIndexerName);
1878 name = new MemberName (TypeContainer.DefaultIndexerName);
1880 indexer = new Indexer (current_class, decl.type, name,
1881 (int) $2, false, decl.param_list, (Attributes) $1,
1882 get_block, set_block, loc);
1884 current_container.AddIndexer (indexer);
1886 current_local_parameters = null;
1887 implicit_value_parameter_type = null;
1888 indexer_parameters = null;
1893 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1895 Parameters pars = (Parameters) $4;
1896 if (pars.HasArglist) {
1897 // "__arglist is not valid in this context"
1898 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
1899 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
1900 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1903 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
1905 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1907 Parameters pars = (Parameters) $6;
1909 if (pars.HasArglist) {
1910 // "__arglist is not valid in this context"
1911 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
1912 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
1913 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1915 MemberName name = (MemberName) $2;
1916 $$ = new IndexerDeclaration ((Expression) $1, name, pars);
1928 Location enum_location = lexer.Location;
1930 MemberName full_name = MakeName (new MemberName ((string) $4));
1931 Enum e = new Enum (current_namespace, current_container, (Expression) $5, (int) $2,
1932 full_name, (Attributes) $1, enum_location);
1934 foreach (VariableDeclaration ev in (ArrayList) $6) {
1935 e.AddEnumMember (ev.identifier,
1936 (Expression) ev.expression_or_array_initializer,
1937 ev.Location, ev.OptAttributes);
1940 string name = full_name.GetName ();
1941 current_container.AddEnum (e);
1942 RootContext.Tree.RecordDecl (name, e);
1948 : /* empty */ { $$ = TypeManager.system_int32_expr; }
1949 | COLON type { $$ = $2; }
1953 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1959 opt_enum_member_declarations
1960 : /* empty */ { $$ = new ArrayList (4); }
1961 | enum_member_declarations opt_comma { $$ = $1; }
1964 enum_member_declarations
1965 : enum_member_declaration
1967 ArrayList l = new ArrayList (4);
1972 | enum_member_declarations COMMA enum_member_declaration
1974 ArrayList l = (ArrayList) $1;
1982 enum_member_declaration
1983 : opt_attributes IDENTIFIER
1985 $$ = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
1987 | opt_attributes IDENTIFIER
1989 $$ = lexer.Location;
1993 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
1997 delegate_declaration
2000 DELEGATE type member_name
2001 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2004 Location l = lexer.Location;
2005 MemberName name = MakeName ((MemberName) $5);
2006 Delegate del = new Delegate (current_namespace, current_container, (Expression) $4,
2007 (int) $2, name, (Parameters) $7, (Attributes) $1, l);
2009 current_container.AddDelegate (del);
2010 RootContext.Tree.RecordDecl (name.GetName (true), del);
2014 DELEGATE VOID member_name
2015 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2018 Location l = lexer.Location;
2019 MemberName name = MakeName ((MemberName) $5);
2020 Delegate del = new Delegate (
2021 current_namespace, current_container,
2022 TypeManager.system_void_expr, (int) $2, name,
2023 (Parameters) $7, (Attributes) $1, l);
2025 current_container.AddDelegate (del);
2026 RootContext.Tree.RecordDecl (name.GetName (true), del);
2030 namespace_or_type_name
2032 | namespace_or_type_name DOT IDENTIFIER {
2033 $$ = new MemberName ((MemberName) $1, (string) $3);
2039 $$ = new MemberName ((string) $1);
2044 * Before you think of adding a return_type, notice that we have been
2045 * using two rules in the places where it matters (one rule using type
2046 * and another identical one that uses VOID as the return type). This
2047 * gets rid of a shift/reduce couple
2050 : namespace_or_type_name
2052 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2064 // Note that here only unmanaged types are allowed but we
2065 // can't perform checks during this phase - we do it during
2066 // semantic analysis.
2068 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2072 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2078 | non_expression_type rank_specifier
2080 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2082 | non_expression_type STAR
2084 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2086 | expression rank_specifiers
2088 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2092 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2096 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2097 // through this different path
2099 | multiplicative_expression STAR
2101 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2108 ArrayList types = new ArrayList (4);
2113 | type_list COMMA type
2115 ArrayList types = (ArrayList) $1;
2123 * replaces all the productions for isolating the various
2124 * simple types, but we need this to reuse it easily in local_variable_type
2127 : OBJECT { $$ = TypeManager.system_object_expr; }
2128 | STRING { $$ = TypeManager.system_string_expr; }
2129 | BOOL { $$ = TypeManager.system_boolean_expr; }
2130 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2131 | FLOAT { $$ = TypeManager.system_single_expr; }
2132 | DOUBLE { $$ = TypeManager.system_double_expr; }
2137 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2138 | BYTE { $$ = TypeManager.system_byte_expr; }
2139 | SHORT { $$ = TypeManager.system_int16_expr; }
2140 | USHORT { $$ = TypeManager.system_uint16_expr; }
2141 | INT { $$ = TypeManager.system_int32_expr; }
2142 | UINT { $$ = TypeManager.system_uint32_expr; }
2143 | LONG { $$ = TypeManager.system_int64_expr; }
2144 | ULONG { $$ = TypeManager.system_uint64_expr; }
2145 | CHAR { $$ = TypeManager.system_char_expr; }
2146 | VOID { $$ = TypeManager.system_void_expr; }
2150 : type rank_specifiers
2152 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2157 // Expressions, section 7.5
2167 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2169 | parenthesized_expression
2171 | invocation_expression
2175 | post_increment_expression
2176 | post_decrement_expression
2180 | checked_expression
2181 | unchecked_expression
2182 | pointer_member_access
2183 | anonymous_method_expression
2190 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2191 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2192 | NULL { $$ = NullLiteral.Null; }
2196 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2197 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2198 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2203 object v = lexer.Value;
2209 $$ = IntLiteral.Zero;
2211 $$ = IntLiteral.One;
2213 $$ = new IntLiteral (i);
2214 } else if (v is uint)
2215 $$ = new UIntLiteral ((UInt32) v);
2217 $$ = new LongLiteral ((Int64) v);
2218 else if (v is ulong)
2219 $$ = new ULongLiteral ((UInt64) v);
2221 Console.WriteLine ("OOPS. Unexpected result from scanner");
2226 : TRUE { $$ = new BoolLiteral (true); }
2227 | FALSE { $$ = new BoolLiteral (false); }
2230 parenthesized_expression_0
2231 : OPEN_PARENS expression CLOSE_PARENS
2234 lexer.Deambiguate_CloseParens ();
2235 // After this, the next token returned is one of
2236 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2237 // or CLOSE_PARENS_MINUS.
2241 parenthesized_expression
2242 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2246 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2248 // If a parenthesized expression is followed by a minus, we need to wrap
2249 // the expression inside a ParenthesizedExpression for the CS0075 check
2250 // in Binary.DoResolve().
2251 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2256 : primary_expression DOT IDENTIFIER
2258 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2260 | predefined_type DOT IDENTIFIER
2262 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2270 invocation_expression
2271 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2274 Location l = lexer.Location;
2275 Report.Error (1, l, "Parse error");
2277 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2279 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2281 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2283 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2285 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2290 : /* empty */ { $$ = null; }
2297 ArrayList list = new ArrayList (4);
2301 | argument_list COMMA argument
2303 ArrayList list = (ArrayList) $1;
2307 | argument_list error {
2308 CheckToken (1026, yyToken, ", or ) expected");
2315 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2317 | REF variable_reference
2319 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2321 | OUT variable_reference
2323 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2325 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2327 ArrayList list = (ArrayList) $3;
2328 Argument[] args = new Argument [list.Count];
2329 list.CopyTo (args, 0);
2331 Expression expr = new Arglist (args, lexer.Location);
2332 $$ = new Argument (expr, Argument.AType.Expression);
2336 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2341 : expression { note ("section 5.4"); $$ = $1; }
2345 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2347 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2349 | primary_expression rank_specifiers
2351 // So the super-trick is that primary_expression
2352 // can only be either a SimpleName or a MemberAccess.
2353 // The MemberAccess case arises when you have a fully qualified type-name like :
2355 // SimpleName is when you have
2358 Expression expr = (Expression) $1;
2359 if (expr is ComposedCast){
2360 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2361 } else if (!(expr is SimpleName || expr is MemberAccess)){
2362 Error_ExpectingTypeName (lexer.Location, expr);
2363 $$ = TypeManager.system_object_expr;
2366 // So we extract the string corresponding to the SimpleName
2369 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2377 ArrayList list = new ArrayList (4);
2381 | expression_list COMMA expression
2383 ArrayList list = (ArrayList) $1;
2392 $$ = new This (current_block, lexer.Location);
2397 : BASE DOT IDENTIFIER
2399 $$ = new BaseAccess ((string) $3, lexer.Location);
2401 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2403 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2406 Report.Error (175, "Use of keyword `base' is not valid in this context");
2411 post_increment_expression
2412 : primary_expression OP_INC
2414 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2415 (Expression) $1, lexer.Location);
2419 post_decrement_expression
2420 : primary_expression OP_DEC
2422 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2423 (Expression) $1, lexer.Location);
2428 : object_or_delegate_creation_expression
2429 | array_creation_expression
2432 object_or_delegate_creation_expression
2433 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2435 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2439 array_creation_expression
2440 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2442 opt_array_initializer
2444 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2446 | NEW type rank_specifiers array_initializer
2448 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2452 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2468 : rank_specifier opt_rank_specifier
2470 $$ = (string) $2 + (string) $1;
2475 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2477 $$ = "[" + (string) $2 + "]";
2497 | dim_separators COMMA
2499 $$ = (string) $1 + ",";
2503 opt_array_initializer
2515 : OPEN_BRACE CLOSE_BRACE
2517 ArrayList list = new ArrayList (4);
2520 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2522 $$ = (ArrayList) $2;
2526 variable_initializer_list
2527 : variable_initializer
2529 ArrayList list = new ArrayList (4);
2533 | variable_initializer_list COMMA variable_initializer
2535 ArrayList list = (ArrayList) $1;
2542 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2544 $$ = new TypeOfVoid (lexer.Location);
2546 | TYPEOF OPEN_PARENS type CLOSE_PARENS
2548 $$ = new TypeOf ((Expression) $3, lexer.Location);
2553 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2554 $$ = new SizeOf ((Expression) $3, lexer.Location);
2559 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2561 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2565 unchecked_expression
2566 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2568 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2572 pointer_member_access
2573 : primary_expression OP_PTR IDENTIFIER
2577 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2578 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2582 anonymous_method_expression
2583 : DELEGATE opt_anonymous_method_signature {
2584 oob_stack.Push (current_local_parameters);
2585 current_local_parameters = (Parameters)$2;
2586 create_toplevel_block = true;
2589 Report.Error (-213, lexer.Location, "Anonymous methods are not supported in this branch");
2592 create_toplevel_block = false;
2593 if (RootContext.Version == LanguageVersion.ISO_1){
2594 Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
2597 $$ = new AnonymousMethod ((Parameters) $2, (Block) $4, lexer.Location);
2598 current_local_parameters = (Parameters) oob_stack.Pop ();
2603 opt_anonymous_method_signature
2604 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
2605 | anonymous_method_signature
2608 anonymous_method_signature
2609 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
2612 $$ = Parameters.EmptyReadOnlyParameters;
2614 ArrayList par_list = (ArrayList) $2;
2615 Parameter [] pars = new Parameter [par_list.Count];
2616 par_list.CopyTo (pars);
2617 $$ = new Parameters (pars, null, lexer.Location);
2622 opt_anonymous_method_parameter_list
2623 : /* empty */ { $$ = null; }
2624 | anonymous_method_parameter_list { $$ = $1; }
2627 anonymous_method_parameter_list
2628 : anonymous_method_parameter
2630 ArrayList a = new ArrayList (4);
2634 | anonymous_method_parameter_list COMMA anonymous_method_parameter
2636 ArrayList a = (ArrayList) $1;
2642 anonymous_method_parameter
2643 : opt_parameter_modifier type IDENTIFIER {
2644 $$ = new Parameter ((Expression) $2, (string) $2, (Parameter.Modifier) $1, null);
2649 : primary_expression
2650 | BANG prefixed_unary_expression
2652 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2654 | TILDE prefixed_unary_expression
2656 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2662 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
2664 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2666 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
2668 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2674 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2676 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2681 // The idea to split this out is from Rhys' grammar
2682 // to solve the problem with casts.
2684 prefixed_unary_expression
2686 | PLUS prefixed_unary_expression
2688 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2690 | MINUS prefixed_unary_expression
2692 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2694 | OP_INC prefixed_unary_expression
2696 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2697 (Expression) $2, lexer.Location);
2699 | OP_DEC prefixed_unary_expression
2701 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2702 (Expression) $2, lexer.Location);
2704 | STAR prefixed_unary_expression
2706 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2708 | BITWISE_AND prefixed_unary_expression
2710 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2714 pre_increment_expression
2715 : OP_INC prefixed_unary_expression
2717 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2718 (Expression) $2, lexer.Location);
2722 pre_decrement_expression
2723 : OP_DEC prefixed_unary_expression
2725 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2726 (Expression) $2, lexer.Location);
2730 multiplicative_expression
2731 : prefixed_unary_expression
2732 | multiplicative_expression STAR prefixed_unary_expression
2734 $$ = new Binary (Binary.Operator.Multiply,
2735 (Expression) $1, (Expression) $3, lexer.Location);
2737 | multiplicative_expression DIV prefixed_unary_expression
2739 $$ = new Binary (Binary.Operator.Division,
2740 (Expression) $1, (Expression) $3, lexer.Location);
2742 | multiplicative_expression PERCENT prefixed_unary_expression
2744 $$ = new Binary (Binary.Operator.Modulus,
2745 (Expression) $1, (Expression) $3, lexer.Location);
2750 : multiplicative_expression
2751 | additive_expression PLUS multiplicative_expression
2753 $$ = new Binary (Binary.Operator.Addition,
2754 (Expression) $1, (Expression) $3, lexer.Location);
2756 | additive_expression MINUS multiplicative_expression
2758 $$ = new Binary (Binary.Operator.Subtraction,
2759 (Expression) $1, (Expression) $3, lexer.Location);
2764 : additive_expression
2765 | shift_expression OP_SHIFT_LEFT additive_expression
2767 $$ = new Binary (Binary.Operator.LeftShift,
2768 (Expression) $1, (Expression) $3, lexer.Location);
2770 | shift_expression OP_SHIFT_RIGHT additive_expression
2772 $$ = new Binary (Binary.Operator.RightShift,
2773 (Expression) $1, (Expression) $3, lexer.Location);
2777 relational_expression
2779 | relational_expression OP_LT shift_expression
2781 $$ = new Binary (Binary.Operator.LessThan,
2782 (Expression) $1, (Expression) $3, lexer.Location);
2784 | relational_expression OP_GT shift_expression
2786 $$ = new Binary (Binary.Operator.GreaterThan,
2787 (Expression) $1, (Expression) $3, lexer.Location);
2789 | relational_expression OP_LE shift_expression
2791 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2792 (Expression) $1, (Expression) $3, lexer.Location);
2794 | relational_expression OP_GE shift_expression
2796 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2797 (Expression) $1, (Expression) $3, lexer.Location);
2799 | relational_expression IS type
2801 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
2803 | relational_expression AS type
2805 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
2810 : relational_expression
2811 | equality_expression OP_EQ relational_expression
2813 $$ = new Binary (Binary.Operator.Equality,
2814 (Expression) $1, (Expression) $3, lexer.Location);
2816 | equality_expression OP_NE relational_expression
2818 $$ = new Binary (Binary.Operator.Inequality,
2819 (Expression) $1, (Expression) $3, lexer.Location);
2824 : equality_expression
2825 | and_expression BITWISE_AND equality_expression
2827 $$ = new Binary (Binary.Operator.BitwiseAnd,
2828 (Expression) $1, (Expression) $3, lexer.Location);
2832 exclusive_or_expression
2834 | exclusive_or_expression CARRET and_expression
2836 $$ = new Binary (Binary.Operator.ExclusiveOr,
2837 (Expression) $1, (Expression) $3, lexer.Location);
2841 inclusive_or_expression
2842 : exclusive_or_expression
2843 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2845 $$ = new Binary (Binary.Operator.BitwiseOr,
2846 (Expression) $1, (Expression) $3, lexer.Location);
2850 conditional_and_expression
2851 : inclusive_or_expression
2852 | conditional_and_expression OP_AND inclusive_or_expression
2854 $$ = new Binary (Binary.Operator.LogicalAnd,
2855 (Expression) $1, (Expression) $3, lexer.Location);
2859 conditional_or_expression
2860 : conditional_and_expression
2861 | conditional_or_expression OP_OR conditional_and_expression
2863 $$ = new Binary (Binary.Operator.LogicalOr,
2864 (Expression) $1, (Expression) $3, lexer.Location);
2868 conditional_expression
2869 : conditional_or_expression
2870 | conditional_or_expression INTERR expression COLON expression
2872 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2876 assignment_expression
2877 : prefixed_unary_expression ASSIGN expression
2879 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2881 | prefixed_unary_expression OP_MULT_ASSIGN expression
2883 Location l = lexer.Location;
2885 $$ = new CompoundAssign (
2886 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
2888 | prefixed_unary_expression OP_DIV_ASSIGN expression
2890 Location l = lexer.Location;
2892 $$ = new CompoundAssign (
2893 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
2895 | prefixed_unary_expression OP_MOD_ASSIGN expression
2897 Location l = lexer.Location;
2899 $$ = new CompoundAssign (
2900 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
2902 | prefixed_unary_expression OP_ADD_ASSIGN expression
2904 Location l = lexer.Location;
2906 $$ = new CompoundAssign (
2907 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
2909 | prefixed_unary_expression OP_SUB_ASSIGN expression
2911 Location l = lexer.Location;
2913 $$ = new CompoundAssign (
2914 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
2916 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2918 Location l = lexer.Location;
2920 $$ = new CompoundAssign (
2921 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
2923 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2925 Location l = lexer.Location;
2927 $$ = new CompoundAssign (
2928 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
2930 | prefixed_unary_expression OP_AND_ASSIGN expression
2932 Location l = lexer.Location;
2934 $$ = new CompoundAssign (
2935 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
2937 | prefixed_unary_expression OP_OR_ASSIGN expression
2939 Location l = lexer.Location;
2941 $$ = new CompoundAssign (
2942 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
2944 | prefixed_unary_expression OP_XOR_ASSIGN expression
2946 Location l = lexer.Location;
2948 $$ = new CompoundAssign (
2949 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
2954 : conditional_expression
2955 | assignment_expression
2975 MemberName name = MakeName ((MemberName) $5);
2976 bool partial = (bool) $3;
2977 int mod_flags = (int) $2;
2980 ClassPart part = PartialContainer.CreatePart (
2981 current_namespace, current_container, name, mod_flags,
2982 (Attributes) $1, Kind.Class, lexer.Location);
2984 current_container = part.PartialContainer;
2985 current_class = part;
2987 if ((mod_flags & Modifiers.STATIC) != 0) {
2988 current_class = new StaticClass (
2989 current_namespace, current_container, name,
2990 mod_flags, (Attributes) $1, lexer.Location);
2992 current_class = new Class (
2993 current_namespace, current_container, name,
2994 mod_flags, (Attributes) $1, lexer.Location);
2997 current_container = current_class;
2998 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
3004 if (current_class.Name == "System.Object") {
3005 Report.Error (537, current_class.Location,
3006 "The class System.Object cannot have a base " +
3007 "class or implement an interface.");
3009 current_class.Bases = (ArrayList) $7;
3012 current_class.Register ();
3019 current_container = current_container.Parent;
3020 current_class = current_container;
3026 { $$ = (bool) false; }
3028 { $$ = (bool) true; }
3032 : /* empty */ { $$ = (int) 0; }
3038 | modifiers modifier
3043 if ((m1 & m2) != 0) {
3044 Location l = lexer.Location;
3045 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3047 $$ = (int) (m1 | m2);
3052 : NEW { $$ = Modifiers.NEW; }
3053 | PUBLIC { $$ = Modifiers.PUBLIC; }
3054 | PROTECTED { $$ = Modifiers.PROTECTED; }
3055 | INTERNAL { $$ = Modifiers.INTERNAL; }
3056 | PRIVATE { $$ = Modifiers.PRIVATE; }
3057 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3058 | SEALED { $$ = Modifiers.SEALED; }
3059 | STATIC { $$ = Modifiers.STATIC; }
3060 | READONLY { $$ = Modifiers.READONLY; }
3061 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3062 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3063 | EXTERN { $$ = Modifiers.EXTERN; }
3064 | VOLATILE { $$ = Modifiers.VOLATILE; }
3065 | UNSAFE { $$ = Modifiers.UNSAFE; }
3069 : /* empty */ { $$ = null; }
3070 | class_base { $$ = $1; }
3074 : COLON type_list { $$ = $2; }
3082 // A block is "contained" on the following places:
3084 // property_declaration as part of the accessor body (get/set)
3085 // operator_declaration
3086 // constructor_declaration
3087 // destructor_declaration
3088 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3093 if (current_block == null || create_toplevel_block){
3094 current_block = new ToplevelBlock (current_local_parameters, lexer.Location);
3096 current_block = new Block (current_block, current_local_parameters,
3097 lexer.Location, Location.Null);
3100 opt_statement_list CLOSE_BRACE
3102 while (current_block.Implicit)
3103 current_block = current_block.Parent;
3105 current_block.SetEndLocation (lexer.Location);
3106 current_block = current_block.Parent;
3117 | statement_list statement
3121 : declaration_statement
3123 if ($1 != null && (Block) $1 != current_block){
3124 current_block.AddStatement ((Statement) $1);
3125 current_block = (Block) $1;
3128 | valid_declaration_statement
3130 current_block.AddStatement ((Statement) $1);
3135 valid_declaration_statement
3138 | expression_statement
3139 | selection_statement
3140 | iteration_statement
3144 | unchecked_statement
3152 : valid_declaration_statement
3153 | declaration_statement
3155 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3160 Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3168 $$ = EmptyStatement.Value;
3175 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3177 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3178 current_block.AddStatement (labeled);
3183 declaration_statement
3184 : local_variable_declaration SEMICOLON
3187 DictionaryEntry de = (DictionaryEntry) $1;
3189 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3193 | local_constant_declaration SEMICOLON
3196 DictionaryEntry de = (DictionaryEntry) $1;
3198 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3204 * The following is from Rhys' grammar:
3205 * > Types in local variable declarations must be recognized as
3206 * > expressions to prevent reduce/reduce errors in the grammar.
3207 * > The expressions are converted into types during semantic analysis.
3210 : primary_expression opt_rank_specifier
3212 // FIXME: Do something smart here regarding the composition of the type.
3214 // Ok, the above "primary_expression" is there to get rid of
3215 // both reduce/reduce and shift/reduces in the grammar, it should
3216 // really just be "type_name". If you use type_name, a reduce/reduce
3217 // creeps up. If you use namespace_or_type_name (which is all we need
3218 // really) two shift/reduces appear.
3221 // So the super-trick is that primary_expression
3222 // can only be either a SimpleName or a MemberAccess.
3223 // The MemberAccess case arises when you have a fully qualified type-name like :
3225 // SimpleName is when you have
3228 Expression expr = (Expression) $1;
3229 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3230 Error_ExpectingTypeName (lexer.Location, expr);
3234 // So we extract the string corresponding to the SimpleName
3238 if ((string) $2 == "")
3241 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3244 | builtin_types opt_rank_specifier
3246 if ((string) $2 == "")
3249 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3253 local_variable_pointer_type
3254 : primary_expression STAR
3256 Expression expr = (Expression) $1;
3257 Location l = lexer.Location;
3259 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3260 Error_ExpectingTypeName (l, expr);
3264 $$ = new ComposedCast ((Expression) $1, "*", l);
3266 | builtin_types STAR
3268 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3272 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3274 | local_variable_pointer_type STAR
3276 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3280 local_variable_declaration
3281 : local_variable_type variable_declarators
3284 $$ = new DictionaryEntry ($1, $2);
3288 | local_variable_pointer_type opt_rank_specifier variable_declarators
3293 if ((string) $2 == "")
3294 t = (Expression) $1;
3296 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3297 $$ = new DictionaryEntry (t, $3);
3303 local_constant_declaration
3304 : CONST local_variable_type constant_declarators
3307 $$ = new DictionaryEntry ($2, $3);
3313 expression_statement
3314 : statement_expression SEMICOLON
3321 // We have to do the wrapping here and not in the case above,
3322 // because statement_expression is used for example in for_statement
3324 statement_expression
3325 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3326 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3327 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3328 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3329 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3330 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3331 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3333 Report.Error (1002, lexer.Location, "Expecting `;'");
3338 object_creation_expression
3339 : object_or_delegate_creation_expression
3340 { note ("complain if this is a delegate maybe?"); }
3349 : if_statement_open if_statement_rest
3358 oob_stack.Push (lexer.Location);
3363 : boolean_expression CLOSE_PARENS
3366 Location l = (Location) oob_stack.Pop ();
3368 $$ = new If ((Expression) $1, (Statement) $3, l);
3370 if (RootContext.WarningLevel >= 3){
3371 if ($3 == EmptyStatement.Value)
3372 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3376 | boolean_expression CLOSE_PARENS
3377 embedded_statement ELSE embedded_statement
3379 Location l = (Location) oob_stack.Pop ();
3381 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
3386 : SWITCH OPEN_PARENS
3388 oob_stack.Push (lexer.Location);
3389 switch_stack.Push (current_block);
3391 expression CLOSE_PARENS
3394 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
3395 current_block = (Block) switch_stack.Pop ();
3411 Report.Error (1522, lexer.Location, "Empty switch block");
3419 ArrayList sections = new ArrayList (4);
3424 | switch_sections switch_section
3426 ArrayList sections = (ArrayList) $1;
3436 current_block = current_block.CreateSwitchBlock (lexer.Location);
3440 Block topmost = current_block;
3442 while (topmost.Implicit)
3443 topmost = topmost.Parent;
3444 $$ = new SwitchSection ((ArrayList) $1, topmost);
3451 ArrayList labels = new ArrayList (4);
3456 | switch_labels switch_label
3458 ArrayList labels = (ArrayList) ($1);
3466 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3467 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3470 1523, lexer.Location,
3471 "The keyword case or default must precede code in switch block");
3485 oob_stack.Push (lexer.Location);
3487 boolean_expression CLOSE_PARENS embedded_statement
3489 Location l = (Location) oob_stack.Pop ();
3490 $$ = new While ((Expression) $4, (Statement) $6, l);
3492 if (RootContext.WarningLevel >= 3){
3493 if ($6 == EmptyStatement.Value)
3494 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3500 : DO embedded_statement
3502 oob_stack.Push (lexer.Location);
3504 boolean_expression CLOSE_PARENS SEMICOLON
3506 Location l = (Location) oob_stack.Pop ();
3508 $$ = new Do ((Statement) $2, (Expression) $6, l);
3514 opt_for_initializer SEMICOLON
3516 Block assign_block = new Block (current_block);
3517 current_block = assign_block;
3519 if ($3 is DictionaryEntry){
3520 DictionaryEntry de = (DictionaryEntry) $3;
3522 Expression type = (Expression) de.Key;
3523 ArrayList var_declarators = (ArrayList) de.Value;
3525 foreach (VariableDeclaration decl in var_declarators){
3529 vi = current_block.AddVariable (
3530 type, decl.identifier, current_local_parameters, decl.Location);
3534 Location l = lexer.Location;
3536 if (decl.expression_or_array_initializer is Expression){
3537 expr = (Expression) decl.expression_or_array_initializer;
3538 } else if (decl.expression_or_array_initializer == null) {
3541 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3542 expr = new ArrayCreation (type, "", init, decl.Location);
3545 LocalVariableReference var;
3546 var = new LocalVariableReference (assign_block, decl.identifier, l);
3549 Assign a = new Assign (var, expr, decl.Location);
3551 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3557 oob_stack.Push (lexer.Location);
3559 opt_for_condition SEMICOLON
3560 opt_for_iterator CLOSE_PARENS
3563 Location l = (Location) oob_stack.Pop ();
3565 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3567 if (RootContext.WarningLevel >= 3){
3568 if ($10 == EmptyStatement.Value)
3569 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3572 current_block.AddStatement (f);
3573 while (current_block.Implicit)
3574 current_block = current_block.Parent;
3576 current_block = current_block.Parent;
3581 : /* empty */ { $$ = EmptyStatement.Value; }
3586 : local_variable_declaration
3587 | statement_expression_list
3591 : /* empty */ { $$ = null; }
3592 | boolean_expression
3596 : /* empty */ { $$ = EmptyStatement.Value; }
3601 : statement_expression_list
3604 statement_expression_list
3605 : statement_expression
3607 // CHANGE: was `null'
3608 Block b = new Block (current_block, Block.Flags.Implicit);
3610 b.AddStatement ((Statement) $1);
3613 | statement_expression_list COMMA statement_expression
3615 Block b = (Block) $1;
3617 b.AddStatement ((Statement) $3);
3623 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
3625 Report.Error (230, lexer.Location, "Type and identifier are both required in a foreach statement");
3628 | FOREACH OPEN_PARENS type IDENTIFIER IN
3630 oob_stack.Push (lexer.Location);
3632 expression CLOSE_PARENS
3634 oob_stack.Push (current_block);
3636 Block foreach_block = new Block (current_block);
3637 LocalVariableReference v = null;
3638 Location l = lexer.Location;
3641 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3645 // Get a writable reference to this read-only variable.
3646 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3648 current_block = foreach_block;
3651 oob_stack.Push (current_block);
3655 Block foreach_block = (Block) oob_stack.Pop ();
3656 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3657 Block prev_block = (Block) oob_stack.Pop ();
3658 Location l = (Location) oob_stack.Pop ();
3660 current_block = prev_block;
3663 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3664 foreach_block.AddStatement (f);
3673 | continue_statement
3683 $$ = new Break (lexer.Location);
3688 : CONTINUE SEMICOLON
3690 $$ = new Continue (lexer.Location);
3695 : GOTO IDENTIFIER SEMICOLON
3697 $$ = new Goto (current_block, (string) $2, lexer.Location);
3699 | GOTO CASE constant_expression SEMICOLON
3701 $$ = new GotoCase ((Expression) $3, lexer.Location);
3703 | GOTO DEFAULT SEMICOLON
3705 $$ = new GotoDefault (lexer.Location);
3710 : RETURN opt_expression SEMICOLON
3712 $$ = new Return ((Expression) $2, lexer.Location);
3717 : THROW opt_expression SEMICOLON
3719 $$ = new Throw ((Expression) $2, lexer.Location);
3724 : IDENTIFIER RETURN expression SEMICOLON
3726 string s = (string) $1;
3728 Report.Error (1003, lexer.Location, "; expected");
3731 if (RootContext.Version == LanguageVersion.ISO_1){
3732 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
3735 if (iterator_container == null){
3736 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3739 iterator_container.SetYields ();
3740 $$ = new Yield ((Expression) $3, lexer.Location);
3743 | IDENTIFIER BREAK SEMICOLON
3745 string s = (string) $1;
3747 Report.Error (1003, lexer.Location, "; expected");
3750 if (RootContext.Version == LanguageVersion.ISO_1){
3751 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
3754 if (iterator_container == null){
3755 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3758 iterator_container.SetYields ();
3759 $$ = new YieldBreak (lexer.Location);
3770 : TRY block catch_clauses
3774 ArrayList c = (ArrayList)$3;
3775 for (int i = 0; i < c.Count; ++i) {
3776 Catch cc = (Catch) c [i];
3778 if (i != c.Count - 1)
3779 Report.Error (1017, cc.loc, "Empty catch block must be the last in a series of catch blocks");
3786 // Now s contains the list of specific catch clauses
3787 // and g contains the general one.
3789 $$ = new Try ((Block) $2, c, g, null, lexer.Location);
3791 | TRY block opt_catch_clauses FINALLY block
3794 ArrayList s = new ArrayList (4);
3795 ArrayList catch_list = (ArrayList) $3;
3797 if (catch_list != null){
3798 foreach (Catch cc in catch_list) {
3806 $$ = new Try ((Block) $2, s, g, (Block) $5, lexer.Location);
3810 Report.Error (1524, lexer.Location, "Expected catch or finally");
3815 : /* empty */ { $$ = null; }
3822 ArrayList l = new ArrayList (4);
3827 | catch_clauses catch_clause
3829 ArrayList l = (ArrayList) $1;
3837 : /* empty */ { $$ = null; }
3842 : CATCH opt_catch_args
3844 Expression type = null;
3848 DictionaryEntry cc = (DictionaryEntry) $2;
3849 type = (Expression) cc.Key;
3850 id = (string) cc.Value;
3853 ArrayList one = new ArrayList (4);
3854 Location loc = lexer.Location;
3856 one.Add (new VariableDeclaration (id, null, loc));
3859 current_block = new Block (current_block);
3860 Block b = declare_local_variables (type, one, loc);
3865 Expression type = null;
3869 DictionaryEntry cc = (DictionaryEntry) $2;
3870 type = (Expression) cc.Key;
3871 id = (string) cc.Value;
3875 // FIXME: I can change this for an assignment.
3877 while (current_block != (Block) $1)
3878 current_block = current_block.Parent;
3883 $$ = new Catch (type, id , (Block) $4, lexer.Location);
3888 : /* empty */ { $$ = null; }
3893 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3895 $$ = new DictionaryEntry ($2, $3);
3902 $$ = new Checked ((Block) $2);
3909 $$ = new Unchecked ((Block) $2);
3916 if (!RootContext.Unsafe){
3917 Report.Error (227, lexer.Location,
3918 "Unsafe code can only be used if --unsafe is used");
3921 $$ = new Unsafe ((Block) $3);
3927 type fixed_pointer_declarators
3930 ArrayList list = (ArrayList) $4;
3931 Expression type = (Expression) $3;
3932 Location l = lexer.Location;
3933 int top = list.Count;
3935 Block assign_block = new Block (current_block);
3936 current_block = assign_block;
3938 for (int i = 0; i < top; i++){
3939 Pair p = (Pair) list [i];
3942 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3956 Location l = (Location) oob_stack.Pop ();
3958 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
3960 if (RootContext.WarningLevel >= 3){
3961 if ($7 == EmptyStatement.Value)
3962 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3965 current_block.AddStatement (f);
3966 while (current_block.Implicit)
3967 current_block = current_block.Parent;
3969 current_block = current_block.Parent;
3973 fixed_pointer_declarators
3974 : fixed_pointer_declarator {
3975 ArrayList declarators = new ArrayList (4);
3977 declarators.Add ($1);
3980 | fixed_pointer_declarators COMMA fixed_pointer_declarator
3982 ArrayList declarators = (ArrayList) $1;
3984 declarators.Add ($3);
3989 fixed_pointer_declarator
3990 : IDENTIFIER ASSIGN expression
3992 $$ = new Pair ($1, $3);
3996 Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
4002 : LOCK OPEN_PARENS expression CLOSE_PARENS
4008 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4013 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4015 Block assign_block = new Block (current_block);
4016 current_block = assign_block;
4018 oob_stack.Push (lexer.Location);
4020 if ($3 is DictionaryEntry){
4021 DictionaryEntry de = (DictionaryEntry) $3;
4022 Location l = lexer.Location;
4024 Expression type = (Expression) de.Key;
4025 ArrayList var_declarators = (ArrayList) de.Value;
4027 ArrayList vars = new ArrayList (4);
4029 foreach (VariableDeclaration decl in var_declarators){
4031 LocalInfo vi = current_block.AddVariable (
4032 type, decl.identifier,
4033 current_local_parameters, decl.Location);
4039 if (decl.expression_or_array_initializer is Expression){
4040 expr = (Expression) decl.expression_or_array_initializer;
4042 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4044 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4047 expr = new ArrayCreation (type, "", init, decl.Location);
4050 LocalVariableReference var;
4052 // Get a writable reference to this read-only variable.
4053 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4055 // This is so that it is not a warning on using variables
4058 vars.Add (new DictionaryEntry (var, expr));
4060 // Assign a = new Assign (var, expr, decl.Location);
4061 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4063 $3 = new DictionaryEntry (type, vars);
4068 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
4069 current_block.AddStatement (u);
4070 while (current_block.Implicit)
4071 current_block = current_block.Parent;
4073 current_block = current_block.Parent;
4077 resource_acquisition
4078 : local_variable_declaration
4085 // A class used to pass around variable declarations and constants
4087 public class VariableDeclaration {
4088 public string identifier;
4089 public object expression_or_array_initializer;
4090 public Location Location;
4091 public Attributes OptAttributes;
4093 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4095 this.identifier = id;
4096 this.expression_or_array_initializer = eoai;
4098 this.OptAttributes = opt_attrs;
4101 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4107 /// Used to pass around interface property information
4109 public class InterfaceAccessorInfo {
4111 public readonly Accessor Get, Set;
4113 public InterfaceAccessorInfo (bool has_get, bool has_set,
4114 Attributes get_attrs, Attributes set_attrs, Location get_loc, Location set_loc)
4117 Get = new Accessor (null, get_attrs, get_loc);
4119 Set = new Accessor (null, set_attrs, set_loc);
4125 // A class used to hold info about an indexer declarator
4127 public class IndexerDeclaration {
4128 public Expression type;
4129 public MemberName interface_type;
4130 public Parameters param_list;
4132 public IndexerDeclaration (Expression type, MemberName interface_type,
4133 Parameters param_list)
4136 this.interface_type = interface_type;
4137 this.param_list = param_list;
4142 // We use this when we do not have an object in advance that is an IIteratorContainer
4144 public class SimpleIteratorContainer : IIteratorContainer {
4147 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4152 public static SimpleIteratorContainer GetSimple () {
4153 Simple.Yields = false;
4157 public void SetYields () { Yields = true; }
4161 // A class used to hold info about an operator declarator
4163 public class OperatorDeclaration {
4164 public Operator.OpType optype;
4165 public Expression ret_type, arg1type, arg2type;
4166 public string arg1name, arg2name;
4167 public Location location;
4169 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4170 Expression arg1type, string arg1name,
4171 Expression arg2type, string arg2name, Location location)
4174 this.ret_type = ret_type;
4175 this.arg1type = arg1type;
4176 this.arg1name = arg1name;
4177 this.arg2type = arg2type;
4178 this.arg2name = arg2name;
4179 this.location = location;
4184 void Error_ExpectingTypeName (Location l, Expression expr)
4186 if (expr is Invocation){
4187 Report.Error (1002, l, "; expected");
4189 Report.Error (-1, l, "Invalid Type definition");
4194 // Given the @class_name name, it creates a fully qualified name
4195 // based on the containing declaration space
4198 MakeName (MemberName class_name)
4200 string ns = current_namespace.FullName;
4202 if (current_container.Name == ""){
4204 return new MemberName (new MemberName (ns), class_name);
4208 return new MemberName (current_container.MemberName, class_name);
4212 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4214 Block implicit_block;
4215 ArrayList inits = null;
4218 // We use the `Used' property to check whether statements
4219 // have been added to the current block. If so, we need
4220 // to create another block to contain the new declaration
4221 // otherwise, as an optimization, we use the same block to
4222 // add the declaration.
4224 // FIXME: A further optimization is to check if the statements
4225 // that were added were added as part of the initialization
4226 // below. In which case, no other statements have been executed
4227 // and we might be able to reduce the number of blocks for
4228 // situations like this:
4230 // int j = 1; int k = j + 1;
4232 if (current_block.Used)
4233 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4235 implicit_block = current_block;
4237 foreach (VariableDeclaration decl in variable_declarators){
4239 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4240 if (decl.expression_or_array_initializer != null){
4242 inits = new ArrayList (4);
4249 return implicit_block;
4251 foreach (VariableDeclaration decl in inits){
4255 if (decl.expression_or_array_initializer is Expression){
4256 expr = (Expression) decl.expression_or_array_initializer;
4259 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4261 expr = new ArrayCreation (type, "", init, decl.Location);
4264 LocalVariableReference var;
4265 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4267 assign = new Assign (var, expr, decl.Location);
4269 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4272 return implicit_block;
4275 Block declare_local_constants (Expression type, ArrayList declarators)
4277 Block implicit_block;
4279 if (current_block.Used)
4280 implicit_block = new Block (current_block, Block.Flags.Implicit);
4282 implicit_block = current_block;
4284 foreach (VariableDeclaration decl in declarators){
4285 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4286 current_local_parameters, decl.Location);
4289 return implicit_block;
4292 void CheckAttributeTarget (string a)
4296 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4300 Location l = lexer.Location;
4301 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4307 void CheckUnaryOperator (Operator.OpType op)
4311 case Operator.OpType.LogicalNot:
4312 case Operator.OpType.OnesComplement:
4313 case Operator.OpType.Increment:
4314 case Operator.OpType.Decrement:
4315 case Operator.OpType.True:
4316 case Operator.OpType.False:
4317 case Operator.OpType.Addition:
4318 case Operator.OpType.Subtraction:
4323 Location l = lexer.Location;
4324 Report.Error (1019, l, "Overloadable unary operator expected");
4330 void CheckBinaryOperator (Operator.OpType op)
4334 case Operator.OpType.Addition:
4335 case Operator.OpType.Subtraction:
4336 case Operator.OpType.Multiply:
4337 case Operator.OpType.Division:
4338 case Operator.OpType.Modulus:
4339 case Operator.OpType.BitwiseAnd:
4340 case Operator.OpType.BitwiseOr:
4341 case Operator.OpType.ExclusiveOr:
4342 case Operator.OpType.LeftShift:
4343 case Operator.OpType.RightShift:
4344 case Operator.OpType.Equality:
4345 case Operator.OpType.Inequality:
4346 case Operator.OpType.GreaterThan:
4347 case Operator.OpType.LessThan:
4348 case Operator.OpType.GreaterThanOrEqual:
4349 case Operator.OpType.LessThanOrEqual:
4353 Location l = lexer.Location;
4354 Report.Error (1020, l, "Overloadable binary operator expected");
4360 void syntax_error (Location l, string msg)
4362 Report.Error (1003, l, "Syntax error, " + msg);
4365 void output (string s)
4367 Console.WriteLine (s);
4370 void note (string s)
4372 // Used to put annotations
4377 public Tokenizer Lexer {
4383 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4385 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4386 this.name = file.Name;
4388 current_container = RootContext.Tree.Types;
4389 current_container.NamespaceEntry = current_namespace;
4390 oob_stack = new Stack ();
4391 switch_stack = new Stack ();
4393 lexer = new Tokenizer (reader, file, defines);
4396 public void parse ()
4399 if (yacc_verbose_flag)
4400 yyparse (lexer, new yydebug.yyDebugSimple ());
4403 Tokenizer tokenizer = lexer as Tokenizer;
4404 tokenizer.cleanup ();
4405 } catch (Exception e){
4406 // Please do not remove this, it is used during debugging
4409 Console.WriteLine (e);
4410 Report.Error (-25, lexer.Location, "Parsing error");
4411 if (Driver.parser_verbose)
4412 Console.WriteLine (e);
4416 void CheckToken (int error, int yyToken, string msg)
4418 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4419 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4422 Report.Error (error, lexer.Location, msg);
4425 void CheckIdentifierToken (int yyToken)
4427 CheckToken (1041, yyToken, "Identifier expected");