3 // cs-parser.jay: The Parser for the C# compiler
5 // Authors: Miguel de Icaza (miguel@gnu.org)
6 // Ravi Pratap (ravi@ximian.com)
7 // Marek Safar (marek.safar@gmail.com)
9 // Dual Licensed under the terms of the GNU GPL and the MIT X11 license
11 // (C) 2001 Ximian, Inc (http://www.ximian.com)
12 // (C) 2004 Novell, Inc
15 // (1) Figure out why error productions dont work. `type-declaration' is a
16 // great spot to put an `error' because you can reproduce it with this input:
19 // Possible optimization:
20 // Run memory profiler with parsing only, and consider dropping
21 // arraylists where not needed. Some pieces can use linked lists.
30 using System.Collections;
35 public class CSharpParser
38 enum ParameterModifierType
45 DefaultValue = 1 << 6,
47 All = Ref | Out | This | Params | Arglist | DefaultValue
50 NamespaceEntry current_namespace;
51 TypeContainer current_container;
52 DeclSpace current_class;
55 /// Current block is used to add statements as we find
60 Delegate current_delegate;
62 GenericMethod current_generic_method;
63 AnonymousMethodExpression current_anonymous_method;
66 /// This is used by the unary_expression code to resolve
67 /// a name against a parameter.
70 // FIXME: This is very ugly and it's very hard to reset it correctly
71 // on all places, especially when some parameters are autogenerated.
72 ParametersCompiled current_local_parameters;
75 /// Using during property parsing to describe the implicit
76 /// value parameter that is passed to the "set" and "get"accesor
77 /// methods (properties and indexers).
79 FullNamedExpression implicit_value_parameter_type;
80 ParametersCompiled indexer_parameters;
83 /// Hack to help create non-typed array initializer
85 public static FullNamedExpression current_array_type;
86 FullNamedExpression pushed_current_array_type;
89 /// Used to determine if we are parsing the get/set pair
90 /// of an indexer or a property
94 bool parsing_anonymous_method;
97 /// An out-of-band stack.
99 static Stack oob_stack;
107 /// Controls the verbosity of the errors produced by the parser
109 static public int yacc_verbose_flag;
112 /// Used by the interactive shell, flags whether EOF was reached
113 /// and an error was produced
115 public bool UnexpectedEOF;
118 /// The current file.
120 CompilationUnit file;
123 /// Temporary Xml documentation cache.
124 /// For enum types, we need one more temporary store.
127 string enumTypeComment;
129 /// Current attribute target
130 string current_attr_target;
132 /// assembly and module attribute definitions are enabled
133 bool global_attrs_enabled = true;
134 bool has_get, has_set;
136 ParameterModifierType valid_param_mod;
138 bool default_parameter_used;
140 /// When using the interactive parser, this holds the
141 /// resulting expression
142 public object InteractiveResult;
145 // Keeps track of global data changes to undo on parser error
149 // Stack<ToplevelBlock>
150 Stack linq_clause_blocks;
152 // A counter to create new class names in interactive mode
153 static int class_count;
155 CompilerContext compiler;
159 %token NONE /* This token is never returned by our lexer */
160 %token ERROR // This is used not by the parser, but by the tokenizer.
164 *These are the C# keywords
263 %token INTERR_NULLABLE
266 /* Generics <,> tokens */
267 %token OP_GENERICS_LT
268 %token OP_GENERICS_LT_DECL
269 %token OP_GENERICS_GT
271 /* C# keywords which are not really keywords */
277 /* C# single character operators/punctuation. */
305 /* C# multi-character operators. */
310 %token OP_SHIFT_RIGHT
317 %token OP_MULT_ASSIGN
322 %token OP_SHIFT_LEFT_ASSIGN
323 %token OP_SHIFT_RIGHT_ASSIGN
331 %token LITERAL_INTEGER
333 %token LITERAL_DOUBLE
334 %token LITERAL_DECIMAL
335 %token LITERAL_CHARACTER
336 %token LITERAL_STRING
339 %token OPEN_PARENS_LAMBDA
340 %token OPEN_PARENS_CAST
341 %token GENERIC_DIMENSION
344 // Make the parser go into eval mode parsing (statements and compilation units).
345 %token EVAL_STATEMENT_PARSER
346 %token EVAL_COMPILATION_UNIT_PARSER
347 %token EVAL_USING_DECLARATIONS_UNIT_PARSER
350 // This token is generated to trigger the completion engine at this point
352 %token GENERATE_COMPLETION
355 // This token is return repeatedly after the first GENERATE_COMPLETION
356 // token is produced and before the final EOF
358 %token COMPLETE_COMPLETION
360 /* Add precedence rules to solve dangling else s/r conflict */
364 /* Define the operator tokens and their precedences */
372 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
374 %left STAR DIV PERCENT
375 %right BANG CARRET UMINUS
376 %nonassoc OP_INC OP_DEC
378 %left OPEN_BRACKET OPEN_BRACE
381 %start compilation_unit
385 : outer_declarations opt_EOF
386 | outer_declarations global_attributes opt_EOF
387 | global_attributes opt_EOF
388 | opt_EOF /* allow empty files */
389 | interactive_parsing { Lexer.CompleteOnEOF = false; } opt_EOF
395 Lexer.check_incorrect_doc_comment ();
399 Lexer.check_incorrect_doc_comment ();
405 | outer_declarations outer_declaration
409 : extern_alias_directive
411 | namespace_member_declaration
414 extern_alias_directives
415 : extern_alias_directive
416 | extern_alias_directives extern_alias_directive
419 extern_alias_directive
420 : EXTERN_ALIAS IDENTIFIER IDENTIFIER SEMICOLON
422 LocatedToken lt = (LocatedToken) $2;
425 syntax_error (lt.Location, "`alias' expected");
426 } else if (RootContext.Version == LanguageVersion.ISO_1) {
427 Report.FeatureIsNotAvailable (lt.Location, "external alias");
429 lt = (LocatedToken) $3;
430 current_namespace.AddUsingExternalAlias (lt.Value, lt.Location, Report);
435 syntax_error (GetLocation ($1), "`alias' expected"); // TODO: better
441 | using_directives using_directive
445 : using_alias_directive
447 if (RootContext.Documentation != null)
448 Lexer.doc_state = XmlCommentState.Allowed;
450 | using_namespace_directive
452 if (RootContext.Documentation != null)
453 Lexer.doc_state = XmlCommentState.Allowed;
457 using_alias_directive
458 : USING IDENTIFIER ASSIGN namespace_or_type_name SEMICOLON
460 LocatedToken lt = (LocatedToken) $2;
461 current_namespace.AddUsingAlias (lt.Value, (MemberName) $4, (Location) $1);
464 CheckIdentifierToken (yyToken, GetLocation ($2));
469 using_namespace_directive
470 : USING namespace_name SEMICOLON
472 current_namespace.AddUsing ((MemberName) $2, (Location) $1);
477 // Strictly speaking, namespaces don't have attributes but
478 // we parse global attributes along with namespace declarations and then
481 namespace_declaration
482 : opt_attributes NAMESPACE qualified_identifier
484 MemberName name = (MemberName) $3;
487 Report.Error(1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
490 current_namespace = new NamespaceEntry (
491 current_namespace, file, name.GetName ());
492 current_class = current_namespace.SlaveDeclSpace;
493 current_container = current_class.PartialContainer;
495 namespace_body opt_semicolon
497 current_namespace = current_namespace.Parent;
498 current_class = current_namespace.SlaveDeclSpace;
499 current_container = current_class.PartialContainer;
506 LocatedToken lt = (LocatedToken) $1;
507 $$ = new MemberName (lt.Value, lt.Location);
509 | qualified_identifier DOT IDENTIFIER
511 LocatedToken lt = (LocatedToken) $3;
512 $$ = new MemberName ((MemberName) $1, lt.Value, lt.Location);
516 syntax_error (lexer.Location, "`.' expected");
517 $$ = new MemberName ("<invalid>", lexer.Location);
532 : namespace_or_type_name
534 MemberName name = (MemberName) $1;
536 if (name.TypeArguments != null)
537 syntax_error (lexer.Location, "namespace name expected");
546 if (RootContext.Documentation != null)
547 Lexer.doc_state = XmlCommentState.Allowed;
553 : opt_extern_alias_directives
555 opt_namespace_member_declarations
559 Report.Error (1518, lexer.Location, "Expected `class', `delegate', `enum', `interface', or `struct'");
562 | opt_extern_alias_directives
564 opt_namespace_member_declarations
567 Report.Error (1513, lexer.Location, "Expected `}'");
576 opt_extern_alias_directives
578 | extern_alias_directives
581 opt_namespace_member_declarations
583 | namespace_member_declarations
586 namespace_member_declarations
587 : namespace_member_declaration
588 | namespace_member_declarations namespace_member_declaration
591 namespace_member_declaration
595 DeclSpace ds = (DeclSpace)$1;
597 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
598 Report.Error (1527, ds.Location,
599 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
602 current_namespace.DeclarationFound = true;
604 | namespace_declaration {
605 current_namespace.DeclarationFound = true;
608 | field_declaration {
609 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
611 | method_declaration {
612 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
619 | interface_declaration
621 | delegate_declaration
623 // Enable this when we have handled all errors, because this acts as a generic fallback
626 // Console.WriteLine ("Token=" + yyToken);
627 // Report.Error (1518, GetLocation ($1), "Expected class, struct, interface, enum or delegate");
639 Attributes attrs = (Attributes)$1;
640 if (global_attrs_enabled) {
641 CodeGen.Assembly.AddAttributes (attrs.Attrs, current_namespace);
643 foreach (Attribute a in attrs.Attrs) {
644 Report.Error (1730, a.Location, "Assembly and module attributes must precede all other elements except using clauses and extern alias declarations");
655 global_attrs_enabled = false;
660 global_attrs_enabled = false;
669 if (current_attr_target != String.Empty) {
670 ArrayList sect = (ArrayList) $1;
672 if (global_attrs_enabled) {
673 if (current_attr_target == "module") {
674 current_container.Module.Compiled.AddAttributes (sect);
676 } else if (current_attr_target != null && current_attr_target.Length > 0) {
677 CodeGen.Assembly.AddAttributes (sect, current_namespace);
680 $$ = new Attributes (sect);
683 if (RootContext.Documentation != null) {
684 Lexer.check_incorrect_doc_comment ();
686 XmlCommentState.Allowed;
690 $$ = new Attributes (sect);
695 current_attr_target = null;
697 | attribute_sections attribute_section
699 if (current_attr_target != String.Empty) {
700 Attributes attrs = $1 as Attributes;
701 ArrayList sect = (ArrayList) $2;
703 if (global_attrs_enabled) {
704 if (current_attr_target == "module") {
705 current_container.Module.Compiled.AddAttributes (sect);
707 } else if (current_attr_target == "assembly") {
708 CodeGen.Assembly.AddAttributes (sect, current_namespace);
712 attrs = new Attributes (sect);
714 attrs.AddAttributes (sect);
718 attrs = new Attributes (sect);
720 attrs.AddAttributes (sect);
726 current_attr_target = null;
731 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
735 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
741 attribute_target_specifier
742 : attribute_target COLON
744 current_attr_target = (string)$1;
752 LocatedToken lt = (LocatedToken) $1;
753 $$ = CheckAttributeTarget (lt.Value, lt.Location);
755 | EVENT { $$ = "event"; }
756 | RETURN { $$ = "return"; }
759 string name = GetTokenName (yyToken);
760 $$ = CheckAttributeTarget (name, GetLocation ($1));
767 ArrayList attrs = new ArrayList (4);
773 | attribute_list COMMA attribute
775 ArrayList attrs = (ArrayList) $1;
785 ++lexer.parsing_block;
787 opt_attribute_arguments
789 --lexer.parsing_block;
790 MemberName mname = (MemberName) $1;
791 if (mname.IsGeneric) {
792 Report.Error (404, lexer.Location,
793 "'<' unexpected: attributes cannot be generic");
796 Arguments [] arguments = (Arguments []) $3;
797 ATypeNameExpression expr = mname.GetTypeExpression ();
799 if (current_attr_target == String.Empty)
801 else if (global_attrs_enabled && (current_attr_target == "assembly" || current_attr_target == "module"))
802 // FIXME: supply "nameEscaped" parameter here.
803 $$ = new GlobalAttribute (current_namespace, current_attr_target,
804 expr, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
806 $$ = new Attribute (current_attr_target, expr, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
811 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
814 opt_attribute_arguments
815 : /* empty */ { $$ = null; }
816 | OPEN_PARENS attribute_arguments CLOSE_PARENS
824 : /* empty */ { $$ = null; }
825 | positional_or_named_argument
827 Arguments a = new Arguments (4);
828 a.Add ((Argument) $1);
829 $$ = new Arguments [] { a, null };
831 | named_attribute_argument
833 Arguments a = new Arguments (4);
834 a.Add ((Argument) $1);
835 $$ = new Arguments [] { null, a };
837 | attribute_arguments COMMA positional_or_named_argument
839 Arguments[] o = (Arguments[]) $1;
841 Report.Error (1016, ((Argument) $3).Expr.Location, "Named attribute arguments must appear after the positional arguments");
842 o [0] = new Arguments (4);
845 Arguments args = ((Arguments) o [0]);
846 if (args.Count > 0 && !($3 is NamedArgument) && args [args.Count - 1] is NamedArgument)
847 Error_NamedArgumentExpected ((NamedArgument) args [args.Count - 1]);
849 args.Add ((Argument) $3);
851 | attribute_arguments COMMA named_attribute_argument
853 Arguments[] o = (Arguments[]) $1;
855 o [1] = new Arguments (4);
858 ((Arguments) o [1]).Add ((Argument) $3);
862 positional_or_named_argument
865 $$ = new Argument ((Expression) $1);
870 named_attribute_argument
871 : IDENTIFIER ASSIGN expression
873 $$ = new NamedArgument ((LocatedToken) $1, (Expression) $3);
878 : IDENTIFIER COLON expression
880 if (RootContext.Version <= LanguageVersion.V_3)
881 Report.FeatureIsNotAvailable (GetLocation ($1), "named argument");
883 $$ = new NamedArgument ((LocatedToken) $1, (Expression) $3);
889 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
892 opt_class_member_declarations
894 | class_member_declarations
897 class_member_declarations
898 : class_member_declaration
899 | class_member_declarations
900 class_member_declaration
903 class_member_declaration
904 : constant_declaration // done
905 | field_declaration // done
906 | method_declaration // done
907 | property_declaration // done
908 | event_declaration // done
909 | indexer_declaration // done
910 | operator_declaration // done
911 | constructor_declaration // done
912 | destructor_declaration // done
916 Report.Error (1519, lexer.Location, "Unexpected symbol `{0}' in class, struct, or interface member declaration",
917 GetSymbolName (yyToken));
919 lexer.parsing_generic_declaration = false;
929 lexer.ConstraintsParsing = true;
931 type_declaration_name
933 MemberName name = MakeName ((MemberName) $6);
934 push_current_class (new Struct (current_namespace, current_class, name, (int) $2, (Attributes) $1), $3);
937 opt_type_parameter_constraints_clauses
939 lexer.ConstraintsParsing = false;
941 current_class.SetParameterInfo ((ArrayList) $9);
943 if (RootContext.Documentation != null)
944 current_container.DocComment = Lexer.consume_doc_comment ();
948 --lexer.parsing_declaration;
949 if (RootContext.Documentation != null)
950 Lexer.doc_state = XmlCommentState.Allowed;
954 $$ = pop_current_class ();
956 | opt_attributes opt_modifiers opt_partial STRUCT error {
957 CheckIdentifierToken (yyToken, GetLocation ($5));
964 if (RootContext.Documentation != null)
965 Lexer.doc_state = XmlCommentState.Allowed;
967 opt_struct_member_declarations CLOSE_BRACE
970 opt_struct_member_declarations
972 | struct_member_declarations
975 struct_member_declarations
976 : struct_member_declaration
977 | struct_member_declarations struct_member_declaration
980 struct_member_declaration
981 : constant_declaration
984 | property_declaration
986 | indexer_declaration
987 | operator_declaration
988 | constructor_declaration
992 * This is only included so we can flag error 575:
993 * destructors only allowed on class types
995 | destructor_declaration
1003 constant_declarators
1006 int modflags = (int) $2;
1007 foreach (VariableDeclaration constant in (ArrayList) $5){
1008 Location l = constant.Location;
1009 if ((modflags & Modifiers.STATIC) != 0) {
1010 Report.Error (504, l, "The constant `{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
1014 Const c = new Const (
1015 current_class, (FullNamedExpression) $4, (string) constant.identifier,
1016 (Expression) constant.expression_or_array_initializer, modflags,
1017 (Attributes) $1, l);
1019 if (RootContext.Documentation != null) {
1020 c.DocComment = Lexer.consume_doc_comment ();
1021 Lexer.doc_state = XmlCommentState.Allowed;
1023 current_container.AddConstant (c);
1028 constant_declarators
1029 : constant_declarator
1031 ArrayList constants = new ArrayList (4);
1036 | constant_declarators COMMA constant_declarator
1039 ArrayList constants = (ArrayList) $1;
1048 ++lexer.parsing_block;
1050 constant_initializer
1052 --lexer.parsing_block;
1053 $$ = new VariableDeclaration ((LocatedToken) $1, $4);
1057 // A const field requires a value to be provided
1058 Report.Error (145, ((LocatedToken) $1).Location, "A const field requires a value to be provided");
1063 constant_initializer
1064 : constant_expression
1072 variable_declarators
1075 FullNamedExpression type = (FullNamedExpression) $3;
1076 if (type == TypeManager.system_void_expr)
1077 Report.Error (670, GetLocation ($3), "Fields cannot have void type");
1081 current_array_type = null;
1083 foreach (VariableMemberDeclaration var in (ArrayList) $4){
1084 Field field = new Field (current_class, type, mod, var.MemberName, (Attributes) $1);
1086 field.Initializer = var.expression_or_array_initializer;
1088 if (RootContext.Documentation != null) {
1089 field.DocComment = Lexer.consume_doc_comment ();
1090 Lexer.doc_state = XmlCommentState.Allowed;
1092 current_container.AddField (field);
1093 $$ = field; // FIXME: might be better if it points to the top item
1100 fixed_variable_declarators
1103 FullNamedExpression type = (FullNamedExpression) $4;
1107 current_array_type = null;
1109 foreach (VariableDeclaration var in (ArrayList) $5) {
1110 FixedField field = new FixedField (current_class, type, mod, var.identifier,
1111 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
1113 if (RootContext.Documentation != null) {
1114 field.DocComment = Lexer.consume_doc_comment ();
1115 Lexer.doc_state = XmlCommentState.Allowed;
1117 current_container.AddField (field);
1118 $$ = field; // FIXME: might be better if it points to the top item
1127 Report.Error (1641, GetLocation ($4), "A fixed size buffer field must have the array size specifier after the field name");
1131 fixed_variable_declarators
1132 : fixed_variable_declarator
1134 ArrayList decl = new ArrayList (2);
1138 | fixed_variable_declarators COMMA fixed_variable_declarator
1140 ArrayList decls = (ArrayList) $1;
1146 fixed_variable_declarator
1147 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
1149 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1151 | IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1153 Report.Error (443, lexer.Location, "Value or constant expected");
1154 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1159 local_variable_declarators
1160 : local_variable_declarator
1162 ArrayList decl = new ArrayList (4);
1167 | local_variable_declarators COMMA local_variable_declarator
1169 ArrayList decls = (ArrayList) $1;
1175 local_variable_declarator
1176 : IDENTIFIER ASSIGN local_variable_initializer
1178 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1182 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1184 | IDENTIFIER variable_bad_array
1190 local_variable_initializer
1193 | STACKALLOC simple_type OPEN_BRACKET expression CLOSE_BRACKET
1195 $$ = new StackAlloc ((Expression) $2, (Expression) $4, (Location) $1);
1199 $$ = new ArglistAccess ((Location) $1);
1201 | STACKALLOC simple_type
1203 Report.Error (1575, (Location) $1, "A stackalloc expression requires [] after type");
1204 $$ = new StackAlloc ((Expression) $2, null, (Location) $1);
1208 variable_declarators
1209 : variable_declarator
1211 ArrayList decl = new ArrayList (4);
1216 | variable_declarators COMMA variable_declarator
1218 ArrayList decls = (ArrayList) $1;
1225 : member_declaration_name ASSIGN
1227 ++lexer.parsing_block;
1228 lexer.parsing_generic_declaration = false;
1230 variable_initializer
1232 --lexer.parsing_block;
1233 $$ = new VariableMemberDeclaration ((MemberName) $1, $4);
1235 | member_declaration_name
1237 lexer.parsing_generic_declaration = false;
1238 $$ = new VariableMemberDeclaration ((MemberName) $1, null);
1240 | member_declaration_name variable_bad_array
1242 lexer.parsing_generic_declaration = false;
1248 : OPEN_BRACKET opt_expression CLOSE_BRACKET
1250 Report.Error (650, GetLocation ($1), "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
1251 "To declare a fixed size buffer field, use the fixed keyword before the field type");
1255 variable_initializer
1262 if (RootContext.Documentation != null)
1263 Lexer.doc_state = XmlCommentState.NotAllowed;
1267 Method method = (Method) $1;
1268 method.Block = (ToplevelBlock) $3;
1269 current_container.AddMethod (method);
1271 if (current_container.Kind == Kind.Interface && method.Block != null) {
1272 Report.Error (531, method.Location, "`{0}': interface members cannot have a definition", method.GetSignatureForError ());
1275 current_generic_method = null;
1276 current_local_parameters = null;
1278 if (RootContext.Documentation != null)
1279 Lexer.doc_state = XmlCommentState.Allowed;
1287 method_declaration_name OPEN_PARENS
1289 valid_param_mod = ParameterModifierType.All;
1291 opt_formal_parameter_list CLOSE_PARENS
1293 lexer.ConstraintsParsing = true;
1295 opt_type_parameter_constraints_clauses
1297 lexer.ConstraintsParsing = false;
1298 valid_param_mod = 0;
1299 MemberName name = (MemberName) $4;
1300 current_local_parameters = (ParametersCompiled) $7;
1302 if ($10 != null && name.TypeArguments == null)
1303 Report.Error (80, lexer.Location,
1304 "Constraints are not allowed on non-generic declarations");
1308 GenericMethod generic = null;
1309 if (name.TypeArguments != null) {
1310 generic = new GenericMethod (current_namespace, current_class, name,
1311 (FullNamedExpression) $3, current_local_parameters);
1313 generic.SetParameterInfo ((ArrayList) $10);
1316 method = new Method (current_class, generic, (FullNamedExpression) $3, (int) $2,
1317 name, current_local_parameters, (Attributes) $1);
1319 current_generic_method = generic;
1321 if (RootContext.Documentation != null)
1322 method.DocComment = Lexer.consume_doc_comment ();
1329 VOID method_declaration_name
1332 valid_param_mod = ParameterModifierType.All;
1334 opt_formal_parameter_list CLOSE_PARENS
1336 lexer.ConstraintsParsing = true;
1338 opt_type_parameter_constraints_clauses
1340 lexer.ConstraintsParsing = false;
1341 valid_param_mod = 0;
1343 MemberName name = (MemberName) $5;
1344 current_local_parameters = (ParametersCompiled) $8;
1346 if ($10 != null && name.TypeArguments == null)
1347 Report.Error (80, lexer.Location,
1348 "Constraints are not allowed on non-generic declarations");
1351 GenericMethod generic = null;
1352 if (name.TypeArguments != null) {
1353 generic = new GenericMethod (current_namespace, current_class, name,
1354 TypeManager.system_void_expr, current_local_parameters);
1356 generic.SetParameterInfo ((ArrayList) $11);
1359 int modifiers = (int) $2;
1362 const int invalid_partial_mod = Modifiers.Accessibility | Modifiers.ABSTRACT | Modifiers.EXTERN |
1363 Modifiers.NEW | Modifiers.OVERRIDE | Modifiers.SEALED | Modifiers.VIRTUAL;
1365 if ((modifiers & invalid_partial_mod) != 0) {
1366 Report.Error (750, name.Location, "A partial method cannot define access modifier or " +
1367 "any of abstract, extern, new, override, sealed, or virtual modifiers");
1368 modifiers &= ~invalid_partial_mod;
1371 if ((current_class.ModFlags & Modifiers.PARTIAL) == 0) {
1372 Report.Error (751, name.Location, "A partial method must be declared within a " +
1373 "partial class or partial struct");
1376 modifiers |= Modifiers.PARTIAL | Modifiers.PRIVATE;
1378 method = new Method (current_class, generic, TypeManager.system_void_expr,
1379 modifiers, name, current_local_parameters, (Attributes) $1);
1381 current_generic_method = generic;
1383 if (RootContext.Documentation != null)
1384 method.DocComment = Lexer.consume_doc_comment ();
1391 modifiers method_declaration_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1393 MemberName name = (MemberName) $5;
1394 Report.Error (1585, name.Location,
1395 "Member modifier `{0}' must precede the member type and name", Modifiers.Name ((int) $4));
1397 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1398 0, name, (ParametersCompiled) $7, (Attributes) $1);
1400 current_local_parameters = (ParametersCompiled) $7;
1402 if (RootContext.Documentation != null)
1403 method.DocComment = Lexer.consume_doc_comment ();
1411 | SEMICOLON { $$ = null; }
1414 opt_formal_parameter_list
1415 : /* empty */ { $$ = ParametersCompiled.EmptyReadOnlyParameters; }
1416 | formal_parameter_list
1419 formal_parameter_list
1422 ArrayList pars_list = (ArrayList) $1;
1424 Parameter [] pars = new Parameter [pars_list.Count];
1425 pars_list.CopyTo (pars);
1427 $$ = new ParametersCompiled (pars);
1429 | fixed_parameters COMMA parameter_array
1431 ArrayList pars_list = (ArrayList) $1;
1434 Parameter [] pars = new Parameter [pars_list.Count];
1435 pars_list.CopyTo (pars);
1437 $$ = new ParametersCompiled (pars);
1439 | fixed_parameters COMMA arglist_modifier
1441 ArrayList pars_list = (ArrayList) $1;
1442 pars_list.Add (new ArglistParameter (GetLocation ($3)));
1444 Parameter [] pars = new Parameter [pars_list.Count];
1445 pars_list.CopyTo (pars);
1447 $$ = new ParametersCompiled (pars, true);
1449 | parameter_array COMMA error
1452 Report.Error (231, ((Parameter) $1).Location, "A params parameter must be the last parameter in a formal parameter list");
1454 $$ = new ParametersCompiled (new Parameter[] { (Parameter) $1 } );
1456 | fixed_parameters COMMA parameter_array COMMA error
1459 Report.Error (231, ((Parameter) $3).Location, "A params parameter must be the last parameter in a formal parameter list");
1461 ArrayList pars_list = (ArrayList) $1;
1462 pars_list.Add (new ArglistParameter (GetLocation ($3)));
1464 Parameter [] pars = new Parameter [pars_list.Count];
1465 pars_list.CopyTo (pars);
1467 $$ = new ParametersCompiled (pars, true);
1469 | arglist_modifier COMMA error
1471 Report.Error (257, (Location) $1, "An __arglist parameter must be the last parameter in a formal parameter list");
1473 $$ = new ParametersCompiled (new Parameter [] { new ArglistParameter ((Location) $1) }, true);
1475 | fixed_parameters COMMA ARGLIST COMMA error
1477 Report.Error (257, (Location) $3, "An __arglist parameter must be the last parameter in a formal parameter list");
1479 ArrayList pars_list = (ArrayList) $1;
1480 pars_list.Add (new ArglistParameter (GetLocation ($3)));
1482 Parameter [] pars = new Parameter [pars_list.Count];
1483 pars_list.CopyTo (pars);
1485 $$ = new ParametersCompiled (pars, true);
1489 $$ = new ParametersCompiled (new Parameter[] { (Parameter) $1 } );
1493 $$ = new ParametersCompiled (new Parameter [] { new ArglistParameter ((Location) $1) }, true);
1500 ArrayList pars = new ArrayList (4);
1501 Parameter p = (Parameter) $1;
1504 default_parameter_used = p.HasDefaultValue;
1507 | fixed_parameters COMMA fixed_parameter
1509 ArrayList pars = (ArrayList) $1;
1510 Parameter p = (Parameter) $3;
1512 if (p.HasExtensionMethodModifier)
1513 Report.Error (1100, p.Location, "The parameter modifier `this' can only be used on the first parameter");
1514 else if (!p.HasDefaultValue && default_parameter_used)
1515 Report.Error (1737, p.Location, "Optional parameter cannot precede required parameters");
1517 default_parameter_used |= p.HasDefaultValue;
1526 opt_parameter_modifier
1530 LocatedToken lt = (LocatedToken) $4;
1531 $$ = new Parameter ((FullNamedExpression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1534 opt_parameter_modifier
1536 IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1538 LocatedToken lt = (LocatedToken) $4;
1539 Report.Error (1552, lt.Location, "Array type specifier, [], must appear before parameter name");
1540 $$ = new Parameter ((FullNamedExpression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1543 opt_parameter_modifier
1547 Location l = GetLocation ($4);
1548 CheckIdentifierToken (yyToken, l);
1549 $$ = new Parameter ((FullNamedExpression) $3, "NeedSomeGeneratorHere", (Parameter.Modifier) $2, (Attributes) $1, l);
1552 opt_parameter_modifier
1558 if (RootContext.Version <= LanguageVersion.V_3) {
1559 Report.FeatureIsNotAvailable (GetLocation ($5), "optional parameter");
1562 Parameter.Modifier mod = (Parameter.Modifier) $2;
1563 if (mod != Parameter.Modifier.NONE) {
1565 case Parameter.Modifier.REF:
1566 case Parameter.Modifier.OUT:
1567 Report.Error (1741, GetLocation ($2), "Cannot specify a default value for the `{0}' parameter",
1568 Parameter.GetModifierSignature (mod));
1571 case Parameter.Modifier.This:
1572 Report.Error (1743, GetLocation ($2), "Cannot specify a default value for the `{0}' parameter",
1573 Parameter.GetModifierSignature (mod));
1576 throw new NotImplementedException (mod.ToString ());
1579 mod = Parameter.Modifier.NONE;
1582 if ((valid_param_mod & ParameterModifierType.DefaultValue) == 0)
1583 Report.Error (1065, GetLocation ($6), "Optional parameter is not valid in this context");
1585 LocatedToken lt = (LocatedToken) $4;
1586 $$ = new Parameter ((FullNamedExpression) $3, lt.Value, mod, (Attributes) $1, lt.Location);
1588 ((Parameter) $$).DefaultValue = (Expression) $6;
1592 opt_parameter_modifier
1593 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1594 | parameter_modifiers
1598 : parameter_modifier
1602 | parameter_modifiers parameter_modifier
1604 Parameter.Modifier p2 = (Parameter.Modifier)$2;
1605 Parameter.Modifier mod = (Parameter.Modifier)$1 | p2;
1606 if (((Parameter.Modifier)$1 & p2) == p2) {
1607 Error_DuplicateParameterModifier (lexer.Location, p2);
1609 switch (mod & ~Parameter.Modifier.This) {
1610 case Parameter.Modifier.REF:
1611 Report.Error (1101, lexer.Location, "The parameter modifiers `this' and `ref' cannot be used altogether");
1613 case Parameter.Modifier.OUT:
1614 Report.Error (1102, lexer.Location, "The parameter modifiers `this' and `out' cannot be used altogether");
1617 Report.Error (1108, lexer.Location, "A parameter cannot have specified more than one modifier");
1628 if ((valid_param_mod & ParameterModifierType.Ref) == 0)
1629 Error_ParameterModifierNotValid ("ref", (Location)$1);
1631 $$ = Parameter.Modifier.REF;
1635 if ((valid_param_mod & ParameterModifierType.Out) == 0)
1636 Error_ParameterModifierNotValid ("out", (Location)$1);
1638 $$ = Parameter.Modifier.OUT;
1642 if ((valid_param_mod & ParameterModifierType.This) == 0)
1643 Error_ParameterModifierNotValid ("this", (Location)$1);
1645 if (RootContext.Version <= LanguageVersion.ISO_2)
1646 Report.FeatureIsNotAvailable (GetLocation ($1), "extension methods");
1648 $$ = Parameter.Modifier.This;
1653 : opt_attributes params_modifier type IDENTIFIER
1655 LocatedToken lt = (LocatedToken) $4;
1656 $$ = new ParamsParameter ((FullNamedExpression) $3, lt.Value, (Attributes) $1, lt.Location);
1658 | opt_attributes params_modifier type IDENTIFIER ASSIGN constant_expression
1660 Report.Error (1751, GetLocation ($2), "Cannot specify a default value for a parameter array");
1662 LocatedToken lt = (LocatedToken) $4;
1663 $$ = new ParamsParameter ((FullNamedExpression) $3, lt.Value, (Attributes) $1, lt.Location);
1665 | opt_attributes params_modifier type error {
1666 CheckIdentifierToken (yyToken, GetLocation ($4));
1674 if ((valid_param_mod & ParameterModifierType.Params) == 0)
1675 Report.Error (1670, ((Location) $1), "The `params' modifier is not allowed in current context");
1677 | PARAMS parameter_modifier
1679 Parameter.Modifier mod = (Parameter.Modifier)$2;
1680 if ((mod & Parameter.Modifier.This) != 0) {
1681 Report.Error (1104, (Location)$1, "The parameter modifiers `this' and `params' cannot be used altogether");
1683 Report.Error (1611, (Location)$1, "The params parameter cannot be declared as ref or out");
1686 | PARAMS params_modifier
1688 Error_DuplicateParameterModifier ((Location)$1, Parameter.Modifier.PARAMS);
1695 if ((valid_param_mod & ParameterModifierType.Arglist) == 0)
1696 Report.Error (1669, (Location) $1, "__arglist is not valid in this context");
1700 property_declaration
1704 member_declaration_name
1706 if (RootContext.Documentation != null)
1707 tmpComment = Lexer.consume_doc_comment ();
1711 implicit_value_parameter_type = (FullNamedExpression) $3;
1712 lexer.PropertyParsing = true;
1714 accessor_declarations
1716 lexer.PropertyParsing = false;
1717 has_get = has_set = false;
1722 Accessors accessors = (Accessors) $8;
1723 Accessor get_block = accessors != null ? accessors.get_or_add : null;
1724 Accessor set_block = accessors != null ? accessors.set_or_remove : null;
1725 bool order = accessors != null ? accessors.declared_in_reverse : false;
1727 MemberName name = (MemberName) $4;
1728 FullNamedExpression ptype = (FullNamedExpression) $3;
1730 prop = new Property (current_class, ptype, (int) $2,
1731 name, (Attributes) $1, get_block, set_block, order, current_block);
1733 if (ptype == TypeManager.system_void_expr)
1734 Report.Error (547, name.Location, "`{0}': property or indexer cannot have void type", prop.GetSignatureForError ());
1736 if (accessors == null)
1737 Report.Error (548, prop.Location, "`{0}': property or indexer must have at least one accessor", prop.GetSignatureForError ());
1739 if (current_container.Kind == Kind.Interface) {
1740 if (prop.Get.Block != null)
1741 Report.Error (531, prop.Location, "`{0}.get': interface members cannot have a definition", prop.GetSignatureForError ());
1743 if (prop.Set.Block != null)
1744 Report.Error (531, prop.Location, "`{0}.set': interface members cannot have a definition", prop.GetSignatureForError ());
1747 current_container.AddProperty (prop);
1748 implicit_value_parameter_type = null;
1750 if (RootContext.Documentation != null)
1751 prop.DocComment = ConsumeStoredComment ();
1756 accessor_declarations
1757 : get_accessor_declaration
1759 $$ = new Accessors ((Accessor) $1, null);
1761 | get_accessor_declaration accessor_declarations
1763 Accessors accessors = (Accessors) $2;
1764 accessors.get_or_add = (Accessor) $1;
1767 | set_accessor_declaration
1769 $$ = new Accessors (null, (Accessor) $1);
1771 | set_accessor_declaration accessor_declarations
1773 Accessors accessors = (Accessors) $2;
1774 accessors.set_or_remove = (Accessor) $1;
1775 accessors.declared_in_reverse = true;
1780 if (yyToken == Token.CLOSE_BRACE) {
1783 if (yyToken == Token.SEMICOLON)
1784 Report.Error (1597, lexer.Location, "Semicolon after method or accessor block is not valid");
1786 Report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1788 $$ = new Accessors (null, null);
1793 get_accessor_declaration
1794 : opt_attributes opt_modifiers GET
1796 // If this is not the case, then current_local_parameters has already
1797 // been set in indexer_declaration
1798 if (parsing_indexer == false)
1799 current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters;
1801 current_local_parameters = indexer_parameters;
1802 lexer.PropertyParsing = false;
1807 Report.Error (1007, GetLocation ($3), "Property accessor already defined");
1810 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, current_local_parameters, (Location) $3);
1812 current_local_parameters = null;
1813 lexer.PropertyParsing = true;
1815 if (RootContext.Documentation != null)
1816 if (Lexer.doc_state == XmlCommentState.Error)
1817 Lexer.doc_state = XmlCommentState.NotAllowed;
1823 set_accessor_declaration
1824 : opt_attributes opt_modifiers SET
1826 Parameter implicit_value_parameter = new Parameter (
1827 implicit_value_parameter_type, "value",
1828 Parameter.Modifier.NONE, null, (Location) $3);
1830 if (!parsing_indexer) {
1831 current_local_parameters = new ParametersCompiled (new Parameter [] { implicit_value_parameter });
1833 current_local_parameters = ParametersCompiled.MergeGenerated (
1834 indexer_parameters, true, implicit_value_parameter, null);
1837 lexer.PropertyParsing = false;
1842 Report.Error (1007, GetLocation ($3), "Property accessor already defined");
1845 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, current_local_parameters, (Location) $3);
1847 current_local_parameters = null;
1848 lexer.PropertyParsing = true;
1850 if (RootContext.Documentation != null
1851 && Lexer.doc_state == XmlCommentState.Error)
1852 Lexer.doc_state = XmlCommentState.NotAllowed;
1866 Error_SyntaxError (1043, yyToken, "Invalid accessor body");
1871 interface_declaration
1877 lexer.ConstraintsParsing = true;
1879 type_declaration_name
1881 MemberName name = MakeName ((MemberName) $6);
1882 push_current_class (new Interface (current_namespace, current_class, name, (int) $2, (Attributes) $1), $3);
1885 opt_type_parameter_constraints_clauses
1887 lexer.ConstraintsParsing = false;
1889 current_class.SetParameterInfo ((ArrayList) $9);
1891 if (RootContext.Documentation != null) {
1892 current_container.DocComment = Lexer.consume_doc_comment ();
1893 Lexer.doc_state = XmlCommentState.Allowed;
1898 --lexer.parsing_declaration;
1899 if (RootContext.Documentation != null)
1900 Lexer.doc_state = XmlCommentState.Allowed;
1904 $$ = pop_current_class ();
1906 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1907 CheckIdentifierToken (yyToken, GetLocation ($5));
1913 opt_interface_member_declarations
1917 opt_interface_member_declarations
1919 | interface_member_declarations
1922 interface_member_declarations
1923 : interface_member_declaration
1924 | interface_member_declarations interface_member_declaration
1927 interface_member_declaration
1928 : constant_declaration
1930 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1934 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1936 | method_declaration
1937 | property_declaration
1939 | indexer_declaration
1940 | operator_declaration
1942 Report.Error (567, GetLocation ($1), "Interfaces cannot contain operators");
1944 | constructor_declaration
1946 Report.Error (526, GetLocation ($1), "Interfaces cannot contain contructors");
1950 Report.Error (524, GetLocation ($1), "Interfaces cannot declare classes, structs, interfaces, delegates, or enumerations");
1954 operator_declaration
1955 : opt_attributes opt_modifiers operator_declarator
1963 OperatorDeclaration decl = (OperatorDeclaration) $3;
1964 Operator op = new Operator (
1965 current_class, decl.optype, decl.ret_type, (int) $2,
1966 current_local_parameters,
1967 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1969 if (RootContext.Documentation != null) {
1970 op.DocComment = tmpComment;
1971 Lexer.doc_state = XmlCommentState.Allowed;
1974 // Note again, checking is done in semantic analysis
1975 current_container.AddOperator (op);
1977 current_local_parameters = null;
1983 | SEMICOLON { $$ = null; }
1987 : type_expression_or_array
1990 Report.Error (590, lexer.Location, "User-defined operators cannot return void");
1991 $$ = TypeManager.system_void_expr;
1996 : operator_type OPERATOR overloadable_operator OPEN_PARENS
1998 valid_param_mod = ParameterModifierType.DefaultValue;
2000 opt_formal_parameter_list CLOSE_PARENS
2002 valid_param_mod = 0;
2004 Location loc = (Location) $2;
2005 Operator.OpType op = (Operator.OpType) $3;
2006 current_local_parameters = (ParametersCompiled)$6;
2008 int p_count = current_local_parameters.Count;
2010 if (op == Operator.OpType.Addition)
2011 op = Operator.OpType.UnaryPlus;
2012 else if (op == Operator.OpType.Subtraction)
2013 op = Operator.OpType.UnaryNegation;
2016 if (IsUnaryOperator (op)) {
2018 Report.Error (1020, loc, "Overloadable binary operator expected");
2019 } else if (p_count != 1) {
2020 Report.Error (1535, loc, "Overloaded unary operator `{0}' takes one parameter",
2021 Operator.GetName (op));
2025 Report.Error (1534, loc, "Overloaded binary operator `{0}' takes two parameters",
2026 Operator.GetName (op));
2027 } else if (p_count != 2) {
2028 Report.Error (1019, loc, "Overloadable unary operator expected");
2032 if (RootContext.Documentation != null) {
2033 tmpComment = Lexer.consume_doc_comment ();
2034 Lexer.doc_state = XmlCommentState.NotAllowed;
2037 $$ = new OperatorDeclaration (op, (FullNamedExpression) $1, loc);
2039 | conversion_operator_declarator
2042 overloadable_operator
2044 : BANG { $$ = Operator.OpType.LogicalNot; }
2045 | TILDE { $$ = Operator.OpType.OnesComplement; }
2046 | OP_INC { $$ = Operator.OpType.Increment; }
2047 | OP_DEC { $$ = Operator.OpType.Decrement; }
2048 | TRUE { $$ = Operator.OpType.True; }
2049 | FALSE { $$ = Operator.OpType.False; }
2050 // Unary and binary:
2051 | PLUS { $$ = Operator.OpType.Addition; }
2052 | MINUS { $$ = Operator.OpType.Subtraction; }
2054 | STAR { $$ = Operator.OpType.Multiply; }
2055 | DIV { $$ = Operator.OpType.Division; }
2056 | PERCENT { $$ = Operator.OpType.Modulus; }
2057 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
2058 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
2059 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
2060 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
2061 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
2062 | OP_EQ { $$ = Operator.OpType.Equality; }
2063 | OP_NE { $$ = Operator.OpType.Inequality; }
2064 | OP_GT { $$ = Operator.OpType.GreaterThan; }
2065 | OP_LT { $$ = Operator.OpType.LessThan; }
2066 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
2067 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
2070 conversion_operator_declarator
2071 : IMPLICIT OPERATOR type OPEN_PARENS
2073 valid_param_mod = ParameterModifierType.DefaultValue;
2075 opt_formal_parameter_list CLOSE_PARENS
2077 valid_param_mod = 0;
2079 Location loc = (Location) $2;
2080 current_local_parameters = (ParametersCompiled)$6;
2082 if (RootContext.Documentation != null) {
2083 tmpComment = Lexer.consume_doc_comment ();
2084 Lexer.doc_state = XmlCommentState.NotAllowed;
2087 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (FullNamedExpression) $3, loc);
2089 | EXPLICIT OPERATOR type OPEN_PARENS
2091 valid_param_mod = ParameterModifierType.DefaultValue;
2093 opt_formal_parameter_list CLOSE_PARENS
2095 valid_param_mod = 0;
2097 Location loc = (Location) $2;
2098 current_local_parameters = (ParametersCompiled)$6;
2100 if (RootContext.Documentation != null) {
2101 tmpComment = Lexer.consume_doc_comment ();
2102 Lexer.doc_state = XmlCommentState.NotAllowed;
2105 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (FullNamedExpression) $3, loc);
2109 Error_SyntaxError (yyToken);
2110 current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters;
2111 $$ = new OperatorDeclaration (Operator.OpType.Implicit, null, GetLocation ($1));
2115 Error_SyntaxError (yyToken);
2116 current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters;
2117 $$ = new OperatorDeclaration (Operator.OpType.Explicit, null, GetLocation ($1));
2121 constructor_declaration
2122 : constructor_declarator
2125 Constructor c = (Constructor) $1;
2126 c.Block = (ToplevelBlock) $2;
2128 if (RootContext.Documentation != null)
2129 c.DocComment = ConsumeStoredComment ();
2131 current_container.AddConstructor (c);
2133 current_local_parameters = null;
2134 if (RootContext.Documentation != null)
2135 Lexer.doc_state = XmlCommentState.Allowed;
2139 constructor_declarator
2144 if (RootContext.Documentation != null) {
2145 tmpComment = Lexer.consume_doc_comment ();
2146 Lexer.doc_state = XmlCommentState.Allowed;
2149 valid_param_mod = ParameterModifierType.All;
2151 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2153 valid_param_mod = 0;
2154 current_local_parameters = (ParametersCompiled) $6;
2157 // start block here, so possible anonymous methods inside
2158 // constructor initializer can get correct parent block
2160 start_block (lexer.Location);
2162 opt_constructor_initializer
2164 LocatedToken lt = (LocatedToken) $3;
2165 int mods = (int) $2;
2166 ConstructorInitializer ci = (ConstructorInitializer) $9;
2168 Constructor c = new Constructor (current_class, lt.Value, mods,
2169 (Attributes) $1, current_local_parameters, ci, lt.Location);
2171 if (lt.Value != current_container.MemberName.Name) {
2172 Report.Error (1520, c.Location, "Class, struct, or interface method must have a return type");
2173 } else if ((mods & Modifiers.STATIC) != 0) {
2174 if ((mods & Modifiers.Accessibility) != 0){
2175 Report.Error (515, c.Location,
2176 "`{0}': static constructor cannot have an access modifier",
2177 c.GetSignatureForError ());
2180 Report.Error (514, c.Location,
2181 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2182 c.GetSignatureForError ());
2193 | SEMICOLON { current_block = null; $$ = null; }
2196 opt_constructor_initializer
2198 | constructor_initializer
2201 constructor_initializer
2202 : COLON BASE OPEN_PARENS
2204 ++lexer.parsing_block;
2206 opt_argument_list CLOSE_PARENS
2208 --lexer.parsing_block;
2209 $$ = new ConstructorBaseInitializer ((Arguments) $5, (Location) $2);
2211 | COLON THIS OPEN_PARENS
2213 ++lexer.parsing_block;
2215 opt_argument_list CLOSE_PARENS
2217 --lexer.parsing_block;
2218 $$ = new ConstructorThisInitializer ((Arguments) $5, (Location) $2);
2221 Report.Error (1018, GetLocation ($1), "Keyword `this' or `base' expected");
2226 destructor_declaration
2227 : opt_attributes opt_modifiers TILDE
2229 if (RootContext.Documentation != null) {
2230 tmpComment = Lexer.consume_doc_comment ();
2231 Lexer.doc_state = XmlCommentState.NotAllowed;
2234 current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters;
2236 IDENTIFIER OPEN_PARENS CLOSE_PARENS method_body
2238 LocatedToken lt = (LocatedToken) $5;
2239 if (lt.Value != current_container.MemberName.Name){
2240 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2241 } else if (current_container.Kind != Kind.Class){
2242 Report.Error (575, lt.Location, "Only class types can contain destructor");
2244 Destructor d = new Destructor (current_class, (int) $2,
2245 ParametersCompiled.EmptyReadOnlyParameters, (Attributes) $1, lt.Location);
2246 if (RootContext.Documentation != null)
2247 d.DocComment = ConsumeStoredComment ();
2249 d.Block = (ToplevelBlock) $8;
2250 current_container.AddMethod (d);
2253 current_local_parameters = null;
2260 EVENT type variable_declarators SEMICOLON
2262 current_array_type = null;
2263 foreach (VariableMemberDeclaration var in (ArrayList) $5) {
2265 EventField e = new EventField (
2266 current_class, (FullNamedExpression) $4, (int) $2, var.MemberName, (Attributes) $1);
2268 if (var.expression_or_array_initializer != null) {
2269 if (current_container.Kind == Kind.Interface) {
2270 Report.Error (68, e.Location, "`{0}': event in interface cannot have initializer", e.GetSignatureForError ());
2273 e.Initializer = var.expression_or_array_initializer;
2276 if (var.MemberName.Left != null) {
2277 Report.Error (71, e.Location,
2278 "`{0}': An explicit interface implementation of an event must use property syntax",
2279 e.GetSignatureForError ());
2282 current_container.AddEvent (e);
2284 if (RootContext.Documentation != null) {
2285 e.DocComment = Lexer.consume_doc_comment ();
2286 Lexer.doc_state = XmlCommentState.Allowed;
2292 EVENT type member_declaration_name
2295 implicit_value_parameter_type = (FullNamedExpression) $4;
2296 current_local_parameters = new ParametersCompiled (
2297 new Parameter (implicit_value_parameter_type, "value",
2298 Parameter.Modifier.NONE, null, GetLocation ($3)));
2300 lexer.EventParsing = true;
2302 event_accessor_declarations
2304 lexer.EventParsing = false;
2308 MemberName name = (MemberName) $5;
2310 if (current_container.Kind == Kind.Interface) {
2311 Report.Error (69, (Location) $3, "Event in interface cannot have add or remove accessors");
2312 $8 = new Accessors (null, null);
2313 } else if ($8 == null) {
2314 Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2315 current_container.GetSignatureForError (), name.GetSignatureForError ());
2316 $8 = new Accessors (null, null);
2319 Accessors accessors = (Accessors) $8;
2321 if (accessors.get_or_add == null || accessors.set_or_remove == null)
2322 // CS0073 is already reported, so no CS0065 here.
2325 Event e = new EventProperty (
2326 current_class, (FullNamedExpression) $4, (int) $2, name,
2327 (Attributes) $1, accessors.get_or_add, accessors.set_or_remove);
2328 if (RootContext.Documentation != null) {
2329 e.DocComment = Lexer.consume_doc_comment ();
2330 Lexer.doc_state = XmlCommentState.Allowed;
2333 current_container.AddEvent (e);
2334 implicit_value_parameter_type = null;
2336 current_local_parameters = null;
2338 | opt_attributes opt_modifiers EVENT type member_declaration_name error
2340 MemberName mn = (MemberName) $5;
2341 if (mn.Left != null)
2342 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2344 if (RootContext.Documentation != null)
2345 Lexer.doc_state = XmlCommentState.Allowed;
2347 Error_SyntaxError (yyToken);
2352 event_accessor_declarations
2353 : add_accessor_declaration remove_accessor_declaration
2355 $$ = new Accessors ((Accessor) $1, (Accessor) $2);
2357 | remove_accessor_declaration add_accessor_declaration
2359 Accessors accessors = new Accessors ((Accessor) $2, (Accessor) $1);
2360 accessors.declared_in_reverse = true;
2363 | add_accessor_declaration { $$ = null; }
2364 | remove_accessor_declaration { $$ = null; }
2367 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2373 add_accessor_declaration
2374 : opt_attributes ADD
2376 lexer.EventParsing = false;
2380 Accessor accessor = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, null, (Location) $2);
2381 lexer.EventParsing = true;
2384 | opt_attributes ADD error {
2385 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2388 | opt_attributes modifiers ADD {
2389 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2394 remove_accessor_declaration
2395 : opt_attributes REMOVE
2397 lexer.EventParsing = false;
2401 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, null, (Location) $2);
2402 lexer.EventParsing = true;
2404 | opt_attributes REMOVE error {
2405 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2408 | opt_attributes modifiers REMOVE {
2409 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2415 : opt_attributes opt_modifiers
2416 member_type indexer_declaration_name OPEN_BRACKET
2418 valid_param_mod = ParameterModifierType.Params | ParameterModifierType.DefaultValue;
2420 opt_formal_parameter_list CLOSE_BRACKET
2423 valid_param_mod = 0;
2424 implicit_value_parameter_type = (FullNamedExpression) $3;
2425 indexer_parameters = (ParametersCompiled) $7;
2427 if (indexer_parameters.IsEmpty) {
2428 Report.Error (1551, GetLocation ($5), "Indexers must have at least one parameter");
2431 if (RootContext.Documentation != null) {
2432 tmpComment = Lexer.consume_doc_comment ();
2433 Lexer.doc_state = XmlCommentState.Allowed;
2436 lexer.PropertyParsing = true;
2437 parsing_indexer = true;
2440 accessor_declarations
2442 lexer.PropertyParsing = false;
2443 has_get = has_set = false;
2444 parsing_indexer = false;
2448 Accessors accessors = (Accessors) $11;
2449 Accessor get_block = accessors != null ? accessors.get_or_add : null;
2450 Accessor set_block = accessors != null ? accessors.set_or_remove : null;
2451 bool order = accessors != null ? accessors.declared_in_reverse : false;
2453 Indexer indexer = new Indexer (current_class, (FullNamedExpression) $3,
2454 (MemberName)$4, (int) $2, (ParametersCompiled) $7, (Attributes) $1,
2455 get_block, set_block, order);
2457 if ($3 == TypeManager.system_void_expr)
2458 Report.Error (620, GetLocation ($3), "`{0}': indexer return type cannot be `void'", indexer.GetSignatureForError ());
2460 if (accessors == null)
2461 Report.Error (548, indexer.Location, "`{0}': property or indexer must have at least one accessor", indexer.GetSignatureForError ());
2463 if (current_container.Kind == Kind.Interface) {
2464 if (indexer.Get.Block != null)
2465 Report.Error (531, indexer.Location, "`{0}.get': interface members cannot have a definition", indexer.GetSignatureForError ());
2467 if (indexer.Set.Block != null)
2468 Report.Error (531, indexer.Location, "`{0}.set': interface members cannot have a definition", indexer.GetSignatureForError ());
2471 if (RootContext.Documentation != null)
2472 indexer.DocComment = ConsumeStoredComment ();
2474 current_container.AddIndexer (indexer);
2476 current_local_parameters = null;
2477 implicit_value_parameter_type = null;
2478 indexer_parameters = null;
2485 ENUM type_declaration_name
2487 if (RootContext.Documentation != null)
2488 enumTypeComment = Lexer.consume_doc_comment ();
2493 MemberName name = (MemberName) $4;
2494 if (name.IsGeneric) {
2495 Report.Error (1675, name.Location, "Enums cannot have type parameters");
2498 name = MakeName (name);
2499 Enum e = new Enum (current_namespace, current_class, (TypeExpr) $5, (int) $2,
2500 name, (Attributes) $1);
2502 if (RootContext.Documentation != null)
2503 e.DocComment = enumTypeComment;
2506 EnumMember em = null;
2507 foreach (VariableDeclaration ev in (ArrayList) $7) {
2508 em = new EnumMember (
2509 e, em, ev.identifier, (Expression) ev.expression_or_array_initializer,
2510 ev.OptAttributes, ev.Location);
2512 // if (RootContext.Documentation != null)
2513 em.DocComment = ev.DocComment;
2515 e.AddEnumMember (em);
2517 if (RootContext.EvalMode)
2518 undo.AddTypeContainer (current_container, e);
2520 current_container.AddTypeContainer (e);
2530 $$ = TypeManager.system_int32_expr;
2534 if ($2 != TypeManager.system_int32_expr && $2 != TypeManager.system_uint32_expr &&
2535 $2 != TypeManager.system_int64_expr && $2 != TypeManager.system_uint64_expr &&
2536 $2 != TypeManager.system_int16_expr && $2 != TypeManager.system_uint16_expr &&
2537 $2 != TypeManager.system_byte_expr && $2 != TypeManager.system_sbyte_expr) {
2538 Enum.Error_1008 (GetLocation ($2), Report);
2539 $2 = TypeManager.system_int32_expr;
2546 Error_TypeExpected (lexer.Location);
2547 $$ = TypeManager.system_int32_expr;
2554 if (RootContext.Documentation != null)
2555 Lexer.doc_state = XmlCommentState.Allowed;
2557 opt_enum_member_declarations
2559 // here will be evaluated after CLOSE_BLACE is consumed.
2560 if (RootContext.Documentation != null)
2561 Lexer.doc_state = XmlCommentState.Allowed;
2569 opt_enum_member_declarations
2570 : /* empty */ { $$ = new ArrayList (0); }
2571 | enum_member_declarations opt_comma { $$ = $1; }
2574 enum_member_declarations
2575 : enum_member_declaration
2577 ArrayList l = new ArrayList (4);
2582 | enum_member_declarations COMMA enum_member_declaration
2584 ArrayList l = (ArrayList) $1;
2592 enum_member_declaration
2593 : opt_attributes IDENTIFIER
2595 VariableDeclaration vd = new VariableDeclaration (
2596 (LocatedToken) $2, null, (Attributes) $1);
2598 if (RootContext.Documentation != null) {
2599 vd.DocComment = Lexer.consume_doc_comment ();
2600 Lexer.doc_state = XmlCommentState.Allowed;
2605 | opt_attributes IDENTIFIER
2607 ++lexer.parsing_block;
2608 if (RootContext.Documentation != null) {
2609 tmpComment = Lexer.consume_doc_comment ();
2610 Lexer.doc_state = XmlCommentState.NotAllowed;
2613 ASSIGN constant_expression
2615 --lexer.parsing_block;
2616 VariableDeclaration vd = new VariableDeclaration (
2617 (LocatedToken) $2, $5, (Attributes) $1);
2619 if (RootContext.Documentation != null)
2620 vd.DocComment = ConsumeStoredComment ();
2626 delegate_declaration
2630 member_type type_declaration_name
2633 valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out | ParameterModifierType.Params | ParameterModifierType.DefaultValue;
2635 opt_formal_parameter_list CLOSE_PARENS
2637 valid_param_mod = 0;
2639 MemberName name = MakeName ((MemberName) $5);
2640 ParametersCompiled p = (ParametersCompiled) $8;
2642 Delegate del = new Delegate (current_namespace, current_class, (FullNamedExpression) $4,
2643 (int) $2, name, p, (Attributes) $1);
2645 if (RootContext.Documentation != null) {
2646 del.DocComment = Lexer.consume_doc_comment ();
2647 Lexer.doc_state = XmlCommentState.Allowed;
2650 current_container.AddDelegate (del);
2651 current_delegate = del;
2652 lexer.ConstraintsParsing = true;
2654 opt_type_parameter_constraints_clauses
2656 lexer.ConstraintsParsing = false;
2660 current_delegate.SetParameterInfo ((ArrayList) $11);
2661 $$ = current_delegate;
2663 current_delegate = null;
2674 if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)
2675 Report.FeatureIsNotSupported (lexer.Location, "nullable types");
2676 else if (RootContext.Version < LanguageVersion.ISO_2)
2677 Report.FeatureIsNotAvailable (lexer.Location, "nullable types");
2683 namespace_or_type_name
2685 | qualified_alias_member IDENTIFIER opt_type_argument_list
2687 LocatedToken lt1 = (LocatedToken) $1;
2688 LocatedToken lt2 = (LocatedToken) $2;
2690 $$ = new MemberName (lt1.Value, lt2.Value, (TypeArguments) $3, lt1.Location);
2696 | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list
2698 LocatedToken lt = (LocatedToken) $3;
2699 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $4, lt.Location);
2704 : IDENTIFIER opt_type_argument_list
2706 LocatedToken lt = (LocatedToken) $1;
2707 $$ = new MemberName (lt.Value, (TypeArguments)$2, lt.Location);
2712 // Generics arguments (any type, without attributes)
2714 opt_type_argument_list
2715 : /* empty */ { $$ = null; }
2716 | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2718 if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)
2719 Report.FeatureIsNotSupported (lexer.Location, "generics");
2720 else if (RootContext.Version < LanguageVersion.ISO_2)
2721 Report.FeatureIsNotAvailable (GetLocation ($1), "generics");
2725 | OP_GENERICS_LT error
2727 Error_TypeExpected (lexer.Location);
2728 $$ = new TypeArguments ();
2735 TypeArguments type_args = new TypeArguments ();
2736 type_args.Add ((FullNamedExpression) $1);
2739 | type_arguments COMMA type
2741 TypeArguments type_args = (TypeArguments) $1;
2742 type_args.Add ((FullNamedExpression) $3);
2748 // Generics parameters (identifiers only, with attributes), used in type or method declarations
2750 type_declaration_name
2753 lexer.parsing_generic_declaration = true;
2755 opt_type_parameter_list
2757 lexer.parsing_generic_declaration = false;
2758 LocatedToken lt = (LocatedToken) $1;
2759 $$ = new MemberName (lt.Value, (TypeArguments)$3, lt.Location);
2763 member_declaration_name
2764 : method_declaration_name
2766 MemberName mn = (MemberName)$1;
2767 if (mn.TypeArguments != null)
2768 syntax_error (mn.Location, string.Format ("Member `{0}' cannot declare type arguments",
2769 mn.GetSignatureForError ()));
2773 method_declaration_name
2774 : type_declaration_name
2775 | explicit_interface IDENTIFIER opt_type_parameter_list
2777 lexer.parsing_generic_declaration = false;
2778 LocatedToken lt = (LocatedToken) $2;
2779 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $3, lt.Location);
2783 indexer_declaration_name
2786 lexer.parsing_generic_declaration = false;
2787 $$ = new MemberName (TypeContainer.DefaultIndexerName, GetLocation ($1));
2789 | explicit_interface THIS
2791 lexer.parsing_generic_declaration = false;
2792 $$ = new MemberName ((MemberName) $1, TypeContainer.DefaultIndexerName, null, GetLocation ($1));
2797 : IDENTIFIER opt_type_argument_list DOT
2799 LocatedToken lt = (LocatedToken) $1;
2800 $$ = new MemberName (lt.Value, (TypeArguments) $2, lt.Location);
2802 | qualified_alias_member IDENTIFIER opt_type_argument_list DOT
2804 LocatedToken lt1 = (LocatedToken) $1;
2805 LocatedToken lt2 = (LocatedToken) $2;
2807 $$ = new MemberName (lt1.Value, lt2.Value, (TypeArguments) $3, lt1.Location);
2809 | explicit_interface IDENTIFIER opt_type_argument_list DOT
2811 LocatedToken lt = (LocatedToken) $2;
2812 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $3, lt.Location);
2816 opt_type_parameter_list
2817 : /* empty */ { $$ = null; }
2818 | OP_GENERICS_LT_DECL type_parameters OP_GENERICS_GT
2820 if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)
2821 Report.FeatureIsNotSupported (lexer.Location, "generics");
2822 else if (RootContext.Version < LanguageVersion.ISO_2)
2823 Report.FeatureIsNotAvailable (GetLocation ($1), "generics");
2832 TypeArguments type_args = new TypeArguments ();
2833 type_args.Add ((FullNamedExpression)$1);
2836 | type_parameters COMMA type_parameter
2838 TypeArguments type_args = (TypeArguments) $1;
2839 type_args.Add ((FullNamedExpression)$3);
2845 : opt_attributes opt_type_parameter_variance IDENTIFIER
2847 LocatedToken lt = (LocatedToken)$3;
2848 $$ = new TypeParameterName (lt.Value, (Attributes)$1, (Variance) $2, lt.Location);
2852 if (GetTokenName (yyToken) == "type")
2853 Report.Error (81, GetLocation ($1), "Type parameter declaration must be an identifier not a type");
2855 Error_SyntaxError (yyToken);
2857 $$ = new TypeParameterName ("", null, lexer.Location);
2862 // All types where void is allowed
2865 : type_expression_or_array
2868 $$ = TypeManager.system_void_expr;
2875 lexer.parsing_generic_declaration = true;
2880 // A type which does not allow `void' to be used
2883 : type_expression_or_array
2886 Expression.Error_VoidInvalidInTheContext (lexer.Location, Report);
2887 $$ = TypeManager.system_void_expr;
2895 Expression.Error_VoidInvalidInTheContext (lexer.Location, Report);
2896 $$ = TypeManager.system_void_expr;
2901 : type_expression_or_array
2904 Report.Error (1536, lexer.Location, "Invalid parameter type `void'");
2905 $$ = TypeManager.system_void_expr;
2909 type_expression_or_array
2911 | type_expression rank_specifiers
2913 string rank_specifiers = (string) $2;
2914 $$ = current_array_type = new ComposedCast ((FullNamedExpression) $1, rank_specifiers);
2919 : namespace_or_type_name opt_nullable
2921 MemberName name = (MemberName) $1;
2924 $$ = new ComposedCast (name.GetTypeExpression (), "?", lexer.Location);
2926 if (name.Left == null && name.Name == "var")
2927 $$ = current_array_type = new VarExpr (name.Location);
2929 $$ = name.GetTypeExpression ();
2932 | builtin_types opt_nullable
2935 $$ = new ComposedCast ((FullNamedExpression) $1, "?", lexer.Location);
2937 | type_expression STAR
2940 // Note that here only unmanaged types are allowed but we
2941 // can't perform checks during this phase - we do it during
2942 // semantic analysis.
2944 $$ = new ComposedCast ((FullNamedExpression) $1, "*", Lexer.Location);
2948 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2955 ArrayList types = new ArrayList (2);
2959 | type_list COMMA base_type_name
2961 ArrayList types = (ArrayList) $1;
2970 if ($1 is ComposedCast)
2971 Report.Error (1521, GetLocation ($1), "Invalid base type `{0}'", ((ComposedCast)$1).GetSignatureForError ());
2976 Error_TypeExpected (lexer.Location);
2981 * replaces all the productions for isolating the various
2982 * simple types, but we need this to reuse it easily in variable_type
2985 : OBJECT { $$ = TypeManager.system_object_expr; }
2986 | STRING { $$ = TypeManager.system_string_expr; }
2987 | BOOL { $$ = TypeManager.system_boolean_expr; }
2988 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2989 | FLOAT { $$ = TypeManager.system_single_expr; }
2990 | DOUBLE { $$ = TypeManager.system_double_expr; }
2995 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2996 | BYTE { $$ = TypeManager.system_byte_expr; }
2997 | SHORT { $$ = TypeManager.system_int16_expr; }
2998 | USHORT { $$ = TypeManager.system_uint16_expr; }
2999 | INT { $$ = TypeManager.system_int32_expr; }
3000 | UINT { $$ = TypeManager.system_uint32_expr; }
3001 | LONG { $$ = TypeManager.system_int64_expr; }
3002 | ULONG { $$ = TypeManager.system_uint64_expr; }
3003 | CHAR { $$ = TypeManager.system_char_expr; }
3010 $$ = TypeManager.system_void_expr;
3015 // Expressions, section 7.5
3020 : primary_expression_no_array_creation
3021 | array_creation_expression
3024 primary_expression_no_array_creation
3026 | IDENTIFIER opt_type_argument_list
3028 LocatedToken lt = (LocatedToken) $1;
3029 $$ = new SimpleName (MemberName.MakeName (lt.Value, (TypeArguments)$2), (TypeArguments)$2, lt.Location);
3031 | IDENTIFIER GENERATE_COMPLETION {
3032 LocatedToken lt = (LocatedToken) $1;
3033 $$ = new CompletionSimpleName (MemberName.MakeName (lt.Value, null), lt.Location);
3035 | parenthesized_expression
3036 | default_value_expression
3038 | invocation_expression
3042 | post_increment_expression
3043 | post_decrement_expression
3044 | object_or_delegate_creation_expression
3045 | anonymous_type_expression
3048 | checked_expression
3049 | unchecked_expression
3050 | pointer_member_access
3051 | anonymous_method_expression
3058 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
3059 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); }
3060 | NULL { $$ = new NullLiteral (lexer.Location); }
3064 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
3065 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
3066 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
3071 object v = lexer.Value;
3074 $$ = new IntLiteral ((int) v, lexer.Location);
3075 } else if (v is uint)
3076 $$ = new UIntLiteral ((UInt32) v, lexer.Location);
3078 $$ = new LongLiteral ((Int64) v, lexer.Location);
3079 else if (v is ulong)
3080 $$ = new ULongLiteral ((UInt64) v, lexer.Location);
3082 Console.WriteLine ("OOPS. Unexpected result from scanner");
3087 : TRUE { $$ = new BoolLiteral (true, lexer.Location); }
3088 | FALSE { $$ = new BoolLiteral (false, lexer.Location); }
3093 // Here is the trick, tokenizer may think that parens is a special but
3094 // parser is interested in open parens only, so we merge them.
3095 // Consider: if (a)foo ();
3100 | OPEN_PARENS_LAMBDA
3103 parenthesized_expression
3104 : OPEN_PARENS expression CLOSE_PARENS
3106 $$ = new ParenthesizedExpression ((Expression) $2);
3108 | OPEN_PARENS expression COMPLETE_COMPLETION
3110 $$ = new ParenthesizedExpression ((Expression) $2);
3115 : primary_expression DOT IDENTIFIER opt_type_argument_list
3117 LocatedToken lt = (LocatedToken) $3;
3118 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
3120 | predefined_type DOT IDENTIFIER opt_type_argument_list
3122 LocatedToken lt = (LocatedToken) $3;
3123 // TODO: Location is wrong as some predefined types doesn't hold a location
3124 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
3126 | qualified_alias_member IDENTIFIER opt_type_argument_list
3128 LocatedToken lt1 = (LocatedToken) $1;
3129 LocatedToken lt2 = (LocatedToken) $2;
3131 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, (TypeArguments) $3, lt1.Location);
3133 | primary_expression DOT GENERATE_COMPLETION {
3134 $$ = new CompletionMemberAccess ((Expression) $1, null,GetLocation ($3));
3136 | primary_expression DOT IDENTIFIER GENERATE_COMPLETION {
3137 LocatedToken lt = (LocatedToken) $3;
3138 $$ = new CompletionMemberAccess ((Expression) $1, lt.Value, lt.Location);
3140 | predefined_type DOT GENERATE_COMPLETION
3142 // TODO: Location is wrong as some predefined types doesn't hold a location
3143 $$ = new CompletionMemberAccess ((Expression) $1, null, lexer.Location);
3145 | predefined_type DOT IDENTIFIER GENERATE_COMPLETION {
3146 LocatedToken lt = (LocatedToken) $3;
3147 $$ = new CompletionMemberAccess ((Expression) $1, lt.Value, lt.Location);
3151 invocation_expression
3152 : primary_expression open_parens_any opt_argument_list CLOSE_PARENS
3154 $$ = new Invocation ((Expression) $1, (Arguments) $3);
3158 opt_object_or_collection_initializer
3159 : /* empty */ { $$ = null; }
3160 | object_or_collection_initializer
3163 object_or_collection_initializer
3164 : OPEN_BRACE opt_member_initializer_list close_brace_or_complete_completion
3167 $$ = CollectionOrObjectInitializers.Empty;
3169 $$ = new CollectionOrObjectInitializers ((ArrayList) $2, GetLocation ($1));
3171 | OPEN_BRACE member_initializer_list COMMA CLOSE_BRACE
3173 $$ = new CollectionOrObjectInitializers ((ArrayList) $2, GetLocation ($1));
3177 opt_member_initializer_list
3178 : /* empty */ { $$ = null; }
3179 | member_initializer_list
3185 member_initializer_list
3186 : member_initializer
3188 ArrayList a = new ArrayList ();
3192 | member_initializer_list COMMA member_initializer
3194 ArrayList a = (ArrayList)$1;
3201 : IDENTIFIER ASSIGN initializer_value
3203 LocatedToken lt = $1 as LocatedToken;
3204 $$ = new ElementInitializer (lt.Value, (Expression)$3, lt.Location);
3206 | GENERATE_COMPLETION
3208 $$ = new CompletionElementInitializer (null, GetLocation ($1));
3210 | non_assignment_expression opt_COMPLETE_COMPLETION {
3211 CompletionSimpleName csn = $1 as CompletionSimpleName;
3213 $$ = new CollectionElementInitializer ((Expression)$1);
3215 $$ = new CompletionElementInitializer (csn.Prefix, csn.Location);
3217 | OPEN_BRACE expression_list CLOSE_BRACE
3219 $$ = new CollectionElementInitializer ((ArrayList)$2, GetLocation ($1));
3221 | OPEN_BRACE CLOSE_BRACE
3223 Report.Error (1920, GetLocation ($1), "An element initializer cannot be empty");
3229 | object_or_collection_initializer
3233 : /* empty */ { $$ = null; }
3238 : argument_or_named_argument
3240 Arguments list = new Arguments (4);
3241 list.Add ((Argument) $1);
3244 | argument_list COMMA argument
3246 Arguments list = (Arguments) $1;
3247 if (list [list.Count - 1] is NamedArgument)
3248 Error_NamedArgumentExpected ((NamedArgument) list [list.Count - 1]);
3250 list.Add ((Argument) $3);
3253 | argument_list COMMA named_argument
3255 Arguments list = (Arguments) $1;
3256 NamedArgument a = (NamedArgument) $3;
3257 for (int i = 0; i < list.Count; ++i) {
3258 NamedArgument na = list [i] as NamedArgument;
3259 if (na != null && na.Name.Value == a.Name.Value)
3260 Report.Error (1740, na.Name.Location, "Named argument `{0}' specified multiple times",
3267 | argument_list COMMA
3269 Report.Error (839, GetLocation ($2), "An argument is missing");
3272 | COMMA argument_or_named_argument
3274 Report.Error (839, GetLocation ($1), "An argument is missing");
3282 $$ = new Argument ((Expression) $1);
3284 | non_simple_argument
3287 argument_or_named_argument
3293 : REF variable_reference
3295 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
3297 | OUT variable_reference
3299 $$ = new Argument ((Expression) $2, Argument.AType.Out);
3301 | ARGLIST open_parens_any argument_list CLOSE_PARENS
3303 $$ = new Argument (new Arglist ((Arguments) $3, (Location) $1));
3305 | ARGLIST open_parens_any CLOSE_PARENS
3307 $$ = new Argument (new Arglist ((Location) $1));
3311 $$ = new Argument (new ArglistAccess ((Location) $1));
3320 : primary_expression_no_array_creation OPEN_BRACKET expression_list_arguments CLOSE_BRACKET
3322 $$ = new ElementAccess ((Expression) $1, (Arguments) $3);
3324 | array_creation_expression OPEN_BRACKET expression_list_arguments CLOSE_BRACKET
3326 // LAMESPEC: Not allowed according to specification
3327 $$ = new ElementAccess ((Expression) $1, (Arguments) $3);
3329 | primary_expression_no_array_creation rank_specifiers
3331 // So the super-trick is that primary_expression
3332 // can only be either a SimpleName or a MemberAccess.
3333 // The MemberAccess case arises when you have a fully qualified type-name like :
3335 // SimpleName is when you have
3338 Expression expr = (Expression) $1;
3339 if (expr is ComposedCast){
3340 $$ = new ComposedCast ((ComposedCast)expr, (string) $2);
3341 } else if (expr is ATypeNameExpression){
3343 // So we extract the string corresponding to the SimpleName
3346 $$ = new ComposedCast ((ATypeNameExpression)expr, (string) $2);
3348 Error_ExpectingTypeName (expr);
3349 $$ = TypeManager.system_object_expr;
3352 current_array_type = (FullNamedExpression)$$;
3359 ArrayList list = new ArrayList (4);
3363 | expression_list COMMA expression
3365 ArrayList list = (ArrayList) $1;
3371 expression_list_arguments
3372 : expression_list_argument
3374 Arguments args = new Arguments (4);
3375 args.Add ((Argument) $1);
3378 | expression_list_arguments COMMA expression_list_argument
3380 Arguments args = (Arguments) $1;
3381 args.Add ((Argument) $3);
3386 expression_list_argument
3389 $$ = new Argument ((Expression) $1);
3397 $$ = new This (current_block, (Location) $1);
3402 : BASE DOT IDENTIFIER opt_type_argument_list
3404 LocatedToken lt = (LocatedToken) $3;
3405 $$ = new BaseAccess (lt.Value, (TypeArguments) $4, lt.Location);
3407 | BASE OPEN_BRACKET expression_list_arguments CLOSE_BRACKET
3409 $$ = new BaseIndexerAccess ((Arguments) $3, (Location) $1);
3413 Error_SyntaxError (yyToken);
3414 $$ = new BaseAccess (null, GetLocation ($2));
3418 post_increment_expression
3419 : primary_expression OP_INC
3421 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement, (Expression) $1);
3425 post_decrement_expression
3426 : primary_expression OP_DEC
3428 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement, (Expression) $1);
3432 object_or_delegate_creation_expression
3433 : new_expr_start open_parens_any opt_argument_list CLOSE_PARENS opt_object_or_collection_initializer
3436 if (RootContext.Version <= LanguageVersion.ISO_2)
3437 Report.FeatureIsNotAvailable (GetLocation ($1), "object initializers");
3439 $$ = new NewInitialize ((Expression) $1, (Arguments) $3, (CollectionOrObjectInitializers) $5, GetLocation ($1));
3442 $$ = new New ((Expression) $1, (Arguments) $3, GetLocation ($1));
3444 | new_expr_start object_or_collection_initializer
3446 if (RootContext.Version <= LanguageVersion.ISO_2)
3447 Report.FeatureIsNotAvailable (GetLocation ($1), "collection initializers");
3449 $$ = new NewInitialize ((Expression) $1, null, (CollectionOrObjectInitializers) $2, GetLocation ($1));
3453 array_creation_expression
3454 : new_expr_start OPEN_BRACKET expression_list CLOSE_BRACKET
3455 opt_rank_specifier // shift/reduce on OPEN_BRACE
3456 opt_array_initializer
3458 $$ = new ArrayCreation ((FullNamedExpression) $1, (ArrayList) $3, (string) $5, (ArrayList) $6, GetLocation ($1));
3460 | new_expr_start rank_specifiers opt_array_initializer
3463 Report.Error (1586, GetLocation ($1), "Array creation must have array size or array initializer");
3465 $$ = new ArrayCreation ((FullNamedExpression) $1, (string) $2, (ArrayList) $3, GetLocation ($1));
3467 | NEW rank_specifiers array_initializer
3469 if (RootContext.Version <= LanguageVersion.ISO_2)
3470 Report.FeatureIsNotAvailable (GetLocation ($1), "implicitly typed arrays");
3472 $$ = new ImplicitlyTypedArrayCreation ((string) $2, (ArrayList) $3, GetLocation ($1));
3474 | new_expr_start error
3476 Report.Error (1526, GetLocation ($1), "A new expression requires () or [] after type");
3477 $$ = new ArrayCreation ((FullNamedExpression) $1, "[]", null, GetLocation ($1));
3484 ++lexer.parsing_type;
3488 --lexer.parsing_type;
3493 anonymous_type_expression
3494 : NEW OPEN_BRACE anonymous_type_parameters_opt_comma CLOSE_BRACE
3496 if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)
3497 Report.FeatureIsNotSupported (lexer.Location, "anonymous types");
3498 else if (RootContext.Version <= LanguageVersion.ISO_2)
3499 Report.FeatureIsNotAvailable (GetLocation ($1), "anonymous types");
3501 $$ = new AnonymousTypeDeclaration ((ArrayList) $3, current_container, GetLocation ($1));
3505 anonymous_type_parameters_opt_comma
3506 : anonymous_type_parameters_opt
3507 | anonymous_type_parameters COMMA
3510 anonymous_type_parameters_opt
3512 | anonymous_type_parameters
3515 anonymous_type_parameters
3516 : anonymous_type_parameter
3518 ArrayList a = new ArrayList (4);
3522 | anonymous_type_parameters COMMA anonymous_type_parameter
3524 ArrayList a = (ArrayList) $1;
3530 anonymous_type_parameter
3531 : IDENTIFIER ASSIGN variable_initializer
3533 LocatedToken lt = (LocatedToken)$1;
3534 $$ = new AnonymousTypeParameter ((Expression)$3, lt.Value, lt.Location);
3538 LocatedToken lt = (LocatedToken)$1;
3539 $$ = new AnonymousTypeParameter (new SimpleName (lt.Value, lt.Location),
3540 lt.Value, lt.Location);
3542 | BASE DOT IDENTIFIER opt_type_argument_list
3544 LocatedToken lt = (LocatedToken) $3;
3545 BaseAccess ba = new BaseAccess (lt.Value, (TypeArguments) $4, lt.Location);
3546 $$ = new AnonymousTypeParameter (ba, lt.Value, lt.Location);
3550 MemberAccess ma = (MemberAccess) $1;
3551 $$ = new AnonymousTypeParameter (ma, ma.Name, ma.Location);
3555 Report.Error (746, lexer.Location, "Invalid anonymous type member declarator. " +
3556 "Anonymous type members must be a member assignment, simple name or member access expression");
3571 opt_rank_specifier_or_nullable
3579 | opt_nullable rank_specifiers
3582 $$ = "?" + (string) $2;
3590 | rank_specifier rank_specifiers
3592 $$ = (string) $2 + (string) $1;
3597 : OPEN_BRACKET CLOSE_BRACKET
3601 | OPEN_BRACKET dim_separators CLOSE_BRACKET
3603 $$ = "[" + (string) $2 + "]";
3605 | OPEN_BRACKET error
3607 Error_SyntaxError (178, yyToken, "Invalid rank specifier");
3617 | dim_separators COMMA
3619 $$ = (string) $1 + ",";
3623 opt_array_initializer
3635 : OPEN_BRACE CLOSE_BRACE
3637 ArrayList list = new ArrayList (4);
3640 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3642 $$ = (ArrayList) $2;
3646 variable_initializer_list
3647 : variable_initializer
3649 ArrayList list = new ArrayList (4);
3653 | variable_initializer_list COMMA variable_initializer
3655 ArrayList list = (ArrayList) $1;
3661 Error_SyntaxError (yyToken);
3662 $$ = new ArrayList ();
3669 pushed_current_array_type = current_array_type;
3670 lexer.TypeOfParsing = true;
3672 open_parens_any typeof_type_expression CLOSE_PARENS
3674 lexer.TypeOfParsing = false;
3675 Expression type = (Expression)$4;
3676 if (type == TypeManager.system_void_expr)
3677 $$ = new TypeOfVoid ((Location) $1);
3679 $$ = new TypeOf (type, (Location) $1);
3680 current_array_type = pushed_current_array_type;
3684 typeof_type_expression
3689 Error_TypeExpected (lexer.Location);
3695 : IDENTIFIER generic_dimension
3697 LocatedToken lt = (LocatedToken) $1;
3699 $$ = new SimpleName (MemberName.MakeName (lt.Value, (int)$2), lt.Location);
3701 | qualified_alias_member IDENTIFIER generic_dimension
3703 LocatedToken lt1 = (LocatedToken) $1;
3704 LocatedToken lt2 = (LocatedToken) $2;
3706 $$ = new QualifiedAliasMember (lt1.Value, MemberName.MakeName (lt2.Value, (int) $3), lt1.Location);
3708 | unbound_type_name DOT IDENTIFIER
3710 LocatedToken lt = (LocatedToken) $3;
3712 $$ = new MemberAccess ((Expression) $1, lt.Value, lt.Location);
3714 | unbound_type_name DOT IDENTIFIER generic_dimension
3716 LocatedToken lt = (LocatedToken) $3;
3718 $$ = new MemberAccess ((Expression) $1, MemberName.MakeName (lt.Value, (int) $4), lt.Location);
3720 | namespace_or_type_name DOT IDENTIFIER generic_dimension
3722 LocatedToken lt = (LocatedToken) $3;
3723 MemberName name = (MemberName) $1;
3725 $$ = new MemberAccess (name.GetTypeExpression (), MemberName.MakeName (lt.Value, (int) $4), lt.Location);
3732 if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)
3733 Report.FeatureIsNotSupported (lexer.Location, "generics");
3734 else if (RootContext.Version < LanguageVersion.ISO_2)
3735 Report.FeatureIsNotAvailable (lexer.Location, "generics");
3741 qualified_alias_member
3742 : IDENTIFIER DOUBLE_COLON
3744 LocatedToken lt = (LocatedToken) $1;
3745 if (RootContext.Version == LanguageVersion.ISO_1)
3746 Report.FeatureIsNotAvailable (lt.Location, "namespace alias qualifier");
3753 : SIZEOF open_parens_any type CLOSE_PARENS {
3754 $$ = new SizeOf ((Expression) $3, (Location) $1);
3759 : CHECKED open_parens_any expression CLOSE_PARENS
3761 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3765 unchecked_expression
3766 : UNCHECKED open_parens_any expression CLOSE_PARENS
3768 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3772 pointer_member_access
3773 : primary_expression OP_PTR IDENTIFIER
3776 LocatedToken lt = (LocatedToken) $3;
3778 deref = new Indirection ((Expression) $1, lt.Location);
3779 $$ = new MemberAccess (deref, lt.Value);
3783 anonymous_method_expression
3784 : DELEGATE opt_anonymous_method_signature
3786 start_anonymous (false, (ParametersCompiled) $2, (Location) $1);
3790 $$ = end_anonymous ((ToplevelBlock) $4);
3794 opt_anonymous_method_signature
3797 $$ = ParametersCompiled.Undefined;
3799 | anonymous_method_signature
3802 anonymous_method_signature
3805 valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out;
3807 opt_formal_parameter_list CLOSE_PARENS
3809 valid_param_mod = 0;
3814 default_value_expression
3815 : DEFAULT open_parens_any type CLOSE_PARENS
3817 if (RootContext.Version < LanguageVersion.ISO_2)
3818 Report.FeatureIsNotAvailable (lexer.Location, "default value expression");
3820 $$ = new DefaultValueExpression ((Expression) $3, GetLocation ($1));
3825 : primary_expression
3826 | BANG prefixed_unary_expression
3828 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2);
3830 | TILDE prefixed_unary_expression
3832 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2);
3838 : OPEN_PARENS_CAST type CLOSE_PARENS prefixed_unary_expression
3840 $$ = new Cast ((FullNamedExpression) $2, (Expression) $4, GetLocation ($1));
3842 | OPEN_PARENS predefined_type CLOSE_PARENS prefixed_unary_expression
3844 $$ = new Cast ((FullNamedExpression) $2, (Expression) $4, GetLocation ($1));
3849 // The idea to split this out is from Rhys' grammar
3850 // to solve the problem with casts.
3852 prefixed_unary_expression
3854 | PLUS prefixed_unary_expression
3856 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2);
3858 | MINUS prefixed_unary_expression
3860 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2);
3862 | OP_INC prefixed_unary_expression
3864 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement, (Expression) $2);
3866 | OP_DEC prefixed_unary_expression
3868 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement, (Expression) $2);
3870 | STAR prefixed_unary_expression
3872 $$ = new Indirection ((Expression) $2, GetLocation ($1));
3874 | BITWISE_AND prefixed_unary_expression
3876 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2);
3880 multiplicative_expression
3881 : prefixed_unary_expression
3882 | multiplicative_expression STAR prefixed_unary_expression
3884 $$ = new Binary (Binary.Operator.Multiply,
3885 (Expression) $1, (Expression) $3);
3887 | multiplicative_expression DIV prefixed_unary_expression
3889 $$ = new Binary (Binary.Operator.Division,
3890 (Expression) $1, (Expression) $3);
3892 | multiplicative_expression PERCENT prefixed_unary_expression
3894 $$ = new Binary (Binary.Operator.Modulus,
3895 (Expression) $1, (Expression) $3);
3900 : multiplicative_expression
3901 | additive_expression PLUS multiplicative_expression
3903 $$ = new Binary (Binary.Operator.Addition,
3904 (Expression) $1, (Expression) $3);
3906 | additive_expression MINUS multiplicative_expression
3908 $$ = new Binary (Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3910 | parenthesized_expression MINUS multiplicative_expression
3912 // Shift/Reduce conflict
3913 $$ = new Binary (Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3915 | additive_expression AS type
3917 $$ = new As ((Expression) $1, (Expression) $3, (Location) $2);
3919 | additive_expression IS type
3921 $$ = new Is ((Expression) $1, (Expression) $3, (Location) $2);
3926 : additive_expression
3927 | shift_expression OP_SHIFT_LEFT additive_expression
3929 $$ = new Binary (Binary.Operator.LeftShift,
3930 (Expression) $1, (Expression) $3);
3932 | shift_expression OP_SHIFT_RIGHT additive_expression
3934 $$ = new Binary (Binary.Operator.RightShift,
3935 (Expression) $1, (Expression) $3);
3939 relational_expression
3941 | relational_expression OP_LT shift_expression
3943 $$ = new Binary (Binary.Operator.LessThan,
3944 (Expression) $1, (Expression) $3);
3946 | relational_expression OP_GT shift_expression
3948 $$ = new Binary (Binary.Operator.GreaterThan,
3949 (Expression) $1, (Expression) $3);
3951 | relational_expression OP_LE shift_expression
3953 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3954 (Expression) $1, (Expression) $3);
3956 | relational_expression OP_GE shift_expression
3958 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3959 (Expression) $1, (Expression) $3);
3964 : relational_expression
3965 | equality_expression OP_EQ relational_expression
3967 $$ = new Binary (Binary.Operator.Equality,
3968 (Expression) $1, (Expression) $3);
3970 | equality_expression OP_NE relational_expression
3972 $$ = new Binary (Binary.Operator.Inequality,
3973 (Expression) $1, (Expression) $3);
3978 : equality_expression
3979 | and_expression BITWISE_AND equality_expression
3981 $$ = new Binary (Binary.Operator.BitwiseAnd,
3982 (Expression) $1, (Expression) $3);
3986 exclusive_or_expression
3988 | exclusive_or_expression CARRET and_expression
3990 $$ = new Binary (Binary.Operator.ExclusiveOr,
3991 (Expression) $1, (Expression) $3);
3995 inclusive_or_expression
3996 : exclusive_or_expression
3997 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3999 $$ = new Binary (Binary.Operator.BitwiseOr,
4000 (Expression) $1, (Expression) $3);
4004 conditional_and_expression
4005 : inclusive_or_expression
4006 | conditional_and_expression OP_AND inclusive_or_expression
4008 $$ = new Binary (Binary.Operator.LogicalAnd,
4009 (Expression) $1, (Expression) $3);
4013 conditional_or_expression
4014 : conditional_and_expression
4015 | conditional_or_expression OP_OR conditional_and_expression
4017 $$ = new Binary (Binary.Operator.LogicalOr,
4018 (Expression) $1, (Expression) $3);
4022 null_coalescing_expression
4023 : conditional_or_expression
4024 | conditional_or_expression OP_COALESCING null_coalescing_expression
4026 if (RootContext.Version < LanguageVersion.ISO_2)
4027 Report.FeatureIsNotAvailable (GetLocation ($2), "null coalescing operator");
4029 $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $3, lexer.Location);
4033 conditional_expression
4034 : null_coalescing_expression
4035 | null_coalescing_expression INTERR expression COLON expression
4037 $$ = new Conditional (new BooleanExpression ((Expression) $1), (Expression) $3, (Expression) $5);
4041 assignment_expression
4042 : prefixed_unary_expression ASSIGN expression
4044 $$ = new SimpleAssign ((Expression) $1, (Expression) $3);
4046 | prefixed_unary_expression OP_MULT_ASSIGN expression
4048 $$ = new CompoundAssign (
4049 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
4051 | prefixed_unary_expression OP_DIV_ASSIGN expression
4053 $$ = new CompoundAssign (
4054 Binary.Operator.Division, (Expression) $1, (Expression) $3);
4056 | prefixed_unary_expression OP_MOD_ASSIGN expression
4058 $$ = new CompoundAssign (
4059 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
4061 | prefixed_unary_expression OP_ADD_ASSIGN expression
4063 $$ = new CompoundAssign (
4064 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
4066 | prefixed_unary_expression OP_SUB_ASSIGN expression
4068 $$ = new CompoundAssign (
4069 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
4071 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
4073 $$ = new CompoundAssign (
4074 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
4076 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
4078 $$ = new CompoundAssign (
4079 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
4081 | prefixed_unary_expression OP_AND_ASSIGN expression
4083 $$ = new CompoundAssign (
4084 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
4086 | prefixed_unary_expression OP_OR_ASSIGN expression
4088 $$ = new CompoundAssign (
4089 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
4091 | prefixed_unary_expression OP_XOR_ASSIGN expression
4093 $$ = new CompoundAssign (
4094 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
4098 lambda_parameter_list
4101 ArrayList pars = new ArrayList (4);
4106 | lambda_parameter_list COMMA lambda_parameter
4108 ArrayList pars = (ArrayList) $1;
4109 Parameter p = (Parameter)$3;
4110 if (pars[0].GetType () != p.GetType ()) {
4111 Report.Error (748, p.Location, "All lambda parameters must be typed either explicitly or implicitly");
4120 : parameter_modifier parameter_type IDENTIFIER
4122 LocatedToken lt = (LocatedToken) $3;
4124 $$ = new Parameter ((FullNamedExpression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
4126 | parameter_type IDENTIFIER
4128 LocatedToken lt = (LocatedToken) $2;
4130 $$ = new Parameter ((FullNamedExpression) $1, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
4134 LocatedToken lt = (LocatedToken) $1;
4135 $$ = new ImplicitLambdaParameter (lt.Value, lt.Location);
4139 opt_lambda_parameter_list
4140 : /* empty */ { $$ = ParametersCompiled.EmptyReadOnlyParameters; }
4141 | lambda_parameter_list {
4142 ArrayList pars_list = (ArrayList) $1;
4143 $$ = new ParametersCompiled ((Parameter[])pars_list.ToArray (typeof (Parameter)));
4147 lambda_expression_body
4149 start_block (lexer.Location);
4153 Block b = end_block (lexer.Location);
4154 b.AddStatement (new ContextualReturn ((Expression) $2));
4165 LocatedToken lt = (LocatedToken) $1;
4166 Parameter p = new ImplicitLambdaParameter (lt.Value, lt.Location);
4167 start_anonymous (true, new ParametersCompiled (p), GetLocation ($1));
4169 lambda_expression_body
4171 $$ = end_anonymous ((ToplevelBlock) $4);
4173 | OPEN_PARENS_LAMBDA
4175 valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out;
4177 opt_lambda_parameter_list CLOSE_PARENS ARROW
4179 valid_param_mod = 0;
4180 start_anonymous (true, (ParametersCompiled) $3, GetLocation ($1));
4182 lambda_expression_body
4184 $$ = end_anonymous ((ToplevelBlock) $7);
4189 : assignment_expression
4190 | non_assignment_expression
4193 non_assignment_expression
4194 : conditional_expression
4206 $$ = new BooleanExpression ((Expression) $1);
4219 lexer.ConstraintsParsing = true;
4221 type_declaration_name
4223 MemberName name = MakeName ((MemberName) $6);
4224 push_current_class (new Class (current_namespace, current_class, name, (int) $2, (Attributes) $1), $3);
4227 opt_type_parameter_constraints_clauses
4229 lexer.ConstraintsParsing = false;
4231 current_class.SetParameterInfo ((ArrayList) $9);
4233 if (RootContext.Documentation != null) {
4234 current_container.DocComment = Lexer.consume_doc_comment ();
4235 Lexer.doc_state = XmlCommentState.Allowed;
4240 --lexer.parsing_declaration;
4241 if (RootContext.Documentation != null)
4242 Lexer.doc_state = XmlCommentState.Allowed;
4246 $$ = pop_current_class ();
4254 { $$ = $1; } // location
4258 : /* empty */ { $$ = (int) 0; }
4264 | modifiers modifier
4269 if ((m1 & m2) != 0) {
4270 Location l = lexer.Location;
4271 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
4273 $$ = (int) (m1 | m2);
4281 if (current_container == RootContext.ToplevelTypes)
4282 Report.Error (1530, lexer.Location, "Keyword `new' is not allowed on namespace elements");
4284 | PUBLIC { $$ = Modifiers.PUBLIC; }
4285 | PROTECTED { $$ = Modifiers.PROTECTED; }
4286 | INTERNAL { $$ = Modifiers.INTERNAL; }
4287 | PRIVATE { $$ = Modifiers.PRIVATE; }
4288 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
4289 | SEALED { $$ = Modifiers.SEALED; }
4290 | STATIC { $$ = Modifiers.STATIC; }
4291 | READONLY { $$ = Modifiers.READONLY; }
4292 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
4293 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
4294 | EXTERN { $$ = Modifiers.EXTERN; }
4295 | VOLATILE { $$ = Modifiers.VOLATILE; }
4296 | UNSAFE { $$ = Modifiers.UNSAFE; }
4305 : COLON type_list { current_container.AddBasesForPart (current_class, (ArrayList) $2); }
4308 opt_type_parameter_constraints_clauses
4309 : /* empty */ { $$ = null; }
4310 | type_parameter_constraints_clauses
4314 type_parameter_constraints_clauses
4315 : type_parameter_constraints_clause {
4316 ArrayList constraints = new ArrayList (1);
4317 constraints.Add ($1);
4320 | type_parameter_constraints_clauses type_parameter_constraints_clause {
4321 ArrayList constraints = (ArrayList) $1;
4322 Constraints new_constraint = (Constraints)$2;
4324 foreach (Constraints c in constraints) {
4325 if (new_constraint.TypeParameter == c.TypeParameter) {
4326 Report.Error (409, new_constraint.Location, "A constraint clause has already been specified for type parameter `{0}'",
4327 new_constraint.TypeParameter);
4331 constraints.Add (new_constraint);
4336 type_parameter_constraints_clause
4337 : WHERE IDENTIFIER COLON type_parameter_constraints {
4338 LocatedToken lt = (LocatedToken) $2;
4339 $$ = new Constraints (lt.Value, (ArrayList) $4, lt.Location);
4343 type_parameter_constraints
4344 : type_parameter_constraint {
4345 ArrayList constraints = new ArrayList (1);
4346 constraints.Add ($1);
4349 | type_parameter_constraints COMMA type_parameter_constraint {
4350 ArrayList constraints = (ArrayList) $1;
4352 constraints.Add ($3);
4357 type_parameter_constraint
4359 | NEW OPEN_PARENS CLOSE_PARENS {
4360 $$ = SpecialConstraint.Constructor;
4363 $$ = SpecialConstraint.ReferenceType;
4366 $$ = SpecialConstraint.ValueType;
4370 opt_type_parameter_variance
4375 | type_parameter_variance
4377 if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)
4378 Report.FeatureIsNotSupported (lexer.Location, "generic type variance");
4379 else if (RootContext.Version <= LanguageVersion.V_3)
4380 Report.FeatureIsNotAvailable (lexer.Location, "generic type variance");
4386 type_parameter_variance
4389 $$ = Variance.Covariant;
4393 $$ = Variance.Contravariant;
4402 // A block is "contained" on the following places:
4404 // property_declaration as part of the accessor body (get/set)
4405 // operator_declaration
4406 // constructor_declaration
4407 // destructor_declaration
4408 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
4413 ++lexer.parsing_block;
4414 start_block ((Location) $1);
4416 opt_statement_list block_end
4425 --lexer.parsing_block;
4426 $$ = end_block ((Location) $1);
4428 | COMPLETE_COMPLETION
4430 --lexer.parsing_block;
4431 $$ = end_block (lexer.Location);
4439 ++lexer.parsing_block;
4440 current_block.StartLocation = GetLocation ($1);
4442 opt_statement_list CLOSE_BRACE
4444 --lexer.parsing_block;
4445 $$ = end_block ((Location) $4);
4456 | statement_list statement
4460 : declaration_statement
4462 if ($1 != null && (Block) $1 != current_block){
4463 current_block.AddStatement ((Statement) $1);
4464 current_block = (Block) $1;
4467 | valid_declaration_statement
4469 current_block.AddStatement ((Statement) $1);
4475 // The interactive_statement and its derivatives are only
4476 // used to provide a special version of `expression_statement'
4477 // that has a side effect of assigning the expression to
4480 interactive_statement_list
4481 : interactive_statement
4482 | interactive_statement_list interactive_statement
4485 interactive_statement
4486 : declaration_statement
4488 if ($1 != null && (Block) $1 != current_block){
4489 current_block.AddStatement ((Statement) $1);
4490 current_block = (Block) $1;
4493 | interactive_valid_declaration_statement
4495 current_block.AddStatement ((Statement) $1);
4500 valid_declaration_statement
4503 | expression_statement
4504 | selection_statement
4505 | iteration_statement
4509 | unchecked_statement
4516 interactive_valid_declaration_statement
4519 | interactive_expression_statement
4520 | selection_statement
4521 | iteration_statement
4525 | unchecked_statement
4533 : valid_declaration_statement
4534 | declaration_statement
4536 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4541 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4549 $$ = EmptyStatement.Value;
4556 LocatedToken lt = (LocatedToken) $1;
4557 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
4559 if (current_block.AddLabel (labeled))
4560 current_block.AddStatement (labeled);
4565 declaration_statement
4566 : local_variable_declaration SEMICOLON
4568 current_array_type = null;
4570 DictionaryEntry de = (DictionaryEntry) $1;
4571 Expression e = (Expression) de.Key;
4573 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
4577 | local_constant_declaration SEMICOLON
4579 current_array_type = null;
4581 DictionaryEntry de = (DictionaryEntry) $1;
4583 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
4589 * The following is from Rhys' grammar:
4590 * > Types in local variable declarations must be recognized as
4591 * > expressions to prevent reduce/reduce errors in the grammar.
4592 * > The expressions are converted into types during semantic analysis.
4595 : primary_expression_no_array_creation opt_rank_specifier_or_nullable
4597 // FIXME: Do something smart here regarding the composition of the type.
4599 // Ok, the above "primary_expression" is there to get rid of
4600 // both reduce/reduce and shift/reduces in the grammar, it should
4601 // really just be "type_name". If you use type_name, a reduce/reduce
4602 // creeps up. If you use namespace_or_type_name (which is all we need
4603 // really) two shift/reduces appear.
4606 // So the super-trick is that primary_expression
4607 // can only be either a SimpleName or a MemberAccess.
4608 // The MemberAccess case arises when you have a fully qualified type-name like :
4610 // SimpleName is when you have
4613 Expression expr = (Expression) $1;
4614 string rank_or_nullable = (string) $2;
4616 if (expr is ComposedCast){
4617 $$ = new ComposedCast ((ComposedCast)expr, rank_or_nullable);
4618 } else if (expr is ATypeNameExpression){
4620 // So we extract the string corresponding to the SimpleName
4623 if (rank_or_nullable.Length == 0) {
4624 SimpleName sn = expr as SimpleName;
4625 if (sn != null && sn.Name == "var")
4626 $$ = current_array_type = new VarExpr (sn.Location);
4630 $$ = new ComposedCast ((ATypeNameExpression)expr, rank_or_nullable);
4633 Error_ExpectingTypeName (expr);
4634 $$ = TypeManager.system_object_expr;
4637 | builtin_types opt_rank_specifier_or_nullable
4639 if ((string) $2 == "")
4642 $$ = current_array_type = new ComposedCast ((FullNamedExpression) $1, (string) $2, lexer.Location);
4644 | VOID opt_rank_specifier
4646 Expression.Error_VoidInvalidInTheContext (lexer.Location, Report);
4647 $$ = TypeManager.system_void_expr;
4651 local_variable_pointer_type
4652 : primary_expression_no_array_creation STAR
4654 ATypeNameExpression expr = $1 as ATypeNameExpression;
4657 $$ = new ComposedCast (expr, "*");
4659 Error_ExpectingTypeName ((Expression)$1);
4663 | builtin_types STAR
4665 $$ = new ComposedCast ((FullNamedExpression) $1, "*", GetLocation ($1));
4669 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
4671 | local_variable_pointer_type STAR
4673 $$ = new ComposedCast ((FullNamedExpression) $1, "*");
4679 | local_variable_pointer_type opt_rank_specifier
4682 string rank = (string)$2;
4687 $$ = current_array_type = new ComposedCast ((FullNamedExpression) $1, rank);
4694 local_variable_declaration
4695 : local_variable_type local_variable_declarators
4698 VarExpr ve = $1 as VarExpr;
4700 ve.VariableInitializer = (ArrayList)$2;
4702 $$ = new DictionaryEntry ($1, $2);
4708 local_constant_declaration
4709 : CONST variable_type constant_declarators
4712 $$ = new DictionaryEntry ($2, $3);
4718 expression_statement
4719 : statement_expression SEMICOLON { $$ = $1; }
4720 | statement_expression COMPLETE_COMPLETION { $$ = $1; }
4723 interactive_expression_statement
4724 : interactive_statement_expression SEMICOLON { $$ = $1; }
4725 | interactive_statement_expression COMPLETE_COMPLETION { $$ = $1; }
4729 // We have to do the wrapping here and not in the case above,
4730 // because statement_expression is used for example in for_statement
4732 statement_expression
4735 ExpressionStatement s = $1 as ExpressionStatement;
4737 Expression.Error_InvalidExpressionStatement (Report, GetLocation ($1));
4738 s = EmptyExpressionStatement.Instance;
4741 $$ = new StatementExpression (s);
4745 Error_SyntaxError (yyToken);
4750 interactive_statement_expression
4753 Expression expr = (Expression) $1;
4754 ExpressionStatement s;
4756 s = new OptionalAssign (new SimpleName ("$retval", lexer.Location), expr, lexer.Location);
4757 $$ = new StatementExpression (s);
4761 Error_SyntaxError (yyToken);
4772 : IF open_parens_any boolean_expression CLOSE_PARENS
4775 Location l = (Location) $1;
4777 $$ = new If ((BooleanExpression) $3, (Statement) $5, l);
4779 // FIXME: location for warning should be loc property of $5.
4780 if ($5 == EmptyStatement.Value)
4781 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4784 | IF open_parens_any boolean_expression CLOSE_PARENS
4785 embedded_statement ELSE embedded_statement
4787 Location l = (Location) $1;
4789 $$ = new If ((BooleanExpression) $3, (Statement) $5, (Statement) $7, l);
4791 // FIXME: location for warning should be loc property of $5 and $7.
4792 if ($5 == EmptyStatement.Value)
4793 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4794 if ($7 == EmptyStatement.Value)
4795 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4800 : SWITCH open_parens_any
4802 if (switch_stack == null)
4803 switch_stack = new Stack (2);
4804 switch_stack.Push (current_block);
4806 expression CLOSE_PARENS
4809 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
4810 current_block = (Block) switch_stack.Pop ();
4826 Report.Warning (1522, 1, lexer.Location, "Empty switch block");
4827 $$ = new ArrayList ();
4835 ArrayList sections = new ArrayList (4);
4840 | switch_sections switch_section
4842 ArrayList sections = (ArrayList) $1;
4852 current_block = current_block.CreateSwitchBlock (lexer.Location);
4856 $$ = new SwitchSection ((ArrayList) $1, current_block.Explicit);
4863 ArrayList labels = new ArrayList (4);
4868 | switch_labels switch_label
4870 ArrayList labels = (ArrayList) ($1);
4878 : CASE constant_expression COLON
4880 $$ = new SwitchLabel ((Expression) $2, (Location) $1);
4884 $$ = new SwitchLabel (null, (Location) $1);
4896 : WHILE open_parens_any boolean_expression CLOSE_PARENS embedded_statement
4898 Location l = (Location) $1;
4899 $$ = new While ((BooleanExpression) $3, (Statement) $5, l);
4904 : DO embedded_statement
4905 WHILE open_parens_any boolean_expression CLOSE_PARENS SEMICOLON
4907 Location l = (Location) $1;
4909 $$ = new Do ((Statement) $2, (BooleanExpression) $5, l);
4914 : FOR open_parens_any opt_for_initializer SEMICOLON
4916 Location l = lexer.Location;
4918 Block assign_block = current_block;
4920 if ($3 is DictionaryEntry){
4921 DictionaryEntry de = (DictionaryEntry) $3;
4923 Expression type = (Expression) de.Key;
4924 ArrayList var_declarators = (ArrayList) de.Value;
4926 foreach (VariableDeclaration decl in var_declarators){
4930 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4934 Expression expr = decl.expression_or_array_initializer;
4936 LocalVariableReference var;
4937 var = new LocalVariableReference (assign_block, decl.identifier, l);
4940 Assign a = new SimpleAssign (var, expr, decl.Location);
4942 assign_block.AddStatement (new StatementExpression (a));
4946 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4947 // This can be referred to as $5 below.
4953 opt_for_condition SEMICOLON
4954 opt_for_iterator CLOSE_PARENS
4957 Location l = (Location) $1;
4959 For f = new For ((Statement) $5, (BooleanExpression) $6, (Statement) $8, (Statement) $10, l);
4961 current_block.AddStatement (f);
4963 $$ = end_block (lexer.Location);
4968 : /* empty */ { $$ = EmptyStatement.Value; }
4973 : local_variable_declaration
4974 | statement_expression_list
4978 : /* empty */ { $$ = null; }
4979 | boolean_expression
4983 : /* empty */ { $$ = EmptyStatement.Value; }
4988 : statement_expression_list
4991 statement_expression_list
4992 : statement_expression
4994 // CHANGE: was `null'
4995 Statement s = (Statement) $1;
4996 Block b = new Block (current_block, s.loc, lexer.Location);
5001 | statement_expression_list COMMA statement_expression
5003 Block b = (Block) $1;
5005 b.AddStatement ((Statement) $3);
5011 : FOREACH open_parens_any type IN expression CLOSE_PARENS
5013 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
5016 | FOREACH open_parens_any type IDENTIFIER IN
5017 expression CLOSE_PARENS
5019 start_block (lexer.Location);
5020 Block foreach_block = current_block;
5022 LocatedToken lt = (LocatedToken) $4;
5023 Location l = lt.Location;
5024 LocalInfo vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
5026 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
5028 // Get a writable reference to this read-only variable.
5030 // Note that the $$ here refers to the value of _this_ code block,
5031 // not the value of the LHS non-terminal. This can be referred to as $8 below.
5032 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
5039 LocalVariableReference v = (LocalVariableReference) $8;
5040 Location l = (Location) $1;
5043 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
5044 current_block.AddStatement (f);
5047 $$ = end_block (lexer.Location);
5053 | continue_statement
5063 $$ = new Break ((Location) $1);
5068 : CONTINUE SEMICOLON
5070 $$ = new Continue ((Location) $1);
5075 : GOTO IDENTIFIER SEMICOLON
5077 LocatedToken lt = (LocatedToken) $2;
5078 $$ = new Goto (lt.Value, lt.Location);
5080 | GOTO CASE constant_expression SEMICOLON
5082 $$ = new GotoCase ((Expression) $3, (Location) $1);
5084 | GOTO DEFAULT SEMICOLON
5086 $$ = new GotoDefault ((Location) $1);
5091 : RETURN opt_expression SEMICOLON
5093 $$ = new Return ((Expression) $2, (Location) $1);
5098 : THROW opt_expression SEMICOLON
5100 $$ = new Throw ((Expression) $2, (Location) $1);
5105 : IDENTIFIER RETURN expression SEMICOLON
5107 LocatedToken lt = (LocatedToken) $1;
5108 string s = lt.Value;
5110 Report.Error (1003, lt.Location, "; expected");
5113 if (RootContext.Version == LanguageVersion.ISO_1){
5114 Report.FeatureIsNotAvailable (lt.Location, "yield statement");
5117 current_block.Toplevel.IsIterator = true;
5118 $$ = new Yield ((Expression) $3, lt.Location);
5120 | IDENTIFIER RETURN SEMICOLON
5122 Report.Error (1627, (Location) $2, "Expression expected after yield return");
5125 | IDENTIFIER BREAK SEMICOLON
5127 LocatedToken lt = (LocatedToken) $1;
5128 string s = lt.Value;
5130 Report.Error (1003, lt.Location, "; expected");
5133 if (RootContext.Version == LanguageVersion.ISO_1){
5134 Report.FeatureIsNotAvailable (lt.Location, "yield statement");
5138 current_block.Toplevel.IsIterator = true;
5139 $$ = new YieldBreak (lt.Location);
5149 : TRY block catch_clauses
5151 $$ = new TryCatch ((Block) $2, (ArrayList) $3, (Location) $1, false);
5153 | TRY block FINALLY block
5155 $$ = new TryFinally ((Statement) $2, (Block) $4, (Location) $1);
5157 | TRY block catch_clauses FINALLY block
5159 $$ = new TryFinally (new TryCatch ((Block) $2, (ArrayList) $3, (Location) $1, true), (Block) $5, (Location) $1);
5163 Report.Error (1524, (Location) $1, "Expected catch or finally");
5171 ArrayList l = new ArrayList (4);
5176 | catch_clauses catch_clause
5178 ArrayList l = (ArrayList) $1;
5180 Catch c = (Catch) $2;
5181 if (((Catch) l [0]).IsGeneral) {
5182 Report.Error (1017, c.loc, "Try statement already has an empty catch block");
5195 : /* empty */ { $$ = null; }
5200 : CATCH opt_catch_args
5202 Expression type = null;
5205 DictionaryEntry cc = (DictionaryEntry) $2;
5206 type = (Expression) cc.Key;
5207 LocatedToken lt = (LocatedToken) cc.Value;
5210 ArrayList one = new ArrayList (4);
5212 one.Add (new VariableDeclaration (lt, null));
5214 start_block (lexer.Location);
5215 current_block = declare_local_variables (type, one, lt.Location);
5219 Expression type = null;
5221 Block var_block = null;
5224 DictionaryEntry cc = (DictionaryEntry) $2;
5225 type = (Expression) cc.Key;
5226 LocatedToken lt = (LocatedToken) cc.Value;
5230 var_block = end_block (lexer.Location);
5234 $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
5239 : /* empty */ { $$ = null; }
5244 : open_parens_any type opt_identifier CLOSE_PARENS
5246 $$ = new DictionaryEntry ($2, $3);
5248 | open_parens_any CLOSE_PARENS
5250 Report.Error (1015, GetLocation ($1), "A type that derives from `System.Exception', `object', or `string' expected");
5258 $$ = new Checked ((Block) $2);
5265 $$ = new Unchecked ((Block) $2);
5272 RootContext.CheckUnsafeOption ((Location) $1, Report);
5274 $$ = new Unsafe ((Block) $3);
5279 : FIXED open_parens_any
5280 type_and_void fixed_pointer_declarators
5283 ArrayList list = (ArrayList) $4;
5284 Expression type = (Expression) $3;
5285 Location l = (Location) $1;
5286 int top = list.Count;
5288 start_block (lexer.Location);
5290 for (int i = 0; i < top; i++){
5291 Pair p = (Pair) list [i];
5294 v = current_block.AddVariable (type, (string) p.First, l);
5298 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
5306 Location l = (Location) $1;
5308 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
5310 current_block.AddStatement (f);
5312 $$ = end_block (lexer.Location);
5316 fixed_pointer_declarators
5317 : fixed_pointer_declarator {
5318 ArrayList declarators = new ArrayList (4);
5320 declarators.Add ($1);
5323 | fixed_pointer_declarators COMMA fixed_pointer_declarator
5325 ArrayList declarators = (ArrayList) $1;
5327 declarators.Add ($3);
5332 fixed_pointer_declarator
5333 : IDENTIFIER ASSIGN expression
5335 LocatedToken lt = (LocatedToken) $1;
5336 // FIXME: keep location
5337 $$ = new Pair (lt.Value, $3);
5341 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
5347 : LOCK open_parens_any expression CLOSE_PARENS
5353 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
5358 : USING open_parens_any local_variable_declaration CLOSE_PARENS
5360 start_block (lexer.Location);
5361 Block assign_block = current_block;
5363 DictionaryEntry de = (DictionaryEntry) $3;
5364 Location l = (Location) $1;
5366 Expression type = (Expression) de.Key;
5367 ArrayList var_declarators = (ArrayList) de.Value;
5369 Stack vars = new Stack ();
5371 foreach (VariableDeclaration decl in var_declarators) {
5372 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
5375 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
5377 Expression expr = decl.expression_or_array_initializer;
5379 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
5382 LocalVariableReference var;
5384 // Get a writable reference to this read-only variable.
5385 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
5387 // This is so that it is not a warning on using variables
5390 vars.Push (new DictionaryEntry (var, expr));
5392 // Assign a = new SimpleAssign (var, expr, decl.Location);
5393 // assign_block.AddStatement (new StatementExpression (a));
5396 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
5397 // It can be referred to as $5 below.
5402 Statement stmt = (Statement) $6;
5403 Stack vars = (Stack) $5;
5404 Location l = (Location) $1;
5406 while (vars.Count > 0) {
5407 DictionaryEntry de = (DictionaryEntry) vars.Pop ();
5408 stmt = new Using ((Expression) de.Key, (Expression) de.Value, stmt, l);
5410 current_block.AddStatement (stmt);
5411 $$ = end_block (lexer.Location);
5413 | USING open_parens_any expression CLOSE_PARENS
5415 start_block (lexer.Location);
5419 current_block.AddStatement (new UsingTemporary ((Expression) $3, (Statement) $6, (Location) $1));
5420 $$ = end_block (lexer.Location);
5428 : first_from_clause query_body
5430 lexer.query_parsing = false;
5432 Linq.AQueryClause from = $1 as Linq.AQueryClause;
5434 from.Tail.Next = (Linq.AQueryClause)$2;
5437 current_block.SetEndLocation (lexer.Location);
5438 current_block = current_block.Parent;
5440 | nested_from_clause query_body
5442 Linq.AQueryClause from = $1 as Linq.AQueryClause;
5444 from.Tail.Next = (Linq.AQueryClause)$2;
5447 current_block.SetEndLocation (lexer.Location);
5448 current_block = current_block.Parent;
5453 : FROM_FIRST IDENTIFIER IN expression
5455 $$ = new Linq.QueryExpression (current_block, new Linq.QueryStartClause ((Expression)$4));
5456 current_block = new Linq.QueryBlock (compiler, current_block, (LocatedToken) $2, GetLocation ($1));
5458 | FROM_FIRST type IDENTIFIER IN expression
5460 $$ = new Linq.QueryExpression (current_block, new Linq.Cast ((FullNamedExpression)$2, (Expression)$5));
5461 current_block = new Linq.QueryBlock (compiler, current_block, (LocatedToken) $3, GetLocation ($1));
5466 : FROM IDENTIFIER IN expression
5468 $$ = new Linq.QueryExpression (current_block, new Linq.QueryStartClause ((Expression)$4));
5469 current_block = new Linq.QueryBlock (compiler, current_block, (LocatedToken) $2, GetLocation ($1));
5471 | FROM type IDENTIFIER IN expression
5473 $$ = new Linq.QueryExpression (current_block, new Linq.Cast ((FullNamedExpression)$2, (Expression)$5));
5474 current_block = new Linq.QueryBlock (compiler, current_block, (LocatedToken) $3, GetLocation ($1));
5479 : FROM IDENTIFIER IN
5481 current_block = new Linq.QueryBlock (compiler, current_block, GetLocation ($1));
5485 LocatedToken lt = (LocatedToken) $2;
5486 $$ = new Linq.SelectMany (current_block.Toplevel, lt, (Expression)$5);
5488 current_block.SetEndLocation (lexer.Location);
5489 current_block = current_block.Parent;
5491 ((Linq.QueryBlock)current_block).AddTransparentParameter (lt);
5493 | FROM type IDENTIFIER IN
5495 current_block = new Linq.QueryBlock (compiler, current_block, GetLocation ($1));
5499 LocatedToken lt = (LocatedToken) $3;
5500 FullNamedExpression type = (FullNamedExpression)$2;
5502 $$ = new Linq.SelectMany (current_block.Toplevel, lt, new Linq.Cast (type, (FullNamedExpression)$6));
5504 current_block.SetEndLocation (lexer.Location);
5505 current_block = current_block.Parent;
5507 ((Linq.QueryBlock)current_block).AddTransparentParameter (lt);
5512 : opt_query_body_clauses select_or_group_clause opt_query_continuation
5514 Linq.AQueryClause head = (Linq.AQueryClause)$2;
5517 head.Next = (Linq.AQueryClause)$3;
5520 Linq.AQueryClause clause = (Linq.AQueryClause)$1;
5521 clause.Tail.Next = head;
5529 select_or_group_clause
5532 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5536 $$ = new Linq.Select (current_block.Toplevel, (Expression)$3, GetLocation ($1));
5538 current_block.SetEndLocation (lexer.Location);
5539 current_block = current_block.Parent;
5543 if (linq_clause_blocks == null)
5544 linq_clause_blocks = new Stack ();
5546 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5547 linq_clause_blocks.Push (current_block);
5551 current_block.SetEndLocation (lexer.Location);
5552 current_block = current_block.Parent;
5554 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5558 $$ = new Linq.GroupBy (current_block.Toplevel, (Expression)$3, (ToplevelBlock) linq_clause_blocks.Pop (), (Expression)$6, GetLocation ($1));
5560 current_block.SetEndLocation (lexer.Location);
5561 current_block = current_block.Parent;
5565 opt_query_body_clauses
5567 | query_body_clauses
5572 | query_body_clauses query_body_clause
5574 ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$2;
5588 : LET IDENTIFIER ASSIGN
5590 current_block = new Linq.QueryBlock (compiler, current_block, GetLocation ($1));
5594 LocatedToken lt = (LocatedToken) $2;
5595 $$ = new Linq.Let (current_block.Toplevel, current_container, lt, (Expression)$5);
5597 current_block.SetEndLocation (lexer.Location);
5598 current_block = current_block.Parent;
5600 ((Linq.QueryBlock)current_block).AddTransparentParameter (lt);
5607 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5611 $$ = new Linq.Where (current_block.Toplevel, (BooleanExpression)$3, GetLocation ($1));
5613 current_block.SetEndLocation (lexer.Location);
5614 current_block = current_block.Parent;
5619 : JOIN IDENTIFIER IN
5621 if (linq_clause_blocks == null)
5622 linq_clause_blocks = new Stack ();
5624 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5625 linq_clause_blocks.Push (current_block);
5629 current_block.SetEndLocation (lexer.Location);
5630 current_block = current_block.Parent;
5632 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5633 linq_clause_blocks.Push (current_block);
5637 current_block.AddStatement (new ContextualReturn ((Expression) $8));
5638 current_block.SetEndLocation (lexer.Location);
5639 current_block = current_block.Parent;
5641 current_block = new Linq.QueryBlock (compiler, current_block, (LocatedToken) $2, lexer.Location);
5643 expression opt_join_into
5645 LocatedToken lt = (LocatedToken) $2;
5647 ToplevelBlock outer_selector = (ToplevelBlock) linq_clause_blocks.Pop ();
5648 ToplevelBlock block = (ToplevelBlock) linq_clause_blocks.Pop ();
5651 $$ = new Linq.Join (block, lt, (Expression)$5, outer_selector, current_block.Toplevel, GetLocation ($1));
5653 $$ = new Linq.GroupJoin (block, lt, (Expression)$5, outer_selector, current_block.Toplevel,
5654 (LocatedToken) $12, GetLocation ($1));
5657 current_block.AddStatement (new ContextualReturn ((Expression) $11));
5658 current_block.SetEndLocation (lexer.Location);
5659 current_block = current_block.Parent;
5662 ((Linq.QueryBlock)current_block).AddTransparentParameter (lt);
5664 ((Linq.QueryBlock)current_block).AddTransparentParameter ((LocatedToken) $12);
5666 | JOIN type IDENTIFIER IN
5668 if (linq_clause_blocks == null)
5669 linq_clause_blocks = new Stack ();
5671 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5672 linq_clause_blocks.Push (current_block);
5676 current_block.SetEndLocation (lexer.Location);
5677 current_block = current_block.Parent;
5679 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5680 linq_clause_blocks.Push (current_block);
5684 current_block.AddStatement (new ContextualReturn ((Expression) $9));
5685 current_block.SetEndLocation (lexer.Location);
5686 current_block = current_block.Parent;
5688 current_block = new Linq.QueryBlock (compiler, current_block, (LocatedToken) $3, lexer.Location);
5690 expression opt_join_into
5692 LocatedToken lt = (LocatedToken) $3;
5693 ToplevelBlock outer_selector = (ToplevelBlock) linq_clause_blocks.Pop ();
5694 ToplevelBlock block = (ToplevelBlock) linq_clause_blocks.Pop ();
5696 Linq.Cast cast = new Linq.Cast ((FullNamedExpression)$2, (Expression)$6);
5698 $$ = new Linq.Join (block, lt, cast, outer_selector, current_block.Toplevel, GetLocation ($1));
5700 $$ = new Linq.GroupJoin (block, lt, cast, outer_selector, current_block.Toplevel,
5701 (LocatedToken) $13, GetLocation ($1));
5704 current_block.AddStatement (new ContextualReturn ((Expression) $12));
5705 current_block.SetEndLocation (lexer.Location);
5706 current_block = current_block.Parent;
5709 ((Linq.QueryBlock)current_block).AddTransparentParameter (lt);
5711 ((Linq.QueryBlock)current_block).AddTransparentParameter ((LocatedToken) $13);
5726 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5730 current_block.SetEndLocation (lexer.Location);
5731 current_block = current_block.Parent;
5741 current_block.SetEndLocation (lexer.Location);
5742 current_block = current_block.Parent;
5744 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5748 ((Linq.AQueryClause)$1).Next = (Linq.AQueryClause)$4;
5755 | orderings_then_by COMMA
5757 current_block.SetEndLocation (lexer.Location);
5758 current_block = current_block.Parent;
5760 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5764 ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$3;
5772 $$ = new Linq.OrderByAscending (current_block.Toplevel, (Expression)$1);
5774 | expression ASCENDING
5776 $$ = new Linq.OrderByAscending (current_block.Toplevel, (Expression)$1);
5778 | expression DESCENDING
5780 $$ = new Linq.OrderByDescending (current_block.Toplevel, (Expression)$1);
5787 $$ = new Linq.ThenByAscending (current_block.Toplevel, (Expression)$1);
5789 | expression ASCENDING
5791 $$ = new Linq.ThenByAscending (current_block.Toplevel, (Expression)$1);
5793 | expression DESCENDING
5795 $$ = new Linq.ThenByDescending (current_block.Toplevel, (Expression)$1);
5800 opt_query_continuation
5804 // query continuation block is not linked with query block but with block
5805 // before. This means each query can use same range variable names for
5806 // different identifiers.
5808 current_block.SetEndLocation (GetLocation ($1));
5809 current_block = current_block.Parent;
5811 current_block = new Linq.QueryBlock (compiler, current_block, (LocatedToken) $2, GetLocation ($1));
5815 $$ = new Linq.QueryExpression (current_block, (Linq.AQueryClause)$4);
5820 // Support for using the compiler as an interactive parser
5822 // The INTERACTIVE_PARSER token is first sent to parse our
5823 // productions; If the result is a Statement, the parsing
5824 // is repeated, this time with INTERACTIVE_PARSE_WITH_BLOCK
5825 // to setup the blocks in advance.
5827 // This setup is here so that in the future we can add
5828 // support for other constructs (type parsing, namespaces, etc)
5829 // that do not require a block to be setup in advance
5833 : EVAL_STATEMENT_PARSER EOF
5834 | EVAL_USING_DECLARATIONS_UNIT_PARSER using_directives
5835 | EVAL_STATEMENT_PARSER {
5836 Evaluator.LoadAliases (current_namespace);
5838 push_current_class (new Class (current_namespace, current_class, new MemberName ("Class" + class_count++),
5839 Modifiers.PUBLIC, null), null);
5841 ArrayList baseclass_list = new ArrayList ();
5842 baseclass_list.Add (new TypeExpression (Evaluator.InteractiveBaseClass, lexer.Location));
5843 current_container.AddBasesForPart (current_class, baseclass_list);
5845 // (ref object retval)
5846 Parameter [] mpar = new Parameter [1];
5847 mpar [0] = new Parameter (TypeManager.system_object_expr, "$retval", Parameter.Modifier.REF, null, Location.Null);
5849 ParametersCompiled pars = new ParametersCompiled (mpar);
5850 current_local_parameters = pars;
5851 Method method = new Method (
5854 TypeManager.system_void_expr,
5855 Modifiers.PUBLIC | Modifiers.STATIC,
5856 new MemberName ("Host"),
5858 null /* attributes */);
5860 oob_stack.Push (method);
5861 ++lexer.parsing_block;
5862 start_block (lexer.Location);
5864 interactive_statement_list opt_COMPLETE_COMPLETION
5866 --lexer.parsing_block;
5867 Method method = (Method) oob_stack.Pop ();
5869 method.Block = (ToplevelBlock) end_block(lexer.Location);
5870 current_container.AddMethod (method);
5872 --lexer.parsing_declaration;
5873 InteractiveResult = pop_current_class ();
5874 current_local_parameters = null;
5876 | EVAL_COMPILATION_UNIT_PARSER {
5877 Evaluator.LoadAliases (current_namespace);
5879 interactive_compilation_unit
5882 interactive_compilation_unit
5883 : outer_declarations
5884 | outer_declarations global_attributes
5889 opt_COMPLETE_COMPLETION
5891 | COMPLETE_COMPLETION
5894 close_brace_or_complete_completion
5896 | COMPLETE_COMPLETION
5901 // A class used to pass around variable declarations and constants
5903 public class VariableDeclaration {
5904 public string identifier;
5905 public Expression expression_or_array_initializer;
5906 public Location Location;
5907 public Attributes OptAttributes;
5908 public string DocComment;
5910 public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
5912 this.identifier = lt.Value;
5913 if (eoai is ArrayList) {
5914 this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
5916 this.expression_or_array_initializer = (Expression)eoai;
5918 this.Location = lt.Location;
5919 this.OptAttributes = opt_attrs;
5922 public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
5927 class VariableMemberDeclaration
5929 public readonly MemberName MemberName;
5930 public Expression expression_or_array_initializer;
5932 public VariableMemberDeclaration (MemberName mn, object initializer)
5936 if (initializer is ArrayList) {
5937 this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)initializer, mn.Location);
5939 this.expression_or_array_initializer = (Expression)initializer;
5946 // A class used to hold info about an operator declarator
5948 struct OperatorDeclaration {
5949 public readonly Operator.OpType optype;
5950 public readonly FullNamedExpression ret_type;
5951 public readonly Location location;
5953 public OperatorDeclaration (Operator.OpType op, FullNamedExpression ret_type, Location location)
5956 this.ret_type = ret_type;
5957 this.location = location;
5961 void Error_ExpectingTypeName (Expression expr)
5963 if (expr is Invocation){
5964 Report.Error (1002, expr.Location, "Expecting `;'");
5966 Expression.Error_InvalidExpressionStatement (Report, expr.Location);
5970 void Error_ParameterModifierNotValid (string modifier, Location loc)
5972 Report.Error (631, loc, "The parameter modifier `{0}' is not valid in this context",
5976 void Error_DuplicateParameterModifier (Location loc, Parameter.Modifier mod)
5978 Report.Error (1107, loc, "Duplicate parameter modifier `{0}'",
5979 Parameter.GetModifierSignature (mod));
5982 void Error_TypeExpected (Location loc)
5984 Report.Error (1031, loc, "Type expected");
5987 void Error_NamedArgumentExpected (NamedArgument a)
5989 Report.Error (1738, a.Name.Location, "Named arguments must appear after the positional arguments");
5992 void push_current_class (TypeContainer tc, object partial_token)
5994 if (RootContext.EvalMode){
5995 tc.ModFlags = (tc.ModFlags & ~(Modifiers.PRIVATE|Modifiers.INTERNAL)) | Modifiers.PUBLIC;
5996 undo.AddTypeContainer (current_container, tc);
5999 if (partial_token != null)
6000 current_container = current_container.AddPartial (tc);
6002 current_container = current_container.AddTypeContainer (tc);
6004 ++lexer.parsing_declaration;
6008 DeclSpace pop_current_class ()
6010 DeclSpace retval = current_class;
6012 current_class = current_class.Parent;
6013 current_container = current_class.PartialContainer;
6019 // Given the @class_name name, it creates a fully qualified name
6020 // based on the containing declaration space
6023 MakeName (MemberName class_name)
6025 Namespace ns = current_namespace.NS;
6027 if (current_container == RootContext.ToplevelTypes) {
6028 if (ns.Name.Length != 0)
6029 return new MemberName (ns.MemberName, class_name);
6033 return new MemberName (current_container.MemberName, class_name);
6037 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
6039 Block implicit_block;
6040 ArrayList inits = null;
6043 // If we are doing interactive editing, we want variable declarations
6044 // that are in the top block to be added instead to the class as
6047 if (RootContext.StatementMode){
6050 for (Block b = current_block; b != null; b = b.Parent){
6051 if (b is ExplicitBlock && !(b is ToplevelBlock)){
6052 // There has been an explicit block, we cant add to the class
6059 // We can use "current_block" since we know there are no explicit blocks
6061 foreach (VariableDeclaration decl in variable_declarators){
6062 // We can not use the super-handy f.Initializer, because
6063 // multiple lines would force code to be executed out of sync
6064 if (decl.expression_or_array_initializer != null){
6065 string id = "$" + decl.identifier;
6066 LocalInfo vi = current_block.AddVariable (type, id, decl.Location);
6068 // Avoid warning about this variable not being used.
6071 LocalVariableReference var;
6072 var = new LocalVariableReferenceWithClassSideEffect (current_container, decl.identifier, current_block, id, vi, decl.Location);
6073 Assign assign = new SimpleAssign (var, decl.expression_or_array_initializer, decl.Location);
6074 current_block.AddStatement (new StatementExpression (assign));
6075 assign = new SimpleAssign (new SimpleName (decl.identifier, decl.Location), var);
6076 current_block.AddStatement (new StatementExpression (assign));
6078 Field f = new Field (current_container, (FullNamedExpression) type, Modifiers.PUBLIC | Modifiers.STATIC,
6079 new MemberName (decl.identifier, loc), null);
6080 current_container.AddField (f);
6082 // Register the field to be visible later as a global variable
6083 Evaluator.QueueField (f);
6087 return current_block;
6092 // We use the `Used' property to check whether statements
6093 // have been added to the current block. If so, we need
6094 // to create another block to contain the new declaration
6095 // otherwise, as an optimization, we use the same block to
6096 // add the declaration.
6098 // FIXME: A further optimization is to check if the statements
6099 // that were added were added as part of the initialization
6100 // below. In which case, no other statements have been executed
6101 // and we might be able to reduce the number of blocks for
6102 // situations like this:
6104 // int j = 1; int k = j + 1;
6106 if (current_block.Used)
6107 implicit_block = new Block (current_block, loc, lexer.Location);
6109 implicit_block = current_block;
6111 foreach (VariableDeclaration decl in variable_declarators){
6113 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
6114 if (decl.expression_or_array_initializer != null){
6116 inits = new ArrayList (4);
6123 return implicit_block;
6125 foreach (VariableDeclaration decl in inits){
6127 Expression expr = decl.expression_or_array_initializer;
6129 LocalVariableReference var;
6130 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
6132 assign = new SimpleAssign (var, expr, decl.Location);
6134 implicit_block.AddStatement (new StatementExpression (assign));
6137 return implicit_block;
6140 Block declare_local_constants (Expression type, ArrayList declarators)
6142 Block implicit_block;
6144 if (current_block.Used)
6145 implicit_block = new Block (current_block);
6147 implicit_block = current_block;
6149 foreach (VariableDeclaration decl in declarators){
6150 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
6153 return implicit_block;
6156 string CheckAttributeTarget (string a, Location l)
6159 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
6163 Report.Warning (658, 1, l,
6164 "`{0}' is invalid attribute target. All attributes in this attribute section will be ignored", a);
6165 return string.Empty;
6168 static bool IsUnaryOperator (Operator.OpType op)
6172 case Operator.OpType.LogicalNot:
6173 case Operator.OpType.OnesComplement:
6174 case Operator.OpType.Increment:
6175 case Operator.OpType.Decrement:
6176 case Operator.OpType.True:
6177 case Operator.OpType.False:
6178 case Operator.OpType.UnaryPlus:
6179 case Operator.OpType.UnaryNegation:
6185 void syntax_error (Location l, string msg)
6187 Report.Error (1003, l, "Syntax error, " + msg);
6192 public Tokenizer Lexer {
6198 static CSharpParser ()
6200 oob_stack = new Stack ();
6203 public CSharpParser (SeekableStreamReader reader, CompilationUnit file, CompilerContext ctx)
6205 if (RootContext.EvalMode)
6209 this.compiler = ctx;
6210 current_namespace = new NamespaceEntry (null, file, null);
6211 current_class = current_namespace.SlaveDeclSpace;
6212 current_container = current_class.PartialContainer; // == RootContest.ToplevelTypes
6214 lexer = new Tokenizer (reader, file, ctx);
6217 public void parse ()
6219 eof_token = Token.EOF;
6222 if (yacc_verbose_flag > 1)
6223 yyparse (lexer, new yydebug.yyDebugSimple ());
6227 Tokenizer tokenizer = lexer as Tokenizer;
6228 tokenizer.cleanup ();
6229 } catch (Exception e){
6230 if (e is yyParser.yyUnexpectedEof)
6231 UnexpectedEOF = true;
6233 if (e is yyParser.yyException)
6234 Report.Error (-25, lexer.Location, "Parsing error");
6235 else if (yacc_verbose_flag > 0)
6236 throw; // Used by compiler-tester to test internal errors
6238 Report.Error (589, lexer.Location, "Internal compiler error during parsing");
6241 if (RootContext.ToplevelTypes.NamespaceEntry != null)
6242 throw new InternalErrorException ("who set it?");
6245 void CheckToken (int error, int yyToken, string msg, Location loc)
6247 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
6248 Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, GetTokenName (yyToken));
6250 Report.Error (error, loc, msg);
6253 void CheckIdentifierToken (int yyToken, Location loc)
6255 CheckToken (1041, yyToken, "Identifier expected", loc);
6258 string ConsumeStoredComment ()
6260 string s = tmpComment;
6262 Lexer.doc_state = XmlCommentState.Allowed;
6266 Location GetLocation (object obj)
6268 if (obj is MemberCore)
6269 return ((MemberCore) obj).Location;
6270 if (obj is MemberName)
6271 return ((MemberName) obj).Location;
6272 if (obj is LocatedToken)
6273 return ((LocatedToken) obj).Location;
6274 if (obj is Location)
6275 return (Location) obj;
6276 return lexer.Location;
6280 get { return compiler.Report; }
6283 void start_block (Location loc)
6285 if (current_block == null || parsing_anonymous_method) {
6286 current_block = new ToplevelBlock (compiler, current_block, current_local_parameters, current_generic_method, loc);
6287 parsing_anonymous_method = false;
6289 current_block = new ExplicitBlock (current_block, loc, Location.Null);
6294 end_block (Location loc)
6296 Block retval = current_block.Explicit;
6297 retval.SetEndLocation (loc);
6298 current_block = retval.Parent;
6303 start_anonymous (bool lambda, ParametersCompiled parameters, Location loc)
6305 if (RootContext.Version == LanguageVersion.ISO_1){
6306 Report.FeatureIsNotAvailable (loc, "anonymous methods");
6309 oob_stack.Push (current_anonymous_method);
6310 oob_stack.Push (current_local_parameters);
6312 current_local_parameters = parameters;
6314 current_anonymous_method = lambda
6315 ? new LambdaExpression (loc)
6316 : new AnonymousMethodExpression (loc);
6318 // Force the next block to be created as a ToplevelBlock
6319 parsing_anonymous_method = true;
6323 * Completes the anonymous method processing, if lambda_expr is null, this
6324 * means that we have a Statement instead of an Expression embedded
6326 AnonymousMethodExpression end_anonymous (ToplevelBlock anon_block)
6328 AnonymousMethodExpression retval;
6330 current_anonymous_method.Block = anon_block;
6331 retval = current_anonymous_method;
6333 current_local_parameters = (ParametersCompiled) oob_stack.Pop ();
6334 current_anonymous_method = (AnonymousMethodExpression) oob_stack.Pop ();
6339 public NamespaceEntry CurrentNamespace {
6341 return current_namespace;
6346 void Error_SyntaxError (int token)
6348 Error_SyntaxError (0, token, "Unexpected symbol");
6351 void Error_SyntaxError (int error_code, int token, string msg)
6353 string symbol = GetSymbolName (token);
6354 string expecting = GetExpecting ();
6356 if (error_code == 0) {
6357 if (expecting == "`)'")
6363 if (expecting != null)
6364 Report.Error (error_code, lexer.Location, "{2} `{0}', expecting {1}",
6365 symbol, expecting, msg);
6367 Report.Error (error_code, lexer.Location, "{1} `{0}'", symbol, msg);
6370 string GetExpecting ()
6372 int [] tokens = yyExpectingTokens (yyExpectingState);
6373 ArrayList names = new ArrayList (tokens.Length);
6374 bool has_type = false;
6375 bool has_identifier = false;
6376 for (int i = 0; i < tokens.Length; i++){
6377 int token = tokens [i];
6378 has_identifier |= token == Token.IDENTIFIER;
6380 string name = GetTokenName (token);
6381 if (name == "<internal>")
6384 has_type |= name == "type";
6385 if (names.Contains (name))
6392 // Too many tokens to enumerate
6394 if (names.Count > 8)
6397 if (has_type && has_identifier)
6398 names.Remove ("identifier");
6400 if (names.Count == 1)
6401 return "`" + GetTokenName (tokens [0]) + "'";
6403 StringBuilder sb = new StringBuilder ();
6405 int count = names.Count;
6406 for (int i = 0; i < count; i++){
6407 bool last = i + 1 == count;
6411 sb.Append (names [i]);
6412 sb.Append (last ? "'" : count < 3 ? "' " : "', ");
6414 return sb.ToString ();
6418 string GetSymbolName (int token)
6421 case Token.LITERAL_FLOAT:
6422 case Token.LITERAL_INTEGER:
6423 case Token.LITERAL_DOUBLE:
6424 case Token.LITERAL_DECIMAL:
6425 case Token.LITERAL_CHARACTER:
6426 case Token.LITERAL_STRING:
6427 return lexer.Value.ToString ();
6428 case Token.IDENTIFIER:
6429 return ((LocatedToken)lexer.Value).Value;
6471 case Token.BITWISE_AND:
6473 case Token.BITWISE_OR:
6487 case Token.OP_SHIFT_LEFT:
6489 case Token.OP_SHIFT_RIGHT:
6509 case Token.OP_COALESCING:
6511 case Token.OP_MULT_ASSIGN:
6513 case Token.OP_DIV_ASSIGN:
6515 case Token.OP_MOD_ASSIGN:
6517 case Token.OP_ADD_ASSIGN:
6519 case Token.OP_SUB_ASSIGN:
6521 case Token.OP_SHIFT_LEFT_ASSIGN:
6523 case Token.OP_SHIFT_RIGHT_ASSIGN:
6525 case Token.OP_AND_ASSIGN:
6527 case Token.OP_XOR_ASSIGN:
6529 case Token.OP_OR_ASSIGN:
6533 return GetTokenName (token);
6536 static string GetTokenName (int token)
6539 case Token.ABSTRACT:
6559 case Token.CONTINUE:
6563 case Token.DELEGATE:
6573 case Token.EXPLICIT:
6591 case Token.IMPLICIT:
6595 case Token.INTERFACE:
6597 case Token.INTERNAL:
6603 case Token.NAMESPACE:
6609 case Token.OPERATOR:
6613 case Token.OVERRIDE:
6619 case Token.PROTECTED:
6623 case Token.READONLY:
6635 case Token.STACKALLOC:
6636 return "stackalloc";
6653 case Token.UNCHECKED:
6661 case Token.VOLATILE:
6674 case Token.FROM_FIRST:
6692 case Token.ASCENDING:
6694 case Token.DESCENDING:
6695 return "descending";
6702 case Token.OPEN_BRACE:
6704 case Token.CLOSE_BRACE:
6706 case Token.OPEN_BRACKET:
6708 case Token.CLOSE_BRACKET:
6710 case Token.OPEN_PARENS_CAST:
6711 case Token.OPEN_PARENS_LAMBDA:
6712 case Token.OPEN_PARENS:
6714 case Token.CLOSE_PARENS:
6720 case Token.DEFAULT_COLON:
6724 case Token.SEMICOLON:
6735 case Token.BITWISE_AND:
6736 case Token.BITWISE_OR:
6743 case Token.OP_SHIFT_LEFT:
6744 case Token.OP_SHIFT_RIGHT:
6752 case Token.OP_COALESCING:
6753 case Token.OP_MULT_ASSIGN:
6754 case Token.OP_DIV_ASSIGN:
6755 case Token.OP_MOD_ASSIGN:
6756 case Token.OP_ADD_ASSIGN:
6757 case Token.OP_SUB_ASSIGN:
6758 case Token.OP_SHIFT_LEFT_ASSIGN:
6759 case Token.OP_SHIFT_RIGHT_ASSIGN:
6760 case Token.OP_AND_ASSIGN:
6761 case Token.OP_XOR_ASSIGN:
6762 case Token.OP_OR_ASSIGN:
6763 return "<operator>";
6785 case Token.OP_GENERICS_LT:
6786 case Token.GENERIC_DIMENSION:
6788 case Token.OP_GENERICS_GT:
6791 case Token.INTERR_NULLABLE:
6793 case Token.DOUBLE_COLON:
6795 case Token.LITERAL_FLOAT:
6796 case Token.LITERAL_INTEGER:
6797 case Token.LITERAL_DOUBLE:
6798 case Token.LITERAL_DECIMAL:
6799 case Token.LITERAL_CHARACTER:
6800 case Token.LITERAL_STRING:
6802 case Token.IDENTIFIER:
6803 return "identifier";
6805 // All of these are internal.
6808 case Token.FIRST_KEYWORD:
6810 case Token.EVAL_COMPILATION_UNIT_PARSER:
6811 case Token.EVAL_USING_DECLARATIONS_UNIT_PARSER:
6812 case Token.EVAL_STATEMENT_PARSER:
6813 case Token.LAST_KEYWORD:
6814 case Token.GENERATE_COMPLETION:
6815 case Token.COMPLETE_COMPLETION:
6816 return "<internal>";
6818 // A bit more robust.
6820 return yyNames [token];