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 IIteratorContainer iterator_container;
41 /// Current block is used to add statements as we find
44 Block current_block, top_current_block;
47 /// This is used by the unary_expression code to resolve
48 /// a name against a parameter.
50 Parameters current_local_parameters;
53 /// Using during property parsing to describe the implicit
54 /// value parameter that is passed to the "set" and "get"accesor
55 /// methods (properties and indexers).
57 Expression implicit_value_parameter_type;
58 Parameters indexer_parameters;
61 /// Hack to help create non-typed array initializer
63 public static Expression current_array_type;
64 Expression pushed_current_array_type;
67 /// Used to determine if we are parsing the get/set pair
68 /// of an indexer or a property
73 /// An out-of-band stack.
82 static public int yacc_verbose_flag;
84 // Name of the file we are parsing
93 /// Temporary Xml documentation cache.
94 /// For enum types, we need one more temporary store.
97 string enumTypeComment;
99 /// Current attribute target
100 string current_attr_target;
102 /// assembly and module attribute definitions are enabled
103 bool global_attrs_enabled = true;
105 bool has_get, has_set;
110 %token NONE /* This token is never returned by our lexer */
111 %token ERROR // This is used not by the parser, but by the tokenizer.
115 *These are the C# keywords
201 /* C# keywords which are not really keywords */
207 /* C# single character operators/punctuation. */
208 %token OPEN_BRACE "{"
209 %token CLOSE_BRACE "}"
210 %token OPEN_BRACKET "["
211 %token CLOSE_BRACKET "]"
212 %token OPEN_PARENS "("
213 %token CLOSE_PARENS ")"
226 %token BITWISE_AND "&"
227 %token BITWISE_OR "|"
234 /* C# multi-character operators. */
235 %token DOUBLE_COLON "::"
238 %token OP_SHIFT_LEFT "<<"
239 %token OP_SHIFT_RIGHT ">>"
246 %token OP_MULT_ASSIGN "*="
247 %token OP_DIV_ASSIGN "/="
248 %token OP_MOD_ASSIGN "%="
249 %token OP_ADD_ASSIGN "+="
250 %token OP_SUB_ASSIGN "-="
251 %token OP_SHIFT_LEFT_ASSIGN "<<="
252 %token OP_SHIFT_RIGHT_ASSIGN ">>="
253 %token OP_AND_ASSIGN "&="
254 %token OP_XOR_ASSIGN "^="
255 %token OP_OR_ASSIGN "|="
259 %token LITERAL_INTEGER "int literal"
260 %token LITERAL_FLOAT "float literal"
261 %token LITERAL_DOUBLE "double literal"
262 %token LITERAL_DECIMAL "decimal literal"
263 %token LITERAL_CHARACTER "character literal"
264 %token LITERAL_STRING "string literal"
267 %token CLOSE_PARENS_CAST
268 %token CLOSE_PARENS_NO_CAST
269 %token CLOSE_PARENS_OPEN_PARENS
270 %token CLOSE_PARENS_MINUS
272 /* Add precedence rules to solve dangling else s/r conflict */
281 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
283 %left STAR DIV PERCENT
284 %right BANG CARRET UMINUS
285 %nonassoc OP_INC OP_DEC
287 %left OPEN_BRACKET OPEN_BRACE
291 %start compilation_unit
295 : outer_declarations opt_EOF
296 | outer_declarations global_attributes opt_EOF
297 | global_attributes opt_EOF
298 | opt_EOF /* allow empty files */
304 Lexer.check_incorrect_doc_comment ();
308 Lexer.check_incorrect_doc_comment ();
314 | outer_declarations outer_declaration
318 : extern_alias_directive
320 | namespace_member_declaration
323 extern_alias_directives
324 : extern_alias_directive
325 | extern_alias_directives extern_alias_directive;
327 extern_alias_directive
328 : EXTERN IDENTIFIER IDENTIFIER SEMICOLON
330 LocatedToken lt = (LocatedToken) $2;
333 Report.Error (1003, lt.Location, "'alias' expected");
334 } else if (RootContext.Version == LanguageVersion.ISO_1) {
335 Report.FeatureIsNotStandardized (lt.Location, "external alias");
337 lt = (LocatedToken) $3;
338 current_namespace.UsingExternalAlias (lt.Value, lt.Location);
345 | using_directives using_directive
349 : using_alias_directive
351 if (RootContext.Documentation != null)
352 Lexer.doc_state = XmlCommentState.Allowed;
354 | using_namespace_directive
356 if (RootContext.Documentation != null)
357 Lexer.doc_state = XmlCommentState.Allowed;
361 using_alias_directive
362 : USING IDENTIFIER ASSIGN
363 namespace_or_type_name SEMICOLON
365 LocatedToken lt = (LocatedToken) $2;
366 current_namespace.UsingAlias (lt.Value, (MemberName) $4, (Location) $1);
369 CheckIdentifierToken (yyToken, GetLocation ($2));
373 using_namespace_directive
374 : USING namespace_name SEMICOLON
376 current_namespace.Using ((MemberName) $2, (Location) $1);
381 // Strictly speaking, namespaces don't have attributes but
382 // we parse global attributes along with namespace declarations and then
385 namespace_declaration
386 : opt_attributes NAMESPACE namespace_or_type_name
388 MemberName name = (MemberName) $3;
391 Report.Error(1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
394 current_namespace = new NamespaceEntry (
395 current_namespace, file, name.GetName ());
396 current_class = current_namespace.SlaveDeclSpace;
397 current_container = current_class.PartialContainer;
399 namespace_body opt_semicolon
401 current_namespace = current_namespace.Parent;
402 current_class = current_namespace.SlaveDeclSpace;
403 current_container = current_class.PartialContainer;
418 : namespace_or_type_name
424 if (RootContext.Documentation != null)
425 Lexer.doc_state = XmlCommentState.Allowed;
427 opt_extern_alias_directives
429 opt_namespace_member_declarations
438 opt_extern_alias_directives
440 | extern_alias_directives
443 opt_namespace_member_declarations
445 | namespace_member_declarations
448 namespace_member_declarations
449 : namespace_member_declaration
450 | namespace_member_declarations namespace_member_declaration
453 namespace_member_declaration
457 DeclSpace ds = (DeclSpace)$1;
459 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
460 Report.Error (1527, ds.Location,
461 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
464 current_namespace.DeclarationFound = true;
466 | namespace_declaration {
467 current_namespace.DeclarationFound = true;
470 | field_declaration {
471 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
473 | method_declaration {
474 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
481 | interface_declaration
483 | delegate_declaration
485 // Enable this when we have handled all errors, because this acts as a generic fallback
488 // Console.WriteLine ("Token=" + yyToken);
489 // Report.Error (1518, GetLocation ($1), "Expected class, struct, interface, enum or delegate");
501 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
509 global_attrs_enabled = false;
514 global_attrs_enabled = false;
523 ArrayList sect = (ArrayList) $1;
525 if (global_attrs_enabled) {
526 if (current_attr_target == "module") {
527 CodeGen.Module.AddAttributes (sect);
529 } else if (current_attr_target != null && current_attr_target.Length > 0) {
530 CodeGen.Assembly.AddAttributes (sect);
533 $$ = new Attributes (sect);
536 if (RootContext.Documentation != null) {
537 Lexer.check_incorrect_doc_comment ();
539 XmlCommentState.Allowed;
543 $$ = new Attributes (sect);
545 current_attr_target = null;
547 | attribute_sections attribute_section
549 Attributes attrs = $1 as Attributes;
550 ArrayList sect = (ArrayList) $2;
552 if (global_attrs_enabled) {
553 if (current_attr_target == "module") {
554 CodeGen.Module.AddAttributes (sect);
556 } else if (current_attr_target == "assembly") {
557 CodeGen.Assembly.AddAttributes (sect);
561 attrs = new Attributes (sect);
563 attrs.AddAttributes (sect);
567 attrs = new Attributes (sect);
569 attrs.AddAttributes (sect);
572 current_attr_target = null;
577 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
581 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
587 attribute_target_specifier
588 : attribute_target COLON
590 current_attr_target = (string)$1;
598 LocatedToken lt = (LocatedToken) $1;
599 CheckAttributeTarget (lt.Value, lt.Location);
600 $$ = lt.Value; // Location won't be required anymore.
602 | EVENT { $$ = "event"; }
603 | RETURN { $$ = "return"; }
609 ArrayList attrs = new ArrayList (4);
615 | attribute_list COMMA attribute
617 ArrayList attrs = (ArrayList) $1;
625 : attribute_name opt_attribute_arguments
627 MemberName mname = (MemberName) $1;
628 object[] arguments = (object[]) $2;
629 MemberName left = mname.Left;
630 string identifier = mname.Name;
632 Expression left_expr = left == null ? null : left.GetTypeExpression ();
634 if (current_attr_target == "assembly" || current_attr_target == "module")
635 // FIXME: supply "nameEscaped" parameter here.
636 $$ = new GlobalAttribute (current_namespace, current_attr_target,
637 left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
639 $$ = new Attribute (current_attr_target, left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
644 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
647 opt_attribute_arguments
648 : /* empty */ { $$ = null; }
649 | OPEN_PARENS attribute_arguments CLOSE_PARENS
657 : opt_positional_argument_list
662 $$ = new object [] { $1, null };
665 | positional_argument_list COMMA named_argument_list
667 $$ = new object[] { $1, $3 };
669 | named_argument_list
671 $$ = new object [] { null, $1 };
676 opt_positional_argument_list
677 : /* empty */ { $$ = null; }
678 | positional_argument_list
681 positional_argument_list
684 ArrayList args = new ArrayList (4);
685 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
689 | positional_argument_list COMMA expression
691 ArrayList args = (ArrayList) $1;
692 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
701 ArrayList args = new ArrayList (4);
706 | named_argument_list COMMA named_argument
708 ArrayList args = (ArrayList) $1;
713 | named_argument_list COMMA expression
715 Report.Error (1016, ((Expression) $3).Location, "Named attribute argument expected");
721 : IDENTIFIER ASSIGN expression
723 // FIXME: keep location
724 $$ = new DictionaryEntry (
725 ((LocatedToken) $1).Value,
726 new Argument ((Expression) $3, Argument.AType.Expression));
732 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
735 opt_class_member_declarations
737 | class_member_declarations
740 class_member_declarations
741 : class_member_declaration
742 | class_member_declarations
743 class_member_declaration
746 class_member_declaration
747 : constant_declaration // done
748 | field_declaration // done
749 | method_declaration // done
750 | property_declaration // done
751 | event_declaration // done
752 | indexer_declaration // done
753 | operator_declaration // done
754 | constructor_declaration // done
755 | destructor_declaration // done
765 MemberName name = MakeName ((MemberName) $5);
766 push_current_class (new Struct (
767 current_namespace, current_class, name, (int) $2,
768 (Attributes) $1), false, $3);
774 current_container.AddBasesForPart (current_class, (ArrayList) $7);
776 if (RootContext.Documentation != null)
777 current_container.DocComment = Lexer.consume_doc_comment ();
781 if (RootContext.Documentation != null)
782 Lexer.doc_state = XmlCommentState.Allowed;
786 $$ = pop_current_class ();
788 | opt_attributes opt_modifiers opt_partial STRUCT error {
789 CheckIdentifierToken (yyToken, GetLocation ($5));
796 if (RootContext.Documentation != null)
797 Lexer.doc_state = XmlCommentState.Allowed;
799 opt_struct_member_declarations CLOSE_BRACE
802 opt_struct_member_declarations
804 | struct_member_declarations
807 struct_member_declarations
808 : struct_member_declaration
809 | struct_member_declarations struct_member_declaration
812 struct_member_declaration
813 : constant_declaration
816 | property_declaration
818 | indexer_declaration
819 | operator_declaration
820 | constructor_declaration
824 * This is only included so we can flag error 575:
825 * destructors only allowed on class types
827 | destructor_declaration
838 int modflags = (int) $2;
839 foreach (VariableDeclaration constant in (ArrayList) $5){
840 Location l = constant.Location;
841 if ((modflags & Modifiers.STATIC) != 0) {
842 Report.Error (504, l, "The constant `{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
846 Const c = new Const (
847 current_class, (Expression) $4, (string) constant.identifier,
848 (Expression) constant.expression_or_array_initializer, modflags,
851 if (RootContext.Documentation != null) {
852 c.DocComment = Lexer.consume_doc_comment ();
853 Lexer.doc_state = XmlCommentState.Allowed;
855 current_container.AddConstant (c);
861 : constant_declarator
863 ArrayList constants = new ArrayList (4);
868 | constant_declarators COMMA constant_declarator
871 ArrayList constants = (ArrayList) $1;
878 : IDENTIFIER ASSIGN constant_expression
880 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
884 // A const field requires a value to be provided
885 Report.Error (145, ((LocatedToken) $1).Location, "A const field requires a value to be provided");
897 Expression type = (Expression) $3;
900 current_array_type = null;
902 foreach (VariableDeclaration var in (ArrayList) $4){
903 Field field = new Field (current_class, type, mod, var.identifier,
904 (Attributes) $1, var.Location);
906 field.Initializer = var.expression_or_array_initializer;
908 if (RootContext.Documentation != null) {
909 field.DocComment = Lexer.consume_doc_comment ();
910 Lexer.doc_state = XmlCommentState.Allowed;
912 current_container.AddField (field);
913 $$ = field; // FIXME: might be better if it points to the top item
920 fixed_variable_declarators
923 Expression type = (Expression) $4;
926 current_array_type = null;
928 foreach (VariableDeclaration var in (ArrayList) $5) {
929 FixedField field = new FixedField (current_class, type, mod, var.identifier,
930 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
932 if (RootContext.Documentation != null) {
933 field.DocComment = Lexer.consume_doc_comment ();
934 Lexer.doc_state = XmlCommentState.Allowed;
936 current_container.AddField (field);
937 $$ = field; // FIXME: might be better if it points to the top item
945 current_array_type = null;
946 Report.Error (670, (Location) $3, "Fields cannot have void type");
950 fixed_variable_declarators
951 : fixed_variable_declarator
953 ArrayList decl = new ArrayList (2);
957 | fixed_variable_declarators COMMA fixed_variable_declarator
959 ArrayList decls = (ArrayList) $1;
965 fixed_variable_declarator
966 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
968 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
970 | IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
972 Report.Error (443, lexer.Location, "Value or constant expected");
973 $$ = new VariableDeclaration ((LocatedToken) $1, null);
978 : variable_declarator
980 ArrayList decl = new ArrayList (4);
985 | variable_declarators COMMA variable_declarator
987 ArrayList decls = (ArrayList) $1;
994 : IDENTIFIER ASSIGN variable_initializer
996 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1000 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1002 | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
1004 Report.Error (650, ((LocatedToken) $1).Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
1005 "To declare a fixed size buffer field, use the fixed keyword before the field type");
1010 variable_initializer
1019 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
1021 $$ = new StackAlloc ((Expression) $2, (Expression) $4, (Location) $1);
1025 $$ = new ArglistAccess ((Location) $1);
1029 Report.Error (1575, (Location) $1, "A stackalloc expression requires [] after type");
1036 iterator_container = (IIteratorContainer) $1;
1037 if (RootContext.Documentation != null)
1038 Lexer.doc_state = XmlCommentState.NotAllowed;
1042 Method method = (Method) $1;
1043 method.Block = (ToplevelBlock) $3;
1044 current_container.AddMethod (method);
1046 current_local_parameters = null;
1047 iterator_container = null;
1049 if (RootContext.Documentation != null)
1050 Lexer.doc_state = XmlCommentState.Allowed;
1063 Report.Error (1585, lexer.Location,
1064 "Member modifier `{0}' must precede the member type and name",
1065 Modifiers.Name (i));
1076 type namespace_or_type_name
1077 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1079 MemberName name = (MemberName) $4;
1081 Method method = new Method (current_class, (Expression) $3, (int) $2,
1082 false, name, (Parameters) $6, (Attributes) $1);
1084 current_local_parameters = (Parameters) $6;
1086 if (RootContext.Documentation != null)
1087 method.DocComment = Lexer.consume_doc_comment ();
1093 VOID namespace_or_type_name
1094 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1096 MemberName name = (MemberName) $4;
1098 Method method = new Method (current_class, TypeManager.system_void_expr,
1099 (int) $2, false, name, (Parameters) $6,
1102 current_local_parameters = (Parameters) $6;
1104 if (RootContext.Documentation != null)
1105 method.DocComment = Lexer.consume_doc_comment ();
1112 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1114 MemberName name = (MemberName) $5;
1115 Report.Error (1585, name.Location,
1116 "Member modifier `{0}' must precede the member type and name", Modifiers.Name ((int) $4));
1118 Method method = new Method (current_class, TypeManager.system_void_expr,
1119 0, false, name, (Parameters) $7, (Attributes) $1);
1121 current_local_parameters = (Parameters) $7;
1123 if (RootContext.Documentation != null)
1124 method.DocComment = Lexer.consume_doc_comment ();
1132 | SEMICOLON { $$ = null; }
1135 opt_formal_parameter_list
1136 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1137 | formal_parameter_list
1140 formal_parameter_list
1143 ArrayList pars_list = (ArrayList) $1;
1145 Parameter [] pars = new Parameter [pars_list.Count];
1146 pars_list.CopyTo (pars);
1148 $$ = new Parameters (pars);
1150 | fixed_parameters COMMA parameter_array
1152 ArrayList pars_list = (ArrayList) $1;
1155 Parameter [] pars = new Parameter [pars_list.Count];
1156 pars_list.CopyTo (pars);
1158 $$ = new Parameters (pars);
1160 | fixed_parameters COMMA ARGLIST
1162 ArrayList pars_list = (ArrayList) $1;
1163 //pars_list.Add (new ArglistParameter (GetLocation ($3)));
1165 Parameter [] pars = new Parameter [pars_list.Count];
1166 pars_list.CopyTo (pars);
1168 $$ = new Parameters (pars, true);
1170 | parameter_array COMMA error
1173 Report.Error (231, ((Parameter) $1).Location, "A params parameter must be the last parameter in a formal parameter list");
1176 | fixed_parameters COMMA parameter_array COMMA error
1179 Report.Error (231, ((Parameter) $3).Location, "A params parameter must be the last parameter in a formal parameter list");
1182 | ARGLIST COMMA error
1184 Report.Error (257, (Location) $1, "An __arglist parameter must be the last parameter in a formal parameter list");
1187 | fixed_parameters COMMA ARGLIST COMMA error
1189 Report.Error (257, (Location) $3, "An __arglist parameter must be the last parameter in a formal parameter list");
1194 $$ = new Parameters (new Parameter[] { (Parameter) $1 } );
1198 $$ = new Parameters (new Parameter[0], true);
1205 ArrayList pars = new ArrayList (4);
1210 | fixed_parameters COMMA fixed_parameter
1212 ArrayList pars = (ArrayList) $1;
1221 opt_parameter_modifier
1225 LocatedToken lt = (LocatedToken) $4;
1226 $$ = new Parameter ((Expression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1229 opt_parameter_modifier
1231 IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1233 LocatedToken lt = (LocatedToken) $4;
1234 Report.Error (1552, lt.Location, "Array type specifier, [], must appear before parameter name");
1238 opt_parameter_modifier
1241 Report.Error (1001, GetLocation ($3), "Identifier expected");
1245 opt_parameter_modifier
1248 CheckIdentifierToken (yyToken, GetLocation ($4));
1252 opt_parameter_modifier
1258 LocatedToken lt = (LocatedToken) $4;
1259 Report.Error (241, lt.Location, "Default parameter specifiers are not permitted");
1264 opt_parameter_modifier
1265 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1266 | parameter_modifier
1270 : REF { $$ = Parameter.Modifier.REF; }
1271 | OUT { $$ = Parameter.Modifier.OUT; }
1275 : opt_attributes PARAMS type IDENTIFIER
1277 LocatedToken lt = (LocatedToken) $4;
1278 $$ = new ParamsParameter ((Expression) $3, lt.Value, (Attributes) $1, lt.Location);
1279 note ("type must be a single-dimension array type");
1281 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1283 Report.Error (1611, (Location) $2, "The params parameter cannot be declared as ref or out");
1286 | opt_attributes PARAMS type error {
1287 CheckIdentifierToken (yyToken, GetLocation ($4));
1292 property_declaration
1296 namespace_or_type_name
1298 if (RootContext.Documentation != null)
1299 tmpComment = Lexer.consume_doc_comment ();
1303 implicit_value_parameter_type = (Expression) $3;
1305 lexer.PropertyParsing = true;
1307 accessor_declarations
1309 lexer.PropertyParsing = false;
1310 has_get = has_set = false;
1318 Pair pair = (Pair) $8;
1319 Accessor get_block = (Accessor) pair.First;
1320 Accessor set_block = (Accessor) pair.Second;
1322 MemberName name = (MemberName) $4;
1324 prop = new Property (current_class, (Expression) $3, (int) $2,
1325 false, name, (Attributes) $1, get_block, set_block);
1327 current_container.AddProperty (prop);
1328 implicit_value_parameter_type = null;
1330 if (RootContext.Documentation != null)
1331 prop.DocComment = ConsumeStoredComment ();
1336 accessor_declarations
1337 : get_accessor_declaration
1339 $$ = new Pair ($1, null);
1341 | get_accessor_declaration accessor_declarations
1343 Pair pair = (Pair) $2;
1347 | set_accessor_declaration
1349 $$ = new Pair (null, $1);
1351 | set_accessor_declaration accessor_declarations
1353 Pair pair = (Pair) $2;
1359 Report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1364 get_accessor_declaration
1365 : opt_attributes opt_modifiers GET
1367 // If this is not the case, then current_local_parameters has already
1368 // been set in indexer_declaration
1369 if (parsing_indexer == false)
1370 current_local_parameters = null;
1372 current_local_parameters = indexer_parameters;
1373 lexer.PropertyParsing = false;
1375 iterator_container = SimpleIteratorContainer.GetSimple ();
1380 Report.Error (1007, (Location) $3, "Property accessor already defined");
1383 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1385 current_local_parameters = null;
1386 lexer.PropertyParsing = true;
1388 if (SimpleIteratorContainer.Simple.Yields)
1389 accessor.SetYields ();
1391 iterator_container = null;
1393 if (RootContext.Documentation != null)
1394 if (Lexer.doc_state == XmlCommentState.Error)
1395 Lexer.doc_state = XmlCommentState.NotAllowed;
1401 set_accessor_declaration
1402 : opt_attributes opt_modifiers SET
1405 Parameter implicit_value_parameter = new Parameter (
1406 implicit_value_parameter_type, "value",
1407 Parameter.Modifier.NONE, null, (Location) $3);
1409 if (parsing_indexer == false) {
1410 args = new Parameter [1];
1411 args [0] = implicit_value_parameter;
1412 current_local_parameters = new Parameters (args);
1414 Parameter [] fpars = indexer_parameters.FixedParameters;
1417 int count = fpars.Length;
1419 args = new Parameter [count + 1];
1420 fpars.CopyTo (args, 0);
1421 args [count] = implicit_value_parameter;
1424 current_local_parameters = new Parameters (
1428 lexer.PropertyParsing = false;
1430 iterator_container = SimpleIteratorContainer.GetSimple ();
1435 Report.Error (1007, ((LocatedToken) $3).Location, "Property accessor already defined");
1438 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1440 current_local_parameters = null;
1441 lexer.PropertyParsing = true;
1443 if (SimpleIteratorContainer.Simple.Yields)
1444 accessor.SetYields ();
1446 iterator_container = null;
1448 if (RootContext.Documentation != null
1449 && Lexer.doc_state == XmlCommentState.Error)
1450 Lexer.doc_state = XmlCommentState.NotAllowed;
1458 | SEMICOLON { $$ = null; }
1461 interface_declaration
1465 INTERFACE member_name
1467 MemberName name = MakeName ((MemberName) $5);
1469 push_current_class (new Interface (
1470 current_namespace, current_class, name, (int) $2,
1471 (Attributes) $1), true, $3);
1475 current_container.AddBasesForPart (current_class, (ArrayList) $7);
1477 if (RootContext.Documentation != null) {
1478 current_container.DocComment = Lexer.consume_doc_comment ();
1479 Lexer.doc_state = XmlCommentState.Allowed;
1484 if (RootContext.Documentation != null)
1485 Lexer.doc_state = XmlCommentState.Allowed;
1489 $$ = pop_current_class ();
1491 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1492 CheckIdentifierToken (yyToken, GetLocation ($5));
1498 opt_interface_member_declarations
1502 opt_interface_member_declarations
1504 | interface_member_declarations
1507 interface_member_declarations
1508 : interface_member_declaration
1509 | interface_member_declarations interface_member_declaration
1512 interface_member_declaration
1513 : interface_method_declaration
1518 Method m = (Method) $1;
1520 if (m.IsExplicitImpl)
1521 Report.Error (541, m.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1522 m.GetSignatureForError ());
1524 current_container.AddMethod (m);
1526 if (RootContext.Documentation != null)
1527 Lexer.doc_state = XmlCommentState.Allowed;
1529 | interface_property_declaration
1534 Property p = (Property) $1;
1536 if (p.IsExplicitImpl)
1537 Report.Error (541, p.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1538 p.GetSignatureForError ());
1540 current_container.AddProperty (p);
1542 if (RootContext.Documentation != null)
1543 Lexer.doc_state = XmlCommentState.Allowed;
1545 | interface_event_declaration
1548 Event e = (Event) $1;
1550 if (e.IsExplicitImpl)
1551 Report.Error (541, e.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1552 e.GetSignatureForError ());
1554 current_container.AddEvent (e);
1557 if (RootContext.Documentation != null)
1558 Lexer.doc_state = XmlCommentState.Allowed;
1560 | interface_indexer_declaration
1565 Indexer i = (Indexer) $1;
1567 if (i.IsExplicitImpl)
1568 Report.Error (541, i.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1569 i.GetSignatureForError ());
1571 current_container.AddIndexer (i);
1573 if (RootContext.Documentation != null)
1574 Lexer.doc_state = XmlCommentState.Allowed;
1576 | delegate_declaration
1579 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1580 ((MemberCore)$1).GetSignatureForError ());
1586 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1587 ((MemberCore)$1).GetSignatureForError ());
1590 | struct_declaration
1593 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1594 ((MemberCore)$1).GetSignatureForError ());
1600 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1601 ((MemberCore)$1).GetSignatureForError ());
1604 | interface_declaration
1607 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1608 ((MemberCore)$1).GetSignatureForError ());
1611 | constant_declaration
1613 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1621 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, GetLocation ($1));
1626 interface_method_declaration
1627 : opt_attributes opt_new type namespace_or_type_name
1628 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1631 $$ = new Method (current_class, (Expression) $3, (int) $2, true,
1632 (MemberName) $4, (Parameters) $6, (Attributes) $1);
1633 if (RootContext.Documentation != null)
1634 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1636 | opt_attributes opt_new type namespace_or_type_name
1637 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1638 OPEN_BRACE opt_statement_list CLOSE_BRACE
1640 $$ = new Method (current_class, (Expression) $3, (int) $2, true,
1641 (MemberName) $4, (Parameters) $6, (Attributes) $1);
1643 Report.Error (531, lexer.Location, "`{0}': interface members cannot have a definition",
1644 ((Method)$$).GetSignatureForError ());
1648 interface_property_declaration
1653 { lexer.PropertyParsing = true; }
1654 accessor_declarations
1656 has_get = has_set = false;
1657 lexer.PropertyParsing = false;
1661 LocatedToken lt = (LocatedToken) $4;
1662 MemberName name = new MemberName (lt.Value, lt.Location);
1664 if ($3 == TypeManager.system_void_expr) {
1665 Report.Error (547, lt.Location, "`{0}': property or indexer cannot have void type", lt.Value);
1671 p = new Property (current_class, (Expression) $3, (int) $2, true,
1672 name, (Attributes) $1,
1675 Report.Error (548, p.Location, "`{0}': property or indexer must have at least one accessor", p.GetSignatureForError ());
1679 Pair pair = (Pair) $7;
1680 p = new Property (current_class, (Expression) $3, (int) $2, true,
1681 name, (Attributes) $1,
1682 (Accessor)pair.First, (Accessor)pair.Second);
1684 if (pair.First != null && ((Accessor)(pair.First)).Block != null) {
1685 Report.Error (531, p.Location, "`{0}.get': interface members cannot have a definition", p.GetSignatureForError ());
1690 if (pair.Second != null && ((Accessor)(pair.Second)).Block != null) {
1691 Report.Error (531, p.Location, "`{0}.set': interface members cannot have a definition", p.GetSignatureForError ());
1696 if (RootContext.Documentation != null)
1697 p.DocComment = Lexer.consume_doc_comment ();
1704 CheckIdentifierToken (yyToken, GetLocation ($4));
1710 interface_event_declaration
1711 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1713 LocatedToken lt = (LocatedToken) $5;
1714 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1715 new MemberName (lt.Value, lt.Location),
1717 if (RootContext.Documentation != null)
1718 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1720 | opt_attributes opt_new EVENT type error {
1721 CheckIdentifierToken (yyToken, GetLocation ($5));
1724 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1725 LocatedToken lt = (LocatedToken) $5;
1726 Report.Error (68, lt.Location, "`{0}.{1}': event in interface cannot have initializer", current_container.Name, lt.Value);
1729 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1731 lexer.EventParsing = true;
1733 event_accessor_declarations
1735 lexer.EventParsing = false;
1738 Report.Error (69, (Location) $3, "Event in interface cannot have add or remove accessors");
1743 interface_indexer_declaration
1744 : opt_attributes opt_new type THIS
1745 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1747 { lexer.PropertyParsing = true; }
1748 accessor_declarations
1750 has_get = has_set = false;
1751 lexer.PropertyParsing = false;
1757 i = new Indexer (current_class, (Expression) $3,
1758 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1759 (int) $2, true, (Parameters) $6, (Attributes) $1,
1762 Report.Error (548, i.Location, "`{0}': property or indexer must have at least one accessor", i.GetSignatureForError ());
1766 Pair pair = (Pair) $10;
1767 i = new Indexer (current_class, (Expression) $3,
1768 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1769 (int) $2, true, (Parameters) $6, (Attributes) $1,
1770 (Accessor)pair.First, (Accessor)pair.Second);
1772 if (pair.First != null && ((Accessor)(pair.First)).Block != null) {
1773 Report.Error (531, i.Location, "`{0}.get': interface members cannot have a definition", i.GetSignatureForError ());
1778 if (pair.Second != null && ((Accessor)(pair.Second)).Block != null) {
1779 Report.Error (531, i.Location, "`{0}.set': interface members cannot have a definition", i.GetSignatureForError ());
1784 if (RootContext.Documentation != null)
1785 i.DocComment = ConsumeStoredComment ();
1791 operator_declaration
1792 : opt_attributes opt_modifiers operator_declarator
1794 iterator_container = SimpleIteratorContainer.GetSimple ();
1801 OperatorDeclaration decl = (OperatorDeclaration) $3;
1803 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1805 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null, decl.location);
1806 if (decl.arg2type != null)
1807 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null, decl.location);
1809 Operator op = new Operator (
1810 current_class, decl.optype, decl.ret_type, (int) $2,
1811 new Parameters (param_list),
1812 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1814 if (RootContext.Documentation != null) {
1815 op.DocComment = tmpComment;
1816 Lexer.doc_state = XmlCommentState.Allowed;
1819 if (SimpleIteratorContainer.Simple.Yields)
1822 // Note again, checking is done in semantic analysis
1823 current_container.AddOperator (op);
1825 current_local_parameters = null;
1826 iterator_container = null;
1832 | SEMICOLON { $$ = null; }
1835 : type OPERATOR overloadable_operator
1836 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1838 LocatedToken lt = (LocatedToken) $6;
1839 Operator.OpType op = (Operator.OpType) $3;
1840 CheckUnaryOperator (op, lt.Location);
1842 if (op == Operator.OpType.Addition)
1843 op = Operator.OpType.UnaryPlus;
1845 if (op == Operator.OpType.Subtraction)
1846 op = Operator.OpType.UnaryNegation;
1848 Parameter [] pars = new Parameter [1];
1849 Expression type = (Expression) $5;
1851 pars [0] = new Parameter (type, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1853 current_local_parameters = new Parameters (pars);
1855 if (RootContext.Documentation != null) {
1856 tmpComment = Lexer.consume_doc_comment ();
1857 Lexer.doc_state = XmlCommentState.NotAllowed;
1860 $$ = new OperatorDeclaration (op, (Expression) $1, type, lt.Value,
1861 null, null, (Location) $2);
1863 | type OPERATOR overloadable_operator
1865 type IDENTIFIER COMMA
1869 LocatedToken ltParam1 = (LocatedToken) $6;
1870 LocatedToken ltParam2 = (LocatedToken) $9;
1871 CheckBinaryOperator ((Operator.OpType) $3, (Location) $2);
1873 Parameter [] pars = new Parameter [2];
1875 Expression typeL = (Expression) $5;
1876 Expression typeR = (Expression) $8;
1878 pars [0] = new Parameter (typeL, ltParam1.Value, Parameter.Modifier.NONE, null, ltParam1.Location);
1879 pars [1] = new Parameter (typeR, ltParam2.Value, Parameter.Modifier.NONE, null, ltParam2.Location);
1881 current_local_parameters = new Parameters (pars);
1883 if (RootContext.Documentation != null) {
1884 tmpComment = Lexer.consume_doc_comment ();
1885 Lexer.doc_state = XmlCommentState.NotAllowed;
1888 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1889 typeL, ltParam1.Value,
1890 typeR, ltParam2.Value, (Location) $2);
1892 | conversion_operator_declarator
1893 | type OPERATOR overloadable_operator
1895 type IDENTIFIER COMMA
1896 type IDENTIFIER COMMA
1900 Report.Error (1534, (Location) $2, "Overloaded binary operator `{0}' takes two parameters",
1901 Operator.GetName ((Operator.OpType) $3));
1904 | type OPERATOR overloadable_operator
1905 OPEN_PARENS CLOSE_PARENS
1907 Report.Error (1535, (Location) $2, "Overloaded unary operator `{0}' takes one parameter",
1908 Operator.GetName ((Operator.OpType) $3));
1913 overloadable_operator
1915 : BANG { $$ = Operator.OpType.LogicalNot; }
1916 | TILDE { $$ = Operator.OpType.OnesComplement; }
1917 | OP_INC { $$ = Operator.OpType.Increment; }
1918 | OP_DEC { $$ = Operator.OpType.Decrement; }
1919 | TRUE { $$ = Operator.OpType.True; }
1920 | FALSE { $$ = Operator.OpType.False; }
1921 // Unary and binary:
1922 | PLUS { $$ = Operator.OpType.Addition; }
1923 | MINUS { $$ = Operator.OpType.Subtraction; }
1925 | STAR { $$ = Operator.OpType.Multiply; }
1926 | DIV { $$ = Operator.OpType.Division; }
1927 | PERCENT { $$ = Operator.OpType.Modulus; }
1928 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1929 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1930 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1931 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1932 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1933 | OP_EQ { $$ = Operator.OpType.Equality; }
1934 | OP_NE { $$ = Operator.OpType.Inequality; }
1935 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1936 | OP_LT { $$ = Operator.OpType.LessThan; }
1937 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1938 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1941 conversion_operator_declarator
1942 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1944 LocatedToken lt = (LocatedToken) $6;
1945 Parameter [] pars = new Parameter [1];
1947 pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1949 current_local_parameters = new Parameters (pars);
1951 if (RootContext.Documentation != null) {
1952 tmpComment = Lexer.consume_doc_comment ();
1953 Lexer.doc_state = XmlCommentState.NotAllowed;
1956 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, lt.Value,
1957 null, null, (Location) $2);
1959 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1961 LocatedToken lt = (LocatedToken) $6;
1962 Parameter [] pars = new Parameter [1];
1964 pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1966 current_local_parameters = new Parameters (pars);
1968 if (RootContext.Documentation != null) {
1969 tmpComment = Lexer.consume_doc_comment ();
1970 Lexer.doc_state = XmlCommentState.NotAllowed;
1973 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, lt.Value,
1974 null, null, (Location) $2);
1978 syntax_error ((Location) $1, "'operator' expected");
1982 syntax_error ((Location) $1, "'operator' expected");
1986 constructor_declaration
1989 constructor_declarator
1992 Constructor c = (Constructor) $3;
1993 c.Block = (ToplevelBlock) $4;
1994 c.OptAttributes = (Attributes) $1;
1995 c.ModFlags = (int) $2;
1997 if (RootContext.Documentation != null)
1998 c.DocComment = ConsumeStoredComment ();
2000 if (c.Name == current_container.Basename){
2001 if ((c.ModFlags & Modifiers.STATIC) != 0){
2002 if ((c.ModFlags & Modifiers.Accessibility) != 0){
2003 Report.Error (515, c.Location,
2004 "`{0}': access modifiers are not allowed on static constructors",
2005 c.GetSignatureForError ());
2008 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2010 if (c.Initializer != null){
2011 Report.Error (514, c.Location,
2012 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2013 c.GetSignatureForError ());
2016 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2019 // We let another layer check the validity of the constructor.
2020 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
2023 current_container.AddConstructor (c);
2025 current_local_parameters = null;
2026 if (RootContext.Documentation != null)
2027 Lexer.doc_state = XmlCommentState.Allowed;
2031 constructor_declarator
2034 if (RootContext.Documentation != null) {
2035 tmpComment = Lexer.consume_doc_comment ();
2036 Lexer.doc_state = XmlCommentState.Allowed;
2039 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2041 current_local_parameters = (Parameters) $4;
2043 opt_constructor_initializer
2045 LocatedToken lt = (LocatedToken) $1;
2046 $$ = new Constructor (current_class, lt.Value, 0, (Parameters) $4,
2047 (ConstructorInitializer) $7, lt.Location);
2053 | SEMICOLON { $$ = null; }
2056 opt_constructor_initializer
2057 : /* empty */ { $$ = null; }
2058 | constructor_initializer
2061 constructor_initializer
2062 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
2064 $$ = new ConstructorBaseInitializer ((ArrayList) $4, (Location) $2);
2066 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
2068 $$ = new ConstructorThisInitializer ((ArrayList) $4, (Location) $2);
2071 Report.Error (1018, (Location) $1, "Keyword this or base expected");
2077 : /* EMPTY */ { $$ = 0; }
2078 | UNSAFE { $$ = Modifiers.UNSAFE; }
2079 | EXTERN { $$ = Modifiers.EXTERN; }
2082 destructor_declaration
2083 : opt_attributes opt_finalizer TILDE
2085 if (RootContext.Documentation != null) {
2086 tmpComment = Lexer.consume_doc_comment ();
2087 Lexer.doc_state = XmlCommentState.NotAllowed;
2090 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2092 LocatedToken lt = (LocatedToken) $5;
2093 if (lt.Value != current_container.Basename){
2094 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2095 } else if (current_container.Kind != Kind.Class){
2096 Report.Error (575, lt.Location, "Only class types can contain destructor");
2098 Location l = lt.Location;
2101 if (!RootContext.StdLib && current_container.Name == "System.Object")
2102 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2104 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2106 Method d = new Destructor (
2107 current_class, TypeManager.system_void_expr, m, "Finalize",
2108 Parameters.EmptyReadOnlyParameters, (Attributes) $1, l);
2109 if (RootContext.Documentation != null)
2110 d.DocComment = ConsumeStoredComment ();
2112 d.Block = (ToplevelBlock) $8;
2113 current_container.AddMethod (d);
2121 EVENT type variable_declarators SEMICOLON
2123 current_array_type = null;
2124 foreach (VariableDeclaration var in (ArrayList) $5) {
2126 MemberName name = new MemberName (var.identifier,
2129 EventField e = new EventField (
2130 current_class, (Expression) $4, (int) $2, false, name,
2133 e.Initializer = var.expression_or_array_initializer;
2135 current_container.AddEvent (e);
2137 if (RootContext.Documentation != null) {
2138 e.DocComment = Lexer.consume_doc_comment ();
2139 Lexer.doc_state = XmlCommentState.Allowed;
2145 EVENT type namespace_or_type_name
2148 implicit_value_parameter_type = (Expression) $4;
2149 lexer.EventParsing = true;
2151 event_accessor_declarations
2153 lexer.EventParsing = false;
2157 MemberName name = (MemberName) $5;
2160 Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2161 current_container.Name, name.ToString ());
2164 Pair pair = (Pair) $8;
2165 if (pair.First == null || pair.Second == null)
2166 // CS0073 is already reported, so no CS0065 here.
2169 Event e = new EventProperty (
2170 current_class, (Expression) $4, (int) $2, false, name,
2171 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second);
2172 if (RootContext.Documentation != null) {
2173 e.DocComment = Lexer.consume_doc_comment ();
2174 Lexer.doc_state = XmlCommentState.Allowed;
2177 current_container.AddEvent (e);
2178 implicit_value_parameter_type = null;
2182 | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
2183 MemberName mn = (MemberName) $5;
2185 if (mn.Left != null)
2186 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2188 Report.Error (71, mn.Location, "Event declaration should use property syntax");
2190 if (RootContext.Documentation != null)
2191 Lexer.doc_state = XmlCommentState.Allowed;
2195 event_accessor_declarations
2196 : add_accessor_declaration remove_accessor_declaration
2198 $$ = new Pair ($1, $2);
2200 | remove_accessor_declaration add_accessor_declaration
2202 $$ = new Pair ($2, $1);
2204 | add_accessor_declaration { $$ = null; }
2205 | remove_accessor_declaration { $$ = null; }
2208 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2214 add_accessor_declaration
2215 : opt_attributes ADD
2217 Parameter [] args = new Parameter [1];
2218 Parameter implicit_value_parameter = new Parameter (
2219 implicit_value_parameter_type, "value",
2220 Parameter.Modifier.NONE, null, (Location) $2);
2222 args [0] = implicit_value_parameter;
2224 current_local_parameters = new Parameters (args);
2225 lexer.EventParsing = false;
2229 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2230 lexer.EventParsing = true;
2232 | opt_attributes ADD error {
2233 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2236 | opt_attributes modifiers ADD {
2237 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2242 remove_accessor_declaration
2243 : opt_attributes REMOVE
2245 Parameter [] args = new Parameter [1];
2246 Parameter implicit_value_parameter = new Parameter (
2247 implicit_value_parameter_type, "value",
2248 Parameter.Modifier.NONE, null, (Location) $2);
2250 args [0] = implicit_value_parameter;
2252 current_local_parameters = new Parameters (args);
2253 lexer.EventParsing = false;
2257 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2258 lexer.EventParsing = true;
2260 | opt_attributes REMOVE error {
2261 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2264 | opt_attributes modifiers REMOVE {
2265 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2271 : opt_attributes opt_modifiers indexer_declarator
2274 IndexerDeclaration decl = (IndexerDeclaration) $3;
2276 implicit_value_parameter_type = decl.type;
2278 lexer.PropertyParsing = true;
2279 parsing_indexer = true;
2281 indexer_parameters = decl.param_list;
2283 accessor_declarations
2285 lexer.PropertyParsing = false;
2286 has_get = has_set = false;
2287 parsing_indexer = false;
2294 // The signature is computed from the signature of the indexer. Look
2295 // at section 3.6 on the spec
2297 IndexerDeclaration decl = (IndexerDeclaration) $3;
2298 Location loc = decl.location;
2299 Pair pair = (Pair) $6;
2300 Accessor get_block = (Accessor) pair.First;
2301 Accessor set_block = (Accessor) pair.Second;
2304 if (decl.interface_type != null)
2305 name = new MemberName (decl.interface_type, TypeContainer.DefaultIndexerName, loc);
2307 name = new MemberName (TypeContainer.DefaultIndexerName, loc);
2309 indexer = new Indexer (current_class, decl.type, name,
2310 (int) $2, false, decl.param_list, (Attributes) $1,
2311 get_block, set_block);
2313 if (RootContext.Documentation != null)
2314 indexer.DocComment = ConsumeStoredComment ();
2316 current_container.AddIndexer (indexer);
2318 current_local_parameters = null;
2319 implicit_value_parameter_type = null;
2320 indexer_parameters = null;
2325 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2327 Parameters pars = (Parameters) $4;
2328 if (pars.HasArglist) {
2329 // "__arglist is not valid in this context"
2330 Report.Error (1669, (Location) $2, "__arglist is not valid in this context");
2331 } else if (pars.Empty){
2332 Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
2334 if (RootContext.Documentation != null) {
2335 tmpComment = Lexer.consume_doc_comment ();
2336 Lexer.doc_state = XmlCommentState.Allowed;
2339 $$ = new IndexerDeclaration ((Expression) $1, null, pars, (Location) $2);
2341 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2343 Parameters pars = (Parameters) $6;
2345 if (pars.HasArglist) {
2346 // "__arglist is not valid in this context"
2347 Report.Error (1669, (Location) $4, "__arglist is not valid in this context");
2348 } else if (pars.Empty){
2349 Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
2352 MemberName name = (MemberName) $2;
2353 $$ = new IndexerDeclaration ((Expression) $1, name, pars, (Location) $4);
2355 if (RootContext.Documentation != null) {
2356 tmpComment = Lexer.consume_doc_comment ();
2357 Lexer.doc_state = XmlCommentState.Allowed;
2368 if (RootContext.Documentation != null)
2369 enumTypeComment = Lexer.consume_doc_comment ();
2374 LocatedToken lt = (LocatedToken) $5;
2375 Location enum_location = lt.Location;
2378 Report.Error (267, lt.Location, "The partial modifier can only appear immediately before `class', `struct' or `interface'");
2379 break; // assumes that the parser put us in a switch
2382 MemberName name = MakeName (new MemberName (lt.Value, enum_location));
2383 Enum e = new Enum (current_namespace, current_class, (Expression) $6, (int) $2,
2384 name, (Attributes) $1);
2386 if (RootContext.Documentation != null)
2387 e.DocComment = enumTypeComment;
2390 EnumMember em = null;
2391 foreach (VariableDeclaration ev in (ArrayList) $8) {
2392 em = new EnumMember (e, em, (Expression) ev.expression_or_array_initializer,
2393 new MemberName (ev.identifier, ev.Location), ev.OptAttributes);
2395 // if (RootContext.Documentation != null)
2396 em.DocComment = ev.DocComment;
2398 e.AddEnumMember (em);
2401 current_container.AddEnum (e);
2408 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2409 | COLON type { $$ = $2; }
2415 if (RootContext.Documentation != null)
2416 Lexer.doc_state = XmlCommentState.Allowed;
2418 opt_enum_member_declarations
2420 // here will be evaluated after CLOSE_BLACE is consumed.
2421 if (RootContext.Documentation != null)
2422 Lexer.doc_state = XmlCommentState.Allowed;
2430 opt_enum_member_declarations
2431 : /* empty */ { $$ = new ArrayList (4); }
2432 | enum_member_declarations opt_comma { $$ = $1; }
2435 enum_member_declarations
2436 : enum_member_declaration
2438 ArrayList l = new ArrayList (4);
2443 | enum_member_declarations COMMA enum_member_declaration
2445 ArrayList l = (ArrayList) $1;
2453 enum_member_declaration
2454 : opt_attributes IDENTIFIER
2456 VariableDeclaration vd = new VariableDeclaration (
2457 (LocatedToken) $2, null, (Attributes) $1);
2459 if (RootContext.Documentation != null) {
2460 vd.DocComment = Lexer.consume_doc_comment ();
2461 Lexer.doc_state = XmlCommentState.Allowed;
2466 | opt_attributes IDENTIFIER
2468 if (RootContext.Documentation != null) {
2469 tmpComment = Lexer.consume_doc_comment ();
2470 Lexer.doc_state = XmlCommentState.NotAllowed;
2475 VariableDeclaration vd = new VariableDeclaration (
2476 (LocatedToken) $2, $5, (Attributes) $1);
2478 if (RootContext.Documentation != null)
2479 vd.DocComment = ConsumeStoredComment ();
2485 delegate_declaration
2488 DELEGATE type member_name
2489 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2492 MemberName name = MakeName ((MemberName) $5);
2493 Parameters p = (Parameters) $7;
2495 // TODO: wrong location
2496 Report.Error (1669, name.Location, "__arglist is not valid in this context");
2499 Delegate del = new Delegate (current_namespace, current_class, (Expression) $4,
2500 (int) $2, name, p, (Attributes) $1);
2502 if (RootContext.Documentation != null) {
2503 del.DocComment = Lexer.consume_doc_comment ();
2504 Lexer.doc_state = XmlCommentState.Allowed;
2507 current_container.AddDelegate (del);
2512 namespace_or_type_name
2514 | IDENTIFIER DOUBLE_COLON IDENTIFIER {
2515 LocatedToken lt1 = (LocatedToken) $1;
2516 LocatedToken lt2 = (LocatedToken) $3;
2517 $$ = new MemberName (lt1.Value, lt2.Value, lt2.Location);
2519 | namespace_or_type_name DOT IDENTIFIER {
2520 LocatedToken lt = (LocatedToken) $3;
2521 $$ = new MemberName ((MemberName) $1, lt.Value);
2527 LocatedToken lt = (LocatedToken) $1;
2528 $$ = new MemberName (lt.Value, lt.Location);
2533 * Before you think of adding a return_type, notice that we have been
2534 * using two rules in the places where it matters (one rule using type
2535 * and another identical one that uses VOID as the return type). This
2536 * gets rid of a shift/reduce couple
2539 : namespace_or_type_name
2541 MemberName name = (MemberName) $1;
2542 $$ = name.GetTypeExpression ();
2554 // Note that here only unmanaged types are allowed but we
2555 // can't perform checks during this phase - we do it during
2556 // semantic analysis.
2558 $$ = new ComposedCast ((Expression) $1, "*", Lexer.Location);
2562 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2568 | non_expression_type rank_specifier
2570 Location loc = GetLocation ($1);
2572 loc = lexer.Location;
2573 $$ = new ComposedCast ((Expression) $1, (string) $2, loc);
2575 | non_expression_type STAR
2577 Location loc = GetLocation ($1);
2579 loc = lexer.Location;
2580 $$ = new ComposedCast ((Expression) $1, "*", loc);
2582 | expression rank_specifiers
2584 $$ = new ComposedCast ((Expression) $1, (string) $2);
2588 $$ = new ComposedCast ((Expression) $1, "*");
2592 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2593 // through this different path
2595 | multiplicative_expression STAR
2597 $$ = new ComposedCast ((Expression) $1, "*");
2604 ArrayList types = new ArrayList (4);
2609 | type_list COMMA type
2611 ArrayList types = (ArrayList) $1;
2619 * replaces all the productions for isolating the various
2620 * simple types, but we need this to reuse it easily in local_variable_type
2623 : OBJECT { $$ = TypeManager.system_object_expr; }
2624 | STRING { $$ = TypeManager.system_string_expr; }
2625 | BOOL { $$ = TypeManager.system_boolean_expr; }
2626 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2627 | FLOAT { $$ = TypeManager.system_single_expr; }
2628 | DOUBLE { $$ = TypeManager.system_double_expr; }
2633 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2634 | BYTE { $$ = TypeManager.system_byte_expr; }
2635 | SHORT { $$ = TypeManager.system_int16_expr; }
2636 | USHORT { $$ = TypeManager.system_uint16_expr; }
2637 | INT { $$ = TypeManager.system_int32_expr; }
2638 | UINT { $$ = TypeManager.system_uint32_expr; }
2639 | LONG { $$ = TypeManager.system_int64_expr; }
2640 | ULONG { $$ = TypeManager.system_uint64_expr; }
2641 | CHAR { $$ = TypeManager.system_char_expr; }
2642 | VOID { $$ = TypeManager.system_void_expr; }
2646 : type rank_specifiers
2648 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2);
2653 // Expressions, section 7.5
2663 MemberName mn = (MemberName) $1;
2664 $$ = mn.GetTypeExpression ();
2666 | IDENTIFIER DOUBLE_COLON IDENTIFIER
2668 LocatedToken lt1 = (LocatedToken) $1;
2669 LocatedToken lt2 = (LocatedToken) $3;
2670 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, lt2.Location);
2672 | parenthesized_expression
2674 | invocation_expression
2678 | post_increment_expression
2679 | post_decrement_expression
2683 | checked_expression
2684 | unchecked_expression
2685 | pointer_member_access
2686 | anonymous_method_expression
2693 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
2694 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); }
2695 | NULL { $$ = new NullLiteral (lexer.Location); }
2699 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
2700 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
2701 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
2706 object v = lexer.Value;
2709 $$ = new IntLiteral ((int) v, lexer.Location);
2710 } else if (v is uint)
2711 $$ = new UIntLiteral ((UInt32) v, lexer.Location);
2713 $$ = new LongLiteral ((Int64) v, lexer.Location);
2714 else if (v is ulong)
2715 $$ = new ULongLiteral ((UInt64) v, lexer.Location);
2717 Console.WriteLine ("OOPS. Unexpected result from scanner");
2722 : TRUE { $$ = new BoolLiteral (true, lexer.Location); }
2723 | FALSE { $$ = new BoolLiteral (false, lexer.Location); }
2726 parenthesized_expression_0
2727 : OPEN_PARENS expression CLOSE_PARENS
2730 lexer.Deambiguate_CloseParens ();
2731 // After this, the next token returned is one of
2732 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2733 // or CLOSE_PARENS_MINUS.
2735 | OPEN_PARENS expression error { CheckToken (1026, yyToken, "Expecting ')'", lexer.Location); }
2738 parenthesized_expression
2739 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2743 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2745 // If a parenthesized expression is followed by a minus, we need to wrap
2746 // the expression inside a ParenthesizedExpression for the CS0075 check
2747 // in Binary.DoResolve().
2748 $$ = new ParenthesizedExpression ((Expression) $1);
2753 : primary_expression DOT IDENTIFIER
2755 LocatedToken lt = (LocatedToken) $3;
2756 $$ = new MemberAccess ((Expression) $1, lt.Value);
2758 | predefined_type DOT IDENTIFIER
2760 LocatedToken lt = (LocatedToken) $3;
2761 $$ = new MemberAccess ((Expression) $1, lt.Value);
2769 invocation_expression
2770 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2773 Report.Error (1, (Location) $2, "Parse error");
2775 $$ = new Invocation ((Expression) $1, (ArrayList) $3);
2777 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2779 $$ = new Invocation ((Expression) $1, new ArrayList ());
2781 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2783 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
2785 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2787 ArrayList args = new ArrayList (1);
2789 $$ = new Invocation ((Expression) $1, args);
2791 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2793 ArrayList args = ((ArrayList) $4);
2795 $$ = new Invocation ((Expression) $1, args);
2800 : /* empty */ { $$ = null; }
2807 ArrayList list = new ArrayList (4);
2811 | argument_list COMMA argument
2813 ArrayList list = (ArrayList) $1;
2817 | argument_list error {
2818 CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
2826 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2828 | non_simple_argument
2835 : REF variable_reference
2837 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2839 | OUT variable_reference
2841 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2843 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2845 ArrayList list = (ArrayList) $3;
2846 Argument[] args = new Argument [list.Count];
2847 list.CopyTo (args, 0);
2849 Expression expr = new Arglist (args, (Location) $1);
2850 $$ = new Argument (expr, Argument.AType.Expression);
2854 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
2859 : expression { note ("section 5.4"); $$ = $1; }
2863 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2865 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
2867 | primary_expression rank_specifiers
2869 // So the super-trick is that primary_expression
2870 // can only be either a SimpleName or a MemberAccess.
2871 // The MemberAccess case arises when you have a fully qualified type-name like :
2873 // SimpleName is when you have
2876 Expression expr = (Expression) $1;
2877 if (expr is ComposedCast){
2878 $$ = new ComposedCast (expr, (string) $2);
2879 } else if (!(expr is SimpleName || expr is MemberAccess || expr is QualifiedAliasMember)){
2880 Error_ExpectingTypeName (expr);
2881 $$ = TypeManager.system_object_expr;
2884 // So we extract the string corresponding to the SimpleName
2887 $$ = new ComposedCast (expr, (string) $2);
2889 current_array_type = (Expression)$$;
2896 ArrayList list = new ArrayList (4);
2900 | expression_list COMMA expression
2902 ArrayList list = (ArrayList) $1;
2911 $$ = new This (current_block, (Location) $1);
2916 : BASE DOT IDENTIFIER
2918 LocatedToken lt = (LocatedToken) $3;
2919 $$ = new BaseAccess (lt.Value, lt.Location);
2921 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2923 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
2926 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
2931 post_increment_expression
2932 : primary_expression OP_INC
2934 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2935 (Expression) $1, (Location) $2);
2939 post_decrement_expression
2940 : primary_expression OP_DEC
2942 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2943 (Expression) $1, (Location) $2);
2948 : object_or_delegate_creation_expression
2949 | array_creation_expression
2952 object_or_delegate_creation_expression
2953 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2955 $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
2959 array_creation_expression
2960 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2962 opt_array_initializer
2964 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
2966 | NEW type rank_specifiers array_initializer
2968 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, (Location) $1);
2972 Report.Error (1031, (Location) $1, "Type expected");
2977 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
2994 : rank_specifier opt_rank_specifier
2996 $$ = (string) $2 + (string) $1;
3001 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3003 $$ = "[" + (string) $2 + "]";
3023 | dim_separators COMMA
3025 $$ = (string) $1 + ",";
3029 opt_array_initializer
3041 : OPEN_BRACE CLOSE_BRACE
3043 ArrayList list = new ArrayList (4);
3046 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3048 $$ = (ArrayList) $2;
3052 variable_initializer_list
3053 : variable_initializer
3055 ArrayList list = new ArrayList (4);
3059 | variable_initializer_list COMMA variable_initializer
3061 ArrayList list = (ArrayList) $1;
3070 pushed_current_array_type = current_array_type;
3072 OPEN_PARENS type CLOSE_PARENS
3074 Expression type = (Expression)$4;
3075 if (type == TypeManager.system_void_expr)
3076 $$ = new TypeOfVoid ((Location) $1);
3078 $$ = new TypeOf (type, (Location) $1);
3079 current_array_type = pushed_current_array_type;
3084 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3085 $$ = new SizeOf ((Expression) $3, (Location) $1);
3090 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3092 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3096 unchecked_expression
3097 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3099 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3103 pointer_member_access
3104 : primary_expression OP_PTR IDENTIFIER
3107 LocatedToken lt = (LocatedToken) $3;
3109 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
3110 $$ = new MemberAccess (deref, lt.Value);
3114 anonymous_method_expression
3115 : DELEGATE opt_anonymous_method_signature
3117 if (oob_stack == null)
3118 oob_stack = new Stack (6);
3120 oob_stack.Push (current_local_parameters);
3121 current_local_parameters = (Parameters)$2;
3123 // Force the next block to be created as a ToplevelBlock
3124 oob_stack.Push (current_block);
3125 oob_stack.Push (top_current_block);
3126 current_block = null;
3130 Location loc = (Location) $1;
3131 top_current_block = (Block) oob_stack.Pop ();
3132 current_block = (Block) oob_stack.Pop ();
3133 if (RootContext.Version == LanguageVersion.ISO_1){
3134 Report.FeatureIsNotStandardized (loc, "anonymous methods");
3137 ToplevelBlock anon_block = (ToplevelBlock) $4;
3139 anon_block.Parent = current_block;
3140 $$ = new AnonymousMethod (current_container, (Parameters) $2, (ToplevelBlock) top_current_block,
3143 current_local_parameters = (Parameters) oob_stack.Pop ();
3147 opt_anonymous_method_signature
3148 : /* empty */ { $$ = null; }
3149 | anonymous_method_signature
3152 anonymous_method_signature
3153 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3156 $$ = Parameters.EmptyReadOnlyParameters;
3158 ArrayList par_list = (ArrayList) $2;
3159 Parameter [] pars = new Parameter [par_list.Count];
3160 par_list.CopyTo (pars);
3161 $$ = new Parameters (pars);
3166 opt_anonymous_method_parameter_list
3167 : /* empty */ { $$ = null; }
3168 | anonymous_method_parameter_list { $$ = $1; }
3171 anonymous_method_parameter_list
3172 : anonymous_method_parameter
3174 ArrayList a = new ArrayList (4);
3178 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3180 ArrayList a = (ArrayList) $1;
3186 anonymous_method_parameter
3187 : opt_parameter_modifier type IDENTIFIER {
3188 LocatedToken lt = (LocatedToken) $3;
3189 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3191 | PARAMS type IDENTIFIER {
3192 Report.Error (1670, ((LocatedToken) $3).Location, "The `params' modifier is not allowed in anonymous method declaration");
3198 : primary_expression
3199 | BANG prefixed_unary_expression
3201 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3203 | TILDE prefixed_unary_expression
3205 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3211 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3213 $$ = new Cast ((Expression) $1, (Expression) $3);
3215 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3217 $$ = new Cast ((Expression) $1, (Expression) $3);
3223 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3225 // TODO: wrong location
3226 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3231 // The idea to split this out is from Rhys' grammar
3232 // to solve the problem with casts.
3234 prefixed_unary_expression
3236 | PLUS prefixed_unary_expression
3238 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3240 | MINUS prefixed_unary_expression
3242 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3244 | OP_INC prefixed_unary_expression
3246 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3247 (Expression) $2, (Location) $1);
3249 | OP_DEC prefixed_unary_expression
3251 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3252 (Expression) $2, (Location) $1);
3254 | STAR prefixed_unary_expression
3256 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, (Location) $1);
3258 | BITWISE_AND prefixed_unary_expression
3260 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3264 pre_increment_expression
3265 : OP_INC prefixed_unary_expression
3267 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3268 (Expression) $2, (Location) $1);
3272 pre_decrement_expression
3273 : OP_DEC prefixed_unary_expression
3275 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3276 (Expression) $2, (Location) $1);
3280 multiplicative_expression
3281 : prefixed_unary_expression
3282 | multiplicative_expression STAR prefixed_unary_expression
3284 $$ = new Binary (Binary.Operator.Multiply,
3285 (Expression) $1, (Expression) $3);
3287 | multiplicative_expression DIV prefixed_unary_expression
3289 $$ = new Binary (Binary.Operator.Division,
3290 (Expression) $1, (Expression) $3);
3292 | multiplicative_expression PERCENT prefixed_unary_expression
3294 $$ = new Binary (Binary.Operator.Modulus,
3295 (Expression) $1, (Expression) $3);
3300 : multiplicative_expression
3301 | additive_expression PLUS multiplicative_expression
3303 $$ = new Binary (Binary.Operator.Addition,
3304 (Expression) $1, (Expression) $3);
3306 | additive_expression MINUS multiplicative_expression
3308 $$ = new Binary (Binary.Operator.Subtraction,
3309 (Expression) $1, (Expression) $3);
3314 : additive_expression
3315 | shift_expression OP_SHIFT_LEFT additive_expression
3317 $$ = new Binary (Binary.Operator.LeftShift,
3318 (Expression) $1, (Expression) $3);
3320 | shift_expression OP_SHIFT_RIGHT additive_expression
3322 $$ = new Binary (Binary.Operator.RightShift,
3323 (Expression) $1, (Expression) $3);
3327 relational_expression
3329 | relational_expression OP_LT shift_expression
3331 $$ = new Binary (Binary.Operator.LessThan,
3332 (Expression) $1, (Expression) $3);
3334 | relational_expression OP_GT shift_expression
3336 $$ = new Binary (Binary.Operator.GreaterThan,
3337 (Expression) $1, (Expression) $3);
3339 | relational_expression OP_LE shift_expression
3341 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3342 (Expression) $1, (Expression) $3);
3344 | relational_expression OP_GE shift_expression
3346 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3347 (Expression) $1, (Expression) $3);
3349 | relational_expression IS type
3351 $$ = new Is ((Expression) $1, (Expression) $3, (Location) $2);
3353 | relational_expression AS type
3355 $$ = new As ((Expression) $1, (Expression) $3, (Location) $2);
3360 : relational_expression
3361 | equality_expression OP_EQ relational_expression
3363 $$ = new Binary (Binary.Operator.Equality,
3364 (Expression) $1, (Expression) $3);
3366 | equality_expression OP_NE relational_expression
3368 $$ = new Binary (Binary.Operator.Inequality,
3369 (Expression) $1, (Expression) $3);
3374 : equality_expression
3375 | and_expression BITWISE_AND equality_expression
3377 $$ = new Binary (Binary.Operator.BitwiseAnd,
3378 (Expression) $1, (Expression) $3);
3382 exclusive_or_expression
3384 | exclusive_or_expression CARRET and_expression
3386 $$ = new Binary (Binary.Operator.ExclusiveOr,
3387 (Expression) $1, (Expression) $3);
3391 inclusive_or_expression
3392 : exclusive_or_expression
3393 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3395 $$ = new Binary (Binary.Operator.BitwiseOr,
3396 (Expression) $1, (Expression) $3);
3400 conditional_and_expression
3401 : inclusive_or_expression
3402 | conditional_and_expression OP_AND inclusive_or_expression
3404 $$ = new Binary (Binary.Operator.LogicalAnd,
3405 (Expression) $1, (Expression) $3);
3409 conditional_or_expression
3410 : conditional_and_expression
3411 | conditional_or_expression OP_OR conditional_and_expression
3413 $$ = new Binary (Binary.Operator.LogicalOr,
3414 (Expression) $1, (Expression) $3);
3418 conditional_expression
3419 : conditional_or_expression
3420 | conditional_or_expression INTERR expression COLON expression
3422 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
3426 assignment_expression
3427 : prefixed_unary_expression ASSIGN expression
3429 $$ = new Assign ((Expression) $1, (Expression) $3);
3431 | prefixed_unary_expression OP_MULT_ASSIGN expression
3433 $$ = new CompoundAssign (
3434 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
3436 | prefixed_unary_expression OP_DIV_ASSIGN expression
3438 $$ = new CompoundAssign (
3439 Binary.Operator.Division, (Expression) $1, (Expression) $3);
3441 | prefixed_unary_expression OP_MOD_ASSIGN expression
3443 $$ = new CompoundAssign (
3444 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
3446 | prefixed_unary_expression OP_ADD_ASSIGN expression
3448 $$ = new CompoundAssign (
3449 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
3451 | prefixed_unary_expression OP_SUB_ASSIGN expression
3453 $$ = new CompoundAssign (
3454 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3456 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3458 $$ = new CompoundAssign (
3459 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
3461 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3463 $$ = new CompoundAssign (
3464 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
3466 | prefixed_unary_expression OP_AND_ASSIGN expression
3468 $$ = new CompoundAssign (
3469 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
3471 | prefixed_unary_expression OP_OR_ASSIGN expression
3473 $$ = new CompoundAssign (
3474 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
3476 | prefixed_unary_expression OP_XOR_ASSIGN expression
3478 $$ = new CompoundAssign (
3479 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
3484 : conditional_expression
3485 | assignment_expression
3505 MemberName name = MakeName ((MemberName) $5);
3506 int mod_flags = (int) $2;
3508 push_current_class (new Class (
3509 current_namespace, current_class, name,
3510 mod_flags, (Attributes) $1), false, $3);
3515 if (current_class.Name == "System.Object") {
3516 Report.Error (537, current_class.Location,
3517 "The class System.Object cannot have a base " +
3518 "class or implement an interface.");
3520 current_container.AddBasesForPart (current_class, (ArrayList) $7);
3523 if (RootContext.Documentation != null) {
3524 current_container.DocComment = Lexer.consume_doc_comment ();
3525 Lexer.doc_state = XmlCommentState.Allowed;
3530 if (RootContext.Documentation != null)
3531 Lexer.doc_state = XmlCommentState.Allowed;
3535 $$ = pop_current_class ();
3543 { $$ = $1; } // location
3547 : /* empty */ { $$ = (int) 0; }
3553 | modifiers modifier
3558 if ((m1 & m2) != 0) {
3559 Location l = lexer.Location;
3560 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
3562 $$ = (int) (m1 | m2);
3567 : NEW { $$ = Modifiers.NEW; }
3568 | PUBLIC { $$ = Modifiers.PUBLIC; }
3569 | PROTECTED { $$ = Modifiers.PROTECTED; }
3570 | INTERNAL { $$ = Modifiers.INTERNAL; }
3571 | PRIVATE { $$ = Modifiers.PRIVATE; }
3572 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3573 | SEALED { $$ = Modifiers.SEALED; }
3574 | STATIC { $$ = Modifiers.STATIC; }
3575 | READONLY { $$ = Modifiers.READONLY; }
3576 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3577 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3578 | EXTERN { $$ = Modifiers.EXTERN; }
3579 | VOLATILE { $$ = Modifiers.VOLATILE; }
3580 | UNSAFE { $$ = Modifiers.UNSAFE; }
3584 : /* empty */ { $$ = null; }
3585 | class_base { $$ = $1; }
3589 : COLON type_list { $$ = $2; }
3597 // A block is "contained" on the following places:
3599 // property_declaration as part of the accessor body (get/set)
3600 // operator_declaration
3601 // constructor_declaration
3602 // destructor_declaration
3603 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3608 if (current_block == null){
3609 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, (Location) $1);
3610 top_current_block = current_block;
3612 current_block = new Block (current_block, (Location) $1, Location.Null);
3615 opt_statement_list CLOSE_BRACE
3617 while (current_block.Implicit)
3618 current_block = current_block.Parent;
3620 current_block.SetEndLocation ((Location) $4);
3621 current_block = current_block.Parent;
3622 if (current_block == null)
3623 top_current_block = null;
3634 | statement_list statement
3638 : declaration_statement
3640 if ($1 != null && (Block) $1 != current_block){
3641 current_block.AddStatement ((Statement) $1);
3642 current_block = (Block) $1;
3645 | valid_declaration_statement
3647 current_block.AddStatement ((Statement) $1);
3652 valid_declaration_statement
3655 | expression_statement
3656 | selection_statement
3657 | iteration_statement
3661 | unchecked_statement
3669 : valid_declaration_statement
3670 | declaration_statement
3672 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3677 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3685 $$ = EmptyStatement.Value;
3692 LocatedToken lt = (LocatedToken) $1;
3693 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
3695 if (current_block.AddLabel (labeled))
3696 current_block.AddStatement (labeled);
3701 declaration_statement
3702 : local_variable_declaration SEMICOLON
3704 current_array_type = null;
3706 DictionaryEntry de = (DictionaryEntry) $1;
3707 Expression e = (Expression) de.Key;
3709 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
3713 | local_constant_declaration SEMICOLON
3715 current_array_type = null;
3717 DictionaryEntry de = (DictionaryEntry) $1;
3719 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3725 * The following is from Rhys' grammar:
3726 * > Types in local variable declarations must be recognized as
3727 * > expressions to prevent reduce/reduce errors in the grammar.
3728 * > The expressions are converted into types during semantic analysis.
3731 : primary_expression opt_rank_specifier
3733 // FIXME: Do something smart here regarding the composition of the type.
3735 // Ok, the above "primary_expression" is there to get rid of
3736 // both reduce/reduce and shift/reduces in the grammar, it should
3737 // really just be "type_name". If you use type_name, a reduce/reduce
3738 // creeps up. If you use namespace_or_type_name (which is all we need
3739 // really) two shift/reduces appear.
3742 // So the super-trick is that primary_expression
3743 // can only be either a SimpleName or a MemberAccess.
3744 // The MemberAccess case arises when you have a fully qualified type-name like :
3746 // SimpleName is when you have
3749 Expression expr = (Expression) $1;
3750 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3751 Error_ExpectingTypeName (expr);
3755 // So we extract the string corresponding to the SimpleName
3759 if ((string) $2 == "")
3762 $$ = new ComposedCast ((Expression) $1, (string) $2);
3765 | builtin_types opt_rank_specifier
3767 if ((string) $2 == "")
3770 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3774 local_variable_pointer_type
3775 : primary_expression STAR
3777 Expression expr = (Expression) $1;
3779 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3780 Error_ExpectingTypeName (expr);
3784 $$ = new ComposedCast ((Expression) $1, "*");
3786 | builtin_types STAR
3788 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3792 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
3794 | local_variable_pointer_type STAR
3796 $$ = new ComposedCast ((Expression) $1, "*");
3800 local_variable_declaration
3801 : local_variable_type variable_declarators
3804 $$ = new DictionaryEntry ($1, $2);
3808 | local_variable_pointer_type opt_rank_specifier variable_declarators
3813 if ((string) $2 == "")
3814 t = (Expression) $1;
3816 t = new ComposedCast ((Expression) $1, (string) $2);
3817 $$ = new DictionaryEntry (t, $3);
3823 local_constant_declaration
3824 : CONST local_variable_type constant_declarators
3827 $$ = new DictionaryEntry ($2, $3);
3833 expression_statement
3834 : statement_expression SEMICOLON { $$ = $1; }
3838 // We have to do the wrapping here and not in the case above,
3839 // because statement_expression is used for example in for_statement
3841 statement_expression
3844 Expression expr = (Expression) $1;
3845 ExpressionStatement s = expr as ExpressionStatement;
3847 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
3850 $$ = new StatementExpression (s);
3854 Report.Error (1002, GetLocation ($1), "Expecting `;'");
3859 object_creation_expression
3860 : object_or_delegate_creation_expression
3861 { note ("complain if this is a delegate maybe?"); }
3870 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
3873 Location l = (Location) $1;
3875 $$ = new If ((Expression) $3, (Statement) $5, l);
3877 // FIXME: location for warning should be loc property of $5.
3878 if ($5 == EmptyStatement.Value)
3879 Report.Warning (642, 3, l, "Possible mistaken empty statement");
3882 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
3883 embedded_statement ELSE embedded_statement
3885 Location l = (Location) $1;
3887 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
3889 // FIXME: location for warning should be loc property of $5 and $7.
3890 if ($5 == EmptyStatement.Value)
3891 Report.Warning (642, 3, l, "Possible mistaken empty statement");
3892 if ($7 == EmptyStatement.Value)
3893 Report.Warning (642, 3, l, "Possible mistaken empty statement");
3898 : SWITCH OPEN_PARENS
3900 if (switch_stack == null)
3901 switch_stack = new Stack (2);
3902 switch_stack.Push (current_block);
3904 expression CLOSE_PARENS
3907 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
3908 current_block = (Block) switch_stack.Pop ();
3924 Report.Error (1522, lexer.Location, "Empty switch block");
3932 ArrayList sections = new ArrayList (4);
3937 | switch_sections switch_section
3939 ArrayList sections = (ArrayList) $1;
3949 current_block = current_block.CreateSwitchBlock (lexer.Location);
3953 Block topmost = current_block;
3955 while (topmost.Implicit)
3956 topmost = topmost.Parent;
3957 $$ = new SwitchSection ((ArrayList) $1, topmost);
3964 ArrayList labels = new ArrayList (4);
3969 | switch_labels switch_label
3971 ArrayList labels = (ArrayList) ($1);
3979 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
3980 | DEFAULT COLON { $$ = new SwitchLabel (null, (Location) $1); }
3983 1523, GetLocation ($1),
3984 "The keyword case or default must precede code in switch block");
3996 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
3998 Location l = (Location) $1;
3999 $$ = new While ((Expression) $3, (Statement) $5, l);
4004 : DO embedded_statement
4005 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
4007 Location l = (Location) $1;
4009 $$ = new Do ((Statement) $2, (Expression) $5, l);
4015 opt_for_initializer SEMICOLON
4017 Block assign_block = new Block (current_block);
4018 current_block = assign_block;
4020 if ($3 is DictionaryEntry){
4021 DictionaryEntry de = (DictionaryEntry) $3;
4023 Expression type = (Expression) de.Key;
4024 ArrayList var_declarators = (ArrayList) de.Value;
4026 foreach (VariableDeclaration decl in var_declarators){
4030 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4034 Location l = lexer.Location;
4035 Expression expr = decl.expression_or_array_initializer;
4037 LocalVariableReference var;
4038 var = new LocalVariableReference (assign_block, decl.identifier, l);
4041 Assign a = new Assign (var, expr, decl.Location);
4043 assign_block.AddStatement (new StatementExpression (a));
4047 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4048 // This can be referred to as $5 below.
4054 opt_for_condition SEMICOLON
4055 opt_for_iterator CLOSE_PARENS
4058 Location l = (Location) $1;
4060 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4062 current_block.AddStatement (f);
4063 while (current_block.Implicit)
4064 current_block = current_block.Parent;
4066 current_block = current_block.Parent;
4071 : /* empty */ { $$ = EmptyStatement.Value; }
4076 : local_variable_declaration
4077 | statement_expression_list
4081 : /* empty */ { $$ = null; }
4082 | boolean_expression
4086 : /* empty */ { $$ = EmptyStatement.Value; }
4091 : statement_expression_list
4094 statement_expression_list
4095 : statement_expression
4097 // CHANGE: was `null'
4098 Statement s = (Statement) $1;
4099 Block b = new Block (current_block, Block.Flags.Implicit, s.loc, lexer.Location);
4104 | statement_expression_list COMMA statement_expression
4106 Block b = (Block) $1;
4108 b.AddStatement ((Statement) $3);
4114 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4116 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4119 | FOREACH OPEN_PARENS type IDENTIFIER IN
4120 expression CLOSE_PARENS
4122 Block foreach_block = new Block (current_block);
4123 current_block = foreach_block;
4125 LocatedToken lt = (LocatedToken) $4;
4126 Location l = lt.Location;
4129 vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4131 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4133 // Get a writable reference to this read-only variable.
4135 // Note that the $$ here refers to the value of _this_ code block,
4136 // not the value of the LHS non-terminal. This can be referred to as $8 below.
4137 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
4144 LocalVariableReference v = (LocalVariableReference) $8;
4145 Location l = (Location) $1;
4148 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
4149 current_block.AddStatement (f);
4152 while (current_block.Implicit)
4153 current_block = current_block.Parent;
4155 current_block = current_block.Parent;
4161 | continue_statement
4171 $$ = new Break ((Location) $1);
4176 : CONTINUE SEMICOLON
4178 $$ = new Continue ((Location) $1);
4183 : GOTO IDENTIFIER SEMICOLON
4185 LocatedToken lt = (LocatedToken) $2;
4186 $$ = new Goto (lt.Value, lt.Location);
4188 | GOTO CASE constant_expression SEMICOLON
4190 $$ = new GotoCase ((Expression) $3, (Location) $1);
4192 | GOTO DEFAULT SEMICOLON
4194 $$ = new GotoDefault ((Location) $1);
4199 : RETURN opt_expression SEMICOLON
4201 $$ = new Return ((Expression) $2, (Location) $1);
4206 : THROW opt_expression SEMICOLON
4208 $$ = new Throw ((Expression) $2, (Location) $1);
4213 : IDENTIFIER RETURN expression SEMICOLON
4215 LocatedToken lt = (LocatedToken) $1;
4216 string s = lt.Value;
4218 Report.Error (1003, lt.Location, "; expected");
4221 if (RootContext.Version == LanguageVersion.ISO_1){
4222 Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4225 if (iterator_container == null){
4226 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4229 iterator_container.SetYields ();
4230 $$ = new Yield ((Expression) $3, lt.Location);
4233 | IDENTIFIER RETURN SEMICOLON
4235 Report.Error (1627, (Location) $2, "Expression expected after yield return");
4238 | IDENTIFIER BREAK SEMICOLON
4240 LocatedToken lt = (LocatedToken) $1;
4241 string s = lt.Value;
4243 Report.Error (1003, lt.Location, "; expected");
4246 if (RootContext.Version == LanguageVersion.ISO_1){
4247 Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4250 if (iterator_container == null){
4251 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4254 iterator_container.SetYields ();
4255 $$ = new YieldBreak (lt.Location);
4266 : TRY block catch_clauses
4270 ArrayList c = (ArrayList)$3;
4271 for (int i = 0; i < c.Count; ++i) {
4272 Catch cc = (Catch) c [i];
4274 if (i != c.Count - 1)
4275 Report.Error (1017, cc.loc, "Try statement already has an empty catch block");
4282 // Now s contains the list of specific catch clauses
4283 // and g contains the general one.
4285 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4287 | TRY block opt_catch_clauses FINALLY block
4290 ArrayList s = new ArrayList (4);
4291 ArrayList catch_list = (ArrayList) $3;
4293 if (catch_list != null){
4294 foreach (Catch cc in catch_list) {
4302 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4306 Report.Error (1524, (Location) $1, "Expected catch or finally");
4312 : /* empty */ { $$ = null; }
4319 ArrayList l = new ArrayList (4);
4324 | catch_clauses catch_clause
4326 ArrayList l = (ArrayList) $1;
4334 : /* empty */ { $$ = null; }
4339 : CATCH opt_catch_args
4341 Expression type = null;
4344 DictionaryEntry cc = (DictionaryEntry) $2;
4345 type = (Expression) cc.Key;
4346 LocatedToken lt = (LocatedToken) cc.Value;
4349 ArrayList one = new ArrayList (4);
4351 one.Add (new VariableDeclaration (lt, null));
4353 current_block = new Block (current_block);
4354 Block b = declare_local_variables (type, one, lt.Location);
4359 Expression type = null;
4361 Block var_block = null;
4364 DictionaryEntry cc = (DictionaryEntry) $2;
4365 type = (Expression) cc.Key;
4366 LocatedToken lt = (LocatedToken) cc.Value;
4370 while (current_block.Implicit)
4371 current_block = current_block.Parent;
4372 var_block = current_block;
4373 current_block = current_block.Parent;
4377 $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
4382 : /* empty */ { $$ = null; }
4387 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4389 $$ = new DictionaryEntry ($2, $3);
4397 $$ = new Checked ((Block) $2);
4404 $$ = new Unchecked ((Block) $2);
4411 RootContext.CheckUnsafeOption ((Location) $1);
4413 $$ = new Unsafe ((Block) $3);
4419 type fixed_pointer_declarators
4422 ArrayList list = (ArrayList) $4;
4423 Expression type = (Expression) $3;
4424 Location l = (Location) $1;
4425 int top = list.Count;
4427 Block assign_block = new Block (current_block);
4428 current_block = assign_block;
4430 for (int i = 0; i < top; i++){
4431 Pair p = (Pair) list [i];
4434 v = current_block.AddVariable (type, (string) p.First, l);
4438 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4446 Location l = (Location) $1;
4448 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4450 current_block.AddStatement (f);
4451 while (current_block.Implicit)
4452 current_block = current_block.Parent;
4454 current_block = current_block.Parent;
4458 fixed_pointer_declarators
4459 : fixed_pointer_declarator {
4460 ArrayList declarators = new ArrayList (4);
4462 declarators.Add ($1);
4465 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4467 ArrayList declarators = (ArrayList) $1;
4469 declarators.Add ($3);
4474 fixed_pointer_declarator
4475 : IDENTIFIER ASSIGN expression
4477 LocatedToken lt = (LocatedToken) $1;
4478 // FIXME: keep location
4479 $$ = new Pair (lt.Value, $3);
4483 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
4489 : LOCK OPEN_PARENS expression CLOSE_PARENS
4495 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
4500 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4502 Block assign_block = new Block (current_block);
4503 current_block = assign_block;
4505 if ($3 is DictionaryEntry){
4506 DictionaryEntry de = (DictionaryEntry) $3;
4507 Location l = (Location) $1;
4509 Expression type = (Expression) de.Key;
4510 ArrayList var_declarators = (ArrayList) de.Value;
4512 ArrayList vars = new ArrayList (4);
4514 foreach (VariableDeclaration decl in var_declarators){
4516 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4519 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
4521 Expression expr = decl.expression_or_array_initializer;
4523 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4526 LocalVariableReference var;
4528 // Get a writable reference to this read-only variable.
4529 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4531 // This is so that it is not a warning on using variables
4534 vars.Add (new DictionaryEntry (var, expr));
4536 // Assign a = new Assign (var, expr, decl.Location);
4537 // assign_block.AddStatement (new StatementExpression (a));
4540 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4541 // It can be referred to as $5 below.
4542 $$ = new DictionaryEntry (type, vars);
4549 Using u = new Using ($5, (Statement) $6, (Location) $1);
4550 current_block.AddStatement (u);
4551 while (current_block.Implicit)
4552 current_block = current_block.Parent;
4554 current_block = current_block.Parent;
4558 resource_acquisition
4559 : local_variable_declaration
4566 // A class used to pass around variable declarations and constants
4568 public class VariableDeclaration {
4569 public string identifier;
4570 public Expression expression_or_array_initializer;
4571 public Location Location;
4572 public Attributes OptAttributes;
4573 public string DocComment;
4575 public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
4577 this.identifier = lt.Value;
4578 if (eoai is ArrayList) {
4579 if (CSharpParser.current_array_type == null)
4580 Report.Error (622, lt.Location,
4581 "Can only use array initializer expressions to assign to array types. Try using a new expression instead.");
4582 this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
4584 this.expression_or_array_initializer = (Expression)eoai;
4586 this.Location = lt.Location;
4587 this.OptAttributes = opt_attrs;
4590 public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
4596 // A class used to hold info about an indexer declarator
4598 public class IndexerDeclaration {
4599 public Expression type;
4600 public MemberName interface_type;
4601 public Parameters param_list;
4602 public Location location;
4604 public IndexerDeclaration (Expression type, MemberName interface_type,
4605 Parameters param_list, Location loc)
4608 this.interface_type = interface_type;
4609 this.param_list = param_list;
4610 this.location = loc;
4615 // We use this when we do not have an object in advance that is an IIteratorContainer
4617 public class SimpleIteratorContainer : IIteratorContainer {
4620 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4625 public static SimpleIteratorContainer GetSimple () {
4626 Simple.Yields = false;
4630 public void SetYields () { Yields = true; }
4634 // A class used to hold info about an operator declarator
4636 public class OperatorDeclaration {
4637 public Operator.OpType optype;
4638 public Expression ret_type, arg1type, arg2type;
4639 public string arg1name, arg2name;
4640 public Location location;
4642 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4643 Expression arg1type, string arg1name,
4644 Expression arg2type, string arg2name, Location location)
4647 this.ret_type = ret_type;
4648 this.arg1type = arg1type;
4649 this.arg1name = arg1name;
4650 this.arg2type = arg2type;
4651 this.arg2name = arg2name;
4652 this.location = location;
4657 void Error_ExpectingTypeName (Expression expr)
4659 if (expr is Invocation){
4660 Report.Error (1002, expr.Location, "Expecting `;'");
4662 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
4666 void push_current_class (TypeContainer tc, bool is_interface, object partial_token)
4668 if (partial_token != null)
4669 current_container = current_container.AddPartial (tc, is_interface);
4671 current_container = current_container.AddTypeContainer (tc, is_interface);
4675 DeclSpace pop_current_class ()
4677 DeclSpace retval = current_class;
4679 current_class = current_class.Parent;
4680 current_container = current_class.PartialContainer;
4686 // Given the @class_name name, it creates a fully qualified name
4687 // based on the containing declaration space
4690 MakeName (MemberName class_name)
4692 Namespace ns = current_namespace.NS;
4694 if (current_container.Name.Length == 0){
4695 if (ns.Name.Length != 0)
4696 return new MemberName (ns.MemberName, class_name);
4700 return new MemberName (current_container.MemberName, class_name);
4704 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4706 Block implicit_block;
4707 ArrayList inits = null;
4710 // We use the `Used' property to check whether statements
4711 // have been added to the current block. If so, we need
4712 // to create another block to contain the new declaration
4713 // otherwise, as an optimization, we use the same block to
4714 // add the declaration.
4716 // FIXME: A further optimization is to check if the statements
4717 // that were added were added as part of the initialization
4718 // below. In which case, no other statements have been executed
4719 // and we might be able to reduce the number of blocks for
4720 // situations like this:
4722 // int j = 1; int k = j + 1;
4724 if (current_block.Used)
4725 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4727 implicit_block = current_block;
4729 foreach (VariableDeclaration decl in variable_declarators){
4731 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
4732 if (decl.expression_or_array_initializer != null){
4734 inits = new ArrayList (4);
4741 return implicit_block;
4743 foreach (VariableDeclaration decl in inits){
4745 Expression expr = decl.expression_or_array_initializer;
4747 LocalVariableReference var;
4748 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4750 assign = new Assign (var, expr, decl.Location);
4752 implicit_block.AddStatement (new StatementExpression (assign));
4755 return implicit_block;
4758 Block declare_local_constants (Expression type, ArrayList declarators)
4760 Block implicit_block;
4762 if (current_block.Used)
4763 implicit_block = new Block (current_block, Block.Flags.Implicit);
4765 implicit_block = current_block;
4767 foreach (VariableDeclaration decl in declarators){
4768 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
4771 return implicit_block;
4774 void CheckAttributeTarget (string a, Location l)
4778 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4782 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4788 void CheckUnaryOperator (Operator.OpType op, Location l)
4792 case Operator.OpType.LogicalNot:
4793 case Operator.OpType.OnesComplement:
4794 case Operator.OpType.Increment:
4795 case Operator.OpType.Decrement:
4796 case Operator.OpType.True:
4797 case Operator.OpType.False:
4798 case Operator.OpType.Addition:
4799 case Operator.OpType.Subtraction:
4804 Report.Error (1019, l, "Overloadable unary operator expected");
4810 void CheckBinaryOperator (Operator.OpType op, Location l)
4814 case Operator.OpType.Addition:
4815 case Operator.OpType.Subtraction:
4816 case Operator.OpType.Multiply:
4817 case Operator.OpType.Division:
4818 case Operator.OpType.Modulus:
4819 case Operator.OpType.BitwiseAnd:
4820 case Operator.OpType.BitwiseOr:
4821 case Operator.OpType.ExclusiveOr:
4822 case Operator.OpType.LeftShift:
4823 case Operator.OpType.RightShift:
4824 case Operator.OpType.Equality:
4825 case Operator.OpType.Inequality:
4826 case Operator.OpType.GreaterThan:
4827 case Operator.OpType.LessThan:
4828 case Operator.OpType.GreaterThanOrEqual:
4829 case Operator.OpType.LessThanOrEqual:
4833 Report.Error (1020, l, "Overloadable binary operator expected");
4839 void syntax_error (Location l, string msg)
4841 Report.Error (1003, l, "Syntax error, " + msg);
4844 void note (string s)
4846 // Used to put annotations
4851 public Tokenizer Lexer {
4857 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4859 this.name = file.Name;
4861 current_namespace = new NamespaceEntry (null, file, null);
4862 current_class = current_namespace.SlaveDeclSpace;
4863 current_container = current_class.PartialContainer; // == RootContest.ToplevelTypes
4865 lexer = new Tokenizer (reader, file, defines);
4868 public void parse ()
4870 int errors = Report.Errors;
4872 if (yacc_verbose_flag > 1)
4873 yyparse (lexer, new yydebug.yyDebugSimple ());
4876 Tokenizer tokenizer = lexer as Tokenizer;
4877 tokenizer.cleanup ();
4878 } catch (Exception e){
4880 // Removed for production use, use parser verbose to get the output.
4882 // Console.WriteLine (e);
4883 if (Report.Errors == errors)
4884 Report.Error (-25, lexer.Location, "Parsing error");
4885 if (yacc_verbose_flag > 0)
4886 Console.WriteLine (e);
4889 if (RootContext.ToplevelTypes.NamespaceEntry != null)
4890 throw new InternalErrorException ("who set it?");
4893 static void CheckToken (int error, int yyToken, string msg, Location loc)
4895 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
4896 Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ());
4898 Report.Error (error, loc, msg);
4901 void CheckIdentifierToken (int yyToken, Location loc)
4903 CheckToken (1041, yyToken, "Identifier expected", loc);
4906 string ConsumeStoredComment ()
4908 string s = tmpComment;
4910 Lexer.doc_state = XmlCommentState.Allowed;
4914 Location GetLocation (object obj)
4916 if (obj is MemberCore)
4917 return ((MemberCore) obj).Location;
4918 if (obj is MemberName)
4919 return ((MemberName) obj).Location;
4920 if (obj is LocatedToken)
4921 return ((LocatedToken) obj).Location;
4922 if (obj is Location)
4923 return (Location) obj;
4924 return lexer.Location;