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;
36 TypeContainer current_class;
38 IIteratorContainer iterator_container;
41 /// Current block is used to add statements as we find
47 /// If true, creates a toplevel block in the block production
48 /// This is flagged by the delegate creation
50 bool create_toplevel_block;
53 /// This is used by the unary_expression code to resolve
54 /// a name against a parameter.
56 Parameters current_local_parameters;
59 /// Using during property parsing to describe the implicit
60 /// value parameter that is passed to the "set" and "get"accesor
61 /// methods (properties and indexers).
63 Expression implicit_value_parameter_type;
64 Parameters indexer_parameters;
67 /// Used to determine if we are parsing the get/set pair
68 /// of an indexer or a property
73 /// An out-of-band stack.
88 /// Current attribute target
89 string current_attr_target;
91 /// assembly and module attribute definition is enabled
92 bool global_attrs_enabled = true;
96 %token NONE /* This token is never returned by our lexer */
97 %token ERROR // This is used not by the parser, but by the tokenizer.
101 *These are the C# keywords
187 /* C# keywords which are not really keywords */
193 /* C# single character operators/punctuation. */
194 %token OPEN_BRACE "{"
195 %token CLOSE_BRACE "}"
196 %token OPEN_BRACKET "["
197 %token CLOSE_BRACKET "]"
198 %token OPEN_PARENS "("
199 %token CLOSE_PARENS ")"
212 %token BITWISE_AND "&"
213 %token BITWISE_OR "|"
220 /* C# multi-character operators. */
223 %token OP_SHIFT_LEFT "<<"
224 %token OP_SHIFT_RIGHT ">>"
231 %token OP_MULT_ASSIGN "*="
232 %token OP_DIV_ASSIGN "/="
233 %token OP_MOD_ASSIGN "%="
234 %token OP_ADD_ASSIGN "+="
235 %token OP_SUB_ASSIGN "-="
236 %token OP_SHIFT_LEFT_ASSIGN "<<="
237 %token OP_SHIFT_RIGHT_ASSIGN ">>="
238 %token OP_AND_ASSIGN "&="
239 %token OP_XOR_ASSIGN "^="
240 %token OP_OR_ASSIGN "|="
244 %token LITERAL_INTEGER "int literal"
245 %token LITERAL_FLOAT "float literal"
246 %token LITERAL_DOUBLE "double literal"
247 %token LITERAL_DECIMAL "decimal literal"
248 %token LITERAL_CHARACTER "character literal"
249 %token LITERAL_STRING "string literal"
252 %token CLOSE_PARENS_CAST
253 %token CLOSE_PARENS_NO_CAST
254 %token CLOSE_PARENS_OPEN_PARENS
255 %token CLOSE_PARENS_MINUS
257 /* Add precedence rules to solve dangling else s/r conflict */
266 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
268 %left STAR DIV PERCENT
269 %right BANG CARRET UMINUS
270 %nonassoc OP_INC OP_DEC
272 %left OPEN_BRACKET OPEN_BRACE
276 %start compilation_unit
280 : outer_declarations opt_EOF
281 | outer_declarations global_attributes opt_EOF
282 | global_attributes opt_EOF
283 | opt_EOF /* allow empty files */
293 | outer_declarations outer_declaration
298 | namespace_member_declaration
303 | using_directives using_directive
307 : using_alias_directive
308 | using_namespace_directive
311 using_alias_directive
312 : USING IDENTIFIER ASSIGN
313 namespace_or_type_name SEMICOLON
315 current_namespace.UsingAlias ((string) $2, (Expression) $4, lexer.Location);
318 CheckIdentifierToken (yyToken);
322 using_namespace_directive
323 : USING namespace_name SEMICOLON
325 current_namespace.Using ((string) $2, lexer.Location);
330 // Strictly speaking, namespaces don't have attributes but
331 // we parse global attributes along with namespace declarations and then
334 namespace_declaration
335 : opt_attributes NAMESPACE namespace_or_type_name
338 Report.Error(1518, Lexer.Location, "Attributes cannot be applied to namespaces."
339 + " Expected class, delegate, enum, interface, or struct");
342 if (current_namespace.Parent != null && $3 is MemberAccess) {
343 // Cannot use qualified namespace names in nested namespace declarations
344 Report.Error_T (134, lexer.Location);
347 current_namespace = new NamespaceEntry (
348 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 name = MakeName ((string) $5);
711 bool partial = (bool) $3;
714 ClassPart part = PartialContainer.CreatePart (
715 current_namespace, current_container, name, (int) $2,
716 (Attributes) $1, Kind.Struct, lexer.Location);
718 current_container = part.PartialContainer;
719 current_class = part;
721 current_class = new Struct (
722 current_namespace, current_container, name, (int) $2,
723 (Attributes) $1, lexer.Location);
725 current_container = current_class;
726 RootContext.Tree.RecordDecl (name, current_class);
732 current_class.Bases = (ArrayList) $7;
734 current_class.Register ();
741 current_container = current_container.Parent;
742 current_class = current_container;
744 | opt_attributes opt_modifiers opt_partial STRUCT error {
745 CheckIdentifierToken (yyToken);
750 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
753 opt_struct_member_declarations
755 | struct_member_declarations
758 struct_member_declarations
759 : struct_member_declaration
760 | struct_member_declarations struct_member_declaration
763 struct_member_declaration
764 : constant_declaration
767 | property_declaration
769 | indexer_declaration
770 | operator_declaration
771 | constructor_declaration
775 * This is only included so we can flag error 575:
776 * destructors only allowed on class types
778 | destructor_declaration
789 foreach (VariableDeclaration constant in (ArrayList) $5){
790 Location l = constant.Location;
792 Const c = new Const (
793 current_class, (Expression) $4, (string) constant.identifier,
794 (Expression) constant.expression_or_array_initializer, (int) $2,
797 CheckDef (current_container.AddConstant (c), c.Name, l);
803 : constant_declarator
805 ArrayList constants = new ArrayList (4);
810 | constant_declarators COMMA constant_declarator
813 ArrayList constants = (ArrayList) $1;
820 : IDENTIFIER ASSIGN constant_expression
822 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
826 // A const field requires a value to be provided
827 Report.Error_T (145, lexer.Location);
839 Expression type = (Expression) $3;
842 foreach (VariableDeclaration var in (ArrayList) $4){
843 Location l = var.Location;
845 Field field = new Field (current_class, type, mod, var.identifier,
846 var.expression_or_array_initializer,
849 CheckDef (current_container.AddField (field), field.Name, l);
857 Report.Error (670, lexer.Location, "void type is not allowed for fields");
862 : variable_declarator
864 ArrayList decl = new ArrayList (4);
868 | variable_declarators COMMA variable_declarator
870 ArrayList decls = (ArrayList) $1;
877 : IDENTIFIER ASSIGN variable_initializer
879 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
883 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
896 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
898 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
904 iterator_container = (IIteratorContainer) $1;
908 Method method = (Method) $1;
909 Block b = (Block) $3;
910 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
913 if ((method.ModFlags & extern_abstract) == 0){
915 501, lexer.Location, current_container.MakeName (method.Name) +
916 "must declare a body because it is not marked abstract or extern");
919 if ((method.ModFlags & Modifiers.EXTERN) != 0){
921 179, lexer.Location, current_container.MakeName (method.Name) +
922 " is declared extern, but has a body");
926 method.Block = (Block) $3;
927 CheckDef (current_container.AddMethod (method), method.Name, method.Location);
929 current_local_parameters = null;
930 iterator_container = null;
944 1585, lexer.Location, "Member modifier `" +
945 Modifiers.Name (i) + "' must precede member type and name");
958 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
960 Method method = new Method (current_class, (Expression) $3, (int) $2,
961 false, (string) $4, (Parameters) $6,
962 (Attributes) $1, lexer.Location);
964 current_local_parameters = (Parameters) $6;
972 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
974 Method method = new Method (current_class, TypeManager.system_void_expr,
975 (int) $2, false, (string) $4, (Parameters) $6,
976 (Attributes) $1, lexer.Location);
978 current_local_parameters = (Parameters) $6;
984 modifiers member_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
986 Report.Error (1585, lexer.Location,
987 String.Format ("Modifier {0} should appear before type",
988 Modifiers.Name ((int) $4)));
989 Method method = new Method (current_class, TypeManager.system_void_expr,
990 0, false, (string) $5, (Parameters) $7,
991 (Attributes) $1, lexer.Location);
993 current_local_parameters = (Parameters) $7;
1000 | SEMICOLON { $$ = null; }
1003 opt_formal_parameter_list
1004 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1005 | formal_parameter_list
1008 formal_parameter_list
1011 ArrayList pars_list = (ArrayList) $1;
1013 Parameter [] pars = new Parameter [pars_list.Count];
1014 pars_list.CopyTo (pars);
1016 $$ = new Parameters (pars, null, lexer.Location);
1018 | fixed_parameters COMMA parameter_array
1020 ArrayList pars_list = (ArrayList) $1;
1022 Parameter [] pars = new Parameter [pars_list.Count];
1023 pars_list.CopyTo (pars);
1025 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
1027 | fixed_parameters COMMA ARGLIST
1029 ArrayList pars_list = (ArrayList) $1;
1031 Parameter [] pars = new Parameter [pars_list.Count];
1032 pars_list.CopyTo (pars);
1034 $$ = new Parameters (pars, true, lexer.Location);
1038 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
1042 $$ = new Parameters (null, true, lexer.Location);
1049 ArrayList pars = new ArrayList (4);
1054 | fixed_parameters COMMA fixed_parameter
1056 ArrayList pars = (ArrayList) $1;
1065 opt_parameter_modifier
1069 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
1072 opt_parameter_modifier
1075 CheckIdentifierToken (yyToken);
1080 opt_parameter_modifier
1081 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1082 | parameter_modifier
1086 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1087 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1091 : opt_attributes PARAMS type IDENTIFIER
1093 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1094 note ("type must be a single-dimension array type");
1096 | opt_attributes PARAMS type error {
1097 CheckIdentifierToken (yyToken);
1103 : namespace_or_type_name { $$ = ($1).ToString (); }
1106 property_declaration
1112 implicit_value_parameter_type = (Expression) $3;
1114 lexer.PropertyParsing = true;
1116 $$ = lexer.Location;
1118 iterator_container = SimpleIteratorContainer.GetSimple ();
1120 accessor_declarations
1122 lexer.PropertyParsing = false;
1127 Pair pair = (Pair) $7;
1128 Accessor get_block = (Accessor) pair.First;
1129 Accessor set_block = (Accessor) pair.Second;
1131 Location loc = (Location) $6;
1132 prop = new Property (current_class, (Expression) $3, (int) $2, false,
1133 (string) $4, (Attributes) $1, get_block, set_block, loc);
1134 if (SimpleIteratorContainer.Simple.Yields)
1137 CheckDef (current_container.AddProperty (prop), prop.Name, loc);
1138 implicit_value_parameter_type = null;
1139 iterator_container = null;
1143 accessor_declarations
1144 : get_accessor_declaration opt_set_accessor_declaration
1146 $$ = new Pair ($1, $2);
1148 | set_accessor_declaration opt_get_accessor_declaration
1150 $$ = new Pair ($2, $1);
1154 opt_get_accessor_declaration
1155 : /* empty */ { $$ = null; }
1156 | get_accessor_declaration
1159 opt_set_accessor_declaration
1160 : /* empty */ { $$ = null; }
1161 | set_accessor_declaration
1164 get_accessor_declaration
1165 : opt_attributes GET
1167 // If this is not the case, then current_local_parameters has already
1168 // been set in indexer_declaration
1169 if (parsing_indexer == false)
1170 current_local_parameters = null;
1172 current_local_parameters = indexer_parameters;
1173 lexer.PropertyParsing = false;
1177 $$ = new Accessor ((Block) $4, (Attributes) $1);
1178 current_local_parameters = null;
1179 lexer.PropertyParsing = true;
1183 set_accessor_declaration
1184 : opt_attributes SET
1187 Parameter implicit_value_parameter = new Parameter (
1188 implicit_value_parameter_type, "value",
1189 Parameter.Modifier.NONE, null);
1191 if (parsing_indexer == false) {
1192 args = new Parameter [1];
1193 args [0] = implicit_value_parameter;
1194 current_local_parameters = new Parameters (args, null, lexer.Location);
1196 Parameter [] fpars = indexer_parameters.FixedParameters;
1199 int count = fpars.Length;
1201 args = new Parameter [count + 1];
1202 fpars.CopyTo (args, 0);
1203 args [count] = implicit_value_parameter;
1206 current_local_parameters = new Parameters (
1207 args, indexer_parameters.ArrayParameter, lexer.Location);
1210 lexer.PropertyParsing = false;
1214 $$ = new Accessor ((Block) $4, (Attributes) $1);
1215 current_local_parameters = null;
1216 lexer.PropertyParsing = true;
1222 | SEMICOLON { $$ = null; }
1225 interface_declaration
1229 INTERFACE IDENTIFIER
1231 string name = MakeName ((string) $5);
1232 bool partial = (bool) $3;
1235 ClassPart part = PartialContainer.CreatePart (
1236 current_namespace, current_container, name, (int) $2,
1237 (Attributes) $1, Kind.Interface, lexer.Location);
1239 current_container = part.PartialContainer;
1240 current_class = part;
1242 current_class = new Interface (
1243 current_namespace, current_container, name, (int) $2,
1244 (Attributes) $1, lexer.Location);
1246 current_container = current_class;
1247 RootContext.Tree.RecordDecl (name, current_class);
1252 current_class.Bases = (ArrayList) $7;
1254 current_class.Register ();
1261 current_container = current_container.Parent;
1262 current_class = current_container;
1264 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1265 CheckIdentifierToken (yyToken);
1271 opt_interface_member_declarations
1275 opt_interface_member_declarations
1277 | interface_member_declarations
1280 interface_member_declarations
1281 : interface_member_declaration
1282 | interface_member_declarations interface_member_declaration
1285 interface_member_declaration
1286 : interface_method_declaration
1288 Method m = (Method) $1;
1290 CheckDef (current_container.AddMethod (m), m.Name, m.Location);
1292 | interface_property_declaration
1294 Property p = (Property) $1;
1296 CheckDef (current_container.AddProperty (p), p.Name, p.Location);
1298 | interface_event_declaration
1301 Event e = (Event) $1;
1302 CheckDef (current_container.AddEvent (e), e.Name, lexer.Location);
1305 | interface_indexer_declaration
1307 Indexer i = (Indexer) $1;
1309 current_container.AddIndexer (i);
1317 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, lexer.Location);
1322 interface_method_declaration
1323 : opt_attributes opt_new type IDENTIFIER
1324 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1327 $$ = new Method (current_class, (Expression) $3, (int) $2, true,
1328 (string) $4, (Parameters) $6, (Attributes) $1,
1331 | opt_attributes opt_new VOID IDENTIFIER
1332 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1335 $$ = new Method (current_class, TypeManager.system_void_expr, (int) $2,
1336 true, (string) $4, (Parameters) $6, (Attributes) $1,
1341 interface_property_declaration
1346 { lexer.PropertyParsing = true; }
1348 { lexer.PropertyParsing = false; }
1351 InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
1353 $$ = new Property (current_class, (Expression) $3, (int) $2, true,
1354 (string) $4, (Attributes) $1, pinfo.Get, pinfo.Set,
1360 CheckIdentifierToken (yyToken);
1366 : opt_attributes GET SEMICOLON { $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null); }
1367 | opt_attributes SET SEMICOLON { $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1); }
1368 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1369 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $3); }
1370 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1371 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $3, (Attributes) $1); }
1374 interface_event_declaration
1375 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1377 $$ = new EventField (current_class, (Expression) $4, (int) $2, true, (string) $5, null,
1378 (Attributes) $1, lexer.Location);
1380 | opt_attributes opt_new EVENT type error {
1381 CheckIdentifierToken (yyToken);
1384 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1385 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1388 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE event_accessor_declarations CLOSE_BRACE {
1389 Report.Error (69, lexer.Location, "Event accessors not valid on interfaces");
1394 interface_indexer_declaration
1395 : opt_attributes opt_new type THIS
1396 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1398 { lexer.PropertyParsing = true; }
1400 { lexer.PropertyParsing = false; }
1403 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1405 $$ = new Indexer (current_class, (Expression) $3, null, (int) $2, true,
1406 (Parameters) $6, (Attributes) $1, info.Get, info.Set,
1411 operator_declaration
1412 : opt_attributes opt_modifiers operator_declarator
1414 iterator_container = SimpleIteratorContainer.GetSimple ();
1418 OperatorDeclaration decl = (OperatorDeclaration) $3;
1420 Operator op = new Operator (
1421 current_class, decl.optype, decl.ret_type, (int) $2, decl.arg1type,
1422 decl.arg1name, decl.arg2type, decl.arg2name, (Block) $5,
1423 (Attributes) $1, decl.location);
1425 if (SimpleIteratorContainer.Simple.Yields)
1428 // Note again, checking is done in semantic analysis
1429 current_container.AddOperator (op);
1431 current_local_parameters = null;
1432 iterator_container = null;
1438 | SEMICOLON { $$ = null; }
1441 : type OPERATOR overloadable_operator
1442 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1444 Operator.OpType op = (Operator.OpType) $3;
1445 CheckUnaryOperator (op);
1447 if (op == Operator.OpType.Addition)
1448 op = Operator.OpType.UnaryPlus;
1450 if (op == Operator.OpType.Subtraction)
1451 op = Operator.OpType.UnaryNegation;
1453 Parameter [] pars = new Parameter [1];
1455 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1457 current_local_parameters = new Parameters (pars, null, lexer.Location);
1459 $$ = new OperatorDeclaration (op, (Expression) $1, (Expression) $5, (string) $6,
1460 null, null, lexer.Location);
1462 | type OPERATOR overloadable_operator
1464 type IDENTIFIER COMMA
1468 CheckBinaryOperator ((Operator.OpType) $3);
1470 Parameter [] pars = new Parameter [2];
1472 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1473 pars [1] = new Parameter ((Expression) $8, (string) $9, Parameter.Modifier.NONE, null);
1475 current_local_parameters = new Parameters (pars, null, lexer.Location);
1477 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1478 (Expression) $5, (string) $6,
1479 (Expression) $8, (string) $9, lexer.Location);
1481 | conversion_operator_declarator
1484 overloadable_operator
1486 : BANG { $$ = Operator.OpType.LogicalNot; }
1487 | TILDE { $$ = Operator.OpType.OnesComplement; }
1488 | OP_INC { $$ = Operator.OpType.Increment; }
1489 | OP_DEC { $$ = Operator.OpType.Decrement; }
1490 | TRUE { $$ = Operator.OpType.True; }
1491 | FALSE { $$ = Operator.OpType.False; }
1492 // Unary and binary:
1493 | PLUS { $$ = Operator.OpType.Addition; }
1494 | MINUS { $$ = Operator.OpType.Subtraction; }
1496 | STAR { $$ = Operator.OpType.Multiply; }
1497 | DIV { $$ = Operator.OpType.Division; }
1498 | PERCENT { $$ = Operator.OpType.Modulus; }
1499 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1500 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1501 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1502 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1503 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1504 | OP_EQ { $$ = Operator.OpType.Equality; }
1505 | OP_NE { $$ = Operator.OpType.Inequality; }
1506 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1507 | OP_LT { $$ = Operator.OpType.LessThan; }
1508 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1509 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1512 conversion_operator_declarator
1513 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1515 Parameter [] pars = new Parameter [1];
1517 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1519 current_local_parameters = new Parameters (pars, null, lexer.Location);
1521 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1522 null, null, lexer.Location);
1524 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1526 Parameter [] pars = new Parameter [1];
1528 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1530 current_local_parameters = new Parameters (pars, null, lexer.Location);
1532 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1533 null, null, lexer.Location);
1537 syntax_error (lexer.Location, "'operator' expected");
1541 syntax_error (lexer.Location, "'operator' expected");
1545 constructor_declaration
1548 constructor_declarator
1551 Constructor c = (Constructor) $3;
1552 c.Block = (Block) $4;
1553 c.OptAttributes = (Attributes) $1;
1554 c.ModFlags = (int) $2;
1556 if (c.Name == current_container.Basename){
1557 if ((c.ModFlags & Modifiers.STATIC) != 0){
1558 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1560 515, c.Location, String.Format (
1561 "`{0}.{1}': static constructor can not have access modifiers",
1562 c.Name, current_container.Name));
1565 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1567 if (c.Initializer != null){
1570 "Static constructors can not have an explicit this or base " +
1571 "constructor invocations");
1574 if (!c.Parameters.Empty){
1576 132, c.Location, "Static constructors should not have parameters");
1579 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1582 // We let another layer check the validity of the constructor.
1583 Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1586 CheckDef (current_container.AddConstructor (c), c.Name, c.Location);
1588 current_local_parameters = null;
1592 constructor_declarator
1594 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1596 oob_stack.Push (lexer.Location);
1598 current_local_parameters = (Parameters) $3;
1600 opt_constructor_initializer
1602 Location l = (Location) oob_stack.Pop ();
1603 $$ = new Constructor (current_class, (string) $1, 0, (Parameters) $3,
1604 (ConstructorInitializer) $6, l);
1610 | SEMICOLON { $$ = null; }
1613 opt_constructor_initializer
1614 : /* empty */ { $$ = null; }
1615 | constructor_initializer
1618 constructor_initializer
1619 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1621 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1623 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1625 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1628 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1634 : /* EMPTY */ { $$ = 0; }
1635 | UNSAFE { $$ = Modifiers.UNSAFE; }
1636 | EXTERN { $$ = Modifiers.EXTERN; }
1639 destructor_declaration
1640 : opt_attributes opt_finalizer TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1642 if ((string) $4 != current_container.Basename){
1643 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1644 } else if (!(current_container is Class)){
1645 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1647 Location l = lexer.Location;
1650 if (!RootContext.StdLib && current_container.Name == "System.Object")
1651 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
1653 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
1655 if ((m & Modifiers.UNSAFE) != 0){
1656 if (!RootContext.Unsafe){
1657 Report.Error (227, l,
1658 "Unsafe code requires the -unsafe command " +
1659 "line option to be specified");
1663 Method d = new Destructor (
1664 current_class, TypeManager.system_void_expr, m, "Finalize",
1665 new Parameters (null, null, l), (Attributes) $1, l);
1667 d.Block = (Block) $7;
1668 CheckDef (current_container.AddMethod (d), d.Name, d.Location);
1676 EVENT type variable_declarators SEMICOLON
1678 foreach (VariableDeclaration var in (ArrayList) $5) {
1680 Event e = new EventField (current_class, (Expression) $4, (int) $2, false, var.identifier,
1681 var.expression_or_array_initializer,
1682 (Attributes) $1, lexer.Location);
1684 CheckDef (current_container.AddEvent (e), e.Name, e.Location);
1690 EVENT type member_name
1693 implicit_value_parameter_type = (Expression) $4;
1694 lexer.EventParsing = true;
1695 oob_stack.Push (lexer.Location);
1697 event_accessor_declarations
1699 lexer.EventParsing = false;
1703 Location loc = (Location) oob_stack.Pop ();
1706 Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
1709 Pair pair = (Pair) $8;
1711 Event e = new EventProperty (current_class, (Expression) $4, (int) $2, false, (string) $5,
1712 null, (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
1715 CheckDef (current_container.AddEvent (e), e.Name, loc);
1716 implicit_value_parameter_type = null;
1719 | opt_attributes opt_modifiers EVENT type member_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS block {
1720 string mn = (string) $5;
1722 if (mn.IndexOf ('.') != -1)
1723 Report.Error (71, lexer.Location, "Explicit implementation of events requires property syntax");
1725 Report.Error (71, lexer.Location, "Event declaration should use property syntax");
1729 event_accessor_declarations
1730 : add_accessor_declaration remove_accessor_declaration
1732 $$ = new Pair ($1, $2);
1734 | remove_accessor_declaration add_accessor_declaration
1736 $$ = new Pair ($2, $1);
1738 | add_accessor_declaration { $$ = null; }
1739 | remove_accessor_declaration { $$ = null; }
1742 add_accessor_declaration
1743 : opt_attributes ADD
1745 Parameter [] args = new Parameter [1];
1746 Parameter implicit_value_parameter = new Parameter (
1747 implicit_value_parameter_type, "value",
1748 Parameter.Modifier.NONE, null);
1750 args [0] = implicit_value_parameter;
1752 current_local_parameters = new Parameters (args, null, lexer.Location);
1753 lexer.EventParsing = false;
1757 $$ = new Accessor ((Block) $4, (Attributes) $1);
1758 lexer.EventParsing = true;
1760 | opt_attributes ADD error {
1761 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1766 remove_accessor_declaration
1767 : opt_attributes REMOVE
1769 Parameter [] args = new Parameter [1];
1770 Parameter implicit_value_parameter = new Parameter (
1771 implicit_value_parameter_type, "value",
1772 Parameter.Modifier.NONE, null);
1774 args [0] = implicit_value_parameter;
1776 current_local_parameters = new Parameters (args, null, lexer.Location);
1777 lexer.EventParsing = false;
1781 $$ = new Accessor ((Block) $4, (Attributes) $1);
1782 lexer.EventParsing = true;
1784 | opt_attributes REMOVE error {
1785 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1791 : opt_attributes opt_modifiers indexer_declarator
1794 IndexerDeclaration decl = (IndexerDeclaration) $3;
1796 implicit_value_parameter_type = decl.type;
1798 lexer.PropertyParsing = true;
1799 parsing_indexer = true;
1801 indexer_parameters = decl.param_list;
1802 oob_stack.Push (lexer.Location);
1804 accessor_declarations
1806 lexer.PropertyParsing = false;
1807 parsing_indexer = false;
1811 // The signature is computed from the signature of the indexer. Look
1812 // at section 3.6 on the spec
1813 Location loc = (Location) oob_stack.Pop ();
1815 IndexerDeclaration decl = (IndexerDeclaration) $3;
1816 Pair pair = (Pair) $6;
1817 Accessor get_block = (Accessor) pair.First;
1818 Accessor set_block = (Accessor) pair.Second;
1820 indexer = new Indexer (current_class, decl.type, decl.interface_type,
1821 (int) $2, false, decl.param_list, (Attributes) $1,
1822 get_block, set_block, loc);
1824 // Note that there is no equivalent of CheckDef for this case
1825 // We shall handle this in semantic analysis
1827 current_container.AddIndexer (indexer);
1829 current_local_parameters = null;
1830 implicit_value_parameter_type = null;
1831 indexer_parameters = null;
1836 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1838 Parameters pars = (Parameters) $4;
1839 if (pars.HasArglist) {
1840 // "__arglist is not valid in this context"
1841 Report.Error_T (1669, lexer.Location);
1842 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
1843 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1846 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
1848 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1850 Parameters pars = (Parameters) $6;
1852 if (pars.HasArglist) {
1853 // "__arglist is not valid in this context"
1854 Report.Error_T (1669, lexer.Location);
1855 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
1856 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1858 $$ = new IndexerDeclaration ((Expression) $1, $2.ToString (), pars);
1870 Location enum_location = lexer.Location;
1872 string full_name = MakeName ((string) $4);
1873 Enum e = new Enum (current_namespace, current_container, (Expression) $5, (int) $2,
1874 full_name, (Attributes) $1, enum_location);
1876 foreach (VariableDeclaration ev in (ArrayList) $6) {
1877 Location loc = (Location) ev.Location;
1879 CheckDef (e.AddEnumMember (ev.identifier,
1880 (Expression) ev.expression_or_array_initializer,
1881 loc, ev.OptAttributes),
1882 ev.identifier, loc);
1885 CheckDef (current_container.AddEnum (e), full_name, enum_location);
1886 RootContext.Tree.RecordDecl (full_name, e);
1892 : /* empty */ { $$ = TypeManager.system_int32_expr; }
1893 | COLON type { $$ = $2; }
1897 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1903 opt_enum_member_declarations
1904 : /* empty */ { $$ = new ArrayList (4); }
1905 | enum_member_declarations opt_comma { $$ = $1; }
1908 enum_member_declarations
1909 : enum_member_declaration
1911 ArrayList l = new ArrayList (4);
1916 | enum_member_declarations COMMA enum_member_declaration
1918 ArrayList l = (ArrayList) $1;
1926 enum_member_declaration
1927 : opt_attributes IDENTIFIER
1929 $$ = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
1931 | opt_attributes IDENTIFIER
1933 $$ = lexer.Location;
1937 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
1941 delegate_declaration
1945 IDENTIFIER OPEN_PARENS
1946 opt_formal_parameter_list
1950 Location l = lexer.Location;
1951 Delegate del = new Delegate (current_namespace, current_container, (Expression) $4,
1952 (int) $2, MakeName ((string) $5), (Parameters) $7,
1953 (Attributes) $1, l);
1955 CheckDef (current_container.AddDelegate (del), del.Name, l);
1960 IDENTIFIER OPEN_PARENS
1961 opt_formal_parameter_list
1965 Location l = lexer.Location;
1966 Delegate del = new Delegate (
1967 current_namespace, current_container,
1968 TypeManager.system_void_expr, (int) $2, MakeName ((string) $5),
1969 (Parameters) $7, (Attributes) $1, l);
1971 CheckDef (current_container.AddDelegate (del), del.Name, l);
1976 : namespace_or_type_name
1979 namespace_or_type_name
1980 : IDENTIFIER { $$ = new SimpleName ((string) $1, lexer.Location);}
1981 | namespace_or_type_name DOT IDENTIFIER {
1982 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1987 * Before you think of adding a return_type, notice that we have been
1988 * using two rules in the places where it matters (one rule using type
1989 * and another identical one that uses VOID as the return type). This
1990 * gets rid of a shift/reduce couple
1993 : type_name { /* class_type */
1995 This does interfaces, delegates, struct_types, class_types,
1996 parent classes, and more! 4.2
2009 // Note that here only unmanaged types are allowed but we
2010 // can't perform checks during this phase - we do it during
2011 // semantic analysis.
2013 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2017 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2023 | non_expression_type rank_specifier
2025 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2027 | non_expression_type STAR
2029 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2031 | expression rank_specifiers
2033 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2037 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2041 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2042 // through this different path
2044 | multiplicative_expression STAR
2046 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2053 ArrayList types = new ArrayList (4);
2058 | type_list COMMA type
2060 ArrayList types = (ArrayList) $1;
2068 * replaces all the productions for isolating the various
2069 * simple types, but we need this to reuse it easily in local_variable_type
2072 : OBJECT { $$ = TypeManager.system_object_expr; }
2073 | STRING { $$ = TypeManager.system_string_expr; }
2074 | BOOL { $$ = TypeManager.system_boolean_expr; }
2075 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2076 | FLOAT { $$ = TypeManager.system_single_expr; }
2077 | DOUBLE { $$ = TypeManager.system_double_expr; }
2082 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2083 | BYTE { $$ = TypeManager.system_byte_expr; }
2084 | SHORT { $$ = TypeManager.system_int16_expr; }
2085 | USHORT { $$ = TypeManager.system_uint16_expr; }
2086 | INT { $$ = TypeManager.system_int32_expr; }
2087 | UINT { $$ = TypeManager.system_uint32_expr; }
2088 | LONG { $$ = TypeManager.system_int64_expr; }
2089 | ULONG { $$ = TypeManager.system_uint64_expr; }
2090 | CHAR { $$ = TypeManager.system_char_expr; }
2091 | VOID { $$ = TypeManager.system_void_expr; }
2099 : type rank_specifiers
2101 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2106 // Expressions, section 7.5
2114 | IDENTIFIER { $$ = new SimpleName ((string) $1, lexer.Location); }
2115 | parenthesized_expression
2117 | invocation_expression
2121 | post_increment_expression
2122 | post_decrement_expression
2126 | checked_expression
2127 | unchecked_expression
2128 | pointer_member_access
2129 | anonymous_method_expression
2136 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2137 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2138 | NULL { $$ = NullLiteral.Null; }
2142 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2143 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2144 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2149 object v = lexer.Value;
2155 $$ = IntLiteral.Zero;
2157 $$ = IntLiteral.One;
2159 $$ = new IntLiteral (i);
2160 } else if (v is uint)
2161 $$ = new UIntLiteral ((UInt32) v);
2163 $$ = new LongLiteral ((Int64) v);
2164 else if (v is ulong)
2165 $$ = new ULongLiteral ((UInt64) v);
2167 Console.WriteLine ("OOPS. Unexpected result from scanner");
2172 : TRUE { $$ = new BoolLiteral (true); }
2173 | FALSE { $$ = new BoolLiteral (false); }
2176 parenthesized_expression_0
2177 : OPEN_PARENS expression CLOSE_PARENS
2180 lexer.Deambiguate_CloseParens ();
2181 // After this, the next token returned is one of
2182 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2183 // or CLOSE_PARENS_MINUS.
2187 parenthesized_expression
2188 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2192 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2194 // If a parenthesized expression is followed by a minus, we need to wrap
2195 // the expression inside a ParenthesizedExpression for the CS0075 check
2196 // in Binary.DoResolve().
2197 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2202 : primary_expression DOT IDENTIFIER
2204 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2206 | predefined_type DOT IDENTIFIER
2208 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2216 invocation_expression
2217 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2220 Location l = lexer.Location;
2221 Report.Error (1, l, "Parse error");
2223 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2225 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2227 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2229 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2231 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2236 : /* empty */ { $$ = null; }
2243 ArrayList list = new ArrayList (4);
2247 | argument_list COMMA argument
2249 ArrayList list = (ArrayList) $1;
2253 | argument_list error {
2254 CheckToken (1026, yyToken, ", or ) expected");
2261 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2263 | REF variable_reference
2265 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2267 | OUT variable_reference
2269 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2271 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2273 ArrayList list = (ArrayList) $3;
2274 Argument[] args = new Argument [list.Count];
2275 list.CopyTo (args, 0);
2277 Expression expr = new Arglist (args, lexer.Location);
2278 $$ = new Argument (expr, Argument.AType.Expression);
2282 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2287 : expression { note ("section 5.4"); $$ = $1; }
2291 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2293 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2295 | primary_expression rank_specifiers
2297 // So the super-trick is that primary_expression
2298 // can only be either a SimpleName or a MemberAccess.
2299 // The MemberAccess case arises when you have a fully qualified type-name like :
2301 // SimpleName is when you have
2304 Expression expr = (Expression) $1;
2305 if (expr is ComposedCast){
2306 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2307 } else if (!(expr is SimpleName || expr is MemberAccess)){
2308 Error_ExpectingTypeName (lexer.Location, expr);
2309 $$ = TypeManager.system_object_expr;
2312 // So we extract the string corresponding to the SimpleName
2315 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2323 ArrayList list = new ArrayList (4);
2327 | expression_list COMMA expression
2329 ArrayList list = (ArrayList) $1;
2338 $$ = new This (current_block, lexer.Location);
2343 : BASE DOT IDENTIFIER
2345 $$ = new BaseAccess ((string) $3, lexer.Location);
2347 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2349 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2352 Report.Error (175, "Use of keyword `base' is not valid in this context");
2357 post_increment_expression
2358 : primary_expression OP_INC
2360 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2361 (Expression) $1, lexer.Location);
2365 post_decrement_expression
2366 : primary_expression OP_DEC
2368 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2369 (Expression) $1, lexer.Location);
2374 : object_or_delegate_creation_expression
2375 | array_creation_expression
2378 object_or_delegate_creation_expression
2379 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2381 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2385 array_creation_expression
2386 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2388 opt_array_initializer
2390 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2392 | NEW type rank_specifiers array_initializer
2394 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2398 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2414 : rank_specifier opt_rank_specifier
2416 $$ = (string) $2 + (string) $1;
2421 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2423 $$ = "[" + (string) $2 + "]";
2443 | dim_separators COMMA
2445 $$ = (string) $1 + ",";
2449 opt_array_initializer
2461 : OPEN_BRACE CLOSE_BRACE
2463 ArrayList list = new ArrayList (4);
2466 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2468 $$ = (ArrayList) $2;
2472 variable_initializer_list
2473 : variable_initializer
2475 ArrayList list = new ArrayList (4);
2479 | variable_initializer_list COMMA variable_initializer
2481 ArrayList list = (ArrayList) $1;
2488 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2490 $$ = new TypeOfVoid (lexer.Location);
2492 | TYPEOF OPEN_PARENS type CLOSE_PARENS
2494 $$ = new TypeOf ((Expression) $3, lexer.Location);
2499 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2500 $$ = new SizeOf ((Expression) $3, lexer.Location);
2505 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2507 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2511 unchecked_expression
2512 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2514 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2518 pointer_member_access
2519 : primary_expression OP_PTR IDENTIFIER
2523 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2524 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2528 anonymous_method_expression
2529 : DELEGATE opt_anonymous_method_signature {
2530 oob_stack.Push (current_local_parameters);
2531 current_local_parameters = (Parameters)$2;
2532 create_toplevel_block = true;
2535 Report.Error (-213, lexer.Location, "Anonymous methods are not supported in this branch");
2538 create_toplevel_block = false;
2539 if (!RootContext.V2){
2540 Report.Error (-213, lexer.Location, "Anonymous methods are only supported in V2");
2543 $$ = new AnonymousMethod ((Parameters) $2, (Block) $4, lexer.Location);
2544 current_local_parameters = (Parameters) oob_stack.Pop ();
2549 opt_anonymous_method_signature
2550 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
2551 | anonymous_method_signature
2554 anonymous_method_signature
2555 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
2558 $$ = Parameters.EmptyReadOnlyParameters;
2560 ArrayList par_list = (ArrayList) $2;
2561 Parameter [] pars = new Parameter [par_list.Count];
2562 par_list.CopyTo (pars);
2563 $$ = new Parameters (pars, null, lexer.Location);
2568 opt_anonymous_method_parameter_list
2569 : /* empty */ { $$ = null; }
2570 | anonymous_method_parameter_list { $$ = $1; }
2573 anonymous_method_parameter_list
2574 : anonymous_method_parameter
2576 ArrayList a = new ArrayList (4);
2580 | anonymous_method_parameter_list COMMA anonymous_method_parameter
2582 ArrayList a = (ArrayList) $1;
2588 anonymous_method_parameter
2589 : opt_parameter_modifier type IDENTIFIER {
2590 $$ = new Parameter ((Expression) $2, (string) $2, (Parameter.Modifier) $1, null);
2595 : primary_expression
2596 | BANG prefixed_unary_expression
2598 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2600 | TILDE prefixed_unary_expression
2602 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2608 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
2610 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2612 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
2614 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2620 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2622 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2627 // The idea to split this out is from Rhys' grammar
2628 // to solve the problem with casts.
2630 prefixed_unary_expression
2632 | PLUS prefixed_unary_expression
2634 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2636 | MINUS prefixed_unary_expression
2638 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2640 | OP_INC prefixed_unary_expression
2642 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2643 (Expression) $2, lexer.Location);
2645 | OP_DEC prefixed_unary_expression
2647 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2648 (Expression) $2, lexer.Location);
2650 | STAR prefixed_unary_expression
2652 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2654 | BITWISE_AND prefixed_unary_expression
2656 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2660 pre_increment_expression
2661 : OP_INC prefixed_unary_expression
2663 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2664 (Expression) $2, lexer.Location);
2668 pre_decrement_expression
2669 : OP_DEC prefixed_unary_expression
2671 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2672 (Expression) $2, lexer.Location);
2676 multiplicative_expression
2677 : prefixed_unary_expression
2678 | multiplicative_expression STAR prefixed_unary_expression
2680 $$ = new Binary (Binary.Operator.Multiply,
2681 (Expression) $1, (Expression) $3, lexer.Location);
2683 | multiplicative_expression DIV prefixed_unary_expression
2685 $$ = new Binary (Binary.Operator.Division,
2686 (Expression) $1, (Expression) $3, lexer.Location);
2688 | multiplicative_expression PERCENT prefixed_unary_expression
2690 $$ = new Binary (Binary.Operator.Modulus,
2691 (Expression) $1, (Expression) $3, lexer.Location);
2696 : multiplicative_expression
2697 | additive_expression PLUS multiplicative_expression
2699 $$ = new Binary (Binary.Operator.Addition,
2700 (Expression) $1, (Expression) $3, lexer.Location);
2702 | additive_expression MINUS multiplicative_expression
2704 $$ = new Binary (Binary.Operator.Subtraction,
2705 (Expression) $1, (Expression) $3, lexer.Location);
2710 : additive_expression
2711 | shift_expression OP_SHIFT_LEFT additive_expression
2713 $$ = new Binary (Binary.Operator.LeftShift,
2714 (Expression) $1, (Expression) $3, lexer.Location);
2716 | shift_expression OP_SHIFT_RIGHT additive_expression
2718 $$ = new Binary (Binary.Operator.RightShift,
2719 (Expression) $1, (Expression) $3, lexer.Location);
2723 relational_expression
2725 | relational_expression OP_LT shift_expression
2727 $$ = new Binary (Binary.Operator.LessThan,
2728 (Expression) $1, (Expression) $3, lexer.Location);
2730 | relational_expression OP_GT shift_expression
2732 $$ = new Binary (Binary.Operator.GreaterThan,
2733 (Expression) $1, (Expression) $3, lexer.Location);
2735 | relational_expression OP_LE shift_expression
2737 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2738 (Expression) $1, (Expression) $3, lexer.Location);
2740 | relational_expression OP_GE shift_expression
2742 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2743 (Expression) $1, (Expression) $3, lexer.Location);
2745 | relational_expression IS type
2747 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
2749 | relational_expression AS type
2751 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
2756 : relational_expression
2757 | equality_expression OP_EQ relational_expression
2759 $$ = new Binary (Binary.Operator.Equality,
2760 (Expression) $1, (Expression) $3, lexer.Location);
2762 | equality_expression OP_NE relational_expression
2764 $$ = new Binary (Binary.Operator.Inequality,
2765 (Expression) $1, (Expression) $3, lexer.Location);
2770 : equality_expression
2771 | and_expression BITWISE_AND equality_expression
2773 $$ = new Binary (Binary.Operator.BitwiseAnd,
2774 (Expression) $1, (Expression) $3, lexer.Location);
2778 exclusive_or_expression
2780 | exclusive_or_expression CARRET and_expression
2782 $$ = new Binary (Binary.Operator.ExclusiveOr,
2783 (Expression) $1, (Expression) $3, lexer.Location);
2787 inclusive_or_expression
2788 : exclusive_or_expression
2789 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2791 $$ = new Binary (Binary.Operator.BitwiseOr,
2792 (Expression) $1, (Expression) $3, lexer.Location);
2796 conditional_and_expression
2797 : inclusive_or_expression
2798 | conditional_and_expression OP_AND inclusive_or_expression
2800 $$ = new Binary (Binary.Operator.LogicalAnd,
2801 (Expression) $1, (Expression) $3, lexer.Location);
2805 conditional_or_expression
2806 : conditional_and_expression
2807 | conditional_or_expression OP_OR conditional_and_expression
2809 $$ = new Binary (Binary.Operator.LogicalOr,
2810 (Expression) $1, (Expression) $3, lexer.Location);
2814 conditional_expression
2815 : conditional_or_expression
2816 | conditional_or_expression INTERR expression COLON expression
2818 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2822 assignment_expression
2823 : prefixed_unary_expression ASSIGN expression
2825 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2827 | prefixed_unary_expression OP_MULT_ASSIGN expression
2829 Location l = lexer.Location;
2831 $$ = new CompoundAssign (
2832 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
2834 | prefixed_unary_expression OP_DIV_ASSIGN expression
2836 Location l = lexer.Location;
2838 $$ = new CompoundAssign (
2839 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
2841 | prefixed_unary_expression OP_MOD_ASSIGN expression
2843 Location l = lexer.Location;
2845 $$ = new CompoundAssign (
2846 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
2848 | prefixed_unary_expression OP_ADD_ASSIGN expression
2850 Location l = lexer.Location;
2852 $$ = new CompoundAssign (
2853 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
2855 | prefixed_unary_expression OP_SUB_ASSIGN expression
2857 Location l = lexer.Location;
2859 $$ = new CompoundAssign (
2860 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
2862 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2864 Location l = lexer.Location;
2866 $$ = new CompoundAssign (
2867 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
2869 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2871 Location l = lexer.Location;
2873 $$ = new CompoundAssign (
2874 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
2876 | prefixed_unary_expression OP_AND_ASSIGN expression
2878 Location l = lexer.Location;
2880 $$ = new CompoundAssign (
2881 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
2883 | prefixed_unary_expression OP_OR_ASSIGN expression
2885 Location l = lexer.Location;
2887 $$ = new CompoundAssign (
2888 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
2890 | prefixed_unary_expression OP_XOR_ASSIGN expression
2892 Location l = lexer.Location;
2894 $$ = new CompoundAssign (
2895 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
2900 : conditional_expression
2901 | assignment_expression
2921 string name = MakeName ((string) $5);
2922 bool partial = (bool) $3;
2925 ClassPart part = PartialContainer.CreatePart (
2926 current_namespace, current_container, name, (int) $2,
2927 (Attributes) $1, Kind.Class, lexer.Location);
2929 current_container = part.PartialContainer;
2930 current_class = part;
2932 current_class = new Class (
2933 current_namespace, current_container, name, (int) $2,
2934 (Attributes) $1, lexer.Location);
2936 current_container = current_class;
2937 RootContext.Tree.RecordDecl (name, current_class);
2943 if (current_class.Name == "System.Object") {
2944 Report.Error (537, current_class.Location,
2945 "The class System.Object cannot have a base " +
2946 "class or implement an interface.");
2948 current_class.Bases = (ArrayList) $7;
2951 current_class.Register ();
2958 current_container = current_container.Parent;
2959 current_class = current_container;
2965 { $$ = (bool) false; }
2967 { $$ = (bool) true; }
2971 : /* empty */ { $$ = (int) 0; }
2977 | modifiers modifier
2982 if ((m1 & m2) != 0) {
2983 Location l = lexer.Location;
2984 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2986 $$ = (int) (m1 | m2);
2991 : NEW { $$ = Modifiers.NEW; }
2992 | PUBLIC { $$ = Modifiers.PUBLIC; }
2993 | PROTECTED { $$ = Modifiers.PROTECTED; }
2994 | INTERNAL { $$ = Modifiers.INTERNAL; }
2995 | PRIVATE { $$ = Modifiers.PRIVATE; }
2996 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2997 | SEALED { $$ = Modifiers.SEALED; }
2998 | STATIC { $$ = Modifiers.STATIC; }
2999 | READONLY { $$ = Modifiers.READONLY; }
3000 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3001 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3002 | EXTERN { $$ = Modifiers.EXTERN; }
3003 | VOLATILE { $$ = Modifiers.VOLATILE; }
3004 | UNSAFE { $$ = Modifiers.UNSAFE; }
3008 : /* empty */ { $$ = null; }
3009 | class_base { $$ = $1; }
3013 : COLON type_list { $$ = $2; }
3021 // A block is "contained" on the following places:
3023 // property_declaration as part of the accessor body (get/set)
3024 // operator_declaration
3025 // constructor_declaration
3026 // destructor_declaration
3027 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3032 if (current_block == null || create_toplevel_block){
3033 current_block = new ToplevelBlock (current_local_parameters, lexer.Location);
3035 current_block = new Block (current_block, current_local_parameters,
3036 lexer.Location, Location.Null);
3039 opt_statement_list CLOSE_BRACE
3041 while (current_block.Implicit)
3042 current_block = current_block.Parent;
3044 current_block.SetEndLocation (lexer.Location);
3045 current_block = current_block.Parent;
3056 | statement_list statement
3060 : declaration_statement
3062 if ($1 != null && (Block) $1 != current_block){
3063 current_block.AddStatement ((Statement) $1);
3064 current_block = (Block) $1;
3067 | valid_declaration_statement
3069 current_block.AddStatement ((Statement) $1);
3074 valid_declaration_statement
3077 | expression_statement
3078 | selection_statement
3079 | iteration_statement
3083 | unchecked_statement
3091 : valid_declaration_statement
3092 | declaration_statement
3094 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3099 Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3107 $$ = EmptyStatement.Value;
3114 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3116 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3117 current_block.AddStatement (labeled);
3122 declaration_statement
3123 : local_variable_declaration SEMICOLON
3126 DictionaryEntry de = (DictionaryEntry) $1;
3128 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3132 | local_constant_declaration SEMICOLON
3135 DictionaryEntry de = (DictionaryEntry) $1;
3137 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3143 * The following is from Rhys' grammar:
3144 * > Types in local variable declarations must be recognized as
3145 * > expressions to prevent reduce/reduce errors in the grammar.
3146 * > The expressions are converted into types during semantic analysis.
3149 : primary_expression opt_rank_specifier
3151 // FIXME: Do something smart here regarding the composition of the type.
3153 // Ok, the above "primary_expression" is there to get rid of
3154 // both reduce/reduce and shift/reduces in the grammar, it should
3155 // really just be "type_name". If you use type_name, a reduce/reduce
3156 // creeps up. If you use namespace_or_type_name (which is all we need
3157 // really) two shift/reduces appear.
3160 // So the super-trick is that primary_expression
3161 // can only be either a SimpleName or a MemberAccess.
3162 // The MemberAccess case arises when you have a fully qualified type-name like :
3164 // SimpleName is when you have
3167 Expression expr = (Expression) $1;
3168 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3169 Error_ExpectingTypeName (lexer.Location, expr);
3173 // So we extract the string corresponding to the SimpleName
3177 if ((string) $2 == "")
3180 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3183 | builtin_types opt_rank_specifier
3185 if ((string) $2 == "")
3188 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3192 local_variable_pointer_type
3193 : primary_expression STAR
3195 Expression expr = (Expression) $1;
3196 Location l = lexer.Location;
3198 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3199 Error_ExpectingTypeName (l, expr);
3203 $$ = new ComposedCast ((Expression) $1, "*", l);
3205 | builtin_types STAR
3207 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3211 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3213 | local_variable_pointer_type STAR
3215 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3219 local_variable_declaration
3220 : local_variable_type variable_declarators
3223 $$ = new DictionaryEntry ($1, $2);
3227 | local_variable_pointer_type opt_rank_specifier variable_declarators
3232 if ((string) $2 == "")
3233 t = (Expression) $1;
3235 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3236 $$ = new DictionaryEntry (t, $3);
3242 local_constant_declaration
3243 : CONST local_variable_type constant_declarators
3246 $$ = new DictionaryEntry ($2, $3);
3252 expression_statement
3253 : statement_expression SEMICOLON
3260 // We have to do the wrapping here and not in the case above,
3261 // because statement_expression is used for example in for_statement
3263 statement_expression
3264 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3265 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3266 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3267 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3268 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3269 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3270 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3272 Report.Error (1002, lexer.Location, "Expecting `;'");
3277 object_creation_expression
3278 : object_or_delegate_creation_expression
3279 { note ("complain if this is a delegate maybe?"); }
3288 : if_statement_open if_statement_rest
3297 oob_stack.Push (lexer.Location);
3302 : boolean_expression CLOSE_PARENS
3305 Location l = (Location) oob_stack.Pop ();
3307 $$ = new If ((Expression) $1, (Statement) $3, l);
3309 if (RootContext.WarningLevel >= 3){
3310 if ($3 == EmptyStatement.Value)
3311 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3315 | boolean_expression CLOSE_PARENS
3316 embedded_statement ELSE embedded_statement
3318 Location l = (Location) oob_stack.Pop ();
3320 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
3325 : SWITCH OPEN_PARENS
3327 oob_stack.Push (lexer.Location);
3328 switch_stack.Push (current_block);
3330 expression CLOSE_PARENS
3333 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
3334 current_block = (Block) switch_stack.Pop ();
3350 Report.Error (1522, lexer.Location, "Empty switch block");
3358 ArrayList sections = new ArrayList (4);
3363 | switch_sections switch_section
3365 ArrayList sections = (ArrayList) $1;
3375 current_block = current_block.CreateSwitchBlock (lexer.Location);
3379 Block topmost = current_block;
3381 while (topmost.Implicit)
3382 topmost = topmost.Parent;
3383 $$ = new SwitchSection ((ArrayList) $1, topmost);
3390 ArrayList labels = new ArrayList (4);
3395 | switch_labels switch_label
3397 ArrayList labels = (ArrayList) ($1);
3405 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3406 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3409 1523, lexer.Location,
3410 "The keyword case or default must precede code in switch block");
3424 oob_stack.Push (lexer.Location);
3426 boolean_expression CLOSE_PARENS embedded_statement
3428 Location l = (Location) oob_stack.Pop ();
3429 $$ = new While ((Expression) $4, (Statement) $6, l);
3431 if (RootContext.WarningLevel >= 3){
3432 if ($6 == EmptyStatement.Value)
3433 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3439 : DO embedded_statement
3441 oob_stack.Push (lexer.Location);
3443 boolean_expression CLOSE_PARENS SEMICOLON
3445 Location l = (Location) oob_stack.Pop ();
3447 $$ = new Do ((Statement) $2, (Expression) $6, l);
3453 opt_for_initializer SEMICOLON
3455 Block assign_block = new Block (current_block);
3456 current_block = assign_block;
3458 if ($3 is DictionaryEntry){
3459 DictionaryEntry de = (DictionaryEntry) $3;
3461 Expression type = (Expression) de.Key;
3462 ArrayList var_declarators = (ArrayList) de.Value;
3464 foreach (VariableDeclaration decl in var_declarators){
3468 vi = current_block.AddVariable (
3469 type, decl.identifier, current_local_parameters, decl.Location);
3473 Location l = lexer.Location;
3475 if (decl.expression_or_array_initializer is Expression){
3476 expr = (Expression) decl.expression_or_array_initializer;
3477 } else if (decl.expression_or_array_initializer == null) {
3480 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3481 expr = new ArrayCreation (type, "", init, decl.Location);
3484 LocalVariableReference var;
3485 var = new LocalVariableReference (assign_block, decl.identifier, l);
3488 Assign a = new Assign (var, expr, decl.Location);
3490 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3496 oob_stack.Push (lexer.Location);
3498 opt_for_condition SEMICOLON
3499 opt_for_iterator CLOSE_PARENS
3502 Location l = (Location) oob_stack.Pop ();
3504 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3506 if (RootContext.WarningLevel >= 3){
3507 if ($10 == EmptyStatement.Value)
3508 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3511 current_block.AddStatement (f);
3512 while (current_block.Implicit)
3513 current_block = current_block.Parent;
3515 current_block = current_block.Parent;
3520 : /* empty */ { $$ = EmptyStatement.Value; }
3525 : local_variable_declaration
3526 | statement_expression_list
3530 : /* empty */ { $$ = null; }
3531 | boolean_expression
3535 : /* empty */ { $$ = EmptyStatement.Value; }
3540 : statement_expression_list
3543 statement_expression_list
3544 : statement_expression
3546 // CHANGE: was `null'
3547 Block b = new Block (current_block, Block.Flags.Implicit);
3549 b.AddStatement ((Statement) $1);
3552 | statement_expression_list COMMA statement_expression
3554 Block b = (Block) $1;
3556 b.AddStatement ((Statement) $3);
3562 : FOREACH OPEN_PARENS type IDENTIFIER IN
3564 oob_stack.Push (lexer.Location);
3566 expression CLOSE_PARENS
3568 oob_stack.Push (current_block);
3570 Block foreach_block = new Block (current_block);
3571 LocalVariableReference v = null;
3572 Location l = lexer.Location;
3575 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3579 // Get a writable reference to this read-only variable.
3580 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3582 current_block = foreach_block;
3585 oob_stack.Push (current_block);
3589 Block foreach_block = (Block) oob_stack.Pop ();
3590 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3591 Block prev_block = (Block) oob_stack.Pop ();
3592 Location l = (Location) oob_stack.Pop ();
3594 current_block = prev_block;
3597 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3598 foreach_block.AddStatement (f);
3607 | continue_statement
3617 $$ = new Break (lexer.Location);
3622 : CONTINUE SEMICOLON
3624 $$ = new Continue (lexer.Location);
3629 : GOTO IDENTIFIER SEMICOLON
3631 $$ = new Goto (current_block, (string) $2, lexer.Location);
3633 | GOTO CASE constant_expression SEMICOLON
3635 $$ = new GotoCase ((Expression) $3, lexer.Location);
3637 | GOTO DEFAULT SEMICOLON
3639 $$ = new GotoDefault (lexer.Location);
3644 : RETURN opt_expression SEMICOLON
3646 $$ = new Return ((Expression) $2, lexer.Location);
3651 : THROW opt_expression SEMICOLON
3653 $$ = new Throw ((Expression) $2, lexer.Location);
3658 : IDENTIFIER RETURN expression SEMICOLON
3660 string s = (string) $1;
3662 Report.Error (1003, lexer.Location, "; expected");
3665 if (!RootContext.V2){
3666 Report.Error (-222, lexer.Location, "yield statement only available in C# 2.0 mode");
3669 if (iterator_container == null){
3670 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3673 iterator_container.SetYields ();
3674 $$ = new Yield ((Expression) $3, lexer.Location);
3677 | IDENTIFIER BREAK SEMICOLON
3679 string s = (string) $1;
3681 Report.Error (1003, lexer.Location, "; expected");
3684 if (!RootContext.V2){
3685 Report.Error (-222, lexer.Location, "yield statement only available in C# 2.0 mode");
3688 if (iterator_container == null){
3689 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3692 iterator_container.SetYields ();
3693 $$ = new YieldBreak (lexer.Location);
3704 : TRY block catch_clauses
3707 ArrayList s = new ArrayList (4);
3709 foreach (Catch cc in (ArrayList) $3) {
3716 // Now s contains the list of specific catch clauses
3717 // and g contains the general one.
3719 $$ = new Try ((Block) $2, s, g, null, lexer.Location);
3721 | TRY block opt_catch_clauses FINALLY block
3724 ArrayList s = new ArrayList (4);
3725 ArrayList catch_list = (ArrayList) $3;
3727 if (catch_list != null){
3728 foreach (Catch cc in catch_list) {
3736 $$ = new Try ((Block) $2, s, g, (Block) $5, lexer.Location);
3740 Report.Error (1524, lexer.Location, "Expected catch or finally");
3745 : /* empty */ { $$ = null; }
3752 ArrayList l = new ArrayList (4);
3757 | catch_clauses catch_clause
3759 ArrayList l = (ArrayList) $1;
3767 : /* empty */ { $$ = null; }
3772 : CATCH opt_catch_args
3774 Expression type = null;
3778 DictionaryEntry cc = (DictionaryEntry) $2;
3779 type = (Expression) cc.Key;
3780 id = (string) cc.Value;
3783 ArrayList one = new ArrayList (4);
3784 Location loc = lexer.Location;
3786 one.Add (new VariableDeclaration (id, null, loc));
3789 current_block = new Block (current_block);
3790 Block b = declare_local_variables (type, one, loc);
3795 Expression type = null;
3799 DictionaryEntry cc = (DictionaryEntry) $2;
3800 type = (Expression) cc.Key;
3801 id = (string) cc.Value;
3805 // FIXME: I can change this for an assignment.
3807 while (current_block != (Block) $1)
3808 current_block = current_block.Parent;
3813 $$ = new Catch (type, id , (Block) $4, lexer.Location);
3818 : /* empty */ { $$ = null; }
3823 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3825 $$ = new DictionaryEntry ($2, $3);
3832 $$ = new Checked ((Block) $2);
3839 $$ = new Unchecked ((Block) $2);
3846 if (!RootContext.Unsafe){
3847 Report.Error (227, lexer.Location,
3848 "Unsafe code can only be used if --unsafe is used");
3851 $$ = new Unsafe ((Block) $3);
3857 type fixed_pointer_declarators
3860 Block assign_block = new Block (current_block, Block.Flags.Implicit);
3861 ArrayList list = (ArrayList) $4;
3862 Expression type = (Expression) $3;
3863 Location l = lexer.Location;
3864 int top = list.Count;
3866 for (int i = 0; i < top; i++){
3867 Pair p = (Pair) list [i];
3870 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3879 current_block.AddStatement (assign_block);
3880 current_block = assign_block;
3881 oob_stack.Push (assign_block);
3886 Location l = (Location) oob_stack.Pop ();
3889 $$ = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
3893 fixed_pointer_declarators
3894 : fixed_pointer_declarator {
3895 ArrayList declarators = new ArrayList (4);
3897 declarators.Add ($1);
3900 | fixed_pointer_declarators COMMA fixed_pointer_declarator
3902 ArrayList declarators = (ArrayList) $1;
3904 declarators.Add ($3);
3909 fixed_pointer_declarator
3910 : IDENTIFIER ASSIGN expression
3912 $$ = new Pair ($1, $3);
3916 // You must provide an initializer in a fixed or using statement declaration
3917 Report.Error_T (210, lexer.Location);
3923 : LOCK OPEN_PARENS expression CLOSE_PARENS
3929 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
3934 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
3936 Block assign_block = new Block (current_block);
3937 current_block = assign_block;
3939 oob_stack.Push (lexer.Location);
3941 if ($3 is DictionaryEntry){
3942 DictionaryEntry de = (DictionaryEntry) $3;
3943 Location l = lexer.Location;
3945 Expression type = (Expression) de.Key;
3946 ArrayList var_declarators = (ArrayList) de.Value;
3948 ArrayList vars = new ArrayList (4);
3950 foreach (VariableDeclaration decl in var_declarators){
3952 LocalInfo vi = current_block.AddVariable (
3953 type, decl.identifier,
3954 current_local_parameters, decl.Location);
3960 if (decl.expression_or_array_initializer is Expression){
3961 expr = (Expression) decl.expression_or_array_initializer;
3963 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3965 // You must provide an initializer in a fixed or using statement declaration
3966 Report.Error_T (210, l);
3969 expr = new ArrayCreation (type, "", init, decl.Location);
3972 LocalVariableReference var;
3974 // Get a writable reference to this read-only variable.
3975 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
3977 // This is so that it is not a warning on using variables
3980 vars.Add (new DictionaryEntry (var, expr));
3982 // Assign a = new Assign (var, expr, decl.Location);
3983 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3985 $3 = new DictionaryEntry (type, vars);
3990 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
3991 current_block.AddStatement (u);
3992 while (current_block.Implicit)
3993 current_block = current_block.Parent;
3995 current_block = current_block.Parent;
3999 resource_acquisition
4000 : local_variable_declaration
4007 // A class used to pass around variable declarations and constants
4009 public class VariableDeclaration {
4010 public string identifier;
4011 public object expression_or_array_initializer;
4012 public Location Location;
4013 public Attributes OptAttributes;
4015 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4017 this.identifier = id;
4018 this.expression_or_array_initializer = eoai;
4020 this.OptAttributes = opt_attrs;
4023 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4029 /// Used to pass around interface property information
4031 public class InterfaceAccessorInfo {
4033 public readonly Accessor Get, Set;
4035 public InterfaceAccessorInfo (bool has_get, bool has_set,
4036 Attributes get_attrs, Attributes set_attrs)
4039 Get = new Accessor (null, get_attrs);
4041 Set = new Accessor (null, set_attrs);
4047 // A class used to hold info about an indexer declarator
4049 public class IndexerDeclaration {
4050 public Expression type;
4051 public string interface_type;
4052 public Parameters param_list;
4054 public IndexerDeclaration (Expression type, string interface_type, Parameters param_list)
4057 this.interface_type = interface_type;
4058 this.param_list = param_list;
4063 // We use this when we do not have an object in advance that is an IIteratorContainer
4065 public class SimpleIteratorContainer : IIteratorContainer {
4068 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4073 public static SimpleIteratorContainer GetSimple () {
4074 Simple.Yields = false;
4078 public void SetYields () { Yields = true; }
4082 // A class used to hold info about an operator declarator
4084 public class OperatorDeclaration {
4085 public Operator.OpType optype;
4086 public Expression ret_type, arg1type, arg2type;
4087 public string arg1name, arg2name;
4088 public Location location;
4090 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4091 Expression arg1type, string arg1name,
4092 Expression arg2type, string arg2name, Location location)
4095 this.ret_type = ret_type;
4096 this.arg1type = arg1type;
4097 this.arg1name = arg1name;
4098 this.arg2type = arg2type;
4099 this.arg2name = arg2name;
4100 this.location = location;
4105 void Error_ExpectingTypeName (Location l, Expression expr)
4107 if (expr is Invocation){
4108 Report.Error (1002, l, "; expected");
4110 Report.Error (-1, l, "Invalid Type definition");
4115 // Given the @class_name name, it creates a fully qualified name
4116 // based on the containing declaration space
4119 MakeName (string class_name)
4121 string ns = current_namespace.FullName;
4122 string container_name = current_container.Name;
4124 if (container_name == ""){
4126 return ns + "." + class_name;
4130 return container_name + "." + class_name;
4134 // Used to report back to the user the result of a declaration
4135 // in the current declaration space
4138 CheckDef (DeclSpace.AdditionResult result, string name, Location l)
4140 current_container.CheckDef (result, name, l);
4144 CheckDef (bool result, string name, Location l)
4148 CheckDef (DeclSpace.AdditionResult.NameExists, name, l);
4151 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4153 Block implicit_block;
4154 ArrayList inits = null;
4157 // We use the `Used' property to check whether statements
4158 // have been added to the current block. If so, we need
4159 // to create another block to contain the new declaration
4160 // otherwise, as an optimization, we use the same block to
4161 // add the declaration.
4163 // FIXME: A further optimization is to check if the statements
4164 // that were added were added as part of the initialization
4165 // below. In which case, no other statements have been executed
4166 // and we might be able to reduce the number of blocks for
4167 // situations like this:
4169 // int j = 1; int k = j + 1;
4171 if (current_block.Used)
4172 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4174 implicit_block = current_block;
4176 foreach (VariableDeclaration decl in variable_declarators){
4178 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4179 if (decl.expression_or_array_initializer != null){
4181 inits = new ArrayList (4);
4188 return implicit_block;
4190 foreach (VariableDeclaration decl in inits){
4194 if (decl.expression_or_array_initializer is Expression){
4195 expr = (Expression) decl.expression_or_array_initializer;
4198 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4200 expr = new ArrayCreation (type, "", init, decl.Location);
4203 LocalVariableReference var;
4204 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4206 assign = new Assign (var, expr, decl.Location);
4208 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4211 return implicit_block;
4214 Block declare_local_constants (Expression type, ArrayList declarators)
4216 Block implicit_block;
4218 if (current_block.Used)
4219 implicit_block = new Block (current_block, Block.Flags.Implicit);
4221 implicit_block = current_block;
4223 foreach (VariableDeclaration decl in declarators){
4224 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4225 current_local_parameters, decl.Location);
4228 return implicit_block;
4231 void CheckAttributeTarget (string a)
4235 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4239 Location l = lexer.Location;
4240 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4246 void CheckUnaryOperator (Operator.OpType op)
4250 case Operator.OpType.LogicalNot:
4251 case Operator.OpType.OnesComplement:
4252 case Operator.OpType.Increment:
4253 case Operator.OpType.Decrement:
4254 case Operator.OpType.True:
4255 case Operator.OpType.False:
4256 case Operator.OpType.Addition:
4257 case Operator.OpType.Subtraction:
4262 Location l = lexer.Location;
4263 Report.Error (1019, l, "Overloadable unary operator expected");
4269 void CheckBinaryOperator (Operator.OpType op)
4273 case Operator.OpType.Addition:
4274 case Operator.OpType.Subtraction:
4275 case Operator.OpType.Multiply:
4276 case Operator.OpType.Division:
4277 case Operator.OpType.Modulus:
4278 case Operator.OpType.BitwiseAnd:
4279 case Operator.OpType.BitwiseOr:
4280 case Operator.OpType.ExclusiveOr:
4281 case Operator.OpType.LeftShift:
4282 case Operator.OpType.RightShift:
4283 case Operator.OpType.Equality:
4284 case Operator.OpType.Inequality:
4285 case Operator.OpType.GreaterThan:
4286 case Operator.OpType.LessThan:
4287 case Operator.OpType.GreaterThanOrEqual:
4288 case Operator.OpType.LessThanOrEqual:
4292 Location l = lexer.Location;
4293 Report.Error (1020, l, "Overloadable binary operator expected");
4299 void syntax_error (Location l, string msg)
4301 Report.Error (1003, l, "Syntax error, " + msg);
4304 void output (string s)
4306 Console.WriteLine (s);
4309 void note (string s)
4311 // Used to put annotations
4316 public Tokenizer Lexer {
4322 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4324 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4325 this.name = file.Name;
4327 current_container = RootContext.Tree.Types;
4328 current_container.NamespaceEntry = current_namespace;
4329 oob_stack = new Stack ();
4330 switch_stack = new Stack ();
4332 lexer = new Tokenizer (reader, file, defines);
4335 public override void parse ()
4338 if (yacc_verbose_flag)
4339 yyparse (lexer, new yydebug.yyDebugSimple ());
4342 Tokenizer tokenizer = lexer as Tokenizer;
4343 tokenizer.cleanup ();
4344 } catch (Exception e){
4345 // Please do not remove this, it is used during debugging
4348 Console.WriteLine (e);
4349 Report.Error (-25, lexer.Location, "Parsing error");
4350 if (Driver.parser_verbose)
4351 Console.WriteLine (e);
4355 void CheckToken (int error, int yyToken, string msg)
4357 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4358 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4361 Report.Error (error, lexer.Location, msg);
4364 void CheckIdentifierToken (int yyToken)
4366 CheckToken (1041, yyToken, "Identifier expected");