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
192 /* C# keywords which are not really keywords */
198 /* C# single character operators/punctuation. */
199 %token OPEN_BRACE "{"
200 %token CLOSE_BRACE "}"
201 %token OPEN_BRACKET "["
202 %token CLOSE_BRACKET "]"
203 %token OPEN_PARENS "("
204 %token CLOSE_PARENS ")"
217 %token BITWISE_AND "&"
218 %token BITWISE_OR "|"
225 /* C# multi-character operators. */
228 %token OP_SHIFT_LEFT "<<"
229 %token OP_SHIFT_RIGHT ">>"
236 %token OP_MULT_ASSIGN "*="
237 %token OP_DIV_ASSIGN "/="
238 %token OP_MOD_ASSIGN "%="
239 %token OP_ADD_ASSIGN "+="
240 %token OP_SUB_ASSIGN "-="
241 %token OP_SHIFT_LEFT_ASSIGN "<<="
242 %token OP_SHIFT_RIGHT_ASSIGN ">>="
243 %token OP_AND_ASSIGN "&="
244 %token OP_XOR_ASSIGN "^="
245 %token OP_OR_ASSIGN "|="
249 %token LITERAL_INTEGER "int literal"
250 %token LITERAL_FLOAT "float literal"
251 %token LITERAL_DOUBLE "double literal"
252 %token LITERAL_DECIMAL "decimal literal"
253 %token LITERAL_CHARACTER "character literal"
254 %token LITERAL_STRING "string literal"
257 %token CLOSE_PARENS_CAST
258 %token CLOSE_PARENS_NO_CAST
259 %token CLOSE_PARENS_OPEN_PARENS
260 %token CLOSE_PARENS_MINUS
262 /* Add precedence rules to solve dangling else s/r conflict */
271 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
273 %left STAR DIV PERCENT
274 %right BANG CARRET UMINUS
275 %nonassoc OP_INC OP_DEC
277 %left OPEN_BRACKET OPEN_BRACE
281 %start compilation_unit
285 : outer_declarations opt_EOF
286 | outer_declarations global_attributes opt_EOF
287 | global_attributes opt_EOF
288 | opt_EOF /* allow empty files */
298 | outer_declarations outer_declaration
303 | namespace_member_declaration
308 | using_directives using_directive
312 : using_alias_directive
313 | using_namespace_directive
316 using_alias_directive
317 : USING IDENTIFIER ASSIGN
318 namespace_or_type_name SEMICOLON
320 current_namespace.UsingAlias ((string) $2, (Expression) $4, lexer.Location);
323 CheckIdentifierToken (yyToken);
327 using_namespace_directive
328 : USING namespace_name SEMICOLON
330 current_namespace.Using ((string) $2, lexer.Location);
335 // Strictly speaking, namespaces don't have attributes but
336 // we parse global attributes along with namespace declarations and then
339 namespace_declaration
340 : opt_attributes NAMESPACE namespace_or_type_name
343 Report.Error(1518, Lexer.Location, "Attributes cannot be applied to namespaces."
344 + " Expected class, delegate, enum, interface, or struct");
347 current_namespace = RootContext.Tree.RecordNamespace (current_namespace, file, $3.ToString (), lexer.Location);
349 namespace_body opt_semicolon
351 current_namespace = current_namespace.Parent;
366 : namespace_or_type_name { $$ = ($1).ToString (); }
372 opt_namespace_member_declarations
383 opt_namespace_member_declarations
385 | namespace_member_declarations
388 namespace_member_declarations
389 : namespace_member_declaration
390 | namespace_member_declarations namespace_member_declaration
393 namespace_member_declaration
400 Class c = (Class) $1;
401 mod_flags = c.ModFlags;
403 } else if ($1 is Struct){
404 Struct s = (Struct) $1;
405 mod_flags = s.ModFlags;
410 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
412 1527, lexer.Location,
413 "Namespace elements cant be explicitly " +
414 "declared private or protected in `" + name + "'");
416 current_namespace.DeclarationFound = true;
418 | namespace_declaration {
419 current_namespace.DeclarationFound = true;
422 | field_declaration {
423 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
425 | method_declaration {
426 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
433 | interface_declaration
435 | delegate_declaration
437 // Enable this when we have handled all errors, because this acts as a generic fallback
440 // Console.WriteLine ("Token=" + yyToken);
441 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
453 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
461 global_attrs_enabled = false;
466 global_attrs_enabled = false;
475 ArrayList sect = (ArrayList) $1;
477 if (global_attrs_enabled) {
478 if (current_attr_target == "module") {
479 CodeGen.Module.AddAttributes (sect);
481 } else if (current_attr_target == "assembly") {
482 CodeGen.Assembly.AddAttributes (sect);
485 $$ = new Attributes (sect);
488 $$ = new Attributes (sect);
490 current_attr_target = null;
492 | attribute_sections attribute_section
494 Attributes attrs = $1 as Attributes;
495 ArrayList sect = (ArrayList) $2;
497 if (global_attrs_enabled) {
498 if (current_attr_target == "module") {
499 CodeGen.Module.AddAttributes (sect);
501 } else if (current_attr_target == "assembly") {
502 CodeGen.Assembly.AddAttributes (sect);
506 attrs = new Attributes (sect);
508 attrs.AddAttributes (sect);
512 attrs = new Attributes (sect);
514 attrs.AddAttributes (sect);
517 current_attr_target = null;
522 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
526 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
532 attribute_target_specifier
533 : attribute_target COLON
535 current_attr_target = (string)$1;
543 CheckAttributeTarget ((string) $1);
546 | EVENT { $$ = "event"; }
547 | RETURN { $$ = "return"; }
553 ArrayList attrs = new ArrayList (4);
559 | attribute_list COMMA attribute
561 ArrayList attrs = (ArrayList) $1;
573 opt_attribute_arguments
575 if (current_attr_target == "assembly" || current_attr_target == "module")
576 $$ = new GlobalAttribute (current_container, current_attr_target, (string) ($1).ToString (), (ArrayList) $3, (Location) $2);
578 $$ = new Attribute (current_attr_target, (string) ($1).ToString (), (ArrayList) $3, (Location) $2);
583 : type_name { /* reserved attribute name or identifier: 17.4 */ }
586 opt_attribute_arguments
587 : /* empty */ { $$ = null; }
588 | OPEN_PARENS attribute_arguments CLOSE_PARENS
596 : opt_positional_argument_list
601 ArrayList args = new ArrayList (4);
607 | positional_argument_list COMMA named_argument_list
609 ArrayList args = new ArrayList (4);
615 | named_argument_list
617 ArrayList args = new ArrayList (4);
626 opt_positional_argument_list
627 : /* empty */ { $$ = null; }
628 | positional_argument_list
631 positional_argument_list
634 ArrayList args = new ArrayList (4);
635 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
639 | positional_argument_list COMMA expression
641 ArrayList args = (ArrayList) $1;
642 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
651 ArrayList args = new ArrayList (4);
656 | named_argument_list COMMA named_argument
658 ArrayList args = (ArrayList) $1;
666 : IDENTIFIER ASSIGN expression
668 $$ = new DictionaryEntry (
670 new Argument ((Expression) $3, Argument.AType.Expression));
676 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
679 opt_class_member_declarations
681 | class_member_declarations
684 class_member_declarations
685 : class_member_declaration
686 | class_member_declarations
687 class_member_declaration
690 class_member_declaration
691 : constant_declaration // done
692 | field_declaration // done
693 | method_declaration // done
694 | property_declaration // done
695 | event_declaration // done
696 | indexer_declaration // done
697 | operator_declaration // done
698 | constructor_declaration // done
699 | destructor_declaration // done
709 string full_struct_name = MakeName ((string) $4);
711 new_struct = new Struct (current_namespace, current_container, full_struct_name,
712 (int) $2, (Attributes) $1, lexer.Location);
713 current_container = new_struct;
714 RootContext.Tree.RecordDecl (full_struct_name, new_struct);
720 Struct new_struct = (Struct) current_container;
723 new_struct.Bases = (ArrayList) $6;
725 current_container = current_container.Parent;
726 CheckDef (current_container.AddStruct (new_struct), new_struct.Name, new_struct.Location);
729 | opt_attributes opt_modifiers STRUCT error {
730 CheckIdentifierToken (yyToken);
735 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
738 opt_struct_member_declarations
740 | struct_member_declarations
743 struct_member_declarations
744 : struct_member_declaration
745 | struct_member_declarations struct_member_declaration
748 struct_member_declaration
749 : constant_declaration
752 | property_declaration
754 | indexer_declaration
755 | operator_declaration
756 | constructor_declaration
760 * This is only included so we can flag error 575:
761 * destructors only allowed on class types
763 | destructor_declaration
774 foreach (VariableDeclaration constant in (ArrayList) $5){
775 Location l = constant.Location;
777 Const c = new Const (
778 (Expression) $4, (string) constant.identifier,
779 (Expression) constant.expression_or_array_initializer, (int) $2,
782 CheckDef (current_container.AddConstant (c), c.Name, l);
788 : constant_declarator
790 ArrayList constants = new ArrayList (4);
794 | constant_declarators COMMA constant_declarator
796 ArrayList constants = (ArrayList) $1;
803 : IDENTIFIER ASSIGN constant_expression
805 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
816 Expression type = (Expression) $3;
819 foreach (VariableDeclaration var in (ArrayList) $4){
820 Location l = var.Location;
822 Field field = new Field (type, mod, var.identifier,
823 var.expression_or_array_initializer,
826 CheckDef (current_container.AddField (field), field.Name, l);
834 Report.Error (670, lexer.Location, "void type is not allowed for fields");
839 : variable_declarator
841 ArrayList decl = new ArrayList (4);
845 | variable_declarators COMMA variable_declarator
847 ArrayList decls = (ArrayList) $1;
854 : IDENTIFIER ASSIGN variable_initializer
856 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
860 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
873 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
875 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
881 iterator_container = (IIteratorContainer) $1;
885 Method method = (Method) $1;
886 Block b = (Block) $3;
887 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
890 if ((method.ModFlags & extern_abstract) == 0){
892 501, lexer.Location, current_container.MakeName (method.Name) +
893 "must declare a body because it is not marked abstract or extern");
896 if ((method.ModFlags & Modifiers.EXTERN) != 0){
898 179, lexer.Location, current_container.MakeName (method.Name) +
899 " is declared extern, but has a body");
903 method.Block = (Block) $3;
904 CheckDef (current_container.AddMethod (method), method.Name, method.Location);
906 current_local_parameters = null;
907 iterator_container = null;
921 1585, lexer.Location, "Member modifier `" +
922 Modifiers.Name (i) + "' must precede member type and name");
935 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
937 Method method = new Method (current_container, (Expression) $3, (int) $2,
938 false, (string) $4, (Parameters) $6,
939 (Attributes) $1, lexer.Location);
941 current_local_parameters = (Parameters) $6;
949 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
951 Method method = new Method (current_container, TypeManager.system_void_expr,
952 (int) $2, false, (string) $4, (Parameters) $6,
953 (Attributes) $1, lexer.Location);
955 current_local_parameters = (Parameters) $6;
961 modifiers member_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
963 Report.Error (1585, lexer.Location,
964 String.Format ("Modifier {0} should appear before type",
965 Modifiers.Name ((int) $4)));
966 Method method = new Method (current_container, TypeManager.system_void_expr,
967 0, false, (string) $5, (Parameters) $7,
968 (Attributes) $1, lexer.Location);
970 current_local_parameters = (Parameters) $7;
977 | SEMICOLON { $$ = null; }
980 opt_formal_parameter_list
981 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
982 | formal_parameter_list
985 formal_parameter_list
988 ArrayList pars_list = (ArrayList) $1;
990 Parameter [] pars = new Parameter [pars_list.Count];
991 pars_list.CopyTo (pars);
993 $$ = new Parameters (pars, null, lexer.Location);
995 | fixed_parameters COMMA parameter_array
997 ArrayList pars_list = (ArrayList) $1;
999 Parameter [] pars = new Parameter [pars_list.Count];
1000 pars_list.CopyTo (pars);
1002 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
1006 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
1013 ArrayList pars = new ArrayList (4);
1018 | fixed_parameters COMMA fixed_parameter
1020 ArrayList pars = (ArrayList) $1;
1029 opt_parameter_modifier
1033 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
1036 opt_parameter_modifier
1039 CheckIdentifierToken (yyToken);
1044 opt_parameter_modifier
1045 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1046 | parameter_modifier
1050 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1051 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1055 : opt_attributes PARAMS type IDENTIFIER
1057 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1058 note ("type must be a single-dimension array type");
1060 | opt_attributes PARAMS type error {
1061 CheckIdentifierToken (yyToken);
1067 : namespace_or_type_name { $$ = ($1).ToString (); }
1070 property_declaration
1076 implicit_value_parameter_type = (Expression) $3;
1078 lexer.PropertyParsing = true;
1080 $$ = lexer.Location;
1082 iterator_container = SimpleIteratorContainer.GetSimple ();
1084 accessor_declarations
1086 lexer.PropertyParsing = false;
1091 Pair pair = (Pair) $7;
1092 Accessor get_block = (Accessor) pair.First;
1093 Accessor set_block = (Accessor) pair.Second;
1095 Location loc = (Location) $6;
1096 prop = new Property (current_container, (Expression) $3, (int) $2, false,
1097 (string) $4, (Attributes) $1, get_block, set_block, loc);
1098 if (SimpleIteratorContainer.Simple.Yields)
1101 CheckDef (current_container.AddProperty (prop), prop.Name, loc);
1102 implicit_value_parameter_type = null;
1103 iterator_container = null;
1107 accessor_declarations
1108 : get_accessor_declaration opt_set_accessor_declaration
1110 $$ = new Pair ($1, $2);
1112 | set_accessor_declaration opt_get_accessor_declaration
1114 $$ = new Pair ($2, $1);
1118 opt_get_accessor_declaration
1119 : /* empty */ { $$ = null; }
1120 | get_accessor_declaration
1123 opt_set_accessor_declaration
1124 : /* empty */ { $$ = null; }
1125 | set_accessor_declaration
1128 get_accessor_declaration
1129 : opt_attributes GET
1131 // If this is not the case, then current_local_parameters has already
1132 // been set in indexer_declaration
1133 if (parsing_indexer == false)
1134 current_local_parameters = null;
1136 current_local_parameters = indexer_parameters;
1137 lexer.PropertyParsing = false;
1141 $$ = new Accessor ((Block) $4, (Attributes) $1);
1142 current_local_parameters = null;
1143 lexer.PropertyParsing = true;
1147 set_accessor_declaration
1148 : opt_attributes SET
1151 Parameter implicit_value_parameter = new Parameter (
1152 implicit_value_parameter_type, "value",
1153 Parameter.Modifier.NONE, null);
1155 if (parsing_indexer == false) {
1156 args = new Parameter [1];
1157 args [0] = implicit_value_parameter;
1158 current_local_parameters = new Parameters (args, null, lexer.Location);
1160 Parameter [] fpars = indexer_parameters.FixedParameters;
1163 int count = fpars.Length;
1165 args = new Parameter [count + 1];
1166 fpars.CopyTo (args, 0);
1167 args [count] = implicit_value_parameter;
1170 current_local_parameters = new Parameters (
1171 args, indexer_parameters.ArrayParameter, lexer.Location);
1174 lexer.PropertyParsing = false;
1178 $$ = new Accessor ((Block) $4, (Attributes) $1);
1179 current_local_parameters = null;
1180 lexer.PropertyParsing = true;
1186 | SEMICOLON { $$ = null; }
1189 interface_declaration
1192 INTERFACE IDENTIFIER
1194 Interface new_interface;
1195 string full_interface_name = MakeName ((string) $4);
1197 new_interface = new Interface (current_namespace, current_container, full_interface_name,
1198 (int) $2, (Attributes) $1, lexer.Location);
1199 if (current_interface != null) {
1200 Location l = lexer.Location;
1201 Report.Error (-2, l, "Internal compiler error: interface inside interface");
1203 current_interface = new_interface;
1204 current_container = new_interface;
1205 RootContext.Tree.RecordDecl (full_interface_name, new_interface);
1208 interface_body opt_semicolon
1210 Interface new_interface = (Interface) current_interface;
1213 new_interface.Bases = (ArrayList) $6;
1215 current_interface = null;
1216 current_container = current_container.Parent;
1217 CheckDef (current_container.AddInterface (new_interface),
1218 new_interface.Name, new_interface.Location);
1220 | opt_attributes opt_modifiers INTERFACE error {
1221 CheckIdentifierToken (yyToken);
1227 opt_interface_member_declarations
1231 opt_interface_member_declarations
1233 | interface_member_declarations
1236 interface_member_declarations
1237 : interface_member_declaration
1238 | interface_member_declarations interface_member_declaration
1241 interface_member_declaration
1242 : interface_method_declaration
1244 Method m = (Method) $1;
1246 CheckDef (current_interface.AddMethod (m), m.Name, m.Location);
1248 | interface_property_declaration
1250 Property p = (Property) $1;
1252 CheckDef (current_interface.AddProperty (p), p.Name, p.Location);
1254 | interface_event_declaration
1257 Event e = (Event) $1;
1258 CheckDef (current_interface.AddEvent (e), e.Name, lexer.Location);
1261 | interface_indexer_declaration
1263 Indexer i = (Indexer) $1;
1265 current_interface.AddIndexer (i);
1273 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, lexer.Location);
1278 interface_method_declaration
1279 : opt_attributes opt_new type IDENTIFIER
1280 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1283 $$ = new Method (current_container, (Expression) $3, (int) $2, true,
1284 (string) $4, (Parameters) $6, (Attributes) $1,
1287 | opt_attributes opt_new VOID IDENTIFIER
1288 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1291 $$ = new Method (current_container, TypeManager.system_void_expr, (int) $2,
1292 true, (string) $4, (Parameters) $6, (Attributes) $1,
1297 interface_property_declaration
1302 { lexer.PropertyParsing = true; }
1304 { lexer.PropertyParsing = false; }
1307 InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
1309 $$ = new Property (current_container, (Expression) $3, (int) $2, true,
1310 (string) $4, (Attributes) $1, pinfo.Get, pinfo.Set,
1316 CheckIdentifierToken (yyToken);
1322 : opt_attributes GET SEMICOLON { $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null); }
1323 | opt_attributes SET SEMICOLON { $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1); }
1324 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1325 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $3); }
1326 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1327 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $3, (Attributes) $1); }
1330 interface_event_declaration
1331 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1333 $$ = new EventField (current_container, (Expression) $4, (int) $2, true, (string) $5, null,
1334 (Attributes) $1, lexer.Location);
1336 | opt_attributes opt_new EVENT type error {
1337 CheckIdentifierToken (yyToken);
1340 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1341 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1344 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE event_accessor_declarations CLOSE_BRACE {
1345 Report.Error (69, lexer.Location, "Event accessors not valid on interfaces");
1350 interface_indexer_declaration
1351 : opt_attributes opt_new type THIS
1352 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1354 { lexer.PropertyParsing = true; }
1356 { lexer.PropertyParsing = false; }
1359 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1361 $$ = new Indexer (current_container, (Expression) $3, null, (int) $2, true,
1362 (Parameters) $6, (Attributes) $1, info.Get, info.Set,
1367 operator_declaration
1368 : opt_attributes opt_modifiers operator_declarator
1370 iterator_container = SimpleIteratorContainer.GetSimple ();
1374 OperatorDeclaration decl = (OperatorDeclaration) $3;
1376 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
1377 decl.arg2type, decl.arg2name, (Block) $5, (Attributes) $1, decl.location);
1379 if (SimpleIteratorContainer.Simple.Yields)
1382 // Note again, checking is done in semantic analysis
1383 current_container.AddOperator (op);
1385 current_local_parameters = null;
1386 iterator_container = null;
1392 | SEMICOLON { $$ = null; }
1395 : type OPERATOR overloadable_operator
1396 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1398 Operator.OpType op = (Operator.OpType) $3;
1399 CheckUnaryOperator (op);
1401 if (op == Operator.OpType.Addition)
1402 op = Operator.OpType.UnaryPlus;
1404 if (op == Operator.OpType.Subtraction)
1405 op = Operator.OpType.UnaryNegation;
1407 Parameter [] pars = new Parameter [1];
1409 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1411 current_local_parameters = new Parameters (pars, null, lexer.Location);
1413 $$ = new OperatorDeclaration (op, (Expression) $1, (Expression) $5, (string) $6,
1414 null, null, lexer.Location);
1416 | type OPERATOR overloadable_operator
1418 type IDENTIFIER COMMA
1422 CheckBinaryOperator ((Operator.OpType) $3);
1424 Parameter [] pars = new Parameter [2];
1426 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1427 pars [1] = new Parameter ((Expression) $8, (string) $9, Parameter.Modifier.NONE, null);
1429 current_local_parameters = new Parameters (pars, null, lexer.Location);
1431 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1432 (Expression) $5, (string) $6,
1433 (Expression) $8, (string) $9, lexer.Location);
1435 | conversion_operator_declarator
1438 overloadable_operator
1440 : BANG { $$ = Operator.OpType.LogicalNot; }
1441 | TILDE { $$ = Operator.OpType.OnesComplement; }
1442 | OP_INC { $$ = Operator.OpType.Increment; }
1443 | OP_DEC { $$ = Operator.OpType.Decrement; }
1444 | TRUE { $$ = Operator.OpType.True; }
1445 | FALSE { $$ = Operator.OpType.False; }
1446 // Unary and binary:
1447 | PLUS { $$ = Operator.OpType.Addition; }
1448 | MINUS { $$ = Operator.OpType.Subtraction; }
1450 | STAR { $$ = Operator.OpType.Multiply; }
1451 | DIV { $$ = Operator.OpType.Division; }
1452 | PERCENT { $$ = Operator.OpType.Modulus; }
1453 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1454 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1455 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1456 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1457 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1458 | OP_EQ { $$ = Operator.OpType.Equality; }
1459 | OP_NE { $$ = Operator.OpType.Inequality; }
1460 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1461 | OP_LT { $$ = Operator.OpType.LessThan; }
1462 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1463 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1466 conversion_operator_declarator
1467 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1469 Parameter [] pars = new Parameter [1];
1471 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1473 current_local_parameters = new Parameters (pars, null, lexer.Location);
1475 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1476 null, null, lexer.Location);
1478 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1480 Parameter [] pars = new Parameter [1];
1482 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1484 current_local_parameters = new Parameters (pars, null, lexer.Location);
1486 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1487 null, null, lexer.Location);
1491 syntax_error (lexer.Location, "'operator' expected");
1495 syntax_error (lexer.Location, "'operator' expected");
1499 constructor_declaration
1502 constructor_declarator
1505 Constructor c = (Constructor) $3;
1506 c.Block = (Block) $4;
1507 c.OptAttributes = (Attributes) $1;
1508 c.ModFlags = (int) $2;
1510 if (c.Name == current_container.Basename){
1511 if ((c.ModFlags & Modifiers.STATIC) != 0){
1512 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1514 515, c.Location, String.Format (
1515 "`{0}.{1}': static constructor can not have access modifiers",
1516 c.Name, current_container.Name));
1519 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1521 if (c.Initializer != null){
1524 "Static constructors can not have an explicit this or base " +
1525 "constructor invocations");
1528 if (!c.Parameters.Empty){
1530 132, c.Location, "Static constructors should not have parameters");
1533 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1536 // We let another layer check the validity of the constructor.
1537 Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1540 CheckDef (current_container.AddConstructor (c), c.Name, c.Location);
1542 current_local_parameters = null;
1546 constructor_declarator
1548 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1550 oob_stack.Push (lexer.Location);
1552 current_local_parameters = (Parameters) $3;
1554 opt_constructor_initializer
1556 Location l = (Location) oob_stack.Pop ();
1557 $$ = new Constructor (current_container, (string) $1, 0, (Parameters) $3,
1558 (ConstructorInitializer) $6, l);
1564 | SEMICOLON { $$ = null; }
1567 opt_constructor_initializer
1568 : /* empty */ { $$ = null; }
1569 | constructor_initializer
1572 constructor_initializer
1573 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1575 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1577 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1579 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1582 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1588 : /* EMPTY */ { $$ = 0; }
1589 | UNSAFE { $$ = Modifiers.UNSAFE; }
1590 | EXTERN { $$ = Modifiers.EXTERN; }
1593 destructor_declaration
1594 : opt_attributes opt_finalizer TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1596 if ((string) $4 != current_container.Basename){
1597 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1598 } else if (!(current_container is Class)){
1599 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1601 Location l = lexer.Location;
1604 if (!RootContext.StdLib && current_container.Name == "System.Object")
1605 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
1607 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
1609 if ((m & Modifiers.UNSAFE) != 0){
1610 if (!RootContext.Unsafe){
1611 Report.Error (227, l,
1612 "Unsafe code requires the -unsafe command " +
1613 "line option to be specified");
1617 Method d = new Destructor (
1618 current_container, TypeManager.system_void_expr, m, "Finalize",
1619 new Parameters (null, null, l), (Attributes) $1, l);
1621 d.Block = (Block) $7;
1622 CheckDef (current_container.AddMethod (d), d.Name, d.Location);
1630 EVENT type variable_declarators SEMICOLON
1632 foreach (VariableDeclaration var in (ArrayList) $5) {
1634 Event e = new EventField (current_container, (Expression) $4, (int) $2, false, var.identifier,
1635 var.expression_or_array_initializer,
1636 (Attributes) $1, lexer.Location);
1638 CheckDef (current_container.AddEvent (e), e.Name, e.Location);
1644 EVENT type member_name
1647 implicit_value_parameter_type = (Expression) $4;
1648 lexer.EventParsing = true;
1649 oob_stack.Push (lexer.Location);
1651 event_accessor_declarations
1653 lexer.EventParsing = false;
1657 Location loc = (Location) oob_stack.Pop ();
1660 Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
1663 Pair pair = (Pair) $8;
1665 Event e = new EventProperty (current_container, (Expression) $4, (int) $2, false, (string) $5,
1666 null, (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
1669 CheckDef (current_container.AddEvent (e), e.Name, loc);
1670 implicit_value_parameter_type = null;
1673 | opt_attributes opt_modifiers EVENT type member_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS block {
1674 string mn = (string) $5;
1676 if (mn.IndexOf ('.') != -1)
1677 Report.Error (71, lexer.Location, "Explicit implementation of events requires property syntax");
1679 Report.Error (71, lexer.Location, "Event declaration should use property syntax");
1683 event_accessor_declarations
1684 : add_accessor_declaration remove_accessor_declaration
1686 $$ = new Pair ($1, $2);
1688 | remove_accessor_declaration add_accessor_declaration
1690 $$ = new Pair ($2, $1);
1692 | add_accessor_declaration { $$ = null; }
1693 | remove_accessor_declaration { $$ = null; }
1696 add_accessor_declaration
1697 : opt_attributes ADD
1699 Parameter [] args = new Parameter [1];
1700 Parameter implicit_value_parameter = new Parameter (
1701 implicit_value_parameter_type, "value",
1702 Parameter.Modifier.NONE, null);
1704 args [0] = implicit_value_parameter;
1706 current_local_parameters = new Parameters (args, null, lexer.Location);
1707 lexer.EventParsing = false;
1711 $$ = new Accessor ((Block) $4, (Attributes) $1);
1712 lexer.EventParsing = true;
1714 | opt_attributes ADD error {
1715 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1720 remove_accessor_declaration
1721 : opt_attributes REMOVE
1723 Parameter [] args = new Parameter [1];
1724 Parameter implicit_value_parameter = new Parameter (
1725 implicit_value_parameter_type, "value",
1726 Parameter.Modifier.NONE, null);
1728 args [0] = implicit_value_parameter;
1730 current_local_parameters = new Parameters (args, null, lexer.Location);
1731 lexer.EventParsing = false;
1735 $$ = new Accessor ((Block) $4, (Attributes) $1);
1736 lexer.EventParsing = true;
1738 | opt_attributes REMOVE error {
1739 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1745 : opt_attributes opt_modifiers indexer_declarator
1748 IndexerDeclaration decl = (IndexerDeclaration) $3;
1750 implicit_value_parameter_type = decl.type;
1752 lexer.PropertyParsing = true;
1753 parsing_indexer = true;
1755 indexer_parameters = decl.param_list;
1756 oob_stack.Push (lexer.Location);
1758 accessor_declarations
1760 lexer.PropertyParsing = false;
1761 parsing_indexer = false;
1765 // The signature is computed from the signature of the indexer. Look
1766 // at section 3.6 on the spec
1767 Location loc = (Location) oob_stack.Pop ();
1769 IndexerDeclaration decl = (IndexerDeclaration) $3;
1770 Pair pair = (Pair) $6;
1771 Accessor get_block = (Accessor) pair.First;
1772 Accessor set_block = (Accessor) pair.Second;
1774 indexer = new Indexer (current_container, decl.type, decl.interface_type,
1775 (int) $2, false, decl.param_list, (Attributes) $1,
1776 get_block, set_block, loc);
1778 // Note that there is no equivalent of CheckDef for this case
1779 // We shall handle this in semantic analysis
1781 current_container.AddIndexer (indexer);
1783 current_local_parameters = null;
1784 implicit_value_parameter_type = null;
1785 indexer_parameters = null;
1790 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1792 Parameters pars = (Parameters) $4;
1794 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1795 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1798 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
1800 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1802 Parameters pars = (Parameters) $6;
1804 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1805 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1807 $$ = new IndexerDeclaration ((Expression) $1, $2.ToString (), pars);
1819 Location enum_location = lexer.Location;
1821 string full_name = MakeName ((string) $4);
1822 Enum e = new Enum (current_namespace, current_container, (Expression) $5, (int) $2,
1823 full_name, (Attributes) $1, enum_location);
1825 foreach (VariableDeclaration ev in (ArrayList) $6) {
1826 Location loc = (Location) ev.Location;
1828 CheckDef (e.AddEnumMember (ev.identifier,
1829 (Expression) ev.expression_or_array_initializer,
1830 loc, ev.OptAttributes),
1831 ev.identifier, loc);
1834 CheckDef (current_container.AddEnum (e), full_name, enum_location);
1835 RootContext.Tree.RecordDecl (full_name, e);
1841 : /* empty */ { $$ = TypeManager.system_int32_expr; }
1842 | COLON type { $$ = $2; }
1846 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1852 opt_enum_member_declarations
1853 : /* empty */ { $$ = new ArrayList (4); }
1854 | enum_member_declarations opt_comma { $$ = $1; }
1857 enum_member_declarations
1858 : enum_member_declaration
1860 ArrayList l = new ArrayList (4);
1865 | enum_member_declarations COMMA enum_member_declaration
1867 ArrayList l = (ArrayList) $1;
1875 enum_member_declaration
1876 : opt_attributes IDENTIFIER
1878 $$ = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
1880 | opt_attributes IDENTIFIER
1882 $$ = lexer.Location;
1886 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
1890 delegate_declaration
1894 IDENTIFIER OPEN_PARENS
1895 opt_formal_parameter_list
1899 Location l = lexer.Location;
1900 Delegate del = new Delegate (current_namespace, current_container, (Expression) $4,
1901 (int) $2, MakeName ((string) $5), (Parameters) $7,
1902 (Attributes) $1, l);
1904 CheckDef (current_container.AddDelegate (del), del.Name, l);
1909 IDENTIFIER OPEN_PARENS
1910 opt_formal_parameter_list
1914 Location l = lexer.Location;
1915 Delegate del = new Delegate (
1916 current_namespace, current_container,
1917 TypeManager.system_void_expr, (int) $2, MakeName ((string) $5),
1918 (Parameters) $7, (Attributes) $1, l);
1920 CheckDef (current_container.AddDelegate (del), del.Name, l);
1925 : namespace_or_type_name
1928 namespace_or_type_name
1929 : IDENTIFIER { $$ = new SimpleName ((string) $1, lexer.Location);}
1930 | namespace_or_type_name DOT IDENTIFIER {
1931 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1936 * Before you think of adding a return_type, notice that we have been
1937 * using two rules in the places where it matters (one rule using type
1938 * and another identical one that uses VOID as the return type). This
1939 * gets rid of a shift/reduce couple
1942 : type_name { /* class_type */
1944 This does interfaces, delegates, struct_types, class_types,
1945 parent classes, and more! 4.2
1958 // Note that here only unmanaged types are allowed but we
1959 // can't perform checks during this phase - we do it during
1960 // semantic analysis.
1962 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1966 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
1972 | non_expression_type rank_specifier
1974 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1976 | non_expression_type STAR
1978 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1980 | expression rank_specifiers
1982 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1986 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1990 // We need this because the parser will happily go and reduce IDENTIFIER STAR
1991 // through this different path
1993 | multiplicative_expression STAR
1995 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2002 ArrayList types = new ArrayList (4);
2007 | type_list COMMA type
2009 ArrayList types = (ArrayList) $1;
2017 * replaces all the productions for isolating the various
2018 * simple types, but we need this to reuse it easily in local_variable_type
2021 : OBJECT { $$ = TypeManager.system_object_expr; }
2022 | STRING { $$ = TypeManager.system_string_expr; }
2023 | BOOL { $$ = TypeManager.system_boolean_expr; }
2024 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2025 | FLOAT { $$ = TypeManager.system_single_expr; }
2026 | DOUBLE { $$ = TypeManager.system_double_expr; }
2031 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2032 | BYTE { $$ = TypeManager.system_byte_expr; }
2033 | SHORT { $$ = TypeManager.system_int16_expr; }
2034 | USHORT { $$ = TypeManager.system_uint16_expr; }
2035 | INT { $$ = TypeManager.system_int32_expr; }
2036 | UINT { $$ = TypeManager.system_uint32_expr; }
2037 | LONG { $$ = TypeManager.system_int64_expr; }
2038 | ULONG { $$ = TypeManager.system_uint64_expr; }
2039 | CHAR { $$ = TypeManager.system_char_expr; }
2040 | VOID { $$ = TypeManager.system_void_expr; }
2048 : type rank_specifiers
2050 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2055 // Expressions, section 7.5
2063 | IDENTIFIER { $$ = new SimpleName ((string) $1, lexer.Location); }
2064 | parenthesized_expression
2066 | invocation_expression
2070 | post_increment_expression
2071 | post_decrement_expression
2075 | checked_expression
2076 | unchecked_expression
2077 | pointer_member_access
2078 | anonymous_method_expression
2085 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2086 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2087 | NULL { $$ = NullLiteral.Null; }
2091 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2092 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2093 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2098 object v = lexer.Value;
2104 $$ = IntLiteral.Zero;
2106 $$ = IntLiteral.One;
2108 $$ = new IntLiteral (i);
2109 } else if (v is uint)
2110 $$ = new UIntLiteral ((UInt32) v);
2112 $$ = new LongLiteral ((Int64) v);
2113 else if (v is ulong)
2114 $$ = new ULongLiteral ((UInt64) v);
2116 Console.WriteLine ("OOPS. Unexpected result from scanner");
2121 : TRUE { $$ = new BoolLiteral (true); }
2122 | FALSE { $$ = new BoolLiteral (false); }
2125 parenthesized_expression_0
2126 : OPEN_PARENS expression CLOSE_PARENS
2129 lexer.Deambiguate_CloseParens ();
2130 // After this, the next token returned is one of
2131 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2132 // or CLOSE_PARENS_MINUS.
2136 parenthesized_expression
2137 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2141 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2143 // If a parenthesized expression is followed by a minus, we need to wrap
2144 // the expression inside a ParenthesizedExpression for the CS0075 check
2145 // in Binary.DoResolve().
2146 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2151 : primary_expression DOT IDENTIFIER
2153 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2155 | predefined_type DOT IDENTIFIER
2157 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2165 invocation_expression
2166 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2169 Location l = lexer.Location;
2170 Report.Error (1, l, "Parse error");
2172 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2174 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2176 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2178 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2180 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2185 : /* empty */ { $$ = null; }
2192 ArrayList list = new ArrayList (4);
2196 | argument_list COMMA argument
2198 ArrayList list = (ArrayList) $1;
2202 | argument_list error {
2203 CheckToken (1026, yyToken, ", or ) expected");
2210 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2212 | REF variable_reference
2214 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2216 | OUT variable_reference
2218 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2223 : expression { note ("section 5.4"); $$ = $1; }
2227 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2229 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2231 | primary_expression rank_specifiers
2233 // So the super-trick is that primary_expression
2234 // can only be either a SimpleName or a MemberAccess.
2235 // The MemberAccess case arises when you have a fully qualified type-name like :
2237 // SimpleName is when you have
2240 Expression expr = (Expression) $1;
2241 if (expr is ComposedCast){
2242 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2243 } else if (!(expr is SimpleName || expr is MemberAccess)){
2244 Error_ExpectingTypeName (lexer.Location, expr);
2245 $$ = TypeManager.system_object_expr;
2248 // So we extract the string corresponding to the SimpleName
2251 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2259 ArrayList list = new ArrayList (4);
2263 | expression_list COMMA expression
2265 ArrayList list = (ArrayList) $1;
2274 $$ = new This (current_block, lexer.Location);
2279 : BASE DOT IDENTIFIER
2281 $$ = new BaseAccess ((string) $3, lexer.Location);
2283 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2285 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2288 Report.Error (175, "Use of keyword `base' is not valid in this context");
2293 post_increment_expression
2294 : primary_expression OP_INC
2296 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2297 (Expression) $1, lexer.Location);
2301 post_decrement_expression
2302 : primary_expression OP_DEC
2304 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2305 (Expression) $1, lexer.Location);
2310 : object_or_delegate_creation_expression
2311 | array_creation_expression
2314 object_or_delegate_creation_expression
2315 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2317 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2321 array_creation_expression
2322 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2324 opt_array_initializer
2326 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2328 | NEW type rank_specifiers array_initializer
2330 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2334 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2350 : rank_specifier opt_rank_specifier
2352 $$ = (string) $2 + (string) $1;
2357 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2359 $$ = "[" + (string) $2 + "]";
2379 | dim_separators COMMA
2381 $$ = (string) $1 + ",";
2385 opt_array_initializer
2397 : OPEN_BRACE CLOSE_BRACE
2399 ArrayList list = new ArrayList (4);
2402 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2404 $$ = (ArrayList) $2;
2408 variable_initializer_list
2409 : variable_initializer
2411 ArrayList list = new ArrayList (4);
2415 | variable_initializer_list COMMA variable_initializer
2417 ArrayList list = (ArrayList) $1;
2424 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2426 $$ = new TypeOfVoid (lexer.Location);
2428 | TYPEOF OPEN_PARENS type CLOSE_PARENS
2430 $$ = new TypeOf ((Expression) $3, lexer.Location);
2435 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2436 $$ = new SizeOf ((Expression) $3, lexer.Location);
2441 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2443 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2447 unchecked_expression
2448 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2450 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2454 pointer_member_access
2455 : primary_expression OP_PTR IDENTIFIER
2459 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2460 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2464 anonymous_method_expression
2465 : DELEGATE opt_anonymous_method_signature {
2466 oob_stack.Push (current_local_parameters);
2467 current_local_parameters = (Parameters)$2;
2468 create_toplevel_block = true;
2471 Report.Error (-213, lexer.Location, "Anonymous methods are not supported in this branch");
2474 create_toplevel_block = false;
2475 if (!RootContext.V2){
2476 Report.Error (-213, lexer.Location, "Anonymous methods are only supported in V2");
2479 $$ = new AnonymousMethod ((Parameters) $2, (Block) $4, lexer.Location);
2480 current_local_parameters = (Parameters) oob_stack.Pop ();
2485 opt_anonymous_method_signature
2486 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
2487 | anonymous_method_signature
2490 anonymous_method_signature
2491 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
2494 $$ = Parameters.EmptyReadOnlyParameters;
2496 ArrayList par_list = (ArrayList) $2;
2497 Parameter [] pars = new Parameter [par_list.Count];
2498 par_list.CopyTo (pars);
2499 $$ = new Parameters (pars, null, lexer.Location);
2504 opt_anonymous_method_parameter_list
2505 : /* empty */ { $$ = null; }
2506 | anonymous_method_parameter_list { $$ = $1; }
2509 anonymous_method_parameter_list
2510 : anonymous_method_parameter
2512 ArrayList a = new ArrayList (4);
2516 | anonymous_method_parameter_list COMMA anonymous_method_parameter
2518 ArrayList a = (ArrayList) $1;
2524 anonymous_method_parameter
2525 : opt_parameter_modifier type IDENTIFIER {
2526 $$ = new Parameter ((Expression) $2, (string) $2, (Parameter.Modifier) $1, null);
2531 : primary_expression
2532 | BANG prefixed_unary_expression
2534 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2536 | TILDE prefixed_unary_expression
2538 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2544 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
2546 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2548 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
2550 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2556 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2558 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2563 // The idea to split this out is from Rhys' grammar
2564 // to solve the problem with casts.
2566 prefixed_unary_expression
2568 | PLUS prefixed_unary_expression
2570 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2572 | MINUS prefixed_unary_expression
2574 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2576 | OP_INC prefixed_unary_expression
2578 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2579 (Expression) $2, lexer.Location);
2581 | OP_DEC prefixed_unary_expression
2583 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2584 (Expression) $2, lexer.Location);
2586 | STAR prefixed_unary_expression
2588 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2590 | BITWISE_AND prefixed_unary_expression
2592 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2596 pre_increment_expression
2597 : OP_INC prefixed_unary_expression
2599 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2600 (Expression) $2, lexer.Location);
2604 pre_decrement_expression
2605 : OP_DEC prefixed_unary_expression
2607 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2608 (Expression) $2, lexer.Location);
2612 multiplicative_expression
2613 : prefixed_unary_expression
2614 | multiplicative_expression STAR prefixed_unary_expression
2616 $$ = new Binary (Binary.Operator.Multiply,
2617 (Expression) $1, (Expression) $3, lexer.Location);
2619 | multiplicative_expression DIV prefixed_unary_expression
2621 $$ = new Binary (Binary.Operator.Division,
2622 (Expression) $1, (Expression) $3, lexer.Location);
2624 | multiplicative_expression PERCENT prefixed_unary_expression
2626 $$ = new Binary (Binary.Operator.Modulus,
2627 (Expression) $1, (Expression) $3, lexer.Location);
2632 : multiplicative_expression
2633 | additive_expression PLUS multiplicative_expression
2635 $$ = new Binary (Binary.Operator.Addition,
2636 (Expression) $1, (Expression) $3, lexer.Location);
2638 | additive_expression MINUS multiplicative_expression
2640 $$ = new Binary (Binary.Operator.Subtraction,
2641 (Expression) $1, (Expression) $3, lexer.Location);
2646 : additive_expression
2647 | shift_expression OP_SHIFT_LEFT additive_expression
2649 $$ = new Binary (Binary.Operator.LeftShift,
2650 (Expression) $1, (Expression) $3, lexer.Location);
2652 | shift_expression OP_SHIFT_RIGHT additive_expression
2654 $$ = new Binary (Binary.Operator.RightShift,
2655 (Expression) $1, (Expression) $3, lexer.Location);
2659 relational_expression
2661 | relational_expression OP_LT shift_expression
2663 $$ = new Binary (Binary.Operator.LessThan,
2664 (Expression) $1, (Expression) $3, lexer.Location);
2666 | relational_expression OP_GT shift_expression
2668 $$ = new Binary (Binary.Operator.GreaterThan,
2669 (Expression) $1, (Expression) $3, lexer.Location);
2671 | relational_expression OP_LE shift_expression
2673 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2674 (Expression) $1, (Expression) $3, lexer.Location);
2676 | relational_expression OP_GE shift_expression
2678 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2679 (Expression) $1, (Expression) $3, lexer.Location);
2681 | relational_expression IS type
2683 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
2685 | relational_expression AS type
2687 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
2692 : relational_expression
2693 | equality_expression OP_EQ relational_expression
2695 $$ = new Binary (Binary.Operator.Equality,
2696 (Expression) $1, (Expression) $3, lexer.Location);
2698 | equality_expression OP_NE relational_expression
2700 $$ = new Binary (Binary.Operator.Inequality,
2701 (Expression) $1, (Expression) $3, lexer.Location);
2706 : equality_expression
2707 | and_expression BITWISE_AND equality_expression
2709 $$ = new Binary (Binary.Operator.BitwiseAnd,
2710 (Expression) $1, (Expression) $3, lexer.Location);
2714 exclusive_or_expression
2716 | exclusive_or_expression CARRET and_expression
2718 $$ = new Binary (Binary.Operator.ExclusiveOr,
2719 (Expression) $1, (Expression) $3, lexer.Location);
2723 inclusive_or_expression
2724 : exclusive_or_expression
2725 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2727 $$ = new Binary (Binary.Operator.BitwiseOr,
2728 (Expression) $1, (Expression) $3, lexer.Location);
2732 conditional_and_expression
2733 : inclusive_or_expression
2734 | conditional_and_expression OP_AND inclusive_or_expression
2736 $$ = new Binary (Binary.Operator.LogicalAnd,
2737 (Expression) $1, (Expression) $3, lexer.Location);
2741 conditional_or_expression
2742 : conditional_and_expression
2743 | conditional_or_expression OP_OR conditional_and_expression
2745 $$ = new Binary (Binary.Operator.LogicalOr,
2746 (Expression) $1, (Expression) $3, lexer.Location);
2750 conditional_expression
2751 : conditional_or_expression
2752 | conditional_or_expression INTERR expression COLON expression
2754 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2758 assignment_expression
2759 : prefixed_unary_expression ASSIGN expression
2761 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2763 | prefixed_unary_expression OP_MULT_ASSIGN expression
2765 Location l = lexer.Location;
2767 $$ = new CompoundAssign (
2768 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
2770 | prefixed_unary_expression OP_DIV_ASSIGN expression
2772 Location l = lexer.Location;
2774 $$ = new CompoundAssign (
2775 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
2777 | prefixed_unary_expression OP_MOD_ASSIGN expression
2779 Location l = lexer.Location;
2781 $$ = new CompoundAssign (
2782 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
2784 | prefixed_unary_expression OP_ADD_ASSIGN expression
2786 Location l = lexer.Location;
2788 $$ = new CompoundAssign (
2789 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
2791 | prefixed_unary_expression OP_SUB_ASSIGN expression
2793 Location l = lexer.Location;
2795 $$ = new CompoundAssign (
2796 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
2798 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2800 Location l = lexer.Location;
2802 $$ = new CompoundAssign (
2803 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
2805 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2807 Location l = lexer.Location;
2809 $$ = new CompoundAssign (
2810 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
2812 | prefixed_unary_expression OP_AND_ASSIGN expression
2814 Location l = lexer.Location;
2816 $$ = new CompoundAssign (
2817 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
2819 | prefixed_unary_expression OP_OR_ASSIGN expression
2821 Location l = lexer.Location;
2823 $$ = new CompoundAssign (
2824 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
2826 | prefixed_unary_expression OP_XOR_ASSIGN expression
2828 Location l = lexer.Location;
2830 $$ = new CompoundAssign (
2831 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
2836 : conditional_expression
2837 | assignment_expression
2859 name = MakeName ((string) $4);
2861 new_class = new Class (current_namespace, current_container, name, (int) $2,
2862 (Attributes) $1, lexer.Location);
2863 current_container = new_class;
2864 RootContext.Tree.RecordDecl (name, new_class);
2870 Class new_class = (Class) current_container;
2873 if (new_class.Name == "System.Object") {
2874 Report.Error (537, new_class.Location, "The class System.Object cannot have a base class or implement an interface.");
2876 new_class.Bases = (ArrayList) $6;
2879 current_container = current_container.Parent;
2880 CheckDef (current_container.AddClass (new_class), new_class.Name, new_class.Location);
2887 : /* empty */ { $$ = (int) 0; }
2893 | modifiers modifier
2898 if ((m1 & m2) != 0) {
2899 Location l = lexer.Location;
2900 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2902 $$ = (int) (m1 | m2);
2907 : NEW { $$ = Modifiers.NEW; }
2908 | PUBLIC { $$ = Modifiers.PUBLIC; }
2909 | PROTECTED { $$ = Modifiers.PROTECTED; }
2910 | INTERNAL { $$ = Modifiers.INTERNAL; }
2911 | PRIVATE { $$ = Modifiers.PRIVATE; }
2912 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2913 | SEALED { $$ = Modifiers.SEALED; }
2914 | STATIC { $$ = Modifiers.STATIC; }
2915 | READONLY { $$ = Modifiers.READONLY; }
2916 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2917 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2918 | EXTERN { $$ = Modifiers.EXTERN; }
2919 | VOLATILE { $$ = Modifiers.VOLATILE; }
2920 | UNSAFE { $$ = Modifiers.UNSAFE; }
2924 : /* empty */ { $$ = null; }
2925 | class_base { $$ = $1; }
2929 : COLON type_list { $$ = $2; }
2937 // A block is "contained" on the following places:
2939 // property_declaration as part of the accessor body (get/set)
2940 // operator_declaration
2941 // constructor_declaration
2942 // destructor_declaration
2943 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2948 if (current_block == null || create_toplevel_block){
2949 current_block = new ToplevelBlock (current_local_parameters, lexer.Location);
2951 current_block = new Block (current_block, current_local_parameters,
2952 lexer.Location, Location.Null);
2955 opt_statement_list CLOSE_BRACE
2957 while (current_block.Implicit)
2958 current_block = current_block.Parent;
2960 current_block.SetEndLocation (lexer.Location);
2961 current_block = current_block.Parent;
2972 | statement_list statement
2976 : declaration_statement
2978 if ($1 != null && (Block) $1 != current_block){
2979 current_block.AddStatement ((Statement) $1);
2980 current_block = (Block) $1;
2983 | valid_declaration_statement
2985 current_block.AddStatement ((Statement) $1);
2990 valid_declaration_statement
2993 | expression_statement
2994 | selection_statement
2995 | iteration_statement
2999 | unchecked_statement
3007 : valid_declaration_statement
3008 | declaration_statement
3010 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3015 Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3023 $$ = EmptyStatement.Value;
3030 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3032 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3033 current_block.AddStatement (labeled);
3038 declaration_statement
3039 : local_variable_declaration SEMICOLON
3042 DictionaryEntry de = (DictionaryEntry) $1;
3044 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3048 | local_constant_declaration SEMICOLON
3051 DictionaryEntry de = (DictionaryEntry) $1;
3053 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3059 * The following is from Rhys' grammar:
3060 * > Types in local variable declarations must be recognized as
3061 * > expressions to prevent reduce/reduce errors in the grammar.
3062 * > The expressions are converted into types during semantic analysis.
3065 : primary_expression opt_rank_specifier
3067 // FIXME: Do something smart here regarding the composition of the type.
3069 // Ok, the above "primary_expression" is there to get rid of
3070 // both reduce/reduce and shift/reduces in the grammar, it should
3071 // really just be "type_name". If you use type_name, a reduce/reduce
3072 // creeps up. If you use namespace_or_type_name (which is all we need
3073 // really) two shift/reduces appear.
3076 // So the super-trick is that primary_expression
3077 // can only be either a SimpleName or a MemberAccess.
3078 // The MemberAccess case arises when you have a fully qualified type-name like :
3080 // SimpleName is when you have
3083 Expression expr = (Expression) $1;
3084 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3085 Error_ExpectingTypeName (lexer.Location, expr);
3089 // So we extract the string corresponding to the SimpleName
3093 if ((string) $2 == "")
3096 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3099 | builtin_types opt_rank_specifier
3101 if ((string) $2 == "")
3104 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3108 local_variable_pointer_type
3109 : primary_expression STAR
3111 Expression expr = (Expression) $1;
3112 Location l = lexer.Location;
3114 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3115 Error_ExpectingTypeName (l, expr);
3119 $$ = new ComposedCast ((Expression) $1, "*", l);
3121 | builtin_types STAR
3123 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3127 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3129 | local_variable_pointer_type STAR
3131 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3135 local_variable_declaration
3136 : local_variable_type variable_declarators
3139 $$ = new DictionaryEntry ($1, $2);
3143 | local_variable_pointer_type opt_rank_specifier variable_declarators
3148 if ((string) $2 == "")
3149 t = (Expression) $1;
3151 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3152 $$ = new DictionaryEntry (t, $3);
3158 local_constant_declaration
3159 : CONST local_variable_type constant_declarators
3162 $$ = new DictionaryEntry ($2, $3);
3168 expression_statement
3169 : statement_expression SEMICOLON
3176 // We have to do the wrapping here and not in the case above,
3177 // because statement_expression is used for example in for_statement
3179 statement_expression
3180 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3181 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3182 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3183 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3184 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3185 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3186 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3188 Report.Error (1002, lexer.Location, "Expecting `;'");
3193 object_creation_expression
3194 : object_or_delegate_creation_expression
3195 { note ("complain if this is a delegate maybe?"); }
3204 : if_statement_open if_statement_rest
3213 oob_stack.Push (lexer.Location);
3218 : boolean_expression CLOSE_PARENS
3221 Location l = (Location) oob_stack.Pop ();
3223 $$ = new If ((Expression) $1, (Statement) $3, l);
3225 if (RootContext.WarningLevel >= 3){
3226 if ($3 == EmptyStatement.Value)
3227 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3231 | boolean_expression CLOSE_PARENS
3232 embedded_statement ELSE embedded_statement
3234 Location l = (Location) oob_stack.Pop ();
3236 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
3241 : SWITCH OPEN_PARENS
3243 oob_stack.Push (lexer.Location);
3244 switch_stack.Push (current_block);
3246 expression CLOSE_PARENS
3249 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
3250 current_block = (Block) switch_stack.Pop ();
3266 Report.Error (1522, lexer.Location, "Empty switch block");
3274 ArrayList sections = new ArrayList (4);
3279 | switch_sections switch_section
3281 ArrayList sections = (ArrayList) $1;
3291 current_block = current_block.CreateSwitchBlock (lexer.Location);
3295 Block topmost = current_block;
3297 while (topmost.Implicit)
3298 topmost = topmost.Parent;
3299 $$ = new SwitchSection ((ArrayList) $1, topmost);
3306 ArrayList labels = new ArrayList (4);
3311 | switch_labels switch_label
3313 ArrayList labels = (ArrayList) ($1);
3321 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3322 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3325 1523, lexer.Location,
3326 "The keyword case or default must precede code in switch block");
3340 oob_stack.Push (lexer.Location);
3342 boolean_expression CLOSE_PARENS embedded_statement
3344 Location l = (Location) oob_stack.Pop ();
3345 $$ = new While ((Expression) $4, (Statement) $6, l);
3347 if (RootContext.WarningLevel >= 3){
3348 if ($6 == EmptyStatement.Value)
3349 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3355 : DO embedded_statement
3357 oob_stack.Push (lexer.Location);
3359 boolean_expression CLOSE_PARENS SEMICOLON
3361 Location l = (Location) oob_stack.Pop ();
3363 $$ = new Do ((Statement) $2, (Expression) $6, l);
3369 opt_for_initializer SEMICOLON
3371 Block assign_block = new Block (current_block);
3372 current_block = assign_block;
3374 if ($3 is DictionaryEntry){
3375 DictionaryEntry de = (DictionaryEntry) $3;
3377 Expression type = (Expression) de.Key;
3378 ArrayList var_declarators = (ArrayList) de.Value;
3380 foreach (VariableDeclaration decl in var_declarators){
3384 vi = current_block.AddVariable (
3385 type, decl.identifier, current_local_parameters, decl.Location);
3389 Location l = lexer.Location;
3391 if (decl.expression_or_array_initializer is Expression){
3392 expr = (Expression) decl.expression_or_array_initializer;
3393 } else if (decl.expression_or_array_initializer == null) {
3396 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3397 expr = new ArrayCreation (type, "", init, decl.Location);
3400 LocalVariableReference var;
3401 var = new LocalVariableReference (assign_block, decl.identifier, l);
3404 Assign a = new Assign (var, expr, decl.Location);
3406 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3412 oob_stack.Push (lexer.Location);
3414 opt_for_condition SEMICOLON
3415 opt_for_iterator CLOSE_PARENS
3418 Location l = (Location) oob_stack.Pop ();
3420 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3422 if (RootContext.WarningLevel >= 3){
3423 if ($10 == EmptyStatement.Value)
3424 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3427 current_block.AddStatement (f);
3428 while (current_block.Implicit)
3429 current_block = current_block.Parent;
3431 current_block = current_block.Parent;
3436 : /* empty */ { $$ = EmptyStatement.Value; }
3441 : local_variable_declaration
3442 | statement_expression_list
3446 : /* empty */ { $$ = null; }
3447 | boolean_expression
3451 : /* empty */ { $$ = EmptyStatement.Value; }
3456 : statement_expression_list
3459 statement_expression_list
3460 : statement_expression
3462 // CHANGE: was `null'
3463 Block b = new Block (current_block, Block.Flags.Implicit);
3465 b.AddStatement ((Statement) $1);
3468 | statement_expression_list COMMA statement_expression
3470 Block b = (Block) $1;
3472 b.AddStatement ((Statement) $3);
3478 : FOREACH OPEN_PARENS type IDENTIFIER IN
3480 oob_stack.Push (lexer.Location);
3482 expression CLOSE_PARENS
3484 oob_stack.Push (current_block);
3486 Block foreach_block = new Block (current_block);
3487 LocalVariableReference v = null;
3488 Location l = lexer.Location;
3491 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3495 // Get a writable reference to this read-only variable.
3496 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3498 current_block = foreach_block;
3501 oob_stack.Push (current_block);
3505 Block foreach_block = (Block) oob_stack.Pop ();
3506 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3507 Block prev_block = (Block) oob_stack.Pop ();
3508 Location l = (Location) oob_stack.Pop ();
3510 current_block = prev_block;
3513 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3514 foreach_block.AddStatement (f);
3523 | continue_statement
3533 $$ = new Break (lexer.Location);
3538 : CONTINUE SEMICOLON
3540 $$ = new Continue (lexer.Location);
3545 : GOTO IDENTIFIER SEMICOLON
3547 $$ = new Goto (current_block, (string) $2, lexer.Location);
3549 | GOTO CASE constant_expression SEMICOLON
3551 $$ = new GotoCase ((Expression) $3, lexer.Location);
3553 | GOTO DEFAULT SEMICOLON
3555 $$ = new GotoDefault (lexer.Location);
3560 : RETURN opt_expression SEMICOLON
3562 $$ = new Return ((Expression) $2, lexer.Location);
3567 : THROW opt_expression SEMICOLON
3569 $$ = new Throw ((Expression) $2, lexer.Location);
3574 : IDENTIFIER RETURN expression SEMICOLON
3576 string s = (string) $1;
3578 Report.Error (1003, lexer.Location, "; expected");
3581 if (!RootContext.V2){
3582 Report.Error (-222, lexer.Location, "yield statement only available in C# 2.0 mode");
3585 if (iterator_container == null){
3586 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3589 iterator_container.SetYields ();
3590 $$ = new Yield ((Expression) $3, lexer.Location);
3593 | IDENTIFIER BREAK SEMICOLON
3595 string s = (string) $1;
3597 Report.Error (1003, lexer.Location, "; expected");
3600 if (!RootContext.V2){
3601 Report.Error (-222, lexer.Location, "yield statement only available in C# 2.0 mode");
3604 if (iterator_container == null){
3605 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3608 iterator_container.SetYields ();
3609 $$ = new YieldBreak (lexer.Location);
3620 : TRY block catch_clauses
3623 ArrayList s = new ArrayList (4);
3625 foreach (Catch cc in (ArrayList) $3) {
3632 // Now s contains the list of specific catch clauses
3633 // and g contains the general one.
3635 $$ = new Try ((Block) $2, s, g, null, lexer.Location);
3637 | TRY block opt_catch_clauses FINALLY block
3640 ArrayList s = new ArrayList (4);
3641 ArrayList catch_list = (ArrayList) $3;
3643 if (catch_list != null){
3644 foreach (Catch cc in catch_list) {
3652 $$ = new Try ((Block) $2, s, g, (Block) $5, lexer.Location);
3656 Report.Error (1524, lexer.Location, "Expected catch or finally");
3661 : /* empty */ { $$ = null; }
3668 ArrayList l = new ArrayList (4);
3673 | catch_clauses catch_clause
3675 ArrayList l = (ArrayList) $1;
3683 : /* empty */ { $$ = null; }
3688 : CATCH opt_catch_args
3690 Expression type = null;
3694 DictionaryEntry cc = (DictionaryEntry) $2;
3695 type = (Expression) cc.Key;
3696 id = (string) cc.Value;
3699 ArrayList one = new ArrayList (4);
3700 Location loc = lexer.Location;
3702 one.Add (new VariableDeclaration (id, null, loc));
3705 current_block = new Block (current_block);
3706 Block b = declare_local_variables (type, one, loc);
3711 Expression type = null;
3715 DictionaryEntry cc = (DictionaryEntry) $2;
3716 type = (Expression) cc.Key;
3717 id = (string) cc.Value;
3721 // FIXME: I can change this for an assignment.
3723 while (current_block != (Block) $1)
3724 current_block = current_block.Parent;
3729 $$ = new Catch (type, id , (Block) $4, lexer.Location);
3734 : /* empty */ { $$ = null; }
3739 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3741 $$ = new DictionaryEntry ($2, $3);
3748 $$ = new Checked ((Block) $2);
3755 $$ = new Unchecked ((Block) $2);
3762 if (!RootContext.Unsafe){
3763 Report.Error (227, lexer.Location,
3764 "Unsafe code can only be used if --unsafe is used");
3767 $$ = new Unsafe ((Block) $3);
3773 type fixed_pointer_declarators
3776 Block assign_block = new Block (current_block, Block.Flags.Implicit);
3777 ArrayList list = (ArrayList) $4;
3778 Expression type = (Expression) $3;
3779 Location l = lexer.Location;
3780 int top = list.Count;
3782 for (int i = 0; i < top; i++){
3783 Pair p = (Pair) list [i];
3786 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3793 current_block.AddStatement (assign_block);
3794 current_block = assign_block;
3795 oob_stack.Push (assign_block);
3800 Location l = (Location) oob_stack.Pop ();
3803 $$ = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
3807 fixed_pointer_declarators
3808 : fixed_pointer_declarator {
3809 ArrayList declarators = new ArrayList (4);
3810 declarators.Add ($1);
3813 | fixed_pointer_declarators COMMA fixed_pointer_declarator
3815 ArrayList declarators = (ArrayList) $1;
3816 declarators.Add ($3);
3821 fixed_pointer_declarator
3822 : IDENTIFIER ASSIGN expression
3824 $$ = new Pair ($1, $3);
3829 : LOCK OPEN_PARENS expression CLOSE_PARENS
3835 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
3840 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
3842 Block assign_block = new Block (current_block);
3843 current_block = assign_block;
3845 oob_stack.Push (lexer.Location);
3847 if ($3 is DictionaryEntry){
3848 DictionaryEntry de = (DictionaryEntry) $3;
3849 Location l = lexer.Location;
3851 Expression type = (Expression) de.Key;
3852 ArrayList var_declarators = (ArrayList) de.Value;
3854 ArrayList vars = new ArrayList (4);
3856 foreach (VariableDeclaration decl in var_declarators){
3858 LocalInfo vi = current_block.AddVariable (
3859 type, decl.identifier,
3860 current_local_parameters, decl.Location);
3866 if (decl.expression_or_array_initializer is Expression){
3867 expr = (Expression) decl.expression_or_array_initializer;
3869 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3871 expr = new ArrayCreation (type, "", init, decl.Location);
3874 LocalVariableReference var;
3876 // Get a writable reference to this read-only variable.
3877 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
3879 // This is so that it is not a warning on using variables
3882 vars.Add (new DictionaryEntry (var, expr));
3884 // Assign a = new Assign (var, expr, decl.Location);
3885 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3887 $3 = new DictionaryEntry (type, vars);
3892 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
3893 current_block.AddStatement (u);
3894 while (current_block.Implicit)
3895 current_block = current_block.Parent;
3897 current_block = current_block.Parent;
3901 resource_acquisition
3902 : local_variable_declaration
3909 // A class used to pass around variable declarations and constants
3911 public class VariableDeclaration {
3912 public string identifier;
3913 public object expression_or_array_initializer;
3914 public Location Location;
3915 public Attributes OptAttributes;
3917 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
3919 this.identifier = id;
3920 this.expression_or_array_initializer = eoai;
3922 this.OptAttributes = opt_attrs;
3925 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
3931 /// Used to pass around interface property information
3933 public class InterfaceAccessorInfo {
3935 public readonly Accessor Get, Set;
3937 public InterfaceAccessorInfo (bool has_get, bool has_set,
3938 Attributes get_attrs, Attributes set_attrs)
3941 Get = new Accessor (null, get_attrs);
3943 Set = new Accessor (null, set_attrs);
3949 // A class used to hold info about an indexer declarator
3951 public class IndexerDeclaration {
3952 public Expression type;
3953 public string interface_type;
3954 public Parameters param_list;
3956 public IndexerDeclaration (Expression type, string interface_type, Parameters param_list)
3959 this.interface_type = interface_type;
3960 this.param_list = param_list;
3965 // We use this when we do not have an object in advance that is an IIteratorContainer
3967 public class SimpleIteratorContainer : IIteratorContainer {
3970 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
3975 public static SimpleIteratorContainer GetSimple () {
3976 Simple.Yields = false;
3980 public void SetYields () { Yields = true; }
3984 // A class used to hold info about an operator declarator
3986 public class OperatorDeclaration {
3987 public Operator.OpType optype;
3988 public Expression ret_type, arg1type, arg2type;
3989 public string arg1name, arg2name;
3990 public Location location;
3992 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
3993 Expression arg1type, string arg1name,
3994 Expression arg2type, string arg2name, Location location)
3997 this.ret_type = ret_type;
3998 this.arg1type = arg1type;
3999 this.arg1name = arg1name;
4000 this.arg2type = arg2type;
4001 this.arg2name = arg2name;
4002 this.location = location;
4007 void Error_ExpectingTypeName (Location l, Expression expr)
4009 if (expr is Invocation){
4010 Report.Error (1002, l, "; expected");
4012 Report.Error (-1, l, "Invalid Type definition");
4017 // Given the @class_name name, it creates a fully qualified name
4018 // based on the containing declaration space
4021 MakeName (string class_name)
4023 string ns = current_namespace.FullName;
4024 string container_name = current_container.Name;
4026 if (container_name == ""){
4028 return ns + "." + class_name;
4032 return container_name + "." + class_name;
4036 // Used to report back to the user the result of a declaration
4037 // in the current declaration space
4040 CheckDef (DeclSpace.AdditionResult result, string name, Location l)
4042 if (result == DeclSpace.AdditionResult.Success)
4046 case DeclSpace.AdditionResult.NameExists:
4047 Report.Error (102, l, "The container `" + current_container.Name +
4048 "' already contains a definition for `"+
4054 // This is handled only for static Constructors, because
4055 // in reality we handle these by the semantic analysis later
4057 case DeclSpace.AdditionResult.MethodExists:
4059 111, l, "Class `"+current_container.Name+
4060 "' already defines a member called '" +
4061 name + "' with the same parameter types (more than one default constructor)");
4064 case DeclSpace.AdditionResult.EnclosingClash:
4065 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
4068 case DeclSpace.AdditionResult.NotAConstructor:
4069 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
4072 case DeclSpace.AdditionResult.Error:
4073 // Error has already been reported.
4079 CheckDef (bool result, string name, Location l)
4083 CheckDef (DeclSpace.AdditionResult.NameExists, name, l);
4086 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4088 Block implicit_block;
4089 ArrayList inits = null;
4092 // We use the `Used' property to check whether statements
4093 // have been added to the current block. If so, we need
4094 // to create another block to contain the new declaration
4095 // otherwise, as an optimization, we use the same block to
4096 // add the declaration.
4098 // FIXME: A further optimization is to check if the statements
4099 // that were added were added as part of the initialization
4100 // below. In which case, no other statements have been executed
4101 // and we might be able to reduce the number of blocks for
4102 // situations like this:
4104 // int j = 1; int k = j + 1;
4106 if (current_block.Used)
4107 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4109 implicit_block = current_block;
4111 foreach (VariableDeclaration decl in variable_declarators){
4113 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4114 if (decl.expression_or_array_initializer != null){
4116 inits = new ArrayList (4);
4123 return implicit_block;
4125 foreach (VariableDeclaration decl in inits){
4129 if (decl.expression_or_array_initializer is Expression){
4130 expr = (Expression) decl.expression_or_array_initializer;
4133 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4135 expr = new ArrayCreation (type, "", init, decl.Location);
4138 LocalVariableReference var;
4139 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4141 assign = new Assign (var, expr, decl.Location);
4143 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4146 return implicit_block;
4149 Block declare_local_constants (Expression type, ArrayList declarators)
4151 Block implicit_block;
4153 if (current_block.Used)
4154 implicit_block = new Block (current_block, Block.Flags.Implicit);
4156 implicit_block = current_block;
4158 foreach (VariableDeclaration decl in declarators){
4159 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4160 current_local_parameters, decl.Location);
4163 return implicit_block;
4166 void CheckAttributeTarget (string a)
4170 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4174 Location l = lexer.Location;
4175 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4181 void CheckUnaryOperator (Operator.OpType op)
4185 case Operator.OpType.LogicalNot:
4186 case Operator.OpType.OnesComplement:
4187 case Operator.OpType.Increment:
4188 case Operator.OpType.Decrement:
4189 case Operator.OpType.True:
4190 case Operator.OpType.False:
4191 case Operator.OpType.Addition:
4192 case Operator.OpType.Subtraction:
4197 Location l = lexer.Location;
4198 Report.Error (1019, l, "Overloadable unary operator expected");
4204 void CheckBinaryOperator (Operator.OpType op)
4208 case Operator.OpType.Addition:
4209 case Operator.OpType.Subtraction:
4210 case Operator.OpType.Multiply:
4211 case Operator.OpType.Division:
4212 case Operator.OpType.Modulus:
4213 case Operator.OpType.BitwiseAnd:
4214 case Operator.OpType.BitwiseOr:
4215 case Operator.OpType.ExclusiveOr:
4216 case Operator.OpType.LeftShift:
4217 case Operator.OpType.RightShift:
4218 case Operator.OpType.Equality:
4219 case Operator.OpType.Inequality:
4220 case Operator.OpType.GreaterThan:
4221 case Operator.OpType.LessThan:
4222 case Operator.OpType.GreaterThanOrEqual:
4223 case Operator.OpType.LessThanOrEqual:
4227 Location l = lexer.Location;
4228 Report.Error (1020, l, "Overloadable binary operator expected");
4234 void syntax_error (Location l, string msg)
4236 Report.Error (1003, l, "Syntax error, " + msg);
4239 void output (string s)
4241 Console.WriteLine (s);
4244 void note (string s)
4246 // Used to put annotations
4251 public Tokenizer Lexer {
4257 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4259 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4260 this.name = file.Name;
4262 current_container = RootContext.Tree.Types;
4263 current_container.NamespaceEntry = current_namespace;
4264 oob_stack = new Stack ();
4265 switch_stack = new Stack ();
4267 lexer = new Tokenizer (reader, file, defines);
4270 public override void parse ()
4273 if (yacc_verbose_flag)
4274 yyparse (lexer, new yydebug.yyDebugSimple ());
4277 Tokenizer tokenizer = lexer as Tokenizer;
4278 tokenizer.cleanup ();
4279 } catch (Exception e){
4280 // Please do not remove this, it is used during debugging
4283 Console.WriteLine (e);
4284 Report.Error (-25, lexer.Location, "Parsing error");
4285 if (Driver.parser_verbose)
4286 Console.WriteLine (e);
4290 void CheckToken (int error, int yyToken, string msg)
4292 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4293 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4296 Report.Error (error, lexer.Location, msg);
4299 void CheckIdentifierToken (int yyToken)
4301 CheckToken (1041, yyToken, "Identifier expected");