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 current_namespace = RootContext.Tree.RecordNamespace (current_namespace, file, $3.ToString (), lexer.Location);
350 namespace_body opt_semicolon
352 current_namespace = current_namespace.Parent;
367 : namespace_or_type_name { $$ = ($1).ToString (); }
373 opt_namespace_member_declarations
384 opt_namespace_member_declarations
386 | namespace_member_declarations
389 namespace_member_declarations
390 : namespace_member_declaration
391 | namespace_member_declarations namespace_member_declaration
394 namespace_member_declaration
401 Class c = (Class) $1;
402 mod_flags = c.ModFlags;
404 } else if ($1 is Struct){
405 Struct s = (Struct) $1;
406 mod_flags = s.ModFlags;
411 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
413 1527, lexer.Location,
414 "Namespace elements cant be explicitly " +
415 "declared private or protected in `" + name + "'");
417 current_namespace.DeclarationFound = true;
419 | namespace_declaration {
420 current_namespace.DeclarationFound = true;
423 | field_declaration {
424 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
426 | method_declaration {
427 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
434 | interface_declaration
436 | delegate_declaration
438 // Enable this when we have handled all errors, because this acts as a generic fallback
441 // Console.WriteLine ("Token=" + yyToken);
442 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
454 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
462 global_attrs_enabled = false;
467 global_attrs_enabled = false;
476 ArrayList sect = (ArrayList) $1;
478 if (global_attrs_enabled) {
479 if (current_attr_target == "module") {
480 CodeGen.Module.AddAttributes (sect);
482 } else if (current_attr_target == "assembly") {
483 CodeGen.Assembly.AddAttributes (sect);
486 $$ = new Attributes (sect);
489 $$ = new Attributes (sect);
491 current_attr_target = null;
493 | attribute_sections attribute_section
495 Attributes attrs = $1 as Attributes;
496 ArrayList sect = (ArrayList) $2;
498 if (global_attrs_enabled) {
499 if (current_attr_target == "module") {
500 CodeGen.Module.AddAttributes (sect);
502 } else if (current_attr_target == "assembly") {
503 CodeGen.Assembly.AddAttributes (sect);
507 attrs = new Attributes (sect);
509 attrs.AddAttributes (sect);
513 attrs = new Attributes (sect);
515 attrs.AddAttributes (sect);
518 current_attr_target = null;
523 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
527 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
533 attribute_target_specifier
534 : attribute_target COLON
536 current_attr_target = (string)$1;
544 CheckAttributeTarget ((string) $1);
547 | EVENT { $$ = "event"; }
548 | RETURN { $$ = "return"; }
554 ArrayList attrs = new ArrayList (4);
560 | attribute_list COMMA attribute
562 ArrayList attrs = (ArrayList) $1;
574 opt_attribute_arguments
576 if (current_attr_target == "assembly" || current_attr_target == "module")
577 $$ = new GlobalAttribute (current_container, current_attr_target, (string) ($1).ToString (), (ArrayList) $3, (Location) $2);
579 $$ = new Attribute (current_attr_target, (string) ($1).ToString (), (ArrayList) $3, (Location) $2);
584 : type_name { /* reserved attribute name or identifier: 17.4 */ }
587 opt_attribute_arguments
588 : /* empty */ { $$ = null; }
589 | OPEN_PARENS attribute_arguments CLOSE_PARENS
597 : opt_positional_argument_list
602 ArrayList args = new ArrayList (4);
608 | positional_argument_list COMMA named_argument_list
610 ArrayList args = new ArrayList (4);
616 | named_argument_list
618 ArrayList args = new ArrayList (4);
627 opt_positional_argument_list
628 : /* empty */ { $$ = null; }
629 | positional_argument_list
632 positional_argument_list
635 ArrayList args = new ArrayList (4);
636 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
640 | positional_argument_list COMMA expression
642 ArrayList args = (ArrayList) $1;
643 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
652 ArrayList args = new ArrayList (4);
657 | named_argument_list COMMA named_argument
659 ArrayList args = (ArrayList) $1;
667 : IDENTIFIER ASSIGN expression
669 $$ = new DictionaryEntry (
671 new Argument ((Expression) $3, Argument.AType.Expression));
677 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
680 opt_class_member_declarations
682 | class_member_declarations
685 class_member_declarations
686 : class_member_declaration
687 | class_member_declarations
688 class_member_declaration
691 class_member_declaration
692 : constant_declaration // done
693 | field_declaration // done
694 | method_declaration // done
695 | property_declaration // done
696 | event_declaration // done
697 | indexer_declaration // done
698 | operator_declaration // done
699 | constructor_declaration // done
700 | destructor_declaration // done
710 string full_struct_name = MakeName ((string) $4);
712 new_struct = new Struct (current_namespace, current_container, full_struct_name,
713 (int) $2, (Attributes) $1, lexer.Location);
714 current_container = new_struct;
715 RootContext.Tree.RecordDecl (full_struct_name, new_struct);
721 Struct new_struct = (Struct) current_container;
724 new_struct.Bases = (ArrayList) $6;
726 current_container = current_container.Parent;
727 CheckDef (current_container.AddStruct (new_struct), new_struct.Name, new_struct.Location);
730 | opt_attributes opt_modifiers STRUCT error {
731 CheckIdentifierToken (yyToken);
736 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
739 opt_struct_member_declarations
741 | struct_member_declarations
744 struct_member_declarations
745 : struct_member_declaration
746 | struct_member_declarations struct_member_declaration
749 struct_member_declaration
750 : constant_declaration
753 | property_declaration
755 | indexer_declaration
756 | operator_declaration
757 | constructor_declaration
761 * This is only included so we can flag error 575:
762 * destructors only allowed on class types
764 | destructor_declaration
775 foreach (VariableDeclaration constant in (ArrayList) $5){
776 Location l = constant.Location;
778 Const c = new Const (
779 (Expression) $4, (string) constant.identifier,
780 (Expression) constant.expression_or_array_initializer, (int) $2,
783 CheckDef (current_container.AddConstant (c), c.Name, l);
789 : constant_declarator
791 ArrayList constants = new ArrayList (4);
795 | constant_declarators COMMA constant_declarator
797 ArrayList constants = (ArrayList) $1;
804 : IDENTIFIER ASSIGN constant_expression
806 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
817 Expression type = (Expression) $3;
820 foreach (VariableDeclaration var in (ArrayList) $4){
821 Location l = var.Location;
823 Field field = new Field (type, mod, var.identifier,
824 var.expression_or_array_initializer,
827 CheckDef (current_container.AddField (field), field.Name, l);
835 Report.Error (670, lexer.Location, "void type is not allowed for fields");
840 : variable_declarator
842 ArrayList decl = new ArrayList (4);
846 | variable_declarators COMMA variable_declarator
848 ArrayList decls = (ArrayList) $1;
855 : IDENTIFIER ASSIGN variable_initializer
857 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
861 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
874 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
876 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
882 iterator_container = (IIteratorContainer) $1;
886 Method method = (Method) $1;
887 Block b = (Block) $3;
888 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
891 if ((method.ModFlags & extern_abstract) == 0){
893 501, lexer.Location, current_container.MakeName (method.Name) +
894 "must declare a body because it is not marked abstract or extern");
897 if ((method.ModFlags & Modifiers.EXTERN) != 0){
899 179, lexer.Location, current_container.MakeName (method.Name) +
900 " is declared extern, but has a body");
904 method.Block = (Block) $3;
905 CheckDef (current_container.AddMethod (method), method.Name, method.Location);
907 current_local_parameters = null;
908 iterator_container = null;
922 1585, lexer.Location, "Member modifier `" +
923 Modifiers.Name (i) + "' must precede member type and name");
936 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
938 Method method = new Method (current_container, (Expression) $3, (int) $2,
939 false, (string) $4, (Parameters) $6,
940 (Attributes) $1, lexer.Location);
942 current_local_parameters = (Parameters) $6;
950 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
952 Method method = new Method (current_container, TypeManager.system_void_expr,
953 (int) $2, false, (string) $4, (Parameters) $6,
954 (Attributes) $1, lexer.Location);
956 current_local_parameters = (Parameters) $6;
962 modifiers member_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
964 Report.Error (1585, lexer.Location,
965 String.Format ("Modifier {0} should appear before type",
966 Modifiers.Name ((int) $4)));
967 Method method = new Method (current_container, TypeManager.system_void_expr,
968 0, false, (string) $5, (Parameters) $7,
969 (Attributes) $1, lexer.Location);
971 current_local_parameters = (Parameters) $7;
978 | SEMICOLON { $$ = null; }
981 opt_formal_parameter_list
982 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
983 | formal_parameter_list
986 formal_parameter_list
989 ArrayList pars_list = (ArrayList) $1;
991 Parameter [] pars = new Parameter [pars_list.Count];
992 pars_list.CopyTo (pars);
994 $$ = new Parameters (pars, null, lexer.Location);
996 | fixed_parameters COMMA parameter_array
998 ArrayList pars_list = (ArrayList) $1;
1000 Parameter [] pars = new Parameter [pars_list.Count];
1001 pars_list.CopyTo (pars);
1003 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
1005 | fixed_parameters COMMA ARGLIST
1007 ArrayList pars_list = (ArrayList) $1;
1009 Parameter [] pars = new Parameter [pars_list.Count];
1010 pars_list.CopyTo (pars);
1012 $$ = new Parameters (pars, true, lexer.Location);
1016 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
1020 $$ = new Parameters (null, true, lexer.Location);
1027 ArrayList pars = new ArrayList (4);
1032 | fixed_parameters COMMA fixed_parameter
1034 ArrayList pars = (ArrayList) $1;
1043 opt_parameter_modifier
1047 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
1050 opt_parameter_modifier
1053 CheckIdentifierToken (yyToken);
1058 opt_parameter_modifier
1059 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1060 | parameter_modifier
1064 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1065 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1069 : opt_attributes PARAMS type IDENTIFIER
1071 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1072 note ("type must be a single-dimension array type");
1074 | opt_attributes PARAMS type error {
1075 CheckIdentifierToken (yyToken);
1081 : namespace_or_type_name { $$ = ($1).ToString (); }
1084 property_declaration
1090 implicit_value_parameter_type = (Expression) $3;
1092 lexer.PropertyParsing = true;
1094 $$ = lexer.Location;
1096 iterator_container = SimpleIteratorContainer.GetSimple ();
1098 accessor_declarations
1100 lexer.PropertyParsing = false;
1105 Pair pair = (Pair) $7;
1106 Accessor get_block = (Accessor) pair.First;
1107 Accessor set_block = (Accessor) pair.Second;
1109 Location loc = (Location) $6;
1110 prop = new Property (current_container, (Expression) $3, (int) $2, false,
1111 (string) $4, (Attributes) $1, get_block, set_block, loc);
1112 if (SimpleIteratorContainer.Simple.Yields)
1115 CheckDef (current_container.AddProperty (prop), prop.Name, loc);
1116 implicit_value_parameter_type = null;
1117 iterator_container = null;
1121 accessor_declarations
1122 : get_accessor_declaration opt_set_accessor_declaration
1124 $$ = new Pair ($1, $2);
1126 | set_accessor_declaration opt_get_accessor_declaration
1128 $$ = new Pair ($2, $1);
1132 opt_get_accessor_declaration
1133 : /* empty */ { $$ = null; }
1134 | get_accessor_declaration
1137 opt_set_accessor_declaration
1138 : /* empty */ { $$ = null; }
1139 | set_accessor_declaration
1142 get_accessor_declaration
1143 : opt_attributes GET
1145 // If this is not the case, then current_local_parameters has already
1146 // been set in indexer_declaration
1147 if (parsing_indexer == false)
1148 current_local_parameters = null;
1150 current_local_parameters = indexer_parameters;
1151 lexer.PropertyParsing = false;
1155 $$ = new Accessor ((Block) $4, (Attributes) $1);
1156 current_local_parameters = null;
1157 lexer.PropertyParsing = true;
1161 set_accessor_declaration
1162 : opt_attributes SET
1165 Parameter implicit_value_parameter = new Parameter (
1166 implicit_value_parameter_type, "value",
1167 Parameter.Modifier.NONE, null);
1169 if (parsing_indexer == false) {
1170 args = new Parameter [1];
1171 args [0] = implicit_value_parameter;
1172 current_local_parameters = new Parameters (args, null, lexer.Location);
1174 Parameter [] fpars = indexer_parameters.FixedParameters;
1177 int count = fpars.Length;
1179 args = new Parameter [count + 1];
1180 fpars.CopyTo (args, 0);
1181 args [count] = implicit_value_parameter;
1184 current_local_parameters = new Parameters (
1185 args, indexer_parameters.ArrayParameter, lexer.Location);
1188 lexer.PropertyParsing = false;
1192 $$ = new Accessor ((Block) $4, (Attributes) $1);
1193 current_local_parameters = null;
1194 lexer.PropertyParsing = true;
1200 | SEMICOLON { $$ = null; }
1203 interface_declaration
1206 INTERFACE IDENTIFIER
1208 Interface new_interface;
1209 string full_interface_name = MakeName ((string) $4);
1211 new_interface = new Interface (current_namespace, current_container, full_interface_name,
1212 (int) $2, (Attributes) $1, lexer.Location);
1213 if (current_interface != null) {
1214 Location l = lexer.Location;
1215 Report.Error (-2, l, "Internal compiler error: interface inside interface");
1217 current_interface = new_interface;
1218 current_container = new_interface;
1219 RootContext.Tree.RecordDecl (full_interface_name, new_interface);
1222 interface_body opt_semicolon
1224 Interface new_interface = (Interface) current_interface;
1227 new_interface.Bases = (ArrayList) $6;
1229 current_interface = null;
1230 current_container = current_container.Parent;
1231 CheckDef (current_container.AddInterface (new_interface),
1232 new_interface.Name, new_interface.Location);
1234 | opt_attributes opt_modifiers INTERFACE error {
1235 CheckIdentifierToken (yyToken);
1241 opt_interface_member_declarations
1245 opt_interface_member_declarations
1247 | interface_member_declarations
1250 interface_member_declarations
1251 : interface_member_declaration
1252 | interface_member_declarations interface_member_declaration
1255 interface_member_declaration
1256 : interface_method_declaration
1258 Method m = (Method) $1;
1260 CheckDef (current_interface.AddMethod (m), m.Name, m.Location);
1262 | interface_property_declaration
1264 Property p = (Property) $1;
1266 CheckDef (current_interface.AddProperty (p), p.Name, p.Location);
1268 | interface_event_declaration
1271 Event e = (Event) $1;
1272 CheckDef (current_interface.AddEvent (e), e.Name, lexer.Location);
1275 | interface_indexer_declaration
1277 Indexer i = (Indexer) $1;
1279 current_interface.AddIndexer (i);
1287 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, lexer.Location);
1292 interface_method_declaration
1293 : opt_attributes opt_new type IDENTIFIER
1294 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1297 $$ = new Method (current_container, (Expression) $3, (int) $2, true,
1298 (string) $4, (Parameters) $6, (Attributes) $1,
1301 | opt_attributes opt_new VOID IDENTIFIER
1302 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1305 $$ = new Method (current_container, TypeManager.system_void_expr, (int) $2,
1306 true, (string) $4, (Parameters) $6, (Attributes) $1,
1311 interface_property_declaration
1316 { lexer.PropertyParsing = true; }
1318 { lexer.PropertyParsing = false; }
1321 InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
1323 $$ = new Property (current_container, (Expression) $3, (int) $2, true,
1324 (string) $4, (Attributes) $1, pinfo.Get, pinfo.Set,
1330 CheckIdentifierToken (yyToken);
1336 : opt_attributes GET SEMICOLON { $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null); }
1337 | opt_attributes SET SEMICOLON { $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1); }
1338 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1339 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $3); }
1340 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1341 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $3, (Attributes) $1); }
1344 interface_event_declaration
1345 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1347 $$ = new EventField (current_container, (Expression) $4, (int) $2, true, (string) $5, null,
1348 (Attributes) $1, lexer.Location);
1350 | opt_attributes opt_new EVENT type error {
1351 CheckIdentifierToken (yyToken);
1354 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1355 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1358 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE event_accessor_declarations CLOSE_BRACE {
1359 Report.Error (69, lexer.Location, "Event accessors not valid on interfaces");
1364 interface_indexer_declaration
1365 : opt_attributes opt_new type THIS
1366 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1368 { lexer.PropertyParsing = true; }
1370 { lexer.PropertyParsing = false; }
1373 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1375 $$ = new Indexer (current_container, (Expression) $3, null, (int) $2, true,
1376 (Parameters) $6, (Attributes) $1, info.Get, info.Set,
1381 operator_declaration
1382 : opt_attributes opt_modifiers operator_declarator
1384 iterator_container = SimpleIteratorContainer.GetSimple ();
1388 OperatorDeclaration decl = (OperatorDeclaration) $3;
1390 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
1391 decl.arg2type, decl.arg2name, (Block) $5, (Attributes) $1, decl.location);
1393 if (SimpleIteratorContainer.Simple.Yields)
1396 // Note again, checking is done in semantic analysis
1397 current_container.AddOperator (op);
1399 current_local_parameters = null;
1400 iterator_container = null;
1406 | SEMICOLON { $$ = null; }
1409 : type OPERATOR overloadable_operator
1410 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1412 Operator.OpType op = (Operator.OpType) $3;
1413 CheckUnaryOperator (op);
1415 if (op == Operator.OpType.Addition)
1416 op = Operator.OpType.UnaryPlus;
1418 if (op == Operator.OpType.Subtraction)
1419 op = Operator.OpType.UnaryNegation;
1421 Parameter [] pars = new Parameter [1];
1423 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1425 current_local_parameters = new Parameters (pars, null, lexer.Location);
1427 $$ = new OperatorDeclaration (op, (Expression) $1, (Expression) $5, (string) $6,
1428 null, null, lexer.Location);
1430 | type OPERATOR overloadable_operator
1432 type IDENTIFIER COMMA
1436 CheckBinaryOperator ((Operator.OpType) $3);
1438 Parameter [] pars = new Parameter [2];
1440 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1441 pars [1] = new Parameter ((Expression) $8, (string) $9, Parameter.Modifier.NONE, null);
1443 current_local_parameters = new Parameters (pars, null, lexer.Location);
1445 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1446 (Expression) $5, (string) $6,
1447 (Expression) $8, (string) $9, lexer.Location);
1449 | conversion_operator_declarator
1452 overloadable_operator
1454 : BANG { $$ = Operator.OpType.LogicalNot; }
1455 | TILDE { $$ = Operator.OpType.OnesComplement; }
1456 | OP_INC { $$ = Operator.OpType.Increment; }
1457 | OP_DEC { $$ = Operator.OpType.Decrement; }
1458 | TRUE { $$ = Operator.OpType.True; }
1459 | FALSE { $$ = Operator.OpType.False; }
1460 // Unary and binary:
1461 | PLUS { $$ = Operator.OpType.Addition; }
1462 | MINUS { $$ = Operator.OpType.Subtraction; }
1464 | STAR { $$ = Operator.OpType.Multiply; }
1465 | DIV { $$ = Operator.OpType.Division; }
1466 | PERCENT { $$ = Operator.OpType.Modulus; }
1467 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1468 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1469 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1470 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1471 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1472 | OP_EQ { $$ = Operator.OpType.Equality; }
1473 | OP_NE { $$ = Operator.OpType.Inequality; }
1474 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1475 | OP_LT { $$ = Operator.OpType.LessThan; }
1476 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1477 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1480 conversion_operator_declarator
1481 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1483 Parameter [] pars = new Parameter [1];
1485 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1487 current_local_parameters = new Parameters (pars, null, lexer.Location);
1489 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1490 null, null, lexer.Location);
1492 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1494 Parameter [] pars = new Parameter [1];
1496 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1498 current_local_parameters = new Parameters (pars, null, lexer.Location);
1500 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1501 null, null, lexer.Location);
1505 syntax_error (lexer.Location, "'operator' expected");
1509 syntax_error (lexer.Location, "'operator' expected");
1513 constructor_declaration
1516 constructor_declarator
1519 Constructor c = (Constructor) $3;
1520 c.Block = (Block) $4;
1521 c.OptAttributes = (Attributes) $1;
1522 c.ModFlags = (int) $2;
1524 if (c.Name == current_container.Basename){
1525 if ((c.ModFlags & Modifiers.STATIC) != 0){
1526 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1528 515, c.Location, String.Format (
1529 "`{0}.{1}': static constructor can not have access modifiers",
1530 c.Name, current_container.Name));
1533 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1535 if (c.Initializer != null){
1538 "Static constructors can not have an explicit this or base " +
1539 "constructor invocations");
1542 if (!c.Parameters.Empty){
1544 132, c.Location, "Static constructors should not have parameters");
1547 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1550 // We let another layer check the validity of the constructor.
1551 Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1554 CheckDef (current_container.AddConstructor (c), c.Name, c.Location);
1556 current_local_parameters = null;
1560 constructor_declarator
1562 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1564 oob_stack.Push (lexer.Location);
1566 current_local_parameters = (Parameters) $3;
1568 opt_constructor_initializer
1570 Location l = (Location) oob_stack.Pop ();
1571 $$ = new Constructor (current_container, (string) $1, 0, (Parameters) $3,
1572 (ConstructorInitializer) $6, l);
1578 | SEMICOLON { $$ = null; }
1581 opt_constructor_initializer
1582 : /* empty */ { $$ = null; }
1583 | constructor_initializer
1586 constructor_initializer
1587 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1589 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1591 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1593 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1596 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1602 : /* EMPTY */ { $$ = 0; }
1603 | UNSAFE { $$ = Modifiers.UNSAFE; }
1604 | EXTERN { $$ = Modifiers.EXTERN; }
1607 destructor_declaration
1608 : opt_attributes opt_finalizer TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1610 if ((string) $4 != current_container.Basename){
1611 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1612 } else if (!(current_container is Class)){
1613 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1615 Location l = lexer.Location;
1618 if (!RootContext.StdLib && current_container.Name == "System.Object")
1619 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
1621 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
1623 if ((m & Modifiers.UNSAFE) != 0){
1624 if (!RootContext.Unsafe){
1625 Report.Error (227, l,
1626 "Unsafe code requires the -unsafe command " +
1627 "line option to be specified");
1631 Method d = new Destructor (
1632 current_container, TypeManager.system_void_expr, m, "Finalize",
1633 new Parameters (null, null, l), (Attributes) $1, l);
1635 d.Block = (Block) $7;
1636 CheckDef (current_container.AddMethod (d), d.Name, d.Location);
1644 EVENT type variable_declarators SEMICOLON
1646 foreach (VariableDeclaration var in (ArrayList) $5) {
1648 Event e = new EventField (current_container, (Expression) $4, (int) $2, false, var.identifier,
1649 var.expression_or_array_initializer,
1650 (Attributes) $1, lexer.Location);
1652 CheckDef (current_container.AddEvent (e), e.Name, e.Location);
1658 EVENT type member_name
1661 implicit_value_parameter_type = (Expression) $4;
1662 lexer.EventParsing = true;
1663 oob_stack.Push (lexer.Location);
1665 event_accessor_declarations
1667 lexer.EventParsing = false;
1671 Location loc = (Location) oob_stack.Pop ();
1674 Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
1677 Pair pair = (Pair) $8;
1679 Event e = new EventProperty (current_container, (Expression) $4, (int) $2, false, (string) $5,
1680 null, (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
1683 CheckDef (current_container.AddEvent (e), e.Name, loc);
1684 implicit_value_parameter_type = null;
1687 | opt_attributes opt_modifiers EVENT type member_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS block {
1688 string mn = (string) $5;
1690 if (mn.IndexOf ('.') != -1)
1691 Report.Error (71, lexer.Location, "Explicit implementation of events requires property syntax");
1693 Report.Error (71, lexer.Location, "Event declaration should use property syntax");
1697 event_accessor_declarations
1698 : add_accessor_declaration remove_accessor_declaration
1700 $$ = new Pair ($1, $2);
1702 | remove_accessor_declaration add_accessor_declaration
1704 $$ = new Pair ($2, $1);
1706 | add_accessor_declaration { $$ = null; }
1707 | remove_accessor_declaration { $$ = null; }
1710 add_accessor_declaration
1711 : opt_attributes ADD
1713 Parameter [] args = new Parameter [1];
1714 Parameter implicit_value_parameter = new Parameter (
1715 implicit_value_parameter_type, "value",
1716 Parameter.Modifier.NONE, null);
1718 args [0] = implicit_value_parameter;
1720 current_local_parameters = new Parameters (args, null, lexer.Location);
1721 lexer.EventParsing = false;
1725 $$ = new Accessor ((Block) $4, (Attributes) $1);
1726 lexer.EventParsing = true;
1728 | opt_attributes ADD error {
1729 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1734 remove_accessor_declaration
1735 : opt_attributes REMOVE
1737 Parameter [] args = new Parameter [1];
1738 Parameter implicit_value_parameter = new Parameter (
1739 implicit_value_parameter_type, "value",
1740 Parameter.Modifier.NONE, null);
1742 args [0] = implicit_value_parameter;
1744 current_local_parameters = new Parameters (args, null, lexer.Location);
1745 lexer.EventParsing = false;
1749 $$ = new Accessor ((Block) $4, (Attributes) $1);
1750 lexer.EventParsing = true;
1752 | opt_attributes REMOVE error {
1753 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1759 : opt_attributes opt_modifiers indexer_declarator
1762 IndexerDeclaration decl = (IndexerDeclaration) $3;
1764 implicit_value_parameter_type = decl.type;
1766 lexer.PropertyParsing = true;
1767 parsing_indexer = true;
1769 indexer_parameters = decl.param_list;
1770 oob_stack.Push (lexer.Location);
1772 accessor_declarations
1774 lexer.PropertyParsing = false;
1775 parsing_indexer = false;
1779 // The signature is computed from the signature of the indexer. Look
1780 // at section 3.6 on the spec
1781 Location loc = (Location) oob_stack.Pop ();
1783 IndexerDeclaration decl = (IndexerDeclaration) $3;
1784 Pair pair = (Pair) $6;
1785 Accessor get_block = (Accessor) pair.First;
1786 Accessor set_block = (Accessor) pair.Second;
1788 indexer = new Indexer (current_container, decl.type, decl.interface_type,
1789 (int) $2, false, decl.param_list, (Attributes) $1,
1790 get_block, set_block, loc);
1792 // Note that there is no equivalent of CheckDef for this case
1793 // We shall handle this in semantic analysis
1795 current_container.AddIndexer (indexer);
1797 current_local_parameters = null;
1798 implicit_value_parameter_type = null;
1799 indexer_parameters = null;
1804 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1806 Parameters pars = (Parameters) $4;
1808 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1809 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1812 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
1814 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1816 Parameters pars = (Parameters) $6;
1818 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1819 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1821 $$ = new IndexerDeclaration ((Expression) $1, $2.ToString (), pars);
1833 Location enum_location = lexer.Location;
1835 string full_name = MakeName ((string) $4);
1836 Enum e = new Enum (current_namespace, current_container, (Expression) $5, (int) $2,
1837 full_name, (Attributes) $1, enum_location);
1839 foreach (VariableDeclaration ev in (ArrayList) $6) {
1840 Location loc = (Location) ev.Location;
1842 CheckDef (e.AddEnumMember (ev.identifier,
1843 (Expression) ev.expression_or_array_initializer,
1844 loc, ev.OptAttributes),
1845 ev.identifier, loc);
1848 CheckDef (current_container.AddEnum (e), full_name, enum_location);
1849 RootContext.Tree.RecordDecl (full_name, e);
1855 : /* empty */ { $$ = TypeManager.system_int32_expr; }
1856 | COLON type { $$ = $2; }
1860 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1866 opt_enum_member_declarations
1867 : /* empty */ { $$ = new ArrayList (4); }
1868 | enum_member_declarations opt_comma { $$ = $1; }
1871 enum_member_declarations
1872 : enum_member_declaration
1874 ArrayList l = new ArrayList (4);
1879 | enum_member_declarations COMMA enum_member_declaration
1881 ArrayList l = (ArrayList) $1;
1889 enum_member_declaration
1890 : opt_attributes IDENTIFIER
1892 $$ = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
1894 | opt_attributes IDENTIFIER
1896 $$ = lexer.Location;
1900 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
1904 delegate_declaration
1908 IDENTIFIER OPEN_PARENS
1909 opt_formal_parameter_list
1913 Location l = lexer.Location;
1914 Delegate del = new Delegate (current_namespace, current_container, (Expression) $4,
1915 (int) $2, MakeName ((string) $5), (Parameters) $7,
1916 (Attributes) $1, l);
1918 CheckDef (current_container.AddDelegate (del), del.Name, l);
1923 IDENTIFIER OPEN_PARENS
1924 opt_formal_parameter_list
1928 Location l = lexer.Location;
1929 Delegate del = new Delegate (
1930 current_namespace, current_container,
1931 TypeManager.system_void_expr, (int) $2, MakeName ((string) $5),
1932 (Parameters) $7, (Attributes) $1, l);
1934 CheckDef (current_container.AddDelegate (del), del.Name, l);
1939 : namespace_or_type_name
1942 namespace_or_type_name
1943 : IDENTIFIER { $$ = new SimpleName ((string) $1, lexer.Location);}
1944 | namespace_or_type_name DOT IDENTIFIER {
1945 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1950 * Before you think of adding a return_type, notice that we have been
1951 * using two rules in the places where it matters (one rule using type
1952 * and another identical one that uses VOID as the return type). This
1953 * gets rid of a shift/reduce couple
1956 : type_name { /* class_type */
1958 This does interfaces, delegates, struct_types, class_types,
1959 parent classes, and more! 4.2
1972 // Note that here only unmanaged types are allowed but we
1973 // can't perform checks during this phase - we do it during
1974 // semantic analysis.
1976 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1980 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
1986 | non_expression_type rank_specifier
1988 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1990 | non_expression_type STAR
1992 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1994 | expression rank_specifiers
1996 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2000 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2004 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2005 // through this different path
2007 | multiplicative_expression STAR
2009 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2016 ArrayList types = new ArrayList (4);
2021 | type_list COMMA type
2023 ArrayList types = (ArrayList) $1;
2031 * replaces all the productions for isolating the various
2032 * simple types, but we need this to reuse it easily in local_variable_type
2035 : OBJECT { $$ = TypeManager.system_object_expr; }
2036 | STRING { $$ = TypeManager.system_string_expr; }
2037 | BOOL { $$ = TypeManager.system_boolean_expr; }
2038 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2039 | FLOAT { $$ = TypeManager.system_single_expr; }
2040 | DOUBLE { $$ = TypeManager.system_double_expr; }
2045 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2046 | BYTE { $$ = TypeManager.system_byte_expr; }
2047 | SHORT { $$ = TypeManager.system_int16_expr; }
2048 | USHORT { $$ = TypeManager.system_uint16_expr; }
2049 | INT { $$ = TypeManager.system_int32_expr; }
2050 | UINT { $$ = TypeManager.system_uint32_expr; }
2051 | LONG { $$ = TypeManager.system_int64_expr; }
2052 | ULONG { $$ = TypeManager.system_uint64_expr; }
2053 | CHAR { $$ = TypeManager.system_char_expr; }
2054 | VOID { $$ = TypeManager.system_void_expr; }
2062 : type rank_specifiers
2064 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2069 // Expressions, section 7.5
2077 | IDENTIFIER { $$ = new SimpleName ((string) $1, lexer.Location); }
2078 | parenthesized_expression
2080 | invocation_expression
2084 | post_increment_expression
2085 | post_decrement_expression
2089 | checked_expression
2090 | unchecked_expression
2091 | pointer_member_access
2092 | anonymous_method_expression
2099 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2100 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2101 | NULL { $$ = NullLiteral.Null; }
2105 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2106 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2107 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2112 object v = lexer.Value;
2118 $$ = IntLiteral.Zero;
2120 $$ = IntLiteral.One;
2122 $$ = new IntLiteral (i);
2123 } else if (v is uint)
2124 $$ = new UIntLiteral ((UInt32) v);
2126 $$ = new LongLiteral ((Int64) v);
2127 else if (v is ulong)
2128 $$ = new ULongLiteral ((UInt64) v);
2130 Console.WriteLine ("OOPS. Unexpected result from scanner");
2135 : TRUE { $$ = new BoolLiteral (true); }
2136 | FALSE { $$ = new BoolLiteral (false); }
2139 parenthesized_expression_0
2140 : OPEN_PARENS expression CLOSE_PARENS
2143 lexer.Deambiguate_CloseParens ();
2144 // After this, the next token returned is one of
2145 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2146 // or CLOSE_PARENS_MINUS.
2150 parenthesized_expression
2151 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2155 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2157 // If a parenthesized expression is followed by a minus, we need to wrap
2158 // the expression inside a ParenthesizedExpression for the CS0075 check
2159 // in Binary.DoResolve().
2160 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2165 : primary_expression DOT IDENTIFIER
2167 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2169 | predefined_type DOT IDENTIFIER
2171 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2179 invocation_expression
2180 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2183 Location l = lexer.Location;
2184 Report.Error (1, l, "Parse error");
2186 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2188 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2190 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2192 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2194 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2199 : /* empty */ { $$ = null; }
2206 ArrayList list = new ArrayList (4);
2210 | argument_list COMMA argument
2212 ArrayList list = (ArrayList) $1;
2216 | argument_list error {
2217 CheckToken (1026, yyToken, ", or ) expected");
2224 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2226 | REF variable_reference
2228 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2230 | OUT variable_reference
2232 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2234 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2236 ArrayList list = (ArrayList) $3;
2237 Argument[] args = new Argument [list.Count];
2238 list.CopyTo (args, 0);
2240 Expression expr = new Arglist (args, lexer.Location);
2241 $$ = new Argument (expr, Argument.AType.Expression);
2245 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2250 : expression { note ("section 5.4"); $$ = $1; }
2254 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2256 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2258 | primary_expression rank_specifiers
2260 // So the super-trick is that primary_expression
2261 // can only be either a SimpleName or a MemberAccess.
2262 // The MemberAccess case arises when you have a fully qualified type-name like :
2264 // SimpleName is when you have
2267 Expression expr = (Expression) $1;
2268 if (expr is ComposedCast){
2269 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2270 } else if (!(expr is SimpleName || expr is MemberAccess)){
2271 Error_ExpectingTypeName (lexer.Location, expr);
2272 $$ = TypeManager.system_object_expr;
2275 // So we extract the string corresponding to the SimpleName
2278 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2286 ArrayList list = new ArrayList (4);
2290 | expression_list COMMA expression
2292 ArrayList list = (ArrayList) $1;
2301 $$ = new This (current_block, lexer.Location);
2306 : BASE DOT IDENTIFIER
2308 $$ = new BaseAccess ((string) $3, lexer.Location);
2310 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2312 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2315 Report.Error (175, "Use of keyword `base' is not valid in this context");
2320 post_increment_expression
2321 : primary_expression OP_INC
2323 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2324 (Expression) $1, lexer.Location);
2328 post_decrement_expression
2329 : primary_expression OP_DEC
2331 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2332 (Expression) $1, lexer.Location);
2337 : object_or_delegate_creation_expression
2338 | array_creation_expression
2341 object_or_delegate_creation_expression
2342 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2344 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2348 array_creation_expression
2349 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2351 opt_array_initializer
2353 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2355 | NEW type rank_specifiers array_initializer
2357 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2361 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2377 : rank_specifier opt_rank_specifier
2379 $$ = (string) $2 + (string) $1;
2384 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2386 $$ = "[" + (string) $2 + "]";
2406 | dim_separators COMMA
2408 $$ = (string) $1 + ",";
2412 opt_array_initializer
2424 : OPEN_BRACE CLOSE_BRACE
2426 ArrayList list = new ArrayList (4);
2429 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2431 $$ = (ArrayList) $2;
2435 variable_initializer_list
2436 : variable_initializer
2438 ArrayList list = new ArrayList (4);
2442 | variable_initializer_list COMMA variable_initializer
2444 ArrayList list = (ArrayList) $1;
2451 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2453 $$ = new TypeOfVoid (lexer.Location);
2455 | TYPEOF OPEN_PARENS type CLOSE_PARENS
2457 $$ = new TypeOf ((Expression) $3, lexer.Location);
2462 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2463 $$ = new SizeOf ((Expression) $3, lexer.Location);
2468 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2470 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2474 unchecked_expression
2475 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2477 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2481 pointer_member_access
2482 : primary_expression OP_PTR IDENTIFIER
2486 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2487 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2491 anonymous_method_expression
2492 : DELEGATE opt_anonymous_method_signature {
2493 oob_stack.Push (current_local_parameters);
2494 current_local_parameters = (Parameters)$2;
2495 create_toplevel_block = true;
2498 Report.Error (-213, lexer.Location, "Anonymous methods are not supported in this branch");
2501 create_toplevel_block = false;
2502 if (!RootContext.V2){
2503 Report.Error (-213, lexer.Location, "Anonymous methods are only supported in V2");
2506 $$ = new AnonymousMethod ((Parameters) $2, (Block) $4, lexer.Location);
2507 current_local_parameters = (Parameters) oob_stack.Pop ();
2512 opt_anonymous_method_signature
2513 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
2514 | anonymous_method_signature
2517 anonymous_method_signature
2518 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
2521 $$ = Parameters.EmptyReadOnlyParameters;
2523 ArrayList par_list = (ArrayList) $2;
2524 Parameter [] pars = new Parameter [par_list.Count];
2525 par_list.CopyTo (pars);
2526 $$ = new Parameters (pars, null, lexer.Location);
2531 opt_anonymous_method_parameter_list
2532 : /* empty */ { $$ = null; }
2533 | anonymous_method_parameter_list { $$ = $1; }
2536 anonymous_method_parameter_list
2537 : anonymous_method_parameter
2539 ArrayList a = new ArrayList (4);
2543 | anonymous_method_parameter_list COMMA anonymous_method_parameter
2545 ArrayList a = (ArrayList) $1;
2551 anonymous_method_parameter
2552 : opt_parameter_modifier type IDENTIFIER {
2553 $$ = new Parameter ((Expression) $2, (string) $2, (Parameter.Modifier) $1, null);
2558 : primary_expression
2559 | BANG prefixed_unary_expression
2561 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2563 | TILDE prefixed_unary_expression
2565 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2571 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
2573 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2575 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
2577 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2583 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2585 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2590 // The idea to split this out is from Rhys' grammar
2591 // to solve the problem with casts.
2593 prefixed_unary_expression
2595 | PLUS prefixed_unary_expression
2597 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2599 | MINUS prefixed_unary_expression
2601 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2603 | OP_INC prefixed_unary_expression
2605 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2606 (Expression) $2, lexer.Location);
2608 | OP_DEC prefixed_unary_expression
2610 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2611 (Expression) $2, lexer.Location);
2613 | STAR prefixed_unary_expression
2615 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2617 | BITWISE_AND prefixed_unary_expression
2619 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2623 pre_increment_expression
2624 : OP_INC prefixed_unary_expression
2626 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2627 (Expression) $2, lexer.Location);
2631 pre_decrement_expression
2632 : OP_DEC prefixed_unary_expression
2634 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2635 (Expression) $2, lexer.Location);
2639 multiplicative_expression
2640 : prefixed_unary_expression
2641 | multiplicative_expression STAR prefixed_unary_expression
2643 $$ = new Binary (Binary.Operator.Multiply,
2644 (Expression) $1, (Expression) $3, lexer.Location);
2646 | multiplicative_expression DIV prefixed_unary_expression
2648 $$ = new Binary (Binary.Operator.Division,
2649 (Expression) $1, (Expression) $3, lexer.Location);
2651 | multiplicative_expression PERCENT prefixed_unary_expression
2653 $$ = new Binary (Binary.Operator.Modulus,
2654 (Expression) $1, (Expression) $3, lexer.Location);
2659 : multiplicative_expression
2660 | additive_expression PLUS multiplicative_expression
2662 $$ = new Binary (Binary.Operator.Addition,
2663 (Expression) $1, (Expression) $3, lexer.Location);
2665 | additive_expression MINUS multiplicative_expression
2667 $$ = new Binary (Binary.Operator.Subtraction,
2668 (Expression) $1, (Expression) $3, lexer.Location);
2673 : additive_expression
2674 | shift_expression OP_SHIFT_LEFT additive_expression
2676 $$ = new Binary (Binary.Operator.LeftShift,
2677 (Expression) $1, (Expression) $3, lexer.Location);
2679 | shift_expression OP_SHIFT_RIGHT additive_expression
2681 $$ = new Binary (Binary.Operator.RightShift,
2682 (Expression) $1, (Expression) $3, lexer.Location);
2686 relational_expression
2688 | relational_expression OP_LT shift_expression
2690 $$ = new Binary (Binary.Operator.LessThan,
2691 (Expression) $1, (Expression) $3, lexer.Location);
2693 | relational_expression OP_GT shift_expression
2695 $$ = new Binary (Binary.Operator.GreaterThan,
2696 (Expression) $1, (Expression) $3, lexer.Location);
2698 | relational_expression OP_LE shift_expression
2700 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2701 (Expression) $1, (Expression) $3, lexer.Location);
2703 | relational_expression OP_GE shift_expression
2705 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2706 (Expression) $1, (Expression) $3, lexer.Location);
2708 | relational_expression IS type
2710 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
2712 | relational_expression AS type
2714 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
2719 : relational_expression
2720 | equality_expression OP_EQ relational_expression
2722 $$ = new Binary (Binary.Operator.Equality,
2723 (Expression) $1, (Expression) $3, lexer.Location);
2725 | equality_expression OP_NE relational_expression
2727 $$ = new Binary (Binary.Operator.Inequality,
2728 (Expression) $1, (Expression) $3, lexer.Location);
2733 : equality_expression
2734 | and_expression BITWISE_AND equality_expression
2736 $$ = new Binary (Binary.Operator.BitwiseAnd,
2737 (Expression) $1, (Expression) $3, lexer.Location);
2741 exclusive_or_expression
2743 | exclusive_or_expression CARRET and_expression
2745 $$ = new Binary (Binary.Operator.ExclusiveOr,
2746 (Expression) $1, (Expression) $3, lexer.Location);
2750 inclusive_or_expression
2751 : exclusive_or_expression
2752 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2754 $$ = new Binary (Binary.Operator.BitwiseOr,
2755 (Expression) $1, (Expression) $3, lexer.Location);
2759 conditional_and_expression
2760 : inclusive_or_expression
2761 | conditional_and_expression OP_AND inclusive_or_expression
2763 $$ = new Binary (Binary.Operator.LogicalAnd,
2764 (Expression) $1, (Expression) $3, lexer.Location);
2768 conditional_or_expression
2769 : conditional_and_expression
2770 | conditional_or_expression OP_OR conditional_and_expression
2772 $$ = new Binary (Binary.Operator.LogicalOr,
2773 (Expression) $1, (Expression) $3, lexer.Location);
2777 conditional_expression
2778 : conditional_or_expression
2779 | conditional_or_expression INTERR expression COLON expression
2781 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2785 assignment_expression
2786 : prefixed_unary_expression ASSIGN expression
2788 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2790 | prefixed_unary_expression OP_MULT_ASSIGN expression
2792 Location l = lexer.Location;
2794 $$ = new CompoundAssign (
2795 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
2797 | prefixed_unary_expression OP_DIV_ASSIGN expression
2799 Location l = lexer.Location;
2801 $$ = new CompoundAssign (
2802 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
2804 | prefixed_unary_expression OP_MOD_ASSIGN expression
2806 Location l = lexer.Location;
2808 $$ = new CompoundAssign (
2809 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
2811 | prefixed_unary_expression OP_ADD_ASSIGN expression
2813 Location l = lexer.Location;
2815 $$ = new CompoundAssign (
2816 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
2818 | prefixed_unary_expression OP_SUB_ASSIGN expression
2820 Location l = lexer.Location;
2822 $$ = new CompoundAssign (
2823 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
2825 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2827 Location l = lexer.Location;
2829 $$ = new CompoundAssign (
2830 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
2832 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2834 Location l = lexer.Location;
2836 $$ = new CompoundAssign (
2837 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
2839 | prefixed_unary_expression OP_AND_ASSIGN expression
2841 Location l = lexer.Location;
2843 $$ = new CompoundAssign (
2844 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
2846 | prefixed_unary_expression OP_OR_ASSIGN expression
2848 Location l = lexer.Location;
2850 $$ = new CompoundAssign (
2851 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
2853 | prefixed_unary_expression OP_XOR_ASSIGN expression
2855 Location l = lexer.Location;
2857 $$ = new CompoundAssign (
2858 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
2863 : conditional_expression
2864 | assignment_expression
2886 name = MakeName ((string) $4);
2888 new_class = new Class (current_namespace, current_container, name, (int) $2,
2889 (Attributes) $1, lexer.Location);
2890 current_container = new_class;
2891 RootContext.Tree.RecordDecl (name, new_class);
2897 Class new_class = (Class) current_container;
2900 if (new_class.Name == "System.Object") {
2901 Report.Error (537, new_class.Location, "The class System.Object cannot have a base class or implement an interface.");
2903 new_class.Bases = (ArrayList) $6;
2906 current_container = current_container.Parent;
2907 CheckDef (current_container.AddClass (new_class), new_class.Name, new_class.Location);
2914 : /* empty */ { $$ = (int) 0; }
2920 | modifiers modifier
2925 if ((m1 & m2) != 0) {
2926 Location l = lexer.Location;
2927 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2929 $$ = (int) (m1 | m2);
2934 : NEW { $$ = Modifiers.NEW; }
2935 | PUBLIC { $$ = Modifiers.PUBLIC; }
2936 | PROTECTED { $$ = Modifiers.PROTECTED; }
2937 | INTERNAL { $$ = Modifiers.INTERNAL; }
2938 | PRIVATE { $$ = Modifiers.PRIVATE; }
2939 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2940 | SEALED { $$ = Modifiers.SEALED; }
2941 | STATIC { $$ = Modifiers.STATIC; }
2942 | READONLY { $$ = Modifiers.READONLY; }
2943 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2944 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2945 | EXTERN { $$ = Modifiers.EXTERN; }
2946 | VOLATILE { $$ = Modifiers.VOLATILE; }
2947 | UNSAFE { $$ = Modifiers.UNSAFE; }
2951 : /* empty */ { $$ = null; }
2952 | class_base { $$ = $1; }
2956 : COLON type_list { $$ = $2; }
2964 // A block is "contained" on the following places:
2966 // property_declaration as part of the accessor body (get/set)
2967 // operator_declaration
2968 // constructor_declaration
2969 // destructor_declaration
2970 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2975 if (current_block == null || create_toplevel_block){
2976 current_block = new ToplevelBlock (current_local_parameters, lexer.Location);
2978 current_block = new Block (current_block, current_local_parameters,
2979 lexer.Location, Location.Null);
2982 opt_statement_list CLOSE_BRACE
2984 while (current_block.Implicit)
2985 current_block = current_block.Parent;
2987 current_block.SetEndLocation (lexer.Location);
2988 current_block = current_block.Parent;
2999 | statement_list statement
3003 : declaration_statement
3005 if ($1 != null && (Block) $1 != current_block){
3006 current_block.AddStatement ((Statement) $1);
3007 current_block = (Block) $1;
3010 | valid_declaration_statement
3012 current_block.AddStatement ((Statement) $1);
3017 valid_declaration_statement
3020 | expression_statement
3021 | selection_statement
3022 | iteration_statement
3026 | unchecked_statement
3034 : valid_declaration_statement
3035 | declaration_statement
3037 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3042 Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3050 $$ = EmptyStatement.Value;
3057 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3059 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3060 current_block.AddStatement (labeled);
3065 declaration_statement
3066 : local_variable_declaration SEMICOLON
3069 DictionaryEntry de = (DictionaryEntry) $1;
3071 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3075 | local_constant_declaration SEMICOLON
3078 DictionaryEntry de = (DictionaryEntry) $1;
3080 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3086 * The following is from Rhys' grammar:
3087 * > Types in local variable declarations must be recognized as
3088 * > expressions to prevent reduce/reduce errors in the grammar.
3089 * > The expressions are converted into types during semantic analysis.
3092 : primary_expression opt_rank_specifier
3094 // FIXME: Do something smart here regarding the composition of the type.
3096 // Ok, the above "primary_expression" is there to get rid of
3097 // both reduce/reduce and shift/reduces in the grammar, it should
3098 // really just be "type_name". If you use type_name, a reduce/reduce
3099 // creeps up. If you use namespace_or_type_name (which is all we need
3100 // really) two shift/reduces appear.
3103 // So the super-trick is that primary_expression
3104 // can only be either a SimpleName or a MemberAccess.
3105 // The MemberAccess case arises when you have a fully qualified type-name like :
3107 // SimpleName is when you have
3110 Expression expr = (Expression) $1;
3111 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3112 Error_ExpectingTypeName (lexer.Location, expr);
3116 // So we extract the string corresponding to the SimpleName
3120 if ((string) $2 == "")
3123 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3126 | builtin_types opt_rank_specifier
3128 if ((string) $2 == "")
3131 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3135 local_variable_pointer_type
3136 : primary_expression STAR
3138 Expression expr = (Expression) $1;
3139 Location l = lexer.Location;
3141 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3142 Error_ExpectingTypeName (l, expr);
3146 $$ = new ComposedCast ((Expression) $1, "*", l);
3148 | builtin_types STAR
3150 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3154 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3156 | local_variable_pointer_type STAR
3158 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3162 local_variable_declaration
3163 : local_variable_type variable_declarators
3166 $$ = new DictionaryEntry ($1, $2);
3170 | local_variable_pointer_type opt_rank_specifier variable_declarators
3175 if ((string) $2 == "")
3176 t = (Expression) $1;
3178 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3179 $$ = new DictionaryEntry (t, $3);
3185 local_constant_declaration
3186 : CONST local_variable_type constant_declarators
3189 $$ = new DictionaryEntry ($2, $3);
3195 expression_statement
3196 : statement_expression SEMICOLON
3203 // We have to do the wrapping here and not in the case above,
3204 // because statement_expression is used for example in for_statement
3206 statement_expression
3207 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3208 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3209 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3210 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3211 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3212 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3213 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3215 Report.Error (1002, lexer.Location, "Expecting `;'");
3220 object_creation_expression
3221 : object_or_delegate_creation_expression
3222 { note ("complain if this is a delegate maybe?"); }
3231 : if_statement_open if_statement_rest
3240 oob_stack.Push (lexer.Location);
3245 : boolean_expression CLOSE_PARENS
3248 Location l = (Location) oob_stack.Pop ();
3250 $$ = new If ((Expression) $1, (Statement) $3, l);
3252 if (RootContext.WarningLevel >= 3){
3253 if ($3 == EmptyStatement.Value)
3254 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3258 | boolean_expression CLOSE_PARENS
3259 embedded_statement ELSE embedded_statement
3261 Location l = (Location) oob_stack.Pop ();
3263 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
3268 : SWITCH OPEN_PARENS
3270 oob_stack.Push (lexer.Location);
3271 switch_stack.Push (current_block);
3273 expression CLOSE_PARENS
3276 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
3277 current_block = (Block) switch_stack.Pop ();
3293 Report.Error (1522, lexer.Location, "Empty switch block");
3301 ArrayList sections = new ArrayList (4);
3306 | switch_sections switch_section
3308 ArrayList sections = (ArrayList) $1;
3318 current_block = current_block.CreateSwitchBlock (lexer.Location);
3322 Block topmost = current_block;
3324 while (topmost.Implicit)
3325 topmost = topmost.Parent;
3326 $$ = new SwitchSection ((ArrayList) $1, topmost);
3333 ArrayList labels = new ArrayList (4);
3338 | switch_labels switch_label
3340 ArrayList labels = (ArrayList) ($1);
3348 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3349 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3352 1523, lexer.Location,
3353 "The keyword case or default must precede code in switch block");
3367 oob_stack.Push (lexer.Location);
3369 boolean_expression CLOSE_PARENS embedded_statement
3371 Location l = (Location) oob_stack.Pop ();
3372 $$ = new While ((Expression) $4, (Statement) $6, l);
3374 if (RootContext.WarningLevel >= 3){
3375 if ($6 == EmptyStatement.Value)
3376 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3382 : DO embedded_statement
3384 oob_stack.Push (lexer.Location);
3386 boolean_expression CLOSE_PARENS SEMICOLON
3388 Location l = (Location) oob_stack.Pop ();
3390 $$ = new Do ((Statement) $2, (Expression) $6, l);
3396 opt_for_initializer SEMICOLON
3398 Block assign_block = new Block (current_block);
3399 current_block = assign_block;
3401 if ($3 is DictionaryEntry){
3402 DictionaryEntry de = (DictionaryEntry) $3;
3404 Expression type = (Expression) de.Key;
3405 ArrayList var_declarators = (ArrayList) de.Value;
3407 foreach (VariableDeclaration decl in var_declarators){
3411 vi = current_block.AddVariable (
3412 type, decl.identifier, current_local_parameters, decl.Location);
3416 Location l = lexer.Location;
3418 if (decl.expression_or_array_initializer is Expression){
3419 expr = (Expression) decl.expression_or_array_initializer;
3420 } else if (decl.expression_or_array_initializer == null) {
3423 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3424 expr = new ArrayCreation (type, "", init, decl.Location);
3427 LocalVariableReference var;
3428 var = new LocalVariableReference (assign_block, decl.identifier, l);
3431 Assign a = new Assign (var, expr, decl.Location);
3433 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3439 oob_stack.Push (lexer.Location);
3441 opt_for_condition SEMICOLON
3442 opt_for_iterator CLOSE_PARENS
3445 Location l = (Location) oob_stack.Pop ();
3447 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3449 if (RootContext.WarningLevel >= 3){
3450 if ($10 == EmptyStatement.Value)
3451 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3454 current_block.AddStatement (f);
3455 while (current_block.Implicit)
3456 current_block = current_block.Parent;
3458 current_block = current_block.Parent;
3463 : /* empty */ { $$ = EmptyStatement.Value; }
3468 : local_variable_declaration
3469 | statement_expression_list
3473 : /* empty */ { $$ = null; }
3474 | boolean_expression
3478 : /* empty */ { $$ = EmptyStatement.Value; }
3483 : statement_expression_list
3486 statement_expression_list
3487 : statement_expression
3489 // CHANGE: was `null'
3490 Block b = new Block (current_block, Block.Flags.Implicit);
3492 b.AddStatement ((Statement) $1);
3495 | statement_expression_list COMMA statement_expression
3497 Block b = (Block) $1;
3499 b.AddStatement ((Statement) $3);
3505 : FOREACH OPEN_PARENS type IDENTIFIER IN
3507 oob_stack.Push (lexer.Location);
3509 expression CLOSE_PARENS
3511 oob_stack.Push (current_block);
3513 Block foreach_block = new Block (current_block);
3514 LocalVariableReference v = null;
3515 Location l = lexer.Location;
3518 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3522 // Get a writable reference to this read-only variable.
3523 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3525 current_block = foreach_block;
3528 oob_stack.Push (current_block);
3532 Block foreach_block = (Block) oob_stack.Pop ();
3533 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3534 Block prev_block = (Block) oob_stack.Pop ();
3535 Location l = (Location) oob_stack.Pop ();
3537 current_block = prev_block;
3540 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3541 foreach_block.AddStatement (f);
3550 | continue_statement
3560 $$ = new Break (lexer.Location);
3565 : CONTINUE SEMICOLON
3567 $$ = new Continue (lexer.Location);
3572 : GOTO IDENTIFIER SEMICOLON
3574 $$ = new Goto (current_block, (string) $2, lexer.Location);
3576 | GOTO CASE constant_expression SEMICOLON
3578 $$ = new GotoCase ((Expression) $3, lexer.Location);
3580 | GOTO DEFAULT SEMICOLON
3582 $$ = new GotoDefault (lexer.Location);
3587 : RETURN opt_expression SEMICOLON
3589 $$ = new Return ((Expression) $2, lexer.Location);
3594 : THROW opt_expression SEMICOLON
3596 $$ = new Throw ((Expression) $2, lexer.Location);
3601 : IDENTIFIER RETURN expression SEMICOLON
3603 string s = (string) $1;
3605 Report.Error (1003, lexer.Location, "; expected");
3608 if (!RootContext.V2){
3609 Report.Error (-222, lexer.Location, "yield statement only available in C# 2.0 mode");
3612 if (iterator_container == null){
3613 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3616 iterator_container.SetYields ();
3617 $$ = new Yield ((Expression) $3, lexer.Location);
3620 | IDENTIFIER BREAK SEMICOLON
3622 string s = (string) $1;
3624 Report.Error (1003, lexer.Location, "; expected");
3627 if (!RootContext.V2){
3628 Report.Error (-222, lexer.Location, "yield statement only available in C# 2.0 mode");
3631 if (iterator_container == null){
3632 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3635 iterator_container.SetYields ();
3636 $$ = new YieldBreak (lexer.Location);
3647 : TRY block catch_clauses
3650 ArrayList s = new ArrayList (4);
3652 foreach (Catch cc in (ArrayList) $3) {
3659 // Now s contains the list of specific catch clauses
3660 // and g contains the general one.
3662 $$ = new Try ((Block) $2, s, g, null, lexer.Location);
3664 | TRY block opt_catch_clauses FINALLY block
3667 ArrayList s = new ArrayList (4);
3668 ArrayList catch_list = (ArrayList) $3;
3670 if (catch_list != null){
3671 foreach (Catch cc in catch_list) {
3679 $$ = new Try ((Block) $2, s, g, (Block) $5, lexer.Location);
3683 Report.Error (1524, lexer.Location, "Expected catch or finally");
3688 : /* empty */ { $$ = null; }
3695 ArrayList l = new ArrayList (4);
3700 | catch_clauses catch_clause
3702 ArrayList l = (ArrayList) $1;
3710 : /* empty */ { $$ = null; }
3715 : CATCH opt_catch_args
3717 Expression type = null;
3721 DictionaryEntry cc = (DictionaryEntry) $2;
3722 type = (Expression) cc.Key;
3723 id = (string) cc.Value;
3726 ArrayList one = new ArrayList (4);
3727 Location loc = lexer.Location;
3729 one.Add (new VariableDeclaration (id, null, loc));
3732 current_block = new Block (current_block);
3733 Block b = declare_local_variables (type, one, loc);
3738 Expression type = null;
3742 DictionaryEntry cc = (DictionaryEntry) $2;
3743 type = (Expression) cc.Key;
3744 id = (string) cc.Value;
3748 // FIXME: I can change this for an assignment.
3750 while (current_block != (Block) $1)
3751 current_block = current_block.Parent;
3756 $$ = new Catch (type, id , (Block) $4, lexer.Location);
3761 : /* empty */ { $$ = null; }
3766 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3768 $$ = new DictionaryEntry ($2, $3);
3775 $$ = new Checked ((Block) $2);
3782 $$ = new Unchecked ((Block) $2);
3789 if (!RootContext.Unsafe){
3790 Report.Error (227, lexer.Location,
3791 "Unsafe code can only be used if --unsafe is used");
3794 $$ = new Unsafe ((Block) $3);
3800 type fixed_pointer_declarators
3803 Block assign_block = new Block (current_block, Block.Flags.Implicit);
3804 ArrayList list = (ArrayList) $4;
3805 Expression type = (Expression) $3;
3806 Location l = lexer.Location;
3807 int top = list.Count;
3809 for (int i = 0; i < top; i++){
3810 Pair p = (Pair) list [i];
3813 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3820 current_block.AddStatement (assign_block);
3821 current_block = assign_block;
3822 oob_stack.Push (assign_block);
3827 Location l = (Location) oob_stack.Pop ();
3830 $$ = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
3834 fixed_pointer_declarators
3835 : fixed_pointer_declarator {
3836 ArrayList declarators = new ArrayList (4);
3837 declarators.Add ($1);
3840 | fixed_pointer_declarators COMMA fixed_pointer_declarator
3842 ArrayList declarators = (ArrayList) $1;
3843 declarators.Add ($3);
3848 fixed_pointer_declarator
3849 : IDENTIFIER ASSIGN expression
3851 $$ = new Pair ($1, $3);
3856 : LOCK OPEN_PARENS expression CLOSE_PARENS
3862 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
3867 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
3869 Block assign_block = new Block (current_block);
3870 current_block = assign_block;
3872 oob_stack.Push (lexer.Location);
3874 if ($3 is DictionaryEntry){
3875 DictionaryEntry de = (DictionaryEntry) $3;
3876 Location l = lexer.Location;
3878 Expression type = (Expression) de.Key;
3879 ArrayList var_declarators = (ArrayList) de.Value;
3881 ArrayList vars = new ArrayList (4);
3883 foreach (VariableDeclaration decl in var_declarators){
3885 LocalInfo vi = current_block.AddVariable (
3886 type, decl.identifier,
3887 current_local_parameters, decl.Location);
3893 if (decl.expression_or_array_initializer is Expression){
3894 expr = (Expression) decl.expression_or_array_initializer;
3896 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3898 expr = new ArrayCreation (type, "", init, decl.Location);
3901 LocalVariableReference var;
3903 // Get a writable reference to this read-only variable.
3904 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
3906 // This is so that it is not a warning on using variables
3909 vars.Add (new DictionaryEntry (var, expr));
3911 // Assign a = new Assign (var, expr, decl.Location);
3912 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3914 $3 = new DictionaryEntry (type, vars);
3919 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
3920 current_block.AddStatement (u);
3921 while (current_block.Implicit)
3922 current_block = current_block.Parent;
3924 current_block = current_block.Parent;
3928 resource_acquisition
3929 : local_variable_declaration
3936 // A class used to pass around variable declarations and constants
3938 public class VariableDeclaration {
3939 public string identifier;
3940 public object expression_or_array_initializer;
3941 public Location Location;
3942 public Attributes OptAttributes;
3944 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
3946 this.identifier = id;
3947 this.expression_or_array_initializer = eoai;
3949 this.OptAttributes = opt_attrs;
3952 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
3958 /// Used to pass around interface property information
3960 public class InterfaceAccessorInfo {
3962 public readonly Accessor Get, Set;
3964 public InterfaceAccessorInfo (bool has_get, bool has_set,
3965 Attributes get_attrs, Attributes set_attrs)
3968 Get = new Accessor (null, get_attrs);
3970 Set = new Accessor (null, set_attrs);
3976 // A class used to hold info about an indexer declarator
3978 public class IndexerDeclaration {
3979 public Expression type;
3980 public string interface_type;
3981 public Parameters param_list;
3983 public IndexerDeclaration (Expression type, string interface_type, Parameters param_list)
3986 this.interface_type = interface_type;
3987 this.param_list = param_list;
3992 // We use this when we do not have an object in advance that is an IIteratorContainer
3994 public class SimpleIteratorContainer : IIteratorContainer {
3997 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4002 public static SimpleIteratorContainer GetSimple () {
4003 Simple.Yields = false;
4007 public void SetYields () { Yields = true; }
4011 // A class used to hold info about an operator declarator
4013 public class OperatorDeclaration {
4014 public Operator.OpType optype;
4015 public Expression ret_type, arg1type, arg2type;
4016 public string arg1name, arg2name;
4017 public Location location;
4019 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4020 Expression arg1type, string arg1name,
4021 Expression arg2type, string arg2name, Location location)
4024 this.ret_type = ret_type;
4025 this.arg1type = arg1type;
4026 this.arg1name = arg1name;
4027 this.arg2type = arg2type;
4028 this.arg2name = arg2name;
4029 this.location = location;
4034 void Error_ExpectingTypeName (Location l, Expression expr)
4036 if (expr is Invocation){
4037 Report.Error (1002, l, "; expected");
4039 Report.Error (-1, l, "Invalid Type definition");
4044 // Given the @class_name name, it creates a fully qualified name
4045 // based on the containing declaration space
4048 MakeName (string class_name)
4050 string ns = current_namespace.FullName;
4051 string container_name = current_container.Name;
4053 if (container_name == ""){
4055 return ns + "." + class_name;
4059 return container_name + "." + class_name;
4063 // Used to report back to the user the result of a declaration
4064 // in the current declaration space
4067 CheckDef (DeclSpace.AdditionResult result, string name, Location l)
4069 if (result == DeclSpace.AdditionResult.Success)
4073 case DeclSpace.AdditionResult.NameExists:
4074 Report.Error (102, l, "The container `" + current_container.Name +
4075 "' already contains a definition for `"+
4081 // This is handled only for static Constructors, because
4082 // in reality we handle these by the semantic analysis later
4084 case DeclSpace.AdditionResult.MethodExists:
4086 111, l, "Class `"+current_container.Name+
4087 "' already defines a member called '" +
4088 name + "' with the same parameter types (more than one default constructor)");
4091 case DeclSpace.AdditionResult.EnclosingClash:
4092 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
4095 case DeclSpace.AdditionResult.NotAConstructor:
4096 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
4099 case DeclSpace.AdditionResult.Error:
4100 // Error has already been reported.
4106 CheckDef (bool result, string name, Location l)
4110 CheckDef (DeclSpace.AdditionResult.NameExists, name, l);
4113 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4115 Block implicit_block;
4116 ArrayList inits = null;
4119 // We use the `Used' property to check whether statements
4120 // have been added to the current block. If so, we need
4121 // to create another block to contain the new declaration
4122 // otherwise, as an optimization, we use the same block to
4123 // add the declaration.
4125 // FIXME: A further optimization is to check if the statements
4126 // that were added were added as part of the initialization
4127 // below. In which case, no other statements have been executed
4128 // and we might be able to reduce the number of blocks for
4129 // situations like this:
4131 // int j = 1; int k = j + 1;
4133 if (current_block.Used)
4134 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4136 implicit_block = current_block;
4138 foreach (VariableDeclaration decl in variable_declarators){
4140 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4141 if (decl.expression_or_array_initializer != null){
4143 inits = new ArrayList (4);
4150 return implicit_block;
4152 foreach (VariableDeclaration decl in inits){
4156 if (decl.expression_or_array_initializer is Expression){
4157 expr = (Expression) decl.expression_or_array_initializer;
4160 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4162 expr = new ArrayCreation (type, "", init, decl.Location);
4165 LocalVariableReference var;
4166 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4168 assign = new Assign (var, expr, decl.Location);
4170 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4173 return implicit_block;
4176 Block declare_local_constants (Expression type, ArrayList declarators)
4178 Block implicit_block;
4180 if (current_block.Used)
4181 implicit_block = new Block (current_block, Block.Flags.Implicit);
4183 implicit_block = current_block;
4185 foreach (VariableDeclaration decl in declarators){
4186 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4187 current_local_parameters, decl.Location);
4190 return implicit_block;
4193 void CheckAttributeTarget (string a)
4197 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4201 Location l = lexer.Location;
4202 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4208 void CheckUnaryOperator (Operator.OpType op)
4212 case Operator.OpType.LogicalNot:
4213 case Operator.OpType.OnesComplement:
4214 case Operator.OpType.Increment:
4215 case Operator.OpType.Decrement:
4216 case Operator.OpType.True:
4217 case Operator.OpType.False:
4218 case Operator.OpType.Addition:
4219 case Operator.OpType.Subtraction:
4224 Location l = lexer.Location;
4225 Report.Error (1019, l, "Overloadable unary operator expected");
4231 void CheckBinaryOperator (Operator.OpType op)
4235 case Operator.OpType.Addition:
4236 case Operator.OpType.Subtraction:
4237 case Operator.OpType.Multiply:
4238 case Operator.OpType.Division:
4239 case Operator.OpType.Modulus:
4240 case Operator.OpType.BitwiseAnd:
4241 case Operator.OpType.BitwiseOr:
4242 case Operator.OpType.ExclusiveOr:
4243 case Operator.OpType.LeftShift:
4244 case Operator.OpType.RightShift:
4245 case Operator.OpType.Equality:
4246 case Operator.OpType.Inequality:
4247 case Operator.OpType.GreaterThan:
4248 case Operator.OpType.LessThan:
4249 case Operator.OpType.GreaterThanOrEqual:
4250 case Operator.OpType.LessThanOrEqual:
4254 Location l = lexer.Location;
4255 Report.Error (1020, l, "Overloadable binary operator expected");
4261 void syntax_error (Location l, string msg)
4263 Report.Error (1003, l, "Syntax error, " + msg);
4266 void output (string s)
4268 Console.WriteLine (s);
4271 void note (string s)
4273 // Used to put annotations
4278 public Tokenizer Lexer {
4284 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4286 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4287 this.name = file.Name;
4289 current_container = RootContext.Tree.Types;
4290 current_container.NamespaceEntry = current_namespace;
4291 oob_stack = new Stack ();
4292 switch_stack = new Stack ();
4294 lexer = new Tokenizer (reader, file, defines);
4297 public override void parse ()
4300 if (yacc_verbose_flag)
4301 yyparse (lexer, new yydebug.yyDebugSimple ());
4304 Tokenizer tokenizer = lexer as Tokenizer;
4305 tokenizer.cleanup ();
4306 } catch (Exception e){
4307 // Please do not remove this, it is used during debugging
4310 Console.WriteLine (e);
4311 Report.Error (-25, lexer.Location, "Parsing error");
4312 if (Driver.parser_verbose)
4313 Console.WriteLine (e);
4317 void CheckToken (int error, int yyToken, string msg)
4319 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4320 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4323 Report.Error (error, lexer.Location, msg);
4326 void CheckIdentifierToken (int yyToken)
4328 CheckToken (1041, yyToken, "Identifier expected");