3 // cs-parser.jay: The Parser for the C# compiler
5 // Authors: Miguel de Icaza (miguel@gnu.org)
6 // Ravi Pratap (ravi@ximian.com)
8 // Licensed under the terms of the GNU GPL
10 // (C) 2001 Ximian, Inc (http://www.ximian.com)
11 // (C) 2004 Novell, Inc
14 // (1) Figure out why error productions dont work. `type-declaration' is a
15 // great spot to put an `error' because you can reproduce it with this input:
18 // Possible optimization:
19 // Run memory profiler with parsing only, and consider dropping
20 // arraylists where not needed. Some pieces can use linked lists.
28 using System.Collections;
33 public class CSharpParser {
34 NamespaceEntry current_namespace;
35 TypeContainer current_container;
36 DeclSpace current_class;
38 IAnonymousHost anonymous_host;
41 /// Current block is used to add statements as we find
44 Block current_block, top_current_block;
46 Delegate current_delegate;
48 GenericMethod current_generic_method;
49 AnonymousMethodExpression current_anonymous_method;
52 /// This is used by the unary_expression code to resolve
53 /// a name against a parameter.
55 Parameters current_local_parameters;
58 /// Using during property parsing to describe the implicit
59 /// value parameter that is passed to the "set" and "get"accesor
60 /// methods (properties and indexers).
62 Expression implicit_value_parameter_type;
63 Parameters indexer_parameters;
66 /// Hack to help create non-typed array initializer
68 public static Expression current_array_type;
69 Expression pushed_current_array_type;
72 /// Used to determine if we are parsing the get/set pair
73 /// of an indexer or a property
77 bool parsing_anonymous_method;
80 /// An out-of-band stack.
89 static public int yacc_verbose_flag;
91 // Name of the file we are parsing
100 /// Temporary Xml documentation cache.
101 /// For enum types, we need one more temporary store.
104 string enumTypeComment;
106 /// Current attribute target
107 string current_attr_target;
109 /// assembly and module attribute definitions are enabled
110 bool global_attrs_enabled = true;
111 bool has_get, has_set;
116 %token NONE /* This token is never returned by our lexer */
117 %token ERROR // This is used not by the parser, but by the tokenizer.
121 *These are the C# keywords
209 /* C# keywords which are not really keywords */
215 /* C# single character operators/punctuation. */
216 %token OPEN_BRACE "{"
217 %token CLOSE_BRACE "}"
218 %token OPEN_BRACKET "["
219 %token CLOSE_BRACKET "]"
220 %token OPEN_PARENS "("
221 %token CLOSE_PARENS ")"
233 %token OP_GENERICS_LT "<"
235 %token OP_GENERICS_GT ">"
236 %token BITWISE_AND "&"
237 %token BITWISE_OR "|"
244 /* C# multi-character operators. */
245 %token DOUBLE_COLON "::"
248 %token OP_SHIFT_LEFT "<<"
249 %token OP_SHIFT_RIGHT ">>"
256 %token OP_MULT_ASSIGN "*="
257 %token OP_DIV_ASSIGN "/="
258 %token OP_MOD_ASSIGN "%="
259 %token OP_ADD_ASSIGN "+="
260 %token OP_SUB_ASSIGN "-="
261 %token OP_SHIFT_LEFT_ASSIGN "<<="
262 %token OP_SHIFT_RIGHT_ASSIGN ">>="
263 %token OP_AND_ASSIGN "&="
264 %token OP_XOR_ASSIGN "^="
265 %token OP_OR_ASSIGN "|="
269 %token LITERAL_INTEGER "int literal"
270 %token LITERAL_FLOAT "float literal"
271 %token LITERAL_DOUBLE "double literal"
272 %token LITERAL_DECIMAL "decimal literal"
273 %token LITERAL_CHARACTER "character literal"
274 %token LITERAL_STRING "string literal"
277 %token OPEN_PARENS_LAMBDA
278 %token CLOSE_PARENS_CAST
279 %token CLOSE_PARENS_NO_CAST
280 %token CLOSE_PARENS_OPEN_PARENS
281 %token CLOSE_PARENS_MINUS
282 %token DEFAULT_OPEN_PARENS
283 %token GENERIC_DIMENSION
286 /* Add precedence rules to solve dangling else s/r conflict */
295 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
297 %left STAR DIV PERCENT
298 %right BANG CARRET UMINUS
299 %nonassoc OP_INC OP_DEC
301 %left OPEN_BRACKET OPEN_BRACE
305 %start compilation_unit
309 : outer_declarations opt_EOF
310 | outer_declarations global_attributes opt_EOF
311 | global_attributes opt_EOF
312 | opt_EOF /* allow empty files */
318 Lexer.check_incorrect_doc_comment ();
322 Lexer.check_incorrect_doc_comment ();
328 | outer_declarations outer_declaration
332 : extern_alias_directive
334 | namespace_member_declaration
337 extern_alias_directives
338 : extern_alias_directive
339 | extern_alias_directives extern_alias_directive;
341 extern_alias_directive
342 : EXTERN IDENTIFIER IDENTIFIER SEMICOLON
344 LocatedToken lt = (LocatedToken) $2;
347 Report.Error (1003, lt.Location, "'alias' expected");
348 } else if (RootContext.Version == LanguageVersion.ISO_1) {
349 Report.FeatureIsNotISO1 (lt.Location, "external alias");
351 lt = (LocatedToken) $3;
352 current_namespace.UsingExternalAlias (lt.Value, lt.Location);
359 | using_directives using_directive
363 : using_alias_directive
365 if (RootContext.Documentation != null)
366 Lexer.doc_state = XmlCommentState.Allowed;
368 | using_namespace_directive
370 if (RootContext.Documentation != null)
371 Lexer.doc_state = XmlCommentState.Allowed;
375 using_alias_directive
376 : USING IDENTIFIER ASSIGN
377 namespace_or_type_name SEMICOLON
379 LocatedToken lt = (LocatedToken) $2;
380 current_namespace.UsingAlias (lt.Value, (MemberName) $4, (Location) $1);
383 CheckIdentifierToken (yyToken, GetLocation ($2));
387 using_namespace_directive
388 : USING namespace_name SEMICOLON
390 current_namespace.Using ((MemberName) $2, (Location) $1);
395 // Strictly speaking, namespaces don't have attributes but
396 // we parse global attributes along with namespace declarations and then
399 namespace_declaration
400 : opt_attributes NAMESPACE namespace_or_type_name
402 MemberName name = (MemberName) $3;
405 Report.Error(1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
408 if (name.TypeArguments != null)
409 syntax_error (lexer.Location, "namespace name expected");
411 current_namespace = new NamespaceEntry (
412 current_namespace, file, name.GetName ());
413 current_class = current_namespace.SlaveDeclSpace;
414 current_container = current_class.PartialContainer;
416 namespace_body opt_semicolon
418 current_namespace = current_namespace.Parent;
419 current_class = current_namespace.SlaveDeclSpace;
420 current_container = current_class.PartialContainer;
435 : namespace_or_type_name {
436 MemberName name = (MemberName) $1;
438 if (name.TypeArguments != null)
439 syntax_error (lexer.Location, "namespace name expected");
448 if (RootContext.Documentation != null)
449 Lexer.doc_state = XmlCommentState.Allowed;
455 : opt_extern_alias_directives
457 opt_namespace_member_declarations
461 Report.Error (1518, lexer.Location, "Expected `class', `delegate', `enum', `interface', or `struct'");
464 | opt_extern_alias_directives
466 opt_namespace_member_declarations
469 Report.Error (1513, lexer.Location, "} expected");
478 opt_extern_alias_directives
480 | extern_alias_directives
483 opt_namespace_member_declarations
485 | namespace_member_declarations
488 namespace_member_declarations
489 : namespace_member_declaration
490 | namespace_member_declarations namespace_member_declaration
493 namespace_member_declaration
497 DeclSpace ds = (DeclSpace)$1;
499 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
500 Report.Error (1527, ds.Location,
501 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
504 current_namespace.DeclarationFound = true;
506 | namespace_declaration {
507 current_namespace.DeclarationFound = true;
510 | field_declaration {
511 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
513 | method_declaration {
514 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
521 | interface_declaration
523 | delegate_declaration
525 // Enable this when we have handled all errors, because this acts as a generic fallback
528 // Console.WriteLine ("Token=" + yyToken);
529 // Report.Error (1518, GetLocation ($1), "Expected class, struct, interface, enum or delegate");
541 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
549 global_attrs_enabled = false;
554 global_attrs_enabled = false;
563 ArrayList sect = (ArrayList) $1;
565 if (global_attrs_enabled) {
566 if (current_attr_target == "module") {
567 CodeGen.Module.AddAttributes (sect);
569 } else if (current_attr_target != null && current_attr_target.Length > 0) {
570 CodeGen.Assembly.AddAttributes (sect);
573 $$ = new Attributes (sect);
576 if (RootContext.Documentation != null) {
577 Lexer.check_incorrect_doc_comment ();
579 XmlCommentState.Allowed;
583 $$ = new Attributes (sect);
585 current_attr_target = null;
587 | attribute_sections attribute_section
589 Attributes attrs = $1 as Attributes;
590 ArrayList sect = (ArrayList) $2;
592 if (global_attrs_enabled) {
593 if (current_attr_target == "module") {
594 CodeGen.Module.AddAttributes (sect);
596 } else if (current_attr_target == "assembly") {
597 CodeGen.Assembly.AddAttributes (sect);
601 attrs = new Attributes (sect);
603 attrs.AddAttributes (sect);
607 attrs = new Attributes (sect);
609 attrs.AddAttributes (sect);
612 current_attr_target = null;
617 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
621 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
627 attribute_target_specifier
628 : attribute_target COLON
630 current_attr_target = (string)$1;
638 LocatedToken lt = (LocatedToken) $1;
639 CheckAttributeTarget (lt.Value, lt.Location);
640 $$ = lt.Value; // Location won't be required anymore.
642 | EVENT { $$ = "event"; }
643 | RETURN { $$ = "return"; }
649 ArrayList attrs = new ArrayList (4);
655 | attribute_list COMMA attribute
657 ArrayList attrs = (ArrayList) $1;
665 : attribute_name opt_attribute_arguments
667 MemberName mname = (MemberName) $1;
668 if (mname.IsGeneric) {
669 Report.Error (404, lexer.Location,
670 "'<' unexpected: attributes cannot be generic");
673 object [] arguments = (object []) $2;
674 MemberName left = mname.Left;
675 string identifier = mname.Name;
677 Expression left_expr = left == null ? null : left.GetTypeExpression ();
679 if (current_attr_target == "assembly" || current_attr_target == "module")
680 // FIXME: supply "nameEscaped" parameter here.
681 $$ = new GlobalAttribute (current_namespace, current_attr_target,
682 left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
684 $$ = new Attribute (current_attr_target, left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
689 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
692 opt_attribute_arguments
693 : /* empty */ { $$ = null; }
694 | OPEN_PARENS attribute_arguments CLOSE_PARENS
702 : opt_positional_argument_list
707 $$ = new object [] { $1, null };
710 | positional_argument_list COMMA named_argument_list
712 $$ = new object[] { $1, $3 };
714 | named_argument_list
716 $$ = new object [] { null, $1 };
721 opt_positional_argument_list
722 : /* empty */ { $$ = null; }
723 | positional_argument_list
726 positional_argument_list
729 ArrayList args = new ArrayList (4);
730 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
734 | positional_argument_list COMMA expression
736 ArrayList args = (ArrayList) $1;
737 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
746 ArrayList args = new ArrayList (4);
751 | named_argument_list COMMA named_argument
753 ArrayList args = (ArrayList) $1;
758 | named_argument_list COMMA expression
760 Report.Error (1016, ((Expression) $3).Location, "Named attribute argument expected");
766 : IDENTIFIER ASSIGN expression
768 // FIXME: keep location
769 $$ = new DictionaryEntry (
770 ((LocatedToken) $1).Value,
771 new Argument ((Expression) $3, Argument.AType.Expression));
777 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
780 opt_class_member_declarations
782 | class_member_declarations
785 class_member_declarations
786 : class_member_declaration
787 | class_member_declarations
788 class_member_declaration
791 class_member_declaration
792 : constant_declaration // done
793 | field_declaration // done
794 | method_declaration // done
795 | property_declaration // done
796 | event_declaration // done
797 | indexer_declaration // done
798 | operator_declaration // done
799 | constructor_declaration // done
800 | destructor_declaration // done
810 lexer.ConstraintsParsing = true;
814 MemberName name = MakeName ((MemberName) $6);
815 push_current_class (new Struct (
816 current_namespace, current_class, name, (int) $2,
817 (Attributes) $1), false, $3);
820 opt_type_parameter_constraints_clauses
822 lexer.ConstraintsParsing = false;
825 current_container.AddBasesForPart (current_class, (ArrayList) $8);
827 current_class.SetParameterInfo ((ArrayList) $9);
829 if (RootContext.Documentation != null)
830 current_container.DocComment = Lexer.consume_doc_comment ();
834 if (RootContext.Documentation != null)
835 Lexer.doc_state = XmlCommentState.Allowed;
839 $$ = pop_current_class ();
841 | opt_attributes opt_modifiers opt_partial STRUCT error {
842 CheckIdentifierToken (yyToken, GetLocation ($5));
849 if (RootContext.Documentation != null)
850 Lexer.doc_state = XmlCommentState.Allowed;
852 opt_struct_member_declarations CLOSE_BRACE
855 opt_struct_member_declarations
857 | struct_member_declarations
860 struct_member_declarations
861 : struct_member_declaration
862 | struct_member_declarations struct_member_declaration
865 struct_member_declaration
866 : constant_declaration
869 | property_declaration
871 | indexer_declaration
872 | operator_declaration
873 | constructor_declaration
877 * This is only included so we can flag error 575:
878 * destructors only allowed on class types
880 | destructor_declaration
891 int modflags = (int) $2;
892 foreach (VariableDeclaration constant in (ArrayList) $5){
893 Location l = constant.Location;
894 if ((modflags & Modifiers.STATIC) != 0) {
895 Report.Error (504, l, "The constant `{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
899 Const c = new Const (
900 current_class, (Expression) $4, (string) constant.identifier,
901 (Expression) constant.expression_or_array_initializer, modflags,
904 if (RootContext.Documentation != null) {
905 c.DocComment = Lexer.consume_doc_comment ();
906 Lexer.doc_state = XmlCommentState.Allowed;
908 current_container.AddConstant (c);
914 : constant_declarator
916 ArrayList constants = new ArrayList (4);
921 | constant_declarators COMMA constant_declarator
924 ArrayList constants = (ArrayList) $1;
931 : IDENTIFIER ASSIGN constant_expression
933 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
937 // A const field requires a value to be provided
938 Report.Error (145, ((LocatedToken) $1).Location, "A const field requires a value to be provided");
950 Expression type = (Expression) $3;
953 current_array_type = null;
955 foreach (VariableDeclaration var in (ArrayList) $4){
956 Field field = new Field (current_class, type, mod, var.identifier,
957 (Attributes) $1, var.Location);
959 field.Initializer = var.expression_or_array_initializer;
961 if (RootContext.Documentation != null) {
962 field.DocComment = Lexer.consume_doc_comment ();
963 Lexer.doc_state = XmlCommentState.Allowed;
965 current_container.AddField (field);
966 $$ = field; // FIXME: might be better if it points to the top item
973 fixed_variable_declarators
976 Expression type = (Expression) $4;
979 current_array_type = null;
981 foreach (VariableDeclaration var in (ArrayList) $5) {
982 FixedField field = new FixedField (current_class, type, mod, var.identifier,
983 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
985 if (RootContext.Documentation != null) {
986 field.DocComment = Lexer.consume_doc_comment ();
987 Lexer.doc_state = XmlCommentState.Allowed;
989 current_container.AddField (field);
990 $$ = field; // FIXME: might be better if it points to the top item
999 Report.Error (1641, GetLocation ($4), "A fixed size buffer field must have the array size specifier after the field name");
1004 variable_declarators
1006 current_array_type = null;
1007 Report.Error (670, (Location) $3, "Fields cannot have void type");
1011 fixed_variable_declarators
1012 : fixed_variable_declarator
1014 ArrayList decl = new ArrayList (2);
1018 | fixed_variable_declarators COMMA fixed_variable_declarator
1020 ArrayList decls = (ArrayList) $1;
1026 fixed_variable_declarator
1027 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
1029 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1031 | IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1033 Report.Error (443, lexer.Location, "Value or constant expected");
1034 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1038 variable_declarators
1039 : variable_declarator
1041 ArrayList decl = new ArrayList (4);
1046 | variable_declarators COMMA variable_declarator
1048 ArrayList decls = (ArrayList) $1;
1055 : IDENTIFIER ASSIGN variable_initializer
1057 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1061 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1063 | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
1065 Report.Error (650, ((LocatedToken) $1).Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
1066 "To declare a fixed size buffer field, use the fixed keyword before the field type");
1071 variable_initializer
1080 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
1082 $$ = new StackAlloc ((Expression) $2, (Expression) $4, (Location) $1);
1086 $$ = new ArglistAccess ((Location) $1);
1090 Report.Error (1575, (Location) $1, "A stackalloc expression requires [] after type");
1097 anonymous_host = (IAnonymousHost) $1;
1098 if (RootContext.Documentation != null)
1099 Lexer.doc_state = XmlCommentState.NotAllowed;
1103 Method method = (Method) $1;
1104 method.Block = (ToplevelBlock) $3;
1105 current_container.AddMethod (method);
1107 anonymous_host = null;
1108 current_generic_method = null;
1109 current_local_parameters = null;
1111 if (RootContext.Documentation != null)
1112 Lexer.doc_state = XmlCommentState.Allowed;
1125 Report.Error (1585, lexer.Location,
1126 "Member modifier `{0}' must precede the member type and name",
1127 Modifiers.Name (i));
1136 // This rule is used to handle the cases where OPEN_PARENS_LAMBDA
1137 // is returned (type followed by an identifier), these are the
1138 // declarations (methods, overloads, constructors, etc) and a handful
1139 // of expressions ("using", "fixed") or "catch".
1143 | OPEN_PARENS_LAMBDA
1149 type namespace_or_type_name
1150 open_parens opt_formal_parameter_list CLOSE_PARENS
1152 lexer.ConstraintsParsing = true;
1154 opt_type_parameter_constraints_clauses
1156 lexer.ConstraintsParsing = false;
1158 MemberName name = (MemberName) $4;
1160 if ($9 != null && name.TypeArguments == null)
1161 Report.Error (80, lexer.Location,
1162 "Constraints are not allowed on non-generic declarations");
1166 GenericMethod generic = null;
1167 if (name.TypeArguments != null) {
1168 generic = new GenericMethod (current_namespace, current_class, name,
1169 (Expression) $3, (Parameters) $6);
1171 generic.SetParameterInfo ((ArrayList) $9);
1174 method = new Method (current_class, generic, (Expression) $3, (int) $2, false,
1175 name, (Parameters) $6, (Attributes) $1);
1177 anonymous_host = method;
1178 current_local_parameters = (Parameters) $6;
1179 current_generic_method = generic;
1181 if (RootContext.Documentation != null)
1182 method.DocComment = Lexer.consume_doc_comment ();
1188 VOID namespace_or_type_name
1189 open_parens opt_formal_parameter_list CLOSE_PARENS
1191 lexer.ConstraintsParsing = true;
1193 opt_type_parameter_constraints_clauses
1195 lexer.ConstraintsParsing = false;
1197 MemberName name = (MemberName) $4;
1199 if ($9 != null && name.TypeArguments == null)
1200 Report.Error (80, lexer.Location,
1201 "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 TypeManager.system_void_expr, (Parameters) $6);
1209 generic.SetParameterInfo ((ArrayList) $9);
1212 method = new Method (current_class, generic, TypeManager.system_void_expr,
1213 (int) $2, false, 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 modifiers namespace_or_type_name open_parens opt_formal_parameter_list CLOSE_PARENS
1229 MemberName name = (MemberName) $5;
1230 Report.Error (1585, name.Location,
1231 "Member modifier `{0}' must precede the member type and name", Modifiers.Name ((int) $4));
1233 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1234 0, false, name, (Parameters) $6, (Attributes) $1);
1236 current_local_parameters = (Parameters) $6;
1238 if (RootContext.Documentation != null)
1239 method.DocComment = Lexer.consume_doc_comment ();
1247 | SEMICOLON { $$ = null; }
1250 opt_formal_parameter_list
1251 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1252 | formal_parameter_list
1255 formal_parameter_list
1258 ArrayList pars_list = (ArrayList) $1;
1260 Parameter [] pars = new Parameter [pars_list.Count];
1261 pars_list.CopyTo (pars);
1263 $$ = new Parameters (pars);
1265 | fixed_parameters COMMA parameter_array
1267 ArrayList pars_list = (ArrayList) $1;
1270 Parameter [] pars = new Parameter [pars_list.Count];
1271 pars_list.CopyTo (pars);
1273 $$ = new Parameters (pars);
1275 | fixed_parameters COMMA ARGLIST
1277 ArrayList pars_list = (ArrayList) $1;
1278 //pars_list.Add (new ArglistParameter (GetLocation ($3)));
1280 Parameter [] pars = new Parameter [pars_list.Count];
1281 pars_list.CopyTo (pars);
1283 $$ = new Parameters (pars, true);
1285 | parameter_array COMMA error
1288 Report.Error (231, ((Parameter) $1).Location, "A params parameter must be the last parameter in a formal parameter list");
1291 | fixed_parameters COMMA parameter_array COMMA error
1294 Report.Error (231, ((Parameter) $3).Location, "A params parameter must be the last parameter in a formal parameter list");
1297 | ARGLIST COMMA error
1299 Report.Error (257, (Location) $1, "An __arglist parameter must be the last parameter in a formal parameter list");
1302 | fixed_parameters COMMA ARGLIST COMMA error
1304 Report.Error (257, (Location) $3, "An __arglist parameter must be the last parameter in a formal parameter list");
1309 $$ = new Parameters (new Parameter[] { (Parameter) $1 } );
1313 $$ = new Parameters (new Parameter[0], true);
1320 ArrayList pars = new ArrayList (4);
1325 | fixed_parameters COMMA fixed_parameter
1327 ArrayList pars = (ArrayList) $1;
1336 opt_parameter_modifier
1340 LocatedToken lt = (LocatedToken) $4;
1341 $$ = new Parameter ((Expression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1344 opt_parameter_modifier
1346 IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1348 LocatedToken lt = (LocatedToken) $4;
1349 Report.Error (1552, lt.Location, "Array type specifier, [], must appear before parameter name");
1353 opt_parameter_modifier
1356 Report.Error (1001, GetLocation ($3), "Identifier expected");
1360 opt_parameter_modifier
1363 CheckIdentifierToken (yyToken, GetLocation ($4));
1367 opt_parameter_modifier
1373 LocatedToken lt = (LocatedToken) $4;
1374 Report.Error (241, lt.Location, "Default parameter specifiers are not permitted");
1379 opt_parameter_modifier
1380 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1381 | parameter_modifiers
1385 : parameter_modifier
1389 | parameter_modifiers parameter_modifier
1391 Report.Error (1108, lexer.Location, "A parameter cannot have specified more than one modifier");
1397 : REF { $$ = Parameter.Modifier.REF; }
1398 | OUT { $$ = Parameter.Modifier.OUT; }
1402 : opt_attributes PARAMS type IDENTIFIER
1404 LocatedToken lt = (LocatedToken) $4;
1405 $$ = new ParamsParameter ((Expression) $3, lt.Value, (Attributes) $1, lt.Location);
1407 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1409 Report.Error (1611, (Location) $2, "The params parameter cannot be declared as ref or out");
1412 | opt_attributes PARAMS type error {
1413 CheckIdentifierToken (yyToken, GetLocation ($4));
1418 property_declaration
1422 namespace_or_type_name
1424 if (RootContext.Documentation != null)
1425 tmpComment = Lexer.consume_doc_comment ();
1429 implicit_value_parameter_type = (Expression) $3;
1431 lexer.PropertyParsing = true;
1433 accessor_declarations
1435 lexer.PropertyParsing = false;
1436 has_get = has_set = false;
1444 Accessors accessors = (Accessors) $8;
1445 Accessor get_block = accessors.get_or_add;
1446 Accessor set_block = accessors.set_or_remove;
1448 MemberName name = (MemberName) $4;
1450 if (name.TypeArguments != null)
1451 syntax_error (lexer.Location, "a property can't have type arguments");
1453 prop = new Property (current_class, (Expression) $3, (int) $2, false,
1454 name, (Attributes) $1, get_block, set_block, accessors.declared_in_reverse);
1456 current_container.AddProperty (prop);
1457 implicit_value_parameter_type = null;
1459 if (RootContext.Documentation != null)
1460 prop.DocComment = ConsumeStoredComment ();
1465 accessor_declarations
1466 : get_accessor_declaration
1468 $$ = new Accessors ((Accessor) $1, null);
1470 | get_accessor_declaration accessor_declarations
1472 Accessors accessors = (Accessors) $2;
1473 accessors.get_or_add = (Accessor) $1;
1476 | set_accessor_declaration
1478 $$ = new Accessors (null, (Accessor) $1);
1480 | set_accessor_declaration accessor_declarations
1482 Accessors accessors = (Accessors) $2;
1483 accessors.set_or_remove = (Accessor) $1;
1484 accessors.declared_in_reverse = true;
1489 Report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1494 get_accessor_declaration
1495 : opt_attributes opt_modifiers GET
1497 // If this is not the case, then current_local_parameters has already
1498 // been set in indexer_declaration
1499 if (parsing_indexer == false)
1500 current_local_parameters = null;
1502 current_local_parameters = indexer_parameters;
1503 lexer.PropertyParsing = false;
1505 anonymous_host = SimpleAnonymousHost.GetSimple ();
1510 Report.Error (1007, (Location) $3, "Property accessor already defined");
1513 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1515 current_local_parameters = null;
1516 lexer.PropertyParsing = true;
1518 SimpleAnonymousHost.Simple.Propagate (accessor);
1519 anonymous_host = null;
1521 if (RootContext.Documentation != null)
1522 if (Lexer.doc_state == XmlCommentState.Error)
1523 Lexer.doc_state = XmlCommentState.NotAllowed;
1529 set_accessor_declaration
1530 : opt_attributes opt_modifiers SET
1533 Parameter implicit_value_parameter = new Parameter (
1534 implicit_value_parameter_type, "value",
1535 Parameter.Modifier.NONE, null, (Location) $3);
1537 if (parsing_indexer == false) {
1538 args = new Parameter [1];
1539 args [0] = implicit_value_parameter;
1540 current_local_parameters = new Parameters (args);
1542 Parameter [] fpars = indexer_parameters.FixedParameters;
1545 int count = fpars.Length;
1547 args = new Parameter [count + 1];
1548 fpars.CopyTo (args, 0);
1549 args [count] = implicit_value_parameter;
1552 current_local_parameters = new Parameters (
1556 lexer.PropertyParsing = false;
1558 anonymous_host = SimpleAnonymousHost.GetSimple ();
1563 Report.Error (1007, ((LocatedToken) $3).Location, "Property accessor already defined");
1566 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1568 current_local_parameters = null;
1569 lexer.PropertyParsing = true;
1571 SimpleAnonymousHost.Simple.Propagate (accessor);
1572 anonymous_host = null;
1574 if (RootContext.Documentation != null
1575 && Lexer.doc_state == XmlCommentState.Error)
1576 Lexer.doc_state = XmlCommentState.NotAllowed;
1584 | SEMICOLON { $$ = null; }
1587 interface_declaration
1593 lexer.ConstraintsParsing = true;
1597 MemberName name = MakeName ((MemberName) $6);
1599 push_current_class (new Interface (
1600 current_namespace, current_class, name, (int) $2,
1601 (Attributes) $1), true, $3);
1604 opt_type_parameter_constraints_clauses
1606 lexer.ConstraintsParsing = false;
1609 current_container.AddBasesForPart (current_class, (ArrayList) $8);
1611 current_class.SetParameterInfo ((ArrayList) $9);
1613 if (RootContext.Documentation != null) {
1614 current_container.DocComment = Lexer.consume_doc_comment ();
1615 Lexer.doc_state = XmlCommentState.Allowed;
1620 if (RootContext.Documentation != null)
1621 Lexer.doc_state = XmlCommentState.Allowed;
1625 $$ = pop_current_class ();
1627 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1628 CheckIdentifierToken (yyToken, GetLocation ($5));
1634 opt_interface_member_declarations
1638 opt_interface_member_declarations
1640 | interface_member_declarations
1643 interface_member_declarations
1644 : interface_member_declaration
1645 | interface_member_declarations interface_member_declaration
1648 interface_member_declaration
1649 : interface_method_declaration
1654 Method m = (Method) $1;
1656 if (m.IsExplicitImpl)
1657 Report.Error (541, m.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1658 m.GetSignatureForError ());
1660 current_container.AddMethod (m);
1662 if (RootContext.Documentation != null)
1663 Lexer.doc_state = XmlCommentState.Allowed;
1665 | interface_property_declaration
1670 Property p = (Property) $1;
1672 if (p.IsExplicitImpl)
1673 Report.Error (541, p.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1674 p.GetSignatureForError ());
1676 current_container.AddProperty (p);
1678 if (RootContext.Documentation != null)
1679 Lexer.doc_state = XmlCommentState.Allowed;
1681 | interface_event_declaration
1684 Event e = (Event) $1;
1686 if (e.IsExplicitImpl)
1687 Report.Error (541, e.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1688 e.GetSignatureForError ());
1690 current_container.AddEvent (e);
1693 if (RootContext.Documentation != null)
1694 Lexer.doc_state = XmlCommentState.Allowed;
1696 | interface_indexer_declaration
1701 Indexer i = (Indexer) $1;
1703 if (i.IsExplicitImpl)
1704 Report.Error (541, i.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1705 i.GetSignatureForError ());
1707 current_container.AddIndexer (i);
1709 if (RootContext.Documentation != null)
1710 Lexer.doc_state = XmlCommentState.Allowed;
1712 | delegate_declaration
1715 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1716 ((MemberCore)$1).GetSignatureForError ());
1722 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1723 ((MemberCore)$1).GetSignatureForError ());
1726 | struct_declaration
1729 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1730 ((MemberCore)$1).GetSignatureForError ());
1736 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1737 ((MemberCore)$1).GetSignatureForError ());
1740 | interface_declaration
1743 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1744 ((MemberCore)$1).GetSignatureForError ());
1747 | constant_declaration
1749 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1757 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, GetLocation ($1));
1762 interface_method_declaration_body
1765 lexer.ConstraintsParsing = false;
1767 opt_statement_list CLOSE_BRACE
1769 Report.Error (531, lexer.Location,
1770 "'{0}': interface members cannot have a definition", ((MemberName) $-1).ToString ());
1776 interface_method_declaration
1777 : opt_attributes opt_new type namespace_or_type_name
1778 open_parens opt_formal_parameter_list CLOSE_PARENS
1780 lexer.ConstraintsParsing = true;
1782 opt_type_parameter_constraints_clauses
1784 // Refer to the name as $-1 in interface_method_declaration_body
1787 interface_method_declaration_body
1789 lexer.ConstraintsParsing = false;
1791 MemberName name = (MemberName) $4;
1793 if ($9 != null && name.TypeArguments == null)
1794 Report.Error (80, lexer.Location,
1795 "Constraints are not allowed on non-generic declarations");
1797 GenericMethod generic = null;
1798 if (name.TypeArguments != null) {
1799 generic = new GenericMethod (current_namespace, current_class, name,
1800 (Expression) $3, (Parameters) $6);
1802 generic.SetParameterInfo ((ArrayList) $9);
1805 $$ = new Method (current_class, generic, (Expression) $3, (int) $2, true, name,
1806 (Parameters) $6, (Attributes) $1);
1807 if (RootContext.Documentation != null)
1808 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1810 | opt_attributes opt_new VOID namespace_or_type_name
1811 open_parens opt_formal_parameter_list CLOSE_PARENS
1813 lexer.ConstraintsParsing = true;
1815 opt_type_parameter_constraints_clauses
1819 interface_method_declaration_body
1821 lexer.ConstraintsParsing = false;
1823 MemberName name = (MemberName) $4;
1825 if ($9 != null && name.TypeArguments == null)
1826 Report.Error (80, lexer.Location,
1827 "Constraints are not allowed on non-generic declarations");
1829 GenericMethod generic = null;
1830 if (name.TypeArguments != null) {
1831 generic = new GenericMethod (current_namespace, current_class, name,
1832 TypeManager.system_void_expr, (Parameters) $6);
1834 generic.SetParameterInfo ((ArrayList) $9);
1837 $$ = new Method (current_class, generic, TypeManager.system_void_expr, (int) $2,
1838 true, name, (Parameters) $6, (Attributes) $1);
1839 if (RootContext.Documentation != null)
1840 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1844 interface_property_declaration
1849 { lexer.PropertyParsing = true; }
1850 accessor_declarations
1852 has_get = has_set = false;
1853 lexer.PropertyParsing = false;
1857 LocatedToken lt = (LocatedToken) $4;
1858 MemberName name = new MemberName (lt.Value, lt.Location);
1860 if ($3 == TypeManager.system_void_expr) {
1861 Report.Error (547, lt.Location, "`{0}': property or indexer cannot have void type", lt.Value);
1867 p = new Property (current_class, (Expression) $3, (int) $2, true,
1868 name, (Attributes) $1,
1871 Report.Error (548, p.Location, "`{0}': property or indexer must have at least one accessor", p.GetSignatureForError ());
1875 Accessors accessor = (Accessors) $7;
1876 p = new Property (current_class, (Expression) $3, (int) $2, true,
1877 name, (Attributes) $1,
1878 accessor.get_or_add, accessor.set_or_remove, accessor.declared_in_reverse);
1880 if (accessor.get_or_add != null && accessor.get_or_add.Block != null) {
1881 Report.Error (531, p.Location, "`{0}.get': interface members cannot have a definition", p.GetSignatureForError ());
1886 if (accessor.set_or_remove != null && accessor.set_or_remove.Block != null) {
1887 Report.Error (531, p.Location, "`{0}.set': interface members cannot have a definition", p.GetSignatureForError ());
1892 if (RootContext.Documentation != null)
1893 p.DocComment = Lexer.consume_doc_comment ();
1900 CheckIdentifierToken (yyToken, GetLocation ($4));
1906 interface_event_declaration
1907 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1909 LocatedToken lt = (LocatedToken) $5;
1910 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1911 new MemberName (lt.Value, lt.Location),
1913 if (RootContext.Documentation != null)
1914 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1916 | opt_attributes opt_new EVENT type error {
1917 CheckIdentifierToken (yyToken, GetLocation ($5));
1920 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1921 LocatedToken lt = (LocatedToken) $5;
1922 Report.Error (68, lt.Location, "`{0}.{1}': event in interface cannot have initializer", current_container.Name, lt.Value);
1925 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1927 lexer.EventParsing = true;
1929 event_accessor_declarations
1931 lexer.EventParsing = false;
1934 Report.Error (69, (Location) $3, "Event in interface cannot have add or remove accessors");
1939 interface_indexer_declaration
1940 : opt_attributes opt_new type THIS
1941 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1943 { lexer.PropertyParsing = true; }
1944 accessor_declarations
1946 has_get = has_set = false;
1947 lexer.PropertyParsing = false;
1953 i = new Indexer (current_class, (Expression) $3,
1954 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1955 (int) $2, true, (Parameters) $6, (Attributes) $1,
1958 Report.Error (548, i.Location, "`{0}': property or indexer must have at least one accessor", i.GetSignatureForError ());
1962 Accessors accessors = (Accessors) $10;
1963 i = new Indexer (current_class, (Expression) $3,
1964 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1965 (int) $2, true, (Parameters) $6, (Attributes) $1,
1966 accessors.get_or_add, accessors.set_or_remove, accessors.declared_in_reverse);
1968 if (accessors.get_or_add != null && accessors.get_or_add.Block != null) {
1969 Report.Error (531, i.Location, "`{0}.get': interface members cannot have a definition", i.GetSignatureForError ());
1974 if (accessors.set_or_remove != null && accessors.set_or_remove.Block != null) {
1975 Report.Error (531, i.Location, "`{0}.set': interface members cannot have a definition", i.GetSignatureForError ());
1980 if (RootContext.Documentation != null)
1981 i.DocComment = ConsumeStoredComment ();
1987 operator_declaration
1988 : opt_attributes opt_modifiers operator_declarator
1990 anonymous_host = SimpleAnonymousHost.GetSimple ();
1997 OperatorDeclaration decl = (OperatorDeclaration) $3;
1999 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
2001 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null, decl.location);
2002 if (decl.arg2type != null)
2003 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null, decl.location);
2005 Operator op = new Operator (
2006 current_class, decl.optype, decl.ret_type, (int) $2,
2007 new Parameters (param_list),
2008 (ToplevelBlock) $5, (Attributes) $1, decl.location);
2010 if (RootContext.Documentation != null) {
2011 op.DocComment = tmpComment;
2012 Lexer.doc_state = XmlCommentState.Allowed;
2015 SimpleAnonymousHost.Simple.Propagate (op);
2016 anonymous_host = null;
2018 // Note again, checking is done in semantic analysis
2019 current_container.AddOperator (op);
2021 current_local_parameters = null;
2027 | SEMICOLON { $$ = null; }
2030 : type OPERATOR overloadable_operator
2031 open_parens opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
2033 // TODO: wrong location
2034 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
2035 Error_ParameterModifierNotValid ((Location) $2);
2037 LocatedToken lt = (LocatedToken) $7;
2038 Operator.OpType op = (Operator.OpType) $3;
2039 CheckUnaryOperator (op, lt.Location);
2041 if (op == Operator.OpType.Addition)
2042 op = Operator.OpType.UnaryPlus;
2044 if (op == Operator.OpType.Subtraction)
2045 op = Operator.OpType.UnaryNegation;
2047 Parameter [] pars = new Parameter [1];
2048 Expression type = (Expression) $6;
2050 pars [0] = new Parameter (type, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2052 current_local_parameters = new Parameters (pars);
2054 if (RootContext.Documentation != null) {
2055 tmpComment = Lexer.consume_doc_comment ();
2056 Lexer.doc_state = XmlCommentState.NotAllowed;
2059 $$ = new OperatorDeclaration (op, (Expression) $1, type, lt.Value,
2060 null, null, (Location) $2);
2062 | type OPERATOR overloadable_operator
2064 opt_parameter_modifier type IDENTIFIER COMMA
2065 opt_parameter_modifier type IDENTIFIER
2068 // TODO: wrong location
2069 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE || (Parameter.Modifier)$9 != Parameter.Modifier.NONE)
2070 Error_ParameterModifierNotValid ((Location) $2);
2072 LocatedToken ltParam1 = (LocatedToken) $7;
2073 LocatedToken ltParam2 = (LocatedToken) $11;
2074 CheckBinaryOperator ((Operator.OpType) $3, (Location) $2);
2076 Parameter [] pars = new Parameter [2];
2078 Expression typeL = (Expression) $6;
2079 Expression typeR = (Expression) $10;
2081 pars [0] = new Parameter (typeL, ltParam1.Value, Parameter.Modifier.NONE, null, ltParam1.Location);
2082 pars [1] = new Parameter (typeR, ltParam2.Value, Parameter.Modifier.NONE, null, ltParam2.Location);
2084 current_local_parameters = new Parameters (pars);
2086 if (RootContext.Documentation != null) {
2087 tmpComment = Lexer.consume_doc_comment ();
2088 Lexer.doc_state = XmlCommentState.NotAllowed;
2091 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
2092 typeL, ltParam1.Value,
2093 typeR, ltParam2.Value, (Location) $2);
2095 | conversion_operator_declarator
2096 | type OPERATOR overloadable_operator
2098 opt_parameter_modifier type IDENTIFIER COMMA
2099 opt_parameter_modifier type IDENTIFIER COMMA
2100 opt_parameter_modifier type IDENTIFIER
2103 Report.Error (1534, (Location) $2, "Overloaded binary operator `{0}' takes two parameters",
2104 Operator.GetName ((Operator.OpType) $3));
2107 | type OPERATOR overloadable_operator
2108 open_parens CLOSE_PARENS
2110 Report.Error (1535, (Location) $2, "Overloaded unary operator `{0}' takes one parameter",
2111 Operator.GetName ((Operator.OpType) $3));
2116 overloadable_operator
2118 : BANG { $$ = Operator.OpType.LogicalNot; }
2119 | TILDE { $$ = Operator.OpType.OnesComplement; }
2120 | OP_INC { $$ = Operator.OpType.Increment; }
2121 | OP_DEC { $$ = Operator.OpType.Decrement; }
2122 | TRUE { $$ = Operator.OpType.True; }
2123 | FALSE { $$ = Operator.OpType.False; }
2124 // Unary and binary:
2125 | PLUS { $$ = Operator.OpType.Addition; }
2126 | MINUS { $$ = Operator.OpType.Subtraction; }
2128 | STAR { $$ = Operator.OpType.Multiply; }
2129 | DIV { $$ = Operator.OpType.Division; }
2130 | PERCENT { $$ = Operator.OpType.Modulus; }
2131 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
2132 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
2133 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
2134 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
2135 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
2136 | OP_EQ { $$ = Operator.OpType.Equality; }
2137 | OP_NE { $$ = Operator.OpType.Inequality; }
2138 | OP_GT { $$ = Operator.OpType.GreaterThan; }
2139 | OP_LT { $$ = Operator.OpType.LessThan; }
2140 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
2141 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
2144 conversion_operator_declarator
2145 : IMPLICIT OPERATOR type open_parens opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
2147 // TODO: wrong location
2148 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
2149 Error_ParameterModifierNotValid (GetLocation ($4));
2151 LocatedToken lt = (LocatedToken) $7;
2152 Parameter [] pars = new Parameter [1];
2154 pars [0] = new Parameter ((Expression) $6, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2156 current_local_parameters = new Parameters (pars);
2158 if (RootContext.Documentation != null) {
2159 tmpComment = Lexer.consume_doc_comment ();
2160 Lexer.doc_state = XmlCommentState.NotAllowed;
2163 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $6, lt.Value,
2164 null, null, (Location) $2);
2166 | EXPLICIT OPERATOR type open_parens opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
2168 // TODO: wrong location
2169 if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
2170 Error_ParameterModifierNotValid (GetLocation ($4));
2172 LocatedToken lt = (LocatedToken) $7;
2173 Parameter [] pars = new Parameter [1];
2175 pars [0] = new Parameter ((Expression) $6, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2177 current_local_parameters = new Parameters (pars);
2179 if (RootContext.Documentation != null) {
2180 tmpComment = Lexer.consume_doc_comment ();
2181 Lexer.doc_state = XmlCommentState.NotAllowed;
2184 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $6, lt.Value,
2185 null, null, (Location) $2);
2189 syntax_error ((Location) $1, "'operator' expected");
2193 syntax_error ((Location) $1, "'operator' expected");
2197 constructor_declaration
2200 constructor_declarator
2203 Constructor c = (Constructor) $3;
2204 c.Block = (ToplevelBlock) $4;
2205 c.OptAttributes = (Attributes) $1;
2206 c.ModFlags = (int) $2;
2208 if (RootContext.Documentation != null)
2209 c.DocComment = ConsumeStoredComment ();
2211 if (c.Name == current_container.Basename){
2212 if ((c.ModFlags & Modifiers.STATIC) != 0){
2213 if ((c.ModFlags & Modifiers.Accessibility) != 0){
2214 Report.Error (515, c.Location,
2215 "`{0}': access modifiers are not allowed on static constructors",
2216 c.GetSignatureForError ());
2219 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2221 if (c.Initializer != null){
2222 Report.Error (514, c.Location,
2223 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2224 c.GetSignatureForError ());
2227 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2230 // We let another layer check the validity of the constructor.
2231 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
2234 current_container.AddConstructor (c);
2236 current_local_parameters = null;
2237 if (RootContext.Documentation != null)
2238 Lexer.doc_state = XmlCommentState.Allowed;
2242 constructor_declarator
2245 if (RootContext.Documentation != null) {
2246 tmpComment = Lexer.consume_doc_comment ();
2247 Lexer.doc_state = XmlCommentState.Allowed;
2250 open_parens opt_formal_parameter_list CLOSE_PARENS
2252 current_local_parameters = (Parameters) $4;
2254 opt_constructor_initializer
2256 LocatedToken lt = (LocatedToken) $1;
2257 $$ = new Constructor (current_class, lt.Value, 0, (Parameters) $4,
2258 (ConstructorInitializer) $7, lt.Location);
2260 anonymous_host = (IAnonymousHost) $$;
2266 | SEMICOLON { $$ = null; }
2269 opt_constructor_initializer
2270 : /* empty */ { $$ = null; }
2271 | constructor_initializer
2274 constructor_initializer
2275 : COLON BASE open_parens opt_argument_list CLOSE_PARENS
2277 $$ = new ConstructorBaseInitializer ((ArrayList) $4, (Location) $2);
2279 | COLON THIS open_parens opt_argument_list CLOSE_PARENS
2281 $$ = new ConstructorThisInitializer ((ArrayList) $4, (Location) $2);
2284 Report.Error (1018, (Location) $1, "Keyword this or base expected");
2290 : /* EMPTY */ { $$ = 0; }
2291 | UNSAFE { $$ = Modifiers.UNSAFE; }
2292 | EXTERN { $$ = Modifiers.EXTERN; }
2295 destructor_declaration
2296 : opt_attributes opt_finalizer TILDE
2298 if (RootContext.Documentation != null) {
2299 tmpComment = Lexer.consume_doc_comment ();
2300 Lexer.doc_state = XmlCommentState.NotAllowed;
2303 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2305 LocatedToken lt = (LocatedToken) $5;
2306 if (lt.Value != current_container.MemberName.Name){
2307 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2308 } else if (current_container.Kind != Kind.Class){
2309 Report.Error (575, lt.Location, "Only class types can contain destructor");
2311 Location l = lt.Location;
2314 if (!RootContext.StdLib && current_container.Name == "System.Object")
2315 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2317 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2319 Method d = new Destructor (
2320 current_class, TypeManager.system_void_expr, m, "Finalize",
2321 Parameters.EmptyReadOnlyParameters, (Attributes) $1, l);
2322 if (RootContext.Documentation != null)
2323 d.DocComment = ConsumeStoredComment ();
2325 d.Block = (ToplevelBlock) $8;
2326 current_container.AddMethod (d);
2334 EVENT type variable_declarators SEMICOLON
2336 current_array_type = null;
2337 foreach (VariableDeclaration var in (ArrayList) $5) {
2339 MemberName name = new MemberName (var.identifier,
2342 EventField e = new EventField (
2343 current_class, (Expression) $4, (int) $2, false, name,
2346 e.Initializer = var.expression_or_array_initializer;
2348 current_container.AddEvent (e);
2350 if (RootContext.Documentation != null) {
2351 e.DocComment = Lexer.consume_doc_comment ();
2352 Lexer.doc_state = XmlCommentState.Allowed;
2358 EVENT type namespace_or_type_name
2361 implicit_value_parameter_type = (Expression) $4;
2362 lexer.EventParsing = true;
2364 event_accessor_declarations
2366 lexer.EventParsing = false;
2370 MemberName name = (MemberName) $5;
2373 Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2374 current_container.Name, name.ToString ());
2377 Accessors accessors = (Accessors) $8;
2379 if (name.TypeArguments != null)
2380 syntax_error (lexer.Location, "an event can't have type arguments");
2382 if (accessors.get_or_add == null || accessors.set_or_remove == null)
2383 // CS0073 is already reported, so no CS0065 here.
2386 Event e = new EventProperty (
2387 current_class, (Expression) $4, (int) $2, false, name,
2388 (Attributes) $1, accessors.get_or_add, accessors.set_or_remove);
2389 if (RootContext.Documentation != null) {
2390 e.DocComment = Lexer.consume_doc_comment ();
2391 Lexer.doc_state = XmlCommentState.Allowed;
2394 current_container.AddEvent (e);
2395 implicit_value_parameter_type = null;
2399 | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
2400 MemberName mn = (MemberName) $5;
2402 if (mn.Left != null)
2403 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2405 Report.Error (71, mn.Location, "Event declaration should use property syntax");
2407 if (RootContext.Documentation != null)
2408 Lexer.doc_state = XmlCommentState.Allowed;
2412 event_accessor_declarations
2413 : add_accessor_declaration remove_accessor_declaration
2415 $$ = new Accessors ((Accessor) $1, (Accessor) $2);
2417 | remove_accessor_declaration add_accessor_declaration
2419 Accessors accessors = new Accessors ((Accessor) $2, (Accessor) $1);
2420 accessors.declared_in_reverse = true;
2423 | add_accessor_declaration { $$ = null; }
2424 | remove_accessor_declaration { $$ = null; }
2427 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2433 add_accessor_declaration
2434 : opt_attributes ADD
2436 Parameter [] args = new Parameter [1];
2437 Parameter implicit_value_parameter = new Parameter (
2438 implicit_value_parameter_type, "value",
2439 Parameter.Modifier.NONE, null, (Location) $2);
2441 args [0] = implicit_value_parameter;
2443 current_local_parameters = new Parameters (args);
2444 lexer.EventParsing = false;
2448 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2449 lexer.EventParsing = true;
2451 | opt_attributes ADD error {
2452 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2455 | opt_attributes modifiers ADD {
2456 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2461 remove_accessor_declaration
2462 : opt_attributes REMOVE
2464 Parameter [] args = new Parameter [1];
2465 Parameter implicit_value_parameter = new Parameter (
2466 implicit_value_parameter_type, "value",
2467 Parameter.Modifier.NONE, null, (Location) $2);
2469 args [0] = implicit_value_parameter;
2471 current_local_parameters = new Parameters (args);
2472 lexer.EventParsing = false;
2476 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2477 lexer.EventParsing = true;
2479 | opt_attributes REMOVE error {
2480 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2483 | opt_attributes modifiers REMOVE {
2484 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2490 : opt_attributes opt_modifiers indexer_declarator
2493 IndexerDeclaration decl = (IndexerDeclaration) $3;
2495 implicit_value_parameter_type = decl.type;
2497 lexer.PropertyParsing = true;
2498 parsing_indexer = true;
2500 indexer_parameters = decl.param_list;
2501 anonymous_host = SimpleAnonymousHost.GetSimple ();
2503 accessor_declarations
2505 lexer.PropertyParsing = false;
2506 has_get = has_set = false;
2507 parsing_indexer = false;
2514 // The signature is computed from the signature of the indexer. Look
2515 // at section 3.6 on the spec
2517 IndexerDeclaration decl = (IndexerDeclaration) $3;
2518 Accessors accessors = (Accessors) $6;
2519 Accessor get_block = accessors.get_or_add;
2520 Accessor set_block = accessors.set_or_remove;
2523 if (decl.interface_type != null)
2524 name = new MemberName (
2525 decl.interface_type, TypeContainer.DefaultIndexerName, decl.location);
2527 name = new MemberName (TypeContainer.DefaultIndexerName, decl.location);
2529 indexer = new Indexer (current_class, decl.type, name,
2530 (int) $2, false, decl.param_list, (Attributes) $1,
2531 get_block, set_block, accessors.declared_in_reverse);
2533 if (RootContext.Documentation != null)
2534 indexer.DocComment = ConsumeStoredComment ();
2536 current_container.AddIndexer (indexer);
2538 current_local_parameters = null;
2539 implicit_value_parameter_type = null;
2540 indexer_parameters = null;
2545 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2547 Parameters pars = (Parameters) $4;
2548 if (pars.HasArglist) {
2549 // "__arglist is not valid in this context"
2550 Report.Error (1669, (Location) $2, "__arglist is not valid in this context");
2551 } else if (pars.Empty){
2552 Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
2554 if (RootContext.Documentation != null) {
2555 tmpComment = Lexer.consume_doc_comment ();
2556 Lexer.doc_state = XmlCommentState.Allowed;
2559 $$ = new IndexerDeclaration ((Expression) $1, null, pars, (Location) $2);
2561 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2563 Parameters pars = (Parameters) $6;
2565 if (pars.HasArglist) {
2566 // "__arglist is not valid in this context"
2567 Report.Error (1669, (Location) $4, "__arglist is not valid in this context");
2568 } else if (pars.Empty){
2569 Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
2572 MemberName name = (MemberName) $2;
2573 $$ = new IndexerDeclaration ((Expression) $1, name, pars, (Location) $4);
2575 if (RootContext.Documentation != null) {
2576 tmpComment = Lexer.consume_doc_comment ();
2577 Lexer.doc_state = XmlCommentState.Allowed;
2587 if (RootContext.Documentation != null)
2588 enumTypeComment = Lexer.consume_doc_comment ();
2593 LocatedToken lt = (LocatedToken) $4;
2594 Location enum_location = lt.Location;
2596 MemberName name = MakeName (new MemberName (lt.Value, enum_location));
2597 Enum e = new Enum (current_namespace, current_class, (Expression) $5, (int) $2,
2598 name, (Attributes) $1);
2600 if (RootContext.Documentation != null)
2601 e.DocComment = enumTypeComment;
2604 EnumMember em = null;
2605 foreach (VariableDeclaration ev in (ArrayList) $7) {
2606 em = new EnumMember (e, em, (Expression) ev.expression_or_array_initializer,
2607 new MemberName (ev.identifier, ev.Location), ev.OptAttributes);
2609 // if (RootContext.Documentation != null)
2610 em.DocComment = ev.DocComment;
2612 e.AddEnumMember (em);
2615 current_container.AddEnum (e);
2622 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2623 | COLON type { $$ = $2; }
2629 if (RootContext.Documentation != null)
2630 Lexer.doc_state = XmlCommentState.Allowed;
2632 opt_enum_member_declarations
2634 // here will be evaluated after CLOSE_BLACE is consumed.
2635 if (RootContext.Documentation != null)
2636 Lexer.doc_state = XmlCommentState.Allowed;
2644 opt_enum_member_declarations
2645 : /* empty */ { $$ = new ArrayList (4); }
2646 | enum_member_declarations opt_comma { $$ = $1; }
2649 enum_member_declarations
2650 : enum_member_declaration
2652 ArrayList l = new ArrayList (4);
2657 | enum_member_declarations COMMA enum_member_declaration
2659 ArrayList l = (ArrayList) $1;
2667 enum_member_declaration
2668 : opt_attributes IDENTIFIER
2670 VariableDeclaration vd = new VariableDeclaration (
2671 (LocatedToken) $2, null, (Attributes) $1);
2673 if (RootContext.Documentation != null) {
2674 vd.DocComment = Lexer.consume_doc_comment ();
2675 Lexer.doc_state = XmlCommentState.Allowed;
2680 | opt_attributes IDENTIFIER
2682 if (RootContext.Documentation != null) {
2683 tmpComment = Lexer.consume_doc_comment ();
2684 Lexer.doc_state = XmlCommentState.NotAllowed;
2689 VariableDeclaration vd = new VariableDeclaration (
2690 (LocatedToken) $2, $5, (Attributes) $1);
2692 if (RootContext.Documentation != null)
2693 vd.DocComment = ConsumeStoredComment ();
2699 delegate_declaration
2704 lexer.ConstraintsParsing = true;
2707 open_parens opt_formal_parameter_list CLOSE_PARENS
2709 MemberName name = MakeName ((MemberName) $6);
2710 Parameters p = (Parameters) $8;
2712 // TODO: wrong location
2713 Report.Error (1669, name.Location, "__arglist is not valid in this context");
2716 Delegate del = new Delegate (current_namespace, current_class, (Expression) $5,
2717 (int) $2, name, p, (Attributes) $1);
2719 if (RootContext.Documentation != null) {
2720 del.DocComment = Lexer.consume_doc_comment ();
2721 Lexer.doc_state = XmlCommentState.Allowed;
2724 current_container.AddDelegate (del);
2725 current_delegate = del;
2727 opt_type_parameter_constraints_clauses
2729 lexer.ConstraintsParsing = false;
2733 current_delegate.SetParameterInfo ((ArrayList) $11);
2734 $$ = current_delegate;
2736 current_delegate = null;
2743 lexer.CheckNullable (false);
2748 lexer.CheckNullable (true);
2753 namespace_or_type_name
2755 | IDENTIFIER DOUBLE_COLON IDENTIFIER {
2756 LocatedToken lt1 = (LocatedToken) $1;
2757 LocatedToken lt2 = (LocatedToken) $3;
2758 $$ = new MemberName (lt1.Value, lt2.Value, lt2.Location);
2760 | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list {
2761 LocatedToken lt = (LocatedToken) $3;
2762 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $4, lt.Location);
2767 : IDENTIFIER opt_type_argument_list {
2768 LocatedToken lt = (LocatedToken) $1;
2769 $$ = new MemberName (lt.Value, (TypeArguments) $2, lt.Location);
2773 opt_type_argument_list
2774 : /* empty */ { $$ = null; }
2775 | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2778 if (RootContext.Version == LanguageVersion.ISO_1)
2779 Report.FeatureIsNotISO1 (lexer.Location, "generics");
2783 $$ = new TypeArguments ((int) $1, lexer.Location);
2784 if (RootContext.Version == LanguageVersion.ISO_1)
2785 Report.FeatureIsNotISO1 (lexer.Location, "generics");
2790 : opt_attributes type {
2791 TypeArguments type_args = new TypeArguments (lexer.Location);
2793 SimpleName sn = $2 as SimpleName;
2795 Report.Error (1031, lexer.Location, "Type expected");
2797 $2 = new TypeParameterName (sn.Name, (Attributes) $1, lexer.Location);
2799 type_args.Add ((Expression) $2);
2802 | type_arguments COMMA opt_attributes type {
2803 TypeArguments type_args = (TypeArguments) $1;
2805 SimpleName sn = $4 as SimpleName;
2807 Report.Error (1031, lexer.Location, "Type expected");
2809 $4 = new TypeParameterName (sn.Name, (Attributes) $3, lexer.Location);
2811 type_args.Add ((Expression) $4);
2818 * Before you think of adding a return_type, notice that we have been
2819 * using two rules in the places where it matters (one rule using type
2820 * and another identical one that uses VOID as the return type). This
2821 * gets rid of a shift/reduce couple
2824 : namespace_or_type_name opt_nullable
2826 MemberName name = (MemberName) $1;
2827 $$ = name.GetTypeExpression ();
2830 $$ = new ComposedCast ((Expression) $$, "?", lexer.Location);
2832 | builtin_types opt_nullable
2835 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2845 // Note that here only unmanaged types are allowed but we
2846 // can't perform checks during this phase - we do it during
2847 // semantic analysis.
2849 $$ = new ComposedCast ((Expression) $1, "*", Lexer.Location);
2853 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2858 : builtin_types opt_nullable
2861 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2863 | non_expression_type rank_specifier
2865 Location loc = GetLocation ($1);
2867 loc = lexer.Location;
2868 $$ = new ComposedCast ((Expression) $1, (string) $2, loc);
2870 | non_expression_type STAR
2872 Location loc = GetLocation ($1);
2874 loc = lexer.Location;
2875 $$ = new ComposedCast ((Expression) $1, "*", loc);
2877 | expression rank_specifiers
2879 $$ = new ComposedCast ((Expression) $1, (string) $2);
2883 $$ = new ComposedCast ((Expression) $1, "*");
2887 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2888 // through this different path
2890 | multiplicative_expression STAR
2892 $$ = new ComposedCast ((Expression) $1, "*");
2899 ArrayList types = new ArrayList (4);
2904 | type_list COMMA type
2906 ArrayList types = (ArrayList) $1;
2914 * replaces all the productions for isolating the various
2915 * simple types, but we need this to reuse it easily in local_variable_type
2918 : OBJECT { $$ = TypeManager.system_object_expr; }
2919 | STRING { $$ = TypeManager.system_string_expr; }
2920 | BOOL { $$ = TypeManager.system_boolean_expr; }
2921 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2922 | FLOAT { $$ = TypeManager.system_single_expr; }
2923 | DOUBLE { $$ = TypeManager.system_double_expr; }
2928 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2929 | BYTE { $$ = TypeManager.system_byte_expr; }
2930 | SHORT { $$ = TypeManager.system_int16_expr; }
2931 | USHORT { $$ = TypeManager.system_uint16_expr; }
2932 | INT { $$ = TypeManager.system_int32_expr; }
2933 | UINT { $$ = TypeManager.system_uint32_expr; }
2934 | LONG { $$ = TypeManager.system_int64_expr; }
2935 | ULONG { $$ = TypeManager.system_uint64_expr; }
2936 | CHAR { $$ = TypeManager.system_char_expr; }
2937 | VOID { $$ = TypeManager.system_void_expr; }
2941 : type rank_specifiers opt_nullable
2943 string rank_specifiers = (string) $2;
2945 rank_specifiers += "?";
2947 $$ = current_array_type = new ComposedCast ((Expression) $1, rank_specifiers);
2952 // Expressions, section 7.5
2961 MemberName mn = (MemberName) $1;
2962 $$ = mn.GetTypeExpression ();
2964 | IDENTIFIER DOUBLE_COLON IDENTIFIER
2966 LocatedToken lt1 = (LocatedToken) $1;
2967 LocatedToken lt2 = (LocatedToken) $3;
2968 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, lt2.Location);
2970 | parenthesized_expression
2971 | default_value_expression
2973 | invocation_expression
2977 | post_increment_expression
2978 | post_decrement_expression
2982 | checked_expression
2983 | unchecked_expression
2984 | pointer_member_access
2985 | anonymous_method_expression
2992 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
2993 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); }
2994 | NULL { $$ = new NullLiteral (lexer.Location); }
2998 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
2999 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
3000 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
3005 object v = lexer.Value;
3008 $$ = new IntLiteral ((int) v, lexer.Location);
3009 } else if (v is uint)
3010 $$ = new UIntLiteral ((UInt32) v, lexer.Location);
3012 $$ = new LongLiteral ((Int64) v, lexer.Location);
3013 else if (v is ulong)
3014 $$ = new ULongLiteral ((UInt64) v, lexer.Location);
3016 Console.WriteLine ("OOPS. Unexpected result from scanner");
3021 : TRUE { $$ = new BoolLiteral (true, lexer.Location); }
3022 | FALSE { $$ = new BoolLiteral (false, lexer.Location); }
3025 parenthesized_expression_0
3026 : OPEN_PARENS expression CLOSE_PARENS
3029 lexer.Deambiguate_CloseParens ($$);
3030 // After this, the next token returned is one of
3031 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST (CLOSE_PARENS), CLOSE_PARENS_OPEN_PARENS
3032 // or CLOSE_PARENS_MINUS.
3034 | OPEN_PARENS expression error { CheckToken (1026, yyToken, "Expecting ')'", lexer.Location); }
3037 parenthesized_expression
3038 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
3042 | parenthesized_expression_0 CLOSE_PARENS
3046 | parenthesized_expression_0 CLOSE_PARENS_MINUS
3048 // If a parenthesized expression is followed by a minus, we need to wrap
3049 // the expression inside a ParenthesizedExpression for the CS0075 check
3050 // in Binary.DoResolve().
3051 $$ = new ParenthesizedExpression ((Expression) $1);
3056 : primary_expression DOT IDENTIFIER opt_type_argument_list
3058 LocatedToken lt = (LocatedToken) $3;
3059 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
3061 | predefined_type DOT IDENTIFIER opt_type_argument_list
3063 LocatedToken lt = (LocatedToken) $3;
3064 // TODO: Location is wrong as some predefined types doesn't hold a location
3065 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
3073 invocation_expression
3074 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
3077 Report.Error (1, (Location) $2, "Parse error");
3079 $$ = new Invocation ((Expression) $1, (ArrayList) $3);
3081 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
3083 $$ = new Invocation ((Expression) $1, new ArrayList ());
3085 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
3087 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
3089 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
3091 ArrayList args = new ArrayList (1);
3093 $$ = new Invocation ((Expression) $1, args);
3095 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
3097 ArrayList args = ((ArrayList) $4);
3099 $$ = new Invocation ((Expression) $1, args);
3104 : /* empty */ { $$ = null; }
3111 ArrayList list = new ArrayList (4);
3115 | argument_list COMMA argument
3117 ArrayList list = (ArrayList) $1;
3121 | argument_list error {
3122 CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
3130 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
3132 | non_simple_argument
3139 : REF variable_reference
3141 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
3143 | OUT variable_reference
3145 $$ = new Argument ((Expression) $2, Argument.AType.Out);
3147 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
3149 ArrayList list = (ArrayList) $3;
3150 Argument[] args = new Argument [list.Count];
3151 list.CopyTo (args, 0);
3153 Expression expr = new Arglist (args, (Location) $1);
3154 $$ = new Argument (expr, Argument.AType.Expression);
3158 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
3163 : expression { note ("section 5.4"); $$ = $1; }
3167 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
3169 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
3171 | primary_expression rank_specifiers
3173 // So the super-trick is that primary_expression
3174 // can only be either a SimpleName or a MemberAccess.
3175 // The MemberAccess case arises when you have a fully qualified type-name like :
3177 // SimpleName is when you have
3180 Expression expr = (Expression) $1;
3181 if (expr is ComposedCast){
3182 $$ = new ComposedCast (expr, (string) $2);
3183 } else if (!(expr is SimpleName || expr is MemberAccess || expr is ConstructedType || expr is QualifiedAliasMember)){
3184 Error_ExpectingTypeName (expr);
3185 $$ = TypeManager.system_object_expr;
3188 // So we extract the string corresponding to the SimpleName
3191 $$ = new ComposedCast (expr, (string) $2);
3193 current_array_type = (Expression)$$;
3200 ArrayList list = new ArrayList (4);
3204 | expression_list COMMA expression
3206 ArrayList list = (ArrayList) $1;
3215 $$ = new This (current_block, (Location) $1);
3220 : BASE DOT IDENTIFIER opt_type_argument_list
3222 LocatedToken lt = (LocatedToken) $3;
3223 $$ = new BaseAccess (lt.Value, (TypeArguments) $4, lt.Location);
3225 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
3227 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
3230 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
3235 post_increment_expression
3236 : primary_expression OP_INC
3238 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
3239 (Expression) $1, (Location) $2);
3243 post_decrement_expression
3244 : primary_expression OP_DEC
3246 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
3247 (Expression) $1, (Location) $2);
3252 : object_or_delegate_creation_expression
3253 | array_creation_expression
3256 object_or_delegate_creation_expression
3257 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
3259 $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
3263 array_creation_expression
3264 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
3266 opt_array_initializer
3268 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
3270 | NEW type rank_specifiers array_initializer
3272 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, (Location) $1);
3276 Report.Error (1031, (Location) $1, "Type expected");
3281 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
3297 opt_rank_specifier_or_nullable
3306 | opt_nullable rank_specifiers
3313 | opt_nullable rank_specifiers INTERR
3316 $$ = "?" + $2 + "?";
3323 : rank_specifier opt_rank_specifier
3325 $$ = (string) $2 + (string) $1;
3330 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3332 $$ = "[" + (string) $2 + "]";
3352 | dim_separators COMMA
3354 $$ = (string) $1 + ",";
3358 opt_array_initializer
3370 : OPEN_BRACE CLOSE_BRACE
3372 ArrayList list = new ArrayList (4);
3375 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3377 $$ = (ArrayList) $2;
3381 variable_initializer_list
3382 : variable_initializer
3384 ArrayList list = new ArrayList (4);
3388 | variable_initializer_list COMMA variable_initializer
3390 ArrayList list = (ArrayList) $1;
3399 pushed_current_array_type = current_array_type;
3400 lexer.TypeOfParsing = true;
3402 OPEN_PARENS type CLOSE_PARENS
3404 lexer.TypeOfParsing = false;
3405 Expression type = (Expression)$4;
3406 if (type == TypeManager.system_void_expr)
3407 $$ = new TypeOfVoid ((Location) $1);
3409 $$ = new TypeOf (type, (Location) $1);
3410 current_array_type = pushed_current_array_type;
3415 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3416 $$ = new SizeOf ((Expression) $3, (Location) $1);
3421 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3423 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3427 unchecked_expression
3428 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3430 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3434 pointer_member_access
3435 : primary_expression OP_PTR IDENTIFIER
3438 LocatedToken lt = (LocatedToken) $3;
3440 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
3441 $$ = new MemberAccess (deref, lt.Value);
3445 anonymous_method_expression
3446 : DELEGATE opt_anonymous_method_signature
3448 if (oob_stack == null)
3449 oob_stack = new Stack (6);
3451 oob_stack.Push (current_anonymous_method);
3452 oob_stack.Push (current_local_parameters);
3453 current_local_parameters = (Parameters)$2;
3455 // Force the next block to be created as a ToplevelBlock
3456 oob_stack.Push (current_block);
3457 oob_stack.Push (top_current_block);
3459 Location loc = (Location) $1;
3460 current_anonymous_method = new AnonymousMethodExpression (
3461 current_anonymous_method, current_generic_method, current_container,
3462 (Parameters) $2, (ToplevelBlock) top_current_block, loc);
3464 parsing_anonymous_method = true;
3468 Location loc = (Location) $1;
3469 top_current_block = (Block) oob_stack.Pop ();
3470 current_block = (Block) oob_stack.Pop ();
3472 if (RootContext.Version == LanguageVersion.ISO_1){
3473 Report.FeatureIsNotISO1 (loc, "anonymous methods");
3476 ToplevelBlock anon_block = (ToplevelBlock) $4;
3478 anon_block.Parent = current_block;
3480 current_anonymous_method.Block = anon_block;
3481 if ((anonymous_host != null) && (current_anonymous_method.Parent == null))
3482 anonymous_host.AddAnonymousMethod (current_anonymous_method);
3484 $$ = current_anonymous_method;
3487 current_local_parameters = (Parameters) oob_stack.Pop ();
3488 current_anonymous_method = (AnonymousMethodExpression) oob_stack.Pop ();
3492 opt_anonymous_method_signature
3493 : /* empty */ { $$ = null; }
3494 | anonymous_method_signature
3497 anonymous_method_signature
3498 : open_parens opt_anonymous_method_parameter_list CLOSE_PARENS
3501 $$ = Parameters.EmptyReadOnlyParameters;
3503 ArrayList par_list = (ArrayList) $2;
3504 Parameter [] pars = new Parameter [par_list.Count];
3505 par_list.CopyTo (pars);
3506 $$ = new Parameters (pars);
3511 opt_anonymous_method_parameter_list
3512 : /* empty */ { $$ = null; }
3513 | anonymous_method_parameter_list { $$ = $1; }
3516 anonymous_method_parameter_list
3517 : anonymous_method_parameter
3519 ArrayList a = new ArrayList (4);
3523 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3525 ArrayList a = (ArrayList) $1;
3531 anonymous_method_parameter
3532 : opt_parameter_modifier type IDENTIFIER {
3533 LocatedToken lt = (LocatedToken) $3;
3534 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3536 | PARAMS type IDENTIFIER {
3537 Report.Error (1670, ((LocatedToken) $3).Location, "The `params' modifier is not allowed in anonymous method declaration");
3542 default_value_expression
3543 : DEFAULT_OPEN_PARENS type CLOSE_PARENS
3545 $$ = new DefaultValueExpression ((Expression) $2, lexer.Location);
3550 : primary_expression
3551 | BANG prefixed_unary_expression
3553 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3555 | TILDE prefixed_unary_expression
3557 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3563 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3565 $$ = new Cast ((Expression) $1, (Expression) $3);
3567 | parenthesized_expression_0 CLOSE_PARENS_NO_CAST default_value_expression
3569 $$ = new Cast ((Expression) $1, (Expression) $3);
3571 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3573 $$ = new Cast ((Expression) $1, (Expression) $3);
3579 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3581 // TODO: wrong location
3582 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3587 // The idea to split this out is from Rhys' grammar
3588 // to solve the problem with casts.
3590 prefixed_unary_expression
3592 | PLUS prefixed_unary_expression
3594 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3596 | MINUS prefixed_unary_expression
3598 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3600 | OP_INC prefixed_unary_expression
3602 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3603 (Expression) $2, (Location) $1);
3605 | OP_DEC prefixed_unary_expression
3607 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3608 (Expression) $2, (Location) $1);
3610 | STAR prefixed_unary_expression
3612 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, (Location) $1);
3614 | BITWISE_AND prefixed_unary_expression
3616 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3620 pre_increment_expression
3621 : OP_INC prefixed_unary_expression
3623 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3624 (Expression) $2, (Location) $1);
3628 pre_decrement_expression
3629 : OP_DEC prefixed_unary_expression
3631 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3632 (Expression) $2, (Location) $1);
3636 multiplicative_expression
3637 : prefixed_unary_expression
3638 | multiplicative_expression STAR prefixed_unary_expression
3640 $$ = new Binary (Binary.Operator.Multiply,
3641 (Expression) $1, (Expression) $3);
3643 | multiplicative_expression DIV prefixed_unary_expression
3645 $$ = new Binary (Binary.Operator.Division,
3646 (Expression) $1, (Expression) $3);
3648 | multiplicative_expression PERCENT prefixed_unary_expression
3650 $$ = new Binary (Binary.Operator.Modulus,
3651 (Expression) $1, (Expression) $3);
3656 : multiplicative_expression
3657 | additive_expression PLUS multiplicative_expression
3659 $$ = new Binary (Binary.Operator.Addition,
3660 (Expression) $1, (Expression) $3);
3662 | additive_expression MINUS multiplicative_expression
3664 $$ = new Binary (Binary.Operator.Subtraction,
3665 (Expression) $1, (Expression) $3);
3670 : additive_expression
3671 | shift_expression OP_SHIFT_LEFT additive_expression
3673 $$ = new Binary (Binary.Operator.LeftShift,
3674 (Expression) $1, (Expression) $3);
3676 | shift_expression OP_SHIFT_RIGHT additive_expression
3678 $$ = new Binary (Binary.Operator.RightShift,
3679 (Expression) $1, (Expression) $3);
3690 lexer.PutbackNullable ();
3695 nullable_type_or_conditional
3698 if (((bool) $2) && ($1 is ComposedCast))
3699 $$ = ((ComposedCast) $1).RemoveNullable ();
3705 relational_expression
3707 | relational_expression OP_LT shift_expression
3709 $$ = new Binary (Binary.Operator.LessThan,
3710 (Expression) $1, (Expression) $3);
3712 | relational_expression OP_GT shift_expression
3714 $$ = new Binary (Binary.Operator.GreaterThan,
3715 (Expression) $1, (Expression) $3);
3717 | relational_expression OP_LE shift_expression
3719 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3720 (Expression) $1, (Expression) $3);
3722 | relational_expression OP_GE shift_expression
3724 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3725 (Expression) $1, (Expression) $3);
3727 | relational_expression IS
3730 } nullable_type_or_conditional
3732 $$ = new Is ((Expression) $1, (Expression) $4, (Location) $2);
3734 | relational_expression AS
3737 } nullable_type_or_conditional
3739 $$ = new As ((Expression) $1, (Expression) $4, (Location) $2);
3744 : relational_expression
3745 | equality_expression OP_EQ relational_expression
3747 $$ = new Binary (Binary.Operator.Equality,
3748 (Expression) $1, (Expression) $3);
3750 | equality_expression OP_NE relational_expression
3752 $$ = new Binary (Binary.Operator.Inequality,
3753 (Expression) $1, (Expression) $3);
3758 : equality_expression
3759 | and_expression BITWISE_AND equality_expression
3761 $$ = new Binary (Binary.Operator.BitwiseAnd,
3762 (Expression) $1, (Expression) $3);
3766 exclusive_or_expression
3768 | exclusive_or_expression CARRET and_expression
3770 $$ = new Binary (Binary.Operator.ExclusiveOr,
3771 (Expression) $1, (Expression) $3);
3775 inclusive_or_expression
3776 : exclusive_or_expression
3777 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3779 $$ = new Binary (Binary.Operator.BitwiseOr,
3780 (Expression) $1, (Expression) $3);
3784 conditional_and_expression
3785 : inclusive_or_expression
3786 | conditional_and_expression OP_AND inclusive_or_expression
3788 $$ = new Binary (Binary.Operator.LogicalAnd,
3789 (Expression) $1, (Expression) $3);
3793 conditional_or_expression
3794 : conditional_and_expression
3795 | conditional_or_expression OP_OR conditional_and_expression
3797 $$ = new Binary (Binary.Operator.LogicalOr,
3798 (Expression) $1, (Expression) $3);
3802 conditional_expression
3803 : conditional_or_expression
3804 | conditional_or_expression INTERR expression COLON expression
3806 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
3808 | conditional_or_expression INTERR INTERR expression
3810 $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $4, lexer.Location);
3812 // We'll be resolved into a `parenthesized_expression_0' later on.
3813 | conditional_or_expression INTERR CLOSE_PARENS
3815 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
3816 lexer.PutbackCloseParens ();
3820 assignment_expression
3821 : prefixed_unary_expression ASSIGN expression
3823 $$ = new Assign ((Expression) $1, (Expression) $3);
3825 | prefixed_unary_expression OP_MULT_ASSIGN expression
3827 $$ = new CompoundAssign (
3828 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
3830 | prefixed_unary_expression OP_DIV_ASSIGN expression
3832 $$ = new CompoundAssign (
3833 Binary.Operator.Division, (Expression) $1, (Expression) $3);
3835 | prefixed_unary_expression OP_MOD_ASSIGN expression
3837 $$ = new CompoundAssign (
3838 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
3840 | prefixed_unary_expression OP_ADD_ASSIGN expression
3842 $$ = new CompoundAssign (
3843 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
3845 | prefixed_unary_expression OP_SUB_ASSIGN expression
3847 $$ = new CompoundAssign (
3848 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3850 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3852 $$ = new CompoundAssign (
3853 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
3855 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3857 $$ = new CompoundAssign (
3858 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
3860 | prefixed_unary_expression OP_AND_ASSIGN expression
3862 $$ = new CompoundAssign (
3863 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
3865 | prefixed_unary_expression OP_OR_ASSIGN expression
3867 $$ = new CompoundAssign (
3868 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
3870 | prefixed_unary_expression OP_XOR_ASSIGN expression
3872 $$ = new CompoundAssign (
3873 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
3877 implicitly_typed_lambda_parameter_list
3879 ArrayList a = new ArrayList (4);
3883 | implicitly_typed_lambda_parameter_list COMMA IDENTIFIER {
3884 ArrayList a = (ArrayList) $1;
3890 explicitly_typed_lambda_parameter_list
3891 : explicitly_typed_lambda_parameter
3893 ArrayList pars = new ArrayList (4);
3898 | explicitly_typed_lambda_parameter_list COMMA explicitly_typed_lambda_parameter
3900 ArrayList pars = (ArrayList) $1;
3907 explicitly_typed_lambda_parameter
3908 : parameter_modifier type IDENTIFIER
3910 LocatedToken lt = (LocatedToken) $3;
3912 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3916 LocatedToken lt = (LocatedToken) $2;
3918 $$ = new Parameter ((Expression) $2, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
3922 lambda_parameter_list
3923 : implicitly_typed_lambda_parameter_list { $$ = $1; }
3924 | explicitly_typed_lambda_parameter_list { $$ = $1; }
3927 lambda_expression_body
3933 : IDENTIFIER ARROW lambda_expression_body
3935 Console.WriteLine ("return");
3936 ArrayList a = new ArrayList (1);
3938 $$ = new LambdaExpression (a, $3, (Location) $2);
3940 | OPEN_PARENS CLOSE_PARENS ARROW lambda_expression_body {
3941 $$ = new LambdaExpression (new ArrayList (), $4, (Location) $3);
3943 | OPEN_PARENS_LAMBDA lambda_parameter_list CLOSE_PARENS ARROW lambda_expression_body
3945 $$ = new LambdaExpression ((ArrayList) $2, $5, (Location) $4);
3950 : conditional_expression
3951 | assignment_expression
3972 lexer.ConstraintsParsing = true;
3976 MemberName name = MakeName ((MemberName) $6);
3977 int mod_flags = (int) $2;
3979 push_current_class (new Class (
3980 current_namespace, current_class, name,
3981 mod_flags, (Attributes) $1), false, $3);
3984 opt_type_parameter_constraints_clauses
3986 lexer.ConstraintsParsing = false;
3989 if (current_class.Name == "System.Object") {
3990 Report.Error (537, current_class.Location,
3991 "The class System.Object cannot have a base " +
3992 "class or implement an interface.");
3994 current_container.AddBasesForPart (current_class, (ArrayList) $8);
3997 current_class.SetParameterInfo ((ArrayList) $9);
3999 if (RootContext.Documentation != null) {
4000 current_container.DocComment = Lexer.consume_doc_comment ();
4001 Lexer.doc_state = XmlCommentState.Allowed;
4006 if (RootContext.Documentation != null)
4007 Lexer.doc_state = XmlCommentState.Allowed;
4011 $$ = pop_current_class ();
4019 { $$ = $1; } // location
4023 : /* empty */ { $$ = (int) 0; }
4029 | modifiers modifier
4034 if ((m1 & m2) != 0) {
4035 Location l = lexer.Location;
4036 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
4038 $$ = (int) (m1 | m2);
4043 : NEW { $$ = Modifiers.NEW; }
4044 | PUBLIC { $$ = Modifiers.PUBLIC; }
4045 | PROTECTED { $$ = Modifiers.PROTECTED; }
4046 | INTERNAL { $$ = Modifiers.INTERNAL; }
4047 | PRIVATE { $$ = Modifiers.PRIVATE; }
4048 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
4049 | SEALED { $$ = Modifiers.SEALED; }
4050 | STATIC { $$ = Modifiers.STATIC; }
4051 | READONLY { $$ = Modifiers.READONLY; }
4052 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
4053 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
4054 | EXTERN { $$ = Modifiers.EXTERN; }
4055 | VOLATILE { $$ = Modifiers.VOLATILE; }
4056 | UNSAFE { $$ = Modifiers.UNSAFE; }
4060 : /* empty */ { $$ = null; }
4061 | class_base { $$ = $1; }
4065 : COLON type_list { $$ = $2; }
4068 opt_type_parameter_constraints_clauses
4069 : /* empty */ { $$ = null; }
4070 | type_parameter_constraints_clauses
4074 type_parameter_constraints_clauses
4075 : type_parameter_constraints_clause {
4076 ArrayList constraints = new ArrayList (1);
4077 constraints.Add ($1);
4080 | type_parameter_constraints_clauses type_parameter_constraints_clause {
4081 ArrayList constraints = (ArrayList) $1;
4082 Constraints new_constraint = (Constraints)$2;
4084 foreach (Constraints c in constraints) {
4085 if (new_constraint.TypeParameter == c.TypeParameter) {
4086 Report.Error (409, new_constraint.Location, "A constraint clause has already been specified for type parameter `{0}'",
4087 new_constraint.TypeParameter);
4091 constraints.Add (new_constraint);
4096 type_parameter_constraints_clause
4097 : WHERE IDENTIFIER COLON type_parameter_constraints {
4098 LocatedToken lt = (LocatedToken) $2;
4099 $$ = new Constraints (lt.Value, (ArrayList) $4, lt.Location);
4103 type_parameter_constraints
4104 : type_parameter_constraint {
4105 ArrayList constraints = new ArrayList (1);
4106 constraints.Add ($1);
4109 | type_parameter_constraints COMMA type_parameter_constraint {
4110 ArrayList constraints = (ArrayList) $1;
4112 constraints.Add ($3);
4117 type_parameter_constraint
4119 | NEW OPEN_PARENS CLOSE_PARENS {
4120 $$ = SpecialConstraint.Constructor;
4123 $$ = SpecialConstraint.ReferenceType;
4126 $$ = SpecialConstraint.ValueType;
4135 // A block is "contained" on the following places:
4137 // property_declaration as part of the accessor body (get/set)
4138 // operator_declaration
4139 // constructor_declaration
4140 // destructor_declaration
4141 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
4146 if (parsing_anonymous_method) {
4147 top_current_block = new ToplevelBlock (
4148 current_block, current_local_parameters, current_generic_method,
4150 if (current_block != null)
4151 current_block.AddAnonymousChild ((ToplevelBlock) top_current_block);
4152 current_block = top_current_block;
4153 parsing_anonymous_method = false;
4154 } else if (current_block == null) {
4155 current_block = new ToplevelBlock (
4156 (ToplevelBlock) top_current_block, current_local_parameters,
4157 current_generic_method, (Location) $1);
4158 top_current_block = current_block;
4160 current_block = new Block (current_block, (Location) $1, Location.Null);
4163 opt_statement_list CLOSE_BRACE
4165 while (current_block.Implicit)
4166 current_block = current_block.Parent;
4168 current_block.SetEndLocation ((Location) $4);
4169 current_block = current_block.Parent;
4170 if (current_block == null)
4171 top_current_block = null;
4182 | statement_list statement
4186 : declaration_statement
4188 if ($1 != null && (Block) $1 != current_block){
4189 current_block.AddStatement ((Statement) $1);
4190 current_block = (Block) $1;
4193 | valid_declaration_statement
4195 current_block.AddStatement ((Statement) $1);
4200 valid_declaration_statement
4203 | expression_statement
4204 | selection_statement
4205 | iteration_statement
4209 | unchecked_statement
4217 : valid_declaration_statement
4218 | declaration_statement
4220 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4225 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4233 $$ = EmptyStatement.Value;
4240 LocatedToken lt = (LocatedToken) $1;
4241 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
4243 if (current_block.AddLabel (labeled))
4244 current_block.AddStatement (labeled);
4249 declaration_statement
4250 : local_variable_declaration SEMICOLON
4252 current_array_type = null;
4254 DictionaryEntry de = (DictionaryEntry) $1;
4255 Expression e = (Expression) de.Key;
4257 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
4261 | local_constant_declaration SEMICOLON
4263 current_array_type = null;
4265 DictionaryEntry de = (DictionaryEntry) $1;
4267 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
4273 * The following is from Rhys' grammar:
4274 * > Types in local variable declarations must be recognized as
4275 * > expressions to prevent reduce/reduce errors in the grammar.
4276 * > The expressions are converted into types during semantic analysis.
4279 : primary_expression opt_rank_specifier_or_nullable
4281 // FIXME: Do something smart here regarding the composition of the type.
4283 // Ok, the above "primary_expression" is there to get rid of
4284 // both reduce/reduce and shift/reduces in the grammar, it should
4285 // really just be "type_name". If you use type_name, a reduce/reduce
4286 // creeps up. If you use namespace_or_type_name (which is all we need
4287 // really) two shift/reduces appear.
4290 // So the super-trick is that primary_expression
4291 // can only be either a SimpleName or a MemberAccess.
4292 // The MemberAccess case arises when you have a fully qualified type-name like :
4294 // SimpleName is when you have
4297 Expression expr = (Expression) $1;
4298 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType || expr is QualifiedAliasMember)) {
4299 Error_ExpectingTypeName (expr);
4303 // So we extract the string corresponding to the SimpleName
4307 if ((string) $2 == "")
4310 $$ = new ComposedCast ((Expression) $1, (string) $2);
4313 | builtin_types opt_rank_specifier_or_nullable
4315 if ((string) $2 == "")
4318 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
4322 local_variable_pointer_type
4323 : primary_expression STAR
4325 Expression expr = (Expression) $1;
4327 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType || expr is QualifiedAliasMember)) {
4328 Error_ExpectingTypeName (expr);
4332 $$ = new ComposedCast ((Expression) $1, "*");
4334 | builtin_types STAR
4336 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
4340 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
4342 | local_variable_pointer_type STAR
4344 $$ = new ComposedCast ((Expression) $1, "*");
4348 local_variable_declaration
4349 : local_variable_type variable_declarators
4352 $$ = new DictionaryEntry ($1, $2);
4356 | local_variable_pointer_type opt_rank_specifier_or_nullable variable_declarators
4361 if ((string) $2 == "")
4362 t = (Expression) $1;
4364 t = new ComposedCast ((Expression) $1, (string) $2);
4365 $$ = new DictionaryEntry (t, $3);
4371 local_constant_declaration
4372 : CONST local_variable_type constant_declarators
4375 $$ = new DictionaryEntry ($2, $3);
4381 expression_statement
4382 : statement_expression SEMICOLON { $$ = $1; }
4386 // We have to do the wrapping here and not in the case above,
4387 // because statement_expression is used for example in for_statement
4389 statement_expression
4392 Expression expr = (Expression) $1;
4393 ExpressionStatement s = expr as ExpressionStatement;
4395 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
4398 $$ = new StatementExpression (s);
4402 Report.Error (1002, GetLocation ($1), "Expecting `;'");
4407 object_creation_expression
4408 : object_or_delegate_creation_expression
4409 { note ("complain if this is a delegate maybe?"); }
4418 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
4421 Location l = (Location) $1;
4423 $$ = new If ((Expression) $3, (Statement) $5, l);
4425 // FIXME: location for warning should be loc property of $5.
4426 if ($5 == EmptyStatement.Value)
4427 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4430 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
4431 embedded_statement ELSE embedded_statement
4433 Location l = (Location) $1;
4435 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
4437 // FIXME: location for warning should be loc property of $5 and $7.
4438 if ($5 == EmptyStatement.Value)
4439 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4440 if ($7 == EmptyStatement.Value)
4441 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4446 : SWITCH OPEN_PARENS
4448 if (switch_stack == null)
4449 switch_stack = new Stack (2);
4450 switch_stack.Push (current_block);
4452 expression CLOSE_PARENS
4455 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
4456 current_block = (Block) switch_stack.Pop ();
4472 Report.Warning (1522, 1, lexer.Location, "Empty switch block");
4473 $$ = new ArrayList ();
4481 ArrayList sections = new ArrayList (4);
4486 | switch_sections switch_section
4488 ArrayList sections = (ArrayList) $1;
4498 current_block = current_block.CreateSwitchBlock (lexer.Location);
4502 Block topmost = current_block;
4504 while (topmost.Implicit)
4505 topmost = topmost.Parent;
4506 $$ = new SwitchSection ((ArrayList) $1, topmost);
4513 ArrayList labels = new ArrayList (4);
4518 | switch_labels switch_label
4520 ArrayList labels = (ArrayList) ($1);
4528 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
4529 | DEFAULT_COLON { $$ = new SwitchLabel (null, (Location) $1); }
4532 1523, GetLocation ($1),
4533 "The keyword case or default must precede code in switch block");
4545 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
4547 Location l = (Location) $1;
4548 $$ = new While ((Expression) $3, (Statement) $5, l);
4553 : DO embedded_statement
4554 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
4556 Location l = (Location) $1;
4558 $$ = new Do ((Statement) $2, (Expression) $5, l);
4564 opt_for_initializer SEMICOLON
4566 Block assign_block = new Block (current_block);
4567 current_block = assign_block;
4569 if ($3 is DictionaryEntry){
4570 DictionaryEntry de = (DictionaryEntry) $3;
4572 Expression type = (Expression) de.Key;
4573 ArrayList var_declarators = (ArrayList) de.Value;
4575 foreach (VariableDeclaration decl in var_declarators){
4579 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4583 Location l = lexer.Location;
4584 Expression expr = decl.expression_or_array_initializer;
4586 LocalVariableReference var;
4587 var = new LocalVariableReference (assign_block, decl.identifier, l);
4590 Assign a = new Assign (var, expr, decl.Location);
4592 assign_block.AddStatement (new StatementExpression (a));
4596 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4597 // This can be referred to as $5 below.
4603 opt_for_condition SEMICOLON
4604 opt_for_iterator CLOSE_PARENS
4607 Location l = (Location) $1;
4609 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4611 current_block.AddStatement (f);
4612 while (current_block.Implicit)
4613 current_block = current_block.Parent;
4615 current_block = current_block.Parent;
4620 : /* empty */ { $$ = EmptyStatement.Value; }
4625 : local_variable_declaration
4626 | statement_expression_list
4630 : /* empty */ { $$ = null; }
4631 | boolean_expression
4635 : /* empty */ { $$ = EmptyStatement.Value; }
4640 : statement_expression_list
4643 statement_expression_list
4644 : statement_expression
4646 // CHANGE: was `null'
4647 Statement s = (Statement) $1;
4648 Block b = new Block (current_block, Block.Flags.Implicit, s.loc, lexer.Location);
4653 | statement_expression_list COMMA statement_expression
4655 Block b = (Block) $1;
4657 b.AddStatement ((Statement) $3);
4663 : FOREACH open_parens type IN expression CLOSE_PARENS
4665 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4668 | FOREACH open_parens type IDENTIFIER IN
4669 expression CLOSE_PARENS
4671 Block foreach_block = new Block (current_block);
4672 current_block = foreach_block;
4674 LocatedToken lt = (LocatedToken) $4;
4675 Location l = lt.Location;
4678 vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4680 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4682 // Get a writable reference to this read-only variable.
4684 // Note that the $$ here refers to the value of _this_ code block,
4685 // not the value of the LHS non-terminal. This can be referred to as $8 below.
4686 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
4693 LocalVariableReference v = (LocalVariableReference) $8;
4694 Location l = (Location) $1;
4697 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
4698 current_block.AddStatement (f);
4701 while (current_block.Implicit)
4702 current_block = current_block.Parent;
4704 current_block = current_block.Parent;
4710 | continue_statement
4720 $$ = new Break ((Location) $1);
4725 : CONTINUE SEMICOLON
4727 $$ = new Continue ((Location) $1);
4732 : GOTO IDENTIFIER SEMICOLON
4734 LocatedToken lt = (LocatedToken) $2;
4735 $$ = new Goto (lt.Value, lt.Location);
4737 | GOTO CASE constant_expression SEMICOLON
4739 $$ = new GotoCase ((Expression) $3, (Location) $1);
4741 | GOTO DEFAULT SEMICOLON
4743 $$ = new GotoDefault ((Location) $1);
4748 : RETURN opt_expression SEMICOLON
4750 $$ = new Return ((Expression) $2, (Location) $1);
4755 : THROW opt_expression SEMICOLON
4757 $$ = new Throw ((Expression) $2, (Location) $1);
4762 : IDENTIFIER RETURN expression SEMICOLON
4764 LocatedToken lt = (LocatedToken) $1;
4765 string s = lt.Value;
4767 Report.Error (1003, lt.Location, "; expected");
4770 if (RootContext.Version == LanguageVersion.ISO_1){
4771 Report.FeatureIsNotISO1 (lt.Location, "yield statement");
4774 if (anonymous_host == null){
4775 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4778 anonymous_host.SetYields ();
4779 $$ = new Yield ((Expression) $3, lt.Location);
4782 | IDENTIFIER RETURN SEMICOLON
4784 Report.Error (1627, (Location) $2, "Expression expected after yield return");
4787 | IDENTIFIER BREAK SEMICOLON
4789 LocatedToken lt = (LocatedToken) $1;
4790 string s = lt.Value;
4792 Report.Error (1003, lt.Location, "; expected");
4795 if (RootContext.Version == LanguageVersion.ISO_1){
4796 Report.FeatureIsNotISO1 (lt.Location, "yield statement");
4799 if (anonymous_host == null){
4800 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4803 anonymous_host.SetYields ();
4804 $$ = new YieldBreak (lt.Location);
4815 : TRY block catch_clauses
4819 ArrayList c = (ArrayList)$3;
4820 for (int i = 0; i < c.Count; ++i) {
4821 Catch cc = (Catch) c [i];
4823 if (i != c.Count - 1)
4824 Report.Error (1017, cc.loc, "Try statement already has an empty catch block");
4831 // Now s contains the list of specific catch clauses
4832 // and g contains the general one.
4834 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4836 | TRY block opt_catch_clauses FINALLY block
4839 ArrayList s = new ArrayList (4);
4840 ArrayList catch_list = (ArrayList) $3;
4842 if (catch_list != null){
4843 foreach (Catch cc in catch_list) {
4851 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4855 Report.Error (1524, (Location) $1, "Expected catch or finally");
4861 : /* empty */ { $$ = null; }
4868 ArrayList l = new ArrayList (4);
4873 | catch_clauses catch_clause
4875 ArrayList l = (ArrayList) $1;
4883 : /* empty */ { $$ = null; }
4888 : CATCH opt_catch_args
4890 Expression type = null;
4893 DictionaryEntry cc = (DictionaryEntry) $2;
4894 type = (Expression) cc.Key;
4895 LocatedToken lt = (LocatedToken) cc.Value;
4898 ArrayList one = new ArrayList (4);
4900 one.Add (new VariableDeclaration (lt, null));
4902 current_block = new Block (current_block);
4903 Block b = declare_local_variables (type, one, lt.Location);
4908 Expression type = null;
4910 Block var_block = null;
4913 DictionaryEntry cc = (DictionaryEntry) $2;
4914 type = (Expression) cc.Key;
4915 LocatedToken lt = (LocatedToken) cc.Value;
4919 while (current_block.Implicit)
4920 current_block = current_block.Parent;
4921 var_block = current_block;
4922 current_block = current_block.Parent;
4926 $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
4931 : /* empty */ { $$ = null; }
4936 : open_parens type opt_identifier CLOSE_PARENS
4938 $$ = new DictionaryEntry ($2, $3);
4946 $$ = new Checked ((Block) $2);
4953 $$ = new Unchecked ((Block) $2);
4960 RootContext.CheckUnsafeOption ((Location) $1);
4962 $$ = new Unsafe ((Block) $3);
4968 type fixed_pointer_declarators
4971 ArrayList list = (ArrayList) $4;
4972 Expression type = (Expression) $3;
4973 Location l = (Location) $1;
4974 int top = list.Count;
4976 Block assign_block = new Block (current_block);
4977 current_block = assign_block;
4979 for (int i = 0; i < top; i++){
4980 Pair p = (Pair) list [i];
4983 v = current_block.AddVariable (type, (string) p.First, l);
4987 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4995 Location l = (Location) $1;
4997 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4999 current_block.AddStatement (f);
5000 while (current_block.Implicit)
5001 current_block = current_block.Parent;
5003 current_block = current_block.Parent;
5007 fixed_pointer_declarators
5008 : fixed_pointer_declarator {
5009 ArrayList declarators = new ArrayList (4);
5011 declarators.Add ($1);
5014 | fixed_pointer_declarators COMMA fixed_pointer_declarator
5016 ArrayList declarators = (ArrayList) $1;
5018 declarators.Add ($3);
5023 fixed_pointer_declarator
5024 : IDENTIFIER ASSIGN expression
5026 LocatedToken lt = (LocatedToken) $1;
5027 // FIXME: keep location
5028 $$ = new Pair (lt.Value, $3);
5032 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
5038 : LOCK OPEN_PARENS expression CLOSE_PARENS
5044 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
5049 : USING open_parens resource_acquisition CLOSE_PARENS
5051 Block assign_block = new Block (current_block);
5052 current_block = assign_block;
5054 if ($3 is DictionaryEntry){
5055 DictionaryEntry de = (DictionaryEntry) $3;
5056 Location l = (Location) $1;
5058 Expression type = (Expression) de.Key;
5059 ArrayList var_declarators = (ArrayList) de.Value;
5061 ArrayList vars = new ArrayList (4);
5063 foreach (VariableDeclaration decl in var_declarators){
5065 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
5068 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
5070 Expression expr = decl.expression_or_array_initializer;
5072 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
5075 LocalVariableReference var;
5077 // Get a writable reference to this read-only variable.
5078 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
5080 // This is so that it is not a warning on using variables
5083 vars.Add (new DictionaryEntry (var, expr));
5085 // Assign a = new Assign (var, expr, decl.Location);
5086 // assign_block.AddStatement (new StatementExpression (a));
5089 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
5090 // It can be referred to as $5 below.
5091 $$ = new DictionaryEntry (type, vars);
5098 Using u = new Using ($5, (Statement) $6, (Location) $1);
5099 current_block.AddStatement (u);
5100 while (current_block.Implicit)
5101 current_block = current_block.Parent;
5103 current_block = current_block.Parent;
5107 resource_acquisition
5108 : local_variable_declaration
5115 // A class used to pass around variable declarations and constants
5117 public class VariableDeclaration {
5118 public string identifier;
5119 public Expression expression_or_array_initializer;
5120 public Location Location;
5121 public Attributes OptAttributes;
5122 public string DocComment;
5124 public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
5126 this.identifier = lt.Value;
5127 if (eoai is ArrayList) {
5128 if (CSharpParser.current_array_type == null)
5129 Report.Error (622, lt.Location,
5130 "Can only use array initializer expressions to assign to array types. Try using a new expression instead.");
5131 this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
5133 this.expression_or_array_initializer = (Expression)eoai;
5135 this.Location = lt.Location;
5136 this.OptAttributes = opt_attrs;
5139 public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
5145 // A class used to hold info about an indexer declarator
5147 public class IndexerDeclaration {
5148 public Expression type;
5149 public MemberName interface_type;
5150 public Parameters param_list;
5151 public Location location;
5153 public IndexerDeclaration (Expression type, MemberName interface_type,
5154 Parameters param_list, Location loc)
5157 this.interface_type = interface_type;
5158 this.param_list = param_list;
5159 this.location = loc;
5164 // We use this when we do not have an object in advance that is an IAnonymousHost
5166 public class SimpleAnonymousHost : IAnonymousHost {
5167 public static readonly SimpleAnonymousHost Simple = new SimpleAnonymousHost ();
5170 ArrayList anonymous_methods;
5172 public static SimpleAnonymousHost GetSimple () {
5173 Simple.yields = false;
5174 Simple.anonymous_methods = null;
5178 public void SetYields ()
5183 public void AddAnonymousMethod (AnonymousMethodExpression anonymous)
5185 if (anonymous_methods == null)
5186 anonymous_methods = new ArrayList ();
5187 anonymous_methods.Add (anonymous);
5190 public void Propagate (IAnonymousHost real_host)
5193 real_host.SetYields ();
5194 if (anonymous_methods != null) {
5195 foreach (AnonymousMethodExpression ame in anonymous_methods)
5196 real_host.AddAnonymousMethod (ame);
5202 // A class used to hold info about an operator declarator
5204 public class OperatorDeclaration {
5205 public Operator.OpType optype;
5206 public Expression ret_type, arg1type, arg2type;
5207 public string arg1name, arg2name;
5208 public Location location;
5210 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
5211 Expression arg1type, string arg1name,
5212 Expression arg2type, string arg2name, Location location)
5215 this.ret_type = ret_type;
5216 this.arg1type = arg1type;
5217 this.arg1name = arg1name;
5218 this.arg2type = arg2type;
5219 this.arg2name = arg2name;
5220 this.location = location;
5225 void Error_ExpectingTypeName (Expression expr)
5227 if (expr is Invocation){
5228 Report.Error (1002, expr.Location, "Expecting `;'");
5230 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
5234 public static void Error_ParameterModifierNotValid (Location loc)
5236 Report.Error (631, loc, "The modifiers `ref' and `out' are not valid in this context");
5239 void push_current_class (TypeContainer tc, bool is_interface, object partial_token)
5241 if (partial_token != null)
5242 current_container = current_container.AddPartial (tc, is_interface);
5244 current_container = current_container.AddTypeContainer (tc, is_interface);
5248 DeclSpace pop_current_class ()
5250 DeclSpace retval = current_class;
5252 current_class = current_class.Parent;
5253 current_container = current_class.PartialContainer;
5259 // Given the @class_name name, it creates a fully qualified name
5260 // based on the containing declaration space
5263 MakeName (MemberName class_name)
5265 Namespace ns = current_namespace.NS;
5267 if (current_container.Name.Length == 0){
5268 if (ns.Name.Length != 0)
5269 return new MemberName (ns.MemberName, class_name);
5273 return new MemberName (current_container.MemberName, class_name);
5277 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
5279 Block implicit_block;
5280 ArrayList inits = null;
5283 // We use the `Used' property to check whether statements
5284 // have been added to the current block. If so, we need
5285 // to create another block to contain the new declaration
5286 // otherwise, as an optimization, we use the same block to
5287 // add the declaration.
5289 // FIXME: A further optimization is to check if the statements
5290 // that were added were added as part of the initialization
5291 // below. In which case, no other statements have been executed
5292 // and we might be able to reduce the number of blocks for
5293 // situations like this:
5295 // int j = 1; int k = j + 1;
5297 if (current_block.Used)
5298 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
5300 implicit_block = current_block;
5302 foreach (VariableDeclaration decl in variable_declarators){
5304 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
5305 if (decl.expression_or_array_initializer != null){
5307 inits = new ArrayList (4);
5314 return implicit_block;
5316 foreach (VariableDeclaration decl in inits){
5318 Expression expr = decl.expression_or_array_initializer;
5320 LocalVariableReference var;
5321 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
5323 assign = new Assign (var, expr, decl.Location);
5325 implicit_block.AddStatement (new StatementExpression (assign));
5328 return implicit_block;
5331 Block declare_local_constants (Expression type, ArrayList declarators)
5333 Block implicit_block;
5335 if (current_block.Used)
5336 implicit_block = new Block (current_block, Block.Flags.Implicit);
5338 implicit_block = current_block;
5340 foreach (VariableDeclaration decl in declarators){
5341 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
5344 return implicit_block;
5347 void CheckAttributeTarget (string a, Location l)
5351 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
5355 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
5361 void CheckUnaryOperator (Operator.OpType op, Location l)
5365 case Operator.OpType.LogicalNot:
5366 case Operator.OpType.OnesComplement:
5367 case Operator.OpType.Increment:
5368 case Operator.OpType.Decrement:
5369 case Operator.OpType.True:
5370 case Operator.OpType.False:
5371 case Operator.OpType.Addition:
5372 case Operator.OpType.Subtraction:
5377 Report.Error (1019, l, "Overloadable unary operator expected");
5383 void CheckBinaryOperator (Operator.OpType op, Location l)
5387 case Operator.OpType.Addition:
5388 case Operator.OpType.Subtraction:
5389 case Operator.OpType.Multiply:
5390 case Operator.OpType.Division:
5391 case Operator.OpType.Modulus:
5392 case Operator.OpType.BitwiseAnd:
5393 case Operator.OpType.BitwiseOr:
5394 case Operator.OpType.ExclusiveOr:
5395 case Operator.OpType.LeftShift:
5396 case Operator.OpType.RightShift:
5397 case Operator.OpType.Equality:
5398 case Operator.OpType.Inequality:
5399 case Operator.OpType.GreaterThan:
5400 case Operator.OpType.LessThan:
5401 case Operator.OpType.GreaterThanOrEqual:
5402 case Operator.OpType.LessThanOrEqual:
5406 Report.Error (1020, l, "Overloadable binary operator expected");
5412 void syntax_error (Location l, string msg)
5414 Report.Error (1003, l, "Syntax error, " + msg);
5417 void note (string s)
5419 // Used to put annotations
5424 public Tokenizer Lexer {
5430 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
5432 this.name = file.Name;
5434 current_namespace = new NamespaceEntry (null, file, null);
5435 current_class = current_namespace.SlaveDeclSpace;
5436 current_container = current_class.PartialContainer; // == RootContest.ToplevelTypes
5438 lexer = new Tokenizer (reader, file, defines);
5441 public void parse ()
5443 int errors = Report.Errors;
5445 if (yacc_verbose_flag > 1)
5446 yyparse (lexer, new yydebug.yyDebugSimple ());
5449 Tokenizer tokenizer = lexer as Tokenizer;
5450 tokenizer.cleanup ();
5451 } catch (Exception e){
5453 // Removed for production use, use parser verbose to get the output.
5455 // Console.WriteLine (e);
5456 if (Report.Errors == errors)
5457 Report.Error (-25, lexer.Location, "Parsing error");
5458 if (yacc_verbose_flag > 0)
5459 Console.WriteLine (e);
5462 if (RootContext.ToplevelTypes.NamespaceEntry != null)
5463 throw new InternalErrorException ("who set it?");
5466 static void CheckToken (int error, int yyToken, string msg, Location loc)
5468 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
5469 Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ());
5471 Report.Error (error, loc, msg);
5474 void CheckIdentifierToken (int yyToken, Location loc)
5476 CheckToken (1041, yyToken, "Identifier expected", loc);
5479 string ConsumeStoredComment ()
5481 string s = tmpComment;
5483 Lexer.doc_state = XmlCommentState.Allowed;
5487 Location GetLocation (object obj)
5489 if (obj is MemberCore)
5490 return ((MemberCore) obj).Location;
5491 if (obj is MemberName)
5492 return ((MemberName) obj).Location;
5493 if (obj is LocatedToken)
5494 return ((LocatedToken) obj).Location;
5495 if (obj is Location)
5496 return (Location) obj;
5497 return lexer.Location;