3 // cs-parser.jay: The Parser for the C# compiler
5 // Authors: Miguel de Icaza (miguel@gnu.org)
6 // Ravi Pratap (ravi@ximian.com)
8 // Licensed under the terms of the GNU GPL
10 // (C) 2001 Ximian, Inc (http://www.ximian.com)
13 // (1) Figure out why error productions dont work. `type-declaration' is a
14 // great spot to put an `error' because you can reproduce it with this input:
17 // Possible optimization:
18 // Run memory profiler with parsing only, and consider dropping
19 // arraylists where not needed. Some pieces can use linked lists.
27 using System.Collections;
33 public class CSharpParser : GenericParser {
34 Namespace current_namespace;
35 TypeContainer current_container;
38 // Current block is used to add statements as we find
45 // Current interface is used by the various declaration
46 // productions in the interface declaration to "add"
47 // the interfaces as we find them.
49 Interface current_interface;
52 // This is used by the unary_expression code to resolve
53 // a name against a parameter.
55 Parameters current_local_parameters;
58 // Using during property parsing to describe the implicit
59 // value parameter that is passed to the "set" and "get"accesor
60 // methods (properties and indexers).
62 Expression implicit_value_parameter_type;
63 Parameters indexer_parameters;
66 // Used to determine if we are parsing the get/set pair
67 // of an indexer or a property
72 // An out-of-band stack.
88 %token NONE /* This token is never returned by our lexer */
89 %token ERROR // This is used not by the parser, but by the tokenizer.
93 *These are the C# keywords
176 /* C# keywords which are not really keywords */
180 /* C# single character operators/punctuation. */
181 %token OPEN_BRACE "{"
182 %token CLOSE_BRACE "}"
183 %token OPEN_BRACKET "["
184 %token CLOSE_BRACKET "]"
185 %token OPEN_PARENS "("
186 %token CLOSE_PARENS ")"
199 %token BITWISE_AND "&"
200 %token BITWISE_OR "|"
207 /* C# multi-character operators. */
210 %token OP_SHIFT_LEFT "<<"
211 %token OP_SHIFT_RIGHT ">>"
218 %token OP_MULT_ASSIGN "*="
219 %token OP_DIV_ASSIGN "/="
220 %token OP_MOD_ASSIGN "%="
221 %token OP_ADD_ASSIGN "+="
222 %token OP_SUB_ASSIGN "-="
223 %token OP_SHIFT_LEFT_ASSIGN "<<="
224 %token OP_SHIFT_RIGHT_ASSIGN ">>="
225 %token OP_AND_ASSIGN "&="
226 %token OP_XOR_ASSIGN "^="
227 %token OP_OR_ASSIGN "|="
231 %token LITERAL_INTEGER "int literal"
232 %token LITERAL_FLOAT "float literal"
233 %token LITERAL_DOUBLE "double literal"
234 %token LITERAL_DECIMAL "decimal literal"
235 %token LITERAL_CHARACTER "character literal"
236 %token LITERAL_STRING "string literal"
240 /* Add precedence rules to solve dangling else s/r conflict */
249 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
251 %left STAR DIV PERCENT
252 %right BANG CARRET UMINUS
253 %nonassoc OP_INC OP_DEC
255 %left OPEN_BRACKET OPEN_BRACE
259 %start compilation_unit
263 : outer_declarations opt_EOF
264 | outer_declarations attribute_sections opt_EOF
265 | attribute_sections opt_EOF
266 | opt_EOF /* allow empty files */
276 | outer_declarations outer_declaration
281 | namespace_member_declaration
286 | using_directives using_directive
290 : using_alias_directive
291 | using_namespace_directive
294 using_alias_directive
295 : USING IDENTIFIER ASSIGN
296 namespace_or_type_name SEMICOLON
298 current_namespace.UsingAlias ((string) $2, (string) $4, lexer.Location);
302 using_namespace_directive
303 : USING namespace_name SEMICOLON
305 current_namespace.Using ((string) $2, lexer.Location);
310 // Strictly speaking, namespaces don't have attributes but
311 // we parse global attributes along with namespace declarations and then
314 namespace_declaration
315 : opt_attributes NAMESPACE qualified_identifier
317 Attributes attrs = (Attributes) $1;
320 foreach (AttributeSection asec in attrs.AttributeSections)
321 if (asec.Target == "assembly")
322 RootContext.AddGlobalAttributeSection (current_container, asec);
325 current_namespace = RootContext.Tree.RecordNamespace (current_namespace, file, (string) $3);
327 namespace_body opt_semicolon
329 current_namespace = current_namespace.Parent;
345 | qualified_identifier DOT IDENTIFIER {
346 $$ = (($1).ToString ()) + "." + ($3.ToString ()); }
351 : namespace_or_type_name
357 opt_namespace_member_declarations
368 opt_namespace_member_declarations
370 | namespace_member_declarations
373 namespace_member_declarations
374 : namespace_member_declaration
375 | namespace_member_declarations namespace_member_declaration
378 namespace_member_declaration
385 Class c = (Class) $1;
386 mod_flags = c.ModFlags;
388 } else if ($1 is Struct){
389 Struct s = (Struct) $1;
390 mod_flags = s.ModFlags;
395 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
397 1527, lexer.Location,
398 "Namespace elements cant be explicitly " +
399 "declared private or protected in `" + name + "'");
401 current_namespace.DeclarationFound = true;
403 | namespace_declaration
409 | interface_declaration
411 | delegate_declaration
413 // Enable this when we have handled all errors, because this acts as a generic fallback
416 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
426 | attribute_sections { $$ = $1; }
432 AttributeSection sect = (AttributeSection) $1;
434 if (sect.Target == "assembly")
435 RootContext.AddGlobalAttributeSection (current_container, sect);
438 $$ = new Attributes ((AttributeSection) $1);
440 | attribute_sections attribute_section
442 Attributes attrs = null;
443 AttributeSection sect = (AttributeSection) $2;
445 if (sect.Target == "assembly")
446 RootContext.AddGlobalAttributeSection (current_container, sect);
449 attrs = (Attributes) $1;
450 attrs.AddAttributeSection (sect);
458 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
460 string target = null;
463 target = (string) $2;
465 $$ = new AttributeSection (target, (ArrayList) $3);
467 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
469 $$ = new AttributeSection (null, (ArrayList) $2);
473 attribute_target_specifier
474 : attribute_target COLON
483 CheckAttributeTarget ((string) $1);
486 | EVENT { $$ = "event"; }
487 | RETURN { $$ = "return"; }
493 ArrayList attrs = new ArrayList ();
499 | attribute_list COMMA attribute
501 ArrayList attrs = (ArrayList) $1;
513 opt_attribute_arguments
515 $$ = new Attribute ((string) $1, (ArrayList) $3, (Location) $2);
520 : type_name { /* reserved attribute name or identifier: 17.4 */ }
523 opt_attribute_arguments
524 : /* empty */ { $$ = null; }
525 | OPEN_PARENS attribute_arguments CLOSE_PARENS
533 : opt_positional_argument_list
538 ArrayList args = new ArrayList ();
544 | positional_argument_list COMMA named_argument_list
546 ArrayList args = new ArrayList ();
552 | named_argument_list
554 ArrayList args = new ArrayList ();
563 opt_positional_argument_list
564 : /* empty */ { $$ = null; }
565 | positional_argument_list
568 positional_argument_list
571 ArrayList args = new ArrayList ();
572 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
576 | positional_argument_list COMMA expression
578 ArrayList args = (ArrayList) $1;
579 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
588 ArrayList args = new ArrayList ();
593 | named_argument_list COMMA named_argument
595 ArrayList args = (ArrayList) $1;
603 : IDENTIFIER ASSIGN expression
605 $$ = new DictionaryEntry (
607 new Argument ((Expression) $3, Argument.AType.Expression));
613 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
616 opt_class_member_declarations
618 | class_member_declarations
621 class_member_declarations
622 : class_member_declaration
623 | class_member_declarations
624 class_member_declaration
627 class_member_declaration
628 : constant_declaration // done
629 | field_declaration // done
630 | method_declaration // done
631 | property_declaration // done
632 | event_declaration // done
633 | indexer_declaration // done
634 | operator_declaration // done
635 | constructor_declaration // done
636 | destructor_declaration // done
646 string full_struct_name = MakeName ((string) $4);
648 new_struct = new Struct (current_container, full_struct_name, (int) $2,
649 (Attributes) $1, lexer.Location);
650 current_container = new_struct;
651 current_container.Namespace = current_namespace;
652 RootContext.Tree.RecordDecl (full_struct_name, new_struct);
658 Struct new_struct = (Struct) current_container;
661 new_struct.Bases = (ArrayList) $6;
663 current_container = current_container.Parent;
664 CheckDef (current_container.AddStruct (new_struct), new_struct.Name, new_struct.Location);
670 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
673 opt_struct_member_declarations
675 | struct_member_declarations
678 struct_member_declarations
679 : struct_member_declaration
680 | struct_member_declarations struct_member_declaration
683 struct_member_declaration
684 : constant_declaration
687 | property_declaration
689 | indexer_declaration
690 | operator_declaration
691 | constructor_declaration
695 * This is only included so we can flag error 575:
696 * destructors only allowed on class types
698 | destructor_declaration
709 foreach (VariableDeclaration constant in (ArrayList) $5){
710 Location l = constant.Location;
712 Const c = new Const (
713 (Expression) $4, (string) constant.identifier,
714 (Expression) constant.expression_or_array_initializer, (int) $2,
717 CheckDef (current_container.AddConstant (c), c.Name, l);
723 : constant_declarator
725 ArrayList constants = new ArrayList ();
729 | constant_declarators COMMA constant_declarator
731 ArrayList constants = (ArrayList) $1;
738 : IDENTIFIER ASSIGN constant_expression
740 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
751 Expression type = (Expression) $3;
754 foreach (VariableDeclaration var in (ArrayList) $4){
755 Location l = var.Location;
757 Field field = new Field (type, mod, var.identifier,
758 var.expression_or_array_initializer,
761 CheckDef (current_container.AddField (field), field.Name, l);
767 : variable_declarator
769 ArrayList decl = new ArrayList ();
773 | variable_declarators COMMA variable_declarator
775 ArrayList decls = (ArrayList) $1;
782 : IDENTIFIER ASSIGN variable_initializer
784 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
788 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
801 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
803 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
811 Method method = (Method) $1;
812 Block b = (Block) $2;
813 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
816 if ((method.ModFlags & extern_abstract) == 0){
818 501, lexer.Location, current_container.MakeName (method.Name) +
819 "must declare a body because it is not marked abstract or extern");
822 if ((method.ModFlags & Modifiers.EXTERN) != 0){
824 179, lexer.Location, current_container.MakeName (method.Name) +
825 " is declared extern, but has a body");
829 method.Block = (Block) $2;
830 CheckDef (current_container.AddMethod (method), method.Name, method.Location);
832 current_local_parameters = null;
846 1585, lexer.Location, "Member modifier `" +
847 Modifiers.Name (i) + "' must precede member type and name");
860 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
862 Method method = new Method ((Expression) $3, (int) $2, (string) $4,
863 (Parameters) $6, (Attributes) $1, lexer.Location);
865 current_local_parameters = (Parameters) $6;
873 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
875 Method method = new Method (TypeManager.system_void_expr, (int) $2, (string) $4,
876 (Parameters) $6, (Attributes) $1, lexer.Location);
878 current_local_parameters = (Parameters) $6;
885 | SEMICOLON { $$ = null; }
888 opt_formal_parameter_list
889 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
890 | formal_parameter_list
893 formal_parameter_list
896 ArrayList pars_list = (ArrayList) $1;
898 Parameter [] pars = new Parameter [pars_list.Count];
899 pars_list.CopyTo (pars);
901 $$ = new Parameters (pars, null, lexer.Location);
903 | fixed_parameters COMMA parameter_array
905 ArrayList pars_list = (ArrayList) $1;
907 Parameter [] pars = new Parameter [pars_list.Count];
908 pars_list.CopyTo (pars);
910 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
914 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
921 ArrayList pars = new ArrayList ();
926 | fixed_parameters COMMA fixed_parameter
928 ArrayList pars = (ArrayList) $1;
937 opt_parameter_modifier
941 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
945 opt_parameter_modifier
946 : /* empty */ { $$ = Parameter.Modifier.NONE; }
951 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
952 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
956 : opt_attributes PARAMS type IDENTIFIER
958 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
959 note ("type must be a single-dimension array type");
964 : qualified_identifier
973 implicit_value_parameter_type = (Expression) $3;
975 lexer.PropertyParsing = true;
979 accessor_declarations
981 lexer.PropertyParsing = false;
986 Pair pair = (Pair) $7;
987 Accessor get_block = (Accessor) pair.First;
988 Accessor set_block = (Accessor) pair.Second;
990 Location loc = (Location) $6;
991 prop = new Property ((Expression) $3, (string) $4, (int) $2, get_block, set_block,
992 (Attributes) $1, loc);
994 CheckDef (current_container.AddProperty (prop), prop.Name, loc);
995 implicit_value_parameter_type = null;
999 accessor_declarations
1000 : get_accessor_declaration opt_set_accessor_declaration
1002 $$ = new Pair ($1, $2);
1004 | set_accessor_declaration opt_get_accessor_declaration
1006 $$ = new Pair ($2, $1);
1010 opt_get_accessor_declaration
1011 : /* empty */ { $$ = null; }
1012 | get_accessor_declaration
1015 opt_set_accessor_declaration
1016 : /* empty */ { $$ = null; }
1017 | set_accessor_declaration
1020 get_accessor_declaration
1021 : opt_attributes GET
1023 // If this is not the case, then current_local_parameters has already
1024 // been set in indexer_declaration
1025 if (parsing_indexer == false)
1026 current_local_parameters = null;
1028 current_local_parameters = indexer_parameters;
1029 lexer.PropertyParsing = false;
1033 $$ = new Accessor ((Block) $4, (Attributes) $1);
1034 current_local_parameters = null;
1035 lexer.PropertyParsing = true;
1039 set_accessor_declaration
1040 : opt_attributes SET
1043 Parameter implicit_value_parameter = new Parameter (
1044 implicit_value_parameter_type, "value",
1045 Parameter.Modifier.NONE, null);
1047 if (parsing_indexer == false) {
1048 args = new Parameter [1];
1049 args [0] = implicit_value_parameter;
1050 current_local_parameters = new Parameters (args, null, lexer.Location);
1052 Parameter [] fpars = indexer_parameters.FixedParameters;
1055 int count = fpars.Length;
1057 args = new Parameter [count + 1];
1058 fpars.CopyTo (args, 0);
1059 args [count] = implicit_value_parameter;
1062 current_local_parameters = new Parameters (
1063 args, indexer_parameters.ArrayParameter, lexer.Location);
1066 lexer.PropertyParsing = false;
1070 $$ = new Accessor ((Block) $4, (Attributes) $1);
1071 current_local_parameters = null;
1072 lexer.PropertyParsing = true;
1078 | SEMICOLON { $$ = null; }
1081 interface_declaration
1084 INTERFACE IDENTIFIER
1086 Interface new_interface;
1087 string full_interface_name = MakeName ((string) $4);
1089 new_interface = new Interface (current_container, full_interface_name, (int) $2,
1090 (Attributes) $1, lexer.Location);
1091 if (current_interface != null) {
1092 Location l = lexer.Location;
1093 Report.Error (-2, l, "Internal compiler error: interface inside interface");
1095 current_interface = new_interface;
1096 new_interface.Namespace = current_namespace;
1097 RootContext.Tree.RecordDecl (full_interface_name, new_interface);
1100 interface_body opt_semicolon
1102 Interface new_interface = (Interface) current_interface;
1105 new_interface.Bases = (ArrayList) $6;
1107 current_interface = null;
1108 CheckDef (current_container.AddInterface (new_interface),
1109 new_interface.Name, new_interface.Location);
1114 : /* empty */ { $$ = null; }
1119 : COLON interface_type_list { $$ = $2; }
1125 ArrayList interfaces = new ArrayList ();
1127 interfaces.Add ($1);
1130 | interface_type_list COMMA interface_type
1132 ArrayList interfaces = (ArrayList) $1;
1133 interfaces.Add ($3);
1140 opt_interface_member_declarations
1144 opt_interface_member_declarations
1146 | interface_member_declarations
1149 interface_member_declarations
1150 : interface_member_declaration
1151 | interface_member_declarations interface_member_declaration
1154 interface_member_declaration
1155 : interface_method_declaration
1157 InterfaceMethod m = (InterfaceMethod) $1;
1159 CheckDef (current_interface.AddMethod (m), m.Name, m.Location);
1161 | interface_property_declaration
1163 InterfaceProperty p = (InterfaceProperty) $1;
1165 CheckDef (current_interface.AddProperty (p), p.Name, p.Location);
1167 | interface_event_declaration
1169 InterfaceEvent e = (InterfaceEvent) $1;
1171 CheckDef (current_interface.AddEvent (e), e.Name, lexer.Location);
1173 | interface_indexer_declaration
1175 InterfaceIndexer i = (InterfaceIndexer) $1;
1177 CheckDef (current_interface.AddIndexer (i), "indexer", i.Location);
1182 : /* empty */ { $$ = false; }
1183 | NEW { $$ = true; }
1186 interface_method_declaration
1187 : opt_attributes opt_new type IDENTIFIER
1188 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1191 $$ = new InterfaceMethod ((Expression) $3, (string) $4, (bool) $2,
1192 (Parameters) $6, (Attributes) $1, lexer.Location);
1194 | opt_attributes opt_new VOID IDENTIFIER
1195 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1198 $$ = new InterfaceMethod (
1199 TypeManager.system_void_expr, (string) $4, (bool) $2, (Parameters) $6,
1200 (Attributes) $1, lexer.Location);
1204 interface_property_declaration
1209 { lexer.PropertyParsing = true; }
1211 { lexer.PropertyParsing = false; }
1216 $$ = new InterfaceProperty ((Expression) $3, (string) $4, (bool) $2,
1217 (gs & 1) == 1, (gs & 2) == 2, (Attributes) $1,
1223 : opt_attributes GET SEMICOLON { $$ = 1; }
1224 | opt_attributes SET SEMICOLON { $$ = 2; }
1225 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1227 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1231 interface_event_declaration
1232 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1234 $$ = new InterfaceEvent ((Expression) $4, (string) $5, (bool) $2, (Attributes) $1,
1239 interface_indexer_declaration
1240 : opt_attributes opt_new type THIS
1241 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1243 { lexer.PropertyParsing = true; }
1245 { lexer.PropertyParsing = false; }
1248 int a_flags = (int) $10;
1250 bool do_get = (a_flags & 1) == 1;
1251 bool do_set = (a_flags & 2) == 2;
1253 $$ = new InterfaceIndexer ((Expression) $3, (Parameters) $6, do_get, do_set,
1254 (bool) $2, (Attributes) $1, lexer.Location);
1258 operator_declaration
1259 : opt_attributes opt_modifiers operator_declarator operator_body
1261 OperatorDeclaration decl = (OperatorDeclaration) $3;
1263 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
1264 decl.arg2type, decl.arg2name, (Block) $4, (Attributes) $1, decl.location);
1266 // Note again, checking is done in semantic analysis
1267 current_container.AddOperator (op);
1269 current_local_parameters = null;
1275 | SEMICOLON { $$ = null; }
1278 : type OPERATOR overloadable_operator
1279 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1281 Operator.OpType op = (Operator.OpType) $3;
1282 CheckUnaryOperator (op);
1284 if (op == Operator.OpType.Addition)
1285 op = Operator.OpType.UnaryPlus;
1287 if (op == Operator.OpType.Subtraction)
1288 op = Operator.OpType.UnaryNegation;
1290 Parameter [] pars = new Parameter [1];
1292 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1294 current_local_parameters = new Parameters (pars, null, lexer.Location);
1296 $$ = new OperatorDeclaration (op, (Expression) $1, (Expression) $5, (string) $6,
1297 null, null, lexer.Location);
1299 | type OPERATOR overloadable_operator
1301 type IDENTIFIER COMMA
1305 CheckBinaryOperator ((Operator.OpType) $3);
1307 Parameter [] pars = new Parameter [2];
1309 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1310 pars [1] = new Parameter ((Expression) $8, (string) $9, Parameter.Modifier.NONE, null);
1312 current_local_parameters = new Parameters (pars, null, lexer.Location);
1314 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1315 (Expression) $5, (string) $6,
1316 (Expression) $8, (string) $9, lexer.Location);
1318 | conversion_operator_declarator
1321 overloadable_operator
1323 : BANG { $$ = Operator.OpType.LogicalNot; }
1324 | TILDE { $$ = Operator.OpType.OnesComplement; }
1325 | OP_INC { $$ = Operator.OpType.Increment; }
1326 | OP_DEC { $$ = Operator.OpType.Decrement; }
1327 | TRUE { $$ = Operator.OpType.True; }
1328 | FALSE { $$ = Operator.OpType.False; }
1329 // Unary and binary:
1330 | PLUS { $$ = Operator.OpType.Addition; }
1331 | MINUS { $$ = Operator.OpType.Subtraction; }
1333 | STAR { $$ = Operator.OpType.Multiply; }
1334 | DIV { $$ = Operator.OpType.Division; }
1335 | PERCENT { $$ = Operator.OpType.Modulus; }
1336 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1337 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1338 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1339 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1340 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1341 | OP_EQ { $$ = Operator.OpType.Equality; }
1342 | OP_NE { $$ = Operator.OpType.Inequality; }
1343 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1344 | OP_LT { $$ = Operator.OpType.LessThan; }
1345 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1346 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1349 conversion_operator_declarator
1350 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1352 Parameter [] pars = new Parameter [1];
1354 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1356 current_local_parameters = new Parameters (pars, null, lexer.Location);
1358 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1359 null, null, lexer.Location);
1361 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1363 Parameter [] pars = new Parameter [1];
1365 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1367 current_local_parameters = new Parameters (pars, null, lexer.Location);
1369 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1370 null, null, lexer.Location);
1374 syntax_error (lexer.Location, "'operator' expected");
1378 syntax_error (lexer.Location, "'operator' expected");
1382 constructor_declaration
1385 constructor_declarator
1388 Constructor c = (Constructor) $3;
1389 c.Block = (Block) $4;
1390 c.OptAttributes = (Attributes) $1;
1391 c.ModFlags = (int) $2;
1393 if (c.Name == current_container.Basename){
1394 if ((c.ModFlags & Modifiers.STATIC) != 0){
1395 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1397 515, c.Location, String.Format (
1398 "`{0}.{1}': static constructor can not have access modifiers",
1399 c.Name, current_container.Name));
1402 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1404 if (c.Initializer != null){
1407 "Static constructors can not have an explicit this or base " +
1408 "constructor invocations");
1411 if (!c.Parameters.Empty){
1413 132, c.Location, "Static constructors should not have parameters");
1416 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1419 // We let another layer check the validity of the constructor.
1420 Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1423 CheckDef (current_container.AddConstructor (c), c.Name, c.Location);
1425 current_local_parameters = null;
1429 constructor_declarator
1431 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1433 oob_stack.Push (lexer.Location);
1435 current_local_parameters = (Parameters) $3;
1437 opt_constructor_initializer
1439 Location l = (Location) oob_stack.Pop ();
1440 $$ = new Constructor ((string) $1, (Parameters) $3, (ConstructorInitializer) $6, l);
1446 | SEMICOLON { $$ = null; }
1449 opt_constructor_initializer
1450 : /* empty */ { $$ = null; }
1451 | constructor_initializer
1454 constructor_initializer
1455 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1457 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1459 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1461 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1465 destructor_declaration
1466 : opt_attributes TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1468 if ((string) $3 != current_container.Basename){
1469 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1470 } else if (!(current_container is Class)){
1471 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1473 Location l = lexer.Location;
1476 if (!RootContext.StdLib && current_container.Name == "System.Object")
1477 m = Modifiers.PROTECTED | Modifiers.VIRTUAL;
1479 m = Modifiers.PROTECTED | Modifiers.OVERRIDE;
1481 Method d = new Method (
1482 TypeManager.system_void_expr, m, "Finalize",
1483 new Parameters (null, null, l), (Attributes) $1, l);
1485 d.Block = (Block) $6;
1486 CheckDef (current_container.AddMethod (d), d.Name, d.Location);
1494 EVENT type variable_declarators SEMICOLON
1496 foreach (VariableDeclaration var in (ArrayList) $5) {
1498 Event e = new Event ((Expression) $4, var.identifier,
1499 var.expression_or_array_initializer,
1500 (int) $2, null, null, (Attributes) $1, lexer.Location);
1502 CheckDef (current_container.AddEvent (e), e.Name, e.Location);
1508 EVENT type member_name
1511 implicit_value_parameter_type = (Expression) $4;
1512 lexer.EventParsing = true;
1513 oob_stack.Push (lexer.Location);
1515 event_accessor_declarations
1517 lexer.EventParsing = false;
1521 Location loc = (Location) oob_stack.Pop ();
1523 Pair pair = (Pair) $8;
1524 Accessor add_accessor = null;
1525 Accessor rem_accessor = null;
1527 if (pair.First != null)
1528 add_accessor = (Accessor) pair.First;
1529 if (pair.Second != null)
1530 rem_accessor = (Accessor) pair.Second;
1532 Event e = new Event ((Expression) $4, (string) $5, null, (int) $2, add_accessor, rem_accessor,
1533 (Attributes) $1, loc);
1535 CheckDef (current_container.AddEvent (e), e.Name, loc);
1536 implicit_value_parameter_type = null;
1540 event_accessor_declarations
1541 : add_accessor_declaration remove_accessor_declaration
1543 $$ = new Pair ($1, $2);
1545 | remove_accessor_declaration add_accessor_declaration
1547 $$ = new Pair ($2, $1);
1551 add_accessor_declaration
1552 : opt_attributes ADD
1554 Parameter [] args = new Parameter [1];
1555 Parameter implicit_value_parameter = new Parameter (
1556 implicit_value_parameter_type, "value",
1557 Parameter.Modifier.NONE, null);
1559 args [0] = implicit_value_parameter;
1561 current_local_parameters = new Parameters (args, null, lexer.Location);
1562 lexer.EventParsing = false;
1566 $$ = new Accessor ((Block) $4, (Attributes) $1);
1567 lexer.EventParsing = true;
1571 remove_accessor_declaration
1572 : opt_attributes REMOVE
1574 Parameter [] args = new Parameter [1];
1575 Parameter implicit_value_parameter = new Parameter (
1576 implicit_value_parameter_type, "value",
1577 Parameter.Modifier.NONE, null);
1579 args [0] = implicit_value_parameter;
1581 current_local_parameters = new Parameters (args, null, lexer.Location);
1582 lexer.EventParsing = false;
1586 $$ = new Accessor ((Block) $4, (Attributes) $1);
1587 lexer.EventParsing = true;
1592 : opt_attributes opt_modifiers indexer_declarator
1595 IndexerDeclaration decl = (IndexerDeclaration) $3;
1597 implicit_value_parameter_type = decl.type;
1599 lexer.PropertyParsing = true;
1600 parsing_indexer = true;
1602 indexer_parameters = decl.param_list;
1603 oob_stack.Push (lexer.Location);
1605 accessor_declarations
1607 lexer.PropertyParsing = false;
1608 parsing_indexer = false;
1612 // The signature is computed from the signature of the indexer. Look
1613 // at section 3.6 on the spec
1614 Location loc = (Location) oob_stack.Pop ();
1616 IndexerDeclaration decl = (IndexerDeclaration) $3;
1617 Pair pair = (Pair) $6;
1618 Accessor get_block = (Accessor) pair.First;
1619 Accessor set_block = (Accessor) pair.Second;
1621 indexer = new Indexer (decl.type, decl.interface_type, (int) $2, decl.param_list,
1622 get_block, set_block, (Attributes) $1, loc);
1624 // Note that there is no equivalent of CheckDef for this case
1625 // We shall handle this in semantic analysis
1627 current_container.AddIndexer (indexer);
1629 current_local_parameters = null;
1630 implicit_value_parameter_type = null;
1631 indexer_parameters = null;
1636 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1638 Parameters pars = (Parameters) $4;
1640 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1641 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1644 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
1646 | type qualified_identifier DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1648 Parameters pars = (Parameters) $6;
1650 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1651 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1653 $$ = new IndexerDeclaration ((Expression) $1, (string) $2, pars);
1665 Location enum_location = lexer.Location;
1667 string full_name = MakeName ((string) $4);
1668 Enum e = new Enum (current_container, (Expression) $5, (int) $2, full_name,
1669 (Attributes) $1, enum_location);
1671 foreach (VariableDeclaration ev in (ArrayList) $6) {
1672 Location loc = (Location) ev.Location;
1674 CheckDef (e.AddEnumMember (ev.identifier,
1675 (Expression) ev.expression_or_array_initializer,
1676 loc, ev.OptAttributes),
1677 ev.identifier, loc);
1680 e.Namespace = current_namespace;
1682 CheckDef (current_container.AddEnum (e), full_name, enum_location);
1683 RootContext.Tree.RecordDecl (full_name, e);
1689 : /* empty */ { $$ = TypeManager.system_int32_expr; }
1690 | COLON type { $$ = $2; }
1694 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1700 opt_enum_member_declarations
1701 : /* empty */ { $$ = new ArrayList (); }
1702 | enum_member_declarations opt_comma { $$ = $1; }
1705 enum_member_declarations
1706 : enum_member_declaration
1708 ArrayList l = new ArrayList ();
1713 | enum_member_declarations COMMA enum_member_declaration
1715 ArrayList l = (ArrayList) $1;
1723 enum_member_declaration
1724 : opt_attributes IDENTIFIER
1726 $$ = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
1728 | opt_attributes IDENTIFIER
1730 $$ = lexer.Location;
1734 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
1738 delegate_declaration
1742 IDENTIFIER OPEN_PARENS
1743 opt_formal_parameter_list
1747 Location l = lexer.Location;
1748 Delegate del = new Delegate (current_container, (Expression) $4, (int) $2,
1749 MakeName ((string) $5), (Parameters) $7,
1750 (Attributes) $1, l);
1752 del.Namespace = current_namespace;
1753 CheckDef (current_container.AddDelegate (del), del.Name, l);
1758 IDENTIFIER OPEN_PARENS
1759 opt_formal_parameter_list
1763 Location l = lexer.Location;
1764 Delegate del = new Delegate (
1766 TypeManager.system_void_expr, (int) $2, MakeName ((string) $5),
1767 (Parameters) $7, (Attributes) $1, l);
1769 del.Namespace = current_namespace;
1770 CheckDef (current_container.AddDelegate (del), del.Name, l);
1775 : namespace_or_type_name
1778 namespace_or_type_name
1779 : qualified_identifier
1783 * Before you think of adding a return_type, notice that we have been
1784 * using two rules in the places where it matters (one rule using type
1785 * and another identical one that uses VOID as the return type). This
1786 * gets rid of a shift/reduce couple
1789 : type_name { /* class_type */
1791 This does interfaces, delegates, struct_types, class_types,
1792 parent classes, and more! 4.2
1794 $$ = DecomposeQI ((string) $1, lexer.Location);
1806 // Note that here only unmanaged types are allowed but we
1807 // can't perform checks during this phase - we do it during
1808 // semantic analysis.
1810 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1814 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
1820 | non_expression_type rank_specifier
1822 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1824 | non_expression_type STAR
1826 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1828 | expression rank_specifiers
1830 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1834 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1838 // We need this because the parser will happily go and reduce IDENTIFIER STAR
1839 // through this different path
1841 | multiplicative_expression STAR
1843 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1850 ArrayList types = new ArrayList ();
1855 | type_list COMMA type
1857 ArrayList types = (ArrayList) $1;
1865 * replaces all the productions for isolating the various
1866 * simple types, but we need this to reuse it easily in local_variable_type
1869 : OBJECT { $$ = TypeManager.system_object_expr; }
1870 | STRING { $$ = TypeManager.system_string_expr; }
1871 | BOOL { $$ = TypeManager.system_boolean_expr; }
1872 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
1873 | FLOAT { $$ = TypeManager.system_single_expr; }
1874 | DOUBLE { $$ = TypeManager.system_double_expr; }
1879 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
1880 | BYTE { $$ = TypeManager.system_byte_expr; }
1881 | SHORT { $$ = TypeManager.system_int16_expr; }
1882 | USHORT { $$ = TypeManager.system_uint16_expr; }
1883 | INT { $$ = TypeManager.system_int32_expr; }
1884 | UINT { $$ = TypeManager.system_uint32_expr; }
1885 | LONG { $$ = TypeManager.system_int64_expr; }
1886 | ULONG { $$ = TypeManager.system_uint64_expr; }
1887 | CHAR { $$ = TypeManager.system_char_expr; }
1888 | VOID { $$ = TypeManager.system_void_expr; }
1896 : type rank_specifiers
1898 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1903 // Expressions, section 7.5
1911 | qualified_identifier
1913 string name = (string) $1;
1916 $$ = DecomposeQI (name, lexer.Location);
1918 | parenthesized_expression
1920 | invocation_expression
1924 | post_increment_expression
1925 | post_decrement_expression
1929 | checked_expression
1930 | unchecked_expression
1931 | pointer_member_access
1938 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
1939 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
1940 | NULL { $$ = NullLiteral.Null; }
1944 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
1945 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
1946 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
1951 object v = lexer.Value;
1954 $$ = new IntLiteral ((Int32) v);
1956 $$ = new UIntLiteral ((UInt32) v);
1958 $$ = new LongLiteral ((Int64) v);
1959 else if (v is ulong)
1960 $$ = new ULongLiteral ((UInt64) v);
1962 Console.WriteLine ("OOPS. Unexpected result from scanner");
1967 : TRUE { $$ = new BoolLiteral (true); }
1968 | FALSE { $$ = new BoolLiteral (false); }
1971 parenthesized_expression
1972 : OPEN_PARENS expression CLOSE_PARENS
1977 : primary_expression DOT IDENTIFIER
1979 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1981 | predefined_type DOT IDENTIFIER
1983 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1991 invocation_expression
1992 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
1995 Location l = lexer.Location;
1996 Report.Error (1, l, "Parse error");
1998 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2003 : /* empty */ { $$ = null; }
2010 ArrayList list = new ArrayList ();
2014 | argument_list COMMA argument
2016 ArrayList list = (ArrayList) $1;
2025 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2027 | REF variable_reference
2029 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2031 | OUT variable_reference
2033 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2038 : expression { note ("section 5.4"); $$ = $1; }
2042 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2044 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2046 | primary_expression rank_specifiers
2048 // So the super-trick is that primary_expression
2049 // can only be either a SimpleName or a MemberAccess.
2050 // The MemberAccess case arises when you have a fully qualified type-name like :
2052 // SimpleName is when you have
2055 Expression expr = (Expression) $1;
2056 if (!(expr is SimpleName || expr is MemberAccess)) {
2057 Error_ExpectingTypeName (lexer.Location, expr);
2058 $$ = TypeManager.system_object_expr;
2061 // So we extract the string corresponding to the SimpleName
2064 $$ = new SimpleName (GetQualifiedIdentifier (expr) + (string) $2, lexer.Location);
2072 ArrayList list = new ArrayList ();
2076 | expression_list COMMA expression
2078 ArrayList list = (ArrayList) $1;
2087 $$ = new This (current_block, lexer.Location);
2092 : BASE DOT IDENTIFIER
2094 $$ = new BaseAccess ((string) $3, lexer.Location);
2096 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2098 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2102 post_increment_expression
2103 : primary_expression OP_INC
2105 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2106 (Expression) $1, lexer.Location);
2110 post_decrement_expression
2111 : primary_expression OP_DEC
2113 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2114 (Expression) $1, lexer.Location);
2119 : object_or_delegate_creation_expression
2120 | array_creation_expression
2123 object_or_delegate_creation_expression
2124 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2126 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2130 array_creation_expression
2131 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2133 opt_array_initializer
2135 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2137 | NEW type rank_specifiers array_initializer
2139 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2143 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2159 : rank_specifier opt_rank_specifier
2161 $$ = (string) $2 + (string) $1;
2166 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2168 $$ = "[" + (string) $2 + "]";
2188 | dim_separators COMMA
2190 $$ = (string) $1 + ",";
2194 opt_array_initializer
2206 : OPEN_BRACE CLOSE_BRACE
2208 ArrayList list = new ArrayList ();
2211 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2213 $$ = (ArrayList) $2;
2217 variable_initializer_list
2218 : variable_initializer
2220 ArrayList list = new ArrayList ();
2224 | variable_initializer_list COMMA variable_initializer
2226 ArrayList list = (ArrayList) $1;
2233 : TYPEOF OPEN_PARENS type CLOSE_PARENS
2235 $$ = new TypeOf ((Expression) $3, lexer.Location);
2240 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2241 $$ = new SizeOf ((Expression) $3, lexer.Location);
2246 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2248 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2252 unchecked_expression
2253 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2255 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2259 pointer_member_access
2260 : primary_expression OP_PTR IDENTIFIER
2264 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2265 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2269 : primary_expression
2270 | BANG prefixed_unary_expression
2272 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2274 | TILDE prefixed_unary_expression
2276 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2282 : OPEN_PARENS expression CLOSE_PARENS unary_expression
2284 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2286 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2288 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2293 // The idea to split this out is from Rhys' grammar
2294 // to solve the problem with casts.
2296 prefixed_unary_expression
2298 | PLUS prefixed_unary_expression
2300 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2302 | MINUS prefixed_unary_expression
2304 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2306 | OP_INC prefixed_unary_expression
2308 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2309 (Expression) $2, lexer.Location);
2311 | OP_DEC prefixed_unary_expression
2313 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2314 (Expression) $2, lexer.Location);
2316 | STAR prefixed_unary_expression
2318 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2320 | BITWISE_AND prefixed_unary_expression
2322 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2326 pre_increment_expression
2327 : OP_INC prefixed_unary_expression
2329 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2330 (Expression) $2, lexer.Location);
2334 pre_decrement_expression
2335 : OP_DEC prefixed_unary_expression
2337 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2338 (Expression) $2, lexer.Location);
2342 multiplicative_expression
2343 : prefixed_unary_expression
2344 | multiplicative_expression STAR prefixed_unary_expression
2346 $$ = new Binary (Binary.Operator.Multiply,
2347 (Expression) $1, (Expression) $3, lexer.Location);
2349 | multiplicative_expression DIV prefixed_unary_expression
2351 $$ = new Binary (Binary.Operator.Division,
2352 (Expression) $1, (Expression) $3, lexer.Location);
2354 | multiplicative_expression PERCENT prefixed_unary_expression
2356 $$ = new Binary (Binary.Operator.Modulus,
2357 (Expression) $1, (Expression) $3, lexer.Location);
2362 : multiplicative_expression
2363 | additive_expression PLUS multiplicative_expression
2365 $$ = new Binary (Binary.Operator.Addition,
2366 (Expression) $1, (Expression) $3, lexer.Location);
2368 | additive_expression MINUS multiplicative_expression
2370 $$ = new Binary (Binary.Operator.Subtraction,
2371 (Expression) $1, (Expression) $3, lexer.Location);
2376 : additive_expression
2377 | shift_expression OP_SHIFT_LEFT additive_expression
2379 $$ = new Binary (Binary.Operator.LeftShift,
2380 (Expression) $1, (Expression) $3, lexer.Location);
2382 | shift_expression OP_SHIFT_RIGHT additive_expression
2384 $$ = new Binary (Binary.Operator.RightShift,
2385 (Expression) $1, (Expression) $3, lexer.Location);
2389 relational_expression
2391 | relational_expression OP_LT shift_expression
2393 $$ = new Binary (Binary.Operator.LessThan,
2394 (Expression) $1, (Expression) $3, lexer.Location);
2396 | relational_expression OP_GT shift_expression
2398 $$ = new Binary (Binary.Operator.GreaterThan,
2399 (Expression) $1, (Expression) $3, lexer.Location);
2401 | relational_expression OP_LE shift_expression
2403 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2404 (Expression) $1, (Expression) $3, lexer.Location);
2406 | relational_expression OP_GE shift_expression
2408 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2409 (Expression) $1, (Expression) $3, lexer.Location);
2411 | relational_expression IS type
2413 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
2415 | relational_expression AS type
2417 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
2422 : relational_expression
2423 | equality_expression OP_EQ relational_expression
2425 $$ = new Binary (Binary.Operator.Equality,
2426 (Expression) $1, (Expression) $3, lexer.Location);
2428 | equality_expression OP_NE relational_expression
2430 $$ = new Binary (Binary.Operator.Inequality,
2431 (Expression) $1, (Expression) $3, lexer.Location);
2436 : equality_expression
2437 | and_expression BITWISE_AND equality_expression
2439 $$ = new Binary (Binary.Operator.BitwiseAnd,
2440 (Expression) $1, (Expression) $3, lexer.Location);
2444 exclusive_or_expression
2446 | exclusive_or_expression CARRET and_expression
2448 $$ = new Binary (Binary.Operator.ExclusiveOr,
2449 (Expression) $1, (Expression) $3, lexer.Location);
2453 inclusive_or_expression
2454 : exclusive_or_expression
2455 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2457 $$ = new Binary (Binary.Operator.BitwiseOr,
2458 (Expression) $1, (Expression) $3, lexer.Location);
2462 conditional_and_expression
2463 : inclusive_or_expression
2464 | conditional_and_expression OP_AND inclusive_or_expression
2466 $$ = new Binary (Binary.Operator.LogicalAnd,
2467 (Expression) $1, (Expression) $3, lexer.Location);
2471 conditional_or_expression
2472 : conditional_and_expression
2473 | conditional_or_expression OP_OR conditional_and_expression
2475 $$ = new Binary (Binary.Operator.LogicalOr,
2476 (Expression) $1, (Expression) $3, lexer.Location);
2480 conditional_expression
2481 : conditional_or_expression
2482 | conditional_or_expression INTERR expression COLON expression
2484 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2488 assignment_expression
2489 : prefixed_unary_expression ASSIGN expression
2491 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2493 | prefixed_unary_expression OP_MULT_ASSIGN expression
2495 Location l = lexer.Location;
2497 $$ = new CompoundAssign (
2498 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
2500 | prefixed_unary_expression OP_DIV_ASSIGN expression
2502 Location l = lexer.Location;
2504 $$ = new CompoundAssign (
2505 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
2507 | prefixed_unary_expression OP_MOD_ASSIGN expression
2509 Location l = lexer.Location;
2511 $$ = new CompoundAssign (
2512 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
2514 | prefixed_unary_expression OP_ADD_ASSIGN expression
2516 Location l = lexer.Location;
2518 $$ = new CompoundAssign (
2519 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
2521 | prefixed_unary_expression OP_SUB_ASSIGN expression
2523 Location l = lexer.Location;
2525 $$ = new CompoundAssign (
2526 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
2528 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2530 Location l = lexer.Location;
2532 $$ = new CompoundAssign (
2533 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
2535 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2537 Location l = lexer.Location;
2539 $$ = new CompoundAssign (
2540 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
2542 | prefixed_unary_expression OP_AND_ASSIGN expression
2544 Location l = lexer.Location;
2546 $$ = new CompoundAssign (
2547 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
2549 | prefixed_unary_expression OP_OR_ASSIGN expression
2551 Location l = lexer.Location;
2553 $$ = new CompoundAssign (
2554 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
2556 | prefixed_unary_expression OP_XOR_ASSIGN expression
2558 Location l = lexer.Location;
2560 $$ = new CompoundAssign (
2561 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
2566 : conditional_expression
2567 | assignment_expression
2589 name = MakeName ((string) $4);
2591 new_class = new Class (current_container, name, (int) $2,
2592 (Attributes) $1, lexer.Location);
2593 current_container = new_class;
2594 current_container.Namespace = current_namespace;
2595 RootContext.Tree.RecordDecl (name, new_class);
2601 Class new_class = (Class) current_container;
2604 new_class.Bases = (ArrayList) $6;
2606 current_container = current_container.Parent;
2607 CheckDef (current_container.AddClass (new_class), new_class.Name, new_class.Location);
2614 : /* empty */ { $$ = (int) 0; }
2620 | modifiers modifier
2625 if ((m1 & m2) != 0) {
2626 Location l = lexer.Location;
2627 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2629 $$ = (int) (m1 | m2);
2634 : NEW { $$ = Modifiers.NEW; }
2635 | PUBLIC { $$ = Modifiers.PUBLIC; }
2636 | PROTECTED { $$ = Modifiers.PROTECTED; }
2637 | INTERNAL { $$ = Modifiers.INTERNAL; }
2638 | PRIVATE { $$ = Modifiers.PRIVATE; }
2639 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2640 | SEALED { $$ = Modifiers.SEALED; }
2641 | STATIC { $$ = Modifiers.STATIC; }
2642 | READONLY { $$ = Modifiers.READONLY; }
2643 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2644 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2645 | EXTERN { $$ = Modifiers.EXTERN; }
2646 | VOLATILE { $$ = Modifiers.VOLATILE; }
2647 | UNSAFE { $$ = Modifiers.UNSAFE; }
2651 : /* empty */ { $$ = null; }
2652 | class_base { $$ = $1; }
2656 : COLON type_list { $$ = $2; }
2664 // A block is "contained" on the following places:
2666 // property_declaration as part of the accessor body (get/set)
2667 // operator_declaration
2668 // constructor_declaration
2669 // destructor_declaration
2670 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2675 current_block = new Block (current_block, current_local_parameters,
2676 lexer.Location, Location.Null);
2678 opt_statement_list CLOSE_BRACE
2680 while (current_block.Implicit)
2681 current_block = current_block.Parent;
2683 current_block.SetEndLocation (lexer.Location);
2684 current_block = current_block.Parent;
2695 | statement_list statement
2699 : declaration_statement
2701 if ($1 != null && (Block) $1 != current_block){
2702 current_block.AddStatement ((Statement) $1);
2703 current_block = (Block) $1;
2706 | embedded_statement
2708 Statement s = (Statement) $1;
2711 current_block.AddStatement ((Statement) $1);
2719 | expression_statement
2720 | selection_statement
2721 | iteration_statement
2725 | unchecked_statement
2735 $$ = new EmptyStatement ();
2742 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
2744 if (!current_block.AddLabel ((string) $1, labeled)){
2745 Location l = lexer.Location;
2746 Report.Error (140, l, "The label '" + ((string) $1) + "' is a duplicate");
2748 current_block.AddStatement (labeled);
2753 declaration_statement
2754 : local_variable_declaration SEMICOLON
2757 DictionaryEntry de = (DictionaryEntry) $1;
2759 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
2763 | local_constant_declaration SEMICOLON
2766 DictionaryEntry de = (DictionaryEntry) $1;
2768 $$ = declare_local_constant ((Expression) de.Key, (VariableDeclaration) de.Value);
2774 * The following is from Rhys' grammar:
2775 * > Types in local variable declarations must be recognized as
2776 * > expressions to prevent reduce/reduce errors in the grammar.
2777 * > The expressions are converted into types during semantic analysis.
2780 : primary_expression opt_rank_specifier
2782 // FIXME: Do something smart here regarding the composition of the type.
2784 // Ok, the above "primary_expression" is there to get rid of
2785 // both reduce/reduce and shift/reduces in the grammar, it should
2786 // really just be "type_name". If you use type_name, a reduce/reduce
2787 // creeps up. If you use qualified_identifier (which is all we need
2788 // really) two shift/reduces appear.
2791 // So the super-trick is that primary_expression
2792 // can only be either a SimpleName or a MemberAccess.
2793 // The MemberAccess case arises when you have a fully qualified type-name like :
2795 // SimpleName is when you have
2798 Expression expr = (Expression) $1;
2799 if (!(expr is SimpleName || expr is MemberAccess)) {
2800 Error_ExpectingTypeName (lexer.Location, expr);
2804 // So we extract the string corresponding to the SimpleName
2807 if ((string) $2 == "")
2810 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2813 | builtin_types opt_rank_specifier
2815 if ((string) $2 == "")
2818 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2822 local_variable_pointer_type
2823 : primary_expression STAR
2825 Expression expr = (Expression) $1;
2826 Location l = lexer.Location;
2828 if (!(expr is SimpleName || expr is MemberAccess)) {
2829 Error_ExpectingTypeName (l, expr);
2833 $$ = new ComposedCast ((Expression) $1, "*", l);
2835 | builtin_types STAR
2837 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
2841 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
2843 | local_variable_pointer_type STAR
2845 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2849 local_variable_declaration
2850 : local_variable_type variable_declarators
2853 $$ = new DictionaryEntry ($1, $2);
2857 | local_variable_pointer_type opt_rank_specifier variable_declarators
2862 if ((string) $2 == "")
2863 t = (Expression) $1;
2865 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2866 $$ = new DictionaryEntry (t, $3);
2872 local_constant_declaration
2873 : CONST local_variable_type constant_declarator
2876 $$ = new DictionaryEntry ($2, $3);
2882 expression_statement
2883 : statement_expression SEMICOLON
2890 // We have to do the wrapping here and not in the case above,
2891 // because statement_expression is used for example in for_statement
2893 statement_expression
2894 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2895 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2896 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2897 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2898 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2899 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2900 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2902 Report.Error (1002, lexer.Location, "Expecting `;'");
2906 object_creation_expression
2907 : object_or_delegate_creation_expression
2908 { note ("complain if this is a delegate maybe?"); }
2917 : if_statement_open if_statement_rest
2926 oob_stack.Push (lexer.Location);
2931 : boolean_expression CLOSE_PARENS
2934 Location l = (Location) oob_stack.Pop ();
2936 $$ = new If ((Expression) $1, (Statement) $3, l);
2938 if (RootContext.WarningLevel >= 3){
2939 if ($3 is EmptyStatement)
2940 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
2944 | boolean_expression CLOSE_PARENS
2945 embedded_statement ELSE embedded_statement
2947 Location l = (Location) oob_stack.Pop ();
2949 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
2954 : SWITCH OPEN_PARENS
2956 oob_stack.Push (lexer.Location);
2957 switch_stack.Push (current_block);
2959 expression CLOSE_PARENS
2962 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
2963 current_block = (Block) switch_stack.Pop ();
2979 Report.Error (1522, lexer.Location, "Empty switch block");
2987 ArrayList sections = new ArrayList ();
2992 | switch_sections switch_section
2994 ArrayList sections = (ArrayList) $1;
3004 current_block = new Block (current_block, lexer.Location, lexer.Location);
3008 Block topmost = current_block;
3010 while (topmost.Implicit)
3011 topmost = topmost.Parent;
3012 $$ = new SwitchSection ((ArrayList) $1, topmost);
3019 ArrayList labels = new ArrayList ();
3024 | switch_labels switch_label
3026 ArrayList labels = (ArrayList) ($1);
3034 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3035 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3038 1523, lexer.Location,
3039 "The keyword case or default must precede code in switch block");
3053 oob_stack.Push (lexer.Location);
3055 boolean_expression CLOSE_PARENS embedded_statement
3057 Location l = (Location) oob_stack.Pop ();
3058 $$ = new While ((Expression) $4, (Statement) $6, l);
3060 if (RootContext.WarningLevel >= 3){
3061 if ($6 is EmptyStatement)
3062 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3068 : DO embedded_statement
3070 oob_stack.Push (lexer.Location);
3072 boolean_expression CLOSE_PARENS SEMICOLON
3074 Location l = (Location) oob_stack.Pop ();
3076 $$ = new Do ((Statement) $2, (Expression) $6, l);
3082 opt_for_initializer SEMICOLON
3084 Block assign_block = new Block (current_block);
3085 current_block = assign_block;
3087 if ($3 is DictionaryEntry){
3088 DictionaryEntry de = (DictionaryEntry) $3;
3090 Expression type = (Expression) de.Key;
3091 ArrayList var_declarators = (ArrayList) de.Value;
3093 foreach (VariableDeclaration decl in var_declarators){
3097 vi = current_block.AddVariable (
3098 type, decl.identifier, current_local_parameters, decl.Location);
3102 Location l = lexer.Location;
3104 if (decl.expression_or_array_initializer is Expression){
3105 expr = (Expression) decl.expression_or_array_initializer;
3106 } else if (decl.expression_or_array_initializer == null) {
3109 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3110 expr = new ArrayCreation (type, "", init, decl.Location);
3113 LocalVariableReference var;
3114 var = new LocalVariableReference (assign_block, decl.identifier, l);
3117 Assign a = new Assign (var, expr, decl.Location);
3119 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3125 oob_stack.Push (lexer.Location);
3127 opt_for_condition SEMICOLON
3128 opt_for_iterator CLOSE_PARENS
3131 Location l = (Location) oob_stack.Pop ();
3133 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3135 if (RootContext.WarningLevel >= 3){
3136 if ($10 is EmptyStatement)
3137 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3140 current_block.AddStatement (f);
3141 while (current_block.Implicit)
3142 current_block = current_block.Parent;
3144 current_block = current_block.Parent;
3149 : /* empty */ { $$ = new EmptyStatement (); }
3154 : local_variable_declaration
3155 | statement_expression_list
3159 : /* empty */ { $$ = null; }
3160 | boolean_expression
3164 : /* empty */ { $$ = new EmptyStatement (); }
3169 : statement_expression_list
3172 statement_expression_list
3173 : statement_expression
3175 // CHANGE: was `null'
3176 Block b = new Block (current_block, true);
3178 b.AddStatement ((Statement) $1);
3181 | statement_expression_list COMMA statement_expression
3183 Block b = (Block) $1;
3185 b.AddStatement ((Statement) $3);
3191 : FOREACH OPEN_PARENS type IDENTIFIER IN
3193 oob_stack.Push (lexer.Location);
3195 expression CLOSE_PARENS
3197 oob_stack.Push (current_block);
3199 Block foreach_block = new Block (current_block, true);
3200 LocalVariableReference v = null;
3201 Location l = lexer.Location;
3204 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3208 // Get a writable reference to this read-only variable.
3209 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3211 current_block = foreach_block;
3214 oob_stack.Push (current_block);
3218 Block foreach_block = (Block) oob_stack.Pop ();
3219 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3220 Block prev_block = (Block) oob_stack.Pop ();
3221 Location l = (Location) oob_stack.Pop ();
3223 current_block = prev_block;
3226 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3227 foreach_block.AddStatement (f);
3236 | continue_statement
3245 $$ = new Break (lexer.Location);
3250 : CONTINUE SEMICOLON
3252 $$ = new Continue (lexer.Location);
3257 : GOTO IDENTIFIER SEMICOLON
3259 $$ = new Goto (current_block, (string) $2, lexer.Location);
3261 | GOTO CASE constant_expression SEMICOLON
3263 $$ = new GotoCase ((Expression) $3, lexer.Location);
3265 | GOTO DEFAULT SEMICOLON
3267 $$ = new GotoDefault (lexer.Location);
3272 : RETURN opt_expression SEMICOLON
3274 $$ = new Return ((Expression) $2, lexer.Location);
3279 : THROW opt_expression SEMICOLON
3281 $$ = new Throw ((Expression) $2, lexer.Location);
3291 : TRY block catch_clauses
3294 ArrayList s = new ArrayList ();
3296 foreach (Catch cc in (ArrayList) $3) {
3303 // Now s contains the list of specific catch clauses
3304 // and g contains the general one.
3306 $$ = new Try ((Block) $2, s, g, null, lexer.Location);
3308 | TRY block opt_catch_clauses FINALLY block
3311 ArrayList s = new ArrayList ();
3312 ArrayList catch_list = (ArrayList) $3;
3314 if (catch_list != null){
3315 foreach (Catch cc in catch_list) {
3323 $$ = new Try ((Block) $2, s, g, (Block) $5, lexer.Location);
3327 Report.Error (1524, lexer.Location, "Expected catch or finally");
3332 : /* empty */ { $$ = null; }
3339 ArrayList l = new ArrayList ();
3344 | catch_clauses catch_clause
3346 ArrayList l = (ArrayList) $1;
3354 : /* empty */ { $$ = null; }
3359 : CATCH opt_catch_args
3361 Expression type = null;
3365 DictionaryEntry cc = (DictionaryEntry) $2;
3366 type = (Expression) cc.Key;
3367 id = (string) cc.Value;
3370 ArrayList one = new ArrayList ();
3371 Location loc = lexer.Location;
3373 one.Add (new VariableDeclaration (id, null, loc));
3376 current_block = new Block (current_block);
3377 Block b = declare_local_variables (type, one, loc);
3382 Expression type = null;
3386 DictionaryEntry cc = (DictionaryEntry) $2;
3387 type = (Expression) cc.Key;
3388 id = (string) cc.Value;
3392 // FIXME: I can change this for an assignment.
3394 while (current_block != (Block) $1)
3395 current_block = current_block.Parent;
3400 $$ = new Catch (type, id , (Block) $4, lexer.Location);
3405 : /* empty */ { $$ = null; }
3410 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3412 $$ = new DictionaryEntry ($2, $3);
3419 $$ = new Checked ((Block) $2);
3426 $$ = new Unchecked ((Block) $2);
3433 if (!RootContext.Unsafe){
3434 Report.Error (227, lexer.Location,
3435 "Unsafe code can only be used if --unsafe is used");
3438 $$ = new Unsafe ((Block) $3);
3444 type fixed_pointer_declarators
3447 Block assign_block = new Block (current_block, true);
3448 ArrayList list = (ArrayList) $4;
3449 Expression type = (Expression) $3;
3450 Location l = lexer.Location;
3451 int top = list.Count;
3453 for (int i = 0; i < top; i++){
3454 Pair p = (Pair) list [i];
3457 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3464 current_block.AddStatement (assign_block);
3465 current_block = assign_block;
3466 oob_stack.Push (assign_block);
3471 Location l = (Location) oob_stack.Pop ();
3472 Block assign_block = (Block) oob_stack.Pop ();
3474 ArrayList list = (ArrayList) $4;
3475 int top = list.Count;
3477 $$ = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
3481 fixed_pointer_declarators
3482 : fixed_pointer_declarator {
3483 ArrayList declarators = new ArrayList ();
3484 declarators.Add ($1);
3487 | fixed_pointer_declarators COMMA fixed_pointer_declarator
3489 ArrayList declarators = (ArrayList) $1;
3490 declarators.Add ($3);
3495 fixed_pointer_declarator
3496 : IDENTIFIER ASSIGN expression
3498 $$ = new Pair ($1, $3);
3503 : LOCK OPEN_PARENS expression CLOSE_PARENS
3509 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
3514 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
3516 Block assign_block = new Block (current_block);
3517 current_block = assign_block;
3519 oob_stack.Push (lexer.Location);
3521 if ($3 is DictionaryEntry){
3522 DictionaryEntry de = (DictionaryEntry) $3;
3523 Location l = lexer.Location;
3525 Expression type = (Expression) de.Key;
3526 ArrayList var_declarators = (ArrayList) de.Value;
3528 ArrayList vars = new ArrayList ();
3530 foreach (VariableDeclaration decl in var_declarators){
3532 VariableInfo vi = current_block.AddVariable (
3533 type, decl.identifier,
3534 current_local_parameters, decl.Location);
3540 if (decl.expression_or_array_initializer is Expression){
3541 expr = (Expression) decl.expression_or_array_initializer;
3543 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3545 expr = new ArrayCreation (type, "", init, decl.Location);
3548 LocalVariableReference var;
3550 // Get a writable reference to this read-only variable.
3551 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
3553 // This is so that it is not a warning on using variables
3556 vars.Add (new DictionaryEntry (var, expr));
3558 // Assign a = new Assign (var, expr, decl.Location);
3559 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3561 $3 = new DictionaryEntry (type, vars);
3566 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
3567 current_block.AddStatement (u);
3568 while (current_block.Implicit)
3569 current_block = current_block.Parent;
3571 current_block = current_block.Parent;
3575 resource_acquisition
3576 : local_variable_declaration
3583 // A class used to pass around variable declarations and constants
3585 public class VariableDeclaration {
3586 public string identifier;
3587 public object expression_or_array_initializer;
3588 public Location Location;
3589 public Attributes OptAttributes;
3591 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
3593 this.identifier = id;
3594 this.expression_or_array_initializer = eoai;
3596 this.OptAttributes = opt_attrs;
3599 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
3605 // A class used to hold info about an indexer declarator
3608 public class IndexerDeclaration {
3609 public Expression type;
3610 public string interface_type;
3611 public Parameters param_list;
3613 public IndexerDeclaration (Expression type, string interface_type, Parameters param_list)
3616 this.interface_type = interface_type;
3617 this.param_list = param_list;
3622 // A class used to hold info about an operator declarator
3625 public class OperatorDeclaration {
3626 public Operator.OpType optype;
3627 public Expression ret_type, arg1type, arg2type;
3628 public string arg1name, arg2name;
3629 public Location location;
3631 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
3632 Expression arg1type, string arg1name,
3633 Expression arg2type, string arg2name, Location location)
3636 this.ret_type = ret_type;
3637 this.arg1type = arg1type;
3638 this.arg1name = arg1name;
3639 this.arg2type = arg2type;
3640 this.arg2name = arg2name;
3641 this.location = location;
3646 void Error_ExpectingTypeName (Location l, Expression expr)
3648 if (expr is Invocation){
3649 Report.Error (1002, l, "; expected");
3651 Report.Error (-1, l, "Invalid Type definition");
3656 // Given the @class_name name, it creates a fully qualified name
3657 // based on the containing declaration space
3660 MakeName (string class_name)
3662 string ns = current_namespace.Name;
3663 string container_name = current_container.Name;
3665 if (container_name == ""){
3667 return ns + "." + class_name;
3671 return container_name + "." + class_name;
3675 // Used to report back to the user the result of a declaration
3676 // in the current declaration space
3679 CheckDef (AdditionResult result, string name, Location l)
3681 if (result == AdditionResult.Success)
3685 case AdditionResult.NameExists:
3686 Report.Error (102, l, "The container `" + current_container.Name +
3687 "' already contains a definition for `"+
3693 // This is handled only for static Constructors, because
3694 // in reality we handle these by the semantic analysis later
3696 case AdditionResult.MethodExists:
3698 111, l, "Class `"+current_container.Name+
3699 "' already defines a member called '" +
3700 name + "' with the same parameter types (more than one default constructor)");
3703 case AdditionResult.EnclosingClash:
3704 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
3707 case AdditionResult.NotAConstructor:
3708 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
3714 CheckDef (bool result, string name, Location l)
3718 CheckDef (AdditionResult.NameExists, name, l);
3721 Expression DecomposeQI (string name, Location loc)
3725 if (name.IndexOf ('.') == -1){
3726 return new SimpleName (name, loc);
3728 int pos = name.LastIndexOf (".");
3729 string left = name.Substring (0, pos);
3730 string right = name.Substring (pos + 1);
3732 o = DecomposeQI (left, loc);
3734 return new MemberAccess (o, right, loc);
3739 // This method is used to get at the complete string representation of
3740 // a fully-qualified type name, hiding inside a MemberAccess ;-)
3741 // This is necessary because local_variable_type admits primary_expression
3742 // as the type of the variable. So we do some extra checking
3744 string GetQualifiedIdentifier (Expression expr)
3746 if (expr is SimpleName)
3747 return ((SimpleName)expr).Name;
3748 else if (expr is MemberAccess)
3749 return GetQualifiedIdentifier (((MemberAccess)expr).Expr) + "." + ((MemberAccess) expr).Identifier;
3751 throw new Exception ("Expr has to be either SimpleName or MemberAccess! (" + expr + ")");
3755 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
3757 Block implicit_block;
3758 ArrayList inits = null;
3761 // We use the `Used' property to check whether statements
3762 // have been added to the current block. If so, we need
3763 // to create another block to contain the new declaration
3764 // otherwise, as an optimization, we use the same block to
3765 // add the declaration.
3767 // FIXME: A further optimization is to check if the statements
3768 // that were added were added as part of the initialization
3769 // below. In which case, no other statements have been executed
3770 // and we might be able to reduce the number of blocks for
3771 // situations like this:
3773 // int j = 1; int k = j + 1;
3775 if (current_block.Used) {
3776 implicit_block = new Block (current_block, true, loc, Location.Null);
3777 implicit_block.AddChildVariableNames (current_block);
3779 implicit_block = current_block;
3781 foreach (VariableDeclaration decl in variable_declarators){
3783 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
3784 if (decl.expression_or_array_initializer != null){
3786 inits = new ArrayList ();
3793 return implicit_block;
3795 foreach (VariableDeclaration decl in inits){
3799 if (decl.expression_or_array_initializer is Expression){
3800 expr = (Expression) decl.expression_or_array_initializer;
3803 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3805 expr = new ArrayCreation (type, "", init, decl.Location);
3808 LocalVariableReference var;
3809 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
3811 assign = new Assign (var, expr, decl.Location);
3813 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
3816 return implicit_block;
3819 Block declare_local_constant (Expression type, VariableDeclaration decl)
3821 Block implicit_block;
3823 if (current_block.Used)
3824 implicit_block = new Block (current_block, true);
3826 implicit_block = current_block;
3828 if (!(implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
3829 current_local_parameters, decl.Location))){
3832 return implicit_block;
3835 void CheckAttributeTarget (string a)
3839 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
3843 Location l = lexer.Location;
3844 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
3850 void CheckUnaryOperator (Operator.OpType op)
3854 case Operator.OpType.LogicalNot:
3855 case Operator.OpType.OnesComplement:
3856 case Operator.OpType.Increment:
3857 case Operator.OpType.Decrement:
3858 case Operator.OpType.True:
3859 case Operator.OpType.False:
3860 case Operator.OpType.Addition:
3861 case Operator.OpType.Subtraction:
3866 Location l = lexer.Location;
3867 Report.Error (1019, l, "Overloadable unary operator expected");
3873 void CheckBinaryOperator (Operator.OpType op)
3877 case Operator.OpType.Addition:
3878 case Operator.OpType.Subtraction:
3879 case Operator.OpType.Multiply:
3880 case Operator.OpType.Division:
3881 case Operator.OpType.Modulus:
3882 case Operator.OpType.BitwiseAnd:
3883 case Operator.OpType.BitwiseOr:
3884 case Operator.OpType.ExclusiveOr:
3885 case Operator.OpType.LeftShift:
3886 case Operator.OpType.RightShift:
3887 case Operator.OpType.Equality:
3888 case Operator.OpType.Inequality:
3889 case Operator.OpType.GreaterThan:
3890 case Operator.OpType.LessThan:
3891 case Operator.OpType.GreaterThanOrEqual:
3892 case Operator.OpType.LessThanOrEqual:
3896 Location l = lexer.Location;
3897 Report.Error (1020, l, "Overloadable binary operator expected");
3903 void syntax_error (Location l, string msg)
3905 Report.Error (1003, l, "Syntax error, " + msg);
3908 void output (string s)
3910 Console.WriteLine (s);
3913 void note (string s)
3915 // Used to put annotations
3920 public Tokenizer Lexer {
3926 public CSharpParser (StreamReader reader, SourceFile file, ArrayList defines)
3928 current_namespace = new Namespace (null, file, "");
3929 this.name = file.Name;
3931 current_container = RootContext.Tree.Types;
3932 current_container.Namespace = current_namespace;
3933 oob_stack = new Stack ();
3934 switch_stack = new Stack ();
3936 lexer = new Tokenizer (reader, file, defines);
3939 public override void parse ()
3942 if (yacc_verbose_flag)
3943 yyparse (lexer, new yydebug.yyDebugSimple ());
3946 Tokenizer tokenizer = lexer as Tokenizer;
3947 tokenizer.cleanup ();
3948 } catch (Exception e){
3949 // Please do not remove this, it is used during debugging
3952 Report.Error (-25, lexer.Location, ": Parsing error ");
3953 Console.WriteLine (e);