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;
928 | SEMICOLON { $$ = null; }
931 opt_formal_parameter_list
932 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
933 | formal_parameter_list
936 formal_parameter_list
939 ArrayList pars_list = (ArrayList) $1;
941 Parameter [] pars = new Parameter [pars_list.Count];
942 pars_list.CopyTo (pars);
944 $$ = new Parameters (pars, null, lexer.Location);
946 | fixed_parameters COMMA parameter_array
948 ArrayList pars_list = (ArrayList) $1;
950 Parameter [] pars = new Parameter [pars_list.Count];
951 pars_list.CopyTo (pars);
953 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
957 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
964 ArrayList pars = new ArrayList (4);
969 | fixed_parameters COMMA fixed_parameter
971 ArrayList pars = (ArrayList) $1;
980 opt_parameter_modifier
984 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
987 opt_parameter_modifier
990 CheckIdentifierToken (yyToken);
995 opt_parameter_modifier
996 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1001 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1002 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1006 : opt_attributes PARAMS type IDENTIFIER
1008 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1009 note ("type must be a single-dimension array type");
1011 | opt_attributes PARAMS type error {
1012 CheckIdentifierToken (yyToken);
1018 : qualified_identifier { $$ = ($1).ToString (); }
1021 property_declaration
1027 implicit_value_parameter_type = (Expression) $3;
1029 lexer.PropertyParsing = true;
1031 $$ = lexer.Location;
1033 accessor_declarations
1035 lexer.PropertyParsing = false;
1040 Pair pair = (Pair) $7;
1041 Accessor get_block = (Accessor) pair.First;
1042 Accessor set_block = (Accessor) pair.Second;
1044 Location loc = (Location) $6;
1045 prop = new Property (current_container, (Expression) $3, (string) $4, (int) $2,
1046 get_block, set_block, (Attributes) $1, loc);
1048 CheckDef (current_container.AddProperty (prop), prop.Name, loc);
1049 implicit_value_parameter_type = null;
1053 accessor_declarations
1054 : get_accessor_declaration opt_set_accessor_declaration
1056 $$ = new Pair ($1, $2);
1058 | set_accessor_declaration opt_get_accessor_declaration
1060 $$ = new Pair ($2, $1);
1064 opt_get_accessor_declaration
1065 : /* empty */ { $$ = null; }
1066 | get_accessor_declaration
1069 opt_set_accessor_declaration
1070 : /* empty */ { $$ = null; }
1071 | set_accessor_declaration
1074 get_accessor_declaration
1075 : opt_attributes GET
1077 // If this is not the case, then current_local_parameters has already
1078 // been set in indexer_declaration
1079 if (parsing_indexer == false)
1080 current_local_parameters = null;
1082 current_local_parameters = indexer_parameters;
1083 lexer.PropertyParsing = false;
1087 $$ = new Accessor ((Block) $4, (Attributes) $1);
1088 current_local_parameters = null;
1089 lexer.PropertyParsing = true;
1093 set_accessor_declaration
1094 : opt_attributes SET
1097 Parameter implicit_value_parameter = new Parameter (
1098 implicit_value_parameter_type, "value",
1099 Parameter.Modifier.NONE, null);
1101 if (parsing_indexer == false) {
1102 args = new Parameter [1];
1103 args [0] = implicit_value_parameter;
1104 current_local_parameters = new Parameters (args, null, lexer.Location);
1106 Parameter [] fpars = indexer_parameters.FixedParameters;
1109 int count = fpars.Length;
1111 args = new Parameter [count + 1];
1112 fpars.CopyTo (args, 0);
1113 args [count] = implicit_value_parameter;
1116 current_local_parameters = new Parameters (
1117 args, indexer_parameters.ArrayParameter, lexer.Location);
1120 lexer.PropertyParsing = false;
1124 $$ = new Accessor ((Block) $4, (Attributes) $1);
1125 current_local_parameters = null;
1126 lexer.PropertyParsing = true;
1132 | SEMICOLON { $$ = null; }
1135 interface_declaration
1138 INTERFACE IDENTIFIER
1140 Interface new_interface;
1141 string full_interface_name = MakeName ((string) $4);
1143 new_interface = new Interface (current_namespace, current_container, full_interface_name,
1144 (int) $2, (Attributes) $1, lexer.Location);
1145 if (current_interface != null) {
1146 Location l = lexer.Location;
1147 Report.Error (-2, l, "Internal compiler error: interface inside interface");
1149 current_interface = new_interface;
1150 RootContext.Tree.RecordDecl (full_interface_name, new_interface);
1153 interface_body opt_semicolon
1155 Interface new_interface = (Interface) current_interface;
1158 new_interface.Bases = (ArrayList) $6;
1160 current_interface = null;
1161 CheckDef (current_container.AddInterface (new_interface),
1162 new_interface.Name, new_interface.Location);
1164 | opt_attributes opt_modifiers INTERFACE error {
1165 CheckIdentifierToken (yyToken);
1170 : /* empty */ { $$ = null; }
1175 : COLON interface_type_list { $$ = $2; }
1181 ArrayList interfaces = new ArrayList (4);
1183 interfaces.Add (($1).ToString ());
1186 | interface_type_list COMMA interface_type
1188 ArrayList interfaces = (ArrayList) $1;
1189 interfaces.Add (($3).ToString ());
1196 opt_interface_member_declarations
1200 opt_interface_member_declarations
1202 | interface_member_declarations
1205 interface_member_declarations
1206 : interface_member_declaration
1207 | interface_member_declarations interface_member_declaration
1210 interface_member_declaration
1211 : interface_method_declaration
1213 InterfaceMethod m = (InterfaceMethod) $1;
1215 CheckDef (current_interface.AddMethod (m), m.Name, m.Location);
1217 | interface_property_declaration
1219 InterfaceProperty p = (InterfaceProperty) $1;
1221 CheckDef (current_interface.AddProperty (p), p.Name, p.Location);
1223 | interface_event_declaration
1226 InterfaceEvent e = (InterfaceEvent) $1;
1227 CheckDef (current_interface.AddEvent (e), e.Name, lexer.Location);
1230 | interface_indexer_declaration
1232 InterfaceIndexer i = (InterfaceIndexer) $1;
1234 CheckDef (current_interface.AddIndexer (i), "indexer", i.Location);
1243 val = Modifiers.Check (Modifiers.NEW, val, 0, lexer.Location);
1244 $$ = (bool) (val != 0);
1248 interface_method_declaration
1249 : opt_attributes opt_new type IDENTIFIER
1250 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1253 $$ = new InterfaceMethod ((Expression) $3, (string) $4, (bool) $2,
1254 (Parameters) $6, (Attributes) $1, lexer.Location);
1256 | opt_attributes opt_new VOID IDENTIFIER
1257 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1260 $$ = new InterfaceMethod (
1261 TypeManager.system_void_expr, (string) $4, (bool) $2, (Parameters) $6,
1262 (Attributes) $1, lexer.Location);
1266 interface_property_declaration
1271 { lexer.PropertyParsing = true; }
1273 { lexer.PropertyParsing = false; }
1278 $$ = new InterfaceProperty ((Expression) $3, (string) $4, (bool) $2,
1279 (gs & 1) == 1, (gs & 2) == 2, (Attributes) $1,
1285 CheckIdentifierToken (yyToken);
1291 : opt_attributes GET SEMICOLON { $$ = 1; }
1292 | opt_attributes SET SEMICOLON { $$ = 2; }
1293 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1295 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1299 interface_event_declaration
1300 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1302 $$ = new InterfaceEvent ((Expression) $4, (string) $5, (bool) $2, (Attributes) $1,
1305 | opt_attributes opt_new EVENT type error {
1306 CheckIdentifierToken (yyToken);
1309 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1310 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1313 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE event_accessor_declarations CLOSE_BRACE {
1314 Report.Error (69, lexer.Location, "Event accessors not valid on interfaces");
1319 interface_indexer_declaration
1320 : opt_attributes opt_new type THIS
1321 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1323 { lexer.PropertyParsing = true; }
1325 { lexer.PropertyParsing = false; }
1328 int a_flags = (int) $10;
1330 bool do_get = (a_flags & 1) == 1;
1331 bool do_set = (a_flags & 2) == 2;
1333 $$ = new InterfaceIndexer ((Expression) $3, (Parameters) $6, do_get, do_set,
1334 (bool) $2, (Attributes) $1, lexer.Location);
1338 operator_declaration
1339 : opt_attributes opt_modifiers operator_declarator operator_body
1341 OperatorDeclaration decl = (OperatorDeclaration) $3;
1343 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
1344 decl.arg2type, decl.arg2name, (Block) $4, (Attributes) $1, decl.location);
1346 // Note again, checking is done in semantic analysis
1347 current_container.AddOperator (op);
1349 current_local_parameters = null;
1355 | SEMICOLON { $$ = null; }
1358 : type OPERATOR overloadable_operator
1359 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1361 Operator.OpType op = (Operator.OpType) $3;
1362 CheckUnaryOperator (op);
1364 if (op == Operator.OpType.Addition)
1365 op = Operator.OpType.UnaryPlus;
1367 if (op == Operator.OpType.Subtraction)
1368 op = Operator.OpType.UnaryNegation;
1370 Parameter [] pars = new Parameter [1];
1372 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1374 current_local_parameters = new Parameters (pars, null, lexer.Location);
1376 $$ = new OperatorDeclaration (op, (Expression) $1, (Expression) $5, (string) $6,
1377 null, null, lexer.Location);
1379 | type OPERATOR overloadable_operator
1381 type IDENTIFIER COMMA
1385 CheckBinaryOperator ((Operator.OpType) $3);
1387 Parameter [] pars = new Parameter [2];
1389 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1390 pars [1] = new Parameter ((Expression) $8, (string) $9, Parameter.Modifier.NONE, null);
1392 current_local_parameters = new Parameters (pars, null, lexer.Location);
1394 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1395 (Expression) $5, (string) $6,
1396 (Expression) $8, (string) $9, lexer.Location);
1398 | conversion_operator_declarator
1401 overloadable_operator
1403 : BANG { $$ = Operator.OpType.LogicalNot; }
1404 | TILDE { $$ = Operator.OpType.OnesComplement; }
1405 | OP_INC { $$ = Operator.OpType.Increment; }
1406 | OP_DEC { $$ = Operator.OpType.Decrement; }
1407 | TRUE { $$ = Operator.OpType.True; }
1408 | FALSE { $$ = Operator.OpType.False; }
1409 // Unary and binary:
1410 | PLUS { $$ = Operator.OpType.Addition; }
1411 | MINUS { $$ = Operator.OpType.Subtraction; }
1413 | STAR { $$ = Operator.OpType.Multiply; }
1414 | DIV { $$ = Operator.OpType.Division; }
1415 | PERCENT { $$ = Operator.OpType.Modulus; }
1416 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1417 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1418 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1419 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1420 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1421 | OP_EQ { $$ = Operator.OpType.Equality; }
1422 | OP_NE { $$ = Operator.OpType.Inequality; }
1423 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1424 | OP_LT { $$ = Operator.OpType.LessThan; }
1425 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1426 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1429 conversion_operator_declarator
1430 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1432 Parameter [] pars = new Parameter [1];
1434 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1436 current_local_parameters = new Parameters (pars, null, lexer.Location);
1438 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1439 null, null, lexer.Location);
1441 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1443 Parameter [] pars = new Parameter [1];
1445 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1447 current_local_parameters = new Parameters (pars, null, lexer.Location);
1449 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1450 null, null, lexer.Location);
1454 syntax_error (lexer.Location, "'operator' expected");
1458 syntax_error (lexer.Location, "'operator' expected");
1462 constructor_declaration
1465 constructor_declarator
1468 Constructor c = (Constructor) $3;
1469 c.Block = (Block) $4;
1470 c.OptAttributes = (Attributes) $1;
1471 c.ModFlags = (int) $2;
1473 if (c.Name == current_container.Basename){
1474 if ((c.ModFlags & Modifiers.STATIC) != 0){
1475 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1477 515, c.Location, String.Format (
1478 "`{0}.{1}': static constructor can not have access modifiers",
1479 c.Name, current_container.Name));
1482 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1484 if (c.Initializer != null){
1487 "Static constructors can not have an explicit this or base " +
1488 "constructor invocations");
1491 if (!c.Parameters.Empty){
1493 132, c.Location, "Static constructors should not have parameters");
1496 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1499 // We let another layer check the validity of the constructor.
1500 Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1503 CheckDef (current_container.AddConstructor (c), c.Name, c.Location);
1505 current_local_parameters = null;
1509 constructor_declarator
1511 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1513 oob_stack.Push (lexer.Location);
1515 current_local_parameters = (Parameters) $3;
1517 opt_constructor_initializer
1519 Location l = (Location) oob_stack.Pop ();
1520 $$ = new Constructor (current_container, (string) $1, (Parameters) $3,
1521 (ConstructorInitializer) $6, l);
1527 | SEMICOLON { $$ = null; }
1530 opt_constructor_initializer
1531 : /* empty */ { $$ = null; }
1532 | constructor_initializer
1535 constructor_initializer
1536 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1538 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1540 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1542 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1545 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1551 : /* EMPTY */ { $$ = 0; }
1552 | UNSAFE { $$ = Modifiers.UNSAFE; }
1553 | EXTERN { $$ = Modifiers.EXTERN; }
1556 destructor_declaration
1557 : opt_attributes opt_finalizer TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1559 if ((string) $4 != current_container.Basename){
1560 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1561 } else if (!(current_container is Class)){
1562 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1564 Location l = lexer.Location;
1567 if (!RootContext.StdLib && current_container.Name == "System.Object")
1568 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
1570 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
1572 if ((m & Modifiers.UNSAFE) != 0){
1573 if (!RootContext.Unsafe){
1574 Report.Error (227, l,
1575 "Unsafe code requires the --unsafe command " +
1576 "line option to be specified");
1580 Method d = new Method (
1581 current_container, TypeManager.system_void_expr, m, "Finalize",
1582 new Parameters (null, null, l), (Attributes) $1, l);
1584 d.Block = (Block) $7;
1585 CheckDef (current_container.AddMethod (d), d.Name, d.Location);
1593 EVENT type variable_declarators SEMICOLON
1595 foreach (VariableDeclaration var in (ArrayList) $5) {
1597 Event e = new Event ((Expression) $4, var.identifier,
1598 var.expression_or_array_initializer,
1599 (int) $2, null, null, (Attributes) $1, lexer.Location);
1601 CheckDef (current_container.AddEvent (e), e.Name, e.Location);
1607 EVENT type member_name
1610 implicit_value_parameter_type = (Expression) $4;
1611 lexer.EventParsing = true;
1612 oob_stack.Push (lexer.Location);
1614 event_accessor_declarations
1616 lexer.EventParsing = false;
1620 Location loc = (Location) oob_stack.Pop ();
1623 Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
1626 Pair pair = (Pair) $8;
1627 Accessor add_accessor = null;
1628 Accessor rem_accessor = null;
1630 if (pair.First != null)
1631 add_accessor = (Accessor) pair.First;
1632 if (pair.Second != null)
1633 rem_accessor = (Accessor) pair.Second;
1635 Event e = new Event ((Expression) $4, (string) $5, null, (int) $2, add_accessor, rem_accessor,
1636 (Attributes) $1, loc);
1638 CheckDef (current_container.AddEvent (e), e.Name, loc);
1639 implicit_value_parameter_type = null;
1644 event_accessor_declarations
1645 : add_accessor_declaration remove_accessor_declaration
1647 $$ = new Pair ($1, $2);
1649 | remove_accessor_declaration add_accessor_declaration
1651 $$ = new Pair ($2, $1);
1653 | add_accessor_declaration { $$ = null; }
1654 | remove_accessor_declaration { $$ = null; }
1657 add_accessor_declaration
1658 : opt_attributes ADD
1660 Parameter [] args = new Parameter [1];
1661 Parameter implicit_value_parameter = new Parameter (
1662 implicit_value_parameter_type, "value",
1663 Parameter.Modifier.NONE, null);
1665 args [0] = implicit_value_parameter;
1667 current_local_parameters = new Parameters (args, null, lexer.Location);
1668 lexer.EventParsing = false;
1672 $$ = new Accessor ((Block) $4, (Attributes) $1);
1673 lexer.EventParsing = true;
1675 | opt_attributes ADD error {
1676 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1681 remove_accessor_declaration
1682 : opt_attributes REMOVE
1684 Parameter [] args = new Parameter [1];
1685 Parameter implicit_value_parameter = new Parameter (
1686 implicit_value_parameter_type, "value",
1687 Parameter.Modifier.NONE, null);
1689 args [0] = implicit_value_parameter;
1691 current_local_parameters = new Parameters (args, null, lexer.Location);
1692 lexer.EventParsing = false;
1696 $$ = new Accessor ((Block) $4, (Attributes) $1);
1697 lexer.EventParsing = true;
1699 | opt_attributes REMOVE error {
1700 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1706 : opt_attributes opt_modifiers indexer_declarator
1709 IndexerDeclaration decl = (IndexerDeclaration) $3;
1711 implicit_value_parameter_type = decl.type;
1713 lexer.PropertyParsing = true;
1714 parsing_indexer = true;
1716 indexer_parameters = decl.param_list;
1717 oob_stack.Push (lexer.Location);
1719 accessor_declarations
1721 lexer.PropertyParsing = false;
1722 parsing_indexer = false;
1726 // The signature is computed from the signature of the indexer. Look
1727 // at section 3.6 on the spec
1728 Location loc = (Location) oob_stack.Pop ();
1730 IndexerDeclaration decl = (IndexerDeclaration) $3;
1731 Pair pair = (Pair) $6;
1732 Accessor get_block = (Accessor) pair.First;
1733 Accessor set_block = (Accessor) pair.Second;
1735 indexer = new Indexer (current_container, decl.type, decl.interface_type, (int) $2,
1736 decl.param_list, get_block, set_block, (Attributes) $1, loc);
1738 // Note that there is no equivalent of CheckDef for this case
1739 // We shall handle this in semantic analysis
1741 current_container.AddIndexer (indexer);
1743 current_local_parameters = null;
1744 implicit_value_parameter_type = null;
1745 indexer_parameters = null;
1750 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1752 Parameters pars = (Parameters) $4;
1754 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1755 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1758 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
1760 | type qualified_identifier DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1762 Parameters pars = (Parameters) $6;
1764 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1765 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1767 $$ = new IndexerDeclaration ((Expression) $1, (string) ($2).ToString (), pars);
1779 Location enum_location = lexer.Location;
1781 string full_name = MakeName ((string) $4);
1782 Enum e = new Enum (current_namespace, current_container, (Expression) $5, (int) $2,
1783 full_name, (Attributes) $1, enum_location);
1785 foreach (VariableDeclaration ev in (ArrayList) $6) {
1786 Location loc = (Location) ev.Location;
1788 CheckDef (e.AddEnumMember (ev.identifier,
1789 (Expression) ev.expression_or_array_initializer,
1790 loc, ev.OptAttributes),
1791 ev.identifier, loc);
1794 CheckDef (current_container.AddEnum (e), full_name, enum_location);
1795 RootContext.Tree.RecordDecl (full_name, e);
1801 : /* empty */ { $$ = TypeManager.system_int32_expr; }
1802 | COLON type { $$ = $2; }
1806 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1812 opt_enum_member_declarations
1813 : /* empty */ { $$ = new ArrayList (4); }
1814 | enum_member_declarations opt_comma { $$ = $1; }
1817 enum_member_declarations
1818 : enum_member_declaration
1820 ArrayList l = new ArrayList (4);
1825 | enum_member_declarations COMMA enum_member_declaration
1827 ArrayList l = (ArrayList) $1;
1835 enum_member_declaration
1836 : opt_attributes IDENTIFIER
1838 $$ = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
1840 | opt_attributes IDENTIFIER
1842 $$ = lexer.Location;
1846 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
1850 delegate_declaration
1854 IDENTIFIER OPEN_PARENS
1855 opt_formal_parameter_list
1859 Location l = lexer.Location;
1860 Delegate del = new Delegate (current_namespace, current_container, (Expression) $4,
1861 (int) $2, MakeName ((string) $5), (Parameters) $7,
1862 (Attributes) $1, l);
1864 CheckDef (current_container.AddDelegate (del), del.Name, l);
1869 IDENTIFIER OPEN_PARENS
1870 opt_formal_parameter_list
1874 Location l = lexer.Location;
1875 Delegate del = new Delegate (
1876 current_namespace, current_container,
1877 TypeManager.system_void_expr, (int) $2, MakeName ((string) $5),
1878 (Parameters) $7, (Attributes) $1, l);
1880 CheckDef (current_container.AddDelegate (del), del.Name, l);
1885 : namespace_or_type_name
1888 namespace_or_type_name
1889 : qualified_identifier
1893 * Before you think of adding a return_type, notice that we have been
1894 * using two rules in the places where it matters (one rule using type
1895 * and another identical one that uses VOID as the return type). This
1896 * gets rid of a shift/reduce couple
1899 : type_name { /* class_type */
1901 This does interfaces, delegates, struct_types, class_types,
1902 parent classes, and more! 4.2
1904 $$ = DecomposeQI ((QualifiedIdentifier) $1, lexer.Location);
1916 // Note that here only unmanaged types are allowed but we
1917 // can't perform checks during this phase - we do it during
1918 // semantic analysis.
1920 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1924 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
1930 | non_expression_type rank_specifier
1932 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1934 | non_expression_type STAR
1936 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1938 | expression rank_specifiers
1940 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1944 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1948 // We need this because the parser will happily go and reduce IDENTIFIER STAR
1949 // through this different path
1951 | multiplicative_expression STAR
1953 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1960 ArrayList types = new ArrayList (4);
1965 | type_list COMMA type
1967 ArrayList types = (ArrayList) $1;
1975 * replaces all the productions for isolating the various
1976 * simple types, but we need this to reuse it easily in local_variable_type
1979 : OBJECT { $$ = TypeManager.system_object_expr; }
1980 | STRING { $$ = TypeManager.system_string_expr; }
1981 | BOOL { $$ = TypeManager.system_boolean_expr; }
1982 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
1983 | FLOAT { $$ = TypeManager.system_single_expr; }
1984 | DOUBLE { $$ = TypeManager.system_double_expr; }
1989 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
1990 | BYTE { $$ = TypeManager.system_byte_expr; }
1991 | SHORT { $$ = TypeManager.system_int16_expr; }
1992 | USHORT { $$ = TypeManager.system_uint16_expr; }
1993 | INT { $$ = TypeManager.system_int32_expr; }
1994 | UINT { $$ = TypeManager.system_uint32_expr; }
1995 | LONG { $$ = TypeManager.system_int64_expr; }
1996 | ULONG { $$ = TypeManager.system_uint64_expr; }
1997 | CHAR { $$ = TypeManager.system_char_expr; }
1998 | VOID { $$ = TypeManager.system_void_expr; }
2006 : type rank_specifiers
2008 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2013 // Expressions, section 7.5
2021 | IDENTIFIER { $$ = new SimpleName ((string) $1, lexer.Location); }
2022 | parenthesized_expression
2024 | invocation_expression
2028 | post_increment_expression
2029 | post_decrement_expression
2033 | checked_expression
2034 | unchecked_expression
2035 | pointer_member_access
2036 | anonymous_method_expression
2043 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2044 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2045 | NULL { $$ = NullLiteral.Null; }
2049 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2050 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2051 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2056 object v = lexer.Value;
2062 $$ = IntLiteral.Zero;
2064 $$ = IntLiteral.One;
2066 $$ = new IntLiteral (i);
2067 } else if (v is uint)
2068 $$ = new UIntLiteral ((UInt32) v);
2070 $$ = new LongLiteral ((Int64) v);
2071 else if (v is ulong)
2072 $$ = new ULongLiteral ((UInt64) v);
2074 Console.WriteLine ("OOPS. Unexpected result from scanner");
2079 : TRUE { $$ = new BoolLiteral (true); }
2080 | FALSE { $$ = new BoolLiteral (false); }
2083 parenthesized_expression_0
2084 : OPEN_PARENS expression CLOSE_PARENS
2087 lexer.Deambiguate_CloseParens ();
2088 // After this, the next token returned is one of
2089 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2090 // or CLOSE_PARENS_MINUS.
2094 parenthesized_expression
2095 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2099 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2101 // If a parenthesized expression is followed by a minus, we need to wrap
2102 // the expression inside a ParenthesizedExpression for the CS0075 check
2103 // in Binary.DoResolve().
2104 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2109 : primary_expression DOT IDENTIFIER
2111 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2113 | predefined_type DOT IDENTIFIER
2115 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2123 invocation_expression
2124 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2127 Location l = lexer.Location;
2128 Report.Error (1, l, "Parse error");
2130 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2132 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2134 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2136 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2138 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2143 : /* empty */ { $$ = null; }
2150 ArrayList list = new ArrayList (4);
2154 | argument_list COMMA argument
2156 ArrayList list = (ArrayList) $1;
2160 | argument_list error {
2161 CheckToken (1026, yyToken, ", or ) expected");
2168 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2170 | REF variable_reference
2172 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2174 | OUT variable_reference
2176 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2181 : expression { note ("section 5.4"); $$ = $1; }
2185 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2187 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2189 | primary_expression rank_specifiers
2191 // So the super-trick is that primary_expression
2192 // can only be either a SimpleName or a MemberAccess.
2193 // The MemberAccess case arises when you have a fully qualified type-name like :
2195 // SimpleName is when you have
2198 Expression expr = (Expression) $1;
2199 if (expr is ComposedCast){
2200 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2201 } else if (!(expr is SimpleName || expr is MemberAccess)){
2202 Error_ExpectingTypeName (lexer.Location, expr);
2203 $$ = TypeManager.system_object_expr;
2206 // So we extract the string corresponding to the SimpleName
2209 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2217 ArrayList list = new ArrayList (4);
2221 | expression_list COMMA expression
2223 ArrayList list = (ArrayList) $1;
2232 $$ = new This (current_block, lexer.Location);
2237 : BASE DOT IDENTIFIER
2239 $$ = new BaseAccess ((string) $3, lexer.Location);
2241 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2243 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2246 Report.Error (175, "Use of keyword `base' is not valid in this context");
2251 post_increment_expression
2252 : primary_expression OP_INC
2254 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2255 (Expression) $1, lexer.Location);
2259 post_decrement_expression
2260 : primary_expression OP_DEC
2262 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2263 (Expression) $1, lexer.Location);
2268 : object_or_delegate_creation_expression
2269 | array_creation_expression
2272 object_or_delegate_creation_expression
2273 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2275 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2279 array_creation_expression
2280 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2282 opt_array_initializer
2284 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2286 | NEW type rank_specifiers array_initializer
2288 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2292 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2308 : rank_specifier opt_rank_specifier
2310 $$ = (string) $2 + (string) $1;
2315 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2317 $$ = "[" + (string) $2 + "]";
2337 | dim_separators COMMA
2339 $$ = (string) $1 + ",";
2343 opt_array_initializer
2355 : OPEN_BRACE CLOSE_BRACE
2357 ArrayList list = new ArrayList (4);
2360 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2362 $$ = (ArrayList) $2;
2366 variable_initializer_list
2367 : variable_initializer
2369 ArrayList list = new ArrayList (4);
2373 | variable_initializer_list COMMA variable_initializer
2375 ArrayList list = (ArrayList) $1;
2382 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2384 $$ = new TypeOfVoid (lexer.Location);
2386 | TYPEOF OPEN_PARENS type CLOSE_PARENS
2388 $$ = new TypeOf ((Expression) $3, lexer.Location);
2393 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2394 $$ = new SizeOf ((Expression) $3, lexer.Location);
2399 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2401 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2405 unchecked_expression
2406 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2408 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2412 pointer_member_access
2413 : primary_expression OP_PTR IDENTIFIER
2417 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2418 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2422 anonymous_method_expression
2423 : DELEGATE opt_anonymous_method_signature {
2424 oob_stack.Push (current_local_parameters);
2425 current_local_parameters = (Parameters)$2;
2427 if (!RootContext.V2){
2428 Report.Error (-213, lexer.Location, "Anonymous methods are only supported in V2");
2431 $$ = new AnonymousMethod ((Parameters) $2, (Block) $4, lexer.Location);
2432 current_local_parameters = (Parameters) oob_stack.Pop ();
2436 opt_anonymous_method_signature
2437 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
2438 | anonymous_method_signature
2441 anonymous_method_signature
2442 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
2445 $$ = Parameters.EmptyReadOnlyParameters;
2447 ArrayList par_list = (ArrayList) $2;
2448 Parameter [] pars = new Parameter [par_list.Count];
2449 par_list.CopyTo (pars);
2450 $$ = new Parameters (pars, null, lexer.Location);
2455 opt_anonymous_method_parameter_list
2456 : /* empty */ { $$ = null; }
2457 | anonymous_method_parameter_list { $$ = $1; }
2460 anonymous_method_parameter_list
2461 : anonymous_method_parameter
2463 ArrayList a = new ArrayList (4);
2467 | anonymous_method_parameter_list COMMA anonymous_method_parameter
2469 ArrayList a = (ArrayList) $1;
2475 anonymous_method_parameter
2476 : opt_parameter_modifier type IDENTIFIER {
2477 $$ = new Parameter ((Expression) $2, (string) $2, (Parameter.Modifier) $1, null);
2482 : primary_expression
2483 | BANG prefixed_unary_expression
2485 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2487 | TILDE prefixed_unary_expression
2489 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2495 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
2497 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2499 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
2501 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2507 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2509 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2514 // The idea to split this out is from Rhys' grammar
2515 // to solve the problem with casts.
2517 prefixed_unary_expression
2519 | PLUS prefixed_unary_expression
2521 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2523 | MINUS prefixed_unary_expression
2525 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2527 | OP_INC prefixed_unary_expression
2529 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2530 (Expression) $2, lexer.Location);
2532 | OP_DEC prefixed_unary_expression
2534 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2535 (Expression) $2, lexer.Location);
2537 | STAR prefixed_unary_expression
2539 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2541 | BITWISE_AND prefixed_unary_expression
2543 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2547 pre_increment_expression
2548 : OP_INC prefixed_unary_expression
2550 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2551 (Expression) $2, lexer.Location);
2555 pre_decrement_expression
2556 : OP_DEC prefixed_unary_expression
2558 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2559 (Expression) $2, lexer.Location);
2563 multiplicative_expression
2564 : prefixed_unary_expression
2565 | multiplicative_expression STAR prefixed_unary_expression
2567 $$ = new Binary (Binary.Operator.Multiply,
2568 (Expression) $1, (Expression) $3, lexer.Location);
2570 | multiplicative_expression DIV prefixed_unary_expression
2572 $$ = new Binary (Binary.Operator.Division,
2573 (Expression) $1, (Expression) $3, lexer.Location);
2575 | multiplicative_expression PERCENT prefixed_unary_expression
2577 $$ = new Binary (Binary.Operator.Modulus,
2578 (Expression) $1, (Expression) $3, lexer.Location);
2583 : multiplicative_expression
2584 | additive_expression PLUS multiplicative_expression
2586 $$ = new Binary (Binary.Operator.Addition,
2587 (Expression) $1, (Expression) $3, lexer.Location);
2589 | additive_expression MINUS multiplicative_expression
2591 $$ = new Binary (Binary.Operator.Subtraction,
2592 (Expression) $1, (Expression) $3, lexer.Location);
2597 : additive_expression
2598 | shift_expression OP_SHIFT_LEFT additive_expression
2600 $$ = new Binary (Binary.Operator.LeftShift,
2601 (Expression) $1, (Expression) $3, lexer.Location);
2603 | shift_expression OP_SHIFT_RIGHT additive_expression
2605 $$ = new Binary (Binary.Operator.RightShift,
2606 (Expression) $1, (Expression) $3, lexer.Location);
2610 relational_expression
2612 | relational_expression OP_LT shift_expression
2614 $$ = new Binary (Binary.Operator.LessThan,
2615 (Expression) $1, (Expression) $3, lexer.Location);
2617 | relational_expression OP_GT shift_expression
2619 $$ = new Binary (Binary.Operator.GreaterThan,
2620 (Expression) $1, (Expression) $3, lexer.Location);
2622 | relational_expression OP_LE shift_expression
2624 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2625 (Expression) $1, (Expression) $3, lexer.Location);
2627 | relational_expression OP_GE shift_expression
2629 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2630 (Expression) $1, (Expression) $3, lexer.Location);
2632 | relational_expression IS type
2634 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
2636 | relational_expression AS type
2638 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
2643 : relational_expression
2644 | equality_expression OP_EQ relational_expression
2646 $$ = new Binary (Binary.Operator.Equality,
2647 (Expression) $1, (Expression) $3, lexer.Location);
2649 | equality_expression OP_NE relational_expression
2651 $$ = new Binary (Binary.Operator.Inequality,
2652 (Expression) $1, (Expression) $3, lexer.Location);
2657 : equality_expression
2658 | and_expression BITWISE_AND equality_expression
2660 $$ = new Binary (Binary.Operator.BitwiseAnd,
2661 (Expression) $1, (Expression) $3, lexer.Location);
2665 exclusive_or_expression
2667 | exclusive_or_expression CARRET and_expression
2669 $$ = new Binary (Binary.Operator.ExclusiveOr,
2670 (Expression) $1, (Expression) $3, lexer.Location);
2674 inclusive_or_expression
2675 : exclusive_or_expression
2676 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2678 $$ = new Binary (Binary.Operator.BitwiseOr,
2679 (Expression) $1, (Expression) $3, lexer.Location);
2683 conditional_and_expression
2684 : inclusive_or_expression
2685 | conditional_and_expression OP_AND inclusive_or_expression
2687 $$ = new Binary (Binary.Operator.LogicalAnd,
2688 (Expression) $1, (Expression) $3, lexer.Location);
2692 conditional_or_expression
2693 : conditional_and_expression
2694 | conditional_or_expression OP_OR conditional_and_expression
2696 $$ = new Binary (Binary.Operator.LogicalOr,
2697 (Expression) $1, (Expression) $3, lexer.Location);
2701 conditional_expression
2702 : conditional_or_expression
2703 | conditional_or_expression INTERR expression COLON expression
2705 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2709 assignment_expression
2710 : prefixed_unary_expression ASSIGN expression
2712 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2714 | prefixed_unary_expression OP_MULT_ASSIGN expression
2716 Location l = lexer.Location;
2718 $$ = new CompoundAssign (
2719 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
2721 | prefixed_unary_expression OP_DIV_ASSIGN expression
2723 Location l = lexer.Location;
2725 $$ = new CompoundAssign (
2726 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
2728 | prefixed_unary_expression OP_MOD_ASSIGN expression
2730 Location l = lexer.Location;
2732 $$ = new CompoundAssign (
2733 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
2735 | prefixed_unary_expression OP_ADD_ASSIGN expression
2737 Location l = lexer.Location;
2739 $$ = new CompoundAssign (
2740 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
2742 | prefixed_unary_expression OP_SUB_ASSIGN expression
2744 Location l = lexer.Location;
2746 $$ = new CompoundAssign (
2747 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
2749 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2751 Location l = lexer.Location;
2753 $$ = new CompoundAssign (
2754 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
2756 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2758 Location l = lexer.Location;
2760 $$ = new CompoundAssign (
2761 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
2763 | prefixed_unary_expression OP_AND_ASSIGN expression
2765 Location l = lexer.Location;
2767 $$ = new CompoundAssign (
2768 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
2770 | prefixed_unary_expression OP_OR_ASSIGN expression
2772 Location l = lexer.Location;
2774 $$ = new CompoundAssign (
2775 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
2777 | prefixed_unary_expression OP_XOR_ASSIGN expression
2779 Location l = lexer.Location;
2781 $$ = new CompoundAssign (
2782 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
2787 : conditional_expression
2788 | assignment_expression
2810 name = MakeName ((string) $4);
2812 new_class = new Class (current_namespace, current_container, name, (int) $2,
2813 (Attributes) $1, lexer.Location);
2814 current_container = new_class;
2815 RootContext.Tree.RecordDecl (name, new_class);
2821 Class new_class = (Class) current_container;
2824 new_class.Bases = (ArrayList) $6;
2826 current_container = current_container.Parent;
2827 CheckDef (current_container.AddClass (new_class), new_class.Name, new_class.Location);
2834 : /* empty */ { $$ = (int) 0; }
2840 | modifiers modifier
2845 if ((m1 & m2) != 0) {
2846 Location l = lexer.Location;
2847 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2849 $$ = (int) (m1 | m2);
2854 : NEW { $$ = Modifiers.NEW; }
2855 | PUBLIC { $$ = Modifiers.PUBLIC; }
2856 | PROTECTED { $$ = Modifiers.PROTECTED; }
2857 | INTERNAL { $$ = Modifiers.INTERNAL; }
2858 | PRIVATE { $$ = Modifiers.PRIVATE; }
2859 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2860 | SEALED { $$ = Modifiers.SEALED; }
2861 | STATIC { $$ = Modifiers.STATIC; }
2862 | READONLY { $$ = Modifiers.READONLY; }
2863 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2864 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2865 | EXTERN { $$ = Modifiers.EXTERN; }
2866 | VOLATILE { $$ = Modifiers.VOLATILE; }
2867 | UNSAFE { $$ = Modifiers.UNSAFE; }
2871 : /* empty */ { $$ = null; }
2872 | class_base { $$ = $1; }
2876 : COLON type_list { $$ = $2; }
2884 // A block is "contained" on the following places:
2886 // property_declaration as part of the accessor body (get/set)
2887 // operator_declaration
2888 // constructor_declaration
2889 // destructor_declaration
2890 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2895 current_block = new Block (current_block, current_local_parameters,
2896 lexer.Location, Location.Null);
2898 opt_statement_list CLOSE_BRACE
2900 while (current_block.Implicit)
2901 current_block = current_block.Parent;
2903 current_block.SetEndLocation (lexer.Location);
2904 current_block = current_block.Parent;
2915 | statement_list statement
2919 : declaration_statement
2921 if ($1 != null && (Block) $1 != current_block){
2922 current_block.AddStatement ((Statement) $1);
2923 current_block = (Block) $1;
2926 | embedded_statement
2928 current_block.AddStatement ((Statement) $1);
2936 | expression_statement
2937 | selection_statement
2938 | iteration_statement
2942 | unchecked_statement
2952 $$ = EmptyStatement.Value;
2959 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
2961 if (!current_block.AddLabel ((string) $1, labeled)){
2962 Location l = lexer.Location;
2963 Report.Error (140, l, "The label '" + ((string) $1) + "' is a duplicate");
2965 current_block.AddStatement (labeled);
2970 declaration_statement
2971 : local_variable_declaration SEMICOLON
2974 DictionaryEntry de = (DictionaryEntry) $1;
2976 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
2980 | local_constant_declaration SEMICOLON
2983 DictionaryEntry de = (DictionaryEntry) $1;
2985 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
2991 * The following is from Rhys' grammar:
2992 * > Types in local variable declarations must be recognized as
2993 * > expressions to prevent reduce/reduce errors in the grammar.
2994 * > The expressions are converted into types during semantic analysis.
2997 : primary_expression opt_rank_specifier
2999 // FIXME: Do something smart here regarding the composition of the type.
3001 // Ok, the above "primary_expression" is there to get rid of
3002 // both reduce/reduce and shift/reduces in the grammar, it should
3003 // really just be "type_name". If you use type_name, a reduce/reduce
3004 // creeps up. If you use qualified_identifier (which is all we need
3005 // really) two shift/reduces appear.
3008 // So the super-trick is that primary_expression
3009 // can only be either a SimpleName or a MemberAccess.
3010 // The MemberAccess case arises when you have a fully qualified type-name like :
3012 // SimpleName is when you have
3015 Expression expr = (Expression) $1;
3016 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3017 Error_ExpectingTypeName (lexer.Location, expr);
3021 // So we extract the string corresponding to the SimpleName
3025 if ((string) $2 == "")
3028 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3031 | builtin_types opt_rank_specifier
3033 if ((string) $2 == "")
3036 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3040 local_variable_pointer_type
3041 : primary_expression STAR
3043 Expression expr = (Expression) $1;
3044 Location l = lexer.Location;
3046 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3047 Error_ExpectingTypeName (l, expr);
3051 $$ = new ComposedCast ((Expression) $1, "*", l);
3053 | builtin_types STAR
3055 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3059 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3061 | local_variable_pointer_type STAR
3063 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3067 local_variable_declaration
3068 : local_variable_type variable_declarators
3071 $$ = new DictionaryEntry ($1, $2);
3075 | local_variable_pointer_type opt_rank_specifier variable_declarators
3080 if ((string) $2 == "")
3081 t = (Expression) $1;
3083 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3084 $$ = new DictionaryEntry (t, $3);
3090 local_constant_declaration
3091 : CONST local_variable_type constant_declarators
3094 $$ = new DictionaryEntry ($2, $3);
3100 expression_statement
3101 : statement_expression SEMICOLON
3108 // We have to do the wrapping here and not in the case above,
3109 // because statement_expression is used for example in for_statement
3111 statement_expression
3112 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3113 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3114 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3115 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3116 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3117 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3118 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3120 Report.Error (1002, lexer.Location, "Expecting `;'");
3125 object_creation_expression
3126 : object_or_delegate_creation_expression
3127 { note ("complain if this is a delegate maybe?"); }
3136 : if_statement_open if_statement_rest
3145 oob_stack.Push (lexer.Location);
3150 : boolean_expression CLOSE_PARENS
3153 Location l = (Location) oob_stack.Pop ();
3155 $$ = new If ((Expression) $1, (Statement) $3, l);
3157 if (RootContext.WarningLevel >= 3){
3158 if ($3 == EmptyStatement.Value)
3159 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3163 | boolean_expression CLOSE_PARENS
3164 embedded_statement ELSE embedded_statement
3166 Location l = (Location) oob_stack.Pop ();
3168 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
3173 : SWITCH OPEN_PARENS
3175 oob_stack.Push (lexer.Location);
3176 switch_stack.Push (current_block);
3178 expression CLOSE_PARENS
3181 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
3182 current_block = (Block) switch_stack.Pop ();
3198 Report.Error (1522, lexer.Location, "Empty switch block");
3206 ArrayList sections = new ArrayList (4);
3211 | switch_sections switch_section
3213 ArrayList sections = (ArrayList) $1;
3223 current_block = current_block.CreateSwitchBlock (lexer.Location);
3227 Block topmost = current_block;
3229 while (topmost.Implicit)
3230 topmost = topmost.Parent;
3231 $$ = new SwitchSection ((ArrayList) $1, topmost);
3238 ArrayList labels = new ArrayList (4);
3243 | switch_labels switch_label
3245 ArrayList labels = (ArrayList) ($1);
3253 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3254 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3257 1523, lexer.Location,
3258 "The keyword case or default must precede code in switch block");
3272 oob_stack.Push (lexer.Location);
3274 boolean_expression CLOSE_PARENS embedded_statement
3276 Location l = (Location) oob_stack.Pop ();
3277 $$ = new While ((Expression) $4, (Statement) $6, l);
3279 if (RootContext.WarningLevel >= 3){
3280 if ($6 == EmptyStatement.Value)
3281 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3287 : DO embedded_statement
3289 oob_stack.Push (lexer.Location);
3291 boolean_expression CLOSE_PARENS SEMICOLON
3293 Location l = (Location) oob_stack.Pop ();
3295 $$ = new Do ((Statement) $2, (Expression) $6, l);
3301 opt_for_initializer SEMICOLON
3303 Block assign_block = new Block (current_block);
3304 current_block = assign_block;
3306 if ($3 is DictionaryEntry){
3307 DictionaryEntry de = (DictionaryEntry) $3;
3309 Expression type = (Expression) de.Key;
3310 ArrayList var_declarators = (ArrayList) de.Value;
3312 foreach (VariableDeclaration decl in var_declarators){
3316 vi = current_block.AddVariable (
3317 type, decl.identifier, current_local_parameters, decl.Location);
3321 Location l = lexer.Location;
3323 if (decl.expression_or_array_initializer is Expression){
3324 expr = (Expression) decl.expression_or_array_initializer;
3325 } else if (decl.expression_or_array_initializer == null) {
3328 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3329 expr = new ArrayCreation (type, "", init, decl.Location);
3332 LocalVariableReference var;
3333 var = new LocalVariableReference (assign_block, decl.identifier, l);
3336 Assign a = new Assign (var, expr, decl.Location);
3338 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3344 oob_stack.Push (lexer.Location);
3346 opt_for_condition SEMICOLON
3347 opt_for_iterator CLOSE_PARENS
3350 Location l = (Location) oob_stack.Pop ();
3352 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3354 if (RootContext.WarningLevel >= 3){
3355 if ($10 == EmptyStatement.Value)
3356 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3359 current_block.AddStatement (f);
3360 while (current_block.Implicit)
3361 current_block = current_block.Parent;
3363 current_block = current_block.Parent;
3368 : /* empty */ { $$ = EmptyStatement.Value; }
3373 : local_variable_declaration
3374 | statement_expression_list
3378 : /* empty */ { $$ = null; }
3379 | boolean_expression
3383 : /* empty */ { $$ = EmptyStatement.Value; }
3388 : statement_expression_list
3391 statement_expression_list
3392 : statement_expression
3394 // CHANGE: was `null'
3395 Block b = new Block (current_block, Block.Flags.Implicit);
3397 b.AddStatement ((Statement) $1);
3400 | statement_expression_list COMMA statement_expression
3402 Block b = (Block) $1;
3404 b.AddStatement ((Statement) $3);
3410 : FOREACH OPEN_PARENS type IDENTIFIER IN
3412 oob_stack.Push (lexer.Location);
3414 expression CLOSE_PARENS
3416 oob_stack.Push (current_block);
3418 Block foreach_block = new Block (current_block, Block.Flags.Implicit);
3419 LocalVariableReference v = null;
3420 Location l = lexer.Location;
3423 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3427 // Get a writable reference to this read-only variable.
3428 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3430 current_block = foreach_block;
3433 oob_stack.Push (current_block);
3437 Block foreach_block = (Block) oob_stack.Pop ();
3438 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3439 Block prev_block = (Block) oob_stack.Pop ();
3440 Location l = (Location) oob_stack.Pop ();
3442 current_block = prev_block;
3445 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3446 foreach_block.AddStatement (f);
3455 | continue_statement
3465 $$ = new Break (lexer.Location);
3470 : CONTINUE SEMICOLON
3472 $$ = new Continue (lexer.Location);
3477 : GOTO IDENTIFIER SEMICOLON
3479 $$ = new Goto (current_block, (string) $2, lexer.Location);
3481 | GOTO CASE constant_expression SEMICOLON
3483 $$ = new GotoCase ((Expression) $3, lexer.Location);
3485 | GOTO DEFAULT SEMICOLON
3487 $$ = new GotoDefault (lexer.Location);
3492 : RETURN opt_expression SEMICOLON
3494 $$ = new Return ((Expression) $2, lexer.Location);
3499 : THROW opt_expression SEMICOLON
3501 $$ = new Throw ((Expression) $2, lexer.Location);
3506 : YIELD RETURN expression SEMICOLON
3508 if (iterator_container == null){
3509 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3512 iterator_container.SetYields ();
3513 $$ = new Yield ((Expression) $3, lexer.Location);
3516 | YIELD expression SEMICOLON
3518 if (iterator_container == null){
3519 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3522 iterator_container.SetYields ();
3523 $$ = new Yield ((Expression) $2, lexer.Location);
3526 | YIELD BREAK SEMICOLON
3528 if (iterator_container == null){
3529 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3532 iterator_container.SetYields ();
3533 $$ = new YieldBreak (lexer.Location);
3544 : TRY block catch_clauses
3547 ArrayList s = new ArrayList (4);
3549 foreach (Catch cc in (ArrayList) $3) {
3556 // Now s contains the list of specific catch clauses
3557 // and g contains the general one.
3559 $$ = new Try ((Block) $2, s, g, null, lexer.Location);
3561 | TRY block opt_catch_clauses FINALLY block
3564 ArrayList s = new ArrayList (4);
3565 ArrayList catch_list = (ArrayList) $3;
3567 if (catch_list != null){
3568 foreach (Catch cc in catch_list) {
3576 $$ = new Try ((Block) $2, s, g, (Block) $5, lexer.Location);
3580 Report.Error (1524, lexer.Location, "Expected catch or finally");
3585 : /* empty */ { $$ = null; }
3592 ArrayList l = new ArrayList (4);
3597 | catch_clauses catch_clause
3599 ArrayList l = (ArrayList) $1;
3607 : /* empty */ { $$ = null; }
3612 : CATCH opt_catch_args
3614 Expression type = null;
3618 DictionaryEntry cc = (DictionaryEntry) $2;
3619 type = (Expression) cc.Key;
3620 id = (string) cc.Value;
3623 ArrayList one = new ArrayList (4);
3624 Location loc = lexer.Location;
3626 one.Add (new VariableDeclaration (id, null, loc));
3629 current_block = new Block (current_block);
3630 Block b = declare_local_variables (type, one, loc);
3635 Expression type = null;
3639 DictionaryEntry cc = (DictionaryEntry) $2;
3640 type = (Expression) cc.Key;
3641 id = (string) cc.Value;
3645 // FIXME: I can change this for an assignment.
3647 while (current_block != (Block) $1)
3648 current_block = current_block.Parent;
3653 $$ = new Catch (type, id , (Block) $4, lexer.Location);
3658 : /* empty */ { $$ = null; }
3663 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3665 $$ = new DictionaryEntry ($2, $3);
3672 $$ = new Checked ((Block) $2);
3679 $$ = new Unchecked ((Block) $2);
3686 if (!RootContext.Unsafe){
3687 Report.Error (227, lexer.Location,
3688 "Unsafe code can only be used if --unsafe is used");
3691 $$ = new Unsafe ((Block) $3);
3697 type fixed_pointer_declarators
3700 Block assign_block = new Block (current_block, Block.Flags.Implicit);
3701 ArrayList list = (ArrayList) $4;
3702 Expression type = (Expression) $3;
3703 Location l = lexer.Location;
3704 int top = list.Count;
3706 for (int i = 0; i < top; i++){
3707 Pair p = (Pair) list [i];
3710 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3717 current_block.AddStatement (assign_block);
3718 current_block = assign_block;
3719 oob_stack.Push (assign_block);
3724 Location l = (Location) oob_stack.Pop ();
3727 $$ = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
3731 fixed_pointer_declarators
3732 : fixed_pointer_declarator {
3733 ArrayList declarators = new ArrayList (4);
3734 declarators.Add ($1);
3737 | fixed_pointer_declarators COMMA fixed_pointer_declarator
3739 ArrayList declarators = (ArrayList) $1;
3740 declarators.Add ($3);
3745 fixed_pointer_declarator
3746 : IDENTIFIER ASSIGN expression
3748 $$ = new Pair ($1, $3);
3753 : LOCK OPEN_PARENS expression CLOSE_PARENS
3759 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
3764 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
3766 Block assign_block = new Block (current_block);
3767 current_block = assign_block;
3769 oob_stack.Push (lexer.Location);
3771 if ($3 is DictionaryEntry){
3772 DictionaryEntry de = (DictionaryEntry) $3;
3773 Location l = lexer.Location;
3775 Expression type = (Expression) de.Key;
3776 ArrayList var_declarators = (ArrayList) de.Value;
3778 ArrayList vars = new ArrayList (4);
3780 foreach (VariableDeclaration decl in var_declarators){
3782 LocalInfo vi = current_block.AddVariable (
3783 type, decl.identifier,
3784 current_local_parameters, decl.Location);
3790 if (decl.expression_or_array_initializer is Expression){
3791 expr = (Expression) decl.expression_or_array_initializer;
3793 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3795 expr = new ArrayCreation (type, "", init, decl.Location);
3798 LocalVariableReference var;
3800 // Get a writable reference to this read-only variable.
3801 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
3803 // This is so that it is not a warning on using variables
3806 vars.Add (new DictionaryEntry (var, expr));
3808 // Assign a = new Assign (var, expr, decl.Location);
3809 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3811 $3 = new DictionaryEntry (type, vars);
3816 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
3817 current_block.AddStatement (u);
3818 while (current_block.Implicit)
3819 current_block = current_block.Parent;
3821 current_block = current_block.Parent;
3825 resource_acquisition
3826 : local_variable_declaration
3833 // A class used to pass around variable declarations and constants
3835 public class VariableDeclaration {
3836 public string identifier;
3837 public object expression_or_array_initializer;
3838 public Location Location;
3839 public Attributes OptAttributes;
3841 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
3843 this.identifier = id;
3844 this.expression_or_array_initializer = eoai;
3846 this.OptAttributes = opt_attrs;
3849 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
3855 // A class used to hold info about an indexer declarator
3857 public class IndexerDeclaration {
3858 public Expression type;
3859 public string interface_type;
3860 public Parameters param_list;
3862 public IndexerDeclaration (Expression type, string interface_type, Parameters param_list)
3865 this.interface_type = interface_type;
3866 this.param_list = param_list;
3871 // A class used to hold info about an operator declarator
3873 public class OperatorDeclaration {
3874 public Operator.OpType optype;
3875 public Expression ret_type, arg1type, arg2type;
3876 public string arg1name, arg2name;
3877 public Location location;
3879 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
3880 Expression arg1type, string arg1name,
3881 Expression arg2type, string arg2name, Location location)
3884 this.ret_type = ret_type;
3885 this.arg1type = arg1type;
3886 this.arg1name = arg1name;
3887 this.arg2type = arg2type;
3888 this.arg2name = arg2name;
3889 this.location = location;
3894 public class QualifiedIdentifier {
3896 public QualifiedIdentifier prev;
3898 public QualifiedIdentifier (string s)
3904 public QualifiedIdentifier (QualifiedIdentifier prev, string s)
3910 public override string ToString ()
3917 if (prev.prev == null)
3918 return prev.s + "." + s;
3919 if (prev.prev.prev == null)
3920 return prev.prev.s + "." + prev.s + "." + s;
3923 QualifiedIdentifier ptr = this.prev;
3925 while (ptr != null){
3929 string [] ss = new string [count];
3931 for (int i = count; i-- > 0;){
3935 return String.Join (".", ss, 0, ss.Length);
3939 void Error_ExpectingTypeName (Location l, Expression expr)
3941 if (expr is Invocation){
3942 Report.Error (1002, l, "; expected");
3944 Report.Error (-1, l, "Invalid Type definition");
3949 // Given the @class_name name, it creates a fully qualified name
3950 // based on the containing declaration space
3953 MakeName (string class_name)
3955 string ns = current_namespace.FullName;
3956 string container_name = current_container.Name;
3958 if (container_name == ""){
3960 return ns + "." + class_name;
3964 return container_name + "." + class_name;
3968 // Used to report back to the user the result of a declaration
3969 // in the current declaration space
3972 CheckDef (DeclSpace.AdditionResult result, string name, Location l)
3974 if (result == DeclSpace.AdditionResult.Success)
3978 case DeclSpace.AdditionResult.NameExists:
3979 Report.Error (102, l, "The container `" + current_container.Name +
3980 "' already contains a definition for `"+
3986 // This is handled only for static Constructors, because
3987 // in reality we handle these by the semantic analysis later
3989 case DeclSpace.AdditionResult.MethodExists:
3991 111, l, "Class `"+current_container.Name+
3992 "' already defines a member called '" +
3993 name + "' with the same parameter types (more than one default constructor)");
3996 case DeclSpace.AdditionResult.EnclosingClash:
3997 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
4000 case DeclSpace.AdditionResult.NotAConstructor:
4001 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
4004 case DeclSpace.AdditionResult.Error:
4005 // Error has already been reported.
4011 CheckDef (bool result, string name, Location l)
4015 CheckDef (DeclSpace.AdditionResult.NameExists, name, l);
4018 Expression DecomposeQI (QualifiedIdentifier qi, Location loc)
4022 if (qi.prev == null)
4023 return new SimpleName (qi.s, loc);
4025 o = DecomposeQI (qi.prev, loc);
4026 return new MemberAccess (o, qi.s, loc);
4031 // This method is used to get at the complete string representation of
4032 // a fully-qualified type name, hiding inside a MemberAccess ;-)
4033 // This is necessary because local_variable_type admits primary_expression
4034 // as the type of the variable. So we do some extra checking
4037 string GetQualifiedIdentifier (Expression expr)
4039 if (expr is SimpleName)
4040 return ((SimpleName)expr).Name;
4041 else if (expr is MemberAccess)
4042 return GetQualifiedIdentifier (((MemberAccess)expr).Expr) + "." + ((MemberAccess) expr).Identifier;
4044 throw new Exception ("Expr has to be either SimpleName or MemberAccess! (" + expr + ")");
4049 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4051 Block implicit_block;
4052 ArrayList inits = null;
4055 // We use the `Used' property to check whether statements
4056 // have been added to the current block. If so, we need
4057 // to create another block to contain the new declaration
4058 // otherwise, as an optimization, we use the same block to
4059 // add the declaration.
4061 // FIXME: A further optimization is to check if the statements
4062 // that were added were added as part of the initialization
4063 // below. In which case, no other statements have been executed
4064 // and we might be able to reduce the number of blocks for
4065 // situations like this:
4067 // int j = 1; int k = j + 1;
4069 if (current_block.Used) {
4070 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4071 implicit_block.AddChildVariableNames (current_block);
4073 implicit_block = current_block;
4075 foreach (VariableDeclaration decl in variable_declarators){
4077 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4078 if (decl.expression_or_array_initializer != null){
4080 inits = new ArrayList (4);
4087 return implicit_block;
4089 foreach (VariableDeclaration decl in inits){
4093 if (decl.expression_or_array_initializer is Expression){
4094 expr = (Expression) decl.expression_or_array_initializer;
4097 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4099 expr = new ArrayCreation (type, "", init, decl.Location);
4102 LocalVariableReference var;
4103 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4105 assign = new Assign (var, expr, decl.Location);
4107 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4110 return implicit_block;
4113 Block declare_local_constants (Expression type, ArrayList declarators)
4115 Block implicit_block;
4117 if (current_block.Used)
4118 implicit_block = new Block (current_block, Block.Flags.Implicit);
4120 implicit_block = current_block;
4122 foreach (VariableDeclaration decl in declarators){
4123 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4124 current_local_parameters, decl.Location);
4127 return implicit_block;
4130 void CheckAttributeTarget (string a)
4134 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
4138 Location l = lexer.Location;
4139 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4145 void CheckUnaryOperator (Operator.OpType op)
4149 case Operator.OpType.LogicalNot:
4150 case Operator.OpType.OnesComplement:
4151 case Operator.OpType.Increment:
4152 case Operator.OpType.Decrement:
4153 case Operator.OpType.True:
4154 case Operator.OpType.False:
4155 case Operator.OpType.Addition:
4156 case Operator.OpType.Subtraction:
4161 Location l = lexer.Location;
4162 Report.Error (1019, l, "Overloadable unary operator expected");
4168 void CheckBinaryOperator (Operator.OpType op)
4172 case Operator.OpType.Addition:
4173 case Operator.OpType.Subtraction:
4174 case Operator.OpType.Multiply:
4175 case Operator.OpType.Division:
4176 case Operator.OpType.Modulus:
4177 case Operator.OpType.BitwiseAnd:
4178 case Operator.OpType.BitwiseOr:
4179 case Operator.OpType.ExclusiveOr:
4180 case Operator.OpType.LeftShift:
4181 case Operator.OpType.RightShift:
4182 case Operator.OpType.Equality:
4183 case Operator.OpType.Inequality:
4184 case Operator.OpType.GreaterThan:
4185 case Operator.OpType.LessThan:
4186 case Operator.OpType.GreaterThanOrEqual:
4187 case Operator.OpType.LessThanOrEqual:
4191 Location l = lexer.Location;
4192 Report.Error (1020, l, "Overloadable binary operator expected");
4198 void syntax_error (Location l, string msg)
4200 Report.Error (1003, l, "Syntax error, " + msg);
4203 void output (string s)
4205 Console.WriteLine (s);
4208 void note (string s)
4210 // Used to put annotations
4215 public Tokenizer Lexer {
4221 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4223 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4224 this.name = file.Name;
4226 current_container = RootContext.Tree.Types;
4227 current_container.NamespaceEntry = current_namespace;
4228 oob_stack = new Stack ();
4229 switch_stack = new Stack ();
4231 lexer = new Tokenizer (reader, file, defines);
4234 public override void parse ()
4237 if (yacc_verbose_flag)
4238 yyparse (lexer, new yydebug.yyDebugSimple ());
4241 Tokenizer tokenizer = lexer as Tokenizer;
4242 tokenizer.cleanup ();
4243 } catch (Exception e){
4244 // Please do not remove this, it is used during debugging
4247 Console.WriteLine (e);
4248 Report.Error (-25, lexer.Location, "Parsing error");
4249 if (Driver.parser_verbose)
4250 Console.WriteLine (e);
4254 void CheckToken (int error, int yyToken, string msg)
4256 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4257 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4260 Report.Error (error, lexer.Location, msg);
4263 void CheckIdentifierToken (int yyToken)
4265 CheckToken (1041, yyToken, "Identifier expected");