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;
676 : IDENTIFIER ASSIGN expression
678 $$ = new DictionaryEntry (
680 new Argument ((Expression) $3, Argument.AType.Expression));
686 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
689 opt_class_member_declarations
691 | class_member_declarations
694 class_member_declarations
695 : class_member_declaration
696 | class_member_declarations
697 class_member_declaration
700 class_member_declaration
701 : constant_declaration // done
702 | field_declaration // done
703 | method_declaration // done
704 | property_declaration // done
705 | event_declaration // done
706 | indexer_declaration // done
707 | operator_declaration // done
708 | constructor_declaration // done
709 | destructor_declaration // done
719 MemberName name = MakeName ((MemberName) $5);
720 bool partial = (bool) $3;
723 ClassPart part = PartialContainer.CreatePart (
724 current_namespace, current_container, name, (int) $2,
725 (Attributes) $1, Kind.Struct, lexer.Location);
727 current_container = part.PartialContainer;
728 current_class = part;
730 current_class = new Struct (
731 current_namespace, current_container, name, (int) $2,
732 (Attributes) $1, lexer.Location);
734 current_container = current_class;
735 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
741 current_class.Bases = (ArrayList) $7;
743 current_class.Register ();
750 current_container = current_container.Parent;
751 current_class = current_container;
753 | opt_attributes opt_modifiers opt_partial STRUCT error {
754 CheckIdentifierToken (yyToken);
759 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
762 opt_struct_member_declarations
764 | struct_member_declarations
767 struct_member_declarations
768 : struct_member_declaration
769 | struct_member_declarations struct_member_declaration
772 struct_member_declaration
773 : constant_declaration
776 | property_declaration
778 | indexer_declaration
779 | operator_declaration
780 | constructor_declaration
784 * This is only included so we can flag error 575:
785 * destructors only allowed on class types
787 | destructor_declaration
798 int modflags = (int) $2;
799 foreach (VariableDeclaration constant in (ArrayList) $5){
800 Location l = constant.Location;
801 if ((modflags & Modifiers.STATIC) != 0) {
802 Report.Error (504, l, "The constant '{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
806 Const c = new Const (
807 current_class, (Expression) $4, (string) constant.identifier,
808 (Expression) constant.expression_or_array_initializer, modflags,
811 current_container.AddConstant (c);
817 : constant_declarator
819 ArrayList constants = new ArrayList (4);
824 | constant_declarators COMMA constant_declarator
827 ArrayList constants = (ArrayList) $1;
834 : IDENTIFIER ASSIGN constant_expression
836 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
840 // A const field requires a value to be provided
841 Report.Error (145, lexer.Location, "A const field requires a value to be provided");
853 Expression type = (Expression) $3;
856 foreach (VariableDeclaration var in (ArrayList) $4){
857 Location l = var.Location;
859 Field field = new Field (current_class, type, mod, var.identifier,
860 var.expression_or_array_initializer,
863 current_container.AddField (field);
871 Report.Error (670, lexer.Location, "void type is not allowed for fields");
876 : variable_declarator
878 ArrayList decl = new ArrayList (4);
882 | variable_declarators COMMA variable_declarator
884 ArrayList decls = (ArrayList) $1;
891 : IDENTIFIER ASSIGN variable_initializer
893 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
897 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
910 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
912 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
918 iterator_container = (IIteratorContainer) $1;
922 Method method = (Method) $1;
923 Block b = (Block) $3;
924 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
927 if ((method.ModFlags & extern_abstract) == 0){
929 501, lexer.Location, current_container.MakeName (method.Name) +
930 "must declare a body because it is not marked abstract or extern");
933 if ((method.ModFlags & Modifiers.EXTERN) != 0){
935 179, lexer.Location, current_container.MakeName (method.Name) +
936 " is declared extern, but has a body");
940 method.Block = (ToplevelBlock) $3;
941 current_container.AddMethod (method);
943 current_local_parameters = null;
944 iterator_container = null;
958 1585, lexer.Location, "Member modifier `" +
959 Modifiers.Name (i) + "' must precede member type and name");
970 type namespace_or_type_name
971 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
973 MemberName name = (MemberName) $4;
975 Method method = new Method (current_class, (Expression) $3, (int) $2,
976 false, name, (Parameters) $6, (Attributes) $1,
979 current_local_parameters = (Parameters) $6;
985 VOID namespace_or_type_name
986 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
988 MemberName name = (MemberName) $4;
990 Method method = new Method (current_class, TypeManager.system_void_expr,
991 (int) $2, false, name, (Parameters) $6,
992 (Attributes) $1, lexer.Location);
994 current_local_parameters = (Parameters) $6;
1000 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1002 Report.Error (1585, lexer.Location,
1003 String.Format ("Modifier {0} should appear before type",
1004 Modifiers.Name ((int) $4)));
1005 MemberName name = (MemberName) $4;
1007 Method method = new Method (current_class, TypeManager.system_void_expr,
1008 0, false, name, (Parameters) $7, (Attributes) $1,
1011 current_local_parameters = (Parameters) $7;
1018 | SEMICOLON { $$ = null; }
1021 opt_formal_parameter_list
1022 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1023 | formal_parameter_list
1026 formal_parameter_list
1029 ArrayList pars_list = (ArrayList) $1;
1031 Parameter [] pars = new Parameter [pars_list.Count];
1032 pars_list.CopyTo (pars);
1034 $$ = new Parameters (pars, null, lexer.Location);
1036 | fixed_parameters COMMA parameter_array
1038 ArrayList pars_list = (ArrayList) $1;
1040 Parameter [] pars = new Parameter [pars_list.Count];
1041 pars_list.CopyTo (pars);
1043 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
1045 | fixed_parameters COMMA ARGLIST
1047 ArrayList pars_list = (ArrayList) $1;
1049 Parameter [] pars = new Parameter [pars_list.Count];
1050 pars_list.CopyTo (pars);
1052 $$ = new Parameters (pars, true, lexer.Location);
1054 | parameter_array COMMA fixed_parameters
1056 Report.Error (231, lexer.Location, "A params parameter must be the last parameter in a formal parameter list");
1059 | ARGLIST COMMA fixed_parameters
1061 Report.Error (257, lexer.Location, "An __arglist parameter must be the last parameter in a formal parameter list");
1066 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
1070 $$ = new Parameters (null, true, lexer.Location);
1077 ArrayList pars = new ArrayList (4);
1082 | fixed_parameters COMMA fixed_parameter
1084 ArrayList pars = (ArrayList) $1;
1093 opt_parameter_modifier
1097 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
1100 opt_parameter_modifier
1103 CheckIdentifierToken (yyToken);
1107 opt_parameter_modifier
1113 Report.Error (241, lexer.Location, "Default parameter specifiers are not permitted");
1118 opt_parameter_modifier
1119 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1120 | parameter_modifier
1124 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1125 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1129 : opt_attributes PARAMS type IDENTIFIER
1131 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1132 note ("type must be a single-dimension array type");
1134 | opt_attributes PARAMS type error {
1135 CheckIdentifierToken (yyToken);
1140 property_declaration
1143 type namespace_or_type_name
1146 implicit_value_parameter_type = (Expression) $3;
1148 lexer.PropertyParsing = true;
1150 $$ = lexer.Location;
1152 iterator_container = SimpleIteratorContainer.GetSimple ();
1154 accessor_declarations
1156 lexer.PropertyParsing = false;
1161 Pair pair = (Pair) $7;
1162 Accessor get_block = (Accessor) pair.First;
1163 Accessor set_block = (Accessor) pair.Second;
1165 Location loc = (Location) $6;
1166 MemberName name = (MemberName) $4;
1168 prop = new Property (current_class, (Expression) $3, (int) $2, false,
1169 name, (Attributes) $1, get_block, set_block, loc);
1170 if (SimpleIteratorContainer.Simple.Yields)
1173 current_container.AddProperty (prop);
1174 implicit_value_parameter_type = null;
1175 iterator_container = null;
1179 accessor_declarations
1180 : get_accessor_declaration opt_set_accessor_declaration
1182 $$ = new Pair ($1, $2);
1184 | set_accessor_declaration opt_get_accessor_declaration
1186 $$ = new Pair ($2, $1);
1190 opt_get_accessor_declaration
1191 : /* empty */ { $$ = null; }
1192 | get_accessor_declaration
1195 opt_set_accessor_declaration
1196 : /* empty */ { $$ = null; }
1197 | set_accessor_declaration
1200 get_accessor_declaration
1201 : opt_attributes GET
1203 // If this is not the case, then current_local_parameters has already
1204 // been set in indexer_declaration
1205 if (parsing_indexer == false)
1206 current_local_parameters = null;
1208 current_local_parameters = indexer_parameters;
1209 lexer.PropertyParsing = false;
1213 $$ = new Accessor ((ToplevelBlock) $4, (Attributes) $1, lexer.Location);
1214 current_local_parameters = null;
1215 lexer.PropertyParsing = true;
1219 set_accessor_declaration
1220 : opt_attributes SET
1223 Parameter implicit_value_parameter = new Parameter (
1224 implicit_value_parameter_type, "value",
1225 Parameter.Modifier.NONE, null);
1227 if (parsing_indexer == false) {
1228 args = new Parameter [1];
1229 args [0] = implicit_value_parameter;
1230 current_local_parameters = new Parameters (args, null, lexer.Location);
1232 Parameter [] fpars = indexer_parameters.FixedParameters;
1235 int count = fpars.Length;
1237 args = new Parameter [count + 1];
1238 fpars.CopyTo (args, 0);
1239 args [count] = implicit_value_parameter;
1242 current_local_parameters = new Parameters (
1243 args, indexer_parameters.ArrayParameter, lexer.Location);
1246 lexer.PropertyParsing = false;
1250 $$ = new Accessor ((ToplevelBlock) $4, (Attributes) $1, lexer.Location);
1251 current_local_parameters = null;
1252 lexer.PropertyParsing = true;
1258 | SEMICOLON { $$ = null; }
1261 interface_declaration
1265 INTERFACE member_name
1267 MemberName name = MakeName ((MemberName) $5);
1268 bool partial = (bool) $3;
1271 ClassPart part = PartialContainer.CreatePart (
1272 current_namespace, current_container, name, (int) $2,
1273 (Attributes) $1, Kind.Interface, lexer.Location);
1275 current_container = part.PartialContainer;
1276 current_class = part;
1278 current_class = new Interface (
1279 current_namespace, current_container, name, (int) $2,
1280 (Attributes) $1, lexer.Location);
1282 current_container = current_class;
1283 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
1288 current_class.Bases = (ArrayList) $7;
1290 current_class.Register ();
1297 current_container = current_container.Parent;
1298 current_class = current_container;
1300 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1301 CheckIdentifierToken (yyToken);
1307 opt_interface_member_declarations
1311 opt_interface_member_declarations
1313 | interface_member_declarations
1316 interface_member_declarations
1317 : interface_member_declaration
1318 | interface_member_declarations interface_member_declaration
1321 interface_member_declaration
1322 : interface_method_declaration
1324 Method m = (Method) $1;
1326 current_container.AddMethod (m);
1328 | interface_property_declaration
1330 Property p = (Property) $1;
1332 current_container.AddProperty (p);
1334 | interface_event_declaration
1337 Event e = (Event) $1;
1338 current_container.AddEvent (e);
1341 | interface_indexer_declaration
1343 Indexer i = (Indexer) $1;
1345 current_container.AddIndexer (i);
1353 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, lexer.Location);
1358 interface_method_declaration
1359 : opt_attributes opt_new type namespace_or_type_name
1360 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1363 MemberName name = (MemberName) $4;
1365 $$ = new Method (current_class, (Expression) $3, (int) $2, true,
1366 name, (Parameters) $6, (Attributes) $1, lexer.Location);
1368 | opt_attributes opt_new VOID namespace_or_type_name
1369 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1372 MemberName name = (MemberName) $4;
1374 $$ = new Method (current_class, TypeManager.system_void_expr, (int) $2,
1375 true, name, (Parameters) $6, (Attributes) $1, lexer.Location);
1379 interface_property_declaration
1384 { lexer.PropertyParsing = true; }
1386 { lexer.PropertyParsing = false; }
1389 InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
1391 $$ = new Property (current_class, (Expression) $3, (int) $2, true,
1392 new MemberName ((string) $4), (Attributes) $1,
1393 pinfo.Get, pinfo.Set, lexer.Location);
1398 CheckIdentifierToken (yyToken);
1404 : opt_attributes GET SEMICOLON { $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, lexer.Location, lexer.Location); }
1405 | opt_attributes SET SEMICOLON { $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1, lexer.Location, lexer.Location); }
1406 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1407 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $3, lexer.Location, lexer.Location); }
1408 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1409 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $3, (Attributes) $1, lexer.Location, lexer.Location); }
1412 interface_event_declaration
1413 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1415 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1416 new MemberName ((string) $5), null,
1417 (Attributes) $1, lexer.Location);
1419 | opt_attributes opt_new EVENT type error {
1420 CheckIdentifierToken (yyToken);
1423 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1424 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1427 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE event_accessor_declarations CLOSE_BRACE {
1428 Report.Error (69, lexer.Location, "Event accessors not valid on interfaces");
1433 interface_indexer_declaration
1434 : opt_attributes opt_new type THIS
1435 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1437 { lexer.PropertyParsing = true; }
1439 { lexer.PropertyParsing = false; }
1442 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1444 $$ = new Indexer (current_class, (Expression) $3,
1445 new MemberName (TypeContainer.DefaultIndexerName),
1446 (int) $2, true, (Parameters) $6, (Attributes) $1,
1447 info.Get, info.Set, lexer.Location);
1451 operator_declaration
1452 : opt_attributes opt_modifiers operator_declarator
1454 iterator_container = SimpleIteratorContainer.GetSimple ();
1458 OperatorDeclaration decl = (OperatorDeclaration) $3;
1460 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1462 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null);
1463 if (decl.arg2type != null)
1464 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null);
1466 Operator op = new Operator (
1467 current_class, decl.optype, decl.ret_type, (int) $2,
1468 new Parameters (param_list, null, decl.location),
1469 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1471 if (SimpleIteratorContainer.Simple.Yields)
1474 // Note again, checking is done in semantic analysis
1475 current_container.AddOperator (op);
1477 current_local_parameters = null;
1478 iterator_container = null;
1484 | SEMICOLON { $$ = null; }
1487 : type OPERATOR overloadable_operator
1488 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1490 Operator.OpType op = (Operator.OpType) $3;
1491 CheckUnaryOperator (op);
1493 if (op == Operator.OpType.Addition)
1494 op = Operator.OpType.UnaryPlus;
1496 if (op == Operator.OpType.Subtraction)
1497 op = Operator.OpType.UnaryNegation;
1499 Parameter [] pars = new Parameter [1];
1501 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1503 current_local_parameters = new Parameters (pars, null, lexer.Location);
1505 $$ = new OperatorDeclaration (op, (Expression) $1, (Expression) $5, (string) $6,
1506 null, null, lexer.Location);
1508 | type OPERATOR overloadable_operator
1510 type IDENTIFIER COMMA
1514 CheckBinaryOperator ((Operator.OpType) $3);
1516 Parameter [] pars = new Parameter [2];
1518 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1519 pars [1] = new Parameter ((Expression) $8, (string) $9, Parameter.Modifier.NONE, null);
1521 current_local_parameters = new Parameters (pars, null, lexer.Location);
1523 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1524 (Expression) $5, (string) $6,
1525 (Expression) $8, (string) $9, lexer.Location);
1527 | conversion_operator_declarator
1530 overloadable_operator
1532 : BANG { $$ = Operator.OpType.LogicalNot; }
1533 | TILDE { $$ = Operator.OpType.OnesComplement; }
1534 | OP_INC { $$ = Operator.OpType.Increment; }
1535 | OP_DEC { $$ = Operator.OpType.Decrement; }
1536 | TRUE { $$ = Operator.OpType.True; }
1537 | FALSE { $$ = Operator.OpType.False; }
1538 // Unary and binary:
1539 | PLUS { $$ = Operator.OpType.Addition; }
1540 | MINUS { $$ = Operator.OpType.Subtraction; }
1542 | STAR { $$ = Operator.OpType.Multiply; }
1543 | DIV { $$ = Operator.OpType.Division; }
1544 | PERCENT { $$ = Operator.OpType.Modulus; }
1545 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1546 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1547 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1548 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1549 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1550 | OP_EQ { $$ = Operator.OpType.Equality; }
1551 | OP_NE { $$ = Operator.OpType.Inequality; }
1552 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1553 | OP_LT { $$ = Operator.OpType.LessThan; }
1554 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1555 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1558 conversion_operator_declarator
1559 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1561 Parameter [] pars = new Parameter [1];
1563 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1565 current_local_parameters = new Parameters (pars, null, lexer.Location);
1567 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1568 null, null, lexer.Location);
1570 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1572 Parameter [] pars = new Parameter [1];
1574 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1576 current_local_parameters = new Parameters (pars, null, lexer.Location);
1578 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1579 null, null, lexer.Location);
1583 syntax_error (lexer.Location, "'operator' expected");
1587 syntax_error (lexer.Location, "'operator' expected");
1591 constructor_declaration
1594 constructor_declarator
1597 Constructor c = (Constructor) $3;
1598 c.Block = (ToplevelBlock) $4;
1599 c.OptAttributes = (Attributes) $1;
1600 c.ModFlags = (int) $2;
1602 if (c.Name == current_container.Basename){
1603 if ((c.ModFlags & Modifiers.STATIC) != 0){
1604 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1606 515, c.Location, String.Format (
1607 "`{0}.{1}': static constructor can not have access modifiers",
1608 c.Name, current_container.Name));
1611 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1613 if (c.Initializer != null){
1616 "Static constructors can not have an explicit this or base " +
1617 "constructor invocations");
1620 if (!c.Parameters.Empty){
1622 132, c.Location, "Static constructors should not have parameters");
1625 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1628 // We let another layer check the validity of the constructor.
1629 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1632 current_container.AddConstructor (c);
1634 current_local_parameters = null;
1638 constructor_declarator
1640 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1642 oob_stack.Push (lexer.Location);
1644 current_local_parameters = (Parameters) $3;
1646 opt_constructor_initializer
1648 Location l = (Location) oob_stack.Pop ();
1649 $$ = new Constructor (current_class, (string) $1, 0, (Parameters) $3,
1650 (ConstructorInitializer) $6, l);
1656 | SEMICOLON { $$ = null; }
1659 opt_constructor_initializer
1660 : /* empty */ { $$ = null; }
1661 | constructor_initializer
1664 constructor_initializer
1665 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1667 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1669 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1671 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1674 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1680 : /* EMPTY */ { $$ = 0; }
1681 | UNSAFE { $$ = Modifiers.UNSAFE; }
1682 | EXTERN { $$ = Modifiers.EXTERN; }
1685 destructor_declaration
1686 : opt_attributes opt_finalizer TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1688 if ((string) $4 != current_container.Basename){
1689 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1690 } else if (!(current_container is Class)){
1691 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1693 Location l = lexer.Location;
1696 if (!RootContext.StdLib && current_container.Name == "System.Object")
1697 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
1699 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
1701 if ((m & Modifiers.UNSAFE) != 0){
1702 if (!RootContext.Unsafe){
1703 Report.Error (227, l,
1704 "Unsafe code requires the -unsafe command " +
1705 "line option to be specified");
1709 Method d = new Destructor (
1710 current_class, TypeManager.system_void_expr, m, "Finalize",
1711 new Parameters (null, null, l), (Attributes) $1, l);
1713 d.Block = (ToplevelBlock) $7;
1714 current_container.AddMethod (d);
1722 EVENT type variable_declarators SEMICOLON
1724 foreach (VariableDeclaration var in (ArrayList) $5) {
1726 MemberName name = new MemberName (var.identifier);
1728 Event e = new EventField (
1729 current_class, (Expression) $4, (int) $2, false, name,
1730 var.expression_or_array_initializer, (Attributes) $1,
1733 current_container.AddEvent (e);
1739 EVENT type namespace_or_type_name
1742 implicit_value_parameter_type = (Expression) $4;
1743 lexer.EventParsing = true;
1744 oob_stack.Push (lexer.Location);
1746 event_accessor_declarations
1748 lexer.EventParsing = false;
1752 Location loc = (Location) oob_stack.Pop ();
1755 Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
1758 Pair pair = (Pair) $8;
1760 MemberName name = (MemberName) $5;
1762 Event e = new EventProperty (
1763 current_class, (Expression) $4, (int) $2, false, name, null,
1764 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
1767 current_container.AddEvent (e);
1768 implicit_value_parameter_type = null;
1771 | opt_attributes opt_modifiers EVENT type namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS block {
1772 MemberName mn = (MemberName) $5;
1774 if (mn.Left != null)
1775 Report.Error (71, lexer.Location, "Explicit implementation of events requires property syntax");
1777 Report.Error (71, lexer.Location, "Event declaration should use property syntax");
1781 event_accessor_declarations
1782 : add_accessor_declaration remove_accessor_declaration
1784 $$ = new Pair ($1, $2);
1786 | remove_accessor_declaration add_accessor_declaration
1788 $$ = new Pair ($2, $1);
1790 | add_accessor_declaration { $$ = null; }
1791 | remove_accessor_declaration { $$ = null; }
1794 add_accessor_declaration
1795 : opt_attributes ADD
1797 Parameter [] args = new Parameter [1];
1798 Parameter implicit_value_parameter = new Parameter (
1799 implicit_value_parameter_type, "value",
1800 Parameter.Modifier.NONE, null);
1802 args [0] = implicit_value_parameter;
1804 current_local_parameters = new Parameters (args, null, lexer.Location);
1805 lexer.EventParsing = false;
1809 $$ = new Accessor ((ToplevelBlock) $4, (Attributes) $1, lexer.Location);
1810 lexer.EventParsing = true;
1812 | opt_attributes ADD error {
1813 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1818 remove_accessor_declaration
1819 : opt_attributes REMOVE
1821 Parameter [] args = new Parameter [1];
1822 Parameter implicit_value_parameter = new Parameter (
1823 implicit_value_parameter_type, "value",
1824 Parameter.Modifier.NONE, null);
1826 args [0] = implicit_value_parameter;
1828 current_local_parameters = new Parameters (args, null, lexer.Location);
1829 lexer.EventParsing = false;
1833 $$ = new Accessor ((ToplevelBlock) $4, (Attributes) $1, lexer.Location);
1834 lexer.EventParsing = true;
1836 | opt_attributes REMOVE error {
1837 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1843 : opt_attributes opt_modifiers indexer_declarator
1846 IndexerDeclaration decl = (IndexerDeclaration) $3;
1848 implicit_value_parameter_type = decl.type;
1850 lexer.PropertyParsing = true;
1851 parsing_indexer = true;
1853 indexer_parameters = decl.param_list;
1854 oob_stack.Push (lexer.Location);
1856 accessor_declarations
1858 lexer.PropertyParsing = false;
1859 parsing_indexer = false;
1863 // The signature is computed from the signature of the indexer. Look
1864 // at section 3.6 on the spec
1865 Location loc = (Location) oob_stack.Pop ();
1867 IndexerDeclaration decl = (IndexerDeclaration) $3;
1868 Pair pair = (Pair) $6;
1869 Accessor get_block = (Accessor) pair.First;
1870 Accessor set_block = (Accessor) pair.Second;
1873 if (decl.interface_type != null)
1874 name = new MemberName (decl.interface_type,
1875 TypeContainer.DefaultIndexerName);
1877 name = new MemberName (TypeContainer.DefaultIndexerName);
1879 indexer = new Indexer (current_class, decl.type, name,
1880 (int) $2, false, decl.param_list, (Attributes) $1,
1881 get_block, set_block, loc);
1883 current_container.AddIndexer (indexer);
1885 current_local_parameters = null;
1886 implicit_value_parameter_type = null;
1887 indexer_parameters = null;
1892 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1894 Parameters pars = (Parameters) $4;
1895 if (pars.HasArglist) {
1896 // "__arglist is not valid in this context"
1897 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
1898 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
1899 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1902 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
1904 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1906 Parameters pars = (Parameters) $6;
1908 if (pars.HasArglist) {
1909 // "__arglist is not valid in this context"
1910 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
1911 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
1912 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1914 MemberName name = (MemberName) $2;
1915 $$ = new IndexerDeclaration ((Expression) $1, name, pars);
1927 Location enum_location = lexer.Location;
1929 MemberName full_name = MakeName (new MemberName ((string) $4));
1930 Enum e = new Enum (current_namespace, current_container, (Expression) $5, (int) $2,
1931 full_name, (Attributes) $1, enum_location);
1933 foreach (VariableDeclaration ev in (ArrayList) $6) {
1934 e.AddEnumMember (ev.identifier,
1935 (Expression) ev.expression_or_array_initializer,
1936 ev.Location, ev.OptAttributes);
1939 string name = full_name.GetName ();
1940 current_container.AddEnum (e);
1941 RootContext.Tree.RecordDecl (name, e);
1947 : /* empty */ { $$ = TypeManager.system_int32_expr; }
1948 | COLON type { $$ = $2; }
1952 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1958 opt_enum_member_declarations
1959 : /* empty */ { $$ = new ArrayList (4); }
1960 | enum_member_declarations opt_comma { $$ = $1; }
1963 enum_member_declarations
1964 : enum_member_declaration
1966 ArrayList l = new ArrayList (4);
1971 | enum_member_declarations COMMA enum_member_declaration
1973 ArrayList l = (ArrayList) $1;
1981 enum_member_declaration
1982 : opt_attributes IDENTIFIER
1984 $$ = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
1986 | opt_attributes IDENTIFIER
1988 $$ = lexer.Location;
1992 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
1996 delegate_declaration
1999 DELEGATE type member_name
2000 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2003 Location l = lexer.Location;
2004 MemberName name = MakeName ((MemberName) $5);
2005 Delegate del = new Delegate (current_namespace, current_container, (Expression) $4,
2006 (int) $2, name, (Parameters) $7, (Attributes) $1, l);
2008 current_container.AddDelegate (del);
2009 RootContext.Tree.RecordDecl (name.GetName (true), del);
2013 DELEGATE VOID member_name
2014 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2017 Location l = lexer.Location;
2018 MemberName name = MakeName ((MemberName) $5);
2019 Delegate del = new Delegate (
2020 current_namespace, current_container,
2021 TypeManager.system_void_expr, (int) $2, name,
2022 (Parameters) $7, (Attributes) $1, l);
2024 current_container.AddDelegate (del);
2025 RootContext.Tree.RecordDecl (name.GetName (true), del);
2029 namespace_or_type_name
2031 | namespace_or_type_name DOT IDENTIFIER {
2032 $$ = new MemberName ((MemberName) $1, (string) $3);
2038 $$ = new MemberName ((string) $1);
2043 * Before you think of adding a return_type, notice that we have been
2044 * using two rules in the places where it matters (one rule using type
2045 * and another identical one that uses VOID as the return type). This
2046 * gets rid of a shift/reduce couple
2049 : namespace_or_type_name
2051 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2063 // Note that here only unmanaged types are allowed but we
2064 // can't perform checks during this phase - we do it during
2065 // semantic analysis.
2067 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2071 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2077 | non_expression_type rank_specifier
2079 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2081 | non_expression_type STAR
2083 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2085 | expression rank_specifiers
2087 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2091 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2095 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2096 // through this different path
2098 | multiplicative_expression STAR
2100 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2107 ArrayList types = new ArrayList (4);
2112 | type_list COMMA type
2114 ArrayList types = (ArrayList) $1;
2122 * replaces all the productions for isolating the various
2123 * simple types, but we need this to reuse it easily in local_variable_type
2126 : OBJECT { $$ = TypeManager.system_object_expr; }
2127 | STRING { $$ = TypeManager.system_string_expr; }
2128 | BOOL { $$ = TypeManager.system_boolean_expr; }
2129 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2130 | FLOAT { $$ = TypeManager.system_single_expr; }
2131 | DOUBLE { $$ = TypeManager.system_double_expr; }
2136 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2137 | BYTE { $$ = TypeManager.system_byte_expr; }
2138 | SHORT { $$ = TypeManager.system_int16_expr; }
2139 | USHORT { $$ = TypeManager.system_uint16_expr; }
2140 | INT { $$ = TypeManager.system_int32_expr; }
2141 | UINT { $$ = TypeManager.system_uint32_expr; }
2142 | LONG { $$ = TypeManager.system_int64_expr; }
2143 | ULONG { $$ = TypeManager.system_uint64_expr; }
2144 | CHAR { $$ = TypeManager.system_char_expr; }
2145 | VOID { $$ = TypeManager.system_void_expr; }
2149 : type rank_specifiers
2151 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2156 // Expressions, section 7.5
2166 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2168 | parenthesized_expression
2170 | invocation_expression
2174 | post_increment_expression
2175 | post_decrement_expression
2179 | checked_expression
2180 | unchecked_expression
2181 | pointer_member_access
2182 | anonymous_method_expression
2189 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2190 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2191 | NULL { $$ = NullLiteral.Null; }
2195 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2196 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2197 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2202 object v = lexer.Value;
2208 $$ = IntLiteral.Zero;
2210 $$ = IntLiteral.One;
2212 $$ = new IntLiteral (i);
2213 } else if (v is uint)
2214 $$ = new UIntLiteral ((UInt32) v);
2216 $$ = new LongLiteral ((Int64) v);
2217 else if (v is ulong)
2218 $$ = new ULongLiteral ((UInt64) v);
2220 Console.WriteLine ("OOPS. Unexpected result from scanner");
2225 : TRUE { $$ = new BoolLiteral (true); }
2226 | FALSE { $$ = new BoolLiteral (false); }
2229 parenthesized_expression_0
2230 : OPEN_PARENS expression CLOSE_PARENS
2233 lexer.Deambiguate_CloseParens ();
2234 // After this, the next token returned is one of
2235 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2236 // or CLOSE_PARENS_MINUS.
2240 parenthesized_expression
2241 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2245 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2247 // If a parenthesized expression is followed by a minus, we need to wrap
2248 // the expression inside a ParenthesizedExpression for the CS0075 check
2249 // in Binary.DoResolve().
2250 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2255 : primary_expression DOT IDENTIFIER
2257 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2259 | predefined_type DOT IDENTIFIER
2261 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2269 invocation_expression
2270 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2273 Location l = lexer.Location;
2274 Report.Error (1, l, "Parse error");
2276 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2278 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2280 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2282 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2284 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2289 : /* empty */ { $$ = null; }
2296 ArrayList list = new ArrayList (4);
2300 | argument_list COMMA argument
2302 ArrayList list = (ArrayList) $1;
2306 | argument_list error {
2307 CheckToken (1026, yyToken, ", or ) expected");
2314 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2316 | REF variable_reference
2318 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2320 | OUT variable_reference
2322 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2324 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2326 ArrayList list = (ArrayList) $3;
2327 Argument[] args = new Argument [list.Count];
2328 list.CopyTo (args, 0);
2330 Expression expr = new Arglist (args, lexer.Location);
2331 $$ = new Argument (expr, Argument.AType.Expression);
2335 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2340 : expression { note ("section 5.4"); $$ = $1; }
2344 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2346 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2348 | primary_expression rank_specifiers
2350 // So the super-trick is that primary_expression
2351 // can only be either a SimpleName or a MemberAccess.
2352 // The MemberAccess case arises when you have a fully qualified type-name like :
2354 // SimpleName is when you have
2357 Expression expr = (Expression) $1;
2358 if (expr is ComposedCast){
2359 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2360 } else if (!(expr is SimpleName || expr is MemberAccess)){
2361 Error_ExpectingTypeName (lexer.Location, expr);
2362 $$ = TypeManager.system_object_expr;
2365 // So we extract the string corresponding to the SimpleName
2368 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2376 ArrayList list = new ArrayList (4);
2380 | expression_list COMMA expression
2382 ArrayList list = (ArrayList) $1;
2391 $$ = new This (current_block, lexer.Location);
2396 : BASE DOT IDENTIFIER
2398 $$ = new BaseAccess ((string) $3, lexer.Location);
2400 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2402 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2405 Report.Error (175, lexer.Location, "Use of keyword `base' is not valid in this context");
2410 post_increment_expression
2411 : primary_expression OP_INC
2413 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2414 (Expression) $1, lexer.Location);
2418 post_decrement_expression
2419 : primary_expression OP_DEC
2421 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2422 (Expression) $1, lexer.Location);
2427 : object_or_delegate_creation_expression
2428 | array_creation_expression
2431 object_or_delegate_creation_expression
2432 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2434 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2438 array_creation_expression
2439 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2441 opt_array_initializer
2443 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2445 | NEW type rank_specifiers array_initializer
2447 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2451 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2467 : rank_specifier opt_rank_specifier
2469 $$ = (string) $2 + (string) $1;
2474 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2476 $$ = "[" + (string) $2 + "]";
2496 | dim_separators COMMA
2498 $$ = (string) $1 + ",";
2502 opt_array_initializer
2514 : OPEN_BRACE CLOSE_BRACE
2516 ArrayList list = new ArrayList (4);
2519 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2521 $$ = (ArrayList) $2;
2525 variable_initializer_list
2526 : variable_initializer
2528 ArrayList list = new ArrayList (4);
2532 | variable_initializer_list COMMA variable_initializer
2534 ArrayList list = (ArrayList) $1;
2541 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2543 $$ = new TypeOfVoid (lexer.Location);
2545 | TYPEOF OPEN_PARENS type CLOSE_PARENS
2547 $$ = new TypeOf ((Expression) $3, lexer.Location);
2552 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2553 $$ = new SizeOf ((Expression) $3, lexer.Location);
2558 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2560 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2564 unchecked_expression
2565 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2567 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2571 pointer_member_access
2572 : primary_expression OP_PTR IDENTIFIER
2576 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2577 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2581 anonymous_method_expression
2582 : DELEGATE opt_anonymous_method_signature {
2583 oob_stack.Push (current_local_parameters);
2584 current_local_parameters = (Parameters)$2;
2586 // Force the next block to be created as a ToplevelBlock
2587 oob_stack.Push (current_block);
2588 oob_stack.Push (top_current_block);
2589 oob_stack.Push (lexer.Location);
2590 current_block = null;
2592 Location loc = (Location) oob_stack.Pop ();
2593 top_current_block = (Block) oob_stack.Pop ();
2594 current_block = (Block) oob_stack.Pop ();
2595 if (RootContext.Version == LanguageVersion.ISO_1){
2596 Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
2599 ToplevelBlock anon_block = (ToplevelBlock) $4;
2601 anon_block.Parent = current_block;
2602 $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block,
2605 current_local_parameters = (Parameters) oob_stack.Pop ();
2609 opt_anonymous_method_signature
2610 : /* empty */ { $$ = null; }
2611 | anonymous_method_signature
2614 anonymous_method_signature
2615 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
2618 $$ = Parameters.EmptyReadOnlyParameters;
2620 ArrayList par_list = (ArrayList) $2;
2621 Parameter [] pars = new Parameter [par_list.Count];
2622 par_list.CopyTo (pars);
2623 $$ = new Parameters (pars, null, lexer.Location);
2628 opt_anonymous_method_parameter_list
2629 : /* empty */ { $$ = null; }
2630 | anonymous_method_parameter_list { $$ = $1; }
2633 anonymous_method_parameter_list
2634 : anonymous_method_parameter
2636 ArrayList a = new ArrayList (4);
2640 | anonymous_method_parameter_list COMMA anonymous_method_parameter
2642 ArrayList a = (ArrayList) $1;
2648 anonymous_method_parameter
2649 : opt_parameter_modifier type IDENTIFIER {
2650 $$ = new Parameter ((Expression) $2, (string) $3, (Parameter.Modifier) $1, null);
2652 | PARAMS type IDENTIFIER {
2653 Report.Error (-221, lexer.Location, "params modifier not allowed in anonymous method declaration");
2659 : primary_expression
2660 | BANG prefixed_unary_expression
2662 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2664 | TILDE prefixed_unary_expression
2666 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2672 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
2674 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2676 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
2678 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2684 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2686 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2691 // The idea to split this out is from Rhys' grammar
2692 // to solve the problem with casts.
2694 prefixed_unary_expression
2696 | PLUS prefixed_unary_expression
2698 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2700 | MINUS prefixed_unary_expression
2702 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2704 | OP_INC prefixed_unary_expression
2706 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2707 (Expression) $2, lexer.Location);
2709 | OP_DEC prefixed_unary_expression
2711 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2712 (Expression) $2, lexer.Location);
2714 | STAR prefixed_unary_expression
2716 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2718 | BITWISE_AND prefixed_unary_expression
2720 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2724 pre_increment_expression
2725 : OP_INC prefixed_unary_expression
2727 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2728 (Expression) $2, lexer.Location);
2732 pre_decrement_expression
2733 : OP_DEC prefixed_unary_expression
2735 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2736 (Expression) $2, lexer.Location);
2740 multiplicative_expression
2741 : prefixed_unary_expression
2742 | multiplicative_expression STAR prefixed_unary_expression
2744 $$ = new Binary (Binary.Operator.Multiply,
2745 (Expression) $1, (Expression) $3, lexer.Location);
2747 | multiplicative_expression DIV prefixed_unary_expression
2749 $$ = new Binary (Binary.Operator.Division,
2750 (Expression) $1, (Expression) $3, lexer.Location);
2752 | multiplicative_expression PERCENT prefixed_unary_expression
2754 $$ = new Binary (Binary.Operator.Modulus,
2755 (Expression) $1, (Expression) $3, lexer.Location);
2760 : multiplicative_expression
2761 | additive_expression PLUS multiplicative_expression
2763 $$ = new Binary (Binary.Operator.Addition,
2764 (Expression) $1, (Expression) $3, lexer.Location);
2766 | additive_expression MINUS multiplicative_expression
2768 $$ = new Binary (Binary.Operator.Subtraction,
2769 (Expression) $1, (Expression) $3, lexer.Location);
2774 : additive_expression
2775 | shift_expression OP_SHIFT_LEFT additive_expression
2777 $$ = new Binary (Binary.Operator.LeftShift,
2778 (Expression) $1, (Expression) $3, lexer.Location);
2780 | shift_expression OP_SHIFT_RIGHT additive_expression
2782 $$ = new Binary (Binary.Operator.RightShift,
2783 (Expression) $1, (Expression) $3, lexer.Location);
2787 relational_expression
2789 | relational_expression OP_LT shift_expression
2791 $$ = new Binary (Binary.Operator.LessThan,
2792 (Expression) $1, (Expression) $3, lexer.Location);
2794 | relational_expression OP_GT shift_expression
2796 $$ = new Binary (Binary.Operator.GreaterThan,
2797 (Expression) $1, (Expression) $3, lexer.Location);
2799 | relational_expression OP_LE shift_expression
2801 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2802 (Expression) $1, (Expression) $3, lexer.Location);
2804 | relational_expression OP_GE shift_expression
2806 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2807 (Expression) $1, (Expression) $3, lexer.Location);
2809 | relational_expression IS type
2811 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
2813 | relational_expression AS type
2815 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
2820 : relational_expression
2821 | equality_expression OP_EQ relational_expression
2823 $$ = new Binary (Binary.Operator.Equality,
2824 (Expression) $1, (Expression) $3, lexer.Location);
2826 | equality_expression OP_NE relational_expression
2828 $$ = new Binary (Binary.Operator.Inequality,
2829 (Expression) $1, (Expression) $3, lexer.Location);
2834 : equality_expression
2835 | and_expression BITWISE_AND equality_expression
2837 $$ = new Binary (Binary.Operator.BitwiseAnd,
2838 (Expression) $1, (Expression) $3, lexer.Location);
2842 exclusive_or_expression
2844 | exclusive_or_expression CARRET and_expression
2846 $$ = new Binary (Binary.Operator.ExclusiveOr,
2847 (Expression) $1, (Expression) $3, lexer.Location);
2851 inclusive_or_expression
2852 : exclusive_or_expression
2853 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2855 $$ = new Binary (Binary.Operator.BitwiseOr,
2856 (Expression) $1, (Expression) $3, lexer.Location);
2860 conditional_and_expression
2861 : inclusive_or_expression
2862 | conditional_and_expression OP_AND inclusive_or_expression
2864 $$ = new Binary (Binary.Operator.LogicalAnd,
2865 (Expression) $1, (Expression) $3, lexer.Location);
2869 conditional_or_expression
2870 : conditional_and_expression
2871 | conditional_or_expression OP_OR conditional_and_expression
2873 $$ = new Binary (Binary.Operator.LogicalOr,
2874 (Expression) $1, (Expression) $3, lexer.Location);
2878 conditional_expression
2879 : conditional_or_expression
2880 | conditional_or_expression INTERR expression COLON expression
2882 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2886 assignment_expression
2887 : prefixed_unary_expression ASSIGN expression
2889 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2891 | prefixed_unary_expression OP_MULT_ASSIGN expression
2893 Location l = lexer.Location;
2895 $$ = new CompoundAssign (
2896 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
2898 | prefixed_unary_expression OP_DIV_ASSIGN expression
2900 Location l = lexer.Location;
2902 $$ = new CompoundAssign (
2903 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
2905 | prefixed_unary_expression OP_MOD_ASSIGN expression
2907 Location l = lexer.Location;
2909 $$ = new CompoundAssign (
2910 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
2912 | prefixed_unary_expression OP_ADD_ASSIGN expression
2914 Location l = lexer.Location;
2916 $$ = new CompoundAssign (
2917 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
2919 | prefixed_unary_expression OP_SUB_ASSIGN expression
2921 Location l = lexer.Location;
2923 $$ = new CompoundAssign (
2924 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
2926 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2928 Location l = lexer.Location;
2930 $$ = new CompoundAssign (
2931 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
2933 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2935 Location l = lexer.Location;
2937 $$ = new CompoundAssign (
2938 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
2940 | prefixed_unary_expression OP_AND_ASSIGN expression
2942 Location l = lexer.Location;
2944 $$ = new CompoundAssign (
2945 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
2947 | prefixed_unary_expression OP_OR_ASSIGN expression
2949 Location l = lexer.Location;
2951 $$ = new CompoundAssign (
2952 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
2954 | prefixed_unary_expression OP_XOR_ASSIGN expression
2956 Location l = lexer.Location;
2958 $$ = new CompoundAssign (
2959 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
2964 : conditional_expression
2965 | assignment_expression
2985 MemberName name = MakeName ((MemberName) $5);
2986 bool partial = (bool) $3;
2987 int mod_flags = (int) $2;
2990 ClassPart part = PartialContainer.CreatePart (
2991 current_namespace, current_container, name, mod_flags,
2992 (Attributes) $1, Kind.Class, lexer.Location);
2994 current_container = part.PartialContainer;
2995 current_class = part;
2997 if ((mod_flags & Modifiers.STATIC) != 0) {
2998 current_class = new StaticClass (
2999 current_namespace, current_container, name,
3000 mod_flags, (Attributes) $1, lexer.Location);
3002 current_class = new Class (
3003 current_namespace, current_container, name,
3004 mod_flags, (Attributes) $1, lexer.Location);
3007 current_container = current_class;
3008 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
3014 if (current_class.Name == "System.Object") {
3015 Report.Error (537, current_class.Location,
3016 "The class System.Object cannot have a base " +
3017 "class or implement an interface.");
3019 current_class.Bases = (ArrayList) $7;
3022 current_class.Register ();
3029 current_container = current_container.Parent;
3030 current_class = current_container;
3036 { $$ = (bool) false; }
3038 { $$ = (bool) true; }
3042 : /* empty */ { $$ = (int) 0; }
3048 | modifiers modifier
3053 if ((m1 & m2) != 0) {
3054 Location l = lexer.Location;
3055 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3057 $$ = (int) (m1 | m2);
3062 : NEW { $$ = Modifiers.NEW; }
3063 | PUBLIC { $$ = Modifiers.PUBLIC; }
3064 | PROTECTED { $$ = Modifiers.PROTECTED; }
3065 | INTERNAL { $$ = Modifiers.INTERNAL; }
3066 | PRIVATE { $$ = Modifiers.PRIVATE; }
3067 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3068 | SEALED { $$ = Modifiers.SEALED; }
3069 | STATIC { $$ = Modifiers.STATIC; }
3070 | READONLY { $$ = Modifiers.READONLY; }
3071 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3072 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3073 | EXTERN { $$ = Modifiers.EXTERN; }
3074 | VOLATILE { $$ = Modifiers.VOLATILE; }
3075 | UNSAFE { $$ = Modifiers.UNSAFE; }
3079 : /* empty */ { $$ = null; }
3080 | class_base { $$ = $1; }
3084 : COLON type_list { $$ = $2; }
3092 // A block is "contained" on the following places:
3094 // property_declaration as part of the accessor body (get/set)
3095 // operator_declaration
3096 // constructor_declaration
3097 // destructor_declaration
3098 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3103 if (current_block == null){
3104 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, lexer.Location);
3105 top_current_block = current_block;
3107 current_block = new Block (current_block, current_local_parameters,
3108 lexer.Location, Location.Null);
3111 opt_statement_list CLOSE_BRACE
3113 while (current_block.Implicit)
3114 current_block = current_block.Parent;
3116 current_block.SetEndLocation (lexer.Location);
3117 current_block = current_block.Parent;
3118 if (current_block == null)
3119 top_current_block = null;
3130 | statement_list statement
3134 : declaration_statement
3136 if ($1 != null && (Block) $1 != current_block){
3137 current_block.AddStatement ((Statement) $1);
3138 current_block = (Block) $1;
3141 | valid_declaration_statement
3143 current_block.AddStatement ((Statement) $1);
3148 valid_declaration_statement
3151 | expression_statement
3152 | selection_statement
3153 | iteration_statement
3157 | unchecked_statement
3165 : valid_declaration_statement
3166 | declaration_statement
3168 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3173 Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3181 $$ = EmptyStatement.Value;
3188 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3190 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3191 current_block.AddStatement (labeled);
3196 declaration_statement
3197 : local_variable_declaration SEMICOLON
3200 DictionaryEntry de = (DictionaryEntry) $1;
3202 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3206 | local_constant_declaration SEMICOLON
3209 DictionaryEntry de = (DictionaryEntry) $1;
3211 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3217 * The following is from Rhys' grammar:
3218 * > Types in local variable declarations must be recognized as
3219 * > expressions to prevent reduce/reduce errors in the grammar.
3220 * > The expressions are converted into types during semantic analysis.
3223 : primary_expression opt_rank_specifier
3225 // FIXME: Do something smart here regarding the composition of the type.
3227 // Ok, the above "primary_expression" is there to get rid of
3228 // both reduce/reduce and shift/reduces in the grammar, it should
3229 // really just be "type_name". If you use type_name, a reduce/reduce
3230 // creeps up. If you use namespace_or_type_name (which is all we need
3231 // really) two shift/reduces appear.
3234 // So the super-trick is that primary_expression
3235 // can only be either a SimpleName or a MemberAccess.
3236 // The MemberAccess case arises when you have a fully qualified type-name like :
3238 // SimpleName is when you have
3241 Expression expr = (Expression) $1;
3242 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3243 Error_ExpectingTypeName (lexer.Location, expr);
3247 // So we extract the string corresponding to the SimpleName
3251 if ((string) $2 == "")
3254 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3257 | builtin_types opt_rank_specifier
3259 if ((string) $2 == "")
3262 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3266 local_variable_pointer_type
3267 : primary_expression STAR
3269 Expression expr = (Expression) $1;
3270 Location l = lexer.Location;
3272 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3273 Error_ExpectingTypeName (l, expr);
3277 $$ = new ComposedCast ((Expression) $1, "*", l);
3279 | builtin_types STAR
3281 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3285 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3287 | local_variable_pointer_type STAR
3289 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3293 local_variable_declaration
3294 : local_variable_type variable_declarators
3297 $$ = new DictionaryEntry ($1, $2);
3301 | local_variable_pointer_type opt_rank_specifier variable_declarators
3306 if ((string) $2 == "")
3307 t = (Expression) $1;
3309 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3310 $$ = new DictionaryEntry (t, $3);
3316 local_constant_declaration
3317 : CONST local_variable_type constant_declarators
3320 $$ = new DictionaryEntry ($2, $3);
3326 expression_statement
3327 : statement_expression SEMICOLON
3334 // We have to do the wrapping here and not in the case above,
3335 // because statement_expression is used for example in for_statement
3337 statement_expression
3338 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3339 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3340 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3341 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3342 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3343 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3344 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3346 Report.Error (1002, lexer.Location, "Expecting `;'");
3351 object_creation_expression
3352 : object_or_delegate_creation_expression
3353 { note ("complain if this is a delegate maybe?"); }
3362 : if_statement_open if_statement_rest
3371 oob_stack.Push (lexer.Location);
3376 : boolean_expression CLOSE_PARENS
3379 Location l = (Location) oob_stack.Pop ();
3381 $$ = new If ((Expression) $1, (Statement) $3, l);
3383 if (RootContext.WarningLevel >= 3){
3384 if ($3 == EmptyStatement.Value)
3385 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3389 | boolean_expression CLOSE_PARENS
3390 embedded_statement ELSE embedded_statement
3392 Location l = (Location) oob_stack.Pop ();
3394 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
3399 : SWITCH OPEN_PARENS
3401 oob_stack.Push (lexer.Location);
3402 switch_stack.Push (current_block);
3404 expression CLOSE_PARENS
3407 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
3408 current_block = (Block) switch_stack.Pop ();
3424 Report.Error (1522, lexer.Location, "Empty switch block");
3432 ArrayList sections = new ArrayList (4);
3437 | switch_sections switch_section
3439 ArrayList sections = (ArrayList) $1;
3449 current_block = current_block.CreateSwitchBlock (lexer.Location);
3453 Block topmost = current_block;
3455 while (topmost.Implicit)
3456 topmost = topmost.Parent;
3457 $$ = new SwitchSection ((ArrayList) $1, topmost);
3464 ArrayList labels = new ArrayList (4);
3469 | switch_labels switch_label
3471 ArrayList labels = (ArrayList) ($1);
3479 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3480 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3483 1523, lexer.Location,
3484 "The keyword case or default must precede code in switch block");
3498 oob_stack.Push (lexer.Location);
3500 boolean_expression CLOSE_PARENS embedded_statement
3502 Location l = (Location) oob_stack.Pop ();
3503 $$ = new While ((Expression) $4, (Statement) $6, l);
3505 if (RootContext.WarningLevel >= 3){
3506 if ($6 == EmptyStatement.Value)
3507 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3513 : DO embedded_statement
3515 oob_stack.Push (lexer.Location);
3517 boolean_expression CLOSE_PARENS SEMICOLON
3519 Location l = (Location) oob_stack.Pop ();
3521 $$ = new Do ((Statement) $2, (Expression) $6, l);
3527 opt_for_initializer SEMICOLON
3529 Block assign_block = new Block (current_block);
3530 current_block = assign_block;
3532 if ($3 is DictionaryEntry){
3533 DictionaryEntry de = (DictionaryEntry) $3;
3535 Expression type = (Expression) de.Key;
3536 ArrayList var_declarators = (ArrayList) de.Value;
3538 foreach (VariableDeclaration decl in var_declarators){
3542 vi = current_block.AddVariable (
3543 type, decl.identifier, current_local_parameters, decl.Location);
3547 Location l = lexer.Location;
3549 if (decl.expression_or_array_initializer is Expression){
3550 expr = (Expression) decl.expression_or_array_initializer;
3551 } else if (decl.expression_or_array_initializer == null) {
3554 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3555 expr = new ArrayCreation (type, "", init, decl.Location);
3558 LocalVariableReference var;
3559 var = new LocalVariableReference (assign_block, decl.identifier, l);
3562 Assign a = new Assign (var, expr, decl.Location);
3564 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3570 oob_stack.Push (lexer.Location);
3572 opt_for_condition SEMICOLON
3573 opt_for_iterator CLOSE_PARENS
3576 Location l = (Location) oob_stack.Pop ();
3578 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3580 if (RootContext.WarningLevel >= 3){
3581 if ($10 == EmptyStatement.Value)
3582 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3585 current_block.AddStatement (f);
3586 while (current_block.Implicit)
3587 current_block = current_block.Parent;
3589 current_block = current_block.Parent;
3594 : /* empty */ { $$ = EmptyStatement.Value; }
3599 : local_variable_declaration
3600 | statement_expression_list
3604 : /* empty */ { $$ = null; }
3605 | boolean_expression
3609 : /* empty */ { $$ = EmptyStatement.Value; }
3614 : statement_expression_list
3617 statement_expression_list
3618 : statement_expression
3620 // CHANGE: was `null'
3621 Block b = new Block (current_block, Block.Flags.Implicit);
3623 b.AddStatement ((Statement) $1);
3626 | statement_expression_list COMMA statement_expression
3628 Block b = (Block) $1;
3630 b.AddStatement ((Statement) $3);
3636 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
3638 Report.Error (230, lexer.Location, "Type and identifier are both required in a foreach statement");
3641 | FOREACH OPEN_PARENS type IDENTIFIER IN
3643 oob_stack.Push (lexer.Location);
3645 expression CLOSE_PARENS
3647 oob_stack.Push (current_block);
3649 Block foreach_block = new Block (current_block);
3650 LocalVariableReference v = null;
3651 Location l = lexer.Location;
3654 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3658 // Get a writable reference to this read-only variable.
3659 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3661 current_block = foreach_block;
3664 oob_stack.Push (current_block);
3668 Block foreach_block = (Block) oob_stack.Pop ();
3669 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3670 Block prev_block = (Block) oob_stack.Pop ();
3671 Location l = (Location) oob_stack.Pop ();
3673 current_block = prev_block;
3676 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3677 foreach_block.AddStatement (f);
3686 | continue_statement
3696 $$ = new Break (lexer.Location);
3701 : CONTINUE SEMICOLON
3703 $$ = new Continue (lexer.Location);
3708 : GOTO IDENTIFIER SEMICOLON
3710 $$ = new Goto (current_block, (string) $2, lexer.Location);
3712 | GOTO CASE constant_expression SEMICOLON
3714 $$ = new GotoCase ((Expression) $3, lexer.Location);
3716 | GOTO DEFAULT SEMICOLON
3718 $$ = new GotoDefault (lexer.Location);
3723 : RETURN opt_expression SEMICOLON
3725 $$ = new Return ((Expression) $2, lexer.Location);
3730 : THROW opt_expression SEMICOLON
3732 $$ = new Throw ((Expression) $2, lexer.Location);
3737 : IDENTIFIER RETURN expression SEMICOLON
3739 string s = (string) $1;
3741 Report.Error (1003, lexer.Location, "; expected");
3744 if (RootContext.Version == LanguageVersion.ISO_1){
3745 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
3748 if (iterator_container == null){
3749 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3752 iterator_container.SetYields ();
3753 $$ = new Yield ((Expression) $3, lexer.Location);
3756 | IDENTIFIER BREAK SEMICOLON
3758 string s = (string) $1;
3760 Report.Error (1003, lexer.Location, "; expected");
3763 if (RootContext.Version == LanguageVersion.ISO_1){
3764 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
3767 if (iterator_container == null){
3768 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3771 iterator_container.SetYields ();
3772 $$ = new YieldBreak (lexer.Location);
3783 : TRY block catch_clauses
3787 ArrayList c = (ArrayList)$3;
3788 for (int i = 0; i < c.Count; ++i) {
3789 Catch cc = (Catch) c [i];
3791 if (i != c.Count - 1)
3792 Report.Error (1017, cc.loc, "Empty catch block must be the last in a series of catch blocks");
3799 // Now s contains the list of specific catch clauses
3800 // and g contains the general one.
3802 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
3804 | TRY block opt_catch_clauses FINALLY block
3807 ArrayList s = new ArrayList (4);
3808 ArrayList catch_list = (ArrayList) $3;
3810 if (catch_list != null){
3811 foreach (Catch cc in catch_list) {
3819 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
3823 Report.Error (1524, lexer.Location, "Expected catch or finally");
3828 : /* empty */ { $$ = null; }
3835 ArrayList l = new ArrayList (4);
3840 | catch_clauses catch_clause
3842 ArrayList l = (ArrayList) $1;
3850 : /* empty */ { $$ = null; }
3855 : CATCH opt_catch_args
3857 Expression type = null;
3861 DictionaryEntry cc = (DictionaryEntry) $2;
3862 type = (Expression) cc.Key;
3863 id = (string) cc.Value;
3866 ArrayList one = new ArrayList (4);
3867 Location loc = lexer.Location;
3869 one.Add (new VariableDeclaration (id, null, loc));
3872 current_block = new Block (current_block);
3873 Block b = declare_local_variables (type, one, loc);
3878 Expression type = null;
3882 DictionaryEntry cc = (DictionaryEntry) $2;
3883 type = (Expression) cc.Key;
3884 id = (string) cc.Value;
3888 // FIXME: I can change this for an assignment.
3890 while (current_block != (Block) $1)
3891 current_block = current_block.Parent;
3896 $$ = new Catch (type, id , (Block) $4, ((Block) $4).loc);
3901 : /* empty */ { $$ = null; }
3906 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3908 $$ = new DictionaryEntry ($2, $3);
3915 $$ = new Checked ((Block) $2);
3922 $$ = new Unchecked ((Block) $2);
3929 if (!RootContext.Unsafe){
3930 Report.Error (227, lexer.Location,
3931 "Unsafe code can only be used if --unsafe is used");
3934 $$ = new Unsafe ((Block) $3);
3940 type fixed_pointer_declarators
3943 ArrayList list = (ArrayList) $4;
3944 Expression type = (Expression) $3;
3945 Location l = lexer.Location;
3946 int top = list.Count;
3948 Block assign_block = new Block (current_block);
3949 current_block = assign_block;
3951 for (int i = 0; i < top; i++){
3952 Pair p = (Pair) list [i];
3955 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3969 Location l = (Location) oob_stack.Pop ();
3971 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
3973 if (RootContext.WarningLevel >= 3){
3974 if ($7 == EmptyStatement.Value)
3975 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3978 current_block.AddStatement (f);
3979 while (current_block.Implicit)
3980 current_block = current_block.Parent;
3982 current_block = current_block.Parent;
3986 fixed_pointer_declarators
3987 : fixed_pointer_declarator {
3988 ArrayList declarators = new ArrayList (4);
3990 declarators.Add ($1);
3993 | fixed_pointer_declarators COMMA fixed_pointer_declarator
3995 ArrayList declarators = (ArrayList) $1;
3997 declarators.Add ($3);
4002 fixed_pointer_declarator
4003 : IDENTIFIER ASSIGN expression
4005 $$ = new Pair ($1, $3);
4009 Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
4015 : LOCK OPEN_PARENS expression CLOSE_PARENS
4021 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4026 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4028 Block assign_block = new Block (current_block);
4029 current_block = assign_block;
4031 oob_stack.Push (lexer.Location);
4033 if ($3 is DictionaryEntry){
4034 DictionaryEntry de = (DictionaryEntry) $3;
4035 Location l = lexer.Location;
4037 Expression type = (Expression) de.Key;
4038 ArrayList var_declarators = (ArrayList) de.Value;
4040 ArrayList vars = new ArrayList (4);
4042 foreach (VariableDeclaration decl in var_declarators){
4044 LocalInfo vi = current_block.AddVariable (
4045 type, decl.identifier,
4046 current_local_parameters, decl.Location);
4052 if (decl.expression_or_array_initializer is Expression){
4053 expr = (Expression) decl.expression_or_array_initializer;
4055 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4057 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4060 expr = new ArrayCreation (type, "", init, decl.Location);
4063 LocalVariableReference var;
4065 // Get a writable reference to this read-only variable.
4066 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4068 // This is so that it is not a warning on using variables
4071 vars.Add (new DictionaryEntry (var, expr));
4073 // Assign a = new Assign (var, expr, decl.Location);
4074 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4076 $3 = new DictionaryEntry (type, vars);
4081 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
4082 current_block.AddStatement (u);
4083 while (current_block.Implicit)
4084 current_block = current_block.Parent;
4086 current_block = current_block.Parent;
4090 resource_acquisition
4091 : local_variable_declaration
4098 // A class used to pass around variable declarations and constants
4100 public class VariableDeclaration {
4101 public string identifier;
4102 public object expression_or_array_initializer;
4103 public Location Location;
4104 public Attributes OptAttributes;
4106 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4108 this.identifier = id;
4109 this.expression_or_array_initializer = eoai;
4111 this.OptAttributes = opt_attrs;
4114 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4120 /// Used to pass around interface property information
4122 public class InterfaceAccessorInfo {
4124 public readonly Accessor Get, Set;
4126 public InterfaceAccessorInfo (bool has_get, bool has_set,
4127 Attributes get_attrs, Attributes set_attrs, Location get_loc, Location set_loc)
4130 Get = new Accessor (null, get_attrs, get_loc);
4132 Set = new Accessor (null, set_attrs, set_loc);
4138 // A class used to hold info about an indexer declarator
4140 public class IndexerDeclaration {
4141 public Expression type;
4142 public MemberName interface_type;
4143 public Parameters param_list;
4145 public IndexerDeclaration (Expression type, MemberName interface_type,
4146 Parameters param_list)
4149 this.interface_type = interface_type;
4150 this.param_list = param_list;
4155 // We use this when we do not have an object in advance that is an IIteratorContainer
4157 public class SimpleIteratorContainer : IIteratorContainer {
4160 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4165 public static SimpleIteratorContainer GetSimple () {
4166 Simple.Yields = false;
4170 public void SetYields () { Yields = true; }
4174 // A class used to hold info about an operator declarator
4176 public class OperatorDeclaration {
4177 public Operator.OpType optype;
4178 public Expression ret_type, arg1type, arg2type;
4179 public string arg1name, arg2name;
4180 public Location location;
4182 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4183 Expression arg1type, string arg1name,
4184 Expression arg2type, string arg2name, Location location)
4187 this.ret_type = ret_type;
4188 this.arg1type = arg1type;
4189 this.arg1name = arg1name;
4190 this.arg2type = arg2type;
4191 this.arg2name = arg2name;
4192 this.location = location;
4197 void Error_ExpectingTypeName (Location l, Expression expr)
4199 if (expr is Invocation){
4200 Report.Error (1002, l, "; expected");
4202 Report.Error (-1, l, "Invalid Type definition");
4207 // Given the @class_name name, it creates a fully qualified name
4208 // based on the containing declaration space
4211 MakeName (MemberName class_name)
4213 string ns = current_namespace.FullName;
4215 if (current_container.Name == ""){
4217 return new MemberName (new MemberName (ns), class_name);
4221 return new MemberName (current_container.MemberName, class_name);
4225 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4227 Block implicit_block;
4228 ArrayList inits = null;
4231 // We use the `Used' property to check whether statements
4232 // have been added to the current block. If so, we need
4233 // to create another block to contain the new declaration
4234 // otherwise, as an optimization, we use the same block to
4235 // add the declaration.
4237 // FIXME: A further optimization is to check if the statements
4238 // that were added were added as part of the initialization
4239 // below. In which case, no other statements have been executed
4240 // and we might be able to reduce the number of blocks for
4241 // situations like this:
4243 // int j = 1; int k = j + 1;
4245 if (current_block.Used)
4246 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4248 implicit_block = current_block;
4250 foreach (VariableDeclaration decl in variable_declarators){
4252 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4253 if (decl.expression_or_array_initializer != null){
4255 inits = new ArrayList (4);
4262 return implicit_block;
4264 foreach (VariableDeclaration decl in inits){
4268 if (decl.expression_or_array_initializer is Expression){
4269 expr = (Expression) decl.expression_or_array_initializer;
4272 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4274 expr = new ArrayCreation (type, "", init, decl.Location);
4277 LocalVariableReference var;
4278 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4280 assign = new Assign (var, expr, decl.Location);
4282 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4285 return implicit_block;
4288 Block declare_local_constants (Expression type, ArrayList declarators)
4290 Block implicit_block;
4292 if (current_block.Used)
4293 implicit_block = new Block (current_block, Block.Flags.Implicit);
4295 implicit_block = current_block;
4297 foreach (VariableDeclaration decl in declarators){
4298 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4299 current_local_parameters, decl.Location);
4302 return implicit_block;
4305 void CheckAttributeTarget (string a)
4309 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4313 Location l = lexer.Location;
4314 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4320 void CheckUnaryOperator (Operator.OpType op)
4324 case Operator.OpType.LogicalNot:
4325 case Operator.OpType.OnesComplement:
4326 case Operator.OpType.Increment:
4327 case Operator.OpType.Decrement:
4328 case Operator.OpType.True:
4329 case Operator.OpType.False:
4330 case Operator.OpType.Addition:
4331 case Operator.OpType.Subtraction:
4336 Location l = lexer.Location;
4337 Report.Error (1019, l, "Overloadable unary operator expected");
4343 void CheckBinaryOperator (Operator.OpType op)
4347 case Operator.OpType.Addition:
4348 case Operator.OpType.Subtraction:
4349 case Operator.OpType.Multiply:
4350 case Operator.OpType.Division:
4351 case Operator.OpType.Modulus:
4352 case Operator.OpType.BitwiseAnd:
4353 case Operator.OpType.BitwiseOr:
4354 case Operator.OpType.ExclusiveOr:
4355 case Operator.OpType.LeftShift:
4356 case Operator.OpType.RightShift:
4357 case Operator.OpType.Equality:
4358 case Operator.OpType.Inequality:
4359 case Operator.OpType.GreaterThan:
4360 case Operator.OpType.LessThan:
4361 case Operator.OpType.GreaterThanOrEqual:
4362 case Operator.OpType.LessThanOrEqual:
4366 Location l = lexer.Location;
4367 Report.Error (1020, l, "Overloadable binary operator expected");
4373 void syntax_error (Location l, string msg)
4375 Report.Error (1003, l, "Syntax error, " + msg);
4378 void output (string s)
4380 Console.WriteLine (s);
4383 void note (string s)
4385 // Used to put annotations
4390 public Tokenizer Lexer {
4396 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4398 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4399 this.name = file.Name;
4401 current_container = RootContext.Tree.Types;
4402 current_container.NamespaceEntry = current_namespace;
4403 oob_stack = new Stack ();
4404 switch_stack = new Stack ();
4406 lexer = new Tokenizer (reader, file, defines);
4409 public void parse ()
4412 if (yacc_verbose_flag)
4413 yyparse (lexer, new yydebug.yyDebugSimple ());
4416 Tokenizer tokenizer = lexer as Tokenizer;
4417 tokenizer.cleanup ();
4418 } catch (Exception e){
4420 // Removed for production use, use parser verbose to get the output.
4422 // Console.WriteLine (e);
4423 Report.Error (-25, lexer.Location, "Parsing error");
4424 if (Driver.parser_verbose)
4425 Console.WriteLine (e);
4429 void CheckToken (int error, int yyToken, string msg)
4431 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4432 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4435 Report.Error (error, lexer.Location, msg);
4438 void CheckIdentifierToken (int yyToken)
4440 CheckToken (1041, yyToken, "Identifier expected");