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 (134, lexer.Location, "Cannot use qualified namespace names in nested namespace declarations");
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 current_container.AddConstant (c);
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 (145, lexer.Location, "A const field requires a value to be provided");
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 current_container.AddField (field);
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 current_container.AddMethod (method);
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);
1036 | parameter_array COMMA fixed_parameters
1038 Report.Error (231, lexer.Location, "A params parameter must be the last parameter in a formal parameter list");
1041 | ARGLIST COMMA fixed_parameters
1043 Report.Error (257, lexer.Location, "An __arglist parameter must be the last parameter in a formal parameter list");
1048 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
1052 $$ = new Parameters (null, true, lexer.Location);
1059 ArrayList pars = new ArrayList (4);
1064 | fixed_parameters COMMA fixed_parameter
1066 ArrayList pars = (ArrayList) $1;
1075 opt_parameter_modifier
1079 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
1082 opt_parameter_modifier
1085 CheckIdentifierToken (yyToken);
1090 opt_parameter_modifier
1091 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1092 | parameter_modifier
1096 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1097 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1101 : opt_attributes PARAMS type IDENTIFIER
1103 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1104 note ("type must be a single-dimension array type");
1106 | opt_attributes PARAMS type error {
1107 CheckIdentifierToken (yyToken);
1113 : namespace_or_type_name { $$ = ($1).ToString (); }
1116 property_declaration
1122 implicit_value_parameter_type = (Expression) $3;
1124 lexer.PropertyParsing = true;
1126 $$ = lexer.Location;
1128 iterator_container = SimpleIteratorContainer.GetSimple ();
1130 accessor_declarations
1132 lexer.PropertyParsing = false;
1137 Pair pair = (Pair) $7;
1138 Accessor get_block = (Accessor) pair.First;
1139 Accessor set_block = (Accessor) pair.Second;
1141 Location loc = (Location) $6;
1142 prop = new Property (current_class, (Expression) $3, (int) $2, false,
1143 (string) $4, (Attributes) $1, get_block, set_block, loc);
1144 if (SimpleIteratorContainer.Simple.Yields)
1147 current_container.AddProperty (prop);
1148 implicit_value_parameter_type = null;
1149 iterator_container = null;
1153 accessor_declarations
1154 : get_accessor_declaration opt_set_accessor_declaration
1156 $$ = new Pair ($1, $2);
1158 | set_accessor_declaration opt_get_accessor_declaration
1160 $$ = new Pair ($2, $1);
1164 opt_get_accessor_declaration
1165 : /* empty */ { $$ = null; }
1166 | get_accessor_declaration
1169 opt_set_accessor_declaration
1170 : /* empty */ { $$ = null; }
1171 | set_accessor_declaration
1174 get_accessor_declaration
1175 : opt_attributes GET
1177 // If this is not the case, then current_local_parameters has already
1178 // been set in indexer_declaration
1179 if (parsing_indexer == false)
1180 current_local_parameters = null;
1182 current_local_parameters = indexer_parameters;
1183 lexer.PropertyParsing = false;
1187 $$ = new Accessor ((Block) $4, (Attributes) $1, lexer.Location);
1188 current_local_parameters = null;
1189 lexer.PropertyParsing = true;
1193 set_accessor_declaration
1194 : opt_attributes SET
1197 Parameter implicit_value_parameter = new Parameter (
1198 implicit_value_parameter_type, "value",
1199 Parameter.Modifier.NONE, null);
1201 if (parsing_indexer == false) {
1202 args = new Parameter [1];
1203 args [0] = implicit_value_parameter;
1204 current_local_parameters = new Parameters (args, null, lexer.Location);
1206 Parameter [] fpars = indexer_parameters.FixedParameters;
1209 int count = fpars.Length;
1211 args = new Parameter [count + 1];
1212 fpars.CopyTo (args, 0);
1213 args [count] = implicit_value_parameter;
1216 current_local_parameters = new Parameters (
1217 args, indexer_parameters.ArrayParameter, lexer.Location);
1220 lexer.PropertyParsing = false;
1224 $$ = new Accessor ((Block) $4, (Attributes) $1, lexer.Location);
1225 current_local_parameters = null;
1226 lexer.PropertyParsing = true;
1232 | SEMICOLON { $$ = null; }
1235 interface_declaration
1239 INTERFACE IDENTIFIER
1241 string name = MakeName ((string) $5);
1242 bool partial = (bool) $3;
1245 ClassPart part = PartialContainer.CreatePart (
1246 current_namespace, current_container, name, (int) $2,
1247 (Attributes) $1, Kind.Interface, lexer.Location);
1249 current_container = part.PartialContainer;
1250 current_class = part;
1252 current_class = new Interface (
1253 current_namespace, current_container, name, (int) $2,
1254 (Attributes) $1, lexer.Location);
1256 current_container = current_class;
1257 RootContext.Tree.RecordDecl (name, current_class);
1262 current_class.Bases = (ArrayList) $7;
1264 current_class.Register ();
1271 current_container = current_container.Parent;
1272 current_class = current_container;
1274 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1275 CheckIdentifierToken (yyToken);
1281 opt_interface_member_declarations
1285 opt_interface_member_declarations
1287 | interface_member_declarations
1290 interface_member_declarations
1291 : interface_member_declaration
1292 | interface_member_declarations interface_member_declaration
1295 interface_member_declaration
1296 : interface_method_declaration
1298 Method m = (Method) $1;
1300 current_container.AddMethod (m);
1302 | interface_property_declaration
1304 Property p = (Property) $1;
1306 current_container.AddProperty (p);
1308 | interface_event_declaration
1311 Event e = (Event) $1;
1312 current_container.AddEvent (e);
1315 | interface_indexer_declaration
1317 Indexer i = (Indexer) $1;
1319 current_container.AddIndexer (i);
1327 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, lexer.Location);
1332 interface_method_declaration
1333 : opt_attributes opt_new type IDENTIFIER
1334 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1337 $$ = new Method (current_class, (Expression) $3, (int) $2, true,
1338 (string) $4, (Parameters) $6, (Attributes) $1,
1341 | opt_attributes opt_new VOID IDENTIFIER
1342 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1345 $$ = new Method (current_class, TypeManager.system_void_expr, (int) $2,
1346 true, (string) $4, (Parameters) $6, (Attributes) $1,
1351 interface_property_declaration
1356 { lexer.PropertyParsing = true; }
1358 { lexer.PropertyParsing = false; }
1361 InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
1363 $$ = new Property (current_class, (Expression) $3, (int) $2, true,
1364 (string) $4, (Attributes) $1, pinfo.Get, pinfo.Set,
1370 CheckIdentifierToken (yyToken);
1376 : opt_attributes GET SEMICOLON { $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, lexer.Location, lexer.Location); }
1377 | opt_attributes SET SEMICOLON { $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1, lexer.Location, lexer.Location); }
1378 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1379 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $3, lexer.Location, lexer.Location); }
1380 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1381 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $3, (Attributes) $1, lexer.Location, lexer.Location); }
1384 interface_event_declaration
1385 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1387 $$ = new EventField (current_class, (Expression) $4, (int) $2, true, (string) $5, null,
1388 (Attributes) $1, lexer.Location);
1390 | opt_attributes opt_new EVENT type error {
1391 CheckIdentifierToken (yyToken);
1394 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1395 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1398 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE event_accessor_declarations CLOSE_BRACE {
1399 Report.Error (69, lexer.Location, "Event accessors not valid on interfaces");
1404 interface_indexer_declaration
1405 : opt_attributes opt_new type THIS
1406 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1408 { lexer.PropertyParsing = true; }
1410 { lexer.PropertyParsing = false; }
1413 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1415 $$ = new Indexer (current_class, (Expression) $3, TypeContainer.DefaultIndexerName, (int) $2, true,
1416 (Parameters) $6, (Attributes) $1, info.Get, info.Set,
1421 operator_declaration
1422 : opt_attributes opt_modifiers operator_declarator
1424 iterator_container = SimpleIteratorContainer.GetSimple ();
1428 OperatorDeclaration decl = (OperatorDeclaration) $3;
1430 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1432 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null);
1433 if (decl.arg2type != null)
1434 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null);
1436 Operator op = new Operator (
1437 current_class, decl.optype, decl.ret_type, (int) $2,
1438 new Parameters (param_list, null, decl.location),
1439 (Block) $5, (Attributes) $1, decl.location);
1441 if (SimpleIteratorContainer.Simple.Yields)
1444 // Note again, checking is done in semantic analysis
1445 current_container.AddOperator (op);
1447 current_local_parameters = null;
1448 iterator_container = null;
1454 | SEMICOLON { $$ = null; }
1457 : type OPERATOR overloadable_operator
1458 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1460 Operator.OpType op = (Operator.OpType) $3;
1461 CheckUnaryOperator (op);
1463 if (op == Operator.OpType.Addition)
1464 op = Operator.OpType.UnaryPlus;
1466 if (op == Operator.OpType.Subtraction)
1467 op = Operator.OpType.UnaryNegation;
1469 Parameter [] pars = new Parameter [1];
1471 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1473 current_local_parameters = new Parameters (pars, null, lexer.Location);
1475 $$ = new OperatorDeclaration (op, (Expression) $1, (Expression) $5, (string) $6,
1476 null, null, lexer.Location);
1478 | type OPERATOR overloadable_operator
1480 type IDENTIFIER COMMA
1484 CheckBinaryOperator ((Operator.OpType) $3);
1486 Parameter [] pars = new Parameter [2];
1488 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1489 pars [1] = new Parameter ((Expression) $8, (string) $9, Parameter.Modifier.NONE, null);
1491 current_local_parameters = new Parameters (pars, null, lexer.Location);
1493 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1494 (Expression) $5, (string) $6,
1495 (Expression) $8, (string) $9, lexer.Location);
1497 | conversion_operator_declarator
1500 overloadable_operator
1502 : BANG { $$ = Operator.OpType.LogicalNot; }
1503 | TILDE { $$ = Operator.OpType.OnesComplement; }
1504 | OP_INC { $$ = Operator.OpType.Increment; }
1505 | OP_DEC { $$ = Operator.OpType.Decrement; }
1506 | TRUE { $$ = Operator.OpType.True; }
1507 | FALSE { $$ = Operator.OpType.False; }
1508 // Unary and binary:
1509 | PLUS { $$ = Operator.OpType.Addition; }
1510 | MINUS { $$ = Operator.OpType.Subtraction; }
1512 | STAR { $$ = Operator.OpType.Multiply; }
1513 | DIV { $$ = Operator.OpType.Division; }
1514 | PERCENT { $$ = Operator.OpType.Modulus; }
1515 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1516 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1517 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1518 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1519 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1520 | OP_EQ { $$ = Operator.OpType.Equality; }
1521 | OP_NE { $$ = Operator.OpType.Inequality; }
1522 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1523 | OP_LT { $$ = Operator.OpType.LessThan; }
1524 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1525 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1528 conversion_operator_declarator
1529 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1531 Parameter [] pars = new Parameter [1];
1533 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1535 current_local_parameters = new Parameters (pars, null, lexer.Location);
1537 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1538 null, null, lexer.Location);
1540 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1542 Parameter [] pars = new Parameter [1];
1544 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1546 current_local_parameters = new Parameters (pars, null, lexer.Location);
1548 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1549 null, null, lexer.Location);
1553 syntax_error (lexer.Location, "'operator' expected");
1557 syntax_error (lexer.Location, "'operator' expected");
1561 constructor_declaration
1564 constructor_declarator
1567 Constructor c = (Constructor) $3;
1568 c.Block = (Block) $4;
1569 c.OptAttributes = (Attributes) $1;
1570 c.ModFlags = (int) $2;
1572 if (c.Name == current_container.Basename){
1573 if ((c.ModFlags & Modifiers.STATIC) != 0){
1574 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1576 515, c.Location, String.Format (
1577 "`{0}.{1}': static constructor can not have access modifiers",
1578 c.Name, current_container.Name));
1581 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1583 if (c.Initializer != null){
1586 "Static constructors can not have an explicit this or base " +
1587 "constructor invocations");
1590 if (!c.Parameters.Empty){
1592 132, c.Location, "Static constructors should not have parameters");
1595 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1598 // We let another layer check the validity of the constructor.
1599 Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1602 current_container.AddConstructor (c);
1604 current_local_parameters = null;
1608 constructor_declarator
1610 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1612 oob_stack.Push (lexer.Location);
1614 current_local_parameters = (Parameters) $3;
1616 opt_constructor_initializer
1618 Location l = (Location) oob_stack.Pop ();
1619 $$ = new Constructor (current_class, (string) $1, 0, (Parameters) $3,
1620 (ConstructorInitializer) $6, l);
1626 | SEMICOLON { $$ = null; }
1629 opt_constructor_initializer
1630 : /* empty */ { $$ = null; }
1631 | constructor_initializer
1634 constructor_initializer
1635 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1637 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1639 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1641 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1644 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1650 : /* EMPTY */ { $$ = 0; }
1651 | UNSAFE { $$ = Modifiers.UNSAFE; }
1652 | EXTERN { $$ = Modifiers.EXTERN; }
1655 destructor_declaration
1656 : opt_attributes opt_finalizer TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1658 if ((string) $4 != current_container.Basename){
1659 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1660 } else if (!(current_container is Class)){
1661 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1663 Location l = lexer.Location;
1666 if (!RootContext.StdLib && current_container.Name == "System.Object")
1667 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
1669 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
1671 if ((m & Modifiers.UNSAFE) != 0){
1672 if (!RootContext.Unsafe){
1673 Report.Error (227, l,
1674 "Unsafe code requires the -unsafe command " +
1675 "line option to be specified");
1679 Method d = new Destructor (
1680 current_class, TypeManager.system_void_expr, m, "Finalize",
1681 new Parameters (null, null, l), (Attributes) $1, l);
1683 d.Block = (Block) $7;
1684 current_container.AddMethod (d);
1692 EVENT type variable_declarators SEMICOLON
1694 foreach (VariableDeclaration var in (ArrayList) $5) {
1696 Event e = new EventField (current_class, (Expression) $4, (int) $2, false, var.identifier,
1697 var.expression_or_array_initializer,
1698 (Attributes) $1, lexer.Location);
1700 current_container.AddEvent (e);
1706 EVENT type member_name
1709 implicit_value_parameter_type = (Expression) $4;
1710 lexer.EventParsing = true;
1711 oob_stack.Push (lexer.Location);
1713 event_accessor_declarations
1715 lexer.EventParsing = false;
1719 Location loc = (Location) oob_stack.Pop ();
1722 Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
1725 Pair pair = (Pair) $8;
1727 Event e = new EventProperty (current_class, (Expression) $4, (int) $2, false, (string) $5,
1728 null, (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
1731 current_container.AddEvent (e);
1732 implicit_value_parameter_type = null;
1735 | opt_attributes opt_modifiers EVENT type member_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS block {
1736 string mn = (string) $5;
1738 if (mn.IndexOf ('.') != -1)
1739 Report.Error (71, lexer.Location, "Explicit implementation of events requires property syntax");
1741 Report.Error (71, lexer.Location, "Event declaration should use property syntax");
1745 event_accessor_declarations
1746 : add_accessor_declaration remove_accessor_declaration
1748 $$ = new Pair ($1, $2);
1750 | remove_accessor_declaration add_accessor_declaration
1752 $$ = new Pair ($2, $1);
1754 | add_accessor_declaration { $$ = null; }
1755 | remove_accessor_declaration { $$ = null; }
1758 add_accessor_declaration
1759 : opt_attributes ADD
1761 Parameter [] args = new Parameter [1];
1762 Parameter implicit_value_parameter = new Parameter (
1763 implicit_value_parameter_type, "value",
1764 Parameter.Modifier.NONE, null);
1766 args [0] = implicit_value_parameter;
1768 current_local_parameters = new Parameters (args, null, lexer.Location);
1769 lexer.EventParsing = false;
1773 $$ = new Accessor ((Block) $4, (Attributes) $1, lexer.Location);
1774 lexer.EventParsing = true;
1776 | opt_attributes ADD error {
1777 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1782 remove_accessor_declaration
1783 : opt_attributes REMOVE
1785 Parameter [] args = new Parameter [1];
1786 Parameter implicit_value_parameter = new Parameter (
1787 implicit_value_parameter_type, "value",
1788 Parameter.Modifier.NONE, null);
1790 args [0] = implicit_value_parameter;
1792 current_local_parameters = new Parameters (args, null, lexer.Location);
1793 lexer.EventParsing = false;
1797 $$ = new Accessor ((Block) $4, (Attributes) $1, lexer.Location);
1798 lexer.EventParsing = true;
1800 | opt_attributes REMOVE error {
1801 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1807 : opt_attributes opt_modifiers indexer_declarator
1810 IndexerDeclaration decl = (IndexerDeclaration) $3;
1812 implicit_value_parameter_type = decl.type;
1814 lexer.PropertyParsing = true;
1815 parsing_indexer = true;
1817 indexer_parameters = decl.param_list;
1818 oob_stack.Push (lexer.Location);
1820 accessor_declarations
1822 lexer.PropertyParsing = false;
1823 parsing_indexer = false;
1827 // The signature is computed from the signature of the indexer. Look
1828 // at section 3.6 on the spec
1829 Location loc = (Location) oob_stack.Pop ();
1831 IndexerDeclaration decl = (IndexerDeclaration) $3;
1832 Pair pair = (Pair) $6;
1833 Accessor get_block = (Accessor) pair.First;
1834 Accessor set_block = (Accessor) pair.Second;
1835 string name = decl.interface_type;
1838 name += TypeContainer.DefaultIndexerName;
1840 indexer = new Indexer (current_class, decl.type, name,
1841 (int) $2, false, decl.param_list, (Attributes) $1,
1842 get_block, set_block, loc);
1844 // Note that there is no equivalent of CheckDef for this case
1845 // We shall handle this in semantic analysis
1847 current_container.AddIndexer (indexer);
1849 current_local_parameters = null;
1850 implicit_value_parameter_type = null;
1851 indexer_parameters = null;
1856 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1858 Parameters pars = (Parameters) $4;
1859 if (pars.HasArglist) {
1860 // "__arglist is not valid in this context"
1861 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
1862 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
1863 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1866 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
1868 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1870 Parameters pars = (Parameters) $6;
1872 if (pars.HasArglist) {
1873 // "__arglist is not valid in this context"
1874 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
1875 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
1876 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1878 $$ = new IndexerDeclaration ((Expression) $1, $2.ToString (), pars);
1890 Location enum_location = lexer.Location;
1892 string full_name = MakeName ((string) $4);
1893 Enum e = new Enum (current_namespace, current_container, (Expression) $5, (int) $2,
1894 full_name, (Attributes) $1, enum_location);
1896 foreach (VariableDeclaration ev in (ArrayList) $6) {
1897 e.AddEnumMember (ev.identifier,
1898 (Expression) ev.expression_or_array_initializer,
1899 ev.Location, ev.OptAttributes);
1902 current_container.AddEnum (e);
1903 RootContext.Tree.RecordDecl (full_name, e);
1909 : /* empty */ { $$ = TypeManager.system_int32_expr; }
1910 | COLON type { $$ = $2; }
1914 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1920 opt_enum_member_declarations
1921 : /* empty */ { $$ = new ArrayList (4); }
1922 | enum_member_declarations opt_comma { $$ = $1; }
1925 enum_member_declarations
1926 : enum_member_declaration
1928 ArrayList l = new ArrayList (4);
1933 | enum_member_declarations COMMA enum_member_declaration
1935 ArrayList l = (ArrayList) $1;
1943 enum_member_declaration
1944 : opt_attributes IDENTIFIER
1946 $$ = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
1948 | opt_attributes IDENTIFIER
1950 $$ = lexer.Location;
1954 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
1958 delegate_declaration
1962 IDENTIFIER OPEN_PARENS
1963 opt_formal_parameter_list
1967 Location l = lexer.Location;
1968 string name = MakeName ((string) $5);
1969 Delegate del = new Delegate (current_namespace, current_container, (Expression) $4,
1970 (int) $2, name, (Parameters) $7, (Attributes) $1, l);
1972 current_container.AddDelegate (del);
1973 RootContext.Tree.RecordDecl (name, del);
1978 IDENTIFIER OPEN_PARENS
1979 opt_formal_parameter_list
1983 Location l = lexer.Location;
1984 string name = MakeName ((string) $5);
1985 Delegate del = new Delegate (
1986 current_namespace, current_container,
1987 TypeManager.system_void_expr, (int) $2, name,
1988 (Parameters) $7, (Attributes) $1, l);
1990 current_container.AddDelegate (del);
1991 RootContext.Tree.RecordDecl (name, del);
1996 : namespace_or_type_name
1999 namespace_or_type_name
2000 : IDENTIFIER { $$ = new SimpleName ((string) $1, lexer.Location);}
2001 | namespace_or_type_name DOT IDENTIFIER {
2002 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2007 * Before you think of adding a return_type, notice that we have been
2008 * using two rules in the places where it matters (one rule using type
2009 * and another identical one that uses VOID as the return type). This
2010 * gets rid of a shift/reduce couple
2013 : type_name { /* class_type */
2015 This does interfaces, delegates, struct_types, class_types,
2016 parent classes, and more! 4.2
2029 // Note that here only unmanaged types are allowed but we
2030 // can't perform checks during this phase - we do it during
2031 // semantic analysis.
2033 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2037 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2043 | non_expression_type rank_specifier
2045 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2047 | non_expression_type STAR
2049 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2051 | expression rank_specifiers
2053 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2057 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2061 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2062 // through this different path
2064 | multiplicative_expression STAR
2066 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2073 ArrayList types = new ArrayList (4);
2078 | type_list COMMA type
2080 ArrayList types = (ArrayList) $1;
2088 * replaces all the productions for isolating the various
2089 * simple types, but we need this to reuse it easily in local_variable_type
2092 : OBJECT { $$ = TypeManager.system_object_expr; }
2093 | STRING { $$ = TypeManager.system_string_expr; }
2094 | BOOL { $$ = TypeManager.system_boolean_expr; }
2095 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2096 | FLOAT { $$ = TypeManager.system_single_expr; }
2097 | DOUBLE { $$ = TypeManager.system_double_expr; }
2102 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2103 | BYTE { $$ = TypeManager.system_byte_expr; }
2104 | SHORT { $$ = TypeManager.system_int16_expr; }
2105 | USHORT { $$ = TypeManager.system_uint16_expr; }
2106 | INT { $$ = TypeManager.system_int32_expr; }
2107 | UINT { $$ = TypeManager.system_uint32_expr; }
2108 | LONG { $$ = TypeManager.system_int64_expr; }
2109 | ULONG { $$ = TypeManager.system_uint64_expr; }
2110 | CHAR { $$ = TypeManager.system_char_expr; }
2111 | VOID { $$ = TypeManager.system_void_expr; }
2119 : type rank_specifiers
2121 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2126 // Expressions, section 7.5
2134 | IDENTIFIER { $$ = new SimpleName ((string) $1, lexer.Location); }
2135 | parenthesized_expression
2137 | invocation_expression
2141 | post_increment_expression
2142 | post_decrement_expression
2146 | checked_expression
2147 | unchecked_expression
2148 | pointer_member_access
2149 | anonymous_method_expression
2156 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2157 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2158 | NULL { $$ = NullLiteral.Null; }
2162 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2163 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2164 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2169 object v = lexer.Value;
2175 $$ = IntLiteral.Zero;
2177 $$ = IntLiteral.One;
2179 $$ = new IntLiteral (i);
2180 } else if (v is uint)
2181 $$ = new UIntLiteral ((UInt32) v);
2183 $$ = new LongLiteral ((Int64) v);
2184 else if (v is ulong)
2185 $$ = new ULongLiteral ((UInt64) v);
2187 Console.WriteLine ("OOPS. Unexpected result from scanner");
2192 : TRUE { $$ = new BoolLiteral (true); }
2193 | FALSE { $$ = new BoolLiteral (false); }
2196 parenthesized_expression_0
2197 : OPEN_PARENS expression CLOSE_PARENS
2200 lexer.Deambiguate_CloseParens ();
2201 // After this, the next token returned is one of
2202 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2203 // or CLOSE_PARENS_MINUS.
2207 parenthesized_expression
2208 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2212 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2214 // If a parenthesized expression is followed by a minus, we need to wrap
2215 // the expression inside a ParenthesizedExpression for the CS0075 check
2216 // in Binary.DoResolve().
2217 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2222 : primary_expression DOT IDENTIFIER
2224 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2226 | predefined_type DOT IDENTIFIER
2228 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2236 invocation_expression
2237 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2240 Location l = lexer.Location;
2241 Report.Error (1, l, "Parse error");
2243 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2245 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2247 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2249 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2251 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2256 : /* empty */ { $$ = null; }
2263 ArrayList list = new ArrayList (4);
2267 | argument_list COMMA argument
2269 ArrayList list = (ArrayList) $1;
2273 | argument_list error {
2274 CheckToken (1026, yyToken, ", or ) expected");
2281 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2283 | REF variable_reference
2285 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2287 | OUT variable_reference
2289 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2291 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2293 ArrayList list = (ArrayList) $3;
2294 Argument[] args = new Argument [list.Count];
2295 list.CopyTo (args, 0);
2297 Expression expr = new Arglist (args, lexer.Location);
2298 $$ = new Argument (expr, Argument.AType.Expression);
2302 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2307 : expression { note ("section 5.4"); $$ = $1; }
2311 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2313 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2315 | primary_expression rank_specifiers
2317 // So the super-trick is that primary_expression
2318 // can only be either a SimpleName or a MemberAccess.
2319 // The MemberAccess case arises when you have a fully qualified type-name like :
2321 // SimpleName is when you have
2324 Expression expr = (Expression) $1;
2325 if (expr is ComposedCast){
2326 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2327 } else if (!(expr is SimpleName || expr is MemberAccess)){
2328 Error_ExpectingTypeName (lexer.Location, expr);
2329 $$ = TypeManager.system_object_expr;
2332 // So we extract the string corresponding to the SimpleName
2335 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2343 ArrayList list = new ArrayList (4);
2347 | expression_list COMMA expression
2349 ArrayList list = (ArrayList) $1;
2358 $$ = new This (current_block, lexer.Location);
2363 : BASE DOT IDENTIFIER
2365 $$ = new BaseAccess ((string) $3, lexer.Location);
2367 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2369 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2372 Report.Error (175, "Use of keyword `base' is not valid in this context");
2377 post_increment_expression
2378 : primary_expression OP_INC
2380 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2381 (Expression) $1, lexer.Location);
2385 post_decrement_expression
2386 : primary_expression OP_DEC
2388 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2389 (Expression) $1, lexer.Location);
2394 : object_or_delegate_creation_expression
2395 | array_creation_expression
2398 object_or_delegate_creation_expression
2399 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2401 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2405 array_creation_expression
2406 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2408 opt_array_initializer
2410 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2412 | NEW type rank_specifiers array_initializer
2414 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2418 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2434 : rank_specifier opt_rank_specifier
2436 $$ = (string) $2 + (string) $1;
2441 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2443 $$ = "[" + (string) $2 + "]";
2463 | dim_separators COMMA
2465 $$ = (string) $1 + ",";
2469 opt_array_initializer
2481 : OPEN_BRACE CLOSE_BRACE
2483 ArrayList list = new ArrayList (4);
2486 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2488 $$ = (ArrayList) $2;
2492 variable_initializer_list
2493 : variable_initializer
2495 ArrayList list = new ArrayList (4);
2499 | variable_initializer_list COMMA variable_initializer
2501 ArrayList list = (ArrayList) $1;
2508 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2510 $$ = new TypeOfVoid (lexer.Location);
2512 | TYPEOF OPEN_PARENS type CLOSE_PARENS
2514 $$ = new TypeOf ((Expression) $3, lexer.Location);
2519 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2520 $$ = new SizeOf ((Expression) $3, lexer.Location);
2525 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2527 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2531 unchecked_expression
2532 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2534 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2538 pointer_member_access
2539 : primary_expression OP_PTR IDENTIFIER
2543 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2544 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2548 anonymous_method_expression
2549 : DELEGATE opt_anonymous_method_signature {
2550 oob_stack.Push (current_local_parameters);
2551 current_local_parameters = (Parameters)$2;
2552 create_toplevel_block = true;
2555 Report.Error (-213, lexer.Location, "Anonymous methods are not supported in this branch");
2558 create_toplevel_block = false;
2559 if (RootContext.Version == LanguageVersion.ISO_1){
2560 Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
2563 $$ = new AnonymousMethod ((Parameters) $2, (Block) $4, lexer.Location);
2564 current_local_parameters = (Parameters) oob_stack.Pop ();
2569 opt_anonymous_method_signature
2570 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
2571 | anonymous_method_signature
2574 anonymous_method_signature
2575 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
2578 $$ = Parameters.EmptyReadOnlyParameters;
2580 ArrayList par_list = (ArrayList) $2;
2581 Parameter [] pars = new Parameter [par_list.Count];
2582 par_list.CopyTo (pars);
2583 $$ = new Parameters (pars, null, lexer.Location);
2588 opt_anonymous_method_parameter_list
2589 : /* empty */ { $$ = null; }
2590 | anonymous_method_parameter_list { $$ = $1; }
2593 anonymous_method_parameter_list
2594 : anonymous_method_parameter
2596 ArrayList a = new ArrayList (4);
2600 | anonymous_method_parameter_list COMMA anonymous_method_parameter
2602 ArrayList a = (ArrayList) $1;
2608 anonymous_method_parameter
2609 : opt_parameter_modifier type IDENTIFIER {
2610 $$ = new Parameter ((Expression) $2, (string) $2, (Parameter.Modifier) $1, null);
2615 : primary_expression
2616 | BANG prefixed_unary_expression
2618 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2620 | TILDE prefixed_unary_expression
2622 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2628 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
2630 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2632 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
2634 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2640 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2642 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2647 // The idea to split this out is from Rhys' grammar
2648 // to solve the problem with casts.
2650 prefixed_unary_expression
2652 | PLUS prefixed_unary_expression
2654 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2656 | MINUS prefixed_unary_expression
2658 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2660 | OP_INC prefixed_unary_expression
2662 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2663 (Expression) $2, lexer.Location);
2665 | OP_DEC prefixed_unary_expression
2667 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2668 (Expression) $2, lexer.Location);
2670 | STAR prefixed_unary_expression
2672 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2674 | BITWISE_AND prefixed_unary_expression
2676 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2680 pre_increment_expression
2681 : OP_INC prefixed_unary_expression
2683 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2684 (Expression) $2, lexer.Location);
2688 pre_decrement_expression
2689 : OP_DEC prefixed_unary_expression
2691 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2692 (Expression) $2, lexer.Location);
2696 multiplicative_expression
2697 : prefixed_unary_expression
2698 | multiplicative_expression STAR prefixed_unary_expression
2700 $$ = new Binary (Binary.Operator.Multiply,
2701 (Expression) $1, (Expression) $3, lexer.Location);
2703 | multiplicative_expression DIV prefixed_unary_expression
2705 $$ = new Binary (Binary.Operator.Division,
2706 (Expression) $1, (Expression) $3, lexer.Location);
2708 | multiplicative_expression PERCENT prefixed_unary_expression
2710 $$ = new Binary (Binary.Operator.Modulus,
2711 (Expression) $1, (Expression) $3, lexer.Location);
2716 : multiplicative_expression
2717 | additive_expression PLUS multiplicative_expression
2719 $$ = new Binary (Binary.Operator.Addition,
2720 (Expression) $1, (Expression) $3, lexer.Location);
2722 | additive_expression MINUS multiplicative_expression
2724 $$ = new Binary (Binary.Operator.Subtraction,
2725 (Expression) $1, (Expression) $3, lexer.Location);
2730 : additive_expression
2731 | shift_expression OP_SHIFT_LEFT additive_expression
2733 $$ = new Binary (Binary.Operator.LeftShift,
2734 (Expression) $1, (Expression) $3, lexer.Location);
2736 | shift_expression OP_SHIFT_RIGHT additive_expression
2738 $$ = new Binary (Binary.Operator.RightShift,
2739 (Expression) $1, (Expression) $3, lexer.Location);
2743 relational_expression
2745 | relational_expression OP_LT shift_expression
2747 $$ = new Binary (Binary.Operator.LessThan,
2748 (Expression) $1, (Expression) $3, lexer.Location);
2750 | relational_expression OP_GT shift_expression
2752 $$ = new Binary (Binary.Operator.GreaterThan,
2753 (Expression) $1, (Expression) $3, lexer.Location);
2755 | relational_expression OP_LE shift_expression
2757 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2758 (Expression) $1, (Expression) $3, lexer.Location);
2760 | relational_expression OP_GE shift_expression
2762 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2763 (Expression) $1, (Expression) $3, lexer.Location);
2765 | relational_expression IS type
2767 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
2769 | relational_expression AS type
2771 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
2776 : relational_expression
2777 | equality_expression OP_EQ relational_expression
2779 $$ = new Binary (Binary.Operator.Equality,
2780 (Expression) $1, (Expression) $3, lexer.Location);
2782 | equality_expression OP_NE relational_expression
2784 $$ = new Binary (Binary.Operator.Inequality,
2785 (Expression) $1, (Expression) $3, lexer.Location);
2790 : equality_expression
2791 | and_expression BITWISE_AND equality_expression
2793 $$ = new Binary (Binary.Operator.BitwiseAnd,
2794 (Expression) $1, (Expression) $3, lexer.Location);
2798 exclusive_or_expression
2800 | exclusive_or_expression CARRET and_expression
2802 $$ = new Binary (Binary.Operator.ExclusiveOr,
2803 (Expression) $1, (Expression) $3, lexer.Location);
2807 inclusive_or_expression
2808 : exclusive_or_expression
2809 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2811 $$ = new Binary (Binary.Operator.BitwiseOr,
2812 (Expression) $1, (Expression) $3, lexer.Location);
2816 conditional_and_expression
2817 : inclusive_or_expression
2818 | conditional_and_expression OP_AND inclusive_or_expression
2820 $$ = new Binary (Binary.Operator.LogicalAnd,
2821 (Expression) $1, (Expression) $3, lexer.Location);
2825 conditional_or_expression
2826 : conditional_and_expression
2827 | conditional_or_expression OP_OR conditional_and_expression
2829 $$ = new Binary (Binary.Operator.LogicalOr,
2830 (Expression) $1, (Expression) $3, lexer.Location);
2834 conditional_expression
2835 : conditional_or_expression
2836 | conditional_or_expression INTERR expression COLON expression
2838 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2842 assignment_expression
2843 : prefixed_unary_expression ASSIGN expression
2845 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2847 | prefixed_unary_expression OP_MULT_ASSIGN expression
2849 Location l = lexer.Location;
2851 $$ = new CompoundAssign (
2852 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
2854 | prefixed_unary_expression OP_DIV_ASSIGN expression
2856 Location l = lexer.Location;
2858 $$ = new CompoundAssign (
2859 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
2861 | prefixed_unary_expression OP_MOD_ASSIGN expression
2863 Location l = lexer.Location;
2865 $$ = new CompoundAssign (
2866 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
2868 | prefixed_unary_expression OP_ADD_ASSIGN expression
2870 Location l = lexer.Location;
2872 $$ = new CompoundAssign (
2873 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
2875 | prefixed_unary_expression OP_SUB_ASSIGN expression
2877 Location l = lexer.Location;
2879 $$ = new CompoundAssign (
2880 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
2882 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2884 Location l = lexer.Location;
2886 $$ = new CompoundAssign (
2887 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
2889 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2891 Location l = lexer.Location;
2893 $$ = new CompoundAssign (
2894 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
2896 | prefixed_unary_expression OP_AND_ASSIGN expression
2898 Location l = lexer.Location;
2900 $$ = new CompoundAssign (
2901 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
2903 | prefixed_unary_expression OP_OR_ASSIGN expression
2905 Location l = lexer.Location;
2907 $$ = new CompoundAssign (
2908 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
2910 | prefixed_unary_expression OP_XOR_ASSIGN expression
2912 Location l = lexer.Location;
2914 $$ = new CompoundAssign (
2915 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
2920 : conditional_expression
2921 | assignment_expression
2941 string name = MakeName ((string) $5);
2942 bool partial = (bool) $3;
2943 int mod_flags = (int) $2;
2946 ClassPart part = PartialContainer.CreatePart (
2947 current_namespace, current_container, name, mod_flags,
2948 (Attributes) $1, Kind.Class, lexer.Location);
2950 current_container = part.PartialContainer;
2951 current_class = part;
2953 if ((mod_flags & Modifiers.STATIC) != 0) {
2954 current_class = new StaticClass (current_namespace, current_container, name, mod_flags,
2955 (Attributes) $1, lexer.Location);
2957 current_class = new Class (current_namespace, current_container, name, mod_flags,
2958 (Attributes) $1, lexer.Location);
2960 current_container = current_class;
2961 RootContext.Tree.RecordDecl (name, current_class);
2967 if (current_class.Name == "System.Object") {
2968 Report.Error (537, current_class.Location,
2969 "The class System.Object cannot have a base " +
2970 "class or implement an interface.");
2972 current_class.Bases = (ArrayList) $7;
2975 current_class.Register ();
2982 current_container = current_container.Parent;
2983 current_class = current_container;
2989 { $$ = (bool) false; }
2991 { $$ = (bool) true; }
2995 : /* empty */ { $$ = (int) 0; }
3001 | modifiers modifier
3006 if ((m1 & m2) != 0) {
3007 Location l = lexer.Location;
3008 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3010 $$ = (int) (m1 | m2);
3015 : NEW { $$ = Modifiers.NEW; }
3016 | PUBLIC { $$ = Modifiers.PUBLIC; }
3017 | PROTECTED { $$ = Modifiers.PROTECTED; }
3018 | INTERNAL { $$ = Modifiers.INTERNAL; }
3019 | PRIVATE { $$ = Modifiers.PRIVATE; }
3020 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3021 | SEALED { $$ = Modifiers.SEALED; }
3022 | STATIC { $$ = Modifiers.STATIC; }
3023 | READONLY { $$ = Modifiers.READONLY; }
3024 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3025 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3026 | EXTERN { $$ = Modifiers.EXTERN; }
3027 | VOLATILE { $$ = Modifiers.VOLATILE; }
3028 | UNSAFE { $$ = Modifiers.UNSAFE; }
3032 : /* empty */ { $$ = null; }
3033 | class_base { $$ = $1; }
3037 : COLON type_list { $$ = $2; }
3045 // A block is "contained" on the following places:
3047 // property_declaration as part of the accessor body (get/set)
3048 // operator_declaration
3049 // constructor_declaration
3050 // destructor_declaration
3051 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3056 if (current_block == null || create_toplevel_block){
3057 current_block = new ToplevelBlock (current_local_parameters, lexer.Location);
3059 current_block = new Block (current_block, current_local_parameters,
3060 lexer.Location, Location.Null);
3063 opt_statement_list CLOSE_BRACE
3065 while (current_block.Implicit)
3066 current_block = current_block.Parent;
3068 current_block.SetEndLocation (lexer.Location);
3069 current_block = current_block.Parent;
3080 | statement_list statement
3084 : declaration_statement
3086 if ($1 != null && (Block) $1 != current_block){
3087 current_block.AddStatement ((Statement) $1);
3088 current_block = (Block) $1;
3091 | valid_declaration_statement
3093 current_block.AddStatement ((Statement) $1);
3098 valid_declaration_statement
3101 | expression_statement
3102 | selection_statement
3103 | iteration_statement
3107 | unchecked_statement
3115 : valid_declaration_statement
3116 | declaration_statement
3118 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3123 Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3131 $$ = EmptyStatement.Value;
3138 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3140 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3141 current_block.AddStatement (labeled);
3146 declaration_statement
3147 : local_variable_declaration SEMICOLON
3150 DictionaryEntry de = (DictionaryEntry) $1;
3152 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3156 | local_constant_declaration SEMICOLON
3159 DictionaryEntry de = (DictionaryEntry) $1;
3161 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3167 * The following is from Rhys' grammar:
3168 * > Types in local variable declarations must be recognized as
3169 * > expressions to prevent reduce/reduce errors in the grammar.
3170 * > The expressions are converted into types during semantic analysis.
3173 : primary_expression opt_rank_specifier
3175 // FIXME: Do something smart here regarding the composition of the type.
3177 // Ok, the above "primary_expression" is there to get rid of
3178 // both reduce/reduce and shift/reduces in the grammar, it should
3179 // really just be "type_name". If you use type_name, a reduce/reduce
3180 // creeps up. If you use namespace_or_type_name (which is all we need
3181 // really) two shift/reduces appear.
3184 // So the super-trick is that primary_expression
3185 // can only be either a SimpleName or a MemberAccess.
3186 // The MemberAccess case arises when you have a fully qualified type-name like :
3188 // SimpleName is when you have
3191 Expression expr = (Expression) $1;
3192 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3193 Error_ExpectingTypeName (lexer.Location, expr);
3197 // So we extract the string corresponding to the SimpleName
3201 if ((string) $2 == "")
3204 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3207 | builtin_types opt_rank_specifier
3209 if ((string) $2 == "")
3212 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3216 local_variable_pointer_type
3217 : primary_expression STAR
3219 Expression expr = (Expression) $1;
3220 Location l = lexer.Location;
3222 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3223 Error_ExpectingTypeName (l, expr);
3227 $$ = new ComposedCast ((Expression) $1, "*", l);
3229 | builtin_types STAR
3231 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3235 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3237 | local_variable_pointer_type STAR
3239 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3243 local_variable_declaration
3244 : local_variable_type variable_declarators
3247 $$ = new DictionaryEntry ($1, $2);
3251 | local_variable_pointer_type opt_rank_specifier variable_declarators
3256 if ((string) $2 == "")
3257 t = (Expression) $1;
3259 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3260 $$ = new DictionaryEntry (t, $3);
3266 local_constant_declaration
3267 : CONST local_variable_type constant_declarators
3270 $$ = new DictionaryEntry ($2, $3);
3276 expression_statement
3277 : statement_expression SEMICOLON
3284 // We have to do the wrapping here and not in the case above,
3285 // because statement_expression is used for example in for_statement
3287 statement_expression
3288 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3289 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3290 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3291 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3292 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3293 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3294 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3296 Report.Error (1002, lexer.Location, "Expecting `;'");
3301 object_creation_expression
3302 : object_or_delegate_creation_expression
3303 { note ("complain if this is a delegate maybe?"); }
3312 : if_statement_open if_statement_rest
3321 oob_stack.Push (lexer.Location);
3326 : boolean_expression CLOSE_PARENS
3329 Location l = (Location) oob_stack.Pop ();
3331 $$ = new If ((Expression) $1, (Statement) $3, l);
3333 if (RootContext.WarningLevel >= 3){
3334 if ($3 == EmptyStatement.Value)
3335 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3339 | boolean_expression CLOSE_PARENS
3340 embedded_statement ELSE embedded_statement
3342 Location l = (Location) oob_stack.Pop ();
3344 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
3349 : SWITCH OPEN_PARENS
3351 oob_stack.Push (lexer.Location);
3352 switch_stack.Push (current_block);
3354 expression CLOSE_PARENS
3357 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
3358 current_block = (Block) switch_stack.Pop ();
3374 Report.Error (1522, lexer.Location, "Empty switch block");
3382 ArrayList sections = new ArrayList (4);
3387 | switch_sections switch_section
3389 ArrayList sections = (ArrayList) $1;
3399 current_block = current_block.CreateSwitchBlock (lexer.Location);
3403 Block topmost = current_block;
3405 while (topmost.Implicit)
3406 topmost = topmost.Parent;
3407 $$ = new SwitchSection ((ArrayList) $1, topmost);
3414 ArrayList labels = new ArrayList (4);
3419 | switch_labels switch_label
3421 ArrayList labels = (ArrayList) ($1);
3429 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3430 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3433 1523, lexer.Location,
3434 "The keyword case or default must precede code in switch block");
3448 oob_stack.Push (lexer.Location);
3450 boolean_expression CLOSE_PARENS embedded_statement
3452 Location l = (Location) oob_stack.Pop ();
3453 $$ = new While ((Expression) $4, (Statement) $6, l);
3455 if (RootContext.WarningLevel >= 3){
3456 if ($6 == EmptyStatement.Value)
3457 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3463 : DO embedded_statement
3465 oob_stack.Push (lexer.Location);
3467 boolean_expression CLOSE_PARENS SEMICOLON
3469 Location l = (Location) oob_stack.Pop ();
3471 $$ = new Do ((Statement) $2, (Expression) $6, l);
3477 opt_for_initializer SEMICOLON
3479 Block assign_block = new Block (current_block);
3480 current_block = assign_block;
3482 if ($3 is DictionaryEntry){
3483 DictionaryEntry de = (DictionaryEntry) $3;
3485 Expression type = (Expression) de.Key;
3486 ArrayList var_declarators = (ArrayList) de.Value;
3488 foreach (VariableDeclaration decl in var_declarators){
3492 vi = current_block.AddVariable (
3493 type, decl.identifier, current_local_parameters, decl.Location);
3497 Location l = lexer.Location;
3499 if (decl.expression_or_array_initializer is Expression){
3500 expr = (Expression) decl.expression_or_array_initializer;
3501 } else if (decl.expression_or_array_initializer == null) {
3504 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3505 expr = new ArrayCreation (type, "", init, decl.Location);
3508 LocalVariableReference var;
3509 var = new LocalVariableReference (assign_block, decl.identifier, l);
3512 Assign a = new Assign (var, expr, decl.Location);
3514 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3520 oob_stack.Push (lexer.Location);
3522 opt_for_condition SEMICOLON
3523 opt_for_iterator CLOSE_PARENS
3526 Location l = (Location) oob_stack.Pop ();
3528 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3530 if (RootContext.WarningLevel >= 3){
3531 if ($10 == EmptyStatement.Value)
3532 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3535 current_block.AddStatement (f);
3536 while (current_block.Implicit)
3537 current_block = current_block.Parent;
3539 current_block = current_block.Parent;
3544 : /* empty */ { $$ = EmptyStatement.Value; }
3549 : local_variable_declaration
3550 | statement_expression_list
3554 : /* empty */ { $$ = null; }
3555 | boolean_expression
3559 : /* empty */ { $$ = EmptyStatement.Value; }
3564 : statement_expression_list
3567 statement_expression_list
3568 : statement_expression
3570 // CHANGE: was `null'
3571 Block b = new Block (current_block, Block.Flags.Implicit);
3573 b.AddStatement ((Statement) $1);
3576 | statement_expression_list COMMA statement_expression
3578 Block b = (Block) $1;
3580 b.AddStatement ((Statement) $3);
3586 : FOREACH OPEN_PARENS type IDENTIFIER IN
3588 oob_stack.Push (lexer.Location);
3590 expression CLOSE_PARENS
3592 oob_stack.Push (current_block);
3594 Block foreach_block = new Block (current_block);
3595 LocalVariableReference v = null;
3596 Location l = lexer.Location;
3599 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3603 // Get a writable reference to this read-only variable.
3604 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3606 current_block = foreach_block;
3609 oob_stack.Push (current_block);
3613 Block foreach_block = (Block) oob_stack.Pop ();
3614 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3615 Block prev_block = (Block) oob_stack.Pop ();
3616 Location l = (Location) oob_stack.Pop ();
3618 current_block = prev_block;
3621 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3622 foreach_block.AddStatement (f);
3631 | continue_statement
3641 $$ = new Break (lexer.Location);
3646 : CONTINUE SEMICOLON
3648 $$ = new Continue (lexer.Location);
3653 : GOTO IDENTIFIER SEMICOLON
3655 $$ = new Goto (current_block, (string) $2, lexer.Location);
3657 | GOTO CASE constant_expression SEMICOLON
3659 $$ = new GotoCase ((Expression) $3, lexer.Location);
3661 | GOTO DEFAULT SEMICOLON
3663 $$ = new GotoDefault (lexer.Location);
3668 : RETURN opt_expression SEMICOLON
3670 $$ = new Return ((Expression) $2, lexer.Location);
3675 : THROW opt_expression SEMICOLON
3677 $$ = new Throw ((Expression) $2, lexer.Location);
3682 : IDENTIFIER RETURN expression SEMICOLON
3684 string s = (string) $1;
3686 Report.Error (1003, lexer.Location, "; expected");
3689 if (RootContext.Version == LanguageVersion.ISO_1){
3690 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
3693 if (iterator_container == null){
3694 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3697 iterator_container.SetYields ();
3698 $$ = new Yield ((Expression) $3, lexer.Location);
3701 | IDENTIFIER BREAK SEMICOLON
3703 string s = (string) $1;
3705 Report.Error (1003, lexer.Location, "; expected");
3708 if (RootContext.Version == LanguageVersion.ISO_1){
3709 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
3712 if (iterator_container == null){
3713 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3716 iterator_container.SetYields ();
3717 $$ = new YieldBreak (lexer.Location);
3728 : TRY block catch_clauses
3731 ArrayList s = new ArrayList (4);
3733 foreach (Catch cc in (ArrayList) $3) {
3740 // Now s contains the list of specific catch clauses
3741 // and g contains the general one.
3743 $$ = new Try ((Block) $2, s, g, null, lexer.Location);
3745 | TRY block opt_catch_clauses FINALLY block
3748 ArrayList s = new ArrayList (4);
3749 ArrayList catch_list = (ArrayList) $3;
3751 if (catch_list != null){
3752 foreach (Catch cc in catch_list) {
3760 $$ = new Try ((Block) $2, s, g, (Block) $5, lexer.Location);
3764 Report.Error (1524, lexer.Location, "Expected catch or finally");
3769 : /* empty */ { $$ = null; }
3776 ArrayList l = new ArrayList (4);
3781 | catch_clauses catch_clause
3783 ArrayList l = (ArrayList) $1;
3791 : /* empty */ { $$ = null; }
3796 : CATCH opt_catch_args
3798 Expression type = null;
3802 DictionaryEntry cc = (DictionaryEntry) $2;
3803 type = (Expression) cc.Key;
3804 id = (string) cc.Value;
3807 ArrayList one = new ArrayList (4);
3808 Location loc = lexer.Location;
3810 one.Add (new VariableDeclaration (id, null, loc));
3813 current_block = new Block (current_block);
3814 Block b = declare_local_variables (type, one, loc);
3819 Expression type = null;
3823 DictionaryEntry cc = (DictionaryEntry) $2;
3824 type = (Expression) cc.Key;
3825 id = (string) cc.Value;
3829 // FIXME: I can change this for an assignment.
3831 while (current_block != (Block) $1)
3832 current_block = current_block.Parent;
3837 $$ = new Catch (type, id , (Block) $4, lexer.Location);
3842 : /* empty */ { $$ = null; }
3847 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3849 $$ = new DictionaryEntry ($2, $3);
3856 $$ = new Checked ((Block) $2);
3863 $$ = new Unchecked ((Block) $2);
3870 if (!RootContext.Unsafe){
3871 Report.Error (227, lexer.Location,
3872 "Unsafe code can only be used if --unsafe is used");
3875 $$ = new Unsafe ((Block) $3);
3881 type fixed_pointer_declarators
3884 Block assign_block = new Block (current_block, Block.Flags.Implicit);
3885 ArrayList list = (ArrayList) $4;
3886 Expression type = (Expression) $3;
3887 Location l = lexer.Location;
3888 int top = list.Count;
3890 for (int i = 0; i < top; i++){
3891 Pair p = (Pair) list [i];
3894 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3903 current_block.AddStatement (assign_block);
3904 current_block = assign_block;
3905 oob_stack.Push (assign_block);
3910 Location l = (Location) oob_stack.Pop ();
3913 $$ = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
3917 fixed_pointer_declarators
3918 : fixed_pointer_declarator {
3919 ArrayList declarators = new ArrayList (4);
3921 declarators.Add ($1);
3924 | fixed_pointer_declarators COMMA fixed_pointer_declarator
3926 ArrayList declarators = (ArrayList) $1;
3928 declarators.Add ($3);
3933 fixed_pointer_declarator
3934 : IDENTIFIER ASSIGN expression
3936 $$ = new Pair ($1, $3);
3940 Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
3946 : LOCK OPEN_PARENS expression CLOSE_PARENS
3952 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
3957 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
3959 Block assign_block = new Block (current_block);
3960 current_block = assign_block;
3962 oob_stack.Push (lexer.Location);
3964 if ($3 is DictionaryEntry){
3965 DictionaryEntry de = (DictionaryEntry) $3;
3966 Location l = lexer.Location;
3968 Expression type = (Expression) de.Key;
3969 ArrayList var_declarators = (ArrayList) de.Value;
3971 ArrayList vars = new ArrayList (4);
3973 foreach (VariableDeclaration decl in var_declarators){
3975 LocalInfo vi = current_block.AddVariable (
3976 type, decl.identifier,
3977 current_local_parameters, decl.Location);
3983 if (decl.expression_or_array_initializer is Expression){
3984 expr = (Expression) decl.expression_or_array_initializer;
3986 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3988 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
3991 expr = new ArrayCreation (type, "", init, decl.Location);
3994 LocalVariableReference var;
3996 // Get a writable reference to this read-only variable.
3997 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
3999 // This is so that it is not a warning on using variables
4002 vars.Add (new DictionaryEntry (var, expr));
4004 // Assign a = new Assign (var, expr, decl.Location);
4005 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4007 $3 = new DictionaryEntry (type, vars);
4012 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
4013 current_block.AddStatement (u);
4014 while (current_block.Implicit)
4015 current_block = current_block.Parent;
4017 current_block = current_block.Parent;
4021 resource_acquisition
4022 : local_variable_declaration
4029 // A class used to pass around variable declarations and constants
4031 public class VariableDeclaration {
4032 public string identifier;
4033 public object expression_or_array_initializer;
4034 public Location Location;
4035 public Attributes OptAttributes;
4037 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4039 this.identifier = id;
4040 this.expression_or_array_initializer = eoai;
4042 this.OptAttributes = opt_attrs;
4045 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4051 /// Used to pass around interface property information
4053 public class InterfaceAccessorInfo {
4055 public readonly Accessor Get, Set;
4057 public InterfaceAccessorInfo (bool has_get, bool has_set,
4058 Attributes get_attrs, Attributes set_attrs, Location get_loc, Location set_loc)
4061 Get = new Accessor (null, get_attrs, get_loc);
4063 Set = new Accessor (null, set_attrs, set_loc);
4069 // A class used to hold info about an indexer declarator
4071 public class IndexerDeclaration {
4072 public Expression type;
4073 public string interface_type;
4074 public Parameters param_list;
4076 public IndexerDeclaration (Expression type, string interface_type, Parameters param_list)
4079 this.interface_type = interface_type;
4080 this.param_list = param_list;
4085 // We use this when we do not have an object in advance that is an IIteratorContainer
4087 public class SimpleIteratorContainer : IIteratorContainer {
4090 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4095 public static SimpleIteratorContainer GetSimple () {
4096 Simple.Yields = false;
4100 public void SetYields () { Yields = true; }
4104 // A class used to hold info about an operator declarator
4106 public class OperatorDeclaration {
4107 public Operator.OpType optype;
4108 public Expression ret_type, arg1type, arg2type;
4109 public string arg1name, arg2name;
4110 public Location location;
4112 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4113 Expression arg1type, string arg1name,
4114 Expression arg2type, string arg2name, Location location)
4117 this.ret_type = ret_type;
4118 this.arg1type = arg1type;
4119 this.arg1name = arg1name;
4120 this.arg2type = arg2type;
4121 this.arg2name = arg2name;
4122 this.location = location;
4127 void Error_ExpectingTypeName (Location l, Expression expr)
4129 if (expr is Invocation){
4130 Report.Error (1002, l, "; expected");
4132 Report.Error (-1, l, "Invalid Type definition");
4137 // Given the @class_name name, it creates a fully qualified name
4138 // based on the containing declaration space
4141 MakeName (string class_name)
4143 string ns = current_namespace.FullName;
4144 string container_name = current_container.Name;
4146 if (container_name == ""){
4148 return ns + "." + class_name;
4152 return container_name + "." + class_name;
4155 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4157 Block implicit_block;
4158 ArrayList inits = null;
4161 // We use the `Used' property to check whether statements
4162 // have been added to the current block. If so, we need
4163 // to create another block to contain the new declaration
4164 // otherwise, as an optimization, we use the same block to
4165 // add the declaration.
4167 // FIXME: A further optimization is to check if the statements
4168 // that were added were added as part of the initialization
4169 // below. In which case, no other statements have been executed
4170 // and we might be able to reduce the number of blocks for
4171 // situations like this:
4173 // int j = 1; int k = j + 1;
4175 if (current_block.Used)
4176 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4178 implicit_block = current_block;
4180 foreach (VariableDeclaration decl in variable_declarators){
4182 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4183 if (decl.expression_or_array_initializer != null){
4185 inits = new ArrayList (4);
4192 return implicit_block;
4194 foreach (VariableDeclaration decl in inits){
4198 if (decl.expression_or_array_initializer is Expression){
4199 expr = (Expression) decl.expression_or_array_initializer;
4202 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4204 expr = new ArrayCreation (type, "", init, decl.Location);
4207 LocalVariableReference var;
4208 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4210 assign = new Assign (var, expr, decl.Location);
4212 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4215 return implicit_block;
4218 Block declare_local_constants (Expression type, ArrayList declarators)
4220 Block implicit_block;
4222 if (current_block.Used)
4223 implicit_block = new Block (current_block, Block.Flags.Implicit);
4225 implicit_block = current_block;
4227 foreach (VariableDeclaration decl in declarators){
4228 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4229 current_local_parameters, decl.Location);
4232 return implicit_block;
4235 void CheckAttributeTarget (string a)
4239 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4243 Location l = lexer.Location;
4244 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4250 void CheckUnaryOperator (Operator.OpType op)
4254 case Operator.OpType.LogicalNot:
4255 case Operator.OpType.OnesComplement:
4256 case Operator.OpType.Increment:
4257 case Operator.OpType.Decrement:
4258 case Operator.OpType.True:
4259 case Operator.OpType.False:
4260 case Operator.OpType.Addition:
4261 case Operator.OpType.Subtraction:
4266 Location l = lexer.Location;
4267 Report.Error (1019, l, "Overloadable unary operator expected");
4273 void CheckBinaryOperator (Operator.OpType op)
4277 case Operator.OpType.Addition:
4278 case Operator.OpType.Subtraction:
4279 case Operator.OpType.Multiply:
4280 case Operator.OpType.Division:
4281 case Operator.OpType.Modulus:
4282 case Operator.OpType.BitwiseAnd:
4283 case Operator.OpType.BitwiseOr:
4284 case Operator.OpType.ExclusiveOr:
4285 case Operator.OpType.LeftShift:
4286 case Operator.OpType.RightShift:
4287 case Operator.OpType.Equality:
4288 case Operator.OpType.Inequality:
4289 case Operator.OpType.GreaterThan:
4290 case Operator.OpType.LessThan:
4291 case Operator.OpType.GreaterThanOrEqual:
4292 case Operator.OpType.LessThanOrEqual:
4296 Location l = lexer.Location;
4297 Report.Error (1020, l, "Overloadable binary operator expected");
4303 void syntax_error (Location l, string msg)
4305 Report.Error (1003, l, "Syntax error, " + msg);
4308 void output (string s)
4310 Console.WriteLine (s);
4313 void note (string s)
4315 // Used to put annotations
4320 public Tokenizer Lexer {
4326 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4328 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4329 this.name = file.Name;
4331 current_container = RootContext.Tree.Types;
4332 current_container.NamespaceEntry = current_namespace;
4333 oob_stack = new Stack ();
4334 switch_stack = new Stack ();
4336 lexer = new Tokenizer (reader, file, defines);
4339 public override void parse ()
4342 if (yacc_verbose_flag)
4343 yyparse (lexer, new yydebug.yyDebugSimple ());
4346 Tokenizer tokenizer = lexer as Tokenizer;
4347 tokenizer.cleanup ();
4348 } catch (Exception e){
4349 // Please do not remove this, it is used during debugging
4352 Console.WriteLine (e);
4353 Report.Error (-25, lexer.Location, "Parsing error");
4354 if (Driver.parser_verbose)
4355 Console.WriteLine (e);
4359 void CheckToken (int error, int yyToken, string msg)
4361 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4362 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4365 Report.Error (error, lexer.Location, msg);
4368 void CheckIdentifierToken (int yyToken)
4370 CheckToken (1041, yyToken, "Identifier expected");