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;
33 public class CSharpParser : GenericParser {
34 NamespaceEntry current_namespace;
35 TypeContainer current_container;
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;
53 /// Current interface is used by the various declaration
54 /// productions in the interface declaration to "add"
55 /// the interfaces as we find them.
57 Interface current_interface;
60 /// This is used by the unary_expression code to resolve
61 /// a name against a parameter.
63 Parameters current_local_parameters;
66 /// Using during property parsing to describe the implicit
67 /// value parameter that is passed to the "set" and "get"accesor
68 /// methods (properties and indexers).
70 Expression implicit_value_parameter_type;
71 Parameters indexer_parameters;
74 /// Used to determine if we are parsing the get/set pair
75 /// of an indexer or a property
80 /// An out-of-band stack.
95 /// Current attribute target
96 string current_attr_target;
98 /// assembly and module attribute definition is enabled
99 bool global_attrs_enabled = true;
103 %token NONE /* This token is never returned by our lexer */
104 %token ERROR // This is used not by the parser, but by the tokenizer.
108 *These are the C# keywords
193 /* C# keywords which are not really keywords */
199 /* C# single character operators/punctuation. */
200 %token OPEN_BRACE "{"
201 %token CLOSE_BRACE "}"
202 %token OPEN_BRACKET "["
203 %token CLOSE_BRACKET "]"
204 %token OPEN_PARENS "("
205 %token CLOSE_PARENS ")"
218 %token BITWISE_AND "&"
219 %token BITWISE_OR "|"
226 /* C# multi-character operators. */
229 %token OP_SHIFT_LEFT "<<"
230 %token OP_SHIFT_RIGHT ">>"
237 %token OP_MULT_ASSIGN "*="
238 %token OP_DIV_ASSIGN "/="
239 %token OP_MOD_ASSIGN "%="
240 %token OP_ADD_ASSIGN "+="
241 %token OP_SUB_ASSIGN "-="
242 %token OP_SHIFT_LEFT_ASSIGN "<<="
243 %token OP_SHIFT_RIGHT_ASSIGN ">>="
244 %token OP_AND_ASSIGN "&="
245 %token OP_XOR_ASSIGN "^="
246 %token OP_OR_ASSIGN "|="
250 %token LITERAL_INTEGER "int literal"
251 %token LITERAL_FLOAT "float literal"
252 %token LITERAL_DOUBLE "double literal"
253 %token LITERAL_DECIMAL "decimal literal"
254 %token LITERAL_CHARACTER "character literal"
255 %token LITERAL_STRING "string literal"
258 %token CLOSE_PARENS_CAST
259 %token CLOSE_PARENS_NO_CAST
260 %token CLOSE_PARENS_OPEN_PARENS
261 %token CLOSE_PARENS_MINUS
263 /* Add precedence rules to solve dangling else s/r conflict */
272 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
274 %left STAR DIV PERCENT
275 %right BANG CARRET UMINUS
276 %nonassoc OP_INC OP_DEC
278 %left OPEN_BRACKET OPEN_BRACE
282 %start compilation_unit
286 : outer_declarations opt_EOF
287 | outer_declarations global_attributes opt_EOF
288 | global_attributes opt_EOF
289 | opt_EOF /* allow empty files */
299 | outer_declarations outer_declaration
304 | namespace_member_declaration
309 | using_directives using_directive
313 : using_alias_directive
314 | using_namespace_directive
317 using_alias_directive
318 : USING IDENTIFIER ASSIGN
319 namespace_or_type_name SEMICOLON
321 current_namespace.UsingAlias ((string) $2, (Expression) $4, lexer.Location);
324 CheckIdentifierToken (yyToken);
328 using_namespace_directive
329 : USING namespace_name SEMICOLON
331 current_namespace.Using ((string) $2, lexer.Location);
336 // Strictly speaking, namespaces don't have attributes but
337 // we parse global attributes along with namespace declarations and then
340 namespace_declaration
341 : opt_attributes NAMESPACE namespace_or_type_name
344 Report.Error(1518, Lexer.Location, "Attributes cannot be applied to namespaces."
345 + " Expected class, delegate, enum, interface, or struct");
348 if (current_namespace.Parent != null && $3 is MemberAccess) {
349 // Cannot use qualified namespace names in nested namespace declarations
350 Report.Error_T (134, lexer.Location);
353 current_namespace = RootContext.Tree.RecordNamespace (current_namespace, file, $3.ToString (), lexer.Location);
355 namespace_body opt_semicolon
357 current_namespace = current_namespace.Parent;
372 : namespace_or_type_name { $$ = ($1).ToString (); }
378 opt_namespace_member_declarations
389 opt_namespace_member_declarations
391 | namespace_member_declarations
394 namespace_member_declarations
395 : namespace_member_declaration
396 | namespace_member_declarations namespace_member_declaration
399 namespace_member_declaration
406 Class c = (Class) $1;
407 mod_flags = c.ModFlags;
409 } else if ($1 is Struct){
410 Struct s = (Struct) $1;
411 mod_flags = s.ModFlags;
416 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
418 1527, lexer.Location,
419 "Namespace elements cant be explicitly " +
420 "declared private or protected in `" + name + "'");
422 current_namespace.DeclarationFound = true;
424 | namespace_declaration {
425 current_namespace.DeclarationFound = true;
428 | field_declaration {
429 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
431 | method_declaration {
432 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
439 | interface_declaration
441 | delegate_declaration
443 // Enable this when we have handled all errors, because this acts as a generic fallback
446 // Console.WriteLine ("Token=" + yyToken);
447 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
459 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
467 global_attrs_enabled = false;
472 global_attrs_enabled = false;
481 ArrayList sect = (ArrayList) $1;
483 if (global_attrs_enabled) {
484 if (current_attr_target == "module") {
485 CodeGen.Module.AddAttributes (sect);
487 } else if (current_attr_target == "assembly") {
488 CodeGen.Assembly.AddAttributes (sect);
491 $$ = new Attributes (sect);
494 $$ = new Attributes (sect);
496 current_attr_target = null;
498 | attribute_sections attribute_section
500 Attributes attrs = $1 as Attributes;
501 ArrayList sect = (ArrayList) $2;
503 if (global_attrs_enabled) {
504 if (current_attr_target == "module") {
505 CodeGen.Module.AddAttributes (sect);
507 } else if (current_attr_target == "assembly") {
508 CodeGen.Assembly.AddAttributes (sect);
512 attrs = new Attributes (sect);
514 attrs.AddAttributes (sect);
518 attrs = new Attributes (sect);
520 attrs.AddAttributes (sect);
523 current_attr_target = null;
528 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
532 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
538 attribute_target_specifier
539 : attribute_target COLON
541 current_attr_target = (string)$1;
549 CheckAttributeTarget ((string) $1);
552 | EVENT { $$ = "event"; }
553 | RETURN { $$ = "return"; }
559 ArrayList attrs = new ArrayList (4);
565 | attribute_list COMMA attribute
567 ArrayList attrs = (ArrayList) $1;
579 opt_attribute_arguments
581 if (current_attr_target == "assembly" || current_attr_target == "module")
582 $$ = new GlobalAttribute (current_container, current_attr_target, (string) ($1).ToString (), (ArrayList) $3, (Location) $2);
584 $$ = new Attribute (current_attr_target, (string) ($1).ToString (), (ArrayList) $3, (Location) $2);
589 : type_name { /* reserved attribute name or identifier: 17.4 */ }
592 opt_attribute_arguments
593 : /* empty */ { $$ = null; }
594 | OPEN_PARENS attribute_arguments CLOSE_PARENS
602 : opt_positional_argument_list
607 ArrayList args = new ArrayList (4);
613 | positional_argument_list COMMA named_argument_list
615 ArrayList args = new ArrayList (4);
621 | named_argument_list
623 ArrayList args = new ArrayList (4);
632 opt_positional_argument_list
633 : /* empty */ { $$ = null; }
634 | positional_argument_list
637 positional_argument_list
640 ArrayList args = new ArrayList (4);
641 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
645 | positional_argument_list COMMA expression
647 ArrayList args = (ArrayList) $1;
648 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
657 ArrayList args = new ArrayList (4);
662 | named_argument_list COMMA named_argument
664 ArrayList args = (ArrayList) $1;
672 : IDENTIFIER ASSIGN expression
674 $$ = new DictionaryEntry (
676 new Argument ((Expression) $3, Argument.AType.Expression));
682 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
685 opt_class_member_declarations
687 | class_member_declarations
690 class_member_declarations
691 : class_member_declaration
692 | class_member_declarations
693 class_member_declaration
696 class_member_declaration
697 : constant_declaration // done
698 | field_declaration // done
699 | method_declaration // done
700 | property_declaration // done
701 | event_declaration // done
702 | indexer_declaration // done
703 | operator_declaration // done
704 | constructor_declaration // done
705 | destructor_declaration // done
715 string full_struct_name = MakeName ((string) $4);
717 new_struct = new Struct (current_namespace, current_container, full_struct_name,
718 (int) $2, (Attributes) $1, lexer.Location);
719 current_container = new_struct;
720 RootContext.Tree.RecordDecl (full_struct_name, new_struct);
726 Struct new_struct = (Struct) current_container;
729 new_struct.Bases = (ArrayList) $6;
731 current_container = current_container.Parent;
732 CheckDef (current_container.AddStruct (new_struct), new_struct.Name, new_struct.Location);
735 | opt_attributes opt_modifiers STRUCT error {
736 CheckIdentifierToken (yyToken);
741 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
744 opt_struct_member_declarations
746 | struct_member_declarations
749 struct_member_declarations
750 : struct_member_declaration
751 | struct_member_declarations struct_member_declaration
754 struct_member_declaration
755 : constant_declaration
758 | property_declaration
760 | indexer_declaration
761 | operator_declaration
762 | constructor_declaration
766 * This is only included so we can flag error 575:
767 * destructors only allowed on class types
769 | destructor_declaration
780 foreach (VariableDeclaration constant in (ArrayList) $5){
781 Location l = constant.Location;
783 Const c = new Const (
784 (Expression) $4, (string) constant.identifier,
785 (Expression) constant.expression_or_array_initializer, (int) $2,
788 CheckDef (current_container.AddConstant (c), c.Name, l);
794 : constant_declarator
796 ArrayList constants = new ArrayList (4);
801 | constant_declarators COMMA constant_declarator
804 ArrayList constants = (ArrayList) $1;
811 : IDENTIFIER ASSIGN constant_expression
813 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
817 // A const field requires a value to be provided
818 Report.Error_T (145, lexer.Location);
830 Expression type = (Expression) $3;
833 foreach (VariableDeclaration var in (ArrayList) $4){
834 Location l = var.Location;
836 Field field = new Field (type, mod, var.identifier,
837 var.expression_or_array_initializer,
840 CheckDef (current_container.AddField (field), field.Name, l);
848 Report.Error (670, lexer.Location, "void type is not allowed for fields");
853 : variable_declarator
855 ArrayList decl = new ArrayList (4);
859 | variable_declarators COMMA variable_declarator
861 ArrayList decls = (ArrayList) $1;
868 : IDENTIFIER ASSIGN variable_initializer
870 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
874 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
887 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
889 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
895 iterator_container = (IIteratorContainer) $1;
899 Method method = (Method) $1;
900 Block b = (Block) $3;
901 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
904 if ((method.ModFlags & extern_abstract) == 0){
906 501, lexer.Location, current_container.MakeName (method.Name) +
907 "must declare a body because it is not marked abstract or extern");
910 if ((method.ModFlags & Modifiers.EXTERN) != 0){
912 179, lexer.Location, current_container.MakeName (method.Name) +
913 " is declared extern, but has a body");
917 method.Block = (Block) $3;
918 CheckDef (current_container.AddMethod (method), method.Name, method.Location);
920 current_local_parameters = null;
921 iterator_container = null;
935 1585, lexer.Location, "Member modifier `" +
936 Modifiers.Name (i) + "' must precede member type and name");
949 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
951 Method method = new Method (current_container, (Expression) $3, (int) $2,
952 false, (string) $4, (Parameters) $6,
953 (Attributes) $1, lexer.Location);
955 current_local_parameters = (Parameters) $6;
963 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
965 Method method = new Method (current_container, TypeManager.system_void_expr,
966 (int) $2, false, (string) $4, (Parameters) $6,
967 (Attributes) $1, lexer.Location);
969 current_local_parameters = (Parameters) $6;
975 modifiers member_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
977 Report.Error (1585, lexer.Location,
978 String.Format ("Modifier {0} should appear before type",
979 Modifiers.Name ((int) $4)));
980 Method method = new Method (current_container, TypeManager.system_void_expr,
981 0, false, (string) $5, (Parameters) $7,
982 (Attributes) $1, lexer.Location);
984 current_local_parameters = (Parameters) $7;
991 | SEMICOLON { $$ = null; }
994 opt_formal_parameter_list
995 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
996 | formal_parameter_list
999 formal_parameter_list
1002 ArrayList pars_list = (ArrayList) $1;
1004 Parameter [] pars = new Parameter [pars_list.Count];
1005 pars_list.CopyTo (pars);
1007 $$ = new Parameters (pars, null, lexer.Location);
1009 | fixed_parameters COMMA parameter_array
1011 ArrayList pars_list = (ArrayList) $1;
1013 Parameter [] pars = new Parameter [pars_list.Count];
1014 pars_list.CopyTo (pars);
1016 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
1018 | fixed_parameters COMMA ARGLIST
1020 ArrayList pars_list = (ArrayList) $1;
1022 Parameter [] pars = new Parameter [pars_list.Count];
1023 pars_list.CopyTo (pars);
1025 $$ = new Parameters (pars, true, lexer.Location);
1029 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
1033 $$ = new Parameters (null, true, lexer.Location);
1040 ArrayList pars = new ArrayList (4);
1045 | fixed_parameters COMMA fixed_parameter
1047 ArrayList pars = (ArrayList) $1;
1056 opt_parameter_modifier
1060 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
1063 opt_parameter_modifier
1066 CheckIdentifierToken (yyToken);
1071 opt_parameter_modifier
1072 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1073 | parameter_modifier
1077 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1078 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1082 : opt_attributes PARAMS type IDENTIFIER
1084 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1085 note ("type must be a single-dimension array type");
1087 | opt_attributes PARAMS type error {
1088 CheckIdentifierToken (yyToken);
1094 : namespace_or_type_name { $$ = ($1).ToString (); }
1097 property_declaration
1103 implicit_value_parameter_type = (Expression) $3;
1105 lexer.PropertyParsing = true;
1107 $$ = lexer.Location;
1109 iterator_container = SimpleIteratorContainer.GetSimple ();
1111 accessor_declarations
1113 lexer.PropertyParsing = false;
1118 Pair pair = (Pair) $7;
1119 Accessor get_block = (Accessor) pair.First;
1120 Accessor set_block = (Accessor) pair.Second;
1122 Location loc = (Location) $6;
1123 prop = new Property (current_container, (Expression) $3, (int) $2, false,
1124 (string) $4, (Attributes) $1, get_block, set_block, loc);
1125 if (SimpleIteratorContainer.Simple.Yields)
1128 CheckDef (current_container.AddProperty (prop), prop.Name, loc);
1129 implicit_value_parameter_type = null;
1130 iterator_container = null;
1134 accessor_declarations
1135 : get_accessor_declaration opt_set_accessor_declaration
1137 $$ = new Pair ($1, $2);
1139 | set_accessor_declaration opt_get_accessor_declaration
1141 $$ = new Pair ($2, $1);
1145 opt_get_accessor_declaration
1146 : /* empty */ { $$ = null; }
1147 | get_accessor_declaration
1150 opt_set_accessor_declaration
1151 : /* empty */ { $$ = null; }
1152 | set_accessor_declaration
1155 get_accessor_declaration
1156 : opt_attributes GET
1158 // If this is not the case, then current_local_parameters has already
1159 // been set in indexer_declaration
1160 if (parsing_indexer == false)
1161 current_local_parameters = null;
1163 current_local_parameters = indexer_parameters;
1164 lexer.PropertyParsing = false;
1168 $$ = new Accessor ((Block) $4, (Attributes) $1);
1169 current_local_parameters = null;
1170 lexer.PropertyParsing = true;
1174 set_accessor_declaration
1175 : opt_attributes SET
1178 Parameter implicit_value_parameter = new Parameter (
1179 implicit_value_parameter_type, "value",
1180 Parameter.Modifier.NONE, null);
1182 if (parsing_indexer == false) {
1183 args = new Parameter [1];
1184 args [0] = implicit_value_parameter;
1185 current_local_parameters = new Parameters (args, null, lexer.Location);
1187 Parameter [] fpars = indexer_parameters.FixedParameters;
1190 int count = fpars.Length;
1192 args = new Parameter [count + 1];
1193 fpars.CopyTo (args, 0);
1194 args [count] = implicit_value_parameter;
1197 current_local_parameters = new Parameters (
1198 args, indexer_parameters.ArrayParameter, lexer.Location);
1201 lexer.PropertyParsing = false;
1205 $$ = new Accessor ((Block) $4, (Attributes) $1);
1206 current_local_parameters = null;
1207 lexer.PropertyParsing = true;
1213 | SEMICOLON { $$ = null; }
1216 interface_declaration
1219 INTERFACE IDENTIFIER
1221 Interface new_interface;
1222 string full_interface_name = MakeName ((string) $4);
1224 new_interface = new Interface (current_namespace, current_container, full_interface_name,
1225 (int) $2, (Attributes) $1, lexer.Location);
1226 if (current_interface != null) {
1227 Location l = lexer.Location;
1228 Report.Error (-2, l, "Internal compiler error: interface inside interface");
1230 current_interface = new_interface;
1231 current_container = new_interface;
1232 RootContext.Tree.RecordDecl (full_interface_name, new_interface);
1235 interface_body opt_semicolon
1237 Interface new_interface = (Interface) current_interface;
1240 new_interface.Bases = (ArrayList) $6;
1242 current_interface = null;
1243 current_container = current_container.Parent;
1244 CheckDef (current_container.AddInterface (new_interface),
1245 new_interface.Name, new_interface.Location);
1247 | opt_attributes opt_modifiers INTERFACE error {
1248 CheckIdentifierToken (yyToken);
1254 opt_interface_member_declarations
1258 opt_interface_member_declarations
1260 | interface_member_declarations
1263 interface_member_declarations
1264 : interface_member_declaration
1265 | interface_member_declarations interface_member_declaration
1268 interface_member_declaration
1269 : interface_method_declaration
1271 Method m = (Method) $1;
1273 CheckDef (current_interface.AddMethod (m), m.Name, m.Location);
1275 | interface_property_declaration
1277 Property p = (Property) $1;
1279 CheckDef (current_interface.AddProperty (p), p.Name, p.Location);
1281 | interface_event_declaration
1284 Event e = (Event) $1;
1285 CheckDef (current_interface.AddEvent (e), e.Name, lexer.Location);
1288 | interface_indexer_declaration
1290 Indexer i = (Indexer) $1;
1292 current_interface.AddIndexer (i);
1300 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, lexer.Location);
1305 interface_method_declaration
1306 : opt_attributes opt_new type IDENTIFIER
1307 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1310 $$ = new Method (current_container, (Expression) $3, (int) $2, true,
1311 (string) $4, (Parameters) $6, (Attributes) $1,
1314 | opt_attributes opt_new VOID IDENTIFIER
1315 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1318 $$ = new Method (current_container, TypeManager.system_void_expr, (int) $2,
1319 true, (string) $4, (Parameters) $6, (Attributes) $1,
1324 interface_property_declaration
1329 { lexer.PropertyParsing = true; }
1331 { lexer.PropertyParsing = false; }
1334 InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
1336 $$ = new Property (current_container, (Expression) $3, (int) $2, true,
1337 (string) $4, (Attributes) $1, pinfo.Get, pinfo.Set,
1343 CheckIdentifierToken (yyToken);
1349 : opt_attributes GET SEMICOLON { $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null); }
1350 | opt_attributes SET SEMICOLON { $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1); }
1351 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1352 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $3); }
1353 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1354 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $3, (Attributes) $1); }
1357 interface_event_declaration
1358 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1360 $$ = new EventField (current_container, (Expression) $4, (int) $2, true, (string) $5, null,
1361 (Attributes) $1, lexer.Location);
1363 | opt_attributes opt_new EVENT type error {
1364 CheckIdentifierToken (yyToken);
1367 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1368 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1371 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE event_accessor_declarations CLOSE_BRACE {
1372 Report.Error (69, lexer.Location, "Event accessors not valid on interfaces");
1377 interface_indexer_declaration
1378 : opt_attributes opt_new type THIS
1379 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1381 { lexer.PropertyParsing = true; }
1383 { lexer.PropertyParsing = false; }
1386 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1388 $$ = new Indexer (current_container, (Expression) $3, null, (int) $2, true,
1389 (Parameters) $6, (Attributes) $1, info.Get, info.Set,
1394 operator_declaration
1395 : opt_attributes opt_modifiers operator_declarator
1397 iterator_container = SimpleIteratorContainer.GetSimple ();
1401 OperatorDeclaration decl = (OperatorDeclaration) $3;
1403 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
1404 decl.arg2type, decl.arg2name, (Block) $5, (Attributes) $1, decl.location);
1406 if (SimpleIteratorContainer.Simple.Yields)
1409 // Note again, checking is done in semantic analysis
1410 current_container.AddOperator (op);
1412 current_local_parameters = null;
1413 iterator_container = null;
1419 | SEMICOLON { $$ = null; }
1422 : type OPERATOR overloadable_operator
1423 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1425 Operator.OpType op = (Operator.OpType) $3;
1426 CheckUnaryOperator (op);
1428 if (op == Operator.OpType.Addition)
1429 op = Operator.OpType.UnaryPlus;
1431 if (op == Operator.OpType.Subtraction)
1432 op = Operator.OpType.UnaryNegation;
1434 Parameter [] pars = new Parameter [1];
1436 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1438 current_local_parameters = new Parameters (pars, null, lexer.Location);
1440 $$ = new OperatorDeclaration (op, (Expression) $1, (Expression) $5, (string) $6,
1441 null, null, lexer.Location);
1443 | type OPERATOR overloadable_operator
1445 type IDENTIFIER COMMA
1449 CheckBinaryOperator ((Operator.OpType) $3);
1451 Parameter [] pars = new Parameter [2];
1453 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1454 pars [1] = new Parameter ((Expression) $8, (string) $9, Parameter.Modifier.NONE, null);
1456 current_local_parameters = new Parameters (pars, null, lexer.Location);
1458 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1459 (Expression) $5, (string) $6,
1460 (Expression) $8, (string) $9, lexer.Location);
1462 | conversion_operator_declarator
1465 overloadable_operator
1467 : BANG { $$ = Operator.OpType.LogicalNot; }
1468 | TILDE { $$ = Operator.OpType.OnesComplement; }
1469 | OP_INC { $$ = Operator.OpType.Increment; }
1470 | OP_DEC { $$ = Operator.OpType.Decrement; }
1471 | TRUE { $$ = Operator.OpType.True; }
1472 | FALSE { $$ = Operator.OpType.False; }
1473 // Unary and binary:
1474 | PLUS { $$ = Operator.OpType.Addition; }
1475 | MINUS { $$ = Operator.OpType.Subtraction; }
1477 | STAR { $$ = Operator.OpType.Multiply; }
1478 | DIV { $$ = Operator.OpType.Division; }
1479 | PERCENT { $$ = Operator.OpType.Modulus; }
1480 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1481 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1482 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1483 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1484 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1485 | OP_EQ { $$ = Operator.OpType.Equality; }
1486 | OP_NE { $$ = Operator.OpType.Inequality; }
1487 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1488 | OP_LT { $$ = Operator.OpType.LessThan; }
1489 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1490 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1493 conversion_operator_declarator
1494 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
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 (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1503 null, null, lexer.Location);
1505 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1507 Parameter [] pars = new Parameter [1];
1509 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1511 current_local_parameters = new Parameters (pars, null, lexer.Location);
1513 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1514 null, null, lexer.Location);
1518 syntax_error (lexer.Location, "'operator' expected");
1522 syntax_error (lexer.Location, "'operator' expected");
1526 constructor_declaration
1529 constructor_declarator
1532 Constructor c = (Constructor) $3;
1533 c.Block = (Block) $4;
1534 c.OptAttributes = (Attributes) $1;
1535 c.ModFlags = (int) $2;
1537 if (c.Name == current_container.Basename){
1538 if ((c.ModFlags & Modifiers.STATIC) != 0){
1539 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1541 515, c.Location, String.Format (
1542 "`{0}.{1}': static constructor can not have access modifiers",
1543 c.Name, current_container.Name));
1546 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1548 if (c.Initializer != null){
1551 "Static constructors can not have an explicit this or base " +
1552 "constructor invocations");
1555 if (!c.Parameters.Empty){
1557 132, c.Location, "Static constructors should not have parameters");
1560 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1563 // We let another layer check the validity of the constructor.
1564 Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1567 CheckDef (current_container.AddConstructor (c), c.Name, c.Location);
1569 current_local_parameters = null;
1573 constructor_declarator
1575 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1577 oob_stack.Push (lexer.Location);
1579 current_local_parameters = (Parameters) $3;
1581 opt_constructor_initializer
1583 Location l = (Location) oob_stack.Pop ();
1584 $$ = new Constructor (current_container, (string) $1, 0, (Parameters) $3,
1585 (ConstructorInitializer) $6, l);
1591 | SEMICOLON { $$ = null; }
1594 opt_constructor_initializer
1595 : /* empty */ { $$ = null; }
1596 | constructor_initializer
1599 constructor_initializer
1600 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1602 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1604 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1606 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1609 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1615 : /* EMPTY */ { $$ = 0; }
1616 | UNSAFE { $$ = Modifiers.UNSAFE; }
1617 | EXTERN { $$ = Modifiers.EXTERN; }
1620 destructor_declaration
1621 : opt_attributes opt_finalizer TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1623 if ((string) $4 != current_container.Basename){
1624 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1625 } else if (!(current_container is Class)){
1626 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1628 Location l = lexer.Location;
1631 if (!RootContext.StdLib && current_container.Name == "System.Object")
1632 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
1634 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
1636 if ((m & Modifiers.UNSAFE) != 0){
1637 if (!RootContext.Unsafe){
1638 Report.Error (227, l,
1639 "Unsafe code requires the -unsafe command " +
1640 "line option to be specified");
1644 Method d = new Destructor (
1645 current_container, TypeManager.system_void_expr, m, "Finalize",
1646 new Parameters (null, null, l), (Attributes) $1, l);
1648 d.Block = (Block) $7;
1649 CheckDef (current_container.AddMethod (d), d.Name, d.Location);
1657 EVENT type variable_declarators SEMICOLON
1659 foreach (VariableDeclaration var in (ArrayList) $5) {
1661 Event e = new EventField (current_container, (Expression) $4, (int) $2, false, var.identifier,
1662 var.expression_or_array_initializer,
1663 (Attributes) $1, lexer.Location);
1665 CheckDef (current_container.AddEvent (e), e.Name, e.Location);
1671 EVENT type member_name
1674 implicit_value_parameter_type = (Expression) $4;
1675 lexer.EventParsing = true;
1676 oob_stack.Push (lexer.Location);
1678 event_accessor_declarations
1680 lexer.EventParsing = false;
1684 Location loc = (Location) oob_stack.Pop ();
1687 Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
1690 Pair pair = (Pair) $8;
1692 Event e = new EventProperty (current_container, (Expression) $4, (int) $2, false, (string) $5,
1693 null, (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
1696 CheckDef (current_container.AddEvent (e), e.Name, loc);
1697 implicit_value_parameter_type = null;
1700 | opt_attributes opt_modifiers EVENT type member_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS block {
1701 string mn = (string) $5;
1703 if (mn.IndexOf ('.') != -1)
1704 Report.Error (71, lexer.Location, "Explicit implementation of events requires property syntax");
1706 Report.Error (71, lexer.Location, "Event declaration should use property syntax");
1710 event_accessor_declarations
1711 : add_accessor_declaration remove_accessor_declaration
1713 $$ = new Pair ($1, $2);
1715 | remove_accessor_declaration add_accessor_declaration
1717 $$ = new Pair ($2, $1);
1719 | add_accessor_declaration { $$ = null; }
1720 | remove_accessor_declaration { $$ = null; }
1723 add_accessor_declaration
1724 : opt_attributes ADD
1726 Parameter [] args = new Parameter [1];
1727 Parameter implicit_value_parameter = new Parameter (
1728 implicit_value_parameter_type, "value",
1729 Parameter.Modifier.NONE, null);
1731 args [0] = implicit_value_parameter;
1733 current_local_parameters = new Parameters (args, null, lexer.Location);
1734 lexer.EventParsing = false;
1738 $$ = new Accessor ((Block) $4, (Attributes) $1);
1739 lexer.EventParsing = true;
1741 | opt_attributes ADD error {
1742 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1747 remove_accessor_declaration
1748 : opt_attributes REMOVE
1750 Parameter [] args = new Parameter [1];
1751 Parameter implicit_value_parameter = new Parameter (
1752 implicit_value_parameter_type, "value",
1753 Parameter.Modifier.NONE, null);
1755 args [0] = implicit_value_parameter;
1757 current_local_parameters = new Parameters (args, null, lexer.Location);
1758 lexer.EventParsing = false;
1762 $$ = new Accessor ((Block) $4, (Attributes) $1);
1763 lexer.EventParsing = true;
1765 | opt_attributes REMOVE error {
1766 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1772 : opt_attributes opt_modifiers indexer_declarator
1775 IndexerDeclaration decl = (IndexerDeclaration) $3;
1777 implicit_value_parameter_type = decl.type;
1779 lexer.PropertyParsing = true;
1780 parsing_indexer = true;
1782 indexer_parameters = decl.param_list;
1783 oob_stack.Push (lexer.Location);
1785 accessor_declarations
1787 lexer.PropertyParsing = false;
1788 parsing_indexer = false;
1792 // The signature is computed from the signature of the indexer. Look
1793 // at section 3.6 on the spec
1794 Location loc = (Location) oob_stack.Pop ();
1796 IndexerDeclaration decl = (IndexerDeclaration) $3;
1797 Pair pair = (Pair) $6;
1798 Accessor get_block = (Accessor) pair.First;
1799 Accessor set_block = (Accessor) pair.Second;
1801 indexer = new Indexer (current_container, decl.type, decl.interface_type,
1802 (int) $2, false, decl.param_list, (Attributes) $1,
1803 get_block, set_block, loc);
1805 // Note that there is no equivalent of CheckDef for this case
1806 // We shall handle this in semantic analysis
1808 current_container.AddIndexer (indexer);
1810 current_local_parameters = null;
1811 implicit_value_parameter_type = null;
1812 indexer_parameters = null;
1817 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1819 Parameters pars = (Parameters) $4;
1820 if (pars.HasArglist) {
1821 // "__arglist is not valid in this context"
1822 Report.Error_T (1669, lexer.Location);
1823 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
1824 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1827 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
1829 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1831 Parameters pars = (Parameters) $6;
1833 if (pars.HasArglist) {
1834 // "__arglist is not valid in this context"
1835 Report.Error_T (1669, lexer.Location);
1836 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
1837 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1839 $$ = new IndexerDeclaration ((Expression) $1, $2.ToString (), pars);
1851 Location enum_location = lexer.Location;
1853 string full_name = MakeName ((string) $4);
1854 Enum e = new Enum (current_namespace, current_container, (Expression) $5, (int) $2,
1855 full_name, (Attributes) $1, enum_location);
1857 foreach (VariableDeclaration ev in (ArrayList) $6) {
1858 Location loc = (Location) ev.Location;
1860 CheckDef (e.AddEnumMember (ev.identifier,
1861 (Expression) ev.expression_or_array_initializer,
1862 loc, ev.OptAttributes),
1863 ev.identifier, loc);
1866 CheckDef (current_container.AddEnum (e), full_name, enum_location);
1867 RootContext.Tree.RecordDecl (full_name, e);
1873 : /* empty */ { $$ = TypeManager.system_int32_expr; }
1874 | COLON type { $$ = $2; }
1878 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1884 opt_enum_member_declarations
1885 : /* empty */ { $$ = new ArrayList (4); }
1886 | enum_member_declarations opt_comma { $$ = $1; }
1889 enum_member_declarations
1890 : enum_member_declaration
1892 ArrayList l = new ArrayList (4);
1897 | enum_member_declarations COMMA enum_member_declaration
1899 ArrayList l = (ArrayList) $1;
1907 enum_member_declaration
1908 : opt_attributes IDENTIFIER
1910 $$ = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
1912 | opt_attributes IDENTIFIER
1914 $$ = lexer.Location;
1918 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
1922 delegate_declaration
1926 IDENTIFIER OPEN_PARENS
1927 opt_formal_parameter_list
1931 Location l = lexer.Location;
1932 string name = MakeName ((string) $5);
1933 Delegate del = new Delegate (current_namespace, current_container, (Expression) $4,
1934 (int) $2, name, (Parameters) $7, (Attributes) $1, l);
1936 CheckDef (current_container.AddDelegate (del), del.Name, l);
1937 RootContext.Tree.RecordDecl (name, del);
1942 IDENTIFIER OPEN_PARENS
1943 opt_formal_parameter_list
1947 Location l = lexer.Location;
1948 string name = MakeName ((string) $5);
1949 Delegate del = new Delegate (
1950 current_namespace, current_container,
1951 TypeManager.system_void_expr, (int) $2, name,
1952 (Parameters) $7, (Attributes) $1, l);
1954 CheckDef (current_container.AddDelegate (del), del.Name, l);
1955 RootContext.Tree.RecordDecl (name, del);
1960 : namespace_or_type_name
1963 namespace_or_type_name
1964 : IDENTIFIER { $$ = new SimpleName ((string) $1, lexer.Location);}
1965 | namespace_or_type_name DOT IDENTIFIER {
1966 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1971 * Before you think of adding a return_type, notice that we have been
1972 * using two rules in the places where it matters (one rule using type
1973 * and another identical one that uses VOID as the return type). This
1974 * gets rid of a shift/reduce couple
1977 : type_name { /* class_type */
1979 This does interfaces, delegates, struct_types, class_types,
1980 parent classes, and more! 4.2
1993 // Note that here only unmanaged types are allowed but we
1994 // can't perform checks during this phase - we do it during
1995 // semantic analysis.
1997 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2001 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2007 | non_expression_type rank_specifier
2009 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2011 | non_expression_type STAR
2013 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2015 | expression rank_specifiers
2017 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2021 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2025 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2026 // through this different path
2028 | multiplicative_expression STAR
2030 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2037 ArrayList types = new ArrayList (4);
2042 | type_list COMMA type
2044 ArrayList types = (ArrayList) $1;
2052 * replaces all the productions for isolating the various
2053 * simple types, but we need this to reuse it easily in local_variable_type
2056 : OBJECT { $$ = TypeManager.system_object_expr; }
2057 | STRING { $$ = TypeManager.system_string_expr; }
2058 | BOOL { $$ = TypeManager.system_boolean_expr; }
2059 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2060 | FLOAT { $$ = TypeManager.system_single_expr; }
2061 | DOUBLE { $$ = TypeManager.system_double_expr; }
2066 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2067 | BYTE { $$ = TypeManager.system_byte_expr; }
2068 | SHORT { $$ = TypeManager.system_int16_expr; }
2069 | USHORT { $$ = TypeManager.system_uint16_expr; }
2070 | INT { $$ = TypeManager.system_int32_expr; }
2071 | UINT { $$ = TypeManager.system_uint32_expr; }
2072 | LONG { $$ = TypeManager.system_int64_expr; }
2073 | ULONG { $$ = TypeManager.system_uint64_expr; }
2074 | CHAR { $$ = TypeManager.system_char_expr; }
2075 | VOID { $$ = TypeManager.system_void_expr; }
2083 : type rank_specifiers
2085 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2090 // Expressions, section 7.5
2098 | IDENTIFIER { $$ = new SimpleName ((string) $1, lexer.Location); }
2099 | parenthesized_expression
2101 | invocation_expression
2105 | post_increment_expression
2106 | post_decrement_expression
2110 | checked_expression
2111 | unchecked_expression
2112 | pointer_member_access
2113 | anonymous_method_expression
2120 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2121 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2122 | NULL { $$ = NullLiteral.Null; }
2126 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2127 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2128 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2133 object v = lexer.Value;
2139 $$ = IntLiteral.Zero;
2141 $$ = IntLiteral.One;
2143 $$ = new IntLiteral (i);
2144 } else if (v is uint)
2145 $$ = new UIntLiteral ((UInt32) v);
2147 $$ = new LongLiteral ((Int64) v);
2148 else if (v is ulong)
2149 $$ = new ULongLiteral ((UInt64) v);
2151 Console.WriteLine ("OOPS. Unexpected result from scanner");
2156 : TRUE { $$ = new BoolLiteral (true); }
2157 | FALSE { $$ = new BoolLiteral (false); }
2160 parenthesized_expression_0
2161 : OPEN_PARENS expression CLOSE_PARENS
2164 lexer.Deambiguate_CloseParens ();
2165 // After this, the next token returned is one of
2166 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2167 // or CLOSE_PARENS_MINUS.
2171 parenthesized_expression
2172 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2176 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2178 // If a parenthesized expression is followed by a minus, we need to wrap
2179 // the expression inside a ParenthesizedExpression for the CS0075 check
2180 // in Binary.DoResolve().
2181 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2186 : primary_expression DOT IDENTIFIER
2188 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2190 | predefined_type DOT IDENTIFIER
2192 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2200 invocation_expression
2201 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2204 Location l = lexer.Location;
2205 Report.Error (1, l, "Parse error");
2207 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2209 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2211 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2213 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2215 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2220 : /* empty */ { $$ = null; }
2227 ArrayList list = new ArrayList (4);
2231 | argument_list COMMA argument
2233 ArrayList list = (ArrayList) $1;
2237 | argument_list error {
2238 CheckToken (1026, yyToken, ", or ) expected");
2245 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2247 | REF variable_reference
2249 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2251 | OUT variable_reference
2253 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2255 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2257 ArrayList list = (ArrayList) $3;
2258 Argument[] args = new Argument [list.Count];
2259 list.CopyTo (args, 0);
2261 Expression expr = new Arglist (args, lexer.Location);
2262 $$ = new Argument (expr, Argument.AType.Expression);
2266 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2271 : expression { note ("section 5.4"); $$ = $1; }
2275 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2277 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2279 | primary_expression rank_specifiers
2281 // So the super-trick is that primary_expression
2282 // can only be either a SimpleName or a MemberAccess.
2283 // The MemberAccess case arises when you have a fully qualified type-name like :
2285 // SimpleName is when you have
2288 Expression expr = (Expression) $1;
2289 if (expr is ComposedCast){
2290 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2291 } else if (!(expr is SimpleName || expr is MemberAccess)){
2292 Error_ExpectingTypeName (lexer.Location, expr);
2293 $$ = TypeManager.system_object_expr;
2296 // So we extract the string corresponding to the SimpleName
2299 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2307 ArrayList list = new ArrayList (4);
2311 | expression_list COMMA expression
2313 ArrayList list = (ArrayList) $1;
2322 $$ = new This (current_block, lexer.Location);
2327 : BASE DOT IDENTIFIER
2329 $$ = new BaseAccess ((string) $3, lexer.Location);
2331 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2333 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2336 Report.Error (175, "Use of keyword `base' is not valid in this context");
2341 post_increment_expression
2342 : primary_expression OP_INC
2344 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2345 (Expression) $1, lexer.Location);
2349 post_decrement_expression
2350 : primary_expression OP_DEC
2352 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2353 (Expression) $1, lexer.Location);
2358 : object_or_delegate_creation_expression
2359 | array_creation_expression
2362 object_or_delegate_creation_expression
2363 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2365 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2369 array_creation_expression
2370 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2372 opt_array_initializer
2374 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2376 | NEW type rank_specifiers array_initializer
2378 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2382 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2398 : rank_specifier opt_rank_specifier
2400 $$ = (string) $2 + (string) $1;
2405 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2407 $$ = "[" + (string) $2 + "]";
2427 | dim_separators COMMA
2429 $$ = (string) $1 + ",";
2433 opt_array_initializer
2445 : OPEN_BRACE CLOSE_BRACE
2447 ArrayList list = new ArrayList (4);
2450 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2452 $$ = (ArrayList) $2;
2456 variable_initializer_list
2457 : variable_initializer
2459 ArrayList list = new ArrayList (4);
2463 | variable_initializer_list COMMA variable_initializer
2465 ArrayList list = (ArrayList) $1;
2472 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2474 $$ = new TypeOfVoid (lexer.Location);
2476 | TYPEOF OPEN_PARENS type CLOSE_PARENS
2478 $$ = new TypeOf ((Expression) $3, lexer.Location);
2483 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2484 $$ = new SizeOf ((Expression) $3, lexer.Location);
2489 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2491 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2495 unchecked_expression
2496 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2498 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2502 pointer_member_access
2503 : primary_expression OP_PTR IDENTIFIER
2507 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2508 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2512 anonymous_method_expression
2513 : DELEGATE opt_anonymous_method_signature {
2514 oob_stack.Push (current_local_parameters);
2515 current_local_parameters = (Parameters)$2;
2516 create_toplevel_block = true;
2519 Report.Error (-213, lexer.Location, "Anonymous methods are not supported in this branch");
2522 create_toplevel_block = false;
2523 if (!RootContext.V2){
2524 Report.Error (-213, lexer.Location, "Anonymous methods are only supported in V2");
2527 $$ = new AnonymousMethod ((Parameters) $2, (Block) $4, lexer.Location);
2528 current_local_parameters = (Parameters) oob_stack.Pop ();
2533 opt_anonymous_method_signature
2534 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
2535 | anonymous_method_signature
2538 anonymous_method_signature
2539 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
2542 $$ = Parameters.EmptyReadOnlyParameters;
2544 ArrayList par_list = (ArrayList) $2;
2545 Parameter [] pars = new Parameter [par_list.Count];
2546 par_list.CopyTo (pars);
2547 $$ = new Parameters (pars, null, lexer.Location);
2552 opt_anonymous_method_parameter_list
2553 : /* empty */ { $$ = null; }
2554 | anonymous_method_parameter_list { $$ = $1; }
2557 anonymous_method_parameter_list
2558 : anonymous_method_parameter
2560 ArrayList a = new ArrayList (4);
2564 | anonymous_method_parameter_list COMMA anonymous_method_parameter
2566 ArrayList a = (ArrayList) $1;
2572 anonymous_method_parameter
2573 : opt_parameter_modifier type IDENTIFIER {
2574 $$ = new Parameter ((Expression) $2, (string) $2, (Parameter.Modifier) $1, null);
2579 : primary_expression
2580 | BANG prefixed_unary_expression
2582 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2584 | TILDE prefixed_unary_expression
2586 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2592 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
2594 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2596 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
2598 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2604 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2606 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2611 // The idea to split this out is from Rhys' grammar
2612 // to solve the problem with casts.
2614 prefixed_unary_expression
2616 | PLUS prefixed_unary_expression
2618 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2620 | MINUS prefixed_unary_expression
2622 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2624 | OP_INC prefixed_unary_expression
2626 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2627 (Expression) $2, lexer.Location);
2629 | OP_DEC prefixed_unary_expression
2631 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2632 (Expression) $2, lexer.Location);
2634 | STAR prefixed_unary_expression
2636 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2638 | BITWISE_AND prefixed_unary_expression
2640 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2644 pre_increment_expression
2645 : OP_INC prefixed_unary_expression
2647 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2648 (Expression) $2, lexer.Location);
2652 pre_decrement_expression
2653 : OP_DEC prefixed_unary_expression
2655 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2656 (Expression) $2, lexer.Location);
2660 multiplicative_expression
2661 : prefixed_unary_expression
2662 | multiplicative_expression STAR prefixed_unary_expression
2664 $$ = new Binary (Binary.Operator.Multiply,
2665 (Expression) $1, (Expression) $3, lexer.Location);
2667 | multiplicative_expression DIV prefixed_unary_expression
2669 $$ = new Binary (Binary.Operator.Division,
2670 (Expression) $1, (Expression) $3, lexer.Location);
2672 | multiplicative_expression PERCENT prefixed_unary_expression
2674 $$ = new Binary (Binary.Operator.Modulus,
2675 (Expression) $1, (Expression) $3, lexer.Location);
2680 : multiplicative_expression
2681 | additive_expression PLUS multiplicative_expression
2683 $$ = new Binary (Binary.Operator.Addition,
2684 (Expression) $1, (Expression) $3, lexer.Location);
2686 | additive_expression MINUS multiplicative_expression
2688 $$ = new Binary (Binary.Operator.Subtraction,
2689 (Expression) $1, (Expression) $3, lexer.Location);
2694 : additive_expression
2695 | shift_expression OP_SHIFT_LEFT additive_expression
2697 $$ = new Binary (Binary.Operator.LeftShift,
2698 (Expression) $1, (Expression) $3, lexer.Location);
2700 | shift_expression OP_SHIFT_RIGHT additive_expression
2702 $$ = new Binary (Binary.Operator.RightShift,
2703 (Expression) $1, (Expression) $3, lexer.Location);
2707 relational_expression
2709 | relational_expression OP_LT shift_expression
2711 $$ = new Binary (Binary.Operator.LessThan,
2712 (Expression) $1, (Expression) $3, lexer.Location);
2714 | relational_expression OP_GT shift_expression
2716 $$ = new Binary (Binary.Operator.GreaterThan,
2717 (Expression) $1, (Expression) $3, lexer.Location);
2719 | relational_expression OP_LE shift_expression
2721 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2722 (Expression) $1, (Expression) $3, lexer.Location);
2724 | relational_expression OP_GE shift_expression
2726 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2727 (Expression) $1, (Expression) $3, lexer.Location);
2729 | relational_expression IS type
2731 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
2733 | relational_expression AS type
2735 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
2740 : relational_expression
2741 | equality_expression OP_EQ relational_expression
2743 $$ = new Binary (Binary.Operator.Equality,
2744 (Expression) $1, (Expression) $3, lexer.Location);
2746 | equality_expression OP_NE relational_expression
2748 $$ = new Binary (Binary.Operator.Inequality,
2749 (Expression) $1, (Expression) $3, lexer.Location);
2754 : equality_expression
2755 | and_expression BITWISE_AND equality_expression
2757 $$ = new Binary (Binary.Operator.BitwiseAnd,
2758 (Expression) $1, (Expression) $3, lexer.Location);
2762 exclusive_or_expression
2764 | exclusive_or_expression CARRET and_expression
2766 $$ = new Binary (Binary.Operator.ExclusiveOr,
2767 (Expression) $1, (Expression) $3, lexer.Location);
2771 inclusive_or_expression
2772 : exclusive_or_expression
2773 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2775 $$ = new Binary (Binary.Operator.BitwiseOr,
2776 (Expression) $1, (Expression) $3, lexer.Location);
2780 conditional_and_expression
2781 : inclusive_or_expression
2782 | conditional_and_expression OP_AND inclusive_or_expression
2784 $$ = new Binary (Binary.Operator.LogicalAnd,
2785 (Expression) $1, (Expression) $3, lexer.Location);
2789 conditional_or_expression
2790 : conditional_and_expression
2791 | conditional_or_expression OP_OR conditional_and_expression
2793 $$ = new Binary (Binary.Operator.LogicalOr,
2794 (Expression) $1, (Expression) $3, lexer.Location);
2798 conditional_expression
2799 : conditional_or_expression
2800 | conditional_or_expression INTERR expression COLON expression
2802 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2806 assignment_expression
2807 : prefixed_unary_expression ASSIGN expression
2809 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2811 | prefixed_unary_expression OP_MULT_ASSIGN expression
2813 Location l = lexer.Location;
2815 $$ = new CompoundAssign (
2816 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
2818 | prefixed_unary_expression OP_DIV_ASSIGN expression
2820 Location l = lexer.Location;
2822 $$ = new CompoundAssign (
2823 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
2825 | prefixed_unary_expression OP_MOD_ASSIGN expression
2827 Location l = lexer.Location;
2829 $$ = new CompoundAssign (
2830 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
2832 | prefixed_unary_expression OP_ADD_ASSIGN expression
2834 Location l = lexer.Location;
2836 $$ = new CompoundAssign (
2837 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
2839 | prefixed_unary_expression OP_SUB_ASSIGN expression
2841 Location l = lexer.Location;
2843 $$ = new CompoundAssign (
2844 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
2846 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2848 Location l = lexer.Location;
2850 $$ = new CompoundAssign (
2851 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
2853 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2855 Location l = lexer.Location;
2857 $$ = new CompoundAssign (
2858 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
2860 | prefixed_unary_expression OP_AND_ASSIGN expression
2862 Location l = lexer.Location;
2864 $$ = new CompoundAssign (
2865 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
2867 | prefixed_unary_expression OP_OR_ASSIGN expression
2869 Location l = lexer.Location;
2871 $$ = new CompoundAssign (
2872 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
2874 | prefixed_unary_expression OP_XOR_ASSIGN expression
2876 Location l = lexer.Location;
2878 $$ = new CompoundAssign (
2879 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
2884 : conditional_expression
2885 | assignment_expression
2907 name = MakeName ((string) $4);
2909 new_class = new Class (current_namespace, current_container, name, (int) $2,
2910 (Attributes) $1, lexer.Location);
2911 current_container = new_class;
2912 RootContext.Tree.RecordDecl (name, new_class);
2918 Class new_class = (Class) current_container;
2921 if (new_class.Name == "System.Object") {
2922 Report.Error (537, new_class.Location, "The class System.Object cannot have a base class or implement an interface.");
2924 new_class.Bases = (ArrayList) $6;
2927 current_container = current_container.Parent;
2928 CheckDef (current_container.AddClass (new_class), new_class.Name, new_class.Location);
2935 : /* empty */ { $$ = (int) 0; }
2941 | modifiers modifier
2946 if ((m1 & m2) != 0) {
2947 Location l = lexer.Location;
2948 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2950 $$ = (int) (m1 | m2);
2955 : NEW { $$ = Modifiers.NEW; }
2956 | PUBLIC { $$ = Modifiers.PUBLIC; }
2957 | PROTECTED { $$ = Modifiers.PROTECTED; }
2958 | INTERNAL { $$ = Modifiers.INTERNAL; }
2959 | PRIVATE { $$ = Modifiers.PRIVATE; }
2960 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2961 | SEALED { $$ = Modifiers.SEALED; }
2962 | STATIC { $$ = Modifiers.STATIC; }
2963 | READONLY { $$ = Modifiers.READONLY; }
2964 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2965 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2966 | EXTERN { $$ = Modifiers.EXTERN; }
2967 | VOLATILE { $$ = Modifiers.VOLATILE; }
2968 | UNSAFE { $$ = Modifiers.UNSAFE; }
2972 : /* empty */ { $$ = null; }
2973 | class_base { $$ = $1; }
2977 : COLON type_list { $$ = $2; }
2985 // A block is "contained" on the following places:
2987 // property_declaration as part of the accessor body (get/set)
2988 // operator_declaration
2989 // constructor_declaration
2990 // destructor_declaration
2991 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2996 if (current_block == null || create_toplevel_block){
2997 current_block = new ToplevelBlock (current_local_parameters, lexer.Location);
2999 current_block = new Block (current_block, current_local_parameters,
3000 lexer.Location, Location.Null);
3003 opt_statement_list CLOSE_BRACE
3005 while (current_block.Implicit)
3006 current_block = current_block.Parent;
3008 current_block.SetEndLocation (lexer.Location);
3009 current_block = current_block.Parent;
3020 | statement_list statement
3024 : declaration_statement
3026 if ($1 != null && (Block) $1 != current_block){
3027 current_block.AddStatement ((Statement) $1);
3028 current_block = (Block) $1;
3031 | valid_declaration_statement
3033 current_block.AddStatement ((Statement) $1);
3038 valid_declaration_statement
3041 | expression_statement
3042 | selection_statement
3043 | iteration_statement
3047 | unchecked_statement
3055 : valid_declaration_statement
3056 | declaration_statement
3058 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3063 Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3071 $$ = EmptyStatement.Value;
3078 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3080 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3081 current_block.AddStatement (labeled);
3086 declaration_statement
3087 : local_variable_declaration SEMICOLON
3090 DictionaryEntry de = (DictionaryEntry) $1;
3092 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3096 | local_constant_declaration SEMICOLON
3099 DictionaryEntry de = (DictionaryEntry) $1;
3101 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3107 * The following is from Rhys' grammar:
3108 * > Types in local variable declarations must be recognized as
3109 * > expressions to prevent reduce/reduce errors in the grammar.
3110 * > The expressions are converted into types during semantic analysis.
3113 : primary_expression opt_rank_specifier
3115 // FIXME: Do something smart here regarding the composition of the type.
3117 // Ok, the above "primary_expression" is there to get rid of
3118 // both reduce/reduce and shift/reduces in the grammar, it should
3119 // really just be "type_name". If you use type_name, a reduce/reduce
3120 // creeps up. If you use namespace_or_type_name (which is all we need
3121 // really) two shift/reduces appear.
3124 // So the super-trick is that primary_expression
3125 // can only be either a SimpleName or a MemberAccess.
3126 // The MemberAccess case arises when you have a fully qualified type-name like :
3128 // SimpleName is when you have
3131 Expression expr = (Expression) $1;
3132 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3133 Error_ExpectingTypeName (lexer.Location, expr);
3137 // So we extract the string corresponding to the SimpleName
3141 if ((string) $2 == "")
3144 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3147 | builtin_types opt_rank_specifier
3149 if ((string) $2 == "")
3152 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3156 local_variable_pointer_type
3157 : primary_expression STAR
3159 Expression expr = (Expression) $1;
3160 Location l = lexer.Location;
3162 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3163 Error_ExpectingTypeName (l, expr);
3167 $$ = new ComposedCast ((Expression) $1, "*", l);
3169 | builtin_types STAR
3171 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3175 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3177 | local_variable_pointer_type STAR
3179 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3183 local_variable_declaration
3184 : local_variable_type variable_declarators
3187 $$ = new DictionaryEntry ($1, $2);
3191 | local_variable_pointer_type opt_rank_specifier variable_declarators
3196 if ((string) $2 == "")
3197 t = (Expression) $1;
3199 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3200 $$ = new DictionaryEntry (t, $3);
3206 local_constant_declaration
3207 : CONST local_variable_type constant_declarators
3210 $$ = new DictionaryEntry ($2, $3);
3216 expression_statement
3217 : statement_expression SEMICOLON
3224 // We have to do the wrapping here and not in the case above,
3225 // because statement_expression is used for example in for_statement
3227 statement_expression
3228 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3229 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3230 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3231 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3232 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3233 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3234 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3236 Report.Error (1002, lexer.Location, "Expecting `;'");
3241 object_creation_expression
3242 : object_or_delegate_creation_expression
3243 { note ("complain if this is a delegate maybe?"); }
3252 : if_statement_open if_statement_rest
3261 oob_stack.Push (lexer.Location);
3266 : boolean_expression CLOSE_PARENS
3269 Location l = (Location) oob_stack.Pop ();
3271 $$ = new If ((Expression) $1, (Statement) $3, l);
3273 if (RootContext.WarningLevel >= 3){
3274 if ($3 == EmptyStatement.Value)
3275 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3279 | boolean_expression CLOSE_PARENS
3280 embedded_statement ELSE embedded_statement
3282 Location l = (Location) oob_stack.Pop ();
3284 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
3289 : SWITCH OPEN_PARENS
3291 oob_stack.Push (lexer.Location);
3292 switch_stack.Push (current_block);
3294 expression CLOSE_PARENS
3297 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
3298 current_block = (Block) switch_stack.Pop ();
3314 Report.Error (1522, lexer.Location, "Empty switch block");
3322 ArrayList sections = new ArrayList (4);
3327 | switch_sections switch_section
3329 ArrayList sections = (ArrayList) $1;
3339 current_block = current_block.CreateSwitchBlock (lexer.Location);
3343 Block topmost = current_block;
3345 while (topmost.Implicit)
3346 topmost = topmost.Parent;
3347 $$ = new SwitchSection ((ArrayList) $1, topmost);
3354 ArrayList labels = new ArrayList (4);
3359 | switch_labels switch_label
3361 ArrayList labels = (ArrayList) ($1);
3369 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3370 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3373 1523, lexer.Location,
3374 "The keyword case or default must precede code in switch block");
3388 oob_stack.Push (lexer.Location);
3390 boolean_expression CLOSE_PARENS embedded_statement
3392 Location l = (Location) oob_stack.Pop ();
3393 $$ = new While ((Expression) $4, (Statement) $6, l);
3395 if (RootContext.WarningLevel >= 3){
3396 if ($6 == EmptyStatement.Value)
3397 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3403 : DO embedded_statement
3405 oob_stack.Push (lexer.Location);
3407 boolean_expression CLOSE_PARENS SEMICOLON
3409 Location l = (Location) oob_stack.Pop ();
3411 $$ = new Do ((Statement) $2, (Expression) $6, l);
3417 opt_for_initializer SEMICOLON
3419 Block assign_block = new Block (current_block);
3420 current_block = assign_block;
3422 if ($3 is DictionaryEntry){
3423 DictionaryEntry de = (DictionaryEntry) $3;
3425 Expression type = (Expression) de.Key;
3426 ArrayList var_declarators = (ArrayList) de.Value;
3428 foreach (VariableDeclaration decl in var_declarators){
3432 vi = current_block.AddVariable (
3433 type, decl.identifier, current_local_parameters, decl.Location);
3437 Location l = lexer.Location;
3439 if (decl.expression_or_array_initializer is Expression){
3440 expr = (Expression) decl.expression_or_array_initializer;
3441 } else if (decl.expression_or_array_initializer == null) {
3444 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3445 expr = new ArrayCreation (type, "", init, decl.Location);
3448 LocalVariableReference var;
3449 var = new LocalVariableReference (assign_block, decl.identifier, l);
3452 Assign a = new Assign (var, expr, decl.Location);
3454 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3460 oob_stack.Push (lexer.Location);
3462 opt_for_condition SEMICOLON
3463 opt_for_iterator CLOSE_PARENS
3466 Location l = (Location) oob_stack.Pop ();
3468 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3470 if (RootContext.WarningLevel >= 3){
3471 if ($10 == EmptyStatement.Value)
3472 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3475 current_block.AddStatement (f);
3476 while (current_block.Implicit)
3477 current_block = current_block.Parent;
3479 current_block = current_block.Parent;
3484 : /* empty */ { $$ = EmptyStatement.Value; }
3489 : local_variable_declaration
3490 | statement_expression_list
3494 : /* empty */ { $$ = null; }
3495 | boolean_expression
3499 : /* empty */ { $$ = EmptyStatement.Value; }
3504 : statement_expression_list
3507 statement_expression_list
3508 : statement_expression
3510 // CHANGE: was `null'
3511 Block b = new Block (current_block, Block.Flags.Implicit);
3513 b.AddStatement ((Statement) $1);
3516 | statement_expression_list COMMA statement_expression
3518 Block b = (Block) $1;
3520 b.AddStatement ((Statement) $3);
3526 : FOREACH OPEN_PARENS type IDENTIFIER IN
3528 oob_stack.Push (lexer.Location);
3530 expression CLOSE_PARENS
3532 oob_stack.Push (current_block);
3534 Block foreach_block = new Block (current_block);
3535 LocalVariableReference v = null;
3536 Location l = lexer.Location;
3539 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3543 // Get a writable reference to this read-only variable.
3544 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3546 current_block = foreach_block;
3549 oob_stack.Push (current_block);
3553 Block foreach_block = (Block) oob_stack.Pop ();
3554 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3555 Block prev_block = (Block) oob_stack.Pop ();
3556 Location l = (Location) oob_stack.Pop ();
3558 current_block = prev_block;
3561 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3562 foreach_block.AddStatement (f);
3571 | continue_statement
3581 $$ = new Break (lexer.Location);
3586 : CONTINUE SEMICOLON
3588 $$ = new Continue (lexer.Location);
3593 : GOTO IDENTIFIER SEMICOLON
3595 $$ = new Goto (current_block, (string) $2, lexer.Location);
3597 | GOTO CASE constant_expression SEMICOLON
3599 $$ = new GotoCase ((Expression) $3, lexer.Location);
3601 | GOTO DEFAULT SEMICOLON
3603 $$ = new GotoDefault (lexer.Location);
3608 : RETURN opt_expression SEMICOLON
3610 $$ = new Return ((Expression) $2, lexer.Location);
3615 : THROW opt_expression SEMICOLON
3617 $$ = new Throw ((Expression) $2, lexer.Location);
3622 : IDENTIFIER RETURN expression SEMICOLON
3624 string s = (string) $1;
3626 Report.Error (1003, lexer.Location, "; expected");
3629 if (!RootContext.V2){
3630 Report.Error (-222, lexer.Location, "yield statement only available in C# 2.0 mode");
3633 if (iterator_container == null){
3634 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3637 iterator_container.SetYields ();
3638 $$ = new Yield ((Expression) $3, lexer.Location);
3641 | IDENTIFIER BREAK SEMICOLON
3643 string s = (string) $1;
3645 Report.Error (1003, lexer.Location, "; expected");
3648 if (!RootContext.V2){
3649 Report.Error (-222, lexer.Location, "yield statement only available in C# 2.0 mode");
3652 if (iterator_container == null){
3653 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3656 iterator_container.SetYields ();
3657 $$ = new YieldBreak (lexer.Location);
3668 : TRY block catch_clauses
3671 ArrayList s = new ArrayList (4);
3673 foreach (Catch cc in (ArrayList) $3) {
3680 // Now s contains the list of specific catch clauses
3681 // and g contains the general one.
3683 $$ = new Try ((Block) $2, s, g, null, lexer.Location);
3685 | TRY block opt_catch_clauses FINALLY block
3688 ArrayList s = new ArrayList (4);
3689 ArrayList catch_list = (ArrayList) $3;
3691 if (catch_list != null){
3692 foreach (Catch cc in catch_list) {
3700 $$ = new Try ((Block) $2, s, g, (Block) $5, lexer.Location);
3704 Report.Error (1524, lexer.Location, "Expected catch or finally");
3709 : /* empty */ { $$ = null; }
3716 ArrayList l = new ArrayList (4);
3721 | catch_clauses catch_clause
3723 ArrayList l = (ArrayList) $1;
3731 : /* empty */ { $$ = null; }
3736 : CATCH opt_catch_args
3738 Expression type = null;
3742 DictionaryEntry cc = (DictionaryEntry) $2;
3743 type = (Expression) cc.Key;
3744 id = (string) cc.Value;
3747 ArrayList one = new ArrayList (4);
3748 Location loc = lexer.Location;
3750 one.Add (new VariableDeclaration (id, null, loc));
3753 current_block = new Block (current_block);
3754 Block b = declare_local_variables (type, one, loc);
3759 Expression type = null;
3763 DictionaryEntry cc = (DictionaryEntry) $2;
3764 type = (Expression) cc.Key;
3765 id = (string) cc.Value;
3769 // FIXME: I can change this for an assignment.
3771 while (current_block != (Block) $1)
3772 current_block = current_block.Parent;
3777 $$ = new Catch (type, id , (Block) $4, lexer.Location);
3782 : /* empty */ { $$ = null; }
3787 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3789 $$ = new DictionaryEntry ($2, $3);
3796 $$ = new Checked ((Block) $2);
3803 $$ = new Unchecked ((Block) $2);
3810 if (!RootContext.Unsafe){
3811 Report.Error (227, lexer.Location,
3812 "Unsafe code can only be used if --unsafe is used");
3815 $$ = new Unsafe ((Block) $3);
3821 type fixed_pointer_declarators
3824 ArrayList list = (ArrayList) $4;
3825 Expression type = (Expression) $3;
3826 Location l = lexer.Location;
3827 int top = list.Count;
3829 Block assign_block = new Block (current_block);
3830 current_block = assign_block;
3832 for (int i = 0; i < top; i++){
3833 Pair p = (Pair) list [i];
3836 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3848 Location l = (Location) oob_stack.Pop ();
3850 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
3852 if (RootContext.WarningLevel >= 3){
3853 if ($7 == EmptyStatement.Value)
3854 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3857 current_block.AddStatement (f);
3858 while (current_block.Implicit)
3859 current_block = current_block.Parent;
3861 current_block = current_block.Parent;
3865 fixed_pointer_declarators
3866 : fixed_pointer_declarator {
3867 ArrayList declarators = new ArrayList (4);
3869 declarators.Add ($1);
3872 | fixed_pointer_declarators COMMA fixed_pointer_declarator
3874 ArrayList declarators = (ArrayList) $1;
3876 declarators.Add ($3);
3881 fixed_pointer_declarator
3882 : IDENTIFIER ASSIGN expression
3884 $$ = new Pair ($1, $3);
3888 // You must provide an initializer in a fixed or using statement declaration
3889 Report.Error_T (210, lexer.Location);
3895 : LOCK OPEN_PARENS expression CLOSE_PARENS
3901 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
3906 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
3908 Block assign_block = new Block (current_block);
3909 current_block = assign_block;
3911 oob_stack.Push (lexer.Location);
3913 if ($3 is DictionaryEntry){
3914 DictionaryEntry de = (DictionaryEntry) $3;
3915 Location l = lexer.Location;
3917 Expression type = (Expression) de.Key;
3918 ArrayList var_declarators = (ArrayList) de.Value;
3920 ArrayList vars = new ArrayList (4);
3922 foreach (VariableDeclaration decl in var_declarators){
3924 LocalInfo vi = current_block.AddVariable (
3925 type, decl.identifier,
3926 current_local_parameters, decl.Location);
3932 if (decl.expression_or_array_initializer is Expression){
3933 expr = (Expression) decl.expression_or_array_initializer;
3935 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3937 // You must provide an initializer in a fixed or using statement declaration
3938 Report.Error_T (210, l);
3941 expr = new ArrayCreation (type, "", init, decl.Location);
3944 LocalVariableReference var;
3946 // Get a writable reference to this read-only variable.
3947 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
3949 // This is so that it is not a warning on using variables
3952 vars.Add (new DictionaryEntry (var, expr));
3954 // Assign a = new Assign (var, expr, decl.Location);
3955 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3957 $3 = new DictionaryEntry (type, vars);
3962 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
3963 current_block.AddStatement (u);
3964 while (current_block.Implicit)
3965 current_block = current_block.Parent;
3967 current_block = current_block.Parent;
3971 resource_acquisition
3972 : local_variable_declaration
3979 // A class used to pass around variable declarations and constants
3981 public class VariableDeclaration {
3982 public string identifier;
3983 public object expression_or_array_initializer;
3984 public Location Location;
3985 public Attributes OptAttributes;
3987 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
3989 this.identifier = id;
3990 this.expression_or_array_initializer = eoai;
3992 this.OptAttributes = opt_attrs;
3995 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4001 /// Used to pass around interface property information
4003 public class InterfaceAccessorInfo {
4005 public readonly Accessor Get, Set;
4007 public InterfaceAccessorInfo (bool has_get, bool has_set,
4008 Attributes get_attrs, Attributes set_attrs)
4011 Get = new Accessor (null, get_attrs);
4013 Set = new Accessor (null, set_attrs);
4019 // A class used to hold info about an indexer declarator
4021 public class IndexerDeclaration {
4022 public Expression type;
4023 public string interface_type;
4024 public Parameters param_list;
4026 public IndexerDeclaration (Expression type, string interface_type, Parameters param_list)
4029 this.interface_type = interface_type;
4030 this.param_list = param_list;
4035 // We use this when we do not have an object in advance that is an IIteratorContainer
4037 public class SimpleIteratorContainer : IIteratorContainer {
4040 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4045 public static SimpleIteratorContainer GetSimple () {
4046 Simple.Yields = false;
4050 public void SetYields () { Yields = true; }
4054 // A class used to hold info about an operator declarator
4056 public class OperatorDeclaration {
4057 public Operator.OpType optype;
4058 public Expression ret_type, arg1type, arg2type;
4059 public string arg1name, arg2name;
4060 public Location location;
4062 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4063 Expression arg1type, string arg1name,
4064 Expression arg2type, string arg2name, Location location)
4067 this.ret_type = ret_type;
4068 this.arg1type = arg1type;
4069 this.arg1name = arg1name;
4070 this.arg2type = arg2type;
4071 this.arg2name = arg2name;
4072 this.location = location;
4077 void Error_ExpectingTypeName (Location l, Expression expr)
4079 if (expr is Invocation){
4080 Report.Error (1002, l, "; expected");
4082 Report.Error (-1, l, "Invalid Type definition");
4087 // Given the @class_name name, it creates a fully qualified name
4088 // based on the containing declaration space
4091 MakeName (string class_name)
4093 string ns = current_namespace.FullName;
4094 string container_name = current_container.Name;
4096 if (container_name == ""){
4098 return ns + "." + class_name;
4102 return container_name + "." + class_name;
4106 // Used to report back to the user the result of a declaration
4107 // in the current declaration space
4110 CheckDef (DeclSpace.AdditionResult result, string name, Location l)
4112 if (result == DeclSpace.AdditionResult.Success)
4116 case DeclSpace.AdditionResult.NameExists:
4117 Report.Error (102, l, "The container `" + current_container.Name +
4118 "' already contains a definition for `"+
4124 // This is handled only for static Constructors, because
4125 // in reality we handle these by the semantic analysis later
4127 case DeclSpace.AdditionResult.MethodExists:
4129 111, l, "Class `"+current_container.Name+
4130 "' already defines a member called '" +
4131 name + "' with the same parameter types (more than one default constructor)");
4134 case DeclSpace.AdditionResult.EnclosingClash:
4135 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
4138 case DeclSpace.AdditionResult.NotAConstructor:
4139 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
4142 case DeclSpace.AdditionResult.Error:
4143 // Error has already been reported.
4149 CheckDef (bool result, string name, Location l)
4153 CheckDef (DeclSpace.AdditionResult.NameExists, name, l);
4156 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4158 Block implicit_block;
4159 ArrayList inits = null;
4162 // We use the `Used' property to check whether statements
4163 // have been added to the current block. If so, we need
4164 // to create another block to contain the new declaration
4165 // otherwise, as an optimization, we use the same block to
4166 // add the declaration.
4168 // FIXME: A further optimization is to check if the statements
4169 // that were added were added as part of the initialization
4170 // below. In which case, no other statements have been executed
4171 // and we might be able to reduce the number of blocks for
4172 // situations like this:
4174 // int j = 1; int k = j + 1;
4176 if (current_block.Used)
4177 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4179 implicit_block = current_block;
4181 foreach (VariableDeclaration decl in variable_declarators){
4183 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4184 if (decl.expression_or_array_initializer != null){
4186 inits = new ArrayList (4);
4193 return implicit_block;
4195 foreach (VariableDeclaration decl in inits){
4199 if (decl.expression_or_array_initializer is Expression){
4200 expr = (Expression) decl.expression_or_array_initializer;
4203 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4205 expr = new ArrayCreation (type, "", init, decl.Location);
4208 LocalVariableReference var;
4209 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4211 assign = new Assign (var, expr, decl.Location);
4213 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4216 return implicit_block;
4219 Block declare_local_constants (Expression type, ArrayList declarators)
4221 Block implicit_block;
4223 if (current_block.Used)
4224 implicit_block = new Block (current_block, Block.Flags.Implicit);
4226 implicit_block = current_block;
4228 foreach (VariableDeclaration decl in declarators){
4229 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4230 current_local_parameters, decl.Location);
4233 return implicit_block;
4236 void CheckAttributeTarget (string a)
4240 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4244 Location l = lexer.Location;
4245 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4251 void CheckUnaryOperator (Operator.OpType op)
4255 case Operator.OpType.LogicalNot:
4256 case Operator.OpType.OnesComplement:
4257 case Operator.OpType.Increment:
4258 case Operator.OpType.Decrement:
4259 case Operator.OpType.True:
4260 case Operator.OpType.False:
4261 case Operator.OpType.Addition:
4262 case Operator.OpType.Subtraction:
4267 Location l = lexer.Location;
4268 Report.Error (1019, l, "Overloadable unary operator expected");
4274 void CheckBinaryOperator (Operator.OpType op)
4278 case Operator.OpType.Addition:
4279 case Operator.OpType.Subtraction:
4280 case Operator.OpType.Multiply:
4281 case Operator.OpType.Division:
4282 case Operator.OpType.Modulus:
4283 case Operator.OpType.BitwiseAnd:
4284 case Operator.OpType.BitwiseOr:
4285 case Operator.OpType.ExclusiveOr:
4286 case Operator.OpType.LeftShift:
4287 case Operator.OpType.RightShift:
4288 case Operator.OpType.Equality:
4289 case Operator.OpType.Inequality:
4290 case Operator.OpType.GreaterThan:
4291 case Operator.OpType.LessThan:
4292 case Operator.OpType.GreaterThanOrEqual:
4293 case Operator.OpType.LessThanOrEqual:
4297 Location l = lexer.Location;
4298 Report.Error (1020, l, "Overloadable binary operator expected");
4304 void syntax_error (Location l, string msg)
4306 Report.Error (1003, l, "Syntax error, " + msg);
4309 void output (string s)
4311 Console.WriteLine (s);
4314 void note (string s)
4316 // Used to put annotations
4321 public Tokenizer Lexer {
4327 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4329 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4330 this.name = file.Name;
4332 current_container = RootContext.Tree.Types;
4333 current_container.NamespaceEntry = current_namespace;
4334 oob_stack = new Stack ();
4335 switch_stack = new Stack ();
4337 lexer = new Tokenizer (reader, file, defines);
4340 public override void parse ()
4343 if (yacc_verbose_flag)
4344 yyparse (lexer, new yydebug.yyDebugSimple ());
4347 Tokenizer tokenizer = lexer as Tokenizer;
4348 tokenizer.cleanup ();
4349 } catch (Exception e){
4350 // Please do not remove this, it is used during debugging
4353 // Console.WriteLine (e);
4354 Report.Error (-25, lexer.Location, "Parsing error");
4355 if (Driver.parser_verbose)
4356 Console.WriteLine (e);
4360 void CheckToken (int error, int yyToken, string msg)
4362 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4363 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4366 Report.Error (error, lexer.Location, msg);
4369 void CheckIdentifierToken (int yyToken)
4371 CheckToken (1041, yyToken, "Identifier expected");