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 DeclSpace 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 /// Hack to help create non-typed array initializer
65 public static Expression current_array_type;
68 /// Used to determine if we are parsing the get/set pair
69 /// of an indexer or a property
74 /// An out-of-band stack.
83 static public int yacc_verbose_flag;
85 // Name of the file we are parsing
94 /// Temporary Xml documentation cache.
95 /// For enum types, we need one more temporary store.
98 string enumTypeComment;
100 /// Current attribute target
101 string current_attr_target;
103 /// assembly and module attribute definitions are enabled
104 bool global_attrs_enabled = true;
105 bool has_get, has_set;
110 %token NONE /* This token is never returned by our lexer */
111 %token ERROR // This is used not by the parser, but by the tokenizer.
115 *These are the C# keywords
202 /* C# keywords which are not really keywords */
208 /* C# single character operators/punctuation. */
209 %token OPEN_BRACE "{"
210 %token CLOSE_BRACE "}"
211 %token OPEN_BRACKET "["
212 %token CLOSE_BRACKET "]"
213 %token OPEN_PARENS "("
214 %token CLOSE_PARENS ")"
226 %token OP_GENERICS_LT "<"
228 %token OP_GENERICS_GT ">"
229 %token BITWISE_AND "&"
230 %token BITWISE_OR "|"
237 /* C# multi-character operators. */
238 %token DOUBLE_COLON "::"
241 %token OP_SHIFT_LEFT "<<"
242 %token OP_SHIFT_RIGHT ">>"
249 %token OP_MULT_ASSIGN "*="
250 %token OP_DIV_ASSIGN "/="
251 %token OP_MOD_ASSIGN "%="
252 %token OP_ADD_ASSIGN "+="
253 %token OP_SUB_ASSIGN "-="
254 %token OP_SHIFT_LEFT_ASSIGN "<<="
255 %token OP_SHIFT_RIGHT_ASSIGN ">>="
256 %token OP_AND_ASSIGN "&="
257 %token OP_XOR_ASSIGN "^="
258 %token OP_OR_ASSIGN "|="
262 %token LITERAL_INTEGER "int literal"
263 %token LITERAL_FLOAT "float literal"
264 %token LITERAL_DOUBLE "double literal"
265 %token LITERAL_DECIMAL "decimal literal"
266 %token LITERAL_CHARACTER "character literal"
267 %token LITERAL_STRING "string literal"
270 %token CLOSE_PARENS_CAST
271 %token CLOSE_PARENS_NO_CAST
272 %token CLOSE_PARENS_OPEN_PARENS
273 %token CLOSE_PARENS_MINUS
274 %token DEFAULT_OPEN_PARENS
275 %token GENERIC_DIMENSION
277 /* Add precedence rules to solve dangling else s/r conflict */
286 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
288 %left STAR DIV PERCENT
289 %right BANG CARRET UMINUS
290 %nonassoc OP_INC OP_DEC
292 %left OPEN_BRACKET OPEN_BRACE
296 %start compilation_unit
300 : outer_declarations opt_EOF
301 | outer_declarations global_attributes opt_EOF
302 | global_attributes opt_EOF
303 | opt_EOF /* allow empty files */
309 Lexer.check_incorrect_doc_comment ();
313 Lexer.check_incorrect_doc_comment ();
319 | outer_declarations outer_declaration
323 : extern_alias_directive
325 | namespace_member_declaration
328 extern_alias_directives
329 : extern_alias_directive
330 | extern_alias_directives extern_alias_directive;
332 extern_alias_directive
333 : EXTERN IDENTIFIER IDENTIFIER SEMICOLON
335 LocatedToken lt = (LocatedToken) $2;
338 Report.Error (1003, lt.Location, "'alias' expected");
339 } else if (RootContext.Version == LanguageVersion.ISO_1) {
340 Report.FeatureIsNotStandardized (lt.Location, "external alias");
342 lt = (LocatedToken) $3;
343 current_namespace.UsingExternalAlias (lt.Value, lt.Location);
350 | using_directives using_directive
354 : using_alias_directive
356 if (RootContext.Documentation != null)
357 Lexer.doc_state = XmlCommentState.Allowed;
359 | using_namespace_directive
361 if (RootContext.Documentation != null)
362 Lexer.doc_state = XmlCommentState.Allowed;
366 using_alias_directive
367 : USING IDENTIFIER ASSIGN
368 namespace_or_type_name SEMICOLON
370 LocatedToken lt = (LocatedToken) $2;
371 current_namespace.UsingAlias (lt.Value, (MemberName) $4, (Location) $1);
374 CheckIdentifierToken (yyToken, GetLocation ($2));
378 using_namespace_directive
379 : USING namespace_name SEMICOLON
381 current_namespace.Using ((MemberName) $2, (Location) $1);
386 // Strictly speaking, namespaces don't have attributes but
387 // we parse global attributes along with namespace declarations and then
390 namespace_declaration
391 : opt_attributes NAMESPACE namespace_or_type_name
393 MemberName name = (MemberName) $3;
396 Report.Error(1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
399 if (name.TypeArguments != null)
400 syntax_error (lexer.Location, "namespace name expected");
402 current_namespace = new NamespaceEntry (
403 current_namespace, file, name.GetName ());
404 current_class = current_namespace.SlaveDeclSpace;
405 current_container = current_class.PartialContainer;
407 namespace_body opt_semicolon
409 current_namespace = current_namespace.Parent;
410 current_class = current_namespace.SlaveDeclSpace;
411 current_container = current_class.PartialContainer;
426 : namespace_or_type_name {
427 MemberName name = (MemberName) $1;
429 if (name.TypeArguments != null)
430 syntax_error (lexer.Location, "namespace name expected");
439 if (RootContext.Documentation != null)
440 Lexer.doc_state = XmlCommentState.Allowed;
442 opt_extern_alias_directives
444 opt_namespace_member_declarations
453 opt_extern_alias_directives
455 | extern_alias_directives
458 opt_namespace_member_declarations
460 | namespace_member_declarations
463 namespace_member_declarations
464 : namespace_member_declaration
465 | namespace_member_declarations namespace_member_declaration
468 namespace_member_declaration
472 DeclSpace ds = (DeclSpace)$1;
474 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
475 Report.Error (1527, ds.Location,
476 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
479 current_namespace.DeclarationFound = true;
481 | namespace_declaration {
482 current_namespace.DeclarationFound = true;
485 | field_declaration {
486 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
488 | method_declaration {
489 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
496 | interface_declaration
498 | delegate_declaration
500 // Enable this when we have handled all errors, because this acts as a generic fallback
503 // Console.WriteLine ("Token=" + yyToken);
504 // Report.Error (1518, GetLocation ($1), "Expected class, struct, interface, enum or delegate");
516 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
524 global_attrs_enabled = false;
529 global_attrs_enabled = false;
538 ArrayList sect = (ArrayList) $1;
540 if (global_attrs_enabled) {
541 if (current_attr_target == "module") {
542 CodeGen.Module.AddAttributes (sect);
544 } else if (current_attr_target != null && current_attr_target.Length > 0) {
545 CodeGen.Assembly.AddAttributes (sect);
548 $$ = new Attributes (sect);
551 if (RootContext.Documentation != null) {
552 Lexer.check_incorrect_doc_comment ();
554 XmlCommentState.Allowed;
558 $$ = new Attributes (sect);
560 current_attr_target = null;
562 | attribute_sections attribute_section
564 Attributes attrs = $1 as Attributes;
565 ArrayList sect = (ArrayList) $2;
567 if (global_attrs_enabled) {
568 if (current_attr_target == "module") {
569 CodeGen.Module.AddAttributes (sect);
571 } else if (current_attr_target == "assembly") {
572 CodeGen.Assembly.AddAttributes (sect);
576 attrs = new Attributes (sect);
578 attrs.AddAttributes (sect);
582 attrs = new Attributes (sect);
584 attrs.AddAttributes (sect);
587 current_attr_target = null;
592 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
596 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
602 attribute_target_specifier
603 : attribute_target COLON
605 current_attr_target = (string)$1;
613 LocatedToken lt = (LocatedToken) $1;
614 CheckAttributeTarget (lt.Value, lt.Location);
615 $$ = lt.Value; // Location won't be required anymore.
617 | EVENT { $$ = "event"; }
618 | RETURN { $$ = "return"; }
624 ArrayList attrs = new ArrayList (4);
630 | attribute_list COMMA attribute
632 ArrayList attrs = (ArrayList) $1;
640 : attribute_name opt_attribute_arguments
642 MemberName mname = (MemberName) $1;
643 if (mname.IsGeneric) {
644 Report.Error (404, lexer.Location,
645 "'<' unexpected: attributes cannot be generic");
648 object [] arguments = (object []) $2;
649 MemberName left = mname.Left;
650 string identifier = mname.Name;
652 Expression left_expr = left == null ? null : left.GetTypeExpression ();
654 if (current_attr_target == "assembly" || current_attr_target == "module")
655 // FIXME: supply "nameEscaped" parameter here.
656 $$ = new GlobalAttribute (current_namespace, current_attr_target,
657 left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
659 $$ = new Attribute (current_attr_target, left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
664 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
667 opt_attribute_arguments
668 : /* empty */ { $$ = null; }
669 | OPEN_PARENS attribute_arguments CLOSE_PARENS
677 : opt_positional_argument_list
682 $$ = new object [] { $1, null };
685 | positional_argument_list COMMA named_argument_list
687 $$ = new object[] { $1, $3 };
689 | named_argument_list
691 $$ = new object [] { null, $1 };
696 opt_positional_argument_list
697 : /* empty */ { $$ = null; }
698 | positional_argument_list
701 positional_argument_list
704 ArrayList args = new ArrayList (4);
705 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
709 | positional_argument_list COMMA expression
711 ArrayList args = (ArrayList) $1;
712 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
721 ArrayList args = new ArrayList (4);
726 | named_argument_list COMMA named_argument
728 ArrayList args = (ArrayList) $1;
733 | named_argument_list COMMA expression
735 Report.Error (1016, ((Expression) $3).Location, "Named attribute argument expected");
741 : IDENTIFIER ASSIGN expression
743 // FIXME: keep location
744 $$ = new DictionaryEntry (
745 ((LocatedToken) $1).Value,
746 new Argument ((Expression) $3, Argument.AType.Expression));
752 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
755 opt_class_member_declarations
757 | class_member_declarations
760 class_member_declarations
761 : class_member_declaration
762 | class_member_declarations
763 class_member_declaration
766 class_member_declaration
767 : constant_declaration // done
768 | field_declaration // done
769 | method_declaration // done
770 | property_declaration // done
771 | event_declaration // done
772 | indexer_declaration // done
773 | operator_declaration // done
774 | constructor_declaration // done
775 | destructor_declaration // done
785 lexer.ConstraintsParsing = true;
789 MemberName name = MakeName ((MemberName) $6);
790 push_current_class (new Struct (
791 current_namespace, current_class, name, (int) $2,
792 (Attributes) $1), false, $3);
795 opt_type_parameter_constraints_clauses
797 lexer.ConstraintsParsing = false;
800 current_container.AddBasesForPart (current_class, (ArrayList) $8);
802 current_class.SetParameterInfo ((ArrayList) $9);
804 if (RootContext.Documentation != null)
805 current_container.DocComment = Lexer.consume_doc_comment ();
809 if (RootContext.Documentation != null)
810 Lexer.doc_state = XmlCommentState.Allowed;
814 $$ = pop_current_class ();
816 | opt_attributes opt_modifiers opt_partial STRUCT error {
817 CheckIdentifierToken (yyToken, GetLocation ($5));
824 if (RootContext.Documentation != null)
825 Lexer.doc_state = XmlCommentState.Allowed;
827 opt_struct_member_declarations CLOSE_BRACE
830 opt_struct_member_declarations
832 | struct_member_declarations
835 struct_member_declarations
836 : struct_member_declaration
837 | struct_member_declarations struct_member_declaration
840 struct_member_declaration
841 : constant_declaration
844 | property_declaration
846 | indexer_declaration
847 | operator_declaration
848 | constructor_declaration
852 * This is only included so we can flag error 575:
853 * destructors only allowed on class types
855 | destructor_declaration
866 int modflags = (int) $2;
867 foreach (VariableDeclaration constant in (ArrayList) $5){
868 Location l = constant.Location;
869 if ((modflags & Modifiers.STATIC) != 0) {
870 Report.Error (504, l, "The constant `{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
874 Const c = new Const (
875 current_class, (Expression) $4, (string) constant.identifier,
876 (Expression) constant.expression_or_array_initializer, modflags,
879 if (RootContext.Documentation != null) {
880 c.DocComment = Lexer.consume_doc_comment ();
881 Lexer.doc_state = XmlCommentState.Allowed;
883 current_container.AddConstant (c);
889 : constant_declarator
891 ArrayList constants = new ArrayList (4);
896 | constant_declarators COMMA constant_declarator
899 ArrayList constants = (ArrayList) $1;
906 : IDENTIFIER ASSIGN constant_expression
908 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
912 // A const field requires a value to be provided
913 Report.Error (145, ((LocatedToken) $1).Location, "A const field requires a value to be provided");
925 Expression type = (Expression) $3;
928 current_array_type = null;
930 foreach (VariableDeclaration var in (ArrayList) $4){
931 Field field = new Field (current_class, type, mod, var.identifier,
932 (Attributes) $1, var.Location);
934 field.Initializer = var.expression_or_array_initializer;
936 if (RootContext.Documentation != null) {
937 field.DocComment = Lexer.consume_doc_comment ();
938 Lexer.doc_state = XmlCommentState.Allowed;
940 current_container.AddField (field);
941 $$ = field; // FIXME: might be better if it points to the top item
948 fixed_variable_declarators
951 Expression type = (Expression) $4;
954 current_array_type = null;
956 foreach (VariableDeclaration var in (ArrayList) $5) {
957 FixedField field = new FixedField (current_class, type, mod, var.identifier,
958 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
960 if (RootContext.Documentation != null) {
961 field.DocComment = Lexer.consume_doc_comment ();
962 Lexer.doc_state = XmlCommentState.Allowed;
964 current_container.AddField (field);
965 $$ = field; // FIXME: might be better if it points to the top item
973 current_array_type = null;
974 Report.Error (670, (Location) $3, "Fields cannot have void type");
978 fixed_variable_declarators
979 : fixed_variable_declarator
981 ArrayList decl = new ArrayList (2);
985 | fixed_variable_declarators COMMA fixed_variable_declarator
987 ArrayList decls = (ArrayList) $1;
993 fixed_variable_declarator
994 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
996 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
998 | IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1000 Report.Error (443, lexer.Location, "Value or constant expected");
1001 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1005 variable_declarators
1006 : variable_declarator
1008 ArrayList decl = new ArrayList (4);
1013 | variable_declarators COMMA variable_declarator
1015 ArrayList decls = (ArrayList) $1;
1022 : IDENTIFIER ASSIGN variable_initializer
1024 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1028 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1030 | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
1032 Report.Error (650, ((LocatedToken) $1).Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
1033 "To declare a fixed size buffer field, use the fixed keyword before the field type");
1038 variable_initializer
1047 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
1049 $$ = new StackAlloc ((Expression) $2, (Expression) $4, (Location) $1);
1053 $$ = new ArglistAccess ((Location) $1);
1057 Report.Error (1575, (Location) $1, "A stackalloc expression requires [] after type");
1064 iterator_container = (IIteratorContainer) $1;
1065 if (RootContext.Documentation != null)
1066 Lexer.doc_state = XmlCommentState.NotAllowed;
1070 Method method = (Method) $1;
1071 method.Block = (ToplevelBlock) $3;
1072 current_container.AddMethod (method);
1074 current_local_parameters = null;
1075 iterator_container = null;
1077 if (RootContext.Documentation != null)
1078 Lexer.doc_state = XmlCommentState.Allowed;
1091 Report.Error (1585, lexer.Location,
1092 "Member modifier `{0}' must precede the member type and name",
1093 Modifiers.Name (i));
1104 type namespace_or_type_name
1105 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1107 lexer.ConstraintsParsing = true;
1109 opt_type_parameter_constraints_clauses
1111 lexer.ConstraintsParsing = false;
1113 MemberName name = (MemberName) $4;
1115 if ($9 != null && name.TypeArguments == null)
1116 Report.Error (80, lexer.Location,
1117 "Constraints are not allowed on non-generic declarations");
1121 GenericMethod generic = null;
1122 if (name.TypeArguments != null) {
1123 generic = new GenericMethod (current_namespace, current_class, name,
1124 (Expression) $3, (Parameters) $6);
1126 generic.SetParameterInfo ((ArrayList) $9);
1129 method = new Method (current_class, generic, (Expression) $3, (int) $2, false,
1130 name, (Parameters) $6, (Attributes) $1);
1132 current_local_parameters = (Parameters) $6;
1134 if (RootContext.Documentation != null)
1135 method.DocComment = Lexer.consume_doc_comment ();
1141 VOID namespace_or_type_name
1142 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1144 lexer.ConstraintsParsing = true;
1146 opt_type_parameter_constraints_clauses
1148 lexer.ConstraintsParsing = false;
1150 MemberName name = (MemberName) $4;
1152 if ($9 != null && name.TypeArguments == null)
1153 Report.Error (80, lexer.Location,
1154 "Constraints are not allowed on non-generic declarations");
1157 GenericMethod generic = null;
1158 if (name.TypeArguments != null) {
1159 generic = new GenericMethod (current_namespace, current_class, name,
1160 TypeManager.system_void_expr, (Parameters) $6);
1162 generic.SetParameterInfo ((ArrayList) $9);
1165 method = new Method (current_class, generic, TypeManager.system_void_expr,
1166 (int) $2, false, name, (Parameters) $6, (Attributes) $1);
1168 current_local_parameters = (Parameters) $6;
1170 if (RootContext.Documentation != null)
1171 method.DocComment = Lexer.consume_doc_comment ();
1178 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1180 MemberName name = (MemberName) $5;
1181 Report.Error (1585, name.Location,
1182 "Member modifier `{0}' must precede the member type and name", Modifiers.Name ((int) $4));
1184 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1185 0, false, name, (Parameters) $6, (Attributes) $1);
1187 current_local_parameters = (Parameters) $6;
1189 if (RootContext.Documentation != null)
1190 method.DocComment = Lexer.consume_doc_comment ();
1198 | SEMICOLON { $$ = null; }
1201 opt_formal_parameter_list
1202 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1203 | formal_parameter_list
1206 formal_parameter_list
1209 ArrayList pars_list = (ArrayList) $1;
1211 Parameter [] pars = new Parameter [pars_list.Count];
1212 pars_list.CopyTo (pars);
1214 $$ = new Parameters (pars);
1216 | fixed_parameters COMMA parameter_array
1218 ArrayList pars_list = (ArrayList) $1;
1221 Parameter [] pars = new Parameter [pars_list.Count];
1222 pars_list.CopyTo (pars);
1224 $$ = new Parameters (pars);
1226 | fixed_parameters COMMA ARGLIST
1228 ArrayList pars_list = (ArrayList) $1;
1229 //pars_list.Add (new ArglistParameter (GetLocation ($3)));
1231 Parameter [] pars = new Parameter [pars_list.Count];
1232 pars_list.CopyTo (pars);
1234 $$ = new Parameters (pars, true);
1236 | parameter_array COMMA error
1239 Report.Error (231, ((Parameter) $1).Location, "A params parameter must be the last parameter in a formal parameter list");
1242 | fixed_parameters COMMA parameter_array COMMA error
1245 Report.Error (231, ((Parameter) $3).Location, "A params parameter must be the last parameter in a formal parameter list");
1248 | ARGLIST COMMA error
1250 Report.Error (257, (Location) $1, "An __arglist parameter must be the last parameter in a formal parameter list");
1253 | fixed_parameters COMMA ARGLIST COMMA error
1255 Report.Error (257, (Location) $3, "An __arglist parameter must be the last parameter in a formal parameter list");
1260 $$ = new Parameters (new Parameter[] { (Parameter) $1 } );
1264 $$ = new Parameters (new Parameter[0], true);
1271 ArrayList pars = new ArrayList (4);
1276 | fixed_parameters COMMA fixed_parameter
1278 ArrayList pars = (ArrayList) $1;
1287 opt_parameter_modifier
1291 LocatedToken lt = (LocatedToken) $4;
1292 $$ = new Parameter ((Expression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1295 opt_parameter_modifier
1297 IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1299 LocatedToken lt = (LocatedToken) $4;
1300 Report.Error (1552, lt.Location, "Array type specifier, [], must appear before parameter name");
1304 opt_parameter_modifier
1307 Report.Error (1001, GetLocation ($3), "Identifier expected");
1311 opt_parameter_modifier
1314 CheckIdentifierToken (yyToken, GetLocation ($4));
1318 opt_parameter_modifier
1324 LocatedToken lt = (LocatedToken) $4;
1325 Report.Error (241, lt.Location, "Default parameter specifiers are not permitted");
1330 opt_parameter_modifier
1331 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1332 | parameter_modifier
1336 : REF { $$ = Parameter.Modifier.REF; }
1337 | OUT { $$ = Parameter.Modifier.OUT; }
1341 : opt_attributes PARAMS type IDENTIFIER
1343 LocatedToken lt = (LocatedToken) $4;
1344 $$ = new ParamsParameter ((Expression) $3, lt.Value, (Attributes) $1, lt.Location);
1345 note ("type must be a single-dimension array type");
1347 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1349 Report.Error (1611, (Location) $2, "The params parameter cannot be declared as ref or out");
1352 | opt_attributes PARAMS type error {
1353 CheckIdentifierToken (yyToken, GetLocation ($4));
1358 property_declaration
1362 namespace_or_type_name
1364 if (RootContext.Documentation != null)
1365 tmpComment = Lexer.consume_doc_comment ();
1369 implicit_value_parameter_type = (Expression) $3;
1371 lexer.PropertyParsing = true;
1373 accessor_declarations
1375 lexer.PropertyParsing = false;
1376 has_get = has_set = false;
1384 Pair pair = (Pair) $8;
1385 Accessor get_block = (Accessor) pair.First;
1386 Accessor set_block = (Accessor) pair.Second;
1388 MemberName name = (MemberName) $4;
1390 if (name.TypeArguments != null)
1391 syntax_error (lexer.Location, "a property can't have type arguments");
1393 prop = new Property (current_class, (Expression) $3, (int) $2, false,
1394 name, (Attributes) $1, get_block, set_block);
1396 current_container.AddProperty (prop);
1397 implicit_value_parameter_type = null;
1399 if (RootContext.Documentation != null)
1400 prop.DocComment = ConsumeStoredComment ();
1405 accessor_declarations
1406 : get_accessor_declaration
1408 $$ = new Pair ($1, null);
1410 | get_accessor_declaration accessor_declarations
1412 Pair pair = (Pair) $2;
1416 | set_accessor_declaration
1418 $$ = new Pair (null, $1);
1420 | set_accessor_declaration accessor_declarations
1422 Pair pair = (Pair) $2;
1428 Report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1433 get_accessor_declaration
1434 : opt_attributes opt_modifiers GET
1436 // If this is not the case, then current_local_parameters has already
1437 // been set in indexer_declaration
1438 if (parsing_indexer == false)
1439 current_local_parameters = null;
1441 current_local_parameters = indexer_parameters;
1442 lexer.PropertyParsing = false;
1444 iterator_container = SimpleIteratorContainer.GetSimple ();
1449 Report.Error (1007, (Location) $3, "Property accessor already defined");
1452 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1454 current_local_parameters = null;
1455 lexer.PropertyParsing = true;
1457 if (SimpleIteratorContainer.Simple.Yields)
1458 accessor.SetYields ();
1460 iterator_container = null;
1462 if (RootContext.Documentation != null)
1463 if (Lexer.doc_state == XmlCommentState.Error)
1464 Lexer.doc_state = XmlCommentState.NotAllowed;
1470 set_accessor_declaration
1471 : opt_attributes opt_modifiers SET
1474 Parameter implicit_value_parameter = new Parameter (
1475 implicit_value_parameter_type, "value",
1476 Parameter.Modifier.NONE, null, (Location) $3);
1478 if (parsing_indexer == false) {
1479 args = new Parameter [1];
1480 args [0] = implicit_value_parameter;
1481 current_local_parameters = new Parameters (args);
1483 Parameter [] fpars = indexer_parameters.FixedParameters;
1486 int count = fpars.Length;
1488 args = new Parameter [count + 1];
1489 fpars.CopyTo (args, 0);
1490 args [count] = implicit_value_parameter;
1493 current_local_parameters = new Parameters (
1497 lexer.PropertyParsing = false;
1499 iterator_container = SimpleIteratorContainer.GetSimple ();
1504 Report.Error (1007, ((LocatedToken) $3).Location, "Property accessor already defined");
1507 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1509 current_local_parameters = null;
1510 lexer.PropertyParsing = true;
1512 if (SimpleIteratorContainer.Simple.Yields)
1513 accessor.SetYields ();
1515 iterator_container = null;
1517 if (RootContext.Documentation != null
1518 && Lexer.doc_state == XmlCommentState.Error)
1519 Lexer.doc_state = XmlCommentState.NotAllowed;
1527 | SEMICOLON { $$ = null; }
1530 interface_declaration
1536 lexer.ConstraintsParsing = true;
1540 MemberName name = MakeName ((MemberName) $6);
1542 push_current_class (new Interface (
1543 current_namespace, current_class, name, (int) $2,
1544 (Attributes) $1), true, $3);
1547 opt_type_parameter_constraints_clauses
1549 lexer.ConstraintsParsing = false;
1552 current_container.AddBasesForPart (current_class, (ArrayList) $8);
1554 current_class.SetParameterInfo ((ArrayList) $9);
1556 if (RootContext.Documentation != null) {
1557 current_container.DocComment = Lexer.consume_doc_comment ();
1558 Lexer.doc_state = XmlCommentState.Allowed;
1563 if (RootContext.Documentation != null)
1564 Lexer.doc_state = XmlCommentState.Allowed;
1568 $$ = pop_current_class ();
1570 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1571 CheckIdentifierToken (yyToken, GetLocation ($5));
1577 opt_interface_member_declarations
1581 opt_interface_member_declarations
1583 | interface_member_declarations
1586 interface_member_declarations
1587 : interface_member_declaration
1588 | interface_member_declarations interface_member_declaration
1591 interface_member_declaration
1592 : interface_method_declaration
1597 Method m = (Method) $1;
1599 if (m.IsExplicitImpl)
1600 Report.Error (541, m.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1601 m.GetSignatureForError ());
1603 current_container.AddMethod (m);
1605 if (RootContext.Documentation != null)
1606 Lexer.doc_state = XmlCommentState.Allowed;
1608 | interface_property_declaration
1613 Property p = (Property) $1;
1615 if (p.IsExplicitImpl)
1616 Report.Error (541, p.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1617 p.GetSignatureForError ());
1619 current_container.AddProperty (p);
1621 if (RootContext.Documentation != null)
1622 Lexer.doc_state = XmlCommentState.Allowed;
1624 | interface_event_declaration
1627 Event e = (Event) $1;
1629 if (e.IsExplicitImpl)
1630 Report.Error (541, e.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1631 e.GetSignatureForError ());
1633 current_container.AddEvent (e);
1636 if (RootContext.Documentation != null)
1637 Lexer.doc_state = XmlCommentState.Allowed;
1639 | interface_indexer_declaration
1644 Indexer i = (Indexer) $1;
1646 if (i.IsExplicitImpl)
1647 Report.Error (541, i.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1648 i.GetSignatureForError ());
1650 current_container.AddIndexer (i);
1652 if (RootContext.Documentation != null)
1653 Lexer.doc_state = XmlCommentState.Allowed;
1655 | delegate_declaration
1658 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1659 ((MemberCore)$1).GetSignatureForError ());
1665 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1666 ((MemberCore)$1).GetSignatureForError ());
1669 | struct_declaration
1672 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1673 ((MemberCore)$1).GetSignatureForError ());
1679 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1680 ((MemberCore)$1).GetSignatureForError ());
1683 | interface_declaration
1686 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1687 ((MemberCore)$1).GetSignatureForError ());
1690 | constant_declaration
1692 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1700 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, GetLocation ($1));
1705 interface_method_declaration_body
1708 lexer.ConstraintsParsing = false;
1710 opt_statement_list CLOSE_BRACE
1712 Report.Error (531, lexer.Location,
1713 "'{0}': interface members cannot have a definition", ((MemberName) $-1).ToString ());
1719 interface_method_declaration
1720 : opt_attributes opt_new type namespace_or_type_name
1721 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1723 lexer.ConstraintsParsing = true;
1725 opt_type_parameter_constraints_clauses
1727 // Refer to the name as $-1 in interface_method_declaration_body
1730 interface_method_declaration_body
1732 lexer.ConstraintsParsing = false;
1734 MemberName name = (MemberName) $4;
1736 if ($9 != null && name.TypeArguments == null)
1737 Report.Error (80, lexer.Location,
1738 "Constraints are not allowed on non-generic declarations");
1740 GenericMethod generic = null;
1741 if (name.TypeArguments != null) {
1742 generic = new GenericMethod (current_namespace, current_class, name,
1743 (Expression) $3, (Parameters) $6);
1745 generic.SetParameterInfo ((ArrayList) $9);
1748 $$ = new Method (current_class, generic, (Expression) $3, (int) $2, true, name,
1749 (Parameters) $6, (Attributes) $1);
1750 if (RootContext.Documentation != null)
1751 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1753 | opt_attributes opt_new VOID namespace_or_type_name
1754 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1756 lexer.ConstraintsParsing = true;
1758 opt_type_parameter_constraints_clauses
1762 interface_method_declaration_body
1764 lexer.ConstraintsParsing = false;
1766 MemberName name = (MemberName) $4;
1768 if ($9 != null && name.TypeArguments == null)
1769 Report.Error (80, lexer.Location,
1770 "Constraints are not allowed on non-generic declarations");
1772 GenericMethod generic = null;
1773 if (name.TypeArguments != null) {
1774 generic = new GenericMethod (current_namespace, current_class, name,
1775 TypeManager.system_void_expr, (Parameters) $6);
1777 generic.SetParameterInfo ((ArrayList) $9);
1780 $$ = new Method (current_class, generic, TypeManager.system_void_expr, (int) $2,
1781 true, name, (Parameters) $6, (Attributes) $1);
1782 if (RootContext.Documentation != null)
1783 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1787 interface_property_declaration
1792 { lexer.PropertyParsing = true; }
1793 accessor_declarations
1795 has_get = has_set = false;
1796 lexer.PropertyParsing = false;
1800 LocatedToken lt = (LocatedToken) $4;
1801 MemberName name = new MemberName (lt.Value, lt.Location);
1803 if ($3 == TypeManager.system_void_expr) {
1804 Report.Error (547, lt.Location, "`{0}': property or indexer cannot have void type", lt.Value);
1810 p = new Property (current_class, (Expression) $3, (int) $2, true,
1811 name, (Attributes) $1,
1814 Report.Error (548, p.Location, "`{0}': property or indexer must have at least one accessor", p.GetSignatureForError ());
1818 Pair pair = (Pair) $7;
1819 p = new Property (current_class, (Expression) $3, (int) $2, true,
1820 name, (Attributes) $1,
1821 (Accessor)pair.First, (Accessor)pair.Second);
1823 if (pair.First != null && ((Accessor)(pair.First)).Block != null) {
1824 Report.Error (531, p.Location, "`{0}.get': interface members cannot have a definition", p.GetSignatureForError ());
1829 if (pair.Second != null && ((Accessor)(pair.Second)).Block != null) {
1830 Report.Error (531, p.Location, "`{0}.set': interface members cannot have a definition", p.GetSignatureForError ());
1835 if (RootContext.Documentation != null)
1836 p.DocComment = Lexer.consume_doc_comment ();
1843 CheckIdentifierToken (yyToken, GetLocation ($4));
1849 interface_event_declaration
1850 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1852 LocatedToken lt = (LocatedToken) $5;
1853 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1854 new MemberName (lt.Value, lt.Location),
1856 if (RootContext.Documentation != null)
1857 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1859 | opt_attributes opt_new EVENT type error {
1860 CheckIdentifierToken (yyToken, GetLocation ($5));
1863 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1864 LocatedToken lt = (LocatedToken) $5;
1865 Report.Error (68, lt.Location, "`{0}.{1}': event in interface cannot have initializer", current_container.Name, lt.Value);
1868 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1870 lexer.EventParsing = true;
1872 event_accessor_declarations
1874 lexer.EventParsing = false;
1877 Report.Error (69, (Location) $3, "Event in interface cannot have add or remove accessors");
1882 interface_indexer_declaration
1883 : opt_attributes opt_new type THIS
1884 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1886 { lexer.PropertyParsing = true; }
1887 accessor_declarations
1889 has_get = has_set = false;
1890 lexer.PropertyParsing = false;
1896 i = new Indexer (current_class, (Expression) $3,
1897 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1898 (int) $2, true, (Parameters) $6, (Attributes) $1,
1901 Report.Error (548, i.Location, "`{0}': property or indexer must have at least one accessor", i.GetSignatureForError ());
1905 Pair pair = (Pair) $10;
1906 i = new Indexer (current_class, (Expression) $3,
1907 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1908 (int) $2, true, (Parameters) $6, (Attributes) $1,
1909 (Accessor)pair.First, (Accessor)pair.Second);
1911 if (pair.First != null && ((Accessor)(pair.First)).Block != null) {
1912 Report.Error (531, i.Location, "`{0}.get': interface members cannot have a definition", i.GetSignatureForError ());
1917 if (pair.Second != null && ((Accessor)(pair.Second)).Block != null) {
1918 Report.Error (531, i.Location, "`{0}.set': interface members cannot have a definition", i.GetSignatureForError ());
1923 if (RootContext.Documentation != null)
1924 i.DocComment = ConsumeStoredComment ();
1930 operator_declaration
1931 : opt_attributes opt_modifiers operator_declarator
1933 iterator_container = SimpleIteratorContainer.GetSimple ();
1940 OperatorDeclaration decl = (OperatorDeclaration) $3;
1942 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1944 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null, decl.location);
1945 if (decl.arg2type != null)
1946 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null, decl.location);
1948 Operator op = new Operator (
1949 current_class, decl.optype, decl.ret_type, (int) $2,
1950 new Parameters (param_list),
1951 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1953 if (RootContext.Documentation != null) {
1954 op.DocComment = tmpComment;
1955 Lexer.doc_state = XmlCommentState.Allowed;
1958 if (SimpleIteratorContainer.Simple.Yields)
1961 // Note again, checking is done in semantic analysis
1962 current_container.AddOperator (op);
1964 current_local_parameters = null;
1965 iterator_container = null;
1971 | SEMICOLON { $$ = null; }
1974 : type OPERATOR overloadable_operator
1975 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1977 LocatedToken lt = (LocatedToken) $6;
1978 Operator.OpType op = (Operator.OpType) $3;
1979 CheckUnaryOperator (op, lt.Location);
1981 if (op == Operator.OpType.Addition)
1982 op = Operator.OpType.UnaryPlus;
1984 if (op == Operator.OpType.Subtraction)
1985 op = Operator.OpType.UnaryNegation;
1987 Parameter [] pars = new Parameter [1];
1988 Expression type = (Expression) $5;
1990 pars [0] = new Parameter (type, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1992 current_local_parameters = new Parameters (pars);
1994 if (RootContext.Documentation != null) {
1995 tmpComment = Lexer.consume_doc_comment ();
1996 Lexer.doc_state = XmlCommentState.NotAllowed;
1999 $$ = new OperatorDeclaration (op, (Expression) $1, type, lt.Value,
2000 null, null, (Location) $2);
2002 | type OPERATOR overloadable_operator
2004 type IDENTIFIER COMMA
2008 LocatedToken ltParam1 = (LocatedToken) $6;
2009 LocatedToken ltParam2 = (LocatedToken) $9;
2010 CheckBinaryOperator ((Operator.OpType) $3, (Location) $2);
2012 Parameter [] pars = new Parameter [2];
2014 Expression typeL = (Expression) $5;
2015 Expression typeR = (Expression) $8;
2017 pars [0] = new Parameter (typeL, ltParam1.Value, Parameter.Modifier.NONE, null, ltParam1.Location);
2018 pars [1] = new Parameter (typeR, ltParam2.Value, Parameter.Modifier.NONE, null, ltParam2.Location);
2020 current_local_parameters = new Parameters (pars);
2022 if (RootContext.Documentation != null) {
2023 tmpComment = Lexer.consume_doc_comment ();
2024 Lexer.doc_state = XmlCommentState.NotAllowed;
2027 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
2028 typeL, ltParam1.Value,
2029 typeR, ltParam2.Value, (Location) $2);
2031 | conversion_operator_declarator
2032 | type OPERATOR overloadable_operator
2034 type IDENTIFIER COMMA
2035 type IDENTIFIER COMMA
2039 Report.Error (1534, (Location) $2, "Overloaded binary operator `{0}' takes two parameters",
2040 Operator.GetName ((Operator.OpType) $3));
2043 | type OPERATOR overloadable_operator
2044 OPEN_PARENS CLOSE_PARENS
2046 Report.Error (1535, (Location) $2, "Overloaded unary operator `{0}' takes one parameter",
2047 Operator.GetName ((Operator.OpType) $3));
2052 overloadable_operator
2054 : BANG { $$ = Operator.OpType.LogicalNot; }
2055 | TILDE { $$ = Operator.OpType.OnesComplement; }
2056 | OP_INC { $$ = Operator.OpType.Increment; }
2057 | OP_DEC { $$ = Operator.OpType.Decrement; }
2058 | TRUE { $$ = Operator.OpType.True; }
2059 | FALSE { $$ = Operator.OpType.False; }
2060 // Unary and binary:
2061 | PLUS { $$ = Operator.OpType.Addition; }
2062 | MINUS { $$ = Operator.OpType.Subtraction; }
2064 | STAR { $$ = Operator.OpType.Multiply; }
2065 | DIV { $$ = Operator.OpType.Division; }
2066 | PERCENT { $$ = Operator.OpType.Modulus; }
2067 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
2068 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
2069 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
2070 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
2071 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
2072 | OP_EQ { $$ = Operator.OpType.Equality; }
2073 | OP_NE { $$ = Operator.OpType.Inequality; }
2074 | OP_GT { $$ = Operator.OpType.GreaterThan; }
2075 | OP_LT { $$ = Operator.OpType.LessThan; }
2076 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
2077 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
2080 conversion_operator_declarator
2081 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
2083 LocatedToken lt = (LocatedToken) $6;
2084 Parameter [] pars = new Parameter [1];
2086 pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2088 current_local_parameters = new Parameters (pars);
2090 if (RootContext.Documentation != null) {
2091 tmpComment = Lexer.consume_doc_comment ();
2092 Lexer.doc_state = XmlCommentState.NotAllowed;
2095 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, lt.Value,
2096 null, null, (Location) $2);
2098 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
2100 LocatedToken lt = (LocatedToken) $6;
2101 Parameter [] pars = new Parameter [1];
2103 pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2105 current_local_parameters = new Parameters (pars);
2107 if (RootContext.Documentation != null) {
2108 tmpComment = Lexer.consume_doc_comment ();
2109 Lexer.doc_state = XmlCommentState.NotAllowed;
2112 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, lt.Value,
2113 null, null, (Location) $2);
2117 syntax_error ((Location) $1, "'operator' expected");
2121 syntax_error ((Location) $1, "'operator' expected");
2125 constructor_declaration
2128 constructor_declarator
2131 Constructor c = (Constructor) $3;
2132 c.Block = (ToplevelBlock) $4;
2133 c.OptAttributes = (Attributes) $1;
2134 c.ModFlags = (int) $2;
2136 if (RootContext.Documentation != null)
2137 c.DocComment = ConsumeStoredComment ();
2139 if (c.Name == current_container.Basename){
2140 if ((c.ModFlags & Modifiers.STATIC) != 0){
2141 if ((c.ModFlags & Modifiers.Accessibility) != 0){
2142 Report.Error (515, c.Location,
2143 "`{0}': access modifiers are not allowed on static constructors",
2144 c.GetSignatureForError ());
2147 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2149 if (c.Initializer != null){
2150 Report.Error (514, c.Location,
2151 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2152 c.GetSignatureForError ());
2155 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2158 // We let another layer check the validity of the constructor.
2159 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
2162 current_container.AddConstructor (c);
2164 current_local_parameters = null;
2165 if (RootContext.Documentation != null)
2166 Lexer.doc_state = XmlCommentState.Allowed;
2170 constructor_declarator
2173 if (RootContext.Documentation != null) {
2174 tmpComment = Lexer.consume_doc_comment ();
2175 Lexer.doc_state = XmlCommentState.Allowed;
2178 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2180 current_local_parameters = (Parameters) $4;
2182 opt_constructor_initializer
2184 LocatedToken lt = (LocatedToken) $1;
2185 $$ = new Constructor (current_class, lt.Value, 0, (Parameters) $4,
2186 (ConstructorInitializer) $7, lt.Location);
2192 | SEMICOLON { $$ = null; }
2195 opt_constructor_initializer
2196 : /* empty */ { $$ = null; }
2197 | constructor_initializer
2200 constructor_initializer
2201 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
2203 $$ = new ConstructorBaseInitializer ((ArrayList) $4, (Location) $2);
2205 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
2207 $$ = new ConstructorThisInitializer ((ArrayList) $4, (Location) $2);
2210 Report.Error (1018, (Location) $1, "Keyword this or base expected");
2216 : /* EMPTY */ { $$ = 0; }
2217 | UNSAFE { $$ = Modifiers.UNSAFE; }
2218 | EXTERN { $$ = Modifiers.EXTERN; }
2221 destructor_declaration
2222 : opt_attributes opt_finalizer TILDE
2224 if (RootContext.Documentation != null) {
2225 tmpComment = Lexer.consume_doc_comment ();
2226 Lexer.doc_state = XmlCommentState.NotAllowed;
2229 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2231 LocatedToken lt = (LocatedToken) $5;
2232 if (lt.Value != current_container.MemberName.Name){
2233 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2234 } else if (current_container.Kind != Kind.Class){
2235 Report.Error (575, lt.Location, "Only class types can contain destructor");
2237 Location l = lt.Location;
2240 if (!RootContext.StdLib && current_container.Name == "System.Object")
2241 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2243 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2245 Method d = new Destructor (
2246 current_class, TypeManager.system_void_expr, m, "Finalize",
2247 Parameters.EmptyReadOnlyParameters, (Attributes) $1, l);
2248 if (RootContext.Documentation != null)
2249 d.DocComment = ConsumeStoredComment ();
2251 d.Block = (ToplevelBlock) $8;
2252 current_container.AddMethod (d);
2260 EVENT type variable_declarators SEMICOLON
2262 current_array_type = null;
2263 foreach (VariableDeclaration var in (ArrayList) $5) {
2265 MemberName name = new MemberName (var.identifier,
2268 EventField e = new EventField (
2269 current_class, (Expression) $4, (int) $2, false, name,
2272 e.Initializer = var.expression_or_array_initializer;
2274 current_container.AddEvent (e);
2276 if (RootContext.Documentation != null) {
2277 e.DocComment = Lexer.consume_doc_comment ();
2278 Lexer.doc_state = XmlCommentState.Allowed;
2284 EVENT type namespace_or_type_name
2287 implicit_value_parameter_type = (Expression) $4;
2288 lexer.EventParsing = true;
2290 event_accessor_declarations
2292 lexer.EventParsing = false;
2296 MemberName name = (MemberName) $5;
2299 Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2300 current_container.Name, name.ToString ());
2303 Pair pair = (Pair) $8;
2305 if (name.TypeArguments != null)
2306 syntax_error (lexer.Location, "an event can't have type arguments");
2308 if (pair.First == null || pair.Second == null)
2309 // CS0073 is already reported, so no CS0065 here.
2312 Event e = new EventProperty (
2313 current_class, (Expression) $4, (int) $2, false, name,
2314 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second);
2315 if (RootContext.Documentation != null) {
2316 e.DocComment = Lexer.consume_doc_comment ();
2317 Lexer.doc_state = XmlCommentState.Allowed;
2320 current_container.AddEvent (e);
2321 implicit_value_parameter_type = null;
2325 | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
2326 MemberName mn = (MemberName) $5;
2328 if (mn.Left != null)
2329 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2331 Report.Error (71, mn.Location, "Event declaration should use property syntax");
2333 if (RootContext.Documentation != null)
2334 Lexer.doc_state = XmlCommentState.Allowed;
2338 event_accessor_declarations
2339 : add_accessor_declaration remove_accessor_declaration
2341 $$ = new Pair ($1, $2);
2343 | remove_accessor_declaration add_accessor_declaration
2345 $$ = new Pair ($2, $1);
2347 | add_accessor_declaration { $$ = null; }
2348 | remove_accessor_declaration { $$ = null; }
2351 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2357 add_accessor_declaration
2358 : opt_attributes ADD
2360 Parameter [] args = new Parameter [1];
2361 Parameter implicit_value_parameter = new Parameter (
2362 implicit_value_parameter_type, "value",
2363 Parameter.Modifier.NONE, null, (Location) $2);
2365 args [0] = implicit_value_parameter;
2367 current_local_parameters = new Parameters (args);
2368 lexer.EventParsing = false;
2372 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2373 lexer.EventParsing = true;
2375 | opt_attributes ADD error {
2376 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2379 | opt_attributes modifiers ADD {
2380 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2385 remove_accessor_declaration
2386 : opt_attributes REMOVE
2388 Parameter [] args = new Parameter [1];
2389 Parameter implicit_value_parameter = new Parameter (
2390 implicit_value_parameter_type, "value",
2391 Parameter.Modifier.NONE, null, (Location) $2);
2393 args [0] = implicit_value_parameter;
2395 current_local_parameters = new Parameters (args);
2396 lexer.EventParsing = false;
2400 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2401 lexer.EventParsing = true;
2403 | opt_attributes REMOVE error {
2404 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2407 | opt_attributes modifiers REMOVE {
2408 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2414 : opt_attributes opt_modifiers indexer_declarator
2417 IndexerDeclaration decl = (IndexerDeclaration) $3;
2419 implicit_value_parameter_type = decl.type;
2421 lexer.PropertyParsing = true;
2422 parsing_indexer = true;
2424 indexer_parameters = decl.param_list;
2425 iterator_container = SimpleIteratorContainer.GetSimple ();
2427 accessor_declarations
2429 lexer.PropertyParsing = false;
2430 has_get = has_set = false;
2431 parsing_indexer = false;
2438 // The signature is computed from the signature of the indexer. Look
2439 // at section 3.6 on the spec
2441 IndexerDeclaration decl = (IndexerDeclaration) $3;
2442 Pair pair = (Pair) $6;
2443 Accessor get_block = (Accessor) pair.First;
2444 Accessor set_block = (Accessor) pair.Second;
2447 if (decl.interface_type != null)
2448 name = new MemberName (
2449 decl.interface_type, TypeContainer.DefaultIndexerName, decl.location);
2451 name = new MemberName (TypeContainer.DefaultIndexerName, decl.location);
2453 indexer = new Indexer (current_class, decl.type, name,
2454 (int) $2, false, decl.param_list, (Attributes) $1,
2455 get_block, set_block);
2457 if (RootContext.Documentation != null)
2458 indexer.DocComment = ConsumeStoredComment ();
2460 current_container.AddIndexer (indexer);
2462 current_local_parameters = null;
2463 implicit_value_parameter_type = null;
2464 indexer_parameters = null;
2469 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2471 Parameters pars = (Parameters) $4;
2472 if (pars.HasArglist) {
2473 // "__arglist is not valid in this context"
2474 Report.Error (1669, (Location) $2, "__arglist is not valid in this context");
2475 } else if (pars.Empty){
2476 Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
2478 if (RootContext.Documentation != null) {
2479 tmpComment = Lexer.consume_doc_comment ();
2480 Lexer.doc_state = XmlCommentState.Allowed;
2483 $$ = new IndexerDeclaration ((Expression) $1, null, pars, (Location) $2);
2485 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2487 Parameters pars = (Parameters) $6;
2489 if (pars.HasArglist) {
2490 // "__arglist is not valid in this context"
2491 Report.Error (1669, (Location) $4, "__arglist is not valid in this context");
2492 } else if (pars.Empty){
2493 Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
2496 MemberName name = (MemberName) $2;
2497 $$ = new IndexerDeclaration ((Expression) $1, name, pars, (Location) $4);
2499 if (RootContext.Documentation != null) {
2500 tmpComment = Lexer.consume_doc_comment ();
2501 Lexer.doc_state = XmlCommentState.Allowed;
2512 if (RootContext.Documentation != null)
2513 enumTypeComment = Lexer.consume_doc_comment ();
2518 LocatedToken lt = (LocatedToken) $5;
2519 Location enum_location = lt.Location;
2522 Report.Error (267, lt.Location, "The partial modifier can only appear immediately before `class', `struct' or `interface'");
2523 break; // assumes that the parser put us in a switch
2526 MemberName name = MakeName (new MemberName (lt.Value, enum_location));
2527 Enum e = new Enum (current_namespace, current_class, (Expression) $6, (int) $2,
2528 name, (Attributes) $1);
2530 if (RootContext.Documentation != null)
2531 e.DocComment = enumTypeComment;
2534 EnumMember em = null;
2535 foreach (VariableDeclaration ev in (ArrayList) $8) {
2536 em = new EnumMember (e, em, (Expression) ev.expression_or_array_initializer,
2537 new MemberName (ev.identifier, ev.Location), ev.OptAttributes);
2539 // if (RootContext.Documentation != null)
2540 em.DocComment = ev.DocComment;
2542 e.AddEnumMember (em);
2545 current_container.AddEnum (e);
2552 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2553 | COLON type { $$ = $2; }
2559 if (RootContext.Documentation != null)
2560 Lexer.doc_state = XmlCommentState.Allowed;
2562 opt_enum_member_declarations
2564 // here will be evaluated after CLOSE_BLACE is consumed.
2565 if (RootContext.Documentation != null)
2566 Lexer.doc_state = XmlCommentState.Allowed;
2574 opt_enum_member_declarations
2575 : /* empty */ { $$ = new ArrayList (4); }
2576 | enum_member_declarations opt_comma { $$ = $1; }
2579 enum_member_declarations
2580 : enum_member_declaration
2582 ArrayList l = new ArrayList (4);
2587 | enum_member_declarations COMMA enum_member_declaration
2589 ArrayList l = (ArrayList) $1;
2597 enum_member_declaration
2598 : opt_attributes IDENTIFIER
2600 VariableDeclaration vd = new VariableDeclaration (
2601 (LocatedToken) $2, null, (Attributes) $1);
2603 if (RootContext.Documentation != null) {
2604 vd.DocComment = Lexer.consume_doc_comment ();
2605 Lexer.doc_state = XmlCommentState.Allowed;
2610 | opt_attributes IDENTIFIER
2612 if (RootContext.Documentation != null) {
2613 tmpComment = Lexer.consume_doc_comment ();
2614 Lexer.doc_state = XmlCommentState.NotAllowed;
2619 VariableDeclaration vd = new VariableDeclaration (
2620 (LocatedToken) $2, $5, (Attributes) $1);
2622 if (RootContext.Documentation != null)
2623 vd.DocComment = ConsumeStoredComment ();
2629 delegate_declaration
2634 lexer.ConstraintsParsing = true;
2637 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2639 MemberName name = MakeName ((MemberName) $6);
2640 Parameters p = (Parameters) $8;
2642 // TODO: wrong location
2643 Report.Error (1669, name.Location, "__arglist is not valid in this context");
2646 Delegate del = new Delegate (current_namespace, current_class, (Expression) $5,
2647 (int) $2, name, p, (Attributes) $1);
2649 if (RootContext.Documentation != null) {
2650 del.DocComment = Lexer.consume_doc_comment ();
2651 Lexer.doc_state = XmlCommentState.Allowed;
2654 current_container.AddDelegate (del);
2655 current_delegate = del;
2657 opt_type_parameter_constraints_clauses
2659 lexer.ConstraintsParsing = false;
2663 current_delegate.SetParameterInfo ((ArrayList) $11);
2664 $$ = current_delegate;
2666 current_delegate = null;
2673 lexer.CheckNullable (false);
2678 lexer.CheckNullable (true);
2683 namespace_or_type_name
2685 | IDENTIFIER DOUBLE_COLON IDENTIFIER {
2686 LocatedToken lt1 = (LocatedToken) $1;
2687 LocatedToken lt2 = (LocatedToken) $3;
2688 $$ = new MemberName (lt1.Value, lt2.Value, lt2.Location);
2690 | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list {
2691 LocatedToken lt = (LocatedToken) $3;
2692 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $4, lt.Location);
2697 : IDENTIFIER opt_type_argument_list {
2698 LocatedToken lt = (LocatedToken) $1;
2699 $$ = new MemberName (lt.Value, (TypeArguments) $2, lt.Location);
2703 opt_type_argument_list
2704 : /* empty */ { $$ = null; }
2705 | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2708 if (RootContext.Version == LanguageVersion.ISO_1)
2709 Report.FeatureIsNotStandardized (lexer.Location, "generics");
2713 $$ = new TypeArguments ((int) $1, lexer.Location);
2714 if (RootContext.Version == LanguageVersion.ISO_1)
2715 Report.FeatureIsNotStandardized (lexer.Location, "generics");
2720 : opt_attributes type {
2721 TypeArguments type_args = new TypeArguments (lexer.Location);
2723 SimpleName sn = $2 as SimpleName;
2725 Report.Error (1031, lexer.Location, "Type expected");
2727 $2 = new TypeParameterName (sn.Name, (Attributes) $1, lexer.Location);
2729 type_args.Add ((Expression) $2);
2732 | type_arguments COMMA opt_attributes type {
2733 TypeArguments type_args = (TypeArguments) $1;
2735 SimpleName sn = $4 as SimpleName;
2737 Report.Error (1031, lexer.Location, "Type expected");
2739 $4 = new TypeParameterName (sn.Name, (Attributes) $3, lexer.Location);
2741 type_args.Add ((Expression) $4);
2748 * Before you think of adding a return_type, notice that we have been
2749 * using two rules in the places where it matters (one rule using type
2750 * and another identical one that uses VOID as the return type). This
2751 * gets rid of a shift/reduce couple
2754 : namespace_or_type_name opt_nullable
2756 MemberName name = (MemberName) $1;
2757 $$ = name.GetTypeExpression ();
2760 $$ = new ComposedCast ((Expression) $$, "?", lexer.Location);
2762 | builtin_types opt_nullable
2765 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2775 // Note that here only unmanaged types are allowed but we
2776 // can't perform checks during this phase - we do it during
2777 // semantic analysis.
2779 $$ = new ComposedCast ((Expression) $1, "*", Lexer.Location);
2783 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2788 : builtin_types opt_nullable
2791 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2793 | non_expression_type rank_specifier
2795 Location loc = GetLocation ($1);
2797 loc = lexer.Location;
2798 $$ = new ComposedCast ((Expression) $1, (string) $2, loc);
2800 | non_expression_type STAR
2802 Location loc = GetLocation ($1);
2804 loc = lexer.Location;
2805 $$ = new ComposedCast ((Expression) $1, "*", loc);
2807 | expression rank_specifiers
2809 $$ = new ComposedCast ((Expression) $1, (string) $2);
2813 $$ = new ComposedCast ((Expression) $1, "*");
2817 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2818 // through this different path
2820 | multiplicative_expression STAR
2822 $$ = new ComposedCast ((Expression) $1, "*");
2829 ArrayList types = new ArrayList (4);
2834 | type_list COMMA type
2836 ArrayList types = (ArrayList) $1;
2844 * replaces all the productions for isolating the various
2845 * simple types, but we need this to reuse it easily in local_variable_type
2848 : OBJECT { $$ = TypeManager.system_object_expr; }
2849 | STRING { $$ = TypeManager.system_string_expr; }
2850 | BOOL { $$ = TypeManager.system_boolean_expr; }
2851 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2852 | FLOAT { $$ = TypeManager.system_single_expr; }
2853 | DOUBLE { $$ = TypeManager.system_double_expr; }
2858 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2859 | BYTE { $$ = TypeManager.system_byte_expr; }
2860 | SHORT { $$ = TypeManager.system_int16_expr; }
2861 | USHORT { $$ = TypeManager.system_uint16_expr; }
2862 | INT { $$ = TypeManager.system_int32_expr; }
2863 | UINT { $$ = TypeManager.system_uint32_expr; }
2864 | LONG { $$ = TypeManager.system_int64_expr; }
2865 | ULONG { $$ = TypeManager.system_uint64_expr; }
2866 | CHAR { $$ = TypeManager.system_char_expr; }
2867 | VOID { $$ = TypeManager.system_void_expr; }
2871 : type rank_specifiers opt_nullable
2873 string rank_specifiers = (string) $2;
2875 rank_specifiers += "?";
2877 $$ = current_array_type = new ComposedCast ((Expression) $1, rank_specifiers);
2882 // Expressions, section 7.5
2891 MemberName mn = (MemberName) $1;
2892 $$ = mn.GetTypeExpression ();
2894 | IDENTIFIER DOUBLE_COLON IDENTIFIER
2896 LocatedToken lt1 = (LocatedToken) $1;
2897 LocatedToken lt2 = (LocatedToken) $3;
2898 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, lt2.Location);
2900 | parenthesized_expression
2901 | default_value_expression
2903 | invocation_expression
2907 | post_increment_expression
2908 | post_decrement_expression
2912 | checked_expression
2913 | unchecked_expression
2914 | pointer_member_access
2915 | anonymous_method_expression
2922 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
2923 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); }
2924 | NULL { $$ = new NullLiteral (lexer.Location); }
2928 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
2929 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
2930 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
2935 object v = lexer.Value;
2938 $$ = new IntLiteral ((int) v, lexer.Location);
2939 } else if (v is uint)
2940 $$ = new UIntLiteral ((UInt32) v, lexer.Location);
2942 $$ = new LongLiteral ((Int64) v, lexer.Location);
2943 else if (v is ulong)
2944 $$ = new ULongLiteral ((UInt64) v, lexer.Location);
2946 Console.WriteLine ("OOPS. Unexpected result from scanner");
2951 : TRUE { $$ = new BoolLiteral (true, lexer.Location); }
2952 | FALSE { $$ = new BoolLiteral (false, lexer.Location); }
2955 parenthesized_expression_0
2956 : OPEN_PARENS expression CLOSE_PARENS
2959 lexer.Deambiguate_CloseParens ();
2960 // After this, the next token returned is one of
2961 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2962 // or CLOSE_PARENS_MINUS.
2964 | OPEN_PARENS expression error { CheckToken (1026, yyToken, "Expecting ')'", lexer.Location); }
2967 parenthesized_expression
2968 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2972 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2974 // If a parenthesized expression is followed by a minus, we need to wrap
2975 // the expression inside a ParenthesizedExpression for the CS0075 check
2976 // in Binary.DoResolve().
2977 $$ = new ParenthesizedExpression ((Expression) $1);
2982 : primary_expression DOT IDENTIFIER opt_type_argument_list
2984 LocatedToken lt = (LocatedToken) $3;
2985 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4);
2987 | predefined_type DOT IDENTIFIER opt_type_argument_list
2989 LocatedToken lt = (LocatedToken) $3;
2990 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4);
2998 invocation_expression
2999 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
3002 Report.Error (1, (Location) $2, "Parse error");
3004 $$ = new Invocation ((Expression) $1, (ArrayList) $3);
3006 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
3008 $$ = new Invocation ((Expression) $1, new ArrayList ());
3010 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
3012 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
3014 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
3016 ArrayList args = new ArrayList (1);
3018 $$ = new Invocation ((Expression) $1, args);
3020 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
3022 ArrayList args = ((ArrayList) $4);
3024 $$ = new Invocation ((Expression) $1, args);
3029 : /* empty */ { $$ = null; }
3036 ArrayList list = new ArrayList (4);
3040 | argument_list COMMA argument
3042 ArrayList list = (ArrayList) $1;
3046 | argument_list error {
3047 CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
3055 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
3057 | non_simple_argument
3064 : REF variable_reference
3066 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
3068 | OUT variable_reference
3070 $$ = new Argument ((Expression) $2, Argument.AType.Out);
3072 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
3074 ArrayList list = (ArrayList) $3;
3075 Argument[] args = new Argument [list.Count];
3076 list.CopyTo (args, 0);
3078 Expression expr = new Arglist (args, (Location) $1);
3079 $$ = new Argument (expr, Argument.AType.Expression);
3083 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
3088 : expression { note ("section 5.4"); $$ = $1; }
3092 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
3094 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
3096 | primary_expression rank_specifiers
3098 // So the super-trick is that primary_expression
3099 // can only be either a SimpleName or a MemberAccess.
3100 // The MemberAccess case arises when you have a fully qualified type-name like :
3102 // SimpleName is when you have
3105 Expression expr = (Expression) $1;
3106 if (expr is ComposedCast){
3107 $$ = new ComposedCast (expr, (string) $2);
3108 } else if (!(expr is SimpleName || expr is MemberAccess || expr is ConstructedType || expr is QualifiedAliasMember)){
3109 Error_ExpectingTypeName (expr);
3110 $$ = TypeManager.system_object_expr;
3113 // So we extract the string corresponding to the SimpleName
3116 $$ = new ComposedCast (expr, (string) $2);
3118 current_array_type = (Expression)$$;
3125 ArrayList list = new ArrayList (4);
3129 | expression_list COMMA expression
3131 ArrayList list = (ArrayList) $1;
3140 $$ = new This (current_block, (Location) $1);
3145 : BASE DOT IDENTIFIER opt_type_argument_list
3147 LocatedToken lt = (LocatedToken) $3;
3148 $$ = new BaseAccess (lt.Value, (TypeArguments) $4, lt.Location);
3150 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
3152 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
3155 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
3160 post_increment_expression
3161 : primary_expression OP_INC
3163 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
3164 (Expression) $1, (Location) $2);
3168 post_decrement_expression
3169 : primary_expression OP_DEC
3171 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
3172 (Expression) $1, (Location) $2);
3177 : object_or_delegate_creation_expression
3178 | array_creation_expression
3181 object_or_delegate_creation_expression
3182 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
3184 $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
3188 array_creation_expression
3189 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
3191 opt_array_initializer
3193 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
3195 | NEW type rank_specifiers array_initializer
3197 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, (Location) $1);
3201 Report.Error (1031, (Location) $1, "Type expected");
3206 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
3222 opt_rank_specifier_or_nullable
3231 | opt_nullable rank_specifiers
3238 | opt_nullable rank_specifiers INTERR
3241 $$ = "?" + $2 + "?";
3248 : rank_specifier opt_rank_specifier
3250 $$ = (string) $2 + (string) $1;
3255 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3257 $$ = "[" + (string) $2 + "]";
3277 | dim_separators COMMA
3279 $$ = (string) $1 + ",";
3283 opt_array_initializer
3295 : OPEN_BRACE CLOSE_BRACE
3297 ArrayList list = new ArrayList (4);
3300 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3302 $$ = (ArrayList) $2;
3306 variable_initializer_list
3307 : variable_initializer
3309 ArrayList list = new ArrayList (4);
3313 | variable_initializer_list COMMA variable_initializer
3315 ArrayList list = (ArrayList) $1;
3321 void_pointer_expression
3322 : void_pointer_expression STAR
3324 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3328 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3333 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
3335 $$ = new TypeOfVoid ((Location) $1);
3337 | TYPEOF OPEN_PARENS void_pointer_expression CLOSE_PARENS
3339 $$ = new TypeOf ((Expression) $3, (Location) $1);
3341 | TYPEOF OPEN_PARENS
3343 lexer.TypeOfParsing = true;
3347 lexer.TypeOfParsing = false;
3348 $$ = new TypeOf ((Expression) $4, lexer.Location);
3353 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3354 $$ = new SizeOf ((Expression) $3, (Location) $1);
3359 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3361 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3365 unchecked_expression
3366 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3368 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3372 pointer_member_access
3373 : primary_expression OP_PTR IDENTIFIER
3376 LocatedToken lt = (LocatedToken) $3;
3378 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
3379 $$ = new MemberAccess (deref, lt.Value);
3383 anonymous_method_expression
3384 : DELEGATE opt_anonymous_method_signature
3386 if (oob_stack == null)
3387 oob_stack = new Stack (6);
3389 oob_stack.Push (current_local_parameters);
3390 current_local_parameters = (Parameters)$2;
3392 // Force the next block to be created as a ToplevelBlock
3393 oob_stack.Push (current_block);
3394 oob_stack.Push (top_current_block);
3395 current_block = null;
3399 Location loc = (Location) $1;
3400 top_current_block = (Block) oob_stack.Pop ();
3401 current_block = (Block) oob_stack.Pop ();
3402 if (RootContext.Version == LanguageVersion.ISO_1){
3403 Report.FeatureIsNotStandardized (loc, "anonymous methods");
3406 ToplevelBlock anon_block = (ToplevelBlock) $4;
3408 anon_block.Parent = current_block;
3409 $$ = new AnonymousMethod (current_container, (Parameters) $2, (ToplevelBlock) top_current_block,
3412 current_local_parameters = (Parameters) oob_stack.Pop ();
3416 opt_anonymous_method_signature
3417 : /* empty */ { $$ = null; }
3418 | anonymous_method_signature
3421 anonymous_method_signature
3422 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3425 $$ = Parameters.EmptyReadOnlyParameters;
3427 ArrayList par_list = (ArrayList) $2;
3428 Parameter [] pars = new Parameter [par_list.Count];
3429 par_list.CopyTo (pars);
3430 $$ = new Parameters (pars);
3435 opt_anonymous_method_parameter_list
3436 : /* empty */ { $$ = null; }
3437 | anonymous_method_parameter_list { $$ = $1; }
3440 anonymous_method_parameter_list
3441 : anonymous_method_parameter
3443 ArrayList a = new ArrayList (4);
3447 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3449 ArrayList a = (ArrayList) $1;
3455 anonymous_method_parameter
3456 : opt_parameter_modifier type IDENTIFIER {
3457 LocatedToken lt = (LocatedToken) $3;
3458 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3460 | PARAMS type IDENTIFIER {
3461 Report.Error (1670, ((LocatedToken) $3).Location, "The `params' modifier is not allowed in anonymous method declaration");
3466 default_value_expression
3467 : DEFAULT_OPEN_PARENS type CLOSE_PARENS
3469 $$ = new DefaultValueExpression ((Expression) $2, lexer.Location);
3474 : primary_expression
3475 | BANG prefixed_unary_expression
3477 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3479 | TILDE prefixed_unary_expression
3481 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3487 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3489 $$ = new Cast ((Expression) $1, (Expression) $3);
3491 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3493 $$ = new Cast ((Expression) $1, (Expression) $3);
3499 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3501 // TODO: wrong location
3502 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3507 // The idea to split this out is from Rhys' grammar
3508 // to solve the problem with casts.
3510 prefixed_unary_expression
3512 | PLUS prefixed_unary_expression
3514 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3516 | MINUS prefixed_unary_expression
3518 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3520 | OP_INC prefixed_unary_expression
3522 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3523 (Expression) $2, (Location) $1);
3525 | OP_DEC prefixed_unary_expression
3527 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3528 (Expression) $2, (Location) $1);
3530 | STAR prefixed_unary_expression
3532 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, (Location) $1);
3534 | BITWISE_AND prefixed_unary_expression
3536 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3540 pre_increment_expression
3541 : OP_INC prefixed_unary_expression
3543 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3544 (Expression) $2, (Location) $1);
3548 pre_decrement_expression
3549 : OP_DEC prefixed_unary_expression
3551 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3552 (Expression) $2, (Location) $1);
3556 multiplicative_expression
3557 : prefixed_unary_expression
3558 | multiplicative_expression STAR prefixed_unary_expression
3560 $$ = new Binary (Binary.Operator.Multiply,
3561 (Expression) $1, (Expression) $3);
3563 | multiplicative_expression DIV prefixed_unary_expression
3565 $$ = new Binary (Binary.Operator.Division,
3566 (Expression) $1, (Expression) $3);
3568 | multiplicative_expression PERCENT prefixed_unary_expression
3570 $$ = new Binary (Binary.Operator.Modulus,
3571 (Expression) $1, (Expression) $3);
3576 : multiplicative_expression
3577 | additive_expression PLUS multiplicative_expression
3579 $$ = new Binary (Binary.Operator.Addition,
3580 (Expression) $1, (Expression) $3);
3582 | additive_expression MINUS multiplicative_expression
3584 $$ = new Binary (Binary.Operator.Subtraction,
3585 (Expression) $1, (Expression) $3);
3590 : additive_expression
3591 | shift_expression OP_SHIFT_LEFT additive_expression
3593 $$ = new Binary (Binary.Operator.LeftShift,
3594 (Expression) $1, (Expression) $3);
3596 | shift_expression OP_SHIFT_RIGHT additive_expression
3598 $$ = new Binary (Binary.Operator.RightShift,
3599 (Expression) $1, (Expression) $3);
3610 lexer.PutbackNullable ();
3615 nullable_type_or_conditional
3618 if (((bool) $2) && ($1 is ComposedCast))
3619 $$ = ((ComposedCast) $1).RemoveNullable ();
3625 relational_expression
3627 | relational_expression OP_LT shift_expression
3629 $$ = new Binary (Binary.Operator.LessThan,
3630 (Expression) $1, (Expression) $3);
3632 | relational_expression OP_GT shift_expression
3634 $$ = new Binary (Binary.Operator.GreaterThan,
3635 (Expression) $1, (Expression) $3);
3637 | relational_expression OP_LE shift_expression
3639 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3640 (Expression) $1, (Expression) $3);
3642 | relational_expression OP_GE shift_expression
3644 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3645 (Expression) $1, (Expression) $3);
3647 | relational_expression IS
3650 } nullable_type_or_conditional
3652 $$ = new Is ((Expression) $1, (Expression) $4, (Location) $2);
3654 | relational_expression AS
3657 } nullable_type_or_conditional
3659 $$ = new As ((Expression) $1, (Expression) $4, (Location) $2);
3664 : relational_expression
3665 | equality_expression OP_EQ relational_expression
3667 $$ = new Binary (Binary.Operator.Equality,
3668 (Expression) $1, (Expression) $3);
3670 | equality_expression OP_NE relational_expression
3672 $$ = new Binary (Binary.Operator.Inequality,
3673 (Expression) $1, (Expression) $3);
3678 : equality_expression
3679 | and_expression BITWISE_AND equality_expression
3681 $$ = new Binary (Binary.Operator.BitwiseAnd,
3682 (Expression) $1, (Expression) $3);
3686 exclusive_or_expression
3688 | exclusive_or_expression CARRET and_expression
3690 $$ = new Binary (Binary.Operator.ExclusiveOr,
3691 (Expression) $1, (Expression) $3);
3695 inclusive_or_expression
3696 : exclusive_or_expression
3697 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3699 $$ = new Binary (Binary.Operator.BitwiseOr,
3700 (Expression) $1, (Expression) $3);
3704 conditional_and_expression
3705 : inclusive_or_expression
3706 | conditional_and_expression OP_AND inclusive_or_expression
3708 $$ = new Binary (Binary.Operator.LogicalAnd,
3709 (Expression) $1, (Expression) $3);
3713 conditional_or_expression
3714 : conditional_and_expression
3715 | conditional_or_expression OP_OR conditional_and_expression
3717 $$ = new Binary (Binary.Operator.LogicalOr,
3718 (Expression) $1, (Expression) $3);
3722 conditional_expression
3723 : conditional_or_expression
3724 | conditional_or_expression INTERR expression COLON expression
3726 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
3728 | conditional_or_expression INTERR INTERR expression
3730 $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $4, lexer.Location);
3732 // We'll be resolved into a `parenthesized_expression_0' later on.
3733 | conditional_or_expression INTERR CLOSE_PARENS
3735 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
3736 lexer.PutbackCloseParens ();
3740 assignment_expression
3741 : prefixed_unary_expression ASSIGN expression
3743 $$ = new Assign ((Expression) $1, (Expression) $3);
3745 | prefixed_unary_expression OP_MULT_ASSIGN expression
3747 $$ = new CompoundAssign (
3748 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
3750 | prefixed_unary_expression OP_DIV_ASSIGN expression
3752 $$ = new CompoundAssign (
3753 Binary.Operator.Division, (Expression) $1, (Expression) $3);
3755 | prefixed_unary_expression OP_MOD_ASSIGN expression
3757 $$ = new CompoundAssign (
3758 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
3760 | prefixed_unary_expression OP_ADD_ASSIGN expression
3762 $$ = new CompoundAssign (
3763 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
3765 | prefixed_unary_expression OP_SUB_ASSIGN expression
3767 $$ = new CompoundAssign (
3768 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3770 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3772 $$ = new CompoundAssign (
3773 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
3775 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3777 $$ = new CompoundAssign (
3778 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
3780 | prefixed_unary_expression OP_AND_ASSIGN expression
3782 $$ = new CompoundAssign (
3783 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
3785 | prefixed_unary_expression OP_OR_ASSIGN expression
3787 $$ = new CompoundAssign (
3788 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
3790 | prefixed_unary_expression OP_XOR_ASSIGN expression
3792 $$ = new CompoundAssign (
3793 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
3798 : conditional_expression
3799 | assignment_expression
3819 lexer.ConstraintsParsing = true;
3823 MemberName name = MakeName ((MemberName) $6);
3824 int mod_flags = (int) $2;
3826 push_current_class (new Class (
3827 current_namespace, current_class, name,
3828 mod_flags, (Attributes) $1), false, $3);
3831 opt_type_parameter_constraints_clauses
3833 lexer.ConstraintsParsing = false;
3836 if (current_class.Name == "System.Object") {
3837 Report.Error (537, current_class.Location,
3838 "The class System.Object cannot have a base " +
3839 "class or implement an interface.");
3841 current_container.AddBasesForPart (current_class, (ArrayList) $8);
3844 current_class.SetParameterInfo ((ArrayList) $9);
3846 if (RootContext.Documentation != null) {
3847 current_container.DocComment = Lexer.consume_doc_comment ();
3848 Lexer.doc_state = XmlCommentState.Allowed;
3853 if (RootContext.Documentation != null)
3854 Lexer.doc_state = XmlCommentState.Allowed;
3858 $$ = pop_current_class ();
3866 { $$ = $1; } // location
3870 : /* empty */ { $$ = (int) 0; }
3876 | modifiers modifier
3881 if ((m1 & m2) != 0) {
3882 Location l = lexer.Location;
3883 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
3885 $$ = (int) (m1 | m2);
3890 : NEW { $$ = Modifiers.NEW; }
3891 | PUBLIC { $$ = Modifiers.PUBLIC; }
3892 | PROTECTED { $$ = Modifiers.PROTECTED; }
3893 | INTERNAL { $$ = Modifiers.INTERNAL; }
3894 | PRIVATE { $$ = Modifiers.PRIVATE; }
3895 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3896 | SEALED { $$ = Modifiers.SEALED; }
3897 | STATIC { $$ = Modifiers.STATIC; }
3898 | READONLY { $$ = Modifiers.READONLY; }
3899 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3900 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3901 | EXTERN { $$ = Modifiers.EXTERN; }
3902 | VOLATILE { $$ = Modifiers.VOLATILE; }
3903 | UNSAFE { $$ = Modifiers.UNSAFE; }
3907 : /* empty */ { $$ = null; }
3908 | class_base { $$ = $1; }
3912 : COLON type_list { $$ = $2; }
3915 opt_type_parameter_constraints_clauses
3916 : /* empty */ { $$ = null; }
3917 | type_parameter_constraints_clauses
3921 type_parameter_constraints_clauses
3922 : type_parameter_constraints_clause {
3923 ArrayList constraints = new ArrayList (1);
3924 constraints.Add ($1);
3927 | type_parameter_constraints_clauses type_parameter_constraints_clause {
3928 ArrayList constraints = (ArrayList) $1;
3929 Constraints new_constraint = (Constraints)$2;
3931 foreach (Constraints c in constraints) {
3932 if (new_constraint.TypeParameter == c.TypeParameter) {
3933 Report.Error (409, new_constraint.Location, "A constraint clause has already been specified for type parameter `{0}'",
3934 new_constraint.TypeParameter);
3938 constraints.Add (new_constraint);
3943 type_parameter_constraints_clause
3944 : WHERE IDENTIFIER COLON type_parameter_constraints {
3945 LocatedToken lt = (LocatedToken) $2;
3946 $$ = new Constraints (lt.Value, (ArrayList) $4, lt.Location);
3950 type_parameter_constraints
3951 : type_parameter_constraint {
3952 ArrayList constraints = new ArrayList (1);
3953 constraints.Add ($1);
3956 | type_parameter_constraints COMMA type_parameter_constraint {
3957 ArrayList constraints = (ArrayList) $1;
3959 constraints.Add ($3);
3964 type_parameter_constraint
3966 | NEW OPEN_PARENS CLOSE_PARENS {
3967 $$ = SpecialConstraint.Constructor;
3970 $$ = SpecialConstraint.ReferenceType;
3973 $$ = SpecialConstraint.ValueType;
3982 // A block is "contained" on the following places:
3984 // property_declaration as part of the accessor body (get/set)
3985 // operator_declaration
3986 // constructor_declaration
3987 // destructor_declaration
3988 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3993 if (current_block == null){
3994 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, (Location) $1);
3995 top_current_block = current_block;
3997 current_block = new Block (current_block, (Location) $1, Location.Null);
4000 opt_statement_list CLOSE_BRACE
4002 while (current_block.Implicit)
4003 current_block = current_block.Parent;
4005 current_block.SetEndLocation ((Location) $4);
4006 current_block = current_block.Parent;
4007 if (current_block == null)
4008 top_current_block = null;
4019 | statement_list statement
4023 : declaration_statement
4025 if ($1 != null && (Block) $1 != current_block){
4026 current_block.AddStatement ((Statement) $1);
4027 current_block = (Block) $1;
4030 | valid_declaration_statement
4032 current_block.AddStatement ((Statement) $1);
4037 valid_declaration_statement
4040 | expression_statement
4041 | selection_statement
4042 | iteration_statement
4046 | unchecked_statement
4054 : valid_declaration_statement
4055 | declaration_statement
4057 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4062 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4070 $$ = EmptyStatement.Value;
4077 LocatedToken lt = (LocatedToken) $1;
4078 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
4080 if (current_block.AddLabel (labeled))
4081 current_block.AddStatement (labeled);
4086 declaration_statement
4087 : local_variable_declaration SEMICOLON
4089 current_array_type = null;
4091 DictionaryEntry de = (DictionaryEntry) $1;
4092 Expression e = (Expression) de.Key;
4094 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
4098 | local_constant_declaration SEMICOLON
4100 current_array_type = null;
4102 DictionaryEntry de = (DictionaryEntry) $1;
4104 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
4110 * The following is from Rhys' grammar:
4111 * > Types in local variable declarations must be recognized as
4112 * > expressions to prevent reduce/reduce errors in the grammar.
4113 * > The expressions are converted into types during semantic analysis.
4116 : primary_expression opt_rank_specifier_or_nullable
4118 // FIXME: Do something smart here regarding the composition of the type.
4120 // Ok, the above "primary_expression" is there to get rid of
4121 // both reduce/reduce and shift/reduces in the grammar, it should
4122 // really just be "type_name". If you use type_name, a reduce/reduce
4123 // creeps up. If you use namespace_or_type_name (which is all we need
4124 // really) two shift/reduces appear.
4127 // So the super-trick is that primary_expression
4128 // can only be either a SimpleName or a MemberAccess.
4129 // The MemberAccess case arises when you have a fully qualified type-name like :
4131 // SimpleName is when you have
4134 Expression expr = (Expression) $1;
4135 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType || expr is QualifiedAliasMember)) {
4136 Error_ExpectingTypeName (expr);
4140 // So we extract the string corresponding to the SimpleName
4144 if ((string) $2 == "")
4147 $$ = new ComposedCast ((Expression) $1, (string) $2);
4150 | builtin_types opt_rank_specifier_or_nullable
4152 if ((string) $2 == "")
4155 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
4159 local_variable_pointer_type
4160 : primary_expression STAR
4162 Expression expr = (Expression) $1;
4164 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType || expr is QualifiedAliasMember)) {
4165 Error_ExpectingTypeName (expr);
4169 $$ = new ComposedCast ((Expression) $1, "*");
4171 | builtin_types STAR
4173 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
4177 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
4179 | local_variable_pointer_type STAR
4181 $$ = new ComposedCast ((Expression) $1, "*");
4185 local_variable_declaration
4186 : local_variable_type variable_declarators
4189 $$ = new DictionaryEntry ($1, $2);
4193 | local_variable_pointer_type opt_rank_specifier_or_nullable variable_declarators
4198 if ((string) $2 == "")
4199 t = (Expression) $1;
4201 t = new ComposedCast ((Expression) $1, (string) $2);
4202 $$ = new DictionaryEntry (t, $3);
4208 local_constant_declaration
4209 : CONST local_variable_type constant_declarators
4212 $$ = new DictionaryEntry ($2, $3);
4218 expression_statement
4219 : statement_expression SEMICOLON { $$ = $1; }
4223 // We have to do the wrapping here and not in the case above,
4224 // because statement_expression is used for example in for_statement
4226 statement_expression
4229 Expression expr = (Expression) $1;
4230 ExpressionStatement s = expr as ExpressionStatement;
4232 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
4235 $$ = new StatementExpression (s);
4239 Report.Error (1002, GetLocation ($1), "Expecting `;'");
4244 object_creation_expression
4245 : object_or_delegate_creation_expression
4246 { note ("complain if this is a delegate maybe?"); }
4255 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
4258 Location l = (Location) $1;
4260 $$ = new If ((Expression) $3, (Statement) $5, l);
4262 // FIXME: location for warning should be loc property of $5.
4263 if ($5 == EmptyStatement.Value)
4264 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4267 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
4268 embedded_statement ELSE embedded_statement
4270 Location l = (Location) $1;
4272 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
4274 // FIXME: location for warning should be loc property of $5 and $7.
4275 if ($5 == EmptyStatement.Value)
4276 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4277 if ($7 == EmptyStatement.Value)
4278 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4283 : SWITCH OPEN_PARENS
4285 if (switch_stack == null)
4286 switch_stack = new Stack (2);
4287 switch_stack.Push (current_block);
4289 expression CLOSE_PARENS
4292 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
4293 current_block = (Block) switch_stack.Pop ();
4309 Report.Error (1522, lexer.Location, "Empty switch block");
4317 ArrayList sections = new ArrayList (4);
4322 | switch_sections switch_section
4324 ArrayList sections = (ArrayList) $1;
4334 current_block = current_block.CreateSwitchBlock (lexer.Location);
4338 Block topmost = current_block;
4340 while (topmost.Implicit)
4341 topmost = topmost.Parent;
4342 $$ = new SwitchSection ((ArrayList) $1, topmost);
4349 ArrayList labels = new ArrayList (4);
4354 | switch_labels switch_label
4356 ArrayList labels = (ArrayList) ($1);
4364 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
4365 | DEFAULT COLON { $$ = new SwitchLabel (null, (Location) $2); }
4368 1523, GetLocation ($1),
4369 "The keyword case or default must precede code in switch block");
4381 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
4383 Location l = (Location) $1;
4384 $$ = new While ((Expression) $3, (Statement) $5, l);
4389 : DO embedded_statement
4390 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
4392 Location l = (Location) $1;
4394 $$ = new Do ((Statement) $2, (Expression) $5, l);
4400 opt_for_initializer SEMICOLON
4402 Block assign_block = new Block (current_block);
4403 current_block = assign_block;
4405 if ($3 is DictionaryEntry){
4406 DictionaryEntry de = (DictionaryEntry) $3;
4408 Expression type = (Expression) de.Key;
4409 ArrayList var_declarators = (ArrayList) de.Value;
4411 foreach (VariableDeclaration decl in var_declarators){
4415 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4419 Location l = lexer.Location;
4420 Expression expr = decl.expression_or_array_initializer;
4422 LocalVariableReference var;
4423 var = new LocalVariableReference (assign_block, decl.identifier, l);
4426 Assign a = new Assign (var, expr, decl.Location);
4428 assign_block.AddStatement (new StatementExpression (a));
4432 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4433 // This can be referred to as $5 below.
4439 opt_for_condition SEMICOLON
4440 opt_for_iterator CLOSE_PARENS
4443 Location l = (Location) $1;
4445 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4447 current_block.AddStatement (f);
4448 while (current_block.Implicit)
4449 current_block = current_block.Parent;
4451 current_block = current_block.Parent;
4456 : /* empty */ { $$ = EmptyStatement.Value; }
4461 : local_variable_declaration
4462 | statement_expression_list
4466 : /* empty */ { $$ = null; }
4467 | boolean_expression
4471 : /* empty */ { $$ = EmptyStatement.Value; }
4476 : statement_expression_list
4479 statement_expression_list
4480 : statement_expression
4482 // CHANGE: was `null'
4483 Statement s = (Statement) $1;
4484 Block b = new Block (current_block, Block.Flags.Implicit, s.loc, lexer.Location);
4489 | statement_expression_list COMMA statement_expression
4491 Block b = (Block) $1;
4493 b.AddStatement ((Statement) $3);
4499 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4501 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4504 | FOREACH OPEN_PARENS type IDENTIFIER IN
4505 expression CLOSE_PARENS
4507 Block foreach_block = new Block (current_block);
4508 current_block = foreach_block;
4510 LocatedToken lt = (LocatedToken) $4;
4511 Location l = lt.Location;
4514 vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4516 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4518 // Get a writable reference to this read-only variable.
4520 // Note that the $$ here refers to the value of _this_ code block,
4521 // not the value of the LHS non-terminal. This can be referred to as $8 below.
4522 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
4529 LocalVariableReference v = (LocalVariableReference) $8;
4530 Location l = (Location) $1;
4533 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
4534 current_block.AddStatement (f);
4537 while (current_block.Implicit)
4538 current_block = current_block.Parent;
4540 current_block = current_block.Parent;
4546 | continue_statement
4556 $$ = new Break ((Location) $1);
4561 : CONTINUE SEMICOLON
4563 $$ = new Continue ((Location) $1);
4568 : GOTO IDENTIFIER SEMICOLON
4570 LocatedToken lt = (LocatedToken) $2;
4571 $$ = new Goto (lt.Value, lt.Location);
4573 | GOTO CASE constant_expression SEMICOLON
4575 $$ = new GotoCase ((Expression) $3, (Location) $1);
4577 | GOTO DEFAULT SEMICOLON
4579 $$ = new GotoDefault ((Location) $1);
4584 : RETURN opt_expression SEMICOLON
4586 $$ = new Return ((Expression) $2, (Location) $1);
4591 : THROW opt_expression SEMICOLON
4593 $$ = new Throw ((Expression) $2, (Location) $1);
4598 : IDENTIFIER RETURN expression SEMICOLON
4600 LocatedToken lt = (LocatedToken) $1;
4601 string s = lt.Value;
4603 Report.Error (1003, lt.Location, "; expected");
4606 if (RootContext.Version == LanguageVersion.ISO_1){
4607 Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4610 if (iterator_container == null){
4611 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4614 iterator_container.SetYields ();
4615 $$ = new Yield ((Expression) $3, lt.Location);
4618 | IDENTIFIER RETURN SEMICOLON
4620 Report.Error (1627, (Location) $2, "Expression expected after yield return");
4623 | IDENTIFIER BREAK SEMICOLON
4625 LocatedToken lt = (LocatedToken) $1;
4626 string s = lt.Value;
4628 Report.Error (1003, lt.Location, "; expected");
4631 if (RootContext.Version == LanguageVersion.ISO_1){
4632 Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4635 if (iterator_container == null){
4636 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4639 iterator_container.SetYields ();
4640 $$ = new YieldBreak (lt.Location);
4651 : TRY block catch_clauses
4655 ArrayList c = (ArrayList)$3;
4656 for (int i = 0; i < c.Count; ++i) {
4657 Catch cc = (Catch) c [i];
4659 if (i != c.Count - 1)
4660 Report.Error (1017, cc.loc, "Try statement already has an empty catch block");
4667 // Now s contains the list of specific catch clauses
4668 // and g contains the general one.
4670 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4672 | TRY block opt_catch_clauses FINALLY block
4675 ArrayList s = new ArrayList (4);
4676 ArrayList catch_list = (ArrayList) $3;
4678 if (catch_list != null){
4679 foreach (Catch cc in catch_list) {
4687 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4691 Report.Error (1524, (Location) $1, "Expected catch or finally");
4697 : /* empty */ { $$ = null; }
4704 ArrayList l = new ArrayList (4);
4709 | catch_clauses catch_clause
4711 ArrayList l = (ArrayList) $1;
4719 : /* empty */ { $$ = null; }
4724 : CATCH opt_catch_args
4726 Expression type = null;
4729 DictionaryEntry cc = (DictionaryEntry) $2;
4730 type = (Expression) cc.Key;
4731 LocatedToken lt = (LocatedToken) cc.Value;
4734 ArrayList one = new ArrayList (4);
4736 one.Add (new VariableDeclaration (lt, null));
4738 current_block = new Block (current_block);
4739 Block b = declare_local_variables (type, one, lt.Location);
4744 Expression type = null;
4746 Block var_block = null;
4749 DictionaryEntry cc = (DictionaryEntry) $2;
4750 type = (Expression) cc.Key;
4751 LocatedToken lt = (LocatedToken) cc.Value;
4755 while (current_block.Implicit)
4756 current_block = current_block.Parent;
4757 var_block = current_block;
4758 current_block = current_block.Parent;
4762 $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
4767 : /* empty */ { $$ = null; }
4772 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4774 $$ = new DictionaryEntry ($2, $3);
4782 $$ = new Checked ((Block) $2);
4789 $$ = new Unchecked ((Block) $2);
4796 RootContext.CheckUnsafeOption ((Location) $1);
4798 $$ = new Unsafe ((Block) $3);
4804 type fixed_pointer_declarators
4807 ArrayList list = (ArrayList) $4;
4808 Expression type = (Expression) $3;
4809 Location l = (Location) $1;
4810 int top = list.Count;
4812 Block assign_block = new Block (current_block);
4813 current_block = assign_block;
4815 for (int i = 0; i < top; i++){
4816 Pair p = (Pair) list [i];
4819 v = current_block.AddVariable (type, (string) p.First, l);
4823 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4831 Location l = (Location) $1;
4833 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4835 current_block.AddStatement (f);
4836 while (current_block.Implicit)
4837 current_block = current_block.Parent;
4839 current_block = current_block.Parent;
4843 fixed_pointer_declarators
4844 : fixed_pointer_declarator {
4845 ArrayList declarators = new ArrayList (4);
4847 declarators.Add ($1);
4850 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4852 ArrayList declarators = (ArrayList) $1;
4854 declarators.Add ($3);
4859 fixed_pointer_declarator
4860 : IDENTIFIER ASSIGN expression
4862 LocatedToken lt = (LocatedToken) $1;
4863 // FIXME: keep location
4864 $$ = new Pair (lt.Value, $3);
4868 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
4874 : LOCK OPEN_PARENS expression CLOSE_PARENS
4880 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
4885 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4887 Block assign_block = new Block (current_block);
4888 current_block = assign_block;
4890 if ($3 is DictionaryEntry){
4891 DictionaryEntry de = (DictionaryEntry) $3;
4892 Location l = (Location) $1;
4894 Expression type = (Expression) de.Key;
4895 ArrayList var_declarators = (ArrayList) de.Value;
4897 ArrayList vars = new ArrayList (4);
4899 foreach (VariableDeclaration decl in var_declarators){
4901 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4904 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
4906 Expression expr = decl.expression_or_array_initializer;
4908 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4911 LocalVariableReference var;
4913 // Get a writable reference to this read-only variable.
4914 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4916 // This is so that it is not a warning on using variables
4919 vars.Add (new DictionaryEntry (var, expr));
4921 // Assign a = new Assign (var, expr, decl.Location);
4922 // assign_block.AddStatement (new StatementExpression (a));
4925 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4926 // It can be referred to as $5 below.
4927 $$ = new DictionaryEntry (type, vars);
4934 Using u = new Using ($5, (Statement) $6, (Location) $1);
4935 current_block.AddStatement (u);
4936 while (current_block.Implicit)
4937 current_block = current_block.Parent;
4939 current_block = current_block.Parent;
4943 resource_acquisition
4944 : local_variable_declaration
4951 // A class used to pass around variable declarations and constants
4953 public class VariableDeclaration {
4954 public string identifier;
4955 public Expression expression_or_array_initializer;
4956 public Location Location;
4957 public Attributes OptAttributes;
4958 public string DocComment;
4960 public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
4962 this.identifier = lt.Value;
4963 if (eoai is ArrayList) {
4964 if (CSharpParser.current_array_type == null)
4965 Report.Error (622, lt.Location,
4966 "Can only use array initializer expressions to assign to array types. Try using a new expression instead.");
4967 this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
4969 this.expression_or_array_initializer = (Expression)eoai;
4971 this.Location = lt.Location;
4972 this.OptAttributes = opt_attrs;
4975 public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
4981 // A class used to hold info about an indexer declarator
4983 public class IndexerDeclaration {
4984 public Expression type;
4985 public MemberName interface_type;
4986 public Parameters param_list;
4987 public Location location;
4989 public IndexerDeclaration (Expression type, MemberName interface_type,
4990 Parameters param_list, Location loc)
4993 this.interface_type = interface_type;
4994 this.param_list = param_list;
4995 this.location = loc;
5000 // We use this when we do not have an object in advance that is an IIteratorContainer
5002 public class SimpleIteratorContainer : IIteratorContainer {
5005 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
5010 public static SimpleIteratorContainer GetSimple () {
5011 Simple.Yields = false;
5015 public void SetYields () { Yields = true; }
5019 // A class used to hold info about an operator declarator
5021 public class OperatorDeclaration {
5022 public Operator.OpType optype;
5023 public Expression ret_type, arg1type, arg2type;
5024 public string arg1name, arg2name;
5025 public Location location;
5027 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
5028 Expression arg1type, string arg1name,
5029 Expression arg2type, string arg2name, Location location)
5032 this.ret_type = ret_type;
5033 this.arg1type = arg1type;
5034 this.arg1name = arg1name;
5035 this.arg2type = arg2type;
5036 this.arg2name = arg2name;
5037 this.location = location;
5042 void Error_ExpectingTypeName (Expression expr)
5044 if (expr is Invocation){
5045 Report.Error (1002, expr.Location, "Expecting `;'");
5047 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
5051 void push_current_class (TypeContainer tc, bool is_interface, object partial_token)
5053 if (partial_token != null)
5054 current_container = current_container.AddPartial (tc, is_interface);
5056 current_container = current_container.AddTypeContainer (tc, is_interface);
5060 DeclSpace pop_current_class ()
5062 DeclSpace retval = current_class;
5064 current_class = current_class.Parent;
5065 current_container = current_class.PartialContainer;
5071 // Given the @class_name name, it creates a fully qualified name
5072 // based on the containing declaration space
5075 MakeName (MemberName class_name)
5077 Namespace ns = current_namespace.NS;
5079 if (current_container.Name.Length == 0){
5080 if (ns.Name.Length != 0)
5081 return new MemberName (ns.MemberName, class_name);
5085 return new MemberName (current_container.MemberName, class_name);
5089 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
5091 Block implicit_block;
5092 ArrayList inits = null;
5095 // We use the `Used' property to check whether statements
5096 // have been added to the current block. If so, we need
5097 // to create another block to contain the new declaration
5098 // otherwise, as an optimization, we use the same block to
5099 // add the declaration.
5101 // FIXME: A further optimization is to check if the statements
5102 // that were added were added as part of the initialization
5103 // below. In which case, no other statements have been executed
5104 // and we might be able to reduce the number of blocks for
5105 // situations like this:
5107 // int j = 1; int k = j + 1;
5109 if (current_block.Used)
5110 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
5112 implicit_block = current_block;
5114 foreach (VariableDeclaration decl in variable_declarators){
5116 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
5117 if (decl.expression_or_array_initializer != null){
5119 inits = new ArrayList (4);
5126 return implicit_block;
5128 foreach (VariableDeclaration decl in inits){
5130 Expression expr = decl.expression_or_array_initializer;
5132 LocalVariableReference var;
5133 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
5135 assign = new Assign (var, expr, decl.Location);
5137 implicit_block.AddStatement (new StatementExpression (assign));
5140 return implicit_block;
5143 Block declare_local_constants (Expression type, ArrayList declarators)
5145 Block implicit_block;
5147 if (current_block.Used)
5148 implicit_block = new Block (current_block, Block.Flags.Implicit);
5150 implicit_block = current_block;
5152 foreach (VariableDeclaration decl in declarators){
5153 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
5156 return implicit_block;
5159 void CheckAttributeTarget (string a, Location l)
5163 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
5167 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
5173 void CheckUnaryOperator (Operator.OpType op, Location l)
5177 case Operator.OpType.LogicalNot:
5178 case Operator.OpType.OnesComplement:
5179 case Operator.OpType.Increment:
5180 case Operator.OpType.Decrement:
5181 case Operator.OpType.True:
5182 case Operator.OpType.False:
5183 case Operator.OpType.Addition:
5184 case Operator.OpType.Subtraction:
5189 Report.Error (1019, l, "Overloadable unary operator expected");
5195 void CheckBinaryOperator (Operator.OpType op, Location l)
5199 case Operator.OpType.Addition:
5200 case Operator.OpType.Subtraction:
5201 case Operator.OpType.Multiply:
5202 case Operator.OpType.Division:
5203 case Operator.OpType.Modulus:
5204 case Operator.OpType.BitwiseAnd:
5205 case Operator.OpType.BitwiseOr:
5206 case Operator.OpType.ExclusiveOr:
5207 case Operator.OpType.LeftShift:
5208 case Operator.OpType.RightShift:
5209 case Operator.OpType.Equality:
5210 case Operator.OpType.Inequality:
5211 case Operator.OpType.GreaterThan:
5212 case Operator.OpType.LessThan:
5213 case Operator.OpType.GreaterThanOrEqual:
5214 case Operator.OpType.LessThanOrEqual:
5218 Report.Error (1020, l, "Overloadable binary operator expected");
5224 void syntax_error (Location l, string msg)
5226 Report.Error (1003, l, "Syntax error, " + msg);
5229 void note (string s)
5231 // Used to put annotations
5236 public Tokenizer Lexer {
5242 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
5244 this.name = file.Name;
5246 current_namespace = new NamespaceEntry (null, file, null);
5247 current_class = current_namespace.SlaveDeclSpace;
5248 current_container = current_class.PartialContainer; // == RootContest.ToplevelTypes
5250 lexer = new Tokenizer (reader, file, defines);
5253 public void parse ()
5255 int errors = Report.Errors;
5257 if (yacc_verbose_flag > 1)
5258 yyparse (lexer, new yydebug.yyDebugSimple ());
5261 Tokenizer tokenizer = lexer as Tokenizer;
5262 tokenizer.cleanup ();
5263 } catch (Exception e){
5265 // Removed for production use, use parser verbose to get the output.
5267 // Console.WriteLine (e);
5268 if (Report.Errors == errors)
5269 Report.Error (-25, lexer.Location, "Parsing error");
5270 if (yacc_verbose_flag > 0)
5271 Console.WriteLine (e);
5274 if (RootContext.ToplevelTypes.NamespaceEntry != null)
5275 throw new InternalErrorException ("who set it?");
5278 static void CheckToken (int error, int yyToken, string msg, Location loc)
5280 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
5281 Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ());
5283 Report.Error (error, loc, msg);
5286 void CheckIdentifierToken (int yyToken, Location loc)
5288 CheckToken (1041, yyToken, "Identifier expected", loc);
5291 string ConsumeStoredComment ()
5293 string s = tmpComment;
5295 Lexer.doc_state = XmlCommentState.Allowed;
5299 Location GetLocation (object obj)
5301 if (obj is MemberCore)
5302 return ((MemberCore) obj).Location;
5303 if (obj is MemberName)
5304 return ((MemberName) obj).Location;
5305 if (obj is LocatedToken)
5306 return ((LocatedToken) obj).Location;
5307 if (obj is Location)
5308 return (Location) obj;
5309 return lexer.Location;