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;
431 | interface_declaration
433 | delegate_declaration
435 // Enable this when we have handled all errors, because this acts as a generic fallback
438 // Console.WriteLine ("Token=" + yyToken);
439 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
449 | attribute_sections { $$ = $1; }
455 AttributeSection sect = (AttributeSection) $1;
457 if (sect.Target == "assembly")
458 RootContext.AddGlobalAttributeSection (current_container, sect);
461 $$ = new Attributes ((AttributeSection) $1);
463 | attribute_sections attribute_section
465 Attributes attrs = null;
466 AttributeSection sect = (AttributeSection) $2;
468 if (sect.Target == "assembly")
469 RootContext.AddGlobalAttributeSection (current_container, sect);
472 attrs = (Attributes) $1;
473 attrs.AddAttributeSection (sect);
481 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
483 string target = null;
486 target = (string) $2;
488 $$ = new AttributeSection (target, (ArrayList) $3);
490 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
492 $$ = new AttributeSection (null, (ArrayList) $2);
496 attribute_target_specifier
497 : attribute_target COLON
506 CheckAttributeTarget ((string) $1);
509 | EVENT { $$ = "event"; }
510 | RETURN { $$ = "return"; }
516 ArrayList attrs = new ArrayList (4);
522 | attribute_list COMMA attribute
524 ArrayList attrs = (ArrayList) $1;
536 opt_attribute_arguments
538 $$ = new Attribute ((string) ($1).ToString (), (ArrayList) $3, (Location) $2);
543 : type_name { /* reserved attribute name or identifier: 17.4 */ }
546 opt_attribute_arguments
547 : /* empty */ { $$ = null; }
548 | OPEN_PARENS attribute_arguments CLOSE_PARENS
556 : opt_positional_argument_list
561 ArrayList args = new ArrayList (4);
567 | positional_argument_list COMMA named_argument_list
569 ArrayList args = new ArrayList (4);
575 | named_argument_list
577 ArrayList args = new ArrayList (4);
586 opt_positional_argument_list
587 : /* empty */ { $$ = null; }
588 | positional_argument_list
591 positional_argument_list
594 ArrayList args = new ArrayList (4);
595 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
599 | positional_argument_list COMMA expression
601 ArrayList args = (ArrayList) $1;
602 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
611 ArrayList args = new ArrayList (4);
616 | named_argument_list COMMA named_argument
618 ArrayList args = (ArrayList) $1;
626 : IDENTIFIER ASSIGN expression
628 $$ = new DictionaryEntry (
630 new Argument ((Expression) $3, Argument.AType.Expression));
636 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
639 opt_class_member_declarations
641 | class_member_declarations
644 class_member_declarations
645 : class_member_declaration
646 | class_member_declarations
647 class_member_declaration
650 class_member_declaration
651 : constant_declaration // done
652 | field_declaration // done
653 | method_declaration // done
654 | property_declaration // done
655 | event_declaration // done
656 | indexer_declaration // done
657 | operator_declaration // done
658 | constructor_declaration // done
659 | destructor_declaration // done
669 string full_struct_name = MakeName ((string) $4);
671 new_struct = new Struct (current_namespace, current_container, full_struct_name,
672 (int) $2, (Attributes) $1, lexer.Location);
673 current_container = new_struct;
674 RootContext.Tree.RecordDecl (full_struct_name, new_struct);
680 Struct new_struct = (Struct) current_container;
683 new_struct.Bases = (ArrayList) $6;
685 current_container = current_container.Parent;
686 CheckDef (current_container.AddStruct (new_struct), new_struct.Name, new_struct.Location);
689 | opt_attributes opt_modifiers STRUCT error {
690 CheckIdentifierToken (yyToken);
695 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
698 opt_struct_member_declarations
700 | struct_member_declarations
703 struct_member_declarations
704 : struct_member_declaration
705 | struct_member_declarations struct_member_declaration
708 struct_member_declaration
709 : constant_declaration
712 | property_declaration
714 | indexer_declaration
715 | operator_declaration
716 | constructor_declaration
720 * This is only included so we can flag error 575:
721 * destructors only allowed on class types
723 | destructor_declaration
734 foreach (VariableDeclaration constant in (ArrayList) $5){
735 Location l = constant.Location;
737 Const c = new Const (
738 (Expression) $4, (string) constant.identifier,
739 (Expression) constant.expression_or_array_initializer, (int) $2,
742 CheckDef (current_container.AddConstant (c), c.Name, l);
748 : constant_declarator
750 ArrayList constants = new ArrayList (4);
754 | constant_declarators COMMA constant_declarator
756 ArrayList constants = (ArrayList) $1;
763 : IDENTIFIER ASSIGN constant_expression
765 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
776 Expression type = (Expression) $3;
779 foreach (VariableDeclaration var in (ArrayList) $4){
780 Location l = var.Location;
782 Field field = new Field (type, mod, var.identifier,
783 var.expression_or_array_initializer,
786 CheckDef (current_container.AddField (field), field.Name, l);
794 Report.Error (670, lexer.Location, "void type is not allowed for fields");
799 : variable_declarator
801 ArrayList decl = new ArrayList (4);
805 | variable_declarators COMMA variable_declarator
807 ArrayList decls = (ArrayList) $1;
814 : IDENTIFIER ASSIGN variable_initializer
816 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
820 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
833 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
835 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
841 iterator_container = (IIteratorContainer) $1;
845 Method method = (Method) $1;
846 Block b = (Block) $3;
847 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
850 if ((method.ModFlags & extern_abstract) == 0){
852 501, lexer.Location, current_container.MakeName (method.Name) +
853 "must declare a body because it is not marked abstract or extern");
856 if ((method.ModFlags & Modifiers.EXTERN) != 0){
858 179, lexer.Location, current_container.MakeName (method.Name) +
859 " is declared extern, but has a body");
863 method.Block = (Block) $3;
864 CheckDef (current_container.AddMethod (method), method.Name, method.Location);
866 current_local_parameters = null;
867 iterator_container = null;
881 1585, lexer.Location, "Member modifier `" +
882 Modifiers.Name (i) + "' must precede member type and name");
895 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
897 Method method = new Method (current_container, (Expression) $3, (int) $2, (string) $4,
898 (Parameters) $6, (Attributes) $1, lexer.Location);
900 current_local_parameters = (Parameters) $6;
908 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
910 Method method = new Method (current_container, TypeManager.system_void_expr, (int) $2,
911 (string) $4, (Parameters) $6, (Attributes) $1,
914 current_local_parameters = (Parameters) $6;
921 | SEMICOLON { $$ = null; }
924 opt_formal_parameter_list
925 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
926 | formal_parameter_list
929 formal_parameter_list
932 ArrayList pars_list = (ArrayList) $1;
934 Parameter [] pars = new Parameter [pars_list.Count];
935 pars_list.CopyTo (pars);
937 $$ = new Parameters (pars, null, lexer.Location);
939 | fixed_parameters COMMA parameter_array
941 ArrayList pars_list = (ArrayList) $1;
943 Parameter [] pars = new Parameter [pars_list.Count];
944 pars_list.CopyTo (pars);
946 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
950 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
957 ArrayList pars = new ArrayList (4);
962 | fixed_parameters COMMA fixed_parameter
964 ArrayList pars = (ArrayList) $1;
973 opt_parameter_modifier
977 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
980 opt_parameter_modifier
983 CheckIdentifierToken (yyToken);
988 opt_parameter_modifier
989 : /* empty */ { $$ = Parameter.Modifier.NONE; }
994 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
995 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
999 : opt_attributes PARAMS type IDENTIFIER
1001 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1002 note ("type must be a single-dimension array type");
1004 | opt_attributes PARAMS type error {
1005 CheckIdentifierToken (yyToken);
1011 : qualified_identifier { $$ = ($1).ToString (); }
1014 property_declaration
1020 implicit_value_parameter_type = (Expression) $3;
1022 lexer.PropertyParsing = true;
1024 $$ = lexer.Location;
1026 accessor_declarations
1028 lexer.PropertyParsing = false;
1033 Pair pair = (Pair) $7;
1034 Accessor get_block = (Accessor) pair.First;
1035 Accessor set_block = (Accessor) pair.Second;
1037 Location loc = (Location) $6;
1038 prop = new Property (current_container, (Expression) $3, (string) $4, (int) $2,
1039 get_block, set_block, (Attributes) $1, loc);
1041 CheckDef (current_container.AddProperty (prop), prop.Name, loc);
1042 implicit_value_parameter_type = null;
1046 accessor_declarations
1047 : get_accessor_declaration opt_set_accessor_declaration
1049 $$ = new Pair ($1, $2);
1051 | set_accessor_declaration opt_get_accessor_declaration
1053 $$ = new Pair ($2, $1);
1057 opt_get_accessor_declaration
1058 : /* empty */ { $$ = null; }
1059 | get_accessor_declaration
1062 opt_set_accessor_declaration
1063 : /* empty */ { $$ = null; }
1064 | set_accessor_declaration
1067 get_accessor_declaration
1068 : opt_attributes GET
1070 // If this is not the case, then current_local_parameters has already
1071 // been set in indexer_declaration
1072 if (parsing_indexer == false)
1073 current_local_parameters = null;
1075 current_local_parameters = indexer_parameters;
1076 lexer.PropertyParsing = false;
1080 $$ = new Accessor ((Block) $4, (Attributes) $1);
1081 current_local_parameters = null;
1082 lexer.PropertyParsing = true;
1086 set_accessor_declaration
1087 : opt_attributes SET
1090 Parameter implicit_value_parameter = new Parameter (
1091 implicit_value_parameter_type, "value",
1092 Parameter.Modifier.NONE, null);
1094 if (parsing_indexer == false) {
1095 args = new Parameter [1];
1096 args [0] = implicit_value_parameter;
1097 current_local_parameters = new Parameters (args, null, lexer.Location);
1099 Parameter [] fpars = indexer_parameters.FixedParameters;
1102 int count = fpars.Length;
1104 args = new Parameter [count + 1];
1105 fpars.CopyTo (args, 0);
1106 args [count] = implicit_value_parameter;
1109 current_local_parameters = new Parameters (
1110 args, indexer_parameters.ArrayParameter, lexer.Location);
1113 lexer.PropertyParsing = false;
1117 $$ = new Accessor ((Block) $4, (Attributes) $1);
1118 current_local_parameters = null;
1119 lexer.PropertyParsing = true;
1125 | SEMICOLON { $$ = null; }
1128 interface_declaration
1131 INTERFACE IDENTIFIER
1133 Interface new_interface;
1134 string full_interface_name = MakeName ((string) $4);
1136 new_interface = new Interface (current_namespace, current_container, full_interface_name,
1137 (int) $2, (Attributes) $1, lexer.Location);
1138 if (current_interface != null) {
1139 Location l = lexer.Location;
1140 Report.Error (-2, l, "Internal compiler error: interface inside interface");
1142 current_interface = new_interface;
1143 RootContext.Tree.RecordDecl (full_interface_name, new_interface);
1146 interface_body opt_semicolon
1148 Interface new_interface = (Interface) current_interface;
1151 new_interface.Bases = (ArrayList) $6;
1153 current_interface = null;
1154 CheckDef (current_container.AddInterface (new_interface),
1155 new_interface.Name, new_interface.Location);
1157 | opt_attributes opt_modifiers INTERFACE error {
1158 CheckIdentifierToken (yyToken);
1163 : /* empty */ { $$ = null; }
1168 : COLON interface_type_list { $$ = $2; }
1174 ArrayList interfaces = new ArrayList (4);
1176 interfaces.Add (($1).ToString ());
1179 | interface_type_list COMMA interface_type
1181 ArrayList interfaces = (ArrayList) $1;
1182 interfaces.Add (($3).ToString ());
1189 opt_interface_member_declarations
1193 opt_interface_member_declarations
1195 | interface_member_declarations
1198 interface_member_declarations
1199 : interface_member_declaration
1200 | interface_member_declarations interface_member_declaration
1203 interface_member_declaration
1204 : interface_method_declaration
1206 InterfaceMethod m = (InterfaceMethod) $1;
1208 CheckDef (current_interface.AddMethod (m), m.Name, m.Location);
1210 | interface_property_declaration
1212 InterfaceProperty p = (InterfaceProperty) $1;
1214 CheckDef (current_interface.AddProperty (p), p.Name, p.Location);
1216 | interface_event_declaration
1218 InterfaceEvent e = (InterfaceEvent) $1;
1220 CheckDef (current_interface.AddEvent (e), e.Name, lexer.Location);
1222 | interface_indexer_declaration
1224 InterfaceIndexer i = (InterfaceIndexer) $1;
1226 CheckDef (current_interface.AddIndexer (i), "indexer", i.Location);
1231 : /* empty */ { $$ = false; }
1232 | NEW { $$ = true; }
1235 interface_method_declaration
1236 : opt_attributes opt_new type IDENTIFIER
1237 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1240 $$ = new InterfaceMethod ((Expression) $3, (string) $4, (bool) $2,
1241 (Parameters) $6, (Attributes) $1, lexer.Location);
1243 | opt_attributes opt_new VOID IDENTIFIER
1244 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1247 $$ = new InterfaceMethod (
1248 TypeManager.system_void_expr, (string) $4, (bool) $2, (Parameters) $6,
1249 (Attributes) $1, lexer.Location);
1253 interface_property_declaration
1258 { lexer.PropertyParsing = true; }
1260 { lexer.PropertyParsing = false; }
1265 $$ = new InterfaceProperty ((Expression) $3, (string) $4, (bool) $2,
1266 (gs & 1) == 1, (gs & 2) == 2, (Attributes) $1,
1272 CheckIdentifierToken (yyToken);
1278 : opt_attributes GET SEMICOLON { $$ = 1; }
1279 | opt_attributes SET SEMICOLON { $$ = 2; }
1280 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1282 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1286 interface_event_declaration
1287 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1289 $$ = new InterfaceEvent ((Expression) $4, (string) $5, (bool) $2, (Attributes) $1,
1292 | opt_attributes opt_new EVENT type error {
1293 CheckIdentifierToken (yyToken);
1298 interface_indexer_declaration
1299 : opt_attributes opt_new type THIS
1300 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1302 { lexer.PropertyParsing = true; }
1304 { lexer.PropertyParsing = false; }
1307 int a_flags = (int) $10;
1309 bool do_get = (a_flags & 1) == 1;
1310 bool do_set = (a_flags & 2) == 2;
1312 $$ = new InterfaceIndexer ((Expression) $3, (Parameters) $6, do_get, do_set,
1313 (bool) $2, (Attributes) $1, lexer.Location);
1317 operator_declaration
1318 : opt_attributes opt_modifiers operator_declarator operator_body
1320 OperatorDeclaration decl = (OperatorDeclaration) $3;
1322 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
1323 decl.arg2type, decl.arg2name, (Block) $4, (Attributes) $1, decl.location);
1325 // Note again, checking is done in semantic analysis
1326 current_container.AddOperator (op);
1328 current_local_parameters = null;
1334 | SEMICOLON { $$ = null; }
1337 : type OPERATOR overloadable_operator
1338 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1340 Operator.OpType op = (Operator.OpType) $3;
1341 CheckUnaryOperator (op);
1343 if (op == Operator.OpType.Addition)
1344 op = Operator.OpType.UnaryPlus;
1346 if (op == Operator.OpType.Subtraction)
1347 op = Operator.OpType.UnaryNegation;
1349 Parameter [] pars = new Parameter [1];
1351 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1353 current_local_parameters = new Parameters (pars, null, lexer.Location);
1355 $$ = new OperatorDeclaration (op, (Expression) $1, (Expression) $5, (string) $6,
1356 null, null, lexer.Location);
1358 | type OPERATOR overloadable_operator
1360 type IDENTIFIER COMMA
1364 CheckBinaryOperator ((Operator.OpType) $3);
1366 Parameter [] pars = new Parameter [2];
1368 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1369 pars [1] = new Parameter ((Expression) $8, (string) $9, Parameter.Modifier.NONE, null);
1371 current_local_parameters = new Parameters (pars, null, lexer.Location);
1373 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1374 (Expression) $5, (string) $6,
1375 (Expression) $8, (string) $9, lexer.Location);
1377 | conversion_operator_declarator
1380 overloadable_operator
1382 : BANG { $$ = Operator.OpType.LogicalNot; }
1383 | TILDE { $$ = Operator.OpType.OnesComplement; }
1384 | OP_INC { $$ = Operator.OpType.Increment; }
1385 | OP_DEC { $$ = Operator.OpType.Decrement; }
1386 | TRUE { $$ = Operator.OpType.True; }
1387 | FALSE { $$ = Operator.OpType.False; }
1388 // Unary and binary:
1389 | PLUS { $$ = Operator.OpType.Addition; }
1390 | MINUS { $$ = Operator.OpType.Subtraction; }
1392 | STAR { $$ = Operator.OpType.Multiply; }
1393 | DIV { $$ = Operator.OpType.Division; }
1394 | PERCENT { $$ = Operator.OpType.Modulus; }
1395 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1396 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1397 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1398 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1399 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1400 | OP_EQ { $$ = Operator.OpType.Equality; }
1401 | OP_NE { $$ = Operator.OpType.Inequality; }
1402 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1403 | OP_LT { $$ = Operator.OpType.LessThan; }
1404 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1405 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1408 conversion_operator_declarator
1409 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1411 Parameter [] pars = new Parameter [1];
1413 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1415 current_local_parameters = new Parameters (pars, null, lexer.Location);
1417 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1418 null, null, lexer.Location);
1420 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1422 Parameter [] pars = new Parameter [1];
1424 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1426 current_local_parameters = new Parameters (pars, null, lexer.Location);
1428 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1429 null, null, lexer.Location);
1433 syntax_error (lexer.Location, "'operator' expected");
1437 syntax_error (lexer.Location, "'operator' expected");
1441 constructor_declaration
1444 constructor_declarator
1447 Constructor c = (Constructor) $3;
1448 c.Block = (Block) $4;
1449 c.OptAttributes = (Attributes) $1;
1450 c.ModFlags = (int) $2;
1452 if (c.Name == current_container.Basename){
1453 if ((c.ModFlags & Modifiers.STATIC) != 0){
1454 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1456 515, c.Location, String.Format (
1457 "`{0}.{1}': static constructor can not have access modifiers",
1458 c.Name, current_container.Name));
1461 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1463 if (c.Initializer != null){
1466 "Static constructors can not have an explicit this or base " +
1467 "constructor invocations");
1470 if (!c.Parameters.Empty){
1472 132, c.Location, "Static constructors should not have parameters");
1475 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1478 // We let another layer check the validity of the constructor.
1479 Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1482 CheckDef (current_container.AddConstructor (c), c.Name, c.Location);
1484 current_local_parameters = null;
1488 constructor_declarator
1490 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1492 oob_stack.Push (lexer.Location);
1494 current_local_parameters = (Parameters) $3;
1496 opt_constructor_initializer
1498 Location l = (Location) oob_stack.Pop ();
1499 $$ = new Constructor (current_container, (string) $1, (Parameters) $3,
1500 (ConstructorInitializer) $6, l);
1506 | SEMICOLON { $$ = null; }
1509 opt_constructor_initializer
1510 : /* empty */ { $$ = null; }
1511 | constructor_initializer
1514 constructor_initializer
1515 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1517 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1519 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1521 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1524 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1530 : /* EMPTY */ { $$ = 0; }
1531 | UNSAFE { $$ = Modifiers.UNSAFE; }
1532 | EXTERN { $$ = Modifiers.EXTERN; }
1535 destructor_declaration
1536 : opt_attributes opt_finalizer TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1538 if ((string) $4 != current_container.Basename){
1539 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1540 } else if (!(current_container is Class)){
1541 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1543 Location l = lexer.Location;
1546 if (!RootContext.StdLib && current_container.Name == "System.Object")
1547 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
1549 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
1551 if ((m & Modifiers.UNSAFE) != 0){
1552 if (!RootContext.Unsafe){
1553 Report.Error (227, l,
1554 "Unsafe code requires the --unsafe command " +
1555 "line option to be specified");
1559 Method d = new Method (
1560 current_container, TypeManager.system_void_expr, m, "Finalize",
1561 new Parameters (null, null, l), (Attributes) $1, l);
1563 d.Block = (Block) $7;
1564 CheckDef (current_container.AddMethod (d), d.Name, d.Location);
1572 EVENT type variable_declarators SEMICOLON
1574 foreach (VariableDeclaration var in (ArrayList) $5) {
1576 Event e = new Event ((Expression) $4, var.identifier,
1577 var.expression_or_array_initializer,
1578 (int) $2, null, null, (Attributes) $1, lexer.Location);
1580 CheckDef (current_container.AddEvent (e), e.Name, e.Location);
1586 EVENT type member_name
1589 implicit_value_parameter_type = (Expression) $4;
1590 lexer.EventParsing = true;
1591 oob_stack.Push (lexer.Location);
1593 event_accessor_declarations
1595 lexer.EventParsing = false;
1599 Location loc = (Location) oob_stack.Pop ();
1601 Pair pair = (Pair) $8;
1602 Accessor add_accessor = null;
1603 Accessor rem_accessor = null;
1605 if (pair.First != null)
1606 add_accessor = (Accessor) pair.First;
1607 if (pair.Second != null)
1608 rem_accessor = (Accessor) pair.Second;
1610 Event e = new Event ((Expression) $4, (string) $5, null, (int) $2, add_accessor, rem_accessor,
1611 (Attributes) $1, loc);
1613 CheckDef (current_container.AddEvent (e), e.Name, loc);
1614 implicit_value_parameter_type = null;
1618 event_accessor_declarations
1619 : add_accessor_declaration remove_accessor_declaration
1621 $$ = new Pair ($1, $2);
1623 | remove_accessor_declaration add_accessor_declaration
1625 $$ = new Pair ($2, $1);
1629 add_accessor_declaration
1630 : opt_attributes ADD
1632 Parameter [] args = new Parameter [1];
1633 Parameter implicit_value_parameter = new Parameter (
1634 implicit_value_parameter_type, "value",
1635 Parameter.Modifier.NONE, null);
1637 args [0] = implicit_value_parameter;
1639 current_local_parameters = new Parameters (args, null, lexer.Location);
1640 lexer.EventParsing = false;
1644 $$ = new Accessor ((Block) $4, (Attributes) $1);
1645 lexer.EventParsing = true;
1647 | opt_attributes ADD error {
1648 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1653 remove_accessor_declaration
1654 : opt_attributes REMOVE
1656 Parameter [] args = new Parameter [1];
1657 Parameter implicit_value_parameter = new Parameter (
1658 implicit_value_parameter_type, "value",
1659 Parameter.Modifier.NONE, null);
1661 args [0] = implicit_value_parameter;
1663 current_local_parameters = new Parameters (args, null, lexer.Location);
1664 lexer.EventParsing = false;
1668 $$ = new Accessor ((Block) $4, (Attributes) $1);
1669 lexer.EventParsing = true;
1671 | opt_attributes REMOVE error {
1672 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1678 : opt_attributes opt_modifiers indexer_declarator
1681 IndexerDeclaration decl = (IndexerDeclaration) $3;
1683 implicit_value_parameter_type = decl.type;
1685 lexer.PropertyParsing = true;
1686 parsing_indexer = true;
1688 indexer_parameters = decl.param_list;
1689 oob_stack.Push (lexer.Location);
1691 accessor_declarations
1693 lexer.PropertyParsing = false;
1694 parsing_indexer = false;
1698 // The signature is computed from the signature of the indexer. Look
1699 // at section 3.6 on the spec
1700 Location loc = (Location) oob_stack.Pop ();
1702 IndexerDeclaration decl = (IndexerDeclaration) $3;
1703 Pair pair = (Pair) $6;
1704 Accessor get_block = (Accessor) pair.First;
1705 Accessor set_block = (Accessor) pair.Second;
1707 indexer = new Indexer (current_container, decl.type, decl.interface_type, (int) $2,
1708 decl.param_list, get_block, set_block, (Attributes) $1, loc);
1710 // Note that there is no equivalent of CheckDef for this case
1711 // We shall handle this in semantic analysis
1713 current_container.AddIndexer (indexer);
1715 current_local_parameters = null;
1716 implicit_value_parameter_type = null;
1717 indexer_parameters = null;
1722 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1724 Parameters pars = (Parameters) $4;
1726 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1727 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1730 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
1732 | type qualified_identifier DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1734 Parameters pars = (Parameters) $6;
1736 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1737 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1739 $$ = new IndexerDeclaration ((Expression) $1, (string) ($2).ToString (), pars);
1751 Location enum_location = lexer.Location;
1753 string full_name = MakeName ((string) $4);
1754 Enum e = new Enum (current_namespace, current_container, (Expression) $5, (int) $2,
1755 full_name, (Attributes) $1, enum_location);
1757 foreach (VariableDeclaration ev in (ArrayList) $6) {
1758 Location loc = (Location) ev.Location;
1760 CheckDef (e.AddEnumMember (ev.identifier,
1761 (Expression) ev.expression_or_array_initializer,
1762 loc, ev.OptAttributes),
1763 ev.identifier, loc);
1766 CheckDef (current_container.AddEnum (e), full_name, enum_location);
1767 RootContext.Tree.RecordDecl (full_name, e);
1773 : /* empty */ { $$ = TypeManager.system_int32_expr; }
1774 | COLON type { $$ = $2; }
1778 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1784 opt_enum_member_declarations
1785 : /* empty */ { $$ = new ArrayList (4); }
1786 | enum_member_declarations opt_comma { $$ = $1; }
1789 enum_member_declarations
1790 : enum_member_declaration
1792 ArrayList l = new ArrayList (4);
1797 | enum_member_declarations COMMA enum_member_declaration
1799 ArrayList l = (ArrayList) $1;
1807 enum_member_declaration
1808 : opt_attributes IDENTIFIER
1810 $$ = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
1812 | opt_attributes IDENTIFIER
1814 $$ = lexer.Location;
1818 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
1822 delegate_declaration
1826 IDENTIFIER OPEN_PARENS
1827 opt_formal_parameter_list
1831 Location l = lexer.Location;
1832 Delegate del = new Delegate (current_namespace, current_container, (Expression) $4,
1833 (int) $2, MakeName ((string) $5), (Parameters) $7,
1834 (Attributes) $1, l);
1836 CheckDef (current_container.AddDelegate (del), del.Name, l);
1841 IDENTIFIER OPEN_PARENS
1842 opt_formal_parameter_list
1846 Location l = lexer.Location;
1847 Delegate del = new Delegate (
1848 current_namespace, current_container,
1849 TypeManager.system_void_expr, (int) $2, MakeName ((string) $5),
1850 (Parameters) $7, (Attributes) $1, l);
1852 CheckDef (current_container.AddDelegate (del), del.Name, l);
1857 : namespace_or_type_name
1860 namespace_or_type_name
1861 : qualified_identifier
1865 * Before you think of adding a return_type, notice that we have been
1866 * using two rules in the places where it matters (one rule using type
1867 * and another identical one that uses VOID as the return type). This
1868 * gets rid of a shift/reduce couple
1871 : type_name { /* class_type */
1873 This does interfaces, delegates, struct_types, class_types,
1874 parent classes, and more! 4.2
1876 $$ = DecomposeQI ((QualifiedIdentifier) $1, lexer.Location);
1888 // Note that here only unmanaged types are allowed but we
1889 // can't perform checks during this phase - we do it during
1890 // semantic analysis.
1892 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1896 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
1902 | non_expression_type rank_specifier
1904 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1906 | non_expression_type STAR
1908 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1910 | expression rank_specifiers
1912 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1916 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1920 // We need this because the parser will happily go and reduce IDENTIFIER STAR
1921 // through this different path
1923 | multiplicative_expression STAR
1925 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1932 ArrayList types = new ArrayList (4);
1937 | type_list COMMA type
1939 ArrayList types = (ArrayList) $1;
1947 * replaces all the productions for isolating the various
1948 * simple types, but we need this to reuse it easily in local_variable_type
1951 : OBJECT { $$ = TypeManager.system_object_expr; }
1952 | STRING { $$ = TypeManager.system_string_expr; }
1953 | BOOL { $$ = TypeManager.system_boolean_expr; }
1954 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
1955 | FLOAT { $$ = TypeManager.system_single_expr; }
1956 | DOUBLE { $$ = TypeManager.system_double_expr; }
1961 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
1962 | BYTE { $$ = TypeManager.system_byte_expr; }
1963 | SHORT { $$ = TypeManager.system_int16_expr; }
1964 | USHORT { $$ = TypeManager.system_uint16_expr; }
1965 | INT { $$ = TypeManager.system_int32_expr; }
1966 | UINT { $$ = TypeManager.system_uint32_expr; }
1967 | LONG { $$ = TypeManager.system_int64_expr; }
1968 | ULONG { $$ = TypeManager.system_uint64_expr; }
1969 | CHAR { $$ = TypeManager.system_char_expr; }
1970 | VOID { $$ = TypeManager.system_void_expr; }
1978 : type rank_specifiers
1980 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1985 // Expressions, section 7.5
1993 | qualified_identifier
1995 $$ = DecomposeQI ((QualifiedIdentifier) $1, lexer.Location);
1997 | parenthesized_expression
1999 | invocation_expression
2003 | post_increment_expression
2004 | post_decrement_expression
2008 | checked_expression
2009 | unchecked_expression
2010 | pointer_member_access
2011 | anonymous_method_expression
2018 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2019 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2020 | NULL { $$ = NullLiteral.Null; }
2024 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2025 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2026 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2031 object v = lexer.Value;
2037 $$ = IntLiteral.Zero;
2039 $$ = IntLiteral.One;
2041 $$ = new IntLiteral (i);
2042 } else if (v is uint)
2043 $$ = new UIntLiteral ((UInt32) v);
2045 $$ = new LongLiteral ((Int64) v);
2046 else if (v is ulong)
2047 $$ = new ULongLiteral ((UInt64) v);
2049 Console.WriteLine ("OOPS. Unexpected result from scanner");
2054 : TRUE { $$ = new BoolLiteral (true); }
2055 | FALSE { $$ = new BoolLiteral (false); }
2058 parenthesized_expression_0
2059 : OPEN_PARENS expression CLOSE_PARENS
2062 lexer.Deambiguate_CloseParens ();
2063 // After this, the next token returned is one of
2064 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2065 // or CLOSE_PARENS_MINUS.
2069 parenthesized_expression
2070 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2074 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2076 // If a parenthesized expression is followed by a minus, we need to wrap
2077 // the expression inside a ParenthesizedExpression for the CS0075 check
2078 // in Binary.DoResolve().
2079 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2084 : primary_expression DOT IDENTIFIER
2086 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2088 | predefined_type DOT IDENTIFIER
2090 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2098 invocation_expression
2099 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2102 Location l = lexer.Location;
2103 Report.Error (1, l, "Parse error");
2105 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2107 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2109 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2111 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2113 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2118 : /* empty */ { $$ = null; }
2125 ArrayList list = new ArrayList (4);
2129 | argument_list COMMA argument
2131 ArrayList list = (ArrayList) $1;
2135 | argument_list error {
2136 CheckToken (1026, yyToken, ", or ) expected");
2143 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2145 | REF variable_reference
2147 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2149 | OUT variable_reference
2151 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2156 : expression { note ("section 5.4"); $$ = $1; }
2160 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2162 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2164 | primary_expression rank_specifiers
2166 // So the super-trick is that primary_expression
2167 // can only be either a SimpleName or a MemberAccess.
2168 // The MemberAccess case arises when you have a fully qualified type-name like :
2170 // SimpleName is when you have
2173 Expression expr = (Expression) $1;
2174 if (expr is ComposedCast){
2175 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2176 } else if (!(expr is SimpleName || expr is MemberAccess)){
2177 Error_ExpectingTypeName (lexer.Location, expr);
2178 $$ = TypeManager.system_object_expr;
2181 // So we extract the string corresponding to the SimpleName
2184 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2192 ArrayList list = new ArrayList (4);
2196 | expression_list COMMA expression
2198 ArrayList list = (ArrayList) $1;
2207 $$ = new This (current_block, lexer.Location);
2212 : BASE DOT IDENTIFIER
2214 $$ = new BaseAccess ((string) $3, lexer.Location);
2216 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2218 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2221 Report.Error (175, "Use of keyword `base' is not valid in this context");
2226 post_increment_expression
2227 : primary_expression OP_INC
2229 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2230 (Expression) $1, lexer.Location);
2234 post_decrement_expression
2235 : primary_expression OP_DEC
2237 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2238 (Expression) $1, lexer.Location);
2243 : object_or_delegate_creation_expression
2244 | array_creation_expression
2247 object_or_delegate_creation_expression
2248 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2250 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2254 array_creation_expression
2255 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2257 opt_array_initializer
2259 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2261 | NEW type rank_specifiers array_initializer
2263 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2267 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2283 : rank_specifier opt_rank_specifier
2285 $$ = (string) $2 + (string) $1;
2290 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2292 $$ = "[" + (string) $2 + "]";
2312 | dim_separators COMMA
2314 $$ = (string) $1 + ",";
2318 opt_array_initializer
2330 : OPEN_BRACE CLOSE_BRACE
2332 ArrayList list = new ArrayList (4);
2335 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2337 $$ = (ArrayList) $2;
2341 variable_initializer_list
2342 : variable_initializer
2344 ArrayList list = new ArrayList (4);
2348 | variable_initializer_list COMMA variable_initializer
2350 ArrayList list = (ArrayList) $1;
2357 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2359 $$ = new TypeOfVoid (lexer.Location);
2361 | TYPEOF OPEN_PARENS type CLOSE_PARENS
2363 $$ = new TypeOf ((Expression) $3, lexer.Location);
2368 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2369 $$ = new SizeOf ((Expression) $3, lexer.Location);
2374 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2376 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2380 unchecked_expression
2381 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2383 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2387 pointer_member_access
2388 : primary_expression OP_PTR IDENTIFIER
2392 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2393 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2397 anonymous_method_expression
2398 : DELEGATE opt_anonymous_method_signature {
2399 oob_stack.Push (current_local_parameters);
2400 current_local_parameters = (Parameters)$2;
2402 if (!RootContext.V2){
2403 Report.Error (-213, lexer.Location, "Anonymous methods are only supported in V2");
2406 $$ = new AnonymousMethod ((Parameters) $2, (Block) $4, lexer.Location);
2407 current_local_parameters = (Parameters) oob_stack.Pop ();
2411 opt_anonymous_method_signature
2412 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
2413 | anonymous_method_signature
2416 anonymous_method_signature
2417 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
2420 $$ = Parameters.EmptyReadOnlyParameters;
2422 ArrayList par_list = (ArrayList) $2;
2423 Parameter [] pars = new Parameter [par_list.Count];
2424 par_list.CopyTo (pars);
2425 $$ = new Parameters (pars, null, lexer.Location);
2430 opt_anonymous_method_parameter_list
2431 : /* empty */ { $$ = null; }
2432 | anonymous_method_parameter_list { $$ = $1; }
2435 anonymous_method_parameter_list
2436 : anonymous_method_parameter
2438 ArrayList a = new ArrayList (4);
2442 | anonymous_method_parameter_list COMMA anonymous_method_parameter
2444 ArrayList a = (ArrayList) $1;
2450 anonymous_method_parameter
2451 : opt_parameter_modifier type IDENTIFIER {
2452 $$ = new Parameter ((Expression) $2, (string) $2, (Parameter.Modifier) $1, null);
2457 : primary_expression
2458 | BANG prefixed_unary_expression
2460 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2462 | TILDE prefixed_unary_expression
2464 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2470 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
2472 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2474 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
2476 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2482 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2484 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2489 // The idea to split this out is from Rhys' grammar
2490 // to solve the problem with casts.
2492 prefixed_unary_expression
2494 | PLUS prefixed_unary_expression
2496 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2498 | MINUS prefixed_unary_expression
2500 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2502 | OP_INC prefixed_unary_expression
2504 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2505 (Expression) $2, lexer.Location);
2507 | OP_DEC prefixed_unary_expression
2509 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2510 (Expression) $2, lexer.Location);
2512 | STAR prefixed_unary_expression
2514 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2516 | BITWISE_AND prefixed_unary_expression
2518 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2522 pre_increment_expression
2523 : OP_INC prefixed_unary_expression
2525 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2526 (Expression) $2, lexer.Location);
2530 pre_decrement_expression
2531 : OP_DEC prefixed_unary_expression
2533 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2534 (Expression) $2, lexer.Location);
2538 multiplicative_expression
2539 : prefixed_unary_expression
2540 | multiplicative_expression STAR prefixed_unary_expression
2542 $$ = new Binary (Binary.Operator.Multiply,
2543 (Expression) $1, (Expression) $3, lexer.Location);
2545 | multiplicative_expression DIV prefixed_unary_expression
2547 $$ = new Binary (Binary.Operator.Division,
2548 (Expression) $1, (Expression) $3, lexer.Location);
2550 | multiplicative_expression PERCENT prefixed_unary_expression
2552 $$ = new Binary (Binary.Operator.Modulus,
2553 (Expression) $1, (Expression) $3, lexer.Location);
2558 : multiplicative_expression
2559 | additive_expression PLUS multiplicative_expression
2561 $$ = new Binary (Binary.Operator.Addition,
2562 (Expression) $1, (Expression) $3, lexer.Location);
2564 | additive_expression MINUS multiplicative_expression
2566 $$ = new Binary (Binary.Operator.Subtraction,
2567 (Expression) $1, (Expression) $3, lexer.Location);
2572 : additive_expression
2573 | shift_expression OP_SHIFT_LEFT additive_expression
2575 $$ = new Binary (Binary.Operator.LeftShift,
2576 (Expression) $1, (Expression) $3, lexer.Location);
2578 | shift_expression OP_SHIFT_RIGHT additive_expression
2580 $$ = new Binary (Binary.Operator.RightShift,
2581 (Expression) $1, (Expression) $3, lexer.Location);
2585 relational_expression
2587 | relational_expression OP_LT shift_expression
2589 $$ = new Binary (Binary.Operator.LessThan,
2590 (Expression) $1, (Expression) $3, lexer.Location);
2592 | relational_expression OP_GT shift_expression
2594 $$ = new Binary (Binary.Operator.GreaterThan,
2595 (Expression) $1, (Expression) $3, lexer.Location);
2597 | relational_expression OP_LE shift_expression
2599 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2600 (Expression) $1, (Expression) $3, lexer.Location);
2602 | relational_expression OP_GE shift_expression
2604 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2605 (Expression) $1, (Expression) $3, lexer.Location);
2607 | relational_expression IS type
2609 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
2611 | relational_expression AS type
2613 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
2618 : relational_expression
2619 | equality_expression OP_EQ relational_expression
2621 $$ = new Binary (Binary.Operator.Equality,
2622 (Expression) $1, (Expression) $3, lexer.Location);
2624 | equality_expression OP_NE relational_expression
2626 $$ = new Binary (Binary.Operator.Inequality,
2627 (Expression) $1, (Expression) $3, lexer.Location);
2632 : equality_expression
2633 | and_expression BITWISE_AND equality_expression
2635 $$ = new Binary (Binary.Operator.BitwiseAnd,
2636 (Expression) $1, (Expression) $3, lexer.Location);
2640 exclusive_or_expression
2642 | exclusive_or_expression CARRET and_expression
2644 $$ = new Binary (Binary.Operator.ExclusiveOr,
2645 (Expression) $1, (Expression) $3, lexer.Location);
2649 inclusive_or_expression
2650 : exclusive_or_expression
2651 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2653 $$ = new Binary (Binary.Operator.BitwiseOr,
2654 (Expression) $1, (Expression) $3, lexer.Location);
2658 conditional_and_expression
2659 : inclusive_or_expression
2660 | conditional_and_expression OP_AND inclusive_or_expression
2662 $$ = new Binary (Binary.Operator.LogicalAnd,
2663 (Expression) $1, (Expression) $3, lexer.Location);
2667 conditional_or_expression
2668 : conditional_and_expression
2669 | conditional_or_expression OP_OR conditional_and_expression
2671 $$ = new Binary (Binary.Operator.LogicalOr,
2672 (Expression) $1, (Expression) $3, lexer.Location);
2676 conditional_expression
2677 : conditional_or_expression
2678 | conditional_or_expression INTERR expression COLON expression
2680 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2684 assignment_expression
2685 : prefixed_unary_expression ASSIGN expression
2687 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2689 | prefixed_unary_expression OP_MULT_ASSIGN expression
2691 Location l = lexer.Location;
2693 $$ = new CompoundAssign (
2694 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
2696 | prefixed_unary_expression OP_DIV_ASSIGN expression
2698 Location l = lexer.Location;
2700 $$ = new CompoundAssign (
2701 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
2703 | prefixed_unary_expression OP_MOD_ASSIGN expression
2705 Location l = lexer.Location;
2707 $$ = new CompoundAssign (
2708 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
2710 | prefixed_unary_expression OP_ADD_ASSIGN expression
2712 Location l = lexer.Location;
2714 $$ = new CompoundAssign (
2715 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
2717 | prefixed_unary_expression OP_SUB_ASSIGN expression
2719 Location l = lexer.Location;
2721 $$ = new CompoundAssign (
2722 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
2724 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2726 Location l = lexer.Location;
2728 $$ = new CompoundAssign (
2729 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
2731 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2733 Location l = lexer.Location;
2735 $$ = new CompoundAssign (
2736 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
2738 | prefixed_unary_expression OP_AND_ASSIGN expression
2740 Location l = lexer.Location;
2742 $$ = new CompoundAssign (
2743 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
2745 | prefixed_unary_expression OP_OR_ASSIGN expression
2747 Location l = lexer.Location;
2749 $$ = new CompoundAssign (
2750 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
2752 | prefixed_unary_expression OP_XOR_ASSIGN expression
2754 Location l = lexer.Location;
2756 $$ = new CompoundAssign (
2757 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
2762 : conditional_expression
2763 | assignment_expression
2785 name = MakeName ((string) $4);
2787 new_class = new Class (current_namespace, current_container, name, (int) $2,
2788 (Attributes) $1, lexer.Location);
2789 current_container = new_class;
2790 RootContext.Tree.RecordDecl (name, new_class);
2796 Class new_class = (Class) current_container;
2799 new_class.Bases = (ArrayList) $6;
2801 current_container = current_container.Parent;
2802 CheckDef (current_container.AddClass (new_class), new_class.Name, new_class.Location);
2809 : /* empty */ { $$ = (int) 0; }
2815 | modifiers modifier
2820 if ((m1 & m2) != 0) {
2821 Location l = lexer.Location;
2822 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2824 $$ = (int) (m1 | m2);
2829 : NEW { $$ = Modifiers.NEW; }
2830 | PUBLIC { $$ = Modifiers.PUBLIC; }
2831 | PROTECTED { $$ = Modifiers.PROTECTED; }
2832 | INTERNAL { $$ = Modifiers.INTERNAL; }
2833 | PRIVATE { $$ = Modifiers.PRIVATE; }
2834 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2835 | SEALED { $$ = Modifiers.SEALED; }
2836 | STATIC { $$ = Modifiers.STATIC; }
2837 | READONLY { $$ = Modifiers.READONLY; }
2838 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2839 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2840 | EXTERN { $$ = Modifiers.EXTERN; }
2841 | VOLATILE { $$ = Modifiers.VOLATILE; }
2842 | UNSAFE { $$ = Modifiers.UNSAFE; }
2846 : /* empty */ { $$ = null; }
2847 | class_base { $$ = $1; }
2851 : COLON type_list { $$ = $2; }
2859 // A block is "contained" on the following places:
2861 // property_declaration as part of the accessor body (get/set)
2862 // operator_declaration
2863 // constructor_declaration
2864 // destructor_declaration
2865 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2870 current_block = new Block (current_block, current_local_parameters,
2871 lexer.Location, Location.Null);
2873 opt_statement_list CLOSE_BRACE
2875 while (current_block.Implicit)
2876 current_block = current_block.Parent;
2878 current_block.SetEndLocation (lexer.Location);
2879 current_block = current_block.Parent;
2890 | statement_list statement
2894 : declaration_statement
2896 if ($1 != null && (Block) $1 != current_block){
2897 current_block.AddStatement ((Statement) $1);
2898 current_block = (Block) $1;
2901 | embedded_statement
2903 Statement s = (Statement) $1;
2906 current_block.AddStatement ((Statement) $1);
2914 | expression_statement
2915 | selection_statement
2916 | iteration_statement
2920 | unchecked_statement
2930 $$ = EmptyStatement.Value;
2937 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
2939 if (!current_block.AddLabel ((string) $1, labeled)){
2940 Location l = lexer.Location;
2941 Report.Error (140, l, "The label '" + ((string) $1) + "' is a duplicate");
2943 current_block.AddStatement (labeled);
2948 declaration_statement
2949 : local_variable_declaration SEMICOLON
2952 DictionaryEntry de = (DictionaryEntry) $1;
2954 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
2958 | local_constant_declaration SEMICOLON
2961 DictionaryEntry de = (DictionaryEntry) $1;
2963 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
2969 * The following is from Rhys' grammar:
2970 * > Types in local variable declarations must be recognized as
2971 * > expressions to prevent reduce/reduce errors in the grammar.
2972 * > The expressions are converted into types during semantic analysis.
2975 : primary_expression opt_rank_specifier
2977 // FIXME: Do something smart here regarding the composition of the type.
2979 // Ok, the above "primary_expression" is there to get rid of
2980 // both reduce/reduce and shift/reduces in the grammar, it should
2981 // really just be "type_name". If you use type_name, a reduce/reduce
2982 // creeps up. If you use qualified_identifier (which is all we need
2983 // really) two shift/reduces appear.
2986 // So the super-trick is that primary_expression
2987 // can only be either a SimpleName or a MemberAccess.
2988 // The MemberAccess case arises when you have a fully qualified type-name like :
2990 // SimpleName is when you have
2993 Expression expr = (Expression) $1;
2994 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
2995 Error_ExpectingTypeName (lexer.Location, expr);
2999 // So we extract the string corresponding to the SimpleName
3003 if ((string) $2 == "")
3006 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3009 | builtin_types opt_rank_specifier
3011 if ((string) $2 == "")
3014 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3018 local_variable_pointer_type
3019 : primary_expression STAR
3021 Expression expr = (Expression) $1;
3022 Location l = lexer.Location;
3024 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3025 Error_ExpectingTypeName (l, expr);
3029 $$ = new ComposedCast ((Expression) $1, "*", l);
3031 | builtin_types STAR
3033 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3037 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3039 | local_variable_pointer_type STAR
3041 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3045 local_variable_declaration
3046 : local_variable_type variable_declarators
3049 $$ = new DictionaryEntry ($1, $2);
3053 | local_variable_pointer_type opt_rank_specifier variable_declarators
3058 if ((string) $2 == "")
3059 t = (Expression) $1;
3061 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3062 $$ = new DictionaryEntry (t, $3);
3068 local_constant_declaration
3069 : CONST local_variable_type constant_declarators
3072 $$ = new DictionaryEntry ($2, $3);
3078 expression_statement
3079 : statement_expression SEMICOLON
3086 // We have to do the wrapping here and not in the case above,
3087 // because statement_expression is used for example in for_statement
3089 statement_expression
3090 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3091 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3092 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3093 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3094 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3095 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3096 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3098 Report.Error (1002, lexer.Location, "Expecting `;'");
3103 object_creation_expression
3104 : object_or_delegate_creation_expression
3105 { note ("complain if this is a delegate maybe?"); }
3114 : if_statement_open if_statement_rest
3123 oob_stack.Push (lexer.Location);
3128 : boolean_expression CLOSE_PARENS
3131 Location l = (Location) oob_stack.Pop ();
3133 $$ = new If ((Expression) $1, (Statement) $3, l);
3135 if (RootContext.WarningLevel >= 3){
3136 if ($3 == EmptyStatement.Value)
3137 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3141 | boolean_expression CLOSE_PARENS
3142 embedded_statement ELSE embedded_statement
3144 Location l = (Location) oob_stack.Pop ();
3146 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
3151 : SWITCH OPEN_PARENS
3153 oob_stack.Push (lexer.Location);
3154 switch_stack.Push (current_block);
3156 expression CLOSE_PARENS
3159 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
3160 current_block = (Block) switch_stack.Pop ();
3176 Report.Error (1522, lexer.Location, "Empty switch block");
3184 ArrayList sections = new ArrayList (4);
3189 | switch_sections switch_section
3191 ArrayList sections = (ArrayList) $1;
3201 current_block = current_block.CreateSwitchBlock (lexer.Location);
3205 Block topmost = current_block;
3207 while (topmost.Implicit)
3208 topmost = topmost.Parent;
3209 $$ = new SwitchSection ((ArrayList) $1, topmost);
3216 ArrayList labels = new ArrayList (4);
3221 | switch_labels switch_label
3223 ArrayList labels = (ArrayList) ($1);
3231 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3232 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3235 1523, lexer.Location,
3236 "The keyword case or default must precede code in switch block");
3250 oob_stack.Push (lexer.Location);
3252 boolean_expression CLOSE_PARENS embedded_statement
3254 Location l = (Location) oob_stack.Pop ();
3255 $$ = new While ((Expression) $4, (Statement) $6, l);
3257 if (RootContext.WarningLevel >= 3){
3258 if ($6 == EmptyStatement.Value)
3259 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3265 : DO embedded_statement
3267 oob_stack.Push (lexer.Location);
3269 boolean_expression CLOSE_PARENS SEMICOLON
3271 Location l = (Location) oob_stack.Pop ();
3273 $$ = new Do ((Statement) $2, (Expression) $6, l);
3279 opt_for_initializer SEMICOLON
3281 Block assign_block = new Block (current_block);
3282 current_block = assign_block;
3284 if ($3 is DictionaryEntry){
3285 DictionaryEntry de = (DictionaryEntry) $3;
3287 Expression type = (Expression) de.Key;
3288 ArrayList var_declarators = (ArrayList) de.Value;
3290 foreach (VariableDeclaration decl in var_declarators){
3294 vi = current_block.AddVariable (
3295 type, decl.identifier, current_local_parameters, decl.Location);
3299 Location l = lexer.Location;
3301 if (decl.expression_or_array_initializer is Expression){
3302 expr = (Expression) decl.expression_or_array_initializer;
3303 } else if (decl.expression_or_array_initializer == null) {
3306 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3307 expr = new ArrayCreation (type, "", init, decl.Location);
3310 LocalVariableReference var;
3311 var = new LocalVariableReference (assign_block, decl.identifier, l);
3314 Assign a = new Assign (var, expr, decl.Location);
3316 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3322 oob_stack.Push (lexer.Location);
3324 opt_for_condition SEMICOLON
3325 opt_for_iterator CLOSE_PARENS
3328 Location l = (Location) oob_stack.Pop ();
3330 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3332 if (RootContext.WarningLevel >= 3){
3333 if ($10 == EmptyStatement.Value)
3334 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3337 current_block.AddStatement (f);
3338 while (current_block.Implicit)
3339 current_block = current_block.Parent;
3341 current_block = current_block.Parent;
3346 : /* empty */ { $$ = EmptyStatement.Value; }
3351 : local_variable_declaration
3352 | statement_expression_list
3356 : /* empty */ { $$ = null; }
3357 | boolean_expression
3361 : /* empty */ { $$ = EmptyStatement.Value; }
3366 : statement_expression_list
3369 statement_expression_list
3370 : statement_expression
3372 // CHANGE: was `null'
3373 Block b = new Block (current_block, Block.Flags.Implicit);
3375 b.AddStatement ((Statement) $1);
3378 | statement_expression_list COMMA statement_expression
3380 Block b = (Block) $1;
3382 b.AddStatement ((Statement) $3);
3388 : FOREACH OPEN_PARENS type IDENTIFIER IN
3390 oob_stack.Push (lexer.Location);
3392 expression CLOSE_PARENS
3394 oob_stack.Push (current_block);
3396 Block foreach_block = new Block (current_block, Block.Flags.Implicit);
3397 LocalVariableReference v = null;
3398 Location l = lexer.Location;
3401 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3405 // Get a writable reference to this read-only variable.
3406 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3408 current_block = foreach_block;
3411 oob_stack.Push (current_block);
3415 Block foreach_block = (Block) oob_stack.Pop ();
3416 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3417 Block prev_block = (Block) oob_stack.Pop ();
3418 Location l = (Location) oob_stack.Pop ();
3420 current_block = prev_block;
3423 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3424 foreach_block.AddStatement (f);
3433 | continue_statement
3443 $$ = new Break (lexer.Location);
3448 : CONTINUE SEMICOLON
3450 $$ = new Continue (lexer.Location);
3455 : GOTO IDENTIFIER SEMICOLON
3457 $$ = new Goto (current_block, (string) $2, lexer.Location);
3459 | GOTO CASE constant_expression SEMICOLON
3461 $$ = new GotoCase ((Expression) $3, lexer.Location);
3463 | GOTO DEFAULT SEMICOLON
3465 $$ = new GotoDefault (lexer.Location);
3470 : RETURN opt_expression SEMICOLON
3472 $$ = new Return ((Expression) $2, lexer.Location);
3477 : THROW opt_expression SEMICOLON
3479 $$ = new Throw ((Expression) $2, lexer.Location);
3484 : YIELD RETURN expression SEMICOLON
3486 if (iterator_container == null){
3487 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3490 iterator_container.SetYields ();
3491 $$ = new Yield ((Expression) $3, lexer.Location);
3494 | YIELD expression SEMICOLON
3496 if (iterator_container == null){
3497 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3500 iterator_container.SetYields ();
3501 $$ = new Yield ((Expression) $2, lexer.Location);
3504 | YIELD BREAK SEMICOLON
3506 if (iterator_container == null){
3507 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3510 iterator_container.SetYields ();
3511 $$ = new YieldBreak (lexer.Location);
3522 : TRY block catch_clauses
3525 ArrayList s = new ArrayList (4);
3527 foreach (Catch cc in (ArrayList) $3) {
3534 // Now s contains the list of specific catch clauses
3535 // and g contains the general one.
3537 $$ = new Try ((Block) $2, s, g, null, lexer.Location);
3539 | TRY block opt_catch_clauses FINALLY block
3542 ArrayList s = new ArrayList (4);
3543 ArrayList catch_list = (ArrayList) $3;
3545 if (catch_list != null){
3546 foreach (Catch cc in catch_list) {
3554 $$ = new Try ((Block) $2, s, g, (Block) $5, lexer.Location);
3558 Report.Error (1524, lexer.Location, "Expected catch or finally");
3563 : /* empty */ { $$ = null; }
3570 ArrayList l = new ArrayList (4);
3575 | catch_clauses catch_clause
3577 ArrayList l = (ArrayList) $1;
3585 : /* empty */ { $$ = null; }
3590 : CATCH opt_catch_args
3592 Expression type = null;
3596 DictionaryEntry cc = (DictionaryEntry) $2;
3597 type = (Expression) cc.Key;
3598 id = (string) cc.Value;
3601 ArrayList one = new ArrayList (4);
3602 Location loc = lexer.Location;
3604 one.Add (new VariableDeclaration (id, null, loc));
3607 current_block = new Block (current_block);
3608 Block b = declare_local_variables (type, one, loc);
3613 Expression type = null;
3617 DictionaryEntry cc = (DictionaryEntry) $2;
3618 type = (Expression) cc.Key;
3619 id = (string) cc.Value;
3623 // FIXME: I can change this for an assignment.
3625 while (current_block != (Block) $1)
3626 current_block = current_block.Parent;
3631 $$ = new Catch (type, id , (Block) $4, lexer.Location);
3636 : /* empty */ { $$ = null; }
3641 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3643 $$ = new DictionaryEntry ($2, $3);
3650 $$ = new Checked ((Block) $2);
3657 $$ = new Unchecked ((Block) $2);
3664 if (!RootContext.Unsafe){
3665 Report.Error (227, lexer.Location,
3666 "Unsafe code can only be used if --unsafe is used");
3669 $$ = new Unsafe ((Block) $3);
3675 type fixed_pointer_declarators
3678 Block assign_block = new Block (current_block, Block.Flags.Implicit);
3679 ArrayList list = (ArrayList) $4;
3680 Expression type = (Expression) $3;
3681 Location l = lexer.Location;
3682 int top = list.Count;
3684 for (int i = 0; i < top; i++){
3685 Pair p = (Pair) list [i];
3688 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3695 current_block.AddStatement (assign_block);
3696 current_block = assign_block;
3697 oob_stack.Push (assign_block);
3702 Location l = (Location) oob_stack.Pop ();
3703 Block assign_block = (Block) oob_stack.Pop ();
3705 ArrayList list = (ArrayList) $4;
3706 int top = list.Count;
3708 $$ = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
3712 fixed_pointer_declarators
3713 : fixed_pointer_declarator {
3714 ArrayList declarators = new ArrayList (4);
3715 declarators.Add ($1);
3718 | fixed_pointer_declarators COMMA fixed_pointer_declarator
3720 ArrayList declarators = (ArrayList) $1;
3721 declarators.Add ($3);
3726 fixed_pointer_declarator
3727 : IDENTIFIER ASSIGN expression
3729 $$ = new Pair ($1, $3);
3734 : LOCK OPEN_PARENS expression CLOSE_PARENS
3740 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
3745 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
3747 Block assign_block = new Block (current_block);
3748 current_block = assign_block;
3750 oob_stack.Push (lexer.Location);
3752 if ($3 is DictionaryEntry){
3753 DictionaryEntry de = (DictionaryEntry) $3;
3754 Location l = lexer.Location;
3756 Expression type = (Expression) de.Key;
3757 ArrayList var_declarators = (ArrayList) de.Value;
3759 ArrayList vars = new ArrayList (4);
3761 foreach (VariableDeclaration decl in var_declarators){
3763 LocalInfo vi = current_block.AddVariable (
3764 type, decl.identifier,
3765 current_local_parameters, decl.Location);
3771 if (decl.expression_or_array_initializer is Expression){
3772 expr = (Expression) decl.expression_or_array_initializer;
3774 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3776 expr = new ArrayCreation (type, "", init, decl.Location);
3779 LocalVariableReference var;
3781 // Get a writable reference to this read-only variable.
3782 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
3784 // This is so that it is not a warning on using variables
3787 vars.Add (new DictionaryEntry (var, expr));
3789 // Assign a = new Assign (var, expr, decl.Location);
3790 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3792 $3 = new DictionaryEntry (type, vars);
3797 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
3798 current_block.AddStatement (u);
3799 while (current_block.Implicit)
3800 current_block = current_block.Parent;
3802 current_block = current_block.Parent;
3806 resource_acquisition
3807 : local_variable_declaration
3814 // A class used to pass around variable declarations and constants
3816 public class VariableDeclaration {
3817 public string identifier;
3818 public object expression_or_array_initializer;
3819 public Location Location;
3820 public Attributes OptAttributes;
3822 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
3824 this.identifier = id;
3825 this.expression_or_array_initializer = eoai;
3827 this.OptAttributes = opt_attrs;
3830 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
3836 // A class used to hold info about an indexer declarator
3838 public class IndexerDeclaration {
3839 public Expression type;
3840 public string interface_type;
3841 public Parameters param_list;
3843 public IndexerDeclaration (Expression type, string interface_type, Parameters param_list)
3846 this.interface_type = interface_type;
3847 this.param_list = param_list;
3852 // A class used to hold info about an operator declarator
3854 public class OperatorDeclaration {
3855 public Operator.OpType optype;
3856 public Expression ret_type, arg1type, arg2type;
3857 public string arg1name, arg2name;
3858 public Location location;
3860 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
3861 Expression arg1type, string arg1name,
3862 Expression arg2type, string arg2name, Location location)
3865 this.ret_type = ret_type;
3866 this.arg1type = arg1type;
3867 this.arg1name = arg1name;
3868 this.arg2type = arg2type;
3869 this.arg2name = arg2name;
3870 this.location = location;
3875 public class QualifiedIdentifier {
3877 public QualifiedIdentifier prev;
3879 public QualifiedIdentifier (string s)
3885 public QualifiedIdentifier (QualifiedIdentifier prev, string s)
3891 public override string ToString ()
3898 if (prev.prev == null)
3899 return prev.s + "." + s;
3900 if (prev.prev.prev == null)
3901 return prev.prev.s + "." + prev.s + "." + s;
3904 QualifiedIdentifier ptr = this.prev;
3906 while (ptr != null){
3910 string [] ss = new string [count];
3912 for (int i = count; i-- > 0;){
3916 return String.Join (".", ss, 0, ss.Length);
3920 void Error_ExpectingTypeName (Location l, Expression expr)
3922 if (expr is Invocation){
3923 Report.Error (1002, l, "; expected");
3925 Report.Error (-1, l, "Invalid Type definition");
3930 // Given the @class_name name, it creates a fully qualified name
3931 // based on the containing declaration space
3934 MakeName (string class_name)
3936 string ns = current_namespace.FullName;
3937 string container_name = current_container.Name;
3939 if (container_name == ""){
3941 return ns + "." + class_name;
3945 return container_name + "." + class_name;
3949 // Used to report back to the user the result of a declaration
3950 // in the current declaration space
3953 CheckDef (DeclSpace.AdditionResult result, string name, Location l)
3955 if (result == DeclSpace.AdditionResult.Success)
3959 case DeclSpace.AdditionResult.NameExists:
3960 Report.Error (102, l, "The container `" + current_container.Name +
3961 "' already contains a definition for `"+
3967 // This is handled only for static Constructors, because
3968 // in reality we handle these by the semantic analysis later
3970 case DeclSpace.AdditionResult.MethodExists:
3972 111, l, "Class `"+current_container.Name+
3973 "' already defines a member called '" +
3974 name + "' with the same parameter types (more than one default constructor)");
3977 case DeclSpace.AdditionResult.EnclosingClash:
3978 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
3981 case DeclSpace.AdditionResult.NotAConstructor:
3982 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
3985 case DeclSpace.AdditionResult.Error:
3986 // Error has already been reported.
3992 CheckDef (bool result, string name, Location l)
3996 CheckDef (DeclSpace.AdditionResult.NameExists, name, l);
3999 Expression DecomposeQI (QualifiedIdentifier qi, Location loc)
4003 if (qi.prev == null)
4004 return new SimpleName (qi.s, loc);
4006 o = DecomposeQI (qi.prev, loc);
4007 return new MemberAccess (o, qi.s, loc);
4012 // This method is used to get at the complete string representation of
4013 // a fully-qualified type name, hiding inside a MemberAccess ;-)
4014 // This is necessary because local_variable_type admits primary_expression
4015 // as the type of the variable. So we do some extra checking
4018 string GetQualifiedIdentifier (Expression expr)
4020 if (expr is SimpleName)
4021 return ((SimpleName)expr).Name;
4022 else if (expr is MemberAccess)
4023 return GetQualifiedIdentifier (((MemberAccess)expr).Expr) + "." + ((MemberAccess) expr).Identifier;
4025 throw new Exception ("Expr has to be either SimpleName or MemberAccess! (" + expr + ")");
4030 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4032 Block implicit_block;
4033 ArrayList inits = null;
4036 // We use the `Used' property to check whether statements
4037 // have been added to the current block. If so, we need
4038 // to create another block to contain the new declaration
4039 // otherwise, as an optimization, we use the same block to
4040 // add the declaration.
4042 // FIXME: A further optimization is to check if the statements
4043 // that were added were added as part of the initialization
4044 // below. In which case, no other statements have been executed
4045 // and we might be able to reduce the number of blocks for
4046 // situations like this:
4048 // int j = 1; int k = j + 1;
4050 if (current_block.Used) {
4051 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4052 implicit_block.AddChildVariableNames (current_block);
4054 implicit_block = current_block;
4056 foreach (VariableDeclaration decl in variable_declarators){
4058 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4059 if (decl.expression_or_array_initializer != null){
4061 inits = new ArrayList (4);
4068 return implicit_block;
4070 foreach (VariableDeclaration decl in inits){
4074 if (decl.expression_or_array_initializer is Expression){
4075 expr = (Expression) decl.expression_or_array_initializer;
4078 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4080 expr = new ArrayCreation (type, "", init, decl.Location);
4083 LocalVariableReference var;
4084 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4086 assign = new Assign (var, expr, decl.Location);
4088 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4091 return implicit_block;
4094 Block declare_local_constants (Expression type, ArrayList declarators)
4096 Block implicit_block;
4098 if (current_block.Used)
4099 implicit_block = new Block (current_block, Block.Flags.Implicit);
4101 implicit_block = current_block;
4103 foreach (VariableDeclaration decl in declarators){
4104 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4105 current_local_parameters, decl.Location);
4108 return implicit_block;
4111 void CheckAttributeTarget (string a)
4115 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
4119 Location l = lexer.Location;
4120 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4126 void CheckUnaryOperator (Operator.OpType op)
4130 case Operator.OpType.LogicalNot:
4131 case Operator.OpType.OnesComplement:
4132 case Operator.OpType.Increment:
4133 case Operator.OpType.Decrement:
4134 case Operator.OpType.True:
4135 case Operator.OpType.False:
4136 case Operator.OpType.Addition:
4137 case Operator.OpType.Subtraction:
4142 Location l = lexer.Location;
4143 Report.Error (1019, l, "Overloadable unary operator expected");
4149 void CheckBinaryOperator (Operator.OpType op)
4153 case Operator.OpType.Addition:
4154 case Operator.OpType.Subtraction:
4155 case Operator.OpType.Multiply:
4156 case Operator.OpType.Division:
4157 case Operator.OpType.Modulus:
4158 case Operator.OpType.BitwiseAnd:
4159 case Operator.OpType.BitwiseOr:
4160 case Operator.OpType.ExclusiveOr:
4161 case Operator.OpType.LeftShift:
4162 case Operator.OpType.RightShift:
4163 case Operator.OpType.Equality:
4164 case Operator.OpType.Inequality:
4165 case Operator.OpType.GreaterThan:
4166 case Operator.OpType.LessThan:
4167 case Operator.OpType.GreaterThanOrEqual:
4168 case Operator.OpType.LessThanOrEqual:
4172 Location l = lexer.Location;
4173 Report.Error (1020, l, "Overloadable binary operator expected");
4179 void syntax_error (Location l, string msg)
4181 Report.Error (1003, l, "Syntax error, " + msg);
4184 void output (string s)
4186 Console.WriteLine (s);
4189 void note (string s)
4191 // Used to put annotations
4196 public Tokenizer Lexer {
4202 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4204 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4205 this.name = file.Name;
4207 current_container = RootContext.Tree.Types;
4208 current_container.NamespaceEntry = current_namespace;
4209 oob_stack = new Stack ();
4210 switch_stack = new Stack ();
4212 lexer = new Tokenizer (reader, file, defines);
4215 public override void parse ()
4218 if (yacc_verbose_flag)
4219 yyparse (lexer, new yydebug.yyDebugSimple ());
4222 Tokenizer tokenizer = lexer as Tokenizer;
4223 tokenizer.cleanup ();
4224 } catch (Exception e){
4225 // Please do not remove this, it is used during debugging
4228 Console.WriteLine (e);
4229 Report.Error (-25, lexer.Location, "Parsing error");
4230 if (Driver.parser_verbose)
4231 Console.WriteLine (e);
4235 void CheckToken (int error, int yyToken, string msg)
4237 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4238 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4241 Report.Error (error, lexer.Location, msg);
4244 void CheckIdentifierToken (int yyToken)
4246 CheckToken (1041, yyToken, "Identifier expected");