3 // cs-parser.jay: The Parser for the C# compiler
5 // Authors: Miguel de Icaza (miguel@gnu.org)
6 // Ravi Pratap (ravi@ximian.com)
8 // Licensed under the terms of the GNU GPL
10 // (C) 2001 Ximian, Inc (http://www.ximian.com)
13 // (1) Figure out why error productions dont work. `type-declaration' is a
14 // great spot to put an `error' because you can reproduce it with this input:
17 // Possible optimization:
18 // Run memory profiler with parsing only, and consider dropping
19 // arraylists where not needed. Some pieces can use linked lists.
27 using System.Collections;
33 public class CSharpParser : GenericParser {
34 NamespaceEntry current_namespace;
35 TypeContainer current_container;
37 IIteratorContainer iterator_container;
40 // Current block is used to add statements as we find
47 // Current interface is used by the various declaration
48 // productions in the interface declaration to "add"
49 // the interfaces as we find them.
51 Interface current_interface;
54 // This is used by the unary_expression code to resolve
55 // a name against a parameter.
57 Parameters current_local_parameters;
60 // Using during property parsing to describe the implicit
61 // value parameter that is passed to the "set" and "get"accesor
62 // methods (properties and indexers).
64 Expression implicit_value_parameter_type;
65 Parameters indexer_parameters;
68 // Used to determine if we are parsing the get/set pair
69 // of an indexer or a property
74 // An out-of-band stack.
90 %token NONE /* This token is never returned by our lexer */
91 %token ERROR // This is used not by the parser, but by the tokenizer.
95 *These are the C# keywords
182 /* C# keywords which are not really keywords */
188 /* C# single character operators/punctuation. */
189 %token OPEN_BRACE "{"
190 %token CLOSE_BRACE "}"
191 %token OPEN_BRACKET "["
192 %token CLOSE_BRACKET "]"
193 %token OPEN_PARENS "("
194 %token CLOSE_PARENS ")"
207 %token BITWISE_AND "&"
208 %token BITWISE_OR "|"
215 /* C# multi-character operators. */
218 %token OP_SHIFT_LEFT "<<"
219 %token OP_SHIFT_RIGHT ">>"
226 %token OP_MULT_ASSIGN "*="
227 %token OP_DIV_ASSIGN "/="
228 %token OP_MOD_ASSIGN "%="
229 %token OP_ADD_ASSIGN "+="
230 %token OP_SUB_ASSIGN "-="
231 %token OP_SHIFT_LEFT_ASSIGN "<<="
232 %token OP_SHIFT_RIGHT_ASSIGN ">>="
233 %token OP_AND_ASSIGN "&="
234 %token OP_XOR_ASSIGN "^="
235 %token OP_OR_ASSIGN "|="
239 %token LITERAL_INTEGER "int literal"
240 %token LITERAL_FLOAT "float literal"
241 %token LITERAL_DOUBLE "double literal"
242 %token LITERAL_DECIMAL "decimal literal"
243 %token LITERAL_CHARACTER "character literal"
244 %token LITERAL_STRING "string literal"
247 %token CLOSE_PARENS_CAST
248 %token CLOSE_PARENS_NO_CAST
249 %token CLOSE_PARENS_OPEN_PARENS
250 %token CLOSE_PARENS_MINUS
252 /* Add precedence rules to solve dangling else s/r conflict */
261 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
263 %left STAR DIV PERCENT
264 %right BANG CARRET UMINUS
265 %nonassoc OP_INC OP_DEC
267 %left OPEN_BRACKET OPEN_BRACE
271 %start compilation_unit
275 : outer_declarations opt_EOF
276 | outer_declarations attribute_sections opt_EOF
277 | attribute_sections opt_EOF
278 | opt_EOF /* allow empty files */
288 | outer_declarations outer_declaration
293 | namespace_member_declaration
298 | using_directives using_directive
302 : using_alias_directive
303 | using_namespace_directive
306 using_alias_directive
307 : USING IDENTIFIER ASSIGN
308 namespace_or_type_name SEMICOLON
310 current_namespace.UsingAlias ((string) $2, (string) ($4).ToString (), lexer.Location);
313 CheckIdentifierToken (yyToken);
317 using_namespace_directive
318 : USING namespace_name SEMICOLON
320 current_namespace.Using ((string) $2, lexer.Location);
325 // Strictly speaking, namespaces don't have attributes but
326 // we parse global attributes along with namespace declarations and then
329 namespace_declaration
330 : opt_attributes NAMESPACE qualified_identifier
332 Attributes attrs = (Attributes) $1;
335 foreach (AttributeSection asec in attrs.AttributeSections)
336 if (asec.Target == "assembly")
337 RootContext.AddGlobalAttributeSection (current_container, asec);
339 Report.Error(1518, Lexer.Location,
340 "Attributes cannot be applied to namespaces."
341 + " Expected class, delegate, enum, interface, or struct");
344 current_namespace = RootContext.Tree.RecordNamespace (current_namespace, file, (string) ($3).ToString (), lexer.Location);
346 namespace_body opt_semicolon
348 current_namespace = current_namespace.Parent;
363 : IDENTIFIER { $$ = new QualifiedIdentifier ((string) $1); }
364 | qualified_identifier DOT IDENTIFIER {
365 $$ = new QualifiedIdentifier ((QualifiedIdentifier) $1, (string) $3);
371 : namespace_or_type_name { $$ = ($1).ToString (); }
377 opt_namespace_member_declarations
388 opt_namespace_member_declarations
390 | namespace_member_declarations
393 namespace_member_declarations
394 : namespace_member_declaration
395 | namespace_member_declarations namespace_member_declaration
398 namespace_member_declaration
405 Class c = (Class) $1;
406 mod_flags = c.ModFlags;
408 } else if ($1 is Struct){
409 Struct s = (Struct) $1;
410 mod_flags = s.ModFlags;
415 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
417 1527, lexer.Location,
418 "Namespace elements cant be explicitly " +
419 "declared private or protected in `" + name + "'");
421 current_namespace.DeclarationFound = true;
423 | namespace_declaration {
424 current_namespace.DeclarationFound = true;
427 | field_declaration {
428 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
430 | method_declaration {
431 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
438 | interface_declaration
440 | delegate_declaration
442 // Enable this when we have handled all errors, because this acts as a generic fallback
445 // Console.WriteLine ("Token=" + yyToken);
446 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
456 | attribute_sections { $$ = $1; }
462 AttributeSection sect = (AttributeSection) $1;
464 if (sect.Target == "assembly")
465 RootContext.AddGlobalAttributeSection (current_container, sect);
468 $$ = new Attributes ((AttributeSection) $1);
470 | attribute_sections attribute_section
472 Attributes attrs = null;
473 AttributeSection sect = (AttributeSection) $2;
475 if (sect.Target == "assembly")
476 RootContext.AddGlobalAttributeSection (current_container, sect);
479 attrs = (Attributes) $1;
480 attrs.AddAttributeSection (sect);
488 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
490 string target = null;
493 target = (string) $2;
495 $$ = new AttributeSection (target, (ArrayList) $3);
497 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
499 $$ = new AttributeSection (null, (ArrayList) $2);
503 attribute_target_specifier
504 : attribute_target COLON
513 CheckAttributeTarget ((string) $1);
516 | EVENT { $$ = "event"; }
517 | RETURN { $$ = "return"; }
523 ArrayList attrs = new ArrayList (4);
529 | attribute_list COMMA attribute
531 ArrayList attrs = (ArrayList) $1;
543 opt_attribute_arguments
545 $$ = new Attribute ((string) ($1).ToString (), (ArrayList) $3, (Location) $2);
550 : type_name { /* reserved attribute name or identifier: 17.4 */ }
553 opt_attribute_arguments
554 : /* empty */ { $$ = null; }
555 | OPEN_PARENS attribute_arguments CLOSE_PARENS
563 : opt_positional_argument_list
568 ArrayList args = new ArrayList (4);
574 | positional_argument_list COMMA named_argument_list
576 ArrayList args = new ArrayList (4);
582 | named_argument_list
584 ArrayList args = new ArrayList (4);
593 opt_positional_argument_list
594 : /* empty */ { $$ = null; }
595 | positional_argument_list
598 positional_argument_list
601 ArrayList args = new ArrayList (4);
602 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
606 | positional_argument_list COMMA expression
608 ArrayList args = (ArrayList) $1;
609 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
618 ArrayList args = new ArrayList (4);
623 | named_argument_list COMMA named_argument
625 ArrayList args = (ArrayList) $1;
633 : IDENTIFIER ASSIGN expression
635 $$ = new DictionaryEntry (
637 new Argument ((Expression) $3, Argument.AType.Expression));
643 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
646 opt_class_member_declarations
648 | class_member_declarations
651 class_member_declarations
652 : class_member_declaration
653 | class_member_declarations
654 class_member_declaration
657 class_member_declaration
658 : constant_declaration // done
659 | field_declaration // done
660 | method_declaration // done
661 | property_declaration // done
662 | event_declaration // done
663 | indexer_declaration // done
664 | operator_declaration // done
665 | constructor_declaration // done
666 | destructor_declaration // done
676 string full_struct_name = MakeName ((string) $4);
678 new_struct = new Struct (current_namespace, current_container, full_struct_name,
679 (int) $2, (Attributes) $1, lexer.Location);
680 current_container = new_struct;
681 RootContext.Tree.RecordDecl (full_struct_name, new_struct);
687 Struct new_struct = (Struct) current_container;
690 new_struct.Bases = (ArrayList) $6;
692 current_container = current_container.Parent;
693 CheckDef (current_container.AddStruct (new_struct), new_struct.Name, new_struct.Location);
696 | opt_attributes opt_modifiers STRUCT error {
697 CheckIdentifierToken (yyToken);
702 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
705 opt_struct_member_declarations
707 | struct_member_declarations
710 struct_member_declarations
711 : struct_member_declaration
712 | struct_member_declarations struct_member_declaration
715 struct_member_declaration
716 : constant_declaration
719 | property_declaration
721 | indexer_declaration
722 | operator_declaration
723 | constructor_declaration
727 * This is only included so we can flag error 575:
728 * destructors only allowed on class types
730 | destructor_declaration
741 foreach (VariableDeclaration constant in (ArrayList) $5){
742 Location l = constant.Location;
744 Const c = new Const (
745 (Expression) $4, (string) constant.identifier,
746 (Expression) constant.expression_or_array_initializer, (int) $2,
749 CheckDef (current_container.AddConstant (c), c.Name, l);
755 : constant_declarator
757 ArrayList constants = new ArrayList (4);
761 | constant_declarators COMMA constant_declarator
763 ArrayList constants = (ArrayList) $1;
770 : IDENTIFIER ASSIGN constant_expression
772 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
783 Expression type = (Expression) $3;
786 foreach (VariableDeclaration var in (ArrayList) $4){
787 Location l = var.Location;
789 Field field = new Field (type, mod, var.identifier,
790 var.expression_or_array_initializer,
793 CheckDef (current_container.AddField (field), field.Name, l);
801 Report.Error (670, lexer.Location, "void type is not allowed for fields");
806 : variable_declarator
808 ArrayList decl = new ArrayList (4);
812 | variable_declarators COMMA variable_declarator
814 ArrayList decls = (ArrayList) $1;
821 : IDENTIFIER ASSIGN variable_initializer
823 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
827 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
840 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
842 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
848 iterator_container = (IIteratorContainer) $1;
852 Method method = (Method) $1;
853 Block b = (Block) $3;
854 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
857 if ((method.ModFlags & extern_abstract) == 0){
859 501, lexer.Location, current_container.MakeName (method.Name) +
860 "must declare a body because it is not marked abstract or extern");
863 if ((method.ModFlags & Modifiers.EXTERN) != 0){
865 179, lexer.Location, current_container.MakeName (method.Name) +
866 " is declared extern, but has a body");
870 method.Block = (Block) $3;
871 CheckDef (current_container.AddMethod (method), method.Name, method.Location);
873 current_local_parameters = null;
874 iterator_container = null;
888 1585, lexer.Location, "Member modifier `" +
889 Modifiers.Name (i) + "' must precede member type and name");
902 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
904 Method method = new Method (current_container, (Expression) $3, (int) $2, (string) $4,
905 (Parameters) $6, (Attributes) $1, lexer.Location);
907 current_local_parameters = (Parameters) $6;
915 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
917 Method method = new Method (current_container, TypeManager.system_void_expr, (int) $2,
918 (string) $4, (Parameters) $6, (Attributes) $1,
921 current_local_parameters = (Parameters) $6;
927 modifiers member_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
929 Report.Error (1585, lexer.Location,
930 String.Format ("Modifier {0} should appear before type",
931 Modifiers.Name ((int) $4)));
932 Method method = new Method (current_container, TypeManager.system_void_expr, 0,
933 (string) $5, (Parameters) $7, (Attributes) $1,
936 current_local_parameters = (Parameters) $7;
943 | SEMICOLON { $$ = null; }
946 opt_formal_parameter_list
947 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
948 | formal_parameter_list
951 formal_parameter_list
954 ArrayList pars_list = (ArrayList) $1;
956 Parameter [] pars = new Parameter [pars_list.Count];
957 pars_list.CopyTo (pars);
959 $$ = new Parameters (pars, null, lexer.Location);
961 | fixed_parameters COMMA parameter_array
963 ArrayList pars_list = (ArrayList) $1;
965 Parameter [] pars = new Parameter [pars_list.Count];
966 pars_list.CopyTo (pars);
968 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
972 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
979 ArrayList pars = new ArrayList (4);
984 | fixed_parameters COMMA fixed_parameter
986 ArrayList pars = (ArrayList) $1;
995 opt_parameter_modifier
999 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
1002 opt_parameter_modifier
1005 CheckIdentifierToken (yyToken);
1010 opt_parameter_modifier
1011 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1012 | parameter_modifier
1016 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1017 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1021 : opt_attributes PARAMS type IDENTIFIER
1023 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1024 note ("type must be a single-dimension array type");
1026 | opt_attributes PARAMS type error {
1027 CheckIdentifierToken (yyToken);
1033 : qualified_identifier { $$ = ($1).ToString (); }
1036 property_declaration
1042 implicit_value_parameter_type = (Expression) $3;
1044 lexer.PropertyParsing = true;
1046 $$ = lexer.Location;
1048 accessor_declarations
1050 lexer.PropertyParsing = false;
1055 Pair pair = (Pair) $7;
1056 Accessor get_block = (Accessor) pair.First;
1057 Accessor set_block = (Accessor) pair.Second;
1059 Location loc = (Location) $6;
1060 prop = new Property (current_container, (Expression) $3, (string) $4, (int) $2,
1061 get_block, set_block, (Attributes) $1, loc);
1063 CheckDef (current_container.AddProperty (prop), prop.Name, loc);
1064 implicit_value_parameter_type = null;
1068 accessor_declarations
1069 : get_accessor_declaration opt_set_accessor_declaration
1071 $$ = new Pair ($1, $2);
1073 | set_accessor_declaration opt_get_accessor_declaration
1075 $$ = new Pair ($2, $1);
1079 opt_get_accessor_declaration
1080 : /* empty */ { $$ = null; }
1081 | get_accessor_declaration
1084 opt_set_accessor_declaration
1085 : /* empty */ { $$ = null; }
1086 | set_accessor_declaration
1089 get_accessor_declaration
1090 : opt_attributes GET
1092 // If this is not the case, then current_local_parameters has already
1093 // been set in indexer_declaration
1094 if (parsing_indexer == false)
1095 current_local_parameters = null;
1097 current_local_parameters = indexer_parameters;
1098 lexer.PropertyParsing = false;
1102 $$ = new Accessor ((Block) $4, (Attributes) $1);
1103 current_local_parameters = null;
1104 lexer.PropertyParsing = true;
1108 set_accessor_declaration
1109 : opt_attributes SET
1112 Parameter implicit_value_parameter = new Parameter (
1113 implicit_value_parameter_type, "value",
1114 Parameter.Modifier.NONE, null);
1116 if (parsing_indexer == false) {
1117 args = new Parameter [1];
1118 args [0] = implicit_value_parameter;
1119 current_local_parameters = new Parameters (args, null, lexer.Location);
1121 Parameter [] fpars = indexer_parameters.FixedParameters;
1124 int count = fpars.Length;
1126 args = new Parameter [count + 1];
1127 fpars.CopyTo (args, 0);
1128 args [count] = implicit_value_parameter;
1131 current_local_parameters = new Parameters (
1132 args, indexer_parameters.ArrayParameter, lexer.Location);
1135 lexer.PropertyParsing = false;
1139 $$ = new Accessor ((Block) $4, (Attributes) $1);
1140 current_local_parameters = null;
1141 lexer.PropertyParsing = true;
1147 | SEMICOLON { $$ = null; }
1150 interface_declaration
1153 INTERFACE IDENTIFIER
1155 Interface new_interface;
1156 string full_interface_name = MakeName ((string) $4);
1158 new_interface = new Interface (current_namespace, current_container, full_interface_name,
1159 (int) $2, (Attributes) $1, lexer.Location);
1160 if (current_interface != null) {
1161 Location l = lexer.Location;
1162 Report.Error (-2, l, "Internal compiler error: interface inside interface");
1164 current_interface = new_interface;
1165 RootContext.Tree.RecordDecl (full_interface_name, new_interface);
1168 interface_body opt_semicolon
1170 Interface new_interface = (Interface) current_interface;
1173 new_interface.Bases = (ArrayList) $6;
1175 current_interface = null;
1176 CheckDef (current_container.AddInterface (new_interface),
1177 new_interface.Name, new_interface.Location);
1179 | opt_attributes opt_modifiers INTERFACE error {
1180 CheckIdentifierToken (yyToken);
1185 : /* empty */ { $$ = null; }
1190 : COLON interface_type_list { $$ = $2; }
1196 ArrayList interfaces = new ArrayList (4);
1198 interfaces.Add (($1).ToString ());
1201 | interface_type_list COMMA interface_type
1203 ArrayList interfaces = (ArrayList) $1;
1204 interfaces.Add (($3).ToString ());
1211 opt_interface_member_declarations
1215 opt_interface_member_declarations
1217 | interface_member_declarations
1220 interface_member_declarations
1221 : interface_member_declaration
1222 | interface_member_declarations interface_member_declaration
1225 interface_member_declaration
1226 : interface_method_declaration
1228 InterfaceMethod m = (InterfaceMethod) $1;
1230 CheckDef (current_interface.AddMethod (m), m.Name, m.Location);
1232 | interface_property_declaration
1234 InterfaceProperty p = (InterfaceProperty) $1;
1236 CheckDef (current_interface.AddProperty (p), p.Name, p.Location);
1238 | interface_event_declaration
1241 InterfaceEvent e = (InterfaceEvent) $1;
1242 CheckDef (current_interface.AddEvent (e), e.Name, lexer.Location);
1245 | interface_indexer_declaration
1247 InterfaceIndexer i = (InterfaceIndexer) $1;
1249 CheckDef (current_interface.AddIndexer (i), "indexer", i.Location);
1258 val = Modifiers.Check (Modifiers.NEW, val, 0, lexer.Location);
1259 $$ = (bool) (val != 0);
1263 interface_method_declaration
1264 : opt_attributes opt_new type IDENTIFIER
1265 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1268 $$ = new InterfaceMethod ((Expression) $3, (string) $4, (bool) $2,
1269 (Parameters) $6, (Attributes) $1, lexer.Location);
1271 | opt_attributes opt_new VOID IDENTIFIER
1272 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1275 $$ = new InterfaceMethod (
1276 TypeManager.system_void_expr, (string) $4, (bool) $2, (Parameters) $6,
1277 (Attributes) $1, lexer.Location);
1281 interface_property_declaration
1286 { lexer.PropertyParsing = true; }
1288 { lexer.PropertyParsing = false; }
1293 $$ = new InterfaceProperty ((Expression) $3, (string) $4, (bool) $2,
1294 (gs & 1) == 1, (gs & 2) == 2, (Attributes) $1,
1300 CheckIdentifierToken (yyToken);
1306 : opt_attributes GET SEMICOLON { $$ = 1; }
1307 | opt_attributes SET SEMICOLON { $$ = 2; }
1308 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1310 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1314 interface_event_declaration
1315 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1317 $$ = new InterfaceEvent ((Expression) $4, (string) $5, (bool) $2, (Attributes) $1,
1320 | opt_attributes opt_new EVENT type error {
1321 CheckIdentifierToken (yyToken);
1324 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1325 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1328 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE event_accessor_declarations CLOSE_BRACE {
1329 Report.Error (69, lexer.Location, "Event accessors not valid on interfaces");
1334 interface_indexer_declaration
1335 : opt_attributes opt_new type THIS
1336 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1338 { lexer.PropertyParsing = true; }
1340 { lexer.PropertyParsing = false; }
1343 int a_flags = (int) $10;
1345 bool do_get = (a_flags & 1) == 1;
1346 bool do_set = (a_flags & 2) == 2;
1348 $$ = new InterfaceIndexer ((Expression) $3, (Parameters) $6, do_get, do_set,
1349 (bool) $2, (Attributes) $1, lexer.Location);
1353 operator_declaration
1354 : opt_attributes opt_modifiers operator_declarator operator_body
1356 OperatorDeclaration decl = (OperatorDeclaration) $3;
1358 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
1359 decl.arg2type, decl.arg2name, (Block) $4, (Attributes) $1, decl.location);
1361 // Note again, checking is done in semantic analysis
1362 current_container.AddOperator (op);
1364 current_local_parameters = null;
1370 | SEMICOLON { $$ = null; }
1373 : type OPERATOR overloadable_operator
1374 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1376 Operator.OpType op = (Operator.OpType) $3;
1377 CheckUnaryOperator (op);
1379 if (op == Operator.OpType.Addition)
1380 op = Operator.OpType.UnaryPlus;
1382 if (op == Operator.OpType.Subtraction)
1383 op = Operator.OpType.UnaryNegation;
1385 Parameter [] pars = new Parameter [1];
1387 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1389 current_local_parameters = new Parameters (pars, null, lexer.Location);
1391 $$ = new OperatorDeclaration (op, (Expression) $1, (Expression) $5, (string) $6,
1392 null, null, lexer.Location);
1394 | type OPERATOR overloadable_operator
1396 type IDENTIFIER COMMA
1400 CheckBinaryOperator ((Operator.OpType) $3);
1402 Parameter [] pars = new Parameter [2];
1404 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1405 pars [1] = new Parameter ((Expression) $8, (string) $9, Parameter.Modifier.NONE, null);
1407 current_local_parameters = new Parameters (pars, null, lexer.Location);
1409 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1410 (Expression) $5, (string) $6,
1411 (Expression) $8, (string) $9, lexer.Location);
1413 | conversion_operator_declarator
1416 overloadable_operator
1418 : BANG { $$ = Operator.OpType.LogicalNot; }
1419 | TILDE { $$ = Operator.OpType.OnesComplement; }
1420 | OP_INC { $$ = Operator.OpType.Increment; }
1421 | OP_DEC { $$ = Operator.OpType.Decrement; }
1422 | TRUE { $$ = Operator.OpType.True; }
1423 | FALSE { $$ = Operator.OpType.False; }
1424 // Unary and binary:
1425 | PLUS { $$ = Operator.OpType.Addition; }
1426 | MINUS { $$ = Operator.OpType.Subtraction; }
1428 | STAR { $$ = Operator.OpType.Multiply; }
1429 | DIV { $$ = Operator.OpType.Division; }
1430 | PERCENT { $$ = Operator.OpType.Modulus; }
1431 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1432 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1433 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1434 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1435 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1436 | OP_EQ { $$ = Operator.OpType.Equality; }
1437 | OP_NE { $$ = Operator.OpType.Inequality; }
1438 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1439 | OP_LT { $$ = Operator.OpType.LessThan; }
1440 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1441 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1444 conversion_operator_declarator
1445 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1447 Parameter [] pars = new Parameter [1];
1449 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1451 current_local_parameters = new Parameters (pars, null, lexer.Location);
1453 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1454 null, null, lexer.Location);
1456 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1458 Parameter [] pars = new Parameter [1];
1460 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1462 current_local_parameters = new Parameters (pars, null, lexer.Location);
1464 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1465 null, null, lexer.Location);
1469 syntax_error (lexer.Location, "'operator' expected");
1473 syntax_error (lexer.Location, "'operator' expected");
1477 constructor_declaration
1480 constructor_declarator
1483 Constructor c = (Constructor) $3;
1484 c.Block = (Block) $4;
1485 c.OptAttributes = (Attributes) $1;
1486 c.ModFlags = (int) $2;
1488 if (c.Name == current_container.Basename){
1489 if ((c.ModFlags & Modifiers.STATIC) != 0){
1490 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1492 515, c.Location, String.Format (
1493 "`{0}.{1}': static constructor can not have access modifiers",
1494 c.Name, current_container.Name));
1497 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1499 if (c.Initializer != null){
1502 "Static constructors can not have an explicit this or base " +
1503 "constructor invocations");
1506 if (!c.Parameters.Empty){
1508 132, c.Location, "Static constructors should not have parameters");
1511 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1514 // We let another layer check the validity of the constructor.
1515 Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1518 CheckDef (current_container.AddConstructor (c), c.Name, c.Location);
1520 current_local_parameters = null;
1524 constructor_declarator
1526 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1528 oob_stack.Push (lexer.Location);
1530 current_local_parameters = (Parameters) $3;
1532 opt_constructor_initializer
1534 Location l = (Location) oob_stack.Pop ();
1535 $$ = new Constructor (current_container, (string) $1, (Parameters) $3,
1536 (ConstructorInitializer) $6, l);
1542 | SEMICOLON { $$ = null; }
1545 opt_constructor_initializer
1546 : /* empty */ { $$ = null; }
1547 | constructor_initializer
1550 constructor_initializer
1551 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1553 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1555 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1557 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1560 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1566 : /* EMPTY */ { $$ = 0; }
1567 | UNSAFE { $$ = Modifiers.UNSAFE; }
1568 | EXTERN { $$ = Modifiers.EXTERN; }
1571 destructor_declaration
1572 : opt_attributes opt_finalizer TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1574 if ((string) $4 != current_container.Basename){
1575 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1576 } else if (!(current_container is Class)){
1577 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1579 Location l = lexer.Location;
1582 if (!RootContext.StdLib && current_container.Name == "System.Object")
1583 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
1585 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
1587 if ((m & Modifiers.UNSAFE) != 0){
1588 if (!RootContext.Unsafe){
1589 Report.Error (227, l,
1590 "Unsafe code requires the --unsafe command " +
1591 "line option to be specified");
1595 Method d = new Destructor (
1596 current_container, TypeManager.system_void_expr, m, "Finalize",
1597 new Parameters (null, null, l), (Attributes) $1, l);
1599 d.Block = (Block) $7;
1600 CheckDef (current_container.AddMethod (d), d.Name, d.Location);
1608 EVENT type variable_declarators SEMICOLON
1610 foreach (VariableDeclaration var in (ArrayList) $5) {
1612 Event e = new Event ((Expression) $4, var.identifier,
1613 var.expression_or_array_initializer,
1614 (int) $2, null, null, (Attributes) $1, lexer.Location);
1616 CheckDef (current_container.AddEvent (e), e.Name, e.Location);
1622 EVENT type member_name
1625 implicit_value_parameter_type = (Expression) $4;
1626 lexer.EventParsing = true;
1627 oob_stack.Push (lexer.Location);
1629 event_accessor_declarations
1631 lexer.EventParsing = false;
1635 Location loc = (Location) oob_stack.Pop ();
1638 Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
1641 Pair pair = (Pair) $8;
1642 Accessor add_accessor = null;
1643 Accessor rem_accessor = null;
1645 if (pair.First != null)
1646 add_accessor = (Accessor) pair.First;
1647 if (pair.Second != null)
1648 rem_accessor = (Accessor) pair.Second;
1650 Event e = new Event ((Expression) $4, (string) $5, null, (int) $2, add_accessor, rem_accessor,
1651 (Attributes) $1, loc);
1653 CheckDef (current_container.AddEvent (e), e.Name, loc);
1654 implicit_value_parameter_type = null;
1659 event_accessor_declarations
1660 : add_accessor_declaration remove_accessor_declaration
1662 $$ = new Pair ($1, $2);
1664 | remove_accessor_declaration add_accessor_declaration
1666 $$ = new Pair ($2, $1);
1668 | add_accessor_declaration { $$ = null; }
1669 | remove_accessor_declaration { $$ = null; }
1672 add_accessor_declaration
1673 : opt_attributes ADD
1675 Parameter [] args = new Parameter [1];
1676 Parameter implicit_value_parameter = new Parameter (
1677 implicit_value_parameter_type, "value",
1678 Parameter.Modifier.NONE, null);
1680 args [0] = implicit_value_parameter;
1682 current_local_parameters = new Parameters (args, null, lexer.Location);
1683 lexer.EventParsing = false;
1687 $$ = new Accessor ((Block) $4, (Attributes) $1);
1688 lexer.EventParsing = true;
1690 | opt_attributes ADD error {
1691 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1696 remove_accessor_declaration
1697 : opt_attributes REMOVE
1699 Parameter [] args = new Parameter [1];
1700 Parameter implicit_value_parameter = new Parameter (
1701 implicit_value_parameter_type, "value",
1702 Parameter.Modifier.NONE, null);
1704 args [0] = implicit_value_parameter;
1706 current_local_parameters = new Parameters (args, null, lexer.Location);
1707 lexer.EventParsing = false;
1711 $$ = new Accessor ((Block) $4, (Attributes) $1);
1712 lexer.EventParsing = true;
1714 | opt_attributes REMOVE error {
1715 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1721 : opt_attributes opt_modifiers indexer_declarator
1724 IndexerDeclaration decl = (IndexerDeclaration) $3;
1726 implicit_value_parameter_type = decl.type;
1728 lexer.PropertyParsing = true;
1729 parsing_indexer = true;
1731 indexer_parameters = decl.param_list;
1732 oob_stack.Push (lexer.Location);
1734 accessor_declarations
1736 lexer.PropertyParsing = false;
1737 parsing_indexer = false;
1741 // The signature is computed from the signature of the indexer. Look
1742 // at section 3.6 on the spec
1743 Location loc = (Location) oob_stack.Pop ();
1745 IndexerDeclaration decl = (IndexerDeclaration) $3;
1746 Pair pair = (Pair) $6;
1747 Accessor get_block = (Accessor) pair.First;
1748 Accessor set_block = (Accessor) pair.Second;
1750 indexer = new Indexer (current_container, decl.type, decl.interface_type, (int) $2,
1751 decl.param_list, get_block, set_block, (Attributes) $1, loc);
1753 // Note that there is no equivalent of CheckDef for this case
1754 // We shall handle this in semantic analysis
1756 current_container.AddIndexer (indexer);
1758 current_local_parameters = null;
1759 implicit_value_parameter_type = null;
1760 indexer_parameters = null;
1765 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1767 Parameters pars = (Parameters) $4;
1769 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1770 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1773 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
1775 | type qualified_identifier DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1777 Parameters pars = (Parameters) $6;
1779 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1780 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1782 $$ = new IndexerDeclaration ((Expression) $1, (string) ($2).ToString (), pars);
1794 Location enum_location = lexer.Location;
1796 string full_name = MakeName ((string) $4);
1797 Enum e = new Enum (current_namespace, current_container, (Expression) $5, (int) $2,
1798 full_name, (Attributes) $1, enum_location);
1800 foreach (VariableDeclaration ev in (ArrayList) $6) {
1801 Location loc = (Location) ev.Location;
1803 CheckDef (e.AddEnumMember (ev.identifier,
1804 (Expression) ev.expression_or_array_initializer,
1805 loc, ev.OptAttributes),
1806 ev.identifier, loc);
1809 CheckDef (current_container.AddEnum (e), full_name, enum_location);
1810 RootContext.Tree.RecordDecl (full_name, e);
1816 : /* empty */ { $$ = TypeManager.system_int32_expr; }
1817 | COLON type { $$ = $2; }
1821 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1827 opt_enum_member_declarations
1828 : /* empty */ { $$ = new ArrayList (4); }
1829 | enum_member_declarations opt_comma { $$ = $1; }
1832 enum_member_declarations
1833 : enum_member_declaration
1835 ArrayList l = new ArrayList (4);
1840 | enum_member_declarations COMMA enum_member_declaration
1842 ArrayList l = (ArrayList) $1;
1850 enum_member_declaration
1851 : opt_attributes IDENTIFIER
1853 $$ = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
1855 | opt_attributes IDENTIFIER
1857 $$ = lexer.Location;
1861 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
1865 delegate_declaration
1869 IDENTIFIER OPEN_PARENS
1870 opt_formal_parameter_list
1874 Location l = lexer.Location;
1875 Delegate del = new Delegate (current_namespace, current_container, (Expression) $4,
1876 (int) $2, MakeName ((string) $5), (Parameters) $7,
1877 (Attributes) $1, l);
1879 CheckDef (current_container.AddDelegate (del), del.Name, l);
1884 IDENTIFIER OPEN_PARENS
1885 opt_formal_parameter_list
1889 Location l = lexer.Location;
1890 Delegate del = new Delegate (
1891 current_namespace, current_container,
1892 TypeManager.system_void_expr, (int) $2, MakeName ((string) $5),
1893 (Parameters) $7, (Attributes) $1, l);
1895 CheckDef (current_container.AddDelegate (del), del.Name, l);
1900 : namespace_or_type_name
1903 namespace_or_type_name
1904 : qualified_identifier
1907 type_name_expression
1908 : IDENTIFIER { $$ = new SimpleName ((string) $1, lexer.Location);}
1909 | type_name_expression DOT IDENTIFIER {
1910 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1915 * Before you think of adding a return_type, notice that we have been
1916 * using two rules in the places where it matters (one rule using type
1917 * and another identical one that uses VOID as the return type). This
1918 * gets rid of a shift/reduce couple
1921 : type_name_expression { /* class_type */
1923 This does interfaces, delegates, struct_types, class_types,
1924 parent classes, and more! 4.2
1937 // Note that here only unmanaged types are allowed but we
1938 // can't perform checks during this phase - we do it during
1939 // semantic analysis.
1941 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1945 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
1951 | non_expression_type rank_specifier
1953 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1955 | non_expression_type STAR
1957 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1959 | expression rank_specifiers
1961 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1965 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1969 // We need this because the parser will happily go and reduce IDENTIFIER STAR
1970 // through this different path
1972 | multiplicative_expression STAR
1974 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1981 ArrayList types = new ArrayList (4);
1986 | type_list COMMA type
1988 ArrayList types = (ArrayList) $1;
1996 * replaces all the productions for isolating the various
1997 * simple types, but we need this to reuse it easily in local_variable_type
2000 : OBJECT { $$ = TypeManager.system_object_expr; }
2001 | STRING { $$ = TypeManager.system_string_expr; }
2002 | BOOL { $$ = TypeManager.system_boolean_expr; }
2003 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2004 | FLOAT { $$ = TypeManager.system_single_expr; }
2005 | DOUBLE { $$ = TypeManager.system_double_expr; }
2010 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2011 | BYTE { $$ = TypeManager.system_byte_expr; }
2012 | SHORT { $$ = TypeManager.system_int16_expr; }
2013 | USHORT { $$ = TypeManager.system_uint16_expr; }
2014 | INT { $$ = TypeManager.system_int32_expr; }
2015 | UINT { $$ = TypeManager.system_uint32_expr; }
2016 | LONG { $$ = TypeManager.system_int64_expr; }
2017 | ULONG { $$ = TypeManager.system_uint64_expr; }
2018 | CHAR { $$ = TypeManager.system_char_expr; }
2019 | VOID { $$ = TypeManager.system_void_expr; }
2027 : type rank_specifiers
2029 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2034 // Expressions, section 7.5
2042 | IDENTIFIER { $$ = new SimpleName ((string) $1, lexer.Location); }
2043 | parenthesized_expression
2045 | invocation_expression
2049 | post_increment_expression
2050 | post_decrement_expression
2054 | checked_expression
2055 | unchecked_expression
2056 | pointer_member_access
2057 | anonymous_method_expression
2064 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2065 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2066 | NULL { $$ = NullLiteral.Null; }
2070 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2071 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2072 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2077 object v = lexer.Value;
2083 $$ = IntLiteral.Zero;
2085 $$ = IntLiteral.One;
2087 $$ = new IntLiteral (i);
2088 } else if (v is uint)
2089 $$ = new UIntLiteral ((UInt32) v);
2091 $$ = new LongLiteral ((Int64) v);
2092 else if (v is ulong)
2093 $$ = new ULongLiteral ((UInt64) v);
2095 Console.WriteLine ("OOPS. Unexpected result from scanner");
2100 : TRUE { $$ = new BoolLiteral (true); }
2101 | FALSE { $$ = new BoolLiteral (false); }
2104 parenthesized_expression_0
2105 : OPEN_PARENS expression CLOSE_PARENS
2108 lexer.Deambiguate_CloseParens ();
2109 // After this, the next token returned is one of
2110 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2111 // or CLOSE_PARENS_MINUS.
2115 parenthesized_expression
2116 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2120 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2122 // If a parenthesized expression is followed by a minus, we need to wrap
2123 // the expression inside a ParenthesizedExpression for the CS0075 check
2124 // in Binary.DoResolve().
2125 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2130 : primary_expression DOT IDENTIFIER
2132 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2134 | predefined_type DOT IDENTIFIER
2136 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2144 invocation_expression
2145 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2148 Location l = lexer.Location;
2149 Report.Error (1, l, "Parse error");
2151 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2153 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2155 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2157 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2159 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2164 : /* empty */ { $$ = null; }
2171 ArrayList list = new ArrayList (4);
2175 | argument_list COMMA argument
2177 ArrayList list = (ArrayList) $1;
2181 | argument_list error {
2182 CheckToken (1026, yyToken, ", or ) expected");
2189 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2191 | REF variable_reference
2193 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2195 | OUT variable_reference
2197 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2202 : expression { note ("section 5.4"); $$ = $1; }
2206 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2208 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2210 | primary_expression rank_specifiers
2212 // So the super-trick is that primary_expression
2213 // can only be either a SimpleName or a MemberAccess.
2214 // The MemberAccess case arises when you have a fully qualified type-name like :
2216 // SimpleName is when you have
2219 Expression expr = (Expression) $1;
2220 if (expr is ComposedCast){
2221 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2222 } else if (!(expr is SimpleName || expr is MemberAccess)){
2223 Error_ExpectingTypeName (lexer.Location, expr);
2224 $$ = TypeManager.system_object_expr;
2227 // So we extract the string corresponding to the SimpleName
2230 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2238 ArrayList list = new ArrayList (4);
2242 | expression_list COMMA expression
2244 ArrayList list = (ArrayList) $1;
2253 $$ = new This (current_block, lexer.Location);
2258 : BASE DOT IDENTIFIER
2260 $$ = new BaseAccess ((string) $3, lexer.Location);
2262 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2264 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2267 Report.Error (175, "Use of keyword `base' is not valid in this context");
2272 post_increment_expression
2273 : primary_expression OP_INC
2275 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2276 (Expression) $1, lexer.Location);
2280 post_decrement_expression
2281 : primary_expression OP_DEC
2283 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2284 (Expression) $1, lexer.Location);
2289 : object_or_delegate_creation_expression
2290 | array_creation_expression
2293 object_or_delegate_creation_expression
2294 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2296 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2300 array_creation_expression
2301 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2303 opt_array_initializer
2305 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2307 | NEW type rank_specifiers array_initializer
2309 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2313 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2329 : rank_specifier opt_rank_specifier
2331 $$ = (string) $2 + (string) $1;
2336 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2338 $$ = "[" + (string) $2 + "]";
2358 | dim_separators COMMA
2360 $$ = (string) $1 + ",";
2364 opt_array_initializer
2376 : OPEN_BRACE CLOSE_BRACE
2378 ArrayList list = new ArrayList (4);
2381 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2383 $$ = (ArrayList) $2;
2387 variable_initializer_list
2388 : variable_initializer
2390 ArrayList list = new ArrayList (4);
2394 | variable_initializer_list COMMA variable_initializer
2396 ArrayList list = (ArrayList) $1;
2403 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2405 $$ = new TypeOfVoid (lexer.Location);
2407 | TYPEOF OPEN_PARENS type CLOSE_PARENS
2409 $$ = new TypeOf ((Expression) $3, lexer.Location);
2414 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2415 $$ = new SizeOf ((Expression) $3, lexer.Location);
2420 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2422 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2426 unchecked_expression
2427 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2429 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2433 pointer_member_access
2434 : primary_expression OP_PTR IDENTIFIER
2438 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2439 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2443 anonymous_method_expression
2444 : DELEGATE opt_anonymous_method_signature {
2445 oob_stack.Push (current_local_parameters);
2446 current_local_parameters = (Parameters)$2;
2448 if (!RootContext.V2){
2449 Report.Error (-213, lexer.Location, "Anonymous methods are only supported in V2");
2452 $$ = new AnonymousMethod ((Parameters) $2, (Block) $4, lexer.Location);
2453 current_local_parameters = (Parameters) oob_stack.Pop ();
2457 opt_anonymous_method_signature
2458 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
2459 | anonymous_method_signature
2462 anonymous_method_signature
2463 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
2466 $$ = Parameters.EmptyReadOnlyParameters;
2468 ArrayList par_list = (ArrayList) $2;
2469 Parameter [] pars = new Parameter [par_list.Count];
2470 par_list.CopyTo (pars);
2471 $$ = new Parameters (pars, null, lexer.Location);
2476 opt_anonymous_method_parameter_list
2477 : /* empty */ { $$ = null; }
2478 | anonymous_method_parameter_list { $$ = $1; }
2481 anonymous_method_parameter_list
2482 : anonymous_method_parameter
2484 ArrayList a = new ArrayList (4);
2488 | anonymous_method_parameter_list COMMA anonymous_method_parameter
2490 ArrayList a = (ArrayList) $1;
2496 anonymous_method_parameter
2497 : opt_parameter_modifier type IDENTIFIER {
2498 $$ = new Parameter ((Expression) $2, (string) $2, (Parameter.Modifier) $1, null);
2503 : primary_expression
2504 | BANG prefixed_unary_expression
2506 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2508 | TILDE prefixed_unary_expression
2510 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2516 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
2518 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2520 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
2522 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2528 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2530 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2535 // The idea to split this out is from Rhys' grammar
2536 // to solve the problem with casts.
2538 prefixed_unary_expression
2540 | PLUS prefixed_unary_expression
2542 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2544 | MINUS prefixed_unary_expression
2546 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2548 | OP_INC prefixed_unary_expression
2550 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2551 (Expression) $2, lexer.Location);
2553 | OP_DEC prefixed_unary_expression
2555 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2556 (Expression) $2, lexer.Location);
2558 | STAR prefixed_unary_expression
2560 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2562 | BITWISE_AND prefixed_unary_expression
2564 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2568 pre_increment_expression
2569 : OP_INC prefixed_unary_expression
2571 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2572 (Expression) $2, lexer.Location);
2576 pre_decrement_expression
2577 : OP_DEC prefixed_unary_expression
2579 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2580 (Expression) $2, lexer.Location);
2584 multiplicative_expression
2585 : prefixed_unary_expression
2586 | multiplicative_expression STAR prefixed_unary_expression
2588 $$ = new Binary (Binary.Operator.Multiply,
2589 (Expression) $1, (Expression) $3, lexer.Location);
2591 | multiplicative_expression DIV prefixed_unary_expression
2593 $$ = new Binary (Binary.Operator.Division,
2594 (Expression) $1, (Expression) $3, lexer.Location);
2596 | multiplicative_expression PERCENT prefixed_unary_expression
2598 $$ = new Binary (Binary.Operator.Modulus,
2599 (Expression) $1, (Expression) $3, lexer.Location);
2604 : multiplicative_expression
2605 | additive_expression PLUS multiplicative_expression
2607 $$ = new Binary (Binary.Operator.Addition,
2608 (Expression) $1, (Expression) $3, lexer.Location);
2610 | additive_expression MINUS multiplicative_expression
2612 $$ = new Binary (Binary.Operator.Subtraction,
2613 (Expression) $1, (Expression) $3, lexer.Location);
2618 : additive_expression
2619 | shift_expression OP_SHIFT_LEFT additive_expression
2621 $$ = new Binary (Binary.Operator.LeftShift,
2622 (Expression) $1, (Expression) $3, lexer.Location);
2624 | shift_expression OP_SHIFT_RIGHT additive_expression
2626 $$ = new Binary (Binary.Operator.RightShift,
2627 (Expression) $1, (Expression) $3, lexer.Location);
2631 relational_expression
2633 | relational_expression OP_LT shift_expression
2635 $$ = new Binary (Binary.Operator.LessThan,
2636 (Expression) $1, (Expression) $3, lexer.Location);
2638 | relational_expression OP_GT shift_expression
2640 $$ = new Binary (Binary.Operator.GreaterThan,
2641 (Expression) $1, (Expression) $3, lexer.Location);
2643 | relational_expression OP_LE shift_expression
2645 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2646 (Expression) $1, (Expression) $3, lexer.Location);
2648 | relational_expression OP_GE shift_expression
2650 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2651 (Expression) $1, (Expression) $3, lexer.Location);
2653 | relational_expression IS type
2655 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
2657 | relational_expression AS type
2659 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
2664 : relational_expression
2665 | equality_expression OP_EQ relational_expression
2667 $$ = new Binary (Binary.Operator.Equality,
2668 (Expression) $1, (Expression) $3, lexer.Location);
2670 | equality_expression OP_NE relational_expression
2672 $$ = new Binary (Binary.Operator.Inequality,
2673 (Expression) $1, (Expression) $3, lexer.Location);
2678 : equality_expression
2679 | and_expression BITWISE_AND equality_expression
2681 $$ = new Binary (Binary.Operator.BitwiseAnd,
2682 (Expression) $1, (Expression) $3, lexer.Location);
2686 exclusive_or_expression
2688 | exclusive_or_expression CARRET and_expression
2690 $$ = new Binary (Binary.Operator.ExclusiveOr,
2691 (Expression) $1, (Expression) $3, lexer.Location);
2695 inclusive_or_expression
2696 : exclusive_or_expression
2697 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2699 $$ = new Binary (Binary.Operator.BitwiseOr,
2700 (Expression) $1, (Expression) $3, lexer.Location);
2704 conditional_and_expression
2705 : inclusive_or_expression
2706 | conditional_and_expression OP_AND inclusive_or_expression
2708 $$ = new Binary (Binary.Operator.LogicalAnd,
2709 (Expression) $1, (Expression) $3, lexer.Location);
2713 conditional_or_expression
2714 : conditional_and_expression
2715 | conditional_or_expression OP_OR conditional_and_expression
2717 $$ = new Binary (Binary.Operator.LogicalOr,
2718 (Expression) $1, (Expression) $3, lexer.Location);
2722 conditional_expression
2723 : conditional_or_expression
2724 | conditional_or_expression INTERR expression COLON expression
2726 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2730 assignment_expression
2731 : prefixed_unary_expression ASSIGN expression
2733 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2735 | prefixed_unary_expression OP_MULT_ASSIGN expression
2737 Location l = lexer.Location;
2739 $$ = new CompoundAssign (
2740 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
2742 | prefixed_unary_expression OP_DIV_ASSIGN expression
2744 Location l = lexer.Location;
2746 $$ = new CompoundAssign (
2747 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
2749 | prefixed_unary_expression OP_MOD_ASSIGN expression
2751 Location l = lexer.Location;
2753 $$ = new CompoundAssign (
2754 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
2756 | prefixed_unary_expression OP_ADD_ASSIGN expression
2758 Location l = lexer.Location;
2760 $$ = new CompoundAssign (
2761 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
2763 | prefixed_unary_expression OP_SUB_ASSIGN expression
2765 Location l = lexer.Location;
2767 $$ = new CompoundAssign (
2768 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
2770 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2772 Location l = lexer.Location;
2774 $$ = new CompoundAssign (
2775 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
2777 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2779 Location l = lexer.Location;
2781 $$ = new CompoundAssign (
2782 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
2784 | prefixed_unary_expression OP_AND_ASSIGN expression
2786 Location l = lexer.Location;
2788 $$ = new CompoundAssign (
2789 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
2791 | prefixed_unary_expression OP_OR_ASSIGN expression
2793 Location l = lexer.Location;
2795 $$ = new CompoundAssign (
2796 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
2798 | prefixed_unary_expression OP_XOR_ASSIGN expression
2800 Location l = lexer.Location;
2802 $$ = new CompoundAssign (
2803 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
2808 : conditional_expression
2809 | assignment_expression
2831 name = MakeName ((string) $4);
2833 new_class = new Class (current_namespace, current_container, name, (int) $2,
2834 (Attributes) $1, lexer.Location);
2835 current_container = new_class;
2836 RootContext.Tree.RecordDecl (name, new_class);
2842 Class new_class = (Class) current_container;
2845 new_class.Bases = (ArrayList) $6;
2847 current_container = current_container.Parent;
2848 CheckDef (current_container.AddClass (new_class), new_class.Name, new_class.Location);
2855 : /* empty */ { $$ = (int) 0; }
2861 | modifiers modifier
2866 if ((m1 & m2) != 0) {
2867 Location l = lexer.Location;
2868 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2870 $$ = (int) (m1 | m2);
2875 : NEW { $$ = Modifiers.NEW; }
2876 | PUBLIC { $$ = Modifiers.PUBLIC; }
2877 | PROTECTED { $$ = Modifiers.PROTECTED; }
2878 | INTERNAL { $$ = Modifiers.INTERNAL; }
2879 | PRIVATE { $$ = Modifiers.PRIVATE; }
2880 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2881 | SEALED { $$ = Modifiers.SEALED; }
2882 | STATIC { $$ = Modifiers.STATIC; }
2883 | READONLY { $$ = Modifiers.READONLY; }
2884 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2885 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2886 | EXTERN { $$ = Modifiers.EXTERN; }
2887 | VOLATILE { $$ = Modifiers.VOLATILE; }
2888 | UNSAFE { $$ = Modifiers.UNSAFE; }
2892 : /* empty */ { $$ = null; }
2893 | class_base { $$ = $1; }
2897 : COLON type_list { $$ = $2; }
2905 // A block is "contained" on the following places:
2907 // property_declaration as part of the accessor body (get/set)
2908 // operator_declaration
2909 // constructor_declaration
2910 // destructor_declaration
2911 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2916 current_block = new Block (current_block, current_local_parameters,
2917 lexer.Location, Location.Null);
2919 opt_statement_list CLOSE_BRACE
2921 while (current_block.Implicit)
2922 current_block = current_block.Parent;
2924 current_block.SetEndLocation (lexer.Location);
2925 current_block = current_block.Parent;
2936 | statement_list statement
2940 : declaration_statement
2942 if ($1 != null && (Block) $1 != current_block){
2943 current_block.AddStatement ((Statement) $1);
2944 current_block = (Block) $1;
2947 | embedded_statement
2949 current_block.AddStatement ((Statement) $1);
2957 | expression_statement
2958 | selection_statement
2959 | iteration_statement
2963 | unchecked_statement
2973 $$ = EmptyStatement.Value;
2980 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
2982 if (!current_block.AddLabel ((string) $1, labeled)){
2983 Location l = lexer.Location;
2984 Report.Error (140, l, "The label '" + ((string) $1) + "' is a duplicate");
2986 current_block.AddStatement (labeled);
2991 declaration_statement
2992 : local_variable_declaration SEMICOLON
2995 DictionaryEntry de = (DictionaryEntry) $1;
2997 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3001 | local_constant_declaration SEMICOLON
3004 DictionaryEntry de = (DictionaryEntry) $1;
3006 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3012 * The following is from Rhys' grammar:
3013 * > Types in local variable declarations must be recognized as
3014 * > expressions to prevent reduce/reduce errors in the grammar.
3015 * > The expressions are converted into types during semantic analysis.
3018 : primary_expression opt_rank_specifier
3020 // FIXME: Do something smart here regarding the composition of the type.
3022 // Ok, the above "primary_expression" is there to get rid of
3023 // both reduce/reduce and shift/reduces in the grammar, it should
3024 // really just be "type_name". If you use type_name, a reduce/reduce
3025 // creeps up. If you use qualified_identifier (which is all we need
3026 // really) two shift/reduces appear.
3029 // So the super-trick is that primary_expression
3030 // can only be either a SimpleName or a MemberAccess.
3031 // The MemberAccess case arises when you have a fully qualified type-name like :
3033 // SimpleName is when you have
3036 Expression expr = (Expression) $1;
3037 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3038 Error_ExpectingTypeName (lexer.Location, expr);
3042 // So we extract the string corresponding to the SimpleName
3046 if ((string) $2 == "")
3049 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3052 | builtin_types opt_rank_specifier
3054 if ((string) $2 == "")
3057 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3061 local_variable_pointer_type
3062 : primary_expression STAR
3064 Expression expr = (Expression) $1;
3065 Location l = lexer.Location;
3067 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3068 Error_ExpectingTypeName (l, expr);
3072 $$ = new ComposedCast ((Expression) $1, "*", l);
3074 | builtin_types STAR
3076 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3080 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3082 | local_variable_pointer_type STAR
3084 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3088 local_variable_declaration
3089 : local_variable_type variable_declarators
3092 $$ = new DictionaryEntry ($1, $2);
3096 | local_variable_pointer_type opt_rank_specifier variable_declarators
3101 if ((string) $2 == "")
3102 t = (Expression) $1;
3104 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3105 $$ = new DictionaryEntry (t, $3);
3111 local_constant_declaration
3112 : CONST local_variable_type constant_declarators
3115 $$ = new DictionaryEntry ($2, $3);
3121 expression_statement
3122 : statement_expression SEMICOLON
3129 // We have to do the wrapping here and not in the case above,
3130 // because statement_expression is used for example in for_statement
3132 statement_expression
3133 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3134 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3135 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3136 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3137 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3138 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3139 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3141 Report.Error (1002, lexer.Location, "Expecting `;'");
3146 object_creation_expression
3147 : object_or_delegate_creation_expression
3148 { note ("complain if this is a delegate maybe?"); }
3157 : if_statement_open if_statement_rest
3166 oob_stack.Push (lexer.Location);
3171 : boolean_expression CLOSE_PARENS
3174 Location l = (Location) oob_stack.Pop ();
3176 $$ = new If ((Expression) $1, (Statement) $3, l);
3178 if (RootContext.WarningLevel >= 3){
3179 if ($3 == EmptyStatement.Value)
3180 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3184 | boolean_expression CLOSE_PARENS
3185 embedded_statement ELSE embedded_statement
3187 Location l = (Location) oob_stack.Pop ();
3189 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
3194 : SWITCH OPEN_PARENS
3196 oob_stack.Push (lexer.Location);
3197 switch_stack.Push (current_block);
3199 expression CLOSE_PARENS
3202 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
3203 current_block = (Block) switch_stack.Pop ();
3219 Report.Error (1522, lexer.Location, "Empty switch block");
3227 ArrayList sections = new ArrayList (4);
3232 | switch_sections switch_section
3234 ArrayList sections = (ArrayList) $1;
3244 current_block = current_block.CreateSwitchBlock (lexer.Location);
3248 Block topmost = current_block;
3250 while (topmost.Implicit)
3251 topmost = topmost.Parent;
3252 $$ = new SwitchSection ((ArrayList) $1, topmost);
3259 ArrayList labels = new ArrayList (4);
3264 | switch_labels switch_label
3266 ArrayList labels = (ArrayList) ($1);
3274 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3275 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3278 1523, lexer.Location,
3279 "The keyword case or default must precede code in switch block");
3293 oob_stack.Push (lexer.Location);
3295 boolean_expression CLOSE_PARENS embedded_statement
3297 Location l = (Location) oob_stack.Pop ();
3298 $$ = new While ((Expression) $4, (Statement) $6, l);
3300 if (RootContext.WarningLevel >= 3){
3301 if ($6 == EmptyStatement.Value)
3302 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3308 : DO embedded_statement
3310 oob_stack.Push (lexer.Location);
3312 boolean_expression CLOSE_PARENS SEMICOLON
3314 Location l = (Location) oob_stack.Pop ();
3316 $$ = new Do ((Statement) $2, (Expression) $6, l);
3322 opt_for_initializer SEMICOLON
3324 Block assign_block = new Block (current_block);
3325 current_block = assign_block;
3327 if ($3 is DictionaryEntry){
3328 DictionaryEntry de = (DictionaryEntry) $3;
3330 Expression type = (Expression) de.Key;
3331 ArrayList var_declarators = (ArrayList) de.Value;
3333 foreach (VariableDeclaration decl in var_declarators){
3337 vi = current_block.AddVariable (
3338 type, decl.identifier, current_local_parameters, decl.Location);
3342 Location l = lexer.Location;
3344 if (decl.expression_or_array_initializer is Expression){
3345 expr = (Expression) decl.expression_or_array_initializer;
3346 } else if (decl.expression_or_array_initializer == null) {
3349 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3350 expr = new ArrayCreation (type, "", init, decl.Location);
3353 LocalVariableReference var;
3354 var = new LocalVariableReference (assign_block, decl.identifier, l);
3357 Assign a = new Assign (var, expr, decl.Location);
3359 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3365 oob_stack.Push (lexer.Location);
3367 opt_for_condition SEMICOLON
3368 opt_for_iterator CLOSE_PARENS
3371 Location l = (Location) oob_stack.Pop ();
3373 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3375 if (RootContext.WarningLevel >= 3){
3376 if ($10 == EmptyStatement.Value)
3377 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3380 current_block.AddStatement (f);
3381 while (current_block.Implicit)
3382 current_block = current_block.Parent;
3384 current_block = current_block.Parent;
3389 : /* empty */ { $$ = EmptyStatement.Value; }
3394 : local_variable_declaration
3395 | statement_expression_list
3399 : /* empty */ { $$ = null; }
3400 | boolean_expression
3404 : /* empty */ { $$ = EmptyStatement.Value; }
3409 : statement_expression_list
3412 statement_expression_list
3413 : statement_expression
3415 // CHANGE: was `null'
3416 Block b = new Block (current_block, Block.Flags.Implicit);
3418 b.AddStatement ((Statement) $1);
3421 | statement_expression_list COMMA statement_expression
3423 Block b = (Block) $1;
3425 b.AddStatement ((Statement) $3);
3431 : FOREACH OPEN_PARENS type IDENTIFIER IN
3433 oob_stack.Push (lexer.Location);
3435 expression CLOSE_PARENS
3437 oob_stack.Push (current_block);
3439 Block foreach_block = new Block (current_block, Block.Flags.Implicit);
3440 LocalVariableReference v = null;
3441 Location l = lexer.Location;
3444 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3448 // Get a writable reference to this read-only variable.
3449 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3451 current_block = foreach_block;
3454 oob_stack.Push (current_block);
3458 Block foreach_block = (Block) oob_stack.Pop ();
3459 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3460 Block prev_block = (Block) oob_stack.Pop ();
3461 Location l = (Location) oob_stack.Pop ();
3463 current_block = prev_block;
3466 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3467 foreach_block.AddStatement (f);
3476 | continue_statement
3486 $$ = new Break (lexer.Location);
3491 : CONTINUE SEMICOLON
3493 $$ = new Continue (lexer.Location);
3498 : GOTO IDENTIFIER SEMICOLON
3500 $$ = new Goto (current_block, (string) $2, lexer.Location);
3502 | GOTO CASE constant_expression SEMICOLON
3504 $$ = new GotoCase ((Expression) $3, lexer.Location);
3506 | GOTO DEFAULT SEMICOLON
3508 $$ = new GotoDefault (lexer.Location);
3513 : RETURN opt_expression SEMICOLON
3515 $$ = new Return ((Expression) $2, lexer.Location);
3520 : THROW opt_expression SEMICOLON
3522 $$ = new Throw ((Expression) $2, lexer.Location);
3527 : YIELD RETURN expression SEMICOLON
3529 if (iterator_container == null){
3530 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3533 iterator_container.SetYields ();
3534 $$ = new Yield ((Expression) $3, lexer.Location);
3537 | YIELD expression SEMICOLON
3539 if (iterator_container == null){
3540 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3543 iterator_container.SetYields ();
3544 $$ = new Yield ((Expression) $2, lexer.Location);
3547 | YIELD BREAK SEMICOLON
3549 if (iterator_container == null){
3550 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3553 iterator_container.SetYields ();
3554 $$ = new YieldBreak (lexer.Location);
3565 : TRY block catch_clauses
3568 ArrayList s = new ArrayList (4);
3570 foreach (Catch cc in (ArrayList) $3) {
3577 // Now s contains the list of specific catch clauses
3578 // and g contains the general one.
3580 $$ = new Try ((Block) $2, s, g, null, lexer.Location);
3582 | TRY block opt_catch_clauses FINALLY block
3585 ArrayList s = new ArrayList (4);
3586 ArrayList catch_list = (ArrayList) $3;
3588 if (catch_list != null){
3589 foreach (Catch cc in catch_list) {
3597 $$ = new Try ((Block) $2, s, g, (Block) $5, lexer.Location);
3601 Report.Error (1524, lexer.Location, "Expected catch or finally");
3606 : /* empty */ { $$ = null; }
3613 ArrayList l = new ArrayList (4);
3618 | catch_clauses catch_clause
3620 ArrayList l = (ArrayList) $1;
3628 : /* empty */ { $$ = null; }
3633 : CATCH opt_catch_args
3635 Expression type = null;
3639 DictionaryEntry cc = (DictionaryEntry) $2;
3640 type = (Expression) cc.Key;
3641 id = (string) cc.Value;
3644 ArrayList one = new ArrayList (4);
3645 Location loc = lexer.Location;
3647 one.Add (new VariableDeclaration (id, null, loc));
3650 current_block = new Block (current_block);
3651 Block b = declare_local_variables (type, one, loc);
3656 Expression type = null;
3660 DictionaryEntry cc = (DictionaryEntry) $2;
3661 type = (Expression) cc.Key;
3662 id = (string) cc.Value;
3666 // FIXME: I can change this for an assignment.
3668 while (current_block != (Block) $1)
3669 current_block = current_block.Parent;
3674 $$ = new Catch (type, id , (Block) $4, lexer.Location);
3679 : /* empty */ { $$ = null; }
3684 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3686 $$ = new DictionaryEntry ($2, $3);
3693 $$ = new Checked ((Block) $2);
3700 $$ = new Unchecked ((Block) $2);
3707 if (!RootContext.Unsafe){
3708 Report.Error (227, lexer.Location,
3709 "Unsafe code can only be used if --unsafe is used");
3712 $$ = new Unsafe ((Block) $3);
3718 type fixed_pointer_declarators
3721 Block assign_block = new Block (current_block, Block.Flags.Implicit);
3722 ArrayList list = (ArrayList) $4;
3723 Expression type = (Expression) $3;
3724 Location l = lexer.Location;
3725 int top = list.Count;
3727 for (int i = 0; i < top; i++){
3728 Pair p = (Pair) list [i];
3731 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3738 current_block.AddStatement (assign_block);
3739 current_block = assign_block;
3740 oob_stack.Push (assign_block);
3745 Location l = (Location) oob_stack.Pop ();
3748 $$ = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
3752 fixed_pointer_declarators
3753 : fixed_pointer_declarator {
3754 ArrayList declarators = new ArrayList (4);
3755 declarators.Add ($1);
3758 | fixed_pointer_declarators COMMA fixed_pointer_declarator
3760 ArrayList declarators = (ArrayList) $1;
3761 declarators.Add ($3);
3766 fixed_pointer_declarator
3767 : IDENTIFIER ASSIGN expression
3769 $$ = new Pair ($1, $3);
3774 : LOCK OPEN_PARENS expression CLOSE_PARENS
3780 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
3785 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
3787 Block assign_block = new Block (current_block);
3788 current_block = assign_block;
3790 oob_stack.Push (lexer.Location);
3792 if ($3 is DictionaryEntry){
3793 DictionaryEntry de = (DictionaryEntry) $3;
3794 Location l = lexer.Location;
3796 Expression type = (Expression) de.Key;
3797 ArrayList var_declarators = (ArrayList) de.Value;
3799 ArrayList vars = new ArrayList (4);
3801 foreach (VariableDeclaration decl in var_declarators){
3803 LocalInfo vi = current_block.AddVariable (
3804 type, decl.identifier,
3805 current_local_parameters, decl.Location);
3811 if (decl.expression_or_array_initializer is Expression){
3812 expr = (Expression) decl.expression_or_array_initializer;
3814 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3816 expr = new ArrayCreation (type, "", init, decl.Location);
3819 LocalVariableReference var;
3821 // Get a writable reference to this read-only variable.
3822 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
3824 // This is so that it is not a warning on using variables
3827 vars.Add (new DictionaryEntry (var, expr));
3829 // Assign a = new Assign (var, expr, decl.Location);
3830 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3832 $3 = new DictionaryEntry (type, vars);
3837 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
3838 current_block.AddStatement (u);
3839 while (current_block.Implicit)
3840 current_block = current_block.Parent;
3842 current_block = current_block.Parent;
3846 resource_acquisition
3847 : local_variable_declaration
3854 // A class used to pass around variable declarations and constants
3856 public class VariableDeclaration {
3857 public string identifier;
3858 public object expression_or_array_initializer;
3859 public Location Location;
3860 public Attributes OptAttributes;
3862 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
3864 this.identifier = id;
3865 this.expression_or_array_initializer = eoai;
3867 this.OptAttributes = opt_attrs;
3870 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
3876 // A class used to hold info about an indexer declarator
3878 public class IndexerDeclaration {
3879 public Expression type;
3880 public string interface_type;
3881 public Parameters param_list;
3883 public IndexerDeclaration (Expression type, string interface_type, Parameters param_list)
3886 this.interface_type = interface_type;
3887 this.param_list = param_list;
3892 // A class used to hold info about an operator declarator
3894 public class OperatorDeclaration {
3895 public Operator.OpType optype;
3896 public Expression ret_type, arg1type, arg2type;
3897 public string arg1name, arg2name;
3898 public Location location;
3900 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
3901 Expression arg1type, string arg1name,
3902 Expression arg2type, string arg2name, Location location)
3905 this.ret_type = ret_type;
3906 this.arg1type = arg1type;
3907 this.arg1name = arg1name;
3908 this.arg2type = arg2type;
3909 this.arg2name = arg2name;
3910 this.location = location;
3915 public class QualifiedIdentifier {
3917 public QualifiedIdentifier prev;
3919 public QualifiedIdentifier (string s)
3925 public QualifiedIdentifier (QualifiedIdentifier prev, string s)
3931 public override string ToString ()
3938 if (prev.prev == null)
3939 return prev.s + "." + s;
3940 if (prev.prev.prev == null)
3941 return prev.prev.s + "." + prev.s + "." + s;
3944 QualifiedIdentifier ptr = this.prev;
3946 while (ptr != null){
3950 string [] ss = new string [count];
3952 for (int i = count; i-- > 0;){
3956 return String.Join (".", ss, 0, ss.Length);
3960 void Error_ExpectingTypeName (Location l, Expression expr)
3962 if (expr is Invocation){
3963 Report.Error (1002, l, "; expected");
3965 Report.Error (-1, l, "Invalid Type definition");
3970 // Given the @class_name name, it creates a fully qualified name
3971 // based on the containing declaration space
3974 MakeName (string class_name)
3976 string ns = current_namespace.FullName;
3977 string container_name = current_container.Name;
3979 if (container_name == ""){
3981 return ns + "." + class_name;
3985 return container_name + "." + class_name;
3989 // Used to report back to the user the result of a declaration
3990 // in the current declaration space
3993 CheckDef (DeclSpace.AdditionResult result, string name, Location l)
3995 if (result == DeclSpace.AdditionResult.Success)
3999 case DeclSpace.AdditionResult.NameExists:
4000 Report.Error (102, l, "The container `" + current_container.Name +
4001 "' already contains a definition for `"+
4007 // This is handled only for static Constructors, because
4008 // in reality we handle these by the semantic analysis later
4010 case DeclSpace.AdditionResult.MethodExists:
4012 111, l, "Class `"+current_container.Name+
4013 "' already defines a member called '" +
4014 name + "' with the same parameter types (more than one default constructor)");
4017 case DeclSpace.AdditionResult.EnclosingClash:
4018 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
4021 case DeclSpace.AdditionResult.NotAConstructor:
4022 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
4025 case DeclSpace.AdditionResult.Error:
4026 // Error has already been reported.
4032 CheckDef (bool result, string name, Location l)
4036 CheckDef (DeclSpace.AdditionResult.NameExists, name, l);
4039 Expression DecomposeQI (QualifiedIdentifier qi, Location loc)
4043 if (qi.prev == null)
4044 return new SimpleName (qi.s, loc);
4046 o = DecomposeQI (qi.prev, loc);
4047 return new MemberAccess (o, qi.s, loc);
4052 // This method is used to get at the complete string representation of
4053 // a fully-qualified type name, hiding inside a MemberAccess ;-)
4054 // This is necessary because local_variable_type admits primary_expression
4055 // as the type of the variable. So we do some extra checking
4058 string GetQualifiedIdentifier (Expression expr)
4060 if (expr is SimpleName)
4061 return ((SimpleName)expr).Name;
4062 else if (expr is MemberAccess)
4063 return GetQualifiedIdentifier (((MemberAccess)expr).Expr) + "." + ((MemberAccess) expr).Identifier;
4065 throw new Exception ("Expr has to be either SimpleName or MemberAccess! (" + expr + ")");
4070 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4072 Block implicit_block;
4073 ArrayList inits = null;
4076 // We use the `Used' property to check whether statements
4077 // have been added to the current block. If so, we need
4078 // to create another block to contain the new declaration
4079 // otherwise, as an optimization, we use the same block to
4080 // add the declaration.
4082 // FIXME: A further optimization is to check if the statements
4083 // that were added were added as part of the initialization
4084 // below. In which case, no other statements have been executed
4085 // and we might be able to reduce the number of blocks for
4086 // situations like this:
4088 // int j = 1; int k = j + 1;
4090 if (current_block.Used) {
4091 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4092 implicit_block.AddChildVariableNames (current_block);
4094 implicit_block = current_block;
4096 foreach (VariableDeclaration decl in variable_declarators){
4098 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4099 if (decl.expression_or_array_initializer != null){
4101 inits = new ArrayList (4);
4108 return implicit_block;
4110 foreach (VariableDeclaration decl in inits){
4114 if (decl.expression_or_array_initializer is Expression){
4115 expr = (Expression) decl.expression_or_array_initializer;
4118 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4120 expr = new ArrayCreation (type, "", init, decl.Location);
4123 LocalVariableReference var;
4124 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4126 assign = new Assign (var, expr, decl.Location);
4128 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4131 return implicit_block;
4134 Block declare_local_constants (Expression type, ArrayList declarators)
4136 Block implicit_block;
4138 if (current_block.Used)
4139 implicit_block = new Block (current_block, Block.Flags.Implicit);
4141 implicit_block = current_block;
4143 foreach (VariableDeclaration decl in declarators){
4144 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4145 current_local_parameters, decl.Location);
4148 return implicit_block;
4151 void CheckAttributeTarget (string a)
4155 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
4159 Location l = lexer.Location;
4160 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4166 void CheckUnaryOperator (Operator.OpType op)
4170 case Operator.OpType.LogicalNot:
4171 case Operator.OpType.OnesComplement:
4172 case Operator.OpType.Increment:
4173 case Operator.OpType.Decrement:
4174 case Operator.OpType.True:
4175 case Operator.OpType.False:
4176 case Operator.OpType.Addition:
4177 case Operator.OpType.Subtraction:
4182 Location l = lexer.Location;
4183 Report.Error (1019, l, "Overloadable unary operator expected");
4189 void CheckBinaryOperator (Operator.OpType op)
4193 case Operator.OpType.Addition:
4194 case Operator.OpType.Subtraction:
4195 case Operator.OpType.Multiply:
4196 case Operator.OpType.Division:
4197 case Operator.OpType.Modulus:
4198 case Operator.OpType.BitwiseAnd:
4199 case Operator.OpType.BitwiseOr:
4200 case Operator.OpType.ExclusiveOr:
4201 case Operator.OpType.LeftShift:
4202 case Operator.OpType.RightShift:
4203 case Operator.OpType.Equality:
4204 case Operator.OpType.Inequality:
4205 case Operator.OpType.GreaterThan:
4206 case Operator.OpType.LessThan:
4207 case Operator.OpType.GreaterThanOrEqual:
4208 case Operator.OpType.LessThanOrEqual:
4212 Location l = lexer.Location;
4213 Report.Error (1020, l, "Overloadable binary operator expected");
4219 void syntax_error (Location l, string msg)
4221 Report.Error (1003, l, "Syntax error, " + msg);
4224 void output (string s)
4226 Console.WriteLine (s);
4229 void note (string s)
4231 // Used to put annotations
4236 public Tokenizer Lexer {
4242 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4244 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4245 this.name = file.Name;
4247 current_container = RootContext.Tree.Types;
4248 current_container.NamespaceEntry = current_namespace;
4249 oob_stack = new Stack ();
4250 switch_stack = new Stack ();
4252 lexer = new Tokenizer (reader, file, defines);
4255 public override void parse ()
4258 if (yacc_verbose_flag)
4259 yyparse (lexer, new yydebug.yyDebugSimple ());
4262 Tokenizer tokenizer = lexer as Tokenizer;
4263 tokenizer.cleanup ();
4264 } catch (Exception e){
4265 // Please do not remove this, it is used during debugging
4268 Console.WriteLine (e);
4269 Report.Error (-25, lexer.Location, "Parsing error");
4270 if (Driver.parser_verbose)
4271 Console.WriteLine (e);
4275 void CheckToken (int error, int yyToken, string msg)
4277 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4278 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4281 Report.Error (error, lexer.Location, msg);
4284 void CheckIdentifierToken (int yyToken)
4286 CheckToken (1041, yyToken, "Identifier expected");