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:
23 using System.Collections.Generic;
30 public class CSharpParser
33 enum ParameterModifierType
40 DefaultValue = 1 << 6,
42 All = Ref | Out | This | Params | Arglist | DefaultValue
45 NamespaceEntry current_namespace;
46 TypeContainer current_container;
47 DeclSpace current_class;
50 /// Current block is used to add statements as we find
55 Delegate current_delegate;
57 AnonymousMethodExpression current_anonymous_method;
60 /// This is used by the unary_expression code to resolve
61 /// a name against a parameter.
64 // FIXME: This is very ugly and it's very hard to reset it correctly
65 // on all places, especially when some parameters are autogenerated.
66 ParametersCompiled current_local_parameters;
69 /// Using during property parsing to describe the implicit
70 /// value parameter that is passed to the "set" and "get"accesor
71 /// methods (properties and indexers).
73 FullNamedExpression implicit_value_parameter_type;
74 ParametersCompiled indexer_parameters;
77 /// Used to determine if we are parsing the get/set pair
78 /// of an indexer or a property
82 bool parsing_anonymous_method;
85 /// An out-of-band stack.
87 static Stack<object> oob_stack;
92 Stack<Block> switch_stack;
95 /// Controls the verbosity of the errors produced by the parser
97 static public int yacc_verbose_flag;
100 /// Used by the interactive shell, flags whether EOF was reached
101 /// and an error was produced
103 public bool UnexpectedEOF;
106 /// The current file.
108 CompilationUnit file;
111 /// Temporary Xml documentation cache.
112 /// For enum types, we need one more temporary store.
115 string enumTypeComment;
117 /// Current attribute target
118 string current_attr_target;
120 /// assembly and module attribute definitions are enabled
121 bool global_attrs_enabled = true;
122 bool has_get, has_set;
124 ParameterModifierType valid_param_mod;
126 bool default_parameter_used;
128 /// When using the interactive parser, this holds the
129 /// resulting expression
130 public object InteractiveResult;
133 // Keeps track of global data changes to undo on parser error
137 Stack<Linq.QueryBlock> linq_clause_blocks;
139 // A counter to create new class names in interactive mode
140 static int class_count;
142 CompilerContext compiler;
145 // Instead of allocating carrier array everytime we
146 // share the bucket for very common constructs which can never
149 static List<Parameter> parameters_bucket = new List<Parameter> (6);
150 static List<object> variables_bucket = new List<object> (6);
154 %token NONE /* This token is never returned by our lexer */
155 %token ERROR // This is used not by the parser, but by the tokenizer.
159 *These are the C# keywords
258 %token INTERR_NULLABLE
261 /* Generics <,> tokens */
262 %token OP_GENERICS_LT
263 %token OP_GENERICS_LT_DECL
264 %token OP_GENERICS_GT
266 /* C# keywords which are not really keywords */
272 /* C# single character operators/punctuation. */
300 /* C# multi-character operators. */
305 %token OP_SHIFT_RIGHT
312 %token OP_MULT_ASSIGN
317 %token OP_SHIFT_LEFT_ASSIGN
318 %token OP_SHIFT_RIGHT_ASSIGN
328 %token OPEN_PARENS_LAMBDA
329 %token OPEN_PARENS_CAST
330 %token GENERIC_DIMENSION
333 // Make the parser go into eval mode parsing (statements and compilation units).
334 %token EVAL_STATEMENT_PARSER
335 %token EVAL_COMPILATION_UNIT_PARSER
336 %token EVAL_USING_DECLARATIONS_UNIT_PARSER
339 // This token is generated to trigger the completion engine at this point
341 %token GENERATE_COMPLETION
344 // This token is return repeatedly after the first GENERATE_COMPLETION
345 // token is produced and before the final EOF
347 %token COMPLETE_COMPLETION
349 /* Add precedence rules to solve dangling else s/r conflict */
353 /* Define the operator tokens and their precedences */
361 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
363 %left STAR DIV PERCENT
364 %right BANG CARRET UMINUS
365 %nonassoc OP_INC OP_DEC
367 %left OPEN_BRACKET OPEN_BRACE
370 %start compilation_unit
374 : outer_declarations opt_EOF
375 | outer_declarations global_attributes opt_EOF
376 | global_attributes opt_EOF
377 | opt_EOF /* allow empty files */
378 | interactive_parsing { Lexer.CompleteOnEOF = false; } opt_EOF
384 Lexer.check_incorrect_doc_comment ();
388 Lexer.check_incorrect_doc_comment ();
394 | outer_declarations outer_declaration
398 : extern_alias_directive
400 | namespace_member_declaration
403 extern_alias_directives
404 : extern_alias_directive
405 | extern_alias_directives extern_alias_directive
408 extern_alias_directive
409 : EXTERN_ALIAS IDENTIFIER IDENTIFIER SEMICOLON
411 var lt = (Tokenizer.LocatedToken) $2;
414 syntax_error (lt.Location, "`alias' expected");
415 } else if (RootContext.Version == LanguageVersion.ISO_1) {
416 Report.FeatureIsNotAvailable (lt.Location, "external alias");
418 lt = (Tokenizer.LocatedToken) $3;
419 current_namespace.AddUsingExternalAlias (lt.Value, lt.Location, Report);
424 syntax_error (GetLocation ($1), "`alias' expected"); // TODO: better
430 | using_directives using_directive
434 : using_alias_directive
436 if (RootContext.Documentation != null)
437 Lexer.doc_state = XmlCommentState.Allowed;
439 | using_namespace_directive
441 if (RootContext.Documentation != null)
442 Lexer.doc_state = XmlCommentState.Allowed;
446 using_alias_directive
447 : USING IDENTIFIER ASSIGN namespace_or_type_name SEMICOLON
449 var lt = (Tokenizer.LocatedToken) $2;
450 current_namespace.AddUsingAlias (lt.Value, (MemberName) $4, GetLocation ($1));
453 CheckIdentifierToken (yyToken, GetLocation ($2));
458 using_namespace_directive
459 : USING namespace_name SEMICOLON
461 current_namespace.AddUsing ((MemberName) $2, GetLocation ($1));
466 // Strictly speaking, namespaces don't have attributes but
467 // we parse global attributes along with namespace declarations and then
470 namespace_declaration
471 : opt_attributes NAMESPACE qualified_identifier
473 MemberName name = (MemberName) $3;
476 Report.Error(1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
479 current_namespace = new NamespaceEntry (
480 current_namespace, file, name.GetName ());
481 current_class = current_namespace.SlaveDeclSpace;
482 current_container = current_class.PartialContainer;
484 namespace_body opt_semicolon
486 current_namespace = current_namespace.Parent;
487 current_class = current_namespace.SlaveDeclSpace;
488 current_container = current_class.PartialContainer;
495 var lt = (Tokenizer.LocatedToken) $1;
496 $$ = new MemberName (lt.Value, lt.Location);
498 | qualified_identifier DOT IDENTIFIER
500 var lt = (Tokenizer.LocatedToken) $3;
501 $$ = new MemberName ((MemberName) $1, lt.Value, lt.Location);
505 syntax_error (lexer.Location, "`.' expected");
506 $$ = new MemberName ("<invalid>", lexer.Location);
521 : namespace_or_type_name
523 MemberName name = (MemberName) $1;
525 if (name.TypeArguments != null)
526 syntax_error (lexer.Location, "namespace name expected");
535 if (RootContext.Documentation != null)
536 Lexer.doc_state = XmlCommentState.Allowed;
542 : opt_extern_alias_directives
544 opt_namespace_member_declarations
548 Report.Error (1518, lexer.Location, "Expected `class', `delegate', `enum', `interface', or `struct'");
551 | opt_extern_alias_directives
553 opt_namespace_member_declarations
556 Report.Error (1513, lexer.Location, "Expected `}'");
565 opt_extern_alias_directives
567 | extern_alias_directives
570 opt_namespace_member_declarations
572 | namespace_member_declarations
575 namespace_member_declarations
576 : namespace_member_declaration
577 | namespace_member_declarations namespace_member_declaration
580 namespace_member_declaration
584 DeclSpace ds = (DeclSpace)$1;
586 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
587 Report.Error (1527, ds.Location,
588 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
591 current_namespace.DeclarationFound = true;
593 | namespace_declaration {
594 current_namespace.DeclarationFound = true;
597 | field_declaration {
598 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
600 | method_declaration {
601 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
608 | interface_declaration
610 | delegate_declaration
612 // Enable this when we have handled all errors, because this acts as a generic fallback
615 // Console.WriteLine ("Token=" + yyToken);
616 // Report.Error (1518, GetLocation ($1), "Expected class, struct, interface, enum or delegate");
628 Attributes attrs = (Attributes)$1;
629 if (global_attrs_enabled) {
630 CodeGen.Assembly.AddAttributes (attrs.Attrs, current_namespace);
632 foreach (Attribute a in attrs.Attrs) {
633 Report.Error (1730, a.Location, "Assembly and module attributes must precede all other elements except using clauses and extern alias declarations");
644 global_attrs_enabled = false;
649 global_attrs_enabled = false;
658 if (current_attr_target != String.Empty) {
659 var sect = (List<Attribute>) $1;
661 if (global_attrs_enabled) {
662 if (current_attr_target == "module") {
663 current_container.Module.Compiled.AddAttributes (sect);
665 } else if (current_attr_target != null && current_attr_target.Length > 0) {
666 CodeGen.Assembly.AddAttributes (sect, current_namespace);
669 $$ = new Attributes (sect);
672 if (RootContext.Documentation != null) {
673 Lexer.check_incorrect_doc_comment ();
675 XmlCommentState.Allowed;
679 $$ = new Attributes (sect);
684 current_attr_target = null;
686 | attribute_sections attribute_section
688 if (current_attr_target != String.Empty) {
689 Attributes attrs = $1 as Attributes;
690 var sect = (List<Attribute>) $2;
692 if (global_attrs_enabled) {
693 if (current_attr_target == "module") {
694 current_container.Module.Compiled.AddAttributes (sect);
696 } else if (current_attr_target == "assembly") {
697 CodeGen.Assembly.AddAttributes (sect, current_namespace);
701 attrs = new Attributes (sect);
703 attrs.AddAttributes (sect);
707 attrs = new Attributes (sect);
709 attrs.AddAttributes (sect);
715 current_attr_target = null;
720 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
724 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
730 attribute_target_specifier
731 : attribute_target COLON
733 current_attr_target = (string)$1;
741 var lt = (Tokenizer.LocatedToken) $1;
742 $$ = CheckAttributeTarget (lt.Value, lt.Location);
744 | EVENT { $$ = "event"; }
745 | RETURN { $$ = "return"; }
748 string name = GetTokenName (yyToken);
749 $$ = CheckAttributeTarget (name, GetLocation ($1));
756 $$ = new List<Attribute> (4) { (Attribute) $1 };
758 | attribute_list COMMA attribute
760 var attrs = (List<Attribute>) $1;
761 attrs.Add ((Attribute) $3);
770 ++lexer.parsing_block;
772 opt_attribute_arguments
774 --lexer.parsing_block;
775 MemberName mname = (MemberName) $1;
776 if (mname.IsGeneric) {
777 Report.Error (404, lexer.Location,
778 "'<' unexpected: attributes cannot be generic");
781 Arguments [] arguments = (Arguments []) $3;
782 ATypeNameExpression expr = mname.GetTypeExpression ();
784 if (current_attr_target == String.Empty)
786 else if (global_attrs_enabled && (current_attr_target == "assembly" || current_attr_target == "module"))
787 // FIXME: supply "nameEscaped" parameter here.
788 $$ = new GlobalAttribute (current_namespace, current_attr_target,
789 expr, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
791 $$ = new Attribute (current_attr_target, expr, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
796 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
799 opt_attribute_arguments
800 : /* empty */ { $$ = null; }
801 | OPEN_PARENS attribute_arguments CLOSE_PARENS
809 : /* empty */ { $$ = null; }
810 | positional_or_named_argument
812 Arguments a = new Arguments (4);
813 a.Add ((Argument) $1);
814 $$ = new Arguments [] { a, null };
816 | named_attribute_argument
818 Arguments a = new Arguments (4);
819 a.Add ((Argument) $1);
820 $$ = new Arguments [] { null, a };
822 | attribute_arguments COMMA positional_or_named_argument
824 Arguments[] o = (Arguments[]) $1;
826 Report.Error (1016, ((Argument) $3).Expr.Location, "Named attribute arguments must appear after the positional arguments");
827 o [0] = new Arguments (4);
830 Arguments args = ((Arguments) o [0]);
831 if (args.Count > 0 && !($3 is NamedArgument) && args [args.Count - 1] is NamedArgument)
832 Error_NamedArgumentExpected ((NamedArgument) args [args.Count - 1]);
834 args.Add ((Argument) $3);
836 | attribute_arguments COMMA named_attribute_argument
838 Arguments[] o = (Arguments[]) $1;
840 o [1] = new Arguments (4);
843 ((Arguments) o [1]).Add ((Argument) $3);
847 positional_or_named_argument
850 $$ = new Argument ((Expression) $1);
855 named_attribute_argument
856 : IDENTIFIER ASSIGN expression
858 var lt = (Tokenizer.LocatedToken) $1;
859 $$ = new NamedArgument (lt.Value, lt.Location, (Expression) $3);
864 : IDENTIFIER COLON opt_named_modifier expression
866 if (RootContext.Version <= LanguageVersion.V_3)
867 Report.FeatureIsNotAvailable (GetLocation ($1), "named argument");
869 // Avoid boxing in common case (no modifier)
870 var arg_mod = $3 == null ? Argument.AType.None : (Argument.AType) $3;
872 var lt = (Tokenizer.LocatedToken) $1;
873 $$ = new NamedArgument (lt.Value, lt.Location, (Expression) $4, arg_mod);
878 : /* empty */ { $$ = null; }
881 $$ = Argument.AType.Ref;
885 $$ = Argument.AType.Out;
890 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
893 opt_class_member_declarations
895 | class_member_declarations
898 class_member_declarations
899 : class_member_declaration
900 | class_member_declarations
901 class_member_declaration
904 class_member_declaration
905 : constant_declaration // done
906 | field_declaration // done
907 | method_declaration // done
908 | property_declaration // done
909 | event_declaration // done
910 | indexer_declaration // done
911 | operator_declaration // done
912 | constructor_declaration // done
913 | destructor_declaration // done
917 Report.Error (1519, lexer.Location, "Unexpected symbol `{0}' in class, struct, or interface member declaration",
918 GetSymbolName (yyToken));
920 lexer.parsing_generic_declaration = false;
930 lexer.ConstraintsParsing = true;
932 type_declaration_name
934 MemberName name = MakeName ((MemberName) $6);
935 push_current_class (new Struct (current_namespace, current_class, name, (Modifiers) $2, (Attributes) $1), $3);
938 opt_type_parameter_constraints_clauses
940 lexer.ConstraintsParsing = false;
942 current_class.SetParameterInfo ((List<Constraints>) $9);
944 if (RootContext.Documentation != null)
945 current_container.DocComment = Lexer.consume_doc_comment ();
949 --lexer.parsing_declaration;
950 if (RootContext.Documentation != null)
951 Lexer.doc_state = XmlCommentState.Allowed;
955 $$ = pop_current_class ();
957 | opt_attributes opt_modifiers opt_partial STRUCT error {
958 CheckIdentifierToken (yyToken, GetLocation ($5));
965 if (RootContext.Documentation != null)
966 Lexer.doc_state = XmlCommentState.Allowed;
968 opt_struct_member_declarations CLOSE_BRACE
971 opt_struct_member_declarations
973 | struct_member_declarations
976 struct_member_declarations
977 : struct_member_declaration
978 | struct_member_declarations struct_member_declaration
981 struct_member_declaration
982 : constant_declaration
985 | property_declaration
987 | indexer_declaration
988 | operator_declaration
989 | constructor_declaration
993 * This is only included so we can flag error 575:
994 * destructors only allowed on class types
996 | destructor_declaration
1004 constant_declarators
1007 var modflags = (Modifiers) $2;
1008 foreach (VariableDeclaration constant in (List<object>) $5){
1009 Location l = constant.Location;
1010 if ((modflags & Modifiers.STATIC) != 0) {
1011 Report.Error (504, l, "The constant `{0}' cannot be marked static", current_container.GetSignatureForError () + "." + (string) constant.identifier);
1015 Const c = new Const (
1016 current_class, (FullNamedExpression) $4, (string) constant.identifier,
1017 constant.GetInitializer ((FullNamedExpression) $4), modflags,
1018 (Attributes) $1, l);
1020 if (RootContext.Documentation != null) {
1021 c.DocComment = Lexer.consume_doc_comment ();
1022 Lexer.doc_state = XmlCommentState.Allowed;
1024 current_container.AddConstant (c);
1029 constant_declarators
1030 : constant_declarator
1032 variables_bucket.Clear ();
1034 variables_bucket.Add ($1);
1035 $$ = variables_bucket;
1037 | constant_declarators COMMA constant_declarator
1040 var constants = (List<object>) $1;
1049 ++lexer.parsing_block;
1051 constant_initializer
1053 --lexer.parsing_block;
1054 $$ = new VariableDeclaration ((Tokenizer.LocatedToken) $1, (Expression) $4);
1058 // A const field requires a value to be provided
1059 Report.Error (145, GetLocation ($1), "A const field requires a value to be provided");
1064 constant_initializer
1065 : constant_expression
1073 variable_declarators
1076 FullNamedExpression type = (FullNamedExpression) $3;
1077 if (type == TypeManager.system_void_expr)
1078 Report.Error (670, GetLocation ($3), "Fields cannot have void type");
1080 var mod = (Modifiers) $2;
1082 foreach (VariableMemberDeclaration var in (List<object>) $4){
1083 Field field = new Field (current_class, type, mod, var.MemberName, (Attributes) $1);
1085 field.Initializer = var.GetInitializer (type);
1087 if (RootContext.Documentation != null) {
1088 field.DocComment = Lexer.consume_doc_comment ();
1089 Lexer.doc_state = XmlCommentState.Allowed;
1091 current_container.AddField (field);
1092 $$ = field; // FIXME: might be better if it points to the top item
1099 fixed_variable_declarators
1102 FullNamedExpression type = (FullNamedExpression) $4;
1104 var mod = (Modifiers) $2;
1106 foreach (VariableDeclaration var in (List<VariableDeclaration>) $5) {
1107 FixedField field = new FixedField (current_class, type, mod, var.identifier,
1108 var.GetInitializer (type), (Attributes) $1, var.Location);
1110 if (RootContext.Version < LanguageVersion.ISO_2)
1111 Report.FeatureIsNotAvailable (GetLocation ($3), "fixed size buffers");
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 var decl = new List<VariableDeclaration> (2);
1135 decl.Add ((VariableDeclaration)$1);
1138 | fixed_variable_declarators COMMA fixed_variable_declarator
1140 var decls = (List<VariableDeclaration>) $1;
1141 decls.Add ((VariableDeclaration)$3);
1146 fixed_variable_declarator
1147 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
1149 $$ = new VariableDeclaration ((Tokenizer.LocatedToken) $1, (Expression) $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 var decls = (List<object>) $1;
1175 local_variable_declarator
1176 : IDENTIFIER ASSIGN local_variable_initializer
1178 $$ = new VariableDeclaration ((Tokenizer.LocatedToken) $1, (Expression) $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 var decls = (List<object>) $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, (Expression) $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 == MemberKind.Interface && method.Block != null) {
1272 Report.Error (531, method.Location, "`{0}': interface members cannot have a definition", method.GetSignatureForError ());
1275 current_local_parameters = null;
1277 if (RootContext.Documentation != null)
1278 Lexer.doc_state = XmlCommentState.Allowed;
1286 method_declaration_name OPEN_PARENS
1288 valid_param_mod = ParameterModifierType.All;
1290 opt_formal_parameter_list CLOSE_PARENS
1292 lexer.ConstraintsParsing = true;
1294 opt_type_parameter_constraints_clauses
1296 lexer.ConstraintsParsing = false;
1297 valid_param_mod = 0;
1298 MemberName name = (MemberName) $4;
1299 current_local_parameters = (ParametersCompiled) $7;
1301 GenericMethod generic = null;
1302 if (name.TypeArguments != null) {
1303 generic = new GenericMethod (current_namespace, current_class, name,
1304 (FullNamedExpression) $3, current_local_parameters);
1306 generic.SetParameterInfo ((List<Constraints>) $10);
1307 } else if ($10 != null) {
1308 Report.Error (80, GetLocation ($10),
1309 "Constraints are not allowed on non-generic declarations");
1312 Method method = new Method (current_class, generic, (FullNamedExpression) $3, (Modifiers) $2,
1313 name, current_local_parameters, (Attributes) $1);
1315 if ($10 != null && ((method.ModFlags & Modifiers.OVERRIDE) != 0 || method.IsExplicitImpl)) {
1316 Report.Error (460, method.Location,
1317 "`{0}': Cannot specify constraints for overrides and explicit interface implementation methods",
1318 method.GetSignatureForError ());
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 ((List<Constraints>) $11);
1359 var modifiers = (Modifiers) $2;
1362 const Modifiers invalid_partial_mod = Modifiers.AccessibilityMask | 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 if (RootContext.Documentation != null)
1382 method.DocComment = Lexer.consume_doc_comment ();
1389 modifiers method_declaration_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1391 MemberName name = (MemberName) $5;
1392 Report.Error (1585, name.Location,
1393 "Member modifier `{0}' must precede the member type and name", ModifiersExtensions.Name ((Modifiers) $4));
1395 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1396 0, name, (ParametersCompiled) $7, (Attributes) $1);
1398 current_local_parameters = (ParametersCompiled) $7;
1400 if (RootContext.Documentation != null)
1401 method.DocComment = Lexer.consume_doc_comment ();
1409 | SEMICOLON { $$ = null; }
1412 opt_formal_parameter_list
1413 : /* empty */ { $$ = ParametersCompiled.EmptyReadOnlyParameters; }
1414 | formal_parameter_list
1417 formal_parameter_list
1420 var pars_list = (List<Parameter>) $1;
1421 $$ = new ParametersCompiled (compiler, pars_list.ToArray ());
1423 | fixed_parameters COMMA parameter_array
1425 var pars_list = (List<Parameter>) $1;
1426 pars_list.Add ((Parameter) $3);
1428 $$ = new ParametersCompiled (compiler, pars_list.ToArray ());
1430 | fixed_parameters COMMA arglist_modifier
1432 var pars_list = (List<Parameter>) $1;
1433 pars_list.Add (new ArglistParameter (GetLocation ($3)));
1434 $$ = new ParametersCompiled (compiler, pars_list.ToArray (), true);
1436 | parameter_array COMMA error
1439 Report.Error (231, ((Parameter) $1).Location, "A params parameter must be the last parameter in a formal parameter list");
1441 $$ = new ParametersCompiled (compiler, new Parameter[] { (Parameter) $1 } );
1443 | fixed_parameters COMMA parameter_array COMMA error
1446 Report.Error (231, ((Parameter) $3).Location, "A params parameter must be the last parameter in a formal parameter list");
1448 var pars_list = (List<Parameter>) $1;
1449 pars_list.Add (new ArglistParameter (GetLocation ($3)));
1451 $$ = new ParametersCompiled (compiler, pars_list.ToArray (), true);
1453 | arglist_modifier COMMA error
1455 Report.Error (257, GetLocation ($1), "An __arglist parameter must be the last parameter in a formal parameter list");
1457 $$ = new ParametersCompiled (compiler, new Parameter [] { new ArglistParameter (GetLocation ($1)) }, true);
1459 | fixed_parameters COMMA ARGLIST COMMA error
1461 Report.Error (257, GetLocation ($3), "An __arglist parameter must be the last parameter in a formal parameter list");
1463 var pars_list = (List<Parameter>) $1;
1464 pars_list.Add (new ArglistParameter (GetLocation ($3)));
1466 $$ = new ParametersCompiled (compiler, pars_list.ToArray (), true);
1470 $$ = new ParametersCompiled (compiler, new Parameter[] { (Parameter) $1 } );
1474 $$ = new ParametersCompiled (compiler, new Parameter [] { new ArglistParameter (GetLocation ($1)) }, true);
1481 parameters_bucket.Clear ();
1482 Parameter p = (Parameter) $1;
1483 parameters_bucket.Add (p);
1485 default_parameter_used = p.HasDefaultValue;
1486 $$ = parameters_bucket;
1488 | fixed_parameters COMMA fixed_parameter
1490 var pars = (List<Parameter>) $1;
1491 Parameter p = (Parameter) $3;
1493 if (p.HasExtensionMethodModifier)
1494 Report.Error (1100, p.Location, "The parameter modifier `this' can only be used on the first parameter");
1495 else if (!p.HasDefaultValue && default_parameter_used)
1496 Report.Error (1737, p.Location, "Optional parameter cannot precede required parameters");
1498 default_parameter_used |= p.HasDefaultValue;
1507 opt_parameter_modifier
1511 var lt = (Tokenizer.LocatedToken) $4;
1512 $$ = new Parameter ((FullNamedExpression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1515 opt_parameter_modifier
1517 IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1519 var lt = (Tokenizer.LocatedToken) $4;
1520 Report.Error (1552, lt.Location, "Array type specifier, [], must appear before parameter name");
1521 $$ = new Parameter ((FullNamedExpression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1524 opt_parameter_modifier
1528 Location l = GetLocation ($4);
1529 CheckIdentifierToken (yyToken, l);
1530 $$ = new Parameter ((FullNamedExpression) $3, "NeedSomeGeneratorHere", (Parameter.Modifier) $2, (Attributes) $1, l);
1533 opt_parameter_modifier
1539 if (RootContext.Version <= LanguageVersion.V_3) {
1540 Report.FeatureIsNotAvailable (GetLocation ($5), "optional parameter");
1543 Parameter.Modifier mod = (Parameter.Modifier) $2;
1544 if (mod != Parameter.Modifier.NONE) {
1546 case Parameter.Modifier.REF:
1547 case Parameter.Modifier.OUT:
1548 Report.Error (1741, GetLocation ($2), "Cannot specify a default value for the `{0}' parameter",
1549 Parameter.GetModifierSignature (mod));
1552 case Parameter.Modifier.This:
1553 Report.Error (1743, GetLocation ($2), "Cannot specify a default value for the `{0}' parameter",
1554 Parameter.GetModifierSignature (mod));
1557 throw new NotImplementedException (mod.ToString ());
1560 mod = Parameter.Modifier.NONE;
1563 if ((valid_param_mod & ParameterModifierType.DefaultValue) == 0)
1564 Report.Error (1065, GetLocation ($6), "Optional parameter is not valid in this context");
1566 var lt = (Tokenizer.LocatedToken) $4;
1567 $$ = new Parameter ((FullNamedExpression) $3, lt.Value, mod, (Attributes) $1, lt.Location);
1569 ((Parameter) $$).DefaultValue = (Expression) $6;
1573 opt_parameter_modifier
1574 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1575 | parameter_modifiers
1579 : parameter_modifier
1583 | parameter_modifiers parameter_modifier
1585 Parameter.Modifier p2 = (Parameter.Modifier)$2;
1586 Parameter.Modifier mod = (Parameter.Modifier)$1 | p2;
1587 if (((Parameter.Modifier)$1 & p2) == p2) {
1588 Error_DuplicateParameterModifier (lexer.Location, p2);
1590 switch (mod & ~Parameter.Modifier.This) {
1591 case Parameter.Modifier.REF:
1592 Report.Error (1101, lexer.Location, "The parameter modifiers `this' and `ref' cannot be used altogether");
1594 case Parameter.Modifier.OUT:
1595 Report.Error (1102, lexer.Location, "The parameter modifiers `this' and `out' cannot be used altogether");
1598 Report.Error (1108, lexer.Location, "A parameter cannot have specified more than one modifier");
1609 if ((valid_param_mod & ParameterModifierType.Ref) == 0)
1610 Error_ParameterModifierNotValid ("ref", GetLocation ($1));
1612 $$ = Parameter.Modifier.REF;
1616 if ((valid_param_mod & ParameterModifierType.Out) == 0)
1617 Error_ParameterModifierNotValid ("out", GetLocation ($1));
1619 $$ = Parameter.Modifier.OUT;
1623 if ((valid_param_mod & ParameterModifierType.This) == 0)
1624 Error_ParameterModifierNotValid ("this", GetLocation ($1));
1626 if (RootContext.Version <= LanguageVersion.ISO_2)
1627 Report.FeatureIsNotAvailable (GetLocation ($1), "extension methods");
1629 $$ = Parameter.Modifier.This;
1634 : opt_attributes params_modifier type IDENTIFIER
1636 var lt = (Tokenizer.LocatedToken) $4;
1637 $$ = new ParamsParameter ((FullNamedExpression) $3, lt.Value, (Attributes) $1, lt.Location);
1639 | opt_attributes params_modifier type IDENTIFIER ASSIGN constant_expression
1641 Report.Error (1751, GetLocation ($2), "Cannot specify a default value for a parameter array");
1643 var lt = (Tokenizer.LocatedToken) $4;
1644 $$ = new ParamsParameter ((FullNamedExpression) $3, lt.Value, (Attributes) $1, lt.Location);
1646 | opt_attributes params_modifier type error {
1647 CheckIdentifierToken (yyToken, GetLocation ($4));
1655 if ((valid_param_mod & ParameterModifierType.Params) == 0)
1656 Report.Error (1670, (GetLocation ($1)), "The `params' modifier is not allowed in current context");
1658 | PARAMS parameter_modifier
1660 Parameter.Modifier mod = (Parameter.Modifier)$2;
1661 if ((mod & Parameter.Modifier.This) != 0) {
1662 Report.Error (1104, GetLocation ($1), "The parameter modifiers `this' and `params' cannot be used altogether");
1664 Report.Error (1611, GetLocation ($1), "The params parameter cannot be declared as ref or out");
1667 | PARAMS params_modifier
1669 Error_DuplicateParameterModifier (GetLocation ($1), Parameter.Modifier.PARAMS);
1676 if ((valid_param_mod & ParameterModifierType.Arglist) == 0)
1677 Report.Error (1669, GetLocation ($1), "__arglist is not valid in this context");
1681 property_declaration
1685 member_declaration_name
1687 if (RootContext.Documentation != null)
1688 tmpComment = Lexer.consume_doc_comment ();
1692 implicit_value_parameter_type = (FullNamedExpression) $3;
1693 lexer.PropertyParsing = true;
1695 accessor_declarations
1697 lexer.PropertyParsing = false;
1698 has_get = has_set = false;
1703 Accessors accessors = (Accessors) $8;
1704 Accessor get_block = accessors != null ? accessors.get_or_add : null;
1705 Accessor set_block = accessors != null ? accessors.set_or_remove : null;
1706 bool order = accessors != null ? accessors.declared_in_reverse : false;
1708 MemberName name = (MemberName) $4;
1709 FullNamedExpression ptype = (FullNamedExpression) $3;
1711 prop = new Property (current_class, ptype, (Modifiers) $2,
1712 name, (Attributes) $1, get_block, set_block, order, current_block);
1714 if (ptype == TypeManager.system_void_expr)
1715 Report.Error (547, name.Location, "`{0}': property or indexer cannot have void type", prop.GetSignatureForError ());
1717 if (accessors == null)
1718 Report.Error (548, prop.Location, "`{0}': property or indexer must have at least one accessor", prop.GetSignatureForError ());
1720 if (current_container.Kind == MemberKind.Interface) {
1721 if (prop.Get.Block != null)
1722 Report.Error (531, prop.Location, "`{0}.get': interface members cannot have a definition", prop.GetSignatureForError ());
1724 if (prop.Set.Block != null)
1725 Report.Error (531, prop.Location, "`{0}.set': interface members cannot have a definition", prop.GetSignatureForError ());
1728 current_container.AddProperty (prop);
1729 implicit_value_parameter_type = null;
1731 if (RootContext.Documentation != null)
1732 prop.DocComment = ConsumeStoredComment ();
1737 accessor_declarations
1738 : get_accessor_declaration
1740 $$ = new Accessors ((Accessor) $1, null);
1742 | get_accessor_declaration accessor_declarations
1744 Accessors accessors = (Accessors) $2;
1745 accessors.get_or_add = (Accessor) $1;
1748 | set_accessor_declaration
1750 $$ = new Accessors (null, (Accessor) $1);
1752 | set_accessor_declaration accessor_declarations
1754 Accessors accessors = (Accessors) $2;
1755 accessors.set_or_remove = (Accessor) $1;
1756 accessors.declared_in_reverse = true;
1761 if (yyToken == Token.CLOSE_BRACE) {
1764 if (yyToken == Token.SEMICOLON)
1765 Report.Error (1597, lexer.Location, "Semicolon after method or accessor block is not valid");
1767 Report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1769 $$ = new Accessors (null, null);
1774 get_accessor_declaration
1775 : opt_attributes opt_modifiers GET
1777 // If this is not the case, then current_local_parameters has already
1778 // been set in indexer_declaration
1779 if (parsing_indexer == false)
1780 current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters;
1782 current_local_parameters = indexer_parameters;
1783 lexer.PropertyParsing = false;
1788 Report.Error (1007, GetLocation ($3), "Property accessor already defined");
1791 Accessor accessor = new Accessor ((ToplevelBlock) $5, (Modifiers) $2, (Attributes) $1, current_local_parameters, GetLocation ($3));
1793 current_local_parameters = null;
1794 lexer.PropertyParsing = true;
1796 if (RootContext.Documentation != null)
1797 if (Lexer.doc_state == XmlCommentState.Error)
1798 Lexer.doc_state = XmlCommentState.NotAllowed;
1804 set_accessor_declaration
1805 : opt_attributes opt_modifiers SET
1807 Parameter implicit_value_parameter = new Parameter (
1808 implicit_value_parameter_type, "value",
1809 Parameter.Modifier.NONE, null, GetLocation ($3));
1811 if (!parsing_indexer) {
1812 current_local_parameters = new ParametersCompiled (compiler, new Parameter [] { implicit_value_parameter });
1814 current_local_parameters = ParametersCompiled.MergeGenerated (compiler,
1815 indexer_parameters, true, implicit_value_parameter, null);
1818 lexer.PropertyParsing = false;
1823 Report.Error (1007, GetLocation ($3), "Property accessor already defined");
1826 Accessor accessor = new Accessor ((ToplevelBlock) $5, (Modifiers) $2, (Attributes) $1, current_local_parameters, GetLocation ($3));
1828 current_local_parameters = null;
1829 lexer.PropertyParsing = true;
1831 if (RootContext.Documentation != null
1832 && Lexer.doc_state == XmlCommentState.Error)
1833 Lexer.doc_state = XmlCommentState.NotAllowed;
1847 Error_SyntaxError (1043, yyToken, "Invalid accessor body");
1852 interface_declaration
1858 lexer.ConstraintsParsing = true;
1860 type_declaration_name
1862 MemberName name = MakeName ((MemberName) $6);
1863 push_current_class (new Interface (current_namespace, current_class, name, (Modifiers) $2, (Attributes) $1), $3);
1866 opt_type_parameter_constraints_clauses
1868 lexer.ConstraintsParsing = false;
1870 current_class.SetParameterInfo ((List<Constraints>) $9);
1872 if (RootContext.Documentation != null) {
1873 current_container.DocComment = Lexer.consume_doc_comment ();
1874 Lexer.doc_state = XmlCommentState.Allowed;
1879 --lexer.parsing_declaration;
1880 if (RootContext.Documentation != null)
1881 Lexer.doc_state = XmlCommentState.Allowed;
1885 $$ = pop_current_class ();
1887 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1888 CheckIdentifierToken (yyToken, GetLocation ($5));
1894 opt_interface_member_declarations
1898 opt_interface_member_declarations
1900 | interface_member_declarations
1903 interface_member_declarations
1904 : interface_member_declaration
1905 | interface_member_declarations interface_member_declaration
1908 interface_member_declaration
1909 : constant_declaration
1911 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1915 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1917 | method_declaration
1918 | property_declaration
1920 | indexer_declaration
1921 | operator_declaration
1923 Report.Error (567, GetLocation ($1), "Interfaces cannot contain operators");
1925 | constructor_declaration
1927 Report.Error (526, GetLocation ($1), "Interfaces cannot contain contructors");
1931 Report.Error (524, GetLocation ($1), "Interfaces cannot declare classes, structs, interfaces, delegates, or enumerations");
1935 operator_declaration
1936 : opt_attributes opt_modifiers operator_declarator
1944 OperatorDeclaration decl = (OperatorDeclaration) $3;
1945 Operator op = new Operator (
1946 current_class, decl.optype, decl.ret_type, (Modifiers) $2,
1947 current_local_parameters,
1948 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1950 if (RootContext.Documentation != null) {
1951 op.DocComment = tmpComment;
1952 Lexer.doc_state = XmlCommentState.Allowed;
1955 // Note again, checking is done in semantic analysis
1956 current_container.AddOperator (op);
1958 current_local_parameters = null;
1964 | SEMICOLON { $$ = null; }
1968 : type_expression_or_array
1971 Report.Error (590, GetLocation ($1), "User-defined operators cannot return void");
1972 $$ = TypeManager.system_void_expr;
1977 : operator_type OPERATOR overloadable_operator OPEN_PARENS
1979 valid_param_mod = ParameterModifierType.DefaultValue;
1981 opt_formal_parameter_list CLOSE_PARENS
1983 valid_param_mod = 0;
1985 Location loc = GetLocation ($2);
1986 Operator.OpType op = (Operator.OpType) $3;
1987 current_local_parameters = (ParametersCompiled)$6;
1989 int p_count = current_local_parameters.Count;
1991 if (op == Operator.OpType.Addition)
1992 op = Operator.OpType.UnaryPlus;
1993 else if (op == Operator.OpType.Subtraction)
1994 op = Operator.OpType.UnaryNegation;
1997 if (IsUnaryOperator (op)) {
1999 Report.Error (1020, loc, "Overloadable binary operator expected");
2000 } else if (p_count != 1) {
2001 Report.Error (1535, loc, "Overloaded unary operator `{0}' takes one parameter",
2002 Operator.GetName (op));
2006 Report.Error (1534, loc, "Overloaded binary operator `{0}' takes two parameters",
2007 Operator.GetName (op));
2008 } else if (p_count != 2) {
2009 Report.Error (1019, loc, "Overloadable unary operator expected");
2013 if (RootContext.Documentation != null) {
2014 tmpComment = Lexer.consume_doc_comment ();
2015 Lexer.doc_state = XmlCommentState.NotAllowed;
2018 $$ = new OperatorDeclaration (op, (FullNamedExpression) $1, loc);
2020 | conversion_operator_declarator
2023 overloadable_operator
2025 : BANG { $$ = Operator.OpType.LogicalNot; }
2026 | TILDE { $$ = Operator.OpType.OnesComplement; }
2027 | OP_INC { $$ = Operator.OpType.Increment; }
2028 | OP_DEC { $$ = Operator.OpType.Decrement; }
2029 | TRUE { $$ = Operator.OpType.True; }
2030 | FALSE { $$ = Operator.OpType.False; }
2031 // Unary and binary:
2032 | PLUS { $$ = Operator.OpType.Addition; }
2033 | MINUS { $$ = Operator.OpType.Subtraction; }
2035 | STAR { $$ = Operator.OpType.Multiply; }
2036 | DIV { $$ = Operator.OpType.Division; }
2037 | PERCENT { $$ = Operator.OpType.Modulus; }
2038 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
2039 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
2040 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
2041 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
2042 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
2043 | OP_EQ { $$ = Operator.OpType.Equality; }
2044 | OP_NE { $$ = Operator.OpType.Inequality; }
2045 | OP_GT { $$ = Operator.OpType.GreaterThan; }
2046 | OP_LT { $$ = Operator.OpType.LessThan; }
2047 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
2048 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
2051 conversion_operator_declarator
2052 : IMPLICIT OPERATOR type OPEN_PARENS
2054 valid_param_mod = ParameterModifierType.DefaultValue;
2056 opt_formal_parameter_list CLOSE_PARENS
2058 valid_param_mod = 0;
2060 Location loc = GetLocation ($2);
2061 current_local_parameters = (ParametersCompiled)$6;
2063 if (RootContext.Documentation != null) {
2064 tmpComment = Lexer.consume_doc_comment ();
2065 Lexer.doc_state = XmlCommentState.NotAllowed;
2068 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (FullNamedExpression) $3, loc);
2070 | EXPLICIT OPERATOR type OPEN_PARENS
2072 valid_param_mod = ParameterModifierType.DefaultValue;
2074 opt_formal_parameter_list CLOSE_PARENS
2076 valid_param_mod = 0;
2078 Location loc = GetLocation ($2);
2079 current_local_parameters = (ParametersCompiled)$6;
2081 if (RootContext.Documentation != null) {
2082 tmpComment = Lexer.consume_doc_comment ();
2083 Lexer.doc_state = XmlCommentState.NotAllowed;
2086 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (FullNamedExpression) $3, loc);
2090 Error_SyntaxError (yyToken);
2091 current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters;
2092 $$ = new OperatorDeclaration (Operator.OpType.Implicit, null, GetLocation ($1));
2096 Error_SyntaxError (yyToken);
2097 current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters;
2098 $$ = new OperatorDeclaration (Operator.OpType.Explicit, null, GetLocation ($1));
2102 constructor_declaration
2103 : constructor_declarator
2106 Constructor c = (Constructor) $1;
2107 c.Block = (ToplevelBlock) $2;
2109 if (RootContext.Documentation != null)
2110 c.DocComment = ConsumeStoredComment ();
2112 current_container.AddConstructor (c);
2114 current_local_parameters = null;
2115 if (RootContext.Documentation != null)
2116 Lexer.doc_state = XmlCommentState.Allowed;
2120 constructor_declarator
2125 if (RootContext.Documentation != null) {
2126 tmpComment = Lexer.consume_doc_comment ();
2127 Lexer.doc_state = XmlCommentState.Allowed;
2130 valid_param_mod = ParameterModifierType.All;
2132 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2134 valid_param_mod = 0;
2135 current_local_parameters = (ParametersCompiled) $6;
2138 // start block here, so possible anonymous methods inside
2139 // constructor initializer can get correct parent block
2141 start_block (lexer.Location);
2143 opt_constructor_initializer
2145 var lt = (Tokenizer.LocatedToken) $3;
2146 var mods = (Modifiers) $2;
2147 ConstructorInitializer ci = (ConstructorInitializer) $9;
2149 Constructor c = new Constructor (current_class, lt.Value, mods,
2150 (Attributes) $1, current_local_parameters, ci, lt.Location);
2152 if (lt.Value != current_container.MemberName.Name) {
2153 Report.Error (1520, c.Location, "Class, struct, or interface method must have a return type");
2154 } else if ((mods & Modifiers.STATIC) != 0) {
2155 if ((mods & Modifiers.AccessibilityMask) != 0){
2156 Report.Error (515, c.Location,
2157 "`{0}': static constructor cannot have an access modifier",
2158 c.GetSignatureForError ());
2161 Report.Error (514, c.Location,
2162 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2163 c.GetSignatureForError ());
2174 | SEMICOLON { current_block = null; $$ = null; }
2177 opt_constructor_initializer
2179 | constructor_initializer
2182 constructor_initializer
2183 : COLON BASE OPEN_PARENS
2185 ++lexer.parsing_block;
2187 opt_argument_list CLOSE_PARENS
2189 --lexer.parsing_block;
2190 $$ = new ConstructorBaseInitializer ((Arguments) $5, GetLocation ($2));
2192 | COLON THIS OPEN_PARENS
2194 ++lexer.parsing_block;
2196 opt_argument_list CLOSE_PARENS
2198 --lexer.parsing_block;
2199 $$ = new ConstructorThisInitializer ((Arguments) $5, GetLocation ($2));
2202 Report.Error (1018, GetLocation ($1), "Keyword `this' or `base' expected");
2207 destructor_declaration
2208 : opt_attributes opt_modifiers TILDE
2210 if (RootContext.Documentation != null) {
2211 tmpComment = Lexer.consume_doc_comment ();
2212 Lexer.doc_state = XmlCommentState.NotAllowed;
2215 current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters;
2217 IDENTIFIER OPEN_PARENS CLOSE_PARENS method_body
2219 var lt = (Tokenizer.LocatedToken) $5;
2220 if (lt.Value != current_container.MemberName.Name){
2221 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2222 } else if (current_container.Kind != MemberKind.Class){
2223 Report.Error (575, lt.Location, "Only class types can contain destructor");
2225 Destructor d = new Destructor (current_class, (Modifiers) $2,
2226 ParametersCompiled.EmptyReadOnlyParameters, (Attributes) $1, lt.Location);
2227 if (RootContext.Documentation != null)
2228 d.DocComment = ConsumeStoredComment ();
2230 d.Block = (ToplevelBlock) $8;
2231 current_container.AddMethod (d);
2234 current_local_parameters = null;
2241 EVENT type variable_declarators SEMICOLON
2243 foreach (VariableMemberDeclaration var in (List<object>) $5) {
2245 EventField e = new EventField (
2246 current_class, (FullNamedExpression) $4, (Modifiers) $2, var.MemberName, (Attributes) $1);
2248 e.Initializer = var.GetInitializer ((FullNamedExpression) $4);
2249 if (current_container.Kind == MemberKind.Interface && e.Initializer != null) {
2250 Report.Error (68, e.Location, "`{0}': event in interface cannot have initializer", e.GetSignatureForError ());
2253 if (var.MemberName.Left != null) {
2254 Report.Error (71, e.Location,
2255 "`{0}': An explicit interface implementation of an event must use property syntax",
2256 e.GetSignatureForError ());
2259 current_container.AddEvent (e);
2261 if (RootContext.Documentation != null) {
2262 e.DocComment = Lexer.consume_doc_comment ();
2263 Lexer.doc_state = XmlCommentState.Allowed;
2269 EVENT type member_declaration_name
2272 implicit_value_parameter_type = (FullNamedExpression) $4;
2273 current_local_parameters = new ParametersCompiled (compiler,
2274 new Parameter (implicit_value_parameter_type, "value",
2275 Parameter.Modifier.NONE, null, GetLocation ($3)));
2277 lexer.EventParsing = true;
2279 event_accessor_declarations
2281 lexer.EventParsing = false;
2285 MemberName name = (MemberName) $5;
2287 if (current_container.Kind == MemberKind.Interface) {
2288 Report.Error (69, GetLocation ($3), "Event in interface cannot have add or remove accessors");
2289 $8 = new Accessors (null, null);
2290 } else if ($8 == null) {
2291 Report.Error (65, GetLocation ($3), "`{0}.{1}': event property must have both add and remove accessors",
2292 current_container.GetSignatureForError (), name.GetSignatureForError ());
2293 $8 = new Accessors (null, null);
2296 Accessors accessors = (Accessors) $8;
2298 if (accessors.get_or_add == null || accessors.set_or_remove == null)
2299 // CS0073 is already reported, so no CS0065 here.
2302 Event e = new EventProperty (
2303 current_class, (FullNamedExpression) $4, (Modifiers) $2, name,
2304 (Attributes) $1, accessors.get_or_add, accessors.set_or_remove);
2305 if (RootContext.Documentation != null) {
2306 e.DocComment = Lexer.consume_doc_comment ();
2307 Lexer.doc_state = XmlCommentState.Allowed;
2310 current_container.AddEvent (e);
2311 implicit_value_parameter_type = null;
2313 current_local_parameters = null;
2315 | opt_attributes opt_modifiers EVENT type member_declaration_name error
2317 MemberName mn = (MemberName) $5;
2318 if (mn.Left != null)
2319 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2321 if (RootContext.Documentation != null)
2322 Lexer.doc_state = XmlCommentState.Allowed;
2324 Error_SyntaxError (yyToken);
2329 event_accessor_declarations
2330 : add_accessor_declaration remove_accessor_declaration
2332 $$ = new Accessors ((Accessor) $1, (Accessor) $2);
2334 | remove_accessor_declaration add_accessor_declaration
2336 Accessors accessors = new Accessors ((Accessor) $2, (Accessor) $1);
2337 accessors.declared_in_reverse = true;
2340 | add_accessor_declaration { $$ = null; }
2341 | remove_accessor_declaration { $$ = null; }
2344 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2350 add_accessor_declaration
2351 : opt_attributes ADD
2353 lexer.EventParsing = false;
2357 Accessor accessor = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, null, GetLocation ($2));
2358 lexer.EventParsing = true;
2361 | opt_attributes ADD error {
2362 Report.Error (73, GetLocation ($2), "An add or remove accessor must have a body");
2365 | opt_attributes modifiers ADD {
2366 Report.Error (1609, GetLocation ($3), "Modifiers cannot be placed on event accessor declarations");
2371 remove_accessor_declaration
2372 : opt_attributes REMOVE
2374 lexer.EventParsing = false;
2378 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, null, GetLocation ($2));
2379 lexer.EventParsing = true;
2381 | opt_attributes REMOVE error {
2382 Report.Error (73, GetLocation ($2), "An add or remove accessor must have a body");
2385 | opt_attributes modifiers REMOVE {
2386 Report.Error (1609, GetLocation ($3), "Modifiers cannot be placed on event accessor declarations");
2392 : opt_attributes opt_modifiers
2393 member_type indexer_declaration_name OPEN_BRACKET
2395 valid_param_mod = ParameterModifierType.Params | ParameterModifierType.DefaultValue;
2397 opt_formal_parameter_list CLOSE_BRACKET
2400 valid_param_mod = 0;
2401 implicit_value_parameter_type = (FullNamedExpression) $3;
2402 indexer_parameters = (ParametersCompiled) $7;
2404 if (indexer_parameters.IsEmpty) {
2405 Report.Error (1551, GetLocation ($5), "Indexers must have at least one parameter");
2408 if (RootContext.Documentation != null) {
2409 tmpComment = Lexer.consume_doc_comment ();
2410 Lexer.doc_state = XmlCommentState.Allowed;
2413 lexer.PropertyParsing = true;
2414 parsing_indexer = true;
2417 accessor_declarations
2419 lexer.PropertyParsing = false;
2420 has_get = has_set = false;
2421 parsing_indexer = false;
2425 Accessors accessors = (Accessors) $11;
2426 Accessor get_block = accessors != null ? accessors.get_or_add : null;
2427 Accessor set_block = accessors != null ? accessors.set_or_remove : null;
2428 bool order = accessors != null ? accessors.declared_in_reverse : false;
2430 Indexer indexer = new Indexer (current_class, (FullNamedExpression) $3,
2431 (MemberName)$4, (Modifiers) $2, (ParametersCompiled) $7, (Attributes) $1,
2432 get_block, set_block, order);
2434 if ($3 == TypeManager.system_void_expr)
2435 Report.Error (620, GetLocation ($3), "`{0}': indexer return type cannot be `void'", indexer.GetSignatureForError ());
2437 if (accessors == null)
2438 Report.Error (548, indexer.Location, "`{0}': property or indexer must have at least one accessor", indexer.GetSignatureForError ());
2440 if (current_container.Kind == MemberKind.Interface) {
2441 if (indexer.Get.Block != null)
2442 Report.Error (531, indexer.Location, "`{0}.get': interface members cannot have a definition", indexer.GetSignatureForError ());
2444 if (indexer.Set.Block != null)
2445 Report.Error (531, indexer.Location, "`{0}.set': interface members cannot have a definition", indexer.GetSignatureForError ());
2448 if (RootContext.Documentation != null)
2449 indexer.DocComment = ConsumeStoredComment ();
2451 current_container.AddIndexer (indexer);
2453 current_local_parameters = null;
2454 implicit_value_parameter_type = null;
2455 indexer_parameters = null;
2462 ENUM type_declaration_name
2464 if (RootContext.Documentation != null)
2465 enumTypeComment = Lexer.consume_doc_comment ();
2470 MemberName name = (MemberName) $4;
2471 if (name.IsGeneric) {
2472 Report.Error (1675, name.Location, "Enums cannot have type parameters");
2475 name = MakeName (name);
2476 Enum e = new Enum (current_namespace, current_class, (TypeExpr) $5, (Modifiers) $2,
2477 name, (Attributes) $1);
2479 if (RootContext.Documentation != null)
2480 e.DocComment = enumTypeComment;
2483 EnumMember em = null;
2484 foreach (VariableDeclaration ev in (IList<VariableDeclaration>) $7) {
2485 em = new EnumMember (
2486 e, em, ev.identifier, ev.GetInitializer ((FullNamedExpression) $5),
2487 ev.OptAttributes, ev.Location);
2489 // if (RootContext.Documentation != null)
2490 em.DocComment = ev.DocComment;
2492 e.AddEnumMember (em);
2494 if (RootContext.EvalMode)
2495 undo.AddTypeContainer (current_container, e);
2497 current_container.AddTypeContainer (e);
2507 $$ = TypeManager.system_int32_expr;
2511 if ($2 != TypeManager.system_int32_expr && $2 != TypeManager.system_uint32_expr &&
2512 $2 != TypeManager.system_int64_expr && $2 != TypeManager.system_uint64_expr &&
2513 $2 != TypeManager.system_int16_expr && $2 != TypeManager.system_uint16_expr &&
2514 $2 != TypeManager.system_byte_expr && $2 != TypeManager.system_sbyte_expr) {
2515 Enum.Error_1008 (GetLocation ($2), Report);
2516 $2 = TypeManager.system_int32_expr;
2523 Error_TypeExpected (GetLocation ($1));
2524 $$ = TypeManager.system_int32_expr;
2531 if (RootContext.Documentation != null)
2532 Lexer.doc_state = XmlCommentState.Allowed;
2534 opt_enum_member_declarations
2536 // here will be evaluated after CLOSE_BLACE is consumed.
2537 if (RootContext.Documentation != null)
2538 Lexer.doc_state = XmlCommentState.Allowed;
2546 opt_enum_member_declarations
2547 : /* empty */ { $$ = new VariableDeclaration [0]; }
2548 | enum_member_declarations opt_comma { $$ = $1; }
2551 enum_member_declarations
2552 : enum_member_declaration
2554 var l = new List<VariableDeclaration> (4);
2555 l.Add ((VariableDeclaration) $1);
2558 | enum_member_declarations COMMA enum_member_declaration
2560 var l = (List<VariableDeclaration>) $1;
2561 l.Add ((VariableDeclaration) $3);
2566 enum_member_declaration
2567 : opt_attributes IDENTIFIER
2569 VariableDeclaration vd = new VariableDeclaration (
2570 (Tokenizer.LocatedToken) $2, null, (Attributes) $1);
2572 if (RootContext.Documentation != null) {
2573 vd.DocComment = Lexer.consume_doc_comment ();
2574 Lexer.doc_state = XmlCommentState.Allowed;
2579 | opt_attributes IDENTIFIER
2581 ++lexer.parsing_block;
2582 if (RootContext.Documentation != null) {
2583 tmpComment = Lexer.consume_doc_comment ();
2584 Lexer.doc_state = XmlCommentState.NotAllowed;
2587 ASSIGN constant_expression
2589 --lexer.parsing_block;
2590 VariableDeclaration vd = new VariableDeclaration (
2591 (Tokenizer.LocatedToken) $2, (Expression) $5, (Attributes) $1);
2593 if (RootContext.Documentation != null)
2594 vd.DocComment = ConsumeStoredComment ();
2600 delegate_declaration
2604 member_type type_declaration_name
2607 valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out | ParameterModifierType.Params | ParameterModifierType.DefaultValue;
2609 opt_formal_parameter_list CLOSE_PARENS
2611 valid_param_mod = 0;
2613 MemberName name = MakeName ((MemberName) $5);
2614 ParametersCompiled p = (ParametersCompiled) $8;
2616 Delegate del = new Delegate (current_namespace, current_class, (FullNamedExpression) $4,
2617 (Modifiers) $2, name, p, (Attributes) $1);
2619 if (RootContext.Documentation != null) {
2620 del.DocComment = Lexer.consume_doc_comment ();
2621 Lexer.doc_state = XmlCommentState.Allowed;
2624 current_container.AddDelegate (del);
2625 current_delegate = del;
2626 lexer.ConstraintsParsing = true;
2628 opt_type_parameter_constraints_clauses
2630 lexer.ConstraintsParsing = false;
2634 current_delegate.SetParameterInfo ((List<Constraints>) $11);
2635 $$ = current_delegate;
2637 current_delegate = null;
2648 if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)
2649 Report.FeatureIsNotSupported (GetLocation ($1), "nullable types");
2650 else if (RootContext.Version < LanguageVersion.ISO_2)
2651 Report.FeatureIsNotAvailable (GetLocation ($1), "nullable types");
2657 namespace_or_type_name
2659 | qualified_alias_member IDENTIFIER opt_type_argument_list
2661 var lt1 = (Tokenizer.LocatedToken) $1;
2662 var lt2 = (Tokenizer.LocatedToken) $2;
2664 $$ = new MemberName (lt1.Value, lt2.Value, (TypeArguments) $3, lt1.Location);
2670 | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list
2672 var lt = (Tokenizer.LocatedToken) $3;
2673 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $4, lt.Location);
2678 : IDENTIFIER opt_type_argument_list
2680 var lt = (Tokenizer.LocatedToken) $1;
2681 $$ = new MemberName (lt.Value, (TypeArguments)$2, lt.Location);
2686 // Generics arguments (any type, without attributes)
2688 opt_type_argument_list
2689 : /* empty */ { $$ = null; }
2690 | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2692 if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)
2693 Report.FeatureIsNotSupported (GetLocation ($1), "generics");
2694 else if (RootContext.Version < LanguageVersion.ISO_2)
2695 Report.FeatureIsNotAvailable (GetLocation ($1), "generics");
2699 | OP_GENERICS_LT error
2701 Error_TypeExpected (lexer.Location);
2702 $$ = new TypeArguments ();
2709 TypeArguments type_args = new TypeArguments ();
2710 type_args.Add ((FullNamedExpression) $1);
2713 | type_arguments COMMA type
2715 TypeArguments type_args = (TypeArguments) $1;
2716 type_args.Add ((FullNamedExpression) $3);
2722 // Generics parameters (identifiers only, with attributes), used in type or method declarations
2724 type_declaration_name
2727 lexer.parsing_generic_declaration = true;
2729 opt_type_parameter_list
2731 lexer.parsing_generic_declaration = false;
2732 var lt = (Tokenizer.LocatedToken) $1;
2733 $$ = new MemberName (lt.Value, (TypeArguments)$3, lt.Location);
2737 member_declaration_name
2738 : method_declaration_name
2740 MemberName mn = (MemberName)$1;
2741 if (mn.TypeArguments != null)
2742 syntax_error (mn.Location, string.Format ("Member `{0}' cannot declare type arguments",
2743 mn.GetSignatureForError ()));
2747 method_declaration_name
2748 : type_declaration_name
2749 | explicit_interface IDENTIFIER opt_type_parameter_list
2751 lexer.parsing_generic_declaration = false;
2752 var lt = (Tokenizer.LocatedToken) $2;
2753 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $3, lt.Location);
2757 indexer_declaration_name
2760 lexer.parsing_generic_declaration = false;
2761 $$ = new MemberName (TypeContainer.DefaultIndexerName, GetLocation ($1));
2763 | explicit_interface THIS
2765 lexer.parsing_generic_declaration = false;
2766 $$ = new MemberName ((MemberName) $1, TypeContainer.DefaultIndexerName, null, GetLocation ($1));
2771 : IDENTIFIER opt_type_argument_list DOT
2773 var lt = (Tokenizer.LocatedToken) $1;
2774 $$ = new MemberName (lt.Value, (TypeArguments) $2, lt.Location);
2776 | qualified_alias_member IDENTIFIER opt_type_argument_list DOT
2778 var lt1 = (Tokenizer.LocatedToken) $1;
2779 var lt2 = (Tokenizer.LocatedToken) $2;
2781 $$ = new MemberName (lt1.Value, lt2.Value, (TypeArguments) $3, lt1.Location);
2783 | explicit_interface IDENTIFIER opt_type_argument_list DOT
2785 var lt = (Tokenizer.LocatedToken) $2;
2786 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $3, lt.Location);
2790 opt_type_parameter_list
2791 : /* empty */ { $$ = null; }
2792 | OP_GENERICS_LT_DECL type_parameters OP_GENERICS_GT
2794 if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)
2795 Report.FeatureIsNotSupported (GetLocation ($1), "generics");
2796 else if (RootContext.Version < LanguageVersion.ISO_2)
2797 Report.FeatureIsNotAvailable (GetLocation ($1), "generics");
2806 TypeArguments type_args = new TypeArguments ();
2807 type_args.Add ((FullNamedExpression)$1);
2810 | type_parameters COMMA type_parameter
2812 TypeArguments type_args = (TypeArguments) $1;
2813 type_args.Add ((FullNamedExpression)$3);
2819 : opt_attributes opt_type_parameter_variance IDENTIFIER
2821 var lt = (Tokenizer.LocatedToken)$3;
2822 $$ = new TypeParameterName (lt.Value, (Attributes)$1, (Variance) $2, lt.Location);
2826 if (GetTokenName (yyToken) == "type")
2827 Report.Error (81, GetLocation ($1), "Type parameter declaration must be an identifier not a type");
2829 Error_SyntaxError (yyToken);
2831 $$ = new TypeParameterName ("", null, lexer.Location);
2836 // All types where void is allowed
2839 : type_expression_or_array
2842 $$ = TypeManager.system_void_expr;
2849 lexer.parsing_generic_declaration = true;
2854 // A type which does not allow `void' to be used
2857 : type_expression_or_array
2860 Expression.Error_VoidInvalidInTheContext (GetLocation ($1), Report);
2861 $$ = TypeManager.system_void_expr;
2869 Expression.Error_VoidInvalidInTheContext (GetLocation ($1), Report);
2870 $$ = TypeManager.system_void_expr;
2875 : type_expression_or_array
2878 Report.Error (1536, GetLocation ($1), "Invalid parameter type `void'");
2879 $$ = TypeManager.system_void_expr;
2883 type_expression_or_array
2885 | type_expression rank_specifiers
2887 string rank_specifiers = (string) $2;
2888 $$ = new ComposedCast ((FullNamedExpression) $1, rank_specifiers);
2893 : namespace_or_type_name opt_nullable
2895 MemberName name = (MemberName) $1;
2898 $$ = new ComposedCast (name.GetTypeExpression (), "?", lexer.Location);
2900 if (name.Left == null && name.Name == "var")
2901 $$ = new VarExpr (name.Location);
2903 $$ = name.GetTypeExpression ();
2906 | builtin_types opt_nullable
2909 $$ = new ComposedCast ((FullNamedExpression) $1, "?", lexer.Location);
2911 | type_expression STAR
2914 // Note that here only unmanaged types are allowed but we
2915 // can't perform checks during this phase - we do it during
2916 // semantic analysis.
2918 $$ = new ComposedCast ((FullNamedExpression) $1, "*", Lexer.Location);
2922 $$ = new ComposedCast (TypeManager.system_void_expr, "*", GetLocation ($1));
2929 var types = new List<FullNamedExpression> (2);
2930 types.Add ((FullNamedExpression) $1);
2933 | type_list COMMA base_type_name
2935 var types = (List<FullNamedExpression>) $1;
2936 types.Add ((FullNamedExpression) $3);
2944 if ($1 is ComposedCast) {
2945 Report.Error (1521, GetLocation ($1), "Invalid base type `{0}'", ((ComposedCast)$1).GetSignatureForError ());
2951 Error_TypeExpected (lexer.Location);
2957 * replaces all the productions for isolating the various
2958 * simple types, but we need this to reuse it easily in variable_type
2961 : OBJECT { $$ = TypeManager.system_object_expr; }
2962 | STRING { $$ = TypeManager.system_string_expr; }
2963 | BOOL { $$ = TypeManager.system_boolean_expr; }
2964 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2965 | FLOAT { $$ = TypeManager.system_single_expr; }
2966 | DOUBLE { $$ = TypeManager.system_double_expr; }
2971 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2972 | BYTE { $$ = TypeManager.system_byte_expr; }
2973 | SHORT { $$ = TypeManager.system_int16_expr; }
2974 | USHORT { $$ = TypeManager.system_uint16_expr; }
2975 | INT { $$ = TypeManager.system_int32_expr; }
2976 | UINT { $$ = TypeManager.system_uint32_expr; }
2977 | LONG { $$ = TypeManager.system_int64_expr; }
2978 | ULONG { $$ = TypeManager.system_uint64_expr; }
2979 | CHAR { $$ = TypeManager.system_char_expr; }
2983 // Expressions, section 7.5
2988 : primary_expression_no_array_creation
2989 | array_creation_expression
2992 primary_expression_no_array_creation
2994 | IDENTIFIER opt_type_argument_list
2996 var lt = (Tokenizer.LocatedToken) $1;
2997 $$ = new SimpleName (lt.Value, (TypeArguments)$2, lt.Location);
2999 | IDENTIFIER GENERATE_COMPLETION {
3000 var lt = (Tokenizer.LocatedToken) $1;
3001 $$ = new CompletionSimpleName (MemberName.MakeName (lt.Value, null), lt.Location);
3003 | parenthesized_expression
3004 | default_value_expression
3006 | invocation_expression
3010 | post_increment_expression
3011 | post_decrement_expression
3012 | object_or_delegate_creation_expression
3013 | anonymous_type_expression
3016 | checked_expression
3017 | unchecked_expression
3018 | pointer_member_access
3019 | anonymous_method_expression
3025 | NULL { $$ = new NullLiteral (GetLocation ($1)); }
3029 : TRUE { $$ = new BoolLiteral (true, GetLocation ($1)); }
3030 | FALSE { $$ = new BoolLiteral (false, GetLocation ($1)); }
3035 // Here is the trick, tokenizer may think that parens is a special but
3036 // parser is interested in open parens only, so we merge them.
3037 // Consider: if (a)foo ();
3045 // Use this production to accept closing parenthesis or
3046 // performing completion
3050 | COMPLETE_COMPLETION
3054 parenthesized_expression
3055 : OPEN_PARENS expression CLOSE_PARENS
3057 $$ = new ParenthesizedExpression ((Expression) $2);
3059 | OPEN_PARENS expression COMPLETE_COMPLETION
3061 $$ = new ParenthesizedExpression ((Expression) $2);
3066 : primary_expression DOT IDENTIFIER opt_type_argument_list
3068 var lt = (Tokenizer.LocatedToken) $3;
3069 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
3071 | builtin_types DOT IDENTIFIER opt_type_argument_list
3073 var lt = (Tokenizer.LocatedToken) $3;
3074 // TODO: Location is wrong as some predefined types doesn't hold a location
3075 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
3077 | qualified_alias_member IDENTIFIER opt_type_argument_list
3079 var lt1 = (Tokenizer.LocatedToken) $1;
3080 var lt2 = (Tokenizer.LocatedToken) $2;
3082 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, (TypeArguments) $3, lt1.Location);
3084 | primary_expression DOT GENERATE_COMPLETION {
3085 $$ = new CompletionMemberAccess ((Expression) $1, null,GetLocation ($3));
3087 | primary_expression DOT IDENTIFIER GENERATE_COMPLETION {
3088 var lt = (Tokenizer.LocatedToken) $3;
3089 $$ = new CompletionMemberAccess ((Expression) $1, lt.Value, lt.Location);
3091 | builtin_types DOT GENERATE_COMPLETION
3093 // TODO: Location is wrong as some predefined types doesn't hold a location
3094 $$ = new CompletionMemberAccess ((Expression) $1, null, lexer.Location);
3096 | builtin_types DOT IDENTIFIER GENERATE_COMPLETION {
3097 var lt = (Tokenizer.LocatedToken) $3;
3098 $$ = new CompletionMemberAccess ((Expression) $1, lt.Value, lt.Location);
3102 invocation_expression
3103 : primary_expression open_parens_any opt_argument_list close_parens
3105 $$ = new Invocation ((Expression) $1, (Arguments) $3);
3109 opt_object_or_collection_initializer
3110 : /* empty */ { $$ = null; }
3111 | object_or_collection_initializer
3114 object_or_collection_initializer
3115 : OPEN_BRACE opt_member_initializer_list close_brace_or_complete_completion
3118 $$ = CollectionOrObjectInitializers.Empty;
3120 $$ = new CollectionOrObjectInitializers ((List<Expression>) $2, GetLocation ($1));
3122 | OPEN_BRACE member_initializer_list COMMA CLOSE_BRACE
3124 $$ = new CollectionOrObjectInitializers ((List<Expression>) $2, GetLocation ($1));
3128 opt_member_initializer_list
3129 : /* empty */ { $$ = null; }
3130 | member_initializer_list
3136 member_initializer_list
3137 : member_initializer
3139 var a = new List<Expression> ();
3140 a.Add ((Expression) $1);
3143 | member_initializer_list COMMA member_initializer
3145 var a = (List<Expression>)$1;
3146 a.Add ((Expression) $3);
3149 | member_initializer_list error {
3150 Error_SyntaxError (yyToken);
3156 : IDENTIFIER ASSIGN initializer_value
3158 var lt = (Tokenizer.LocatedToken) $1;
3159 $$ = new ElementInitializer (lt.Value, (Expression)$3, lt.Location);
3161 | GENERATE_COMPLETION
3163 $$ = new CompletionElementInitializer (null, GetLocation ($1));
3165 | non_assignment_expression opt_COMPLETE_COMPLETION {
3166 CompletionSimpleName csn = $1 as CompletionSimpleName;
3168 $$ = new CollectionElementInitializer ((Expression)$1);
3170 $$ = new CompletionElementInitializer (csn.Prefix, csn.Location);
3172 | OPEN_BRACE expression_list CLOSE_BRACE
3177 $$ = new CollectionElementInitializer ((List<Expression>)$2, GetLocation ($1));
3179 | OPEN_BRACE CLOSE_BRACE
3181 Report.Error (1920, GetLocation ($1), "An element initializer cannot be empty");
3188 | object_or_collection_initializer
3192 : /* empty */ { $$ = null; }
3197 : argument_or_named_argument
3199 Arguments list = new Arguments (4);
3200 list.Add ((Argument) $1);
3203 | argument_list COMMA argument
3205 Arguments list = (Arguments) $1;
3206 if (list [list.Count - 1] is NamedArgument)
3207 Error_NamedArgumentExpected ((NamedArgument) list [list.Count - 1]);
3209 list.Add ((Argument) $3);
3212 | argument_list COMMA named_argument
3214 Arguments list = (Arguments) $1;
3215 NamedArgument a = (NamedArgument) $3;
3216 for (int i = 0; i < list.Count; ++i) {
3217 NamedArgument na = list [i] as NamedArgument;
3218 if (na != null && na.Name == a.Name)
3219 Report.Error (1740, na.Location, "Named argument `{0}' specified multiple times",
3226 | argument_list COMMA
3228 Report.Error (839, GetLocation ($2), "An argument is missing");
3231 | COMMA argument_or_named_argument
3233 Report.Error (839, GetLocation ($1), "An argument is missing");
3241 $$ = new Argument ((Expression) $1);
3243 | non_simple_argument
3246 argument_or_named_argument
3252 : REF variable_reference
3254 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
3256 | OUT variable_reference
3258 $$ = new Argument ((Expression) $2, Argument.AType.Out);
3260 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
3262 $$ = new Argument (new Arglist ((Arguments) $3, GetLocation ($1)));
3264 | ARGLIST OPEN_PARENS CLOSE_PARENS
3266 $$ = new Argument (new Arglist (GetLocation ($1)));
3270 $$ = new Argument (new ArglistAccess (GetLocation ($1)));
3279 : primary_expression_no_array_creation OPEN_BRACKET expression_list_arguments CLOSE_BRACKET
3281 $$ = new ElementAccess ((Expression) $1, (Arguments) $3);
3283 | array_creation_expression OPEN_BRACKET expression_list_arguments CLOSE_BRACKET
3285 // LAMESPEC: Not allowed according to specification
3286 $$ = new ElementAccess ((Expression) $1, (Arguments) $3);
3288 | primary_expression_no_array_creation rank_specifiers
3290 // So the super-trick is that primary_expression
3291 // can only be either a SimpleName or a MemberAccess.
3292 // The MemberAccess case arises when you have a fully qualified type-name like :
3294 // SimpleName is when you have
3297 Expression expr = (Expression) $1;
3298 if (expr is ComposedCast){
3299 $$ = new ComposedCast ((ComposedCast)expr, (string) $2);
3300 } else if (expr is ATypeNameExpression){
3302 // So we extract the string corresponding to the SimpleName
3305 $$ = new ComposedCast ((ATypeNameExpression)expr, (string) $2);
3307 Error_ExpectingTypeName (expr);
3308 $$ = TypeManager.system_object_expr;
3316 var list = new List<Expression> (4);
3317 list.Add ((Expression) $1);
3320 | expression_list COMMA expression
3322 var list = (List<Expression>) $1;
3323 list.Add ((Expression) $3);
3326 | expression_list error {
3327 Error_SyntaxError (yyToken);
3332 expression_list_arguments
3333 : expression_list_argument
3335 Arguments args = new Arguments (4);
3336 args.Add ((Argument) $1);
3339 | expression_list_arguments COMMA expression_list_argument
3341 Arguments args = (Arguments) $1;
3342 args.Add ((Argument) $3);
3347 expression_list_argument
3350 $$ = new Argument ((Expression) $1);
3358 $$ = new This (GetLocation ($1));
3363 : BASE DOT IDENTIFIER opt_type_argument_list
3365 var lt = (Tokenizer.LocatedToken) $3;
3366 $$ = new BaseAccess (lt.Value, (TypeArguments) $4, lt.Location);
3368 | BASE OPEN_BRACKET expression_list_arguments CLOSE_BRACKET
3370 $$ = new BaseIndexerAccess ((Arguments) $3, GetLocation ($1));
3374 Error_SyntaxError (yyToken);
3375 $$ = new BaseAccess (null, GetLocation ($2));
3379 post_increment_expression
3380 : primary_expression OP_INC
3382 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement, (Expression) $1, GetLocation ($2));
3386 post_decrement_expression
3387 : primary_expression OP_DEC
3389 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement, (Expression) $1, GetLocation ($2));
3393 object_or_delegate_creation_expression
3394 : new_expr_start open_parens_any opt_argument_list CLOSE_PARENS opt_object_or_collection_initializer
3397 if (RootContext.Version <= LanguageVersion.ISO_2)
3398 Report.FeatureIsNotAvailable (GetLocation ($1), "object initializers");
3400 $$ = new NewInitialize ((Expression) $1, (Arguments) $3, (CollectionOrObjectInitializers) $5, GetLocation ($1));
3403 $$ = new New ((Expression) $1, (Arguments) $3, GetLocation ($1));
3405 | new_expr_start object_or_collection_initializer
3407 if (RootContext.Version <= LanguageVersion.ISO_2)
3408 Report.FeatureIsNotAvailable (GetLocation ($1), "collection initializers");
3410 $$ = new NewInitialize ((Expression) $1, null, (CollectionOrObjectInitializers) $2, GetLocation ($1));
3414 array_creation_expression
3415 : new_expr_start OPEN_BRACKET expression_list CLOSE_BRACKET
3416 opt_rank_specifier // shift/reduce on OPEN_BRACE
3417 opt_array_initializer
3419 $$ = new ArrayCreation ((FullNamedExpression) $1, (List<Expression>) $3, (string) $5, (ArrayInitializer) $6, GetLocation ($1));
3421 | new_expr_start rank_specifiers opt_array_initializer
3424 Report.Error (1586, GetLocation ($1), "Array creation must have array size or array initializer");
3426 $$ = new ArrayCreation ((FullNamedExpression) $1, (string) $2, (ArrayInitializer) $3, GetLocation ($1));
3428 | NEW rank_specifiers array_initializer
3430 if (RootContext.Version <= LanguageVersion.ISO_2)
3431 Report.FeatureIsNotAvailable (GetLocation ($1), "implicitly typed arrays");
3433 $$ = new ImplicitlyTypedArrayCreation ((string) $2, (ArrayInitializer) $3, GetLocation ($1));
3435 | new_expr_start error
3437 Report.Error (1526, GetLocation ($1), "A new expression requires () or [] after type");
3438 $$ = new ArrayCreation ((FullNamedExpression) $1, "[]", null, GetLocation ($1));
3445 ++lexer.parsing_type;
3449 --lexer.parsing_type;
3454 anonymous_type_expression
3455 : NEW OPEN_BRACE anonymous_type_parameters_opt_comma CLOSE_BRACE
3457 if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)
3458 Report.FeatureIsNotSupported (GetLocation ($1), "anonymous types");
3459 else if (RootContext.Version <= LanguageVersion.ISO_2)
3460 Report.FeatureIsNotAvailable (GetLocation ($1), "anonymous types");
3462 $$ = new NewAnonymousType ((List<AnonymousTypeParameter>) $3, current_container, GetLocation ($1));
3466 anonymous_type_parameters_opt_comma
3467 : anonymous_type_parameters_opt
3468 | anonymous_type_parameters COMMA
3471 anonymous_type_parameters_opt
3473 | anonymous_type_parameters
3476 anonymous_type_parameters
3477 : anonymous_type_parameter
3479 var a = new List<AnonymousTypeParameter> (4);
3480 a.Add ((AnonymousTypeParameter) $1);
3483 | anonymous_type_parameters COMMA anonymous_type_parameter
3485 var a = (List<AnonymousTypeParameter>) $1;
3486 a.Add ((AnonymousTypeParameter) $3);
3491 anonymous_type_parameter
3492 : IDENTIFIER ASSIGN variable_initializer
3494 var lt = (Tokenizer.LocatedToken)$1;
3495 $$ = new AnonymousTypeParameter ((Expression)$3, lt.Value, lt.Location);
3499 var lt = (Tokenizer.LocatedToken)$1;
3500 $$ = new AnonymousTypeParameter (new SimpleName (lt.Value, lt.Location),
3501 lt.Value, lt.Location);
3503 | BASE DOT IDENTIFIER opt_type_argument_list
3505 var lt = (Tokenizer.LocatedToken) $3;
3506 BaseAccess ba = new BaseAccess (lt.Value, (TypeArguments) $4, lt.Location);
3507 $$ = new AnonymousTypeParameter (ba, lt.Value, lt.Location);
3511 MemberAccess ma = (MemberAccess) $1;
3512 $$ = new AnonymousTypeParameter (ma, ma.Name, ma.Location);
3516 Report.Error (746, lexer.Location,
3517 "Invalid anonymous type member declarator. Anonymous type members must be a member assignment, simple name or member access expression");
3533 opt_rank_specifier_or_nullable
3541 | opt_nullable rank_specifiers
3544 $$ = "?" + (string) $2;
3552 | rank_specifier rank_specifiers
3554 $$ = (string) $1 + (string) $2;
3559 : OPEN_BRACKET CLOSE_BRACKET
3563 | OPEN_BRACKET dim_separators CLOSE_BRACKET
3565 $$ = "[" + (string) $2 + "]";
3567 | OPEN_BRACKET error
3569 Error_SyntaxError (178, yyToken, "Invalid rank specifier");
3579 | dim_separators COMMA
3581 $$ = (string) $1 + ",";
3585 opt_array_initializer
3597 : OPEN_BRACE CLOSE_BRACE
3599 $$ = new ArrayInitializer (0, GetLocation ($1));
3601 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3603 $$ = new ArrayInitializer ((List<Expression>) $2, GetLocation ($1));
3607 variable_initializer_list
3608 : variable_initializer
3610 var list = new List<Expression> (4);
3611 list.Add ((Expression) $1);
3614 | variable_initializer_list COMMA variable_initializer
3616 var list = (List<Expression>) $1;
3617 list.Add ((Expression) $3);
3622 Error_SyntaxError (yyToken);
3623 $$ = new List<Expression> ();
3630 lexer.TypeOfParsing = true;
3632 open_parens_any typeof_type_expression CLOSE_PARENS
3634 lexer.TypeOfParsing = false;
3635 Expression type = (Expression)$4;
3636 if (type == TypeManager.system_void_expr)
3637 $$ = new TypeOfVoid (GetLocation ($1));
3639 $$ = new TypeOf (type, GetLocation ($1));
3643 typeof_type_expression
3648 Error_TypeExpected (lexer.Location);
3654 : IDENTIFIER generic_dimension
3656 var lt = (Tokenizer.LocatedToken) $1;
3658 $$ = new SimpleName (lt.Value, (int) $2, lt.Location);
3660 | qualified_alias_member IDENTIFIER generic_dimension
3662 var lt1 = (Tokenizer.LocatedToken) $1;
3663 var lt2 = (Tokenizer.LocatedToken) $2;
3665 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, (int) $3, lt1.Location);
3667 | unbound_type_name DOT IDENTIFIER
3669 var lt = (Tokenizer.LocatedToken) $3;
3671 $$ = new MemberAccess ((Expression) $1, lt.Value, lt.Location);
3673 | unbound_type_name DOT IDENTIFIER generic_dimension
3675 var lt = (Tokenizer.LocatedToken) $3;
3677 $$ = new MemberAccess ((Expression) $1, lt.Value, (int) $4, lt.Location);
3679 | namespace_or_type_name DOT IDENTIFIER generic_dimension
3681 var lt = (Tokenizer.LocatedToken) $3;
3682 MemberName name = (MemberName) $1;
3684 $$ = new MemberAccess (name.GetTypeExpression (), lt.Value, (int) $4, lt.Location);
3691 if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)
3692 Report.FeatureIsNotSupported (GetLocation ($1), "generics");
3693 else if (RootContext.Version < LanguageVersion.ISO_2)
3694 Report.FeatureIsNotAvailable (GetLocation ($1), "generics");
3700 qualified_alias_member
3701 : IDENTIFIER DOUBLE_COLON
3703 var lt = (Tokenizer.LocatedToken) $1;
3704 if (RootContext.Version == LanguageVersion.ISO_1)
3705 Report.FeatureIsNotAvailable (lt.Location, "namespace alias qualifier");
3712 : SIZEOF open_parens_any type CLOSE_PARENS {
3713 $$ = new SizeOf ((Expression) $3, GetLocation ($1));
3718 : CHECKED open_parens_any expression CLOSE_PARENS
3720 $$ = new CheckedExpr ((Expression) $3, GetLocation ($1));
3724 unchecked_expression
3725 : UNCHECKED open_parens_any expression CLOSE_PARENS
3727 $$ = new UnCheckedExpr ((Expression) $3, GetLocation ($1));
3731 pointer_member_access
3732 : primary_expression OP_PTR IDENTIFIER
3735 var lt = (Tokenizer.LocatedToken) $3;
3737 deref = new Indirection ((Expression) $1, lt.Location);
3738 $$ = new MemberAccess (deref, lt.Value);
3742 anonymous_method_expression
3743 : DELEGATE opt_anonymous_method_signature
3745 start_anonymous (false, (ParametersCompiled) $2, GetLocation ($1));
3749 $$ = end_anonymous ((ToplevelBlock) $4);
3753 opt_anonymous_method_signature
3756 $$ = ParametersCompiled.Undefined;
3758 | anonymous_method_signature
3761 anonymous_method_signature
3764 valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out;
3766 opt_formal_parameter_list CLOSE_PARENS
3768 valid_param_mod = 0;
3773 default_value_expression
3774 : DEFAULT open_parens_any type CLOSE_PARENS
3776 if (RootContext.Version < LanguageVersion.ISO_2)
3777 Report.FeatureIsNotAvailable (GetLocation ($1), "default value expression");
3779 $$ = new DefaultValueExpression ((Expression) $3, GetLocation ($1));
3784 : primary_expression
3785 | BANG prefixed_unary_expression
3787 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, GetLocation ($1));
3789 | TILDE prefixed_unary_expression
3791 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, GetLocation ($1));
3797 : OPEN_PARENS_CAST type CLOSE_PARENS prefixed_unary_expression
3799 $$ = new Cast ((FullNamedExpression) $2, (Expression) $4, GetLocation ($1));
3801 | OPEN_PARENS builtin_types CLOSE_PARENS prefixed_unary_expression
3803 $$ = new Cast ((FullNamedExpression) $2, (Expression) $4, GetLocation ($1));
3808 // The idea to split this out is from Rhys' grammar
3809 // to solve the problem with casts.
3811 prefixed_unary_expression
3813 | PLUS prefixed_unary_expression
3815 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, GetLocation ($1));
3817 | MINUS prefixed_unary_expression
3819 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, GetLocation ($1));
3821 | OP_INC prefixed_unary_expression
3823 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement, (Expression) $2, GetLocation ($1));
3825 | OP_DEC prefixed_unary_expression
3827 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement, (Expression) $2, GetLocation ($1));
3829 | STAR prefixed_unary_expression
3831 $$ = new Indirection ((Expression) $2, GetLocation ($1));
3833 | BITWISE_AND prefixed_unary_expression
3835 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, GetLocation ($1));
3839 multiplicative_expression
3840 : prefixed_unary_expression
3841 | multiplicative_expression STAR prefixed_unary_expression
3843 $$ = new Binary (Binary.Operator.Multiply,
3844 (Expression) $1, (Expression) $3, GetLocation ($2));
3846 | multiplicative_expression DIV prefixed_unary_expression
3848 $$ = new Binary (Binary.Operator.Division,
3849 (Expression) $1, (Expression) $3, GetLocation ($2));
3851 | multiplicative_expression PERCENT prefixed_unary_expression
3853 $$ = new Binary (Binary.Operator.Modulus,
3854 (Expression) $1, (Expression) $3, GetLocation ($2));
3859 : multiplicative_expression
3860 | additive_expression PLUS multiplicative_expression
3862 $$ = new Binary (Binary.Operator.Addition,
3863 (Expression) $1, (Expression) $3, GetLocation ($2));
3865 | additive_expression MINUS multiplicative_expression
3867 $$ = new Binary (Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, GetLocation ($2));
3869 | parenthesized_expression MINUS multiplicative_expression
3871 // Shift/Reduce conflict
3872 $$ = new Binary (Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, GetLocation ($2));
3874 | additive_expression AS type
3876 $$ = new As ((Expression) $1, (Expression) $3, GetLocation ($2));
3878 | additive_expression IS type
3880 $$ = new Is ((Expression) $1, (Expression) $3, GetLocation ($2));
3885 : additive_expression
3886 | shift_expression OP_SHIFT_LEFT additive_expression
3888 $$ = new Binary (Binary.Operator.LeftShift,
3889 (Expression) $1, (Expression) $3, GetLocation ($2));
3891 | shift_expression OP_SHIFT_RIGHT additive_expression
3893 $$ = new Binary (Binary.Operator.RightShift,
3894 (Expression) $1, (Expression) $3, GetLocation ($2));
3898 relational_expression
3900 | relational_expression OP_LT shift_expression
3902 $$ = new Binary (Binary.Operator.LessThan,
3903 (Expression) $1, (Expression) $3, GetLocation ($2));
3905 | relational_expression OP_GT shift_expression
3907 $$ = new Binary (Binary.Operator.GreaterThan,
3908 (Expression) $1, (Expression) $3, GetLocation ($2));
3910 | relational_expression OP_LE shift_expression
3912 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3913 (Expression) $1, (Expression) $3, GetLocation ($2));
3915 | relational_expression OP_GE shift_expression
3917 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3918 (Expression) $1, (Expression) $3, GetLocation ($2));
3923 : relational_expression
3924 | equality_expression OP_EQ relational_expression
3926 $$ = new Binary (Binary.Operator.Equality,
3927 (Expression) $1, (Expression) $3, GetLocation ($2));
3929 | equality_expression OP_NE relational_expression
3931 $$ = new Binary (Binary.Operator.Inequality,
3932 (Expression) $1, (Expression) $3, GetLocation ($2));
3937 : equality_expression
3938 | and_expression BITWISE_AND equality_expression
3940 $$ = new Binary (Binary.Operator.BitwiseAnd,
3941 (Expression) $1, (Expression) $3, GetLocation ($2));
3945 exclusive_or_expression
3947 | exclusive_or_expression CARRET and_expression
3949 $$ = new Binary (Binary.Operator.ExclusiveOr,
3950 (Expression) $1, (Expression) $3, GetLocation ($2));
3954 inclusive_or_expression
3955 : exclusive_or_expression
3956 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3958 $$ = new Binary (Binary.Operator.BitwiseOr,
3959 (Expression) $1, (Expression) $3, GetLocation ($2));
3963 conditional_and_expression
3964 : inclusive_or_expression
3965 | conditional_and_expression OP_AND inclusive_or_expression
3967 $$ = new Binary (Binary.Operator.LogicalAnd,
3968 (Expression) $1, (Expression) $3, GetLocation ($2));
3972 conditional_or_expression
3973 : conditional_and_expression
3974 | conditional_or_expression OP_OR conditional_and_expression
3976 $$ = new Binary (Binary.Operator.LogicalOr,
3977 (Expression) $1, (Expression) $3, GetLocation ($2));
3981 null_coalescing_expression
3982 : conditional_or_expression
3983 | conditional_or_expression OP_COALESCING null_coalescing_expression
3985 if (RootContext.Version < LanguageVersion.ISO_2)
3986 Report.FeatureIsNotAvailable (GetLocation ($2), "null coalescing operator");
3988 $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $3, GetLocation ($2));
3992 conditional_expression
3993 : null_coalescing_expression
3994 | null_coalescing_expression INTERR expression COLON expression
3996 $$ = new Conditional (new BooleanExpression ((Expression) $1), (Expression) $3, (Expression) $5);
4000 assignment_expression
4001 : prefixed_unary_expression ASSIGN expression
4003 $$ = new SimpleAssign ((Expression) $1, (Expression) $3);
4005 | prefixed_unary_expression OP_MULT_ASSIGN expression
4007 $$ = new CompoundAssign (
4008 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
4010 | prefixed_unary_expression OP_DIV_ASSIGN expression
4012 $$ = new CompoundAssign (
4013 Binary.Operator.Division, (Expression) $1, (Expression) $3);
4015 | prefixed_unary_expression OP_MOD_ASSIGN expression
4017 $$ = new CompoundAssign (
4018 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
4020 | prefixed_unary_expression OP_ADD_ASSIGN expression
4022 $$ = new CompoundAssign (
4023 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
4025 | prefixed_unary_expression OP_SUB_ASSIGN expression
4027 $$ = new CompoundAssign (
4028 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
4030 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
4032 $$ = new CompoundAssign (
4033 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
4035 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
4037 $$ = new CompoundAssign (
4038 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
4040 | prefixed_unary_expression OP_AND_ASSIGN expression
4042 $$ = new CompoundAssign (
4043 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
4045 | prefixed_unary_expression OP_OR_ASSIGN expression
4047 $$ = new CompoundAssign (
4048 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
4050 | prefixed_unary_expression OP_XOR_ASSIGN expression
4052 $$ = new CompoundAssign (
4053 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
4057 lambda_parameter_list
4060 var pars = new List<Parameter> (4);
4061 pars.Add ((Parameter) $1);
4065 | lambda_parameter_list COMMA lambda_parameter
4067 var pars = (List<Parameter>) $1;
4068 Parameter p = (Parameter)$3;
4069 if (pars[0].GetType () != p.GetType ()) {
4070 Report.Error (748, p.Location, "All lambda parameters must be typed either explicitly or implicitly");
4079 : parameter_modifier parameter_type IDENTIFIER
4081 var lt = (Tokenizer.LocatedToken) $3;
4083 $$ = new Parameter ((FullNamedExpression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
4085 | parameter_type IDENTIFIER
4087 var lt = (Tokenizer.LocatedToken) $2;
4089 $$ = new Parameter ((FullNamedExpression) $1, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
4093 var lt = (Tokenizer.LocatedToken) $1;
4094 $$ = new ImplicitLambdaParameter (lt.Value, lt.Location);
4098 opt_lambda_parameter_list
4099 : /* empty */ { $$ = ParametersCompiled.EmptyReadOnlyParameters; }
4100 | lambda_parameter_list {
4101 var pars_list = (List<Parameter>) $1;
4102 $$ = new ParametersCompiled (compiler, pars_list.ToArray ());
4106 lambda_expression_body
4108 start_block (lexer.Location);
4112 Block b = end_block (lexer.Location);
4113 b.AddStatement (new ContextualReturn ((Expression) $2));
4124 var lt = (Tokenizer.LocatedToken) $1;
4125 Parameter p = new ImplicitLambdaParameter (lt.Value, lt.Location);
4126 start_anonymous (true, new ParametersCompiled (compiler, p), GetLocation ($1));
4128 lambda_expression_body
4130 $$ = end_anonymous ((ToplevelBlock) $4);
4132 | OPEN_PARENS_LAMBDA
4134 if (RootContext.Version <= LanguageVersion.ISO_2)
4135 Report.FeatureIsNotAvailable (GetLocation ($1), "lambda expressions");
4137 valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out;
4139 opt_lambda_parameter_list CLOSE_PARENS ARROW
4141 valid_param_mod = 0;
4142 start_anonymous (true, (ParametersCompiled) $3, GetLocation ($1));
4144 lambda_expression_body
4146 $$ = end_anonymous ((ToplevelBlock) $7);
4151 : assignment_expression
4152 | non_assignment_expression
4155 non_assignment_expression
4156 : conditional_expression
4168 $$ = new BooleanExpression ((Expression) $1);
4181 lexer.ConstraintsParsing = true;
4183 type_declaration_name
4185 MemberName name = MakeName ((MemberName) $6);
4186 push_current_class (new Class (current_namespace, current_class, name, (Modifiers) $2, (Attributes) $1), $3);
4189 opt_type_parameter_constraints_clauses
4191 lexer.ConstraintsParsing = false;
4193 current_class.SetParameterInfo ((List<Constraints>) $9);
4195 if (RootContext.Documentation != null) {
4196 current_container.DocComment = Lexer.consume_doc_comment ();
4197 Lexer.doc_state = XmlCommentState.Allowed;
4202 --lexer.parsing_declaration;
4203 if (RootContext.Documentation != null)
4204 Lexer.doc_state = XmlCommentState.Allowed;
4208 $$ = pop_current_class ();
4216 { $$ = $1; } // location
4220 : /* empty */ { $$ = (int) 0; }
4226 | modifiers modifier
4228 var m1 = (Modifiers) $1;
4229 var m2 = (Modifiers) $2;
4231 if ((m1 & m2) != 0) {
4232 Location l = lexer.Location;
4233 Report.Error (1004, l, "Duplicate `{0}' modifier", ModifiersExtensions.Name (m2));
4243 if (current_container == RootContext.ToplevelTypes)
4244 Report.Error (1530, GetLocation ($1), "Keyword `new' is not allowed on namespace elements");
4246 | PUBLIC { $$ = Modifiers.PUBLIC; }
4247 | PROTECTED { $$ = Modifiers.PROTECTED; }
4248 | INTERNAL { $$ = Modifiers.INTERNAL; }
4249 | PRIVATE { $$ = Modifiers.PRIVATE; }
4250 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
4251 | SEALED { $$ = Modifiers.SEALED; }
4252 | STATIC { $$ = Modifiers.STATIC; }
4253 | READONLY { $$ = Modifiers.READONLY; }
4254 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
4255 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
4256 | EXTERN { $$ = Modifiers.EXTERN; }
4257 | VOLATILE { $$ = Modifiers.VOLATILE; }
4258 | UNSAFE { $$ = Modifiers.UNSAFE; }
4269 current_container.AddBasesForPart (current_class, (List<FullNamedExpression>) $2);
4273 opt_type_parameter_constraints_clauses
4274 : /* empty */ { $$ = null; }
4275 | type_parameter_constraints_clauses
4281 type_parameter_constraints_clauses
4282 : type_parameter_constraints_clause
4284 var constraints = new List<Constraints> (1);
4285 constraints.Add ((Constraints) $1);
4288 | type_parameter_constraints_clauses type_parameter_constraints_clause
4290 var constraints = (List<Constraints>) $1;
4291 Constraints new_constraint = (Constraints)$2;
4293 foreach (Constraints c in constraints) {
4294 if (new_constraint.TypeParameter.Value == c.TypeParameter.Value) {
4295 Report.Error (409, new_constraint.Location,
4296 "A constraint clause has already been specified for type parameter `{0}'",
4297 new_constraint.TypeParameter.Value);
4301 constraints.Add (new_constraint);
4306 type_parameter_constraints_clause
4307 : WHERE IDENTIFIER COLON type_parameter_constraints
4309 var lt = (Tokenizer.LocatedToken) $2;
4310 $$ = new Constraints (new SimpleMemberName (lt.Value, lt.Location), (List<FullNamedExpression>) $4, GetLocation ($1));
4314 type_parameter_constraints
4315 : type_parameter_constraint
4317 var constraints = new List<FullNamedExpression> (1);
4318 constraints.Add ((FullNamedExpression) $1);
4321 | type_parameter_constraints COMMA type_parameter_constraint
4323 var constraints = (List<FullNamedExpression>) $1;
4324 var prev = constraints [constraints.Count - 1] as SpecialContraintExpr;
4325 if (prev != null && (prev.Constraint & SpecialConstraint.Constructor) != 0) {
4326 Report.Error (401, GetLocation ($2), "The `new()' constraint must be the last constraint specified");
4329 prev = $3 as SpecialContraintExpr;
4331 if ((prev.Constraint & (SpecialConstraint.Class | SpecialConstraint.Struct)) != 0) {
4332 Report.Error (449, prev.Location, "The `class' or `struct' constraint must be the first constraint specified");
4334 prev = constraints [0] as SpecialContraintExpr;
4335 if (prev != null && (prev.Constraint & SpecialConstraint.Struct) != 0) {
4336 Report.Error (451, GetLocation ($3), "The `new()' constraint cannot be used with the `struct' constraint");
4341 constraints.Add ((FullNamedExpression) $3);
4346 type_parameter_constraint
4349 if ($1 is ComposedCast)
4350 Report.Error (706, GetLocation ($1), "Invalid constraint type `{0}'", ((ComposedCast)$1).GetSignatureForError ());
4354 | NEW OPEN_PARENS CLOSE_PARENS
4356 $$ = new SpecialContraintExpr (SpecialConstraint.Constructor, GetLocation ($1));
4360 $$ = new SpecialContraintExpr (SpecialConstraint.Class, GetLocation ($1));
4364 $$ = new SpecialContraintExpr (SpecialConstraint.Struct, GetLocation ($1));
4368 opt_type_parameter_variance
4373 | type_parameter_variance
4375 if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)
4376 Report.FeatureIsNotSupported (lexer.Location, "generic type variance");
4377 else if (RootContext.Version <= LanguageVersion.V_3)
4378 Report.FeatureIsNotAvailable (lexer.Location, "generic type variance");
4384 type_parameter_variance
4387 $$ = Variance.Covariant;
4391 $$ = Variance.Contravariant;
4400 // A block is "contained" on the following places:
4402 // property_declaration as part of the accessor body (get/set)
4403 // operator_declaration
4404 // constructor_declaration
4405 // destructor_declaration
4406 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
4411 ++lexer.parsing_block;
4412 start_block (GetLocation ($1));
4414 opt_statement_list block_end
4423 --lexer.parsing_block;
4424 $$ = end_block (GetLocation ($1));
4426 | COMPLETE_COMPLETION
4428 --lexer.parsing_block;
4429 $$ = end_block (lexer.Location);
4437 ++lexer.parsing_block;
4438 current_block.StartLocation = GetLocation ($1);
4440 opt_statement_list CLOSE_BRACE
4442 --lexer.parsing_block;
4443 $$ = end_block (GetLocation ($4));
4454 | statement_list statement
4458 : declaration_statement
4460 if ($1 != null && (Block) $1 != current_block){
4461 current_block.AddStatement ((Statement) $1);
4462 current_block = (Block) $1;
4465 | valid_declaration_statement
4467 current_block.AddStatement ((Statement) $1);
4473 // The interactive_statement and its derivatives are only
4474 // used to provide a special version of `expression_statement'
4475 // that has a side effect of assigning the expression to
4478 interactive_statement_list
4479 : interactive_statement
4480 | interactive_statement_list interactive_statement
4483 interactive_statement
4484 : declaration_statement
4486 if ($1 != null && (Block) $1 != current_block){
4487 current_block.AddStatement ((Statement) $1);
4488 current_block = (Block) $1;
4491 | interactive_valid_declaration_statement
4493 current_block.AddStatement ((Statement) $1);
4498 valid_declaration_statement
4501 | expression_statement
4502 | selection_statement
4503 | iteration_statement
4507 | unchecked_statement
4514 interactive_valid_declaration_statement
4517 | interactive_expression_statement
4518 | selection_statement
4519 | iteration_statement
4523 | unchecked_statement
4531 : valid_declaration_statement
4532 | declaration_statement
4534 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4539 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4547 $$ = new EmptyStatement (GetLocation ($1));
4554 var lt = (Tokenizer.LocatedToken) $1;
4555 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
4557 if (current_block.AddLabel (labeled))
4558 current_block.AddStatement (labeled);
4563 declaration_statement
4564 : local_variable_declaration SEMICOLON
4567 var de = (Tuple<FullNamedExpression, List<object>>) $1;
4568 $$ = declare_local_variables (de.Item1, de.Item2, de.Item1.Location);
4572 | local_constant_declaration SEMICOLON
4575 var de = (Tuple<FullNamedExpression, List<object>>) $1;
4577 $$ = declare_local_constants (de.Item1, de.Item2);
4583 * The following is from Rhys' grammar:
4584 * > Types in local variable declarations must be recognized as
4585 * > expressions to prevent reduce/reduce errors in the grammar.
4586 * > The expressions are converted into types during semantic analysis.
4589 : primary_expression_no_array_creation opt_rank_specifier_or_nullable
4591 // FIXME: Do something smart here regarding the composition of the type.
4593 // Ok, the above "primary_expression" is there to get rid of
4594 // both reduce/reduce and shift/reduces in the grammar, it should
4595 // really just be "type_name". If you use type_name, a reduce/reduce
4596 // creeps up. If you use namespace_or_type_name (which is all we need
4597 // really) two shift/reduces appear.
4600 // So the super-trick is that primary_expression
4601 // can only be either a SimpleName or a MemberAccess.
4602 // The MemberAccess case arises when you have a fully qualified type-name like :
4604 // SimpleName is when you have
4607 Expression expr = (Expression) $1;
4608 string rank_or_nullable = (string) $2;
4610 if (expr is ComposedCast){
4611 $$ = new ComposedCast ((ComposedCast)expr, rank_or_nullable);
4612 } else if (expr is ATypeNameExpression){
4614 // So we extract the string corresponding to the SimpleName
4617 if (rank_or_nullable.Length == 0) {
4618 SimpleName sn = expr as SimpleName;
4619 if (sn != null && sn.Name == "var")
4620 $$ = new VarExpr (sn.Location);
4624 $$ = new ComposedCast ((ATypeNameExpression)expr, rank_or_nullable);
4627 Error_ExpectingTypeName (expr);
4628 $$ = TypeManager.system_object_expr;
4631 | builtin_types opt_rank_specifier_or_nullable
4633 if ((string) $2 == "")
4636 $$ = new ComposedCast ((FullNamedExpression) $1, (string) $2, lexer.Location);
4638 | VOID opt_rank_specifier
4640 Expression.Error_VoidInvalidInTheContext (GetLocation ($1), Report);
4641 $$ = TypeManager.system_void_expr;
4645 local_variable_pointer_type
4646 : primary_expression_no_array_creation STAR
4648 ATypeNameExpression expr = $1 as ATypeNameExpression;
4651 $$ = new ComposedCast (expr, "*");
4653 Error_ExpectingTypeName ((Expression)$1);
4657 | builtin_types STAR
4659 $$ = new ComposedCast ((FullNamedExpression) $1, "*", GetLocation ($1));
4663 $$ = new ComposedCast (TypeManager.system_void_expr, "*", GetLocation ($1));
4665 | local_variable_pointer_type STAR
4667 $$ = new ComposedCast ((FullNamedExpression) $1, "*");
4673 | local_variable_pointer_type opt_rank_specifier
4676 string rank = (string)$2;
4681 $$ = new ComposedCast ((FullNamedExpression) $1, rank);
4688 local_variable_declaration
4689 : local_variable_type local_variable_declarators
4692 VarExpr ve = $1 as VarExpr;
4694 if (!((VariableDeclaration) ((List<object>)$2) [0]).HasInitializer)
4695 ve.VariableInitializersCount = 0;
4697 ve.VariableInitializersCount = ((List<object>)$2).Count;
4700 $$ = new Tuple<FullNamedExpression, List<object>> ((FullNamedExpression) $1, (List<object>) $2);
4706 local_constant_declaration
4707 : CONST variable_type constant_declarators
4710 $$ = new Tuple<FullNamedExpression, List<object>> ((FullNamedExpression) $2, (List<object>) $3);
4716 expression_statement
4717 : statement_expression SEMICOLON { $$ = $1; }
4718 | statement_expression COMPLETE_COMPLETION { $$ = $1; }
4721 interactive_expression_statement
4722 : interactive_statement_expression SEMICOLON { $$ = $1; }
4723 | interactive_statement_expression COMPLETE_COMPLETION { $$ = $1; }
4727 // We have to do the wrapping here and not in the case above,
4728 // because statement_expression is used for example in for_statement
4730 statement_expression
4733 ExpressionStatement s = $1 as ExpressionStatement;
4735 Expression.Error_InvalidExpressionStatement (Report, GetLocation ($1));
4736 s = EmptyExpressionStatement.Instance;
4739 $$ = new StatementExpression (s);
4743 Error_SyntaxError (yyToken);
4748 interactive_statement_expression
4751 Expression expr = (Expression) $1;
4752 ExpressionStatement s;
4754 s = new OptionalAssign (new SimpleName ("$retval", lexer.Location), expr, lexer.Location);
4755 $$ = new StatementExpression (s);
4759 Error_SyntaxError (yyToken);
4760 $$ = new EmptyStatement (GetLocation ($1));
4770 : IF open_parens_any boolean_expression CLOSE_PARENS
4773 if ($5 is EmptyStatement)
4774 Report.Warning (642, 3, GetLocation ($5), "Possible mistaken empty statement");
4776 $$ = new If ((BooleanExpression) $3, (Statement) $5, GetLocation ($1));
4778 | IF open_parens_any boolean_expression CLOSE_PARENS
4779 embedded_statement ELSE embedded_statement
4781 $$ = new If ((BooleanExpression) $3, (Statement) $5, (Statement) $7, GetLocation ($1));
4783 if ($5 is EmptyStatement)
4784 Report.Warning (642, 3, GetLocation ($5), "Possible mistaken empty statement");
4785 if ($7 is EmptyStatement)
4786 Report.Warning (642, 3, GetLocation ($7), "Possible mistaken empty statement");
4791 : SWITCH open_parens_any
4793 if (switch_stack == null)
4794 switch_stack = new Stack<Block> (2);
4795 switch_stack.Push (current_block);
4797 expression CLOSE_PARENS
4800 $$ = new Switch ((Expression) $4, (List<SwitchSection>) $6, GetLocation ($1));
4801 current_block = (Block) switch_stack.Pop ();
4817 Report.Warning (1522, 1, lexer.Location, "Empty switch block");
4818 $$ = new List<SwitchSection> ();
4826 var sections = new List<SwitchSection> (4);
4828 sections.Add ((SwitchSection) $1);
4831 | switch_sections switch_section
4833 var sections = (List<SwitchSection>) $1;
4835 sections.Add ((SwitchSection) $2);
4843 current_block = current_block.CreateSwitchBlock (lexer.Location);
4847 $$ = new SwitchSection ((List<SwitchLabel>) $1, current_block.Explicit);
4854 var labels = new List<SwitchLabel> (4);
4856 labels.Add ((SwitchLabel) $1);
4859 | switch_labels switch_label
4861 var labels = (List<SwitchLabel>) ($1);
4862 labels.Add ((SwitchLabel) $2);
4869 : CASE constant_expression COLON
4871 $$ = new SwitchLabel ((Expression) $2, GetLocation ($1));
4875 $$ = new SwitchLabel (null, GetLocation ($1));
4887 : WHILE open_parens_any boolean_expression CLOSE_PARENS embedded_statement
4889 Location l = GetLocation ($1);
4890 $$ = new While ((BooleanExpression) $3, (Statement) $5, l);
4895 : DO embedded_statement
4896 WHILE open_parens_any boolean_expression CLOSE_PARENS SEMICOLON
4898 Location l = GetLocation ($1);
4900 $$ = new Do ((Statement) $2, (BooleanExpression) $5, l);
4905 : FOR open_parens_any opt_for_initializer SEMICOLON
4907 Location l = lexer.Location;
4909 Block assign_block = current_block;
4911 if ($3 is Tuple<FullNamedExpression, List<object>>){
4912 var de = (Tuple<FullNamedExpression, List<object>>) $3;
4914 var type = de.Item1;
4916 foreach (VariableDeclaration decl in de.Item2){
4920 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4924 Expression expr = decl.GetInitializer (type);
4926 LocalVariableReference var;
4927 var = new LocalVariableReference (assign_block, decl.identifier, l);
4930 Assign a = new SimpleAssign (var, expr, decl.Location);
4932 assign_block.AddStatement (new StatementExpression (a));
4936 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4937 // This can be referred to as $5 below.
4943 opt_for_condition SEMICOLON
4944 opt_for_iterator CLOSE_PARENS
4947 Location l = GetLocation ($1);
4949 For f = new For ((Statement) $5, (BooleanExpression) $6, (Statement) $8, (Statement) $10, l);
4951 current_block.AddStatement (f);
4953 $$ = end_block (lexer.Location);
4958 : /* empty */ { $$ = new EmptyStatement (lexer.Location); }
4963 : local_variable_declaration
4964 | statement_expression_list
4968 : /* empty */ { $$ = null; }
4969 | boolean_expression
4973 : /* empty */ { $$ = new EmptyStatement (lexer.Location); }
4978 : statement_expression_list
4981 statement_expression_list
4982 : statement_expression
4984 // CHANGE: was `null'
4985 Statement s = (Statement) $1;
4986 Block b = new Block (current_block, s.loc, lexer.Location);
4991 | statement_expression_list COMMA statement_expression
4993 Block b = (Block) $1;
4995 b.AddStatement ((Statement) $3);
5001 : FOREACH open_parens_any type IN expression CLOSE_PARENS
5003 Report.Error (230, GetLocation ($1), "Type and identifier are both required in a foreach statement");
5006 | FOREACH open_parens_any type IDENTIFIER IN
5007 expression CLOSE_PARENS
5009 start_block (lexer.Location);
5010 Block foreach_block = current_block;
5012 var lt = (Tokenizer.LocatedToken) $4;
5013 Location l = lt.Location;
5014 LocalInfo vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
5016 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
5018 // Get a writable reference to this read-only variable.
5020 // Note that the $$ here refers to the value of _this_ code block,
5021 // not the value of the LHS non-terminal. This can be referred to as $8 below.
5022 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
5029 LocalVariableReference v = (LocalVariableReference) $8;
5030 Location l = GetLocation ($1);
5033 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
5034 current_block.AddStatement (f);
5037 $$ = end_block (lexer.Location);
5043 | continue_statement
5053 $$ = new Break (GetLocation ($1));
5058 : CONTINUE SEMICOLON
5060 $$ = new Continue (GetLocation ($1));
5065 : GOTO IDENTIFIER SEMICOLON
5067 var lt = (Tokenizer.LocatedToken) $2;
5068 $$ = new Goto (lt.Value, lt.Location);
5070 | GOTO CASE constant_expression SEMICOLON
5072 $$ = new GotoCase ((Expression) $3, GetLocation ($1));
5074 | GOTO DEFAULT SEMICOLON
5076 $$ = new GotoDefault (GetLocation ($1));
5081 : RETURN opt_expression SEMICOLON
5083 $$ = new Return ((Expression) $2, GetLocation ($1));
5088 : THROW opt_expression SEMICOLON
5090 $$ = new Throw ((Expression) $2, GetLocation ($1));
5095 : IDENTIFIER RETURN expression SEMICOLON
5097 var lt = (Tokenizer.LocatedToken) $1;
5098 string s = lt.Value;
5100 Report.Error (1003, lt.Location, "; expected");
5103 if (RootContext.Version == LanguageVersion.ISO_1){
5104 Report.FeatureIsNotAvailable (lt.Location, "yield statement");
5107 current_block.Toplevel.IsIterator = true;
5108 $$ = new Yield ((Expression) $3, lt.Location);
5110 | IDENTIFIER RETURN SEMICOLON
5112 Report.Error (1627, GetLocation ($2), "Expression expected after yield return");
5115 | IDENTIFIER BREAK SEMICOLON
5117 var lt = (Tokenizer.LocatedToken) $1;
5118 string s = lt.Value;
5120 Report.Error (1003, lt.Location, "; expected");
5123 if (RootContext.Version == LanguageVersion.ISO_1){
5124 Report.FeatureIsNotAvailable (lt.Location, "yield statement");
5128 current_block.Toplevel.IsIterator = true;
5129 $$ = new YieldBreak (lt.Location);
5139 : TRY block catch_clauses
5141 $$ = new TryCatch ((Block) $2, (List<Catch>) $3, GetLocation ($1), false);
5143 | TRY block FINALLY block
5145 $$ = new TryFinally ((Statement) $2, (Block) $4, GetLocation ($1));
5147 | TRY block catch_clauses FINALLY block
5149 $$ = new TryFinally (new TryCatch ((Block) $2, (List<Catch>) $3, GetLocation ($1), true), (Block) $5, GetLocation ($1));
5153 Report.Error (1524, GetLocation ($1), "Expected catch or finally");
5161 var l = new List<Catch> (2);
5166 | catch_clauses catch_clause
5168 var l = (List<Catch>) $1;
5170 Catch c = (Catch) $2;
5171 if (l [0].IsGeneral) {
5172 Report.Error (1017, c.loc, "Try statement already has an empty catch block");
5185 : /* empty */ { $$ = null; }
5190 : CATCH opt_catch_args
5193 var cc = (Tuple<FullNamedExpression, Tokenizer.LocatedToken>) $2;
5197 List<object> one = new List<object> (1);
5199 one.Add (new VariableDeclaration (lt, null));
5201 start_block (lexer.Location);
5202 current_block = declare_local_variables (cc.Item1, one, lt.Location);
5206 Expression type = null;
5208 Block var_block = null;
5211 var cc = (Tuple<FullNamedExpression, Tokenizer.LocatedToken>) $2;
5217 var_block = end_block (lexer.Location);
5221 $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
5226 : /* empty */ { $$ = null; }
5231 : open_parens_any type opt_identifier CLOSE_PARENS
5233 $$ = new Tuple<FullNamedExpression, Tokenizer.LocatedToken> ((FullNamedExpression)$2, (Tokenizer.LocatedToken) $3);
5235 | open_parens_any CLOSE_PARENS
5237 Report.Error (1015, GetLocation ($1), "A type that derives from `System.Exception', `object', or `string' expected");
5245 $$ = new Checked ((Block) $2);
5252 $$ = new Unchecked ((Block) $2);
5259 RootContext.CheckUnsafeOption (GetLocation ($1), Report);
5261 $$ = new Unsafe ((Block) $3);
5266 : FIXED open_parens_any
5267 type_and_void fixed_pointer_declarators
5270 start_block (lexer.Location);
5274 Expression type = (Expression) $3;
5275 var list = (List<KeyValuePair<Tokenizer.LocatedToken, Expression>>) $4;
5276 Fixed f = new Fixed (type,
5277 list.ConvertAll (i => {
5278 var v = new KeyValuePair<LocalInfo, Expression> (current_block.AddVariable (type, i.Key.Value, i.Key.Location), i.Value);
5279 if (v.Key != null) {
5280 v.Key.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
5281 v.Key.Pinned = true;
5284 }), (Statement) $7, GetLocation ($1));
5286 current_block.AddStatement (f);
5288 $$ = end_block (lexer.Location);
5292 fixed_pointer_declarators
5293 : fixed_pointer_declarator {
5294 var declarators = new List<KeyValuePair<Tokenizer.LocatedToken, Expression>> (2);
5296 declarators.Add ((KeyValuePair<Tokenizer.LocatedToken, Expression>)$1);
5299 | fixed_pointer_declarators COMMA fixed_pointer_declarator
5301 var declarators = (List<KeyValuePair<Tokenizer.LocatedToken, Expression>>) $1;
5303 declarators.Add ((KeyValuePair<Tokenizer.LocatedToken, Expression>)$3);
5308 fixed_pointer_declarator
5309 : IDENTIFIER ASSIGN expression
5311 var lt = (Tokenizer.LocatedToken) $1;
5312 $$ = new KeyValuePair<Tokenizer.LocatedToken, Expression> (lt, (Expression) $3);
5316 Report.Error (210, ((Tokenizer.LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
5322 : LOCK open_parens_any expression CLOSE_PARENS
5328 $$ = new Lock ((Expression) $3, (Statement) $6, GetLocation ($1));
5333 : USING open_parens_any local_variable_declaration CLOSE_PARENS
5335 start_block (lexer.Location);
5336 Block assign_block = current_block;
5338 var de = (Tuple<FullNamedExpression, List<object>>) $3;
5339 Location l = GetLocation ($1);
5341 var vars = new Stack<Tuple<LocalVariableReference, Expression>> ();
5343 foreach (VariableDeclaration decl in de.Item2) {
5344 LocalInfo vi = current_block.AddVariable (de.Item1, decl.identifier, decl.Location);
5347 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
5349 Expression expr = decl.GetInitializer (de.Item1);
5351 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
5354 LocalVariableReference var;
5356 // Get a writable reference to this read-only variable.
5357 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
5359 // This is so that it is not a warning on using variables
5362 vars.Push (new Tuple<LocalVariableReference, Expression> (var, expr));
5364 // Assign a = new SimpleAssign (var, expr, decl.Location);
5365 // assign_block.AddStatement (new StatementExpression (a));
5368 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
5369 // It can be referred to as $5 below.
5374 Statement stmt = (Statement) $6;
5375 var vars = (Stack<Tuple<LocalVariableReference, Expression>>) $5;
5376 Location l = GetLocation ($1);
5378 while (vars.Count > 0) {
5379 var de = vars.Pop ();
5380 stmt = new Using (de.Item1, de.Item2, stmt, l);
5382 current_block.AddStatement (stmt);
5383 $$ = end_block (lexer.Location);
5385 | USING open_parens_any expression CLOSE_PARENS
5387 start_block (lexer.Location);
5391 current_block.AddStatement (new UsingTemporary ((Expression) $3, (Statement) $6, GetLocation ($1)));
5392 $$ = end_block (lexer.Location);
5400 : first_from_clause query_body
5402 lexer.query_parsing = false;
5404 Linq.AQueryClause from = $1 as Linq.AQueryClause;
5406 from.Tail.Next = (Linq.AQueryClause)$2;
5409 current_block.SetEndLocation (lexer.Location);
5410 current_block = current_block.Parent;
5412 | nested_from_clause query_body
5414 Linq.AQueryClause from = $1 as Linq.AQueryClause;
5416 from.Tail.Next = (Linq.AQueryClause)$2;
5419 current_block.SetEndLocation (lexer.Location);
5420 current_block = current_block.Parent;
5423 // Bubble up COMPLETE_COMPLETION productions
5424 | first_from_clause COMPLETE_COMPLETION {
5425 lexer.query_parsing = false;
5428 current_block.SetEndLocation (lexer.Location);
5429 current_block = current_block.Parent;
5431 | nested_from_clause COMPLETE_COMPLETION {
5433 current_block.SetEndLocation (lexer.Location);
5434 current_block = current_block.Parent;
5439 : FROM_FIRST IDENTIFIER IN expression
5441 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5443 var lt = (Tokenizer.LocatedToken) $2;
5444 $$ = new Linq.QueryExpression (new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$4, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1)));
5446 | FROM_FIRST type IDENTIFIER IN expression
5448 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5450 var lt = (Tokenizer.LocatedToken) $3;
5451 $$ = new Linq.QueryExpression (
5452 new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$5, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1)) {
5453 IdentifierType = (FullNamedExpression)$2
5460 : FROM IDENTIFIER IN expression
5462 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5464 var lt = (Tokenizer.LocatedToken) $2;
5465 $$ = new Linq.QueryExpression (new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$4, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1)));
5467 | FROM type IDENTIFIER IN expression
5469 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5471 var lt = (Tokenizer.LocatedToken) $3;
5472 $$ = new Linq.QueryExpression (
5473 new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$5, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1)) {
5474 IdentifierType = (FullNamedExpression)$2
5481 : FROM IDENTIFIER IN
5483 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5487 var lt = (Tokenizer.LocatedToken) $2;
5488 var sn = new SimpleMemberName (lt.Value, lt.Location);
5489 $$ = new Linq.SelectMany ((Linq.QueryBlock)current_block, sn, (Expression)$5, GetLocation ($1));
5491 current_block.SetEndLocation (lexer.Location);
5492 current_block = current_block.Parent;
5494 ((Linq.QueryBlock)current_block).AddRangeVariable (sn);
5496 | FROM type IDENTIFIER IN
5498 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5502 var lt = (Tokenizer.LocatedToken) $3;
5503 var sn = new SimpleMemberName (lt.Value, lt.Location);
5505 $$ = new Linq.SelectMany ((Linq.QueryBlock)current_block, sn, (Expression)$6, GetLocation ($1)) {
5506 IdentifierType = (FullNamedExpression)$2
5509 current_block.SetEndLocation (lexer.Location);
5510 current_block = current_block.Parent;
5512 ((Linq.QueryBlock)current_block).AddRangeVariable (sn);
5517 : opt_query_body_clauses select_or_group_clause opt_query_continuation
5519 Linq.AQueryClause head = (Linq.AQueryClause)$2;
5522 head.Next = (Linq.AQueryClause)$3;
5525 Linq.AQueryClause clause = (Linq.AQueryClause)$1;
5526 clause.Tail.Next = head;
5532 | opt_query_body_clauses COMPLETE_COMPLETION
5535 select_or_group_clause
5538 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5542 $$ = new Linq.Select ((Linq.QueryBlock)current_block, (Expression)$3, GetLocation ($1));
5544 current_block.SetEndLocation (lexer.Location);
5545 current_block = current_block.Parent;
5549 if (linq_clause_blocks == null)
5550 linq_clause_blocks = new Stack<Linq.QueryBlock> ();
5552 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5553 linq_clause_blocks.Push ((Linq.QueryBlock)current_block);
5557 current_block.SetEndLocation (lexer.Location);
5558 current_block = current_block.Parent;
5560 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5564 $$ = new Linq.GroupBy ((Linq.QueryBlock)current_block, (Expression)$3, linq_clause_blocks.Pop (), (Expression)$6, GetLocation ($1));
5566 current_block.SetEndLocation (lexer.Location);
5567 current_block = current_block.Parent;
5571 opt_query_body_clauses
5573 | query_body_clauses
5578 | query_body_clauses query_body_clause
5580 ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$2;
5594 : LET IDENTIFIER ASSIGN
5596 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5600 var lt = (Tokenizer.LocatedToken) $2;
5601 var sn = new SimpleMemberName (lt.Value, lt.Location);
5602 $$ = new Linq.Let ((Linq.QueryBlock) current_block, sn, (Expression)$5, GetLocation ($1));
5604 current_block.SetEndLocation (lexer.Location);
5605 current_block = current_block.Parent;
5607 ((Linq.QueryBlock)current_block).AddRangeVariable (sn);
5614 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5618 $$ = new Linq.Where ((Linq.QueryBlock)current_block, (BooleanExpression)$3, GetLocation ($1));
5620 current_block.SetEndLocation (lexer.Location);
5621 current_block = current_block.Parent;
5626 : JOIN IDENTIFIER IN
5628 if (linq_clause_blocks == null)
5629 linq_clause_blocks = new Stack<Linq.QueryBlock> ();
5631 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5632 linq_clause_blocks.Push ((Linq.QueryBlock) current_block);
5636 current_block.SetEndLocation (lexer.Location);
5637 current_block = current_block.Parent;
5639 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5640 linq_clause_blocks.Push ((Linq.QueryBlock) current_block);
5644 current_block.AddStatement (new ContextualReturn ((Expression) $8));
5645 current_block.SetEndLocation (lexer.Location);
5646 current_block = current_block.Parent;
5648 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5650 expression opt_join_into
5652 var lt = (Tokenizer.LocatedToken) $2;
5653 var sn = new SimpleMemberName (lt.Value, lt.Location);
5654 SimpleMemberName sn2 = null;
5656 var outer_selector = linq_clause_blocks.Pop ();
5657 var block = linq_clause_blocks.Pop ();
5660 $$ = new Linq.Join (block, sn, (Expression)$5, outer_selector, (Linq.QueryBlock) current_block, GetLocation ($1));
5662 var lt2 = (Tokenizer.LocatedToken) $12;
5663 sn2 = new SimpleMemberName (lt2.Value, lt2.Location);
5664 $$ = new Linq.GroupJoin (block, sn, (Expression)$5, outer_selector, (Linq.QueryBlock) current_block,
5665 sn2, GetLocation ($1));
5668 current_block.AddStatement (new ContextualReturn ((Expression) $11));
5669 current_block.SetEndLocation (lexer.Location);
5670 current_block = current_block.Parent;
5672 ((Linq.QueryBlock)current_block).AddRangeVariable (sn2 ?? sn);
5674 | JOIN type IDENTIFIER IN
5676 if (linq_clause_blocks == null)
5677 linq_clause_blocks = new Stack<Linq.QueryBlock> ();
5679 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5680 linq_clause_blocks.Push ((Linq.QueryBlock) current_block);
5684 current_block.SetEndLocation (lexer.Location);
5685 current_block = current_block.Parent;
5687 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5688 linq_clause_blocks.Push ((Linq.QueryBlock) current_block);
5692 current_block.AddStatement (new ContextualReturn ((Expression) $9));
5693 current_block.SetEndLocation (lexer.Location);
5694 current_block = current_block.Parent;
5696 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5698 expression opt_join_into
5700 var lt = (Tokenizer.LocatedToken) $3;
5701 var sn = new SimpleMemberName (lt.Value, lt.Location);
5702 SimpleMemberName sn2 = null;
5703 var outer_selector = linq_clause_blocks.Pop ();
5704 var block = linq_clause_blocks.Pop ();
5707 $$ = new Linq.Join (block, sn, (Expression)$6, outer_selector, (Linq.QueryBlock) current_block, GetLocation ($1)) {
5708 IdentifierType = (FullNamedExpression)$2
5711 var lt2 = (Tokenizer.LocatedToken) $13;
5712 sn2 = new SimpleMemberName (lt2.Value, lt2.Location);
5713 $$ = new Linq.GroupJoin (block, sn, (Expression)$6, outer_selector, (Linq.QueryBlock) current_block, sn2, GetLocation ($1)) {
5714 IdentifierType = (FullNamedExpression)$2
5718 current_block.AddStatement (new ContextualReturn ((Expression) $12));
5719 current_block.SetEndLocation (lexer.Location);
5720 current_block = current_block.Parent;
5722 ((Linq.QueryBlock)current_block).AddRangeVariable (sn2 ?? sn);
5737 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5741 current_block.SetEndLocation (lexer.Location);
5742 current_block = current_block.Parent;
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).Next = (Linq.AQueryClause)$4;
5766 | orderings_then_by COMMA
5768 current_block.SetEndLocation (lexer.Location);
5769 current_block = current_block.Parent;
5771 current_block = new Linq.QueryBlock (compiler, (Linq.QueryBlock) current_block, lexer.Location);
5775 ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$4;
5783 $$ = new Linq.OrderByAscending ((Linq.QueryBlock) current_block, (Expression)$1);
5785 | expression ASCENDING
5787 $$ = new Linq.OrderByAscending ((Linq.QueryBlock) current_block, (Expression)$1);
5789 | expression DESCENDING
5791 $$ = new Linq.OrderByDescending ((Linq.QueryBlock) current_block, (Expression)$1);
5798 $$ = new Linq.ThenByAscending ((Linq.QueryBlock) current_block, (Expression)$1);
5800 | expression ASCENDING
5802 $$ = new Linq.ThenByAscending ((Linq.QueryBlock) current_block, (Expression)$1);
5804 | expression DESCENDING
5806 $$ = new Linq.ThenByDescending ((Linq.QueryBlock) current_block, (Expression)$1);
5811 opt_query_continuation
5815 // query continuation block is not linked with query block but with block
5816 // before. This means each query can use same range variable names for
5817 // different identifiers.
5819 current_block.SetEndLocation (GetLocation ($1));
5820 current_block = current_block.Parent;
5822 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5824 if (linq_clause_blocks == null)
5825 linq_clause_blocks = new Stack<Linq.QueryBlock> ();
5827 linq_clause_blocks.Push ((Linq.QueryBlock) current_block);
5831 var current_block = linq_clause_blocks.Pop ();
5832 var lt = (Tokenizer.LocatedToken) $2;
5833 $$ = new Linq.QueryStartClause ((Linq.QueryBlock)current_block, null, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1)) {
5834 next = (Linq.AQueryClause)$4
5840 // Support for using the compiler as an interactive parser
5842 // The INTERACTIVE_PARSER token is first sent to parse our
5843 // productions; If the result is a Statement, the parsing
5844 // is repeated, this time with INTERACTIVE_PARSE_WITH_BLOCK
5845 // to setup the blocks in advance.
5847 // This setup is here so that in the future we can add
5848 // support for other constructs (type parsing, namespaces, etc)
5849 // that do not require a block to be setup in advance
5853 : EVAL_STATEMENT_PARSER EOF
5854 | EVAL_USING_DECLARATIONS_UNIT_PARSER using_directives
5855 | EVAL_STATEMENT_PARSER {
5856 Evaluator.LoadAliases (current_namespace);
5858 push_current_class (new Class (current_namespace, current_class, new MemberName ("Class" + class_count++),
5859 Modifiers.PUBLIC, null), null);
5861 var baseclass_list = new List<FullNamedExpression> ();
5862 baseclass_list.Add (new TypeExpression (Evaluator.InteractiveBaseClass, lexer.Location));
5863 current_container.AddBasesForPart (current_class, baseclass_list);
5865 // (ref object retval)
5866 Parameter [] mpar = new Parameter [1];
5867 mpar [0] = new Parameter (TypeManager.system_object_expr, "$retval", Parameter.Modifier.REF, null, Location.Null);
5869 ParametersCompiled pars = new ParametersCompiled (compiler, mpar);
5870 current_local_parameters = pars;
5871 Method method = new Method (
5874 TypeManager.system_void_expr,
5875 Modifiers.PUBLIC | Modifiers.STATIC,
5876 new MemberName ("Host"),
5878 null /* attributes */);
5880 oob_stack.Push (method);
5881 ++lexer.parsing_block;
5882 start_block (lexer.Location);
5884 interactive_statement_list opt_COMPLETE_COMPLETION
5886 --lexer.parsing_block;
5887 Method method = (Method) oob_stack.Pop ();
5889 method.Block = (ToplevelBlock) end_block(lexer.Location);
5890 current_container.AddMethod (method);
5892 --lexer.parsing_declaration;
5893 InteractiveResult = pop_current_class ();
5894 current_local_parameters = null;
5896 | EVAL_COMPILATION_UNIT_PARSER {
5897 Evaluator.LoadAliases (current_namespace);
5899 interactive_compilation_unit
5902 interactive_compilation_unit
5903 : outer_declarations
5904 | outer_declarations global_attributes
5909 opt_COMPLETE_COMPLETION
5911 | COMPLETE_COMPLETION
5914 close_brace_or_complete_completion
5916 | COMPLETE_COMPLETION
5921 // A class used to pass around variable declarations and constants
5923 class VariableDeclaration {
5924 public string identifier;
5925 Expression initializer;
5926 public Location Location;
5927 public Attributes OptAttributes;
5928 public string DocComment;
5930 public VariableDeclaration (Tokenizer.LocatedToken lt, Expression initializer, Attributes opt_attrs)
5932 this.identifier = lt.Value;
5933 this.initializer = initializer;
5934 this.Location = lt.Location;
5935 this.OptAttributes = opt_attrs;
5938 public VariableDeclaration (Tokenizer.LocatedToken lt, Expression initializer)
5939 : this (lt, initializer, null)
5943 public Expression GetInitializer (FullNamedExpression type)
5945 if (initializer is ArrayInitializer)
5946 return new ArrayCreation (type, "", (ArrayInitializer)initializer, Location);
5951 public bool HasInitializer {
5952 get { return initializer != null; }
5956 class VariableMemberDeclaration
5958 public readonly MemberName MemberName;
5959 Expression initializer;
5961 public VariableMemberDeclaration (MemberName mn, Expression initializer)
5964 this.initializer = initializer;
5967 public Expression GetInitializer (FullNamedExpression type)
5969 if (initializer is ArrayInitializer)
5970 return new ArrayCreation (type, "", (ArrayInitializer)initializer, MemberName.Location);
5978 // A class used to hold info about an operator declarator
5980 struct OperatorDeclaration {
5981 public readonly Operator.OpType optype;
5982 public readonly FullNamedExpression ret_type;
5983 public readonly Location location;
5985 public OperatorDeclaration (Operator.OpType op, FullNamedExpression ret_type, Location location)
5988 this.ret_type = ret_type;
5989 this.location = location;
5993 void Error_ExpectingTypeName (Expression expr)
5995 if (expr is Invocation){
5996 Report.Error (1002, expr.Location, "Expecting `;'");
5998 Expression.Error_InvalidExpressionStatement (Report, expr.Location);
6002 void Error_ParameterModifierNotValid (string modifier, Location loc)
6004 Report.Error (631, loc, "The parameter modifier `{0}' is not valid in this context",
6008 void Error_DuplicateParameterModifier (Location loc, Parameter.Modifier mod)
6010 Report.Error (1107, loc, "Duplicate parameter modifier `{0}'",
6011 Parameter.GetModifierSignature (mod));
6014 void Error_TypeExpected (Location loc)
6016 Report.Error (1031, loc, "Type expected");
6019 void Error_NamedArgumentExpected (NamedArgument a)
6021 Report.Error (1738, a.Location, "Named arguments must appear after the positional arguments");
6024 void push_current_class (TypeContainer tc, object partial_token)
6026 if (RootContext.EvalMode){
6027 tc.ModFlags = (tc.ModFlags & ~(Modifiers.PRIVATE|Modifiers.INTERNAL)) | Modifiers.PUBLIC;
6028 undo.AddTypeContainer (current_container, tc);
6031 if (partial_token != null)
6032 current_container = current_container.AddPartial (tc);
6034 current_container = current_container.AddTypeContainer (tc);
6036 ++lexer.parsing_declaration;
6040 DeclSpace pop_current_class ()
6042 DeclSpace retval = current_class;
6044 current_class = current_class.Parent;
6045 current_container = current_class.PartialContainer;
6051 // Given the @class_name name, it creates a fully qualified name
6052 // based on the containing declaration space
6055 MakeName (MemberName class_name)
6057 Namespace ns = current_namespace.NS;
6059 if (current_container == RootContext.ToplevelTypes) {
6060 if (ns.Name.Length != 0)
6061 return new MemberName (ns.MemberName, class_name);
6065 return new MemberName (current_container.MemberName, class_name);
6069 Block declare_local_variables (FullNamedExpression type, List<object> variable_declarators, Location loc)
6071 Block implicit_block;
6074 // If we are doing interactive editing, we want variable declarations
6075 // that are in the top block to be added instead to the class as
6078 if (RootContext.StatementMode){
6081 for (Block b = current_block; b != null; b = b.Parent){
6082 if (b is ExplicitBlock && !(b is ToplevelBlock)){
6083 // There has been an explicit block, we cant add to the class
6090 // We can use "current_block" since we know there are no explicit blocks
6092 foreach (VariableDeclaration decl in variable_declarators){
6093 // We can not use the super-handy f.Initializer, because
6094 // multiple lines would force code to be executed out of sync
6095 var init = decl.GetInitializer (type);
6097 string id = "$" + decl.identifier;
6098 LocalInfo vi = current_block.AddVariable (type, id, decl.Location);
6100 // Avoid warning about this variable not being used.
6103 LocalVariableReference var;
6104 var = new LocalVariableReferenceWithClassSideEffect (current_container, decl.identifier, current_block, id, vi, decl.Location);
6105 Assign assign = new SimpleAssign (var, init, decl.Location);
6106 current_block.AddStatement (new StatementExpression (assign));
6107 assign = new SimpleAssign (new SimpleName (decl.identifier, decl.Location), var);
6108 current_block.AddStatement (new StatementExpression (assign));
6110 Field f = new Field (current_container, (FullNamedExpression) type, Modifiers.PUBLIC | Modifiers.STATIC,
6111 new MemberName (decl.identifier, loc), null);
6112 current_container.AddField (f);
6114 // Register the field to be visible later as a global variable
6115 Evaluator.QueueField (f);
6119 return current_block;
6124 // We use the `Used' property to check whether statements
6125 // have been added to the current block. If so, we need
6126 // to create another block to contain the new declaration
6127 // otherwise, as an optimization, we use the same block to
6128 // add the declaration.
6130 // FIXME: A further optimization is to check if the statements
6131 // that were added were added as part of the initialization
6132 // below. In which case, no other statements have been executed
6133 // and we might be able to reduce the number of blocks for
6134 // situations like this:
6136 // int j = 1; int k = j + 1;
6138 if (current_block.Used)
6139 implicit_block = new Block (current_block, loc, lexer.Location);
6141 implicit_block = current_block;
6143 foreach (VariableDeclaration decl in variable_declarators){
6145 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
6146 if (decl.HasInitializer){
6149 var lvr = new LocalVariableReference (implicit_block, decl.identifier, loc);
6151 assign = new SimpleAssign (lvr, decl.GetInitializer (type), decl.Location);
6153 implicit_block.AddStatement (new StatementExpression (assign));
6158 return implicit_block;
6161 Block declare_local_constants (FullNamedExpression type, List<object> declarators)
6163 Block implicit_block;
6165 if (current_block.Used)
6166 implicit_block = new Block (current_block);
6168 implicit_block = current_block;
6170 foreach (VariableDeclaration decl in declarators){
6171 implicit_block.AddConstant (type, decl.identifier, decl.GetInitializer (type), decl.Location);
6174 return implicit_block;
6177 string CheckAttributeTarget (string a, Location l)
6180 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
6184 Report.Warning (658, 1, l,
6185 "`{0}' is invalid attribute target. All attributes in this attribute section will be ignored", a);
6186 return string.Empty;
6189 static bool IsUnaryOperator (Operator.OpType op)
6193 case Operator.OpType.LogicalNot:
6194 case Operator.OpType.OnesComplement:
6195 case Operator.OpType.Increment:
6196 case Operator.OpType.Decrement:
6197 case Operator.OpType.True:
6198 case Operator.OpType.False:
6199 case Operator.OpType.UnaryPlus:
6200 case Operator.OpType.UnaryNegation:
6206 void syntax_error (Location l, string msg)
6208 Report.Error (1003, l, "Syntax error, " + msg);
6213 public Tokenizer Lexer {
6219 static CSharpParser ()
6221 oob_stack = new Stack<object> ();
6224 public CSharpParser (SeekableStreamReader reader, CompilationUnit file, CompilerContext ctx)
6226 if (RootContext.EvalMode)
6230 this.compiler = ctx;
6231 current_namespace = new NamespaceEntry (null, file, null);
6232 current_class = current_namespace.SlaveDeclSpace;
6233 current_container = current_class.PartialContainer; // == RootContest.ToplevelTypes
6235 lexer = new Tokenizer (reader, file, ctx);
6237 use_global_stacks = true;
6240 public void parse ()
6242 eof_token = Token.EOF;
6243 Tokenizer.LocatedToken.Initialize ();
6246 if (yacc_verbose_flag > 1)
6247 yyparse (lexer, new yydebug.yyDebugSimple ());
6251 Tokenizer tokenizer = lexer as Tokenizer;
6252 tokenizer.cleanup ();
6253 } catch (Exception e){
6254 if (e is yyParser.yyUnexpectedEof)
6255 UnexpectedEOF = true;
6257 if (e is yyParser.yyException)
6258 Report.Error (-25, lexer.Location, "Parsing error");
6259 else if (yacc_verbose_flag > 0)
6260 throw; // Used by compiler-tester to test internal errors
6262 Report.Error (589, lexer.Location, "Internal compiler error during parsing");
6265 if (RootContext.ToplevelTypes.NamespaceEntry != null)
6266 throw new InternalErrorException ("who set it?");
6269 void CheckToken (int error, int yyToken, string msg, Location loc)
6271 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
6272 Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, GetTokenName (yyToken));
6274 Report.Error (error, loc, msg);
6277 void CheckIdentifierToken (int yyToken, Location loc)
6279 CheckToken (1041, yyToken, "Identifier expected", loc);
6282 string ConsumeStoredComment ()
6284 string s = tmpComment;
6286 Lexer.doc_state = XmlCommentState.Allowed;
6290 Location GetLocation (object obj)
6292 if (obj is Tokenizer.LocatedToken)
6293 return ((Tokenizer.LocatedToken) obj).Location;
6294 if (obj is MemberName)
6295 return ((MemberName) obj).Location;
6297 // if (obj is Expression)
6298 // return ((Expression) obj).Location;
6300 return lexer.Location;
6304 get { return compiler.Report; }
6307 void start_block (Location loc)
6309 if (current_block == null || parsing_anonymous_method) {
6310 current_block = new ToplevelBlock (compiler, current_block, current_local_parameters, loc);
6311 parsing_anonymous_method = false;
6313 current_block = new ExplicitBlock (current_block, loc, Location.Null);
6318 end_block (Location loc)
6320 Block retval = current_block.Explicit;
6321 retval.SetEndLocation (loc);
6322 current_block = retval.Parent;
6327 start_anonymous (bool lambda, ParametersCompiled parameters, Location loc)
6329 if (RootContext.Version == LanguageVersion.ISO_1){
6330 Report.FeatureIsNotAvailable (loc, "anonymous methods");
6333 oob_stack.Push (current_anonymous_method);
6334 oob_stack.Push (current_local_parameters);
6336 current_local_parameters = parameters;
6338 current_anonymous_method = lambda
6339 ? new LambdaExpression (loc)
6340 : new AnonymousMethodExpression (loc);
6342 // Force the next block to be created as a ToplevelBlock
6343 parsing_anonymous_method = true;
6347 * Completes the anonymous method processing, if lambda_expr is null, this
6348 * means that we have a Statement instead of an Expression embedded
6350 AnonymousMethodExpression end_anonymous (ToplevelBlock anon_block)
6352 AnonymousMethodExpression retval;
6354 current_anonymous_method.Block = anon_block;
6355 retval = current_anonymous_method;
6357 current_local_parameters = (ParametersCompiled) oob_stack.Pop ();
6358 current_anonymous_method = (AnonymousMethodExpression) oob_stack.Pop ();
6363 public NamespaceEntry CurrentNamespace {
6365 return current_namespace;
6370 void Error_SyntaxError (int token)
6372 Error_SyntaxError (0, token, "Unexpected symbol");
6375 void Error_SyntaxError (int error_code, int token, string msg)
6377 string symbol = GetSymbolName (token);
6378 string expecting = GetExpecting ();
6380 if (error_code == 0) {
6381 if (expecting == "`)'")
6387 if (expecting != null)
6388 Report.Error (error_code, lexer.Location, "{2} `{0}', expecting {1}",
6389 symbol, expecting, msg);
6391 Report.Error (error_code, lexer.Location, "{1} `{0}'", symbol, msg);
6394 string GetExpecting ()
6396 int [] tokens = yyExpectingTokens (yyExpectingState);
6397 var names = new List<string> (tokens.Length);
6398 bool has_type = false;
6399 bool has_identifier = false;
6400 for (int i = 0; i < tokens.Length; i++){
6401 int token = tokens [i];
6402 has_identifier |= token == Token.IDENTIFIER;
6404 string name = GetTokenName (token);
6405 if (name == "<internal>")
6408 has_type |= name == "type";
6409 if (names.Contains (name))
6416 // Too many tokens to enumerate
6418 if (names.Count > 8)
6421 if (has_type && has_identifier)
6422 names.Remove ("identifier");
6424 if (names.Count == 1)
6425 return "`" + GetTokenName (tokens [0]) + "'";
6427 StringBuilder sb = new StringBuilder ();
6429 int count = names.Count;
6430 for (int i = 0; i < count; i++){
6431 bool last = i + 1 == count;
6435 sb.Append (names [i]);
6436 sb.Append (last ? "'" : count < 3 ? "' " : "', ");
6438 return sb.ToString ();
6442 string GetSymbolName (int token)
6446 return ((Constant)lexer.Value).GetValue ().ToString ();
6447 case Token.IDENTIFIER:
6448 return ((Tokenizer.LocatedToken)lexer.Value).Value;
6490 case Token.BITWISE_AND:
6492 case Token.BITWISE_OR:
6506 case Token.OP_SHIFT_LEFT:
6508 case Token.OP_SHIFT_RIGHT:
6528 case Token.OP_COALESCING:
6530 case Token.OP_MULT_ASSIGN:
6532 case Token.OP_DIV_ASSIGN:
6534 case Token.OP_MOD_ASSIGN:
6536 case Token.OP_ADD_ASSIGN:
6538 case Token.OP_SUB_ASSIGN:
6540 case Token.OP_SHIFT_LEFT_ASSIGN:
6542 case Token.OP_SHIFT_RIGHT_ASSIGN:
6544 case Token.OP_AND_ASSIGN:
6546 case Token.OP_XOR_ASSIGN:
6548 case Token.OP_OR_ASSIGN:
6552 return GetTokenName (token);
6555 static string GetTokenName (int token)
6558 case Token.ABSTRACT:
6578 case Token.CONTINUE:
6582 case Token.DELEGATE:
6592 case Token.EXPLICIT:
6610 case Token.IMPLICIT:
6614 case Token.INTERFACE:
6616 case Token.INTERNAL:
6622 case Token.NAMESPACE:
6628 case Token.OPERATOR:
6632 case Token.OVERRIDE:
6638 case Token.PROTECTED:
6642 case Token.READONLY:
6654 case Token.STACKALLOC:
6655 return "stackalloc";
6672 case Token.UNCHECKED:
6680 case Token.VOLATILE:
6693 case Token.FROM_FIRST:
6711 case Token.ASCENDING:
6713 case Token.DESCENDING:
6714 return "descending";
6721 case Token.OPEN_BRACE:
6723 case Token.CLOSE_BRACE:
6725 case Token.OPEN_BRACKET:
6727 case Token.CLOSE_BRACKET:
6729 case Token.OPEN_PARENS_CAST:
6730 case Token.OPEN_PARENS_LAMBDA:
6731 case Token.OPEN_PARENS:
6733 case Token.CLOSE_PARENS:
6739 case Token.DEFAULT_COLON:
6743 case Token.SEMICOLON:
6754 case Token.BITWISE_AND:
6755 case Token.BITWISE_OR:
6762 case Token.OP_SHIFT_LEFT:
6763 case Token.OP_SHIFT_RIGHT:
6771 case Token.OP_COALESCING:
6772 case Token.OP_MULT_ASSIGN:
6773 case Token.OP_DIV_ASSIGN:
6774 case Token.OP_MOD_ASSIGN:
6775 case Token.OP_ADD_ASSIGN:
6776 case Token.OP_SUB_ASSIGN:
6777 case Token.OP_SHIFT_LEFT_ASSIGN:
6778 case Token.OP_SHIFT_RIGHT_ASSIGN:
6779 case Token.OP_AND_ASSIGN:
6780 case Token.OP_XOR_ASSIGN:
6781 case Token.OP_OR_ASSIGN:
6782 return "<operator>";
6804 case Token.OP_GENERICS_LT:
6805 case Token.GENERIC_DIMENSION:
6807 case Token.OP_GENERICS_GT:
6810 case Token.INTERR_NULLABLE:
6812 case Token.DOUBLE_COLON:
6816 case Token.IDENTIFIER:
6817 return "identifier";
6819 // All of these are internal.
6822 case Token.FIRST_KEYWORD:
6824 case Token.EVAL_COMPILATION_UNIT_PARSER:
6825 case Token.EVAL_USING_DECLARATIONS_UNIT_PARSER:
6826 case Token.EVAL_STATEMENT_PARSER:
6827 case Token.LAST_KEYWORD:
6828 case Token.GENERATE_COMPLETION:
6829 case Token.COMPLETE_COMPLETION:
6830 return "<internal>";
6832 // A bit more robust.
6834 return yyNames [token];