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;
37 IIteratorContainer iterator_container;
40 // Current block is used to add statements as we find
47 // Current interface is used by the various declaration
48 // productions in the interface declaration to "add"
49 // the interfaces as we find them.
51 Interface current_interface;
54 // This is used by the unary_expression code to resolve
55 // a name against a parameter.
57 Parameters current_local_parameters;
60 // Using during property parsing to describe the implicit
61 // value parameter that is passed to the "set" and "get"accesor
62 // methods (properties and indexers).
64 Expression implicit_value_parameter_type;
65 Parameters indexer_parameters;
68 // Used to determine if we are parsing the get/set pair
69 // of an indexer or a property
74 // An out-of-band stack.
90 %token NONE /* This token is never returned by our lexer */
91 %token ERROR // This is used not by the parser, but by the tokenizer.
95 *These are the C# keywords
181 /* C# keywords which are not really keywords */
185 /* C# single character operators/punctuation. */
186 %token OPEN_BRACE "{"
187 %token CLOSE_BRACE "}"
188 %token OPEN_BRACKET "["
189 %token CLOSE_BRACKET "]"
190 %token OPEN_PARENS "("
191 %token CLOSE_PARENS ")"
204 %token BITWISE_AND "&"
205 %token BITWISE_OR "|"
212 /* C# multi-character operators. */
215 %token OP_SHIFT_LEFT "<<"
216 %token OP_SHIFT_RIGHT ">>"
223 %token OP_MULT_ASSIGN "*="
224 %token OP_DIV_ASSIGN "/="
225 %token OP_MOD_ASSIGN "%="
226 %token OP_ADD_ASSIGN "+="
227 %token OP_SUB_ASSIGN "-="
228 %token OP_SHIFT_LEFT_ASSIGN "<<="
229 %token OP_SHIFT_RIGHT_ASSIGN ">>="
230 %token OP_AND_ASSIGN "&="
231 %token OP_XOR_ASSIGN "^="
232 %token OP_OR_ASSIGN "|="
236 %token LITERAL_INTEGER "int literal"
237 %token LITERAL_FLOAT "float literal"
238 %token LITERAL_DOUBLE "double literal"
239 %token LITERAL_DECIMAL "decimal literal"
240 %token LITERAL_CHARACTER "character literal"
241 %token LITERAL_STRING "string literal"
245 /* Add precedence rules to solve dangling else s/r conflict */
254 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
256 %left STAR DIV PERCENT
257 %right BANG CARRET UMINUS
258 %nonassoc OP_INC OP_DEC
260 %left OPEN_BRACKET OPEN_BRACE
264 %start compilation_unit
268 : outer_declarations opt_EOF
269 | outer_declarations attribute_sections opt_EOF
270 | attribute_sections opt_EOF
271 | opt_EOF /* allow empty files */
281 | outer_declarations outer_declaration
286 | namespace_member_declaration
291 | using_directives using_directive
295 : using_alias_directive
296 | using_namespace_directive
299 using_alias_directive
300 : USING IDENTIFIER ASSIGN
301 namespace_or_type_name SEMICOLON
303 current_namespace.UsingAlias ((string) $2, (string) $4, lexer.Location);
307 using_namespace_directive
308 : USING namespace_name SEMICOLON
310 current_namespace.Using ((string) $2, lexer.Location);
315 // Strictly speaking, namespaces don't have attributes but
316 // we parse global attributes along with namespace declarations and then
319 namespace_declaration
320 : opt_attributes NAMESPACE qualified_identifier
322 Attributes attrs = (Attributes) $1;
325 foreach (AttributeSection asec in attrs.AttributeSections)
326 if (asec.Target == "assembly")
327 RootContext.AddGlobalAttributeSection (current_container, asec);
329 Report.Error(1518, Lexer.Location,
330 "Attributes cannot be applied to namespaces."
331 + " Expected class, delegate, enum, interface, or struct");
334 current_namespace = RootContext.Tree.RecordNamespace (current_namespace, file, (string) $3);
336 namespace_body opt_semicolon
338 current_namespace = current_namespace.Parent;
354 | qualified_identifier DOT IDENTIFIER {
355 $$ = (($1).ToString ()) + "." + ($3.ToString ()); }
360 : namespace_or_type_name
366 opt_namespace_member_declarations
377 opt_namespace_member_declarations
379 | namespace_member_declarations
382 namespace_member_declarations
383 : namespace_member_declaration
384 | namespace_member_declarations namespace_member_declaration
387 namespace_member_declaration
394 Class c = (Class) $1;
395 mod_flags = c.ModFlags;
397 } else if ($1 is Struct){
398 Struct s = (Struct) $1;
399 mod_flags = s.ModFlags;
404 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
406 1527, lexer.Location,
407 "Namespace elements cant be explicitly " +
408 "declared private or protected in `" + name + "'");
410 current_namespace.DeclarationFound = true;
412 | namespace_declaration {
413 current_namespace.DeclarationFound = true;
420 | interface_declaration
422 | delegate_declaration
424 // Enable this when we have handled all errors, because this acts as a generic fallback
427 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
437 | attribute_sections { $$ = $1; }
443 AttributeSection sect = (AttributeSection) $1;
445 if (sect.Target == "assembly")
446 RootContext.AddGlobalAttributeSection (current_container, sect);
449 $$ = new Attributes ((AttributeSection) $1);
451 | attribute_sections attribute_section
453 Attributes attrs = null;
454 AttributeSection sect = (AttributeSection) $2;
456 if (sect.Target == "assembly")
457 RootContext.AddGlobalAttributeSection (current_container, sect);
460 attrs = (Attributes) $1;
461 attrs.AddAttributeSection (sect);
469 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
471 string target = null;
474 target = (string) $2;
476 $$ = new AttributeSection (target, (ArrayList) $3);
478 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
480 $$ = new AttributeSection (null, (ArrayList) $2);
484 attribute_target_specifier
485 : attribute_target COLON
494 CheckAttributeTarget ((string) $1);
497 | EVENT { $$ = "event"; }
498 | RETURN { $$ = "return"; }
504 ArrayList attrs = new ArrayList ();
510 | attribute_list COMMA attribute
512 ArrayList attrs = (ArrayList) $1;
524 opt_attribute_arguments
526 $$ = new Attribute ((string) $1, (ArrayList) $3, (Location) $2);
531 : type_name { /* reserved attribute name or identifier: 17.4 */ }
534 opt_attribute_arguments
535 : /* empty */ { $$ = null; }
536 | OPEN_PARENS attribute_arguments CLOSE_PARENS
544 : opt_positional_argument_list
549 ArrayList args = new ArrayList ();
555 | positional_argument_list COMMA named_argument_list
557 ArrayList args = new ArrayList ();
563 | named_argument_list
565 ArrayList args = new ArrayList ();
574 opt_positional_argument_list
575 : /* empty */ { $$ = null; }
576 | positional_argument_list
579 positional_argument_list
582 ArrayList args = new ArrayList ();
583 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
587 | positional_argument_list COMMA expression
589 ArrayList args = (ArrayList) $1;
590 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
599 ArrayList args = new ArrayList ();
604 | named_argument_list COMMA named_argument
606 ArrayList args = (ArrayList) $1;
614 : IDENTIFIER ASSIGN expression
616 $$ = new DictionaryEntry (
618 new Argument ((Expression) $3, Argument.AType.Expression));
624 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
627 opt_class_member_declarations
629 | class_member_declarations
632 class_member_declarations
633 : class_member_declaration
634 | class_member_declarations
635 class_member_declaration
638 class_member_declaration
639 : constant_declaration // done
640 | field_declaration // done
641 | method_declaration // done
642 | property_declaration // done
643 | event_declaration // done
644 | indexer_declaration // done
645 | operator_declaration // done
646 | constructor_declaration // done
647 | destructor_declaration // done
657 string full_struct_name = MakeName ((string) $4);
659 new_struct = new Struct (current_container, full_struct_name, (int) $2,
660 (Attributes) $1, lexer.Location);
661 current_container = new_struct;
662 current_container.Namespace = current_namespace;
663 RootContext.Tree.RecordDecl (full_struct_name, new_struct);
669 Struct new_struct = (Struct) current_container;
672 new_struct.Bases = (ArrayList) $6;
674 current_container = current_container.Parent;
675 CheckDef (current_container.AddStruct (new_struct), new_struct.Name, new_struct.Location);
681 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
684 opt_struct_member_declarations
686 | struct_member_declarations
689 struct_member_declarations
690 : struct_member_declaration
691 | struct_member_declarations struct_member_declaration
694 struct_member_declaration
695 : constant_declaration
698 | property_declaration
700 | indexer_declaration
701 | operator_declaration
702 | constructor_declaration
706 * This is only included so we can flag error 575:
707 * destructors only allowed on class types
709 | destructor_declaration
720 foreach (VariableDeclaration constant in (ArrayList) $5){
721 Location l = constant.Location;
723 Const c = new Const (
724 (Expression) $4, (string) constant.identifier,
725 (Expression) constant.expression_or_array_initializer, (int) $2,
728 CheckDef (current_container.AddConstant (c), c.Name, l);
734 : constant_declarator
736 ArrayList constants = new ArrayList ();
740 | constant_declarators COMMA constant_declarator
742 ArrayList constants = (ArrayList) $1;
749 : IDENTIFIER ASSIGN constant_expression
751 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
762 Expression type = (Expression) $3;
765 foreach (VariableDeclaration var in (ArrayList) $4){
766 Location l = var.Location;
768 Field field = new Field (type, mod, var.identifier,
769 var.expression_or_array_initializer,
772 CheckDef (current_container.AddField (field), field.Name, l);
780 Report.Error (670, lexer.Location, "void type is not allowed for fields");
785 : variable_declarator
787 ArrayList decl = new ArrayList ();
791 | variable_declarators COMMA variable_declarator
793 ArrayList decls = (ArrayList) $1;
800 : IDENTIFIER ASSIGN variable_initializer
802 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
806 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
819 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
821 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
827 iterator_container = (IIteratorContainer) $1;
831 Method method = (Method) $1;
832 Block b = (Block) $3;
833 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
836 if ((method.ModFlags & extern_abstract) == 0){
838 501, lexer.Location, current_container.MakeName (method.Name) +
839 "must declare a body because it is not marked abstract or extern");
842 if ((method.ModFlags & Modifiers.EXTERN) != 0){
844 179, lexer.Location, current_container.MakeName (method.Name) +
845 " is declared extern, but has a body");
849 method.Block = (Block) $3;
850 CheckDef (current_container.AddMethod (method), method.Name, method.Location);
852 current_local_parameters = null;
853 iterator_container = null;
867 1585, lexer.Location, "Member modifier `" +
868 Modifiers.Name (i) + "' must precede member type and name");
881 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
883 Method method = new Method ((Expression) $3, (int) $2, (string) $4,
884 (Parameters) $6, (Attributes) $1, lexer.Location);
886 current_local_parameters = (Parameters) $6;
894 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
896 Method method = new Method (TypeManager.system_void_expr, (int) $2, (string) $4,
897 (Parameters) $6, (Attributes) $1, lexer.Location);
899 current_local_parameters = (Parameters) $6;
906 | SEMICOLON { $$ = null; }
909 opt_formal_parameter_list
910 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
911 | formal_parameter_list
914 formal_parameter_list
917 ArrayList pars_list = (ArrayList) $1;
919 Parameter [] pars = new Parameter [pars_list.Count];
920 pars_list.CopyTo (pars);
922 $$ = new Parameters (pars, null, lexer.Location);
924 | fixed_parameters COMMA parameter_array
926 ArrayList pars_list = (ArrayList) $1;
928 Parameter [] pars = new Parameter [pars_list.Count];
929 pars_list.CopyTo (pars);
931 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
935 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
942 ArrayList pars = new ArrayList ();
947 | fixed_parameters COMMA fixed_parameter
949 ArrayList pars = (ArrayList) $1;
958 opt_parameter_modifier
962 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
966 opt_parameter_modifier
967 : /* empty */ { $$ = Parameter.Modifier.NONE; }
972 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
973 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
977 : opt_attributes PARAMS type IDENTIFIER
979 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
980 note ("type must be a single-dimension array type");
985 : qualified_identifier
994 implicit_value_parameter_type = (Expression) $3;
996 lexer.PropertyParsing = true;
1000 accessor_declarations
1002 lexer.PropertyParsing = false;
1007 Pair pair = (Pair) $7;
1008 Accessor get_block = (Accessor) pair.First;
1009 Accessor set_block = (Accessor) pair.Second;
1011 Location loc = (Location) $6;
1012 prop = new Property ((Expression) $3, (string) $4, (int) $2, get_block, set_block,
1013 (Attributes) $1, loc);
1015 CheckDef (current_container.AddProperty (prop), prop.Name, loc);
1016 implicit_value_parameter_type = null;
1020 accessor_declarations
1021 : get_accessor_declaration opt_set_accessor_declaration
1023 $$ = new Pair ($1, $2);
1025 | set_accessor_declaration opt_get_accessor_declaration
1027 $$ = new Pair ($2, $1);
1031 opt_get_accessor_declaration
1032 : /* empty */ { $$ = null; }
1033 | get_accessor_declaration
1036 opt_set_accessor_declaration
1037 : /* empty */ { $$ = null; }
1038 | set_accessor_declaration
1041 get_accessor_declaration
1042 : opt_attributes GET
1044 // If this is not the case, then current_local_parameters has already
1045 // been set in indexer_declaration
1046 if (parsing_indexer == false)
1047 current_local_parameters = null;
1049 current_local_parameters = indexer_parameters;
1050 lexer.PropertyParsing = false;
1054 $$ = new Accessor ((Block) $4, (Attributes) $1);
1055 current_local_parameters = null;
1056 lexer.PropertyParsing = true;
1060 set_accessor_declaration
1061 : opt_attributes SET
1064 Parameter implicit_value_parameter = new Parameter (
1065 implicit_value_parameter_type, "value",
1066 Parameter.Modifier.NONE, null);
1068 if (parsing_indexer == false) {
1069 args = new Parameter [1];
1070 args [0] = implicit_value_parameter;
1071 current_local_parameters = new Parameters (args, null, lexer.Location);
1073 Parameter [] fpars = indexer_parameters.FixedParameters;
1076 int count = fpars.Length;
1078 args = new Parameter [count + 1];
1079 fpars.CopyTo (args, 0);
1080 args [count] = implicit_value_parameter;
1083 current_local_parameters = new Parameters (
1084 args, indexer_parameters.ArrayParameter, lexer.Location);
1087 lexer.PropertyParsing = false;
1091 $$ = new Accessor ((Block) $4, (Attributes) $1);
1092 current_local_parameters = null;
1093 lexer.PropertyParsing = true;
1099 | SEMICOLON { $$ = null; }
1102 interface_declaration
1105 INTERFACE IDENTIFIER
1107 Interface new_interface;
1108 string full_interface_name = MakeName ((string) $4);
1110 new_interface = new Interface (current_container, full_interface_name, (int) $2,
1111 (Attributes) $1, lexer.Location);
1112 if (current_interface != null) {
1113 Location l = lexer.Location;
1114 Report.Error (-2, l, "Internal compiler error: interface inside interface");
1116 current_interface = new_interface;
1117 new_interface.Namespace = current_namespace;
1118 RootContext.Tree.RecordDecl (full_interface_name, new_interface);
1121 interface_body opt_semicolon
1123 Interface new_interface = (Interface) current_interface;
1126 new_interface.Bases = (ArrayList) $6;
1128 current_interface = null;
1129 CheckDef (current_container.AddInterface (new_interface),
1130 new_interface.Name, new_interface.Location);
1135 : /* empty */ { $$ = null; }
1140 : COLON interface_type_list { $$ = $2; }
1146 ArrayList interfaces = new ArrayList ();
1148 interfaces.Add ($1);
1151 | interface_type_list COMMA interface_type
1153 ArrayList interfaces = (ArrayList) $1;
1154 interfaces.Add ($3);
1161 opt_interface_member_declarations
1165 opt_interface_member_declarations
1167 | interface_member_declarations
1170 interface_member_declarations
1171 : interface_member_declaration
1172 | interface_member_declarations interface_member_declaration
1175 interface_member_declaration
1176 : interface_method_declaration
1178 InterfaceMethod m = (InterfaceMethod) $1;
1180 CheckDef (current_interface.AddMethod (m), m.Name, m.Location);
1182 | interface_property_declaration
1184 InterfaceProperty p = (InterfaceProperty) $1;
1186 CheckDef (current_interface.AddProperty (p), p.Name, p.Location);
1188 | interface_event_declaration
1190 InterfaceEvent e = (InterfaceEvent) $1;
1192 CheckDef (current_interface.AddEvent (e), e.Name, lexer.Location);
1194 | interface_indexer_declaration
1196 InterfaceIndexer i = (InterfaceIndexer) $1;
1198 CheckDef (current_interface.AddIndexer (i), "indexer", i.Location);
1203 : /* empty */ { $$ = false; }
1204 | NEW { $$ = true; }
1207 interface_method_declaration
1208 : opt_attributes opt_new type IDENTIFIER
1209 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1212 $$ = new InterfaceMethod ((Expression) $3, (string) $4, (bool) $2,
1213 (Parameters) $6, (Attributes) $1, lexer.Location);
1215 | opt_attributes opt_new VOID IDENTIFIER
1216 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1219 $$ = new InterfaceMethod (
1220 TypeManager.system_void_expr, (string) $4, (bool) $2, (Parameters) $6,
1221 (Attributes) $1, lexer.Location);
1225 interface_property_declaration
1230 { lexer.PropertyParsing = true; }
1232 { lexer.PropertyParsing = false; }
1237 $$ = new InterfaceProperty ((Expression) $3, (string) $4, (bool) $2,
1238 (gs & 1) == 1, (gs & 2) == 2, (Attributes) $1,
1244 : opt_attributes GET SEMICOLON { $$ = 1; }
1245 | opt_attributes SET SEMICOLON { $$ = 2; }
1246 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1248 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1252 interface_event_declaration
1253 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1255 $$ = new InterfaceEvent ((Expression) $4, (string) $5, (bool) $2, (Attributes) $1,
1260 interface_indexer_declaration
1261 : opt_attributes opt_new type THIS
1262 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1264 { lexer.PropertyParsing = true; }
1266 { lexer.PropertyParsing = false; }
1269 int a_flags = (int) $10;
1271 bool do_get = (a_flags & 1) == 1;
1272 bool do_set = (a_flags & 2) == 2;
1274 $$ = new InterfaceIndexer ((Expression) $3, (Parameters) $6, do_get, do_set,
1275 (bool) $2, (Attributes) $1, lexer.Location);
1279 operator_declaration
1280 : opt_attributes opt_modifiers operator_declarator operator_body
1282 OperatorDeclaration decl = (OperatorDeclaration) $3;
1284 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
1285 decl.arg2type, decl.arg2name, (Block) $4, (Attributes) $1, decl.location);
1287 // Note again, checking is done in semantic analysis
1288 current_container.AddOperator (op);
1290 current_local_parameters = null;
1296 | SEMICOLON { $$ = null; }
1299 : type OPERATOR overloadable_operator
1300 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1302 Operator.OpType op = (Operator.OpType) $3;
1303 CheckUnaryOperator (op);
1305 if (op == Operator.OpType.Addition)
1306 op = Operator.OpType.UnaryPlus;
1308 if (op == Operator.OpType.Subtraction)
1309 op = Operator.OpType.UnaryNegation;
1311 Parameter [] pars = new Parameter [1];
1313 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1315 current_local_parameters = new Parameters (pars, null, lexer.Location);
1317 $$ = new OperatorDeclaration (op, (Expression) $1, (Expression) $5, (string) $6,
1318 null, null, lexer.Location);
1320 | type OPERATOR overloadable_operator
1322 type IDENTIFIER COMMA
1326 CheckBinaryOperator ((Operator.OpType) $3);
1328 Parameter [] pars = new Parameter [2];
1330 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1331 pars [1] = new Parameter ((Expression) $8, (string) $9, Parameter.Modifier.NONE, null);
1333 current_local_parameters = new Parameters (pars, null, lexer.Location);
1335 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1336 (Expression) $5, (string) $6,
1337 (Expression) $8, (string) $9, lexer.Location);
1339 | conversion_operator_declarator
1342 overloadable_operator
1344 : BANG { $$ = Operator.OpType.LogicalNot; }
1345 | TILDE { $$ = Operator.OpType.OnesComplement; }
1346 | OP_INC { $$ = Operator.OpType.Increment; }
1347 | OP_DEC { $$ = Operator.OpType.Decrement; }
1348 | TRUE { $$ = Operator.OpType.True; }
1349 | FALSE { $$ = Operator.OpType.False; }
1350 // Unary and binary:
1351 | PLUS { $$ = Operator.OpType.Addition; }
1352 | MINUS { $$ = Operator.OpType.Subtraction; }
1354 | STAR { $$ = Operator.OpType.Multiply; }
1355 | DIV { $$ = Operator.OpType.Division; }
1356 | PERCENT { $$ = Operator.OpType.Modulus; }
1357 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1358 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1359 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1360 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1361 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1362 | OP_EQ { $$ = Operator.OpType.Equality; }
1363 | OP_NE { $$ = Operator.OpType.Inequality; }
1364 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1365 | OP_LT { $$ = Operator.OpType.LessThan; }
1366 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1367 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1370 conversion_operator_declarator
1371 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1373 Parameter [] pars = new Parameter [1];
1375 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1377 current_local_parameters = new Parameters (pars, null, lexer.Location);
1379 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1380 null, null, lexer.Location);
1382 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1384 Parameter [] pars = new Parameter [1];
1386 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1388 current_local_parameters = new Parameters (pars, null, lexer.Location);
1390 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1391 null, null, lexer.Location);
1395 syntax_error (lexer.Location, "'operator' expected");
1399 syntax_error (lexer.Location, "'operator' expected");
1403 constructor_declaration
1406 constructor_declarator
1409 Constructor c = (Constructor) $3;
1410 c.Block = (Block) $4;
1411 c.OptAttributes = (Attributes) $1;
1412 c.ModFlags = (int) $2;
1414 if (c.Name == current_container.Basename){
1415 if ((c.ModFlags & Modifiers.STATIC) != 0){
1416 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1418 515, c.Location, String.Format (
1419 "`{0}.{1}': static constructor can not have access modifiers",
1420 c.Name, current_container.Name));
1423 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1425 if (c.Initializer != null){
1428 "Static constructors can not have an explicit this or base " +
1429 "constructor invocations");
1432 if (!c.Parameters.Empty){
1434 132, c.Location, "Static constructors should not have parameters");
1437 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1440 // We let another layer check the validity of the constructor.
1441 Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1444 CheckDef (current_container.AddConstructor (c), c.Name, c.Location);
1446 current_local_parameters = null;
1450 constructor_declarator
1452 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1454 oob_stack.Push (lexer.Location);
1456 current_local_parameters = (Parameters) $3;
1458 opt_constructor_initializer
1460 Location l = (Location) oob_stack.Pop ();
1461 $$ = new Constructor ((string) $1, (Parameters) $3, (ConstructorInitializer) $6, l);
1467 | SEMICOLON { $$ = null; }
1470 opt_constructor_initializer
1471 : /* empty */ { $$ = null; }
1472 | constructor_initializer
1475 constructor_initializer
1476 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1478 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1480 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1482 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1485 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1490 destructor_declaration
1491 : opt_attributes TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1493 if ((string) $3 != current_container.Basename){
1494 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1495 } else if (!(current_container is Class)){
1496 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1498 Location l = lexer.Location;
1501 if (!RootContext.StdLib && current_container.Name == "System.Object")
1502 m = Modifiers.PROTECTED | Modifiers.VIRTUAL;
1504 m = Modifiers.PROTECTED | Modifiers.OVERRIDE;
1506 Method d = new Method (
1507 TypeManager.system_void_expr, m, "Finalize",
1508 new Parameters (null, null, l), (Attributes) $1, l);
1510 d.Block = (Block) $6;
1511 CheckDef (current_container.AddMethod (d), d.Name, d.Location);
1519 EVENT type variable_declarators SEMICOLON
1521 foreach (VariableDeclaration var in (ArrayList) $5) {
1523 Event e = new Event ((Expression) $4, var.identifier,
1524 var.expression_or_array_initializer,
1525 (int) $2, null, null, (Attributes) $1, lexer.Location);
1527 CheckDef (current_container.AddEvent (e), e.Name, e.Location);
1533 EVENT type member_name
1536 implicit_value_parameter_type = (Expression) $4;
1537 lexer.EventParsing = true;
1538 oob_stack.Push (lexer.Location);
1540 event_accessor_declarations
1542 lexer.EventParsing = false;
1546 Location loc = (Location) oob_stack.Pop ();
1548 Pair pair = (Pair) $8;
1549 Accessor add_accessor = null;
1550 Accessor rem_accessor = null;
1552 if (pair.First != null)
1553 add_accessor = (Accessor) pair.First;
1554 if (pair.Second != null)
1555 rem_accessor = (Accessor) pair.Second;
1557 Event e = new Event ((Expression) $4, (string) $5, null, (int) $2, add_accessor, rem_accessor,
1558 (Attributes) $1, loc);
1560 CheckDef (current_container.AddEvent (e), e.Name, loc);
1561 implicit_value_parameter_type = null;
1565 event_accessor_declarations
1566 : add_accessor_declaration remove_accessor_declaration
1568 $$ = new Pair ($1, $2);
1570 | remove_accessor_declaration add_accessor_declaration
1572 $$ = new Pair ($2, $1);
1576 add_accessor_declaration
1577 : opt_attributes ADD
1579 Parameter [] args = new Parameter [1];
1580 Parameter implicit_value_parameter = new Parameter (
1581 implicit_value_parameter_type, "value",
1582 Parameter.Modifier.NONE, null);
1584 args [0] = implicit_value_parameter;
1586 current_local_parameters = new Parameters (args, null, lexer.Location);
1587 lexer.EventParsing = false;
1591 $$ = new Accessor ((Block) $4, (Attributes) $1);
1592 lexer.EventParsing = true;
1594 | opt_attributes ADD error {
1595 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1600 remove_accessor_declaration
1601 : opt_attributes REMOVE
1603 Parameter [] args = new Parameter [1];
1604 Parameter implicit_value_parameter = new Parameter (
1605 implicit_value_parameter_type, "value",
1606 Parameter.Modifier.NONE, null);
1608 args [0] = implicit_value_parameter;
1610 current_local_parameters = new Parameters (args, null, lexer.Location);
1611 lexer.EventParsing = false;
1615 $$ = new Accessor ((Block) $4, (Attributes) $1);
1616 lexer.EventParsing = true;
1618 | opt_attributes REMOVE error {
1619 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1625 : opt_attributes opt_modifiers indexer_declarator
1628 IndexerDeclaration decl = (IndexerDeclaration) $3;
1630 implicit_value_parameter_type = decl.type;
1632 lexer.PropertyParsing = true;
1633 parsing_indexer = true;
1635 indexer_parameters = decl.param_list;
1636 oob_stack.Push (lexer.Location);
1638 accessor_declarations
1640 lexer.PropertyParsing = false;
1641 parsing_indexer = false;
1645 // The signature is computed from the signature of the indexer. Look
1646 // at section 3.6 on the spec
1647 Location loc = (Location) oob_stack.Pop ();
1649 IndexerDeclaration decl = (IndexerDeclaration) $3;
1650 Pair pair = (Pair) $6;
1651 Accessor get_block = (Accessor) pair.First;
1652 Accessor set_block = (Accessor) pair.Second;
1654 indexer = new Indexer (decl.type, decl.interface_type, (int) $2, decl.param_list,
1655 get_block, set_block, (Attributes) $1, loc);
1657 // Note that there is no equivalent of CheckDef for this case
1658 // We shall handle this in semantic analysis
1660 current_container.AddIndexer (indexer);
1662 current_local_parameters = null;
1663 implicit_value_parameter_type = null;
1664 indexer_parameters = null;
1669 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1671 Parameters pars = (Parameters) $4;
1673 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1674 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1677 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
1679 | type qualified_identifier DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1681 Parameters pars = (Parameters) $6;
1683 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1684 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1686 $$ = new IndexerDeclaration ((Expression) $1, (string) $2, pars);
1698 Location enum_location = lexer.Location;
1700 string full_name = MakeName ((string) $4);
1701 Enum e = new Enum (current_container, (Expression) $5, (int) $2, full_name,
1702 (Attributes) $1, enum_location);
1704 foreach (VariableDeclaration ev in (ArrayList) $6) {
1705 Location loc = (Location) ev.Location;
1707 CheckDef (e.AddEnumMember (ev.identifier,
1708 (Expression) ev.expression_or_array_initializer,
1709 loc, ev.OptAttributes),
1710 ev.identifier, loc);
1713 e.Namespace = current_namespace;
1715 CheckDef (current_container.AddEnum (e), full_name, enum_location);
1716 RootContext.Tree.RecordDecl (full_name, e);
1722 : /* empty */ { $$ = TypeManager.system_int32_expr; }
1723 | COLON type { $$ = $2; }
1727 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1733 opt_enum_member_declarations
1734 : /* empty */ { $$ = new ArrayList (); }
1735 | enum_member_declarations opt_comma { $$ = $1; }
1738 enum_member_declarations
1739 : enum_member_declaration
1741 ArrayList l = new ArrayList ();
1746 | enum_member_declarations COMMA enum_member_declaration
1748 ArrayList l = (ArrayList) $1;
1756 enum_member_declaration
1757 : opt_attributes IDENTIFIER
1759 $$ = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
1761 | opt_attributes IDENTIFIER
1763 $$ = lexer.Location;
1767 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
1771 delegate_declaration
1775 IDENTIFIER OPEN_PARENS
1776 opt_formal_parameter_list
1780 Location l = lexer.Location;
1781 Delegate del = new Delegate (current_container, (Expression) $4, (int) $2,
1782 MakeName ((string) $5), (Parameters) $7,
1783 (Attributes) $1, l);
1785 del.Namespace = current_namespace;
1786 CheckDef (current_container.AddDelegate (del), del.Name, l);
1791 IDENTIFIER OPEN_PARENS
1792 opt_formal_parameter_list
1796 Location l = lexer.Location;
1797 Delegate del = new Delegate (
1799 TypeManager.system_void_expr, (int) $2, MakeName ((string) $5),
1800 (Parameters) $7, (Attributes) $1, l);
1802 del.Namespace = current_namespace;
1803 CheckDef (current_container.AddDelegate (del), del.Name, l);
1808 : namespace_or_type_name
1811 namespace_or_type_name
1812 : qualified_identifier
1816 * Before you think of adding a return_type, notice that we have been
1817 * using two rules in the places where it matters (one rule using type
1818 * and another identical one that uses VOID as the return type). This
1819 * gets rid of a shift/reduce couple
1822 : type_name { /* class_type */
1824 This does interfaces, delegates, struct_types, class_types,
1825 parent classes, and more! 4.2
1827 $$ = DecomposeQI ((string) $1, lexer.Location);
1839 // Note that here only unmanaged types are allowed but we
1840 // can't perform checks during this phase - we do it during
1841 // semantic analysis.
1843 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1847 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
1853 | non_expression_type rank_specifier
1855 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1857 | non_expression_type STAR
1859 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1861 | expression rank_specifiers
1863 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1867 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1871 // We need this because the parser will happily go and reduce IDENTIFIER STAR
1872 // through this different path
1874 | multiplicative_expression STAR
1876 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1883 ArrayList types = new ArrayList ();
1888 | type_list COMMA type
1890 ArrayList types = (ArrayList) $1;
1898 * replaces all the productions for isolating the various
1899 * simple types, but we need this to reuse it easily in local_variable_type
1902 : OBJECT { $$ = TypeManager.system_object_expr; }
1903 | STRING { $$ = TypeManager.system_string_expr; }
1904 | BOOL { $$ = TypeManager.system_boolean_expr; }
1905 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
1906 | FLOAT { $$ = TypeManager.system_single_expr; }
1907 | DOUBLE { $$ = TypeManager.system_double_expr; }
1912 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
1913 | BYTE { $$ = TypeManager.system_byte_expr; }
1914 | SHORT { $$ = TypeManager.system_int16_expr; }
1915 | USHORT { $$ = TypeManager.system_uint16_expr; }
1916 | INT { $$ = TypeManager.system_int32_expr; }
1917 | UINT { $$ = TypeManager.system_uint32_expr; }
1918 | LONG { $$ = TypeManager.system_int64_expr; }
1919 | ULONG { $$ = TypeManager.system_uint64_expr; }
1920 | CHAR { $$ = TypeManager.system_char_expr; }
1921 | VOID { $$ = TypeManager.system_void_expr; }
1929 : type rank_specifiers
1931 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1936 // Expressions, section 7.5
1944 | qualified_identifier
1946 string name = (string) $1;
1949 $$ = DecomposeQI (name, lexer.Location);
1951 | parenthesized_expression
1953 | invocation_expression
1957 | post_increment_expression
1958 | post_decrement_expression
1962 | checked_expression
1963 | unchecked_expression
1964 | pointer_member_access
1971 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
1972 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
1973 | NULL { $$ = NullLiteral.Null; }
1977 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
1978 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
1979 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
1984 object v = lexer.Value;
1987 $$ = new IntLiteral ((Int32) v);
1989 $$ = new UIntLiteral ((UInt32) v);
1991 $$ = new LongLiteral ((Int64) v);
1992 else if (v is ulong)
1993 $$ = new ULongLiteral ((UInt64) v);
1995 Console.WriteLine ("OOPS. Unexpected result from scanner");
2000 : TRUE { $$ = new BoolLiteral (true); }
2001 | FALSE { $$ = new BoolLiteral (false); }
2004 parenthesized_expression
2005 : OPEN_PARENS expression CLOSE_PARENS
2010 : primary_expression DOT IDENTIFIER
2012 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2014 | predefined_type DOT IDENTIFIER
2016 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2024 invocation_expression
2025 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2028 Location l = lexer.Location;
2029 Report.Error (1, l, "Parse error");
2031 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2036 : /* empty */ { $$ = null; }
2043 ArrayList list = new ArrayList ();
2047 | argument_list COMMA argument
2049 ArrayList list = (ArrayList) $1;
2058 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2060 | REF variable_reference
2062 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2064 | OUT variable_reference
2066 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2071 : expression { note ("section 5.4"); $$ = $1; }
2075 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2077 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2079 | primary_expression rank_specifiers
2081 // So the super-trick is that primary_expression
2082 // can only be either a SimpleName or a MemberAccess.
2083 // The MemberAccess case arises when you have a fully qualified type-name like :
2085 // SimpleName is when you have
2088 Expression expr = (Expression) $1;
2089 if (expr is ComposedCast){
2090 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2091 } else if (!(expr is SimpleName || expr is MemberAccess)){
2092 Error_ExpectingTypeName (lexer.Location, expr);
2093 $$ = TypeManager.system_object_expr;
2096 // So we extract the string corresponding to the SimpleName
2099 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2107 ArrayList list = new ArrayList ();
2111 | expression_list COMMA expression
2113 ArrayList list = (ArrayList) $1;
2122 $$ = new This (current_block, lexer.Location);
2127 : BASE DOT IDENTIFIER
2129 $$ = new BaseAccess ((string) $3, lexer.Location);
2131 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2133 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2137 post_increment_expression
2138 : primary_expression OP_INC
2140 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2141 (Expression) $1, lexer.Location);
2145 post_decrement_expression
2146 : primary_expression OP_DEC
2148 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2149 (Expression) $1, lexer.Location);
2154 : object_or_delegate_creation_expression
2155 | array_creation_expression
2158 object_or_delegate_creation_expression
2159 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2161 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2165 array_creation_expression
2166 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2168 opt_array_initializer
2170 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2172 | NEW type rank_specifiers array_initializer
2174 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2178 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2194 : rank_specifier opt_rank_specifier
2196 $$ = (string) $2 + (string) $1;
2201 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2203 $$ = "[" + (string) $2 + "]";
2223 | dim_separators COMMA
2225 $$ = (string) $1 + ",";
2229 opt_array_initializer
2241 : OPEN_BRACE CLOSE_BRACE
2243 ArrayList list = new ArrayList ();
2246 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2248 $$ = (ArrayList) $2;
2252 variable_initializer_list
2253 : variable_initializer
2255 ArrayList list = new ArrayList ();
2259 | variable_initializer_list COMMA variable_initializer
2261 ArrayList list = (ArrayList) $1;
2268 : TYPEOF OPEN_PARENS type CLOSE_PARENS
2270 $$ = new TypeOf ((Expression) $3, lexer.Location);
2275 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2276 $$ = new SizeOf ((Expression) $3, lexer.Location);
2281 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2283 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2287 unchecked_expression
2288 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2290 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2294 pointer_member_access
2295 : primary_expression OP_PTR IDENTIFIER
2299 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2300 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2304 : primary_expression
2305 | BANG prefixed_unary_expression
2307 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2309 | TILDE prefixed_unary_expression
2311 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2317 : OPEN_PARENS expression CLOSE_PARENS unary_expression
2319 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2321 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2323 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2328 // The idea to split this out is from Rhys' grammar
2329 // to solve the problem with casts.
2331 prefixed_unary_expression
2333 | PLUS prefixed_unary_expression
2335 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2337 | MINUS prefixed_unary_expression
2339 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2341 | OP_INC prefixed_unary_expression
2343 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2344 (Expression) $2, lexer.Location);
2346 | OP_DEC prefixed_unary_expression
2348 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2349 (Expression) $2, lexer.Location);
2351 | STAR prefixed_unary_expression
2353 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2355 | BITWISE_AND prefixed_unary_expression
2357 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2361 pre_increment_expression
2362 : OP_INC prefixed_unary_expression
2364 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2365 (Expression) $2, lexer.Location);
2369 pre_decrement_expression
2370 : OP_DEC prefixed_unary_expression
2372 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2373 (Expression) $2, lexer.Location);
2377 multiplicative_expression
2378 : prefixed_unary_expression
2379 | multiplicative_expression STAR prefixed_unary_expression
2381 $$ = new Binary (Binary.Operator.Multiply,
2382 (Expression) $1, (Expression) $3, lexer.Location);
2384 | multiplicative_expression DIV prefixed_unary_expression
2386 $$ = new Binary (Binary.Operator.Division,
2387 (Expression) $1, (Expression) $3, lexer.Location);
2389 | multiplicative_expression PERCENT prefixed_unary_expression
2391 $$ = new Binary (Binary.Operator.Modulus,
2392 (Expression) $1, (Expression) $3, lexer.Location);
2397 : multiplicative_expression
2398 | additive_expression PLUS multiplicative_expression
2400 $$ = new Binary (Binary.Operator.Addition,
2401 (Expression) $1, (Expression) $3, lexer.Location);
2403 | additive_expression MINUS multiplicative_expression
2405 $$ = new Binary (Binary.Operator.Subtraction,
2406 (Expression) $1, (Expression) $3, lexer.Location);
2411 : additive_expression
2412 | shift_expression OP_SHIFT_LEFT additive_expression
2414 $$ = new Binary (Binary.Operator.LeftShift,
2415 (Expression) $1, (Expression) $3, lexer.Location);
2417 | shift_expression OP_SHIFT_RIGHT additive_expression
2419 $$ = new Binary (Binary.Operator.RightShift,
2420 (Expression) $1, (Expression) $3, lexer.Location);
2424 relational_expression
2426 | relational_expression OP_LT shift_expression
2428 $$ = new Binary (Binary.Operator.LessThan,
2429 (Expression) $1, (Expression) $3, lexer.Location);
2431 | relational_expression OP_GT shift_expression
2433 $$ = new Binary (Binary.Operator.GreaterThan,
2434 (Expression) $1, (Expression) $3, lexer.Location);
2436 | relational_expression OP_LE shift_expression
2438 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2439 (Expression) $1, (Expression) $3, lexer.Location);
2441 | relational_expression OP_GE shift_expression
2443 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2444 (Expression) $1, (Expression) $3, lexer.Location);
2446 | relational_expression IS type
2448 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
2450 | relational_expression AS type
2452 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
2457 : relational_expression
2458 | equality_expression OP_EQ relational_expression
2460 $$ = new Binary (Binary.Operator.Equality,
2461 (Expression) $1, (Expression) $3, lexer.Location);
2463 | equality_expression OP_NE relational_expression
2465 $$ = new Binary (Binary.Operator.Inequality,
2466 (Expression) $1, (Expression) $3, lexer.Location);
2471 : equality_expression
2472 | and_expression BITWISE_AND equality_expression
2474 $$ = new Binary (Binary.Operator.BitwiseAnd,
2475 (Expression) $1, (Expression) $3, lexer.Location);
2479 exclusive_or_expression
2481 | exclusive_or_expression CARRET and_expression
2483 $$ = new Binary (Binary.Operator.ExclusiveOr,
2484 (Expression) $1, (Expression) $3, lexer.Location);
2488 inclusive_or_expression
2489 : exclusive_or_expression
2490 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2492 $$ = new Binary (Binary.Operator.BitwiseOr,
2493 (Expression) $1, (Expression) $3, lexer.Location);
2497 conditional_and_expression
2498 : inclusive_or_expression
2499 | conditional_and_expression OP_AND inclusive_or_expression
2501 $$ = new Binary (Binary.Operator.LogicalAnd,
2502 (Expression) $1, (Expression) $3, lexer.Location);
2506 conditional_or_expression
2507 : conditional_and_expression
2508 | conditional_or_expression OP_OR conditional_and_expression
2510 $$ = new Binary (Binary.Operator.LogicalOr,
2511 (Expression) $1, (Expression) $3, lexer.Location);
2515 conditional_expression
2516 : conditional_or_expression
2517 | conditional_or_expression INTERR expression COLON expression
2519 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2523 assignment_expression
2524 : prefixed_unary_expression ASSIGN expression
2526 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2528 | prefixed_unary_expression OP_MULT_ASSIGN expression
2530 Location l = lexer.Location;
2532 $$ = new CompoundAssign (
2533 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
2535 | prefixed_unary_expression OP_DIV_ASSIGN expression
2537 Location l = lexer.Location;
2539 $$ = new CompoundAssign (
2540 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
2542 | prefixed_unary_expression OP_MOD_ASSIGN expression
2544 Location l = lexer.Location;
2546 $$ = new CompoundAssign (
2547 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
2549 | prefixed_unary_expression OP_ADD_ASSIGN expression
2551 Location l = lexer.Location;
2553 $$ = new CompoundAssign (
2554 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
2556 | prefixed_unary_expression OP_SUB_ASSIGN expression
2558 Location l = lexer.Location;
2560 $$ = new CompoundAssign (
2561 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
2563 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2565 Location l = lexer.Location;
2567 $$ = new CompoundAssign (
2568 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
2570 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2572 Location l = lexer.Location;
2574 $$ = new CompoundAssign (
2575 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
2577 | prefixed_unary_expression OP_AND_ASSIGN expression
2579 Location l = lexer.Location;
2581 $$ = new CompoundAssign (
2582 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
2584 | prefixed_unary_expression OP_OR_ASSIGN expression
2586 Location l = lexer.Location;
2588 $$ = new CompoundAssign (
2589 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
2591 | prefixed_unary_expression OP_XOR_ASSIGN expression
2593 Location l = lexer.Location;
2595 $$ = new CompoundAssign (
2596 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
2601 : conditional_expression
2602 | assignment_expression
2624 name = MakeName ((string) $4);
2626 new_class = new Class (current_container, name, (int) $2,
2627 (Attributes) $1, lexer.Location);
2628 current_container = new_class;
2629 current_container.Namespace = current_namespace;
2630 RootContext.Tree.RecordDecl (name, new_class);
2636 Class new_class = (Class) current_container;
2639 new_class.Bases = (ArrayList) $6;
2641 current_container = current_container.Parent;
2642 CheckDef (current_container.AddClass (new_class), new_class.Name, new_class.Location);
2649 : /* empty */ { $$ = (int) 0; }
2655 | modifiers modifier
2660 if ((m1 & m2) != 0) {
2661 Location l = lexer.Location;
2662 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2664 $$ = (int) (m1 | m2);
2669 : NEW { $$ = Modifiers.NEW; }
2670 | PUBLIC { $$ = Modifiers.PUBLIC; }
2671 | PROTECTED { $$ = Modifiers.PROTECTED; }
2672 | INTERNAL { $$ = Modifiers.INTERNAL; }
2673 | PRIVATE { $$ = Modifiers.PRIVATE; }
2674 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2675 | SEALED { $$ = Modifiers.SEALED; }
2676 | STATIC { $$ = Modifiers.STATIC; }
2677 | READONLY { $$ = Modifiers.READONLY; }
2678 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2679 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2680 | EXTERN { $$ = Modifiers.EXTERN; }
2681 | VOLATILE { $$ = Modifiers.VOLATILE; }
2682 | UNSAFE { $$ = Modifiers.UNSAFE; }
2686 : /* empty */ { $$ = null; }
2687 | class_base { $$ = $1; }
2691 : COLON type_list { $$ = $2; }
2699 // A block is "contained" on the following places:
2701 // property_declaration as part of the accessor body (get/set)
2702 // operator_declaration
2703 // constructor_declaration
2704 // destructor_declaration
2705 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2710 current_block = new Block (current_block, current_local_parameters,
2711 lexer.Location, Location.Null);
2713 opt_statement_list CLOSE_BRACE
2715 while (current_block.Implicit)
2716 current_block = current_block.Parent;
2718 current_block.SetEndLocation (lexer.Location);
2719 current_block = current_block.Parent;
2730 | statement_list statement
2734 : declaration_statement
2736 if ($1 != null && (Block) $1 != current_block){
2737 current_block.AddStatement ((Statement) $1);
2738 current_block = (Block) $1;
2741 | embedded_statement
2743 Statement s = (Statement) $1;
2746 current_block.AddStatement ((Statement) $1);
2754 | expression_statement
2755 | selection_statement
2756 | iteration_statement
2760 | unchecked_statement
2770 $$ = new EmptyStatement ();
2777 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
2779 if (!current_block.AddLabel ((string) $1, labeled)){
2780 Location l = lexer.Location;
2781 Report.Error (140, l, "The label '" + ((string) $1) + "' is a duplicate");
2783 current_block.AddStatement (labeled);
2788 declaration_statement
2789 : local_variable_declaration SEMICOLON
2792 DictionaryEntry de = (DictionaryEntry) $1;
2794 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
2798 | local_constant_declaration SEMICOLON
2801 DictionaryEntry de = (DictionaryEntry) $1;
2803 $$ = declare_local_constant ((Expression) de.Key, (VariableDeclaration) de.Value);
2809 * The following is from Rhys' grammar:
2810 * > Types in local variable declarations must be recognized as
2811 * > expressions to prevent reduce/reduce errors in the grammar.
2812 * > The expressions are converted into types during semantic analysis.
2815 : primary_expression opt_rank_specifier
2817 // FIXME: Do something smart here regarding the composition of the type.
2819 // Ok, the above "primary_expression" is there to get rid of
2820 // both reduce/reduce and shift/reduces in the grammar, it should
2821 // really just be "type_name". If you use type_name, a reduce/reduce
2822 // creeps up. If you use qualified_identifier (which is all we need
2823 // really) two shift/reduces appear.
2826 // So the super-trick is that primary_expression
2827 // can only be either a SimpleName or a MemberAccess.
2828 // The MemberAccess case arises when you have a fully qualified type-name like :
2830 // SimpleName is when you have
2833 Expression expr = (Expression) $1;
2834 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
2835 Error_ExpectingTypeName (lexer.Location, expr);
2839 // So we extract the string corresponding to the SimpleName
2843 if ((string) $2 == "")
2846 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2849 | builtin_types opt_rank_specifier
2851 if ((string) $2 == "")
2854 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2858 local_variable_pointer_type
2859 : primary_expression STAR
2861 Expression expr = (Expression) $1;
2862 Location l = lexer.Location;
2864 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
2865 Error_ExpectingTypeName (l, expr);
2869 $$ = new ComposedCast ((Expression) $1, "*", l);
2871 | builtin_types STAR
2873 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
2877 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
2879 | local_variable_pointer_type STAR
2881 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2885 local_variable_declaration
2886 : local_variable_type variable_declarators
2889 $$ = new DictionaryEntry ($1, $2);
2893 | local_variable_pointer_type opt_rank_specifier variable_declarators
2898 if ((string) $2 == "")
2899 t = (Expression) $1;
2901 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2902 $$ = new DictionaryEntry (t, $3);
2908 local_constant_declaration
2909 : CONST local_variable_type constant_declarator
2912 $$ = new DictionaryEntry ($2, $3);
2918 expression_statement
2919 : statement_expression SEMICOLON
2926 // We have to do the wrapping here and not in the case above,
2927 // because statement_expression is used for example in for_statement
2929 statement_expression
2930 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2931 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2932 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2933 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2934 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2935 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2936 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2938 Report.Error (1002, lexer.Location, "Expecting `;'");
2942 object_creation_expression
2943 : object_or_delegate_creation_expression
2944 { note ("complain if this is a delegate maybe?"); }
2953 : if_statement_open if_statement_rest
2962 oob_stack.Push (lexer.Location);
2967 : boolean_expression CLOSE_PARENS
2970 Location l = (Location) oob_stack.Pop ();
2972 $$ = new If ((Expression) $1, (Statement) $3, l);
2974 if (RootContext.WarningLevel >= 3){
2975 if ($3 is EmptyStatement)
2976 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
2980 | boolean_expression CLOSE_PARENS
2981 embedded_statement ELSE embedded_statement
2983 Location l = (Location) oob_stack.Pop ();
2985 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
2990 : SWITCH OPEN_PARENS
2992 oob_stack.Push (lexer.Location);
2993 switch_stack.Push (current_block);
2995 expression CLOSE_PARENS
2998 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
2999 current_block = (Block) switch_stack.Pop ();
3015 Report.Error (1522, lexer.Location, "Empty switch block");
3023 ArrayList sections = new ArrayList ();
3028 | switch_sections switch_section
3030 ArrayList sections = (ArrayList) $1;
3040 current_block = current_block.CreateSwitchBlock (lexer.Location);
3044 Block topmost = current_block;
3046 while (topmost.Implicit)
3047 topmost = topmost.Parent;
3048 $$ = new SwitchSection ((ArrayList) $1, topmost);
3055 ArrayList labels = new ArrayList ();
3060 | switch_labels switch_label
3062 ArrayList labels = (ArrayList) ($1);
3070 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3071 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3074 1523, lexer.Location,
3075 "The keyword case or default must precede code in switch block");
3089 oob_stack.Push (lexer.Location);
3091 boolean_expression CLOSE_PARENS embedded_statement
3093 Location l = (Location) oob_stack.Pop ();
3094 $$ = new While ((Expression) $4, (Statement) $6, l);
3096 if (RootContext.WarningLevel >= 3){
3097 if ($6 is EmptyStatement)
3098 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3104 : DO embedded_statement
3106 oob_stack.Push (lexer.Location);
3108 boolean_expression CLOSE_PARENS SEMICOLON
3110 Location l = (Location) oob_stack.Pop ();
3112 $$ = new Do ((Statement) $2, (Expression) $6, l);
3118 opt_for_initializer SEMICOLON
3120 Block assign_block = new Block (current_block);
3121 current_block = assign_block;
3123 if ($3 is DictionaryEntry){
3124 DictionaryEntry de = (DictionaryEntry) $3;
3126 Expression type = (Expression) de.Key;
3127 ArrayList var_declarators = (ArrayList) de.Value;
3129 foreach (VariableDeclaration decl in var_declarators){
3133 vi = current_block.AddVariable (
3134 type, decl.identifier, current_local_parameters, decl.Location);
3138 Location l = lexer.Location;
3140 if (decl.expression_or_array_initializer is Expression){
3141 expr = (Expression) decl.expression_or_array_initializer;
3142 } else if (decl.expression_or_array_initializer == null) {
3145 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3146 expr = new ArrayCreation (type, "", init, decl.Location);
3149 LocalVariableReference var;
3150 var = new LocalVariableReference (assign_block, decl.identifier, l);
3153 Assign a = new Assign (var, expr, decl.Location);
3155 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3161 oob_stack.Push (lexer.Location);
3163 opt_for_condition SEMICOLON
3164 opt_for_iterator CLOSE_PARENS
3167 Location l = (Location) oob_stack.Pop ();
3169 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3171 if (RootContext.WarningLevel >= 3){
3172 if ($10 is EmptyStatement)
3173 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3176 current_block.AddStatement (f);
3177 while (current_block.Implicit)
3178 current_block = current_block.Parent;
3180 current_block = current_block.Parent;
3185 : /* empty */ { $$ = new EmptyStatement (); }
3190 : local_variable_declaration
3191 | statement_expression_list
3195 : /* empty */ { $$ = null; }
3196 | boolean_expression
3200 : /* empty */ { $$ = new EmptyStatement (); }
3205 : statement_expression_list
3208 statement_expression_list
3209 : statement_expression
3211 // CHANGE: was `null'
3212 Block b = new Block (current_block, Block.Flags.Implicit);
3214 b.AddStatement ((Statement) $1);
3217 | statement_expression_list COMMA statement_expression
3219 Block b = (Block) $1;
3221 b.AddStatement ((Statement) $3);
3227 : FOREACH OPEN_PARENS type IDENTIFIER IN
3229 oob_stack.Push (lexer.Location);
3231 expression CLOSE_PARENS
3233 oob_stack.Push (current_block);
3235 Block foreach_block = new Block (current_block, Block.Flags.Implicit);
3236 LocalVariableReference v = null;
3237 Location l = lexer.Location;
3240 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3244 // Get a writable reference to this read-only variable.
3245 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3247 current_block = foreach_block;
3250 oob_stack.Push (current_block);
3254 Block foreach_block = (Block) oob_stack.Pop ();
3255 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3256 Block prev_block = (Block) oob_stack.Pop ();
3257 Location l = (Location) oob_stack.Pop ();
3259 current_block = prev_block;
3262 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3263 foreach_block.AddStatement (f);
3272 | continue_statement
3282 $$ = new Break (lexer.Location);
3287 : CONTINUE SEMICOLON
3289 $$ = new Continue (lexer.Location);
3294 : GOTO IDENTIFIER SEMICOLON
3296 $$ = new Goto (current_block, (string) $2, lexer.Location);
3298 | GOTO CASE constant_expression SEMICOLON
3300 $$ = new GotoCase ((Expression) $3, lexer.Location);
3302 | GOTO DEFAULT SEMICOLON
3304 $$ = new GotoDefault (lexer.Location);
3309 : RETURN opt_expression SEMICOLON
3311 $$ = new Return ((Expression) $2, lexer.Location);
3316 : THROW opt_expression SEMICOLON
3318 $$ = new Throw ((Expression) $2, lexer.Location);
3323 : YIELD expression SEMICOLON
3325 if (iterator_container == null){
3326 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3329 iterator_container.SetYields ();
3330 $$ = new Yield ((Expression) $2, lexer.Location);
3333 | YIELD BREAK SEMICOLON
3335 if (iterator_container == null){
3336 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3339 iterator_container.SetYields ();
3340 $$ = new YieldBreak (lexer.Location);
3351 : TRY block catch_clauses
3354 ArrayList s = new ArrayList ();
3356 foreach (Catch cc in (ArrayList) $3) {
3363 // Now s contains the list of specific catch clauses
3364 // and g contains the general one.
3366 $$ = new Try ((Block) $2, s, g, null, lexer.Location);
3368 | TRY block opt_catch_clauses FINALLY block
3371 ArrayList s = new ArrayList ();
3372 ArrayList catch_list = (ArrayList) $3;
3374 if (catch_list != null){
3375 foreach (Catch cc in catch_list) {
3383 $$ = new Try ((Block) $2, s, g, (Block) $5, lexer.Location);
3387 Report.Error (1524, lexer.Location, "Expected catch or finally");
3392 : /* empty */ { $$ = null; }
3399 ArrayList l = new ArrayList ();
3404 | catch_clauses catch_clause
3406 ArrayList l = (ArrayList) $1;
3414 : /* empty */ { $$ = null; }
3419 : CATCH opt_catch_args
3421 Expression type = null;
3425 DictionaryEntry cc = (DictionaryEntry) $2;
3426 type = (Expression) cc.Key;
3427 id = (string) cc.Value;
3430 ArrayList one = new ArrayList ();
3431 Location loc = lexer.Location;
3433 one.Add (new VariableDeclaration (id, null, loc));
3436 current_block = new Block (current_block);
3437 Block b = declare_local_variables (type, one, loc);
3442 Expression type = null;
3446 DictionaryEntry cc = (DictionaryEntry) $2;
3447 type = (Expression) cc.Key;
3448 id = (string) cc.Value;
3452 // FIXME: I can change this for an assignment.
3454 while (current_block != (Block) $1)
3455 current_block = current_block.Parent;
3460 $$ = new Catch (type, id , (Block) $4, lexer.Location);
3465 : /* empty */ { $$ = null; }
3470 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3472 $$ = new DictionaryEntry ($2, $3);
3479 $$ = new Checked ((Block) $2);
3486 $$ = new Unchecked ((Block) $2);
3493 if (!RootContext.Unsafe){
3494 Report.Error (227, lexer.Location,
3495 "Unsafe code can only be used if --unsafe is used");
3498 $$ = new Unsafe ((Block) $3);
3504 type fixed_pointer_declarators
3507 Block assign_block = new Block (current_block, Block.Flags.Implicit);
3508 ArrayList list = (ArrayList) $4;
3509 Expression type = (Expression) $3;
3510 Location l = lexer.Location;
3511 int top = list.Count;
3513 for (int i = 0; i < top; i++){
3514 Pair p = (Pair) list [i];
3517 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3524 current_block.AddStatement (assign_block);
3525 current_block = assign_block;
3526 oob_stack.Push (assign_block);
3531 Location l = (Location) oob_stack.Pop ();
3532 Block assign_block = (Block) oob_stack.Pop ();
3534 ArrayList list = (ArrayList) $4;
3535 int top = list.Count;
3537 $$ = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
3541 fixed_pointer_declarators
3542 : fixed_pointer_declarator {
3543 ArrayList declarators = new ArrayList ();
3544 declarators.Add ($1);
3547 | fixed_pointer_declarators COMMA fixed_pointer_declarator
3549 ArrayList declarators = (ArrayList) $1;
3550 declarators.Add ($3);
3555 fixed_pointer_declarator
3556 : IDENTIFIER ASSIGN expression
3558 $$ = new Pair ($1, $3);
3563 : LOCK OPEN_PARENS expression CLOSE_PARENS
3569 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
3574 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
3576 Block assign_block = new Block (current_block);
3577 current_block = assign_block;
3579 oob_stack.Push (lexer.Location);
3581 if ($3 is DictionaryEntry){
3582 DictionaryEntry de = (DictionaryEntry) $3;
3583 Location l = lexer.Location;
3585 Expression type = (Expression) de.Key;
3586 ArrayList var_declarators = (ArrayList) de.Value;
3588 ArrayList vars = new ArrayList ();
3590 foreach (VariableDeclaration decl in var_declarators){
3592 LocalInfo vi = current_block.AddVariable (
3593 type, decl.identifier,
3594 current_local_parameters, decl.Location);
3600 if (decl.expression_or_array_initializer is Expression){
3601 expr = (Expression) decl.expression_or_array_initializer;
3603 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3605 expr = new ArrayCreation (type, "", init, decl.Location);
3608 LocalVariableReference var;
3610 // Get a writable reference to this read-only variable.
3611 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
3613 // This is so that it is not a warning on using variables
3616 vars.Add (new DictionaryEntry (var, expr));
3618 // Assign a = new Assign (var, expr, decl.Location);
3619 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3621 $3 = new DictionaryEntry (type, vars);
3626 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
3627 current_block.AddStatement (u);
3628 while (current_block.Implicit)
3629 current_block = current_block.Parent;
3631 current_block = current_block.Parent;
3635 resource_acquisition
3636 : local_variable_declaration
3643 // A class used to pass around variable declarations and constants
3645 public class VariableDeclaration {
3646 public string identifier;
3647 public object expression_or_array_initializer;
3648 public Location Location;
3649 public Attributes OptAttributes;
3651 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
3653 this.identifier = id;
3654 this.expression_or_array_initializer = eoai;
3656 this.OptAttributes = opt_attrs;
3659 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
3665 // A class used to hold info about an indexer declarator
3668 public class IndexerDeclaration {
3669 public Expression type;
3670 public string interface_type;
3671 public Parameters param_list;
3673 public IndexerDeclaration (Expression type, string interface_type, Parameters param_list)
3676 this.interface_type = interface_type;
3677 this.param_list = param_list;
3682 // A class used to hold info about an operator declarator
3685 public class OperatorDeclaration {
3686 public Operator.OpType optype;
3687 public Expression ret_type, arg1type, arg2type;
3688 public string arg1name, arg2name;
3689 public Location location;
3691 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
3692 Expression arg1type, string arg1name,
3693 Expression arg2type, string arg2name, Location location)
3696 this.ret_type = ret_type;
3697 this.arg1type = arg1type;
3698 this.arg1name = arg1name;
3699 this.arg2type = arg2type;
3700 this.arg2name = arg2name;
3701 this.location = location;
3706 void Error_ExpectingTypeName (Location l, Expression expr)
3708 if (expr is Invocation){
3709 Report.Error (1002, l, "; expected");
3711 Report.Error (-1, l, "Invalid Type definition");
3716 // Given the @class_name name, it creates a fully qualified name
3717 // based on the containing declaration space
3720 MakeName (string class_name)
3722 string ns = current_namespace.Name;
3723 string container_name = current_container.Name;
3725 if (container_name == ""){
3727 return ns + "." + class_name;
3731 return container_name + "." + class_name;
3735 // Used to report back to the user the result of a declaration
3736 // in the current declaration space
3739 CheckDef (AdditionResult result, string name, Location l)
3741 if (result == AdditionResult.Success)
3745 case AdditionResult.NameExists:
3746 Report.Error (102, l, "The container `" + current_container.Name +
3747 "' already contains a definition for `"+
3753 // This is handled only for static Constructors, because
3754 // in reality we handle these by the semantic analysis later
3756 case AdditionResult.MethodExists:
3758 111, l, "Class `"+current_container.Name+
3759 "' already defines a member called '" +
3760 name + "' with the same parameter types (more than one default constructor)");
3763 case AdditionResult.EnclosingClash:
3764 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
3767 case AdditionResult.NotAConstructor:
3768 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
3774 CheckDef (bool result, string name, Location l)
3778 CheckDef (AdditionResult.NameExists, name, l);
3781 Expression DecomposeQI (string name, Location loc)
3785 if (name.IndexOf ('.') == -1){
3786 return new SimpleName (name, loc);
3788 int pos = name.LastIndexOf (".");
3789 string left = name.Substring (0, pos);
3790 string right = name.Substring (pos + 1);
3792 o = DecomposeQI (left, loc);
3794 return new MemberAccess (o, right, loc);
3799 // This method is used to get at the complete string representation of
3800 // a fully-qualified type name, hiding inside a MemberAccess ;-)
3801 // This is necessary because local_variable_type admits primary_expression
3802 // as the type of the variable. So we do some extra checking
3805 string GetQualifiedIdentifier (Expression expr)
3807 if (expr is SimpleName)
3808 return ((SimpleName)expr).Name;
3809 else if (expr is MemberAccess)
3810 return GetQualifiedIdentifier (((MemberAccess)expr).Expr) + "." + ((MemberAccess) expr).Identifier;
3812 throw new Exception ("Expr has to be either SimpleName or MemberAccess! (" + expr + ")");
3817 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
3819 Block implicit_block;
3820 ArrayList inits = null;
3823 // We use the `Used' property to check whether statements
3824 // have been added to the current block. If so, we need
3825 // to create another block to contain the new declaration
3826 // otherwise, as an optimization, we use the same block to
3827 // add the declaration.
3829 // FIXME: A further optimization is to check if the statements
3830 // that were added were added as part of the initialization
3831 // below. In which case, no other statements have been executed
3832 // and we might be able to reduce the number of blocks for
3833 // situations like this:
3835 // int j = 1; int k = j + 1;
3837 if (current_block.Used) {
3838 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
3839 implicit_block.AddChildVariableNames (current_block);
3841 implicit_block = current_block;
3843 foreach (VariableDeclaration decl in variable_declarators){
3845 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
3846 if (decl.expression_or_array_initializer != null){
3848 inits = new ArrayList ();
3855 return implicit_block;
3857 foreach (VariableDeclaration decl in inits){
3861 if (decl.expression_or_array_initializer is Expression){
3862 expr = (Expression) decl.expression_or_array_initializer;
3865 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3867 expr = new ArrayCreation (type, "", init, decl.Location);
3870 LocalVariableReference var;
3871 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
3873 assign = new Assign (var, expr, decl.Location);
3875 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
3878 return implicit_block;
3881 Block declare_local_constant (Expression type, VariableDeclaration decl)
3883 Block implicit_block;
3885 if (current_block.Used)
3886 implicit_block = new Block (current_block, Block.Flags.Implicit);
3888 implicit_block = current_block;
3890 if (!(implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
3891 current_local_parameters, decl.Location))){
3894 return implicit_block;
3897 void CheckAttributeTarget (string a)
3901 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
3905 Location l = lexer.Location;
3906 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
3912 void CheckUnaryOperator (Operator.OpType op)
3916 case Operator.OpType.LogicalNot:
3917 case Operator.OpType.OnesComplement:
3918 case Operator.OpType.Increment:
3919 case Operator.OpType.Decrement:
3920 case Operator.OpType.True:
3921 case Operator.OpType.False:
3922 case Operator.OpType.Addition:
3923 case Operator.OpType.Subtraction:
3928 Location l = lexer.Location;
3929 Report.Error (1019, l, "Overloadable unary operator expected");
3935 void CheckBinaryOperator (Operator.OpType op)
3939 case Operator.OpType.Addition:
3940 case Operator.OpType.Subtraction:
3941 case Operator.OpType.Multiply:
3942 case Operator.OpType.Division:
3943 case Operator.OpType.Modulus:
3944 case Operator.OpType.BitwiseAnd:
3945 case Operator.OpType.BitwiseOr:
3946 case Operator.OpType.ExclusiveOr:
3947 case Operator.OpType.LeftShift:
3948 case Operator.OpType.RightShift:
3949 case Operator.OpType.Equality:
3950 case Operator.OpType.Inequality:
3951 case Operator.OpType.GreaterThan:
3952 case Operator.OpType.LessThan:
3953 case Operator.OpType.GreaterThanOrEqual:
3954 case Operator.OpType.LessThanOrEqual:
3958 Location l = lexer.Location;
3959 Report.Error (1020, l, "Overloadable binary operator expected");
3965 void syntax_error (Location l, string msg)
3967 Report.Error (1003, l, "Syntax error, " + msg);
3970 void output (string s)
3972 Console.WriteLine (s);
3975 void note (string s)
3977 // Used to put annotations
3982 public Tokenizer Lexer {
3988 public CSharpParser (StreamReader reader, SourceFile file, ArrayList defines)
3990 current_namespace = new Namespace (null, file, "");
3991 this.name = file.Name;
3993 current_container = RootContext.Tree.Types;
3994 current_container.Namespace = current_namespace;
3995 oob_stack = new Stack ();
3996 switch_stack = new Stack ();
3998 lexer = new Tokenizer (reader, file, defines);
4001 public override void parse ()
4004 if (yacc_verbose_flag)
4005 yyparse (lexer, new yydebug.yyDebugSimple ());
4008 Tokenizer tokenizer = lexer as Tokenizer;
4009 tokenizer.cleanup ();
4010 } catch (Exception e){
4011 // Please do not remove this, it is used during debugging
4014 Report.Error (-25, lexer.Location, ": Parsing error ");
4015 Console.WriteLine (e);