4 // cs-parser.jay: The Parser for the C# compiler
6 // Authors: Miguel de Icaza (miguel@gnu.org)
7 // Ravi Pratap (ravi@ximian.com)
9 // Licensed under the terms of the GNU GPL
11 // (C) 2001 Ximian, Inc (http://www.ximian.com)
14 // (1) Figure out why error productions dont work. `type-declaration' is a
15 // great spot to put an `error' because you can reproduce it with this input:
18 // Possible optimization:
19 // Run memory profiler with parsing only, and consider dropping
20 // arraylists where not needed. Some pieces can use linked lists.
28 using System.Collections;
34 public class CSharpParser : GenericParser {
35 NamespaceEntry current_namespace;
36 TypeContainer current_container;
38 IIteratorContainer iterator_container;
41 // Current block is used to add statements as we find
48 // Current interface is used by the various declaration
49 // productions in the interface declaration to "add"
50 // the interfaces as we find them.
52 Interface current_interface;
55 // This is used by the unary_expression code to resolve
56 // a name against a parameter.
58 Parameters current_local_parameters;
61 // Using during property parsing to describe the implicit
62 // value parameter that is passed to the "set" and "get"accesor
63 // methods (properties and indexers).
65 Expression implicit_value_parameter_type;
66 Parameters indexer_parameters;
69 // Used to determine if we are parsing the get/set pair
70 // of an indexer or a property
75 // An out-of-band stack.
91 %token NONE /* This token is never returned by our lexer */
92 %token ERROR // This is used not by the parser, but by the tokenizer.
96 *These are the C# keywords
182 /* C# keywords which are not really keywords */
186 /* C# single character operators/punctuation. */
187 %token OPEN_BRACE "{"
188 %token CLOSE_BRACE "}"
189 %token OPEN_BRACKET "["
190 %token CLOSE_BRACKET "]"
191 %token OPEN_PARENS "("
192 %token CLOSE_PARENS ")"
205 %token BITWISE_AND "&"
206 %token BITWISE_OR "|"
213 /* C# multi-character operators. */
216 %token OP_SHIFT_LEFT "<<"
217 %token OP_SHIFT_RIGHT ">>"
224 %token OP_MULT_ASSIGN "*="
225 %token OP_DIV_ASSIGN "/="
226 %token OP_MOD_ASSIGN "%="
227 %token OP_ADD_ASSIGN "+="
228 %token OP_SUB_ASSIGN "-="
229 %token OP_SHIFT_LEFT_ASSIGN "<<="
230 %token OP_SHIFT_RIGHT_ASSIGN ">>="
231 %token OP_AND_ASSIGN "&="
232 %token OP_XOR_ASSIGN "^="
233 %token OP_OR_ASSIGN "|="
237 %token LITERAL_INTEGER "int literal"
238 %token LITERAL_FLOAT "float literal"
239 %token LITERAL_DOUBLE "double literal"
240 %token LITERAL_DECIMAL "decimal literal"
241 %token LITERAL_CHARACTER "character literal"
242 %token LITERAL_STRING "string literal"
246 /* Add precedence rules to solve dangling else s/r conflict */
255 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
257 %left STAR DIV PERCENT
258 %right BANG CARRET UMINUS
259 %nonassoc OP_INC OP_DEC
261 %left OPEN_BRACKET OPEN_BRACE
265 %start compilation_unit
269 : outer_declarations opt_EOF
270 | outer_declarations attribute_sections opt_EOF
271 | attribute_sections opt_EOF
272 | opt_EOF /* allow empty files */
282 | outer_declarations outer_declaration
287 | namespace_member_declaration
292 | using_directives using_directive
296 : using_alias_directive
297 | using_namespace_directive
300 using_alias_directive
301 : USING IDENTIFIER ASSIGN
302 namespace_or_type_name SEMICOLON
304 current_namespace.UsingAlias ((string) $2, (string) $4, lexer.Location);
308 using_namespace_directive
309 : USING namespace_name SEMICOLON
311 current_namespace.Using ((string) $2, lexer.Location);
316 // Strictly speaking, namespaces don't have attributes but
317 // we parse global attributes along with namespace declarations and then
320 namespace_declaration
321 : opt_attributes NAMESPACE qualified_identifier
323 Attributes attrs = (Attributes) $1;
326 foreach (AttributeSection asec in attrs.AttributeSections)
327 if (asec.Target == "assembly")
328 RootContext.AddGlobalAttributeSection (current_container, asec);
330 Report.Error(1518, Lexer.Location,
331 "Attributes cannot be applied to namespaces."
332 + " Expected class, delegate, enum, interface, or struct");
335 current_namespace = RootContext.Tree.RecordNamespace (current_namespace, file, (string) $3);
337 namespace_body opt_semicolon
339 current_namespace = current_namespace.Parent;
355 | qualified_identifier DOT IDENTIFIER {
356 $$ = (($1).ToString ()) + "." + ($3.ToString ()); }
361 : namespace_or_type_name
367 opt_namespace_member_declarations
378 opt_namespace_member_declarations
380 | namespace_member_declarations
383 namespace_member_declarations
384 : namespace_member_declaration
385 | namespace_member_declarations namespace_member_declaration
388 namespace_member_declaration
395 Class c = (Class) $1;
396 mod_flags = c.ModFlags;
398 } else if ($1 is Struct){
399 Struct s = (Struct) $1;
400 mod_flags = s.ModFlags;
405 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
407 1527, lexer.Location,
408 "Namespace elements cant be explicitly " +
409 "declared private or protected in `" + name + "'");
411 current_namespace.DeclarationFound = true;
413 | namespace_declaration {
414 current_namespace.DeclarationFound = true;
421 | interface_declaration
423 | delegate_declaration
425 // Enable this when we have handled all errors, because this acts as a generic fallback
428 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
438 | attribute_sections { $$ = $1; }
444 AttributeSection sect = (AttributeSection) $1;
446 if (sect.Target == "assembly")
447 RootContext.AddGlobalAttributeSection (current_container, sect);
450 $$ = new Attributes ((AttributeSection) $1);
452 | attribute_sections attribute_section
454 Attributes attrs = null;
455 AttributeSection sect = (AttributeSection) $2;
457 if (sect.Target == "assembly")
458 RootContext.AddGlobalAttributeSection (current_container, sect);
461 attrs = (Attributes) $1;
462 attrs.AddAttributeSection (sect);
470 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
472 string target = null;
475 target = (string) $2;
477 $$ = new AttributeSection (target, (ArrayList) $3);
479 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
481 $$ = new AttributeSection (null, (ArrayList) $2);
485 attribute_target_specifier
486 : attribute_target COLON
495 CheckAttributeTarget ((string) $1);
498 | EVENT { $$ = "event"; }
499 | RETURN { $$ = "return"; }
505 ArrayList attrs = new ArrayList ();
511 | attribute_list COMMA attribute
513 ArrayList attrs = (ArrayList) $1;
525 opt_attribute_arguments
527 $$ = new Attribute ((string) $1, (ArrayList) $3, (Location) $2);
532 : type_name { /* reserved attribute name or identifier: 17.4 */ }
535 opt_attribute_arguments
536 : /* empty */ { $$ = null; }
537 | OPEN_PARENS attribute_arguments CLOSE_PARENS
545 : opt_positional_argument_list
550 ArrayList args = new ArrayList ();
556 | positional_argument_list COMMA named_argument_list
558 ArrayList args = new ArrayList ();
564 | named_argument_list
566 ArrayList args = new ArrayList ();
575 opt_positional_argument_list
576 : /* empty */ { $$ = null; }
577 | positional_argument_list
580 positional_argument_list
583 ArrayList args = new ArrayList ();
584 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
588 | positional_argument_list COMMA expression
590 ArrayList args = (ArrayList) $1;
591 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
600 ArrayList args = new ArrayList ();
605 | named_argument_list COMMA named_argument
607 ArrayList args = (ArrayList) $1;
615 : IDENTIFIER ASSIGN expression
617 $$ = new DictionaryEntry (
619 new Argument ((Expression) $3, Argument.AType.Expression));
625 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
628 opt_class_member_declarations
630 | class_member_declarations
633 class_member_declarations
634 : class_member_declaration
635 | class_member_declarations
636 class_member_declaration
639 class_member_declaration
640 : constant_declaration // done
641 | field_declaration // done
642 | method_declaration // done
643 | property_declaration // done
644 | event_declaration // done
645 | indexer_declaration // done
646 | operator_declaration // done
647 | constructor_declaration // done
648 | destructor_declaration // done
658 string full_struct_name = MakeName ((string) $4);
660 new_struct = new Struct (current_container, full_struct_name, (int) $2,
661 (Attributes) $1, lexer.Location);
662 current_container = new_struct;
663 current_container.Namespace = current_namespace;
664 RootContext.Tree.RecordDecl (full_struct_name, new_struct);
670 Struct new_struct = (Struct) current_container;
673 new_struct.Bases = (ArrayList) $6;
675 current_container = current_container.Parent;
676 CheckDef (current_container.AddStruct (new_struct), new_struct.Name, new_struct.Location);
682 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
685 opt_struct_member_declarations
687 | struct_member_declarations
690 struct_member_declarations
691 : struct_member_declaration
692 | struct_member_declarations struct_member_declaration
695 struct_member_declaration
696 : constant_declaration
699 | property_declaration
701 | indexer_declaration
702 | operator_declaration
703 | constructor_declaration
707 * This is only included so we can flag error 575:
708 * destructors only allowed on class types
710 | destructor_declaration
721 foreach (VariableDeclaration constant in (ArrayList) $5){
722 Location l = constant.Location;
724 Const c = new Const (
725 (Expression) $4, (string) constant.identifier,
726 (Expression) constant.expression_or_array_initializer, (int) $2,
729 CheckDef (current_container.AddConstant (c), c.Name, l);
735 : constant_declarator
737 ArrayList constants = new ArrayList ();
741 | constant_declarators COMMA constant_declarator
743 ArrayList constants = (ArrayList) $1;
750 : IDENTIFIER ASSIGN constant_expression
752 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
763 Expression type = (Expression) $3;
766 foreach (VariableDeclaration var in (ArrayList) $4){
767 Location l = var.Location;
769 Field field = new Field (type, mod, var.identifier,
770 var.expression_or_array_initializer,
773 CheckDef (current_container.AddField (field), field.Name, l);
781 Report.Error (670, lexer.Location, "void type is not allowed for fields");
786 : variable_declarator
788 ArrayList decl = new ArrayList ();
792 | variable_declarators COMMA variable_declarator
794 ArrayList decls = (ArrayList) $1;
801 : IDENTIFIER ASSIGN variable_initializer
803 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
807 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
820 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
822 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
828 iterator_container = (IIteratorContainer) $1;
832 Method method = (Method) $1;
833 Block b = (Block) $3;
834 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
837 if ((method.ModFlags & extern_abstract) == 0){
839 501, lexer.Location, current_container.MakeName (method.Name) +
840 "must declare a body because it is not marked abstract or extern");
843 if ((method.ModFlags & Modifiers.EXTERN) != 0){
845 179, lexer.Location, current_container.MakeName (method.Name) +
846 " is declared extern, but has a body");
850 method.Block = (Block) $3;
851 CheckDef (current_container.AddMethod (method), method.Name, method.Location);
853 current_local_parameters = null;
854 iterator_container = null;
868 1585, lexer.Location, "Member modifier `" +
869 Modifiers.Name (i) + "' must precede member type and name");
882 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
884 Method method = new Method ((Expression) $3, (int) $2, (string) $4,
885 (Parameters) $6, (Attributes) $1, lexer.Location);
887 current_local_parameters = (Parameters) $6;
895 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
897 Method method = new Method (TypeManager.system_void_expr, (int) $2, (string) $4,
898 (Parameters) $6, (Attributes) $1, lexer.Location);
900 current_local_parameters = (Parameters) $6;
907 | SEMICOLON { $$ = null; }
910 opt_formal_parameter_list
911 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
912 | formal_parameter_list
915 formal_parameter_list
918 ArrayList pars_list = (ArrayList) $1;
920 Parameter [] pars = new Parameter [pars_list.Count];
921 pars_list.CopyTo (pars);
923 $$ = new Parameters (pars, null, lexer.Location);
925 | fixed_parameters COMMA parameter_array
927 ArrayList pars_list = (ArrayList) $1;
929 Parameter [] pars = new Parameter [pars_list.Count];
930 pars_list.CopyTo (pars);
932 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
936 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
943 ArrayList pars = new ArrayList ();
948 | fixed_parameters COMMA fixed_parameter
950 ArrayList pars = (ArrayList) $1;
959 opt_parameter_modifier
963 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
967 opt_parameter_modifier
968 : /* empty */ { $$ = Parameter.Modifier.NONE; }
973 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
974 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
978 : opt_attributes PARAMS type IDENTIFIER
980 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
981 note ("type must be a single-dimension array type");
986 : qualified_identifier
995 implicit_value_parameter_type = (Expression) $3;
997 lexer.PropertyParsing = true;
1001 accessor_declarations
1003 lexer.PropertyParsing = false;
1008 Pair pair = (Pair) $7;
1009 Accessor get_block = (Accessor) pair.First;
1010 Accessor set_block = (Accessor) pair.Second;
1012 Location loc = (Location) $6;
1013 prop = new Property ((Expression) $3, (string) $4, (int) $2, get_block, set_block,
1014 (Attributes) $1, loc);
1016 CheckDef (current_container.AddProperty (prop), prop.Name, loc);
1017 implicit_value_parameter_type = null;
1021 accessor_declarations
1022 : get_accessor_declaration opt_set_accessor_declaration
1024 $$ = new Pair ($1, $2);
1026 | set_accessor_declaration opt_get_accessor_declaration
1028 $$ = new Pair ($2, $1);
1032 opt_get_accessor_declaration
1033 : /* empty */ { $$ = null; }
1034 | get_accessor_declaration
1037 opt_set_accessor_declaration
1038 : /* empty */ { $$ = null; }
1039 | set_accessor_declaration
1042 get_accessor_declaration
1043 : opt_attributes GET
1045 // If this is not the case, then current_local_parameters has already
1046 // been set in indexer_declaration
1047 if (parsing_indexer == false)
1048 current_local_parameters = null;
1050 current_local_parameters = indexer_parameters;
1051 lexer.PropertyParsing = false;
1055 $$ = new Accessor ((Block) $4, (Attributes) $1);
1056 current_local_parameters = null;
1057 lexer.PropertyParsing = true;
1061 set_accessor_declaration
1062 : opt_attributes SET
1065 Parameter implicit_value_parameter = new Parameter (
1066 implicit_value_parameter_type, "value",
1067 Parameter.Modifier.NONE, null);
1069 if (parsing_indexer == false) {
1070 args = new Parameter [1];
1071 args [0] = implicit_value_parameter;
1072 current_local_parameters = new Parameters (args, null, lexer.Location);
1074 Parameter [] fpars = indexer_parameters.FixedParameters;
1077 int count = fpars.Length;
1079 args = new Parameter [count + 1];
1080 fpars.CopyTo (args, 0);
1081 args [count] = implicit_value_parameter;
1084 current_local_parameters = new Parameters (
1085 args, indexer_parameters.ArrayParameter, lexer.Location);
1088 lexer.PropertyParsing = false;
1092 $$ = new Accessor ((Block) $4, (Attributes) $1);
1093 current_local_parameters = null;
1094 lexer.PropertyParsing = true;
1100 | SEMICOLON { $$ = null; }
1103 interface_declaration
1106 INTERFACE IDENTIFIER
1108 Interface new_interface;
1109 string full_interface_name = MakeName ((string) $4);
1111 new_interface = new Interface (current_container, full_interface_name, (int) $2,
1112 (Attributes) $1, lexer.Location);
1113 if (current_interface != null) {
1114 Location l = lexer.Location;
1115 Report.Error (-2, l, "Internal compiler error: interface inside interface");
1117 current_interface = new_interface;
1118 new_interface.Namespace = current_namespace;
1119 RootContext.Tree.RecordDecl (full_interface_name, new_interface);
1122 interface_body opt_semicolon
1124 Interface new_interface = (Interface) current_interface;
1127 new_interface.Bases = (ArrayList) $6;
1129 current_interface = null;
1130 CheckDef (current_container.AddInterface (new_interface),
1131 new_interface.Name, new_interface.Location);
1136 : /* empty */ { $$ = null; }
1141 : COLON interface_type_list { $$ = $2; }
1147 ArrayList interfaces = new ArrayList ();
1149 interfaces.Add ($1);
1152 | interface_type_list COMMA interface_type
1154 ArrayList interfaces = (ArrayList) $1;
1155 interfaces.Add ($3);
1162 opt_interface_member_declarations
1166 opt_interface_member_declarations
1168 | interface_member_declarations
1171 interface_member_declarations
1172 : interface_member_declaration
1173 | interface_member_declarations interface_member_declaration
1176 interface_member_declaration
1177 : interface_method_declaration
1179 InterfaceMethod m = (InterfaceMethod) $1;
1181 CheckDef (current_interface.AddMethod (m), m.Name, m.Location);
1183 | interface_property_declaration
1185 InterfaceProperty p = (InterfaceProperty) $1;
1187 CheckDef (current_interface.AddProperty (p), p.Name, p.Location);
1189 | interface_event_declaration
1191 InterfaceEvent e = (InterfaceEvent) $1;
1193 CheckDef (current_interface.AddEvent (e), e.Name, lexer.Location);
1195 | interface_indexer_declaration
1197 InterfaceIndexer i = (InterfaceIndexer) $1;
1199 CheckDef (current_interface.AddIndexer (i), "indexer", i.Location);
1204 : /* empty */ { $$ = false; }
1205 | NEW { $$ = true; }
1208 interface_method_declaration
1209 : opt_attributes opt_new type IDENTIFIER
1210 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1213 $$ = new InterfaceMethod ((Expression) $3, (string) $4, (bool) $2,
1214 (Parameters) $6, (Attributes) $1, lexer.Location);
1216 | opt_attributes opt_new VOID IDENTIFIER
1217 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1220 $$ = new InterfaceMethod (
1221 TypeManager.system_void_expr, (string) $4, (bool) $2, (Parameters) $6,
1222 (Attributes) $1, lexer.Location);
1226 interface_property_declaration
1231 { lexer.PropertyParsing = true; }
1233 { lexer.PropertyParsing = false; }
1238 $$ = new InterfaceProperty ((Expression) $3, (string) $4, (bool) $2,
1239 (gs & 1) == 1, (gs & 2) == 2, (Attributes) $1,
1245 : opt_attributes GET SEMICOLON { $$ = 1; }
1246 | opt_attributes SET SEMICOLON { $$ = 2; }
1247 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1249 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1253 interface_event_declaration
1254 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1256 $$ = new InterfaceEvent ((Expression) $4, (string) $5, (bool) $2, (Attributes) $1,
1261 interface_indexer_declaration
1262 : opt_attributes opt_new type THIS
1263 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1265 { lexer.PropertyParsing = true; }
1267 { lexer.PropertyParsing = false; }
1270 int a_flags = (int) $10;
1272 bool do_get = (a_flags & 1) == 1;
1273 bool do_set = (a_flags & 2) == 2;
1275 $$ = new InterfaceIndexer ((Expression) $3, (Parameters) $6, do_get, do_set,
1276 (bool) $2, (Attributes) $1, lexer.Location);
1280 operator_declaration
1281 : opt_attributes opt_modifiers operator_declarator operator_body
1283 OperatorDeclaration decl = (OperatorDeclaration) $3;
1285 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
1286 decl.arg2type, decl.arg2name, (Block) $4, (Attributes) $1, decl.location);
1288 // Note again, checking is done in semantic analysis
1289 current_container.AddOperator (op);
1291 current_local_parameters = null;
1297 | SEMICOLON { $$ = null; }
1300 : type OPERATOR overloadable_operator
1301 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1303 Operator.OpType op = (Operator.OpType) $3;
1304 CheckUnaryOperator (op);
1306 if (op == Operator.OpType.Addition)
1307 op = Operator.OpType.UnaryPlus;
1309 if (op == Operator.OpType.Subtraction)
1310 op = Operator.OpType.UnaryNegation;
1312 Parameter [] pars = new Parameter [1];
1314 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1316 current_local_parameters = new Parameters (pars, null, lexer.Location);
1318 $$ = new OperatorDeclaration (op, (Expression) $1, (Expression) $5, (string) $6,
1319 null, null, lexer.Location);
1321 | type OPERATOR overloadable_operator
1323 type IDENTIFIER COMMA
1327 CheckBinaryOperator ((Operator.OpType) $3);
1329 Parameter [] pars = new Parameter [2];
1331 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1332 pars [1] = new Parameter ((Expression) $8, (string) $9, Parameter.Modifier.NONE, null);
1334 current_local_parameters = new Parameters (pars, null, lexer.Location);
1336 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1337 (Expression) $5, (string) $6,
1338 (Expression) $8, (string) $9, lexer.Location);
1340 | conversion_operator_declarator
1343 overloadable_operator
1345 : BANG { $$ = Operator.OpType.LogicalNot; }
1346 | TILDE { $$ = Operator.OpType.OnesComplement; }
1347 | OP_INC { $$ = Operator.OpType.Increment; }
1348 | OP_DEC { $$ = Operator.OpType.Decrement; }
1349 | TRUE { $$ = Operator.OpType.True; }
1350 | FALSE { $$ = Operator.OpType.False; }
1351 // Unary and binary:
1352 | PLUS { $$ = Operator.OpType.Addition; }
1353 | MINUS { $$ = Operator.OpType.Subtraction; }
1355 | STAR { $$ = Operator.OpType.Multiply; }
1356 | DIV { $$ = Operator.OpType.Division; }
1357 | PERCENT { $$ = Operator.OpType.Modulus; }
1358 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1359 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1360 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1361 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1362 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1363 | OP_EQ { $$ = Operator.OpType.Equality; }
1364 | OP_NE { $$ = Operator.OpType.Inequality; }
1365 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1366 | OP_LT { $$ = Operator.OpType.LessThan; }
1367 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1368 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1371 conversion_operator_declarator
1372 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1374 Parameter [] pars = new Parameter [1];
1376 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1378 current_local_parameters = new Parameters (pars, null, lexer.Location);
1380 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1381 null, null, lexer.Location);
1383 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1385 Parameter [] pars = new Parameter [1];
1387 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1389 current_local_parameters = new Parameters (pars, null, lexer.Location);
1391 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1392 null, null, lexer.Location);
1396 syntax_error (lexer.Location, "'operator' expected");
1400 syntax_error (lexer.Location, "'operator' expected");
1404 constructor_declaration
1407 constructor_declarator
1410 Constructor c = (Constructor) $3;
1411 c.Block = (Block) $4;
1412 c.OptAttributes = (Attributes) $1;
1413 c.ModFlags = (int) $2;
1415 if (c.Name == current_container.Basename){
1416 if ((c.ModFlags & Modifiers.STATIC) != 0){
1417 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1419 515, c.Location, String.Format (
1420 "`{0}.{1}': static constructor can not have access modifiers",
1421 c.Name, current_container.Name));
1424 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1426 if (c.Initializer != null){
1429 "Static constructors can not have an explicit this or base " +
1430 "constructor invocations");
1433 if (!c.Parameters.Empty){
1435 132, c.Location, "Static constructors should not have parameters");
1438 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1441 // We let another layer check the validity of the constructor.
1442 Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1445 CheckDef (current_container.AddConstructor (c), c.Name, c.Location);
1447 current_local_parameters = null;
1451 constructor_declarator
1453 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1455 oob_stack.Push (lexer.Location);
1457 current_local_parameters = (Parameters) $3;
1459 opt_constructor_initializer
1461 Location l = (Location) oob_stack.Pop ();
1462 $$ = new Constructor ((string) $1, (Parameters) $3, (ConstructorInitializer) $6, l);
1468 | SEMICOLON { $$ = null; }
1471 opt_constructor_initializer
1472 : /* empty */ { $$ = null; }
1473 | constructor_initializer
1476 constructor_initializer
1477 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1479 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1481 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1483 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1486 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1492 : /* EMPTY */ { $$ = 0; }
1493 | UNSAFE { $$ = Modifiers.UNSAFE; }
1494 | EXTERN { $$ = Modifiers.EXTERN; }
1497 destructor_declaration
1498 : opt_attributes opt_finalizer TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1500 if ((string) $4 != current_container.Basename){
1501 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1502 } else if (!(current_container is Class)){
1503 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1505 Location l = lexer.Location;
1508 if (!RootContext.StdLib && current_container.Name == "System.Object")
1509 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
1511 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
1513 if ((m & Modifiers.UNSAFE) != 0){
1514 if (!RootContext.Unsafe){
1515 Report.Error (227, l,
1516 "Unsafe code requires the --unsafe command " +
1517 "line option to be specified");
1521 Method d = new Method (
1522 TypeManager.system_void_expr, m, "Finalize",
1523 new Parameters (null, null, l), (Attributes) $1, l);
1525 d.Block = (Block) $7;
1526 CheckDef (current_container.AddMethod (d), d.Name, d.Location);
1534 EVENT type variable_declarators SEMICOLON
1536 foreach (VariableDeclaration var in (ArrayList) $5) {
1538 Event e = new Event ((Expression) $4, var.identifier,
1539 var.expression_or_array_initializer,
1540 (int) $2, null, null, (Attributes) $1, lexer.Location);
1542 CheckDef (current_container.AddEvent (e), e.Name, e.Location);
1548 EVENT type member_name
1551 implicit_value_parameter_type = (Expression) $4;
1552 lexer.EventParsing = true;
1553 oob_stack.Push (lexer.Location);
1555 event_accessor_declarations
1557 lexer.EventParsing = false;
1561 Location loc = (Location) oob_stack.Pop ();
1563 Pair pair = (Pair) $8;
1564 Accessor add_accessor = null;
1565 Accessor rem_accessor = null;
1567 if (pair.First != null)
1568 add_accessor = (Accessor) pair.First;
1569 if (pair.Second != null)
1570 rem_accessor = (Accessor) pair.Second;
1572 Event e = new Event ((Expression) $4, (string) $5, null, (int) $2, add_accessor, rem_accessor,
1573 (Attributes) $1, loc);
1575 CheckDef (current_container.AddEvent (e), e.Name, loc);
1576 implicit_value_parameter_type = null;
1580 event_accessor_declarations
1581 : add_accessor_declaration remove_accessor_declaration
1583 $$ = new Pair ($1, $2);
1585 | remove_accessor_declaration add_accessor_declaration
1587 $$ = new Pair ($2, $1);
1591 add_accessor_declaration
1592 : opt_attributes ADD
1594 Parameter [] args = new Parameter [1];
1595 Parameter implicit_value_parameter = new Parameter (
1596 implicit_value_parameter_type, "value",
1597 Parameter.Modifier.NONE, null);
1599 args [0] = implicit_value_parameter;
1601 current_local_parameters = new Parameters (args, null, lexer.Location);
1602 lexer.EventParsing = false;
1606 $$ = new Accessor ((Block) $4, (Attributes) $1);
1607 lexer.EventParsing = true;
1609 | opt_attributes ADD error {
1610 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1615 remove_accessor_declaration
1616 : opt_attributes REMOVE
1618 Parameter [] args = new Parameter [1];
1619 Parameter implicit_value_parameter = new Parameter (
1620 implicit_value_parameter_type, "value",
1621 Parameter.Modifier.NONE, null);
1623 args [0] = implicit_value_parameter;
1625 current_local_parameters = new Parameters (args, null, lexer.Location);
1626 lexer.EventParsing = false;
1630 $$ = new Accessor ((Block) $4, (Attributes) $1);
1631 lexer.EventParsing = true;
1633 | opt_attributes REMOVE error {
1634 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1640 : opt_attributes opt_modifiers indexer_declarator
1643 IndexerDeclaration decl = (IndexerDeclaration) $3;
1645 implicit_value_parameter_type = decl.type;
1647 lexer.PropertyParsing = true;
1648 parsing_indexer = true;
1650 indexer_parameters = decl.param_list;
1651 oob_stack.Push (lexer.Location);
1653 accessor_declarations
1655 lexer.PropertyParsing = false;
1656 parsing_indexer = false;
1660 // The signature is computed from the signature of the indexer. Look
1661 // at section 3.6 on the spec
1662 Location loc = (Location) oob_stack.Pop ();
1664 IndexerDeclaration decl = (IndexerDeclaration) $3;
1665 Pair pair = (Pair) $6;
1666 Accessor get_block = (Accessor) pair.First;
1667 Accessor set_block = (Accessor) pair.Second;
1669 indexer = new Indexer (decl.type, decl.interface_type, (int) $2, decl.param_list,
1670 get_block, set_block, (Attributes) $1, loc);
1672 // Note that there is no equivalent of CheckDef for this case
1673 // We shall handle this in semantic analysis
1675 current_container.AddIndexer (indexer);
1677 current_local_parameters = null;
1678 implicit_value_parameter_type = null;
1679 indexer_parameters = null;
1684 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1686 Parameters pars = (Parameters) $4;
1688 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1689 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1692 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
1694 | type qualified_identifier DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1696 Parameters pars = (Parameters) $6;
1698 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1699 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1701 $$ = new IndexerDeclaration ((Expression) $1, (string) $2, pars);
1713 Location enum_location = lexer.Location;
1715 string full_name = MakeName ((string) $4);
1716 Enum e = new Enum (current_container, (Expression) $5, (int) $2, full_name,
1717 (Attributes) $1, enum_location);
1719 foreach (VariableDeclaration ev in (ArrayList) $6) {
1720 Location loc = (Location) ev.Location;
1722 CheckDef (e.AddEnumMember (ev.identifier,
1723 (Expression) ev.expression_or_array_initializer,
1724 loc, ev.OptAttributes),
1725 ev.identifier, loc);
1728 e.Namespace = current_namespace;
1730 CheckDef (current_container.AddEnum (e), full_name, enum_location);
1731 RootContext.Tree.RecordDecl (full_name, e);
1737 : /* empty */ { $$ = TypeManager.system_int32_expr; }
1738 | COLON type { $$ = $2; }
1742 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1748 opt_enum_member_declarations
1749 : /* empty */ { $$ = new ArrayList (); }
1750 | enum_member_declarations opt_comma { $$ = $1; }
1753 enum_member_declarations
1754 : enum_member_declaration
1756 ArrayList l = new ArrayList ();
1761 | enum_member_declarations COMMA enum_member_declaration
1763 ArrayList l = (ArrayList) $1;
1771 enum_member_declaration
1772 : opt_attributes IDENTIFIER
1774 $$ = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
1776 | opt_attributes IDENTIFIER
1778 $$ = lexer.Location;
1782 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
1786 delegate_declaration
1790 IDENTIFIER OPEN_PARENS
1791 opt_formal_parameter_list
1795 Location l = lexer.Location;
1796 Delegate del = new Delegate (current_container, (Expression) $4, (int) $2,
1797 MakeName ((string) $5), (Parameters) $7,
1798 (Attributes) $1, l);
1800 del.Namespace = current_namespace;
1801 CheckDef (current_container.AddDelegate (del), del.Name, l);
1806 IDENTIFIER OPEN_PARENS
1807 opt_formal_parameter_list
1811 Location l = lexer.Location;
1812 Delegate del = new Delegate (
1814 TypeManager.system_void_expr, (int) $2, MakeName ((string) $5),
1815 (Parameters) $7, (Attributes) $1, l);
1817 del.Namespace = current_namespace;
1818 CheckDef (current_container.AddDelegate (del), del.Name, l);
1823 : namespace_or_type_name
1826 namespace_or_type_name
1827 : qualified_identifier
1831 * Before you think of adding a return_type, notice that we have been
1832 * using two rules in the places where it matters (one rule using type
1833 * and another identical one that uses VOID as the return type). This
1834 * gets rid of a shift/reduce couple
1837 : type_name { /* class_type */
1839 This does interfaces, delegates, struct_types, class_types,
1840 parent classes, and more! 4.2
1842 $$ = DecomposeQI ((string) $1, lexer.Location);
1854 // Note that here only unmanaged types are allowed but we
1855 // can't perform checks during this phase - we do it during
1856 // semantic analysis.
1858 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1862 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
1868 | non_expression_type rank_specifier
1870 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1872 | non_expression_type STAR
1874 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1876 | expression rank_specifiers
1878 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1882 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1886 // We need this because the parser will happily go and reduce IDENTIFIER STAR
1887 // through this different path
1889 | multiplicative_expression STAR
1891 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1898 ArrayList types = new ArrayList ();
1903 | type_list COMMA type
1905 ArrayList types = (ArrayList) $1;
1913 * replaces all the productions for isolating the various
1914 * simple types, but we need this to reuse it easily in local_variable_type
1917 : OBJECT { $$ = TypeManager.system_object_expr; }
1918 | STRING { $$ = TypeManager.system_string_expr; }
1919 | BOOL { $$ = TypeManager.system_boolean_expr; }
1920 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
1921 | FLOAT { $$ = TypeManager.system_single_expr; }
1922 | DOUBLE { $$ = TypeManager.system_double_expr; }
1927 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
1928 | BYTE { $$ = TypeManager.system_byte_expr; }
1929 | SHORT { $$ = TypeManager.system_int16_expr; }
1930 | USHORT { $$ = TypeManager.system_uint16_expr; }
1931 | INT { $$ = TypeManager.system_int32_expr; }
1932 | UINT { $$ = TypeManager.system_uint32_expr; }
1933 | LONG { $$ = TypeManager.system_int64_expr; }
1934 | ULONG { $$ = TypeManager.system_uint64_expr; }
1935 | CHAR { $$ = TypeManager.system_char_expr; }
1936 | VOID { $$ = TypeManager.system_void_expr; }
1944 : type rank_specifiers
1946 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1951 // Expressions, section 7.5
1959 | qualified_identifier
1961 string name = (string) $1;
1964 $$ = DecomposeQI (name, lexer.Location);
1966 | parenthesized_expression
1968 | invocation_expression
1972 | post_increment_expression
1973 | post_decrement_expression
1977 | checked_expression
1978 | unchecked_expression
1979 | pointer_member_access
1980 | anonymous_method_expression
1987 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
1988 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
1989 | NULL { $$ = NullLiteral.Null; }
1993 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
1994 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
1995 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2000 object v = lexer.Value;
2003 $$ = new IntLiteral ((Int32) v);
2005 $$ = new UIntLiteral ((UInt32) v);
2007 $$ = new LongLiteral ((Int64) v);
2008 else if (v is ulong)
2009 $$ = new ULongLiteral ((UInt64) v);
2011 Console.WriteLine ("OOPS. Unexpected result from scanner");
2016 : TRUE { $$ = new BoolLiteral (true); }
2017 | FALSE { $$ = new BoolLiteral (false); }
2020 parenthesized_expression
2021 : OPEN_PARENS expression CLOSE_PARENS
2026 : primary_expression DOT IDENTIFIER
2028 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2030 | predefined_type DOT IDENTIFIER
2032 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2040 invocation_expression
2041 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2044 Location l = lexer.Location;
2045 Report.Error (1, l, "Parse error");
2047 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2052 : /* empty */ { $$ = null; }
2059 ArrayList list = new ArrayList ();
2063 | argument_list COMMA argument
2065 ArrayList list = (ArrayList) $1;
2074 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2076 | REF variable_reference
2078 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2080 | OUT variable_reference
2082 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2087 : expression { note ("section 5.4"); $$ = $1; }
2091 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2093 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2095 | primary_expression rank_specifiers
2097 // So the super-trick is that primary_expression
2098 // can only be either a SimpleName or a MemberAccess.
2099 // The MemberAccess case arises when you have a fully qualified type-name like :
2101 // SimpleName is when you have
2104 Expression expr = (Expression) $1;
2105 if (expr is ComposedCast){
2106 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2107 } else if (!(expr is SimpleName || expr is MemberAccess)){
2108 Error_ExpectingTypeName (lexer.Location, expr);
2109 $$ = TypeManager.system_object_expr;
2112 // So we extract the string corresponding to the SimpleName
2115 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2123 ArrayList list = new ArrayList ();
2127 | expression_list COMMA expression
2129 ArrayList list = (ArrayList) $1;
2138 $$ = new This (current_block, lexer.Location);
2143 : BASE DOT IDENTIFIER
2145 $$ = new BaseAccess ((string) $3, lexer.Location);
2147 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2149 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2153 post_increment_expression
2154 : primary_expression OP_INC
2156 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2157 (Expression) $1, lexer.Location);
2161 post_decrement_expression
2162 : primary_expression OP_DEC
2164 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2165 (Expression) $1, lexer.Location);
2170 : object_or_delegate_creation_expression
2171 | array_creation_expression
2174 object_or_delegate_creation_expression
2175 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2177 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2181 array_creation_expression
2182 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2184 opt_array_initializer
2186 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2188 | NEW type rank_specifiers array_initializer
2190 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2194 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2210 : rank_specifier opt_rank_specifier
2212 $$ = (string) $2 + (string) $1;
2217 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2219 $$ = "[" + (string) $2 + "]";
2239 | dim_separators COMMA
2241 $$ = (string) $1 + ",";
2245 opt_array_initializer
2257 : OPEN_BRACE CLOSE_BRACE
2259 ArrayList list = new ArrayList ();
2262 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2264 $$ = (ArrayList) $2;
2268 variable_initializer_list
2269 : variable_initializer
2271 ArrayList list = new ArrayList ();
2275 | variable_initializer_list COMMA variable_initializer
2277 ArrayList list = (ArrayList) $1;
2284 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2286 $$ = new TypeOfVoid (lexer.Location);
2288 | TYPEOF OPEN_PARENS type CLOSE_PARENS
2290 $$ = new TypeOf ((Expression) $3, lexer.Location);
2295 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2296 $$ = new SizeOf ((Expression) $3, lexer.Location);
2301 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2303 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2307 unchecked_expression
2308 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2310 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2314 pointer_member_access
2315 : primary_expression OP_PTR IDENTIFIER
2319 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2320 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2324 anonymous_method_expression
2325 : DELEGATE opt_anonymous_method_signature {
2326 oob_stack.Push (current_local_parameters);
2327 current_local_parameters = (Parameters)$2;
2329 if (!RootContext.V2){
2330 Report.Error (-213, lexer.Location, "Anonymous methods are only supported in V2");
2333 $$ = new AnonymousMethod ((Parameters) $2, (Block) $3, lexer.Location);
2334 current_local_parameters = (Parameters) oob_stack.Pop ();
2338 opt_anonymous_method_signature
2339 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
2340 | anonymous_method_signature
2343 anonymous_method_signature
2344 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
2347 $$ = Parameters.EmptyReadOnlyParameters;
2349 ArrayList par_list = (ArrayList) $2;
2350 Parameter [] pars = new Parameter [par_list.Count];
2351 par_list.CopyTo (pars);
2352 $$ = new Parameters (pars, null, lexer.Location);
2357 opt_anonymous_method_parameter_list
2358 : /* empty */ { $$ = null; }
2359 | anonymous_method_parameter_list { $$ = $1; }
2362 anonymous_method_parameter_list
2363 : anonymous_method_parameter
2365 ArrayList a = new ArrayList ();
2369 | anonymous_method_parameter_list COMMA anonymous_method_parameter
2371 ArrayList a = (ArrayList) $1;
2377 anonymous_method_parameter
2378 : opt_parameter_modifier type IDENTIFIER {
2379 $$ = new Parameter ((Expression) $2, (string) $2, (Parameter.Modifier) $1, null);
2384 : primary_expression
2385 | BANG prefixed_unary_expression
2387 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2389 | TILDE prefixed_unary_expression
2391 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2397 : OPEN_PARENS expression CLOSE_PARENS unary_expression
2399 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2401 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2403 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2408 // The idea to split this out is from Rhys' grammar
2409 // to solve the problem with casts.
2411 prefixed_unary_expression
2413 | PLUS prefixed_unary_expression
2415 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2417 | MINUS prefixed_unary_expression
2419 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2421 | OP_INC prefixed_unary_expression
2423 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2424 (Expression) $2, lexer.Location);
2426 | OP_DEC prefixed_unary_expression
2428 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2429 (Expression) $2, lexer.Location);
2431 | STAR prefixed_unary_expression
2433 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2435 | BITWISE_AND prefixed_unary_expression
2437 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2441 pre_increment_expression
2442 : OP_INC prefixed_unary_expression
2444 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2445 (Expression) $2, lexer.Location);
2449 pre_decrement_expression
2450 : OP_DEC prefixed_unary_expression
2452 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2453 (Expression) $2, lexer.Location);
2457 multiplicative_expression
2458 : prefixed_unary_expression
2459 | multiplicative_expression STAR prefixed_unary_expression
2461 $$ = new Binary (Binary.Operator.Multiply,
2462 (Expression) $1, (Expression) $3, lexer.Location);
2464 | multiplicative_expression DIV prefixed_unary_expression
2466 $$ = new Binary (Binary.Operator.Division,
2467 (Expression) $1, (Expression) $3, lexer.Location);
2469 | multiplicative_expression PERCENT prefixed_unary_expression
2471 $$ = new Binary (Binary.Operator.Modulus,
2472 (Expression) $1, (Expression) $3, lexer.Location);
2477 : multiplicative_expression
2478 | additive_expression PLUS multiplicative_expression
2480 $$ = new Binary (Binary.Operator.Addition,
2481 (Expression) $1, (Expression) $3, lexer.Location);
2483 | additive_expression MINUS multiplicative_expression
2485 $$ = new Binary (Binary.Operator.Subtraction,
2486 (Expression) $1, (Expression) $3, lexer.Location);
2491 : additive_expression
2492 | shift_expression OP_SHIFT_LEFT additive_expression
2494 $$ = new Binary (Binary.Operator.LeftShift,
2495 (Expression) $1, (Expression) $3, lexer.Location);
2497 | shift_expression OP_SHIFT_RIGHT additive_expression
2499 $$ = new Binary (Binary.Operator.RightShift,
2500 (Expression) $1, (Expression) $3, lexer.Location);
2504 relational_expression
2506 | relational_expression OP_LT shift_expression
2508 $$ = new Binary (Binary.Operator.LessThan,
2509 (Expression) $1, (Expression) $3, lexer.Location);
2511 | relational_expression OP_GT shift_expression
2513 $$ = new Binary (Binary.Operator.GreaterThan,
2514 (Expression) $1, (Expression) $3, lexer.Location);
2516 | relational_expression OP_LE shift_expression
2518 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2519 (Expression) $1, (Expression) $3, lexer.Location);
2521 | relational_expression OP_GE shift_expression
2523 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2524 (Expression) $1, (Expression) $3, lexer.Location);
2526 | relational_expression IS type
2528 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
2530 | relational_expression AS type
2532 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
2537 : relational_expression
2538 | equality_expression OP_EQ relational_expression
2540 $$ = new Binary (Binary.Operator.Equality,
2541 (Expression) $1, (Expression) $3, lexer.Location);
2543 | equality_expression OP_NE relational_expression
2545 $$ = new Binary (Binary.Operator.Inequality,
2546 (Expression) $1, (Expression) $3, lexer.Location);
2551 : equality_expression
2552 | and_expression BITWISE_AND equality_expression
2554 $$ = new Binary (Binary.Operator.BitwiseAnd,
2555 (Expression) $1, (Expression) $3, lexer.Location);
2559 exclusive_or_expression
2561 | exclusive_or_expression CARRET and_expression
2563 $$ = new Binary (Binary.Operator.ExclusiveOr,
2564 (Expression) $1, (Expression) $3, lexer.Location);
2568 inclusive_or_expression
2569 : exclusive_or_expression
2570 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2572 $$ = new Binary (Binary.Operator.BitwiseOr,
2573 (Expression) $1, (Expression) $3, lexer.Location);
2577 conditional_and_expression
2578 : inclusive_or_expression
2579 | conditional_and_expression OP_AND inclusive_or_expression
2581 $$ = new Binary (Binary.Operator.LogicalAnd,
2582 (Expression) $1, (Expression) $3, lexer.Location);
2586 conditional_or_expression
2587 : conditional_and_expression
2588 | conditional_or_expression OP_OR conditional_and_expression
2590 $$ = new Binary (Binary.Operator.LogicalOr,
2591 (Expression) $1, (Expression) $3, lexer.Location);
2595 conditional_expression
2596 : conditional_or_expression
2597 | conditional_or_expression INTERR expression COLON expression
2599 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2603 assignment_expression
2604 : prefixed_unary_expression ASSIGN expression
2606 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2608 | prefixed_unary_expression OP_MULT_ASSIGN expression
2610 Location l = lexer.Location;
2612 $$ = new CompoundAssign (
2613 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
2615 | prefixed_unary_expression OP_DIV_ASSIGN expression
2617 Location l = lexer.Location;
2619 $$ = new CompoundAssign (
2620 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
2622 | prefixed_unary_expression OP_MOD_ASSIGN expression
2624 Location l = lexer.Location;
2626 $$ = new CompoundAssign (
2627 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
2629 | prefixed_unary_expression OP_ADD_ASSIGN expression
2631 Location l = lexer.Location;
2633 $$ = new CompoundAssign (
2634 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
2636 | prefixed_unary_expression OP_SUB_ASSIGN expression
2638 Location l = lexer.Location;
2640 $$ = new CompoundAssign (
2641 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
2643 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2645 Location l = lexer.Location;
2647 $$ = new CompoundAssign (
2648 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
2650 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2652 Location l = lexer.Location;
2654 $$ = new CompoundAssign (
2655 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
2657 | prefixed_unary_expression OP_AND_ASSIGN expression
2659 Location l = lexer.Location;
2661 $$ = new CompoundAssign (
2662 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
2664 | prefixed_unary_expression OP_OR_ASSIGN expression
2666 Location l = lexer.Location;
2668 $$ = new CompoundAssign (
2669 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
2671 | prefixed_unary_expression OP_XOR_ASSIGN expression
2673 Location l = lexer.Location;
2675 $$ = new CompoundAssign (
2676 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
2681 : conditional_expression
2682 | assignment_expression
2704 name = MakeName ((string) $4);
2706 new_class = new Class (current_container, name, (int) $2,
2707 (Attributes) $1, lexer.Location);
2708 current_container = new_class;
2709 current_container.Namespace = current_namespace;
2710 RootContext.Tree.RecordDecl (name, new_class);
2716 Class new_class = (Class) current_container;
2719 new_class.Bases = (ArrayList) $6;
2721 current_container = current_container.Parent;
2722 CheckDef (current_container.AddClass (new_class), new_class.Name, new_class.Location);
2729 : /* empty */ { $$ = (int) 0; }
2735 | modifiers modifier
2740 if ((m1 & m2) != 0) {
2741 Location l = lexer.Location;
2742 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2744 $$ = (int) (m1 | m2);
2749 : NEW { $$ = Modifiers.NEW; }
2750 | PUBLIC { $$ = Modifiers.PUBLIC; }
2751 | PROTECTED { $$ = Modifiers.PROTECTED; }
2752 | INTERNAL { $$ = Modifiers.INTERNAL; }
2753 | PRIVATE { $$ = Modifiers.PRIVATE; }
2754 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2755 | SEALED { $$ = Modifiers.SEALED; }
2756 | STATIC { $$ = Modifiers.STATIC; }
2757 | READONLY { $$ = Modifiers.READONLY; }
2758 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2759 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2760 | EXTERN { $$ = Modifiers.EXTERN; }
2761 | VOLATILE { $$ = Modifiers.VOLATILE; }
2762 | UNSAFE { $$ = Modifiers.UNSAFE; }
2766 : /* empty */ { $$ = null; }
2767 | class_base { $$ = $1; }
2771 : COLON type_list { $$ = $2; }
2779 // A block is "contained" on the following places:
2781 // property_declaration as part of the accessor body (get/set)
2782 // operator_declaration
2783 // constructor_declaration
2784 // destructor_declaration
2785 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2790 current_block = new Block (current_block, current_local_parameters,
2791 lexer.Location, Location.Null);
2793 opt_statement_list CLOSE_BRACE
2795 while (current_block.Implicit)
2796 current_block = current_block.Parent;
2798 current_block.SetEndLocation (lexer.Location);
2799 current_block = current_block.Parent;
2810 | statement_list statement
2814 : declaration_statement
2816 if ($1 != null && (Block) $1 != current_block){
2817 current_block.AddStatement ((Statement) $1);
2818 current_block = (Block) $1;
2821 | embedded_statement
2823 Statement s = (Statement) $1;
2826 current_block.AddStatement ((Statement) $1);
2834 | expression_statement
2835 | selection_statement
2836 | iteration_statement
2840 | unchecked_statement
2850 $$ = new EmptyStatement ();
2857 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
2859 if (!current_block.AddLabel ((string) $1, labeled)){
2860 Location l = lexer.Location;
2861 Report.Error (140, l, "The label '" + ((string) $1) + "' is a duplicate");
2863 current_block.AddStatement (labeled);
2868 declaration_statement
2869 : local_variable_declaration SEMICOLON
2872 DictionaryEntry de = (DictionaryEntry) $1;
2874 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
2878 | local_constant_declaration SEMICOLON
2881 DictionaryEntry de = (DictionaryEntry) $1;
2883 $$ = declare_local_constant ((Expression) de.Key, (VariableDeclaration) de.Value);
2889 * The following is from Rhys' grammar:
2890 * > Types in local variable declarations must be recognized as
2891 * > expressions to prevent reduce/reduce errors in the grammar.
2892 * > The expressions are converted into types during semantic analysis.
2895 : primary_expression opt_rank_specifier
2897 // FIXME: Do something smart here regarding the composition of the type.
2899 // Ok, the above "primary_expression" is there to get rid of
2900 // both reduce/reduce and shift/reduces in the grammar, it should
2901 // really just be "type_name". If you use type_name, a reduce/reduce
2902 // creeps up. If you use qualified_identifier (which is all we need
2903 // really) two shift/reduces appear.
2906 // So the super-trick is that primary_expression
2907 // can only be either a SimpleName or a MemberAccess.
2908 // The MemberAccess case arises when you have a fully qualified type-name like :
2910 // SimpleName is when you have
2913 Expression expr = (Expression) $1;
2914 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
2915 Error_ExpectingTypeName (lexer.Location, expr);
2919 // So we extract the string corresponding to the SimpleName
2923 if ((string) $2 == "")
2926 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2929 | builtin_types opt_rank_specifier
2931 if ((string) $2 == "")
2934 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2938 local_variable_pointer_type
2939 : primary_expression STAR
2941 Expression expr = (Expression) $1;
2942 Location l = lexer.Location;
2944 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
2945 Error_ExpectingTypeName (l, expr);
2949 $$ = new ComposedCast ((Expression) $1, "*", l);
2951 | builtin_types STAR
2953 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
2957 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
2959 | local_variable_pointer_type STAR
2961 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2965 local_variable_declaration
2966 : local_variable_type variable_declarators
2969 $$ = new DictionaryEntry ($1, $2);
2973 | local_variable_pointer_type opt_rank_specifier variable_declarators
2978 if ((string) $2 == "")
2979 t = (Expression) $1;
2981 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2982 $$ = new DictionaryEntry (t, $3);
2988 local_constant_declaration
2989 : CONST local_variable_type constant_declarator
2992 $$ = new DictionaryEntry ($2, $3);
2998 expression_statement
2999 : statement_expression SEMICOLON
3006 // We have to do the wrapping here and not in the case above,
3007 // because statement_expression is used for example in for_statement
3009 statement_expression
3010 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3011 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3012 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3013 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3014 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3015 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3016 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3018 Report.Error (1002, lexer.Location, "Expecting `;'");
3023 object_creation_expression
3024 : object_or_delegate_creation_expression
3025 { note ("complain if this is a delegate maybe?"); }
3034 : if_statement_open if_statement_rest
3043 oob_stack.Push (lexer.Location);
3048 : boolean_expression CLOSE_PARENS
3051 Location l = (Location) oob_stack.Pop ();
3053 $$ = new If ((Expression) $1, (Statement) $3, l);
3055 if (RootContext.WarningLevel >= 3){
3056 if ($3 is EmptyStatement)
3057 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3061 | boolean_expression CLOSE_PARENS
3062 embedded_statement ELSE embedded_statement
3064 Location l = (Location) oob_stack.Pop ();
3066 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
3071 : SWITCH OPEN_PARENS
3073 oob_stack.Push (lexer.Location);
3074 switch_stack.Push (current_block);
3076 expression CLOSE_PARENS
3079 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
3080 current_block = (Block) switch_stack.Pop ();
3096 Report.Error (1522, lexer.Location, "Empty switch block");
3104 ArrayList sections = new ArrayList ();
3109 | switch_sections switch_section
3111 ArrayList sections = (ArrayList) $1;
3121 current_block = current_block.CreateSwitchBlock (lexer.Location);
3125 Block topmost = current_block;
3127 while (topmost.Implicit)
3128 topmost = topmost.Parent;
3129 $$ = new SwitchSection ((ArrayList) $1, topmost);
3136 ArrayList labels = new ArrayList ();
3141 | switch_labels switch_label
3143 ArrayList labels = (ArrayList) ($1);
3151 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3152 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3155 1523, lexer.Location,
3156 "The keyword case or default must precede code in switch block");
3170 oob_stack.Push (lexer.Location);
3172 boolean_expression CLOSE_PARENS embedded_statement
3174 Location l = (Location) oob_stack.Pop ();
3175 $$ = new While ((Expression) $4, (Statement) $6, l);
3177 if (RootContext.WarningLevel >= 3){
3178 if ($6 is EmptyStatement)
3179 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3185 : DO embedded_statement
3187 oob_stack.Push (lexer.Location);
3189 boolean_expression CLOSE_PARENS SEMICOLON
3191 Location l = (Location) oob_stack.Pop ();
3193 $$ = new Do ((Statement) $2, (Expression) $6, l);
3199 opt_for_initializer SEMICOLON
3201 Block assign_block = new Block (current_block);
3202 current_block = assign_block;
3204 if ($3 is DictionaryEntry){
3205 DictionaryEntry de = (DictionaryEntry) $3;
3207 Expression type = (Expression) de.Key;
3208 ArrayList var_declarators = (ArrayList) de.Value;
3210 foreach (VariableDeclaration decl in var_declarators){
3214 vi = current_block.AddVariable (
3215 type, decl.identifier, current_local_parameters, decl.Location);
3219 Location l = lexer.Location;
3221 if (decl.expression_or_array_initializer is Expression){
3222 expr = (Expression) decl.expression_or_array_initializer;
3223 } else if (decl.expression_or_array_initializer == null) {
3226 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3227 expr = new ArrayCreation (type, "", init, decl.Location);
3230 LocalVariableReference var;
3231 var = new LocalVariableReference (assign_block, decl.identifier, l);
3234 Assign a = new Assign (var, expr, decl.Location);
3236 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3242 oob_stack.Push (lexer.Location);
3244 opt_for_condition SEMICOLON
3245 opt_for_iterator CLOSE_PARENS
3248 Location l = (Location) oob_stack.Pop ();
3250 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3252 if (RootContext.WarningLevel >= 3){
3253 if ($10 is EmptyStatement)
3254 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3257 current_block.AddStatement (f);
3258 while (current_block.Implicit)
3259 current_block = current_block.Parent;
3261 current_block = current_block.Parent;
3266 : /* empty */ { $$ = new EmptyStatement (); }
3271 : local_variable_declaration
3272 | statement_expression_list
3276 : /* empty */ { $$ = null; }
3277 | boolean_expression
3281 : /* empty */ { $$ = new EmptyStatement (); }
3286 : statement_expression_list
3289 statement_expression_list
3290 : statement_expression
3292 // CHANGE: was `null'
3293 Block b = new Block (current_block, Block.Flags.Implicit);
3295 b.AddStatement ((Statement) $1);
3298 | statement_expression_list COMMA statement_expression
3300 Block b = (Block) $1;
3302 b.AddStatement ((Statement) $3);
3308 : FOREACH OPEN_PARENS type IDENTIFIER IN
3310 oob_stack.Push (lexer.Location);
3312 expression CLOSE_PARENS
3314 oob_stack.Push (current_block);
3316 Block foreach_block = new Block (current_block, Block.Flags.Implicit);
3317 LocalVariableReference v = null;
3318 Location l = lexer.Location;
3321 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3325 // Get a writable reference to this read-only variable.
3326 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3328 current_block = foreach_block;
3331 oob_stack.Push (current_block);
3335 Block foreach_block = (Block) oob_stack.Pop ();
3336 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3337 Block prev_block = (Block) oob_stack.Pop ();
3338 Location l = (Location) oob_stack.Pop ();
3340 current_block = prev_block;
3343 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3344 foreach_block.AddStatement (f);
3353 | continue_statement
3363 $$ = new Break (lexer.Location);
3368 : CONTINUE SEMICOLON
3370 $$ = new Continue (lexer.Location);
3375 : GOTO IDENTIFIER SEMICOLON
3377 $$ = new Goto (current_block, (string) $2, lexer.Location);
3379 | GOTO CASE constant_expression SEMICOLON
3381 $$ = new GotoCase ((Expression) $3, lexer.Location);
3383 | GOTO DEFAULT SEMICOLON
3385 $$ = new GotoDefault (lexer.Location);
3390 : RETURN opt_expression SEMICOLON
3392 $$ = new Return ((Expression) $2, lexer.Location);
3397 : THROW opt_expression SEMICOLON
3399 $$ = new Throw ((Expression) $2, lexer.Location);
3404 : YIELD expression SEMICOLON
3406 if (iterator_container == null){
3407 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3410 iterator_container.SetYields ();
3411 $$ = new Yield ((Expression) $2, lexer.Location);
3414 | YIELD BREAK SEMICOLON
3416 if (iterator_container == null){
3417 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3420 iterator_container.SetYields ();
3421 $$ = new YieldBreak (lexer.Location);
3432 : TRY block catch_clauses
3435 ArrayList s = new ArrayList ();
3437 foreach (Catch cc in (ArrayList) $3) {
3444 // Now s contains the list of specific catch clauses
3445 // and g contains the general one.
3447 $$ = new Try ((Block) $2, s, g, null, lexer.Location);
3449 | TRY block opt_catch_clauses FINALLY block
3452 ArrayList s = new ArrayList ();
3453 ArrayList catch_list = (ArrayList) $3;
3455 if (catch_list != null){
3456 foreach (Catch cc in catch_list) {
3464 $$ = new Try ((Block) $2, s, g, (Block) $5, lexer.Location);
3468 Report.Error (1524, lexer.Location, "Expected catch or finally");
3473 : /* empty */ { $$ = null; }
3480 ArrayList l = new ArrayList ();
3485 | catch_clauses catch_clause
3487 ArrayList l = (ArrayList) $1;
3495 : /* empty */ { $$ = null; }
3500 : CATCH opt_catch_args
3502 Expression type = null;
3506 DictionaryEntry cc = (DictionaryEntry) $2;
3507 type = (Expression) cc.Key;
3508 id = (string) cc.Value;
3511 ArrayList one = new ArrayList ();
3512 Location loc = lexer.Location;
3514 one.Add (new VariableDeclaration (id, null, loc));
3517 current_block = new Block (current_block);
3518 Block b = declare_local_variables (type, one, loc);
3523 Expression type = null;
3527 DictionaryEntry cc = (DictionaryEntry) $2;
3528 type = (Expression) cc.Key;
3529 id = (string) cc.Value;
3533 // FIXME: I can change this for an assignment.
3535 while (current_block != (Block) $1)
3536 current_block = current_block.Parent;
3541 $$ = new Catch (type, id , (Block) $4, lexer.Location);
3546 : /* empty */ { $$ = null; }
3551 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3553 $$ = new DictionaryEntry ($2, $3);
3560 $$ = new Checked ((Block) $2);
3567 $$ = new Unchecked ((Block) $2);
3574 if (!RootContext.Unsafe){
3575 Report.Error (227, lexer.Location,
3576 "Unsafe code can only be used if --unsafe is used");
3579 $$ = new Unsafe ((Block) $3);
3585 type fixed_pointer_declarators
3588 Block assign_block = new Block (current_block, Block.Flags.Implicit);
3589 ArrayList list = (ArrayList) $4;
3590 Expression type = (Expression) $3;
3591 Location l = lexer.Location;
3592 int top = list.Count;
3594 for (int i = 0; i < top; i++){
3595 Pair p = (Pair) list [i];
3598 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3605 current_block.AddStatement (assign_block);
3606 current_block = assign_block;
3607 oob_stack.Push (assign_block);
3612 Location l = (Location) oob_stack.Pop ();
3613 Block assign_block = (Block) oob_stack.Pop ();
3615 ArrayList list = (ArrayList) $4;
3616 int top = list.Count;
3618 $$ = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
3622 fixed_pointer_declarators
3623 : fixed_pointer_declarator {
3624 ArrayList declarators = new ArrayList ();
3625 declarators.Add ($1);
3628 | fixed_pointer_declarators COMMA fixed_pointer_declarator
3630 ArrayList declarators = (ArrayList) $1;
3631 declarators.Add ($3);
3636 fixed_pointer_declarator
3637 : IDENTIFIER ASSIGN expression
3639 $$ = new Pair ($1, $3);
3644 : LOCK OPEN_PARENS expression CLOSE_PARENS
3650 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
3655 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
3657 Block assign_block = new Block (current_block);
3658 current_block = assign_block;
3660 oob_stack.Push (lexer.Location);
3662 if ($3 is DictionaryEntry){
3663 DictionaryEntry de = (DictionaryEntry) $3;
3664 Location l = lexer.Location;
3666 Expression type = (Expression) de.Key;
3667 ArrayList var_declarators = (ArrayList) de.Value;
3669 ArrayList vars = new ArrayList ();
3671 foreach (VariableDeclaration decl in var_declarators){
3673 LocalInfo vi = current_block.AddVariable (
3674 type, decl.identifier,
3675 current_local_parameters, decl.Location);
3681 if (decl.expression_or_array_initializer is Expression){
3682 expr = (Expression) decl.expression_or_array_initializer;
3684 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3686 expr = new ArrayCreation (type, "", init, decl.Location);
3689 LocalVariableReference var;
3691 // Get a writable reference to this read-only variable.
3692 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
3694 // This is so that it is not a warning on using variables
3697 vars.Add (new DictionaryEntry (var, expr));
3699 // Assign a = new Assign (var, expr, decl.Location);
3700 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3702 $3 = new DictionaryEntry (type, vars);
3707 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
3708 current_block.AddStatement (u);
3709 while (current_block.Implicit)
3710 current_block = current_block.Parent;
3712 current_block = current_block.Parent;
3716 resource_acquisition
3717 : local_variable_declaration
3724 // A class used to pass around variable declarations and constants
3726 public class VariableDeclaration {
3727 public string identifier;
3728 public object expression_or_array_initializer;
3729 public Location Location;
3730 public Attributes OptAttributes;
3732 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
3734 this.identifier = id;
3735 this.expression_or_array_initializer = eoai;
3737 this.OptAttributes = opt_attrs;
3740 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
3746 // A class used to hold info about an indexer declarator
3749 public class IndexerDeclaration {
3750 public Expression type;
3751 public string interface_type;
3752 public Parameters param_list;
3754 public IndexerDeclaration (Expression type, string interface_type, Parameters param_list)
3757 this.interface_type = interface_type;
3758 this.param_list = param_list;
3763 // A class used to hold info about an operator declarator
3766 public class OperatorDeclaration {
3767 public Operator.OpType optype;
3768 public Expression ret_type, arg1type, arg2type;
3769 public string arg1name, arg2name;
3770 public Location location;
3772 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
3773 Expression arg1type, string arg1name,
3774 Expression arg2type, string arg2name, Location location)
3777 this.ret_type = ret_type;
3778 this.arg1type = arg1type;
3779 this.arg1name = arg1name;
3780 this.arg2type = arg2type;
3781 this.arg2name = arg2name;
3782 this.location = location;
3787 void Error_ExpectingTypeName (Location l, Expression expr)
3789 if (expr is Invocation){
3790 Report.Error (1002, l, "; expected");
3792 Report.Error (-1, l, "Invalid Type definition");
3797 // Given the @class_name name, it creates a fully qualified name
3798 // based on the containing declaration space
3801 MakeName (string class_name)
3803 string ns = current_namespace.Name;
3804 string container_name = current_container.Name;
3806 if (container_name == ""){
3808 return ns + "." + class_name;
3812 return container_name + "." + class_name;
3816 // Used to report back to the user the result of a declaration
3817 // in the current declaration space
3820 CheckDef (AdditionResult result, string name, Location l)
3822 if (result == AdditionResult.Success)
3826 case AdditionResult.NameExists:
3827 Report.Error (102, l, "The container `" + current_container.Name +
3828 "' already contains a definition for `"+
3834 // This is handled only for static Constructors, because
3835 // in reality we handle these by the semantic analysis later
3837 case AdditionResult.MethodExists:
3839 111, l, "Class `"+current_container.Name+
3840 "' already defines a member called '" +
3841 name + "' with the same parameter types (more than one default constructor)");
3844 case AdditionResult.EnclosingClash:
3845 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
3848 case AdditionResult.NotAConstructor:
3849 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
3852 case AdditionResult.Error:
3853 // Error has already been reported.
3859 CheckDef (bool result, string name, Location l)
3863 CheckDef (AdditionResult.NameExists, name, l);
3866 Expression DecomposeQI (string name, Location loc)
3870 if (name.IndexOf ('.') == -1){
3871 return new SimpleName (name, loc);
3873 int pos = name.LastIndexOf (".");
3874 string left = name.Substring (0, pos);
3875 string right = name.Substring (pos + 1);
3877 o = DecomposeQI (left, loc);
3879 return new MemberAccess (o, right, loc);
3884 // This method is used to get at the complete string representation of
3885 // a fully-qualified type name, hiding inside a MemberAccess ;-)
3886 // This is necessary because local_variable_type admits primary_expression
3887 // as the type of the variable. So we do some extra checking
3890 string GetQualifiedIdentifier (Expression expr)
3892 if (expr is SimpleName)
3893 return ((SimpleName)expr).Name;
3894 else if (expr is MemberAccess)
3895 return GetQualifiedIdentifier (((MemberAccess)expr).Expr) + "." + ((MemberAccess) expr).Identifier;
3897 throw new Exception ("Expr has to be either SimpleName or MemberAccess! (" + expr + ")");
3902 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
3904 Block implicit_block;
3905 ArrayList inits = null;
3908 // We use the `Used' property to check whether statements
3909 // have been added to the current block. If so, we need
3910 // to create another block to contain the new declaration
3911 // otherwise, as an optimization, we use the same block to
3912 // add the declaration.
3914 // FIXME: A further optimization is to check if the statements
3915 // that were added were added as part of the initialization
3916 // below. In which case, no other statements have been executed
3917 // and we might be able to reduce the number of blocks for
3918 // situations like this:
3920 // int j = 1; int k = j + 1;
3922 if (current_block.Used) {
3923 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
3924 implicit_block.AddChildVariableNames (current_block);
3926 implicit_block = current_block;
3928 foreach (VariableDeclaration decl in variable_declarators){
3930 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
3931 if (decl.expression_or_array_initializer != null){
3933 inits = new ArrayList ();
3940 return implicit_block;
3942 foreach (VariableDeclaration decl in inits){
3946 if (decl.expression_or_array_initializer is Expression){
3947 expr = (Expression) decl.expression_or_array_initializer;
3950 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3952 expr = new ArrayCreation (type, "", init, decl.Location);
3955 LocalVariableReference var;
3956 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
3958 assign = new Assign (var, expr, decl.Location);
3960 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
3963 return implicit_block;
3966 Block declare_local_constant (Expression type, VariableDeclaration decl)
3968 Block implicit_block;
3970 if (current_block.Used)
3971 implicit_block = new Block (current_block, Block.Flags.Implicit);
3973 implicit_block = current_block;
3975 if (!(implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
3976 current_local_parameters, decl.Location))){
3979 return implicit_block;
3982 void CheckAttributeTarget (string a)
3986 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
3990 Location l = lexer.Location;
3991 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
3997 void CheckUnaryOperator (Operator.OpType op)
4001 case Operator.OpType.LogicalNot:
4002 case Operator.OpType.OnesComplement:
4003 case Operator.OpType.Increment:
4004 case Operator.OpType.Decrement:
4005 case Operator.OpType.True:
4006 case Operator.OpType.False:
4007 case Operator.OpType.Addition:
4008 case Operator.OpType.Subtraction:
4013 Location l = lexer.Location;
4014 Report.Error (1019, l, "Overloadable unary operator expected");
4020 void CheckBinaryOperator (Operator.OpType op)
4024 case Operator.OpType.Addition:
4025 case Operator.OpType.Subtraction:
4026 case Operator.OpType.Multiply:
4027 case Operator.OpType.Division:
4028 case Operator.OpType.Modulus:
4029 case Operator.OpType.BitwiseAnd:
4030 case Operator.OpType.BitwiseOr:
4031 case Operator.OpType.ExclusiveOr:
4032 case Operator.OpType.LeftShift:
4033 case Operator.OpType.RightShift:
4034 case Operator.OpType.Equality:
4035 case Operator.OpType.Inequality:
4036 case Operator.OpType.GreaterThan:
4037 case Operator.OpType.LessThan:
4038 case Operator.OpType.GreaterThanOrEqual:
4039 case Operator.OpType.LessThanOrEqual:
4043 Location l = lexer.Location;
4044 Report.Error (1020, l, "Overloadable binary operator expected");
4050 void syntax_error (Location l, string msg)
4052 Report.Error (1003, l, "Syntax error, " + msg);
4055 void output (string s)
4057 Console.WriteLine (s);
4060 void note (string s)
4062 // Used to put annotations
4067 public Tokenizer Lexer {
4073 public CSharpParser (StreamReader reader, SourceFile file, ArrayList defines)
4075 current_namespace = new NamespaceEntry (null, file, null);
4076 this.name = file.Name;
4078 current_container = RootContext.Tree.Types;
4079 current_container.Namespace = current_namespace;
4080 oob_stack = new Stack ();
4081 switch_stack = new Stack ();
4083 lexer = new Tokenizer (reader, file, defines);
4086 public override void parse ()
4089 if (yacc_verbose_flag)
4090 yyparse (lexer, new yydebug.yyDebugSimple ());
4093 Tokenizer tokenizer = lexer as Tokenizer;
4094 tokenizer.cleanup ();
4095 } catch (Exception e){
4096 // Please do not remove this, it is used during debugging
4099 Report.Error (-25, lexer.Location, ": Parsing error ");
4100 Console.WriteLine (e);