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;
100 bool has_get, has_set;
105 %token NONE /* This token is never returned by our lexer */
106 %token ERROR // This is used not by the parser, but by the tokenizer.
110 *These are the C# keywords
197 /* C# keywords which are not really keywords */
203 /* C# single character operators/punctuation. */
204 %token OPEN_BRACE "{"
205 %token CLOSE_BRACE "}"
206 %token OPEN_BRACKET "["
207 %token CLOSE_BRACKET "]"
208 %token OPEN_PARENS "("
209 %token CLOSE_PARENS ")"
221 %token OP_GENERICS_LT "<"
223 %token OP_GENERICS_GT ">"
224 %token BITWISE_AND "&"
225 %token BITWISE_OR "|"
232 /* C# multi-character operators. */
235 %token OP_SHIFT_LEFT "<<"
236 %token OP_SHIFT_RIGHT ">>"
243 %token OP_MULT_ASSIGN "*="
244 %token OP_DIV_ASSIGN "/="
245 %token OP_MOD_ASSIGN "%="
246 %token OP_ADD_ASSIGN "+="
247 %token OP_SUB_ASSIGN "-="
248 %token OP_SHIFT_LEFT_ASSIGN "<<="
249 %token OP_SHIFT_RIGHT_ASSIGN ">>="
250 %token OP_AND_ASSIGN "&="
251 %token OP_XOR_ASSIGN "^="
252 %token OP_OR_ASSIGN "|="
256 %token LITERAL_INTEGER "int literal"
257 %token LITERAL_FLOAT "float literal"
258 %token LITERAL_DOUBLE "double literal"
259 %token LITERAL_DECIMAL "decimal literal"
260 %token LITERAL_CHARACTER "character literal"
261 %token LITERAL_STRING "string literal"
264 %token CLOSE_PARENS_CAST
265 %token CLOSE_PARENS_NO_CAST
266 %token CLOSE_PARENS_OPEN_PARENS
267 %token CLOSE_PARENS_MINUS
268 %token DEFAULT_OPEN_PARENS
269 %token GENERIC_DIMENSION
271 /* Add precedence rules to solve dangling else s/r conflict */
280 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
282 %left STAR DIV PERCENT
283 %right BANG CARRET UMINUS
284 %nonassoc OP_INC OP_DEC
286 %left OPEN_BRACKET OPEN_BRACE
290 %start compilation_unit
294 : outer_declarations opt_EOF
295 | outer_declarations global_attributes opt_EOF
296 | global_attributes opt_EOF
297 | opt_EOF /* allow empty files */
303 Lexer.check_incorrect_doc_comment ();
307 Lexer.check_incorrect_doc_comment ();
313 | outer_declarations outer_declaration
318 | namespace_member_declaration
323 | using_directives using_directive
327 : using_alias_directive
329 if (RootContext.Documentation != null)
330 Lexer.doc_state = XmlCommentState.Allowed;
332 | using_namespace_directive
334 if (RootContext.Documentation != null)
335 Lexer.doc_state = XmlCommentState.Allowed;
339 using_alias_directive
340 : USING IDENTIFIER ASSIGN
341 namespace_or_type_name SEMICOLON
343 MemberName name = (MemberName) $4;
344 current_namespace.UsingAlias ((string) $2, name.GetTypeExpression (lexer.Location), lexer.Location);
347 CheckIdentifierToken (yyToken);
351 using_namespace_directive
352 : USING namespace_name SEMICOLON
354 MemberName ns_name = (MemberName) $2;
355 current_namespace.Using (ns_name.GetTypeExpression (lexer.Location), lexer.Location);
360 // Strictly speaking, namespaces don't have attributes but
361 // we parse global attributes along with namespace declarations and then
364 namespace_declaration
365 : opt_attributes NAMESPACE namespace_or_type_name
368 Report.Error(1671, Lexer.Location, "A namespace declaration cannot have modifiers or attributes");
371 MemberName name = (MemberName) $3;
373 if (name.TypeArguments != null)
374 syntax_error (lexer.Location, "namespace name expected");
376 current_namespace = new NamespaceEntry (
377 current_namespace, file, name.GetName (), lexer.Location);
379 namespace_body opt_semicolon
381 current_namespace = current_namespace.Parent;
396 : namespace_or_type_name {
397 MemberName name = (MemberName) $1;
399 if (name.TypeArguments != null)
400 syntax_error (lexer.Location, "namespace name expected");
409 if (RootContext.Documentation != null)
410 Lexer.doc_state = XmlCommentState.Allowed;
413 opt_namespace_member_declarations
424 opt_namespace_member_declarations
426 | namespace_member_declarations
429 namespace_member_declarations
430 : namespace_member_declaration
431 | namespace_member_declarations namespace_member_declaration
434 namespace_member_declaration
441 Class c = (Class) $1;
442 mod_flags = c.ModFlags;
444 } else if ($1 is Struct){
445 Struct s = (Struct) $1;
446 mod_flags = s.ModFlags;
451 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
453 1527, lexer.Location,
454 "Namespace elements cant be explicitly " +
455 "declared private or protected in `" + name + "'");
457 current_namespace.DeclarationFound = true;
459 | namespace_declaration {
460 current_namespace.DeclarationFound = true;
463 | field_declaration {
464 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
466 | method_declaration {
467 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
474 | interface_declaration
476 | delegate_declaration
478 // Enable this when we have handled all errors, because this acts as a generic fallback
481 // Console.WriteLine ("Token=" + yyToken);
482 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
494 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
502 global_attrs_enabled = false;
507 global_attrs_enabled = false;
516 ArrayList sect = (ArrayList) $1;
518 if (global_attrs_enabled) {
519 if (current_attr_target == "module") {
520 CodeGen.Module.AddAttributes (sect);
522 } else if (current_attr_target == "assembly") {
523 CodeGen.Assembly.AddAttributes (sect);
526 $$ = new Attributes (sect);
529 if (RootContext.Documentation != null) {
530 Lexer.check_incorrect_doc_comment ();
532 XmlCommentState.Allowed;
536 $$ = new Attributes (sect);
538 current_attr_target = null;
540 | attribute_sections attribute_section
542 Attributes attrs = $1 as Attributes;
543 ArrayList sect = (ArrayList) $2;
545 if (global_attrs_enabled) {
546 if (current_attr_target == "module") {
547 CodeGen.Module.AddAttributes (sect);
549 } else if (current_attr_target == "assembly") {
550 CodeGen.Assembly.AddAttributes (sect);
554 attrs = new Attributes (sect);
556 attrs.AddAttributes (sect);
560 attrs = new Attributes (sect);
562 attrs.AddAttributes (sect);
565 current_attr_target = null;
570 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
574 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
580 attribute_target_specifier
581 : attribute_target COLON
583 current_attr_target = (string)$1;
591 CheckAttributeTarget ((string) $1);
594 | EVENT { $$ = "event"; }
595 | RETURN { $$ = "return"; }
601 ArrayList attrs = new ArrayList (4);
607 | attribute_list COMMA attribute
609 ArrayList attrs = (ArrayList) $1;
621 opt_attribute_arguments
623 Location loc = (Location) $2;
624 MemberName mname = (MemberName) $1;
625 if (mname.IsGeneric) {
626 Report.Error (404, lexer.Location,
627 "'<' unexpected: attributes cannot be generic");
630 MemberName left = mname.Left;
631 string identifier = mname.Name;
633 Expression left_expr = left == null ? null : left.GetTypeExpression (loc);
635 if (current_attr_target == "assembly" || current_attr_target == "module")
636 $$ = new GlobalAttribute (current_container, current_attr_target,
637 left_expr, identifier, (ArrayList) $3, loc);
639 $$ = new Attribute (current_attr_target, left_expr, identifier, (ArrayList) $3, loc);
644 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
647 opt_attribute_arguments
648 : /* empty */ { $$ = null; }
649 | OPEN_PARENS attribute_arguments CLOSE_PARENS
657 : opt_positional_argument_list
662 ArrayList args = new ArrayList (4);
668 | positional_argument_list COMMA named_argument_list
670 ArrayList args = new ArrayList (4);
676 | named_argument_list
678 ArrayList args = new ArrayList (4);
687 opt_positional_argument_list
688 : /* empty */ { $$ = null; }
689 | positional_argument_list
692 positional_argument_list
695 ArrayList args = new ArrayList (4);
696 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
700 | positional_argument_list COMMA expression
702 ArrayList args = (ArrayList) $1;
703 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
712 ArrayList args = new ArrayList (4);
717 | named_argument_list COMMA named_argument
719 ArrayList args = (ArrayList) $1;
724 | named_argument_list COMMA expression
726 Report.Error (1016, lexer.Location, "Named attribute argument expected");
732 : IDENTIFIER ASSIGN expression
734 $$ = new DictionaryEntry (
736 new Argument ((Expression) $3, Argument.AType.Expression));
742 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
745 opt_class_member_declarations
747 | class_member_declarations
750 class_member_declarations
751 : class_member_declaration
752 | class_member_declarations
753 class_member_declaration
756 class_member_declaration
757 : constant_declaration // done
758 | field_declaration // done
759 | method_declaration // done
760 | property_declaration // done
761 | event_declaration // done
762 | indexer_declaration // done
763 | operator_declaration // done
764 | constructor_declaration // done
765 | destructor_declaration // done
775 MemberName name = MakeName ((MemberName) $5);
776 bool partial = (bool) $3;
779 ClassPart part = PartialContainer.CreatePart (
780 current_namespace, current_class, name, (int) $2,
781 (Attributes) $1, Kind.Struct, lexer.Location);
783 current_container = part.PartialContainer;
784 current_class = part;
786 current_class = new Struct (
787 current_namespace, current_class, name, (int) $2,
788 (Attributes) $1, lexer.Location);
790 current_container.AddClassOrStruct (current_class);
791 current_container = current_class;
792 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
795 lexer.ConstraintsParsing = true;
798 opt_type_parameter_constraints_clauses
800 lexer.ConstraintsParsing = false;
803 current_class.Bases = (ArrayList) $7;
805 current_class.SetParameterInfo ((ArrayList) $8);
807 if (RootContext.Documentation != null)
808 current_class.DocComment = Lexer.consume_doc_comment ();
812 if (RootContext.Documentation != null)
813 Lexer.doc_state = XmlCommentState.Allowed;
817 $$ = pop_current_class ();
819 | opt_attributes opt_modifiers opt_partial STRUCT error {
820 CheckIdentifierToken (yyToken);
827 if (RootContext.Documentation != null)
828 Lexer.doc_state = XmlCommentState.Allowed;
830 opt_struct_member_declarations CLOSE_BRACE
833 opt_struct_member_declarations
835 | struct_member_declarations
838 struct_member_declarations
839 : struct_member_declaration
840 | struct_member_declarations struct_member_declaration
843 struct_member_declaration
844 : constant_declaration
847 | property_declaration
849 | indexer_declaration
850 | operator_declaration
851 | constructor_declaration
855 * This is only included so we can flag error 575:
856 * destructors only allowed on class types
858 | destructor_declaration
869 int modflags = (int) $2;
870 foreach (VariableDeclaration constant in (ArrayList) $5){
871 Location l = constant.Location;
872 if ((modflags & Modifiers.STATIC) != 0) {
873 Report.Error (504, l, "The constant '{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
877 Const c = new Const (
878 current_class, (Expression) $4, (string) constant.identifier,
879 (Expression) constant.expression_or_array_initializer, modflags,
882 if (RootContext.Documentation != null) {
883 c.DocComment = Lexer.consume_doc_comment ();
884 Lexer.doc_state = XmlCommentState.Allowed;
886 current_container.AddConstant (c);
892 : constant_declarator
894 ArrayList constants = new ArrayList (4);
899 | constant_declarators COMMA constant_declarator
902 ArrayList constants = (ArrayList) $1;
909 : IDENTIFIER ASSIGN constant_expression
911 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
915 // A const field requires a value to be provided
916 Report.Error (145, lexer.Location, "A const field requires a value to be provided");
928 Expression type = (Expression) $3;
931 foreach (VariableDeclaration var in (ArrayList) $4){
932 Field field = new Field (current_class, type, mod, var.identifier,
933 var.expression_or_array_initializer,
934 (Attributes) $1, var.Location);
936 if (RootContext.Documentation != null) {
937 field.DocComment = Lexer.consume_doc_comment ();
938 Lexer.doc_state = XmlCommentState.Allowed;
940 current_container.AddField (field);
947 fixed_variable_declarators
950 Expression type = (Expression) $4;
953 foreach (VariableDeclaration var in (ArrayList) $5) {
954 FixedField field = new FixedField (current_class, type, mod, var.identifier,
955 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
957 if (RootContext.Documentation != null) {
958 field.DocComment = Lexer.consume_doc_comment ();
959 Lexer.doc_state = XmlCommentState.Allowed;
961 current_container.AddField (field);
969 Report.Error (670, lexer.Location, "void type is not allowed for fields");
973 fixed_variable_declarators
974 : fixed_variable_declarator
976 ArrayList decl = new ArrayList (2);
980 | fixed_variable_declarators COMMA fixed_variable_declarator
982 ArrayList decls = (ArrayList) $1;
988 fixed_variable_declarator
989 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
991 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
996 : variable_declarator
998 ArrayList decl = new ArrayList (4);
1002 | variable_declarators COMMA variable_declarator
1004 ArrayList decls = (ArrayList) $1;
1011 : IDENTIFIER ASSIGN variable_initializer
1013 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
1017 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
1019 | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
1021 Report.Error (650, lexer.Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
1022 "To declare a fixed buffer field, use the fixed keyword before the field type");
1026 variable_initializer
1035 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
1037 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
1041 Report.Error (1575, lexer.Location, "A stackalloc expression requires [] after type");
1048 iterator_container = (IIteratorContainer) $1;
1049 if (RootContext.Documentation != null)
1050 Lexer.doc_state = XmlCommentState.NotAllowed;
1054 Method method = (Method) $1;
1055 Block b = (Block) $3;
1056 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
1059 if ((method.ModFlags & extern_abstract) == 0){
1061 501, lexer.Location, current_container.MakeName (method.Name) +
1062 "must declare a body because it is not marked abstract or extern");
1065 if ((method.ModFlags & Modifiers.EXTERN) != 0){
1067 179, lexer.Location, current_container.MakeName (method.Name) +
1068 " is declared extern, but has a body");
1072 method.Block = (ToplevelBlock) $3;
1073 current_container.AddMethod (method);
1075 current_local_parameters = null;
1076 iterator_container = null;
1078 if (RootContext.Documentation != null)
1079 Lexer.doc_state = XmlCommentState.Allowed;
1086 type namespace_or_type_name
1087 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1089 lexer.ConstraintsParsing = true;
1091 opt_type_parameter_constraints_clauses
1093 lexer.ConstraintsParsing = false;
1095 MemberName name = (MemberName) $4;
1097 if ($9 != null && name.TypeArguments == null)
1098 Report.Error (80, lexer.Location,
1099 "Contraints are not allowed on non-generic declarations");
1103 GenericMethod generic = null;
1104 if (name.TypeArguments != null) {
1105 generic = new GenericMethod (current_namespace, current_class,
1106 name, lexer.Location);
1108 generic.SetParameterInfo ((ArrayList) $9);
1111 method = new Method (current_class, generic, (Expression) $3, (int) $2, false,
1112 name, (Parameters) $6, (Attributes) $1, lexer.Location);
1114 current_local_parameters = (Parameters) $6;
1116 if (RootContext.Documentation != null)
1117 method.DocComment = Lexer.consume_doc_comment ();
1123 VOID namespace_or_type_name
1124 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1126 lexer.ConstraintsParsing = true;
1128 opt_type_parameter_constraints_clauses
1130 lexer.ConstraintsParsing = false;
1132 MemberName name = (MemberName) $4;
1134 if ($9 != null && name.TypeArguments == null)
1135 Report.Error (80, lexer.Location,
1136 "Contraints are not allowed on non-generic declarations");
1139 GenericMethod generic = null;
1140 if (name.TypeArguments != null) {
1141 generic = new GenericMethod (current_namespace, current_class,
1142 name, lexer.Location);
1144 generic.SetParameterInfo ((ArrayList) $9);
1147 method = new Method (current_class, generic, TypeManager.system_void_expr,
1148 (int) $2, false, name, (Parameters) $6, (Attributes) $1,
1151 current_local_parameters = (Parameters) $6;
1153 if (RootContext.Documentation != null)
1154 method.DocComment = Lexer.consume_doc_comment ();
1161 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1163 Report.Error (1585, lexer.Location,
1164 String.Format ("Modifier {0} should appear before type",
1165 Modifiers.Name ((int) $4)));
1166 MemberName name = (MemberName) $4;
1168 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1169 0, false, name, (Parameters) $6, (Attributes) $1,
1172 current_local_parameters = (Parameters) $6;
1174 if (RootContext.Documentation != null)
1175 method.DocComment = Lexer.consume_doc_comment ();
1183 | SEMICOLON { $$ = null; }
1186 opt_formal_parameter_list
1187 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1188 | formal_parameter_list
1191 formal_parameter_list
1194 ArrayList pars_list = (ArrayList) $1;
1196 Parameter [] pars = new Parameter [pars_list.Count];
1197 pars_list.CopyTo (pars);
1199 $$ = new Parameters (pars, null, lexer.Location);
1201 | fixed_parameters COMMA parameter_array
1203 ArrayList pars_list = (ArrayList) $1;
1205 Parameter [] pars = new Parameter [pars_list.Count];
1206 pars_list.CopyTo (pars);
1208 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
1210 | fixed_parameters COMMA ARGLIST
1212 ArrayList pars_list = (ArrayList) $1;
1214 Parameter [] pars = new Parameter [pars_list.Count];
1215 pars_list.CopyTo (pars);
1217 $$ = new Parameters (pars, true, lexer.Location);
1221 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
1225 $$ = new Parameters (null, true, lexer.Location);
1232 ArrayList pars = new ArrayList (4);
1237 | fixed_parameters COMMA fixed_parameter
1239 ArrayList pars = (ArrayList) $1;
1248 opt_parameter_modifier
1252 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
1255 opt_parameter_modifier
1258 Report.Error (1001, lexer.Location, "Identifier expected");
1262 opt_parameter_modifier
1265 CheckIdentifierToken (yyToken);
1269 opt_parameter_modifier
1275 Report.Error (241, lexer.Location, "Default parameter specifiers are not permitted");
1280 opt_parameter_modifier
1281 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1282 | parameter_modifier
1286 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1287 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1291 : opt_attributes PARAMS type IDENTIFIER
1293 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1294 note ("type must be a single-dimension array type");
1296 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1298 Report.Error (1611, lexer.Location, "The params parameter cannot be declared as ref or out");
1301 | opt_attributes PARAMS type error {
1302 CheckIdentifierToken (yyToken);
1307 property_declaration
1311 namespace_or_type_name
1313 if (RootContext.Documentation != null)
1314 tmpComment = Lexer.consume_doc_comment ();
1318 implicit_value_parameter_type = (Expression) $3;
1320 lexer.PropertyParsing = true;
1322 $$ = lexer.Location;
1324 iterator_container = SimpleIteratorContainer.GetSimple ();
1326 accessor_declarations
1328 lexer.PropertyParsing = false;
1329 has_get = has_set = false;
1337 Pair pair = (Pair) $8;
1338 Accessor get_block = (Accessor) pair.First;
1339 Accessor set_block = (Accessor) pair.Second;
1341 Location loc = (Location) $7;
1342 MemberName name = (MemberName) $4;
1344 if (name.TypeArguments != null)
1345 syntax_error (lexer.Location, "a property can't have type arguments");
1347 prop = new Property (current_container, (Expression) $3, (int) $2, false,
1348 name, (Attributes) $1, get_block, set_block, loc);
1349 if (SimpleIteratorContainer.Simple.Yields)
1352 current_container.AddProperty (prop);
1353 implicit_value_parameter_type = null;
1354 iterator_container = null;
1356 if (RootContext.Documentation != null)
1357 prop.DocComment = ConsumeStoredComment ();
1362 accessor_declarations
1363 : get_accessor_declaration
1365 $$ = new Pair ($1, null);
1367 | get_accessor_declaration accessor_declarations
1369 Pair pair = (Pair) $2;
1373 | set_accessor_declaration
1375 $$ = new Pair (null, $1);
1377 | set_accessor_declaration accessor_declarations
1379 Pair pair = (Pair) $2;
1385 Report.Error (1014, lexer.Location, "A get or set accessor expected");
1390 get_accessor_declaration
1391 : opt_attributes opt_modifiers GET
1393 // If this is not the case, then current_local_parameters has already
1394 // been set in indexer_declaration
1395 if (parsing_indexer == false)
1396 current_local_parameters = null;
1398 current_local_parameters = indexer_parameters;
1399 lexer.PropertyParsing = false;
1404 Report.Error (1007, lexer.Location, "Property accessor already defined");
1407 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1409 current_local_parameters = null;
1410 lexer.PropertyParsing = true;
1412 if (RootContext.Documentation != null)
1413 if (Lexer.doc_state == XmlCommentState.Error)
1414 Lexer.doc_state = XmlCommentState.NotAllowed;
1418 set_accessor_declaration
1419 : opt_attributes opt_modifiers SET
1422 Parameter implicit_value_parameter = new Parameter (
1423 implicit_value_parameter_type, "value",
1424 Parameter.Modifier.NONE, null);
1426 if (parsing_indexer == false) {
1427 args = new Parameter [1];
1428 args [0] = implicit_value_parameter;
1429 current_local_parameters = new Parameters (args, null, lexer.Location);
1431 Parameter [] fpars = indexer_parameters.FixedParameters;
1434 int count = fpars.Length;
1436 args = new Parameter [count + 1];
1437 fpars.CopyTo (args, 0);
1438 args [count] = implicit_value_parameter;
1441 current_local_parameters = new Parameters (
1442 args, indexer_parameters.ArrayParameter, lexer.Location);
1445 lexer.PropertyParsing = false;
1450 Report.Error (1007, lexer.Location, "Property accessor already defined");
1453 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1455 current_local_parameters = null;
1456 lexer.PropertyParsing = true;
1458 if (RootContext.Documentation != null
1459 && Lexer.doc_state == XmlCommentState.Error)
1460 Lexer.doc_state = XmlCommentState.NotAllowed;
1466 | SEMICOLON { $$ = null; }
1469 interface_declaration
1473 INTERFACE member_name
1475 MemberName name = MakeName ((MemberName) $5);
1476 bool partial = (bool) $3;
1479 ClassPart part = PartialContainer.CreatePart (
1480 current_namespace, current_class, name, (int) $2,
1481 (Attributes) $1, Kind.Interface, lexer.Location);
1483 current_container = part.PartialContainer;
1484 current_class = part;
1486 current_class = new Interface (
1487 current_namespace, current_class, name, (int) $2,
1488 (Attributes) $1, lexer.Location);
1490 current_container.AddInterface (current_class);
1491 current_container = current_class;
1492 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
1495 lexer.ConstraintsParsing = true;
1498 opt_type_parameter_constraints_clauses
1500 lexer.ConstraintsParsing = false;
1503 current_class.Bases = (ArrayList) $7;
1505 current_class.SetParameterInfo ((ArrayList) $8);
1507 if (RootContext.Documentation != null) {
1508 current_class.DocComment = Lexer.consume_doc_comment ();
1509 Lexer.doc_state = XmlCommentState.Allowed;
1514 if (RootContext.Documentation != null)
1515 Lexer.doc_state = XmlCommentState.Allowed;
1519 $$ = pop_current_class ();
1521 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1522 CheckIdentifierToken (yyToken);
1528 opt_interface_member_declarations
1532 opt_interface_member_declarations
1534 | interface_member_declarations
1537 interface_member_declarations
1538 : interface_member_declaration
1539 | interface_member_declarations interface_member_declaration
1542 interface_member_declaration
1543 : interface_method_declaration
1548 Method m = (Method) $1;
1550 if (m.IsExplicitImpl)
1551 Report.Error (541, lexer.Location,
1552 "Explicit interface declaration can only be declared in a class or struct");
1554 current_container.AddMethod (m);
1556 if (RootContext.Documentation != null)
1557 Lexer.doc_state = XmlCommentState.Allowed;
1559 | interface_property_declaration
1564 Property p = (Property) $1;
1566 if (p.IsExplicitImpl)
1567 Report.Error (541, lexer.Location,
1568 "Explicit interface declaration can only be declared in a class or struct");
1570 current_container.AddProperty (p);
1572 if (RootContext.Documentation != null)
1573 Lexer.doc_state = XmlCommentState.Allowed;
1575 | interface_event_declaration
1578 Event e = (Event) $1;
1580 if (e.IsExplicitImpl)
1581 Report.Error (541, lexer.Location,
1582 "Explicit interface declaration can only be declared in a class or struct");
1584 current_container.AddEvent (e);
1587 if (RootContext.Documentation != null)
1588 Lexer.doc_state = XmlCommentState.Allowed;
1590 | interface_indexer_declaration
1595 Indexer i = (Indexer) $1;
1597 if (i.IsExplicitImpl)
1598 Report.Error (541, lexer.Location,
1599 "Explicit interface declaration can only be declared in a class or struct");
1601 current_container.AddIndexer (i);
1603 if (RootContext.Documentation != null)
1604 Lexer.doc_state = XmlCommentState.Allowed;
1606 | delegate_declaration
1608 Report.Error (524, lexer.Location, "Interfaces can not declare delegates");
1612 Report.Error (524, lexer.Location, "Interfaces can not declare classes");
1614 | struct_declaration
1616 Report.Error (524, lexer.Location, "Interfaces can not declare structures");
1620 Report.Error (524, lexer.Location, "Interfaces can not declare enumerations");
1622 | interface_declaration
1624 Report.Error (524, lexer.Location, "Interfaces can not declare interfaces");
1626 | constant_declaration
1628 Report.Error (525, lexer.Location, "Interfaces cannot contain constants");
1636 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, lexer.Location);
1641 interface_method_declaration
1642 : opt_attributes opt_new type namespace_or_type_name
1643 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1645 lexer.ConstraintsParsing = true;
1647 opt_type_parameter_constraints_clauses
1650 lexer.ConstraintsParsing = false;
1652 MemberName name = (MemberName) $4;
1654 if ($9 != null && name.TypeArguments == null)
1655 Report.Error (80, lexer.Location,
1656 "Contraints are not allowed on non-generic declarations");
1658 GenericMethod generic = null;
1659 if (name.TypeArguments != null) {
1660 generic = new GenericMethod (current_namespace, current_class,
1661 name, lexer.Location);
1663 generic.SetParameterInfo ((ArrayList) $9);
1666 $$ = new Method (current_class, generic, (Expression) $3, (int) $2, true, name,
1667 (Parameters) $6, (Attributes) $1, lexer.Location);
1668 if (RootContext.Documentation != null)
1669 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1671 | opt_attributes opt_new VOID namespace_or_type_name
1672 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1674 lexer.ConstraintsParsing = true;
1676 opt_type_parameter_constraints_clauses
1679 lexer.ConstraintsParsing = false;
1681 MemberName name = (MemberName) $4;
1683 if ($9 != null && name.TypeArguments == null)
1684 Report.Error (80, lexer.Location,
1685 "Contraints are not allowed on non-generic declarations");
1687 GenericMethod generic = null;
1688 if (name.TypeArguments != null) {
1689 generic = new GenericMethod (current_namespace, current_class,
1690 name, lexer.Location);
1692 generic.SetParameterInfo ((ArrayList) $9);
1695 $$ = new Method (current_class, generic, TypeManager.system_void_expr, (int) $2,
1696 true, name, (Parameters) $6, (Attributes) $1, lexer.Location);
1697 if (RootContext.Documentation != null)
1698 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1700 | opt_attributes opt_new type namespace_or_type_name
1701 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1703 lexer.ConstraintsParsing = true;
1705 opt_type_parameter_constraints_clauses
1706 OPEN_BRACE opt_statement_list CLOSE_BRACE
1708 lexer.ConstraintsParsing = false;
1709 Report.Error (531, lexer.Location, "'{0}': interface members cannot have a definition", $4);
1715 interface_property_declaration
1720 { lexer.PropertyParsing = true; }
1722 { lexer.PropertyParsing = false; }
1725 if ($3 == TypeManager.system_void_expr) {
1726 Report.Error (547, lexer.Location, "'{0}': property cannot have void type", $4);
1733 InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
1735 $$ = new Property (current_class, (Expression) $3, (int) $2, true,
1736 new MemberName ((string) $4), (Attributes) $1,
1737 pinfo.Get, pinfo.Set, lexer.Location);
1738 if (RootContext.Documentation != null)
1739 ((Property) $$).DocComment = Lexer.consume_doc_comment ();
1744 CheckIdentifierToken (yyToken);
1750 : opt_attributes opt_modifiers GET SEMICOLON
1751 { $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, (int) $2, 0, lexer.Location, lexer.Location); }
1752 | opt_attributes opt_modifiers GET OPEN_BRACE
1754 Report.Error (531, lexer.Location, "'{0}': interface members cannot have a definition", ".get");
1757 | opt_attributes opt_modifiers SET SEMICOLON
1758 { $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1, 0, (int) $2, lexer.Location, lexer.Location); }
1759 | opt_attributes opt_modifiers GET SEMICOLON opt_attributes opt_modifiers SET SEMICOLON
1760 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $5, (int) $2, (int) $6, lexer.Location, lexer.Location); }
1761 | opt_attributes opt_modifiers SET SEMICOLON opt_attributes opt_modifiers GET SEMICOLON
1762 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $5, (Attributes) $1, (int) $6, (int) $2, lexer.Location, lexer.Location); }
1765 Report.Error (548, lexer.Location, "'{0}' : property or indexer must have at least one accessor", "");
1769 interface_event_declaration
1770 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1772 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1773 new MemberName ((string) $5), null,
1774 (Attributes) $1, lexer.Location);
1775 if (RootContext.Documentation != null)
1776 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1778 | opt_attributes opt_new EVENT type error {
1779 CheckIdentifierToken (yyToken);
1782 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1783 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1786 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1788 lexer.EventParsing = true;
1790 event_accessor_declarations
1792 lexer.EventParsing = false;
1795 Report.Error (69, lexer.Location, "Event in interface cannot have add or remove accessors");
1800 interface_indexer_declaration
1801 : opt_attributes opt_new type THIS
1802 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1804 { lexer.PropertyParsing = true; }
1806 { lexer.PropertyParsing = false; }
1812 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1814 $$ = new Indexer (current_class, (Expression) $3,
1815 new MemberName (TypeContainer.DefaultIndexerName),
1816 (int) $2, true, (Parameters) $6, (Attributes) $1,
1817 info.Get, info.Set, lexer.Location);
1818 if (RootContext.Documentation != null)
1819 ((Indexer) $$).DocComment = ConsumeStoredComment ();
1823 operator_declaration
1824 : opt_attributes opt_modifiers operator_declarator
1826 iterator_container = SimpleIteratorContainer.GetSimple ();
1833 OperatorDeclaration decl = (OperatorDeclaration) $3;
1835 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1837 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null);
1838 if (decl.arg2type != null)
1839 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null);
1841 Operator op = new Operator (
1842 current_class, decl.optype, decl.ret_type, (int) $2,
1843 new Parameters (param_list, null, decl.location),
1844 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1846 if (RootContext.Documentation != null) {
1847 op.DocComment = tmpComment;
1848 Lexer.doc_state = XmlCommentState.Allowed;
1851 if (SimpleIteratorContainer.Simple.Yields)
1854 // Note again, checking is done in semantic analysis
1855 current_container.AddOperator (op);
1857 current_local_parameters = null;
1858 iterator_container = null;
1864 | SEMICOLON { $$ = null; }
1867 : type OPERATOR overloadable_operator
1868 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1870 Operator.OpType op = (Operator.OpType) $3;
1871 CheckUnaryOperator (op);
1873 if (op == Operator.OpType.Addition)
1874 op = Operator.OpType.UnaryPlus;
1876 if (op == Operator.OpType.Subtraction)
1877 op = Operator.OpType.UnaryNegation;
1879 Parameter [] pars = new Parameter [1];
1880 Expression type = (Expression) $5;
1882 pars [0] = new Parameter (type, (string) $6, Parameter.Modifier.NONE, null);
1884 current_local_parameters = new Parameters (pars, null, lexer.Location);
1886 if (RootContext.Documentation != null) {
1887 tmpComment = Lexer.consume_doc_comment ();
1888 Lexer.doc_state = XmlCommentState.NotAllowed;
1891 $$ = new OperatorDeclaration (op, (Expression) $1, type, (string) $6,
1892 null, null, lexer.Location);
1894 | type OPERATOR overloadable_operator
1896 type IDENTIFIER COMMA
1900 CheckBinaryOperator ((Operator.OpType) $3);
1902 Parameter [] pars = new Parameter [2];
1904 Expression typeL = (Expression) $5;
1905 Expression typeR = (Expression) $8;
1907 pars [0] = new Parameter (typeL, (string) $6, Parameter.Modifier.NONE, null);
1908 pars [1] = new Parameter (typeR, (string) $9, Parameter.Modifier.NONE, null);
1910 current_local_parameters = new Parameters (pars, null, lexer.Location);
1912 if (RootContext.Documentation != null) {
1913 tmpComment = Lexer.consume_doc_comment ();
1914 Lexer.doc_state = XmlCommentState.NotAllowed;
1917 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1919 typeR, (string) $9, lexer.Location);
1921 | conversion_operator_declarator
1922 | type OPERATOR overloadable_operator
1924 type IDENTIFIER COMMA
1925 type IDENTIFIER COMMA
1929 Report.Error (1534, lexer.Location, "Overloaded binary operator '{0}' takes two parameters", $3);
1935 overloadable_operator
1937 : BANG { $$ = Operator.OpType.LogicalNot; }
1938 | TILDE { $$ = Operator.OpType.OnesComplement; }
1939 | OP_INC { $$ = Operator.OpType.Increment; }
1940 | OP_DEC { $$ = Operator.OpType.Decrement; }
1941 | TRUE { $$ = Operator.OpType.True; }
1942 | FALSE { $$ = Operator.OpType.False; }
1943 // Unary and binary:
1944 | PLUS { $$ = Operator.OpType.Addition; }
1945 | MINUS { $$ = Operator.OpType.Subtraction; }
1947 | STAR { $$ = Operator.OpType.Multiply; }
1948 | DIV { $$ = Operator.OpType.Division; }
1949 | PERCENT { $$ = Operator.OpType.Modulus; }
1950 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1951 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1952 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1953 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1954 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1955 | OP_EQ { $$ = Operator.OpType.Equality; }
1956 | OP_NE { $$ = Operator.OpType.Inequality; }
1957 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1958 | OP_LT { $$ = Operator.OpType.LessThan; }
1959 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1960 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1963 conversion_operator_declarator
1964 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1966 Parameter [] pars = new Parameter [1];
1968 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1970 current_local_parameters = new Parameters (pars, null, lexer.Location);
1972 if (RootContext.Documentation != null) {
1973 tmpComment = Lexer.consume_doc_comment ();
1974 Lexer.doc_state = XmlCommentState.NotAllowed;
1977 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1978 null, null, lexer.Location);
1980 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1982 Parameter [] pars = new Parameter [1];
1984 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1986 current_local_parameters = new Parameters (pars, null, lexer.Location);
1988 if (RootContext.Documentation != null) {
1989 tmpComment = Lexer.consume_doc_comment ();
1990 Lexer.doc_state = XmlCommentState.NotAllowed;
1993 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1994 null, null, lexer.Location);
1998 syntax_error (lexer.Location, "'operator' expected");
2002 syntax_error (lexer.Location, "'operator' expected");
2006 constructor_declaration
2009 constructor_declarator
2012 Constructor c = (Constructor) $3;
2013 c.Block = (ToplevelBlock) $4;
2014 c.OptAttributes = (Attributes) $1;
2015 c.ModFlags = (int) $2;
2017 if (RootContext.Documentation != null)
2018 c.DocComment = ConsumeStoredComment ();
2020 if (c.Name == current_container.Basename){
2021 if ((c.ModFlags & Modifiers.STATIC) != 0){
2022 if ((c.ModFlags & Modifiers.Accessibility) != 0){
2024 515, c.Location, String.Format (
2025 "`{0}.{1}': static constructor can not have access modifiers",
2026 c.Name, current_container.Name));
2029 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2031 if (c.Initializer != null){
2034 "Static constructors can not have an explicit this or base " +
2035 "constructor invocations");
2038 if (!c.Parameters.Empty){
2040 132, c.Location, "Static constructors should not have parameters");
2043 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2046 // We let another layer check the validity of the constructor.
2047 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
2050 current_container.AddConstructor (c);
2052 current_local_parameters = null;
2053 if (RootContext.Documentation != null)
2054 Lexer.doc_state = XmlCommentState.Allowed;
2058 constructor_declarator
2061 if (RootContext.Documentation != null) {
2062 tmpComment = Lexer.consume_doc_comment ();
2063 Lexer.doc_state = XmlCommentState.Allowed;
2066 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS _mark_
2068 current_local_parameters = (Parameters) $4;
2070 opt_constructor_initializer
2072 $$ = new Constructor (current_class, (string) $1, 0, (Parameters) $4,
2073 (ConstructorInitializer) $8, (Location) $6);
2079 | SEMICOLON { $$ = null; }
2082 opt_constructor_initializer
2083 : /* empty */ { $$ = null; }
2084 | constructor_initializer
2087 constructor_initializer
2088 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
2090 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
2092 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
2094 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
2097 Report.Error (1018, lexer.Location, "Keyword this or base expected");
2103 : /* EMPTY */ { $$ = 0; }
2104 | UNSAFE { $$ = Modifiers.UNSAFE; }
2105 | EXTERN { $$ = Modifiers.EXTERN; }
2108 destructor_declaration
2109 : opt_attributes opt_finalizer TILDE
2111 if (RootContext.Documentation != null) {
2112 tmpComment = Lexer.consume_doc_comment ();
2113 Lexer.doc_state = XmlCommentState.NotAllowed;
2116 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2118 if ((string) $5 != current_container.Basename){
2119 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
2120 } else if (current_container.Kind != Kind.Class){
2121 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
2123 Location l = lexer.Location;
2126 if (!RootContext.StdLib && current_container.Name == "System.Object")
2127 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2129 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2131 if ((m & Modifiers.UNSAFE) != 0){
2132 if (!RootContext.Unsafe){
2133 Report.Error (227, l,
2134 "Unsafe code requires the -unsafe command " +
2135 "line option to be specified");
2139 Method d = new Destructor (
2140 current_class, TypeManager.system_void_expr, m, "Finalize",
2141 new Parameters (null, null, l), (Attributes) $1, l);
2142 if (RootContext.Documentation != null)
2143 d.DocComment = ConsumeStoredComment ();
2145 d.Block = (ToplevelBlock) $8;
2146 current_container.AddMethod (d);
2154 EVENT type variable_declarators SEMICOLON
2156 foreach (VariableDeclaration var in (ArrayList) $5) {
2158 MemberName name = new MemberName (var.identifier);
2160 Event e = new EventField (
2161 current_class, (Expression) $4, (int) $2, false, name,
2162 var.expression_or_array_initializer, (Attributes) $1,
2165 current_container.AddEvent (e);
2167 if (RootContext.Documentation != null) {
2168 e.DocComment = Lexer.consume_doc_comment ();
2169 Lexer.doc_state = XmlCommentState.Allowed;
2175 EVENT type namespace_or_type_name
2178 implicit_value_parameter_type = (Expression) $4;
2179 lexer.EventParsing = true;
2181 event_accessor_declarations
2183 lexer.EventParsing = false;
2187 Location loc = (Location) $7;
2190 Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
2193 Pair pair = (Pair) $9;
2195 MemberName name = (MemberName) $5;
2197 if (name.TypeArguments != null)
2198 syntax_error (lexer.Location, "an event can't have type arguments");
2200 Event e = new EventProperty (
2201 current_class, (Expression) $4, (int) $2, false, name, null,
2202 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
2204 if (RootContext.Documentation != null) {
2205 e.DocComment = Lexer.consume_doc_comment ();
2206 Lexer.doc_state = XmlCommentState.Allowed;
2209 current_container.AddEvent (e);
2210 implicit_value_parameter_type = null;
2213 | opt_attributes opt_modifiers EVENT type namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS block {
2214 MemberName mn = (MemberName) $5;
2216 if (mn.Left != null)
2217 Report.Error (71, lexer.Location, "Explicit implementation of events requires property syntax");
2219 Report.Error (71, lexer.Location, "Event declaration should use property syntax");
2221 if (RootContext.Documentation != null)
2222 Lexer.doc_state = XmlCommentState.Allowed;
2226 event_accessor_declarations
2227 : add_accessor_declaration remove_accessor_declaration
2229 $$ = new Pair ($1, $2);
2231 | remove_accessor_declaration add_accessor_declaration
2233 $$ = new Pair ($2, $1);
2235 | add_accessor_declaration { $$ = null; }
2236 | remove_accessor_declaration { $$ = null; }
2239 Report.Error (1055, lexer.Location, "An add or remove accessor expected");
2245 add_accessor_declaration
2246 : opt_attributes ADD
2248 Parameter [] args = new Parameter [1];
2249 Parameter implicit_value_parameter = new Parameter (
2250 implicit_value_parameter_type, "value",
2251 Parameter.Modifier.NONE, null);
2253 args [0] = implicit_value_parameter;
2255 current_local_parameters = new Parameters (args, null, lexer.Location);
2256 lexer.EventParsing = false;
2260 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2261 lexer.EventParsing = true;
2263 | opt_attributes ADD error {
2264 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2267 | opt_attributes modifiers ADD {
2268 Report.Error (1609, lexer.Location, "Modifiers cannot be placed on event accessor declarations");
2273 remove_accessor_declaration
2274 : opt_attributes REMOVE
2276 Parameter [] args = new Parameter [1];
2277 Parameter implicit_value_parameter = new Parameter (
2278 implicit_value_parameter_type, "value",
2279 Parameter.Modifier.NONE, null);
2281 args [0] = implicit_value_parameter;
2283 current_local_parameters = new Parameters (args, null, lexer.Location);
2284 lexer.EventParsing = false;
2288 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2289 lexer.EventParsing = true;
2291 | opt_attributes REMOVE error {
2292 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2295 | opt_attributes modifiers REMOVE {
2296 Report.Error (1609, lexer.Location, "Modifiers cannot be placed on event accessor declarations");
2302 : opt_attributes opt_modifiers indexer_declarator
2305 IndexerDeclaration decl = (IndexerDeclaration) $3;
2307 implicit_value_parameter_type = decl.type;
2309 lexer.PropertyParsing = true;
2310 parsing_indexer = true;
2312 indexer_parameters = decl.param_list;
2314 accessor_declarations
2316 lexer.PropertyParsing = false;
2317 has_get = has_set = false;
2318 parsing_indexer = false;
2325 // The signature is computed from the signature of the indexer. Look
2326 // at section 3.6 on the spec
2327 Location loc = (Location) $5;
2329 IndexerDeclaration decl = (IndexerDeclaration) $3;
2330 Pair pair = (Pair) $7;
2331 Accessor get_block = (Accessor) pair.First;
2332 Accessor set_block = (Accessor) pair.Second;
2335 if (decl.interface_type != null)
2336 name = new MemberName (decl.interface_type,
2337 TypeContainer.DefaultIndexerName, null);
2339 name = new MemberName (TypeContainer.DefaultIndexerName);
2341 indexer = new Indexer (current_class, decl.type, name,
2342 (int) $2, false, decl.param_list, (Attributes) $1,
2343 get_block, set_block, loc);
2344 if (RootContext.Documentation != null)
2345 indexer.DocComment = ConsumeStoredComment ();
2347 current_container.AddIndexer (indexer);
2349 current_local_parameters = null;
2350 implicit_value_parameter_type = null;
2351 indexer_parameters = null;
2356 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2358 Parameters pars = (Parameters) $4;
2359 if (pars.HasArglist) {
2360 // "__arglist is not valid in this context"
2361 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2362 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2363 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2365 if (RootContext.Documentation != null) {
2366 tmpComment = Lexer.consume_doc_comment ();
2367 Lexer.doc_state = XmlCommentState.Allowed;
2370 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
2372 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2374 Parameters pars = (Parameters) $6;
2376 if (pars.HasArglist) {
2377 // "__arglist is not valid in this context"
2378 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2379 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2380 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2383 MemberName name = (MemberName) $2;
2385 $$ = new IndexerDeclaration ((Expression) $1, name, pars);
2387 if (RootContext.Documentation != null) {
2388 tmpComment = Lexer.consume_doc_comment ();
2389 Lexer.doc_state = XmlCommentState.Allowed;
2399 if (RootContext.Documentation != null)
2400 enumTypeComment = Lexer.consume_doc_comment ();
2405 Location enum_location = lexer.Location;
2407 MemberName full_name = MakeName (new MemberName ((string) $4));
2408 Enum e = new Enum (current_namespace, current_class, (Expression) $5, (int) $2,
2409 full_name, (Attributes) $1, enum_location);
2411 if (RootContext.Documentation != null)
2412 e.DocComment = enumTypeComment;
2414 foreach (VariableDeclaration ev in (ArrayList) $7) {
2415 e.AddEnumMember (ev.identifier,
2416 (Expression) ev.expression_or_array_initializer,
2417 ev.Location, ev.OptAttributes,
2421 string name = full_name.GetName ();
2422 current_container.AddEnum (e);
2423 RootContext.Tree.RecordDecl (name, e);
2429 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2430 | COLON type { $$ = $2; }
2436 if (RootContext.Documentation != null)
2437 Lexer.doc_state = XmlCommentState.Allowed;
2439 opt_enum_member_declarations
2441 // here will be evaluated after CLOSE_BLACE is consumed.
2442 if (RootContext.Documentation != null)
2443 Lexer.doc_state = XmlCommentState.Allowed;
2451 opt_enum_member_declarations
2452 : /* empty */ { $$ = new ArrayList (4); }
2453 | enum_member_declarations opt_comma { $$ = $1; }
2456 enum_member_declarations
2457 : enum_member_declaration
2459 ArrayList l = new ArrayList (4);
2464 | enum_member_declarations COMMA enum_member_declaration
2466 ArrayList l = (ArrayList) $1;
2474 enum_member_declaration
2475 : opt_attributes IDENTIFIER
2477 VariableDeclaration vd = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
2479 if (RootContext.Documentation != null) {
2480 vd.DocComment = Lexer.consume_doc_comment ();
2481 Lexer.doc_state = XmlCommentState.Allowed;
2486 | opt_attributes IDENTIFIER
2488 $$ = lexer.Location;
2489 if (RootContext.Documentation != null) {
2490 tmpComment = Lexer.consume_doc_comment ();
2491 Lexer.doc_state = XmlCommentState.NotAllowed;
2496 VariableDeclaration vd = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
2498 if (RootContext.Documentation != null)
2499 vd.DocComment = ConsumeStoredComment ();
2505 delegate_declaration
2508 DELEGATE type member_name
2509 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2511 Location l = lexer.Location;
2512 MemberName name = MakeName ((MemberName) $5);
2513 Delegate del = new Delegate (current_namespace, current_class, (Expression) $4,
2514 (int) $2, name, (Parameters) $7, (Attributes) $1, l);
2516 if (RootContext.Documentation != null) {
2517 del.DocComment = Lexer.consume_doc_comment ();
2518 Lexer.doc_state = XmlCommentState.Allowed;
2521 current_container.AddDelegate (del);
2522 RootContext.Tree.RecordDecl (name.GetName (true), del);
2524 current_delegate = del;
2526 lexer.ConstraintsParsing = true;
2528 opt_type_parameter_constraints_clauses
2530 lexer.ConstraintsParsing = false;
2534 current_delegate.SetParameterInfo ((ArrayList) $9);
2536 current_delegate = null;
2543 lexer.CheckNullable (false);
2548 lexer.CheckNullable (true);
2553 namespace_or_type_name
2555 | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list {
2556 $$ = new MemberName ((MemberName) $1, (string) $3, (TypeArguments) $4);
2561 : IDENTIFIER opt_type_argument_list {
2562 $$ = new MemberName ((string) $1, (TypeArguments) $2);
2566 opt_type_argument_list
2567 : /* empty */ { $$ = null; }
2568 | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2574 $$ = new TypeArguments ((int) $1, lexer.Location);
2580 TypeArguments type_args = new TypeArguments (lexer.Location);
2581 type_args.Add ((Expression) $1);
2584 | type_arguments COMMA type {
2585 TypeArguments type_args = (TypeArguments) $1;
2586 type_args.Add ((Expression) $3);
2592 * Before you think of adding a return_type, notice that we have been
2593 * using two rules in the places where it matters (one rule using type
2594 * and another identical one that uses VOID as the return type). This
2595 * gets rid of a shift/reduce couple
2598 : namespace_or_type_name opt_nullable
2600 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2603 $$ = new ComposedCast ((Expression) $$, "?", lexer.Location);
2605 | builtin_types opt_nullable
2608 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2618 // Note that here only unmanaged types are allowed but we
2619 // can't perform checks during this phase - we do it during
2620 // semantic analysis.
2622 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2626 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2631 : builtin_types opt_nullable
2634 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2636 | non_expression_type rank_specifier
2638 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2640 | non_expression_type STAR
2642 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2644 | expression rank_specifiers
2646 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2650 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2654 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2655 // through this different path
2657 | multiplicative_expression STAR
2659 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2666 ArrayList types = new ArrayList (4);
2671 | type_list COMMA type
2673 ArrayList types = (ArrayList) $1;
2681 * replaces all the productions for isolating the various
2682 * simple types, but we need this to reuse it easily in local_variable_type
2685 : OBJECT { $$ = TypeManager.system_object_expr; }
2686 | STRING { $$ = TypeManager.system_string_expr; }
2687 | BOOL { $$ = TypeManager.system_boolean_expr; }
2688 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2689 | FLOAT { $$ = TypeManager.system_single_expr; }
2690 | DOUBLE { $$ = TypeManager.system_double_expr; }
2695 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2696 | BYTE { $$ = TypeManager.system_byte_expr; }
2697 | SHORT { $$ = TypeManager.system_int16_expr; }
2698 | USHORT { $$ = TypeManager.system_uint16_expr; }
2699 | INT { $$ = TypeManager.system_int32_expr; }
2700 | UINT { $$ = TypeManager.system_uint32_expr; }
2701 | LONG { $$ = TypeManager.system_int64_expr; }
2702 | ULONG { $$ = TypeManager.system_uint64_expr; }
2703 | CHAR { $$ = TypeManager.system_char_expr; }
2704 | VOID { $$ = TypeManager.system_void_expr; }
2708 : type rank_specifiers opt_nullable
2710 string rank_specifiers = (string) $2;
2712 rank_specifiers += "?";
2714 $$ = new ComposedCast ((Expression) $1, rank_specifiers, lexer.Location);
2719 // Expressions, section 7.5
2728 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2730 | parenthesized_expression
2731 | default_value_expression
2733 | invocation_expression
2737 | post_increment_expression
2738 | post_decrement_expression
2742 | checked_expression
2743 | unchecked_expression
2744 | pointer_member_access
2745 | anonymous_method_expression
2752 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2753 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2754 | NULL { $$ = NullLiteral.Null; }
2758 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2759 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2760 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2765 object v = lexer.Value;
2771 $$ = IntLiteral.Zero;
2773 $$ = IntLiteral.One;
2775 $$ = new IntLiteral (i);
2776 } else if (v is uint)
2777 $$ = new UIntLiteral ((UInt32) v);
2779 $$ = new LongLiteral ((Int64) v);
2780 else if (v is ulong)
2781 $$ = new ULongLiteral ((UInt64) v);
2783 Console.WriteLine ("OOPS. Unexpected result from scanner");
2788 : TRUE { $$ = new BoolLiteral (true); }
2789 | FALSE { $$ = new BoolLiteral (false); }
2792 parenthesized_expression_0
2793 : OPEN_PARENS expression CLOSE_PARENS
2796 lexer.Deambiguate_CloseParens ();
2797 // After this, the next token returned is one of
2798 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2799 // or CLOSE_PARENS_MINUS.
2803 parenthesized_expression
2804 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2808 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2810 // If a parenthesized expression is followed by a minus, we need to wrap
2811 // the expression inside a ParenthesizedExpression for the CS0075 check
2812 // in Binary.DoResolve().
2813 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2818 : primary_expression DOT IDENTIFIER opt_type_argument_list
2820 $$ = new MemberAccess ((Expression) $1, (string) $3,
2821 (TypeArguments) $4, lexer.Location);
2823 | predefined_type DOT IDENTIFIER opt_type_argument_list
2825 $$ = new MemberAccess ((Expression) $1, (string) $3,
2826 (TypeArguments) $4, lexer.Location);
2834 invocation_expression
2835 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2838 Location l = lexer.Location;
2839 Report.Error (1, l, "Parse error");
2841 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2843 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2845 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2847 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2849 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2851 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2853 ArrayList args = new ArrayList (1);
2855 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2857 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2859 ArrayList args = ((ArrayList) $4);
2861 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2866 : /* empty */ { $$ = null; }
2873 ArrayList list = new ArrayList (4);
2877 | argument_list COMMA argument
2879 ArrayList list = (ArrayList) $1;
2883 | argument_list error {
2884 CheckToken (1026, yyToken, ", or ) expected");
2891 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2893 | non_simple_argument
2900 : REF variable_reference
2902 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2904 | OUT variable_reference
2906 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2908 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2910 ArrayList list = (ArrayList) $3;
2911 Argument[] args = new Argument [list.Count];
2912 list.CopyTo (args, 0);
2914 Expression expr = new Arglist (args, lexer.Location);
2915 $$ = new Argument (expr, Argument.AType.Expression);
2919 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2924 : expression { note ("section 5.4"); $$ = $1; }
2928 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2930 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2932 | primary_expression rank_specifiers
2934 // So the super-trick is that primary_expression
2935 // can only be either a SimpleName or a MemberAccess.
2936 // The MemberAccess case arises when you have a fully qualified type-name like :
2938 // SimpleName is when you have
2941 Expression expr = (Expression) $1;
2942 if (expr is ComposedCast){
2943 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2944 } else if (!(expr is SimpleName || expr is MemberAccess || expr is ConstructedType)){
2945 Error_ExpectingTypeName (lexer.Location, expr);
2946 $$ = TypeManager.system_object_expr;
2949 // So we extract the string corresponding to the SimpleName
2952 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2960 ArrayList list = new ArrayList (4);
2964 | expression_list COMMA expression
2966 ArrayList list = (ArrayList) $1;
2975 $$ = new This (current_block, lexer.Location);
2980 : BASE DOT IDENTIFIER
2982 $$ = new BaseAccess ((string) $3, lexer.Location);
2984 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2986 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2989 Report.Error (175, lexer.Location, "Use of keyword `base' is not valid in this context");
2994 post_increment_expression
2995 : primary_expression OP_INC
2997 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2998 (Expression) $1, lexer.Location);
3002 post_decrement_expression
3003 : primary_expression OP_DEC
3005 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
3006 (Expression) $1, lexer.Location);
3011 : object_or_delegate_creation_expression
3012 | array_creation_expression
3015 object_or_delegate_creation_expression
3016 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
3018 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
3022 array_creation_expression
3023 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
3025 opt_array_initializer
3027 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
3029 | NEW type rank_specifiers array_initializer
3031 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
3035 Report.Error (1031, lexer.Location, "Type expected");
3040 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
3055 opt_rank_specifier_or_nullable
3064 | opt_nullable rank_specifiers
3071 | opt_nullable rank_specifiers INTERR
3074 $$ = "?" + $2 + "?";
3081 : rank_specifier opt_rank_specifier
3083 $$ = (string) $2 + (string) $1;
3088 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3090 $$ = "[" + (string) $2 + "]";
3110 | dim_separators COMMA
3112 $$ = (string) $1 + ",";
3116 opt_array_initializer
3128 : OPEN_BRACE CLOSE_BRACE
3130 ArrayList list = new ArrayList (4);
3133 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3135 $$ = (ArrayList) $2;
3139 variable_initializer_list
3140 : variable_initializer
3142 ArrayList list = new ArrayList (4);
3146 | variable_initializer_list COMMA variable_initializer
3148 ArrayList list = (ArrayList) $1;
3154 void_pointer_expression
3155 : void_pointer_expression STAR
3157 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3161 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3166 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
3168 $$ = new TypeOfVoid (lexer.Location);
3170 | TYPEOF OPEN_PARENS void_pointer_expression CLOSE_PARENS
3172 $$ = new TypeOf ((Expression) $3, lexer.Location);
3174 | TYPEOF OPEN_PARENS
3176 lexer.TypeOfParsing = true;
3180 lexer.TypeOfParsing = false;
3181 $$ = new TypeOf ((Expression) $4, lexer.Location);
3186 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3187 $$ = new SizeOf ((Expression) $3, lexer.Location);
3192 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3194 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
3198 unchecked_expression
3199 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3201 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
3205 pointer_member_access
3206 : primary_expression OP_PTR IDENTIFIER
3210 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
3211 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
3215 anonymous_method_expression
3216 : DELEGATE opt_anonymous_method_signature _mark_
3218 oob_stack.Push (current_local_parameters);
3219 current_local_parameters = (Parameters)$2;
3221 // Force the next block to be created as a ToplevelBlock
3222 oob_stack.Push (current_block);
3223 oob_stack.Push (top_current_block);
3224 current_block = null;
3228 Location loc = (Location) $3;
3229 top_current_block = (Block) oob_stack.Pop ();
3230 current_block = (Block) oob_stack.Pop ();
3231 if (RootContext.Version == LanguageVersion.ISO_1){
3232 Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
3235 ToplevelBlock anon_block = (ToplevelBlock) $5;
3237 anon_block.Parent = current_block;
3238 $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block,
3241 current_local_parameters = (Parameters) oob_stack.Pop ();
3245 opt_anonymous_method_signature
3246 : /* empty */ { $$ = null; }
3247 | anonymous_method_signature
3250 anonymous_method_signature
3251 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3254 $$ = Parameters.EmptyReadOnlyParameters;
3256 ArrayList par_list = (ArrayList) $2;
3257 Parameter [] pars = new Parameter [par_list.Count];
3258 par_list.CopyTo (pars);
3259 $$ = new Parameters (pars, null, lexer.Location);
3264 opt_anonymous_method_parameter_list
3265 : /* empty */ { $$ = null; }
3266 | anonymous_method_parameter_list { $$ = $1; }
3269 anonymous_method_parameter_list
3270 : anonymous_method_parameter
3272 ArrayList a = new ArrayList (4);
3276 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3278 ArrayList a = (ArrayList) $1;
3284 anonymous_method_parameter
3285 : opt_parameter_modifier type IDENTIFIER {
3286 $$ = new Parameter ((Expression) $2, (string) $3, (Parameter.Modifier) $1, null);
3288 | PARAMS type IDENTIFIER {
3289 Report.Error (1670, lexer.Location, "params modifier not allowed in anonymous method declaration");
3294 default_value_expression
3295 : DEFAULT_OPEN_PARENS type CLOSE_PARENS
3297 $$ = new DefaultValueExpression ((Expression) $2, lexer.Location);
3302 : primary_expression
3303 | BANG prefixed_unary_expression
3305 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
3307 | TILDE prefixed_unary_expression
3309 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
3315 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3317 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3319 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3321 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3327 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3329 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3334 // The idea to split this out is from Rhys' grammar
3335 // to solve the problem with casts.
3337 prefixed_unary_expression
3339 | PLUS prefixed_unary_expression
3341 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
3343 | MINUS prefixed_unary_expression
3345 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
3347 | OP_INC prefixed_unary_expression
3349 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3350 (Expression) $2, lexer.Location);
3352 | OP_DEC prefixed_unary_expression
3354 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3355 (Expression) $2, lexer.Location);
3357 | STAR prefixed_unary_expression
3359 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
3361 | BITWISE_AND prefixed_unary_expression
3363 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
3367 pre_increment_expression
3368 : OP_INC prefixed_unary_expression
3370 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3371 (Expression) $2, lexer.Location);
3375 pre_decrement_expression
3376 : OP_DEC prefixed_unary_expression
3378 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3379 (Expression) $2, lexer.Location);
3383 multiplicative_expression
3384 : prefixed_unary_expression
3385 | multiplicative_expression STAR prefixed_unary_expression
3387 $$ = new Binary (Binary.Operator.Multiply,
3388 (Expression) $1, (Expression) $3, lexer.Location);
3390 | multiplicative_expression DIV prefixed_unary_expression
3392 $$ = new Binary (Binary.Operator.Division,
3393 (Expression) $1, (Expression) $3, lexer.Location);
3395 | multiplicative_expression PERCENT prefixed_unary_expression
3397 $$ = new Binary (Binary.Operator.Modulus,
3398 (Expression) $1, (Expression) $3, lexer.Location);
3403 : multiplicative_expression
3404 | additive_expression PLUS multiplicative_expression
3406 $$ = new Binary (Binary.Operator.Addition,
3407 (Expression) $1, (Expression) $3, lexer.Location);
3409 | additive_expression MINUS multiplicative_expression
3411 $$ = new Binary (Binary.Operator.Subtraction,
3412 (Expression) $1, (Expression) $3, lexer.Location);
3417 : additive_expression
3418 | shift_expression OP_SHIFT_LEFT additive_expression
3420 $$ = new Binary (Binary.Operator.LeftShift,
3421 (Expression) $1, (Expression) $3, lexer.Location);
3423 | shift_expression OP_SHIFT_RIGHT additive_expression
3425 $$ = new Binary (Binary.Operator.RightShift,
3426 (Expression) $1, (Expression) $3, lexer.Location);
3434 lexer.PutbackNullable ();
3438 nullable_type_or_conditional
3445 relational_expression
3447 | relational_expression OP_LT shift_expression
3449 $$ = new Binary (Binary.Operator.LessThan,
3450 (Expression) $1, (Expression) $3, lexer.Location);
3452 | relational_expression OP_GT shift_expression
3454 $$ = new Binary (Binary.Operator.GreaterThan,
3455 (Expression) $1, (Expression) $3, lexer.Location);
3457 | relational_expression OP_LE shift_expression
3459 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3460 (Expression) $1, (Expression) $3, lexer.Location);
3462 | relational_expression OP_GE shift_expression
3464 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3465 (Expression) $1, (Expression) $3, lexer.Location);
3467 | relational_expression IS
3470 } nullable_type_or_conditional
3472 $$ = new Is ((Expression) $1, (Expression) $4, lexer.Location);
3474 | relational_expression AS
3477 } nullable_type_or_conditional
3479 $$ = new As ((Expression) $1, (Expression) $4, lexer.Location);
3484 : relational_expression
3485 | equality_expression OP_EQ relational_expression
3487 $$ = new Binary (Binary.Operator.Equality,
3488 (Expression) $1, (Expression) $3, lexer.Location);
3490 | equality_expression OP_NE relational_expression
3492 $$ = new Binary (Binary.Operator.Inequality,
3493 (Expression) $1, (Expression) $3, lexer.Location);
3498 : equality_expression
3499 | and_expression BITWISE_AND equality_expression
3501 $$ = new Binary (Binary.Operator.BitwiseAnd,
3502 (Expression) $1, (Expression) $3, lexer.Location);
3506 exclusive_or_expression
3508 | exclusive_or_expression CARRET and_expression
3510 $$ = new Binary (Binary.Operator.ExclusiveOr,
3511 (Expression) $1, (Expression) $3, lexer.Location);
3515 inclusive_or_expression
3516 : exclusive_or_expression
3517 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3519 $$ = new Binary (Binary.Operator.BitwiseOr,
3520 (Expression) $1, (Expression) $3, lexer.Location);
3524 conditional_and_expression
3525 : inclusive_or_expression
3526 | conditional_and_expression OP_AND inclusive_or_expression
3528 $$ = new Binary (Binary.Operator.LogicalAnd,
3529 (Expression) $1, (Expression) $3, lexer.Location);
3533 conditional_or_expression
3534 : conditional_and_expression
3535 | conditional_or_expression OP_OR conditional_and_expression
3537 $$ = new Binary (Binary.Operator.LogicalOr,
3538 (Expression) $1, (Expression) $3, lexer.Location);
3542 conditional_expression
3543 : conditional_or_expression
3544 | conditional_or_expression INTERR expression COLON expression
3546 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
3548 | conditional_or_expression INTERR INTERR expression
3550 $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $4, lexer.Location);
3552 // We'll be resolved into a `parenthesized_expression_0' later on.
3553 | conditional_or_expression INTERR CLOSE_PARENS
3555 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
3556 lexer.PutbackCloseParens ();
3560 assignment_expression
3561 : prefixed_unary_expression ASSIGN expression
3563 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
3565 | prefixed_unary_expression OP_MULT_ASSIGN expression
3567 Location l = lexer.Location;
3569 $$ = new CompoundAssign (
3570 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
3572 | prefixed_unary_expression OP_DIV_ASSIGN expression
3574 Location l = lexer.Location;
3576 $$ = new CompoundAssign (
3577 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
3579 | prefixed_unary_expression OP_MOD_ASSIGN expression
3581 Location l = lexer.Location;
3583 $$ = new CompoundAssign (
3584 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
3586 | prefixed_unary_expression OP_ADD_ASSIGN expression
3588 Location l = lexer.Location;
3590 $$ = new CompoundAssign (
3591 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
3593 | prefixed_unary_expression OP_SUB_ASSIGN expression
3595 Location l = lexer.Location;
3597 $$ = new CompoundAssign (
3598 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
3600 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3602 Location l = lexer.Location;
3604 $$ = new CompoundAssign (
3605 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
3607 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3609 Location l = lexer.Location;
3611 $$ = new CompoundAssign (
3612 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
3614 | prefixed_unary_expression OP_AND_ASSIGN expression
3616 Location l = lexer.Location;
3618 $$ = new CompoundAssign (
3619 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
3621 | prefixed_unary_expression OP_OR_ASSIGN expression
3623 Location l = lexer.Location;
3625 $$ = new CompoundAssign (
3626 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
3628 | prefixed_unary_expression OP_XOR_ASSIGN expression
3630 Location l = lexer.Location;
3632 $$ = new CompoundAssign (
3633 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
3638 : conditional_expression
3639 | assignment_expression
3659 MemberName name = MakeName ((MemberName) $5);
3660 bool partial = (bool) $3;
3661 int mod_flags = (int) $2;
3664 ClassPart part = PartialContainer.CreatePart (
3665 current_namespace, current_class, name, mod_flags,
3666 (Attributes) $1, Kind.Class, lexer.Location);
3668 current_container = part.PartialContainer;
3669 current_class = part;
3671 if ((mod_flags & Modifiers.STATIC) != 0) {
3672 current_class = new StaticClass (
3673 current_namespace, current_class, name,
3674 mod_flags, (Attributes) $1, lexer.Location);
3676 current_class = new Class (
3677 current_namespace, current_class, name,
3678 mod_flags, (Attributes) $1, lexer.Location);
3681 current_container.AddClassOrStruct (current_class);
3682 current_container = current_class;
3683 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
3686 lexer.ConstraintsParsing = true;
3689 opt_type_parameter_constraints_clauses
3691 lexer.ConstraintsParsing = false;
3694 if (current_class.Name == "System.Object") {
3695 Report.Error (537, current_class.Location,
3696 "The class System.Object cannot have a base " +
3697 "class or implement an interface.");
3699 current_class.Bases = (ArrayList) $7;
3702 current_class.SetParameterInfo ((ArrayList) $8);
3704 if (RootContext.Documentation != null) {
3705 current_class.DocComment = Lexer.consume_doc_comment ();
3706 Lexer.doc_state = XmlCommentState.Allowed;
3711 if (RootContext.Documentation != null)
3712 Lexer.doc_state = XmlCommentState.Allowed;
3716 $$ = pop_current_class ();
3722 { $$ = (bool) false; }
3724 { $$ = (bool) true; }
3728 : /* empty */ { $$ = (int) 0; }
3734 | modifiers modifier
3739 if ((m1 & m2) != 0) {
3740 Location l = lexer.Location;
3741 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3743 $$ = (int) (m1 | m2);
3748 : NEW { $$ = Modifiers.NEW; }
3749 | PUBLIC { $$ = Modifiers.PUBLIC; }
3750 | PROTECTED { $$ = Modifiers.PROTECTED; }
3751 | INTERNAL { $$ = Modifiers.INTERNAL; }
3752 | PRIVATE { $$ = Modifiers.PRIVATE; }
3753 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3754 | SEALED { $$ = Modifiers.SEALED; }
3755 | STATIC { $$ = Modifiers.STATIC; }
3756 | READONLY { $$ = Modifiers.READONLY; }
3757 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3758 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3759 | EXTERN { $$ = Modifiers.EXTERN; }
3760 | VOLATILE { $$ = Modifiers.VOLATILE; }
3761 | UNSAFE { $$ = Modifiers.UNSAFE; }
3765 : /* empty */ { $$ = null; }
3766 | class_base { $$ = $1; }
3770 : COLON type_list { $$ = $2; }
3773 opt_type_parameter_constraints_clauses
3774 : /* empty */ { $$ = null; }
3775 | type_parameter_constraints_clauses
3779 type_parameter_constraints_clauses
3780 : type_parameter_constraints_clause {
3781 ArrayList constraints = new ArrayList (1);
3782 constraints.Add ($1);
3785 | type_parameter_constraints_clauses type_parameter_constraints_clause {
3786 ArrayList constraints = (ArrayList) $1;
3788 constraints.Add ($2);
3793 type_parameter_constraints_clause
3794 : WHERE IDENTIFIER COLON type_parameter_constraints {
3795 $$ = new Constraints ((string) $2, (ArrayList) $4, lexer.Location);
3799 type_parameter_constraints
3800 : type_parameter_constraint {
3801 ArrayList constraints = new ArrayList (1);
3802 constraints.Add ($1);
3805 | type_parameter_constraints COMMA type_parameter_constraint {
3806 ArrayList constraints = (ArrayList) $1;
3808 constraints.Add ($3);
3813 type_parameter_constraint
3815 | NEW OPEN_PARENS CLOSE_PARENS {
3816 $$ = SpecialConstraint.Constructor;
3819 $$ = SpecialConstraint.ReferenceType;
3822 $$ = SpecialConstraint.ValueType;
3831 // A block is "contained" on the following places:
3833 // property_declaration as part of the accessor body (get/set)
3834 // operator_declaration
3835 // constructor_declaration
3836 // destructor_declaration
3837 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3842 if (current_block == null){
3843 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, lexer.Location);
3844 top_current_block = current_block;
3846 current_block = new Block (current_block, current_local_parameters,
3847 lexer.Location, Location.Null);
3850 opt_statement_list CLOSE_BRACE
3852 while (current_block.Implicit)
3853 current_block = current_block.Parent;
3855 current_block.SetEndLocation (lexer.Location);
3856 current_block = current_block.Parent;
3857 if (current_block == null)
3858 top_current_block = null;
3869 | statement_list statement
3873 : declaration_statement
3875 if ($1 != null && (Block) $1 != current_block){
3876 current_block.AddStatement ((Statement) $1);
3877 current_block = (Block) $1;
3880 | valid_declaration_statement
3882 current_block.AddStatement ((Statement) $1);
3887 valid_declaration_statement
3890 | expression_statement
3891 | selection_statement
3892 | iteration_statement
3896 | unchecked_statement
3904 : valid_declaration_statement
3905 | declaration_statement
3907 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3912 Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3920 $$ = EmptyStatement.Value;
3927 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3929 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3930 current_block.AddStatement (labeled);
3935 declaration_statement
3936 : local_variable_declaration SEMICOLON
3939 DictionaryEntry de = (DictionaryEntry) $1;
3941 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3945 | local_constant_declaration SEMICOLON
3948 DictionaryEntry de = (DictionaryEntry) $1;
3950 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3956 * The following is from Rhys' grammar:
3957 * > Types in local variable declarations must be recognized as
3958 * > expressions to prevent reduce/reduce errors in the grammar.
3959 * > The expressions are converted into types during semantic analysis.
3962 : primary_expression opt_rank_specifier_or_nullable
3964 // FIXME: Do something smart here regarding the composition of the type.
3966 // Ok, the above "primary_expression" is there to get rid of
3967 // both reduce/reduce and shift/reduces in the grammar, it should
3968 // really just be "type_name". If you use type_name, a reduce/reduce
3969 // creeps up. If you use namespace_or_type_name (which is all we need
3970 // really) two shift/reduces appear.
3973 // So the super-trick is that primary_expression
3974 // can only be either a SimpleName or a MemberAccess.
3975 // The MemberAccess case arises when you have a fully qualified type-name like :
3977 // SimpleName is when you have
3980 Expression expr = (Expression) $1;
3981 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType)) {
3982 Error_ExpectingTypeName (lexer.Location, expr);
3986 // So we extract the string corresponding to the SimpleName
3990 if ((string) $2 == "")
3993 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3996 | builtin_types opt_rank_specifier_or_nullable
3998 if ((string) $2 == "")
4001 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
4005 local_variable_pointer_type
4006 : primary_expression STAR
4008 Expression expr = (Expression) $1;
4009 Location l = lexer.Location;
4011 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType)) {
4012 Error_ExpectingTypeName (l, expr);
4016 $$ = new ComposedCast ((Expression) $1, "*", l);
4018 | builtin_types STAR
4020 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
4024 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
4026 | local_variable_pointer_type STAR
4028 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
4032 local_variable_declaration
4033 : local_variable_type variable_declarators
4036 $$ = new DictionaryEntry ($1, $2);
4040 | local_variable_pointer_type opt_rank_specifier_or_nullable variable_declarators
4045 if ((string) $2 == "")
4046 t = (Expression) $1;
4048 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
4049 $$ = new DictionaryEntry (t, $3);
4055 local_constant_declaration
4056 : CONST local_variable_type constant_declarators
4059 $$ = new DictionaryEntry ($2, $3);
4065 expression_statement
4066 : statement_expression SEMICOLON
4073 // We have to do the wrapping here and not in the case above,
4074 // because statement_expression is used for example in for_statement
4076 statement_expression
4077 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4078 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4079 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4080 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4081 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4082 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4083 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4085 Report.Error (1002, lexer.Location, "Expecting `;'");
4090 object_creation_expression
4091 : object_or_delegate_creation_expression
4092 { note ("complain if this is a delegate maybe?"); }
4101 : IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS
4104 Location l = (Location) $3;
4106 $$ = new If ((Expression) $4, (Statement) $6, l);
4108 if (RootContext.WarningLevel >= 4){
4109 if ($6 == EmptyStatement.Value)
4110 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4114 | IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS
4115 embedded_statement ELSE embedded_statement
4117 Location l = (Location) $3;
4119 $$ = new If ((Expression) $4, (Statement) $6, (Statement) $8, l);
4124 : SWITCH OPEN_PARENS _mark_
4126 switch_stack.Push (current_block);
4128 expression CLOSE_PARENS
4131 $$ = new Switch ((Expression) $5, (ArrayList) $7, (Location) $3);
4132 current_block = (Block) switch_stack.Pop ();
4148 Report.Error (1522, lexer.Location, "Empty switch block");
4156 ArrayList sections = new ArrayList (4);
4161 | switch_sections switch_section
4163 ArrayList sections = (ArrayList) $1;
4173 current_block = current_block.CreateSwitchBlock (lexer.Location);
4177 Block topmost = current_block;
4179 while (topmost.Implicit)
4180 topmost = topmost.Parent;
4181 $$ = new SwitchSection ((ArrayList) $1, topmost);
4188 ArrayList labels = new ArrayList (4);
4193 | switch_labels switch_label
4195 ArrayList labels = (ArrayList) ($1);
4203 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
4204 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
4207 1523, lexer.Location,
4208 "The keyword case or default must precede code in switch block");
4220 : WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS embedded_statement
4222 Location l = (Location) $3;
4223 $$ = new While ((Expression) $4, (Statement) $6, l);
4225 if (RootContext.WarningLevel >= 4){
4226 if ($6 == EmptyStatement.Value)
4227 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4233 : DO embedded_statement
4234 WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS SEMICOLON
4236 Location l = (Location) $5;
4238 $$ = new Do ((Statement) $2, (Expression) $6, l);
4244 opt_for_initializer SEMICOLON _mark_
4246 Block assign_block = new Block (current_block);
4247 current_block = assign_block;
4249 if ($3 is DictionaryEntry){
4250 DictionaryEntry de = (DictionaryEntry) $3;
4252 Expression type = (Expression) de.Key;
4253 ArrayList var_declarators = (ArrayList) de.Value;
4255 foreach (VariableDeclaration decl in var_declarators){
4259 vi = current_block.AddVariable (
4260 type, decl.identifier, current_local_parameters, decl.Location);
4264 Location l = lexer.Location;
4266 if (decl.expression_or_array_initializer is Expression){
4267 expr = (Expression) decl.expression_or_array_initializer;
4268 } else if (decl.expression_or_array_initializer == null) {
4271 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4272 expr = new ArrayCreation (type, "", init, decl.Location);
4275 LocalVariableReference var;
4276 var = new LocalVariableReference (assign_block, decl.identifier, l);
4279 Assign a = new Assign (var, expr, decl.Location);
4281 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4285 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4286 // This can be referred to as $6 below.
4292 opt_for_condition SEMICOLON
4293 opt_for_iterator CLOSE_PARENS
4296 Location l = (Location) $5;
4298 For f = new For ((Statement) $6, (Expression) $7, (Statement) $9, (Statement) $11, l);
4300 if (RootContext.WarningLevel >= 4){
4301 if ($11 == EmptyStatement.Value)
4302 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4305 current_block.AddStatement (f);
4306 while (current_block.Implicit)
4307 current_block = current_block.Parent;
4309 current_block = current_block.Parent;
4314 : /* empty */ { $$ = EmptyStatement.Value; }
4319 : local_variable_declaration
4320 | statement_expression_list
4324 : /* empty */ { $$ = null; }
4325 | boolean_expression
4329 : /* empty */ { $$ = EmptyStatement.Value; }
4334 : statement_expression_list
4337 statement_expression_list
4338 : statement_expression
4340 // CHANGE: was `null'
4341 Block b = new Block (current_block, Block.Flags.Implicit);
4343 b.AddStatement ((Statement) $1);
4346 | statement_expression_list COMMA statement_expression
4348 Block b = (Block) $1;
4350 b.AddStatement ((Statement) $3);
4356 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4358 Report.Error (230, lexer.Location, "Type and identifier are both required in a foreach statement");
4361 | FOREACH OPEN_PARENS type IDENTIFIER IN _mark_
4362 expression CLOSE_PARENS
4364 Block foreach_block = new Block (current_block);
4365 current_block = foreach_block;
4367 Location l = lexer.Location;
4370 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
4374 // Get a writable reference to this read-only variable.
4376 // Note that the $$ here refers to the value of _this_ code block,
4377 // not the value of the LHS non-terminal. This can be referred to as $9 below.
4378 $$ = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
4385 LocalVariableReference v = (LocalVariableReference) $9;
4386 Location l = (Location) $6;
4389 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
4390 current_block.AddStatement (f);
4393 while (current_block.Implicit)
4394 current_block = current_block.Parent;
4396 current_block = current_block.Parent;
4402 | continue_statement
4412 $$ = new Break (lexer.Location);
4417 : CONTINUE SEMICOLON
4419 $$ = new Continue (lexer.Location);
4424 : GOTO IDENTIFIER SEMICOLON
4426 $$ = new Goto (current_block, (string) $2, lexer.Location);
4428 | GOTO CASE constant_expression SEMICOLON
4430 $$ = new GotoCase ((Expression) $3, lexer.Location);
4432 | GOTO DEFAULT SEMICOLON
4434 $$ = new GotoDefault (lexer.Location);
4439 : RETURN opt_expression SEMICOLON
4441 $$ = new Return ((Expression) $2, lexer.Location);
4446 : THROW opt_expression SEMICOLON
4448 $$ = new Throw ((Expression) $2, lexer.Location);
4453 : IDENTIFIER RETURN expression SEMICOLON
4455 string s = (string) $1;
4457 Report.Error (1003, lexer.Location, "; expected");
4460 if (RootContext.Version == LanguageVersion.ISO_1){
4461 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4464 if (iterator_container == null){
4465 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4468 iterator_container.SetYields ();
4469 $$ = new Yield ((Expression) $3, lexer.Location);
4472 | IDENTIFIER RETURN SEMICOLON
4474 Report.Error (1627, lexer.Location, "Expression expected after yield return");
4477 | IDENTIFIER BREAK SEMICOLON
4479 string s = (string) $1;
4481 Report.Error (1003, lexer.Location, "; expected");
4484 if (RootContext.Version == LanguageVersion.ISO_1){
4485 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4488 if (iterator_container == null){
4489 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4492 iterator_container.SetYields ();
4493 $$ = new YieldBreak (lexer.Location);
4504 : TRY block catch_clauses
4508 ArrayList c = (ArrayList)$3;
4509 for (int i = 0; i < c.Count; ++i) {
4510 Catch cc = (Catch) c [i];
4512 if (i != c.Count - 1)
4513 Report.Error (1017, cc.loc, "Empty catch block must be the last in a series of catch blocks");
4520 // Now s contains the list of specific catch clauses
4521 // and g contains the general one.
4523 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4525 | TRY block opt_catch_clauses FINALLY block
4528 ArrayList s = new ArrayList (4);
4529 ArrayList catch_list = (ArrayList) $3;
4531 if (catch_list != null){
4532 foreach (Catch cc in catch_list) {
4540 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4544 Report.Error (1524, lexer.Location, "Expected catch or finally");
4549 : /* empty */ { $$ = null; }
4556 ArrayList l = new ArrayList (4);
4561 | catch_clauses catch_clause
4563 ArrayList l = (ArrayList) $1;
4571 : /* empty */ { $$ = null; }
4576 : CATCH opt_catch_args
4578 Expression type = null;
4582 DictionaryEntry cc = (DictionaryEntry) $2;
4583 type = (Expression) cc.Key;
4584 id = (string) cc.Value;
4587 ArrayList one = new ArrayList (4);
4588 Location loc = lexer.Location;
4590 one.Add (new VariableDeclaration (id, null, loc));
4592 current_block = new Block (current_block);
4593 Block b = declare_local_variables (type, one, loc);
4598 Expression type = null;
4602 DictionaryEntry cc = (DictionaryEntry) $2;
4603 type = (Expression) cc.Key;
4604 id = (string) cc.Value;
4607 while (current_block.Implicit)
4608 current_block = current_block.Parent;
4609 current_block = current_block.Parent;
4613 $$ = new Catch (type, id, (Block) $4, ((Block) $4).loc);
4618 : /* empty */ { $$ = null; }
4623 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4625 $$ = new DictionaryEntry ($2, $3);
4632 $$ = new Checked ((Block) $2);
4639 $$ = new Unchecked ((Block) $2);
4646 if (!RootContext.Unsafe){
4647 Report.Error (227, lexer.Location,
4648 "Unsafe code can only be used if --unsafe is used");
4651 $$ = new Unsafe ((Block) $3);
4657 type fixed_pointer_declarators
4660 ArrayList list = (ArrayList) $4;
4661 Expression type = (Expression) $3;
4662 Location l = lexer.Location;
4663 int top = list.Count;
4665 Block assign_block = new Block (current_block);
4666 current_block = assign_block;
4668 for (int i = 0; i < top; i++){
4669 Pair p = (Pair) list [i];
4672 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
4684 Location l = (Location) $6;
4686 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $8, l);
4688 if (RootContext.WarningLevel >= 4){
4689 if ($8 == EmptyStatement.Value)
4690 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4693 current_block.AddStatement (f);
4694 while (current_block.Implicit)
4695 current_block = current_block.Parent;
4697 current_block = current_block.Parent;
4701 fixed_pointer_declarators
4702 : fixed_pointer_declarator {
4703 ArrayList declarators = new ArrayList (4);
4705 declarators.Add ($1);
4708 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4710 ArrayList declarators = (ArrayList) $1;
4712 declarators.Add ($3);
4717 fixed_pointer_declarator
4718 : IDENTIFIER ASSIGN expression
4720 $$ = new Pair ($1, $3);
4724 Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
4730 : LOCK OPEN_PARENS expression CLOSE_PARENS
4736 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4741 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS _mark_
4743 Block assign_block = new Block (current_block);
4744 current_block = assign_block;
4746 if ($3 is DictionaryEntry){
4747 DictionaryEntry de = (DictionaryEntry) $3;
4748 Location l = lexer.Location;
4750 Expression type = (Expression) de.Key;
4751 ArrayList var_declarators = (ArrayList) de.Value;
4753 ArrayList vars = new ArrayList (4);
4755 foreach (VariableDeclaration decl in var_declarators){
4757 LocalInfo vi = current_block.AddVariable (
4758 type, decl.identifier,
4759 current_local_parameters, decl.Location);
4765 if (decl.expression_or_array_initializer is Expression){
4766 expr = (Expression) decl.expression_or_array_initializer;
4768 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4770 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4773 expr = new ArrayCreation (type, "", init, decl.Location);
4776 LocalVariableReference var;
4778 // Get a writable reference to this read-only variable.
4779 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4781 // This is so that it is not a warning on using variables
4784 vars.Add (new DictionaryEntry (var, expr));
4786 // Assign a = new Assign (var, expr, decl.Location);
4787 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4790 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4791 // It can be referred to as $6 below.
4792 $$ = new DictionaryEntry (type, vars);
4799 Using u = new Using ($6, (Statement) $7, (Location) $5);
4800 current_block.AddStatement (u);
4801 while (current_block.Implicit)
4802 current_block = current_block.Parent;
4804 current_block = current_block.Parent;
4808 resource_acquisition
4809 : local_variable_declaration
4813 // Utility rule to save location information
4816 { $$ = lexer.Location; }
4821 // A class used to pass around variable declarations and constants
4823 public class VariableDeclaration {
4824 public string identifier;
4825 public object expression_or_array_initializer;
4826 public Location Location;
4827 public Attributes OptAttributes;
4828 public string DocComment;
4830 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4832 this.identifier = id;
4833 this.expression_or_array_initializer = eoai;
4835 this.OptAttributes = opt_attrs;
4838 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4844 /// Used to pass around interface property information
4846 public class InterfaceAccessorInfo {
4848 public readonly Accessor Get, Set;
4850 public InterfaceAccessorInfo (bool has_get, bool has_set,
4851 Attributes get_attrs, Attributes set_attrs, int get_mod, int set_mod, Location get_loc, Location set_loc)
4854 Report.Error (275, get_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4856 Report.Error (275, set_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4859 Get = new Accessor (null, 0, get_attrs, get_loc);
4861 Set = new Accessor (null, 0, set_attrs, set_loc);
4868 // A class used to hold info about an indexer declarator
4870 public class IndexerDeclaration {
4871 public Expression type;
4872 public MemberName interface_type;
4873 public Parameters param_list;
4875 public IndexerDeclaration (Expression type, MemberName interface_type,
4876 Parameters param_list)
4879 this.interface_type = interface_type;
4880 this.param_list = param_list;
4885 // We use this when we do not have an object in advance that is an IIteratorContainer
4887 public class SimpleIteratorContainer : IIteratorContainer {
4890 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4895 public static SimpleIteratorContainer GetSimple () {
4896 Simple.Yields = false;
4900 public void SetYields () { Yields = true; }
4904 // A class used to hold info about an operator declarator
4906 public class OperatorDeclaration {
4907 public Operator.OpType optype;
4908 public Expression ret_type, arg1type, arg2type;
4909 public string arg1name, arg2name;
4910 public Location location;
4912 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4913 Expression arg1type, string arg1name,
4914 Expression arg2type, string arg2name, Location location)
4917 this.ret_type = ret_type;
4918 this.arg1type = arg1type;
4919 this.arg1name = arg1name;
4920 this.arg2type = arg2type;
4921 this.arg2name = arg2name;
4922 this.location = location;
4927 void Error_ExpectingTypeName (Location l, Expression expr)
4929 if (expr is Invocation){
4930 Report.Error (1002, l, "; expected");
4932 Report.Error (-1, l, "Invalid Type definition");
4936 TypeContainer pop_current_class ()
4938 TypeContainer retval = current_class;
4940 current_class = current_class.Parent;
4941 current_container = current_container.Parent;
4943 if (current_class != current_container) {
4944 if (!(current_class is ClassPart) ||
4945 ((ClassPart) current_class).PartialContainer != current_container)
4946 throw new InternalErrorException ();
4947 } else if (current_container is ClassPart)
4948 current_container = ((ClassPart) current_class).PartialContainer;
4954 // Given the @class_name name, it creates a fully qualified name
4955 // based on the containing declaration space
4958 MakeName (MemberName class_name)
4960 string ns = current_namespace.FullName;
4962 if (current_container.Name == ""){
4964 return new MemberName (new MemberName (ns), class_name);
4968 return new MemberName (current_container.MemberName, class_name);
4972 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4974 Block implicit_block;
4975 ArrayList inits = null;
4978 // We use the `Used' property to check whether statements
4979 // have been added to the current block. If so, we need
4980 // to create another block to contain the new declaration
4981 // otherwise, as an optimization, we use the same block to
4982 // add the declaration.
4984 // FIXME: A further optimization is to check if the statements
4985 // that were added were added as part of the initialization
4986 // below. In which case, no other statements have been executed
4987 // and we might be able to reduce the number of blocks for
4988 // situations like this:
4990 // int j = 1; int k = j + 1;
4992 if (current_block.Used)
4993 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4995 implicit_block = current_block;
4997 foreach (VariableDeclaration decl in variable_declarators){
4999 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
5000 if (decl.expression_or_array_initializer != null){
5002 inits = new ArrayList (4);
5009 return implicit_block;
5011 foreach (VariableDeclaration decl in inits){
5015 if (decl.expression_or_array_initializer is Expression){
5016 expr = (Expression) decl.expression_or_array_initializer;
5019 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
5021 expr = new ArrayCreation (type, "", init, decl.Location);
5024 LocalVariableReference var;
5025 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
5027 assign = new Assign (var, expr, decl.Location);
5029 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
5032 return implicit_block;
5035 Block declare_local_constants (Expression type, ArrayList declarators)
5037 Block implicit_block;
5039 if (current_block.Used)
5040 implicit_block = new Block (current_block, Block.Flags.Implicit);
5042 implicit_block = current_block;
5044 foreach (VariableDeclaration decl in declarators){
5045 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
5046 current_local_parameters, decl.Location);
5049 return implicit_block;
5052 void CheckAttributeTarget (string a)
5056 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
5060 Location l = lexer.Location;
5061 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
5067 void CheckUnaryOperator (Operator.OpType op)
5071 case Operator.OpType.LogicalNot:
5072 case Operator.OpType.OnesComplement:
5073 case Operator.OpType.Increment:
5074 case Operator.OpType.Decrement:
5075 case Operator.OpType.True:
5076 case Operator.OpType.False:
5077 case Operator.OpType.Addition:
5078 case Operator.OpType.Subtraction:
5083 Location l = lexer.Location;
5084 Report.Error (1019, l, "Overloadable unary operator expected");
5090 void CheckBinaryOperator (Operator.OpType op)
5094 case Operator.OpType.Addition:
5095 case Operator.OpType.Subtraction:
5096 case Operator.OpType.Multiply:
5097 case Operator.OpType.Division:
5098 case Operator.OpType.Modulus:
5099 case Operator.OpType.BitwiseAnd:
5100 case Operator.OpType.BitwiseOr:
5101 case Operator.OpType.ExclusiveOr:
5102 case Operator.OpType.LeftShift:
5103 case Operator.OpType.RightShift:
5104 case Operator.OpType.Equality:
5105 case Operator.OpType.Inequality:
5106 case Operator.OpType.GreaterThan:
5107 case Operator.OpType.LessThan:
5108 case Operator.OpType.GreaterThanOrEqual:
5109 case Operator.OpType.LessThanOrEqual:
5113 Location l = lexer.Location;
5114 Report.Error (1020, l, "Overloadable binary operator expected");
5120 void syntax_error (Location l, string msg)
5122 Report.Error (1003, l, "Syntax error, " + msg);
5125 void output (string s)
5127 Console.WriteLine (s);
5130 void note (string s)
5132 // Used to put annotations
5137 public Tokenizer Lexer {
5143 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
5145 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
5146 this.name = file.Name;
5148 current_container = RootContext.Tree.Types;
5149 // TODO: Make RootContext.Tree.Types a PartialContainer.
5150 current_class = current_container;
5151 current_container.NamespaceEntry = current_namespace;
5152 oob_stack = new Stack ();
5153 switch_stack = new Stack ();
5155 lexer = new Tokenizer (reader, file, defines);
5158 public void parse ()
5161 if (yacc_verbose_flag > 1)
5162 yyparse (lexer, new yydebug.yyDebugSimple ());
5165 Tokenizer tokenizer = lexer as Tokenizer;
5166 tokenizer.cleanup ();
5168 } catch (Exception e){
5170 // Removed for production use, use parser verbose to get the output.
5172 // Console.WriteLine (e);
5173 Report.Error (-25, lexer.Location, "Parsing error");
5174 if (yacc_verbose_flag > 0)
5175 Console.WriteLine (e);
5180 void CheckToken (int error, int yyToken, string msg)
5182 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
5183 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
5186 Report.Error (error, lexer.Location, msg);
5189 void CheckIdentifierToken (int yyToken)
5191 CheckToken (1041, yyToken, "Identifier expected");
5194 string ConsumeStoredComment ()
5196 string s = tmpComment;
5198 Lexer.doc_state = XmlCommentState.Allowed;