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:
23 using System.Collections;
29 public class CSharpParser : GenericParser {
30 Namespace current_namespace;
31 TypeContainer current_container;
34 // Current block is used to add statements as we find
41 // Current interface is used by the various declaration
42 // productions in the interface declaration to "add"
43 // the interfaces as we find them.
45 Interface current_interface;
48 // This is used by the unary_expression code to resolve
49 // a name against a parameter.
51 Parameters current_local_parameters;
54 // Using during property parsing to describe the implicit
55 // value parameter that is passed to the "set" and "get"accesor
56 // methods (properties and indexers).
58 Expression implicit_value_parameter_type;
59 Parameters indexer_parameters;
62 // Used to determine if we are parsing the get/set pair
63 // of an indexer or a property
68 // An out-of-band stack.
79 %token NONE /* This token is never returned by our lexer */
80 %token ERROR // This is used not by the parser, but by the tokenizer.
84 *These are the C# keywords
167 /* C# keywords which are not really keywords */
171 /* C# single character operators/punctuation. */
172 %token OPEN_BRACE "{"
173 %token CLOSE_BRACE "}"
174 %token OPEN_BRACKET "["
175 %token CLOSE_BRACKET "]"
176 %token OPEN_PARENS "("
177 %token CLOSE_PARENS ")"
190 %token BITWISE_AND "&"
191 %token BITWISE_OR "|"
198 /* C# multi-character operators. */
201 %token OP_SHIFT_LEFT "<<"
202 %token OP_SHIFT_RIGHT ">>"
209 %token OP_MULT_ASSIGN "*="
210 %token OP_DIV_ASSIGN "/="
211 %token OP_MOD_ASSIGN "%="
212 %token OP_ADD_ASSIGN "+="
213 %token OP_SUB_ASSIGN "-="
214 %token OP_SHIFT_LEFT_ASSIGN "<<="
215 %token OP_SHIFT_RIGHT_ASSIGN ">>="
216 %token OP_AND_ASSIGN "&="
217 %token OP_XOR_ASSIGN "^="
218 %token OP_OR_ASSIGN "|="
222 %token LITERAL_INTEGER "int literal"
223 %token LITERAL_FLOAT "float literal"
224 %token LITERAL_DOUBLE "double literal"
225 %token LITERAL_DECIMAL "decimal literal"
226 %token LITERAL_CHARACTER "character literal"
227 %token LITERAL_STRING "string literal"
231 /* Add precedence rules to solve dangling else s/r conflict */
240 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
242 %left STAR DIV PERCENT
243 %right BANG CARRET UMINUS
244 %nonassoc OP_INC OP_DEC
246 %left OPEN_BRACKET OPEN_BRACE
250 %start compilation_unit
254 : outer_declarations opt_EOF
255 | outer_declarations attribute_sections opt_EOF
256 | attribute_sections opt_EOF
266 | outer_declarations outer_declaration
271 | namespace_member_declaration
276 | using_directives using_directive
280 : using_alias_directive
281 | using_namespace_directive
284 using_alias_directive
285 : USING IDENTIFIER ASSIGN
286 namespace_or_type_name SEMICOLON
288 current_namespace.UsingAlias ((string) $2, (string) $4, lexer.Location);
292 using_namespace_directive
293 : USING namespace_name SEMICOLON
295 current_namespace.Using ((string) $2, lexer.Location);
300 // Strictly speaking, namespaces don't have attributes but
301 // we parse global attributes along with namespace declarations and then
304 namespace_declaration
305 : opt_attributes NAMESPACE qualified_identifier
307 Attributes attrs = (Attributes) $1;
310 foreach (AttributeSection asec in attrs.AttributeSections)
311 if (asec.Target == "assembly")
312 RootContext.AddGlobalAttribute (
313 current_container, asec, lexer.Location);
316 current_namespace = RootContext.Tree.RecordNamespace (current_namespace, name, (string) $3);
318 namespace_body opt_semicolon
320 current_namespace = current_namespace.Parent;
336 | qualified_identifier DOT IDENTIFIER {
337 $$ = (($1).ToString ()) + "." + ($3.ToString ()); }
342 : namespace_or_type_name
348 opt_namespace_member_declarations
359 opt_namespace_member_declarations
361 | namespace_member_declarations
364 namespace_member_declarations
365 : namespace_member_declaration
366 | namespace_member_declarations namespace_member_declaration
369 namespace_member_declaration
376 Class c = (Class) $1;
377 mod_flags = c.ModFlags;
379 } else if ($1 is Struct){
380 Struct s = (Struct) $1;
381 mod_flags = s.ModFlags;
386 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
388 1527, lexer.Location,
389 "Namespace elements cant be explicitly " +
390 "declared private or protected in `" + name + "'");
392 current_namespace.DeclarationFound = true;
394 | namespace_declaration
400 | interface_declaration
402 | delegate_declaration
404 // Enable this when we have handled all errors, because this acts as a generic fallback
407 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
417 | attribute_sections { $$ = $1; }
423 AttributeSection sect = (AttributeSection) $1;
425 if (sect.Target == "assembly")
426 RootContext.AddGlobalAttribute (current_container, sect, lexer.Location);
428 $$ = new Attributes ((AttributeSection) $1, lexer.Location);
430 | attribute_sections attribute_section
432 Attributes attrs = null;
433 AttributeSection sect = (AttributeSection) $2;
435 if (sect.Target == "assembly")
436 RootContext.AddGlobalAttribute (current_container, sect, lexer.Location);
439 attrs = (Attributes) $1;
440 attrs.AddAttribute (sect);
448 : OPEN_BRACKET attribute_target_specifier attribute_list CLOSE_BRACKET
450 string target = null;
453 target = (string) $2;
455 $$ = new AttributeSection (target, (ArrayList) $3);
457 | OPEN_BRACKET attribute_list CLOSE_BRACKET
459 $$ = new AttributeSection (null, (ArrayList) $2);
463 attribute_target_specifier
464 : attribute_target COLON
473 CheckAttributeTarget ((string) $1);
476 | EVENT { $$ = "event"; }
477 | RETURN { $$ = "return"; }
483 ArrayList attrs = new ArrayList ();
489 | attribute_list COMMA attribute
491 ArrayList attrs = (ArrayList) $1;
503 opt_attribute_arguments
505 $$ = new Attribute ((string) $1, (ArrayList) $3, (Location) $2);
510 : type_name { /* reserved attribute name or identifier: 17.4 */ }
513 opt_attribute_arguments
514 : /* empty */ { $$ = null; }
515 | OPEN_PARENS attribute_arguments CLOSE_PARENS
523 : opt_positional_argument_list
528 ArrayList args = new ArrayList ();
534 | positional_argument_list COMMA named_argument_list
536 ArrayList args = new ArrayList ();
542 | named_argument_list
544 ArrayList args = new ArrayList ();
553 opt_positional_argument_list
554 : /* empty */ { $$ = null; }
555 | positional_argument_list
558 positional_argument_list
561 ArrayList args = new ArrayList ();
562 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
566 | positional_argument_list COMMA expression
568 ArrayList args = (ArrayList) $1;
569 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
578 ArrayList args = new ArrayList ();
583 | named_argument_list COMMA named_argument
585 ArrayList args = (ArrayList) $1;
593 : IDENTIFIER ASSIGN expression
595 $$ = new DictionaryEntry (
597 new Argument ((Expression) $3, Argument.AType.Expression));
603 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
606 opt_class_member_declarations
608 | class_member_declarations
611 class_member_declarations
612 : class_member_declaration
613 | class_member_declarations
614 class_member_declaration
617 class_member_declaration
618 : constant_declaration // done
619 | field_declaration // done
620 | method_declaration // done
621 | property_declaration // done
622 | event_declaration // done
623 | indexer_declaration // done
624 | operator_declaration // done
625 | constructor_declaration // done
626 | destructor_declaration // done
636 string full_struct_name = MakeName ((string) $4);
638 new_struct = new Struct (current_container, full_struct_name, (int) $2,
639 (Attributes) $1, lexer.Location);
640 current_container = new_struct;
641 current_container.Namespace = current_namespace;
642 RootContext.Tree.RecordDecl (full_struct_name, new_struct);
648 Struct new_struct = (Struct) current_container;
651 new_struct.Bases = (ArrayList) $6;
653 current_container = current_container.Parent;
654 CheckDef (current_container.AddStruct (new_struct), new_struct.Name, new_struct.Location);
660 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
663 opt_struct_member_declarations
665 | struct_member_declarations
668 struct_member_declarations
669 : struct_member_declaration
670 | struct_member_declarations struct_member_declaration
673 struct_member_declaration
674 : constant_declaration
677 | property_declaration
679 | indexer_declaration
680 | operator_declaration
681 | constructor_declaration
685 * This is only included so we can flag error 575:
686 * destructors only allowed on class types
688 | destructor_declaration
699 foreach (VariableDeclaration constant in (ArrayList) $5){
700 Location l = constant.Location;
702 Const c = new Const (
703 (Expression) $4, (string) constant.identifier,
704 (Expression) constant.expression_or_array_initializer, (int) $2,
707 CheckDef (current_container.AddConstant (c), c.Name, l);
713 : constant_declarator
715 ArrayList constants = new ArrayList ();
719 | constant_declarators COMMA constant_declarator
721 ArrayList constants = (ArrayList) $1;
728 : IDENTIFIER ASSIGN constant_expression
730 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
741 Expression type = (Expression) $3;
744 foreach (VariableDeclaration var in (ArrayList) $4){
745 Location l = var.Location;
747 Field field = new Field (type, mod, var.identifier,
748 var.expression_or_array_initializer,
751 CheckDef (current_container.AddField (field), field.Name, l);
757 : variable_declarator
759 ArrayList decl = new ArrayList ();
763 | variable_declarators COMMA variable_declarator
765 ArrayList decls = (ArrayList) $1;
772 : IDENTIFIER ASSIGN variable_initializer
774 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
778 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
791 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
793 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
801 Method method = (Method) $1;
802 Block b = (Block) $2;
803 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
806 if ((method.ModFlags & extern_abstract) == 0){
808 501, lexer.Location, current_container.MakeName (method.Name) +
809 "must declare a body because it is not marked abstract or extern");
812 if ((method.ModFlags & Modifiers.EXTERN) != 0){
814 179, lexer.Location, current_container.MakeName (method.Name) +
815 " is declared extern, but has a body");
819 method.Block = (Block) $2;
820 CheckDef (current_container.AddMethod (method), method.Name, method.Location);
822 current_local_parameters = null;
836 1585, lexer.Location, "Member modifier `" +
837 Modifiers.Name (i) + "' must precede member type and name");
850 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
852 Method method = new Method ((Expression) $3, (int) $2, (string) $4,
853 (Parameters) $6, (Attributes) $1, lexer.Location);
855 current_local_parameters = (Parameters) $6;
863 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
865 Method method = new Method (TypeManager.system_void_expr, (int) $2, (string) $4,
866 (Parameters) $6, (Attributes) $1, lexer.Location);
868 current_local_parameters = (Parameters) $6;
875 | SEMICOLON { $$ = null; }
878 opt_formal_parameter_list
879 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
880 | formal_parameter_list
883 formal_parameter_list
886 ArrayList pars_list = (ArrayList) $1;
888 Parameter [] pars = new Parameter [pars_list.Count];
889 pars_list.CopyTo (pars);
891 $$ = new Parameters (pars, null, lexer.Location);
893 | fixed_parameters COMMA parameter_array
895 ArrayList pars_list = (ArrayList) $1;
897 Parameter [] pars = new Parameter [pars_list.Count];
898 pars_list.CopyTo (pars);
900 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
904 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
911 ArrayList pars = new ArrayList ();
916 | fixed_parameters COMMA fixed_parameter
918 ArrayList pars = (ArrayList) $1;
927 opt_parameter_modifier
931 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
935 opt_parameter_modifier
936 : /* empty */ { $$ = Parameter.Modifier.NONE; }
941 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
942 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
946 : opt_attributes PARAMS type IDENTIFIER
948 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
949 note ("type must be a single-dimension array type");
954 : qualified_identifier
963 implicit_value_parameter_type = (Expression) $3;
965 lexer.PropertyParsing = true;
969 accessor_declarations
971 lexer.PropertyParsing = false;
976 Pair pair = (Pair) $7;
977 Accessor get_block = (Accessor) pair.First;
978 Accessor set_block = (Accessor) pair.Second;
980 Location loc = (Location) $6;
981 prop = new Property ((Expression) $3, (string) $4, (int) $2, get_block, set_block,
982 (Attributes) $1, loc);
984 CheckDef (current_container.AddProperty (prop), prop.Name, loc);
985 implicit_value_parameter_type = null;
989 accessor_declarations
990 : get_accessor_declaration opt_set_accessor_declaration
992 $$ = new Pair ($1, $2);
994 | set_accessor_declaration opt_get_accessor_declaration
996 $$ = new Pair ($2, $1);
1000 opt_get_accessor_declaration
1001 : /* empty */ { $$ = null; }
1002 | get_accessor_declaration
1005 opt_set_accessor_declaration
1006 : /* empty */ { $$ = null; }
1007 | set_accessor_declaration
1010 get_accessor_declaration
1011 : opt_attributes GET
1013 // If this is not the case, then current_local_parameters has already
1014 // been set in indexer_declaration
1015 if (parsing_indexer == false)
1016 current_local_parameters = null;
1018 current_local_parameters = indexer_parameters;
1019 lexer.PropertyParsing = false;
1023 $$ = new Accessor ((Block) $4, (Attributes) $1);
1024 current_local_parameters = null;
1025 lexer.PropertyParsing = true;
1029 set_accessor_declaration
1030 : opt_attributes SET
1033 Parameter implicit_value_parameter = new Parameter (
1034 implicit_value_parameter_type, "value",
1035 Parameter.Modifier.NONE, null);
1037 if (parsing_indexer == false) {
1038 args = new Parameter [1];
1039 args [0] = implicit_value_parameter;
1040 current_local_parameters = new Parameters (args, null, lexer.Location);
1042 Parameter [] fpars = indexer_parameters.FixedParameters;
1045 int count = fpars.Length;
1047 args = new Parameter [count + 1];
1048 fpars.CopyTo (args, 0);
1049 args [count] = implicit_value_parameter;
1052 current_local_parameters = new Parameters (
1053 args, indexer_parameters.ArrayParameter, lexer.Location);
1056 lexer.PropertyParsing = false;
1060 $$ = new Accessor ((Block) $4, (Attributes) $1);
1061 current_local_parameters = null;
1062 lexer.PropertyParsing = true;
1068 | SEMICOLON { $$ = null; }
1071 interface_declaration
1074 INTERFACE IDENTIFIER
1076 Interface new_interface;
1077 string full_interface_name = MakeName ((string) $4);
1079 new_interface = new Interface (current_container, full_interface_name, (int) $2,
1080 (Attributes) $1, lexer.Location);
1081 if (current_interface != null) {
1082 Location l = lexer.Location;
1083 Report.Error (-2, l, "Internal compiler error: interface inside interface");
1085 current_interface = new_interface;
1086 new_interface.Namespace = current_namespace;
1087 RootContext.Tree.RecordDecl (full_interface_name, new_interface);
1090 interface_body opt_semicolon
1092 Interface new_interface = (Interface) current_interface;
1095 new_interface.Bases = (ArrayList) $6;
1097 current_interface = null;
1098 CheckDef (current_container.AddInterface (new_interface),
1099 new_interface.Name, new_interface.Location);
1104 : /* empty */ { $$ = null; }
1109 : COLON interface_type_list { $$ = $2; }
1115 ArrayList interfaces = new ArrayList ();
1117 interfaces.Add ($1);
1120 | interface_type_list COMMA interface_type
1122 ArrayList interfaces = (ArrayList) $1;
1123 interfaces.Add ($3);
1130 opt_interface_member_declarations
1134 opt_interface_member_declarations
1136 | interface_member_declarations
1139 interface_member_declarations
1140 : interface_member_declaration
1141 | interface_member_declarations interface_member_declaration
1144 interface_member_declaration
1145 : interface_method_declaration
1147 InterfaceMethod m = (InterfaceMethod) $1;
1149 CheckDef (current_interface.AddMethod (m), m.Name, m.Location);
1151 | interface_property_declaration
1153 InterfaceProperty p = (InterfaceProperty) $1;
1155 CheckDef (current_interface.AddProperty (p), p.Name, p.Location);
1157 | interface_event_declaration
1159 InterfaceEvent e = (InterfaceEvent) $1;
1161 CheckDef (current_interface.AddEvent (e), e.Name, lexer.Location);
1163 | interface_indexer_declaration
1165 InterfaceIndexer i = (InterfaceIndexer) $1;
1167 CheckDef (current_interface.AddIndexer (i), "indexer", i.Location);
1172 : /* empty */ { $$ = false; }
1173 | NEW { $$ = true; }
1176 interface_method_declaration
1177 : opt_attributes opt_new type IDENTIFIER
1178 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1181 $$ = new InterfaceMethod ((Expression) $3, (string) $4, (bool) $2,
1182 (Parameters) $6, (Attributes) $1, lexer.Location);
1184 | opt_attributes opt_new VOID IDENTIFIER
1185 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1188 $$ = new InterfaceMethod (
1189 TypeManager.system_void_expr, (string) $4, (bool) $2, (Parameters) $6,
1190 (Attributes) $1, lexer.Location);
1194 interface_property_declaration
1199 { lexer.PropertyParsing = true; }
1201 { lexer.PropertyParsing = false; }
1206 $$ = new InterfaceProperty ((Expression) $3, (string) $4, (bool) $2,
1207 (gs & 1) == 1, (gs & 2) == 2, (Attributes) $1,
1213 : opt_attributes GET SEMICOLON { $$ = 1; }
1214 | opt_attributes SET SEMICOLON { $$ = 2; }
1215 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1217 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1221 interface_event_declaration
1222 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1224 $$ = new InterfaceEvent ((Expression) $4, (string) $5, (bool) $2, (Attributes) $1,
1229 interface_indexer_declaration
1230 : opt_attributes opt_new type THIS
1231 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1233 { lexer.PropertyParsing = true; }
1235 { lexer.PropertyParsing = false; }
1238 int a_flags = (int) $10;
1240 bool do_get = (a_flags & 1) == 1;
1241 bool do_set = (a_flags & 2) == 2;
1243 $$ = new InterfaceIndexer ((Expression) $3, (Parameters) $6, do_get, do_set,
1244 (bool) $2, (Attributes) $1, lexer.Location);
1248 operator_declaration
1249 : opt_attributes opt_modifiers operator_declarator operator_body
1251 OperatorDeclaration decl = (OperatorDeclaration) $3;
1253 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
1254 decl.arg2type, decl.arg2name, (Block) $4, (Attributes) $1, decl.location);
1256 // Note again, checking is done in semantic analysis
1257 current_container.AddOperator (op);
1259 current_local_parameters = null;
1265 | SEMICOLON { $$ = null; }
1268 : type OPERATOR overloadable_operator
1269 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1271 Operator.OpType op = (Operator.OpType) $3;
1272 CheckUnaryOperator (op);
1274 if (op == Operator.OpType.Addition)
1275 op = Operator.OpType.UnaryPlus;
1277 if (op == Operator.OpType.Subtraction)
1278 op = Operator.OpType.UnaryNegation;
1280 Parameter [] pars = new Parameter [1];
1282 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1284 current_local_parameters = new Parameters (pars, null, lexer.Location);
1286 $$ = new OperatorDeclaration (op, (Expression) $1, (Expression) $5, (string) $6,
1287 null, null, lexer.Location);
1289 | type OPERATOR overloadable_operator
1291 type IDENTIFIER COMMA
1295 CheckBinaryOperator ((Operator.OpType) $3);
1297 Parameter [] pars = new Parameter [2];
1299 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1300 pars [1] = new Parameter ((Expression) $8, (string) $9, Parameter.Modifier.NONE, null);
1302 current_local_parameters = new Parameters (pars, null, lexer.Location);
1304 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1305 (Expression) $5, (string) $6,
1306 (Expression) $8, (string) $9, lexer.Location);
1308 | conversion_operator_declarator
1311 overloadable_operator
1313 : BANG { $$ = Operator.OpType.LogicalNot; }
1314 | TILDE { $$ = Operator.OpType.OnesComplement; }
1315 | OP_INC { $$ = Operator.OpType.Increment; }
1316 | OP_DEC { $$ = Operator.OpType.Decrement; }
1317 | TRUE { $$ = Operator.OpType.True; }
1318 | FALSE { $$ = Operator.OpType.False; }
1319 // Unary and binary:
1320 | PLUS { $$ = Operator.OpType.Addition; }
1321 | MINUS { $$ = Operator.OpType.Subtraction; }
1323 | STAR { $$ = Operator.OpType.Multiply; }
1324 | DIV { $$ = Operator.OpType.Division; }
1325 | PERCENT { $$ = Operator.OpType.Modulus; }
1326 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1327 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1328 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1329 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1330 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1331 | OP_EQ { $$ = Operator.OpType.Equality; }
1332 | OP_NE { $$ = Operator.OpType.Inequality; }
1333 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1334 | OP_LT { $$ = Operator.OpType.LessThan; }
1335 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1336 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1339 conversion_operator_declarator
1340 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1342 Parameter [] pars = new Parameter [1];
1344 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1346 current_local_parameters = new Parameters (pars, null, lexer.Location);
1348 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1349 null, null, lexer.Location);
1351 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1353 Parameter [] pars = new Parameter [1];
1355 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1357 current_local_parameters = new Parameters (pars, null, lexer.Location);
1359 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1360 null, null, lexer.Location);
1364 syntax_error (lexer.Location, "'operator' expected");
1368 syntax_error (lexer.Location, "'operator' expected");
1372 constructor_declaration
1375 constructor_declarator
1378 Constructor c = (Constructor) $3;
1379 c.Block = (Block) $4;
1380 c.ModFlags = (int) $2;
1381 c.OptAttributes = (Attributes) $1;
1383 if ((c.ModFlags & Modifiers.STATIC) != 0){
1384 if ((c.ModFlags & Modifiers.Accessibility) != 0) {
1387 "Access modifiers are not allowed on static constructors");
1390 if (c.Initializer != null){
1393 "Static constructors can not have an explicit this or base " +
1394 "constructor invocations");
1397 if (!c.Parameters.Empty){
1399 132, c.Location, "Static constructors should not have parameters");
1403 CheckDef (current_container.AddConstructor (c), c.Name, c.Location);
1405 current_local_parameters = null;
1409 constructor_declarator
1411 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1413 oob_stack.Push (lexer.Location);
1415 current_local_parameters = (Parameters) $3;
1417 opt_constructor_initializer
1419 Location l = (Location) oob_stack.Pop ();
1420 $$ = new Constructor ((string) $1, (Parameters) $3, (ConstructorInitializer) $6, l);
1426 | SEMICOLON { $$ = null; }
1429 opt_constructor_initializer
1430 : /* empty */ { $$ = null; }
1431 | constructor_initializer
1434 constructor_initializer
1435 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1437 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1439 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1441 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1445 destructor_declaration
1446 : opt_attributes TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1448 if ((string) $3 != current_container.Basename){
1449 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1450 } else if (!(current_container is Class)){
1451 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1453 Location l = lexer.Location;
1456 if (!RootContext.StdLib && current_container.Name == "System.Object")
1457 m = Modifiers.PROTECTED | Modifiers.VIRTUAL;
1459 m = Modifiers.PROTECTED | Modifiers.OVERRIDE;
1461 Method d = new Method (
1462 TypeManager.system_void_expr, m, "Finalize",
1463 new Parameters (null, null, l), (Attributes) $1, l);
1465 d.Block = (Block) $6;
1466 CheckDef (current_container.AddMethod (d), d.Name, d.Location);
1474 EVENT type variable_declarators SEMICOLON
1476 foreach (VariableDeclaration var in (ArrayList) $5) {
1478 Event e = new Event ((Expression) $4, var.identifier,
1479 var.expression_or_array_initializer,
1480 (int) $2, null, null, (Attributes) $1, lexer.Location);
1482 CheckDef (current_container.AddEvent (e), e.Name, e.Location);
1488 EVENT type member_name
1491 implicit_value_parameter_type = (Expression) $4;
1492 lexer.EventParsing = true;
1493 oob_stack.Push (lexer.Location);
1495 event_accessor_declarations
1497 lexer.EventParsing = false;
1501 Location loc = (Location) oob_stack.Pop ();
1503 Pair pair = (Pair) $8;
1504 Accessor add_accessor = null;
1505 Accessor rem_accessor = null;
1507 if (pair.First != null)
1508 add_accessor = (Accessor) pair.First;
1509 if (pair.Second != null)
1510 rem_accessor = (Accessor) pair.Second;
1512 Event e = new Event ((Expression) $4, (string) $5, null, (int) $2, add_accessor, rem_accessor,
1513 (Attributes) $1, loc);
1515 CheckDef (current_container.AddEvent (e), e.Name, loc);
1516 implicit_value_parameter_type = null;
1520 event_accessor_declarations
1521 : add_accessor_declaration remove_accessor_declaration
1523 $$ = new Pair ($1, $2);
1525 | remove_accessor_declaration add_accessor_declaration
1527 $$ = new Pair ($2, $1);
1531 add_accessor_declaration
1532 : opt_attributes ADD
1534 Parameter [] args = new Parameter [1];
1535 Parameter implicit_value_parameter = new Parameter (
1536 implicit_value_parameter_type, "value",
1537 Parameter.Modifier.NONE, null);
1539 args [0] = implicit_value_parameter;
1541 current_local_parameters = new Parameters (args, null, lexer.Location);
1542 lexer.EventParsing = false;
1546 $$ = new Accessor ((Block) $4, (Attributes) $1);
1547 lexer.EventParsing = true;
1551 remove_accessor_declaration
1552 : opt_attributes REMOVE
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;
1572 : opt_attributes opt_modifiers indexer_declarator
1575 IndexerDeclaration decl = (IndexerDeclaration) $3;
1577 implicit_value_parameter_type = decl.type;
1579 lexer.PropertyParsing = true;
1580 parsing_indexer = true;
1582 indexer_parameters = decl.param_list;
1583 oob_stack.Push (lexer.Location);
1585 accessor_declarations
1587 lexer.PropertyParsing = false;
1588 parsing_indexer = false;
1592 // The signature is computed from the signature of the indexer. Look
1593 // at section 3.6 on the spec
1594 Location loc = (Location) oob_stack.Pop ();
1596 IndexerDeclaration decl = (IndexerDeclaration) $3;
1597 Pair pair = (Pair) $6;
1598 Accessor get_block = (Accessor) pair.First;
1599 Accessor set_block = (Accessor) pair.Second;
1601 indexer = new Indexer (decl.type, decl.interface_type, (int) $2, decl.param_list,
1602 get_block, set_block, (Attributes) $1, loc);
1604 // Note that there is no equivalent of CheckDef for this case
1605 // We shall handle this in semantic analysis
1607 current_container.AddIndexer (indexer);
1609 current_local_parameters = null;
1610 implicit_value_parameter_type = null;
1611 indexer_parameters = null;
1616 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1618 Parameters pars = (Parameters) $4;
1620 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1621 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1624 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
1626 | type qualified_identifier DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1628 Parameters pars = (Parameters) $6;
1630 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1631 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1633 $$ = new IndexerDeclaration ((Expression) $1, (string) $2, pars);
1645 Location enum_location = lexer.Location;
1647 string full_name = MakeName ((string) $4);
1648 Enum e = new Enum (current_container, (Expression) $5, (int) $2, full_name,
1649 (Attributes) $1, enum_location);
1651 foreach (VariableDeclaration ev in (ArrayList) $6) {
1652 Location loc = (Location) ev.Location;
1654 CheckDef (e.AddEnumMember (ev.identifier,
1655 (Expression) ev.expression_or_array_initializer,
1656 loc, ev.OptAttributes),
1657 ev.identifier, loc);
1660 e.Namespace = current_namespace;
1662 CheckDef (current_container.AddEnum (e), full_name, enum_location);
1663 RootContext.Tree.RecordDecl (full_name, e);
1669 : /* empty */ { $$ = TypeManager.system_int32_expr; }
1670 | COLON type { $$ = $2; }
1674 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1680 opt_enum_member_declarations
1681 : /* empty */ { $$ = new ArrayList (); }
1682 | enum_member_declarations opt_comma { $$ = $1; }
1685 enum_member_declarations
1686 : enum_member_declaration
1688 ArrayList l = new ArrayList ();
1693 | enum_member_declarations COMMA enum_member_declaration
1695 ArrayList l = (ArrayList) $1;
1703 enum_member_declaration
1704 : opt_attributes IDENTIFIER
1706 $$ = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
1708 | opt_attributes IDENTIFIER
1710 $$ = lexer.Location;
1714 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
1718 delegate_declaration
1722 IDENTIFIER OPEN_PARENS
1723 opt_formal_parameter_list
1727 Location l = lexer.Location;
1728 Delegate del = new Delegate (current_container, (Expression) $4, (int) $2,
1729 MakeName ((string) $5), (Parameters) $7,
1730 (Attributes) $1, l);
1732 del.Namespace = current_namespace;
1733 CheckDef (current_container.AddDelegate (del), del.Name, l);
1738 IDENTIFIER OPEN_PARENS
1739 opt_formal_parameter_list
1743 Location l = lexer.Location;
1744 Delegate del = new Delegate (
1746 TypeManager.system_void_expr, (int) $2, MakeName ((string) $5),
1747 (Parameters) $7, (Attributes) $1, l);
1749 del.Namespace = current_namespace;
1750 CheckDef (current_container.AddDelegate (del), del.Name, l);
1755 : namespace_or_type_name
1758 namespace_or_type_name
1759 : qualified_identifier
1763 * Before you think of adding a return_type, notice that we have been
1764 * using two rules in the places where it matters (one rule using type
1765 * and another identical one that uses VOID as the return type). This
1766 * gets rid of a shift/reduce couple
1769 : type_name { /* class_type */
1771 This does interfaces, delegates, struct_types, class_types,
1772 parent classes, and more! 4.2
1774 $$ = DecomposeQI ((string) $1, lexer.Location);
1786 // Note that here only unmanaged types are allowed but we
1787 // can't perform checks during this phase - we do it during
1788 // semantic analysis.
1790 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1794 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
1800 | non_expression_type rank_specifier
1802 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1804 | non_expression_type STAR
1806 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1808 | expression rank_specifiers
1810 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1814 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1821 ArrayList types = new ArrayList ();
1826 | type_list COMMA type
1828 ArrayList types = (ArrayList) $1;
1836 * replaces all the productions for isolating the various
1837 * simple types, but we need this to reuse it easily in local_variable_type
1840 : OBJECT { $$ = TypeManager.system_object_expr; }
1841 | STRING { $$ = TypeManager.system_string_expr; }
1842 | BOOL { $$ = TypeManager.system_boolean_expr; }
1843 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
1844 | FLOAT { $$ = TypeManager.system_single_expr; }
1845 | DOUBLE { $$ = TypeManager.system_double_expr; }
1850 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
1851 | BYTE { $$ = TypeManager.system_byte_expr; }
1852 | SHORT { $$ = TypeManager.system_int16_expr; }
1853 | USHORT { $$ = TypeManager.system_uint16_expr; }
1854 | INT { $$ = TypeManager.system_int32_expr; }
1855 | UINT { $$ = TypeManager.system_uint32_expr; }
1856 | LONG { $$ = TypeManager.system_int64_expr; }
1857 | ULONG { $$ = TypeManager.system_uint64_expr; }
1858 | CHAR { $$ = TypeManager.system_char_expr; }
1859 | VOID { $$ = TypeManager.system_void_expr; }
1867 : type rank_specifiers
1869 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1874 // Expressions, section 7.5
1882 | qualified_identifier
1884 string name = (string) $1;
1887 $$ = DecomposeQI (name, lexer.Location);
1889 | parenthesized_expression
1891 | invocation_expression
1895 | post_increment_expression
1896 | post_decrement_expression
1900 | checked_expression
1901 | unchecked_expression
1902 | pointer_member_access
1909 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
1910 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
1911 | NULL { $$ = NullLiteral.Null; }
1915 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
1916 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
1917 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
1922 object v = lexer.Value;
1925 $$ = new IntLiteral ((Int32) v);
1927 $$ = new UIntLiteral ((UInt32) v);
1929 $$ = new LongLiteral ((Int64) v);
1930 else if (v is ulong)
1931 $$ = new ULongLiteral ((UInt64) v);
1933 Console.WriteLine ("OOPS. Unexpected result from scanner");
1938 : TRUE { $$ = new BoolLiteral (true); }
1939 | FALSE { $$ = new BoolLiteral (false); }
1942 parenthesized_expression
1943 : OPEN_PARENS expression CLOSE_PARENS
1948 : primary_expression DOT IDENTIFIER
1950 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1952 | predefined_type DOT IDENTIFIER
1954 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1962 invocation_expression
1963 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
1966 Location l = lexer.Location;
1967 Report.Error (1, l, "THIS IS CRAZY");
1969 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
1974 : /* empty */ { $$ = null; }
1981 ArrayList list = new ArrayList ();
1985 | argument_list COMMA argument
1987 ArrayList list = (ArrayList) $1;
1996 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
1998 | REF variable_reference
2000 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2002 | OUT variable_reference
2004 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2009 : expression { note ("section 5.4"); $$ = $1; }
2013 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2015 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2017 | primary_expression rank_specifiers
2019 // So the super-trick is that primary_expression
2020 // can only be either a SimpleName or a MemberAccess.
2021 // The MemberAccess case arises when you have a fully qualified type-name like :
2023 // SimpleName is when you have
2026 Expression expr = (Expression) $1;
2027 if (!(expr is SimpleName || expr is MemberAccess)) {
2028 Location l = lexer.Location;
2029 Report.Error (-1, l, "Invalid Type definition");
2030 $$ = "System.Object";
2034 // So we extract the string corresponding to the SimpleName
2037 $$ = new SimpleName (GetQualifiedIdentifier (expr) + (string) $2, lexer.Location);
2044 ArrayList list = new ArrayList ();
2048 | expression_list COMMA expression
2050 ArrayList list = (ArrayList) $1;
2059 $$ = new This (current_block, lexer.Location);
2064 : BASE DOT IDENTIFIER
2066 $$ = new BaseAccess ((string) $3, lexer.Location);
2068 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2070 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2074 post_increment_expression
2075 : primary_expression OP_INC
2077 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2078 (Expression) $1, lexer.Location);
2082 post_decrement_expression
2083 : primary_expression OP_DEC
2085 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2086 (Expression) $1, lexer.Location);
2091 : object_or_delegate_creation_expression
2092 | array_creation_expression
2095 object_or_delegate_creation_expression
2096 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2098 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2102 array_creation_expression
2103 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2105 opt_array_initializer
2107 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2109 | NEW type rank_specifiers array_initializer
2111 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2115 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2135 | rank_specifiers rank_specifier
2137 $$ = (string) $2 + (string) $1;
2142 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2144 $$ = "[" + (string) $2 + "]";
2164 | dim_separators COMMA
2166 $$ = (string) $1 + ",";
2170 opt_array_initializer
2182 : OPEN_BRACE CLOSE_BRACE
2184 ArrayList list = new ArrayList ();
2187 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2189 $$ = (ArrayList) $2;
2193 variable_initializer_list
2194 : variable_initializer
2196 ArrayList list = new ArrayList ();
2200 | variable_initializer_list COMMA variable_initializer
2202 ArrayList list = (ArrayList) $1;
2209 : TYPEOF OPEN_PARENS type CLOSE_PARENS
2211 $$ = new TypeOf ((Expression) $3, lexer.Location);
2216 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2217 $$ = new SizeOf ((Expression) $3, lexer.Location);
2222 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2224 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2228 unchecked_expression
2229 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2231 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2235 pointer_member_access
2236 : primary_expression OP_PTR IDENTIFIER
2240 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2241 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2245 : primary_expression
2246 | BANG prefixed_unary_expression
2248 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2250 | TILDE prefixed_unary_expression
2252 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2254 | OPEN_PARENS expression CLOSE_PARENS unary_expression
2256 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2258 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2260 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2265 // The idea to split this out is from Rhys' grammar
2266 // to solve the problem with casts.
2268 prefixed_unary_expression
2270 | PLUS prefixed_unary_expression
2272 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2274 | MINUS prefixed_unary_expression
2276 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2278 | OP_INC prefixed_unary_expression
2280 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2281 (Expression) $2, lexer.Location);
2283 | OP_DEC prefixed_unary_expression
2285 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2286 (Expression) $2, lexer.Location);
2288 | STAR prefixed_unary_expression
2290 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2292 | BITWISE_AND prefixed_unary_expression
2294 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2298 pre_increment_expression
2299 : OP_INC prefixed_unary_expression
2301 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2302 (Expression) $2, lexer.Location);
2306 pre_decrement_expression
2307 : OP_DEC prefixed_unary_expression
2309 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2310 (Expression) $2, lexer.Location);
2314 multiplicative_expression
2315 : prefixed_unary_expression
2316 | multiplicative_expression STAR prefixed_unary_expression
2318 $$ = new Binary (Binary.Operator.Multiply,
2319 (Expression) $1, (Expression) $3, lexer.Location);
2321 | multiplicative_expression DIV prefixed_unary_expression
2323 $$ = new Binary (Binary.Operator.Division,
2324 (Expression) $1, (Expression) $3, lexer.Location);
2326 | multiplicative_expression PERCENT prefixed_unary_expression
2328 $$ = new Binary (Binary.Operator.Modulus,
2329 (Expression) $1, (Expression) $3, lexer.Location);
2334 : multiplicative_expression
2335 | additive_expression PLUS multiplicative_expression
2337 $$ = new Binary (Binary.Operator.Addition,
2338 (Expression) $1, (Expression) $3, lexer.Location);
2340 | additive_expression MINUS multiplicative_expression
2342 $$ = new Binary (Binary.Operator.Subtraction,
2343 (Expression) $1, (Expression) $3, lexer.Location);
2348 : additive_expression
2349 | shift_expression OP_SHIFT_LEFT additive_expression
2351 $$ = new Binary (Binary.Operator.LeftShift,
2352 (Expression) $1, (Expression) $3, lexer.Location);
2354 | shift_expression OP_SHIFT_RIGHT additive_expression
2356 $$ = new Binary (Binary.Operator.RightShift,
2357 (Expression) $1, (Expression) $3, lexer.Location);
2361 relational_expression
2363 | relational_expression OP_LT shift_expression
2365 $$ = new Binary (Binary.Operator.LessThan,
2366 (Expression) $1, (Expression) $3, lexer.Location);
2368 | relational_expression OP_GT shift_expression
2370 $$ = new Binary (Binary.Operator.GreaterThan,
2371 (Expression) $1, (Expression) $3, lexer.Location);
2373 | relational_expression OP_LE shift_expression
2375 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2376 (Expression) $1, (Expression) $3, lexer.Location);
2378 | relational_expression OP_GE shift_expression
2380 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2381 (Expression) $1, (Expression) $3, lexer.Location);
2383 | relational_expression IS type
2385 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
2387 | relational_expression AS type
2389 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
2394 : relational_expression
2395 | equality_expression OP_EQ relational_expression
2397 $$ = new Binary (Binary.Operator.Equality,
2398 (Expression) $1, (Expression) $3, lexer.Location);
2400 | equality_expression OP_NE relational_expression
2402 $$ = new Binary (Binary.Operator.Inequality,
2403 (Expression) $1, (Expression) $3, lexer.Location);
2408 : equality_expression
2409 | and_expression BITWISE_AND equality_expression
2411 $$ = new Binary (Binary.Operator.BitwiseAnd,
2412 (Expression) $1, (Expression) $3, lexer.Location);
2416 exclusive_or_expression
2418 | exclusive_or_expression CARRET and_expression
2420 $$ = new Binary (Binary.Operator.ExclusiveOr,
2421 (Expression) $1, (Expression) $3, lexer.Location);
2425 inclusive_or_expression
2426 : exclusive_or_expression
2427 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2429 $$ = new Binary (Binary.Operator.BitwiseOr,
2430 (Expression) $1, (Expression) $3, lexer.Location);
2434 conditional_and_expression
2435 : inclusive_or_expression
2436 | conditional_and_expression OP_AND inclusive_or_expression
2438 $$ = new Binary (Binary.Operator.LogicalAnd,
2439 (Expression) $1, (Expression) $3, lexer.Location);
2443 conditional_or_expression
2444 : conditional_and_expression
2445 | conditional_or_expression OP_OR conditional_and_expression
2447 $$ = new Binary (Binary.Operator.LogicalOr,
2448 (Expression) $1, (Expression) $3, lexer.Location);
2452 conditional_expression
2453 : conditional_or_expression
2454 | conditional_or_expression INTERR expression COLON expression
2456 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2460 assignment_expression
2461 : prefixed_unary_expression ASSIGN expression
2463 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2465 | prefixed_unary_expression OP_MULT_ASSIGN expression
2467 Location l = lexer.Location;
2469 $$ = new CompoundAssign (
2470 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
2472 | prefixed_unary_expression OP_DIV_ASSIGN expression
2474 Location l = lexer.Location;
2476 $$ = new CompoundAssign (
2477 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
2479 | prefixed_unary_expression OP_MOD_ASSIGN expression
2481 Location l = lexer.Location;
2483 $$ = new CompoundAssign (
2484 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
2486 | prefixed_unary_expression OP_ADD_ASSIGN expression
2488 Location l = lexer.Location;
2490 $$ = new CompoundAssign (
2491 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
2493 | prefixed_unary_expression OP_SUB_ASSIGN expression
2495 Location l = lexer.Location;
2497 $$ = new CompoundAssign (
2498 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
2500 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2502 Location l = lexer.Location;
2504 $$ = new CompoundAssign (
2505 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
2507 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2509 Location l = lexer.Location;
2511 $$ = new CompoundAssign (
2512 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
2514 | prefixed_unary_expression OP_AND_ASSIGN expression
2516 Location l = lexer.Location;
2518 $$ = new CompoundAssign (
2519 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
2521 | prefixed_unary_expression OP_OR_ASSIGN expression
2523 Location l = lexer.Location;
2525 $$ = new CompoundAssign (
2526 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
2528 | prefixed_unary_expression OP_XOR_ASSIGN expression
2530 Location l = lexer.Location;
2532 $$ = new CompoundAssign (
2533 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
2538 : conditional_expression
2539 | assignment_expression
2561 name = MakeName ((string) $4);
2563 new_class = new Class (current_container, name, (int) $2,
2564 (Attributes) $1, lexer.Location);
2565 current_container = new_class;
2566 current_container.Namespace = current_namespace;
2567 RootContext.Tree.RecordDecl (name, new_class);
2573 Class new_class = (Class) current_container;
2576 new_class.Bases = (ArrayList) $6;
2578 current_container = current_container.Parent;
2579 CheckDef (current_container.AddClass (new_class), new_class.Name, new_class.Location);
2586 : /* empty */ { $$ = (int) 0; }
2592 | modifiers modifier
2597 if ((m1 & m2) != 0) {
2598 Location l = lexer.Location;
2599 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2601 $$ = (int) (m1 | m2);
2606 : NEW { $$ = Modifiers.NEW; }
2607 | PUBLIC { $$ = Modifiers.PUBLIC; }
2608 | PROTECTED { $$ = Modifiers.PROTECTED; }
2609 | INTERNAL { $$ = Modifiers.INTERNAL; }
2610 | PRIVATE { $$ = Modifiers.PRIVATE; }
2611 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2612 | SEALED { $$ = Modifiers.SEALED; }
2613 | STATIC { $$ = Modifiers.STATIC; }
2614 | READONLY { $$ = Modifiers.READONLY; }
2615 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2616 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2617 | EXTERN { $$ = Modifiers.EXTERN; }
2618 | VOLATILE { $$ = Modifiers.VOLATILE; }
2619 | UNSAFE { $$ = Modifiers.UNSAFE; }
2623 : /* empty */ { $$ = null; }
2624 | class_base { $$ = $1; }
2628 : COLON type_list { $$ = $2; }
2636 // A block is "contained" on the following places:
2638 // property_declaration as part of the accessor body (get/set)
2639 // operator_declaration
2640 // constructor_declaration
2641 // destructor_declaration
2642 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2647 current_block = new Block (current_block, current_local_parameters,
2648 lexer.Location, Location.Null);
2650 opt_statement_list CLOSE_BRACE
2652 while (current_block.Implicit)
2653 current_block = current_block.Parent;
2655 current_block.SetEndLocation (lexer.Location);
2656 current_block = current_block.Parent;
2667 | statement_list statement
2671 : declaration_statement
2673 if ((Block) $1 != current_block){
2674 current_block.AddStatement ((Statement) $1);
2675 current_block = (Block) $1;
2678 | embedded_statement
2680 Statement s = (Statement) $1;
2683 current_block.AddStatement ((Statement) $1);
2691 | expression_statement
2692 | selection_statement
2693 | iteration_statement
2697 | unchecked_statement
2707 $$ = new EmptyStatement ();
2714 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
2716 if (!current_block.AddLabel ((string) $1, labeled)){
2717 Location l = lexer.Location;
2718 Report.Error (140, l, "The label '" + ((string) $1) + "' is a duplicate");
2720 current_block.AddStatement (labeled);
2725 declaration_statement
2726 : local_variable_declaration SEMICOLON
2728 DictionaryEntry de = (DictionaryEntry) $1;
2730 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
2733 | local_constant_declaration SEMICOLON
2735 DictionaryEntry de = (DictionaryEntry) $1;
2737 $$ = declare_local_constant ((Expression) de.Key, (VariableDeclaration) de.Value);
2742 * The following is from Rhys' grammar:
2743 * > Types in local variable declarations must be recognized as
2744 * > expressions to prevent reduce/reduce errors in the grammar.
2745 * > The expressions are converted into types during semantic analysis.
2748 : primary_expression opt_rank_specifier
2750 // FIXME: Do something smart here regarding the composition of the type.
2752 // Ok, the above "primary_expression" is there to get rid of
2753 // both reduce/reduce and shift/reduces in the grammar, it should
2754 // really just be "type_name". If you use type_name, a reduce/reduce
2755 // creeps up. If you use qualified_identifier (which is all we need
2756 // really) two shift/reduces appear.
2759 // So the super-trick is that primary_expression
2760 // can only be either a SimpleName or a MemberAccess.
2761 // The MemberAccess case arises when you have a fully qualified type-name like :
2763 // SimpleName is when you have
2766 Expression expr = (Expression) $1;
2767 if (!(expr is SimpleName || expr is MemberAccess)) {
2768 Location l = lexer.Location;
2769 Report.Error (-1, l, "Invalid Type definition");
2770 $$ = TypeManager.system_object_expr;
2774 // So we extract the string corresponding to the SimpleName
2777 if ((string) $2 == "")
2780 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2782 | builtin_types opt_rank_specifier
2784 if ((string) $2 == "")
2787 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2791 local_variable_pointer_type
2792 : primary_expression STAR
2794 Expression expr = (Expression) $1;
2795 if (!(expr is SimpleName || expr is MemberAccess)) {
2796 Location l = lexer.Location;
2797 Report.Error (-1, l, "Invalid Type definition");
2798 $$ = "System.Object";
2801 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2803 | builtin_types STAR
2805 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
2809 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
2811 | local_variable_pointer_type STAR
2813 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2817 local_variable_declaration
2818 : local_variable_type variable_declarators
2820 $$ = new DictionaryEntry ($1, $2);
2822 | local_variable_pointer_type opt_rank_specifier variable_declarators
2826 if ((string) $2 == "")
2827 t = (Expression) $1;
2829 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2830 $$ = new DictionaryEntry (t, $3);
2834 local_constant_declaration
2835 : CONST local_variable_type constant_declarator
2837 $$ = new DictionaryEntry ($2, $3);
2841 expression_statement
2842 : statement_expression SEMICOLON
2849 // We have to do the wrapping here and not in the case above,
2850 // because statement_expression is used for example in for_statement
2852 statement_expression
2853 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2854 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2855 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2856 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2857 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2858 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2859 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2861 Report.Error (1002, lexer.Location, "Expecting `;'");
2865 object_creation_expression
2866 : object_or_delegate_creation_expression
2867 { note ("complain if this is a delegate maybe?"); }
2876 : if_statement_open if_statement_rest
2885 oob_stack.Push (lexer.Location);
2890 : boolean_expression CLOSE_PARENS
2893 Location l = (Location) oob_stack.Pop ();
2895 $$ = new If ((Expression) $1, (Statement) $3, l);
2897 if (RootContext.WarningLevel >= 3){
2898 if ($3 is EmptyStatement)
2899 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
2903 | boolean_expression CLOSE_PARENS
2904 embedded_statement ELSE embedded_statement
2906 Location l = (Location) oob_stack.Pop ();
2908 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
2913 : SWITCH OPEN_PARENS
2915 oob_stack.Push (lexer.Location);
2916 switch_stack.Push (current_block);
2918 expression CLOSE_PARENS
2921 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
2922 current_block = (Block) switch_stack.Pop ();
2938 Report.Error (1522, lexer.Location, "Empty switch block");
2946 ArrayList sections = new ArrayList ();
2951 | switch_sections switch_section
2953 ArrayList sections = (ArrayList) $1;
2963 current_block = new Block (current_block);
2967 while (current_block.Implicit)
2968 current_block = current_block.Parent;
2969 $$ = new SwitchSection ((ArrayList) $1, current_block);
2976 ArrayList labels = new ArrayList ();
2981 | switch_labels switch_label
2983 ArrayList labels = (ArrayList) ($1);
2991 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
2992 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
2995 1523, lexer.Location,
2996 "The keyword case or default must precede code in switch block");
3010 oob_stack.Push (lexer.Location);
3012 boolean_expression CLOSE_PARENS embedded_statement
3014 Location l = (Location) oob_stack.Pop ();
3015 $$ = new While ((Expression) $4, (Statement) $6, l);
3017 if (RootContext.WarningLevel >= 3){
3018 if ($6 is EmptyStatement)
3019 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3025 : DO embedded_statement
3026 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
3028 $$ = new Do ((Statement) $2, (Expression) $5, lexer.Location);
3034 opt_for_initializer SEMICOLON
3036 Block assign_block = new Block (current_block);
3037 current_block = assign_block;
3039 if ($3 is DictionaryEntry){
3040 DictionaryEntry de = (DictionaryEntry) $3;
3042 Expression type = (Expression) de.Key;
3043 ArrayList var_declarators = (ArrayList) de.Value;
3045 foreach (VariableDeclaration decl in var_declarators){
3049 vi = current_block.AddVariable (
3050 type, decl.identifier, current_local_parameters, decl.Location);
3054 Location l = lexer.Location;
3056 if (decl.expression_or_array_initializer is Expression){
3057 expr = (Expression) decl.expression_or_array_initializer;
3059 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3060 expr = new ArrayCreation (type, "", init, decl.Location);
3063 LocalVariableReference var;
3064 var = new LocalVariableReference (assign_block, decl.identifier, l);
3066 Assign a = new Assign (var, expr, decl.Location);
3068 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3073 oob_stack.Push (lexer.Location);
3075 opt_for_condition SEMICOLON
3076 opt_for_iterator CLOSE_PARENS
3079 Location l = (Location) oob_stack.Pop ();
3081 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3083 if (RootContext.WarningLevel >= 3){
3084 if ($10 is EmptyStatement)
3085 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3088 current_block.AddStatement (f);
3089 while (current_block.Implicit)
3090 current_block = current_block.Parent;
3092 current_block = current_block.Parent;
3097 : /* empty */ { $$ = new EmptyStatement (); }
3102 : local_variable_declaration
3103 | statement_expression_list
3107 : /* empty */ { $$ = null; }
3108 | boolean_expression
3112 : /* empty */ { $$ = new EmptyStatement (); }
3117 : statement_expression_list
3120 statement_expression_list
3121 : statement_expression
3123 // CHANGE: was `null'
3124 Block b = new Block (current_block, true);
3126 b.AddStatement ((Statement) $1);
3129 | statement_expression_list COMMA statement_expression
3131 Block b = (Block) $1;
3133 b.AddStatement ((Statement) $3);
3139 : FOREACH OPEN_PARENS type IDENTIFIER IN
3141 oob_stack.Push (lexer.Location);
3143 expression CLOSE_PARENS
3145 oob_stack.Push (current_block);
3147 Block foreach_block = new Block (current_block, true);
3148 LocalVariableReference v = null;
3149 Location l = lexer.Location;
3152 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3156 // Get a writable reference to this read-only variable.
3157 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3159 current_block = foreach_block;
3162 oob_stack.Push (current_block);
3166 Block foreach_block = (Block) oob_stack.Pop ();
3167 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3168 Block prev_block = (Block) oob_stack.Pop ();
3169 Location l = (Location) oob_stack.Pop ();
3171 current_block = prev_block;
3174 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3175 foreach_block.AddStatement (f);
3184 | continue_statement
3193 $$ = new Break (lexer.Location);
3198 : CONTINUE SEMICOLON
3200 $$ = new Continue (lexer.Location);
3205 : GOTO IDENTIFIER SEMICOLON
3207 $$ = new Goto (current_block, (string) $2, lexer.Location);
3209 | GOTO CASE constant_expression SEMICOLON
3211 $$ = new GotoCase ((Expression) $3, lexer.Location);
3213 | GOTO DEFAULT SEMICOLON
3215 $$ = new GotoDefault (lexer.Location);
3220 : RETURN opt_expression SEMICOLON
3222 $$ = new Return ((Expression) $2, lexer.Location);
3227 : THROW opt_expression SEMICOLON
3229 $$ = new Throw ((Expression) $2, lexer.Location);
3239 : TRY block catch_clauses
3242 ArrayList s = new ArrayList ();
3244 foreach (Catch cc in (ArrayList) $3) {
3251 // Now s contains the list of specific catch clauses
3252 // and g contains the general one.
3254 $$ = new Try ((Block) $2, s, g, null, lexer.Location);
3256 | TRY block opt_catch_clauses FINALLY block
3259 ArrayList s = new ArrayList ();
3260 ArrayList catch_list = (ArrayList) $3;
3262 if (catch_list != null){
3263 foreach (Catch cc in catch_list) {
3271 $$ = new Try ((Block) $2, s, g, (Block) $5, lexer.Location);
3275 Report.Error (1524, lexer.Location, "Expected catch or finally");
3280 : /* empty */ { $$ = null; }
3287 ArrayList l = new ArrayList ();
3292 | catch_clauses catch_clause
3294 ArrayList l = (ArrayList) $1;
3302 : /* empty */ { $$ = null; }
3307 : CATCH opt_catch_args
3309 Expression type = null;
3313 DictionaryEntry cc = (DictionaryEntry) $2;
3314 type = (Expression) cc.Key;
3315 id = (string) cc.Value;
3318 ArrayList one = new ArrayList ();
3319 Location loc = lexer.Location;
3321 one.Add (new VariableDeclaration (id, null, loc));
3324 current_block = new Block (current_block);
3325 Block b = declare_local_variables (type, one, loc);
3330 Expression type = null;
3334 DictionaryEntry cc = (DictionaryEntry) $2;
3335 type = (Expression) cc.Key;
3336 id = (string) cc.Value;
3340 // FIXME: I can change this for an assignment.
3342 while (current_block != (Block) $1)
3343 current_block = current_block.Parent;
3348 $$ = new Catch (type, id , (Block) $4, lexer.Location);
3353 : /* empty */ { $$ = null; }
3358 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3360 $$ = new DictionaryEntry ($2, $3);
3367 $$ = new Checked ((Block) $2);
3374 $$ = new Unchecked ((Block) $2);
3381 if (!RootContext.Unsafe){
3382 Report.Error (227, lexer.Location,
3383 "Unsafe code can only be used if --unsafe is used");
3386 $$ = new Unsafe ((Block) $3);
3392 pointer_type fixed_pointer_declarators
3395 Block assign_block = new Block (current_block, true);
3396 ArrayList list = (ArrayList) $4;
3397 Expression type = (Expression) $3;
3398 Location l = lexer.Location;
3399 int top = list.Count;
3401 for (int i = 0; i < top; i++){
3402 Pair p = (Pair) list [i];
3405 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3412 current_block.AddStatement (assign_block);
3413 current_block = assign_block;
3414 oob_stack.Push (assign_block);
3419 Location l = (Location) oob_stack.Pop ();
3420 Block assign_block = (Block) oob_stack.Pop ();
3422 ArrayList list = (ArrayList) $4;
3423 int top = list.Count;
3425 $$ = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
3429 fixed_pointer_declarators
3430 : fixed_pointer_declarator {
3431 ArrayList declarators = new ArrayList ();
3432 declarators.Add ($1);
3435 | fixed_pointer_declarators COMMA fixed_pointer_declarator
3437 ArrayList declarators = (ArrayList) $1;
3438 declarators.Add ($3);
3443 fixed_pointer_declarator
3444 : IDENTIFIER ASSIGN expression
3446 $$ = new Pair ($1, $3);
3451 : LOCK OPEN_PARENS expression CLOSE_PARENS
3457 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
3462 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
3464 Block assign_block = new Block (current_block);
3465 current_block = assign_block;
3467 oob_stack.Push (lexer.Location);
3469 if ($3 is DictionaryEntry){
3470 DictionaryEntry de = (DictionaryEntry) $3;
3471 Location l = lexer.Location;
3473 Expression type = (Expression) de.Key;
3474 ArrayList var_declarators = (ArrayList) de.Value;
3476 ArrayList vars = new ArrayList ();
3478 foreach (VariableDeclaration decl in var_declarators){
3480 VariableInfo vi = current_block.AddVariable (
3481 type, decl.identifier,
3482 current_local_parameters, decl.Location);
3488 if (decl.expression_or_array_initializer is Expression){
3489 expr = (Expression) decl.expression_or_array_initializer;
3491 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3493 expr = new ArrayCreation (type, "", init, decl.Location);
3496 LocalVariableReference var;
3498 // Get a writable reference to this read-only variable.
3499 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
3501 // This is so that it is not a warning on using variables
3504 vars.Add (new DictionaryEntry (var, expr));
3506 // Assign a = new Assign (var, expr, decl.Location);
3507 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3509 $3 = new DictionaryEntry (type, vars);
3514 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
3515 current_block.AddStatement (u);
3516 while (current_block.Implicit)
3517 current_block = current_block.Parent;
3519 current_block = current_block.Parent;
3523 resource_acquisition
3524 : local_variable_declaration
3531 // A class used to pass around variable declarations and constants
3533 public class VariableDeclaration {
3534 public string identifier;
3535 public object expression_or_array_initializer;
3536 public Location Location;
3537 public Attributes OptAttributes;
3539 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
3541 this.identifier = id;
3542 this.expression_or_array_initializer = eoai;
3544 this.OptAttributes = opt_attrs;
3547 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
3553 // A class used to hold info about an indexer declarator
3556 public class IndexerDeclaration {
3557 public Expression type;
3558 public string interface_type;
3559 public Parameters param_list;
3561 public IndexerDeclaration (Expression type, string interface_type, Parameters param_list)
3564 this.interface_type = interface_type;
3565 this.param_list = param_list;
3570 // A class used to hold info about an operator declarator
3573 public class OperatorDeclaration {
3574 public Operator.OpType optype;
3575 public Expression ret_type, arg1type, arg2type;
3576 public string arg1name, arg2name;
3577 public Location location;
3579 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
3580 Expression arg1type, string arg1name,
3581 Expression arg2type, string arg2name, Location location)
3584 this.ret_type = ret_type;
3585 this.arg1type = arg1type;
3586 this.arg1name = arg1name;
3587 this.arg2type = arg2type;
3588 this.arg2name = arg2name;
3589 this.location = location;
3595 // Given the @class_name name, it creates a fully qualified name
3596 // based on the containing declaration space
3599 MakeName (string class_name)
3601 string ns = current_namespace.Name;
3602 string container_name = current_container.Name;
3604 if (container_name == ""){
3606 return ns + "." + class_name;
3610 return container_name + "." + class_name;
3614 // Used to report back to the user the result of a declaration
3615 // in the current declaration space
3618 CheckDef (AdditionResult result, string name, Location l)
3620 if (result == AdditionResult.Success)
3624 case AdditionResult.NameExists:
3625 Report.Error (102, l, "The container `" + current_container.Name +
3626 "' already contains a definition for `"+
3632 // This is handled only for static Constructors, because
3633 // in reality we handle these by the semantic analysis later
3635 case AdditionResult.MethodExists:
3637 111, l, "Class `"+current_container.Name+
3638 "' already defines a member called '" +
3639 name + "' with the same parameter types (more than one default constructor)");
3642 case AdditionResult.EnclosingClash:
3643 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
3646 case AdditionResult.NotAConstructor:
3647 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
3653 CheckDef (bool result, string name, Location l)
3657 CheckDef (AdditionResult.NameExists, name, l);
3660 Expression DecomposeQI (string name, Location loc)
3664 if (name.IndexOf ('.') == -1){
3665 return new SimpleName (name, loc);
3667 int pos = name.LastIndexOf (".");
3668 string left = name.Substring (0, pos);
3669 string right = name.Substring (pos + 1);
3671 o = DecomposeQI (left, loc);
3673 return new MemberAccess (o, right, loc);
3678 // This method is used to get at the complete string representation of
3679 // a fully-qualified type name, hiding inside a MemberAccess ;-)
3680 // This is necessary because local_variable_type admits primary_expression
3681 // as the type of the variable. So we do some extra checking
3683 string GetQualifiedIdentifier (Expression expr)
3685 if (expr is SimpleName)
3686 return ((SimpleName)expr).Name;
3687 else if (expr is MemberAccess)
3688 return GetQualifiedIdentifier (((MemberAccess)expr).Expr) + "." + ((MemberAccess) expr).Identifier;
3690 throw new Exception ("Expr has to be either SimpleName or MemberAccess! (" + expr + ")");
3694 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
3696 Block implicit_block;
3697 ArrayList inits = null;
3700 // We use the `Used' property to check whether statements
3701 // have been added to the current block. If so, we need
3702 // to create another block to contain the new declaration
3703 // otherwise, as an optimization, we use the same block to
3704 // add the declaration.
3706 // FIXME: A further optimization is to check if the statements
3707 // that were added were added as part of the initialization
3708 // below. In which case, no other statements have been executed
3709 // and we might be able to reduce the number of blocks for
3710 // situations like this:
3712 // int j = 1; int k = j + 1;
3714 if (current_block.Used)
3715 implicit_block = new Block (current_block, true, loc, Location.Null);
3717 implicit_block = current_block;
3719 foreach (VariableDeclaration decl in variable_declarators){
3721 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
3722 if (decl.expression_or_array_initializer != null){
3724 inits = new ArrayList ();
3731 return implicit_block;
3733 foreach (VariableDeclaration decl in inits){
3737 if (decl.expression_or_array_initializer is Expression){
3738 expr = (Expression) decl.expression_or_array_initializer;
3741 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3743 expr = new ArrayCreation (type, "", init, decl.Location);
3746 LocalVariableReference var;
3747 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
3749 assign = new Assign (var, expr, decl.Location);
3751 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
3754 return implicit_block;
3757 Block declare_local_constant (Expression type, VariableDeclaration decl)
3759 Block implicit_block;
3761 if (current_block.Used)
3762 implicit_block = new Block (current_block, true);
3764 implicit_block = current_block;
3766 if (!(implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
3767 current_local_parameters, decl.Location))){
3770 return implicit_block;
3773 void CheckAttributeTarget (string a)
3777 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
3781 Location l = lexer.Location;
3782 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
3788 void CheckUnaryOperator (Operator.OpType op)
3792 case Operator.OpType.LogicalNot:
3793 case Operator.OpType.OnesComplement:
3794 case Operator.OpType.Increment:
3795 case Operator.OpType.Decrement:
3796 case Operator.OpType.True:
3797 case Operator.OpType.False:
3798 case Operator.OpType.Addition:
3799 case Operator.OpType.Subtraction:
3804 Location l = lexer.Location;
3805 Report.Error (1019, l, "Overloadable unary operator expected");
3811 void CheckBinaryOperator (Operator.OpType op)
3815 case Operator.OpType.Addition:
3816 case Operator.OpType.Subtraction:
3817 case Operator.OpType.Multiply:
3818 case Operator.OpType.Division:
3819 case Operator.OpType.Modulus:
3820 case Operator.OpType.BitwiseAnd:
3821 case Operator.OpType.BitwiseOr:
3822 case Operator.OpType.ExclusiveOr:
3823 case Operator.OpType.LeftShift:
3824 case Operator.OpType.RightShift:
3825 case Operator.OpType.Equality:
3826 case Operator.OpType.Inequality:
3827 case Operator.OpType.GreaterThan:
3828 case Operator.OpType.LessThan:
3829 case Operator.OpType.GreaterThanOrEqual:
3830 case Operator.OpType.LessThanOrEqual:
3834 Location l = lexer.Location;
3835 Report.Error (1020, l, "Overloadable binary operator expected");
3841 void syntax_error (Location l, string msg)
3843 Report.Error (1003, l, "Syntax error, " + msg);
3846 void output (string s)
3848 Console.WriteLine (s);
3851 void note (string s)
3853 // Used to put annotations
3858 public Tokenizer Lexer {
3864 public CSharpParser (string name, System.IO.Stream input, ArrayList defines)
3866 current_namespace = new Namespace (null, "");
3869 current_container = RootContext.Tree.Types;
3870 current_container.Namespace = current_namespace;
3871 oob_stack = new Stack ();
3872 switch_stack = new Stack ();
3874 lexer = new Tokenizer (input, name, defines);
3877 public override void parse ()
3880 if (yacc_verbose_flag)
3881 yyparse (lexer, new yydebug.yyDebugSimple ());
3884 } catch (Exception e){
3885 // Please do not remove this, it is used during debugging
3888 Console.WriteLine (lexer.location + " : Parsing error ");
3889 Console.WriteLine (e);