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:
24 using System.Collections;
30 public class CSharpParser : GenericParser {
31 Namespace current_namespace;
32 TypeContainer current_container;
35 // Current block is used to add statements as we find
42 // Current interface is used by the various declaration
43 // productions in the interface declaration to "add"
44 // the interfaces as we find them.
46 Interface current_interface;
49 // This is used by the unary_expression code to resolve
50 // a name against a parameter.
52 Parameters current_local_parameters;
55 // Using during property parsing to describe the implicit
56 // value parameter that is passed to the "set" and "get"accesor
57 // methods (properties and indexers).
59 Expression implicit_value_parameter_type;
60 Parameters indexer_parameters;
63 // Used to determine if we are parsing the get/set pair
64 // of an indexer or a property
69 // An out-of-band stack.
80 %token NONE /* This token is never returned by our lexer */
81 %token ERROR // This is used not by the parser, but by the tokenizer.
85 *These are the C# keywords
168 /* C# keywords which are not really keywords */
172 /* C# single character operators/punctuation. */
173 %token OPEN_BRACE "{"
174 %token CLOSE_BRACE "}"
175 %token OPEN_BRACKET "["
176 %token CLOSE_BRACKET "]"
177 %token OPEN_PARENS "("
178 %token CLOSE_PARENS ")"
191 %token BITWISE_AND "&"
192 %token BITWISE_OR "|"
199 /* C# multi-character operators. */
202 %token OP_SHIFT_LEFT "<<"
203 %token OP_SHIFT_RIGHT ">>"
210 %token OP_MULT_ASSIGN "*="
211 %token OP_DIV_ASSIGN "/="
212 %token OP_MOD_ASSIGN "%="
213 %token OP_ADD_ASSIGN "+="
214 %token OP_SUB_ASSIGN "-="
215 %token OP_SHIFT_LEFT_ASSIGN "<<="
216 %token OP_SHIFT_RIGHT_ASSIGN ">>="
217 %token OP_AND_ASSIGN "&="
218 %token OP_XOR_ASSIGN "^="
219 %token OP_OR_ASSIGN "|="
223 %token LITERAL_INTEGER "int literal"
224 %token LITERAL_FLOAT "float literal"
225 %token LITERAL_DOUBLE "double literal"
226 %token LITERAL_DECIMAL "decimal literal"
227 %token LITERAL_CHARACTER "character literal"
228 %token LITERAL_STRING "string literal"
232 /* Add precedence rules to solve dangling else s/r conflict */
241 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
243 %left STAR DIV PERCENT
244 %right BANG CARRET UMINUS
245 %nonassoc OP_INC OP_DEC
247 %left OPEN_BRACKET OPEN_BRACE
251 %start compilation_unit
255 : outer_declarations opt_EOF
256 | outer_declarations attribute_sections opt_EOF
257 | attribute_sections opt_EOF
258 | opt_EOF /* allow empty files */
268 | outer_declarations outer_declaration
273 | namespace_member_declaration
278 | using_directives using_directive
282 : using_alias_directive
283 | using_namespace_directive
286 using_alias_directive
287 : USING IDENTIFIER ASSIGN
288 namespace_or_type_name SEMICOLON
290 current_namespace.UsingAlias ((string) $2, (string) $4, lexer.Location);
294 using_namespace_directive
295 : USING namespace_name SEMICOLON
297 current_namespace.Using ((string) $2, lexer.Location);
302 // Strictly speaking, namespaces don't have attributes but
303 // we parse global attributes along with namespace declarations and then
306 namespace_declaration
307 : opt_attributes NAMESPACE qualified_identifier
309 Attributes attrs = (Attributes) $1;
312 foreach (AttributeSection asec in attrs.AttributeSections)
313 if (asec.Target == "assembly")
314 RootContext.AddGlobalAttribute (
315 current_container, asec, lexer.Location);
318 current_namespace = RootContext.Tree.RecordNamespace (current_namespace, name, (string) $3);
320 namespace_body opt_semicolon
322 current_namespace = current_namespace.Parent;
338 | qualified_identifier DOT IDENTIFIER {
339 $$ = (($1).ToString ()) + "." + ($3.ToString ()); }
344 : namespace_or_type_name
350 opt_namespace_member_declarations
361 opt_namespace_member_declarations
363 | namespace_member_declarations
366 namespace_member_declarations
367 : namespace_member_declaration
368 | namespace_member_declarations namespace_member_declaration
371 namespace_member_declaration
378 Class c = (Class) $1;
379 mod_flags = c.ModFlags;
381 } else if ($1 is Struct){
382 Struct s = (Struct) $1;
383 mod_flags = s.ModFlags;
388 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
390 1527, lexer.Location,
391 "Namespace elements cant be explicitly " +
392 "declared private or protected in `" + name + "'");
394 current_namespace.DeclarationFound = true;
396 | namespace_declaration
402 | interface_declaration
404 | delegate_declaration
406 // Enable this when we have handled all errors, because this acts as a generic fallback
409 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
419 | attribute_sections { $$ = $1; }
425 AttributeSection sect = (AttributeSection) $1;
427 if (sect.Target == "assembly")
428 RootContext.AddGlobalAttribute (current_container, sect, lexer.Location);
431 $$ = new Attributes ((AttributeSection) $1, lexer.Location);
433 | attribute_sections attribute_section
435 Attributes attrs = null;
436 AttributeSection sect = (AttributeSection) $2;
438 if (sect.Target == "assembly")
439 RootContext.AddGlobalAttribute (current_container, sect, lexer.Location);
442 attrs = (Attributes) $1;
443 attrs.AddAttribute (sect);
451 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
453 string target = null;
456 target = (string) $2;
458 $$ = new AttributeSection (target, (ArrayList) $3);
460 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
462 $$ = new AttributeSection (null, (ArrayList) $2);
466 attribute_target_specifier
467 : attribute_target COLON
476 CheckAttributeTarget ((string) $1);
479 | EVENT { $$ = "event"; }
480 | RETURN { $$ = "return"; }
486 ArrayList attrs = new ArrayList ();
492 | attribute_list COMMA attribute
494 ArrayList attrs = (ArrayList) $1;
506 opt_attribute_arguments
508 $$ = new Attribute ((string) $1, (ArrayList) $3, (Location) $2);
513 : type_name { /* reserved attribute name or identifier: 17.4 */ }
516 opt_attribute_arguments
517 : /* empty */ { $$ = null; }
518 | OPEN_PARENS attribute_arguments CLOSE_PARENS
526 : opt_positional_argument_list
531 ArrayList args = new ArrayList ();
537 | positional_argument_list COMMA named_argument_list
539 ArrayList args = new ArrayList ();
545 | named_argument_list
547 ArrayList args = new ArrayList ();
556 opt_positional_argument_list
557 : /* empty */ { $$ = null; }
558 | positional_argument_list
561 positional_argument_list
564 ArrayList args = new ArrayList ();
565 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
569 | positional_argument_list COMMA expression
571 ArrayList args = (ArrayList) $1;
572 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
581 ArrayList args = new ArrayList ();
586 | named_argument_list COMMA named_argument
588 ArrayList args = (ArrayList) $1;
596 : IDENTIFIER ASSIGN expression
598 $$ = new DictionaryEntry (
600 new Argument ((Expression) $3, Argument.AType.Expression));
606 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
609 opt_class_member_declarations
611 | class_member_declarations
614 class_member_declarations
615 : class_member_declaration
616 | class_member_declarations
617 class_member_declaration
620 class_member_declaration
621 : constant_declaration // done
622 | field_declaration // done
623 | method_declaration // done
624 | property_declaration // done
625 | event_declaration // done
626 | indexer_declaration // done
627 | operator_declaration // done
628 | constructor_declaration // done
629 | destructor_declaration // done
639 string full_struct_name = MakeName ((string) $4);
641 new_struct = new Struct (current_container, full_struct_name, (int) $2,
642 (Attributes) $1, lexer.Location);
643 current_container = new_struct;
644 current_container.Namespace = current_namespace;
645 RootContext.Tree.RecordDecl (full_struct_name, new_struct);
651 Struct new_struct = (Struct) current_container;
654 new_struct.Bases = (ArrayList) $6;
656 current_container = current_container.Parent;
657 CheckDef (current_container.AddStruct (new_struct), new_struct.Name, new_struct.Location);
663 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
666 opt_struct_member_declarations
668 | struct_member_declarations
671 struct_member_declarations
672 : struct_member_declaration
673 | struct_member_declarations struct_member_declaration
676 struct_member_declaration
677 : constant_declaration
680 | property_declaration
682 | indexer_declaration
683 | operator_declaration
684 | constructor_declaration
688 * This is only included so we can flag error 575:
689 * destructors only allowed on class types
691 | destructor_declaration
702 foreach (VariableDeclaration constant in (ArrayList) $5){
703 Location l = constant.Location;
705 Const c = new Const (
706 (Expression) $4, (string) constant.identifier,
707 (Expression) constant.expression_or_array_initializer, (int) $2,
710 CheckDef (current_container.AddConstant (c), c.Name, l);
716 : constant_declarator
718 ArrayList constants = new ArrayList ();
722 | constant_declarators COMMA constant_declarator
724 ArrayList constants = (ArrayList) $1;
731 : IDENTIFIER ASSIGN constant_expression
733 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
744 Expression type = (Expression) $3;
747 foreach (VariableDeclaration var in (ArrayList) $4){
748 Location l = var.Location;
750 Field field = new Field (type, mod, var.identifier,
751 var.expression_or_array_initializer,
754 CheckDef (current_container.AddField (field), field.Name, l);
760 : variable_declarator
762 ArrayList decl = new ArrayList ();
766 | variable_declarators COMMA variable_declarator
768 ArrayList decls = (ArrayList) $1;
775 : IDENTIFIER ASSIGN variable_initializer
777 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
781 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
794 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
796 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
804 Method method = (Method) $1;
805 Block b = (Block) $2;
806 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
809 if ((method.ModFlags & extern_abstract) == 0){
811 501, lexer.Location, current_container.MakeName (method.Name) +
812 "must declare a body because it is not marked abstract or extern");
815 if ((method.ModFlags & Modifiers.EXTERN) != 0){
817 179, lexer.Location, current_container.MakeName (method.Name) +
818 " is declared extern, but has a body");
822 method.Block = (Block) $2;
823 CheckDef (current_container.AddMethod (method), method.Name, method.Location);
825 current_local_parameters = null;
839 1585, lexer.Location, "Member modifier `" +
840 Modifiers.Name (i) + "' must precede member type and name");
853 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
855 Method method = new Method ((Expression) $3, (int) $2, (string) $4,
856 (Parameters) $6, (Attributes) $1, lexer.Location);
858 current_local_parameters = (Parameters) $6;
866 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
868 Method method = new Method (TypeManager.system_void_expr, (int) $2, (string) $4,
869 (Parameters) $6, (Attributes) $1, lexer.Location);
871 current_local_parameters = (Parameters) $6;
878 | SEMICOLON { $$ = null; }
881 opt_formal_parameter_list
882 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
883 | formal_parameter_list
886 formal_parameter_list
889 ArrayList pars_list = (ArrayList) $1;
891 Parameter [] pars = new Parameter [pars_list.Count];
892 pars_list.CopyTo (pars);
894 $$ = new Parameters (pars, null, lexer.Location);
896 | fixed_parameters COMMA parameter_array
898 ArrayList pars_list = (ArrayList) $1;
900 Parameter [] pars = new Parameter [pars_list.Count];
901 pars_list.CopyTo (pars);
903 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
907 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
914 ArrayList pars = new ArrayList ();
919 | fixed_parameters COMMA fixed_parameter
921 ArrayList pars = (ArrayList) $1;
930 opt_parameter_modifier
934 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
938 opt_parameter_modifier
939 : /* empty */ { $$ = Parameter.Modifier.NONE; }
944 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
945 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
949 : opt_attributes PARAMS type IDENTIFIER
951 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
952 note ("type must be a single-dimension array type");
957 : qualified_identifier
966 implicit_value_parameter_type = (Expression) $3;
968 lexer.PropertyParsing = true;
972 accessor_declarations
974 lexer.PropertyParsing = false;
979 Pair pair = (Pair) $7;
980 Accessor get_block = (Accessor) pair.First;
981 Accessor set_block = (Accessor) pair.Second;
983 Location loc = (Location) $6;
984 prop = new Property ((Expression) $3, (string) $4, (int) $2, get_block, set_block,
985 (Attributes) $1, loc);
987 CheckDef (current_container.AddProperty (prop), prop.Name, loc);
988 implicit_value_parameter_type = null;
992 accessor_declarations
993 : get_accessor_declaration opt_set_accessor_declaration
995 $$ = new Pair ($1, $2);
997 | set_accessor_declaration opt_get_accessor_declaration
999 $$ = new Pair ($2, $1);
1003 opt_get_accessor_declaration
1004 : /* empty */ { $$ = null; }
1005 | get_accessor_declaration
1008 opt_set_accessor_declaration
1009 : /* empty */ { $$ = null; }
1010 | set_accessor_declaration
1013 get_accessor_declaration
1014 : opt_attributes GET
1016 // If this is not the case, then current_local_parameters has already
1017 // been set in indexer_declaration
1018 if (parsing_indexer == false)
1019 current_local_parameters = null;
1021 current_local_parameters = indexer_parameters;
1022 lexer.PropertyParsing = false;
1026 $$ = new Accessor ((Block) $4, (Attributes) $1);
1027 current_local_parameters = null;
1028 lexer.PropertyParsing = true;
1032 set_accessor_declaration
1033 : opt_attributes SET
1036 Parameter implicit_value_parameter = new Parameter (
1037 implicit_value_parameter_type, "value",
1038 Parameter.Modifier.NONE, null);
1040 if (parsing_indexer == false) {
1041 args = new Parameter [1];
1042 args [0] = implicit_value_parameter;
1043 current_local_parameters = new Parameters (args, null, lexer.Location);
1045 Parameter [] fpars = indexer_parameters.FixedParameters;
1048 int count = fpars.Length;
1050 args = new Parameter [count + 1];
1051 fpars.CopyTo (args, 0);
1052 args [count] = implicit_value_parameter;
1055 current_local_parameters = new Parameters (
1056 args, indexer_parameters.ArrayParameter, lexer.Location);
1059 lexer.PropertyParsing = false;
1063 $$ = new Accessor ((Block) $4, (Attributes) $1);
1064 current_local_parameters = null;
1065 lexer.PropertyParsing = true;
1071 | SEMICOLON { $$ = null; }
1074 interface_declaration
1077 INTERFACE IDENTIFIER
1079 Interface new_interface;
1080 string full_interface_name = MakeName ((string) $4);
1082 new_interface = new Interface (current_container, full_interface_name, (int) $2,
1083 (Attributes) $1, lexer.Location);
1084 if (current_interface != null) {
1085 Location l = lexer.Location;
1086 Report.Error (-2, l, "Internal compiler error: interface inside interface");
1088 current_interface = new_interface;
1089 new_interface.Namespace = current_namespace;
1090 RootContext.Tree.RecordDecl (full_interface_name, new_interface);
1093 interface_body opt_semicolon
1095 Interface new_interface = (Interface) current_interface;
1098 new_interface.Bases = (ArrayList) $6;
1100 current_interface = null;
1101 CheckDef (current_container.AddInterface (new_interface),
1102 new_interface.Name, new_interface.Location);
1107 : /* empty */ { $$ = null; }
1112 : COLON interface_type_list { $$ = $2; }
1118 ArrayList interfaces = new ArrayList ();
1120 interfaces.Add ($1);
1123 | interface_type_list COMMA interface_type
1125 ArrayList interfaces = (ArrayList) $1;
1126 interfaces.Add ($3);
1133 opt_interface_member_declarations
1137 opt_interface_member_declarations
1139 | interface_member_declarations
1142 interface_member_declarations
1143 : interface_member_declaration
1144 | interface_member_declarations interface_member_declaration
1147 interface_member_declaration
1148 : interface_method_declaration
1150 InterfaceMethod m = (InterfaceMethod) $1;
1152 CheckDef (current_interface.AddMethod (m), m.Name, m.Location);
1154 | interface_property_declaration
1156 InterfaceProperty p = (InterfaceProperty) $1;
1158 CheckDef (current_interface.AddProperty (p), p.Name, p.Location);
1160 | interface_event_declaration
1162 InterfaceEvent e = (InterfaceEvent) $1;
1164 CheckDef (current_interface.AddEvent (e), e.Name, lexer.Location);
1166 | interface_indexer_declaration
1168 InterfaceIndexer i = (InterfaceIndexer) $1;
1170 CheckDef (current_interface.AddIndexer (i), "indexer", i.Location);
1175 : /* empty */ { $$ = false; }
1176 | NEW { $$ = true; }
1179 interface_method_declaration
1180 : opt_attributes opt_new type IDENTIFIER
1181 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1184 $$ = new InterfaceMethod ((Expression) $3, (string) $4, (bool) $2,
1185 (Parameters) $6, (Attributes) $1, lexer.Location);
1187 | opt_attributes opt_new VOID IDENTIFIER
1188 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1191 $$ = new InterfaceMethod (
1192 TypeManager.system_void_expr, (string) $4, (bool) $2, (Parameters) $6,
1193 (Attributes) $1, lexer.Location);
1197 interface_property_declaration
1202 { lexer.PropertyParsing = true; }
1204 { lexer.PropertyParsing = false; }
1209 $$ = new InterfaceProperty ((Expression) $3, (string) $4, (bool) $2,
1210 (gs & 1) == 1, (gs & 2) == 2, (Attributes) $1,
1216 : opt_attributes GET SEMICOLON { $$ = 1; }
1217 | opt_attributes SET SEMICOLON { $$ = 2; }
1218 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1220 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1224 interface_event_declaration
1225 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1227 $$ = new InterfaceEvent ((Expression) $4, (string) $5, (bool) $2, (Attributes) $1,
1232 interface_indexer_declaration
1233 : opt_attributes opt_new type THIS
1234 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1236 { lexer.PropertyParsing = true; }
1238 { lexer.PropertyParsing = false; }
1241 int a_flags = (int) $10;
1243 bool do_get = (a_flags & 1) == 1;
1244 bool do_set = (a_flags & 2) == 2;
1246 $$ = new InterfaceIndexer ((Expression) $3, (Parameters) $6, do_get, do_set,
1247 (bool) $2, (Attributes) $1, lexer.Location);
1251 operator_declaration
1252 : opt_attributes opt_modifiers operator_declarator operator_body
1254 OperatorDeclaration decl = (OperatorDeclaration) $3;
1256 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
1257 decl.arg2type, decl.arg2name, (Block) $4, (Attributes) $1, decl.location);
1259 // Note again, checking is done in semantic analysis
1260 current_container.AddOperator (op);
1262 current_local_parameters = null;
1268 | SEMICOLON { $$ = null; }
1271 : type OPERATOR overloadable_operator
1272 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1274 Operator.OpType op = (Operator.OpType) $3;
1275 CheckUnaryOperator (op);
1277 if (op == Operator.OpType.Addition)
1278 op = Operator.OpType.UnaryPlus;
1280 if (op == Operator.OpType.Subtraction)
1281 op = Operator.OpType.UnaryNegation;
1283 Parameter [] pars = new Parameter [1];
1285 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1287 current_local_parameters = new Parameters (pars, null, lexer.Location);
1289 $$ = new OperatorDeclaration (op, (Expression) $1, (Expression) $5, (string) $6,
1290 null, null, lexer.Location);
1292 | type OPERATOR overloadable_operator
1294 type IDENTIFIER COMMA
1298 CheckBinaryOperator ((Operator.OpType) $3);
1300 Parameter [] pars = new Parameter [2];
1302 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1303 pars [1] = new Parameter ((Expression) $8, (string) $9, Parameter.Modifier.NONE, null);
1305 current_local_parameters = new Parameters (pars, null, lexer.Location);
1307 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1308 (Expression) $5, (string) $6,
1309 (Expression) $8, (string) $9, lexer.Location);
1311 | conversion_operator_declarator
1314 overloadable_operator
1316 : BANG { $$ = Operator.OpType.LogicalNot; }
1317 | TILDE { $$ = Operator.OpType.OnesComplement; }
1318 | OP_INC { $$ = Operator.OpType.Increment; }
1319 | OP_DEC { $$ = Operator.OpType.Decrement; }
1320 | TRUE { $$ = Operator.OpType.True; }
1321 | FALSE { $$ = Operator.OpType.False; }
1322 // Unary and binary:
1323 | PLUS { $$ = Operator.OpType.Addition; }
1324 | MINUS { $$ = Operator.OpType.Subtraction; }
1326 | STAR { $$ = Operator.OpType.Multiply; }
1327 | DIV { $$ = Operator.OpType.Division; }
1328 | PERCENT { $$ = Operator.OpType.Modulus; }
1329 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1330 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1331 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1332 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1333 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1334 | OP_EQ { $$ = Operator.OpType.Equality; }
1335 | OP_NE { $$ = Operator.OpType.Inequality; }
1336 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1337 | OP_LT { $$ = Operator.OpType.LessThan; }
1338 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1339 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1342 conversion_operator_declarator
1343 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1345 Parameter [] pars = new Parameter [1];
1347 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1349 current_local_parameters = new Parameters (pars, null, lexer.Location);
1351 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1352 null, null, lexer.Location);
1354 | EXPLICIT 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.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1363 null, null, lexer.Location);
1367 syntax_error (lexer.Location, "'operator' expected");
1371 syntax_error (lexer.Location, "'operator' expected");
1375 constructor_declaration
1378 constructor_declarator
1381 Constructor c = (Constructor) $3;
1382 c.Block = (Block) $4;
1383 c.OptAttributes = (Attributes) $1;
1385 if ((c.ModFlags & Modifiers.STATIC) != 0){
1386 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, 0, c.Location);
1388 if ((c.ModFlags & Modifiers.Accessibility) != 0) {
1391 "Access modifiers are not allowed on static constructors");
1394 if (c.Initializer != null){
1397 "Static constructors can not have an explicit this or base " +
1398 "constructor invocations");
1401 if (!c.Parameters.Empty){
1403 132, c.Location, "Static constructors should not have parameters");
1406 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1409 CheckDef (current_container.AddConstructor (c), c.Name, c.Location);
1411 current_local_parameters = null;
1415 constructor_declarator
1417 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1419 oob_stack.Push (lexer.Location);
1421 current_local_parameters = (Parameters) $3;
1423 opt_constructor_initializer
1425 Location l = (Location) oob_stack.Pop ();
1426 $$ = new Constructor ((string) $1, (Parameters) $3, (ConstructorInitializer) $6, l);
1432 | SEMICOLON { $$ = null; }
1435 opt_constructor_initializer
1436 : /* empty */ { $$ = null; }
1437 | constructor_initializer
1440 constructor_initializer
1441 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1443 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1445 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1447 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1451 destructor_declaration
1452 : opt_attributes TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1454 if ((string) $3 != current_container.Basename){
1455 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1456 } else if (!(current_container is Class)){
1457 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1459 Location l = lexer.Location;
1462 if (!RootContext.StdLib && current_container.Name == "System.Object")
1463 m = Modifiers.PROTECTED | Modifiers.VIRTUAL;
1465 m = Modifiers.PROTECTED | Modifiers.OVERRIDE;
1467 Method d = new Method (
1468 TypeManager.system_void_expr, m, "Finalize",
1469 new Parameters (null, null, l), (Attributes) $1, l);
1471 d.Block = (Block) $6;
1472 CheckDef (current_container.AddMethod (d), d.Name, d.Location);
1480 EVENT type variable_declarators SEMICOLON
1482 foreach (VariableDeclaration var in (ArrayList) $5) {
1484 Event e = new Event ((Expression) $4, var.identifier,
1485 var.expression_or_array_initializer,
1486 (int) $2, null, null, (Attributes) $1, lexer.Location);
1488 CheckDef (current_container.AddEvent (e), e.Name, e.Location);
1494 EVENT type member_name
1497 implicit_value_parameter_type = (Expression) $4;
1498 lexer.EventParsing = true;
1499 oob_stack.Push (lexer.Location);
1501 event_accessor_declarations
1503 lexer.EventParsing = false;
1507 Location loc = (Location) oob_stack.Pop ();
1509 Pair pair = (Pair) $8;
1510 Accessor add_accessor = null;
1511 Accessor rem_accessor = null;
1513 if (pair.First != null)
1514 add_accessor = (Accessor) pair.First;
1515 if (pair.Second != null)
1516 rem_accessor = (Accessor) pair.Second;
1518 Event e = new Event ((Expression) $4, (string) $5, null, (int) $2, add_accessor, rem_accessor,
1519 (Attributes) $1, loc);
1521 CheckDef (current_container.AddEvent (e), e.Name, loc);
1522 implicit_value_parameter_type = null;
1526 event_accessor_declarations
1527 : add_accessor_declaration remove_accessor_declaration
1529 $$ = new Pair ($1, $2);
1531 | remove_accessor_declaration add_accessor_declaration
1533 $$ = new Pair ($2, $1);
1537 add_accessor_declaration
1538 : opt_attributes ADD
1540 Parameter [] args = new Parameter [1];
1541 Parameter implicit_value_parameter = new Parameter (
1542 implicit_value_parameter_type, "value",
1543 Parameter.Modifier.NONE, null);
1545 args [0] = implicit_value_parameter;
1547 current_local_parameters = new Parameters (args, null, lexer.Location);
1548 lexer.EventParsing = false;
1552 $$ = new Accessor ((Block) $4, (Attributes) $1);
1553 lexer.EventParsing = true;
1557 remove_accessor_declaration
1558 : opt_attributes REMOVE
1560 Parameter [] args = new Parameter [1];
1561 Parameter implicit_value_parameter = new Parameter (
1562 implicit_value_parameter_type, "value",
1563 Parameter.Modifier.NONE, null);
1565 args [0] = implicit_value_parameter;
1567 current_local_parameters = new Parameters (args, null, lexer.Location);
1568 lexer.EventParsing = false;
1572 $$ = new Accessor ((Block) $4, (Attributes) $1);
1573 lexer.EventParsing = true;
1578 : opt_attributes opt_modifiers indexer_declarator
1581 IndexerDeclaration decl = (IndexerDeclaration) $3;
1583 implicit_value_parameter_type = decl.type;
1585 lexer.PropertyParsing = true;
1586 parsing_indexer = true;
1588 indexer_parameters = decl.param_list;
1589 oob_stack.Push (lexer.Location);
1591 accessor_declarations
1593 lexer.PropertyParsing = false;
1594 parsing_indexer = false;
1598 // The signature is computed from the signature of the indexer. Look
1599 // at section 3.6 on the spec
1600 Location loc = (Location) oob_stack.Pop ();
1602 IndexerDeclaration decl = (IndexerDeclaration) $3;
1603 Pair pair = (Pair) $6;
1604 Accessor get_block = (Accessor) pair.First;
1605 Accessor set_block = (Accessor) pair.Second;
1607 indexer = new Indexer (decl.type, decl.interface_type, (int) $2, decl.param_list,
1608 get_block, set_block, (Attributes) $1, loc);
1610 // Note that there is no equivalent of CheckDef for this case
1611 // We shall handle this in semantic analysis
1613 current_container.AddIndexer (indexer);
1615 current_local_parameters = null;
1616 implicit_value_parameter_type = null;
1617 indexer_parameters = null;
1622 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1624 Parameters pars = (Parameters) $4;
1626 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1627 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1630 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
1632 | type qualified_identifier DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1634 Parameters pars = (Parameters) $6;
1636 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1637 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1639 $$ = new IndexerDeclaration ((Expression) $1, (string) $2, pars);
1651 Location enum_location = lexer.Location;
1653 string full_name = MakeName ((string) $4);
1654 Enum e = new Enum (current_container, (Expression) $5, (int) $2, full_name,
1655 (Attributes) $1, enum_location);
1657 foreach (VariableDeclaration ev in (ArrayList) $6) {
1658 Location loc = (Location) ev.Location;
1660 CheckDef (e.AddEnumMember (ev.identifier,
1661 (Expression) ev.expression_or_array_initializer,
1662 loc, ev.OptAttributes),
1663 ev.identifier, loc);
1666 e.Namespace = current_namespace;
1668 CheckDef (current_container.AddEnum (e), full_name, enum_location);
1669 RootContext.Tree.RecordDecl (full_name, e);
1675 : /* empty */ { $$ = TypeManager.system_int32_expr; }
1676 | COLON type { $$ = $2; }
1680 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1686 opt_enum_member_declarations
1687 : /* empty */ { $$ = new ArrayList (); }
1688 | enum_member_declarations opt_comma { $$ = $1; }
1691 enum_member_declarations
1692 : enum_member_declaration
1694 ArrayList l = new ArrayList ();
1699 | enum_member_declarations COMMA enum_member_declaration
1701 ArrayList l = (ArrayList) $1;
1709 enum_member_declaration
1710 : opt_attributes IDENTIFIER
1712 $$ = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
1714 | opt_attributes IDENTIFIER
1716 $$ = lexer.Location;
1720 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
1724 delegate_declaration
1728 IDENTIFIER OPEN_PARENS
1729 opt_formal_parameter_list
1733 Location l = lexer.Location;
1734 Delegate del = new Delegate (current_container, (Expression) $4, (int) $2,
1735 MakeName ((string) $5), (Parameters) $7,
1736 (Attributes) $1, l);
1738 del.Namespace = current_namespace;
1739 CheckDef (current_container.AddDelegate (del), del.Name, l);
1744 IDENTIFIER OPEN_PARENS
1745 opt_formal_parameter_list
1749 Location l = lexer.Location;
1750 Delegate del = new Delegate (
1752 TypeManager.system_void_expr, (int) $2, MakeName ((string) $5),
1753 (Parameters) $7, (Attributes) $1, l);
1755 del.Namespace = current_namespace;
1756 CheckDef (current_container.AddDelegate (del), del.Name, l);
1761 : namespace_or_type_name
1764 namespace_or_type_name
1765 : qualified_identifier
1769 * Before you think of adding a return_type, notice that we have been
1770 * using two rules in the places where it matters (one rule using type
1771 * and another identical one that uses VOID as the return type). This
1772 * gets rid of a shift/reduce couple
1775 : type_name { /* class_type */
1777 This does interfaces, delegates, struct_types, class_types,
1778 parent classes, and more! 4.2
1780 $$ = DecomposeQI ((string) $1, lexer.Location);
1792 // Note that here only unmanaged types are allowed but we
1793 // can't perform checks during this phase - we do it during
1794 // semantic analysis.
1796 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1800 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
1806 | non_expression_type rank_specifier
1808 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1810 | non_expression_type STAR
1812 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1814 | expression rank_specifiers
1816 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1820 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1824 // We need this because the parser will happily go and reduce IDENTIFIER STAR
1825 // through this different path
1827 | multiplicative_expression STAR
1829 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1836 ArrayList types = new ArrayList ();
1841 | type_list COMMA type
1843 ArrayList types = (ArrayList) $1;
1851 * replaces all the productions for isolating the various
1852 * simple types, but we need this to reuse it easily in local_variable_type
1855 : OBJECT { $$ = TypeManager.system_object_expr; }
1856 | STRING { $$ = TypeManager.system_string_expr; }
1857 | BOOL { $$ = TypeManager.system_boolean_expr; }
1858 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
1859 | FLOAT { $$ = TypeManager.system_single_expr; }
1860 | DOUBLE { $$ = TypeManager.system_double_expr; }
1865 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
1866 | BYTE { $$ = TypeManager.system_byte_expr; }
1867 | SHORT { $$ = TypeManager.system_int16_expr; }
1868 | USHORT { $$ = TypeManager.system_uint16_expr; }
1869 | INT { $$ = TypeManager.system_int32_expr; }
1870 | UINT { $$ = TypeManager.system_uint32_expr; }
1871 | LONG { $$ = TypeManager.system_int64_expr; }
1872 | ULONG { $$ = TypeManager.system_uint64_expr; }
1873 | CHAR { $$ = TypeManager.system_char_expr; }
1874 | VOID { $$ = TypeManager.system_void_expr; }
1882 : type rank_specifiers
1884 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1889 // Expressions, section 7.5
1897 | qualified_identifier
1899 string name = (string) $1;
1902 $$ = DecomposeQI (name, lexer.Location);
1904 | parenthesized_expression
1906 | invocation_expression
1910 | post_increment_expression
1911 | post_decrement_expression
1915 | checked_expression
1916 | unchecked_expression
1917 | pointer_member_access
1924 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
1925 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
1926 | NULL { $$ = NullLiteral.Null; }
1930 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
1931 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
1932 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
1937 object v = lexer.Value;
1940 $$ = new IntLiteral ((Int32) v);
1942 $$ = new UIntLiteral ((UInt32) v);
1944 $$ = new LongLiteral ((Int64) v);
1945 else if (v is ulong)
1946 $$ = new ULongLiteral ((UInt64) v);
1948 Console.WriteLine ("OOPS. Unexpected result from scanner");
1953 : TRUE { $$ = new BoolLiteral (true); }
1954 | FALSE { $$ = new BoolLiteral (false); }
1957 parenthesized_expression
1958 : OPEN_PARENS expression CLOSE_PARENS
1963 : primary_expression DOT IDENTIFIER
1965 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1967 | predefined_type DOT IDENTIFIER
1969 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1977 invocation_expression
1978 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
1981 Location l = lexer.Location;
1982 Report.Error (1, l, "Parse error");
1984 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
1989 : /* empty */ { $$ = null; }
1996 ArrayList list = new ArrayList ();
2000 | argument_list COMMA argument
2002 ArrayList list = (ArrayList) $1;
2011 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2013 | REF variable_reference
2015 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2017 | OUT variable_reference
2019 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2024 : expression { note ("section 5.4"); $$ = $1; }
2028 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2030 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2032 | primary_expression rank_specifiers
2034 // So the super-trick is that primary_expression
2035 // can only be either a SimpleName or a MemberAccess.
2036 // The MemberAccess case arises when you have a fully qualified type-name like :
2038 // SimpleName is when you have
2041 Expression expr = (Expression) $1;
2042 if (!(expr is SimpleName || expr is MemberAccess)) {
2043 Error_ExpectingTypeName (lexer.Location, expr);
2044 $$ = TypeManager.system_object_expr;
2047 // So we extract the string corresponding to the SimpleName
2050 $$ = new SimpleName (GetQualifiedIdentifier (expr) + (string) $2, lexer.Location);
2058 ArrayList list = new ArrayList ();
2062 | expression_list COMMA expression
2064 ArrayList list = (ArrayList) $1;
2073 $$ = new This (current_block, lexer.Location);
2078 : BASE DOT IDENTIFIER
2080 $$ = new BaseAccess ((string) $3, lexer.Location);
2082 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2084 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2088 post_increment_expression
2089 : primary_expression OP_INC
2091 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2092 (Expression) $1, lexer.Location);
2096 post_decrement_expression
2097 : primary_expression OP_DEC
2099 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2100 (Expression) $1, lexer.Location);
2105 : object_or_delegate_creation_expression
2106 | array_creation_expression
2109 object_or_delegate_creation_expression
2110 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2112 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2116 array_creation_expression
2117 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2119 opt_array_initializer
2121 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2123 | NEW type rank_specifiers array_initializer
2125 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2129 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2145 : rank_specifier opt_rank_specifier
2147 $$ = (string) $2 + (string) $1;
2152 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2154 $$ = "[" + (string) $2 + "]";
2174 | dim_separators COMMA
2176 $$ = (string) $1 + ",";
2180 opt_array_initializer
2192 : OPEN_BRACE CLOSE_BRACE
2194 ArrayList list = new ArrayList ();
2197 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2199 $$ = (ArrayList) $2;
2203 variable_initializer_list
2204 : variable_initializer
2206 ArrayList list = new ArrayList ();
2210 | variable_initializer_list COMMA variable_initializer
2212 ArrayList list = (ArrayList) $1;
2219 : TYPEOF OPEN_PARENS type CLOSE_PARENS
2221 $$ = new TypeOf ((Expression) $3, lexer.Location);
2226 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2227 $$ = new SizeOf ((Expression) $3, lexer.Location);
2232 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2234 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2238 unchecked_expression
2239 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2241 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2245 pointer_member_access
2246 : primary_expression OP_PTR IDENTIFIER
2250 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2251 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2255 : primary_expression
2256 | BANG prefixed_unary_expression
2258 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2260 | TILDE prefixed_unary_expression
2262 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2268 : OPEN_PARENS expression CLOSE_PARENS unary_expression
2270 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2272 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2274 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2279 // The idea to split this out is from Rhys' grammar
2280 // to solve the problem with casts.
2282 prefixed_unary_expression
2284 | PLUS prefixed_unary_expression
2286 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2288 | MINUS prefixed_unary_expression
2290 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2292 | OP_INC prefixed_unary_expression
2294 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2295 (Expression) $2, lexer.Location);
2297 | OP_DEC prefixed_unary_expression
2299 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2300 (Expression) $2, lexer.Location);
2302 | STAR prefixed_unary_expression
2304 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2306 | BITWISE_AND prefixed_unary_expression
2308 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2312 pre_increment_expression
2313 : OP_INC prefixed_unary_expression
2315 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2316 (Expression) $2, lexer.Location);
2320 pre_decrement_expression
2321 : OP_DEC prefixed_unary_expression
2323 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2324 (Expression) $2, lexer.Location);
2328 multiplicative_expression
2329 : prefixed_unary_expression
2330 | multiplicative_expression STAR prefixed_unary_expression
2332 $$ = new Binary (Binary.Operator.Multiply,
2333 (Expression) $1, (Expression) $3, lexer.Location);
2335 | multiplicative_expression DIV prefixed_unary_expression
2337 $$ = new Binary (Binary.Operator.Division,
2338 (Expression) $1, (Expression) $3, lexer.Location);
2340 | multiplicative_expression PERCENT prefixed_unary_expression
2342 $$ = new Binary (Binary.Operator.Modulus,
2343 (Expression) $1, (Expression) $3, lexer.Location);
2348 : multiplicative_expression
2349 | additive_expression PLUS multiplicative_expression
2351 $$ = new Binary (Binary.Operator.Addition,
2352 (Expression) $1, (Expression) $3, lexer.Location);
2354 | additive_expression MINUS multiplicative_expression
2356 $$ = new Binary (Binary.Operator.Subtraction,
2357 (Expression) $1, (Expression) $3, lexer.Location);
2362 : additive_expression
2363 | shift_expression OP_SHIFT_LEFT additive_expression
2365 $$ = new Binary (Binary.Operator.LeftShift,
2366 (Expression) $1, (Expression) $3, lexer.Location);
2368 | shift_expression OP_SHIFT_RIGHT additive_expression
2370 $$ = new Binary (Binary.Operator.RightShift,
2371 (Expression) $1, (Expression) $3, lexer.Location);
2375 relational_expression
2377 | relational_expression OP_LT shift_expression
2379 $$ = new Binary (Binary.Operator.LessThan,
2380 (Expression) $1, (Expression) $3, lexer.Location);
2382 | relational_expression OP_GT shift_expression
2384 $$ = new Binary (Binary.Operator.GreaterThan,
2385 (Expression) $1, (Expression) $3, lexer.Location);
2387 | relational_expression OP_LE shift_expression
2389 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2390 (Expression) $1, (Expression) $3, lexer.Location);
2392 | relational_expression OP_GE shift_expression
2394 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2395 (Expression) $1, (Expression) $3, lexer.Location);
2397 | relational_expression IS type
2399 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
2401 | relational_expression AS type
2403 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
2408 : relational_expression
2409 | equality_expression OP_EQ relational_expression
2411 $$ = new Binary (Binary.Operator.Equality,
2412 (Expression) $1, (Expression) $3, lexer.Location);
2414 | equality_expression OP_NE relational_expression
2416 $$ = new Binary (Binary.Operator.Inequality,
2417 (Expression) $1, (Expression) $3, lexer.Location);
2422 : equality_expression
2423 | and_expression BITWISE_AND equality_expression
2425 $$ = new Binary (Binary.Operator.BitwiseAnd,
2426 (Expression) $1, (Expression) $3, lexer.Location);
2430 exclusive_or_expression
2432 | exclusive_or_expression CARRET and_expression
2434 $$ = new Binary (Binary.Operator.ExclusiveOr,
2435 (Expression) $1, (Expression) $3, lexer.Location);
2439 inclusive_or_expression
2440 : exclusive_or_expression
2441 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2443 $$ = new Binary (Binary.Operator.BitwiseOr,
2444 (Expression) $1, (Expression) $3, lexer.Location);
2448 conditional_and_expression
2449 : inclusive_or_expression
2450 | conditional_and_expression OP_AND inclusive_or_expression
2452 $$ = new Binary (Binary.Operator.LogicalAnd,
2453 (Expression) $1, (Expression) $3, lexer.Location);
2457 conditional_or_expression
2458 : conditional_and_expression
2459 | conditional_or_expression OP_OR conditional_and_expression
2461 $$ = new Binary (Binary.Operator.LogicalOr,
2462 (Expression) $1, (Expression) $3, lexer.Location);
2466 conditional_expression
2467 : conditional_or_expression
2468 | conditional_or_expression INTERR expression COLON expression
2470 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2474 assignment_expression
2475 : prefixed_unary_expression ASSIGN expression
2477 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2479 | prefixed_unary_expression OP_MULT_ASSIGN expression
2481 Location l = lexer.Location;
2483 $$ = new CompoundAssign (
2484 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
2486 | prefixed_unary_expression OP_DIV_ASSIGN expression
2488 Location l = lexer.Location;
2490 $$ = new CompoundAssign (
2491 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
2493 | prefixed_unary_expression OP_MOD_ASSIGN expression
2495 Location l = lexer.Location;
2497 $$ = new CompoundAssign (
2498 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
2500 | prefixed_unary_expression OP_ADD_ASSIGN expression
2502 Location l = lexer.Location;
2504 $$ = new CompoundAssign (
2505 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
2507 | prefixed_unary_expression OP_SUB_ASSIGN expression
2509 Location l = lexer.Location;
2511 $$ = new CompoundAssign (
2512 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
2514 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2516 Location l = lexer.Location;
2518 $$ = new CompoundAssign (
2519 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
2521 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2523 Location l = lexer.Location;
2525 $$ = new CompoundAssign (
2526 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
2528 | prefixed_unary_expression OP_AND_ASSIGN expression
2530 Location l = lexer.Location;
2532 $$ = new CompoundAssign (
2533 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
2535 | prefixed_unary_expression OP_OR_ASSIGN expression
2537 Location l = lexer.Location;
2539 $$ = new CompoundAssign (
2540 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
2542 | prefixed_unary_expression OP_XOR_ASSIGN expression
2544 Location l = lexer.Location;
2546 $$ = new CompoundAssign (
2547 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
2552 : conditional_expression
2553 | assignment_expression
2575 name = MakeName ((string) $4);
2577 new_class = new Class (current_container, name, (int) $2,
2578 (Attributes) $1, lexer.Location);
2579 current_container = new_class;
2580 current_container.Namespace = current_namespace;
2581 RootContext.Tree.RecordDecl (name, new_class);
2587 Class new_class = (Class) current_container;
2590 new_class.Bases = (ArrayList) $6;
2592 current_container = current_container.Parent;
2593 CheckDef (current_container.AddClass (new_class), new_class.Name, new_class.Location);
2600 : /* empty */ { $$ = (int) 0; }
2606 | modifiers modifier
2611 if ((m1 & m2) != 0) {
2612 Location l = lexer.Location;
2613 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2615 $$ = (int) (m1 | m2);
2620 : NEW { $$ = Modifiers.NEW; }
2621 | PUBLIC { $$ = Modifiers.PUBLIC; }
2622 | PROTECTED { $$ = Modifiers.PROTECTED; }
2623 | INTERNAL { $$ = Modifiers.INTERNAL; }
2624 | PRIVATE { $$ = Modifiers.PRIVATE; }
2625 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2626 | SEALED { $$ = Modifiers.SEALED; }
2627 | STATIC { $$ = Modifiers.STATIC; }
2628 | READONLY { $$ = Modifiers.READONLY; }
2629 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2630 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2631 | EXTERN { $$ = Modifiers.EXTERN; }
2632 | VOLATILE { $$ = Modifiers.VOLATILE; }
2633 | UNSAFE { $$ = Modifiers.UNSAFE; }
2637 : /* empty */ { $$ = null; }
2638 | class_base { $$ = $1; }
2642 : COLON type_list { $$ = $2; }
2650 // A block is "contained" on the following places:
2652 // property_declaration as part of the accessor body (get/set)
2653 // operator_declaration
2654 // constructor_declaration
2655 // destructor_declaration
2656 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2661 current_block = new Block (current_block, current_local_parameters,
2662 lexer.Location, Location.Null);
2664 opt_statement_list CLOSE_BRACE
2666 while (current_block.Implicit)
2667 current_block = current_block.Parent;
2669 current_block.SetEndLocation (lexer.Location);
2670 current_block = current_block.Parent;
2681 | statement_list statement
2685 : declaration_statement
2687 if ($1 != null && (Block) $1 != current_block){
2688 current_block.AddStatement ((Statement) $1);
2689 current_block = (Block) $1;
2692 | embedded_statement
2694 Statement s = (Statement) $1;
2697 current_block.AddStatement ((Statement) $1);
2705 | expression_statement
2706 | selection_statement
2707 | iteration_statement
2711 | unchecked_statement
2721 $$ = new EmptyStatement ();
2728 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
2730 if (!current_block.AddLabel ((string) $1, labeled)){
2731 Location l = lexer.Location;
2732 Report.Error (140, l, "The label '" + ((string) $1) + "' is a duplicate");
2734 current_block.AddStatement (labeled);
2739 declaration_statement
2740 : local_variable_declaration SEMICOLON
2743 DictionaryEntry de = (DictionaryEntry) $1;
2745 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
2749 | local_constant_declaration SEMICOLON
2752 DictionaryEntry de = (DictionaryEntry) $1;
2754 $$ = declare_local_constant ((Expression) de.Key, (VariableDeclaration) de.Value);
2760 * The following is from Rhys' grammar:
2761 * > Types in local variable declarations must be recognized as
2762 * > expressions to prevent reduce/reduce errors in the grammar.
2763 * > The expressions are converted into types during semantic analysis.
2766 : primary_expression opt_rank_specifier
2768 // FIXME: Do something smart here regarding the composition of the type.
2770 // Ok, the above "primary_expression" is there to get rid of
2771 // both reduce/reduce and shift/reduces in the grammar, it should
2772 // really just be "type_name". If you use type_name, a reduce/reduce
2773 // creeps up. If you use qualified_identifier (which is all we need
2774 // really) two shift/reduces appear.
2777 // So the super-trick is that primary_expression
2778 // can only be either a SimpleName or a MemberAccess.
2779 // The MemberAccess case arises when you have a fully qualified type-name like :
2781 // SimpleName is when you have
2784 Expression expr = (Expression) $1;
2785 if (!(expr is SimpleName || expr is MemberAccess)) {
2786 Error_ExpectingTypeName (lexer.Location, expr);
2790 // So we extract the string corresponding to the SimpleName
2793 if ((string) $2 == "")
2796 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2799 | builtin_types opt_rank_specifier
2801 if ((string) $2 == "")
2804 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2808 local_variable_pointer_type
2809 : primary_expression STAR
2811 Expression expr = (Expression) $1;
2812 Location l = lexer.Location;
2814 if (!(expr is SimpleName || expr is MemberAccess)) {
2815 Error_ExpectingTypeName (l, expr);
2819 $$ = new ComposedCast ((Expression) $1, "*", l);
2821 | builtin_types STAR
2823 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
2827 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
2829 | local_variable_pointer_type STAR
2831 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2835 local_variable_declaration
2836 : local_variable_type variable_declarators
2839 $$ = new DictionaryEntry ($1, $2);
2843 | local_variable_pointer_type opt_rank_specifier variable_declarators
2848 if ((string) $2 == "")
2849 t = (Expression) $1;
2851 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2852 $$ = new DictionaryEntry (t, $3);
2858 local_constant_declaration
2859 : CONST local_variable_type constant_declarator
2862 $$ = new DictionaryEntry ($2, $3);
2868 expression_statement
2869 : statement_expression SEMICOLON
2876 // We have to do the wrapping here and not in the case above,
2877 // because statement_expression is used for example in for_statement
2879 statement_expression
2880 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2881 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2882 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2883 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2884 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2885 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2886 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2888 Report.Error (1002, lexer.Location, "Expecting `;'");
2892 object_creation_expression
2893 : object_or_delegate_creation_expression
2894 { note ("complain if this is a delegate maybe?"); }
2903 : if_statement_open if_statement_rest
2912 oob_stack.Push (lexer.Location);
2917 : boolean_expression CLOSE_PARENS
2920 Location l = (Location) oob_stack.Pop ();
2922 $$ = new If ((Expression) $1, (Statement) $3, l);
2924 if (RootContext.WarningLevel >= 3){
2925 if ($3 is EmptyStatement)
2926 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
2930 | boolean_expression CLOSE_PARENS
2931 embedded_statement ELSE embedded_statement
2933 Location l = (Location) oob_stack.Pop ();
2935 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
2940 : SWITCH OPEN_PARENS
2942 oob_stack.Push (lexer.Location);
2943 switch_stack.Push (current_block);
2945 expression CLOSE_PARENS
2948 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
2949 current_block = (Block) switch_stack.Pop ();
2965 Report.Error (1522, lexer.Location, "Empty switch block");
2973 ArrayList sections = new ArrayList ();
2978 | switch_sections switch_section
2980 ArrayList sections = (ArrayList) $1;
2990 current_block = new Block (current_block, lexer.Location, lexer.Location);
2994 Block topmost = current_block;
2996 while (topmost.Implicit)
2997 topmost = topmost.Parent;
2998 $$ = new SwitchSection ((ArrayList) $1, topmost);
3005 ArrayList labels = new ArrayList ();
3010 | switch_labels switch_label
3012 ArrayList labels = (ArrayList) ($1);
3020 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3021 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3024 1523, lexer.Location,
3025 "The keyword case or default must precede code in switch block");
3039 oob_stack.Push (lexer.Location);
3041 boolean_expression CLOSE_PARENS embedded_statement
3043 Location l = (Location) oob_stack.Pop ();
3044 $$ = new While ((Expression) $4, (Statement) $6, l);
3046 if (RootContext.WarningLevel >= 3){
3047 if ($6 is EmptyStatement)
3048 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3054 : DO embedded_statement
3056 oob_stack.Push (lexer.Location);
3058 boolean_expression CLOSE_PARENS SEMICOLON
3060 Location l = (Location) oob_stack.Pop ();
3062 $$ = new Do ((Statement) $2, (Expression) $6, l);
3068 opt_for_initializer SEMICOLON
3070 Block assign_block = new Block (current_block);
3071 current_block = assign_block;
3073 if ($3 is DictionaryEntry){
3074 DictionaryEntry de = (DictionaryEntry) $3;
3076 Expression type = (Expression) de.Key;
3077 ArrayList var_declarators = (ArrayList) de.Value;
3079 foreach (VariableDeclaration decl in var_declarators){
3083 vi = current_block.AddVariable (
3084 type, decl.identifier, current_local_parameters, decl.Location);
3088 Location l = lexer.Location;
3090 if (decl.expression_or_array_initializer is Expression){
3091 expr = (Expression) decl.expression_or_array_initializer;
3092 } else if (decl.expression_or_array_initializer == null) {
3095 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3096 expr = new ArrayCreation (type, "", init, decl.Location);
3099 LocalVariableReference var;
3100 var = new LocalVariableReference (assign_block, decl.identifier, l);
3103 Assign a = new Assign (var, expr, decl.Location);
3105 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3111 oob_stack.Push (lexer.Location);
3113 opt_for_condition SEMICOLON
3114 opt_for_iterator CLOSE_PARENS
3117 Location l = (Location) oob_stack.Pop ();
3119 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3121 if (RootContext.WarningLevel >= 3){
3122 if ($10 is EmptyStatement)
3123 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3126 current_block.AddStatement (f);
3127 while (current_block.Implicit)
3128 current_block = current_block.Parent;
3130 current_block = current_block.Parent;
3135 : /* empty */ { $$ = new EmptyStatement (); }
3140 : local_variable_declaration
3141 | statement_expression_list
3145 : /* empty */ { $$ = null; }
3146 | boolean_expression
3150 : /* empty */ { $$ = new EmptyStatement (); }
3155 : statement_expression_list
3158 statement_expression_list
3159 : statement_expression
3161 // CHANGE: was `null'
3162 Block b = new Block (current_block, true);
3164 b.AddStatement ((Statement) $1);
3167 | statement_expression_list COMMA statement_expression
3169 Block b = (Block) $1;
3171 b.AddStatement ((Statement) $3);
3177 : FOREACH OPEN_PARENS type IDENTIFIER IN
3179 oob_stack.Push (lexer.Location);
3181 expression CLOSE_PARENS
3183 oob_stack.Push (current_block);
3185 Block foreach_block = new Block (current_block, true);
3186 LocalVariableReference v = null;
3187 Location l = lexer.Location;
3190 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3194 // Get a writable reference to this read-only variable.
3195 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3197 current_block = foreach_block;
3200 oob_stack.Push (current_block);
3204 Block foreach_block = (Block) oob_stack.Pop ();
3205 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3206 Block prev_block = (Block) oob_stack.Pop ();
3207 Location l = (Location) oob_stack.Pop ();
3209 current_block = prev_block;
3212 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3213 foreach_block.AddStatement (f);
3222 | continue_statement
3231 $$ = new Break (lexer.Location);
3236 : CONTINUE SEMICOLON
3238 $$ = new Continue (lexer.Location);
3243 : GOTO IDENTIFIER SEMICOLON
3245 $$ = new Goto (current_block, (string) $2, lexer.Location);
3247 | GOTO CASE constant_expression SEMICOLON
3249 $$ = new GotoCase ((Expression) $3, lexer.Location);
3251 | GOTO DEFAULT SEMICOLON
3253 $$ = new GotoDefault (lexer.Location);
3258 : RETURN opt_expression SEMICOLON
3260 $$ = new Return ((Expression) $2, lexer.Location);
3265 : THROW opt_expression SEMICOLON
3267 $$ = new Throw ((Expression) $2, lexer.Location);
3277 : TRY block catch_clauses
3280 ArrayList s = new ArrayList ();
3282 foreach (Catch cc in (ArrayList) $3) {
3289 // Now s contains the list of specific catch clauses
3290 // and g contains the general one.
3292 $$ = new Try ((Block) $2, s, g, null, lexer.Location);
3294 | TRY block opt_catch_clauses FINALLY block
3297 ArrayList s = new ArrayList ();
3298 ArrayList catch_list = (ArrayList) $3;
3300 if (catch_list != null){
3301 foreach (Catch cc in catch_list) {
3309 $$ = new Try ((Block) $2, s, g, (Block) $5, lexer.Location);
3313 Report.Error (1524, lexer.Location, "Expected catch or finally");
3318 : /* empty */ { $$ = null; }
3325 ArrayList l = new ArrayList ();
3330 | catch_clauses catch_clause
3332 ArrayList l = (ArrayList) $1;
3340 : /* empty */ { $$ = null; }
3345 : CATCH opt_catch_args
3347 Expression type = null;
3351 DictionaryEntry cc = (DictionaryEntry) $2;
3352 type = (Expression) cc.Key;
3353 id = (string) cc.Value;
3356 ArrayList one = new ArrayList ();
3357 Location loc = lexer.Location;
3359 one.Add (new VariableDeclaration (id, null, loc));
3362 current_block = new Block (current_block);
3363 Block b = declare_local_variables (type, one, loc);
3368 Expression type = null;
3372 DictionaryEntry cc = (DictionaryEntry) $2;
3373 type = (Expression) cc.Key;
3374 id = (string) cc.Value;
3378 // FIXME: I can change this for an assignment.
3380 while (current_block != (Block) $1)
3381 current_block = current_block.Parent;
3386 $$ = new Catch (type, id , (Block) $4, lexer.Location);
3391 : /* empty */ { $$ = null; }
3396 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3398 $$ = new DictionaryEntry ($2, $3);
3405 $$ = new Checked ((Block) $2);
3412 $$ = new Unchecked ((Block) $2);
3419 if (!RootContext.Unsafe){
3420 Report.Error (227, lexer.Location,
3421 "Unsafe code can only be used if --unsafe is used");
3424 $$ = new Unsafe ((Block) $3);
3430 pointer_type fixed_pointer_declarators
3433 Block assign_block = new Block (current_block, true);
3434 ArrayList list = (ArrayList) $4;
3435 Expression type = (Expression) $3;
3436 Location l = lexer.Location;
3437 int top = list.Count;
3439 for (int i = 0; i < top; i++){
3440 Pair p = (Pair) list [i];
3443 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3450 current_block.AddStatement (assign_block);
3451 current_block = assign_block;
3452 oob_stack.Push (assign_block);
3457 Location l = (Location) oob_stack.Pop ();
3458 Block assign_block = (Block) oob_stack.Pop ();
3460 ArrayList list = (ArrayList) $4;
3461 int top = list.Count;
3463 $$ = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
3467 fixed_pointer_declarators
3468 : fixed_pointer_declarator {
3469 ArrayList declarators = new ArrayList ();
3470 declarators.Add ($1);
3473 | fixed_pointer_declarators COMMA fixed_pointer_declarator
3475 ArrayList declarators = (ArrayList) $1;
3476 declarators.Add ($3);
3481 fixed_pointer_declarator
3482 : IDENTIFIER ASSIGN expression
3484 $$ = new Pair ($1, $3);
3489 : LOCK OPEN_PARENS expression CLOSE_PARENS
3495 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
3500 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
3502 Block assign_block = new Block (current_block);
3503 current_block = assign_block;
3505 oob_stack.Push (lexer.Location);
3507 if ($3 is DictionaryEntry){
3508 DictionaryEntry de = (DictionaryEntry) $3;
3509 Location l = lexer.Location;
3511 Expression type = (Expression) de.Key;
3512 ArrayList var_declarators = (ArrayList) de.Value;
3514 ArrayList vars = new ArrayList ();
3516 foreach (VariableDeclaration decl in var_declarators){
3518 VariableInfo vi = current_block.AddVariable (
3519 type, decl.identifier,
3520 current_local_parameters, decl.Location);
3526 if (decl.expression_or_array_initializer is Expression){
3527 expr = (Expression) decl.expression_or_array_initializer;
3529 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3531 expr = new ArrayCreation (type, "", init, decl.Location);
3534 LocalVariableReference var;
3536 // Get a writable reference to this read-only variable.
3537 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
3539 // This is so that it is not a warning on using variables
3542 vars.Add (new DictionaryEntry (var, expr));
3544 // Assign a = new Assign (var, expr, decl.Location);
3545 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3547 $3 = new DictionaryEntry (type, vars);
3552 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
3553 current_block.AddStatement (u);
3554 while (current_block.Implicit)
3555 current_block = current_block.Parent;
3557 current_block = current_block.Parent;
3561 resource_acquisition
3562 : local_variable_declaration
3569 // A class used to pass around variable declarations and constants
3571 public class VariableDeclaration {
3572 public string identifier;
3573 public object expression_or_array_initializer;
3574 public Location Location;
3575 public Attributes OptAttributes;
3577 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
3579 this.identifier = id;
3580 this.expression_or_array_initializer = eoai;
3582 this.OptAttributes = opt_attrs;
3585 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
3591 // A class used to hold info about an indexer declarator
3594 public class IndexerDeclaration {
3595 public Expression type;
3596 public string interface_type;
3597 public Parameters param_list;
3599 public IndexerDeclaration (Expression type, string interface_type, Parameters param_list)
3602 this.interface_type = interface_type;
3603 this.param_list = param_list;
3608 // A class used to hold info about an operator declarator
3611 public class OperatorDeclaration {
3612 public Operator.OpType optype;
3613 public Expression ret_type, arg1type, arg2type;
3614 public string arg1name, arg2name;
3615 public Location location;
3617 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
3618 Expression arg1type, string arg1name,
3619 Expression arg2type, string arg2name, Location location)
3622 this.ret_type = ret_type;
3623 this.arg1type = arg1type;
3624 this.arg1name = arg1name;
3625 this.arg2type = arg2type;
3626 this.arg2name = arg2name;
3627 this.location = location;
3632 void Error_ExpectingTypeName (Location l, Expression expr)
3634 if (expr is Invocation){
3635 Report.Error (1002, l, "; expected");
3637 Report.Error (-1, l, "Invalid Type definition");
3642 // Given the @class_name name, it creates a fully qualified name
3643 // based on the containing declaration space
3646 MakeName (string class_name)
3648 string ns = current_namespace.Name;
3649 string container_name = current_container.Name;
3651 if (container_name == ""){
3653 return ns + "." + class_name;
3657 return container_name + "." + class_name;
3661 // Used to report back to the user the result of a declaration
3662 // in the current declaration space
3665 CheckDef (AdditionResult result, string name, Location l)
3667 if (result == AdditionResult.Success)
3671 case AdditionResult.NameExists:
3672 Report.Error (102, l, "The container `" + current_container.Name +
3673 "' already contains a definition for `"+
3679 // This is handled only for static Constructors, because
3680 // in reality we handle these by the semantic analysis later
3682 case AdditionResult.MethodExists:
3684 111, l, "Class `"+current_container.Name+
3685 "' already defines a member called '" +
3686 name + "' with the same parameter types (more than one default constructor)");
3689 case AdditionResult.EnclosingClash:
3690 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
3693 case AdditionResult.NotAConstructor:
3694 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
3700 CheckDef (bool result, string name, Location l)
3704 CheckDef (AdditionResult.NameExists, name, l);
3707 Expression DecomposeQI (string name, Location loc)
3711 if (name.IndexOf ('.') == -1){
3712 return new SimpleName (name, loc);
3714 int pos = name.LastIndexOf (".");
3715 string left = name.Substring (0, pos);
3716 string right = name.Substring (pos + 1);
3718 o = DecomposeQI (left, loc);
3720 return new MemberAccess (o, right, loc);
3725 // This method is used to get at the complete string representation of
3726 // a fully-qualified type name, hiding inside a MemberAccess ;-)
3727 // This is necessary because local_variable_type admits primary_expression
3728 // as the type of the variable. So we do some extra checking
3730 string GetQualifiedIdentifier (Expression expr)
3732 if (expr is SimpleName)
3733 return ((SimpleName)expr).Name;
3734 else if (expr is MemberAccess)
3735 return GetQualifiedIdentifier (((MemberAccess)expr).Expr) + "." + ((MemberAccess) expr).Identifier;
3737 throw new Exception ("Expr has to be either SimpleName or MemberAccess! (" + expr + ")");
3741 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
3743 Block implicit_block;
3744 ArrayList inits = null;
3747 // We use the `Used' property to check whether statements
3748 // have been added to the current block. If so, we need
3749 // to create another block to contain the new declaration
3750 // otherwise, as an optimization, we use the same block to
3751 // add the declaration.
3753 // FIXME: A further optimization is to check if the statements
3754 // that were added were added as part of the initialization
3755 // below. In which case, no other statements have been executed
3756 // and we might be able to reduce the number of blocks for
3757 // situations like this:
3759 // int j = 1; int k = j + 1;
3761 if (current_block.Used) {
3762 implicit_block = new Block (current_block, true, loc, Location.Null);
3763 implicit_block.AddChildVariableNames (current_block);
3765 implicit_block = current_block;
3767 foreach (VariableDeclaration decl in variable_declarators){
3769 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
3770 if (decl.expression_or_array_initializer != null){
3772 inits = new ArrayList ();
3779 return implicit_block;
3781 foreach (VariableDeclaration decl in inits){
3785 if (decl.expression_or_array_initializer is Expression){
3786 expr = (Expression) decl.expression_or_array_initializer;
3789 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3791 expr = new ArrayCreation (type, "", init, decl.Location);
3794 LocalVariableReference var;
3795 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
3797 assign = new Assign (var, expr, decl.Location);
3799 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
3802 return implicit_block;
3805 Block declare_local_constant (Expression type, VariableDeclaration decl)
3807 Block implicit_block;
3809 if (current_block.Used)
3810 implicit_block = new Block (current_block, true);
3812 implicit_block = current_block;
3814 if (!(implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
3815 current_local_parameters, decl.Location))){
3818 return implicit_block;
3821 void CheckAttributeTarget (string a)
3825 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
3829 Location l = lexer.Location;
3830 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
3836 void CheckUnaryOperator (Operator.OpType op)
3840 case Operator.OpType.LogicalNot:
3841 case Operator.OpType.OnesComplement:
3842 case Operator.OpType.Increment:
3843 case Operator.OpType.Decrement:
3844 case Operator.OpType.True:
3845 case Operator.OpType.False:
3846 case Operator.OpType.Addition:
3847 case Operator.OpType.Subtraction:
3852 Location l = lexer.Location;
3853 Report.Error (1019, l, "Overloadable unary operator expected");
3859 void CheckBinaryOperator (Operator.OpType op)
3863 case Operator.OpType.Addition:
3864 case Operator.OpType.Subtraction:
3865 case Operator.OpType.Multiply:
3866 case Operator.OpType.Division:
3867 case Operator.OpType.Modulus:
3868 case Operator.OpType.BitwiseAnd:
3869 case Operator.OpType.BitwiseOr:
3870 case Operator.OpType.ExclusiveOr:
3871 case Operator.OpType.LeftShift:
3872 case Operator.OpType.RightShift:
3873 case Operator.OpType.Equality:
3874 case Operator.OpType.Inequality:
3875 case Operator.OpType.GreaterThan:
3876 case Operator.OpType.LessThan:
3877 case Operator.OpType.GreaterThanOrEqual:
3878 case Operator.OpType.LessThanOrEqual:
3882 Location l = lexer.Location;
3883 Report.Error (1020, l, "Overloadable binary operator expected");
3889 void syntax_error (Location l, string msg)
3891 Report.Error (1003, l, "Syntax error, " + msg);
3894 void output (string s)
3896 Console.WriteLine (s);
3899 void note (string s)
3901 // Used to put annotations
3906 public Tokenizer Lexer {
3912 public CSharpParser (StreamReader reader, string name, ArrayList defines)
3914 current_namespace = new Namespace (null, "");
3916 current_container = RootContext.Tree.Types;
3917 current_container.Namespace = current_namespace;
3918 oob_stack = new Stack ();
3919 switch_stack = new Stack ();
3921 lexer = new Tokenizer (reader, name, defines);
3924 public override void parse ()
3927 if (yacc_verbose_flag)
3928 yyparse (lexer, new yydebug.yyDebugSimple ());
3931 } catch (Exception e){
3932 // Please do not remove this, it is used during debugging
3935 Console.WriteLine (lexer.location + " : Parsing error ");
3936 Console.WriteLine (e);