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");
375 else if ((current_namespace.Parent != null) && (name.Left != null)) {
376 Report.Error (134, lexer.Location,
377 "Cannot use qualified namespace names in nested " +
378 "namespace declarations");
381 current_namespace = new NamespaceEntry (
382 current_namespace, file, name.GetName (), lexer.Location);
384 namespace_body opt_semicolon
386 current_namespace = current_namespace.Parent;
401 : namespace_or_type_name {
402 MemberName name = (MemberName) $1;
404 if (name.TypeArguments != null)
405 syntax_error (lexer.Location, "namespace name expected");
414 if (RootContext.Documentation != null)
415 Lexer.doc_state = XmlCommentState.Allowed;
418 opt_namespace_member_declarations
429 opt_namespace_member_declarations
431 | namespace_member_declarations
434 namespace_member_declarations
435 : namespace_member_declaration
436 | namespace_member_declarations namespace_member_declaration
439 namespace_member_declaration
446 Class c = (Class) $1;
447 mod_flags = c.ModFlags;
449 } else if ($1 is Struct){
450 Struct s = (Struct) $1;
451 mod_flags = s.ModFlags;
456 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
458 1527, lexer.Location,
459 "Namespace elements cant be explicitly " +
460 "declared private or protected in `" + name + "'");
462 current_namespace.DeclarationFound = true;
464 | namespace_declaration {
465 current_namespace.DeclarationFound = true;
468 | field_declaration {
469 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
471 | method_declaration {
472 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
479 | interface_declaration
481 | delegate_declaration
483 // Enable this when we have handled all errors, because this acts as a generic fallback
486 // Console.WriteLine ("Token=" + yyToken);
487 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
499 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
507 global_attrs_enabled = false;
512 global_attrs_enabled = false;
521 ArrayList sect = (ArrayList) $1;
523 if (global_attrs_enabled) {
524 if (current_attr_target == "module") {
525 CodeGen.Module.AddAttributes (sect);
527 } else if (current_attr_target == "assembly") {
528 CodeGen.Assembly.AddAttributes (sect);
531 $$ = new Attributes (sect);
534 if (RootContext.Documentation != null) {
535 Lexer.check_incorrect_doc_comment ();
537 XmlCommentState.Allowed;
541 $$ = new Attributes (sect);
543 current_attr_target = null;
545 | attribute_sections attribute_section
547 Attributes attrs = $1 as Attributes;
548 ArrayList sect = (ArrayList) $2;
550 if (global_attrs_enabled) {
551 if (current_attr_target == "module") {
552 CodeGen.Module.AddAttributes (sect);
554 } else if (current_attr_target == "assembly") {
555 CodeGen.Assembly.AddAttributes (sect);
559 attrs = new Attributes (sect);
561 attrs.AddAttributes (sect);
565 attrs = new Attributes (sect);
567 attrs.AddAttributes (sect);
570 current_attr_target = null;
575 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
579 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
585 attribute_target_specifier
586 : attribute_target COLON
588 current_attr_target = (string)$1;
596 CheckAttributeTarget ((string) $1);
599 | EVENT { $$ = "event"; }
600 | RETURN { $$ = "return"; }
606 ArrayList attrs = new ArrayList (4);
612 | attribute_list COMMA attribute
614 ArrayList attrs = (ArrayList) $1;
626 opt_attribute_arguments
628 Location loc = (Location) $2;
629 MemberName mname = (MemberName) $1;
630 if (mname.IsGeneric) {
631 Report.Error (404, lexer.Location,
632 "'<' unexpected: attributes cannot be generic");
635 MemberName left = mname.Left;
636 string identifier = mname.Name;
638 Expression left_expr = left == null ? null : left.GetTypeExpression (loc);
640 if (current_attr_target == "assembly" || current_attr_target == "module")
641 $$ = new GlobalAttribute (current_container, current_attr_target,
642 left_expr, identifier, (ArrayList) $3, loc);
644 $$ = new Attribute (current_attr_target, left_expr, identifier, (ArrayList) $3, loc);
649 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
652 opt_attribute_arguments
653 : /* empty */ { $$ = null; }
654 | OPEN_PARENS attribute_arguments CLOSE_PARENS
662 : opt_positional_argument_list
667 ArrayList args = new ArrayList (4);
673 | positional_argument_list COMMA named_argument_list
675 ArrayList args = new ArrayList (4);
681 | named_argument_list
683 ArrayList args = new ArrayList (4);
692 opt_positional_argument_list
693 : /* empty */ { $$ = null; }
694 | positional_argument_list
697 positional_argument_list
700 ArrayList args = new ArrayList (4);
701 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
705 | positional_argument_list COMMA expression
707 ArrayList args = (ArrayList) $1;
708 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
717 ArrayList args = new ArrayList (4);
722 | named_argument_list COMMA named_argument
724 ArrayList args = (ArrayList) $1;
729 | named_argument_list COMMA expression
731 Report.Error (1016, lexer.Location, "Named attribute argument expected");
737 : IDENTIFIER ASSIGN expression
739 $$ = new DictionaryEntry (
741 new Argument ((Expression) $3, Argument.AType.Expression));
747 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
750 opt_class_member_declarations
752 | class_member_declarations
755 class_member_declarations
756 : class_member_declaration
757 | class_member_declarations
758 class_member_declaration
761 class_member_declaration
762 : constant_declaration // done
763 | field_declaration // done
764 | method_declaration // done
765 | property_declaration // done
766 | event_declaration // done
767 | indexer_declaration // done
768 | operator_declaration // done
769 | constructor_declaration // done
770 | destructor_declaration // done
780 MemberName name = MakeName ((MemberName) $5);
781 bool partial = (bool) $3;
784 ClassPart part = PartialContainer.CreatePart (
785 current_namespace, current_container, name, (int) $2,
786 (Attributes) $1, Kind.Struct, lexer.Location);
788 current_container = part.PartialContainer;
789 current_class = part;
791 current_class = new Struct (
792 current_namespace, current_container, name, (int) $2,
793 (Attributes) $1, lexer.Location);
795 current_container = current_class;
796 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
799 lexer.ConstraintsParsing = true;
802 opt_type_parameter_constraints_clauses
804 lexer.ConstraintsParsing = false;
807 current_class.Bases = (ArrayList) $7;
809 current_class.SetParameterInfo ((ArrayList) $8);
811 if (RootContext.Documentation != null)
812 current_class.DocComment = Lexer.consume_doc_comment ();
814 current_class.Register ();
818 if (RootContext.Documentation != null)
819 Lexer.doc_state = XmlCommentState.Allowed;
825 current_container = current_container.Parent;
826 current_class = current_container;
828 | opt_attributes opt_modifiers opt_partial STRUCT error {
829 CheckIdentifierToken (yyToken);
836 if (RootContext.Documentation != null)
837 Lexer.doc_state = XmlCommentState.Allowed;
839 opt_struct_member_declarations CLOSE_BRACE
842 opt_struct_member_declarations
844 | struct_member_declarations
847 struct_member_declarations
848 : struct_member_declaration
849 | struct_member_declarations struct_member_declaration
852 struct_member_declaration
853 : constant_declaration
856 | property_declaration
858 | indexer_declaration
859 | operator_declaration
860 | constructor_declaration
864 * This is only included so we can flag error 575:
865 * destructors only allowed on class types
867 | destructor_declaration
878 int modflags = (int) $2;
879 foreach (VariableDeclaration constant in (ArrayList) $5){
880 Location l = constant.Location;
881 if ((modflags & Modifiers.STATIC) != 0) {
882 Report.Error (504, l, "The constant '{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
886 Const c = new Const (
887 current_class, (Expression) $4, (string) constant.identifier,
888 (Expression) constant.expression_or_array_initializer, modflags,
891 if (RootContext.Documentation != null) {
892 c.DocComment = Lexer.consume_doc_comment ();
893 Lexer.doc_state = XmlCommentState.Allowed;
895 current_container.AddConstant (c);
901 : constant_declarator
903 ArrayList constants = new ArrayList (4);
908 | constant_declarators COMMA constant_declarator
911 ArrayList constants = (ArrayList) $1;
918 : IDENTIFIER ASSIGN constant_expression
920 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
924 // A const field requires a value to be provided
925 Report.Error (145, lexer.Location, "A const field requires a value to be provided");
937 Expression type = (Expression) $3;
940 foreach (VariableDeclaration var in (ArrayList) $4){
941 Field field = new Field (current_class, type, mod, var.identifier,
942 var.expression_or_array_initializer,
943 (Attributes) $1, var.Location);
945 if (RootContext.Documentation != null) {
946 field.DocComment = Lexer.consume_doc_comment ();
947 Lexer.doc_state = XmlCommentState.Allowed;
949 current_container.AddField (field);
956 fixed_variable_declarators
959 Expression type = (Expression) $4;
962 foreach (VariableDeclaration var in (ArrayList) $5) {
963 FixedField field = new FixedField (current_class, type, mod, var.identifier,
964 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
966 if (RootContext.Documentation != null) {
967 field.DocComment = Lexer.consume_doc_comment ();
968 Lexer.doc_state = XmlCommentState.Allowed;
970 current_container.AddField (field);
978 Report.Error (670, lexer.Location, "void type is not allowed for fields");
982 fixed_variable_declarators
983 : fixed_variable_declarator
985 ArrayList decl = new ArrayList (2);
989 | fixed_variable_declarators COMMA fixed_variable_declarator
991 ArrayList decls = (ArrayList) $1;
997 fixed_variable_declarator
998 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
1000 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
1004 variable_declarators
1005 : variable_declarator
1007 ArrayList decl = new ArrayList (4);
1011 | variable_declarators COMMA variable_declarator
1013 ArrayList decls = (ArrayList) $1;
1020 : IDENTIFIER ASSIGN variable_initializer
1022 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
1026 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
1028 | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
1030 Report.Error (650, lexer.Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
1031 "To declare a fixed buffer field, use the fixed keyword before the field type");
1035 variable_initializer
1044 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
1046 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
1050 Report.Error (1575, lexer.Location, "A stackalloc expression requires [] after type");
1057 iterator_container = (IIteratorContainer) $1;
1058 if (RootContext.Documentation != null)
1059 Lexer.doc_state = XmlCommentState.NotAllowed;
1063 Method method = (Method) $1;
1064 Block b = (Block) $3;
1065 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
1068 if ((method.ModFlags & extern_abstract) == 0){
1070 501, lexer.Location, current_container.MakeName (method.Name) +
1071 "must declare a body because it is not marked abstract or extern");
1074 if ((method.ModFlags & Modifiers.EXTERN) != 0){
1076 179, lexer.Location, current_container.MakeName (method.Name) +
1077 " is declared extern, but has a body");
1081 method.Block = (ToplevelBlock) $3;
1082 current_container.AddMethod (method);
1084 current_local_parameters = null;
1085 iterator_container = null;
1087 if (RootContext.Documentation != null)
1088 Lexer.doc_state = XmlCommentState.Allowed;
1095 type namespace_or_type_name
1096 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1098 lexer.ConstraintsParsing = true;
1100 opt_type_parameter_constraints_clauses
1102 lexer.ConstraintsParsing = false;
1104 MemberName name = (MemberName) $4;
1106 if ($9 != null && name.TypeArguments == null)
1107 Report.Error (80, lexer.Location,
1108 "Contraints are not allowed on non-generic declarations");
1112 GenericMethod generic = null;
1113 if (name.TypeArguments != null) {
1114 generic = new GenericMethod (current_namespace, current_class,
1115 name, lexer.Location);
1117 generic.SetParameterInfo ((ArrayList) $9);
1120 method = new Method (current_class, generic, (Expression) $3, (int) $2, false,
1121 name, (Parameters) $6, (Attributes) $1, lexer.Location);
1123 current_local_parameters = (Parameters) $6;
1125 if (RootContext.Documentation != null)
1126 method.DocComment = Lexer.consume_doc_comment ();
1132 VOID namespace_or_type_name
1133 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1135 lexer.ConstraintsParsing = true;
1137 opt_type_parameter_constraints_clauses
1139 lexer.ConstraintsParsing = false;
1141 MemberName name = (MemberName) $4;
1143 if ($9 != null && name.TypeArguments == null)
1144 Report.Error (80, lexer.Location,
1145 "Contraints are not allowed on non-generic declarations");
1148 GenericMethod generic = null;
1149 if (name.TypeArguments != null) {
1150 generic = new GenericMethod (current_namespace, current_class,
1151 name, lexer.Location);
1153 generic.SetParameterInfo ((ArrayList) $9);
1156 method = new Method (current_class, generic, TypeManager.system_void_expr,
1157 (int) $2, false, name, (Parameters) $6, (Attributes) $1,
1160 current_local_parameters = (Parameters) $6;
1162 if (RootContext.Documentation != null)
1163 method.DocComment = Lexer.consume_doc_comment ();
1170 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1172 Report.Error (1585, lexer.Location,
1173 String.Format ("Modifier {0} should appear before type",
1174 Modifiers.Name ((int) $4)));
1175 MemberName name = (MemberName) $4;
1177 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1178 0, false, name, (Parameters) $6, (Attributes) $1,
1181 current_local_parameters = (Parameters) $6;
1183 if (RootContext.Documentation != null)
1184 method.DocComment = Lexer.consume_doc_comment ();
1192 | SEMICOLON { $$ = null; }
1195 opt_formal_parameter_list
1196 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1197 | formal_parameter_list
1200 formal_parameter_list
1203 ArrayList pars_list = (ArrayList) $1;
1205 Parameter [] pars = new Parameter [pars_list.Count];
1206 pars_list.CopyTo (pars);
1208 $$ = new Parameters (pars, null, lexer.Location);
1210 | fixed_parameters COMMA parameter_array
1212 ArrayList pars_list = (ArrayList) $1;
1214 Parameter [] pars = new Parameter [pars_list.Count];
1215 pars_list.CopyTo (pars);
1217 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
1219 | fixed_parameters COMMA ARGLIST
1221 ArrayList pars_list = (ArrayList) $1;
1223 Parameter [] pars = new Parameter [pars_list.Count];
1224 pars_list.CopyTo (pars);
1226 $$ = new Parameters (pars, true, lexer.Location);
1230 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
1234 $$ = new Parameters (null, true, lexer.Location);
1241 ArrayList pars = new ArrayList (4);
1246 | fixed_parameters COMMA fixed_parameter
1248 ArrayList pars = (ArrayList) $1;
1257 opt_parameter_modifier
1261 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
1264 opt_parameter_modifier
1267 Report.Error (1001, lexer.Location, "Identifier expected");
1271 opt_parameter_modifier
1274 CheckIdentifierToken (yyToken);
1278 opt_parameter_modifier
1284 Report.Error (241, lexer.Location, "Default parameter specifiers are not permitted");
1289 opt_parameter_modifier
1290 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1291 | parameter_modifier
1295 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1296 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1300 : opt_attributes PARAMS type IDENTIFIER
1302 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1303 note ("type must be a single-dimension array type");
1305 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1307 Report.Error (1611, lexer.Location, "The params parameter cannot be declared as ref or out");
1310 | opt_attributes PARAMS type error {
1311 CheckIdentifierToken (yyToken);
1316 property_declaration
1320 namespace_or_type_name
1322 if (RootContext.Documentation != null)
1323 tmpComment = Lexer.consume_doc_comment ();
1327 implicit_value_parameter_type = (Expression) $3;
1329 lexer.PropertyParsing = true;
1331 $$ = lexer.Location;
1333 iterator_container = SimpleIteratorContainer.GetSimple ();
1335 accessor_declarations
1337 lexer.PropertyParsing = false;
1338 has_get = has_set = false;
1346 Pair pair = (Pair) $8;
1347 Accessor get_block = (Accessor) pair.First;
1348 Accessor set_block = (Accessor) pair.Second;
1350 Location loc = (Location) $7;
1351 MemberName name = (MemberName) $4;
1353 if (name.TypeArguments != null)
1354 syntax_error (lexer.Location, "a property can't have type arguments");
1356 prop = new Property (current_container, (Expression) $3, (int) $2, false,
1357 name, (Attributes) $1, get_block, set_block, loc);
1358 if (SimpleIteratorContainer.Simple.Yields)
1361 current_container.AddProperty (prop);
1362 implicit_value_parameter_type = null;
1363 iterator_container = null;
1365 if (RootContext.Documentation != null)
1366 prop.DocComment = ConsumeStoredComment ();
1371 accessor_declarations
1372 : get_accessor_declaration
1374 $$ = new Pair ($1, null);
1376 | get_accessor_declaration accessor_declarations
1378 Pair pair = (Pair) $2;
1382 | set_accessor_declaration
1384 $$ = new Pair (null, $1);
1386 | set_accessor_declaration accessor_declarations
1388 Pair pair = (Pair) $2;
1394 Report.Error (1014, lexer.Location, "A get or set accessor expected");
1399 get_accessor_declaration
1400 : opt_attributes opt_modifiers GET
1402 // If this is not the case, then current_local_parameters has already
1403 // been set in indexer_declaration
1404 if (parsing_indexer == false)
1405 current_local_parameters = null;
1407 current_local_parameters = indexer_parameters;
1408 lexer.PropertyParsing = false;
1413 Report.Error (1007, lexer.Location, "Property accessor already defined");
1416 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1418 current_local_parameters = null;
1419 lexer.PropertyParsing = true;
1421 if (RootContext.Documentation != null)
1422 if (Lexer.doc_state == XmlCommentState.Error)
1423 Lexer.doc_state = XmlCommentState.NotAllowed;
1427 set_accessor_declaration
1428 : opt_attributes opt_modifiers SET
1431 Parameter implicit_value_parameter = new Parameter (
1432 implicit_value_parameter_type, "value",
1433 Parameter.Modifier.NONE, null);
1435 if (parsing_indexer == false) {
1436 args = new Parameter [1];
1437 args [0] = implicit_value_parameter;
1438 current_local_parameters = new Parameters (args, null, lexer.Location);
1440 Parameter [] fpars = indexer_parameters.FixedParameters;
1443 int count = fpars.Length;
1445 args = new Parameter [count + 1];
1446 fpars.CopyTo (args, 0);
1447 args [count] = implicit_value_parameter;
1450 current_local_parameters = new Parameters (
1451 args, indexer_parameters.ArrayParameter, lexer.Location);
1454 lexer.PropertyParsing = false;
1459 Report.Error (1007, lexer.Location, "Property accessor already defined");
1462 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1464 current_local_parameters = null;
1465 lexer.PropertyParsing = true;
1467 if (RootContext.Documentation != null
1468 && Lexer.doc_state == XmlCommentState.Error)
1469 Lexer.doc_state = XmlCommentState.NotAllowed;
1475 | SEMICOLON { $$ = null; }
1478 interface_declaration
1482 INTERFACE member_name
1484 MemberName name = MakeName ((MemberName) $5);
1485 bool partial = (bool) $3;
1488 ClassPart part = PartialContainer.CreatePart (
1489 current_namespace, current_container, name, (int) $2,
1490 (Attributes) $1, Kind.Interface, lexer.Location);
1492 current_container = part.PartialContainer;
1493 current_class = part;
1495 current_class = new Interface (
1496 current_namespace, current_container, name, (int) $2,
1497 (Attributes) $1, lexer.Location);
1499 current_container = current_class;
1500 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
1503 lexer.ConstraintsParsing = true;
1506 opt_type_parameter_constraints_clauses
1508 lexer.ConstraintsParsing = false;
1511 current_class.Bases = (ArrayList) $7;
1513 current_class.SetParameterInfo ((ArrayList) $8);
1515 if (RootContext.Documentation != null) {
1516 current_class.DocComment = Lexer.consume_doc_comment ();
1517 Lexer.doc_state = XmlCommentState.Allowed;
1520 current_class.Register ();
1524 if (RootContext.Documentation != null)
1525 Lexer.doc_state = XmlCommentState.Allowed;
1531 current_container = current_container.Parent;
1532 current_class = current_container;
1534 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1535 CheckIdentifierToken (yyToken);
1541 opt_interface_member_declarations
1545 opt_interface_member_declarations
1547 | interface_member_declarations
1550 interface_member_declarations
1551 : interface_member_declaration
1552 | interface_member_declarations interface_member_declaration
1555 interface_member_declaration
1556 : interface_method_declaration
1561 Method m = (Method) $1;
1563 if (m.IsExplicitImpl)
1564 Report.Error (541, lexer.Location,
1565 "Explicit interface declaration can only be declared in a class or struct");
1567 current_container.AddMethod (m);
1569 if (RootContext.Documentation != null)
1570 Lexer.doc_state = XmlCommentState.Allowed;
1572 | interface_property_declaration
1577 Property p = (Property) $1;
1579 if (p.IsExplicitImpl)
1580 Report.Error (541, lexer.Location,
1581 "Explicit interface declaration can only be declared in a class or struct");
1583 current_container.AddProperty (p);
1585 if (RootContext.Documentation != null)
1586 Lexer.doc_state = XmlCommentState.Allowed;
1588 | interface_event_declaration
1591 Event e = (Event) $1;
1593 if (e.IsExplicitImpl)
1594 Report.Error (541, lexer.Location,
1595 "Explicit interface declaration can only be declared in a class or struct");
1597 current_container.AddEvent (e);
1600 if (RootContext.Documentation != null)
1601 Lexer.doc_state = XmlCommentState.Allowed;
1603 | interface_indexer_declaration
1608 Indexer i = (Indexer) $1;
1610 if (i.IsExplicitImpl)
1611 Report.Error (541, lexer.Location,
1612 "Explicit interface declaration can only be declared in a class or struct");
1614 current_container.AddIndexer (i);
1616 if (RootContext.Documentation != null)
1617 Lexer.doc_state = XmlCommentState.Allowed;
1619 | delegate_declaration
1621 Report.Error (524, lexer.Location, "Interfaces can not declare delegates");
1625 Report.Error (524, lexer.Location, "Interfaces can not declare classes");
1627 | struct_declaration
1629 Report.Error (524, lexer.Location, "Interfaces can not declare structures");
1633 Report.Error (524, lexer.Location, "Interfaces can not declare enumerations");
1635 | interface_declaration
1637 Report.Error (524, lexer.Location, "Interfaces can not declare interfaces");
1639 | constant_declaration
1641 Report.Error (525, lexer.Location, "Interfaces cannot contain constants");
1649 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, lexer.Location);
1654 interface_method_declaration
1655 : opt_attributes opt_new type namespace_or_type_name
1656 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1658 lexer.ConstraintsParsing = true;
1660 opt_type_parameter_constraints_clauses
1663 lexer.ConstraintsParsing = false;
1665 MemberName name = (MemberName) $4;
1667 if ($9 != null && name.TypeArguments == null)
1668 Report.Error (80, lexer.Location,
1669 "Contraints are not allowed on non-generic declarations");
1671 GenericMethod generic = null;
1672 if (name.TypeArguments != null) {
1673 generic = new GenericMethod (current_namespace, current_class,
1674 name, lexer.Location);
1676 generic.SetParameterInfo ((ArrayList) $9);
1679 $$ = new Method (current_class, generic, (Expression) $3, (int) $2, true, name,
1680 (Parameters) $6, (Attributes) $1, lexer.Location);
1681 if (RootContext.Documentation != null)
1682 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1684 | opt_attributes opt_new VOID namespace_or_type_name
1685 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1687 lexer.ConstraintsParsing = true;
1689 opt_type_parameter_constraints_clauses
1692 lexer.ConstraintsParsing = false;
1694 MemberName name = (MemberName) $4;
1696 if ($9 != null && name.TypeArguments == null)
1697 Report.Error (80, lexer.Location,
1698 "Contraints are not allowed on non-generic declarations");
1700 GenericMethod generic = null;
1701 if (name.TypeArguments != null) {
1702 generic = new GenericMethod (current_namespace, current_class,
1703 name, lexer.Location);
1705 generic.SetParameterInfo ((ArrayList) $9);
1708 $$ = new Method (current_class, generic, TypeManager.system_void_expr, (int) $2,
1709 true, name, (Parameters) $6, (Attributes) $1, lexer.Location);
1710 if (RootContext.Documentation != null)
1711 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1713 | opt_attributes opt_new type namespace_or_type_name
1714 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1716 lexer.ConstraintsParsing = true;
1718 opt_type_parameter_constraints_clauses
1719 OPEN_BRACE opt_statement_list CLOSE_BRACE
1721 lexer.ConstraintsParsing = false;
1722 Report.Error (531, lexer.Location, "'{0}': interface members cannot have a definition", $4);
1728 interface_property_declaration
1733 { lexer.PropertyParsing = true; }
1735 { lexer.PropertyParsing = false; }
1738 if ($3 == TypeManager.system_void_expr) {
1739 Report.Error (547, lexer.Location, "'{0}': property cannot have void type", $4);
1746 InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
1748 $$ = new Property (current_class, (Expression) $3, (int) $2, true,
1749 new MemberName ((string) $4), (Attributes) $1,
1750 pinfo.Get, pinfo.Set, lexer.Location);
1751 if (RootContext.Documentation != null)
1752 ((Property) $$).DocComment = Lexer.consume_doc_comment ();
1757 CheckIdentifierToken (yyToken);
1763 : opt_attributes opt_modifiers GET SEMICOLON
1764 { $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, (int) $2, 0, lexer.Location, lexer.Location); }
1765 | opt_attributes opt_modifiers GET OPEN_BRACE
1767 Report.Error (531, lexer.Location, "'{0}': interface members cannot have a definition", ".get");
1770 | opt_attributes opt_modifiers SET SEMICOLON
1771 { $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1, 0, (int) $2, lexer.Location, lexer.Location); }
1772 | opt_attributes opt_modifiers GET SEMICOLON opt_attributes opt_modifiers SET SEMICOLON
1773 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $5, (int) $2, (int) $6, lexer.Location, lexer.Location); }
1774 | opt_attributes opt_modifiers SET SEMICOLON opt_attributes opt_modifiers GET SEMICOLON
1775 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $5, (Attributes) $1, (int) $6, (int) $2, lexer.Location, lexer.Location); }
1778 Report.Error (548, lexer.Location, "'{0}' : property or indexer must have at least one accessor", "");
1782 interface_event_declaration
1783 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1785 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1786 new MemberName ((string) $5), null,
1787 (Attributes) $1, lexer.Location);
1788 if (RootContext.Documentation != null)
1789 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1791 | opt_attributes opt_new EVENT type error {
1792 CheckIdentifierToken (yyToken);
1795 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1796 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1799 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1801 lexer.EventParsing = true;
1803 event_accessor_declarations
1805 lexer.EventParsing = false;
1808 Report.Error (69, lexer.Location, "Event in interface cannot have add or remove accessors");
1813 interface_indexer_declaration
1814 : opt_attributes opt_new type THIS
1815 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1817 { lexer.PropertyParsing = true; }
1819 { lexer.PropertyParsing = false; }
1825 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1827 $$ = new Indexer (current_class, (Expression) $3,
1828 new MemberName (TypeContainer.DefaultIndexerName),
1829 (int) $2, true, (Parameters) $6, (Attributes) $1,
1830 info.Get, info.Set, lexer.Location);
1831 if (RootContext.Documentation != null)
1832 ((Indexer) $$).DocComment = ConsumeStoredComment ();
1836 operator_declaration
1837 : opt_attributes opt_modifiers operator_declarator
1839 iterator_container = SimpleIteratorContainer.GetSimple ();
1843 OperatorDeclaration decl = (OperatorDeclaration) $3;
1845 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1847 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null);
1848 if (decl.arg2type != null)
1849 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null);
1851 Operator op = new Operator (
1852 current_class, decl.optype, decl.ret_type, (int) $2,
1853 new Parameters (param_list, null, decl.location),
1854 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1856 if (RootContext.Documentation != null) {
1857 op.DocComment = tmpComment;
1858 Lexer.doc_state = XmlCommentState.Allowed;
1861 if (SimpleIteratorContainer.Simple.Yields)
1864 // Note again, checking is done in semantic analysis
1865 current_container.AddOperator (op);
1867 current_local_parameters = null;
1868 iterator_container = null;
1874 | SEMICOLON { $$ = null; }
1877 : type OPERATOR overloadable_operator
1878 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1880 Operator.OpType op = (Operator.OpType) $3;
1881 CheckUnaryOperator (op);
1883 if (op == Operator.OpType.Addition)
1884 op = Operator.OpType.UnaryPlus;
1886 if (op == Operator.OpType.Subtraction)
1887 op = Operator.OpType.UnaryNegation;
1889 Parameter [] pars = new Parameter [1];
1890 Expression type = (Expression) $5;
1892 pars [0] = new Parameter (type, (string) $6, Parameter.Modifier.NONE, null);
1894 current_local_parameters = new Parameters (pars, null, lexer.Location);
1896 if (RootContext.Documentation != null) {
1897 tmpComment = Lexer.consume_doc_comment ();
1898 Lexer.doc_state = XmlCommentState.NotAllowed;
1901 $$ = new OperatorDeclaration (op, (Expression) $1, type, (string) $6,
1902 null, null, lexer.Location);
1904 | type OPERATOR overloadable_operator
1906 type IDENTIFIER COMMA
1910 CheckBinaryOperator ((Operator.OpType) $3);
1912 Parameter [] pars = new Parameter [2];
1914 Expression typeL = (Expression) $5;
1915 Expression typeR = (Expression) $8;
1917 pars [0] = new Parameter (typeL, (string) $6, Parameter.Modifier.NONE, null);
1918 pars [1] = new Parameter (typeR, (string) $9, Parameter.Modifier.NONE, null);
1920 current_local_parameters = new Parameters (pars, null, lexer.Location);
1922 if (RootContext.Documentation != null) {
1923 tmpComment = Lexer.consume_doc_comment ();
1924 Lexer.doc_state = XmlCommentState.NotAllowed;
1927 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1929 typeR, (string) $9, lexer.Location);
1931 | conversion_operator_declarator
1934 overloadable_operator
1936 : BANG { $$ = Operator.OpType.LogicalNot; }
1937 | TILDE { $$ = Operator.OpType.OnesComplement; }
1938 | OP_INC { $$ = Operator.OpType.Increment; }
1939 | OP_DEC { $$ = Operator.OpType.Decrement; }
1940 | TRUE { $$ = Operator.OpType.True; }
1941 | FALSE { $$ = Operator.OpType.False; }
1942 // Unary and binary:
1943 | PLUS { $$ = Operator.OpType.Addition; }
1944 | MINUS { $$ = Operator.OpType.Subtraction; }
1946 | STAR { $$ = Operator.OpType.Multiply; }
1947 | DIV { $$ = Operator.OpType.Division; }
1948 | PERCENT { $$ = Operator.OpType.Modulus; }
1949 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1950 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1951 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1952 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1953 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1954 | OP_EQ { $$ = Operator.OpType.Equality; }
1955 | OP_NE { $$ = Operator.OpType.Inequality; }
1956 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1957 | OP_LT { $$ = Operator.OpType.LessThan; }
1958 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1959 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1962 conversion_operator_declarator
1963 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1965 Parameter [] pars = new Parameter [1];
1967 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1969 current_local_parameters = new Parameters (pars, null, lexer.Location);
1971 if (RootContext.Documentation != null) {
1972 tmpComment = Lexer.consume_doc_comment ();
1973 Lexer.doc_state = XmlCommentState.NotAllowed;
1976 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1977 null, null, lexer.Location);
1979 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1981 Parameter [] pars = new Parameter [1];
1983 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1985 current_local_parameters = new Parameters (pars, null, lexer.Location);
1987 if (RootContext.Documentation != null) {
1988 tmpComment = Lexer.consume_doc_comment ();
1989 Lexer.doc_state = XmlCommentState.NotAllowed;
1992 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1993 null, null, lexer.Location);
1997 syntax_error (lexer.Location, "'operator' expected");
2001 syntax_error (lexer.Location, "'operator' expected");
2005 constructor_declaration
2008 constructor_declarator
2011 Constructor c = (Constructor) $3;
2012 c.Block = (ToplevelBlock) $4;
2013 c.OptAttributes = (Attributes) $1;
2014 c.ModFlags = (int) $2;
2016 if (RootContext.Documentation != null)
2017 c.DocComment = ConsumeStoredComment ();
2019 if (c.Name == current_container.Basename){
2020 if ((c.ModFlags & Modifiers.STATIC) != 0){
2021 if ((c.ModFlags & Modifiers.Accessibility) != 0){
2023 515, c.Location, String.Format (
2024 "`{0}.{1}': static constructor can not have access modifiers",
2025 c.Name, current_container.Name));
2028 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2030 if (c.Initializer != null){
2033 "Static constructors can not have an explicit this or base " +
2034 "constructor invocations");
2037 if (!c.Parameters.Empty){
2039 132, c.Location, "Static constructors should not have parameters");
2042 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2045 // We let another layer check the validity of the constructor.
2046 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
2049 current_container.AddConstructor (c);
2051 current_local_parameters = null;
2052 if (RootContext.Documentation != null)
2053 Lexer.doc_state = XmlCommentState.Allowed;
2057 constructor_declarator
2060 if (RootContext.Documentation != null) {
2061 tmpComment = Lexer.consume_doc_comment ();
2062 Lexer.doc_state = XmlCommentState.Allowed;
2065 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS _mark_
2067 current_local_parameters = (Parameters) $4;
2069 opt_constructor_initializer
2071 $$ = new Constructor (current_class, (string) $1, 0, (Parameters) $4,
2072 (ConstructorInitializer) $8, (Location) $6);
2078 | SEMICOLON { $$ = null; }
2081 opt_constructor_initializer
2082 : /* empty */ { $$ = null; }
2083 | constructor_initializer
2086 constructor_initializer
2087 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
2089 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
2091 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
2093 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
2096 Report.Error (1018, lexer.Location, "Keyword this or base expected");
2102 : /* EMPTY */ { $$ = 0; }
2103 | UNSAFE { $$ = Modifiers.UNSAFE; }
2104 | EXTERN { $$ = Modifiers.EXTERN; }
2107 destructor_declaration
2108 : opt_attributes opt_finalizer TILDE
2110 if (RootContext.Documentation != null) {
2111 tmpComment = Lexer.consume_doc_comment ();
2112 Lexer.doc_state = XmlCommentState.NotAllowed;
2115 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2117 if ((string) $5 != current_container.Basename){
2118 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
2119 } else if (current_container.Kind != Kind.Class){
2120 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
2122 Location l = lexer.Location;
2125 if (!RootContext.StdLib && current_container.Name == "System.Object")
2126 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2128 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2130 if ((m & Modifiers.UNSAFE) != 0){
2131 if (!RootContext.Unsafe){
2132 Report.Error (227, l,
2133 "Unsafe code requires the -unsafe command " +
2134 "line option to be specified");
2138 Method d = new Destructor (
2139 current_class, TypeManager.system_void_expr, m, "Finalize",
2140 new Parameters (null, null, l), (Attributes) $1, l);
2141 if (RootContext.Documentation != null)
2142 d.DocComment = ConsumeStoredComment ();
2144 d.Block = (ToplevelBlock) $8;
2145 current_container.AddMethod (d);
2153 EVENT type variable_declarators SEMICOLON
2155 foreach (VariableDeclaration var in (ArrayList) $5) {
2157 MemberName name = new MemberName (var.identifier);
2159 Event e = new EventField (
2160 current_class, (Expression) $4, (int) $2, false, name,
2161 var.expression_or_array_initializer, (Attributes) $1,
2164 current_container.AddEvent (e);
2166 if (RootContext.Documentation != null) {
2167 e.DocComment = Lexer.consume_doc_comment ();
2168 Lexer.doc_state = XmlCommentState.Allowed;
2174 EVENT type namespace_or_type_name
2177 implicit_value_parameter_type = (Expression) $4;
2178 lexer.EventParsing = true;
2180 event_accessor_declarations
2182 lexer.EventParsing = false;
2186 Location loc = (Location) $7;
2189 Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
2192 Pair pair = (Pair) $9;
2194 MemberName name = (MemberName) $5;
2196 if (name.TypeArguments != null)
2197 syntax_error (lexer.Location, "an event can't have type arguments");
2199 Event e = new EventProperty (
2200 current_class, (Expression) $4, (int) $2, false, name, null,
2201 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
2203 if (RootContext.Documentation != null) {
2204 e.DocComment = Lexer.consume_doc_comment ();
2205 Lexer.doc_state = XmlCommentState.Allowed;
2208 current_container.AddEvent (e);
2209 implicit_value_parameter_type = null;
2212 | opt_attributes opt_modifiers EVENT type namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS block {
2213 MemberName mn = (MemberName) $5;
2215 if (mn.Left != null)
2216 Report.Error (71, lexer.Location, "Explicit implementation of events requires property syntax");
2218 Report.Error (71, lexer.Location, "Event declaration should use property syntax");
2220 if (RootContext.Documentation != null)
2221 Lexer.doc_state = XmlCommentState.Allowed;
2225 event_accessor_declarations
2226 : add_accessor_declaration remove_accessor_declaration
2228 $$ = new Pair ($1, $2);
2230 | remove_accessor_declaration add_accessor_declaration
2232 $$ = new Pair ($2, $1);
2234 | add_accessor_declaration { $$ = null; }
2235 | remove_accessor_declaration { $$ = null; }
2238 Report.Error (1055, lexer.Location, "An add or remove accessor expected");
2244 add_accessor_declaration
2245 : opt_attributes ADD
2247 Parameter [] args = new Parameter [1];
2248 Parameter implicit_value_parameter = new Parameter (
2249 implicit_value_parameter_type, "value",
2250 Parameter.Modifier.NONE, null);
2252 args [0] = implicit_value_parameter;
2254 current_local_parameters = new Parameters (args, null, lexer.Location);
2255 lexer.EventParsing = false;
2259 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2260 lexer.EventParsing = true;
2262 | opt_attributes ADD error {
2263 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2266 | opt_attributes modifiers ADD {
2267 Report.Error (1609, lexer.Location, "Modifiers cannot be placed on event accessor declarations");
2272 remove_accessor_declaration
2273 : opt_attributes REMOVE
2275 Parameter [] args = new Parameter [1];
2276 Parameter implicit_value_parameter = new Parameter (
2277 implicit_value_parameter_type, "value",
2278 Parameter.Modifier.NONE, null);
2280 args [0] = implicit_value_parameter;
2282 current_local_parameters = new Parameters (args, null, lexer.Location);
2283 lexer.EventParsing = false;
2287 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2288 lexer.EventParsing = true;
2290 | opt_attributes REMOVE error {
2291 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2294 | opt_attributes modifiers REMOVE {
2295 Report.Error (1609, lexer.Location, "Modifiers cannot be placed on event accessor declarations");
2301 : opt_attributes opt_modifiers indexer_declarator
2304 IndexerDeclaration decl = (IndexerDeclaration) $3;
2306 implicit_value_parameter_type = decl.type;
2308 lexer.PropertyParsing = true;
2309 parsing_indexer = true;
2311 indexer_parameters = decl.param_list;
2313 accessor_declarations
2315 lexer.PropertyParsing = false;
2316 has_get = has_set = false;
2317 parsing_indexer = false;
2324 // The signature is computed from the signature of the indexer. Look
2325 // at section 3.6 on the spec
2326 Location loc = (Location) $5;
2328 IndexerDeclaration decl = (IndexerDeclaration) $3;
2329 Pair pair = (Pair) $7;
2330 Accessor get_block = (Accessor) pair.First;
2331 Accessor set_block = (Accessor) pair.Second;
2334 if (decl.interface_type != null)
2335 name = new MemberName (decl.interface_type,
2336 TypeContainer.DefaultIndexerName, null);
2338 name = new MemberName (TypeContainer.DefaultIndexerName);
2340 indexer = new Indexer (current_class, decl.type, name,
2341 (int) $2, false, decl.param_list, (Attributes) $1,
2342 get_block, set_block, loc);
2343 if (RootContext.Documentation != null)
2344 indexer.DocComment = ConsumeStoredComment ();
2346 current_container.AddIndexer (indexer);
2348 current_local_parameters = null;
2349 implicit_value_parameter_type = null;
2350 indexer_parameters = null;
2355 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2357 Parameters pars = (Parameters) $4;
2358 if (pars.HasArglist) {
2359 // "__arglist is not valid in this context"
2360 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2361 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2362 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2364 if (RootContext.Documentation != null) {
2365 tmpComment = Lexer.consume_doc_comment ();
2366 Lexer.doc_state = XmlCommentState.Allowed;
2369 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
2371 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2373 Parameters pars = (Parameters) $6;
2375 if (pars.HasArglist) {
2376 // "__arglist is not valid in this context"
2377 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2378 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2379 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2382 MemberName name = (MemberName) $2;
2384 $$ = new IndexerDeclaration ((Expression) $1, name, pars);
2386 if (RootContext.Documentation != null) {
2387 tmpComment = Lexer.consume_doc_comment ();
2388 Lexer.doc_state = XmlCommentState.Allowed;
2398 if (RootContext.Documentation != null)
2399 enumTypeComment = Lexer.consume_doc_comment ();
2404 Location enum_location = lexer.Location;
2406 MemberName full_name = MakeName (new MemberName ((string) $4));
2407 Enum e = new Enum (current_namespace, current_container, (Expression) $5, (int) $2,
2408 full_name, (Attributes) $1, enum_location);
2410 if (RootContext.Documentation != null)
2411 e.DocComment = enumTypeComment;
2413 foreach (VariableDeclaration ev in (ArrayList) $7) {
2414 e.AddEnumMember (ev.identifier,
2415 (Expression) ev.expression_or_array_initializer,
2416 ev.Location, ev.OptAttributes,
2420 string name = full_name.GetName ();
2421 current_container.AddEnum (e);
2422 RootContext.Tree.RecordDecl (name, e);
2428 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2429 | COLON type { $$ = $2; }
2435 if (RootContext.Documentation != null)
2436 Lexer.doc_state = XmlCommentState.Allowed;
2438 opt_enum_member_declarations
2440 // here will be evaluated after CLOSE_BLACE is consumed.
2441 if (RootContext.Documentation != null)
2442 Lexer.doc_state = XmlCommentState.Allowed;
2450 opt_enum_member_declarations
2451 : /* empty */ { $$ = new ArrayList (4); }
2452 | enum_member_declarations opt_comma { $$ = $1; }
2455 enum_member_declarations
2456 : enum_member_declaration
2458 ArrayList l = new ArrayList (4);
2463 | enum_member_declarations COMMA enum_member_declaration
2465 ArrayList l = (ArrayList) $1;
2473 enum_member_declaration
2474 : opt_attributes IDENTIFIER
2476 VariableDeclaration vd = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
2478 if (RootContext.Documentation != null) {
2479 vd.DocComment = Lexer.consume_doc_comment ();
2480 Lexer.doc_state = XmlCommentState.Allowed;
2485 | opt_attributes IDENTIFIER
2487 $$ = lexer.Location;
2488 if (RootContext.Documentation != null) {
2489 tmpComment = Lexer.consume_doc_comment ();
2490 Lexer.doc_state = XmlCommentState.NotAllowed;
2495 VariableDeclaration vd = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
2497 if (RootContext.Documentation != null)
2498 vd.DocComment = ConsumeStoredComment ();
2504 delegate_declaration
2507 DELEGATE type member_name
2508 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2510 Location l = lexer.Location;
2511 MemberName name = MakeName ((MemberName) $5);
2512 Delegate del = new Delegate (current_namespace, current_container, (Expression) $4,
2513 (int) $2, name, (Parameters) $7, (Attributes) $1, l);
2515 if (RootContext.Documentation != null) {
2516 del.DocComment = Lexer.consume_doc_comment ();
2517 Lexer.doc_state = XmlCommentState.Allowed;
2520 current_container.AddDelegate (del);
2521 RootContext.Tree.RecordDecl (name.GetName (true), del);
2523 current_delegate = del;
2525 lexer.ConstraintsParsing = true;
2527 opt_type_parameter_constraints_clauses
2529 lexer.ConstraintsParsing = false;
2533 current_delegate.SetParameterInfo ((ArrayList) $9);
2535 current_delegate = null;
2542 lexer.CheckNullable (false);
2547 lexer.CheckNullable (true);
2552 namespace_or_type_name
2554 | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list {
2555 $$ = new MemberName ((MemberName) $1, (string) $3, (TypeArguments) $4);
2560 : IDENTIFIER opt_type_argument_list {
2561 $$ = new MemberName ((string) $1, (TypeArguments) $2);
2565 opt_type_argument_list
2566 : /* empty */ { $$ = null; }
2567 | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2573 $$ = new TypeArguments ((int) $1, lexer.Location);
2579 TypeArguments type_args = new TypeArguments (lexer.Location);
2580 type_args.Add ((Expression) $1);
2583 | type_arguments COMMA type {
2584 TypeArguments type_args = (TypeArguments) $1;
2585 type_args.Add ((Expression) $3);
2591 * Before you think of adding a return_type, notice that we have been
2592 * using two rules in the places where it matters (one rule using type
2593 * and another identical one that uses VOID as the return type). This
2594 * gets rid of a shift/reduce couple
2597 : namespace_or_type_name opt_nullable
2599 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2602 $$ = new ComposedCast ((Expression) $$, "?", lexer.Location);
2604 | builtin_types opt_nullable
2607 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2617 // Note that here only unmanaged types are allowed but we
2618 // can't perform checks during this phase - we do it during
2619 // semantic analysis.
2621 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2625 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2630 : builtin_types opt_nullable
2633 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2635 | non_expression_type rank_specifier
2637 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2639 | non_expression_type STAR
2641 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2643 | expression rank_specifiers
2645 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2649 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2653 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2654 // through this different path
2656 | multiplicative_expression STAR
2658 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2665 ArrayList types = new ArrayList (4);
2670 | type_list COMMA type
2672 ArrayList types = (ArrayList) $1;
2680 * replaces all the productions for isolating the various
2681 * simple types, but we need this to reuse it easily in local_variable_type
2684 : OBJECT { $$ = TypeManager.system_object_expr; }
2685 | STRING { $$ = TypeManager.system_string_expr; }
2686 | BOOL { $$ = TypeManager.system_boolean_expr; }
2687 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2688 | FLOAT { $$ = TypeManager.system_single_expr; }
2689 | DOUBLE { $$ = TypeManager.system_double_expr; }
2694 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2695 | BYTE { $$ = TypeManager.system_byte_expr; }
2696 | SHORT { $$ = TypeManager.system_int16_expr; }
2697 | USHORT { $$ = TypeManager.system_uint16_expr; }
2698 | INT { $$ = TypeManager.system_int32_expr; }
2699 | UINT { $$ = TypeManager.system_uint32_expr; }
2700 | LONG { $$ = TypeManager.system_int64_expr; }
2701 | ULONG { $$ = TypeManager.system_uint64_expr; }
2702 | CHAR { $$ = TypeManager.system_char_expr; }
2703 | VOID { $$ = TypeManager.system_void_expr; }
2707 : type rank_specifiers opt_nullable
2709 string rank_specifiers = (string) $2;
2711 rank_specifiers += "?";
2713 $$ = new ComposedCast ((Expression) $1, rank_specifiers, lexer.Location);
2718 // Expressions, section 7.5
2727 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2729 | parenthesized_expression
2730 | default_value_expression
2732 | invocation_expression
2736 | post_increment_expression
2737 | post_decrement_expression
2741 | checked_expression
2742 | unchecked_expression
2743 | pointer_member_access
2744 | anonymous_method_expression
2751 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2752 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2753 | NULL { $$ = NullLiteral.Null; }
2757 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2758 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2759 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2764 object v = lexer.Value;
2770 $$ = IntLiteral.Zero;
2772 $$ = IntLiteral.One;
2774 $$ = new IntLiteral (i);
2775 } else if (v is uint)
2776 $$ = new UIntLiteral ((UInt32) v);
2778 $$ = new LongLiteral ((Int64) v);
2779 else if (v is ulong)
2780 $$ = new ULongLiteral ((UInt64) v);
2782 Console.WriteLine ("OOPS. Unexpected result from scanner");
2787 : TRUE { $$ = new BoolLiteral (true); }
2788 | FALSE { $$ = new BoolLiteral (false); }
2791 parenthesized_expression_0
2792 : OPEN_PARENS expression CLOSE_PARENS
2795 lexer.Deambiguate_CloseParens ();
2796 // After this, the next token returned is one of
2797 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2798 // or CLOSE_PARENS_MINUS.
2802 parenthesized_expression
2803 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2807 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2809 // If a parenthesized expression is followed by a minus, we need to wrap
2810 // the expression inside a ParenthesizedExpression for the CS0075 check
2811 // in Binary.DoResolve().
2812 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2817 : primary_expression DOT IDENTIFIER opt_type_argument_list
2819 $$ = new MemberAccess ((Expression) $1, (string) $3,
2820 (TypeArguments) $4, lexer.Location);
2822 | predefined_type DOT IDENTIFIER opt_type_argument_list
2824 $$ = new MemberAccess ((Expression) $1, (string) $3,
2825 (TypeArguments) $4, lexer.Location);
2833 invocation_expression
2834 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2837 Location l = lexer.Location;
2838 Report.Error (1, l, "Parse error");
2840 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2842 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2844 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2846 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2848 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2850 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2852 ArrayList args = new ArrayList (1);
2854 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2856 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2858 ArrayList args = ((ArrayList) $4);
2860 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2865 : /* empty */ { $$ = null; }
2872 ArrayList list = new ArrayList (4);
2876 | argument_list COMMA argument
2878 ArrayList list = (ArrayList) $1;
2882 | argument_list error {
2883 CheckToken (1026, yyToken, ", or ) expected");
2890 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2892 | non_simple_argument
2899 : REF variable_reference
2901 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2903 | OUT variable_reference
2905 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2907 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2909 ArrayList list = (ArrayList) $3;
2910 Argument[] args = new Argument [list.Count];
2911 list.CopyTo (args, 0);
2913 Expression expr = new Arglist (args, lexer.Location);
2914 $$ = new Argument (expr, Argument.AType.Expression);
2918 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2923 : expression { note ("section 5.4"); $$ = $1; }
2927 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2929 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2931 | primary_expression rank_specifiers
2933 // So the super-trick is that primary_expression
2934 // can only be either a SimpleName or a MemberAccess.
2935 // The MemberAccess case arises when you have a fully qualified type-name like :
2937 // SimpleName is when you have
2940 Expression expr = (Expression) $1;
2941 if (expr is ComposedCast){
2942 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2943 } else if (!(expr is SimpleName || expr is MemberAccess || expr is ConstructedType)){
2944 Error_ExpectingTypeName (lexer.Location, expr);
2945 $$ = TypeManager.system_object_expr;
2948 // So we extract the string corresponding to the SimpleName
2951 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2959 ArrayList list = new ArrayList (4);
2963 | expression_list COMMA expression
2965 ArrayList list = (ArrayList) $1;
2974 $$ = new This (current_block, lexer.Location);
2979 : BASE DOT IDENTIFIER
2981 $$ = new BaseAccess ((string) $3, lexer.Location);
2983 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2985 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2988 Report.Error (175, lexer.Location, "Use of keyword `base' is not valid in this context");
2993 post_increment_expression
2994 : primary_expression OP_INC
2996 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2997 (Expression) $1, lexer.Location);
3001 post_decrement_expression
3002 : primary_expression OP_DEC
3004 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
3005 (Expression) $1, lexer.Location);
3010 : object_or_delegate_creation_expression
3011 | array_creation_expression
3014 object_or_delegate_creation_expression
3015 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
3017 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
3021 array_creation_expression
3022 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
3024 opt_array_initializer
3026 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
3028 | NEW type rank_specifiers array_initializer
3030 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
3034 Report.Error (1031, lexer.Location, "Type expected");
3039 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
3054 opt_rank_specifier_or_nullable
3063 | opt_nullable rank_specifiers
3070 | opt_nullable rank_specifiers INTERR
3073 $$ = "?" + $2 + "?";
3080 : rank_specifier opt_rank_specifier
3082 $$ = (string) $2 + (string) $1;
3087 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3089 $$ = "[" + (string) $2 + "]";
3109 | dim_separators COMMA
3111 $$ = (string) $1 + ",";
3115 opt_array_initializer
3127 : OPEN_BRACE CLOSE_BRACE
3129 ArrayList list = new ArrayList (4);
3132 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3134 $$ = (ArrayList) $2;
3138 variable_initializer_list
3139 : variable_initializer
3141 ArrayList list = new ArrayList (4);
3145 | variable_initializer_list COMMA variable_initializer
3147 ArrayList list = (ArrayList) $1;
3153 void_pointer_expression
3154 : void_pointer_expression STAR
3156 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3160 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3165 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
3167 $$ = new TypeOfVoid (lexer.Location);
3169 | TYPEOF OPEN_PARENS void_pointer_expression CLOSE_PARENS
3171 $$ = new TypeOf ((Expression) $3, lexer.Location);
3173 | TYPEOF OPEN_PARENS
3175 lexer.TypeOfParsing = true;
3179 lexer.TypeOfParsing = false;
3180 $$ = new TypeOf ((Expression) $4, lexer.Location);
3185 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3186 $$ = new SizeOf ((Expression) $3, lexer.Location);
3191 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3193 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
3197 unchecked_expression
3198 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3200 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
3204 pointer_member_access
3205 : primary_expression OP_PTR IDENTIFIER
3209 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
3210 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
3214 anonymous_method_expression
3215 : DELEGATE opt_anonymous_method_signature _mark_
3217 oob_stack.Push (current_local_parameters);
3218 current_local_parameters = (Parameters)$2;
3220 // Force the next block to be created as a ToplevelBlock
3221 oob_stack.Push (current_block);
3222 oob_stack.Push (top_current_block);
3223 current_block = null;
3227 Location loc = (Location) $3;
3228 top_current_block = (Block) oob_stack.Pop ();
3229 current_block = (Block) oob_stack.Pop ();
3230 if (RootContext.Version == LanguageVersion.ISO_1){
3231 Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
3234 ToplevelBlock anon_block = (ToplevelBlock) $5;
3236 anon_block.Parent = current_block;
3237 $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block,
3240 current_local_parameters = (Parameters) oob_stack.Pop ();
3244 opt_anonymous_method_signature
3245 : /* empty */ { $$ = null; }
3246 | anonymous_method_signature
3249 anonymous_method_signature
3250 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3253 $$ = Parameters.EmptyReadOnlyParameters;
3255 ArrayList par_list = (ArrayList) $2;
3256 Parameter [] pars = new Parameter [par_list.Count];
3257 par_list.CopyTo (pars);
3258 $$ = new Parameters (pars, null, lexer.Location);
3263 opt_anonymous_method_parameter_list
3264 : /* empty */ { $$ = null; }
3265 | anonymous_method_parameter_list { $$ = $1; }
3268 anonymous_method_parameter_list
3269 : anonymous_method_parameter
3271 ArrayList a = new ArrayList (4);
3275 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3277 ArrayList a = (ArrayList) $1;
3283 anonymous_method_parameter
3284 : opt_parameter_modifier type IDENTIFIER {
3285 $$ = new Parameter ((Expression) $2, (string) $3, (Parameter.Modifier) $1, null);
3287 | PARAMS type IDENTIFIER {
3288 Report.Error (1670, lexer.Location, "params modifier not allowed in anonymous method declaration");
3293 default_value_expression
3294 : DEFAULT_OPEN_PARENS type CLOSE_PARENS
3296 $$ = new DefaultValueExpression ((Expression) $2, lexer.Location);
3301 : primary_expression
3302 | BANG prefixed_unary_expression
3304 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
3306 | TILDE prefixed_unary_expression
3308 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
3314 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3316 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3318 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3320 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3326 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3328 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3333 // The idea to split this out is from Rhys' grammar
3334 // to solve the problem with casts.
3336 prefixed_unary_expression
3338 | PLUS prefixed_unary_expression
3340 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
3342 | MINUS prefixed_unary_expression
3344 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
3346 | OP_INC prefixed_unary_expression
3348 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3349 (Expression) $2, lexer.Location);
3351 | OP_DEC prefixed_unary_expression
3353 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3354 (Expression) $2, lexer.Location);
3356 | STAR prefixed_unary_expression
3358 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
3360 | BITWISE_AND prefixed_unary_expression
3362 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
3366 pre_increment_expression
3367 : OP_INC prefixed_unary_expression
3369 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3370 (Expression) $2, lexer.Location);
3374 pre_decrement_expression
3375 : OP_DEC prefixed_unary_expression
3377 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3378 (Expression) $2, lexer.Location);
3382 multiplicative_expression
3383 : prefixed_unary_expression
3384 | multiplicative_expression STAR prefixed_unary_expression
3386 $$ = new Binary (Binary.Operator.Multiply,
3387 (Expression) $1, (Expression) $3, lexer.Location);
3389 | multiplicative_expression DIV prefixed_unary_expression
3391 $$ = new Binary (Binary.Operator.Division,
3392 (Expression) $1, (Expression) $3, lexer.Location);
3394 | multiplicative_expression PERCENT prefixed_unary_expression
3396 $$ = new Binary (Binary.Operator.Modulus,
3397 (Expression) $1, (Expression) $3, lexer.Location);
3402 : multiplicative_expression
3403 | additive_expression PLUS multiplicative_expression
3405 $$ = new Binary (Binary.Operator.Addition,
3406 (Expression) $1, (Expression) $3, lexer.Location);
3408 | additive_expression MINUS multiplicative_expression
3410 $$ = new Binary (Binary.Operator.Subtraction,
3411 (Expression) $1, (Expression) $3, lexer.Location);
3416 : additive_expression
3417 | shift_expression OP_SHIFT_LEFT additive_expression
3419 $$ = new Binary (Binary.Operator.LeftShift,
3420 (Expression) $1, (Expression) $3, lexer.Location);
3422 | shift_expression OP_SHIFT_RIGHT additive_expression
3424 $$ = new Binary (Binary.Operator.RightShift,
3425 (Expression) $1, (Expression) $3, lexer.Location);
3433 lexer.PutbackNullable ();
3437 nullable_type_or_conditional
3444 relational_expression
3446 | relational_expression OP_LT shift_expression
3448 $$ = new Binary (Binary.Operator.LessThan,
3449 (Expression) $1, (Expression) $3, lexer.Location);
3451 | relational_expression OP_GT shift_expression
3453 $$ = new Binary (Binary.Operator.GreaterThan,
3454 (Expression) $1, (Expression) $3, lexer.Location);
3456 | relational_expression OP_LE shift_expression
3458 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3459 (Expression) $1, (Expression) $3, lexer.Location);
3461 | relational_expression OP_GE shift_expression
3463 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3464 (Expression) $1, (Expression) $3, lexer.Location);
3466 | relational_expression IS
3469 } nullable_type_or_conditional
3471 $$ = new Is ((Expression) $1, (Expression) $4, lexer.Location);
3473 | relational_expression AS
3476 } nullable_type_or_conditional
3478 $$ = new As ((Expression) $1, (Expression) $4, lexer.Location);
3483 : relational_expression
3484 | equality_expression OP_EQ relational_expression
3486 $$ = new Binary (Binary.Operator.Equality,
3487 (Expression) $1, (Expression) $3, lexer.Location);
3489 | equality_expression OP_NE relational_expression
3491 $$ = new Binary (Binary.Operator.Inequality,
3492 (Expression) $1, (Expression) $3, lexer.Location);
3497 : equality_expression
3498 | and_expression BITWISE_AND equality_expression
3500 $$ = new Binary (Binary.Operator.BitwiseAnd,
3501 (Expression) $1, (Expression) $3, lexer.Location);
3505 exclusive_or_expression
3507 | exclusive_or_expression CARRET and_expression
3509 $$ = new Binary (Binary.Operator.ExclusiveOr,
3510 (Expression) $1, (Expression) $3, lexer.Location);
3514 inclusive_or_expression
3515 : exclusive_or_expression
3516 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3518 $$ = new Binary (Binary.Operator.BitwiseOr,
3519 (Expression) $1, (Expression) $3, lexer.Location);
3523 conditional_and_expression
3524 : inclusive_or_expression
3525 | conditional_and_expression OP_AND inclusive_or_expression
3527 $$ = new Binary (Binary.Operator.LogicalAnd,
3528 (Expression) $1, (Expression) $3, lexer.Location);
3532 conditional_or_expression
3533 : conditional_and_expression
3534 | conditional_or_expression OP_OR conditional_and_expression
3536 $$ = new Binary (Binary.Operator.LogicalOr,
3537 (Expression) $1, (Expression) $3, lexer.Location);
3541 conditional_expression
3542 : conditional_or_expression
3543 | conditional_or_expression INTERR expression COLON expression
3545 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
3547 | conditional_or_expression INTERR INTERR expression
3549 $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $4, lexer.Location);
3551 // We'll be resolved into a `parenthesized_expression_0' later on.
3552 | conditional_or_expression INTERR CLOSE_PARENS
3554 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
3555 lexer.PutbackCloseParens ();
3559 assignment_expression
3560 : prefixed_unary_expression ASSIGN expression
3562 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
3564 | prefixed_unary_expression OP_MULT_ASSIGN expression
3566 Location l = lexer.Location;
3568 $$ = new CompoundAssign (
3569 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
3571 | prefixed_unary_expression OP_DIV_ASSIGN expression
3573 Location l = lexer.Location;
3575 $$ = new CompoundAssign (
3576 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
3578 | prefixed_unary_expression OP_MOD_ASSIGN expression
3580 Location l = lexer.Location;
3582 $$ = new CompoundAssign (
3583 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
3585 | prefixed_unary_expression OP_ADD_ASSIGN expression
3587 Location l = lexer.Location;
3589 $$ = new CompoundAssign (
3590 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
3592 | prefixed_unary_expression OP_SUB_ASSIGN expression
3594 Location l = lexer.Location;
3596 $$ = new CompoundAssign (
3597 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
3599 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3601 Location l = lexer.Location;
3603 $$ = new CompoundAssign (
3604 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
3606 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3608 Location l = lexer.Location;
3610 $$ = new CompoundAssign (
3611 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
3613 | prefixed_unary_expression OP_AND_ASSIGN expression
3615 Location l = lexer.Location;
3617 $$ = new CompoundAssign (
3618 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
3620 | prefixed_unary_expression OP_OR_ASSIGN expression
3622 Location l = lexer.Location;
3624 $$ = new CompoundAssign (
3625 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
3627 | prefixed_unary_expression OP_XOR_ASSIGN expression
3629 Location l = lexer.Location;
3631 $$ = new CompoundAssign (
3632 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
3637 : conditional_expression
3638 | assignment_expression
3658 MemberName name = MakeName ((MemberName) $5);
3659 bool partial = (bool) $3;
3660 int mod_flags = (int) $2;
3663 ClassPart part = PartialContainer.CreatePart (
3664 current_namespace, current_container, name, mod_flags,
3665 (Attributes) $1, Kind.Class, lexer.Location);
3667 current_container = part.PartialContainer;
3668 current_class = part;
3670 if ((mod_flags & Modifiers.STATIC) != 0) {
3671 current_class = new StaticClass (
3672 current_namespace, current_container, name,
3673 mod_flags, (Attributes) $1, lexer.Location);
3675 current_class = new Class (
3676 current_namespace, current_container, name,
3677 mod_flags, (Attributes) $1, lexer.Location);
3680 current_container = current_class;
3681 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
3684 lexer.ConstraintsParsing = true;
3687 opt_type_parameter_constraints_clauses
3689 lexer.ConstraintsParsing = false;
3692 if (current_class.Name == "System.Object") {
3693 Report.Error (537, current_class.Location,
3694 "The class System.Object cannot have a base " +
3695 "class or implement an interface.");
3697 current_class.Bases = (ArrayList) $7;
3700 current_class.SetParameterInfo ((ArrayList) $8);
3702 if (RootContext.Documentation != null) {
3703 current_class.DocComment = Lexer.consume_doc_comment ();
3704 Lexer.doc_state = XmlCommentState.Allowed;
3707 current_class.Register ();
3711 if (RootContext.Documentation != null)
3712 Lexer.doc_state = XmlCommentState.Allowed;
3718 current_container = current_container.Parent;
3719 current_class = current_container;
3725 { $$ = (bool) false; }
3727 { $$ = (bool) true; }
3731 : /* empty */ { $$ = (int) 0; }
3737 | modifiers modifier
3742 if ((m1 & m2) != 0) {
3743 Location l = lexer.Location;
3744 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3746 $$ = (int) (m1 | m2);
3751 : NEW { $$ = Modifiers.NEW; }
3752 | PUBLIC { $$ = Modifiers.PUBLIC; }
3753 | PROTECTED { $$ = Modifiers.PROTECTED; }
3754 | INTERNAL { $$ = Modifiers.INTERNAL; }
3755 | PRIVATE { $$ = Modifiers.PRIVATE; }
3756 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3757 | SEALED { $$ = Modifiers.SEALED; }
3758 | STATIC { $$ = Modifiers.STATIC; }
3759 | READONLY { $$ = Modifiers.READONLY; }
3760 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3761 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3762 | EXTERN { $$ = Modifiers.EXTERN; }
3763 | VOLATILE { $$ = Modifiers.VOLATILE; }
3764 | UNSAFE { $$ = Modifiers.UNSAFE; }
3768 : /* empty */ { $$ = null; }
3769 | class_base { $$ = $1; }
3773 : COLON type_list { $$ = $2; }
3776 opt_type_parameter_constraints_clauses
3777 : /* empty */ { $$ = null; }
3778 | type_parameter_constraints_clauses
3782 type_parameter_constraints_clauses
3783 : type_parameter_constraints_clause {
3784 ArrayList constraints = new ArrayList (1);
3785 constraints.Add ($1);
3788 | type_parameter_constraints_clauses type_parameter_constraints_clause {
3789 ArrayList constraints = (ArrayList) $1;
3791 constraints.Add ($2);
3796 type_parameter_constraints_clause
3797 : WHERE IDENTIFIER COLON type_parameter_constraints {
3798 $$ = new Constraints ((string) $2, (ArrayList) $4, lexer.Location);
3802 type_parameter_constraints
3803 : type_parameter_constraint {
3804 ArrayList constraints = new ArrayList (1);
3805 constraints.Add ($1);
3808 | type_parameter_constraints COMMA type_parameter_constraint {
3809 ArrayList constraints = (ArrayList) $1;
3811 constraints.Add ($3);
3816 type_parameter_constraint
3818 | NEW OPEN_PARENS CLOSE_PARENS {
3819 $$ = SpecialConstraint.Constructor;
3822 $$ = SpecialConstraint.ReferenceType;
3825 $$ = SpecialConstraint.ValueType;
3834 // A block is "contained" on the following places:
3836 // property_declaration as part of the accessor body (get/set)
3837 // operator_declaration
3838 // constructor_declaration
3839 // destructor_declaration
3840 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3845 if (current_block == null){
3846 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, lexer.Location);
3847 top_current_block = current_block;
3849 current_block = new Block (current_block, current_local_parameters,
3850 lexer.Location, Location.Null);
3853 opt_statement_list CLOSE_BRACE
3855 while (current_block.Implicit)
3856 current_block = current_block.Parent;
3858 current_block.SetEndLocation (lexer.Location);
3859 current_block = current_block.Parent;
3860 if (current_block == null)
3861 top_current_block = null;
3872 | statement_list statement
3876 : declaration_statement
3878 if ($1 != null && (Block) $1 != current_block){
3879 current_block.AddStatement ((Statement) $1);
3880 current_block = (Block) $1;
3883 | valid_declaration_statement
3885 current_block.AddStatement ((Statement) $1);
3890 valid_declaration_statement
3893 | expression_statement
3894 | selection_statement
3895 | iteration_statement
3899 | unchecked_statement
3907 : valid_declaration_statement
3908 | declaration_statement
3910 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3915 Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3923 $$ = EmptyStatement.Value;
3930 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3932 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3933 current_block.AddStatement (labeled);
3938 declaration_statement
3939 : local_variable_declaration SEMICOLON
3942 DictionaryEntry de = (DictionaryEntry) $1;
3944 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3948 | local_constant_declaration SEMICOLON
3951 DictionaryEntry de = (DictionaryEntry) $1;
3953 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3959 * The following is from Rhys' grammar:
3960 * > Types in local variable declarations must be recognized as
3961 * > expressions to prevent reduce/reduce errors in the grammar.
3962 * > The expressions are converted into types during semantic analysis.
3965 : primary_expression opt_rank_specifier_or_nullable
3967 // FIXME: Do something smart here regarding the composition of the type.
3969 // Ok, the above "primary_expression" is there to get rid of
3970 // both reduce/reduce and shift/reduces in the grammar, it should
3971 // really just be "type_name". If you use type_name, a reduce/reduce
3972 // creeps up. If you use namespace_or_type_name (which is all we need
3973 // really) two shift/reduces appear.
3976 // So the super-trick is that primary_expression
3977 // can only be either a SimpleName or a MemberAccess.
3978 // The MemberAccess case arises when you have a fully qualified type-name like :
3980 // SimpleName is when you have
3983 Expression expr = (Expression) $1;
3984 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType)) {
3985 Error_ExpectingTypeName (lexer.Location, expr);
3989 // So we extract the string corresponding to the SimpleName
3993 if ((string) $2 == "")
3996 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3999 | builtin_types opt_rank_specifier_or_nullable
4001 if ((string) $2 == "")
4004 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
4008 local_variable_pointer_type
4009 : primary_expression STAR
4011 Expression expr = (Expression) $1;
4012 Location l = lexer.Location;
4014 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType)) {
4015 Error_ExpectingTypeName (l, expr);
4019 $$ = new ComposedCast ((Expression) $1, "*", l);
4021 | builtin_types STAR
4023 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
4027 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
4029 | local_variable_pointer_type STAR
4031 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
4035 local_variable_declaration
4036 : local_variable_type variable_declarators
4039 $$ = new DictionaryEntry ($1, $2);
4043 | local_variable_pointer_type opt_rank_specifier_or_nullable variable_declarators
4048 if ((string) $2 == "")
4049 t = (Expression) $1;
4051 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
4052 $$ = new DictionaryEntry (t, $3);
4058 local_constant_declaration
4059 : CONST local_variable_type constant_declarators
4062 $$ = new DictionaryEntry ($2, $3);
4068 expression_statement
4069 : statement_expression SEMICOLON
4076 // We have to do the wrapping here and not in the case above,
4077 // because statement_expression is used for example in for_statement
4079 statement_expression
4080 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4081 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4082 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4083 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4084 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4085 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4086 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4088 Report.Error (1002, lexer.Location, "Expecting `;'");
4093 object_creation_expression
4094 : object_or_delegate_creation_expression
4095 { note ("complain if this is a delegate maybe?"); }
4104 : IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS
4107 Location l = (Location) $3;
4109 $$ = new If ((Expression) $4, (Statement) $6, l);
4111 if (RootContext.WarningLevel >= 4){
4112 if ($6 == EmptyStatement.Value)
4113 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4117 | IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS
4118 embedded_statement ELSE embedded_statement
4120 Location l = (Location) $3;
4122 $$ = new If ((Expression) $4, (Statement) $6, (Statement) $8, l);
4127 : SWITCH OPEN_PARENS _mark_
4129 switch_stack.Push (current_block);
4131 expression CLOSE_PARENS
4134 $$ = new Switch ((Expression) $5, (ArrayList) $7, (Location) $3);
4135 current_block = (Block) switch_stack.Pop ();
4151 Report.Error (1522, lexer.Location, "Empty switch block");
4159 ArrayList sections = new ArrayList (4);
4164 | switch_sections switch_section
4166 ArrayList sections = (ArrayList) $1;
4176 current_block = current_block.CreateSwitchBlock (lexer.Location);
4180 Block topmost = current_block;
4182 while (topmost.Implicit)
4183 topmost = topmost.Parent;
4184 $$ = new SwitchSection ((ArrayList) $1, topmost);
4191 ArrayList labels = new ArrayList (4);
4196 | switch_labels switch_label
4198 ArrayList labels = (ArrayList) ($1);
4206 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
4207 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
4210 1523, lexer.Location,
4211 "The keyword case or default must precede code in switch block");
4223 : WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS embedded_statement
4225 Location l = (Location) $3;
4226 $$ = new While ((Expression) $4, (Statement) $6, l);
4228 if (RootContext.WarningLevel >= 4){
4229 if ($6 == EmptyStatement.Value)
4230 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4236 : DO embedded_statement
4237 WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS SEMICOLON
4239 Location l = (Location) $5;
4241 $$ = new Do ((Statement) $2, (Expression) $6, l);
4247 opt_for_initializer SEMICOLON _mark_
4249 Block assign_block = new Block (current_block);
4250 current_block = assign_block;
4252 if ($3 is DictionaryEntry){
4253 DictionaryEntry de = (DictionaryEntry) $3;
4255 Expression type = (Expression) de.Key;
4256 ArrayList var_declarators = (ArrayList) de.Value;
4258 foreach (VariableDeclaration decl in var_declarators){
4262 vi = current_block.AddVariable (
4263 type, decl.identifier, current_local_parameters, decl.Location);
4267 Location l = lexer.Location;
4269 if (decl.expression_or_array_initializer is Expression){
4270 expr = (Expression) decl.expression_or_array_initializer;
4271 } else if (decl.expression_or_array_initializer == null) {
4274 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4275 expr = new ArrayCreation (type, "", init, decl.Location);
4278 LocalVariableReference var;
4279 var = new LocalVariableReference (assign_block, decl.identifier, l);
4282 Assign a = new Assign (var, expr, decl.Location);
4284 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4288 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4289 // This can be referred to as $6 below.
4295 opt_for_condition SEMICOLON
4296 opt_for_iterator CLOSE_PARENS
4299 Location l = (Location) $5;
4301 For f = new For ((Statement) $6, (Expression) $7, (Statement) $9, (Statement) $11, l);
4303 if (RootContext.WarningLevel >= 4){
4304 if ($11 == EmptyStatement.Value)
4305 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4308 current_block.AddStatement (f);
4309 while (current_block.Implicit)
4310 current_block = current_block.Parent;
4312 current_block = current_block.Parent;
4317 : /* empty */ { $$ = EmptyStatement.Value; }
4322 : local_variable_declaration
4323 | statement_expression_list
4327 : /* empty */ { $$ = null; }
4328 | boolean_expression
4332 : /* empty */ { $$ = EmptyStatement.Value; }
4337 : statement_expression_list
4340 statement_expression_list
4341 : statement_expression
4343 // CHANGE: was `null'
4344 Block b = new Block (current_block, Block.Flags.Implicit);
4346 b.AddStatement ((Statement) $1);
4349 | statement_expression_list COMMA statement_expression
4351 Block b = (Block) $1;
4353 b.AddStatement ((Statement) $3);
4359 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4361 Report.Error (230, lexer.Location, "Type and identifier are both required in a foreach statement");
4364 | FOREACH OPEN_PARENS type IDENTIFIER IN _mark_
4365 expression CLOSE_PARENS
4367 Block foreach_block = new Block (current_block);
4368 current_block = foreach_block;
4370 Location l = lexer.Location;
4373 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
4377 // Get a writable reference to this read-only variable.
4379 // Note that the $$ here refers to the value of _this_ code block,
4380 // not the value of the LHS non-terminal. This can be referred to as $9 below.
4381 $$ = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
4388 LocalVariableReference v = (LocalVariableReference) $9;
4389 Location l = (Location) $6;
4392 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
4393 current_block.AddStatement (f);
4396 while (current_block.Implicit)
4397 current_block = current_block.Parent;
4399 current_block = current_block.Parent;
4405 | continue_statement
4415 $$ = new Break (lexer.Location);
4420 : CONTINUE SEMICOLON
4422 $$ = new Continue (lexer.Location);
4427 : GOTO IDENTIFIER SEMICOLON
4429 $$ = new Goto (current_block, (string) $2, lexer.Location);
4431 | GOTO CASE constant_expression SEMICOLON
4433 $$ = new GotoCase ((Expression) $3, lexer.Location);
4435 | GOTO DEFAULT SEMICOLON
4437 $$ = new GotoDefault (lexer.Location);
4442 : RETURN opt_expression SEMICOLON
4444 $$ = new Return ((Expression) $2, lexer.Location);
4449 : THROW opt_expression SEMICOLON
4451 $$ = new Throw ((Expression) $2, lexer.Location);
4456 : IDENTIFIER RETURN expression SEMICOLON
4458 string s = (string) $1;
4460 Report.Error (1003, lexer.Location, "; expected");
4463 if (RootContext.Version == LanguageVersion.ISO_1){
4464 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4467 if (iterator_container == null){
4468 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4471 iterator_container.SetYields ();
4472 $$ = new Yield ((Expression) $3, lexer.Location);
4475 | IDENTIFIER RETURN SEMICOLON
4477 Report.Error (1627, lexer.Location, "Expression expected after yield return");
4480 | IDENTIFIER BREAK SEMICOLON
4482 string s = (string) $1;
4484 Report.Error (1003, lexer.Location, "; expected");
4487 if (RootContext.Version == LanguageVersion.ISO_1){
4488 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4491 if (iterator_container == null){
4492 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4495 iterator_container.SetYields ();
4496 $$ = new YieldBreak (lexer.Location);
4507 : TRY block catch_clauses
4511 ArrayList c = (ArrayList)$3;
4512 for (int i = 0; i < c.Count; ++i) {
4513 Catch cc = (Catch) c [i];
4515 if (i != c.Count - 1)
4516 Report.Error (1017, cc.loc, "Empty catch block must be the last in a series of catch blocks");
4523 // Now s contains the list of specific catch clauses
4524 // and g contains the general one.
4526 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4528 | TRY block opt_catch_clauses FINALLY block
4531 ArrayList s = new ArrayList (4);
4532 ArrayList catch_list = (ArrayList) $3;
4534 if (catch_list != null){
4535 foreach (Catch cc in catch_list) {
4543 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4547 Report.Error (1524, lexer.Location, "Expected catch or finally");
4552 : /* empty */ { $$ = null; }
4559 ArrayList l = new ArrayList (4);
4564 | catch_clauses catch_clause
4566 ArrayList l = (ArrayList) $1;
4574 : /* empty */ { $$ = null; }
4579 : CATCH opt_catch_args
4581 Expression type = null;
4585 DictionaryEntry cc = (DictionaryEntry) $2;
4586 type = (Expression) cc.Key;
4587 id = (string) cc.Value;
4590 ArrayList one = new ArrayList (4);
4591 Location loc = lexer.Location;
4593 one.Add (new VariableDeclaration (id, null, loc));
4595 current_block = new Block (current_block);
4596 Block b = declare_local_variables (type, one, loc);
4601 Expression type = null;
4605 DictionaryEntry cc = (DictionaryEntry) $2;
4606 type = (Expression) cc.Key;
4607 id = (string) cc.Value;
4610 while (current_block.Implicit)
4611 current_block = current_block.Parent;
4612 current_block = current_block.Parent;
4616 $$ = new Catch (type, id, (Block) $4, ((Block) $4).loc);
4621 : /* empty */ { $$ = null; }
4626 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4628 $$ = new DictionaryEntry ($2, $3);
4635 $$ = new Checked ((Block) $2);
4642 $$ = new Unchecked ((Block) $2);
4649 if (!RootContext.Unsafe){
4650 Report.Error (227, lexer.Location,
4651 "Unsafe code can only be used if --unsafe is used");
4654 $$ = new Unsafe ((Block) $3);
4660 type fixed_pointer_declarators
4663 ArrayList list = (ArrayList) $4;
4664 Expression type = (Expression) $3;
4665 Location l = lexer.Location;
4666 int top = list.Count;
4668 Block assign_block = new Block (current_block);
4669 current_block = assign_block;
4671 for (int i = 0; i < top; i++){
4672 Pair p = (Pair) list [i];
4675 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
4687 Location l = (Location) $6;
4689 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $8, l);
4691 if (RootContext.WarningLevel >= 4){
4692 if ($8 == EmptyStatement.Value)
4693 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4696 current_block.AddStatement (f);
4697 while (current_block.Implicit)
4698 current_block = current_block.Parent;
4700 current_block = current_block.Parent;
4704 fixed_pointer_declarators
4705 : fixed_pointer_declarator {
4706 ArrayList declarators = new ArrayList (4);
4708 declarators.Add ($1);
4711 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4713 ArrayList declarators = (ArrayList) $1;
4715 declarators.Add ($3);
4720 fixed_pointer_declarator
4721 : IDENTIFIER ASSIGN expression
4723 $$ = new Pair ($1, $3);
4727 Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
4733 : LOCK OPEN_PARENS expression CLOSE_PARENS
4739 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4744 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS _mark_
4746 Block assign_block = new Block (current_block);
4747 current_block = assign_block;
4749 if ($3 is DictionaryEntry){
4750 DictionaryEntry de = (DictionaryEntry) $3;
4751 Location l = lexer.Location;
4753 Expression type = (Expression) de.Key;
4754 ArrayList var_declarators = (ArrayList) de.Value;
4756 ArrayList vars = new ArrayList (4);
4758 foreach (VariableDeclaration decl in var_declarators){
4760 LocalInfo vi = current_block.AddVariable (
4761 type, decl.identifier,
4762 current_local_parameters, decl.Location);
4768 if (decl.expression_or_array_initializer is Expression){
4769 expr = (Expression) decl.expression_or_array_initializer;
4771 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4773 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4776 expr = new ArrayCreation (type, "", init, decl.Location);
4779 LocalVariableReference var;
4781 // Get a writable reference to this read-only variable.
4782 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4784 // This is so that it is not a warning on using variables
4787 vars.Add (new DictionaryEntry (var, expr));
4789 // Assign a = new Assign (var, expr, decl.Location);
4790 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4793 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4794 // It can be referred to as $6 below.
4795 $$ = new DictionaryEntry (type, vars);
4802 Using u = new Using ($6, (Statement) $7, (Location) $5);
4803 current_block.AddStatement (u);
4804 while (current_block.Implicit)
4805 current_block = current_block.Parent;
4807 current_block = current_block.Parent;
4811 resource_acquisition
4812 : local_variable_declaration
4816 // Utility rule to save location information
4819 { $$ = lexer.Location; }
4824 // A class used to pass around variable declarations and constants
4826 public class VariableDeclaration {
4827 public string identifier;
4828 public object expression_or_array_initializer;
4829 public Location Location;
4830 public Attributes OptAttributes;
4831 public string DocComment;
4833 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4835 this.identifier = id;
4836 this.expression_or_array_initializer = eoai;
4838 this.OptAttributes = opt_attrs;
4841 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4847 /// Used to pass around interface property information
4849 public class InterfaceAccessorInfo {
4851 public readonly Accessor Get, Set;
4853 public InterfaceAccessorInfo (bool has_get, bool has_set,
4854 Attributes get_attrs, Attributes set_attrs, int get_mod, int set_mod, Location get_loc, Location set_loc)
4857 Report.Error (275, get_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4859 Report.Error (275, set_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4862 Get = new Accessor (null, 0, get_attrs, get_loc);
4864 Set = new Accessor (null, 0, set_attrs, set_loc);
4871 // A class used to hold info about an indexer declarator
4873 public class IndexerDeclaration {
4874 public Expression type;
4875 public MemberName interface_type;
4876 public Parameters param_list;
4878 public IndexerDeclaration (Expression type, MemberName interface_type,
4879 Parameters param_list)
4882 this.interface_type = interface_type;
4883 this.param_list = param_list;
4888 // We use this when we do not have an object in advance that is an IIteratorContainer
4890 public class SimpleIteratorContainer : IIteratorContainer {
4893 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4898 public static SimpleIteratorContainer GetSimple () {
4899 Simple.Yields = false;
4903 public void SetYields () { Yields = true; }
4907 // A class used to hold info about an operator declarator
4909 public class OperatorDeclaration {
4910 public Operator.OpType optype;
4911 public Expression ret_type, arg1type, arg2type;
4912 public string arg1name, arg2name;
4913 public Location location;
4915 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4916 Expression arg1type, string arg1name,
4917 Expression arg2type, string arg2name, Location location)
4920 this.ret_type = ret_type;
4921 this.arg1type = arg1type;
4922 this.arg1name = arg1name;
4923 this.arg2type = arg2type;
4924 this.arg2name = arg2name;
4925 this.location = location;
4930 void Error_ExpectingTypeName (Location l, Expression expr)
4932 if (expr is Invocation){
4933 Report.Error (1002, l, "; expected");
4935 Report.Error (-1, l, "Invalid Type definition");
4940 // Given the @class_name name, it creates a fully qualified name
4941 // based on the containing declaration space
4944 MakeName (MemberName class_name)
4946 string ns = current_namespace.FullName;
4948 if (current_container.Name == ""){
4950 return new MemberName (new MemberName (ns), class_name);
4954 return new MemberName (current_container.MemberName, class_name);
4958 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4960 Block implicit_block;
4961 ArrayList inits = null;
4964 // We use the `Used' property to check whether statements
4965 // have been added to the current block. If so, we need
4966 // to create another block to contain the new declaration
4967 // otherwise, as an optimization, we use the same block to
4968 // add the declaration.
4970 // FIXME: A further optimization is to check if the statements
4971 // that were added were added as part of the initialization
4972 // below. In which case, no other statements have been executed
4973 // and we might be able to reduce the number of blocks for
4974 // situations like this:
4976 // int j = 1; int k = j + 1;
4978 if (current_block.Used)
4979 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4981 implicit_block = current_block;
4983 foreach (VariableDeclaration decl in variable_declarators){
4985 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4986 if (decl.expression_or_array_initializer != null){
4988 inits = new ArrayList (4);
4995 return implicit_block;
4997 foreach (VariableDeclaration decl in inits){
5001 if (decl.expression_or_array_initializer is Expression){
5002 expr = (Expression) decl.expression_or_array_initializer;
5005 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
5007 expr = new ArrayCreation (type, "", init, decl.Location);
5010 LocalVariableReference var;
5011 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
5013 assign = new Assign (var, expr, decl.Location);
5015 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
5018 return implicit_block;
5021 Block declare_local_constants (Expression type, ArrayList declarators)
5023 Block implicit_block;
5025 if (current_block.Used)
5026 implicit_block = new Block (current_block, Block.Flags.Implicit);
5028 implicit_block = current_block;
5030 foreach (VariableDeclaration decl in declarators){
5031 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
5032 current_local_parameters, decl.Location);
5035 return implicit_block;
5038 void CheckAttributeTarget (string a)
5042 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
5046 Location l = lexer.Location;
5047 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
5053 void CheckUnaryOperator (Operator.OpType op)
5057 case Operator.OpType.LogicalNot:
5058 case Operator.OpType.OnesComplement:
5059 case Operator.OpType.Increment:
5060 case Operator.OpType.Decrement:
5061 case Operator.OpType.True:
5062 case Operator.OpType.False:
5063 case Operator.OpType.Addition:
5064 case Operator.OpType.Subtraction:
5069 Location l = lexer.Location;
5070 Report.Error (1019, l, "Overloadable unary operator expected");
5076 void CheckBinaryOperator (Operator.OpType op)
5080 case Operator.OpType.Addition:
5081 case Operator.OpType.Subtraction:
5082 case Operator.OpType.Multiply:
5083 case Operator.OpType.Division:
5084 case Operator.OpType.Modulus:
5085 case Operator.OpType.BitwiseAnd:
5086 case Operator.OpType.BitwiseOr:
5087 case Operator.OpType.ExclusiveOr:
5088 case Operator.OpType.LeftShift:
5089 case Operator.OpType.RightShift:
5090 case Operator.OpType.Equality:
5091 case Operator.OpType.Inequality:
5092 case Operator.OpType.GreaterThan:
5093 case Operator.OpType.LessThan:
5094 case Operator.OpType.GreaterThanOrEqual:
5095 case Operator.OpType.LessThanOrEqual:
5099 Location l = lexer.Location;
5100 Report.Error (1020, l, "Overloadable binary operator expected");
5106 void syntax_error (Location l, string msg)
5108 Report.Error (1003, l, "Syntax error, " + msg);
5111 void output (string s)
5113 Console.WriteLine (s);
5116 void note (string s)
5118 // Used to put annotations
5123 public Tokenizer Lexer {
5129 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
5131 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
5132 this.name = file.Name;
5134 current_container = RootContext.Tree.Types;
5135 current_container.NamespaceEntry = current_namespace;
5136 oob_stack = new Stack ();
5137 switch_stack = new Stack ();
5139 lexer = new Tokenizer (reader, file, defines);
5142 public void parse ()
5145 if (yacc_verbose_flag > 1)
5146 yyparse (lexer, new yydebug.yyDebugSimple ());
5149 Tokenizer tokenizer = lexer as Tokenizer;
5150 tokenizer.cleanup ();
5152 } catch (Exception e){
5154 // Removed for production use, use parser verbose to get the output.
5156 // Console.WriteLine (e);
5157 Report.Error (-25, lexer.Location, "Parsing error");
5158 if (yacc_verbose_flag > 0)
5159 Console.WriteLine (e);
5164 void CheckToken (int error, int yyToken, string msg)
5166 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
5167 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
5170 Report.Error (error, lexer.Location, msg);
5173 void CheckIdentifierToken (int yyToken)
5175 CheckToken (1041, yyToken, "Identifier expected");
5178 string ConsumeStoredComment ()
5180 string s = tmpComment;
5182 Lexer.doc_state = XmlCommentState.Allowed;