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 : positional_argument_list
502 ArrayList args = new ArrayList ();
507 | positional_argument_list COMMA named_argument_list
509 ArrayList args = new ArrayList ();
515 | named_argument_list
517 ArrayList args = new ArrayList ();
526 positional_argument_list
529 ArrayList args = new ArrayList ();
530 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
534 | positional_argument_list COMMA expression
536 ArrayList args = (ArrayList) $1;
537 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
546 ArrayList args = new ArrayList ();
551 | named_argument_list COMMA named_argument
553 ArrayList args = (ArrayList) $1;
561 : IDENTIFIER ASSIGN expression
563 $$ = new DictionaryEntry (
565 new Argument ((Expression) $3, Argument.AType.Expression));
571 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
574 opt_class_member_declarations
576 | class_member_declarations
579 class_member_declarations
580 : class_member_declaration
581 | class_member_declarations
582 class_member_declaration
585 class_member_declaration
586 : constant_declaration // done
587 | field_declaration // done
588 | method_declaration // done
589 | property_declaration // done
590 | event_declaration // done
591 | indexer_declaration // done
592 | operator_declaration // done
593 | constructor_declaration // done
594 | destructor_declaration // done
604 string full_struct_name = MakeName ((string) $4);
606 new_struct = new Struct (current_container, full_struct_name, (int) $2,
607 (Attributes) $1, lexer.Location);
608 current_container = new_struct;
609 current_container.Namespace = current_namespace;
610 RootContext.Tree.RecordStruct (full_struct_name, new_struct);
612 opt_struct_interfaces
616 Struct new_struct = (Struct) current_container;
618 current_container = current_container.Parent;
619 CheckDef (current_container.AddStruct (new_struct), new_struct.Name);
624 opt_struct_interfaces
631 | struct_interfaces struct_interface
639 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
642 opt_struct_member_declarations
644 | struct_member_declarations
647 struct_member_declarations
648 : struct_member_declaration
649 | struct_member_declarations struct_member_declaration
652 struct_member_declaration
653 : constant_declaration
656 | property_declaration
658 | indexer_declaration
659 | operator_declaration
660 | constructor_declaration
664 * This is only included so we can flag error 575:
665 * destructors only allowed on class types
667 | destructor_declaration
678 foreach (VariableDeclaration constant in (ArrayList) $5){
679 Const c = new Const (
680 (string) $4, (string) constant.identifier,
681 (Expression) constant.expression_or_array_initializer, (int) $2, (Attributes) $1,
684 CheckDef (current_container.AddConstant (c), c.Name);
690 : constant_declarator
692 ArrayList constants = new ArrayList ();
696 | constant_declarators COMMA constant_declarator
698 ArrayList constants = (ArrayList) $1;
705 : IDENTIFIER ASSIGN constant_expression
707 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
718 string type = (string) $3;
721 foreach (VariableDeclaration var in (ArrayList) $4){
722 Field field = new Field (type, mod, var.identifier,
723 var.expression_or_array_initializer, (Attributes) $1, var.Location);
725 CheckDef (current_container.AddField (field), field.Name);
731 : variable_declarator
733 ArrayList decl = new ArrayList ();
737 | variable_declarators COMMA variable_declarator
739 ArrayList decls = (ArrayList) $1;
746 : IDENTIFIER ASSIGN variable_initializer
748 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
752 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
765 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
767 $$ = new StackAlloc ((string) $2, (Expression) $4, lexer.Location);
775 Method method = (Method) $1;
776 Block b = (Block) $2;
777 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
780 if ((method.ModFlags & extern_abstract) == 0){
782 501, lexer.Location, current_container.MakeName (method.Name) +
783 "must declare a body because it is not marked abstract or extern");
786 if ((method.ModFlags & Modifiers.EXTERN) != 0){
788 179, lexer.Location, current_container.MakeName (method.Name) +
789 " is declared extern, but has a body");
793 method.Block = (Block) $2;
794 CheckDef (current_container.AddMethod (method), method.Name);
796 current_local_parameters = null;
810 1585, lexer.Location, "Member modifier `" +
811 Modifiers.Name (i) + "' must precede member type and name");
824 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
826 Method method = new Method ((string) $3, (int) $2, (string) $4,
827 (Parameters) $6, (Attributes) $1, lexer.Location);
829 current_local_parameters = (Parameters) $6;
837 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
839 Method method = new Method ("System.Void", (int) $2, (string) $4,
840 (Parameters) $6, (Attributes) $1, lexer.Location);
842 current_local_parameters = (Parameters) $6;
849 | SEMICOLON { $$ = null; }
852 opt_formal_parameter_list
853 : /* empty */ { $$ = Parameters.GetEmptyReadOnlyParameters (); }
854 | formal_parameter_list
857 formal_parameter_list
860 ArrayList pars_list = (ArrayList) $1;
862 Parameter [] pars = new Parameter [pars_list.Count];
863 pars_list.CopyTo (pars);
865 $$ = new Parameters (pars, null, lexer.Location);
867 | fixed_parameters COMMA parameter_array
869 ArrayList pars_list = (ArrayList) $1;
871 Parameter [] pars = new Parameter [pars_list.Count];
872 pars_list.CopyTo (pars);
874 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
878 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
885 ArrayList pars = new ArrayList ();
890 | fixed_parameters COMMA fixed_parameter
892 ArrayList pars = (ArrayList) $1;
901 opt_parameter_modifier
905 $$ = new Parameter ((string) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
909 opt_parameter_modifier
910 : /* empty */ { $$ = Parameter.Modifier.NONE; }
915 : REF { $$ = Parameter.Modifier.REF; }
916 | OUT { $$ = Parameter.Modifier.OUT; }
920 : opt_attributes PARAMS type IDENTIFIER
922 $$ = new Parameter ((string) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
923 note ("type must be a single-dimension array type");
928 : qualified_identifier
937 implicit_value_parameter_type = (string) $3;
939 lexer.PropertyParsing = true;
943 accessor_declarations
945 lexer.PropertyParsing = false;
950 Pair pair = (Pair) $7;
951 Block get_block = null;
952 Block set_block = null;
954 if (pair.First != null)
955 get_block = (Block) pair.First;
956 if (pair.Second != null)
957 set_block = (Block) pair.Second;
959 prop = new Property ((string) $3, (string) $4, (int) $2, get_block, set_block,
960 (Attributes) $1, (Location) $6);
962 CheckDef (current_container.AddProperty (prop), prop.Name);
963 implicit_value_parameter_type = null;
967 accessor_declarations
968 : get_accessor_declaration opt_set_accessor_declaration
970 $$ = new Pair ($1, $2);
972 | set_accessor_declaration opt_get_accessor_declaration
974 $$ = new Pair ($2, $1);
978 opt_get_accessor_declaration
979 : /* empty */ { $$ = null; }
980 | get_accessor_declaration
983 opt_set_accessor_declaration
984 : /* empty */ { $$ = null; }
985 | set_accessor_declaration
988 get_accessor_declaration
991 // If this is not the case, then current_local_parameters has already
992 // been set in indexer_declaration
993 if (parsing_indexer == false)
994 current_local_parameters = null;
996 current_local_parameters = indexer_parameters;
997 lexer.PropertyParsing = false;
1002 current_local_parameters = null;
1003 lexer.PropertyParsing = true;
1007 set_accessor_declaration
1008 : opt_attributes SET
1011 Parameter implicit_value_parameter = new Parameter (
1012 implicit_value_parameter_type, "value",
1013 Parameter.Modifier.NONE, null);
1015 if (parsing_indexer == false) {
1016 args = new Parameter [1];
1017 args [0] = implicit_value_parameter;
1019 Parameter [] fp = indexer_parameters.FixedParameters;
1020 int count = fp.Length;
1022 args = new Parameter [count + 1];
1024 fp.CopyTo (args, 0);
1025 args [count] = implicit_value_parameter;
1028 current_local_parameters = new Parameters (args, null, lexer.Location);
1029 lexer.PropertyParsing = false;
1034 current_local_parameters = null;
1035 lexer.PropertyParsing = true;
1041 | SEMICOLON { $$ = new Block (null); }
1044 interface_declaration
1047 INTERFACE IDENTIFIER
1049 Interface new_interface;
1050 string full_interface_name = MakeName ((string) $4);
1052 new_interface = new Interface (current_container, full_interface_name, (int) $2,
1053 (Attributes) $1, lexer.Location);
1054 if (current_interface != null) {
1055 Location l = lexer.Location;
1056 Report.Error (-2, l, "Internal compiler error: interface inside interface");
1058 current_interface = new_interface;
1059 new_interface.Namespace = current_namespace;
1060 RootContext.Tree.RecordInterface (full_interface_name, new_interface);
1065 Interface new_interface = (Interface) current_interface;
1068 new_interface.Bases = (ArrayList) $6;
1070 current_interface = null;
1071 CheckDef (current_container.AddInterface (new_interface), new_interface.Name);
1076 : /* empty */ { $$ = null; }
1081 : COLON interface_type_list { $$ = $2; }
1087 ArrayList interfaces = new ArrayList ();
1089 interfaces.Add ($1);
1092 | interface_type_list COMMA interface_type
1094 ArrayList interfaces = (ArrayList) $1;
1095 interfaces.Add ($3);
1102 opt_interface_member_declarations
1106 opt_interface_member_declarations
1108 | interface_member_declarations
1111 interface_member_declarations
1112 : interface_member_declaration
1113 | interface_member_declarations interface_member_declaration
1116 interface_member_declaration
1117 : interface_method_declaration
1119 InterfaceMethod m = (InterfaceMethod) $1;
1121 CheckDef (current_interface.AddMethod (m), m.Name);
1123 | interface_property_declaration
1125 InterfaceProperty p = (InterfaceProperty) $1;
1127 CheckDef (current_interface.AddProperty (p), p.Name);
1129 | interface_event_declaration
1131 InterfaceEvent e = (InterfaceEvent) $1;
1133 CheckDef (current_interface.AddEvent (e), e.Name);
1135 | interface_indexer_declaration
1137 InterfaceIndexer i = (InterfaceIndexer) $1;
1139 CheckDef (current_interface.AddIndexer (i), "indexer");
1144 : /* empty */ { $$ = false; }
1145 | NEW { $$ = true; }
1148 interface_method_declaration
1149 : opt_attributes opt_new type IDENTIFIER
1150 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1153 $$ = new InterfaceMethod ((string) $3, (string) $4, (bool) $2,
1154 (Parameters) $6, (Attributes) $1, lexer.Location);
1156 | opt_attributes opt_new VOID IDENTIFIER
1157 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1160 $$ = new InterfaceMethod ("System.Void", (string) $4, (bool) $2, (Parameters) $6,
1161 (Attributes) $1, lexer.Location);
1165 interface_property_declaration
1170 { lexer.PropertyParsing = true; }
1172 { lexer.PropertyParsing = false; }
1177 $$ = new InterfaceProperty ((string) $3, (string) $4, (bool) $2,
1178 (gs & 1) == 1, (gs & 2) == 2, (Attributes) $1,
1184 : opt_attributes GET SEMICOLON { $$ = 1; }
1185 | opt_attributes SET SEMICOLON { $$ = 2; }
1186 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1188 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1192 interface_event_declaration
1193 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1195 $$ = new InterfaceEvent ((string) $4, (string) $5, (bool) $2, (Attributes) $1);
1199 interface_indexer_declaration
1200 : opt_attributes opt_new type THIS
1201 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1203 { lexer.PropertyParsing = true; }
1205 { lexer.PropertyParsing = false; }
1208 int a_flags = (int) $10;
1210 bool do_get = (a_flags & 1) == 1;
1211 bool do_set = (a_flags & 2) == 2;
1213 $$ = new InterfaceIndexer ((string) $3, (Parameters) $6, do_get, do_set,
1214 (bool) $2, (Attributes) $1, lexer.Location);
1218 operator_declaration
1219 : opt_attributes opt_modifiers operator_declarator block
1221 OperatorDeclaration decl = (OperatorDeclaration) $3;
1223 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
1224 decl.arg2type, decl.arg2name, (Block) $4, (Attributes) $1, decl.location);
1226 // Note again, checking is done in semantic analysis
1227 current_container.AddOperator (op);
1229 current_local_parameters = null;
1234 : type OPERATOR overloadable_operator
1235 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1237 Operator.OpType op = (Operator.OpType) $3;
1238 CheckUnaryOperator (op);
1240 if (op == Operator.OpType.Addition)
1241 op = Operator.OpType.UnaryPlus;
1243 if (op == Operator.OpType.Subtraction)
1244 op = Operator.OpType.UnaryNegation;
1246 Parameter [] pars = new Parameter [1];
1248 pars [0] = new Parameter ((string) $5, (string) $6, Parameter.Modifier.NONE, null);
1250 current_local_parameters = new Parameters (pars, null, lexer.Location);
1252 $$ = new OperatorDeclaration (op, (string) $1, (string) $5, (string) $6,
1253 null, null, lexer.Location);
1255 | type OPERATOR overloadable_operator
1257 type IDENTIFIER COMMA
1261 CheckBinaryOperator ((Operator.OpType) $3);
1263 Parameter [] pars = new Parameter [2];
1265 pars [0] = new Parameter ((string) $5, (string) $6, Parameter.Modifier.NONE, null);
1266 pars [1] = new Parameter ((string) $8, (string) $9, Parameter.Modifier.NONE, null);
1268 current_local_parameters = new Parameters (pars, null, lexer.Location);
1270 $$ = new OperatorDeclaration ((Operator.OpType) $3, (string) $1, (string) $5, (string) $6,
1271 (string) $8, (string) $9, lexer.Location);
1273 | conversion_operator_declarator
1276 overloadable_operator
1278 : BANG { $$ = Operator.OpType.LogicalNot; }
1279 | TILDE { $$ = Operator.OpType.OnesComplement; }
1280 | OP_INC { $$ = Operator.OpType.Increment; }
1281 | OP_DEC { $$ = Operator.OpType.Decrement; }
1282 | TRUE { $$ = Operator.OpType.True; }
1283 | FALSE { $$ = Operator.OpType.False; }
1284 // Unary and binary:
1285 | PLUS { $$ = Operator.OpType.Addition; }
1286 | MINUS { $$ = Operator.OpType.Subtraction; }
1288 | STAR { $$ = Operator.OpType.Multiply; }
1289 | DIV { $$ = Operator.OpType.Division; }
1290 | PERCENT { $$ = Operator.OpType.Modulus; }
1291 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1292 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1293 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1294 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1295 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1296 | OP_EQ { $$ = Operator.OpType.Equality; }
1297 | OP_NE { $$ = Operator.OpType.Inequality; }
1298 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1299 | OP_LT { $$ = Operator.OpType.LessThan; }
1300 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1301 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1304 conversion_operator_declarator
1305 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1307 Parameter [] pars = new Parameter [1];
1309 pars [0] = new Parameter ((string) $5, (string) $6, Parameter.Modifier.NONE, null);
1311 current_local_parameters = new Parameters (pars, null, lexer.Location);
1313 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (string) $3, (string) $5, (string) $6,
1314 null, null, lexer.Location);
1316 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1318 Parameter [] pars = new Parameter [1];
1320 pars [0] = new Parameter ((string) $5, (string) $6, Parameter.Modifier.NONE, null);
1322 current_local_parameters = new Parameters (pars, null, lexer.Location);
1324 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (string) $3, (string) $5, (string) $6,
1325 null, null, lexer.Location);
1329 syntax_error (lexer.Location, "'operator' expected");
1333 syntax_error (lexer.Location, "'operator' expected");
1337 constructor_declaration
1340 constructor_declarator
1343 Constructor c = (Constructor) $3;
1344 c.Block = (Block) $4;
1345 c.ModFlags = (int) $2;
1346 c.OptAttributes = (Attributes) $1;
1348 if ((c.ModFlags & Modifiers.STATIC) != 0){
1349 if ((c.ModFlags & Modifiers.Accessibility) != 0) {
1350 Location l = lexer.Location;
1351 Report.Error (515, l, "Access modifiers are not allowed on static constructors");
1354 if (c.Initializer != null){
1355 Location l = lexer.Location;
1356 Report.Error (514, l, "Static constructors can not have an explicit this or base constructor invocations");
1359 if (!c.Parameters.Empty){
1360 Location l = lexer.Location;
1361 Report.Error (132, l, "Static constructors should not have parameters");
1365 CheckDef (current_container.AddConstructor (c), c.Name);
1367 current_local_parameters = null;
1371 constructor_declarator
1373 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1375 oob_stack.Push (lexer.Location);
1377 current_local_parameters = (Parameters) $3;
1379 opt_constructor_initializer
1381 Location l = (Location) oob_stack.Pop ();
1382 $$ = new Constructor ((string) $1, (Parameters) $3, (ConstructorInitializer) $6, l);
1386 opt_constructor_initializer
1387 : /* empty */ { $$ = null; }
1388 | constructor_initializer
1391 constructor_initializer
1392 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1394 $$ = new ConstructorBaseInitializer ((ArrayList) $4, lexer.Location);
1396 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1398 $$ = new ConstructorThisInitializer ((ArrayList) $4, lexer.Location);
1402 destructor_declaration
1403 : opt_attributes TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1405 if ((string) $3 != current_container.Basename){
1406 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1407 } else if (!(current_container is Class)){
1408 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1410 Location l = lexer.Location;
1412 Method d = new Method (
1413 "System.Void", 0, "Finalize",
1414 new Parameters (null, null, l), (Attributes) $1, l);
1416 d.Block = (Block) $6;
1417 CheckDef (current_container.AddMethod (d), d.Name);
1425 EVENT type variable_declarators SEMICOLON
1427 foreach (VariableDeclaration var in (ArrayList) $5) {
1429 Event e = new Event ((string) $4, var.identifier, var.expression_or_array_initializer,
1430 (int) $2, null, null, (Attributes) $1, lexer.Location);
1432 CheckDef (current_container.AddEvent (e), e.Name);
1438 EVENT type member_name
1441 implicit_value_parameter_type = (string) $4;
1442 lexer.EventParsing = true;
1444 event_accessor_declarations
1446 lexer.EventParsing = false;
1450 Pair pair = (Pair) $8;
1451 Block add_block = null;
1452 Block rem_block = null;
1454 if (pair.First != null)
1455 add_block = (Block) pair.First;
1456 if (pair.Second != null)
1457 rem_block = (Block) pair.Second;
1459 Event e = new Event ((string) $4, (string) $5, null, (int) $2, add_block, rem_block,
1460 (Attributes) $1, lexer.Location);
1462 CheckDef (current_container.AddEvent (e), e.Name);
1463 implicit_value_parameter_type = null;
1467 event_accessor_declarations
1468 : add_accessor_declaration remove_accessor_declaration
1470 $$ = new Pair ($1, $2);
1472 | remove_accessor_declaration add_accessor_declaration
1474 $$ = new Pair ($2, $1);
1478 add_accessor_declaration
1479 : opt_attributes ADD
1481 Parameter [] args = new Parameter [1];
1482 Parameter implicit_value_parameter = new Parameter (
1483 implicit_value_parameter_type, "value",
1484 Parameter.Modifier.NONE, null);
1486 args [0] = implicit_value_parameter;
1488 current_local_parameters = new Parameters (args, null, lexer.Location);
1489 lexer.EventParsing = false;
1494 lexer.EventParsing = true;
1498 remove_accessor_declaration
1499 : opt_attributes REMOVE
1501 Parameter [] args = new Parameter [1];
1502 Parameter implicit_value_parameter = new Parameter (
1503 implicit_value_parameter_type, "value",
1504 Parameter.Modifier.NONE, null);
1506 args [0] = implicit_value_parameter;
1508 current_local_parameters = new Parameters (args, null, lexer.Location);
1509 lexer.EventParsing = false;
1514 lexer.EventParsing = true;
1519 : opt_attributes opt_modifiers indexer_declarator
1522 IndexerDeclaration decl = (IndexerDeclaration) $3;
1524 implicit_value_parameter_type = decl.type;
1526 lexer.PropertyParsing = true;
1527 parsing_indexer = true;
1529 indexer_parameters = decl.param_list;
1530 $$ = lexer.Location;
1532 accessor_declarations
1534 lexer.PropertyParsing = false;
1535 parsing_indexer = false;
1539 // The signature is computed from the signature of the indexer. Look
1540 // at section 3.6 on the spec
1543 IndexerDeclaration decl = (IndexerDeclaration) $3;
1544 Pair pair = (Pair) $6;
1545 Block get_block = null;
1546 Block set_block = null;
1548 if (pair.First != null)
1549 get_block = (Block) pair.First;
1550 if (pair.Second != null)
1551 set_block = (Block) pair.Second;
1553 indexer = new Indexer (decl.type, decl.interface_type, (int) $2, decl.param_list,
1554 get_block, set_block, (Attributes) $1, (Location) $5);
1556 // Note that there is no equivalent of CheckDef for this case
1557 // We shall handle this in semantic analysis
1559 current_container.AddIndexer (indexer);
1561 current_local_parameters = null;
1562 implicit_value_parameter_type = null;
1563 indexer_parameters = null;
1568 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1570 Parameters pars = (Parameters) $4;
1572 if (pars.FixedParameters == null){
1573 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1576 $$ = new IndexerDeclaration ((string) $1, null, pars);
1578 | type qualified_identifier DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1580 Parameters pars = (Parameters) $6;
1582 if (pars.FixedParameters == null){
1583 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1585 $$ = new IndexerDeclaration ((string) $1, (string) $2, pars);
1597 string full_name = MakeName ((string) $4);
1598 Enum e = new Enum (current_container, (string) $5, (int) $2, full_name, (Attributes) $1, lexer.Location);
1600 foreach (VariableDeclaration ev in (ArrayList) $6){
1601 CheckDef (e.AddEnumMember (ev.identifier,
1602 (Expression) ev.expression_or_array_initializer,
1603 (Location) ev.Location),
1607 CheckDef (current_container.AddEnum (e), name);
1612 : /* empty */ { $$ = "System.Int32"; }
1613 | COLON type { $$ = $2; }
1617 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1623 opt_enum_member_declarations
1624 : /* empty */ { $$ = new ArrayList (); }
1625 | enum_member_declarations opt_comma { $$ = $1; }
1628 enum_member_declarations
1629 : enum_member_declaration
1631 ArrayList l = new ArrayList ();
1636 | enum_member_declarations COMMA enum_member_declaration
1638 ArrayList l = (ArrayList) $1;
1646 enum_member_declaration
1647 : opt_attributes IDENTIFIER
1649 $$ = new VariableDeclaration ((string) $2, null, lexer.Location);
1651 | opt_attributes IDENTIFIER
1653 $$ = lexer.Location;
1657 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location);
1661 delegate_declaration
1665 IDENTIFIER OPEN_PARENS
1666 opt_formal_parameter_list
1670 Delegate del = new Delegate ((string) $4, (int) $2,
1671 MakeName ((string) $5), (Parameters) $7,
1672 (Attributes) $1, lexer.Location);
1674 CheckDef (current_container.AddDelegate (del), del.Name);
1679 IDENTIFIER OPEN_PARENS
1680 opt_formal_parameter_list
1684 Delegate del = new Delegate ("System.Void", (int) $2, (string) $5, (Parameters) $7,
1685 (Attributes) $1, lexer.Location);
1687 CheckDef (current_container.AddDelegate (del), del.Name);
1692 : namespace_or_type_name
1695 namespace_or_type_name
1696 : qualified_identifier
1700 * Before you think of adding a return_type, notice that we have been
1701 * using two rules in the places where it matters (one rule using type
1702 * and another identical one that uses VOID as the return type). This
1703 * gets rid of a shift/reduce couple
1706 : type_name { /* class_type */
1708 This does interfaces, delegates, struct_types, class_types,
1709 parent classes, and more! 4.2
1723 // Note that here only unmanaged types are allowed but we
1724 // can't perform checks during this phase - we do it during
1725 // semantic analysis.
1727 $$ = (string) $1 + "*";
1731 $$ = "System.Void*";
1738 $$ = new SimpleName ((string) $1, lexer.Location);
1740 | non_expression_type rank_specifier
1742 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1744 | non_expression_type STAR
1746 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1748 | expression rank_specifiers
1750 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1754 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1761 ArrayList types = new ArrayList ();
1766 | type_list COMMA type
1768 ArrayList types = (ArrayList) $1;
1776 * replaces all the productions for isolating the various
1777 * simple types, but we need this to reuse it easily in local_variable_type
1780 : OBJECT { $$ = "System.Object"; }
1781 | STRING { $$ = "System.String"; }
1782 | BOOL { $$ = "System.Boolean"; }
1783 | DECIMAL { $$ = "System.Decimal"; }
1784 | FLOAT { $$ = "System.Single"; }
1785 | DOUBLE { $$ = "System.Double"; }
1790 : SBYTE { $$ = "System.SByte"; }
1791 | BYTE { $$ = "System.Byte"; }
1792 | SHORT { $$ = "System.Int16"; }
1793 | USHORT { $$ = "System.UInt16"; }
1794 | INT { $$ = "System.Int32"; }
1795 | UINT { $$ = "System.UInt32"; }
1796 | LONG { $$ = "System.Int64"; }
1797 | ULONG { $$ = "System.UInt64"; }
1798 | CHAR { $$ = "System.Char"; }
1806 : type rank_specifiers
1808 $$ = (string) $1 + (string) $2;
1813 // Expressions, section 7.5
1821 | qualified_identifier
1823 string name = (string) $1;
1826 $$ = DecomposeQI (name, lexer.Location);
1828 | parenthesized_expression
1830 | invocation_expression
1834 | post_increment_expression
1835 | post_decrement_expression
1839 | checked_expression
1840 | unchecked_expression
1841 | pointer_member_access
1842 // TODO: pointer_element_access
1843 // TODO: sizeof-expression
1850 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
1851 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
1852 | NULL { $$ = NullLiteral.Null; }
1856 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
1857 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
1858 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
1863 object v = lexer.Value;
1866 // FIXME: Possible optimization would be to
1867 // compute the *Literal objects directly in the scanner
1870 $$ = new IntLiteral ((Int32) v);
1872 $$ = new UIntLiteral ((UInt32) v);
1874 $$ = new LongLiteral ((Int64) v);
1875 else if (v is ulong)
1876 $$ = new ULongLiteral ((UInt64) v);
1878 Console.WriteLine ("OOPS. Unexpected result from scanner");
1883 : TRUE { $$ = new BoolLiteral (true); }
1884 | FALSE { $$ = new BoolLiteral (false); }
1887 parenthesized_expression
1888 : OPEN_PARENS expression CLOSE_PARENS
1893 : primary_expression DOT IDENTIFIER
1895 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1897 | predefined_type DOT IDENTIFIER
1899 $$ = new SimpleName ((string) $1 + "." + (string) $3, lexer.Location);
1907 invocation_expression
1908 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
1911 Location l = lexer.Location;
1912 Report.Error (1, l, "THIS IS CRAZY");
1914 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
1919 : /* empty */ { $$ = null; }
1926 ArrayList list = new ArrayList ();
1930 | argument_list COMMA argument
1932 ArrayList list = (ArrayList) $1;
1941 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
1943 | REF variable_reference
1945 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
1947 | OUT variable_reference
1949 $$ = new Argument ((Expression) $2, Argument.AType.Out);
1954 : expression { note ("section 5.4"); $$ = $1; }
1958 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
1960 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
1962 | primary_expression rank_specifiers
1964 // So the super-trick is that primary_expression
1965 // can only be either a SimpleName or a MemberAccess.
1966 // The MemberAccess case arises when you have a fully qualified type-name like :
1968 // SimpleName is when you have
1971 Expression expr = (Expression) $1;
1972 if (!(expr is SimpleName || expr is MemberAccess)) {
1973 Location l = lexer.Location;
1974 Report.Error (-1, l, "Invalid Type definition");
1975 $$ = "System.Object";
1979 // So we extract the string corresponding to the SimpleName
1982 $$ = new SimpleName (GetQualifiedIdentifier (expr) + (string) $2, lexer.Location);
1989 ArrayList list = new ArrayList ();
1993 | expression_list COMMA expression
1995 ArrayList list = (ArrayList) $1;
2004 $$ = new This (lexer.Location);
2009 : BASE DOT IDENTIFIER
2011 $$ = new BaseAccess ((string) $3, lexer.Location);
2013 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2015 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2019 post_increment_expression
2020 : primary_expression OP_INC
2022 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2023 (Expression) $1, lexer.Location);
2027 post_decrement_expression
2028 : primary_expression OP_DEC
2030 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2031 (Expression) $1, lexer.Location);
2036 : object_or_delegate_creation_expression
2037 | array_creation_expression
2040 object_or_delegate_creation_expression
2041 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2043 $$ = new New ((string) $2, (ArrayList) $4, lexer.Location);
2047 array_creation_expression
2048 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2050 opt_array_initializer
2052 $$ = new ArrayCreation ((string) $2, (ArrayList) $4, (string) $6, (ArrayList) $7,
2055 | NEW type rank_specifiers array_initializer
2057 $$ = new ArrayCreation ((string) $2, (string) $3, (ArrayList) $4, lexer.Location);
2061 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2081 | rank_specifiers rank_specifier
2083 $$ = (string) $2 + (string) $1;
2088 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2090 $$ = "[" + (string) $2 + "]";
2110 | dim_separators COMMA
2112 $$ = (string) $1 + ",";
2116 opt_array_initializer
2128 : OPEN_BRACE CLOSE_BRACE
2130 ArrayList list = new ArrayList ();
2133 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2135 $$ = (ArrayList) $2;
2139 variable_initializer_list
2140 : variable_initializer
2142 ArrayList list = new ArrayList ();
2146 | variable_initializer_list COMMA variable_initializer
2148 ArrayList list = (ArrayList) $1;
2155 : TYPEOF OPEN_PARENS type CLOSE_PARENS
2157 $$ = new TypeOf ((string) $3, lexer.Location);
2162 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2163 $$ = new SizeOf ((string) $3, lexer.Location);
2165 note ("Verify type is unmanaged");
2166 note ("if (5.8) builtin, yield constant expression");
2171 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2173 $$ = new CheckedExpr ((Expression) $3);
2177 unchecked_expression
2178 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2180 $$ = new UnCheckedExpr ((Expression) $3);
2184 pointer_member_access
2185 : primary_expression OP_PTR IDENTIFIER
2189 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2190 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2194 : primary_expression
2195 | BANG prefixed_unary_expression
2197 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2199 | TILDE prefixed_unary_expression
2201 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2203 | OPEN_PARENS expression CLOSE_PARENS unary_expression
2205 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2207 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2209 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2214 // The idea to split this out is from Rhys' grammar
2215 // to solve the problem with casts.
2217 prefixed_unary_expression
2219 | PLUS prefixed_unary_expression
2221 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2223 | MINUS prefixed_unary_expression
2225 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2227 | OP_INC prefixed_unary_expression
2229 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2230 (Expression) $2, lexer.Location);
2232 | OP_DEC prefixed_unary_expression
2234 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2235 (Expression) $2, lexer.Location);
2237 | STAR prefixed_unary_expression
2239 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2241 | BITWISE_AND prefixed_unary_expression
2243 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2247 pre_increment_expression
2248 : OP_INC prefixed_unary_expression
2250 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2251 (Expression) $2, lexer.Location);
2255 pre_decrement_expression
2256 : OP_DEC prefixed_unary_expression
2258 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2259 (Expression) $2, lexer.Location);
2263 multiplicative_expression
2264 : prefixed_unary_expression
2265 | multiplicative_expression STAR prefixed_unary_expression
2267 $$ = new Binary (Binary.Operator.Multiply,
2268 (Expression) $1, (Expression) $3, lexer.Location);
2270 | multiplicative_expression DIV prefixed_unary_expression
2272 $$ = new Binary (Binary.Operator.Division,
2273 (Expression) $1, (Expression) $3, lexer.Location);
2275 | multiplicative_expression PERCENT prefixed_unary_expression
2277 $$ = new Binary (Binary.Operator.Modulus,
2278 (Expression) $1, (Expression) $3, lexer.Location);
2283 : multiplicative_expression
2284 | additive_expression PLUS multiplicative_expression
2286 $$ = new Binary (Binary.Operator.Addition,
2287 (Expression) $1, (Expression) $3, lexer.Location);
2289 | additive_expression MINUS multiplicative_expression
2291 $$ = new Binary (Binary.Operator.Subtraction,
2292 (Expression) $1, (Expression) $3, lexer.Location);
2297 : additive_expression
2298 | shift_expression OP_SHIFT_LEFT additive_expression
2300 $$ = new Binary (Binary.Operator.LeftShift,
2301 (Expression) $1, (Expression) $3, lexer.Location);
2303 | shift_expression OP_SHIFT_RIGHT additive_expression
2305 $$ = new Binary (Binary.Operator.RightShift,
2306 (Expression) $1, (Expression) $3, lexer.Location);
2310 relational_expression
2312 | relational_expression OP_LT shift_expression
2314 $$ = new Binary (Binary.Operator.LessThan,
2315 (Expression) $1, (Expression) $3, lexer.Location);
2317 | relational_expression OP_GT shift_expression
2319 $$ = new Binary (Binary.Operator.GreaterThan,
2320 (Expression) $1, (Expression) $3, lexer.Location);
2322 | relational_expression OP_LE shift_expression
2324 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2325 (Expression) $1, (Expression) $3, lexer.Location);
2327 | relational_expression OP_GE shift_expression
2329 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2330 (Expression) $1, (Expression) $3, lexer.Location);
2332 | relational_expression IS type
2334 $$ = new Is ((Expression) $1, (string) $3, lexer.Location);
2336 | relational_expression AS type
2338 $$ = new As ((Expression) $1, (string) $3, lexer.Location);
2343 : relational_expression
2344 | equality_expression OP_EQ relational_expression
2346 $$ = new Binary (Binary.Operator.Equality,
2347 (Expression) $1, (Expression) $3, lexer.Location);
2349 | equality_expression OP_NE relational_expression
2351 $$ = new Binary (Binary.Operator.Inequality,
2352 (Expression) $1, (Expression) $3, lexer.Location);
2357 : equality_expression
2358 | and_expression BITWISE_AND equality_expression
2360 $$ = new Binary (Binary.Operator.BitwiseAnd,
2361 (Expression) $1, (Expression) $3, lexer.Location);
2365 exclusive_or_expression
2367 | exclusive_or_expression CARRET and_expression
2369 $$ = new Binary (Binary.Operator.ExclusiveOr,
2370 (Expression) $1, (Expression) $3, lexer.Location);
2374 inclusive_or_expression
2375 : exclusive_or_expression
2376 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2378 $$ = new Binary (Binary.Operator.BitwiseOr,
2379 (Expression) $1, (Expression) $3, lexer.Location);
2383 conditional_and_expression
2384 : inclusive_or_expression
2385 | conditional_and_expression OP_AND inclusive_or_expression
2387 $$ = new Binary (Binary.Operator.LogicalAnd,
2388 (Expression) $1, (Expression) $3, lexer.Location);
2392 conditional_or_expression
2393 : conditional_and_expression
2394 | conditional_or_expression OP_OR conditional_and_expression
2396 $$ = new Binary (Binary.Operator.LogicalOr,
2397 (Expression) $1, (Expression) $3, lexer.Location);
2401 conditional_expression
2402 : conditional_or_expression
2403 | conditional_or_expression INTERR expression COLON expression
2405 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2409 assignment_expression
2410 : prefixed_unary_expression ASSIGN expression
2412 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2414 | prefixed_unary_expression OP_MULT_ASSIGN expression
2416 Location l = lexer.Location;
2418 $$ = new Assign ((Expression) $1,
2419 new Binary (Binary.Operator.Multiply,
2421 (Expression) $3, l), l);
2423 | prefixed_unary_expression OP_DIV_ASSIGN expression
2425 Location l = lexer.Location;
2427 $$ = new Assign ((Expression) $1,
2428 new Binary (Binary.Operator.Division,
2430 (Expression) $3, l), l);
2432 | prefixed_unary_expression OP_MOD_ASSIGN expression
2434 Location l = lexer.Location;
2436 $$ = new Assign ((Expression) $1,
2437 new Binary (Binary.Operator.Modulus,
2439 (Expression) $3, l), l);
2441 | prefixed_unary_expression OP_ADD_ASSIGN expression
2443 Location l = lexer.Location;
2445 $$ = new Assign ((Expression) $1,
2446 new Binary (Binary.Operator.Addition,
2448 (Expression) $3, l), l);
2450 | prefixed_unary_expression OP_SUB_ASSIGN expression
2452 Location l = lexer.Location;
2454 $$ = new Assign ((Expression) $1,
2455 new Binary (Binary.Operator.Subtraction,
2457 (Expression) $3, l), l);
2459 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2461 Location l = lexer.Location;
2463 $$ = new Assign ((Expression) $1,
2464 new Binary (Binary.Operator.LeftShift,
2466 (Expression) $3, l), l);
2468 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2470 Location l = lexer.Location;
2472 $$ = new Assign ((Expression) $1,
2473 new Binary (Binary.Operator.RightShift,
2475 (Expression) $3, l), l);
2477 | prefixed_unary_expression OP_AND_ASSIGN expression
2479 Location l = lexer.Location;
2481 $$ = new Assign ((Expression) $1,
2482 new Binary (Binary.Operator.BitwiseAnd,
2484 (Expression) $3, l), l);
2486 | prefixed_unary_expression OP_OR_ASSIGN expression
2488 Location l = lexer.Location;
2490 $$ = new Assign ((Expression) $1,
2491 new Binary (Binary.Operator.BitwiseOr,
2493 (Expression) $3, l), l);
2495 | prefixed_unary_expression OP_XOR_ASSIGN expression
2497 Location l = lexer.Location;
2499 $$ = new Assign ((Expression) $1,
2500 new Binary (Binary.Operator.ExclusiveOr,
2502 (Expression) $3, l), l);
2507 : conditional_expression
2508 | assignment_expression
2530 name = MakeName ((string) $4);
2532 new_class = new Class (current_container, name, (int) $2,
2533 (Attributes) $1, lexer.Location);
2534 current_container = new_class;
2535 current_container.Namespace = current_namespace;
2536 RootContext.Tree.RecordClass (name, new_class);
2542 Class new_class = (Class) current_container;
2545 new_class.Bases = (ArrayList) $6;
2547 current_container = current_container.Parent;
2548 CheckDef (current_container.AddClass (new_class), new_class.Name);
2555 : /* empty */ { $$ = (int) 0; }
2561 | modifiers modifier
2566 if ((m1 & m2) != 0) {
2567 Location l = lexer.Location;
2568 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2570 $$ = (int) (m1 | m2);
2575 : NEW { $$ = Modifiers.NEW; }
2576 | PUBLIC { $$ = Modifiers.PUBLIC; }
2577 | PROTECTED { $$ = Modifiers.PROTECTED; }
2578 | INTERNAL { $$ = Modifiers.INTERNAL; }
2579 | PRIVATE { $$ = Modifiers.PRIVATE; }
2580 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2581 | SEALED { $$ = Modifiers.SEALED; }
2582 | STATIC { $$ = Modifiers.STATIC; }
2583 | READONLY { $$ = Modifiers.READONLY; }
2584 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2585 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2586 | EXTERN { $$ = Modifiers.EXTERN; }
2587 | VOLATILE { $$ = Modifiers.VOLATILE; }
2588 | UNSAFE { $$ = Modifiers.UNSAFE; }
2592 : /* empty */ { $$ = null; }
2593 | class_base { $$ = $1; }
2597 : COLON type_list { $$ = $2; }
2605 // A block is "contained" on the following places:
2607 // property_declaration as part of the accessor body (get/set)
2608 // operator_declaration
2609 // constructor_declaration
2610 // destructor_declaration
2611 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2616 current_block = new Block (current_block);
2618 opt_statement_list CLOSE_BRACE
2620 while (current_block.Implicit)
2621 current_block = current_block.Parent;
2623 current_block = current_block.Parent;
2634 | statement_list statement
2638 : declaration_statement
2640 if ((Block) $1 != current_block){
2641 current_block.AddStatement ((Statement) $1);
2642 current_block = (Block) $1;
2645 | embedded_statement
2647 Statement s = (Statement) $1;
2650 current_block.AddStatement ((Statement) $1);
2658 | expression_statement
2659 | selection_statement
2660 | iteration_statement
2664 | unchecked_statement
2674 $$ = new EmptyStatement ();
2681 LabeledStatement labeled = new LabeledStatement ((string) $1);
2683 if (!current_block.AddLabel ((string) $1, labeled)){
2684 Location l = lexer.Location;
2685 Report.Error (140, l, "The label '" + ((string) $1) + "' is a duplicate");
2687 current_block.AddStatement (labeled);
2692 declaration_statement
2693 : local_variable_declaration SEMICOLON
2695 DictionaryEntry de = (DictionaryEntry) $1;
2697 $$ = declare_local_variables ((string) de.Key, (ArrayList) de.Value, lexer.Location);
2700 | local_constant_declaration SEMICOLON
2702 DictionaryEntry de = (DictionaryEntry) $1;
2704 $$ = declare_local_constant ((string) de.Key, (VariableDeclaration) de.Value);
2709 * The following is from Rhys' grammar:
2710 * > Types in local variable declarations must be recognized as
2711 * > expressions to prevent reduce/reduce errors in the grammar.
2712 * > The expressions are converted into types during semantic analysis.
2715 : primary_expression opt_rank_specifier
2717 // FIXME: Do something smart here regarding the composition of the type.
2719 // Ok, the above "primary_expression" is there to get rid of
2720 // both reduce/reduce and shift/reduces in the grammar, it should
2721 // really just be "type_name". If you use type_name, a reduce/reduce
2722 // creeps up. If you use qualified_identifier (which is all we need
2723 // really) two shift/reduces appear.
2726 // So the super-trick is that primary_expression
2727 // can only be either a SimpleName or a MemberAccess.
2728 // The MemberAccess case arises when you have a fully qualified type-name like :
2730 // SimpleName is when you have
2733 Expression expr = (Expression) $1;
2734 if (!(expr is SimpleName || expr is MemberAccess)) {
2735 Location l = lexer.Location;
2736 Report.Error (-1, l, "Invalid Type definition");
2737 $$ = "System.Object";
2741 // So we extract the string corresponding to the SimpleName
2744 $$ = GetQualifiedIdentifier (expr) + (string) $2;
2746 | builtin_types opt_rank_specifier
2748 $$ = (string) $1 + (string) $2;
2752 local_variable_pointer_type
2753 : primary_expression STAR
2755 Expression expr = (Expression) $1;
2756 if (!(expr is SimpleName || expr is MemberAccess)) {
2757 Location l = lexer.Location;
2758 Report.Error (-1, l, "Invalid Type definition");
2759 $$ = "System.Object";
2762 $$ = GetQualifiedIdentifier (expr) + "*";
2764 | builtin_types STAR
2766 $$ = (string) $1 + "*";
2770 $$ = "System.Void*";
2772 | local_variable_pointer_type STAR
2774 $$ = (string) $1 + "*";
2778 local_variable_declaration
2779 : local_variable_type variable_declarators
2781 $$ = new DictionaryEntry ($1, $2);
2783 | local_variable_pointer_type opt_rank_specifier variable_declarators
2785 $$ = new DictionaryEntry ((string) $1 + (string) $2, $3);
2789 local_constant_declaration
2790 : CONST local_variable_type constant_declarator
2792 $$ = new DictionaryEntry ($2, $3);
2796 expression_statement
2797 : statement_expression SEMICOLON
2804 // We have to do the wrapping here and not in the case above,
2805 // because statement_expression is used for example in for_statement
2807 statement_expression
2808 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2809 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2810 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2811 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2812 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2813 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2814 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2816 Report.Error (1002, lexer.Location, "Expecting `;'");
2820 object_creation_expression
2821 : object_or_delegate_creation_expression
2822 { note ("complain if this is a delegate maybe?"); }
2831 : if_statement_open if_statement_rest
2840 oob_stack.Push (lexer.Location);
2845 : boolean_expression CLOSE_PARENS
2848 Location l = (Location) oob_stack.Pop ();
2850 $$ = new If ((Expression) $1, (Statement) $3, l);
2852 if (RootContext.WarningLevel >= 3){
2853 if ($3 is EmptyStatement)
2854 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
2858 | boolean_expression CLOSE_PARENS
2859 embedded_statement ELSE embedded_statement
2861 Location l = (Location) oob_stack.Pop ();
2863 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
2868 : SWITCH OPEN_PARENS
2870 oob_stack.Push (lexer.Location);
2872 expression CLOSE_PARENS
2875 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
2891 Report.Error (1522, lexer.Location, "Empty switch block");
2899 ArrayList sections = new ArrayList ();
2904 | switch_sections switch_section
2906 ArrayList sections = (ArrayList) $1;
2916 current_block = new Block (current_block);
2920 while (current_block.Implicit)
2921 current_block = current_block.Parent;
2922 $$ = new SwitchSection ((ArrayList) $1, current_block);
2923 current_block = current_block.Parent;
2930 ArrayList labels = new ArrayList ();
2935 | switch_labels switch_label
2937 ArrayList labels = (ArrayList) ($1);
2945 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
2946 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
2949 1523, lexer.Location,
2950 "The keyword case or default must precede code in switch block");
2964 oob_stack.Push (lexer.Location);
2966 boolean_expression CLOSE_PARENS embedded_statement
2968 Location l = (Location) oob_stack.Pop ();
2969 $$ = new While ((Expression) $4, (Statement) $6, l);
2971 if (RootContext.WarningLevel >= 3){
2972 if ($6 is EmptyStatement)
2973 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
2979 : DO embedded_statement
2980 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
2982 $$ = new Do ((Statement) $2, (Expression) $5, lexer.Location);
2988 opt_for_initializer SEMICOLON
2990 Block assign_block = new Block (current_block);
2991 current_block = assign_block;
2993 if ($3 is DictionaryEntry){
2994 DictionaryEntry de = (DictionaryEntry) $3;
2996 string type = (string) de.Key;
2997 ArrayList var_declarators = (ArrayList) de.Value;
2999 foreach (VariableDeclaration decl in var_declarators){
3003 vi = current_block.AddVariable (
3004 type, decl.identifier, current_local_parameters, decl.Location);
3006 Report.Error (128, decl.Location,
3007 "A local variable `" + decl.identifier +
3008 "' is already defined in this scope");
3010 Location l = lexer.Location;
3012 if (decl.expression_or_array_initializer is Expression){
3013 expr = (Expression) decl.expression_or_array_initializer;
3017 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3019 string base_type = type.Substring (0, type.IndexOf ("["));
3020 string rank = type.Substring (type.IndexOf ("["));
3022 expr = new ArrayCreation (base_type, rank, init, decl.Location);
3025 LocalVariableReference var;
3026 var = new LocalVariableReference (
3027 assign_block, decl.identifier, l);
3029 Assign a = new Assign (var, expr, decl.Location);
3031 assign_block.AddStatement (new StatementExpression (a));
3037 oob_stack.Push (lexer.Location);
3039 opt_for_condition SEMICOLON
3040 opt_for_iterator CLOSE_PARENS
3043 Location l = (Location) oob_stack.Pop ();
3045 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3047 if (RootContext.WarningLevel >= 3){
3048 if ($10 is EmptyStatement)
3049 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3052 current_block.AddStatement (f);
3053 while (current_block.Implicit)
3054 current_block = current_block.Parent;
3056 current_block = current_block.Parent;
3061 : /* empty */ { $$ = new EmptyStatement (); }
3066 : local_variable_declaration
3067 | statement_expression_list
3071 : /* empty */ { $$ = null; }
3072 | boolean_expression
3076 : /* empty */ { $$ = new EmptyStatement (); }
3081 : statement_expression_list
3084 statement_expression_list
3085 : statement_expression
3087 Block b = new Block (null, true);
3089 b.AddStatement ((Statement) $1);
3092 | statement_expression_list COMMA statement_expression
3094 Block b = (Block) $1;
3096 b.AddStatement ((Statement) $3);
3102 : FOREACH OPEN_PARENS type IDENTIFIER IN
3104 oob_stack.Push (lexer.Location);
3106 expression CLOSE_PARENS
3108 oob_stack.Push (current_block);
3110 Block foreach_block = new Block (current_block, true);
3111 LocalVariableReference v;
3112 Location l = lexer.Location;
3115 vi = foreach_block.AddVariable ((string) $3, (string) $4, current_local_parameters, l);
3118 128, l, "A local variable `" + (string) $4 + "' is already "+
3119 "defined in this scope");
3122 v = new LocalVariableReference (foreach_block, (string) $4, l);
3124 current_block.AddStatement (foreach_block);
3125 current_block = foreach_block;
3131 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3132 Block prev_block = (Block) oob_stack.Pop ();
3133 Location l = (Location) oob_stack.Pop ();
3135 while (current_block != prev_block)
3136 current_block = current_block.Parent;
3138 $$ = new Foreach ((string) $3, v, (Expression) $7, (Statement) $10, l);
3144 | continue_statement
3153 $$ = new Break (lexer.Location);
3158 : CONTINUE SEMICOLON
3160 $$ = new Continue (lexer.Location);
3165 : GOTO IDENTIFIER SEMICOLON
3167 $$ = new Goto (current_block, (string) $2, lexer.Location);
3169 | GOTO CASE constant_expression SEMICOLON
3171 $$ = new GotoCase ((Expression) $3, lexer.Location);
3173 | GOTO DEFAULT SEMICOLON
3175 $$ = new GotoDefault (lexer.Location);
3180 : RETURN opt_expression SEMICOLON
3182 $$ = new Return ((Expression) $2, lexer.Location);
3187 : THROW opt_expression SEMICOLON
3189 $$ = new Throw ((Expression) $2, lexer.Location);
3199 : TRY block catch_clauses
3202 ArrayList s = new ArrayList ();
3204 foreach (Catch cc in (ArrayList) $3) {
3205 if (cc.Type == null)
3211 // Now s contains the list of specific catch clauses
3212 // and g contains the general one.
3214 $$ = new Try ((Block) $2, s, g, null);
3216 | TRY block opt_catch_clauses FINALLY block
3219 ArrayList s = new ArrayList ();
3220 ArrayList catch_list = (ArrayList) $3;
3222 if (catch_list != null){
3223 foreach (Catch cc in catch_list) {
3224 if (cc.Type == null)
3231 $$ = new Try ((Block) $2, s, g, (Block) $5);
3235 Report.Error (1524, lexer.Location, "Expected catch or finally");
3240 : /* empty */ { $$ = null; }
3247 ArrayList l = new ArrayList ();
3252 | catch_clauses catch_clause
3254 ArrayList l = (ArrayList) $1;
3262 : /* empty */ { $$ = null; }
3267 : CATCH opt_catch_args
3269 string type = null, id = null;
3272 DictionaryEntry cc = (DictionaryEntry) $2;
3273 type = (string) cc.Key;
3274 id = (string) cc.Value;
3277 ArrayList one = new ArrayList ();
3278 Location loc = lexer.Location;
3280 one.Add (new VariableDeclaration (id, null, loc));
3283 current_block = new Block (current_block);
3284 Block b = declare_local_variables (type, one, loc);
3291 string type = null, id = null;
3294 DictionaryEntry cc = (DictionaryEntry) $2;
3295 type = (string) cc.Key;
3296 id = (string) cc.Value;
3299 while (current_block != (Block) $1)
3300 current_block = current_block.Parent;
3305 $$ = new Catch (type, id , (Block) $4, lexer.Location);
3310 : /* empty */ { $$ = null; }
3315 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3317 $$ = new DictionaryEntry ($2, $3);
3324 $$ = new Checked ((Block) $2);
3331 $$ = new Unchecked ((Block) $2);
3338 if (!RootContext.Unsafe){
3339 Report.Error (227, lexer.Location,
3340 "Unsafe code can only be used if --unsafe is used");
3343 $$ = new Unsafe ((Block) $3);
3349 pointer_type fixed_pointer_declarators
3352 Block assign_block = new Block (current_block, true);
3353 ArrayList list = (ArrayList) $4;
3354 string type = (string) $3;
3355 Location l = lexer.Location;
3356 int top = list.Count;
3358 for (int i = 0; i < top; i++){
3359 Pair p = (Pair) list [i];
3362 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3365 128, l, "A local variable `" + (string) p.First + "' is already "+
3366 "defined in this scope");
3372 current_block.AddStatement (assign_block);
3373 current_block = assign_block;
3374 oob_stack.Push (assign_block);
3379 Location l = (Location) oob_stack.Pop ();
3380 Block assign_block = (Block) oob_stack.Pop ();
3382 ArrayList list = (ArrayList) $4;
3383 int top = list.Count;
3385 $$ = new Fixed ((string) $3, (ArrayList) $4, (Statement) $7, l);
3389 fixed_pointer_declarators
3390 : fixed_pointer_declarator {
3391 ArrayList declarators = new ArrayList ();
3392 declarators.Add ($1);
3395 | fixed_pointer_declarators COMMA fixed_pointer_declarator
3397 ArrayList declarators = (ArrayList) $1;
3398 declarators.Add ($3);
3403 fixed_pointer_declarator
3404 : IDENTIFIER ASSIGN expression
3406 $$ = new Pair ($1, $3);
3411 : LOCK OPEN_PARENS expression CLOSE_PARENS
3417 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
3422 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
3424 Block assign_block = new Block (current_block);
3425 current_block = assign_block;
3427 oob_stack.Push (lexer.Location);
3429 if ($3 is DictionaryEntry){
3430 DictionaryEntry de = (DictionaryEntry) $3;
3431 Location l = lexer.Location;
3433 string type = (string) de.Key;
3434 ArrayList var_declarators = (ArrayList) de.Value;
3436 foreach (VariableDeclaration decl in var_declarators){
3437 if (current_block.AddVariable (
3438 type, decl.identifier,
3439 current_local_parameters, decl.Location) == null){
3440 Report.Error (128, decl.Location,
3441 "A local variable `" + decl.identifier + "' is already" +
3442 "defined in this scope");
3446 ArrayList vars = new ArrayList ();
3448 foreach (VariableDeclaration decl in var_declarators){
3451 if (decl.expression_or_array_initializer is Expression){
3452 expr = (Expression) decl.expression_or_array_initializer;
3456 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3458 string base_type = type.Substring (0, type.IndexOf ("["));
3459 string rank = type.Substring (type.IndexOf ("["));
3461 expr = new ArrayCreation (base_type, rank, init, decl.Location);
3464 LocalVariableReference var;
3467 var = new LocalVariableReference (assign_block, decl.identifier, l);
3468 vi = var.VariableInfo;
3471 // This is so that it is not a warning on using variables
3474 vars.Add (new DictionaryEntry (var, expr));
3476 // Assign a = new Assign (var, expr, decl.Location);
3477 // assign_block.AddStatement (new StatementExpression (a));
3479 $3 = new DictionaryEntry (type, vars);
3484 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
3485 current_block.AddStatement (u);
3486 while (current_block.Implicit)
3487 current_block = current_block.Parent;
3489 current_block = current_block.Parent;
3493 resource_acquisition
3494 : local_variable_declaration
3501 // A class used to pass around variable declarations and constants
3503 public class VariableDeclaration {
3504 public string identifier;
3505 public object expression_or_array_initializer;
3506 public Location Location;
3508 public VariableDeclaration (string id, object eoai, Location l){
3509 this.identifier = id;
3510 this.expression_or_array_initializer = eoai;
3516 // A class used to hold info about an indexer declarator
3519 public class IndexerDeclaration {
3521 public string interface_type;
3522 public Parameters param_list;
3524 public IndexerDeclaration (string type, string interface_type, Parameters param_list)
3527 this.interface_type = interface_type;
3528 this.param_list = param_list;
3533 // A class used to hold info about an operator declarator
3536 public class OperatorDeclaration {
3537 public Operator.OpType optype;
3538 public string ret_type;
3539 public string arg1type;
3540 public string arg1name;
3541 public string arg2type;
3542 public string arg2name;
3543 public Location location;
3545 public OperatorDeclaration (Operator.OpType op, string ret_type, string arg1type, string arg1name,
3546 string arg2type, string arg2name, Location location)
3549 this.ret_type = ret_type;
3550 this.arg1type = arg1type;
3551 this.arg1name = arg1name;
3552 this.arg2type = arg2type;
3553 this.arg2name = arg2name;
3554 this.location = location;
3560 // Given the @class_name name, it creates a fully qualified name
3561 // based on the containing declaration space
3564 MakeName (string class_name)
3566 string ns = current_namespace.Name;
3567 string container_name = current_container.Name;
3569 if (container_name == ""){
3571 return ns + "." + class_name;
3575 return container_name + "." + class_name;
3579 // Used to report back to the user the result of a declaration
3580 // in the current declaration space
3583 CheckDef (AdditionResult result, string name)
3585 if (result == AdditionResult.Success)
3588 Location l = lexer.Location;
3591 case AdditionResult.NameExists:
3592 Report.Error (102, l, "The namespace `" + current_container.Name +
3593 "' already contains a definition for `"+
3599 // This is handled only for static Constructors, because
3600 // in reality we handle these by the semantic analysis later
3602 case AdditionResult.MethodExists:
3604 111, l, "Class `"+current_container.Name+
3605 "' already defines a member called '" +
3606 name + "' with the same parameter types (more than one default constructor)");
3609 case AdditionResult.EnclosingClash:
3610 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
3613 case AdditionResult.NotAConstructor:
3614 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
3620 CheckDef (bool result, string name)
3624 CheckDef (AdditionResult.NameExists, name);
3628 SimpleLookup (string name, Location loc)
3631 // we need to check against current_block not being null
3632 // as `expression' is allowed in argument_lists, which
3633 // do not exist inside a block.
3635 if (current_block != null){
3636 if (current_block.IsVariableDefined (name))
3637 return new LocalVariableReference (current_block, name, loc);
3640 if (current_local_parameters != null){
3642 Parameter par = current_local_parameters.GetParameterByName (name, out idx);
3644 return new ParameterReference (current_local_parameters, idx, name);
3650 Expression DecomposeQI (string name, Location loc)
3654 if (name.IndexOf ('.') == -1){
3655 o = SimpleLookup (name, loc);
3657 return new SimpleName (name, loc);
3660 int pos = name.LastIndexOf (".");
3661 string left = name.Substring (0, pos);
3662 string right = name.Substring (pos + 1);
3664 o = DecomposeQI (left, loc);
3665 return new MemberAccess (o, right, loc);
3670 // This method is used to get at the complete string representation of
3671 // a fully-qualified type name, hiding inside a MemberAccess ;-)
3672 // This is necessary because local_variable_type admits primary_expression
3673 // as the type of the variable. So we do some extra checking
3675 string GetQualifiedIdentifier (Expression expr)
3677 if (expr is SimpleName)
3678 return ((SimpleName)expr).Name;
3679 else if (expr is MemberAccess)
3680 return GetQualifiedIdentifier (((MemberAccess)expr).Expr) + "." + ((MemberAccess) expr).Identifier;
3682 throw new Exception ("Expr has to be either SimpleName or MemberAccess! (" + expr + ")");
3686 Block declare_local_variables (string type, ArrayList variable_declarators, Location loc)
3688 Block implicit_block;
3689 ArrayList inits = null;
3692 // We use the `Used' property to check whether statements
3693 // have been added to the current block. If so, we need
3694 // to create another block to contain the new declaration
3695 // otherwise, as an optimization, we use the same block to
3696 // add the declaration.
3698 // FIXME: A further optimization is to check if the statements
3699 // that were added were added as part of the initialization
3700 // below. In which case, no other statements have been executed
3701 // and we might be able to reduce the number of blocks for
3702 // situations like this:
3704 // int j = 1; int k = j + 1;
3706 if (current_block.Used)
3707 implicit_block = new Block (current_block, true);
3709 implicit_block = current_block;
3711 foreach (VariableDeclaration decl in variable_declarators){
3713 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
3714 if (decl.expression_or_array_initializer != null){
3716 inits = new ArrayList ();
3720 Report.Error (128, decl.Location, "A local variable `" + decl.identifier +
3721 "' is already defined in this scope");
3726 return implicit_block;
3728 foreach (VariableDeclaration decl in inits){
3732 if (decl.expression_or_array_initializer is Expression){
3733 expr = (Expression) decl.expression_or_array_initializer;
3736 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3738 string base_type = type.Substring (0, type.IndexOf ("["));
3739 string rank = type.Substring (type.IndexOf ("["));
3741 expr = new ArrayCreation (base_type, rank, init, decl.Location);
3745 LocalVariableReference var;
3746 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
3748 assign = new Assign (var, expr, decl.Location);
3750 implicit_block.AddStatement (new StatementExpression (assign));
3753 return implicit_block;
3756 Block declare_local_constant (string type, VariableDeclaration decl)
3758 Block implicit_block;
3760 if (current_block.Used)
3761 implicit_block = new Block (current_block, true);
3763 implicit_block = current_block;
3765 if (!(implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
3766 current_local_parameters, decl.Location))){
3767 Report.Error (128, decl.Location, "A local variable `" + decl.identifier +
3768 "' is already defined in this scope");
3771 return implicit_block;
3774 void CheckAttributeTarget (string a)
3778 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
3782 Location l = lexer.Location;
3783 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
3789 void CheckUnaryOperator (Operator.OpType op)
3793 case Operator.OpType.LogicalNot:
3794 case Operator.OpType.OnesComplement:
3795 case Operator.OpType.Increment:
3796 case Operator.OpType.Decrement:
3797 case Operator.OpType.True:
3798 case Operator.OpType.False:
3799 case Operator.OpType.Addition:
3800 case Operator.OpType.Subtraction:
3805 Location l = lexer.Location;
3806 Report.Error (1019, l, "Overloadable unary operator expected");
3812 void CheckBinaryOperator (Operator.OpType op)
3816 case Operator.OpType.Addition:
3817 case Operator.OpType.Subtraction:
3818 case Operator.OpType.Multiply:
3819 case Operator.OpType.Division:
3820 case Operator.OpType.Modulus:
3821 case Operator.OpType.BitwiseAnd:
3822 case Operator.OpType.BitwiseOr:
3823 case Operator.OpType.ExclusiveOr:
3824 case Operator.OpType.LeftShift:
3825 case Operator.OpType.RightShift:
3826 case Operator.OpType.Equality:
3827 case Operator.OpType.Inequality:
3828 case Operator.OpType.GreaterThan:
3829 case Operator.OpType.LessThan:
3830 case Operator.OpType.GreaterThanOrEqual:
3831 case Operator.OpType.LessThanOrEqual:
3835 Location l = lexer.Location;
3836 Report.Error (1020, l, "Overloadable binary operator expected");
3842 void syntax_error (Location l, string msg)
3844 Report.Error (1003, l, "Syntax error, " + msg);
3847 void output (string s)
3849 Console.WriteLine (s);
3852 void note (string s)
3854 // Used to put annotations
3859 public Tokenizer Lexer {
3865 public CSharpParser (string name, System.IO.Stream input, ArrayList defines)
3867 current_namespace = new Namespace (null, "");
3870 current_container = RootContext.Tree.Types;
3871 current_container.Namespace = current_namespace;
3872 oob_stack = new Stack ();
3874 lexer = new Tokenizer (input, name, defines);
3877 public override int parse ()
3879 StringBuilder value = new StringBuilder ();
3883 if (yacc_verbose_flag)
3884 yyparse (lexer, new yydebug.yyDebugSimple ());
3887 } catch (Exception e){
3888 // Console.WriteLine ("Fatal error: " + name);
3889 // Console.WriteLine (lexer.location);
3892 // Please do not remove this, it is used during debugging
3895 Console.WriteLine (lexer.location + " : Parsing error ");
3896 Console.WriteLine (e);
3900 return global_errors;