3 // cs-parser.jay: The Parser for the C# compiler
5 // Authors: Miguel de Icaza (miguel@gnu.org)
6 // Ravi Pratap (ravi@ximian.com)
8 // Licensed under the terms of the GNU GPL
10 // (C) 2001 Ximian, Inc (http://www.ximian.com)
13 // (1) Figure out why error productions dont work. `type-declaration' is a
14 // great spot to put an `error' because you can reproduce it with this input:
17 // Possible optimization:
18 // Run memory profiler with parsing only, and consider dropping
19 // arraylists where not needed. Some pieces can use linked lists.
27 using System.Collections;
32 public class CSharpParser {
33 NamespaceEntry current_namespace;
34 TypeContainer current_container;
36 IIteratorContainer iterator_container;
39 /// Current block is used to add statements as we find
46 /// If true, creates a toplevel block in the block production
47 /// This is flagged by the delegate creation
49 bool create_toplevel_block;
53 /// Current interface is used by the various declaration
54 /// productions in the interface declaration to "add"
55 /// the interfaces as we find them.
57 Interface current_interface;
60 /// This is used by the unary_expression code to resolve
61 /// a name against a parameter.
63 Parameters current_local_parameters;
66 /// Using during property parsing to describe the implicit
67 /// value parameter that is passed to the "set" and "get"accesor
68 /// methods (properties and indexers).
70 Expression implicit_value_parameter_type;
71 Parameters indexer_parameters;
74 /// Used to determine if we are parsing the get/set pair
75 /// of an indexer or a property
80 /// An out-of-band stack.
89 public bool yacc_verbose_flag;
91 // Name of the file we are parsing
101 %token NONE /* This token is never returned by our lexer */
102 %token ERROR // This is used not by the parser, but by the tokenizer.
106 *These are the C# keywords
194 /* C# keywords which are not really keywords */
200 /* C# single character operators/punctuation. */
201 %token OPEN_BRACE "{"
202 %token CLOSE_BRACE "}"
203 %token OPEN_BRACKET "["
204 %token CLOSE_BRACKET "]"
205 %token OPEN_PARENS "("
206 %token CLOSE_PARENS ")"
218 %token OP_GENERICS_LT "<"
220 %token OP_GENERICS_GT ">"
221 %token BITWISE_AND "&"
222 %token BITWISE_OR "|"
229 /* C# multi-character operators. */
232 %token OP_SHIFT_LEFT "<<"
233 %token OP_SHIFT_RIGHT ">>"
240 %token OP_MULT_ASSIGN "*="
241 %token OP_DIV_ASSIGN "/="
242 %token OP_MOD_ASSIGN "%="
243 %token OP_ADD_ASSIGN "+="
244 %token OP_SUB_ASSIGN "-="
245 %token OP_SHIFT_LEFT_ASSIGN "<<="
246 %token OP_SHIFT_RIGHT_ASSIGN ">>="
247 %token OP_AND_ASSIGN "&="
248 %token OP_XOR_ASSIGN "^="
249 %token OP_OR_ASSIGN "|="
253 %token LITERAL_INTEGER "int literal"
254 %token LITERAL_FLOAT "float literal"
255 %token LITERAL_DOUBLE "double literal"
256 %token LITERAL_DECIMAL "decimal literal"
257 %token LITERAL_CHARACTER "character literal"
258 %token LITERAL_STRING "string literal"
261 %token CLOSE_PARENS_CAST
262 %token CLOSE_PARENS_NO_CAST
263 %token CLOSE_PARENS_OPEN_PARENS
264 %token CLOSE_PARENS_MINUS
266 /* Add precedence rules to solve dangling else s/r conflict */
275 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
277 %left STAR DIV PERCENT
278 %right BANG CARRET UMINUS
279 %nonassoc OP_INC OP_DEC
281 %left OPEN_BRACKET OPEN_BRACE
285 %start compilation_unit
289 : outer_declarations opt_EOF
290 | outer_declarations attribute_sections opt_EOF
291 | attribute_sections opt_EOF
292 | opt_EOF /* allow empty files */
302 | outer_declarations outer_declaration
307 | namespace_member_declaration
312 | using_directives using_directive
316 : using_alias_directive
317 | using_namespace_directive
320 using_alias_directive
321 : USING IDENTIFIER ASSIGN
322 namespace_or_type_name SEMICOLON
324 current_namespace.UsingAlias ((string) $2, (Expression) $4, lexer.Location);
327 CheckIdentifierToken (yyToken);
331 using_namespace_directive
332 : USING namespace_name SEMICOLON
334 current_namespace.Using ((string) $2, lexer.Location);
339 // Strictly speaking, namespaces don't have attributes but
340 // we parse global attributes along with namespace declarations and then
343 namespace_declaration
344 : opt_attributes NAMESPACE namespace_or_type_name
346 Attributes attrs = (Attributes) $1;
349 foreach (AttributeSection asec in attrs.AttributeSections)
350 if (asec.Target == "assembly")
351 RootContext.AddGlobalAttributeSection (current_container, asec);
353 Report.Error(1518, Lexer.Location,
354 "Attributes cannot be applied to namespaces."
355 + " Expected class, delegate, enum, interface, or struct");
358 current_namespace = RootContext.Tree.RecordNamespace (current_namespace, file, $3.ToString (), lexer.Location);
360 namespace_body opt_semicolon
362 current_namespace = current_namespace.Parent;
377 : namespace_or_type_name { $$ = ($1).ToString (); }
383 opt_namespace_member_declarations
394 opt_namespace_member_declarations
396 | namespace_member_declarations
399 namespace_member_declarations
400 : namespace_member_declaration
401 | namespace_member_declarations namespace_member_declaration
404 namespace_member_declaration
411 Class c = (Class) $1;
412 mod_flags = c.ModFlags;
414 } else if ($1 is Struct){
415 Struct s = (Struct) $1;
416 mod_flags = s.ModFlags;
421 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
423 1527, lexer.Location,
424 "Namespace elements cant be explicitly " +
425 "declared private or protected in `" + name + "'");
427 current_namespace.DeclarationFound = true;
429 | namespace_declaration {
430 current_namespace.DeclarationFound = true;
433 | field_declaration {
434 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
436 | method_declaration {
437 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
444 | interface_declaration
446 | delegate_declaration
448 // Enable this when we have handled all errors, because this acts as a generic fallback
451 // Console.WriteLine ("Token=" + yyToken);
452 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
462 | attribute_sections { $$ = $1; }
468 AttributeSection sect = (AttributeSection) $1;
470 if (sect.Target == "assembly") {
471 RootContext.AddGlobalAttributeSection (current_container, sect);
475 $$ = new Attributes (sect);
477 | attribute_sections attribute_section
479 Attributes attrs = $1 as Attributes;
480 AttributeSection sect = (AttributeSection) $2;
482 if (sect.Target == "assembly") {
483 RootContext.AddGlobalAttributeSection (current_container, sect);
486 attrs = new Attributes (sect);
488 attrs.AddAttributeSection (sect);
496 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
498 string target = null;
501 target = (string) $2;
503 $$ = new AttributeSection (target, (ArrayList) $3);
505 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
507 $$ = new AttributeSection (null, (ArrayList) $2);
511 attribute_target_specifier
512 : attribute_target COLON
521 CheckAttributeTarget ((string) $1);
524 | EVENT { $$ = "event"; }
525 | RETURN { $$ = "return"; }
531 ArrayList attrs = new ArrayList (4);
537 | attribute_list COMMA attribute
539 ArrayList attrs = (ArrayList) $1;
551 opt_attribute_arguments
554 // Attributes need a string, not an expression: generic types will fail here.
556 $$ = new Attribute (((Expression) $1).ToString (), (ArrayList) $3, (Location) $2);
561 : type_name { /* reserved attribute name or identifier: 17.4 */ }
564 opt_attribute_arguments
565 : /* empty */ { $$ = null; }
566 | OPEN_PARENS attribute_arguments CLOSE_PARENS
574 : opt_positional_argument_list
579 ArrayList args = new ArrayList (4);
585 | positional_argument_list COMMA named_argument_list
587 ArrayList args = new ArrayList (4);
593 | named_argument_list
595 ArrayList args = new ArrayList (4);
604 opt_positional_argument_list
605 : /* empty */ { $$ = null; }
606 | positional_argument_list
609 positional_argument_list
612 ArrayList args = new ArrayList (4);
613 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
617 | positional_argument_list COMMA expression
619 ArrayList args = (ArrayList) $1;
620 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
629 ArrayList args = new ArrayList (4);
634 | named_argument_list COMMA named_argument
636 ArrayList args = (ArrayList) $1;
644 : IDENTIFIER ASSIGN expression
646 $$ = new DictionaryEntry (
648 new Argument ((Expression) $3, Argument.AType.Expression));
654 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
657 opt_class_member_declarations
659 | class_member_declarations
662 class_member_declarations
663 : class_member_declaration
664 | class_member_declarations
665 class_member_declaration
668 class_member_declaration
669 : constant_declaration // done
670 | field_declaration // done
671 | method_declaration // done
672 | property_declaration // done
673 | event_declaration // done
674 | indexer_declaration // done
675 | operator_declaration // done
676 | constructor_declaration // done
677 | destructor_declaration // done
687 string full_struct_name = MakeName ((string) $4);
689 new_struct = new Struct (current_namespace, current_container, full_struct_name,
690 (int) $2, (Attributes) $1, lexer.Location);
691 current_container = new_struct;
692 RootContext.Tree.RecordDecl (full_struct_name, new_struct);
693 lexer.ConstraintsParsing = true;
695 opt_type_parameter_list
697 opt_type_parameter_constraints_clauses
699 lexer.ConstraintsParsing = false;
704 Struct new_struct = (Struct) current_container;
706 if ($8 != null && $6 == null)
707 Report.Error (-200, new_struct.Location,
708 "Type parameter constraints only valid if there is a type parameter list");
710 CheckDef (new_struct.SetParameterInfo ((ArrayList) $6, (ArrayList) $8), new_struct.Name, new_struct.Location);
713 new_struct.Bases = (ArrayList) $7;
715 current_container = current_container.Parent;
716 CheckDef (current_container.AddStruct (new_struct), new_struct.Name, new_struct.Location);
719 | opt_attributes opt_modifiers STRUCT error {
720 CheckIdentifierToken (yyToken);
725 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
728 opt_struct_member_declarations
730 | struct_member_declarations
733 struct_member_declarations
734 : struct_member_declaration
735 | struct_member_declarations struct_member_declaration
738 struct_member_declaration
739 : constant_declaration
742 | property_declaration
744 | indexer_declaration
745 | operator_declaration
746 | constructor_declaration
750 * This is only included so we can flag error 575:
751 * destructors only allowed on class types
753 | destructor_declaration
764 foreach (VariableDeclaration constant in (ArrayList) $5){
765 Location l = constant.Location;
767 Const c = new Const (
768 (Expression) $4, (string) constant.identifier,
769 (Expression) constant.expression_or_array_initializer, (int) $2,
772 CheckDef (current_container.AddConstant (c), c.Name, l);
778 : constant_declarator
780 ArrayList constants = new ArrayList (4);
784 | constant_declarators COMMA constant_declarator
786 ArrayList constants = (ArrayList) $1;
793 : IDENTIFIER ASSIGN constant_expression
795 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
806 Expression type = (Expression) $3;
809 foreach (VariableDeclaration var in (ArrayList) $4){
810 Location l = var.Location;
812 Field field = new Field (type, mod, var.identifier,
813 var.expression_or_array_initializer,
816 CheckDef (current_container.AddField (field), field.Name, l);
824 Report.Error (670, lexer.Location, "void type is not allowed for fields");
829 : variable_declarator
831 ArrayList decl = new ArrayList (4);
835 | variable_declarators COMMA variable_declarator
837 ArrayList decls = (ArrayList) $1;
844 : IDENTIFIER ASSIGN variable_initializer
846 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
850 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
863 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
865 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
871 iterator_container = (IIteratorContainer) $1;
875 Method method = (Method) $1;
876 Block b = (Block) $3;
877 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
880 if ((method.ModFlags & extern_abstract) == 0){
882 501, lexer.Location, current_container.MakeName (method.Name) +
883 "must declare a body because it is not marked abstract or extern");
886 if ((method.ModFlags & Modifiers.EXTERN) != 0){
888 179, lexer.Location, current_container.MakeName (method.Name) +
889 " is declared extern, but has a body");
893 method.Block = (Block) $3;
894 CheckDef (current_container.AddMethod (method), method.Name, method.Location);
896 current_local_parameters = null;
897 iterator_container = null;
911 1585, lexer.Location, "Member modifier `" +
912 Modifiers.Name (i) + "' must precede member type and name");
925 opt_type_parameter_list
926 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
928 lexer.ConstraintsParsing = true;
930 opt_type_parameter_constraints_clauses
932 lexer.ConstraintsParsing = false;
933 if ($10 != null && $5 == null)
934 Report.Error (-200, lexer.Location,
935 "Type parameter constraints only valid if there is a type parameter list");
938 GenericMethod generic = null;
940 generic = new GenericMethod (current_namespace, current_container,
941 (string) $4, (Attributes) $1,
944 CheckDef (generic.SetParameterInfo ((ArrayList) $5, (ArrayList) $10), generic.Name, generic.Location);
945 method = new Method (generic, (Expression) $3, (int) $2, (string) $4,
946 (Parameters) $7, (Attributes) $1, lexer.Location);
948 method = new Method (current_container, (Expression) $3, (int) $2, (string) $4,
949 (Parameters) $7, (Attributes) $1, lexer.Location);
951 current_local_parameters = (Parameters) $7;
959 opt_type_parameter_list
960 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
962 lexer.ConstraintsParsing = true;
964 opt_type_parameter_constraints_clauses
966 lexer.ConstraintsParsing = false;
967 if ($10 != null && $5 == null)
968 Report.Error (-200, lexer.Location,
969 "Type parameter constraints only valid if there is a type parameter list");
972 GenericMethod generic = null;
974 generic = new GenericMethod (current_namespace, current_container,
975 (string) $4, (Attributes) $1,
978 CheckDef (generic.SetParameterInfo ((ArrayList) $5, (ArrayList) $10), generic.Name, generic.Location);
980 method = new Method (generic, TypeManager.system_void_expr, (int) $2,
981 (string) $4, (Parameters) $7, (Attributes) $1,
984 method = new Method (current_container, TypeManager.system_void_expr, (int) $2,
985 (string) $4, (Parameters) $7, (Attributes) $1,
988 current_local_parameters = (Parameters) $7;
995 modifiers member_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
997 Report.Error (1585, lexer.Location,
998 String.Format ("Modifier {0} should appear before type",
999 Modifiers.Name ((int) $4)));
1000 Method method = new Method (current_container, TypeManager.system_void_expr, 0,
1001 (string) $5, (Parameters) $7, (Attributes) $1,
1004 current_local_parameters = (Parameters) $7;
1011 | SEMICOLON { $$ = null; }
1014 opt_formal_parameter_list
1015 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1016 | formal_parameter_list
1019 formal_parameter_list
1022 ArrayList pars_list = (ArrayList) $1;
1024 Parameter [] pars = new Parameter [pars_list.Count];
1025 pars_list.CopyTo (pars);
1027 $$ = new Parameters (pars, null, lexer.Location);
1029 | fixed_parameters COMMA parameter_array
1031 ArrayList pars_list = (ArrayList) $1;
1033 Parameter [] pars = new Parameter [pars_list.Count];
1034 pars_list.CopyTo (pars);
1036 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
1040 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
1047 ArrayList pars = new ArrayList (4);
1052 | fixed_parameters COMMA fixed_parameter
1054 ArrayList pars = (ArrayList) $1;
1063 opt_parameter_modifier
1067 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
1070 opt_parameter_modifier
1073 CheckIdentifierToken (yyToken);
1078 opt_parameter_modifier
1079 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1080 | parameter_modifier
1084 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1085 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1089 : opt_attributes PARAMS type IDENTIFIER
1091 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1092 note ("type must be a single-dimension array type");
1094 | opt_attributes PARAMS type error {
1095 CheckIdentifierToken (yyToken);
1101 : namespace_or_type_name { $$ = ($1).ToString (); }
1104 property_declaration
1110 implicit_value_parameter_type = (Expression) $3;
1112 lexer.PropertyParsing = true;
1114 $$ = lexer.Location;
1116 accessor_declarations
1118 lexer.PropertyParsing = false;
1123 Pair pair = (Pair) $7;
1124 Accessor get_block = (Accessor) pair.First;
1125 Accessor set_block = (Accessor) pair.Second;
1127 Location loc = (Location) $6;
1128 prop = new Property (current_container, (Expression) $3, (string) $4, (int) $2,
1129 get_block, set_block, (Attributes) $1, loc);
1131 CheckDef (current_container.AddProperty (prop), prop.Name, loc);
1132 implicit_value_parameter_type = null;
1136 accessor_declarations
1137 : get_accessor_declaration opt_set_accessor_declaration
1139 $$ = new Pair ($1, $2);
1141 | set_accessor_declaration opt_get_accessor_declaration
1143 $$ = new Pair ($2, $1);
1147 opt_get_accessor_declaration
1148 : /* empty */ { $$ = null; }
1149 | get_accessor_declaration
1152 opt_set_accessor_declaration
1153 : /* empty */ { $$ = null; }
1154 | set_accessor_declaration
1157 get_accessor_declaration
1158 : opt_attributes GET
1160 // If this is not the case, then current_local_parameters has already
1161 // been set in indexer_declaration
1162 if (parsing_indexer == false)
1163 current_local_parameters = null;
1165 current_local_parameters = indexer_parameters;
1166 lexer.PropertyParsing = false;
1170 $$ = new Accessor ((Block) $4, (Attributes) $1);
1171 current_local_parameters = null;
1172 lexer.PropertyParsing = true;
1176 set_accessor_declaration
1177 : opt_attributes SET
1180 Parameter implicit_value_parameter = new Parameter (
1181 implicit_value_parameter_type, "value",
1182 Parameter.Modifier.NONE, null);
1184 if (parsing_indexer == false) {
1185 args = new Parameter [1];
1186 args [0] = implicit_value_parameter;
1187 current_local_parameters = new Parameters (args, null, lexer.Location);
1189 Parameter [] fpars = indexer_parameters.FixedParameters;
1192 int count = fpars.Length;
1194 args = new Parameter [count + 1];
1195 fpars.CopyTo (args, 0);
1196 args [count] = implicit_value_parameter;
1199 current_local_parameters = new Parameters (
1200 args, indexer_parameters.ArrayParameter, lexer.Location);
1203 lexer.PropertyParsing = false;
1207 $$ = new Accessor ((Block) $4, (Attributes) $1);
1208 current_local_parameters = null;
1209 lexer.PropertyParsing = true;
1215 | SEMICOLON { $$ = null; }
1218 interface_declaration
1221 INTERFACE IDENTIFIER
1223 Interface new_interface;
1224 string full_interface_name = MakeName ((string) $4);
1226 new_interface = new Interface (current_namespace, current_container, full_interface_name,
1227 (int) $2, (Attributes) $1, lexer.Location);
1228 if (current_interface != null) {
1229 Location l = lexer.Location;
1230 Report.Error (-2, l, "Internal compiler error: interface inside interface");
1232 current_interface = new_interface;
1233 RootContext.Tree.RecordDecl (full_interface_name, new_interface);
1234 lexer.ConstraintsParsing = true;
1236 opt_type_parameter_list
1238 opt_type_parameter_constraints_clauses
1240 lexer.ConstraintsParsing = false;
1242 interface_body opt_semicolon
1244 Interface new_interface = (Interface) current_interface;
1246 if ($8 != null && $6 == null)
1247 Report.Error (-200, new_interface.Location,
1248 "Type parameter constraints only valid if there is a type parameter list");
1251 CheckDef (new_interface.SetParameterInfo ((ArrayList) $6, (ArrayList) $8), new_interface.Name, new_interface.Location);
1253 new_interface.Bases = (ArrayList) $7;
1255 current_interface = null;
1256 CheckDef (current_container.AddInterface (new_interface),
1257 new_interface.Name, new_interface.Location);
1259 | opt_attributes opt_modifiers INTERFACE error {
1260 CheckIdentifierToken (yyToken);
1265 : /* empty */ { $$ = null; }
1270 : COLON interface_type_list { $$ = $2; }
1276 ArrayList interfaces = new ArrayList (4);
1278 interfaces.Add ($1);
1281 | interface_type_list COMMA interface_type
1283 ArrayList interfaces = (ArrayList) $1;
1284 interfaces.Add ($3);
1291 opt_interface_member_declarations
1295 opt_interface_member_declarations
1297 | interface_member_declarations
1300 interface_member_declarations
1301 : interface_member_declaration
1302 | interface_member_declarations interface_member_declaration
1305 interface_member_declaration
1306 : interface_method_declaration
1308 InterfaceMethod m = (InterfaceMethod) $1;
1310 CheckDef (current_interface.AddMethod (m), m.Name, m.Location);
1312 | interface_property_declaration
1314 InterfaceProperty p = (InterfaceProperty) $1;
1316 CheckDef (current_interface.AddProperty (p), p.Name, p.Location);
1318 | interface_event_declaration
1321 InterfaceEvent e = (InterfaceEvent) $1;
1322 CheckDef (current_interface.AddEvent (e), e.Name, lexer.Location);
1325 | interface_indexer_declaration
1327 InterfaceIndexer i = (InterfaceIndexer) $1;
1329 CheckDef (current_interface.AddIndexer (i), "indexer", i.Location);
1338 val = Modifiers.Check (Modifiers.NEW, val, 0, lexer.Location);
1339 $$ = (bool) (val != 0);
1343 interface_method_declaration
1344 : opt_attributes opt_new type IDENTIFIER
1345 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1348 $$ = new InterfaceMethod ((Expression) $3, (string) $4, (bool) $2,
1349 (Parameters) $6, (Attributes) $1, lexer.Location);
1351 | opt_attributes opt_new VOID IDENTIFIER
1352 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1355 $$ = new InterfaceMethod (
1356 TypeManager.system_void_expr, (string) $4, (bool) $2, (Parameters) $6,
1357 (Attributes) $1, lexer.Location);
1361 interface_property_declaration
1366 { lexer.PropertyParsing = true; }
1368 { lexer.PropertyParsing = false; }
1373 $$ = new InterfaceProperty ((Expression) $3, (string) $4, (bool) $2,
1374 (gs & 1) == 1, (gs & 2) == 2, (Attributes) $1,
1380 CheckIdentifierToken (yyToken);
1386 : opt_attributes GET SEMICOLON { $$ = 1; }
1387 | opt_attributes SET SEMICOLON { $$ = 2; }
1388 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1390 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1394 interface_event_declaration
1395 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1397 $$ = new InterfaceEvent ((Expression) $4, (string) $5, (bool) $2, (Attributes) $1,
1400 | opt_attributes opt_new EVENT type error {
1401 CheckIdentifierToken (yyToken);
1404 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1405 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1408 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE event_accessor_declarations CLOSE_BRACE {
1409 Report.Error (69, lexer.Location, "Event accessors not valid on interfaces");
1414 interface_indexer_declaration
1415 : opt_attributes opt_new type THIS
1416 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1418 { lexer.PropertyParsing = true; }
1420 { lexer.PropertyParsing = false; }
1423 int a_flags = (int) $10;
1425 bool do_get = (a_flags & 1) == 1;
1426 bool do_set = (a_flags & 2) == 2;
1428 $$ = new InterfaceIndexer ((Expression) $3, (Parameters) $6, do_get, do_set,
1429 (bool) $2, (Attributes) $1, lexer.Location);
1433 operator_declaration
1434 : opt_attributes opt_modifiers operator_declarator operator_body
1436 OperatorDeclaration decl = (OperatorDeclaration) $3;
1438 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
1439 decl.arg2type, decl.arg2name, (Block) $4, (Attributes) $1, decl.location);
1441 // Note again, checking is done in semantic analysis
1442 current_container.AddOperator (op);
1444 current_local_parameters = null;
1450 | SEMICOLON { $$ = null; }
1453 : type OPERATOR overloadable_operator
1454 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1456 Operator.OpType op = (Operator.OpType) $3;
1457 CheckUnaryOperator (op);
1459 if (op == Operator.OpType.Addition)
1460 op = Operator.OpType.UnaryPlus;
1462 if (op == Operator.OpType.Subtraction)
1463 op = Operator.OpType.UnaryNegation;
1465 Parameter [] pars = new Parameter [1];
1467 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1469 current_local_parameters = new Parameters (pars, null, lexer.Location);
1471 $$ = new OperatorDeclaration (op, (Expression) $1, (Expression) $5, (string) $6,
1472 null, null, lexer.Location);
1474 | type OPERATOR overloadable_operator
1476 type IDENTIFIER COMMA
1480 CheckBinaryOperator ((Operator.OpType) $3);
1482 Parameter [] pars = new Parameter [2];
1484 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1485 pars [1] = new Parameter ((Expression) $8, (string) $9, Parameter.Modifier.NONE, null);
1487 current_local_parameters = new Parameters (pars, null, lexer.Location);
1489 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1490 (Expression) $5, (string) $6,
1491 (Expression) $8, (string) $9, lexer.Location);
1493 | conversion_operator_declarator
1496 overloadable_operator
1498 : BANG { $$ = Operator.OpType.LogicalNot; }
1499 | TILDE { $$ = Operator.OpType.OnesComplement; }
1500 | OP_INC { $$ = Operator.OpType.Increment; }
1501 | OP_DEC { $$ = Operator.OpType.Decrement; }
1502 | TRUE { $$ = Operator.OpType.True; }
1503 | FALSE { $$ = Operator.OpType.False; }
1504 // Unary and binary:
1505 | PLUS { $$ = Operator.OpType.Addition; }
1506 | MINUS { $$ = Operator.OpType.Subtraction; }
1508 | STAR { $$ = Operator.OpType.Multiply; }
1509 | DIV { $$ = Operator.OpType.Division; }
1510 | PERCENT { $$ = Operator.OpType.Modulus; }
1511 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1512 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1513 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1514 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1515 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1516 | OP_EQ { $$ = Operator.OpType.Equality; }
1517 | OP_NE { $$ = Operator.OpType.Inequality; }
1518 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1519 | OP_LT { $$ = Operator.OpType.LessThan; }
1520 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1521 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1524 conversion_operator_declarator
1525 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1527 Parameter [] pars = new Parameter [1];
1529 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1531 current_local_parameters = new Parameters (pars, null, lexer.Location);
1533 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1534 null, null, lexer.Location);
1536 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1538 Parameter [] pars = new Parameter [1];
1540 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1542 current_local_parameters = new Parameters (pars, null, lexer.Location);
1544 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1545 null, null, lexer.Location);
1549 syntax_error (lexer.Location, "'operator' expected");
1553 syntax_error (lexer.Location, "'operator' expected");
1557 constructor_declaration
1560 constructor_declarator
1563 Constructor c = (Constructor) $3;
1564 c.Block = (Block) $4;
1565 c.OptAttributes = (Attributes) $1;
1566 c.ModFlags = (int) $2;
1568 if (c.Name == current_container.Basename){
1569 if ((c.ModFlags & Modifiers.STATIC) != 0){
1570 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1572 515, c.Location, String.Format (
1573 "`{0}.{1}': static constructor can not have access modifiers",
1574 c.Name, current_container.Name));
1577 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1579 if (c.Initializer != null){
1582 "Static constructors can not have an explicit this or base " +
1583 "constructor invocations");
1586 if (!c.Parameters.Empty){
1588 132, c.Location, "Static constructors should not have parameters");
1591 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1594 // We let another layer check the validity of the constructor.
1595 Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1598 CheckDef (current_container.AddConstructor (c), c.Name, c.Location);
1600 current_local_parameters = null;
1604 constructor_declarator
1606 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1608 oob_stack.Push (lexer.Location);
1610 current_local_parameters = (Parameters) $3;
1612 opt_constructor_initializer
1614 Location l = (Location) oob_stack.Pop ();
1615 $$ = new Constructor (current_container, (string) $1, (Parameters) $3,
1616 (ConstructorInitializer) $6, l);
1622 | SEMICOLON { $$ = null; }
1625 opt_constructor_initializer
1626 : /* empty */ { $$ = null; }
1627 | constructor_initializer
1630 constructor_initializer
1631 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1633 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1635 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1637 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1640 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1646 : /* EMPTY */ { $$ = 0; }
1647 | UNSAFE { $$ = Modifiers.UNSAFE; }
1648 | EXTERN { $$ = Modifiers.EXTERN; }
1651 destructor_declaration
1652 : opt_attributes opt_finalizer TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1654 if ((string) $4 != current_container.Basename){
1655 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1656 } else if (!(current_container is Class)){
1657 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1659 Location l = lexer.Location;
1662 if (!RootContext.StdLib && current_container.Name == "System.Object")
1663 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
1665 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
1667 if ((m & Modifiers.UNSAFE) != 0){
1668 if (!RootContext.Unsafe){
1669 Report.Error (227, l,
1670 "Unsafe code requires the --unsafe command " +
1671 "line option to be specified");
1675 Method d = new Destructor (
1676 current_container, TypeManager.system_void_expr, m, "Finalize",
1677 new Parameters (null, null, l), (Attributes) $1, l);
1679 d.Block = (Block) $7;
1680 CheckDef (current_container.AddMethod (d), d.Name, d.Location);
1688 EVENT type variable_declarators SEMICOLON
1690 foreach (VariableDeclaration var in (ArrayList) $5) {
1692 Event e = new Event ((Expression) $4, var.identifier,
1693 var.expression_or_array_initializer,
1694 (int) $2, null, null, (Attributes) $1, lexer.Location);
1696 CheckDef (current_container.AddEvent (e), e.Name, e.Location);
1702 EVENT type member_name
1705 implicit_value_parameter_type = (Expression) $4;
1706 lexer.EventParsing = true;
1707 oob_stack.Push (lexer.Location);
1709 event_accessor_declarations
1711 lexer.EventParsing = false;
1715 Location loc = (Location) oob_stack.Pop ();
1718 Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
1721 Pair pair = (Pair) $8;
1722 Accessor add_accessor = null;
1723 Accessor rem_accessor = null;
1725 if (pair.First != null)
1726 add_accessor = (Accessor) pair.First;
1727 if (pair.Second != null)
1728 rem_accessor = (Accessor) pair.Second;
1730 Event e = new Event ((Expression) $4, (string) $5, null, (int) $2, add_accessor, rem_accessor,
1731 (Attributes) $1, loc);
1733 CheckDef (current_container.AddEvent (e), e.Name, loc);
1734 implicit_value_parameter_type = null;
1739 event_accessor_declarations
1740 : add_accessor_declaration remove_accessor_declaration
1742 $$ = new Pair ($1, $2);
1744 | remove_accessor_declaration add_accessor_declaration
1746 $$ = new Pair ($2, $1);
1748 | add_accessor_declaration { $$ = null; }
1749 | remove_accessor_declaration { $$ = null; }
1752 add_accessor_declaration
1753 : opt_attributes ADD
1755 Parameter [] args = new Parameter [1];
1756 Parameter implicit_value_parameter = new Parameter (
1757 implicit_value_parameter_type, "value",
1758 Parameter.Modifier.NONE, null);
1760 args [0] = implicit_value_parameter;
1762 current_local_parameters = new Parameters (args, null, lexer.Location);
1763 lexer.EventParsing = false;
1767 $$ = new Accessor ((Block) $4, (Attributes) $1);
1768 lexer.EventParsing = true;
1770 | opt_attributes ADD error {
1771 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1776 remove_accessor_declaration
1777 : opt_attributes REMOVE
1779 Parameter [] args = new Parameter [1];
1780 Parameter implicit_value_parameter = new Parameter (
1781 implicit_value_parameter_type, "value",
1782 Parameter.Modifier.NONE, null);
1784 args [0] = implicit_value_parameter;
1786 current_local_parameters = new Parameters (args, null, lexer.Location);
1787 lexer.EventParsing = false;
1791 $$ = new Accessor ((Block) $4, (Attributes) $1);
1792 lexer.EventParsing = true;
1794 | opt_attributes REMOVE error {
1795 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
1801 : opt_attributes opt_modifiers indexer_declarator
1804 IndexerDeclaration decl = (IndexerDeclaration) $3;
1806 implicit_value_parameter_type = decl.type;
1808 lexer.PropertyParsing = true;
1809 parsing_indexer = true;
1811 indexer_parameters = decl.param_list;
1812 oob_stack.Push (lexer.Location);
1814 accessor_declarations
1816 lexer.PropertyParsing = false;
1817 parsing_indexer = false;
1821 // The signature is computed from the signature of the indexer. Look
1822 // at section 3.6 on the spec
1823 Location loc = (Location) oob_stack.Pop ();
1825 IndexerDeclaration decl = (IndexerDeclaration) $3;
1826 Pair pair = (Pair) $6;
1827 Accessor get_block = (Accessor) pair.First;
1828 Accessor set_block = (Accessor) pair.Second;
1830 indexer = new Indexer (current_container, decl.type, decl.interface_type, (int) $2,
1831 decl.param_list, get_block, set_block, (Attributes) $1, loc);
1833 // Note that there is no equivalent of CheckDef for this case
1834 // We shall handle this in semantic analysis
1836 current_container.AddIndexer (indexer);
1838 current_local_parameters = null;
1839 implicit_value_parameter_type = null;
1840 indexer_parameters = null;
1845 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1847 Parameters pars = (Parameters) $4;
1849 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1850 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1853 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
1855 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
1857 Parameters pars = (Parameters) $6;
1859 if (pars.FixedParameters == null && pars.ArrayParameter == null){
1860 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
1862 $$ = new IndexerDeclaration ((Expression) $1, $2.ToString (), pars);
1874 Location enum_location = lexer.Location;
1876 string full_name = MakeName ((string) $4);
1877 Enum e = new Enum (current_namespace, current_container, (Expression) $5, (int) $2,
1878 full_name, (Attributes) $1, enum_location);
1880 foreach (VariableDeclaration ev in (ArrayList) $6) {
1881 Location loc = (Location) ev.Location;
1883 CheckDef (e.AddEnumMember (ev.identifier,
1884 (Expression) ev.expression_or_array_initializer,
1885 loc, ev.OptAttributes),
1886 ev.identifier, loc);
1889 CheckDef (current_container.AddEnum (e), full_name, enum_location);
1890 RootContext.Tree.RecordDecl (full_name, e);
1896 : /* empty */ { $$ = TypeManager.system_int32_expr; }
1897 | COLON type { $$ = $2; }
1901 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1907 opt_enum_member_declarations
1908 : /* empty */ { $$ = new ArrayList (4); }
1909 | enum_member_declarations opt_comma { $$ = $1; }
1912 enum_member_declarations
1913 : enum_member_declaration
1915 ArrayList l = new ArrayList (4);
1920 | enum_member_declarations COMMA enum_member_declaration
1922 ArrayList l = (ArrayList) $1;
1930 enum_member_declaration
1931 : opt_attributes IDENTIFIER
1933 $$ = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
1935 | opt_attributes IDENTIFIER
1937 $$ = lexer.Location;
1941 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
1945 delegate_declaration
1949 IDENTIFIER OPEN_PARENS
1950 opt_formal_parameter_list
1954 Location l = lexer.Location;
1955 Delegate del = new Delegate (current_namespace, current_container, (Expression) $4,
1956 (int) $2, MakeName ((string) $5), (Parameters) $7,
1957 (Attributes) $1, l);
1959 CheckDef (current_container.AddDelegate (del), del.Name, l);
1964 IDENTIFIER OPEN_PARENS
1965 opt_formal_parameter_list
1969 Location l = lexer.Location;
1970 Delegate del = new Delegate (
1971 current_namespace, current_container,
1972 TypeManager.system_void_expr, (int) $2, MakeName ((string) $5),
1973 (Parameters) $7, (Attributes) $1, l);
1975 CheckDef (current_container.AddDelegate (del), del.Name, l);
1980 : namespace_or_type_name
1983 namespace_or_type_name
1984 : IDENTIFIER opt_type_argument_list {
1986 $$ = new SimpleName ((string) $1, lexer.Location);
1988 $$ = new ConstructedType ((string) $1, (TypeArguments) $2, lexer.Location);
1990 | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list
1995 // Third argument will become an Expression, when we have sorted out
1996 // the issues with SimpleName first
2000 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2002 $$ = new GenericMemberAccess ((Expression) $1, (string) $3,
2003 (TypeArguments) $4, lexer.Location);
2005 | namespace_or_type_name DOT DEFAULT
2007 $$ = new DefaultValueExpression ((Expression) $1, lexer.Location);
2013 // Figure out what to do with the list
2015 opt_type_argument_list
2016 : /* empty */ { $$ = null; }
2017 | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2025 TypeArguments type_args = new TypeArguments ();
2026 type_args.Add ((Expression) $1);
2029 | type_arguments COMMA type {
2030 TypeArguments type_args = (TypeArguments) $1;
2031 type_args.Add ((Expression) $3);
2037 * Before you think of adding a return_type, notice that we have been
2038 * using two rules in the places where it matters (one rule using type
2039 * and another identical one that uses VOID as the return type). This
2040 * gets rid of a shift/reduce couple
2043 : type_name { /* class_type */
2055 // Note that here only unmanaged types are allowed but we
2056 // can't perform checks during this phase - we do it during
2057 // semantic analysis.
2059 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2063 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2069 | non_expression_type rank_specifier
2071 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2073 | non_expression_type STAR
2075 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2077 | expression rank_specifiers
2079 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2083 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2087 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2088 // through this different path
2090 | multiplicative_expression STAR
2092 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2099 ArrayList types = new ArrayList (4);
2104 | type_list COMMA type
2106 ArrayList types = (ArrayList) $1;
2114 * replaces all the productions for isolating the various
2115 * simple types, but we need this to reuse it easily in local_variable_type
2118 : OBJECT { $$ = TypeManager.system_object_expr; }
2119 | STRING { $$ = TypeManager.system_string_expr; }
2120 | BOOL { $$ = TypeManager.system_boolean_expr; }
2121 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2122 | FLOAT { $$ = TypeManager.system_single_expr; }
2123 | DOUBLE { $$ = TypeManager.system_double_expr; }
2128 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2129 | BYTE { $$ = TypeManager.system_byte_expr; }
2130 | SHORT { $$ = TypeManager.system_int16_expr; }
2131 | USHORT { $$ = TypeManager.system_uint16_expr; }
2132 | INT { $$ = TypeManager.system_int32_expr; }
2133 | UINT { $$ = TypeManager.system_uint32_expr; }
2134 | LONG { $$ = TypeManager.system_int64_expr; }
2135 | ULONG { $$ = TypeManager.system_uint64_expr; }
2136 | CHAR { $$ = TypeManager.system_char_expr; }
2137 | VOID { $$ = TypeManager.system_void_expr; }
2145 : type rank_specifiers
2147 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2152 // Expressions, section 7.5
2164 | parenthesized_expression
2165 | default_value_expression
2167 | invocation_expression
2171 | post_increment_expression
2172 | post_decrement_expression
2176 | checked_expression
2177 | unchecked_expression
2178 | pointer_member_access
2179 | anonymous_method_expression
2186 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2187 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2188 | NULL { $$ = NullLiteral.Null; }
2192 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2193 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2194 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2199 object v = lexer.Value;
2205 $$ = IntLiteral.Zero;
2207 $$ = IntLiteral.One;
2209 $$ = new IntLiteral (i);
2210 } else if (v is uint)
2211 $$ = new UIntLiteral ((UInt32) v);
2213 $$ = new LongLiteral ((Int64) v);
2214 else if (v is ulong)
2215 $$ = new ULongLiteral ((UInt64) v);
2217 Console.WriteLine ("OOPS. Unexpected result from scanner");
2222 : TRUE { $$ = new BoolLiteral (true); }
2223 | FALSE { $$ = new BoolLiteral (false); }
2226 parenthesized_expression_0
2227 : OPEN_PARENS expression CLOSE_PARENS
2230 lexer.Deambiguate_CloseParens ();
2231 // After this, the next token returned is one of
2232 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2233 // or CLOSE_PARENS_MINUS.
2237 parenthesized_expression
2238 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2242 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2244 // If a parenthesized expression is followed by a minus, we need to wrap
2245 // the expression inside a ParenthesizedExpression for the CS0075 check
2246 // in Binary.DoResolve().
2247 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2252 : primary_expression DOT IDENTIFIER
2254 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2256 | predefined_type DOT IDENTIFIER
2258 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2266 invocation_expression
2267 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2270 Location l = lexer.Location;
2271 Report.Error (1, l, "Parse error");
2273 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2275 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2277 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2279 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2281 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2286 : /* empty */ { $$ = null; }
2293 ArrayList list = new ArrayList (4);
2297 | argument_list COMMA argument
2299 ArrayList list = (ArrayList) $1;
2303 | argument_list error {
2304 CheckToken (1026, yyToken, ", or ) expected");
2311 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2313 | REF variable_reference
2315 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2317 | OUT variable_reference
2319 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2324 : expression { note ("section 5.4"); $$ = $1; }
2328 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2330 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2332 | primary_expression rank_specifiers
2334 // So the super-trick is that primary_expression
2335 // can only be either a SimpleName or a MemberAccess.
2336 // The MemberAccess case arises when you have a fully qualified type-name like :
2338 // SimpleName is when you have
2341 Expression expr = (Expression) $1;
2342 if (expr is ComposedCast){
2343 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2344 } else if (!(expr is SimpleName || expr is MemberAccess || expr is ConstructedType)){
2345 Error_ExpectingTypeName (lexer.Location, expr);
2346 $$ = TypeManager.system_object_expr;
2349 // So we extract the string corresponding to the SimpleName
2352 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2360 ArrayList list = new ArrayList (4);
2364 | expression_list COMMA expression
2366 ArrayList list = (ArrayList) $1;
2375 $$ = new This (current_block, lexer.Location);
2380 : BASE DOT IDENTIFIER
2382 $$ = new BaseAccess ((string) $3, lexer.Location);
2384 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2386 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2389 Report.Error (175, "Use of keyword `base' is not valid in this context");
2394 post_increment_expression
2395 : primary_expression OP_INC
2397 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2398 (Expression) $1, lexer.Location);
2402 post_decrement_expression
2403 : primary_expression OP_DEC
2405 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2406 (Expression) $1, lexer.Location);
2411 : object_or_delegate_creation_expression
2412 | array_creation_expression
2415 object_or_delegate_creation_expression
2416 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2418 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2422 array_creation_expression
2423 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2425 opt_array_initializer
2427 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2429 | NEW type rank_specifiers array_initializer
2431 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2435 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2451 : rank_specifier opt_rank_specifier
2453 $$ = (string) $2 + (string) $1;
2458 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2460 $$ = "[" + (string) $2 + "]";
2480 | dim_separators COMMA
2482 $$ = (string) $1 + ",";
2486 opt_array_initializer
2498 : OPEN_BRACE CLOSE_BRACE
2500 ArrayList list = new ArrayList (4);
2503 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2505 $$ = (ArrayList) $2;
2509 variable_initializer_list
2510 : variable_initializer
2512 ArrayList list = new ArrayList (4);
2516 | variable_initializer_list COMMA variable_initializer
2518 ArrayList list = (ArrayList) $1;
2525 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2527 $$ = new TypeOfVoid (lexer.Location);
2529 | TYPEOF OPEN_PARENS type CLOSE_PARENS
2531 $$ = new TypeOf ((Expression) $3, lexer.Location);
2536 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2537 $$ = new SizeOf ((Expression) $3, lexer.Location);
2542 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2544 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2548 unchecked_expression
2549 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2551 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2555 pointer_member_access
2556 : primary_expression OP_PTR IDENTIFIER
2560 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2561 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2565 anonymous_method_expression
2566 : DELEGATE opt_anonymous_method_signature {
2567 oob_stack.Push (current_local_parameters);
2568 current_local_parameters = (Parameters)$2;
2569 create_toplevel_block = true;
2571 create_toplevel_block = false;
2572 if (!RootContext.V2){
2573 Report.Error (-213, lexer.Location, "Anonymous methods are only supported in V2");
2576 $$ = new AnonymousMethod ((Parameters) $2, (Block) $4, lexer.Location);
2577 current_local_parameters = (Parameters) oob_stack.Pop ();
2581 opt_anonymous_method_signature
2582 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
2583 | anonymous_method_signature
2586 anonymous_method_signature
2587 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
2590 $$ = Parameters.EmptyReadOnlyParameters;
2592 ArrayList par_list = (ArrayList) $2;
2593 Parameter [] pars = new Parameter [par_list.Count];
2594 par_list.CopyTo (pars);
2595 $$ = new Parameters (pars, null, lexer.Location);
2600 opt_anonymous_method_parameter_list
2601 : /* empty */ { $$ = null; }
2602 | anonymous_method_parameter_list { $$ = $1; }
2605 anonymous_method_parameter_list
2606 : anonymous_method_parameter
2608 ArrayList a = new ArrayList (4);
2612 | anonymous_method_parameter_list COMMA anonymous_method_parameter
2614 ArrayList a = (ArrayList) $1;
2620 anonymous_method_parameter
2621 : opt_parameter_modifier type IDENTIFIER {
2622 $$ = new Parameter ((Expression) $2, (string) $2, (Parameter.Modifier) $1, null);
2626 default_value_expression
2627 : primary_expression DOT DEFAULT
2629 $$ = new DefaultValueExpression ((Expression) $1, lexer.Location);
2631 | predefined_type DOT DEFAULT
2633 $$ = new DefaultValueExpression ((Expression) $1, lexer.Location);
2638 : primary_expression
2639 | BANG prefixed_unary_expression
2641 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2643 | TILDE prefixed_unary_expression
2645 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2651 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
2653 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2655 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
2657 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2663 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2665 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2670 // The idea to split this out is from Rhys' grammar
2671 // to solve the problem with casts.
2673 prefixed_unary_expression
2675 | PLUS prefixed_unary_expression
2677 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2679 | MINUS prefixed_unary_expression
2681 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2683 | OP_INC prefixed_unary_expression
2685 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2686 (Expression) $2, lexer.Location);
2688 | OP_DEC prefixed_unary_expression
2690 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2691 (Expression) $2, lexer.Location);
2693 | STAR prefixed_unary_expression
2695 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2697 | BITWISE_AND prefixed_unary_expression
2699 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2703 pre_increment_expression
2704 : OP_INC prefixed_unary_expression
2706 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2707 (Expression) $2, lexer.Location);
2711 pre_decrement_expression
2712 : OP_DEC prefixed_unary_expression
2714 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2715 (Expression) $2, lexer.Location);
2719 multiplicative_expression
2720 : prefixed_unary_expression
2721 | multiplicative_expression STAR prefixed_unary_expression
2723 $$ = new Binary (Binary.Operator.Multiply,
2724 (Expression) $1, (Expression) $3, lexer.Location);
2726 | multiplicative_expression DIV prefixed_unary_expression
2728 $$ = new Binary (Binary.Operator.Division,
2729 (Expression) $1, (Expression) $3, lexer.Location);
2731 | multiplicative_expression PERCENT prefixed_unary_expression
2733 $$ = new Binary (Binary.Operator.Modulus,
2734 (Expression) $1, (Expression) $3, lexer.Location);
2739 : multiplicative_expression
2740 | additive_expression PLUS multiplicative_expression
2742 $$ = new Binary (Binary.Operator.Addition,
2743 (Expression) $1, (Expression) $3, lexer.Location);
2745 | additive_expression MINUS multiplicative_expression
2747 $$ = new Binary (Binary.Operator.Subtraction,
2748 (Expression) $1, (Expression) $3, lexer.Location);
2753 : additive_expression
2754 | shift_expression OP_SHIFT_LEFT additive_expression
2756 $$ = new Binary (Binary.Operator.LeftShift,
2757 (Expression) $1, (Expression) $3, lexer.Location);
2759 | shift_expression OP_SHIFT_RIGHT additive_expression
2761 $$ = new Binary (Binary.Operator.RightShift,
2762 (Expression) $1, (Expression) $3, lexer.Location);
2766 relational_expression
2768 | relational_expression OP_LT shift_expression
2770 $$ = new Binary (Binary.Operator.LessThan,
2771 (Expression) $1, (Expression) $3, lexer.Location);
2773 | relational_expression OP_GT shift_expression
2775 $$ = new Binary (Binary.Operator.GreaterThan,
2776 (Expression) $1, (Expression) $3, lexer.Location);
2778 | relational_expression OP_LE shift_expression
2780 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2781 (Expression) $1, (Expression) $3, lexer.Location);
2783 | relational_expression OP_GE shift_expression
2785 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2786 (Expression) $1, (Expression) $3, lexer.Location);
2788 | relational_expression IS type
2790 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
2792 | relational_expression AS type
2794 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
2799 : relational_expression
2800 | equality_expression OP_EQ relational_expression
2802 $$ = new Binary (Binary.Operator.Equality,
2803 (Expression) $1, (Expression) $3, lexer.Location);
2805 | equality_expression OP_NE relational_expression
2807 $$ = new Binary (Binary.Operator.Inequality,
2808 (Expression) $1, (Expression) $3, lexer.Location);
2813 : equality_expression
2814 | and_expression BITWISE_AND equality_expression
2816 $$ = new Binary (Binary.Operator.BitwiseAnd,
2817 (Expression) $1, (Expression) $3, lexer.Location);
2821 exclusive_or_expression
2823 | exclusive_or_expression CARRET and_expression
2825 $$ = new Binary (Binary.Operator.ExclusiveOr,
2826 (Expression) $1, (Expression) $3, lexer.Location);
2830 inclusive_or_expression
2831 : exclusive_or_expression
2832 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2834 $$ = new Binary (Binary.Operator.BitwiseOr,
2835 (Expression) $1, (Expression) $3, lexer.Location);
2839 conditional_and_expression
2840 : inclusive_or_expression
2841 | conditional_and_expression OP_AND inclusive_or_expression
2843 $$ = new Binary (Binary.Operator.LogicalAnd,
2844 (Expression) $1, (Expression) $3, lexer.Location);
2848 conditional_or_expression
2849 : conditional_and_expression
2850 | conditional_or_expression OP_OR conditional_and_expression
2852 $$ = new Binary (Binary.Operator.LogicalOr,
2853 (Expression) $1, (Expression) $3, lexer.Location);
2857 conditional_expression
2858 : conditional_or_expression
2859 | conditional_or_expression INTERR expression COLON expression
2861 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2865 assignment_expression
2866 : prefixed_unary_expression ASSIGN expression
2868 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2870 | prefixed_unary_expression OP_MULT_ASSIGN expression
2872 Location l = lexer.Location;
2874 $$ = new CompoundAssign (
2875 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
2877 | prefixed_unary_expression OP_DIV_ASSIGN expression
2879 Location l = lexer.Location;
2881 $$ = new CompoundAssign (
2882 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
2884 | prefixed_unary_expression OP_MOD_ASSIGN expression
2886 Location l = lexer.Location;
2888 $$ = new CompoundAssign (
2889 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
2891 | prefixed_unary_expression OP_ADD_ASSIGN expression
2893 Location l = lexer.Location;
2895 $$ = new CompoundAssign (
2896 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
2898 | prefixed_unary_expression OP_SUB_ASSIGN expression
2900 Location l = lexer.Location;
2902 $$ = new CompoundAssign (
2903 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
2905 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2907 Location l = lexer.Location;
2909 $$ = new CompoundAssign (
2910 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
2912 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2914 Location l = lexer.Location;
2916 $$ = new CompoundAssign (
2917 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
2919 | prefixed_unary_expression OP_AND_ASSIGN expression
2921 Location l = lexer.Location;
2923 $$ = new CompoundAssign (
2924 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
2926 | prefixed_unary_expression OP_OR_ASSIGN expression
2928 Location l = lexer.Location;
2930 $$ = new CompoundAssign (
2931 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
2933 | prefixed_unary_expression OP_XOR_ASSIGN expression
2935 Location l = lexer.Location;
2937 $$ = new CompoundAssign (
2938 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
2943 : conditional_expression
2944 | assignment_expression
2966 name = MakeName ((string) $4);
2968 new_class = new Class (current_namespace, current_container, name, (int) $2,
2969 (Attributes) $1, lexer.Location);
2970 current_container = new_class;
2971 RootContext.Tree.RecordDecl (name, new_class);
2973 lexer.ConstraintsParsing = true;
2975 opt_type_parameter_list
2977 opt_type_parameter_constraints_clauses
2979 lexer.ConstraintsParsing = false;
2984 Class new_class = (Class) current_container;
2986 if ($8 != null && $6 == null)
2987 Report.Error (-200, new_class.Location,
2988 "Type parameter constraints only valid if there is a type parameter list");
2991 CheckDef (new_class.SetParameterInfo ((ArrayList) $6, (ArrayList) $8), new_class.Name, new_class.Location);
2993 if (new_class.Name == "System.Object") {
2994 Report.Error (537, new_class.Location, "The class System.Object cannot have a base class or implement an interface.");
2997 new_class.Bases = (ArrayList) $7;
2999 current_container = current_container.Parent;
3000 CheckDef (current_container.AddClass (new_class), new_class.Name, new_class.Location);
3008 lexer.ConstraintsParsing = true;
3011 Report.Error (-200, lexer.Location,
3012 "Type parameter constraints only valid if there is a type parameter list");
3019 : /* empty */ { $$ = (int) 0; }
3025 | modifiers modifier
3030 if ((m1 & m2) != 0) {
3031 Location l = lexer.Location;
3032 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3034 $$ = (int) (m1 | m2);
3039 : NEW { $$ = Modifiers.NEW; }
3040 | PUBLIC { $$ = Modifiers.PUBLIC; }
3041 | PROTECTED { $$ = Modifiers.PROTECTED; }
3042 | INTERNAL { $$ = Modifiers.INTERNAL; }
3043 | PRIVATE { $$ = Modifiers.PRIVATE; }
3044 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3045 | SEALED { $$ = Modifiers.SEALED; }
3046 | STATIC { $$ = Modifiers.STATIC; }
3047 | READONLY { $$ = Modifiers.READONLY; }
3048 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3049 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3050 | EXTERN { $$ = Modifiers.EXTERN; }
3051 | VOLATILE { $$ = Modifiers.VOLATILE; }
3052 | UNSAFE { $$ = Modifiers.UNSAFE; }
3056 : /* empty */ { $$ = null; }
3057 | class_base { $$ = $1; }
3061 : COLON type_list { $$ = $2; }
3064 opt_type_parameter_list
3065 : /* empty */ { $$ = null; }
3066 | type_parameter_list { $$ = $1; }
3070 : OP_GENERICS_LT type_parameters OP_GENERICS_GT { $$ = $2; }
3076 // Do some profiling to find the optimal size, for now we
3077 // assume most people will be generic on one type (saves space
3079 ArrayList type_parameters = new ArrayList (1);
3080 type_parameters.Add ($1);
3081 $$ = type_parameters;
3083 | type_parameters COMMA type_parameter {
3084 ArrayList type_parameters = (ArrayList) $1;
3086 type_parameters.Add ($3);
3087 $$ = type_parameters;
3095 opt_type_parameter_constraints_clauses
3096 : /* empty */ { $$ = null; }
3097 | type_parameter_constraints_clauses
3101 type_parameter_constraints_clauses
3102 : type_parameter_constraints_clause
3103 | type_parameter_constraints_clauses type_parameter_constraints_clause {
3104 ArrayList constraints = (ArrayList) $1;
3106 constraints.Add ($2);
3111 type_parameter_constraints_clause
3112 : WHERE type_parameter COLON type_parameter_constraints {
3113 ArrayList constraints = new ArrayList (1);
3114 constraints.Add (new Constraints ((string) $2, (ArrayList) $4, lexer.Location));
3119 type_parameter_constraints
3120 : type_parameter_constraint {
3121 ArrayList constraints = new ArrayList (1);
3122 constraints.Add ($1);
3125 | type_parameter_constraints COMMA type_parameter_constraint {
3126 ArrayList constraints = (ArrayList) $1;
3128 constraints.Add ($3);
3133 type_parameter_constraint
3135 | NEW OPEN_PARENS CLOSE_PARENS {
3145 // A block is "contained" on the following places:
3147 // property_declaration as part of the accessor body (get/set)
3148 // operator_declaration
3149 // constructor_declaration
3150 // destructor_declaration
3151 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3156 if (current_block == null || create_toplevel_block){
3157 current_block = new ToplevelBlock (current_local_parameters, lexer.Location);
3159 current_block = new Block (current_block, current_local_parameters,
3160 lexer.Location, Location.Null);
3163 opt_statement_list CLOSE_BRACE
3165 while (current_block.Implicit)
3166 current_block = current_block.Parent;
3168 current_block.SetEndLocation (lexer.Location);
3169 current_block = current_block.Parent;
3180 | statement_list statement
3184 : declaration_statement
3186 if ($1 != null && (Block) $1 != current_block){
3187 current_block.AddStatement ((Statement) $1);
3188 current_block = (Block) $1;
3191 | embedded_statement
3193 current_block.AddStatement ((Statement) $1);
3201 | expression_statement
3202 | selection_statement
3203 | iteration_statement
3207 | unchecked_statement
3217 $$ = EmptyStatement.Value;
3224 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3226 if (!current_block.AddLabel ((string) $1, labeled)){
3227 Location l = lexer.Location;
3228 Report.Error (140, l, "The label '" + ((string) $1) + "' is a duplicate");
3230 current_block.AddStatement (labeled);
3235 declaration_statement
3236 : local_variable_declaration SEMICOLON
3239 DictionaryEntry de = (DictionaryEntry) $1;
3241 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3245 | local_constant_declaration SEMICOLON
3248 DictionaryEntry de = (DictionaryEntry) $1;
3250 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3256 * The following is from Rhys' grammar:
3257 * > Types in local variable declarations must be recognized as
3258 * > expressions to prevent reduce/reduce errors in the grammar.
3259 * > The expressions are converted into types during semantic analysis.
3262 : primary_expression opt_rank_specifier
3264 // FIXME: Do something smart here regarding the composition of the type.
3266 // Ok, the above "primary_expression" is there to get rid of
3267 // both reduce/reduce and shift/reduces in the grammar, it should
3268 // really just be "type_name". If you use type_name, a reduce/reduce
3269 // creeps up. If you use namespace_or_type_name (which is all we need
3270 // really) two shift/reduces appear.
3273 // So the super-trick is that primary_expression
3274 // can only be either a SimpleName or a MemberAccess.
3275 // The MemberAccess case arises when you have a fully qualified type-name like :
3277 // SimpleName is when you have
3280 Expression expr = (Expression) $1;
3281 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType)) {
3282 Error_ExpectingTypeName (lexer.Location, expr);
3286 // So we extract the string corresponding to the SimpleName
3290 if ((string) $2 == "")
3293 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3296 | builtin_types opt_rank_specifier
3298 if ((string) $2 == "")
3301 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3305 local_variable_pointer_type
3306 : primary_expression STAR
3308 Expression expr = (Expression) $1;
3309 Location l = lexer.Location;
3311 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType)) {
3312 Error_ExpectingTypeName (l, expr);
3316 $$ = new ComposedCast ((Expression) $1, "*", l);
3318 | builtin_types STAR
3320 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3324 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3326 | local_variable_pointer_type STAR
3328 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3332 local_variable_declaration
3333 : local_variable_type variable_declarators
3336 $$ = new DictionaryEntry ($1, $2);
3340 | local_variable_pointer_type opt_rank_specifier variable_declarators
3345 if ((string) $2 == "")
3346 t = (Expression) $1;
3348 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3349 $$ = new DictionaryEntry (t, $3);
3355 local_constant_declaration
3356 : CONST local_variable_type constant_declarators
3359 $$ = new DictionaryEntry ($2, $3);
3365 expression_statement
3366 : statement_expression SEMICOLON
3373 // We have to do the wrapping here and not in the case above,
3374 // because statement_expression is used for example in for_statement
3376 statement_expression
3377 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3378 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3379 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3380 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3381 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3382 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3383 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3385 Report.Error (1002, lexer.Location, "Expecting `;'");
3390 object_creation_expression
3391 : object_or_delegate_creation_expression
3392 { note ("complain if this is a delegate maybe?"); }
3401 : if_statement_open if_statement_rest
3410 oob_stack.Push (lexer.Location);
3415 : boolean_expression CLOSE_PARENS
3418 Location l = (Location) oob_stack.Pop ();
3420 $$ = new If ((Expression) $1, (Statement) $3, l);
3422 if (RootContext.WarningLevel >= 3){
3423 if ($3 == EmptyStatement.Value)
3424 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3428 | boolean_expression CLOSE_PARENS
3429 embedded_statement ELSE embedded_statement
3431 Location l = (Location) oob_stack.Pop ();
3433 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
3438 : SWITCH OPEN_PARENS
3440 oob_stack.Push (lexer.Location);
3441 switch_stack.Push (current_block);
3443 expression CLOSE_PARENS
3446 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
3447 current_block = (Block) switch_stack.Pop ();
3463 Report.Error (1522, lexer.Location, "Empty switch block");
3471 ArrayList sections = new ArrayList (4);
3476 | switch_sections switch_section
3478 ArrayList sections = (ArrayList) $1;
3488 current_block = current_block.CreateSwitchBlock (lexer.Location);
3492 Block topmost = current_block;
3494 while (topmost.Implicit)
3495 topmost = topmost.Parent;
3496 $$ = new SwitchSection ((ArrayList) $1, topmost);
3503 ArrayList labels = new ArrayList (4);
3508 | switch_labels switch_label
3510 ArrayList labels = (ArrayList) ($1);
3518 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3519 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3522 1523, lexer.Location,
3523 "The keyword case or default must precede code in switch block");
3537 oob_stack.Push (lexer.Location);
3539 boolean_expression CLOSE_PARENS embedded_statement
3541 Location l = (Location) oob_stack.Pop ();
3542 $$ = new While ((Expression) $4, (Statement) $6, l);
3544 if (RootContext.WarningLevel >= 3){
3545 if ($6 == EmptyStatement.Value)
3546 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3552 : DO embedded_statement
3554 oob_stack.Push (lexer.Location);
3556 boolean_expression CLOSE_PARENS SEMICOLON
3558 Location l = (Location) oob_stack.Pop ();
3560 $$ = new Do ((Statement) $2, (Expression) $6, l);
3566 opt_for_initializer SEMICOLON
3568 Block assign_block = new Block (current_block);
3569 current_block = assign_block;
3571 if ($3 is DictionaryEntry){
3572 DictionaryEntry de = (DictionaryEntry) $3;
3574 Expression type = (Expression) de.Key;
3575 ArrayList var_declarators = (ArrayList) de.Value;
3577 foreach (VariableDeclaration decl in var_declarators){
3581 vi = current_block.AddVariable (
3582 type, decl.identifier, current_local_parameters, decl.Location);
3586 Location l = lexer.Location;
3588 if (decl.expression_or_array_initializer is Expression){
3589 expr = (Expression) decl.expression_or_array_initializer;
3590 } else if (decl.expression_or_array_initializer == null) {
3593 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3594 expr = new ArrayCreation (type, "", init, decl.Location);
3597 LocalVariableReference var;
3598 var = new LocalVariableReference (assign_block, decl.identifier, l);
3601 Assign a = new Assign (var, expr, decl.Location);
3603 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3609 oob_stack.Push (lexer.Location);
3611 opt_for_condition SEMICOLON
3612 opt_for_iterator CLOSE_PARENS
3615 Location l = (Location) oob_stack.Pop ();
3617 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3619 if (RootContext.WarningLevel >= 3){
3620 if ($10 == EmptyStatement.Value)
3621 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3624 current_block.AddStatement (f);
3625 while (current_block.Implicit)
3626 current_block = current_block.Parent;
3628 current_block = current_block.Parent;
3633 : /* empty */ { $$ = EmptyStatement.Value; }
3638 : local_variable_declaration
3639 | statement_expression_list
3643 : /* empty */ { $$ = null; }
3644 | boolean_expression
3648 : /* empty */ { $$ = EmptyStatement.Value; }
3653 : statement_expression_list
3656 statement_expression_list
3657 : statement_expression
3659 // CHANGE: was `null'
3660 Block b = new Block (current_block, Block.Flags.Implicit);
3662 b.AddStatement ((Statement) $1);
3665 | statement_expression_list COMMA statement_expression
3667 Block b = (Block) $1;
3669 b.AddStatement ((Statement) $3);
3675 : FOREACH OPEN_PARENS type IDENTIFIER IN
3677 oob_stack.Push (lexer.Location);
3679 expression CLOSE_PARENS
3681 oob_stack.Push (current_block);
3683 Block foreach_block = new Block (current_block, Block.Flags.Implicit);
3684 LocalVariableReference v = null;
3685 Location l = lexer.Location;
3688 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3692 // Get a writable reference to this read-only variable.
3693 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3695 current_block = foreach_block;
3698 oob_stack.Push (current_block);
3702 Block foreach_block = (Block) oob_stack.Pop ();
3703 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3704 Block prev_block = (Block) oob_stack.Pop ();
3705 Location l = (Location) oob_stack.Pop ();
3707 current_block = prev_block;
3710 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3711 foreach_block.AddStatement (f);
3720 | continue_statement
3730 $$ = new Break (lexer.Location);
3735 : CONTINUE SEMICOLON
3737 $$ = new Continue (lexer.Location);
3742 : GOTO IDENTIFIER SEMICOLON
3744 $$ = new Goto (current_block, (string) $2, lexer.Location);
3746 | GOTO CASE constant_expression SEMICOLON
3748 $$ = new GotoCase ((Expression) $3, lexer.Location);
3750 | GOTO DEFAULT SEMICOLON
3752 $$ = new GotoDefault (lexer.Location);
3757 : RETURN opt_expression SEMICOLON
3759 $$ = new Return ((Expression) $2, lexer.Location);
3764 : THROW opt_expression SEMICOLON
3766 $$ = new Throw ((Expression) $2, lexer.Location);
3771 : YIELD RETURN expression SEMICOLON
3773 if (iterator_container == null){
3774 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3777 iterator_container.SetYields ();
3778 $$ = new Yield ((Expression) $3, lexer.Location);
3781 | YIELD expression SEMICOLON
3783 if (iterator_container == null){
3784 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3787 iterator_container.SetYields ();
3788 $$ = new Yield ((Expression) $2, lexer.Location);
3791 | YIELD BREAK SEMICOLON
3793 if (iterator_container == null){
3794 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3797 iterator_container.SetYields ();
3798 $$ = new YieldBreak (lexer.Location);
3809 : TRY block catch_clauses
3812 ArrayList s = new ArrayList (4);
3814 foreach (Catch cc in (ArrayList) $3) {
3821 // Now s contains the list of specific catch clauses
3822 // and g contains the general one.
3824 $$ = new Try ((Block) $2, s, g, null, lexer.Location);
3826 | TRY block opt_catch_clauses FINALLY block
3829 ArrayList s = new ArrayList (4);
3830 ArrayList catch_list = (ArrayList) $3;
3832 if (catch_list != null){
3833 foreach (Catch cc in catch_list) {
3841 $$ = new Try ((Block) $2, s, g, (Block) $5, lexer.Location);
3845 Report.Error (1524, lexer.Location, "Expected catch or finally");
3850 : /* empty */ { $$ = null; }
3857 ArrayList l = new ArrayList (4);
3862 | catch_clauses catch_clause
3864 ArrayList l = (ArrayList) $1;
3872 : /* empty */ { $$ = null; }
3877 : CATCH opt_catch_args
3879 Expression type = null;
3883 DictionaryEntry cc = (DictionaryEntry) $2;
3884 type = (Expression) cc.Key;
3885 id = (string) cc.Value;
3888 ArrayList one = new ArrayList (4);
3889 Location loc = lexer.Location;
3891 one.Add (new VariableDeclaration (id, null, loc));
3894 current_block = new Block (current_block);
3895 Block b = declare_local_variables (type, one, loc);
3900 Expression type = null;
3904 DictionaryEntry cc = (DictionaryEntry) $2;
3905 type = (Expression) cc.Key;
3906 id = (string) cc.Value;
3910 // FIXME: I can change this for an assignment.
3912 while (current_block != (Block) $1)
3913 current_block = current_block.Parent;
3918 $$ = new Catch (type, id , (Block) $4, lexer.Location);
3923 : /* empty */ { $$ = null; }
3928 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3930 $$ = new DictionaryEntry ($2, $3);
3937 $$ = new Checked ((Block) $2);
3944 $$ = new Unchecked ((Block) $2);
3951 if (!RootContext.Unsafe){
3952 Report.Error (227, lexer.Location,
3953 "Unsafe code can only be used if --unsafe is used");
3956 $$ = new Unsafe ((Block) $3);
3962 type fixed_pointer_declarators
3965 Block assign_block = new Block (current_block, Block.Flags.Implicit);
3966 ArrayList list = (ArrayList) $4;
3967 Expression type = (Expression) $3;
3968 Location l = lexer.Location;
3969 int top = list.Count;
3971 for (int i = 0; i < top; i++){
3972 Pair p = (Pair) list [i];
3975 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3982 current_block.AddStatement (assign_block);
3983 current_block = assign_block;
3984 oob_stack.Push (assign_block);
3989 Location l = (Location) oob_stack.Pop ();
3992 $$ = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
3996 fixed_pointer_declarators
3997 : fixed_pointer_declarator {
3998 ArrayList declarators = new ArrayList (4);
3999 declarators.Add ($1);
4002 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4004 ArrayList declarators = (ArrayList) $1;
4005 declarators.Add ($3);
4010 fixed_pointer_declarator
4011 : IDENTIFIER ASSIGN expression
4013 $$ = new Pair ($1, $3);
4018 : LOCK OPEN_PARENS expression CLOSE_PARENS
4024 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4029 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4031 Block assign_block = new Block (current_block);
4032 current_block = assign_block;
4034 oob_stack.Push (lexer.Location);
4036 if ($3 is DictionaryEntry){
4037 DictionaryEntry de = (DictionaryEntry) $3;
4038 Location l = lexer.Location;
4040 Expression type = (Expression) de.Key;
4041 ArrayList var_declarators = (ArrayList) de.Value;
4043 ArrayList vars = new ArrayList (4);
4045 foreach (VariableDeclaration decl in var_declarators){
4047 LocalInfo vi = current_block.AddVariable (
4048 type, decl.identifier,
4049 current_local_parameters, decl.Location);
4055 if (decl.expression_or_array_initializer is Expression){
4056 expr = (Expression) decl.expression_or_array_initializer;
4058 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4060 expr = new ArrayCreation (type, "", init, decl.Location);
4063 LocalVariableReference var;
4065 // Get a writable reference to this read-only variable.
4066 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4068 // This is so that it is not a warning on using variables
4071 vars.Add (new DictionaryEntry (var, expr));
4073 // Assign a = new Assign (var, expr, decl.Location);
4074 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4076 $3 = new DictionaryEntry (type, vars);
4081 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
4082 current_block.AddStatement (u);
4083 while (current_block.Implicit)
4084 current_block = current_block.Parent;
4086 current_block = current_block.Parent;
4090 resource_acquisition
4091 : local_variable_declaration
4098 // A class used to pass around variable declarations and constants
4100 public class VariableDeclaration {
4101 public string identifier;
4102 public object expression_or_array_initializer;
4103 public Location Location;
4104 public Attributes OptAttributes;
4106 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4108 this.identifier = id;
4109 this.expression_or_array_initializer = eoai;
4111 this.OptAttributes = opt_attrs;
4114 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4120 // A class used to hold info about an indexer declarator
4122 public class IndexerDeclaration {
4123 public Expression type;
4124 public string interface_type;
4125 public Parameters param_list;
4127 public IndexerDeclaration (Expression type, string interface_type, Parameters param_list)
4130 this.interface_type = interface_type;
4131 this.param_list = param_list;
4136 // A class used to hold info about an operator declarator
4138 public class OperatorDeclaration {
4139 public Operator.OpType optype;
4140 public Expression ret_type, arg1type, arg2type;
4141 public string arg1name, arg2name;
4142 public Location location;
4144 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4145 Expression arg1type, string arg1name,
4146 Expression arg2type, string arg2name, Location location)
4149 this.ret_type = ret_type;
4150 this.arg1type = arg1type;
4151 this.arg1name = arg1name;
4152 this.arg2type = arg2type;
4153 this.arg2name = arg2name;
4154 this.location = location;
4159 void Error_ExpectingTypeName (Location l, Expression expr)
4161 if (expr is Invocation){
4162 Report.Error (1002, l, "; expected");
4164 Report.Error (-1, l, "Invalid Type definition");
4169 // Given the @class_name name, it creates a fully qualified name
4170 // based on the containing declaration space
4173 MakeName (string class_name)
4175 string ns = current_namespace.FullName;
4176 string container_name = current_container.Name;
4178 if (container_name == ""){
4180 return ns + "." + class_name;
4184 return container_name + "." + class_name;
4188 // Used to report back to the user the result of a declaration
4189 // in the current declaration space
4192 CheckDef (DeclSpace.AdditionResult result, string name, Location l)
4194 if (result == DeclSpace.AdditionResult.Success)
4198 case DeclSpace.AdditionResult.NameExists:
4199 Report.Error (102, l, "The container `" + current_container.Name +
4200 "' already contains a definition for `"+
4206 // This is handled only for static Constructors, because
4207 // in reality we handle these by the semantic analysis later
4209 case DeclSpace.AdditionResult.MethodExists:
4211 111, l, "Class `"+current_container.Name+
4212 "' already defines a member called '" +
4213 name + "' with the same parameter types (more than one default constructor)");
4216 case DeclSpace.AdditionResult.EnclosingClash:
4217 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
4220 case DeclSpace.AdditionResult.NotAConstructor:
4221 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
4224 case DeclSpace.AdditionResult.Error:
4225 // Error has already been reported.
4231 CheckDef (bool result, string name, Location l)
4235 CheckDef (DeclSpace.AdditionResult.NameExists, name, l);
4238 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4240 Block implicit_block;
4241 ArrayList inits = null;
4244 // We use the `Used' property to check whether statements
4245 // have been added to the current block. If so, we need
4246 // to create another block to contain the new declaration
4247 // otherwise, as an optimization, we use the same block to
4248 // add the declaration.
4250 // FIXME: A further optimization is to check if the statements
4251 // that were added were added as part of the initialization
4252 // below. In which case, no other statements have been executed
4253 // and we might be able to reduce the number of blocks for
4254 // situations like this:
4256 // int j = 1; int k = j + 1;
4258 if (current_block.Used) {
4259 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4260 implicit_block.AddChildVariableNames (current_block);
4262 implicit_block = current_block;
4264 foreach (VariableDeclaration decl in variable_declarators){
4266 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4267 if (decl.expression_or_array_initializer != null){
4269 inits = new ArrayList (4);
4276 return implicit_block;
4278 foreach (VariableDeclaration decl in inits){
4282 if (decl.expression_or_array_initializer is Expression){
4283 expr = (Expression) decl.expression_or_array_initializer;
4286 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4288 expr = new ArrayCreation (type, "", init, decl.Location);
4291 LocalVariableReference var;
4292 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4294 assign = new Assign (var, expr, decl.Location);
4296 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4299 return implicit_block;
4302 Block declare_local_constants (Expression type, ArrayList declarators)
4304 Block implicit_block;
4306 if (current_block.Used)
4307 implicit_block = new Block (current_block, Block.Flags.Implicit);
4309 implicit_block = current_block;
4311 foreach (VariableDeclaration decl in declarators){
4312 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4313 current_local_parameters, decl.Location);
4316 return implicit_block;
4319 void CheckAttributeTarget (string a)
4323 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
4327 Location l = lexer.Location;
4328 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4334 void CheckUnaryOperator (Operator.OpType op)
4338 case Operator.OpType.LogicalNot:
4339 case Operator.OpType.OnesComplement:
4340 case Operator.OpType.Increment:
4341 case Operator.OpType.Decrement:
4342 case Operator.OpType.True:
4343 case Operator.OpType.False:
4344 case Operator.OpType.Addition:
4345 case Operator.OpType.Subtraction:
4350 Location l = lexer.Location;
4351 Report.Error (1019, l, "Overloadable unary operator expected");
4357 void CheckBinaryOperator (Operator.OpType op)
4361 case Operator.OpType.Addition:
4362 case Operator.OpType.Subtraction:
4363 case Operator.OpType.Multiply:
4364 case Operator.OpType.Division:
4365 case Operator.OpType.Modulus:
4366 case Operator.OpType.BitwiseAnd:
4367 case Operator.OpType.BitwiseOr:
4368 case Operator.OpType.ExclusiveOr:
4369 case Operator.OpType.LeftShift:
4370 case Operator.OpType.RightShift:
4371 case Operator.OpType.Equality:
4372 case Operator.OpType.Inequality:
4373 case Operator.OpType.GreaterThan:
4374 case Operator.OpType.LessThan:
4375 case Operator.OpType.GreaterThanOrEqual:
4376 case Operator.OpType.LessThanOrEqual:
4380 Location l = lexer.Location;
4381 Report.Error (1020, l, "Overloadable binary operator expected");
4387 void syntax_error (Location l, string msg)
4389 Report.Error (1003, l, "Syntax error, " + msg);
4392 void output (string s)
4394 Console.WriteLine (s);
4397 void note (string s)
4399 // Used to put annotations
4404 public Tokenizer Lexer {
4410 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4412 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4413 this.name = file.Name;
4415 current_container = RootContext.Tree.Types;
4416 current_container.NamespaceEntry = current_namespace;
4417 oob_stack = new Stack ();
4418 switch_stack = new Stack ();
4420 lexer = new Tokenizer (reader, file, defines);
4423 public void parse ()
4426 if (yacc_verbose_flag)
4427 yyparse (lexer, new yydebug.yyDebugSimple ());
4430 Tokenizer tokenizer = lexer as Tokenizer;
4431 tokenizer.cleanup ();
4432 } catch (Exception e){
4433 // Please do not remove this, it is used during debugging
4436 Console.WriteLine (e);
4437 Report.Error (-25, lexer.Location, "Parsing error");
4438 if (Driver.parser_verbose)
4439 Console.WriteLine (e);
4443 void CheckToken (int error, int yyToken, string msg)
4445 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4446 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4449 Report.Error (error, lexer.Location, msg);
4452 void CheckIdentifierToken (int yyToken)
4454 CheckToken (1041, yyToken, "Identifier expected");