3 // cs-parser.jay: The Parser for the C# compiler
5 // Author: Miguel de Icaza (miguel@gnu.org)
7 // Licensed under the terms of the GNU GPL
9 // (C) 2001 Ximian, Inc (http://www.ximian.com)
12 // (1) Get rid of the *Collections.cs, that is an idea I took from System.CodeDOM
13 // And come to think of it, it is not that great, it duplicates a lot of code
14 // for something which is not really needed. We still have piles of typecasts
15 // anwyays (due to the nature of the stack being a collection of Objects).
17 // (2) Figure out why error productions dont work. `type-declaration' is a
18 // great spot to put an `error' because you can reproduce it with this input:
21 // (3) Move Modifier checking from each object into the parser itself, that will
22 // get rid of the global "error" symbol that we use now to report errors.
23 // We still need to pass a pointer to the tree.ErrorHandler, but that is a
31 using System.Collections;
37 public class CSharpParser : GenericParser {
38 Namespace current_namespace;
39 TypeContainer current_container;
42 // Current block is used to add statements as we find
49 // Current interface is used by the various declaration
50 // productions in the interface declaration to "add"
51 // the interfaces as we find them.
53 Interface current_interface;
56 // This is used by the unary_expression code to resolve
57 // a name against a parameter.
59 Parameters current_local_parameters;
62 // Using during property parsing to describe the implicit
63 // value parameter that is passed to the "set" and "get"accesor
64 // methods (properties and indexers).
66 string implicit_value_parameter_type;
67 Parameters indexer_parameters;
70 // Used to determine if we are parsing the get/set pair
71 // of an indexer or a property
76 // An out-of-band stack.
82 %token NONE /* This token is never returned by our lexer */
83 %token ERROR // This is used not by the parser, but by the tokenizer.
87 *These are the C# keywords
170 /* C# keywords which are not really keywords */
174 /* C# single character operators/punctuation. */
175 %token OPEN_BRACE "{"
176 %token CLOSE_BRACE "}"
177 %token OPEN_BRACKET "["
178 %token CLOSE_BRACKET "]"
179 %token OPEN_PARENS "("
180 %token CLOSE_PARENS ")"
193 %token BITWISE_AND "&"
194 %token BITWISE_OR "|"
201 /* C# multi-character operators. */
204 %token OP_SHIFT_LEFT "<<"
205 %token OP_SHIFT_RIGHT ">>"
212 %token OP_MULT_ASSIGN "*="
213 %token OP_DIV_ASSIGN "/="
214 %token OP_MOD_ASSIGN "%="
215 %token OP_ADD_ASSIGN "+="
216 %token OP_SUB_ASSIGN "-="
217 %token OP_SHIFT_LEFT_ASSIGN "<<="
218 %token OP_SHIFT_RIGHT_ASSIGN ">>="
219 %token OP_AND_ASSIGN "&="
220 %token OP_XOR_ASSIGN "^="
221 %token OP_OR_ASSIGN "|="
225 %token LITERAL_INTEGER "int literal"
226 %token LITERAL_FLOAT "float literal"
227 %token LITERAL_DOUBLE "double literal"
228 %token LITERAL_DECIMAL "decimal literal"
229 %token LITERAL_CHARACTER "character literal"
230 %token LITERAL_STRING "string literal"
234 /* Add precedence rules to solve dangling else s/r conflict */
243 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
245 %left STAR DIV PERCENT
246 %right BANG CARRET UMINUS
247 %nonassoc OP_INC OP_DEC
249 %left OPEN_BRACKET OPEN_BRACE
253 %start compilation_unit
254 /*%start namespace_declaration */
258 : opt_using_directives opt_namespace_member_declarations opt_attributes opt_EOF
260 // Check that using comes only before namespace elements
271 | using_directives using_directive
275 : using_alias_directive
276 | using_namespace_directive
279 using_alias_directive
280 : USING IDENTIFIER ASSIGN
281 namespace_or_type_name SEMICOLON
283 current_namespace.UsingAlias ((string) $2, (string) $4, lexer.Location);
287 using_namespace_directive
288 : USING namespace_name SEMICOLON
290 current_namespace.Using ((string) $2);
294 // namespace_declarations
295 // : namespace_declaration
296 // | namespace_declarations namespace_declaration
298 namespace_declaration
299 : NAMESPACE qualified_identifier
301 current_namespace = RootContext.Tree.RecordNamespace (current_namespace, name, (string) $2);
303 namespace_body opt_semicolon
305 current_namespace = current_namespace.Parent;
321 | qualified_identifier DOT IDENTIFIER {
322 $$ = (($1).ToString ()) + "." + ($3.ToString ()); }
327 : namespace_or_type_name
333 opt_namespace_member_declarations
344 opt_namespace_member_declarations
346 | namespace_member_declarations
349 namespace_member_declarations
350 : namespace_member_declaration
351 | namespace_member_declarations namespace_member_declaration
354 namespace_member_declaration
361 Class c = (Class) $1;
362 mod_flags = c.ModFlags;
364 } else if ($1 is Struct){
365 Struct s = (Struct) $1;
366 mod_flags = s.ModFlags;
371 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
373 1527, lexer.Location,
374 "Namespace elements cant be explicitly " +
375 "declared private or protected in `" + name + "'");
378 | namespace_declaration
384 | interface_declaration
386 | delegate_declaration
388 // Enable this when we have handled all errors, because this acts as a generic fallback
391 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
399 : /* empty */ { $$ = null; }
400 | attribute_section opt_attributes
402 Attributes attrs = null;
403 AttributeSection sect = (AttributeSection) $1;
405 if (sect.Target == "assembly"){
406 RootContext.AddGlobalAttributes (sect, lexer.Location);
412 attrs = (Attributes) $2;
413 attrs.AddAttribute (sect);
417 attrs = new Attributes (sect, lexer.Location);
425 : OPEN_BRACKET attribute_target_specifier attribute_list CLOSE_BRACKET
427 string target = null;
430 target = (string) $2;
432 $$ = new AttributeSection (target, (ArrayList) $3);
434 | OPEN_BRACKET attribute_list CLOSE_BRACKET
436 $$ = new AttributeSection (null, (ArrayList) $2);
440 attribute_target_specifier
441 : attribute_target COLON
450 CheckAttributeTarget ((string) $1);
453 | EVENT { $$ = "event"; }
454 | RETURN { $$ = "return"; }
460 ArrayList attrs = new ArrayList ();
466 | attribute_list COMMA attribute
468 ArrayList attrs = (ArrayList) $1;
480 opt_attribute_arguments
482 $$ = new Attribute ((string) $1, (ArrayList) $3, (Location) $2);
487 : type_name { /* reserved attribute name or identifier: 17.4 */ }
490 opt_attribute_arguments
491 : /* empty */ { $$ = null; }
492 | OPEN_PARENS attribute_arguments CLOSE_PARENS
500 : opt_positional_argument_list
505 ArrayList args = new ArrayList ();
511 | positional_argument_list COMMA named_argument_list
513 ArrayList args = new ArrayList ();
519 | named_argument_list
521 ArrayList args = new ArrayList ();
530 opt_positional_argument_list
531 : /* empty */ { $$ = null; }
532 | positional_argument_list
535 positional_argument_list
538 ArrayList args = new ArrayList ();
539 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
543 | positional_argument_list COMMA expression
545 ArrayList args = (ArrayList) $1;
546 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
555 ArrayList args = new ArrayList ();
560 | named_argument_list COMMA named_argument
562 ArrayList args = (ArrayList) $1;
570 : IDENTIFIER ASSIGN expression
572 $$ = new DictionaryEntry (
574 new Argument ((Expression) $3, Argument.AType.Expression));
580 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
583 opt_class_member_declarations
585 | class_member_declarations
588 class_member_declarations
589 : class_member_declaration
590 | class_member_declarations
591 class_member_declaration
594 class_member_declaration
595 : constant_declaration // done
596 | field_declaration // done
597 | method_declaration // done
598 | property_declaration // done
599 | event_declaration // done
600 | indexer_declaration // done
601 | operator_declaration // done
602 | constructor_declaration // done
603 | destructor_declaration // done
613 string full_struct_name = MakeName ((string) $4);
615 new_struct = new Struct (current_container, full_struct_name, (int) $2,
616 (Attributes) $1, lexer.Location);
617 current_container = new_struct;
618 current_container.Namespace = current_namespace;
619 RootContext.Tree.RecordStruct (full_struct_name, new_struct);
621 opt_struct_interfaces
625 Struct new_struct = (Struct) current_container;
627 current_container = current_container.Parent;
628 CheckDef (current_container.AddStruct (new_struct), new_struct.Name);
633 opt_struct_interfaces
640 | struct_interfaces struct_interface
648 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
651 opt_struct_member_declarations
653 | struct_member_declarations
656 struct_member_declarations
657 : struct_member_declaration
658 | struct_member_declarations struct_member_declaration
661 struct_member_declaration
662 : constant_declaration
665 | property_declaration
667 | indexer_declaration
668 | operator_declaration
669 | constructor_declaration
673 * This is only included so we can flag error 575:
674 * destructors only allowed on class types
676 | destructor_declaration
687 foreach (VariableDeclaration constant in (ArrayList) $5){
688 Const c = new Const (
689 (string) $4, (string) constant.identifier,
690 (Expression) constant.expression_or_array_initializer, (int) $2, (Attributes) $1,
693 CheckDef (current_container.AddConstant (c), c.Name);
699 : constant_declarator
701 ArrayList constants = new ArrayList ();
705 | constant_declarators COMMA constant_declarator
707 ArrayList constants = (ArrayList) $1;
714 : IDENTIFIER ASSIGN constant_expression
716 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
727 string type = (string) $3;
730 foreach (VariableDeclaration var in (ArrayList) $4){
731 Field field = new Field (type, mod, var.identifier,
732 var.expression_or_array_initializer, (Attributes) $1, var.Location);
734 CheckDef (current_container.AddField (field), field.Name);
740 : variable_declarator
742 ArrayList decl = new ArrayList ();
746 | variable_declarators COMMA variable_declarator
748 ArrayList decls = (ArrayList) $1;
755 : IDENTIFIER ASSIGN variable_initializer
757 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
761 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
774 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
776 $$ = new StackAlloc ((string) $2, (Expression) $4, lexer.Location);
784 Method method = (Method) $1;
785 Block b = (Block) $2;
786 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
789 if ((method.ModFlags & extern_abstract) == 0){
791 501, lexer.Location, current_container.MakeName (method.Name) +
792 "must declare a body because it is not marked abstract or extern");
795 if ((method.ModFlags & Modifiers.EXTERN) != 0){
797 179, lexer.Location, current_container.MakeName (method.Name) +
798 " is declared extern, but has a body");
802 method.Block = (Block) $2;
803 CheckDef (current_container.AddMethod (method), method.Name);
805 current_local_parameters = null;
819 1585, lexer.Location, "Member modifier `" +
820 Modifiers.Name (i) + "' must precede member type and name");
833 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
835 Method method = new Method ((string) $3, (int) $2, (string) $4,
836 (Parameters) $6, (Attributes) $1, lexer.Location);
838 current_local_parameters = (Parameters) $6;
846 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
848 Method method = new Method ("System.Void", (int) $2, (string) $4,
849 (Parameters) $6, (Attributes) $1, lexer.Location);
851 current_local_parameters = (Parameters) $6;
858 | SEMICOLON { $$ = null; }
861 opt_formal_parameter_list
862 : /* empty */ { $$ = Parameters.GetEmptyReadOnlyParameters (); }
863 | formal_parameter_list
866 formal_parameter_list
869 ArrayList pars_list = (ArrayList) $1;
871 Parameter [] pars = new Parameter [pars_list.Count];
872 pars_list.CopyTo (pars);
874 $$ = new Parameters (pars, null, lexer.Location);
876 | fixed_parameters COMMA parameter_array
878 ArrayList pars_list = (ArrayList) $1;
880 Parameter [] pars = new Parameter [pars_list.Count];
881 pars_list.CopyTo (pars);
883 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
887 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
894 ArrayList pars = new ArrayList ();
899 | fixed_parameters COMMA fixed_parameter
901 ArrayList pars = (ArrayList) $1;
910 opt_parameter_modifier
914 $$ = new Parameter ((string) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
918 opt_parameter_modifier
919 : /* empty */ { $$ = Parameter.Modifier.NONE; }
924 : REF { $$ = Parameter.Modifier.REF; }
925 | OUT { $$ = Parameter.Modifier.OUT; }
929 : opt_attributes PARAMS type IDENTIFIER
931 $$ = new Parameter ((string) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
932 note ("type must be a single-dimension array type");
937 : qualified_identifier
946 implicit_value_parameter_type = (string) $3;
948 lexer.PropertyParsing = true;
952 accessor_declarations
954 lexer.PropertyParsing = false;
959 Pair pair = (Pair) $7;
960 Block get_block = null;
961 Block set_block = null;
963 if (pair.First != null)
964 get_block = (Block) pair.First;
965 if (pair.Second != null)
966 set_block = (Block) pair.Second;
968 prop = new Property ((string) $3, (string) $4, (int) $2, get_block, set_block,
969 (Attributes) $1, (Location) $6);
971 CheckDef (current_container.AddProperty (prop), prop.Name);
972 implicit_value_parameter_type = null;
976 accessor_declarations
977 : get_accessor_declaration opt_set_accessor_declaration
979 $$ = new Pair ($1, $2);
981 | set_accessor_declaration opt_get_accessor_declaration
983 $$ = new Pair ($2, $1);
987 opt_get_accessor_declaration
988 : /* empty */ { $$ = null; }
989 | get_accessor_declaration
992 opt_set_accessor_declaration
993 : /* empty */ { $$ = null; }
994 | set_accessor_declaration
997 get_accessor_declaration
1000 // If this is not the case, then current_local_parameters has already
1001 // been set in indexer_declaration
1002 if (parsing_indexer == false)
1003 current_local_parameters = null;
1005 current_local_parameters = indexer_parameters;
1006 lexer.PropertyParsing = false;
1011 current_local_parameters = null;
1012 lexer.PropertyParsing = true;
1016 set_accessor_declaration
1017 : opt_attributes SET
1020 Parameter implicit_value_parameter = new Parameter (
1021 implicit_value_parameter_type, "value",
1022 Parameter.Modifier.NONE, null);
1024 if (parsing_indexer == false) {
1025 args = new Parameter [1];
1026 args [0] = implicit_value_parameter;
1028 Parameter [] fp = indexer_parameters.FixedParameters;
1029 int count = fp.Length;
1031 args = new Parameter [count + 1];
1033 fp.CopyTo (args, 0);
1034 args [count] = implicit_value_parameter;
1037 current_local_parameters = new Parameters (args, null, lexer.Location);
1038 lexer.PropertyParsing = false;
1043 current_local_parameters = null;
1044 lexer.PropertyParsing = true;
1050 | SEMICOLON { $$ = new Block (null); }
1053 interface_declaration
1056 INTERFACE IDENTIFIER
1058 Interface new_interface;
1059 string full_interface_name = MakeName ((string) $4);
1061 new_interface = new Interface (current_container, full_interface_name, (int) $2,
1062 (Attributes) $1, lexer.Location);
1063 if (current_interface != null) {
1064 Location l = lexer.Location;
1065 Report.Error (-2, l, "Internal compiler error: interface inside interface");
1067 current_interface = new_interface;
1068 new_interface.Namespace = current_namespace;
1069 RootContext.Tree.RecordInterface (full_interface_name, new_interface);
1072 interface_body opt_semicolon
1074 Interface new_interface = (Interface) current_interface;
1077 new_interface.Bases = (ArrayList) $6;
1079 current_interface = null;
1080 CheckDef (current_container.AddInterface (new_interface), new_interface.Name);
1085 : /* empty */ { $$ = null; }
1090 : COLON interface_type_list { $$ = $2; }
1096 ArrayList interfaces = new ArrayList ();
1098 interfaces.Add ($1);
1101 | interface_type_list COMMA interface_type
1103 ArrayList interfaces = (ArrayList) $1;
1104 interfaces.Add ($3);
1111 opt_interface_member_declarations
1115 opt_interface_member_declarations
1117 | interface_member_declarations
1120 interface_member_declarations
1121 : interface_member_declaration
1122 | interface_member_declarations interface_member_declaration
1125 interface_member_declaration
1126 : interface_method_declaration
1128 InterfaceMethod m = (InterfaceMethod) $1;
1130 CheckDef (current_interface.AddMethod (m), m.Name);
1132 | interface_property_declaration
1134 InterfaceProperty p = (InterfaceProperty) $1;
1136 CheckDef (current_interface.AddProperty (p), p.Name);
1138 | interface_event_declaration
1140 InterfaceEvent e = (InterfaceEvent) $1;
1142 CheckDef (current_interface.AddEvent (e), e.Name);
1144 | interface_indexer_declaration
1146 InterfaceIndexer i = (InterfaceIndexer) $1;
1148 CheckDef (current_interface.AddIndexer (i), "indexer");
1153 : /* empty */ { $$ = false; }
1154 | NEW { $$ = true; }
1157 interface_method_declaration
1158 : opt_attributes opt_new type IDENTIFIER
1159 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1162 $$ = new InterfaceMethod ((string) $3, (string) $4, (bool) $2,
1163 (Parameters) $6, (Attributes) $1, lexer.Location);
1165 | opt_attributes opt_new VOID IDENTIFIER
1166 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1169 $$ = new InterfaceMethod ("System.Void", (string) $4, (bool) $2, (Parameters) $6,
1170 (Attributes) $1, lexer.Location);
1174 interface_property_declaration
1179 { lexer.PropertyParsing = true; }
1181 { lexer.PropertyParsing = false; }
1186 $$ = new InterfaceProperty ((string) $3, (string) $4, (bool) $2,
1187 (gs & 1) == 1, (gs & 2) == 2, (Attributes) $1,
1193 : opt_attributes GET SEMICOLON { $$ = 1; }
1194 | opt_attributes SET SEMICOLON { $$ = 2; }
1195 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1197 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1201 interface_event_declaration
1202 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1204 $$ = new InterfaceEvent ((string) $4, (string) $5, (bool) $2, (Attributes) $1);
1208 interface_indexer_declaration
1209 : opt_attributes opt_new type THIS
1210 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1212 { lexer.PropertyParsing = true; }
1214 { lexer.PropertyParsing = false; }
1217 int a_flags = (int) $10;
1219 bool do_get = (a_flags & 1) == 1;
1220 bool do_set = (a_flags & 2) == 2;
1222 $$ = new InterfaceIndexer ((string) $3, (Parameters) $6, do_get, do_set,
1223 (bool) $2, (Attributes) $1, lexer.Location);
1227 operator_declaration
1228 : opt_attributes opt_modifiers operator_declarator operator_body
1230 OperatorDeclaration decl = (OperatorDeclaration) $3;
1232 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
1233 decl.arg2type, decl.arg2name, (Block) $4, (Attributes) $1, decl.location);
1235 // Note again, checking is done in semantic analysis
1236 current_container.AddOperator (op);
1238 current_local_parameters = null;
1244 | SEMICOLON { $$ = null; }
1247 : type OPERATOR overloadable_operator
1248 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1250 Operator.OpType op = (Operator.OpType) $3;
1251 CheckUnaryOperator (op);
1253 if (op == Operator.OpType.Addition)
1254 op = Operator.OpType.UnaryPlus;
1256 if (op == Operator.OpType.Subtraction)
1257 op = Operator.OpType.UnaryNegation;
1259 Parameter [] pars = new Parameter [1];
1261 pars [0] = new Parameter ((string) $5, (string) $6, Parameter.Modifier.NONE, null);
1263 current_local_parameters = new Parameters (pars, null, lexer.Location);
1265 $$ = new OperatorDeclaration (op, (string) $1, (string) $5, (string) $6,
1266 null, null, lexer.Location);
1268 | type OPERATOR overloadable_operator
1270 type IDENTIFIER COMMA
1274 CheckBinaryOperator ((Operator.OpType) $3);
1276 Parameter [] pars = new Parameter [2];
1278 pars [0] = new Parameter ((string) $5, (string) $6, Parameter.Modifier.NONE, null);
1279 pars [1] = new Parameter ((string) $8, (string) $9, Parameter.Modifier.NONE, null);
1281 current_local_parameters = new Parameters (pars, null, lexer.Location);
1283 $$ = new OperatorDeclaration ((Operator.OpType) $3, (string) $1, (string) $5, (string) $6,
1284 (string) $8, (string) $9, lexer.Location);
1286 | conversion_operator_declarator
1289 overloadable_operator
1291 : BANG { $$ = Operator.OpType.LogicalNot; }
1292 | TILDE { $$ = Operator.OpType.OnesComplement; }
1293 | OP_INC { $$ = Operator.OpType.Increment; }
1294 | OP_DEC { $$ = Operator.OpType.Decrement; }
1295 | TRUE { $$ = Operator.OpType.True; }
1296 | FALSE { $$ = Operator.OpType.False; }
1297 // Unary and binary:
1298 | PLUS { $$ = Operator.OpType.Addition; }
1299 | MINUS { $$ = Operator.OpType.Subtraction; }
1301 | STAR { $$ = Operator.OpType.Multiply; }
1302 | DIV { $$ = Operator.OpType.Division; }
1303 | PERCENT { $$ = Operator.OpType.Modulus; }
1304 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1305 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1306 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1307 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1308 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1309 | OP_EQ { $$ = Operator.OpType.Equality; }
1310 | OP_NE { $$ = Operator.OpType.Inequality; }
1311 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1312 | OP_LT { $$ = Operator.OpType.LessThan; }
1313 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1314 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1317 conversion_operator_declarator
1318 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1320 Parameter [] pars = new Parameter [1];
1322 pars [0] = new Parameter ((string) $5, (string) $6, Parameter.Modifier.NONE, null);
1324 current_local_parameters = new Parameters (pars, null, lexer.Location);
1326 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (string) $3, (string) $5, (string) $6,
1327 null, null, lexer.Location);
1329 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1331 Parameter [] pars = new Parameter [1];
1333 pars [0] = new Parameter ((string) $5, (string) $6, Parameter.Modifier.NONE, null);
1335 current_local_parameters = new Parameters (pars, null, lexer.Location);
1337 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (string) $3, (string) $5, (string) $6,
1338 null, null, lexer.Location);
1342 syntax_error (lexer.Location, "'operator' expected");
1346 syntax_error (lexer.Location, "'operator' expected");
1350 constructor_declaration
1353 constructor_declarator
1356 Constructor c = (Constructor) $3;
1357 c.Block = (Block) $4;
1358 c.ModFlags = (int) $2;
1359 c.OptAttributes = (Attributes) $1;
1361 if ((c.ModFlags & Modifiers.STATIC) != 0){
1362 if ((c.ModFlags & Modifiers.Accessibility) != 0) {
1363 Location l = lexer.Location;
1364 Report.Error (515, l, "Access modifiers are not allowed on static constructors");
1367 if (c.Initializer != null){
1368 Location l = lexer.Location;
1369 Report.Error (514, l, "Static constructors can not have an explicit this or base constructor invocations");
1372 if (!c.Parameters.Empty){
1373 Location l = lexer.Location;
1374 Report.Error (132, l, "Static constructors should not have parameters");
1378 CheckDef (current_container.AddConstructor (c), c.Name);
1380 current_local_parameters = null;
1384 constructor_declarator
1386 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1388 oob_stack.Push (lexer.Location);
1390 current_local_parameters = (Parameters) $3;
1392 opt_constructor_initializer
1394 Location l = (Location) oob_stack.Pop ();
1395 $$ = new Constructor ((string) $1, (Parameters) $3, (ConstructorInitializer) $6, l);
1399 opt_constructor_initializer
1400 : /* empty */ { $$ = null; }
1401 | constructor_initializer
1404 constructor_initializer
1405 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1407 $$ = new ConstructorBaseInitializer ((ArrayList) $4, lexer.Location);
1409 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1411 $$ = new ConstructorThisInitializer ((ArrayList) $4, lexer.Location);
1415 destructor_declaration
1416 : opt_attributes TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1418 if ((string) $3 != current_container.Basename){
1419 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1420 } else if (!(current_container is Class)){
1421 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1423 Location l = lexer.Location;
1425 Method d = new Method (
1426 "System.Void", 0, "Finalize",
1427 new Parameters (null, null, l), (Attributes) $1, l);
1429 d.Block = (Block) $6;
1430 CheckDef (current_container.AddMethod (d), d.Name);
1438 EVENT type variable_declarators SEMICOLON
1440 foreach (VariableDeclaration var in (ArrayList) $5) {
1442 Event e = new Event ((string) $4, var.identifier, var.expression_or_array_initializer,
1443 (int) $2, null, null, (Attributes) $1, lexer.Location);
1445 CheckDef (current_container.AddEvent (e), e.Name);
1451 EVENT type member_name
1454 implicit_value_parameter_type = (string) $4;
1455 lexer.EventParsing = true;
1457 event_accessor_declarations
1459 lexer.EventParsing = false;
1463 Pair pair = (Pair) $8;
1464 Block add_block = null;
1465 Block rem_block = null;
1467 if (pair.First != null)
1468 add_block = (Block) pair.First;
1469 if (pair.Second != null)
1470 rem_block = (Block) pair.Second;
1472 Event e = new Event ((string) $4, (string) $5, null, (int) $2, add_block, rem_block,
1473 (Attributes) $1, lexer.Location);
1475 CheckDef (current_container.AddEvent (e), e.Name);
1476 implicit_value_parameter_type = null;
1480 event_accessor_declarations
1481 : add_accessor_declaration remove_accessor_declaration
1483 $$ = new Pair ($1, $2);
1485 | remove_accessor_declaration add_accessor_declaration
1487 $$ = new Pair ($2, $1);
1491 add_accessor_declaration
1492 : opt_attributes ADD
1494 Parameter [] args = new Parameter [1];
1495 Parameter implicit_value_parameter = new Parameter (
1496 implicit_value_parameter_type, "value",
1497 Parameter.Modifier.NONE, null);
1499 args [0] = implicit_value_parameter;
1501 current_local_parameters = new Parameters (args, null, lexer.Location);
1502 lexer.EventParsing = false;
1507 lexer.EventParsing = true;
1511 remove_accessor_declaration
1512 : opt_attributes REMOVE
1514 Parameter [] args = new Parameter [1];
1515 Parameter implicit_value_parameter = new Parameter (
1516 implicit_value_parameter_type, "value",
1517 Parameter.Modifier.NONE, null);
1519 args [0] = implicit_value_parameter;
1521 current_local_parameters = new Parameters (args, null, lexer.Location);
1522 lexer.EventParsing = false;
1527 lexer.EventParsing = true;
1532 : opt_attributes opt_modifiers indexer_declarator
1535 IndexerDeclaration decl = (IndexerDeclaration) $3;
1537 implicit_value_parameter_type = decl.type;
1539 lexer.PropertyParsing = true;
1540 parsing_indexer = true;
1542 indexer_parameters = decl.param_list;
1543 $$ = lexer.Location;
1545 accessor_declarations
1547 lexer.PropertyParsing = false;
1548 parsing_indexer = false;
1552 // The signature is computed from the signature of the indexer. Look
1553 // at section 3.6 on the spec
1556 IndexerDeclaration decl = (IndexerDeclaration) $3;
1557 Pair pair = (Pair) $6;
1558 Block get_block = null;
1559 Block set_block = null;
1561 if (pair.First != null)
1562 get_block = (Block) pair.First;
1563 if (pair.Second != null)
1564 set_block = (Block) pair.Second;
1566 indexer = new Indexer (decl.type, decl.interface_type, (int) $2, decl.param_list,
1567 get_block, set_block, (Attributes) $1, (Location) $5);
1569 // Note that there is no equivalent of CheckDef for this case
1570 // We shall handle this in semantic analysis
1572 current_container.AddIndexer (indexer);
1574 current_local_parameters = null;
1575 implicit_value_parameter_type = null;
1576 indexer_parameters = null;
1581 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1583 Parameters pars = (Parameters) $4;
1585 if (pars.FixedParameters == null){
1586 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1589 $$ = new IndexerDeclaration ((string) $1, null, pars);
1591 | type qualified_identifier DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1593 Parameters pars = (Parameters) $6;
1595 if (pars.FixedParameters == null){
1596 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1598 $$ = new IndexerDeclaration ((string) $1, (string) $2, pars);
1610 string full_name = MakeName ((string) $4);
1611 Enum e = new Enum (current_container, (string) $5, (int) $2, full_name, (Attributes) $1, lexer.Location);
1613 foreach (VariableDeclaration ev in (ArrayList) $6){
1614 CheckDef (e.AddEnumMember (ev.identifier,
1615 (Expression) ev.expression_or_array_initializer,
1616 (Location) ev.Location),
1620 e.Namespace = current_namespace;
1622 CheckDef (current_container.AddEnum (e), full_name);
1623 RootContext.Tree.RecordEnum (full_name, e);
1629 : /* empty */ { $$ = "System.Int32"; }
1630 | COLON type { $$ = $2; }
1634 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1640 opt_enum_member_declarations
1641 : /* empty */ { $$ = new ArrayList (); }
1642 | enum_member_declarations opt_comma { $$ = $1; }
1645 enum_member_declarations
1646 : enum_member_declaration
1648 ArrayList l = new ArrayList ();
1653 | enum_member_declarations COMMA enum_member_declaration
1655 ArrayList l = (ArrayList) $1;
1663 enum_member_declaration
1664 : opt_attributes IDENTIFIER
1666 $$ = new VariableDeclaration ((string) $2, null, lexer.Location);
1668 | opt_attributes IDENTIFIER
1670 $$ = lexer.Location;
1674 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location);
1678 delegate_declaration
1682 IDENTIFIER OPEN_PARENS
1683 opt_formal_parameter_list
1687 Delegate del = new Delegate ((string) $4, (int) $2,
1688 MakeName ((string) $5), (Parameters) $7,
1689 (Attributes) $1, lexer.Location);
1691 CheckDef (current_container.AddDelegate (del), del.Name);
1696 IDENTIFIER OPEN_PARENS
1697 opt_formal_parameter_list
1701 Delegate del = new Delegate ("System.Void", (int) $2, (string) $5, (Parameters) $7,
1702 (Attributes) $1, lexer.Location);
1704 CheckDef (current_container.AddDelegate (del), del.Name);
1709 : namespace_or_type_name
1712 namespace_or_type_name
1713 : qualified_identifier
1717 * Before you think of adding a return_type, notice that we have been
1718 * using two rules in the places where it matters (one rule using type
1719 * and another identical one that uses VOID as the return type). This
1720 * gets rid of a shift/reduce couple
1723 : type_name { /* class_type */
1725 This does interfaces, delegates, struct_types, class_types,
1726 parent classes, and more! 4.2
1740 // Note that here only unmanaged types are allowed but we
1741 // can't perform checks during this phase - we do it during
1742 // semantic analysis.
1744 $$ = (string) $1 + "*";
1748 $$ = "System.Void*";
1755 $$ = new SimpleName ((string) $1, lexer.Location);
1757 | non_expression_type rank_specifier
1759 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1761 | non_expression_type STAR
1763 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1765 | expression rank_specifiers
1767 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1771 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1778 ArrayList types = new ArrayList ();
1783 | type_list COMMA type
1785 ArrayList types = (ArrayList) $1;
1793 * replaces all the productions for isolating the various
1794 * simple types, but we need this to reuse it easily in local_variable_type
1797 : OBJECT { $$ = "System.Object"; }
1798 | STRING { $$ = "System.String"; }
1799 | BOOL { $$ = "System.Boolean"; }
1800 | DECIMAL { $$ = "System.Decimal"; }
1801 | FLOAT { $$ = "System.Single"; }
1802 | DOUBLE { $$ = "System.Double"; }
1807 : SBYTE { $$ = "System.SByte"; }
1808 | BYTE { $$ = "System.Byte"; }
1809 | SHORT { $$ = "System.Int16"; }
1810 | USHORT { $$ = "System.UInt16"; }
1811 | INT { $$ = "System.Int32"; }
1812 | UINT { $$ = "System.UInt32"; }
1813 | LONG { $$ = "System.Int64"; }
1814 | ULONG { $$ = "System.UInt64"; }
1815 | CHAR { $$ = "System.Char"; }
1816 | VOID { $$ = "System.Void"; }
1824 : type rank_specifiers
1826 $$ = (string) $1 + (string) $2;
1831 // Expressions, section 7.5
1839 | qualified_identifier
1841 string name = (string) $1;
1844 $$ = DecomposeQI (name, lexer.Location);
1846 | parenthesized_expression
1848 | invocation_expression
1852 | post_increment_expression
1853 | post_decrement_expression
1857 | checked_expression
1858 | unchecked_expression
1859 | pointer_member_access
1860 // TODO: pointer_element_access
1861 // TODO: sizeof-expression
1868 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
1869 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
1870 | NULL { $$ = NullLiteral.Null; }
1874 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
1875 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
1876 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
1881 object v = lexer.Value;
1884 // FIXME: Possible optimization would be to
1885 // compute the *Literal objects directly in the scanner
1888 $$ = new IntLiteral ((Int32) v);
1890 $$ = new UIntLiteral ((UInt32) v);
1892 $$ = new LongLiteral ((Int64) v);
1893 else if (v is ulong)
1894 $$ = new ULongLiteral ((UInt64) v);
1896 Console.WriteLine ("OOPS. Unexpected result from scanner");
1901 : TRUE { $$ = new BoolLiteral (true); }
1902 | FALSE { $$ = new BoolLiteral (false); }
1905 parenthesized_expression
1906 : OPEN_PARENS expression CLOSE_PARENS
1911 : primary_expression DOT IDENTIFIER
1913 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1915 | predefined_type DOT IDENTIFIER
1917 $$ = new MemberAccess (new SimpleName ((string) $1, lexer.Location), (string) $3, lexer.Location);
1925 invocation_expression
1926 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
1929 Location l = lexer.Location;
1930 Report.Error (1, l, "THIS IS CRAZY");
1932 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
1937 : /* empty */ { $$ = null; }
1944 ArrayList list = new ArrayList ();
1948 | argument_list COMMA argument
1950 ArrayList list = (ArrayList) $1;
1959 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
1961 | REF variable_reference
1963 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
1965 | OUT variable_reference
1967 $$ = new Argument ((Expression) $2, Argument.AType.Out);
1972 : expression { note ("section 5.4"); $$ = $1; }
1976 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
1978 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
1980 | primary_expression rank_specifiers
1982 // So the super-trick is that primary_expression
1983 // can only be either a SimpleName or a MemberAccess.
1984 // The MemberAccess case arises when you have a fully qualified type-name like :
1986 // SimpleName is when you have
1989 Expression expr = (Expression) $1;
1990 if (!(expr is SimpleName || expr is MemberAccess)) {
1991 Location l = lexer.Location;
1992 Report.Error (-1, l, "Invalid Type definition");
1993 $$ = "System.Object";
1997 // So we extract the string corresponding to the SimpleName
2000 $$ = new SimpleName (GetQualifiedIdentifier (expr) + (string) $2, lexer.Location);
2007 ArrayList list = new ArrayList ();
2011 | expression_list COMMA expression
2013 ArrayList list = (ArrayList) $1;
2022 $$ = new This (lexer.Location);
2027 : BASE DOT IDENTIFIER
2029 $$ = new BaseAccess ((string) $3, lexer.Location);
2031 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2033 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2037 post_increment_expression
2038 : primary_expression OP_INC
2040 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2041 (Expression) $1, lexer.Location);
2045 post_decrement_expression
2046 : primary_expression OP_DEC
2048 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2049 (Expression) $1, lexer.Location);
2054 : object_or_delegate_creation_expression
2055 | array_creation_expression
2058 object_or_delegate_creation_expression
2059 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2061 $$ = new New ((string) $2, (ArrayList) $4, lexer.Location);
2065 array_creation_expression
2066 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2068 opt_array_initializer
2070 $$ = new ArrayCreation ((string) $2, (ArrayList) $4, (string) $6, (ArrayList) $7,
2073 | NEW type rank_specifiers array_initializer
2075 $$ = new ArrayCreation ((string) $2, (string) $3, (ArrayList) $4, lexer.Location);
2079 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2099 | rank_specifiers rank_specifier
2101 $$ = (string) $2 + (string) $1;
2106 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2108 $$ = "[" + (string) $2 + "]";
2128 | dim_separators COMMA
2130 $$ = (string) $1 + ",";
2134 opt_array_initializer
2146 : OPEN_BRACE CLOSE_BRACE
2148 ArrayList list = new ArrayList ();
2151 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2153 $$ = (ArrayList) $2;
2157 variable_initializer_list
2158 : variable_initializer
2160 ArrayList list = new ArrayList ();
2164 | variable_initializer_list COMMA variable_initializer
2166 ArrayList list = (ArrayList) $1;
2173 : TYPEOF OPEN_PARENS type CLOSE_PARENS
2175 $$ = new TypeOf ((string) $3, lexer.Location);
2180 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2181 $$ = new SizeOf ((string) $3, lexer.Location);
2183 note ("Verify type is unmanaged");
2184 note ("if (5.8) builtin, yield constant expression");
2189 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2191 $$ = new CheckedExpr ((Expression) $3);
2195 unchecked_expression
2196 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2198 $$ = new UnCheckedExpr ((Expression) $3);
2202 pointer_member_access
2203 : primary_expression OP_PTR IDENTIFIER
2207 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2208 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2212 : primary_expression
2213 | BANG prefixed_unary_expression
2215 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2217 | TILDE prefixed_unary_expression
2219 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2221 | OPEN_PARENS expression CLOSE_PARENS unary_expression
2223 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2225 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2227 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2232 // The idea to split this out is from Rhys' grammar
2233 // to solve the problem with casts.
2235 prefixed_unary_expression
2237 | PLUS prefixed_unary_expression
2239 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2241 | MINUS prefixed_unary_expression
2243 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2245 | OP_INC prefixed_unary_expression
2247 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2248 (Expression) $2, lexer.Location);
2250 | OP_DEC prefixed_unary_expression
2252 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2253 (Expression) $2, lexer.Location);
2255 | STAR prefixed_unary_expression
2257 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2259 | BITWISE_AND prefixed_unary_expression
2261 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2265 pre_increment_expression
2266 : OP_INC prefixed_unary_expression
2268 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2269 (Expression) $2, lexer.Location);
2273 pre_decrement_expression
2274 : OP_DEC prefixed_unary_expression
2276 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2277 (Expression) $2, lexer.Location);
2281 multiplicative_expression
2282 : prefixed_unary_expression
2283 | multiplicative_expression STAR prefixed_unary_expression
2285 $$ = new Binary (Binary.Operator.Multiply,
2286 (Expression) $1, (Expression) $3, lexer.Location);
2288 | multiplicative_expression DIV prefixed_unary_expression
2290 $$ = new Binary (Binary.Operator.Division,
2291 (Expression) $1, (Expression) $3, lexer.Location);
2293 | multiplicative_expression PERCENT prefixed_unary_expression
2295 $$ = new Binary (Binary.Operator.Modulus,
2296 (Expression) $1, (Expression) $3, lexer.Location);
2301 : multiplicative_expression
2302 | additive_expression PLUS multiplicative_expression
2304 $$ = new Binary (Binary.Operator.Addition,
2305 (Expression) $1, (Expression) $3, lexer.Location);
2307 | additive_expression MINUS multiplicative_expression
2309 $$ = new Binary (Binary.Operator.Subtraction,
2310 (Expression) $1, (Expression) $3, lexer.Location);
2315 : additive_expression
2316 | shift_expression OP_SHIFT_LEFT additive_expression
2318 $$ = new Binary (Binary.Operator.LeftShift,
2319 (Expression) $1, (Expression) $3, lexer.Location);
2321 | shift_expression OP_SHIFT_RIGHT additive_expression
2323 $$ = new Binary (Binary.Operator.RightShift,
2324 (Expression) $1, (Expression) $3, lexer.Location);
2328 relational_expression
2330 | relational_expression OP_LT shift_expression
2332 $$ = new Binary (Binary.Operator.LessThan,
2333 (Expression) $1, (Expression) $3, lexer.Location);
2335 | relational_expression OP_GT shift_expression
2337 $$ = new Binary (Binary.Operator.GreaterThan,
2338 (Expression) $1, (Expression) $3, lexer.Location);
2340 | relational_expression OP_LE shift_expression
2342 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2343 (Expression) $1, (Expression) $3, lexer.Location);
2345 | relational_expression OP_GE shift_expression
2347 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2348 (Expression) $1, (Expression) $3, lexer.Location);
2350 | relational_expression IS type
2352 $$ = new Is ((Expression) $1, (string) $3, lexer.Location);
2354 | relational_expression AS type
2356 $$ = new As ((Expression) $1, (string) $3, lexer.Location);
2361 : relational_expression
2362 | equality_expression OP_EQ relational_expression
2364 $$ = new Binary (Binary.Operator.Equality,
2365 (Expression) $1, (Expression) $3, lexer.Location);
2367 | equality_expression OP_NE relational_expression
2369 $$ = new Binary (Binary.Operator.Inequality,
2370 (Expression) $1, (Expression) $3, lexer.Location);
2375 : equality_expression
2376 | and_expression BITWISE_AND equality_expression
2378 $$ = new Binary (Binary.Operator.BitwiseAnd,
2379 (Expression) $1, (Expression) $3, lexer.Location);
2383 exclusive_or_expression
2385 | exclusive_or_expression CARRET and_expression
2387 $$ = new Binary (Binary.Operator.ExclusiveOr,
2388 (Expression) $1, (Expression) $3, lexer.Location);
2392 inclusive_or_expression
2393 : exclusive_or_expression
2394 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2396 $$ = new Binary (Binary.Operator.BitwiseOr,
2397 (Expression) $1, (Expression) $3, lexer.Location);
2401 conditional_and_expression
2402 : inclusive_or_expression
2403 | conditional_and_expression OP_AND inclusive_or_expression
2405 $$ = new Binary (Binary.Operator.LogicalAnd,
2406 (Expression) $1, (Expression) $3, lexer.Location);
2410 conditional_or_expression
2411 : conditional_and_expression
2412 | conditional_or_expression OP_OR conditional_and_expression
2414 $$ = new Binary (Binary.Operator.LogicalOr,
2415 (Expression) $1, (Expression) $3, lexer.Location);
2419 conditional_expression
2420 : conditional_or_expression
2421 | conditional_or_expression INTERR expression COLON expression
2423 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2427 assignment_expression
2428 : prefixed_unary_expression ASSIGN expression
2430 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2432 | prefixed_unary_expression OP_MULT_ASSIGN expression
2434 Location l = lexer.Location;
2436 $$ = new Assign ((Expression) $1,
2437 new Binary (Binary.Operator.Multiply,
2439 (Expression) $3, l), l);
2441 | prefixed_unary_expression OP_DIV_ASSIGN expression
2443 Location l = lexer.Location;
2445 $$ = new Assign ((Expression) $1,
2446 new Binary (Binary.Operator.Division,
2448 (Expression) $3, l), l);
2450 | prefixed_unary_expression OP_MOD_ASSIGN expression
2452 Location l = lexer.Location;
2454 $$ = new Assign ((Expression) $1,
2455 new Binary (Binary.Operator.Modulus,
2457 (Expression) $3, l), l);
2459 | prefixed_unary_expression OP_ADD_ASSIGN expression
2461 Location l = lexer.Location;
2463 $$ = new Assign ((Expression) $1,
2464 new Binary (Binary.Operator.Addition,
2466 (Expression) $3, l), l);
2468 | prefixed_unary_expression OP_SUB_ASSIGN expression
2470 Location l = lexer.Location;
2472 $$ = new Assign ((Expression) $1,
2473 new Binary (Binary.Operator.Subtraction,
2475 (Expression) $3, l), l);
2477 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2479 Location l = lexer.Location;
2481 $$ = new Assign ((Expression) $1,
2482 new Binary (Binary.Operator.LeftShift,
2484 (Expression) $3, l), l);
2486 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2488 Location l = lexer.Location;
2490 $$ = new Assign ((Expression) $1,
2491 new Binary (Binary.Operator.RightShift,
2493 (Expression) $3, l), l);
2495 | prefixed_unary_expression OP_AND_ASSIGN expression
2497 Location l = lexer.Location;
2499 $$ = new Assign ((Expression) $1,
2500 new Binary (Binary.Operator.BitwiseAnd,
2502 (Expression) $3, l), l);
2504 | prefixed_unary_expression OP_OR_ASSIGN expression
2506 Location l = lexer.Location;
2508 $$ = new Assign ((Expression) $1,
2509 new Binary (Binary.Operator.BitwiseOr,
2511 (Expression) $3, l), l);
2513 | prefixed_unary_expression OP_XOR_ASSIGN expression
2515 Location l = lexer.Location;
2517 $$ = new Assign ((Expression) $1,
2518 new Binary (Binary.Operator.ExclusiveOr,
2520 (Expression) $3, l), l);
2525 : conditional_expression
2526 | assignment_expression
2548 name = MakeName ((string) $4);
2550 new_class = new Class (current_container, name, (int) $2,
2551 (Attributes) $1, lexer.Location);
2552 current_container = new_class;
2553 current_container.Namespace = current_namespace;
2554 RootContext.Tree.RecordClass (name, new_class);
2560 Class new_class = (Class) current_container;
2563 new_class.Bases = (ArrayList) $6;
2565 current_container = current_container.Parent;
2566 CheckDef (current_container.AddClass (new_class), new_class.Name);
2573 : /* empty */ { $$ = (int) 0; }
2579 | modifiers modifier
2584 if ((m1 & m2) != 0) {
2585 Location l = lexer.Location;
2586 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2588 $$ = (int) (m1 | m2);
2593 : NEW { $$ = Modifiers.NEW; }
2594 | PUBLIC { $$ = Modifiers.PUBLIC; }
2595 | PROTECTED { $$ = Modifiers.PROTECTED; }
2596 | INTERNAL { $$ = Modifiers.INTERNAL; }
2597 | PRIVATE { $$ = Modifiers.PRIVATE; }
2598 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2599 | SEALED { $$ = Modifiers.SEALED; }
2600 | STATIC { $$ = Modifiers.STATIC; }
2601 | READONLY { $$ = Modifiers.READONLY; }
2602 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2603 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2604 | EXTERN { $$ = Modifiers.EXTERN; }
2605 | VOLATILE { $$ = Modifiers.VOLATILE; }
2606 | UNSAFE { $$ = Modifiers.UNSAFE; }
2610 : /* empty */ { $$ = null; }
2611 | class_base { $$ = $1; }
2615 : COLON type_list { $$ = $2; }
2623 // A block is "contained" on the following places:
2625 // property_declaration as part of the accessor body (get/set)
2626 // operator_declaration
2627 // constructor_declaration
2628 // destructor_declaration
2629 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2634 current_block = new Block (current_block);
2636 opt_statement_list CLOSE_BRACE
2638 while (current_block.Implicit)
2639 current_block = current_block.Parent;
2641 current_block = current_block.Parent;
2652 | statement_list statement
2656 : declaration_statement
2658 if ((Block) $1 != current_block){
2659 current_block.AddStatement ((Statement) $1);
2660 current_block = (Block) $1;
2663 | embedded_statement
2665 Statement s = (Statement) $1;
2668 current_block.AddStatement ((Statement) $1);
2676 | expression_statement
2677 | selection_statement
2678 | iteration_statement
2682 | unchecked_statement
2692 $$ = new EmptyStatement ();
2699 LabeledStatement labeled = new LabeledStatement ((string) $1);
2701 if (!current_block.AddLabel ((string) $1, labeled)){
2702 Location l = lexer.Location;
2703 Report.Error (140, l, "The label '" + ((string) $1) + "' is a duplicate");
2705 current_block.AddStatement (labeled);
2710 declaration_statement
2711 : local_variable_declaration SEMICOLON
2713 DictionaryEntry de = (DictionaryEntry) $1;
2715 $$ = declare_local_variables ((string) de.Key, (ArrayList) de.Value, lexer.Location);
2718 | local_constant_declaration SEMICOLON
2720 DictionaryEntry de = (DictionaryEntry) $1;
2722 $$ = declare_local_constant ((string) de.Key, (VariableDeclaration) de.Value);
2727 * The following is from Rhys' grammar:
2728 * > Types in local variable declarations must be recognized as
2729 * > expressions to prevent reduce/reduce errors in the grammar.
2730 * > The expressions are converted into types during semantic analysis.
2733 : primary_expression opt_rank_specifier
2735 // FIXME: Do something smart here regarding the composition of the type.
2737 // Ok, the above "primary_expression" is there to get rid of
2738 // both reduce/reduce and shift/reduces in the grammar, it should
2739 // really just be "type_name". If you use type_name, a reduce/reduce
2740 // creeps up. If you use qualified_identifier (which is all we need
2741 // really) two shift/reduces appear.
2744 // So the super-trick is that primary_expression
2745 // can only be either a SimpleName or a MemberAccess.
2746 // The MemberAccess case arises when you have a fully qualified type-name like :
2748 // SimpleName is when you have
2751 Expression expr = (Expression) $1;
2752 if (!(expr is SimpleName || expr is MemberAccess)) {
2753 Location l = lexer.Location;
2754 Report.Error (-1, l, "Invalid Type definition");
2755 $$ = "System.Object";
2759 // So we extract the string corresponding to the SimpleName
2762 $$ = GetQualifiedIdentifier (expr) + (string) $2;
2764 | builtin_types opt_rank_specifier
2766 $$ = (string) $1 + (string) $2;
2770 local_variable_pointer_type
2771 : primary_expression STAR
2773 Expression expr = (Expression) $1;
2774 if (!(expr is SimpleName || expr is MemberAccess)) {
2775 Location l = lexer.Location;
2776 Report.Error (-1, l, "Invalid Type definition");
2777 $$ = "System.Object";
2780 $$ = GetQualifiedIdentifier (expr) + "*";
2782 | builtin_types STAR
2784 $$ = (string) $1 + "*";
2788 $$ = "System.Void*";
2790 | local_variable_pointer_type STAR
2792 $$ = (string) $1 + "*";
2796 local_variable_declaration
2797 : local_variable_type variable_declarators
2799 $$ = new DictionaryEntry ($1, $2);
2801 | local_variable_pointer_type opt_rank_specifier variable_declarators
2803 $$ = new DictionaryEntry ((string) $1 + (string) $2, $3);
2807 local_constant_declaration
2808 : CONST local_variable_type constant_declarator
2810 $$ = new DictionaryEntry ($2, $3);
2814 expression_statement
2815 : statement_expression SEMICOLON
2822 // We have to do the wrapping here and not in the case above,
2823 // because statement_expression is used for example in for_statement
2825 statement_expression
2826 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2827 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2828 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2829 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2830 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2831 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2832 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2834 Report.Error (1002, lexer.Location, "Expecting `;'");
2838 object_creation_expression
2839 : object_or_delegate_creation_expression
2840 { note ("complain if this is a delegate maybe?"); }
2849 : if_statement_open if_statement_rest
2858 oob_stack.Push (lexer.Location);
2863 : boolean_expression CLOSE_PARENS
2866 Location l = (Location) oob_stack.Pop ();
2868 $$ = new If ((Expression) $1, (Statement) $3, l);
2870 if (RootContext.WarningLevel >= 3){
2871 if ($3 is EmptyStatement)
2872 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
2876 | boolean_expression CLOSE_PARENS
2877 embedded_statement ELSE embedded_statement
2879 Location l = (Location) oob_stack.Pop ();
2881 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
2886 : SWITCH OPEN_PARENS
2888 oob_stack.Push (lexer.Location);
2890 expression CLOSE_PARENS
2893 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
2909 Report.Error (1522, lexer.Location, "Empty switch block");
2917 ArrayList sections = new ArrayList ();
2922 | switch_sections switch_section
2924 ArrayList sections = (ArrayList) $1;
2934 current_block = new Block (current_block);
2938 while (current_block.Implicit)
2939 current_block = current_block.Parent;
2940 $$ = new SwitchSection ((ArrayList) $1, current_block);
2941 current_block = current_block.Parent;
2948 ArrayList labels = new ArrayList ();
2953 | switch_labels switch_label
2955 ArrayList labels = (ArrayList) ($1);
2963 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
2964 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
2967 1523, lexer.Location,
2968 "The keyword case or default must precede code in switch block");
2982 oob_stack.Push (lexer.Location);
2984 boolean_expression CLOSE_PARENS embedded_statement
2986 Location l = (Location) oob_stack.Pop ();
2987 $$ = new While ((Expression) $4, (Statement) $6, l);
2989 if (RootContext.WarningLevel >= 3){
2990 if ($6 is EmptyStatement)
2991 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
2997 : DO embedded_statement
2998 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
3000 $$ = new Do ((Statement) $2, (Expression) $5, lexer.Location);
3006 opt_for_initializer SEMICOLON
3008 Block assign_block = new Block (current_block);
3009 current_block = assign_block;
3011 if ($3 is DictionaryEntry){
3012 DictionaryEntry de = (DictionaryEntry) $3;
3014 string type = (string) de.Key;
3015 ArrayList var_declarators = (ArrayList) de.Value;
3017 foreach (VariableDeclaration decl in var_declarators){
3021 vi = current_block.AddVariable (
3022 type, decl.identifier, current_local_parameters, decl.Location);
3024 Report.Error (128, decl.Location,
3025 "A local variable `" + decl.identifier +
3026 "' is already defined in this scope");
3028 Location l = lexer.Location;
3030 if (decl.expression_or_array_initializer is Expression){
3031 expr = (Expression) decl.expression_or_array_initializer;
3035 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3037 string base_type = type.Substring (0, type.IndexOf ("["));
3038 string rank = type.Substring (type.IndexOf ("["));
3040 expr = new ArrayCreation (base_type, rank, init, decl.Location);
3043 LocalVariableReference var;
3044 var = new LocalVariableReference (
3045 assign_block, decl.identifier, l);
3047 Assign a = new Assign (var, expr, decl.Location);
3049 assign_block.AddStatement (new StatementExpression (a));
3055 oob_stack.Push (lexer.Location);
3057 opt_for_condition SEMICOLON
3058 opt_for_iterator CLOSE_PARENS
3061 Location l = (Location) oob_stack.Pop ();
3063 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3065 if (RootContext.WarningLevel >= 3){
3066 if ($10 is EmptyStatement)
3067 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3070 current_block.AddStatement (f);
3071 while (current_block.Implicit)
3072 current_block = current_block.Parent;
3074 current_block = current_block.Parent;
3079 : /* empty */ { $$ = new EmptyStatement (); }
3084 : local_variable_declaration
3085 | statement_expression_list
3089 : /* empty */ { $$ = null; }
3090 | boolean_expression
3094 : /* empty */ { $$ = new EmptyStatement (); }
3099 : statement_expression_list
3102 statement_expression_list
3103 : statement_expression
3105 Block b = new Block (null, true);
3107 b.AddStatement ((Statement) $1);
3110 | statement_expression_list COMMA statement_expression
3112 Block b = (Block) $1;
3114 b.AddStatement ((Statement) $3);
3120 : FOREACH OPEN_PARENS type IDENTIFIER IN
3122 oob_stack.Push (lexer.Location);
3124 expression CLOSE_PARENS
3126 oob_stack.Push (current_block);
3128 Block foreach_block = new Block (current_block, true);
3129 LocalVariableReference v;
3130 Location l = lexer.Location;
3133 vi = foreach_block.AddVariable ((string) $3, (string) $4, current_local_parameters, l);
3136 128, l, "A local variable `" + (string) $4 + "' is already "+
3137 "defined in this scope");
3140 v = new LocalVariableReference (foreach_block, (string) $4, l);
3142 current_block.AddStatement (foreach_block);
3143 current_block = foreach_block;
3149 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3150 Block prev_block = (Block) oob_stack.Pop ();
3151 Location l = (Location) oob_stack.Pop ();
3153 while (current_block != prev_block)
3154 current_block = current_block.Parent;
3156 $$ = new Foreach ((string) $3, v, (Expression) $7, (Statement) $10, l);
3162 | continue_statement
3171 $$ = new Break (lexer.Location);
3176 : CONTINUE SEMICOLON
3178 $$ = new Continue (lexer.Location);
3183 : GOTO IDENTIFIER SEMICOLON
3185 $$ = new Goto (current_block, (string) $2, lexer.Location);
3187 | GOTO CASE constant_expression SEMICOLON
3189 $$ = new GotoCase ((Expression) $3, lexer.Location);
3191 | GOTO DEFAULT SEMICOLON
3193 $$ = new GotoDefault (lexer.Location);
3198 : RETURN opt_expression SEMICOLON
3200 $$ = new Return ((Expression) $2, lexer.Location);
3205 : THROW opt_expression SEMICOLON
3207 $$ = new Throw ((Expression) $2, lexer.Location);
3217 : TRY block catch_clauses
3220 ArrayList s = new ArrayList ();
3222 foreach (Catch cc in (ArrayList) $3) {
3223 if (cc.Type == null)
3229 // Now s contains the list of specific catch clauses
3230 // and g contains the general one.
3232 $$ = new Try ((Block) $2, s, g, null);
3234 | TRY block opt_catch_clauses FINALLY block
3237 ArrayList s = new ArrayList ();
3238 ArrayList catch_list = (ArrayList) $3;
3240 if (catch_list != null){
3241 foreach (Catch cc in catch_list) {
3242 if (cc.Type == null)
3249 $$ = new Try ((Block) $2, s, g, (Block) $5);
3253 Report.Error (1524, lexer.Location, "Expected catch or finally");
3258 : /* empty */ { $$ = null; }
3265 ArrayList l = new ArrayList ();
3270 | catch_clauses catch_clause
3272 ArrayList l = (ArrayList) $1;
3280 : /* empty */ { $$ = null; }
3285 : CATCH opt_catch_args
3287 string type = null, id = null;
3290 DictionaryEntry cc = (DictionaryEntry) $2;
3291 type = (string) cc.Key;
3292 id = (string) cc.Value;
3295 ArrayList one = new ArrayList ();
3296 Location loc = lexer.Location;
3298 one.Add (new VariableDeclaration (id, null, loc));
3301 current_block = new Block (current_block);
3302 Block b = declare_local_variables (type, one, loc);
3309 string type = null, id = null;
3312 DictionaryEntry cc = (DictionaryEntry) $2;
3313 type = (string) cc.Key;
3314 id = (string) cc.Value;
3317 while (current_block != (Block) $1)
3318 current_block = current_block.Parent;
3323 $$ = new Catch (type, id , (Block) $4, lexer.Location);
3328 : /* empty */ { $$ = null; }
3333 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3335 $$ = new DictionaryEntry ($2, $3);
3342 $$ = new Checked ((Block) $2);
3349 $$ = new Unchecked ((Block) $2);
3356 if (!RootContext.Unsafe){
3357 Report.Error (227, lexer.Location,
3358 "Unsafe code can only be used if --unsafe is used");
3361 $$ = new Unsafe ((Block) $3);
3367 pointer_type fixed_pointer_declarators
3370 Block assign_block = new Block (current_block, true);
3371 ArrayList list = (ArrayList) $4;
3372 string type = (string) $3;
3373 Location l = lexer.Location;
3374 int top = list.Count;
3376 for (int i = 0; i < top; i++){
3377 Pair p = (Pair) list [i];
3380 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3383 128, l, "A local variable `" + (string) p.First + "' is already "+
3384 "defined in this scope");
3390 current_block.AddStatement (assign_block);
3391 current_block = assign_block;
3392 oob_stack.Push (assign_block);
3397 Location l = (Location) oob_stack.Pop ();
3398 Block assign_block = (Block) oob_stack.Pop ();
3400 ArrayList list = (ArrayList) $4;
3401 int top = list.Count;
3403 $$ = new Fixed ((string) $3, (ArrayList) $4, (Statement) $7, l);
3407 fixed_pointer_declarators
3408 : fixed_pointer_declarator {
3409 ArrayList declarators = new ArrayList ();
3410 declarators.Add ($1);
3413 | fixed_pointer_declarators COMMA fixed_pointer_declarator
3415 ArrayList declarators = (ArrayList) $1;
3416 declarators.Add ($3);
3421 fixed_pointer_declarator
3422 : IDENTIFIER ASSIGN expression
3424 $$ = new Pair ($1, $3);
3429 : LOCK OPEN_PARENS expression CLOSE_PARENS
3435 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
3440 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
3442 Block assign_block = new Block (current_block);
3443 current_block = assign_block;
3445 oob_stack.Push (lexer.Location);
3447 if ($3 is DictionaryEntry){
3448 DictionaryEntry de = (DictionaryEntry) $3;
3449 Location l = lexer.Location;
3451 string type = (string) de.Key;
3452 ArrayList var_declarators = (ArrayList) de.Value;
3454 foreach (VariableDeclaration decl in var_declarators){
3455 if (current_block.AddVariable (
3456 type, decl.identifier,
3457 current_local_parameters, decl.Location) == null){
3458 Report.Error (128, decl.Location,
3459 "A local variable `" + decl.identifier + "' is already" +
3460 "defined in this scope");
3464 ArrayList vars = new ArrayList ();
3466 foreach (VariableDeclaration decl in var_declarators){
3469 if (decl.expression_or_array_initializer is Expression){
3470 expr = (Expression) decl.expression_or_array_initializer;
3474 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3476 string base_type = type.Substring (0, type.IndexOf ("["));
3477 string rank = type.Substring (type.IndexOf ("["));
3479 expr = new ArrayCreation (base_type, rank, init, decl.Location);
3482 LocalVariableReference var;
3485 var = new LocalVariableReference (assign_block, decl.identifier, l);
3486 vi = var.VariableInfo;
3489 // This is so that it is not a warning on using variables
3492 vars.Add (new DictionaryEntry (var, expr));
3494 // Assign a = new Assign (var, expr, decl.Location);
3495 // assign_block.AddStatement (new StatementExpression (a));
3497 $3 = new DictionaryEntry (type, vars);
3502 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
3503 current_block.AddStatement (u);
3504 while (current_block.Implicit)
3505 current_block = current_block.Parent;
3507 current_block = current_block.Parent;
3511 resource_acquisition
3512 : local_variable_declaration
3519 // A class used to pass around variable declarations and constants
3521 public class VariableDeclaration {
3522 public string identifier;
3523 public object expression_or_array_initializer;
3524 public Location Location;
3526 public VariableDeclaration (string id, object eoai, Location l){
3527 this.identifier = id;
3528 this.expression_or_array_initializer = eoai;
3534 // A class used to hold info about an indexer declarator
3537 public class IndexerDeclaration {
3539 public string interface_type;
3540 public Parameters param_list;
3542 public IndexerDeclaration (string type, string interface_type, Parameters param_list)
3545 this.interface_type = interface_type;
3546 this.param_list = param_list;
3551 // A class used to hold info about an operator declarator
3554 public class OperatorDeclaration {
3555 public Operator.OpType optype;
3556 public string ret_type;
3557 public string arg1type;
3558 public string arg1name;
3559 public string arg2type;
3560 public string arg2name;
3561 public Location location;
3563 public OperatorDeclaration (Operator.OpType op, string ret_type, string arg1type, string arg1name,
3564 string arg2type, string arg2name, Location location)
3567 this.ret_type = ret_type;
3568 this.arg1type = arg1type;
3569 this.arg1name = arg1name;
3570 this.arg2type = arg2type;
3571 this.arg2name = arg2name;
3572 this.location = location;
3578 // Given the @class_name name, it creates a fully qualified name
3579 // based on the containing declaration space
3582 MakeName (string class_name)
3584 string ns = current_namespace.Name;
3585 string container_name = current_container.Name;
3587 if (container_name == ""){
3589 return ns + "." + class_name;
3593 return container_name + "." + class_name;
3597 // Used to report back to the user the result of a declaration
3598 // in the current declaration space
3601 CheckDef (AdditionResult result, string name)
3603 if (result == AdditionResult.Success)
3606 Location l = lexer.Location;
3609 case AdditionResult.NameExists:
3610 Report.Error (102, l, "The namespace `" + current_container.Name +
3611 "' already contains a definition for `"+
3617 // This is handled only for static Constructors, because
3618 // in reality we handle these by the semantic analysis later
3620 case AdditionResult.MethodExists:
3622 111, l, "Class `"+current_container.Name+
3623 "' already defines a member called '" +
3624 name + "' with the same parameter types (more than one default constructor)");
3627 case AdditionResult.EnclosingClash:
3628 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
3631 case AdditionResult.NotAConstructor:
3632 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
3638 CheckDef (bool result, string name)
3642 CheckDef (AdditionResult.NameExists, name);
3646 SimpleLookup (string name, Location loc)
3649 // we need to check against current_block not being null
3650 // as `expression' is allowed in argument_lists, which
3651 // do not exist inside a block.
3653 if (current_block != null){
3654 if (current_block.IsVariableDefined (name))
3655 return new LocalVariableReference (current_block, name, loc);
3658 if (current_local_parameters != null){
3660 Parameter par = current_local_parameters.GetParameterByName (name, out idx);
3662 return new ParameterReference (current_local_parameters, idx, name);
3668 Expression DecomposeQI (string name, Location loc)
3672 if (name.IndexOf ('.') == -1){
3673 o = SimpleLookup (name, loc);
3675 return new SimpleName (name, loc);
3678 int pos = name.LastIndexOf (".");
3679 string left = name.Substring (0, pos);
3680 string right = name.Substring (pos + 1);
3682 o = DecomposeQI (left, loc);
3683 return new MemberAccess (o, right, loc);
3688 // This method is used to get at the complete string representation of
3689 // a fully-qualified type name, hiding inside a MemberAccess ;-)
3690 // This is necessary because local_variable_type admits primary_expression
3691 // as the type of the variable. So we do some extra checking
3693 string GetQualifiedIdentifier (Expression expr)
3695 if (expr is SimpleName)
3696 return ((SimpleName)expr).Name;
3697 else if (expr is MemberAccess)
3698 return GetQualifiedIdentifier (((MemberAccess)expr).Expr) + "." + ((MemberAccess) expr).Identifier;
3700 throw new Exception ("Expr has to be either SimpleName or MemberAccess! (" + expr + ")");
3704 Block declare_local_variables (string type, ArrayList variable_declarators, Location loc)
3706 Block implicit_block;
3707 ArrayList inits = null;
3710 // We use the `Used' property to check whether statements
3711 // have been added to the current block. If so, we need
3712 // to create another block to contain the new declaration
3713 // otherwise, as an optimization, we use the same block to
3714 // add the declaration.
3716 // FIXME: A further optimization is to check if the statements
3717 // that were added were added as part of the initialization
3718 // below. In which case, no other statements have been executed
3719 // and we might be able to reduce the number of blocks for
3720 // situations like this:
3722 // int j = 1; int k = j + 1;
3724 if (current_block.Used)
3725 implicit_block = new Block (current_block, true);
3727 implicit_block = current_block;
3729 foreach (VariableDeclaration decl in variable_declarators){
3731 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
3732 if (decl.expression_or_array_initializer != null){
3734 inits = new ArrayList ();
3738 Report.Error (128, decl.Location, "A local variable `" + decl.identifier +
3739 "' is already defined in this scope");
3744 return implicit_block;
3746 foreach (VariableDeclaration decl in inits){
3750 if (decl.expression_or_array_initializer is Expression){
3751 expr = (Expression) decl.expression_or_array_initializer;
3754 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3756 string base_type = type.Substring (0, type.IndexOf ("["));
3757 string rank = type.Substring (type.IndexOf ("["));
3759 expr = new ArrayCreation (base_type, rank, init, decl.Location);
3763 LocalVariableReference var;
3764 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
3766 assign = new Assign (var, expr, decl.Location);
3768 implicit_block.AddStatement (new StatementExpression (assign));
3771 return implicit_block;
3774 Block declare_local_constant (string type, VariableDeclaration decl)
3776 Block implicit_block;
3778 if (current_block.Used)
3779 implicit_block = new Block (current_block, true);
3781 implicit_block = current_block;
3783 if (!(implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
3784 current_local_parameters, decl.Location))){
3785 Report.Error (128, decl.Location, "A local variable `" + decl.identifier +
3786 "' is already defined in this scope");
3789 return implicit_block;
3792 void CheckAttributeTarget (string a)
3796 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
3800 Location l = lexer.Location;
3801 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
3807 void CheckUnaryOperator (Operator.OpType op)
3811 case Operator.OpType.LogicalNot:
3812 case Operator.OpType.OnesComplement:
3813 case Operator.OpType.Increment:
3814 case Operator.OpType.Decrement:
3815 case Operator.OpType.True:
3816 case Operator.OpType.False:
3817 case Operator.OpType.Addition:
3818 case Operator.OpType.Subtraction:
3823 Location l = lexer.Location;
3824 Report.Error (1019, l, "Overloadable unary operator expected");
3830 void CheckBinaryOperator (Operator.OpType op)
3834 case Operator.OpType.Addition:
3835 case Operator.OpType.Subtraction:
3836 case Operator.OpType.Multiply:
3837 case Operator.OpType.Division:
3838 case Operator.OpType.Modulus:
3839 case Operator.OpType.BitwiseAnd:
3840 case Operator.OpType.BitwiseOr:
3841 case Operator.OpType.ExclusiveOr:
3842 case Operator.OpType.LeftShift:
3843 case Operator.OpType.RightShift:
3844 case Operator.OpType.Equality:
3845 case Operator.OpType.Inequality:
3846 case Operator.OpType.GreaterThan:
3847 case Operator.OpType.LessThan:
3848 case Operator.OpType.GreaterThanOrEqual:
3849 case Operator.OpType.LessThanOrEqual:
3853 Location l = lexer.Location;
3854 Report.Error (1020, l, "Overloadable binary operator expected");
3860 void syntax_error (Location l, string msg)
3862 Report.Error (1003, l, "Syntax error, " + msg);
3865 void output (string s)
3867 Console.WriteLine (s);
3870 void note (string s)
3872 // Used to put annotations
3877 public Tokenizer Lexer {
3883 public CSharpParser (string name, System.IO.Stream input, ArrayList defines)
3885 current_namespace = new Namespace (null, "");
3888 current_container = RootContext.Tree.Types;
3889 current_container.Namespace = current_namespace;
3890 oob_stack = new Stack ();
3892 lexer = new Tokenizer (input, name, defines);
3895 public override int parse ()
3897 StringBuilder value = new StringBuilder ();
3901 if (yacc_verbose_flag)
3902 yyparse (lexer, new yydebug.yyDebugSimple ());
3905 } catch (Exception e){
3906 // Console.WriteLine ("Fatal error: " + name);
3907 // Console.WriteLine (lexer.location);
3910 // Please do not remove this, it is used during debugging
3913 Console.WriteLine (lexer.location + " : Parsing error ");
3914 Console.WriteLine (e);
3918 return global_errors;