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;
158 // Instead of allocating carrier array everytime we
159 // share the bucket for very common constructs which can never
162 static ArrayList parameters_bucket = new ArrayList (6);
163 static ArrayList variables_bucket = new ArrayList (6);
167 %token NONE /* This token is never returned by our lexer */
168 %token ERROR // This is used not by the parser, but by the tokenizer.
172 *These are the C# keywords
271 %token INTERR_NULLABLE
274 /* Generics <,> tokens */
275 %token OP_GENERICS_LT
276 %token OP_GENERICS_LT_DECL
277 %token OP_GENERICS_GT
279 /* C# keywords which are not really keywords */
285 /* C# single character operators/punctuation. */
313 /* C# multi-character operators. */
318 %token OP_SHIFT_RIGHT
325 %token OP_MULT_ASSIGN
330 %token OP_SHIFT_LEFT_ASSIGN
331 %token OP_SHIFT_RIGHT_ASSIGN
341 %token OPEN_PARENS_LAMBDA
342 %token OPEN_PARENS_CAST
343 %token GENERIC_DIMENSION
346 // Make the parser go into eval mode parsing (statements and compilation units).
347 %token EVAL_STATEMENT_PARSER
348 %token EVAL_COMPILATION_UNIT_PARSER
349 %token EVAL_USING_DECLARATIONS_UNIT_PARSER
352 // This token is generated to trigger the completion engine at this point
354 %token GENERATE_COMPLETION
357 // This token is return repeatedly after the first GENERATE_COMPLETION
358 // token is produced and before the final EOF
360 %token COMPLETE_COMPLETION
362 /* Add precedence rules to solve dangling else s/r conflict */
366 /* Define the operator tokens and their precedences */
374 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
376 %left STAR DIV PERCENT
377 %right BANG CARRET UMINUS
378 %nonassoc OP_INC OP_DEC
380 %left OPEN_BRACKET OPEN_BRACE
383 %start compilation_unit
387 : outer_declarations opt_EOF
388 | outer_declarations global_attributes opt_EOF
389 | global_attributes opt_EOF
390 | opt_EOF /* allow empty files */
391 | interactive_parsing { Lexer.CompleteOnEOF = false; } opt_EOF
397 Lexer.check_incorrect_doc_comment ();
401 Lexer.check_incorrect_doc_comment ();
407 | outer_declarations outer_declaration
411 : extern_alias_directive
413 | namespace_member_declaration
416 extern_alias_directives
417 : extern_alias_directive
418 | extern_alias_directives extern_alias_directive
421 extern_alias_directive
422 : EXTERN_ALIAS IDENTIFIER IDENTIFIER SEMICOLON
424 var lt = (Tokenizer.LocatedToken) $2;
427 syntax_error (lt.Location, "`alias' expected");
428 } else if (RootContext.Version == LanguageVersion.ISO_1) {
429 Report.FeatureIsNotAvailable (lt.Location, "external alias");
431 lt = (Tokenizer.LocatedToken) $3;
432 current_namespace.AddUsingExternalAlias (lt.Value, lt.Location, Report);
437 syntax_error (GetLocation ($1), "`alias' expected"); // TODO: better
443 | using_directives using_directive
447 : using_alias_directive
449 if (RootContext.Documentation != null)
450 Lexer.doc_state = XmlCommentState.Allowed;
452 | using_namespace_directive
454 if (RootContext.Documentation != null)
455 Lexer.doc_state = XmlCommentState.Allowed;
459 using_alias_directive
460 : USING IDENTIFIER ASSIGN namespace_or_type_name SEMICOLON
462 var lt = (Tokenizer.LocatedToken) $2;
463 current_namespace.AddUsingAlias (lt.Value, (MemberName) $4, GetLocation ($1));
466 CheckIdentifierToken (yyToken, GetLocation ($2));
471 using_namespace_directive
472 : USING namespace_name SEMICOLON
474 current_namespace.AddUsing ((MemberName) $2, GetLocation ($1));
479 // Strictly speaking, namespaces don't have attributes but
480 // we parse global attributes along with namespace declarations and then
483 namespace_declaration
484 : opt_attributes NAMESPACE qualified_identifier
486 MemberName name = (MemberName) $3;
489 Report.Error(1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
492 current_namespace = new NamespaceEntry (
493 current_namespace, file, name.GetName ());
494 current_class = current_namespace.SlaveDeclSpace;
495 current_container = current_class.PartialContainer;
497 namespace_body opt_semicolon
499 current_namespace = current_namespace.Parent;
500 current_class = current_namespace.SlaveDeclSpace;
501 current_container = current_class.PartialContainer;
508 var lt = (Tokenizer.LocatedToken) $1;
509 $$ = new MemberName (lt.Value, lt.Location);
511 | qualified_identifier DOT IDENTIFIER
513 var lt = (Tokenizer.LocatedToken) $3;
514 $$ = new MemberName ((MemberName) $1, lt.Value, lt.Location);
518 syntax_error (lexer.Location, "`.' expected");
519 $$ = new MemberName ("<invalid>", lexer.Location);
534 : namespace_or_type_name
536 MemberName name = (MemberName) $1;
538 if (name.TypeArguments != null)
539 syntax_error (lexer.Location, "namespace name expected");
548 if (RootContext.Documentation != null)
549 Lexer.doc_state = XmlCommentState.Allowed;
555 : opt_extern_alias_directives
557 opt_namespace_member_declarations
561 Report.Error (1518, lexer.Location, "Expected `class', `delegate', `enum', `interface', or `struct'");
564 | opt_extern_alias_directives
566 opt_namespace_member_declarations
569 Report.Error (1513, lexer.Location, "Expected `}'");
578 opt_extern_alias_directives
580 | extern_alias_directives
583 opt_namespace_member_declarations
585 | namespace_member_declarations
588 namespace_member_declarations
589 : namespace_member_declaration
590 | namespace_member_declarations namespace_member_declaration
593 namespace_member_declaration
597 DeclSpace ds = (DeclSpace)$1;
599 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
600 Report.Error (1527, ds.Location,
601 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
604 current_namespace.DeclarationFound = true;
606 | namespace_declaration {
607 current_namespace.DeclarationFound = true;
610 | field_declaration {
611 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
613 | method_declaration {
614 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
621 | interface_declaration
623 | delegate_declaration
625 // Enable this when we have handled all errors, because this acts as a generic fallback
628 // Console.WriteLine ("Token=" + yyToken);
629 // Report.Error (1518, GetLocation ($1), "Expected class, struct, interface, enum or delegate");
641 Attributes attrs = (Attributes)$1;
642 if (global_attrs_enabled) {
643 CodeGen.Assembly.AddAttributes (attrs.Attrs, current_namespace);
645 foreach (Attribute a in attrs.Attrs) {
646 Report.Error (1730, a.Location, "Assembly and module attributes must precede all other elements except using clauses and extern alias declarations");
657 global_attrs_enabled = false;
662 global_attrs_enabled = false;
671 if (current_attr_target != String.Empty) {
672 ArrayList sect = (ArrayList) $1;
674 if (global_attrs_enabled) {
675 if (current_attr_target == "module") {
676 current_container.Module.Compiled.AddAttributes (sect);
678 } else if (current_attr_target != null && current_attr_target.Length > 0) {
679 CodeGen.Assembly.AddAttributes (sect, current_namespace);
682 $$ = new Attributes (sect);
685 if (RootContext.Documentation != null) {
686 Lexer.check_incorrect_doc_comment ();
688 XmlCommentState.Allowed;
692 $$ = new Attributes (sect);
697 current_attr_target = null;
699 | attribute_sections attribute_section
701 if (current_attr_target != String.Empty) {
702 Attributes attrs = $1 as Attributes;
703 ArrayList sect = (ArrayList) $2;
705 if (global_attrs_enabled) {
706 if (current_attr_target == "module") {
707 current_container.Module.Compiled.AddAttributes (sect);
709 } else if (current_attr_target == "assembly") {
710 CodeGen.Assembly.AddAttributes (sect, current_namespace);
714 attrs = new Attributes (sect);
716 attrs.AddAttributes (sect);
720 attrs = new Attributes (sect);
722 attrs.AddAttributes (sect);
728 current_attr_target = null;
733 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
737 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
743 attribute_target_specifier
744 : attribute_target COLON
746 current_attr_target = (string)$1;
754 var lt = (Tokenizer.LocatedToken) $1;
755 $$ = CheckAttributeTarget (lt.Value, lt.Location);
757 | EVENT { $$ = "event"; }
758 | RETURN { $$ = "return"; }
761 string name = GetTokenName (yyToken);
762 $$ = CheckAttributeTarget (name, GetLocation ($1));
769 $$ = new ArrayList (4) { $1 };
771 | attribute_list COMMA attribute
773 ArrayList attrs = (ArrayList) $1;
783 ++lexer.parsing_block;
785 opt_attribute_arguments
787 --lexer.parsing_block;
788 MemberName mname = (MemberName) $1;
789 if (mname.IsGeneric) {
790 Report.Error (404, lexer.Location,
791 "'<' unexpected: attributes cannot be generic");
794 Arguments [] arguments = (Arguments []) $3;
795 ATypeNameExpression expr = mname.GetTypeExpression ();
797 if (current_attr_target == String.Empty)
799 else if (global_attrs_enabled && (current_attr_target == "assembly" || current_attr_target == "module"))
800 // FIXME: supply "nameEscaped" parameter here.
801 $$ = new GlobalAttribute (current_namespace, current_attr_target,
802 expr, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
804 $$ = new Attribute (current_attr_target, expr, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
809 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
812 opt_attribute_arguments
813 : /* empty */ { $$ = null; }
814 | OPEN_PARENS attribute_arguments CLOSE_PARENS
822 : /* empty */ { $$ = null; }
823 | positional_or_named_argument
825 Arguments a = new Arguments (4);
826 a.Add ((Argument) $1);
827 $$ = new Arguments [] { a, null };
829 | named_attribute_argument
831 Arguments a = new Arguments (4);
832 a.Add ((Argument) $1);
833 $$ = new Arguments [] { null, a };
835 | attribute_arguments COMMA positional_or_named_argument
837 Arguments[] o = (Arguments[]) $1;
839 Report.Error (1016, ((Argument) $3).Expr.Location, "Named attribute arguments must appear after the positional arguments");
840 o [0] = new Arguments (4);
843 Arguments args = ((Arguments) o [0]);
844 if (args.Count > 0 && !($3 is NamedArgument) && args [args.Count - 1] is NamedArgument)
845 Error_NamedArgumentExpected ((NamedArgument) args [args.Count - 1]);
847 args.Add ((Argument) $3);
849 | attribute_arguments COMMA named_attribute_argument
851 Arguments[] o = (Arguments[]) $1;
853 o [1] = new Arguments (4);
856 ((Arguments) o [1]).Add ((Argument) $3);
860 positional_or_named_argument
863 $$ = new Argument ((Expression) $1);
868 named_attribute_argument
869 : IDENTIFIER ASSIGN expression
871 var lt = (Tokenizer.LocatedToken) $1;
872 $$ = new NamedArgument (lt.Value, lt.Location, (Expression) $3);
877 : IDENTIFIER COLON expression
879 if (RootContext.Version <= LanguageVersion.V_3)
880 Report.FeatureIsNotAvailable (GetLocation ($1), "named argument");
882 var lt = (Tokenizer.LocatedToken) $1;
883 $$ = new NamedArgument (lt.Value, lt.Location, (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 variables_bucket.Clear ();
1033 variables_bucket.Add ($1);
1034 $$ = variables_bucket;
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 ((Tokenizer.LocatedToken) $1, $4);
1057 // A const field requires a value to be provided
1058 Report.Error (145, GetLocation ($1), "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 ((Tokenizer.LocatedToken) $1, $3);
1151 | IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1153 Report.Error (443, lexer.Location, "Value or constant expected");
1154 $$ = new VariableDeclaration ((Tokenizer.LocatedToken) $1, null);
1159 local_variable_declarators
1160 : local_variable_declarator
1162 variables_bucket.Clear ();
1164 variables_bucket.Add ($1);
1165 $$ = variables_bucket;
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 ((Tokenizer.LocatedToken) $1, $3);
1182 $$ = new VariableDeclaration ((Tokenizer.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, GetLocation ($1));
1199 $$ = new ArglistAccess (GetLocation ($1));
1201 | STACKALLOC simple_type
1203 Report.Error (1575, GetLocation ($1), "A stackalloc expression requires [] after type");
1204 $$ = new StackAlloc ((Expression) $2, null, GetLocation ($1));
1208 variable_declarators
1209 : variable_declarator
1211 variables_bucket.Clear ();
1213 variables_bucket.Add ($1);
1214 $$ = variables_bucket;
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 (compiler, 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 (compiler, 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 (compiler, 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 (compiler, 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 (compiler, pars, true);
1469 | arglist_modifier COMMA error
1471 Report.Error (257, GetLocation ($1), "An __arglist parameter must be the last parameter in a formal parameter list");
1473 $$ = new ParametersCompiled (compiler, new Parameter [] { new ArglistParameter (GetLocation ($1)) }, true);
1475 | fixed_parameters COMMA ARGLIST COMMA error
1477 Report.Error (257, GetLocation ($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 (compiler, pars, true);
1489 $$ = new ParametersCompiled (compiler, new Parameter[] { (Parameter) $1 } );
1493 $$ = new ParametersCompiled (compiler, new Parameter [] { new ArglistParameter (GetLocation ($1)) }, true);
1500 parameters_bucket.Clear ();
1501 Parameter p = (Parameter) $1;
1502 parameters_bucket.Add (p);
1504 default_parameter_used = p.HasDefaultValue;
1505 $$ = parameters_bucket;
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 var lt = (Tokenizer.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 var lt = (Tokenizer.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 var lt = (Tokenizer.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", GetLocation ($1));
1631 $$ = Parameter.Modifier.REF;
1635 if ((valid_param_mod & ParameterModifierType.Out) == 0)
1636 Error_ParameterModifierNotValid ("out", GetLocation ($1));
1638 $$ = Parameter.Modifier.OUT;
1642 if ((valid_param_mod & ParameterModifierType.This) == 0)
1643 Error_ParameterModifierNotValid ("this", GetLocation ($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 var lt = (Tokenizer.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 var lt = (Tokenizer.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, (GetLocation ($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, GetLocation ($1), "The parameter modifiers `this' and `params' cannot be used altogether");
1683 Report.Error (1611, GetLocation ($1), "The params parameter cannot be declared as ref or out");
1686 | PARAMS params_modifier
1688 Error_DuplicateParameterModifier (GetLocation ($1), Parameter.Modifier.PARAMS);
1695 if ((valid_param_mod & ParameterModifierType.Arglist) == 0)
1696 Report.Error (1669, GetLocation ($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, GetLocation ($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, GetLocation ($3));
1830 if (!parsing_indexer) {
1831 current_local_parameters = new ParametersCompiled (compiler, new Parameter [] { implicit_value_parameter });
1833 current_local_parameters = ParametersCompiled.MergeGenerated (compiler,
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, GetLocation ($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, GetLocation ($1), "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 = GetLocation ($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 = GetLocation ($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 = GetLocation ($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 var lt = (Tokenizer.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, GetLocation ($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, GetLocation ($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 var lt = (Tokenizer.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 (compiler,
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, GetLocation ($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, GetLocation ($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, GetLocation ($2));
2381 lexer.EventParsing = true;
2384 | opt_attributes ADD error {
2385 Report.Error (73, GetLocation ($2), "An add or remove accessor must have a body");
2388 | opt_attributes modifiers ADD {
2389 Report.Error (1609, GetLocation ($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, GetLocation ($2));
2402 lexer.EventParsing = true;
2404 | opt_attributes REMOVE error {
2405 Report.Error (73, GetLocation ($2), "An add or remove accessor must have a body");
2408 | opt_attributes modifiers REMOVE {
2409 Report.Error (1609, GetLocation ($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 (Tokenizer.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 (Tokenizer.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 (GetLocation ($1), "nullable types");
2676 else if (RootContext.Version < LanguageVersion.ISO_2)
2677 Report.FeatureIsNotAvailable (GetLocation ($1), "nullable types");
2683 namespace_or_type_name
2685 | qualified_alias_member IDENTIFIER opt_type_argument_list
2687 var lt1 = (Tokenizer.LocatedToken) $1;
2688 var lt2 = (Tokenizer.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 var lt = (Tokenizer.LocatedToken) $3;
2699 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $4, lt.Location);
2704 : IDENTIFIER opt_type_argument_list
2706 var lt = (Tokenizer.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 (GetLocation ($1), "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 var lt = (Tokenizer.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 var lt = (Tokenizer.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 var lt = (Tokenizer.LocatedToken) $1;
2800 $$ = new MemberName (lt.Value, (TypeArguments) $2, lt.Location);
2802 | qualified_alias_member IDENTIFIER opt_type_argument_list DOT
2804 var lt1 = (Tokenizer.LocatedToken) $1;
2805 var lt2 = (Tokenizer.LocatedToken) $2;
2807 $$ = new MemberName (lt1.Value, lt2.Value, (TypeArguments) $3, lt1.Location);
2809 | explicit_interface IDENTIFIER opt_type_argument_list DOT
2811 var lt = (Tokenizer.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 (GetLocation ($1), "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 var lt = (Tokenizer.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 (GetLocation ($1), Report);
2887 $$ = TypeManager.system_void_expr;
2895 Expression.Error_VoidInvalidInTheContext (GetLocation ($1), Report);
2896 $$ = TypeManager.system_void_expr;
2901 : type_expression_or_array
2904 Report.Error (1536, GetLocation ($1), "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, "*", GetLocation ($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 var lt = (Tokenizer.LocatedToken) $1;
3029 $$ = new SimpleName (MemberName.MakeName (lt.Value, (TypeArguments)$2), (TypeArguments)$2, lt.Location);
3031 | IDENTIFIER GENERATE_COMPLETION {
3032 var lt = (Tokenizer.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
3057 | NULL { $$ = new NullLiteral (GetLocation ($1)); }
3061 : TRUE { $$ = new BoolLiteral (true, GetLocation ($1)); }
3062 | FALSE { $$ = new BoolLiteral (false, GetLocation ($1)); }
3067 // Here is the trick, tokenizer may think that parens is a special but
3068 // parser is interested in open parens only, so we merge them.
3069 // Consider: if (a)foo ();
3074 | OPEN_PARENS_LAMBDA
3077 parenthesized_expression
3078 : OPEN_PARENS expression CLOSE_PARENS
3080 $$ = new ParenthesizedExpression ((Expression) $2);
3082 | OPEN_PARENS expression COMPLETE_COMPLETION
3084 $$ = new ParenthesizedExpression ((Expression) $2);
3089 : primary_expression DOT IDENTIFIER opt_type_argument_list
3091 var lt = (Tokenizer.LocatedToken) $3;
3092 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
3094 | predefined_type DOT IDENTIFIER opt_type_argument_list
3096 var lt = (Tokenizer.LocatedToken) $3;
3097 // TODO: Location is wrong as some predefined types doesn't hold a location
3098 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
3100 | qualified_alias_member IDENTIFIER opt_type_argument_list
3102 var lt1 = (Tokenizer.LocatedToken) $1;
3103 var lt2 = (Tokenizer.LocatedToken) $2;
3105 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, (TypeArguments) $3, lt1.Location);
3107 | primary_expression DOT GENERATE_COMPLETION {
3108 $$ = new CompletionMemberAccess ((Expression) $1, null,GetLocation ($3));
3110 | primary_expression DOT IDENTIFIER GENERATE_COMPLETION {
3111 var lt = (Tokenizer.LocatedToken) $3;
3112 $$ = new CompletionMemberAccess ((Expression) $1, lt.Value, lt.Location);
3114 | predefined_type DOT GENERATE_COMPLETION
3116 // TODO: Location is wrong as some predefined types doesn't hold a location
3117 $$ = new CompletionMemberAccess ((Expression) $1, null, lexer.Location);
3119 | predefined_type DOT IDENTIFIER GENERATE_COMPLETION {
3120 var lt = (Tokenizer.LocatedToken) $3;
3121 $$ = new CompletionMemberAccess ((Expression) $1, lt.Value, lt.Location);
3125 invocation_expression
3126 : primary_expression open_parens_any opt_argument_list CLOSE_PARENS
3128 $$ = new Invocation ((Expression) $1, (Arguments) $3);
3132 opt_object_or_collection_initializer
3133 : /* empty */ { $$ = null; }
3134 | object_or_collection_initializer
3137 object_or_collection_initializer
3138 : OPEN_BRACE opt_member_initializer_list close_brace_or_complete_completion
3141 $$ = CollectionOrObjectInitializers.Empty;
3143 $$ = new CollectionOrObjectInitializers ((ArrayList) $2, GetLocation ($1));
3145 | OPEN_BRACE member_initializer_list COMMA CLOSE_BRACE
3147 $$ = new CollectionOrObjectInitializers ((ArrayList) $2, GetLocation ($1));
3151 opt_member_initializer_list
3152 : /* empty */ { $$ = null; }
3153 | member_initializer_list
3159 member_initializer_list
3160 : member_initializer
3162 ArrayList a = new ArrayList ();
3166 | member_initializer_list COMMA member_initializer
3168 ArrayList a = (ArrayList)$1;
3175 : IDENTIFIER ASSIGN initializer_value
3177 var lt = (Tokenizer.LocatedToken) $1;
3178 $$ = new ElementInitializer (lt.Value, (Expression)$3, lt.Location);
3180 | GENERATE_COMPLETION
3182 $$ = new CompletionElementInitializer (null, GetLocation ($1));
3184 | non_assignment_expression opt_COMPLETE_COMPLETION {
3185 CompletionSimpleName csn = $1 as CompletionSimpleName;
3187 $$ = new CollectionElementInitializer ((Expression)$1);
3189 $$ = new CompletionElementInitializer (csn.Prefix, csn.Location);
3191 | OPEN_BRACE expression_list CLOSE_BRACE
3193 $$ = new CollectionElementInitializer ((ArrayList)$2, GetLocation ($1));
3195 | OPEN_BRACE CLOSE_BRACE
3197 Report.Error (1920, GetLocation ($1), "An element initializer cannot be empty");
3203 | object_or_collection_initializer
3207 : /* empty */ { $$ = null; }
3212 : argument_or_named_argument
3214 Arguments list = new Arguments (4);
3215 list.Add ((Argument) $1);
3218 | argument_list COMMA argument
3220 Arguments list = (Arguments) $1;
3221 if (list [list.Count - 1] is NamedArgument)
3222 Error_NamedArgumentExpected ((NamedArgument) list [list.Count - 1]);
3224 list.Add ((Argument) $3);
3227 | argument_list COMMA named_argument
3229 Arguments list = (Arguments) $1;
3230 NamedArgument a = (NamedArgument) $3;
3231 for (int i = 0; i < list.Count; ++i) {
3232 NamedArgument na = list [i] as NamedArgument;
3233 if (na != null && na.Name == a.Name)
3234 Report.Error (1740, na.Location, "Named argument `{0}' specified multiple times",
3241 | argument_list COMMA
3243 Report.Error (839, GetLocation ($2), "An argument is missing");
3246 | COMMA argument_or_named_argument
3248 Report.Error (839, GetLocation ($1), "An argument is missing");
3256 $$ = new Argument ((Expression) $1);
3258 | non_simple_argument
3261 argument_or_named_argument
3267 : REF variable_reference
3269 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
3271 | OUT variable_reference
3273 $$ = new Argument ((Expression) $2, Argument.AType.Out);
3275 | ARGLIST open_parens_any argument_list CLOSE_PARENS
3277 $$ = new Argument (new Arglist ((Arguments) $3, GetLocation ($1)));
3279 | ARGLIST open_parens_any CLOSE_PARENS
3281 $$ = new Argument (new Arglist (GetLocation ($1)));
3285 $$ = new Argument (new ArglistAccess (GetLocation ($1)));
3294 : primary_expression_no_array_creation OPEN_BRACKET expression_list_arguments CLOSE_BRACKET
3296 $$ = new ElementAccess ((Expression) $1, (Arguments) $3);
3298 | array_creation_expression OPEN_BRACKET expression_list_arguments CLOSE_BRACKET
3300 // LAMESPEC: Not allowed according to specification
3301 $$ = new ElementAccess ((Expression) $1, (Arguments) $3);
3303 | primary_expression_no_array_creation rank_specifiers
3305 // So the super-trick is that primary_expression
3306 // can only be either a SimpleName or a MemberAccess.
3307 // The MemberAccess case arises when you have a fully qualified type-name like :
3309 // SimpleName is when you have
3312 Expression expr = (Expression) $1;
3313 if (expr is ComposedCast){
3314 $$ = new ComposedCast ((ComposedCast)expr, (string) $2);
3315 } else if (expr is ATypeNameExpression){
3317 // So we extract the string corresponding to the SimpleName
3320 $$ = new ComposedCast ((ATypeNameExpression)expr, (string) $2);
3322 Error_ExpectingTypeName (expr);
3323 $$ = TypeManager.system_object_expr;
3326 current_array_type = (FullNamedExpression)$$;
3333 ArrayList list = new ArrayList (4);
3337 | expression_list COMMA expression
3339 ArrayList list = (ArrayList) $1;
3345 expression_list_arguments
3346 : expression_list_argument
3348 Arguments args = new Arguments (4);
3349 args.Add ((Argument) $1);
3352 | expression_list_arguments COMMA expression_list_argument
3354 Arguments args = (Arguments) $1;
3355 args.Add ((Argument) $3);
3360 expression_list_argument
3363 $$ = new Argument ((Expression) $1);
3371 $$ = new This (current_block, GetLocation ($1));
3376 : BASE DOT IDENTIFIER opt_type_argument_list
3378 var lt = (Tokenizer.LocatedToken) $3;
3379 $$ = new BaseAccess (lt.Value, (TypeArguments) $4, lt.Location);
3381 | BASE OPEN_BRACKET expression_list_arguments CLOSE_BRACKET
3383 $$ = new BaseIndexerAccess ((Arguments) $3, GetLocation ($1));
3387 Error_SyntaxError (yyToken);
3388 $$ = new BaseAccess (null, GetLocation ($2));
3392 post_increment_expression
3393 : primary_expression OP_INC
3395 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement, (Expression) $1);
3399 post_decrement_expression
3400 : primary_expression OP_DEC
3402 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement, (Expression) $1);
3406 object_or_delegate_creation_expression
3407 : new_expr_start open_parens_any opt_argument_list CLOSE_PARENS opt_object_or_collection_initializer
3410 if (RootContext.Version <= LanguageVersion.ISO_2)
3411 Report.FeatureIsNotAvailable (GetLocation ($1), "object initializers");
3413 $$ = new NewInitialize ((Expression) $1, (Arguments) $3, (CollectionOrObjectInitializers) $5, GetLocation ($1));
3416 $$ = new New ((Expression) $1, (Arguments) $3, GetLocation ($1));
3418 | new_expr_start object_or_collection_initializer
3420 if (RootContext.Version <= LanguageVersion.ISO_2)
3421 Report.FeatureIsNotAvailable (GetLocation ($1), "collection initializers");
3423 $$ = new NewInitialize ((Expression) $1, null, (CollectionOrObjectInitializers) $2, GetLocation ($1));
3427 array_creation_expression
3428 : new_expr_start OPEN_BRACKET expression_list CLOSE_BRACKET
3429 opt_rank_specifier // shift/reduce on OPEN_BRACE
3430 opt_array_initializer
3432 $$ = new ArrayCreation ((FullNamedExpression) $1, (ArrayList) $3, (string) $5, (ArrayList) $6, GetLocation ($1));
3434 | new_expr_start rank_specifiers opt_array_initializer
3437 Report.Error (1586, GetLocation ($1), "Array creation must have array size or array initializer");
3439 $$ = new ArrayCreation ((FullNamedExpression) $1, (string) $2, (ArrayList) $3, GetLocation ($1));
3441 | NEW rank_specifiers array_initializer
3443 if (RootContext.Version <= LanguageVersion.ISO_2)
3444 Report.FeatureIsNotAvailable (GetLocation ($1), "implicitly typed arrays");
3446 $$ = new ImplicitlyTypedArrayCreation ((string) $2, (ArrayList) $3, GetLocation ($1));
3448 | new_expr_start error
3450 Report.Error (1526, GetLocation ($1), "A new expression requires () or [] after type");
3451 $$ = new ArrayCreation ((FullNamedExpression) $1, "[]", null, GetLocation ($1));
3458 ++lexer.parsing_type;
3462 --lexer.parsing_type;
3467 anonymous_type_expression
3468 : NEW OPEN_BRACE anonymous_type_parameters_opt_comma CLOSE_BRACE
3470 if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)
3471 Report.FeatureIsNotSupported (GetLocation ($1), "anonymous types");
3472 else if (RootContext.Version <= LanguageVersion.ISO_2)
3473 Report.FeatureIsNotAvailable (GetLocation ($1), "anonymous types");
3475 $$ = new NewAnonymousType ((ArrayList) $3, current_container, GetLocation ($1));
3479 anonymous_type_parameters_opt_comma
3480 : anonymous_type_parameters_opt
3481 | anonymous_type_parameters COMMA
3484 anonymous_type_parameters_opt
3486 | anonymous_type_parameters
3489 anonymous_type_parameters
3490 : anonymous_type_parameter
3492 ArrayList a = new ArrayList (4);
3496 | anonymous_type_parameters COMMA anonymous_type_parameter
3498 ArrayList a = (ArrayList) $1;
3504 anonymous_type_parameter
3505 : IDENTIFIER ASSIGN variable_initializer
3507 var lt = (Tokenizer.LocatedToken)$1;
3508 $$ = new AnonymousTypeParameter ((Expression)$3, lt.Value, lt.Location);
3512 var lt = (Tokenizer.LocatedToken)$1;
3513 $$ = new AnonymousTypeParameter (new SimpleName (lt.Value, lt.Location),
3514 lt.Value, lt.Location);
3516 | BASE DOT IDENTIFIER opt_type_argument_list
3518 var lt = (Tokenizer.LocatedToken) $3;
3519 BaseAccess ba = new BaseAccess (lt.Value, (TypeArguments) $4, lt.Location);
3520 $$ = new AnonymousTypeParameter (ba, lt.Value, lt.Location);
3524 MemberAccess ma = (MemberAccess) $1;
3525 $$ = new AnonymousTypeParameter (ma, ma.Name, ma.Location);
3529 Report.Error (746, lexer.Location, "Invalid anonymous type member declarator. " +
3530 "Anonymous type members must be a member assignment, simple name or member access expression");
3545 opt_rank_specifier_or_nullable
3553 | opt_nullable rank_specifiers
3556 $$ = "?" + (string) $2;
3564 | rank_specifier rank_specifiers
3566 $$ = (string) $2 + (string) $1;
3571 : OPEN_BRACKET CLOSE_BRACKET
3575 | OPEN_BRACKET dim_separators CLOSE_BRACKET
3577 $$ = "[" + (string) $2 + "]";
3579 | OPEN_BRACKET error
3581 Error_SyntaxError (178, yyToken, "Invalid rank specifier");
3591 | dim_separators COMMA
3593 $$ = (string) $1 + ",";
3597 opt_array_initializer
3609 : OPEN_BRACE CLOSE_BRACE
3611 $$ = new ArrayList (0);
3613 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3615 $$ = (ArrayList) $2;
3619 variable_initializer_list
3620 : variable_initializer
3622 ArrayList list = new ArrayList (4);
3626 | variable_initializer_list COMMA variable_initializer
3628 ArrayList list = (ArrayList) $1;
3634 Error_SyntaxError (yyToken);
3635 $$ = new ArrayList ();
3642 pushed_current_array_type = current_array_type;
3643 lexer.TypeOfParsing = true;
3645 open_parens_any typeof_type_expression CLOSE_PARENS
3647 lexer.TypeOfParsing = false;
3648 Expression type = (Expression)$4;
3649 if (type == TypeManager.system_void_expr)
3650 $$ = new TypeOfVoid (GetLocation ($1));
3652 $$ = new TypeOf (type, GetLocation ($1));
3653 current_array_type = pushed_current_array_type;
3657 typeof_type_expression
3662 Error_TypeExpected (lexer.Location);
3668 : IDENTIFIER generic_dimension
3670 var lt = (Tokenizer.LocatedToken) $1;
3672 $$ = new SimpleName (MemberName.MakeName (lt.Value, (int)$2), lt.Location);
3674 | qualified_alias_member IDENTIFIER generic_dimension
3676 var lt1 = (Tokenizer.LocatedToken) $1;
3677 var lt2 = (Tokenizer.LocatedToken) $2;
3679 $$ = new QualifiedAliasMember (lt1.Value, MemberName.MakeName (lt2.Value, (int) $3), lt1.Location);
3681 | unbound_type_name DOT IDENTIFIER
3683 var lt = (Tokenizer.LocatedToken) $3;
3685 $$ = new MemberAccess ((Expression) $1, lt.Value, lt.Location);
3687 | unbound_type_name DOT IDENTIFIER generic_dimension
3689 var lt = (Tokenizer.LocatedToken) $3;
3691 $$ = new MemberAccess ((Expression) $1, MemberName.MakeName (lt.Value, (int) $4), lt.Location);
3693 | namespace_or_type_name DOT IDENTIFIER generic_dimension
3695 var lt = (Tokenizer.LocatedToken) $3;
3696 MemberName name = (MemberName) $1;
3698 $$ = new MemberAccess (name.GetTypeExpression (), MemberName.MakeName (lt.Value, (int) $4), lt.Location);
3705 if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)
3706 Report.FeatureIsNotSupported (GetLocation ($1), "generics");
3707 else if (RootContext.Version < LanguageVersion.ISO_2)
3708 Report.FeatureIsNotAvailable (GetLocation ($1), "generics");
3714 qualified_alias_member
3715 : IDENTIFIER DOUBLE_COLON
3717 var lt = (Tokenizer.LocatedToken) $1;
3718 if (RootContext.Version == LanguageVersion.ISO_1)
3719 Report.FeatureIsNotAvailable (lt.Location, "namespace alias qualifier");
3726 : SIZEOF open_parens_any type CLOSE_PARENS {
3727 $$ = new SizeOf ((Expression) $3, GetLocation ($1));
3732 : CHECKED open_parens_any expression CLOSE_PARENS
3734 $$ = new CheckedExpr ((Expression) $3, GetLocation ($1));
3738 unchecked_expression
3739 : UNCHECKED open_parens_any expression CLOSE_PARENS
3741 $$ = new UnCheckedExpr ((Expression) $3, GetLocation ($1));
3745 pointer_member_access
3746 : primary_expression OP_PTR IDENTIFIER
3749 var lt = (Tokenizer.LocatedToken) $3;
3751 deref = new Indirection ((Expression) $1, lt.Location);
3752 $$ = new MemberAccess (deref, lt.Value);
3756 anonymous_method_expression
3757 : DELEGATE opt_anonymous_method_signature
3759 start_anonymous (false, (ParametersCompiled) $2, GetLocation ($1));
3763 $$ = end_anonymous ((ToplevelBlock) $4);
3767 opt_anonymous_method_signature
3770 $$ = ParametersCompiled.Undefined;
3772 | anonymous_method_signature
3775 anonymous_method_signature
3778 valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out;
3780 opt_formal_parameter_list CLOSE_PARENS
3782 valid_param_mod = 0;
3787 default_value_expression
3788 : DEFAULT open_parens_any type CLOSE_PARENS
3790 if (RootContext.Version < LanguageVersion.ISO_2)
3791 Report.FeatureIsNotAvailable (GetLocation ($1), "default value expression");
3793 $$ = new DefaultValueExpression ((Expression) $3, GetLocation ($1));
3798 : primary_expression
3799 | BANG prefixed_unary_expression
3801 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2);
3803 | TILDE prefixed_unary_expression
3805 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2);
3811 : OPEN_PARENS_CAST type CLOSE_PARENS prefixed_unary_expression
3813 $$ = new Cast ((FullNamedExpression) $2, (Expression) $4, GetLocation ($1));
3815 | OPEN_PARENS predefined_type CLOSE_PARENS prefixed_unary_expression
3817 $$ = new Cast ((FullNamedExpression) $2, (Expression) $4, GetLocation ($1));
3822 // The idea to split this out is from Rhys' grammar
3823 // to solve the problem with casts.
3825 prefixed_unary_expression
3827 | PLUS prefixed_unary_expression
3829 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2);
3831 | MINUS prefixed_unary_expression
3833 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2);
3835 | OP_INC prefixed_unary_expression
3837 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement, (Expression) $2);
3839 | OP_DEC prefixed_unary_expression
3841 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement, (Expression) $2);
3843 | STAR prefixed_unary_expression
3845 $$ = new Indirection ((Expression) $2, GetLocation ($1));
3847 | BITWISE_AND prefixed_unary_expression
3849 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2);
3853 multiplicative_expression
3854 : prefixed_unary_expression
3855 | multiplicative_expression STAR prefixed_unary_expression
3857 $$ = new Binary (Binary.Operator.Multiply,
3858 (Expression) $1, (Expression) $3);
3860 | multiplicative_expression DIV prefixed_unary_expression
3862 $$ = new Binary (Binary.Operator.Division,
3863 (Expression) $1, (Expression) $3);
3865 | multiplicative_expression PERCENT prefixed_unary_expression
3867 $$ = new Binary (Binary.Operator.Modulus,
3868 (Expression) $1, (Expression) $3);
3873 : multiplicative_expression
3874 | additive_expression PLUS multiplicative_expression
3876 $$ = new Binary (Binary.Operator.Addition,
3877 (Expression) $1, (Expression) $3);
3879 | additive_expression MINUS multiplicative_expression
3881 $$ = new Binary (Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3883 | parenthesized_expression MINUS multiplicative_expression
3885 // Shift/Reduce conflict
3886 $$ = new Binary (Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3888 | additive_expression AS type
3890 $$ = new As ((Expression) $1, (Expression) $3, GetLocation ($2));
3892 | additive_expression IS type
3894 $$ = new Is ((Expression) $1, (Expression) $3, GetLocation ($2));
3899 : additive_expression
3900 | shift_expression OP_SHIFT_LEFT additive_expression
3902 $$ = new Binary (Binary.Operator.LeftShift,
3903 (Expression) $1, (Expression) $3);
3905 | shift_expression OP_SHIFT_RIGHT additive_expression
3907 $$ = new Binary (Binary.Operator.RightShift,
3908 (Expression) $1, (Expression) $3);
3912 relational_expression
3914 | relational_expression OP_LT shift_expression
3916 $$ = new Binary (Binary.Operator.LessThan,
3917 (Expression) $1, (Expression) $3);
3919 | relational_expression OP_GT shift_expression
3921 $$ = new Binary (Binary.Operator.GreaterThan,
3922 (Expression) $1, (Expression) $3);
3924 | relational_expression OP_LE shift_expression
3926 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3927 (Expression) $1, (Expression) $3);
3929 | relational_expression OP_GE shift_expression
3931 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3932 (Expression) $1, (Expression) $3);
3937 : relational_expression
3938 | equality_expression OP_EQ relational_expression
3940 $$ = new Binary (Binary.Operator.Equality,
3941 (Expression) $1, (Expression) $3);
3943 | equality_expression OP_NE relational_expression
3945 $$ = new Binary (Binary.Operator.Inequality,
3946 (Expression) $1, (Expression) $3);
3951 : equality_expression
3952 | and_expression BITWISE_AND equality_expression
3954 $$ = new Binary (Binary.Operator.BitwiseAnd,
3955 (Expression) $1, (Expression) $3);
3959 exclusive_or_expression
3961 | exclusive_or_expression CARRET and_expression
3963 $$ = new Binary (Binary.Operator.ExclusiveOr,
3964 (Expression) $1, (Expression) $3);
3968 inclusive_or_expression
3969 : exclusive_or_expression
3970 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3972 $$ = new Binary (Binary.Operator.BitwiseOr,
3973 (Expression) $1, (Expression) $3);
3977 conditional_and_expression
3978 : inclusive_or_expression
3979 | conditional_and_expression OP_AND inclusive_or_expression
3981 $$ = new Binary (Binary.Operator.LogicalAnd,
3982 (Expression) $1, (Expression) $3);
3986 conditional_or_expression
3987 : conditional_and_expression
3988 | conditional_or_expression OP_OR conditional_and_expression
3990 $$ = new Binary (Binary.Operator.LogicalOr,
3991 (Expression) $1, (Expression) $3);
3995 null_coalescing_expression
3996 : conditional_or_expression
3997 | conditional_or_expression OP_COALESCING null_coalescing_expression
3999 if (RootContext.Version < LanguageVersion.ISO_2)
4000 Report.FeatureIsNotAvailable (GetLocation ($2), "null coalescing operator");
4002 $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $3, GetLocation ($2));
4006 conditional_expression
4007 : null_coalescing_expression
4008 | null_coalescing_expression INTERR expression COLON expression
4010 $$ = new Conditional (new BooleanExpression ((Expression) $1), (Expression) $3, (Expression) $5);
4014 assignment_expression
4015 : prefixed_unary_expression ASSIGN expression
4017 $$ = new SimpleAssign ((Expression) $1, (Expression) $3);
4019 | prefixed_unary_expression OP_MULT_ASSIGN expression
4021 $$ = new CompoundAssign (
4022 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
4024 | prefixed_unary_expression OP_DIV_ASSIGN expression
4026 $$ = new CompoundAssign (
4027 Binary.Operator.Division, (Expression) $1, (Expression) $3);
4029 | prefixed_unary_expression OP_MOD_ASSIGN expression
4031 $$ = new CompoundAssign (
4032 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
4034 | prefixed_unary_expression OP_ADD_ASSIGN expression
4036 $$ = new CompoundAssign (
4037 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
4039 | prefixed_unary_expression OP_SUB_ASSIGN expression
4041 $$ = new CompoundAssign (
4042 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
4044 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
4046 $$ = new CompoundAssign (
4047 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
4049 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
4051 $$ = new CompoundAssign (
4052 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
4054 | prefixed_unary_expression OP_AND_ASSIGN expression
4056 $$ = new CompoundAssign (
4057 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
4059 | prefixed_unary_expression OP_OR_ASSIGN expression
4061 $$ = new CompoundAssign (
4062 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
4064 | prefixed_unary_expression OP_XOR_ASSIGN expression
4066 $$ = new CompoundAssign (
4067 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
4071 lambda_parameter_list
4074 ArrayList pars = new ArrayList (4);
4079 | lambda_parameter_list COMMA lambda_parameter
4081 ArrayList pars = (ArrayList) $1;
4082 Parameter p = (Parameter)$3;
4083 if (pars[0].GetType () != p.GetType ()) {
4084 Report.Error (748, p.Location, "All lambda parameters must be typed either explicitly or implicitly");
4093 : parameter_modifier parameter_type IDENTIFIER
4095 var lt = (Tokenizer.LocatedToken) $3;
4097 $$ = new Parameter ((FullNamedExpression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
4099 | parameter_type IDENTIFIER
4101 var lt = (Tokenizer.LocatedToken) $2;
4103 $$ = new Parameter ((FullNamedExpression) $1, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
4107 var lt = (Tokenizer.LocatedToken) $1;
4108 $$ = new ImplicitLambdaParameter (lt.Value, lt.Location);
4112 opt_lambda_parameter_list
4113 : /* empty */ { $$ = ParametersCompiled.EmptyReadOnlyParameters; }
4114 | lambda_parameter_list {
4115 ArrayList pars_list = (ArrayList) $1;
4116 $$ = new ParametersCompiled (compiler, (Parameter[])pars_list.ToArray (typeof (Parameter)));
4120 lambda_expression_body
4122 start_block (lexer.Location);
4126 Block b = end_block (lexer.Location);
4127 b.AddStatement (new ContextualReturn ((Expression) $2));
4138 var lt = (Tokenizer.LocatedToken) $1;
4139 Parameter p = new ImplicitLambdaParameter (lt.Value, lt.Location);
4140 start_anonymous (true, new ParametersCompiled (compiler, p), GetLocation ($1));
4142 lambda_expression_body
4144 $$ = end_anonymous ((ToplevelBlock) $4);
4146 | OPEN_PARENS_LAMBDA
4148 valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out;
4150 opt_lambda_parameter_list CLOSE_PARENS ARROW
4152 valid_param_mod = 0;
4153 start_anonymous (true, (ParametersCompiled) $3, GetLocation ($1));
4155 lambda_expression_body
4157 $$ = end_anonymous ((ToplevelBlock) $7);
4162 : assignment_expression
4163 | non_assignment_expression
4166 non_assignment_expression
4167 : conditional_expression
4179 $$ = new BooleanExpression ((Expression) $1);
4192 lexer.ConstraintsParsing = true;
4194 type_declaration_name
4196 MemberName name = MakeName ((MemberName) $6);
4197 push_current_class (new Class (current_namespace, current_class, name, (int) $2, (Attributes) $1), $3);
4200 opt_type_parameter_constraints_clauses
4202 lexer.ConstraintsParsing = false;
4204 current_class.SetParameterInfo ((ArrayList) $9);
4206 if (RootContext.Documentation != null) {
4207 current_container.DocComment = Lexer.consume_doc_comment ();
4208 Lexer.doc_state = XmlCommentState.Allowed;
4213 --lexer.parsing_declaration;
4214 if (RootContext.Documentation != null)
4215 Lexer.doc_state = XmlCommentState.Allowed;
4219 $$ = pop_current_class ();
4227 { $$ = $1; } // location
4231 : /* empty */ { $$ = (int) 0; }
4237 | modifiers modifier
4242 if ((m1 & m2) != 0) {
4243 Location l = lexer.Location;
4244 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
4246 $$ = (int) (m1 | m2);
4254 if (current_container == RootContext.ToplevelTypes)
4255 Report.Error (1530, GetLocation ($1), "Keyword `new' is not allowed on namespace elements");
4257 | PUBLIC { $$ = Modifiers.PUBLIC; }
4258 | PROTECTED { $$ = Modifiers.PROTECTED; }
4259 | INTERNAL { $$ = Modifiers.INTERNAL; }
4260 | PRIVATE { $$ = Modifiers.PRIVATE; }
4261 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
4262 | SEALED { $$ = Modifiers.SEALED; }
4263 | STATIC { $$ = Modifiers.STATIC; }
4264 | READONLY { $$ = Modifiers.READONLY; }
4265 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
4266 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
4267 | EXTERN { $$ = Modifiers.EXTERN; }
4268 | VOLATILE { $$ = Modifiers.VOLATILE; }
4269 | UNSAFE { $$ = Modifiers.UNSAFE; }
4278 : COLON type_list { current_container.AddBasesForPart (current_class, (ArrayList) $2); }
4281 opt_type_parameter_constraints_clauses
4282 : /* empty */ { $$ = null; }
4283 | type_parameter_constraints_clauses
4287 type_parameter_constraints_clauses
4288 : type_parameter_constraints_clause {
4289 ArrayList constraints = new ArrayList (1);
4290 constraints.Add ($1);
4293 | type_parameter_constraints_clauses type_parameter_constraints_clause {
4294 ArrayList constraints = (ArrayList) $1;
4295 Constraints new_constraint = (Constraints)$2;
4297 foreach (Constraints c in constraints) {
4298 if (new_constraint.TypeParameter == c.TypeParameter) {
4299 Report.Error (409, new_constraint.Location, "A constraint clause has already been specified for type parameter `{0}'",
4300 new_constraint.TypeParameter);
4304 constraints.Add (new_constraint);
4309 type_parameter_constraints_clause
4310 : WHERE IDENTIFIER COLON type_parameter_constraints {
4311 var lt = (Tokenizer.LocatedToken) $2;
4312 $$ = new Constraints (lt.Value, (ArrayList) $4, lt.Location);
4316 type_parameter_constraints
4317 : type_parameter_constraint {
4318 ArrayList constraints = new ArrayList (1);
4319 constraints.Add ($1);
4322 | type_parameter_constraints COMMA type_parameter_constraint {
4323 ArrayList constraints = (ArrayList) $1;
4325 constraints.Add ($3);
4330 type_parameter_constraint
4332 | NEW OPEN_PARENS CLOSE_PARENS {
4333 $$ = SpecialConstraint.Constructor;
4336 $$ = SpecialConstraint.ReferenceType;
4339 $$ = SpecialConstraint.ValueType;
4343 opt_type_parameter_variance
4348 | type_parameter_variance
4350 if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)
4351 Report.FeatureIsNotSupported (lexer.Location, "generic type variance");
4352 else if (RootContext.Version <= LanguageVersion.V_3)
4353 Report.FeatureIsNotAvailable (lexer.Location, "generic type variance");
4359 type_parameter_variance
4362 $$ = Variance.Covariant;
4366 $$ = Variance.Contravariant;
4375 // A block is "contained" on the following places:
4377 // property_declaration as part of the accessor body (get/set)
4378 // operator_declaration
4379 // constructor_declaration
4380 // destructor_declaration
4381 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
4386 ++lexer.parsing_block;
4387 start_block (GetLocation ($1));
4389 opt_statement_list block_end
4398 --lexer.parsing_block;
4399 $$ = end_block (GetLocation ($1));
4401 | COMPLETE_COMPLETION
4403 --lexer.parsing_block;
4404 $$ = end_block (lexer.Location);
4412 ++lexer.parsing_block;
4413 current_block.StartLocation = GetLocation ($1);
4415 opt_statement_list CLOSE_BRACE
4417 --lexer.parsing_block;
4418 $$ = end_block (GetLocation ($4));
4429 | statement_list statement
4433 : declaration_statement
4435 if ($1 != null && (Block) $1 != current_block){
4436 current_block.AddStatement ((Statement) $1);
4437 current_block = (Block) $1;
4440 | valid_declaration_statement
4442 current_block.AddStatement ((Statement) $1);
4448 // The interactive_statement and its derivatives are only
4449 // used to provide a special version of `expression_statement'
4450 // that has a side effect of assigning the expression to
4453 interactive_statement_list
4454 : interactive_statement
4455 | interactive_statement_list interactive_statement
4458 interactive_statement
4459 : declaration_statement
4461 if ($1 != null && (Block) $1 != current_block){
4462 current_block.AddStatement ((Statement) $1);
4463 current_block = (Block) $1;
4466 | interactive_valid_declaration_statement
4468 current_block.AddStatement ((Statement) $1);
4473 valid_declaration_statement
4476 | expression_statement
4477 | selection_statement
4478 | iteration_statement
4482 | unchecked_statement
4489 interactive_valid_declaration_statement
4492 | interactive_expression_statement
4493 | selection_statement
4494 | iteration_statement
4498 | unchecked_statement
4506 : valid_declaration_statement
4507 | declaration_statement
4509 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4514 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4522 $$ = EmptyStatement.Value;
4529 var lt = (Tokenizer.LocatedToken) $1;
4530 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
4532 if (current_block.AddLabel (labeled))
4533 current_block.AddStatement (labeled);
4538 declaration_statement
4539 : local_variable_declaration SEMICOLON
4541 current_array_type = null;
4543 DictionaryEntry de = (DictionaryEntry) $1;
4544 Expression e = (Expression) de.Key;
4546 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
4550 | local_constant_declaration SEMICOLON
4552 current_array_type = null;
4554 DictionaryEntry de = (DictionaryEntry) $1;
4556 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
4562 * The following is from Rhys' grammar:
4563 * > Types in local variable declarations must be recognized as
4564 * > expressions to prevent reduce/reduce errors in the grammar.
4565 * > The expressions are converted into types during semantic analysis.
4568 : primary_expression_no_array_creation opt_rank_specifier_or_nullable
4570 // FIXME: Do something smart here regarding the composition of the type.
4572 // Ok, the above "primary_expression" is there to get rid of
4573 // both reduce/reduce and shift/reduces in the grammar, it should
4574 // really just be "type_name". If you use type_name, a reduce/reduce
4575 // creeps up. If you use namespace_or_type_name (which is all we need
4576 // really) two shift/reduces appear.
4579 // So the super-trick is that primary_expression
4580 // can only be either a SimpleName or a MemberAccess.
4581 // The MemberAccess case arises when you have a fully qualified type-name like :
4583 // SimpleName is when you have
4586 Expression expr = (Expression) $1;
4587 string rank_or_nullable = (string) $2;
4589 if (expr is ComposedCast){
4590 $$ = new ComposedCast ((ComposedCast)expr, rank_or_nullable);
4591 } else if (expr is ATypeNameExpression){
4593 // So we extract the string corresponding to the SimpleName
4596 if (rank_or_nullable.Length == 0) {
4597 SimpleName sn = expr as SimpleName;
4598 if (sn != null && sn.Name == "var")
4599 $$ = current_array_type = new VarExpr (sn.Location);
4603 $$ = new ComposedCast ((ATypeNameExpression)expr, rank_or_nullable);
4606 Error_ExpectingTypeName (expr);
4607 $$ = TypeManager.system_object_expr;
4610 | builtin_types opt_rank_specifier_or_nullable
4612 if ((string) $2 == "")
4615 $$ = current_array_type = new ComposedCast ((FullNamedExpression) $1, (string) $2, lexer.Location);
4617 | VOID opt_rank_specifier
4619 Expression.Error_VoidInvalidInTheContext (GetLocation ($1), Report);
4620 $$ = TypeManager.system_void_expr;
4624 local_variable_pointer_type
4625 : primary_expression_no_array_creation STAR
4627 ATypeNameExpression expr = $1 as ATypeNameExpression;
4630 $$ = new ComposedCast (expr, "*");
4632 Error_ExpectingTypeName ((Expression)$1);
4636 | builtin_types STAR
4638 $$ = new ComposedCast ((FullNamedExpression) $1, "*", GetLocation ($1));
4642 $$ = new ComposedCast (TypeManager.system_void_expr, "*", GetLocation ($1));
4644 | local_variable_pointer_type STAR
4646 $$ = new ComposedCast ((FullNamedExpression) $1, "*");
4652 | local_variable_pointer_type opt_rank_specifier
4655 string rank = (string)$2;
4660 $$ = current_array_type = new ComposedCast ((FullNamedExpression) $1, rank);
4667 local_variable_declaration
4668 : local_variable_type local_variable_declarators
4671 VarExpr ve = $1 as VarExpr;
4673 if (((VariableDeclaration)((ArrayList)$2) [0]).expression_or_array_initializer == null)
4674 ve.VariableInitializersCount = 0;
4676 ve.VariableInitializersCount = ((ArrayList)$2).Count;
4679 $$ = new DictionaryEntry ($1, $2);
4685 local_constant_declaration
4686 : CONST variable_type constant_declarators
4689 $$ = new DictionaryEntry ($2, $3);
4695 expression_statement
4696 : statement_expression SEMICOLON { $$ = $1; }
4697 | statement_expression COMPLETE_COMPLETION { $$ = $1; }
4700 interactive_expression_statement
4701 : interactive_statement_expression SEMICOLON { $$ = $1; }
4702 | interactive_statement_expression COMPLETE_COMPLETION { $$ = $1; }
4706 // We have to do the wrapping here and not in the case above,
4707 // because statement_expression is used for example in for_statement
4709 statement_expression
4712 ExpressionStatement s = $1 as ExpressionStatement;
4714 Expression.Error_InvalidExpressionStatement (Report, GetLocation ($1));
4715 s = EmptyExpressionStatement.Instance;
4718 $$ = new StatementExpression (s);
4722 Error_SyntaxError (yyToken);
4727 interactive_statement_expression
4730 Expression expr = (Expression) $1;
4731 ExpressionStatement s;
4733 s = new OptionalAssign (new SimpleName ("$retval", lexer.Location), expr, lexer.Location);
4734 $$ = new StatementExpression (s);
4738 Error_SyntaxError (yyToken);
4749 : IF open_parens_any boolean_expression CLOSE_PARENS
4752 Location l = GetLocation ($1);
4754 $$ = new If ((BooleanExpression) $3, (Statement) $5, l);
4756 // FIXME: location for warning should be loc property of $5.
4757 if ($5 == EmptyStatement.Value)
4758 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4761 | IF open_parens_any boolean_expression CLOSE_PARENS
4762 embedded_statement ELSE embedded_statement
4764 Location l = GetLocation ($1);
4766 $$ = new If ((BooleanExpression) $3, (Statement) $5, (Statement) $7, l);
4768 // FIXME: location for warning should be loc property of $5 and $7.
4769 if ($5 == EmptyStatement.Value)
4770 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4771 if ($7 == EmptyStatement.Value)
4772 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4777 : SWITCH open_parens_any
4779 if (switch_stack == null)
4780 switch_stack = new Stack (2);
4781 switch_stack.Push (current_block);
4783 expression CLOSE_PARENS
4786 $$ = new Switch ((Expression) $4, (ArrayList) $6, GetLocation ($1));
4787 current_block = (Block) switch_stack.Pop ();
4803 Report.Warning (1522, 1, lexer.Location, "Empty switch block");
4804 $$ = new ArrayList ();
4812 ArrayList sections = new ArrayList (4);
4817 | switch_sections switch_section
4819 ArrayList sections = (ArrayList) $1;
4829 current_block = current_block.CreateSwitchBlock (lexer.Location);
4833 $$ = new SwitchSection ((ArrayList) $1, current_block.Explicit);
4840 ArrayList labels = new ArrayList (4);
4845 | switch_labels switch_label
4847 ArrayList labels = (ArrayList) ($1);
4855 : CASE constant_expression COLON
4857 $$ = new SwitchLabel ((Expression) $2, GetLocation ($1));
4861 $$ = new SwitchLabel (null, GetLocation ($1));
4873 : WHILE open_parens_any boolean_expression CLOSE_PARENS embedded_statement
4875 Location l = GetLocation ($1);
4876 $$ = new While ((BooleanExpression) $3, (Statement) $5, l);
4881 : DO embedded_statement
4882 WHILE open_parens_any boolean_expression CLOSE_PARENS SEMICOLON
4884 Location l = GetLocation ($1);
4886 $$ = new Do ((Statement) $2, (BooleanExpression) $5, l);
4891 : FOR open_parens_any opt_for_initializer SEMICOLON
4893 Location l = lexer.Location;
4895 Block assign_block = current_block;
4897 if ($3 is DictionaryEntry){
4898 DictionaryEntry de = (DictionaryEntry) $3;
4900 Expression type = (Expression) de.Key;
4901 ArrayList var_declarators = (ArrayList) de.Value;
4903 foreach (VariableDeclaration decl in var_declarators){
4907 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4911 Expression expr = decl.expression_or_array_initializer;
4913 LocalVariableReference var;
4914 var = new LocalVariableReference (assign_block, decl.identifier, l);
4917 Assign a = new SimpleAssign (var, expr, decl.Location);
4919 assign_block.AddStatement (new StatementExpression (a));
4923 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4924 // This can be referred to as $5 below.
4930 opt_for_condition SEMICOLON
4931 opt_for_iterator CLOSE_PARENS
4934 Location l = GetLocation ($1);
4936 For f = new For ((Statement) $5, (BooleanExpression) $6, (Statement) $8, (Statement) $10, l);
4938 current_block.AddStatement (f);
4940 $$ = end_block (lexer.Location);
4945 : /* empty */ { $$ = EmptyStatement.Value; }
4950 : local_variable_declaration
4951 | statement_expression_list
4955 : /* empty */ { $$ = null; }
4956 | boolean_expression
4960 : /* empty */ { $$ = EmptyStatement.Value; }
4965 : statement_expression_list
4968 statement_expression_list
4969 : statement_expression
4971 // CHANGE: was `null'
4972 Statement s = (Statement) $1;
4973 Block b = new Block (current_block, s.loc, lexer.Location);
4978 | statement_expression_list COMMA statement_expression
4980 Block b = (Block) $1;
4982 b.AddStatement ((Statement) $3);
4988 : FOREACH open_parens_any type IN expression CLOSE_PARENS
4990 Report.Error (230, GetLocation ($1), "Type and identifier are both required in a foreach statement");
4993 | FOREACH open_parens_any type IDENTIFIER IN
4994 expression CLOSE_PARENS
4996 start_block (lexer.Location);
4997 Block foreach_block = current_block;
4999 var lt = (Tokenizer.LocatedToken) $4;
5000 Location l = lt.Location;
5001 LocalInfo vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
5003 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
5005 // Get a writable reference to this read-only variable.
5007 // Note that the $$ here refers to the value of _this_ code block,
5008 // not the value of the LHS non-terminal. This can be referred to as $8 below.
5009 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
5016 LocalVariableReference v = (LocalVariableReference) $8;
5017 Location l = GetLocation ($1);
5020 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
5021 current_block.AddStatement (f);
5024 $$ = end_block (lexer.Location);
5030 | continue_statement
5040 $$ = new Break (GetLocation ($1));
5045 : CONTINUE SEMICOLON
5047 $$ = new Continue (GetLocation ($1));
5052 : GOTO IDENTIFIER SEMICOLON
5054 var lt = (Tokenizer.LocatedToken) $2;
5055 $$ = new Goto (lt.Value, lt.Location);
5057 | GOTO CASE constant_expression SEMICOLON
5059 $$ = new GotoCase ((Expression) $3, GetLocation ($1));
5061 | GOTO DEFAULT SEMICOLON
5063 $$ = new GotoDefault (GetLocation ($1));
5068 : RETURN opt_expression SEMICOLON
5070 $$ = new Return ((Expression) $2, GetLocation ($1));
5075 : THROW opt_expression SEMICOLON
5077 $$ = new Throw ((Expression) $2, GetLocation ($1));
5082 : IDENTIFIER RETURN expression SEMICOLON
5084 var lt = (Tokenizer.LocatedToken) $1;
5085 string s = lt.Value;
5087 Report.Error (1003, lt.Location, "; expected");
5090 if (RootContext.Version == LanguageVersion.ISO_1){
5091 Report.FeatureIsNotAvailable (lt.Location, "yield statement");
5094 current_block.Toplevel.IsIterator = true;
5095 $$ = new Yield ((Expression) $3, lt.Location);
5097 | IDENTIFIER RETURN SEMICOLON
5099 Report.Error (1627, GetLocation ($2), "Expression expected after yield return");
5102 | IDENTIFIER BREAK SEMICOLON
5104 var lt = (Tokenizer.LocatedToken) $1;
5105 string s = lt.Value;
5107 Report.Error (1003, lt.Location, "; expected");
5110 if (RootContext.Version == LanguageVersion.ISO_1){
5111 Report.FeatureIsNotAvailable (lt.Location, "yield statement");
5115 current_block.Toplevel.IsIterator = true;
5116 $$ = new YieldBreak (lt.Location);
5126 : TRY block catch_clauses
5128 $$ = new TryCatch ((Block) $2, (ArrayList) $3, GetLocation ($1), false);
5130 | TRY block FINALLY block
5132 $$ = new TryFinally ((Statement) $2, (Block) $4, GetLocation ($1));
5134 | TRY block catch_clauses FINALLY block
5136 $$ = new TryFinally (new TryCatch ((Block) $2, (ArrayList) $3, GetLocation ($1), true), (Block) $5, GetLocation ($1));
5140 Report.Error (1524, GetLocation ($1), "Expected catch or finally");
5148 ArrayList l = new ArrayList (4);
5153 | catch_clauses catch_clause
5155 ArrayList l = (ArrayList) $1;
5157 Catch c = (Catch) $2;
5158 if (((Catch) l [0]).IsGeneral) {
5159 Report.Error (1017, c.loc, "Try statement already has an empty catch block");
5172 : /* empty */ { $$ = null; }
5177 : CATCH opt_catch_args
5179 Expression type = null;
5182 DictionaryEntry cc = (DictionaryEntry) $2;
5183 type = (Expression) cc.Key;
5184 var lt = (Tokenizer.LocatedToken) cc.Value;
5187 ArrayList one = new ArrayList (2);
5189 one.Add (new VariableDeclaration (lt, null));
5191 start_block (lexer.Location);
5192 current_block = declare_local_variables (type, one, lt.Location);
5196 Expression type = null;
5198 Block var_block = null;
5201 DictionaryEntry cc = (DictionaryEntry) $2;
5202 type = (Expression) cc.Key;
5203 var lt = (Tokenizer.LocatedToken) cc.Value;
5207 var_block = end_block (lexer.Location);
5211 $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
5216 : /* empty */ { $$ = null; }
5221 : open_parens_any type opt_identifier CLOSE_PARENS
5223 $$ = new DictionaryEntry ($2, $3);
5225 | open_parens_any CLOSE_PARENS
5227 Report.Error (1015, GetLocation ($1), "A type that derives from `System.Exception', `object', or `string' expected");
5235 $$ = new Checked ((Block) $2);
5242 $$ = new Unchecked ((Block) $2);
5249 RootContext.CheckUnsafeOption (GetLocation ($1), Report);
5251 $$ = new Unsafe ((Block) $3);
5256 : FIXED open_parens_any
5257 type_and_void fixed_pointer_declarators
5260 ArrayList list = (ArrayList) $4;
5261 Expression type = (Expression) $3;
5262 Location l = GetLocation ($1);
5263 int top = list.Count;
5265 start_block (lexer.Location);
5267 for (int i = 0; i < top; i++){
5268 Pair p = (Pair) list [i];
5271 v = current_block.AddVariable (type, (string) p.First, l);
5275 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
5283 Location l = GetLocation ($1);
5285 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
5287 current_block.AddStatement (f);
5289 $$ = end_block (lexer.Location);
5293 fixed_pointer_declarators
5294 : fixed_pointer_declarator {
5295 ArrayList declarators = new ArrayList (4);
5297 declarators.Add ($1);
5300 | fixed_pointer_declarators COMMA fixed_pointer_declarator
5302 ArrayList declarators = (ArrayList) $1;
5304 declarators.Add ($3);
5309 fixed_pointer_declarator
5310 : IDENTIFIER ASSIGN expression
5312 var lt = (Tokenizer.LocatedToken) $1;
5313 // FIXME: keep location
5314 $$ = new Pair (lt.Value, $3);
5318 Report.Error (210, ((Tokenizer.LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
5324 : LOCK open_parens_any expression CLOSE_PARENS
5330 $$ = new Lock ((Expression) $3, (Statement) $6, GetLocation ($1));
5335 : USING open_parens_any local_variable_declaration CLOSE_PARENS
5337 start_block (lexer.Location);
5338 Block assign_block = current_block;
5340 DictionaryEntry de = (DictionaryEntry) $3;
5341 Location l = GetLocation ($1);
5343 Expression type = (Expression) de.Key;
5344 ArrayList var_declarators = (ArrayList) de.Value;
5346 Stack vars = new Stack ();
5348 foreach (VariableDeclaration decl in var_declarators) {
5349 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
5352 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
5354 Expression expr = decl.expression_or_array_initializer;
5356 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
5359 LocalVariableReference var;
5361 // Get a writable reference to this read-only variable.
5362 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
5364 // This is so that it is not a warning on using variables
5367 vars.Push (new DictionaryEntry (var, expr));
5369 // Assign a = new SimpleAssign (var, expr, decl.Location);
5370 // assign_block.AddStatement (new StatementExpression (a));
5373 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
5374 // It can be referred to as $5 below.
5379 Statement stmt = (Statement) $6;
5380 Stack vars = (Stack) $5;
5381 Location l = GetLocation ($1);
5383 while (vars.Count > 0) {
5384 DictionaryEntry de = (DictionaryEntry) vars.Pop ();
5385 stmt = new Using ((Expression) de.Key, (Expression) de.Value, stmt, l);
5387 current_block.AddStatement (stmt);
5388 $$ = end_block (lexer.Location);
5390 | USING open_parens_any expression CLOSE_PARENS
5392 start_block (lexer.Location);
5396 current_block.AddStatement (new UsingTemporary ((Expression) $3, (Statement) $6, GetLocation ($1)));
5397 $$ = end_block (lexer.Location);
5405 : first_from_clause query_body
5407 lexer.query_parsing = false;
5409 Linq.AQueryClause from = $1 as Linq.AQueryClause;
5411 from.Tail.Next = (Linq.AQueryClause)$2;
5414 current_block.SetEndLocation (lexer.Location);
5415 current_block = current_block.Parent;
5417 | nested_from_clause query_body
5419 Linq.AQueryClause from = $1 as Linq.AQueryClause;
5421 from.Tail.Next = (Linq.AQueryClause)$2;
5424 current_block.SetEndLocation (lexer.Location);
5425 current_block = current_block.Parent;
5430 : FROM_FIRST IDENTIFIER IN expression
5432 $$ = new Linq.QueryExpression (current_block, new Linq.QueryStartClause ((Expression)$4));
5433 var lt = (Tokenizer.LocatedToken) $2;
5434 current_block = new Linq.QueryBlock (compiler, current_block, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1));
5436 | FROM_FIRST type IDENTIFIER IN expression
5438 var lt = (Tokenizer.LocatedToken) $3;
5439 $$ = new Linq.QueryExpression (current_block, new Linq.Cast ((FullNamedExpression)$2, (Expression)$5));
5440 current_block = new Linq.QueryBlock (compiler, current_block, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1));
5445 : FROM IDENTIFIER IN expression
5447 $$ = new Linq.QueryExpression (current_block, new Linq.QueryStartClause ((Expression)$4));
5448 var lt = (Tokenizer.LocatedToken) $2;
5449 current_block = new Linq.QueryBlock (compiler, current_block, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1));
5451 | FROM type IDENTIFIER IN expression
5453 $$ = new Linq.QueryExpression (current_block, new Linq.Cast ((FullNamedExpression)$2, (Expression)$5));
5454 var lt = (Tokenizer.LocatedToken) $3;
5455 current_block = new Linq.QueryBlock (compiler, current_block, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1));
5460 : FROM IDENTIFIER IN
5462 current_block = new Linq.QueryBlock (compiler, current_block, GetLocation ($1));
5466 var lt = (Tokenizer.LocatedToken) $2;
5467 var sn = new SimpleMemberName (lt.Value, lt.Location);
5468 $$ = new Linq.SelectMany (current_block.Toplevel, sn, (Expression)$5);
5470 current_block.SetEndLocation (lexer.Location);
5471 current_block = current_block.Parent;
5473 ((Linq.QueryBlock)current_block).AddTransparentParameter (compiler, sn);
5475 | FROM type IDENTIFIER IN
5477 current_block = new Linq.QueryBlock (compiler, current_block, GetLocation ($1));
5481 var lt = (Tokenizer.LocatedToken) $3;
5482 var sn = new SimpleMemberName (lt.Value, lt.Location);
5484 FullNamedExpression type = (FullNamedExpression)$2;
5486 $$ = new Linq.SelectMany (current_block.Toplevel, sn, new Linq.Cast (type, (FullNamedExpression)$6));
5488 current_block.SetEndLocation (lexer.Location);
5489 current_block = current_block.Parent;
5491 ((Linq.QueryBlock)current_block).AddTransparentParameter (compiler, sn);
5496 : opt_query_body_clauses select_or_group_clause opt_query_continuation
5498 Linq.AQueryClause head = (Linq.AQueryClause)$2;
5501 head.Next = (Linq.AQueryClause)$3;
5504 Linq.AQueryClause clause = (Linq.AQueryClause)$1;
5505 clause.Tail.Next = head;
5513 select_or_group_clause
5516 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5520 $$ = new Linq.Select (current_block.Toplevel, (Expression)$3, GetLocation ($1));
5522 current_block.SetEndLocation (lexer.Location);
5523 current_block = current_block.Parent;
5527 if (linq_clause_blocks == null)
5528 linq_clause_blocks = new Stack ();
5530 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5531 linq_clause_blocks.Push (current_block);
5535 current_block.SetEndLocation (lexer.Location);
5536 current_block = current_block.Parent;
5538 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5542 $$ = new Linq.GroupBy (current_block.Toplevel, (Expression)$3, (ToplevelBlock) linq_clause_blocks.Pop (), (Expression)$6, GetLocation ($1));
5544 current_block.SetEndLocation (lexer.Location);
5545 current_block = current_block.Parent;
5549 opt_query_body_clauses
5551 | query_body_clauses
5556 | query_body_clauses query_body_clause
5558 ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$2;
5572 : LET IDENTIFIER ASSIGN
5574 current_block = new Linq.QueryBlock (compiler, current_block, GetLocation ($1));
5578 var lt = (Tokenizer.LocatedToken) $2;
5579 var sn = new SimpleMemberName (lt.Value, lt.Location);
5580 $$ = new Linq.Let (current_block.Toplevel, current_container, sn, (Expression)$5);
5582 current_block.SetEndLocation (lexer.Location);
5583 current_block = current_block.Parent;
5585 ((Linq.QueryBlock)current_block).AddTransparentParameter (compiler, sn);
5592 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5596 $$ = new Linq.Where (current_block.Toplevel, (BooleanExpression)$3, GetLocation ($1));
5598 current_block.SetEndLocation (lexer.Location);
5599 current_block = current_block.Parent;
5604 : JOIN IDENTIFIER IN
5606 if (linq_clause_blocks == null)
5607 linq_clause_blocks = new Stack ();
5609 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5610 linq_clause_blocks.Push (current_block);
5614 current_block.SetEndLocation (lexer.Location);
5615 current_block = current_block.Parent;
5617 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5618 linq_clause_blocks.Push (current_block);
5622 current_block.AddStatement (new ContextualReturn ((Expression) $8));
5623 current_block.SetEndLocation (lexer.Location);
5624 current_block = current_block.Parent;
5626 var lt = (Tokenizer.LocatedToken) $2;
5627 current_block = new Linq.QueryBlock (compiler, current_block, new SimpleMemberName (lt.Value, lt.Location), lexer.Location);
5629 expression opt_join_into
5631 var lt = (Tokenizer.LocatedToken) $2;
5632 var sn = new SimpleMemberName (lt.Value, lt.Location);
5633 SimpleMemberName sn2 = null;
5635 ToplevelBlock outer_selector = (ToplevelBlock) linq_clause_blocks.Pop ();
5636 ToplevelBlock block = (ToplevelBlock) linq_clause_blocks.Pop ();
5639 $$ = new Linq.Join (block, sn, (Expression)$5, outer_selector, current_block.Toplevel, GetLocation ($1));
5641 var lt2 = (Tokenizer.LocatedToken) $12;
5642 sn2 = new SimpleMemberName (lt2.Value, lt2.Location);
5643 $$ = new Linq.GroupJoin (block, sn, (Expression)$5, outer_selector, current_block.Toplevel,
5644 sn2, GetLocation ($1));
5647 current_block.AddStatement (new ContextualReturn ((Expression) $11));
5648 current_block.SetEndLocation (lexer.Location);
5649 current_block = current_block.Parent;
5652 ((Linq.QueryBlock)current_block).AddTransparentParameter (compiler, sn);
5654 ((Linq.QueryBlock)current_block).AddTransparentParameter (compiler, sn2);
5656 | JOIN type IDENTIFIER IN
5658 if (linq_clause_blocks == null)
5659 linq_clause_blocks = new Stack ();
5661 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5662 linq_clause_blocks.Push (current_block);
5666 current_block.SetEndLocation (lexer.Location);
5667 current_block = current_block.Parent;
5669 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5670 linq_clause_blocks.Push (current_block);
5674 current_block.AddStatement (new ContextualReturn ((Expression) $9));
5675 current_block.SetEndLocation (lexer.Location);
5676 current_block = current_block.Parent;
5678 var lt = (Tokenizer.LocatedToken) $3;
5679 current_block = new Linq.QueryBlock (compiler, current_block, new SimpleMemberName (lt.Value, lt.Location), lexer.Location);
5681 expression opt_join_into
5683 var lt = (Tokenizer.LocatedToken) $3;
5684 var sn = new SimpleMemberName (lt.Value, lt.Location);
5685 SimpleMemberName sn2 = null;
5686 ToplevelBlock outer_selector = (ToplevelBlock) linq_clause_blocks.Pop ();
5687 ToplevelBlock block = (ToplevelBlock) linq_clause_blocks.Pop ();
5689 Linq.Cast cast = new Linq.Cast ((FullNamedExpression)$2, (Expression)$6);
5691 $$ = new Linq.Join (block, sn, cast, outer_selector, current_block.Toplevel, GetLocation ($1));
5693 var lt2 = (Tokenizer.LocatedToken) $13;
5694 sn2 = new SimpleMemberName (lt2.Value, lt2.Location);
5695 $$ = new Linq.GroupJoin (block, sn, cast, outer_selector, current_block.Toplevel,
5696 sn2, GetLocation ($1));
5699 current_block.AddStatement (new ContextualReturn ((Expression) $12));
5700 current_block.SetEndLocation (lexer.Location);
5701 current_block = current_block.Parent;
5704 ((Linq.QueryBlock)current_block).AddTransparentParameter (compiler, sn);
5706 ((Linq.QueryBlock)current_block).AddTransparentParameter (compiler, sn2);
5721 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5725 current_block.SetEndLocation (lexer.Location);
5726 current_block = current_block.Parent;
5736 current_block.SetEndLocation (lexer.Location);
5737 current_block = current_block.Parent;
5739 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5743 ((Linq.AQueryClause)$1).Next = (Linq.AQueryClause)$4;
5750 | orderings_then_by COMMA
5752 current_block.SetEndLocation (lexer.Location);
5753 current_block = current_block.Parent;
5755 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5759 ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$3;
5767 $$ = new Linq.OrderByAscending (current_block.Toplevel, (Expression)$1);
5769 | expression ASCENDING
5771 $$ = new Linq.OrderByAscending (current_block.Toplevel, (Expression)$1);
5773 | expression DESCENDING
5775 $$ = new Linq.OrderByDescending (current_block.Toplevel, (Expression)$1);
5782 $$ = new Linq.ThenByAscending (current_block.Toplevel, (Expression)$1);
5784 | expression ASCENDING
5786 $$ = new Linq.ThenByAscending (current_block.Toplevel, (Expression)$1);
5788 | expression DESCENDING
5790 $$ = new Linq.ThenByDescending (current_block.Toplevel, (Expression)$1);
5795 opt_query_continuation
5799 // query continuation block is not linked with query block but with block
5800 // before. This means each query can use same range variable names for
5801 // different identifiers.
5803 current_block.SetEndLocation (GetLocation ($1));
5804 current_block = current_block.Parent;
5806 var lt = (Tokenizer.LocatedToken) $2;
5808 current_block = new Linq.QueryBlock (compiler, current_block, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1));
5812 $$ = new Linq.QueryExpression (current_block, (Linq.AQueryClause)$4);
5817 // Support for using the compiler as an interactive parser
5819 // The INTERACTIVE_PARSER token is first sent to parse our
5820 // productions; If the result is a Statement, the parsing
5821 // is repeated, this time with INTERACTIVE_PARSE_WITH_BLOCK
5822 // to setup the blocks in advance.
5824 // This setup is here so that in the future we can add
5825 // support for other constructs (type parsing, namespaces, etc)
5826 // that do not require a block to be setup in advance
5830 : EVAL_STATEMENT_PARSER EOF
5831 | EVAL_USING_DECLARATIONS_UNIT_PARSER using_directives
5832 | EVAL_STATEMENT_PARSER {
5833 Evaluator.LoadAliases (current_namespace);
5835 push_current_class (new Class (current_namespace, current_class, new MemberName ("Class" + class_count++),
5836 Modifiers.PUBLIC, null), null);
5838 ArrayList baseclass_list = new ArrayList ();
5839 baseclass_list.Add (new TypeExpression (Evaluator.InteractiveBaseClass, lexer.Location));
5840 current_container.AddBasesForPart (current_class, baseclass_list);
5842 // (ref object retval)
5843 Parameter [] mpar = new Parameter [1];
5844 mpar [0] = new Parameter (TypeManager.system_object_expr, "$retval", Parameter.Modifier.REF, null, Location.Null);
5846 ParametersCompiled pars = new ParametersCompiled (compiler, mpar);
5847 current_local_parameters = pars;
5848 Method method = new Method (
5851 TypeManager.system_void_expr,
5852 Modifiers.PUBLIC | Modifiers.STATIC,
5853 new MemberName ("Host"),
5855 null /* attributes */);
5857 oob_stack.Push (method);
5858 ++lexer.parsing_block;
5859 start_block (lexer.Location);
5861 interactive_statement_list opt_COMPLETE_COMPLETION
5863 --lexer.parsing_block;
5864 Method method = (Method) oob_stack.Pop ();
5866 method.Block = (ToplevelBlock) end_block(lexer.Location);
5867 current_container.AddMethod (method);
5869 --lexer.parsing_declaration;
5870 InteractiveResult = pop_current_class ();
5871 current_local_parameters = null;
5873 | EVAL_COMPILATION_UNIT_PARSER {
5874 Evaluator.LoadAliases (current_namespace);
5876 interactive_compilation_unit
5879 interactive_compilation_unit
5880 : outer_declarations
5881 | outer_declarations global_attributes
5886 opt_COMPLETE_COMPLETION
5888 | COMPLETE_COMPLETION
5891 close_brace_or_complete_completion
5893 | COMPLETE_COMPLETION
5898 // A class used to pass around variable declarations and constants
5900 class VariableDeclaration {
5901 public string identifier;
5902 public Expression expression_or_array_initializer;
5903 public Location Location;
5904 public Attributes OptAttributes;
5905 public string DocComment;
5907 public VariableDeclaration (Tokenizer.LocatedToken lt, object eoai, Attributes opt_attrs)
5909 this.identifier = lt.Value;
5910 if (eoai is ArrayList) {
5911 this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
5913 this.expression_or_array_initializer = (Expression)eoai;
5915 this.Location = lt.Location;
5916 this.OptAttributes = opt_attrs;
5919 public VariableDeclaration (Tokenizer.LocatedToken lt, object eoai) : this (lt, eoai, null)
5924 class VariableMemberDeclaration
5926 public readonly MemberName MemberName;
5927 public Expression expression_or_array_initializer;
5929 public VariableMemberDeclaration (MemberName mn, object initializer)
5933 if (initializer is ArrayList) {
5934 this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)initializer, mn.Location);
5936 this.expression_or_array_initializer = (Expression)initializer;
5943 // A class used to hold info about an operator declarator
5945 struct OperatorDeclaration {
5946 public readonly Operator.OpType optype;
5947 public readonly FullNamedExpression ret_type;
5948 public readonly Location location;
5950 public OperatorDeclaration (Operator.OpType op, FullNamedExpression ret_type, Location location)
5953 this.ret_type = ret_type;
5954 this.location = location;
5958 void Error_ExpectingTypeName (Expression expr)
5960 if (expr is Invocation){
5961 Report.Error (1002, expr.Location, "Expecting `;'");
5963 Expression.Error_InvalidExpressionStatement (Report, expr.Location);
5967 void Error_ParameterModifierNotValid (string modifier, Location loc)
5969 Report.Error (631, loc, "The parameter modifier `{0}' is not valid in this context",
5973 void Error_DuplicateParameterModifier (Location loc, Parameter.Modifier mod)
5975 Report.Error (1107, loc, "Duplicate parameter modifier `{0}'",
5976 Parameter.GetModifierSignature (mod));
5979 void Error_TypeExpected (Location loc)
5981 Report.Error (1031, loc, "Type expected");
5984 void Error_NamedArgumentExpected (NamedArgument a)
5986 Report.Error (1738, a.Location, "Named arguments must appear after the positional arguments");
5989 void push_current_class (TypeContainer tc, object partial_token)
5991 if (RootContext.EvalMode){
5992 tc.ModFlags = (tc.ModFlags & ~(Modifiers.PRIVATE|Modifiers.INTERNAL)) | Modifiers.PUBLIC;
5993 undo.AddTypeContainer (current_container, tc);
5996 if (partial_token != null)
5997 current_container = current_container.AddPartial (tc);
5999 current_container = current_container.AddTypeContainer (tc);
6001 ++lexer.parsing_declaration;
6005 DeclSpace pop_current_class ()
6007 DeclSpace retval = current_class;
6009 current_class = current_class.Parent;
6010 current_container = current_class.PartialContainer;
6016 // Given the @class_name name, it creates a fully qualified name
6017 // based on the containing declaration space
6020 MakeName (MemberName class_name)
6022 Namespace ns = current_namespace.NS;
6024 if (current_container == RootContext.ToplevelTypes) {
6025 if (ns.Name.Length != 0)
6026 return new MemberName (ns.MemberName, class_name);
6030 return new MemberName (current_container.MemberName, class_name);
6034 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
6036 Block implicit_block;
6039 // If we are doing interactive editing, we want variable declarations
6040 // that are in the top block to be added instead to the class as
6043 if (RootContext.StatementMode){
6046 for (Block b = current_block; b != null; b = b.Parent){
6047 if (b is ExplicitBlock && !(b is ToplevelBlock)){
6048 // There has been an explicit block, we cant add to the class
6055 // We can use "current_block" since we know there are no explicit blocks
6057 foreach (VariableDeclaration decl in variable_declarators){
6058 // We can not use the super-handy f.Initializer, because
6059 // multiple lines would force code to be executed out of sync
6060 if (decl.expression_or_array_initializer != null){
6061 string id = "$" + decl.identifier;
6062 LocalInfo vi = current_block.AddVariable (type, id, decl.Location);
6064 // Avoid warning about this variable not being used.
6067 LocalVariableReference var;
6068 var = new LocalVariableReferenceWithClassSideEffect (current_container, decl.identifier, current_block, id, vi, decl.Location);
6069 Assign assign = new SimpleAssign (var, decl.expression_or_array_initializer, decl.Location);
6070 current_block.AddStatement (new StatementExpression (assign));
6071 assign = new SimpleAssign (new SimpleName (decl.identifier, decl.Location), var);
6072 current_block.AddStatement (new StatementExpression (assign));
6074 Field f = new Field (current_container, (FullNamedExpression) type, Modifiers.PUBLIC | Modifiers.STATIC,
6075 new MemberName (decl.identifier, loc), null);
6076 current_container.AddField (f);
6078 // Register the field to be visible later as a global variable
6079 Evaluator.QueueField (f);
6083 return current_block;
6088 // We use the `Used' property to check whether statements
6089 // have been added to the current block. If so, we need
6090 // to create another block to contain the new declaration
6091 // otherwise, as an optimization, we use the same block to
6092 // add the declaration.
6094 // FIXME: A further optimization is to check if the statements
6095 // that were added were added as part of the initialization
6096 // below. In which case, no other statements have been executed
6097 // and we might be able to reduce the number of blocks for
6098 // situations like this:
6100 // int j = 1; int k = j + 1;
6102 if (current_block.Used)
6103 implicit_block = new Block (current_block, loc, lexer.Location);
6105 implicit_block = current_block;
6107 foreach (VariableDeclaration decl in variable_declarators){
6109 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
6110 if (decl.expression_or_array_initializer != null){
6112 Expression expr = decl.expression_or_array_initializer;
6114 var lvr = new LocalVariableReference (implicit_block, decl.identifier, loc);
6116 assign = new SimpleAssign (lvr, expr, decl.Location);
6118 implicit_block.AddStatement (new StatementExpression (assign));
6123 return implicit_block;
6126 Block declare_local_constants (Expression type, ArrayList declarators)
6128 Block implicit_block;
6130 if (current_block.Used)
6131 implicit_block = new Block (current_block);
6133 implicit_block = current_block;
6135 foreach (VariableDeclaration decl in declarators){
6136 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
6139 return implicit_block;
6142 string CheckAttributeTarget (string a, Location l)
6145 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
6149 Report.Warning (658, 1, l,
6150 "`{0}' is invalid attribute target. All attributes in this attribute section will be ignored", a);
6151 return string.Empty;
6154 static bool IsUnaryOperator (Operator.OpType op)
6158 case Operator.OpType.LogicalNot:
6159 case Operator.OpType.OnesComplement:
6160 case Operator.OpType.Increment:
6161 case Operator.OpType.Decrement:
6162 case Operator.OpType.True:
6163 case Operator.OpType.False:
6164 case Operator.OpType.UnaryPlus:
6165 case Operator.OpType.UnaryNegation:
6171 void syntax_error (Location l, string msg)
6173 Report.Error (1003, l, "Syntax error, " + msg);
6178 public Tokenizer Lexer {
6184 static CSharpParser ()
6186 oob_stack = new Stack ();
6189 public CSharpParser (SeekableStreamReader reader, CompilationUnit file, CompilerContext ctx)
6191 if (RootContext.EvalMode)
6195 this.compiler = ctx;
6196 current_namespace = new NamespaceEntry (null, file, null);
6197 current_class = current_namespace.SlaveDeclSpace;
6198 current_container = current_class.PartialContainer; // == RootContest.ToplevelTypes
6200 lexer = new Tokenizer (reader, file, ctx);
6202 use_global_stacks = true;
6205 public void parse ()
6207 eof_token = Token.EOF;
6208 Tokenizer.LocatedToken.Initialize ();
6211 if (yacc_verbose_flag > 1)
6212 yyparse (lexer, new yydebug.yyDebugSimple ());
6216 Tokenizer tokenizer = lexer as Tokenizer;
6217 tokenizer.cleanup ();
6218 } catch (Exception e){
6219 if (e is yyParser.yyUnexpectedEof)
6220 UnexpectedEOF = true;
6222 if (e is yyParser.yyException)
6223 Report.Error (-25, lexer.Location, "Parsing error");
6224 else if (yacc_verbose_flag > 0)
6225 throw; // Used by compiler-tester to test internal errors
6227 Report.Error (589, lexer.Location, "Internal compiler error during parsing");
6230 if (RootContext.ToplevelTypes.NamespaceEntry != null)
6231 throw new InternalErrorException ("who set it?");
6234 void CheckToken (int error, int yyToken, string msg, Location loc)
6236 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
6237 Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, GetTokenName (yyToken));
6239 Report.Error (error, loc, msg);
6242 void CheckIdentifierToken (int yyToken, Location loc)
6244 CheckToken (1041, yyToken, "Identifier expected", loc);
6247 string ConsumeStoredComment ()
6249 string s = tmpComment;
6251 Lexer.doc_state = XmlCommentState.Allowed;
6255 Location GetLocation (object obj)
6257 if (obj is Tokenizer.LocatedToken)
6258 return ((Tokenizer.LocatedToken) obj).Location;
6259 if (obj is MemberName)
6260 return ((MemberName) obj).Location;
6262 return lexer.Location;
6266 get { return compiler.Report; }
6269 void start_block (Location loc)
6271 if (current_block == null || parsing_anonymous_method) {
6272 current_block = new ToplevelBlock (compiler, current_block, current_local_parameters, current_generic_method, loc);
6273 parsing_anonymous_method = false;
6275 current_block = new ExplicitBlock (current_block, loc, Location.Null);
6280 end_block (Location loc)
6282 Block retval = current_block.Explicit;
6283 retval.SetEndLocation (loc);
6284 current_block = retval.Parent;
6289 start_anonymous (bool lambda, ParametersCompiled parameters, Location loc)
6291 if (RootContext.Version == LanguageVersion.ISO_1){
6292 Report.FeatureIsNotAvailable (loc, "anonymous methods");
6295 oob_stack.Push (current_anonymous_method);
6296 oob_stack.Push (current_local_parameters);
6298 current_local_parameters = parameters;
6300 current_anonymous_method = lambda
6301 ? new LambdaExpression (loc)
6302 : new AnonymousMethodExpression (loc);
6304 // Force the next block to be created as a ToplevelBlock
6305 parsing_anonymous_method = true;
6309 * Completes the anonymous method processing, if lambda_expr is null, this
6310 * means that we have a Statement instead of an Expression embedded
6312 AnonymousMethodExpression end_anonymous (ToplevelBlock anon_block)
6314 AnonymousMethodExpression retval;
6316 current_anonymous_method.Block = anon_block;
6317 retval = current_anonymous_method;
6319 current_local_parameters = (ParametersCompiled) oob_stack.Pop ();
6320 current_anonymous_method = (AnonymousMethodExpression) oob_stack.Pop ();
6325 public NamespaceEntry CurrentNamespace {
6327 return current_namespace;
6332 void Error_SyntaxError (int token)
6334 Error_SyntaxError (0, token, "Unexpected symbol");
6337 void Error_SyntaxError (int error_code, int token, string msg)
6339 string symbol = GetSymbolName (token);
6340 string expecting = GetExpecting ();
6342 if (error_code == 0) {
6343 if (expecting == "`)'")
6349 if (expecting != null)
6350 Report.Error (error_code, lexer.Location, "{2} `{0}', expecting {1}",
6351 symbol, expecting, msg);
6353 Report.Error (error_code, lexer.Location, "{1} `{0}'", symbol, msg);
6356 string GetExpecting ()
6358 int [] tokens = yyExpectingTokens (yyExpectingState);
6359 ArrayList names = new ArrayList (tokens.Length);
6360 bool has_type = false;
6361 bool has_identifier = false;
6362 for (int i = 0; i < tokens.Length; i++){
6363 int token = tokens [i];
6364 has_identifier |= token == Token.IDENTIFIER;
6366 string name = GetTokenName (token);
6367 if (name == "<internal>")
6370 has_type |= name == "type";
6371 if (names.Contains (name))
6378 // Too many tokens to enumerate
6380 if (names.Count > 8)
6383 if (has_type && has_identifier)
6384 names.Remove ("identifier");
6386 if (names.Count == 1)
6387 return "`" + GetTokenName (tokens [0]) + "'";
6389 StringBuilder sb = new StringBuilder ();
6391 int count = names.Count;
6392 for (int i = 0; i < count; i++){
6393 bool last = i + 1 == count;
6397 sb.Append (names [i]);
6398 sb.Append (last ? "'" : count < 3 ? "' " : "', ");
6400 return sb.ToString ();
6404 string GetSymbolName (int token)
6408 return ((Constant)lexer.Value).GetValue ().ToString ();
6409 case Token.IDENTIFIER:
6410 return ((Tokenizer.LocatedToken)lexer.Value).Value;
6452 case Token.BITWISE_AND:
6454 case Token.BITWISE_OR:
6468 case Token.OP_SHIFT_LEFT:
6470 case Token.OP_SHIFT_RIGHT:
6490 case Token.OP_COALESCING:
6492 case Token.OP_MULT_ASSIGN:
6494 case Token.OP_DIV_ASSIGN:
6496 case Token.OP_MOD_ASSIGN:
6498 case Token.OP_ADD_ASSIGN:
6500 case Token.OP_SUB_ASSIGN:
6502 case Token.OP_SHIFT_LEFT_ASSIGN:
6504 case Token.OP_SHIFT_RIGHT_ASSIGN:
6506 case Token.OP_AND_ASSIGN:
6508 case Token.OP_XOR_ASSIGN:
6510 case Token.OP_OR_ASSIGN:
6514 return GetTokenName (token);
6517 static string GetTokenName (int token)
6520 case Token.ABSTRACT:
6540 case Token.CONTINUE:
6544 case Token.DELEGATE:
6554 case Token.EXPLICIT:
6572 case Token.IMPLICIT:
6576 case Token.INTERFACE:
6578 case Token.INTERNAL:
6584 case Token.NAMESPACE:
6590 case Token.OPERATOR:
6594 case Token.OVERRIDE:
6600 case Token.PROTECTED:
6604 case Token.READONLY:
6616 case Token.STACKALLOC:
6617 return "stackalloc";
6634 case Token.UNCHECKED:
6642 case Token.VOLATILE:
6655 case Token.FROM_FIRST:
6673 case Token.ASCENDING:
6675 case Token.DESCENDING:
6676 return "descending";
6683 case Token.OPEN_BRACE:
6685 case Token.CLOSE_BRACE:
6687 case Token.OPEN_BRACKET:
6689 case Token.CLOSE_BRACKET:
6691 case Token.OPEN_PARENS_CAST:
6692 case Token.OPEN_PARENS_LAMBDA:
6693 case Token.OPEN_PARENS:
6695 case Token.CLOSE_PARENS:
6701 case Token.DEFAULT_COLON:
6705 case Token.SEMICOLON:
6716 case Token.BITWISE_AND:
6717 case Token.BITWISE_OR:
6724 case Token.OP_SHIFT_LEFT:
6725 case Token.OP_SHIFT_RIGHT:
6733 case Token.OP_COALESCING:
6734 case Token.OP_MULT_ASSIGN:
6735 case Token.OP_DIV_ASSIGN:
6736 case Token.OP_MOD_ASSIGN:
6737 case Token.OP_ADD_ASSIGN:
6738 case Token.OP_SUB_ASSIGN:
6739 case Token.OP_SHIFT_LEFT_ASSIGN:
6740 case Token.OP_SHIFT_RIGHT_ASSIGN:
6741 case Token.OP_AND_ASSIGN:
6742 case Token.OP_XOR_ASSIGN:
6743 case Token.OP_OR_ASSIGN:
6744 return "<operator>";
6766 case Token.OP_GENERICS_LT:
6767 case Token.GENERIC_DIMENSION:
6769 case Token.OP_GENERICS_GT:
6772 case Token.INTERR_NULLABLE:
6774 case Token.DOUBLE_COLON:
6778 case Token.IDENTIFIER:
6779 return "identifier";
6781 // All of these are internal.
6784 case Token.FIRST_KEYWORD:
6786 case Token.EVAL_COMPILATION_UNIT_PARSER:
6787 case Token.EVAL_USING_DECLARATIONS_UNIT_PARSER:
6788 case Token.EVAL_STATEMENT_PARSER:
6789 case Token.LAST_KEYWORD:
6790 case Token.GENERATE_COMPLETION:
6791 case Token.COMPLETE_COMPLETION:
6792 return "<internal>";
6794 // A bit more robust.
6796 return yyNames [token];