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.
29 using System.Collections;
34 public class CSharpParser {
35 NamespaceEntry current_namespace;
36 TypeContainer current_container;
37 DeclSpace current_class;
39 IAnonymousHost anonymous_host;
42 /// Current block is used to add statements as we find
47 Delegate current_delegate;
49 GenericMethod current_generic_method;
50 AnonymousMethodExpression current_anonymous_method;
53 /// This is used by the unary_expression code to resolve
54 /// a name against a parameter.
56 Parameters current_local_parameters;
59 /// Using during property parsing to describe the implicit
60 /// value parameter that is passed to the "set" and "get"accesor
61 /// methods (properties and indexers).
63 Expression implicit_value_parameter_type;
64 Parameters indexer_parameters;
67 /// Hack to help create non-typed array initializer
69 public static Expression current_array_type;
70 Expression pushed_current_array_type;
73 /// Used to determine if we are parsing the get/set pair
74 /// of an indexer or a property
78 bool parsing_anonymous_method;
81 /// An out-of-band stack.
83 static Stack oob_stack;
90 static public int yacc_verbose_flag;
92 // Name of the file we are parsing
101 /// Temporary Xml documentation cache.
102 /// For enum types, we need one more temporary store.
105 string enumTypeComment;
107 /// Current attribute target
108 string current_attr_target;
110 /// assembly and module attribute definitions are enabled
111 bool global_attrs_enabled = true;
112 bool has_get, has_set;
117 %token NONE /* This token is never returned by our lexer */
118 %token ERROR // This is used not by the parser, but by the tokenizer.
122 *These are the C# keywords
209 %token QUERY_FIRST_TOKEN
222 %token QUERY_LAST_TOKEN
224 /* C# keywords which are not really keywords */
230 /* C# single character operators/punctuation. */
231 %token OPEN_BRACE "{"
232 %token CLOSE_BRACE "}"
233 %token OPEN_BRACKET "["
234 %token CLOSE_BRACKET "]"
235 %token OPEN_PARENS "("
236 %token CLOSE_PARENS ")"
248 %token OP_GENERICS_LT "<"
250 %token OP_GENERICS_GT ">"
251 %token BITWISE_AND "&"
252 %token BITWISE_OR "|"
259 /* C# multi-character operators. */
260 %token DOUBLE_COLON "::"
263 %token OP_SHIFT_LEFT "<<"
264 %token OP_SHIFT_RIGHT ">>"
271 %token OP_MULT_ASSIGN "*="
272 %token OP_DIV_ASSIGN "/="
273 %token OP_MOD_ASSIGN "%="
274 %token OP_ADD_ASSIGN "+="
275 %token OP_SUB_ASSIGN "-="
276 %token OP_SHIFT_LEFT_ASSIGN "<<="
277 %token OP_SHIFT_RIGHT_ASSIGN ">>="
278 %token OP_AND_ASSIGN "&="
279 %token OP_XOR_ASSIGN "^="
280 %token OP_OR_ASSIGN "|="
282 %token OP_COALESCING "??"
285 %token LITERAL_INTEGER "int literal"
286 %token LITERAL_FLOAT "float literal"
287 %token LITERAL_DOUBLE "double literal"
288 %token LITERAL_DECIMAL "decimal literal"
289 %token LITERAL_CHARACTER "character literal"
290 %token LITERAL_STRING "string literal"
293 %token OPEN_PARENS_LAMBDA
294 %token CLOSE_PARENS_CAST
295 %token CLOSE_PARENS_NO_CAST
296 %token CLOSE_PARENS_OPEN_PARENS
297 %token CLOSE_PARENS_MINUS
298 %token DEFAULT_OPEN_PARENS
299 %token GENERIC_DIMENSION
302 /* Add precedence rules to solve dangling else s/r conflict */
311 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
313 %left STAR DIV PERCENT
314 %right BANG CARRET UMINUS
315 %nonassoc OP_INC OP_DEC
317 %left OPEN_BRACKET OPEN_BRACE
321 %start compilation_unit
325 : outer_declarations opt_EOF
326 | outer_declarations global_attributes opt_EOF
327 | global_attributes opt_EOF
328 | opt_EOF /* allow empty files */
334 Lexer.check_incorrect_doc_comment ();
338 Lexer.check_incorrect_doc_comment ();
344 | outer_declarations outer_declaration
348 : extern_alias_directive
350 | namespace_member_declaration
353 extern_alias_directives
354 : extern_alias_directive
355 | extern_alias_directives extern_alias_directive;
357 extern_alias_directive
358 : EXTERN IDENTIFIER IDENTIFIER SEMICOLON
360 LocatedToken lt = (LocatedToken) $2;
363 Report.Error (1003, lt.Location, "'alias' expected");
364 } else if (RootContext.Version == LanguageVersion.ISO_1) {
365 Report.FeatureIsNotISO1 (lt.Location, "external alias");
367 lt = (LocatedToken) $3;
368 current_namespace.UsingExternalAlias (lt.Value, lt.Location);
375 | using_directives using_directive
379 : using_alias_directive
381 if (RootContext.Documentation != null)
382 Lexer.doc_state = XmlCommentState.Allowed;
384 | using_namespace_directive
386 if (RootContext.Documentation != null)
387 Lexer.doc_state = XmlCommentState.Allowed;
391 using_alias_directive
392 : USING IDENTIFIER ASSIGN
393 namespace_or_type_name SEMICOLON
395 LocatedToken lt = (LocatedToken) $2;
396 current_namespace.UsingAlias (lt.Value, (MemberName) $4, (Location) $1);
399 CheckIdentifierToken (yyToken, GetLocation ($2));
403 using_namespace_directive
404 : USING namespace_name SEMICOLON
406 current_namespace.Using ((MemberName) $2, (Location) $1);
411 // Strictly speaking, namespaces don't have attributes but
412 // we parse global attributes along with namespace declarations and then
415 namespace_declaration
416 : opt_attributes NAMESPACE namespace_or_type_name
418 MemberName name = (MemberName) $3;
421 Report.Error(1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
424 if (name.TypeArguments != null)
425 syntax_error (lexer.Location, "namespace name expected");
427 current_namespace = new NamespaceEntry (
428 current_namespace, file, name.GetName ());
429 current_class = current_namespace.SlaveDeclSpace;
430 current_container = current_class.PartialContainer;
432 namespace_body opt_semicolon
434 current_namespace = current_namespace.Parent;
435 current_class = current_namespace.SlaveDeclSpace;
436 current_container = current_class.PartialContainer;
451 : namespace_or_type_name {
452 MemberName name = (MemberName) $1;
454 if (name.TypeArguments != null)
455 syntax_error (lexer.Location, "namespace name expected");
464 if (RootContext.Documentation != null)
465 Lexer.doc_state = XmlCommentState.Allowed;
471 : opt_extern_alias_directives
473 opt_namespace_member_declarations
477 Report.Error (1518, lexer.Location, "Expected `class', `delegate', `enum', `interface', or `struct'");
480 | opt_extern_alias_directives
482 opt_namespace_member_declarations
485 Report.Error (1513, lexer.Location, "} expected");
494 opt_extern_alias_directives
496 | extern_alias_directives
499 opt_namespace_member_declarations
501 | namespace_member_declarations
504 namespace_member_declarations
505 : namespace_member_declaration
506 | namespace_member_declarations namespace_member_declaration
509 namespace_member_declaration
513 DeclSpace ds = (DeclSpace)$1;
515 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
516 Report.Error (1527, ds.Location,
517 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
520 current_namespace.DeclarationFound = true;
522 | namespace_declaration {
523 current_namespace.DeclarationFound = true;
526 | field_declaration {
527 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
529 | method_declaration {
530 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
537 | interface_declaration
539 | delegate_declaration
541 // Enable this when we have handled all errors, because this acts as a generic fallback
544 // Console.WriteLine ("Token=" + yyToken);
545 // Report.Error (1518, GetLocation ($1), "Expected class, struct, interface, enum or delegate");
557 Attributes attrs = (Attributes)$1;
558 if (global_attrs_enabled) {
559 CodeGen.Assembly.AddAttributes (attrs.Attrs);
561 foreach (Attribute a in attrs.Attrs) {
562 Report.Error (1730, a.Location, "Assembly and module attributes must precede all other elements except using clauses and extern alias declarations");
573 global_attrs_enabled = false;
578 global_attrs_enabled = false;
587 if (current_attr_target != String.Empty) {
588 ArrayList sect = (ArrayList) $1;
590 if (global_attrs_enabled) {
591 if (current_attr_target == "module") {
592 CodeGen.Module.AddAttributes (sect);
594 } else if (current_attr_target != null && current_attr_target.Length > 0) {
595 CodeGen.Assembly.AddAttributes (sect);
598 $$ = new Attributes (sect);
601 if (RootContext.Documentation != null) {
602 Lexer.check_incorrect_doc_comment ();
604 XmlCommentState.Allowed;
608 $$ = new Attributes (sect);
613 current_attr_target = null;
615 | attribute_sections attribute_section
617 if (current_attr_target != String.Empty) {
618 Attributes attrs = $1 as Attributes;
619 ArrayList sect = (ArrayList) $2;
621 if (global_attrs_enabled) {
622 if (current_attr_target == "module") {
623 CodeGen.Module.AddAttributes (sect);
625 } else if (current_attr_target == "assembly") {
626 CodeGen.Assembly.AddAttributes (sect);
630 attrs = new Attributes (sect);
632 attrs.AddAttributes (sect);
636 attrs = new Attributes (sect);
638 attrs.AddAttributes (sect);
644 current_attr_target = null;
649 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
653 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
659 attribute_target_specifier
660 : attribute_target COLON
662 current_attr_target = (string)$1;
670 LocatedToken lt = (LocatedToken) $1;
671 $$ = CheckAttributeTarget (lt.Value, lt.Location);
673 | EVENT { $$ = "event"; }
674 | RETURN { $$ = "return"; }
677 string name = yyNames [yyToken].ToLower ();
678 $$ = CheckAttributeTarget (name, GetLocation ($1));
685 ArrayList attrs = new ArrayList (4);
691 | attribute_list COMMA attribute
693 ArrayList attrs = (ArrayList) $1;
701 : attribute_name opt_attribute_arguments
703 MemberName mname = (MemberName) $1;
704 if (mname.IsGeneric) {
705 Report.Error (404, lexer.Location,
706 "'<' unexpected: attributes cannot be generic");
709 object [] arguments = (object []) $2;
710 MemberName left = mname.Left;
711 string identifier = mname.Name;
713 Expression left_expr = left == null ? null : left.GetTypeExpression ();
715 if (current_attr_target == String.Empty)
717 else if (global_attrs_enabled && (current_attr_target == "assembly" || current_attr_target == "module"))
718 // FIXME: supply "nameEscaped" parameter here.
719 $$ = new GlobalAttribute (current_namespace, current_attr_target,
720 left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
722 $$ = new Attribute (current_attr_target, left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
727 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
730 opt_attribute_arguments
731 : /* empty */ { $$ = null; }
732 | OPEN_PARENS attribute_arguments CLOSE_PARENS
740 : opt_positional_argument_list
745 $$ = new object [] { $1, null };
748 | positional_argument_list COMMA named_argument_list
750 $$ = new object[] { $1, $3 };
752 | named_argument_list
754 $$ = new object [] { null, $1 };
759 opt_positional_argument_list
760 : /* empty */ { $$ = null; }
761 | positional_argument_list
764 positional_argument_list
767 ArrayList args = new ArrayList (4);
768 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
772 | positional_argument_list COMMA expression
774 ArrayList args = (ArrayList) $1;
775 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
784 ArrayList args = new ArrayList (4);
789 | named_argument_list COMMA named_argument
791 ArrayList args = (ArrayList) $1;
796 | named_argument_list COMMA expression
798 Report.Error (1016, ((Expression) $3).Location, "Named attribute argument expected");
804 : IDENTIFIER ASSIGN expression
806 // FIXME: keep location
807 $$ = new DictionaryEntry (
808 ((LocatedToken) $1).Value,
809 new Argument ((Expression) $3, Argument.AType.Expression));
815 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
818 opt_class_member_declarations
820 | class_member_declarations
823 class_member_declarations
824 : class_member_declaration
825 | class_member_declarations
826 class_member_declaration
829 class_member_declaration
830 : constant_declaration // done
831 | field_declaration // done
832 | method_declaration // done
833 | property_declaration // done
834 | event_declaration // done
835 | indexer_declaration // done
836 | operator_declaration // done
837 | constructor_declaration // done
838 | destructor_declaration // done
848 lexer.ConstraintsParsing = true;
852 MemberName name = MakeName ((MemberName) $6);
853 push_current_class (new Struct (
854 current_namespace, current_class, name, (int) $2,
855 (Attributes) $1), $3);
858 opt_type_parameter_constraints_clauses
860 lexer.ConstraintsParsing = false;
863 current_container.AddBasesForPart (current_class, (ArrayList) $8);
865 current_class.SetParameterInfo ((ArrayList) $9);
867 if (RootContext.Documentation != null)
868 current_container.DocComment = Lexer.consume_doc_comment ();
872 if (RootContext.Documentation != null)
873 Lexer.doc_state = XmlCommentState.Allowed;
877 $$ = pop_current_class ();
879 | opt_attributes opt_modifiers opt_partial STRUCT error {
880 CheckIdentifierToken (yyToken, GetLocation ($5));
887 if (RootContext.Documentation != null)
888 Lexer.doc_state = XmlCommentState.Allowed;
890 opt_struct_member_declarations CLOSE_BRACE
893 opt_struct_member_declarations
895 | struct_member_declarations
898 struct_member_declarations
899 : struct_member_declaration
900 | struct_member_declarations struct_member_declaration
903 struct_member_declaration
904 : constant_declaration
907 | property_declaration
909 | indexer_declaration
910 | operator_declaration
911 | constructor_declaration
915 * This is only included so we can flag error 575:
916 * destructors only allowed on class types
918 | destructor_declaration
929 int modflags = (int) $2;
930 foreach (VariableDeclaration constant in (ArrayList) $5){
931 Location l = constant.Location;
932 if ((modflags & Modifiers.STATIC) != 0) {
933 Report.Error (504, l, "The constant `{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
937 Const c = new Const (
938 current_class, (Expression) $4, (string) constant.identifier,
939 (Expression) constant.expression_or_array_initializer, modflags,
942 if (RootContext.Documentation != null) {
943 c.DocComment = Lexer.consume_doc_comment ();
944 Lexer.doc_state = XmlCommentState.Allowed;
946 current_container.AddConstant (c);
952 : constant_declarator
954 ArrayList constants = new ArrayList (4);
959 | constant_declarators COMMA constant_declarator
962 ArrayList constants = (ArrayList) $1;
969 : IDENTIFIER ASSIGN constant_expression
971 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
975 // A const field requires a value to be provided
976 Report.Error (145, ((LocatedToken) $1).Location, "A const field requires a value to be provided");
988 Expression type = (Expression) $3;
991 current_array_type = null;
993 foreach (VariableDeclaration var in (ArrayList) $4){
994 Field field = new Field (current_class, type, mod, var.identifier,
995 (Attributes) $1, var.Location);
997 field.Initializer = var.expression_or_array_initializer;
999 if (RootContext.Documentation != null) {
1000 field.DocComment = Lexer.consume_doc_comment ();
1001 Lexer.doc_state = XmlCommentState.Allowed;
1003 current_container.AddField (field);
1004 $$ = field; // FIXME: might be better if it points to the top item
1011 fixed_variable_declarators
1014 Expression type = (Expression) $4;
1017 current_array_type = null;
1019 foreach (VariableDeclaration var in (ArrayList) $5) {
1020 FixedField field = new FixedField (current_class, type, mod, var.identifier,
1021 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
1023 if (RootContext.Documentation != null) {
1024 field.DocComment = Lexer.consume_doc_comment ();
1025 Lexer.doc_state = XmlCommentState.Allowed;
1027 current_container.AddField (field);
1028 $$ = field; // FIXME: might be better if it points to the top item
1037 Report.Error (1641, GetLocation ($4), "A fixed size buffer field must have the array size specifier after the field name");
1042 variable_declarators
1044 current_array_type = null;
1045 Report.Error (670, (Location) $3, "Fields cannot have void type");
1049 fixed_variable_declarators
1050 : fixed_variable_declarator
1052 ArrayList decl = new ArrayList (2);
1056 | fixed_variable_declarators COMMA fixed_variable_declarator
1058 ArrayList decls = (ArrayList) $1;
1064 fixed_variable_declarator
1065 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
1067 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1069 | IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1071 Report.Error (443, lexer.Location, "Value or constant expected");
1072 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1076 variable_declarators
1077 : variable_declarator
1079 ArrayList decl = new ArrayList (4);
1084 | variable_declarators COMMA variable_declarator
1086 ArrayList decls = (ArrayList) $1;
1093 : IDENTIFIER ASSIGN variable_initializer
1095 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1099 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1101 | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
1103 Report.Error (650, ((LocatedToken) $1).Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
1104 "To declare a fixed size buffer field, use the fixed keyword before the field type");
1109 variable_initializer
1118 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
1120 $$ = new StackAlloc ((Expression) $2, (Expression) $4, (Location) $1);
1124 $$ = new ArglistAccess ((Location) $1);
1128 Report.Error (1575, (Location) $1, "A stackalloc expression requires [] after type");
1135 anonymous_host = (IAnonymousHost) $1;
1136 if (RootContext.Documentation != null)
1137 Lexer.doc_state = XmlCommentState.NotAllowed;
1141 Method method = (Method) $1;
1142 method.Block = (ToplevelBlock) $3;
1143 current_container.AddMethod (method);
1145 anonymous_host = null;
1146 current_generic_method = null;
1147 current_local_parameters = null;
1149 if (RootContext.Documentation != null)
1150 Lexer.doc_state = XmlCommentState.Allowed;
1163 Report.Error (1585, lexer.Location,
1164 "Member modifier `{0}' must precede the member type and name",
1165 Modifiers.Name (i));
1174 // This rule is used to handle the cases where OPEN_PARENS_LAMBDA
1175 // is returned (type followed by an identifier), these are the
1176 // declarations (methods, overloads, constructors, etc) and a handful
1177 // of expressions ("using", "fixed") or "catch".
1181 | OPEN_PARENS_LAMBDA
1188 open_parens opt_formal_parameter_list CLOSE_PARENS
1190 lexer.ConstraintsParsing = true;
1192 opt_type_parameter_constraints_clauses
1194 lexer.ConstraintsParsing = false;
1196 MemberName name = (MemberName) $4;
1198 if ($9 != null && name.TypeArguments == null)
1199 Report.Error (80, lexer.Location,
1200 "Constraints are not allowed on non-generic declarations");
1204 GenericMethod generic = null;
1205 if (name.TypeArguments != null) {
1206 generic = new GenericMethod (current_namespace, current_class, name,
1207 (Expression) $3, (Parameters) $6);
1209 generic.SetParameterInfo ((ArrayList) $9);
1212 method = new Method (current_class, generic, (Expression) $3, (int) $2, false,
1213 name, (Parameters) $6, (Attributes) $1);
1215 anonymous_host = method;
1216 current_local_parameters = (Parameters) $6;
1217 current_generic_method = generic;
1219 if (RootContext.Documentation != null)
1220 method.DocComment = Lexer.consume_doc_comment ();
1227 open_parens opt_formal_parameter_list CLOSE_PARENS
1229 lexer.ConstraintsParsing = true;
1231 opt_type_parameter_constraints_clauses
1233 lexer.ConstraintsParsing = false;
1235 MemberName name = (MemberName) $4;
1237 if ($9 != null && name.TypeArguments == null)
1238 Report.Error (80, lexer.Location,
1239 "Constraints are not allowed on non-generic declarations");
1242 GenericMethod generic = null;
1243 if (name.TypeArguments != null) {
1244 generic = new GenericMethod (current_namespace, current_class, name,
1245 TypeManager.system_void_expr, (Parameters) $6);
1247 generic.SetParameterInfo ((ArrayList) $9);
1250 method = new Method (current_class, generic, TypeManager.system_void_expr,
1251 (int) $2, false, name, (Parameters) $6, (Attributes) $1);
1253 anonymous_host = method;
1254 current_local_parameters = (Parameters) $6;
1255 current_generic_method = generic;
1257 if (RootContext.Documentation != null)
1258 method.DocComment = Lexer.consume_doc_comment ();
1265 modifiers member_name open_parens opt_formal_parameter_list CLOSE_PARENS
1267 MemberName name = (MemberName) $5;
1268 Report.Error (1585, name.Location,
1269 "Member modifier `{0}' must precede the member type and name", Modifiers.Name ((int) $4));
1271 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1272 0, false, name, (Parameters) $6, (Attributes) $1);
1274 current_local_parameters = (Parameters) $6;
1276 if (RootContext.Documentation != null)
1277 method.DocComment = Lexer.consume_doc_comment ();
1285 | SEMICOLON { $$ = null; }
1288 opt_formal_parameter_list
1289 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1290 | formal_parameter_list
1293 formal_parameter_list
1296 ArrayList pars_list = (ArrayList) $1;
1298 Parameter [] pars = new Parameter [pars_list.Count];
1299 pars_list.CopyTo (pars);
1301 $$ = new Parameters (pars);
1303 | fixed_parameters COMMA parameter_array
1305 ArrayList pars_list = (ArrayList) $1;
1308 Parameter [] pars = new Parameter [pars_list.Count];
1309 pars_list.CopyTo (pars);
1311 $$ = new Parameters (pars);
1313 | fixed_parameters COMMA ARGLIST
1315 ArrayList pars_list = (ArrayList) $1;
1316 //pars_list.Add (new ArglistParameter (GetLocation ($3)));
1318 Parameter [] pars = new Parameter [pars_list.Count];
1319 pars_list.CopyTo (pars);
1321 $$ = new Parameters (pars, true);
1323 | parameter_array COMMA error
1326 Report.Error (231, ((Parameter) $1).Location, "A params parameter must be the last parameter in a formal parameter list");
1329 | fixed_parameters COMMA parameter_array COMMA error
1332 Report.Error (231, ((Parameter) $3).Location, "A params parameter must be the last parameter in a formal parameter list");
1335 | ARGLIST COMMA error
1337 Report.Error (257, (Location) $1, "An __arglist parameter must be the last parameter in a formal parameter list");
1340 | fixed_parameters COMMA ARGLIST COMMA error
1342 Report.Error (257, (Location) $3, "An __arglist parameter must be the last parameter in a formal parameter list");
1347 $$ = new Parameters (new Parameter[] { (Parameter) $1 } );
1351 $$ = new Parameters (new Parameter[0], true);
1358 ArrayList pars = new ArrayList (4);
1363 | fixed_parameters COMMA fixed_parameter
1365 ArrayList pars = (ArrayList) $1;
1366 Parameter p = (Parameter)$3;
1368 if ((p.modFlags & Parameter.Modifier.This) != 0)
1369 Report.Error (1100, p.Location, "The parameter modifier `this' can only be used on the first parameter");
1378 opt_parameter_modifier
1382 LocatedToken lt = (LocatedToken) $4;
1383 $$ = new Parameter ((Expression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1386 opt_parameter_modifier
1388 IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1390 LocatedToken lt = (LocatedToken) $4;
1391 Report.Error (1552, lt.Location, "Array type specifier, [], must appear before parameter name");
1395 opt_parameter_modifier
1398 Report.Error (1001, GetLocation ($3), "Identifier expected");
1402 opt_parameter_modifier
1405 CheckIdentifierToken (yyToken, GetLocation ($4));
1409 opt_parameter_modifier
1415 LocatedToken lt = (LocatedToken) $4;
1416 Report.Error (241, lt.Location, "Default parameter specifiers are not permitted");
1421 opt_parameter_modifier
1422 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1423 | parameter_modifiers
1427 : parameter_modifier
1431 | parameter_modifiers parameter_modifier
1433 Parameter.Modifier p2 = (Parameter.Modifier)$2;
1434 Parameter.Modifier mod = (Parameter.Modifier)$1 | p2;
1435 if (((Parameter.Modifier)$1 & p2) == p2) {
1436 Error_DuplicateParameterModifier (lexer.Location, p2);
1438 switch (mod & ~Parameter.Modifier.This) {
1439 case Parameter.Modifier.REF:
1440 Report.Error (1101, lexer.Location, "The parameter modifiers `this' and `ref' cannot be used altogether");
1442 case Parameter.Modifier.OUT:
1443 Report.Error (1102, lexer.Location, "The parameter modifiers `this' and `out' cannot be used altogether");
1446 Report.Error (1108, lexer.Location, "A parameter cannot have specified more than one modifier");
1455 : REF { $$ = Parameter.Modifier.REF; }
1456 | OUT { $$ = Parameter.Modifier.OUT; }
1457 | THIS { $$ = Parameter.Modifier.This; }
1461 : opt_attributes PARAMS type IDENTIFIER
1463 LocatedToken lt = (LocatedToken) $4;
1464 $$ = new ParamsParameter ((Expression) $3, lt.Value, (Attributes) $1, lt.Location);
1466 | opt_attributes PARAMS PARAMS type IDENTIFIER
1468 Error_DuplicateParameterModifier (lexer.Location, Parameter.Modifier.PARAMS);
1471 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1473 Parameter.Modifier mod = (Parameter.Modifier)$3;
1474 if ((mod & Parameter.Modifier.This) != 0) {
1475 Report.Error (1104, lexer.Location, "The parameter modifiers `this' and `params' cannot be used altogether");
1477 Report.Error (1611, (Location) $2, "The params parameter cannot be declared as ref or out");
1481 | opt_attributes PARAMS type error {
1482 CheckIdentifierToken (yyToken, GetLocation ($4));
1487 property_declaration
1491 namespace_or_type_name
1493 if (RootContext.Documentation != null)
1494 tmpComment = Lexer.consume_doc_comment ();
1498 implicit_value_parameter_type = (Expression) $3;
1500 lexer.PropertyParsing = true;
1502 accessor_declarations
1504 lexer.PropertyParsing = false;
1505 has_get = has_set = false;
1513 Accessors accessors = (Accessors) $8;
1514 Accessor get_block = accessors.get_or_add;
1515 Accessor set_block = accessors.set_or_remove;
1517 MemberName name = (MemberName) $4;
1519 if (name.TypeArguments != null)
1520 syntax_error (lexer.Location, "a property can't have type arguments");
1522 prop = new Property (current_class, (Expression) $3, (int) $2, false,
1523 name, (Attributes) $1, get_block, set_block, accessors.declared_in_reverse, current_block);
1525 current_container.AddProperty (prop);
1526 implicit_value_parameter_type = null;
1528 if (RootContext.Documentation != null)
1529 prop.DocComment = ConsumeStoredComment ();
1534 accessor_declarations
1535 : get_accessor_declaration
1537 $$ = new Accessors ((Accessor) $1, null);
1539 | get_accessor_declaration accessor_declarations
1541 Accessors accessors = (Accessors) $2;
1542 accessors.get_or_add = (Accessor) $1;
1545 | set_accessor_declaration
1547 $$ = new Accessors (null, (Accessor) $1);
1549 | set_accessor_declaration accessor_declarations
1551 Accessors accessors = (Accessors) $2;
1552 accessors.set_or_remove = (Accessor) $1;
1553 accessors.declared_in_reverse = true;
1558 Report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1563 get_accessor_declaration
1564 : opt_attributes opt_modifiers GET
1566 // If this is not the case, then current_local_parameters has already
1567 // been set in indexer_declaration
1568 if (parsing_indexer == false)
1569 current_local_parameters = null;
1571 current_local_parameters = indexer_parameters;
1572 lexer.PropertyParsing = false;
1574 anonymous_host = SimpleAnonymousHost.GetSimple ();
1579 Report.Error (1007, (Location) $3, "Property accessor already defined");
1582 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1584 current_local_parameters = null;
1585 lexer.PropertyParsing = true;
1587 SimpleAnonymousHost.Simple.Propagate (accessor);
1588 anonymous_host = null;
1590 if (RootContext.Documentation != null)
1591 if (Lexer.doc_state == XmlCommentState.Error)
1592 Lexer.doc_state = XmlCommentState.NotAllowed;
1598 set_accessor_declaration
1599 : opt_attributes opt_modifiers SET
1602 Parameter implicit_value_parameter = new Parameter (
1603 implicit_value_parameter_type, "value",
1604 Parameter.Modifier.NONE, null, (Location) $3);
1606 if (parsing_indexer == false) {
1607 args = new Parameter [1];
1608 args [0] = implicit_value_parameter;
1609 current_local_parameters = new Parameters (args);
1611 Parameter [] fpars = indexer_parameters.FixedParameters;
1614 int count = fpars.Length;
1616 args = new Parameter [count + 1];
1617 fpars.CopyTo (args, 0);
1618 args [count] = implicit_value_parameter;
1621 current_local_parameters = new Parameters (
1625 lexer.PropertyParsing = false;
1627 anonymous_host = SimpleAnonymousHost.GetSimple ();
1632 Report.Error (1007, ((LocatedToken) $3).Location, "Property accessor already defined");
1635 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1637 current_local_parameters = null;
1638 lexer.PropertyParsing = true;
1640 SimpleAnonymousHost.Simple.Propagate (accessor);
1641 anonymous_host = null;
1643 if (RootContext.Documentation != null
1644 && Lexer.doc_state == XmlCommentState.Error)
1645 Lexer.doc_state = XmlCommentState.NotAllowed;
1653 | SEMICOLON { $$ = null; }
1656 interface_declaration
1662 lexer.ConstraintsParsing = true;
1666 MemberName name = MakeName ((MemberName) $6);
1668 push_current_class (new Interface (
1669 current_namespace, current_class, name, (int) $2,
1670 (Attributes) $1), $3);
1673 opt_type_parameter_constraints_clauses
1675 lexer.ConstraintsParsing = false;
1678 current_container.AddBasesForPart (current_class, (ArrayList) $8);
1680 current_class.SetParameterInfo ((ArrayList) $9);
1682 if (RootContext.Documentation != null) {
1683 current_container.DocComment = Lexer.consume_doc_comment ();
1684 Lexer.doc_state = XmlCommentState.Allowed;
1689 if (RootContext.Documentation != null)
1690 Lexer.doc_state = XmlCommentState.Allowed;
1694 $$ = pop_current_class ();
1696 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1697 CheckIdentifierToken (yyToken, GetLocation ($5));
1703 opt_interface_member_declarations
1707 opt_interface_member_declarations
1709 | interface_member_declarations
1712 interface_member_declarations
1713 : interface_member_declaration
1714 | interface_member_declarations interface_member_declaration
1717 interface_member_declaration
1718 : interface_method_declaration
1723 Method m = (Method) $1;
1725 if (m.IsExplicitImpl)
1726 Report.Error (541, m.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1727 m.GetSignatureForError ());
1729 current_container.AddMethod (m);
1731 if (RootContext.Documentation != null)
1732 Lexer.doc_state = XmlCommentState.Allowed;
1734 | interface_property_declaration
1739 Property p = (Property) $1;
1741 if (p.IsExplicitImpl)
1742 Report.Error (541, p.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1743 p.GetSignatureForError ());
1745 current_container.AddProperty (p);
1747 if (RootContext.Documentation != null)
1748 Lexer.doc_state = XmlCommentState.Allowed;
1750 | interface_event_declaration
1753 Event e = (Event) $1;
1755 if (e.IsExplicitImpl)
1756 Report.Error (541, e.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1757 e.GetSignatureForError ());
1759 current_container.AddEvent (e);
1762 if (RootContext.Documentation != null)
1763 Lexer.doc_state = XmlCommentState.Allowed;
1765 | interface_indexer_declaration
1770 Indexer i = (Indexer) $1;
1772 if (i.IsExplicitImpl)
1773 Report.Error (541, i.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1774 i.GetSignatureForError ());
1776 current_container.AddIndexer (i);
1778 if (RootContext.Documentation != null)
1779 Lexer.doc_state = XmlCommentState.Allowed;
1781 | delegate_declaration
1784 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1785 ((MemberCore)$1).GetSignatureForError ());
1791 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1792 ((MemberCore)$1).GetSignatureForError ());
1795 | struct_declaration
1798 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1799 ((MemberCore)$1).GetSignatureForError ());
1805 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1806 ((MemberCore)$1).GetSignatureForError ());
1809 | interface_declaration
1812 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1813 ((MemberCore)$1).GetSignatureForError ());
1816 | constant_declaration
1818 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1826 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, GetLocation ($1));
1831 interface_method_declaration_body
1834 lexer.ConstraintsParsing = false;
1836 opt_statement_list CLOSE_BRACE
1838 Report.Error (531, lexer.Location,
1839 "'{0}': interface members cannot have a definition", ((MemberName) $-1).ToString ());
1845 interface_method_declaration
1846 : opt_attributes opt_new type namespace_or_type_name
1847 open_parens opt_formal_parameter_list CLOSE_PARENS
1849 lexer.ConstraintsParsing = true;
1851 opt_type_parameter_constraints_clauses
1853 // Refer to the name as $-1 in interface_method_declaration_body
1856 interface_method_declaration_body
1858 lexer.ConstraintsParsing = false;
1860 MemberName name = (MemberName) $4;
1862 if ($9 != null && name.TypeArguments == null)
1863 Report.Error (80, lexer.Location,
1864 "Constraints are not allowed on non-generic declarations");
1866 GenericMethod generic = null;
1867 if (name.TypeArguments != null) {
1868 generic = new GenericMethod (current_namespace, current_class, name,
1869 (Expression) $3, (Parameters) $6);
1871 generic.SetParameterInfo ((ArrayList) $9);
1874 $$ = new Method (current_class, generic, (Expression) $3, (int) $2, true, name,
1875 (Parameters) $6, (Attributes) $1);
1876 if (RootContext.Documentation != null)
1877 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1879 | opt_attributes opt_new VOID namespace_or_type_name
1880 open_parens opt_formal_parameter_list CLOSE_PARENS
1882 lexer.ConstraintsParsing = true;
1884 opt_type_parameter_constraints_clauses
1888 interface_method_declaration_body
1890 lexer.ConstraintsParsing = false;
1892 MemberName name = (MemberName) $4;
1894 if ($9 != null && name.TypeArguments == null)
1895 Report.Error (80, lexer.Location,
1896 "Constraints are not allowed on non-generic declarations");
1898 GenericMethod generic = null;
1899 if (name.TypeArguments != null) {
1900 generic = new GenericMethod (current_namespace, current_class, name,
1901 TypeManager.system_void_expr, (Parameters) $6);
1903 generic.SetParameterInfo ((ArrayList) $9);
1906 $$ = new Method (current_class, generic, TypeManager.system_void_expr, (int) $2,
1907 true, name, (Parameters) $6, (Attributes) $1);
1908 if (RootContext.Documentation != null)
1909 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1913 interface_property_declaration
1919 lexer.PropertyParsing = true;
1920 implicit_value_parameter_type = (Expression)$3;
1922 accessor_declarations
1924 has_get = has_set = false;
1925 lexer.PropertyParsing = false;
1926 implicit_value_parameter_type = null;
1930 LocatedToken lt = (LocatedToken) $4;
1931 MemberName name = new MemberName (lt.Value, lt.Location);
1933 if ($3 == TypeManager.system_void_expr) {
1934 Report.Error (547, lt.Location, "`{0}': property or indexer cannot have void type", lt.Value);
1940 p = new Property (current_class, (Expression) $3, (int) $2, true,
1941 name, (Attributes) $1,
1944 Report.Error (548, p.Location, "`{0}': property or indexer must have at least one accessor", p.GetSignatureForError ());
1948 Accessors accessor = (Accessors) $7;
1949 p = new Property (current_class, (Expression) $3, (int) $2, true,
1950 name, (Attributes) $1,
1951 accessor.get_or_add, accessor.set_or_remove, accessor.declared_in_reverse);
1953 if (accessor.get_or_add != null && accessor.get_or_add.Block != null) {
1954 Report.Error (531, p.Location, "`{0}.get': interface members cannot have a definition", p.GetSignatureForError ());
1959 if (accessor.set_or_remove != null && accessor.set_or_remove.Block != null) {
1960 Report.Error (531, p.Location, "`{0}.set': interface members cannot have a definition", p.GetSignatureForError ());
1965 if (RootContext.Documentation != null)
1966 p.DocComment = Lexer.consume_doc_comment ();
1973 CheckIdentifierToken (yyToken, GetLocation ($4));
1979 interface_event_declaration
1980 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1982 LocatedToken lt = (LocatedToken) $5;
1983 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1984 new MemberName (lt.Value, lt.Location),
1986 if (RootContext.Documentation != null)
1987 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1989 | opt_attributes opt_new EVENT type error {
1990 CheckIdentifierToken (yyToken, GetLocation ($5));
1993 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1994 LocatedToken lt = (LocatedToken) $5;
1995 Report.Error (68, lt.Location, "`{0}.{1}': event in interface cannot have initializer", current_container.Name, lt.Value);
1998 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
2000 implicit_value_parameter_type = (Expression) $4;
2001 lexer.EventParsing = true;
2003 event_accessor_declarations
2005 lexer.EventParsing = false;
2006 implicit_value_parameter_type = null;
2009 Report.Error (69, (Location) $3, "Event in interface cannot have add or remove accessors");
2014 interface_indexer_declaration
2015 : opt_attributes opt_new type THIS
2016 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
2019 lexer.PropertyParsing = true;
2020 implicit_value_parameter_type = (Expression)$3;
2022 accessor_declarations
2024 has_get = has_set = false;
2025 lexer.PropertyParsing = false;
2026 implicit_value_parameter_type = null;
2032 i = new Indexer (current_class, (Expression) $3,
2033 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
2034 (int) $2, true, (Parameters) $6, (Attributes) $1,
2037 Report.Error (548, i.Location, "`{0}': property or indexer must have at least one accessor", i.GetSignatureForError ());
2041 Accessors accessors = (Accessors) $10;
2042 i = new Indexer (current_class, (Expression) $3,
2043 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
2044 (int) $2, true, (Parameters) $6, (Attributes) $1,
2045 accessors.get_or_add, accessors.set_or_remove, accessors.declared_in_reverse);
2047 if (accessors.get_or_add != null && accessors.get_or_add.Block != null) {
2048 Report.Error (531, i.Location, "`{0}.get': interface members cannot have a definition", i.GetSignatureForError ());
2053 if (accessors.set_or_remove != null && accessors.set_or_remove.Block != null) {
2054 Report.Error (531, i.Location, "`{0}.set': interface members cannot have a definition", i.GetSignatureForError ());
2059 if (RootContext.Documentation != null)
2060 i.DocComment = ConsumeStoredComment ();
2066 operator_declaration
2067 : opt_attributes opt_modifiers operator_declarator
2069 anonymous_host = SimpleAnonymousHost.GetSimple ();
2076 OperatorDeclaration decl = (OperatorDeclaration) $3;
2078 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
2080 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null, decl.location);
2081 if (decl.arg2type != null)
2082 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null, decl.location);
2084 Operator op = new Operator (
2085 current_class, decl.optype, decl.ret_type, (int) $2,
2086 new Parameters (param_list),
2087 (ToplevelBlock) $5, (Attributes) $1, decl.location);
2089 if (RootContext.Documentation != null) {
2090 op.DocComment = tmpComment;
2091 Lexer.doc_state = XmlCommentState.Allowed;
2094 SimpleAnonymousHost.Simple.Propagate (op);
2095 anonymous_host = null;
2097 // Note again, checking is done in semantic analysis
2098 current_container.AddOperator (op);
2100 current_local_parameters = null;
2106 | SEMICOLON { $$ = null; }
2109 : type OPERATOR overloadable_operator
2110 open_parens opt_attributes opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
2112 // TODO: wrong location
2113 if ((Parameter.Modifier)$6 != Parameter.Modifier.NONE)
2114 Error_ParameterModifierNotValid ((Location) $2);
2116 LocatedToken lt = (LocatedToken) $8;
2117 Operator.OpType op = (Operator.OpType) $3;
2118 CheckUnaryOperator (op, lt.Location);
2120 if (op == Operator.OpType.Addition)
2121 op = Operator.OpType.UnaryPlus;
2123 if (op == Operator.OpType.Subtraction)
2124 op = Operator.OpType.UnaryNegation;
2126 Parameter [] pars = new Parameter [1];
2127 Expression type = (Expression) $7;
2129 pars [0] = new Parameter (type, lt.Value, Parameter.Modifier.NONE, (Attributes) $5, lt.Location);
2131 current_local_parameters = new Parameters (pars);
2133 if (RootContext.Documentation != null) {
2134 tmpComment = Lexer.consume_doc_comment ();
2135 Lexer.doc_state = XmlCommentState.NotAllowed;
2138 $$ = new OperatorDeclaration (op, (Expression) $1, type, lt.Value,
2139 null, null, (Location) $2);
2141 | type OPERATOR overloadable_operator
2143 opt_attributes opt_parameter_modifier type IDENTIFIER COMMA
2144 opt_attributes opt_parameter_modifier type IDENTIFIER
2147 // TODO: wrong location
2148 if ((Parameter.Modifier)$6 != Parameter.Modifier.NONE || (Parameter.Modifier)$11 != Parameter.Modifier.NONE)
2149 Error_ParameterModifierNotValid ((Location) $2);
2151 LocatedToken ltParam1 = (LocatedToken) $8;
2152 LocatedToken ltParam2 = (LocatedToken) $13;
2153 CheckBinaryOperator ((Operator.OpType) $3, (Location) $2);
2155 Parameter [] pars = new Parameter [2];
2157 Expression typeL = (Expression) $7;
2158 Expression typeR = (Expression) $12;
2160 pars [0] = new Parameter (typeL, ltParam1.Value, Parameter.Modifier.NONE, (Attributes) $5, ltParam1.Location);
2161 pars [1] = new Parameter (typeR, ltParam2.Value, Parameter.Modifier.NONE, (Attributes) $10, ltParam2.Location);
2163 current_local_parameters = new Parameters (pars);
2165 if (RootContext.Documentation != null) {
2166 tmpComment = Lexer.consume_doc_comment ();
2167 Lexer.doc_state = XmlCommentState.NotAllowed;
2170 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
2171 typeL, ltParam1.Value,
2172 typeR, ltParam2.Value, (Location) $2);
2174 | conversion_operator_declarator
2175 | type OPERATOR overloadable_operator
2177 opt_attributes opt_parameter_modifier type IDENTIFIER COMMA
2178 opt_attributes opt_parameter_modifier type IDENTIFIER COMMA error
2180 Report.Error (1534, (Location) $2, "Overloaded binary operator `{0}' takes two parameters",
2181 Operator.GetName ((Operator.OpType) $3));
2184 | type OPERATOR overloadable_operator
2185 open_parens CLOSE_PARENS
2187 Report.Error (1535, (Location) $2, "Overloaded unary operator `{0}' takes one parameter",
2188 Operator.GetName ((Operator.OpType) $3));
2193 overloadable_operator
2195 : BANG { $$ = Operator.OpType.LogicalNot; }
2196 | TILDE { $$ = Operator.OpType.OnesComplement; }
2197 | OP_INC { $$ = Operator.OpType.Increment; }
2198 | OP_DEC { $$ = Operator.OpType.Decrement; }
2199 | TRUE { $$ = Operator.OpType.True; }
2200 | FALSE { $$ = Operator.OpType.False; }
2201 // Unary and binary:
2202 | PLUS { $$ = Operator.OpType.Addition; }
2203 | MINUS { $$ = Operator.OpType.Subtraction; }
2205 | STAR { $$ = Operator.OpType.Multiply; }
2206 | DIV { $$ = Operator.OpType.Division; }
2207 | PERCENT { $$ = Operator.OpType.Modulus; }
2208 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
2209 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
2210 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
2211 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
2212 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
2213 | OP_EQ { $$ = Operator.OpType.Equality; }
2214 | OP_NE { $$ = Operator.OpType.Inequality; }
2215 | OP_GT { $$ = Operator.OpType.GreaterThan; }
2216 | OP_LT { $$ = Operator.OpType.LessThan; }
2217 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
2218 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
2221 conversion_operator_declarator
2222 : IMPLICIT OPERATOR type open_parens opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
2224 // TODO: wrong location
2225 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
2226 Error_ParameterModifierNotValid (GetLocation ($4));
2228 LocatedToken lt = (LocatedToken) $7;
2229 Parameter [] pars = new Parameter [1];
2231 pars [0] = new Parameter ((Expression) $6, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2233 current_local_parameters = new Parameters (pars);
2235 if (RootContext.Documentation != null) {
2236 tmpComment = Lexer.consume_doc_comment ();
2237 Lexer.doc_state = XmlCommentState.NotAllowed;
2240 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $6, lt.Value,
2241 null, null, (Location) $2);
2243 | EXPLICIT OPERATOR type open_parens opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
2245 // TODO: wrong location
2246 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
2247 Error_ParameterModifierNotValid (GetLocation ($4));
2249 LocatedToken lt = (LocatedToken) $7;
2250 Parameter [] pars = new Parameter [1];
2252 pars [0] = new Parameter ((Expression) $6, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2254 current_local_parameters = new Parameters (pars);
2256 if (RootContext.Documentation != null) {
2257 tmpComment = Lexer.consume_doc_comment ();
2258 Lexer.doc_state = XmlCommentState.NotAllowed;
2261 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $6, lt.Value,
2262 null, null, (Location) $2);
2266 syntax_error ((Location) $1, "'operator' expected");
2270 syntax_error ((Location) $1, "'operator' expected");
2274 constructor_declaration
2277 constructor_declarator
2280 Constructor c = (Constructor) $3;
2281 c.Block = (ToplevelBlock) $4;
2282 c.OptAttributes = (Attributes) $1;
2283 c.ModFlags = (int) $2;
2285 if (RootContext.Documentation != null)
2286 c.DocComment = ConsumeStoredComment ();
2288 if (c.Name == current_container.Basename){
2289 if ((c.ModFlags & Modifiers.STATIC) != 0){
2290 if ((c.ModFlags & Modifiers.Accessibility) != 0){
2291 Report.Error (515, c.Location,
2292 "`{0}': access modifiers are not allowed on static constructors",
2293 c.GetSignatureForError ());
2296 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2298 if (c.Initializer != null){
2299 Report.Error (514, c.Location,
2300 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2301 c.GetSignatureForError ());
2304 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2307 // We let another layer check the validity of the constructor.
2308 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
2311 current_container.AddConstructor (c);
2313 current_local_parameters = null;
2314 if (RootContext.Documentation != null)
2315 Lexer.doc_state = XmlCommentState.Allowed;
2319 constructor_declarator
2320 : constructor_header
2324 | constructor_header constructor_initializer
2326 ((Constructor)$1).Initializer = (ConstructorInitializer) $2;
2334 if (RootContext.Documentation != null) {
2335 tmpComment = Lexer.consume_doc_comment ();
2336 Lexer.doc_state = XmlCommentState.Allowed;
2339 open_parens opt_formal_parameter_list CLOSE_PARENS
2341 LocatedToken lt = (LocatedToken) $1;
2342 current_local_parameters = (Parameters) $4;
2343 current_block = new ToplevelBlock (null, current_local_parameters, null, lt.Location);
2345 $$ = new Constructor (current_class, lt.Value, 0, current_local_parameters,
2348 anonymous_host = (IAnonymousHost) $$;
2354 | SEMICOLON { current_block = null; $$ = null; }
2357 constructor_initializer
2358 : COLON BASE open_parens opt_argument_list CLOSE_PARENS
2360 $$ = new ConstructorBaseInitializer ((ArrayList) $4, (Location) $2);
2362 | COLON THIS open_parens opt_argument_list CLOSE_PARENS
2364 $$ = new ConstructorThisInitializer ((ArrayList) $4, (Location) $2);
2367 Report.Error (1018, (Location) $1, "Keyword this or base expected");
2373 : /* EMPTY */ { $$ = 0; }
2374 | UNSAFE { $$ = Modifiers.UNSAFE; }
2375 | EXTERN { $$ = Modifiers.EXTERN; }
2378 destructor_declaration
2379 : opt_attributes opt_finalizer TILDE
2381 if (RootContext.Documentation != null) {
2382 tmpComment = Lexer.consume_doc_comment ();
2383 Lexer.doc_state = XmlCommentState.NotAllowed;
2386 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2388 LocatedToken lt = (LocatedToken) $5;
2389 if (lt.Value != current_container.MemberName.Name){
2390 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2391 } else if (current_container.Kind != Kind.Class){
2392 Report.Error (575, lt.Location, "Only class types can contain destructor");
2394 Location l = lt.Location;
2397 if (!RootContext.StdLib && current_container.Name == "System.Object")
2398 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2400 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2402 Method d = new Destructor (
2403 current_class, TypeManager.system_void_expr, m, "Finalize",
2404 Parameters.EmptyReadOnlyParameters, (Attributes) $1, l);
2405 if (RootContext.Documentation != null)
2406 d.DocComment = ConsumeStoredComment ();
2408 d.Block = (ToplevelBlock) $8;
2409 current_container.AddMethod (d);
2417 EVENT type variable_declarators SEMICOLON
2419 current_array_type = null;
2420 foreach (VariableDeclaration var in (ArrayList) $5) {
2422 MemberName name = new MemberName (var.identifier,
2425 EventField e = new EventField (
2426 current_class, (Expression) $4, (int) $2, false, name,
2429 e.Initializer = var.expression_or_array_initializer;
2431 current_container.AddEvent (e);
2433 if (RootContext.Documentation != null) {
2434 e.DocComment = Lexer.consume_doc_comment ();
2435 Lexer.doc_state = XmlCommentState.Allowed;
2441 EVENT type namespace_or_type_name
2444 implicit_value_parameter_type = (Expression) $4;
2445 lexer.EventParsing = true;
2447 event_accessor_declarations
2449 lexer.EventParsing = false;
2453 MemberName name = (MemberName) $5;
2456 Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2457 current_container.Name, name.ToString ());
2460 Accessors accessors = (Accessors) $8;
2462 if (name.TypeArguments != null)
2463 syntax_error (lexer.Location, "an event can't have type arguments");
2465 if (accessors.get_or_add == null || accessors.set_or_remove == null)
2466 // CS0073 is already reported, so no CS0065 here.
2469 Event e = new EventProperty (
2470 current_class, (Expression) $4, (int) $2, false, name,
2471 (Attributes) $1, accessors.get_or_add, accessors.set_or_remove);
2472 if (RootContext.Documentation != null) {
2473 e.DocComment = Lexer.consume_doc_comment ();
2474 Lexer.doc_state = XmlCommentState.Allowed;
2477 current_container.AddEvent (e);
2478 implicit_value_parameter_type = null;
2482 | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
2483 MemberName mn = (MemberName) $5;
2485 if (mn.Left != null)
2486 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2488 Report.Error (71, mn.Location, "Event declaration should use property syntax");
2490 if (RootContext.Documentation != null)
2491 Lexer.doc_state = XmlCommentState.Allowed;
2495 event_accessor_declarations
2496 : add_accessor_declaration remove_accessor_declaration
2498 $$ = new Accessors ((Accessor) $1, (Accessor) $2);
2500 | remove_accessor_declaration add_accessor_declaration
2502 Accessors accessors = new Accessors ((Accessor) $2, (Accessor) $1);
2503 accessors.declared_in_reverse = true;
2506 | add_accessor_declaration { $$ = null; }
2507 | remove_accessor_declaration { $$ = null; }
2510 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2516 add_accessor_declaration
2517 : opt_attributes ADD
2519 Parameter [] args = new Parameter [1];
2520 Parameter implicit_value_parameter = new Parameter (
2521 implicit_value_parameter_type, "value",
2522 Parameter.Modifier.NONE, null, (Location) $2);
2524 args [0] = implicit_value_parameter;
2526 current_local_parameters = new Parameters (args);
2527 lexer.EventParsing = false;
2529 anonymous_host = SimpleAnonymousHost.GetSimple ();
2533 Accessor accessor = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2534 lexer.EventParsing = true;
2536 current_local_parameters = null;
2537 SimpleAnonymousHost.Simple.Propagate (accessor);
2538 anonymous_host = null;
2542 | opt_attributes ADD error {
2543 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2546 | opt_attributes modifiers ADD {
2547 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2552 remove_accessor_declaration
2553 : opt_attributes REMOVE
2555 Parameter [] args = new Parameter [1];
2556 Parameter implicit_value_parameter = new Parameter (
2557 implicit_value_parameter_type, "value",
2558 Parameter.Modifier.NONE, null, (Location) $2);
2560 args [0] = implicit_value_parameter;
2562 current_local_parameters = new Parameters (args);
2563 lexer.EventParsing = false;
2567 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2568 lexer.EventParsing = true;
2570 | opt_attributes REMOVE error {
2571 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2574 | opt_attributes modifiers REMOVE {
2575 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2581 : opt_attributes opt_modifiers indexer_declarator
2584 IndexerDeclaration decl = (IndexerDeclaration) $3;
2586 implicit_value_parameter_type = decl.type;
2588 lexer.PropertyParsing = true;
2589 parsing_indexer = true;
2591 indexer_parameters = decl.param_list;
2592 anonymous_host = SimpleAnonymousHost.GetSimple ();
2594 accessor_declarations
2596 lexer.PropertyParsing = false;
2597 has_get = has_set = false;
2598 parsing_indexer = false;
2605 // The signature is computed from the signature of the indexer. Look
2606 // at section 3.6 on the spec
2608 IndexerDeclaration decl = (IndexerDeclaration) $3;
2609 Accessors accessors = (Accessors) $6;
2610 Accessor get_block = accessors.get_or_add;
2611 Accessor set_block = accessors.set_or_remove;
2614 if (decl.interface_type != null)
2615 name = new MemberName (
2616 decl.interface_type, TypeContainer.DefaultIndexerName, decl.location);
2618 name = new MemberName (TypeContainer.DefaultIndexerName, decl.location);
2620 indexer = new Indexer (current_class, decl.type, name,
2621 (int) $2, false, decl.param_list, (Attributes) $1,
2622 get_block, set_block, accessors.declared_in_reverse);
2624 if (RootContext.Documentation != null)
2625 indexer.DocComment = ConsumeStoredComment ();
2627 current_container.AddIndexer (indexer);
2629 current_local_parameters = null;
2630 implicit_value_parameter_type = null;
2631 indexer_parameters = null;
2636 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2638 Parameters pars = (Parameters) $4;
2639 if (pars.HasArglist) {
2640 // "__arglist is not valid in this context"
2641 Report.Error (1669, (Location) $2, "__arglist is not valid in this context");
2642 } else if (pars.Empty){
2643 Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
2645 if (RootContext.Documentation != null) {
2646 tmpComment = Lexer.consume_doc_comment ();
2647 Lexer.doc_state = XmlCommentState.Allowed;
2650 $$ = new IndexerDeclaration ((Expression) $1, null, pars, (Location) $2);
2652 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2654 Parameters pars = (Parameters) $6;
2656 if (pars.HasArglist) {
2657 // "__arglist is not valid in this context"
2658 Report.Error (1669, (Location) $4, "__arglist is not valid in this context");
2659 } else if (pars.Empty){
2660 Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
2663 MemberName name = (MemberName) $2;
2664 $$ = new IndexerDeclaration ((Expression) $1, name, pars, (Location) $4);
2666 if (RootContext.Documentation != null) {
2667 tmpComment = Lexer.consume_doc_comment ();
2668 Lexer.doc_state = XmlCommentState.Allowed;
2678 if (RootContext.Documentation != null)
2679 enumTypeComment = Lexer.consume_doc_comment ();
2684 LocatedToken lt = (LocatedToken) $4;
2685 Location enum_location = lt.Location;
2687 MemberName name = MakeName (new MemberName (lt.Value, enum_location));
2688 Enum e = new Enum (current_namespace, current_class, (Expression) $5, (int) $2,
2689 name, (Attributes) $1);
2691 if (RootContext.Documentation != null)
2692 e.DocComment = enumTypeComment;
2695 EnumMember em = null;
2696 foreach (VariableDeclaration ev in (ArrayList) $7) {
2697 em = new EnumMember (
2698 e, em, ev.identifier, (Expression) ev.expression_or_array_initializer,
2699 ev.OptAttributes, ev.Location);
2701 // if (RootContext.Documentation != null)
2702 em.DocComment = ev.DocComment;
2704 e.AddEnumMember (em);
2707 current_container.AddTypeContainer (e);
2714 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2715 | COLON type { $$ = $2; }
2721 if (RootContext.Documentation != null)
2722 Lexer.doc_state = XmlCommentState.Allowed;
2724 opt_enum_member_declarations
2726 // here will be evaluated after CLOSE_BLACE is consumed.
2727 if (RootContext.Documentation != null)
2728 Lexer.doc_state = XmlCommentState.Allowed;
2736 opt_enum_member_declarations
2737 : /* empty */ { $$ = new ArrayList (4); }
2738 | enum_member_declarations opt_comma { $$ = $1; }
2741 enum_member_declarations
2742 : enum_member_declaration
2744 ArrayList l = new ArrayList (4);
2749 | enum_member_declarations COMMA enum_member_declaration
2751 ArrayList l = (ArrayList) $1;
2759 enum_member_declaration
2760 : opt_attributes IDENTIFIER
2762 VariableDeclaration vd = new VariableDeclaration (
2763 (LocatedToken) $2, null, (Attributes) $1);
2765 if (RootContext.Documentation != null) {
2766 vd.DocComment = Lexer.consume_doc_comment ();
2767 Lexer.doc_state = XmlCommentState.Allowed;
2772 | opt_attributes IDENTIFIER
2774 if (RootContext.Documentation != null) {
2775 tmpComment = Lexer.consume_doc_comment ();
2776 Lexer.doc_state = XmlCommentState.NotAllowed;
2781 VariableDeclaration vd = new VariableDeclaration (
2782 (LocatedToken) $2, $5, (Attributes) $1);
2784 if (RootContext.Documentation != null)
2785 vd.DocComment = ConsumeStoredComment ();
2791 delegate_declaration
2796 lexer.ConstraintsParsing = true;
2799 open_parens opt_formal_parameter_list CLOSE_PARENS
2801 MemberName name = MakeName ((MemberName) $6);
2802 Parameters p = (Parameters) $8;
2804 // TODO: wrong location
2805 Report.Error (1669, name.Location, "__arglist is not valid in this context");
2808 Delegate del = new Delegate (current_namespace, current_class, (Expression) $5,
2809 (int) $2, name, p, (Attributes) $1);
2811 if (RootContext.Documentation != null) {
2812 del.DocComment = Lexer.consume_doc_comment ();
2813 Lexer.doc_state = XmlCommentState.Allowed;
2816 current_container.AddDelegate (del);
2817 current_delegate = del;
2819 opt_type_parameter_constraints_clauses
2821 lexer.ConstraintsParsing = false;
2825 current_delegate.SetParameterInfo ((ArrayList) $11);
2826 $$ = current_delegate;
2828 current_delegate = null;
2835 lexer.CheckNullable (false);
2840 lexer.CheckNullable (true);
2845 namespace_or_type_name
2846 : IDENTIFIER opt_type_argument_list
2848 LocatedToken lt = (LocatedToken) $1;
2849 $$ = new MemberName (lt.Value, (TypeArguments) $2, lt.Location);
2851 | IDENTIFIER DOUBLE_COLON IDENTIFIER {
2852 LocatedToken lt1 = (LocatedToken) $1;
2853 LocatedToken lt2 = (LocatedToken) $3;
2854 $$ = new MemberName (lt1.Value, lt2.Value, lt2.Location);
2856 | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list {
2857 LocatedToken lt = (LocatedToken) $3;
2858 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $4, lt.Location);
2863 : IDENTIFIER opt_type_parameter_list
2865 LocatedToken lt = (LocatedToken) $1;
2866 $$ = new MemberName (lt.Value, (TypeArguments) $2, lt.Location);
2868 | namespace_or_type_name DOT IDENTIFIER opt_type_parameter_list
2870 LocatedToken lt = (LocatedToken) $3;
2871 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $4, lt.Location);
2876 : IDENTIFIER opt_type_parameter_list
2878 LocatedToken lt = (LocatedToken) $1;
2879 $$ = new MemberName (lt.Value, (TypeArguments)$2, lt.Location);
2884 // Generics arguments (any type, without attributes)
2887 opt_type_argument_list
2888 : /* empty */ { $$ = null; }
2889 | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2891 if (RootContext.Version == LanguageVersion.ISO_1)
2892 Report.FeatureIsNotISO1 (lexer.Location, "generics");
2899 // Generics parameters (identifiers only, with attributes), used in type, method declarations
2902 opt_type_parameter_list
2903 : /* empty */ { $$ = null; }
2904 | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2906 if (RootContext.Version == LanguageVersion.ISO_1)
2907 Report.FeatureIsNotISO1 (lexer.Location, "generics");
2916 TypeArguments type_args = new TypeArguments (lexer.Location);
2917 type_args.Add ((Expression) $1);
2920 | type_arguments COMMA type_argument
2922 TypeArguments type_args = (TypeArguments) $1;
2923 type_args.Add ((Expression) $3);
2933 | attribute_sections type
2935 SimpleName sn = $2 as SimpleName;
2937 Error_TypeExpected (GetLocation ($2));
2939 $2 = new TypeParameterName (sn.Name, (Attributes) $1, lexer.Location);
2945 * Before you think of adding a return_type, notice that we have been
2946 * using two rules in the places where it matters (one rule using type
2947 * and another identical one that uses VOID as the return type). This
2948 * gets rid of a shift/reduce couple
2951 : namespace_or_type_name opt_nullable
2953 MemberName name = (MemberName) $1;
2954 $$ = name.GetTypeExpression ();
2957 $$ = new ComposedCast ((Expression) $$, "?", lexer.Location);
2959 | builtin_types opt_nullable
2962 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2972 // Note that here only unmanaged types are allowed but we
2973 // can't perform checks during this phase - we do it during
2974 // semantic analysis.
2976 $$ = new ComposedCast ((Expression) $1, "*", Lexer.Location);
2980 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2985 : builtin_types opt_nullable
2988 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2990 | non_expression_type rank_specifier
2992 Location loc = GetLocation ($1);
2994 loc = lexer.Location;
2995 $$ = new ComposedCast ((Expression) $1, (string) $2, loc);
2997 | non_expression_type STAR
2999 Location loc = GetLocation ($1);
3001 loc = lexer.Location;
3002 $$ = new ComposedCast ((Expression) $1, "*", loc);
3004 | expression rank_specifiers
3006 $$ = new ComposedCast ((Expression) $1, (string) $2);
3010 $$ = new ComposedCast ((Expression) $1, "*");
3014 // We need this because the parser will happily go and reduce IDENTIFIER STAR
3015 // through this different path
3017 | multiplicative_expression STAR
3019 $$ = new ComposedCast ((Expression) $1, "*");
3026 ArrayList types = new ArrayList (4);
3031 | type_list COMMA type
3033 ArrayList types = (ArrayList) $1;
3041 * replaces all the productions for isolating the various
3042 * simple types, but we need this to reuse it easily in local_variable_type
3045 : OBJECT { $$ = TypeManager.system_object_expr; }
3046 | STRING { $$ = TypeManager.system_string_expr; }
3047 | BOOL { $$ = TypeManager.system_boolean_expr; }
3048 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
3049 | FLOAT { $$ = TypeManager.system_single_expr; }
3050 | DOUBLE { $$ = TypeManager.system_double_expr; }
3055 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
3056 | BYTE { $$ = TypeManager.system_byte_expr; }
3057 | SHORT { $$ = TypeManager.system_int16_expr; }
3058 | USHORT { $$ = TypeManager.system_uint16_expr; }
3059 | INT { $$ = TypeManager.system_int32_expr; }
3060 | UINT { $$ = TypeManager.system_uint32_expr; }
3061 | LONG { $$ = TypeManager.system_int64_expr; }
3062 | ULONG { $$ = TypeManager.system_uint64_expr; }
3063 | CHAR { $$ = TypeManager.system_char_expr; }
3064 | VOID { $$ = TypeManager.system_void_expr; }
3068 : type rank_specifiers opt_nullable
3070 string rank_specifiers = (string) $2;
3072 rank_specifiers += "?";
3074 $$ = current_array_type = new ComposedCast ((Expression) $1, rank_specifiers);
3079 // Expressions, section 7.5
3088 MemberName mn = (MemberName) $1;
3089 $$ = mn.GetTypeExpression ();
3091 | IDENTIFIER DOUBLE_COLON IDENTIFIER
3093 LocatedToken lt1 = (LocatedToken) $1;
3094 LocatedToken lt2 = (LocatedToken) $3;
3095 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, lt2.Location);
3097 | parenthesized_expression
3098 | default_value_expression
3100 | invocation_expression
3104 | post_increment_expression
3105 | post_decrement_expression
3109 | checked_expression
3110 | unchecked_expression
3111 | pointer_member_access
3112 | anonymous_method_expression
3119 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
3120 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); }
3121 | NULL { $$ = new NullLiteral (lexer.Location); }
3125 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
3126 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
3127 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
3132 object v = lexer.Value;
3135 $$ = new IntLiteral ((int) v, lexer.Location);
3136 } else if (v is uint)
3137 $$ = new UIntLiteral ((UInt32) v, lexer.Location);
3139 $$ = new LongLiteral ((Int64) v, lexer.Location);
3140 else if (v is ulong)
3141 $$ = new ULongLiteral ((UInt64) v, lexer.Location);
3143 Console.WriteLine ("OOPS. Unexpected result from scanner");
3148 : TRUE { $$ = new BoolLiteral (true, lexer.Location); }
3149 | FALSE { $$ = new BoolLiteral (false, lexer.Location); }
3152 parenthesized_expression_0
3153 : OPEN_PARENS expression CLOSE_PARENS
3156 lexer.Deambiguate_CloseParens ($$);
3157 // After this, the next token returned is one of
3158 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST (CLOSE_PARENS), CLOSE_PARENS_OPEN_PARENS
3159 // or CLOSE_PARENS_MINUS.
3161 | OPEN_PARENS expression error { CheckToken (1026, yyToken, "Expecting ')'", lexer.Location); }
3164 parenthesized_expression
3165 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
3169 | parenthesized_expression_0 CLOSE_PARENS
3173 | parenthesized_expression_0 CLOSE_PARENS_MINUS
3175 // If a parenthesized expression is followed by a minus, we need to wrap
3176 // the expression inside a ParenthesizedExpression for the CS0075 check
3177 // in Binary.DoResolve().
3178 $$ = new ParenthesizedExpression ((Expression) $1);
3183 : primary_expression DOT IDENTIFIER opt_type_argument_list
3185 LocatedToken lt = (LocatedToken) $3;
3186 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
3188 | predefined_type DOT IDENTIFIER opt_type_argument_list
3190 LocatedToken lt = (LocatedToken) $3;
3191 // TODO: Location is wrong as some predefined types doesn't hold a location
3192 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
3200 invocation_expression
3201 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
3204 Report.Error (1, (Location) $2, "Parse error");
3206 $$ = new Invocation ((Expression) $1, (ArrayList) $3);
3208 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
3210 $$ = new Invocation ((Expression) $1, new ArrayList ());
3212 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
3214 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
3216 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
3218 ArrayList args = new ArrayList (1);
3220 $$ = new Invocation ((Expression) $1, args);
3222 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
3224 ArrayList args = ((ArrayList) $4);
3226 $$ = new Invocation ((Expression) $1, args);
3230 opt_object_initializer
3231 : /* empty */ { $$ = null; }
3232 | OPEN_BRACE variable_declarators CLOSE_BRACE
3234 ArrayList vars = (ArrayList) $2;
3235 ArrayList inits = new ArrayList (vars.Count);
3236 foreach (VariableDeclaration variable in vars) {
3237 Initializer i = new Initializer (variable.identifier, variable.expression_or_array_initializer);
3244 opt_object_or_collection_initializer
3245 : /* empty */ { $$ = null; }
3246 | object_or_collection_initializer
3249 object_or_collection_initializer
3250 : object_initializer
3251 | collection_initializer
3255 : OPEN_BRACE opt_member_initializer_list CLOSE_BRACE
3260 $$ = new ObjectInitializer ((ArrayList)$2);
3262 | OPEN_BRACE member_initializer_list COMMA CLOSE_BRACE
3264 $$ = new ObjectInitializer ((ArrayList) $2);
3268 opt_member_initializer_list
3269 : /* empty */ { $$ = null; }
3270 | member_initializer_list
3273 member_initializer_list
3274 : member_initializer
3276 ArrayList a = new ArrayList ();
3280 | member_initializer_list COMMA member_initializer
3282 ArrayList a = (ArrayList)$1;
3289 : IDENTIFIER ASSIGN initializer_value
3291 LocatedToken id = $1 as LocatedToken;
3292 IInitializable i = $3 as IInitializable;
3294 $$ = new Initializer (id.Value, i);
3296 $$ = new Initializer (id.Value, (Expression)$3);
3302 | object_or_collection_initializer
3305 collection_initializer
3306 : OPEN_BRACE element_initializer_list CLOSE_BRACE
3308 $$ = new CollectionInitializer ((ArrayList) $2);
3310 | OPEN_BRACE element_initializer_list COMMA CLOSE_BRACE
3312 $$ = new CollectionInitializer ((ArrayList) $2);
3316 element_initializer_list
3317 : element_initializer
3319 ArrayList a = new ArrayList ();
3323 | element_initializer_list COMMA element_initializer
3325 ArrayList a = $1 as ArrayList;
3332 : non_assignment_expression
3336 : /* empty */ { $$ = null; }
3343 ArrayList list = new ArrayList (4);
3347 | argument_list COMMA argument
3349 ArrayList list = (ArrayList) $1;
3353 | argument_list error {
3354 CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
3362 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
3364 | non_simple_argument
3371 : REF variable_reference
3373 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
3375 | OUT variable_reference
3377 $$ = new Argument ((Expression) $2, Argument.AType.Out);
3379 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
3381 ArrayList list = (ArrayList) $3;
3382 Argument[] args = new Argument [list.Count];
3383 list.CopyTo (args, 0);
3385 Expression expr = new Arglist (args, (Location) $1);
3386 $$ = new Argument (expr, Argument.AType.Expression);
3388 | ARGLIST OPEN_PARENS CLOSE_PARENS
3390 $$ = new Argument (new Arglist ((Location) $1), Argument.AType.Expression);
3394 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
3399 : expression { note ("section 5.4"); $$ = $1; }
3403 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
3405 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
3407 | primary_expression rank_specifiers
3409 // So the super-trick is that primary_expression
3410 // can only be either a SimpleName or a MemberAccess.
3411 // The MemberAccess case arises when you have a fully qualified type-name like :
3413 // SimpleName is when you have
3416 Expression expr = (Expression) $1;
3417 if (expr is ComposedCast){
3418 $$ = new ComposedCast (expr, (string) $2);
3419 } else if (!(expr is SimpleName || expr is MemberAccess || expr is ConstructedType || expr is QualifiedAliasMember)){
3420 Error_ExpectingTypeName (expr);
3421 $$ = TypeManager.system_object_expr;
3424 // So we extract the string corresponding to the SimpleName
3427 $$ = new ComposedCast (expr, (string) $2);
3429 current_array_type = (Expression)$$;
3436 ArrayList list = new ArrayList (4);
3440 | expression_list COMMA expression
3442 ArrayList list = (ArrayList) $1;
3451 $$ = new This (current_block, (Location) $1);
3456 : BASE DOT IDENTIFIER opt_type_argument_list
3458 LocatedToken lt = (LocatedToken) $3;
3459 $$ = new BaseAccess (lt.Value, (TypeArguments) $4, lt.Location);
3461 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
3463 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
3466 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
3471 post_increment_expression
3472 : primary_expression OP_INC
3474 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
3475 (Expression) $1, (Location) $2);
3479 post_decrement_expression
3480 : primary_expression OP_DEC
3482 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
3483 (Expression) $1, (Location) $2);
3488 : object_or_delegate_creation_expression
3489 | array_creation_expression
3490 | anonymous_type_expression
3493 object_or_delegate_creation_expression
3494 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS opt_object_or_collection_initializer
3497 if (RootContext.Version < LanguageVersion.LINQ) {
3498 Error_ExpectingTypeName ((Expression) $2);
3502 $$ = new NewInitialize ((Expression) $2, (ArrayList) $4, (IInitializable) $6, (Location) $1);
3505 $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
3507 | NEW type object_or_collection_initializer
3509 if (RootContext.Version < LanguageVersion.LINQ) {
3510 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
3514 $$ = new NewInitialize ((Expression) $2, new ArrayList (), (IInitializable) $3, (Location) $1);
3518 array_creation_expression
3519 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
3521 opt_array_initializer
3523 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
3525 | NEW type rank_specifiers array_initializer
3527 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, (Location) $1);
3529 | NEW rank_specifiers array_initializer
3531 if (RootContext.Version < LanguageVersion.LINQ) {
3532 Report.Error (1031, (Location) $1, "Type expected");
3536 $$ = new ImplicitlyTypedArrayCreation ((string) $2, (ArrayList) $3, (Location) $1);
3540 Report.Error (1031, (Location) $1, "Type expected");
3545 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
3550 anonymous_type_expression
3551 : NEW OPEN_BRACE anonymous_type_parameters CLOSE_BRACE
3553 if (RootContext.Version < LanguageVersion.LINQ) {
3554 Report.Error (1031, (Location) $1, "Type expected");
3558 $$ = new AnonymousType ((ArrayList) $3, current_container, lexer.Location);
3562 anonymous_type_parameters
3563 : anonymous_type_parameter
3565 ArrayList a = new ArrayList (4);
3569 | anonymous_type_parameters COMMA anonymous_type_parameter
3571 ArrayList a = (ArrayList) $1;
3577 anonymous_type_parameter
3578 : variable_declarator
3580 VariableDeclaration v = (VariableDeclaration) $1;
3581 Expression e = v.expression_or_array_initializer;
3583 e = new LocalVariableReference (current_block, v.identifier, lexer.Location);
3585 $$ = new AnonymousTypeParameter (e, v.identifier);
3589 MemberAccess ma = (MemberAccess) $1;
3590 $$ = new AnonymousTypeParameter (ma, ma.Identifier);
3605 opt_rank_specifier_or_nullable
3614 | opt_nullable rank_specifiers
3621 | opt_nullable rank_specifiers INTERR
3624 $$ = "?" + $2 + "?";
3631 : rank_specifier opt_rank_specifier
3633 $$ = (string) $2 + (string) $1;
3638 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3640 $$ = "[" + (string) $2 + "]";
3660 | dim_separators COMMA
3662 $$ = (string) $1 + ",";
3666 opt_array_initializer
3678 : OPEN_BRACE CLOSE_BRACE
3680 ArrayList list = new ArrayList (4);
3683 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3685 $$ = (ArrayList) $2;
3689 variable_initializer_list
3690 : variable_initializer
3692 ArrayList list = new ArrayList (4);
3696 | variable_initializer_list COMMA variable_initializer
3698 ArrayList list = (ArrayList) $1;
3707 pushed_current_array_type = current_array_type;
3708 lexer.TypeOfParsing = true;
3710 OPEN_PARENS typeof_type_expression CLOSE_PARENS
3712 lexer.TypeOfParsing = false;
3713 Expression type = (Expression)$4;
3714 if (type == TypeManager.system_void_expr)
3715 $$ = new TypeOfVoid ((Location) $1);
3717 $$ = new TypeOf (type, (Location) $1);
3718 current_array_type = pushed_current_array_type;
3722 typeof_type_expression
3729 $$ = new UnboundTypeExpression ((MemberName)$1, lexer.Location);
3734 : IDENTIFIER GENERIC_DIMENSION
3736 if (RootContext.Version == LanguageVersion.ISO_1)
3737 Report.FeatureIsNotISO1 (lexer.Location, "generics");
3739 LocatedToken lt = (LocatedToken) $1;
3740 TypeArguments ta = new TypeArguments ((int)$2, lt.Location);
3742 $$ = new MemberName (lt.Value, ta, lt.Location);
3744 | IDENTIFIER DOUBLE_COLON IDENTIFIER GENERIC_DIMENSION
3746 LocatedToken lt = (LocatedToken) $1;
3747 MemberName left = new MemberName (lt.Value, lt.Location);
3748 lt = (LocatedToken) $3;
3749 TypeArguments ta = new TypeArguments ((int)$4, lt.Location);
3751 $$ = new MemberName (left, lt.Value, ta, lt.Location);
3753 | unbound_type_name DOT IDENTIFIER GENERIC_DIMENSION
3755 LocatedToken lt = (LocatedToken) $3;
3756 TypeArguments ta = new TypeArguments ((int)$4, lt.Location);
3758 $$ = new MemberName ((MemberName)$1, lt.Value, ta, lt.Location);
3760 | namespace_or_type_name DOT IDENTIFIER GENERIC_DIMENSION
3762 LocatedToken lt = (LocatedToken) $3;
3763 TypeArguments ta = new TypeArguments ((int)$4, lt.Location);
3765 $$ = new MemberName ((MemberName)$1, lt.Value, ta, lt.Location);
3771 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3772 $$ = new SizeOf ((Expression) $3, (Location) $1);
3777 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3779 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3783 unchecked_expression
3784 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3786 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3790 pointer_member_access
3791 : primary_expression OP_PTR IDENTIFIER
3794 LocatedToken lt = (LocatedToken) $3;
3796 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
3797 $$ = new MemberAccess (deref, lt.Value);
3801 anonymous_method_expression
3802 : DELEGATE opt_anonymous_method_signature
3804 start_anonymous (false, (Parameters) $2, (Location) $1);
3808 $$ = end_anonymous ((ToplevelBlock) $4, (Location) $1);
3812 opt_anonymous_method_signature
3813 : /* empty */ { $$ = null; }
3814 | anonymous_method_signature
3817 anonymous_method_signature
3818 : open_parens opt_anonymous_method_parameter_list CLOSE_PARENS
3821 $$ = Parameters.EmptyReadOnlyParameters;
3823 ArrayList par_list = (ArrayList) $2;
3824 Parameter [] pars = new Parameter [par_list.Count];
3825 par_list.CopyTo (pars);
3826 $$ = new Parameters (pars);
3831 opt_anonymous_method_parameter_list
3832 : /* empty */ { $$ = null; }
3833 | anonymous_method_parameter_list { $$ = $1; }
3836 anonymous_method_parameter_list
3837 : anonymous_method_parameter
3839 ArrayList a = new ArrayList (4);
3843 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3845 ArrayList a = (ArrayList) $1;
3851 anonymous_method_parameter
3852 : opt_parameter_modifier type IDENTIFIER {
3853 LocatedToken lt = (LocatedToken) $3;
3854 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3856 | PARAMS type IDENTIFIER {
3857 Report.Error (1670, ((LocatedToken) $3).Location, "The `params' modifier is not allowed in anonymous method declaration");
3862 default_value_expression
3863 : DEFAULT_OPEN_PARENS type CLOSE_PARENS
3865 $$ = new DefaultValueExpression ((Expression) $2, lexer.Location);
3870 : primary_expression
3871 | BANG prefixed_unary_expression
3873 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3875 | TILDE prefixed_unary_expression
3877 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3883 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3885 $$ = new Cast ((Expression) $1, (Expression) $3);
3887 | parenthesized_expression_0 CLOSE_PARENS_NO_CAST default_value_expression
3889 $$ = new Cast ((Expression) $1, (Expression) $3);
3891 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3893 $$ = new Cast ((Expression) $1, (Expression) $3);
3899 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3901 // TODO: wrong location
3902 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3907 // The idea to split this out is from Rhys' grammar
3908 // to solve the problem with casts.
3910 prefixed_unary_expression
3912 | PLUS prefixed_unary_expression
3914 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3916 | MINUS prefixed_unary_expression
3918 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3920 | OP_INC prefixed_unary_expression
3922 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3923 (Expression) $2, (Location) $1);
3925 | OP_DEC prefixed_unary_expression
3927 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3928 (Expression) $2, (Location) $1);
3930 | STAR prefixed_unary_expression
3932 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, (Location) $1);
3934 | BITWISE_AND prefixed_unary_expression
3936 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3940 pre_increment_expression
3941 : OP_INC prefixed_unary_expression
3943 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3944 (Expression) $2, (Location) $1);
3948 pre_decrement_expression
3949 : OP_DEC prefixed_unary_expression
3951 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3952 (Expression) $2, (Location) $1);
3956 multiplicative_expression
3957 : prefixed_unary_expression
3958 | multiplicative_expression STAR prefixed_unary_expression
3960 $$ = new Binary (Binary.Operator.Multiply,
3961 (Expression) $1, (Expression) $3);
3963 | multiplicative_expression DIV prefixed_unary_expression
3965 $$ = new Binary (Binary.Operator.Division,
3966 (Expression) $1, (Expression) $3);
3968 | multiplicative_expression PERCENT prefixed_unary_expression
3970 $$ = new Binary (Binary.Operator.Modulus,
3971 (Expression) $1, (Expression) $3);
3976 : multiplicative_expression
3977 | additive_expression PLUS multiplicative_expression
3979 $$ = new Binary (Binary.Operator.Addition,
3980 (Expression) $1, (Expression) $3);
3982 | additive_expression MINUS multiplicative_expression
3984 $$ = new Binary (Binary.Operator.Subtraction,
3985 (Expression) $1, (Expression) $3);
3990 : additive_expression
3991 | shift_expression OP_SHIFT_LEFT additive_expression
3993 $$ = new Binary (Binary.Operator.LeftShift,
3994 (Expression) $1, (Expression) $3);
3996 | shift_expression OP_SHIFT_RIGHT additive_expression
3998 $$ = new Binary (Binary.Operator.RightShift,
3999 (Expression) $1, (Expression) $3);
4010 lexer.PutbackNullable ();
4015 nullable_type_or_conditional
4018 if (((bool) $2) && ($1 is ComposedCast))
4019 $$ = ((ComposedCast) $1).RemoveNullable ();
4025 relational_expression
4027 | relational_expression OP_LT shift_expression
4029 $$ = new Binary (Binary.Operator.LessThan,
4030 (Expression) $1, (Expression) $3);
4032 | relational_expression OP_GT shift_expression
4034 $$ = new Binary (Binary.Operator.GreaterThan,
4035 (Expression) $1, (Expression) $3);
4037 | relational_expression OP_LE shift_expression
4039 $$ = new Binary (Binary.Operator.LessThanOrEqual,
4040 (Expression) $1, (Expression) $3);
4042 | relational_expression OP_GE shift_expression
4044 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
4045 (Expression) $1, (Expression) $3);
4047 | relational_expression IS
4050 } nullable_type_or_conditional
4052 $$ = new Is ((Expression) $1, (Expression) $4, (Location) $2);
4054 | relational_expression AS
4057 } nullable_type_or_conditional
4059 $$ = new As ((Expression) $1, (Expression) $4, (Location) $2);
4064 : relational_expression
4065 | equality_expression OP_EQ relational_expression
4067 $$ = new Binary (Binary.Operator.Equality,
4068 (Expression) $1, (Expression) $3);
4070 | equality_expression OP_NE relational_expression
4072 $$ = new Binary (Binary.Operator.Inequality,
4073 (Expression) $1, (Expression) $3);
4078 : equality_expression
4079 | and_expression BITWISE_AND equality_expression
4081 $$ = new Binary (Binary.Operator.BitwiseAnd,
4082 (Expression) $1, (Expression) $3);
4086 exclusive_or_expression
4088 | exclusive_or_expression CARRET and_expression
4090 $$ = new Binary (Binary.Operator.ExclusiveOr,
4091 (Expression) $1, (Expression) $3);
4095 inclusive_or_expression
4096 : exclusive_or_expression
4097 | inclusive_or_expression BITWISE_OR exclusive_or_expression
4099 $$ = new Binary (Binary.Operator.BitwiseOr,
4100 (Expression) $1, (Expression) $3);
4104 conditional_and_expression
4105 : inclusive_or_expression
4106 | conditional_and_expression OP_AND inclusive_or_expression
4108 $$ = new Binary (Binary.Operator.LogicalAnd,
4109 (Expression) $1, (Expression) $3);
4113 conditional_or_expression
4114 : conditional_and_expression
4115 | conditional_or_expression OP_OR conditional_and_expression
4117 $$ = new Binary (Binary.Operator.LogicalOr,
4118 (Expression) $1, (Expression) $3);
4122 conditional_expression
4123 : conditional_or_expression
4124 | conditional_or_expression INTERR expression COLON expression
4126 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
4128 | conditional_or_expression OP_COALESCING expression
4130 $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $3, lexer.Location);
4132 // We'll be resolved into a `parenthesized_expression_0' later on.
4133 | conditional_or_expression INTERR CLOSE_PARENS
4135 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
4136 lexer.PutbackCloseParens ();
4140 assignment_expression
4141 : prefixed_unary_expression ASSIGN expression
4143 $$ = new Assign ((Expression) $1, (Expression) $3);
4145 | prefixed_unary_expression OP_MULT_ASSIGN expression
4147 $$ = new CompoundAssign (
4148 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
4150 | prefixed_unary_expression OP_DIV_ASSIGN expression
4152 $$ = new CompoundAssign (
4153 Binary.Operator.Division, (Expression) $1, (Expression) $3);
4155 | prefixed_unary_expression OP_MOD_ASSIGN expression
4157 $$ = new CompoundAssign (
4158 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
4160 | prefixed_unary_expression OP_ADD_ASSIGN expression
4162 $$ = new CompoundAssign (
4163 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
4165 | prefixed_unary_expression OP_SUB_ASSIGN expression
4167 $$ = new CompoundAssign (
4168 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
4170 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
4172 $$ = new CompoundAssign (
4173 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
4175 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
4177 $$ = new CompoundAssign (
4178 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
4180 | prefixed_unary_expression OP_AND_ASSIGN expression
4182 $$ = new CompoundAssign (
4183 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
4185 | prefixed_unary_expression OP_OR_ASSIGN expression
4187 $$ = new CompoundAssign (
4188 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
4190 | prefixed_unary_expression OP_XOR_ASSIGN expression
4192 $$ = new CompoundAssign (
4193 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
4197 implicitly_typed_lambda_parameter_list
4199 LocatedToken lt = (LocatedToken) $1;
4200 ArrayList a = new ArrayList (4);
4202 a.Add (new Parameter ((Expression)null, lt.Value, Parameter.Modifier.NONE, null, lt.Location));
4205 | implicitly_typed_lambda_parameter_list COMMA IDENTIFIER {
4206 LocatedToken lt = (LocatedToken) $3;
4207 ArrayList a = (ArrayList) $1;
4208 a.Add (new Parameter ((Expression)null, lt.Value, Parameter.Modifier.NONE, null, lt.Location));
4213 explicitly_typed_lambda_parameter_list
4214 : explicitly_typed_lambda_parameter
4216 ArrayList pars = new ArrayList (4);
4221 | explicitly_typed_lambda_parameter_list COMMA explicitly_typed_lambda_parameter
4223 ArrayList pars = (ArrayList) $1;
4230 explicitly_typed_lambda_parameter
4231 : parameter_modifier type IDENTIFIER
4233 LocatedToken lt = (LocatedToken) $3;
4235 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
4239 LocatedToken lt = (LocatedToken) $2;
4241 $$ = new Parameter ((Expression) $1, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
4245 lambda_parameter_list
4246 : implicitly_typed_lambda_parameter_list { $$ = $1; }
4247 | explicitly_typed_lambda_parameter_list { $$ = $1; }
4250 opt_lambda_parameter_list
4251 : /* empty */ { $$ = null; }
4252 | lambda_parameter_list {
4253 ArrayList pars_list = (ArrayList) $1;
4255 Parameter [] pars = new Parameter [pars_list.Count];
4256 pars_list.CopyTo (pars);
4257 $$ = new Parameters (pars);
4261 lambda_expression_body
4263 start_block (lexer.Location);
4267 Block b = end_block (lexer.Location);
4268 b.AddStatement (new ContextualReturn ((Expression) $2));
4279 LocatedToken lt = (LocatedToken) $1;
4280 Parameter p = new ImplicitLambdaParameter (lt.Value, lt.Location);
4281 start_anonymous (true, new Parameters (p), (Location) $2);
4283 lambda_expression_body
4285 $$ = end_anonymous ((ToplevelBlock) $4, (Location) $2);
4287 | OPEN_PARENS_LAMBDA opt_lambda_parameter_list CLOSE_PARENS ARROW
4289 start_anonymous (true, (Parameters) $2, (Location) $4);
4291 lambda_expression_body
4293 $$ = end_anonymous ((ToplevelBlock) $6, (Location) $4);
4298 : assignment_expression
4299 | non_assignment_expression
4302 non_assignment_expression
4303 : conditional_expression
4325 lexer.ConstraintsParsing = true;
4329 MemberName name = MakeName ((MemberName) $6);
4330 int mod_flags = (int) $2;
4332 push_current_class (new Class (
4333 current_namespace, current_class, name,
4334 mod_flags, (Attributes) $1), $3);
4337 opt_type_parameter_constraints_clauses
4339 lexer.ConstraintsParsing = false;
4342 if (current_class.Name == "System.Object") {
4343 Report.Error (537, current_class.Location,
4344 "The class System.Object cannot have a base " +
4345 "class or implement an interface.");
4347 current_container.AddBasesForPart (current_class, (ArrayList) $8);
4350 current_class.SetParameterInfo ((ArrayList) $9);
4352 if (RootContext.Documentation != null) {
4353 current_container.DocComment = Lexer.consume_doc_comment ();
4354 Lexer.doc_state = XmlCommentState.Allowed;
4359 if (RootContext.Documentation != null)
4360 Lexer.doc_state = XmlCommentState.Allowed;
4364 $$ = pop_current_class ();
4372 { $$ = $1; } // location
4376 : /* empty */ { $$ = (int) 0; }
4382 | modifiers modifier
4387 if ((m1 & m2) != 0) {
4388 Location l = lexer.Location;
4389 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
4391 $$ = (int) (m1 | m2);
4396 : NEW { $$ = Modifiers.NEW; }
4397 | PUBLIC { $$ = Modifiers.PUBLIC; }
4398 | PROTECTED { $$ = Modifiers.PROTECTED; }
4399 | INTERNAL { $$ = Modifiers.INTERNAL; }
4400 | PRIVATE { $$ = Modifiers.PRIVATE; }
4401 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
4402 | SEALED { $$ = Modifiers.SEALED; }
4403 | STATIC { $$ = Modifiers.STATIC; }
4404 | READONLY { $$ = Modifiers.READONLY; }
4405 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
4406 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
4407 | EXTERN { $$ = Modifiers.EXTERN; }
4408 | VOLATILE { $$ = Modifiers.VOLATILE; }
4409 | UNSAFE { $$ = Modifiers.UNSAFE; }
4413 : /* empty */ { $$ = null; }
4414 | class_base { $$ = $1; }
4418 : COLON type_list { $$ = $2; }
4421 opt_type_parameter_constraints_clauses
4422 : /* empty */ { $$ = null; }
4423 | type_parameter_constraints_clauses
4427 type_parameter_constraints_clauses
4428 : type_parameter_constraints_clause {
4429 ArrayList constraints = new ArrayList (1);
4430 constraints.Add ($1);
4433 | type_parameter_constraints_clauses type_parameter_constraints_clause {
4434 ArrayList constraints = (ArrayList) $1;
4435 Constraints new_constraint = (Constraints)$2;
4437 foreach (Constraints c in constraints) {
4438 if (new_constraint.TypeParameter == c.TypeParameter) {
4439 Report.Error (409, new_constraint.Location, "A constraint clause has already been specified for type parameter `{0}'",
4440 new_constraint.TypeParameter);
4444 constraints.Add (new_constraint);
4449 type_parameter_constraints_clause
4450 : WHERE IDENTIFIER COLON type_parameter_constraints {
4451 LocatedToken lt = (LocatedToken) $2;
4452 $$ = new Constraints (lt.Value, (ArrayList) $4, lt.Location);
4456 type_parameter_constraints
4457 : type_parameter_constraint {
4458 ArrayList constraints = new ArrayList (1);
4459 constraints.Add ($1);
4462 | type_parameter_constraints COMMA type_parameter_constraint {
4463 ArrayList constraints = (ArrayList) $1;
4465 constraints.Add ($3);
4470 type_parameter_constraint
4472 | NEW OPEN_PARENS CLOSE_PARENS {
4473 $$ = SpecialConstraint.Constructor;
4476 $$ = SpecialConstraint.ReferenceType;
4479 $$ = SpecialConstraint.ValueType;
4488 // A block is "contained" on the following places:
4490 // property_declaration as part of the accessor body (get/set)
4491 // operator_declaration
4492 // constructor_declaration
4493 // destructor_declaration
4494 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
4499 start_block ((Location) $1);
4501 opt_statement_list CLOSE_BRACE
4503 $$ = end_block ((Location) $4);
4509 opt_statement_list CLOSE_BRACE
4511 $$ = end_block ((Location) $3);
4522 | statement_list statement
4526 : declaration_statement
4528 if ($1 != null && (Block) $1 != current_block){
4529 current_block.AddStatement ((Statement) $1);
4530 current_block = (Block) $1;
4533 | valid_declaration_statement
4535 current_block.AddStatement ((Statement) $1);
4540 valid_declaration_statement
4543 | expression_statement
4544 | selection_statement
4545 | iteration_statement
4549 | unchecked_statement
4557 : valid_declaration_statement
4558 | declaration_statement
4560 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4565 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4573 $$ = EmptyStatement.Value;
4580 LocatedToken lt = (LocatedToken) $1;
4581 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
4583 if (current_block.AddLabel (labeled))
4584 current_block.AddStatement (labeled);
4589 declaration_statement
4590 : local_variable_declaration SEMICOLON
4592 current_array_type = null;
4594 DictionaryEntry de = (DictionaryEntry) $1;
4595 Expression e = (Expression) de.Key;
4597 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
4601 | local_constant_declaration SEMICOLON
4603 current_array_type = null;
4605 DictionaryEntry de = (DictionaryEntry) $1;
4607 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
4613 * The following is from Rhys' grammar:
4614 * > Types in local variable declarations must be recognized as
4615 * > expressions to prevent reduce/reduce errors in the grammar.
4616 * > The expressions are converted into types during semantic analysis.
4619 : primary_expression opt_rank_specifier_or_nullable
4621 // FIXME: Do something smart here regarding the composition of the type.
4623 // Ok, the above "primary_expression" is there to get rid of
4624 // both reduce/reduce and shift/reduces in the grammar, it should
4625 // really just be "type_name". If you use type_name, a reduce/reduce
4626 // creeps up. If you use namespace_or_type_name (which is all we need
4627 // really) two shift/reduces appear.
4630 // So the super-trick is that primary_expression
4631 // can only be either a SimpleName or a MemberAccess.
4632 // The MemberAccess case arises when you have a fully qualified type-name like :
4634 // SimpleName is when you have
4637 Expression expr = (Expression) $1;
4638 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType || expr is QualifiedAliasMember)) {
4639 Error_ExpectingTypeName (expr);
4643 // So we extract the string corresponding to the SimpleName
4647 if ((string) $2 == "")
4650 $$ = new ComposedCast ((Expression) $1, (string) $2);
4653 | builtin_types opt_rank_specifier_or_nullable
4655 if ((string) $2 == "")
4658 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
4662 local_variable_pointer_type
4663 : primary_expression STAR
4665 Expression expr = (Expression) $1;
4667 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType || expr is QualifiedAliasMember)) {
4668 Error_ExpectingTypeName (expr);
4672 $$ = new ComposedCast ((Expression) $1, "*");
4674 | builtin_types STAR
4676 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
4680 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
4682 | local_variable_pointer_type STAR
4684 $$ = new ComposedCast ((Expression) $1, "*");
4688 local_variable_declaration
4689 : local_variable_type variable_declarators
4692 VarExpr ve = $1 as VarExpr;
4694 ve.VariableInitializer = (ArrayList)$2;
4696 $$ = new DictionaryEntry ($1, $2);
4700 | local_variable_pointer_type opt_rank_specifier_or_nullable variable_declarators
4705 if ((string) $2 == "")
4706 t = (Expression) $1;
4708 t = new ComposedCast ((Expression) $1, (string) $2);
4709 $$ = new DictionaryEntry (t, $3);
4715 local_constant_declaration
4716 : CONST local_variable_type constant_declarators
4719 $$ = new DictionaryEntry ($2, $3);
4725 expression_statement
4726 : statement_expression SEMICOLON { $$ = $1; }
4730 // We have to do the wrapping here and not in the case above,
4731 // because statement_expression is used for example in for_statement
4733 statement_expression
4736 Expression expr = (Expression) $1;
4737 ExpressionStatement s = expr as ExpressionStatement;
4739 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
4742 $$ = new StatementExpression (s);
4746 Report.Error (1002, GetLocation ($1), "Expecting `;'");
4751 object_creation_expression
4752 : object_or_delegate_creation_expression
4753 { note ("complain if this is a delegate maybe?"); }
4762 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
4765 Location l = (Location) $1;
4767 $$ = new If ((Expression) $3, (Statement) $5, l);
4769 // FIXME: location for warning should be loc property of $5.
4770 if ($5 == EmptyStatement.Value)
4771 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4774 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
4775 embedded_statement ELSE embedded_statement
4777 Location l = (Location) $1;
4779 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
4781 // FIXME: location for warning should be loc property of $5 and $7.
4782 if ($5 == EmptyStatement.Value)
4783 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4784 if ($7 == EmptyStatement.Value)
4785 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4790 : SWITCH OPEN_PARENS
4792 if (switch_stack == null)
4793 switch_stack = new Stack (2);
4794 switch_stack.Push (current_block);
4796 expression CLOSE_PARENS
4799 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
4800 current_block = (Block) switch_stack.Pop ();
4816 Report.Warning (1522, 1, lexer.Location, "Empty switch block");
4817 $$ = new ArrayList ();
4825 ArrayList sections = new ArrayList (4);
4830 | switch_sections switch_section
4832 ArrayList sections = (ArrayList) $1;
4842 current_block = current_block.CreateSwitchBlock (lexer.Location);
4846 $$ = new SwitchSection ((ArrayList) $1, current_block.Explicit);
4853 ArrayList labels = new ArrayList (4);
4858 | switch_labels switch_label
4860 ArrayList labels = (ArrayList) ($1);
4868 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
4869 | DEFAULT_COLON { $$ = new SwitchLabel (null, (Location) $1); }
4872 1523, GetLocation ($1),
4873 "The keyword case or default must precede code in switch block");
4885 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
4887 Location l = (Location) $1;
4888 $$ = new While ((Expression) $3, (Statement) $5, l);
4893 : DO embedded_statement
4894 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
4896 Location l = (Location) $1;
4898 $$ = new Do ((Statement) $2, (Expression) $5, l);
4904 opt_for_initializer SEMICOLON
4906 Location l = lexer.Location;
4908 Block assign_block = current_block;
4910 if ($3 is DictionaryEntry){
4911 DictionaryEntry de = (DictionaryEntry) $3;
4913 Expression type = (Expression) de.Key;
4914 ArrayList var_declarators = (ArrayList) de.Value;
4916 foreach (VariableDeclaration decl in var_declarators){
4920 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4924 Expression expr = decl.expression_or_array_initializer;
4926 LocalVariableReference var;
4927 var = new LocalVariableReference (assign_block, decl.identifier, l);
4930 Assign a = new Assign (var, expr, decl.Location);
4932 assign_block.AddStatement (new StatementExpression (a));
4936 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4937 // This can be referred to as $5 below.
4943 opt_for_condition SEMICOLON
4944 opt_for_iterator CLOSE_PARENS
4947 Location l = (Location) $1;
4949 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4951 current_block.AddStatement (f);
4953 $$ = end_block (lexer.Location);
4958 : /* empty */ { $$ = EmptyStatement.Value; }
4963 : local_variable_declaration
4964 | statement_expression_list
4968 : /* empty */ { $$ = null; }
4969 | boolean_expression
4973 : /* empty */ { $$ = EmptyStatement.Value; }
4978 : statement_expression_list
4981 statement_expression_list
4982 : statement_expression
4984 // CHANGE: was `null'
4985 Statement s = (Statement) $1;
4986 Block b = new Block (current_block, s.loc, lexer.Location);
4991 | statement_expression_list COMMA statement_expression
4993 Block b = (Block) $1;
4995 b.AddStatement ((Statement) $3);
5001 : FOREACH open_parens type IN expression CLOSE_PARENS
5003 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
5006 | FOREACH open_parens type IDENTIFIER IN
5007 expression CLOSE_PARENS
5009 start_block (lexer.Location);
5010 Block foreach_block = current_block;
5012 LocatedToken lt = (LocatedToken) $4;
5013 Location l = lt.Location;
5014 LocalInfo vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
5016 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
5018 // Get a writable reference to this read-only variable.
5020 // Note that the $$ here refers to the value of _this_ code block,
5021 // not the value of the LHS non-terminal. This can be referred to as $8 below.
5022 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
5029 LocalVariableReference v = (LocalVariableReference) $8;
5030 Location l = (Location) $1;
5033 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
5034 current_block.AddStatement (f);
5037 $$ = end_block (lexer.Location);
5043 | continue_statement
5053 $$ = new Break ((Location) $1);
5058 : CONTINUE SEMICOLON
5060 $$ = new Continue ((Location) $1);
5065 : GOTO IDENTIFIER SEMICOLON
5067 LocatedToken lt = (LocatedToken) $2;
5068 $$ = new Goto (lt.Value, lt.Location);
5070 | GOTO CASE constant_expression SEMICOLON
5072 $$ = new GotoCase ((Expression) $3, (Location) $1);
5074 | GOTO DEFAULT SEMICOLON
5076 $$ = new GotoDefault ((Location) $1);
5081 : RETURN opt_expression SEMICOLON
5083 $$ = new Return ((Expression) $2, (Location) $1);
5088 : THROW opt_expression SEMICOLON
5090 $$ = new Throw ((Expression) $2, (Location) $1);
5095 : IDENTIFIER RETURN expression SEMICOLON
5097 LocatedToken lt = (LocatedToken) $1;
5098 string s = lt.Value;
5100 Report.Error (1003, lt.Location, "; expected");
5103 if (RootContext.Version == LanguageVersion.ISO_1){
5104 Report.FeatureIsNotISO1 (lt.Location, "yield statement");
5107 if (anonymous_host == null){
5108 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
5111 anonymous_host.SetYields ();
5112 $$ = new Yield ((Expression) $3, lt.Location);
5115 | IDENTIFIER RETURN SEMICOLON
5117 Report.Error (1627, (Location) $2, "Expression expected after yield return");
5120 | IDENTIFIER BREAK SEMICOLON
5122 LocatedToken lt = (LocatedToken) $1;
5123 string s = lt.Value;
5125 Report.Error (1003, lt.Location, "; expected");
5128 if (RootContext.Version == LanguageVersion.ISO_1){
5129 Report.FeatureIsNotISO1 (lt.Location, "yield statement");
5132 if (anonymous_host == null){
5133 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
5136 anonymous_host.SetYields ();
5137 $$ = new YieldBreak (lt.Location);
5148 : TRY block catch_clauses
5152 ArrayList c = (ArrayList)$3;
5153 for (int i = 0; i < c.Count; ++i) {
5154 Catch cc = (Catch) c [i];
5156 if (i != c.Count - 1)
5157 Report.Error (1017, cc.loc, "Try statement already has an empty catch block");
5164 // Now s contains the list of specific catch clauses
5165 // and g contains the general one.
5167 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
5169 | TRY block opt_catch_clauses FINALLY block
5172 ArrayList s = new ArrayList (4);
5173 ArrayList catch_list = (ArrayList) $3;
5175 if (catch_list != null){
5176 foreach (Catch cc in catch_list) {
5184 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
5188 Report.Error (1524, (Location) $1, "Expected catch or finally");
5194 : /* empty */ { $$ = null; }
5201 ArrayList l = new ArrayList (4);
5206 | catch_clauses catch_clause
5208 ArrayList l = (ArrayList) $1;
5216 : /* empty */ { $$ = null; }
5221 : CATCH opt_catch_args
5223 Expression type = null;
5226 DictionaryEntry cc = (DictionaryEntry) $2;
5227 type = (Expression) cc.Key;
5228 LocatedToken lt = (LocatedToken) cc.Value;
5231 ArrayList one = new ArrayList (4);
5233 one.Add (new VariableDeclaration (lt, null));
5235 start_block (lexer.Location);
5236 current_block = declare_local_variables (type, one, lt.Location);
5240 Expression type = null;
5242 Block var_block = null;
5245 DictionaryEntry cc = (DictionaryEntry) $2;
5246 type = (Expression) cc.Key;
5247 LocatedToken lt = (LocatedToken) cc.Value;
5251 var_block = end_block (lexer.Location);
5255 $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
5260 : /* empty */ { $$ = null; }
5265 : open_parens type opt_identifier CLOSE_PARENS
5267 $$ = new DictionaryEntry ($2, $3);
5275 $$ = new Checked ((Block) $2);
5282 $$ = new Unchecked ((Block) $2);
5289 RootContext.CheckUnsafeOption ((Location) $1);
5291 $$ = new Unsafe ((Block) $3);
5297 type fixed_pointer_declarators
5300 ArrayList list = (ArrayList) $4;
5301 Expression type = (Expression) $3;
5302 Location l = (Location) $1;
5303 int top = list.Count;
5305 if (type is VarExpr)
5306 Report.Error (821, GetLocation ($3), "A fixed statement cannot use an implicitly typed local variable");
5308 start_block (lexer.Location);
5310 for (int i = 0; i < top; i++){
5311 Pair p = (Pair) list [i];
5314 v = current_block.AddVariable (type, (string) p.First, l);
5318 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
5326 Location l = (Location) $1;
5328 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
5330 current_block.AddStatement (f);
5332 $$ = end_block (lexer.Location);
5336 fixed_pointer_declarators
5337 : fixed_pointer_declarator {
5338 ArrayList declarators = new ArrayList (4);
5340 declarators.Add ($1);
5343 | fixed_pointer_declarators COMMA fixed_pointer_declarator
5345 ArrayList declarators = (ArrayList) $1;
5347 declarators.Add ($3);
5352 fixed_pointer_declarator
5353 : IDENTIFIER ASSIGN expression
5355 LocatedToken lt = (LocatedToken) $1;
5356 // FIXME: keep location
5357 $$ = new Pair (lt.Value, $3);
5361 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
5367 : LOCK OPEN_PARENS expression CLOSE_PARENS
5373 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
5378 : USING open_parens resource_acquisition CLOSE_PARENS
5380 start_block (lexer.Location);
5381 Block assign_block = current_block;
5383 if ($3 is DictionaryEntry){
5384 DictionaryEntry de = (DictionaryEntry) $3;
5385 Location l = (Location) $1;
5387 Expression type = (Expression) de.Key;
5388 ArrayList var_declarators = (ArrayList) de.Value;
5390 ArrayList vars = new ArrayList (4);
5392 foreach (VariableDeclaration decl in var_declarators){
5394 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
5397 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
5399 Expression expr = decl.expression_or_array_initializer;
5401 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
5404 LocalVariableReference var;
5406 // Get a writable reference to this read-only variable.
5407 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
5409 // This is so that it is not a warning on using variables
5412 vars.Add (new DictionaryEntry (var, expr));
5414 // Assign a = new Assign (var, expr, decl.Location);
5415 // assign_block.AddStatement (new StatementExpression (a));
5418 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
5419 // It can be referred to as $5 below.
5420 $$ = new DictionaryEntry (type, vars);
5427 Using u = new Using ($5, (Statement) $6, (Location) $1);
5428 current_block.AddStatement (u);
5429 $$ = end_block (lexer.Location);
5433 resource_acquisition
5434 : local_variable_declaration
5442 : from_clause query_body
5444 lexer.QueryParsing = false;
5445 $$ = new Linq.QueryExpression (current_container, current_block, (Expression)$1, (Linq.AQueryClause)$2);
5446 end_block (lexer.Location);
5451 : FROM IDENTIFIER IN expression opt_join_clauses
5453 start_block (lexer.Location);
5454 LocatedToken lt = (LocatedToken) $2;
5456 current_block.AddVariable (new VarExpr ("var", lt.Location), lt.Value, lt.Location);
5459 | FROM type IDENTIFIER IN expression opt_join_clauses
5461 start_block (lexer.Location);
5462 LocatedToken lt = (LocatedToken) $3;
5464 Expression type = (Expression)$2;
5465 current_block.AddVariable (type, lt.Value, lt.Location);
5466 $$ = new Linq.Cast (type, (Expression)$5, lt.Location);
5477 | join_clauses join_clause
5481 : JOIN join_clause_body
5482 | JOIN join_clause_body INTO IDENTIFIER
5483 | JOIN type join_clause_body
5484 | JOIN type join_clause_body INTO IDENTIFIER
5488 : IDENTIFIER IN expression ON expression EQUALS expression
5492 : opt_from_let_where_clauses opt_orderby_clause SELECT expression opt_query_continuation
5494 Linq.AQueryClause ret = new Linq.Select ((Expression)$4, GetLocation ($3));
5497 ((Linq.AQueryClause)$1).Next = ret;
5498 ret = (Linq.AQueryClause)$1;
5503 | opt_from_let_where_clauses opt_orderby_clause group_clause opt_query_continuation
5505 Linq.AQueryClause ret = (Linq.AQueryClause)$3;
5510 opt_from_let_where_clauses
5512 | from_let_where_clauses
5515 from_let_where_clauses
5516 : from_let_where_clause
5517 | from_let_where_clauses from_let_where_clause
5520 from_let_where_clause
5527 : LET IDENTIFIER ASSIGN expression
5531 : WHERE boolean_expression
5533 $$ = new Linq.Where ((Expression)$2, GetLocation ($1));
5548 | orderings COMMA ordering
5553 | expression ASCENDING
5554 | expression DESCENDING
5559 : GROUP expression BY expression
5561 $$ = new Linq.GroupBy ((Expression)$2, (Expression)$4, GetLocation ($1));
5565 opt_query_continuation
5567 | query_continuation
5571 : INTO IDENTIFIER opt_join_clauses query_body
5577 // A class used to pass around variable declarations and constants
5579 public class VariableDeclaration {
5580 public string identifier;
5581 public Expression expression_or_array_initializer;
5582 public Location Location;
5583 public Attributes OptAttributes;
5584 public string DocComment;
5586 public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
5588 this.identifier = lt.Value;
5589 if (eoai is ArrayList) {
5590 this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
5592 this.expression_or_array_initializer = (Expression)eoai;
5594 this.Location = lt.Location;
5595 this.OptAttributes = opt_attrs;
5598 public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
5604 // A class used to hold info about an indexer declarator
5606 public class IndexerDeclaration {
5607 public Expression type;
5608 public MemberName interface_type;
5609 public Parameters param_list;
5610 public Location location;
5612 public IndexerDeclaration (Expression type, MemberName interface_type,
5613 Parameters param_list, Location loc)
5616 this.interface_type = interface_type;
5617 this.param_list = param_list;
5618 this.location = loc;
5623 // We use this when we do not have an object in advance that is an IAnonymousHost
5625 public class SimpleAnonymousHost : IAnonymousHost {
5626 public static readonly SimpleAnonymousHost Simple = new SimpleAnonymousHost ();
5629 ArrayList anonymous_methods;
5631 public static SimpleAnonymousHost GetSimple () {
5632 Simple.yields = false;
5633 Simple.anonymous_methods = null;
5637 public void SetYields ()
5642 public void AddAnonymousMethod (AnonymousMethodExpression anonymous)
5644 if (anonymous_methods == null)
5645 anonymous_methods = new ArrayList ();
5646 anonymous_methods.Add (anonymous);
5649 public void Propagate (IAnonymousHost real_host)
5652 real_host.SetYields ();
5653 if (anonymous_methods != null) {
5654 foreach (AnonymousMethodExpression ame in anonymous_methods)
5655 real_host.AddAnonymousMethod (ame);
5661 // A class used to hold info about an operator declarator
5663 public class OperatorDeclaration {
5664 public Operator.OpType optype;
5665 public Expression ret_type, arg1type, arg2type;
5666 public string arg1name, arg2name;
5667 public Location location;
5669 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
5670 Expression arg1type, string arg1name,
5671 Expression arg2type, string arg2name, Location location)
5674 this.ret_type = ret_type;
5675 this.arg1type = arg1type;
5676 this.arg1name = arg1name;
5677 this.arg2type = arg2type;
5678 this.arg2name = arg2name;
5679 this.location = location;
5684 void Error_ExpectingTypeName (Expression expr)
5686 if (expr is Invocation){
5687 Report.Error (1002, expr.Location, "Expecting `;'");
5689 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
5693 public static void Error_ParameterModifierNotValid (Location loc)
5695 Report.Error (631, loc, "The modifiers `ref' and `out' are not valid in this context");
5698 static void Error_DuplicateParameterModifier (Location loc, Parameter.Modifier mod)
5700 Report.Error (1107, loc, "Duplicate parameter modifier `{0}'",
5701 Parameter.GetModifierSignature (mod));
5704 static void Error_TypeExpected (Location loc)
5706 Report.Error (1031, loc, "Type expected");
5709 void push_current_class (TypeContainer tc, object partial_token)
5711 if (partial_token != null)
5712 current_container = current_container.AddPartial (tc);
5714 current_container = current_container.AddTypeContainer (tc);
5718 DeclSpace pop_current_class ()
5720 DeclSpace retval = current_class;
5722 current_class = current_class.Parent;
5723 current_container = current_class.PartialContainer;
5729 // Given the @class_name name, it creates a fully qualified name
5730 // based on the containing declaration space
5733 MakeName (MemberName class_name)
5735 Namespace ns = current_namespace.NS;
5737 if (current_container.Name.Length == 0){
5738 if (ns.Name.Length != 0)
5739 return new MemberName (ns.MemberName, class_name);
5743 return new MemberName (current_container.MemberName, class_name);
5747 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
5749 Block implicit_block;
5750 ArrayList inits = null;
5753 // We use the `Used' property to check whether statements
5754 // have been added to the current block. If so, we need
5755 // to create another block to contain the new declaration
5756 // otherwise, as an optimization, we use the same block to
5757 // add the declaration.
5759 // FIXME: A further optimization is to check if the statements
5760 // that were added were added as part of the initialization
5761 // below. In which case, no other statements have been executed
5762 // and we might be able to reduce the number of blocks for
5763 // situations like this:
5765 // int j = 1; int k = j + 1;
5767 if (current_block.Used)
5768 implicit_block = new Block (current_block, loc, Location.Null);
5770 implicit_block = current_block;
5772 foreach (VariableDeclaration decl in variable_declarators){
5774 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
5775 if (decl.expression_or_array_initializer != null){
5777 inits = new ArrayList (4);
5784 return implicit_block;
5786 foreach (VariableDeclaration decl in inits){
5788 Expression expr = decl.expression_or_array_initializer;
5790 LocalVariableReference var;
5791 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
5793 assign = new Assign (var, expr, decl.Location);
5795 implicit_block.AddStatement (new StatementExpression (assign));
5798 return implicit_block;
5801 Block declare_local_constants (Expression type, ArrayList declarators)
5803 Block implicit_block;
5805 if (current_block.Used)
5806 implicit_block = new Block (current_block);
5808 implicit_block = current_block;
5810 foreach (VariableDeclaration decl in declarators){
5811 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
5814 return implicit_block;
5817 string CheckAttributeTarget (string a, Location l)
5820 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
5824 Report.Warning (658, 1, l,
5825 "`{0}' is invalid attribute target. All attributes in this attribute section will be ignored", a);
5826 return string.Empty;
5829 void CheckUnaryOperator (Operator.OpType op, Location l)
5833 case Operator.OpType.LogicalNot:
5834 case Operator.OpType.OnesComplement:
5835 case Operator.OpType.Increment:
5836 case Operator.OpType.Decrement:
5837 case Operator.OpType.True:
5838 case Operator.OpType.False:
5839 case Operator.OpType.Addition:
5840 case Operator.OpType.Subtraction:
5845 Report.Error (1019, l, "Overloadable unary operator expected");
5851 void CheckBinaryOperator (Operator.OpType op, Location l)
5855 case Operator.OpType.Addition:
5856 case Operator.OpType.Subtraction:
5857 case Operator.OpType.Multiply:
5858 case Operator.OpType.Division:
5859 case Operator.OpType.Modulus:
5860 case Operator.OpType.BitwiseAnd:
5861 case Operator.OpType.BitwiseOr:
5862 case Operator.OpType.ExclusiveOr:
5863 case Operator.OpType.LeftShift:
5864 case Operator.OpType.RightShift:
5865 case Operator.OpType.Equality:
5866 case Operator.OpType.Inequality:
5867 case Operator.OpType.GreaterThan:
5868 case Operator.OpType.LessThan:
5869 case Operator.OpType.GreaterThanOrEqual:
5870 case Operator.OpType.LessThanOrEqual:
5874 Report.Error (1020, l, "Overloadable binary operator expected");
5880 void syntax_error (Location l, string msg)
5882 Report.Error (1003, l, "Syntax error, " + msg);
5885 void note (string s)
5887 // Used to put annotations
5892 public Tokenizer Lexer {
5898 static CSharpParser ()
5900 oob_stack = new Stack ();
5903 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
5905 this.name = file.Name;
5907 current_namespace = new NamespaceEntry (null, file, null);
5908 current_class = current_namespace.SlaveDeclSpace;
5909 current_container = current_class.PartialContainer; // == RootContest.ToplevelTypes
5911 lexer = new Tokenizer (reader, file, defines);
5914 public void parse ()
5916 int errors = Report.Errors;
5918 if (yacc_verbose_flag > 1)
5919 yyparse (lexer, new yydebug.yyDebugSimple ());
5922 Tokenizer tokenizer = lexer as Tokenizer;
5923 tokenizer.cleanup ();
5924 } catch (Exception e){
5926 // Removed for production use, use parser verbose to get the output.
5928 // Console.WriteLine (e);
5929 if (Report.Errors == errors)
5930 Report.Error (-25, lexer.Location, "Parsing error");
5931 if (yacc_verbose_flag > 0)
5932 Console.WriteLine (e);
5935 if (RootContext.ToplevelTypes.NamespaceEntry != null)
5936 throw new InternalErrorException ("who set it?");
5939 static void CheckToken (int error, int yyToken, string msg, Location loc)
5941 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
5942 Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ());
5944 Report.Error (error, loc, msg);
5947 void CheckIdentifierToken (int yyToken, Location loc)
5949 CheckToken (1041, yyToken, "Identifier expected", loc);
5952 string ConsumeStoredComment ()
5954 string s = tmpComment;
5956 Lexer.doc_state = XmlCommentState.Allowed;
5960 Location GetLocation (object obj)
5962 if (obj is MemberCore)
5963 return ((MemberCore) obj).Location;
5964 if (obj is MemberName)
5965 return ((MemberName) obj).Location;
5966 if (obj is LocatedToken)
5967 return ((LocatedToken) obj).Location;
5968 if (obj is Location)
5969 return (Location) obj;
5970 return lexer.Location;
5974 start_block (Location loc)
5976 if (current_block == null || parsing_anonymous_method) {
5977 current_block = new ToplevelBlock (current_block, current_local_parameters, current_generic_method, loc);
5978 parsing_anonymous_method = false;
5980 current_block = new ExplicitBlock (current_block, loc, Location.Null);
5985 end_block (Location loc)
5987 Block retval = current_block.Explicit;
5988 retval.SetEndLocation (loc);
5989 current_block = retval.Parent;
5994 start_anonymous (bool lambda, Parameters parameters, Location loc)
5996 oob_stack.Push (current_anonymous_method);
5997 oob_stack.Push (current_local_parameters);
5999 current_local_parameters = parameters;
6001 ToplevelBlock top_current_block = current_block == null ? null : current_block.Toplevel;
6003 current_anonymous_method = lambda
6004 ? new LambdaExpression (
6005 current_anonymous_method, current_generic_method, current_container,
6006 parameters, top_current_block, loc)
6007 : new AnonymousMethodExpression (
6008 current_anonymous_method, current_generic_method, current_container,
6009 parameters, top_current_block, loc);
6011 // Force the next block to be created as a ToplevelBlock
6012 parsing_anonymous_method = true;
6016 * Completes the anonymous method processing, if lambda_expr is null, this
6017 * means that we have a Statement instead of an Expression embedded
6019 AnonymousMethodExpression
6020 end_anonymous (ToplevelBlock anon_block, Location loc)
6022 AnonymousMethodExpression retval;
6024 if (RootContext.Version == LanguageVersion.ISO_1){
6025 Report.FeatureIsNotISO1 (loc, "anonymous methods");
6028 current_anonymous_method.Block = anon_block;
6029 if ((anonymous_host != null) && (current_anonymous_method.Parent == null))
6030 anonymous_host.AddAnonymousMethod (current_anonymous_method);
6032 retval = current_anonymous_method;
6035 current_local_parameters = (Parameters) oob_stack.Pop ();
6036 current_anonymous_method = (AnonymousMethodExpression) oob_stack.Pop ();