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, (MemberName) $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 MemberName name = (MemberName) $3;
344 if ((current_namespace.Parent != null) && (name.Left != null)) {
345 Report.Error (134, lexer.Location,
346 "Cannot use qualified namespace names in nested " +
347 "namespace declarations");
350 current_namespace = new NamespaceEntry (
351 current_namespace, file, name.GetName (false), lexer.Location);
353 namespace_body opt_semicolon
355 current_namespace = current_namespace.Parent;
370 : namespace_or_type_name {
371 MemberName name = (MemberName) $1;
373 $$ = name.GetName (false);
380 opt_namespace_member_declarations
391 opt_namespace_member_declarations
393 | namespace_member_declarations
396 namespace_member_declarations
397 : namespace_member_declaration
398 | namespace_member_declarations namespace_member_declaration
401 namespace_member_declaration
408 Class c = (Class) $1;
409 mod_flags = c.ModFlags;
411 } else if ($1 is Struct){
412 Struct s = (Struct) $1;
413 mod_flags = s.ModFlags;
418 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
420 1527, lexer.Location,
421 "Namespace elements cant be explicitly " +
422 "declared private or protected in `" + name + "'");
424 current_namespace.DeclarationFound = true;
426 | namespace_declaration {
427 current_namespace.DeclarationFound = true;
430 | field_declaration {
431 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
433 | method_declaration {
434 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
441 | interface_declaration
443 | delegate_declaration
445 // Enable this when we have handled all errors, because this acts as a generic fallback
448 // Console.WriteLine ("Token=" + yyToken);
449 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
461 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
469 global_attrs_enabled = false;
474 global_attrs_enabled = false;
483 ArrayList sect = (ArrayList) $1;
485 if (global_attrs_enabled) {
486 if (current_attr_target == "module") {
487 CodeGen.Module.AddAttributes (sect);
489 } else if (current_attr_target == "assembly") {
490 CodeGen.Assembly.AddAttributes (sect);
493 $$ = new Attributes (sect);
496 $$ = new Attributes (sect);
498 current_attr_target = null;
500 | attribute_sections attribute_section
502 Attributes attrs = $1 as Attributes;
503 ArrayList sect = (ArrayList) $2;
505 if (global_attrs_enabled) {
506 if (current_attr_target == "module") {
507 CodeGen.Module.AddAttributes (sect);
509 } else if (current_attr_target == "assembly") {
510 CodeGen.Assembly.AddAttributes (sect);
514 attrs = new Attributes (sect);
516 attrs.AddAttributes (sect);
520 attrs = new Attributes (sect);
522 attrs.AddAttributes (sect);
525 current_attr_target = null;
530 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
534 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
540 attribute_target_specifier
541 : attribute_target COLON
543 current_attr_target = (string)$1;
551 CheckAttributeTarget ((string) $1);
554 | EVENT { $$ = "event"; }
555 | RETURN { $$ = "return"; }
561 ArrayList attrs = new ArrayList (4);
567 | attribute_list COMMA attribute
569 ArrayList attrs = (ArrayList) $1;
581 opt_attribute_arguments
583 if (current_attr_target == "assembly" || current_attr_target == "module")
584 $$ = new GlobalAttribute (current_container, current_attr_target, (string) ($1).ToString (), (ArrayList) $3, (Location) $2);
586 $$ = new Attribute (current_attr_target, (string) ($1).ToString (), (ArrayList) $3, (Location) $2);
591 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
594 opt_attribute_arguments
595 : /* empty */ { $$ = null; }
596 | OPEN_PARENS attribute_arguments CLOSE_PARENS
604 : opt_positional_argument_list
609 ArrayList args = new ArrayList (4);
615 | positional_argument_list COMMA named_argument_list
617 ArrayList args = new ArrayList (4);
623 | named_argument_list
625 ArrayList args = new ArrayList (4);
634 opt_positional_argument_list
635 : /* empty */ { $$ = null; }
636 | positional_argument_list
639 positional_argument_list
642 ArrayList args = new ArrayList (4);
643 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
647 | positional_argument_list COMMA expression
649 ArrayList args = (ArrayList) $1;
650 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
659 ArrayList args = new ArrayList (4);
664 | named_argument_list COMMA named_argument
666 ArrayList args = (ArrayList) $1;
674 : IDENTIFIER ASSIGN expression
676 $$ = new DictionaryEntry (
678 new Argument ((Expression) $3, Argument.AType.Expression));
684 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
687 opt_class_member_declarations
689 | class_member_declarations
692 class_member_declarations
693 : class_member_declaration
694 | class_member_declarations
695 class_member_declaration
698 class_member_declaration
699 : constant_declaration // done
700 | field_declaration // done
701 | method_declaration // done
702 | property_declaration // done
703 | event_declaration // done
704 | indexer_declaration // done
705 | operator_declaration // done
706 | constructor_declaration // done
707 | destructor_declaration // done
717 MemberName name = MakeName ((MemberName) $5);
718 bool partial = (bool) $3;
721 ClassPart part = PartialContainer.CreatePart (
722 current_namespace, current_container, name, (int) $2,
723 (Attributes) $1, Kind.Struct, lexer.Location);
725 current_container = part.PartialContainer;
726 current_class = part;
728 current_class = new Struct (
729 current_namespace, current_container, name, (int) $2,
730 (Attributes) $1, lexer.Location);
732 current_container = current_class;
733 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
739 current_class.Bases = (ArrayList) $7;
741 current_class.Register ();
748 current_container = current_container.Parent;
749 current_class = current_container;
751 | opt_attributes opt_modifiers opt_partial STRUCT error {
752 CheckIdentifierToken (yyToken);
757 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
760 opt_struct_member_declarations
762 | struct_member_declarations
765 struct_member_declarations
766 : struct_member_declaration
767 | struct_member_declarations struct_member_declaration
770 struct_member_declaration
771 : constant_declaration
774 | property_declaration
776 | indexer_declaration
777 | operator_declaration
778 | constructor_declaration
782 * This is only included so we can flag error 575:
783 * destructors only allowed on class types
785 | destructor_declaration
796 foreach (VariableDeclaration constant in (ArrayList) $5){
797 Location l = constant.Location;
799 Const c = new Const (
800 current_class, (Expression) $4, (string) constant.identifier,
801 (Expression) constant.expression_or_array_initializer, (int) $2,
804 current_container.AddConstant (c);
810 : constant_declarator
812 ArrayList constants = new ArrayList (4);
817 | constant_declarators COMMA constant_declarator
820 ArrayList constants = (ArrayList) $1;
827 : IDENTIFIER ASSIGN constant_expression
829 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
833 // A const field requires a value to be provided
834 Report.Error (145, lexer.Location, "A const field requires a value to be provided");
846 Expression type = (Expression) $3;
849 foreach (VariableDeclaration var in (ArrayList) $4){
850 Location l = var.Location;
852 Field field = new Field (current_class, type, mod, var.identifier,
853 var.expression_or_array_initializer,
856 current_container.AddField (field);
864 Report.Error (670, lexer.Location, "void type is not allowed for fields");
869 : variable_declarator
871 ArrayList decl = new ArrayList (4);
875 | variable_declarators COMMA variable_declarator
877 ArrayList decls = (ArrayList) $1;
884 : IDENTIFIER ASSIGN variable_initializer
886 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
890 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
903 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
905 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
911 iterator_container = (IIteratorContainer) $1;
915 Method method = (Method) $1;
916 Block b = (Block) $3;
917 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
920 if ((method.ModFlags & extern_abstract) == 0){
922 501, lexer.Location, current_container.MakeName (method.Name) +
923 "must declare a body because it is not marked abstract or extern");
926 if ((method.ModFlags & Modifiers.EXTERN) != 0){
928 179, lexer.Location, current_container.MakeName (method.Name) +
929 " is declared extern, but has a body");
933 method.Block = (Block) $3;
934 current_container.AddMethod (method);
936 current_local_parameters = null;
937 iterator_container = null;
951 1585, lexer.Location, "Member modifier `" +
952 Modifiers.Name (i) + "' must precede member type and name");
963 type namespace_or_type_name
964 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
966 MemberName name = (MemberName) $4;
968 Method method = new Method (current_class, (Expression) $3, (int) $2,
969 false, name, (Parameters) $6, (Attributes) $1,
972 current_local_parameters = (Parameters) $6;
978 VOID namespace_or_type_name
979 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
981 MemberName name = (MemberName) $4;
983 Method method = new Method (current_class, TypeManager.system_void_expr,
984 (int) $2, false, name, (Parameters) $6,
985 (Attributes) $1, lexer.Location);
987 current_local_parameters = (Parameters) $6;
993 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
995 Report.Error (1585, lexer.Location,
996 String.Format ("Modifier {0} should appear before type",
997 Modifiers.Name ((int) $4)));
998 MemberName name = (MemberName) $4;
1000 Method method = new Method (current_class, TypeManager.system_void_expr,
1001 0, false, name, (Parameters) $7, (Attributes) $1,
1004 current_local_parameters = (Parameters) $7;
1011 | SEMICOLON { $$ = null; }
1014 opt_formal_parameter_list
1015 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1016 | formal_parameter_list
1019 formal_parameter_list
1022 ArrayList pars_list = (ArrayList) $1;
1024 Parameter [] pars = new Parameter [pars_list.Count];
1025 pars_list.CopyTo (pars);
1027 $$ = new Parameters (pars, null, lexer.Location);
1029 | fixed_parameters COMMA parameter_array
1031 ArrayList pars_list = (ArrayList) $1;
1033 Parameter [] pars = new Parameter [pars_list.Count];
1034 pars_list.CopyTo (pars);
1036 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
1038 | fixed_parameters COMMA ARGLIST
1040 ArrayList pars_list = (ArrayList) $1;
1042 Parameter [] pars = new Parameter [pars_list.Count];
1043 pars_list.CopyTo (pars);
1045 $$ = new Parameters (pars, true, lexer.Location);
1047 | parameter_array COMMA fixed_parameters
1049 Report.Error (231, lexer.Location, "A params parameter must be the last parameter in a formal parameter list");
1052 | ARGLIST COMMA fixed_parameters
1054 Report.Error (257, lexer.Location, "An __arglist parameter must be the last parameter in a formal parameter list");
1059 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
1063 $$ = new Parameters (null, true, lexer.Location);
1070 ArrayList pars = new ArrayList (4);
1075 | fixed_parameters COMMA fixed_parameter
1077 ArrayList pars = (ArrayList) $1;
1086 opt_parameter_modifier
1090 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
1093 opt_parameter_modifier
1096 CheckIdentifierToken (yyToken);
1100 opt_parameter_modifier
1106 Report.Error (241, lexer.Location, "Default parameter specifiers are not permitted");
1111 opt_parameter_modifier
1112 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1113 | parameter_modifier
1117 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1118 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1122 : opt_attributes PARAMS type IDENTIFIER
1124 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1125 note ("type must be a single-dimension array type");
1127 | opt_attributes PARAMS type error {
1128 CheckIdentifierToken (yyToken);
1133 property_declaration
1136 type namespace_or_type_name
1139 implicit_value_parameter_type = (Expression) $3;
1141 lexer.PropertyParsing = true;
1143 $$ = lexer.Location;
1145 iterator_container = SimpleIteratorContainer.GetSimple ();
1147 accessor_declarations
1149 lexer.PropertyParsing = false;
1154 Pair pair = (Pair) $7;
1155 Accessor get_block = (Accessor) pair.First;
1156 Accessor set_block = (Accessor) pair.Second;
1158 Location loc = (Location) $6;
1159 MemberName name = (MemberName) $4;
1161 prop = new Property (current_class, (Expression) $3, (int) $2, false,
1162 name, (Attributes) $1, get_block, set_block, loc);
1163 if (SimpleIteratorContainer.Simple.Yields)
1166 current_container.AddProperty (prop);
1167 implicit_value_parameter_type = null;
1168 iterator_container = null;
1172 accessor_declarations
1173 : get_accessor_declaration opt_set_accessor_declaration
1175 $$ = new Pair ($1, $2);
1177 | set_accessor_declaration opt_get_accessor_declaration
1179 $$ = new Pair ($2, $1);
1183 opt_get_accessor_declaration
1184 : /* empty */ { $$ = null; }
1185 | get_accessor_declaration
1188 opt_set_accessor_declaration
1189 : /* empty */ { $$ = null; }
1190 | set_accessor_declaration
1193 get_accessor_declaration
1194 : opt_attributes GET
1196 // If this is not the case, then current_local_parameters has already
1197 // been set in indexer_declaration
1198 if (parsing_indexer == false)
1199 current_local_parameters = null;
1201 current_local_parameters = indexer_parameters;
1202 lexer.PropertyParsing = false;
1206 $$ = new Accessor ((Block) $4, (Attributes) $1, lexer.Location);
1207 current_local_parameters = null;
1208 lexer.PropertyParsing = true;
1212 set_accessor_declaration
1213 : opt_attributes SET
1216 Parameter implicit_value_parameter = new Parameter (
1217 implicit_value_parameter_type, "value",
1218 Parameter.Modifier.NONE, null);
1220 if (parsing_indexer == false) {
1221 args = new Parameter [1];
1222 args [0] = implicit_value_parameter;
1223 current_local_parameters = new Parameters (args, null, lexer.Location);
1225 Parameter [] fpars = indexer_parameters.FixedParameters;
1228 int count = fpars.Length;
1230 args = new Parameter [count + 1];
1231 fpars.CopyTo (args, 0);
1232 args [count] = implicit_value_parameter;
1235 current_local_parameters = new Parameters (
1236 args, indexer_parameters.ArrayParameter, lexer.Location);
1239 lexer.PropertyParsing = false;
1243 $$ = new Accessor ((Block) $4, (Attributes) $1, lexer.Location);
1244 current_local_parameters = null;
1245 lexer.PropertyParsing = true;
1251 | SEMICOLON { $$ = null; }
1254 interface_declaration
1258 INTERFACE member_name
1260 MemberName name = MakeName ((MemberName) $5);
1261 bool partial = (bool) $3;
1264 ClassPart part = PartialContainer.CreatePart (
1265 current_namespace, current_container, name, (int) $2,
1266 (Attributes) $1, Kind.Interface, lexer.Location);
1268 current_container = part.PartialContainer;
1269 current_class = part;
1271 current_class = new Interface (
1272 current_namespace, current_container, name, (int) $2,
1273 (Attributes) $1, lexer.Location);
1275 current_container = current_class;
1276 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
1281 current_class.Bases = (ArrayList) $7;
1283 current_class.Register ();
1290 current_container = current_container.Parent;
1291 current_class = current_container;
1293 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1294 CheckIdentifierToken (yyToken);
1300 opt_interface_member_declarations
1304 opt_interface_member_declarations
1306 | interface_member_declarations
1309 interface_member_declarations
1310 : interface_member_declaration
1311 | interface_member_declarations interface_member_declaration
1314 interface_member_declaration
1315 : interface_method_declaration
1317 Method m = (Method) $1;
1319 current_container.AddMethod (m);
1321 | interface_property_declaration
1323 Property p = (Property) $1;
1325 current_container.AddProperty (p);
1327 | interface_event_declaration
1330 Event e = (Event) $1;
1331 current_container.AddEvent (e);
1334 | interface_indexer_declaration
1336 Indexer i = (Indexer) $1;
1338 current_container.AddIndexer (i);
1346 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, lexer.Location);
1351 interface_method_declaration
1352 : opt_attributes opt_new type namespace_or_type_name
1353 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1356 MemberName name = (MemberName) $4;
1358 $$ = new Method (current_class, (Expression) $3, (int) $2, true,
1359 name, (Parameters) $6, (Attributes) $1, lexer.Location);
1361 | opt_attributes opt_new VOID namespace_or_type_name
1362 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1365 MemberName name = (MemberName) $4;
1367 $$ = new Method (current_class, TypeManager.system_void_expr, (int) $2,
1368 true, name, (Parameters) $6, (Attributes) $1, lexer.Location);
1372 interface_property_declaration
1377 { lexer.PropertyParsing = true; }
1379 { lexer.PropertyParsing = false; }
1382 InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
1384 $$ = new Property (current_class, (Expression) $3, (int) $2, true,
1385 new MemberName ((string) $4), (Attributes) $1,
1386 pinfo.Get, pinfo.Set, lexer.Location);
1391 CheckIdentifierToken (yyToken);
1397 : opt_attributes GET SEMICOLON { $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, lexer.Location, lexer.Location); }
1398 | opt_attributes SET SEMICOLON { $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1, lexer.Location, lexer.Location); }
1399 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1400 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $3, lexer.Location, lexer.Location); }
1401 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1402 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $3, (Attributes) $1, lexer.Location, lexer.Location); }
1405 interface_event_declaration
1406 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1408 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1409 new MemberName ((string) $5), null,
1410 (Attributes) $1, lexer.Location);
1412 | opt_attributes opt_new EVENT type error {
1413 CheckIdentifierToken (yyToken);
1416 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1417 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1420 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE event_accessor_declarations CLOSE_BRACE {
1421 Report.Error (69, lexer.Location, "Event accessors not valid on interfaces");
1426 interface_indexer_declaration
1427 : opt_attributes opt_new type THIS
1428 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1430 { lexer.PropertyParsing = true; }
1432 { lexer.PropertyParsing = false; }
1435 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1437 $$ = new Indexer (current_class, (Expression) $3,
1438 new MemberName (TypeContainer.DefaultIndexerName),
1439 (int) $2, true, (Parameters) $6, (Attributes) $1,
1440 info.Get, info.Set, lexer.Location);
1444 operator_declaration
1445 : opt_attributes opt_modifiers operator_declarator
1447 iterator_container = SimpleIteratorContainer.GetSimple ();
1451 OperatorDeclaration decl = (OperatorDeclaration) $3;
1453 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1455 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null);
1456 if (decl.arg2type != null)
1457 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null);
1459 Operator op = new Operator (
1460 current_class, decl.optype, decl.ret_type, (int) $2,
1461 new Parameters (param_list, null, decl.location),
1462 (Block) $5, (Attributes) $1, decl.location);
1464 if (SimpleIteratorContainer.Simple.Yields)
1467 // Note again, checking is done in semantic analysis
1468 current_container.AddOperator (op);
1470 current_local_parameters = null;
1471 iterator_container = null;
1477 | SEMICOLON { $$ = null; }
1480 : type OPERATOR overloadable_operator
1481 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1483 Operator.OpType op = (Operator.OpType) $3;
1484 CheckUnaryOperator (op);
1486 if (op == Operator.OpType.Addition)
1487 op = Operator.OpType.UnaryPlus;
1489 if (op == Operator.OpType.Subtraction)
1490 op = Operator.OpType.UnaryNegation;
1492 Parameter [] pars = new Parameter [1];
1494 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1496 current_local_parameters = new Parameters (pars, null, lexer.Location);
1498 $$ = new OperatorDeclaration (op, (Expression) $1, (Expression) $5, (string) $6,
1499 null, null, lexer.Location);
1501 | type OPERATOR overloadable_operator
1503 type IDENTIFIER COMMA
1507 CheckBinaryOperator ((Operator.OpType) $3);
1509 Parameter [] pars = new Parameter [2];
1511 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1512 pars [1] = new Parameter ((Expression) $8, (string) $9, Parameter.Modifier.NONE, null);
1514 current_local_parameters = new Parameters (pars, null, lexer.Location);
1516 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1517 (Expression) $5, (string) $6,
1518 (Expression) $8, (string) $9, lexer.Location);
1520 | conversion_operator_declarator
1523 overloadable_operator
1525 : BANG { $$ = Operator.OpType.LogicalNot; }
1526 | TILDE { $$ = Operator.OpType.OnesComplement; }
1527 | OP_INC { $$ = Operator.OpType.Increment; }
1528 | OP_DEC { $$ = Operator.OpType.Decrement; }
1529 | TRUE { $$ = Operator.OpType.True; }
1530 | FALSE { $$ = Operator.OpType.False; }
1531 // Unary and binary:
1532 | PLUS { $$ = Operator.OpType.Addition; }
1533 | MINUS { $$ = Operator.OpType.Subtraction; }
1535 | STAR { $$ = Operator.OpType.Multiply; }
1536 | DIV { $$ = Operator.OpType.Division; }
1537 | PERCENT { $$ = Operator.OpType.Modulus; }
1538 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1539 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1540 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1541 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1542 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1543 | OP_EQ { $$ = Operator.OpType.Equality; }
1544 | OP_NE { $$ = Operator.OpType.Inequality; }
1545 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1546 | OP_LT { $$ = Operator.OpType.LessThan; }
1547 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1548 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1551 conversion_operator_declarator
1552 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1554 Parameter [] pars = new Parameter [1];
1556 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1558 current_local_parameters = new Parameters (pars, null, lexer.Location);
1560 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1561 null, null, lexer.Location);
1563 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1565 Parameter [] pars = new Parameter [1];
1567 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1569 current_local_parameters = new Parameters (pars, null, lexer.Location);
1571 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1572 null, null, lexer.Location);
1576 syntax_error (lexer.Location, "'operator' expected");
1580 syntax_error (lexer.Location, "'operator' expected");
1584 constructor_declaration
1587 constructor_declarator
1590 Constructor c = (Constructor) $3;
1591 c.Block = (Block) $4;
1592 c.OptAttributes = (Attributes) $1;
1593 c.ModFlags = (int) $2;
1595 if (c.Name == current_container.Basename){
1596 if ((c.ModFlags & Modifiers.STATIC) != 0){
1597 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1599 515, c.Location, String.Format (
1600 "`{0}.{1}': static constructor can not have access modifiers",
1601 c.Name, current_container.Name));
1604 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1606 if (c.Initializer != null){
1609 "Static constructors can not have an explicit this or base " +
1610 "constructor invocations");
1613 if (!c.Parameters.Empty){
1615 132, c.Location, "Static constructors should not have parameters");
1618 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1621 // We let another layer check the validity of the constructor.
1622 Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1625 current_container.AddConstructor (c);
1627 current_local_parameters = null;
1631 constructor_declarator
1633 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1635 oob_stack.Push (lexer.Location);
1637 current_local_parameters = (Parameters) $3;
1639 opt_constructor_initializer
1641 Location l = (Location) oob_stack.Pop ();
1642 $$ = new Constructor (current_class, (string) $1, 0, (Parameters) $3,
1643 (ConstructorInitializer) $6, l);
1649 | SEMICOLON { $$ = null; }
1652 opt_constructor_initializer
1653 : /* empty */ { $$ = null; }
1654 | constructor_initializer
1657 constructor_initializer
1658 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1660 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1662 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1664 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1667 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1673 : /* EMPTY */ { $$ = 0; }
1674 | UNSAFE { $$ = Modifiers.UNSAFE; }
1675 | EXTERN { $$ = Modifiers.EXTERN; }
1678 destructor_declaration
1679 : opt_attributes opt_finalizer TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1681 if ((string) $4 != current_container.Basename){
1682 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1683 } else if (!(current_container is Class)){
1684 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1686 Location l = lexer.Location;
1689 if (!RootContext.StdLib && current_container.Name == "System.Object")
1690 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
1692 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
1694 if ((m & Modifiers.UNSAFE) != 0){
1695 if (!RootContext.Unsafe){
1696 Report.Error (227, l,
1697 "Unsafe code requires the -unsafe command " +
1698 "line option to be specified");
1702 Method d = new Destructor (
1703 current_class, TypeManager.system_void_expr, m, "Finalize",
1704 new Parameters (null, null, l), (Attributes) $1, l);
1706 d.Block = (Block) $7;
1707 current_container.AddMethod (d);
1715 EVENT type variable_declarators SEMICOLON
1717 foreach (VariableDeclaration var in (ArrayList) $5) {
1719 MemberName name = new MemberName (var.identifier);
1721 Event e = new EventField (
1722 current_class, (Expression) $4, (int) $2, false, name,
1723 var.expression_or_array_initializer, (Attributes) $1,
1726 current_container.AddEvent (e);
1732 EVENT type namespace_or_type_name
1735 implicit_value_parameter_type = (Expression) $4;
1736 lexer.EventParsing = true;
1737 oob_stack.Push (lexer.Location);
1739 event_accessor_declarations
1741 lexer.EventParsing = false;
1745 Location loc = (Location) oob_stack.Pop ();
1748 Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
1751 Pair pair = (Pair) $8;
1753 MemberName name = (MemberName) $5;
1755 Event e = new EventProperty (
1756 current_class, (Expression) $4, (int) $2, false, name, null,
1757 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
1760 current_container.AddEvent (e);
1761 implicit_value_parameter_type = null;
1764 | opt_attributes opt_modifiers EVENT type namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS block {
1765 MemberName mn = (MemberName) $5;
1767 if (mn.Left != null)
1768 Report.Error (71, lexer.Location, "Explicit implementation of events requires property syntax");
1770 Report.Error (71, lexer.Location, "Event declaration should use property syntax");
1774 event_accessor_declarations
1775 : add_accessor_declaration remove_accessor_declaration
1777 $$ = new Pair ($1, $2);
1779 | remove_accessor_declaration add_accessor_declaration
1781 $$ = new Pair ($2, $1);
1783 | add_accessor_declaration { $$ = null; }
1784 | remove_accessor_declaration { $$ = null; }
1787 add_accessor_declaration
1788 : opt_attributes ADD
1790 Parameter [] args = new Parameter [1];
1791 Parameter implicit_value_parameter = new Parameter (
1792 implicit_value_parameter_type, "value",
1793 Parameter.Modifier.NONE, null);
1795 args [0] = implicit_value_parameter;
1797 current_local_parameters = new Parameters (args, null, lexer.Location);
1798 lexer.EventParsing = false;
1802 $$ = new Accessor ((Block) $4, (Attributes) $1, lexer.Location);
1803 lexer.EventParsing = true;
1805 | opt_attributes ADD error {
1806 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1811 remove_accessor_declaration
1812 : opt_attributes REMOVE
1814 Parameter [] args = new Parameter [1];
1815 Parameter implicit_value_parameter = new Parameter (
1816 implicit_value_parameter_type, "value",
1817 Parameter.Modifier.NONE, null);
1819 args [0] = implicit_value_parameter;
1821 current_local_parameters = new Parameters (args, null, lexer.Location);
1822 lexer.EventParsing = false;
1826 $$ = new Accessor ((Block) $4, (Attributes) $1, lexer.Location);
1827 lexer.EventParsing = true;
1829 | opt_attributes REMOVE error {
1830 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1836 : opt_attributes opt_modifiers indexer_declarator
1839 IndexerDeclaration decl = (IndexerDeclaration) $3;
1841 implicit_value_parameter_type = decl.type;
1843 lexer.PropertyParsing = true;
1844 parsing_indexer = true;
1846 indexer_parameters = decl.param_list;
1847 oob_stack.Push (lexer.Location);
1849 accessor_declarations
1851 lexer.PropertyParsing = false;
1852 parsing_indexer = false;
1856 // The signature is computed from the signature of the indexer. Look
1857 // at section 3.6 on the spec
1858 Location loc = (Location) oob_stack.Pop ();
1860 IndexerDeclaration decl = (IndexerDeclaration) $3;
1861 Pair pair = (Pair) $6;
1862 Accessor get_block = (Accessor) pair.First;
1863 Accessor set_block = (Accessor) pair.Second;
1866 if (decl.interface_type != null)
1867 name = new MemberName (decl.interface_type,
1868 TypeContainer.DefaultIndexerName);
1870 name = new MemberName (TypeContainer.DefaultIndexerName);
1872 indexer = new Indexer (current_class, decl.type, name,
1873 (int) $2, false, decl.param_list, (Attributes) $1,
1874 get_block, set_block, loc);
1876 // Note that there is no equivalent of CheckDef for this case
1877 // We shall handle this in semantic analysis
1879 current_container.AddIndexer (indexer);
1881 current_local_parameters = null;
1882 implicit_value_parameter_type = null;
1883 indexer_parameters = null;
1888 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1890 Parameters pars = (Parameters) $4;
1891 if (pars.HasArglist) {
1892 // "__arglist is not valid in this context"
1893 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
1894 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
1895 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1898 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
1900 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1902 Parameters pars = (Parameters) $6;
1904 if (pars.HasArglist) {
1905 // "__arglist is not valid in this context"
1906 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
1907 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
1908 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1910 MemberName name = (MemberName) $2;
1911 $$ = new IndexerDeclaration ((Expression) $1, name, pars);
1923 Location enum_location = lexer.Location;
1925 MemberName full_name = MakeName (new MemberName ((string) $4));
1926 Enum e = new Enum (current_namespace, current_container, (Expression) $5, (int) $2,
1927 full_name, (Attributes) $1, enum_location);
1929 foreach (VariableDeclaration ev in (ArrayList) $6) {
1930 e.AddEnumMember (ev.identifier,
1931 (Expression) ev.expression_or_array_initializer,
1932 ev.Location, ev.OptAttributes);
1935 string name = full_name.GetName (false);
1936 current_container.AddEnum (e);
1937 RootContext.Tree.RecordDecl (name, e);
1943 : /* empty */ { $$ = TypeManager.system_int32_expr; }
1944 | COLON type { $$ = $2; }
1948 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1954 opt_enum_member_declarations
1955 : /* empty */ { $$ = new ArrayList (4); }
1956 | enum_member_declarations opt_comma { $$ = $1; }
1959 enum_member_declarations
1960 : enum_member_declaration
1962 ArrayList l = new ArrayList (4);
1967 | enum_member_declarations COMMA enum_member_declaration
1969 ArrayList l = (ArrayList) $1;
1977 enum_member_declaration
1978 : opt_attributes IDENTIFIER
1980 $$ = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
1982 | opt_attributes IDENTIFIER
1984 $$ = lexer.Location;
1988 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
1992 delegate_declaration
1995 DELEGATE type member_name
1996 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1999 Location l = lexer.Location;
2000 MemberName name = MakeName ((MemberName) $5);
2001 Delegate del = new Delegate (current_namespace, current_container, (Expression) $4,
2002 (int) $2, name, (Parameters) $7, (Attributes) $1, l);
2004 current_container.AddDelegate (del);
2005 RootContext.Tree.RecordDecl (name.GetName (false), del);
2009 DELEGATE VOID member_name
2010 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2013 Location l = lexer.Location;
2014 MemberName name = MakeName ((MemberName) $5);
2015 Delegate del = new Delegate (
2016 current_namespace, current_container,
2017 TypeManager.system_void_expr, (int) $2, name,
2018 (Parameters) $7, (Attributes) $1, l);
2020 current_container.AddDelegate (del);
2021 RootContext.Tree.RecordDecl (name.GetName (false), del);
2025 namespace_or_type_name
2027 | namespace_or_type_name DOT IDENTIFIER {
2028 $$ = new MemberName ((MemberName) $1, (string) $3);
2034 $$ = new MemberName ((string) $1);
2039 * Before you think of adding a return_type, notice that we have been
2040 * using two rules in the places where it matters (one rule using type
2041 * and another identical one that uses VOID as the return type). This
2042 * gets rid of a shift/reduce couple
2045 : namespace_or_type_name
2047 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2059 // Note that here only unmanaged types are allowed but we
2060 // can't perform checks during this phase - we do it during
2061 // semantic analysis.
2063 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2067 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2073 | non_expression_type rank_specifier
2075 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2077 | non_expression_type STAR
2079 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2081 | expression rank_specifiers
2083 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2087 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2091 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2092 // through this different path
2094 | multiplicative_expression STAR
2096 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2103 ArrayList types = new ArrayList (4);
2108 | type_list COMMA type
2110 ArrayList types = (ArrayList) $1;
2118 * replaces all the productions for isolating the various
2119 * simple types, but we need this to reuse it easily in local_variable_type
2122 : OBJECT { $$ = TypeManager.system_object_expr; }
2123 | STRING { $$ = TypeManager.system_string_expr; }
2124 | BOOL { $$ = TypeManager.system_boolean_expr; }
2125 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2126 | FLOAT { $$ = TypeManager.system_single_expr; }
2127 | DOUBLE { $$ = TypeManager.system_double_expr; }
2132 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2133 | BYTE { $$ = TypeManager.system_byte_expr; }
2134 | SHORT { $$ = TypeManager.system_int16_expr; }
2135 | USHORT { $$ = TypeManager.system_uint16_expr; }
2136 | INT { $$ = TypeManager.system_int32_expr; }
2137 | UINT { $$ = TypeManager.system_uint32_expr; }
2138 | LONG { $$ = TypeManager.system_int64_expr; }
2139 | ULONG { $$ = TypeManager.system_uint64_expr; }
2140 | CHAR { $$ = TypeManager.system_char_expr; }
2141 | VOID { $$ = TypeManager.system_void_expr; }
2145 : type rank_specifiers
2147 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2152 // Expressions, section 7.5
2162 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2164 | parenthesized_expression
2166 | invocation_expression
2170 | post_increment_expression
2171 | post_decrement_expression
2175 | checked_expression
2176 | unchecked_expression
2177 | pointer_member_access
2178 | anonymous_method_expression
2185 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2186 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2187 | NULL { $$ = NullLiteral.Null; }
2191 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2192 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2193 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2198 object v = lexer.Value;
2204 $$ = IntLiteral.Zero;
2206 $$ = IntLiteral.One;
2208 $$ = new IntLiteral (i);
2209 } else if (v is uint)
2210 $$ = new UIntLiteral ((UInt32) v);
2212 $$ = new LongLiteral ((Int64) v);
2213 else if (v is ulong)
2214 $$ = new ULongLiteral ((UInt64) v);
2216 Console.WriteLine ("OOPS. Unexpected result from scanner");
2221 : TRUE { $$ = new BoolLiteral (true); }
2222 | FALSE { $$ = new BoolLiteral (false); }
2225 parenthesized_expression_0
2226 : OPEN_PARENS expression CLOSE_PARENS
2229 lexer.Deambiguate_CloseParens ();
2230 // After this, the next token returned is one of
2231 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2232 // or CLOSE_PARENS_MINUS.
2236 parenthesized_expression
2237 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2241 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2243 // If a parenthesized expression is followed by a minus, we need to wrap
2244 // the expression inside a ParenthesizedExpression for the CS0075 check
2245 // in Binary.DoResolve().
2246 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2251 : primary_expression DOT IDENTIFIER
2253 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2255 | predefined_type DOT IDENTIFIER
2257 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2265 invocation_expression
2266 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2269 Location l = lexer.Location;
2270 Report.Error (1, l, "Parse error");
2272 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2274 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2276 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2278 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2280 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2285 : /* empty */ { $$ = null; }
2292 ArrayList list = new ArrayList (4);
2296 | argument_list COMMA argument
2298 ArrayList list = (ArrayList) $1;
2302 | argument_list error {
2303 CheckToken (1026, yyToken, ", or ) expected");
2310 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2312 | REF variable_reference
2314 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2316 | OUT variable_reference
2318 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2320 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2322 ArrayList list = (ArrayList) $3;
2323 Argument[] args = new Argument [list.Count];
2324 list.CopyTo (args, 0);
2326 Expression expr = new Arglist (args, lexer.Location);
2327 $$ = new Argument (expr, Argument.AType.Expression);
2331 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2336 : expression { note ("section 5.4"); $$ = $1; }
2340 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2342 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2344 | primary_expression rank_specifiers
2346 // So the super-trick is that primary_expression
2347 // can only be either a SimpleName or a MemberAccess.
2348 // The MemberAccess case arises when you have a fully qualified type-name like :
2350 // SimpleName is when you have
2353 Expression expr = (Expression) $1;
2354 if (expr is ComposedCast){
2355 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2356 } else if (!(expr is SimpleName || expr is MemberAccess)){
2357 Error_ExpectingTypeName (lexer.Location, expr);
2358 $$ = TypeManager.system_object_expr;
2361 // So we extract the string corresponding to the SimpleName
2364 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2372 ArrayList list = new ArrayList (4);
2376 | expression_list COMMA expression
2378 ArrayList list = (ArrayList) $1;
2387 $$ = new This (current_block, lexer.Location);
2392 : BASE DOT IDENTIFIER
2394 $$ = new BaseAccess ((string) $3, lexer.Location);
2396 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2398 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2401 Report.Error (175, "Use of keyword `base' is not valid in this context");
2406 post_increment_expression
2407 : primary_expression OP_INC
2409 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2410 (Expression) $1, lexer.Location);
2414 post_decrement_expression
2415 : primary_expression OP_DEC
2417 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2418 (Expression) $1, lexer.Location);
2423 : object_or_delegate_creation_expression
2424 | array_creation_expression
2427 object_or_delegate_creation_expression
2428 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2430 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2434 array_creation_expression
2435 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2437 opt_array_initializer
2439 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2441 | NEW type rank_specifiers array_initializer
2443 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2447 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2463 : rank_specifier opt_rank_specifier
2465 $$ = (string) $2 + (string) $1;
2470 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2472 $$ = "[" + (string) $2 + "]";
2492 | dim_separators COMMA
2494 $$ = (string) $1 + ",";
2498 opt_array_initializer
2510 : OPEN_BRACE CLOSE_BRACE
2512 ArrayList list = new ArrayList (4);
2515 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2517 $$ = (ArrayList) $2;
2521 variable_initializer_list
2522 : variable_initializer
2524 ArrayList list = new ArrayList (4);
2528 | variable_initializer_list COMMA variable_initializer
2530 ArrayList list = (ArrayList) $1;
2537 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2539 $$ = new TypeOfVoid (lexer.Location);
2541 | TYPEOF OPEN_PARENS type CLOSE_PARENS
2543 $$ = new TypeOf ((Expression) $3, lexer.Location);
2548 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2549 $$ = new SizeOf ((Expression) $3, lexer.Location);
2554 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2556 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2560 unchecked_expression
2561 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2563 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2567 pointer_member_access
2568 : primary_expression OP_PTR IDENTIFIER
2572 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2573 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2577 anonymous_method_expression
2578 : DELEGATE opt_anonymous_method_signature {
2579 oob_stack.Push (current_local_parameters);
2580 current_local_parameters = (Parameters)$2;
2581 create_toplevel_block = true;
2584 Report.Error (-213, lexer.Location, "Anonymous methods are not supported in this branch");
2587 create_toplevel_block = false;
2588 if (RootContext.Version == LanguageVersion.ISO_1){
2589 Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
2592 $$ = new AnonymousMethod ((Parameters) $2, (Block) $4, lexer.Location);
2593 current_local_parameters = (Parameters) oob_stack.Pop ();
2598 opt_anonymous_method_signature
2599 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
2600 | anonymous_method_signature
2603 anonymous_method_signature
2604 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
2607 $$ = Parameters.EmptyReadOnlyParameters;
2609 ArrayList par_list = (ArrayList) $2;
2610 Parameter [] pars = new Parameter [par_list.Count];
2611 par_list.CopyTo (pars);
2612 $$ = new Parameters (pars, null, lexer.Location);
2617 opt_anonymous_method_parameter_list
2618 : /* empty */ { $$ = null; }
2619 | anonymous_method_parameter_list { $$ = $1; }
2622 anonymous_method_parameter_list
2623 : anonymous_method_parameter
2625 ArrayList a = new ArrayList (4);
2629 | anonymous_method_parameter_list COMMA anonymous_method_parameter
2631 ArrayList a = (ArrayList) $1;
2637 anonymous_method_parameter
2638 : opt_parameter_modifier type IDENTIFIER {
2639 $$ = new Parameter ((Expression) $2, (string) $2, (Parameter.Modifier) $1, null);
2644 : primary_expression
2645 | BANG prefixed_unary_expression
2647 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2649 | TILDE prefixed_unary_expression
2651 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2657 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
2659 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2661 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
2663 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2669 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2671 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2676 // The idea to split this out is from Rhys' grammar
2677 // to solve the problem with casts.
2679 prefixed_unary_expression
2681 | PLUS prefixed_unary_expression
2683 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2685 | MINUS prefixed_unary_expression
2687 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2689 | OP_INC prefixed_unary_expression
2691 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2692 (Expression) $2, lexer.Location);
2694 | OP_DEC prefixed_unary_expression
2696 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2697 (Expression) $2, lexer.Location);
2699 | STAR prefixed_unary_expression
2701 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2703 | BITWISE_AND prefixed_unary_expression
2705 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2709 pre_increment_expression
2710 : OP_INC prefixed_unary_expression
2712 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2713 (Expression) $2, lexer.Location);
2717 pre_decrement_expression
2718 : OP_DEC prefixed_unary_expression
2720 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2721 (Expression) $2, lexer.Location);
2725 multiplicative_expression
2726 : prefixed_unary_expression
2727 | multiplicative_expression STAR prefixed_unary_expression
2729 $$ = new Binary (Binary.Operator.Multiply,
2730 (Expression) $1, (Expression) $3, lexer.Location);
2732 | multiplicative_expression DIV prefixed_unary_expression
2734 $$ = new Binary (Binary.Operator.Division,
2735 (Expression) $1, (Expression) $3, lexer.Location);
2737 | multiplicative_expression PERCENT prefixed_unary_expression
2739 $$ = new Binary (Binary.Operator.Modulus,
2740 (Expression) $1, (Expression) $3, lexer.Location);
2745 : multiplicative_expression
2746 | additive_expression PLUS multiplicative_expression
2748 $$ = new Binary (Binary.Operator.Addition,
2749 (Expression) $1, (Expression) $3, lexer.Location);
2751 | additive_expression MINUS multiplicative_expression
2753 $$ = new Binary (Binary.Operator.Subtraction,
2754 (Expression) $1, (Expression) $3, lexer.Location);
2759 : additive_expression
2760 | shift_expression OP_SHIFT_LEFT additive_expression
2762 $$ = new Binary (Binary.Operator.LeftShift,
2763 (Expression) $1, (Expression) $3, lexer.Location);
2765 | shift_expression OP_SHIFT_RIGHT additive_expression
2767 $$ = new Binary (Binary.Operator.RightShift,
2768 (Expression) $1, (Expression) $3, lexer.Location);
2772 relational_expression
2774 | relational_expression OP_LT shift_expression
2776 $$ = new Binary (Binary.Operator.LessThan,
2777 (Expression) $1, (Expression) $3, lexer.Location);
2779 | relational_expression OP_GT shift_expression
2781 $$ = new Binary (Binary.Operator.GreaterThan,
2782 (Expression) $1, (Expression) $3, lexer.Location);
2784 | relational_expression OP_LE shift_expression
2786 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2787 (Expression) $1, (Expression) $3, lexer.Location);
2789 | relational_expression OP_GE shift_expression
2791 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2792 (Expression) $1, (Expression) $3, lexer.Location);
2794 | relational_expression IS type
2796 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
2798 | relational_expression AS type
2800 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
2805 : relational_expression
2806 | equality_expression OP_EQ relational_expression
2808 $$ = new Binary (Binary.Operator.Equality,
2809 (Expression) $1, (Expression) $3, lexer.Location);
2811 | equality_expression OP_NE relational_expression
2813 $$ = new Binary (Binary.Operator.Inequality,
2814 (Expression) $1, (Expression) $3, lexer.Location);
2819 : equality_expression
2820 | and_expression BITWISE_AND equality_expression
2822 $$ = new Binary (Binary.Operator.BitwiseAnd,
2823 (Expression) $1, (Expression) $3, lexer.Location);
2827 exclusive_or_expression
2829 | exclusive_or_expression CARRET and_expression
2831 $$ = new Binary (Binary.Operator.ExclusiveOr,
2832 (Expression) $1, (Expression) $3, lexer.Location);
2836 inclusive_or_expression
2837 : exclusive_or_expression
2838 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2840 $$ = new Binary (Binary.Operator.BitwiseOr,
2841 (Expression) $1, (Expression) $3, lexer.Location);
2845 conditional_and_expression
2846 : inclusive_or_expression
2847 | conditional_and_expression OP_AND inclusive_or_expression
2849 $$ = new Binary (Binary.Operator.LogicalAnd,
2850 (Expression) $1, (Expression) $3, lexer.Location);
2854 conditional_or_expression
2855 : conditional_and_expression
2856 | conditional_or_expression OP_OR conditional_and_expression
2858 $$ = new Binary (Binary.Operator.LogicalOr,
2859 (Expression) $1, (Expression) $3, lexer.Location);
2863 conditional_expression
2864 : conditional_or_expression
2865 | conditional_or_expression INTERR expression COLON expression
2867 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2871 assignment_expression
2872 : prefixed_unary_expression ASSIGN expression
2874 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2876 | prefixed_unary_expression OP_MULT_ASSIGN expression
2878 Location l = lexer.Location;
2880 $$ = new CompoundAssign (
2881 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
2883 | prefixed_unary_expression OP_DIV_ASSIGN expression
2885 Location l = lexer.Location;
2887 $$ = new CompoundAssign (
2888 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
2890 | prefixed_unary_expression OP_MOD_ASSIGN expression
2892 Location l = lexer.Location;
2894 $$ = new CompoundAssign (
2895 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
2897 | prefixed_unary_expression OP_ADD_ASSIGN expression
2899 Location l = lexer.Location;
2901 $$ = new CompoundAssign (
2902 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
2904 | prefixed_unary_expression OP_SUB_ASSIGN expression
2906 Location l = lexer.Location;
2908 $$ = new CompoundAssign (
2909 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
2911 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2913 Location l = lexer.Location;
2915 $$ = new CompoundAssign (
2916 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
2918 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2920 Location l = lexer.Location;
2922 $$ = new CompoundAssign (
2923 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
2925 | prefixed_unary_expression OP_AND_ASSIGN expression
2927 Location l = lexer.Location;
2929 $$ = new CompoundAssign (
2930 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
2932 | prefixed_unary_expression OP_OR_ASSIGN expression
2934 Location l = lexer.Location;
2936 $$ = new CompoundAssign (
2937 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
2939 | prefixed_unary_expression OP_XOR_ASSIGN expression
2941 Location l = lexer.Location;
2943 $$ = new CompoundAssign (
2944 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
2949 : conditional_expression
2950 | assignment_expression
2970 MemberName name = MakeName ((MemberName) $5);
2971 bool partial = (bool) $3;
2972 int mod_flags = (int) $2;
2975 ClassPart part = PartialContainer.CreatePart (
2976 current_namespace, current_container, name, mod_flags,
2977 (Attributes) $1, Kind.Class, lexer.Location);
2979 current_container = part.PartialContainer;
2980 current_class = part;
2982 if ((mod_flags & Modifiers.STATIC) != 0) {
2983 current_class = new StaticClass (current_namespace, current_container, name, mod_flags,
2984 (Attributes) $1, lexer.Location);
2986 current_class = new Class (current_namespace, current_container, name, mod_flags,
2987 (Attributes) $1, lexer.Location);
2989 current_container = current_class;
2990 RootContext.Tree.RecordDecl (name.GetName (false), current_class);
2996 if (current_class.Name == "System.Object") {
2997 Report.Error (537, current_class.Location,
2998 "The class System.Object cannot have a base " +
2999 "class or implement an interface.");
3001 current_class.Bases = (ArrayList) $7;
3004 current_class.Register ();
3011 current_container = current_container.Parent;
3012 current_class = current_container;
3018 { $$ = (bool) false; }
3020 { $$ = (bool) true; }
3024 : /* empty */ { $$ = (int) 0; }
3030 | modifiers modifier
3035 if ((m1 & m2) != 0) {
3036 Location l = lexer.Location;
3037 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3039 $$ = (int) (m1 | m2);
3044 : NEW { $$ = Modifiers.NEW; }
3045 | PUBLIC { $$ = Modifiers.PUBLIC; }
3046 | PROTECTED { $$ = Modifiers.PROTECTED; }
3047 | INTERNAL { $$ = Modifiers.INTERNAL; }
3048 | PRIVATE { $$ = Modifiers.PRIVATE; }
3049 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3050 | SEALED { $$ = Modifiers.SEALED; }
3051 | STATIC { $$ = Modifiers.STATIC; }
3052 | READONLY { $$ = Modifiers.READONLY; }
3053 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3054 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3055 | EXTERN { $$ = Modifiers.EXTERN; }
3056 | VOLATILE { $$ = Modifiers.VOLATILE; }
3057 | UNSAFE { $$ = Modifiers.UNSAFE; }
3061 : /* empty */ { $$ = null; }
3062 | class_base { $$ = $1; }
3066 : COLON type_list { $$ = $2; }
3074 // A block is "contained" on the following places:
3076 // property_declaration as part of the accessor body (get/set)
3077 // operator_declaration
3078 // constructor_declaration
3079 // destructor_declaration
3080 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3085 if (current_block == null || create_toplevel_block){
3086 current_block = new ToplevelBlock (current_local_parameters, lexer.Location);
3088 current_block = new Block (current_block, current_local_parameters,
3089 lexer.Location, Location.Null);
3092 opt_statement_list CLOSE_BRACE
3094 while (current_block.Implicit)
3095 current_block = current_block.Parent;
3097 current_block.SetEndLocation (lexer.Location);
3098 current_block = current_block.Parent;
3109 | statement_list statement
3113 : declaration_statement
3115 if ($1 != null && (Block) $1 != current_block){
3116 current_block.AddStatement ((Statement) $1);
3117 current_block = (Block) $1;
3120 | valid_declaration_statement
3122 current_block.AddStatement ((Statement) $1);
3127 valid_declaration_statement
3130 | expression_statement
3131 | selection_statement
3132 | iteration_statement
3136 | unchecked_statement
3144 : valid_declaration_statement
3145 | declaration_statement
3147 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3152 Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3160 $$ = EmptyStatement.Value;
3167 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3169 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3170 current_block.AddStatement (labeled);
3175 declaration_statement
3176 : local_variable_declaration SEMICOLON
3179 DictionaryEntry de = (DictionaryEntry) $1;
3181 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3185 | local_constant_declaration SEMICOLON
3188 DictionaryEntry de = (DictionaryEntry) $1;
3190 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3196 * The following is from Rhys' grammar:
3197 * > Types in local variable declarations must be recognized as
3198 * > expressions to prevent reduce/reduce errors in the grammar.
3199 * > The expressions are converted into types during semantic analysis.
3202 : primary_expression opt_rank_specifier
3204 // FIXME: Do something smart here regarding the composition of the type.
3206 // Ok, the above "primary_expression" is there to get rid of
3207 // both reduce/reduce and shift/reduces in the grammar, it should
3208 // really just be "type_name". If you use type_name, a reduce/reduce
3209 // creeps up. If you use namespace_or_type_name (which is all we need
3210 // really) two shift/reduces appear.
3213 // So the super-trick is that primary_expression
3214 // can only be either a SimpleName or a MemberAccess.
3215 // The MemberAccess case arises when you have a fully qualified type-name like :
3217 // SimpleName is when you have
3220 Expression expr = (Expression) $1;
3221 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3222 Error_ExpectingTypeName (lexer.Location, expr);
3226 // So we extract the string corresponding to the SimpleName
3230 if ((string) $2 == "")
3233 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3236 | builtin_types opt_rank_specifier
3238 if ((string) $2 == "")
3241 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3245 local_variable_pointer_type
3246 : primary_expression STAR
3248 Expression expr = (Expression) $1;
3249 Location l = lexer.Location;
3251 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3252 Error_ExpectingTypeName (l, expr);
3256 $$ = new ComposedCast ((Expression) $1, "*", l);
3258 | builtin_types STAR
3260 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3264 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3266 | local_variable_pointer_type STAR
3268 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3272 local_variable_declaration
3273 : local_variable_type variable_declarators
3276 $$ = new DictionaryEntry ($1, $2);
3280 | local_variable_pointer_type opt_rank_specifier variable_declarators
3285 if ((string) $2 == "")
3286 t = (Expression) $1;
3288 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3289 $$ = new DictionaryEntry (t, $3);
3295 local_constant_declaration
3296 : CONST local_variable_type constant_declarators
3299 $$ = new DictionaryEntry ($2, $3);
3305 expression_statement
3306 : statement_expression SEMICOLON
3313 // We have to do the wrapping here and not in the case above,
3314 // because statement_expression is used for example in for_statement
3316 statement_expression
3317 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3318 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3319 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3320 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3321 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3322 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3323 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3325 Report.Error (1002, lexer.Location, "Expecting `;'");
3330 object_creation_expression
3331 : object_or_delegate_creation_expression
3332 { note ("complain if this is a delegate maybe?"); }
3341 : if_statement_open if_statement_rest
3350 oob_stack.Push (lexer.Location);
3355 : boolean_expression CLOSE_PARENS
3358 Location l = (Location) oob_stack.Pop ();
3360 $$ = new If ((Expression) $1, (Statement) $3, l);
3362 if (RootContext.WarningLevel >= 3){
3363 if ($3 == EmptyStatement.Value)
3364 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3368 | boolean_expression CLOSE_PARENS
3369 embedded_statement ELSE embedded_statement
3371 Location l = (Location) oob_stack.Pop ();
3373 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
3378 : SWITCH OPEN_PARENS
3380 oob_stack.Push (lexer.Location);
3381 switch_stack.Push (current_block);
3383 expression CLOSE_PARENS
3386 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
3387 current_block = (Block) switch_stack.Pop ();
3403 Report.Error (1522, lexer.Location, "Empty switch block");
3411 ArrayList sections = new ArrayList (4);
3416 | switch_sections switch_section
3418 ArrayList sections = (ArrayList) $1;
3428 current_block = current_block.CreateSwitchBlock (lexer.Location);
3432 Block topmost = current_block;
3434 while (topmost.Implicit)
3435 topmost = topmost.Parent;
3436 $$ = new SwitchSection ((ArrayList) $1, topmost);
3443 ArrayList labels = new ArrayList (4);
3448 | switch_labels switch_label
3450 ArrayList labels = (ArrayList) ($1);
3458 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3459 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3462 1523, lexer.Location,
3463 "The keyword case or default must precede code in switch block");
3477 oob_stack.Push (lexer.Location);
3479 boolean_expression CLOSE_PARENS embedded_statement
3481 Location l = (Location) oob_stack.Pop ();
3482 $$ = new While ((Expression) $4, (Statement) $6, l);
3484 if (RootContext.WarningLevel >= 3){
3485 if ($6 == EmptyStatement.Value)
3486 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3492 : DO embedded_statement
3494 oob_stack.Push (lexer.Location);
3496 boolean_expression CLOSE_PARENS SEMICOLON
3498 Location l = (Location) oob_stack.Pop ();
3500 $$ = new Do ((Statement) $2, (Expression) $6, l);
3506 opt_for_initializer SEMICOLON
3508 Block assign_block = new Block (current_block);
3509 current_block = assign_block;
3511 if ($3 is DictionaryEntry){
3512 DictionaryEntry de = (DictionaryEntry) $3;
3514 Expression type = (Expression) de.Key;
3515 ArrayList var_declarators = (ArrayList) de.Value;
3517 foreach (VariableDeclaration decl in var_declarators){
3521 vi = current_block.AddVariable (
3522 type, decl.identifier, current_local_parameters, decl.Location);
3526 Location l = lexer.Location;
3528 if (decl.expression_or_array_initializer is Expression){
3529 expr = (Expression) decl.expression_or_array_initializer;
3530 } else if (decl.expression_or_array_initializer == null) {
3533 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3534 expr = new ArrayCreation (type, "", init, decl.Location);
3537 LocalVariableReference var;
3538 var = new LocalVariableReference (assign_block, decl.identifier, l);
3541 Assign a = new Assign (var, expr, decl.Location);
3543 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3549 oob_stack.Push (lexer.Location);
3551 opt_for_condition SEMICOLON
3552 opt_for_iterator CLOSE_PARENS
3555 Location l = (Location) oob_stack.Pop ();
3557 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3559 if (RootContext.WarningLevel >= 3){
3560 if ($10 == EmptyStatement.Value)
3561 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3564 current_block.AddStatement (f);
3565 while (current_block.Implicit)
3566 current_block = current_block.Parent;
3568 current_block = current_block.Parent;
3573 : /* empty */ { $$ = EmptyStatement.Value; }
3578 : local_variable_declaration
3579 | statement_expression_list
3583 : /* empty */ { $$ = null; }
3584 | boolean_expression
3588 : /* empty */ { $$ = EmptyStatement.Value; }
3593 : statement_expression_list
3596 statement_expression_list
3597 : statement_expression
3599 // CHANGE: was `null'
3600 Block b = new Block (current_block, Block.Flags.Implicit);
3602 b.AddStatement ((Statement) $1);
3605 | statement_expression_list COMMA statement_expression
3607 Block b = (Block) $1;
3609 b.AddStatement ((Statement) $3);
3615 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
3617 Report.Error (230, lexer.Location, "Type and identifier are both required in a foreach statement");
3620 | FOREACH OPEN_PARENS type IDENTIFIER IN
3622 oob_stack.Push (lexer.Location);
3624 expression CLOSE_PARENS
3626 oob_stack.Push (current_block);
3628 Block foreach_block = new Block (current_block);
3629 LocalVariableReference v = null;
3630 Location l = lexer.Location;
3633 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3637 // Get a writable reference to this read-only variable.
3638 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3640 current_block = foreach_block;
3643 oob_stack.Push (current_block);
3647 Block foreach_block = (Block) oob_stack.Pop ();
3648 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3649 Block prev_block = (Block) oob_stack.Pop ();
3650 Location l = (Location) oob_stack.Pop ();
3652 current_block = prev_block;
3655 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3656 foreach_block.AddStatement (f);
3665 | continue_statement
3675 $$ = new Break (lexer.Location);
3680 : CONTINUE SEMICOLON
3682 $$ = new Continue (lexer.Location);
3687 : GOTO IDENTIFIER SEMICOLON
3689 $$ = new Goto (current_block, (string) $2, lexer.Location);
3691 | GOTO CASE constant_expression SEMICOLON
3693 $$ = new GotoCase ((Expression) $3, lexer.Location);
3695 | GOTO DEFAULT SEMICOLON
3697 $$ = new GotoDefault (lexer.Location);
3702 : RETURN opt_expression SEMICOLON
3704 $$ = new Return ((Expression) $2, lexer.Location);
3709 : THROW opt_expression SEMICOLON
3711 $$ = new Throw ((Expression) $2, lexer.Location);
3716 : IDENTIFIER RETURN expression SEMICOLON
3718 string s = (string) $1;
3720 Report.Error (1003, lexer.Location, "; expected");
3723 if (RootContext.Version == LanguageVersion.ISO_1){
3724 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
3727 if (iterator_container == null){
3728 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3731 iterator_container.SetYields ();
3732 $$ = new Yield ((Expression) $3, lexer.Location);
3735 | IDENTIFIER BREAK SEMICOLON
3737 string s = (string) $1;
3739 Report.Error (1003, lexer.Location, "; expected");
3742 if (RootContext.Version == LanguageVersion.ISO_1){
3743 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
3746 if (iterator_container == null){
3747 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3750 iterator_container.SetYields ();
3751 $$ = new YieldBreak (lexer.Location);
3762 : TRY block catch_clauses
3765 ArrayList s = new ArrayList (4);
3767 foreach (Catch cc in (ArrayList) $3) {
3774 // Now s contains the list of specific catch clauses
3775 // and g contains the general one.
3777 $$ = new Try ((Block) $2, s, g, null, lexer.Location);
3779 | TRY block opt_catch_clauses FINALLY block
3782 ArrayList s = new ArrayList (4);
3783 ArrayList catch_list = (ArrayList) $3;
3785 if (catch_list != null){
3786 foreach (Catch cc in catch_list) {
3794 $$ = new Try ((Block) $2, s, g, (Block) $5, lexer.Location);
3798 Report.Error (1524, lexer.Location, "Expected catch or finally");
3803 : /* empty */ { $$ = null; }
3810 ArrayList l = new ArrayList (4);
3815 | catch_clauses catch_clause
3817 ArrayList l = (ArrayList) $1;
3825 : /* empty */ { $$ = null; }
3830 : CATCH opt_catch_args
3832 Expression type = null;
3836 DictionaryEntry cc = (DictionaryEntry) $2;
3837 type = (Expression) cc.Key;
3838 id = (string) cc.Value;
3841 ArrayList one = new ArrayList (4);
3842 Location loc = lexer.Location;
3844 one.Add (new VariableDeclaration (id, null, loc));
3847 current_block = new Block (current_block);
3848 Block b = declare_local_variables (type, one, loc);
3853 Expression type = null;
3857 DictionaryEntry cc = (DictionaryEntry) $2;
3858 type = (Expression) cc.Key;
3859 id = (string) cc.Value;
3863 // FIXME: I can change this for an assignment.
3865 while (current_block != (Block) $1)
3866 current_block = current_block.Parent;
3871 $$ = new Catch (type, id , (Block) $4, lexer.Location);
3876 : /* empty */ { $$ = null; }
3881 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3883 $$ = new DictionaryEntry ($2, $3);
3890 $$ = new Checked ((Block) $2);
3897 $$ = new Unchecked ((Block) $2);
3904 if (!RootContext.Unsafe){
3905 Report.Error (227, lexer.Location,
3906 "Unsafe code can only be used if --unsafe is used");
3909 $$ = new Unsafe ((Block) $3);
3915 type fixed_pointer_declarators
3918 ArrayList list = (ArrayList) $4;
3919 Expression type = (Expression) $3;
3920 Location l = lexer.Location;
3921 int top = list.Count;
3923 Block assign_block = new Block (current_block);
3924 current_block = assign_block;
3926 for (int i = 0; i < top; i++){
3927 Pair p = (Pair) list [i];
3930 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3944 Location l = (Location) oob_stack.Pop ();
3946 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
3948 if (RootContext.WarningLevel >= 3){
3949 if ($7 == EmptyStatement.Value)
3950 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3953 current_block.AddStatement (f);
3954 while (current_block.Implicit)
3955 current_block = current_block.Parent;
3957 current_block = current_block.Parent;
3961 fixed_pointer_declarators
3962 : fixed_pointer_declarator {
3963 ArrayList declarators = new ArrayList (4);
3965 declarators.Add ($1);
3968 | fixed_pointer_declarators COMMA fixed_pointer_declarator
3970 ArrayList declarators = (ArrayList) $1;
3972 declarators.Add ($3);
3977 fixed_pointer_declarator
3978 : IDENTIFIER ASSIGN expression
3980 $$ = new Pair ($1, $3);
3984 Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
3990 : LOCK OPEN_PARENS expression CLOSE_PARENS
3996 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4001 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4003 Block assign_block = new Block (current_block);
4004 current_block = assign_block;
4006 oob_stack.Push (lexer.Location);
4008 if ($3 is DictionaryEntry){
4009 DictionaryEntry de = (DictionaryEntry) $3;
4010 Location l = lexer.Location;
4012 Expression type = (Expression) de.Key;
4013 ArrayList var_declarators = (ArrayList) de.Value;
4015 ArrayList vars = new ArrayList (4);
4017 foreach (VariableDeclaration decl in var_declarators){
4019 LocalInfo vi = current_block.AddVariable (
4020 type, decl.identifier,
4021 current_local_parameters, decl.Location);
4027 if (decl.expression_or_array_initializer is Expression){
4028 expr = (Expression) decl.expression_or_array_initializer;
4030 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4032 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4035 expr = new ArrayCreation (type, "", init, decl.Location);
4038 LocalVariableReference var;
4040 // Get a writable reference to this read-only variable.
4041 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4043 // This is so that it is not a warning on using variables
4046 vars.Add (new DictionaryEntry (var, expr));
4048 // Assign a = new Assign (var, expr, decl.Location);
4049 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4051 $3 = new DictionaryEntry (type, vars);
4056 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
4057 current_block.AddStatement (u);
4058 while (current_block.Implicit)
4059 current_block = current_block.Parent;
4061 current_block = current_block.Parent;
4065 resource_acquisition
4066 : local_variable_declaration
4073 // A class used to pass around variable declarations and constants
4075 public class VariableDeclaration {
4076 public string identifier;
4077 public object expression_or_array_initializer;
4078 public Location Location;
4079 public Attributes OptAttributes;
4081 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4083 this.identifier = id;
4084 this.expression_or_array_initializer = eoai;
4086 this.OptAttributes = opt_attrs;
4089 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4095 /// Used to pass around interface property information
4097 public class InterfaceAccessorInfo {
4099 public readonly Accessor Get, Set;
4101 public InterfaceAccessorInfo (bool has_get, bool has_set,
4102 Attributes get_attrs, Attributes set_attrs, Location get_loc, Location set_loc)
4105 Get = new Accessor (null, get_attrs, get_loc);
4107 Set = new Accessor (null, set_attrs, set_loc);
4113 // A class used to hold info about an indexer declarator
4115 public class IndexerDeclaration {
4116 public Expression type;
4117 public MemberName interface_type;
4118 public Parameters param_list;
4120 public IndexerDeclaration (Expression type, MemberName interface_type,
4121 Parameters param_list)
4124 this.interface_type = interface_type;
4125 this.param_list = param_list;
4130 // We use this when we do not have an object in advance that is an IIteratorContainer
4132 public class SimpleIteratorContainer : IIteratorContainer {
4135 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4140 public static SimpleIteratorContainer GetSimple () {
4141 Simple.Yields = false;
4145 public void SetYields () { Yields = true; }
4149 // A class used to hold info about an operator declarator
4151 public class OperatorDeclaration {
4152 public Operator.OpType optype;
4153 public Expression ret_type, arg1type, arg2type;
4154 public string arg1name, arg2name;
4155 public Location location;
4157 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4158 Expression arg1type, string arg1name,
4159 Expression arg2type, string arg2name, Location location)
4162 this.ret_type = ret_type;
4163 this.arg1type = arg1type;
4164 this.arg1name = arg1name;
4165 this.arg2type = arg2type;
4166 this.arg2name = arg2name;
4167 this.location = location;
4172 void Error_ExpectingTypeName (Location l, Expression expr)
4174 if (expr is Invocation){
4175 Report.Error (1002, l, "; expected");
4177 Report.Error (-1, l, "Invalid Type definition");
4182 // Given the @class_name name, it creates a fully qualified name
4183 // based on the containing declaration space
4186 MakeName (MemberName class_name)
4188 string ns = current_namespace.FullName;
4190 if (current_container.Name == ""){
4192 return new MemberName (new MemberName (ns), class_name);
4196 return new MemberName (current_container.MemberName, class_name);
4200 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4202 Block implicit_block;
4203 ArrayList inits = null;
4206 // We use the `Used' property to check whether statements
4207 // have been added to the current block. If so, we need
4208 // to create another block to contain the new declaration
4209 // otherwise, as an optimization, we use the same block to
4210 // add the declaration.
4212 // FIXME: A further optimization is to check if the statements
4213 // that were added were added as part of the initialization
4214 // below. In which case, no other statements have been executed
4215 // and we might be able to reduce the number of blocks for
4216 // situations like this:
4218 // int j = 1; int k = j + 1;
4220 if (current_block.Used)
4221 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4223 implicit_block = current_block;
4225 foreach (VariableDeclaration decl in variable_declarators){
4227 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4228 if (decl.expression_or_array_initializer != null){
4230 inits = new ArrayList (4);
4237 return implicit_block;
4239 foreach (VariableDeclaration decl in inits){
4243 if (decl.expression_or_array_initializer is Expression){
4244 expr = (Expression) decl.expression_or_array_initializer;
4247 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4249 expr = new ArrayCreation (type, "", init, decl.Location);
4252 LocalVariableReference var;
4253 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4255 assign = new Assign (var, expr, decl.Location);
4257 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4260 return implicit_block;
4263 Block declare_local_constants (Expression type, ArrayList declarators)
4265 Block implicit_block;
4267 if (current_block.Used)
4268 implicit_block = new Block (current_block, Block.Flags.Implicit);
4270 implicit_block = current_block;
4272 foreach (VariableDeclaration decl in declarators){
4273 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4274 current_local_parameters, decl.Location);
4277 return implicit_block;
4280 void CheckAttributeTarget (string a)
4284 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4288 Location l = lexer.Location;
4289 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4295 void CheckUnaryOperator (Operator.OpType op)
4299 case Operator.OpType.LogicalNot:
4300 case Operator.OpType.OnesComplement:
4301 case Operator.OpType.Increment:
4302 case Operator.OpType.Decrement:
4303 case Operator.OpType.True:
4304 case Operator.OpType.False:
4305 case Operator.OpType.Addition:
4306 case Operator.OpType.Subtraction:
4311 Location l = lexer.Location;
4312 Report.Error (1019, l, "Overloadable unary operator expected");
4318 void CheckBinaryOperator (Operator.OpType op)
4322 case Operator.OpType.Addition:
4323 case Operator.OpType.Subtraction:
4324 case Operator.OpType.Multiply:
4325 case Operator.OpType.Division:
4326 case Operator.OpType.Modulus:
4327 case Operator.OpType.BitwiseAnd:
4328 case Operator.OpType.BitwiseOr:
4329 case Operator.OpType.ExclusiveOr:
4330 case Operator.OpType.LeftShift:
4331 case Operator.OpType.RightShift:
4332 case Operator.OpType.Equality:
4333 case Operator.OpType.Inequality:
4334 case Operator.OpType.GreaterThan:
4335 case Operator.OpType.LessThan:
4336 case Operator.OpType.GreaterThanOrEqual:
4337 case Operator.OpType.LessThanOrEqual:
4341 Location l = lexer.Location;
4342 Report.Error (1020, l, "Overloadable binary operator expected");
4348 void syntax_error (Location l, string msg)
4350 Report.Error (1003, l, "Syntax error, " + msg);
4353 void output (string s)
4355 Console.WriteLine (s);
4358 void note (string s)
4360 // Used to put annotations
4365 public Tokenizer Lexer {
4371 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4373 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4374 this.name = file.Name;
4376 current_container = RootContext.Tree.Types;
4377 current_container.NamespaceEntry = current_namespace;
4378 oob_stack = new Stack ();
4379 switch_stack = new Stack ();
4381 lexer = new Tokenizer (reader, file, defines);
4384 public override void parse ()
4387 if (yacc_verbose_flag)
4388 yyparse (lexer, new yydebug.yyDebugSimple ());
4391 Tokenizer tokenizer = lexer as Tokenizer;
4392 tokenizer.cleanup ();
4393 } catch (Exception e){
4394 // Please do not remove this, it is used during debugging
4397 Console.WriteLine (e);
4398 Report.Error (-25, lexer.Location, "Parsing error");
4399 if (Driver.parser_verbose)
4400 Console.WriteLine (e);
4404 void CheckToken (int error, int yyToken, string msg)
4406 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4407 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4410 Report.Error (error, lexer.Location, msg);
4413 void CheckIdentifierToken (int yyToken)
4415 CheckToken (1041, yyToken, "Identifier expected");