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
43 Block current_block, top_current_block;
46 /// This is used by the unary_expression code to resolve
47 /// a name against a parameter.
49 Parameters current_local_parameters;
52 /// Using during property parsing to describe the implicit
53 /// value parameter that is passed to the "set" and "get"accesor
54 /// methods (properties and indexers).
56 Expression implicit_value_parameter_type;
57 Parameters indexer_parameters;
60 /// Used to determine if we are parsing the get/set pair
61 /// of an indexer or a property
66 /// An out-of-band stack.
75 public bool yacc_verbose_flag;
77 // Name of the file we are parsing
86 /// Current attribute target
87 string current_attr_target;
89 /// assembly and module attribute definition is enabled
90 bool global_attrs_enabled = true;
94 %token NONE /* This token is never returned by our lexer */
95 %token ERROR // This is used not by the parser, but by the tokenizer.
99 *These are the C# keywords
185 /* C# keywords which are not really keywords */
191 /* C# single character operators/punctuation. */
192 %token OPEN_BRACE "{"
193 %token CLOSE_BRACE "}"
194 %token OPEN_BRACKET "["
195 %token CLOSE_BRACKET "]"
196 %token OPEN_PARENS "("
197 %token CLOSE_PARENS ")"
210 %token BITWISE_AND "&"
211 %token BITWISE_OR "|"
218 /* C# multi-character operators. */
221 %token OP_SHIFT_LEFT "<<"
222 %token OP_SHIFT_RIGHT ">>"
229 %token OP_MULT_ASSIGN "*="
230 %token OP_DIV_ASSIGN "/="
231 %token OP_MOD_ASSIGN "%="
232 %token OP_ADD_ASSIGN "+="
233 %token OP_SUB_ASSIGN "-="
234 %token OP_SHIFT_LEFT_ASSIGN "<<="
235 %token OP_SHIFT_RIGHT_ASSIGN ">>="
236 %token OP_AND_ASSIGN "&="
237 %token OP_XOR_ASSIGN "^="
238 %token OP_OR_ASSIGN "|="
242 %token LITERAL_INTEGER "int literal"
243 %token LITERAL_FLOAT "float literal"
244 %token LITERAL_DOUBLE "double literal"
245 %token LITERAL_DECIMAL "decimal literal"
246 %token LITERAL_CHARACTER "character literal"
247 %token LITERAL_STRING "string literal"
250 %token CLOSE_PARENS_CAST
251 %token CLOSE_PARENS_NO_CAST
252 %token CLOSE_PARENS_OPEN_PARENS
253 %token CLOSE_PARENS_MINUS
255 /* Add precedence rules to solve dangling else s/r conflict */
264 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
266 %left STAR DIV PERCENT
267 %right BANG CARRET UMINUS
268 %nonassoc OP_INC OP_DEC
270 %left OPEN_BRACKET OPEN_BRACE
274 %start compilation_unit
278 : outer_declarations opt_EOF
279 | outer_declarations global_attributes opt_EOF
280 | global_attributes opt_EOF
281 | opt_EOF /* allow empty files */
291 | outer_declarations outer_declaration
296 | namespace_member_declaration
301 | using_directives using_directive
305 : using_alias_directive
306 | using_namespace_directive
309 using_alias_directive
310 : USING IDENTIFIER ASSIGN
311 namespace_or_type_name SEMICOLON
313 current_namespace.UsingAlias ((string) $2, (MemberName) $4, lexer.Location);
316 CheckIdentifierToken (yyToken);
320 using_namespace_directive
321 : USING namespace_name SEMICOLON
323 current_namespace.Using ((string) $2, lexer.Location);
328 // Strictly speaking, namespaces don't have attributes but
329 // we parse global attributes along with namespace declarations and then
332 namespace_declaration
333 : opt_attributes NAMESPACE namespace_or_type_name
336 Report.Error(1518, Lexer.Location, "Attributes cannot be applied to namespaces."
337 + " Expected class, delegate, enum, interface, or struct");
340 MemberName name = (MemberName) $3;
342 if ((current_namespace.Parent != null) && (name.Left != null)) {
343 Report.Error (134, lexer.Location,
344 "Cannot use qualified namespace names in nested " +
345 "namespace declarations");
348 current_namespace = new NamespaceEntry (
349 current_namespace, file, name.GetName (), lexer.Location);
351 namespace_body opt_semicolon
353 current_namespace = current_namespace.Parent;
368 : namespace_or_type_name {
369 MemberName name = (MemberName) $1;
371 $$ = name.GetName ();
378 opt_namespace_member_declarations
389 opt_namespace_member_declarations
391 | namespace_member_declarations
394 namespace_member_declarations
395 : namespace_member_declaration
396 | namespace_member_declarations namespace_member_declaration
399 namespace_member_declaration
406 Class c = (Class) $1;
407 mod_flags = c.ModFlags;
409 } else if ($1 is Struct){
410 Struct s = (Struct) $1;
411 mod_flags = s.ModFlags;
416 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
418 1527, lexer.Location,
419 "Namespace elements cant be explicitly " +
420 "declared private or protected in `" + name + "'");
422 current_namespace.DeclarationFound = true;
424 | namespace_declaration {
425 current_namespace.DeclarationFound = true;
428 | field_declaration {
429 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
431 | method_declaration {
432 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
439 | interface_declaration
441 | delegate_declaration
443 // Enable this when we have handled all errors, because this acts as a generic fallback
446 // Console.WriteLine ("Token=" + yyToken);
447 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
459 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
467 global_attrs_enabled = false;
472 global_attrs_enabled = false;
481 ArrayList sect = (ArrayList) $1;
483 if (global_attrs_enabled) {
484 if (current_attr_target == "module") {
485 CodeGen.Module.AddAttributes (sect);
487 } else if (current_attr_target == "assembly") {
488 CodeGen.Assembly.AddAttributes (sect);
491 $$ = new Attributes (sect);
494 $$ = new Attributes (sect);
496 current_attr_target = null;
498 | attribute_sections attribute_section
500 Attributes attrs = $1 as Attributes;
501 ArrayList sect = (ArrayList) $2;
503 if (global_attrs_enabled) {
504 if (current_attr_target == "module") {
505 CodeGen.Module.AddAttributes (sect);
507 } else if (current_attr_target == "assembly") {
508 CodeGen.Assembly.AddAttributes (sect);
512 attrs = new Attributes (sect);
514 attrs.AddAttributes (sect);
518 attrs = new Attributes (sect);
520 attrs.AddAttributes (sect);
523 current_attr_target = null;
528 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
532 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
538 attribute_target_specifier
539 : attribute_target COLON
541 current_attr_target = (string)$1;
549 CheckAttributeTarget ((string) $1);
552 | EVENT { $$ = "event"; }
553 | RETURN { $$ = "return"; }
559 ArrayList attrs = new ArrayList (4);
565 | attribute_list COMMA attribute
567 ArrayList attrs = (ArrayList) $1;
579 opt_attribute_arguments
581 MemberName mname = (MemberName) $1;
582 string name = mname.GetName ();
583 if (current_attr_target == "assembly" || current_attr_target == "module")
584 $$ = new GlobalAttribute (current_container, current_attr_target,
585 name, (ArrayList) $3, (Location) $2);
587 $$ = new Attribute (current_attr_target, name, (ArrayList) $3,
593 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
596 opt_attribute_arguments
597 : /* empty */ { $$ = null; }
598 | OPEN_PARENS attribute_arguments CLOSE_PARENS
606 : opt_positional_argument_list
611 ArrayList args = new ArrayList (4);
617 | positional_argument_list COMMA named_argument_list
619 ArrayList args = new ArrayList (4);
625 | named_argument_list
627 ArrayList args = new ArrayList (4);
636 opt_positional_argument_list
637 : /* empty */ { $$ = null; }
638 | positional_argument_list
641 positional_argument_list
644 ArrayList args = new ArrayList (4);
645 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
649 | positional_argument_list COMMA expression
651 ArrayList args = (ArrayList) $1;
652 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
661 ArrayList args = new ArrayList (4);
666 | named_argument_list COMMA named_argument
668 ArrayList args = (ArrayList) $1;
673 | named_argument_list COMMA expression
675 Report.Error (1016, lexer.Location, "Named attribute argument expected");
681 : IDENTIFIER ASSIGN expression
683 $$ = new DictionaryEntry (
685 new Argument ((Expression) $3, Argument.AType.Expression));
691 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
694 opt_class_member_declarations
696 | class_member_declarations
699 class_member_declarations
700 : class_member_declaration
701 | class_member_declarations
702 class_member_declaration
705 class_member_declaration
706 : constant_declaration // done
707 | field_declaration // done
708 | method_declaration // done
709 | property_declaration // done
710 | event_declaration // done
711 | indexer_declaration // done
712 | operator_declaration // done
713 | constructor_declaration // done
714 | destructor_declaration // done
724 MemberName name = MakeName ((MemberName) $5);
725 bool partial = (bool) $3;
728 ClassPart part = PartialContainer.CreatePart (
729 current_namespace, current_container, name, (int) $2,
730 (Attributes) $1, Kind.Struct, lexer.Location);
732 current_container = part.PartialContainer;
733 current_class = part;
735 current_class = new Struct (
736 current_namespace, current_container, name, (int) $2,
737 (Attributes) $1, lexer.Location);
739 current_container = current_class;
740 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
746 current_class.Bases = (ArrayList) $7;
748 current_class.Register ();
755 current_container = current_container.Parent;
756 current_class = current_container;
758 | opt_attributes opt_modifiers opt_partial STRUCT error {
759 CheckIdentifierToken (yyToken);
764 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
767 opt_struct_member_declarations
769 | struct_member_declarations
772 struct_member_declarations
773 : struct_member_declaration
774 | struct_member_declarations struct_member_declaration
777 struct_member_declaration
778 : constant_declaration
781 | property_declaration
783 | indexer_declaration
784 | operator_declaration
785 | constructor_declaration
789 * This is only included so we can flag error 575:
790 * destructors only allowed on class types
792 | destructor_declaration
803 int modflags = (int) $2;
804 foreach (VariableDeclaration constant in (ArrayList) $5){
805 Location l = constant.Location;
806 if ((modflags & Modifiers.STATIC) != 0) {
807 Report.Error (504, l, "The constant '{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
811 Const c = new Const (
812 current_class, (Expression) $4, (string) constant.identifier,
813 (Expression) constant.expression_or_array_initializer, modflags,
816 current_container.AddConstant (c);
822 : constant_declarator
824 ArrayList constants = new ArrayList (4);
829 | constant_declarators COMMA constant_declarator
832 ArrayList constants = (ArrayList) $1;
839 : IDENTIFIER ASSIGN constant_expression
841 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
845 // A const field requires a value to be provided
846 Report.Error (145, lexer.Location, "A const field requires a value to be provided");
858 Expression type = (Expression) $3;
861 foreach (VariableDeclaration var in (ArrayList) $4){
862 Location l = var.Location;
864 Field field = new Field (current_class, type, mod, var.identifier,
865 var.expression_or_array_initializer,
868 current_container.AddField (field);
876 Report.Error (670, lexer.Location, "void type is not allowed for fields");
881 : variable_declarator
883 ArrayList decl = new ArrayList (4);
887 | variable_declarators COMMA variable_declarator
889 ArrayList decls = (ArrayList) $1;
896 : IDENTIFIER ASSIGN variable_initializer
898 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
902 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
915 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
917 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
921 Report.Error (1575, lexer.Location, "A stackalloc expression requires [] after type");
928 iterator_container = (IIteratorContainer) $1;
932 Method method = (Method) $1;
933 Block b = (Block) $3;
934 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
937 if ((method.ModFlags & extern_abstract) == 0){
939 501, lexer.Location, current_container.MakeName (method.Name) +
940 "must declare a body because it is not marked abstract or extern");
943 if ((method.ModFlags & Modifiers.EXTERN) != 0){
945 179, lexer.Location, current_container.MakeName (method.Name) +
946 " is declared extern, but has a body");
950 method.Block = (ToplevelBlock) $3;
951 current_container.AddMethod (method);
953 current_local_parameters = null;
954 iterator_container = null;
968 1585, lexer.Location, "Member modifier `" +
969 Modifiers.Name (i) + "' must precede member type and name");
980 type namespace_or_type_name
981 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
983 MemberName name = (MemberName) $4;
985 Method method = new Method (current_class, (Expression) $3, (int) $2,
986 false, name, (Parameters) $6, (Attributes) $1,
989 current_local_parameters = (Parameters) $6;
995 VOID namespace_or_type_name
996 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
998 MemberName name = (MemberName) $4;
1000 Method method = new Method (current_class, TypeManager.system_void_expr,
1001 (int) $2, false, name, (Parameters) $6,
1002 (Attributes) $1, lexer.Location);
1004 current_local_parameters = (Parameters) $6;
1010 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1012 Report.Error (1585, lexer.Location,
1013 String.Format ("Modifier {0} should appear before type",
1014 Modifiers.Name ((int) $4)));
1015 MemberName name = (MemberName) $4;
1017 Method method = new Method (current_class, TypeManager.system_void_expr,
1018 0, false, name, (Parameters) $7, (Attributes) $1,
1021 current_local_parameters = (Parameters) $7;
1028 | SEMICOLON { $$ = null; }
1031 opt_formal_parameter_list
1032 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1033 | formal_parameter_list
1036 formal_parameter_list
1039 ArrayList pars_list = (ArrayList) $1;
1041 Parameter [] pars = new Parameter [pars_list.Count];
1042 pars_list.CopyTo (pars);
1044 $$ = new Parameters (pars, null, lexer.Location);
1046 | fixed_parameters COMMA parameter_array
1048 ArrayList pars_list = (ArrayList) $1;
1050 Parameter [] pars = new Parameter [pars_list.Count];
1051 pars_list.CopyTo (pars);
1053 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
1055 | fixed_parameters COMMA ARGLIST
1057 ArrayList pars_list = (ArrayList) $1;
1059 Parameter [] pars = new Parameter [pars_list.Count];
1060 pars_list.CopyTo (pars);
1062 $$ = new Parameters (pars, true, lexer.Location);
1064 | parameter_array COMMA fixed_parameters
1066 Report.Error (231, lexer.Location, "A params parameter must be the last parameter in a formal parameter list");
1069 | ARGLIST COMMA fixed_parameters
1071 Report.Error (257, lexer.Location, "An __arglist parameter must be the last parameter in a formal parameter list");
1076 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
1080 $$ = new Parameters (null, true, lexer.Location);
1087 ArrayList pars = new ArrayList (4);
1092 | fixed_parameters COMMA fixed_parameter
1094 ArrayList pars = (ArrayList) $1;
1103 opt_parameter_modifier
1107 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
1110 opt_parameter_modifier
1113 Report.Error (1001, lexer.Location, "Identifier expected");
1117 opt_parameter_modifier
1120 CheckIdentifierToken (yyToken);
1124 opt_parameter_modifier
1130 Report.Error (241, lexer.Location, "Default parameter specifiers are not permitted");
1135 opt_parameter_modifier
1136 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1137 | parameter_modifier
1141 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1142 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1146 : opt_attributes PARAMS type IDENTIFIER
1148 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1149 note ("type must be a single-dimension array type");
1151 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1153 Report.Error (1611, lexer.Location, "The params parameter cannot be declared as ref or out");
1156 | opt_attributes PARAMS type error {
1157 CheckIdentifierToken (yyToken);
1162 property_declaration
1165 type namespace_or_type_name
1168 implicit_value_parameter_type = (Expression) $3;
1170 lexer.PropertyParsing = true;
1172 $$ = lexer.Location;
1174 iterator_container = SimpleIteratorContainer.GetSimple ();
1176 accessor_declarations
1178 lexer.PropertyParsing = false;
1183 Pair pair = (Pair) $7;
1184 Accessor get_block = (Accessor) pair.First;
1185 Accessor set_block = (Accessor) pair.Second;
1187 Location loc = (Location) $6;
1188 MemberName name = (MemberName) $4;
1190 prop = new Property (current_class, (Expression) $3, (int) $2, false,
1191 name, (Attributes) $1, get_block, set_block, loc);
1192 if (SimpleIteratorContainer.Simple.Yields)
1195 current_container.AddProperty (prop);
1196 implicit_value_parameter_type = null;
1197 iterator_container = null;
1201 accessor_declarations
1202 : get_accessor_declaration opt_set_accessor_declaration
1204 $$ = new Pair ($1, $2);
1206 | set_accessor_declaration opt_get_accessor_declaration
1208 $$ = new Pair ($2, $1);
1212 opt_get_accessor_declaration
1213 : /* empty */ { $$ = null; }
1214 | get_accessor_declaration
1217 opt_set_accessor_declaration
1218 : /* empty */ { $$ = null; }
1219 | set_accessor_declaration
1222 get_accessor_declaration
1223 : opt_attributes opt_modifiers GET
1225 // If this is not the case, then current_local_parameters has already
1226 // been set in indexer_declaration
1227 if (parsing_indexer == false)
1228 current_local_parameters = null;
1230 current_local_parameters = indexer_parameters;
1231 lexer.PropertyParsing = false;
1235 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1236 current_local_parameters = null;
1237 lexer.PropertyParsing = true;
1241 set_accessor_declaration
1242 : opt_attributes opt_modifiers SET
1245 Parameter implicit_value_parameter = new Parameter (
1246 implicit_value_parameter_type, "value",
1247 Parameter.Modifier.NONE, null);
1249 if (parsing_indexer == false) {
1250 args = new Parameter [1];
1251 args [0] = implicit_value_parameter;
1252 current_local_parameters = new Parameters (args, null, lexer.Location);
1254 Parameter [] fpars = indexer_parameters.FixedParameters;
1257 int count = fpars.Length;
1259 args = new Parameter [count + 1];
1260 fpars.CopyTo (args, 0);
1261 args [count] = implicit_value_parameter;
1264 current_local_parameters = new Parameters (
1265 args, indexer_parameters.ArrayParameter, lexer.Location);
1268 lexer.PropertyParsing = false;
1272 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1273 current_local_parameters = null;
1274 lexer.PropertyParsing = true;
1280 | SEMICOLON { $$ = null; }
1283 interface_declaration
1287 INTERFACE member_name
1289 MemberName name = MakeName ((MemberName) $5);
1290 bool partial = (bool) $3;
1293 ClassPart part = PartialContainer.CreatePart (
1294 current_namespace, current_container, name, (int) $2,
1295 (Attributes) $1, Kind.Interface, lexer.Location);
1297 current_container = part.PartialContainer;
1298 current_class = part;
1300 current_class = new Interface (
1301 current_namespace, current_container, name, (int) $2,
1302 (Attributes) $1, lexer.Location);
1304 current_container = current_class;
1305 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
1310 current_class.Bases = (ArrayList) $7;
1312 current_class.Register ();
1319 current_container = current_container.Parent;
1320 current_class = current_container;
1322 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1323 CheckIdentifierToken (yyToken);
1329 opt_interface_member_declarations
1333 opt_interface_member_declarations
1335 | interface_member_declarations
1338 interface_member_declarations
1339 : interface_member_declaration
1340 | interface_member_declarations interface_member_declaration
1343 interface_member_declaration
1344 : interface_method_declaration
1346 Method m = (Method) $1;
1348 current_container.AddMethod (m);
1350 | interface_property_declaration
1352 Property p = (Property) $1;
1354 current_container.AddProperty (p);
1356 | interface_event_declaration
1359 Event e = (Event) $1;
1360 current_container.AddEvent (e);
1363 | interface_indexer_declaration
1365 Indexer i = (Indexer) $1;
1367 current_container.AddIndexer (i);
1375 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, lexer.Location);
1380 interface_method_declaration
1381 : opt_attributes opt_new type namespace_or_type_name
1382 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1385 MemberName name = (MemberName) $4;
1387 $$ = new Method (current_class, (Expression) $3, (int) $2, true,
1388 name, (Parameters) $6, (Attributes) $1, lexer.Location);
1390 | opt_attributes opt_new VOID namespace_or_type_name
1391 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1394 MemberName name = (MemberName) $4;
1396 $$ = new Method (current_class, TypeManager.system_void_expr, (int) $2,
1397 true, name, (Parameters) $6, (Attributes) $1, lexer.Location);
1401 interface_property_declaration
1406 { lexer.PropertyParsing = true; }
1408 { lexer.PropertyParsing = false; }
1411 InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
1413 $$ = new Property (current_class, (Expression) $3, (int) $2, true,
1414 new MemberName ((string) $4), (Attributes) $1,
1415 pinfo.Get, pinfo.Set, lexer.Location);
1420 CheckIdentifierToken (yyToken);
1426 : opt_attributes GET SEMICOLON { $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, lexer.Location, lexer.Location); }
1427 | opt_attributes SET SEMICOLON { $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1, lexer.Location, lexer.Location); }
1428 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1429 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $3, lexer.Location, lexer.Location); }
1430 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1431 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $3, (Attributes) $1, lexer.Location, lexer.Location); }
1434 interface_event_declaration
1435 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1437 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1438 new MemberName ((string) $5), null,
1439 (Attributes) $1, lexer.Location);
1441 | opt_attributes opt_new EVENT type error {
1442 CheckIdentifierToken (yyToken);
1445 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1446 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1449 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE event_accessor_declarations CLOSE_BRACE {
1450 Report.Error (69, lexer.Location, "Event accessors not valid on interfaces");
1455 interface_indexer_declaration
1456 : opt_attributes opt_new type THIS
1457 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1459 { lexer.PropertyParsing = true; }
1461 { lexer.PropertyParsing = false; }
1464 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1466 $$ = new Indexer (current_class, (Expression) $3,
1467 new MemberName (TypeContainer.DefaultIndexerName),
1468 (int) $2, true, (Parameters) $6, (Attributes) $1,
1469 info.Get, info.Set, lexer.Location);
1473 operator_declaration
1474 : opt_attributes opt_modifiers operator_declarator
1476 iterator_container = SimpleIteratorContainer.GetSimple ();
1480 OperatorDeclaration decl = (OperatorDeclaration) $3;
1482 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1484 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null);
1485 if (decl.arg2type != null)
1486 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null);
1488 Operator op = new Operator (
1489 current_class, decl.optype, decl.ret_type, (int) $2,
1490 new Parameters (param_list, null, decl.location),
1491 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1493 if (SimpleIteratorContainer.Simple.Yields)
1496 // Note again, checking is done in semantic analysis
1497 current_container.AddOperator (op);
1499 current_local_parameters = null;
1500 iterator_container = null;
1506 | SEMICOLON { $$ = null; }
1509 : type OPERATOR overloadable_operator
1510 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1512 Operator.OpType op = (Operator.OpType) $3;
1513 CheckUnaryOperator (op);
1515 if (op == Operator.OpType.Addition)
1516 op = Operator.OpType.UnaryPlus;
1518 if (op == Operator.OpType.Subtraction)
1519 op = Operator.OpType.UnaryNegation;
1521 Parameter [] pars = new Parameter [1];
1523 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1525 current_local_parameters = new Parameters (pars, null, lexer.Location);
1527 $$ = new OperatorDeclaration (op, (Expression) $1, (Expression) $5, (string) $6,
1528 null, null, lexer.Location);
1530 | type OPERATOR overloadable_operator
1532 type IDENTIFIER COMMA
1536 CheckBinaryOperator ((Operator.OpType) $3);
1538 Parameter [] pars = new Parameter [2];
1540 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1541 pars [1] = new Parameter ((Expression) $8, (string) $9, Parameter.Modifier.NONE, null);
1543 current_local_parameters = new Parameters (pars, null, lexer.Location);
1545 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1546 (Expression) $5, (string) $6,
1547 (Expression) $8, (string) $9, lexer.Location);
1549 | conversion_operator_declarator
1552 overloadable_operator
1554 : BANG { $$ = Operator.OpType.LogicalNot; }
1555 | TILDE { $$ = Operator.OpType.OnesComplement; }
1556 | OP_INC { $$ = Operator.OpType.Increment; }
1557 | OP_DEC { $$ = Operator.OpType.Decrement; }
1558 | TRUE { $$ = Operator.OpType.True; }
1559 | FALSE { $$ = Operator.OpType.False; }
1560 // Unary and binary:
1561 | PLUS { $$ = Operator.OpType.Addition; }
1562 | MINUS { $$ = Operator.OpType.Subtraction; }
1564 | STAR { $$ = Operator.OpType.Multiply; }
1565 | DIV { $$ = Operator.OpType.Division; }
1566 | PERCENT { $$ = Operator.OpType.Modulus; }
1567 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1568 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1569 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1570 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1571 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1572 | OP_EQ { $$ = Operator.OpType.Equality; }
1573 | OP_NE { $$ = Operator.OpType.Inequality; }
1574 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1575 | OP_LT { $$ = Operator.OpType.LessThan; }
1576 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1577 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1580 conversion_operator_declarator
1581 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1583 Parameter [] pars = new Parameter [1];
1585 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1587 current_local_parameters = new Parameters (pars, null, lexer.Location);
1589 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1590 null, null, lexer.Location);
1592 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1594 Parameter [] pars = new Parameter [1];
1596 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1598 current_local_parameters = new Parameters (pars, null, lexer.Location);
1600 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1601 null, null, lexer.Location);
1605 syntax_error (lexer.Location, "'operator' expected");
1609 syntax_error (lexer.Location, "'operator' expected");
1613 constructor_declaration
1616 constructor_declarator
1619 Constructor c = (Constructor) $3;
1620 c.Block = (ToplevelBlock) $4;
1621 c.OptAttributes = (Attributes) $1;
1622 c.ModFlags = (int) $2;
1624 if (c.Name == current_container.Basename){
1625 if ((c.ModFlags & Modifiers.STATIC) != 0){
1626 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1628 515, c.Location, String.Format (
1629 "`{0}.{1}': static constructor can not have access modifiers",
1630 c.Name, current_container.Name));
1633 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1635 if (c.Initializer != null){
1638 "Static constructors can not have an explicit this or base " +
1639 "constructor invocations");
1642 if (!c.Parameters.Empty){
1644 132, c.Location, "Static constructors should not have parameters");
1647 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1650 // We let another layer check the validity of the constructor.
1651 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1654 current_container.AddConstructor (c);
1656 current_local_parameters = null;
1660 constructor_declarator
1662 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1664 oob_stack.Push (lexer.Location);
1666 current_local_parameters = (Parameters) $3;
1668 opt_constructor_initializer
1670 Location l = (Location) oob_stack.Pop ();
1671 $$ = new Constructor (current_class, (string) $1, 0, (Parameters) $3,
1672 (ConstructorInitializer) $6, l);
1678 | SEMICOLON { $$ = null; }
1681 opt_constructor_initializer
1682 : /* empty */ { $$ = null; }
1683 | constructor_initializer
1686 constructor_initializer
1687 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1689 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1691 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1693 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1696 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1702 : /* EMPTY */ { $$ = 0; }
1703 | UNSAFE { $$ = Modifiers.UNSAFE; }
1704 | EXTERN { $$ = Modifiers.EXTERN; }
1707 destructor_declaration
1708 : opt_attributes opt_finalizer TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1710 if ((string) $4 != current_container.Basename){
1711 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1712 } else if (!(current_container is Class)){
1713 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1715 Location l = lexer.Location;
1718 if (!RootContext.StdLib && current_container.Name == "System.Object")
1719 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
1721 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
1723 if ((m & Modifiers.UNSAFE) != 0){
1724 if (!RootContext.Unsafe){
1725 Report.Error (227, l,
1726 "Unsafe code requires the -unsafe command " +
1727 "line option to be specified");
1731 Method d = new Destructor (
1732 current_class, TypeManager.system_void_expr, m, "Finalize",
1733 new Parameters (null, null, l), (Attributes) $1, l);
1735 d.Block = (ToplevelBlock) $7;
1736 current_container.AddMethod (d);
1744 EVENT type variable_declarators SEMICOLON
1746 foreach (VariableDeclaration var in (ArrayList) $5) {
1748 MemberName name = new MemberName (var.identifier);
1750 Event e = new EventField (
1751 current_class, (Expression) $4, (int) $2, false, name,
1752 var.expression_or_array_initializer, (Attributes) $1,
1755 current_container.AddEvent (e);
1761 EVENT type namespace_or_type_name
1764 implicit_value_parameter_type = (Expression) $4;
1765 lexer.EventParsing = true;
1766 oob_stack.Push (lexer.Location);
1768 event_accessor_declarations
1770 lexer.EventParsing = false;
1774 Location loc = (Location) oob_stack.Pop ();
1777 Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
1780 Pair pair = (Pair) $8;
1782 MemberName name = (MemberName) $5;
1784 Event e = new EventProperty (
1785 current_class, (Expression) $4, (int) $2, false, name, null,
1786 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
1789 current_container.AddEvent (e);
1790 implicit_value_parameter_type = null;
1793 | opt_attributes opt_modifiers EVENT type namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS block {
1794 MemberName mn = (MemberName) $5;
1796 if (mn.Left != null)
1797 Report.Error (71, lexer.Location, "Explicit implementation of events requires property syntax");
1799 Report.Error (71, lexer.Location, "Event declaration should use property syntax");
1803 event_accessor_declarations
1804 : add_accessor_declaration remove_accessor_declaration
1806 $$ = new Pair ($1, $2);
1808 | remove_accessor_declaration add_accessor_declaration
1810 $$ = new Pair ($2, $1);
1812 | add_accessor_declaration { $$ = null; }
1813 | remove_accessor_declaration { $$ = null; }
1816 Report.Error (1055, lexer.Location, "An add or remove accessor expected");
1822 add_accessor_declaration
1823 : opt_attributes ADD
1825 Parameter [] args = new Parameter [1];
1826 Parameter implicit_value_parameter = new Parameter (
1827 implicit_value_parameter_type, "value",
1828 Parameter.Modifier.NONE, null);
1830 args [0] = implicit_value_parameter;
1832 current_local_parameters = new Parameters (args, null, lexer.Location);
1833 lexer.EventParsing = false;
1837 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
1838 lexer.EventParsing = true;
1840 | opt_attributes ADD error {
1841 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1846 remove_accessor_declaration
1847 : opt_attributes REMOVE
1849 Parameter [] args = new Parameter [1];
1850 Parameter implicit_value_parameter = new Parameter (
1851 implicit_value_parameter_type, "value",
1852 Parameter.Modifier.NONE, null);
1854 args [0] = implicit_value_parameter;
1856 current_local_parameters = new Parameters (args, null, lexer.Location);
1857 lexer.EventParsing = false;
1861 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
1862 lexer.EventParsing = true;
1864 | opt_attributes REMOVE error {
1865 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1871 : opt_attributes opt_modifiers indexer_declarator
1874 IndexerDeclaration decl = (IndexerDeclaration) $3;
1876 implicit_value_parameter_type = decl.type;
1878 lexer.PropertyParsing = true;
1879 parsing_indexer = true;
1881 indexer_parameters = decl.param_list;
1882 oob_stack.Push (lexer.Location);
1884 accessor_declarations
1886 lexer.PropertyParsing = false;
1887 parsing_indexer = false;
1891 // The signature is computed from the signature of the indexer. Look
1892 // at section 3.6 on the spec
1893 Location loc = (Location) oob_stack.Pop ();
1895 IndexerDeclaration decl = (IndexerDeclaration) $3;
1896 Pair pair = (Pair) $6;
1897 Accessor get_block = (Accessor) pair.First;
1898 Accessor set_block = (Accessor) pair.Second;
1901 if (decl.interface_type != null)
1902 name = new MemberName (decl.interface_type,
1903 TypeContainer.DefaultIndexerName);
1905 name = new MemberName (TypeContainer.DefaultIndexerName);
1907 indexer = new Indexer (current_class, decl.type, name,
1908 (int) $2, false, decl.param_list, (Attributes) $1,
1909 get_block, set_block, loc);
1911 current_container.AddIndexer (indexer);
1913 current_local_parameters = null;
1914 implicit_value_parameter_type = null;
1915 indexer_parameters = null;
1920 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1922 Parameters pars = (Parameters) $4;
1923 if (pars.HasArglist) {
1924 // "__arglist is not valid in this context"
1925 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
1926 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
1927 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1930 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
1932 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1934 Parameters pars = (Parameters) $6;
1936 if (pars.HasArglist) {
1937 // "__arglist is not valid in this context"
1938 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
1939 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
1940 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1942 MemberName name = (MemberName) $2;
1943 $$ = new IndexerDeclaration ((Expression) $1, name, pars);
1955 Location enum_location = lexer.Location;
1957 MemberName full_name = MakeName (new MemberName ((string) $4));
1958 Enum e = new Enum (current_namespace, current_container, (Expression) $5, (int) $2,
1959 full_name, (Attributes) $1, enum_location);
1961 foreach (VariableDeclaration ev in (ArrayList) $6) {
1962 e.AddEnumMember (ev.identifier,
1963 (Expression) ev.expression_or_array_initializer,
1964 ev.Location, ev.OptAttributes);
1967 string name = full_name.GetName ();
1968 current_container.AddEnum (e);
1969 RootContext.Tree.RecordDecl (name, e);
1975 : /* empty */ { $$ = TypeManager.system_int32_expr; }
1976 | COLON type { $$ = $2; }
1980 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1986 opt_enum_member_declarations
1987 : /* empty */ { $$ = new ArrayList (4); }
1988 | enum_member_declarations opt_comma { $$ = $1; }
1991 enum_member_declarations
1992 : enum_member_declaration
1994 ArrayList l = new ArrayList (4);
1999 | enum_member_declarations COMMA enum_member_declaration
2001 ArrayList l = (ArrayList) $1;
2009 enum_member_declaration
2010 : opt_attributes IDENTIFIER
2012 $$ = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
2014 | opt_attributes IDENTIFIER
2016 $$ = lexer.Location;
2020 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
2024 delegate_declaration
2027 DELEGATE type member_name
2028 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2031 Location l = lexer.Location;
2032 MemberName name = MakeName ((MemberName) $5);
2033 Delegate del = new Delegate (current_namespace, current_container, (Expression) $4,
2034 (int) $2, name, (Parameters) $7, (Attributes) $1, l);
2036 current_container.AddDelegate (del);
2037 RootContext.Tree.RecordDecl (name.GetName (true), del);
2041 DELEGATE VOID member_name
2042 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2045 Location l = lexer.Location;
2046 MemberName name = MakeName ((MemberName) $5);
2047 Delegate del = new Delegate (
2048 current_namespace, current_container,
2049 TypeManager.system_void_expr, (int) $2, name,
2050 (Parameters) $7, (Attributes) $1, l);
2052 current_container.AddDelegate (del);
2053 RootContext.Tree.RecordDecl (name.GetName (true), del);
2057 namespace_or_type_name
2059 | namespace_or_type_name DOT IDENTIFIER {
2060 $$ = new MemberName ((MemberName) $1, (string) $3);
2066 $$ = new MemberName ((string) $1);
2071 * Before you think of adding a return_type, notice that we have been
2072 * using two rules in the places where it matters (one rule using type
2073 * and another identical one that uses VOID as the return type). This
2074 * gets rid of a shift/reduce couple
2077 : namespace_or_type_name
2079 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2091 // Note that here only unmanaged types are allowed but we
2092 // can't perform checks during this phase - we do it during
2093 // semantic analysis.
2095 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2099 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2105 | non_expression_type rank_specifier
2107 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2109 | non_expression_type STAR
2111 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2113 | expression rank_specifiers
2115 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2119 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2123 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2124 // through this different path
2126 | multiplicative_expression STAR
2128 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2135 ArrayList types = new ArrayList (4);
2140 | type_list COMMA type
2142 ArrayList types = (ArrayList) $1;
2150 * replaces all the productions for isolating the various
2151 * simple types, but we need this to reuse it easily in local_variable_type
2154 : OBJECT { $$ = TypeManager.system_object_expr; }
2155 | STRING { $$ = TypeManager.system_string_expr; }
2156 | BOOL { $$ = TypeManager.system_boolean_expr; }
2157 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2158 | FLOAT { $$ = TypeManager.system_single_expr; }
2159 | DOUBLE { $$ = TypeManager.system_double_expr; }
2164 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2165 | BYTE { $$ = TypeManager.system_byte_expr; }
2166 | SHORT { $$ = TypeManager.system_int16_expr; }
2167 | USHORT { $$ = TypeManager.system_uint16_expr; }
2168 | INT { $$ = TypeManager.system_int32_expr; }
2169 | UINT { $$ = TypeManager.system_uint32_expr; }
2170 | LONG { $$ = TypeManager.system_int64_expr; }
2171 | ULONG { $$ = TypeManager.system_uint64_expr; }
2172 | CHAR { $$ = TypeManager.system_char_expr; }
2173 | VOID { $$ = TypeManager.system_void_expr; }
2177 : type rank_specifiers
2179 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2184 // Expressions, section 7.5
2194 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2196 | parenthesized_expression
2198 | invocation_expression
2202 | post_increment_expression
2203 | post_decrement_expression
2207 | checked_expression
2208 | unchecked_expression
2209 | pointer_member_access
2210 | anonymous_method_expression
2217 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2218 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2219 | NULL { $$ = NullLiteral.Null; }
2223 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2224 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2225 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2230 object v = lexer.Value;
2236 $$ = IntLiteral.Zero;
2238 $$ = IntLiteral.One;
2240 $$ = new IntLiteral (i);
2241 } else if (v is uint)
2242 $$ = new UIntLiteral ((UInt32) v);
2244 $$ = new LongLiteral ((Int64) v);
2245 else if (v is ulong)
2246 $$ = new ULongLiteral ((UInt64) v);
2248 Console.WriteLine ("OOPS. Unexpected result from scanner");
2253 : TRUE { $$ = new BoolLiteral (true); }
2254 | FALSE { $$ = new BoolLiteral (false); }
2257 parenthesized_expression_0
2258 : OPEN_PARENS expression CLOSE_PARENS
2261 lexer.Deambiguate_CloseParens ();
2262 // After this, the next token returned is one of
2263 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2264 // or CLOSE_PARENS_MINUS.
2268 parenthesized_expression
2269 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2273 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2275 // If a parenthesized expression is followed by a minus, we need to wrap
2276 // the expression inside a ParenthesizedExpression for the CS0075 check
2277 // in Binary.DoResolve().
2278 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2283 : primary_expression DOT IDENTIFIER
2285 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2287 | predefined_type DOT IDENTIFIER
2289 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2297 invocation_expression
2298 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2301 Location l = lexer.Location;
2302 Report.Error (1, l, "Parse error");
2304 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2306 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2308 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2310 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2312 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2317 : /* empty */ { $$ = null; }
2324 ArrayList list = new ArrayList (4);
2328 | argument_list COMMA argument
2330 ArrayList list = (ArrayList) $1;
2334 | argument_list error {
2335 CheckToken (1026, yyToken, ", or ) expected");
2342 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2344 | REF variable_reference
2346 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2348 | OUT variable_reference
2350 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2352 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2354 ArrayList list = (ArrayList) $3;
2355 Argument[] args = new Argument [list.Count];
2356 list.CopyTo (args, 0);
2358 Expression expr = new Arglist (args, lexer.Location);
2359 $$ = new Argument (expr, Argument.AType.Expression);
2363 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2368 : expression { note ("section 5.4"); $$ = $1; }
2372 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2374 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2376 | primary_expression rank_specifiers
2378 // So the super-trick is that primary_expression
2379 // can only be either a SimpleName or a MemberAccess.
2380 // The MemberAccess case arises when you have a fully qualified type-name like :
2382 // SimpleName is when you have
2385 Expression expr = (Expression) $1;
2386 if (expr is ComposedCast){
2387 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2388 } else if (!(expr is SimpleName || expr is MemberAccess)){
2389 Error_ExpectingTypeName (lexer.Location, expr);
2390 $$ = TypeManager.system_object_expr;
2393 // So we extract the string corresponding to the SimpleName
2396 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2404 ArrayList list = new ArrayList (4);
2408 | expression_list COMMA expression
2410 ArrayList list = (ArrayList) $1;
2419 $$ = new This (current_block, lexer.Location);
2424 : BASE DOT IDENTIFIER
2426 $$ = new BaseAccess ((string) $3, lexer.Location);
2428 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2430 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2433 Report.Error (175, lexer.Location, "Use of keyword `base' is not valid in this context");
2438 post_increment_expression
2439 : primary_expression OP_INC
2441 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2442 (Expression) $1, lexer.Location);
2446 post_decrement_expression
2447 : primary_expression OP_DEC
2449 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2450 (Expression) $1, lexer.Location);
2455 : object_or_delegate_creation_expression
2456 | array_creation_expression
2459 object_or_delegate_creation_expression
2460 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2462 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2466 array_creation_expression
2467 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2469 opt_array_initializer
2471 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2473 | NEW type rank_specifiers array_initializer
2475 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2479 Report.Error (1031, lexer.Location, "Type expected");
2484 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2500 : rank_specifier opt_rank_specifier
2502 $$ = (string) $2 + (string) $1;
2507 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2509 $$ = "[" + (string) $2 + "]";
2529 | dim_separators COMMA
2531 $$ = (string) $1 + ",";
2535 opt_array_initializer
2547 : OPEN_BRACE CLOSE_BRACE
2549 ArrayList list = new ArrayList (4);
2552 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2554 $$ = (ArrayList) $2;
2558 variable_initializer_list
2559 : variable_initializer
2561 ArrayList list = new ArrayList (4);
2565 | variable_initializer_list COMMA variable_initializer
2567 ArrayList list = (ArrayList) $1;
2574 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2576 $$ = new TypeOfVoid (lexer.Location);
2578 | TYPEOF OPEN_PARENS type CLOSE_PARENS
2580 $$ = new TypeOf ((Expression) $3, lexer.Location);
2585 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2586 $$ = new SizeOf ((Expression) $3, lexer.Location);
2591 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2593 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2597 unchecked_expression
2598 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2600 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2604 pointer_member_access
2605 : primary_expression OP_PTR IDENTIFIER
2609 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2610 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2614 anonymous_method_expression
2615 : DELEGATE opt_anonymous_method_signature {
2616 oob_stack.Push (current_local_parameters);
2617 current_local_parameters = (Parameters)$2;
2619 // Force the next block to be created as a ToplevelBlock
2620 oob_stack.Push (current_block);
2621 oob_stack.Push (top_current_block);
2622 oob_stack.Push (lexer.Location);
2623 current_block = null;
2625 Location loc = (Location) oob_stack.Pop ();
2626 top_current_block = (Block) oob_stack.Pop ();
2627 current_block = (Block) oob_stack.Pop ();
2628 if (RootContext.Version == LanguageVersion.ISO_1){
2629 Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
2632 ToplevelBlock anon_block = (ToplevelBlock) $4;
2634 anon_block.Parent = current_block;
2635 $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block,
2638 current_local_parameters = (Parameters) oob_stack.Pop ();
2642 opt_anonymous_method_signature
2643 : /* empty */ { $$ = null; }
2644 | anonymous_method_signature
2647 anonymous_method_signature
2648 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
2651 $$ = Parameters.EmptyReadOnlyParameters;
2653 ArrayList par_list = (ArrayList) $2;
2654 Parameter [] pars = new Parameter [par_list.Count];
2655 par_list.CopyTo (pars);
2656 $$ = new Parameters (pars, null, lexer.Location);
2661 opt_anonymous_method_parameter_list
2662 : /* empty */ { $$ = null; }
2663 | anonymous_method_parameter_list { $$ = $1; }
2666 anonymous_method_parameter_list
2667 : anonymous_method_parameter
2669 ArrayList a = new ArrayList (4);
2673 | anonymous_method_parameter_list COMMA anonymous_method_parameter
2675 ArrayList a = (ArrayList) $1;
2681 anonymous_method_parameter
2682 : opt_parameter_modifier type IDENTIFIER {
2683 $$ = new Parameter ((Expression) $2, (string) $3, (Parameter.Modifier) $1, null);
2685 | PARAMS type IDENTIFIER {
2686 Report.Error (-221, lexer.Location, "params modifier not allowed in anonymous method declaration");
2692 : primary_expression
2693 | BANG prefixed_unary_expression
2695 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2697 | TILDE prefixed_unary_expression
2699 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2705 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
2707 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2709 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
2711 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2717 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2719 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2724 // The idea to split this out is from Rhys' grammar
2725 // to solve the problem with casts.
2727 prefixed_unary_expression
2729 | PLUS prefixed_unary_expression
2731 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2733 | MINUS prefixed_unary_expression
2735 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2737 | OP_INC prefixed_unary_expression
2739 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2740 (Expression) $2, lexer.Location);
2742 | OP_DEC prefixed_unary_expression
2744 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2745 (Expression) $2, lexer.Location);
2747 | STAR prefixed_unary_expression
2749 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2751 | BITWISE_AND prefixed_unary_expression
2753 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2757 pre_increment_expression
2758 : OP_INC prefixed_unary_expression
2760 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2761 (Expression) $2, lexer.Location);
2765 pre_decrement_expression
2766 : OP_DEC prefixed_unary_expression
2768 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2769 (Expression) $2, lexer.Location);
2773 multiplicative_expression
2774 : prefixed_unary_expression
2775 | multiplicative_expression STAR prefixed_unary_expression
2777 $$ = new Binary (Binary.Operator.Multiply,
2778 (Expression) $1, (Expression) $3, lexer.Location);
2780 | multiplicative_expression DIV prefixed_unary_expression
2782 $$ = new Binary (Binary.Operator.Division,
2783 (Expression) $1, (Expression) $3, lexer.Location);
2785 | multiplicative_expression PERCENT prefixed_unary_expression
2787 $$ = new Binary (Binary.Operator.Modulus,
2788 (Expression) $1, (Expression) $3, lexer.Location);
2793 : multiplicative_expression
2794 | additive_expression PLUS multiplicative_expression
2796 $$ = new Binary (Binary.Operator.Addition,
2797 (Expression) $1, (Expression) $3, lexer.Location);
2799 | additive_expression MINUS multiplicative_expression
2801 $$ = new Binary (Binary.Operator.Subtraction,
2802 (Expression) $1, (Expression) $3, lexer.Location);
2807 : additive_expression
2808 | shift_expression OP_SHIFT_LEFT additive_expression
2810 $$ = new Binary (Binary.Operator.LeftShift,
2811 (Expression) $1, (Expression) $3, lexer.Location);
2813 | shift_expression OP_SHIFT_RIGHT additive_expression
2815 $$ = new Binary (Binary.Operator.RightShift,
2816 (Expression) $1, (Expression) $3, lexer.Location);
2820 relational_expression
2822 | relational_expression OP_LT shift_expression
2824 $$ = new Binary (Binary.Operator.LessThan,
2825 (Expression) $1, (Expression) $3, lexer.Location);
2827 | relational_expression OP_GT shift_expression
2829 $$ = new Binary (Binary.Operator.GreaterThan,
2830 (Expression) $1, (Expression) $3, lexer.Location);
2832 | relational_expression OP_LE shift_expression
2834 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2835 (Expression) $1, (Expression) $3, lexer.Location);
2837 | relational_expression OP_GE shift_expression
2839 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2840 (Expression) $1, (Expression) $3, lexer.Location);
2842 | relational_expression IS type
2844 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
2846 | relational_expression AS type
2848 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
2853 : relational_expression
2854 | equality_expression OP_EQ relational_expression
2856 $$ = new Binary (Binary.Operator.Equality,
2857 (Expression) $1, (Expression) $3, lexer.Location);
2859 | equality_expression OP_NE relational_expression
2861 $$ = new Binary (Binary.Operator.Inequality,
2862 (Expression) $1, (Expression) $3, lexer.Location);
2867 : equality_expression
2868 | and_expression BITWISE_AND equality_expression
2870 $$ = new Binary (Binary.Operator.BitwiseAnd,
2871 (Expression) $1, (Expression) $3, lexer.Location);
2875 exclusive_or_expression
2877 | exclusive_or_expression CARRET and_expression
2879 $$ = new Binary (Binary.Operator.ExclusiveOr,
2880 (Expression) $1, (Expression) $3, lexer.Location);
2884 inclusive_or_expression
2885 : exclusive_or_expression
2886 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2888 $$ = new Binary (Binary.Operator.BitwiseOr,
2889 (Expression) $1, (Expression) $3, lexer.Location);
2893 conditional_and_expression
2894 : inclusive_or_expression
2895 | conditional_and_expression OP_AND inclusive_or_expression
2897 $$ = new Binary (Binary.Operator.LogicalAnd,
2898 (Expression) $1, (Expression) $3, lexer.Location);
2902 conditional_or_expression
2903 : conditional_and_expression
2904 | conditional_or_expression OP_OR conditional_and_expression
2906 $$ = new Binary (Binary.Operator.LogicalOr,
2907 (Expression) $1, (Expression) $3, lexer.Location);
2911 conditional_expression
2912 : conditional_or_expression
2913 | conditional_or_expression INTERR expression COLON expression
2915 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2919 assignment_expression
2920 : prefixed_unary_expression ASSIGN expression
2922 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2924 | prefixed_unary_expression OP_MULT_ASSIGN expression
2926 Location l = lexer.Location;
2928 $$ = new CompoundAssign (
2929 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
2931 | prefixed_unary_expression OP_DIV_ASSIGN expression
2933 Location l = lexer.Location;
2935 $$ = new CompoundAssign (
2936 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
2938 | prefixed_unary_expression OP_MOD_ASSIGN expression
2940 Location l = lexer.Location;
2942 $$ = new CompoundAssign (
2943 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
2945 | prefixed_unary_expression OP_ADD_ASSIGN expression
2947 Location l = lexer.Location;
2949 $$ = new CompoundAssign (
2950 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
2952 | prefixed_unary_expression OP_SUB_ASSIGN expression
2954 Location l = lexer.Location;
2956 $$ = new CompoundAssign (
2957 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
2959 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2961 Location l = lexer.Location;
2963 $$ = new CompoundAssign (
2964 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
2966 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2968 Location l = lexer.Location;
2970 $$ = new CompoundAssign (
2971 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
2973 | prefixed_unary_expression OP_AND_ASSIGN expression
2975 Location l = lexer.Location;
2977 $$ = new CompoundAssign (
2978 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
2980 | prefixed_unary_expression OP_OR_ASSIGN expression
2982 Location l = lexer.Location;
2984 $$ = new CompoundAssign (
2985 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
2987 | prefixed_unary_expression OP_XOR_ASSIGN expression
2989 Location l = lexer.Location;
2991 $$ = new CompoundAssign (
2992 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
2997 : conditional_expression
2998 | assignment_expression
3018 MemberName name = MakeName ((MemberName) $5);
3019 bool partial = (bool) $3;
3020 int mod_flags = (int) $2;
3023 ClassPart part = PartialContainer.CreatePart (
3024 current_namespace, current_container, name, mod_flags,
3025 (Attributes) $1, Kind.Class, lexer.Location);
3027 current_container = part.PartialContainer;
3028 current_class = part;
3030 if ((mod_flags & Modifiers.STATIC) != 0) {
3031 current_class = new StaticClass (
3032 current_namespace, current_container, name,
3033 mod_flags, (Attributes) $1, lexer.Location);
3035 current_class = new Class (
3036 current_namespace, current_container, name,
3037 mod_flags, (Attributes) $1, lexer.Location);
3040 current_container = current_class;
3041 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
3047 if (current_class.Name == "System.Object") {
3048 Report.Error (537, current_class.Location,
3049 "The class System.Object cannot have a base " +
3050 "class or implement an interface.");
3052 current_class.Bases = (ArrayList) $7;
3055 current_class.Register ();
3062 current_container = current_container.Parent;
3063 current_class = current_container;
3069 { $$ = (bool) false; }
3071 { $$ = (bool) true; }
3075 : /* empty */ { $$ = (int) 0; }
3081 | modifiers modifier
3086 if ((m1 & m2) != 0) {
3087 Location l = lexer.Location;
3088 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3090 $$ = (int) (m1 | m2);
3095 : NEW { $$ = Modifiers.NEW; }
3096 | PUBLIC { $$ = Modifiers.PUBLIC; }
3097 | PROTECTED { $$ = Modifiers.PROTECTED; }
3098 | INTERNAL { $$ = Modifiers.INTERNAL; }
3099 | PRIVATE { $$ = Modifiers.PRIVATE; }
3100 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3101 | SEALED { $$ = Modifiers.SEALED; }
3102 | STATIC { $$ = Modifiers.STATIC; }
3103 | READONLY { $$ = Modifiers.READONLY; }
3104 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3105 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3106 | EXTERN { $$ = Modifiers.EXTERN; }
3107 | VOLATILE { $$ = Modifiers.VOLATILE; }
3108 | UNSAFE { $$ = Modifiers.UNSAFE; }
3112 : /* empty */ { $$ = null; }
3113 | class_base { $$ = $1; }
3117 : COLON type_list { $$ = $2; }
3125 // A block is "contained" on the following places:
3127 // property_declaration as part of the accessor body (get/set)
3128 // operator_declaration
3129 // constructor_declaration
3130 // destructor_declaration
3131 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3136 if (current_block == null){
3137 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, lexer.Location);
3138 top_current_block = current_block;
3140 current_block = new Block (current_block, current_local_parameters,
3141 lexer.Location, Location.Null);
3144 opt_statement_list CLOSE_BRACE
3146 while (current_block.Implicit)
3147 current_block = current_block.Parent;
3149 current_block.SetEndLocation (lexer.Location);
3150 current_block = current_block.Parent;
3151 if (current_block == null)
3152 top_current_block = null;
3163 | statement_list statement
3167 : declaration_statement
3169 if ($1 != null && (Block) $1 != current_block){
3170 current_block.AddStatement ((Statement) $1);
3171 current_block = (Block) $1;
3174 | valid_declaration_statement
3176 current_block.AddStatement ((Statement) $1);
3181 valid_declaration_statement
3184 | expression_statement
3185 | selection_statement
3186 | iteration_statement
3190 | unchecked_statement
3198 : valid_declaration_statement
3199 | declaration_statement
3201 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3206 Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3214 $$ = EmptyStatement.Value;
3221 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3223 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3224 current_block.AddStatement (labeled);
3229 declaration_statement
3230 : local_variable_declaration SEMICOLON
3233 DictionaryEntry de = (DictionaryEntry) $1;
3235 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3239 | local_constant_declaration SEMICOLON
3242 DictionaryEntry de = (DictionaryEntry) $1;
3244 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3250 * The following is from Rhys' grammar:
3251 * > Types in local variable declarations must be recognized as
3252 * > expressions to prevent reduce/reduce errors in the grammar.
3253 * > The expressions are converted into types during semantic analysis.
3256 : primary_expression opt_rank_specifier
3258 // FIXME: Do something smart here regarding the composition of the type.
3260 // Ok, the above "primary_expression" is there to get rid of
3261 // both reduce/reduce and shift/reduces in the grammar, it should
3262 // really just be "type_name". If you use type_name, a reduce/reduce
3263 // creeps up. If you use namespace_or_type_name (which is all we need
3264 // really) two shift/reduces appear.
3267 // So the super-trick is that primary_expression
3268 // can only be either a SimpleName or a MemberAccess.
3269 // The MemberAccess case arises when you have a fully qualified type-name like :
3271 // SimpleName is when you have
3274 Expression expr = (Expression) $1;
3275 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3276 Error_ExpectingTypeName (lexer.Location, expr);
3280 // So we extract the string corresponding to the SimpleName
3284 if ((string) $2 == "")
3287 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3290 | builtin_types opt_rank_specifier
3292 if ((string) $2 == "")
3295 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3299 local_variable_pointer_type
3300 : primary_expression STAR
3302 Expression expr = (Expression) $1;
3303 Location l = lexer.Location;
3305 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3306 Error_ExpectingTypeName (l, expr);
3310 $$ = new ComposedCast ((Expression) $1, "*", l);
3312 | builtin_types STAR
3314 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3318 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3320 | local_variable_pointer_type STAR
3322 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3326 local_variable_declaration
3327 : local_variable_type variable_declarators
3330 $$ = new DictionaryEntry ($1, $2);
3334 | local_variable_pointer_type opt_rank_specifier variable_declarators
3339 if ((string) $2 == "")
3340 t = (Expression) $1;
3342 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3343 $$ = new DictionaryEntry (t, $3);
3349 local_constant_declaration
3350 : CONST local_variable_type constant_declarators
3353 $$ = new DictionaryEntry ($2, $3);
3359 expression_statement
3360 : statement_expression SEMICOLON
3367 // We have to do the wrapping here and not in the case above,
3368 // because statement_expression is used for example in for_statement
3370 statement_expression
3371 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3372 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3373 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3374 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3375 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3376 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3377 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3379 Report.Error (1002, lexer.Location, "Expecting `;'");
3384 object_creation_expression
3385 : object_or_delegate_creation_expression
3386 { note ("complain if this is a delegate maybe?"); }
3395 : if_statement_open if_statement_rest
3404 oob_stack.Push (lexer.Location);
3409 : boolean_expression CLOSE_PARENS
3412 Location l = (Location) oob_stack.Pop ();
3414 $$ = new If ((Expression) $1, (Statement) $3, l);
3416 if (RootContext.WarningLevel >= 3){
3417 if ($3 == EmptyStatement.Value)
3418 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3422 | boolean_expression CLOSE_PARENS
3423 embedded_statement ELSE embedded_statement
3425 Location l = (Location) oob_stack.Pop ();
3427 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
3432 : SWITCH OPEN_PARENS
3434 oob_stack.Push (lexer.Location);
3435 switch_stack.Push (current_block);
3437 expression CLOSE_PARENS
3440 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
3441 current_block = (Block) switch_stack.Pop ();
3457 Report.Error (1522, lexer.Location, "Empty switch block");
3465 ArrayList sections = new ArrayList (4);
3470 | switch_sections switch_section
3472 ArrayList sections = (ArrayList) $1;
3482 current_block = current_block.CreateSwitchBlock (lexer.Location);
3486 Block topmost = current_block;
3488 while (topmost.Implicit)
3489 topmost = topmost.Parent;
3490 $$ = new SwitchSection ((ArrayList) $1, topmost);
3497 ArrayList labels = new ArrayList (4);
3502 | switch_labels switch_label
3504 ArrayList labels = (ArrayList) ($1);
3512 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3513 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3516 1523, lexer.Location,
3517 "The keyword case or default must precede code in switch block");
3531 oob_stack.Push (lexer.Location);
3533 boolean_expression CLOSE_PARENS embedded_statement
3535 Location l = (Location) oob_stack.Pop ();
3536 $$ = new While ((Expression) $4, (Statement) $6, l);
3538 if (RootContext.WarningLevel >= 3){
3539 if ($6 == EmptyStatement.Value)
3540 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3546 : DO embedded_statement
3548 oob_stack.Push (lexer.Location);
3550 boolean_expression CLOSE_PARENS SEMICOLON
3552 Location l = (Location) oob_stack.Pop ();
3554 $$ = new Do ((Statement) $2, (Expression) $6, l);
3560 opt_for_initializer SEMICOLON
3562 Block assign_block = new Block (current_block);
3563 current_block = assign_block;
3565 if ($3 is DictionaryEntry){
3566 DictionaryEntry de = (DictionaryEntry) $3;
3568 Expression type = (Expression) de.Key;
3569 ArrayList var_declarators = (ArrayList) de.Value;
3571 foreach (VariableDeclaration decl in var_declarators){
3575 vi = current_block.AddVariable (
3576 type, decl.identifier, current_local_parameters, decl.Location);
3580 Location l = lexer.Location;
3582 if (decl.expression_or_array_initializer is Expression){
3583 expr = (Expression) decl.expression_or_array_initializer;
3584 } else if (decl.expression_or_array_initializer == null) {
3587 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3588 expr = new ArrayCreation (type, "", init, decl.Location);
3591 LocalVariableReference var;
3592 var = new LocalVariableReference (assign_block, decl.identifier, l);
3595 Assign a = new Assign (var, expr, decl.Location);
3597 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3603 oob_stack.Push (lexer.Location);
3605 opt_for_condition SEMICOLON
3606 opt_for_iterator CLOSE_PARENS
3609 Location l = (Location) oob_stack.Pop ();
3611 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3613 if (RootContext.WarningLevel >= 3){
3614 if ($10 == EmptyStatement.Value)
3615 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3618 current_block.AddStatement (f);
3619 while (current_block.Implicit)
3620 current_block = current_block.Parent;
3622 current_block = current_block.Parent;
3627 : /* empty */ { $$ = EmptyStatement.Value; }
3632 : local_variable_declaration
3633 | statement_expression_list
3637 : /* empty */ { $$ = null; }
3638 | boolean_expression
3642 : /* empty */ { $$ = EmptyStatement.Value; }
3647 : statement_expression_list
3650 statement_expression_list
3651 : statement_expression
3653 // CHANGE: was `null'
3654 Block b = new Block (current_block, Block.Flags.Implicit);
3656 b.AddStatement ((Statement) $1);
3659 | statement_expression_list COMMA statement_expression
3661 Block b = (Block) $1;
3663 b.AddStatement ((Statement) $3);
3669 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
3671 Report.Error (230, lexer.Location, "Type and identifier are both required in a foreach statement");
3674 | FOREACH OPEN_PARENS type IDENTIFIER IN
3676 oob_stack.Push (lexer.Location);
3678 expression CLOSE_PARENS
3680 oob_stack.Push (current_block);
3682 Block foreach_block = new Block (current_block);
3683 LocalVariableReference v = null;
3684 Location l = lexer.Location;
3687 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3691 // Get a writable reference to this read-only variable.
3692 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3694 current_block = foreach_block;
3697 oob_stack.Push (current_block);
3701 Block foreach_block = (Block) oob_stack.Pop ();
3702 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3703 Block prev_block = (Block) oob_stack.Pop ();
3704 Location l = (Location) oob_stack.Pop ();
3706 current_block = prev_block;
3709 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3710 foreach_block.AddStatement (f);
3719 | continue_statement
3729 $$ = new Break (lexer.Location);
3734 : CONTINUE SEMICOLON
3736 $$ = new Continue (lexer.Location);
3741 : GOTO IDENTIFIER SEMICOLON
3743 $$ = new Goto (current_block, (string) $2, lexer.Location);
3745 | GOTO CASE constant_expression SEMICOLON
3747 $$ = new GotoCase ((Expression) $3, lexer.Location);
3749 | GOTO DEFAULT SEMICOLON
3751 $$ = new GotoDefault (lexer.Location);
3756 : RETURN opt_expression SEMICOLON
3758 $$ = new Return ((Expression) $2, lexer.Location);
3763 : THROW opt_expression SEMICOLON
3765 $$ = new Throw ((Expression) $2, lexer.Location);
3770 : IDENTIFIER RETURN expression SEMICOLON
3772 string s = (string) $1;
3774 Report.Error (1003, lexer.Location, "; expected");
3777 if (RootContext.Version == LanguageVersion.ISO_1){
3778 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
3781 if (iterator_container == null){
3782 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3785 iterator_container.SetYields ();
3786 $$ = new Yield ((Expression) $3, lexer.Location);
3789 | IDENTIFIER BREAK SEMICOLON
3791 string s = (string) $1;
3793 Report.Error (1003, lexer.Location, "; expected");
3796 if (RootContext.Version == LanguageVersion.ISO_1){
3797 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
3800 if (iterator_container == null){
3801 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3804 iterator_container.SetYields ();
3805 $$ = new YieldBreak (lexer.Location);
3816 : TRY block catch_clauses
3820 ArrayList c = (ArrayList)$3;
3821 for (int i = 0; i < c.Count; ++i) {
3822 Catch cc = (Catch) c [i];
3824 if (i != c.Count - 1)
3825 Report.Error (1017, cc.loc, "Empty catch block must be the last in a series of catch blocks");
3832 // Now s contains the list of specific catch clauses
3833 // and g contains the general one.
3835 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
3837 | TRY block opt_catch_clauses FINALLY block
3840 ArrayList s = new ArrayList (4);
3841 ArrayList catch_list = (ArrayList) $3;
3843 if (catch_list != null){
3844 foreach (Catch cc in catch_list) {
3852 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
3856 Report.Error (1524, lexer.Location, "Expected catch or finally");
3861 : /* empty */ { $$ = null; }
3868 ArrayList l = new ArrayList (4);
3873 | catch_clauses catch_clause
3875 ArrayList l = (ArrayList) $1;
3883 : /* empty */ { $$ = null; }
3888 : CATCH opt_catch_args
3890 Expression type = null;
3894 DictionaryEntry cc = (DictionaryEntry) $2;
3895 type = (Expression) cc.Key;
3896 id = (string) cc.Value;
3899 ArrayList one = new ArrayList (4);
3900 Location loc = lexer.Location;
3902 one.Add (new VariableDeclaration (id, null, loc));
3905 current_block = new Block (current_block);
3906 Block b = declare_local_variables (type, one, loc);
3911 Expression type = null;
3915 DictionaryEntry cc = (DictionaryEntry) $2;
3916 type = (Expression) cc.Key;
3917 id = (string) cc.Value;
3921 // FIXME: I can change this for an assignment.
3923 while (current_block != (Block) $1)
3924 current_block = current_block.Parent;
3929 $$ = new Catch (type, id , (Block) $4, ((Block) $4).loc);
3934 : /* empty */ { $$ = null; }
3939 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3941 $$ = new DictionaryEntry ($2, $3);
3948 $$ = new Checked ((Block) $2);
3955 $$ = new Unchecked ((Block) $2);
3962 if (!RootContext.Unsafe){
3963 Report.Error (227, lexer.Location,
3964 "Unsafe code can only be used if --unsafe is used");
3967 $$ = new Unsafe ((Block) $3);
3973 type fixed_pointer_declarators
3976 ArrayList list = (ArrayList) $4;
3977 Expression type = (Expression) $3;
3978 Location l = lexer.Location;
3979 int top = list.Count;
3981 Block assign_block = new Block (current_block);
3982 current_block = assign_block;
3984 for (int i = 0; i < top; i++){
3985 Pair p = (Pair) list [i];
3988 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
4002 Location l = (Location) oob_stack.Pop ();
4004 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4006 if (RootContext.WarningLevel >= 3){
4007 if ($7 == EmptyStatement.Value)
4008 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4011 current_block.AddStatement (f);
4012 while (current_block.Implicit)
4013 current_block = current_block.Parent;
4015 current_block = current_block.Parent;
4019 fixed_pointer_declarators
4020 : fixed_pointer_declarator {
4021 ArrayList declarators = new ArrayList (4);
4023 declarators.Add ($1);
4026 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4028 ArrayList declarators = (ArrayList) $1;
4030 declarators.Add ($3);
4035 fixed_pointer_declarator
4036 : IDENTIFIER ASSIGN expression
4038 $$ = new Pair ($1, $3);
4042 Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
4048 : LOCK OPEN_PARENS expression CLOSE_PARENS
4054 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4059 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4061 Block assign_block = new Block (current_block);
4062 current_block = assign_block;
4064 oob_stack.Push (lexer.Location);
4066 if ($3 is DictionaryEntry){
4067 DictionaryEntry de = (DictionaryEntry) $3;
4068 Location l = lexer.Location;
4070 Expression type = (Expression) de.Key;
4071 ArrayList var_declarators = (ArrayList) de.Value;
4073 ArrayList vars = new ArrayList (4);
4075 foreach (VariableDeclaration decl in var_declarators){
4077 LocalInfo vi = current_block.AddVariable (
4078 type, decl.identifier,
4079 current_local_parameters, decl.Location);
4085 if (decl.expression_or_array_initializer is Expression){
4086 expr = (Expression) decl.expression_or_array_initializer;
4088 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4090 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4093 expr = new ArrayCreation (type, "", init, decl.Location);
4096 LocalVariableReference var;
4098 // Get a writable reference to this read-only variable.
4099 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4101 // This is so that it is not a warning on using variables
4104 vars.Add (new DictionaryEntry (var, expr));
4106 // Assign a = new Assign (var, expr, decl.Location);
4107 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4109 $3 = new DictionaryEntry (type, vars);
4114 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
4115 current_block.AddStatement (u);
4116 while (current_block.Implicit)
4117 current_block = current_block.Parent;
4119 current_block = current_block.Parent;
4123 resource_acquisition
4124 : local_variable_declaration
4131 // A class used to pass around variable declarations and constants
4133 public class VariableDeclaration {
4134 public string identifier;
4135 public object expression_or_array_initializer;
4136 public Location Location;
4137 public Attributes OptAttributes;
4139 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4141 this.identifier = id;
4142 this.expression_or_array_initializer = eoai;
4144 this.OptAttributes = opt_attrs;
4147 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4153 /// Used to pass around interface property information
4155 public class InterfaceAccessorInfo {
4157 public readonly Accessor Get, Set;
4159 public InterfaceAccessorInfo (bool has_get, bool has_set,
4160 Attributes get_attrs, Attributes set_attrs, Location get_loc, Location set_loc)
4163 Get = new Accessor (null, 0, get_attrs, get_loc);
4165 Set = new Accessor (null, 0, set_attrs, set_loc);
4171 // A class used to hold info about an indexer declarator
4173 public class IndexerDeclaration {
4174 public Expression type;
4175 public MemberName interface_type;
4176 public Parameters param_list;
4178 public IndexerDeclaration (Expression type, MemberName interface_type,
4179 Parameters param_list)
4182 this.interface_type = interface_type;
4183 this.param_list = param_list;
4188 // We use this when we do not have an object in advance that is an IIteratorContainer
4190 public class SimpleIteratorContainer : IIteratorContainer {
4193 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4198 public static SimpleIteratorContainer GetSimple () {
4199 Simple.Yields = false;
4203 public void SetYields () { Yields = true; }
4207 // A class used to hold info about an operator declarator
4209 public class OperatorDeclaration {
4210 public Operator.OpType optype;
4211 public Expression ret_type, arg1type, arg2type;
4212 public string arg1name, arg2name;
4213 public Location location;
4215 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4216 Expression arg1type, string arg1name,
4217 Expression arg2type, string arg2name, Location location)
4220 this.ret_type = ret_type;
4221 this.arg1type = arg1type;
4222 this.arg1name = arg1name;
4223 this.arg2type = arg2type;
4224 this.arg2name = arg2name;
4225 this.location = location;
4230 void Error_ExpectingTypeName (Location l, Expression expr)
4232 if (expr is Invocation){
4233 Report.Error (1002, l, "; expected");
4235 Report.Error (-1, l, "Invalid Type definition");
4240 // Given the @class_name name, it creates a fully qualified name
4241 // based on the containing declaration space
4244 MakeName (MemberName class_name)
4246 string ns = current_namespace.FullName;
4248 if (current_container.Name == ""){
4250 return new MemberName (new MemberName (ns), class_name);
4254 return new MemberName (current_container.MemberName, class_name);
4258 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4260 Block implicit_block;
4261 ArrayList inits = null;
4264 // We use the `Used' property to check whether statements
4265 // have been added to the current block. If so, we need
4266 // to create another block to contain the new declaration
4267 // otherwise, as an optimization, we use the same block to
4268 // add the declaration.
4270 // FIXME: A further optimization is to check if the statements
4271 // that were added were added as part of the initialization
4272 // below. In which case, no other statements have been executed
4273 // and we might be able to reduce the number of blocks for
4274 // situations like this:
4276 // int j = 1; int k = j + 1;
4278 if (current_block.Used)
4279 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4281 implicit_block = current_block;
4283 foreach (VariableDeclaration decl in variable_declarators){
4285 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4286 if (decl.expression_or_array_initializer != null){
4288 inits = new ArrayList (4);
4295 return implicit_block;
4297 foreach (VariableDeclaration decl in inits){
4301 if (decl.expression_or_array_initializer is Expression){
4302 expr = (Expression) decl.expression_or_array_initializer;
4305 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4307 expr = new ArrayCreation (type, "", init, decl.Location);
4310 LocalVariableReference var;
4311 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4313 assign = new Assign (var, expr, decl.Location);
4315 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4318 return implicit_block;
4321 Block declare_local_constants (Expression type, ArrayList declarators)
4323 Block implicit_block;
4325 if (current_block.Used)
4326 implicit_block = new Block (current_block, Block.Flags.Implicit);
4328 implicit_block = current_block;
4330 foreach (VariableDeclaration decl in declarators){
4331 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4332 current_local_parameters, decl.Location);
4335 return implicit_block;
4338 void CheckAttributeTarget (string a)
4342 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4346 Location l = lexer.Location;
4347 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4353 void CheckUnaryOperator (Operator.OpType op)
4357 case Operator.OpType.LogicalNot:
4358 case Operator.OpType.OnesComplement:
4359 case Operator.OpType.Increment:
4360 case Operator.OpType.Decrement:
4361 case Operator.OpType.True:
4362 case Operator.OpType.False:
4363 case Operator.OpType.Addition:
4364 case Operator.OpType.Subtraction:
4369 Location l = lexer.Location;
4370 Report.Error (1019, l, "Overloadable unary operator expected");
4376 void CheckBinaryOperator (Operator.OpType op)
4380 case Operator.OpType.Addition:
4381 case Operator.OpType.Subtraction:
4382 case Operator.OpType.Multiply:
4383 case Operator.OpType.Division:
4384 case Operator.OpType.Modulus:
4385 case Operator.OpType.BitwiseAnd:
4386 case Operator.OpType.BitwiseOr:
4387 case Operator.OpType.ExclusiveOr:
4388 case Operator.OpType.LeftShift:
4389 case Operator.OpType.RightShift:
4390 case Operator.OpType.Equality:
4391 case Operator.OpType.Inequality:
4392 case Operator.OpType.GreaterThan:
4393 case Operator.OpType.LessThan:
4394 case Operator.OpType.GreaterThanOrEqual:
4395 case Operator.OpType.LessThanOrEqual:
4399 Location l = lexer.Location;
4400 Report.Error (1020, l, "Overloadable binary operator expected");
4406 void syntax_error (Location l, string msg)
4408 Report.Error (1003, l, "Syntax error, " + msg);
4411 void output (string s)
4413 Console.WriteLine (s);
4416 void note (string s)
4418 // Used to put annotations
4423 public Tokenizer Lexer {
4429 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4431 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4432 this.name = file.Name;
4434 current_container = RootContext.Tree.Types;
4435 current_container.NamespaceEntry = current_namespace;
4436 oob_stack = new Stack ();
4437 switch_stack = new Stack ();
4439 lexer = new Tokenizer (reader, file, defines);
4442 public void parse ()
4445 if (yacc_verbose_flag)
4446 yyparse (lexer, new yydebug.yyDebugSimple ());
4449 Tokenizer tokenizer = lexer as Tokenizer;
4450 tokenizer.cleanup ();
4451 } catch (Exception e){
4453 // Removed for production use, use parser verbose to get the output.
4455 // Console.WriteLine (e);
4456 Report.Error (-25, lexer.Location, "Parsing error");
4457 if (Driver.parser_verbose)
4458 Console.WriteLine (e);
4462 void CheckToken (int error, int yyToken, string msg)
4464 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4465 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4468 Report.Error (error, lexer.Location, msg);
4471 void CheckIdentifierToken (int yyToken)
4473 CheckToken (1041, yyToken, "Identifier expected");