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;
33 public class CSharpParser {
34 NamespaceEntry current_namespace;
35 TypeContainer current_container;
37 IIteratorContainer iterator_container;
40 // Current block is used to add statements as we find
47 // Current interface is used by the various declaration
48 // productions in the interface declaration to "add"
49 // the interfaces as we find them.
51 Interface current_interface;
54 // This is used by the unary_expression code to resolve
55 // a name against a parameter.
57 Parameters current_local_parameters;
60 // Using during property parsing to describe the implicit
61 // value parameter that is passed to the "set" and "get"accesor
62 // methods (properties and indexers).
64 Expression implicit_value_parameter_type;
65 Parameters indexer_parameters;
68 // Used to determine if we are parsing the get/set pair
69 // of an indexer or a property
74 // An out-of-band stack.
83 public bool yacc_verbose_flag;
85 // Name of the file we are parsing
95 %token NONE /* This token is never returned by our lexer */
96 %token ERROR // This is used not by the parser, but by the tokenizer.
100 *These are the C# keywords
187 /* C# keywords which are not really keywords */
191 /* C# single character operators/punctuation. */
192 %token OPEN_BRACE "{"
193 %token CLOSE_BRACE "}"
194 %token OPEN_BRACKET "["
195 %token CLOSE_BRACKET "]"
196 %token OPEN_PARENS "("
197 %token CLOSE_PARENS ")"
210 %token BITWISE_AND "&"
211 %token BITWISE_OR "|"
218 /* C# multi-character operators. */
227 %token OP_MULT_ASSIGN "*="
228 %token OP_DIV_ASSIGN "/="
229 %token OP_MOD_ASSIGN "%="
230 %token OP_ADD_ASSIGN "+="
231 %token OP_SUB_ASSIGN "-="
232 %token OP_AND_ASSIGN "&="
233 %token OP_XOR_ASSIGN "^="
234 %token OP_OR_ASSIGN "|="
238 %token LITERAL_INTEGER "int literal"
239 %token LITERAL_FLOAT "float literal"
240 %token LITERAL_DOUBLE "double literal"
241 %token LITERAL_DECIMAL "decimal literal"
242 %token LITERAL_CHARACTER "character literal"
243 %token LITERAL_STRING "string literal"
247 /* Add precedence rules to solve dangling else s/r conflict */
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 : qualified_identifier
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 (4);
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 (4);
556 | positional_argument_list COMMA named_argument_list
558 ArrayList args = new ArrayList (4);
564 | named_argument_list
566 ArrayList args = new ArrayList (4);
575 opt_positional_argument_list
576 : /* empty */ { $$ = null; }
577 | positional_argument_list
580 positional_argument_list
583 ArrayList args = new ArrayList (4);
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 (4);
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 (4);
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 (4);
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 (4);
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 (4);
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_LT OP_LT { $$ = Operator.OpType.LeftShift; }
1362 | OP_GT OP_GT { $$ = 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 (4); }
1750 | enum_member_declarations opt_comma { $$ = $1; }
1753 enum_member_declarations
1754 : enum_member_declaration
1756 ArrayList l = new ArrayList (4);
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 : IDENTIFIER opt_type_argument_list {
1829 $$ = new SimpleName ((string) $1, lexer.Location);
1831 $$ = new ConstructedType (null, (string) $1, (TypeArguments) $2, lexer.Location);
1833 | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list
1838 right = new SimpleName ((string) $3, lexer.Location);
1840 right = new ConstructedType ((Expression) $1, (string) $3,
1841 (TypeArguments) $4, lexer.Location);
1844 // This is known to be broken, just to compile for now
1845 // Right should be an expression, not a string
1847 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1853 // Figure out what to do with the list
1855 opt_type_argument_list
1856 : /* empty */ { $$ = null; }
1857 | OP_LT type_arguments OP_GT { $$ = $2; }
1862 TypeArguments type_args = new TypeArguments ();
1863 type_args.Add ((Expression) $1);
1866 | type_arguments COMMA type {
1867 TypeArguments type_args = (TypeArguments) $1;
1868 type_args.Add ((Expression) $3);
1874 * Before you think of adding a return_type, notice that we have been
1875 * using two rules in the places where it matters (one rule using type
1876 * and another identical one that uses VOID as the return type). This
1877 * gets rid of a shift/reduce couple
1880 : type_name { /* class_type */
1882 This does interfaces, delegates, struct_types, class_types,
1883 parent classes, and more! 4.2
1885 $$ = DecomposeQI ((string) $1, lexer.Location);
1897 // Note that here only unmanaged types are allowed but we
1898 // can't perform checks during this phase - we do it during
1899 // semantic analysis.
1901 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1905 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
1911 | non_expression_type rank_specifier
1913 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1915 | non_expression_type STAR
1917 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1919 | expression rank_specifiers
1921 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1925 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1929 // We need this because the parser will happily go and reduce IDENTIFIER STAR
1930 // through this different path
1932 | multiplicative_expression STAR
1934 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1941 ArrayList types = new ArrayList (4);
1946 | type_list COMMA type
1948 ArrayList types = (ArrayList) $1;
1956 * replaces all the productions for isolating the various
1957 * simple types, but we need this to reuse it easily in local_variable_type
1960 : OBJECT { $$ = TypeManager.system_object_expr; }
1961 | STRING { $$ = TypeManager.system_string_expr; }
1962 | BOOL { $$ = TypeManager.system_boolean_expr; }
1963 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
1964 | FLOAT { $$ = TypeManager.system_single_expr; }
1965 | DOUBLE { $$ = TypeManager.system_double_expr; }
1970 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
1971 | BYTE { $$ = TypeManager.system_byte_expr; }
1972 | SHORT { $$ = TypeManager.system_int16_expr; }
1973 | USHORT { $$ = TypeManager.system_uint16_expr; }
1974 | INT { $$ = TypeManager.system_int32_expr; }
1975 | UINT { $$ = TypeManager.system_uint32_expr; }
1976 | LONG { $$ = TypeManager.system_int64_expr; }
1977 | ULONG { $$ = TypeManager.system_uint64_expr; }
1978 | CHAR { $$ = TypeManager.system_char_expr; }
1979 | VOID { $$ = TypeManager.system_void_expr; }
1987 : type rank_specifiers
1989 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1994 // Expressions, section 7.5
2002 | qualified_identifier
2004 string name = (string) $1;
2007 $$ = DecomposeQI (name, lexer.Location);
2009 | parenthesized_expression
2011 | invocation_expression
2015 | post_increment_expression
2016 | post_decrement_expression
2020 | checked_expression
2021 | unchecked_expression
2022 | pointer_member_access
2023 | anonymous_method_expression
2030 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2031 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2032 | NULL { $$ = NullLiteral.Null; }
2036 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2037 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2038 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2043 object v = lexer.Value;
2046 $$ = new IntLiteral ((Int32) v);
2048 $$ = new UIntLiteral ((UInt32) v);
2050 $$ = new LongLiteral ((Int64) v);
2051 else if (v is ulong)
2052 $$ = new ULongLiteral ((UInt64) v);
2054 Console.WriteLine ("OOPS. Unexpected result from scanner");
2059 : TRUE { $$ = new BoolLiteral (true); }
2060 | FALSE { $$ = new BoolLiteral (false); }
2063 parenthesized_expression
2064 : OPEN_PARENS expression CLOSE_PARENS
2069 : primary_expression DOT IDENTIFIER
2071 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2073 | predefined_type DOT IDENTIFIER
2075 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2083 invocation_expression
2084 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2087 Location l = lexer.Location;
2088 Report.Error (1, l, "Parse error");
2090 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2092 | OPEN_PARENS expression CLOSE_PARENS unary_expression
2094 $$ = new InvocationOrCast ((Expression) $2, (Expression) $4, lexer.Location);
2099 : /* empty */ { $$ = null; }
2106 ArrayList list = new ArrayList (4);
2110 | argument_list COMMA argument
2112 ArrayList list = (ArrayList) $1;
2121 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2123 | REF variable_reference
2125 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2127 | OUT variable_reference
2129 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2134 : expression { note ("section 5.4"); $$ = $1; }
2138 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2140 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2142 | primary_expression rank_specifiers
2144 // So the super-trick is that primary_expression
2145 // can only be either a SimpleName or a MemberAccess.
2146 // The MemberAccess case arises when you have a fully qualified type-name like :
2148 // SimpleName is when you have
2151 Expression expr = (Expression) $1;
2152 if (expr is ComposedCast){
2153 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2154 } else if (!(expr is SimpleName || expr is MemberAccess)){
2155 Error_ExpectingTypeName (lexer.Location, expr);
2156 $$ = TypeManager.system_object_expr;
2159 // So we extract the string corresponding to the SimpleName
2162 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2170 ArrayList list = new ArrayList (4);
2174 | expression_list COMMA expression
2176 ArrayList list = (ArrayList) $1;
2185 $$ = new This (current_block, lexer.Location);
2190 : BASE DOT IDENTIFIER
2192 $$ = new BaseAccess ((string) $3, lexer.Location);
2194 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2196 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2200 post_increment_expression
2201 : primary_expression OP_INC
2203 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2204 (Expression) $1, lexer.Location);
2208 post_decrement_expression
2209 : primary_expression OP_DEC
2211 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2212 (Expression) $1, lexer.Location);
2217 : object_or_delegate_creation_expression
2218 | array_creation_expression
2221 object_or_delegate_creation_expression
2222 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2224 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2228 array_creation_expression
2229 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2231 opt_array_initializer
2233 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2235 | NEW type rank_specifiers array_initializer
2237 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2241 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2257 : rank_specifier opt_rank_specifier
2259 $$ = (string) $2 + (string) $1;
2264 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2266 $$ = "[" + (string) $2 + "]";
2286 | dim_separators COMMA
2288 $$ = (string) $1 + ",";
2292 opt_array_initializer
2304 : OPEN_BRACE CLOSE_BRACE
2306 ArrayList list = new ArrayList (4);
2309 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2311 $$ = (ArrayList) $2;
2315 variable_initializer_list
2316 : variable_initializer
2318 ArrayList list = new ArrayList (4);
2322 | variable_initializer_list COMMA variable_initializer
2324 ArrayList list = (ArrayList) $1;
2331 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2333 $$ = new TypeOfVoid (lexer.Location);
2335 | TYPEOF OPEN_PARENS type CLOSE_PARENS
2337 $$ = new TypeOf ((Expression) $3, lexer.Location);
2342 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2343 $$ = new SizeOf ((Expression) $3, lexer.Location);
2348 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2350 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2354 unchecked_expression
2355 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2357 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2361 pointer_member_access
2362 : primary_expression OP_PTR IDENTIFIER
2366 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2367 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2371 anonymous_method_expression
2372 : DELEGATE opt_anonymous_method_signature {
2373 oob_stack.Push (current_local_parameters);
2374 current_local_parameters = (Parameters)$2;
2376 if (!RootContext.V2){
2377 Report.Error (-213, lexer.Location, "Anonymous methods are only supported in V2");
2380 $$ = new AnonymousMethod ((Parameters) $2, (Block) $3, lexer.Location);
2381 current_local_parameters = (Parameters) oob_stack.Pop ();
2385 opt_anonymous_method_signature
2386 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
2387 | anonymous_method_signature
2390 anonymous_method_signature
2391 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
2394 $$ = Parameters.EmptyReadOnlyParameters;
2396 ArrayList par_list = (ArrayList) $2;
2397 Parameter [] pars = new Parameter [par_list.Count];
2398 par_list.CopyTo (pars);
2399 $$ = new Parameters (pars, null, lexer.Location);
2404 opt_anonymous_method_parameter_list
2405 : /* empty */ { $$ = null; }
2406 | anonymous_method_parameter_list { $$ = $1; }
2409 anonymous_method_parameter_list
2410 : anonymous_method_parameter
2412 ArrayList a = new ArrayList (4);
2416 | anonymous_method_parameter_list COMMA anonymous_method_parameter
2418 ArrayList a = (ArrayList) $1;
2424 anonymous_method_parameter
2425 : opt_parameter_modifier type IDENTIFIER {
2426 $$ = new Parameter ((Expression) $2, (string) $2, (Parameter.Modifier) $1, null);
2431 : primary_expression
2432 | BANG prefixed_unary_expression
2434 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2436 | TILDE prefixed_unary_expression
2438 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2444 : OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2446 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2451 // The idea to split this out is from Rhys' grammar
2452 // to solve the problem with casts.
2454 prefixed_unary_expression
2456 | PLUS prefixed_unary_expression
2458 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2460 | MINUS prefixed_unary_expression
2462 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2464 | OP_INC prefixed_unary_expression
2466 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2467 (Expression) $2, lexer.Location);
2469 | OP_DEC prefixed_unary_expression
2471 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2472 (Expression) $2, lexer.Location);
2474 | STAR prefixed_unary_expression
2476 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2478 | BITWISE_AND prefixed_unary_expression
2480 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2484 pre_increment_expression
2485 : OP_INC prefixed_unary_expression
2487 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2488 (Expression) $2, lexer.Location);
2492 pre_decrement_expression
2493 : OP_DEC prefixed_unary_expression
2495 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2496 (Expression) $2, lexer.Location);
2500 multiplicative_expression
2501 : prefixed_unary_expression
2502 | multiplicative_expression STAR prefixed_unary_expression
2504 $$ = new Binary (Binary.Operator.Multiply,
2505 (Expression) $1, (Expression) $3, lexer.Location);
2507 | multiplicative_expression DIV prefixed_unary_expression
2509 $$ = new Binary (Binary.Operator.Division,
2510 (Expression) $1, (Expression) $3, lexer.Location);
2512 | multiplicative_expression PERCENT prefixed_unary_expression
2514 $$ = new Binary (Binary.Operator.Modulus,
2515 (Expression) $1, (Expression) $3, lexer.Location);
2520 : multiplicative_expression
2521 | additive_expression PLUS multiplicative_expression
2523 $$ = new Binary (Binary.Operator.Addition,
2524 (Expression) $1, (Expression) $3, lexer.Location);
2526 | additive_expression MINUS multiplicative_expression
2528 $$ = new Binary (Binary.Operator.Subtraction,
2529 (Expression) $1, (Expression) $3, lexer.Location);
2534 : additive_expression
2535 | shift_expression OP_LT OP_LT additive_expression
2537 $$ = new Binary (Binary.Operator.LeftShift,
2538 (Expression) $1, (Expression) $3, lexer.Location);
2540 | shift_expression OP_GT OP_GT additive_expression
2542 $$ = new Binary (Binary.Operator.RightShift,
2543 (Expression) $1, (Expression) $3, lexer.Location);
2547 relational_expression
2549 | relational_expression OP_LT shift_expression
2551 $$ = new Binary (Binary.Operator.LessThan,
2552 (Expression) $1, (Expression) $3, lexer.Location);
2554 | relational_expression OP_GT shift_expression
2556 $$ = new Binary (Binary.Operator.GreaterThan,
2557 (Expression) $1, (Expression) $3, lexer.Location);
2559 | relational_expression OP_LE shift_expression
2561 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2562 (Expression) $1, (Expression) $3, lexer.Location);
2564 | relational_expression OP_GE shift_expression
2566 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2567 (Expression) $1, (Expression) $3, lexer.Location);
2569 | relational_expression IS type
2571 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
2573 | relational_expression AS type
2575 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
2580 : relational_expression
2581 | equality_expression OP_EQ relational_expression
2583 $$ = new Binary (Binary.Operator.Equality,
2584 (Expression) $1, (Expression) $3, lexer.Location);
2586 | equality_expression OP_NE relational_expression
2588 $$ = new Binary (Binary.Operator.Inequality,
2589 (Expression) $1, (Expression) $3, lexer.Location);
2594 : equality_expression
2595 | and_expression BITWISE_AND equality_expression
2597 $$ = new Binary (Binary.Operator.BitwiseAnd,
2598 (Expression) $1, (Expression) $3, lexer.Location);
2602 exclusive_or_expression
2604 | exclusive_or_expression CARRET and_expression
2606 $$ = new Binary (Binary.Operator.ExclusiveOr,
2607 (Expression) $1, (Expression) $3, lexer.Location);
2611 inclusive_or_expression
2612 : exclusive_or_expression
2613 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2615 $$ = new Binary (Binary.Operator.BitwiseOr,
2616 (Expression) $1, (Expression) $3, lexer.Location);
2620 conditional_and_expression
2621 : inclusive_or_expression
2622 | conditional_and_expression OP_AND inclusive_or_expression
2624 $$ = new Binary (Binary.Operator.LogicalAnd,
2625 (Expression) $1, (Expression) $3, lexer.Location);
2629 conditional_or_expression
2630 : conditional_and_expression
2631 | conditional_or_expression OP_OR conditional_and_expression
2633 $$ = new Binary (Binary.Operator.LogicalOr,
2634 (Expression) $1, (Expression) $3, lexer.Location);
2638 conditional_expression
2639 : conditional_or_expression
2640 | conditional_or_expression INTERR expression COLON expression
2642 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2646 assignment_expression
2647 : prefixed_unary_expression ASSIGN expression
2649 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2651 | prefixed_unary_expression OP_MULT_ASSIGN expression
2653 Location l = lexer.Location;
2655 $$ = new CompoundAssign (
2656 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
2658 | prefixed_unary_expression OP_DIV_ASSIGN expression
2660 Location l = lexer.Location;
2662 $$ = new CompoundAssign (
2663 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
2665 | prefixed_unary_expression OP_MOD_ASSIGN expression
2667 Location l = lexer.Location;
2669 $$ = new CompoundAssign (
2670 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
2672 | prefixed_unary_expression OP_ADD_ASSIGN expression
2674 Location l = lexer.Location;
2676 $$ = new CompoundAssign (
2677 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
2679 | prefixed_unary_expression OP_SUB_ASSIGN expression
2681 Location l = lexer.Location;
2683 $$ = new CompoundAssign (
2684 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
2686 | prefixed_unary_expression OP_LT OP_LE expression
2688 Location l = lexer.Location;
2690 $$ = new CompoundAssign (
2691 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
2693 | prefixed_unary_expression OP_GT OP_GE expression
2695 Location l = lexer.Location;
2697 $$ = new CompoundAssign (
2698 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
2700 | prefixed_unary_expression OP_AND_ASSIGN expression
2702 Location l = lexer.Location;
2704 $$ = new CompoundAssign (
2705 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
2707 | prefixed_unary_expression OP_OR_ASSIGN expression
2709 Location l = lexer.Location;
2711 $$ = new CompoundAssign (
2712 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
2714 | prefixed_unary_expression OP_XOR_ASSIGN expression
2716 Location l = lexer.Location;
2718 $$ = new CompoundAssign (
2719 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
2724 : conditional_expression
2725 | assignment_expression
2747 name = MakeName ((string) $4);
2749 new_class = new Class (current_container, name, (int) $2,
2750 (Attributes) $1, lexer.Location);
2751 current_container = new_class;
2752 current_container.Namespace = current_namespace;
2753 RootContext.Tree.RecordDecl (name, new_class);
2755 opt_type_parameter_list
2757 opt_type_parameter_constraints_clauses
2761 Class new_class = (Class) current_container;
2763 if ($8 != null && $6 == null)
2764 Report.Error (-200, new_class.Location,
2765 "Type parameter constraints only valid if there is a type parameter list");
2768 CheckDef (new_class.SetParameterInfo ((ArrayList) $6, $8), new_class.Name, new_class.Location);
2770 new_class.Bases = (ArrayList) $7;
2771 current_container = current_container.Parent;
2772 CheckDef (current_container.AddClass (new_class), new_class.Name, new_class.Location);
2780 Report.Error (-200, lexer.Location,
2781 "Type parameter constraints only valid if there is a type parameter list");
2788 : /* empty */ { $$ = (int) 0; }
2794 | modifiers modifier
2799 if ((m1 & m2) != 0) {
2800 Location l = lexer.Location;
2801 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2803 $$ = (int) (m1 | m2);
2808 : NEW { $$ = Modifiers.NEW; }
2809 | PUBLIC { $$ = Modifiers.PUBLIC; }
2810 | PROTECTED { $$ = Modifiers.PROTECTED; }
2811 | INTERNAL { $$ = Modifiers.INTERNAL; }
2812 | PRIVATE { $$ = Modifiers.PRIVATE; }
2813 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2814 | SEALED { $$ = Modifiers.SEALED; }
2815 | STATIC { $$ = Modifiers.STATIC; }
2816 | READONLY { $$ = Modifiers.READONLY; }
2817 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2818 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2819 | EXTERN { $$ = Modifiers.EXTERN; }
2820 | VOLATILE { $$ = Modifiers.VOLATILE; }
2821 | UNSAFE { $$ = Modifiers.UNSAFE; }
2825 : /* empty */ { $$ = null; }
2826 | class_base { $$ = $1; }
2830 : COLON type_list { $$ = $2; }
2833 opt_type_parameter_list
2834 : /* empty */ { $$ = null; }
2835 | type_parameter_list { $$ = $1; }
2839 : OP_LT type_parameters OP_GT { $$ = $2; }
2845 // Do some profiling to find the optimal size, for now we
2846 // assume most people will be generic on one type (saves space
2848 ArrayList type_parameters = new ArrayList (1);
2849 type_parameters.Add ($1);
2850 $$ = type_parameters;
2852 | type_parameters COMMA type_parameter {
2853 ArrayList type_parameters = (ArrayList) $1;
2855 type_parameters.Add ($3);
2856 $$ = type_parameters;
2864 opt_type_parameter_constraints_clauses
2865 : /* empty */ { $$ = null; }
2866 | type_parameter_constraints_clauses
2870 type_parameter_constraints_clauses
2871 : type_parameter_constraints_clause
2872 | type_parameter_constraints_clauses type_parameter_constraints_clause
2875 type_parameter_constraint_clause
2876 : WHERE type_parameter COLON type_parameter_constraints {
2877 $$ = new Constraints ((string) $2, (ArrayList) $4);
2881 type_parameter_constraint
2883 // we merge class and interface constraints, the tree resolution
2884 // will split them apart.
2886 | interface_constraints { $$ = $1; }
2889 interface_constraints
2891 ArrayList constraints = new ArrayList (1);
2892 constraints.Add ($1);
2895 | NEW OPEN_PARENS CLOSE_PARENS {
2896 ArrayList constraints = new ArrayList (1);
2897 constraints.Add (true);
2900 | interface_constraints COMMA type {
2901 ArrayList constraints = (ArrayList) $1;
2903 constraints.Add ($3);
2913 // A block is "contained" on the following places:
2915 // property_declaration as part of the accessor body (get/set)
2916 // operator_declaration
2917 // constructor_declaration
2918 // destructor_declaration
2919 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2924 current_block = new Block (current_block, current_local_parameters,
2925 lexer.Location, Location.Null);
2927 opt_statement_list CLOSE_BRACE
2929 while (current_block.Implicit)
2930 current_block = current_block.Parent;
2932 current_block.SetEndLocation (lexer.Location);
2933 current_block = current_block.Parent;
2944 | statement_list statement
2948 : declaration_statement
2950 if ($1 != null && (Block) $1 != current_block){
2951 current_block.AddStatement ((Statement) $1);
2952 current_block = (Block) $1;
2955 | embedded_statement
2957 Statement s = (Statement) $1;
2960 current_block.AddStatement ((Statement) $1);
2968 | expression_statement
2969 | selection_statement
2970 | iteration_statement
2974 | unchecked_statement
2984 $$ = new EmptyStatement ();
2991 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
2993 if (!current_block.AddLabel ((string) $1, labeled)){
2994 Location l = lexer.Location;
2995 Report.Error (140, l, "The label '" + ((string) $1) + "' is a duplicate");
2997 current_block.AddStatement (labeled);
3002 declaration_statement
3003 : local_variable_declaration SEMICOLON
3006 DictionaryEntry de = (DictionaryEntry) $1;
3008 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3012 | local_constant_declaration SEMICOLON
3015 DictionaryEntry de = (DictionaryEntry) $1;
3017 $$ = declare_local_constant ((Expression) de.Key, (VariableDeclaration) de.Value);
3023 * The following is from Rhys' grammar:
3024 * > Types in local variable declarations must be recognized as
3025 * > expressions to prevent reduce/reduce errors in the grammar.
3026 * > The expressions are converted into types during semantic analysis.
3029 : primary_expression opt_rank_specifier
3031 // FIXME: Do something smart here regarding the composition of the type.
3033 // Ok, the above "primary_expression" is there to get rid of
3034 // both reduce/reduce and shift/reduces in the grammar, it should
3035 // really just be "type_name". If you use type_name, a reduce/reduce
3036 // creeps up. If you use qualified_identifier (which is all we need
3037 // really) two shift/reduces appear.
3040 // So the super-trick is that primary_expression
3041 // can only be either a SimpleName or a MemberAccess.
3042 // The MemberAccess case arises when you have a fully qualified type-name like :
3044 // SimpleName is when you have
3047 Expression expr = (Expression) $1;
3048 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3049 Error_ExpectingTypeName (lexer.Location, expr);
3053 // So we extract the string corresponding to the SimpleName
3057 if ((string) $2 == "")
3060 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3063 | builtin_types opt_rank_specifier
3065 if ((string) $2 == "")
3068 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3072 local_variable_pointer_type
3073 : primary_expression STAR
3075 Expression expr = (Expression) $1;
3076 Location l = lexer.Location;
3078 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3079 Error_ExpectingTypeName (l, expr);
3083 $$ = new ComposedCast ((Expression) $1, "*", l);
3085 | builtin_types STAR
3087 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3091 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3093 | local_variable_pointer_type STAR
3095 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3099 local_variable_declaration
3100 : local_variable_type variable_declarators
3103 $$ = new DictionaryEntry ($1, $2);
3107 | local_variable_pointer_type opt_rank_specifier variable_declarators
3112 if ((string) $2 == "")
3113 t = (Expression) $1;
3115 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3116 $$ = new DictionaryEntry (t, $3);
3122 local_constant_declaration
3123 : CONST local_variable_type constant_declarator
3126 $$ = new DictionaryEntry ($2, $3);
3132 expression_statement
3133 : statement_expression SEMICOLON
3140 // We have to do the wrapping here and not in the case above,
3141 // because statement_expression is used for example in for_statement
3143 statement_expression
3144 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3145 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3146 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3147 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3148 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3149 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3150 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3152 Report.Error (1002, lexer.Location, "Expecting `;'");
3157 object_creation_expression
3158 : object_or_delegate_creation_expression
3159 { note ("complain if this is a delegate maybe?"); }
3168 : if_statement_open if_statement_rest
3177 oob_stack.Push (lexer.Location);
3182 : boolean_expression CLOSE_PARENS
3185 Location l = (Location) oob_stack.Pop ();
3187 $$ = new If ((Expression) $1, (Statement) $3, l);
3189 if (RootContext.WarningLevel >= 3){
3190 if ($3 is EmptyStatement)
3191 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3195 | boolean_expression CLOSE_PARENS
3196 embedded_statement ELSE embedded_statement
3198 Location l = (Location) oob_stack.Pop ();
3200 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
3205 : SWITCH OPEN_PARENS
3207 oob_stack.Push (lexer.Location);
3208 switch_stack.Push (current_block);
3210 expression CLOSE_PARENS
3213 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
3214 current_block = (Block) switch_stack.Pop ();
3230 Report.Error (1522, lexer.Location, "Empty switch block");
3238 ArrayList sections = new ArrayList (4);
3243 | switch_sections switch_section
3245 ArrayList sections = (ArrayList) $1;
3255 current_block = current_block.CreateSwitchBlock (lexer.Location);
3259 Block topmost = current_block;
3261 while (topmost.Implicit)
3262 topmost = topmost.Parent;
3263 $$ = new SwitchSection ((ArrayList) $1, topmost);
3270 ArrayList labels = new ArrayList (4);
3275 | switch_labels switch_label
3277 ArrayList labels = (ArrayList) ($1);
3285 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3286 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3289 1523, lexer.Location,
3290 "The keyword case or default must precede code in switch block");
3304 oob_stack.Push (lexer.Location);
3306 boolean_expression CLOSE_PARENS embedded_statement
3308 Location l = (Location) oob_stack.Pop ();
3309 $$ = new While ((Expression) $4, (Statement) $6, l);
3311 if (RootContext.WarningLevel >= 3){
3312 if ($6 is EmptyStatement)
3313 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3319 : DO embedded_statement
3321 oob_stack.Push (lexer.Location);
3323 boolean_expression CLOSE_PARENS SEMICOLON
3325 Location l = (Location) oob_stack.Pop ();
3327 $$ = new Do ((Statement) $2, (Expression) $6, l);
3333 opt_for_initializer SEMICOLON
3335 Block assign_block = new Block (current_block);
3336 current_block = assign_block;
3338 if ($3 is DictionaryEntry){
3339 DictionaryEntry de = (DictionaryEntry) $3;
3341 Expression type = (Expression) de.Key;
3342 ArrayList var_declarators = (ArrayList) de.Value;
3344 foreach (VariableDeclaration decl in var_declarators){
3348 vi = current_block.AddVariable (
3349 type, decl.identifier, current_local_parameters, decl.Location);
3353 Location l = lexer.Location;
3355 if (decl.expression_or_array_initializer is Expression){
3356 expr = (Expression) decl.expression_or_array_initializer;
3357 } else if (decl.expression_or_array_initializer == null) {
3360 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3361 expr = new ArrayCreation (type, "", init, decl.Location);
3364 LocalVariableReference var;
3365 var = new LocalVariableReference (assign_block, decl.identifier, l);
3368 Assign a = new Assign (var, expr, decl.Location);
3370 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3376 oob_stack.Push (lexer.Location);
3378 opt_for_condition SEMICOLON
3379 opt_for_iterator CLOSE_PARENS
3382 Location l = (Location) oob_stack.Pop ();
3384 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3386 if (RootContext.WarningLevel >= 3){
3387 if ($10 is EmptyStatement)
3388 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3391 current_block.AddStatement (f);
3392 while (current_block.Implicit)
3393 current_block = current_block.Parent;
3395 current_block = current_block.Parent;
3400 : /* empty */ { $$ = new EmptyStatement (); }
3405 : local_variable_declaration
3406 | statement_expression_list
3410 : /* empty */ { $$ = null; }
3411 | boolean_expression
3415 : /* empty */ { $$ = new EmptyStatement (); }
3420 : statement_expression_list
3423 statement_expression_list
3424 : statement_expression
3426 // CHANGE: was `null'
3427 Block b = new Block (current_block, Block.Flags.Implicit);
3429 b.AddStatement ((Statement) $1);
3432 | statement_expression_list COMMA statement_expression
3434 Block b = (Block) $1;
3436 b.AddStatement ((Statement) $3);
3442 : FOREACH OPEN_PARENS type IDENTIFIER IN
3444 oob_stack.Push (lexer.Location);
3446 expression CLOSE_PARENS
3448 oob_stack.Push (current_block);
3450 Block foreach_block = new Block (current_block, Block.Flags.Implicit);
3451 LocalVariableReference v = null;
3452 Location l = lexer.Location;
3455 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3459 // Get a writable reference to this read-only variable.
3460 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3462 current_block = foreach_block;
3465 oob_stack.Push (current_block);
3469 Block foreach_block = (Block) oob_stack.Pop ();
3470 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3471 Block prev_block = (Block) oob_stack.Pop ();
3472 Location l = (Location) oob_stack.Pop ();
3474 current_block = prev_block;
3477 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3478 foreach_block.AddStatement (f);
3487 | continue_statement
3497 $$ = new Break (lexer.Location);
3502 : CONTINUE SEMICOLON
3504 $$ = new Continue (lexer.Location);
3509 : GOTO IDENTIFIER SEMICOLON
3511 $$ = new Goto (current_block, (string) $2, lexer.Location);
3513 | GOTO CASE constant_expression SEMICOLON
3515 $$ = new GotoCase ((Expression) $3, lexer.Location);
3517 | GOTO DEFAULT SEMICOLON
3519 $$ = new GotoDefault (lexer.Location);
3524 : RETURN opt_expression SEMICOLON
3526 $$ = new Return ((Expression) $2, lexer.Location);
3531 : THROW opt_expression SEMICOLON
3533 $$ = new Throw ((Expression) $2, lexer.Location);
3538 : YIELD expression SEMICOLON
3540 if (iterator_container == null){
3541 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3544 iterator_container.SetYields ();
3545 $$ = new Yield ((Expression) $2, lexer.Location);
3548 | YIELD BREAK SEMICOLON
3550 if (iterator_container == null){
3551 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3554 iterator_container.SetYields ();
3555 $$ = new YieldBreak (lexer.Location);
3566 : TRY block catch_clauses
3569 ArrayList s = new ArrayList (4);
3571 foreach (Catch cc in (ArrayList) $3) {
3578 // Now s contains the list of specific catch clauses
3579 // and g contains the general one.
3581 $$ = new Try ((Block) $2, s, g, null, lexer.Location);
3583 | TRY block opt_catch_clauses FINALLY block
3586 ArrayList s = new ArrayList (4);
3587 ArrayList catch_list = (ArrayList) $3;
3589 if (catch_list != null){
3590 foreach (Catch cc in catch_list) {
3598 $$ = new Try ((Block) $2, s, g, (Block) $5, lexer.Location);
3602 Report.Error (1524, lexer.Location, "Expected catch or finally");
3607 : /* empty */ { $$ = null; }
3614 ArrayList l = new ArrayList (4);
3619 | catch_clauses catch_clause
3621 ArrayList l = (ArrayList) $1;
3629 : /* empty */ { $$ = null; }
3634 : CATCH opt_catch_args
3636 Expression type = null;
3640 DictionaryEntry cc = (DictionaryEntry) $2;
3641 type = (Expression) cc.Key;
3642 id = (string) cc.Value;
3645 ArrayList one = new ArrayList (4);
3646 Location loc = lexer.Location;
3648 one.Add (new VariableDeclaration (id, null, loc));
3651 current_block = new Block (current_block);
3652 Block b = declare_local_variables (type, one, loc);
3657 Expression type = null;
3661 DictionaryEntry cc = (DictionaryEntry) $2;
3662 type = (Expression) cc.Key;
3663 id = (string) cc.Value;
3667 // FIXME: I can change this for an assignment.
3669 while (current_block != (Block) $1)
3670 current_block = current_block.Parent;
3675 $$ = new Catch (type, id , (Block) $4, lexer.Location);
3680 : /* empty */ { $$ = null; }
3685 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3687 $$ = new DictionaryEntry ($2, $3);
3694 $$ = new Checked ((Block) $2);
3701 $$ = new Unchecked ((Block) $2);
3708 if (!RootContext.Unsafe){
3709 Report.Error (227, lexer.Location,
3710 "Unsafe code can only be used if --unsafe is used");
3713 $$ = new Unsafe ((Block) $3);
3719 type fixed_pointer_declarators
3722 Block assign_block = new Block (current_block, Block.Flags.Implicit);
3723 ArrayList list = (ArrayList) $4;
3724 Expression type = (Expression) $3;
3725 Location l = lexer.Location;
3726 int top = list.Count;
3728 for (int i = 0; i < top; i++){
3729 Pair p = (Pair) list [i];
3732 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3739 current_block.AddStatement (assign_block);
3740 current_block = assign_block;
3741 oob_stack.Push (assign_block);
3746 Location l = (Location) oob_stack.Pop ();
3747 Block assign_block = (Block) oob_stack.Pop ();
3749 ArrayList list = (ArrayList) $4;
3750 int top = list.Count;
3752 $$ = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
3756 fixed_pointer_declarators
3757 : fixed_pointer_declarator {
3758 ArrayList declarators = new ArrayList (4);
3759 declarators.Add ($1);
3762 | fixed_pointer_declarators COMMA fixed_pointer_declarator
3764 ArrayList declarators = (ArrayList) $1;
3765 declarators.Add ($3);
3770 fixed_pointer_declarator
3771 : IDENTIFIER ASSIGN expression
3773 $$ = new Pair ($1, $3);
3778 : LOCK OPEN_PARENS expression CLOSE_PARENS
3784 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
3789 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
3791 Block assign_block = new Block (current_block);
3792 current_block = assign_block;
3794 oob_stack.Push (lexer.Location);
3796 if ($3 is DictionaryEntry){
3797 DictionaryEntry de = (DictionaryEntry) $3;
3798 Location l = lexer.Location;
3800 Expression type = (Expression) de.Key;
3801 ArrayList var_declarators = (ArrayList) de.Value;
3803 ArrayList vars = new ArrayList (4);
3805 foreach (VariableDeclaration decl in var_declarators){
3807 LocalInfo vi = current_block.AddVariable (
3808 type, decl.identifier,
3809 current_local_parameters, decl.Location);
3815 if (decl.expression_or_array_initializer is Expression){
3816 expr = (Expression) decl.expression_or_array_initializer;
3818 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3820 expr = new ArrayCreation (type, "", init, decl.Location);
3823 LocalVariableReference var;
3825 // Get a writable reference to this read-only variable.
3826 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
3828 // This is so that it is not a warning on using variables
3831 vars.Add (new DictionaryEntry (var, expr));
3833 // Assign a = new Assign (var, expr, decl.Location);
3834 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3836 $3 = new DictionaryEntry (type, vars);
3841 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
3842 current_block.AddStatement (u);
3843 while (current_block.Implicit)
3844 current_block = current_block.Parent;
3846 current_block = current_block.Parent;
3850 resource_acquisition
3851 : local_variable_declaration
3858 // A class used to pass around variable declarations and constants
3860 public class VariableDeclaration {
3861 public string identifier;
3862 public object expression_or_array_initializer;
3863 public Location Location;
3864 public Attributes OptAttributes;
3866 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
3868 this.identifier = id;
3869 this.expression_or_array_initializer = eoai;
3871 this.OptAttributes = opt_attrs;
3874 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
3880 // A class used to hold info about an indexer declarator
3883 public class IndexerDeclaration {
3884 public Expression type;
3885 public string interface_type;
3886 public Parameters param_list;
3888 public IndexerDeclaration (Expression type, string interface_type, Parameters param_list)
3891 this.interface_type = interface_type;
3892 this.param_list = param_list;
3897 // A class used to hold info about an operator declarator
3900 public class OperatorDeclaration {
3901 public Operator.OpType optype;
3902 public Expression ret_type, arg1type, arg2type;
3903 public string arg1name, arg2name;
3904 public Location location;
3906 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
3907 Expression arg1type, string arg1name,
3908 Expression arg2type, string arg2name, Location location)
3911 this.ret_type = ret_type;
3912 this.arg1type = arg1type;
3913 this.arg1name = arg1name;
3914 this.arg2type = arg2type;
3915 this.arg2name = arg2name;
3916 this.location = location;
3921 void Error_ExpectingTypeName (Location l, Expression expr)
3923 if (expr is Invocation){
3924 Report.Error (1002, l, "; expected");
3926 Report.Error (-1, l, "Invalid Type definition");
3931 // Given the @class_name name, it creates a fully qualified name
3932 // based on the containing declaration space
3935 MakeName (string class_name)
3937 string ns = current_namespace.Name;
3938 string container_name = current_container.Name;
3940 if (container_name == ""){
3942 return ns + "." + class_name;
3946 return container_name + "." + class_name;
3950 // Used to report back to the user the result of a declaration
3951 // in the current declaration space
3954 CheckDef (AdditionResult result, string name, Location l)
3956 if (result == AdditionResult.Success)
3960 case AdditionResult.NameExists:
3961 Report.Error (102, l, "The container `" + current_container.Name +
3962 "' already contains a definition for `"+
3968 // This is handled only for static Constructors, because
3969 // in reality we handle these by the semantic analysis later
3971 case AdditionResult.MethodExists:
3973 111, l, "Class `"+current_container.Name+
3974 "' already defines a member called '" +
3975 name + "' with the same parameter types (more than one default constructor)");
3978 case AdditionResult.EnclosingClash:
3979 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
3982 case AdditionResult.NotAConstructor:
3983 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
3986 case AdditionResult.Error:
3987 // Error has already been reported.
3993 CheckDef (bool result, string name, Location l)
3997 CheckDef (AdditionResult.NameExists, name, l);
4000 Expression DecomposeQI (string name, Location loc)
4004 if (name.IndexOf ('.') == -1){
4005 return new SimpleName (name, loc);
4007 int pos = name.LastIndexOf (".");
4008 string left = name.Substring (0, pos);
4009 string right = name.Substring (pos + 1);
4011 o = DecomposeQI (left, loc);
4013 return new MemberAccess (o, right, loc);
4018 // This method is used to get at the complete string representation of
4019 // a fully-qualified type name, hiding inside a MemberAccess ;-)
4020 // This is necessary because local_variable_type admits primary_expression
4021 // as the type of the variable. So we do some extra checking
4024 string GetQualifiedIdentifier (Expression expr)
4026 if (expr is SimpleName)
4027 return ((SimpleName)expr).Name;
4028 else if (expr is MemberAccess)
4029 return GetQualifiedIdentifier (((MemberAccess)expr).Expr) + "." + ((MemberAccess) expr).Identifier;
4031 throw new Exception ("Expr has to be either SimpleName or MemberAccess! (" + expr + ")");
4036 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4038 Block implicit_block;
4039 ArrayList inits = null;
4042 // We use the `Used' property to check whether statements
4043 // have been added to the current block. If so, we need
4044 // to create another block to contain the new declaration
4045 // otherwise, as an optimization, we use the same block to
4046 // add the declaration.
4048 // FIXME: A further optimization is to check if the statements
4049 // that were added were added as part of the initialization
4050 // below. In which case, no other statements have been executed
4051 // and we might be able to reduce the number of blocks for
4052 // situations like this:
4054 // int j = 1; int k = j + 1;
4056 if (current_block.Used) {
4057 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4058 implicit_block.AddChildVariableNames (current_block);
4060 implicit_block = current_block;
4062 foreach (VariableDeclaration decl in variable_declarators){
4064 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4065 if (decl.expression_or_array_initializer != null){
4067 inits = new ArrayList (4);
4074 return implicit_block;
4076 foreach (VariableDeclaration decl in inits){
4080 if (decl.expression_or_array_initializer is Expression){
4081 expr = (Expression) decl.expression_or_array_initializer;
4084 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4086 expr = new ArrayCreation (type, "", init, decl.Location);
4089 LocalVariableReference var;
4090 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4092 assign = new Assign (var, expr, decl.Location);
4094 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4097 return implicit_block;
4100 Block declare_local_constant (Expression type, VariableDeclaration decl)
4102 Block implicit_block;
4104 if (current_block.Used)
4105 implicit_block = new Block (current_block, Block.Flags.Implicit);
4107 implicit_block = current_block;
4109 if (!(implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4110 current_local_parameters, decl.Location))){
4113 return implicit_block;
4116 void CheckAttributeTarget (string a)
4120 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
4124 Location l = lexer.Location;
4125 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4131 void CheckUnaryOperator (Operator.OpType op)
4135 case Operator.OpType.LogicalNot:
4136 case Operator.OpType.OnesComplement:
4137 case Operator.OpType.Increment:
4138 case Operator.OpType.Decrement:
4139 case Operator.OpType.True:
4140 case Operator.OpType.False:
4141 case Operator.OpType.Addition:
4142 case Operator.OpType.Subtraction:
4147 Location l = lexer.Location;
4148 Report.Error (1019, l, "Overloadable unary operator expected");
4154 void CheckBinaryOperator (Operator.OpType op)
4158 case Operator.OpType.Addition:
4159 case Operator.OpType.Subtraction:
4160 case Operator.OpType.Multiply:
4161 case Operator.OpType.Division:
4162 case Operator.OpType.Modulus:
4163 case Operator.OpType.BitwiseAnd:
4164 case Operator.OpType.BitwiseOr:
4165 case Operator.OpType.ExclusiveOr:
4166 case Operator.OpType.LeftShift:
4167 case Operator.OpType.RightShift:
4168 case Operator.OpType.Equality:
4169 case Operator.OpType.Inequality:
4170 case Operator.OpType.GreaterThan:
4171 case Operator.OpType.LessThan:
4172 case Operator.OpType.GreaterThanOrEqual:
4173 case Operator.OpType.LessThanOrEqual:
4177 Location l = lexer.Location;
4178 Report.Error (1020, l, "Overloadable binary operator expected");
4184 void syntax_error (Location l, string msg)
4186 Report.Error (1003, l, "Syntax error, " + msg);
4189 void output (string s)
4191 Console.WriteLine (s);
4194 void note (string s)
4196 // Used to put annotations
4201 public Tokenizer Lexer {
4207 public CSharpParser (StreamReader reader, SourceFile file, ArrayList defines)
4209 current_namespace = new NamespaceEntry (null, file, null);
4210 this.name = file.Name;
4212 current_container = RootContext.Tree.Types;
4213 current_container.Namespace = current_namespace;
4214 oob_stack = new Stack ();
4215 switch_stack = new Stack ();
4217 lexer = new Tokenizer (reader, file, defines);
4220 public void parse ()
4223 if (yacc_verbose_flag)
4224 yyparse (lexer, new yydebug.yyDebugSimple ());
4227 Tokenizer tokenizer = lexer as Tokenizer;
4228 tokenizer.cleanup ();
4229 } catch (Exception e){
4230 // Please do not remove this, it is used during debugging
4233 Report.Error (-25, lexer.Location, ": Parsing error ");
4234 Console.WriteLine (e);