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
168 /* C# keywords which are not really keywords */
172 /* C# single character operators/punctuation. */
173 %token OPEN_BRACE "{"
174 %token CLOSE_BRACE "}"
175 %token OPEN_BRACKET "["
176 %token CLOSE_BRACKET "]"
177 %token OPEN_PARENS "("
178 %token CLOSE_PARENS ")"
191 %token BITWISE_AND "&"
192 %token BITWISE_OR "|"
199 /* C# multi-character operators. */
202 %token OP_SHIFT_LEFT "<<"
203 %token OP_SHIFT_RIGHT ">>"
210 %token OP_MULT_ASSIGN "*="
211 %token OP_DIV_ASSIGN "/="
212 %token OP_MOD_ASSIGN "%="
213 %token OP_ADD_ASSIGN "+="
214 %token OP_SUB_ASSIGN "-="
215 %token OP_SHIFT_LEFT_ASSIGN "<<="
216 %token OP_SHIFT_RIGHT_ASSIGN ">>="
217 %token OP_AND_ASSIGN "&="
218 %token OP_XOR_ASSIGN "^="
219 %token OP_OR_ASSIGN "|="
223 %token LITERAL_INTEGER "int literal"
224 %token LITERAL_FLOAT "float literal"
225 %token LITERAL_DOUBLE "double literal"
226 %token LITERAL_DECIMAL "decimal literal"
227 %token LITERAL_CHARACTER "character literal"
228 %token LITERAL_STRING "string literal"
232 /* Add precedence rules to solve dangling else s/r conflict */
241 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
243 %left STAR DIV PERCENT
244 %right BANG CARRET UMINUS
245 %nonassoc OP_INC OP_DEC
247 %left OPEN_BRACKET OPEN_BRACE
251 %start compilation_unit
252 /*%start namespace_declaration */
256 : opt_using_directives opt_namespace_member_declarations opt_EOF
258 // Check that using comes only before namespace elements
269 | using_directives using_directive
273 : using_alias_directive
274 | using_namespace_directive
277 using_alias_directive
278 : USING IDENTIFIER ASSIGN
279 namespace_or_type_name SEMICOLON
281 current_namespace.UsingAlias ((string) $2, (string) $4, lexer.Location);
285 using_namespace_directive
286 : USING namespace_name SEMICOLON
288 current_namespace.Using ((string) $2);
292 // namespace_declarations
293 // : namespace_declaration
294 // | namespace_declarations namespace_declaration
296 namespace_declaration
297 : NAMESPACE qualified_identifier
299 current_namespace = RootContext.Tree.RecordNamespace (current_namespace, name, (string) $2);
301 namespace_body opt_semicolon
303 current_namespace = current_namespace.Parent;
319 | qualified_identifier DOT IDENTIFIER {
320 $$ = (($1).ToString ()) + "." + ($3.ToString ()); }
325 : namespace_or_type_name
331 opt_namespace_member_declarations
342 opt_namespace_member_declarations
344 | namespace_member_declarations
347 namespace_member_declarations
348 : namespace_member_declaration
349 | namespace_member_declarations namespace_member_declaration
352 namespace_member_declaration
359 Class c = (Class) $1;
360 mod_flags = c.ModFlags;
362 } else if ($1 is Struct){
363 Struct s = (Struct) $1;
364 mod_flags = s.ModFlags;
369 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
371 1527, lexer.Location,
372 "Namespace elements cant be explicitly " +
373 "declared private or protected in `" + name + "'");
376 | namespace_declaration
382 | interface_declaration
384 | delegate_declaration
386 // Enable this when we have handled all errors, because this acts as a generic fallback
389 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
397 : /* empty */ { $$ = null; }
398 | attribute_section opt_attributes
403 attrs = (Attributes) $2;
404 attrs.AddAttribute ((AttributeSection) $1);
406 attrs = new Attributes ((AttributeSection) $1);
413 : OPEN_BRACKET attribute_target_specifier attribute_list CLOSE_BRACKET
415 string target = null;
418 target = (string) $2;
420 $$ = new AttributeSection (target, (ArrayList) $3);
422 | OPEN_BRACKET attribute_list CLOSE_BRACKET
424 $$ = new AttributeSection (null, (ArrayList) $2);
428 attribute_target_specifier
429 : attribute_target COLON
438 CheckAttributeTarget ((string) $1);
441 | EVENT { $$ = "event"; }
442 | RETURN { $$ = "return"; }
448 ArrayList attrs = new ArrayList ();
454 | attribute_list COMMA attribute
456 ArrayList attrs = (ArrayList) $1;
468 opt_attribute_arguments
470 $$ = new Attribute ((string) $1, (ArrayList) $3, (Location) $2);
475 : type_name { /* reserved attribute name or identifier: 17.4 */ }
478 opt_attribute_arguments
479 : /* empty */ { $$ = null; }
480 | OPEN_PARENS attribute_arguments CLOSE_PARENS
488 : positional_argument_list
490 ArrayList args = new ArrayList ();
495 | positional_argument_list COMMA named_argument_list
497 ArrayList args = new ArrayList ();
503 | named_argument_list
505 ArrayList args = new ArrayList ();
514 positional_argument_list
517 ArrayList args = new ArrayList ();
518 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
522 | positional_argument_list COMMA expression
524 ArrayList args = (ArrayList) $1;
525 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
534 ArrayList args = new ArrayList ();
539 | named_argument_list COMMA named_argument
541 ArrayList args = (ArrayList) $1;
549 : IDENTIFIER ASSIGN expression
551 $$ = new DictionaryEntry (
553 new Argument ((Expression) $3, Argument.AType.Expression));
559 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
562 opt_class_member_declarations
564 | class_member_declarations
567 class_member_declarations
568 : class_member_declaration
569 | class_member_declarations
570 class_member_declaration
573 class_member_declaration
574 : constant_declaration // done
575 | field_declaration // done
576 | method_declaration // done
577 | property_declaration // done
578 | event_declaration // done
579 | indexer_declaration // done
580 | operator_declaration // done
581 | constructor_declaration // done
582 | destructor_declaration // done
592 string full_struct_name = MakeName ((string) $4);
594 new_struct = new Struct (current_container, full_struct_name, (int) $2,
595 (Attributes) $1, lexer.Location);
596 current_container = new_struct;
597 current_container.Namespace = current_namespace;
598 RootContext.Tree.RecordStruct (full_struct_name, new_struct);
600 opt_struct_interfaces
604 Struct new_struct = (Struct) current_container;
606 current_container = current_container.Parent;
607 CheckDef (current_container.AddStruct (new_struct), new_struct.Name);
612 opt_struct_interfaces
619 | struct_interfaces struct_interface
627 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
630 opt_struct_member_declarations
632 | struct_member_declarations
635 struct_member_declarations
636 : struct_member_declaration
637 | struct_member_declarations struct_member_declaration
640 struct_member_declaration
641 : constant_declaration
644 | property_declaration
646 | indexer_declaration
647 | operator_declaration
648 | constructor_declaration
652 * This is only included so we can flag error 575:
653 * destructors only allowed on class types
655 | destructor_declaration
666 foreach (VariableDeclaration constant in (ArrayList) $5){
667 Const c = new Const (
668 (string) $4, (string) constant.identifier,
669 (Expression) constant.expression_or_array_initializer, (int) $2, (Attributes) $1,
672 CheckDef (current_container.AddConstant (c), c.Name);
678 : constant_declarator
680 ArrayList constants = new ArrayList ();
684 | constant_declarators COMMA constant_declarator
686 ArrayList constants = (ArrayList) $1;
693 : IDENTIFIER ASSIGN constant_expression
695 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
706 string type = (string) $3;
709 foreach (VariableDeclaration var in (ArrayList) $4){
710 Field field = new Field (type, mod, var.identifier,
711 var.expression_or_array_initializer, (Attributes) $1, var.Location);
713 CheckDef (current_container.AddField (field), field.Name);
719 : variable_declarator
721 ArrayList decl = new ArrayList ();
725 | variable_declarators COMMA variable_declarator
727 ArrayList decls = (ArrayList) $1;
734 : IDENTIFIER ASSIGN variable_initializer
736 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
740 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
759 Method method = (Method) $1;
760 Block b = (Block) $2;
761 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
764 if ((method.ModFlags & extern_abstract) == 0){
766 501, lexer.Location, current_container.MakeName (method.Name) +
767 "must declare a body because it is not marked abstract or extern");
770 if ((method.ModFlags & Modifiers.EXTERN) != 0){
772 179, lexer.Location, current_container.MakeName (method.Name) +
773 " is declared extern, but has a body");
777 method.Block = (Block) $2;
778 CheckDef (current_container.AddMethod (method), method.Name);
780 current_local_parameters = null;
794 1585, lexer.Location, "Member modifier `" +
795 Modifiers.Name (i) + "' must precede member type and name");
808 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
810 Method method = new Method ((string) $3, (int) $2, (string) $4,
811 (Parameters) $6, (Attributes) $1, lexer.Location);
813 current_local_parameters = (Parameters) $6;
821 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
823 Method method = new Method ("System.Void", (int) $2, (string) $4,
824 (Parameters) $6, (Attributes) $1, lexer.Location);
826 current_local_parameters = (Parameters) $6;
833 | SEMICOLON { $$ = null; }
836 opt_formal_parameter_list
837 : /* empty */ { $$ = Parameters.GetEmptyReadOnlyParameters (); }
838 | formal_parameter_list
841 formal_parameter_list
844 ArrayList pars_list = (ArrayList) $1;
846 Parameter [] pars = new Parameter [pars_list.Count];
847 pars_list.CopyTo (pars);
849 $$ = new Parameters (pars, null, lexer.Location);
851 | fixed_parameters COMMA parameter_array
853 ArrayList pars_list = (ArrayList) $1;
855 Parameter [] pars = new Parameter [pars_list.Count];
856 pars_list.CopyTo (pars);
858 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
862 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
869 ArrayList pars = new ArrayList ();
874 | fixed_parameters COMMA fixed_parameter
876 ArrayList pars = (ArrayList) $1;
885 opt_parameter_modifier
889 $$ = new Parameter ((string) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
893 opt_parameter_modifier
894 : /* empty */ { $$ = Parameter.Modifier.NONE; }
899 : REF { $$ = Parameter.Modifier.REF; }
900 | OUT { $$ = Parameter.Modifier.OUT; }
904 : opt_attributes PARAMS type IDENTIFIER
906 $$ = new Parameter ((string) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
907 note ("type must be a single-dimension array type");
912 : qualified_identifier
921 implicit_value_parameter_type = (string) $3;
923 lexer.PropertyParsing = true;
927 accessor_declarations
929 lexer.PropertyParsing = false;
934 Pair pair = (Pair) $7;
935 Block get_block = null;
936 Block set_block = null;
938 if (pair.First != null)
939 get_block = (Block) pair.First;
940 if (pair.Second != null)
941 set_block = (Block) pair.Second;
943 prop = new Property ((string) $3, (string) $4, (int) $2, get_block, set_block,
944 (Attributes) $1, (Location) $6);
946 CheckDef (current_container.AddProperty (prop), prop.Name);
947 implicit_value_parameter_type = null;
951 accessor_declarations
952 : get_accessor_declaration opt_set_accessor_declaration
954 $$ = new Pair ($1, $2);
956 | set_accessor_declaration opt_get_accessor_declaration
958 $$ = new Pair ($2, $1);
962 opt_get_accessor_declaration
963 : /* empty */ { $$ = null; }
964 | get_accessor_declaration
967 opt_set_accessor_declaration
968 : /* empty */ { $$ = null; }
969 | set_accessor_declaration
972 get_accessor_declaration
975 // If this is not the case, then current_local_parameters has already
976 // been set in indexer_declaration
977 if (parsing_indexer == false)
978 current_local_parameters = null;
980 current_local_parameters = indexer_parameters;
981 lexer.PropertyParsing = false;
986 current_local_parameters = null;
987 lexer.PropertyParsing = true;
991 set_accessor_declaration
995 Parameter implicit_value_parameter = new Parameter (
996 implicit_value_parameter_type, "value",
997 Parameter.Modifier.NONE, null);
999 if (parsing_indexer == false) {
1000 args = new Parameter [1];
1001 args [0] = implicit_value_parameter;
1003 Parameter [] fp = indexer_parameters.FixedParameters;
1004 int count = fp.Length;
1006 args = new Parameter [count + 1];
1008 fp.CopyTo (args, 0);
1009 args [count] = implicit_value_parameter;
1012 current_local_parameters = new Parameters (args, null, lexer.Location);
1013 lexer.PropertyParsing = false;
1018 current_local_parameters = null;
1019 lexer.PropertyParsing = true;
1025 | SEMICOLON { $$ = new Block (null); }
1028 interface_declaration
1031 INTERFACE IDENTIFIER
1033 Interface new_interface;
1034 string full_interface_name = MakeName ((string) $4);
1036 new_interface = new Interface (current_container, full_interface_name, (int) $2,
1037 (Attributes) $1, lexer.Location);
1038 if (current_interface != null) {
1039 Location l = lexer.Location;
1040 Report.Error (-2, l, "Internal compiler error: interface inside interface");
1042 current_interface = new_interface;
1043 new_interface.Namespace = current_namespace;
1044 RootContext.Tree.RecordInterface (full_interface_name, new_interface);
1049 Interface new_interface = (Interface) current_interface;
1052 new_interface.Bases = (ArrayList) $6;
1054 current_interface = null;
1055 CheckDef (current_container.AddInterface (new_interface), new_interface.Name);
1060 : /* empty */ { $$ = null; }
1065 : COLON interface_type_list { $$ = $2; }
1071 ArrayList interfaces = new ArrayList ();
1073 interfaces.Add ($1);
1076 | interface_type_list COMMA interface_type
1078 ArrayList interfaces = (ArrayList) $1;
1079 interfaces.Add ($3);
1086 opt_interface_member_declarations
1090 opt_interface_member_declarations
1092 | interface_member_declarations
1095 interface_member_declarations
1096 : interface_member_declaration
1097 | interface_member_declarations interface_member_declaration
1100 interface_member_declaration
1101 : interface_method_declaration
1103 InterfaceMethod m = (InterfaceMethod) $1;
1105 CheckDef (current_interface.AddMethod (m), m.Name);
1107 | interface_property_declaration
1109 InterfaceProperty p = (InterfaceProperty) $1;
1111 CheckDef (current_interface.AddProperty (p), p.Name);
1113 | interface_event_declaration
1115 InterfaceEvent e = (InterfaceEvent) $1;
1117 CheckDef (current_interface.AddEvent (e), e.Name);
1119 | interface_indexer_declaration
1121 InterfaceIndexer i = (InterfaceIndexer) $1;
1123 CheckDef (current_interface.AddIndexer (i), "indexer");
1128 : /* empty */ { $$ = false; }
1129 | NEW { $$ = true; }
1132 interface_method_declaration
1133 : opt_attributes opt_new type IDENTIFIER
1134 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1137 $$ = new InterfaceMethod ((string) $3, (string) $4, (bool) $2,
1138 (Parameters) $6, (Attributes) $1, lexer.Location);
1140 | opt_attributes opt_new VOID IDENTIFIER
1141 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1144 $$ = new InterfaceMethod ("System.Void", (string) $4, (bool) $2, (Parameters) $6,
1145 (Attributes) $1, lexer.Location);
1149 interface_property_declaration
1154 { lexer.PropertyParsing = true; }
1156 { lexer.PropertyParsing = false; }
1161 $$ = new InterfaceProperty ((string) $3, (string) $4, (bool) $2,
1162 (gs & 1) == 1, (gs & 2) == 2, (Attributes) $1,
1168 : opt_attributes GET SEMICOLON { $$ = 1; }
1169 | opt_attributes SET SEMICOLON { $$ = 2; }
1170 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1172 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1176 interface_event_declaration
1177 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1179 $$ = new InterfaceEvent ((string) $4, (string) $5, (bool) $2, (Attributes) $1);
1183 interface_indexer_declaration
1184 : opt_attributes opt_new type THIS
1185 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1187 { lexer.PropertyParsing = true; }
1189 { lexer.PropertyParsing = false; }
1192 int a_flags = (int) $10;
1194 bool do_get = (a_flags & 1) == 1;
1195 bool do_set = (a_flags & 2) == 2;
1197 $$ = new InterfaceIndexer ((string) $3, (Parameters) $6, do_get, do_set,
1198 (bool) $2, (Attributes) $1, lexer.Location);
1202 operator_declaration
1203 : opt_attributes opt_modifiers operator_declarator block
1205 OperatorDeclaration decl = (OperatorDeclaration) $3;
1207 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
1208 decl.arg2type, decl.arg2name, (Block) $4, (Attributes) $1, decl.location);
1210 // Note again, checking is done in semantic analysis
1211 current_container.AddOperator (op);
1213 current_local_parameters = null;
1218 : type OPERATOR overloadable_operator
1219 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1221 Operator.OpType op = (Operator.OpType) $3;
1222 CheckUnaryOperator (op);
1224 if (op == Operator.OpType.Addition)
1225 op = Operator.OpType.UnaryPlus;
1227 if (op == Operator.OpType.Subtraction)
1228 op = Operator.OpType.UnaryNegation;
1230 Parameter [] pars = new Parameter [1];
1232 pars [0] = new Parameter ((string) $5, (string) $6, Parameter.Modifier.NONE, null);
1234 current_local_parameters = new Parameters (pars, null, lexer.Location);
1236 $$ = new OperatorDeclaration (op, (string) $1, (string) $5, (string) $6,
1237 null, null, lexer.Location);
1239 | type OPERATOR overloadable_operator
1241 type IDENTIFIER COMMA
1245 CheckBinaryOperator ((Operator.OpType) $3);
1247 Parameter [] pars = new Parameter [2];
1249 pars [0] = new Parameter ((string) $5, (string) $6, Parameter.Modifier.NONE, null);
1250 pars [1] = new Parameter ((string) $8, (string) $9, Parameter.Modifier.NONE, null);
1252 current_local_parameters = new Parameters (pars, null, lexer.Location);
1254 $$ = new OperatorDeclaration ((Operator.OpType) $3, (string) $1, (string) $5, (string) $6,
1255 (string) $8, (string) $9, lexer.Location);
1257 | conversion_operator_declarator
1260 overloadable_operator
1262 : BANG { $$ = Operator.OpType.LogicalNot; }
1263 | TILDE { $$ = Operator.OpType.OnesComplement; }
1264 | OP_INC { $$ = Operator.OpType.Increment; }
1265 | OP_DEC { $$ = Operator.OpType.Decrement; }
1266 | TRUE { $$ = Operator.OpType.True; }
1267 | FALSE { $$ = Operator.OpType.False; }
1268 // Unary and binary:
1269 | PLUS { $$ = Operator.OpType.Addition; }
1270 | MINUS { $$ = Operator.OpType.Subtraction; }
1272 | STAR { $$ = Operator.OpType.Multiply; }
1273 | DIV { $$ = Operator.OpType.Division; }
1274 | PERCENT { $$ = Operator.OpType.Modulus; }
1275 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1276 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1277 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1278 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1279 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1280 | OP_EQ { $$ = Operator.OpType.Equality; }
1281 | OP_NE { $$ = Operator.OpType.Inequality; }
1282 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1283 | OP_LT { $$ = Operator.OpType.LessThan; }
1284 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1285 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1288 conversion_operator_declarator
1289 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1291 Parameter [] pars = new Parameter [1];
1293 pars [0] = new Parameter ((string) $5, (string) $6, Parameter.Modifier.NONE, null);
1295 current_local_parameters = new Parameters (pars, null, lexer.Location);
1297 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (string) $3, (string) $5, (string) $6,
1298 null, null, lexer.Location);
1300 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1302 Parameter [] pars = new Parameter [1];
1304 pars [0] = new Parameter ((string) $5, (string) $6, Parameter.Modifier.NONE, null);
1306 current_local_parameters = new Parameters (pars, null, lexer.Location);
1308 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (string) $3, (string) $5, (string) $6,
1309 null, null, lexer.Location);
1313 syntax_error (lexer.Location, "'operator' expected");
1317 syntax_error (lexer.Location, "'operator' expected");
1321 constructor_declaration
1324 constructor_declarator
1327 Constructor c = (Constructor) $3;
1328 c.Block = (Block) $4;
1329 c.ModFlags = (int) $2;
1330 c.OptAttributes = (Attributes) $1;
1332 if ((c.ModFlags & Modifiers.STATIC) != 0){
1333 if ((c.ModFlags & Modifiers.Accessibility) != 0) {
1334 Location l = lexer.Location;
1335 Report.Error (515, l, "Access modifiers are not allowed on static constructors");
1338 if (c.Initializer != null){
1339 Location l = lexer.Location;
1340 Report.Error (514, l, "Static constructors can not have an explicit this or base constructor invocations");
1343 if (!c.Parameters.Empty){
1344 Location l = lexer.Location;
1345 Report.Error (132, l, "Static constructors should not have parameters");
1349 CheckDef (current_container.AddConstructor (c), c.Name);
1351 current_local_parameters = null;
1355 constructor_declarator
1357 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1359 oob_stack.Push (lexer.Location);
1361 current_local_parameters = (Parameters) $3;
1363 opt_constructor_initializer
1365 Location l = (Location) oob_stack.Pop ();
1366 $$ = new Constructor ((string) $1, (Parameters) $3, (ConstructorInitializer) $6, l);
1370 opt_constructor_initializer
1371 : /* empty */ { $$ = null; }
1372 | constructor_initializer
1375 constructor_initializer
1376 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1378 $$ = new ConstructorBaseInitializer ((ArrayList) $4, lexer.Location);
1380 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1382 $$ = new ConstructorThisInitializer ((ArrayList) $4, lexer.Location);
1386 destructor_declaration
1387 : opt_attributes TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1389 if ((string) $3 != current_container.Basename){
1390 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1391 } else if (!(current_container is Class)){
1392 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1394 Location l = lexer.Location;
1396 Method d = new Method (
1397 "System.Void", 0, "Finalize",
1398 new Parameters (null, null, l), (Attributes) $1, l);
1400 d.Block = (Block) $6;
1401 CheckDef (current_container.AddMethod (d), d.Name);
1409 EVENT type variable_declarators SEMICOLON
1411 foreach (VariableDeclaration var in (ArrayList) $5) {
1413 Event e = new Event ((string) $4, var.identifier, var.expression_or_array_initializer,
1414 (int) $2, null, null, (Attributes) $1, lexer.Location);
1416 CheckDef (current_container.AddEvent (e), e.Name);
1422 EVENT type member_name
1425 implicit_value_parameter_type = (string) $4;
1426 lexer.EventParsing = true;
1428 event_accessor_declarations
1430 lexer.EventParsing = false;
1434 Pair pair = (Pair) $8;
1435 Block add_block = null;
1436 Block rem_block = null;
1438 if (pair.First != null)
1439 add_block = (Block) pair.First;
1440 if (pair.Second != null)
1441 rem_block = (Block) pair.Second;
1443 Event e = new Event ((string) $4, (string) $5, null, (int) $2, add_block, rem_block,
1444 (Attributes) $1, lexer.Location);
1446 CheckDef (current_container.AddEvent (e), e.Name);
1447 implicit_value_parameter_type = null;
1451 event_accessor_declarations
1452 : add_accessor_declaration remove_accessor_declaration
1454 $$ = new Pair ($1, $2);
1456 | remove_accessor_declaration add_accessor_declaration
1458 $$ = new Pair ($2, $1);
1462 add_accessor_declaration
1463 : opt_attributes ADD
1465 Parameter [] args = new Parameter [1];
1466 Parameter implicit_value_parameter = new Parameter (
1467 implicit_value_parameter_type, "value",
1468 Parameter.Modifier.NONE, null);
1470 args [0] = implicit_value_parameter;
1472 current_local_parameters = new Parameters (args, null, lexer.Location);
1473 lexer.EventParsing = false;
1478 lexer.EventParsing = true;
1482 remove_accessor_declaration
1483 : opt_attributes REMOVE
1485 Parameter [] args = new Parameter [1];
1486 Parameter implicit_value_parameter = new Parameter (
1487 implicit_value_parameter_type, "value",
1488 Parameter.Modifier.NONE, null);
1490 args [0] = implicit_value_parameter;
1492 current_local_parameters = new Parameters (args, null, lexer.Location);
1493 lexer.EventParsing = false;
1498 lexer.EventParsing = true;
1503 : opt_attributes opt_modifiers indexer_declarator
1506 IndexerDeclaration decl = (IndexerDeclaration) $3;
1508 implicit_value_parameter_type = decl.type;
1510 lexer.PropertyParsing = true;
1511 parsing_indexer = true;
1513 indexer_parameters = decl.param_list;
1514 $$ = lexer.Location;
1516 accessor_declarations
1518 lexer.PropertyParsing = false;
1519 parsing_indexer = false;
1523 // The signature is computed from the signature of the indexer. Look
1524 // at section 3.6 on the spec
1527 IndexerDeclaration decl = (IndexerDeclaration) $3;
1528 Pair pair = (Pair) $6;
1529 Block get_block = null;
1530 Block set_block = null;
1532 if (pair.First != null)
1533 get_block = (Block) pair.First;
1534 if (pair.Second != null)
1535 set_block = (Block) pair.Second;
1537 indexer = new Indexer (decl.type, decl.interface_type, (int) $2, decl.param_list,
1538 get_block, set_block, (Attributes) $1, (Location) $5);
1540 // Note that there is no equivalent of CheckDef for this case
1541 // We shall handle this in semantic analysis
1543 current_container.AddIndexer (indexer);
1545 current_local_parameters = null;
1546 implicit_value_parameter_type = null;
1547 indexer_parameters = null;
1552 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1554 Parameters pars = (Parameters) $4;
1556 if (pars.FixedParameters == null){
1557 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1560 $$ = new IndexerDeclaration ((string) $1, null, pars);
1562 | type qualified_identifier DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1564 Parameters pars = (Parameters) $6;
1566 if (pars.FixedParameters == null){
1567 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1569 $$ = new IndexerDeclaration ((string) $1, (string) $2, pars);
1581 string full_name = MakeName ((string) $4);
1582 Enum e = new Enum (current_container, (string) $5, (int) $2, full_name, (Attributes) $1, lexer.Location);
1584 foreach (VariableDeclaration ev in (ArrayList) $6){
1585 CheckDef (e.AddEnumMember (ev.identifier,
1586 (Expression) ev.expression_or_array_initializer,
1587 (Location) ev.Location),
1591 CheckDef (current_container.AddEnum (e), name);
1596 : /* empty */ { $$ = "System.Int32"; }
1597 | COLON type { $$ = $2; }
1601 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1607 opt_enum_member_declarations
1608 : /* empty */ { $$ = new ArrayList (); }
1609 | enum_member_declarations opt_comma { $$ = $1; }
1612 enum_member_declarations
1613 : enum_member_declaration
1615 ArrayList l = new ArrayList ();
1620 | enum_member_declarations COMMA enum_member_declaration
1622 ArrayList l = (ArrayList) $1;
1630 enum_member_declaration
1631 : opt_attributes IDENTIFIER
1633 $$ = new VariableDeclaration ((string) $2, null, lexer.Location);
1635 | opt_attributes IDENTIFIER
1637 $$ = lexer.Location;
1641 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location);
1645 delegate_declaration
1649 IDENTIFIER OPEN_PARENS
1650 opt_formal_parameter_list
1654 Delegate del = new Delegate ((string) $4, (int) $2,
1655 MakeName ((string) $5), (Parameters) $7,
1656 (Attributes) $1, lexer.Location);
1658 CheckDef (current_container.AddDelegate (del), del.Name);
1663 IDENTIFIER OPEN_PARENS
1664 opt_formal_parameter_list
1668 Delegate del = new Delegate ("System.Void", (int) $2, (string) $5, (Parameters) $7,
1669 (Attributes) $1, lexer.Location);
1671 CheckDef (current_container.AddDelegate (del), del.Name);
1676 : namespace_or_type_name
1679 namespace_or_type_name
1680 : qualified_identifier
1684 * Before you think of adding a return_type, notice that we have been
1685 * using two rules in the places where it matters (one rule using type
1686 * and another identical one that uses VOID as the return type). This
1687 * gets rid of a shift/reduce couple
1690 : type_name { /* class_type */
1692 This does interfaces, delegates, struct_types, class_types,
1693 parent classes, and more! 4.2
1707 // Note that here only unmanaged types are allowed but we
1708 // can't perform checks during this phase - we do it during
1709 // semantic analysis.
1711 $$ = (string) $1 + "*";
1715 $$ = "System.Void*";
1722 $$ = new SimpleName ((string) $1, lexer.Location);
1724 | non_expression_type rank_specifier
1726 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1728 | non_expression_type STAR
1730 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1732 | expression rank_specifiers
1734 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1738 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
1745 ArrayList types = new ArrayList ();
1750 | type_list COMMA type
1752 ArrayList types = (ArrayList) $1;
1760 * replaces all the productions for isolating the various
1761 * simple types, but we need this to reuse it easily in local_variable_type
1764 : OBJECT { $$ = "System.Object"; }
1765 | STRING { $$ = "System.String"; }
1766 | BOOL { $$ = "System.Boolean"; }
1767 | DECIMAL { $$ = "System.Decimal"; }
1768 | FLOAT { $$ = "System.Single"; }
1769 | DOUBLE { $$ = "System.Double"; }
1774 : SBYTE { $$ = "System.SByte"; }
1775 | BYTE { $$ = "System.Byte"; }
1776 | SHORT { $$ = "System.Int16"; }
1777 | USHORT { $$ = "System.UInt16"; }
1778 | INT { $$ = "System.Int32"; }
1779 | UINT { $$ = "System.UInt32"; }
1780 | LONG { $$ = "System.Int64"; }
1781 | ULONG { $$ = "System.UInt64"; }
1782 | CHAR { $$ = "System.Char"; }
1790 : type rank_specifiers
1792 $$ = (string) $1 + (string) $2;
1797 // Expressions, section 7.5
1805 | qualified_identifier
1807 string name = (string) $1;
1810 $$ = DecomposeQI (name, lexer.Location);
1812 | parenthesized_expression
1814 | invocation_expression
1818 | post_increment_expression
1819 | post_decrement_expression
1823 | checked_expression
1824 | unchecked_expression
1825 | pointer_member_access
1826 // TODO: pointer_element_access
1827 // TODO: sizeof-expression
1834 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
1835 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
1836 | NULL { $$ = NullLiteral.Null; }
1840 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
1841 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
1842 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
1847 object v = lexer.Value;
1850 // FIXME: Possible optimization would be to
1851 // compute the *Literal objects directly in the scanner
1854 $$ = new IntLiteral ((Int32) v);
1856 $$ = new UIntLiteral ((UInt32) v);
1858 $$ = new LongLiteral ((Int64) v);
1859 else if (v is ulong)
1860 $$ = new ULongLiteral ((UInt64) v);
1862 Console.WriteLine ("OOPS. Unexpected result from scanner");
1867 : TRUE { $$ = new BoolLiteral (true); }
1868 | FALSE { $$ = new BoolLiteral (false); }
1871 parenthesized_expression
1872 : OPEN_PARENS expression CLOSE_PARENS
1877 : primary_expression DOT IDENTIFIER
1879 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1881 | predefined_type DOT IDENTIFIER
1883 $$ = new SimpleName ((string) $1 + "." + (string) $3, lexer.Location);
1891 invocation_expression
1892 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
1895 Location l = lexer.Location;
1896 Report.Error (1, l, "THIS IS CRAZY");
1898 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
1903 : /* empty */ { $$ = null; }
1910 ArrayList list = new ArrayList ();
1914 | argument_list COMMA argument
1916 ArrayList list = (ArrayList) $1;
1925 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
1927 | REF variable_reference
1929 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
1931 | OUT variable_reference
1933 $$ = new Argument ((Expression) $2, Argument.AType.Out);
1938 : expression { note ("section 5.4"); $$ = $1; }
1942 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
1944 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
1946 | primary_expression rank_specifiers
1948 // So the super-trick is that primary_expression
1949 // can only be either a SimpleName or a MemberAccess.
1950 // The MemberAccess case arises when you have a fully qualified type-name like :
1952 // SimpleName is when you have
1955 Expression expr = (Expression) $1;
1956 if (!(expr is SimpleName || expr is MemberAccess)) {
1957 Location l = lexer.Location;
1958 Report.Error (-1, l, "Invalid Type definition");
1959 $$ = "System.Object";
1963 // So we extract the string corresponding to the SimpleName
1966 $$ = new SimpleName (GetQualifiedIdentifier (expr) + (string) $2, lexer.Location);
1973 ArrayList list = new ArrayList ();
1977 | expression_list COMMA expression
1979 ArrayList list = (ArrayList) $1;
1988 $$ = new This (lexer.Location);
1993 : BASE DOT IDENTIFIER
1995 $$ = new BaseAccess ((string) $3, lexer.Location);
1997 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
1999 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2003 post_increment_expression
2004 : primary_expression OP_INC
2006 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2007 (Expression) $1, lexer.Location);
2011 post_decrement_expression
2012 : primary_expression OP_DEC
2014 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2015 (Expression) $1, lexer.Location);
2020 : object_or_delegate_creation_expression
2021 | array_creation_expression
2024 object_or_delegate_creation_expression
2025 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2027 $$ = new New ((string) $2, (ArrayList) $4, lexer.Location);
2031 array_creation_expression
2032 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2034 opt_array_initializer
2036 $$ = new ArrayCreation ((string) $2, (ArrayList) $4, (string) $6, (ArrayList) $7,
2039 | NEW type rank_specifiers array_initializer
2041 $$ = new ArrayCreation ((string) $2, (string) $3, (ArrayList) $4, lexer.Location);
2045 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2065 | rank_specifiers rank_specifier
2067 $$ = (string) $2 + (string) $1;
2072 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2074 $$ = "[" + (string) $2 + "]";
2094 | dim_separators COMMA
2096 $$ = (string) $1 + ",";
2100 opt_array_initializer
2112 : OPEN_BRACE CLOSE_BRACE
2114 ArrayList list = new ArrayList ();
2117 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2119 $$ = (ArrayList) $2;
2123 variable_initializer_list
2124 : variable_initializer
2126 ArrayList list = new ArrayList ();
2130 | variable_initializer_list COMMA variable_initializer
2132 ArrayList list = (ArrayList) $1;
2139 : TYPEOF OPEN_PARENS type CLOSE_PARENS
2141 $$ = new TypeOf ((string) $3, lexer.Location);
2146 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2147 $$ = new SizeOf ((string) $3, lexer.Location);
2149 note ("Verify type is unmanaged");
2150 note ("if (5.8) builtin, yield constant expression");
2155 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2157 $$ = new CheckedExpr ((Expression) $3);
2161 unchecked_expression
2162 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2164 $$ = new UnCheckedExpr ((Expression) $3);
2168 pointer_member_access
2169 : primary_expression OP_PTR IDENTIFIER
2173 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2174 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2178 : primary_expression
2179 | BANG prefixed_unary_expression
2181 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2183 | TILDE prefixed_unary_expression
2185 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2187 | OPEN_PARENS expression CLOSE_PARENS unary_expression
2189 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2191 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2193 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2198 // The idea to split this out is from Rhys' grammar
2199 // to solve the problem with casts.
2201 prefixed_unary_expression
2203 | PLUS prefixed_unary_expression
2205 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2207 | MINUS prefixed_unary_expression
2209 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2211 | OP_INC prefixed_unary_expression
2213 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2214 (Expression) $2, lexer.Location);
2216 | OP_DEC prefixed_unary_expression
2218 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2219 (Expression) $2, lexer.Location);
2221 | STAR prefixed_unary_expression
2223 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2225 | BITWISE_AND prefixed_unary_expression
2227 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2231 pre_increment_expression
2232 : OP_INC prefixed_unary_expression
2234 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2235 (Expression) $2, lexer.Location);
2239 pre_decrement_expression
2240 : OP_DEC prefixed_unary_expression
2242 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2243 (Expression) $2, lexer.Location);
2247 multiplicative_expression
2248 : prefixed_unary_expression
2249 | multiplicative_expression STAR prefixed_unary_expression
2251 $$ = new Binary (Binary.Operator.Multiply,
2252 (Expression) $1, (Expression) $3, lexer.Location);
2254 | multiplicative_expression DIV prefixed_unary_expression
2256 $$ = new Binary (Binary.Operator.Division,
2257 (Expression) $1, (Expression) $3, lexer.Location);
2259 | multiplicative_expression PERCENT prefixed_unary_expression
2261 $$ = new Binary (Binary.Operator.Modulus,
2262 (Expression) $1, (Expression) $3, lexer.Location);
2267 : multiplicative_expression
2268 | additive_expression PLUS multiplicative_expression
2270 $$ = new Binary (Binary.Operator.Addition,
2271 (Expression) $1, (Expression) $3, lexer.Location);
2273 | additive_expression MINUS multiplicative_expression
2275 $$ = new Binary (Binary.Operator.Subtraction,
2276 (Expression) $1, (Expression) $3, lexer.Location);
2281 : additive_expression
2282 | shift_expression OP_SHIFT_LEFT additive_expression
2284 $$ = new Binary (Binary.Operator.LeftShift,
2285 (Expression) $1, (Expression) $3, lexer.Location);
2287 | shift_expression OP_SHIFT_RIGHT additive_expression
2289 $$ = new Binary (Binary.Operator.RightShift,
2290 (Expression) $1, (Expression) $3, lexer.Location);
2294 relational_expression
2296 | relational_expression OP_LT shift_expression
2298 $$ = new Binary (Binary.Operator.LessThan,
2299 (Expression) $1, (Expression) $3, lexer.Location);
2301 | relational_expression OP_GT shift_expression
2303 $$ = new Binary (Binary.Operator.GreaterThan,
2304 (Expression) $1, (Expression) $3, lexer.Location);
2306 | relational_expression OP_LE shift_expression
2308 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2309 (Expression) $1, (Expression) $3, lexer.Location);
2311 | relational_expression OP_GE shift_expression
2313 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2314 (Expression) $1, (Expression) $3, lexer.Location);
2316 | relational_expression IS type
2318 $$ = new Is ((Expression) $1, (string) $3, lexer.Location);
2320 | relational_expression AS type
2322 $$ = new As ((Expression) $1, (string) $3, lexer.Location);
2327 : relational_expression
2328 | equality_expression OP_EQ relational_expression
2330 $$ = new Binary (Binary.Operator.Equality,
2331 (Expression) $1, (Expression) $3, lexer.Location);
2333 | equality_expression OP_NE relational_expression
2335 $$ = new Binary (Binary.Operator.Inequality,
2336 (Expression) $1, (Expression) $3, lexer.Location);
2341 : equality_expression
2342 | and_expression BITWISE_AND equality_expression
2344 $$ = new Binary (Binary.Operator.BitwiseAnd,
2345 (Expression) $1, (Expression) $3, lexer.Location);
2349 exclusive_or_expression
2351 | exclusive_or_expression CARRET and_expression
2353 $$ = new Binary (Binary.Operator.ExclusiveOr,
2354 (Expression) $1, (Expression) $3, lexer.Location);
2358 inclusive_or_expression
2359 : exclusive_or_expression
2360 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2362 $$ = new Binary (Binary.Operator.BitwiseOr,
2363 (Expression) $1, (Expression) $3, lexer.Location);
2367 conditional_and_expression
2368 : inclusive_or_expression
2369 | conditional_and_expression OP_AND inclusive_or_expression
2371 $$ = new Binary (Binary.Operator.LogicalAnd,
2372 (Expression) $1, (Expression) $3, lexer.Location);
2376 conditional_or_expression
2377 : conditional_and_expression
2378 | conditional_or_expression OP_OR conditional_and_expression
2380 $$ = new Binary (Binary.Operator.LogicalOr,
2381 (Expression) $1, (Expression) $3, lexer.Location);
2385 conditional_expression
2386 : conditional_or_expression
2387 | conditional_or_expression INTERR expression COLON expression
2389 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2393 assignment_expression
2394 : prefixed_unary_expression ASSIGN expression
2396 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2398 | prefixed_unary_expression OP_MULT_ASSIGN expression
2400 Location l = lexer.Location;
2402 $$ = new Assign ((Expression) $1,
2403 new Binary (Binary.Operator.Multiply,
2405 (Expression) $3, l), l);
2407 | prefixed_unary_expression OP_DIV_ASSIGN expression
2409 Location l = lexer.Location;
2411 $$ = new Assign ((Expression) $1,
2412 new Binary (Binary.Operator.Division,
2414 (Expression) $3, l), l);
2416 | prefixed_unary_expression OP_MOD_ASSIGN expression
2418 Location l = lexer.Location;
2420 $$ = new Assign ((Expression) $1,
2421 new Binary (Binary.Operator.Modulus,
2423 (Expression) $3, l), l);
2425 | prefixed_unary_expression OP_ADD_ASSIGN expression
2427 Location l = lexer.Location;
2429 $$ = new Assign ((Expression) $1,
2430 new Binary (Binary.Operator.Addition,
2432 (Expression) $3, l), l);
2434 | prefixed_unary_expression OP_SUB_ASSIGN expression
2436 Location l = lexer.Location;
2438 $$ = new Assign ((Expression) $1,
2439 new Binary (Binary.Operator.Subtraction,
2441 (Expression) $3, l), l);
2443 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2445 Location l = lexer.Location;
2447 $$ = new Assign ((Expression) $1,
2448 new Binary (Binary.Operator.LeftShift,
2450 (Expression) $3, l), l);
2452 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2454 Location l = lexer.Location;
2456 $$ = new Assign ((Expression) $1,
2457 new Binary (Binary.Operator.RightShift,
2459 (Expression) $3, l), l);
2461 | prefixed_unary_expression OP_AND_ASSIGN expression
2463 Location l = lexer.Location;
2465 $$ = new Assign ((Expression) $1,
2466 new Binary (Binary.Operator.BitwiseAnd,
2468 (Expression) $3, l), l);
2470 | prefixed_unary_expression OP_OR_ASSIGN expression
2472 Location l = lexer.Location;
2474 $$ = new Assign ((Expression) $1,
2475 new Binary (Binary.Operator.BitwiseOr,
2477 (Expression) $3, l), l);
2479 | prefixed_unary_expression OP_XOR_ASSIGN expression
2481 Location l = lexer.Location;
2483 $$ = new Assign ((Expression) $1,
2484 new Binary (Binary.Operator.ExclusiveOr,
2486 (Expression) $3, l), l);
2491 : conditional_expression
2492 | assignment_expression
2514 name = MakeName ((string) $4);
2516 new_class = new Class (current_container, name, (int) $2,
2517 (Attributes) $1, lexer.Location);
2518 current_container = new_class;
2519 current_container.Namespace = current_namespace;
2520 RootContext.Tree.RecordClass (name, new_class);
2526 Class new_class = (Class) current_container;
2529 new_class.Bases = (ArrayList) $6;
2531 current_container = current_container.Parent;
2532 CheckDef (current_container.AddClass (new_class), new_class.Name);
2539 : /* empty */ { $$ = (int) 0; }
2545 | modifiers modifier
2550 if ((m1 & m2) != 0) {
2551 Location l = lexer.Location;
2552 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2554 $$ = (int) (m1 | m2);
2559 : NEW { $$ = Modifiers.NEW; }
2560 | PUBLIC { $$ = Modifiers.PUBLIC; }
2561 | PROTECTED { $$ = Modifiers.PROTECTED; }
2562 | INTERNAL { $$ = Modifiers.INTERNAL; }
2563 | PRIVATE { $$ = Modifiers.PRIVATE; }
2564 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2565 | SEALED { $$ = Modifiers.SEALED; }
2566 | STATIC { $$ = Modifiers.STATIC; }
2567 | READONLY { $$ = Modifiers.READONLY; }
2568 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2569 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2570 | EXTERN { $$ = Modifiers.EXTERN; }
2571 | VOLATILE { $$ = Modifiers.VOLATILE; }
2572 | UNSAFE { $$ = Modifiers.UNSAFE; }
2576 : /* empty */ { $$ = null; }
2577 | class_base { $$ = $1; }
2581 : COLON type_list { $$ = $2; }
2589 // A block is "contained" on the following places:
2591 // property_declaration as part of the accessor body (get/set)
2592 // operator_declaration
2593 // constructor_declaration
2594 // destructor_declaration
2595 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2600 current_block = new Block (current_block);
2602 opt_statement_list CLOSE_BRACE
2604 while (current_block.Implicit)
2605 current_block = current_block.Parent;
2607 current_block = current_block.Parent;
2618 | statement_list statement
2622 : declaration_statement
2624 if ((Block) $1 != current_block){
2625 current_block.AddStatement ((Statement) $1);
2626 current_block = (Block) $1;
2629 | embedded_statement
2631 Statement s = (Statement) $1;
2634 current_block.AddStatement ((Statement) $1);
2642 | expression_statement
2643 | selection_statement
2644 | iteration_statement
2648 | unchecked_statement
2658 $$ = new EmptyStatement ();
2665 LabeledStatement labeled = new LabeledStatement ((string) $1);
2667 if (!current_block.AddLabel ((string) $1, labeled)){
2668 Location l = lexer.Location;
2669 Report.Error (140, l, "The label '" + ((string) $1) + "' is a duplicate");
2671 current_block.AddStatement (labeled);
2676 declaration_statement
2677 : local_variable_declaration SEMICOLON
2679 DictionaryEntry de = (DictionaryEntry) $1;
2681 $$ = declare_local_variables ((string) de.Key, (ArrayList) de.Value, lexer.Location);
2684 | local_constant_declaration SEMICOLON
2686 DictionaryEntry de = (DictionaryEntry) $1;
2688 $$ = declare_local_constant ((string) de.Key, (VariableDeclaration) de.Value);
2693 * The following is from Rhys' grammar:
2694 * > Types in local variable declarations must be recognized as
2695 * > expressions to prevent reduce/reduce errors in the grammar.
2696 * > The expressions are converted into types during semantic analysis.
2699 : primary_expression opt_rank_specifier
2701 // FIXME: Do something smart here regarding the composition of the type.
2703 // Ok, the above "primary_expression" is there to get rid of
2704 // both reduce/reduce and shift/reduces in the grammar, it should
2705 // really just be "type_name". If you use type_name, a reduce/reduce
2706 // creeps up. If you use qualified_identifier (which is all we need
2707 // really) two shift/reduces appear.
2710 // So the super-trick is that primary_expression
2711 // can only be either a SimpleName or a MemberAccess.
2712 // The MemberAccess case arises when you have a fully qualified type-name like :
2714 // SimpleName is when you have
2717 Expression expr = (Expression) $1;
2718 if (!(expr is SimpleName || expr is MemberAccess)) {
2719 Location l = lexer.Location;
2720 Report.Error (-1, l, "Invalid Type definition");
2721 $$ = "System.Object";
2725 // So we extract the string corresponding to the SimpleName
2728 $$ = GetQualifiedIdentifier (expr) + (string) $2;
2730 | builtin_types opt_rank_specifier
2732 $$ = (string) $1 + (string) $2;
2736 local_variable_pointer_type
2737 : primary_expression STAR
2739 Expression expr = (Expression) $1;
2740 if (!(expr is SimpleName || expr is MemberAccess)) {
2741 Location l = lexer.Location;
2742 Report.Error (-1, l, "Invalid Type definition");
2743 $$ = "System.Object";
2746 $$ = GetQualifiedIdentifier (expr) + "*";
2748 | builtin_types STAR
2750 $$ = (string) $1 + "*";
2754 $$ = "System.Void*";
2756 | local_variable_pointer_type STAR
2758 $$ = (string) $1 + "*";
2762 local_variable_declaration
2763 : local_variable_type variable_declarators
2765 $$ = new DictionaryEntry ($1, $2);
2767 | local_variable_pointer_type opt_rank_specifier variable_declarators
2769 $$ = new DictionaryEntry ((string) $1 + (string) $2, $3);
2773 local_constant_declaration
2774 : CONST local_variable_type constant_declarator
2776 $$ = new DictionaryEntry ($2, $3);
2780 expression_statement
2781 : statement_expression SEMICOLON
2788 // We have to do the wrapping here and not in the case above,
2789 // because statement_expression is used for example in for_statement
2791 statement_expression
2792 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2793 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2794 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2795 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2796 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2797 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2798 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2800 Report.Error (1002, lexer.Location, "Expecting `;'");
2804 object_creation_expression
2805 : object_or_delegate_creation_expression
2806 { note ("complain if this is a delegate maybe?"); }
2815 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
2818 $$ = new If ((Expression) $3, (Statement) $5);
2820 if (RootContext.WarningLevel >= 3){
2821 if ($5 is EmptyStatement)
2822 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
2826 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
2827 embedded_statement ELSE embedded_statement
2829 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7);
2834 : SWITCH OPEN_PARENS
2836 oob_stack.Push (lexer.Location);
2838 expression CLOSE_PARENS
2841 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
2857 Report.Error (1522, lexer.Location, "Empty switch block");
2865 ArrayList sections = new ArrayList ();
2870 | switch_sections switch_section
2872 ArrayList sections = (ArrayList) $1;
2882 current_block = new Block (current_block);
2886 while (current_block.Implicit)
2887 current_block = current_block.Parent;
2888 $$ = new SwitchSection ((ArrayList) $1, current_block);
2889 current_block = current_block.Parent;
2896 ArrayList labels = new ArrayList ();
2901 | switch_labels switch_label
2903 ArrayList labels = (ArrayList) ($1);
2911 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
2912 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
2915 1523, lexer.Location,
2916 "The keyword case or default must precede code in switch block");
2928 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
2930 $$ = new While ((Expression) $3, (Statement) $5);
2932 if (RootContext.WarningLevel >= 3){
2933 if ($5 is EmptyStatement)
2934 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
2940 : DO embedded_statement
2941 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
2943 $$ = new Do ((Statement) $2, (Expression) $5);
2949 opt_for_initializer SEMICOLON
2951 Block assign_block = new Block (current_block);
2952 current_block = assign_block;
2954 if ($3 is DictionaryEntry){
2955 DictionaryEntry de = (DictionaryEntry) $3;
2957 string type = (string) de.Key;
2958 ArrayList var_declarators = (ArrayList) de.Value;
2960 foreach (VariableDeclaration decl in var_declarators){
2962 if (!current_block.AddVariable (type, decl.identifier, current_local_parameters,
2964 Report.Error (128, decl.Location,
2965 "A local variable `" + decl.identifier + "' is already" +
2966 "defined in this scope");
2969 Location l = lexer.Location;
2971 if (decl.expression_or_array_initializer is Expression){
2972 expr = (Expression) decl.expression_or_array_initializer;
2976 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
2978 string base_type = type.Substring (0, type.IndexOf ("["));
2979 string rank = type.Substring (type.IndexOf ("["));
2981 expr = new ArrayCreation (base_type, rank, init, decl.Location);
2984 LocalVariableReference var;
2985 var = new LocalVariableReference (
2986 assign_block, decl.identifier, l);
2988 Assign a = new Assign (var, expr, decl.Location);
2990 assign_block.AddStatement (new StatementExpression (a));
2997 opt_for_condition SEMICOLON
2998 opt_for_iterator CLOSE_PARENS
3001 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10);
3003 if (RootContext.WarningLevel >= 3){
3004 if ($10 is EmptyStatement)
3005 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3008 current_block.AddStatement (f);
3009 while (current_block.Implicit)
3010 current_block = current_block.Parent;
3012 current_block = current_block.Parent;
3017 : /* empty */ { $$ = new EmptyStatement (); }
3022 : local_variable_declaration
3023 | statement_expression_list
3027 : /* empty */ { $$ = null; }
3028 | boolean_expression
3032 : /* empty */ { $$ = new EmptyStatement (); }
3037 : statement_expression_list
3040 statement_expression_list
3041 : statement_expression
3043 Block b = new Block (null, true);
3045 b.AddStatement ((Statement) $1);
3048 | statement_expression_list COMMA statement_expression
3050 Block b = (Block) $1;
3052 b.AddStatement ((Statement) $3);
3058 : FOREACH OPEN_PARENS type IDENTIFIER IN
3060 oob_stack.Push (lexer.Location);
3062 expression CLOSE_PARENS
3064 Block foreach_block = new Block (current_block, true);
3065 LocalVariableReference v;
3066 Location l = lexer.Location;
3068 foreach_block.AddVariable ((string) $3, (string) $4, current_local_parameters, l);
3069 v = new LocalVariableReference (foreach_block, (string) $4, l);
3071 current_block.AddStatement (foreach_block);
3072 current_block = foreach_block;
3074 oob_stack.Push (foreach_block);
3079 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3080 Block foreach_block = (Block) oob_stack.Pop ();
3081 Location l = (Location) oob_stack.Pop ();
3083 $$ = new Foreach ((string) $3, v, (Expression) $7, (Statement) $10, l);
3089 | continue_statement
3098 $$ = new Break (lexer.Location);
3103 : CONTINUE SEMICOLON
3105 $$ = new Continue (lexer.Location);
3110 : GOTO IDENTIFIER SEMICOLON
3112 $$ = new Goto (current_block, (string) $2, lexer.Location);
3114 | GOTO CASE constant_expression SEMICOLON
3116 $$ = new GotoCase ((Expression) $3, lexer.Location);
3118 | GOTO DEFAULT SEMICOLON
3120 $$ = new GotoDefault (lexer.Location);
3125 : RETURN opt_expression SEMICOLON
3127 $$ = new Return ((Expression) $2, lexer.Location);
3132 : THROW opt_expression SEMICOLON
3134 $$ = new Throw ((Expression) $2, lexer.Location);
3144 : TRY block catch_clauses
3147 ArrayList s = new ArrayList ();
3149 foreach (Catch cc in (ArrayList) $3) {
3150 if (cc.Type == null)
3156 // Now s contains the list of specific catch clauses
3157 // and g contains the general one.
3159 $$ = new Try ((Block) $2, s, g, null);
3161 | TRY block opt_catch_clauses FINALLY block
3164 ArrayList s = new ArrayList ();
3166 foreach (Catch cc in (ArrayList) $3) {
3167 if (cc.Type == null)
3173 $$ = new Try ((Block) $2, s, g, (Block) $5);
3177 Report.Error (1524, lexer.Location, "Expected catch or finally");
3182 : /* empty */ { $$ = null; }
3189 ArrayList l = new ArrayList ();
3194 | catch_clauses catch_clause
3196 ArrayList l = (ArrayList) $1;
3204 : /* empty */ { $$ = null; }
3209 : CATCH opt_catch_args
3211 string type = null, id = null;
3214 DictionaryEntry cc = (DictionaryEntry) $2;
3215 type = (string) cc.Key;
3216 id = (string) cc.Value;
3219 ArrayList one = new ArrayList ();
3220 Location loc = lexer.Location;
3222 one.Add (new VariableDeclaration (id, null, loc));
3225 current_block = new Block (current_block);
3226 Block b = declare_local_variables (type, one, loc);
3233 string type = null, id = null;
3236 DictionaryEntry cc = (DictionaryEntry) $2;
3237 type = (string) cc.Key;
3238 id = (string) cc.Value;
3241 while (current_block != (Block) $1)
3242 current_block = current_block.Parent;
3247 $$ = new Catch (type, id , (Block) $4, lexer.Location);
3252 : /* empty */ { $$ = null; }
3257 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3259 $$ = new DictionaryEntry ($2, $3);
3266 $$ = new Checked ((Block) $2);
3273 $$ = new Unchecked ((Block) $2);
3280 if (!RootContext.Unsafe){
3281 Report.Error (227, lexer.Location,
3282 "Unsafe code can only be used if --unsafe is used");
3285 $$ = new Unsafe ((Block) $3);
3291 // FIXME: pointer_type fixed_pointer_declarators
3292 CLOSE_PARENS embedded_statement
3294 // $$ = new Fixed ((string) $3, $4, (Statement) $6, lexer.Location);
3298 fixed_pointer_declarators
3299 : fixed_pointer_declarator
3300 | fixed_pointer_declarators COMMA fixed_pointer_declarator
3303 fixed_pointer_declarator
3304 : IDENTIFIER EQUALS fixed_pointer_initializer
3307 fixed_pointer_initializer
3308 : BITWISE_AND variable_reference
3313 : LOCK OPEN_PARENS expression CLOSE_PARENS
3319 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
3324 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
3326 Block assign_block = new Block (current_block);
3327 current_block = assign_block;
3329 oob_stack.Push (lexer.Location);
3331 if ($3 is DictionaryEntry){
3332 DictionaryEntry de = (DictionaryEntry) $3;
3333 Location l = lexer.Location;
3335 string type = (string) de.Key;
3336 ArrayList var_declarators = (ArrayList) de.Value;
3338 foreach (VariableDeclaration decl in var_declarators){
3339 if (!current_block.AddVariable (type, decl.identifier, current_local_parameters,
3341 Report.Error (128, decl.Location,
3342 "A local variable `" + decl.identifier + "' is already" +
3343 "defined in this scope");
3347 ArrayList vars = new ArrayList ();
3349 foreach (VariableDeclaration decl in var_declarators){
3352 if (decl.expression_or_array_initializer is Expression){
3353 expr = (Expression) decl.expression_or_array_initializer;
3357 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3359 string base_type = type.Substring (0, type.IndexOf ("["));
3360 string rank = type.Substring (type.IndexOf ("["));
3362 expr = new ArrayCreation (base_type, rank, init, decl.Location);
3365 LocalVariableReference var;
3368 var = new LocalVariableReference (assign_block, decl.identifier, l);
3369 vi = var.VariableInfo;
3372 // This is so that it is not a warning on using variables
3375 vars.Add (new DictionaryEntry (var, expr));
3377 // Assign a = new Assign (var, expr, decl.Location);
3378 // assign_block.AddStatement (new StatementExpression (a));
3380 $3 = new DictionaryEntry (type, vars);
3385 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
3386 current_block.AddStatement (u);
3387 while (current_block.Implicit)
3388 current_block = current_block.Parent;
3390 current_block = current_block.Parent;
3394 resource_acquisition
3395 : local_variable_declaration
3402 // A class used to pass around variable declarations and constants
3404 public class VariableDeclaration {
3405 public string identifier;
3406 public object expression_or_array_initializer;
3407 public Location Location;
3409 public VariableDeclaration (string id, object eoai, Location l){
3410 this.identifier = id;
3411 this.expression_or_array_initializer = eoai;
3417 // A class used to hold info about an indexer declarator
3420 public class IndexerDeclaration {
3422 public string interface_type;
3423 public Parameters param_list;
3425 public IndexerDeclaration (string type, string interface_type, Parameters param_list)
3428 this.interface_type = interface_type;
3429 this.param_list = param_list;
3434 // A class used to hold info about an operator declarator
3437 public class OperatorDeclaration {
3438 public Operator.OpType optype;
3439 public string ret_type;
3440 public string arg1type;
3441 public string arg1name;
3442 public string arg2type;
3443 public string arg2name;
3444 public Location location;
3446 public OperatorDeclaration (Operator.OpType op, string ret_type, string arg1type, string arg1name,
3447 string arg2type, string arg2name, Location location)
3450 this.ret_type = ret_type;
3451 this.arg1type = arg1type;
3452 this.arg1name = arg1name;
3453 this.arg2type = arg2type;
3454 this.arg2name = arg2name;
3455 this.location = location;
3461 // Given the @class_name name, it creates a fully qualified name
3462 // based on the containing declaration space
3465 MakeName (string class_name)
3467 string ns = current_namespace.Name;
3468 string container_name = current_container.Name;
3470 if (container_name == ""){
3472 return ns + "." + class_name;
3476 return container_name + "." + class_name;
3480 // Used to report back to the user the result of a declaration
3481 // in the current declaration space
3484 CheckDef (AdditionResult result, string name)
3486 if (result == AdditionResult.Success)
3489 Location l = lexer.Location;
3492 case AdditionResult.NameExists:
3493 Report.Error (102, l, "The namespace `" + current_container.Name +
3494 "' already contains a definition for `"+
3500 // This is handled only for static Constructors, because
3501 // in reality we handle these by the semantic analysis later
3503 case AdditionResult.MethodExists:
3505 111, l, "Class `"+current_container.Name+
3506 "' already defines a member called '" +
3507 name + "' with the same parameter types (more than one default constructor)");
3510 case AdditionResult.EnclosingClash:
3511 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
3514 case AdditionResult.NotAConstructor:
3515 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
3521 CheckDef (bool result, string name)
3525 CheckDef (AdditionResult.NameExists, name);
3529 SimpleLookup (string name, Location loc)
3532 // we need to check against current_block not being null
3533 // as `expression' is allowed in argument_lists, which
3534 // do not exist inside a block.
3536 if (current_block != null){
3537 if (current_block.IsVariableDefined (name))
3538 return new LocalVariableReference (current_block, name, loc);
3541 if (current_local_parameters != null){
3543 Parameter par = current_local_parameters.GetParameterByName (name, out idx);
3545 return new ParameterReference (current_local_parameters, idx, name);
3551 Expression DecomposeQI (string name, Location loc)
3555 if (name.IndexOf ('.') == -1){
3556 o = SimpleLookup (name, loc);
3558 return new SimpleName (name, loc);
3561 int pos = name.LastIndexOf (".");
3562 string left = name.Substring (0, pos);
3563 string right = name.Substring (pos + 1);
3565 o = DecomposeQI (left, loc);
3566 return new MemberAccess (o, right, loc);
3571 // This method is used to get at the complete string representation of
3572 // a fully-qualified type name, hiding inside a MemberAccess ;-)
3573 // This is necessary because local_variable_type admits primary_expression
3574 // as the type of the variable. So we do some extra checking
3576 string GetQualifiedIdentifier (Expression expr)
3578 if (expr is SimpleName)
3579 return ((SimpleName)expr).Name;
3580 else if (expr is MemberAccess)
3581 return GetQualifiedIdentifier (((MemberAccess)expr).Expr) + "." + ((MemberAccess) expr).Identifier;
3583 throw new Exception ("Expr has to be either SimpleName or MemberAccess! (" + expr + ")");
3587 Block declare_local_variables (string type, ArrayList variable_declarators, Location loc)
3589 Block implicit_block;
3590 ArrayList inits = null;
3593 // We use the `Used' property to check whether statements
3594 // have been added to the current block. If so, we need
3595 // to create another block to contain the new declaration
3596 // otherwise, as an optimization, we use the same block to
3597 // add the declaration.
3599 // FIXME: A further optimization is to check if the statements
3600 // that were added were added as part of the initialization
3601 // below. In which case, no other statements have been executed
3602 // and we might be able to reduce the number of blocks for
3603 // situations like this:
3605 // int j = 1; int k = j + 1;
3607 if (current_block.Used)
3608 implicit_block = new Block (current_block, true);
3610 implicit_block = current_block;
3612 foreach (VariableDeclaration decl in variable_declarators){
3614 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location)) {
3615 if (decl.expression_or_array_initializer != null){
3617 inits = new ArrayList ();
3621 Report.Error (128, decl.Location, "A local variable `" + decl.identifier +
3622 "' is already defined in this scope");
3627 return implicit_block;
3629 foreach (VariableDeclaration decl in inits){
3633 if (decl.expression_or_array_initializer is Expression){
3634 expr = (Expression) decl.expression_or_array_initializer;
3637 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3639 string base_type = type.Substring (0, type.IndexOf ("["));
3640 string rank = type.Substring (type.IndexOf ("["));
3642 expr = new ArrayCreation (base_type, rank, init, decl.Location);
3646 LocalVariableReference var;
3647 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
3649 assign = new Assign (var, expr, decl.Location);
3651 implicit_block.AddStatement (new StatementExpression (assign));
3654 return implicit_block;
3657 Block declare_local_constant (string type, VariableDeclaration decl)
3659 Block implicit_block;
3661 if (current_block.Used)
3662 implicit_block = new Block (current_block, true);
3664 implicit_block = current_block;
3666 if (!(implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
3667 current_local_parameters, decl.Location))){
3668 Report.Error (128, decl.Location, "A local variable `" + decl.identifier +
3669 "' is already defined in this scope");
3672 return implicit_block;
3675 void CheckAttributeTarget (string a)
3679 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
3683 Location l = lexer.Location;
3684 Report.Error (658, l, "Invalid attribute target");
3690 void CheckUnaryOperator (Operator.OpType op)
3694 case Operator.OpType.LogicalNot:
3695 case Operator.OpType.OnesComplement:
3696 case Operator.OpType.Increment:
3697 case Operator.OpType.Decrement:
3698 case Operator.OpType.True:
3699 case Operator.OpType.False:
3700 case Operator.OpType.Addition:
3701 case Operator.OpType.Subtraction:
3706 Location l = lexer.Location;
3707 Report.Error (1019, l, "Overloadable unary operator expected");
3713 void CheckBinaryOperator (Operator.OpType op)
3717 case Operator.OpType.Addition:
3718 case Operator.OpType.Subtraction:
3719 case Operator.OpType.Multiply:
3720 case Operator.OpType.Division:
3721 case Operator.OpType.Modulus:
3722 case Operator.OpType.BitwiseAnd:
3723 case Operator.OpType.BitwiseOr:
3724 case Operator.OpType.ExclusiveOr:
3725 case Operator.OpType.LeftShift:
3726 case Operator.OpType.RightShift:
3727 case Operator.OpType.Equality:
3728 case Operator.OpType.Inequality:
3729 case Operator.OpType.GreaterThan:
3730 case Operator.OpType.LessThan:
3731 case Operator.OpType.GreaterThanOrEqual:
3732 case Operator.OpType.LessThanOrEqual:
3736 Location l = lexer.Location;
3737 Report.Error (1020, l, "Overloadable binary operator expected");
3743 void syntax_error (Location l, string msg)
3745 Report.Error (1003, l, "Syntax error, " + msg);
3748 void output (string s)
3750 Console.WriteLine (s);
3753 void note (string s)
3755 // Used to put annotations
3760 public Tokenizer Lexer {
3766 public CSharpParser (string name, System.IO.Stream input, ArrayList defines)
3768 current_namespace = new Namespace (null, "");
3771 current_container = RootContext.Tree.Types;
3772 current_container.Namespace = current_namespace;
3773 oob_stack = new Stack ();
3775 lexer = new Tokenizer (input, name, defines);
3778 public override int parse ()
3780 StringBuilder value = new StringBuilder ();
3784 if (yacc_verbose_flag)
3785 yyparse (lexer, new yydebug.yyDebugSimple ());
3788 } catch (Exception e){
3789 // Console.WriteLine ("Fatal error: " + name);
3790 // Console.WriteLine (lexer.location);
3793 // Please do not remove this, it is used during debugging
3796 Console.WriteLine (lexer.location + " : Parsing error ");
3797 Console.WriteLine (e);
3801 return global_errors;