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;
32 public class CSharpParser {
33 NamespaceEntry current_namespace;
34 TypeContainer current_container;
35 TypeContainer current_class;
37 IIteratorContainer iterator_container;
40 /// Current block is used to add statements as we find
46 /// If true, creates a toplevel block in the block production
47 /// This is flagged by the delegate creation
49 bool create_toplevel_block;
52 /// This is used by the unary_expression code to resolve
53 /// a name against a parameter.
55 Parameters current_local_parameters;
58 /// Using during property parsing to describe the implicit
59 /// value parameter that is passed to the "set" and "get"accesor
60 /// methods (properties and indexers).
62 Expression implicit_value_parameter_type;
63 Parameters indexer_parameters;
66 /// Used to determine if we are parsing the get/set pair
67 /// of an indexer or a property
72 /// An out-of-band stack.
81 public bool yacc_verbose_flag;
83 // Name of the file we are parsing
92 /// Current attribute target
93 string current_attr_target;
95 /// assembly and module attribute definition is enabled
96 bool global_attrs_enabled = true;
100 %token NONE /* This token is never returned by our lexer */
101 %token ERROR // This is used not by the parser, but by the tokenizer.
105 *These are the C# keywords
191 /* C# keywords which are not really keywords */
197 /* C# single character operators/punctuation. */
198 %token OPEN_BRACE "{"
199 %token CLOSE_BRACE "}"
200 %token OPEN_BRACKET "["
201 %token CLOSE_BRACKET "]"
202 %token OPEN_PARENS "("
203 %token CLOSE_PARENS ")"
216 %token BITWISE_AND "&"
217 %token BITWISE_OR "|"
224 /* C# multi-character operators. */
227 %token OP_SHIFT_LEFT "<<"
228 %token OP_SHIFT_RIGHT ">>"
235 %token OP_MULT_ASSIGN "*="
236 %token OP_DIV_ASSIGN "/="
237 %token OP_MOD_ASSIGN "%="
238 %token OP_ADD_ASSIGN "+="
239 %token OP_SUB_ASSIGN "-="
240 %token OP_SHIFT_LEFT_ASSIGN "<<="
241 %token OP_SHIFT_RIGHT_ASSIGN ">>="
242 %token OP_AND_ASSIGN "&="
243 %token OP_XOR_ASSIGN "^="
244 %token OP_OR_ASSIGN "|="
248 %token LITERAL_INTEGER "int literal"
249 %token LITERAL_FLOAT "float literal"
250 %token LITERAL_DOUBLE "double literal"
251 %token LITERAL_DECIMAL "decimal literal"
252 %token LITERAL_CHARACTER "character literal"
253 %token LITERAL_STRING "string literal"
256 %token CLOSE_PARENS_CAST
257 %token CLOSE_PARENS_NO_CAST
258 %token CLOSE_PARENS_OPEN_PARENS
259 %token CLOSE_PARENS_MINUS
261 /* Add precedence rules to solve dangling else s/r conflict */
270 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
272 %left STAR DIV PERCENT
273 %right BANG CARRET UMINUS
274 %nonassoc OP_INC OP_DEC
276 %left OPEN_BRACKET OPEN_BRACE
280 %start compilation_unit
284 : outer_declarations opt_EOF
285 | outer_declarations global_attributes opt_EOF
286 | global_attributes opt_EOF
287 | opt_EOF /* allow empty files */
297 | outer_declarations outer_declaration
302 | namespace_member_declaration
307 | using_directives using_directive
311 : using_alias_directive
312 | using_namespace_directive
315 using_alias_directive
316 : USING IDENTIFIER ASSIGN
317 namespace_or_type_name SEMICOLON
319 current_namespace.UsingAlias ((string) $2, (MemberName) $4, lexer.Location);
322 CheckIdentifierToken (yyToken);
326 using_namespace_directive
327 : USING namespace_name SEMICOLON
329 current_namespace.Using ((string) $2, lexer.Location);
334 // Strictly speaking, namespaces don't have attributes but
335 // we parse global attributes along with namespace declarations and then
338 namespace_declaration
339 : opt_attributes NAMESPACE namespace_or_type_name
342 Report.Error(1518, Lexer.Location, "Attributes cannot be applied to namespaces."
343 + " Expected class, delegate, enum, interface, or struct");
346 MemberName name = (MemberName) $3;
348 if ((current_namespace.Parent != null) && (name.Left != null)) {
349 Report.Error (134, lexer.Location,
350 "Cannot use qualified namespace names in nested " +
351 "namespace declarations");
354 current_namespace = new NamespaceEntry (
355 current_namespace, file, name.GetName (), lexer.Location);
357 namespace_body opt_semicolon
359 current_namespace = current_namespace.Parent;
374 : namespace_or_type_name {
375 MemberName name = (MemberName) $1;
377 $$ = name.GetName ();
384 opt_namespace_member_declarations
395 opt_namespace_member_declarations
397 | namespace_member_declarations
400 namespace_member_declarations
401 : namespace_member_declaration
402 | namespace_member_declarations namespace_member_declaration
405 namespace_member_declaration
412 Class c = (Class) $1;
413 mod_flags = c.ModFlags;
415 } else if ($1 is Struct){
416 Struct s = (Struct) $1;
417 mod_flags = s.ModFlags;
422 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
424 1527, lexer.Location,
425 "Namespace elements cant be explicitly " +
426 "declared private or protected in `" + name + "'");
428 current_namespace.DeclarationFound = true;
430 | namespace_declaration {
431 current_namespace.DeclarationFound = true;
434 | field_declaration {
435 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
437 | method_declaration {
438 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
445 | interface_declaration
447 | delegate_declaration
449 // Enable this when we have handled all errors, because this acts as a generic fallback
452 // Console.WriteLine ("Token=" + yyToken);
453 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
465 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
473 global_attrs_enabled = false;
478 global_attrs_enabled = false;
487 ArrayList sect = (ArrayList) $1;
489 if (global_attrs_enabled) {
490 if (current_attr_target == "module") {
491 CodeGen.Module.AddAttributes (sect);
493 } else if (current_attr_target == "assembly") {
494 CodeGen.Assembly.AddAttributes (sect);
497 $$ = new Attributes (sect);
500 $$ = new Attributes (sect);
502 current_attr_target = null;
504 | attribute_sections attribute_section
506 Attributes attrs = $1 as Attributes;
507 ArrayList sect = (ArrayList) $2;
509 if (global_attrs_enabled) {
510 if (current_attr_target == "module") {
511 CodeGen.Module.AddAttributes (sect);
513 } else if (current_attr_target == "assembly") {
514 CodeGen.Assembly.AddAttributes (sect);
518 attrs = new Attributes (sect);
520 attrs.AddAttributes (sect);
524 attrs = new Attributes (sect);
526 attrs.AddAttributes (sect);
529 current_attr_target = null;
534 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
538 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
544 attribute_target_specifier
545 : attribute_target COLON
547 current_attr_target = (string)$1;
555 CheckAttributeTarget ((string) $1);
558 | EVENT { $$ = "event"; }
559 | RETURN { $$ = "return"; }
565 ArrayList attrs = new ArrayList (4);
571 | attribute_list COMMA attribute
573 ArrayList attrs = (ArrayList) $1;
585 opt_attribute_arguments
587 MemberName mname = (MemberName) $1;
588 string name = mname.GetName ();
589 if (current_attr_target == "assembly" || current_attr_target == "module")
590 $$ = new GlobalAttribute (current_container, current_attr_target,
591 name, (ArrayList) $3, (Location) $2);
593 $$ = new Attribute (current_attr_target, name, (ArrayList) $3,
599 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
602 opt_attribute_arguments
603 : /* empty */ { $$ = null; }
604 | OPEN_PARENS attribute_arguments CLOSE_PARENS
612 : opt_positional_argument_list
617 ArrayList args = new ArrayList (4);
623 | positional_argument_list COMMA named_argument_list
625 ArrayList args = new ArrayList (4);
631 | named_argument_list
633 ArrayList args = new ArrayList (4);
642 opt_positional_argument_list
643 : /* empty */ { $$ = null; }
644 | positional_argument_list
647 positional_argument_list
650 ArrayList args = new ArrayList (4);
651 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
655 | positional_argument_list COMMA expression
657 ArrayList args = (ArrayList) $1;
658 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
667 ArrayList args = new ArrayList (4);
672 | named_argument_list COMMA named_argument
674 ArrayList args = (ArrayList) $1;
682 : IDENTIFIER ASSIGN expression
684 $$ = new DictionaryEntry (
686 new Argument ((Expression) $3, Argument.AType.Expression));
692 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
695 opt_class_member_declarations
697 | class_member_declarations
700 class_member_declarations
701 : class_member_declaration
702 | class_member_declarations
703 class_member_declaration
706 class_member_declaration
707 : constant_declaration // done
708 | field_declaration // done
709 | method_declaration // done
710 | property_declaration // done
711 | event_declaration // done
712 | indexer_declaration // done
713 | operator_declaration // done
714 | constructor_declaration // done
715 | destructor_declaration // done
725 MemberName name = MakeName ((MemberName) $5);
726 bool partial = (bool) $3;
729 ClassPart part = PartialContainer.CreatePart (
730 current_namespace, current_container, name, (int) $2,
731 (Attributes) $1, Kind.Struct, lexer.Location);
733 current_container = part.PartialContainer;
734 current_class = part;
736 current_class = new Struct (
737 current_namespace, current_container, name, (int) $2,
738 (Attributes) $1, lexer.Location);
740 current_container = current_class;
741 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
747 current_class.Bases = (ArrayList) $7;
749 current_class.Register ();
756 current_container = current_container.Parent;
757 current_class = current_container;
759 | opt_attributes opt_modifiers opt_partial STRUCT error {
760 CheckIdentifierToken (yyToken);
765 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
768 opt_struct_member_declarations
770 | struct_member_declarations
773 struct_member_declarations
774 : struct_member_declaration
775 | struct_member_declarations struct_member_declaration
778 struct_member_declaration
779 : constant_declaration
782 | property_declaration
784 | indexer_declaration
785 | operator_declaration
786 | constructor_declaration
790 * This is only included so we can flag error 575:
791 * destructors only allowed on class types
793 | destructor_declaration
804 int modflags = (int) $2;
805 foreach (VariableDeclaration constant in (ArrayList) $5){
806 Location l = constant.Location;
807 if ((modflags & Modifiers.STATIC) != 0) {
808 Report.Error (504, l, "The constant '{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
812 Const c = new Const (
813 current_class, (Expression) $4, (string) constant.identifier,
814 (Expression) constant.expression_or_array_initializer, modflags,
817 current_container.AddConstant (c);
823 : constant_declarator
825 ArrayList constants = new ArrayList (4);
830 | constant_declarators COMMA constant_declarator
833 ArrayList constants = (ArrayList) $1;
840 : IDENTIFIER ASSIGN constant_expression
842 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
846 // A const field requires a value to be provided
847 Report.Error (145, lexer.Location, "A const field requires a value to be provided");
859 Expression type = (Expression) $3;
862 foreach (VariableDeclaration var in (ArrayList) $4){
863 Location l = var.Location;
865 Field field = new Field (current_class, type, mod, var.identifier,
866 var.expression_or_array_initializer,
869 current_container.AddField (field);
877 Report.Error (670, lexer.Location, "void type is not allowed for fields");
882 : variable_declarator
884 ArrayList decl = new ArrayList (4);
888 | variable_declarators COMMA variable_declarator
890 ArrayList decls = (ArrayList) $1;
897 : IDENTIFIER ASSIGN variable_initializer
899 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
903 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
916 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
918 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
924 iterator_container = (IIteratorContainer) $1;
928 Method method = (Method) $1;
929 Block b = (Block) $3;
930 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
933 if ((method.ModFlags & extern_abstract) == 0){
935 501, lexer.Location, current_container.MakeName (method.Name) +
936 "must declare a body because it is not marked abstract or extern");
939 if ((method.ModFlags & Modifiers.EXTERN) != 0){
941 179, lexer.Location, current_container.MakeName (method.Name) +
942 " is declared extern, but has a body");
946 method.Block = (Block) $3;
947 current_container.AddMethod (method);
949 current_local_parameters = null;
950 iterator_container = null;
964 1585, lexer.Location, "Member modifier `" +
965 Modifiers.Name (i) + "' must precede member type and name");
976 type namespace_or_type_name
977 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
979 MemberName name = (MemberName) $4;
981 Method method = new Method (current_class, (Expression) $3, (int) $2,
982 false, name, (Parameters) $6, (Attributes) $1,
985 current_local_parameters = (Parameters) $6;
991 VOID namespace_or_type_name
992 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
994 MemberName name = (MemberName) $4;
996 Method method = new Method (current_class, TypeManager.system_void_expr,
997 (int) $2, false, name, (Parameters) $6,
998 (Attributes) $1, lexer.Location);
1000 current_local_parameters = (Parameters) $6;
1006 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1008 Report.Error (1585, lexer.Location,
1009 String.Format ("Modifier {0} should appear before type",
1010 Modifiers.Name ((int) $4)));
1011 MemberName name = (MemberName) $4;
1013 Method method = new Method (current_class, TypeManager.system_void_expr,
1014 0, false, name, (Parameters) $7, (Attributes) $1,
1017 current_local_parameters = (Parameters) $7;
1024 | SEMICOLON { $$ = null; }
1027 opt_formal_parameter_list
1028 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1029 | formal_parameter_list
1032 formal_parameter_list
1035 ArrayList pars_list = (ArrayList) $1;
1037 Parameter [] pars = new Parameter [pars_list.Count];
1038 pars_list.CopyTo (pars);
1040 $$ = new Parameters (pars, null, lexer.Location);
1042 | fixed_parameters COMMA parameter_array
1044 ArrayList pars_list = (ArrayList) $1;
1046 Parameter [] pars = new Parameter [pars_list.Count];
1047 pars_list.CopyTo (pars);
1049 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
1051 | fixed_parameters COMMA ARGLIST
1053 ArrayList pars_list = (ArrayList) $1;
1055 Parameter [] pars = new Parameter [pars_list.Count];
1056 pars_list.CopyTo (pars);
1058 $$ = new Parameters (pars, true, lexer.Location);
1060 | parameter_array COMMA fixed_parameters
1062 Report.Error (231, lexer.Location, "A params parameter must be the last parameter in a formal parameter list");
1065 | ARGLIST COMMA fixed_parameters
1067 Report.Error (257, lexer.Location, "An __arglist parameter must be the last parameter in a formal parameter list");
1072 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
1076 $$ = new Parameters (null, true, lexer.Location);
1083 ArrayList pars = new ArrayList (4);
1088 | fixed_parameters COMMA fixed_parameter
1090 ArrayList pars = (ArrayList) $1;
1099 opt_parameter_modifier
1103 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
1106 opt_parameter_modifier
1109 CheckIdentifierToken (yyToken);
1113 opt_parameter_modifier
1119 Report.Error (241, lexer.Location, "Default parameter specifiers are not permitted");
1124 opt_parameter_modifier
1125 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1126 | parameter_modifier
1130 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1131 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1135 : opt_attributes PARAMS type IDENTIFIER
1137 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1138 note ("type must be a single-dimension array type");
1140 | opt_attributes PARAMS type error {
1141 CheckIdentifierToken (yyToken);
1146 property_declaration
1149 type namespace_or_type_name
1152 implicit_value_parameter_type = (Expression) $3;
1154 lexer.PropertyParsing = true;
1156 $$ = lexer.Location;
1158 iterator_container = SimpleIteratorContainer.GetSimple ();
1160 accessor_declarations
1162 lexer.PropertyParsing = false;
1167 Pair pair = (Pair) $7;
1168 Accessor get_block = (Accessor) pair.First;
1169 Accessor set_block = (Accessor) pair.Second;
1171 Location loc = (Location) $6;
1172 MemberName name = (MemberName) $4;
1174 prop = new Property (current_class, (Expression) $3, (int) $2, false,
1175 name, (Attributes) $1, get_block, set_block, loc);
1176 if (SimpleIteratorContainer.Simple.Yields)
1179 current_container.AddProperty (prop);
1180 implicit_value_parameter_type = null;
1181 iterator_container = null;
1185 accessor_declarations
1186 : get_accessor_declaration opt_set_accessor_declaration
1188 $$ = new Pair ($1, $2);
1190 | set_accessor_declaration opt_get_accessor_declaration
1192 $$ = new Pair ($2, $1);
1196 opt_get_accessor_declaration
1197 : /* empty */ { $$ = null; }
1198 | get_accessor_declaration
1201 opt_set_accessor_declaration
1202 : /* empty */ { $$ = null; }
1203 | set_accessor_declaration
1206 get_accessor_declaration
1207 : opt_attributes GET
1209 // If this is not the case, then current_local_parameters has already
1210 // been set in indexer_declaration
1211 if (parsing_indexer == false)
1212 current_local_parameters = null;
1214 current_local_parameters = indexer_parameters;
1215 lexer.PropertyParsing = false;
1219 $$ = new Accessor ((Block) $4, (Attributes) $1, lexer.Location);
1220 current_local_parameters = null;
1221 lexer.PropertyParsing = true;
1225 set_accessor_declaration
1226 : opt_attributes SET
1229 Parameter implicit_value_parameter = new Parameter (
1230 implicit_value_parameter_type, "value",
1231 Parameter.Modifier.NONE, null);
1233 if (parsing_indexer == false) {
1234 args = new Parameter [1];
1235 args [0] = implicit_value_parameter;
1236 current_local_parameters = new Parameters (args, null, lexer.Location);
1238 Parameter [] fpars = indexer_parameters.FixedParameters;
1241 int count = fpars.Length;
1243 args = new Parameter [count + 1];
1244 fpars.CopyTo (args, 0);
1245 args [count] = implicit_value_parameter;
1248 current_local_parameters = new Parameters (
1249 args, indexer_parameters.ArrayParameter, lexer.Location);
1252 lexer.PropertyParsing = false;
1256 $$ = new Accessor ((Block) $4, (Attributes) $1, lexer.Location);
1257 current_local_parameters = null;
1258 lexer.PropertyParsing = true;
1264 | SEMICOLON { $$ = null; }
1267 interface_declaration
1271 INTERFACE member_name
1273 MemberName name = MakeName ((MemberName) $5);
1274 bool partial = (bool) $3;
1277 ClassPart part = PartialContainer.CreatePart (
1278 current_namespace, current_container, name, (int) $2,
1279 (Attributes) $1, Kind.Interface, lexer.Location);
1281 current_container = part.PartialContainer;
1282 current_class = part;
1284 current_class = new Interface (
1285 current_namespace, current_container, name, (int) $2,
1286 (Attributes) $1, lexer.Location);
1288 current_container = current_class;
1289 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
1294 current_class.Bases = (ArrayList) $7;
1296 current_class.Register ();
1303 current_container = current_container.Parent;
1304 current_class = current_container;
1306 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1307 CheckIdentifierToken (yyToken);
1313 opt_interface_member_declarations
1317 opt_interface_member_declarations
1319 | interface_member_declarations
1322 interface_member_declarations
1323 : interface_member_declaration
1324 | interface_member_declarations interface_member_declaration
1327 interface_member_declaration
1328 : interface_method_declaration
1330 Method m = (Method) $1;
1332 current_container.AddMethod (m);
1334 | interface_property_declaration
1336 Property p = (Property) $1;
1338 current_container.AddProperty (p);
1340 | interface_event_declaration
1343 Event e = (Event) $1;
1344 current_container.AddEvent (e);
1347 | interface_indexer_declaration
1349 Indexer i = (Indexer) $1;
1351 current_container.AddIndexer (i);
1359 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, lexer.Location);
1364 interface_method_declaration
1365 : opt_attributes opt_new type namespace_or_type_name
1366 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1369 MemberName name = (MemberName) $4;
1371 $$ = new Method (current_class, (Expression) $3, (int) $2, true,
1372 name, (Parameters) $6, (Attributes) $1, lexer.Location);
1374 | opt_attributes opt_new VOID namespace_or_type_name
1375 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1378 MemberName name = (MemberName) $4;
1380 $$ = new Method (current_class, TypeManager.system_void_expr, (int) $2,
1381 true, name, (Parameters) $6, (Attributes) $1, lexer.Location);
1385 interface_property_declaration
1390 { lexer.PropertyParsing = true; }
1392 { lexer.PropertyParsing = false; }
1395 InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
1397 $$ = new Property (current_class, (Expression) $3, (int) $2, true,
1398 new MemberName ((string) $4), (Attributes) $1,
1399 pinfo.Get, pinfo.Set, lexer.Location);
1404 CheckIdentifierToken (yyToken);
1410 : opt_attributes GET SEMICOLON { $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, lexer.Location, lexer.Location); }
1411 | opt_attributes SET SEMICOLON { $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1, lexer.Location, lexer.Location); }
1412 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1413 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $3, lexer.Location, lexer.Location); }
1414 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1415 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $3, (Attributes) $1, lexer.Location, lexer.Location); }
1418 interface_event_declaration
1419 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1421 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1422 new MemberName ((string) $5), null,
1423 (Attributes) $1, lexer.Location);
1425 | opt_attributes opt_new EVENT type error {
1426 CheckIdentifierToken (yyToken);
1429 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1430 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1433 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE event_accessor_declarations CLOSE_BRACE {
1434 Report.Error (69, lexer.Location, "Event accessors not valid on interfaces");
1439 interface_indexer_declaration
1440 : opt_attributes opt_new type THIS
1441 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1443 { lexer.PropertyParsing = true; }
1445 { lexer.PropertyParsing = false; }
1448 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1450 $$ = new Indexer (current_class, (Expression) $3,
1451 new MemberName (TypeContainer.DefaultIndexerName),
1452 (int) $2, true, (Parameters) $6, (Attributes) $1,
1453 info.Get, info.Set, lexer.Location);
1457 operator_declaration
1458 : opt_attributes opt_modifiers operator_declarator
1460 iterator_container = SimpleIteratorContainer.GetSimple ();
1464 OperatorDeclaration decl = (OperatorDeclaration) $3;
1466 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1468 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null);
1469 if (decl.arg2type != null)
1470 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null);
1472 Operator op = new Operator (
1473 current_class, decl.optype, decl.ret_type, (int) $2,
1474 new Parameters (param_list, null, decl.location),
1475 (Block) $5, (Attributes) $1, decl.location);
1477 if (SimpleIteratorContainer.Simple.Yields)
1480 // Note again, checking is done in semantic analysis
1481 current_container.AddOperator (op);
1483 current_local_parameters = null;
1484 iterator_container = null;
1490 | SEMICOLON { $$ = null; }
1493 : type OPERATOR overloadable_operator
1494 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1496 Operator.OpType op = (Operator.OpType) $3;
1497 CheckUnaryOperator (op);
1499 if (op == Operator.OpType.Addition)
1500 op = Operator.OpType.UnaryPlus;
1502 if (op == Operator.OpType.Subtraction)
1503 op = Operator.OpType.UnaryNegation;
1505 Parameter [] pars = new Parameter [1];
1507 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1509 current_local_parameters = new Parameters (pars, null, lexer.Location);
1511 $$ = new OperatorDeclaration (op, (Expression) $1, (Expression) $5, (string) $6,
1512 null, null, lexer.Location);
1514 | type OPERATOR overloadable_operator
1516 type IDENTIFIER COMMA
1520 CheckBinaryOperator ((Operator.OpType) $3);
1522 Parameter [] pars = new Parameter [2];
1524 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1525 pars [1] = new Parameter ((Expression) $8, (string) $9, Parameter.Modifier.NONE, null);
1527 current_local_parameters = new Parameters (pars, null, lexer.Location);
1529 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1530 (Expression) $5, (string) $6,
1531 (Expression) $8, (string) $9, lexer.Location);
1533 | conversion_operator_declarator
1536 overloadable_operator
1538 : BANG { $$ = Operator.OpType.LogicalNot; }
1539 | TILDE { $$ = Operator.OpType.OnesComplement; }
1540 | OP_INC { $$ = Operator.OpType.Increment; }
1541 | OP_DEC { $$ = Operator.OpType.Decrement; }
1542 | TRUE { $$ = Operator.OpType.True; }
1543 | FALSE { $$ = Operator.OpType.False; }
1544 // Unary and binary:
1545 | PLUS { $$ = Operator.OpType.Addition; }
1546 | MINUS { $$ = Operator.OpType.Subtraction; }
1548 | STAR { $$ = Operator.OpType.Multiply; }
1549 | DIV { $$ = Operator.OpType.Division; }
1550 | PERCENT { $$ = Operator.OpType.Modulus; }
1551 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1552 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1553 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1554 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1555 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1556 | OP_EQ { $$ = Operator.OpType.Equality; }
1557 | OP_NE { $$ = Operator.OpType.Inequality; }
1558 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1559 | OP_LT { $$ = Operator.OpType.LessThan; }
1560 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1561 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1564 conversion_operator_declarator
1565 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1567 Parameter [] pars = new Parameter [1];
1569 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1571 current_local_parameters = new Parameters (pars, null, lexer.Location);
1573 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1574 null, null, lexer.Location);
1576 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1578 Parameter [] pars = new Parameter [1];
1580 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1582 current_local_parameters = new Parameters (pars, null, lexer.Location);
1584 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1585 null, null, lexer.Location);
1589 syntax_error (lexer.Location, "'operator' expected");
1593 syntax_error (lexer.Location, "'operator' expected");
1597 constructor_declaration
1600 constructor_declarator
1603 Constructor c = (Constructor) $3;
1604 c.Block = (Block) $4;
1605 c.OptAttributes = (Attributes) $1;
1606 c.ModFlags = (int) $2;
1608 if (c.Name == current_container.Basename){
1609 if ((c.ModFlags & Modifiers.STATIC) != 0){
1610 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1612 515, c.Location, String.Format (
1613 "`{0}.{1}': static constructor can not have access modifiers",
1614 c.Name, current_container.Name));
1617 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1619 if (c.Initializer != null){
1622 "Static constructors can not have an explicit this or base " +
1623 "constructor invocations");
1626 if (!c.Parameters.Empty){
1628 132, c.Location, "Static constructors should not have parameters");
1631 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1634 // We let another layer check the validity of the constructor.
1635 Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1638 current_container.AddConstructor (c);
1640 current_local_parameters = null;
1644 constructor_declarator
1646 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1648 oob_stack.Push (lexer.Location);
1650 current_local_parameters = (Parameters) $3;
1652 opt_constructor_initializer
1654 Location l = (Location) oob_stack.Pop ();
1655 $$ = new Constructor (current_class, (string) $1, 0, (Parameters) $3,
1656 (ConstructorInitializer) $6, l);
1662 | SEMICOLON { $$ = null; }
1665 opt_constructor_initializer
1666 : /* empty */ { $$ = null; }
1667 | constructor_initializer
1670 constructor_initializer
1671 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1673 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1675 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1677 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1680 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1686 : /* EMPTY */ { $$ = 0; }
1687 | UNSAFE { $$ = Modifiers.UNSAFE; }
1688 | EXTERN { $$ = Modifiers.EXTERN; }
1691 destructor_declaration
1692 : opt_attributes opt_finalizer TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1694 if ((string) $4 != current_container.Basename){
1695 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1696 } else if (!(current_container is Class)){
1697 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1699 Location l = lexer.Location;
1702 if (!RootContext.StdLib && current_container.Name == "System.Object")
1703 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
1705 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
1707 if ((m & Modifiers.UNSAFE) != 0){
1708 if (!RootContext.Unsafe){
1709 Report.Error (227, l,
1710 "Unsafe code requires the -unsafe command " +
1711 "line option to be specified");
1715 Method d = new Destructor (
1716 current_class, TypeManager.system_void_expr, m, "Finalize",
1717 new Parameters (null, null, l), (Attributes) $1, l);
1719 d.Block = (Block) $7;
1720 current_container.AddMethod (d);
1728 EVENT type variable_declarators SEMICOLON
1730 foreach (VariableDeclaration var in (ArrayList) $5) {
1732 MemberName name = new MemberName (var.identifier);
1734 Event e = new EventField (
1735 current_class, (Expression) $4, (int) $2, false, name,
1736 var.expression_or_array_initializer, (Attributes) $1,
1739 current_container.AddEvent (e);
1745 EVENT type namespace_or_type_name
1748 implicit_value_parameter_type = (Expression) $4;
1749 lexer.EventParsing = true;
1750 oob_stack.Push (lexer.Location);
1752 event_accessor_declarations
1754 lexer.EventParsing = false;
1758 Location loc = (Location) oob_stack.Pop ();
1761 Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
1764 Pair pair = (Pair) $8;
1766 MemberName name = (MemberName) $5;
1768 Event e = new EventProperty (
1769 current_class, (Expression) $4, (int) $2, false, name, null,
1770 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
1773 current_container.AddEvent (e);
1774 implicit_value_parameter_type = null;
1777 | opt_attributes opt_modifiers EVENT type namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS block {
1778 MemberName mn = (MemberName) $5;
1780 if (mn.Left != null)
1781 Report.Error (71, lexer.Location, "Explicit implementation of events requires property syntax");
1783 Report.Error (71, lexer.Location, "Event declaration should use property syntax");
1787 event_accessor_declarations
1788 : add_accessor_declaration remove_accessor_declaration
1790 $$ = new Pair ($1, $2);
1792 | remove_accessor_declaration add_accessor_declaration
1794 $$ = new Pair ($2, $1);
1796 | add_accessor_declaration { $$ = null; }
1797 | remove_accessor_declaration { $$ = null; }
1800 add_accessor_declaration
1801 : opt_attributes ADD
1803 Parameter [] args = new Parameter [1];
1804 Parameter implicit_value_parameter = new Parameter (
1805 implicit_value_parameter_type, "value",
1806 Parameter.Modifier.NONE, null);
1808 args [0] = implicit_value_parameter;
1810 current_local_parameters = new Parameters (args, null, lexer.Location);
1811 lexer.EventParsing = false;
1815 $$ = new Accessor ((Block) $4, (Attributes) $1, lexer.Location);
1816 lexer.EventParsing = true;
1818 | opt_attributes ADD error {
1819 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1824 remove_accessor_declaration
1825 : opt_attributes REMOVE
1827 Parameter [] args = new Parameter [1];
1828 Parameter implicit_value_parameter = new Parameter (
1829 implicit_value_parameter_type, "value",
1830 Parameter.Modifier.NONE, null);
1832 args [0] = implicit_value_parameter;
1834 current_local_parameters = new Parameters (args, null, lexer.Location);
1835 lexer.EventParsing = false;
1839 $$ = new Accessor ((Block) $4, (Attributes) $1, lexer.Location);
1840 lexer.EventParsing = true;
1842 | opt_attributes REMOVE error {
1843 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1849 : opt_attributes opt_modifiers indexer_declarator
1852 IndexerDeclaration decl = (IndexerDeclaration) $3;
1854 implicit_value_parameter_type = decl.type;
1856 lexer.PropertyParsing = true;
1857 parsing_indexer = true;
1859 indexer_parameters = decl.param_list;
1860 oob_stack.Push (lexer.Location);
1862 accessor_declarations
1864 lexer.PropertyParsing = false;
1865 parsing_indexer = false;
1869 // The signature is computed from the signature of the indexer. Look
1870 // at section 3.6 on the spec
1871 Location loc = (Location) oob_stack.Pop ();
1873 IndexerDeclaration decl = (IndexerDeclaration) $3;
1874 Pair pair = (Pair) $6;
1875 Accessor get_block = (Accessor) pair.First;
1876 Accessor set_block = (Accessor) pair.Second;
1879 if (decl.interface_type != null)
1880 name = new MemberName (decl.interface_type,
1881 TypeContainer.DefaultIndexerName);
1883 name = new MemberName (TypeContainer.DefaultIndexerName);
1885 indexer = new Indexer (current_class, decl.type, name,
1886 (int) $2, false, decl.param_list, (Attributes) $1,
1887 get_block, set_block, loc);
1889 current_container.AddIndexer (indexer);
1891 current_local_parameters = null;
1892 implicit_value_parameter_type = null;
1893 indexer_parameters = null;
1898 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1900 Parameters pars = (Parameters) $4;
1901 if (pars.HasArglist) {
1902 // "__arglist is not valid in this context"
1903 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
1904 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
1905 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1908 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
1910 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1912 Parameters pars = (Parameters) $6;
1914 if (pars.HasArglist) {
1915 // "__arglist is not valid in this context"
1916 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
1917 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
1918 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1920 MemberName name = (MemberName) $2;
1921 $$ = new IndexerDeclaration ((Expression) $1, name, pars);
1933 Location enum_location = lexer.Location;
1935 MemberName full_name = MakeName (new MemberName ((string) $4));
1936 Enum e = new Enum (current_namespace, current_container, (Expression) $5, (int) $2,
1937 full_name, (Attributes) $1, enum_location);
1939 foreach (VariableDeclaration ev in (ArrayList) $6) {
1940 e.AddEnumMember (ev.identifier,
1941 (Expression) ev.expression_or_array_initializer,
1942 ev.Location, ev.OptAttributes);
1945 string name = full_name.GetName ();
1946 current_container.AddEnum (e);
1947 RootContext.Tree.RecordDecl (name, e);
1953 : /* empty */ { $$ = TypeManager.system_int32_expr; }
1954 | COLON type { $$ = $2; }
1958 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1964 opt_enum_member_declarations
1965 : /* empty */ { $$ = new ArrayList (4); }
1966 | enum_member_declarations opt_comma { $$ = $1; }
1969 enum_member_declarations
1970 : enum_member_declaration
1972 ArrayList l = new ArrayList (4);
1977 | enum_member_declarations COMMA enum_member_declaration
1979 ArrayList l = (ArrayList) $1;
1987 enum_member_declaration
1988 : opt_attributes IDENTIFIER
1990 $$ = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
1992 | opt_attributes IDENTIFIER
1994 $$ = lexer.Location;
1998 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
2002 delegate_declaration
2005 DELEGATE type member_name
2006 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2009 Location l = lexer.Location;
2010 MemberName name = MakeName ((MemberName) $5);
2011 Delegate del = new Delegate (current_namespace, current_container, (Expression) $4,
2012 (int) $2, name, (Parameters) $7, (Attributes) $1, l);
2014 current_container.AddDelegate (del);
2015 RootContext.Tree.RecordDecl (name.GetName (true), del);
2019 DELEGATE VOID member_name
2020 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2023 Location l = lexer.Location;
2024 MemberName name = MakeName ((MemberName) $5);
2025 Delegate del = new Delegate (
2026 current_namespace, current_container,
2027 TypeManager.system_void_expr, (int) $2, name,
2028 (Parameters) $7, (Attributes) $1, l);
2030 current_container.AddDelegate (del);
2031 RootContext.Tree.RecordDecl (name.GetName (true), del);
2035 namespace_or_type_name
2037 | namespace_or_type_name DOT IDENTIFIER {
2038 $$ = new MemberName ((MemberName) $1, (string) $3);
2044 $$ = new MemberName ((string) $1);
2049 * Before you think of adding a return_type, notice that we have been
2050 * using two rules in the places where it matters (one rule using type
2051 * and another identical one that uses VOID as the return type). This
2052 * gets rid of a shift/reduce couple
2055 : namespace_or_type_name
2057 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2069 // Note that here only unmanaged types are allowed but we
2070 // can't perform checks during this phase - we do it during
2071 // semantic analysis.
2073 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2077 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2083 | non_expression_type rank_specifier
2085 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2087 | non_expression_type STAR
2089 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2091 | expression rank_specifiers
2093 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2097 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2101 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2102 // through this different path
2104 | multiplicative_expression STAR
2106 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2113 ArrayList types = new ArrayList (4);
2118 | type_list COMMA type
2120 ArrayList types = (ArrayList) $1;
2128 * replaces all the productions for isolating the various
2129 * simple types, but we need this to reuse it easily in local_variable_type
2132 : OBJECT { $$ = TypeManager.system_object_expr; }
2133 | STRING { $$ = TypeManager.system_string_expr; }
2134 | BOOL { $$ = TypeManager.system_boolean_expr; }
2135 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2136 | FLOAT { $$ = TypeManager.system_single_expr; }
2137 | DOUBLE { $$ = TypeManager.system_double_expr; }
2142 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2143 | BYTE { $$ = TypeManager.system_byte_expr; }
2144 | SHORT { $$ = TypeManager.system_int16_expr; }
2145 | USHORT { $$ = TypeManager.system_uint16_expr; }
2146 | INT { $$ = TypeManager.system_int32_expr; }
2147 | UINT { $$ = TypeManager.system_uint32_expr; }
2148 | LONG { $$ = TypeManager.system_int64_expr; }
2149 | ULONG { $$ = TypeManager.system_uint64_expr; }
2150 | CHAR { $$ = TypeManager.system_char_expr; }
2151 | VOID { $$ = TypeManager.system_void_expr; }
2155 : type rank_specifiers
2157 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2162 // Expressions, section 7.5
2172 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2174 | parenthesized_expression
2176 | invocation_expression
2180 | post_increment_expression
2181 | post_decrement_expression
2185 | checked_expression
2186 | unchecked_expression
2187 | pointer_member_access
2188 | anonymous_method_expression
2195 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2196 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2197 | NULL { $$ = NullLiteral.Null; }
2201 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2202 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2203 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2208 object v = lexer.Value;
2214 $$ = IntLiteral.Zero;
2216 $$ = IntLiteral.One;
2218 $$ = new IntLiteral (i);
2219 } else if (v is uint)
2220 $$ = new UIntLiteral ((UInt32) v);
2222 $$ = new LongLiteral ((Int64) v);
2223 else if (v is ulong)
2224 $$ = new ULongLiteral ((UInt64) v);
2226 Console.WriteLine ("OOPS. Unexpected result from scanner");
2231 : TRUE { $$ = new BoolLiteral (true); }
2232 | FALSE { $$ = new BoolLiteral (false); }
2235 parenthesized_expression_0
2236 : OPEN_PARENS expression CLOSE_PARENS
2239 lexer.Deambiguate_CloseParens ();
2240 // After this, the next token returned is one of
2241 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2242 // or CLOSE_PARENS_MINUS.
2246 parenthesized_expression
2247 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2251 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2253 // If a parenthesized expression is followed by a minus, we need to wrap
2254 // the expression inside a ParenthesizedExpression for the CS0075 check
2255 // in Binary.DoResolve().
2256 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2261 : primary_expression DOT IDENTIFIER
2263 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2265 | predefined_type DOT IDENTIFIER
2267 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2275 invocation_expression
2276 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2279 Location l = lexer.Location;
2280 Report.Error (1, l, "Parse error");
2282 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2284 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2286 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2288 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2290 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2295 : /* empty */ { $$ = null; }
2302 ArrayList list = new ArrayList (4);
2306 | argument_list COMMA argument
2308 ArrayList list = (ArrayList) $1;
2312 | argument_list error {
2313 CheckToken (1026, yyToken, ", or ) expected");
2320 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2322 | REF variable_reference
2324 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2326 | OUT variable_reference
2328 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2330 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2332 ArrayList list = (ArrayList) $3;
2333 Argument[] args = new Argument [list.Count];
2334 list.CopyTo (args, 0);
2336 Expression expr = new Arglist (args, lexer.Location);
2337 $$ = new Argument (expr, Argument.AType.Expression);
2341 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2346 : expression { note ("section 5.4"); $$ = $1; }
2350 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2352 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2354 | primary_expression rank_specifiers
2356 // So the super-trick is that primary_expression
2357 // can only be either a SimpleName or a MemberAccess.
2358 // The MemberAccess case arises when you have a fully qualified type-name like :
2360 // SimpleName is when you have
2363 Expression expr = (Expression) $1;
2364 if (expr is ComposedCast){
2365 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2366 } else if (!(expr is SimpleName || expr is MemberAccess)){
2367 Error_ExpectingTypeName (lexer.Location, expr);
2368 $$ = TypeManager.system_object_expr;
2371 // So we extract the string corresponding to the SimpleName
2374 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2382 ArrayList list = new ArrayList (4);
2386 | expression_list COMMA expression
2388 ArrayList list = (ArrayList) $1;
2397 $$ = new This (current_block, lexer.Location);
2402 : BASE DOT IDENTIFIER
2404 $$ = new BaseAccess ((string) $3, lexer.Location);
2406 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2408 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2411 Report.Error (175, "Use of keyword `base' is not valid in this context");
2416 post_increment_expression
2417 : primary_expression OP_INC
2419 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2420 (Expression) $1, lexer.Location);
2424 post_decrement_expression
2425 : primary_expression OP_DEC
2427 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2428 (Expression) $1, lexer.Location);
2433 : object_or_delegate_creation_expression
2434 | array_creation_expression
2437 object_or_delegate_creation_expression
2438 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2440 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2444 array_creation_expression
2445 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2447 opt_array_initializer
2449 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2451 | NEW type rank_specifiers array_initializer
2453 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2457 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2473 : rank_specifier opt_rank_specifier
2475 $$ = (string) $2 + (string) $1;
2480 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2482 $$ = "[" + (string) $2 + "]";
2502 | dim_separators COMMA
2504 $$ = (string) $1 + ",";
2508 opt_array_initializer
2520 : OPEN_BRACE CLOSE_BRACE
2522 ArrayList list = new ArrayList (4);
2525 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2527 $$ = (ArrayList) $2;
2531 variable_initializer_list
2532 : variable_initializer
2534 ArrayList list = new ArrayList (4);
2538 | variable_initializer_list COMMA variable_initializer
2540 ArrayList list = (ArrayList) $1;
2547 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2549 $$ = new TypeOfVoid (lexer.Location);
2551 | TYPEOF OPEN_PARENS type CLOSE_PARENS
2553 $$ = new TypeOf ((Expression) $3, lexer.Location);
2558 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2559 $$ = new SizeOf ((Expression) $3, lexer.Location);
2564 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2566 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2570 unchecked_expression
2571 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2573 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2577 pointer_member_access
2578 : primary_expression OP_PTR IDENTIFIER
2582 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2583 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2587 anonymous_method_expression
2588 : DELEGATE opt_anonymous_method_signature {
2589 oob_stack.Push (current_local_parameters);
2590 current_local_parameters = (Parameters)$2;
2591 create_toplevel_block = true;
2594 Report.Error (-213, lexer.Location, "Anonymous methods are not supported in this branch");
2597 create_toplevel_block = false;
2598 if (RootContext.Version == LanguageVersion.ISO_1){
2599 Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
2602 $$ = new AnonymousMethod ((Parameters) $2, (Block) $4, lexer.Location);
2603 current_local_parameters = (Parameters) oob_stack.Pop ();
2608 opt_anonymous_method_signature
2609 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
2610 | anonymous_method_signature
2613 anonymous_method_signature
2614 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
2617 $$ = Parameters.EmptyReadOnlyParameters;
2619 ArrayList par_list = (ArrayList) $2;
2620 Parameter [] pars = new Parameter [par_list.Count];
2621 par_list.CopyTo (pars);
2622 $$ = new Parameters (pars, null, lexer.Location);
2627 opt_anonymous_method_parameter_list
2628 : /* empty */ { $$ = null; }
2629 | anonymous_method_parameter_list { $$ = $1; }
2632 anonymous_method_parameter_list
2633 : anonymous_method_parameter
2635 ArrayList a = new ArrayList (4);
2639 | anonymous_method_parameter_list COMMA anonymous_method_parameter
2641 ArrayList a = (ArrayList) $1;
2647 anonymous_method_parameter
2648 : opt_parameter_modifier type IDENTIFIER {
2649 $$ = new Parameter ((Expression) $2, (string) $2, (Parameter.Modifier) $1, null);
2654 : primary_expression
2655 | BANG prefixed_unary_expression
2657 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2659 | TILDE prefixed_unary_expression
2661 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2667 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
2669 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2671 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
2673 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2679 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2681 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2686 // The idea to split this out is from Rhys' grammar
2687 // to solve the problem with casts.
2689 prefixed_unary_expression
2691 | PLUS prefixed_unary_expression
2693 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2695 | MINUS prefixed_unary_expression
2697 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2699 | OP_INC prefixed_unary_expression
2701 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2702 (Expression) $2, lexer.Location);
2704 | OP_DEC prefixed_unary_expression
2706 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2707 (Expression) $2, lexer.Location);
2709 | STAR prefixed_unary_expression
2711 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2713 | BITWISE_AND prefixed_unary_expression
2715 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2719 pre_increment_expression
2720 : OP_INC prefixed_unary_expression
2722 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2723 (Expression) $2, lexer.Location);
2727 pre_decrement_expression
2728 : OP_DEC prefixed_unary_expression
2730 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2731 (Expression) $2, lexer.Location);
2735 multiplicative_expression
2736 : prefixed_unary_expression
2737 | multiplicative_expression STAR prefixed_unary_expression
2739 $$ = new Binary (Binary.Operator.Multiply,
2740 (Expression) $1, (Expression) $3, lexer.Location);
2742 | multiplicative_expression DIV prefixed_unary_expression
2744 $$ = new Binary (Binary.Operator.Division,
2745 (Expression) $1, (Expression) $3, lexer.Location);
2747 | multiplicative_expression PERCENT prefixed_unary_expression
2749 $$ = new Binary (Binary.Operator.Modulus,
2750 (Expression) $1, (Expression) $3, lexer.Location);
2755 : multiplicative_expression
2756 | additive_expression PLUS multiplicative_expression
2758 $$ = new Binary (Binary.Operator.Addition,
2759 (Expression) $1, (Expression) $3, lexer.Location);
2761 | additive_expression MINUS multiplicative_expression
2763 $$ = new Binary (Binary.Operator.Subtraction,
2764 (Expression) $1, (Expression) $3, lexer.Location);
2769 : additive_expression
2770 | shift_expression OP_SHIFT_LEFT additive_expression
2772 $$ = new Binary (Binary.Operator.LeftShift,
2773 (Expression) $1, (Expression) $3, lexer.Location);
2775 | shift_expression OP_SHIFT_RIGHT additive_expression
2777 $$ = new Binary (Binary.Operator.RightShift,
2778 (Expression) $1, (Expression) $3, lexer.Location);
2782 relational_expression
2784 | relational_expression OP_LT shift_expression
2786 $$ = new Binary (Binary.Operator.LessThan,
2787 (Expression) $1, (Expression) $3, lexer.Location);
2789 | relational_expression OP_GT shift_expression
2791 $$ = new Binary (Binary.Operator.GreaterThan,
2792 (Expression) $1, (Expression) $3, lexer.Location);
2794 | relational_expression OP_LE shift_expression
2796 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2797 (Expression) $1, (Expression) $3, lexer.Location);
2799 | relational_expression OP_GE shift_expression
2801 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2802 (Expression) $1, (Expression) $3, lexer.Location);
2804 | relational_expression IS type
2806 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
2808 | relational_expression AS type
2810 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
2815 : relational_expression
2816 | equality_expression OP_EQ relational_expression
2818 $$ = new Binary (Binary.Operator.Equality,
2819 (Expression) $1, (Expression) $3, lexer.Location);
2821 | equality_expression OP_NE relational_expression
2823 $$ = new Binary (Binary.Operator.Inequality,
2824 (Expression) $1, (Expression) $3, lexer.Location);
2829 : equality_expression
2830 | and_expression BITWISE_AND equality_expression
2832 $$ = new Binary (Binary.Operator.BitwiseAnd,
2833 (Expression) $1, (Expression) $3, lexer.Location);
2837 exclusive_or_expression
2839 | exclusive_or_expression CARRET and_expression
2841 $$ = new Binary (Binary.Operator.ExclusiveOr,
2842 (Expression) $1, (Expression) $3, lexer.Location);
2846 inclusive_or_expression
2847 : exclusive_or_expression
2848 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2850 $$ = new Binary (Binary.Operator.BitwiseOr,
2851 (Expression) $1, (Expression) $3, lexer.Location);
2855 conditional_and_expression
2856 : inclusive_or_expression
2857 | conditional_and_expression OP_AND inclusive_or_expression
2859 $$ = new Binary (Binary.Operator.LogicalAnd,
2860 (Expression) $1, (Expression) $3, lexer.Location);
2864 conditional_or_expression
2865 : conditional_and_expression
2866 | conditional_or_expression OP_OR conditional_and_expression
2868 $$ = new Binary (Binary.Operator.LogicalOr,
2869 (Expression) $1, (Expression) $3, lexer.Location);
2873 conditional_expression
2874 : conditional_or_expression
2875 | conditional_or_expression INTERR expression COLON expression
2877 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2881 assignment_expression
2882 : prefixed_unary_expression ASSIGN expression
2884 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2886 | prefixed_unary_expression OP_MULT_ASSIGN expression
2888 Location l = lexer.Location;
2890 $$ = new CompoundAssign (
2891 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
2893 | prefixed_unary_expression OP_DIV_ASSIGN expression
2895 Location l = lexer.Location;
2897 $$ = new CompoundAssign (
2898 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
2900 | prefixed_unary_expression OP_MOD_ASSIGN expression
2902 Location l = lexer.Location;
2904 $$ = new CompoundAssign (
2905 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
2907 | prefixed_unary_expression OP_ADD_ASSIGN expression
2909 Location l = lexer.Location;
2911 $$ = new CompoundAssign (
2912 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
2914 | prefixed_unary_expression OP_SUB_ASSIGN expression
2916 Location l = lexer.Location;
2918 $$ = new CompoundAssign (
2919 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
2921 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2923 Location l = lexer.Location;
2925 $$ = new CompoundAssign (
2926 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
2928 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2930 Location l = lexer.Location;
2932 $$ = new CompoundAssign (
2933 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
2935 | prefixed_unary_expression OP_AND_ASSIGN expression
2937 Location l = lexer.Location;
2939 $$ = new CompoundAssign (
2940 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
2942 | prefixed_unary_expression OP_OR_ASSIGN expression
2944 Location l = lexer.Location;
2946 $$ = new CompoundAssign (
2947 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
2949 | prefixed_unary_expression OP_XOR_ASSIGN expression
2951 Location l = lexer.Location;
2953 $$ = new CompoundAssign (
2954 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
2959 : conditional_expression
2960 | assignment_expression
2980 MemberName name = MakeName ((MemberName) $5);
2981 bool partial = (bool) $3;
2982 int mod_flags = (int) $2;
2985 ClassPart part = PartialContainer.CreatePart (
2986 current_namespace, current_container, name, mod_flags,
2987 (Attributes) $1, Kind.Class, lexer.Location);
2989 current_container = part.PartialContainer;
2990 current_class = part;
2992 if ((mod_flags & Modifiers.STATIC) != 0) {
2993 current_class = new StaticClass (
2994 current_namespace, current_container, name,
2995 mod_flags, (Attributes) $1, lexer.Location);
2997 current_class = new Class (
2998 current_namespace, current_container, name,
2999 mod_flags, (Attributes) $1, lexer.Location);
3002 current_container = current_class;
3003 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
3009 if (current_class.Name == "System.Object") {
3010 Report.Error (537, current_class.Location,
3011 "The class System.Object cannot have a base " +
3012 "class or implement an interface.");
3014 current_class.Bases = (ArrayList) $7;
3017 current_class.Register ();
3024 current_container = current_container.Parent;
3025 current_class = current_container;
3031 { $$ = (bool) false; }
3033 { $$ = (bool) true; }
3037 : /* empty */ { $$ = (int) 0; }
3043 | modifiers modifier
3048 if ((m1 & m2) != 0) {
3049 Location l = lexer.Location;
3050 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3052 $$ = (int) (m1 | m2);
3057 : NEW { $$ = Modifiers.NEW; }
3058 | PUBLIC { $$ = Modifiers.PUBLIC; }
3059 | PROTECTED { $$ = Modifiers.PROTECTED; }
3060 | INTERNAL { $$ = Modifiers.INTERNAL; }
3061 | PRIVATE { $$ = Modifiers.PRIVATE; }
3062 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3063 | SEALED { $$ = Modifiers.SEALED; }
3064 | STATIC { $$ = Modifiers.STATIC; }
3065 | READONLY { $$ = Modifiers.READONLY; }
3066 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3067 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3068 | EXTERN { $$ = Modifiers.EXTERN; }
3069 | VOLATILE { $$ = Modifiers.VOLATILE; }
3070 | UNSAFE { $$ = Modifiers.UNSAFE; }
3074 : /* empty */ { $$ = null; }
3075 | class_base { $$ = $1; }
3079 : COLON type_list { $$ = $2; }
3087 // A block is "contained" on the following places:
3089 // property_declaration as part of the accessor body (get/set)
3090 // operator_declaration
3091 // constructor_declaration
3092 // destructor_declaration
3093 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3098 if (current_block == null || create_toplevel_block){
3099 current_block = new ToplevelBlock (current_local_parameters, lexer.Location);
3101 current_block = new Block (current_block, current_local_parameters,
3102 lexer.Location, Location.Null);
3105 opt_statement_list CLOSE_BRACE
3107 while (current_block.Implicit)
3108 current_block = current_block.Parent;
3110 current_block.SetEndLocation (lexer.Location);
3111 current_block = current_block.Parent;
3122 | statement_list statement
3126 : declaration_statement
3128 if ($1 != null && (Block) $1 != current_block){
3129 current_block.AddStatement ((Statement) $1);
3130 current_block = (Block) $1;
3133 | valid_declaration_statement
3135 current_block.AddStatement ((Statement) $1);
3140 valid_declaration_statement
3143 | expression_statement
3144 | selection_statement
3145 | iteration_statement
3149 | unchecked_statement
3157 : valid_declaration_statement
3158 | declaration_statement
3160 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3165 Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3173 $$ = EmptyStatement.Value;
3180 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3182 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3183 current_block.AddStatement (labeled);
3188 declaration_statement
3189 : local_variable_declaration SEMICOLON
3192 DictionaryEntry de = (DictionaryEntry) $1;
3194 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3198 | local_constant_declaration SEMICOLON
3201 DictionaryEntry de = (DictionaryEntry) $1;
3203 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3209 * The following is from Rhys' grammar:
3210 * > Types in local variable declarations must be recognized as
3211 * > expressions to prevent reduce/reduce errors in the grammar.
3212 * > The expressions are converted into types during semantic analysis.
3215 : primary_expression opt_rank_specifier
3217 // FIXME: Do something smart here regarding the composition of the type.
3219 // Ok, the above "primary_expression" is there to get rid of
3220 // both reduce/reduce and shift/reduces in the grammar, it should
3221 // really just be "type_name". If you use type_name, a reduce/reduce
3222 // creeps up. If you use namespace_or_type_name (which is all we need
3223 // really) two shift/reduces appear.
3226 // So the super-trick is that primary_expression
3227 // can only be either a SimpleName or a MemberAccess.
3228 // The MemberAccess case arises when you have a fully qualified type-name like :
3230 // SimpleName is when you have
3233 Expression expr = (Expression) $1;
3234 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3235 Error_ExpectingTypeName (lexer.Location, expr);
3239 // So we extract the string corresponding to the SimpleName
3243 if ((string) $2 == "")
3246 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3249 | builtin_types opt_rank_specifier
3251 if ((string) $2 == "")
3254 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3258 local_variable_pointer_type
3259 : primary_expression STAR
3261 Expression expr = (Expression) $1;
3262 Location l = lexer.Location;
3264 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3265 Error_ExpectingTypeName (l, expr);
3269 $$ = new ComposedCast ((Expression) $1, "*", l);
3271 | builtin_types STAR
3273 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3277 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3279 | local_variable_pointer_type STAR
3281 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3285 local_variable_declaration
3286 : local_variable_type variable_declarators
3289 $$ = new DictionaryEntry ($1, $2);
3293 | local_variable_pointer_type opt_rank_specifier variable_declarators
3298 if ((string) $2 == "")
3299 t = (Expression) $1;
3301 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3302 $$ = new DictionaryEntry (t, $3);
3308 local_constant_declaration
3309 : CONST local_variable_type constant_declarators
3312 $$ = new DictionaryEntry ($2, $3);
3318 expression_statement
3319 : statement_expression SEMICOLON
3326 // We have to do the wrapping here and not in the case above,
3327 // because statement_expression is used for example in for_statement
3329 statement_expression
3330 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3331 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3332 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3333 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3334 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3335 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3336 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3338 Report.Error (1002, lexer.Location, "Expecting `;'");
3343 object_creation_expression
3344 : object_or_delegate_creation_expression
3345 { note ("complain if this is a delegate maybe?"); }
3354 : if_statement_open if_statement_rest
3363 oob_stack.Push (lexer.Location);
3368 : boolean_expression CLOSE_PARENS
3371 Location l = (Location) oob_stack.Pop ();
3373 $$ = new If ((Expression) $1, (Statement) $3, l);
3375 if (RootContext.WarningLevel >= 3){
3376 if ($3 == EmptyStatement.Value)
3377 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3381 | boolean_expression CLOSE_PARENS
3382 embedded_statement ELSE embedded_statement
3384 Location l = (Location) oob_stack.Pop ();
3386 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
3391 : SWITCH OPEN_PARENS
3393 oob_stack.Push (lexer.Location);
3394 switch_stack.Push (current_block);
3396 expression CLOSE_PARENS
3399 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
3400 current_block = (Block) switch_stack.Pop ();
3416 Report.Error (1522, lexer.Location, "Empty switch block");
3424 ArrayList sections = new ArrayList (4);
3429 | switch_sections switch_section
3431 ArrayList sections = (ArrayList) $1;
3441 current_block = current_block.CreateSwitchBlock (lexer.Location);
3445 Block topmost = current_block;
3447 while (topmost.Implicit)
3448 topmost = topmost.Parent;
3449 $$ = new SwitchSection ((ArrayList) $1, topmost);
3456 ArrayList labels = new ArrayList (4);
3461 | switch_labels switch_label
3463 ArrayList labels = (ArrayList) ($1);
3471 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3472 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3475 1523, lexer.Location,
3476 "The keyword case or default must precede code in switch block");
3490 oob_stack.Push (lexer.Location);
3492 boolean_expression CLOSE_PARENS embedded_statement
3494 Location l = (Location) oob_stack.Pop ();
3495 $$ = new While ((Expression) $4, (Statement) $6, l);
3497 if (RootContext.WarningLevel >= 3){
3498 if ($6 == EmptyStatement.Value)
3499 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3505 : DO embedded_statement
3507 oob_stack.Push (lexer.Location);
3509 boolean_expression CLOSE_PARENS SEMICOLON
3511 Location l = (Location) oob_stack.Pop ();
3513 $$ = new Do ((Statement) $2, (Expression) $6, l);
3519 opt_for_initializer SEMICOLON
3521 Block assign_block = new Block (current_block);
3522 current_block = assign_block;
3524 if ($3 is DictionaryEntry){
3525 DictionaryEntry de = (DictionaryEntry) $3;
3527 Expression type = (Expression) de.Key;
3528 ArrayList var_declarators = (ArrayList) de.Value;
3530 foreach (VariableDeclaration decl in var_declarators){
3534 vi = current_block.AddVariable (
3535 type, decl.identifier, current_local_parameters, decl.Location);
3539 Location l = lexer.Location;
3541 if (decl.expression_or_array_initializer is Expression){
3542 expr = (Expression) decl.expression_or_array_initializer;
3543 } else if (decl.expression_or_array_initializer == null) {
3546 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3547 expr = new ArrayCreation (type, "", init, decl.Location);
3550 LocalVariableReference var;
3551 var = new LocalVariableReference (assign_block, decl.identifier, l);
3554 Assign a = new Assign (var, expr, decl.Location);
3556 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3562 oob_stack.Push (lexer.Location);
3564 opt_for_condition SEMICOLON
3565 opt_for_iterator CLOSE_PARENS
3568 Location l = (Location) oob_stack.Pop ();
3570 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3572 if (RootContext.WarningLevel >= 3){
3573 if ($10 == EmptyStatement.Value)
3574 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3577 current_block.AddStatement (f);
3578 while (current_block.Implicit)
3579 current_block = current_block.Parent;
3581 current_block = current_block.Parent;
3586 : /* empty */ { $$ = EmptyStatement.Value; }
3591 : local_variable_declaration
3592 | statement_expression_list
3596 : /* empty */ { $$ = null; }
3597 | boolean_expression
3601 : /* empty */ { $$ = EmptyStatement.Value; }
3606 : statement_expression_list
3609 statement_expression_list
3610 : statement_expression
3612 // CHANGE: was `null'
3613 Block b = new Block (current_block, Block.Flags.Implicit);
3615 b.AddStatement ((Statement) $1);
3618 | statement_expression_list COMMA statement_expression
3620 Block b = (Block) $1;
3622 b.AddStatement ((Statement) $3);
3628 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
3630 Report.Error (230, lexer.Location, "Type and identifier are both required in a foreach statement");
3633 | FOREACH OPEN_PARENS type IDENTIFIER IN
3635 oob_stack.Push (lexer.Location);
3637 expression CLOSE_PARENS
3639 oob_stack.Push (current_block);
3641 Block foreach_block = new Block (current_block);
3642 LocalVariableReference v = null;
3643 Location l = lexer.Location;
3646 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3650 // Get a writable reference to this read-only variable.
3651 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3653 current_block = foreach_block;
3656 oob_stack.Push (current_block);
3660 Block foreach_block = (Block) oob_stack.Pop ();
3661 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3662 Block prev_block = (Block) oob_stack.Pop ();
3663 Location l = (Location) oob_stack.Pop ();
3665 current_block = prev_block;
3668 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3669 foreach_block.AddStatement (f);
3678 | continue_statement
3688 $$ = new Break (lexer.Location);
3693 : CONTINUE SEMICOLON
3695 $$ = new Continue (lexer.Location);
3700 : GOTO IDENTIFIER SEMICOLON
3702 $$ = new Goto (current_block, (string) $2, lexer.Location);
3704 | GOTO CASE constant_expression SEMICOLON
3706 $$ = new GotoCase ((Expression) $3, lexer.Location);
3708 | GOTO DEFAULT SEMICOLON
3710 $$ = new GotoDefault (lexer.Location);
3715 : RETURN opt_expression SEMICOLON
3717 $$ = new Return ((Expression) $2, lexer.Location);
3722 : THROW opt_expression SEMICOLON
3724 $$ = new Throw ((Expression) $2, lexer.Location);
3729 : IDENTIFIER RETURN expression SEMICOLON
3731 string s = (string) $1;
3733 Report.Error (1003, lexer.Location, "; expected");
3736 if (RootContext.Version == LanguageVersion.ISO_1){
3737 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
3740 if (iterator_container == null){
3741 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3744 iterator_container.SetYields ();
3745 $$ = new Yield ((Expression) $3, lexer.Location);
3748 | IDENTIFIER BREAK SEMICOLON
3750 string s = (string) $1;
3752 Report.Error (1003, lexer.Location, "; expected");
3755 if (RootContext.Version == LanguageVersion.ISO_1){
3756 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
3759 if (iterator_container == null){
3760 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3763 iterator_container.SetYields ();
3764 $$ = new YieldBreak (lexer.Location);
3775 : TRY block catch_clauses
3779 ArrayList c = (ArrayList)$3;
3780 for (int i = 0; i < c.Count; ++i) {
3781 Catch cc = (Catch) c [i];
3783 if (i != c.Count - 1)
3784 Report.Error (1017, cc.loc, "Empty catch block must be the last in a series of catch blocks");
3791 // Now s contains the list of specific catch clauses
3792 // and g contains the general one.
3794 $$ = new Try ((Block) $2, c, g, null, lexer.Location);
3796 | TRY block opt_catch_clauses FINALLY block
3799 ArrayList s = new ArrayList (4);
3800 ArrayList catch_list = (ArrayList) $3;
3802 if (catch_list != null){
3803 foreach (Catch cc in catch_list) {
3811 $$ = new Try ((Block) $2, s, g, (Block) $5, lexer.Location);
3815 Report.Error (1524, lexer.Location, "Expected catch or finally");
3820 : /* empty */ { $$ = null; }
3827 ArrayList l = new ArrayList (4);
3832 | catch_clauses catch_clause
3834 ArrayList l = (ArrayList) $1;
3842 : /* empty */ { $$ = null; }
3847 : CATCH opt_catch_args
3849 Expression type = null;
3853 DictionaryEntry cc = (DictionaryEntry) $2;
3854 type = (Expression) cc.Key;
3855 id = (string) cc.Value;
3858 ArrayList one = new ArrayList (4);
3859 Location loc = lexer.Location;
3861 one.Add (new VariableDeclaration (id, null, loc));
3864 current_block = new Block (current_block);
3865 Block b = declare_local_variables (type, one, loc);
3870 Expression type = null;
3874 DictionaryEntry cc = (DictionaryEntry) $2;
3875 type = (Expression) cc.Key;
3876 id = (string) cc.Value;
3880 // FIXME: I can change this for an assignment.
3882 while (current_block != (Block) $1)
3883 current_block = current_block.Parent;
3888 $$ = new Catch (type, id , (Block) $4, lexer.Location);
3893 : /* empty */ { $$ = null; }
3898 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3900 $$ = new DictionaryEntry ($2, $3);
3907 $$ = new Checked ((Block) $2);
3914 $$ = new Unchecked ((Block) $2);
3921 if (!RootContext.Unsafe){
3922 Report.Error (227, lexer.Location,
3923 "Unsafe code can only be used if --unsafe is used");
3926 $$ = new Unsafe ((Block) $3);
3932 type fixed_pointer_declarators
3935 ArrayList list = (ArrayList) $4;
3936 Expression type = (Expression) $3;
3937 Location l = lexer.Location;
3938 int top = list.Count;
3940 Block assign_block = new Block (current_block);
3941 current_block = assign_block;
3943 for (int i = 0; i < top; i++){
3944 Pair p = (Pair) list [i];
3947 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3961 Location l = (Location) oob_stack.Pop ();
3963 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
3965 if (RootContext.WarningLevel >= 3){
3966 if ($7 == EmptyStatement.Value)
3967 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3970 current_block.AddStatement (f);
3971 while (current_block.Implicit)
3972 current_block = current_block.Parent;
3974 current_block = current_block.Parent;
3978 fixed_pointer_declarators
3979 : fixed_pointer_declarator {
3980 ArrayList declarators = new ArrayList (4);
3982 declarators.Add ($1);
3985 | fixed_pointer_declarators COMMA fixed_pointer_declarator
3987 ArrayList declarators = (ArrayList) $1;
3989 declarators.Add ($3);
3994 fixed_pointer_declarator
3995 : IDENTIFIER ASSIGN expression
3997 $$ = new Pair ($1, $3);
4001 Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
4007 : LOCK OPEN_PARENS expression CLOSE_PARENS
4013 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4018 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4020 Block assign_block = new Block (current_block);
4021 current_block = assign_block;
4023 oob_stack.Push (lexer.Location);
4025 if ($3 is DictionaryEntry){
4026 DictionaryEntry de = (DictionaryEntry) $3;
4027 Location l = lexer.Location;
4029 Expression type = (Expression) de.Key;
4030 ArrayList var_declarators = (ArrayList) de.Value;
4032 ArrayList vars = new ArrayList (4);
4034 foreach (VariableDeclaration decl in var_declarators){
4036 LocalInfo vi = current_block.AddVariable (
4037 type, decl.identifier,
4038 current_local_parameters, decl.Location);
4044 if (decl.expression_or_array_initializer is Expression){
4045 expr = (Expression) decl.expression_or_array_initializer;
4047 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4049 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4052 expr = new ArrayCreation (type, "", init, decl.Location);
4055 LocalVariableReference var;
4057 // Get a writable reference to this read-only variable.
4058 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4060 // This is so that it is not a warning on using variables
4063 vars.Add (new DictionaryEntry (var, expr));
4065 // Assign a = new Assign (var, expr, decl.Location);
4066 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4068 $3 = new DictionaryEntry (type, vars);
4073 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
4074 current_block.AddStatement (u);
4075 while (current_block.Implicit)
4076 current_block = current_block.Parent;
4078 current_block = current_block.Parent;
4082 resource_acquisition
4083 : local_variable_declaration
4090 // A class used to pass around variable declarations and constants
4092 public class VariableDeclaration {
4093 public string identifier;
4094 public object expression_or_array_initializer;
4095 public Location Location;
4096 public Attributes OptAttributes;
4098 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4100 this.identifier = id;
4101 this.expression_or_array_initializer = eoai;
4103 this.OptAttributes = opt_attrs;
4106 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4112 /// Used to pass around interface property information
4114 public class InterfaceAccessorInfo {
4116 public readonly Accessor Get, Set;
4118 public InterfaceAccessorInfo (bool has_get, bool has_set,
4119 Attributes get_attrs, Attributes set_attrs, Location get_loc, Location set_loc)
4122 Get = new Accessor (null, get_attrs, get_loc);
4124 Set = new Accessor (null, set_attrs, set_loc);
4130 // A class used to hold info about an indexer declarator
4132 public class IndexerDeclaration {
4133 public Expression type;
4134 public MemberName interface_type;
4135 public Parameters param_list;
4137 public IndexerDeclaration (Expression type, MemberName interface_type,
4138 Parameters param_list)
4141 this.interface_type = interface_type;
4142 this.param_list = param_list;
4147 // We use this when we do not have an object in advance that is an IIteratorContainer
4149 public class SimpleIteratorContainer : IIteratorContainer {
4152 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4157 public static SimpleIteratorContainer GetSimple () {
4158 Simple.Yields = false;
4162 public void SetYields () { Yields = true; }
4166 // A class used to hold info about an operator declarator
4168 public class OperatorDeclaration {
4169 public Operator.OpType optype;
4170 public Expression ret_type, arg1type, arg2type;
4171 public string arg1name, arg2name;
4172 public Location location;
4174 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4175 Expression arg1type, string arg1name,
4176 Expression arg2type, string arg2name, Location location)
4179 this.ret_type = ret_type;
4180 this.arg1type = arg1type;
4181 this.arg1name = arg1name;
4182 this.arg2type = arg2type;
4183 this.arg2name = arg2name;
4184 this.location = location;
4189 void Error_ExpectingTypeName (Location l, Expression expr)
4191 if (expr is Invocation){
4192 Report.Error (1002, l, "; expected");
4194 Report.Error (-1, l, "Invalid Type definition");
4199 // Given the @class_name name, it creates a fully qualified name
4200 // based on the containing declaration space
4203 MakeName (MemberName class_name)
4205 string ns = current_namespace.FullName;
4207 if (current_container.Name == ""){
4209 return new MemberName (new MemberName (ns), class_name);
4213 return new MemberName (current_container.MemberName, class_name);
4217 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4219 Block implicit_block;
4220 ArrayList inits = null;
4223 // We use the `Used' property to check whether statements
4224 // have been added to the current block. If so, we need
4225 // to create another block to contain the new declaration
4226 // otherwise, as an optimization, we use the same block to
4227 // add the declaration.
4229 // FIXME: A further optimization is to check if the statements
4230 // that were added were added as part of the initialization
4231 // below. In which case, no other statements have been executed
4232 // and we might be able to reduce the number of blocks for
4233 // situations like this:
4235 // int j = 1; int k = j + 1;
4237 if (current_block.Used)
4238 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4240 implicit_block = current_block;
4242 foreach (VariableDeclaration decl in variable_declarators){
4244 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4245 if (decl.expression_or_array_initializer != null){
4247 inits = new ArrayList (4);
4254 return implicit_block;
4256 foreach (VariableDeclaration decl in inits){
4260 if (decl.expression_or_array_initializer is Expression){
4261 expr = (Expression) decl.expression_or_array_initializer;
4264 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4266 expr = new ArrayCreation (type, "", init, decl.Location);
4269 LocalVariableReference var;
4270 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4272 assign = new Assign (var, expr, decl.Location);
4274 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4277 return implicit_block;
4280 Block declare_local_constants (Expression type, ArrayList declarators)
4282 Block implicit_block;
4284 if (current_block.Used)
4285 implicit_block = new Block (current_block, Block.Flags.Implicit);
4287 implicit_block = current_block;
4289 foreach (VariableDeclaration decl in declarators){
4290 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4291 current_local_parameters, decl.Location);
4294 return implicit_block;
4297 void CheckAttributeTarget (string a)
4301 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4305 Location l = lexer.Location;
4306 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4312 void CheckUnaryOperator (Operator.OpType op)
4316 case Operator.OpType.LogicalNot:
4317 case Operator.OpType.OnesComplement:
4318 case Operator.OpType.Increment:
4319 case Operator.OpType.Decrement:
4320 case Operator.OpType.True:
4321 case Operator.OpType.False:
4322 case Operator.OpType.Addition:
4323 case Operator.OpType.Subtraction:
4328 Location l = lexer.Location;
4329 Report.Error (1019, l, "Overloadable unary operator expected");
4335 void CheckBinaryOperator (Operator.OpType op)
4339 case Operator.OpType.Addition:
4340 case Operator.OpType.Subtraction:
4341 case Operator.OpType.Multiply:
4342 case Operator.OpType.Division:
4343 case Operator.OpType.Modulus:
4344 case Operator.OpType.BitwiseAnd:
4345 case Operator.OpType.BitwiseOr:
4346 case Operator.OpType.ExclusiveOr:
4347 case Operator.OpType.LeftShift:
4348 case Operator.OpType.RightShift:
4349 case Operator.OpType.Equality:
4350 case Operator.OpType.Inequality:
4351 case Operator.OpType.GreaterThan:
4352 case Operator.OpType.LessThan:
4353 case Operator.OpType.GreaterThanOrEqual:
4354 case Operator.OpType.LessThanOrEqual:
4358 Location l = lexer.Location;
4359 Report.Error (1020, l, "Overloadable binary operator expected");
4365 void syntax_error (Location l, string msg)
4367 Report.Error (1003, l, "Syntax error, " + msg);
4370 void output (string s)
4372 Console.WriteLine (s);
4375 void note (string s)
4377 // Used to put annotations
4382 public Tokenizer Lexer {
4388 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4390 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4391 this.name = file.Name;
4393 current_container = RootContext.Tree.Types;
4394 current_container.NamespaceEntry = current_namespace;
4395 oob_stack = new Stack ();
4396 switch_stack = new Stack ();
4398 lexer = new Tokenizer (reader, file, defines);
4401 public void parse ()
4404 if (yacc_verbose_flag)
4405 yyparse (lexer, new yydebug.yyDebugSimple ());
4408 Tokenizer tokenizer = lexer as Tokenizer;
4409 tokenizer.cleanup ();
4410 } catch (Exception e){
4411 // Please do not remove this, it is used during debugging
4414 Console.WriteLine (e);
4415 Report.Error (-25, lexer.Location, "Parsing error");
4416 if (Driver.parser_verbose)
4417 Console.WriteLine (e);
4421 void CheckToken (int error, int yyToken, string msg)
4423 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4424 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4427 Report.Error (error, lexer.Location, msg);
4430 void CheckIdentifierToken (int yyToken)
4432 CheckToken (1041, yyToken, "Identifier expected");