3 // cs-parser.jay: The Parser for the C# compiler
5 // Authors: Miguel de Icaza (miguel@gnu.org)
6 // Ravi Pratap (ravi@ximian.com)
8 // Licensed under the terms of the GNU GPL
10 // (C) 2001 Ximian, Inc (http://www.ximian.com)
13 // (1) Figure out why error productions dont work. `type-declaration' is a
14 // great spot to put an `error' because you can reproduce it with this input:
17 // Possible optimization:
18 // Run memory profiler with parsing only, and consider dropping
19 // arraylists where not needed. Some pieces can use linked lists.
27 using System.Collections;
32 public class CSharpParser {
33 NamespaceEntry current_namespace;
34 TypeContainer current_container;
36 IIteratorContainer iterator_container;
39 // Current block is used to add statements as we find
46 // Current interface is used by the various declaration
47 // productions in the interface declaration to "add"
48 // the interfaces as we find them.
50 Interface current_interface;
53 // This is used by the unary_expression code to resolve
54 // a name against a parameter.
56 Parameters current_local_parameters;
59 // Using during property parsing to describe the implicit
60 // value parameter that is passed to the "set" and "get"accesor
61 // methods (properties and indexers).
63 Expression implicit_value_parameter_type;
64 Parameters indexer_parameters;
67 // Used to determine if we are parsing the get/set pair
68 // of an indexer or a property
73 // An out-of-band stack.
82 public bool yacc_verbose_flag;
84 // Name of the file we are parsing
94 %token NONE /* This token is never returned by our lexer */
95 %token ERROR // This is used not by the parser, but by the tokenizer.
99 *These are the C# keywords
187 /* C# keywords which are not really keywords */
193 /* C# single character operators/punctuation. */
194 %token OPEN_BRACE "{"
195 %token CLOSE_BRACE "}"
196 %token OPEN_BRACKET "["
197 %token CLOSE_BRACKET "]"
198 %token OPEN_PARENS "("
199 %token CLOSE_PARENS ")"
211 %token OP_GENERICS_LT "<"
213 %token OP_GENERICS_GT ">"
214 %token BITWISE_AND "&"
215 %token BITWISE_OR "|"
222 /* C# multi-character operators. */
225 %token OP_SHIFT_LEFT "<<"
226 %token OP_SHIFT_RIGHT ">>"
233 %token OP_MULT_ASSIGN "*="
234 %token OP_DIV_ASSIGN "/="
235 %token OP_MOD_ASSIGN "%="
236 %token OP_ADD_ASSIGN "+="
237 %token OP_SUB_ASSIGN "-="
238 %token OP_SHIFT_LEFT_ASSIGN "<<="
239 %token OP_SHIFT_RIGHT_ASSIGN ">>="
240 %token OP_AND_ASSIGN "&="
241 %token OP_XOR_ASSIGN "^="
242 %token OP_OR_ASSIGN "|="
246 %token LITERAL_INTEGER "int literal"
247 %token LITERAL_FLOAT "float literal"
248 %token LITERAL_DOUBLE "double literal"
249 %token LITERAL_DECIMAL "decimal literal"
250 %token LITERAL_CHARACTER "character literal"
251 %token LITERAL_STRING "string literal"
254 %token CLOSE_PARENS_CAST
255 %token CLOSE_PARENS_NO_CAST
256 %token CLOSE_PARENS_OPEN_PARENS
257 %token CLOSE_PARENS_MINUS
259 /* Add precedence rules to solve dangling else s/r conflict */
268 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
270 %left STAR DIV PERCENT
271 %right BANG CARRET UMINUS
272 %nonassoc OP_INC OP_DEC
274 %left OPEN_BRACKET OPEN_BRACE
278 %start compilation_unit
282 : outer_declarations opt_EOF
283 | outer_declarations attribute_sections opt_EOF
284 | attribute_sections opt_EOF
285 | opt_EOF /* allow empty files */
295 | outer_declarations outer_declaration
300 | namespace_member_declaration
305 | using_directives using_directive
309 : using_alias_directive
310 | using_namespace_directive
313 using_alias_directive
314 : USING IDENTIFIER ASSIGN
315 namespace_or_type_name SEMICOLON
317 current_namespace.UsingAlias ((string) $2, (Expression) $4, lexer.Location);
320 CheckIdentifierToken (yyToken);
324 using_namespace_directive
325 : USING namespace_name SEMICOLON
327 current_namespace.Using ((string) $2, lexer.Location);
332 // Strictly speaking, namespaces don't have attributes but
333 // we parse global attributes along with namespace declarations and then
336 namespace_declaration
337 : opt_attributes NAMESPACE qualified_identifier
339 Attributes attrs = (Attributes) $1;
342 foreach (AttributeSection asec in attrs.AttributeSections)
343 if (asec.Target == "assembly")
344 RootContext.AddGlobalAttributeSection (current_container, asec);
346 Report.Error(1518, Lexer.Location,
347 "Attributes cannot be applied to namespaces."
348 + " Expected class, delegate, enum, interface, or struct");
351 current_namespace = RootContext.Tree.RecordNamespace (current_namespace, file, (string) $3, lexer.Location);
353 namespace_body opt_semicolon
355 current_namespace = current_namespace.Parent;
371 | qualified_identifier DOT IDENTIFIER {
372 $$ = (($1).ToString ()) + "." + ($3.ToString ()); }
377 : qualified_identifier
383 opt_namespace_member_declarations
394 opt_namespace_member_declarations
396 | namespace_member_declarations
399 namespace_member_declarations
400 : namespace_member_declaration
401 | namespace_member_declarations namespace_member_declaration
404 namespace_member_declaration
411 Class c = (Class) $1;
412 mod_flags = c.ModFlags;
414 } else if ($1 is Struct){
415 Struct s = (Struct) $1;
416 mod_flags = s.ModFlags;
421 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
423 1527, lexer.Location,
424 "Namespace elements cant be explicitly " +
425 "declared private or protected in `" + name + "'");
427 current_namespace.DeclarationFound = true;
429 | namespace_declaration {
430 current_namespace.DeclarationFound = true;
437 | interface_declaration
439 | delegate_declaration
441 // Enable this when we have handled all errors, because this acts as a generic fallback
444 // Console.WriteLine ("Token=" + yyToken);
445 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
455 | attribute_sections { $$ = $1; }
461 AttributeSection sect = (AttributeSection) $1;
463 if (sect.Target == "assembly")
464 RootContext.AddGlobalAttributeSection (current_container, sect);
467 $$ = new Attributes ((AttributeSection) $1);
469 | attribute_sections attribute_section
471 Attributes attrs = null;
472 AttributeSection sect = (AttributeSection) $2;
474 if (sect.Target == "assembly")
475 RootContext.AddGlobalAttributeSection (current_container, sect);
478 attrs = (Attributes) $1;
479 attrs.AddAttributeSection (sect);
487 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
489 string target = null;
492 target = (string) $2;
494 $$ = new AttributeSection (target, (ArrayList) $3);
496 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
498 $$ = new AttributeSection (null, (ArrayList) $2);
502 attribute_target_specifier
503 : attribute_target COLON
512 CheckAttributeTarget ((string) $1);
515 | EVENT { $$ = "event"; }
516 | RETURN { $$ = "return"; }
522 ArrayList attrs = new ArrayList (4);
528 | attribute_list COMMA attribute
530 ArrayList attrs = (ArrayList) $1;
542 opt_attribute_arguments
545 // Attributes need a string, not an expression: generic types will fail here.
547 $$ = new Attribute (((Expression) $1).ToString (), (ArrayList) $3, (Location) $2);
552 : type_name { /* reserved attribute name or identifier: 17.4 */ }
555 opt_attribute_arguments
556 : /* empty */ { $$ = null; }
557 | OPEN_PARENS attribute_arguments CLOSE_PARENS
565 : opt_positional_argument_list
570 ArrayList args = new ArrayList (4);
576 | positional_argument_list COMMA named_argument_list
578 ArrayList args = new ArrayList (4);
584 | named_argument_list
586 ArrayList args = new ArrayList (4);
595 opt_positional_argument_list
596 : /* empty */ { $$ = null; }
597 | positional_argument_list
600 positional_argument_list
603 ArrayList args = new ArrayList (4);
604 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
608 | positional_argument_list COMMA expression
610 ArrayList args = (ArrayList) $1;
611 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
620 ArrayList args = new ArrayList (4);
625 | named_argument_list COMMA named_argument
627 ArrayList args = (ArrayList) $1;
635 : IDENTIFIER ASSIGN expression
637 $$ = new DictionaryEntry (
639 new Argument ((Expression) $3, Argument.AType.Expression));
645 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
648 opt_class_member_declarations
650 | class_member_declarations
653 class_member_declarations
654 : class_member_declaration
655 | class_member_declarations
656 class_member_declaration
659 class_member_declaration
660 : constant_declaration // done
661 | field_declaration // done
662 | method_declaration // done
663 | property_declaration // done
664 | event_declaration // done
665 | indexer_declaration // done
666 | operator_declaration // done
667 | constructor_declaration // done
668 | destructor_declaration // done
678 string full_struct_name = MakeName ((string) $4);
680 new_struct = new Struct (current_namespace, current_container, full_struct_name,
681 (int) $2, (Attributes) $1, lexer.Location);
682 current_container = new_struct;
683 RootContext.Tree.RecordDecl (full_struct_name, new_struct);
685 opt_type_parameter_list
687 opt_type_parameter_constraints_clauses
691 Struct new_struct = (Struct) current_container;
693 if ($8 != null && $6 == null)
694 Report.Error (-200, new_struct.Location,
695 "Type parameter constraints only valid if there is a type parameter list");
697 CheckDef (new_struct.SetParameterInfo ((ArrayList) $6, (ArrayList) $8), new_struct.Name, new_struct.Location);
700 new_struct.Bases = (ArrayList) $7;
702 current_container = current_container.Parent;
703 CheckDef (current_container.AddStruct (new_struct), new_struct.Name, new_struct.Location);
706 | opt_attributes opt_modifiers STRUCT error {
707 CheckIdentifierToken (yyToken);
712 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
715 opt_struct_member_declarations
717 | struct_member_declarations
720 struct_member_declarations
721 : struct_member_declaration
722 | struct_member_declarations struct_member_declaration
725 struct_member_declaration
726 : constant_declaration
729 | property_declaration
731 | indexer_declaration
732 | operator_declaration
733 | constructor_declaration
737 * This is only included so we can flag error 575:
738 * destructors only allowed on class types
740 | destructor_declaration
751 foreach (VariableDeclaration constant in (ArrayList) $5){
752 Location l = constant.Location;
754 Const c = new Const (
755 (Expression) $4, (string) constant.identifier,
756 (Expression) constant.expression_or_array_initializer, (int) $2,
759 CheckDef (current_container.AddConstant (c), c.Name, l);
765 : constant_declarator
767 ArrayList constants = new ArrayList (4);
771 | constant_declarators COMMA constant_declarator
773 ArrayList constants = (ArrayList) $1;
780 : IDENTIFIER ASSIGN constant_expression
782 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
793 Expression type = (Expression) $3;
796 foreach (VariableDeclaration var in (ArrayList) $4){
797 Location l = var.Location;
799 Field field = new Field (type, mod, var.identifier,
800 var.expression_or_array_initializer,
803 CheckDef (current_container.AddField (field), field.Name, l);
811 Report.Error (670, lexer.Location, "void type is not allowed for fields");
816 : variable_declarator
818 ArrayList decl = new ArrayList (4);
822 | variable_declarators COMMA variable_declarator
824 ArrayList decls = (ArrayList) $1;
831 : IDENTIFIER ASSIGN variable_initializer
833 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
837 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
850 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
852 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
858 iterator_container = (IIteratorContainer) $1;
862 Method method = (Method) $1;
863 Block b = (Block) $3;
864 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
867 if ((method.ModFlags & extern_abstract) == 0){
869 501, lexer.Location, current_container.MakeName (method.Name) +
870 "must declare a body because it is not marked abstract or extern");
873 if ((method.ModFlags & Modifiers.EXTERN) != 0){
875 179, lexer.Location, current_container.MakeName (method.Name) +
876 " is declared extern, but has a body");
880 method.Block = (Block) $3;
881 CheckDef (current_container.AddMethod (method), method.Name, method.Location);
883 current_local_parameters = null;
884 iterator_container = null;
898 1585, lexer.Location, "Member modifier `" +
899 Modifiers.Name (i) + "' must precede member type and name");
912 opt_type_parameter_list
913 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
914 opt_type_parameter_constraints_clauses
916 if ($9 != null && $5 == null)
917 Report.Error (-200, lexer.Location,
918 "Type parameter constraints only valid if there is a type parameter list");
921 GenericMethod generic = null;
923 generic = new GenericMethod (current_namespace, current_container,
924 (string) $4, lexer.Location);
926 CheckDef (generic.SetParameterInfo ((ArrayList) $5, (ArrayList) $9), generic.Name, generic.Location);
927 method = new Method (generic, (Expression) $3, (int) $2, (string) $4,
928 (Parameters) $7, (Attributes) $1, lexer.Location);
930 method = new Method (current_container, (Expression) $3, (int) $2, (string) $4,
931 (Parameters) $7, (Attributes) $1, lexer.Location);
933 current_local_parameters = (Parameters) $7;
941 opt_type_parameter_list
942 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
943 opt_type_parameter_constraints_clauses
945 if ($9 != null && $5 == null)
946 Report.Error (-200, lexer.Location,
947 "Type parameter constraints only valid if there is a type parameter list");
950 GenericMethod generic = null;
952 generic = new GenericMethod (current_namespace, current_container,
953 (string) $4, lexer.Location);
955 CheckDef (generic.SetParameterInfo ((ArrayList) $5, (ArrayList) $9), generic.Name, generic.Location);
957 method = new Method (generic, TypeManager.system_void_expr, (int) $2,
958 (string) $4, (Parameters) $7, (Attributes) $1,
961 method = new Method (current_container, TypeManager.system_void_expr, (int) $2,
962 (string) $4, (Parameters) $7, (Attributes) $1,
965 current_local_parameters = (Parameters) $7;
973 | SEMICOLON { $$ = null; }
976 opt_formal_parameter_list
977 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
978 | formal_parameter_list
981 formal_parameter_list
984 ArrayList pars_list = (ArrayList) $1;
986 Parameter [] pars = new Parameter [pars_list.Count];
987 pars_list.CopyTo (pars);
989 $$ = new Parameters (pars, null, lexer.Location);
991 | fixed_parameters COMMA parameter_array
993 ArrayList pars_list = (ArrayList) $1;
995 Parameter [] pars = new Parameter [pars_list.Count];
996 pars_list.CopyTo (pars);
998 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
1002 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
1009 ArrayList pars = new ArrayList (4);
1014 | fixed_parameters COMMA fixed_parameter
1016 ArrayList pars = (ArrayList) $1;
1025 opt_parameter_modifier
1029 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
1032 opt_parameter_modifier
1035 CheckIdentifierToken (yyToken);
1040 opt_parameter_modifier
1041 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1042 | parameter_modifier
1046 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1047 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1051 : opt_attributes PARAMS type IDENTIFIER
1053 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1054 note ("type must be a single-dimension array type");
1056 | opt_attributes PARAMS type error {
1057 CheckIdentifierToken (yyToken);
1063 : qualified_identifier
1066 property_declaration
1072 implicit_value_parameter_type = (Expression) $3;
1074 lexer.PropertyParsing = true;
1076 $$ = lexer.Location;
1078 accessor_declarations
1080 lexer.PropertyParsing = false;
1085 Pair pair = (Pair) $7;
1086 Accessor get_block = (Accessor) pair.First;
1087 Accessor set_block = (Accessor) pair.Second;
1089 Location loc = (Location) $6;
1090 prop = new Property (current_container, (Expression) $3, (string) $4, (int) $2,
1091 get_block, set_block, (Attributes) $1, loc);
1093 CheckDef (current_container.AddProperty (prop), prop.Name, loc);
1094 implicit_value_parameter_type = null;
1098 accessor_declarations
1099 : get_accessor_declaration opt_set_accessor_declaration
1101 $$ = new Pair ($1, $2);
1103 | set_accessor_declaration opt_get_accessor_declaration
1105 $$ = new Pair ($2, $1);
1109 opt_get_accessor_declaration
1110 : /* empty */ { $$ = null; }
1111 | get_accessor_declaration
1114 opt_set_accessor_declaration
1115 : /* empty */ { $$ = null; }
1116 | set_accessor_declaration
1119 get_accessor_declaration
1120 : opt_attributes GET
1122 // If this is not the case, then current_local_parameters has already
1123 // been set in indexer_declaration
1124 if (parsing_indexer == false)
1125 current_local_parameters = null;
1127 current_local_parameters = indexer_parameters;
1128 lexer.PropertyParsing = false;
1132 $$ = new Accessor ((Block) $4, (Attributes) $1);
1133 current_local_parameters = null;
1134 lexer.PropertyParsing = true;
1138 set_accessor_declaration
1139 : opt_attributes SET
1142 Parameter implicit_value_parameter = new Parameter (
1143 implicit_value_parameter_type, "value",
1144 Parameter.Modifier.NONE, null);
1146 if (parsing_indexer == false) {
1147 args = new Parameter [1];
1148 args [0] = implicit_value_parameter;
1149 current_local_parameters = new Parameters (args, null, lexer.Location);
1151 Parameter [] fpars = indexer_parameters.FixedParameters;
1154 int count = fpars.Length;
1156 args = new Parameter [count + 1];
1157 fpars.CopyTo (args, 0);
1158 args [count] = implicit_value_parameter;
1161 current_local_parameters = new Parameters (
1162 args, indexer_parameters.ArrayParameter, lexer.Location);
1165 lexer.PropertyParsing = false;
1169 $$ = new Accessor ((Block) $4, (Attributes) $1);
1170 current_local_parameters = null;
1171 lexer.PropertyParsing = true;
1177 | SEMICOLON { $$ = null; }
1180 interface_declaration
1183 INTERFACE IDENTIFIER
1185 Interface new_interface;
1186 string full_interface_name = MakeName ((string) $4);
1188 new_interface = new Interface (current_namespace, current_container, full_interface_name,
1189 (int) $2, (Attributes) $1, lexer.Location);
1190 if (current_interface != null) {
1191 Location l = lexer.Location;
1192 Report.Error (-2, l, "Internal compiler error: interface inside interface");
1194 current_interface = new_interface;
1195 RootContext.Tree.RecordDecl (full_interface_name, new_interface);
1197 opt_type_parameter_list
1199 opt_type_parameter_constraints_clauses
1200 interface_body opt_semicolon
1202 Interface new_interface = (Interface) current_interface;
1204 if ($8 != null && $6 == null)
1205 Report.Error (-200, new_interface.Location,
1206 "Type parameter constraints only valid if there is a type parameter list");
1209 CheckDef (new_interface.SetParameterInfo ((ArrayList) $6, (ArrayList) $8), new_interface.Name, new_interface.Location);
1211 new_interface.Bases = (ArrayList) $7;
1213 current_interface = null;
1214 CheckDef (current_container.AddInterface (new_interface),
1215 new_interface.Name, new_interface.Location);
1217 | opt_attributes opt_modifiers INTERFACE error {
1218 CheckIdentifierToken (yyToken);
1223 : /* empty */ { $$ = null; }
1228 : COLON interface_type_list { $$ = $2; }
1234 ArrayList interfaces = new ArrayList (4);
1236 interfaces.Add ($1);
1239 | interface_type_list COMMA interface_type
1241 ArrayList interfaces = (ArrayList) $1;
1242 interfaces.Add ($3);
1249 opt_interface_member_declarations
1253 opt_interface_member_declarations
1255 | interface_member_declarations
1258 interface_member_declarations
1259 : interface_member_declaration
1260 | interface_member_declarations interface_member_declaration
1263 interface_member_declaration
1264 : interface_method_declaration
1266 InterfaceMethod m = (InterfaceMethod) $1;
1268 CheckDef (current_interface.AddMethod (m), m.Name, m.Location);
1270 | interface_property_declaration
1272 InterfaceProperty p = (InterfaceProperty) $1;
1274 CheckDef (current_interface.AddProperty (p), p.Name, p.Location);
1276 | interface_event_declaration
1278 InterfaceEvent e = (InterfaceEvent) $1;
1280 CheckDef (current_interface.AddEvent (e), e.Name, lexer.Location);
1282 | interface_indexer_declaration
1284 InterfaceIndexer i = (InterfaceIndexer) $1;
1286 CheckDef (current_interface.AddIndexer (i), "indexer", i.Location);
1291 : /* empty */ { $$ = false; }
1292 | NEW { $$ = true; }
1295 interface_method_declaration
1296 : opt_attributes opt_new type IDENTIFIER
1297 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1300 $$ = new InterfaceMethod ((Expression) $3, (string) $4, (bool) $2,
1301 (Parameters) $6, (Attributes) $1, lexer.Location);
1303 | opt_attributes opt_new VOID IDENTIFIER
1304 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1307 $$ = new InterfaceMethod (
1308 TypeManager.system_void_expr, (string) $4, (bool) $2, (Parameters) $6,
1309 (Attributes) $1, lexer.Location);
1313 interface_property_declaration
1318 { lexer.PropertyParsing = true; }
1320 { lexer.PropertyParsing = false; }
1325 $$ = new InterfaceProperty ((Expression) $3, (string) $4, (bool) $2,
1326 (gs & 1) == 1, (gs & 2) == 2, (Attributes) $1,
1332 CheckIdentifierToken (yyToken);
1338 : opt_attributes GET SEMICOLON { $$ = 1; }
1339 | opt_attributes SET SEMICOLON { $$ = 2; }
1340 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1342 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1346 interface_event_declaration
1347 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1349 $$ = new InterfaceEvent ((Expression) $4, (string) $5, (bool) $2, (Attributes) $1,
1352 | opt_attributes opt_new EVENT type error {
1353 CheckIdentifierToken (yyToken);
1358 interface_indexer_declaration
1359 : opt_attributes opt_new type THIS
1360 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1362 { lexer.PropertyParsing = true; }
1364 { lexer.PropertyParsing = false; }
1367 int a_flags = (int) $10;
1369 bool do_get = (a_flags & 1) == 1;
1370 bool do_set = (a_flags & 2) == 2;
1372 $$ = new InterfaceIndexer ((Expression) $3, (Parameters) $6, do_get, do_set,
1373 (bool) $2, (Attributes) $1, lexer.Location);
1377 operator_declaration
1378 : opt_attributes opt_modifiers operator_declarator operator_body
1380 OperatorDeclaration decl = (OperatorDeclaration) $3;
1382 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
1383 decl.arg2type, decl.arg2name, (Block) $4, (Attributes) $1, decl.location);
1385 // Note again, checking is done in semantic analysis
1386 current_container.AddOperator (op);
1388 current_local_parameters = null;
1394 | SEMICOLON { $$ = null; }
1397 : type OPERATOR overloadable_operator
1398 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1400 Operator.OpType op = (Operator.OpType) $3;
1401 CheckUnaryOperator (op);
1403 if (op == Operator.OpType.Addition)
1404 op = Operator.OpType.UnaryPlus;
1406 if (op == Operator.OpType.Subtraction)
1407 op = Operator.OpType.UnaryNegation;
1409 Parameter [] pars = new Parameter [1];
1411 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1413 current_local_parameters = new Parameters (pars, null, lexer.Location);
1415 $$ = new OperatorDeclaration (op, (Expression) $1, (Expression) $5, (string) $6,
1416 null, null, lexer.Location);
1418 | type OPERATOR overloadable_operator
1420 type IDENTIFIER COMMA
1424 CheckBinaryOperator ((Operator.OpType) $3);
1426 Parameter [] pars = new Parameter [2];
1428 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1429 pars [1] = new Parameter ((Expression) $8, (string) $9, Parameter.Modifier.NONE, null);
1431 current_local_parameters = new Parameters (pars, null, lexer.Location);
1433 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1434 (Expression) $5, (string) $6,
1435 (Expression) $8, (string) $9, lexer.Location);
1437 | conversion_operator_declarator
1440 overloadable_operator
1442 : BANG { $$ = Operator.OpType.LogicalNot; }
1443 | TILDE { $$ = Operator.OpType.OnesComplement; }
1444 | OP_INC { $$ = Operator.OpType.Increment; }
1445 | OP_DEC { $$ = Operator.OpType.Decrement; }
1446 | TRUE { $$ = Operator.OpType.True; }
1447 | FALSE { $$ = Operator.OpType.False; }
1448 // Unary and binary:
1449 | PLUS { $$ = Operator.OpType.Addition; }
1450 | MINUS { $$ = Operator.OpType.Subtraction; }
1452 | STAR { $$ = Operator.OpType.Multiply; }
1453 | DIV { $$ = Operator.OpType.Division; }
1454 | PERCENT { $$ = Operator.OpType.Modulus; }
1455 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1456 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1457 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1458 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1459 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1460 | OP_EQ { $$ = Operator.OpType.Equality; }
1461 | OP_NE { $$ = Operator.OpType.Inequality; }
1462 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1463 | OP_LT { $$ = Operator.OpType.LessThan; }
1464 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1465 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1468 conversion_operator_declarator
1469 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1471 Parameter [] pars = new Parameter [1];
1473 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1475 current_local_parameters = new Parameters (pars, null, lexer.Location);
1477 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1478 null, null, lexer.Location);
1480 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1482 Parameter [] pars = new Parameter [1];
1484 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1486 current_local_parameters = new Parameters (pars, null, lexer.Location);
1488 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1489 null, null, lexer.Location);
1493 syntax_error (lexer.Location, "'operator' expected");
1497 syntax_error (lexer.Location, "'operator' expected");
1501 constructor_declaration
1504 constructor_declarator
1507 Constructor c = (Constructor) $3;
1508 c.Block = (Block) $4;
1509 c.OptAttributes = (Attributes) $1;
1510 c.ModFlags = (int) $2;
1512 if (c.Name == current_container.Basename){
1513 if ((c.ModFlags & Modifiers.STATIC) != 0){
1514 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1516 515, c.Location, String.Format (
1517 "`{0}.{1}': static constructor can not have access modifiers",
1518 c.Name, current_container.Name));
1521 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1523 if (c.Initializer != null){
1526 "Static constructors can not have an explicit this or base " +
1527 "constructor invocations");
1530 if (!c.Parameters.Empty){
1532 132, c.Location, "Static constructors should not have parameters");
1535 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1538 // We let another layer check the validity of the constructor.
1539 Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1542 CheckDef (current_container.AddConstructor (c), c.Name, c.Location);
1544 current_local_parameters = null;
1548 constructor_declarator
1550 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1552 oob_stack.Push (lexer.Location);
1554 current_local_parameters = (Parameters) $3;
1556 opt_constructor_initializer
1558 Location l = (Location) oob_stack.Pop ();
1559 $$ = new Constructor (current_container, (string) $1, (Parameters) $3,
1560 (ConstructorInitializer) $6, l);
1566 | SEMICOLON { $$ = null; }
1569 opt_constructor_initializer
1570 : /* empty */ { $$ = null; }
1571 | constructor_initializer
1574 constructor_initializer
1575 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1577 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1579 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1581 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1584 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1590 : /* EMPTY */ { $$ = 0; }
1591 | UNSAFE { $$ = Modifiers.UNSAFE; }
1592 | EXTERN { $$ = Modifiers.EXTERN; }
1595 destructor_declaration
1596 : opt_attributes opt_finalizer TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1598 if ((string) $4 != current_container.Basename){
1599 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1600 } else if (!(current_container is Class)){
1601 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1603 Location l = lexer.Location;
1606 if (!RootContext.StdLib && current_container.Name == "System.Object")
1607 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
1609 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
1611 if ((m & Modifiers.UNSAFE) != 0){
1612 if (!RootContext.Unsafe){
1613 Report.Error (227, l,
1614 "Unsafe code requires the --unsafe command " +
1615 "line option to be specified");
1619 Method d = new Method (
1620 current_container, TypeManager.system_void_expr, m, "Finalize",
1621 new Parameters (null, null, l), (Attributes) $1, l);
1623 d.Block = (Block) $7;
1624 CheckDef (current_container.AddMethod (d), d.Name, d.Location);
1632 EVENT type variable_declarators SEMICOLON
1634 foreach (VariableDeclaration var in (ArrayList) $5) {
1636 Event e = new Event ((Expression) $4, var.identifier,
1637 var.expression_or_array_initializer,
1638 (int) $2, null, null, (Attributes) $1, lexer.Location);
1640 CheckDef (current_container.AddEvent (e), e.Name, e.Location);
1646 EVENT type member_name
1649 implicit_value_parameter_type = (Expression) $4;
1650 lexer.EventParsing = true;
1651 oob_stack.Push (lexer.Location);
1653 event_accessor_declarations
1655 lexer.EventParsing = false;
1659 Location loc = (Location) oob_stack.Pop ();
1661 Pair pair = (Pair) $8;
1662 Accessor add_accessor = null;
1663 Accessor rem_accessor = null;
1665 if (pair.First != null)
1666 add_accessor = (Accessor) pair.First;
1667 if (pair.Second != null)
1668 rem_accessor = (Accessor) pair.Second;
1670 Event e = new Event ((Expression) $4, (string) $5, null, (int) $2, add_accessor, rem_accessor,
1671 (Attributes) $1, loc);
1673 CheckDef (current_container.AddEvent (e), e.Name, loc);
1674 implicit_value_parameter_type = null;
1678 event_accessor_declarations
1679 : add_accessor_declaration remove_accessor_declaration
1681 $$ = new Pair ($1, $2);
1683 | remove_accessor_declaration add_accessor_declaration
1685 $$ = new Pair ($2, $1);
1689 add_accessor_declaration
1690 : opt_attributes ADD
1692 Parameter [] args = new Parameter [1];
1693 Parameter implicit_value_parameter = new Parameter (
1694 implicit_value_parameter_type, "value",
1695 Parameter.Modifier.NONE, null);
1697 args [0] = implicit_value_parameter;
1699 current_local_parameters = new Parameters (args, null, lexer.Location);
1700 lexer.EventParsing = false;
1704 $$ = new Accessor ((Block) $4, (Attributes) $1);
1705 lexer.EventParsing = true;
1707 | opt_attributes ADD error {
1708 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1713 remove_accessor_declaration
1714 : opt_attributes REMOVE
1716 Parameter [] args = new Parameter [1];
1717 Parameter implicit_value_parameter = new Parameter (
1718 implicit_value_parameter_type, "value",
1719 Parameter.Modifier.NONE, null);
1721 args [0] = implicit_value_parameter;
1723 current_local_parameters = new Parameters (args, null, lexer.Location);
1724 lexer.EventParsing = false;
1728 $$ = new Accessor ((Block) $4, (Attributes) $1);
1729 lexer.EventParsing = true;
1731 | opt_attributes REMOVE error {
1732 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1738 : opt_attributes opt_modifiers indexer_declarator
1741 IndexerDeclaration decl = (IndexerDeclaration) $3;
1743 implicit_value_parameter_type = decl.type;
1745 lexer.PropertyParsing = true;
1746 parsing_indexer = true;
1748 indexer_parameters = decl.param_list;
1749 oob_stack.Push (lexer.Location);
1751 accessor_declarations
1753 lexer.PropertyParsing = false;
1754 parsing_indexer = false;
1758 // The signature is computed from the signature of the indexer. Look
1759 // at section 3.6 on the spec
1760 Location loc = (Location) oob_stack.Pop ();
1762 IndexerDeclaration decl = (IndexerDeclaration) $3;
1763 Pair pair = (Pair) $6;
1764 Accessor get_block = (Accessor) pair.First;
1765 Accessor set_block = (Accessor) pair.Second;
1767 indexer = new Indexer (current_container, decl.type, decl.interface_type, (int) $2,
1768 decl.param_list, get_block, set_block, (Attributes) $1, loc);
1770 // Note that there is no equivalent of CheckDef for this case
1771 // We shall handle this in semantic analysis
1773 current_container.AddIndexer (indexer);
1775 current_local_parameters = null;
1776 implicit_value_parameter_type = null;
1777 indexer_parameters = null;
1782 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1784 Parameters pars = (Parameters) $4;
1786 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1787 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1790 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
1792 | type qualified_identifier DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1794 Parameters pars = (Parameters) $6;
1796 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1797 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1799 $$ = new IndexerDeclaration ((Expression) $1, (string) $2, pars);
1811 Location enum_location = lexer.Location;
1813 string full_name = MakeName ((string) $4);
1814 Enum e = new Enum (current_namespace, current_container, (Expression) $5, (int) $2,
1815 full_name, (Attributes) $1, enum_location);
1817 foreach (VariableDeclaration ev in (ArrayList) $6) {
1818 Location loc = (Location) ev.Location;
1820 CheckDef (e.AddEnumMember (ev.identifier,
1821 (Expression) ev.expression_or_array_initializer,
1822 loc, ev.OptAttributes),
1823 ev.identifier, loc);
1826 CheckDef (current_container.AddEnum (e), full_name, enum_location);
1827 RootContext.Tree.RecordDecl (full_name, e);
1833 : /* empty */ { $$ = TypeManager.system_int32_expr; }
1834 | COLON type { $$ = $2; }
1838 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1844 opt_enum_member_declarations
1845 : /* empty */ { $$ = new ArrayList (4); }
1846 | enum_member_declarations opt_comma { $$ = $1; }
1849 enum_member_declarations
1850 : enum_member_declaration
1852 ArrayList l = new ArrayList (4);
1857 | enum_member_declarations COMMA enum_member_declaration
1859 ArrayList l = (ArrayList) $1;
1867 enum_member_declaration
1868 : opt_attributes IDENTIFIER
1870 $$ = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
1872 | opt_attributes IDENTIFIER
1874 $$ = lexer.Location;
1878 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
1882 delegate_declaration
1886 IDENTIFIER OPEN_PARENS
1887 opt_formal_parameter_list
1891 Location l = lexer.Location;
1892 Delegate del = new Delegate (current_namespace, current_container, (Expression) $4,
1893 (int) $2, MakeName ((string) $5), (Parameters) $7,
1894 (Attributes) $1, l);
1896 CheckDef (current_container.AddDelegate (del), del.Name, l);
1901 IDENTIFIER OPEN_PARENS
1902 opt_formal_parameter_list
1906 Location l = lexer.Location;
1907 Delegate del = new Delegate (
1908 current_namespace, current_container,
1909 TypeManager.system_void_expr, (int) $2, MakeName ((string) $5),
1910 (Parameters) $7, (Attributes) $1, l);
1912 CheckDef (current_container.AddDelegate (del), del.Name, l);
1917 : namespace_or_type_name
1920 namespace_or_type_name
1921 : IDENTIFIER opt_type_argument_list {
1923 $$ = new SimpleName ((string) $1, lexer.Location);
1925 $$ = new ConstructedType ((string) $1, (TypeArguments) $2, lexer.Location);
1927 | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list
1932 // Third argument will become an Expression, when we have sorted out
1933 // the issues with SimpleName first
1937 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1939 $$ = new GenericMemberAccess ((Expression) $1, (string) $3,
1940 (TypeArguments) $4, lexer.Location);
1942 | namespace_or_type_name DOT DEFAULT
1944 $$ = new DefaultValueExpression ((Expression) $1, lexer.Location);
1950 // Figure out what to do with the list
1952 opt_type_argument_list
1953 : /* empty */ { $$ = null; }
1954 | OP_GENERICS_LT type_arguments OP_GENERICS_GT
1962 TypeArguments type_args = new TypeArguments ();
1963 type_args.Add ((Expression) $1);
1966 | type_arguments COMMA type {
1967 TypeArguments type_args = (TypeArguments) $1;
1968 type_args.Add ((Expression) $3);
1974 * Before you think of adding a return_type, notice that we have been
1975 * using two rules in the places where it matters (one rule using type
1976 * and another identical one that uses VOID as the return type). This
1977 * gets rid of a shift/reduce couple
1980 : type_name { /* class_type */
1992 // Note that here only unmanaged types are allowed but we
1993 // can't perform checks during this phase - we do it during
1994 // semantic analysis.
1996 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2000 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2006 | non_expression_type rank_specifier
2008 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2010 | non_expression_type STAR
2012 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2014 | expression rank_specifiers
2016 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2020 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2024 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2025 // through this different path
2027 | multiplicative_expression STAR
2029 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2036 ArrayList types = new ArrayList (4);
2041 | type_list COMMA type
2043 ArrayList types = (ArrayList) $1;
2051 * replaces all the productions for isolating the various
2052 * simple types, but we need this to reuse it easily in local_variable_type
2055 : OBJECT { $$ = TypeManager.system_object_expr; }
2056 | STRING { $$ = TypeManager.system_string_expr; }
2057 | BOOL { $$ = TypeManager.system_boolean_expr; }
2058 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2059 | FLOAT { $$ = TypeManager.system_single_expr; }
2060 | DOUBLE { $$ = TypeManager.system_double_expr; }
2065 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2066 | BYTE { $$ = TypeManager.system_byte_expr; }
2067 | SHORT { $$ = TypeManager.system_int16_expr; }
2068 | USHORT { $$ = TypeManager.system_uint16_expr; }
2069 | INT { $$ = TypeManager.system_int32_expr; }
2070 | UINT { $$ = TypeManager.system_uint32_expr; }
2071 | LONG { $$ = TypeManager.system_int64_expr; }
2072 | ULONG { $$ = TypeManager.system_uint64_expr; }
2073 | CHAR { $$ = TypeManager.system_char_expr; }
2074 | VOID { $$ = TypeManager.system_void_expr; }
2082 : type rank_specifiers
2084 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2089 // Expressions, section 7.5
2101 | parenthesized_expression
2102 | default_value_expression
2104 | invocation_expression
2108 | post_increment_expression
2109 | post_decrement_expression
2113 | checked_expression
2114 | unchecked_expression
2115 | pointer_member_access
2116 | anonymous_method_expression
2123 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2124 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2125 | NULL { $$ = NullLiteral.Null; }
2129 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2130 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2131 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2136 object v = lexer.Value;
2142 $$ = IntLiteral.Zero;
2144 $$ = IntLiteral.One;
2146 $$ = new IntLiteral (i);
2147 } else if (v is uint)
2148 $$ = new UIntLiteral ((UInt32) v);
2150 $$ = new LongLiteral ((Int64) v);
2151 else if (v is ulong)
2152 $$ = new ULongLiteral ((UInt64) v);
2154 Console.WriteLine ("OOPS. Unexpected result from scanner");
2159 : TRUE { $$ = new BoolLiteral (true); }
2160 | FALSE { $$ = new BoolLiteral (false); }
2163 parenthesized_expression_0
2164 : OPEN_PARENS expression CLOSE_PARENS
2167 lexer.Deambiguate_CloseParens ();
2168 // After this, the next token returned is one of
2169 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2170 // or CLOSE_PARENS_MINUS.
2174 parenthesized_expression
2175 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2179 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2181 // If a parenthesized expression is followed by a minus, we need to wrap
2182 // the expression inside a ParenthesizedExpression for the CS0075 check
2183 // in Binary.DoResolve().
2184 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2189 : primary_expression DOT IDENTIFIER
2191 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2193 | predefined_type DOT IDENTIFIER
2195 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2203 invocation_expression
2204 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2207 Location l = lexer.Location;
2208 Report.Error (1, l, "Parse error");
2210 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2212 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2214 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2216 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2218 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2223 : /* empty */ { $$ = null; }
2230 ArrayList list = new ArrayList (4);
2234 | argument_list COMMA argument
2236 ArrayList list = (ArrayList) $1;
2240 | argument_list error {
2241 CheckToken (1026, yyToken, ", or ) expected");
2248 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2250 | REF variable_reference
2252 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2254 | OUT variable_reference
2256 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2261 : expression { note ("section 5.4"); $$ = $1; }
2265 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2267 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2269 | primary_expression rank_specifiers
2271 // So the super-trick is that primary_expression
2272 // can only be either a SimpleName or a MemberAccess.
2273 // The MemberAccess case arises when you have a fully qualified type-name like :
2275 // SimpleName is when you have
2278 Expression expr = (Expression) $1;
2279 if (expr is ComposedCast){
2280 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2281 } else if (!(expr is SimpleName || expr is MemberAccess || expr is ConstructedType)){
2282 Error_ExpectingTypeName (lexer.Location, expr);
2283 $$ = TypeManager.system_object_expr;
2286 // So we extract the string corresponding to the SimpleName
2289 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2297 ArrayList list = new ArrayList (4);
2301 | expression_list COMMA expression
2303 ArrayList list = (ArrayList) $1;
2312 $$ = new This (current_block, lexer.Location);
2317 : BASE DOT IDENTIFIER
2319 $$ = new BaseAccess ((string) $3, lexer.Location);
2321 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2323 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2326 Report.Error (175, "Use of keyword `base' is not valid in this context");
2331 post_increment_expression
2332 : primary_expression OP_INC
2334 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2335 (Expression) $1, lexer.Location);
2339 post_decrement_expression
2340 : primary_expression OP_DEC
2342 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2343 (Expression) $1, lexer.Location);
2348 : object_or_delegate_creation_expression
2349 | array_creation_expression
2352 object_or_delegate_creation_expression
2353 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2355 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2359 array_creation_expression
2360 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2362 opt_array_initializer
2364 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2366 | NEW type rank_specifiers array_initializer
2368 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2372 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2388 : rank_specifier opt_rank_specifier
2390 $$ = (string) $2 + (string) $1;
2395 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2397 $$ = "[" + (string) $2 + "]";
2417 | dim_separators COMMA
2419 $$ = (string) $1 + ",";
2423 opt_array_initializer
2435 : OPEN_BRACE CLOSE_BRACE
2437 ArrayList list = new ArrayList (4);
2440 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2442 $$ = (ArrayList) $2;
2446 variable_initializer_list
2447 : variable_initializer
2449 ArrayList list = new ArrayList (4);
2453 | variable_initializer_list COMMA variable_initializer
2455 ArrayList list = (ArrayList) $1;
2462 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2464 $$ = new TypeOfVoid (lexer.Location);
2466 | TYPEOF OPEN_PARENS type CLOSE_PARENS
2468 $$ = new TypeOf ((Expression) $3, lexer.Location);
2473 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2474 $$ = new SizeOf ((Expression) $3, lexer.Location);
2479 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2481 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2485 unchecked_expression
2486 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2488 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2492 pointer_member_access
2493 : primary_expression OP_PTR IDENTIFIER
2497 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2498 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2502 anonymous_method_expression
2503 : DELEGATE opt_anonymous_method_signature {
2504 oob_stack.Push (current_local_parameters);
2505 current_local_parameters = (Parameters)$2;
2507 if (!RootContext.V2){
2508 Report.Error (-213, lexer.Location, "Anonymous methods are only supported in V2");
2511 $$ = new AnonymousMethod ((Parameters) $2, (Block) $4, lexer.Location);
2512 current_local_parameters = (Parameters) oob_stack.Pop ();
2516 opt_anonymous_method_signature
2517 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
2518 | anonymous_method_signature
2521 anonymous_method_signature
2522 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
2525 $$ = Parameters.EmptyReadOnlyParameters;
2527 ArrayList par_list = (ArrayList) $2;
2528 Parameter [] pars = new Parameter [par_list.Count];
2529 par_list.CopyTo (pars);
2530 $$ = new Parameters (pars, null, lexer.Location);
2535 opt_anonymous_method_parameter_list
2536 : /* empty */ { $$ = null; }
2537 | anonymous_method_parameter_list { $$ = $1; }
2540 anonymous_method_parameter_list
2541 : anonymous_method_parameter
2543 ArrayList a = new ArrayList (4);
2547 | anonymous_method_parameter_list COMMA anonymous_method_parameter
2549 ArrayList a = (ArrayList) $1;
2555 anonymous_method_parameter
2556 : opt_parameter_modifier type IDENTIFIER {
2557 $$ = new Parameter ((Expression) $2, (string) $2, (Parameter.Modifier) $1, null);
2561 default_value_expression
2562 : primary_expression DOT DEFAULT
2564 $$ = new DefaultValueExpression ((Expression) $1, lexer.Location);
2566 | predefined_type DOT DEFAULT
2568 $$ = new DefaultValueExpression ((Expression) $1, lexer.Location);
2573 : primary_expression
2574 | BANG prefixed_unary_expression
2576 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2578 | TILDE prefixed_unary_expression
2580 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2586 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
2588 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2590 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
2592 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2598 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2600 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2605 // The idea to split this out is from Rhys' grammar
2606 // to solve the problem with casts.
2608 prefixed_unary_expression
2610 | PLUS prefixed_unary_expression
2612 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2614 | MINUS prefixed_unary_expression
2616 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2618 | OP_INC prefixed_unary_expression
2620 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2621 (Expression) $2, lexer.Location);
2623 | OP_DEC prefixed_unary_expression
2625 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2626 (Expression) $2, lexer.Location);
2628 | STAR prefixed_unary_expression
2630 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2632 | BITWISE_AND prefixed_unary_expression
2634 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2638 pre_increment_expression
2639 : OP_INC prefixed_unary_expression
2641 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2642 (Expression) $2, lexer.Location);
2646 pre_decrement_expression
2647 : OP_DEC prefixed_unary_expression
2649 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2650 (Expression) $2, lexer.Location);
2654 multiplicative_expression
2655 : prefixed_unary_expression
2656 | multiplicative_expression STAR prefixed_unary_expression
2658 $$ = new Binary (Binary.Operator.Multiply,
2659 (Expression) $1, (Expression) $3, lexer.Location);
2661 | multiplicative_expression DIV prefixed_unary_expression
2663 $$ = new Binary (Binary.Operator.Division,
2664 (Expression) $1, (Expression) $3, lexer.Location);
2666 | multiplicative_expression PERCENT prefixed_unary_expression
2668 $$ = new Binary (Binary.Operator.Modulus,
2669 (Expression) $1, (Expression) $3, lexer.Location);
2674 : multiplicative_expression
2675 | additive_expression PLUS multiplicative_expression
2677 $$ = new Binary (Binary.Operator.Addition,
2678 (Expression) $1, (Expression) $3, lexer.Location);
2680 | additive_expression MINUS multiplicative_expression
2682 $$ = new Binary (Binary.Operator.Subtraction,
2683 (Expression) $1, (Expression) $3, lexer.Location);
2688 : additive_expression
2689 | shift_expression OP_SHIFT_LEFT additive_expression
2691 $$ = new Binary (Binary.Operator.LeftShift,
2692 (Expression) $1, (Expression) $3, lexer.Location);
2694 | shift_expression OP_SHIFT_RIGHT additive_expression
2696 $$ = new Binary (Binary.Operator.RightShift,
2697 (Expression) $1, (Expression) $3, lexer.Location);
2701 relational_expression
2703 | relational_expression OP_LT shift_expression
2705 $$ = new Binary (Binary.Operator.LessThan,
2706 (Expression) $1, (Expression) $3, lexer.Location);
2708 | relational_expression OP_GT shift_expression
2710 $$ = new Binary (Binary.Operator.GreaterThan,
2711 (Expression) $1, (Expression) $3, lexer.Location);
2713 | relational_expression OP_LE shift_expression
2715 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2716 (Expression) $1, (Expression) $3, lexer.Location);
2718 | relational_expression OP_GE shift_expression
2720 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2721 (Expression) $1, (Expression) $3, lexer.Location);
2723 | relational_expression IS type
2725 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
2727 | relational_expression AS type
2729 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
2734 : relational_expression
2735 | equality_expression OP_EQ relational_expression
2737 $$ = new Binary (Binary.Operator.Equality,
2738 (Expression) $1, (Expression) $3, lexer.Location);
2740 | equality_expression OP_NE relational_expression
2742 $$ = new Binary (Binary.Operator.Inequality,
2743 (Expression) $1, (Expression) $3, lexer.Location);
2748 : equality_expression
2749 | and_expression BITWISE_AND equality_expression
2751 $$ = new Binary (Binary.Operator.BitwiseAnd,
2752 (Expression) $1, (Expression) $3, lexer.Location);
2756 exclusive_or_expression
2758 | exclusive_or_expression CARRET and_expression
2760 $$ = new Binary (Binary.Operator.ExclusiveOr,
2761 (Expression) $1, (Expression) $3, lexer.Location);
2765 inclusive_or_expression
2766 : exclusive_or_expression
2767 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2769 $$ = new Binary (Binary.Operator.BitwiseOr,
2770 (Expression) $1, (Expression) $3, lexer.Location);
2774 conditional_and_expression
2775 : inclusive_or_expression
2776 | conditional_and_expression OP_AND inclusive_or_expression
2778 $$ = new Binary (Binary.Operator.LogicalAnd,
2779 (Expression) $1, (Expression) $3, lexer.Location);
2783 conditional_or_expression
2784 : conditional_and_expression
2785 | conditional_or_expression OP_OR conditional_and_expression
2787 $$ = new Binary (Binary.Operator.LogicalOr,
2788 (Expression) $1, (Expression) $3, lexer.Location);
2792 conditional_expression
2793 : conditional_or_expression
2794 | conditional_or_expression INTERR expression COLON expression
2796 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2800 assignment_expression
2801 : prefixed_unary_expression ASSIGN expression
2803 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2805 | prefixed_unary_expression OP_MULT_ASSIGN expression
2807 Location l = lexer.Location;
2809 $$ = new CompoundAssign (
2810 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
2812 | prefixed_unary_expression OP_DIV_ASSIGN expression
2814 Location l = lexer.Location;
2816 $$ = new CompoundAssign (
2817 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
2819 | prefixed_unary_expression OP_MOD_ASSIGN expression
2821 Location l = lexer.Location;
2823 $$ = new CompoundAssign (
2824 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
2826 | prefixed_unary_expression OP_ADD_ASSIGN expression
2828 Location l = lexer.Location;
2830 $$ = new CompoundAssign (
2831 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
2833 | prefixed_unary_expression OP_SUB_ASSIGN expression
2835 Location l = lexer.Location;
2837 $$ = new CompoundAssign (
2838 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
2840 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2842 Location l = lexer.Location;
2844 $$ = new CompoundAssign (
2845 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
2847 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2849 Location l = lexer.Location;
2851 $$ = new CompoundAssign (
2852 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
2854 | prefixed_unary_expression OP_AND_ASSIGN expression
2856 Location l = lexer.Location;
2858 $$ = new CompoundAssign (
2859 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
2861 | prefixed_unary_expression OP_OR_ASSIGN expression
2863 Location l = lexer.Location;
2865 $$ = new CompoundAssign (
2866 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
2868 | prefixed_unary_expression OP_XOR_ASSIGN expression
2870 Location l = lexer.Location;
2872 $$ = new CompoundAssign (
2873 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
2878 : conditional_expression
2879 | assignment_expression
2901 name = MakeName ((string) $4);
2903 new_class = new Class (current_namespace, current_container, name, (int) $2,
2904 (Attributes) $1, lexer.Location);
2905 current_container = new_class;
2906 RootContext.Tree.RecordDecl (name, new_class);
2908 opt_type_parameter_list
2910 opt_type_parameter_constraints_clauses
2914 Class new_class = (Class) current_container;
2916 if ($8 != null && $6 == null)
2917 Report.Error (-200, new_class.Location,
2918 "Type parameter constraints only valid if there is a type parameter list");
2921 CheckDef (new_class.SetParameterInfo ((ArrayList) $6, (ArrayList) $8), new_class.Name, new_class.Location);
2923 new_class.Bases = (ArrayList) $7;
2924 current_container = current_container.Parent;
2925 CheckDef (current_container.AddClass (new_class), new_class.Name, new_class.Location);
2933 Report.Error (-200, lexer.Location,
2934 "Type parameter constraints only valid if there is a type parameter list");
2941 : /* empty */ { $$ = (int) 0; }
2947 | modifiers modifier
2952 if ((m1 & m2) != 0) {
2953 Location l = lexer.Location;
2954 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2956 $$ = (int) (m1 | m2);
2961 : NEW { $$ = Modifiers.NEW; }
2962 | PUBLIC { $$ = Modifiers.PUBLIC; }
2963 | PROTECTED { $$ = Modifiers.PROTECTED; }
2964 | INTERNAL { $$ = Modifiers.INTERNAL; }
2965 | PRIVATE { $$ = Modifiers.PRIVATE; }
2966 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2967 | SEALED { $$ = Modifiers.SEALED; }
2968 | STATIC { $$ = Modifiers.STATIC; }
2969 | READONLY { $$ = Modifiers.READONLY; }
2970 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2971 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2972 | EXTERN { $$ = Modifiers.EXTERN; }
2973 | VOLATILE { $$ = Modifiers.VOLATILE; }
2974 | UNSAFE { $$ = Modifiers.UNSAFE; }
2978 : /* empty */ { $$ = null; }
2979 | class_base { $$ = $1; }
2983 : COLON type_list { $$ = $2; }
2986 opt_type_parameter_list
2987 : /* empty */ { $$ = null; }
2988 | type_parameter_list { $$ = $1; }
2992 : OP_GENERICS_LT type_parameters OP_GENERICS_GT { $$ = $2; }
2998 // Do some profiling to find the optimal size, for now we
2999 // assume most people will be generic on one type (saves space
3001 ArrayList type_parameters = new ArrayList (1);
3002 type_parameters.Add ($1);
3003 $$ = type_parameters;
3005 | type_parameters COMMA type_parameter {
3006 ArrayList type_parameters = (ArrayList) $1;
3008 type_parameters.Add ($3);
3009 $$ = type_parameters;
3017 opt_type_parameter_constraints_clauses
3018 : /* empty */ { $$ = null; }
3019 | type_parameter_constraints_clauses
3023 type_parameter_constraints_clauses
3024 : type_parameter_constraints_clause
3025 | type_parameter_constraints_clauses type_parameter_constraints_clause {
3026 ArrayList constraints = (ArrayList) $1;
3028 constraints.Add ($2);
3033 type_parameter_constraints_clause
3034 : WHERE type_parameter COLON type_parameter_constraints {
3035 ArrayList constraints = new ArrayList (1);
3036 constraints.Add (new Constraints ((string) $2, (ArrayList) $4, lexer.Location));
3041 type_parameter_constraints
3042 : type_parameter_constraint {
3043 ArrayList constraints = new ArrayList (1);
3044 constraints.Add ($1);
3047 | type_parameter_constraints COMMA type_parameter_constraint {
3048 ArrayList constraints = (ArrayList) $1;
3050 constraints.Add ($3);
3055 type_parameter_constraint
3057 | NEW OPEN_PARENS CLOSE_PARENS {
3067 // A block is "contained" on the following places:
3069 // property_declaration as part of the accessor body (get/set)
3070 // operator_declaration
3071 // constructor_declaration
3072 // destructor_declaration
3073 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3078 current_block = new Block (current_block, current_local_parameters,
3079 lexer.Location, Location.Null);
3081 opt_statement_list CLOSE_BRACE
3083 while (current_block.Implicit)
3084 current_block = current_block.Parent;
3086 current_block.SetEndLocation (lexer.Location);
3087 current_block = current_block.Parent;
3098 | statement_list statement
3102 : declaration_statement
3104 if ($1 != null && (Block) $1 != current_block){
3105 current_block.AddStatement ((Statement) $1);
3106 current_block = (Block) $1;
3109 | embedded_statement
3111 Statement s = (Statement) $1;
3114 current_block.AddStatement ((Statement) $1);
3122 | expression_statement
3123 | selection_statement
3124 | iteration_statement
3128 | unchecked_statement
3138 $$ = new EmptyStatement ();
3145 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3147 if (!current_block.AddLabel ((string) $1, labeled)){
3148 Location l = lexer.Location;
3149 Report.Error (140, l, "The label '" + ((string) $1) + "' is a duplicate");
3151 current_block.AddStatement (labeled);
3156 declaration_statement
3157 : local_variable_declaration SEMICOLON
3160 DictionaryEntry de = (DictionaryEntry) $1;
3162 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3166 | local_constant_declaration SEMICOLON
3169 DictionaryEntry de = (DictionaryEntry) $1;
3171 $$ = declare_local_constant ((Expression) de.Key, (VariableDeclaration) de.Value);
3177 * The following is from Rhys' grammar:
3178 * > Types in local variable declarations must be recognized as
3179 * > expressions to prevent reduce/reduce errors in the grammar.
3180 * > The expressions are converted into types during semantic analysis.
3183 : primary_expression opt_rank_specifier
3185 // FIXME: Do something smart here regarding the composition of the type.
3187 // Ok, the above "primary_expression" is there to get rid of
3188 // both reduce/reduce and shift/reduces in the grammar, it should
3189 // really just be "type_name". If you use type_name, a reduce/reduce
3190 // creeps up. If you use qualified_identifier (which is all we need
3191 // really) two shift/reduces appear.
3194 // So the super-trick is that primary_expression
3195 // can only be either a SimpleName or a MemberAccess.
3196 // The MemberAccess case arises when you have a fully qualified type-name like :
3198 // SimpleName is when you have
3201 Expression expr = (Expression) $1;
3202 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType)) {
3203 Error_ExpectingTypeName (lexer.Location, expr);
3207 // So we extract the string corresponding to the SimpleName
3211 if ((string) $2 == "")
3214 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3217 | builtin_types opt_rank_specifier
3219 if ((string) $2 == "")
3222 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3226 local_variable_pointer_type
3227 : primary_expression STAR
3229 Expression expr = (Expression) $1;
3230 Location l = lexer.Location;
3232 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType)) {
3233 Error_ExpectingTypeName (l, expr);
3237 $$ = new ComposedCast ((Expression) $1, "*", l);
3239 | builtin_types STAR
3241 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3245 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3247 | local_variable_pointer_type STAR
3249 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3253 local_variable_declaration
3254 : local_variable_type variable_declarators
3257 $$ = new DictionaryEntry ($1, $2);
3261 | local_variable_pointer_type opt_rank_specifier variable_declarators
3266 if ((string) $2 == "")
3267 t = (Expression) $1;
3269 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3270 $$ = new DictionaryEntry (t, $3);
3276 local_constant_declaration
3277 : CONST local_variable_type constant_declarator
3280 $$ = new DictionaryEntry ($2, $3);
3286 expression_statement
3287 : statement_expression SEMICOLON
3294 // We have to do the wrapping here and not in the case above,
3295 // because statement_expression is used for example in for_statement
3297 statement_expression
3298 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3299 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3300 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3301 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3302 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3303 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3304 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3306 Report.Error (1002, lexer.Location, "Expecting `;'");
3311 object_creation_expression
3312 : object_or_delegate_creation_expression
3313 { note ("complain if this is a delegate maybe?"); }
3322 : if_statement_open if_statement_rest
3331 oob_stack.Push (lexer.Location);
3336 : boolean_expression CLOSE_PARENS
3339 Location l = (Location) oob_stack.Pop ();
3341 $$ = new If ((Expression) $1, (Statement) $3, l);
3343 if (RootContext.WarningLevel >= 3){
3344 if ($3 is EmptyStatement)
3345 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3349 | boolean_expression CLOSE_PARENS
3350 embedded_statement ELSE embedded_statement
3352 Location l = (Location) oob_stack.Pop ();
3354 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
3359 : SWITCH OPEN_PARENS
3361 oob_stack.Push (lexer.Location);
3362 switch_stack.Push (current_block);
3364 expression CLOSE_PARENS
3367 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
3368 current_block = (Block) switch_stack.Pop ();
3384 Report.Error (1522, lexer.Location, "Empty switch block");
3392 ArrayList sections = new ArrayList (4);
3397 | switch_sections switch_section
3399 ArrayList sections = (ArrayList) $1;
3409 current_block = current_block.CreateSwitchBlock (lexer.Location);
3413 Block topmost = current_block;
3415 while (topmost.Implicit)
3416 topmost = topmost.Parent;
3417 $$ = new SwitchSection ((ArrayList) $1, topmost);
3424 ArrayList labels = new ArrayList (4);
3429 | switch_labels switch_label
3431 ArrayList labels = (ArrayList) ($1);
3439 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3440 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3443 1523, lexer.Location,
3444 "The keyword case or default must precede code in switch block");
3458 oob_stack.Push (lexer.Location);
3460 boolean_expression CLOSE_PARENS embedded_statement
3462 Location l = (Location) oob_stack.Pop ();
3463 $$ = new While ((Expression) $4, (Statement) $6, l);
3465 if (RootContext.WarningLevel >= 3){
3466 if ($6 is EmptyStatement)
3467 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3473 : DO embedded_statement
3475 oob_stack.Push (lexer.Location);
3477 boolean_expression CLOSE_PARENS SEMICOLON
3479 Location l = (Location) oob_stack.Pop ();
3481 $$ = new Do ((Statement) $2, (Expression) $6, l);
3487 opt_for_initializer SEMICOLON
3489 Block assign_block = new Block (current_block);
3490 current_block = assign_block;
3492 if ($3 is DictionaryEntry){
3493 DictionaryEntry de = (DictionaryEntry) $3;
3495 Expression type = (Expression) de.Key;
3496 ArrayList var_declarators = (ArrayList) de.Value;
3498 foreach (VariableDeclaration decl in var_declarators){
3502 vi = current_block.AddVariable (
3503 type, decl.identifier, current_local_parameters, decl.Location);
3507 Location l = lexer.Location;
3509 if (decl.expression_or_array_initializer is Expression){
3510 expr = (Expression) decl.expression_or_array_initializer;
3511 } else if (decl.expression_or_array_initializer == null) {
3514 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3515 expr = new ArrayCreation (type, "", init, decl.Location);
3518 LocalVariableReference var;
3519 var = new LocalVariableReference (assign_block, decl.identifier, l);
3522 Assign a = new Assign (var, expr, decl.Location);
3524 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3530 oob_stack.Push (lexer.Location);
3532 opt_for_condition SEMICOLON
3533 opt_for_iterator CLOSE_PARENS
3536 Location l = (Location) oob_stack.Pop ();
3538 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3540 if (RootContext.WarningLevel >= 3){
3541 if ($10 is EmptyStatement)
3542 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3545 current_block.AddStatement (f);
3546 while (current_block.Implicit)
3547 current_block = current_block.Parent;
3549 current_block = current_block.Parent;
3554 : /* empty */ { $$ = new EmptyStatement (); }
3559 : local_variable_declaration
3560 | statement_expression_list
3564 : /* empty */ { $$ = null; }
3565 | boolean_expression
3569 : /* empty */ { $$ = new EmptyStatement (); }
3574 : statement_expression_list
3577 statement_expression_list
3578 : statement_expression
3580 // CHANGE: was `null'
3581 Block b = new Block (current_block, Block.Flags.Implicit);
3583 b.AddStatement ((Statement) $1);
3586 | statement_expression_list COMMA statement_expression
3588 Block b = (Block) $1;
3590 b.AddStatement ((Statement) $3);
3596 : FOREACH OPEN_PARENS type IDENTIFIER IN
3598 oob_stack.Push (lexer.Location);
3600 expression CLOSE_PARENS
3602 oob_stack.Push (current_block);
3604 Block foreach_block = new Block (current_block, Block.Flags.Implicit);
3605 LocalVariableReference v = null;
3606 Location l = lexer.Location;
3609 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3613 // Get a writable reference to this read-only variable.
3614 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3616 current_block = foreach_block;
3619 oob_stack.Push (current_block);
3623 Block foreach_block = (Block) oob_stack.Pop ();
3624 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3625 Block prev_block = (Block) oob_stack.Pop ();
3626 Location l = (Location) oob_stack.Pop ();
3628 current_block = prev_block;
3631 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3632 foreach_block.AddStatement (f);
3641 | continue_statement
3651 $$ = new Break (lexer.Location);
3656 : CONTINUE SEMICOLON
3658 $$ = new Continue (lexer.Location);
3663 : GOTO IDENTIFIER SEMICOLON
3665 $$ = new Goto (current_block, (string) $2, lexer.Location);
3667 | GOTO CASE constant_expression SEMICOLON
3669 $$ = new GotoCase ((Expression) $3, lexer.Location);
3671 | GOTO DEFAULT SEMICOLON
3673 $$ = new GotoDefault (lexer.Location);
3678 : RETURN opt_expression SEMICOLON
3680 $$ = new Return ((Expression) $2, lexer.Location);
3685 : THROW opt_expression SEMICOLON
3687 $$ = new Throw ((Expression) $2, lexer.Location);
3692 : YIELD RETURN expression SEMICOLON
3694 if (iterator_container == null){
3695 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3698 iterator_container.SetYields ();
3699 $$ = new Yield ((Expression) $3, lexer.Location);
3702 | YIELD expression SEMICOLON
3704 if (iterator_container == null){
3705 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3708 iterator_container.SetYields ();
3709 $$ = new Yield ((Expression) $2, lexer.Location);
3712 | YIELD BREAK SEMICOLON
3714 if (iterator_container == null){
3715 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3718 iterator_container.SetYields ();
3719 $$ = new YieldBreak (lexer.Location);
3730 : TRY block catch_clauses
3733 ArrayList s = new ArrayList (4);
3735 foreach (Catch cc in (ArrayList) $3) {
3742 // Now s contains the list of specific catch clauses
3743 // and g contains the general one.
3745 $$ = new Try ((Block) $2, s, g, null, lexer.Location);
3747 | TRY block opt_catch_clauses FINALLY block
3750 ArrayList s = new ArrayList (4);
3751 ArrayList catch_list = (ArrayList) $3;
3753 if (catch_list != null){
3754 foreach (Catch cc in catch_list) {
3762 $$ = new Try ((Block) $2, s, g, (Block) $5, lexer.Location);
3766 Report.Error (1524, lexer.Location, "Expected catch or finally");
3771 : /* empty */ { $$ = null; }
3778 ArrayList l = new ArrayList (4);
3783 | catch_clauses catch_clause
3785 ArrayList l = (ArrayList) $1;
3793 : /* empty */ { $$ = null; }
3798 : CATCH opt_catch_args
3800 Expression type = null;
3804 DictionaryEntry cc = (DictionaryEntry) $2;
3805 type = (Expression) cc.Key;
3806 id = (string) cc.Value;
3809 ArrayList one = new ArrayList (4);
3810 Location loc = lexer.Location;
3812 one.Add (new VariableDeclaration (id, null, loc));
3815 current_block = new Block (current_block);
3816 Block b = declare_local_variables (type, one, loc);
3821 Expression type = null;
3825 DictionaryEntry cc = (DictionaryEntry) $2;
3826 type = (Expression) cc.Key;
3827 id = (string) cc.Value;
3831 // FIXME: I can change this for an assignment.
3833 while (current_block != (Block) $1)
3834 current_block = current_block.Parent;
3839 $$ = new Catch (type, id , (Block) $4, lexer.Location);
3844 : /* empty */ { $$ = null; }
3849 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3851 $$ = new DictionaryEntry ($2, $3);
3858 $$ = new Checked ((Block) $2);
3865 $$ = new Unchecked ((Block) $2);
3872 if (!RootContext.Unsafe){
3873 Report.Error (227, lexer.Location,
3874 "Unsafe code can only be used if --unsafe is used");
3877 $$ = new Unsafe ((Block) $3);
3883 type fixed_pointer_declarators
3886 Block assign_block = new Block (current_block, Block.Flags.Implicit);
3887 ArrayList list = (ArrayList) $4;
3888 Expression type = (Expression) $3;
3889 Location l = lexer.Location;
3890 int top = list.Count;
3892 for (int i = 0; i < top; i++){
3893 Pair p = (Pair) list [i];
3896 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3903 current_block.AddStatement (assign_block);
3904 current_block = assign_block;
3905 oob_stack.Push (assign_block);
3910 Location l = (Location) oob_stack.Pop ();
3911 Block assign_block = (Block) oob_stack.Pop ();
3913 ArrayList list = (ArrayList) $4;
3914 int top = list.Count;
3916 $$ = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
3920 fixed_pointer_declarators
3921 : fixed_pointer_declarator {
3922 ArrayList declarators = new ArrayList (4);
3923 declarators.Add ($1);
3926 | fixed_pointer_declarators COMMA fixed_pointer_declarator
3928 ArrayList declarators = (ArrayList) $1;
3929 declarators.Add ($3);
3934 fixed_pointer_declarator
3935 : IDENTIFIER ASSIGN expression
3937 $$ = new Pair ($1, $3);
3942 : LOCK OPEN_PARENS expression CLOSE_PARENS
3948 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
3953 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
3955 Block assign_block = new Block (current_block);
3956 current_block = assign_block;
3958 oob_stack.Push (lexer.Location);
3960 if ($3 is DictionaryEntry){
3961 DictionaryEntry de = (DictionaryEntry) $3;
3962 Location l = lexer.Location;
3964 Expression type = (Expression) de.Key;
3965 ArrayList var_declarators = (ArrayList) de.Value;
3967 ArrayList vars = new ArrayList (4);
3969 foreach (VariableDeclaration decl in var_declarators){
3971 LocalInfo vi = current_block.AddVariable (
3972 type, decl.identifier,
3973 current_local_parameters, decl.Location);
3979 if (decl.expression_or_array_initializer is Expression){
3980 expr = (Expression) decl.expression_or_array_initializer;
3982 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3984 expr = new ArrayCreation (type, "", init, decl.Location);
3987 LocalVariableReference var;
3989 // Get a writable reference to this read-only variable.
3990 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
3992 // This is so that it is not a warning on using variables
3995 vars.Add (new DictionaryEntry (var, expr));
3997 // Assign a = new Assign (var, expr, decl.Location);
3998 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4000 $3 = new DictionaryEntry (type, vars);
4005 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
4006 current_block.AddStatement (u);
4007 while (current_block.Implicit)
4008 current_block = current_block.Parent;
4010 current_block = current_block.Parent;
4014 resource_acquisition
4015 : local_variable_declaration
4022 // A class used to pass around variable declarations and constants
4024 public class VariableDeclaration {
4025 public string identifier;
4026 public object expression_or_array_initializer;
4027 public Location Location;
4028 public Attributes OptAttributes;
4030 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4032 this.identifier = id;
4033 this.expression_or_array_initializer = eoai;
4035 this.OptAttributes = opt_attrs;
4038 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4044 // A class used to hold info about an indexer declarator
4047 public class IndexerDeclaration {
4048 public Expression type;
4049 public string interface_type;
4050 public Parameters param_list;
4052 public IndexerDeclaration (Expression type, string interface_type, Parameters param_list)
4055 this.interface_type = interface_type;
4056 this.param_list = param_list;
4061 // A class used to hold info about an operator declarator
4064 public class OperatorDeclaration {
4065 public Operator.OpType optype;
4066 public Expression ret_type, arg1type, arg2type;
4067 public string arg1name, arg2name;
4068 public Location location;
4070 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4071 Expression arg1type, string arg1name,
4072 Expression arg2type, string arg2name, Location location)
4075 this.ret_type = ret_type;
4076 this.arg1type = arg1type;
4077 this.arg1name = arg1name;
4078 this.arg2type = arg2type;
4079 this.arg2name = arg2name;
4080 this.location = location;
4085 void Error_ExpectingTypeName (Location l, Expression expr)
4087 if (expr is Invocation){
4088 Report.Error (1002, l, "; expected");
4090 Report.Error (-1, l, "Invalid Type definition");
4095 // Given the @class_name name, it creates a fully qualified name
4096 // based on the containing declaration space
4099 MakeName (string class_name)
4101 string ns = current_namespace.FullName;
4102 string container_name = current_container.Name;
4104 if (container_name == ""){
4106 return ns + "." + class_name;
4110 return container_name + "." + class_name;
4114 // Used to report back to the user the result of a declaration
4115 // in the current declaration space
4118 CheckDef (DeclSpace.AdditionResult result, string name, Location l)
4120 if (result == DeclSpace.AdditionResult.Success)
4124 case DeclSpace.AdditionResult.NameExists:
4125 Report.Error (102, l, "The container `" + current_container.Name +
4126 "' already contains a definition for `"+
4132 // This is handled only for static Constructors, because
4133 // in reality we handle these by the semantic analysis later
4135 case DeclSpace.AdditionResult.MethodExists:
4137 111, l, "Class `"+current_container.Name+
4138 "' already defines a member called '" +
4139 name + "' with the same parameter types (more than one default constructor)");
4142 case DeclSpace.AdditionResult.EnclosingClash:
4143 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
4146 case DeclSpace.AdditionResult.NotAConstructor:
4147 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
4150 case DeclSpace.AdditionResult.Error:
4151 // Error has already been reported.
4157 CheckDef (bool result, string name, Location l)
4161 CheckDef (DeclSpace.AdditionResult.NameExists, name, l);
4164 Expression DecomposeQI (string name, Location loc)
4168 if (name.IndexOf ('.') == -1){
4169 return new SimpleName (name, loc);
4171 int pos = name.LastIndexOf (".");
4172 string left = name.Substring (0, pos);
4173 string right = name.Substring (pos + 1);
4175 o = DecomposeQI (left, loc);
4177 return new MemberAccess (o, right, loc);
4181 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4183 Block implicit_block;
4184 ArrayList inits = null;
4187 // We use the `Used' property to check whether statements
4188 // have been added to the current block. If so, we need
4189 // to create another block to contain the new declaration
4190 // otherwise, as an optimization, we use the same block to
4191 // add the declaration.
4193 // FIXME: A further optimization is to check if the statements
4194 // that were added were added as part of the initialization
4195 // below. In which case, no other statements have been executed
4196 // and we might be able to reduce the number of blocks for
4197 // situations like this:
4199 // int j = 1; int k = j + 1;
4201 if (current_block.Used) {
4202 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4203 implicit_block.AddChildVariableNames (current_block);
4205 implicit_block = current_block;
4207 foreach (VariableDeclaration decl in variable_declarators){
4209 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4210 if (decl.expression_or_array_initializer != null){
4212 inits = new ArrayList (4);
4219 return implicit_block;
4221 foreach (VariableDeclaration decl in inits){
4225 if (decl.expression_or_array_initializer is Expression){
4226 expr = (Expression) decl.expression_or_array_initializer;
4229 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4231 expr = new ArrayCreation (type, "", init, decl.Location);
4234 LocalVariableReference var;
4235 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4237 assign = new Assign (var, expr, decl.Location);
4239 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4242 return implicit_block;
4245 Block declare_local_constant (Expression type, VariableDeclaration decl)
4247 Block implicit_block;
4249 if (current_block.Used)
4250 implicit_block = new Block (current_block, Block.Flags.Implicit);
4252 implicit_block = current_block;
4254 if (!(implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4255 current_local_parameters, decl.Location))){
4258 return implicit_block;
4261 void CheckAttributeTarget (string a)
4265 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
4269 Location l = lexer.Location;
4270 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4276 void CheckUnaryOperator (Operator.OpType op)
4280 case Operator.OpType.LogicalNot:
4281 case Operator.OpType.OnesComplement:
4282 case Operator.OpType.Increment:
4283 case Operator.OpType.Decrement:
4284 case Operator.OpType.True:
4285 case Operator.OpType.False:
4286 case Operator.OpType.Addition:
4287 case Operator.OpType.Subtraction:
4292 Location l = lexer.Location;
4293 Report.Error (1019, l, "Overloadable unary operator expected");
4299 void CheckBinaryOperator (Operator.OpType op)
4303 case Operator.OpType.Addition:
4304 case Operator.OpType.Subtraction:
4305 case Operator.OpType.Multiply:
4306 case Operator.OpType.Division:
4307 case Operator.OpType.Modulus:
4308 case Operator.OpType.BitwiseAnd:
4309 case Operator.OpType.BitwiseOr:
4310 case Operator.OpType.ExclusiveOr:
4311 case Operator.OpType.LeftShift:
4312 case Operator.OpType.RightShift:
4313 case Operator.OpType.Equality:
4314 case Operator.OpType.Inequality:
4315 case Operator.OpType.GreaterThan:
4316 case Operator.OpType.LessThan:
4317 case Operator.OpType.GreaterThanOrEqual:
4318 case Operator.OpType.LessThanOrEqual:
4322 Location l = lexer.Location;
4323 Report.Error (1020, l, "Overloadable binary operator expected");
4329 void syntax_error (Location l, string msg)
4331 Report.Error (1003, l, "Syntax error, " + msg);
4334 void output (string s)
4336 Console.WriteLine (s);
4339 void note (string s)
4341 // Used to put annotations
4346 public Tokenizer Lexer {
4352 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4354 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4355 this.name = file.Name;
4357 current_container = RootContext.Tree.Types;
4358 current_container.NamespaceEntry = current_namespace;
4359 oob_stack = new Stack ();
4360 switch_stack = new Stack ();
4362 lexer = new Tokenizer (reader, file, defines);
4365 public void parse ()
4368 if (yacc_verbose_flag)
4369 yyparse (lexer, new yydebug.yyDebugSimple ());
4372 Tokenizer tokenizer = lexer as Tokenizer;
4373 tokenizer.cleanup ();
4374 } catch (Exception e){
4375 // Please do not remove this, it is used during debugging
4378 Console.WriteLine (e);
4379 Report.Error (-25, lexer.Location, "Parsing error");
4380 if (Driver.parser_verbose)
4381 Console.WriteLine (e);
4385 void CheckToken (int error, int yyToken, string msg)
4387 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4388 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4391 Report.Error (error, lexer.Location, msg);
4394 void CheckIdentifierToken (int yyToken)
4396 CheckToken (1041, yyToken, "Identifier expected");