3 // cs-parser.jay: The Parser for the C# compiler
5 // Authors: Miguel de Icaza (miguel@gnu.org)
6 // Ravi Pratap (ravi@ximian.com)
8 // Licensed under the terms of the GNU GPL
10 // (C) 2001 Ximian, Inc (http://www.ximian.com)
13 // (1) Figure out why error productions dont work. `type-declaration' is a
14 // great spot to put an `error' because you can reproduce it with this input:
24 using System.Collections;
30 public class CSharpParser : GenericParser {
31 Namespace current_namespace;
32 TypeContainer current_container;
35 // Current block is used to add statements as we find
42 // Current interface is used by the various declaration
43 // productions in the interface declaration to "add"
44 // the interfaces as we find them.
46 Interface current_interface;
49 // This is used by the unary_expression code to resolve
50 // a name against a parameter.
52 Parameters current_local_parameters;
55 // Using during property parsing to describe the implicit
56 // value parameter that is passed to the "set" and "get"accesor
57 // methods (properties and indexers).
59 Expression implicit_value_parameter_type;
60 Parameters indexer_parameters;
63 // Used to determine if we are parsing the get/set pair
64 // of an indexer or a property
69 // An out-of-band stack.
80 %token NONE /* This token is never returned by our lexer */
81 %token ERROR // This is used not by the parser, but by the tokenizer.
85 *These are the C# keywords
168 /* C# keywords which are not really keywords */
172 /* C# single character operators/punctuation. */
173 %token OPEN_BRACE "{"
174 %token CLOSE_BRACE "}"
175 %token OPEN_BRACKET "["
176 %token CLOSE_BRACKET "]"
177 %token OPEN_PARENS "("
178 %token CLOSE_PARENS ")"
191 %token BITWISE_AND "&"
192 %token BITWISE_OR "|"
199 /* C# multi-character operators. */
202 %token OP_SHIFT_LEFT "<<"
203 %token OP_SHIFT_RIGHT ">>"
210 %token OP_MULT_ASSIGN "*="
211 %token OP_DIV_ASSIGN "/="
212 %token OP_MOD_ASSIGN "%="
213 %token OP_ADD_ASSIGN "+="
214 %token OP_SUB_ASSIGN "-="
215 %token OP_SHIFT_LEFT_ASSIGN "<<="
216 %token OP_SHIFT_RIGHT_ASSIGN ">>="
217 %token OP_AND_ASSIGN "&="
218 %token OP_XOR_ASSIGN "^="
219 %token OP_OR_ASSIGN "|="
223 %token LITERAL_INTEGER "int literal"
224 %token LITERAL_FLOAT "float literal"
225 %token LITERAL_DOUBLE "double literal"
226 %token LITERAL_DECIMAL "decimal literal"
227 %token LITERAL_CHARACTER "character literal"
228 %token LITERAL_STRING "string literal"
232 /* Add precedence rules to solve dangling else s/r conflict */
241 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
243 %left STAR DIV PERCENT
244 %right BANG CARRET UMINUS
245 %nonassoc OP_INC OP_DEC
247 %left OPEN_BRACKET OPEN_BRACE
251 %start compilation_unit
255 : outer_declarations opt_EOF
256 | outer_declarations attribute_sections opt_EOF
257 | attribute_sections opt_EOF
267 | outer_declarations outer_declaration
272 | namespace_member_declaration
277 | using_directives using_directive
281 : using_alias_directive
282 | using_namespace_directive
285 using_alias_directive
286 : USING IDENTIFIER ASSIGN
287 namespace_or_type_name SEMICOLON
289 current_namespace.UsingAlias ((string) $2, (string) $4, lexer.Location);
293 using_namespace_directive
294 : USING namespace_name SEMICOLON
296 current_namespace.Using ((string) $2, lexer.Location);
301 // Strictly speaking, namespaces don't have attributes but
302 // we parse global attributes along with namespace declarations and then
305 namespace_declaration
306 : opt_attributes NAMESPACE qualified_identifier
308 Attributes attrs = (Attributes) $1;
311 foreach (AttributeSection asec in attrs.AttributeSections)
312 if (asec.Target == "assembly")
313 RootContext.AddGlobalAttribute (
314 current_container, asec, lexer.Location);
317 current_namespace = RootContext.Tree.RecordNamespace (current_namespace, name, (string) $3);
319 namespace_body opt_semicolon
321 current_namespace = current_namespace.Parent;
337 | qualified_identifier DOT IDENTIFIER {
338 $$ = (($1).ToString ()) + "." + ($3.ToString ()); }
343 : namespace_or_type_name
349 opt_namespace_member_declarations
360 opt_namespace_member_declarations
362 | namespace_member_declarations
365 namespace_member_declarations
366 : namespace_member_declaration
367 | namespace_member_declarations namespace_member_declaration
370 namespace_member_declaration
377 Class c = (Class) $1;
378 mod_flags = c.ModFlags;
380 } else if ($1 is Struct){
381 Struct s = (Struct) $1;
382 mod_flags = s.ModFlags;
387 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
389 1527, lexer.Location,
390 "Namespace elements cant be explicitly " +
391 "declared private or protected in `" + name + "'");
393 current_namespace.DeclarationFound = true;
395 | namespace_declaration
401 | interface_declaration
403 | delegate_declaration
405 // Enable this when we have handled all errors, because this acts as a generic fallback
408 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
418 | attribute_sections { $$ = $1; }
424 AttributeSection sect = (AttributeSection) $1;
426 if (sect.Target == "assembly")
427 RootContext.AddGlobalAttribute (current_container, sect, lexer.Location);
429 $$ = new Attributes ((AttributeSection) $1, lexer.Location);
431 | attribute_sections attribute_section
433 Attributes attrs = null;
434 AttributeSection sect = (AttributeSection) $2;
436 if (sect.Target == "assembly")
437 RootContext.AddGlobalAttribute (current_container, sect, lexer.Location);
440 attrs = (Attributes) $1;
441 attrs.AddAttribute (sect);
449 : OPEN_BRACKET attribute_target_specifier attribute_list CLOSE_BRACKET
451 string target = null;
454 target = (string) $2;
456 $$ = new AttributeSection (target, (ArrayList) $3);
458 | OPEN_BRACKET attribute_list CLOSE_BRACKET
460 $$ = new AttributeSection (null, (ArrayList) $2);
464 attribute_target_specifier
465 : attribute_target COLON
474 CheckAttributeTarget ((string) $1);
477 | EVENT { $$ = "event"; }
478 | RETURN { $$ = "return"; }
484 ArrayList attrs = new ArrayList ();
490 | attribute_list COMMA attribute
492 ArrayList attrs = (ArrayList) $1;
504 opt_attribute_arguments
506 $$ = new Attribute ((string) $1, (ArrayList) $3, (Location) $2);
511 : type_name { /* reserved attribute name or identifier: 17.4 */ }
514 opt_attribute_arguments
515 : /* empty */ { $$ = null; }
516 | OPEN_PARENS attribute_arguments CLOSE_PARENS
524 : opt_positional_argument_list
529 ArrayList args = new ArrayList ();
535 | positional_argument_list COMMA named_argument_list
537 ArrayList args = new ArrayList ();
543 | named_argument_list
545 ArrayList args = new ArrayList ();
554 opt_positional_argument_list
555 : /* empty */ { $$ = null; }
556 | positional_argument_list
559 positional_argument_list
562 ArrayList args = new ArrayList ();
563 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
567 | positional_argument_list COMMA expression
569 ArrayList args = (ArrayList) $1;
570 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
579 ArrayList args = new ArrayList ();
584 | named_argument_list COMMA named_argument
586 ArrayList args = (ArrayList) $1;
594 : IDENTIFIER ASSIGN expression
596 $$ = new DictionaryEntry (
598 new Argument ((Expression) $3, Argument.AType.Expression));
604 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
607 opt_class_member_declarations
609 | class_member_declarations
612 class_member_declarations
613 : class_member_declaration
614 | class_member_declarations
615 class_member_declaration
618 class_member_declaration
619 : constant_declaration // done
620 | field_declaration // done
621 | method_declaration // done
622 | property_declaration // done
623 | event_declaration // done
624 | indexer_declaration // done
625 | operator_declaration // done
626 | constructor_declaration // done
627 | destructor_declaration // done
637 string full_struct_name = MakeName ((string) $4);
639 new_struct = new Struct (current_container, full_struct_name, (int) $2,
640 (Attributes) $1, lexer.Location);
641 current_container = new_struct;
642 current_container.Namespace = current_namespace;
643 RootContext.Tree.RecordDecl (full_struct_name, new_struct);
649 Struct new_struct = (Struct) current_container;
652 new_struct.Bases = (ArrayList) $6;
654 current_container = current_container.Parent;
655 CheckDef (current_container.AddStruct (new_struct), new_struct.Name, new_struct.Location);
661 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
664 opt_struct_member_declarations
666 | struct_member_declarations
669 struct_member_declarations
670 : struct_member_declaration
671 | struct_member_declarations struct_member_declaration
674 struct_member_declaration
675 : constant_declaration
678 | property_declaration
680 | indexer_declaration
681 | operator_declaration
682 | constructor_declaration
686 * This is only included so we can flag error 575:
687 * destructors only allowed on class types
689 | destructor_declaration
700 foreach (VariableDeclaration constant in (ArrayList) $5){
701 Location l = constant.Location;
703 Const c = new Const (
704 (Expression) $4, (string) constant.identifier,
705 (Expression) constant.expression_or_array_initializer, (int) $2,
708 CheckDef (current_container.AddConstant (c), c.Name, l);
714 : constant_declarator
716 ArrayList constants = new ArrayList ();
720 | constant_declarators COMMA constant_declarator
722 ArrayList constants = (ArrayList) $1;
729 : IDENTIFIER ASSIGN constant_expression
731 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
742 Expression type = (Expression) $3;
745 foreach (VariableDeclaration var in (ArrayList) $4){
746 Location l = var.Location;
748 Field field = new Field (type, mod, var.identifier,
749 var.expression_or_array_initializer,
752 CheckDef (current_container.AddField (field), field.Name, l);
758 : variable_declarator
760 ArrayList decl = new ArrayList ();
764 | variable_declarators COMMA variable_declarator
766 ArrayList decls = (ArrayList) $1;
773 : IDENTIFIER ASSIGN variable_initializer
775 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
779 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
792 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
794 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
802 Method method = (Method) $1;
803 Block b = (Block) $2;
804 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
807 if ((method.ModFlags & extern_abstract) == 0){
809 501, lexer.Location, current_container.MakeName (method.Name) +
810 "must declare a body because it is not marked abstract or extern");
813 if ((method.ModFlags & Modifiers.EXTERN) != 0){
815 179, lexer.Location, current_container.MakeName (method.Name) +
816 " is declared extern, but has a body");
820 method.Block = (Block) $2;
821 CheckDef (current_container.AddMethod (method), method.Name, method.Location);
823 current_local_parameters = null;
837 1585, lexer.Location, "Member modifier `" +
838 Modifiers.Name (i) + "' must precede member type and name");
851 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
853 Method method = new Method ((Expression) $3, (int) $2, (string) $4,
854 (Parameters) $6, (Attributes) $1, lexer.Location);
856 current_local_parameters = (Parameters) $6;
864 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
866 Method method = new Method (TypeManager.system_void_expr, (int) $2, (string) $4,
867 (Parameters) $6, (Attributes) $1, lexer.Location);
869 current_local_parameters = (Parameters) $6;
876 | SEMICOLON { $$ = null; }
879 opt_formal_parameter_list
880 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
881 | formal_parameter_list
884 formal_parameter_list
887 ArrayList pars_list = (ArrayList) $1;
889 Parameter [] pars = new Parameter [pars_list.Count];
890 pars_list.CopyTo (pars);
892 $$ = new Parameters (pars, null, lexer.Location);
894 | fixed_parameters COMMA parameter_array
896 ArrayList pars_list = (ArrayList) $1;
898 Parameter [] pars = new Parameter [pars_list.Count];
899 pars_list.CopyTo (pars);
901 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
905 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
912 ArrayList pars = new ArrayList ();
917 | fixed_parameters COMMA fixed_parameter
919 ArrayList pars = (ArrayList) $1;
928 opt_parameter_modifier
932 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
936 opt_parameter_modifier
937 : /* empty */ { $$ = Parameter.Modifier.NONE; }
942 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
943 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
947 : opt_attributes PARAMS type IDENTIFIER
949 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
950 note ("type must be a single-dimension array type");
955 : qualified_identifier
964 implicit_value_parameter_type = (Expression) $3;
966 lexer.PropertyParsing = true;
970 accessor_declarations
972 lexer.PropertyParsing = false;
977 Pair pair = (Pair) $7;
978 Accessor get_block = (Accessor) pair.First;
979 Accessor set_block = (Accessor) pair.Second;
981 Location loc = (Location) $6;
982 prop = new Property ((Expression) $3, (string) $4, (int) $2, get_block, set_block,
983 (Attributes) $1, loc);
985 CheckDef (current_container.AddProperty (prop), prop.Name, loc);
986 implicit_value_parameter_type = null;
990 accessor_declarations
991 : get_accessor_declaration opt_set_accessor_declaration
993 $$ = new Pair ($1, $2);
995 | set_accessor_declaration opt_get_accessor_declaration
997 $$ = new Pair ($2, $1);
1001 opt_get_accessor_declaration
1002 : /* empty */ { $$ = null; }
1003 | get_accessor_declaration
1006 opt_set_accessor_declaration
1007 : /* empty */ { $$ = null; }
1008 | set_accessor_declaration
1011 get_accessor_declaration
1012 : opt_attributes GET
1014 // If this is not the case, then current_local_parameters has already
1015 // been set in indexer_declaration
1016 if (parsing_indexer == false)
1017 current_local_parameters = null;
1019 current_local_parameters = indexer_parameters;
1020 lexer.PropertyParsing = false;
1024 $$ = new Accessor ((Block) $4, (Attributes) $1);
1025 current_local_parameters = null;
1026 lexer.PropertyParsing = true;
1030 set_accessor_declaration
1031 : opt_attributes SET
1034 Parameter implicit_value_parameter = new Parameter (
1035 implicit_value_parameter_type, "value",
1036 Parameter.Modifier.NONE, null);
1038 if (parsing_indexer == false) {
1039 args = new Parameter [1];
1040 args [0] = implicit_value_parameter;
1041 current_local_parameters = new Parameters (args, null, lexer.Location);
1043 Parameter [] fpars = indexer_parameters.FixedParameters;
1046 int count = fpars.Length;
1048 args = new Parameter [count + 1];
1049 fpars.CopyTo (args, 0);
1050 args [count] = implicit_value_parameter;
1053 current_local_parameters = new Parameters (
1054 args, indexer_parameters.ArrayParameter, lexer.Location);
1057 lexer.PropertyParsing = false;
1061 $$ = new Accessor ((Block) $4, (Attributes) $1);
1062 current_local_parameters = null;
1063 lexer.PropertyParsing = true;
1069 | SEMICOLON { $$ = null; }
1072 interface_declaration
1075 INTERFACE IDENTIFIER
1077 Interface new_interface;
1078 string full_interface_name = MakeName ((string) $4);
1080 new_interface = new Interface (current_container, full_interface_name, (int) $2,
1081 (Attributes) $1, lexer.Location);
1082 if (current_interface != null) {
1083 Location l = lexer.Location;
1084 Report.Error (-2, l, "Internal compiler error: interface inside interface");
1086 current_interface = new_interface;
1087 new_interface.Namespace = current_namespace;
1088 RootContext.Tree.RecordDecl (full_interface_name, new_interface);
1091 interface_body opt_semicolon
1093 Interface new_interface = (Interface) current_interface;
1096 new_interface.Bases = (ArrayList) $6;
1098 current_interface = null;
1099 CheckDef (current_container.AddInterface (new_interface),
1100 new_interface.Name, new_interface.Location);
1105 : /* empty */ { $$ = null; }
1110 : COLON interface_type_list { $$ = $2; }
1116 ArrayList interfaces = new ArrayList ();
1118 interfaces.Add ($1);
1121 | interface_type_list COMMA interface_type
1123 ArrayList interfaces = (ArrayList) $1;
1124 interfaces.Add ($3);
1131 opt_interface_member_declarations
1135 opt_interface_member_declarations
1137 | interface_member_declarations
1140 interface_member_declarations
1141 : interface_member_declaration
1142 | interface_member_declarations interface_member_declaration
1145 interface_member_declaration
1146 : interface_method_declaration
1148 InterfaceMethod m = (InterfaceMethod) $1;
1150 CheckDef (current_interface.AddMethod (m), m.Name, m.Location);
1152 | interface_property_declaration
1154 InterfaceProperty p = (InterfaceProperty) $1;
1156 CheckDef (current_interface.AddProperty (p), p.Name, p.Location);
1158 | interface_event_declaration
1160 InterfaceEvent e = (InterfaceEvent) $1;
1162 CheckDef (current_interface.AddEvent (e), e.Name, lexer.Location);
1164 | interface_indexer_declaration
1166 InterfaceIndexer i = (InterfaceIndexer) $1;
1168 CheckDef (current_interface.AddIndexer (i), "indexer", i.Location);
1173 : /* empty */ { $$ = false; }
1174 | NEW { $$ = true; }
1177 interface_method_declaration
1178 : opt_attributes opt_new type IDENTIFIER
1179 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1182 $$ = new InterfaceMethod ((Expression) $3, (string) $4, (bool) $2,
1183 (Parameters) $6, (Attributes) $1, lexer.Location);
1185 | opt_attributes opt_new VOID IDENTIFIER
1186 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1189 $$ = new InterfaceMethod (
1190 TypeManager.system_void_expr, (string) $4, (bool) $2, (Parameters) $6,
1191 (Attributes) $1, lexer.Location);
1195 interface_property_declaration
1200 { lexer.PropertyParsing = true; }
1202 { lexer.PropertyParsing = false; }
1207 $$ = new InterfaceProperty ((Expression) $3, (string) $4, (bool) $2,
1208 (gs & 1) == 1, (gs & 2) == 2, (Attributes) $1,
1214 : opt_attributes GET SEMICOLON { $$ = 1; }
1215 | opt_attributes SET SEMICOLON { $$ = 2; }
1216 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1218 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1222 interface_event_declaration
1223 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1225 $$ = new InterfaceEvent ((Expression) $4, (string) $5, (bool) $2, (Attributes) $1,
1230 interface_indexer_declaration
1231 : opt_attributes opt_new type THIS
1232 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1234 { lexer.PropertyParsing = true; }
1236 { lexer.PropertyParsing = false; }
1239 int a_flags = (int) $10;
1241 bool do_get = (a_flags & 1) == 1;
1242 bool do_set = (a_flags & 2) == 2;
1244 $$ = new InterfaceIndexer ((Expression) $3, (Parameters) $6, do_get, do_set,
1245 (bool) $2, (Attributes) $1, lexer.Location);
1249 operator_declaration
1250 : opt_attributes opt_modifiers operator_declarator operator_body
1252 OperatorDeclaration decl = (OperatorDeclaration) $3;
1254 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
1255 decl.arg2type, decl.arg2name, (Block) $4, (Attributes) $1, decl.location);
1257 // Note again, checking is done in semantic analysis
1258 current_container.AddOperator (op);
1260 current_local_parameters = null;
1266 | SEMICOLON { $$ = null; }
1269 : type OPERATOR overloadable_operator
1270 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1272 Operator.OpType op = (Operator.OpType) $3;
1273 CheckUnaryOperator (op);
1275 if (op == Operator.OpType.Addition)
1276 op = Operator.OpType.UnaryPlus;
1278 if (op == Operator.OpType.Subtraction)
1279 op = Operator.OpType.UnaryNegation;
1281 Parameter [] pars = new Parameter [1];
1283 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1285 current_local_parameters = new Parameters (pars, null, lexer.Location);
1287 $$ = new OperatorDeclaration (op, (Expression) $1, (Expression) $5, (string) $6,
1288 null, null, lexer.Location);
1290 | type OPERATOR overloadable_operator
1292 type IDENTIFIER COMMA
1296 CheckBinaryOperator ((Operator.OpType) $3);
1298 Parameter [] pars = new Parameter [2];
1300 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1301 pars [1] = new Parameter ((Expression) $8, (string) $9, Parameter.Modifier.NONE, null);
1303 current_local_parameters = new Parameters (pars, null, lexer.Location);
1305 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1306 (Expression) $5, (string) $6,
1307 (Expression) $8, (string) $9, lexer.Location);
1309 | conversion_operator_declarator
1312 overloadable_operator
1314 : BANG { $$ = Operator.OpType.LogicalNot; }
1315 | TILDE { $$ = Operator.OpType.OnesComplement; }
1316 | OP_INC { $$ = Operator.OpType.Increment; }
1317 | OP_DEC { $$ = Operator.OpType.Decrement; }
1318 | TRUE { $$ = Operator.OpType.True; }
1319 | FALSE { $$ = Operator.OpType.False; }
1320 // Unary and binary:
1321 | PLUS { $$ = Operator.OpType.Addition; }
1322 | MINUS { $$ = Operator.OpType.Subtraction; }
1324 | STAR { $$ = Operator.OpType.Multiply; }
1325 | DIV { $$ = Operator.OpType.Division; }
1326 | PERCENT { $$ = Operator.OpType.Modulus; }
1327 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1328 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1329 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1330 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1331 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1332 | OP_EQ { $$ = Operator.OpType.Equality; }
1333 | OP_NE { $$ = Operator.OpType.Inequality; }
1334 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1335 | OP_LT { $$ = Operator.OpType.LessThan; }
1336 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1337 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1340 conversion_operator_declarator
1341 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1343 Parameter [] pars = new Parameter [1];
1345 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1347 current_local_parameters = new Parameters (pars, null, lexer.Location);
1349 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1350 null, null, lexer.Location);
1352 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1354 Parameter [] pars = new Parameter [1];
1356 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1358 current_local_parameters = new Parameters (pars, null, lexer.Location);
1360 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1361 null, null, lexer.Location);
1365 syntax_error (lexer.Location, "'operator' expected");
1369 syntax_error (lexer.Location, "'operator' expected");
1373 constructor_declaration
1376 constructor_declarator
1379 Constructor c = (Constructor) $3;
1380 c.Block = (Block) $4;
1381 c.ModFlags = (int) $2;
1382 c.OptAttributes = (Attributes) $1;
1384 if ((c.ModFlags & Modifiers.STATIC) != 0){
1385 if ((c.ModFlags & Modifiers.Accessibility) != 0) {
1388 "Access modifiers are not allowed on static constructors");
1391 if (c.Initializer != null){
1394 "Static constructors can not have an explicit this or base " +
1395 "constructor invocations");
1398 if (!c.Parameters.Empty){
1400 132, c.Location, "Static constructors should not have parameters");
1404 CheckDef (current_container.AddConstructor (c), c.Name, c.Location);
1406 current_local_parameters = null;
1410 constructor_declarator
1412 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1414 oob_stack.Push (lexer.Location);
1416 current_local_parameters = (Parameters) $3;
1418 opt_constructor_initializer
1420 Location l = (Location) oob_stack.Pop ();
1421 $$ = new Constructor ((string) $1, (Parameters) $3, (ConstructorInitializer) $6, l);
1427 | SEMICOLON { $$ = null; }
1430 opt_constructor_initializer
1431 : /* empty */ { $$ = null; }
1432 | constructor_initializer
1435 constructor_initializer
1436 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1438 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1440 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1442 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1446 destructor_declaration
1447 : opt_attributes TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1449 if ((string) $3 != current_container.Basename){
1450 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1451 } else if (!(current_container is Class)){
1452 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1454 Location l = lexer.Location;
1457 if (!RootContext.StdLib && current_container.Name == "System.Object")
1458 m = Modifiers.PROTECTED | Modifiers.VIRTUAL;
1460 m = Modifiers.PROTECTED | Modifiers.OVERRIDE;
1462 Method d = new Method (
1463 TypeManager.system_void_expr, m, "Finalize",
1464 new Parameters (null, null, l), (Attributes) $1, l);
1466 d.Block = (Block) $6;
1467 CheckDef (current_container.AddMethod (d), d.Name, d.Location);
1475 EVENT type variable_declarators SEMICOLON
1477 foreach (VariableDeclaration var in (ArrayList) $5) {
1479 Event e = new Event ((Expression) $4, var.identifier,
1480 var.expression_or_array_initializer,
1481 (int) $2, null, null, (Attributes) $1, lexer.Location);
1483 CheckDef (current_container.AddEvent (e), e.Name, e.Location);
1489 EVENT type member_name
1492 implicit_value_parameter_type = (Expression) $4;
1493 lexer.EventParsing = true;
1494 oob_stack.Push (lexer.Location);
1496 event_accessor_declarations
1498 lexer.EventParsing = false;
1502 Location loc = (Location) oob_stack.Pop ();
1504 Pair pair = (Pair) $8;
1505 Accessor add_accessor = null;
1506 Accessor rem_accessor = null;
1508 if (pair.First != null)
1509 add_accessor = (Accessor) pair.First;
1510 if (pair.Second != null)
1511 rem_accessor = (Accessor) pair.Second;
1513 Event e = new Event ((Expression) $4, (string) $5, null, (int) $2, add_accessor, rem_accessor,
1514 (Attributes) $1, loc);
1516 CheckDef (current_container.AddEvent (e), e.Name, loc);
1517 implicit_value_parameter_type = null;
1521 event_accessor_declarations
1522 : add_accessor_declaration remove_accessor_declaration
1524 $$ = new Pair ($1, $2);
1526 | remove_accessor_declaration add_accessor_declaration
1528 $$ = new Pair ($2, $1);
1532 add_accessor_declaration
1533 : opt_attributes ADD
1535 Parameter [] args = new Parameter [1];
1536 Parameter implicit_value_parameter = new Parameter (
1537 implicit_value_parameter_type, "value",
1538 Parameter.Modifier.NONE, null);
1540 args [0] = implicit_value_parameter;
1542 current_local_parameters = new Parameters (args, null, lexer.Location);
1543 lexer.EventParsing = false;
1547 $$ = new Accessor ((Block) $4, (Attributes) $1);
1548 lexer.EventParsing = true;
1552 remove_accessor_declaration
1553 : opt_attributes REMOVE
1555 Parameter [] args = new Parameter [1];
1556 Parameter implicit_value_parameter = new Parameter (
1557 implicit_value_parameter_type, "value",
1558 Parameter.Modifier.NONE, null);
1560 args [0] = implicit_value_parameter;
1562 current_local_parameters = new Parameters (args, null, lexer.Location);
1563 lexer.EventParsing = false;
1567 $$ = new Accessor ((Block) $4, (Attributes) $1);
1568 lexer.EventParsing = true;
1573 : opt_attributes opt_modifiers indexer_declarator
1576 IndexerDeclaration decl = (IndexerDeclaration) $3;
1578 implicit_value_parameter_type = decl.type;
1580 lexer.PropertyParsing = true;
1581 parsing_indexer = true;
1583 indexer_parameters = decl.param_list;
1584 oob_stack.Push (lexer.Location);
1586 accessor_declarations
1588 lexer.PropertyParsing = false;
1589 parsing_indexer = false;
1593 // The signature is computed from the signature of the indexer. Look
1594 // at section 3.6 on the spec
1595 Location loc = (Location) oob_stack.Pop ();
1597 IndexerDeclaration decl = (IndexerDeclaration) $3;
1598 Pair pair = (Pair) $6;
1599 Accessor get_block = (Accessor) pair.First;
1600 Accessor set_block = (Accessor) pair.Second;
1602 indexer = new Indexer (decl.type, decl.interface_type, (int) $2, decl.param_list,
1603 get_block, set_block, (Attributes) $1, loc);
1605 // Note that there is no equivalent of CheckDef for this case
1606 // We shall handle this in semantic analysis
1608 current_container.AddIndexer (indexer);
1610 current_local_parameters = null;
1611 implicit_value_parameter_type = null;
1612 indexer_parameters = null;
1617 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1619 Parameters pars = (Parameters) $4;
1621 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1622 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1625 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
1627 | type qualified_identifier DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1629 Parameters pars = (Parameters) $6;
1631 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1632 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1634 $$ = new IndexerDeclaration ((Expression) $1, (string) $2, pars);
1646 Location enum_location = lexer.Location;
1648 string full_name = MakeName ((string) $4);
1649 Enum e = new Enum (current_container, (Expression) $5, (int) $2, full_name,
1650 (Attributes) $1, enum_location);
1652 foreach (VariableDeclaration ev in (ArrayList) $6) {
1653 Location loc = (Location) ev.Location;
1655 CheckDef (e.AddEnumMember (ev.identifier,
1656 (Expression) ev.expression_or_array_initializer,
1657 loc, ev.OptAttributes),
1658 ev.identifier, loc);
1661 e.Namespace = current_namespace;
1663 CheckDef (current_container.AddEnum (e), full_name, enum_location);
1664 RootContext.Tree.RecordDecl (full_name, e);
1670 : /* empty */ { $$ = TypeManager.system_int32_expr; }
1671 | COLON type { $$ = $2; }
1675 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1681 opt_enum_member_declarations
1682 : /* empty */ { $$ = new ArrayList (); }
1683 | enum_member_declarations opt_comma { $$ = $1; }
1686 enum_member_declarations
1687 : enum_member_declaration
1689 ArrayList l = new ArrayList ();
1694 | enum_member_declarations COMMA enum_member_declaration
1696 ArrayList l = (ArrayList) $1;
1704 enum_member_declaration
1705 : opt_attributes IDENTIFIER
1707 $$ = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
1709 | opt_attributes IDENTIFIER
1711 $$ = lexer.Location;
1715 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
1719 delegate_declaration
1723 IDENTIFIER OPEN_PARENS
1724 opt_formal_parameter_list
1728 Location l = lexer.Location;
1729 Delegate del = new Delegate (current_container, (Expression) $4, (int) $2,
1730 MakeName ((string) $5), (Parameters) $7,
1731 (Attributes) $1, l);
1733 del.Namespace = current_namespace;
1734 CheckDef (current_container.AddDelegate (del), del.Name, l);
1739 IDENTIFIER OPEN_PARENS
1740 opt_formal_parameter_list
1744 Location l = lexer.Location;
1745 Delegate del = new Delegate (
1747 TypeManager.system_void_expr, (int) $2, MakeName ((string) $5),
1748 (Parameters) $7, (Attributes) $1, l);
1750 del.Namespace = current_namespace;
1751 CheckDef (current_container.AddDelegate (del), del.Name, l);
1756 : namespace_or_type_name
1759 namespace_or_type_name
1760 : qualified_identifier
1764 * Before you think of adding a return_type, notice that we have been
1765 * using two rules in the places where it matters (one rule using type
1766 * and another identical one that uses VOID as the return type). This
1767 * gets rid of a shift/reduce couple
1770 : type_name { /* class_type */
1772 This does interfaces, delegates, struct_types, class_types,
1773 parent classes, and more! 4.2
1775 $$ = DecomposeQI ((string) $1, lexer.Location);
1787 // Note that here only unmanaged types are allowed but we
1788 // can't perform checks during this phase - we do it during
1789 // semantic analysis.
1791 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1795 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
1801 | non_expression_type rank_specifier
1803 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1805 | non_expression_type STAR
1807 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1809 | expression rank_specifiers
1811 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1815 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1822 ArrayList types = new ArrayList ();
1827 | type_list COMMA type
1829 ArrayList types = (ArrayList) $1;
1837 * replaces all the productions for isolating the various
1838 * simple types, but we need this to reuse it easily in local_variable_type
1841 : OBJECT { $$ = TypeManager.system_object_expr; }
1842 | STRING { $$ = TypeManager.system_string_expr; }
1843 | BOOL { $$ = TypeManager.system_boolean_expr; }
1844 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
1845 | FLOAT { $$ = TypeManager.system_single_expr; }
1846 | DOUBLE { $$ = TypeManager.system_double_expr; }
1851 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
1852 | BYTE { $$ = TypeManager.system_byte_expr; }
1853 | SHORT { $$ = TypeManager.system_int16_expr; }
1854 | USHORT { $$ = TypeManager.system_uint16_expr; }
1855 | INT { $$ = TypeManager.system_int32_expr; }
1856 | UINT { $$ = TypeManager.system_uint32_expr; }
1857 | LONG { $$ = TypeManager.system_int64_expr; }
1858 | ULONG { $$ = TypeManager.system_uint64_expr; }
1859 | CHAR { $$ = TypeManager.system_char_expr; }
1860 | VOID { $$ = TypeManager.system_void_expr; }
1868 : type rank_specifiers
1870 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1875 // Expressions, section 7.5
1883 | qualified_identifier
1885 string name = (string) $1;
1888 $$ = DecomposeQI (name, lexer.Location);
1890 | parenthesized_expression
1892 | invocation_expression
1896 | post_increment_expression
1897 | post_decrement_expression
1901 | checked_expression
1902 | unchecked_expression
1903 | pointer_member_access
1910 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
1911 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
1912 | NULL { $$ = NullLiteral.Null; }
1916 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
1917 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
1918 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
1923 object v = lexer.Value;
1926 $$ = new IntLiteral ((Int32) v);
1928 $$ = new UIntLiteral ((UInt32) v);
1930 $$ = new LongLiteral ((Int64) v);
1931 else if (v is ulong)
1932 $$ = new ULongLiteral ((UInt64) v);
1934 Console.WriteLine ("OOPS. Unexpected result from scanner");
1939 : TRUE { $$ = new BoolLiteral (true); }
1940 | FALSE { $$ = new BoolLiteral (false); }
1943 parenthesized_expression
1944 : OPEN_PARENS expression CLOSE_PARENS
1949 : primary_expression DOT IDENTIFIER
1951 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1953 | predefined_type DOT IDENTIFIER
1955 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1963 invocation_expression
1964 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
1967 Location l = lexer.Location;
1968 Report.Error (1, l, "THIS IS CRAZY");
1970 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
1975 : /* empty */ { $$ = null; }
1982 ArrayList list = new ArrayList ();
1986 | argument_list COMMA argument
1988 ArrayList list = (ArrayList) $1;
1997 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
1999 | REF variable_reference
2001 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2003 | OUT variable_reference
2005 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2010 : expression { note ("section 5.4"); $$ = $1; }
2014 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2016 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2018 | primary_expression rank_specifiers
2020 // So the super-trick is that primary_expression
2021 // can only be either a SimpleName or a MemberAccess.
2022 // The MemberAccess case arises when you have a fully qualified type-name like :
2024 // SimpleName is when you have
2027 Expression expr = (Expression) $1;
2028 if (!(expr is SimpleName || expr is MemberAccess)) {
2029 Error_ExpectingTypeName (lexer.Location, expr);
2030 $$ = TypeManager.system_object_expr;
2033 // So we extract the string corresponding to the SimpleName
2036 $$ = new SimpleName (GetQualifiedIdentifier (expr) + (string) $2, lexer.Location);
2044 ArrayList list = new ArrayList ();
2048 | expression_list COMMA expression
2050 ArrayList list = (ArrayList) $1;
2059 $$ = new This (current_block, lexer.Location);
2064 : BASE DOT IDENTIFIER
2066 $$ = new BaseAccess ((string) $3, lexer.Location);
2068 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2070 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2074 post_increment_expression
2075 : primary_expression OP_INC
2077 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2078 (Expression) $1, lexer.Location);
2082 post_decrement_expression
2083 : primary_expression OP_DEC
2085 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2086 (Expression) $1, lexer.Location);
2091 : object_or_delegate_creation_expression
2092 | array_creation_expression
2095 object_or_delegate_creation_expression
2096 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2098 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2102 array_creation_expression
2103 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2105 opt_array_initializer
2107 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2109 | NEW type rank_specifiers array_initializer
2111 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2115 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2135 | rank_specifiers rank_specifier
2137 $$ = (string) $2 + (string) $1;
2142 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2144 $$ = "[" + (string) $2 + "]";
2164 | dim_separators COMMA
2166 $$ = (string) $1 + ",";
2170 opt_array_initializer
2182 : OPEN_BRACE CLOSE_BRACE
2184 ArrayList list = new ArrayList ();
2187 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2189 $$ = (ArrayList) $2;
2193 variable_initializer_list
2194 : variable_initializer
2196 ArrayList list = new ArrayList ();
2200 | variable_initializer_list COMMA variable_initializer
2202 ArrayList list = (ArrayList) $1;
2209 : TYPEOF OPEN_PARENS type CLOSE_PARENS
2211 $$ = new TypeOf ((Expression) $3, lexer.Location);
2216 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2217 $$ = new SizeOf ((Expression) $3, lexer.Location);
2222 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2224 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2228 unchecked_expression
2229 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2231 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2235 pointer_member_access
2236 : primary_expression OP_PTR IDENTIFIER
2240 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2241 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2245 : primary_expression
2246 | BANG prefixed_unary_expression
2248 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2250 | TILDE prefixed_unary_expression
2252 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2258 : OPEN_PARENS expression CLOSE_PARENS unary_expression
2260 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2262 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2264 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2269 // The idea to split this out is from Rhys' grammar
2270 // to solve the problem with casts.
2272 prefixed_unary_expression
2274 | PLUS prefixed_unary_expression
2276 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2278 | MINUS prefixed_unary_expression
2280 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2282 | OP_INC prefixed_unary_expression
2284 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2285 (Expression) $2, lexer.Location);
2287 | OP_DEC prefixed_unary_expression
2289 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2290 (Expression) $2, lexer.Location);
2292 | STAR prefixed_unary_expression
2294 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2296 | BITWISE_AND prefixed_unary_expression
2298 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2302 pre_increment_expression
2303 : OP_INC prefixed_unary_expression
2305 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2306 (Expression) $2, lexer.Location);
2310 pre_decrement_expression
2311 : OP_DEC prefixed_unary_expression
2313 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2314 (Expression) $2, lexer.Location);
2318 multiplicative_expression
2319 : prefixed_unary_expression
2320 | multiplicative_expression STAR prefixed_unary_expression
2322 $$ = new Binary (Binary.Operator.Multiply,
2323 (Expression) $1, (Expression) $3, lexer.Location);
2325 | multiplicative_expression DIV prefixed_unary_expression
2327 $$ = new Binary (Binary.Operator.Division,
2328 (Expression) $1, (Expression) $3, lexer.Location);
2330 | multiplicative_expression PERCENT prefixed_unary_expression
2332 $$ = new Binary (Binary.Operator.Modulus,
2333 (Expression) $1, (Expression) $3, lexer.Location);
2338 : multiplicative_expression
2339 | additive_expression PLUS multiplicative_expression
2341 $$ = new Binary (Binary.Operator.Addition,
2342 (Expression) $1, (Expression) $3, lexer.Location);
2344 | additive_expression MINUS multiplicative_expression
2346 $$ = new Binary (Binary.Operator.Subtraction,
2347 (Expression) $1, (Expression) $3, lexer.Location);
2352 : additive_expression
2353 | shift_expression OP_SHIFT_LEFT additive_expression
2355 $$ = new Binary (Binary.Operator.LeftShift,
2356 (Expression) $1, (Expression) $3, lexer.Location);
2358 | shift_expression OP_SHIFT_RIGHT additive_expression
2360 $$ = new Binary (Binary.Operator.RightShift,
2361 (Expression) $1, (Expression) $3, lexer.Location);
2365 relational_expression
2367 | relational_expression OP_LT shift_expression
2369 $$ = new Binary (Binary.Operator.LessThan,
2370 (Expression) $1, (Expression) $3, lexer.Location);
2372 | relational_expression OP_GT shift_expression
2374 $$ = new Binary (Binary.Operator.GreaterThan,
2375 (Expression) $1, (Expression) $3, lexer.Location);
2377 | relational_expression OP_LE shift_expression
2379 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2380 (Expression) $1, (Expression) $3, lexer.Location);
2382 | relational_expression OP_GE shift_expression
2384 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2385 (Expression) $1, (Expression) $3, lexer.Location);
2387 | relational_expression IS type
2389 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
2391 | relational_expression AS type
2393 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
2398 : relational_expression
2399 | equality_expression OP_EQ relational_expression
2401 $$ = new Binary (Binary.Operator.Equality,
2402 (Expression) $1, (Expression) $3, lexer.Location);
2404 | equality_expression OP_NE relational_expression
2406 $$ = new Binary (Binary.Operator.Inequality,
2407 (Expression) $1, (Expression) $3, lexer.Location);
2412 : equality_expression
2413 | and_expression BITWISE_AND equality_expression
2415 $$ = new Binary (Binary.Operator.BitwiseAnd,
2416 (Expression) $1, (Expression) $3, lexer.Location);
2420 exclusive_or_expression
2422 | exclusive_or_expression CARRET and_expression
2424 $$ = new Binary (Binary.Operator.ExclusiveOr,
2425 (Expression) $1, (Expression) $3, lexer.Location);
2429 inclusive_or_expression
2430 : exclusive_or_expression
2431 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2433 $$ = new Binary (Binary.Operator.BitwiseOr,
2434 (Expression) $1, (Expression) $3, lexer.Location);
2438 conditional_and_expression
2439 : inclusive_or_expression
2440 | conditional_and_expression OP_AND inclusive_or_expression
2442 $$ = new Binary (Binary.Operator.LogicalAnd,
2443 (Expression) $1, (Expression) $3, lexer.Location);
2447 conditional_or_expression
2448 : conditional_and_expression
2449 | conditional_or_expression OP_OR conditional_and_expression
2451 $$ = new Binary (Binary.Operator.LogicalOr,
2452 (Expression) $1, (Expression) $3, lexer.Location);
2456 conditional_expression
2457 : conditional_or_expression
2458 | conditional_or_expression INTERR expression COLON expression
2460 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2464 assignment_expression
2465 : prefixed_unary_expression ASSIGN expression
2467 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2469 | prefixed_unary_expression OP_MULT_ASSIGN expression
2471 Location l = lexer.Location;
2473 $$ = new CompoundAssign (
2474 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
2476 | prefixed_unary_expression OP_DIV_ASSIGN expression
2478 Location l = lexer.Location;
2480 $$ = new CompoundAssign (
2481 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
2483 | prefixed_unary_expression OP_MOD_ASSIGN expression
2485 Location l = lexer.Location;
2487 $$ = new CompoundAssign (
2488 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
2490 | prefixed_unary_expression OP_ADD_ASSIGN expression
2492 Location l = lexer.Location;
2494 $$ = new CompoundAssign (
2495 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
2497 | prefixed_unary_expression OP_SUB_ASSIGN expression
2499 Location l = lexer.Location;
2501 $$ = new CompoundAssign (
2502 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
2504 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2506 Location l = lexer.Location;
2508 $$ = new CompoundAssign (
2509 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
2511 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2513 Location l = lexer.Location;
2515 $$ = new CompoundAssign (
2516 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
2518 | prefixed_unary_expression OP_AND_ASSIGN expression
2520 Location l = lexer.Location;
2522 $$ = new CompoundAssign (
2523 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
2525 | prefixed_unary_expression OP_OR_ASSIGN expression
2527 Location l = lexer.Location;
2529 $$ = new CompoundAssign (
2530 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
2532 | prefixed_unary_expression OP_XOR_ASSIGN expression
2534 Location l = lexer.Location;
2536 $$ = new CompoundAssign (
2537 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
2542 : conditional_expression
2543 | assignment_expression
2565 name = MakeName ((string) $4);
2567 new_class = new Class (current_container, name, (int) $2,
2568 (Attributes) $1, lexer.Location);
2569 current_container = new_class;
2570 current_container.Namespace = current_namespace;
2571 RootContext.Tree.RecordDecl (name, new_class);
2577 Class new_class = (Class) current_container;
2580 new_class.Bases = (ArrayList) $6;
2582 current_container = current_container.Parent;
2583 CheckDef (current_container.AddClass (new_class), new_class.Name, new_class.Location);
2590 : /* empty */ { $$ = (int) 0; }
2596 | modifiers modifier
2601 if ((m1 & m2) != 0) {
2602 Location l = lexer.Location;
2603 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2605 $$ = (int) (m1 | m2);
2610 : NEW { $$ = Modifiers.NEW; }
2611 | PUBLIC { $$ = Modifiers.PUBLIC; }
2612 | PROTECTED { $$ = Modifiers.PROTECTED; }
2613 | INTERNAL { $$ = Modifiers.INTERNAL; }
2614 | PRIVATE { $$ = Modifiers.PRIVATE; }
2615 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2616 | SEALED { $$ = Modifiers.SEALED; }
2617 | STATIC { $$ = Modifiers.STATIC; }
2618 | READONLY { $$ = Modifiers.READONLY; }
2619 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2620 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2621 | EXTERN { $$ = Modifiers.EXTERN; }
2622 | VOLATILE { $$ = Modifiers.VOLATILE; }
2623 | UNSAFE { $$ = Modifiers.UNSAFE; }
2627 : /* empty */ { $$ = null; }
2628 | class_base { $$ = $1; }
2632 : COLON type_list { $$ = $2; }
2640 // A block is "contained" on the following places:
2642 // property_declaration as part of the accessor body (get/set)
2643 // operator_declaration
2644 // constructor_declaration
2645 // destructor_declaration
2646 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2651 current_block = new Block (current_block, current_local_parameters,
2652 lexer.Location, Location.Null);
2654 opt_statement_list CLOSE_BRACE
2656 while (current_block.Implicit)
2657 current_block = current_block.Parent;
2659 current_block.SetEndLocation (lexer.Location);
2660 current_block = current_block.Parent;
2671 | statement_list statement
2675 : declaration_statement
2677 if ($1 != null && (Block) $1 != current_block){
2678 current_block.AddStatement ((Statement) $1);
2679 current_block = (Block) $1;
2682 | embedded_statement
2684 Statement s = (Statement) $1;
2687 current_block.AddStatement ((Statement) $1);
2695 | expression_statement
2696 | selection_statement
2697 | iteration_statement
2701 | unchecked_statement
2711 $$ = new EmptyStatement ();
2718 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
2720 if (!current_block.AddLabel ((string) $1, labeled)){
2721 Location l = lexer.Location;
2722 Report.Error (140, l, "The label '" + ((string) $1) + "' is a duplicate");
2724 current_block.AddStatement (labeled);
2729 declaration_statement
2730 : local_variable_declaration SEMICOLON
2733 DictionaryEntry de = (DictionaryEntry) $1;
2735 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
2739 | local_constant_declaration SEMICOLON
2742 DictionaryEntry de = (DictionaryEntry) $1;
2744 $$ = declare_local_constant ((Expression) de.Key, (VariableDeclaration) de.Value);
2750 * The following is from Rhys' grammar:
2751 * > Types in local variable declarations must be recognized as
2752 * > expressions to prevent reduce/reduce errors in the grammar.
2753 * > The expressions are converted into types during semantic analysis.
2756 : primary_expression opt_rank_specifier
2758 // FIXME: Do something smart here regarding the composition of the type.
2760 // Ok, the above "primary_expression" is there to get rid of
2761 // both reduce/reduce and shift/reduces in the grammar, it should
2762 // really just be "type_name". If you use type_name, a reduce/reduce
2763 // creeps up. If you use qualified_identifier (which is all we need
2764 // really) two shift/reduces appear.
2767 // So the super-trick is that primary_expression
2768 // can only be either a SimpleName or a MemberAccess.
2769 // The MemberAccess case arises when you have a fully qualified type-name like :
2771 // SimpleName is when you have
2774 Expression expr = (Expression) $1;
2775 if (!(expr is SimpleName || expr is MemberAccess)) {
2776 Error_ExpectingTypeName (lexer.Location, expr);
2780 // So we extract the string corresponding to the SimpleName
2783 if ((string) $2 == "")
2786 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2789 | builtin_types opt_rank_specifier
2791 if ((string) $2 == "")
2794 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2798 local_variable_pointer_type
2799 : primary_expression STAR
2801 Expression expr = (Expression) $1;
2802 Location l = lexer.Location;
2804 if (!(expr is SimpleName || expr is MemberAccess)) {
2805 Error_ExpectingTypeName (l, expr);
2809 $$ = new ComposedCast ((Expression) $1, "*", l);
2811 | builtin_types STAR
2813 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
2817 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
2819 | local_variable_pointer_type STAR
2821 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2825 local_variable_declaration
2826 : local_variable_type variable_declarators
2829 $$ = new DictionaryEntry ($1, $2);
2833 | local_variable_pointer_type opt_rank_specifier variable_declarators
2838 if ((string) $2 == "")
2839 t = (Expression) $1;
2841 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2842 $$ = new DictionaryEntry (t, $3);
2848 local_constant_declaration
2849 : CONST local_variable_type constant_declarator
2852 $$ = new DictionaryEntry ($2, $3);
2858 expression_statement
2859 : statement_expression SEMICOLON
2866 // We have to do the wrapping here and not in the case above,
2867 // because statement_expression is used for example in for_statement
2869 statement_expression
2870 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2871 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2872 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2873 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2874 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2875 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2876 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
2878 Report.Error (1002, lexer.Location, "Expecting `;'");
2882 object_creation_expression
2883 : object_or_delegate_creation_expression
2884 { note ("complain if this is a delegate maybe?"); }
2893 : if_statement_open if_statement_rest
2902 oob_stack.Push (lexer.Location);
2907 : boolean_expression CLOSE_PARENS
2910 Location l = (Location) oob_stack.Pop ();
2912 $$ = new If ((Expression) $1, (Statement) $3, l);
2914 if (RootContext.WarningLevel >= 3){
2915 if ($3 is EmptyStatement)
2916 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
2920 | boolean_expression CLOSE_PARENS
2921 embedded_statement ELSE embedded_statement
2923 Location l = (Location) oob_stack.Pop ();
2925 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
2930 : SWITCH OPEN_PARENS
2932 oob_stack.Push (lexer.Location);
2933 switch_stack.Push (current_block);
2935 expression CLOSE_PARENS
2938 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
2939 current_block = (Block) switch_stack.Pop ();
2955 Report.Error (1522, lexer.Location, "Empty switch block");
2963 ArrayList sections = new ArrayList ();
2968 | switch_sections switch_section
2970 ArrayList sections = (ArrayList) $1;
2980 current_block = new Block (current_block);
2984 while (current_block.Implicit)
2985 current_block = current_block.Parent;
2986 $$ = new SwitchSection ((ArrayList) $1, current_block);
2993 ArrayList labels = new ArrayList ();
2998 | switch_labels switch_label
3000 ArrayList labels = (ArrayList) ($1);
3008 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3009 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3012 1523, lexer.Location,
3013 "The keyword case or default must precede code in switch block");
3027 oob_stack.Push (lexer.Location);
3029 boolean_expression CLOSE_PARENS embedded_statement
3031 Location l = (Location) oob_stack.Pop ();
3032 $$ = new While ((Expression) $4, (Statement) $6, l);
3034 if (RootContext.WarningLevel >= 3){
3035 if ($6 is EmptyStatement)
3036 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3042 : DO embedded_statement
3043 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
3045 $$ = new Do ((Statement) $2, (Expression) $5, lexer.Location);
3051 opt_for_initializer SEMICOLON
3053 Block assign_block = new Block (current_block);
3054 current_block = assign_block;
3056 if ($3 is DictionaryEntry){
3057 DictionaryEntry de = (DictionaryEntry) $3;
3059 Expression type = (Expression) de.Key;
3060 ArrayList var_declarators = (ArrayList) de.Value;
3062 foreach (VariableDeclaration decl in var_declarators){
3066 vi = current_block.AddVariable (
3067 type, decl.identifier, current_local_parameters, decl.Location);
3071 Location l = lexer.Location;
3073 if (decl.expression_or_array_initializer is Expression){
3074 expr = (Expression) decl.expression_or_array_initializer;
3076 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3077 expr = new ArrayCreation (type, "", init, decl.Location);
3080 LocalVariableReference var;
3081 var = new LocalVariableReference (assign_block, decl.identifier, l);
3083 Assign a = new Assign (var, expr, decl.Location);
3085 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3090 oob_stack.Push (lexer.Location);
3092 opt_for_condition SEMICOLON
3093 opt_for_iterator CLOSE_PARENS
3096 Location l = (Location) oob_stack.Pop ();
3098 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3100 if (RootContext.WarningLevel >= 3){
3101 if ($10 is EmptyStatement)
3102 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3105 current_block.AddStatement (f);
3106 while (current_block.Implicit)
3107 current_block = current_block.Parent;
3109 current_block = current_block.Parent;
3114 : /* empty */ { $$ = new EmptyStatement (); }
3119 : local_variable_declaration
3120 | statement_expression_list
3124 : /* empty */ { $$ = null; }
3125 | boolean_expression
3129 : /* empty */ { $$ = new EmptyStatement (); }
3134 : statement_expression_list
3137 statement_expression_list
3138 : statement_expression
3140 // CHANGE: was `null'
3141 Block b = new Block (current_block, true);
3143 b.AddStatement ((Statement) $1);
3146 | statement_expression_list COMMA statement_expression
3148 Block b = (Block) $1;
3150 b.AddStatement ((Statement) $3);
3156 : FOREACH OPEN_PARENS type IDENTIFIER IN
3158 oob_stack.Push (lexer.Location);
3160 expression CLOSE_PARENS
3162 oob_stack.Push (current_block);
3164 Block foreach_block = new Block (current_block, true);
3165 LocalVariableReference v = null;
3166 Location l = lexer.Location;
3169 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3173 // Get a writable reference to this read-only variable.
3174 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3176 current_block = foreach_block;
3179 oob_stack.Push (current_block);
3183 Block foreach_block = (Block) oob_stack.Pop ();
3184 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3185 Block prev_block = (Block) oob_stack.Pop ();
3186 Location l = (Location) oob_stack.Pop ();
3188 current_block = prev_block;
3191 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3192 foreach_block.AddStatement (f);
3201 | continue_statement
3210 $$ = new Break (lexer.Location);
3215 : CONTINUE SEMICOLON
3217 $$ = new Continue (lexer.Location);
3222 : GOTO IDENTIFIER SEMICOLON
3224 $$ = new Goto (current_block, (string) $2, lexer.Location);
3226 | GOTO CASE constant_expression SEMICOLON
3228 $$ = new GotoCase ((Expression) $3, lexer.Location);
3230 | GOTO DEFAULT SEMICOLON
3232 $$ = new GotoDefault (lexer.Location);
3237 : RETURN opt_expression SEMICOLON
3239 $$ = new Return ((Expression) $2, lexer.Location);
3244 : THROW opt_expression SEMICOLON
3246 $$ = new Throw ((Expression) $2, lexer.Location);
3256 : TRY block catch_clauses
3259 ArrayList s = new ArrayList ();
3261 foreach (Catch cc in (ArrayList) $3) {
3268 // Now s contains the list of specific catch clauses
3269 // and g contains the general one.
3271 $$ = new Try ((Block) $2, s, g, null, lexer.Location);
3273 | TRY block opt_catch_clauses FINALLY block
3276 ArrayList s = new ArrayList ();
3277 ArrayList catch_list = (ArrayList) $3;
3279 if (catch_list != null){
3280 foreach (Catch cc in catch_list) {
3288 $$ = new Try ((Block) $2, s, g, (Block) $5, lexer.Location);
3292 Report.Error (1524, lexer.Location, "Expected catch or finally");
3297 : /* empty */ { $$ = null; }
3304 ArrayList l = new ArrayList ();
3309 | catch_clauses catch_clause
3311 ArrayList l = (ArrayList) $1;
3319 : /* empty */ { $$ = null; }
3324 : CATCH opt_catch_args
3326 Expression type = null;
3330 DictionaryEntry cc = (DictionaryEntry) $2;
3331 type = (Expression) cc.Key;
3332 id = (string) cc.Value;
3335 ArrayList one = new ArrayList ();
3336 Location loc = lexer.Location;
3338 one.Add (new VariableDeclaration (id, null, loc));
3341 current_block = new Block (current_block);
3342 Block b = declare_local_variables (type, one, loc);
3347 Expression type = null;
3351 DictionaryEntry cc = (DictionaryEntry) $2;
3352 type = (Expression) cc.Key;
3353 id = (string) cc.Value;
3357 // FIXME: I can change this for an assignment.
3359 while (current_block != (Block) $1)
3360 current_block = current_block.Parent;
3365 $$ = new Catch (type, id , (Block) $4, lexer.Location);
3370 : /* empty */ { $$ = null; }
3375 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3377 $$ = new DictionaryEntry ($2, $3);
3384 $$ = new Checked ((Block) $2);
3391 $$ = new Unchecked ((Block) $2);
3398 if (!RootContext.Unsafe){
3399 Report.Error (227, lexer.Location,
3400 "Unsafe code can only be used if --unsafe is used");
3403 $$ = new Unsafe ((Block) $3);
3409 pointer_type fixed_pointer_declarators
3412 Block assign_block = new Block (current_block, true);
3413 ArrayList list = (ArrayList) $4;
3414 Expression type = (Expression) $3;
3415 Location l = lexer.Location;
3416 int top = list.Count;
3418 for (int i = 0; i < top; i++){
3419 Pair p = (Pair) list [i];
3422 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3429 current_block.AddStatement (assign_block);
3430 current_block = assign_block;
3431 oob_stack.Push (assign_block);
3436 Location l = (Location) oob_stack.Pop ();
3437 Block assign_block = (Block) oob_stack.Pop ();
3439 ArrayList list = (ArrayList) $4;
3440 int top = list.Count;
3442 $$ = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
3446 fixed_pointer_declarators
3447 : fixed_pointer_declarator {
3448 ArrayList declarators = new ArrayList ();
3449 declarators.Add ($1);
3452 | fixed_pointer_declarators COMMA fixed_pointer_declarator
3454 ArrayList declarators = (ArrayList) $1;
3455 declarators.Add ($3);
3460 fixed_pointer_declarator
3461 : IDENTIFIER ASSIGN expression
3463 $$ = new Pair ($1, $3);
3468 : LOCK OPEN_PARENS expression CLOSE_PARENS
3474 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
3479 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
3481 Block assign_block = new Block (current_block);
3482 current_block = assign_block;
3484 oob_stack.Push (lexer.Location);
3486 if ($3 is DictionaryEntry){
3487 DictionaryEntry de = (DictionaryEntry) $3;
3488 Location l = lexer.Location;
3490 Expression type = (Expression) de.Key;
3491 ArrayList var_declarators = (ArrayList) de.Value;
3493 ArrayList vars = new ArrayList ();
3495 foreach (VariableDeclaration decl in var_declarators){
3497 VariableInfo vi = current_block.AddVariable (
3498 type, decl.identifier,
3499 current_local_parameters, decl.Location);
3505 if (decl.expression_or_array_initializer is Expression){
3506 expr = (Expression) decl.expression_or_array_initializer;
3508 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3510 expr = new ArrayCreation (type, "", init, decl.Location);
3513 LocalVariableReference var;
3515 // Get a writable reference to this read-only variable.
3516 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
3518 // This is so that it is not a warning on using variables
3521 vars.Add (new DictionaryEntry (var, expr));
3523 // Assign a = new Assign (var, expr, decl.Location);
3524 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3526 $3 = new DictionaryEntry (type, vars);
3531 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
3532 current_block.AddStatement (u);
3533 while (current_block.Implicit)
3534 current_block = current_block.Parent;
3536 current_block = current_block.Parent;
3540 resource_acquisition
3541 : local_variable_declaration
3548 // A class used to pass around variable declarations and constants
3550 public class VariableDeclaration {
3551 public string identifier;
3552 public object expression_or_array_initializer;
3553 public Location Location;
3554 public Attributes OptAttributes;
3556 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
3558 this.identifier = id;
3559 this.expression_or_array_initializer = eoai;
3561 this.OptAttributes = opt_attrs;
3564 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
3570 // A class used to hold info about an indexer declarator
3573 public class IndexerDeclaration {
3574 public Expression type;
3575 public string interface_type;
3576 public Parameters param_list;
3578 public IndexerDeclaration (Expression type, string interface_type, Parameters param_list)
3581 this.interface_type = interface_type;
3582 this.param_list = param_list;
3587 // A class used to hold info about an operator declarator
3590 public class OperatorDeclaration {
3591 public Operator.OpType optype;
3592 public Expression ret_type, arg1type, arg2type;
3593 public string arg1name, arg2name;
3594 public Location location;
3596 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
3597 Expression arg1type, string arg1name,
3598 Expression arg2type, string arg2name, Location location)
3601 this.ret_type = ret_type;
3602 this.arg1type = arg1type;
3603 this.arg1name = arg1name;
3604 this.arg2type = arg2type;
3605 this.arg2name = arg2name;
3606 this.location = location;
3611 void Error_ExpectingTypeName (Location l, Expression expr)
3613 if (expr is Invocation){
3614 Report.Error (1002, l, "; expected");
3616 Report.Error (-1, l, "Invalid Type definition");
3621 // Given the @class_name name, it creates a fully qualified name
3622 // based on the containing declaration space
3625 MakeName (string class_name)
3627 string ns = current_namespace.Name;
3628 string container_name = current_container.Name;
3630 if (container_name == ""){
3632 return ns + "." + class_name;
3636 return container_name + "." + class_name;
3640 // Used to report back to the user the result of a declaration
3641 // in the current declaration space
3644 CheckDef (AdditionResult result, string name, Location l)
3646 if (result == AdditionResult.Success)
3650 case AdditionResult.NameExists:
3651 Report.Error (102, l, "The container `" + current_container.Name +
3652 "' already contains a definition for `"+
3658 // This is handled only for static Constructors, because
3659 // in reality we handle these by the semantic analysis later
3661 case AdditionResult.MethodExists:
3663 111, l, "Class `"+current_container.Name+
3664 "' already defines a member called '" +
3665 name + "' with the same parameter types (more than one default constructor)");
3668 case AdditionResult.EnclosingClash:
3669 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
3672 case AdditionResult.NotAConstructor:
3673 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
3679 CheckDef (bool result, string name, Location l)
3683 CheckDef (AdditionResult.NameExists, name, l);
3686 Expression DecomposeQI (string name, Location loc)
3690 if (name.IndexOf ('.') == -1){
3691 return new SimpleName (name, loc);
3693 int pos = name.LastIndexOf (".");
3694 string left = name.Substring (0, pos);
3695 string right = name.Substring (pos + 1);
3697 o = DecomposeQI (left, loc);
3699 return new MemberAccess (o, right, loc);
3704 // This method is used to get at the complete string representation of
3705 // a fully-qualified type name, hiding inside a MemberAccess ;-)
3706 // This is necessary because local_variable_type admits primary_expression
3707 // as the type of the variable. So we do some extra checking
3709 string GetQualifiedIdentifier (Expression expr)
3711 if (expr is SimpleName)
3712 return ((SimpleName)expr).Name;
3713 else if (expr is MemberAccess)
3714 return GetQualifiedIdentifier (((MemberAccess)expr).Expr) + "." + ((MemberAccess) expr).Identifier;
3716 throw new Exception ("Expr has to be either SimpleName or MemberAccess! (" + expr + ")");
3720 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
3722 Block implicit_block;
3723 ArrayList inits = null;
3726 // We use the `Used' property to check whether statements
3727 // have been added to the current block. If so, we need
3728 // to create another block to contain the new declaration
3729 // otherwise, as an optimization, we use the same block to
3730 // add the declaration.
3732 // FIXME: A further optimization is to check if the statements
3733 // that were added were added as part of the initialization
3734 // below. In which case, no other statements have been executed
3735 // and we might be able to reduce the number of blocks for
3736 // situations like this:
3738 // int j = 1; int k = j + 1;
3740 if (current_block.Used) {
3741 implicit_block = new Block (current_block, true, loc, Location.Null);
3742 implicit_block.AddChildVariableNames (current_block);
3744 implicit_block = current_block;
3746 foreach (VariableDeclaration decl in variable_declarators){
3748 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
3749 if (decl.expression_or_array_initializer != null){
3751 inits = new ArrayList ();
3758 return implicit_block;
3760 foreach (VariableDeclaration decl in inits){
3764 if (decl.expression_or_array_initializer is Expression){
3765 expr = (Expression) decl.expression_or_array_initializer;
3768 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3770 expr = new ArrayCreation (type, "", init, decl.Location);
3773 LocalVariableReference var;
3774 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
3776 assign = new Assign (var, expr, decl.Location);
3778 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
3781 return implicit_block;
3784 Block declare_local_constant (Expression type, VariableDeclaration decl)
3786 Block implicit_block;
3788 if (current_block.Used)
3789 implicit_block = new Block (current_block, true);
3791 implicit_block = current_block;
3793 if (!(implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
3794 current_local_parameters, decl.Location))){
3797 return implicit_block;
3800 void CheckAttributeTarget (string a)
3804 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
3808 Location l = lexer.Location;
3809 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
3815 void CheckUnaryOperator (Operator.OpType op)
3819 case Operator.OpType.LogicalNot:
3820 case Operator.OpType.OnesComplement:
3821 case Operator.OpType.Increment:
3822 case Operator.OpType.Decrement:
3823 case Operator.OpType.True:
3824 case Operator.OpType.False:
3825 case Operator.OpType.Addition:
3826 case Operator.OpType.Subtraction:
3831 Location l = lexer.Location;
3832 Report.Error (1019, l, "Overloadable unary operator expected");
3838 void CheckBinaryOperator (Operator.OpType op)
3842 case Operator.OpType.Addition:
3843 case Operator.OpType.Subtraction:
3844 case Operator.OpType.Multiply:
3845 case Operator.OpType.Division:
3846 case Operator.OpType.Modulus:
3847 case Operator.OpType.BitwiseAnd:
3848 case Operator.OpType.BitwiseOr:
3849 case Operator.OpType.ExclusiveOr:
3850 case Operator.OpType.LeftShift:
3851 case Operator.OpType.RightShift:
3852 case Operator.OpType.Equality:
3853 case Operator.OpType.Inequality:
3854 case Operator.OpType.GreaterThan:
3855 case Operator.OpType.LessThan:
3856 case Operator.OpType.GreaterThanOrEqual:
3857 case Operator.OpType.LessThanOrEqual:
3861 Location l = lexer.Location;
3862 Report.Error (1020, l, "Overloadable binary operator expected");
3868 void syntax_error (Location l, string msg)
3870 Report.Error (1003, l, "Syntax error, " + msg);
3873 void output (string s)
3875 Console.WriteLine (s);
3878 void note (string s)
3880 // Used to put annotations
3885 public Tokenizer Lexer {
3891 public CSharpParser (StreamReader reader, string name, ArrayList defines)
3893 current_namespace = new Namespace (null, "");
3895 current_container = RootContext.Tree.Types;
3896 current_container.Namespace = current_namespace;
3897 oob_stack = new Stack ();
3898 switch_stack = new Stack ();
3900 lexer = new Tokenizer (reader, name, defines);
3903 public override void parse ()
3906 if (yacc_verbose_flag)
3907 yyparse (lexer, new yydebug.yyDebugSimple ());
3910 } catch (Exception e){
3911 // Please do not remove this, it is used during debugging
3914 Console.WriteLine (lexer.location + " : Parsing error ");
3915 Console.WriteLine (e);