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.AddGlobalAttribute (
315 current_container, asec, lexer.Location);
318 current_namespace = RootContext.Tree.RecordNamespace (current_namespace, name, (string) $3);
320 namespace_body opt_semicolon
322 current_namespace = current_namespace.Parent;
338 | qualified_identifier DOT IDENTIFIER {
339 $$ = (($1).ToString ()) + "." + ($3.ToString ()); }
344 : namespace_or_type_name
350 opt_namespace_member_declarations
361 opt_namespace_member_declarations
363 | namespace_member_declarations
366 namespace_member_declarations
367 : namespace_member_declaration
368 | namespace_member_declarations namespace_member_declaration
371 namespace_member_declaration
378 Class c = (Class) $1;
379 mod_flags = c.ModFlags;
381 } else if ($1 is Struct){
382 Struct s = (Struct) $1;
383 mod_flags = s.ModFlags;
388 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
390 1527, lexer.Location,
391 "Namespace elements cant be explicitly " +
392 "declared private or protected in `" + name + "'");
394 current_namespace.DeclarationFound = true;
396 | namespace_declaration
402 | interface_declaration
404 | delegate_declaration
406 // Enable this when we have handled all errors, because this acts as a generic fallback
409 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
419 | attribute_sections { $$ = $1; }
425 AttributeSection sect = (AttributeSection) $1;
427 if (sect.Target == "assembly")
428 RootContext.AddGlobalAttribute (current_container, sect, lexer.Location);
430 $$ = new Attributes ((AttributeSection) $1, lexer.Location);
432 | attribute_sections attribute_section
434 Attributes attrs = null;
435 AttributeSection sect = (AttributeSection) $2;
437 if (sect.Target == "assembly")
438 RootContext.AddGlobalAttribute (current_container, sect, lexer.Location);
441 attrs = (Attributes) $1;
442 attrs.AddAttribute (sect);
450 : OPEN_BRACKET attribute_target_specifier attribute_list CLOSE_BRACKET
452 string target = null;
455 target = (string) $2;
457 $$ = new AttributeSection (target, (ArrayList) $3);
459 | OPEN_BRACKET attribute_list 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;
1384 if ((c.ModFlags & Modifiers.STATIC) != 0){
1385 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, 0, c.Location);
1387 if ((c.ModFlags & Modifiers.Accessibility) != 0) {
1390 "Access modifiers are not allowed on static constructors");
1393 if (c.Initializer != null){
1396 "Static constructors can not have an explicit this or base " +
1397 "constructor invocations");
1400 if (!c.Parameters.Empty){
1402 132, c.Location, "Static constructors should not have parameters");
1405 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1408 CheckDef (current_container.AddConstructor (c), c.Name, c.Location);
1410 current_local_parameters = null;
1414 constructor_declarator
1416 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1418 oob_stack.Push (lexer.Location);
1420 current_local_parameters = (Parameters) $3;
1422 opt_constructor_initializer
1424 Location l = (Location) oob_stack.Pop ();
1425 $$ = new Constructor ((string) $1, (Parameters) $3, (ConstructorInitializer) $6, l);
1431 | SEMICOLON { $$ = null; }
1434 opt_constructor_initializer
1435 : /* empty */ { $$ = null; }
1436 | constructor_initializer
1439 constructor_initializer
1440 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1442 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1444 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1446 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1450 destructor_declaration
1451 : opt_attributes TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1453 if ((string) $3 != current_container.Basename){
1454 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1455 } else if (!(current_container is Class)){
1456 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1458 Location l = lexer.Location;
1461 if (!RootContext.StdLib && current_container.Name == "System.Object")
1462 m = Modifiers.PROTECTED | Modifiers.VIRTUAL;
1464 m = Modifiers.PROTECTED | Modifiers.OVERRIDE;
1466 Method d = new Method (
1467 TypeManager.system_void_expr, m, "Finalize",
1468 new Parameters (null, null, l), (Attributes) $1, l);
1470 d.Block = (Block) $6;
1471 CheckDef (current_container.AddMethod (d), d.Name, d.Location);
1479 EVENT type variable_declarators SEMICOLON
1481 foreach (VariableDeclaration var in (ArrayList) $5) {
1483 Event e = new Event ((Expression) $4, var.identifier,
1484 var.expression_or_array_initializer,
1485 (int) $2, null, null, (Attributes) $1, lexer.Location);
1487 CheckDef (current_container.AddEvent (e), e.Name, e.Location);
1493 EVENT type member_name
1496 implicit_value_parameter_type = (Expression) $4;
1497 lexer.EventParsing = true;
1498 oob_stack.Push (lexer.Location);
1500 event_accessor_declarations
1502 lexer.EventParsing = false;
1506 Location loc = (Location) oob_stack.Pop ();
1508 Pair pair = (Pair) $8;
1509 Accessor add_accessor = null;
1510 Accessor rem_accessor = null;
1512 if (pair.First != null)
1513 add_accessor = (Accessor) pair.First;
1514 if (pair.Second != null)
1515 rem_accessor = (Accessor) pair.Second;
1517 Event e = new Event ((Expression) $4, (string) $5, null, (int) $2, add_accessor, rem_accessor,
1518 (Attributes) $1, loc);
1520 CheckDef (current_container.AddEvent (e), e.Name, loc);
1521 implicit_value_parameter_type = null;
1525 event_accessor_declarations
1526 : add_accessor_declaration remove_accessor_declaration
1528 $$ = new Pair ($1, $2);
1530 | remove_accessor_declaration add_accessor_declaration
1532 $$ = new Pair ($2, $1);
1536 add_accessor_declaration
1537 : opt_attributes ADD
1539 Parameter [] args = new Parameter [1];
1540 Parameter implicit_value_parameter = new Parameter (
1541 implicit_value_parameter_type, "value",
1542 Parameter.Modifier.NONE, null);
1544 args [0] = implicit_value_parameter;
1546 current_local_parameters = new Parameters (args, null, lexer.Location);
1547 lexer.EventParsing = false;
1551 $$ = new Accessor ((Block) $4, (Attributes) $1);
1552 lexer.EventParsing = true;
1556 remove_accessor_declaration
1557 : opt_attributes REMOVE
1559 Parameter [] args = new Parameter [1];
1560 Parameter implicit_value_parameter = new Parameter (
1561 implicit_value_parameter_type, "value",
1562 Parameter.Modifier.NONE, null);
1564 args [0] = implicit_value_parameter;
1566 current_local_parameters = new Parameters (args, null, lexer.Location);
1567 lexer.EventParsing = false;
1571 $$ = new Accessor ((Block) $4, (Attributes) $1);
1572 lexer.EventParsing = true;
1577 : opt_attributes opt_modifiers indexer_declarator
1580 IndexerDeclaration decl = (IndexerDeclaration) $3;
1582 implicit_value_parameter_type = decl.type;
1584 lexer.PropertyParsing = true;
1585 parsing_indexer = true;
1587 indexer_parameters = decl.param_list;
1588 oob_stack.Push (lexer.Location);
1590 accessor_declarations
1592 lexer.PropertyParsing = false;
1593 parsing_indexer = false;
1597 // The signature is computed from the signature of the indexer. Look
1598 // at section 3.6 on the spec
1599 Location loc = (Location) oob_stack.Pop ();
1601 IndexerDeclaration decl = (IndexerDeclaration) $3;
1602 Pair pair = (Pair) $6;
1603 Accessor get_block = (Accessor) pair.First;
1604 Accessor set_block = (Accessor) pair.Second;
1606 indexer = new Indexer (decl.type, decl.interface_type, (int) $2, decl.param_list,
1607 get_block, set_block, (Attributes) $1, loc);
1609 // Note that there is no equivalent of CheckDef for this case
1610 // We shall handle this in semantic analysis
1612 current_container.AddIndexer (indexer);
1614 current_local_parameters = null;
1615 implicit_value_parameter_type = null;
1616 indexer_parameters = null;
1621 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1623 Parameters pars = (Parameters) $4;
1625 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1626 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1629 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
1631 | type qualified_identifier DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1633 Parameters pars = (Parameters) $6;
1635 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1636 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1638 $$ = new IndexerDeclaration ((Expression) $1, (string) $2, pars);
1650 Location enum_location = lexer.Location;
1652 string full_name = MakeName ((string) $4);
1653 Enum e = new Enum (current_container, (Expression) $5, (int) $2, full_name,
1654 (Attributes) $1, enum_location);
1656 foreach (VariableDeclaration ev in (ArrayList) $6) {
1657 Location loc = (Location) ev.Location;
1659 CheckDef (e.AddEnumMember (ev.identifier,
1660 (Expression) ev.expression_or_array_initializer,
1661 loc, ev.OptAttributes),
1662 ev.identifier, loc);
1665 e.Namespace = current_namespace;
1667 CheckDef (current_container.AddEnum (e), full_name, enum_location);
1668 RootContext.Tree.RecordDecl (full_name, e);
1674 : /* empty */ { $$ = TypeManager.system_int32_expr; }
1675 | COLON type { $$ = $2; }
1679 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1685 opt_enum_member_declarations
1686 : /* empty */ { $$ = new ArrayList (); }
1687 | enum_member_declarations opt_comma { $$ = $1; }
1690 enum_member_declarations
1691 : enum_member_declaration
1693 ArrayList l = new ArrayList ();
1698 | enum_member_declarations COMMA enum_member_declaration
1700 ArrayList l = (ArrayList) $1;
1708 enum_member_declaration
1709 : opt_attributes IDENTIFIER
1711 $$ = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
1713 | opt_attributes IDENTIFIER
1715 $$ = lexer.Location;
1719 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
1723 delegate_declaration
1727 IDENTIFIER OPEN_PARENS
1728 opt_formal_parameter_list
1732 Location l = lexer.Location;
1733 Delegate del = new Delegate (current_container, (Expression) $4, (int) $2,
1734 MakeName ((string) $5), (Parameters) $7,
1735 (Attributes) $1, l);
1737 del.Namespace = current_namespace;
1738 CheckDef (current_container.AddDelegate (del), del.Name, l);
1743 IDENTIFIER OPEN_PARENS
1744 opt_formal_parameter_list
1748 Location l = lexer.Location;
1749 Delegate del = new Delegate (
1751 TypeManager.system_void_expr, (int) $2, MakeName ((string) $5),
1752 (Parameters) $7, (Attributes) $1, l);
1754 del.Namespace = current_namespace;
1755 CheckDef (current_container.AddDelegate (del), del.Name, l);
1760 : namespace_or_type_name
1763 namespace_or_type_name
1764 : qualified_identifier
1768 * Before you think of adding a return_type, notice that we have been
1769 * using two rules in the places where it matters (one rule using type
1770 * and another identical one that uses VOID as the return type). This
1771 * gets rid of a shift/reduce couple
1774 : type_name { /* class_type */
1776 This does interfaces, delegates, struct_types, class_types,
1777 parent classes, and more! 4.2
1779 $$ = DecomposeQI ((string) $1, lexer.Location);
1791 // Note that here only unmanaged types are allowed but we
1792 // can't perform checks during this phase - we do it during
1793 // semantic analysis.
1795 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1799 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
1805 | non_expression_type rank_specifier
1807 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1809 | non_expression_type STAR
1811 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1813 | expression rank_specifiers
1815 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1819 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1826 ArrayList types = new ArrayList ();
1831 | type_list COMMA type
1833 ArrayList types = (ArrayList) $1;
1841 * replaces all the productions for isolating the various
1842 * simple types, but we need this to reuse it easily in local_variable_type
1845 : OBJECT { $$ = TypeManager.system_object_expr; }
1846 | STRING { $$ = TypeManager.system_string_expr; }
1847 | BOOL { $$ = TypeManager.system_boolean_expr; }
1848 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
1849 | FLOAT { $$ = TypeManager.system_single_expr; }
1850 | DOUBLE { $$ = TypeManager.system_double_expr; }
1855 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
1856 | BYTE { $$ = TypeManager.system_byte_expr; }
1857 | SHORT { $$ = TypeManager.system_int16_expr; }
1858 | USHORT { $$ = TypeManager.system_uint16_expr; }
1859 | INT { $$ = TypeManager.system_int32_expr; }
1860 | UINT { $$ = TypeManager.system_uint32_expr; }
1861 | LONG { $$ = TypeManager.system_int64_expr; }
1862 | ULONG { $$ = TypeManager.system_uint64_expr; }
1863 | CHAR { $$ = TypeManager.system_char_expr; }
1864 | VOID { $$ = TypeManager.system_void_expr; }
1872 : type rank_specifiers
1874 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1879 // Expressions, section 7.5
1887 | qualified_identifier
1889 string name = (string) $1;
1892 $$ = DecomposeQI (name, lexer.Location);
1894 | parenthesized_expression
1896 | invocation_expression
1900 | post_increment_expression
1901 | post_decrement_expression
1905 | checked_expression
1906 | unchecked_expression
1907 | pointer_member_access
1914 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
1915 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
1916 | NULL { $$ = NullLiteral.Null; }
1920 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
1921 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
1922 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
1927 object v = lexer.Value;
1930 $$ = new IntLiteral ((Int32) v);
1932 $$ = new UIntLiteral ((UInt32) v);
1934 $$ = new LongLiteral ((Int64) v);
1935 else if (v is ulong)
1936 $$ = new ULongLiteral ((UInt64) v);
1938 Console.WriteLine ("OOPS. Unexpected result from scanner");
1943 : TRUE { $$ = new BoolLiteral (true); }
1944 | FALSE { $$ = new BoolLiteral (false); }
1947 parenthesized_expression
1948 : OPEN_PARENS expression CLOSE_PARENS
1953 : primary_expression DOT IDENTIFIER
1955 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1957 | predefined_type DOT IDENTIFIER
1959 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1967 invocation_expression
1968 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
1971 Location l = lexer.Location;
1972 Report.Error (1, l, "Parse error");
1974 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
1979 : /* empty */ { $$ = null; }
1986 ArrayList list = new ArrayList ();
1990 | argument_list COMMA argument
1992 ArrayList list = (ArrayList) $1;
2001 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2003 | REF variable_reference
2005 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2007 | OUT variable_reference
2009 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2014 : expression { note ("section 5.4"); $$ = $1; }
2018 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2020 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2022 | primary_expression rank_specifiers
2024 // So the super-trick is that primary_expression
2025 // can only be either a SimpleName or a MemberAccess.
2026 // The MemberAccess case arises when you have a fully qualified type-name like :
2028 // SimpleName is when you have
2031 Expression expr = (Expression) $1;
2032 if (!(expr is SimpleName || expr is MemberAccess)) {
2033 Error_ExpectingTypeName (lexer.Location, expr);
2034 $$ = TypeManager.system_object_expr;
2037 // So we extract the string corresponding to the SimpleName
2040 $$ = new SimpleName (GetQualifiedIdentifier (expr) + (string) $2, lexer.Location);
2048 ArrayList list = new ArrayList ();
2052 | expression_list COMMA expression
2054 ArrayList list = (ArrayList) $1;
2063 $$ = new This (current_block, lexer.Location);
2068 : BASE DOT IDENTIFIER
2070 $$ = new BaseAccess ((string) $3, lexer.Location);
2072 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2074 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2078 post_increment_expression
2079 : primary_expression OP_INC
2081 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2082 (Expression) $1, lexer.Location);
2086 post_decrement_expression
2087 : primary_expression OP_DEC
2089 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2090 (Expression) $1, lexer.Location);
2095 : object_or_delegate_creation_expression
2096 | array_creation_expression
2099 object_or_delegate_creation_expression
2100 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2102 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2106 array_creation_expression
2107 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2109 opt_array_initializer
2111 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2113 | NEW type rank_specifiers array_initializer
2115 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2119 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2139 | rank_specifiers rank_specifier
2141 $$ = (string) $2 + (string) $1;
2146 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2148 $$ = "[" + (string) $2 + "]";
2168 | dim_separators COMMA
2170 $$ = (string) $1 + ",";
2174 opt_array_initializer
2186 : OPEN_BRACE CLOSE_BRACE
2188 ArrayList list = new ArrayList ();
2191 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2193 $$ = (ArrayList) $2;
2197 variable_initializer_list
2198 : variable_initializer
2200 ArrayList list = new ArrayList ();
2204 | variable_initializer_list COMMA variable_initializer
2206 ArrayList list = (ArrayList) $1;
2213 : TYPEOF OPEN_PARENS type CLOSE_PARENS
2215 $$ = new TypeOf ((Expression) $3, lexer.Location);
2220 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2221 $$ = new SizeOf ((Expression) $3, lexer.Location);
2226 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2228 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2232 unchecked_expression
2233 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2235 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2239 pointer_member_access
2240 : primary_expression OP_PTR IDENTIFIER
2244 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2245 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2249 : primary_expression
2250 | BANG prefixed_unary_expression
2252 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2254 | TILDE prefixed_unary_expression
2256 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2262 : OPEN_PARENS expression CLOSE_PARENS unary_expression
2264 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2266 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2268 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2273 // The idea to split this out is from Rhys' grammar
2274 // to solve the problem with casts.
2276 prefixed_unary_expression
2278 | PLUS prefixed_unary_expression
2280 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2282 | MINUS prefixed_unary_expression
2284 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2286 | OP_INC prefixed_unary_expression
2288 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2289 (Expression) $2, lexer.Location);
2291 | OP_DEC prefixed_unary_expression
2293 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2294 (Expression) $2, lexer.Location);
2296 | STAR prefixed_unary_expression
2298 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2300 | BITWISE_AND prefixed_unary_expression
2302 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2306 pre_increment_expression
2307 : OP_INC prefixed_unary_expression
2309 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2310 (Expression) $2, lexer.Location);
2314 pre_decrement_expression
2315 : OP_DEC prefixed_unary_expression
2317 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2318 (Expression) $2, lexer.Location);
2322 multiplicative_expression
2323 : prefixed_unary_expression
2324 | multiplicative_expression STAR prefixed_unary_expression
2326 $$ = new Binary (Binary.Operator.Multiply,
2327 (Expression) $1, (Expression) $3, lexer.Location);
2329 | multiplicative_expression DIV prefixed_unary_expression
2331 $$ = new Binary (Binary.Operator.Division,
2332 (Expression) $1, (Expression) $3, lexer.Location);
2334 | multiplicative_expression PERCENT prefixed_unary_expression
2336 $$ = new Binary (Binary.Operator.Modulus,
2337 (Expression) $1, (Expression) $3, lexer.Location);
2342 : multiplicative_expression
2343 | additive_expression PLUS multiplicative_expression
2345 $$ = new Binary (Binary.Operator.Addition,
2346 (Expression) $1, (Expression) $3, lexer.Location);
2348 | additive_expression MINUS multiplicative_expression
2350 $$ = new Binary (Binary.Operator.Subtraction,
2351 (Expression) $1, (Expression) $3, lexer.Location);
2356 : additive_expression
2357 | shift_expression OP_SHIFT_LEFT additive_expression
2359 $$ = new Binary (Binary.Operator.LeftShift,
2360 (Expression) $1, (Expression) $3, lexer.Location);
2362 | shift_expression OP_SHIFT_RIGHT additive_expression
2364 $$ = new Binary (Binary.Operator.RightShift,
2365 (Expression) $1, (Expression) $3, lexer.Location);
2369 relational_expression
2371 | relational_expression OP_LT shift_expression
2373 $$ = new Binary (Binary.Operator.LessThan,
2374 (Expression) $1, (Expression) $3, lexer.Location);
2376 | relational_expression OP_GT shift_expression
2378 $$ = new Binary (Binary.Operator.GreaterThan,
2379 (Expression) $1, (Expression) $3, lexer.Location);
2381 | relational_expression OP_LE shift_expression
2383 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2384 (Expression) $1, (Expression) $3, lexer.Location);
2386 | relational_expression OP_GE shift_expression
2388 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2389 (Expression) $1, (Expression) $3, lexer.Location);
2391 | relational_expression IS type
2393 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
2395 | relational_expression AS type
2397 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
2402 : relational_expression
2403 | equality_expression OP_EQ relational_expression
2405 $$ = new Binary (Binary.Operator.Equality,
2406 (Expression) $1, (Expression) $3, lexer.Location);
2408 | equality_expression OP_NE relational_expression
2410 $$ = new Binary (Binary.Operator.Inequality,
2411 (Expression) $1, (Expression) $3, lexer.Location);
2416 : equality_expression
2417 | and_expression BITWISE_AND equality_expression
2419 $$ = new Binary (Binary.Operator.BitwiseAnd,
2420 (Expression) $1, (Expression) $3, lexer.Location);
2424 exclusive_or_expression
2426 | exclusive_or_expression CARRET and_expression
2428 $$ = new Binary (Binary.Operator.ExclusiveOr,
2429 (Expression) $1, (Expression) $3, lexer.Location);
2433 inclusive_or_expression
2434 : exclusive_or_expression
2435 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2437 $$ = new Binary (Binary.Operator.BitwiseOr,
2438 (Expression) $1, (Expression) $3, lexer.Location);
2442 conditional_and_expression
2443 : inclusive_or_expression
2444 | conditional_and_expression OP_AND inclusive_or_expression
2446 $$ = new Binary (Binary.Operator.LogicalAnd,
2447 (Expression) $1, (Expression) $3, lexer.Location);
2451 conditional_or_expression
2452 : conditional_and_expression
2453 | conditional_or_expression OP_OR conditional_and_expression
2455 $$ = new Binary (Binary.Operator.LogicalOr,
2456 (Expression) $1, (Expression) $3, lexer.Location);
2460 conditional_expression
2461 : conditional_or_expression
2462 | conditional_or_expression INTERR expression COLON expression
2464 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2468 assignment_expression
2469 : prefixed_unary_expression ASSIGN expression
2471 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2473 | prefixed_unary_expression OP_MULT_ASSIGN expression
2475 Location l = lexer.Location;
2477 $$ = new CompoundAssign (
2478 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
2480 | prefixed_unary_expression OP_DIV_ASSIGN expression
2482 Location l = lexer.Location;
2484 $$ = new CompoundAssign (
2485 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
2487 | prefixed_unary_expression OP_MOD_ASSIGN expression
2489 Location l = lexer.Location;
2491 $$ = new CompoundAssign (
2492 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
2494 | prefixed_unary_expression OP_ADD_ASSIGN expression
2496 Location l = lexer.Location;
2498 $$ = new CompoundAssign (
2499 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
2501 | prefixed_unary_expression OP_SUB_ASSIGN expression
2503 Location l = lexer.Location;
2505 $$ = new CompoundAssign (
2506 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
2508 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2510 Location l = lexer.Location;
2512 $$ = new CompoundAssign (
2513 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
2515 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2517 Location l = lexer.Location;
2519 $$ = new CompoundAssign (
2520 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
2522 | prefixed_unary_expression OP_AND_ASSIGN expression
2524 Location l = lexer.Location;
2526 $$ = new CompoundAssign (
2527 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
2529 | prefixed_unary_expression OP_OR_ASSIGN expression
2531 Location l = lexer.Location;
2533 $$ = new CompoundAssign (
2534 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
2536 | prefixed_unary_expression OP_XOR_ASSIGN expression
2538 Location l = lexer.Location;
2540 $$ = new CompoundAssign (
2541 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
2546 : conditional_expression
2547 | assignment_expression
2569 name = MakeName ((string) $4);
2571 new_class = new Class (current_container, name, (int) $2,
2572 (Attributes) $1, lexer.Location);
2573 current_container = new_class;
2574 current_container.Namespace = current_namespace;
2575 RootContext.Tree.RecordDecl (name, new_class);
2581 Class new_class = (Class) current_container;
2584 new_class.Bases = (ArrayList) $6;
2586 current_container = current_container.Parent;
2587 CheckDef (current_container.AddClass (new_class), new_class.Name, new_class.Location);
2594 : /* empty */ { $$ = (int) 0; }
2600 | modifiers modifier
2605 if ((m1 & m2) != 0) {
2606 Location l = lexer.Location;
2607 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2609 $$ = (int) (m1 | m2);
2614 : NEW { $$ = Modifiers.NEW; }
2615 | PUBLIC { $$ = Modifiers.PUBLIC; }
2616 | PROTECTED { $$ = Modifiers.PROTECTED; }
2617 | INTERNAL { $$ = Modifiers.INTERNAL; }
2618 | PRIVATE { $$ = Modifiers.PRIVATE; }
2619 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2620 | SEALED { $$ = Modifiers.SEALED; }
2621 | STATIC { $$ = Modifiers.STATIC; }
2622 | READONLY { $$ = Modifiers.READONLY; }
2623 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2624 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2625 | EXTERN { $$ = Modifiers.EXTERN; }
2626 | VOLATILE { $$ = Modifiers.VOLATILE; }
2627 | UNSAFE { $$ = Modifiers.UNSAFE; }
2631 : /* empty */ { $$ = null; }
2632 | class_base { $$ = $1; }
2636 : COLON type_list { $$ = $2; }
2644 // A block is "contained" on the following places:
2646 // property_declaration as part of the accessor body (get/set)
2647 // operator_declaration
2648 // constructor_declaration
2649 // destructor_declaration
2650 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2655 current_block = new Block (current_block, current_local_parameters,
2656 lexer.Location, Location.Null);
2658 opt_statement_list CLOSE_BRACE
2660 while (current_block.Implicit)
2661 current_block = current_block.Parent;
2663 current_block.SetEndLocation (lexer.Location);
2664 current_block = current_block.Parent;
2675 | statement_list statement
2679 : declaration_statement
2681 if ($1 != null && (Block) $1 != current_block){
2682 current_block.AddStatement ((Statement) $1);
2683 current_block = (Block) $1;
2686 | embedded_statement
2688 Statement s = (Statement) $1;
2691 current_block.AddStatement ((Statement) $1);
2699 | expression_statement
2700 | selection_statement
2701 | iteration_statement
2705 | unchecked_statement
2715 $$ = new EmptyStatement ();
2722 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
2724 if (!current_block.AddLabel ((string) $1, labeled)){
2725 Location l = lexer.Location;
2726 Report.Error (140, l, "The label '" + ((string) $1) + "' is a duplicate");
2728 current_block.AddStatement (labeled);
2733 declaration_statement
2734 : local_variable_declaration SEMICOLON
2737 DictionaryEntry de = (DictionaryEntry) $1;
2739 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
2743 | local_constant_declaration SEMICOLON
2746 DictionaryEntry de = (DictionaryEntry) $1;
2748 $$ = declare_local_constant ((Expression) de.Key, (VariableDeclaration) de.Value);
2754 * The following is from Rhys' grammar:
2755 * > Types in local variable declarations must be recognized as
2756 * > expressions to prevent reduce/reduce errors in the grammar.
2757 * > The expressions are converted into types during semantic analysis.
2760 : primary_expression opt_rank_specifier
2762 // FIXME: Do something smart here regarding the composition of the type.
2764 // Ok, the above "primary_expression" is there to get rid of
2765 // both reduce/reduce and shift/reduces in the grammar, it should
2766 // really just be "type_name". If you use type_name, a reduce/reduce
2767 // creeps up. If you use qualified_identifier (which is all we need
2768 // really) two shift/reduces appear.
2771 // So the super-trick is that primary_expression
2772 // can only be either a SimpleName or a MemberAccess.
2773 // The MemberAccess case arises when you have a fully qualified type-name like :
2775 // SimpleName is when you have
2778 Expression expr = (Expression) $1;
2779 if (!(expr is SimpleName || expr is MemberAccess)) {
2780 Error_ExpectingTypeName (lexer.Location, expr);
2784 // So we extract the string corresponding to the SimpleName
2787 if ((string) $2 == "")
2790 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2793 | builtin_types opt_rank_specifier
2795 if ((string) $2 == "")
2798 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2802 local_variable_pointer_type
2803 : primary_expression STAR
2805 Expression expr = (Expression) $1;
2806 Location l = lexer.Location;
2808 if (!(expr is SimpleName || expr is MemberAccess)) {
2809 Error_ExpectingTypeName (l, expr);
2813 $$ = new ComposedCast ((Expression) $1, "*", l);
2815 | builtin_types STAR
2817 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
2821 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
2823 | local_variable_pointer_type STAR
2825 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2829 local_variable_declaration
2830 : local_variable_type variable_declarators
2833 $$ = new DictionaryEntry ($1, $2);
2837 | local_variable_pointer_type opt_rank_specifier variable_declarators
2842 if ((string) $2 == "")
2843 t = (Expression) $1;
2845 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2846 $$ = new DictionaryEntry (t, $3);
2852 local_constant_declaration
2853 : CONST local_variable_type constant_declarator
2856 $$ = new DictionaryEntry ($2, $3);
2862 expression_statement
2863 : statement_expression SEMICOLON
2870 // We have to do the wrapping here and not in the case above,
2871 // because statement_expression is used for example in for_statement
2873 statement_expression
2874 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2875 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2876 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2877 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2878 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2879 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2880 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2882 Report.Error (1002, lexer.Location, "Expecting `;'");
2886 object_creation_expression
2887 : object_or_delegate_creation_expression
2888 { note ("complain if this is a delegate maybe?"); }
2897 : if_statement_open if_statement_rest
2906 oob_stack.Push (lexer.Location);
2911 : boolean_expression CLOSE_PARENS
2914 Location l = (Location) oob_stack.Pop ();
2916 $$ = new If ((Expression) $1, (Statement) $3, l);
2918 if (RootContext.WarningLevel >= 3){
2919 if ($3 is EmptyStatement)
2920 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
2924 | boolean_expression CLOSE_PARENS
2925 embedded_statement ELSE embedded_statement
2927 Location l = (Location) oob_stack.Pop ();
2929 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
2934 : SWITCH OPEN_PARENS
2936 oob_stack.Push (lexer.Location);
2937 switch_stack.Push (current_block);
2939 expression CLOSE_PARENS
2942 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
2943 current_block = (Block) switch_stack.Pop ();
2959 Report.Error (1522, lexer.Location, "Empty switch block");
2967 ArrayList sections = new ArrayList ();
2972 | switch_sections switch_section
2974 ArrayList sections = (ArrayList) $1;
2984 current_block = new Block (current_block, lexer.Location, lexer.Location);
2988 while (current_block.Implicit)
2989 current_block = current_block.Parent;
2990 $$ = new SwitchSection ((ArrayList) $1, current_block);
2997 ArrayList labels = new ArrayList ();
3002 | switch_labels switch_label
3004 ArrayList labels = (ArrayList) ($1);
3012 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3013 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3016 1523, lexer.Location,
3017 "The keyword case or default must precede code in switch block");
3031 oob_stack.Push (lexer.Location);
3033 boolean_expression CLOSE_PARENS embedded_statement
3035 Location l = (Location) oob_stack.Pop ();
3036 $$ = new While ((Expression) $4, (Statement) $6, l);
3038 if (RootContext.WarningLevel >= 3){
3039 if ($6 is EmptyStatement)
3040 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3046 : DO embedded_statement
3047 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
3049 $$ = new Do ((Statement) $2, (Expression) $5, lexer.Location);
3055 opt_for_initializer SEMICOLON
3057 Block assign_block = new Block (current_block);
3058 current_block = assign_block;
3060 if ($3 is DictionaryEntry){
3061 DictionaryEntry de = (DictionaryEntry) $3;
3063 Expression type = (Expression) de.Key;
3064 ArrayList var_declarators = (ArrayList) de.Value;
3066 foreach (VariableDeclaration decl in var_declarators){
3070 vi = current_block.AddVariable (
3071 type, decl.identifier, current_local_parameters, decl.Location);
3075 Location l = lexer.Location;
3077 if (decl.expression_or_array_initializer is Expression){
3078 expr = (Expression) decl.expression_or_array_initializer;
3080 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3081 expr = new ArrayCreation (type, "", init, decl.Location);
3084 LocalVariableReference var;
3085 var = new LocalVariableReference (assign_block, decl.identifier, l);
3087 Assign a = new Assign (var, expr, decl.Location);
3089 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3094 oob_stack.Push (lexer.Location);
3096 opt_for_condition SEMICOLON
3097 opt_for_iterator CLOSE_PARENS
3100 Location l = (Location) oob_stack.Pop ();
3102 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3104 if (RootContext.WarningLevel >= 3){
3105 if ($10 is EmptyStatement)
3106 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3109 current_block.AddStatement (f);
3110 while (current_block.Implicit)
3111 current_block = current_block.Parent;
3113 current_block = current_block.Parent;
3118 : /* empty */ { $$ = new EmptyStatement (); }
3123 : local_variable_declaration
3124 | statement_expression_list
3128 : /* empty */ { $$ = null; }
3129 | boolean_expression
3133 : /* empty */ { $$ = new EmptyStatement (); }
3138 : statement_expression_list
3141 statement_expression_list
3142 : statement_expression
3144 // CHANGE: was `null'
3145 Block b = new Block (current_block, true);
3147 b.AddStatement ((Statement) $1);
3150 | statement_expression_list COMMA statement_expression
3152 Block b = (Block) $1;
3154 b.AddStatement ((Statement) $3);
3160 : FOREACH OPEN_PARENS type IDENTIFIER IN
3162 oob_stack.Push (lexer.Location);
3164 expression CLOSE_PARENS
3166 oob_stack.Push (current_block);
3168 Block foreach_block = new Block (current_block, true);
3169 LocalVariableReference v = null;
3170 Location l = lexer.Location;
3173 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3177 // Get a writable reference to this read-only variable.
3178 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3180 current_block = foreach_block;
3183 oob_stack.Push (current_block);
3187 Block foreach_block = (Block) oob_stack.Pop ();
3188 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3189 Block prev_block = (Block) oob_stack.Pop ();
3190 Location l = (Location) oob_stack.Pop ();
3192 current_block = prev_block;
3195 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3196 foreach_block.AddStatement (f);
3205 | continue_statement
3214 $$ = new Break (lexer.Location);
3219 : CONTINUE SEMICOLON
3221 $$ = new Continue (lexer.Location);
3226 : GOTO IDENTIFIER SEMICOLON
3228 $$ = new Goto (current_block, (string) $2, lexer.Location);
3230 | GOTO CASE constant_expression SEMICOLON
3232 $$ = new GotoCase ((Expression) $3, lexer.Location);
3234 | GOTO DEFAULT SEMICOLON
3236 $$ = new GotoDefault (lexer.Location);
3241 : RETURN opt_expression SEMICOLON
3243 $$ = new Return ((Expression) $2, lexer.Location);
3248 : THROW opt_expression SEMICOLON
3250 $$ = new Throw ((Expression) $2, lexer.Location);
3260 : TRY block catch_clauses
3263 ArrayList s = new ArrayList ();
3265 foreach (Catch cc in (ArrayList) $3) {
3272 // Now s contains the list of specific catch clauses
3273 // and g contains the general one.
3275 $$ = new Try ((Block) $2, s, g, null, lexer.Location);
3277 | TRY block opt_catch_clauses FINALLY block
3280 ArrayList s = new ArrayList ();
3281 ArrayList catch_list = (ArrayList) $3;
3283 if (catch_list != null){
3284 foreach (Catch cc in catch_list) {
3292 $$ = new Try ((Block) $2, s, g, (Block) $5, lexer.Location);
3296 Report.Error (1524, lexer.Location, "Expected catch or finally");
3301 : /* empty */ { $$ = null; }
3308 ArrayList l = new ArrayList ();
3313 | catch_clauses catch_clause
3315 ArrayList l = (ArrayList) $1;
3323 : /* empty */ { $$ = null; }
3328 : CATCH opt_catch_args
3330 Expression type = null;
3334 DictionaryEntry cc = (DictionaryEntry) $2;
3335 type = (Expression) cc.Key;
3336 id = (string) cc.Value;
3339 ArrayList one = new ArrayList ();
3340 Location loc = lexer.Location;
3342 one.Add (new VariableDeclaration (id, null, loc));
3345 current_block = new Block (current_block);
3346 Block b = declare_local_variables (type, one, loc);
3351 Expression type = null;
3355 DictionaryEntry cc = (DictionaryEntry) $2;
3356 type = (Expression) cc.Key;
3357 id = (string) cc.Value;
3361 // FIXME: I can change this for an assignment.
3363 while (current_block != (Block) $1)
3364 current_block = current_block.Parent;
3369 $$ = new Catch (type, id , (Block) $4, lexer.Location);
3374 : /* empty */ { $$ = null; }
3379 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3381 $$ = new DictionaryEntry ($2, $3);
3388 $$ = new Checked ((Block) $2);
3395 $$ = new Unchecked ((Block) $2);
3402 if (!RootContext.Unsafe){
3403 Report.Error (227, lexer.Location,
3404 "Unsafe code can only be used if --unsafe is used");
3407 $$ = new Unsafe ((Block) $3);
3413 pointer_type fixed_pointer_declarators
3416 Block assign_block = new Block (current_block, true);
3417 ArrayList list = (ArrayList) $4;
3418 Expression type = (Expression) $3;
3419 Location l = lexer.Location;
3420 int top = list.Count;
3422 for (int i = 0; i < top; i++){
3423 Pair p = (Pair) list [i];
3426 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3433 current_block.AddStatement (assign_block);
3434 current_block = assign_block;
3435 oob_stack.Push (assign_block);
3440 Location l = (Location) oob_stack.Pop ();
3441 Block assign_block = (Block) oob_stack.Pop ();
3443 ArrayList list = (ArrayList) $4;
3444 int top = list.Count;
3446 $$ = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
3450 fixed_pointer_declarators
3451 : fixed_pointer_declarator {
3452 ArrayList declarators = new ArrayList ();
3453 declarators.Add ($1);
3456 | fixed_pointer_declarators COMMA fixed_pointer_declarator
3458 ArrayList declarators = (ArrayList) $1;
3459 declarators.Add ($3);
3464 fixed_pointer_declarator
3465 : IDENTIFIER ASSIGN expression
3467 $$ = new Pair ($1, $3);
3472 : LOCK OPEN_PARENS expression CLOSE_PARENS
3478 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
3483 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
3485 Block assign_block = new Block (current_block);
3486 current_block = assign_block;
3488 oob_stack.Push (lexer.Location);
3490 if ($3 is DictionaryEntry){
3491 DictionaryEntry de = (DictionaryEntry) $3;
3492 Location l = lexer.Location;
3494 Expression type = (Expression) de.Key;
3495 ArrayList var_declarators = (ArrayList) de.Value;
3497 ArrayList vars = new ArrayList ();
3499 foreach (VariableDeclaration decl in var_declarators){
3501 VariableInfo vi = current_block.AddVariable (
3502 type, decl.identifier,
3503 current_local_parameters, decl.Location);
3509 if (decl.expression_or_array_initializer is Expression){
3510 expr = (Expression) decl.expression_or_array_initializer;
3512 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3514 expr = new ArrayCreation (type, "", init, decl.Location);
3517 LocalVariableReference var;
3519 // Get a writable reference to this read-only variable.
3520 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
3522 // This is so that it is not a warning on using variables
3525 vars.Add (new DictionaryEntry (var, expr));
3527 // Assign a = new Assign (var, expr, decl.Location);
3528 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3530 $3 = new DictionaryEntry (type, vars);
3535 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
3536 current_block.AddStatement (u);
3537 while (current_block.Implicit)
3538 current_block = current_block.Parent;
3540 current_block = current_block.Parent;
3544 resource_acquisition
3545 : local_variable_declaration
3552 // A class used to pass around variable declarations and constants
3554 public class VariableDeclaration {
3555 public string identifier;
3556 public object expression_or_array_initializer;
3557 public Location Location;
3558 public Attributes OptAttributes;
3560 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
3562 this.identifier = id;
3563 this.expression_or_array_initializer = eoai;
3565 this.OptAttributes = opt_attrs;
3568 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
3574 // A class used to hold info about an indexer declarator
3577 public class IndexerDeclaration {
3578 public Expression type;
3579 public string interface_type;
3580 public Parameters param_list;
3582 public IndexerDeclaration (Expression type, string interface_type, Parameters param_list)
3585 this.interface_type = interface_type;
3586 this.param_list = param_list;
3591 // A class used to hold info about an operator declarator
3594 public class OperatorDeclaration {
3595 public Operator.OpType optype;
3596 public Expression ret_type, arg1type, arg2type;
3597 public string arg1name, arg2name;
3598 public Location location;
3600 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
3601 Expression arg1type, string arg1name,
3602 Expression arg2type, string arg2name, Location location)
3605 this.ret_type = ret_type;
3606 this.arg1type = arg1type;
3607 this.arg1name = arg1name;
3608 this.arg2type = arg2type;
3609 this.arg2name = arg2name;
3610 this.location = location;
3615 void Error_ExpectingTypeName (Location l, Expression expr)
3617 if (expr is Invocation){
3618 Report.Error (1002, l, "; expected");
3620 Report.Error (-1, l, "Invalid Type definition");
3625 // Given the @class_name name, it creates a fully qualified name
3626 // based on the containing declaration space
3629 MakeName (string class_name)
3631 string ns = current_namespace.Name;
3632 string container_name = current_container.Name;
3634 if (container_name == ""){
3636 return ns + "." + class_name;
3640 return container_name + "." + class_name;
3644 // Used to report back to the user the result of a declaration
3645 // in the current declaration space
3648 CheckDef (AdditionResult result, string name, Location l)
3650 if (result == AdditionResult.Success)
3654 case AdditionResult.NameExists:
3655 Report.Error (102, l, "The container `" + current_container.Name +
3656 "' already contains a definition for `"+
3662 // This is handled only for static Constructors, because
3663 // in reality we handle these by the semantic analysis later
3665 case AdditionResult.MethodExists:
3667 111, l, "Class `"+current_container.Name+
3668 "' already defines a member called '" +
3669 name + "' with the same parameter types (more than one default constructor)");
3672 case AdditionResult.EnclosingClash:
3673 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
3676 case AdditionResult.NotAConstructor:
3677 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
3683 CheckDef (bool result, string name, Location l)
3687 CheckDef (AdditionResult.NameExists, name, l);
3690 Expression DecomposeQI (string name, Location loc)
3694 if (name.IndexOf ('.') == -1){
3695 return new SimpleName (name, loc);
3697 int pos = name.LastIndexOf (".");
3698 string left = name.Substring (0, pos);
3699 string right = name.Substring (pos + 1);
3701 o = DecomposeQI (left, loc);
3703 return new MemberAccess (o, right, loc);
3708 // This method is used to get at the complete string representation of
3709 // a fully-qualified type name, hiding inside a MemberAccess ;-)
3710 // This is necessary because local_variable_type admits primary_expression
3711 // as the type of the variable. So we do some extra checking
3713 string GetQualifiedIdentifier (Expression expr)
3715 if (expr is SimpleName)
3716 return ((SimpleName)expr).Name;
3717 else if (expr is MemberAccess)
3718 return GetQualifiedIdentifier (((MemberAccess)expr).Expr) + "." + ((MemberAccess) expr).Identifier;
3720 throw new Exception ("Expr has to be either SimpleName or MemberAccess! (" + expr + ")");
3724 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
3726 Block implicit_block;
3727 ArrayList inits = null;
3730 // We use the `Used' property to check whether statements
3731 // have been added to the current block. If so, we need
3732 // to create another block to contain the new declaration
3733 // otherwise, as an optimization, we use the same block to
3734 // add the declaration.
3736 // FIXME: A further optimization is to check if the statements
3737 // that were added were added as part of the initialization
3738 // below. In which case, no other statements have been executed
3739 // and we might be able to reduce the number of blocks for
3740 // situations like this:
3742 // int j = 1; int k = j + 1;
3744 if (current_block.Used) {
3745 implicit_block = new Block (current_block, true, loc, Location.Null);
3746 implicit_block.AddChildVariableNames (current_block);
3748 implicit_block = current_block;
3750 foreach (VariableDeclaration decl in variable_declarators){
3752 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
3753 if (decl.expression_or_array_initializer != null){
3755 inits = new ArrayList ();
3762 return implicit_block;
3764 foreach (VariableDeclaration decl in inits){
3768 if (decl.expression_or_array_initializer is Expression){
3769 expr = (Expression) decl.expression_or_array_initializer;
3772 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3774 expr = new ArrayCreation (type, "", init, decl.Location);
3777 LocalVariableReference var;
3778 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
3780 assign = new Assign (var, expr, decl.Location);
3782 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
3785 return implicit_block;
3788 Block declare_local_constant (Expression type, VariableDeclaration decl)
3790 Block implicit_block;
3792 if (current_block.Used)
3793 implicit_block = new Block (current_block, true);
3795 implicit_block = current_block;
3797 if (!(implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
3798 current_local_parameters, decl.Location))){
3801 return implicit_block;
3804 void CheckAttributeTarget (string a)
3808 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
3812 Location l = lexer.Location;
3813 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
3819 void CheckUnaryOperator (Operator.OpType op)
3823 case Operator.OpType.LogicalNot:
3824 case Operator.OpType.OnesComplement:
3825 case Operator.OpType.Increment:
3826 case Operator.OpType.Decrement:
3827 case Operator.OpType.True:
3828 case Operator.OpType.False:
3829 case Operator.OpType.Addition:
3830 case Operator.OpType.Subtraction:
3835 Location l = lexer.Location;
3836 Report.Error (1019, l, "Overloadable unary operator expected");
3842 void CheckBinaryOperator (Operator.OpType op)
3846 case Operator.OpType.Addition:
3847 case Operator.OpType.Subtraction:
3848 case Operator.OpType.Multiply:
3849 case Operator.OpType.Division:
3850 case Operator.OpType.Modulus:
3851 case Operator.OpType.BitwiseAnd:
3852 case Operator.OpType.BitwiseOr:
3853 case Operator.OpType.ExclusiveOr:
3854 case Operator.OpType.LeftShift:
3855 case Operator.OpType.RightShift:
3856 case Operator.OpType.Equality:
3857 case Operator.OpType.Inequality:
3858 case Operator.OpType.GreaterThan:
3859 case Operator.OpType.LessThan:
3860 case Operator.OpType.GreaterThanOrEqual:
3861 case Operator.OpType.LessThanOrEqual:
3865 Location l = lexer.Location;
3866 Report.Error (1020, l, "Overloadable binary operator expected");
3872 void syntax_error (Location l, string msg)
3874 Report.Error (1003, l, "Syntax error, " + msg);
3877 void output (string s)
3879 Console.WriteLine (s);
3882 void note (string s)
3884 // Used to put annotations
3889 public Tokenizer Lexer {
3895 public CSharpParser (StreamReader reader, string name, ArrayList defines)
3897 current_namespace = new Namespace (null, "");
3899 current_container = RootContext.Tree.Types;
3900 current_container.Namespace = current_namespace;
3901 oob_stack = new Stack ();
3902 switch_stack = new Stack ();
3904 lexer = new Tokenizer (reader, name, defines);
3907 public override void parse ()
3910 if (yacc_verbose_flag)
3911 yyparse (lexer, new yydebug.yyDebugSimple ());
3914 } catch (Exception e){
3915 // Please do not remove this, it is used during debugging
3918 Console.WriteLine (lexer.location + " : Parsing error ");
3919 Console.WriteLine (e);