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 Report.Error (1001, lexer.Location, "Identifier expected");
1107 opt_parameter_modifier
1110 CheckIdentifierToken (yyToken);
1114 opt_parameter_modifier
1120 Report.Error (241, lexer.Location, "Default parameter specifiers are not permitted");
1125 opt_parameter_modifier
1126 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1127 | parameter_modifier
1131 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1132 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1136 : opt_attributes PARAMS type IDENTIFIER
1138 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1139 note ("type must be a single-dimension array type");
1141 | opt_attributes PARAMS type error {
1142 CheckIdentifierToken (yyToken);
1147 property_declaration
1150 type namespace_or_type_name
1153 implicit_value_parameter_type = (Expression) $3;
1155 lexer.PropertyParsing = true;
1157 $$ = lexer.Location;
1159 iterator_container = SimpleIteratorContainer.GetSimple ();
1161 accessor_declarations
1163 lexer.PropertyParsing = false;
1168 Pair pair = (Pair) $7;
1169 Accessor get_block = (Accessor) pair.First;
1170 Accessor set_block = (Accessor) pair.Second;
1172 Location loc = (Location) $6;
1173 MemberName name = (MemberName) $4;
1175 prop = new Property (current_class, (Expression) $3, (int) $2, false,
1176 name, (Attributes) $1, get_block, set_block, loc);
1177 if (SimpleIteratorContainer.Simple.Yields)
1180 current_container.AddProperty (prop);
1181 implicit_value_parameter_type = null;
1182 iterator_container = null;
1186 accessor_declarations
1187 : get_accessor_declaration opt_set_accessor_declaration
1189 $$ = new Pair ($1, $2);
1191 | set_accessor_declaration opt_get_accessor_declaration
1193 $$ = new Pair ($2, $1);
1197 opt_get_accessor_declaration
1198 : /* empty */ { $$ = null; }
1199 | get_accessor_declaration
1202 opt_set_accessor_declaration
1203 : /* empty */ { $$ = null; }
1204 | set_accessor_declaration
1207 get_accessor_declaration
1208 : opt_attributes GET
1210 // If this is not the case, then current_local_parameters has already
1211 // been set in indexer_declaration
1212 if (parsing_indexer == false)
1213 current_local_parameters = null;
1215 current_local_parameters = indexer_parameters;
1216 lexer.PropertyParsing = false;
1220 $$ = new Accessor ((ToplevelBlock) $4, (Attributes) $1, lexer.Location);
1221 current_local_parameters = null;
1222 lexer.PropertyParsing = true;
1226 set_accessor_declaration
1227 : opt_attributes SET
1230 Parameter implicit_value_parameter = new Parameter (
1231 implicit_value_parameter_type, "value",
1232 Parameter.Modifier.NONE, null);
1234 if (parsing_indexer == false) {
1235 args = new Parameter [1];
1236 args [0] = implicit_value_parameter;
1237 current_local_parameters = new Parameters (args, null, lexer.Location);
1239 Parameter [] fpars = indexer_parameters.FixedParameters;
1242 int count = fpars.Length;
1244 args = new Parameter [count + 1];
1245 fpars.CopyTo (args, 0);
1246 args [count] = implicit_value_parameter;
1249 current_local_parameters = new Parameters (
1250 args, indexer_parameters.ArrayParameter, lexer.Location);
1253 lexer.PropertyParsing = false;
1257 $$ = new Accessor ((ToplevelBlock) $4, (Attributes) $1, lexer.Location);
1258 current_local_parameters = null;
1259 lexer.PropertyParsing = true;
1265 | SEMICOLON { $$ = null; }
1268 interface_declaration
1272 INTERFACE member_name
1274 MemberName name = MakeName ((MemberName) $5);
1275 bool partial = (bool) $3;
1278 ClassPart part = PartialContainer.CreatePart (
1279 current_namespace, current_container, name, (int) $2,
1280 (Attributes) $1, Kind.Interface, lexer.Location);
1282 current_container = part.PartialContainer;
1283 current_class = part;
1285 current_class = new Interface (
1286 current_namespace, current_container, name, (int) $2,
1287 (Attributes) $1, lexer.Location);
1289 current_container = current_class;
1290 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
1295 current_class.Bases = (ArrayList) $7;
1297 current_class.Register ();
1304 current_container = current_container.Parent;
1305 current_class = current_container;
1307 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1308 CheckIdentifierToken (yyToken);
1314 opt_interface_member_declarations
1318 opt_interface_member_declarations
1320 | interface_member_declarations
1323 interface_member_declarations
1324 : interface_member_declaration
1325 | interface_member_declarations interface_member_declaration
1328 interface_member_declaration
1329 : interface_method_declaration
1331 Method m = (Method) $1;
1333 current_container.AddMethod (m);
1335 | interface_property_declaration
1337 Property p = (Property) $1;
1339 current_container.AddProperty (p);
1341 | interface_event_declaration
1344 Event e = (Event) $1;
1345 current_container.AddEvent (e);
1348 | interface_indexer_declaration
1350 Indexer i = (Indexer) $1;
1352 current_container.AddIndexer (i);
1360 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, lexer.Location);
1365 interface_method_declaration
1366 : opt_attributes opt_new type namespace_or_type_name
1367 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1370 MemberName name = (MemberName) $4;
1372 $$ = new Method (current_class, (Expression) $3, (int) $2, true,
1373 name, (Parameters) $6, (Attributes) $1, lexer.Location);
1375 | opt_attributes opt_new VOID namespace_or_type_name
1376 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1379 MemberName name = (MemberName) $4;
1381 $$ = new Method (current_class, TypeManager.system_void_expr, (int) $2,
1382 true, name, (Parameters) $6, (Attributes) $1, lexer.Location);
1386 interface_property_declaration
1391 { lexer.PropertyParsing = true; }
1393 { lexer.PropertyParsing = false; }
1396 InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
1398 $$ = new Property (current_class, (Expression) $3, (int) $2, true,
1399 new MemberName ((string) $4), (Attributes) $1,
1400 pinfo.Get, pinfo.Set, lexer.Location);
1405 CheckIdentifierToken (yyToken);
1411 : opt_attributes GET SEMICOLON { $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, lexer.Location, lexer.Location); }
1412 | opt_attributes SET SEMICOLON { $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1, lexer.Location, lexer.Location); }
1413 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1414 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $3, lexer.Location, lexer.Location); }
1415 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1416 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $3, (Attributes) $1, lexer.Location, lexer.Location); }
1419 interface_event_declaration
1420 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1422 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1423 new MemberName ((string) $5), null,
1424 (Attributes) $1, lexer.Location);
1426 | opt_attributes opt_new EVENT type error {
1427 CheckIdentifierToken (yyToken);
1430 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1431 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1434 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE event_accessor_declarations CLOSE_BRACE {
1435 Report.Error (69, lexer.Location, "Event accessors not valid on interfaces");
1440 interface_indexer_declaration
1441 : opt_attributes opt_new type THIS
1442 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1444 { lexer.PropertyParsing = true; }
1446 { lexer.PropertyParsing = false; }
1449 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1451 $$ = new Indexer (current_class, (Expression) $3,
1452 new MemberName (TypeContainer.DefaultIndexerName),
1453 (int) $2, true, (Parameters) $6, (Attributes) $1,
1454 info.Get, info.Set, lexer.Location);
1458 operator_declaration
1459 : opt_attributes opt_modifiers operator_declarator
1461 iterator_container = SimpleIteratorContainer.GetSimple ();
1465 OperatorDeclaration decl = (OperatorDeclaration) $3;
1467 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1469 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null);
1470 if (decl.arg2type != null)
1471 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null);
1473 Operator op = new Operator (
1474 current_class, decl.optype, decl.ret_type, (int) $2,
1475 new Parameters (param_list, null, decl.location),
1476 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1478 if (SimpleIteratorContainer.Simple.Yields)
1481 // Note again, checking is done in semantic analysis
1482 current_container.AddOperator (op);
1484 current_local_parameters = null;
1485 iterator_container = null;
1491 | SEMICOLON { $$ = null; }
1494 : type OPERATOR overloadable_operator
1495 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1497 Operator.OpType op = (Operator.OpType) $3;
1498 CheckUnaryOperator (op);
1500 if (op == Operator.OpType.Addition)
1501 op = Operator.OpType.UnaryPlus;
1503 if (op == Operator.OpType.Subtraction)
1504 op = Operator.OpType.UnaryNegation;
1506 Parameter [] pars = new Parameter [1];
1508 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1510 current_local_parameters = new Parameters (pars, null, lexer.Location);
1512 $$ = new OperatorDeclaration (op, (Expression) $1, (Expression) $5, (string) $6,
1513 null, null, lexer.Location);
1515 | type OPERATOR overloadable_operator
1517 type IDENTIFIER COMMA
1521 CheckBinaryOperator ((Operator.OpType) $3);
1523 Parameter [] pars = new Parameter [2];
1525 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1526 pars [1] = new Parameter ((Expression) $8, (string) $9, Parameter.Modifier.NONE, null);
1528 current_local_parameters = new Parameters (pars, null, lexer.Location);
1530 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1531 (Expression) $5, (string) $6,
1532 (Expression) $8, (string) $9, lexer.Location);
1534 | conversion_operator_declarator
1537 overloadable_operator
1539 : BANG { $$ = Operator.OpType.LogicalNot; }
1540 | TILDE { $$ = Operator.OpType.OnesComplement; }
1541 | OP_INC { $$ = Operator.OpType.Increment; }
1542 | OP_DEC { $$ = Operator.OpType.Decrement; }
1543 | TRUE { $$ = Operator.OpType.True; }
1544 | FALSE { $$ = Operator.OpType.False; }
1545 // Unary and binary:
1546 | PLUS { $$ = Operator.OpType.Addition; }
1547 | MINUS { $$ = Operator.OpType.Subtraction; }
1549 | STAR { $$ = Operator.OpType.Multiply; }
1550 | DIV { $$ = Operator.OpType.Division; }
1551 | PERCENT { $$ = Operator.OpType.Modulus; }
1552 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1553 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1554 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1555 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1556 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1557 | OP_EQ { $$ = Operator.OpType.Equality; }
1558 | OP_NE { $$ = Operator.OpType.Inequality; }
1559 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1560 | OP_LT { $$ = Operator.OpType.LessThan; }
1561 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1562 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1565 conversion_operator_declarator
1566 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1568 Parameter [] pars = new Parameter [1];
1570 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1572 current_local_parameters = new Parameters (pars, null, lexer.Location);
1574 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1575 null, null, lexer.Location);
1577 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1579 Parameter [] pars = new Parameter [1];
1581 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1583 current_local_parameters = new Parameters (pars, null, lexer.Location);
1585 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1586 null, null, lexer.Location);
1590 syntax_error (lexer.Location, "'operator' expected");
1594 syntax_error (lexer.Location, "'operator' expected");
1598 constructor_declaration
1601 constructor_declarator
1604 Constructor c = (Constructor) $3;
1605 c.Block = (ToplevelBlock) $4;
1606 c.OptAttributes = (Attributes) $1;
1607 c.ModFlags = (int) $2;
1609 if (c.Name == current_container.Basename){
1610 if ((c.ModFlags & Modifiers.STATIC) != 0){
1611 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1613 515, c.Location, String.Format (
1614 "`{0}.{1}': static constructor can not have access modifiers",
1615 c.Name, current_container.Name));
1618 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1620 if (c.Initializer != null){
1623 "Static constructors can not have an explicit this or base " +
1624 "constructor invocations");
1627 if (!c.Parameters.Empty){
1629 132, c.Location, "Static constructors should not have parameters");
1632 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1635 // We let another layer check the validity of the constructor.
1636 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1639 current_container.AddConstructor (c);
1641 current_local_parameters = null;
1645 constructor_declarator
1647 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1649 oob_stack.Push (lexer.Location);
1651 current_local_parameters = (Parameters) $3;
1653 opt_constructor_initializer
1655 Location l = (Location) oob_stack.Pop ();
1656 $$ = new Constructor (current_class, (string) $1, 0, (Parameters) $3,
1657 (ConstructorInitializer) $6, l);
1663 | SEMICOLON { $$ = null; }
1666 opt_constructor_initializer
1667 : /* empty */ { $$ = null; }
1668 | constructor_initializer
1671 constructor_initializer
1672 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1674 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1676 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1678 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1681 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1687 : /* EMPTY */ { $$ = 0; }
1688 | UNSAFE { $$ = Modifiers.UNSAFE; }
1689 | EXTERN { $$ = Modifiers.EXTERN; }
1692 destructor_declaration
1693 : opt_attributes opt_finalizer TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1695 if ((string) $4 != current_container.Basename){
1696 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1697 } else if (!(current_container is Class)){
1698 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1700 Location l = lexer.Location;
1703 if (!RootContext.StdLib && current_container.Name == "System.Object")
1704 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
1706 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
1708 if ((m & Modifiers.UNSAFE) != 0){
1709 if (!RootContext.Unsafe){
1710 Report.Error (227, l,
1711 "Unsafe code requires the -unsafe command " +
1712 "line option to be specified");
1716 Method d = new Destructor (
1717 current_class, TypeManager.system_void_expr, m, "Finalize",
1718 new Parameters (null, null, l), (Attributes) $1, l);
1720 d.Block = (ToplevelBlock) $7;
1721 current_container.AddMethod (d);
1729 EVENT type variable_declarators SEMICOLON
1731 foreach (VariableDeclaration var in (ArrayList) $5) {
1733 MemberName name = new MemberName (var.identifier);
1735 Event e = new EventField (
1736 current_class, (Expression) $4, (int) $2, false, name,
1737 var.expression_or_array_initializer, (Attributes) $1,
1740 current_container.AddEvent (e);
1746 EVENT type namespace_or_type_name
1749 implicit_value_parameter_type = (Expression) $4;
1750 lexer.EventParsing = true;
1751 oob_stack.Push (lexer.Location);
1753 event_accessor_declarations
1755 lexer.EventParsing = false;
1759 Location loc = (Location) oob_stack.Pop ();
1762 Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
1765 Pair pair = (Pair) $8;
1767 MemberName name = (MemberName) $5;
1769 Event e = new EventProperty (
1770 current_class, (Expression) $4, (int) $2, false, name, null,
1771 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
1774 current_container.AddEvent (e);
1775 implicit_value_parameter_type = null;
1778 | opt_attributes opt_modifiers EVENT type namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS block {
1779 MemberName mn = (MemberName) $5;
1781 if (mn.Left != null)
1782 Report.Error (71, lexer.Location, "Explicit implementation of events requires property syntax");
1784 Report.Error (71, lexer.Location, "Event declaration should use property syntax");
1788 event_accessor_declarations
1789 : add_accessor_declaration remove_accessor_declaration
1791 $$ = new Pair ($1, $2);
1793 | remove_accessor_declaration add_accessor_declaration
1795 $$ = new Pair ($2, $1);
1797 | add_accessor_declaration { $$ = null; }
1798 | remove_accessor_declaration { $$ = null; }
1801 add_accessor_declaration
1802 : opt_attributes ADD
1804 Parameter [] args = new Parameter [1];
1805 Parameter implicit_value_parameter = new Parameter (
1806 implicit_value_parameter_type, "value",
1807 Parameter.Modifier.NONE, null);
1809 args [0] = implicit_value_parameter;
1811 current_local_parameters = new Parameters (args, null, lexer.Location);
1812 lexer.EventParsing = false;
1816 $$ = new Accessor ((ToplevelBlock) $4, (Attributes) $1, lexer.Location);
1817 lexer.EventParsing = true;
1819 | opt_attributes ADD error {
1820 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1825 remove_accessor_declaration
1826 : opt_attributes REMOVE
1828 Parameter [] args = new Parameter [1];
1829 Parameter implicit_value_parameter = new Parameter (
1830 implicit_value_parameter_type, "value",
1831 Parameter.Modifier.NONE, null);
1833 args [0] = implicit_value_parameter;
1835 current_local_parameters = new Parameters (args, null, lexer.Location);
1836 lexer.EventParsing = false;
1840 $$ = new Accessor ((ToplevelBlock) $4, (Attributes) $1, lexer.Location);
1841 lexer.EventParsing = true;
1843 | opt_attributes REMOVE error {
1844 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1850 : opt_attributes opt_modifiers indexer_declarator
1853 IndexerDeclaration decl = (IndexerDeclaration) $3;
1855 implicit_value_parameter_type = decl.type;
1857 lexer.PropertyParsing = true;
1858 parsing_indexer = true;
1860 indexer_parameters = decl.param_list;
1861 oob_stack.Push (lexer.Location);
1863 accessor_declarations
1865 lexer.PropertyParsing = false;
1866 parsing_indexer = false;
1870 // The signature is computed from the signature of the indexer. Look
1871 // at section 3.6 on the spec
1872 Location loc = (Location) oob_stack.Pop ();
1874 IndexerDeclaration decl = (IndexerDeclaration) $3;
1875 Pair pair = (Pair) $6;
1876 Accessor get_block = (Accessor) pair.First;
1877 Accessor set_block = (Accessor) pair.Second;
1880 if (decl.interface_type != null)
1881 name = new MemberName (decl.interface_type,
1882 TypeContainer.DefaultIndexerName);
1884 name = new MemberName (TypeContainer.DefaultIndexerName);
1886 indexer = new Indexer (current_class, decl.type, name,
1887 (int) $2, false, decl.param_list, (Attributes) $1,
1888 get_block, set_block, loc);
1890 current_container.AddIndexer (indexer);
1892 current_local_parameters = null;
1893 implicit_value_parameter_type = null;
1894 indexer_parameters = null;
1899 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1901 Parameters pars = (Parameters) $4;
1902 if (pars.HasArglist) {
1903 // "__arglist is not valid in this context"
1904 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
1905 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
1906 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1909 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
1911 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1913 Parameters pars = (Parameters) $6;
1915 if (pars.HasArglist) {
1916 // "__arglist is not valid in this context"
1917 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
1918 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
1919 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1921 MemberName name = (MemberName) $2;
1922 $$ = new IndexerDeclaration ((Expression) $1, name, pars);
1934 Location enum_location = lexer.Location;
1936 MemberName full_name = MakeName (new MemberName ((string) $4));
1937 Enum e = new Enum (current_namespace, current_container, (Expression) $5, (int) $2,
1938 full_name, (Attributes) $1, enum_location);
1940 foreach (VariableDeclaration ev in (ArrayList) $6) {
1941 e.AddEnumMember (ev.identifier,
1942 (Expression) ev.expression_or_array_initializer,
1943 ev.Location, ev.OptAttributes);
1946 string name = full_name.GetName ();
1947 current_container.AddEnum (e);
1948 RootContext.Tree.RecordDecl (name, e);
1954 : /* empty */ { $$ = TypeManager.system_int32_expr; }
1955 | COLON type { $$ = $2; }
1959 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1965 opt_enum_member_declarations
1966 : /* empty */ { $$ = new ArrayList (4); }
1967 | enum_member_declarations opt_comma { $$ = $1; }
1970 enum_member_declarations
1971 : enum_member_declaration
1973 ArrayList l = new ArrayList (4);
1978 | enum_member_declarations COMMA enum_member_declaration
1980 ArrayList l = (ArrayList) $1;
1988 enum_member_declaration
1989 : opt_attributes IDENTIFIER
1991 $$ = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
1993 | opt_attributes IDENTIFIER
1995 $$ = lexer.Location;
1999 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
2003 delegate_declaration
2006 DELEGATE type member_name
2007 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2010 Location l = lexer.Location;
2011 MemberName name = MakeName ((MemberName) $5);
2012 Delegate del = new Delegate (current_namespace, current_container, (Expression) $4,
2013 (int) $2, name, (Parameters) $7, (Attributes) $1, l);
2015 current_container.AddDelegate (del);
2016 RootContext.Tree.RecordDecl (name.GetName (true), del);
2020 DELEGATE VOID member_name
2021 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2024 Location l = lexer.Location;
2025 MemberName name = MakeName ((MemberName) $5);
2026 Delegate del = new Delegate (
2027 current_namespace, current_container,
2028 TypeManager.system_void_expr, (int) $2, name,
2029 (Parameters) $7, (Attributes) $1, l);
2031 current_container.AddDelegate (del);
2032 RootContext.Tree.RecordDecl (name.GetName (true), del);
2036 namespace_or_type_name
2038 | namespace_or_type_name DOT IDENTIFIER {
2039 $$ = new MemberName ((MemberName) $1, (string) $3);
2045 $$ = new MemberName ((string) $1);
2050 * Before you think of adding a return_type, notice that we have been
2051 * using two rules in the places where it matters (one rule using type
2052 * and another identical one that uses VOID as the return type). This
2053 * gets rid of a shift/reduce couple
2056 : namespace_or_type_name
2058 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2070 // Note that here only unmanaged types are allowed but we
2071 // can't perform checks during this phase - we do it during
2072 // semantic analysis.
2074 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2078 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2084 | non_expression_type rank_specifier
2086 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2088 | non_expression_type STAR
2090 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2092 | expression rank_specifiers
2094 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2098 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2102 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2103 // through this different path
2105 | multiplicative_expression STAR
2107 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2114 ArrayList types = new ArrayList (4);
2119 | type_list COMMA type
2121 ArrayList types = (ArrayList) $1;
2129 * replaces all the productions for isolating the various
2130 * simple types, but we need this to reuse it easily in local_variable_type
2133 : OBJECT { $$ = TypeManager.system_object_expr; }
2134 | STRING { $$ = TypeManager.system_string_expr; }
2135 | BOOL { $$ = TypeManager.system_boolean_expr; }
2136 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2137 | FLOAT { $$ = TypeManager.system_single_expr; }
2138 | DOUBLE { $$ = TypeManager.system_double_expr; }
2143 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2144 | BYTE { $$ = TypeManager.system_byte_expr; }
2145 | SHORT { $$ = TypeManager.system_int16_expr; }
2146 | USHORT { $$ = TypeManager.system_uint16_expr; }
2147 | INT { $$ = TypeManager.system_int32_expr; }
2148 | UINT { $$ = TypeManager.system_uint32_expr; }
2149 | LONG { $$ = TypeManager.system_int64_expr; }
2150 | ULONG { $$ = TypeManager.system_uint64_expr; }
2151 | CHAR { $$ = TypeManager.system_char_expr; }
2152 | VOID { $$ = TypeManager.system_void_expr; }
2156 : type rank_specifiers
2158 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2163 // Expressions, section 7.5
2173 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2175 | parenthesized_expression
2177 | invocation_expression
2181 | post_increment_expression
2182 | post_decrement_expression
2186 | checked_expression
2187 | unchecked_expression
2188 | pointer_member_access
2189 | anonymous_method_expression
2196 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2197 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2198 | NULL { $$ = NullLiteral.Null; }
2202 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2203 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2204 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2209 object v = lexer.Value;
2215 $$ = IntLiteral.Zero;
2217 $$ = IntLiteral.One;
2219 $$ = new IntLiteral (i);
2220 } else if (v is uint)
2221 $$ = new UIntLiteral ((UInt32) v);
2223 $$ = new LongLiteral ((Int64) v);
2224 else if (v is ulong)
2225 $$ = new ULongLiteral ((UInt64) v);
2227 Console.WriteLine ("OOPS. Unexpected result from scanner");
2232 : TRUE { $$ = new BoolLiteral (true); }
2233 | FALSE { $$ = new BoolLiteral (false); }
2236 parenthesized_expression_0
2237 : OPEN_PARENS expression CLOSE_PARENS
2240 lexer.Deambiguate_CloseParens ();
2241 // After this, the next token returned is one of
2242 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2243 // or CLOSE_PARENS_MINUS.
2247 parenthesized_expression
2248 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2252 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2254 // If a parenthesized expression is followed by a minus, we need to wrap
2255 // the expression inside a ParenthesizedExpression for the CS0075 check
2256 // in Binary.DoResolve().
2257 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2262 : primary_expression DOT IDENTIFIER
2264 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2266 | predefined_type DOT IDENTIFIER
2268 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2276 invocation_expression
2277 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2280 Location l = lexer.Location;
2281 Report.Error (1, l, "Parse error");
2283 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2285 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2287 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2289 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2291 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2296 : /* empty */ { $$ = null; }
2303 ArrayList list = new ArrayList (4);
2307 | argument_list COMMA argument
2309 ArrayList list = (ArrayList) $1;
2313 | argument_list error {
2314 CheckToken (1026, yyToken, ", or ) expected");
2321 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2323 | REF variable_reference
2325 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2327 | OUT variable_reference
2329 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2331 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2333 ArrayList list = (ArrayList) $3;
2334 Argument[] args = new Argument [list.Count];
2335 list.CopyTo (args, 0);
2337 Expression expr = new Arglist (args, lexer.Location);
2338 $$ = new Argument (expr, Argument.AType.Expression);
2342 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2347 : expression { note ("section 5.4"); $$ = $1; }
2351 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2353 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2355 | primary_expression rank_specifiers
2357 // So the super-trick is that primary_expression
2358 // can only be either a SimpleName or a MemberAccess.
2359 // The MemberAccess case arises when you have a fully qualified type-name like :
2361 // SimpleName is when you have
2364 Expression expr = (Expression) $1;
2365 if (expr is ComposedCast){
2366 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2367 } else if (!(expr is SimpleName || expr is MemberAccess)){
2368 Error_ExpectingTypeName (lexer.Location, expr);
2369 $$ = TypeManager.system_object_expr;
2372 // So we extract the string corresponding to the SimpleName
2375 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2383 ArrayList list = new ArrayList (4);
2387 | expression_list COMMA expression
2389 ArrayList list = (ArrayList) $1;
2398 $$ = new This (current_block, lexer.Location);
2403 : BASE DOT IDENTIFIER
2405 $$ = new BaseAccess ((string) $3, lexer.Location);
2407 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2409 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2412 Report.Error (175, lexer.Location, "Use of keyword `base' is not valid in this context");
2417 post_increment_expression
2418 : primary_expression OP_INC
2420 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2421 (Expression) $1, lexer.Location);
2425 post_decrement_expression
2426 : primary_expression OP_DEC
2428 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2429 (Expression) $1, lexer.Location);
2434 : object_or_delegate_creation_expression
2435 | array_creation_expression
2438 object_or_delegate_creation_expression
2439 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2441 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2445 array_creation_expression
2446 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2448 opt_array_initializer
2450 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2452 | NEW type rank_specifiers array_initializer
2454 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2458 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2474 : rank_specifier opt_rank_specifier
2476 $$ = (string) $2 + (string) $1;
2481 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2483 $$ = "[" + (string) $2 + "]";
2503 | dim_separators COMMA
2505 $$ = (string) $1 + ",";
2509 opt_array_initializer
2521 : OPEN_BRACE CLOSE_BRACE
2523 ArrayList list = new ArrayList (4);
2526 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2528 $$ = (ArrayList) $2;
2532 variable_initializer_list
2533 : variable_initializer
2535 ArrayList list = new ArrayList (4);
2539 | variable_initializer_list COMMA variable_initializer
2541 ArrayList list = (ArrayList) $1;
2548 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2550 $$ = new TypeOfVoid (lexer.Location);
2552 | TYPEOF OPEN_PARENS type CLOSE_PARENS
2554 $$ = new TypeOf ((Expression) $3, lexer.Location);
2559 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2560 $$ = new SizeOf ((Expression) $3, lexer.Location);
2565 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2567 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2571 unchecked_expression
2572 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2574 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2578 pointer_member_access
2579 : primary_expression OP_PTR IDENTIFIER
2583 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2584 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2588 anonymous_method_expression
2589 : DELEGATE opt_anonymous_method_signature {
2590 oob_stack.Push (current_local_parameters);
2591 current_local_parameters = (Parameters)$2;
2593 // Force the next block to be created as a ToplevelBlock
2594 oob_stack.Push (current_block);
2595 oob_stack.Push (top_current_block);
2596 oob_stack.Push (lexer.Location);
2597 current_block = null;
2599 Location loc = (Location) oob_stack.Pop ();
2600 top_current_block = (Block) oob_stack.Pop ();
2601 current_block = (Block) oob_stack.Pop ();
2602 if (RootContext.Version == LanguageVersion.ISO_1){
2603 Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
2606 ToplevelBlock anon_block = (ToplevelBlock) $4;
2608 anon_block.Parent = current_block;
2609 $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block,
2612 current_local_parameters = (Parameters) oob_stack.Pop ();
2616 opt_anonymous_method_signature
2617 : /* empty */ { $$ = null; }
2618 | anonymous_method_signature
2621 anonymous_method_signature
2622 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
2625 $$ = Parameters.EmptyReadOnlyParameters;
2627 ArrayList par_list = (ArrayList) $2;
2628 Parameter [] pars = new Parameter [par_list.Count];
2629 par_list.CopyTo (pars);
2630 $$ = new Parameters (pars, null, lexer.Location);
2635 opt_anonymous_method_parameter_list
2636 : /* empty */ { $$ = null; }
2637 | anonymous_method_parameter_list { $$ = $1; }
2640 anonymous_method_parameter_list
2641 : anonymous_method_parameter
2643 ArrayList a = new ArrayList (4);
2647 | anonymous_method_parameter_list COMMA anonymous_method_parameter
2649 ArrayList a = (ArrayList) $1;
2655 anonymous_method_parameter
2656 : opt_parameter_modifier type IDENTIFIER {
2657 $$ = new Parameter ((Expression) $2, (string) $3, (Parameter.Modifier) $1, null);
2659 | PARAMS type IDENTIFIER {
2660 Report.Error (-221, lexer.Location, "params modifier not allowed in anonymous method declaration");
2666 : primary_expression
2667 | BANG prefixed_unary_expression
2669 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2671 | TILDE prefixed_unary_expression
2673 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2679 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
2681 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2683 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
2685 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2691 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2693 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2698 // The idea to split this out is from Rhys' grammar
2699 // to solve the problem with casts.
2701 prefixed_unary_expression
2703 | PLUS prefixed_unary_expression
2705 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2707 | MINUS prefixed_unary_expression
2709 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2711 | OP_INC prefixed_unary_expression
2713 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2714 (Expression) $2, lexer.Location);
2716 | OP_DEC prefixed_unary_expression
2718 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2719 (Expression) $2, lexer.Location);
2721 | STAR prefixed_unary_expression
2723 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2725 | BITWISE_AND prefixed_unary_expression
2727 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2731 pre_increment_expression
2732 : OP_INC prefixed_unary_expression
2734 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2735 (Expression) $2, lexer.Location);
2739 pre_decrement_expression
2740 : OP_DEC prefixed_unary_expression
2742 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2743 (Expression) $2, lexer.Location);
2747 multiplicative_expression
2748 : prefixed_unary_expression
2749 | multiplicative_expression STAR prefixed_unary_expression
2751 $$ = new Binary (Binary.Operator.Multiply,
2752 (Expression) $1, (Expression) $3, lexer.Location);
2754 | multiplicative_expression DIV prefixed_unary_expression
2756 $$ = new Binary (Binary.Operator.Division,
2757 (Expression) $1, (Expression) $3, lexer.Location);
2759 | multiplicative_expression PERCENT prefixed_unary_expression
2761 $$ = new Binary (Binary.Operator.Modulus,
2762 (Expression) $1, (Expression) $3, lexer.Location);
2767 : multiplicative_expression
2768 | additive_expression PLUS multiplicative_expression
2770 $$ = new Binary (Binary.Operator.Addition,
2771 (Expression) $1, (Expression) $3, lexer.Location);
2773 | additive_expression MINUS multiplicative_expression
2775 $$ = new Binary (Binary.Operator.Subtraction,
2776 (Expression) $1, (Expression) $3, lexer.Location);
2781 : additive_expression
2782 | shift_expression OP_SHIFT_LEFT additive_expression
2784 $$ = new Binary (Binary.Operator.LeftShift,
2785 (Expression) $1, (Expression) $3, lexer.Location);
2787 | shift_expression OP_SHIFT_RIGHT additive_expression
2789 $$ = new Binary (Binary.Operator.RightShift,
2790 (Expression) $1, (Expression) $3, lexer.Location);
2794 relational_expression
2796 | relational_expression OP_LT shift_expression
2798 $$ = new Binary (Binary.Operator.LessThan,
2799 (Expression) $1, (Expression) $3, lexer.Location);
2801 | relational_expression OP_GT shift_expression
2803 $$ = new Binary (Binary.Operator.GreaterThan,
2804 (Expression) $1, (Expression) $3, lexer.Location);
2806 | relational_expression OP_LE shift_expression
2808 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2809 (Expression) $1, (Expression) $3, lexer.Location);
2811 | relational_expression OP_GE shift_expression
2813 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2814 (Expression) $1, (Expression) $3, lexer.Location);
2816 | relational_expression IS type
2818 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
2820 | relational_expression AS type
2822 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
2827 : relational_expression
2828 | equality_expression OP_EQ relational_expression
2830 $$ = new Binary (Binary.Operator.Equality,
2831 (Expression) $1, (Expression) $3, lexer.Location);
2833 | equality_expression OP_NE relational_expression
2835 $$ = new Binary (Binary.Operator.Inequality,
2836 (Expression) $1, (Expression) $3, lexer.Location);
2841 : equality_expression
2842 | and_expression BITWISE_AND equality_expression
2844 $$ = new Binary (Binary.Operator.BitwiseAnd,
2845 (Expression) $1, (Expression) $3, lexer.Location);
2849 exclusive_or_expression
2851 | exclusive_or_expression CARRET and_expression
2853 $$ = new Binary (Binary.Operator.ExclusiveOr,
2854 (Expression) $1, (Expression) $3, lexer.Location);
2858 inclusive_or_expression
2859 : exclusive_or_expression
2860 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2862 $$ = new Binary (Binary.Operator.BitwiseOr,
2863 (Expression) $1, (Expression) $3, lexer.Location);
2867 conditional_and_expression
2868 : inclusive_or_expression
2869 | conditional_and_expression OP_AND inclusive_or_expression
2871 $$ = new Binary (Binary.Operator.LogicalAnd,
2872 (Expression) $1, (Expression) $3, lexer.Location);
2876 conditional_or_expression
2877 : conditional_and_expression
2878 | conditional_or_expression OP_OR conditional_and_expression
2880 $$ = new Binary (Binary.Operator.LogicalOr,
2881 (Expression) $1, (Expression) $3, lexer.Location);
2885 conditional_expression
2886 : conditional_or_expression
2887 | conditional_or_expression INTERR expression COLON expression
2889 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2893 assignment_expression
2894 : prefixed_unary_expression ASSIGN expression
2896 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2898 | prefixed_unary_expression OP_MULT_ASSIGN expression
2900 Location l = lexer.Location;
2902 $$ = new CompoundAssign (
2903 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
2905 | prefixed_unary_expression OP_DIV_ASSIGN expression
2907 Location l = lexer.Location;
2909 $$ = new CompoundAssign (
2910 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
2912 | prefixed_unary_expression OP_MOD_ASSIGN expression
2914 Location l = lexer.Location;
2916 $$ = new CompoundAssign (
2917 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
2919 | prefixed_unary_expression OP_ADD_ASSIGN expression
2921 Location l = lexer.Location;
2923 $$ = new CompoundAssign (
2924 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
2926 | prefixed_unary_expression OP_SUB_ASSIGN expression
2928 Location l = lexer.Location;
2930 $$ = new CompoundAssign (
2931 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
2933 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2935 Location l = lexer.Location;
2937 $$ = new CompoundAssign (
2938 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
2940 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2942 Location l = lexer.Location;
2944 $$ = new CompoundAssign (
2945 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
2947 | prefixed_unary_expression OP_AND_ASSIGN expression
2949 Location l = lexer.Location;
2951 $$ = new CompoundAssign (
2952 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
2954 | prefixed_unary_expression OP_OR_ASSIGN expression
2956 Location l = lexer.Location;
2958 $$ = new CompoundAssign (
2959 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
2961 | prefixed_unary_expression OP_XOR_ASSIGN expression
2963 Location l = lexer.Location;
2965 $$ = new CompoundAssign (
2966 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
2971 : conditional_expression
2972 | assignment_expression
2992 MemberName name = MakeName ((MemberName) $5);
2993 bool partial = (bool) $3;
2994 int mod_flags = (int) $2;
2997 ClassPart part = PartialContainer.CreatePart (
2998 current_namespace, current_container, name, mod_flags,
2999 (Attributes) $1, Kind.Class, lexer.Location);
3001 current_container = part.PartialContainer;
3002 current_class = part;
3004 if ((mod_flags & Modifiers.STATIC) != 0) {
3005 current_class = new StaticClass (
3006 current_namespace, current_container, name,
3007 mod_flags, (Attributes) $1, lexer.Location);
3009 current_class = new Class (
3010 current_namespace, current_container, name,
3011 mod_flags, (Attributes) $1, lexer.Location);
3014 current_container = current_class;
3015 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
3021 if (current_class.Name == "System.Object") {
3022 Report.Error (537, current_class.Location,
3023 "The class System.Object cannot have a base " +
3024 "class or implement an interface.");
3026 current_class.Bases = (ArrayList) $7;
3029 current_class.Register ();
3036 current_container = current_container.Parent;
3037 current_class = current_container;
3043 { $$ = (bool) false; }
3045 { $$ = (bool) true; }
3049 : /* empty */ { $$ = (int) 0; }
3055 | modifiers modifier
3060 if ((m1 & m2) != 0) {
3061 Location l = lexer.Location;
3062 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3064 $$ = (int) (m1 | m2);
3069 : NEW { $$ = Modifiers.NEW; }
3070 | PUBLIC { $$ = Modifiers.PUBLIC; }
3071 | PROTECTED { $$ = Modifiers.PROTECTED; }
3072 | INTERNAL { $$ = Modifiers.INTERNAL; }
3073 | PRIVATE { $$ = Modifiers.PRIVATE; }
3074 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3075 | SEALED { $$ = Modifiers.SEALED; }
3076 | STATIC { $$ = Modifiers.STATIC; }
3077 | READONLY { $$ = Modifiers.READONLY; }
3078 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3079 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3080 | EXTERN { $$ = Modifiers.EXTERN; }
3081 | VOLATILE { $$ = Modifiers.VOLATILE; }
3082 | UNSAFE { $$ = Modifiers.UNSAFE; }
3086 : /* empty */ { $$ = null; }
3087 | class_base { $$ = $1; }
3091 : COLON type_list { $$ = $2; }
3099 // A block is "contained" on the following places:
3101 // property_declaration as part of the accessor body (get/set)
3102 // operator_declaration
3103 // constructor_declaration
3104 // destructor_declaration
3105 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3110 if (current_block == null){
3111 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, lexer.Location);
3112 top_current_block = current_block;
3114 current_block = new Block (current_block, current_local_parameters,
3115 lexer.Location, Location.Null);
3118 opt_statement_list CLOSE_BRACE
3120 while (current_block.Implicit)
3121 current_block = current_block.Parent;
3123 current_block.SetEndLocation (lexer.Location);
3124 current_block = current_block.Parent;
3125 if (current_block == null)
3126 top_current_block = null;
3137 | statement_list statement
3141 : declaration_statement
3143 if ($1 != null && (Block) $1 != current_block){
3144 current_block.AddStatement ((Statement) $1);
3145 current_block = (Block) $1;
3148 | valid_declaration_statement
3150 current_block.AddStatement ((Statement) $1);
3155 valid_declaration_statement
3158 | expression_statement
3159 | selection_statement
3160 | iteration_statement
3164 | unchecked_statement
3172 : valid_declaration_statement
3173 | declaration_statement
3175 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3180 Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3188 $$ = EmptyStatement.Value;
3195 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3197 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3198 current_block.AddStatement (labeled);
3203 declaration_statement
3204 : local_variable_declaration SEMICOLON
3207 DictionaryEntry de = (DictionaryEntry) $1;
3209 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3213 | local_constant_declaration SEMICOLON
3216 DictionaryEntry de = (DictionaryEntry) $1;
3218 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3224 * The following is from Rhys' grammar:
3225 * > Types in local variable declarations must be recognized as
3226 * > expressions to prevent reduce/reduce errors in the grammar.
3227 * > The expressions are converted into types during semantic analysis.
3230 : primary_expression opt_rank_specifier
3232 // FIXME: Do something smart here regarding the composition of the type.
3234 // Ok, the above "primary_expression" is there to get rid of
3235 // both reduce/reduce and shift/reduces in the grammar, it should
3236 // really just be "type_name". If you use type_name, a reduce/reduce
3237 // creeps up. If you use namespace_or_type_name (which is all we need
3238 // really) two shift/reduces appear.
3241 // So the super-trick is that primary_expression
3242 // can only be either a SimpleName or a MemberAccess.
3243 // The MemberAccess case arises when you have a fully qualified type-name like :
3245 // SimpleName is when you have
3248 Expression expr = (Expression) $1;
3249 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3250 Error_ExpectingTypeName (lexer.Location, expr);
3254 // So we extract the string corresponding to the SimpleName
3258 if ((string) $2 == "")
3261 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3264 | builtin_types opt_rank_specifier
3266 if ((string) $2 == "")
3269 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3273 local_variable_pointer_type
3274 : primary_expression STAR
3276 Expression expr = (Expression) $1;
3277 Location l = lexer.Location;
3279 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3280 Error_ExpectingTypeName (l, expr);
3284 $$ = new ComposedCast ((Expression) $1, "*", l);
3286 | builtin_types STAR
3288 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3292 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3294 | local_variable_pointer_type STAR
3296 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3300 local_variable_declaration
3301 : local_variable_type variable_declarators
3304 $$ = new DictionaryEntry ($1, $2);
3308 | local_variable_pointer_type opt_rank_specifier variable_declarators
3313 if ((string) $2 == "")
3314 t = (Expression) $1;
3316 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3317 $$ = new DictionaryEntry (t, $3);
3323 local_constant_declaration
3324 : CONST local_variable_type constant_declarators
3327 $$ = new DictionaryEntry ($2, $3);
3333 expression_statement
3334 : statement_expression SEMICOLON
3341 // We have to do the wrapping here and not in the case above,
3342 // because statement_expression is used for example in for_statement
3344 statement_expression
3345 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3346 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3347 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3348 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3349 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3350 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3351 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3353 Report.Error (1002, lexer.Location, "Expecting `;'");
3358 object_creation_expression
3359 : object_or_delegate_creation_expression
3360 { note ("complain if this is a delegate maybe?"); }
3369 : if_statement_open if_statement_rest
3378 oob_stack.Push (lexer.Location);
3383 : boolean_expression CLOSE_PARENS
3386 Location l = (Location) oob_stack.Pop ();
3388 $$ = new If ((Expression) $1, (Statement) $3, l);
3390 if (RootContext.WarningLevel >= 3){
3391 if ($3 == EmptyStatement.Value)
3392 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3396 | boolean_expression CLOSE_PARENS
3397 embedded_statement ELSE embedded_statement
3399 Location l = (Location) oob_stack.Pop ();
3401 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
3406 : SWITCH OPEN_PARENS
3408 oob_stack.Push (lexer.Location);
3409 switch_stack.Push (current_block);
3411 expression CLOSE_PARENS
3414 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
3415 current_block = (Block) switch_stack.Pop ();
3431 Report.Error (1522, lexer.Location, "Empty switch block");
3439 ArrayList sections = new ArrayList (4);
3444 | switch_sections switch_section
3446 ArrayList sections = (ArrayList) $1;
3456 current_block = current_block.CreateSwitchBlock (lexer.Location);
3460 Block topmost = current_block;
3462 while (topmost.Implicit)
3463 topmost = topmost.Parent;
3464 $$ = new SwitchSection ((ArrayList) $1, topmost);
3471 ArrayList labels = new ArrayList (4);
3476 | switch_labels switch_label
3478 ArrayList labels = (ArrayList) ($1);
3486 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3487 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3490 1523, lexer.Location,
3491 "The keyword case or default must precede code in switch block");
3505 oob_stack.Push (lexer.Location);
3507 boolean_expression CLOSE_PARENS embedded_statement
3509 Location l = (Location) oob_stack.Pop ();
3510 $$ = new While ((Expression) $4, (Statement) $6, l);
3512 if (RootContext.WarningLevel >= 3){
3513 if ($6 == EmptyStatement.Value)
3514 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3520 : DO embedded_statement
3522 oob_stack.Push (lexer.Location);
3524 boolean_expression CLOSE_PARENS SEMICOLON
3526 Location l = (Location) oob_stack.Pop ();
3528 $$ = new Do ((Statement) $2, (Expression) $6, l);
3534 opt_for_initializer SEMICOLON
3536 Block assign_block = new Block (current_block);
3537 current_block = assign_block;
3539 if ($3 is DictionaryEntry){
3540 DictionaryEntry de = (DictionaryEntry) $3;
3542 Expression type = (Expression) de.Key;
3543 ArrayList var_declarators = (ArrayList) de.Value;
3545 foreach (VariableDeclaration decl in var_declarators){
3549 vi = current_block.AddVariable (
3550 type, decl.identifier, current_local_parameters, decl.Location);
3554 Location l = lexer.Location;
3556 if (decl.expression_or_array_initializer is Expression){
3557 expr = (Expression) decl.expression_or_array_initializer;
3558 } else if (decl.expression_or_array_initializer == null) {
3561 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3562 expr = new ArrayCreation (type, "", init, decl.Location);
3565 LocalVariableReference var;
3566 var = new LocalVariableReference (assign_block, decl.identifier, l);
3569 Assign a = new Assign (var, expr, decl.Location);
3571 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3577 oob_stack.Push (lexer.Location);
3579 opt_for_condition SEMICOLON
3580 opt_for_iterator CLOSE_PARENS
3583 Location l = (Location) oob_stack.Pop ();
3585 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3587 if (RootContext.WarningLevel >= 3){
3588 if ($10 == EmptyStatement.Value)
3589 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3592 current_block.AddStatement (f);
3593 while (current_block.Implicit)
3594 current_block = current_block.Parent;
3596 current_block = current_block.Parent;
3601 : /* empty */ { $$ = EmptyStatement.Value; }
3606 : local_variable_declaration
3607 | statement_expression_list
3611 : /* empty */ { $$ = null; }
3612 | boolean_expression
3616 : /* empty */ { $$ = EmptyStatement.Value; }
3621 : statement_expression_list
3624 statement_expression_list
3625 : statement_expression
3627 // CHANGE: was `null'
3628 Block b = new Block (current_block, Block.Flags.Implicit);
3630 b.AddStatement ((Statement) $1);
3633 | statement_expression_list COMMA statement_expression
3635 Block b = (Block) $1;
3637 b.AddStatement ((Statement) $3);
3643 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
3645 Report.Error (230, lexer.Location, "Type and identifier are both required in a foreach statement");
3648 | FOREACH OPEN_PARENS type IDENTIFIER IN
3650 oob_stack.Push (lexer.Location);
3652 expression CLOSE_PARENS
3654 oob_stack.Push (current_block);
3656 Block foreach_block = new Block (current_block);
3657 LocalVariableReference v = null;
3658 Location l = lexer.Location;
3661 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3665 // Get a writable reference to this read-only variable.
3666 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3668 current_block = foreach_block;
3671 oob_stack.Push (current_block);
3675 Block foreach_block = (Block) oob_stack.Pop ();
3676 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3677 Block prev_block = (Block) oob_stack.Pop ();
3678 Location l = (Location) oob_stack.Pop ();
3680 current_block = prev_block;
3683 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3684 foreach_block.AddStatement (f);
3693 | continue_statement
3703 $$ = new Break (lexer.Location);
3708 : CONTINUE SEMICOLON
3710 $$ = new Continue (lexer.Location);
3715 : GOTO IDENTIFIER SEMICOLON
3717 $$ = new Goto (current_block, (string) $2, lexer.Location);
3719 | GOTO CASE constant_expression SEMICOLON
3721 $$ = new GotoCase ((Expression) $3, lexer.Location);
3723 | GOTO DEFAULT SEMICOLON
3725 $$ = new GotoDefault (lexer.Location);
3730 : RETURN opt_expression SEMICOLON
3732 $$ = new Return ((Expression) $2, lexer.Location);
3737 : THROW opt_expression SEMICOLON
3739 $$ = new Throw ((Expression) $2, lexer.Location);
3744 : IDENTIFIER RETURN expression SEMICOLON
3746 string s = (string) $1;
3748 Report.Error (1003, lexer.Location, "; expected");
3751 if (RootContext.Version == LanguageVersion.ISO_1){
3752 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
3755 if (iterator_container == null){
3756 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3759 iterator_container.SetYields ();
3760 $$ = new Yield ((Expression) $3, lexer.Location);
3763 | IDENTIFIER BREAK SEMICOLON
3765 string s = (string) $1;
3767 Report.Error (1003, lexer.Location, "; expected");
3770 if (RootContext.Version == LanguageVersion.ISO_1){
3771 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
3774 if (iterator_container == null){
3775 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3778 iterator_container.SetYields ();
3779 $$ = new YieldBreak (lexer.Location);
3790 : TRY block catch_clauses
3794 ArrayList c = (ArrayList)$3;
3795 for (int i = 0; i < c.Count; ++i) {
3796 Catch cc = (Catch) c [i];
3798 if (i != c.Count - 1)
3799 Report.Error (1017, cc.loc, "Empty catch block must be the last in a series of catch blocks");
3806 // Now s contains the list of specific catch clauses
3807 // and g contains the general one.
3809 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
3811 | TRY block opt_catch_clauses FINALLY block
3814 ArrayList s = new ArrayList (4);
3815 ArrayList catch_list = (ArrayList) $3;
3817 if (catch_list != null){
3818 foreach (Catch cc in catch_list) {
3826 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
3830 Report.Error (1524, lexer.Location, "Expected catch or finally");
3835 : /* empty */ { $$ = null; }
3842 ArrayList l = new ArrayList (4);
3847 | catch_clauses catch_clause
3849 ArrayList l = (ArrayList) $1;
3857 : /* empty */ { $$ = null; }
3862 : CATCH opt_catch_args
3864 Expression type = null;
3868 DictionaryEntry cc = (DictionaryEntry) $2;
3869 type = (Expression) cc.Key;
3870 id = (string) cc.Value;
3873 ArrayList one = new ArrayList (4);
3874 Location loc = lexer.Location;
3876 one.Add (new VariableDeclaration (id, null, loc));
3879 current_block = new Block (current_block);
3880 Block b = declare_local_variables (type, one, loc);
3885 Expression type = null;
3889 DictionaryEntry cc = (DictionaryEntry) $2;
3890 type = (Expression) cc.Key;
3891 id = (string) cc.Value;
3895 // FIXME: I can change this for an assignment.
3897 while (current_block != (Block) $1)
3898 current_block = current_block.Parent;
3903 $$ = new Catch (type, id , (Block) $4, ((Block) $4).loc);
3908 : /* empty */ { $$ = null; }
3913 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3915 $$ = new DictionaryEntry ($2, $3);
3922 $$ = new Checked ((Block) $2);
3929 $$ = new Unchecked ((Block) $2);
3936 if (!RootContext.Unsafe){
3937 Report.Error (227, lexer.Location,
3938 "Unsafe code can only be used if --unsafe is used");
3941 $$ = new Unsafe ((Block) $3);
3947 type fixed_pointer_declarators
3950 ArrayList list = (ArrayList) $4;
3951 Expression type = (Expression) $3;
3952 Location l = lexer.Location;
3953 int top = list.Count;
3955 Block assign_block = new Block (current_block);
3956 current_block = assign_block;
3958 for (int i = 0; i < top; i++){
3959 Pair p = (Pair) list [i];
3962 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3976 Location l = (Location) oob_stack.Pop ();
3978 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
3980 if (RootContext.WarningLevel >= 3){
3981 if ($7 == EmptyStatement.Value)
3982 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3985 current_block.AddStatement (f);
3986 while (current_block.Implicit)
3987 current_block = current_block.Parent;
3989 current_block = current_block.Parent;
3993 fixed_pointer_declarators
3994 : fixed_pointer_declarator {
3995 ArrayList declarators = new ArrayList (4);
3997 declarators.Add ($1);
4000 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4002 ArrayList declarators = (ArrayList) $1;
4004 declarators.Add ($3);
4009 fixed_pointer_declarator
4010 : IDENTIFIER ASSIGN expression
4012 $$ = new Pair ($1, $3);
4016 Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
4022 : LOCK OPEN_PARENS expression CLOSE_PARENS
4028 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4033 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4035 Block assign_block = new Block (current_block);
4036 current_block = assign_block;
4038 oob_stack.Push (lexer.Location);
4040 if ($3 is DictionaryEntry){
4041 DictionaryEntry de = (DictionaryEntry) $3;
4042 Location l = lexer.Location;
4044 Expression type = (Expression) de.Key;
4045 ArrayList var_declarators = (ArrayList) de.Value;
4047 ArrayList vars = new ArrayList (4);
4049 foreach (VariableDeclaration decl in var_declarators){
4051 LocalInfo vi = current_block.AddVariable (
4052 type, decl.identifier,
4053 current_local_parameters, decl.Location);
4059 if (decl.expression_or_array_initializer is Expression){
4060 expr = (Expression) decl.expression_or_array_initializer;
4062 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4064 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4067 expr = new ArrayCreation (type, "", init, decl.Location);
4070 LocalVariableReference var;
4072 // Get a writable reference to this read-only variable.
4073 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4075 // This is so that it is not a warning on using variables
4078 vars.Add (new DictionaryEntry (var, expr));
4080 // Assign a = new Assign (var, expr, decl.Location);
4081 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4083 $3 = new DictionaryEntry (type, vars);
4088 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
4089 current_block.AddStatement (u);
4090 while (current_block.Implicit)
4091 current_block = current_block.Parent;
4093 current_block = current_block.Parent;
4097 resource_acquisition
4098 : local_variable_declaration
4105 // A class used to pass around variable declarations and constants
4107 public class VariableDeclaration {
4108 public string identifier;
4109 public object expression_or_array_initializer;
4110 public Location Location;
4111 public Attributes OptAttributes;
4113 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4115 this.identifier = id;
4116 this.expression_or_array_initializer = eoai;
4118 this.OptAttributes = opt_attrs;
4121 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4127 /// Used to pass around interface property information
4129 public class InterfaceAccessorInfo {
4131 public readonly Accessor Get, Set;
4133 public InterfaceAccessorInfo (bool has_get, bool has_set,
4134 Attributes get_attrs, Attributes set_attrs, Location get_loc, Location set_loc)
4137 Get = new Accessor (null, get_attrs, get_loc);
4139 Set = new Accessor (null, set_attrs, set_loc);
4145 // A class used to hold info about an indexer declarator
4147 public class IndexerDeclaration {
4148 public Expression type;
4149 public MemberName interface_type;
4150 public Parameters param_list;
4152 public IndexerDeclaration (Expression type, MemberName interface_type,
4153 Parameters param_list)
4156 this.interface_type = interface_type;
4157 this.param_list = param_list;
4162 // We use this when we do not have an object in advance that is an IIteratorContainer
4164 public class SimpleIteratorContainer : IIteratorContainer {
4167 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4172 public static SimpleIteratorContainer GetSimple () {
4173 Simple.Yields = false;
4177 public void SetYields () { Yields = true; }
4181 // A class used to hold info about an operator declarator
4183 public class OperatorDeclaration {
4184 public Operator.OpType optype;
4185 public Expression ret_type, arg1type, arg2type;
4186 public string arg1name, arg2name;
4187 public Location location;
4189 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4190 Expression arg1type, string arg1name,
4191 Expression arg2type, string arg2name, Location location)
4194 this.ret_type = ret_type;
4195 this.arg1type = arg1type;
4196 this.arg1name = arg1name;
4197 this.arg2type = arg2type;
4198 this.arg2name = arg2name;
4199 this.location = location;
4204 void Error_ExpectingTypeName (Location l, Expression expr)
4206 if (expr is Invocation){
4207 Report.Error (1002, l, "; expected");
4209 Report.Error (-1, l, "Invalid Type definition");
4214 // Given the @class_name name, it creates a fully qualified name
4215 // based on the containing declaration space
4218 MakeName (MemberName class_name)
4220 string ns = current_namespace.FullName;
4222 if (current_container.Name == ""){
4224 return new MemberName (new MemberName (ns), class_name);
4228 return new MemberName (current_container.MemberName, class_name);
4232 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4234 Block implicit_block;
4235 ArrayList inits = null;
4238 // We use the `Used' property to check whether statements
4239 // have been added to the current block. If so, we need
4240 // to create another block to contain the new declaration
4241 // otherwise, as an optimization, we use the same block to
4242 // add the declaration.
4244 // FIXME: A further optimization is to check if the statements
4245 // that were added were added as part of the initialization
4246 // below. In which case, no other statements have been executed
4247 // and we might be able to reduce the number of blocks for
4248 // situations like this:
4250 // int j = 1; int k = j + 1;
4252 if (current_block.Used)
4253 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4255 implicit_block = current_block;
4257 foreach (VariableDeclaration decl in variable_declarators){
4259 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4260 if (decl.expression_or_array_initializer != null){
4262 inits = new ArrayList (4);
4269 return implicit_block;
4271 foreach (VariableDeclaration decl in inits){
4275 if (decl.expression_or_array_initializer is Expression){
4276 expr = (Expression) decl.expression_or_array_initializer;
4279 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4281 expr = new ArrayCreation (type, "", init, decl.Location);
4284 LocalVariableReference var;
4285 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4287 assign = new Assign (var, expr, decl.Location);
4289 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4292 return implicit_block;
4295 Block declare_local_constants (Expression type, ArrayList declarators)
4297 Block implicit_block;
4299 if (current_block.Used)
4300 implicit_block = new Block (current_block, Block.Flags.Implicit);
4302 implicit_block = current_block;
4304 foreach (VariableDeclaration decl in declarators){
4305 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4306 current_local_parameters, decl.Location);
4309 return implicit_block;
4312 void CheckAttributeTarget (string a)
4316 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4320 Location l = lexer.Location;
4321 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4327 void CheckUnaryOperator (Operator.OpType op)
4331 case Operator.OpType.LogicalNot:
4332 case Operator.OpType.OnesComplement:
4333 case Operator.OpType.Increment:
4334 case Operator.OpType.Decrement:
4335 case Operator.OpType.True:
4336 case Operator.OpType.False:
4337 case Operator.OpType.Addition:
4338 case Operator.OpType.Subtraction:
4343 Location l = lexer.Location;
4344 Report.Error (1019, l, "Overloadable unary operator expected");
4350 void CheckBinaryOperator (Operator.OpType op)
4354 case Operator.OpType.Addition:
4355 case Operator.OpType.Subtraction:
4356 case Operator.OpType.Multiply:
4357 case Operator.OpType.Division:
4358 case Operator.OpType.Modulus:
4359 case Operator.OpType.BitwiseAnd:
4360 case Operator.OpType.BitwiseOr:
4361 case Operator.OpType.ExclusiveOr:
4362 case Operator.OpType.LeftShift:
4363 case Operator.OpType.RightShift:
4364 case Operator.OpType.Equality:
4365 case Operator.OpType.Inequality:
4366 case Operator.OpType.GreaterThan:
4367 case Operator.OpType.LessThan:
4368 case Operator.OpType.GreaterThanOrEqual:
4369 case Operator.OpType.LessThanOrEqual:
4373 Location l = lexer.Location;
4374 Report.Error (1020, l, "Overloadable binary operator expected");
4380 void syntax_error (Location l, string msg)
4382 Report.Error (1003, l, "Syntax error, " + msg);
4385 void output (string s)
4387 Console.WriteLine (s);
4390 void note (string s)
4392 // Used to put annotations
4397 public Tokenizer Lexer {
4403 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4405 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4406 this.name = file.Name;
4408 current_container = RootContext.Tree.Types;
4409 current_container.NamespaceEntry = current_namespace;
4410 oob_stack = new Stack ();
4411 switch_stack = new Stack ();
4413 lexer = new Tokenizer (reader, file, defines);
4416 public void parse ()
4419 if (yacc_verbose_flag)
4420 yyparse (lexer, new yydebug.yyDebugSimple ());
4423 Tokenizer tokenizer = lexer as Tokenizer;
4424 tokenizer.cleanup ();
4425 } catch (Exception e){
4427 // Removed for production use, use parser verbose to get the output.
4429 // Console.WriteLine (e);
4430 Report.Error (-25, lexer.Location, "Parsing error");
4431 if (Driver.parser_verbose)
4432 Console.WriteLine (e);
4436 void CheckToken (int error, int yyToken, string msg)
4438 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4439 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4442 Report.Error (error, lexer.Location, msg);
4445 void CheckIdentifierToken (int yyToken)
4447 CheckToken (1041, yyToken, "Identifier expected");