3 // cs-parser.jay: The Parser for the C# compiler
5 // Authors: Miguel de Icaza (miguel@gnu.org)
6 // Ravi Pratap (ravi@ximian.com)
8 // Licensed under the terms of the GNU GPL
10 // (C) 2001 Ximian, Inc (http://www.ximian.com)
13 // (1) Figure out why error productions dont work. `type-declaration' is a
14 // great spot to put an `error' because you can reproduce it with this input:
17 // Possible optimization:
18 // Run memory profiler with parsing only, and consider dropping
19 // arraylists where not needed. Some pieces can use linked lists.
27 using System.Collections;
33 public class CSharpParser : GenericParser {
34 Namespace current_namespace;
35 TypeContainer current_container;
38 // Current block is used to add statements as we find
45 // Current interface is used by the various declaration
46 // productions in the interface declaration to "add"
47 // the interfaces as we find them.
49 Interface current_interface;
52 // This is used by the unary_expression code to resolve
53 // a name against a parameter.
55 Parameters current_local_parameters;
58 // Using during property parsing to describe the implicit
59 // value parameter that is passed to the "set" and "get"accesor
60 // methods (properties and indexers).
62 Expression implicit_value_parameter_type;
63 Parameters indexer_parameters;
66 // Used to determine if we are parsing the get/set pair
67 // of an indexer or a property
72 // An out-of-band stack.
88 %token NONE /* This token is never returned by our lexer */
89 %token ERROR // This is used not by the parser, but by the tokenizer.
93 *These are the C# keywords
176 /* C# keywords which are not really keywords */
180 /* C# single character operators/punctuation. */
181 %token OPEN_BRACE "{"
182 %token CLOSE_BRACE "}"
183 %token OPEN_BRACKET "["
184 %token CLOSE_BRACKET "]"
185 %token OPEN_PARENS "("
186 %token CLOSE_PARENS ")"
199 %token BITWISE_AND "&"
200 %token BITWISE_OR "|"
207 /* C# multi-character operators. */
210 %token OP_SHIFT_LEFT "<<"
211 %token OP_SHIFT_RIGHT ">>"
218 %token OP_MULT_ASSIGN "*="
219 %token OP_DIV_ASSIGN "/="
220 %token OP_MOD_ASSIGN "%="
221 %token OP_ADD_ASSIGN "+="
222 %token OP_SUB_ASSIGN "-="
223 %token OP_SHIFT_LEFT_ASSIGN "<<="
224 %token OP_SHIFT_RIGHT_ASSIGN ">>="
225 %token OP_AND_ASSIGN "&="
226 %token OP_XOR_ASSIGN "^="
227 %token OP_OR_ASSIGN "|="
231 %token LITERAL_INTEGER "int literal"
232 %token LITERAL_FLOAT "float literal"
233 %token LITERAL_DOUBLE "double literal"
234 %token LITERAL_DECIMAL "decimal literal"
235 %token LITERAL_CHARACTER "character literal"
236 %token LITERAL_STRING "string literal"
240 /* Add precedence rules to solve dangling else s/r conflict */
249 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
251 %left STAR DIV PERCENT
252 %right BANG CARRET UMINUS
253 %nonassoc OP_INC OP_DEC
255 %left OPEN_BRACKET OPEN_BRACE
259 %start compilation_unit
263 : outer_declarations opt_EOF
264 | outer_declarations attribute_sections opt_EOF
265 | attribute_sections opt_EOF
266 | opt_EOF /* allow empty files */
276 | outer_declarations outer_declaration
281 | namespace_member_declaration
286 | using_directives using_directive
290 : using_alias_directive
291 | using_namespace_directive
294 using_alias_directive
295 : USING IDENTIFIER ASSIGN
296 namespace_or_type_name SEMICOLON
298 current_namespace.UsingAlias ((string) $2, (string) $4, lexer.Location);
302 using_namespace_directive
303 : USING namespace_name SEMICOLON
305 current_namespace.Using ((string) $2, lexer.Location);
310 // Strictly speaking, namespaces don't have attributes but
311 // we parse global attributes along with namespace declarations and then
314 namespace_declaration
315 : opt_attributes NAMESPACE qualified_identifier
317 Attributes attrs = (Attributes) $1;
320 foreach (AttributeSection asec in attrs.AttributeSections)
321 if (asec.Target == "assembly")
322 RootContext.AddGlobalAttributeSection (current_container, asec);
324 Report.Error(1518, Lexer.Location,
325 "Attributes cannot be applied to namespaces."
326 + " Expected class, delegate, enum, interface, or struct");
329 current_namespace = RootContext.Tree.RecordNamespace (current_namespace, file, (string) $3);
331 namespace_body opt_semicolon
333 current_namespace = current_namespace.Parent;
349 | qualified_identifier DOT IDENTIFIER {
350 $$ = (($1).ToString ()) + "." + ($3.ToString ()); }
355 : namespace_or_type_name
361 opt_namespace_member_declarations
372 opt_namespace_member_declarations
374 | namespace_member_declarations
377 namespace_member_declarations
378 : namespace_member_declaration
379 | namespace_member_declarations namespace_member_declaration
382 namespace_member_declaration
389 Class c = (Class) $1;
390 mod_flags = c.ModFlags;
392 } else if ($1 is Struct){
393 Struct s = (Struct) $1;
394 mod_flags = s.ModFlags;
399 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
401 1527, lexer.Location,
402 "Namespace elements cant be explicitly " +
403 "declared private or protected in `" + name + "'");
405 current_namespace.DeclarationFound = true;
407 | namespace_declaration
413 | interface_declaration
415 | delegate_declaration
417 // Enable this when we have handled all errors, because this acts as a generic fallback
420 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
430 | attribute_sections { $$ = $1; }
436 AttributeSection sect = (AttributeSection) $1;
438 if (sect.Target == "assembly")
439 RootContext.AddGlobalAttributeSection (current_container, sect);
442 $$ = new Attributes ((AttributeSection) $1);
444 | attribute_sections attribute_section
446 Attributes attrs = null;
447 AttributeSection sect = (AttributeSection) $2;
449 if (sect.Target == "assembly")
450 RootContext.AddGlobalAttributeSection (current_container, sect);
453 attrs = (Attributes) $1;
454 attrs.AddAttributeSection (sect);
462 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
464 string target = null;
467 target = (string) $2;
469 $$ = new AttributeSection (target, (ArrayList) $3);
471 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
473 $$ = new AttributeSection (null, (ArrayList) $2);
477 attribute_target_specifier
478 : attribute_target COLON
487 CheckAttributeTarget ((string) $1);
490 | EVENT { $$ = "event"; }
491 | RETURN { $$ = "return"; }
497 ArrayList attrs = new ArrayList ();
503 | attribute_list COMMA attribute
505 ArrayList attrs = (ArrayList) $1;
517 opt_attribute_arguments
519 $$ = new Attribute ((string) $1, (ArrayList) $3, (Location) $2);
524 : type_name { /* reserved attribute name or identifier: 17.4 */ }
527 opt_attribute_arguments
528 : /* empty */ { $$ = null; }
529 | OPEN_PARENS attribute_arguments CLOSE_PARENS
537 : opt_positional_argument_list
542 ArrayList args = new ArrayList ();
548 | positional_argument_list COMMA named_argument_list
550 ArrayList args = new ArrayList ();
556 | named_argument_list
558 ArrayList args = new ArrayList ();
567 opt_positional_argument_list
568 : /* empty */ { $$ = null; }
569 | positional_argument_list
572 positional_argument_list
575 ArrayList args = new ArrayList ();
576 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
580 | positional_argument_list COMMA expression
582 ArrayList args = (ArrayList) $1;
583 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
592 ArrayList args = new ArrayList ();
597 | named_argument_list COMMA named_argument
599 ArrayList args = (ArrayList) $1;
607 : IDENTIFIER ASSIGN expression
609 $$ = new DictionaryEntry (
611 new Argument ((Expression) $3, Argument.AType.Expression));
617 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
620 opt_class_member_declarations
622 | class_member_declarations
625 class_member_declarations
626 : class_member_declaration
627 | class_member_declarations
628 class_member_declaration
631 class_member_declaration
632 : constant_declaration // done
633 | field_declaration // done
634 | method_declaration // done
635 | property_declaration // done
636 | event_declaration // done
637 | indexer_declaration // done
638 | operator_declaration // done
639 | constructor_declaration // done
640 | destructor_declaration // done
650 string full_struct_name = MakeName ((string) $4);
652 new_struct = new Struct (current_container, full_struct_name, (int) $2,
653 (Attributes) $1, lexer.Location);
654 current_container = new_struct;
655 current_container.Namespace = current_namespace;
656 RootContext.Tree.RecordDecl (full_struct_name, new_struct);
662 Struct new_struct = (Struct) current_container;
665 new_struct.Bases = (ArrayList) $6;
667 current_container = current_container.Parent;
668 CheckDef (current_container.AddStruct (new_struct), new_struct.Name, new_struct.Location);
674 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
677 opt_struct_member_declarations
679 | struct_member_declarations
682 struct_member_declarations
683 : struct_member_declaration
684 | struct_member_declarations struct_member_declaration
687 struct_member_declaration
688 : constant_declaration
691 | property_declaration
693 | indexer_declaration
694 | operator_declaration
695 | constructor_declaration
699 * This is only included so we can flag error 575:
700 * destructors only allowed on class types
702 | destructor_declaration
713 foreach (VariableDeclaration constant in (ArrayList) $5){
714 Location l = constant.Location;
716 Const c = new Const (
717 (Expression) $4, (string) constant.identifier,
718 (Expression) constant.expression_or_array_initializer, (int) $2,
721 CheckDef (current_container.AddConstant (c), c.Name, l);
727 : constant_declarator
729 ArrayList constants = new ArrayList ();
733 | constant_declarators COMMA constant_declarator
735 ArrayList constants = (ArrayList) $1;
742 : IDENTIFIER ASSIGN constant_expression
744 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
755 Expression type = (Expression) $3;
758 foreach (VariableDeclaration var in (ArrayList) $4){
759 Location l = var.Location;
761 Field field = new Field (type, mod, var.identifier,
762 var.expression_or_array_initializer,
765 CheckDef (current_container.AddField (field), field.Name, l);
771 : variable_declarator
773 ArrayList decl = new ArrayList ();
777 | variable_declarators COMMA variable_declarator
779 ArrayList decls = (ArrayList) $1;
786 : IDENTIFIER ASSIGN variable_initializer
788 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
792 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
805 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
807 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
815 Method method = (Method) $1;
816 Block b = (Block) $2;
817 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
820 if ((method.ModFlags & extern_abstract) == 0){
822 501, lexer.Location, current_container.MakeName (method.Name) +
823 "must declare a body because it is not marked abstract or extern");
826 if ((method.ModFlags & Modifiers.EXTERN) != 0){
828 179, lexer.Location, current_container.MakeName (method.Name) +
829 " is declared extern, but has a body");
833 method.Block = (Block) $2;
834 CheckDef (current_container.AddMethod (method), method.Name, method.Location);
836 current_local_parameters = null;
850 1585, lexer.Location, "Member modifier `" +
851 Modifiers.Name (i) + "' must precede member type and name");
864 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
866 Method method = new Method ((Expression) $3, (int) $2, (string) $4,
867 (Parameters) $6, (Attributes) $1, lexer.Location);
869 current_local_parameters = (Parameters) $6;
877 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
879 Method method = new Method (TypeManager.system_void_expr, (int) $2, (string) $4,
880 (Parameters) $6, (Attributes) $1, lexer.Location);
882 current_local_parameters = (Parameters) $6;
889 | SEMICOLON { $$ = null; }
892 opt_formal_parameter_list
893 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
894 | formal_parameter_list
897 formal_parameter_list
900 ArrayList pars_list = (ArrayList) $1;
902 Parameter [] pars = new Parameter [pars_list.Count];
903 pars_list.CopyTo (pars);
905 $$ = new Parameters (pars, null, lexer.Location);
907 | fixed_parameters COMMA parameter_array
909 ArrayList pars_list = (ArrayList) $1;
911 Parameter [] pars = new Parameter [pars_list.Count];
912 pars_list.CopyTo (pars);
914 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
918 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
925 ArrayList pars = new ArrayList ();
930 | fixed_parameters COMMA fixed_parameter
932 ArrayList pars = (ArrayList) $1;
941 opt_parameter_modifier
945 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
949 opt_parameter_modifier
950 : /* empty */ { $$ = Parameter.Modifier.NONE; }
955 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
956 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
960 : opt_attributes PARAMS type IDENTIFIER
962 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
963 note ("type must be a single-dimension array type");
968 : qualified_identifier
977 implicit_value_parameter_type = (Expression) $3;
979 lexer.PropertyParsing = true;
983 accessor_declarations
985 lexer.PropertyParsing = false;
990 Pair pair = (Pair) $7;
991 Accessor get_block = (Accessor) pair.First;
992 Accessor set_block = (Accessor) pair.Second;
994 Location loc = (Location) $6;
995 prop = new Property ((Expression) $3, (string) $4, (int) $2, get_block, set_block,
996 (Attributes) $1, loc);
998 CheckDef (current_container.AddProperty (prop), prop.Name, loc);
999 implicit_value_parameter_type = null;
1003 accessor_declarations
1004 : get_accessor_declaration opt_set_accessor_declaration
1006 $$ = new Pair ($1, $2);
1008 | set_accessor_declaration opt_get_accessor_declaration
1010 $$ = new Pair ($2, $1);
1014 opt_get_accessor_declaration
1015 : /* empty */ { $$ = null; }
1016 | get_accessor_declaration
1019 opt_set_accessor_declaration
1020 : /* empty */ { $$ = null; }
1021 | set_accessor_declaration
1024 get_accessor_declaration
1025 : opt_attributes GET
1027 // If this is not the case, then current_local_parameters has already
1028 // been set in indexer_declaration
1029 if (parsing_indexer == false)
1030 current_local_parameters = null;
1032 current_local_parameters = indexer_parameters;
1033 lexer.PropertyParsing = false;
1037 $$ = new Accessor ((Block) $4, (Attributes) $1);
1038 current_local_parameters = null;
1039 lexer.PropertyParsing = true;
1043 set_accessor_declaration
1044 : opt_attributes SET
1047 Parameter implicit_value_parameter = new Parameter (
1048 implicit_value_parameter_type, "value",
1049 Parameter.Modifier.NONE, null);
1051 if (parsing_indexer == false) {
1052 args = new Parameter [1];
1053 args [0] = implicit_value_parameter;
1054 current_local_parameters = new Parameters (args, null, lexer.Location);
1056 Parameter [] fpars = indexer_parameters.FixedParameters;
1059 int count = fpars.Length;
1061 args = new Parameter [count + 1];
1062 fpars.CopyTo (args, 0);
1063 args [count] = implicit_value_parameter;
1066 current_local_parameters = new Parameters (
1067 args, indexer_parameters.ArrayParameter, lexer.Location);
1070 lexer.PropertyParsing = false;
1074 $$ = new Accessor ((Block) $4, (Attributes) $1);
1075 current_local_parameters = null;
1076 lexer.PropertyParsing = true;
1082 | SEMICOLON { $$ = null; }
1085 interface_declaration
1088 INTERFACE IDENTIFIER
1090 Interface new_interface;
1091 string full_interface_name = MakeName ((string) $4);
1093 new_interface = new Interface (current_container, full_interface_name, (int) $2,
1094 (Attributes) $1, lexer.Location);
1095 if (current_interface != null) {
1096 Location l = lexer.Location;
1097 Report.Error (-2, l, "Internal compiler error: interface inside interface");
1099 current_interface = new_interface;
1100 new_interface.Namespace = current_namespace;
1101 RootContext.Tree.RecordDecl (full_interface_name, new_interface);
1104 interface_body opt_semicolon
1106 Interface new_interface = (Interface) current_interface;
1109 new_interface.Bases = (ArrayList) $6;
1111 current_interface = null;
1112 CheckDef (current_container.AddInterface (new_interface),
1113 new_interface.Name, new_interface.Location);
1118 : /* empty */ { $$ = null; }
1123 : COLON interface_type_list { $$ = $2; }
1129 ArrayList interfaces = new ArrayList ();
1131 interfaces.Add ($1);
1134 | interface_type_list COMMA interface_type
1136 ArrayList interfaces = (ArrayList) $1;
1137 interfaces.Add ($3);
1144 opt_interface_member_declarations
1148 opt_interface_member_declarations
1150 | interface_member_declarations
1153 interface_member_declarations
1154 : interface_member_declaration
1155 | interface_member_declarations interface_member_declaration
1158 interface_member_declaration
1159 : interface_method_declaration
1161 InterfaceMethod m = (InterfaceMethod) $1;
1163 CheckDef (current_interface.AddMethod (m), m.Name, m.Location);
1165 | interface_property_declaration
1167 InterfaceProperty p = (InterfaceProperty) $1;
1169 CheckDef (current_interface.AddProperty (p), p.Name, p.Location);
1171 | interface_event_declaration
1173 InterfaceEvent e = (InterfaceEvent) $1;
1175 CheckDef (current_interface.AddEvent (e), e.Name, lexer.Location);
1177 | interface_indexer_declaration
1179 InterfaceIndexer i = (InterfaceIndexer) $1;
1181 CheckDef (current_interface.AddIndexer (i), "indexer", i.Location);
1186 : /* empty */ { $$ = false; }
1187 | NEW { $$ = true; }
1190 interface_method_declaration
1191 : opt_attributes opt_new type IDENTIFIER
1192 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1195 $$ = new InterfaceMethod ((Expression) $3, (string) $4, (bool) $2,
1196 (Parameters) $6, (Attributes) $1, lexer.Location);
1198 | opt_attributes opt_new VOID IDENTIFIER
1199 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1202 $$ = new InterfaceMethod (
1203 TypeManager.system_void_expr, (string) $4, (bool) $2, (Parameters) $6,
1204 (Attributes) $1, lexer.Location);
1208 interface_property_declaration
1213 { lexer.PropertyParsing = true; }
1215 { lexer.PropertyParsing = false; }
1220 $$ = new InterfaceProperty ((Expression) $3, (string) $4, (bool) $2,
1221 (gs & 1) == 1, (gs & 2) == 2, (Attributes) $1,
1227 : opt_attributes GET SEMICOLON { $$ = 1; }
1228 | opt_attributes SET SEMICOLON { $$ = 2; }
1229 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1231 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1235 interface_event_declaration
1236 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1238 $$ = new InterfaceEvent ((Expression) $4, (string) $5, (bool) $2, (Attributes) $1,
1243 interface_indexer_declaration
1244 : opt_attributes opt_new type THIS
1245 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1247 { lexer.PropertyParsing = true; }
1249 { lexer.PropertyParsing = false; }
1252 int a_flags = (int) $10;
1254 bool do_get = (a_flags & 1) == 1;
1255 bool do_set = (a_flags & 2) == 2;
1257 $$ = new InterfaceIndexer ((Expression) $3, (Parameters) $6, do_get, do_set,
1258 (bool) $2, (Attributes) $1, lexer.Location);
1262 operator_declaration
1263 : opt_attributes opt_modifiers operator_declarator operator_body
1265 OperatorDeclaration decl = (OperatorDeclaration) $3;
1267 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
1268 decl.arg2type, decl.arg2name, (Block) $4, (Attributes) $1, decl.location);
1270 // Note again, checking is done in semantic analysis
1271 current_container.AddOperator (op);
1273 current_local_parameters = null;
1279 | SEMICOLON { $$ = null; }
1282 : type OPERATOR overloadable_operator
1283 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1285 Operator.OpType op = (Operator.OpType) $3;
1286 CheckUnaryOperator (op);
1288 if (op == Operator.OpType.Addition)
1289 op = Operator.OpType.UnaryPlus;
1291 if (op == Operator.OpType.Subtraction)
1292 op = Operator.OpType.UnaryNegation;
1294 Parameter [] pars = new Parameter [1];
1296 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1298 current_local_parameters = new Parameters (pars, null, lexer.Location);
1300 $$ = new OperatorDeclaration (op, (Expression) $1, (Expression) $5, (string) $6,
1301 null, null, lexer.Location);
1303 | type OPERATOR overloadable_operator
1305 type IDENTIFIER COMMA
1309 CheckBinaryOperator ((Operator.OpType) $3);
1311 Parameter [] pars = new Parameter [2];
1313 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1314 pars [1] = new Parameter ((Expression) $8, (string) $9, Parameter.Modifier.NONE, null);
1316 current_local_parameters = new Parameters (pars, null, lexer.Location);
1318 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1319 (Expression) $5, (string) $6,
1320 (Expression) $8, (string) $9, lexer.Location);
1322 | conversion_operator_declarator
1325 overloadable_operator
1327 : BANG { $$ = Operator.OpType.LogicalNot; }
1328 | TILDE { $$ = Operator.OpType.OnesComplement; }
1329 | OP_INC { $$ = Operator.OpType.Increment; }
1330 | OP_DEC { $$ = Operator.OpType.Decrement; }
1331 | TRUE { $$ = Operator.OpType.True; }
1332 | FALSE { $$ = Operator.OpType.False; }
1333 // Unary and binary:
1334 | PLUS { $$ = Operator.OpType.Addition; }
1335 | MINUS { $$ = Operator.OpType.Subtraction; }
1337 | STAR { $$ = Operator.OpType.Multiply; }
1338 | DIV { $$ = Operator.OpType.Division; }
1339 | PERCENT { $$ = Operator.OpType.Modulus; }
1340 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1341 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1342 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1343 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1344 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1345 | OP_EQ { $$ = Operator.OpType.Equality; }
1346 | OP_NE { $$ = Operator.OpType.Inequality; }
1347 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1348 | OP_LT { $$ = Operator.OpType.LessThan; }
1349 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1350 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1353 conversion_operator_declarator
1354 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1356 Parameter [] pars = new Parameter [1];
1358 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1360 current_local_parameters = new Parameters (pars, null, lexer.Location);
1362 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1363 null, null, lexer.Location);
1365 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1367 Parameter [] pars = new Parameter [1];
1369 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1371 current_local_parameters = new Parameters (pars, null, lexer.Location);
1373 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1374 null, null, lexer.Location);
1378 syntax_error (lexer.Location, "'operator' expected");
1382 syntax_error (lexer.Location, "'operator' expected");
1386 constructor_declaration
1389 constructor_declarator
1392 Constructor c = (Constructor) $3;
1393 c.Block = (Block) $4;
1394 c.OptAttributes = (Attributes) $1;
1395 c.ModFlags = (int) $2;
1397 if (c.Name == current_container.Basename){
1398 if ((c.ModFlags & Modifiers.STATIC) != 0){
1399 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1401 515, c.Location, String.Format (
1402 "`{0}.{1}': static constructor can not have access modifiers",
1403 c.Name, current_container.Name));
1406 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1408 if (c.Initializer != null){
1411 "Static constructors can not have an explicit this or base " +
1412 "constructor invocations");
1415 if (!c.Parameters.Empty){
1417 132, c.Location, "Static constructors should not have parameters");
1420 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1423 // We let another layer check the validity of the constructor.
1424 Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1427 CheckDef (current_container.AddConstructor (c), c.Name, c.Location);
1429 current_local_parameters = null;
1433 constructor_declarator
1435 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1437 oob_stack.Push (lexer.Location);
1439 current_local_parameters = (Parameters) $3;
1441 opt_constructor_initializer
1443 Location l = (Location) oob_stack.Pop ();
1444 $$ = new Constructor ((string) $1, (Parameters) $3, (ConstructorInitializer) $6, l);
1450 | SEMICOLON { $$ = null; }
1453 opt_constructor_initializer
1454 : /* empty */ { $$ = null; }
1455 | constructor_initializer
1458 constructor_initializer
1459 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1461 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1463 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1465 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1469 destructor_declaration
1470 : opt_attributes TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1472 if ((string) $3 != current_container.Basename){
1473 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1474 } else if (!(current_container is Class)){
1475 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1477 Location l = lexer.Location;
1480 if (!RootContext.StdLib && current_container.Name == "System.Object")
1481 m = Modifiers.PROTECTED | Modifiers.VIRTUAL;
1483 m = Modifiers.PROTECTED | Modifiers.OVERRIDE;
1485 Method d = new Method (
1486 TypeManager.system_void_expr, m, "Finalize",
1487 new Parameters (null, null, l), (Attributes) $1, l);
1489 d.Block = (Block) $6;
1490 CheckDef (current_container.AddMethod (d), d.Name, d.Location);
1498 EVENT type variable_declarators SEMICOLON
1500 foreach (VariableDeclaration var in (ArrayList) $5) {
1502 Event e = new Event ((Expression) $4, var.identifier,
1503 var.expression_or_array_initializer,
1504 (int) $2, null, null, (Attributes) $1, lexer.Location);
1506 CheckDef (current_container.AddEvent (e), e.Name, e.Location);
1512 EVENT type member_name
1515 implicit_value_parameter_type = (Expression) $4;
1516 lexer.EventParsing = true;
1517 oob_stack.Push (lexer.Location);
1519 event_accessor_declarations
1521 lexer.EventParsing = false;
1525 Location loc = (Location) oob_stack.Pop ();
1527 Pair pair = (Pair) $8;
1528 Accessor add_accessor = null;
1529 Accessor rem_accessor = null;
1531 if (pair.First != null)
1532 add_accessor = (Accessor) pair.First;
1533 if (pair.Second != null)
1534 rem_accessor = (Accessor) pair.Second;
1536 Event e = new Event ((Expression) $4, (string) $5, null, (int) $2, add_accessor, rem_accessor,
1537 (Attributes) $1, loc);
1539 CheckDef (current_container.AddEvent (e), e.Name, loc);
1540 implicit_value_parameter_type = null;
1544 event_accessor_declarations
1545 : add_accessor_declaration remove_accessor_declaration
1547 $$ = new Pair ($1, $2);
1549 | remove_accessor_declaration add_accessor_declaration
1551 $$ = new Pair ($2, $1);
1555 add_accessor_declaration
1556 : opt_attributes ADD
1558 Parameter [] args = new Parameter [1];
1559 Parameter implicit_value_parameter = new Parameter (
1560 implicit_value_parameter_type, "value",
1561 Parameter.Modifier.NONE, null);
1563 args [0] = implicit_value_parameter;
1565 current_local_parameters = new Parameters (args, null, lexer.Location);
1566 lexer.EventParsing = false;
1570 $$ = new Accessor ((Block) $4, (Attributes) $1);
1571 lexer.EventParsing = true;
1573 | opt_attributes ADD error {
1574 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1579 remove_accessor_declaration
1580 : opt_attributes REMOVE
1582 Parameter [] args = new Parameter [1];
1583 Parameter implicit_value_parameter = new Parameter (
1584 implicit_value_parameter_type, "value",
1585 Parameter.Modifier.NONE, null);
1587 args [0] = implicit_value_parameter;
1589 current_local_parameters = new Parameters (args, null, lexer.Location);
1590 lexer.EventParsing = false;
1594 $$ = new Accessor ((Block) $4, (Attributes) $1);
1595 lexer.EventParsing = true;
1597 | opt_attributes REMOVE error {
1598 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1604 : opt_attributes opt_modifiers indexer_declarator
1607 IndexerDeclaration decl = (IndexerDeclaration) $3;
1609 implicit_value_parameter_type = decl.type;
1611 lexer.PropertyParsing = true;
1612 parsing_indexer = true;
1614 indexer_parameters = decl.param_list;
1615 oob_stack.Push (lexer.Location);
1617 accessor_declarations
1619 lexer.PropertyParsing = false;
1620 parsing_indexer = false;
1624 // The signature is computed from the signature of the indexer. Look
1625 // at section 3.6 on the spec
1626 Location loc = (Location) oob_stack.Pop ();
1628 IndexerDeclaration decl = (IndexerDeclaration) $3;
1629 Pair pair = (Pair) $6;
1630 Accessor get_block = (Accessor) pair.First;
1631 Accessor set_block = (Accessor) pair.Second;
1633 indexer = new Indexer (decl.type, decl.interface_type, (int) $2, decl.param_list,
1634 get_block, set_block, (Attributes) $1, loc);
1636 // Note that there is no equivalent of CheckDef for this case
1637 // We shall handle this in semantic analysis
1639 current_container.AddIndexer (indexer);
1641 current_local_parameters = null;
1642 implicit_value_parameter_type = null;
1643 indexer_parameters = null;
1648 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1650 Parameters pars = (Parameters) $4;
1652 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1653 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1656 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
1658 | type qualified_identifier DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1660 Parameters pars = (Parameters) $6;
1662 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1663 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1665 $$ = new IndexerDeclaration ((Expression) $1, (string) $2, pars);
1677 Location enum_location = lexer.Location;
1679 string full_name = MakeName ((string) $4);
1680 Enum e = new Enum (current_container, (Expression) $5, (int) $2, full_name,
1681 (Attributes) $1, enum_location);
1683 foreach (VariableDeclaration ev in (ArrayList) $6) {
1684 Location loc = (Location) ev.Location;
1686 CheckDef (e.AddEnumMember (ev.identifier,
1687 (Expression) ev.expression_or_array_initializer,
1688 loc, ev.OptAttributes),
1689 ev.identifier, loc);
1692 e.Namespace = current_namespace;
1694 CheckDef (current_container.AddEnum (e), full_name, enum_location);
1695 RootContext.Tree.RecordDecl (full_name, e);
1701 : /* empty */ { $$ = TypeManager.system_int32_expr; }
1702 | COLON type { $$ = $2; }
1706 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1712 opt_enum_member_declarations
1713 : /* empty */ { $$ = new ArrayList (); }
1714 | enum_member_declarations opt_comma { $$ = $1; }
1717 enum_member_declarations
1718 : enum_member_declaration
1720 ArrayList l = new ArrayList ();
1725 | enum_member_declarations COMMA enum_member_declaration
1727 ArrayList l = (ArrayList) $1;
1735 enum_member_declaration
1736 : opt_attributes IDENTIFIER
1738 $$ = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
1740 | opt_attributes IDENTIFIER
1742 $$ = lexer.Location;
1746 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
1750 delegate_declaration
1754 IDENTIFIER OPEN_PARENS
1755 opt_formal_parameter_list
1759 Location l = lexer.Location;
1760 Delegate del = new Delegate (current_container, (Expression) $4, (int) $2,
1761 MakeName ((string) $5), (Parameters) $7,
1762 (Attributes) $1, l);
1764 del.Namespace = current_namespace;
1765 CheckDef (current_container.AddDelegate (del), del.Name, l);
1770 IDENTIFIER OPEN_PARENS
1771 opt_formal_parameter_list
1775 Location l = lexer.Location;
1776 Delegate del = new Delegate (
1778 TypeManager.system_void_expr, (int) $2, MakeName ((string) $5),
1779 (Parameters) $7, (Attributes) $1, l);
1781 del.Namespace = current_namespace;
1782 CheckDef (current_container.AddDelegate (del), del.Name, l);
1787 : namespace_or_type_name
1790 namespace_or_type_name
1791 : qualified_identifier
1795 * Before you think of adding a return_type, notice that we have been
1796 * using two rules in the places where it matters (one rule using type
1797 * and another identical one that uses VOID as the return type). This
1798 * gets rid of a shift/reduce couple
1801 : type_name { /* class_type */
1803 This does interfaces, delegates, struct_types, class_types,
1804 parent classes, and more! 4.2
1806 $$ = DecomposeQI ((string) $1, lexer.Location);
1818 // Note that here only unmanaged types are allowed but we
1819 // can't perform checks during this phase - we do it during
1820 // semantic analysis.
1822 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1826 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
1832 | non_expression_type rank_specifier
1834 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1836 | non_expression_type STAR
1838 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1840 | expression rank_specifiers
1842 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1846 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1850 // We need this because the parser will happily go and reduce IDENTIFIER STAR
1851 // through this different path
1853 | multiplicative_expression STAR
1855 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1862 ArrayList types = new ArrayList ();
1867 | type_list COMMA type
1869 ArrayList types = (ArrayList) $1;
1877 * replaces all the productions for isolating the various
1878 * simple types, but we need this to reuse it easily in local_variable_type
1881 : OBJECT { $$ = TypeManager.system_object_expr; }
1882 | STRING { $$ = TypeManager.system_string_expr; }
1883 | BOOL { $$ = TypeManager.system_boolean_expr; }
1884 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
1885 | FLOAT { $$ = TypeManager.system_single_expr; }
1886 | DOUBLE { $$ = TypeManager.system_double_expr; }
1891 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
1892 | BYTE { $$ = TypeManager.system_byte_expr; }
1893 | SHORT { $$ = TypeManager.system_int16_expr; }
1894 | USHORT { $$ = TypeManager.system_uint16_expr; }
1895 | INT { $$ = TypeManager.system_int32_expr; }
1896 | UINT { $$ = TypeManager.system_uint32_expr; }
1897 | LONG { $$ = TypeManager.system_int64_expr; }
1898 | ULONG { $$ = TypeManager.system_uint64_expr; }
1899 | CHAR { $$ = TypeManager.system_char_expr; }
1900 | VOID { $$ = TypeManager.system_void_expr; }
1908 : type rank_specifiers
1910 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1915 // Expressions, section 7.5
1923 | qualified_identifier
1925 string name = (string) $1;
1928 $$ = DecomposeQI (name, lexer.Location);
1930 | parenthesized_expression
1932 | invocation_expression
1936 | post_increment_expression
1937 | post_decrement_expression
1941 | checked_expression
1942 | unchecked_expression
1943 | pointer_member_access
1950 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
1951 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
1952 | NULL { $$ = NullLiteral.Null; }
1956 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
1957 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
1958 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
1963 object v = lexer.Value;
1966 $$ = new IntLiteral ((Int32) v);
1968 $$ = new UIntLiteral ((UInt32) v);
1970 $$ = new LongLiteral ((Int64) v);
1971 else if (v is ulong)
1972 $$ = new ULongLiteral ((UInt64) v);
1974 Console.WriteLine ("OOPS. Unexpected result from scanner");
1979 : TRUE { $$ = new BoolLiteral (true); }
1980 | FALSE { $$ = new BoolLiteral (false); }
1983 parenthesized_expression
1984 : OPEN_PARENS expression CLOSE_PARENS
1989 : primary_expression DOT IDENTIFIER
1991 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1993 | predefined_type DOT IDENTIFIER
1995 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2003 invocation_expression
2004 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2007 Location l = lexer.Location;
2008 Report.Error (1, l, "Parse error");
2010 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2015 : /* empty */ { $$ = null; }
2022 ArrayList list = new ArrayList ();
2026 | argument_list COMMA argument
2028 ArrayList list = (ArrayList) $1;
2037 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2039 | REF variable_reference
2041 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2043 | OUT variable_reference
2045 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2050 : expression { note ("section 5.4"); $$ = $1; }
2054 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2056 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2058 | primary_expression rank_specifiers
2060 // So the super-trick is that primary_expression
2061 // can only be either a SimpleName or a MemberAccess.
2062 // The MemberAccess case arises when you have a fully qualified type-name like :
2064 // SimpleName is when you have
2067 Expression expr = (Expression) $1;
2068 if (!(expr is SimpleName || expr is MemberAccess)) {
2069 Error_ExpectingTypeName (lexer.Location, expr);
2070 $$ = TypeManager.system_object_expr;
2073 // So we extract the string corresponding to the SimpleName
2076 $$ = new SimpleName (GetQualifiedIdentifier (expr) + (string) $2, lexer.Location);
2084 ArrayList list = new ArrayList ();
2088 | expression_list COMMA expression
2090 ArrayList list = (ArrayList) $1;
2099 $$ = new This (current_block, lexer.Location);
2104 : BASE DOT IDENTIFIER
2106 $$ = new BaseAccess ((string) $3, lexer.Location);
2108 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2110 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2114 post_increment_expression
2115 : primary_expression OP_INC
2117 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2118 (Expression) $1, lexer.Location);
2122 post_decrement_expression
2123 : primary_expression OP_DEC
2125 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2126 (Expression) $1, lexer.Location);
2131 : object_or_delegate_creation_expression
2132 | array_creation_expression
2135 object_or_delegate_creation_expression
2136 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2138 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2142 array_creation_expression
2143 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2145 opt_array_initializer
2147 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2149 | NEW type rank_specifiers array_initializer
2151 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2155 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2171 : rank_specifier opt_rank_specifier
2173 $$ = (string) $2 + (string) $1;
2178 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2180 $$ = "[" + (string) $2 + "]";
2200 | dim_separators COMMA
2202 $$ = (string) $1 + ",";
2206 opt_array_initializer
2218 : OPEN_BRACE CLOSE_BRACE
2220 ArrayList list = new ArrayList ();
2223 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2225 $$ = (ArrayList) $2;
2229 variable_initializer_list
2230 : variable_initializer
2232 ArrayList list = new ArrayList ();
2236 | variable_initializer_list COMMA variable_initializer
2238 ArrayList list = (ArrayList) $1;
2245 : TYPEOF OPEN_PARENS type CLOSE_PARENS
2247 $$ = new TypeOf ((Expression) $3, lexer.Location);
2252 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2253 $$ = new SizeOf ((Expression) $3, lexer.Location);
2258 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2260 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2264 unchecked_expression
2265 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2267 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2271 pointer_member_access
2272 : primary_expression OP_PTR IDENTIFIER
2276 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2277 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2281 : primary_expression
2282 | BANG prefixed_unary_expression
2284 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2286 | TILDE prefixed_unary_expression
2288 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2294 : OPEN_PARENS expression CLOSE_PARENS unary_expression
2296 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2298 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2300 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2305 // The idea to split this out is from Rhys' grammar
2306 // to solve the problem with casts.
2308 prefixed_unary_expression
2310 | PLUS prefixed_unary_expression
2312 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2314 | MINUS prefixed_unary_expression
2316 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2318 | OP_INC prefixed_unary_expression
2320 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2321 (Expression) $2, lexer.Location);
2323 | OP_DEC prefixed_unary_expression
2325 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2326 (Expression) $2, lexer.Location);
2328 | STAR prefixed_unary_expression
2330 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2332 | BITWISE_AND prefixed_unary_expression
2334 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2338 pre_increment_expression
2339 : OP_INC prefixed_unary_expression
2341 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2342 (Expression) $2, lexer.Location);
2346 pre_decrement_expression
2347 : OP_DEC prefixed_unary_expression
2349 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2350 (Expression) $2, lexer.Location);
2354 multiplicative_expression
2355 : prefixed_unary_expression
2356 | multiplicative_expression STAR prefixed_unary_expression
2358 $$ = new Binary (Binary.Operator.Multiply,
2359 (Expression) $1, (Expression) $3, lexer.Location);
2361 | multiplicative_expression DIV prefixed_unary_expression
2363 $$ = new Binary (Binary.Operator.Division,
2364 (Expression) $1, (Expression) $3, lexer.Location);
2366 | multiplicative_expression PERCENT prefixed_unary_expression
2368 $$ = new Binary (Binary.Operator.Modulus,
2369 (Expression) $1, (Expression) $3, lexer.Location);
2374 : multiplicative_expression
2375 | additive_expression PLUS multiplicative_expression
2377 $$ = new Binary (Binary.Operator.Addition,
2378 (Expression) $1, (Expression) $3, lexer.Location);
2380 | additive_expression MINUS multiplicative_expression
2382 $$ = new Binary (Binary.Operator.Subtraction,
2383 (Expression) $1, (Expression) $3, lexer.Location);
2388 : additive_expression
2389 | shift_expression OP_SHIFT_LEFT additive_expression
2391 $$ = new Binary (Binary.Operator.LeftShift,
2392 (Expression) $1, (Expression) $3, lexer.Location);
2394 | shift_expression OP_SHIFT_RIGHT additive_expression
2396 $$ = new Binary (Binary.Operator.RightShift,
2397 (Expression) $1, (Expression) $3, lexer.Location);
2401 relational_expression
2403 | relational_expression OP_LT shift_expression
2405 $$ = new Binary (Binary.Operator.LessThan,
2406 (Expression) $1, (Expression) $3, lexer.Location);
2408 | relational_expression OP_GT shift_expression
2410 $$ = new Binary (Binary.Operator.GreaterThan,
2411 (Expression) $1, (Expression) $3, lexer.Location);
2413 | relational_expression OP_LE shift_expression
2415 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2416 (Expression) $1, (Expression) $3, lexer.Location);
2418 | relational_expression OP_GE shift_expression
2420 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2421 (Expression) $1, (Expression) $3, lexer.Location);
2423 | relational_expression IS type
2425 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
2427 | relational_expression AS type
2429 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
2434 : relational_expression
2435 | equality_expression OP_EQ relational_expression
2437 $$ = new Binary (Binary.Operator.Equality,
2438 (Expression) $1, (Expression) $3, lexer.Location);
2440 | equality_expression OP_NE relational_expression
2442 $$ = new Binary (Binary.Operator.Inequality,
2443 (Expression) $1, (Expression) $3, lexer.Location);
2448 : equality_expression
2449 | and_expression BITWISE_AND equality_expression
2451 $$ = new Binary (Binary.Operator.BitwiseAnd,
2452 (Expression) $1, (Expression) $3, lexer.Location);
2456 exclusive_or_expression
2458 | exclusive_or_expression CARRET and_expression
2460 $$ = new Binary (Binary.Operator.ExclusiveOr,
2461 (Expression) $1, (Expression) $3, lexer.Location);
2465 inclusive_or_expression
2466 : exclusive_or_expression
2467 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2469 $$ = new Binary (Binary.Operator.BitwiseOr,
2470 (Expression) $1, (Expression) $3, lexer.Location);
2474 conditional_and_expression
2475 : inclusive_or_expression
2476 | conditional_and_expression OP_AND inclusive_or_expression
2478 $$ = new Binary (Binary.Operator.LogicalAnd,
2479 (Expression) $1, (Expression) $3, lexer.Location);
2483 conditional_or_expression
2484 : conditional_and_expression
2485 | conditional_or_expression OP_OR conditional_and_expression
2487 $$ = new Binary (Binary.Operator.LogicalOr,
2488 (Expression) $1, (Expression) $3, lexer.Location);
2492 conditional_expression
2493 : conditional_or_expression
2494 | conditional_or_expression INTERR expression COLON expression
2496 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2500 assignment_expression
2501 : prefixed_unary_expression ASSIGN expression
2503 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2505 | prefixed_unary_expression OP_MULT_ASSIGN expression
2507 Location l = lexer.Location;
2509 $$ = new CompoundAssign (
2510 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
2512 | prefixed_unary_expression OP_DIV_ASSIGN expression
2514 Location l = lexer.Location;
2516 $$ = new CompoundAssign (
2517 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
2519 | prefixed_unary_expression OP_MOD_ASSIGN expression
2521 Location l = lexer.Location;
2523 $$ = new CompoundAssign (
2524 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
2526 | prefixed_unary_expression OP_ADD_ASSIGN expression
2528 Location l = lexer.Location;
2530 $$ = new CompoundAssign (
2531 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
2533 | prefixed_unary_expression OP_SUB_ASSIGN expression
2535 Location l = lexer.Location;
2537 $$ = new CompoundAssign (
2538 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
2540 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2542 Location l = lexer.Location;
2544 $$ = new CompoundAssign (
2545 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
2547 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2549 Location l = lexer.Location;
2551 $$ = new CompoundAssign (
2552 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
2554 | prefixed_unary_expression OP_AND_ASSIGN expression
2556 Location l = lexer.Location;
2558 $$ = new CompoundAssign (
2559 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
2561 | prefixed_unary_expression OP_OR_ASSIGN expression
2563 Location l = lexer.Location;
2565 $$ = new CompoundAssign (
2566 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
2568 | prefixed_unary_expression OP_XOR_ASSIGN expression
2570 Location l = lexer.Location;
2572 $$ = new CompoundAssign (
2573 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
2578 : conditional_expression
2579 | assignment_expression
2601 name = MakeName ((string) $4);
2603 new_class = new Class (current_container, name, (int) $2,
2604 (Attributes) $1, lexer.Location);
2605 current_container = new_class;
2606 current_container.Namespace = current_namespace;
2607 RootContext.Tree.RecordDecl (name, new_class);
2613 Class new_class = (Class) current_container;
2616 new_class.Bases = (ArrayList) $6;
2618 current_container = current_container.Parent;
2619 CheckDef (current_container.AddClass (new_class), new_class.Name, new_class.Location);
2626 : /* empty */ { $$ = (int) 0; }
2632 | modifiers modifier
2637 if ((m1 & m2) != 0) {
2638 Location l = lexer.Location;
2639 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2641 $$ = (int) (m1 | m2);
2646 : NEW { $$ = Modifiers.NEW; }
2647 | PUBLIC { $$ = Modifiers.PUBLIC; }
2648 | PROTECTED { $$ = Modifiers.PROTECTED; }
2649 | INTERNAL { $$ = Modifiers.INTERNAL; }
2650 | PRIVATE { $$ = Modifiers.PRIVATE; }
2651 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2652 | SEALED { $$ = Modifiers.SEALED; }
2653 | STATIC { $$ = Modifiers.STATIC; }
2654 | READONLY { $$ = Modifiers.READONLY; }
2655 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2656 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2657 | EXTERN { $$ = Modifiers.EXTERN; }
2658 | VOLATILE { $$ = Modifiers.VOLATILE; }
2659 | UNSAFE { $$ = Modifiers.UNSAFE; }
2663 : /* empty */ { $$ = null; }
2664 | class_base { $$ = $1; }
2668 : COLON type_list { $$ = $2; }
2676 // A block is "contained" on the following places:
2678 // property_declaration as part of the accessor body (get/set)
2679 // operator_declaration
2680 // constructor_declaration
2681 // destructor_declaration
2682 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2687 current_block = new Block (current_block, current_local_parameters,
2688 lexer.Location, Location.Null);
2690 opt_statement_list CLOSE_BRACE
2692 while (current_block.Implicit)
2693 current_block = current_block.Parent;
2695 current_block.SetEndLocation (lexer.Location);
2696 current_block = current_block.Parent;
2707 | statement_list statement
2711 : declaration_statement
2713 if ($1 != null && (Block) $1 != current_block){
2714 current_block.AddStatement ((Statement) $1);
2715 current_block = (Block) $1;
2718 | embedded_statement
2720 Statement s = (Statement) $1;
2723 current_block.AddStatement ((Statement) $1);
2731 | expression_statement
2732 | selection_statement
2733 | iteration_statement
2737 | unchecked_statement
2747 $$ = new EmptyStatement ();
2754 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
2756 if (!current_block.AddLabel ((string) $1, labeled)){
2757 Location l = lexer.Location;
2758 Report.Error (140, l, "The label '" + ((string) $1) + "' is a duplicate");
2760 current_block.AddStatement (labeled);
2765 declaration_statement
2766 : local_variable_declaration SEMICOLON
2769 DictionaryEntry de = (DictionaryEntry) $1;
2771 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
2775 | local_constant_declaration SEMICOLON
2778 DictionaryEntry de = (DictionaryEntry) $1;
2780 $$ = declare_local_constant ((Expression) de.Key, (VariableDeclaration) de.Value);
2786 * The following is from Rhys' grammar:
2787 * > Types in local variable declarations must be recognized as
2788 * > expressions to prevent reduce/reduce errors in the grammar.
2789 * > The expressions are converted into types during semantic analysis.
2792 : primary_expression opt_rank_specifier
2794 // FIXME: Do something smart here regarding the composition of the type.
2796 // Ok, the above "primary_expression" is there to get rid of
2797 // both reduce/reduce and shift/reduces in the grammar, it should
2798 // really just be "type_name". If you use type_name, a reduce/reduce
2799 // creeps up. If you use qualified_identifier (which is all we need
2800 // really) two shift/reduces appear.
2803 // So the super-trick is that primary_expression
2804 // can only be either a SimpleName or a MemberAccess.
2805 // The MemberAccess case arises when you have a fully qualified type-name like :
2807 // SimpleName is when you have
2810 Expression expr = (Expression) $1;
2811 if (!(expr is SimpleName || expr is MemberAccess)) {
2812 Error_ExpectingTypeName (lexer.Location, expr);
2816 // So we extract the string corresponding to the SimpleName
2819 if ((string) $2 == "")
2822 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2825 | builtin_types opt_rank_specifier
2827 if ((string) $2 == "")
2830 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2834 local_variable_pointer_type
2835 : primary_expression STAR
2837 Expression expr = (Expression) $1;
2838 Location l = lexer.Location;
2840 if (!(expr is SimpleName || expr is MemberAccess)) {
2841 Error_ExpectingTypeName (l, expr);
2845 $$ = new ComposedCast ((Expression) $1, "*", l);
2847 | builtin_types STAR
2849 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
2853 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
2855 | local_variable_pointer_type STAR
2857 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2861 local_variable_declaration
2862 : local_variable_type variable_declarators
2865 $$ = new DictionaryEntry ($1, $2);
2869 | local_variable_pointer_type opt_rank_specifier variable_declarators
2874 if ((string) $2 == "")
2875 t = (Expression) $1;
2877 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2878 $$ = new DictionaryEntry (t, $3);
2884 local_constant_declaration
2885 : CONST local_variable_type constant_declarator
2888 $$ = new DictionaryEntry ($2, $3);
2894 expression_statement
2895 : statement_expression SEMICOLON
2902 // We have to do the wrapping here and not in the case above,
2903 // because statement_expression is used for example in for_statement
2905 statement_expression
2906 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2907 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2908 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2909 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2910 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2911 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2912 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2914 Report.Error (1002, lexer.Location, "Expecting `;'");
2918 object_creation_expression
2919 : object_or_delegate_creation_expression
2920 { note ("complain if this is a delegate maybe?"); }
2929 : if_statement_open if_statement_rest
2938 oob_stack.Push (lexer.Location);
2943 : boolean_expression CLOSE_PARENS
2946 Location l = (Location) oob_stack.Pop ();
2948 $$ = new If ((Expression) $1, (Statement) $3, l);
2950 if (RootContext.WarningLevel >= 3){
2951 if ($3 is EmptyStatement)
2952 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
2956 | boolean_expression CLOSE_PARENS
2957 embedded_statement ELSE embedded_statement
2959 Location l = (Location) oob_stack.Pop ();
2961 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
2966 : SWITCH OPEN_PARENS
2968 oob_stack.Push (lexer.Location);
2969 switch_stack.Push (current_block);
2971 expression CLOSE_PARENS
2974 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
2975 current_block = (Block) switch_stack.Pop ();
2991 Report.Error (1522, lexer.Location, "Empty switch block");
2999 ArrayList sections = new ArrayList ();
3004 | switch_sections switch_section
3006 ArrayList sections = (ArrayList) $1;
3016 current_block = new Block (current_block, lexer.Location, lexer.Location);
3020 Block topmost = current_block;
3022 while (topmost.Implicit)
3023 topmost = topmost.Parent;
3024 $$ = new SwitchSection ((ArrayList) $1, topmost);
3031 ArrayList labels = new ArrayList ();
3036 | switch_labels switch_label
3038 ArrayList labels = (ArrayList) ($1);
3046 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3047 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3050 1523, lexer.Location,
3051 "The keyword case or default must precede code in switch block");
3065 oob_stack.Push (lexer.Location);
3067 boolean_expression CLOSE_PARENS embedded_statement
3069 Location l = (Location) oob_stack.Pop ();
3070 $$ = new While ((Expression) $4, (Statement) $6, l);
3072 if (RootContext.WarningLevel >= 3){
3073 if ($6 is EmptyStatement)
3074 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3080 : DO embedded_statement
3082 oob_stack.Push (lexer.Location);
3084 boolean_expression CLOSE_PARENS SEMICOLON
3086 Location l = (Location) oob_stack.Pop ();
3088 $$ = new Do ((Statement) $2, (Expression) $6, l);
3094 opt_for_initializer SEMICOLON
3096 Block assign_block = new Block (current_block);
3097 current_block = assign_block;
3099 if ($3 is DictionaryEntry){
3100 DictionaryEntry de = (DictionaryEntry) $3;
3102 Expression type = (Expression) de.Key;
3103 ArrayList var_declarators = (ArrayList) de.Value;
3105 foreach (VariableDeclaration decl in var_declarators){
3109 vi = current_block.AddVariable (
3110 type, decl.identifier, current_local_parameters, decl.Location);
3114 Location l = lexer.Location;
3116 if (decl.expression_or_array_initializer is Expression){
3117 expr = (Expression) decl.expression_or_array_initializer;
3118 } else if (decl.expression_or_array_initializer == null) {
3121 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3122 expr = new ArrayCreation (type, "", init, decl.Location);
3125 LocalVariableReference var;
3126 var = new LocalVariableReference (assign_block, decl.identifier, l);
3129 Assign a = new Assign (var, expr, decl.Location);
3131 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3137 oob_stack.Push (lexer.Location);
3139 opt_for_condition SEMICOLON
3140 opt_for_iterator CLOSE_PARENS
3143 Location l = (Location) oob_stack.Pop ();
3145 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3147 if (RootContext.WarningLevel >= 3){
3148 if ($10 is EmptyStatement)
3149 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3152 current_block.AddStatement (f);
3153 while (current_block.Implicit)
3154 current_block = current_block.Parent;
3156 current_block = current_block.Parent;
3161 : /* empty */ { $$ = new EmptyStatement (); }
3166 : local_variable_declaration
3167 | statement_expression_list
3171 : /* empty */ { $$ = null; }
3172 | boolean_expression
3176 : /* empty */ { $$ = new EmptyStatement (); }
3181 : statement_expression_list
3184 statement_expression_list
3185 : statement_expression
3187 // CHANGE: was `null'
3188 Block b = new Block (current_block, true);
3190 b.AddStatement ((Statement) $1);
3193 | statement_expression_list COMMA statement_expression
3195 Block b = (Block) $1;
3197 b.AddStatement ((Statement) $3);
3203 : FOREACH OPEN_PARENS type IDENTIFIER IN
3205 oob_stack.Push (lexer.Location);
3207 expression CLOSE_PARENS
3209 oob_stack.Push (current_block);
3211 Block foreach_block = new Block (current_block, true);
3212 LocalVariableReference v = null;
3213 Location l = lexer.Location;
3216 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3220 // Get a writable reference to this read-only variable.
3221 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3223 current_block = foreach_block;
3226 oob_stack.Push (current_block);
3230 Block foreach_block = (Block) oob_stack.Pop ();
3231 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3232 Block prev_block = (Block) oob_stack.Pop ();
3233 Location l = (Location) oob_stack.Pop ();
3235 current_block = prev_block;
3238 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3239 foreach_block.AddStatement (f);
3248 | continue_statement
3257 $$ = new Break (lexer.Location);
3262 : CONTINUE SEMICOLON
3264 $$ = new Continue (lexer.Location);
3269 : GOTO IDENTIFIER SEMICOLON
3271 $$ = new Goto (current_block, (string) $2, lexer.Location);
3273 | GOTO CASE constant_expression SEMICOLON
3275 $$ = new GotoCase ((Expression) $3, lexer.Location);
3277 | GOTO DEFAULT SEMICOLON
3279 $$ = new GotoDefault (lexer.Location);
3284 : RETURN opt_expression SEMICOLON
3286 $$ = new Return ((Expression) $2, lexer.Location);
3291 : THROW opt_expression SEMICOLON
3293 $$ = new Throw ((Expression) $2, lexer.Location);
3303 : TRY block catch_clauses
3306 ArrayList s = new ArrayList ();
3308 foreach (Catch cc in (ArrayList) $3) {
3315 // Now s contains the list of specific catch clauses
3316 // and g contains the general one.
3318 $$ = new Try ((Block) $2, s, g, null, lexer.Location);
3320 | TRY block opt_catch_clauses FINALLY block
3323 ArrayList s = new ArrayList ();
3324 ArrayList catch_list = (ArrayList) $3;
3326 if (catch_list != null){
3327 foreach (Catch cc in catch_list) {
3335 $$ = new Try ((Block) $2, s, g, (Block) $5, lexer.Location);
3339 Report.Error (1524, lexer.Location, "Expected catch or finally");
3344 : /* empty */ { $$ = null; }
3351 ArrayList l = new ArrayList ();
3356 | catch_clauses catch_clause
3358 ArrayList l = (ArrayList) $1;
3366 : /* empty */ { $$ = null; }
3371 : CATCH opt_catch_args
3373 Expression type = null;
3377 DictionaryEntry cc = (DictionaryEntry) $2;
3378 type = (Expression) cc.Key;
3379 id = (string) cc.Value;
3382 ArrayList one = new ArrayList ();
3383 Location loc = lexer.Location;
3385 one.Add (new VariableDeclaration (id, null, loc));
3388 current_block = new Block (current_block);
3389 Block b = declare_local_variables (type, one, loc);
3394 Expression type = null;
3398 DictionaryEntry cc = (DictionaryEntry) $2;
3399 type = (Expression) cc.Key;
3400 id = (string) cc.Value;
3404 // FIXME: I can change this for an assignment.
3406 while (current_block != (Block) $1)
3407 current_block = current_block.Parent;
3412 $$ = new Catch (type, id , (Block) $4, lexer.Location);
3417 : /* empty */ { $$ = null; }
3422 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3424 $$ = new DictionaryEntry ($2, $3);
3431 $$ = new Checked ((Block) $2);
3438 $$ = new Unchecked ((Block) $2);
3445 if (!RootContext.Unsafe){
3446 Report.Error (227, lexer.Location,
3447 "Unsafe code can only be used if --unsafe is used");
3450 $$ = new Unsafe ((Block) $3);
3456 type fixed_pointer_declarators
3459 Block assign_block = new Block (current_block, true);
3460 ArrayList list = (ArrayList) $4;
3461 Expression type = (Expression) $3;
3462 Location l = lexer.Location;
3463 int top = list.Count;
3465 for (int i = 0; i < top; i++){
3466 Pair p = (Pair) list [i];
3469 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3476 current_block.AddStatement (assign_block);
3477 current_block = assign_block;
3478 oob_stack.Push (assign_block);
3483 Location l = (Location) oob_stack.Pop ();
3484 Block assign_block = (Block) oob_stack.Pop ();
3486 ArrayList list = (ArrayList) $4;
3487 int top = list.Count;
3489 $$ = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
3493 fixed_pointer_declarators
3494 : fixed_pointer_declarator {
3495 ArrayList declarators = new ArrayList ();
3496 declarators.Add ($1);
3499 | fixed_pointer_declarators COMMA fixed_pointer_declarator
3501 ArrayList declarators = (ArrayList) $1;
3502 declarators.Add ($3);
3507 fixed_pointer_declarator
3508 : IDENTIFIER ASSIGN expression
3510 $$ = new Pair ($1, $3);
3515 : LOCK OPEN_PARENS expression CLOSE_PARENS
3521 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
3526 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
3528 Block assign_block = new Block (current_block);
3529 current_block = assign_block;
3531 oob_stack.Push (lexer.Location);
3533 if ($3 is DictionaryEntry){
3534 DictionaryEntry de = (DictionaryEntry) $3;
3535 Location l = lexer.Location;
3537 Expression type = (Expression) de.Key;
3538 ArrayList var_declarators = (ArrayList) de.Value;
3540 ArrayList vars = new ArrayList ();
3542 foreach (VariableDeclaration decl in var_declarators){
3544 VariableInfo vi = current_block.AddVariable (
3545 type, decl.identifier,
3546 current_local_parameters, decl.Location);
3552 if (decl.expression_or_array_initializer is Expression){
3553 expr = (Expression) decl.expression_or_array_initializer;
3555 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3557 expr = new ArrayCreation (type, "", init, decl.Location);
3560 LocalVariableReference var;
3562 // Get a writable reference to this read-only variable.
3563 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
3565 // This is so that it is not a warning on using variables
3568 vars.Add (new DictionaryEntry (var, expr));
3570 // Assign a = new Assign (var, expr, decl.Location);
3571 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3573 $3 = new DictionaryEntry (type, vars);
3578 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
3579 current_block.AddStatement (u);
3580 while (current_block.Implicit)
3581 current_block = current_block.Parent;
3583 current_block = current_block.Parent;
3587 resource_acquisition
3588 : local_variable_declaration
3595 // A class used to pass around variable declarations and constants
3597 public class VariableDeclaration {
3598 public string identifier;
3599 public object expression_or_array_initializer;
3600 public Location Location;
3601 public Attributes OptAttributes;
3603 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
3605 this.identifier = id;
3606 this.expression_or_array_initializer = eoai;
3608 this.OptAttributes = opt_attrs;
3611 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
3617 // A class used to hold info about an indexer declarator
3620 public class IndexerDeclaration {
3621 public Expression type;
3622 public string interface_type;
3623 public Parameters param_list;
3625 public IndexerDeclaration (Expression type, string interface_type, Parameters param_list)
3628 this.interface_type = interface_type;
3629 this.param_list = param_list;
3634 // A class used to hold info about an operator declarator
3637 public class OperatorDeclaration {
3638 public Operator.OpType optype;
3639 public Expression ret_type, arg1type, arg2type;
3640 public string arg1name, arg2name;
3641 public Location location;
3643 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
3644 Expression arg1type, string arg1name,
3645 Expression arg2type, string arg2name, Location location)
3648 this.ret_type = ret_type;
3649 this.arg1type = arg1type;
3650 this.arg1name = arg1name;
3651 this.arg2type = arg2type;
3652 this.arg2name = arg2name;
3653 this.location = location;
3658 void Error_ExpectingTypeName (Location l, Expression expr)
3660 if (expr is Invocation){
3661 Report.Error (1002, l, "; expected");
3663 Report.Error (-1, l, "Invalid Type definition");
3668 // Given the @class_name name, it creates a fully qualified name
3669 // based on the containing declaration space
3672 MakeName (string class_name)
3674 string ns = current_namespace.Name;
3675 string container_name = current_container.Name;
3677 if (container_name == ""){
3679 return ns + "." + class_name;
3683 return container_name + "." + class_name;
3687 // Used to report back to the user the result of a declaration
3688 // in the current declaration space
3691 CheckDef (AdditionResult result, string name, Location l)
3693 if (result == AdditionResult.Success)
3697 case AdditionResult.NameExists:
3698 Report.Error (102, l, "The container `" + current_container.Name +
3699 "' already contains a definition for `"+
3705 // This is handled only for static Constructors, because
3706 // in reality we handle these by the semantic analysis later
3708 case AdditionResult.MethodExists:
3710 111, l, "Class `"+current_container.Name+
3711 "' already defines a member called '" +
3712 name + "' with the same parameter types (more than one default constructor)");
3715 case AdditionResult.EnclosingClash:
3716 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
3719 case AdditionResult.NotAConstructor:
3720 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
3726 CheckDef (bool result, string name, Location l)
3730 CheckDef (AdditionResult.NameExists, name, l);
3733 Expression DecomposeQI (string name, Location loc)
3737 if (name.IndexOf ('.') == -1){
3738 return new SimpleName (name, loc);
3740 int pos = name.LastIndexOf (".");
3741 string left = name.Substring (0, pos);
3742 string right = name.Substring (pos + 1);
3744 o = DecomposeQI (left, loc);
3746 return new MemberAccess (o, right, loc);
3751 // This method is used to get at the complete string representation of
3752 // a fully-qualified type name, hiding inside a MemberAccess ;-)
3753 // This is necessary because local_variable_type admits primary_expression
3754 // as the type of the variable. So we do some extra checking
3756 string GetQualifiedIdentifier (Expression expr)
3758 if (expr is SimpleName)
3759 return ((SimpleName)expr).Name;
3760 else if (expr is MemberAccess)
3761 return GetQualifiedIdentifier (((MemberAccess)expr).Expr) + "." + ((MemberAccess) expr).Identifier;
3763 throw new Exception ("Expr has to be either SimpleName or MemberAccess! (" + expr + ")");
3767 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
3769 Block implicit_block;
3770 ArrayList inits = null;
3773 // We use the `Used' property to check whether statements
3774 // have been added to the current block. If so, we need
3775 // to create another block to contain the new declaration
3776 // otherwise, as an optimization, we use the same block to
3777 // add the declaration.
3779 // FIXME: A further optimization is to check if the statements
3780 // that were added were added as part of the initialization
3781 // below. In which case, no other statements have been executed
3782 // and we might be able to reduce the number of blocks for
3783 // situations like this:
3785 // int j = 1; int k = j + 1;
3787 if (current_block.Used) {
3788 implicit_block = new Block (current_block, true, loc, Location.Null);
3789 implicit_block.AddChildVariableNames (current_block);
3791 implicit_block = current_block;
3793 foreach (VariableDeclaration decl in variable_declarators){
3795 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
3796 if (decl.expression_or_array_initializer != null){
3798 inits = new ArrayList ();
3805 return implicit_block;
3807 foreach (VariableDeclaration decl in inits){
3811 if (decl.expression_or_array_initializer is Expression){
3812 expr = (Expression) decl.expression_or_array_initializer;
3815 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3817 expr = new ArrayCreation (type, "", init, decl.Location);
3820 LocalVariableReference var;
3821 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
3823 assign = new Assign (var, expr, decl.Location);
3825 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
3828 return implicit_block;
3831 Block declare_local_constant (Expression type, VariableDeclaration decl)
3833 Block implicit_block;
3835 if (current_block.Used)
3836 implicit_block = new Block (current_block, true);
3838 implicit_block = current_block;
3840 if (!(implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
3841 current_local_parameters, decl.Location))){
3844 return implicit_block;
3847 void CheckAttributeTarget (string a)
3851 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
3855 Location l = lexer.Location;
3856 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
3862 void CheckUnaryOperator (Operator.OpType op)
3866 case Operator.OpType.LogicalNot:
3867 case Operator.OpType.OnesComplement:
3868 case Operator.OpType.Increment:
3869 case Operator.OpType.Decrement:
3870 case Operator.OpType.True:
3871 case Operator.OpType.False:
3872 case Operator.OpType.Addition:
3873 case Operator.OpType.Subtraction:
3878 Location l = lexer.Location;
3879 Report.Error (1019, l, "Overloadable unary operator expected");
3885 void CheckBinaryOperator (Operator.OpType op)
3889 case Operator.OpType.Addition:
3890 case Operator.OpType.Subtraction:
3891 case Operator.OpType.Multiply:
3892 case Operator.OpType.Division:
3893 case Operator.OpType.Modulus:
3894 case Operator.OpType.BitwiseAnd:
3895 case Operator.OpType.BitwiseOr:
3896 case Operator.OpType.ExclusiveOr:
3897 case Operator.OpType.LeftShift:
3898 case Operator.OpType.RightShift:
3899 case Operator.OpType.Equality:
3900 case Operator.OpType.Inequality:
3901 case Operator.OpType.GreaterThan:
3902 case Operator.OpType.LessThan:
3903 case Operator.OpType.GreaterThanOrEqual:
3904 case Operator.OpType.LessThanOrEqual:
3908 Location l = lexer.Location;
3909 Report.Error (1020, l, "Overloadable binary operator expected");
3915 void syntax_error (Location l, string msg)
3917 Report.Error (1003, l, "Syntax error, " + msg);
3920 void output (string s)
3922 Console.WriteLine (s);
3925 void note (string s)
3927 // Used to put annotations
3932 public Tokenizer Lexer {
3938 public CSharpParser (StreamReader reader, SourceFile file, ArrayList defines)
3940 current_namespace = new Namespace (null, file, "");
3941 this.name = file.Name;
3943 current_container = RootContext.Tree.Types;
3944 current_container.Namespace = current_namespace;
3945 oob_stack = new Stack ();
3946 switch_stack = new Stack ();
3948 lexer = new Tokenizer (reader, file, defines);
3951 public override void parse ()
3954 if (yacc_verbose_flag)
3955 yyparse (lexer, new yydebug.yyDebugSimple ());
3958 Tokenizer tokenizer = lexer as Tokenizer;
3959 tokenizer.cleanup ();
3960 } catch (Exception e){
3961 // Please do not remove this, it is used during debugging
3964 Report.Error (-25, lexer.Location, ": Parsing error ");
3965 Console.WriteLine (e);