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:
24 using System.Collections;
30 public class CSharpParser : GenericParser {
31 Namespace current_namespace;
32 TypeContainer current_container;
35 // Current block is used to add statements as we find
42 // Current interface is used by the various declaration
43 // productions in the interface declaration to "add"
44 // the interfaces as we find them.
46 Interface current_interface;
49 // This is used by the unary_expression code to resolve
50 // a name against a parameter.
52 Parameters current_local_parameters;
55 // Using during property parsing to describe the implicit
56 // value parameter that is passed to the "set" and "get"accesor
57 // methods (properties and indexers).
59 Expression implicit_value_parameter_type;
60 Parameters indexer_parameters;
63 // Used to determine if we are parsing the get/set pair
64 // of an indexer or a property
69 // An out-of-band stack.
80 %token NONE /* This token is never returned by our lexer */
81 %token ERROR // This is used not by the parser, but by the tokenizer.
85 *These are the C# keywords
168 /* C# keywords which are not really keywords */
172 /* C# single character operators/punctuation. */
173 %token OPEN_BRACE "{"
174 %token CLOSE_BRACE "}"
175 %token OPEN_BRACKET "["
176 %token CLOSE_BRACKET "]"
177 %token OPEN_PARENS "("
178 %token CLOSE_PARENS ")"
191 %token BITWISE_AND "&"
192 %token BITWISE_OR "|"
199 /* C# multi-character operators. */
202 %token OP_SHIFT_LEFT "<<"
203 %token OP_SHIFT_RIGHT ">>"
210 %token OP_MULT_ASSIGN "*="
211 %token OP_DIV_ASSIGN "/="
212 %token OP_MOD_ASSIGN "%="
213 %token OP_ADD_ASSIGN "+="
214 %token OP_SUB_ASSIGN "-="
215 %token OP_SHIFT_LEFT_ASSIGN "<<="
216 %token OP_SHIFT_RIGHT_ASSIGN ">>="
217 %token OP_AND_ASSIGN "&="
218 %token OP_XOR_ASSIGN "^="
219 %token OP_OR_ASSIGN "|="
223 %token LITERAL_INTEGER "int literal"
224 %token LITERAL_FLOAT "float literal"
225 %token LITERAL_DOUBLE "double literal"
226 %token LITERAL_DECIMAL "decimal literal"
227 %token LITERAL_CHARACTER "character literal"
228 %token LITERAL_STRING "string literal"
232 /* Add precedence rules to solve dangling else s/r conflict */
241 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
243 %left STAR DIV PERCENT
244 %right BANG CARRET UMINUS
245 %nonassoc OP_INC OP_DEC
247 %left OPEN_BRACKET OPEN_BRACE
251 %start compilation_unit
255 : outer_declarations opt_EOF
256 | outer_declarations attribute_sections opt_EOF
257 | attribute_sections opt_EOF
258 | opt_EOF /* allow empty files */
268 | outer_declarations outer_declaration
273 | namespace_member_declaration
278 | using_directives using_directive
282 : using_alias_directive
283 | using_namespace_directive
286 using_alias_directive
287 : USING IDENTIFIER ASSIGN
288 namespace_or_type_name SEMICOLON
290 current_namespace.UsingAlias ((string) $2, (string) $4, lexer.Location);
294 using_namespace_directive
295 : USING namespace_name SEMICOLON
297 current_namespace.Using ((string) $2, lexer.Location);
302 // Strictly speaking, namespaces don't have attributes but
303 // we parse global attributes along with namespace declarations and then
306 namespace_declaration
307 : opt_attributes NAMESPACE qualified_identifier
309 Attributes attrs = (Attributes) $1;
312 foreach (AttributeSection asec in attrs.AttributeSections)
313 if (asec.Target == "assembly")
314 RootContext.AddGlobalAttributeSection (current_container, asec);
317 current_namespace = RootContext.Tree.RecordNamespace (current_namespace, name, (string) $3);
319 namespace_body opt_semicolon
321 current_namespace = current_namespace.Parent;
337 | qualified_identifier DOT IDENTIFIER {
338 $$ = (($1).ToString ()) + "." + ($3.ToString ()); }
343 : namespace_or_type_name
349 opt_namespace_member_declarations
360 opt_namespace_member_declarations
362 | namespace_member_declarations
365 namespace_member_declarations
366 : namespace_member_declaration
367 | namespace_member_declarations namespace_member_declaration
370 namespace_member_declaration
377 Class c = (Class) $1;
378 mod_flags = c.ModFlags;
380 } else if ($1 is Struct){
381 Struct s = (Struct) $1;
382 mod_flags = s.ModFlags;
387 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
389 1527, lexer.Location,
390 "Namespace elements cant be explicitly " +
391 "declared private or protected in `" + name + "'");
393 current_namespace.DeclarationFound = true;
395 | namespace_declaration
401 | interface_declaration
403 | delegate_declaration
405 // Enable this when we have handled all errors, because this acts as a generic fallback
408 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
418 | attribute_sections { $$ = $1; }
424 AttributeSection sect = (AttributeSection) $1;
426 if (sect.Target == "assembly")
427 RootContext.AddGlobalAttributeSection (current_container, sect);
430 $$ = new Attributes ((AttributeSection) $1);
432 | attribute_sections attribute_section
434 Attributes attrs = null;
435 AttributeSection sect = (AttributeSection) $2;
437 if (sect.Target == "assembly")
438 RootContext.AddGlobalAttributeSection (current_container, sect);
441 attrs = (Attributes) $1;
442 attrs.AddAttributeSection (sect);
450 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
452 string target = null;
455 target = (string) $2;
457 $$ = new AttributeSection (target, (ArrayList) $3);
459 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
461 $$ = new AttributeSection (null, (ArrayList) $2);
465 attribute_target_specifier
466 : attribute_target COLON
475 CheckAttributeTarget ((string) $1);
478 | EVENT { $$ = "event"; }
479 | RETURN { $$ = "return"; }
485 ArrayList attrs = new ArrayList ();
491 | attribute_list COMMA attribute
493 ArrayList attrs = (ArrayList) $1;
505 opt_attribute_arguments
507 $$ = new Attribute ((string) $1, (ArrayList) $3, (Location) $2);
512 : type_name { /* reserved attribute name or identifier: 17.4 */ }
515 opt_attribute_arguments
516 : /* empty */ { $$ = null; }
517 | OPEN_PARENS attribute_arguments CLOSE_PARENS
525 : opt_positional_argument_list
530 ArrayList args = new ArrayList ();
536 | positional_argument_list COMMA named_argument_list
538 ArrayList args = new ArrayList ();
544 | named_argument_list
546 ArrayList args = new ArrayList ();
555 opt_positional_argument_list
556 : /* empty */ { $$ = null; }
557 | positional_argument_list
560 positional_argument_list
563 ArrayList args = new ArrayList ();
564 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
568 | positional_argument_list COMMA expression
570 ArrayList args = (ArrayList) $1;
571 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
580 ArrayList args = new ArrayList ();
585 | named_argument_list COMMA named_argument
587 ArrayList args = (ArrayList) $1;
595 : IDENTIFIER ASSIGN expression
597 $$ = new DictionaryEntry (
599 new Argument ((Expression) $3, Argument.AType.Expression));
605 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
608 opt_class_member_declarations
610 | class_member_declarations
613 class_member_declarations
614 : class_member_declaration
615 | class_member_declarations
616 class_member_declaration
619 class_member_declaration
620 : constant_declaration // done
621 | field_declaration // done
622 | method_declaration // done
623 | property_declaration // done
624 | event_declaration // done
625 | indexer_declaration // done
626 | operator_declaration // done
627 | constructor_declaration // done
628 | destructor_declaration // done
638 string full_struct_name = MakeName ((string) $4);
640 new_struct = new Struct (current_container, full_struct_name, (int) $2,
641 (Attributes) $1, lexer.Location);
642 current_container = new_struct;
643 current_container.Namespace = current_namespace;
644 RootContext.Tree.RecordDecl (full_struct_name, new_struct);
650 Struct new_struct = (Struct) current_container;
653 new_struct.Bases = (ArrayList) $6;
655 current_container = current_container.Parent;
656 CheckDef (current_container.AddStruct (new_struct), new_struct.Name, new_struct.Location);
662 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
665 opt_struct_member_declarations
667 | struct_member_declarations
670 struct_member_declarations
671 : struct_member_declaration
672 | struct_member_declarations struct_member_declaration
675 struct_member_declaration
676 : constant_declaration
679 | property_declaration
681 | indexer_declaration
682 | operator_declaration
683 | constructor_declaration
687 * This is only included so we can flag error 575:
688 * destructors only allowed on class types
690 | destructor_declaration
701 foreach (VariableDeclaration constant in (ArrayList) $5){
702 Location l = constant.Location;
704 Const c = new Const (
705 (Expression) $4, (string) constant.identifier,
706 (Expression) constant.expression_or_array_initializer, (int) $2,
709 CheckDef (current_container.AddConstant (c), c.Name, l);
715 : constant_declarator
717 ArrayList constants = new ArrayList ();
721 | constant_declarators COMMA constant_declarator
723 ArrayList constants = (ArrayList) $1;
730 : IDENTIFIER ASSIGN constant_expression
732 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
743 Expression type = (Expression) $3;
746 foreach (VariableDeclaration var in (ArrayList) $4){
747 Location l = var.Location;
749 Field field = new Field (type, mod, var.identifier,
750 var.expression_or_array_initializer,
753 CheckDef (current_container.AddField (field), field.Name, l);
759 : variable_declarator
761 ArrayList decl = new ArrayList ();
765 | variable_declarators COMMA variable_declarator
767 ArrayList decls = (ArrayList) $1;
774 : IDENTIFIER ASSIGN variable_initializer
776 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
780 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
793 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
795 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
803 Method method = (Method) $1;
804 Block b = (Block) $2;
805 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
808 if ((method.ModFlags & extern_abstract) == 0){
810 501, lexer.Location, current_container.MakeName (method.Name) +
811 "must declare a body because it is not marked abstract or extern");
814 if ((method.ModFlags & Modifiers.EXTERN) != 0){
816 179, lexer.Location, current_container.MakeName (method.Name) +
817 " is declared extern, but has a body");
821 method.Block = (Block) $2;
822 CheckDef (current_container.AddMethod (method), method.Name, method.Location);
824 current_local_parameters = null;
838 1585, lexer.Location, "Member modifier `" +
839 Modifiers.Name (i) + "' must precede member type and name");
852 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
854 Method method = new Method ((Expression) $3, (int) $2, (string) $4,
855 (Parameters) $6, (Attributes) $1, lexer.Location);
857 current_local_parameters = (Parameters) $6;
865 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
867 Method method = new Method (TypeManager.system_void_expr, (int) $2, (string) $4,
868 (Parameters) $6, (Attributes) $1, lexer.Location);
870 current_local_parameters = (Parameters) $6;
877 | SEMICOLON { $$ = null; }
880 opt_formal_parameter_list
881 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
882 | formal_parameter_list
885 formal_parameter_list
888 ArrayList pars_list = (ArrayList) $1;
890 Parameter [] pars = new Parameter [pars_list.Count];
891 pars_list.CopyTo (pars);
893 $$ = new Parameters (pars, null, lexer.Location);
895 | fixed_parameters COMMA parameter_array
897 ArrayList pars_list = (ArrayList) $1;
899 Parameter [] pars = new Parameter [pars_list.Count];
900 pars_list.CopyTo (pars);
902 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
906 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
913 ArrayList pars = new ArrayList ();
918 | fixed_parameters COMMA fixed_parameter
920 ArrayList pars = (ArrayList) $1;
929 opt_parameter_modifier
933 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
937 opt_parameter_modifier
938 : /* empty */ { $$ = Parameter.Modifier.NONE; }
943 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
944 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
948 : opt_attributes PARAMS type IDENTIFIER
950 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
951 note ("type must be a single-dimension array type");
956 : qualified_identifier
965 implicit_value_parameter_type = (Expression) $3;
967 lexer.PropertyParsing = true;
971 accessor_declarations
973 lexer.PropertyParsing = false;
978 Pair pair = (Pair) $7;
979 Accessor get_block = (Accessor) pair.First;
980 Accessor set_block = (Accessor) pair.Second;
982 Location loc = (Location) $6;
983 prop = new Property ((Expression) $3, (string) $4, (int) $2, get_block, set_block,
984 (Attributes) $1, loc);
986 CheckDef (current_container.AddProperty (prop), prop.Name, loc);
987 implicit_value_parameter_type = null;
991 accessor_declarations
992 : get_accessor_declaration opt_set_accessor_declaration
994 $$ = new Pair ($1, $2);
996 | set_accessor_declaration opt_get_accessor_declaration
998 $$ = new Pair ($2, $1);
1002 opt_get_accessor_declaration
1003 : /* empty */ { $$ = null; }
1004 | get_accessor_declaration
1007 opt_set_accessor_declaration
1008 : /* empty */ { $$ = null; }
1009 | set_accessor_declaration
1012 get_accessor_declaration
1013 : opt_attributes GET
1015 // If this is not the case, then current_local_parameters has already
1016 // been set in indexer_declaration
1017 if (parsing_indexer == false)
1018 current_local_parameters = null;
1020 current_local_parameters = indexer_parameters;
1021 lexer.PropertyParsing = false;
1025 $$ = new Accessor ((Block) $4, (Attributes) $1);
1026 current_local_parameters = null;
1027 lexer.PropertyParsing = true;
1031 set_accessor_declaration
1032 : opt_attributes SET
1035 Parameter implicit_value_parameter = new Parameter (
1036 implicit_value_parameter_type, "value",
1037 Parameter.Modifier.NONE, null);
1039 if (parsing_indexer == false) {
1040 args = new Parameter [1];
1041 args [0] = implicit_value_parameter;
1042 current_local_parameters = new Parameters (args, null, lexer.Location);
1044 Parameter [] fpars = indexer_parameters.FixedParameters;
1047 int count = fpars.Length;
1049 args = new Parameter [count + 1];
1050 fpars.CopyTo (args, 0);
1051 args [count] = implicit_value_parameter;
1054 current_local_parameters = new Parameters (
1055 args, indexer_parameters.ArrayParameter, lexer.Location);
1058 lexer.PropertyParsing = false;
1062 $$ = new Accessor ((Block) $4, (Attributes) $1);
1063 current_local_parameters = null;
1064 lexer.PropertyParsing = true;
1070 | SEMICOLON { $$ = null; }
1073 interface_declaration
1076 INTERFACE IDENTIFIER
1078 Interface new_interface;
1079 string full_interface_name = MakeName ((string) $4);
1081 new_interface = new Interface (current_container, full_interface_name, (int) $2,
1082 (Attributes) $1, lexer.Location);
1083 if (current_interface != null) {
1084 Location l = lexer.Location;
1085 Report.Error (-2, l, "Internal compiler error: interface inside interface");
1087 current_interface = new_interface;
1088 new_interface.Namespace = current_namespace;
1089 RootContext.Tree.RecordDecl (full_interface_name, new_interface);
1092 interface_body opt_semicolon
1094 Interface new_interface = (Interface) current_interface;
1097 new_interface.Bases = (ArrayList) $6;
1099 current_interface = null;
1100 CheckDef (current_container.AddInterface (new_interface),
1101 new_interface.Name, new_interface.Location);
1106 : /* empty */ { $$ = null; }
1111 : COLON interface_type_list { $$ = $2; }
1117 ArrayList interfaces = new ArrayList ();
1119 interfaces.Add ($1);
1122 | interface_type_list COMMA interface_type
1124 ArrayList interfaces = (ArrayList) $1;
1125 interfaces.Add ($3);
1132 opt_interface_member_declarations
1136 opt_interface_member_declarations
1138 | interface_member_declarations
1141 interface_member_declarations
1142 : interface_member_declaration
1143 | interface_member_declarations interface_member_declaration
1146 interface_member_declaration
1147 : interface_method_declaration
1149 InterfaceMethod m = (InterfaceMethod) $1;
1151 CheckDef (current_interface.AddMethod (m), m.Name, m.Location);
1153 | interface_property_declaration
1155 InterfaceProperty p = (InterfaceProperty) $1;
1157 CheckDef (current_interface.AddProperty (p), p.Name, p.Location);
1159 | interface_event_declaration
1161 InterfaceEvent e = (InterfaceEvent) $1;
1163 CheckDef (current_interface.AddEvent (e), e.Name, lexer.Location);
1165 | interface_indexer_declaration
1167 InterfaceIndexer i = (InterfaceIndexer) $1;
1169 CheckDef (current_interface.AddIndexer (i), "indexer", i.Location);
1174 : /* empty */ { $$ = false; }
1175 | NEW { $$ = true; }
1178 interface_method_declaration
1179 : opt_attributes opt_new type IDENTIFIER
1180 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1183 $$ = new InterfaceMethod ((Expression) $3, (string) $4, (bool) $2,
1184 (Parameters) $6, (Attributes) $1, lexer.Location);
1186 | opt_attributes opt_new VOID IDENTIFIER
1187 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1190 $$ = new InterfaceMethod (
1191 TypeManager.system_void_expr, (string) $4, (bool) $2, (Parameters) $6,
1192 (Attributes) $1, lexer.Location);
1196 interface_property_declaration
1201 { lexer.PropertyParsing = true; }
1203 { lexer.PropertyParsing = false; }
1208 $$ = new InterfaceProperty ((Expression) $3, (string) $4, (bool) $2,
1209 (gs & 1) == 1, (gs & 2) == 2, (Attributes) $1,
1215 : opt_attributes GET SEMICOLON { $$ = 1; }
1216 | opt_attributes SET SEMICOLON { $$ = 2; }
1217 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1219 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1223 interface_event_declaration
1224 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1226 $$ = new InterfaceEvent ((Expression) $4, (string) $5, (bool) $2, (Attributes) $1,
1231 interface_indexer_declaration
1232 : opt_attributes opt_new type THIS
1233 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1235 { lexer.PropertyParsing = true; }
1237 { lexer.PropertyParsing = false; }
1240 int a_flags = (int) $10;
1242 bool do_get = (a_flags & 1) == 1;
1243 bool do_set = (a_flags & 2) == 2;
1245 $$ = new InterfaceIndexer ((Expression) $3, (Parameters) $6, do_get, do_set,
1246 (bool) $2, (Attributes) $1, lexer.Location);
1250 operator_declaration
1251 : opt_attributes opt_modifiers operator_declarator operator_body
1253 OperatorDeclaration decl = (OperatorDeclaration) $3;
1255 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
1256 decl.arg2type, decl.arg2name, (Block) $4, (Attributes) $1, decl.location);
1258 // Note again, checking is done in semantic analysis
1259 current_container.AddOperator (op);
1261 current_local_parameters = null;
1267 | SEMICOLON { $$ = null; }
1270 : type OPERATOR overloadable_operator
1271 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1273 Operator.OpType op = (Operator.OpType) $3;
1274 CheckUnaryOperator (op);
1276 if (op == Operator.OpType.Addition)
1277 op = Operator.OpType.UnaryPlus;
1279 if (op == Operator.OpType.Subtraction)
1280 op = Operator.OpType.UnaryNegation;
1282 Parameter [] pars = new Parameter [1];
1284 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1286 current_local_parameters = new Parameters (pars, null, lexer.Location);
1288 $$ = new OperatorDeclaration (op, (Expression) $1, (Expression) $5, (string) $6,
1289 null, null, lexer.Location);
1291 | type OPERATOR overloadable_operator
1293 type IDENTIFIER COMMA
1297 CheckBinaryOperator ((Operator.OpType) $3);
1299 Parameter [] pars = new Parameter [2];
1301 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1302 pars [1] = new Parameter ((Expression) $8, (string) $9, Parameter.Modifier.NONE, null);
1304 current_local_parameters = new Parameters (pars, null, lexer.Location);
1306 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1307 (Expression) $5, (string) $6,
1308 (Expression) $8, (string) $9, lexer.Location);
1310 | conversion_operator_declarator
1313 overloadable_operator
1315 : BANG { $$ = Operator.OpType.LogicalNot; }
1316 | TILDE { $$ = Operator.OpType.OnesComplement; }
1317 | OP_INC { $$ = Operator.OpType.Increment; }
1318 | OP_DEC { $$ = Operator.OpType.Decrement; }
1319 | TRUE { $$ = Operator.OpType.True; }
1320 | FALSE { $$ = Operator.OpType.False; }
1321 // Unary and binary:
1322 | PLUS { $$ = Operator.OpType.Addition; }
1323 | MINUS { $$ = Operator.OpType.Subtraction; }
1325 | STAR { $$ = Operator.OpType.Multiply; }
1326 | DIV { $$ = Operator.OpType.Division; }
1327 | PERCENT { $$ = Operator.OpType.Modulus; }
1328 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1329 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1330 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1331 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1332 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1333 | OP_EQ { $$ = Operator.OpType.Equality; }
1334 | OP_NE { $$ = Operator.OpType.Inequality; }
1335 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1336 | OP_LT { $$ = Operator.OpType.LessThan; }
1337 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1338 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1341 conversion_operator_declarator
1342 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1344 Parameter [] pars = new Parameter [1];
1346 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1348 current_local_parameters = new Parameters (pars, null, lexer.Location);
1350 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1351 null, null, lexer.Location);
1353 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1355 Parameter [] pars = new Parameter [1];
1357 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1359 current_local_parameters = new Parameters (pars, null, lexer.Location);
1361 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1362 null, null, lexer.Location);
1366 syntax_error (lexer.Location, "'operator' expected");
1370 syntax_error (lexer.Location, "'operator' expected");
1374 constructor_declaration
1377 constructor_declarator
1380 Constructor c = (Constructor) $3;
1381 c.Block = (Block) $4;
1382 c.OptAttributes = (Attributes) $1;
1383 c.ModFlags = (int) $2;
1385 if ((c.ModFlags & Modifiers.STATIC) != 0){
1386 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1388 515, c.Location, String.Format (
1389 "`{0}.{1}': static constructor can not have access modifiers",
1390 c.Name, current_container.Name));
1393 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, 0, c.Location);
1395 if (c.Initializer != null){
1398 "Static constructors can not have an explicit this or base " +
1399 "constructor invocations");
1402 if (!c.Parameters.Empty){
1404 132, c.Location, "Static constructors should not have parameters");
1407 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1410 CheckDef (current_container.AddConstructor (c), c.Name, c.Location);
1412 current_local_parameters = null;
1416 constructor_declarator
1418 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1420 oob_stack.Push (lexer.Location);
1422 current_local_parameters = (Parameters) $3;
1424 opt_constructor_initializer
1426 Location l = (Location) oob_stack.Pop ();
1427 $$ = new Constructor ((string) $1, (Parameters) $3, (ConstructorInitializer) $6, l);
1433 | SEMICOLON { $$ = null; }
1436 opt_constructor_initializer
1437 : /* empty */ { $$ = null; }
1438 | constructor_initializer
1441 constructor_initializer
1442 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1444 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1446 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1448 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1452 destructor_declaration
1453 : opt_attributes TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1455 if ((string) $3 != current_container.Basename){
1456 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1457 } else if (!(current_container is Class)){
1458 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1460 Location l = lexer.Location;
1463 if (!RootContext.StdLib && current_container.Name == "System.Object")
1464 m = Modifiers.PROTECTED | Modifiers.VIRTUAL;
1466 m = Modifiers.PROTECTED | Modifiers.OVERRIDE;
1468 Method d = new Method (
1469 TypeManager.system_void_expr, m, "Finalize",
1470 new Parameters (null, null, l), (Attributes) $1, l);
1472 d.Block = (Block) $6;
1473 CheckDef (current_container.AddMethod (d), d.Name, d.Location);
1481 EVENT type variable_declarators SEMICOLON
1483 foreach (VariableDeclaration var in (ArrayList) $5) {
1485 Event e = new Event ((Expression) $4, var.identifier,
1486 var.expression_or_array_initializer,
1487 (int) $2, null, null, (Attributes) $1, lexer.Location);
1489 CheckDef (current_container.AddEvent (e), e.Name, e.Location);
1495 EVENT type member_name
1498 implicit_value_parameter_type = (Expression) $4;
1499 lexer.EventParsing = true;
1500 oob_stack.Push (lexer.Location);
1502 event_accessor_declarations
1504 lexer.EventParsing = false;
1508 Location loc = (Location) oob_stack.Pop ();
1510 Pair pair = (Pair) $8;
1511 Accessor add_accessor = null;
1512 Accessor rem_accessor = null;
1514 if (pair.First != null)
1515 add_accessor = (Accessor) pair.First;
1516 if (pair.Second != null)
1517 rem_accessor = (Accessor) pair.Second;
1519 Event e = new Event ((Expression) $4, (string) $5, null, (int) $2, add_accessor, rem_accessor,
1520 (Attributes) $1, loc);
1522 CheckDef (current_container.AddEvent (e), e.Name, loc);
1523 implicit_value_parameter_type = null;
1527 event_accessor_declarations
1528 : add_accessor_declaration remove_accessor_declaration
1530 $$ = new Pair ($1, $2);
1532 | remove_accessor_declaration add_accessor_declaration
1534 $$ = new Pair ($2, $1);
1538 add_accessor_declaration
1539 : opt_attributes ADD
1541 Parameter [] args = new Parameter [1];
1542 Parameter implicit_value_parameter = new Parameter (
1543 implicit_value_parameter_type, "value",
1544 Parameter.Modifier.NONE, null);
1546 args [0] = implicit_value_parameter;
1548 current_local_parameters = new Parameters (args, null, lexer.Location);
1549 lexer.EventParsing = false;
1553 $$ = new Accessor ((Block) $4, (Attributes) $1);
1554 lexer.EventParsing = true;
1558 remove_accessor_declaration
1559 : opt_attributes REMOVE
1561 Parameter [] args = new Parameter [1];
1562 Parameter implicit_value_parameter = new Parameter (
1563 implicit_value_parameter_type, "value",
1564 Parameter.Modifier.NONE, null);
1566 args [0] = implicit_value_parameter;
1568 current_local_parameters = new Parameters (args, null, lexer.Location);
1569 lexer.EventParsing = false;
1573 $$ = new Accessor ((Block) $4, (Attributes) $1);
1574 lexer.EventParsing = true;
1579 : opt_attributes opt_modifiers indexer_declarator
1582 IndexerDeclaration decl = (IndexerDeclaration) $3;
1584 implicit_value_parameter_type = decl.type;
1586 lexer.PropertyParsing = true;
1587 parsing_indexer = true;
1589 indexer_parameters = decl.param_list;
1590 oob_stack.Push (lexer.Location);
1592 accessor_declarations
1594 lexer.PropertyParsing = false;
1595 parsing_indexer = false;
1599 // The signature is computed from the signature of the indexer. Look
1600 // at section 3.6 on the spec
1601 Location loc = (Location) oob_stack.Pop ();
1603 IndexerDeclaration decl = (IndexerDeclaration) $3;
1604 Pair pair = (Pair) $6;
1605 Accessor get_block = (Accessor) pair.First;
1606 Accessor set_block = (Accessor) pair.Second;
1608 indexer = new Indexer (decl.type, decl.interface_type, (int) $2, decl.param_list,
1609 get_block, set_block, (Attributes) $1, loc);
1611 // Note that there is no equivalent of CheckDef for this case
1612 // We shall handle this in semantic analysis
1614 current_container.AddIndexer (indexer);
1616 current_local_parameters = null;
1617 implicit_value_parameter_type = null;
1618 indexer_parameters = null;
1623 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1625 Parameters pars = (Parameters) $4;
1627 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1628 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1631 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
1633 | type qualified_identifier DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1635 Parameters pars = (Parameters) $6;
1637 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1638 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1640 $$ = new IndexerDeclaration ((Expression) $1, (string) $2, pars);
1652 Location enum_location = lexer.Location;
1654 string full_name = MakeName ((string) $4);
1655 Enum e = new Enum (current_container, (Expression) $5, (int) $2, full_name,
1656 (Attributes) $1, enum_location);
1658 foreach (VariableDeclaration ev in (ArrayList) $6) {
1659 Location loc = (Location) ev.Location;
1661 CheckDef (e.AddEnumMember (ev.identifier,
1662 (Expression) ev.expression_or_array_initializer,
1663 loc, ev.OptAttributes),
1664 ev.identifier, loc);
1667 e.Namespace = current_namespace;
1669 CheckDef (current_container.AddEnum (e), full_name, enum_location);
1670 RootContext.Tree.RecordDecl (full_name, e);
1676 : /* empty */ { $$ = TypeManager.system_int32_expr; }
1677 | COLON type { $$ = $2; }
1681 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1687 opt_enum_member_declarations
1688 : /* empty */ { $$ = new ArrayList (); }
1689 | enum_member_declarations opt_comma { $$ = $1; }
1692 enum_member_declarations
1693 : enum_member_declaration
1695 ArrayList l = new ArrayList ();
1700 | enum_member_declarations COMMA enum_member_declaration
1702 ArrayList l = (ArrayList) $1;
1710 enum_member_declaration
1711 : opt_attributes IDENTIFIER
1713 $$ = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
1715 | opt_attributes IDENTIFIER
1717 $$ = lexer.Location;
1721 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
1725 delegate_declaration
1729 IDENTIFIER OPEN_PARENS
1730 opt_formal_parameter_list
1734 Location l = lexer.Location;
1735 Delegate del = new Delegate (current_container, (Expression) $4, (int) $2,
1736 MakeName ((string) $5), (Parameters) $7,
1737 (Attributes) $1, l);
1739 del.Namespace = current_namespace;
1740 CheckDef (current_container.AddDelegate (del), del.Name, l);
1745 IDENTIFIER OPEN_PARENS
1746 opt_formal_parameter_list
1750 Location l = lexer.Location;
1751 Delegate del = new Delegate (
1753 TypeManager.system_void_expr, (int) $2, MakeName ((string) $5),
1754 (Parameters) $7, (Attributes) $1, l);
1756 del.Namespace = current_namespace;
1757 CheckDef (current_container.AddDelegate (del), del.Name, l);
1762 : namespace_or_type_name
1765 namespace_or_type_name
1766 : qualified_identifier
1770 * Before you think of adding a return_type, notice that we have been
1771 * using two rules in the places where it matters (one rule using type
1772 * and another identical one that uses VOID as the return type). This
1773 * gets rid of a shift/reduce couple
1776 : type_name { /* class_type */
1778 This does interfaces, delegates, struct_types, class_types,
1779 parent classes, and more! 4.2
1781 $$ = DecomposeQI ((string) $1, lexer.Location);
1793 // Note that here only unmanaged types are allowed but we
1794 // can't perform checks during this phase - we do it during
1795 // semantic analysis.
1797 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1801 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
1807 | non_expression_type rank_specifier
1809 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1811 | non_expression_type STAR
1813 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1815 | expression rank_specifiers
1817 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1821 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1825 // We need this because the parser will happily go and reduce IDENTIFIER STAR
1826 // through this different path
1828 | multiplicative_expression STAR
1830 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1837 ArrayList types = new ArrayList ();
1842 | type_list COMMA type
1844 ArrayList types = (ArrayList) $1;
1852 * replaces all the productions for isolating the various
1853 * simple types, but we need this to reuse it easily in local_variable_type
1856 : OBJECT { $$ = TypeManager.system_object_expr; }
1857 | STRING { $$ = TypeManager.system_string_expr; }
1858 | BOOL { $$ = TypeManager.system_boolean_expr; }
1859 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
1860 | FLOAT { $$ = TypeManager.system_single_expr; }
1861 | DOUBLE { $$ = TypeManager.system_double_expr; }
1866 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
1867 | BYTE { $$ = TypeManager.system_byte_expr; }
1868 | SHORT { $$ = TypeManager.system_int16_expr; }
1869 | USHORT { $$ = TypeManager.system_uint16_expr; }
1870 | INT { $$ = TypeManager.system_int32_expr; }
1871 | UINT { $$ = TypeManager.system_uint32_expr; }
1872 | LONG { $$ = TypeManager.system_int64_expr; }
1873 | ULONG { $$ = TypeManager.system_uint64_expr; }
1874 | CHAR { $$ = TypeManager.system_char_expr; }
1875 | VOID { $$ = TypeManager.system_void_expr; }
1883 : type rank_specifiers
1885 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1890 // Expressions, section 7.5
1898 | qualified_identifier
1900 string name = (string) $1;
1903 $$ = DecomposeQI (name, lexer.Location);
1905 | parenthesized_expression
1907 | invocation_expression
1911 | post_increment_expression
1912 | post_decrement_expression
1916 | checked_expression
1917 | unchecked_expression
1918 | pointer_member_access
1925 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
1926 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
1927 | NULL { $$ = NullLiteral.Null; }
1931 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
1932 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
1933 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
1938 object v = lexer.Value;
1941 $$ = new IntLiteral ((Int32) v);
1943 $$ = new UIntLiteral ((UInt32) v);
1945 $$ = new LongLiteral ((Int64) v);
1946 else if (v is ulong)
1947 $$ = new ULongLiteral ((UInt64) v);
1949 Console.WriteLine ("OOPS. Unexpected result from scanner");
1954 : TRUE { $$ = new BoolLiteral (true); }
1955 | FALSE { $$ = new BoolLiteral (false); }
1958 parenthesized_expression
1959 : OPEN_PARENS expression CLOSE_PARENS
1964 : primary_expression DOT IDENTIFIER
1966 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1968 | predefined_type DOT IDENTIFIER
1970 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1978 invocation_expression
1979 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
1982 Location l = lexer.Location;
1983 Report.Error (1, l, "Parse error");
1985 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
1990 : /* empty */ { $$ = null; }
1997 ArrayList list = new ArrayList ();
2001 | argument_list COMMA argument
2003 ArrayList list = (ArrayList) $1;
2012 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2014 | REF variable_reference
2016 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2018 | OUT variable_reference
2020 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2025 : expression { note ("section 5.4"); $$ = $1; }
2029 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2031 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2033 | primary_expression rank_specifiers
2035 // So the super-trick is that primary_expression
2036 // can only be either a SimpleName or a MemberAccess.
2037 // The MemberAccess case arises when you have a fully qualified type-name like :
2039 // SimpleName is when you have
2042 Expression expr = (Expression) $1;
2043 if (!(expr is SimpleName || expr is MemberAccess)) {
2044 Error_ExpectingTypeName (lexer.Location, expr);
2045 $$ = TypeManager.system_object_expr;
2048 // So we extract the string corresponding to the SimpleName
2051 $$ = new SimpleName (GetQualifiedIdentifier (expr) + (string) $2, lexer.Location);
2059 ArrayList list = new ArrayList ();
2063 | expression_list COMMA expression
2065 ArrayList list = (ArrayList) $1;
2074 $$ = new This (current_block, lexer.Location);
2079 : BASE DOT IDENTIFIER
2081 $$ = new BaseAccess ((string) $3, lexer.Location);
2083 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2085 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2089 post_increment_expression
2090 : primary_expression OP_INC
2092 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2093 (Expression) $1, lexer.Location);
2097 post_decrement_expression
2098 : primary_expression OP_DEC
2100 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2101 (Expression) $1, lexer.Location);
2106 : object_or_delegate_creation_expression
2107 | array_creation_expression
2110 object_or_delegate_creation_expression
2111 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2113 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2117 array_creation_expression
2118 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2120 opt_array_initializer
2122 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2124 | NEW type rank_specifiers array_initializer
2126 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2130 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2146 : rank_specifier opt_rank_specifier
2148 $$ = (string) $2 + (string) $1;
2153 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2155 $$ = "[" + (string) $2 + "]";
2175 | dim_separators COMMA
2177 $$ = (string) $1 + ",";
2181 opt_array_initializer
2193 : OPEN_BRACE CLOSE_BRACE
2195 ArrayList list = new ArrayList ();
2198 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2200 $$ = (ArrayList) $2;
2204 variable_initializer_list
2205 : variable_initializer
2207 ArrayList list = new ArrayList ();
2211 | variable_initializer_list COMMA variable_initializer
2213 ArrayList list = (ArrayList) $1;
2220 : TYPEOF OPEN_PARENS type CLOSE_PARENS
2222 $$ = new TypeOf ((Expression) $3, lexer.Location);
2227 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2228 $$ = new SizeOf ((Expression) $3, lexer.Location);
2233 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2235 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2239 unchecked_expression
2240 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2242 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2246 pointer_member_access
2247 : primary_expression OP_PTR IDENTIFIER
2251 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2252 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2256 : primary_expression
2257 | BANG prefixed_unary_expression
2259 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2261 | TILDE prefixed_unary_expression
2263 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2269 : OPEN_PARENS expression CLOSE_PARENS unary_expression
2271 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2273 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2275 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2280 // The idea to split this out is from Rhys' grammar
2281 // to solve the problem with casts.
2283 prefixed_unary_expression
2285 | PLUS prefixed_unary_expression
2287 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2289 | MINUS prefixed_unary_expression
2291 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2293 | OP_INC prefixed_unary_expression
2295 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2296 (Expression) $2, lexer.Location);
2298 | OP_DEC prefixed_unary_expression
2300 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2301 (Expression) $2, lexer.Location);
2303 | STAR prefixed_unary_expression
2305 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2307 | BITWISE_AND prefixed_unary_expression
2309 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2313 pre_increment_expression
2314 : OP_INC prefixed_unary_expression
2316 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2317 (Expression) $2, lexer.Location);
2321 pre_decrement_expression
2322 : OP_DEC prefixed_unary_expression
2324 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2325 (Expression) $2, lexer.Location);
2329 multiplicative_expression
2330 : prefixed_unary_expression
2331 | multiplicative_expression STAR prefixed_unary_expression
2333 $$ = new Binary (Binary.Operator.Multiply,
2334 (Expression) $1, (Expression) $3, lexer.Location);
2336 | multiplicative_expression DIV prefixed_unary_expression
2338 $$ = new Binary (Binary.Operator.Division,
2339 (Expression) $1, (Expression) $3, lexer.Location);
2341 | multiplicative_expression PERCENT prefixed_unary_expression
2343 $$ = new Binary (Binary.Operator.Modulus,
2344 (Expression) $1, (Expression) $3, lexer.Location);
2349 : multiplicative_expression
2350 | additive_expression PLUS multiplicative_expression
2352 $$ = new Binary (Binary.Operator.Addition,
2353 (Expression) $1, (Expression) $3, lexer.Location);
2355 | additive_expression MINUS multiplicative_expression
2357 $$ = new Binary (Binary.Operator.Subtraction,
2358 (Expression) $1, (Expression) $3, lexer.Location);
2363 : additive_expression
2364 | shift_expression OP_SHIFT_LEFT additive_expression
2366 $$ = new Binary (Binary.Operator.LeftShift,
2367 (Expression) $1, (Expression) $3, lexer.Location);
2369 | shift_expression OP_SHIFT_RIGHT additive_expression
2371 $$ = new Binary (Binary.Operator.RightShift,
2372 (Expression) $1, (Expression) $3, lexer.Location);
2376 relational_expression
2378 | relational_expression OP_LT shift_expression
2380 $$ = new Binary (Binary.Operator.LessThan,
2381 (Expression) $1, (Expression) $3, lexer.Location);
2383 | relational_expression OP_GT shift_expression
2385 $$ = new Binary (Binary.Operator.GreaterThan,
2386 (Expression) $1, (Expression) $3, lexer.Location);
2388 | relational_expression OP_LE shift_expression
2390 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2391 (Expression) $1, (Expression) $3, lexer.Location);
2393 | relational_expression OP_GE shift_expression
2395 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2396 (Expression) $1, (Expression) $3, lexer.Location);
2398 | relational_expression IS type
2400 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
2402 | relational_expression AS type
2404 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
2409 : relational_expression
2410 | equality_expression OP_EQ relational_expression
2412 $$ = new Binary (Binary.Operator.Equality,
2413 (Expression) $1, (Expression) $3, lexer.Location);
2415 | equality_expression OP_NE relational_expression
2417 $$ = new Binary (Binary.Operator.Inequality,
2418 (Expression) $1, (Expression) $3, lexer.Location);
2423 : equality_expression
2424 | and_expression BITWISE_AND equality_expression
2426 $$ = new Binary (Binary.Operator.BitwiseAnd,
2427 (Expression) $1, (Expression) $3, lexer.Location);
2431 exclusive_or_expression
2433 | exclusive_or_expression CARRET and_expression
2435 $$ = new Binary (Binary.Operator.ExclusiveOr,
2436 (Expression) $1, (Expression) $3, lexer.Location);
2440 inclusive_or_expression
2441 : exclusive_or_expression
2442 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2444 $$ = new Binary (Binary.Operator.BitwiseOr,
2445 (Expression) $1, (Expression) $3, lexer.Location);
2449 conditional_and_expression
2450 : inclusive_or_expression
2451 | conditional_and_expression OP_AND inclusive_or_expression
2453 $$ = new Binary (Binary.Operator.LogicalAnd,
2454 (Expression) $1, (Expression) $3, lexer.Location);
2458 conditional_or_expression
2459 : conditional_and_expression
2460 | conditional_or_expression OP_OR conditional_and_expression
2462 $$ = new Binary (Binary.Operator.LogicalOr,
2463 (Expression) $1, (Expression) $3, lexer.Location);
2467 conditional_expression
2468 : conditional_or_expression
2469 | conditional_or_expression INTERR expression COLON expression
2471 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2475 assignment_expression
2476 : prefixed_unary_expression ASSIGN expression
2478 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2480 | prefixed_unary_expression OP_MULT_ASSIGN expression
2482 Location l = lexer.Location;
2484 $$ = new CompoundAssign (
2485 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
2487 | prefixed_unary_expression OP_DIV_ASSIGN expression
2489 Location l = lexer.Location;
2491 $$ = new CompoundAssign (
2492 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
2494 | prefixed_unary_expression OP_MOD_ASSIGN expression
2496 Location l = lexer.Location;
2498 $$ = new CompoundAssign (
2499 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
2501 | prefixed_unary_expression OP_ADD_ASSIGN expression
2503 Location l = lexer.Location;
2505 $$ = new CompoundAssign (
2506 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
2508 | prefixed_unary_expression OP_SUB_ASSIGN expression
2510 Location l = lexer.Location;
2512 $$ = new CompoundAssign (
2513 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
2515 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2517 Location l = lexer.Location;
2519 $$ = new CompoundAssign (
2520 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
2522 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2524 Location l = lexer.Location;
2526 $$ = new CompoundAssign (
2527 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
2529 | prefixed_unary_expression OP_AND_ASSIGN expression
2531 Location l = lexer.Location;
2533 $$ = new CompoundAssign (
2534 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
2536 | prefixed_unary_expression OP_OR_ASSIGN expression
2538 Location l = lexer.Location;
2540 $$ = new CompoundAssign (
2541 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
2543 | prefixed_unary_expression OP_XOR_ASSIGN expression
2545 Location l = lexer.Location;
2547 $$ = new CompoundAssign (
2548 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
2553 : conditional_expression
2554 | assignment_expression
2576 name = MakeName ((string) $4);
2578 new_class = new Class (current_container, name, (int) $2,
2579 (Attributes) $1, lexer.Location);
2580 current_container = new_class;
2581 current_container.Namespace = current_namespace;
2582 RootContext.Tree.RecordDecl (name, new_class);
2588 Class new_class = (Class) current_container;
2591 new_class.Bases = (ArrayList) $6;
2593 current_container = current_container.Parent;
2594 CheckDef (current_container.AddClass (new_class), new_class.Name, new_class.Location);
2601 : /* empty */ { $$ = (int) 0; }
2607 | modifiers modifier
2612 if ((m1 & m2) != 0) {
2613 Location l = lexer.Location;
2614 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2616 $$ = (int) (m1 | m2);
2621 : NEW { $$ = Modifiers.NEW; }
2622 | PUBLIC { $$ = Modifiers.PUBLIC; }
2623 | PROTECTED { $$ = Modifiers.PROTECTED; }
2624 | INTERNAL { $$ = Modifiers.INTERNAL; }
2625 | PRIVATE { $$ = Modifiers.PRIVATE; }
2626 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2627 | SEALED { $$ = Modifiers.SEALED; }
2628 | STATIC { $$ = Modifiers.STATIC; }
2629 | READONLY { $$ = Modifiers.READONLY; }
2630 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2631 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2632 | EXTERN { $$ = Modifiers.EXTERN; }
2633 | VOLATILE { $$ = Modifiers.VOLATILE; }
2634 | UNSAFE { $$ = Modifiers.UNSAFE; }
2638 : /* empty */ { $$ = null; }
2639 | class_base { $$ = $1; }
2643 : COLON type_list { $$ = $2; }
2651 // A block is "contained" on the following places:
2653 // property_declaration as part of the accessor body (get/set)
2654 // operator_declaration
2655 // constructor_declaration
2656 // destructor_declaration
2657 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2662 current_block = new Block (current_block, current_local_parameters,
2663 lexer.Location, Location.Null);
2665 opt_statement_list CLOSE_BRACE
2667 while (current_block.Implicit)
2668 current_block = current_block.Parent;
2670 current_block.SetEndLocation (lexer.Location);
2671 current_block = current_block.Parent;
2682 | statement_list statement
2686 : declaration_statement
2688 if ($1 != null && (Block) $1 != current_block){
2689 current_block.AddStatement ((Statement) $1);
2690 current_block = (Block) $1;
2693 | embedded_statement
2695 Statement s = (Statement) $1;
2698 current_block.AddStatement ((Statement) $1);
2706 | expression_statement
2707 | selection_statement
2708 | iteration_statement
2712 | unchecked_statement
2722 $$ = new EmptyStatement ();
2729 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
2731 if (!current_block.AddLabel ((string) $1, labeled)){
2732 Location l = lexer.Location;
2733 Report.Error (140, l, "The label '" + ((string) $1) + "' is a duplicate");
2735 current_block.AddStatement (labeled);
2740 declaration_statement
2741 : local_variable_declaration SEMICOLON
2744 DictionaryEntry de = (DictionaryEntry) $1;
2746 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
2750 | local_constant_declaration SEMICOLON
2753 DictionaryEntry de = (DictionaryEntry) $1;
2755 $$ = declare_local_constant ((Expression) de.Key, (VariableDeclaration) de.Value);
2761 * The following is from Rhys' grammar:
2762 * > Types in local variable declarations must be recognized as
2763 * > expressions to prevent reduce/reduce errors in the grammar.
2764 * > The expressions are converted into types during semantic analysis.
2767 : primary_expression opt_rank_specifier
2769 // FIXME: Do something smart here regarding the composition of the type.
2771 // Ok, the above "primary_expression" is there to get rid of
2772 // both reduce/reduce and shift/reduces in the grammar, it should
2773 // really just be "type_name". If you use type_name, a reduce/reduce
2774 // creeps up. If you use qualified_identifier (which is all we need
2775 // really) two shift/reduces appear.
2778 // So the super-trick is that primary_expression
2779 // can only be either a SimpleName or a MemberAccess.
2780 // The MemberAccess case arises when you have a fully qualified type-name like :
2782 // SimpleName is when you have
2785 Expression expr = (Expression) $1;
2786 if (!(expr is SimpleName || expr is MemberAccess)) {
2787 Error_ExpectingTypeName (lexer.Location, expr);
2791 // So we extract the string corresponding to the SimpleName
2794 if ((string) $2 == "")
2797 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2800 | builtin_types opt_rank_specifier
2802 if ((string) $2 == "")
2805 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2809 local_variable_pointer_type
2810 : primary_expression STAR
2812 Expression expr = (Expression) $1;
2813 Location l = lexer.Location;
2815 if (!(expr is SimpleName || expr is MemberAccess)) {
2816 Error_ExpectingTypeName (l, expr);
2820 $$ = new ComposedCast ((Expression) $1, "*", l);
2822 | builtin_types STAR
2824 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
2828 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
2830 | local_variable_pointer_type STAR
2832 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2836 local_variable_declaration
2837 : local_variable_type variable_declarators
2840 $$ = new DictionaryEntry ($1, $2);
2844 | local_variable_pointer_type opt_rank_specifier variable_declarators
2849 if ((string) $2 == "")
2850 t = (Expression) $1;
2852 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2853 $$ = new DictionaryEntry (t, $3);
2859 local_constant_declaration
2860 : CONST local_variable_type constant_declarator
2863 $$ = new DictionaryEntry ($2, $3);
2869 expression_statement
2870 : statement_expression SEMICOLON
2877 // We have to do the wrapping here and not in the case above,
2878 // because statement_expression is used for example in for_statement
2880 statement_expression
2881 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2882 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2883 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2884 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2885 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2886 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2887 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2889 Report.Error (1002, lexer.Location, "Expecting `;'");
2893 object_creation_expression
2894 : object_or_delegate_creation_expression
2895 { note ("complain if this is a delegate maybe?"); }
2904 : if_statement_open if_statement_rest
2913 oob_stack.Push (lexer.Location);
2918 : boolean_expression CLOSE_PARENS
2921 Location l = (Location) oob_stack.Pop ();
2923 $$ = new If ((Expression) $1, (Statement) $3, l);
2925 if (RootContext.WarningLevel >= 3){
2926 if ($3 is EmptyStatement)
2927 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
2931 | boolean_expression CLOSE_PARENS
2932 embedded_statement ELSE embedded_statement
2934 Location l = (Location) oob_stack.Pop ();
2936 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
2941 : SWITCH OPEN_PARENS
2943 oob_stack.Push (lexer.Location);
2944 switch_stack.Push (current_block);
2946 expression CLOSE_PARENS
2949 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
2950 current_block = (Block) switch_stack.Pop ();
2966 Report.Error (1522, lexer.Location, "Empty switch block");
2974 ArrayList sections = new ArrayList ();
2979 | switch_sections switch_section
2981 ArrayList sections = (ArrayList) $1;
2991 current_block = new Block (current_block, lexer.Location, lexer.Location);
2995 Block topmost = current_block;
2997 while (topmost.Implicit)
2998 topmost = topmost.Parent;
2999 $$ = new SwitchSection ((ArrayList) $1, topmost);
3006 ArrayList labels = new ArrayList ();
3011 | switch_labels switch_label
3013 ArrayList labels = (ArrayList) ($1);
3021 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3022 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3025 1523, lexer.Location,
3026 "The keyword case or default must precede code in switch block");
3040 oob_stack.Push (lexer.Location);
3042 boolean_expression CLOSE_PARENS embedded_statement
3044 Location l = (Location) oob_stack.Pop ();
3045 $$ = new While ((Expression) $4, (Statement) $6, l);
3047 if (RootContext.WarningLevel >= 3){
3048 if ($6 is EmptyStatement)
3049 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3055 : DO embedded_statement
3057 oob_stack.Push (lexer.Location);
3059 boolean_expression CLOSE_PARENS SEMICOLON
3061 Location l = (Location) oob_stack.Pop ();
3063 $$ = new Do ((Statement) $2, (Expression) $6, l);
3069 opt_for_initializer SEMICOLON
3071 Block assign_block = new Block (current_block);
3072 current_block = assign_block;
3074 if ($3 is DictionaryEntry){
3075 DictionaryEntry de = (DictionaryEntry) $3;
3077 Expression type = (Expression) de.Key;
3078 ArrayList var_declarators = (ArrayList) de.Value;
3080 foreach (VariableDeclaration decl in var_declarators){
3084 vi = current_block.AddVariable (
3085 type, decl.identifier, current_local_parameters, decl.Location);
3089 Location l = lexer.Location;
3091 if (decl.expression_or_array_initializer is Expression){
3092 expr = (Expression) decl.expression_or_array_initializer;
3093 } else if (decl.expression_or_array_initializer == null) {
3096 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3097 expr = new ArrayCreation (type, "", init, decl.Location);
3100 LocalVariableReference var;
3101 var = new LocalVariableReference (assign_block, decl.identifier, l);
3104 Assign a = new Assign (var, expr, decl.Location);
3106 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3112 oob_stack.Push (lexer.Location);
3114 opt_for_condition SEMICOLON
3115 opt_for_iterator CLOSE_PARENS
3118 Location l = (Location) oob_stack.Pop ();
3120 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3122 if (RootContext.WarningLevel >= 3){
3123 if ($10 is EmptyStatement)
3124 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3127 current_block.AddStatement (f);
3128 while (current_block.Implicit)
3129 current_block = current_block.Parent;
3131 current_block = current_block.Parent;
3136 : /* empty */ { $$ = new EmptyStatement (); }
3141 : local_variable_declaration
3142 | statement_expression_list
3146 : /* empty */ { $$ = null; }
3147 | boolean_expression
3151 : /* empty */ { $$ = new EmptyStatement (); }
3156 : statement_expression_list
3159 statement_expression_list
3160 : statement_expression
3162 // CHANGE: was `null'
3163 Block b = new Block (current_block, true);
3165 b.AddStatement ((Statement) $1);
3168 | statement_expression_list COMMA statement_expression
3170 Block b = (Block) $1;
3172 b.AddStatement ((Statement) $3);
3178 : FOREACH OPEN_PARENS type IDENTIFIER IN
3180 oob_stack.Push (lexer.Location);
3182 expression CLOSE_PARENS
3184 oob_stack.Push (current_block);
3186 Block foreach_block = new Block (current_block, true);
3187 LocalVariableReference v = null;
3188 Location l = lexer.Location;
3191 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3195 // Get a writable reference to this read-only variable.
3196 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3198 current_block = foreach_block;
3201 oob_stack.Push (current_block);
3205 Block foreach_block = (Block) oob_stack.Pop ();
3206 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3207 Block prev_block = (Block) oob_stack.Pop ();
3208 Location l = (Location) oob_stack.Pop ();
3210 current_block = prev_block;
3213 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3214 foreach_block.AddStatement (f);
3223 | continue_statement
3232 $$ = new Break (lexer.Location);
3237 : CONTINUE SEMICOLON
3239 $$ = new Continue (lexer.Location);
3244 : GOTO IDENTIFIER SEMICOLON
3246 $$ = new Goto (current_block, (string) $2, lexer.Location);
3248 | GOTO CASE constant_expression SEMICOLON
3250 $$ = new GotoCase ((Expression) $3, lexer.Location);
3252 | GOTO DEFAULT SEMICOLON
3254 $$ = new GotoDefault (lexer.Location);
3259 : RETURN opt_expression SEMICOLON
3261 $$ = new Return ((Expression) $2, lexer.Location);
3266 : THROW opt_expression SEMICOLON
3268 $$ = new Throw ((Expression) $2, lexer.Location);
3278 : TRY block catch_clauses
3281 ArrayList s = new ArrayList ();
3283 foreach (Catch cc in (ArrayList) $3) {
3290 // Now s contains the list of specific catch clauses
3291 // and g contains the general one.
3293 $$ = new Try ((Block) $2, s, g, null, lexer.Location);
3295 | TRY block opt_catch_clauses FINALLY block
3298 ArrayList s = new ArrayList ();
3299 ArrayList catch_list = (ArrayList) $3;
3301 if (catch_list != null){
3302 foreach (Catch cc in catch_list) {
3310 $$ = new Try ((Block) $2, s, g, (Block) $5, lexer.Location);
3314 Report.Error (1524, lexer.Location, "Expected catch or finally");
3319 : /* empty */ { $$ = null; }
3326 ArrayList l = new ArrayList ();
3331 | catch_clauses catch_clause
3333 ArrayList l = (ArrayList) $1;
3341 : /* empty */ { $$ = null; }
3346 : CATCH opt_catch_args
3348 Expression type = null;
3352 DictionaryEntry cc = (DictionaryEntry) $2;
3353 type = (Expression) cc.Key;
3354 id = (string) cc.Value;
3357 ArrayList one = new ArrayList ();
3358 Location loc = lexer.Location;
3360 one.Add (new VariableDeclaration (id, null, loc));
3363 current_block = new Block (current_block);
3364 Block b = declare_local_variables (type, one, loc);
3369 Expression type = null;
3373 DictionaryEntry cc = (DictionaryEntry) $2;
3374 type = (Expression) cc.Key;
3375 id = (string) cc.Value;
3379 // FIXME: I can change this for an assignment.
3381 while (current_block != (Block) $1)
3382 current_block = current_block.Parent;
3387 $$ = new Catch (type, id , (Block) $4, lexer.Location);
3392 : /* empty */ { $$ = null; }
3397 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3399 $$ = new DictionaryEntry ($2, $3);
3406 $$ = new Checked ((Block) $2);
3413 $$ = new Unchecked ((Block) $2);
3420 if (!RootContext.Unsafe){
3421 Report.Error (227, lexer.Location,
3422 "Unsafe code can only be used if --unsafe is used");
3425 $$ = new Unsafe ((Block) $3);
3431 type fixed_pointer_declarators
3434 Block assign_block = new Block (current_block, true);
3435 ArrayList list = (ArrayList) $4;
3436 Expression type = (Expression) $3;
3437 Location l = lexer.Location;
3438 int top = list.Count;
3440 for (int i = 0; i < top; i++){
3441 Pair p = (Pair) list [i];
3444 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3451 current_block.AddStatement (assign_block);
3452 current_block = assign_block;
3453 oob_stack.Push (assign_block);
3458 Location l = (Location) oob_stack.Pop ();
3459 Block assign_block = (Block) oob_stack.Pop ();
3461 ArrayList list = (ArrayList) $4;
3462 int top = list.Count;
3464 $$ = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
3468 fixed_pointer_declarators
3469 : fixed_pointer_declarator {
3470 ArrayList declarators = new ArrayList ();
3471 declarators.Add ($1);
3474 | fixed_pointer_declarators COMMA fixed_pointer_declarator
3476 ArrayList declarators = (ArrayList) $1;
3477 declarators.Add ($3);
3482 fixed_pointer_declarator
3483 : IDENTIFIER ASSIGN expression
3485 $$ = new Pair ($1, $3);
3490 : LOCK OPEN_PARENS expression CLOSE_PARENS
3496 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
3501 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
3503 Block assign_block = new Block (current_block);
3504 current_block = assign_block;
3506 oob_stack.Push (lexer.Location);
3508 if ($3 is DictionaryEntry){
3509 DictionaryEntry de = (DictionaryEntry) $3;
3510 Location l = lexer.Location;
3512 Expression type = (Expression) de.Key;
3513 ArrayList var_declarators = (ArrayList) de.Value;
3515 ArrayList vars = new ArrayList ();
3517 foreach (VariableDeclaration decl in var_declarators){
3519 VariableInfo vi = current_block.AddVariable (
3520 type, decl.identifier,
3521 current_local_parameters, decl.Location);
3527 if (decl.expression_or_array_initializer is Expression){
3528 expr = (Expression) decl.expression_or_array_initializer;
3530 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3532 expr = new ArrayCreation (type, "", init, decl.Location);
3535 LocalVariableReference var;
3537 // Get a writable reference to this read-only variable.
3538 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
3540 // This is so that it is not a warning on using variables
3543 vars.Add (new DictionaryEntry (var, expr));
3545 // Assign a = new Assign (var, expr, decl.Location);
3546 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3548 $3 = new DictionaryEntry (type, vars);
3553 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
3554 current_block.AddStatement (u);
3555 while (current_block.Implicit)
3556 current_block = current_block.Parent;
3558 current_block = current_block.Parent;
3562 resource_acquisition
3563 : local_variable_declaration
3570 // A class used to pass around variable declarations and constants
3572 public class VariableDeclaration {
3573 public string identifier;
3574 public object expression_or_array_initializer;
3575 public Location Location;
3576 public Attributes OptAttributes;
3578 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
3580 this.identifier = id;
3581 this.expression_or_array_initializer = eoai;
3583 this.OptAttributes = opt_attrs;
3586 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
3592 // A class used to hold info about an indexer declarator
3595 public class IndexerDeclaration {
3596 public Expression type;
3597 public string interface_type;
3598 public Parameters param_list;
3600 public IndexerDeclaration (Expression type, string interface_type, Parameters param_list)
3603 this.interface_type = interface_type;
3604 this.param_list = param_list;
3609 // A class used to hold info about an operator declarator
3612 public class OperatorDeclaration {
3613 public Operator.OpType optype;
3614 public Expression ret_type, arg1type, arg2type;
3615 public string arg1name, arg2name;
3616 public Location location;
3618 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
3619 Expression arg1type, string arg1name,
3620 Expression arg2type, string arg2name, Location location)
3623 this.ret_type = ret_type;
3624 this.arg1type = arg1type;
3625 this.arg1name = arg1name;
3626 this.arg2type = arg2type;
3627 this.arg2name = arg2name;
3628 this.location = location;
3633 void Error_ExpectingTypeName (Location l, Expression expr)
3635 if (expr is Invocation){
3636 Report.Error (1002, l, "; expected");
3638 Report.Error (-1, l, "Invalid Type definition");
3643 // Given the @class_name name, it creates a fully qualified name
3644 // based on the containing declaration space
3647 MakeName (string class_name)
3649 string ns = current_namespace.Name;
3650 string container_name = current_container.Name;
3652 if (container_name == ""){
3654 return ns + "." + class_name;
3658 return container_name + "." + class_name;
3662 // Used to report back to the user the result of a declaration
3663 // in the current declaration space
3666 CheckDef (AdditionResult result, string name, Location l)
3668 if (result == AdditionResult.Success)
3672 case AdditionResult.NameExists:
3673 Report.Error (102, l, "The container `" + current_container.Name +
3674 "' already contains a definition for `"+
3680 // This is handled only for static Constructors, because
3681 // in reality we handle these by the semantic analysis later
3683 case AdditionResult.MethodExists:
3685 111, l, "Class `"+current_container.Name+
3686 "' already defines a member called '" +
3687 name + "' with the same parameter types (more than one default constructor)");
3690 case AdditionResult.EnclosingClash:
3691 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
3694 case AdditionResult.NotAConstructor:
3695 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
3701 CheckDef (bool result, string name, Location l)
3705 CheckDef (AdditionResult.NameExists, name, l);
3708 Expression DecomposeQI (string name, Location loc)
3712 if (name.IndexOf ('.') == -1){
3713 return new SimpleName (name, loc);
3715 int pos = name.LastIndexOf (".");
3716 string left = name.Substring (0, pos);
3717 string right = name.Substring (pos + 1);
3719 o = DecomposeQI (left, loc);
3721 return new MemberAccess (o, right, loc);
3726 // This method is used to get at the complete string representation of
3727 // a fully-qualified type name, hiding inside a MemberAccess ;-)
3728 // This is necessary because local_variable_type admits primary_expression
3729 // as the type of the variable. So we do some extra checking
3731 string GetQualifiedIdentifier (Expression expr)
3733 if (expr is SimpleName)
3734 return ((SimpleName)expr).Name;
3735 else if (expr is MemberAccess)
3736 return GetQualifiedIdentifier (((MemberAccess)expr).Expr) + "." + ((MemberAccess) expr).Identifier;
3738 throw new Exception ("Expr has to be either SimpleName or MemberAccess! (" + expr + ")");
3742 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
3744 Block implicit_block;
3745 ArrayList inits = null;
3748 // We use the `Used' property to check whether statements
3749 // have been added to the current block. If so, we need
3750 // to create another block to contain the new declaration
3751 // otherwise, as an optimization, we use the same block to
3752 // add the declaration.
3754 // FIXME: A further optimization is to check if the statements
3755 // that were added were added as part of the initialization
3756 // below. In which case, no other statements have been executed
3757 // and we might be able to reduce the number of blocks for
3758 // situations like this:
3760 // int j = 1; int k = j + 1;
3762 if (current_block.Used) {
3763 implicit_block = new Block (current_block, true, loc, Location.Null);
3764 implicit_block.AddChildVariableNames (current_block);
3766 implicit_block = current_block;
3768 foreach (VariableDeclaration decl in variable_declarators){
3770 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
3771 if (decl.expression_or_array_initializer != null){
3773 inits = new ArrayList ();
3780 return implicit_block;
3782 foreach (VariableDeclaration decl in inits){
3786 if (decl.expression_or_array_initializer is Expression){
3787 expr = (Expression) decl.expression_or_array_initializer;
3790 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3792 expr = new ArrayCreation (type, "", init, decl.Location);
3795 LocalVariableReference var;
3796 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
3798 assign = new Assign (var, expr, decl.Location);
3800 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
3803 return implicit_block;
3806 Block declare_local_constant (Expression type, VariableDeclaration decl)
3808 Block implicit_block;
3810 if (current_block.Used)
3811 implicit_block = new Block (current_block, true);
3813 implicit_block = current_block;
3815 if (!(implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
3816 current_local_parameters, decl.Location))){
3819 return implicit_block;
3822 void CheckAttributeTarget (string a)
3826 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
3830 Location l = lexer.Location;
3831 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
3837 void CheckUnaryOperator (Operator.OpType op)
3841 case Operator.OpType.LogicalNot:
3842 case Operator.OpType.OnesComplement:
3843 case Operator.OpType.Increment:
3844 case Operator.OpType.Decrement:
3845 case Operator.OpType.True:
3846 case Operator.OpType.False:
3847 case Operator.OpType.Addition:
3848 case Operator.OpType.Subtraction:
3853 Location l = lexer.Location;
3854 Report.Error (1019, l, "Overloadable unary operator expected");
3860 void CheckBinaryOperator (Operator.OpType op)
3864 case Operator.OpType.Addition:
3865 case Operator.OpType.Subtraction:
3866 case Operator.OpType.Multiply:
3867 case Operator.OpType.Division:
3868 case Operator.OpType.Modulus:
3869 case Operator.OpType.BitwiseAnd:
3870 case Operator.OpType.BitwiseOr:
3871 case Operator.OpType.ExclusiveOr:
3872 case Operator.OpType.LeftShift:
3873 case Operator.OpType.RightShift:
3874 case Operator.OpType.Equality:
3875 case Operator.OpType.Inequality:
3876 case Operator.OpType.GreaterThan:
3877 case Operator.OpType.LessThan:
3878 case Operator.OpType.GreaterThanOrEqual:
3879 case Operator.OpType.LessThanOrEqual:
3883 Location l = lexer.Location;
3884 Report.Error (1020, l, "Overloadable binary operator expected");
3890 void syntax_error (Location l, string msg)
3892 Report.Error (1003, l, "Syntax error, " + msg);
3895 void output (string s)
3897 Console.WriteLine (s);
3900 void note (string s)
3902 // Used to put annotations
3907 public Tokenizer Lexer {
3913 public CSharpParser (StreamReader reader, string name, ArrayList defines)
3915 current_namespace = new Namespace (null, "");
3917 current_container = RootContext.Tree.Types;
3918 current_container.Namespace = current_namespace;
3919 oob_stack = new Stack ();
3920 switch_stack = new Stack ();
3922 lexer = new Tokenizer (reader, name, defines);
3925 public override void parse ()
3928 if (yacc_verbose_flag)
3929 yyparse (lexer, new yydebug.yyDebugSimple ());
3932 } catch (Exception e){
3933 // Please do not remove this, it is used during debugging
3936 Report.Error (-25, lexer.Location, ": Parsing error ");
3937 Console.WriteLine (e);