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 // Licensed under the terms of the GNU GPL
11 // (C) 2001 Ximian, Inc (http://www.ximian.com)
12 // (C) 2004 Novell, Inc
15 // (1) Figure out why error productions dont work. `type-declaration' is a
16 // great spot to put an `error' because you can reproduce it with this input:
19 // Possible optimization:
20 // Run memory profiler with parsing only, and consider dropping
21 // arraylists where not needed. Some pieces can use linked lists.
30 using System.Collections;
35 public class CSharpParser {
36 NamespaceEntry current_namespace;
37 TypeContainer current_container;
38 DeclSpace current_class;
41 /// Current block is used to add statements as we find
46 Delegate current_delegate;
48 GenericMethod current_generic_method;
49 AnonymousMethodExpression current_anonymous_method;
52 /// This is used by the unary_expression code to resolve
53 /// a name against a parameter.
56 // FIXME: This is very ugly and it's very hard to reset it correctly
57 // on all places, especially when some parameters are autogenerated.
58 Parameters current_local_parameters;
61 /// Using during property parsing to describe the implicit
62 /// value parameter that is passed to the "set" and "get"accesor
63 /// methods (properties and indexers).
65 FullNamedExpression implicit_value_parameter_type;
66 Parameters indexer_parameters;
69 /// Hack to help create non-typed array initializer
71 public static FullNamedExpression current_array_type;
72 FullNamedExpression pushed_current_array_type;
75 /// Used to determine if we are parsing the get/set pair
76 /// of an indexer or a property
80 bool parsing_anonymous_method;
83 /// An out-of-band stack.
85 static Stack oob_stack;
93 /// Controls the verbosity of the errors produced by the parser
95 static public int yacc_verbose_flag;
98 /// Used by the interactive shell, flags whether EOF was reached
99 /// and an error was produced
101 public bool UnexpectedEOF;
104 /// The current file.
106 CompilationUnit file;
109 /// Temporary Xml documentation cache.
110 /// For enum types, we need one more temporary store.
113 string enumTypeComment;
115 /// Current attribute target
116 string current_attr_target;
118 /// assembly and module attribute definitions are enabled
119 bool global_attrs_enabled = true;
120 bool has_get, has_set;
121 bool parameter_modifiers_not_allowed;
122 bool params_modifiers_not_allowed;
123 bool arglist_allowed;
126 /// When using the interactive parser, this holds the
127 /// resulting expression
128 public object InteractiveResult;
131 // Keeps track of global data changes to undo on parser error
135 // A counter to create new class names in interactive mode
136 static int class_count;
140 %token NONE /* This token is never returned by our lexer */
141 %token ERROR // This is used not by the parser, but by the tokenizer.
145 *These are the C# keywords
232 %token QUERY_FIRST_TOKEN
245 %token QUERY_LAST_TOKEN
246 %token INTERR_NULLABLE
248 /* C# keywords which are not really keywords */
254 /* C# single character operators/punctuation. */
272 %token OP_GENERICS_LT
274 %token OP_GENERICS_GT
283 /* C# multi-character operators. */
288 %token OP_SHIFT_RIGHT
295 %token OP_MULT_ASSIGN
300 %token OP_SHIFT_LEFT_ASSIGN
301 %token OP_SHIFT_RIGHT_ASSIGN
309 %token LITERAL_INTEGER
311 %token LITERAL_DOUBLE
312 %token LITERAL_DECIMAL
313 %token LITERAL_CHARACTER
314 %token LITERAL_STRING
317 %token OPEN_PARENS_LAMBDA
318 %token CLOSE_PARENS_CAST
319 %token CLOSE_PARENS_NO_CAST
320 %token CLOSE_PARENS_OPEN_PARENS
321 %token CLOSE_PARENS_MINUS
322 %token DEFAULT_OPEN_PARENS
323 %token GENERIC_DIMENSION
326 // Make the parser go into eval mode parsing (statements and compilation units).
327 %token EVAL_STATEMENT_PARSER
328 %token EVAL_COMPILATION_UNIT_PARSER
329 %token EVAL_USING_DECLARATIONS_UNIT_PARSER
331 /* Add precedence rules to solve dangling else s/r conflict */
340 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
342 %left STAR DIV PERCENT
343 %right BANG CARRET UMINUS
344 %nonassoc OP_INC OP_DEC
346 %left OPEN_BRACKET OPEN_BRACE
350 %start compilation_unit
354 : outer_declarations opt_EOF
355 | outer_declarations global_attributes opt_EOF
356 | global_attributes opt_EOF
357 | opt_EOF /* allow empty files */
358 | interactive_parsing opt_EOF
364 Lexer.check_incorrect_doc_comment ();
368 Lexer.check_incorrect_doc_comment ();
374 | outer_declarations outer_declaration
378 : extern_alias_directive
380 | namespace_member_declaration
383 extern_alias_directives
384 : extern_alias_directive
385 | extern_alias_directives extern_alias_directive;
387 extern_alias_directive
388 : EXTERN IDENTIFIER IDENTIFIER SEMICOLON
390 LocatedToken lt = (LocatedToken) $2;
393 Report.Error (1003, lt.Location, "'alias' expected");
394 } else if (RootContext.Version == LanguageVersion.ISO_1) {
395 Report.FeatureIsNotAvailable (lt.Location, "external alias");
397 lt = (LocatedToken) $3;
398 current_namespace.AddUsingExternalAlias (lt.Value, lt.Location);
405 | using_directives using_directive
409 : using_alias_directive
411 if (RootContext.Documentation != null)
412 Lexer.doc_state = XmlCommentState.Allowed;
414 | using_namespace_directive
416 if (RootContext.Documentation != null)
417 Lexer.doc_state = XmlCommentState.Allowed;
421 using_alias_directive
422 : USING IDENTIFIER ASSIGN
423 namespace_or_type_name SEMICOLON
425 LocatedToken lt = (LocatedToken) $2;
426 current_namespace.AddUsingAlias (lt.Value, (MemberName) $4, (Location) $1);
429 CheckIdentifierToken (yyToken, GetLocation ($2));
434 using_namespace_directive
435 : USING namespace_name SEMICOLON
437 current_namespace.AddUsing ((MemberName) $2, (Location) $1);
442 // Strictly speaking, namespaces don't have attributes but
443 // we parse global attributes along with namespace declarations and then
446 namespace_declaration
447 : opt_attributes NAMESPACE qualified_identifier
449 MemberName name = (MemberName) $3;
452 Report.Error(1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
455 current_namespace = new NamespaceEntry (
456 current_namespace, file, name.GetName ());
457 current_class = current_namespace.SlaveDeclSpace;
458 current_container = current_class.PartialContainer;
460 namespace_body opt_semicolon
462 current_namespace = current_namespace.Parent;
463 current_class = current_namespace.SlaveDeclSpace;
464 current_container = current_class.PartialContainer;
471 LocatedToken lt = (LocatedToken) $1;
472 $$ = new MemberName (lt.Value, lt.Location);
474 | qualified_identifier DOT IDENTIFIER
476 LocatedToken lt = (LocatedToken) $3;
477 $$ = new MemberName ((MemberName) $1, lt.Value, lt.Location);
481 syntax_error (lexer.Location, "`.' expected");
496 : namespace_or_type_name {
497 MemberName name = (MemberName) $1;
499 if (name.TypeArguments != null)
500 syntax_error (lexer.Location, "namespace name expected");
509 if (RootContext.Documentation != null)
510 Lexer.doc_state = XmlCommentState.Allowed;
516 : opt_extern_alias_directives
518 opt_namespace_member_declarations
522 Report.Error (1518, lexer.Location, "Expected `class', `delegate', `enum', `interface', or `struct'");
525 | opt_extern_alias_directives
527 opt_namespace_member_declarations
530 Report.Error (1513, lexer.Location, "} expected");
539 opt_extern_alias_directives
541 | extern_alias_directives
544 opt_namespace_member_declarations
546 | namespace_member_declarations
549 namespace_member_declarations
550 : namespace_member_declaration
551 | namespace_member_declarations namespace_member_declaration
554 namespace_member_declaration
558 DeclSpace ds = (DeclSpace)$1;
560 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
561 Report.Error (1527, ds.Location,
562 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
565 current_namespace.DeclarationFound = true;
567 | namespace_declaration {
568 current_namespace.DeclarationFound = true;
571 | field_declaration {
572 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
574 | method_declaration {
575 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
582 | interface_declaration
584 | delegate_declaration
586 // Enable this when we have handled all errors, because this acts as a generic fallback
589 // Console.WriteLine ("Token=" + yyToken);
590 // Report.Error (1518, GetLocation ($1), "Expected class, struct, interface, enum or delegate");
602 Attributes attrs = (Attributes)$1;
603 if (global_attrs_enabled) {
604 CodeGen.Assembly.AddAttributes (attrs.Attrs);
606 foreach (Attribute a in attrs.Attrs) {
607 Report.Error (1730, a.Location, "Assembly and module attributes must precede all other elements except using clauses and extern alias declarations");
618 global_attrs_enabled = false;
623 global_attrs_enabled = false;
632 if (current_attr_target != String.Empty) {
633 ArrayList sect = (ArrayList) $1;
635 if (global_attrs_enabled) {
636 if (current_attr_target == "module") {
637 CodeGen.Module.AddAttributes (sect);
639 } else if (current_attr_target != null && current_attr_target.Length > 0) {
640 CodeGen.Assembly.AddAttributes (sect);
643 $$ = new Attributes (sect);
646 if (RootContext.Documentation != null) {
647 Lexer.check_incorrect_doc_comment ();
649 XmlCommentState.Allowed;
653 $$ = new Attributes (sect);
658 current_attr_target = null;
660 | attribute_sections attribute_section
662 if (current_attr_target != String.Empty) {
663 Attributes attrs = $1 as Attributes;
664 ArrayList sect = (ArrayList) $2;
666 if (global_attrs_enabled) {
667 if (current_attr_target == "module") {
668 CodeGen.Module.AddAttributes (sect);
670 } else if (current_attr_target == "assembly") {
671 CodeGen.Assembly.AddAttributes (sect);
675 attrs = new Attributes (sect);
677 attrs.AddAttributes (sect);
681 attrs = new Attributes (sect);
683 attrs.AddAttributes (sect);
689 current_attr_target = null;
694 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
698 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
704 attribute_target_specifier
705 : attribute_target COLON
707 current_attr_target = (string)$1;
715 LocatedToken lt = (LocatedToken) $1;
716 $$ = CheckAttributeTarget (lt.Value, lt.Location);
718 | EVENT { $$ = "event"; }
719 | RETURN { $$ = "return"; }
722 string name = GetTokenName (yyToken);
723 $$ = CheckAttributeTarget (name, GetLocation ($1));
730 ArrayList attrs = new ArrayList (4);
736 | attribute_list COMMA attribute
738 ArrayList attrs = (ArrayList) $1;
748 ++lexer.parsing_block;
750 opt_attribute_arguments
752 --lexer.parsing_block;
753 MemberName mname = (MemberName) $1;
754 if (mname.IsGeneric) {
755 Report.Error (404, lexer.Location,
756 "'<' unexpected: attributes cannot be generic");
759 object [] arguments = (object []) $3;
760 MemberName left = mname.Left;
761 string identifier = mname.Name;
763 Expression left_expr = left == null ? null : left.GetTypeExpression ();
765 if (current_attr_target == String.Empty)
767 else if (global_attrs_enabled && (current_attr_target == "assembly" || current_attr_target == "module"))
768 // FIXME: supply "nameEscaped" parameter here.
769 $$ = new GlobalAttribute (current_namespace, current_attr_target,
770 left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
772 $$ = new Attribute (current_attr_target, left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
777 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
780 opt_attribute_arguments
781 : /* empty */ { $$ = null; }
782 | OPEN_PARENS attribute_arguments CLOSE_PARENS
790 : opt_positional_argument_list
795 $$ = new object [] { $1, null };
798 | positional_argument_list COMMA named_argument_list
800 $$ = new object[] { $1, $3 };
802 | named_argument_list
804 $$ = new object [] { null, $1 };
809 opt_positional_argument_list
810 : /* empty */ { $$ = null; }
811 | positional_argument_list
814 positional_argument_list
817 ArrayList args = new ArrayList (4);
818 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
822 | positional_argument_list COMMA expression
824 ArrayList args = (ArrayList) $1;
825 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
834 ArrayList args = new ArrayList (4);
839 | named_argument_list COMMA named_argument
841 ArrayList args = (ArrayList) $1;
846 | named_argument_list COMMA expression
848 Report.Error (1016, ((Expression) $3).Location, "Named attribute argument expected");
854 : IDENTIFIER ASSIGN expression
856 // FIXME: keep location
857 $$ = new DictionaryEntry (
858 ((LocatedToken) $1).Value,
859 new Argument ((Expression) $3, Argument.AType.Expression));
865 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
868 opt_class_member_declarations
870 | class_member_declarations
873 class_member_declarations
874 : class_member_declaration
875 | class_member_declarations
876 class_member_declaration
879 class_member_declaration
880 : constant_declaration // done
881 | field_declaration // done
882 | method_declaration // done
883 | property_declaration // done
884 | event_declaration // done
885 | indexer_declaration // done
886 | operator_declaration // done
887 | constructor_declaration // done
888 | destructor_declaration // done
898 lexer.ConstraintsParsing = true;
902 MemberName name = MakeName ((MemberName) $6);
903 push_current_class (new Struct (current_namespace, current_class, name, (int) $2, (Attributes) $1), $3);
906 opt_type_parameter_constraints_clauses
908 lexer.ConstraintsParsing = false;
910 current_class.SetParameterInfo ((ArrayList) $9);
912 if (RootContext.Documentation != null)
913 current_container.DocComment = Lexer.consume_doc_comment ();
917 if (RootContext.Documentation != null)
918 Lexer.doc_state = XmlCommentState.Allowed;
922 $$ = pop_current_class ();
924 | opt_attributes opt_modifiers opt_partial STRUCT error {
925 CheckIdentifierToken (yyToken, GetLocation ($5));
932 if (RootContext.Documentation != null)
933 Lexer.doc_state = XmlCommentState.Allowed;
935 opt_struct_member_declarations CLOSE_BRACE
938 opt_struct_member_declarations
940 | struct_member_declarations
943 struct_member_declarations
944 : struct_member_declaration
945 | struct_member_declarations struct_member_declaration
948 struct_member_declaration
949 : constant_declaration
952 | property_declaration
954 | indexer_declaration
955 | operator_declaration
956 | constructor_declaration
960 * This is only included so we can flag error 575:
961 * destructors only allowed on class types
963 | destructor_declaration
974 int modflags = (int) $2;
975 foreach (VariableDeclaration constant in (ArrayList) $5){
976 Location l = constant.Location;
977 if ((modflags & Modifiers.STATIC) != 0) {
978 Report.Error (504, l, "The constant `{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
982 Const c = new Const (
983 current_class, (FullNamedExpression) $4, (string) constant.identifier,
984 (Expression) constant.expression_or_array_initializer, modflags,
987 if (RootContext.Documentation != null) {
988 c.DocComment = Lexer.consume_doc_comment ();
989 Lexer.doc_state = XmlCommentState.Allowed;
991 current_container.AddConstant (c);
997 : constant_declarator
999 ArrayList constants = new ArrayList (4);
1004 | constant_declarators COMMA constant_declarator
1007 ArrayList constants = (ArrayList) $1;
1016 ++lexer.parsing_block;
1020 --lexer.parsing_block;
1021 $$ = new VariableDeclaration ((LocatedToken) $1, $4);
1025 // A const field requires a value to be provided
1026 Report.Error (145, ((LocatedToken) $1).Location, "A const field requires a value to be provided");
1035 variable_declarators
1038 FullNamedExpression type = (FullNamedExpression) $3;
1041 current_array_type = null;
1043 foreach (VariableDeclaration var in (ArrayList) $4){
1044 Field field = new Field (current_class, type, mod, var.identifier,
1045 (Attributes) $1, var.Location);
1047 field.Initializer = var.expression_or_array_initializer;
1049 if (RootContext.Documentation != null) {
1050 field.DocComment = Lexer.consume_doc_comment ();
1051 Lexer.doc_state = XmlCommentState.Allowed;
1053 current_container.AddField (field);
1054 $$ = field; // FIXME: might be better if it points to the top item
1061 fixed_variable_declarators
1064 FullNamedExpression type = (FullNamedExpression) $4;
1067 current_array_type = null;
1069 foreach (VariableDeclaration var in (ArrayList) $5) {
1070 FixedField field = new FixedField (current_class, type, mod, var.identifier,
1071 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
1073 if (RootContext.Documentation != null) {
1074 field.DocComment = Lexer.consume_doc_comment ();
1075 Lexer.doc_state = XmlCommentState.Allowed;
1077 current_container.AddField (field);
1078 $$ = field; // FIXME: might be better if it points to the top item
1087 Report.Error (1641, GetLocation ($4), "A fixed size buffer field must have the array size specifier after the field name");
1092 variable_declarators
1094 current_array_type = null;
1095 Report.Error (670, (Location) $3, "Fields cannot have void type");
1099 fixed_variable_declarators
1100 : fixed_variable_declarator
1102 ArrayList decl = new ArrayList (2);
1106 | fixed_variable_declarators COMMA fixed_variable_declarator
1108 ArrayList decls = (ArrayList) $1;
1114 fixed_variable_declarator
1115 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
1117 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1119 | IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1121 Report.Error (443, lexer.Location, "Value or constant expected");
1122 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1126 variable_declarators
1127 : variable_declarator
1129 ArrayList decl = new ArrayList (4);
1134 | variable_declarators COMMA variable_declarator
1136 ArrayList decls = (ArrayList) $1;
1145 ++lexer.parsing_block;
1147 variable_initializer
1149 --lexer.parsing_block;
1150 $$ = new VariableDeclaration ((LocatedToken) $1, $4);
1154 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1156 | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
1158 Report.Error (650, ((LocatedToken) $1).Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
1159 "To declare a fixed size buffer field, use the fixed keyword before the field type");
1164 variable_initializer
1173 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
1175 $$ = new StackAlloc ((Expression) $2, (Expression) $4, (Location) $1);
1179 $$ = new ArglistAccess ((Location) $1);
1183 Report.Error (1575, (Location) $1, "A stackalloc expression requires [] after type");
1190 if (RootContext.Documentation != null)
1191 Lexer.doc_state = XmlCommentState.NotAllowed;
1195 Method method = (Method) $1;
1196 method.Block = (ToplevelBlock) $3;
1197 current_container.AddMethod (method);
1199 if (current_container.Kind == Kind.Interface && method.Block != null) {
1200 Report.Error (531, method.Location, "`{0}': interface members cannot have a definition", method.GetSignatureForError ());
1203 current_generic_method = null;
1204 current_local_parameters = null;
1206 if (RootContext.Documentation != null)
1207 Lexer.doc_state = XmlCommentState.Allowed;
1220 Report.Error (1585, lexer.Location,
1221 "Member modifier `{0}' must precede the member type and name",
1222 Modifiers.Name (i));
1236 arglist_allowed = true;
1238 opt_formal_parameter_list CLOSE_PARENS
1240 lexer.ConstraintsParsing = true;
1242 opt_type_parameter_constraints_clauses
1244 lexer.ConstraintsParsing = false;
1245 arglist_allowed = false;
1246 MemberName name = (MemberName) $4;
1247 current_local_parameters = (Parameters) $7;
1249 if ($10 != null && name.TypeArguments == null)
1250 Report.Error (80, lexer.Location,
1251 "Constraints are not allowed on non-generic declarations");
1255 GenericMethod generic = null;
1256 if (name.TypeArguments != null) {
1257 generic = new GenericMethod (current_namespace, current_class, name,
1258 (FullNamedExpression) $3, current_local_parameters);
1260 generic.SetParameterInfo ((ArrayList) $10);
1263 method = new Method (current_class, generic, (FullNamedExpression) $3, (int) $2,
1264 name, current_local_parameters, (Attributes) $1);
1266 current_generic_method = generic;
1268 if (RootContext.Documentation != null)
1269 method.DocComment = Lexer.consume_doc_comment ();
1278 arglist_allowed = true;
1280 opt_formal_parameter_list CLOSE_PARENS
1282 lexer.ConstraintsParsing = true;
1284 opt_type_parameter_constraints_clauses
1286 lexer.ConstraintsParsing = false;
1287 arglist_allowed = false;
1289 MemberName name = (MemberName) $4;
1290 current_local_parameters = (Parameters) $7;
1292 if ($10 != null && name.TypeArguments == null)
1293 Report.Error (80, lexer.Location,
1294 "Constraints are not allowed on non-generic declarations");
1297 GenericMethod generic = null;
1298 if (name.TypeArguments != null) {
1299 generic = new GenericMethod (current_namespace, current_class, name,
1300 TypeManager.system_void_expr, current_local_parameters);
1302 generic.SetParameterInfo ((ArrayList) $10);
1305 method = new Method (current_class, generic, TypeManager.system_void_expr,
1306 (int) $2, name, current_local_parameters, (Attributes) $1);
1308 current_generic_method = generic;
1310 if (RootContext.Documentation != null)
1311 method.DocComment = Lexer.consume_doc_comment ();
1319 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1321 lexer.ConstraintsParsing = true;
1323 opt_type_parameter_constraints_clauses
1325 lexer.ConstraintsParsing = false;
1327 MemberName name = (MemberName) $5;
1328 current_local_parameters = (Parameters) $7;
1330 if ($9 != null && name.TypeArguments == null)
1331 Report.Error (80, lexer.Location,
1332 "Constraints are not allowed on non-generic declarations");
1335 GenericMethod generic = null;
1336 if (name.TypeArguments != null) {
1337 generic = new GenericMethod (current_namespace, current_class, name,
1338 TypeManager.system_void_expr, current_local_parameters);
1340 generic.SetParameterInfo ((ArrayList) $10);
1343 int modifiers = (int) $2;
1346 const int invalid_partial_mod = Modifiers.Accessibility | Modifiers.ABSTRACT | Modifiers.EXTERN |
1347 Modifiers.NEW | Modifiers.OVERRIDE | Modifiers.SEALED | Modifiers.VIRTUAL;
1349 if ((modifiers & invalid_partial_mod) != 0) {
1350 Report.Error (750, name.Location, "A partial method cannot define access modifier or " +
1351 "any of abstract, extern, new, override, sealed, or virtual modifiers");
1352 modifiers &= ~invalid_partial_mod;
1355 if ((current_class.ModFlags & Modifiers.PARTIAL) == 0) {
1356 Report.Error (751, name.Location, "A partial method must be declared within a " +
1357 "partial class or partial struct");
1360 modifiers |= Modifiers.PARTIAL | Modifiers.PRIVATE;
1362 method = new Method (current_class, generic, TypeManager.system_void_expr,
1363 modifiers, name, current_local_parameters, (Attributes) $1);
1365 current_generic_method = generic;
1367 if (RootContext.Documentation != null)
1368 method.DocComment = Lexer.consume_doc_comment ();
1375 modifiers member_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1377 MemberName name = (MemberName) $5;
1378 Report.Error (1585, name.Location,
1379 "Member modifier `{0}' must precede the member type and name", Modifiers.Name ((int) $4));
1381 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1382 0, name, (Parameters) $7, (Attributes) $1);
1384 current_local_parameters = (Parameters) $7;
1386 if (RootContext.Documentation != null)
1387 method.DocComment = Lexer.consume_doc_comment ();
1395 | SEMICOLON { $$ = null; }
1398 opt_formal_parameter_list
1399 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1400 | formal_parameter_list
1403 opt_parameter_list_no_mod
1404 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1407 parameter_modifiers_not_allowed = true;
1409 formal_parameter_list
1411 parameter_modifiers_not_allowed = false;
1416 formal_parameter_list
1419 ArrayList pars_list = (ArrayList) $1;
1421 Parameter [] pars = new Parameter [pars_list.Count];
1422 pars_list.CopyTo (pars);
1424 $$ = new Parameters (pars);
1426 | fixed_parameters COMMA parameter_array
1428 ArrayList pars_list = (ArrayList) $1;
1431 Parameter [] pars = new Parameter [pars_list.Count];
1432 pars_list.CopyTo (pars);
1434 $$ = new Parameters (pars);
1436 | fixed_parameters COMMA arglist_modifier
1438 ArrayList pars_list = (ArrayList) $1;
1439 pars_list.Add (new ArglistParameter (GetLocation ($3)));
1441 Parameter [] pars = new Parameter [pars_list.Count];
1442 pars_list.CopyTo (pars);
1444 $$ = new Parameters (pars, true);
1446 | parameter_array COMMA error
1449 Report.Error (231, ((Parameter) $1).Location, "A params parameter must be the last parameter in a formal parameter list");
1452 | fixed_parameters COMMA parameter_array COMMA error
1455 Report.Error (231, ((Parameter) $3).Location, "A params parameter must be the last parameter in a formal parameter list");
1458 | arglist_modifier COMMA error
1460 Report.Error (257, (Location) $1, "An __arglist parameter must be the last parameter in a formal parameter list");
1463 | fixed_parameters COMMA ARGLIST COMMA error
1465 Report.Error (257, (Location) $3, "An __arglist parameter must be the last parameter in a formal parameter list");
1470 $$ = new Parameters (new Parameter[] { (Parameter) $1 } );
1474 $$ = new Parameters (new Parameter [] { new ArglistParameter ((Location) $1) }, true);
1481 ArrayList pars = new ArrayList (4);
1486 | fixed_parameters COMMA fixed_parameter
1488 ArrayList pars = (ArrayList) $1;
1489 Parameter p = (Parameter)$3;
1491 if (p.HasExtensionMethodModifier)
1492 Report.Error (1100, p.Location, "The parameter modifier `this' can only be used on the first parameter");
1501 opt_parameter_modifier
1505 LocatedToken lt = (LocatedToken) $4;
1506 $$ = new Parameter ((FullNamedExpression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1509 opt_parameter_modifier
1511 IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1513 LocatedToken lt = (LocatedToken) $4;
1514 Report.Error (1552, lt.Location, "Array type specifier, [], must appear before parameter name");
1518 opt_parameter_modifier
1521 Report.Error (1001, GetLocation ($3), "Identifier expected");
1525 opt_parameter_modifier
1528 CheckIdentifierToken (yyToken, GetLocation ($4));
1532 opt_parameter_modifier
1538 LocatedToken lt = (LocatedToken) $4;
1539 Report.Error (241, lt.Location, "Default parameter specifiers are not permitted");
1544 opt_parameter_modifier
1545 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1546 | parameter_modifiers
1550 : parameter_modifier
1554 | parameter_modifiers parameter_modifier
1556 Parameter.Modifier p2 = (Parameter.Modifier)$2;
1557 Parameter.Modifier mod = (Parameter.Modifier)$1 | p2;
1558 if (((Parameter.Modifier)$1 & p2) == p2) {
1559 Error_DuplicateParameterModifier (lexer.Location, p2);
1561 switch (mod & ~Parameter.Modifier.This) {
1562 case Parameter.Modifier.REF:
1563 Report.Error (1101, lexer.Location, "The parameter modifiers `this' and `ref' cannot be used altogether");
1565 case Parameter.Modifier.OUT:
1566 Report.Error (1102, lexer.Location, "The parameter modifiers `this' and `out' cannot be used altogether");
1569 Report.Error (1108, lexer.Location, "A parameter cannot have specified more than one modifier");
1580 if (parameter_modifiers_not_allowed)
1581 Error_ParameterModifierNotValid ("ref", (Location)$1);
1583 $$ = Parameter.Modifier.REF;
1587 if (parameter_modifiers_not_allowed)
1588 Error_ParameterModifierNotValid ("out", (Location)$1);
1590 $$ = Parameter.Modifier.OUT;
1594 if (parameter_modifiers_not_allowed)
1595 Error_ParameterModifierNotValid ("this", (Location)$1);
1597 if (RootContext.Version <= LanguageVersion.ISO_2)
1598 Report.FeatureIsNotAvailable (GetLocation ($1), "extension methods");
1600 $$ = Parameter.Modifier.This;
1605 : opt_attributes params_modifier type IDENTIFIER
1607 LocatedToken lt = (LocatedToken) $4;
1608 $$ = new ParamsParameter ((FullNamedExpression) $3, lt.Value, (Attributes) $1, lt.Location);
1610 | opt_attributes params_modifier type error {
1611 CheckIdentifierToken (yyToken, GetLocation ($4));
1619 if (params_modifiers_not_allowed)
1620 Report.Error (1670, ((Location) $1), "The `params' modifier is not allowed in current context");
1622 | PARAMS parameter_modifier
1624 Parameter.Modifier mod = (Parameter.Modifier)$2;
1625 if ((mod & Parameter.Modifier.This) != 0) {
1626 Report.Error (1104, (Location)$1, "The parameter modifiers `this' and `params' cannot be used altogether");
1628 Report.Error (1611, (Location)$1, "The params parameter cannot be declared as ref or out");
1631 | PARAMS params_modifier
1633 Error_DuplicateParameterModifier ((Location)$1, Parameter.Modifier.PARAMS);
1640 if (!arglist_allowed)
1641 Report.Error (1669, (Location) $1, "__arglist is not valid in this context");
1645 property_declaration
1649 namespace_or_type_name
1651 if (RootContext.Documentation != null)
1652 tmpComment = Lexer.consume_doc_comment ();
1656 implicit_value_parameter_type = (FullNamedExpression) $3;
1658 lexer.PropertyParsing = true;
1660 accessor_declarations
1662 lexer.PropertyParsing = false;
1663 has_get = has_set = false;
1668 Accessors accessors = (Accessors) $8;
1669 Accessor get_block = accessors != null ? accessors.get_or_add : null;
1670 Accessor set_block = accessors != null ? accessors.set_or_remove : null;
1671 bool order = accessors != null ? accessors.declared_in_reverse : false;
1673 MemberName name = (MemberName) $4;
1674 FullNamedExpression ptype = (FullNamedExpression) $3;
1676 if (name.TypeArguments != null)
1677 syntax_error (lexer.Location, "a property can't have type arguments");
1679 prop = new Property (current_class, ptype, (int) $2,
1680 name, (Attributes) $1, get_block, set_block, order, current_block);
1682 if (ptype == TypeManager.system_void_expr)
1683 Report.Error (547, ptype.Location, "`{0}': property return type cannot be `void'", prop.GetSignatureForError ());
1685 if (accessors == null)
1686 Report.Error (548, prop.Location, "`{0}': property or indexer must have at least one accessor", prop.GetSignatureForError ());
1688 if (current_container.Kind == Kind.Interface) {
1689 if (prop.Get.Block != null)
1690 Report.Error (531, prop.Location, "`{0}.get': interface members cannot have a definition", prop.GetSignatureForError ());
1692 if (prop.Set.Block != null)
1693 Report.Error (531, prop.Location, "`{0}.set': interface members cannot have a definition", prop.GetSignatureForError ());
1696 current_container.AddProperty (prop);
1697 implicit_value_parameter_type = null;
1699 if (RootContext.Documentation != null)
1700 prop.DocComment = ConsumeStoredComment ();
1705 accessor_declarations
1706 : get_accessor_declaration
1708 $$ = new Accessors ((Accessor) $1, null);
1710 | get_accessor_declaration accessor_declarations
1712 Accessors accessors = (Accessors) $2;
1713 accessors.get_or_add = (Accessor) $1;
1716 | set_accessor_declaration
1718 $$ = new Accessors (null, (Accessor) $1);
1720 | set_accessor_declaration accessor_declarations
1722 Accessors accessors = (Accessors) $2;
1723 accessors.set_or_remove = (Accessor) $1;
1724 accessors.declared_in_reverse = true;
1729 Report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1734 get_accessor_declaration
1735 : opt_attributes opt_modifiers GET
1737 // If this is not the case, then current_local_parameters has already
1738 // been set in indexer_declaration
1739 if (parsing_indexer == false)
1740 current_local_parameters = null;
1742 current_local_parameters = indexer_parameters;
1743 lexer.PropertyParsing = false;
1748 Report.Error (1007, (Location) $3, "Property accessor already defined");
1751 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, current_local_parameters, (Location) $3);
1753 current_local_parameters = null;
1754 lexer.PropertyParsing = true;
1756 if (RootContext.Documentation != null)
1757 if (Lexer.doc_state == XmlCommentState.Error)
1758 Lexer.doc_state = XmlCommentState.NotAllowed;
1764 set_accessor_declaration
1765 : opt_attributes opt_modifiers SET
1767 Parameter implicit_value_parameter = new Parameter (
1768 implicit_value_parameter_type, "value",
1769 Parameter.Modifier.NONE, null, (Location) $3);
1771 if (!parsing_indexer) {
1772 current_local_parameters = new Parameters (new Parameter [] { implicit_value_parameter });
1774 current_local_parameters = Parameters.MergeGenerated (
1775 indexer_parameters, true, implicit_value_parameter, null);
1778 lexer.PropertyParsing = false;
1783 Report.Error (1007, ((LocatedToken) $3).Location, "Property accessor already defined");
1786 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, current_local_parameters, (Location) $3);
1788 current_local_parameters = null;
1789 lexer.PropertyParsing = true;
1791 if (RootContext.Documentation != null
1792 && Lexer.doc_state == XmlCommentState.Error)
1793 Lexer.doc_state = XmlCommentState.NotAllowed;
1801 | SEMICOLON { $$ = null; }
1804 interface_declaration
1810 lexer.ConstraintsParsing = true;
1814 MemberName name = MakeName ((MemberName) $6);
1815 push_current_class (new Interface (current_namespace, current_class, name, (int) $2, (Attributes) $1), $3);
1818 opt_type_parameter_constraints_clauses
1820 lexer.ConstraintsParsing = false;
1822 current_class.SetParameterInfo ((ArrayList) $9);
1824 if (RootContext.Documentation != null) {
1825 current_container.DocComment = Lexer.consume_doc_comment ();
1826 Lexer.doc_state = XmlCommentState.Allowed;
1831 if (RootContext.Documentation != null)
1832 Lexer.doc_state = XmlCommentState.Allowed;
1836 $$ = pop_current_class ();
1838 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1839 CheckIdentifierToken (yyToken, GetLocation ($5));
1845 opt_interface_member_declarations
1849 opt_interface_member_declarations
1851 | interface_member_declarations
1854 interface_member_declarations
1855 : interface_member_declaration
1856 | interface_member_declarations interface_member_declaration
1859 interface_member_declaration
1860 : constant_declaration
1862 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1866 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1868 | method_declaration
1869 | property_declaration
1871 | indexer_declaration
1872 | operator_declaration
1874 Report.Error (567, GetLocation ($1), "Interfaces cannot contain operators");
1876 | constructor_declaration
1878 Report.Error (526, GetLocation ($1), "Interfaces cannot contain contructors");
1882 Report.Error (524, GetLocation ($1), "Interfaces cannot declare classes, structs, interfaces, delegates, or enumerations");
1886 operator_declaration
1887 : opt_attributes opt_modifiers operator_declarator
1895 OperatorDeclaration decl = (OperatorDeclaration) $3;
1896 Operator op = new Operator (
1897 current_class, decl.optype, decl.ret_type, (int) $2,
1898 current_local_parameters,
1899 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1901 if (RootContext.Documentation != null) {
1902 op.DocComment = tmpComment;
1903 Lexer.doc_state = XmlCommentState.Allowed;
1906 // Note again, checking is done in semantic analysis
1907 current_container.AddOperator (op);
1909 current_local_parameters = null;
1915 | SEMICOLON { $$ = null; }
1919 : type OPERATOR overloadable_operator OPEN_PARENS
1921 params_modifiers_not_allowed = true;
1923 opt_parameter_list_no_mod CLOSE_PARENS
1925 params_modifiers_not_allowed = false;
1927 Location loc = (Location) $2;
1928 Operator.OpType op = (Operator.OpType) $3;
1929 current_local_parameters = (Parameters)$6;
1931 int p_count = current_local_parameters.Count;
1933 if (op == Operator.OpType.Addition)
1934 op = Operator.OpType.UnaryPlus;
1935 else if (op == Operator.OpType.Subtraction)
1936 op = Operator.OpType.UnaryNegation;
1939 if (IsUnaryOperator (op)) {
1941 Report.Error (1020, loc, "Overloadable binary operator expected");
1942 } else if (p_count != 1) {
1943 Report.Error (1535, loc, "Overloaded unary operator `{0}' takes one parameter",
1944 Operator.GetName (op));
1948 Report.Error (1534, loc, "Overloaded binary operator `{0}' takes two parameters",
1949 Operator.GetName (op));
1950 } else if (p_count != 2) {
1951 Report.Error (1019, loc, "Overloadable unary operator expected");
1955 if (RootContext.Documentation != null) {
1956 tmpComment = Lexer.consume_doc_comment ();
1957 Lexer.doc_state = XmlCommentState.NotAllowed;
1960 $$ = new OperatorDeclaration (op, (FullNamedExpression) $1, loc);
1962 | conversion_operator_declarator
1965 overloadable_operator
1967 : BANG { $$ = Operator.OpType.LogicalNot; }
1968 | TILDE { $$ = Operator.OpType.OnesComplement; }
1969 | OP_INC { $$ = Operator.OpType.Increment; }
1970 | OP_DEC { $$ = Operator.OpType.Decrement; }
1971 | TRUE { $$ = Operator.OpType.True; }
1972 | FALSE { $$ = Operator.OpType.False; }
1973 // Unary and binary:
1974 | PLUS { $$ = Operator.OpType.Addition; }
1975 | MINUS { $$ = Operator.OpType.Subtraction; }
1977 | STAR { $$ = Operator.OpType.Multiply; }
1978 | DIV { $$ = Operator.OpType.Division; }
1979 | PERCENT { $$ = Operator.OpType.Modulus; }
1980 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1981 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1982 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1983 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1984 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1985 | OP_EQ { $$ = Operator.OpType.Equality; }
1986 | OP_NE { $$ = Operator.OpType.Inequality; }
1987 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1988 | OP_LT { $$ = Operator.OpType.LessThan; }
1989 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1990 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1993 conversion_operator_declarator
1994 : IMPLICIT OPERATOR type OPEN_PARENS
1996 params_modifiers_not_allowed = true;
1998 opt_parameter_list_no_mod CLOSE_PARENS
2000 params_modifiers_not_allowed = false;
2002 Location loc = (Location) $2;
2003 current_local_parameters = (Parameters)$6;
2005 if (RootContext.Documentation != null) {
2006 tmpComment = Lexer.consume_doc_comment ();
2007 Lexer.doc_state = XmlCommentState.NotAllowed;
2010 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (FullNamedExpression) $3, loc);
2012 | EXPLICIT OPERATOR type OPEN_PARENS
2014 params_modifiers_not_allowed = true;
2016 opt_parameter_list_no_mod CLOSE_PARENS
2018 params_modifiers_not_allowed = false;
2020 Location loc = (Location) $2;
2021 current_local_parameters = (Parameters)$6;
2023 if (RootContext.Documentation != null) {
2024 tmpComment = Lexer.consume_doc_comment ();
2025 Lexer.doc_state = XmlCommentState.NotAllowed;
2028 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (FullNamedExpression) $3, loc);
2032 syntax_error ((Location) $1, "'operator' expected");
2036 syntax_error ((Location) $1, "'operator' expected");
2040 constructor_declaration
2043 constructor_declarator
2046 Constructor c = (Constructor) $3;
2047 c.Block = (ToplevelBlock) $4;
2048 c.OptAttributes = (Attributes) $1;
2049 int mods = (int) $2;
2051 if (RootContext.Documentation != null)
2052 c.DocComment = ConsumeStoredComment ();
2054 if ((mods & Modifiers.STATIC) != 0 && c.Name == current_container.MemberName.Name) {
2055 if ((mods & Modifiers.Accessibility) != 0){
2056 Report.Error (515, c.Location,
2057 "`{0}': static constructor cannot have an access modifier",
2058 c.GetSignatureForError ());
2061 if (c.Initializer != null){
2062 Report.Error (514, c.Location,
2063 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2064 c.GetSignatureForError ());
2068 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, mods, Modifiers.PRIVATE, c.Location);
2069 current_container.AddConstructor (c);
2071 current_local_parameters = null;
2072 if (RootContext.Documentation != null)
2073 Lexer.doc_state = XmlCommentState.Allowed;
2077 constructor_declarator
2078 : constructor_header
2082 | constructor_header constructor_initializer
2084 ((Constructor)$1).Initializer = (ConstructorInitializer) $2;
2092 if (RootContext.Documentation != null) {
2093 tmpComment = Lexer.consume_doc_comment ();
2094 Lexer.doc_state = XmlCommentState.Allowed;
2097 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2099 LocatedToken lt = (LocatedToken) $1;
2100 current_local_parameters = (Parameters) $4;
2101 current_block = new ToplevelBlock (null, current_local_parameters, null, lt.Location);
2103 $$ = new Constructor (current_class, lt.Value, 0, current_local_parameters,
2110 | SEMICOLON { current_block = null; $$ = null; }
2113 constructor_initializer
2114 : COLON BASE OPEN_PARENS
2116 ++lexer.parsing_block;
2118 opt_argument_list CLOSE_PARENS
2120 --lexer.parsing_block;
2121 $$ = new ConstructorBaseInitializer ((ArrayList) $5, (Location) $2);
2123 | COLON THIS OPEN_PARENS
2125 ++lexer.parsing_block;
2127 opt_argument_list CLOSE_PARENS
2129 --lexer.parsing_block;
2130 $$ = new ConstructorThisInitializer ((ArrayList) $5, (Location) $2);
2133 Report.Error (1018, (Location) $1, "Keyword this or base expected");
2138 destructor_declaration
2139 : opt_attributes opt_modifiers TILDE
2141 if (RootContext.Documentation != null) {
2142 tmpComment = Lexer.consume_doc_comment ();
2143 Lexer.doc_state = XmlCommentState.NotAllowed;
2146 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2148 LocatedToken lt = (LocatedToken) $5;
2149 if (lt.Value != current_container.MemberName.Name){
2150 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2151 } else if (current_container.Kind != Kind.Class){
2152 Report.Error (575, lt.Location, "Only class types can contain destructor");
2154 Method d = new Destructor (
2155 current_class, TypeManager.system_void_expr, (int) $2, "Finalize",
2156 Parameters.EmptyReadOnlyParameters, (Attributes) $1, lt.Location);
2157 if (RootContext.Documentation != null)
2158 d.DocComment = ConsumeStoredComment ();
2160 d.Block = (ToplevelBlock) $8;
2161 current_container.AddMethod (d);
2169 EVENT type variable_declarators SEMICOLON
2171 current_array_type = null;
2172 foreach (VariableDeclaration var in (ArrayList) $5) {
2174 MemberName name = new MemberName (var.identifier,
2177 EventField e = new EventField (
2178 current_class, (FullNamedExpression) $4, (int) $2, name,
2181 if (var.expression_or_array_initializer != null) {
2182 if (current_container.Kind == Kind.Interface) {
2183 Report.Error (68, e.Location, "`{0}': event in interface cannot have initializer", e.GetSignatureForError ());
2186 e.Initializer = var.expression_or_array_initializer;
2189 current_container.AddEvent (e);
2191 if (RootContext.Documentation != null) {
2192 e.DocComment = Lexer.consume_doc_comment ();
2193 Lexer.doc_state = XmlCommentState.Allowed;
2199 EVENT type namespace_or_type_name
2202 implicit_value_parameter_type = (FullNamedExpression) $4;
2203 current_local_parameters = new Parameters (
2204 new Parameter (implicit_value_parameter_type, "value",
2205 Parameter.Modifier.NONE, null, GetLocation ($3)));
2207 lexer.EventParsing = true;
2209 event_accessor_declarations
2211 lexer.EventParsing = false;
2215 MemberName name = (MemberName) $5;
2217 if (current_container.Kind == Kind.Interface) {
2218 Report.Error (69, (Location) $3, "Event in interface cannot have add or remove accessors");
2222 Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2223 current_container.Name, name.GetSignatureForError ());
2226 Accessors accessors = (Accessors) $8;
2228 if (name.TypeArguments != null)
2229 syntax_error (lexer.Location, "an event can't have type arguments");
2231 if (accessors.get_or_add == null || accessors.set_or_remove == null)
2232 // CS0073 is already reported, so no CS0065 here.
2235 Event e = new EventProperty (
2236 current_class, (FullNamedExpression) $4, (int) $2, name,
2237 (Attributes) $1, accessors.get_or_add, accessors.set_or_remove);
2238 if (RootContext.Documentation != null) {
2239 e.DocComment = Lexer.consume_doc_comment ();
2240 Lexer.doc_state = XmlCommentState.Allowed;
2243 current_container.AddEvent (e);
2244 implicit_value_parameter_type = null;
2247 current_local_parameters = null;
2249 | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
2250 MemberName mn = (MemberName) $5;
2252 if (mn.Left != null)
2253 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2255 Report.Error (71, mn.Location, "Event declaration should use property syntax");
2257 if (RootContext.Documentation != null)
2258 Lexer.doc_state = XmlCommentState.Allowed;
2262 event_accessor_declarations
2263 : add_accessor_declaration remove_accessor_declaration
2265 $$ = new Accessors ((Accessor) $1, (Accessor) $2);
2267 | remove_accessor_declaration add_accessor_declaration
2269 Accessors accessors = new Accessors ((Accessor) $2, (Accessor) $1);
2270 accessors.declared_in_reverse = true;
2273 | add_accessor_declaration { $$ = null; }
2274 | remove_accessor_declaration { $$ = null; }
2277 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2283 add_accessor_declaration
2284 : opt_attributes ADD
2286 lexer.EventParsing = false;
2290 Accessor accessor = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, null, (Location) $2);
2291 lexer.EventParsing = true;
2294 | opt_attributes ADD error {
2295 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2298 | opt_attributes modifiers ADD {
2299 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2304 remove_accessor_declaration
2305 : opt_attributes REMOVE
2307 lexer.EventParsing = false;
2311 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, null, (Location) $2);
2312 lexer.EventParsing = true;
2314 | opt_attributes REMOVE error {
2315 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2318 | opt_attributes modifiers REMOVE {
2319 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2325 : opt_attributes opt_modifiers indexer_declarator
2328 IndexerDeclaration decl = (IndexerDeclaration) $3;
2330 implicit_value_parameter_type = decl.type;
2332 lexer.PropertyParsing = true;
2333 parsing_indexer = true;
2335 indexer_parameters = decl.param_list;
2337 accessor_declarations
2339 lexer.PropertyParsing = false;
2340 has_get = has_set = false;
2341 parsing_indexer = false;
2345 // The signature is computed from the signature of the indexer. Look
2346 // at section 3.6 on the spec
2348 IndexerDeclaration decl = (IndexerDeclaration) $3;
2349 Location loc = decl.location;
2350 Accessors accessors = (Accessors) $6;
2351 Accessor get_block = accessors != null ? accessors.get_or_add : null;
2352 Accessor set_block = accessors != null ? accessors.set_or_remove : null;
2353 bool order = accessors != null ? accessors.declared_in_reverse : false;
2356 if (decl.interface_type != null)
2357 name = new MemberName (decl.interface_type, TypeContainer.DefaultIndexerName, loc);
2359 name = new MemberName (TypeContainer.DefaultIndexerName, loc);
2361 indexer = new Indexer (current_class, decl.type, name,
2362 (int) $2, decl.param_list, (Attributes) $1,
2363 get_block, set_block, order);
2365 if (decl.type == TypeManager.system_void_expr)
2366 Report.Error (620, loc, "`{0}': indexer return type cannot be `void'", indexer.GetSignatureForError ());
2368 if (accessors == null)
2369 Report.Error (548, indexer.Location, "`{0}': property or indexer must have at least one accessor", indexer.GetSignatureForError ());
2371 if (current_container.Kind == Kind.Interface) {
2372 if (indexer.Get.Block != null)
2373 Report.Error (531, indexer.Location, "`{0}.get': interface members cannot have a definition", indexer.GetSignatureForError ());
2375 if (indexer.Set.Block != null)
2376 Report.Error (531, indexer.Location, "`{0}.set': interface members cannot have a definition", indexer.GetSignatureForError ());
2379 if (RootContext.Documentation != null)
2380 indexer.DocComment = ConsumeStoredComment ();
2382 current_container.AddIndexer (indexer);
2384 current_local_parameters = null;
2385 implicit_value_parameter_type = null;
2386 indexer_parameters = null;
2391 : type THIS OPEN_BRACKET opt_parameter_list_no_mod CLOSE_BRACKET
2393 Parameters pars = (Parameters) $4;
2395 Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
2397 if (RootContext.Documentation != null) {
2398 tmpComment = Lexer.consume_doc_comment ();
2399 Lexer.doc_state = XmlCommentState.Allowed;
2402 $$ = new IndexerDeclaration ((FullNamedExpression) $1, null, pars, (Location) $2);
2404 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2406 Parameters pars = (Parameters) $6;
2408 Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
2411 MemberName name = (MemberName) $2;
2412 $$ = new IndexerDeclaration ((FullNamedExpression) $1, name, pars, (Location) $4);
2414 if (RootContext.Documentation != null) {
2415 tmpComment = Lexer.consume_doc_comment ();
2416 Lexer.doc_state = XmlCommentState.Allowed;
2426 if (RootContext.Documentation != null)
2427 enumTypeComment = Lexer.consume_doc_comment ();
2432 MemberName name = (MemberName) $4;
2433 if (name.IsGeneric) {
2434 Report.Error (1675, name.Location, "Enums cannot have type parameters");
2437 name = MakeName (name);
2438 Enum e = new Enum (current_namespace, current_class, (TypeExpr) $5, (int) $2,
2439 name, (Attributes) $1);
2441 if (RootContext.Documentation != null)
2442 e.DocComment = enumTypeComment;
2445 EnumMember em = null;
2446 foreach (VariableDeclaration ev in (ArrayList) $7) {
2447 em = new EnumMember (
2448 e, em, ev.identifier, (Expression) ev.expression_or_array_initializer,
2449 ev.OptAttributes, ev.Location);
2451 // if (RootContext.Documentation != null)
2452 em.DocComment = ev.DocComment;
2454 e.AddEnumMember (em);
2456 if (RootContext.EvalMode)
2457 undo.AddTypeContainer (current_container, e);
2459 current_container.AddTypeContainer (e);
2467 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2470 if ($2 != TypeManager.system_int32_expr && $2 != TypeManager.system_uint32_expr &&
2471 $2 != TypeManager.system_int64_expr && $2 != TypeManager.system_uint64_expr &&
2472 $2 != TypeManager.system_int16_expr && $2 != TypeManager.system_uint16_expr &&
2473 $2 != TypeManager.system_byte_expr && $2 != TypeManager.system_sbyte_expr)
2474 Enum.Error_1008 (GetLocation ($2));
2480 Error_TypeExpected (lexer.Location);
2487 if (RootContext.Documentation != null)
2488 Lexer.doc_state = XmlCommentState.Allowed;
2490 opt_enum_member_declarations
2492 // here will be evaluated after CLOSE_BLACE is consumed.
2493 if (RootContext.Documentation != null)
2494 Lexer.doc_state = XmlCommentState.Allowed;
2502 opt_enum_member_declarations
2503 : /* empty */ { $$ = new ArrayList (4); }
2504 | enum_member_declarations opt_comma { $$ = $1; }
2507 enum_member_declarations
2508 : enum_member_declaration
2510 ArrayList l = new ArrayList (4);
2515 | enum_member_declarations COMMA enum_member_declaration
2517 ArrayList l = (ArrayList) $1;
2525 enum_member_declaration
2526 : opt_attributes IDENTIFIER
2528 VariableDeclaration vd = new VariableDeclaration (
2529 (LocatedToken) $2, null, (Attributes) $1);
2531 if (RootContext.Documentation != null) {
2532 vd.DocComment = Lexer.consume_doc_comment ();
2533 Lexer.doc_state = XmlCommentState.Allowed;
2538 | opt_attributes IDENTIFIER
2540 if (RootContext.Documentation != null) {
2541 tmpComment = Lexer.consume_doc_comment ();
2542 Lexer.doc_state = XmlCommentState.NotAllowed;
2545 ASSIGN constant_expression
2547 VariableDeclaration vd = new VariableDeclaration (
2548 (LocatedToken) $2, $5, (Attributes) $1);
2550 if (RootContext.Documentation != null)
2551 vd.DocComment = ConsumeStoredComment ();
2557 delegate_declaration
2562 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2564 MemberName name = MakeName ((MemberName) $5);
2565 Parameters p = (Parameters) $7;
2567 Delegate del = new Delegate (current_namespace, current_class, (FullNamedExpression) $4,
2568 (int) $2, name, p, (Attributes) $1);
2570 if (RootContext.Documentation != null) {
2571 del.DocComment = Lexer.consume_doc_comment ();
2572 Lexer.doc_state = XmlCommentState.Allowed;
2575 current_container.AddDelegate (del);
2576 current_delegate = del;
2577 lexer.ConstraintsParsing = true;
2579 opt_type_parameter_constraints_clauses
2581 lexer.ConstraintsParsing = false;
2585 current_delegate.SetParameterInfo ((ArrayList) $10);
2586 $$ = current_delegate;
2588 current_delegate = null;
2599 if (RootContext.Version < LanguageVersion.ISO_2)
2600 Report.FeatureIsNotAvailable (lexer.Location, "nullable types");
2606 namespace_or_type_name
2607 : IDENTIFIER opt_type_argument_list
2609 LocatedToken lt = (LocatedToken) $1;
2610 $$ = new MemberName (lt.Value, (TypeArguments) $2, lt.Location);
2612 | IDENTIFIER DOUBLE_COLON IDENTIFIER opt_type_argument_list {
2613 LocatedToken lt1 = (LocatedToken) $1;
2614 LocatedToken lt2 = (LocatedToken) $3;
2615 if (RootContext.Version == LanguageVersion.ISO_1)
2616 Report.FeatureIsNotAvailable (lt1.Location, "namespace alias qualifier");
2618 $$ = new MemberName (lt1.Value, lt2.Value, (TypeArguments) $4, lt1.Location);
2620 | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list {
2621 LocatedToken lt = (LocatedToken) $3;
2622 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $4, lt.Location);
2627 : IDENTIFIER opt_type_parameter_list
2629 LocatedToken lt = (LocatedToken) $1;
2630 $$ = new MemberName (lt.Value, (TypeArguments) $2, lt.Location);
2632 | namespace_or_type_name DOT IDENTIFIER opt_type_parameter_list
2634 LocatedToken lt = (LocatedToken) $3;
2635 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $4, lt.Location);
2640 : IDENTIFIER opt_type_parameter_list
2642 LocatedToken lt = (LocatedToken) $1;
2643 $$ = new MemberName (lt.Value, (TypeArguments)$2, lt.Location);
2648 // Generics arguments (any type, without attributes)
2651 opt_type_argument_list
2652 : /* empty */ { $$ = null; }
2653 | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2655 if (RootContext.Version < LanguageVersion.ISO_2)
2656 Report.FeatureIsNotAvailable (GetLocation ($1), "generics");
2660 | OP_GENERICS_LT error
2662 Error_TypeExpected (lexer.Location);
2667 // Generics parameters (identifiers only, with attributes), used in type, method declarations
2670 opt_type_parameter_list
2671 : /* empty */ { $$ = null; }
2672 | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2674 if (RootContext.Version < LanguageVersion.ISO_2)
2675 Report.FeatureIsNotAvailable (GetLocation ($1), "generics");
2679 | OP_GENERICS_LT error
2681 Error_TypeExpected (lexer.Location);
2688 TypeArguments type_args = new TypeArguments (lexer.Location);
2689 type_args.Add ((Expression) $1);
2692 | type_arguments COMMA type_argument
2694 TypeArguments type_args = (TypeArguments) $1;
2695 type_args.Add ((Expression) $3);
2705 | attribute_sections type
2707 SimpleName sn = $2 as SimpleName;
2709 Error_TypeExpected (GetLocation ($2));
2711 $2 = new TypeParameterName (sn.Name, (Attributes) $1, lexer.Location);
2717 * Before you think of adding a return_type, notice that we have been
2718 * using two rules in the places where it matters (one rule using type
2719 * and another identical one that uses VOID as the return type). This
2720 * gets rid of a shift/reduce couple
2723 : namespace_or_type_name opt_nullable
2725 MemberName name = (MemberName) $1;
2728 $$ = new ComposedCast (name.GetTypeExpression (), "?", lexer.Location);
2730 if (name.Left == null && name.Name == "var" &&
2731 (RootContext.Version > LanguageVersion.ISO_2 || RootContext.Version == LanguageVersion.Default_MCS))
2732 $$ = current_array_type = new VarExpr (name.Location);
2734 $$ = name.GetTypeExpression ();
2737 | builtin_types opt_nullable
2740 $$ = new ComposedCast ((FullNamedExpression) $1, "?", lexer.Location);
2750 // Note that here only unmanaged types are allowed but we
2751 // can't perform checks during this phase - we do it during
2752 // semantic analysis.
2754 $$ = new ComposedCast ((FullNamedExpression) $1, "*", Lexer.Location);
2758 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2763 : builtin_types opt_nullable
2766 $$ = new ComposedCast ((FullNamedExpression) $1, "?", lexer.Location);
2768 | non_expression_type rank_specifier
2770 Location loc = GetLocation ($1);
2772 loc = lexer.Location;
2773 $$ = new ComposedCast ((FullNamedExpression) $1, (string) $2, loc);
2775 | non_expression_type STAR
2777 Location loc = GetLocation ($1);
2779 loc = lexer.Location;
2780 $$ = new ComposedCast ((FullNamedExpression) $1, "*", loc);
2783 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2784 // through this different path
2786 | multiplicative_expression STAR
2788 FullNamedExpression e = $1 as FullNamedExpression;
2790 $$ = new ComposedCast (e, "*");
2792 Error_TypeExpected (GetLocation ($1));
2799 ArrayList types = new ArrayList (2);
2803 | type_list COMMA base_type_name
2805 ArrayList types = (ArrayList) $1;
2814 if ($1 is ComposedCast)
2815 Report.Error (1521, GetLocation ($1), "Invalid base type `{0}'", ((ComposedCast)$1).GetSignatureForError ());
2820 Error_TypeExpected (lexer.Location);
2825 * replaces all the productions for isolating the various
2826 * simple types, but we need this to reuse it easily in local_variable_type
2829 : OBJECT { $$ = TypeManager.system_object_expr; }
2830 | STRING { $$ = TypeManager.system_string_expr; }
2831 | BOOL { $$ = TypeManager.system_boolean_expr; }
2832 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2833 | FLOAT { $$ = TypeManager.system_single_expr; }
2834 | DOUBLE { $$ = TypeManager.system_double_expr; }
2835 | VOID { $$ = TypeManager.system_void_expr; }
2840 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2841 | BYTE { $$ = TypeManager.system_byte_expr; }
2842 | SHORT { $$ = TypeManager.system_int16_expr; }
2843 | USHORT { $$ = TypeManager.system_uint16_expr; }
2844 | INT { $$ = TypeManager.system_int32_expr; }
2845 | UINT { $$ = TypeManager.system_uint32_expr; }
2846 | LONG { $$ = TypeManager.system_int64_expr; }
2847 | ULONG { $$ = TypeManager.system_uint64_expr; }
2848 | CHAR { $$ = TypeManager.system_char_expr; }
2852 : type rank_specifiers
2854 string rank_specifiers = (string) $2;
2855 $$ = current_array_type = new ComposedCast ((FullNamedExpression) $1, rank_specifiers);
2860 // Expressions, section 7.5
2869 MemberName mn = (MemberName) $1;
2870 $$ = mn.GetTypeExpression ();
2872 | IDENTIFIER DOUBLE_COLON IDENTIFIER opt_type_argument_list
2874 LocatedToken lt1 = (LocatedToken) $1;
2875 LocatedToken lt2 = (LocatedToken) $3;
2876 if (RootContext.Version == LanguageVersion.ISO_1)
2877 Report.FeatureIsNotAvailable (lt1.Location, "namespace alias qualifier");
2879 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, (TypeArguments) $4, lt1.Location);
2881 | parenthesized_expression
2882 | default_value_expression
2884 | invocation_expression
2888 | post_increment_expression
2889 | post_decrement_expression
2893 | checked_expression
2894 | unchecked_expression
2895 | pointer_member_access
2896 | anonymous_method_expression
2903 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
2904 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); }
2905 | NULL { $$ = new NullLiteral (lexer.Location); }
2909 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
2910 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
2911 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
2916 object v = lexer.Value;
2919 $$ = new IntLiteral ((int) v, lexer.Location);
2920 } else if (v is uint)
2921 $$ = new UIntLiteral ((UInt32) v, lexer.Location);
2923 $$ = new LongLiteral ((Int64) v, lexer.Location);
2924 else if (v is ulong)
2925 $$ = new ULongLiteral ((UInt64) v, lexer.Location);
2927 Console.WriteLine ("OOPS. Unexpected result from scanner");
2932 : TRUE { $$ = new BoolLiteral (true, lexer.Location); }
2933 | FALSE { $$ = new BoolLiteral (false, lexer.Location); }
2936 parenthesized_expression_0
2937 : OPEN_PARENS expression CLOSE_PARENS
2940 lexer.Deambiguate_CloseParens ($$);
2941 // After this, the next token returned is one of
2942 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST (CLOSE_PARENS), CLOSE_PARENS_OPEN_PARENS
2943 // or CLOSE_PARENS_MINUS.
2945 | OPEN_PARENS expression error { CheckToken (1026, yyToken, "Expecting ')'", lexer.Location); }
2948 parenthesized_expression
2949 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2953 | parenthesized_expression_0 CLOSE_PARENS
2957 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2959 // If a parenthesized expression is followed by a minus, we need to wrap
2960 // the expression inside a ParenthesizedExpression for the CS0075 check
2961 // in Binary.DoResolve().
2962 $$ = new ParenthesizedExpression ((Expression) $1);
2967 : primary_expression DOT IDENTIFIER opt_type_argument_list
2969 LocatedToken lt = (LocatedToken) $3;
2970 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
2972 | predefined_type DOT IDENTIFIER opt_type_argument_list
2974 LocatedToken lt = (LocatedToken) $3;
2975 // TODO: Location is wrong as some predefined types doesn't hold a location
2976 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
2984 invocation_expression
2985 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2988 Report.Error (1, (Location) $2, "Parse error");
2990 $$ = new Invocation ((Expression) $1, (ArrayList) $3);
2992 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2994 $$ = new Invocation ((Expression) $1, new ArrayList ());
2996 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2998 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
3000 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
3002 ArrayList args = new ArrayList (1);
3004 $$ = new Invocation ((Expression) $1, args);
3006 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
3008 ArrayList args = ((ArrayList) $4);
3010 $$ = new Invocation ((Expression) $1, args);
3014 opt_object_or_collection_initializer
3015 : /* empty */ { $$ = null; }
3016 | object_or_collection_initializer
3019 object_or_collection_initializer
3020 : OPEN_BRACE opt_member_initializer_list CLOSE_BRACE
3023 $$ = CollectionOrObjectInitializers.Empty;
3025 $$ = new CollectionOrObjectInitializers ((ArrayList) $2, GetLocation ($1));
3027 | OPEN_BRACE member_initializer_list COMMA CLOSE_BRACE
3029 $$ = new CollectionOrObjectInitializers ((ArrayList) $2, GetLocation ($1));
3033 opt_member_initializer_list
3034 : /* empty */ { $$ = null; }
3035 | member_initializer_list
3041 member_initializer_list
3042 : member_initializer
3044 ArrayList a = new ArrayList ();
3048 | member_initializer_list COMMA member_initializer
3050 ArrayList a = (ArrayList)$1;
3057 : IDENTIFIER ASSIGN initializer_value
3059 LocatedToken lt = $1 as LocatedToken;
3060 $$ = new ElementInitializer (lt.Value, (Expression)$3, lt.Location);
3062 | non_assignment_expression
3064 $$ = new CollectionElementInitializer ((Expression)$1);
3066 | OPEN_BRACE expression_list CLOSE_BRACE
3068 $$ = new CollectionElementInitializer ((ArrayList)$2, GetLocation ($1));
3070 | OPEN_BRACE CLOSE_BRACE
3072 Report.Error (1920, GetLocation ($1), "An element initializer cannot be empty");
3078 | object_or_collection_initializer
3082 : /* empty */ { $$ = null; }
3089 ArrayList list = new ArrayList (4);
3093 | argument_list COMMA argument
3095 ArrayList list = (ArrayList) $1;
3099 | argument_list COMMA
3101 Report.Error (839, GetLocation ($2), "An argument is missing");
3106 Report.Error (839, GetLocation ($1), "An argument is missing");
3109 | argument_list error {
3110 CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
3118 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
3120 | non_simple_argument
3127 : REF variable_reference
3129 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
3131 | OUT variable_reference
3133 $$ = new Argument ((Expression) $2, Argument.AType.Out);
3135 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
3137 ArrayList list = (ArrayList) $3;
3138 Argument[] args = new Argument [list.Count];
3139 list.CopyTo (args, 0);
3141 Expression expr = new Arglist (args, (Location) $1);
3142 $$ = new Argument (expr, Argument.AType.Expression);
3144 | ARGLIST OPEN_PARENS CLOSE_PARENS
3146 $$ = new Argument (new Arglist ((Location) $1), Argument.AType.Expression);
3150 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
3155 : expression { note ("section 5.4"); $$ = $1; }
3159 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
3161 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
3163 | primary_expression rank_specifiers
3165 // So the super-trick is that primary_expression
3166 // can only be either a SimpleName or a MemberAccess.
3167 // The MemberAccess case arises when you have a fully qualified type-name like :
3169 // SimpleName is when you have
3172 Expression expr = (Expression) $1;
3173 if (expr is ComposedCast){
3174 $$ = new ComposedCast ((ComposedCast)expr, (string) $2);
3175 } else if (expr is ATypeNameExpression){
3177 // So we extract the string corresponding to the SimpleName
3180 $$ = new ComposedCast ((ATypeNameExpression)expr, (string) $2);
3182 Error_ExpectingTypeName (expr);
3183 $$ = TypeManager.system_object_expr;
3186 current_array_type = (FullNamedExpression)$$;
3193 ArrayList list = new ArrayList (4);
3197 | expression_list COMMA expression
3199 ArrayList list = (ArrayList) $1;
3208 $$ = new This (current_block, (Location) $1);
3213 : BASE DOT IDENTIFIER opt_type_argument_list
3215 LocatedToken lt = (LocatedToken) $3;
3216 $$ = new BaseAccess (lt.Value, (TypeArguments) $4, lt.Location);
3218 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
3220 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
3223 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
3228 post_increment_expression
3229 : primary_expression OP_INC
3231 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
3232 (Expression) $1, (Location) $2);
3236 post_decrement_expression
3237 : primary_expression OP_DEC
3239 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
3240 (Expression) $1, (Location) $2);
3245 : object_or_delegate_creation_expression
3246 | array_creation_expression
3247 | anonymous_type_expression
3250 object_or_delegate_creation_expression
3251 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS opt_object_or_collection_initializer
3254 if (RootContext.Version <= LanguageVersion.ISO_2)
3255 Report.FeatureIsNotAvailable (GetLocation ($1), "object initializers");
3257 $$ = new NewInitialize ((Expression) $2, (ArrayList) $4, (CollectionOrObjectInitializers) $6, (Location) $1);
3260 $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
3262 | NEW type object_or_collection_initializer
3264 if (RootContext.Version <= LanguageVersion.ISO_2)
3265 Report.FeatureIsNotAvailable (GetLocation ($1), "collection initializers");
3267 $$ = new NewInitialize ((Expression) $2, null, (CollectionOrObjectInitializers) $3, (Location) $1);
3271 array_creation_expression
3272 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
3274 opt_array_initializer
3276 $$ = new ArrayCreation ((FullNamedExpression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
3278 | NEW type rank_specifiers array_initializer
3280 $$ = new ArrayCreation ((FullNamedExpression) $2, (string) $3, (ArrayList) $4, (Location) $1);
3282 | NEW rank_specifiers array_initializer
3284 $$ = new ImplicitlyTypedArrayCreation ((string) $2, (ArrayList) $3, (Location) $1);
3288 Report.Error (1031, (Location) $1, "Type expected");
3293 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
3298 anonymous_type_expression
3299 : NEW OPEN_BRACE anonymous_type_parameters_opt_comma CLOSE_BRACE
3301 if (RootContext.Version <= LanguageVersion.ISO_2)
3302 Report.FeatureIsNotAvailable (GetLocation ($1), "anonymous types");
3304 $$ = new AnonymousTypeDeclaration ((ArrayList) $3, current_container, GetLocation ($1));
3308 anonymous_type_parameters_opt_comma
3309 : anonymous_type_parameters_opt
3310 | anonymous_type_parameters COMMA
3313 anonymous_type_parameters_opt
3315 | anonymous_type_parameters
3318 anonymous_type_parameters
3319 : anonymous_type_parameter
3321 ArrayList a = new ArrayList (4);
3325 | anonymous_type_parameters COMMA anonymous_type_parameter
3327 ArrayList a = (ArrayList) $1;
3333 anonymous_type_parameter
3334 : IDENTIFIER ASSIGN variable_initializer
3336 LocatedToken lt = (LocatedToken)$1;
3337 $$ = new AnonymousTypeParameter ((Expression)$3, lt.Value, lt.Location);
3341 LocatedToken lt = (LocatedToken)$1;
3342 $$ = new AnonymousTypeParameter (new SimpleName (lt.Value, lt.Location),
3343 lt.Value, lt.Location);
3345 | BASE DOT IDENTIFIER opt_type_argument_list
3347 LocatedToken lt = (LocatedToken) $3;
3348 BaseAccess ba = new BaseAccess (lt.Value, (TypeArguments) $4, lt.Location);
3349 $$ = new AnonymousTypeParameter (ba, lt.Value, lt.Location);
3353 MemberAccess ma = (MemberAccess) $1;
3354 $$ = new AnonymousTypeParameter (ma, ma.Name, ma.Location);
3358 Report.Error (746, lexer.Location, "Invalid anonymous type member declarator. " +
3359 "Anonymous type members must be a member assignment, simple name or member access expression");
3374 opt_rank_specifier_or_nullable
3382 | opt_nullable rank_specifiers
3385 $$ = "?" + (string) $2;
3392 : rank_specifier opt_rank_specifier
3394 $$ = (string) $2 + (string) $1;
3399 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3401 $$ = "[" + (string) $2 + "]";
3421 | dim_separators COMMA
3423 $$ = (string) $1 + ",";
3427 opt_array_initializer
3439 : OPEN_BRACE CLOSE_BRACE
3441 ArrayList list = new ArrayList (4);
3444 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3446 $$ = (ArrayList) $2;
3450 variable_initializer_list
3451 : variable_initializer
3453 ArrayList list = new ArrayList (4);
3457 | variable_initializer_list COMMA variable_initializer
3459 ArrayList list = (ArrayList) $1;
3468 pushed_current_array_type = current_array_type;
3469 lexer.TypeOfParsing = true;
3471 OPEN_PARENS typeof_type_expression CLOSE_PARENS
3473 lexer.TypeOfParsing = false;
3474 Expression type = (Expression)$4;
3475 if (type == TypeManager.system_void_expr)
3476 $$ = new TypeOfVoid ((Location) $1);
3478 $$ = new TypeOf (type, (Location) $1);
3479 current_array_type = pushed_current_array_type;
3483 typeof_type_expression
3490 $$ = new UnboundTypeExpression ((MemberName)$1, lexer.Location);
3494 Error_TypeExpected (lexer.Location);
3499 : IDENTIFIER GENERIC_DIMENSION
3501 if (RootContext.Version < LanguageVersion.ISO_2)
3502 Report.FeatureIsNotAvailable (lexer.Location, "generics");
3504 LocatedToken lt = (LocatedToken) $1;
3505 TypeArguments ta = new TypeArguments ((int)$2, lt.Location);
3507 $$ = new MemberName (lt.Value, ta, lt.Location);
3509 | IDENTIFIER DOUBLE_COLON IDENTIFIER GENERIC_DIMENSION
3511 LocatedToken lt = (LocatedToken) $1;
3512 MemberName left = new MemberName (lt.Value, lt.Location);
3513 lt = (LocatedToken) $3;
3514 TypeArguments ta = new TypeArguments ((int)$4, lt.Location);
3516 if (RootContext.Version == LanguageVersion.ISO_1)
3517 Report.FeatureIsNotAvailable (lt.Location, "namespace alias qualifier");
3519 $$ = new MemberName (left, lt.Value, ta, lt.Location);
3521 | unbound_type_name DOT IDENTIFIER GENERIC_DIMENSION
3523 LocatedToken lt = (LocatedToken) $3;
3524 TypeArguments ta = new TypeArguments ((int)$4, lt.Location);
3526 $$ = new MemberName ((MemberName)$1, lt.Value, ta, lt.Location);
3528 | namespace_or_type_name DOT IDENTIFIER GENERIC_DIMENSION
3530 LocatedToken lt = (LocatedToken) $3;
3531 TypeArguments ta = new TypeArguments ((int)$4, lt.Location);
3533 $$ = new MemberName ((MemberName)$1, lt.Value, ta, lt.Location);
3539 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3540 $$ = new SizeOf ((Expression) $3, (Location) $1);
3545 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3547 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3551 unchecked_expression
3552 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3554 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3558 pointer_member_access
3559 : primary_expression OP_PTR IDENTIFIER
3562 LocatedToken lt = (LocatedToken) $3;
3564 deref = new Indirection ((Expression) $1, lt.Location);
3565 $$ = new MemberAccess (deref, lt.Value);
3569 anonymous_method_expression
3570 : DELEGATE opt_anonymous_method_signature
3572 start_anonymous (false, (Parameters) $2, (Location) $1);
3576 $$ = end_anonymous ((ToplevelBlock) $4, (Location) $1);
3580 opt_anonymous_method_signature
3581 : /* empty */ { $$ = null; }
3582 | anonymous_method_signature
3585 anonymous_method_signature
3588 params_modifiers_not_allowed = true;
3590 opt_formal_parameter_list CLOSE_PARENS
3592 params_modifiers_not_allowed = false;
3597 default_value_expression
3598 : DEFAULT_OPEN_PARENS type CLOSE_PARENS
3600 if (RootContext.Version < LanguageVersion.ISO_2)
3601 Report.FeatureIsNotAvailable (lexer.Location, "default value expression");
3603 $$ = new DefaultValueExpression ((Expression) $2, lexer.Location);
3608 : primary_expression
3609 | BANG prefixed_unary_expression
3611 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3613 | TILDE prefixed_unary_expression
3615 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3621 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3623 $$ = new Cast ((Expression) $1, (Expression) $3);
3625 | parenthesized_expression_0 CLOSE_PARENS_NO_CAST default_value_expression
3627 $$ = new Cast ((Expression) $1, (Expression) $3);
3629 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3631 $$ = new Cast ((Expression) $1, (Expression) $3);
3637 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3639 // TODO: wrong location
3640 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3645 // The idea to split this out is from Rhys' grammar
3646 // to solve the problem with casts.
3648 prefixed_unary_expression
3650 | PLUS prefixed_unary_expression
3652 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3654 | MINUS prefixed_unary_expression
3656 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3658 | OP_INC prefixed_unary_expression
3660 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3661 (Expression) $2, (Location) $1);
3663 | OP_DEC prefixed_unary_expression
3665 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3666 (Expression) $2, (Location) $1);
3668 | STAR prefixed_unary_expression
3670 $$ = new Indirection ((Expression) $2, (Location) $1);
3672 | BITWISE_AND prefixed_unary_expression
3674 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3678 multiplicative_expression
3679 : prefixed_unary_expression
3680 | multiplicative_expression STAR prefixed_unary_expression
3682 $$ = new Binary (Binary.Operator.Multiply,
3683 (Expression) $1, (Expression) $3);
3685 | multiplicative_expression DIV prefixed_unary_expression
3687 $$ = new Binary (Binary.Operator.Division,
3688 (Expression) $1, (Expression) $3);
3690 | multiplicative_expression PERCENT prefixed_unary_expression
3692 $$ = new Binary (Binary.Operator.Modulus,
3693 (Expression) $1, (Expression) $3);
3698 : multiplicative_expression
3699 | additive_expression PLUS multiplicative_expression
3701 $$ = new Binary (Binary.Operator.Addition,
3702 (Expression) $1, (Expression) $3);
3704 | additive_expression MINUS multiplicative_expression
3706 $$ = new Binary (Binary.Operator.Subtraction,
3707 (Expression) $1, (Expression) $3);
3712 : additive_expression
3713 | shift_expression OP_SHIFT_LEFT additive_expression
3715 $$ = new Binary (Binary.Operator.LeftShift,
3716 (Expression) $1, (Expression) $3);
3718 | shift_expression OP_SHIFT_RIGHT additive_expression
3720 $$ = new Binary (Binary.Operator.RightShift,
3721 (Expression) $1, (Expression) $3);
3725 relational_expression
3727 | relational_expression OP_LT shift_expression
3729 $$ = new Binary (Binary.Operator.LessThan,
3730 (Expression) $1, (Expression) $3);
3732 | relational_expression OP_GT shift_expression
3734 $$ = new Binary (Binary.Operator.GreaterThan,
3735 (Expression) $1, (Expression) $3);
3737 | relational_expression OP_LE shift_expression
3739 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3740 (Expression) $1, (Expression) $3);
3742 | relational_expression OP_GE shift_expression
3744 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3745 (Expression) $1, (Expression) $3);
3747 | relational_expression IS type
3749 $$ = new Is ((Expression) $1, (Expression) $3, (Location) $2);
3751 | relational_expression AS type
3753 $$ = new As ((Expression) $1, (Expression) $3, (Location) $2);
3758 : relational_expression
3759 | equality_expression OP_EQ relational_expression
3761 $$ = new Binary (Binary.Operator.Equality,
3762 (Expression) $1, (Expression) $3);
3764 | equality_expression OP_NE relational_expression
3766 $$ = new Binary (Binary.Operator.Inequality,
3767 (Expression) $1, (Expression) $3);
3772 : equality_expression
3773 | and_expression BITWISE_AND equality_expression
3775 $$ = new Binary (Binary.Operator.BitwiseAnd,
3776 (Expression) $1, (Expression) $3);
3780 exclusive_or_expression
3782 | exclusive_or_expression CARRET and_expression
3784 $$ = new Binary (Binary.Operator.ExclusiveOr,
3785 (Expression) $1, (Expression) $3);
3789 inclusive_or_expression
3790 : exclusive_or_expression
3791 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3793 $$ = new Binary (Binary.Operator.BitwiseOr,
3794 (Expression) $1, (Expression) $3);
3798 conditional_and_expression
3799 : inclusive_or_expression
3800 | conditional_and_expression OP_AND inclusive_or_expression
3802 $$ = new Binary (Binary.Operator.LogicalAnd,
3803 (Expression) $1, (Expression) $3);
3807 conditional_or_expression
3808 : conditional_and_expression
3809 | conditional_or_expression OP_OR conditional_and_expression
3811 $$ = new Binary (Binary.Operator.LogicalOr,
3812 (Expression) $1, (Expression) $3);
3816 conditional_expression
3817 : conditional_or_expression
3818 | conditional_or_expression INTERR expression COLON expression
3820 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
3822 | conditional_or_expression OP_COALESCING expression
3824 if (RootContext.Version < LanguageVersion.ISO_2)
3825 Report.FeatureIsNotAvailable (GetLocation ($2), "null coalescing operator");
3827 $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $3, lexer.Location);
3829 | primary_expression INTERR_NULLABLE opt_rank_specifier
3832 // TODO: Is this the best place to handle `= (foo?)a;' ?
3834 if (RootContext.Version < LanguageVersion.ISO_2)
3835 Report.FeatureIsNotAvailable (GetLocation ($1), "nullable types");
3837 $$ = new ComposedCast ((FullNamedExpression) $1, "?" + (string)$3, lexer.Location);
3841 assignment_expression
3842 : prefixed_unary_expression ASSIGN expression
3844 $$ = new SimpleAssign ((Expression) $1, (Expression) $3);
3846 | prefixed_unary_expression OP_MULT_ASSIGN expression
3848 $$ = new CompoundAssign (
3849 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
3851 | prefixed_unary_expression OP_DIV_ASSIGN expression
3853 $$ = new CompoundAssign (
3854 Binary.Operator.Division, (Expression) $1, (Expression) $3);
3856 | prefixed_unary_expression OP_MOD_ASSIGN expression
3858 $$ = new CompoundAssign (
3859 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
3861 | prefixed_unary_expression OP_ADD_ASSIGN expression
3863 $$ = new CompoundAssign (
3864 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
3866 | prefixed_unary_expression OP_SUB_ASSIGN expression
3868 $$ = new CompoundAssign (
3869 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3871 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3873 $$ = new CompoundAssign (
3874 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
3876 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3878 $$ = new CompoundAssign (
3879 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
3881 | prefixed_unary_expression OP_AND_ASSIGN expression
3883 $$ = new CompoundAssign (
3884 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
3886 | prefixed_unary_expression OP_OR_ASSIGN expression
3888 $$ = new CompoundAssign (
3889 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
3891 | prefixed_unary_expression OP_XOR_ASSIGN expression
3893 $$ = new CompoundAssign (
3894 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
3898 lambda_parameter_list
3901 ArrayList pars = new ArrayList (4);
3906 | lambda_parameter_list COMMA lambda_parameter
3908 ArrayList pars = (ArrayList) $1;
3909 Parameter p = (Parameter)$3;
3910 if (pars[0].GetType () != p.GetType ()) {
3911 Report.Error (748, p.Location, "All lambda parameters must be typed either explicitly or implicitly");
3920 : parameter_modifier type IDENTIFIER
3922 LocatedToken lt = (LocatedToken) $3;
3924 $$ = new Parameter ((FullNamedExpression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3928 LocatedToken lt = (LocatedToken) $2;
3930 $$ = new Parameter ((FullNamedExpression) $1, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
3934 LocatedToken lt = (LocatedToken) $1;
3935 $$ = new ImplicitLambdaParameter (lt.Value, lt.Location);
3939 opt_lambda_parameter_list
3940 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
3941 | lambda_parameter_list {
3942 ArrayList pars_list = (ArrayList) $1;
3943 $$ = new Parameters ((Parameter[])pars_list.ToArray (typeof (Parameter)));
3947 lambda_expression_body
3949 start_block (lexer.Location);
3953 Block b = end_block (lexer.Location);
3954 b.AddStatement (new ContextualReturn ((Expression) $2));
3965 LocatedToken lt = (LocatedToken) $1;
3966 Parameter p = new ImplicitLambdaParameter (lt.Value, lt.Location);
3967 start_anonymous (true, new Parameters (p), (Location) $2);
3969 lambda_expression_body
3971 $$ = end_anonymous ((ToplevelBlock) $4, (Location) $2);
3973 | OPEN_PARENS_LAMBDA opt_lambda_parameter_list CLOSE_PARENS ARROW
3975 start_anonymous (true, (Parameters) $2, (Location) $4);
3977 lambda_expression_body
3979 $$ = end_anonymous ((ToplevelBlock) $6, (Location) $4);
3984 : assignment_expression
3985 | non_assignment_expression
3988 non_assignment_expression
3989 : conditional_expression
4011 lexer.ConstraintsParsing = true;
4015 MemberName name = MakeName ((MemberName) $6);
4016 push_current_class (new Class (current_namespace, current_class, name, (int) $2, (Attributes) $1), $3);
4019 opt_type_parameter_constraints_clauses
4021 lexer.ConstraintsParsing = false;
4023 current_class.SetParameterInfo ((ArrayList) $9);
4025 if (RootContext.Documentation != null) {
4026 current_container.DocComment = Lexer.consume_doc_comment ();
4027 Lexer.doc_state = XmlCommentState.Allowed;
4032 if (RootContext.Documentation != null)
4033 Lexer.doc_state = XmlCommentState.Allowed;
4037 $$ = pop_current_class ();
4045 { $$ = $1; } // location
4049 : /* empty */ { $$ = (int) 0; }
4055 | modifiers modifier
4060 if ((m1 & m2) != 0) {
4061 Location l = lexer.Location;
4062 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
4064 $$ = (int) (m1 | m2);
4069 : NEW { $$ = Modifiers.NEW; }
4070 | PUBLIC { $$ = Modifiers.PUBLIC; }
4071 | PROTECTED { $$ = Modifiers.PROTECTED; }
4072 | INTERNAL { $$ = Modifiers.INTERNAL; }
4073 | PRIVATE { $$ = Modifiers.PRIVATE; }
4074 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
4075 | SEALED { $$ = Modifiers.SEALED; }
4076 | STATIC { $$ = Modifiers.STATIC; }
4077 | READONLY { $$ = Modifiers.READONLY; }
4078 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
4079 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
4080 | EXTERN { $$ = Modifiers.EXTERN; }
4081 | VOLATILE { $$ = Modifiers.VOLATILE; }
4082 | UNSAFE { $$ = Modifiers.UNSAFE; }
4091 : COLON type_list { current_container.AddBasesForPart (current_class, (ArrayList) $2); }
4094 opt_type_parameter_constraints_clauses
4095 : /* empty */ { $$ = null; }
4096 | type_parameter_constraints_clauses
4100 type_parameter_constraints_clauses
4101 : type_parameter_constraints_clause {
4102 ArrayList constraints = new ArrayList (1);
4103 constraints.Add ($1);
4106 | type_parameter_constraints_clauses type_parameter_constraints_clause {
4107 ArrayList constraints = (ArrayList) $1;
4108 Constraints new_constraint = (Constraints)$2;
4110 foreach (Constraints c in constraints) {
4111 if (new_constraint.TypeParameter == c.TypeParameter) {
4112 Report.Error (409, new_constraint.Location, "A constraint clause has already been specified for type parameter `{0}'",
4113 new_constraint.TypeParameter);
4117 constraints.Add (new_constraint);
4122 type_parameter_constraints_clause
4123 : WHERE IDENTIFIER COLON type_parameter_constraints {
4124 LocatedToken lt = (LocatedToken) $2;
4125 $$ = new Constraints (lt.Value, (ArrayList) $4, lt.Location);
4129 type_parameter_constraints
4130 : type_parameter_constraint {
4131 ArrayList constraints = new ArrayList (1);
4132 constraints.Add ($1);
4135 | type_parameter_constraints COMMA type_parameter_constraint {
4136 ArrayList constraints = (ArrayList) $1;
4138 constraints.Add ($3);
4143 type_parameter_constraint
4145 | NEW OPEN_PARENS CLOSE_PARENS {
4146 $$ = SpecialConstraint.Constructor;
4149 $$ = SpecialConstraint.ReferenceType;
4152 $$ = SpecialConstraint.ValueType;
4161 // A block is "contained" on the following places:
4163 // property_declaration as part of the accessor body (get/set)
4164 // operator_declaration
4165 // constructor_declaration
4166 // destructor_declaration
4167 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
4172 ++lexer.parsing_block;
4173 start_block ((Location) $1);
4175 opt_statement_list CLOSE_BRACE
4177 --lexer.parsing_block;
4178 $$ = end_block ((Location) $4);
4185 ++lexer.parsing_block;
4187 opt_statement_list CLOSE_BRACE
4189 --lexer.parsing_block;
4190 $$ = end_block ((Location) $4);
4201 | statement_list statement
4205 : declaration_statement
4207 if ($1 != null && (Block) $1 != current_block){
4208 current_block.AddStatement ((Statement) $1);
4209 current_block = (Block) $1;
4212 | valid_declaration_statement
4214 current_block.AddStatement ((Statement) $1);
4220 // The interactive_statement and its derivatives are only
4221 // used to provide a special version of `expression_statement'
4222 // that has a side effect of assigning the expression to
4225 interactive_statement_list
4226 : interactive_statement
4227 | interactive_statement_list interactive_statement
4230 interactive_statement
4231 : declaration_statement
4233 if ($1 != null && (Block) $1 != current_block){
4234 current_block.AddStatement ((Statement) $1);
4235 current_block = (Block) $1;
4238 | interactive_valid_declaration_statement
4240 current_block.AddStatement ((Statement) $1);
4245 valid_declaration_statement
4248 | expression_statement
4249 | selection_statement
4250 | iteration_statement
4254 | unchecked_statement
4261 interactive_valid_declaration_statement
4264 | interactive_expression_statement
4265 | selection_statement
4266 | iteration_statement
4270 | unchecked_statement
4278 : valid_declaration_statement
4279 | declaration_statement
4281 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4286 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4294 $$ = EmptyStatement.Value;
4301 LocatedToken lt = (LocatedToken) $1;
4302 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
4304 if (current_block.AddLabel (labeled))
4305 current_block.AddStatement (labeled);
4310 declaration_statement
4311 : local_variable_declaration SEMICOLON
4313 current_array_type = null;
4315 DictionaryEntry de = (DictionaryEntry) $1;
4316 Expression e = (Expression) de.Key;
4318 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
4322 | local_constant_declaration SEMICOLON
4324 current_array_type = null;
4326 DictionaryEntry de = (DictionaryEntry) $1;
4328 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
4334 * The following is from Rhys' grammar:
4335 * > Types in local variable declarations must be recognized as
4336 * > expressions to prevent reduce/reduce errors in the grammar.
4337 * > The expressions are converted into types during semantic analysis.
4340 : primary_expression opt_rank_specifier_or_nullable
4342 // FIXME: Do something smart here regarding the composition of the type.
4344 // Ok, the above "primary_expression" is there to get rid of
4345 // both reduce/reduce and shift/reduces in the grammar, it should
4346 // really just be "type_name". If you use type_name, a reduce/reduce
4347 // creeps up. If you use namespace_or_type_name (which is all we need
4348 // really) two shift/reduces appear.
4351 // So the super-trick is that primary_expression
4352 // can only be either a SimpleName or a MemberAccess.
4353 // The MemberAccess case arises when you have a fully qualified type-name like :
4355 // SimpleName is when you have
4358 Expression expr = (Expression) $1;
4359 string rank_or_nullable = (string) $2;
4361 if (expr is ComposedCast){
4362 $$ = new ComposedCast ((ComposedCast)expr, rank_or_nullable);
4363 } else if (expr is ATypeNameExpression){
4365 // So we extract the string corresponding to the SimpleName
4368 if (rank_or_nullable.Length == 0) {
4369 SimpleName sn = expr as SimpleName;
4370 if (sn != null && sn.Name == "var" &&
4371 (RootContext.Version > LanguageVersion.ISO_2 || RootContext.Version == LanguageVersion.Default_MCS))
4372 $$ = current_array_type = new VarExpr (sn.Location);
4376 $$ = new ComposedCast ((ATypeNameExpression)expr, rank_or_nullable);
4379 Error_ExpectingTypeName (expr);
4380 $$ = TypeManager.system_object_expr;
4383 | builtin_types opt_rank_specifier_or_nullable
4385 if ((string) $2 == "")
4388 $$ = current_array_type = new ComposedCast ((FullNamedExpression) $1, (string) $2, lexer.Location);
4392 local_variable_pointer_type
4393 : primary_expression STAR
4395 ATypeNameExpression expr = $1 as ATypeNameExpression;
4398 $$ = new ComposedCast (expr, "*");
4400 Error_ExpectingTypeName ((Expression)$1);
4404 | builtin_types STAR
4406 $$ = new ComposedCast ((FullNamedExpression) $1, "*", lexer.Location);
4410 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
4412 | local_variable_pointer_type STAR
4414 $$ = new ComposedCast ((FullNamedExpression) $1, "*");
4418 local_variable_declaration
4419 : local_variable_type variable_declarators
4422 VarExpr ve = $1 as VarExpr;
4424 ve.VariableInitializer = (ArrayList)$2;
4426 $$ = new DictionaryEntry ($1, $2);
4430 | local_variable_pointer_type opt_rank_specifier_or_nullable variable_declarators
4435 if ((string) $2 == "")
4436 t = (Expression) $1;
4438 t = new ComposedCast ((FullNamedExpression) $1, (string) $2);
4439 $$ = new DictionaryEntry (t, $3);
4445 local_constant_declaration
4446 : CONST local_variable_type constant_declarators
4449 $$ = new DictionaryEntry ($2, $3);
4455 expression_statement
4456 : statement_expression SEMICOLON { $$ = $1; }
4459 interactive_expression_statement
4460 : interactive_statement_expression SEMICOLON { $$ = $1; }
4464 // We have to do the wrapping here and not in the case above,
4465 // because statement_expression is used for example in for_statement
4467 statement_expression
4470 Expression expr = (Expression) $1;
4471 ExpressionStatement s;
4473 s = expr as ExpressionStatement;
4476 expr.Error_InvalidExpressionStatement ();
4479 $$ = new StatementExpression (s);
4483 Report.Error (1002, GetLocation ($1), "Expecting {0}, got {1}", GetExpecting (), GetTokenName (yyToken));
4488 interactive_statement_expression
4491 Expression expr = (Expression) $1;
4492 ExpressionStatement s;
4494 s = new OptionalAssign (new SimpleName ("$retval", lexer.Location), expr, lexer.Location);
4495 $$ = new StatementExpression (s);
4499 Report.Error (1002, GetLocation ($1), "Expecting {0}, got {1}", GetExpecting (), GetTokenName (yyToken));
4510 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
4513 Location l = (Location) $1;
4515 $$ = new If ((Expression) $3, (Statement) $5, l);
4517 // FIXME: location for warning should be loc property of $5.
4518 if ($5 == EmptyStatement.Value)
4519 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4522 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
4523 embedded_statement ELSE embedded_statement
4525 Location l = (Location) $1;
4527 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
4529 // FIXME: location for warning should be loc property of $5 and $7.
4530 if ($5 == EmptyStatement.Value)
4531 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4532 if ($7 == EmptyStatement.Value)
4533 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4538 : SWITCH OPEN_PARENS
4540 if (switch_stack == null)
4541 switch_stack = new Stack (2);
4542 switch_stack.Push (current_block);
4544 expression CLOSE_PARENS
4547 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
4548 current_block = (Block) switch_stack.Pop ();
4564 Report.Warning (1522, 1, lexer.Location, "Empty switch block");
4565 $$ = new ArrayList ();
4573 ArrayList sections = new ArrayList (4);
4578 | switch_sections switch_section
4580 ArrayList sections = (ArrayList) $1;
4590 current_block = current_block.CreateSwitchBlock (lexer.Location);
4594 $$ = new SwitchSection ((ArrayList) $1, current_block.Explicit);
4601 ArrayList labels = new ArrayList (4);
4606 | switch_labels switch_label
4608 ArrayList labels = (ArrayList) ($1);
4616 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
4617 | DEFAULT_COLON { $$ = new SwitchLabel (null, (Location) $1); }
4620 1523, GetLocation ($1),
4621 "The keyword case or default must precede code in switch block");
4633 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
4635 Location l = (Location) $1;
4636 $$ = new While ((Expression) $3, (Statement) $5, l);
4641 : DO embedded_statement
4642 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
4644 Location l = (Location) $1;
4646 $$ = new Do ((Statement) $2, (Expression) $5, l);
4652 opt_for_initializer SEMICOLON
4654 Location l = lexer.Location;
4656 Block assign_block = current_block;
4658 if ($3 is DictionaryEntry){
4659 DictionaryEntry de = (DictionaryEntry) $3;
4661 Expression type = (Expression) de.Key;
4662 ArrayList var_declarators = (ArrayList) de.Value;
4664 foreach (VariableDeclaration decl in var_declarators){
4668 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4672 Expression expr = decl.expression_or_array_initializer;
4674 LocalVariableReference var;
4675 var = new LocalVariableReference (assign_block, decl.identifier, l);
4678 Assign a = new SimpleAssign (var, expr, decl.Location);
4680 assign_block.AddStatement (new StatementExpression (a));
4684 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4685 // This can be referred to as $5 below.
4691 opt_for_condition SEMICOLON
4692 opt_for_iterator CLOSE_PARENS
4695 Location l = (Location) $1;
4697 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4699 current_block.AddStatement (f);
4701 $$ = end_block (lexer.Location);
4706 : /* empty */ { $$ = EmptyStatement.Value; }
4711 : local_variable_declaration
4712 | statement_expression_list
4716 : /* empty */ { $$ = null; }
4717 | boolean_expression
4721 : /* empty */ { $$ = EmptyStatement.Value; }
4726 : statement_expression_list
4729 statement_expression_list
4730 : statement_expression
4732 // CHANGE: was `null'
4733 Statement s = (Statement) $1;
4734 Block b = new Block (current_block, s.loc, lexer.Location);
4739 | statement_expression_list COMMA statement_expression
4741 Block b = (Block) $1;
4743 b.AddStatement ((Statement) $3);
4749 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4751 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4754 | FOREACH OPEN_PARENS type IDENTIFIER IN
4755 expression CLOSE_PARENS
4757 start_block (lexer.Location);
4758 Block foreach_block = current_block;
4760 LocatedToken lt = (LocatedToken) $4;
4761 Location l = lt.Location;
4762 LocalInfo vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4764 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4766 // Get a writable reference to this read-only variable.
4768 // Note that the $$ here refers to the value of _this_ code block,
4769 // not the value of the LHS non-terminal. This can be referred to as $8 below.
4770 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
4777 LocalVariableReference v = (LocalVariableReference) $8;
4778 Location l = (Location) $1;
4781 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
4782 current_block.AddStatement (f);
4785 $$ = end_block (lexer.Location);
4791 | continue_statement
4801 $$ = new Break ((Location) $1);
4806 : CONTINUE SEMICOLON
4808 $$ = new Continue ((Location) $1);
4813 : GOTO IDENTIFIER SEMICOLON
4815 LocatedToken lt = (LocatedToken) $2;
4816 $$ = new Goto (lt.Value, lt.Location);
4818 | GOTO CASE constant_expression SEMICOLON
4820 $$ = new GotoCase ((Expression) $3, (Location) $1);
4822 | GOTO DEFAULT SEMICOLON
4824 $$ = new GotoDefault ((Location) $1);
4829 : RETURN opt_expression SEMICOLON
4831 $$ = new Return ((Expression) $2, (Location) $1);
4836 : THROW opt_expression SEMICOLON
4838 $$ = new Throw ((Expression) $2, (Location) $1);
4843 : IDENTIFIER RETURN expression SEMICOLON
4845 LocatedToken lt = (LocatedToken) $1;
4846 string s = lt.Value;
4848 Report.Error (1003, lt.Location, "; expected");
4851 if (RootContext.Version == LanguageVersion.ISO_1){
4852 Report.FeatureIsNotAvailable (lt.Location, "yield statement");
4855 current_block.Toplevel.IsIterator = true;
4856 $$ = new Yield ((Expression) $3, lt.Location);
4858 | IDENTIFIER RETURN SEMICOLON
4860 Report.Error (1627, (Location) $2, "Expression expected after yield return");
4863 | IDENTIFIER BREAK SEMICOLON
4865 LocatedToken lt = (LocatedToken) $1;
4866 string s = lt.Value;
4868 Report.Error (1003, lt.Location, "; expected");
4871 if (RootContext.Version == LanguageVersion.ISO_1){
4872 Report.FeatureIsNotAvailable (lt.Location, "yield statement");
4876 current_block.Toplevel.IsIterator = true;
4877 $$ = new YieldBreak (lt.Location);
4887 : TRY block catch_clauses
4889 $$ = new TryCatch ((Block) $2, (ArrayList) $3, (Location) $1, false);
4891 | TRY block FINALLY block
4893 $$ = new TryFinally ((Statement) $2, (Block) $4, (Location) $1);
4895 | TRY block catch_clauses FINALLY block
4897 $$ = new TryFinally (new TryCatch ((Block) $2, (ArrayList) $3, (Location) $1, true), (Block) $5, (Location) $1);
4901 Report.Error (1524, (Location) $1, "Expected catch or finally");
4909 ArrayList l = new ArrayList (4);
4914 | catch_clauses catch_clause
4916 ArrayList l = (ArrayList) $1;
4924 : /* empty */ { $$ = null; }
4929 : CATCH opt_catch_args
4931 Expression type = null;
4934 DictionaryEntry cc = (DictionaryEntry) $2;
4935 type = (Expression) cc.Key;
4936 LocatedToken lt = (LocatedToken) cc.Value;
4939 ArrayList one = new ArrayList (4);
4941 one.Add (new VariableDeclaration (lt, null));
4943 start_block (lexer.Location);
4944 current_block = declare_local_variables (type, one, lt.Location);
4948 Expression type = null;
4950 Block var_block = null;
4953 DictionaryEntry cc = (DictionaryEntry) $2;
4954 type = (Expression) cc.Key;
4955 LocatedToken lt = (LocatedToken) cc.Value;
4959 var_block = end_block (lexer.Location);
4963 $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
4968 : /* empty */ { $$ = null; }
4973 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4975 $$ = new DictionaryEntry ($2, $3);
4977 | OPEN_PARENS CLOSE_PARENS
4979 Report.Error (1015, GetLocation ($1), "A type that derives from `System.Exception', `object', or `string' expected");
4986 $$ = new Checked ((Block) $2);
4993 $$ = new Unchecked ((Block) $2);
5000 RootContext.CheckUnsafeOption ((Location) $1);
5002 $$ = new Unsafe ((Block) $3);
5008 type fixed_pointer_declarators
5011 ArrayList list = (ArrayList) $4;
5012 Expression type = (Expression) $3;
5013 Location l = (Location) $1;
5014 int top = list.Count;
5016 start_block (lexer.Location);
5018 for (int i = 0; i < top; i++){
5019 Pair p = (Pair) list [i];
5022 v = current_block.AddVariable (type, (string) p.First, l);
5026 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
5034 Location l = (Location) $1;
5036 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
5038 current_block.AddStatement (f);
5040 $$ = end_block (lexer.Location);
5044 fixed_pointer_declarators
5045 : fixed_pointer_declarator {
5046 ArrayList declarators = new ArrayList (4);
5048 declarators.Add ($1);
5051 | fixed_pointer_declarators COMMA fixed_pointer_declarator
5053 ArrayList declarators = (ArrayList) $1;
5055 declarators.Add ($3);
5060 fixed_pointer_declarator
5061 : IDENTIFIER ASSIGN expression
5063 LocatedToken lt = (LocatedToken) $1;
5064 // FIXME: keep location
5065 $$ = new Pair (lt.Value, $3);
5069 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
5075 : LOCK OPEN_PARENS expression CLOSE_PARENS
5081 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
5086 : USING OPEN_PARENS local_variable_declaration CLOSE_PARENS
5088 start_block (lexer.Location);
5089 Block assign_block = current_block;
5091 DictionaryEntry de = (DictionaryEntry) $3;
5092 Location l = (Location) $1;
5094 Expression type = (Expression) de.Key;
5095 ArrayList var_declarators = (ArrayList) de.Value;
5097 Stack vars = new Stack ();
5099 foreach (VariableDeclaration decl in var_declarators) {
5100 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
5103 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
5105 Expression expr = decl.expression_or_array_initializer;
5107 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
5110 LocalVariableReference var;
5112 // Get a writable reference to this read-only variable.
5113 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
5115 // This is so that it is not a warning on using variables
5118 vars.Push (new DictionaryEntry (var, expr));
5120 // Assign a = new SimpleAssign (var, expr, decl.Location);
5121 // assign_block.AddStatement (new StatementExpression (a));
5124 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
5125 // It can be referred to as $5 below.
5130 Statement stmt = (Statement) $6;
5131 Stack vars = (Stack) $5;
5132 Location l = (Location) $1;
5134 while (vars.Count > 0) {
5135 DictionaryEntry de = (DictionaryEntry) vars.Pop ();
5136 stmt = new Using ((Expression) de.Key, (Expression) de.Value, stmt, l);
5138 current_block.AddStatement (stmt);
5139 $$ = end_block (lexer.Location);
5141 | USING OPEN_PARENS expression CLOSE_PARENS
5143 start_block (lexer.Location);
5147 current_block.AddStatement (new UsingTemporary ((Expression) $3, (Statement) $6, (Location) $1));
5148 $$ = end_block (lexer.Location);
5158 ++lexer.query_parsing;
5162 if (--lexer.query_parsing == 1)
5163 lexer.query_parsing = 0;
5165 Linq.AQueryClause from = $1 as Linq.AQueryClause;
5167 from.Tail.Next = (Linq.AQueryClause)$3;
5170 current_block.SetEndLocation (lexer.Location);
5171 current_block = current_block.Parent;
5176 : FROM IDENTIFIER IN expression
5178 current_block = new Linq.QueryBlock (current_block, GetLocation ($1));
5179 LocatedToken lt = (LocatedToken) $2;
5181 current_block.AddVariable (Linq.ImplicitQueryParameter.ImplicitType.Instance, lt.Value, lt.Location);
5182 $$ = new Linq.QueryExpression (lt, new Linq.QueryStartClause ((Expression)$4));
5184 | FROM type IDENTIFIER IN expression
5186 current_block = new Linq.QueryBlock (current_block, GetLocation ($1));
5187 LocatedToken lt = (LocatedToken) $3;
5189 Expression type = (Expression)$2;
5190 current_block.AddVariable (type, lt.Value, lt.Location);
5191 $$ = new Linq.QueryExpression (lt, new Linq.Cast (type, (Expression)$5));
5196 : FROM IDENTIFIER IN expression
5198 LocatedToken lt = (LocatedToken) $2;
5200 current_block.AddVariable (Linq.ImplicitQueryParameter.ImplicitType.Instance,
5201 lt.Value, lt.Location);
5203 $$ = new Linq.SelectMany (lt, (Expression)$4);
5205 | FROM type IDENTIFIER IN expression
5207 LocatedToken lt = (LocatedToken) $3;
5209 Expression type = (Expression)$2;
5210 current_block.AddVariable (type, lt.Value, lt.Location);
5211 $$ = new Linq.SelectMany (lt, new Linq.Cast (type, (Expression)$5));
5216 : opt_query_body_clauses select_or_group_clause opt_query_continuation
5218 Linq.AQueryClause head = (Linq.AQueryClause)$2;
5221 head.Next = (Linq.AQueryClause)$3;
5224 Linq.AQueryClause clause = (Linq.AQueryClause)$1;
5225 clause.Tail.Next = head;
5233 select_or_group_clause
5236 $$ = new Linq.Select ((Expression)$2, GetLocation ($1));
5238 | GROUP expression BY expression
5240 $$ = new Linq.GroupBy ((Expression)$2, (Expression)$4, GetLocation ($1));
5244 opt_query_body_clauses
5246 | query_body_clauses
5251 | query_body_clauses query_body_clause
5253 ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$2;
5267 : LET IDENTIFIER ASSIGN expression
5269 LocatedToken lt = (LocatedToken) $2;
5270 current_block.AddVariable (Linq.ImplicitQueryParameter.ImplicitType.Instance,
5271 lt.Value, lt.Location);
5272 $$ = new Linq.Let (lt, (Expression)$4, GetLocation ($1));
5277 : WHERE boolean_expression
5279 $$ = new Linq.Where ((Expression)$2, GetLocation ($1));
5284 : JOIN IDENTIFIER IN expression ON expression EQUALS expression opt_join_into
5286 Location loc = GetLocation ($1);
5287 LocatedToken lt = (LocatedToken) $2;
5288 current_block.AddVariable (Linq.ImplicitQueryParameter.ImplicitType.Instance,
5289 lt.Value, lt.Location);
5292 $$ = new Linq.Join (lt, (Expression)$4, (Expression)$6,
5293 (Expression)$8, loc);
5295 LocatedToken lt_into = (LocatedToken) $9;
5296 $$ = new Linq.GroupJoin (lt, (Expression)$4, (Expression)$6,
5297 (Expression)$8, lt_into, loc);
5300 | JOIN type IDENTIFIER IN expression ON expression EQUALS expression opt_join_into
5302 Location loc = GetLocation ($1);
5303 LocatedToken lt = (LocatedToken) $3;
5304 current_block.AddVariable ((Expression)$2, lt.Value, lt.Location);
5306 Linq.Cast cast = new Linq.Cast ((Expression)$2, (Expression)$5);
5308 $$ = new Linq.Join (lt, cast, (Expression)$7,
5309 (Expression)$9, loc);
5311 LocatedToken lt_into = (LocatedToken) $10;
5312 $$ = new Linq.GroupJoin (lt, cast, (Expression)$7,
5313 (Expression)$9, lt_into, loc);
5335 | order_by COMMA orderings_then_by
5337 ((Linq.AQueryClause)$1).Next = (Linq.AQueryClause)$3;
5347 | orderings_then_by COMMA then_by
5349 ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$3;
5357 $$ = new Linq.OrderByAscending ((Expression)$1);
5359 | expression ASCENDING
5361 $$ = new Linq.OrderByAscending ((Expression)$1);
5363 | expression DESCENDING
5365 $$ = new Linq.OrderByDescending ((Expression)$1);
5372 $$ = new Linq.ThenByAscending ((Expression)$1);
5374 | expression ASCENDING
5376 $$ = new Linq.ThenByAscending ((Expression)$1);
5378 | expression DESCENDING
5380 $$ = new Linq.ThenByDescending ((Expression)$1);
5385 opt_query_continuation
5389 // query continuation block is not linked with query block but with block
5390 // before. This means each query can use same range variable names for
5391 // different identifiers.
5393 current_block.SetEndLocation (GetLocation ($1));
5394 current_block = current_block.Parent;
5395 current_block = new Linq.QueryBlock (current_block, GetLocation ($1));
5397 LocatedToken lt = (LocatedToken) $2;
5398 current_block.AddVariable (Linq.ImplicitQueryParameter.ImplicitType.Instance,
5399 lt.Value, lt.Location);
5403 $$ = new Linq.QueryExpression ((LocatedToken) $2,
5404 (Linq.AQueryClause)$4);
5409 // Support for using the compiler as an interactive parser
5411 // The INTERACTIVE_PARSER token is first sent to parse our
5412 // productions; If the result is a Statement, the parsing
5413 // is repeated, this time with INTERACTIVE_PARSE_WITH_BLOCK
5414 // to setup the blocks in advance.
5416 // This setup is here so that in the future we can add
5417 // support for other constructs (type parsing, namespaces, etc)
5418 // that do not require a block to be setup in advance
5422 : EVAL_STATEMENT_PARSER EOF
5423 | EVAL_USING_DECLARATIONS_UNIT_PARSER using_directives
5424 | EVAL_STATEMENT_PARSER {
5425 Evaluator.LoadAliases (current_namespace);
5427 push_current_class (new Class (current_namespace, current_class, new MemberName ("Class" + class_count++),
5428 Modifiers.PUBLIC, null), null);
5430 ArrayList baseclass_list = new ArrayList ();
5431 baseclass_list.Add (new TypeExpression (Evaluator.InteractiveBaseClass, lexer.Location));
5432 current_container.AddBasesForPart (current_class, baseclass_list);
5434 // (ref object retval)
5435 Parameter [] mpar = new Parameter [1];
5436 mpar [0] = new Parameter (TypeManager.system_object_expr, "$retval", Parameter.Modifier.REF, null, Location.Null);
5438 Parameters pars = new Parameters (mpar);
5439 current_local_parameters = pars;
5440 Method method = new Method (
5443 TypeManager.system_void_expr,
5444 Modifiers.PUBLIC | Modifiers.STATIC,
5445 new MemberName ("Host"),
5447 null /* attributes */);
5449 oob_stack.Push (method);
5450 ++lexer.parsing_block;
5451 start_block (lexer.Location);
5453 interactive_statement_list
5455 --lexer.parsing_block;
5456 Method method = (Method) oob_stack.Pop ();
5458 method.Block = (ToplevelBlock) end_block(lexer.Location);
5459 current_container.AddMethod (method);
5461 InteractiveResult = pop_current_class ();
5462 current_local_parameters = null;
5464 | EVAL_COMPILATION_UNIT_PARSER {
5465 Evaluator.LoadAliases (current_namespace);
5467 interactive_compilation_unit
5470 interactive_compilation_unit
5471 : outer_declarations
5472 | outer_declarations global_attributes
5479 // A class used to pass around variable declarations and constants
5481 public class VariableDeclaration {
5482 public string identifier;
5483 public Expression expression_or_array_initializer;
5484 public Location Location;
5485 public Attributes OptAttributes;
5486 public string DocComment;
5488 public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
5490 this.identifier = lt.Value;
5491 if (eoai is ArrayList) {
5492 this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
5494 this.expression_or_array_initializer = (Expression)eoai;
5496 this.Location = lt.Location;
5497 this.OptAttributes = opt_attrs;
5500 public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
5506 // A class used to hold info about an indexer declarator
5508 public class IndexerDeclaration {
5509 public FullNamedExpression type;
5510 public MemberName interface_type;
5511 public Parameters param_list;
5512 public Location location;
5514 public IndexerDeclaration (FullNamedExpression type, MemberName interface_type,
5515 Parameters param_list, Location loc)
5518 this.interface_type = interface_type;
5519 this.param_list = param_list;
5520 this.location = loc;
5525 // A class used to hold info about an operator declarator
5527 struct OperatorDeclaration {
5528 public readonly Operator.OpType optype;
5529 public readonly FullNamedExpression ret_type;
5530 public readonly Location location;
5532 public OperatorDeclaration (Operator.OpType op, FullNamedExpression ret_type, Location location)
5535 this.ret_type = ret_type;
5536 this.location = location;
5540 void Error_ExpectingTypeName (Expression expr)
5542 if (expr is Invocation){
5543 Report.Error (1002, expr.Location, "Expecting `;'");
5545 expr.Error_InvalidExpressionStatement ();
5549 static void Error_ParameterModifierNotValid (string modifier, Location loc)
5551 Report.Error (631, loc, "The parameter modifier `{0}' is not valid in this context",
5555 static void Error_DuplicateParameterModifier (Location loc, Parameter.Modifier mod)
5557 Report.Error (1107, loc, "Duplicate parameter modifier `{0}'",
5558 Parameter.GetModifierSignature (mod));
5561 static void Error_TypeExpected (Location loc)
5563 Report.Error (1031, loc, "Type expected");
5566 void push_current_class (TypeContainer tc, object partial_token)
5568 if (RootContext.EvalMode){
5569 tc.ModFlags = (tc.ModFlags & ~(Modifiers.PRIVATE|Modifiers.INTERNAL)) | Modifiers.PUBLIC;
5570 undo.AddTypeContainer (current_container, tc);
5573 if (partial_token != null)
5574 current_container = current_container.AddPartial (tc);
5576 current_container = current_container.AddTypeContainer (tc);
5582 DeclSpace pop_current_class ()
5584 DeclSpace retval = current_class;
5586 current_class = current_class.Parent;
5587 current_container = current_class.PartialContainer;
5593 // Given the @class_name name, it creates a fully qualified name
5594 // based on the containing declaration space
5597 MakeName (MemberName class_name)
5599 Namespace ns = current_namespace.NS;
5601 if (current_container.Name.Length == 0){
5602 if (ns.Name.Length != 0)
5603 return new MemberName (ns.MemberName, class_name);
5607 return new MemberName (current_container.MemberName, class_name);
5611 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
5613 Block implicit_block;
5614 ArrayList inits = null;
5617 // If we are doing interactive editing, we want variable declarations
5618 // that are in the top block to be added instead to the class as
5621 if (RootContext.StatementMode){
5624 for (Block b = current_block; b != null; b = b.Parent){
5625 if (b is ExplicitBlock && !(b is ToplevelBlock)){
5626 // There has been an explicit block, we cant add to the class
5633 // We can use "current_block" since we know there are no explicit blocks
5635 foreach (VariableDeclaration decl in variable_declarators){
5636 // We can not use the super-handy f.Initializer, because
5637 // multiple lines would force code to be executed out of sync
5638 if (decl.expression_or_array_initializer != null){
5639 string id = "$" + decl.identifier;
5640 current_block.AddVariable (type, id, decl.Location);
5642 LocalVariableReference var;
5643 var = new LocalVariableReferenceWithClassSideEffect (current_container, decl.identifier, current_block, id, decl.Location);
5644 Assign assign = new SimpleAssign (var, decl.expression_or_array_initializer, decl.Location);
5645 current_block.AddStatement (new StatementExpression (assign));
5646 assign = new SimpleAssign (new SimpleName (decl.identifier, decl.Location), var);
5647 current_block.AddStatement (new StatementExpression (assign));
5649 Field f = new Field (current_container, (FullNamedExpression) type, Modifiers.PUBLIC | Modifiers.STATIC, decl.identifier, null, loc);
5650 current_container.AddField (f);
5652 // Register the field to be visible later as a global variable
5653 Evaluator.QueueField (f);
5657 return current_block;
5662 // We use the `Used' property to check whether statements
5663 // have been added to the current block. If so, we need
5664 // to create another block to contain the new declaration
5665 // otherwise, as an optimization, we use the same block to
5666 // add the declaration.
5668 // FIXME: A further optimization is to check if the statements
5669 // that were added were added as part of the initialization
5670 // below. In which case, no other statements have been executed
5671 // and we might be able to reduce the number of blocks for
5672 // situations like this:
5674 // int j = 1; int k = j + 1;
5676 if (current_block.Used)
5677 implicit_block = new Block (current_block, loc, lexer.Location);
5679 implicit_block = current_block;
5681 foreach (VariableDeclaration decl in variable_declarators){
5683 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
5684 if (decl.expression_or_array_initializer != null){
5686 inits = new ArrayList (4);
5693 return implicit_block;
5695 foreach (VariableDeclaration decl in inits){
5697 Expression expr = decl.expression_or_array_initializer;
5699 LocalVariableReference var;
5700 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
5702 assign = new SimpleAssign (var, expr, decl.Location);
5704 implicit_block.AddStatement (new StatementExpression (assign));
5707 return implicit_block;
5710 Block declare_local_constants (Expression type, ArrayList declarators)
5712 Block implicit_block;
5714 if (current_block.Used)
5715 implicit_block = new Block (current_block);
5717 implicit_block = current_block;
5719 foreach (VariableDeclaration decl in declarators){
5720 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
5723 return implicit_block;
5726 string CheckAttributeTarget (string a, Location l)
5729 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
5733 Report.Warning (658, 1, l,
5734 "`{0}' is invalid attribute target. All attributes in this attribute section will be ignored", a);
5735 return string.Empty;
5738 static bool IsUnaryOperator (Operator.OpType op)
5742 case Operator.OpType.LogicalNot:
5743 case Operator.OpType.OnesComplement:
5744 case Operator.OpType.Increment:
5745 case Operator.OpType.Decrement:
5746 case Operator.OpType.True:
5747 case Operator.OpType.False:
5748 case Operator.OpType.UnaryPlus:
5749 case Operator.OpType.UnaryNegation:
5755 void syntax_error (Location l, string msg)
5757 Report.Error (1003, l, "Syntax error, " + msg);
5760 void note (string s)
5762 // Used to put annotations
5767 public Tokenizer Lexer {
5773 static CSharpParser ()
5775 oob_stack = new Stack ();
5778 public CSharpParser (SeekableStreamReader reader, CompilationUnit file)
5780 if (RootContext.EvalMode)
5784 current_namespace = new NamespaceEntry (null, file, null);
5785 current_class = current_namespace.SlaveDeclSpace;
5786 current_container = current_class.PartialContainer; // == RootContest.ToplevelTypes
5788 lexer = new Tokenizer (reader, file);
5791 public void parse ()
5793 eof_token = Token.EOF;
5796 if (yacc_verbose_flag > 1)
5797 yyparse (lexer, new yydebug.yyDebugSimple ());
5800 } catch (Exception e){
5801 if (e is yyParser.yyUnexpectedEof)
5802 UnexpectedEOF = true;
5803 else if (yacc_verbose_flag > 0)
5804 Console.WriteLine (e);
5805 if (e is yyParser.yyException)
5806 Report.Error (-25, lexer.Location, "Parsing error");
5808 Report.Error (-32, lexer.Location, "Internal compiler error during parsing, Run with -v for details");
5810 Tokenizer tokenizer = lexer as Tokenizer;
5811 tokenizer.cleanup ();
5813 if (RootContext.ToplevelTypes.NamespaceEntry != null)
5814 throw new InternalErrorException ("who set it?");
5817 static void CheckToken (int error, int yyToken, string msg, Location loc)
5819 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
5820 Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, GetTokenName (yyToken));
5822 Report.Error (error, loc, msg);
5825 void CheckIdentifierToken (int yyToken, Location loc)
5827 CheckToken (1041, yyToken, "Identifier expected", loc);
5830 string ConsumeStoredComment ()
5832 string s = tmpComment;
5834 Lexer.doc_state = XmlCommentState.Allowed;
5838 Location GetLocation (object obj)
5840 if (obj is MemberCore)
5841 return ((MemberCore) obj).Location;
5842 if (obj is MemberName)
5843 return ((MemberName) obj).Location;
5844 if (obj is LocatedToken)
5845 return ((LocatedToken) obj).Location;
5846 if (obj is Location)
5847 return (Location) obj;
5848 return lexer.Location;
5851 void start_block (Location loc)
5853 if (current_block == null || parsing_anonymous_method) {
5854 current_block = new ToplevelBlock (current_block, current_local_parameters, current_generic_method, loc);
5855 parsing_anonymous_method = false;
5857 current_block = new ExplicitBlock (current_block, loc, Location.Null);
5862 end_block (Location loc)
5864 Block retval = current_block.Explicit;
5865 retval.SetEndLocation (loc);
5866 current_block = retval.Parent;
5871 start_anonymous (bool lambda, Parameters parameters, Location loc)
5873 oob_stack.Push (current_anonymous_method);
5874 oob_stack.Push (current_local_parameters);
5876 current_local_parameters = parameters;
5878 current_anonymous_method = lambda
5879 ? new LambdaExpression (parameters, loc)
5880 : new AnonymousMethodExpression (parameters, loc);
5882 // Force the next block to be created as a ToplevelBlock
5883 parsing_anonymous_method = true;
5887 * Completes the anonymous method processing, if lambda_expr is null, this
5888 * means that we have a Statement instead of an Expression embedded
5890 AnonymousMethodExpression end_anonymous (ToplevelBlock anon_block, Location loc)
5892 AnonymousMethodExpression retval;
5894 if (RootContext.Version == LanguageVersion.ISO_1){
5895 Report.FeatureIsNotAvailable (loc, "anonymous methods");
5898 current_anonymous_method.Block = anon_block;
5899 retval = current_anonymous_method;
5902 current_local_parameters = (Parameters) oob_stack.Pop ();
5903 current_anonymous_method = (AnonymousMethodExpression) oob_stack.Pop ();
5908 public NamespaceEntry CurrentNamespace {
5910 return current_namespace;
5914 public string GetExpecting ()
5916 int [] tokens = yyExpectingTokens (yyExpectingState);
5917 if (tokens.Length == 1)
5918 return "`" + GetTokenName (tokens [0]) + "'";
5920 StringBuilder sb = new StringBuilder ();
5921 ArrayList names = new ArrayList ();
5922 for (int i = 0; i < tokens.Length; i++){
5923 string name = GetTokenName (tokens [i]);
5924 if (name == "<internal>")
5926 if (names.Contains (name))
5933 int count = names.Count;
5934 for (int i = 0; i < count; i++){
5935 bool last = i + 1 == count;
5939 sb.Append (names [i]);
5940 sb.Append (last ? "'" : "', ");
5942 return sb.ToString ();
5945 static public string GetTokenName (int token)
5948 case Token.ABSTRACT:
5954 case Token.ASSEMBLY:
5970 case Token.CONTINUE:
5974 case Token.DELEGATE:
5984 case Token.EXPLICIT:
6002 case Token.IMPLICIT:
6006 case Token.INTERFACE:
6008 case Token.INTERNAL:
6014 case Token.NAMESPACE:
6022 case Token.OPERATOR:
6026 case Token.OVERRIDE:
6032 case Token.PROTECTED:
6036 case Token.READONLY:
6048 case Token.STACKALLOC:
6049 return "stackalloc";
6066 case Token.UNCHECKED:
6074 case Token.VOLATILE:
6104 case Token.ASCENDING:
6106 case Token.DESCENDING:
6107 return "descending";
6114 case Token.OPEN_BRACE:
6116 case Token.CLOSE_BRACE:
6118 case Token.OPEN_BRACKET:
6120 case Token.CLOSE_BRACKET:
6122 case Token.DEFAULT_OPEN_PARENS:
6123 case Token.OPEN_PARENS_LAMBDA:
6124 case Token.OPEN_PARENS:
6126 case Token.CLOSE_PARENS_CAST:
6127 case Token.CLOSE_PARENS_NO_CAST:
6128 case Token.CLOSE_PARENS_OPEN_PARENS:
6129 case Token.CLOSE_PARENS_MINUS:
6130 case Token.CLOSE_PARENS:
6136 case Token.DEFAULT_COLON:
6139 case Token.SEMICOLON:
6150 case Token.BITWISE_AND:
6151 case Token.BITWISE_OR:
6158 case Token.OP_SHIFT_LEFT:
6159 case Token.OP_SHIFT_RIGHT:
6167 case Token.OP_COALESCING:
6168 return "<operator>";
6170 case Token.OP_MULT_ASSIGN:
6171 case Token.OP_DIV_ASSIGN:
6172 case Token.OP_MOD_ASSIGN:
6173 case Token.OP_ADD_ASSIGN:
6174 case Token.OP_SUB_ASSIGN:
6175 case Token.OP_SHIFT_LEFT_ASSIGN:
6176 case Token.OP_SHIFT_RIGHT_ASSIGN:
6177 case Token.OP_AND_ASSIGN:
6178 case Token.OP_XOR_ASSIGN:
6179 case Token.OP_OR_ASSIGN:
6180 return "<operator>";
6201 case Token.OP_GENERICS_LT:
6202 case Token.GENERIC_DIMENSION:
6204 case Token.OP_GENERICS_GT:
6207 case Token.INTERR_NULLABLE:
6209 case Token.DOUBLE_COLON:
6211 case Token.LITERAL_FLOAT:
6212 case Token.LITERAL_INTEGER:
6213 case Token.LITERAL_DOUBLE:
6214 case Token.LITERAL_DECIMAL:
6215 case Token.LITERAL_CHARACTER:
6216 case Token.LITERAL_STRING:
6218 case Token.IDENTIFIER:
6219 return "identifier";
6221 // All of these are internal.
6224 case Token.FIRST_KEYWORD:
6226 case Token.EVAL_COMPILATION_UNIT_PARSER:
6227 case Token.EVAL_USING_DECLARATIONS_UNIT_PARSER:
6228 case Token.EVAL_STATEMENT_PARSER:
6230 case Token.QUERY_LAST_TOKEN:
6231 case Token.QUERY_FIRST_TOKEN:
6232 case Token.LAST_KEYWORD:
6233 return "<internal>";
6235 // A bit more robust.
6237 return yyNames [token];