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 AnonymousMethodExpression current_anonymous_method;
51 /// This is used by the unary_expression code to resolve
52 /// a name against a parameter.
54 Parameters current_local_parameters;
57 /// Using during property parsing to describe the implicit
58 /// value parameter that is passed to the "set" and "get"accesor
59 /// methods (properties and indexers).
61 Expression implicit_value_parameter_type;
62 Parameters indexer_parameters;
65 /// Hack to help create non-typed array initializer
67 public static Expression current_array_type;
68 Expression pushed_current_array_type;
71 /// Used to determine if we are parsing the get/set pair
72 /// of an indexer or a property
76 bool parsing_anonymous_method;
79 /// An out-of-band stack.
88 static public int yacc_verbose_flag;
90 // Name of the file we are parsing
99 /// Temporary Xml documentation cache.
100 /// For enum types, we need one more temporary store.
103 string enumTypeComment;
105 /// Current attribute target
106 string current_attr_target;
108 /// assembly and module attribute definitions are enabled
109 bool global_attrs_enabled = true;
110 bool has_get, has_set;
115 %token NONE /* This token is never returned by our lexer */
116 %token ERROR // This is used not by the parser, but by the tokenizer.
120 *These are the C# keywords
206 /* C# keywords which are not really keywords */
212 /* C# single character operators/punctuation. */
213 %token OPEN_BRACE "{"
214 %token CLOSE_BRACE "}"
215 %token OPEN_BRACKET "["
216 %token CLOSE_BRACKET "]"
217 %token OPEN_PARENS "("
218 %token CLOSE_PARENS ")"
231 %token BITWISE_AND "&"
232 %token BITWISE_OR "|"
239 /* C# multi-character operators. */
240 %token DOUBLE_COLON "::"
243 %token OP_SHIFT_LEFT "<<"
244 %token OP_SHIFT_RIGHT ">>"
251 %token OP_MULT_ASSIGN "*="
252 %token OP_DIV_ASSIGN "/="
253 %token OP_MOD_ASSIGN "%="
254 %token OP_ADD_ASSIGN "+="
255 %token OP_SUB_ASSIGN "-="
256 %token OP_SHIFT_LEFT_ASSIGN "<<="
257 %token OP_SHIFT_RIGHT_ASSIGN ">>="
258 %token OP_AND_ASSIGN "&="
259 %token OP_XOR_ASSIGN "^="
260 %token OP_OR_ASSIGN "|="
264 %token LITERAL_INTEGER "int literal"
265 %token LITERAL_FLOAT "float literal"
266 %token LITERAL_DOUBLE "double literal"
267 %token LITERAL_DECIMAL "decimal literal"
268 %token LITERAL_CHARACTER "character literal"
269 %token LITERAL_STRING "string literal"
272 %token CLOSE_PARENS_CAST
273 %token CLOSE_PARENS_NO_CAST
274 %token CLOSE_PARENS_OPEN_PARENS
275 %token CLOSE_PARENS_MINUS
277 /* Add precedence rules to solve dangling else s/r conflict */
286 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
288 %left STAR DIV PERCENT
289 %right BANG CARRET UMINUS
290 %nonassoc OP_INC OP_DEC
292 %left OPEN_BRACKET OPEN_BRACE
296 %start compilation_unit
300 : outer_declarations opt_EOF
301 | outer_declarations global_attributes opt_EOF
302 | global_attributes opt_EOF
303 | opt_EOF /* allow empty files */
309 Lexer.check_incorrect_doc_comment ();
313 Lexer.check_incorrect_doc_comment ();
319 | outer_declarations outer_declaration
323 : extern_alias_directive
325 | namespace_member_declaration
328 extern_alias_directives
329 : extern_alias_directive
330 | extern_alias_directives extern_alias_directive;
332 extern_alias_directive
333 : EXTERN IDENTIFIER IDENTIFIER SEMICOLON
335 LocatedToken lt = (LocatedToken) $2;
338 Report.Error (1003, lt.Location, "'alias' expected");
339 } else if (RootContext.Version == LanguageVersion.ISO_1) {
340 Report.FeatureIsNotStandardized (lt.Location, "external alias");
342 lt = (LocatedToken) $3;
343 current_namespace.UsingExternalAlias (lt.Value, lt.Location);
350 | using_directives using_directive
354 : using_alias_directive
356 if (RootContext.Documentation != null)
357 Lexer.doc_state = XmlCommentState.Allowed;
359 | using_namespace_directive
361 if (RootContext.Documentation != null)
362 Lexer.doc_state = XmlCommentState.Allowed;
366 using_alias_directive
367 : USING IDENTIFIER ASSIGN
368 namespace_or_type_name SEMICOLON
370 LocatedToken lt = (LocatedToken) $2;
371 current_namespace.UsingAlias (lt.Value, (MemberName) $4, (Location) $1);
374 CheckIdentifierToken (yyToken, GetLocation ($2));
378 using_namespace_directive
379 : USING namespace_name SEMICOLON
381 current_namespace.Using ((MemberName) $2, (Location) $1);
386 // Strictly speaking, namespaces don't have attributes but
387 // we parse global attributes along with namespace declarations and then
390 namespace_declaration
391 : opt_attributes NAMESPACE namespace_or_type_name
393 MemberName name = (MemberName) $3;
396 Report.Error(1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
399 current_namespace = new NamespaceEntry (
400 current_namespace, file, name.GetName ());
401 current_class = current_namespace.SlaveDeclSpace;
402 current_container = current_class.PartialContainer;
404 namespace_body opt_semicolon
406 current_namespace = current_namespace.Parent;
407 current_class = current_namespace.SlaveDeclSpace;
408 current_container = current_class.PartialContainer;
423 : namespace_or_type_name
429 if (RootContext.Documentation != null)
430 Lexer.doc_state = XmlCommentState.Allowed;
432 opt_extern_alias_directives
434 opt_namespace_member_declarations
443 opt_extern_alias_directives
445 | extern_alias_directives
448 opt_namespace_member_declarations
450 | namespace_member_declarations
453 namespace_member_declarations
454 : namespace_member_declaration
455 | namespace_member_declarations namespace_member_declaration
458 namespace_member_declaration
462 DeclSpace ds = (DeclSpace)$1;
464 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
465 Report.Error (1527, ds.Location,
466 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
469 current_namespace.DeclarationFound = true;
471 | namespace_declaration {
472 current_namespace.DeclarationFound = true;
475 | field_declaration {
476 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
478 | method_declaration {
479 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
486 | interface_declaration
488 | delegate_declaration
490 // Enable this when we have handled all errors, because this acts as a generic fallback
493 // Console.WriteLine ("Token=" + yyToken);
494 // Report.Error (1518, GetLocation ($1), "Expected class, struct, interface, enum or delegate");
506 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
514 global_attrs_enabled = false;
519 global_attrs_enabled = false;
528 ArrayList sect = (ArrayList) $1;
530 if (global_attrs_enabled) {
531 if (current_attr_target == "module") {
532 CodeGen.Module.AddAttributes (sect);
534 } else if (current_attr_target != null && current_attr_target.Length > 0) {
535 CodeGen.Assembly.AddAttributes (sect);
538 $$ = new Attributes (sect);
541 if (RootContext.Documentation != null) {
542 Lexer.check_incorrect_doc_comment ();
544 XmlCommentState.Allowed;
548 $$ = new Attributes (sect);
550 current_attr_target = null;
552 | attribute_sections attribute_section
554 Attributes attrs = $1 as Attributes;
555 ArrayList sect = (ArrayList) $2;
557 if (global_attrs_enabled) {
558 if (current_attr_target == "module") {
559 CodeGen.Module.AddAttributes (sect);
561 } else if (current_attr_target == "assembly") {
562 CodeGen.Assembly.AddAttributes (sect);
566 attrs = new Attributes (sect);
568 attrs.AddAttributes (sect);
572 attrs = new Attributes (sect);
574 attrs.AddAttributes (sect);
577 current_attr_target = null;
582 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
586 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
592 attribute_target_specifier
593 : attribute_target COLON
595 current_attr_target = (string)$1;
603 LocatedToken lt = (LocatedToken) $1;
604 CheckAttributeTarget (lt.Value, lt.Location);
605 $$ = lt.Value; // Location won't be required anymore.
607 | EVENT { $$ = "event"; }
608 | RETURN { $$ = "return"; }
614 ArrayList attrs = new ArrayList (4);
620 | attribute_list COMMA attribute
622 ArrayList attrs = (ArrayList) $1;
630 : attribute_name opt_attribute_arguments
632 MemberName mname = (MemberName) $1;
633 object[] arguments = (object[]) $2;
634 MemberName left = mname.Left;
635 string identifier = mname.Name;
637 Expression left_expr = left == null ? null : left.GetTypeExpression ();
639 if (current_attr_target == "assembly" || current_attr_target == "module")
640 // FIXME: supply "nameEscaped" parameter here.
641 $$ = new GlobalAttribute (current_namespace, current_attr_target,
642 left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
644 $$ = new Attribute (current_attr_target, left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
649 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
652 opt_attribute_arguments
653 : /* empty */ { $$ = null; }
654 | OPEN_PARENS attribute_arguments CLOSE_PARENS
662 : opt_positional_argument_list
667 $$ = new object [] { $1, null };
670 | positional_argument_list COMMA named_argument_list
672 $$ = new object[] { $1, $3 };
674 | named_argument_list
676 $$ = new object [] { null, $1 };
681 opt_positional_argument_list
682 : /* empty */ { $$ = null; }
683 | positional_argument_list
686 positional_argument_list
689 ArrayList args = new ArrayList (4);
690 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
694 | positional_argument_list COMMA expression
696 ArrayList args = (ArrayList) $1;
697 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
706 ArrayList args = new ArrayList (4);
711 | named_argument_list COMMA named_argument
713 ArrayList args = (ArrayList) $1;
718 | named_argument_list COMMA expression
720 Report.Error (1016, ((Expression) $3).Location, "Named attribute argument expected");
726 : IDENTIFIER ASSIGN expression
728 // FIXME: keep location
729 $$ = new DictionaryEntry (
730 ((LocatedToken) $1).Value,
731 new Argument ((Expression) $3, Argument.AType.Expression));
737 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
740 opt_class_member_declarations
742 | class_member_declarations
745 class_member_declarations
746 : class_member_declaration
747 | class_member_declarations
748 class_member_declaration
751 class_member_declaration
752 : constant_declaration // done
753 | field_declaration // done
754 | method_declaration // done
755 | property_declaration // done
756 | event_declaration // done
757 | indexer_declaration // done
758 | operator_declaration // done
759 | constructor_declaration // done
760 | destructor_declaration // done
770 MemberName name = MakeName ((MemberName) $5);
771 push_current_class (new Struct (
772 current_namespace, current_class, name, (int) $2,
773 (Attributes) $1), false, $3);
779 current_container.AddBasesForPart (current_class, (ArrayList) $7);
781 if (RootContext.Documentation != null)
782 current_container.DocComment = Lexer.consume_doc_comment ();
786 if (RootContext.Documentation != null)
787 Lexer.doc_state = XmlCommentState.Allowed;
791 $$ = pop_current_class ();
793 | opt_attributes opt_modifiers opt_partial STRUCT error {
794 CheckIdentifierToken (yyToken, GetLocation ($5));
801 if (RootContext.Documentation != null)
802 Lexer.doc_state = XmlCommentState.Allowed;
804 opt_struct_member_declarations CLOSE_BRACE
807 opt_struct_member_declarations
809 | struct_member_declarations
812 struct_member_declarations
813 : struct_member_declaration
814 | struct_member_declarations struct_member_declaration
817 struct_member_declaration
818 : constant_declaration
821 | property_declaration
823 | indexer_declaration
824 | operator_declaration
825 | constructor_declaration
829 * This is only included so we can flag error 575:
830 * destructors only allowed on class types
832 | destructor_declaration
843 int modflags = (int) $2;
844 foreach (VariableDeclaration constant in (ArrayList) $5){
845 Location l = constant.Location;
846 if ((modflags & Modifiers.STATIC) != 0) {
847 Report.Error (504, l, "The constant `{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
851 Const c = new Const (
852 current_class, (Expression) $4, (string) constant.identifier,
853 (Expression) constant.expression_or_array_initializer, modflags,
856 if (RootContext.Documentation != null) {
857 c.DocComment = Lexer.consume_doc_comment ();
858 Lexer.doc_state = XmlCommentState.Allowed;
860 current_container.AddConstant (c);
866 : constant_declarator
868 ArrayList constants = new ArrayList (4);
873 | constant_declarators COMMA constant_declarator
876 ArrayList constants = (ArrayList) $1;
883 : IDENTIFIER ASSIGN constant_expression
885 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
889 // A const field requires a value to be provided
890 Report.Error (145, ((LocatedToken) $1).Location, "A const field requires a value to be provided");
902 Expression type = (Expression) $3;
905 current_array_type = null;
907 foreach (VariableDeclaration var in (ArrayList) $4){
908 Field field = new Field (current_class, type, mod, var.identifier,
909 (Attributes) $1, var.Location);
911 field.Initializer = var.expression_or_array_initializer;
913 if (RootContext.Documentation != null) {
914 field.DocComment = Lexer.consume_doc_comment ();
915 Lexer.doc_state = XmlCommentState.Allowed;
917 current_container.AddField (field);
918 $$ = field; // FIXME: might be better if it points to the top item
925 fixed_variable_declarators
928 Expression type = (Expression) $4;
931 current_array_type = null;
933 foreach (VariableDeclaration var in (ArrayList) $5) {
934 FixedField field = new FixedField (current_class, type, mod, var.identifier,
935 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
937 if (RootContext.Documentation != null) {
938 field.DocComment = Lexer.consume_doc_comment ();
939 Lexer.doc_state = XmlCommentState.Allowed;
941 current_container.AddField (field);
942 $$ = field; // FIXME: might be better if it points to the top item
950 current_array_type = null;
951 Report.Error (670, (Location) $3, "Fields cannot have void type");
955 fixed_variable_declarators
956 : fixed_variable_declarator
958 ArrayList decl = new ArrayList (2);
962 | fixed_variable_declarators COMMA fixed_variable_declarator
964 ArrayList decls = (ArrayList) $1;
970 fixed_variable_declarator
971 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
973 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
975 | IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
977 Report.Error (443, lexer.Location, "Value or constant expected");
978 $$ = new VariableDeclaration ((LocatedToken) $1, null);
983 : variable_declarator
985 ArrayList decl = new ArrayList (4);
990 | variable_declarators COMMA variable_declarator
992 ArrayList decls = (ArrayList) $1;
999 : IDENTIFIER ASSIGN variable_initializer
1001 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1005 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1007 | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
1009 Report.Error (650, ((LocatedToken) $1).Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
1010 "To declare a fixed size buffer field, use the fixed keyword before the field type");
1015 variable_initializer
1024 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
1026 $$ = new StackAlloc ((Expression) $2, (Expression) $4, (Location) $1);
1030 $$ = new ArglistAccess ((Location) $1);
1034 Report.Error (1575, (Location) $1, "A stackalloc expression requires [] after type");
1041 anonymous_host = (IAnonymousHost) $1;
1042 if (RootContext.Documentation != null)
1043 Lexer.doc_state = XmlCommentState.NotAllowed;
1047 Method method = (Method) $1;
1048 method.Block = (ToplevelBlock) $3;
1049 current_container.AddMethod (method);
1051 anonymous_host = null;
1052 current_local_parameters = null;
1054 if (RootContext.Documentation != null)
1055 Lexer.doc_state = XmlCommentState.Allowed;
1068 Report.Error (1585, lexer.Location,
1069 "Member modifier `{0}' must precede the member type and name",
1070 Modifiers.Name (i));
1081 type namespace_or_type_name
1082 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1084 MemberName name = (MemberName) $4;
1086 Method method = new Method (current_class, null, (Expression) $3, (int) $2,
1087 false, name, (Parameters) $6, (Attributes) $1);
1089 anonymous_host = method;
1090 current_local_parameters = (Parameters) $6;
1092 if (RootContext.Documentation != null)
1093 method.DocComment = Lexer.consume_doc_comment ();
1099 VOID namespace_or_type_name
1100 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1102 MemberName name = (MemberName) $4;
1104 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1105 (int) $2, false, name, (Parameters) $6,
1108 anonymous_host = method;
1109 current_local_parameters = (Parameters) $6;
1111 if (RootContext.Documentation != null)
1112 method.DocComment = Lexer.consume_doc_comment ();
1119 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1121 MemberName name = (MemberName) $5;
1122 Report.Error (1585, name.Location,
1123 "Member modifier `{0}' must precede the member type and name", Modifiers.Name ((int) $4));
1125 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1126 0, false, name, (Parameters) $7, (Attributes) $1);
1128 current_local_parameters = (Parameters) $7;
1130 if (RootContext.Documentation != null)
1131 method.DocComment = Lexer.consume_doc_comment ();
1139 | SEMICOLON { $$ = null; }
1142 opt_formal_parameter_list
1143 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1144 | formal_parameter_list
1147 formal_parameter_list
1150 ArrayList pars_list = (ArrayList) $1;
1152 Parameter [] pars = new Parameter [pars_list.Count];
1153 pars_list.CopyTo (pars);
1155 $$ = new Parameters (pars);
1157 | fixed_parameters COMMA parameter_array
1159 ArrayList pars_list = (ArrayList) $1;
1162 Parameter [] pars = new Parameter [pars_list.Count];
1163 pars_list.CopyTo (pars);
1165 $$ = new Parameters (pars);
1167 | fixed_parameters COMMA ARGLIST
1169 ArrayList pars_list = (ArrayList) $1;
1170 //pars_list.Add (new ArglistParameter (GetLocation ($3)));
1172 Parameter [] pars = new Parameter [pars_list.Count];
1173 pars_list.CopyTo (pars);
1175 $$ = new Parameters (pars, true);
1177 | parameter_array COMMA error
1180 Report.Error (231, ((Parameter) $1).Location, "A params parameter must be the last parameter in a formal parameter list");
1183 | fixed_parameters COMMA parameter_array COMMA error
1186 Report.Error (231, ((Parameter) $3).Location, "A params parameter must be the last parameter in a formal parameter list");
1189 | ARGLIST COMMA error
1191 Report.Error (257, (Location) $1, "An __arglist parameter must be the last parameter in a formal parameter list");
1194 | fixed_parameters COMMA ARGLIST COMMA error
1196 Report.Error (257, (Location) $3, "An __arglist parameter must be the last parameter in a formal parameter list");
1201 $$ = new Parameters (new Parameter[] { (Parameter) $1 } );
1205 $$ = new Parameters (new Parameter[0], true);
1212 ArrayList pars = new ArrayList (4);
1217 | fixed_parameters COMMA fixed_parameter
1219 ArrayList pars = (ArrayList) $1;
1228 opt_parameter_modifier
1232 LocatedToken lt = (LocatedToken) $4;
1233 $$ = new Parameter ((Expression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1236 opt_parameter_modifier
1238 IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1240 LocatedToken lt = (LocatedToken) $4;
1241 Report.Error (1552, lt.Location, "Array type specifier, [], must appear before parameter name");
1245 opt_parameter_modifier
1248 Report.Error (1001, GetLocation ($3), "Identifier expected");
1252 opt_parameter_modifier
1255 CheckIdentifierToken (yyToken, GetLocation ($4));
1259 opt_parameter_modifier
1265 LocatedToken lt = (LocatedToken) $4;
1266 Report.Error (241, lt.Location, "Default parameter specifiers are not permitted");
1271 opt_parameter_modifier
1272 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1273 | parameter_modifier
1277 : REF { $$ = Parameter.Modifier.REF; }
1278 | OUT { $$ = Parameter.Modifier.OUT; }
1282 : opt_attributes PARAMS type IDENTIFIER
1284 LocatedToken lt = (LocatedToken) $4;
1285 $$ = new ParamsParameter ((Expression) $3, lt.Value, (Attributes) $1, lt.Location);
1286 note ("type must be a single-dimension array type");
1288 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1290 Report.Error (1611, (Location) $2, "The params parameter cannot be declared as ref or out");
1293 | opt_attributes PARAMS type error {
1294 CheckIdentifierToken (yyToken, GetLocation ($4));
1299 property_declaration
1303 namespace_or_type_name
1305 if (RootContext.Documentation != null)
1306 tmpComment = Lexer.consume_doc_comment ();
1310 implicit_value_parameter_type = (Expression) $3;
1312 lexer.PropertyParsing = true;
1314 accessor_declarations
1316 lexer.PropertyParsing = false;
1317 has_get = has_set = false;
1325 Pair pair = (Pair) $8;
1326 Accessor get_block = (Accessor) pair.First;
1327 Accessor set_block = (Accessor) pair.Second;
1329 MemberName name = (MemberName) $4;
1331 prop = new Property (current_class, (Expression) $3, (int) $2,
1332 false, name, (Attributes) $1, get_block, set_block);
1334 current_container.AddProperty (prop);
1335 implicit_value_parameter_type = null;
1337 if (RootContext.Documentation != null)
1338 prop.DocComment = ConsumeStoredComment ();
1343 accessor_declarations
1344 : get_accessor_declaration
1346 $$ = new Pair ($1, null);
1348 | get_accessor_declaration accessor_declarations
1350 Pair pair = (Pair) $2;
1354 | set_accessor_declaration
1356 $$ = new Pair (null, $1);
1358 | set_accessor_declaration accessor_declarations
1360 Pair pair = (Pair) $2;
1366 Report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1371 get_accessor_declaration
1372 : opt_attributes opt_modifiers GET
1374 // If this is not the case, then current_local_parameters has already
1375 // been set in indexer_declaration
1376 if (parsing_indexer == false)
1377 current_local_parameters = null;
1379 current_local_parameters = indexer_parameters;
1380 lexer.PropertyParsing = false;
1382 anonymous_host = SimpleAnonymousHost.GetSimple ();
1387 Report.Error (1007, (Location) $3, "Property accessor already defined");
1390 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1392 current_local_parameters = null;
1393 lexer.PropertyParsing = true;
1395 SimpleAnonymousHost.Simple.Propagate (accessor);
1396 anonymous_host = null;
1398 if (RootContext.Documentation != null)
1399 if (Lexer.doc_state == XmlCommentState.Error)
1400 Lexer.doc_state = XmlCommentState.NotAllowed;
1406 set_accessor_declaration
1407 : opt_attributes opt_modifiers SET
1410 Parameter implicit_value_parameter = new Parameter (
1411 implicit_value_parameter_type, "value",
1412 Parameter.Modifier.NONE, null, (Location) $3);
1414 if (parsing_indexer == false) {
1415 args = new Parameter [1];
1416 args [0] = implicit_value_parameter;
1417 current_local_parameters = new Parameters (args);
1419 Parameter [] fpars = indexer_parameters.FixedParameters;
1422 int count = fpars.Length;
1424 args = new Parameter [count + 1];
1425 fpars.CopyTo (args, 0);
1426 args [count] = implicit_value_parameter;
1429 current_local_parameters = new Parameters (
1433 lexer.PropertyParsing = false;
1435 anonymous_host = SimpleAnonymousHost.GetSimple ();
1440 Report.Error (1007, ((LocatedToken) $3).Location, "Property accessor already defined");
1443 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1445 current_local_parameters = null;
1446 lexer.PropertyParsing = true;
1448 SimpleAnonymousHost.Simple.Propagate (accessor);
1449 anonymous_host = null;
1451 if (RootContext.Documentation != null
1452 && Lexer.doc_state == XmlCommentState.Error)
1453 Lexer.doc_state = XmlCommentState.NotAllowed;
1461 | SEMICOLON { $$ = null; }
1464 interface_declaration
1468 INTERFACE member_name
1470 MemberName name = MakeName ((MemberName) $5);
1472 push_current_class (new Interface (
1473 current_namespace, current_class, name, (int) $2,
1474 (Attributes) $1), true, $3);
1478 current_container.AddBasesForPart (current_class, (ArrayList) $7);
1480 if (RootContext.Documentation != null) {
1481 current_container.DocComment = Lexer.consume_doc_comment ();
1482 Lexer.doc_state = XmlCommentState.Allowed;
1487 if (RootContext.Documentation != null)
1488 Lexer.doc_state = XmlCommentState.Allowed;
1492 $$ = pop_current_class ();
1494 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1495 CheckIdentifierToken (yyToken, GetLocation ($5));
1501 opt_interface_member_declarations
1505 opt_interface_member_declarations
1507 | interface_member_declarations
1510 interface_member_declarations
1511 : interface_member_declaration
1512 | interface_member_declarations interface_member_declaration
1515 interface_member_declaration
1516 : interface_method_declaration
1521 Method m = (Method) $1;
1523 if (m.IsExplicitImpl)
1524 Report.Error (541, m.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1525 m.GetSignatureForError ());
1527 current_container.AddMethod (m);
1529 if (RootContext.Documentation != null)
1530 Lexer.doc_state = XmlCommentState.Allowed;
1532 | interface_property_declaration
1537 Property p = (Property) $1;
1539 if (p.IsExplicitImpl)
1540 Report.Error (541, p.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1541 p.GetSignatureForError ());
1543 current_container.AddProperty (p);
1545 if (RootContext.Documentation != null)
1546 Lexer.doc_state = XmlCommentState.Allowed;
1548 | interface_event_declaration
1551 Event e = (Event) $1;
1553 if (e.IsExplicitImpl)
1554 Report.Error (541, e.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1555 e.GetSignatureForError ());
1557 current_container.AddEvent (e);
1560 if (RootContext.Documentation != null)
1561 Lexer.doc_state = XmlCommentState.Allowed;
1563 | interface_indexer_declaration
1568 Indexer i = (Indexer) $1;
1570 if (i.IsExplicitImpl)
1571 Report.Error (541, i.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1572 i.GetSignatureForError ());
1574 current_container.AddIndexer (i);
1576 if (RootContext.Documentation != null)
1577 Lexer.doc_state = XmlCommentState.Allowed;
1579 | delegate_declaration
1582 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1583 ((MemberCore)$1).GetSignatureForError ());
1589 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1590 ((MemberCore)$1).GetSignatureForError ());
1593 | struct_declaration
1596 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1597 ((MemberCore)$1).GetSignatureForError ());
1603 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1604 ((MemberCore)$1).GetSignatureForError ());
1607 | interface_declaration
1610 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1611 ((MemberCore)$1).GetSignatureForError ());
1614 | constant_declaration
1616 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1624 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, GetLocation ($1));
1629 interface_method_declaration
1630 : opt_attributes opt_new type namespace_or_type_name
1631 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1634 $$ = new Method (current_class, null, (Expression) $3, (int) $2, true,
1635 (MemberName) $4, (Parameters) $6, (Attributes) $1);
1636 if (RootContext.Documentation != null)
1637 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1639 | opt_attributes opt_new type namespace_or_type_name
1640 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1641 OPEN_BRACE opt_statement_list CLOSE_BRACE
1643 $$ = new Method (current_class, null, (Expression) $3, (int) $2, true,
1644 (MemberName) $4, (Parameters) $6, (Attributes) $1);
1646 Report.Error (531, lexer.Location, "`{0}': interface members cannot have a definition",
1647 ((Method)$$).GetSignatureForError ());
1651 interface_property_declaration
1656 { lexer.PropertyParsing = true; }
1657 accessor_declarations
1659 has_get = has_set = false;
1660 lexer.PropertyParsing = false;
1664 LocatedToken lt = (LocatedToken) $4;
1665 MemberName name = new MemberName (lt.Value, lt.Location);
1667 if ($3 == TypeManager.system_void_expr) {
1668 Report.Error (547, lt.Location, "`{0}': property or indexer cannot have void type", lt.Value);
1674 p = new Property (current_class, (Expression) $3, (int) $2, true,
1675 name, (Attributes) $1,
1678 Report.Error (548, p.Location, "`{0}': property or indexer must have at least one accessor", p.GetSignatureForError ());
1682 Pair pair = (Pair) $7;
1683 p = new Property (current_class, (Expression) $3, (int) $2, true,
1684 name, (Attributes) $1,
1685 (Accessor)pair.First, (Accessor)pair.Second);
1687 if (pair.First != null && ((Accessor)(pair.First)).Block != null) {
1688 Report.Error (531, p.Location, "`{0}.get': interface members cannot have a definition", p.GetSignatureForError ());
1693 if (pair.Second != null && ((Accessor)(pair.Second)).Block != null) {
1694 Report.Error (531, p.Location, "`{0}.set': interface members cannot have a definition", p.GetSignatureForError ());
1699 if (RootContext.Documentation != null)
1700 p.DocComment = Lexer.consume_doc_comment ();
1707 CheckIdentifierToken (yyToken, GetLocation ($4));
1713 interface_event_declaration
1714 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1716 LocatedToken lt = (LocatedToken) $5;
1717 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1718 new MemberName (lt.Value, lt.Location),
1720 if (RootContext.Documentation != null)
1721 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1723 | opt_attributes opt_new EVENT type error {
1724 CheckIdentifierToken (yyToken, GetLocation ($5));
1727 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1728 LocatedToken lt = (LocatedToken) $5;
1729 Report.Error (68, lt.Location, "`{0}.{1}': event in interface cannot have initializer", current_container.Name, lt.Value);
1732 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1734 lexer.EventParsing = true;
1736 event_accessor_declarations
1738 lexer.EventParsing = false;
1741 Report.Error (69, (Location) $3, "Event in interface cannot have add or remove accessors");
1746 interface_indexer_declaration
1747 : opt_attributes opt_new type THIS
1748 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1750 { lexer.PropertyParsing = true; }
1751 accessor_declarations
1753 has_get = has_set = false;
1754 lexer.PropertyParsing = false;
1760 i = new Indexer (current_class, (Expression) $3,
1761 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1762 (int) $2, true, (Parameters) $6, (Attributes) $1,
1765 Report.Error (548, i.Location, "`{0}': property or indexer must have at least one accessor", i.GetSignatureForError ());
1769 Pair pair = (Pair) $10;
1770 i = new Indexer (current_class, (Expression) $3,
1771 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1772 (int) $2, true, (Parameters) $6, (Attributes) $1,
1773 (Accessor)pair.First, (Accessor)pair.Second);
1775 if (pair.First != null && ((Accessor)(pair.First)).Block != null) {
1776 Report.Error (531, i.Location, "`{0}.get': interface members cannot have a definition", i.GetSignatureForError ());
1781 if (pair.Second != null && ((Accessor)(pair.Second)).Block != null) {
1782 Report.Error (531, i.Location, "`{0}.set': interface members cannot have a definition", i.GetSignatureForError ());
1787 if (RootContext.Documentation != null)
1788 i.DocComment = ConsumeStoredComment ();
1794 operator_declaration
1795 : opt_attributes opt_modifiers operator_declarator
1797 anonymous_host = SimpleAnonymousHost.GetSimple ();
1804 OperatorDeclaration decl = (OperatorDeclaration) $3;
1806 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1808 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null, decl.location);
1809 if (decl.arg2type != null)
1810 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null, decl.location);
1812 Operator op = new Operator (
1813 current_class, decl.optype, decl.ret_type, (int) $2,
1814 new Parameters (param_list),
1815 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1817 if (RootContext.Documentation != null) {
1818 op.DocComment = tmpComment;
1819 Lexer.doc_state = XmlCommentState.Allowed;
1822 SimpleAnonymousHost.Simple.Propagate (op);
1823 anonymous_host = null;
1825 // Note again, checking is done in semantic analysis
1826 current_container.AddOperator (op);
1828 current_local_parameters = null;
1834 | SEMICOLON { $$ = null; }
1837 : type OPERATOR overloadable_operator
1838 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1840 LocatedToken lt = (LocatedToken) $6;
1841 Operator.OpType op = (Operator.OpType) $3;
1842 CheckUnaryOperator (op, lt.Location);
1844 if (op == Operator.OpType.Addition)
1845 op = Operator.OpType.UnaryPlus;
1847 if (op == Operator.OpType.Subtraction)
1848 op = Operator.OpType.UnaryNegation;
1850 Parameter [] pars = new Parameter [1];
1851 Expression type = (Expression) $5;
1853 pars [0] = new Parameter (type, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1855 current_local_parameters = new Parameters (pars);
1857 if (RootContext.Documentation != null) {
1858 tmpComment = Lexer.consume_doc_comment ();
1859 Lexer.doc_state = XmlCommentState.NotAllowed;
1862 $$ = new OperatorDeclaration (op, (Expression) $1, type, lt.Value,
1863 null, null, (Location) $2);
1865 | type OPERATOR overloadable_operator
1867 type IDENTIFIER COMMA
1871 LocatedToken ltParam1 = (LocatedToken) $6;
1872 LocatedToken ltParam2 = (LocatedToken) $9;
1873 CheckBinaryOperator ((Operator.OpType) $3, (Location) $2);
1875 Parameter [] pars = new Parameter [2];
1877 Expression typeL = (Expression) $5;
1878 Expression typeR = (Expression) $8;
1880 pars [0] = new Parameter (typeL, ltParam1.Value, Parameter.Modifier.NONE, null, ltParam1.Location);
1881 pars [1] = new Parameter (typeR, ltParam2.Value, Parameter.Modifier.NONE, null, ltParam2.Location);
1883 current_local_parameters = new Parameters (pars);
1885 if (RootContext.Documentation != null) {
1886 tmpComment = Lexer.consume_doc_comment ();
1887 Lexer.doc_state = XmlCommentState.NotAllowed;
1890 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1891 typeL, ltParam1.Value,
1892 typeR, ltParam2.Value, (Location) $2);
1894 | conversion_operator_declarator
1895 | type OPERATOR overloadable_operator
1897 type IDENTIFIER COMMA
1898 type IDENTIFIER COMMA
1902 Report.Error (1534, (Location) $2, "Overloaded binary operator `{0}' takes two parameters",
1903 Operator.GetName ((Operator.OpType) $3));
1906 | type OPERATOR overloadable_operator
1907 OPEN_PARENS CLOSE_PARENS
1909 Report.Error (1535, (Location) $2, "Overloaded unary operator `{0}' takes one parameter",
1910 Operator.GetName ((Operator.OpType) $3));
1915 overloadable_operator
1917 : BANG { $$ = Operator.OpType.LogicalNot; }
1918 | TILDE { $$ = Operator.OpType.OnesComplement; }
1919 | OP_INC { $$ = Operator.OpType.Increment; }
1920 | OP_DEC { $$ = Operator.OpType.Decrement; }
1921 | TRUE { $$ = Operator.OpType.True; }
1922 | FALSE { $$ = Operator.OpType.False; }
1923 // Unary and binary:
1924 | PLUS { $$ = Operator.OpType.Addition; }
1925 | MINUS { $$ = Operator.OpType.Subtraction; }
1927 | STAR { $$ = Operator.OpType.Multiply; }
1928 | DIV { $$ = Operator.OpType.Division; }
1929 | PERCENT { $$ = Operator.OpType.Modulus; }
1930 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1931 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1932 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1933 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1934 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1935 | OP_EQ { $$ = Operator.OpType.Equality; }
1936 | OP_NE { $$ = Operator.OpType.Inequality; }
1937 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1938 | OP_LT { $$ = Operator.OpType.LessThan; }
1939 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1940 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1943 conversion_operator_declarator
1944 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1946 LocatedToken lt = (LocatedToken) $6;
1947 Parameter [] pars = new Parameter [1];
1949 pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1951 current_local_parameters = new Parameters (pars);
1953 if (RootContext.Documentation != null) {
1954 tmpComment = Lexer.consume_doc_comment ();
1955 Lexer.doc_state = XmlCommentState.NotAllowed;
1958 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, lt.Value,
1959 null, null, (Location) $2);
1961 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1963 LocatedToken lt = (LocatedToken) $6;
1964 Parameter [] pars = new Parameter [1];
1966 pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1968 current_local_parameters = new Parameters (pars);
1970 if (RootContext.Documentation != null) {
1971 tmpComment = Lexer.consume_doc_comment ();
1972 Lexer.doc_state = XmlCommentState.NotAllowed;
1975 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, lt.Value,
1976 null, null, (Location) $2);
1980 syntax_error ((Location) $1, "'operator' expected");
1984 syntax_error ((Location) $1, "'operator' expected");
1988 constructor_declaration
1991 constructor_declarator
1994 Constructor c = (Constructor) $3;
1995 c.Block = (ToplevelBlock) $4;
1996 c.OptAttributes = (Attributes) $1;
1997 c.ModFlags = (int) $2;
1999 if (RootContext.Documentation != null)
2000 c.DocComment = ConsumeStoredComment ();
2002 if (c.Name == current_container.Basename){
2003 if ((c.ModFlags & Modifiers.STATIC) != 0){
2004 if ((c.ModFlags & Modifiers.Accessibility) != 0){
2005 Report.Error (515, c.Location,
2006 "`{0}': access modifiers are not allowed on static constructors",
2007 c.GetSignatureForError ());
2010 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2012 if (c.Initializer != null){
2013 Report.Error (514, c.Location,
2014 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2015 c.GetSignatureForError ());
2018 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2021 // We let another layer check the validity of the constructor.
2022 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
2025 current_container.AddConstructor (c);
2027 current_local_parameters = null;
2028 if (RootContext.Documentation != null)
2029 Lexer.doc_state = XmlCommentState.Allowed;
2033 constructor_declarator
2036 if (RootContext.Documentation != null) {
2037 tmpComment = Lexer.consume_doc_comment ();
2038 Lexer.doc_state = XmlCommentState.Allowed;
2041 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2043 current_local_parameters = (Parameters) $4;
2045 opt_constructor_initializer
2047 LocatedToken lt = (LocatedToken) $1;
2048 $$ = new Constructor (current_class, lt.Value, 0, (Parameters) $4,
2049 (ConstructorInitializer) $7, lt.Location);
2051 anonymous_host = (IAnonymousHost) $$;
2057 | SEMICOLON { $$ = null; }
2060 opt_constructor_initializer
2061 : /* empty */ { $$ = null; }
2062 | constructor_initializer
2065 constructor_initializer
2066 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
2068 $$ = new ConstructorBaseInitializer ((ArrayList) $4, (Location) $2);
2070 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
2072 $$ = new ConstructorThisInitializer ((ArrayList) $4, (Location) $2);
2075 Report.Error (1018, (Location) $1, "Keyword this or base expected");
2081 : /* EMPTY */ { $$ = 0; }
2082 | UNSAFE { $$ = Modifiers.UNSAFE; }
2083 | EXTERN { $$ = Modifiers.EXTERN; }
2086 destructor_declaration
2087 : opt_attributes opt_finalizer TILDE
2089 if (RootContext.Documentation != null) {
2090 tmpComment = Lexer.consume_doc_comment ();
2091 Lexer.doc_state = XmlCommentState.NotAllowed;
2094 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2096 LocatedToken lt = (LocatedToken) $5;
2097 if (lt.Value != current_container.Basename){
2098 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2099 } else if (current_container.Kind != Kind.Class){
2100 Report.Error (575, lt.Location, "Only class types can contain destructor");
2102 Location l = lt.Location;
2105 if (!RootContext.StdLib && current_container.Name == "System.Object")
2106 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2108 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2110 Method d = new Destructor (
2111 current_class, TypeManager.system_void_expr, m, "Finalize",
2112 Parameters.EmptyReadOnlyParameters, (Attributes) $1, l);
2113 if (RootContext.Documentation != null)
2114 d.DocComment = ConsumeStoredComment ();
2116 d.Block = (ToplevelBlock) $8;
2117 current_container.AddMethod (d);
2125 EVENT type variable_declarators SEMICOLON
2127 current_array_type = null;
2128 foreach (VariableDeclaration var in (ArrayList) $5) {
2130 MemberName name = new MemberName (var.identifier,
2133 EventField e = new EventField (
2134 current_class, (Expression) $4, (int) $2, false, name,
2137 e.Initializer = var.expression_or_array_initializer;
2139 current_container.AddEvent (e);
2141 if (RootContext.Documentation != null) {
2142 e.DocComment = Lexer.consume_doc_comment ();
2143 Lexer.doc_state = XmlCommentState.Allowed;
2149 EVENT type namespace_or_type_name
2152 implicit_value_parameter_type = (Expression) $4;
2153 lexer.EventParsing = true;
2155 event_accessor_declarations
2157 lexer.EventParsing = false;
2161 MemberName name = (MemberName) $5;
2164 Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2165 current_container.Name, name.ToString ());
2168 Pair pair = (Pair) $8;
2169 if (pair.First == null || pair.Second == null)
2170 // CS0073 is already reported, so no CS0065 here.
2173 Event e = new EventProperty (
2174 current_class, (Expression) $4, (int) $2, false, name,
2175 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second);
2176 if (RootContext.Documentation != null) {
2177 e.DocComment = Lexer.consume_doc_comment ();
2178 Lexer.doc_state = XmlCommentState.Allowed;
2181 current_container.AddEvent (e);
2182 implicit_value_parameter_type = null;
2186 | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
2187 MemberName mn = (MemberName) $5;
2189 if (mn.Left != null)
2190 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2192 Report.Error (71, mn.Location, "Event declaration should use property syntax");
2194 if (RootContext.Documentation != null)
2195 Lexer.doc_state = XmlCommentState.Allowed;
2199 event_accessor_declarations
2200 : add_accessor_declaration remove_accessor_declaration
2202 $$ = new Pair ($1, $2);
2204 | remove_accessor_declaration add_accessor_declaration
2206 $$ = new Pair ($2, $1);
2208 | add_accessor_declaration { $$ = null; }
2209 | remove_accessor_declaration { $$ = null; }
2212 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2218 add_accessor_declaration
2219 : opt_attributes ADD
2221 Parameter [] args = new Parameter [1];
2222 Parameter implicit_value_parameter = new Parameter (
2223 implicit_value_parameter_type, "value",
2224 Parameter.Modifier.NONE, null, (Location) $2);
2226 args [0] = implicit_value_parameter;
2228 current_local_parameters = new Parameters (args);
2229 lexer.EventParsing = false;
2233 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2234 lexer.EventParsing = true;
2236 | opt_attributes ADD error {
2237 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2240 | opt_attributes modifiers ADD {
2241 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2246 remove_accessor_declaration
2247 : opt_attributes REMOVE
2249 Parameter [] args = new Parameter [1];
2250 Parameter implicit_value_parameter = new Parameter (
2251 implicit_value_parameter_type, "value",
2252 Parameter.Modifier.NONE, null, (Location) $2);
2254 args [0] = implicit_value_parameter;
2256 current_local_parameters = new Parameters (args);
2257 lexer.EventParsing = false;
2261 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2262 lexer.EventParsing = true;
2264 | opt_attributes REMOVE error {
2265 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2268 | opt_attributes modifiers REMOVE {
2269 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2275 : opt_attributes opt_modifiers indexer_declarator
2278 IndexerDeclaration decl = (IndexerDeclaration) $3;
2280 implicit_value_parameter_type = decl.type;
2282 lexer.PropertyParsing = true;
2283 parsing_indexer = true;
2285 indexer_parameters = decl.param_list;
2286 anonymous_host = SimpleAnonymousHost.GetSimple ();
2288 accessor_declarations
2290 lexer.PropertyParsing = false;
2291 has_get = has_set = false;
2292 parsing_indexer = false;
2299 // The signature is computed from the signature of the indexer. Look
2300 // at section 3.6 on the spec
2302 IndexerDeclaration decl = (IndexerDeclaration) $3;
2303 Location loc = decl.location;
2304 Pair pair = (Pair) $6;
2305 Accessor get_block = (Accessor) pair.First;
2306 Accessor set_block = (Accessor) pair.Second;
2309 if (decl.interface_type != null)
2310 name = new MemberName (decl.interface_type, TypeContainer.DefaultIndexerName, loc);
2312 name = new MemberName (TypeContainer.DefaultIndexerName, loc);
2314 indexer = new Indexer (current_class, decl.type, name,
2315 (int) $2, false, decl.param_list, (Attributes) $1,
2316 get_block, set_block);
2318 if (RootContext.Documentation != null)
2319 indexer.DocComment = ConsumeStoredComment ();
2321 current_container.AddIndexer (indexer);
2323 current_local_parameters = null;
2324 implicit_value_parameter_type = null;
2325 indexer_parameters = null;
2330 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2332 Parameters pars = (Parameters) $4;
2333 if (pars.HasArglist) {
2334 // "__arglist is not valid in this context"
2335 Report.Error (1669, (Location) $2, "__arglist is not valid in this context");
2336 } else if (pars.Empty){
2337 Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
2339 if (RootContext.Documentation != null) {
2340 tmpComment = Lexer.consume_doc_comment ();
2341 Lexer.doc_state = XmlCommentState.Allowed;
2344 $$ = new IndexerDeclaration ((Expression) $1, null, pars, (Location) $2);
2346 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2348 Parameters pars = (Parameters) $6;
2350 if (pars.HasArglist) {
2351 // "__arglist is not valid in this context"
2352 Report.Error (1669, (Location) $4, "__arglist is not valid in this context");
2353 } else if (pars.Empty){
2354 Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
2357 MemberName name = (MemberName) $2;
2358 $$ = new IndexerDeclaration ((Expression) $1, name, pars, (Location) $4);
2360 if (RootContext.Documentation != null) {
2361 tmpComment = Lexer.consume_doc_comment ();
2362 Lexer.doc_state = XmlCommentState.Allowed;
2372 if (RootContext.Documentation != null)
2373 enumTypeComment = Lexer.consume_doc_comment ();
2378 LocatedToken lt = (LocatedToken) $4;
2379 Location enum_location = lt.Location;
2381 MemberName name = MakeName (new MemberName (lt.Value, enum_location));
2382 Enum e = new Enum (current_namespace, current_class, (Expression) $5, (int) $2,
2383 name, (Attributes) $1);
2385 if (RootContext.Documentation != null)
2386 e.DocComment = enumTypeComment;
2389 EnumMember em = null;
2390 foreach (VariableDeclaration ev in (ArrayList) $7) {
2391 em = new EnumMember (e, em, (Expression) ev.expression_or_array_initializer,
2392 new MemberName (ev.identifier, ev.Location), ev.OptAttributes);
2394 // if (RootContext.Documentation != null)
2395 em.DocComment = ev.DocComment;
2397 e.AddEnumMember (em);
2400 current_container.AddEnum (e);
2407 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2408 | COLON type { $$ = $2; }
2414 if (RootContext.Documentation != null)
2415 Lexer.doc_state = XmlCommentState.Allowed;
2417 opt_enum_member_declarations
2419 // here will be evaluated after CLOSE_BLACE is consumed.
2420 if (RootContext.Documentation != null)
2421 Lexer.doc_state = XmlCommentState.Allowed;
2429 opt_enum_member_declarations
2430 : /* empty */ { $$ = new ArrayList (4); }
2431 | enum_member_declarations opt_comma { $$ = $1; }
2434 enum_member_declarations
2435 : enum_member_declaration
2437 ArrayList l = new ArrayList (4);
2442 | enum_member_declarations COMMA enum_member_declaration
2444 ArrayList l = (ArrayList) $1;
2452 enum_member_declaration
2453 : opt_attributes IDENTIFIER
2455 VariableDeclaration vd = new VariableDeclaration (
2456 (LocatedToken) $2, null, (Attributes) $1);
2458 if (RootContext.Documentation != null) {
2459 vd.DocComment = Lexer.consume_doc_comment ();
2460 Lexer.doc_state = XmlCommentState.Allowed;
2465 | opt_attributes IDENTIFIER
2467 if (RootContext.Documentation != null) {
2468 tmpComment = Lexer.consume_doc_comment ();
2469 Lexer.doc_state = XmlCommentState.NotAllowed;
2474 VariableDeclaration vd = new VariableDeclaration (
2475 (LocatedToken) $2, $5, (Attributes) $1);
2477 if (RootContext.Documentation != null)
2478 vd.DocComment = ConsumeStoredComment ();
2484 delegate_declaration
2487 DELEGATE type member_name
2488 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2491 MemberName name = MakeName ((MemberName) $5);
2492 Parameters p = (Parameters) $7;
2494 // TODO: wrong location
2495 Report.Error (1669, name.Location, "__arglist is not valid in this context");
2498 Delegate del = new Delegate (current_namespace, current_class, (Expression) $4,
2499 (int) $2, name, p, (Attributes) $1);
2501 if (RootContext.Documentation != null) {
2502 del.DocComment = Lexer.consume_doc_comment ();
2503 Lexer.doc_state = XmlCommentState.Allowed;
2506 current_container.AddDelegate (del);
2511 namespace_or_type_name
2513 | IDENTIFIER DOUBLE_COLON IDENTIFIER {
2514 LocatedToken lt1 = (LocatedToken) $1;
2515 LocatedToken lt2 = (LocatedToken) $3;
2516 $$ = new MemberName (lt1.Value, lt2.Value, lt2.Location);
2518 | namespace_or_type_name DOT IDENTIFIER {
2519 LocatedToken lt = (LocatedToken) $3;
2520 $$ = new MemberName ((MemberName) $1, lt.Value);
2526 LocatedToken lt = (LocatedToken) $1;
2527 $$ = new MemberName (lt.Value, lt.Location);
2532 * Before you think of adding a return_type, notice that we have been
2533 * using two rules in the places where it matters (one rule using type
2534 * and another identical one that uses VOID as the return type). This
2535 * gets rid of a shift/reduce couple
2538 : namespace_or_type_name
2540 MemberName name = (MemberName) $1;
2541 $$ = name.GetTypeExpression ();
2553 // Note that here only unmanaged types are allowed but we
2554 // can't perform checks during this phase - we do it during
2555 // semantic analysis.
2557 $$ = new ComposedCast ((Expression) $1, "*", Lexer.Location);
2561 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2567 | non_expression_type rank_specifier
2569 Location loc = GetLocation ($1);
2571 loc = lexer.Location;
2572 $$ = new ComposedCast ((Expression) $1, (string) $2, loc);
2574 | non_expression_type STAR
2576 Location loc = GetLocation ($1);
2578 loc = lexer.Location;
2579 $$ = new ComposedCast ((Expression) $1, "*", loc);
2581 | expression rank_specifiers
2583 $$ = new ComposedCast ((Expression) $1, (string) $2);
2587 $$ = new ComposedCast ((Expression) $1, "*");
2591 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2592 // through this different path
2594 | multiplicative_expression STAR
2596 $$ = new ComposedCast ((Expression) $1, "*");
2603 ArrayList types = new ArrayList (4);
2608 | type_list COMMA type
2610 ArrayList types = (ArrayList) $1;
2618 * replaces all the productions for isolating the various
2619 * simple types, but we need this to reuse it easily in local_variable_type
2622 : OBJECT { $$ = TypeManager.system_object_expr; }
2623 | STRING { $$ = TypeManager.system_string_expr; }
2624 | BOOL { $$ = TypeManager.system_boolean_expr; }
2625 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2626 | FLOAT { $$ = TypeManager.system_single_expr; }
2627 | DOUBLE { $$ = TypeManager.system_double_expr; }
2632 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2633 | BYTE { $$ = TypeManager.system_byte_expr; }
2634 | SHORT { $$ = TypeManager.system_int16_expr; }
2635 | USHORT { $$ = TypeManager.system_uint16_expr; }
2636 | INT { $$ = TypeManager.system_int32_expr; }
2637 | UINT { $$ = TypeManager.system_uint32_expr; }
2638 | LONG { $$ = TypeManager.system_int64_expr; }
2639 | ULONG { $$ = TypeManager.system_uint64_expr; }
2640 | CHAR { $$ = TypeManager.system_char_expr; }
2641 | VOID { $$ = TypeManager.system_void_expr; }
2645 : type rank_specifiers
2647 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2);
2652 // Expressions, section 7.5
2662 MemberName mn = (MemberName) $1;
2663 $$ = mn.GetTypeExpression ();
2665 | IDENTIFIER DOUBLE_COLON IDENTIFIER
2667 LocatedToken lt1 = (LocatedToken) $1;
2668 LocatedToken lt2 = (LocatedToken) $3;
2669 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, lt2.Location);
2671 | parenthesized_expression
2673 | invocation_expression
2677 | post_increment_expression
2678 | post_decrement_expression
2682 | checked_expression
2683 | unchecked_expression
2684 | pointer_member_access
2685 | anonymous_method_expression
2692 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
2693 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); }
2694 | NULL { $$ = new NullLiteral (lexer.Location); }
2698 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
2699 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
2700 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
2705 object v = lexer.Value;
2708 $$ = new IntLiteral ((int) v, lexer.Location);
2709 } else if (v is uint)
2710 $$ = new UIntLiteral ((UInt32) v, lexer.Location);
2712 $$ = new LongLiteral ((Int64) v, lexer.Location);
2713 else if (v is ulong)
2714 $$ = new ULongLiteral ((UInt64) v, lexer.Location);
2716 Console.WriteLine ("OOPS. Unexpected result from scanner");
2721 : TRUE { $$ = new BoolLiteral (true, lexer.Location); }
2722 | FALSE { $$ = new BoolLiteral (false, lexer.Location); }
2725 parenthesized_expression_0
2726 : OPEN_PARENS expression CLOSE_PARENS
2729 lexer.Deambiguate_CloseParens ($$);
2730 // After this, the next token returned is one of
2731 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST (CLOSE_PARENS), CLOSE_PARENS_OPEN_PARENS
2732 // or CLOSE_PARENS_MINUS.
2734 | OPEN_PARENS expression error { CheckToken (1026, yyToken, "Expecting ')'", lexer.Location); }
2737 parenthesized_expression
2738 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2742 | parenthesized_expression_0 CLOSE_PARENS
2746 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2748 // If a parenthesized expression is followed by a minus, we need to wrap
2749 // the expression inside a ParenthesizedExpression for the CS0075 check
2750 // in Binary.DoResolve().
2751 $$ = new ParenthesizedExpression ((Expression) $1);
2756 : primary_expression DOT IDENTIFIER
2758 LocatedToken lt = (LocatedToken) $3;
2759 $$ = new MemberAccess ((Expression) $1, lt.Value);
2761 | predefined_type DOT IDENTIFIER
2763 LocatedToken lt = (LocatedToken) $3;
2764 // TODO: Location is wrong as some predefined types doesn't hold a location
2765 $$ = new MemberAccess ((Expression) $1, lt.Value, lt.Location);
2773 invocation_expression
2774 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2777 Report.Error (1, (Location) $2, "Parse error");
2779 $$ = new Invocation ((Expression) $1, (ArrayList) $3);
2781 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2783 $$ = new Invocation ((Expression) $1, new ArrayList ());
2785 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2787 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
2789 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2791 ArrayList args = new ArrayList (1);
2793 $$ = new Invocation ((Expression) $1, args);
2795 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2797 ArrayList args = ((ArrayList) $4);
2799 $$ = new Invocation ((Expression) $1, args);
2804 : /* empty */ { $$ = null; }
2811 ArrayList list = new ArrayList (4);
2815 | argument_list COMMA argument
2817 ArrayList list = (ArrayList) $1;
2821 | argument_list error {
2822 CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
2830 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2832 | non_simple_argument
2839 : REF variable_reference
2841 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2843 | OUT variable_reference
2845 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2847 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2849 ArrayList list = (ArrayList) $3;
2850 Argument[] args = new Argument [list.Count];
2851 list.CopyTo (args, 0);
2853 Expression expr = new Arglist (args, (Location) $1);
2854 $$ = new Argument (expr, Argument.AType.Expression);
2858 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
2863 : expression { note ("section 5.4"); $$ = $1; }
2867 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2869 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
2871 | primary_expression rank_specifiers
2873 // So the super-trick is that primary_expression
2874 // can only be either a SimpleName or a MemberAccess.
2875 // The MemberAccess case arises when you have a fully qualified type-name like :
2877 // SimpleName is when you have
2880 Expression expr = (Expression) $1;
2881 if (expr is ComposedCast){
2882 $$ = new ComposedCast (expr, (string) $2);
2883 } else if (!(expr is SimpleName || expr is MemberAccess || expr is QualifiedAliasMember)){
2884 Error_ExpectingTypeName (expr);
2885 $$ = TypeManager.system_object_expr;
2888 // So we extract the string corresponding to the SimpleName
2891 $$ = new ComposedCast (expr, (string) $2);
2893 current_array_type = (Expression)$$;
2900 ArrayList list = new ArrayList (4);
2904 | expression_list COMMA expression
2906 ArrayList list = (ArrayList) $1;
2915 $$ = new This (current_block, (Location) $1);
2920 : BASE DOT IDENTIFIER
2922 LocatedToken lt = (LocatedToken) $3;
2923 $$ = new BaseAccess (lt.Value, lt.Location);
2925 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2927 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
2930 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
2935 post_increment_expression
2936 : primary_expression OP_INC
2938 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2939 (Expression) $1, (Location) $2);
2943 post_decrement_expression
2944 : primary_expression OP_DEC
2946 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2947 (Expression) $1, (Location) $2);
2952 : object_or_delegate_creation_expression
2953 | array_creation_expression
2956 object_or_delegate_creation_expression
2957 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2959 $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
2963 array_creation_expression
2964 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2966 opt_array_initializer
2968 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
2970 | NEW type rank_specifiers array_initializer
2972 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, (Location) $1);
2976 Report.Error (1031, (Location) $1, "Type expected");
2981 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
2998 : rank_specifier opt_rank_specifier
3000 $$ = (string) $2 + (string) $1;
3005 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3007 $$ = "[" + (string) $2 + "]";
3027 | dim_separators COMMA
3029 $$ = (string) $1 + ",";
3033 opt_array_initializer
3045 : OPEN_BRACE CLOSE_BRACE
3047 ArrayList list = new ArrayList (4);
3050 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3052 $$ = (ArrayList) $2;
3056 variable_initializer_list
3057 : variable_initializer
3059 ArrayList list = new ArrayList (4);
3063 | variable_initializer_list COMMA variable_initializer
3065 ArrayList list = (ArrayList) $1;
3074 pushed_current_array_type = current_array_type;
3076 OPEN_PARENS type CLOSE_PARENS
3078 Expression type = (Expression)$4;
3079 if (type == TypeManager.system_void_expr)
3080 $$ = new TypeOfVoid ((Location) $1);
3082 $$ = new TypeOf (type, (Location) $1);
3083 current_array_type = pushed_current_array_type;
3088 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3089 $$ = new SizeOf ((Expression) $3, (Location) $1);
3094 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3096 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3100 unchecked_expression
3101 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3103 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3107 pointer_member_access
3108 : primary_expression OP_PTR IDENTIFIER
3111 LocatedToken lt = (LocatedToken) $3;
3113 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
3114 $$ = new MemberAccess (deref, lt.Value);
3118 anonymous_method_expression
3119 : DELEGATE opt_anonymous_method_signature
3121 if (oob_stack == null)
3122 oob_stack = new Stack (6);
3124 oob_stack.Push (current_anonymous_method);
3125 oob_stack.Push (current_local_parameters);
3126 current_local_parameters = (Parameters)$2;
3128 // Force the next block to be created as a ToplevelBlock
3129 oob_stack.Push (current_block);
3130 oob_stack.Push (top_current_block);
3132 Location loc = (Location) $1;
3133 current_anonymous_method = new AnonymousMethodExpression (
3134 current_anonymous_method, null, current_container,
3135 (Parameters) $2, (ToplevelBlock) top_current_block, loc);
3137 parsing_anonymous_method = true;
3141 Location loc = (Location) $1;
3142 top_current_block = (Block) oob_stack.Pop ();
3143 current_block = (Block) oob_stack.Pop ();
3145 if (RootContext.Version == LanguageVersion.ISO_1){
3146 Report.FeatureIsNotStandardized (loc, "anonymous methods");
3149 ToplevelBlock anon_block = (ToplevelBlock) $4;
3151 anon_block.Parent = current_block;
3153 current_anonymous_method.Block = anon_block;
3154 if ((anonymous_host != null) && (current_anonymous_method.Parent == null))
3155 anonymous_host.AddAnonymousMethod (current_anonymous_method);
3157 $$ = current_anonymous_method;
3160 current_local_parameters = (Parameters) oob_stack.Pop ();
3161 current_anonymous_method = (AnonymousMethodExpression) oob_stack.Pop ();
3165 opt_anonymous_method_signature
3166 : /* empty */ { $$ = null; }
3167 | anonymous_method_signature
3170 anonymous_method_signature
3171 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3174 $$ = Parameters.EmptyReadOnlyParameters;
3176 ArrayList par_list = (ArrayList) $2;
3177 Parameter [] pars = new Parameter [par_list.Count];
3178 par_list.CopyTo (pars);
3179 $$ = new Parameters (pars);
3184 opt_anonymous_method_parameter_list
3185 : /* empty */ { $$ = null; }
3186 | anonymous_method_parameter_list { $$ = $1; }
3189 anonymous_method_parameter_list
3190 : anonymous_method_parameter
3192 ArrayList a = new ArrayList (4);
3196 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3198 ArrayList a = (ArrayList) $1;
3204 anonymous_method_parameter
3205 : opt_parameter_modifier type IDENTIFIER {
3206 LocatedToken lt = (LocatedToken) $3;
3207 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3209 | PARAMS type IDENTIFIER {
3210 Report.Error (1670, ((LocatedToken) $3).Location, "The `params' modifier is not allowed in anonymous method declaration");
3216 : primary_expression
3217 | BANG prefixed_unary_expression
3219 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3221 | TILDE prefixed_unary_expression
3223 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3229 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3231 $$ = new Cast ((Expression) $1, (Expression) $3);
3233 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3235 $$ = new Cast ((Expression) $1, (Expression) $3);
3241 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3243 // TODO: wrong location
3244 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3249 // The idea to split this out is from Rhys' grammar
3250 // to solve the problem with casts.
3252 prefixed_unary_expression
3254 | PLUS prefixed_unary_expression
3256 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3258 | MINUS prefixed_unary_expression
3260 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3262 | OP_INC prefixed_unary_expression
3264 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3265 (Expression) $2, (Location) $1);
3267 | OP_DEC prefixed_unary_expression
3269 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3270 (Expression) $2, (Location) $1);
3272 | STAR prefixed_unary_expression
3274 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, (Location) $1);
3276 | BITWISE_AND prefixed_unary_expression
3278 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3282 pre_increment_expression
3283 : OP_INC prefixed_unary_expression
3285 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3286 (Expression) $2, (Location) $1);
3290 pre_decrement_expression
3291 : OP_DEC prefixed_unary_expression
3293 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3294 (Expression) $2, (Location) $1);
3298 multiplicative_expression
3299 : prefixed_unary_expression
3300 | multiplicative_expression STAR prefixed_unary_expression
3302 $$ = new Binary (Binary.Operator.Multiply,
3303 (Expression) $1, (Expression) $3);
3305 | multiplicative_expression DIV prefixed_unary_expression
3307 $$ = new Binary (Binary.Operator.Division,
3308 (Expression) $1, (Expression) $3);
3310 | multiplicative_expression PERCENT prefixed_unary_expression
3312 $$ = new Binary (Binary.Operator.Modulus,
3313 (Expression) $1, (Expression) $3);
3318 : multiplicative_expression
3319 | additive_expression PLUS multiplicative_expression
3321 $$ = new Binary (Binary.Operator.Addition,
3322 (Expression) $1, (Expression) $3);
3324 | additive_expression MINUS multiplicative_expression
3326 $$ = new Binary (Binary.Operator.Subtraction,
3327 (Expression) $1, (Expression) $3);
3332 : additive_expression
3333 | shift_expression OP_SHIFT_LEFT additive_expression
3335 $$ = new Binary (Binary.Operator.LeftShift,
3336 (Expression) $1, (Expression) $3);
3338 | shift_expression OP_SHIFT_RIGHT additive_expression
3340 $$ = new Binary (Binary.Operator.RightShift,
3341 (Expression) $1, (Expression) $3);
3345 relational_expression
3347 | relational_expression OP_LT shift_expression
3349 $$ = new Binary (Binary.Operator.LessThan,
3350 (Expression) $1, (Expression) $3);
3352 | relational_expression OP_GT shift_expression
3354 $$ = new Binary (Binary.Operator.GreaterThan,
3355 (Expression) $1, (Expression) $3);
3357 | relational_expression OP_LE shift_expression
3359 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3360 (Expression) $1, (Expression) $3);
3362 | relational_expression OP_GE shift_expression
3364 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3365 (Expression) $1, (Expression) $3);
3367 | relational_expression IS type
3369 $$ = new Is ((Expression) $1, (Expression) $3, (Location) $2);
3371 | relational_expression AS type
3373 $$ = new As ((Expression) $1, (Expression) $3, (Location) $2);
3378 : relational_expression
3379 | equality_expression OP_EQ relational_expression
3381 $$ = new Binary (Binary.Operator.Equality,
3382 (Expression) $1, (Expression) $3);
3384 | equality_expression OP_NE relational_expression
3386 $$ = new Binary (Binary.Operator.Inequality,
3387 (Expression) $1, (Expression) $3);
3392 : equality_expression
3393 | and_expression BITWISE_AND equality_expression
3395 $$ = new Binary (Binary.Operator.BitwiseAnd,
3396 (Expression) $1, (Expression) $3);
3400 exclusive_or_expression
3402 | exclusive_or_expression CARRET and_expression
3404 $$ = new Binary (Binary.Operator.ExclusiveOr,
3405 (Expression) $1, (Expression) $3);
3409 inclusive_or_expression
3410 : exclusive_or_expression
3411 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3413 $$ = new Binary (Binary.Operator.BitwiseOr,
3414 (Expression) $1, (Expression) $3);
3418 conditional_and_expression
3419 : inclusive_or_expression
3420 | conditional_and_expression OP_AND inclusive_or_expression
3422 $$ = new Binary (Binary.Operator.LogicalAnd,
3423 (Expression) $1, (Expression) $3);
3427 conditional_or_expression
3428 : conditional_and_expression
3429 | conditional_or_expression OP_OR conditional_and_expression
3431 $$ = new Binary (Binary.Operator.LogicalOr,
3432 (Expression) $1, (Expression) $3);
3436 conditional_expression
3437 : conditional_or_expression
3438 | conditional_or_expression INTERR expression COLON expression
3440 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
3444 assignment_expression
3445 : prefixed_unary_expression ASSIGN expression
3447 $$ = new Assign ((Expression) $1, (Expression) $3);
3449 | prefixed_unary_expression OP_MULT_ASSIGN expression
3451 $$ = new CompoundAssign (
3452 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
3454 | prefixed_unary_expression OP_DIV_ASSIGN expression
3456 $$ = new CompoundAssign (
3457 Binary.Operator.Division, (Expression) $1, (Expression) $3);
3459 | prefixed_unary_expression OP_MOD_ASSIGN expression
3461 $$ = new CompoundAssign (
3462 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
3464 | prefixed_unary_expression OP_ADD_ASSIGN expression
3466 $$ = new CompoundAssign (
3467 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
3469 | prefixed_unary_expression OP_SUB_ASSIGN expression
3471 $$ = new CompoundAssign (
3472 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3474 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3476 $$ = new CompoundAssign (
3477 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
3479 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3481 $$ = new CompoundAssign (
3482 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
3484 | prefixed_unary_expression OP_AND_ASSIGN expression
3486 $$ = new CompoundAssign (
3487 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
3489 | prefixed_unary_expression OP_OR_ASSIGN expression
3491 $$ = new CompoundAssign (
3492 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
3494 | prefixed_unary_expression OP_XOR_ASSIGN expression
3496 $$ = new CompoundAssign (
3497 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
3502 : conditional_expression
3503 | assignment_expression
3523 MemberName name = MakeName ((MemberName) $5);
3524 int mod_flags = (int) $2;
3526 push_current_class (new Class (
3527 current_namespace, current_class, name,
3528 mod_flags, (Attributes) $1), false, $3);
3533 if (current_class.Name == "System.Object") {
3534 Report.Error (537, current_class.Location,
3535 "The class System.Object cannot have a base " +
3536 "class or implement an interface.");
3538 current_container.AddBasesForPart (current_class, (ArrayList) $7);
3541 if (RootContext.Documentation != null) {
3542 current_container.DocComment = Lexer.consume_doc_comment ();
3543 Lexer.doc_state = XmlCommentState.Allowed;
3548 if (RootContext.Documentation != null)
3549 Lexer.doc_state = XmlCommentState.Allowed;
3553 $$ = pop_current_class ();
3561 { $$ = $1; } // location
3565 : /* empty */ { $$ = (int) 0; }
3571 | modifiers modifier
3576 if ((m1 & m2) != 0) {
3577 Location l = lexer.Location;
3578 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
3580 $$ = (int) (m1 | m2);
3585 : NEW { $$ = Modifiers.NEW; }
3586 | PUBLIC { $$ = Modifiers.PUBLIC; }
3587 | PROTECTED { $$ = Modifiers.PROTECTED; }
3588 | INTERNAL { $$ = Modifiers.INTERNAL; }
3589 | PRIVATE { $$ = Modifiers.PRIVATE; }
3590 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3591 | SEALED { $$ = Modifiers.SEALED; }
3592 | STATIC { $$ = Modifiers.STATIC; }
3593 | READONLY { $$ = Modifiers.READONLY; }
3594 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3595 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3596 | EXTERN { $$ = Modifiers.EXTERN; }
3597 | VOLATILE { $$ = Modifiers.VOLATILE; }
3598 | UNSAFE { $$ = Modifiers.UNSAFE; }
3602 : /* empty */ { $$ = null; }
3603 | class_base { $$ = $1; }
3607 : COLON type_list { $$ = $2; }
3615 // A block is "contained" on the following places:
3617 // property_declaration as part of the accessor body (get/set)
3618 // operator_declaration
3619 // constructor_declaration
3620 // destructor_declaration
3621 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3626 if (parsing_anonymous_method) {
3627 top_current_block = new ToplevelBlock (
3628 current_block, current_local_parameters, null, (Location) $1);
3629 if (current_block != null)
3630 current_block.AddAnonymousChild ((ToplevelBlock) top_current_block);
3631 current_block = top_current_block;
3632 parsing_anonymous_method = false;
3633 } else if (current_block == null){
3634 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, (Location) $1);
3635 top_current_block = current_block;
3637 current_block = new Block (current_block, (Location) $1, Location.Null);
3640 opt_statement_list CLOSE_BRACE
3642 while (current_block.Implicit)
3643 current_block = current_block.Parent;
3645 current_block.SetEndLocation ((Location) $4);
3646 current_block = current_block.Parent;
3647 if (current_block == null)
3648 top_current_block = null;
3659 | statement_list statement
3663 : declaration_statement
3665 if ($1 != null && (Block) $1 != current_block){
3666 current_block.AddStatement ((Statement) $1);
3667 current_block = (Block) $1;
3670 | valid_declaration_statement
3672 current_block.AddStatement ((Statement) $1);
3677 valid_declaration_statement
3680 | expression_statement
3681 | selection_statement
3682 | iteration_statement
3686 | unchecked_statement
3694 : valid_declaration_statement
3695 | declaration_statement
3697 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3702 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3710 $$ = EmptyStatement.Value;
3717 LocatedToken lt = (LocatedToken) $1;
3718 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
3720 if (current_block.AddLabel (labeled))
3721 current_block.AddStatement (labeled);
3726 declaration_statement
3727 : local_variable_declaration SEMICOLON
3729 current_array_type = null;
3731 DictionaryEntry de = (DictionaryEntry) $1;
3732 Expression e = (Expression) de.Key;
3734 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
3738 | local_constant_declaration SEMICOLON
3740 current_array_type = null;
3742 DictionaryEntry de = (DictionaryEntry) $1;
3744 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3750 * The following is from Rhys' grammar:
3751 * > Types in local variable declarations must be recognized as
3752 * > expressions to prevent reduce/reduce errors in the grammar.
3753 * > The expressions are converted into types during semantic analysis.
3756 : primary_expression opt_rank_specifier
3758 // FIXME: Do something smart here regarding the composition of the type.
3760 // Ok, the above "primary_expression" is there to get rid of
3761 // both reduce/reduce and shift/reduces in the grammar, it should
3762 // really just be "type_name". If you use type_name, a reduce/reduce
3763 // creeps up. If you use namespace_or_type_name (which is all we need
3764 // really) two shift/reduces appear.
3767 // So the super-trick is that primary_expression
3768 // can only be either a SimpleName or a MemberAccess.
3769 // The MemberAccess case arises when you have a fully qualified type-name like :
3771 // SimpleName is when you have
3774 Expression expr = (Expression) $1;
3775 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3776 Error_ExpectingTypeName (expr);
3780 // So we extract the string corresponding to the SimpleName
3784 if ((string) $2 == "")
3787 $$ = new ComposedCast ((Expression) $1, (string) $2);
3790 | builtin_types opt_rank_specifier
3792 if ((string) $2 == "")
3795 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3799 local_variable_pointer_type
3800 : primary_expression STAR
3802 Expression expr = (Expression) $1;
3804 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3805 Error_ExpectingTypeName (expr);
3809 $$ = new ComposedCast ((Expression) $1, "*");
3811 | builtin_types STAR
3813 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3817 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
3819 | local_variable_pointer_type STAR
3821 $$ = new ComposedCast ((Expression) $1, "*");
3825 local_variable_declaration
3826 : local_variable_type variable_declarators
3829 $$ = new DictionaryEntry ($1, $2);
3833 | local_variable_pointer_type opt_rank_specifier variable_declarators
3838 if ((string) $2 == "")
3839 t = (Expression) $1;
3841 t = new ComposedCast ((Expression) $1, (string) $2);
3842 $$ = new DictionaryEntry (t, $3);
3848 local_constant_declaration
3849 : CONST local_variable_type constant_declarators
3852 $$ = new DictionaryEntry ($2, $3);
3858 expression_statement
3859 : statement_expression SEMICOLON { $$ = $1; }
3863 // We have to do the wrapping here and not in the case above,
3864 // because statement_expression is used for example in for_statement
3866 statement_expression
3869 Expression expr = (Expression) $1;
3870 ExpressionStatement s = expr as ExpressionStatement;
3872 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
3875 $$ = new StatementExpression (s);
3879 Report.Error (1002, GetLocation ($1), "Expecting `;'");
3884 object_creation_expression
3885 : object_or_delegate_creation_expression
3886 { note ("complain if this is a delegate maybe?"); }
3895 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
3898 Location l = (Location) $1;
3900 $$ = new If ((Expression) $3, (Statement) $5, l);
3902 // FIXME: location for warning should be loc property of $5.
3903 if ($5 == EmptyStatement.Value)
3904 Report.Warning (642, 3, l, "Possible mistaken empty statement");
3907 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
3908 embedded_statement ELSE embedded_statement
3910 Location l = (Location) $1;
3912 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
3914 // FIXME: location for warning should be loc property of $5 and $7.
3915 if ($5 == EmptyStatement.Value)
3916 Report.Warning (642, 3, l, "Possible mistaken empty statement");
3917 if ($7 == EmptyStatement.Value)
3918 Report.Warning (642, 3, l, "Possible mistaken empty statement");
3923 : SWITCH OPEN_PARENS
3925 if (switch_stack == null)
3926 switch_stack = new Stack (2);
3927 switch_stack.Push (current_block);
3929 expression CLOSE_PARENS
3932 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
3933 current_block = (Block) switch_stack.Pop ();
3949 Report.Warning (1522, 1, lexer.Location, "Empty switch block");
3950 $$ = new ArrayList ();
3958 ArrayList sections = new ArrayList (4);
3963 | switch_sections switch_section
3965 ArrayList sections = (ArrayList) $1;
3975 current_block = current_block.CreateSwitchBlock (lexer.Location);
3979 Block topmost = current_block;
3981 while (topmost.Implicit)
3982 topmost = topmost.Parent;
3983 $$ = new SwitchSection ((ArrayList) $1, topmost);
3990 ArrayList labels = new ArrayList (4);
3995 | switch_labels switch_label
3997 ArrayList labels = (ArrayList) ($1);
4005 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
4006 | DEFAULT COLON { $$ = new SwitchLabel (null, (Location) $1); }
4009 1523, GetLocation ($1),
4010 "The keyword case or default must precede code in switch block");
4022 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
4024 Location l = (Location) $1;
4025 $$ = new While ((Expression) $3, (Statement) $5, l);
4030 : DO embedded_statement
4031 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
4033 Location l = (Location) $1;
4035 $$ = new Do ((Statement) $2, (Expression) $5, l);
4041 opt_for_initializer SEMICOLON
4043 Block assign_block = new Block (current_block);
4044 current_block = assign_block;
4046 if ($3 is DictionaryEntry){
4047 DictionaryEntry de = (DictionaryEntry) $3;
4049 Expression type = (Expression) de.Key;
4050 ArrayList var_declarators = (ArrayList) de.Value;
4052 foreach (VariableDeclaration decl in var_declarators){
4056 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4060 Location l = lexer.Location;
4061 Expression expr = decl.expression_or_array_initializer;
4063 LocalVariableReference var;
4064 var = new LocalVariableReference (assign_block, decl.identifier, l);
4067 Assign a = new Assign (var, expr, decl.Location);
4069 assign_block.AddStatement (new StatementExpression (a));
4073 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4074 // This can be referred to as $5 below.
4080 opt_for_condition SEMICOLON
4081 opt_for_iterator CLOSE_PARENS
4084 Location l = (Location) $1;
4086 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4088 current_block.AddStatement (f);
4089 while (current_block.Implicit)
4090 current_block = current_block.Parent;
4092 current_block = current_block.Parent;
4097 : /* empty */ { $$ = EmptyStatement.Value; }
4102 : local_variable_declaration
4103 | statement_expression_list
4107 : /* empty */ { $$ = null; }
4108 | boolean_expression
4112 : /* empty */ { $$ = EmptyStatement.Value; }
4117 : statement_expression_list
4120 statement_expression_list
4121 : statement_expression
4123 // CHANGE: was `null'
4124 Statement s = (Statement) $1;
4125 Block b = new Block (current_block, Block.Flags.Implicit, s.loc, lexer.Location);
4130 | statement_expression_list COMMA statement_expression
4132 Block b = (Block) $1;
4134 b.AddStatement ((Statement) $3);
4140 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4142 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4145 | FOREACH OPEN_PARENS type IDENTIFIER IN
4146 expression CLOSE_PARENS
4148 Block foreach_block = new Block (current_block);
4149 current_block = foreach_block;
4151 LocatedToken lt = (LocatedToken) $4;
4152 Location l = lt.Location;
4155 vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4157 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4159 // Get a writable reference to this read-only variable.
4161 // Note that the $$ here refers to the value of _this_ code block,
4162 // not the value of the LHS non-terminal. This can be referred to as $8 below.
4163 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
4170 LocalVariableReference v = (LocalVariableReference) $8;
4171 Location l = (Location) $1;
4174 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
4175 current_block.AddStatement (f);
4178 while (current_block.Implicit)
4179 current_block = current_block.Parent;
4181 current_block = current_block.Parent;
4187 | continue_statement
4197 $$ = new Break ((Location) $1);
4202 : CONTINUE SEMICOLON
4204 $$ = new Continue ((Location) $1);
4209 : GOTO IDENTIFIER SEMICOLON
4211 LocatedToken lt = (LocatedToken) $2;
4212 $$ = new Goto (lt.Value, lt.Location);
4214 | GOTO CASE constant_expression SEMICOLON
4216 $$ = new GotoCase ((Expression) $3, (Location) $1);
4218 | GOTO DEFAULT SEMICOLON
4220 $$ = new GotoDefault ((Location) $1);
4225 : RETURN opt_expression SEMICOLON
4227 $$ = new Return ((Expression) $2, (Location) $1);
4232 : THROW opt_expression SEMICOLON
4234 $$ = new Throw ((Expression) $2, (Location) $1);
4239 : IDENTIFIER RETURN expression SEMICOLON
4241 LocatedToken lt = (LocatedToken) $1;
4242 string s = lt.Value;
4244 Report.Error (1003, lt.Location, "; expected");
4247 if (RootContext.Version == LanguageVersion.ISO_1){
4248 Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4251 if (anonymous_host == null){
4252 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4255 anonymous_host.SetYields ();
4256 $$ = new Yield ((Expression) $3, lt.Location);
4259 | IDENTIFIER RETURN SEMICOLON
4261 Report.Error (1627, (Location) $2, "Expression expected after yield return");
4264 | IDENTIFIER BREAK SEMICOLON
4266 LocatedToken lt = (LocatedToken) $1;
4267 string s = lt.Value;
4269 Report.Error (1003, lt.Location, "; expected");
4272 if (RootContext.Version == LanguageVersion.ISO_1){
4273 Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4276 if (anonymous_host == null){
4277 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4280 anonymous_host.SetYields ();
4281 $$ = new YieldBreak (lt.Location);
4292 : TRY block catch_clauses
4296 ArrayList c = (ArrayList)$3;
4297 for (int i = 0; i < c.Count; ++i) {
4298 Catch cc = (Catch) c [i];
4300 if (i != c.Count - 1)
4301 Report.Error (1017, cc.loc, "Try statement already has an empty catch block");
4308 // Now s contains the list of specific catch clauses
4309 // and g contains the general one.
4311 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4313 | TRY block opt_catch_clauses FINALLY block
4316 ArrayList s = new ArrayList (4);
4317 ArrayList catch_list = (ArrayList) $3;
4319 if (catch_list != null){
4320 foreach (Catch cc in catch_list) {
4328 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4332 Report.Error (1524, (Location) $1, "Expected catch or finally");
4338 : /* empty */ { $$ = null; }
4345 ArrayList l = new ArrayList (4);
4350 | catch_clauses catch_clause
4352 ArrayList l = (ArrayList) $1;
4360 : /* empty */ { $$ = null; }
4365 : CATCH opt_catch_args
4367 Expression type = null;
4370 DictionaryEntry cc = (DictionaryEntry) $2;
4371 type = (Expression) cc.Key;
4372 LocatedToken lt = (LocatedToken) cc.Value;
4375 ArrayList one = new ArrayList (4);
4377 one.Add (new VariableDeclaration (lt, null));
4379 current_block = new Block (current_block);
4380 Block b = declare_local_variables (type, one, lt.Location);
4385 Expression type = null;
4387 Block var_block = null;
4390 DictionaryEntry cc = (DictionaryEntry) $2;
4391 type = (Expression) cc.Key;
4392 LocatedToken lt = (LocatedToken) cc.Value;
4396 while (current_block.Implicit)
4397 current_block = current_block.Parent;
4398 var_block = current_block;
4399 current_block = current_block.Parent;
4403 $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
4408 : /* empty */ { $$ = null; }
4413 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4415 $$ = new DictionaryEntry ($2, $3);
4423 $$ = new Checked ((Block) $2);
4430 $$ = new Unchecked ((Block) $2);
4437 RootContext.CheckUnsafeOption ((Location) $1);
4439 $$ = new Unsafe ((Block) $3);
4445 type fixed_pointer_declarators
4448 ArrayList list = (ArrayList) $4;
4449 Expression type = (Expression) $3;
4450 Location l = (Location) $1;
4451 int top = list.Count;
4453 Block assign_block = new Block (current_block);
4454 current_block = assign_block;
4456 for (int i = 0; i < top; i++){
4457 Pair p = (Pair) list [i];
4460 v = current_block.AddVariable (type, (string) p.First, l);
4464 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4472 Location l = (Location) $1;
4474 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4476 current_block.AddStatement (f);
4477 while (current_block.Implicit)
4478 current_block = current_block.Parent;
4480 current_block = current_block.Parent;
4484 fixed_pointer_declarators
4485 : fixed_pointer_declarator {
4486 ArrayList declarators = new ArrayList (4);
4488 declarators.Add ($1);
4491 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4493 ArrayList declarators = (ArrayList) $1;
4495 declarators.Add ($3);
4500 fixed_pointer_declarator
4501 : IDENTIFIER ASSIGN expression
4503 LocatedToken lt = (LocatedToken) $1;
4504 // FIXME: keep location
4505 $$ = new Pair (lt.Value, $3);
4509 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
4515 : LOCK OPEN_PARENS expression CLOSE_PARENS
4521 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
4526 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4528 Block assign_block = new Block (current_block);
4529 current_block = assign_block;
4531 if ($3 is DictionaryEntry){
4532 DictionaryEntry de = (DictionaryEntry) $3;
4533 Location l = (Location) $1;
4535 Expression type = (Expression) de.Key;
4536 ArrayList var_declarators = (ArrayList) de.Value;
4538 ArrayList vars = new ArrayList (4);
4540 foreach (VariableDeclaration decl in var_declarators){
4542 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4545 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
4547 Expression expr = decl.expression_or_array_initializer;
4549 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4552 LocalVariableReference var;
4554 // Get a writable reference to this read-only variable.
4555 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4557 // This is so that it is not a warning on using variables
4560 vars.Add (new DictionaryEntry (var, expr));
4562 // Assign a = new Assign (var, expr, decl.Location);
4563 // assign_block.AddStatement (new StatementExpression (a));
4566 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4567 // It can be referred to as $5 below.
4568 $$ = new DictionaryEntry (type, vars);
4575 Using u = new Using ($5, (Statement) $6, (Location) $1);
4576 current_block.AddStatement (u);
4577 while (current_block.Implicit)
4578 current_block = current_block.Parent;
4580 current_block = current_block.Parent;
4584 resource_acquisition
4585 : local_variable_declaration
4592 // A class used to pass around variable declarations and constants
4594 public class VariableDeclaration {
4595 public string identifier;
4596 public Expression expression_or_array_initializer;
4597 public Location Location;
4598 public Attributes OptAttributes;
4599 public string DocComment;
4601 public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
4603 this.identifier = lt.Value;
4604 if (eoai is ArrayList) {
4605 if (CSharpParser.current_array_type == null)
4606 Report.Error (622, lt.Location,
4607 "Can only use array initializer expressions to assign to array types. Try using a new expression instead.");
4608 this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
4610 this.expression_or_array_initializer = (Expression)eoai;
4612 this.Location = lt.Location;
4613 this.OptAttributes = opt_attrs;
4616 public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
4622 // A class used to hold info about an indexer declarator
4624 public class IndexerDeclaration {
4625 public Expression type;
4626 public MemberName interface_type;
4627 public Parameters param_list;
4628 public Location location;
4630 public IndexerDeclaration (Expression type, MemberName interface_type,
4631 Parameters param_list, Location loc)
4634 this.interface_type = interface_type;
4635 this.param_list = param_list;
4636 this.location = loc;
4641 // We use this when we do not have an object in advance that is an IAnonymousHost
4643 public class SimpleAnonymousHost : IAnonymousHost {
4644 public static readonly SimpleAnonymousHost Simple = new SimpleAnonymousHost ();
4647 ArrayList anonymous_methods;
4649 public static SimpleAnonymousHost GetSimple () {
4650 Simple.yields = false;
4651 Simple.anonymous_methods = null;
4655 public void SetYields ()
4660 public void AddAnonymousMethod (AnonymousMethodExpression anonymous)
4662 if (anonymous_methods == null)
4663 anonymous_methods = new ArrayList ();
4664 anonymous_methods.Add (anonymous);
4667 public void Propagate (IAnonymousHost real_host)
4670 real_host.SetYields ();
4671 if (anonymous_methods != null) {
4672 foreach (AnonymousMethodExpression ame in anonymous_methods)
4673 real_host.AddAnonymousMethod (ame);
4679 // A class used to hold info about an operator declarator
4681 public class OperatorDeclaration {
4682 public Operator.OpType optype;
4683 public Expression ret_type, arg1type, arg2type;
4684 public string arg1name, arg2name;
4685 public Location location;
4687 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4688 Expression arg1type, string arg1name,
4689 Expression arg2type, string arg2name, Location location)
4692 this.ret_type = ret_type;
4693 this.arg1type = arg1type;
4694 this.arg1name = arg1name;
4695 this.arg2type = arg2type;
4696 this.arg2name = arg2name;
4697 this.location = location;
4702 void Error_ExpectingTypeName (Expression expr)
4704 if (expr is Invocation){
4705 Report.Error (1002, expr.Location, "Expecting `;'");
4707 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
4711 void push_current_class (TypeContainer tc, bool is_interface, object partial_token)
4713 if (partial_token != null)
4714 current_container = current_container.AddPartial (tc, is_interface);
4716 current_container = current_container.AddTypeContainer (tc, is_interface);
4720 DeclSpace pop_current_class ()
4722 DeclSpace retval = current_class;
4724 current_class = current_class.Parent;
4725 current_container = current_class.PartialContainer;
4731 // Given the @class_name name, it creates a fully qualified name
4732 // based on the containing declaration space
4735 MakeName (MemberName class_name)
4737 Namespace ns = current_namespace.NS;
4739 if (current_container.Name.Length == 0){
4740 if (ns.Name.Length != 0)
4741 return new MemberName (ns.MemberName, class_name);
4745 return new MemberName (current_container.MemberName, class_name);
4749 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4751 Block implicit_block;
4752 ArrayList inits = null;
4755 // We use the `Used' property to check whether statements
4756 // have been added to the current block. If so, we need
4757 // to create another block to contain the new declaration
4758 // otherwise, as an optimization, we use the same block to
4759 // add the declaration.
4761 // FIXME: A further optimization is to check if the statements
4762 // that were added were added as part of the initialization
4763 // below. In which case, no other statements have been executed
4764 // and we might be able to reduce the number of blocks for
4765 // situations like this:
4767 // int j = 1; int k = j + 1;
4769 if (current_block.Used)
4770 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4772 implicit_block = current_block;
4774 foreach (VariableDeclaration decl in variable_declarators){
4776 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
4777 if (decl.expression_or_array_initializer != null){
4779 inits = new ArrayList (4);
4786 return implicit_block;
4788 foreach (VariableDeclaration decl in inits){
4790 Expression expr = decl.expression_or_array_initializer;
4792 LocalVariableReference var;
4793 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4795 assign = new Assign (var, expr, decl.Location);
4797 implicit_block.AddStatement (new StatementExpression (assign));
4800 return implicit_block;
4803 Block declare_local_constants (Expression type, ArrayList declarators)
4805 Block implicit_block;
4807 if (current_block.Used)
4808 implicit_block = new Block (current_block, Block.Flags.Implicit);
4810 implicit_block = current_block;
4812 foreach (VariableDeclaration decl in declarators){
4813 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
4816 return implicit_block;
4819 void CheckAttributeTarget (string a, Location l)
4823 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4827 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4833 void CheckUnaryOperator (Operator.OpType op, Location l)
4837 case Operator.OpType.LogicalNot:
4838 case Operator.OpType.OnesComplement:
4839 case Operator.OpType.Increment:
4840 case Operator.OpType.Decrement:
4841 case Operator.OpType.True:
4842 case Operator.OpType.False:
4843 case Operator.OpType.Addition:
4844 case Operator.OpType.Subtraction:
4849 Report.Error (1019, l, "Overloadable unary operator expected");
4855 void CheckBinaryOperator (Operator.OpType op, Location l)
4859 case Operator.OpType.Addition:
4860 case Operator.OpType.Subtraction:
4861 case Operator.OpType.Multiply:
4862 case Operator.OpType.Division:
4863 case Operator.OpType.Modulus:
4864 case Operator.OpType.BitwiseAnd:
4865 case Operator.OpType.BitwiseOr:
4866 case Operator.OpType.ExclusiveOr:
4867 case Operator.OpType.LeftShift:
4868 case Operator.OpType.RightShift:
4869 case Operator.OpType.Equality:
4870 case Operator.OpType.Inequality:
4871 case Operator.OpType.GreaterThan:
4872 case Operator.OpType.LessThan:
4873 case Operator.OpType.GreaterThanOrEqual:
4874 case Operator.OpType.LessThanOrEqual:
4878 Report.Error (1020, l, "Overloadable binary operator expected");
4884 void syntax_error (Location l, string msg)
4886 Report.Error (1003, l, "Syntax error, " + msg);
4889 void note (string s)
4891 // Used to put annotations
4896 public Tokenizer Lexer {
4902 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4904 this.name = file.Name;
4906 current_namespace = new NamespaceEntry (null, file, null);
4907 current_class = current_namespace.SlaveDeclSpace;
4908 current_container = current_class.PartialContainer; // == RootContest.ToplevelTypes
4910 lexer = new Tokenizer (reader, file, defines);
4913 public void parse ()
4915 int errors = Report.Errors;
4917 if (yacc_verbose_flag > 1)
4918 yyparse (lexer, new yydebug.yyDebugSimple ());
4921 Tokenizer tokenizer = lexer as Tokenizer;
4922 tokenizer.cleanup ();
4923 } catch (Exception e){
4925 // Removed for production use, use parser verbose to get the output.
4927 // Console.WriteLine (e);
4928 if (Report.Errors == errors)
4929 Report.Error (-25, lexer.Location, "Parsing error");
4930 if (yacc_verbose_flag > 0)
4931 Console.WriteLine (e);
4934 if (RootContext.ToplevelTypes.NamespaceEntry != null)
4935 throw new InternalErrorException ("who set it?");
4938 static void CheckToken (int error, int yyToken, string msg, Location loc)
4940 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
4941 Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ());
4943 Report.Error (error, loc, msg);
4946 void CheckIdentifierToken (int yyToken, Location loc)
4948 CheckToken (1041, yyToken, "Identifier expected", loc);
4951 string ConsumeStoredComment ()
4953 string s = tmpComment;
4955 Lexer.doc_state = XmlCommentState.Allowed;
4959 Location GetLocation (object obj)
4961 if (obj is MemberCore)
4962 return ((MemberCore) obj).Location;
4963 if (obj is MemberName)
4964 return ((MemberName) obj).Location;
4965 if (obj is LocatedToken)
4966 return ((LocatedToken) obj).Location;
4967 if (obj is Location)
4968 return (Location) obj;
4969 return lexer.Location;