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)
11 // (C) 2004 Novell, Inc
14 // (1) Figure out why error productions dont work. `type-declaration' is a
15 // great spot to put an `error' because you can reproduce it with this input:
18 // Possible optimization:
19 // Run memory profiler with parsing only, and consider dropping
20 // arraylists where not needed. Some pieces can use linked lists.
28 using System.Collections;
33 public class CSharpParser {
34 NamespaceEntry current_namespace;
35 TypeContainer current_container;
36 TypeContainer current_class;
38 IIteratorContainer iterator_container;
41 /// Current block is used to add statements as we find
44 Block current_block, top_current_block;
46 Delegate current_delegate;
49 /// This is used by the unary_expression code to resolve
50 /// a name against a parameter.
52 Parameters current_local_parameters;
55 /// Using during property parsing to describe the implicit
56 /// value parameter that is passed to the "set" and "get"accesor
57 /// methods (properties and indexers).
59 Expression implicit_value_parameter_type;
60 Parameters indexer_parameters;
63 /// Used to determine if we are parsing the get/set pair
64 /// of an indexer or a property
69 /// An out-of-band stack.
78 static public int yacc_verbose_flag;
80 // Name of the file we are parsing
89 /// Temporary Xml documentation cache.
90 /// For enum types, we need one more temporary store.
93 string enumTypeComment;
95 /// Current attribute target
96 string current_attr_target;
98 /// assembly and module attribute definition is enabled
99 bool global_attrs_enabled = true;
103 %token NONE /* This token is never returned by our lexer */
104 %token ERROR // This is used not by the parser, but by the tokenizer.
108 *These are the C# keywords
195 /* C# keywords which are not really keywords */
201 /* C# single character operators/punctuation. */
202 %token OPEN_BRACE "{"
203 %token CLOSE_BRACE "}"
204 %token OPEN_BRACKET "["
205 %token CLOSE_BRACKET "]"
206 %token OPEN_PARENS "("
207 %token CLOSE_PARENS ")"
219 %token OP_GENERICS_LT "<"
221 %token OP_GENERICS_GT ">"
222 %token BITWISE_AND "&"
223 %token BITWISE_OR "|"
230 /* C# multi-character operators. */
233 %token OP_SHIFT_LEFT "<<"
234 %token OP_SHIFT_RIGHT ">>"
241 %token OP_MULT_ASSIGN "*="
242 %token OP_DIV_ASSIGN "/="
243 %token OP_MOD_ASSIGN "%="
244 %token OP_ADD_ASSIGN "+="
245 %token OP_SUB_ASSIGN "-="
246 %token OP_SHIFT_LEFT_ASSIGN "<<="
247 %token OP_SHIFT_RIGHT_ASSIGN ">>="
248 %token OP_AND_ASSIGN "&="
249 %token OP_XOR_ASSIGN "^="
250 %token OP_OR_ASSIGN "|="
254 %token LITERAL_INTEGER "int literal"
255 %token LITERAL_FLOAT "float literal"
256 %token LITERAL_DOUBLE "double literal"
257 %token LITERAL_DECIMAL "decimal literal"
258 %token LITERAL_CHARACTER "character literal"
259 %token LITERAL_STRING "string literal"
262 %token CLOSE_PARENS_CAST
263 %token CLOSE_PARENS_NO_CAST
264 %token CLOSE_PARENS_OPEN_PARENS
265 %token CLOSE_PARENS_MINUS
266 %token DEFAULT_OPEN_PARENS
267 %token GENERIC_DIMENSION
269 /* Add precedence rules to solve dangling else s/r conflict */
278 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
280 %left STAR DIV PERCENT
281 %right BANG CARRET UMINUS
282 %nonassoc OP_INC OP_DEC
284 %left OPEN_BRACKET OPEN_BRACE
288 %start compilation_unit
292 : outer_declarations opt_EOF
293 | outer_declarations global_attributes opt_EOF
294 | global_attributes opt_EOF
295 | opt_EOF /* allow empty files */
301 Lexer.check_incorrect_doc_comment ();
305 Lexer.check_incorrect_doc_comment ();
311 | outer_declarations outer_declaration
316 | namespace_member_declaration
321 | using_directives using_directive
325 : using_alias_directive
327 if (RootContext.Documentation != null)
328 Lexer.doc_state = XmlCommentState.Allowed;
330 | using_namespace_directive
332 if (RootContext.Documentation != null)
333 Lexer.doc_state = XmlCommentState.Allowed;
337 using_alias_directive
338 : USING IDENTIFIER ASSIGN
339 namespace_or_type_name SEMICOLON
341 MemberName name = (MemberName) $4;
342 current_namespace.UsingAlias ((string) $2, name.GetTypeExpression (lexer.Location), lexer.Location);
345 CheckIdentifierToken (yyToken);
349 using_namespace_directive
350 : USING namespace_name SEMICOLON
352 MemberName ns_name = (MemberName) $2;
353 current_namespace.Using (ns_name.GetTypeExpression (lexer.Location), lexer.Location);
358 // Strictly speaking, namespaces don't have attributes but
359 // we parse global attributes along with namespace declarations and then
362 namespace_declaration
363 : opt_attributes NAMESPACE namespace_or_type_name
366 Report.Error(1518, Lexer.Location, "Attributes cannot be applied to namespaces."
367 + " Expected class, delegate, enum, interface, or struct");
370 MemberName name = (MemberName) $3;
372 if (name.TypeArguments != null)
373 syntax_error (lexer.Location, "namespace name expected");
374 else if ((current_namespace.Parent != null) && (name.Left != null)) {
375 Report.Error (134, lexer.Location,
376 "Cannot use qualified namespace names in nested " +
377 "namespace declarations");
380 current_namespace = new NamespaceEntry (
381 current_namespace, file, name.GetName (), lexer.Location);
383 namespace_body opt_semicolon
385 current_namespace = current_namespace.Parent;
400 : namespace_or_type_name {
401 MemberName name = (MemberName) $1;
403 if (name.TypeArguments != null)
404 syntax_error (lexer.Location, "namespace name expected");
413 if (RootContext.Documentation != null)
414 Lexer.doc_state = XmlCommentState.Allowed;
417 opt_namespace_member_declarations
428 opt_namespace_member_declarations
430 | namespace_member_declarations
433 namespace_member_declarations
434 : namespace_member_declaration
435 | namespace_member_declarations namespace_member_declaration
438 namespace_member_declaration
445 Class c = (Class) $1;
446 mod_flags = c.ModFlags;
448 } else if ($1 is Struct){
449 Struct s = (Struct) $1;
450 mod_flags = s.ModFlags;
455 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
457 1527, lexer.Location,
458 "Namespace elements cant be explicitly " +
459 "declared private or protected in `" + name + "'");
461 current_namespace.DeclarationFound = true;
463 | namespace_declaration {
464 current_namespace.DeclarationFound = true;
467 | field_declaration {
468 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
470 | method_declaration {
471 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
478 | interface_declaration
480 | delegate_declaration
482 // Enable this when we have handled all errors, because this acts as a generic fallback
485 // Console.WriteLine ("Token=" + yyToken);
486 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
498 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
506 global_attrs_enabled = false;
511 global_attrs_enabled = false;
520 ArrayList sect = (ArrayList) $1;
522 if (global_attrs_enabled) {
523 if (current_attr_target == "module") {
524 CodeGen.Module.AddAttributes (sect);
526 } else if (current_attr_target == "assembly") {
527 CodeGen.Assembly.AddAttributes (sect);
530 $$ = new Attributes (sect);
533 if (RootContext.Documentation != null) {
534 Lexer.check_incorrect_doc_comment ();
536 XmlCommentState.Allowed;
540 $$ = new Attributes (sect);
542 current_attr_target = null;
544 | attribute_sections attribute_section
546 Attributes attrs = $1 as Attributes;
547 ArrayList sect = (ArrayList) $2;
549 if (global_attrs_enabled) {
550 if (current_attr_target == "module") {
551 CodeGen.Module.AddAttributes (sect);
553 } else if (current_attr_target == "assembly") {
554 CodeGen.Assembly.AddAttributes (sect);
558 attrs = new Attributes (sect);
560 attrs.AddAttributes (sect);
564 attrs = new Attributes (sect);
566 attrs.AddAttributes (sect);
569 current_attr_target = null;
574 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
578 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
584 attribute_target_specifier
585 : attribute_target COLON
587 current_attr_target = (string)$1;
595 CheckAttributeTarget ((string) $1);
598 | EVENT { $$ = "event"; }
599 | RETURN { $$ = "return"; }
605 ArrayList attrs = new ArrayList (4);
611 | attribute_list COMMA attribute
613 ArrayList attrs = (ArrayList) $1;
625 opt_attribute_arguments
627 Location loc = (Location) $2;
628 MemberName mname = (MemberName) $1;
629 if (mname.IsGeneric) {
630 Report.Error (404, lexer.Location,
631 "'<' unexpected: attributes cannot be generic");
634 MemberName left = mname.Left;
635 string identifier = mname.Name;
637 Expression left_expr = left == null ? null : left.GetTypeExpression (loc);
639 if (current_attr_target == "assembly" || current_attr_target == "module")
640 $$ = new GlobalAttribute (current_container, current_attr_target,
641 left_expr, identifier, (ArrayList) $3, loc);
643 $$ = new Attribute (current_attr_target, left_expr, identifier, (ArrayList) $3, loc);
648 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
651 opt_attribute_arguments
652 : /* empty */ { $$ = null; }
653 | OPEN_PARENS attribute_arguments CLOSE_PARENS
661 : opt_positional_argument_list
666 ArrayList args = new ArrayList (4);
672 | positional_argument_list COMMA named_argument_list
674 ArrayList args = new ArrayList (4);
680 | named_argument_list
682 ArrayList args = new ArrayList (4);
691 opt_positional_argument_list
692 : /* empty */ { $$ = null; }
693 | positional_argument_list
696 positional_argument_list
699 ArrayList args = new ArrayList (4);
700 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
704 | positional_argument_list COMMA expression
706 ArrayList args = (ArrayList) $1;
707 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
716 ArrayList args = new ArrayList (4);
721 | named_argument_list COMMA named_argument
723 ArrayList args = (ArrayList) $1;
728 | named_argument_list COMMA expression
730 Report.Error (1016, lexer.Location, "Named attribute argument expected");
736 : IDENTIFIER ASSIGN expression
738 $$ = new DictionaryEntry (
740 new Argument ((Expression) $3, Argument.AType.Expression));
746 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
749 opt_class_member_declarations
751 | class_member_declarations
754 class_member_declarations
755 : class_member_declaration
756 | class_member_declarations
757 class_member_declaration
760 class_member_declaration
761 : constant_declaration // done
762 | field_declaration // done
763 | method_declaration // done
764 | property_declaration // done
765 | event_declaration // done
766 | indexer_declaration // done
767 | operator_declaration // done
768 | constructor_declaration // done
769 | destructor_declaration // done
779 MemberName name = MakeName ((MemberName) $5);
780 bool partial = (bool) $3;
783 ClassPart part = PartialContainer.CreatePart (
784 current_namespace, current_container, name, (int) $2,
785 (Attributes) $1, Kind.Struct, lexer.Location);
787 current_container = part.PartialContainer;
788 current_class = part;
790 current_class = new Struct (
791 current_namespace, current_container, name, (int) $2,
792 (Attributes) $1, lexer.Location);
794 current_container = current_class;
795 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
798 lexer.ConstraintsParsing = true;
801 opt_type_parameter_constraints_clauses
803 lexer.ConstraintsParsing = false;
806 current_class.Bases = (ArrayList) $7;
808 current_class.SetParameterInfo ((ArrayList) $8);
810 if (RootContext.Documentation != null)
811 current_class.DocComment = Lexer.consume_doc_comment ();
813 current_class.Register ();
817 if (RootContext.Documentation != null)
818 Lexer.doc_state = XmlCommentState.Allowed;
824 current_container = current_container.Parent;
825 current_class = current_container;
827 | opt_attributes opt_modifiers opt_partial STRUCT error {
828 CheckIdentifierToken (yyToken);
835 if (RootContext.Documentation != null)
836 Lexer.doc_state = XmlCommentState.Allowed;
838 opt_struct_member_declarations CLOSE_BRACE
841 opt_struct_member_declarations
843 | struct_member_declarations
846 struct_member_declarations
847 : struct_member_declaration
848 | struct_member_declarations struct_member_declaration
851 struct_member_declaration
852 : constant_declaration
855 | property_declaration
857 | indexer_declaration
858 | operator_declaration
859 | constructor_declaration
863 * This is only included so we can flag error 575:
864 * destructors only allowed on class types
866 | destructor_declaration
877 int modflags = (int) $2;
878 foreach (VariableDeclaration constant in (ArrayList) $5){
879 Location l = constant.Location;
880 if ((modflags & Modifiers.STATIC) != 0) {
881 Report.Error (504, l, "The constant '{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
885 Const c = new Const (
886 current_class, (Expression) $4, (string) constant.identifier,
887 (Expression) constant.expression_or_array_initializer, modflags,
890 if (RootContext.Documentation != null) {
891 c.DocComment = Lexer.consume_doc_comment ();
892 Lexer.doc_state = XmlCommentState.Allowed;
894 current_container.AddConstant (c);
900 : constant_declarator
902 ArrayList constants = new ArrayList (4);
907 | constant_declarators COMMA constant_declarator
910 ArrayList constants = (ArrayList) $1;
917 : IDENTIFIER ASSIGN constant_expression
919 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
923 // A const field requires a value to be provided
924 Report.Error (145, lexer.Location, "A const field requires a value to be provided");
936 Expression type = (Expression) $3;
939 foreach (VariableDeclaration var in (ArrayList) $4){
940 Location l = var.Location;
942 Field field = new Field (current_class, type, mod, var.identifier,
943 var.expression_or_array_initializer,
946 if (RootContext.Documentation != null) {
947 field.DocComment = Lexer.consume_doc_comment ();
948 Lexer.doc_state = XmlCommentState.Allowed;
950 current_container.AddField (field);
958 Report.Error (670, lexer.Location, "void type is not allowed for fields");
963 : variable_declarator
965 ArrayList decl = new ArrayList (4);
969 | variable_declarators COMMA variable_declarator
971 ArrayList decls = (ArrayList) $1;
978 : IDENTIFIER ASSIGN variable_initializer
980 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
984 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
997 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
999 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
1003 Report.Error (1575, lexer.Location, "A stackalloc expression requires [] after type");
1010 iterator_container = (IIteratorContainer) $1;
1011 if (RootContext.Documentation != null)
1012 Lexer.doc_state = XmlCommentState.NotAllowed;
1016 Method method = (Method) $1;
1017 Block b = (Block) $3;
1018 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
1021 if ((method.ModFlags & extern_abstract) == 0){
1023 501, lexer.Location, current_container.MakeName (method.Name) +
1024 "must declare a body because it is not marked abstract or extern");
1027 if ((method.ModFlags & Modifiers.EXTERN) != 0){
1029 179, lexer.Location, current_container.MakeName (method.Name) +
1030 " is declared extern, but has a body");
1034 method.Block = (ToplevelBlock) $3;
1035 current_container.AddMethod (method);
1037 current_local_parameters = null;
1038 iterator_container = null;
1040 if (RootContext.Documentation != null)
1041 Lexer.doc_state = XmlCommentState.Allowed;
1048 type namespace_or_type_name
1049 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1051 lexer.ConstraintsParsing = true;
1053 opt_type_parameter_constraints_clauses
1055 lexer.ConstraintsParsing = false;
1057 MemberName name = (MemberName) $4;
1059 if ($9 != null && name.TypeArguments == null)
1060 Report.Error (80, lexer.Location,
1061 "Contraints are not allowed on non-generic declarations");
1065 GenericMethod generic = null;
1066 if (name.TypeArguments != null) {
1067 generic = new GenericMethod (current_namespace, current_class,
1068 name, lexer.Location);
1070 generic.SetParameterInfo ((ArrayList) $9);
1073 method = new Method (current_class, generic, (Expression) $3, (int) $2, false,
1074 name, (Parameters) $6, (Attributes) $1, lexer.Location);
1076 current_local_parameters = (Parameters) $6;
1078 if (RootContext.Documentation != null)
1079 method.DocComment = Lexer.consume_doc_comment ();
1085 VOID namespace_or_type_name
1086 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1088 lexer.ConstraintsParsing = true;
1090 opt_type_parameter_constraints_clauses
1092 lexer.ConstraintsParsing = false;
1094 MemberName name = (MemberName) $4;
1096 if ($9 != null && name.TypeArguments == null)
1097 Report.Error (80, lexer.Location,
1098 "Contraints are not allowed on non-generic declarations");
1101 GenericMethod generic = null;
1102 if (name.TypeArguments != null) {
1103 generic = new GenericMethod (current_namespace, current_class,
1104 name, lexer.Location);
1106 generic.SetParameterInfo ((ArrayList) $9);
1109 method = new Method (current_class, generic, TypeManager.system_void_expr,
1110 (int) $2, false, name, (Parameters) $6, (Attributes) $1,
1113 current_local_parameters = (Parameters) $6;
1115 if (RootContext.Documentation != null)
1116 method.DocComment = Lexer.consume_doc_comment ();
1123 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1125 Report.Error (1585, lexer.Location,
1126 String.Format ("Modifier {0} should appear before type",
1127 Modifiers.Name ((int) $4)));
1128 MemberName name = (MemberName) $4;
1130 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1131 0, false, name, (Parameters) $6, (Attributes) $1,
1134 current_local_parameters = (Parameters) $6;
1136 if (RootContext.Documentation != null)
1137 method.DocComment = Lexer.consume_doc_comment ();
1145 | SEMICOLON { $$ = null; }
1148 opt_formal_parameter_list
1149 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1150 | formal_parameter_list
1153 formal_parameter_list
1156 ArrayList pars_list = (ArrayList) $1;
1158 Parameter [] pars = new Parameter [pars_list.Count];
1159 pars_list.CopyTo (pars);
1161 $$ = new Parameters (pars, null, lexer.Location);
1163 | fixed_parameters COMMA parameter_array
1165 ArrayList pars_list = (ArrayList) $1;
1167 Parameter [] pars = new Parameter [pars_list.Count];
1168 pars_list.CopyTo (pars);
1170 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
1172 | fixed_parameters COMMA ARGLIST
1174 ArrayList pars_list = (ArrayList) $1;
1176 Parameter [] pars = new Parameter [pars_list.Count];
1177 pars_list.CopyTo (pars);
1179 $$ = new Parameters (pars, true, lexer.Location);
1183 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
1187 $$ = new Parameters (null, true, lexer.Location);
1194 ArrayList pars = new ArrayList (4);
1199 | fixed_parameters COMMA fixed_parameter
1201 ArrayList pars = (ArrayList) $1;
1210 opt_parameter_modifier
1214 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
1217 opt_parameter_modifier
1220 Report.Error (1001, lexer.Location, "Identifier expected");
1224 opt_parameter_modifier
1227 CheckIdentifierToken (yyToken);
1231 opt_parameter_modifier
1237 Report.Error (241, lexer.Location, "Default parameter specifiers are not permitted");
1242 opt_parameter_modifier
1243 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1244 | parameter_modifier
1248 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1249 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1253 : opt_attributes PARAMS type IDENTIFIER
1255 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1256 note ("type must be a single-dimension array type");
1258 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1260 Report.Error (1611, lexer.Location, "The params parameter cannot be declared as ref or out");
1263 | opt_attributes PARAMS type error {
1264 CheckIdentifierToken (yyToken);
1269 property_declaration
1273 namespace_or_type_name
1275 if (RootContext.Documentation != null)
1276 tmpComment = Lexer.consume_doc_comment ();
1280 implicit_value_parameter_type = (Expression) $3;
1282 lexer.PropertyParsing = true;
1284 $$ = lexer.Location;
1286 iterator_container = SimpleIteratorContainer.GetSimple ();
1288 accessor_declarations
1290 lexer.PropertyParsing = false;
1295 Pair pair = (Pair) $8;
1296 Accessor get_block = (Accessor) pair.First;
1297 Accessor set_block = (Accessor) pair.Second;
1299 Location loc = (Location) $7;
1300 MemberName name = (MemberName) $4;
1302 if (name.TypeArguments != null)
1303 syntax_error (lexer.Location, "a property can't have type arguments");
1305 prop = new Property (current_container, (Expression) $3, (int) $2, false,
1306 name, (Attributes) $1, get_block, set_block, loc);
1307 if (SimpleIteratorContainer.Simple.Yields)
1310 current_container.AddProperty (prop);
1311 implicit_value_parameter_type = null;
1312 iterator_container = null;
1314 if (RootContext.Documentation != null)
1315 prop.DocComment = ConsumeStoredComment ();
1320 accessor_declarations
1321 : get_accessor_declaration opt_set_accessor_declaration
1323 $$ = new Pair ($1, $2);
1325 | set_accessor_declaration opt_get_accessor_declaration
1327 $$ = new Pair ($2, $1);
1331 opt_get_accessor_declaration
1332 : /* empty */ { $$ = null; }
1333 | get_accessor_declaration
1336 opt_set_accessor_declaration
1337 : /* empty */ { $$ = null; }
1338 | set_accessor_declaration
1341 get_accessor_declaration
1342 : opt_attributes opt_modifiers GET
1344 // If this is not the case, then current_local_parameters has already
1345 // been set in indexer_declaration
1346 if (parsing_indexer == false)
1347 current_local_parameters = null;
1349 current_local_parameters = indexer_parameters;
1350 lexer.PropertyParsing = false;
1354 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1355 current_local_parameters = null;
1356 lexer.PropertyParsing = true;
1358 if (RootContext.Documentation != null)
1359 if (Lexer.doc_state == XmlCommentState.Error)
1360 Lexer.doc_state = XmlCommentState.NotAllowed;
1364 set_accessor_declaration
1365 : opt_attributes opt_modifiers SET
1368 Parameter implicit_value_parameter = new Parameter (
1369 implicit_value_parameter_type, "value",
1370 Parameter.Modifier.NONE, null);
1372 if (parsing_indexer == false) {
1373 args = new Parameter [1];
1374 args [0] = implicit_value_parameter;
1375 current_local_parameters = new Parameters (args, null, lexer.Location);
1377 Parameter [] fpars = indexer_parameters.FixedParameters;
1380 int count = fpars.Length;
1382 args = new Parameter [count + 1];
1383 fpars.CopyTo (args, 0);
1384 args [count] = implicit_value_parameter;
1387 current_local_parameters = new Parameters (
1388 args, indexer_parameters.ArrayParameter, lexer.Location);
1391 lexer.PropertyParsing = false;
1395 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1396 current_local_parameters = null;
1397 lexer.PropertyParsing = true;
1399 if (RootContext.Documentation != null
1400 && Lexer.doc_state == XmlCommentState.Error)
1401 Lexer.doc_state = XmlCommentState.NotAllowed;
1407 | SEMICOLON { $$ = null; }
1410 interface_declaration
1414 INTERFACE member_name
1416 MemberName name = MakeName ((MemberName) $5);
1417 bool partial = (bool) $3;
1420 ClassPart part = PartialContainer.CreatePart (
1421 current_namespace, current_container, name, (int) $2,
1422 (Attributes) $1, Kind.Interface, lexer.Location);
1424 current_container = part.PartialContainer;
1425 current_class = part;
1427 current_class = new Interface (
1428 current_namespace, current_container, name, (int) $2,
1429 (Attributes) $1, lexer.Location);
1431 current_container = current_class;
1432 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
1435 lexer.ConstraintsParsing = true;
1438 opt_type_parameter_constraints_clauses
1440 lexer.ConstraintsParsing = false;
1443 current_class.Bases = (ArrayList) $7;
1445 current_class.SetParameterInfo ((ArrayList) $8);
1447 if (RootContext.Documentation != null) {
1448 current_class.DocComment = Lexer.consume_doc_comment ();
1449 Lexer.doc_state = XmlCommentState.Allowed;
1452 current_class.Register ();
1456 if (RootContext.Documentation != null)
1457 Lexer.doc_state = XmlCommentState.Allowed;
1463 current_container = current_container.Parent;
1464 current_class = current_container;
1466 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1467 CheckIdentifierToken (yyToken);
1473 opt_interface_member_declarations
1477 opt_interface_member_declarations
1479 | interface_member_declarations
1482 interface_member_declarations
1483 : interface_member_declaration
1484 | interface_member_declarations interface_member_declaration
1487 interface_member_declaration
1488 : interface_method_declaration
1490 Method m = (Method) $1;
1492 if (m.IsExplicitImpl)
1493 Report.Error (541, lexer.Location,
1494 "Explicit interface declaration can only be declared in a class or struct");
1496 current_container.AddMethod (m);
1498 if (RootContext.Documentation != null)
1499 Lexer.doc_state = XmlCommentState.Allowed;
1501 | interface_property_declaration
1503 Property p = (Property) $1;
1505 if (p.IsExplicitImpl)
1506 Report.Error (541, lexer.Location,
1507 "Explicit interface declaration can only be declared in a class or struct");
1509 current_container.AddProperty (p);
1511 if (RootContext.Documentation != null)
1512 Lexer.doc_state = XmlCommentState.Allowed;
1514 | interface_event_declaration
1517 Event e = (Event) $1;
1519 if (e.IsExplicitImpl)
1520 Report.Error (541, lexer.Location,
1521 "Explicit interface declaration can only be declared in a class or struct");
1523 current_container.AddEvent (e);
1526 if (RootContext.Documentation != null)
1527 Lexer.doc_state = XmlCommentState.Allowed;
1529 | interface_indexer_declaration
1531 Indexer i = (Indexer) $1;
1533 if (i.IsExplicitImpl)
1534 Report.Error (541, lexer.Location,
1535 "Explicit interface declaration can only be declared in a class or struct");
1537 current_container.AddIndexer (i);
1539 if (RootContext.Documentation != null)
1540 Lexer.doc_state = XmlCommentState.Allowed;
1542 | delegate_declaration
1544 Report.Error (524, lexer.Location, "Interfaces can not declare delegates");
1548 Report.Error (524, lexer.Location, "Interfaces can not declare classes");
1550 | struct_declaration
1552 Report.Error (524, lexer.Location, "Interfaces can not declare structures");
1556 Report.Error (524, lexer.Location, "Interfaces can not declare enumerations");
1558 | interface_declaration
1560 Report.Error (524, lexer.Location, "Interfaces can not declare interfaces");
1568 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, lexer.Location);
1573 interface_method_declaration
1574 : opt_attributes opt_new type namespace_or_type_name
1575 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1577 lexer.ConstraintsParsing = true;
1579 opt_type_parameter_constraints_clauses
1582 lexer.ConstraintsParsing = false;
1584 MemberName name = (MemberName) $4;
1586 if ($9 != null && name.TypeArguments == null)
1587 Report.Error (80, lexer.Location,
1588 "Contraints are not allowed on non-generic declarations");
1590 GenericMethod generic = null;
1591 if (name.TypeArguments != null) {
1592 generic = new GenericMethod (current_namespace, current_class,
1593 name, lexer.Location);
1595 generic.SetParameterInfo ((ArrayList) $9);
1598 $$ = new Method (current_class, generic, (Expression) $3, (int) $2, true, name,
1599 (Parameters) $6, (Attributes) $1, lexer.Location);
1600 if (RootContext.Documentation != null)
1601 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1603 | opt_attributes opt_new VOID namespace_or_type_name
1604 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1606 lexer.ConstraintsParsing = true;
1608 opt_type_parameter_constraints_clauses
1611 lexer.ConstraintsParsing = false;
1613 MemberName name = (MemberName) $4;
1615 if ($9 != null && name.TypeArguments == null)
1616 Report.Error (80, lexer.Location,
1617 "Contraints are not allowed on non-generic declarations");
1619 GenericMethod generic = null;
1620 if (name.TypeArguments != null) {
1621 generic = new GenericMethod (current_namespace, current_class,
1622 name, lexer.Location);
1624 generic.SetParameterInfo ((ArrayList) $9);
1627 $$ = new Method (current_class, generic, TypeManager.system_void_expr, (int) $2,
1628 true, name, (Parameters) $6, (Attributes) $1, lexer.Location);
1629 if (RootContext.Documentation != null)
1630 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1634 interface_property_declaration
1639 { lexer.PropertyParsing = true; }
1641 { lexer.PropertyParsing = false; }
1644 InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
1646 $$ = new Property (current_class, (Expression) $3, (int) $2, true,
1647 new MemberName ((string) $4), (Attributes) $1,
1648 pinfo.Get, pinfo.Set, lexer.Location);
1649 if (RootContext.Documentation != null)
1650 ((Property) $$).DocComment = Lexer.consume_doc_comment ();
1655 CheckIdentifierToken (yyToken);
1661 : opt_attributes opt_modifiers GET SEMICOLON
1662 { $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, (int) $2, 0, lexer.Location, lexer.Location); }
1663 | opt_attributes opt_modifiers SET SEMICOLON
1664 { $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1, 0, (int) $2, lexer.Location, lexer.Location); }
1665 | opt_attributes opt_modifiers GET SEMICOLON opt_attributes opt_modifiers SET SEMICOLON
1666 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $5, (int) $2, (int) $6, lexer.Location, lexer.Location); }
1667 | opt_attributes opt_modifiers SET SEMICOLON opt_attributes opt_modifiers GET SEMICOLON
1668 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $5, (Attributes) $1, (int) $6, (int) $2, lexer.Location, lexer.Location); }
1671 interface_event_declaration
1672 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1674 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1675 new MemberName ((string) $5), null,
1676 (Attributes) $1, lexer.Location);
1677 if (RootContext.Documentation != null)
1678 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1680 | opt_attributes opt_new EVENT type error {
1681 CheckIdentifierToken (yyToken);
1684 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1685 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1688 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE event_accessor_declarations CLOSE_BRACE {
1689 Report.Error (69, lexer.Location, "Event accessors not valid on interfaces");
1694 interface_indexer_declaration
1695 : opt_attributes opt_new type THIS
1696 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1698 { lexer.PropertyParsing = true; }
1700 { lexer.PropertyParsing = false; }
1703 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1705 $$ = new Indexer (current_class, (Expression) $3,
1706 new MemberName (TypeContainer.DefaultIndexerName),
1707 (int) $2, true, (Parameters) $6, (Attributes) $1,
1708 info.Get, info.Set, lexer.Location);
1709 if (RootContext.Documentation != null)
1710 ((Indexer) $$).DocComment = ConsumeStoredComment ();
1714 operator_declaration
1715 : opt_attributes opt_modifiers operator_declarator
1717 iterator_container = SimpleIteratorContainer.GetSimple ();
1721 OperatorDeclaration decl = (OperatorDeclaration) $3;
1723 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1725 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null);
1726 if (decl.arg2type != null)
1727 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null);
1729 Operator op = new Operator (
1730 current_class, decl.optype, decl.ret_type, (int) $2,
1731 new Parameters (param_list, null, decl.location),
1732 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1734 if (RootContext.Documentation != null) {
1735 op.DocComment = tmpComment;
1736 Lexer.doc_state = XmlCommentState.Allowed;
1739 if (SimpleIteratorContainer.Simple.Yields)
1742 // Note again, checking is done in semantic analysis
1743 current_container.AddOperator (op);
1745 current_local_parameters = null;
1746 iterator_container = null;
1752 | SEMICOLON { $$ = null; }
1755 : type OPERATOR overloadable_operator
1756 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1758 Operator.OpType op = (Operator.OpType) $3;
1759 CheckUnaryOperator (op);
1761 if (op == Operator.OpType.Addition)
1762 op = Operator.OpType.UnaryPlus;
1764 if (op == Operator.OpType.Subtraction)
1765 op = Operator.OpType.UnaryNegation;
1767 Parameter [] pars = new Parameter [1];
1768 Expression type = (Expression) $5;
1770 pars [0] = new Parameter (type, (string) $6, Parameter.Modifier.NONE, null);
1772 current_local_parameters = new Parameters (pars, null, lexer.Location);
1774 if (RootContext.Documentation != null) {
1775 tmpComment = Lexer.consume_doc_comment ();
1776 Lexer.doc_state = XmlCommentState.NotAllowed;
1779 $$ = new OperatorDeclaration (op, (Expression) $1, type, (string) $6,
1780 null, null, lexer.Location);
1782 | type OPERATOR overloadable_operator
1784 type IDENTIFIER COMMA
1788 CheckBinaryOperator ((Operator.OpType) $3);
1790 Parameter [] pars = new Parameter [2];
1792 Expression typeL = (Expression) $5;
1793 Expression typeR = (Expression) $8;
1795 pars [0] = new Parameter (typeL, (string) $6, Parameter.Modifier.NONE, null);
1796 pars [1] = new Parameter (typeR, (string) $9, Parameter.Modifier.NONE, null);
1798 current_local_parameters = new Parameters (pars, null, lexer.Location);
1800 if (RootContext.Documentation != null) {
1801 tmpComment = Lexer.consume_doc_comment ();
1802 Lexer.doc_state = XmlCommentState.NotAllowed;
1805 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1807 typeR, (string) $9, lexer.Location);
1809 | conversion_operator_declarator
1812 overloadable_operator
1814 : BANG { $$ = Operator.OpType.LogicalNot; }
1815 | TILDE { $$ = Operator.OpType.OnesComplement; }
1816 | OP_INC { $$ = Operator.OpType.Increment; }
1817 | OP_DEC { $$ = Operator.OpType.Decrement; }
1818 | TRUE { $$ = Operator.OpType.True; }
1819 | FALSE { $$ = Operator.OpType.False; }
1820 // Unary and binary:
1821 | PLUS { $$ = Operator.OpType.Addition; }
1822 | MINUS { $$ = Operator.OpType.Subtraction; }
1824 | STAR { $$ = Operator.OpType.Multiply; }
1825 | DIV { $$ = Operator.OpType.Division; }
1826 | PERCENT { $$ = Operator.OpType.Modulus; }
1827 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1828 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1829 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1830 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1831 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1832 | OP_EQ { $$ = Operator.OpType.Equality; }
1833 | OP_NE { $$ = Operator.OpType.Inequality; }
1834 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1835 | OP_LT { $$ = Operator.OpType.LessThan; }
1836 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1837 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1840 conversion_operator_declarator
1841 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1843 Parameter [] pars = new Parameter [1];
1845 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1847 current_local_parameters = new Parameters (pars, null, lexer.Location);
1849 if (RootContext.Documentation != null) {
1850 tmpComment = Lexer.consume_doc_comment ();
1851 Lexer.doc_state = XmlCommentState.NotAllowed;
1854 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1855 null, null, lexer.Location);
1857 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1859 Parameter [] pars = new Parameter [1];
1861 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1863 current_local_parameters = new Parameters (pars, null, lexer.Location);
1865 if (RootContext.Documentation != null) {
1866 tmpComment = Lexer.consume_doc_comment ();
1867 Lexer.doc_state = XmlCommentState.NotAllowed;
1870 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1871 null, null, lexer.Location);
1875 syntax_error (lexer.Location, "'operator' expected");
1879 syntax_error (lexer.Location, "'operator' expected");
1883 constructor_declaration
1886 constructor_declarator
1889 Constructor c = (Constructor) $3;
1890 c.Block = (ToplevelBlock) $4;
1891 c.OptAttributes = (Attributes) $1;
1892 c.ModFlags = (int) $2;
1894 if (RootContext.Documentation != null)
1895 c.DocComment = ConsumeStoredComment ();
1897 if (c.Name == current_container.Basename){
1898 if ((c.ModFlags & Modifiers.STATIC) != 0){
1899 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1901 515, c.Location, String.Format (
1902 "`{0}.{1}': static constructor can not have access modifiers",
1903 c.Name, current_container.Name));
1906 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1908 if (c.Initializer != null){
1911 "Static constructors can not have an explicit this or base " +
1912 "constructor invocations");
1915 if (!c.Parameters.Empty){
1917 132, c.Location, "Static constructors should not have parameters");
1920 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1923 // We let another layer check the validity of the constructor.
1924 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1927 current_container.AddConstructor (c);
1929 current_local_parameters = null;
1930 if (RootContext.Documentation != null)
1931 Lexer.doc_state = XmlCommentState.Allowed;
1935 constructor_declarator
1938 if (RootContext.Documentation != null) {
1939 tmpComment = Lexer.consume_doc_comment ();
1940 Lexer.doc_state = XmlCommentState.Allowed;
1943 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS _mark_
1945 current_local_parameters = (Parameters) $4;
1947 opt_constructor_initializer
1949 $$ = new Constructor (current_class, (string) $1, 0, (Parameters) $4,
1950 (ConstructorInitializer) $8, (Location) $6);
1956 | SEMICOLON { $$ = null; }
1959 opt_constructor_initializer
1960 : /* empty */ { $$ = null; }
1961 | constructor_initializer
1964 constructor_initializer
1965 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1967 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1969 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1971 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1974 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1980 : /* EMPTY */ { $$ = 0; }
1981 | UNSAFE { $$ = Modifiers.UNSAFE; }
1982 | EXTERN { $$ = Modifiers.EXTERN; }
1985 destructor_declaration
1986 : opt_attributes opt_finalizer TILDE
1988 if (RootContext.Documentation != null) {
1989 tmpComment = Lexer.consume_doc_comment ();
1990 Lexer.doc_state = XmlCommentState.NotAllowed;
1993 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1995 if ((string) $5 != current_container.Basename){
1996 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1997 } else if (current_container.Kind != Kind.Class){
1998 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
2000 Location l = lexer.Location;
2003 if (!RootContext.StdLib && current_container.Name == "System.Object")
2004 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2006 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2008 if ((m & Modifiers.UNSAFE) != 0){
2009 if (!RootContext.Unsafe){
2010 Report.Error (227, l,
2011 "Unsafe code requires the -unsafe command " +
2012 "line option to be specified");
2016 Method d = new Destructor (
2017 current_class, TypeManager.system_void_expr, m, "Finalize",
2018 new Parameters (null, null, l), (Attributes) $1, l);
2019 if (RootContext.Documentation != null)
2020 d.DocComment = ConsumeStoredComment ();
2022 d.Block = (ToplevelBlock) $8;
2023 current_container.AddMethod (d);
2031 EVENT type variable_declarators SEMICOLON
2033 foreach (VariableDeclaration var in (ArrayList) $5) {
2035 MemberName name = new MemberName (var.identifier);
2037 Event e = new EventField (
2038 current_class, (Expression) $4, (int) $2, false, name,
2039 var.expression_or_array_initializer, (Attributes) $1,
2042 current_container.AddEvent (e);
2044 if (RootContext.Documentation != null) {
2045 e.DocComment = Lexer.consume_doc_comment ();
2046 Lexer.doc_state = XmlCommentState.Allowed;
2052 EVENT type namespace_or_type_name
2055 implicit_value_parameter_type = (Expression) $4;
2056 lexer.EventParsing = true;
2058 event_accessor_declarations
2060 lexer.EventParsing = false;
2064 Location loc = (Location) $7;
2067 Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
2070 Pair pair = (Pair) $9;
2072 MemberName name = (MemberName) $5;
2074 if (name.TypeArguments != null)
2075 syntax_error (lexer.Location, "an event can't have type arguments");
2077 Event e = new EventProperty (
2078 current_class, (Expression) $4, (int) $2, false, name, null,
2079 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
2081 if (RootContext.Documentation != null) {
2082 e.DocComment = Lexer.consume_doc_comment ();
2083 Lexer.doc_state = XmlCommentState.Allowed;
2086 current_container.AddEvent (e);
2087 implicit_value_parameter_type = null;
2090 | opt_attributes opt_modifiers EVENT type namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS block {
2091 MemberName mn = (MemberName) $5;
2093 if (mn.Left != null)
2094 Report.Error (71, lexer.Location, "Explicit implementation of events requires property syntax");
2096 Report.Error (71, lexer.Location, "Event declaration should use property syntax");
2098 if (RootContext.Documentation != null)
2099 Lexer.doc_state = XmlCommentState.Allowed;
2103 event_accessor_declarations
2104 : add_accessor_declaration remove_accessor_declaration
2106 $$ = new Pair ($1, $2);
2108 | remove_accessor_declaration add_accessor_declaration
2110 $$ = new Pair ($2, $1);
2112 | add_accessor_declaration { $$ = null; }
2113 | remove_accessor_declaration { $$ = null; }
2116 Report.Error (1055, lexer.Location, "An add or remove accessor expected");
2122 add_accessor_declaration
2123 : opt_attributes ADD
2125 Parameter [] args = new Parameter [1];
2126 Parameter implicit_value_parameter = new Parameter (
2127 implicit_value_parameter_type, "value",
2128 Parameter.Modifier.NONE, null);
2130 args [0] = implicit_value_parameter;
2132 current_local_parameters = new Parameters (args, null, lexer.Location);
2133 lexer.EventParsing = false;
2137 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2138 lexer.EventParsing = true;
2140 | opt_attributes ADD error {
2141 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2146 remove_accessor_declaration
2147 : opt_attributes REMOVE
2149 Parameter [] args = new Parameter [1];
2150 Parameter implicit_value_parameter = new Parameter (
2151 implicit_value_parameter_type, "value",
2152 Parameter.Modifier.NONE, null);
2154 args [0] = implicit_value_parameter;
2156 current_local_parameters = new Parameters (args, null, lexer.Location);
2157 lexer.EventParsing = false;
2161 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2162 lexer.EventParsing = true;
2164 | opt_attributes REMOVE error {
2165 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2171 : opt_attributes opt_modifiers indexer_declarator
2174 IndexerDeclaration decl = (IndexerDeclaration) $3;
2176 implicit_value_parameter_type = decl.type;
2178 lexer.PropertyParsing = true;
2179 parsing_indexer = true;
2181 indexer_parameters = decl.param_list;
2183 accessor_declarations
2185 lexer.PropertyParsing = false;
2186 parsing_indexer = false;
2190 // The signature is computed from the signature of the indexer. Look
2191 // at section 3.6 on the spec
2192 Location loc = (Location) $5;
2194 IndexerDeclaration decl = (IndexerDeclaration) $3;
2195 Pair pair = (Pair) $7;
2196 Accessor get_block = (Accessor) pair.First;
2197 Accessor set_block = (Accessor) pair.Second;
2200 if (decl.interface_type != null)
2201 name = new MemberName (decl.interface_type,
2202 TypeContainer.DefaultIndexerName, null);
2204 name = new MemberName (TypeContainer.DefaultIndexerName);
2206 indexer = new Indexer (current_class, decl.type, name,
2207 (int) $2, false, decl.param_list, (Attributes) $1,
2208 get_block, set_block, loc);
2209 if (RootContext.Documentation != null)
2210 indexer.DocComment = ConsumeStoredComment ();
2212 current_container.AddIndexer (indexer);
2214 current_local_parameters = null;
2215 implicit_value_parameter_type = null;
2216 indexer_parameters = null;
2221 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2223 Parameters pars = (Parameters) $4;
2224 if (pars.HasArglist) {
2225 // "__arglist is not valid in this context"
2226 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2227 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2228 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2230 if (RootContext.Documentation != null) {
2231 tmpComment = Lexer.consume_doc_comment ();
2232 Lexer.doc_state = XmlCommentState.Allowed;
2235 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
2237 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2239 Parameters pars = (Parameters) $6;
2241 if (pars.HasArglist) {
2242 // "__arglist is not valid in this context"
2243 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2244 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2245 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2248 MemberName name = (MemberName) $2;
2250 $$ = new IndexerDeclaration ((Expression) $1, name, pars);
2252 if (RootContext.Documentation != null) {
2253 tmpComment = Lexer.consume_doc_comment ();
2254 Lexer.doc_state = XmlCommentState.Allowed;
2264 if (RootContext.Documentation != null)
2265 enumTypeComment = Lexer.consume_doc_comment ();
2270 Location enum_location = lexer.Location;
2272 MemberName full_name = MakeName (new MemberName ((string) $4));
2273 Enum e = new Enum (current_namespace, current_container, (Expression) $5, (int) $2,
2274 full_name, (Attributes) $1, enum_location);
2276 if (RootContext.Documentation != null)
2277 e.DocComment = enumTypeComment;
2279 foreach (VariableDeclaration ev in (ArrayList) $7) {
2280 e.AddEnumMember (ev.identifier,
2281 (Expression) ev.expression_or_array_initializer,
2282 ev.Location, ev.OptAttributes,
2286 string name = full_name.GetName ();
2287 current_container.AddEnum (e);
2288 RootContext.Tree.RecordDecl (name, e);
2294 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2295 | COLON type { $$ = $2; }
2301 if (RootContext.Documentation != null)
2302 Lexer.doc_state = XmlCommentState.Allowed;
2304 opt_enum_member_declarations
2306 // here will be evaluated after CLOSE_BLACE is consumed.
2307 if (RootContext.Documentation != null)
2308 Lexer.doc_state = XmlCommentState.Allowed;
2316 opt_enum_member_declarations
2317 : /* empty */ { $$ = new ArrayList (4); }
2318 | enum_member_declarations opt_comma { $$ = $1; }
2321 enum_member_declarations
2322 : enum_member_declaration
2324 ArrayList l = new ArrayList (4);
2329 | enum_member_declarations COMMA enum_member_declaration
2331 ArrayList l = (ArrayList) $1;
2339 enum_member_declaration
2340 : opt_attributes IDENTIFIER
2342 VariableDeclaration vd = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
2344 if (RootContext.Documentation != null) {
2345 vd.DocComment = Lexer.consume_doc_comment ();
2346 Lexer.doc_state = XmlCommentState.Allowed;
2351 | opt_attributes IDENTIFIER
2353 $$ = lexer.Location;
2354 if (RootContext.Documentation != null) {
2355 tmpComment = Lexer.consume_doc_comment ();
2356 Lexer.doc_state = XmlCommentState.NotAllowed;
2361 VariableDeclaration vd = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
2363 if (RootContext.Documentation != null)
2364 vd.DocComment = ConsumeStoredComment ();
2370 delegate_declaration
2373 DELEGATE type member_name
2374 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2376 Location l = lexer.Location;
2377 MemberName name = MakeName ((MemberName) $5);
2378 Delegate del = new Delegate (current_namespace, current_container, (Expression) $4,
2379 (int) $2, name, (Parameters) $7, (Attributes) $1, l);
2381 if (RootContext.Documentation != null) {
2382 del.DocComment = Lexer.consume_doc_comment ();
2383 Lexer.doc_state = XmlCommentState.Allowed;
2386 current_container.AddDelegate (del);
2387 RootContext.Tree.RecordDecl (name.GetName (true), del);
2389 current_delegate = del;
2391 lexer.ConstraintsParsing = true;
2393 opt_type_parameter_constraints_clauses
2395 lexer.ConstraintsParsing = false;
2399 current_delegate.SetParameterInfo ((ArrayList) $9);
2401 current_delegate = null;
2405 DELEGATE VOID member_name
2406 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2408 Location l = lexer.Location;
2409 MemberName name = MakeName ((MemberName) $5);
2410 Delegate del = new Delegate (
2411 current_namespace, current_container,
2412 TypeManager.system_void_expr, (int) $2, name,
2413 (Parameters) $7, (Attributes) $1, l);
2415 if (RootContext.Documentation != null) {
2416 del.DocComment = Lexer.consume_doc_comment ();
2417 Lexer.doc_state = XmlCommentState.Allowed;
2420 current_container.AddDelegate (del);
2421 RootContext.Tree.RecordDecl (name.GetName (true), del);
2423 current_delegate = del;
2425 lexer.ConstraintsParsing = true;
2427 opt_type_parameter_constraints_clauses
2429 lexer.ConstraintsParsing = false;
2433 current_delegate.SetParameterInfo ((ArrayList) $9);
2435 current_delegate = null;
2442 lexer.CheckNullable (false);
2447 lexer.CheckNullable (true);
2452 namespace_or_type_name
2454 | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list {
2455 $$ = new MemberName ((MemberName) $1, (string) $3, (TypeArguments) $4);
2460 : IDENTIFIER opt_type_argument_list {
2461 $$ = new MemberName ((string) $1, (TypeArguments) $2);
2465 opt_type_argument_list
2466 : /* empty */ { $$ = null; }
2467 | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2473 $$ = new TypeArguments ((int) $1, lexer.Location);
2479 TypeArguments type_args = new TypeArguments (lexer.Location);
2480 type_args.Add ((Expression) $1);
2483 | type_arguments COMMA type {
2484 TypeArguments type_args = (TypeArguments) $1;
2485 type_args.Add ((Expression) $3);
2491 * Before you think of adding a return_type, notice that we have been
2492 * using two rules in the places where it matters (one rule using type
2493 * and another identical one that uses VOID as the return type). This
2494 * gets rid of a shift/reduce couple
2497 : namespace_or_type_name opt_nullable
2499 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2502 $$ = new ComposedCast ((Expression) $$, "?", lexer.Location);
2504 | builtin_types opt_nullable
2507 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2517 // Note that here only unmanaged types are allowed but we
2518 // can't perform checks during this phase - we do it during
2519 // semantic analysis.
2521 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2525 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2530 : builtin_types opt_nullable
2533 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2535 | non_expression_type rank_specifier
2537 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2539 | non_expression_type STAR
2541 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2543 | expression rank_specifiers
2545 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2549 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2553 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2554 // through this different path
2556 | multiplicative_expression STAR
2558 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2565 ArrayList types = new ArrayList (4);
2570 | type_list COMMA type
2572 ArrayList types = (ArrayList) $1;
2580 * replaces all the productions for isolating the various
2581 * simple types, but we need this to reuse it easily in local_variable_type
2584 : OBJECT { $$ = TypeManager.system_object_expr; }
2585 | STRING { $$ = TypeManager.system_string_expr; }
2586 | BOOL { $$ = TypeManager.system_boolean_expr; }
2587 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2588 | FLOAT { $$ = TypeManager.system_single_expr; }
2589 | DOUBLE { $$ = TypeManager.system_double_expr; }
2594 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2595 | BYTE { $$ = TypeManager.system_byte_expr; }
2596 | SHORT { $$ = TypeManager.system_int16_expr; }
2597 | USHORT { $$ = TypeManager.system_uint16_expr; }
2598 | INT { $$ = TypeManager.system_int32_expr; }
2599 | UINT { $$ = TypeManager.system_uint32_expr; }
2600 | LONG { $$ = TypeManager.system_int64_expr; }
2601 | ULONG { $$ = TypeManager.system_uint64_expr; }
2602 | CHAR { $$ = TypeManager.system_char_expr; }
2603 | VOID { $$ = TypeManager.system_void_expr; }
2607 : type rank_specifiers opt_nullable
2609 string rank_specifiers = (string) $2;
2611 rank_specifiers += "?";
2613 $$ = new ComposedCast ((Expression) $1, rank_specifiers, lexer.Location);
2618 // Expressions, section 7.5
2627 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2629 | parenthesized_expression
2630 | default_value_expression
2632 | invocation_expression
2636 | post_increment_expression
2637 | post_decrement_expression
2641 | checked_expression
2642 | unchecked_expression
2643 | pointer_member_access
2644 | anonymous_method_expression
2651 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2652 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2653 | NULL { $$ = NullLiteral.Null; }
2657 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2658 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2659 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2664 object v = lexer.Value;
2670 $$ = IntLiteral.Zero;
2672 $$ = IntLiteral.One;
2674 $$ = new IntLiteral (i);
2675 } else if (v is uint)
2676 $$ = new UIntLiteral ((UInt32) v);
2678 $$ = new LongLiteral ((Int64) v);
2679 else if (v is ulong)
2680 $$ = new ULongLiteral ((UInt64) v);
2682 Console.WriteLine ("OOPS. Unexpected result from scanner");
2687 : TRUE { $$ = new BoolLiteral (true); }
2688 | FALSE { $$ = new BoolLiteral (false); }
2691 parenthesized_expression_0
2692 : OPEN_PARENS expression CLOSE_PARENS
2695 lexer.Deambiguate_CloseParens ();
2696 // After this, the next token returned is one of
2697 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2698 // or CLOSE_PARENS_MINUS.
2702 parenthesized_expression
2703 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2707 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2709 // If a parenthesized expression is followed by a minus, we need to wrap
2710 // the expression inside a ParenthesizedExpression for the CS0075 check
2711 // in Binary.DoResolve().
2712 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2717 : primary_expression DOT IDENTIFIER opt_type_argument_list
2719 $$ = new MemberAccess ((Expression) $1, (string) $3,
2720 (TypeArguments) $4, lexer.Location);
2722 | predefined_type DOT IDENTIFIER opt_type_argument_list
2724 $$ = new MemberAccess ((Expression) $1, (string) $3,
2725 (TypeArguments) $4, lexer.Location);
2733 invocation_expression
2734 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2737 Location l = lexer.Location;
2738 Report.Error (1, l, "Parse error");
2740 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2742 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2744 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2746 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2748 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2750 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2752 ArrayList args = new ArrayList (1);
2754 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2756 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2758 ArrayList args = ((ArrayList) $4);
2760 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2765 : /* empty */ { $$ = null; }
2772 ArrayList list = new ArrayList (4);
2776 | argument_list COMMA argument
2778 ArrayList list = (ArrayList) $1;
2782 | argument_list error {
2783 CheckToken (1026, yyToken, ", or ) expected");
2790 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2792 | non_simple_argument
2799 : REF variable_reference
2801 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2803 | OUT variable_reference
2805 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2807 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2809 ArrayList list = (ArrayList) $3;
2810 Argument[] args = new Argument [list.Count];
2811 list.CopyTo (args, 0);
2813 Expression expr = new Arglist (args, lexer.Location);
2814 $$ = new Argument (expr, Argument.AType.Expression);
2818 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2823 : expression { note ("section 5.4"); $$ = $1; }
2827 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2829 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2831 | primary_expression rank_specifiers
2833 // So the super-trick is that primary_expression
2834 // can only be either a SimpleName or a MemberAccess.
2835 // The MemberAccess case arises when you have a fully qualified type-name like :
2837 // SimpleName is when you have
2840 Expression expr = (Expression) $1;
2841 if (expr is ComposedCast){
2842 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2843 } else if (!(expr is SimpleName || expr is MemberAccess || expr is ConstructedType)){
2844 Error_ExpectingTypeName (lexer.Location, expr);
2845 $$ = TypeManager.system_object_expr;
2848 // So we extract the string corresponding to the SimpleName
2851 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2859 ArrayList list = new ArrayList (4);
2863 | expression_list COMMA expression
2865 ArrayList list = (ArrayList) $1;
2874 $$ = new This (current_block, lexer.Location);
2879 : BASE DOT IDENTIFIER
2881 $$ = new BaseAccess ((string) $3, lexer.Location);
2883 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2885 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2888 Report.Error (175, lexer.Location, "Use of keyword `base' is not valid in this context");
2893 post_increment_expression
2894 : primary_expression OP_INC
2896 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2897 (Expression) $1, lexer.Location);
2901 post_decrement_expression
2902 : primary_expression OP_DEC
2904 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2905 (Expression) $1, lexer.Location);
2910 : object_or_delegate_creation_expression
2911 | array_creation_expression
2914 object_or_delegate_creation_expression
2915 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2917 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2921 array_creation_expression
2922 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2924 opt_array_initializer
2926 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2928 | NEW type rank_specifiers array_initializer
2930 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2934 Report.Error (1031, lexer.Location, "Type expected");
2939 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2954 opt_rank_specifier_or_nullable
2963 | opt_nullable rank_specifiers
2970 | opt_nullable rank_specifiers INTERR
2973 $$ = "?" + $2 + "?";
2980 : rank_specifier opt_rank_specifier
2982 $$ = (string) $2 + (string) $1;
2987 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2989 $$ = "[" + (string) $2 + "]";
3009 | dim_separators COMMA
3011 $$ = (string) $1 + ",";
3015 opt_array_initializer
3027 : OPEN_BRACE CLOSE_BRACE
3029 ArrayList list = new ArrayList (4);
3032 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3034 $$ = (ArrayList) $2;
3038 variable_initializer_list
3039 : variable_initializer
3041 ArrayList list = new ArrayList (4);
3045 | variable_initializer_list COMMA variable_initializer
3047 ArrayList list = (ArrayList) $1;
3053 void_pointer_expression
3054 : void_pointer_expression STAR
3056 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3060 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3065 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
3067 $$ = new TypeOfVoid (lexer.Location);
3069 | TYPEOF OPEN_PARENS void_pointer_expression CLOSE_PARENS
3071 $$ = new TypeOf ((Expression) $3, lexer.Location);
3073 | TYPEOF OPEN_PARENS
3075 lexer.TypeOfParsing = true;
3079 lexer.TypeOfParsing = false;
3080 $$ = new TypeOf ((Expression) $4, lexer.Location);
3085 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3086 $$ = new SizeOf ((Expression) $3, lexer.Location);
3091 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3093 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
3097 unchecked_expression
3098 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3100 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
3104 pointer_member_access
3105 : primary_expression OP_PTR IDENTIFIER
3109 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
3110 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
3114 anonymous_method_expression
3115 : DELEGATE opt_anonymous_method_signature _mark_
3117 oob_stack.Push (current_local_parameters);
3118 current_local_parameters = (Parameters)$2;
3120 // Force the next block to be created as a ToplevelBlock
3121 oob_stack.Push (current_block);
3122 oob_stack.Push (top_current_block);
3123 current_block = null;
3127 Location loc = (Location) $3;
3128 top_current_block = (Block) oob_stack.Pop ();
3129 current_block = (Block) oob_stack.Pop ();
3130 if (RootContext.Version == LanguageVersion.ISO_1){
3131 Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
3134 ToplevelBlock anon_block = (ToplevelBlock) $5;
3136 anon_block.Parent = current_block;
3137 $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block,
3140 current_local_parameters = (Parameters) oob_stack.Pop ();
3144 opt_anonymous_method_signature
3145 : /* empty */ { $$ = null; }
3146 | anonymous_method_signature
3149 anonymous_method_signature
3150 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3153 $$ = Parameters.EmptyReadOnlyParameters;
3155 ArrayList par_list = (ArrayList) $2;
3156 Parameter [] pars = new Parameter [par_list.Count];
3157 par_list.CopyTo (pars);
3158 $$ = new Parameters (pars, null, lexer.Location);
3163 opt_anonymous_method_parameter_list
3164 : /* empty */ { $$ = null; }
3165 | anonymous_method_parameter_list { $$ = $1; }
3168 anonymous_method_parameter_list
3169 : anonymous_method_parameter
3171 ArrayList a = new ArrayList (4);
3175 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3177 ArrayList a = (ArrayList) $1;
3183 anonymous_method_parameter
3184 : opt_parameter_modifier type IDENTIFIER {
3185 $$ = new Parameter ((Expression) $2, (string) $3, (Parameter.Modifier) $1, null);
3187 | PARAMS type IDENTIFIER {
3188 Report.Error (-221, lexer.Location, "params modifier not allowed in anonymous method declaration");
3193 default_value_expression
3194 : DEFAULT_OPEN_PARENS type CLOSE_PARENS
3196 $$ = new DefaultValueExpression ((Expression) $2, lexer.Location);
3201 : primary_expression
3202 | BANG prefixed_unary_expression
3204 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
3206 | TILDE prefixed_unary_expression
3208 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
3214 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3216 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3218 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3220 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3226 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3228 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3233 // The idea to split this out is from Rhys' grammar
3234 // to solve the problem with casts.
3236 prefixed_unary_expression
3238 | PLUS prefixed_unary_expression
3240 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
3242 | MINUS prefixed_unary_expression
3244 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
3246 | OP_INC prefixed_unary_expression
3248 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3249 (Expression) $2, lexer.Location);
3251 | OP_DEC prefixed_unary_expression
3253 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3254 (Expression) $2, lexer.Location);
3256 | STAR prefixed_unary_expression
3258 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
3260 | BITWISE_AND prefixed_unary_expression
3262 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
3266 pre_increment_expression
3267 : OP_INC prefixed_unary_expression
3269 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3270 (Expression) $2, lexer.Location);
3274 pre_decrement_expression
3275 : OP_DEC prefixed_unary_expression
3277 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3278 (Expression) $2, lexer.Location);
3282 multiplicative_expression
3283 : prefixed_unary_expression
3284 | multiplicative_expression STAR prefixed_unary_expression
3286 $$ = new Binary (Binary.Operator.Multiply,
3287 (Expression) $1, (Expression) $3, lexer.Location);
3289 | multiplicative_expression DIV prefixed_unary_expression
3291 $$ = new Binary (Binary.Operator.Division,
3292 (Expression) $1, (Expression) $3, lexer.Location);
3294 | multiplicative_expression PERCENT prefixed_unary_expression
3296 $$ = new Binary (Binary.Operator.Modulus,
3297 (Expression) $1, (Expression) $3, lexer.Location);
3302 : multiplicative_expression
3303 | additive_expression PLUS multiplicative_expression
3305 $$ = new Binary (Binary.Operator.Addition,
3306 (Expression) $1, (Expression) $3, lexer.Location);
3308 | additive_expression MINUS multiplicative_expression
3310 $$ = new Binary (Binary.Operator.Subtraction,
3311 (Expression) $1, (Expression) $3, lexer.Location);
3316 : additive_expression
3317 | shift_expression OP_SHIFT_LEFT additive_expression
3319 $$ = new Binary (Binary.Operator.LeftShift,
3320 (Expression) $1, (Expression) $3, lexer.Location);
3322 | shift_expression OP_SHIFT_RIGHT additive_expression
3324 $$ = new Binary (Binary.Operator.RightShift,
3325 (Expression) $1, (Expression) $3, lexer.Location);
3333 lexer.PutbackNullable ();
3337 nullable_type_or_conditional
3344 relational_expression
3346 | relational_expression OP_LT shift_expression
3348 $$ = new Binary (Binary.Operator.LessThan,
3349 (Expression) $1, (Expression) $3, lexer.Location);
3351 | relational_expression OP_GT shift_expression
3353 $$ = new Binary (Binary.Operator.GreaterThan,
3354 (Expression) $1, (Expression) $3, lexer.Location);
3356 | relational_expression OP_LE shift_expression
3358 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3359 (Expression) $1, (Expression) $3, lexer.Location);
3361 | relational_expression OP_GE shift_expression
3363 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3364 (Expression) $1, (Expression) $3, lexer.Location);
3366 | relational_expression IS
3369 } nullable_type_or_conditional
3371 $$ = new Is ((Expression) $1, (Expression) $4, lexer.Location);
3373 | relational_expression AS
3376 } nullable_type_or_conditional
3378 $$ = new As ((Expression) $1, (Expression) $4, lexer.Location);
3383 : relational_expression
3384 | equality_expression OP_EQ relational_expression
3386 $$ = new Binary (Binary.Operator.Equality,
3387 (Expression) $1, (Expression) $3, lexer.Location);
3389 | equality_expression OP_NE relational_expression
3391 $$ = new Binary (Binary.Operator.Inequality,
3392 (Expression) $1, (Expression) $3, lexer.Location);
3397 : equality_expression
3398 | and_expression BITWISE_AND equality_expression
3400 $$ = new Binary (Binary.Operator.BitwiseAnd,
3401 (Expression) $1, (Expression) $3, lexer.Location);
3405 exclusive_or_expression
3407 | exclusive_or_expression CARRET and_expression
3409 $$ = new Binary (Binary.Operator.ExclusiveOr,
3410 (Expression) $1, (Expression) $3, lexer.Location);
3414 inclusive_or_expression
3415 : exclusive_or_expression
3416 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3418 $$ = new Binary (Binary.Operator.BitwiseOr,
3419 (Expression) $1, (Expression) $3, lexer.Location);
3423 conditional_and_expression
3424 : inclusive_or_expression
3425 | conditional_and_expression OP_AND inclusive_or_expression
3427 $$ = new Binary (Binary.Operator.LogicalAnd,
3428 (Expression) $1, (Expression) $3, lexer.Location);
3432 conditional_or_expression
3433 : conditional_and_expression
3434 | conditional_or_expression OP_OR conditional_and_expression
3436 $$ = new Binary (Binary.Operator.LogicalOr,
3437 (Expression) $1, (Expression) $3, lexer.Location);
3441 conditional_expression
3442 : conditional_or_expression
3443 | conditional_or_expression INTERR expression COLON expression
3445 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
3447 | conditional_or_expression INTERR INTERR expression
3449 $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $4, lexer.Location);
3451 // We'll be resolved into a `parenthesized_expression_0' later on.
3452 | conditional_or_expression INTERR CLOSE_PARENS
3454 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
3455 lexer.PutbackCloseParens ();
3459 assignment_expression
3460 : prefixed_unary_expression ASSIGN expression
3462 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
3464 | prefixed_unary_expression OP_MULT_ASSIGN expression
3466 Location l = lexer.Location;
3468 $$ = new CompoundAssign (
3469 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
3471 | prefixed_unary_expression OP_DIV_ASSIGN expression
3473 Location l = lexer.Location;
3475 $$ = new CompoundAssign (
3476 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
3478 | prefixed_unary_expression OP_MOD_ASSIGN expression
3480 Location l = lexer.Location;
3482 $$ = new CompoundAssign (
3483 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
3485 | prefixed_unary_expression OP_ADD_ASSIGN expression
3487 Location l = lexer.Location;
3489 $$ = new CompoundAssign (
3490 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
3492 | prefixed_unary_expression OP_SUB_ASSIGN expression
3494 Location l = lexer.Location;
3496 $$ = new CompoundAssign (
3497 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
3499 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3501 Location l = lexer.Location;
3503 $$ = new CompoundAssign (
3504 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
3506 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3508 Location l = lexer.Location;
3510 $$ = new CompoundAssign (
3511 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
3513 | prefixed_unary_expression OP_AND_ASSIGN expression
3515 Location l = lexer.Location;
3517 $$ = new CompoundAssign (
3518 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
3520 | prefixed_unary_expression OP_OR_ASSIGN expression
3522 Location l = lexer.Location;
3524 $$ = new CompoundAssign (
3525 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
3527 | prefixed_unary_expression OP_XOR_ASSIGN expression
3529 Location l = lexer.Location;
3531 $$ = new CompoundAssign (
3532 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
3537 : conditional_expression
3538 | assignment_expression
3558 MemberName name = MakeName ((MemberName) $5);
3559 bool partial = (bool) $3;
3560 int mod_flags = (int) $2;
3563 ClassPart part = PartialContainer.CreatePart (
3564 current_namespace, current_container, name, mod_flags,
3565 (Attributes) $1, Kind.Class, lexer.Location);
3567 current_container = part.PartialContainer;
3568 current_class = part;
3570 if ((mod_flags & Modifiers.STATIC) != 0) {
3571 current_class = new StaticClass (
3572 current_namespace, current_container, name,
3573 mod_flags, (Attributes) $1, lexer.Location);
3575 current_class = new Class (
3576 current_namespace, current_container, name,
3577 mod_flags, (Attributes) $1, lexer.Location);
3580 current_container = current_class;
3581 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
3584 lexer.ConstraintsParsing = true;
3587 opt_type_parameter_constraints_clauses
3589 lexer.ConstraintsParsing = false;
3592 if (current_class.Name == "System.Object") {
3593 Report.Error (537, current_class.Location,
3594 "The class System.Object cannot have a base " +
3595 "class or implement an interface.");
3597 current_class.Bases = (ArrayList) $7;
3600 current_class.SetParameterInfo ((ArrayList) $8);
3602 if (RootContext.Documentation != null) {
3603 current_class.DocComment = Lexer.consume_doc_comment ();
3604 Lexer.doc_state = XmlCommentState.Allowed;
3607 current_class.Register ();
3611 if (RootContext.Documentation != null)
3612 Lexer.doc_state = XmlCommentState.Allowed;
3618 current_container = current_container.Parent;
3619 current_class = current_container;
3625 { $$ = (bool) false; }
3627 { $$ = (bool) true; }
3631 : /* empty */ { $$ = (int) 0; }
3637 | modifiers modifier
3642 if ((m1 & m2) != 0) {
3643 Location l = lexer.Location;
3644 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3646 $$ = (int) (m1 | m2);
3651 : NEW { $$ = Modifiers.NEW; }
3652 | PUBLIC { $$ = Modifiers.PUBLIC; }
3653 | PROTECTED { $$ = Modifiers.PROTECTED; }
3654 | INTERNAL { $$ = Modifiers.INTERNAL; }
3655 | PRIVATE { $$ = Modifiers.PRIVATE; }
3656 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3657 | SEALED { $$ = Modifiers.SEALED; }
3658 | STATIC { $$ = Modifiers.STATIC; }
3659 | READONLY { $$ = Modifiers.READONLY; }
3660 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3661 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3662 | EXTERN { $$ = Modifiers.EXTERN; }
3663 | VOLATILE { $$ = Modifiers.VOLATILE; }
3664 | UNSAFE { $$ = Modifiers.UNSAFE; }
3668 : /* empty */ { $$ = null; }
3669 | class_base { $$ = $1; }
3673 : COLON type_list { $$ = $2; }
3676 opt_type_parameter_constraints_clauses
3677 : /* empty */ { $$ = null; }
3678 | type_parameter_constraints_clauses
3682 type_parameter_constraints_clauses
3683 : type_parameter_constraints_clause {
3684 ArrayList constraints = new ArrayList (1);
3685 constraints.Add ($1);
3688 | type_parameter_constraints_clauses type_parameter_constraints_clause {
3689 ArrayList constraints = (ArrayList) $1;
3691 constraints.Add ($2);
3696 type_parameter_constraints_clause
3697 : WHERE IDENTIFIER COLON type_parameter_constraints {
3698 $$ = new Constraints ((string) $2, (ArrayList) $4, lexer.Location);
3702 type_parameter_constraints
3703 : type_parameter_constraint {
3704 ArrayList constraints = new ArrayList (1);
3705 constraints.Add ($1);
3708 | type_parameter_constraints COMMA type_parameter_constraint {
3709 ArrayList constraints = (ArrayList) $1;
3711 constraints.Add ($3);
3716 type_parameter_constraint
3718 | NEW OPEN_PARENS CLOSE_PARENS {
3719 $$ = SpecialConstraint.Constructor;
3722 $$ = SpecialConstraint.ReferenceType;
3725 $$ = SpecialConstraint.ValueType;
3734 // A block is "contained" on the following places:
3736 // property_declaration as part of the accessor body (get/set)
3737 // operator_declaration
3738 // constructor_declaration
3739 // destructor_declaration
3740 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3745 if (current_block == null){
3746 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, lexer.Location);
3747 top_current_block = current_block;
3749 current_block = new Block (current_block, current_local_parameters,
3750 lexer.Location, Location.Null);
3753 opt_statement_list CLOSE_BRACE
3755 while (current_block.Implicit)
3756 current_block = current_block.Parent;
3758 current_block.SetEndLocation (lexer.Location);
3759 current_block = current_block.Parent;
3760 if (current_block == null)
3761 top_current_block = null;
3772 | statement_list statement
3776 : declaration_statement
3778 if ($1 != null && (Block) $1 != current_block){
3779 current_block.AddStatement ((Statement) $1);
3780 current_block = (Block) $1;
3783 | valid_declaration_statement
3785 current_block.AddStatement ((Statement) $1);
3790 valid_declaration_statement
3793 | expression_statement
3794 | selection_statement
3795 | iteration_statement
3799 | unchecked_statement
3807 : valid_declaration_statement
3808 | declaration_statement
3810 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3815 Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3823 $$ = EmptyStatement.Value;
3830 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3832 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3833 current_block.AddStatement (labeled);
3838 declaration_statement
3839 : local_variable_declaration SEMICOLON
3842 DictionaryEntry de = (DictionaryEntry) $1;
3844 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3848 | local_constant_declaration SEMICOLON
3851 DictionaryEntry de = (DictionaryEntry) $1;
3853 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3859 * The following is from Rhys' grammar:
3860 * > Types in local variable declarations must be recognized as
3861 * > expressions to prevent reduce/reduce errors in the grammar.
3862 * > The expressions are converted into types during semantic analysis.
3865 : primary_expression opt_rank_specifier_or_nullable
3867 // FIXME: Do something smart here regarding the composition of the type.
3869 // Ok, the above "primary_expression" is there to get rid of
3870 // both reduce/reduce and shift/reduces in the grammar, it should
3871 // really just be "type_name". If you use type_name, a reduce/reduce
3872 // creeps up. If you use namespace_or_type_name (which is all we need
3873 // really) two shift/reduces appear.
3876 // So the super-trick is that primary_expression
3877 // can only be either a SimpleName or a MemberAccess.
3878 // The MemberAccess case arises when you have a fully qualified type-name like :
3880 // SimpleName is when you have
3883 Expression expr = (Expression) $1;
3884 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType)) {
3885 Error_ExpectingTypeName (lexer.Location, expr);
3889 // So we extract the string corresponding to the SimpleName
3893 if ((string) $2 == "")
3896 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3899 | builtin_types opt_rank_specifier_or_nullable
3901 if ((string) $2 == "")
3904 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3908 local_variable_pointer_type
3909 : primary_expression STAR
3911 Expression expr = (Expression) $1;
3912 Location l = lexer.Location;
3914 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType)) {
3915 Error_ExpectingTypeName (l, expr);
3919 $$ = new ComposedCast ((Expression) $1, "*", l);
3921 | builtin_types STAR
3923 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3927 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3929 | local_variable_pointer_type STAR
3931 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3935 local_variable_declaration
3936 : local_variable_type variable_declarators
3939 $$ = new DictionaryEntry ($1, $2);
3943 | local_variable_pointer_type opt_rank_specifier_or_nullable variable_declarators
3948 if ((string) $2 == "")
3949 t = (Expression) $1;
3951 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3952 $$ = new DictionaryEntry (t, $3);
3958 local_constant_declaration
3959 : CONST local_variable_type constant_declarators
3962 $$ = new DictionaryEntry ($2, $3);
3968 expression_statement
3969 : statement_expression SEMICOLON
3976 // We have to do the wrapping here and not in the case above,
3977 // because statement_expression is used for example in for_statement
3979 statement_expression
3980 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3981 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3982 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3983 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3984 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3985 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3986 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3988 Report.Error (1002, lexer.Location, "Expecting `;'");
3993 object_creation_expression
3994 : object_or_delegate_creation_expression
3995 { note ("complain if this is a delegate maybe?"); }
4004 : IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS
4007 Location l = (Location) $3;
4009 $$ = new If ((Expression) $4, (Statement) $6, l);
4011 if (RootContext.WarningLevel >= 4){
4012 if ($6 == EmptyStatement.Value)
4013 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4017 | IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS
4018 embedded_statement ELSE embedded_statement
4020 Location l = (Location) $3;
4022 $$ = new If ((Expression) $4, (Statement) $6, (Statement) $8, l);
4027 : SWITCH OPEN_PARENS _mark_
4029 switch_stack.Push (current_block);
4031 expression CLOSE_PARENS
4034 $$ = new Switch ((Expression) $5, (ArrayList) $7, (Location) $3);
4035 current_block = (Block) switch_stack.Pop ();
4051 Report.Error (1522, lexer.Location, "Empty switch block");
4059 ArrayList sections = new ArrayList (4);
4064 | switch_sections switch_section
4066 ArrayList sections = (ArrayList) $1;
4076 current_block = current_block.CreateSwitchBlock (lexer.Location);
4080 Block topmost = current_block;
4082 while (topmost.Implicit)
4083 topmost = topmost.Parent;
4084 $$ = new SwitchSection ((ArrayList) $1, topmost);
4091 ArrayList labels = new ArrayList (4);
4096 | switch_labels switch_label
4098 ArrayList labels = (ArrayList) ($1);
4106 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
4107 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
4110 1523, lexer.Location,
4111 "The keyword case or default must precede code in switch block");
4123 : WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS embedded_statement
4125 Location l = (Location) $3;
4126 $$ = new While ((Expression) $4, (Statement) $6, l);
4128 if (RootContext.WarningLevel >= 4){
4129 if ($6 == EmptyStatement.Value)
4130 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4136 : DO embedded_statement
4137 WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS SEMICOLON
4139 Location l = (Location) $5;
4141 $$ = new Do ((Statement) $2, (Expression) $6, l);
4147 opt_for_initializer SEMICOLON _mark_
4149 Block assign_block = new Block (current_block);
4150 current_block = assign_block;
4152 if ($3 is DictionaryEntry){
4153 DictionaryEntry de = (DictionaryEntry) $3;
4155 Expression type = (Expression) de.Key;
4156 ArrayList var_declarators = (ArrayList) de.Value;
4158 foreach (VariableDeclaration decl in var_declarators){
4162 vi = current_block.AddVariable (
4163 type, decl.identifier, current_local_parameters, decl.Location);
4167 Location l = lexer.Location;
4169 if (decl.expression_or_array_initializer is Expression){
4170 expr = (Expression) decl.expression_or_array_initializer;
4171 } else if (decl.expression_or_array_initializer == null) {
4174 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4175 expr = new ArrayCreation (type, "", init, decl.Location);
4178 LocalVariableReference var;
4179 var = new LocalVariableReference (assign_block, decl.identifier, l);
4182 Assign a = new Assign (var, expr, decl.Location);
4184 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4188 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4189 // This can be referred to as $6 below.
4195 opt_for_condition SEMICOLON
4196 opt_for_iterator CLOSE_PARENS
4199 Location l = (Location) $5;
4201 For f = new For ((Statement) $6, (Expression) $7, (Statement) $9, (Statement) $11, l);
4203 if (RootContext.WarningLevel >= 4){
4204 if ($11 == EmptyStatement.Value)
4205 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4208 current_block.AddStatement (f);
4209 while (current_block.Implicit)
4210 current_block = current_block.Parent;
4212 current_block = current_block.Parent;
4217 : /* empty */ { $$ = EmptyStatement.Value; }
4222 : local_variable_declaration
4223 | statement_expression_list
4227 : /* empty */ { $$ = null; }
4228 | boolean_expression
4232 : /* empty */ { $$ = EmptyStatement.Value; }
4237 : statement_expression_list
4240 statement_expression_list
4241 : statement_expression
4243 // CHANGE: was `null'
4244 Block b = new Block (current_block, Block.Flags.Implicit);
4246 b.AddStatement ((Statement) $1);
4249 | statement_expression_list COMMA statement_expression
4251 Block b = (Block) $1;
4253 b.AddStatement ((Statement) $3);
4259 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4261 Report.Error (230, lexer.Location, "Type and identifier are both required in a foreach statement");
4264 | FOREACH OPEN_PARENS type IDENTIFIER IN _mark_
4265 expression CLOSE_PARENS
4267 Block foreach_block = new Block (current_block);
4268 current_block = foreach_block;
4270 Location l = lexer.Location;
4273 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
4277 // Get a writable reference to this read-only variable.
4279 // Note that the $$ here refers to the value of _this_ code block,
4280 // not the value of the LHS non-terminal. This can be referred to as $9 below.
4281 $$ = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
4288 LocalVariableReference v = (LocalVariableReference) $9;
4289 Location l = (Location) $6;
4292 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
4293 current_block.AddStatement (f);
4296 while (current_block.Implicit)
4297 current_block = current_block.Parent;
4299 current_block = current_block.Parent;
4305 | continue_statement
4315 $$ = new Break (lexer.Location);
4320 : CONTINUE SEMICOLON
4322 $$ = new Continue (lexer.Location);
4327 : GOTO IDENTIFIER SEMICOLON
4329 $$ = new Goto (current_block, (string) $2, lexer.Location);
4331 | GOTO CASE constant_expression SEMICOLON
4333 $$ = new GotoCase ((Expression) $3, lexer.Location);
4335 | GOTO DEFAULT SEMICOLON
4337 $$ = new GotoDefault (lexer.Location);
4342 : RETURN opt_expression SEMICOLON
4344 $$ = new Return ((Expression) $2, lexer.Location);
4349 : THROW opt_expression SEMICOLON
4351 $$ = new Throw ((Expression) $2, lexer.Location);
4356 : IDENTIFIER RETURN expression SEMICOLON
4358 string s = (string) $1;
4360 Report.Error (1003, lexer.Location, "; expected");
4363 if (RootContext.Version == LanguageVersion.ISO_1){
4364 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4367 if (iterator_container == null){
4368 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4371 iterator_container.SetYields ();
4372 $$ = new Yield ((Expression) $3, lexer.Location);
4375 | IDENTIFIER BREAK SEMICOLON
4377 string s = (string) $1;
4379 Report.Error (1003, lexer.Location, "; expected");
4382 if (RootContext.Version == LanguageVersion.ISO_1){
4383 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4386 if (iterator_container == null){
4387 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4390 iterator_container.SetYields ();
4391 $$ = new YieldBreak (lexer.Location);
4402 : TRY block catch_clauses
4406 ArrayList c = (ArrayList)$3;
4407 for (int i = 0; i < c.Count; ++i) {
4408 Catch cc = (Catch) c [i];
4410 if (i != c.Count - 1)
4411 Report.Error (1017, cc.loc, "Empty catch block must be the last in a series of catch blocks");
4418 // Now s contains the list of specific catch clauses
4419 // and g contains the general one.
4421 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4423 | TRY block opt_catch_clauses FINALLY block
4426 ArrayList s = new ArrayList (4);
4427 ArrayList catch_list = (ArrayList) $3;
4429 if (catch_list != null){
4430 foreach (Catch cc in catch_list) {
4438 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4442 Report.Error (1524, lexer.Location, "Expected catch or finally");
4447 : /* empty */ { $$ = null; }
4454 ArrayList l = new ArrayList (4);
4459 | catch_clauses catch_clause
4461 ArrayList l = (ArrayList) $1;
4469 : /* empty */ { $$ = null; }
4474 : CATCH opt_catch_args
4476 Expression type = null;
4480 DictionaryEntry cc = (DictionaryEntry) $2;
4481 type = (Expression) cc.Key;
4482 id = (string) cc.Value;
4485 ArrayList one = new ArrayList (4);
4486 Location loc = lexer.Location;
4488 one.Add (new VariableDeclaration (id, null, loc));
4490 current_block = new Block (current_block);
4491 Block b = declare_local_variables (type, one, loc);
4496 Expression type = null;
4500 DictionaryEntry cc = (DictionaryEntry) $2;
4501 type = (Expression) cc.Key;
4502 id = (string) cc.Value;
4505 while (current_block.Implicit)
4506 current_block = current_block.Parent;
4507 current_block = current_block.Parent;
4511 $$ = new Catch (type, id, (Block) $4, ((Block) $4).loc);
4516 : /* empty */ { $$ = null; }
4521 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4523 $$ = new DictionaryEntry ($2, $3);
4530 $$ = new Checked ((Block) $2);
4537 $$ = new Unchecked ((Block) $2);
4544 if (!RootContext.Unsafe){
4545 Report.Error (227, lexer.Location,
4546 "Unsafe code can only be used if --unsafe is used");
4549 $$ = new Unsafe ((Block) $3);
4555 type fixed_pointer_declarators
4558 ArrayList list = (ArrayList) $4;
4559 Expression type = (Expression) $3;
4560 Location l = lexer.Location;
4561 int top = list.Count;
4563 Block assign_block = new Block (current_block);
4564 current_block = assign_block;
4566 for (int i = 0; i < top; i++){
4567 Pair p = (Pair) list [i];
4570 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
4582 Location l = (Location) $6;
4584 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $8, l);
4586 if (RootContext.WarningLevel >= 4){
4587 if ($8 == EmptyStatement.Value)
4588 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4591 current_block.AddStatement (f);
4592 while (current_block.Implicit)
4593 current_block = current_block.Parent;
4595 current_block = current_block.Parent;
4599 fixed_pointer_declarators
4600 : fixed_pointer_declarator {
4601 ArrayList declarators = new ArrayList (4);
4603 declarators.Add ($1);
4606 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4608 ArrayList declarators = (ArrayList) $1;
4610 declarators.Add ($3);
4615 fixed_pointer_declarator
4616 : IDENTIFIER ASSIGN expression
4618 $$ = new Pair ($1, $3);
4622 Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
4628 : LOCK OPEN_PARENS expression CLOSE_PARENS
4634 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4639 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS _mark_
4641 Block assign_block = new Block (current_block);
4642 current_block = assign_block;
4644 if ($3 is DictionaryEntry){
4645 DictionaryEntry de = (DictionaryEntry) $3;
4646 Location l = lexer.Location;
4648 Expression type = (Expression) de.Key;
4649 ArrayList var_declarators = (ArrayList) de.Value;
4651 ArrayList vars = new ArrayList (4);
4653 foreach (VariableDeclaration decl in var_declarators){
4655 LocalInfo vi = current_block.AddVariable (
4656 type, decl.identifier,
4657 current_local_parameters, decl.Location);
4663 if (decl.expression_or_array_initializer is Expression){
4664 expr = (Expression) decl.expression_or_array_initializer;
4666 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4668 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4671 expr = new ArrayCreation (type, "", init, decl.Location);
4674 LocalVariableReference var;
4676 // Get a writable reference to this read-only variable.
4677 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4679 // This is so that it is not a warning on using variables
4682 vars.Add (new DictionaryEntry (var, expr));
4684 // Assign a = new Assign (var, expr, decl.Location);
4685 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4688 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4689 // It can be referred to as $6 below.
4690 $$ = new DictionaryEntry (type, vars);
4697 Using u = new Using ($6, (Statement) $7, (Location) $5);
4698 current_block.AddStatement (u);
4699 while (current_block.Implicit)
4700 current_block = current_block.Parent;
4702 current_block = current_block.Parent;
4706 resource_acquisition
4707 : local_variable_declaration
4711 // Utility rule to save location information
4714 { $$ = lexer.Location; }
4719 // A class used to pass around variable declarations and constants
4721 public class VariableDeclaration {
4722 public string identifier;
4723 public object expression_or_array_initializer;
4724 public Location Location;
4725 public Attributes OptAttributes;
4726 public string DocComment;
4728 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4730 this.identifier = id;
4731 this.expression_or_array_initializer = eoai;
4733 this.OptAttributes = opt_attrs;
4736 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4742 /// Used to pass around interface property information
4744 public class InterfaceAccessorInfo {
4746 public readonly Accessor Get, Set;
4748 public InterfaceAccessorInfo (bool has_get, bool has_set,
4749 Attributes get_attrs, Attributes set_attrs, int get_mod, int set_mod, Location get_loc, Location set_loc)
4752 Report.Error (275, get_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4754 Report.Error (275, set_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4757 Get = new Accessor (null, 0, get_attrs, get_loc);
4759 Set = new Accessor (null, 0, set_attrs, set_loc);
4766 // A class used to hold info about an indexer declarator
4768 public class IndexerDeclaration {
4769 public Expression type;
4770 public MemberName interface_type;
4771 public Parameters param_list;
4773 public IndexerDeclaration (Expression type, MemberName interface_type,
4774 Parameters param_list)
4777 this.interface_type = interface_type;
4778 this.param_list = param_list;
4783 // We use this when we do not have an object in advance that is an IIteratorContainer
4785 public class SimpleIteratorContainer : IIteratorContainer {
4788 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4793 public static SimpleIteratorContainer GetSimple () {
4794 Simple.Yields = false;
4798 public void SetYields () { Yields = true; }
4802 // A class used to hold info about an operator declarator
4804 public class OperatorDeclaration {
4805 public Operator.OpType optype;
4806 public Expression ret_type, arg1type, arg2type;
4807 public string arg1name, arg2name;
4808 public Location location;
4810 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4811 Expression arg1type, string arg1name,
4812 Expression arg2type, string arg2name, Location location)
4815 this.ret_type = ret_type;
4816 this.arg1type = arg1type;
4817 this.arg1name = arg1name;
4818 this.arg2type = arg2type;
4819 this.arg2name = arg2name;
4820 this.location = location;
4825 void Error_ExpectingTypeName (Location l, Expression expr)
4827 if (expr is Invocation){
4828 Report.Error (1002, l, "; expected");
4830 Report.Error (-1, l, "Invalid Type definition");
4835 // Given the @class_name name, it creates a fully qualified name
4836 // based on the containing declaration space
4839 MakeName (MemberName class_name)
4841 string ns = current_namespace.FullName;
4843 if (current_container.Name == ""){
4845 return new MemberName (new MemberName (ns), class_name);
4849 return new MemberName (current_container.MemberName, class_name);
4853 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4855 Block implicit_block;
4856 ArrayList inits = null;
4859 // We use the `Used' property to check whether statements
4860 // have been added to the current block. If so, we need
4861 // to create another block to contain the new declaration
4862 // otherwise, as an optimization, we use the same block to
4863 // add the declaration.
4865 // FIXME: A further optimization is to check if the statements
4866 // that were added were added as part of the initialization
4867 // below. In which case, no other statements have been executed
4868 // and we might be able to reduce the number of blocks for
4869 // situations like this:
4871 // int j = 1; int k = j + 1;
4873 if (current_block.Used)
4874 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4876 implicit_block = current_block;
4878 foreach (VariableDeclaration decl in variable_declarators){
4880 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4881 if (decl.expression_or_array_initializer != null){
4883 inits = new ArrayList (4);
4890 return implicit_block;
4892 foreach (VariableDeclaration decl in inits){
4896 if (decl.expression_or_array_initializer is Expression){
4897 expr = (Expression) decl.expression_or_array_initializer;
4900 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4902 expr = new ArrayCreation (type, "", init, decl.Location);
4905 LocalVariableReference var;
4906 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4908 assign = new Assign (var, expr, decl.Location);
4910 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4913 return implicit_block;
4916 Block declare_local_constants (Expression type, ArrayList declarators)
4918 Block implicit_block;
4920 if (current_block.Used)
4921 implicit_block = new Block (current_block, Block.Flags.Implicit);
4923 implicit_block = current_block;
4925 foreach (VariableDeclaration decl in declarators){
4926 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4927 current_local_parameters, decl.Location);
4930 return implicit_block;
4933 void CheckAttributeTarget (string a)
4937 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4941 Location l = lexer.Location;
4942 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4948 void CheckUnaryOperator (Operator.OpType op)
4952 case Operator.OpType.LogicalNot:
4953 case Operator.OpType.OnesComplement:
4954 case Operator.OpType.Increment:
4955 case Operator.OpType.Decrement:
4956 case Operator.OpType.True:
4957 case Operator.OpType.False:
4958 case Operator.OpType.Addition:
4959 case Operator.OpType.Subtraction:
4964 Location l = lexer.Location;
4965 Report.Error (1019, l, "Overloadable unary operator expected");
4971 void CheckBinaryOperator (Operator.OpType op)
4975 case Operator.OpType.Addition:
4976 case Operator.OpType.Subtraction:
4977 case Operator.OpType.Multiply:
4978 case Operator.OpType.Division:
4979 case Operator.OpType.Modulus:
4980 case Operator.OpType.BitwiseAnd:
4981 case Operator.OpType.BitwiseOr:
4982 case Operator.OpType.ExclusiveOr:
4983 case Operator.OpType.LeftShift:
4984 case Operator.OpType.RightShift:
4985 case Operator.OpType.Equality:
4986 case Operator.OpType.Inequality:
4987 case Operator.OpType.GreaterThan:
4988 case Operator.OpType.LessThan:
4989 case Operator.OpType.GreaterThanOrEqual:
4990 case Operator.OpType.LessThanOrEqual:
4994 Location l = lexer.Location;
4995 Report.Error (1020, l, "Overloadable binary operator expected");
5001 void syntax_error (Location l, string msg)
5003 Report.Error (1003, l, "Syntax error, " + msg);
5006 void output (string s)
5008 Console.WriteLine (s);
5011 void note (string s)
5013 // Used to put annotations
5018 public Tokenizer Lexer {
5024 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
5026 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
5027 this.name = file.Name;
5029 current_container = RootContext.Tree.Types;
5030 current_container.NamespaceEntry = current_namespace;
5031 oob_stack = new Stack ();
5032 switch_stack = new Stack ();
5034 lexer = new Tokenizer (reader, file, defines);
5037 public void parse ()
5040 if (yacc_verbose_flag > 1)
5041 yyparse (lexer, new yydebug.yyDebugSimple ());
5044 Tokenizer tokenizer = lexer as Tokenizer;
5045 tokenizer.cleanup ();
5047 } catch (Exception e){
5049 // Removed for production use, use parser verbose to get the output.
5051 // Console.WriteLine (e);
5052 Report.Error (-25, lexer.Location, "Parsing error");
5053 if (yacc_verbose_flag > 0)
5054 Console.WriteLine (e);
5059 void CheckToken (int error, int yyToken, string msg)
5061 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
5062 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
5065 Report.Error (error, lexer.Location, msg);
5068 void CheckIdentifierToken (int yyToken)
5070 CheckToken (1041, yyToken, "Identifier expected");
5073 string ConsumeStoredComment ()
5075 string s = tmpComment;
5077 Lexer.doc_state = XmlCommentState.Allowed;