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);
324 Report.Error(1518, Lexer.Location,
325 "Attributes cannot be applied to namespaces."
326 + " Expected class, delegate, enum, interface, or struct");
329 current_namespace = RootContext.Tree.RecordNamespace (current_namespace, file, (string) $3);
331 namespace_body opt_semicolon
333 current_namespace = current_namespace.Parent;
349 | qualified_identifier DOT IDENTIFIER {
350 $$ = (($1).ToString ()) + "." + ($3.ToString ()); }
355 : namespace_or_type_name
361 opt_namespace_member_declarations
372 opt_namespace_member_declarations
374 | namespace_member_declarations
377 namespace_member_declarations
378 : namespace_member_declaration
379 | namespace_member_declarations namespace_member_declaration
382 namespace_member_declaration
389 Class c = (Class) $1;
390 mod_flags = c.ModFlags;
392 } else if ($1 is Struct){
393 Struct s = (Struct) $1;
394 mod_flags = s.ModFlags;
399 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
401 1527, lexer.Location,
402 "Namespace elements cant be explicitly " +
403 "declared private or protected in `" + name + "'");
405 current_namespace.DeclarationFound = true;
407 | namespace_declaration
413 | interface_declaration
415 | delegate_declaration
417 // Enable this when we have handled all errors, because this acts as a generic fallback
420 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
430 | attribute_sections { $$ = $1; }
436 AttributeSection sect = (AttributeSection) $1;
438 if (sect.Target == "assembly")
439 RootContext.AddGlobalAttributeSection (current_container, sect);
442 $$ = new Attributes ((AttributeSection) $1);
444 | attribute_sections attribute_section
446 Attributes attrs = null;
447 AttributeSection sect = (AttributeSection) $2;
449 if (sect.Target == "assembly")
450 RootContext.AddGlobalAttributeSection (current_container, sect);
453 attrs = (Attributes) $1;
454 attrs.AddAttributeSection (sect);
462 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
464 string target = null;
467 target = (string) $2;
469 $$ = new AttributeSection (target, (ArrayList) $3);
471 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
473 $$ = new AttributeSection (null, (ArrayList) $2);
477 attribute_target_specifier
478 : attribute_target COLON
487 CheckAttributeTarget ((string) $1);
490 | EVENT { $$ = "event"; }
491 | RETURN { $$ = "return"; }
497 ArrayList attrs = new ArrayList ();
503 | attribute_list COMMA attribute
505 ArrayList attrs = (ArrayList) $1;
517 opt_attribute_arguments
519 $$ = new Attribute ((string) $1, (ArrayList) $3, (Location) $2);
524 : type_name { /* reserved attribute name or identifier: 17.4 */ }
527 opt_attribute_arguments
528 : /* empty */ { $$ = null; }
529 | OPEN_PARENS attribute_arguments CLOSE_PARENS
537 : opt_positional_argument_list
542 ArrayList args = new ArrayList ();
548 | positional_argument_list COMMA named_argument_list
550 ArrayList args = new ArrayList ();
556 | named_argument_list
558 ArrayList args = new ArrayList ();
567 opt_positional_argument_list
568 : /* empty */ { $$ = null; }
569 | positional_argument_list
572 positional_argument_list
575 ArrayList args = new ArrayList ();
576 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
580 | positional_argument_list COMMA expression
582 ArrayList args = (ArrayList) $1;
583 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
592 ArrayList args = new ArrayList ();
597 | named_argument_list COMMA named_argument
599 ArrayList args = (ArrayList) $1;
607 : IDENTIFIER ASSIGN expression
609 $$ = new DictionaryEntry (
611 new Argument ((Expression) $3, Argument.AType.Expression));
617 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
620 opt_class_member_declarations
622 | class_member_declarations
625 class_member_declarations
626 : class_member_declaration
627 | class_member_declarations
628 class_member_declaration
631 class_member_declaration
632 : constant_declaration // done
633 | field_declaration // done
634 | method_declaration // done
635 | property_declaration // done
636 | event_declaration // done
637 | indexer_declaration // done
638 | operator_declaration // done
639 | constructor_declaration // done
640 | destructor_declaration // done
650 string full_struct_name = MakeName ((string) $4);
652 new_struct = new Struct (current_container, full_struct_name, (int) $2,
653 (Attributes) $1, lexer.Location);
654 current_container = new_struct;
655 current_container.Namespace = current_namespace;
656 RootContext.Tree.RecordDecl (full_struct_name, new_struct);
662 Struct new_struct = (Struct) current_container;
665 new_struct.Bases = (ArrayList) $6;
667 current_container = current_container.Parent;
668 CheckDef (current_container.AddStruct (new_struct), new_struct.Name, new_struct.Location);
674 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
677 opt_struct_member_declarations
679 | struct_member_declarations
682 struct_member_declarations
683 : struct_member_declaration
684 | struct_member_declarations struct_member_declaration
687 struct_member_declaration
688 : constant_declaration
691 | property_declaration
693 | indexer_declaration
694 | operator_declaration
695 | constructor_declaration
699 * This is only included so we can flag error 575:
700 * destructors only allowed on class types
702 | destructor_declaration
713 foreach (VariableDeclaration constant in (ArrayList) $5){
714 Location l = constant.Location;
716 Const c = new Const (
717 (Expression) $4, (string) constant.identifier,
718 (Expression) constant.expression_or_array_initializer, (int) $2,
721 CheckDef (current_container.AddConstant (c), c.Name, l);
727 : constant_declarator
729 ArrayList constants = new ArrayList ();
733 | constant_declarators COMMA constant_declarator
735 ArrayList constants = (ArrayList) $1;
742 : IDENTIFIER ASSIGN constant_expression
744 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
755 Expression type = (Expression) $3;
758 foreach (VariableDeclaration var in (ArrayList) $4){
759 Location l = var.Location;
761 Field field = new Field (type, mod, var.identifier,
762 var.expression_or_array_initializer,
765 CheckDef (current_container.AddField (field), field.Name, l);
771 : variable_declarator
773 ArrayList decl = new ArrayList ();
777 | variable_declarators COMMA variable_declarator
779 ArrayList decls = (ArrayList) $1;
786 : IDENTIFIER ASSIGN variable_initializer
788 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
792 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
805 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
807 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
815 Method method = (Method) $1;
816 Block b = (Block) $2;
817 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
820 if ((method.ModFlags & extern_abstract) == 0){
822 501, lexer.Location, current_container.MakeName (method.Name) +
823 "must declare a body because it is not marked abstract or extern");
826 if ((method.ModFlags & Modifiers.EXTERN) != 0){
828 179, lexer.Location, current_container.MakeName (method.Name) +
829 " is declared extern, but has a body");
833 method.Block = (Block) $2;
834 CheckDef (current_container.AddMethod (method), method.Name, method.Location);
836 current_local_parameters = null;
850 1585, lexer.Location, "Member modifier `" +
851 Modifiers.Name (i) + "' must precede member type and name");
864 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
866 Method method = new Method ((Expression) $3, (int) $2, (string) $4,
867 (Parameters) $6, (Attributes) $1, lexer.Location);
869 current_local_parameters = (Parameters) $6;
877 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
879 Method method = new Method (TypeManager.system_void_expr, (int) $2, (string) $4,
880 (Parameters) $6, (Attributes) $1, lexer.Location);
882 current_local_parameters = (Parameters) $6;
889 | SEMICOLON { $$ = null; }
892 opt_formal_parameter_list
893 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
894 | formal_parameter_list
897 formal_parameter_list
900 ArrayList pars_list = (ArrayList) $1;
902 Parameter [] pars = new Parameter [pars_list.Count];
903 pars_list.CopyTo (pars);
905 $$ = new Parameters (pars, null, lexer.Location);
907 | fixed_parameters COMMA parameter_array
909 ArrayList pars_list = (ArrayList) $1;
911 Parameter [] pars = new Parameter [pars_list.Count];
912 pars_list.CopyTo (pars);
914 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
918 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
925 ArrayList pars = new ArrayList ();
930 | fixed_parameters COMMA fixed_parameter
932 ArrayList pars = (ArrayList) $1;
941 opt_parameter_modifier
945 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
949 opt_parameter_modifier
950 : /* empty */ { $$ = Parameter.Modifier.NONE; }
955 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
956 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
960 : opt_attributes PARAMS type IDENTIFIER
962 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
963 note ("type must be a single-dimension array type");
968 : qualified_identifier
977 implicit_value_parameter_type = (Expression) $3;
979 lexer.PropertyParsing = true;
983 accessor_declarations
985 lexer.PropertyParsing = false;
990 Pair pair = (Pair) $7;
991 Accessor get_block = (Accessor) pair.First;
992 Accessor set_block = (Accessor) pair.Second;
994 Location loc = (Location) $6;
995 prop = new Property ((Expression) $3, (string) $4, (int) $2, get_block, set_block,
996 (Attributes) $1, loc);
998 CheckDef (current_container.AddProperty (prop), prop.Name, loc);
999 implicit_value_parameter_type = null;
1003 accessor_declarations
1004 : get_accessor_declaration opt_set_accessor_declaration
1006 $$ = new Pair ($1, $2);
1008 | set_accessor_declaration opt_get_accessor_declaration
1010 $$ = new Pair ($2, $1);
1014 opt_get_accessor_declaration
1015 : /* empty */ { $$ = null; }
1016 | get_accessor_declaration
1019 opt_set_accessor_declaration
1020 : /* empty */ { $$ = null; }
1021 | set_accessor_declaration
1024 get_accessor_declaration
1025 : opt_attributes GET
1027 // If this is not the case, then current_local_parameters has already
1028 // been set in indexer_declaration
1029 if (parsing_indexer == false)
1030 current_local_parameters = null;
1032 current_local_parameters = indexer_parameters;
1033 lexer.PropertyParsing = false;
1037 $$ = new Accessor ((Block) $4, (Attributes) $1);
1038 current_local_parameters = null;
1039 lexer.PropertyParsing = true;
1043 set_accessor_declaration
1044 : opt_attributes SET
1047 Parameter implicit_value_parameter = new Parameter (
1048 implicit_value_parameter_type, "value",
1049 Parameter.Modifier.NONE, null);
1051 if (parsing_indexer == false) {
1052 args = new Parameter [1];
1053 args [0] = implicit_value_parameter;
1054 current_local_parameters = new Parameters (args, null, lexer.Location);
1056 Parameter [] fpars = indexer_parameters.FixedParameters;
1059 int count = fpars.Length;
1061 args = new Parameter [count + 1];
1062 fpars.CopyTo (args, 0);
1063 args [count] = implicit_value_parameter;
1066 current_local_parameters = new Parameters (
1067 args, indexer_parameters.ArrayParameter, lexer.Location);
1070 lexer.PropertyParsing = false;
1074 $$ = new Accessor ((Block) $4, (Attributes) $1);
1075 current_local_parameters = null;
1076 lexer.PropertyParsing = true;
1082 | SEMICOLON { $$ = null; }
1085 interface_declaration
1088 INTERFACE IDENTIFIER
1090 Interface new_interface;
1091 string full_interface_name = MakeName ((string) $4);
1093 new_interface = new Interface (current_container, full_interface_name, (int) $2,
1094 (Attributes) $1, lexer.Location);
1095 if (current_interface != null) {
1096 Location l = lexer.Location;
1097 Report.Error (-2, l, "Internal compiler error: interface inside interface");
1099 current_interface = new_interface;
1100 new_interface.Namespace = current_namespace;
1101 RootContext.Tree.RecordDecl (full_interface_name, new_interface);
1104 interface_body opt_semicolon
1106 Interface new_interface = (Interface) current_interface;
1109 new_interface.Bases = (ArrayList) $6;
1111 current_interface = null;
1112 CheckDef (current_container.AddInterface (new_interface),
1113 new_interface.Name, new_interface.Location);
1118 : /* empty */ { $$ = null; }
1123 : COLON interface_type_list { $$ = $2; }
1129 ArrayList interfaces = new ArrayList ();
1131 interfaces.Add ($1);
1134 | interface_type_list COMMA interface_type
1136 ArrayList interfaces = (ArrayList) $1;
1137 interfaces.Add ($3);
1144 opt_interface_member_declarations
1148 opt_interface_member_declarations
1150 | interface_member_declarations
1153 interface_member_declarations
1154 : interface_member_declaration
1155 | interface_member_declarations interface_member_declaration
1158 interface_member_declaration
1159 : interface_method_declaration
1161 InterfaceMethod m = (InterfaceMethod) $1;
1163 CheckDef (current_interface.AddMethod (m), m.Name, m.Location);
1165 | interface_property_declaration
1167 InterfaceProperty p = (InterfaceProperty) $1;
1169 CheckDef (current_interface.AddProperty (p), p.Name, p.Location);
1171 | interface_event_declaration
1173 InterfaceEvent e = (InterfaceEvent) $1;
1175 CheckDef (current_interface.AddEvent (e), e.Name, lexer.Location);
1177 | interface_indexer_declaration
1179 InterfaceIndexer i = (InterfaceIndexer) $1;
1181 CheckDef (current_interface.AddIndexer (i), "indexer", i.Location);
1186 : /* empty */ { $$ = false; }
1187 | NEW { $$ = true; }
1190 interface_method_declaration
1191 : opt_attributes opt_new type IDENTIFIER
1192 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1195 $$ = new InterfaceMethod ((Expression) $3, (string) $4, (bool) $2,
1196 (Parameters) $6, (Attributes) $1, lexer.Location);
1198 | opt_attributes opt_new VOID IDENTIFIER
1199 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1202 $$ = new InterfaceMethod (
1203 TypeManager.system_void_expr, (string) $4, (bool) $2, (Parameters) $6,
1204 (Attributes) $1, lexer.Location);
1208 interface_property_declaration
1213 { lexer.PropertyParsing = true; }
1215 { lexer.PropertyParsing = false; }
1220 $$ = new InterfaceProperty ((Expression) $3, (string) $4, (bool) $2,
1221 (gs & 1) == 1, (gs & 2) == 2, (Attributes) $1,
1227 : opt_attributes GET SEMICOLON { $$ = 1; }
1228 | opt_attributes SET SEMICOLON { $$ = 2; }
1229 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1231 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1235 interface_event_declaration
1236 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1238 $$ = new InterfaceEvent ((Expression) $4, (string) $5, (bool) $2, (Attributes) $1,
1243 interface_indexer_declaration
1244 : opt_attributes opt_new type THIS
1245 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1247 { lexer.PropertyParsing = true; }
1249 { lexer.PropertyParsing = false; }
1252 int a_flags = (int) $10;
1254 bool do_get = (a_flags & 1) == 1;
1255 bool do_set = (a_flags & 2) == 2;
1257 $$ = new InterfaceIndexer ((Expression) $3, (Parameters) $6, do_get, do_set,
1258 (bool) $2, (Attributes) $1, lexer.Location);
1262 operator_declaration
1263 : opt_attributes opt_modifiers operator_declarator operator_body
1265 OperatorDeclaration decl = (OperatorDeclaration) $3;
1267 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
1268 decl.arg2type, decl.arg2name, (Block) $4, (Attributes) $1, decl.location);
1270 // Note again, checking is done in semantic analysis
1271 current_container.AddOperator (op);
1273 current_local_parameters = null;
1279 | SEMICOLON { $$ = null; }
1282 : type OPERATOR overloadable_operator
1283 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1285 Operator.OpType op = (Operator.OpType) $3;
1286 CheckUnaryOperator (op);
1288 if (op == Operator.OpType.Addition)
1289 op = Operator.OpType.UnaryPlus;
1291 if (op == Operator.OpType.Subtraction)
1292 op = Operator.OpType.UnaryNegation;
1294 Parameter [] pars = new Parameter [1];
1296 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1298 current_local_parameters = new Parameters (pars, null, lexer.Location);
1300 $$ = new OperatorDeclaration (op, (Expression) $1, (Expression) $5, (string) $6,
1301 null, null, lexer.Location);
1303 | type OPERATOR overloadable_operator
1305 type IDENTIFIER COMMA
1309 CheckBinaryOperator ((Operator.OpType) $3);
1311 Parameter [] pars = new Parameter [2];
1313 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1314 pars [1] = new Parameter ((Expression) $8, (string) $9, Parameter.Modifier.NONE, null);
1316 current_local_parameters = new Parameters (pars, null, lexer.Location);
1318 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1319 (Expression) $5, (string) $6,
1320 (Expression) $8, (string) $9, lexer.Location);
1322 | conversion_operator_declarator
1325 overloadable_operator
1327 : BANG { $$ = Operator.OpType.LogicalNot; }
1328 | TILDE { $$ = Operator.OpType.OnesComplement; }
1329 | OP_INC { $$ = Operator.OpType.Increment; }
1330 | OP_DEC { $$ = Operator.OpType.Decrement; }
1331 | TRUE { $$ = Operator.OpType.True; }
1332 | FALSE { $$ = Operator.OpType.False; }
1333 // Unary and binary:
1334 | PLUS { $$ = Operator.OpType.Addition; }
1335 | MINUS { $$ = Operator.OpType.Subtraction; }
1337 | STAR { $$ = Operator.OpType.Multiply; }
1338 | DIV { $$ = Operator.OpType.Division; }
1339 | PERCENT { $$ = Operator.OpType.Modulus; }
1340 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1341 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1342 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1343 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1344 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1345 | OP_EQ { $$ = Operator.OpType.Equality; }
1346 | OP_NE { $$ = Operator.OpType.Inequality; }
1347 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1348 | OP_LT { $$ = Operator.OpType.LessThan; }
1349 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1350 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1353 conversion_operator_declarator
1354 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1356 Parameter [] pars = new Parameter [1];
1358 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1360 current_local_parameters = new Parameters (pars, null, lexer.Location);
1362 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1363 null, null, lexer.Location);
1365 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1367 Parameter [] pars = new Parameter [1];
1369 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1371 current_local_parameters = new Parameters (pars, null, lexer.Location);
1373 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1374 null, null, lexer.Location);
1378 syntax_error (lexer.Location, "'operator' expected");
1382 syntax_error (lexer.Location, "'operator' expected");
1386 constructor_declaration
1389 constructor_declarator
1392 Constructor c = (Constructor) $3;
1393 c.Block = (Block) $4;
1394 c.OptAttributes = (Attributes) $1;
1395 c.ModFlags = (int) $2;
1397 if (c.Name == current_container.Basename){
1398 if ((c.ModFlags & Modifiers.STATIC) != 0){
1399 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1401 515, c.Location, String.Format (
1402 "`{0}.{1}': static constructor can not have access modifiers",
1403 c.Name, current_container.Name));
1406 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1408 if (c.Initializer != null){
1411 "Static constructors can not have an explicit this or base " +
1412 "constructor invocations");
1415 if (!c.Parameters.Empty){
1417 132, c.Location, "Static constructors should not have parameters");
1420 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1423 // We let another layer check the validity of the constructor.
1424 Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1427 CheckDef (current_container.AddConstructor (c), c.Name, c.Location);
1429 current_local_parameters = null;
1433 constructor_declarator
1435 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1437 oob_stack.Push (lexer.Location);
1439 current_local_parameters = (Parameters) $3;
1441 opt_constructor_initializer
1443 Location l = (Location) oob_stack.Pop ();
1444 $$ = new Constructor ((string) $1, (Parameters) $3, (ConstructorInitializer) $6, l);
1450 | SEMICOLON { $$ = null; }
1453 opt_constructor_initializer
1454 : /* empty */ { $$ = null; }
1455 | constructor_initializer
1458 constructor_initializer
1459 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1461 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1463 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1465 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1469 destructor_declaration
1470 : opt_attributes TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1472 if ((string) $3 != current_container.Basename){
1473 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1474 } else if (!(current_container is Class)){
1475 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1477 Location l = lexer.Location;
1480 if (!RootContext.StdLib && current_container.Name == "System.Object")
1481 m = Modifiers.PROTECTED | Modifiers.VIRTUAL;
1483 m = Modifiers.PROTECTED | Modifiers.OVERRIDE;
1485 Method d = new Method (
1486 TypeManager.system_void_expr, m, "Finalize",
1487 new Parameters (null, null, l), (Attributes) $1, l);
1489 d.Block = (Block) $6;
1490 CheckDef (current_container.AddMethod (d), d.Name, d.Location);
1498 EVENT type variable_declarators SEMICOLON
1500 foreach (VariableDeclaration var in (ArrayList) $5) {
1502 Event e = new Event ((Expression) $4, var.identifier,
1503 var.expression_or_array_initializer,
1504 (int) $2, null, null, (Attributes) $1, lexer.Location);
1506 CheckDef (current_container.AddEvent (e), e.Name, e.Location);
1512 EVENT type member_name
1515 implicit_value_parameter_type = (Expression) $4;
1516 lexer.EventParsing = true;
1517 oob_stack.Push (lexer.Location);
1519 event_accessor_declarations
1521 lexer.EventParsing = false;
1525 Location loc = (Location) oob_stack.Pop ();
1527 Pair pair = (Pair) $8;
1528 Accessor add_accessor = null;
1529 Accessor rem_accessor = null;
1531 if (pair.First != null)
1532 add_accessor = (Accessor) pair.First;
1533 if (pair.Second != null)
1534 rem_accessor = (Accessor) pair.Second;
1536 Event e = new Event ((Expression) $4, (string) $5, null, (int) $2, add_accessor, rem_accessor,
1537 (Attributes) $1, loc);
1539 CheckDef (current_container.AddEvent (e), e.Name, loc);
1540 implicit_value_parameter_type = null;
1544 event_accessor_declarations
1545 : add_accessor_declaration remove_accessor_declaration
1547 $$ = new Pair ($1, $2);
1549 | remove_accessor_declaration add_accessor_declaration
1551 $$ = new Pair ($2, $1);
1555 add_accessor_declaration
1556 : opt_attributes ADD
1558 Parameter [] args = new Parameter [1];
1559 Parameter implicit_value_parameter = new Parameter (
1560 implicit_value_parameter_type, "value",
1561 Parameter.Modifier.NONE, null);
1563 args [0] = implicit_value_parameter;
1565 current_local_parameters = new Parameters (args, null, lexer.Location);
1566 lexer.EventParsing = false;
1570 $$ = new Accessor ((Block) $4, (Attributes) $1);
1571 lexer.EventParsing = true;
1575 remove_accessor_declaration
1576 : opt_attributes REMOVE
1578 Parameter [] args = new Parameter [1];
1579 Parameter implicit_value_parameter = new Parameter (
1580 implicit_value_parameter_type, "value",
1581 Parameter.Modifier.NONE, null);
1583 args [0] = implicit_value_parameter;
1585 current_local_parameters = new Parameters (args, null, lexer.Location);
1586 lexer.EventParsing = false;
1590 $$ = new Accessor ((Block) $4, (Attributes) $1);
1591 lexer.EventParsing = true;
1596 : opt_attributes opt_modifiers indexer_declarator
1599 IndexerDeclaration decl = (IndexerDeclaration) $3;
1601 implicit_value_parameter_type = decl.type;
1603 lexer.PropertyParsing = true;
1604 parsing_indexer = true;
1606 indexer_parameters = decl.param_list;
1607 oob_stack.Push (lexer.Location);
1609 accessor_declarations
1611 lexer.PropertyParsing = false;
1612 parsing_indexer = false;
1616 // The signature is computed from the signature of the indexer. Look
1617 // at section 3.6 on the spec
1618 Location loc = (Location) oob_stack.Pop ();
1620 IndexerDeclaration decl = (IndexerDeclaration) $3;
1621 Pair pair = (Pair) $6;
1622 Accessor get_block = (Accessor) pair.First;
1623 Accessor set_block = (Accessor) pair.Second;
1625 indexer = new Indexer (decl.type, decl.interface_type, (int) $2, decl.param_list,
1626 get_block, set_block, (Attributes) $1, loc);
1628 // Note that there is no equivalent of CheckDef for this case
1629 // We shall handle this in semantic analysis
1631 current_container.AddIndexer (indexer);
1633 current_local_parameters = null;
1634 implicit_value_parameter_type = null;
1635 indexer_parameters = null;
1640 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1642 Parameters pars = (Parameters) $4;
1644 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1645 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1648 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
1650 | type qualified_identifier DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1652 Parameters pars = (Parameters) $6;
1654 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1655 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1657 $$ = new IndexerDeclaration ((Expression) $1, (string) $2, pars);
1669 Location enum_location = lexer.Location;
1671 string full_name = MakeName ((string) $4);
1672 Enum e = new Enum (current_container, (Expression) $5, (int) $2, full_name,
1673 (Attributes) $1, enum_location);
1675 foreach (VariableDeclaration ev in (ArrayList) $6) {
1676 Location loc = (Location) ev.Location;
1678 CheckDef (e.AddEnumMember (ev.identifier,
1679 (Expression) ev.expression_or_array_initializer,
1680 loc, ev.OptAttributes),
1681 ev.identifier, loc);
1684 e.Namespace = current_namespace;
1686 CheckDef (current_container.AddEnum (e), full_name, enum_location);
1687 RootContext.Tree.RecordDecl (full_name, e);
1693 : /* empty */ { $$ = TypeManager.system_int32_expr; }
1694 | COLON type { $$ = $2; }
1698 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1704 opt_enum_member_declarations
1705 : /* empty */ { $$ = new ArrayList (); }
1706 | enum_member_declarations opt_comma { $$ = $1; }
1709 enum_member_declarations
1710 : enum_member_declaration
1712 ArrayList l = new ArrayList ();
1717 | enum_member_declarations COMMA enum_member_declaration
1719 ArrayList l = (ArrayList) $1;
1727 enum_member_declaration
1728 : opt_attributes IDENTIFIER
1730 $$ = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
1732 | opt_attributes IDENTIFIER
1734 $$ = lexer.Location;
1738 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
1742 delegate_declaration
1746 IDENTIFIER OPEN_PARENS
1747 opt_formal_parameter_list
1751 Location l = lexer.Location;
1752 Delegate del = new Delegate (current_container, (Expression) $4, (int) $2,
1753 MakeName ((string) $5), (Parameters) $7,
1754 (Attributes) $1, l);
1756 del.Namespace = current_namespace;
1757 CheckDef (current_container.AddDelegate (del), del.Name, l);
1762 IDENTIFIER OPEN_PARENS
1763 opt_formal_parameter_list
1767 Location l = lexer.Location;
1768 Delegate del = new Delegate (
1770 TypeManager.system_void_expr, (int) $2, MakeName ((string) $5),
1771 (Parameters) $7, (Attributes) $1, l);
1773 del.Namespace = current_namespace;
1774 CheckDef (current_container.AddDelegate (del), del.Name, l);
1779 : namespace_or_type_name
1782 namespace_or_type_name
1783 : qualified_identifier
1787 * Before you think of adding a return_type, notice that we have been
1788 * using two rules in the places where it matters (one rule using type
1789 * and another identical one that uses VOID as the return type). This
1790 * gets rid of a shift/reduce couple
1793 : type_name { /* class_type */
1795 This does interfaces, delegates, struct_types, class_types,
1796 parent classes, and more! 4.2
1798 $$ = DecomposeQI ((string) $1, lexer.Location);
1810 // Note that here only unmanaged types are allowed but we
1811 // can't perform checks during this phase - we do it during
1812 // semantic analysis.
1814 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1818 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
1824 | non_expression_type rank_specifier
1826 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1828 | non_expression_type STAR
1830 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1832 | expression rank_specifiers
1834 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1838 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1842 // We need this because the parser will happily go and reduce IDENTIFIER STAR
1843 // through this different path
1845 | multiplicative_expression STAR
1847 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1854 ArrayList types = new ArrayList ();
1859 | type_list COMMA type
1861 ArrayList types = (ArrayList) $1;
1869 * replaces all the productions for isolating the various
1870 * simple types, but we need this to reuse it easily in local_variable_type
1873 : OBJECT { $$ = TypeManager.system_object_expr; }
1874 | STRING { $$ = TypeManager.system_string_expr; }
1875 | BOOL { $$ = TypeManager.system_boolean_expr; }
1876 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
1877 | FLOAT { $$ = TypeManager.system_single_expr; }
1878 | DOUBLE { $$ = TypeManager.system_double_expr; }
1883 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
1884 | BYTE { $$ = TypeManager.system_byte_expr; }
1885 | SHORT { $$ = TypeManager.system_int16_expr; }
1886 | USHORT { $$ = TypeManager.system_uint16_expr; }
1887 | INT { $$ = TypeManager.system_int32_expr; }
1888 | UINT { $$ = TypeManager.system_uint32_expr; }
1889 | LONG { $$ = TypeManager.system_int64_expr; }
1890 | ULONG { $$ = TypeManager.system_uint64_expr; }
1891 | CHAR { $$ = TypeManager.system_char_expr; }
1892 | VOID { $$ = TypeManager.system_void_expr; }
1900 : type rank_specifiers
1902 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1907 // Expressions, section 7.5
1915 | qualified_identifier
1917 string name = (string) $1;
1920 $$ = DecomposeQI (name, lexer.Location);
1922 | parenthesized_expression
1924 | invocation_expression
1928 | post_increment_expression
1929 | post_decrement_expression
1933 | checked_expression
1934 | unchecked_expression
1935 | pointer_member_access
1942 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
1943 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
1944 | NULL { $$ = NullLiteral.Null; }
1948 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
1949 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
1950 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
1955 object v = lexer.Value;
1958 $$ = new IntLiteral ((Int32) v);
1960 $$ = new UIntLiteral ((UInt32) v);
1962 $$ = new LongLiteral ((Int64) v);
1963 else if (v is ulong)
1964 $$ = new ULongLiteral ((UInt64) v);
1966 Console.WriteLine ("OOPS. Unexpected result from scanner");
1971 : TRUE { $$ = new BoolLiteral (true); }
1972 | FALSE { $$ = new BoolLiteral (false); }
1975 parenthesized_expression
1976 : OPEN_PARENS expression CLOSE_PARENS
1981 : primary_expression DOT IDENTIFIER
1983 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1985 | predefined_type DOT IDENTIFIER
1987 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1995 invocation_expression
1996 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
1999 Location l = lexer.Location;
2000 Report.Error (1, l, "Parse error");
2002 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2007 : /* empty */ { $$ = null; }
2014 ArrayList list = new ArrayList ();
2018 | argument_list COMMA argument
2020 ArrayList list = (ArrayList) $1;
2029 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2031 | REF variable_reference
2033 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2035 | OUT variable_reference
2037 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2042 : expression { note ("section 5.4"); $$ = $1; }
2046 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2048 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2050 | primary_expression rank_specifiers
2052 // So the super-trick is that primary_expression
2053 // can only be either a SimpleName or a MemberAccess.
2054 // The MemberAccess case arises when you have a fully qualified type-name like :
2056 // SimpleName is when you have
2059 Expression expr = (Expression) $1;
2060 if (!(expr is SimpleName || expr is MemberAccess)) {
2061 Error_ExpectingTypeName (lexer.Location, expr);
2062 $$ = TypeManager.system_object_expr;
2065 // So we extract the string corresponding to the SimpleName
2068 $$ = new SimpleName (GetQualifiedIdentifier (expr) + (string) $2, lexer.Location);
2076 ArrayList list = new ArrayList ();
2080 | expression_list COMMA expression
2082 ArrayList list = (ArrayList) $1;
2091 $$ = new This (current_block, lexer.Location);
2096 : BASE DOT IDENTIFIER
2098 $$ = new BaseAccess ((string) $3, lexer.Location);
2100 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2102 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2106 post_increment_expression
2107 : primary_expression OP_INC
2109 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2110 (Expression) $1, lexer.Location);
2114 post_decrement_expression
2115 : primary_expression OP_DEC
2117 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2118 (Expression) $1, lexer.Location);
2123 : object_or_delegate_creation_expression
2124 | array_creation_expression
2127 object_or_delegate_creation_expression
2128 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2130 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2134 array_creation_expression
2135 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2137 opt_array_initializer
2139 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2141 | NEW type rank_specifiers array_initializer
2143 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2147 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2163 : rank_specifier opt_rank_specifier
2165 $$ = (string) $2 + (string) $1;
2170 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2172 $$ = "[" + (string) $2 + "]";
2192 | dim_separators COMMA
2194 $$ = (string) $1 + ",";
2198 opt_array_initializer
2210 : OPEN_BRACE CLOSE_BRACE
2212 ArrayList list = new ArrayList ();
2215 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2217 $$ = (ArrayList) $2;
2221 variable_initializer_list
2222 : variable_initializer
2224 ArrayList list = new ArrayList ();
2228 | variable_initializer_list COMMA variable_initializer
2230 ArrayList list = (ArrayList) $1;
2237 : TYPEOF OPEN_PARENS type CLOSE_PARENS
2239 $$ = new TypeOf ((Expression) $3, lexer.Location);
2244 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2245 $$ = new SizeOf ((Expression) $3, lexer.Location);
2250 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2252 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2256 unchecked_expression
2257 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2259 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2263 pointer_member_access
2264 : primary_expression OP_PTR IDENTIFIER
2268 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2269 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2273 : primary_expression
2274 | BANG prefixed_unary_expression
2276 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2278 | TILDE prefixed_unary_expression
2280 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2286 : OPEN_PARENS expression CLOSE_PARENS unary_expression
2288 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2290 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2292 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2297 // The idea to split this out is from Rhys' grammar
2298 // to solve the problem with casts.
2300 prefixed_unary_expression
2302 | PLUS prefixed_unary_expression
2304 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2306 | MINUS prefixed_unary_expression
2308 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2310 | OP_INC prefixed_unary_expression
2312 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2313 (Expression) $2, lexer.Location);
2315 | OP_DEC prefixed_unary_expression
2317 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2318 (Expression) $2, lexer.Location);
2320 | STAR prefixed_unary_expression
2322 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2324 | BITWISE_AND prefixed_unary_expression
2326 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2330 pre_increment_expression
2331 : OP_INC prefixed_unary_expression
2333 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2334 (Expression) $2, lexer.Location);
2338 pre_decrement_expression
2339 : OP_DEC prefixed_unary_expression
2341 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2342 (Expression) $2, lexer.Location);
2346 multiplicative_expression
2347 : prefixed_unary_expression
2348 | multiplicative_expression STAR prefixed_unary_expression
2350 $$ = new Binary (Binary.Operator.Multiply,
2351 (Expression) $1, (Expression) $3, lexer.Location);
2353 | multiplicative_expression DIV prefixed_unary_expression
2355 $$ = new Binary (Binary.Operator.Division,
2356 (Expression) $1, (Expression) $3, lexer.Location);
2358 | multiplicative_expression PERCENT prefixed_unary_expression
2360 $$ = new Binary (Binary.Operator.Modulus,
2361 (Expression) $1, (Expression) $3, lexer.Location);
2366 : multiplicative_expression
2367 | additive_expression PLUS multiplicative_expression
2369 $$ = new Binary (Binary.Operator.Addition,
2370 (Expression) $1, (Expression) $3, lexer.Location);
2372 | additive_expression MINUS multiplicative_expression
2374 $$ = new Binary (Binary.Operator.Subtraction,
2375 (Expression) $1, (Expression) $3, lexer.Location);
2380 : additive_expression
2381 | shift_expression OP_SHIFT_LEFT additive_expression
2383 $$ = new Binary (Binary.Operator.LeftShift,
2384 (Expression) $1, (Expression) $3, lexer.Location);
2386 | shift_expression OP_SHIFT_RIGHT additive_expression
2388 $$ = new Binary (Binary.Operator.RightShift,
2389 (Expression) $1, (Expression) $3, lexer.Location);
2393 relational_expression
2395 | relational_expression OP_LT shift_expression
2397 $$ = new Binary (Binary.Operator.LessThan,
2398 (Expression) $1, (Expression) $3, lexer.Location);
2400 | relational_expression OP_GT shift_expression
2402 $$ = new Binary (Binary.Operator.GreaterThan,
2403 (Expression) $1, (Expression) $3, lexer.Location);
2405 | relational_expression OP_LE shift_expression
2407 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2408 (Expression) $1, (Expression) $3, lexer.Location);
2410 | relational_expression OP_GE shift_expression
2412 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2413 (Expression) $1, (Expression) $3, lexer.Location);
2415 | relational_expression IS type
2417 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
2419 | relational_expression AS type
2421 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
2426 : relational_expression
2427 | equality_expression OP_EQ relational_expression
2429 $$ = new Binary (Binary.Operator.Equality,
2430 (Expression) $1, (Expression) $3, lexer.Location);
2432 | equality_expression OP_NE relational_expression
2434 $$ = new Binary (Binary.Operator.Inequality,
2435 (Expression) $1, (Expression) $3, lexer.Location);
2440 : equality_expression
2441 | and_expression BITWISE_AND equality_expression
2443 $$ = new Binary (Binary.Operator.BitwiseAnd,
2444 (Expression) $1, (Expression) $3, lexer.Location);
2448 exclusive_or_expression
2450 | exclusive_or_expression CARRET and_expression
2452 $$ = new Binary (Binary.Operator.ExclusiveOr,
2453 (Expression) $1, (Expression) $3, lexer.Location);
2457 inclusive_or_expression
2458 : exclusive_or_expression
2459 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2461 $$ = new Binary (Binary.Operator.BitwiseOr,
2462 (Expression) $1, (Expression) $3, lexer.Location);
2466 conditional_and_expression
2467 : inclusive_or_expression
2468 | conditional_and_expression OP_AND inclusive_or_expression
2470 $$ = new Binary (Binary.Operator.LogicalAnd,
2471 (Expression) $1, (Expression) $3, lexer.Location);
2475 conditional_or_expression
2476 : conditional_and_expression
2477 | conditional_or_expression OP_OR conditional_and_expression
2479 $$ = new Binary (Binary.Operator.LogicalOr,
2480 (Expression) $1, (Expression) $3, lexer.Location);
2484 conditional_expression
2485 : conditional_or_expression
2486 | conditional_or_expression INTERR expression COLON expression
2488 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2492 assignment_expression
2493 : prefixed_unary_expression ASSIGN expression
2495 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2497 | prefixed_unary_expression OP_MULT_ASSIGN expression
2499 Location l = lexer.Location;
2501 $$ = new CompoundAssign (
2502 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
2504 | prefixed_unary_expression OP_DIV_ASSIGN expression
2506 Location l = lexer.Location;
2508 $$ = new CompoundAssign (
2509 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
2511 | prefixed_unary_expression OP_MOD_ASSIGN expression
2513 Location l = lexer.Location;
2515 $$ = new CompoundAssign (
2516 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
2518 | prefixed_unary_expression OP_ADD_ASSIGN expression
2520 Location l = lexer.Location;
2522 $$ = new CompoundAssign (
2523 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
2525 | prefixed_unary_expression OP_SUB_ASSIGN expression
2527 Location l = lexer.Location;
2529 $$ = new CompoundAssign (
2530 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
2532 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2534 Location l = lexer.Location;
2536 $$ = new CompoundAssign (
2537 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
2539 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2541 Location l = lexer.Location;
2543 $$ = new CompoundAssign (
2544 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
2546 | prefixed_unary_expression OP_AND_ASSIGN expression
2548 Location l = lexer.Location;
2550 $$ = new CompoundAssign (
2551 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
2553 | prefixed_unary_expression OP_OR_ASSIGN expression
2555 Location l = lexer.Location;
2557 $$ = new CompoundAssign (
2558 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
2560 | prefixed_unary_expression OP_XOR_ASSIGN expression
2562 Location l = lexer.Location;
2564 $$ = new CompoundAssign (
2565 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
2570 : conditional_expression
2571 | assignment_expression
2593 name = MakeName ((string) $4);
2595 new_class = new Class (current_container, name, (int) $2,
2596 (Attributes) $1, lexer.Location);
2597 current_container = new_class;
2598 current_container.Namespace = current_namespace;
2599 RootContext.Tree.RecordDecl (name, new_class);
2605 Class new_class = (Class) current_container;
2608 new_class.Bases = (ArrayList) $6;
2610 current_container = current_container.Parent;
2611 CheckDef (current_container.AddClass (new_class), new_class.Name, new_class.Location);
2618 : /* empty */ { $$ = (int) 0; }
2624 | modifiers modifier
2629 if ((m1 & m2) != 0) {
2630 Location l = lexer.Location;
2631 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2633 $$ = (int) (m1 | m2);
2638 : NEW { $$ = Modifiers.NEW; }
2639 | PUBLIC { $$ = Modifiers.PUBLIC; }
2640 | PROTECTED { $$ = Modifiers.PROTECTED; }
2641 | INTERNAL { $$ = Modifiers.INTERNAL; }
2642 | PRIVATE { $$ = Modifiers.PRIVATE; }
2643 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2644 | SEALED { $$ = Modifiers.SEALED; }
2645 | STATIC { $$ = Modifiers.STATIC; }
2646 | READONLY { $$ = Modifiers.READONLY; }
2647 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2648 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2649 | EXTERN { $$ = Modifiers.EXTERN; }
2650 | VOLATILE { $$ = Modifiers.VOLATILE; }
2651 | UNSAFE { $$ = Modifiers.UNSAFE; }
2655 : /* empty */ { $$ = null; }
2656 | class_base { $$ = $1; }
2660 : COLON type_list { $$ = $2; }
2668 // A block is "contained" on the following places:
2670 // property_declaration as part of the accessor body (get/set)
2671 // operator_declaration
2672 // constructor_declaration
2673 // destructor_declaration
2674 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2679 current_block = new Block (current_block, current_local_parameters,
2680 lexer.Location, Location.Null);
2682 opt_statement_list CLOSE_BRACE
2684 while (current_block.Implicit)
2685 current_block = current_block.Parent;
2687 current_block.SetEndLocation (lexer.Location);
2688 current_block = current_block.Parent;
2699 | statement_list statement
2703 : declaration_statement
2705 if ($1 != null && (Block) $1 != current_block){
2706 current_block.AddStatement ((Statement) $1);
2707 current_block = (Block) $1;
2710 | embedded_statement
2712 Statement s = (Statement) $1;
2715 current_block.AddStatement ((Statement) $1);
2723 | expression_statement
2724 | selection_statement
2725 | iteration_statement
2729 | unchecked_statement
2739 $$ = new EmptyStatement ();
2746 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
2748 if (!current_block.AddLabel ((string) $1, labeled)){
2749 Location l = lexer.Location;
2750 Report.Error (140, l, "The label '" + ((string) $1) + "' is a duplicate");
2752 current_block.AddStatement (labeled);
2757 declaration_statement
2758 : local_variable_declaration SEMICOLON
2761 DictionaryEntry de = (DictionaryEntry) $1;
2763 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
2767 | local_constant_declaration SEMICOLON
2770 DictionaryEntry de = (DictionaryEntry) $1;
2772 $$ = declare_local_constant ((Expression) de.Key, (VariableDeclaration) de.Value);
2778 * The following is from Rhys' grammar:
2779 * > Types in local variable declarations must be recognized as
2780 * > expressions to prevent reduce/reduce errors in the grammar.
2781 * > The expressions are converted into types during semantic analysis.
2784 : primary_expression opt_rank_specifier
2786 // FIXME: Do something smart here regarding the composition of the type.
2788 // Ok, the above "primary_expression" is there to get rid of
2789 // both reduce/reduce and shift/reduces in the grammar, it should
2790 // really just be "type_name". If you use type_name, a reduce/reduce
2791 // creeps up. If you use qualified_identifier (which is all we need
2792 // really) two shift/reduces appear.
2795 // So the super-trick is that primary_expression
2796 // can only be either a SimpleName or a MemberAccess.
2797 // The MemberAccess case arises when you have a fully qualified type-name like :
2799 // SimpleName is when you have
2802 Expression expr = (Expression) $1;
2803 if (!(expr is SimpleName || expr is MemberAccess)) {
2804 Error_ExpectingTypeName (lexer.Location, expr);
2808 // So we extract the string corresponding to the SimpleName
2811 if ((string) $2 == "")
2814 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2817 | builtin_types opt_rank_specifier
2819 if ((string) $2 == "")
2822 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2826 local_variable_pointer_type
2827 : primary_expression STAR
2829 Expression expr = (Expression) $1;
2830 Location l = lexer.Location;
2832 if (!(expr is SimpleName || expr is MemberAccess)) {
2833 Error_ExpectingTypeName (l, expr);
2837 $$ = new ComposedCast ((Expression) $1, "*", l);
2839 | builtin_types STAR
2841 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
2845 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
2847 | local_variable_pointer_type STAR
2849 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2853 local_variable_declaration
2854 : local_variable_type variable_declarators
2857 $$ = new DictionaryEntry ($1, $2);
2861 | local_variable_pointer_type opt_rank_specifier variable_declarators
2866 if ((string) $2 == "")
2867 t = (Expression) $1;
2869 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2870 $$ = new DictionaryEntry (t, $3);
2876 local_constant_declaration
2877 : CONST local_variable_type constant_declarator
2880 $$ = new DictionaryEntry ($2, $3);
2886 expression_statement
2887 : statement_expression SEMICOLON
2894 // We have to do the wrapping here and not in the case above,
2895 // because statement_expression is used for example in for_statement
2897 statement_expression
2898 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2899 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2900 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2901 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2902 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2903 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2904 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2906 Report.Error (1002, lexer.Location, "Expecting `;'");
2910 object_creation_expression
2911 : object_or_delegate_creation_expression
2912 { note ("complain if this is a delegate maybe?"); }
2921 : if_statement_open if_statement_rest
2930 oob_stack.Push (lexer.Location);
2935 : boolean_expression CLOSE_PARENS
2938 Location l = (Location) oob_stack.Pop ();
2940 $$ = new If ((Expression) $1, (Statement) $3, l);
2942 if (RootContext.WarningLevel >= 3){
2943 if ($3 is EmptyStatement)
2944 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
2948 | boolean_expression CLOSE_PARENS
2949 embedded_statement ELSE embedded_statement
2951 Location l = (Location) oob_stack.Pop ();
2953 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
2958 : SWITCH OPEN_PARENS
2960 oob_stack.Push (lexer.Location);
2961 switch_stack.Push (current_block);
2963 expression CLOSE_PARENS
2966 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
2967 current_block = (Block) switch_stack.Pop ();
2983 Report.Error (1522, lexer.Location, "Empty switch block");
2991 ArrayList sections = new ArrayList ();
2996 | switch_sections switch_section
2998 ArrayList sections = (ArrayList) $1;
3008 current_block = new Block (current_block, lexer.Location, lexer.Location);
3012 Block topmost = current_block;
3014 while (topmost.Implicit)
3015 topmost = topmost.Parent;
3016 $$ = new SwitchSection ((ArrayList) $1, topmost);
3023 ArrayList labels = new ArrayList ();
3028 | switch_labels switch_label
3030 ArrayList labels = (ArrayList) ($1);
3038 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3039 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3042 1523, lexer.Location,
3043 "The keyword case or default must precede code in switch block");
3057 oob_stack.Push (lexer.Location);
3059 boolean_expression CLOSE_PARENS embedded_statement
3061 Location l = (Location) oob_stack.Pop ();
3062 $$ = new While ((Expression) $4, (Statement) $6, l);
3064 if (RootContext.WarningLevel >= 3){
3065 if ($6 is EmptyStatement)
3066 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3072 : DO embedded_statement
3074 oob_stack.Push (lexer.Location);
3076 boolean_expression CLOSE_PARENS SEMICOLON
3078 Location l = (Location) oob_stack.Pop ();
3080 $$ = new Do ((Statement) $2, (Expression) $6, l);
3086 opt_for_initializer SEMICOLON
3088 Block assign_block = new Block (current_block);
3089 current_block = assign_block;
3091 if ($3 is DictionaryEntry){
3092 DictionaryEntry de = (DictionaryEntry) $3;
3094 Expression type = (Expression) de.Key;
3095 ArrayList var_declarators = (ArrayList) de.Value;
3097 foreach (VariableDeclaration decl in var_declarators){
3101 vi = current_block.AddVariable (
3102 type, decl.identifier, current_local_parameters, decl.Location);
3106 Location l = lexer.Location;
3108 if (decl.expression_or_array_initializer is Expression){
3109 expr = (Expression) decl.expression_or_array_initializer;
3110 } else if (decl.expression_or_array_initializer == null) {
3113 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3114 expr = new ArrayCreation (type, "", init, decl.Location);
3117 LocalVariableReference var;
3118 var = new LocalVariableReference (assign_block, decl.identifier, l);
3121 Assign a = new Assign (var, expr, decl.Location);
3123 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3129 oob_stack.Push (lexer.Location);
3131 opt_for_condition SEMICOLON
3132 opt_for_iterator CLOSE_PARENS
3135 Location l = (Location) oob_stack.Pop ();
3137 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3139 if (RootContext.WarningLevel >= 3){
3140 if ($10 is EmptyStatement)
3141 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3144 current_block.AddStatement (f);
3145 while (current_block.Implicit)
3146 current_block = current_block.Parent;
3148 current_block = current_block.Parent;
3153 : /* empty */ { $$ = new EmptyStatement (); }
3158 : local_variable_declaration
3159 | statement_expression_list
3163 : /* empty */ { $$ = null; }
3164 | boolean_expression
3168 : /* empty */ { $$ = new EmptyStatement (); }
3173 : statement_expression_list
3176 statement_expression_list
3177 : statement_expression
3179 // CHANGE: was `null'
3180 Block b = new Block (current_block, true);
3182 b.AddStatement ((Statement) $1);
3185 | statement_expression_list COMMA statement_expression
3187 Block b = (Block) $1;
3189 b.AddStatement ((Statement) $3);
3195 : FOREACH OPEN_PARENS type IDENTIFIER IN
3197 oob_stack.Push (lexer.Location);
3199 expression CLOSE_PARENS
3201 oob_stack.Push (current_block);
3203 Block foreach_block = new Block (current_block, true);
3204 LocalVariableReference v = null;
3205 Location l = lexer.Location;
3208 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3212 // Get a writable reference to this read-only variable.
3213 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3215 current_block = foreach_block;
3218 oob_stack.Push (current_block);
3222 Block foreach_block = (Block) oob_stack.Pop ();
3223 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3224 Block prev_block = (Block) oob_stack.Pop ();
3225 Location l = (Location) oob_stack.Pop ();
3227 current_block = prev_block;
3230 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3231 foreach_block.AddStatement (f);
3240 | continue_statement
3249 $$ = new Break (lexer.Location);
3254 : CONTINUE SEMICOLON
3256 $$ = new Continue (lexer.Location);
3261 : GOTO IDENTIFIER SEMICOLON
3263 $$ = new Goto (current_block, (string) $2, lexer.Location);
3265 | GOTO CASE constant_expression SEMICOLON
3267 $$ = new GotoCase ((Expression) $3, lexer.Location);
3269 | GOTO DEFAULT SEMICOLON
3271 $$ = new GotoDefault (lexer.Location);
3276 : RETURN opt_expression SEMICOLON
3278 $$ = new Return ((Expression) $2, lexer.Location);
3283 : THROW opt_expression SEMICOLON
3285 $$ = new Throw ((Expression) $2, lexer.Location);
3295 : TRY block catch_clauses
3298 ArrayList s = new ArrayList ();
3300 foreach (Catch cc in (ArrayList) $3) {
3307 // Now s contains the list of specific catch clauses
3308 // and g contains the general one.
3310 $$ = new Try ((Block) $2, s, g, null, lexer.Location);
3312 | TRY block opt_catch_clauses FINALLY block
3315 ArrayList s = new ArrayList ();
3316 ArrayList catch_list = (ArrayList) $3;
3318 if (catch_list != null){
3319 foreach (Catch cc in catch_list) {
3327 $$ = new Try ((Block) $2, s, g, (Block) $5, lexer.Location);
3331 Report.Error (1524, lexer.Location, "Expected catch or finally");
3336 : /* empty */ { $$ = null; }
3343 ArrayList l = new ArrayList ();
3348 | catch_clauses catch_clause
3350 ArrayList l = (ArrayList) $1;
3358 : /* empty */ { $$ = null; }
3363 : CATCH opt_catch_args
3365 Expression type = null;
3369 DictionaryEntry cc = (DictionaryEntry) $2;
3370 type = (Expression) cc.Key;
3371 id = (string) cc.Value;
3374 ArrayList one = new ArrayList ();
3375 Location loc = lexer.Location;
3377 one.Add (new VariableDeclaration (id, null, loc));
3380 current_block = new Block (current_block);
3381 Block b = declare_local_variables (type, one, loc);
3386 Expression type = null;
3390 DictionaryEntry cc = (DictionaryEntry) $2;
3391 type = (Expression) cc.Key;
3392 id = (string) cc.Value;
3396 // FIXME: I can change this for an assignment.
3398 while (current_block != (Block) $1)
3399 current_block = current_block.Parent;
3404 $$ = new Catch (type, id , (Block) $4, lexer.Location);
3409 : /* empty */ { $$ = null; }
3414 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3416 $$ = new DictionaryEntry ($2, $3);
3423 $$ = new Checked ((Block) $2);
3430 $$ = new Unchecked ((Block) $2);
3437 if (!RootContext.Unsafe){
3438 Report.Error (227, lexer.Location,
3439 "Unsafe code can only be used if --unsafe is used");
3442 $$ = new Unsafe ((Block) $3);
3448 type fixed_pointer_declarators
3451 Block assign_block = new Block (current_block, true);
3452 ArrayList list = (ArrayList) $4;
3453 Expression type = (Expression) $3;
3454 Location l = lexer.Location;
3455 int top = list.Count;
3457 for (int i = 0; i < top; i++){
3458 Pair p = (Pair) list [i];
3461 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3468 current_block.AddStatement (assign_block);
3469 current_block = assign_block;
3470 oob_stack.Push (assign_block);
3475 Location l = (Location) oob_stack.Pop ();
3476 Block assign_block = (Block) oob_stack.Pop ();
3478 ArrayList list = (ArrayList) $4;
3479 int top = list.Count;
3481 $$ = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
3485 fixed_pointer_declarators
3486 : fixed_pointer_declarator {
3487 ArrayList declarators = new ArrayList ();
3488 declarators.Add ($1);
3491 | fixed_pointer_declarators COMMA fixed_pointer_declarator
3493 ArrayList declarators = (ArrayList) $1;
3494 declarators.Add ($3);
3499 fixed_pointer_declarator
3500 : IDENTIFIER ASSIGN expression
3502 $$ = new Pair ($1, $3);
3507 : LOCK OPEN_PARENS expression CLOSE_PARENS
3513 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
3518 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
3520 Block assign_block = new Block (current_block);
3521 current_block = assign_block;
3523 oob_stack.Push (lexer.Location);
3525 if ($3 is DictionaryEntry){
3526 DictionaryEntry de = (DictionaryEntry) $3;
3527 Location l = lexer.Location;
3529 Expression type = (Expression) de.Key;
3530 ArrayList var_declarators = (ArrayList) de.Value;
3532 ArrayList vars = new ArrayList ();
3534 foreach (VariableDeclaration decl in var_declarators){
3536 VariableInfo vi = current_block.AddVariable (
3537 type, decl.identifier,
3538 current_local_parameters, decl.Location);
3544 if (decl.expression_or_array_initializer is Expression){
3545 expr = (Expression) decl.expression_or_array_initializer;
3547 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3549 expr = new ArrayCreation (type, "", init, decl.Location);
3552 LocalVariableReference var;
3554 // Get a writable reference to this read-only variable.
3555 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
3557 // This is so that it is not a warning on using variables
3560 vars.Add (new DictionaryEntry (var, expr));
3562 // Assign a = new Assign (var, expr, decl.Location);
3563 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3565 $3 = new DictionaryEntry (type, vars);
3570 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
3571 current_block.AddStatement (u);
3572 while (current_block.Implicit)
3573 current_block = current_block.Parent;
3575 current_block = current_block.Parent;
3579 resource_acquisition
3580 : local_variable_declaration
3587 // A class used to pass around variable declarations and constants
3589 public class VariableDeclaration {
3590 public string identifier;
3591 public object expression_or_array_initializer;
3592 public Location Location;
3593 public Attributes OptAttributes;
3595 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
3597 this.identifier = id;
3598 this.expression_or_array_initializer = eoai;
3600 this.OptAttributes = opt_attrs;
3603 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
3609 // A class used to hold info about an indexer declarator
3612 public class IndexerDeclaration {
3613 public Expression type;
3614 public string interface_type;
3615 public Parameters param_list;
3617 public IndexerDeclaration (Expression type, string interface_type, Parameters param_list)
3620 this.interface_type = interface_type;
3621 this.param_list = param_list;
3626 // A class used to hold info about an operator declarator
3629 public class OperatorDeclaration {
3630 public Operator.OpType optype;
3631 public Expression ret_type, arg1type, arg2type;
3632 public string arg1name, arg2name;
3633 public Location location;
3635 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
3636 Expression arg1type, string arg1name,
3637 Expression arg2type, string arg2name, Location location)
3640 this.ret_type = ret_type;
3641 this.arg1type = arg1type;
3642 this.arg1name = arg1name;
3643 this.arg2type = arg2type;
3644 this.arg2name = arg2name;
3645 this.location = location;
3650 void Error_ExpectingTypeName (Location l, Expression expr)
3652 if (expr is Invocation){
3653 Report.Error (1002, l, "; expected");
3655 Report.Error (-1, l, "Invalid Type definition");
3660 // Given the @class_name name, it creates a fully qualified name
3661 // based on the containing declaration space
3664 MakeName (string class_name)
3666 string ns = current_namespace.Name;
3667 string container_name = current_container.Name;
3669 if (container_name == ""){
3671 return ns + "." + class_name;
3675 return container_name + "." + class_name;
3679 // Used to report back to the user the result of a declaration
3680 // in the current declaration space
3683 CheckDef (AdditionResult result, string name, Location l)
3685 if (result == AdditionResult.Success)
3689 case AdditionResult.NameExists:
3690 Report.Error (102, l, "The container `" + current_container.Name +
3691 "' already contains a definition for `"+
3697 // This is handled only for static Constructors, because
3698 // in reality we handle these by the semantic analysis later
3700 case AdditionResult.MethodExists:
3702 111, l, "Class `"+current_container.Name+
3703 "' already defines a member called '" +
3704 name + "' with the same parameter types (more than one default constructor)");
3707 case AdditionResult.EnclosingClash:
3708 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
3711 case AdditionResult.NotAConstructor:
3712 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
3718 CheckDef (bool result, string name, Location l)
3722 CheckDef (AdditionResult.NameExists, name, l);
3725 Expression DecomposeQI (string name, Location loc)
3729 if (name.IndexOf ('.') == -1){
3730 return new SimpleName (name, loc);
3732 int pos = name.LastIndexOf (".");
3733 string left = name.Substring (0, pos);
3734 string right = name.Substring (pos + 1);
3736 o = DecomposeQI (left, loc);
3738 return new MemberAccess (o, right, loc);
3743 // This method is used to get at the complete string representation of
3744 // a fully-qualified type name, hiding inside a MemberAccess ;-)
3745 // This is necessary because local_variable_type admits primary_expression
3746 // as the type of the variable. So we do some extra checking
3748 string GetQualifiedIdentifier (Expression expr)
3750 if (expr is SimpleName)
3751 return ((SimpleName)expr).Name;
3752 else if (expr is MemberAccess)
3753 return GetQualifiedIdentifier (((MemberAccess)expr).Expr) + "." + ((MemberAccess) expr).Identifier;
3755 throw new Exception ("Expr has to be either SimpleName or MemberAccess! (" + expr + ")");
3759 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
3761 Block implicit_block;
3762 ArrayList inits = null;
3765 // We use the `Used' property to check whether statements
3766 // have been added to the current block. If so, we need
3767 // to create another block to contain the new declaration
3768 // otherwise, as an optimization, we use the same block to
3769 // add the declaration.
3771 // FIXME: A further optimization is to check if the statements
3772 // that were added were added as part of the initialization
3773 // below. In which case, no other statements have been executed
3774 // and we might be able to reduce the number of blocks for
3775 // situations like this:
3777 // int j = 1; int k = j + 1;
3779 if (current_block.Used) {
3780 implicit_block = new Block (current_block, true, loc, Location.Null);
3781 implicit_block.AddChildVariableNames (current_block);
3783 implicit_block = current_block;
3785 foreach (VariableDeclaration decl in variable_declarators){
3787 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
3788 if (decl.expression_or_array_initializer != null){
3790 inits = new ArrayList ();
3797 return implicit_block;
3799 foreach (VariableDeclaration decl in inits){
3803 if (decl.expression_or_array_initializer is Expression){
3804 expr = (Expression) decl.expression_or_array_initializer;
3807 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3809 expr = new ArrayCreation (type, "", init, decl.Location);
3812 LocalVariableReference var;
3813 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
3815 assign = new Assign (var, expr, decl.Location);
3817 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
3820 return implicit_block;
3823 Block declare_local_constant (Expression type, VariableDeclaration decl)
3825 Block implicit_block;
3827 if (current_block.Used)
3828 implicit_block = new Block (current_block, true);
3830 implicit_block = current_block;
3832 if (!(implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
3833 current_local_parameters, decl.Location))){
3836 return implicit_block;
3839 void CheckAttributeTarget (string a)
3843 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
3847 Location l = lexer.Location;
3848 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
3854 void CheckUnaryOperator (Operator.OpType op)
3858 case Operator.OpType.LogicalNot:
3859 case Operator.OpType.OnesComplement:
3860 case Operator.OpType.Increment:
3861 case Operator.OpType.Decrement:
3862 case Operator.OpType.True:
3863 case Operator.OpType.False:
3864 case Operator.OpType.Addition:
3865 case Operator.OpType.Subtraction:
3870 Location l = lexer.Location;
3871 Report.Error (1019, l, "Overloadable unary operator expected");
3877 void CheckBinaryOperator (Operator.OpType op)
3881 case Operator.OpType.Addition:
3882 case Operator.OpType.Subtraction:
3883 case Operator.OpType.Multiply:
3884 case Operator.OpType.Division:
3885 case Operator.OpType.Modulus:
3886 case Operator.OpType.BitwiseAnd:
3887 case Operator.OpType.BitwiseOr:
3888 case Operator.OpType.ExclusiveOr:
3889 case Operator.OpType.LeftShift:
3890 case Operator.OpType.RightShift:
3891 case Operator.OpType.Equality:
3892 case Operator.OpType.Inequality:
3893 case Operator.OpType.GreaterThan:
3894 case Operator.OpType.LessThan:
3895 case Operator.OpType.GreaterThanOrEqual:
3896 case Operator.OpType.LessThanOrEqual:
3900 Location l = lexer.Location;
3901 Report.Error (1020, l, "Overloadable binary operator expected");
3907 void syntax_error (Location l, string msg)
3909 Report.Error (1003, l, "Syntax error, " + msg);
3912 void output (string s)
3914 Console.WriteLine (s);
3917 void note (string s)
3919 // Used to put annotations
3924 public Tokenizer Lexer {
3930 public CSharpParser (StreamReader reader, SourceFile file, ArrayList defines)
3932 current_namespace = new Namespace (null, file, "");
3933 this.name = file.Name;
3935 current_container = RootContext.Tree.Types;
3936 current_container.Namespace = current_namespace;
3937 oob_stack = new Stack ();
3938 switch_stack = new Stack ();
3940 lexer = new Tokenizer (reader, file, defines);
3943 public override void parse ()
3946 if (yacc_verbose_flag)
3947 yyparse (lexer, new yydebug.yyDebugSimple ());
3950 Tokenizer tokenizer = lexer as Tokenizer;
3951 tokenizer.cleanup ();
3952 } catch (Exception e){
3953 // Please do not remove this, it is used during debugging
3956 Report.Error (-25, lexer.Location, ": Parsing error ");
3957 Console.WriteLine (e);