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 TypeContainer 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 /// Used to determine if we are parsing the get/set pair
62 /// of an indexer or a property
67 /// An out-of-band stack.
76 static public int yacc_verbose_flag;
78 // Name of the file we are parsing
87 /// Temporary Xml documentation cache.
88 /// For enum types, we need one more temporary store.
91 string enumTypeComment;
93 /// Current attribute target
94 string current_attr_target;
96 /// assembly and module attribute definitions are enabled
97 bool global_attrs_enabled = true;
99 bool has_get, has_set;
104 %token NONE /* This token is never returned by our lexer */
105 %token ERROR // This is used not by the parser, but by the tokenizer.
109 *These are the C# keywords
195 /* C# keywords which are not really keywords */
201 /* C# single character operators/punctuation. */
202 %token OPEN_BRACE "{"
203 %token CLOSE_BRACE "}"
204 %token OPEN_BRACKET "["
205 %token CLOSE_BRACKET "]"
206 %token OPEN_PARENS "("
207 %token CLOSE_PARENS ")"
220 %token BITWISE_AND "&"
221 %token BITWISE_OR "|"
228 /* C# multi-character operators. */
229 %token DOUBLE_COLON "::"
232 %token OP_SHIFT_LEFT "<<"
233 %token OP_SHIFT_RIGHT ">>"
240 %token OP_MULT_ASSIGN "*="
241 %token OP_DIV_ASSIGN "/="
242 %token OP_MOD_ASSIGN "%="
243 %token OP_ADD_ASSIGN "+="
244 %token OP_SUB_ASSIGN "-="
245 %token OP_SHIFT_LEFT_ASSIGN "<<="
246 %token OP_SHIFT_RIGHT_ASSIGN ">>="
247 %token OP_AND_ASSIGN "&="
248 %token OP_XOR_ASSIGN "^="
249 %token OP_OR_ASSIGN "|="
253 %token LITERAL_INTEGER "int literal"
254 %token LITERAL_FLOAT "float literal"
255 %token LITERAL_DOUBLE "double literal"
256 %token LITERAL_DECIMAL "decimal literal"
257 %token LITERAL_CHARACTER "character literal"
258 %token LITERAL_STRING "string literal"
261 %token CLOSE_PARENS_CAST
262 %token CLOSE_PARENS_NO_CAST
263 %token CLOSE_PARENS_OPEN_PARENS
264 %token CLOSE_PARENS_MINUS
266 /* Add precedence rules to solve dangling else s/r conflict */
275 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
277 %left STAR DIV PERCENT
278 %right BANG CARRET UMINUS
279 %nonassoc OP_INC OP_DEC
281 %left OPEN_BRACKET OPEN_BRACE
285 %start compilation_unit
289 : outer_declarations opt_EOF
290 | outer_declarations global_attributes opt_EOF
291 | global_attributes opt_EOF
292 | opt_EOF /* allow empty files */
298 Lexer.check_incorrect_doc_comment ();
302 Lexer.check_incorrect_doc_comment ();
308 | outer_declarations outer_declaration
313 | namespace_member_declaration
318 | using_directives using_directive
322 : using_alias_directive
324 if (RootContext.Documentation != null)
325 Lexer.doc_state = XmlCommentState.Allowed;
327 | using_namespace_directive
329 if (RootContext.Documentation != null)
330 Lexer.doc_state = XmlCommentState.Allowed;
334 using_alias_directive
335 : USING IDENTIFIER ASSIGN
336 namespace_or_type_name SEMICOLON
338 LocatedToken lt = (LocatedToken) $2;
339 current_namespace.UsingAlias (lt.Value, (MemberName) $4, (Location) $1);
342 CheckIdentifierToken (yyToken, GetLocation ($2));
346 using_namespace_directive
347 : USING namespace_name SEMICOLON
349 current_namespace.Using ((MemberName) $2, (Location) $1);
354 // Strictly speaking, namespaces don't have attributes but
355 // we parse global attributes along with namespace declarations and then
358 namespace_declaration
359 : opt_attributes NAMESPACE namespace_or_type_name
361 MemberName name = (MemberName) $3;
364 Report.Error(1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
367 current_namespace = new NamespaceEntry (
368 current_namespace, file, name.GetName (), name.Location);
370 namespace_body opt_semicolon
372 current_namespace = current_namespace.Parent;
387 : namespace_or_type_name
393 if (RootContext.Documentation != null)
394 Lexer.doc_state = XmlCommentState.Allowed;
397 opt_namespace_member_declarations
406 opt_namespace_member_declarations
408 | namespace_member_declarations
411 namespace_member_declarations
412 : namespace_member_declaration
413 | namespace_member_declarations namespace_member_declaration
416 namespace_member_declaration
420 DeclSpace ds = (DeclSpace)$1;
422 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
423 Report.Error (1527, ds.Location,
424 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
427 current_namespace.DeclarationFound = true;
429 | namespace_declaration {
430 current_namespace.DeclarationFound = true;
433 | field_declaration {
434 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
436 | method_declaration {
437 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
444 | interface_declaration
446 | delegate_declaration
448 // Enable this when we have handled all errors, because this acts as a generic fallback
451 // Console.WriteLine ("Token=" + yyToken);
452 // Report.Error (1518, GetLocation ($1), "Expected class, struct, interface, enum or delegate");
464 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
472 global_attrs_enabled = false;
477 global_attrs_enabled = false;
486 ArrayList sect = (ArrayList) $1;
488 if (global_attrs_enabled) {
489 if (current_attr_target == "module") {
490 CodeGen.Module.AddAttributes (sect);
492 } else if (current_attr_target != null && current_attr_target.Length > 0) {
493 CodeGen.Assembly.AddAttributes (sect);
496 $$ = new Attributes (sect);
499 if (RootContext.Documentation != null) {
500 Lexer.check_incorrect_doc_comment ();
502 XmlCommentState.Allowed;
506 $$ = new Attributes (sect);
508 current_attr_target = null;
510 | attribute_sections attribute_section
512 Attributes attrs = $1 as Attributes;
513 ArrayList sect = (ArrayList) $2;
515 if (global_attrs_enabled) {
516 if (current_attr_target == "module") {
517 CodeGen.Module.AddAttributes (sect);
519 } else if (current_attr_target == "assembly") {
520 CodeGen.Assembly.AddAttributes (sect);
524 attrs = new Attributes (sect);
526 attrs.AddAttributes (sect);
530 attrs = new Attributes (sect);
532 attrs.AddAttributes (sect);
535 current_attr_target = null;
540 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
544 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
550 attribute_target_specifier
551 : attribute_target COLON
553 current_attr_target = (string)$1;
561 LocatedToken lt = (LocatedToken) $1;
562 CheckAttributeTarget (lt.Value, lt.Location);
563 $$ = lt.Value; // Location won't be required anymore.
565 | EVENT { $$ = "event"; }
566 | RETURN { $$ = "return"; }
572 ArrayList attrs = new ArrayList (4);
578 | attribute_list COMMA attribute
580 ArrayList attrs = (ArrayList) $1;
588 : attribute_name opt_attribute_arguments
590 MemberName mname = (MemberName) $1;
591 ArrayList arguments = (ArrayList) $2;
592 MemberName left = mname.Left;
593 string identifier = mname.Name;
595 Expression left_expr = left == null ? null : left.GetTypeExpression ();
597 if (current_attr_target == "assembly" || current_attr_target == "module")
598 // FIXME: supply "nameEscaped" parameter here.
599 $$ = new GlobalAttribute (current_class, current_attr_target,
600 left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
602 $$ = new Attribute (current_attr_target, left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
607 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
610 opt_attribute_arguments
611 : /* empty */ { $$ = null; }
612 | OPEN_PARENS attribute_arguments CLOSE_PARENS
620 : opt_positional_argument_list
625 ArrayList args = new ArrayList (4);
631 | positional_argument_list COMMA named_argument_list
633 ArrayList args = new ArrayList (4);
639 | named_argument_list
641 ArrayList args = new ArrayList (4);
650 opt_positional_argument_list
651 : /* empty */ { $$ = null; }
652 | positional_argument_list
655 positional_argument_list
658 ArrayList args = new ArrayList (4);
659 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
663 | positional_argument_list COMMA expression
665 ArrayList args = (ArrayList) $1;
666 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
675 ArrayList args = new ArrayList (4);
680 | named_argument_list COMMA named_argument
682 ArrayList args = (ArrayList) $1;
687 | named_argument_list COMMA expression
689 Report.Error (1016, ((Expression) $3).Location, "Named attribute argument expected");
695 : IDENTIFIER ASSIGN expression
697 // FIXME: keep location
698 $$ = new DictionaryEntry (
699 ((LocatedToken) $1).Value,
700 new Argument ((Expression) $3, Argument.AType.Expression));
706 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
709 opt_class_member_declarations
711 | class_member_declarations
714 class_member_declarations
715 : class_member_declaration
716 | class_member_declarations
717 class_member_declaration
720 class_member_declaration
721 : constant_declaration // done
722 | field_declaration // done
723 | method_declaration // done
724 | property_declaration // done
725 | event_declaration // done
726 | indexer_declaration // done
727 | operator_declaration // done
728 | constructor_declaration // done
729 | destructor_declaration // done
739 MemberName name = MakeName ((MemberName) $5);
741 ClassPart part = PartialContainer.CreatePart (
742 current_namespace, current_class, name, (int) $2,
743 (Attributes) $1, Kind.Struct, (Location) $3);
745 current_container = part.PartialContainer;
746 current_class = part;
748 current_class = new Struct (
749 current_namespace, current_class, name, (int) $2,
752 current_container.AddClassOrStruct (current_class);
753 current_container = current_class;
754 RootContext.Tree.RecordDecl (current_namespace.NS, name, current_class);
760 current_class.Bases = (ArrayList) $7;
762 if (RootContext.Documentation != null)
763 current_class.DocComment = Lexer.consume_doc_comment ();
767 if (RootContext.Documentation != null)
768 Lexer.doc_state = XmlCommentState.Allowed;
772 $$ = pop_current_class ();
774 | opt_attributes opt_modifiers opt_partial STRUCT error {
775 CheckIdentifierToken (yyToken, GetLocation ($5));
782 if (RootContext.Documentation != null)
783 Lexer.doc_state = XmlCommentState.Allowed;
785 opt_struct_member_declarations CLOSE_BRACE
788 opt_struct_member_declarations
790 | struct_member_declarations
793 struct_member_declarations
794 : struct_member_declaration
795 | struct_member_declarations struct_member_declaration
798 struct_member_declaration
799 : constant_declaration
802 | property_declaration
804 | indexer_declaration
805 | operator_declaration
806 | constructor_declaration
810 * This is only included so we can flag error 575:
811 * destructors only allowed on class types
813 | destructor_declaration
824 int modflags = (int) $2;
825 foreach (VariableDeclaration constant in (ArrayList) $5){
826 Location l = constant.Location;
827 if ((modflags & Modifiers.STATIC) != 0) {
828 Report.Error (504, l, "The constant `{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
832 Const c = new Const (
833 current_class, (Expression) $4, (string) constant.identifier,
834 (Expression) constant.expression_or_array_initializer, modflags,
837 if (RootContext.Documentation != null) {
838 c.DocComment = Lexer.consume_doc_comment ();
839 Lexer.doc_state = XmlCommentState.Allowed;
841 current_container.AddConstant (c);
847 : constant_declarator
849 ArrayList constants = new ArrayList (4);
854 | constant_declarators COMMA constant_declarator
857 ArrayList constants = (ArrayList) $1;
864 : IDENTIFIER ASSIGN constant_expression
866 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
870 // A const field requires a value to be provided
871 Report.Error (145, ((LocatedToken) $1).Location, "A const field requires a value to be provided");
883 Expression type = (Expression) $3;
886 foreach (VariableDeclaration var in (ArrayList) $4){
887 Field field = new Field (current_class, type, mod, var.identifier,
888 var.expression_or_array_initializer,
889 (Attributes) $1, var.Location);
891 if (RootContext.Documentation != null) {
892 field.DocComment = Lexer.consume_doc_comment ();
893 Lexer.doc_state = XmlCommentState.Allowed;
895 current_container.AddField (field);
896 $$ = field; // FIXME: might be better if it points to the top item
903 fixed_variable_declarators
906 Expression type = (Expression) $4;
909 foreach (VariableDeclaration var in (ArrayList) $5) {
910 FixedField field = new FixedField (current_class, type, mod, var.identifier,
911 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
913 if (RootContext.Documentation != null) {
914 field.DocComment = Lexer.consume_doc_comment ();
915 Lexer.doc_state = XmlCommentState.Allowed;
917 current_container.AddField (field);
918 $$ = field; // FIXME: might be better if it points to the top item
926 Report.Error (670, (Location) $3, "Fields cannot have void type");
930 fixed_variable_declarators
931 : fixed_variable_declarator
933 ArrayList decl = new ArrayList (2);
937 | fixed_variable_declarators COMMA fixed_variable_declarator
939 ArrayList decls = (ArrayList) $1;
945 fixed_variable_declarator
946 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
948 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
953 : variable_declarator
955 ArrayList decl = new ArrayList (4);
960 | variable_declarators COMMA variable_declarator
962 ArrayList decls = (ArrayList) $1;
969 : IDENTIFIER ASSIGN variable_initializer
971 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
975 $$ = new VariableDeclaration ((LocatedToken) $1, null);
977 | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
979 Report.Error (650, ((LocatedToken) $1).Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
980 "To declare a fixed size buffer field, use the fixed keyword before the field type");
994 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
996 $$ = new StackAlloc ((Expression) $2, (Expression) $4, (Location) $1);
1000 Report.Error (1575, (Location) $1, "A stackalloc expression requires [] after type");
1007 iterator_container = (IIteratorContainer) $1;
1008 if (RootContext.Documentation != null)
1009 Lexer.doc_state = XmlCommentState.NotAllowed;
1013 Method method = (Method) $1;
1014 method.Block = (ToplevelBlock) $3;
1015 current_container.AddMethod (method);
1017 current_local_parameters = null;
1018 iterator_container = null;
1020 if (RootContext.Documentation != null)
1021 Lexer.doc_state = XmlCommentState.Allowed;
1034 Report.Error (1585, lexer.Location,
1035 "Member modifier `{0}' must precede the member type and name",
1036 Modifiers.Name (i));
1047 type namespace_or_type_name
1048 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1050 MemberName name = (MemberName) $4;
1052 Method method = new Method (current_class, (Expression) $3, (int) $2,
1053 false, name, (Parameters) $6, (Attributes) $1);
1055 current_local_parameters = (Parameters) $6;
1057 if (RootContext.Documentation != null)
1058 method.DocComment = Lexer.consume_doc_comment ();
1064 VOID namespace_or_type_name
1065 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1067 MemberName name = (MemberName) $4;
1069 Method method = new Method (current_class, TypeManager.system_void_expr,
1070 (int) $2, false, name, (Parameters) $6,
1073 current_local_parameters = (Parameters) $6;
1075 if (RootContext.Documentation != null)
1076 method.DocComment = Lexer.consume_doc_comment ();
1083 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1085 MemberName name = (MemberName) $5;
1086 Report.Error (1585, name.Location,
1087 "Member modifier `{0}' must precede the member type and name", Modifiers.Name ((int) $4));
1089 Method method = new Method (current_class, TypeManager.system_void_expr,
1090 0, false, name, (Parameters) $7, (Attributes) $1);
1092 current_local_parameters = (Parameters) $7;
1094 if (RootContext.Documentation != null)
1095 method.DocComment = Lexer.consume_doc_comment ();
1103 | SEMICOLON { $$ = null; }
1106 opt_formal_parameter_list
1107 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1108 | formal_parameter_list
1111 formal_parameter_list
1114 ArrayList pars_list = (ArrayList) $1;
1116 Parameter [] pars = new Parameter [pars_list.Count];
1117 pars_list.CopyTo (pars);
1119 $$ = new Parameters (pars, null);
1121 | fixed_parameters COMMA parameter_array
1123 ArrayList pars_list = (ArrayList) $1;
1125 Parameter [] pars = new Parameter [pars_list.Count];
1126 pars_list.CopyTo (pars);
1128 $$ = new Parameters (pars, (Parameter) $3);
1130 | fixed_parameters COMMA ARGLIST
1132 ArrayList pars_list = (ArrayList) $1;
1134 Parameter [] pars = new Parameter [pars_list.Count];
1135 pars_list.CopyTo (pars);
1137 $$ = new Parameters (pars, true);
1139 | parameter_array COMMA fixed_parameters
1142 Report.Error (231, ((Parameter) $1).Location, "A params parameter must be the last parameter in a formal parameter list");
1145 | ARGLIST COMMA fixed_parameters
1147 Report.Error (257, (Location) $1, "An __arglist parameter must be the last parameter in a formal parameter list");
1152 $$ = new Parameters (null, (Parameter) $1);
1156 $$ = new Parameters (null, true);
1163 ArrayList pars = new ArrayList (4);
1168 | fixed_parameters COMMA fixed_parameter
1170 ArrayList pars = (ArrayList) $1;
1179 opt_parameter_modifier
1183 LocatedToken lt = (LocatedToken) $4;
1184 $$ = new Parameter ((Expression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1187 opt_parameter_modifier
1189 IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1191 LocatedToken lt = (LocatedToken) $4;
1192 Report.Error (1552, lt.Location, "Array type specifier, [], must appear before parameter name");
1196 opt_parameter_modifier
1199 Report.Error (1001, GetLocation ($3), "Identifier expected");
1203 opt_parameter_modifier
1206 CheckIdentifierToken (yyToken, GetLocation ($4));
1210 opt_parameter_modifier
1216 LocatedToken lt = (LocatedToken) $4;
1217 Report.Error (241, lt.Location, "Default parameter specifiers are not permitted");
1222 opt_parameter_modifier
1223 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1224 | parameter_modifier
1228 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1229 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1233 : opt_attributes PARAMS type IDENTIFIER
1235 LocatedToken lt = (LocatedToken) $4;
1236 $$ = new Parameter ((Expression) $3, lt.Value, Parameter.Modifier.PARAMS, (Attributes) $1, lt.Location);
1237 note ("type must be a single-dimension array type");
1239 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1241 Report.Error (1611, (Location) $2, "The params parameter cannot be declared as ref or out");
1244 | opt_attributes PARAMS type error {
1245 CheckIdentifierToken (yyToken, GetLocation ($4));
1250 property_declaration
1254 namespace_or_type_name
1256 if (RootContext.Documentation != null)
1257 tmpComment = Lexer.consume_doc_comment ();
1261 implicit_value_parameter_type = (Expression) $3;
1263 lexer.PropertyParsing = true;
1265 accessor_declarations
1267 lexer.PropertyParsing = false;
1268 has_get = has_set = false;
1276 Pair pair = (Pair) $8;
1277 Accessor get_block = (Accessor) pair.First;
1278 Accessor set_block = (Accessor) pair.Second;
1280 MemberName name = (MemberName) $4;
1282 prop = new Property (current_class, (Expression) $3, (int) $2,
1283 false, name, (Attributes) $1, get_block, set_block);
1285 current_container.AddProperty (prop);
1286 implicit_value_parameter_type = null;
1288 if (RootContext.Documentation != null)
1289 prop.DocComment = ConsumeStoredComment ();
1294 accessor_declarations
1295 : get_accessor_declaration
1297 $$ = new Pair ($1, null);
1299 | get_accessor_declaration accessor_declarations
1301 Pair pair = (Pair) $2;
1305 | set_accessor_declaration
1307 $$ = new Pair (null, $1);
1309 | set_accessor_declaration accessor_declarations
1311 Pair pair = (Pair) $2;
1317 Report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1322 get_accessor_declaration
1323 : opt_attributes opt_modifiers GET
1325 // If this is not the case, then current_local_parameters has already
1326 // been set in indexer_declaration
1327 if (parsing_indexer == false)
1328 current_local_parameters = null;
1330 current_local_parameters = indexer_parameters;
1331 lexer.PropertyParsing = false;
1333 iterator_container = SimpleIteratorContainer.GetSimple ();
1338 Report.Error (1007, (Location) $3, "Property accessor already defined");
1341 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1343 current_local_parameters = null;
1344 lexer.PropertyParsing = true;
1346 if (SimpleIteratorContainer.Simple.Yields)
1347 accessor.SetYields ();
1349 iterator_container = null;
1351 if (RootContext.Documentation != null)
1352 if (Lexer.doc_state == XmlCommentState.Error)
1353 Lexer.doc_state = XmlCommentState.NotAllowed;
1359 set_accessor_declaration
1360 : opt_attributes opt_modifiers SET
1363 Parameter implicit_value_parameter = new Parameter (
1364 implicit_value_parameter_type, "value",
1365 Parameter.Modifier.NONE, null, (Location) $3);
1367 if (parsing_indexer == false) {
1368 args = new Parameter [1];
1369 args [0] = implicit_value_parameter;
1370 current_local_parameters = new Parameters (args, null);
1372 Parameter [] fpars = indexer_parameters.FixedParameters;
1375 int count = fpars.Length;
1377 args = new Parameter [count + 1];
1378 fpars.CopyTo (args, 0);
1379 args [count] = implicit_value_parameter;
1382 current_local_parameters = new Parameters (
1383 args, indexer_parameters.ArrayParameter);
1386 lexer.PropertyParsing = false;
1388 iterator_container = SimpleIteratorContainer.GetSimple ();
1393 Report.Error (1007, ((LocatedToken) $3).Location, "Property accessor already defined");
1396 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1398 current_local_parameters = null;
1399 lexer.PropertyParsing = true;
1401 if (SimpleIteratorContainer.Simple.Yields)
1402 accessor.SetYields ();
1404 iterator_container = null;
1406 if (RootContext.Documentation != null
1407 && Lexer.doc_state == XmlCommentState.Error)
1408 Lexer.doc_state = XmlCommentState.NotAllowed;
1416 | SEMICOLON { $$ = null; }
1419 interface_declaration
1423 INTERFACE member_name
1425 MemberName name = MakeName ((MemberName) $5);
1428 ClassPart part = PartialContainer.CreatePart (
1429 current_namespace, current_class, name, (int) $2,
1430 (Attributes) $1, Kind.Interface, (Location) $3);
1432 current_container = part.PartialContainer;
1433 current_class = part;
1435 current_class = new Interface (
1436 current_namespace, current_class, name, (int) $2,
1439 current_container.AddInterface (current_class);
1440 current_container = current_class;
1441 RootContext.Tree.RecordDecl (current_namespace.NS, name, current_class);
1446 current_class.Bases = (ArrayList) $7;
1448 if (RootContext.Documentation != null) {
1449 current_class.DocComment = Lexer.consume_doc_comment ();
1450 Lexer.doc_state = XmlCommentState.Allowed;
1455 if (RootContext.Documentation != null)
1456 Lexer.doc_state = XmlCommentState.Allowed;
1460 $$ = pop_current_class ();
1462 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1463 CheckIdentifierToken (yyToken, GetLocation ($5));
1469 opt_interface_member_declarations
1473 opt_interface_member_declarations
1475 | interface_member_declarations
1478 interface_member_declarations
1479 : interface_member_declaration
1480 | interface_member_declarations interface_member_declaration
1483 interface_member_declaration
1484 : interface_method_declaration
1489 Method m = (Method) $1;
1491 if (m.IsExplicitImpl)
1492 Report.Error (541, m.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1493 m.GetSignatureForError ());
1495 current_container.AddMethod (m);
1497 if (RootContext.Documentation != null)
1498 Lexer.doc_state = XmlCommentState.Allowed;
1500 | interface_property_declaration
1505 Property p = (Property) $1;
1507 if (p.IsExplicitImpl)
1508 Report.Error (541, p.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1509 p.GetSignatureForError ());
1511 current_container.AddProperty (p);
1513 if (RootContext.Documentation != null)
1514 Lexer.doc_state = XmlCommentState.Allowed;
1516 | interface_event_declaration
1519 Event e = (Event) $1;
1521 if (e.IsExplicitImpl)
1522 Report.Error (541, e.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1523 e.GetSignatureForError ());
1525 current_container.AddEvent (e);
1528 if (RootContext.Documentation != null)
1529 Lexer.doc_state = XmlCommentState.Allowed;
1531 | interface_indexer_declaration
1536 Indexer i = (Indexer) $1;
1538 if (i.IsExplicitImpl)
1539 Report.Error (541, i.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1540 i.GetSignatureForError ());
1542 current_container.AddIndexer (i);
1544 if (RootContext.Documentation != null)
1545 Lexer.doc_state = XmlCommentState.Allowed;
1547 | delegate_declaration
1550 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1551 ((MemberCore)$1).GetSignatureForError ());
1557 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1558 ((MemberCore)$1).GetSignatureForError ());
1561 | struct_declaration
1564 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1565 ((MemberCore)$1).GetSignatureForError ());
1571 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1572 ((MemberCore)$1).GetSignatureForError ());
1575 | interface_declaration
1578 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1579 ((MemberCore)$1).GetSignatureForError ());
1582 | constant_declaration
1584 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1592 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, GetLocation ($1));
1597 interface_method_declaration
1598 : opt_attributes opt_new type namespace_or_type_name
1599 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1602 $$ = new Method (current_class, (Expression) $3, (int) $2, true,
1603 (MemberName) $4, (Parameters) $6, (Attributes) $1);
1604 if (RootContext.Documentation != null)
1605 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1607 | opt_attributes opt_new type namespace_or_type_name
1608 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1609 OPEN_BRACE opt_statement_list CLOSE_BRACE
1611 $$ = new Method (current_class, (Expression) $3, (int) $2, true,
1612 (MemberName) $4, (Parameters) $6, (Attributes) $1);
1614 Report.Error (531, lexer.Location, "`{0}': interface members cannot have a definition",
1615 ((Method)$$).GetSignatureForError ());
1619 interface_property_declaration
1624 { lexer.PropertyParsing = true; }
1625 accessor_declarations
1627 has_get = has_set = false;
1628 lexer.PropertyParsing = false;
1632 LocatedToken lt = (LocatedToken) $4;
1633 MemberName name = new MemberName (lt.Value, lt.Location);
1635 if ($3 == TypeManager.system_void_expr) {
1636 Report.Error (547, lt.Location, "`{0}': property or indexer cannot have void type", lt.Value);
1642 p = new Property (current_class, (Expression) $3, (int) $2, true,
1643 name, (Attributes) $1,
1646 Report.Error (548, p.Location, "`{0}': property or indexer must have at least one accessor", p.GetSignatureForError ());
1650 Pair pair = (Pair) $7;
1651 p = new Property (current_class, (Expression) $3, (int) $2, true,
1652 name, (Attributes) $1,
1653 (Accessor)pair.First, (Accessor)pair.Second);
1655 if (pair.First != null && ((Accessor)(pair.First)).Block != null) {
1656 Report.Error (531, p.Location, "`{0}.get': interface members cannot have a definition", p.GetSignatureForError ());
1661 if (pair.Second != null && ((Accessor)(pair.Second)).Block != null) {
1662 Report.Error (531, p.Location, "`{0}.set': interface members cannot have a definition", p.GetSignatureForError ());
1667 if (RootContext.Documentation != null)
1668 p.DocComment = Lexer.consume_doc_comment ();
1675 CheckIdentifierToken (yyToken, GetLocation ($4));
1681 interface_event_declaration
1682 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1684 LocatedToken lt = (LocatedToken) $5;
1685 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1686 new MemberName (lt.Value, lt.Location), null,
1688 if (RootContext.Documentation != null)
1689 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1691 | opt_attributes opt_new EVENT type error {
1692 CheckIdentifierToken (yyToken, GetLocation ($5));
1695 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1696 LocatedToken lt = (LocatedToken) $5;
1697 Report.Error (68, lt.Location, "`{0}.{1}': event in interface cannot have initializer", current_container.Name, lt.Value);
1700 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1702 lexer.EventParsing = true;
1704 event_accessor_declarations
1706 lexer.EventParsing = false;
1709 Report.Error (69, (Location) $3, "Event in interface cannot have add or remove accessors");
1714 interface_indexer_declaration
1715 : opt_attributes opt_new type THIS
1716 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1718 { lexer.PropertyParsing = true; }
1719 accessor_declarations
1721 has_get = has_set = false;
1722 lexer.PropertyParsing = false;
1728 i = new Indexer (current_class, (Expression) $3,
1729 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1730 (int) $2, true, (Parameters) $6, (Attributes) $1,
1733 Report.Error (548, i.Location, "`{0}': property or indexer must have at least one accessor", i.GetSignatureForError ());
1737 Pair pair = (Pair) $10;
1738 i = new Indexer (current_class, (Expression) $3,
1739 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1740 (int) $2, true, (Parameters) $6, (Attributes) $1,
1741 (Accessor)pair.First, (Accessor)pair.Second);
1743 if (pair.First != null && ((Accessor)(pair.First)).Block != null) {
1744 Report.Error (531, i.Location, "`{0}.get': interface members cannot have a definition", i.GetSignatureForError ());
1749 if (pair.Second != null && ((Accessor)(pair.Second)).Block != null) {
1750 Report.Error (531, i.Location, "`{0}.set': interface members cannot have a definition", i.GetSignatureForError ());
1755 if (RootContext.Documentation != null)
1756 i.DocComment = ConsumeStoredComment ();
1762 operator_declaration
1763 : opt_attributes opt_modifiers operator_declarator
1765 iterator_container = SimpleIteratorContainer.GetSimple ();
1772 OperatorDeclaration decl = (OperatorDeclaration) $3;
1774 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1776 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null, decl.location);
1777 if (decl.arg2type != null)
1778 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null, decl.location);
1780 Operator op = new Operator (
1781 current_class, decl.optype, decl.ret_type, (int) $2,
1782 new Parameters (param_list, null),
1783 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1785 if (RootContext.Documentation != null) {
1786 op.DocComment = tmpComment;
1787 Lexer.doc_state = XmlCommentState.Allowed;
1790 if (SimpleIteratorContainer.Simple.Yields)
1793 // Note again, checking is done in semantic analysis
1794 current_container.AddOperator (op);
1796 current_local_parameters = null;
1797 iterator_container = null;
1803 | SEMICOLON { $$ = null; }
1806 : type OPERATOR overloadable_operator
1807 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1809 LocatedToken lt = (LocatedToken) $6;
1810 Operator.OpType op = (Operator.OpType) $3;
1811 CheckUnaryOperator (op, lt.Location);
1813 if (op == Operator.OpType.Addition)
1814 op = Operator.OpType.UnaryPlus;
1816 if (op == Operator.OpType.Subtraction)
1817 op = Operator.OpType.UnaryNegation;
1819 Parameter [] pars = new Parameter [1];
1820 Expression type = (Expression) $5;
1822 pars [0] = new Parameter (type, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1824 current_local_parameters = new Parameters (pars, null);
1826 if (RootContext.Documentation != null) {
1827 tmpComment = Lexer.consume_doc_comment ();
1828 Lexer.doc_state = XmlCommentState.NotAllowed;
1831 $$ = new OperatorDeclaration (op, (Expression) $1, type, lt.Value,
1832 null, null, (Location) $2);
1834 | type OPERATOR overloadable_operator
1836 type IDENTIFIER COMMA
1840 LocatedToken ltParam1 = (LocatedToken) $6;
1841 LocatedToken ltParam2 = (LocatedToken) $9;
1842 CheckBinaryOperator ((Operator.OpType) $3, (Location) $2);
1844 Parameter [] pars = new Parameter [2];
1846 Expression typeL = (Expression) $5;
1847 Expression typeR = (Expression) $8;
1849 pars [0] = new Parameter (typeL, ltParam1.Value, Parameter.Modifier.NONE, null, ltParam1.Location);
1850 pars [1] = new Parameter (typeR, ltParam2.Value, Parameter.Modifier.NONE, null, ltParam2.Location);
1852 current_local_parameters = new Parameters (pars, null);
1854 if (RootContext.Documentation != null) {
1855 tmpComment = Lexer.consume_doc_comment ();
1856 Lexer.doc_state = XmlCommentState.NotAllowed;
1859 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1860 typeL, ltParam1.Value,
1861 typeR, ltParam2.Value, (Location) $2);
1863 | conversion_operator_declarator
1864 | type OPERATOR overloadable_operator
1866 type IDENTIFIER COMMA
1867 type IDENTIFIER COMMA
1871 Report.Error (1534, (Location) $2, "Overloaded binary operator `{0}' takes two parameters",
1872 Operator.GetName ((Operator.OpType) $3));
1875 | type OPERATOR overloadable_operator
1876 OPEN_PARENS CLOSE_PARENS
1878 Report.Error (1535, (Location) $2, "Overloaded unary operator `{0}' takes one parameter",
1879 Operator.GetName ((Operator.OpType) $3));
1884 overloadable_operator
1886 : BANG { $$ = Operator.OpType.LogicalNot; }
1887 | TILDE { $$ = Operator.OpType.OnesComplement; }
1888 | OP_INC { $$ = Operator.OpType.Increment; }
1889 | OP_DEC { $$ = Operator.OpType.Decrement; }
1890 | TRUE { $$ = Operator.OpType.True; }
1891 | FALSE { $$ = Operator.OpType.False; }
1892 // Unary and binary:
1893 | PLUS { $$ = Operator.OpType.Addition; }
1894 | MINUS { $$ = Operator.OpType.Subtraction; }
1896 | STAR { $$ = Operator.OpType.Multiply; }
1897 | DIV { $$ = Operator.OpType.Division; }
1898 | PERCENT { $$ = Operator.OpType.Modulus; }
1899 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1900 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1901 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1902 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1903 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1904 | OP_EQ { $$ = Operator.OpType.Equality; }
1905 | OP_NE { $$ = Operator.OpType.Inequality; }
1906 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1907 | OP_LT { $$ = Operator.OpType.LessThan; }
1908 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1909 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1912 conversion_operator_declarator
1913 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1915 LocatedToken lt = (LocatedToken) $6;
1916 Parameter [] pars = new Parameter [1];
1918 pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1920 current_local_parameters = new Parameters (pars, null);
1922 if (RootContext.Documentation != null) {
1923 tmpComment = Lexer.consume_doc_comment ();
1924 Lexer.doc_state = XmlCommentState.NotAllowed;
1927 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, lt.Value,
1928 null, null, (Location) $2);
1930 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1932 LocatedToken lt = (LocatedToken) $6;
1933 Parameter [] pars = new Parameter [1];
1935 pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1937 current_local_parameters = new Parameters (pars, null);
1939 if (RootContext.Documentation != null) {
1940 tmpComment = Lexer.consume_doc_comment ();
1941 Lexer.doc_state = XmlCommentState.NotAllowed;
1944 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, lt.Value,
1945 null, null, (Location) $2);
1949 syntax_error ((Location) $1, "'operator' expected");
1953 syntax_error ((Location) $1, "'operator' expected");
1957 constructor_declaration
1960 constructor_declarator
1963 Constructor c = (Constructor) $3;
1964 c.Block = (ToplevelBlock) $4;
1965 c.OptAttributes = (Attributes) $1;
1966 c.ModFlags = (int) $2;
1968 if (RootContext.Documentation != null)
1969 c.DocComment = ConsumeStoredComment ();
1971 if (c.Name == current_container.Basename){
1972 if ((c.ModFlags & Modifiers.STATIC) != 0){
1973 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1974 Report.Error (515, c.Location,
1975 "`{0}': access modifiers are not allowed on static constructors",
1976 c.GetSignatureForError ());
1979 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1981 if (c.Initializer != null){
1982 Report.Error (514, c.Location,
1983 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
1984 c.GetSignatureForError ());
1987 if (!c.Parameters.Empty){
1988 Report.Error (132, c.Location,
1989 "`{0}': The static constructor must be parameterless", c.GetSignatureForError ());
1992 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1995 // We let another layer check the validity of the constructor.
1996 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1999 current_container.AddConstructor (c);
2001 current_local_parameters = null;
2002 if (RootContext.Documentation != null)
2003 Lexer.doc_state = XmlCommentState.Allowed;
2007 constructor_declarator
2010 if (RootContext.Documentation != null) {
2011 tmpComment = Lexer.consume_doc_comment ();
2012 Lexer.doc_state = XmlCommentState.Allowed;
2015 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2017 current_local_parameters = (Parameters) $4;
2019 opt_constructor_initializer
2021 LocatedToken lt = (LocatedToken) $1;
2022 $$ = new Constructor (current_class, lt.Value, 0, (Parameters) $4,
2023 (ConstructorInitializer) $7, lt.Location);
2029 | SEMICOLON { $$ = null; }
2032 opt_constructor_initializer
2033 : /* empty */ { $$ = null; }
2034 | constructor_initializer
2037 constructor_initializer
2038 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
2040 $$ = new ConstructorBaseInitializer ((ArrayList) $4, (Location) $2);
2042 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
2044 $$ = new ConstructorThisInitializer ((ArrayList) $4, (Location) $2);
2047 Report.Error (1018, (Location) $1, "Keyword this or base expected");
2053 : /* EMPTY */ { $$ = 0; }
2054 | UNSAFE { $$ = Modifiers.UNSAFE; }
2055 | EXTERN { $$ = Modifiers.EXTERN; }
2058 destructor_declaration
2059 : opt_attributes opt_finalizer TILDE
2061 if (RootContext.Documentation != null) {
2062 tmpComment = Lexer.consume_doc_comment ();
2063 Lexer.doc_state = XmlCommentState.NotAllowed;
2066 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2068 LocatedToken lt = (LocatedToken) $5;
2069 if (lt.Value != current_container.Basename){
2070 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2071 } else if (current_container.Kind != Kind.Class){
2072 Report.Error (575, lt.Location, "Only class types can contain destructor");
2074 Location l = lt.Location;
2077 if (!RootContext.StdLib && current_container.Name == "System.Object")
2078 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2080 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2082 Method d = new Destructor (
2083 current_class, TypeManager.system_void_expr, m, "Finalize",
2084 new Parameters (null, null), (Attributes) $1, l);
2085 if (RootContext.Documentation != null)
2086 d.DocComment = ConsumeStoredComment ();
2088 d.Block = (ToplevelBlock) $8;
2089 current_container.AddMethod (d);
2097 EVENT type variable_declarators SEMICOLON
2099 foreach (VariableDeclaration var in (ArrayList) $5) {
2101 MemberName name = new MemberName (var.identifier,
2104 Event e = new EventField (
2105 current_class, (Expression) $4, (int) $2, false, name,
2106 var.expression_or_array_initializer, (Attributes) $1);
2108 current_container.AddEvent (e);
2110 if (RootContext.Documentation != null) {
2111 e.DocComment = Lexer.consume_doc_comment ();
2112 Lexer.doc_state = XmlCommentState.Allowed;
2118 EVENT type namespace_or_type_name
2121 implicit_value_parameter_type = (Expression) $4;
2122 lexer.EventParsing = true;
2124 event_accessor_declarations
2126 lexer.EventParsing = false;
2130 MemberName name = (MemberName) $5;
2133 Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2134 current_container.Name, name);
2137 Pair pair = (Pair) $8;
2138 if (pair.First == null || pair.Second == null)
2139 // CS0073 is already reported, so no CS0065 here.
2142 Event e = new EventProperty (
2143 current_class, (Expression) $4, (int) $2, false, name, null,
2144 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second);
2145 if (RootContext.Documentation != null) {
2146 e.DocComment = Lexer.consume_doc_comment ();
2147 Lexer.doc_state = XmlCommentState.Allowed;
2150 current_container.AddEvent (e);
2151 implicit_value_parameter_type = null;
2155 | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
2156 MemberName mn = (MemberName) $5;
2158 if (mn.Left != null)
2159 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2161 Report.Error (71, mn.Location, "Event declaration should use property syntax");
2163 if (RootContext.Documentation != null)
2164 Lexer.doc_state = XmlCommentState.Allowed;
2168 event_accessor_declarations
2169 : add_accessor_declaration remove_accessor_declaration
2171 $$ = new Pair ($1, $2);
2173 | remove_accessor_declaration add_accessor_declaration
2175 $$ = new Pair ($2, $1);
2177 | add_accessor_declaration { $$ = null; }
2178 | remove_accessor_declaration { $$ = null; }
2181 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2187 add_accessor_declaration
2188 : opt_attributes ADD
2190 Parameter [] args = new Parameter [1];
2191 Parameter implicit_value_parameter = new Parameter (
2192 implicit_value_parameter_type, "value",
2193 Parameter.Modifier.NONE, null, (Location) $2);
2195 args [0] = implicit_value_parameter;
2197 current_local_parameters = new Parameters (args, null);
2198 lexer.EventParsing = false;
2202 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2203 lexer.EventParsing = true;
2205 | opt_attributes ADD error {
2206 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2209 | opt_attributes modifiers ADD {
2210 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2215 remove_accessor_declaration
2216 : opt_attributes REMOVE
2218 Parameter [] args = new Parameter [1];
2219 Parameter implicit_value_parameter = new Parameter (
2220 implicit_value_parameter_type, "value",
2221 Parameter.Modifier.NONE, null, (Location) $2);
2223 args [0] = implicit_value_parameter;
2225 current_local_parameters = new Parameters (args, null);
2226 lexer.EventParsing = false;
2230 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2231 lexer.EventParsing = true;
2233 | opt_attributes REMOVE error {
2234 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2237 | opt_attributes modifiers REMOVE {
2238 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2244 : opt_attributes opt_modifiers indexer_declarator
2247 IndexerDeclaration decl = (IndexerDeclaration) $3;
2249 implicit_value_parameter_type = decl.type;
2251 lexer.PropertyParsing = true;
2252 parsing_indexer = true;
2254 indexer_parameters = decl.param_list;
2256 accessor_declarations
2258 lexer.PropertyParsing = false;
2259 has_get = has_set = false;
2260 parsing_indexer = false;
2267 // The signature is computed from the signature of the indexer. Look
2268 // at section 3.6 on the spec
2270 IndexerDeclaration decl = (IndexerDeclaration) $3;
2271 Location loc = decl.location;
2272 Pair pair = (Pair) $6;
2273 Accessor get_block = (Accessor) pair.First;
2274 Accessor set_block = (Accessor) pair.Second;
2277 if (decl.interface_type != null)
2278 name = new MemberName (decl.interface_type, TypeContainer.DefaultIndexerName, loc);
2280 name = new MemberName (TypeContainer.DefaultIndexerName, loc);
2282 indexer = new Indexer (current_class, decl.type, name,
2283 (int) $2, false, decl.param_list, (Attributes) $1,
2284 get_block, set_block);
2286 if (RootContext.Documentation != null)
2287 indexer.DocComment = ConsumeStoredComment ();
2289 current_container.AddIndexer (indexer);
2291 current_local_parameters = null;
2292 implicit_value_parameter_type = null;
2293 indexer_parameters = null;
2298 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2300 Parameters pars = (Parameters) $4;
2301 if (pars.HasArglist) {
2302 // "__arglist is not valid in this context"
2303 Report.Error (1669, (Location) $2, "__arglist is not valid in this context");
2304 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2305 Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
2307 if (RootContext.Documentation != null) {
2308 tmpComment = Lexer.consume_doc_comment ();
2309 Lexer.doc_state = XmlCommentState.Allowed;
2312 $$ = new IndexerDeclaration ((Expression) $1, null, pars, (Location) $2);
2314 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2316 Parameters pars = (Parameters) $6;
2318 if (pars.HasArglist) {
2319 // "__arglist is not valid in this context"
2320 Report.Error (1669, (Location) $4, "__arglist is not valid in this context");
2321 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2322 Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
2325 MemberName name = (MemberName) $2;
2326 $$ = new IndexerDeclaration ((Expression) $1, name, pars, (Location) $4);
2328 if (RootContext.Documentation != null) {
2329 tmpComment = Lexer.consume_doc_comment ();
2330 Lexer.doc_state = XmlCommentState.Allowed;
2341 if (RootContext.Documentation != null)
2342 enumTypeComment = Lexer.consume_doc_comment ();
2347 LocatedToken lt = (LocatedToken) $5;
2348 Location enum_location = lt.Location;
2351 Report.Error (267, lt.Location, "The partial modifier can only appear immediately before `class', `struct' or `interface'");
2352 break; // assumes that the parser put us in a switch
2355 MemberName name = MakeName (new MemberName (lt.Value, enum_location));
2356 Enum e = new Enum (current_namespace, current_class, (Expression) $6, (int) $2,
2357 name, (Attributes) $1);
2359 if (RootContext.Documentation != null)
2360 e.DocComment = enumTypeComment;
2363 EnumMember em = null;
2364 foreach (VariableDeclaration ev in (ArrayList) $8) {
2365 em = new EnumMember (e, em, (Expression) ev.expression_or_array_initializer,
2366 new MemberName (ev.identifier, ev.Location), ev.OptAttributes);
2368 // if (RootContext.Documentation != null)
2369 em.DocComment = ev.DocComment;
2371 e.AddEnumMember (em);
2374 current_container.AddEnum (e);
2375 RootContext.Tree.RecordDecl (current_namespace.NS, name, e);
2382 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2383 | COLON type { $$ = $2; }
2389 if (RootContext.Documentation != null)
2390 Lexer.doc_state = XmlCommentState.Allowed;
2392 opt_enum_member_declarations
2394 // here will be evaluated after CLOSE_BLACE is consumed.
2395 if (RootContext.Documentation != null)
2396 Lexer.doc_state = XmlCommentState.Allowed;
2404 opt_enum_member_declarations
2405 : /* empty */ { $$ = new ArrayList (4); }
2406 | enum_member_declarations opt_comma { $$ = $1; }
2409 enum_member_declarations
2410 : enum_member_declaration
2412 ArrayList l = new ArrayList (4);
2417 | enum_member_declarations COMMA enum_member_declaration
2419 ArrayList l = (ArrayList) $1;
2427 enum_member_declaration
2428 : opt_attributes IDENTIFIER
2430 VariableDeclaration vd = new VariableDeclaration (
2431 (LocatedToken) $2, null, (Attributes) $1);
2433 if (RootContext.Documentation != null) {
2434 vd.DocComment = Lexer.consume_doc_comment ();
2435 Lexer.doc_state = XmlCommentState.Allowed;
2440 | opt_attributes IDENTIFIER
2442 if (RootContext.Documentation != null) {
2443 tmpComment = Lexer.consume_doc_comment ();
2444 Lexer.doc_state = XmlCommentState.NotAllowed;
2449 VariableDeclaration vd = new VariableDeclaration (
2450 (LocatedToken) $2, $5, (Attributes) $1);
2452 if (RootContext.Documentation != null)
2453 vd.DocComment = ConsumeStoredComment ();
2459 delegate_declaration
2462 DELEGATE type member_name
2463 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2466 MemberName name = MakeName ((MemberName) $5);
2467 Delegate del = new Delegate (current_namespace, current_class, (Expression) $4,
2468 (int) $2, name, (Parameters) $7, (Attributes) $1);
2470 if (RootContext.Documentation != null) {
2471 del.DocComment = Lexer.consume_doc_comment ();
2472 Lexer.doc_state = XmlCommentState.Allowed;
2475 current_container.AddDelegate (del);
2476 RootContext.Tree.RecordDecl (current_namespace.NS, name, del);
2481 namespace_or_type_name
2483 | IDENTIFIER DOUBLE_COLON IDENTIFIER {
2484 LocatedToken lt1 = (LocatedToken) $1;
2485 LocatedToken lt2 = (LocatedToken) $3;
2486 $$ = new MemberName (lt1.Value, lt2.Value, lt2.Location);
2488 | namespace_or_type_name DOT IDENTIFIER {
2489 LocatedToken lt = (LocatedToken) $3;
2490 $$ = new MemberName ((MemberName) $1, lt.Value);
2496 LocatedToken lt = (LocatedToken) $1;
2497 $$ = new MemberName (lt.Value, lt.Location);
2502 * Before you think of adding a return_type, notice that we have been
2503 * using two rules in the places where it matters (one rule using type
2504 * and another identical one that uses VOID as the return type). This
2505 * gets rid of a shift/reduce couple
2508 : namespace_or_type_name
2510 MemberName name = (MemberName) $1;
2511 $$ = name.GetTypeExpression ();
2523 // Note that here only unmanaged types are allowed but we
2524 // can't perform checks during this phase - we do it during
2525 // semantic analysis.
2527 $$ = new ComposedCast ((Expression) $1, "*", Lexer.Location);
2531 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2537 | non_expression_type rank_specifier
2539 $$ = new ComposedCast ((Expression) $1, (string) $2);
2541 | non_expression_type STAR
2543 $$ = new ComposedCast ((Expression) $1, "*");
2545 | expression rank_specifiers
2547 $$ = new ComposedCast ((Expression) $1, (string) $2);
2551 $$ = new ComposedCast ((Expression) $1, "*");
2555 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2556 // through this different path
2558 | multiplicative_expression STAR
2560 $$ = new ComposedCast ((Expression) $1, "*");
2567 ArrayList types = new ArrayList (4);
2572 | type_list COMMA type
2574 ArrayList types = (ArrayList) $1;
2582 * replaces all the productions for isolating the various
2583 * simple types, but we need this to reuse it easily in local_variable_type
2586 : OBJECT { $$ = TypeManager.system_object_expr; }
2587 | STRING { $$ = TypeManager.system_string_expr; }
2588 | BOOL { $$ = TypeManager.system_boolean_expr; }
2589 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2590 | FLOAT { $$ = TypeManager.system_single_expr; }
2591 | DOUBLE { $$ = TypeManager.system_double_expr; }
2596 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2597 | BYTE { $$ = TypeManager.system_byte_expr; }
2598 | SHORT { $$ = TypeManager.system_int16_expr; }
2599 | USHORT { $$ = TypeManager.system_uint16_expr; }
2600 | INT { $$ = TypeManager.system_int32_expr; }
2601 | UINT { $$ = TypeManager.system_uint32_expr; }
2602 | LONG { $$ = TypeManager.system_int64_expr; }
2603 | ULONG { $$ = TypeManager.system_uint64_expr; }
2604 | CHAR { $$ = TypeManager.system_char_expr; }
2605 | VOID { $$ = TypeManager.system_void_expr; }
2609 : type rank_specifiers
2611 $$ = new ComposedCast ((Expression) $1, (string) $2);
2616 // Expressions, section 7.5
2626 MemberName mn = (MemberName) $1;
2627 $$ = mn.GetTypeExpression ();
2629 | IDENTIFIER DOUBLE_COLON IDENTIFIER
2631 LocatedToken lt1 = (LocatedToken) $1;
2632 LocatedToken lt2 = (LocatedToken) $3;
2633 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, lt2.Location);
2635 | parenthesized_expression
2637 | invocation_expression
2641 | post_increment_expression
2642 | post_decrement_expression
2646 | checked_expression
2647 | unchecked_expression
2648 | pointer_member_access
2649 | anonymous_method_expression
2656 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
2657 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); }
2658 | NULL { $$ = new NullLiteral (lexer.Location); }
2662 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
2663 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
2664 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
2669 object v = lexer.Value;
2672 $$ = new IntLiteral ((int) v, lexer.Location);
2673 } else if (v is uint)
2674 $$ = new UIntLiteral ((UInt32) v, lexer.Location);
2676 $$ = new LongLiteral ((Int64) v, lexer.Location);
2677 else if (v is ulong)
2678 $$ = new ULongLiteral ((UInt64) v, lexer.Location);
2680 Console.WriteLine ("OOPS. Unexpected result from scanner");
2685 : TRUE { $$ = new BoolLiteral (true, lexer.Location); }
2686 | FALSE { $$ = new BoolLiteral (false, lexer.Location); }
2689 parenthesized_expression_0
2690 : OPEN_PARENS expression CLOSE_PARENS
2693 lexer.Deambiguate_CloseParens ();
2694 // After this, the next token returned is one of
2695 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2696 // or CLOSE_PARENS_MINUS.
2700 parenthesized_expression
2701 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2705 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2707 // If a parenthesized expression is followed by a minus, we need to wrap
2708 // the expression inside a ParenthesizedExpression for the CS0075 check
2709 // in Binary.DoResolve().
2710 $$ = new ParenthesizedExpression ((Expression) $1);
2715 : primary_expression DOT IDENTIFIER
2717 LocatedToken lt = (LocatedToken) $3;
2718 $$ = new MemberAccess ((Expression) $1, lt.Value, lt.Location);
2720 | predefined_type DOT IDENTIFIER
2722 LocatedToken lt = (LocatedToken) $3;
2723 $$ = new MemberAccess ((Expression) $1, lt.Value, lt.Location);
2731 invocation_expression
2732 : primary_expression {
2733 $$ = lexer.Location;
2734 } OPEN_PARENS opt_argument_list CLOSE_PARENS
2737 Location l = (Location) $3;
2738 Report.Error (1, l, "Parse error");
2740 $$ = new Invocation ((Expression) $1, (ArrayList) $4);
2742 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2744 $$ = new Invocation ((Expression) $1, new ArrayList ());
2746 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2748 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
2750 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2752 ArrayList args = new ArrayList (1);
2754 $$ = new Invocation ((Expression) $1, args);
2756 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2758 ArrayList args = ((ArrayList) $4);
2760 $$ = new Invocation ((Expression) $1, args);
2765 : /* empty */ { $$ = null; }
2772 ArrayList list = new ArrayList (4);
2776 | argument_list COMMA argument
2778 ArrayList list = (ArrayList) $1;
2782 | argument_list error {
2783 CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
2791 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2793 | non_simple_argument
2800 : REF variable_reference
2802 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2804 | OUT variable_reference
2806 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2808 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2810 ArrayList list = (ArrayList) $3;
2811 Argument[] args = new Argument [list.Count];
2812 list.CopyTo (args, 0);
2814 Expression expr = new Arglist (args, (Location) $1);
2815 $$ = new Argument (expr, Argument.AType.Expression);
2819 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
2824 : expression { note ("section 5.4"); $$ = $1; }
2828 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2830 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
2832 | primary_expression rank_specifiers
2834 // So the super-trick is that primary_expression
2835 // can only be either a SimpleName or a MemberAccess.
2836 // The MemberAccess case arises when you have a fully qualified type-name like :
2838 // SimpleName is when you have
2841 Expression expr = (Expression) $1;
2842 if (expr is ComposedCast){
2843 $$ = new ComposedCast (expr, (string) $2);
2844 } else if (!(expr is SimpleName || expr is MemberAccess || expr is QualifiedAliasMember)){
2845 Error_ExpectingTypeName (expr);
2846 $$ = TypeManager.system_object_expr;
2849 // So we extract the string corresponding to the SimpleName
2852 $$ = new ComposedCast (expr, (string) $2);
2860 ArrayList list = new ArrayList (4);
2864 | expression_list COMMA expression
2866 ArrayList list = (ArrayList) $1;
2875 $$ = new This (current_block, (Location) $1);
2880 : BASE DOT IDENTIFIER
2882 LocatedToken lt = (LocatedToken) $3;
2883 $$ = new BaseAccess (lt.Value, lt.Location);
2885 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2887 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
2890 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
2895 post_increment_expression
2896 : primary_expression OP_INC
2898 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2899 (Expression) $1, (Location) $2);
2903 post_decrement_expression
2904 : primary_expression OP_DEC
2906 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2907 (Expression) $1, (Location) $2);
2912 : object_or_delegate_creation_expression
2913 | array_creation_expression
2916 object_or_delegate_creation_expression
2917 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2919 $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
2923 array_creation_expression
2924 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2926 opt_array_initializer
2928 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
2930 | NEW type rank_specifiers array_initializer
2932 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, (Location) $1);
2936 Report.Error (1031, (Location) $1, "Type expected");
2941 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
2958 : rank_specifier opt_rank_specifier
2960 $$ = (string) $2 + (string) $1;
2965 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2967 $$ = "[" + (string) $2 + "]";
2987 | dim_separators COMMA
2989 $$ = (string) $1 + ",";
2993 opt_array_initializer
3005 : OPEN_BRACE CLOSE_BRACE
3007 ArrayList list = new ArrayList (4);
3010 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3012 $$ = (ArrayList) $2;
3016 variable_initializer_list
3017 : variable_initializer
3019 ArrayList list = new ArrayList (4);
3023 | variable_initializer_list COMMA variable_initializer
3025 ArrayList list = (ArrayList) $1;
3032 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
3034 $$ = new TypeOfVoid ((Location) $1);
3036 | TYPEOF OPEN_PARENS type CLOSE_PARENS
3038 $$ = new TypeOf ((Expression) $3, (Location) $1);
3043 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3044 $$ = new SizeOf ((Expression) $3, (Location) $1);
3049 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3051 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3055 unchecked_expression
3056 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3058 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3062 pointer_member_access
3063 : primary_expression OP_PTR IDENTIFIER
3066 LocatedToken lt = (LocatedToken) $3;
3068 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
3069 $$ = new MemberAccess (deref, lt.Value, lt.Location);
3073 anonymous_method_expression
3074 : DELEGATE opt_anonymous_method_signature
3076 oob_stack.Push (current_local_parameters);
3077 current_local_parameters = (Parameters)$2;
3079 // Force the next block to be created as a ToplevelBlock
3080 oob_stack.Push (current_block);
3081 oob_stack.Push (top_current_block);
3082 current_block = null;
3086 Location loc = (Location) $1;
3087 top_current_block = (Block) oob_stack.Pop ();
3088 current_block = (Block) oob_stack.Pop ();
3089 if (RootContext.Version == LanguageVersion.ISO_1){
3090 Report.FeatureIsNotStandardized (loc, "anonymous methods");
3093 ToplevelBlock anon_block = (ToplevelBlock) $4;
3095 anon_block.Parent = current_block;
3096 $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block,
3099 current_local_parameters = (Parameters) oob_stack.Pop ();
3103 opt_anonymous_method_signature
3104 : /* empty */ { $$ = null; }
3105 | anonymous_method_signature
3108 anonymous_method_signature
3109 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3112 $$ = Parameters.EmptyReadOnlyParameters;
3114 ArrayList par_list = (ArrayList) $2;
3115 Parameter [] pars = new Parameter [par_list.Count];
3116 par_list.CopyTo (pars);
3117 $$ = new Parameters (pars, null);
3122 opt_anonymous_method_parameter_list
3123 : /* empty */ { $$ = null; }
3124 | anonymous_method_parameter_list { $$ = $1; }
3127 anonymous_method_parameter_list
3128 : anonymous_method_parameter
3130 ArrayList a = new ArrayList (4);
3134 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3136 ArrayList a = (ArrayList) $1;
3142 anonymous_method_parameter
3143 : opt_parameter_modifier type IDENTIFIER {
3144 LocatedToken lt = (LocatedToken) $3;
3145 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3147 | PARAMS type IDENTIFIER {
3148 Report.Error (1670, ((LocatedToken) $3).Location, "The `params' modifier is not allowed in anonymous method declaration");
3154 : primary_expression
3155 | BANG prefixed_unary_expression
3157 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3159 | TILDE prefixed_unary_expression
3161 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3167 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3169 $$ = new Cast ((Expression) $1, (Expression) $3);
3171 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3173 $$ = new Cast ((Expression) $1, (Expression) $3);
3179 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3181 // TODO: wrong location
3182 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3187 // The idea to split this out is from Rhys' grammar
3188 // to solve the problem with casts.
3190 prefixed_unary_expression
3192 | PLUS prefixed_unary_expression
3194 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3196 | MINUS prefixed_unary_expression
3198 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3200 | OP_INC prefixed_unary_expression
3202 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3203 (Expression) $2, (Location) $1);
3205 | OP_DEC prefixed_unary_expression
3207 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3208 (Expression) $2, (Location) $1);
3210 | STAR prefixed_unary_expression
3212 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, (Location) $1);
3214 | BITWISE_AND prefixed_unary_expression
3216 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3220 pre_increment_expression
3221 : OP_INC prefixed_unary_expression
3223 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3224 (Expression) $2, (Location) $1);
3228 pre_decrement_expression
3229 : OP_DEC prefixed_unary_expression
3231 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3232 (Expression) $2, (Location) $1);
3236 multiplicative_expression
3237 : prefixed_unary_expression
3238 | multiplicative_expression STAR prefixed_unary_expression
3240 $$ = new Binary (Binary.Operator.Multiply,
3241 (Expression) $1, (Expression) $3);
3243 | multiplicative_expression DIV prefixed_unary_expression
3245 $$ = new Binary (Binary.Operator.Division,
3246 (Expression) $1, (Expression) $3);
3248 | multiplicative_expression PERCENT prefixed_unary_expression
3250 $$ = new Binary (Binary.Operator.Modulus,
3251 (Expression) $1, (Expression) $3);
3256 : multiplicative_expression
3257 | additive_expression PLUS multiplicative_expression
3259 $$ = new Binary (Binary.Operator.Addition,
3260 (Expression) $1, (Expression) $3);
3262 | additive_expression MINUS multiplicative_expression
3264 $$ = new Binary (Binary.Operator.Subtraction,
3265 (Expression) $1, (Expression) $3);
3270 : additive_expression
3271 | shift_expression OP_SHIFT_LEFT additive_expression
3273 $$ = new Binary (Binary.Operator.LeftShift,
3274 (Expression) $1, (Expression) $3);
3276 | shift_expression OP_SHIFT_RIGHT additive_expression
3278 $$ = new Binary (Binary.Operator.RightShift,
3279 (Expression) $1, (Expression) $3);
3283 relational_expression
3285 | relational_expression OP_LT shift_expression
3287 $$ = new Binary (Binary.Operator.LessThan,
3288 (Expression) $1, (Expression) $3);
3290 | relational_expression OP_GT shift_expression
3292 $$ = new Binary (Binary.Operator.GreaterThan,
3293 (Expression) $1, (Expression) $3);
3295 | relational_expression OP_LE shift_expression
3297 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3298 (Expression) $1, (Expression) $3);
3300 | relational_expression OP_GE shift_expression
3302 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3303 (Expression) $1, (Expression) $3);
3305 | relational_expression IS type
3307 $$ = new Is ((Expression) $1, (Expression) $3, (Location) $2);
3309 | relational_expression AS type
3311 $$ = new As ((Expression) $1, (Expression) $3, (Location) $2);
3316 : relational_expression
3317 | equality_expression OP_EQ relational_expression
3319 $$ = new Binary (Binary.Operator.Equality,
3320 (Expression) $1, (Expression) $3);
3322 | equality_expression OP_NE relational_expression
3324 $$ = new Binary (Binary.Operator.Inequality,
3325 (Expression) $1, (Expression) $3);
3330 : equality_expression
3331 | and_expression BITWISE_AND equality_expression
3333 $$ = new Binary (Binary.Operator.BitwiseAnd,
3334 (Expression) $1, (Expression) $3);
3338 exclusive_or_expression
3340 | exclusive_or_expression CARRET and_expression
3342 $$ = new Binary (Binary.Operator.ExclusiveOr,
3343 (Expression) $1, (Expression) $3);
3347 inclusive_or_expression
3348 : exclusive_or_expression
3349 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3351 $$ = new Binary (Binary.Operator.BitwiseOr,
3352 (Expression) $1, (Expression) $3);
3356 conditional_and_expression
3357 : inclusive_or_expression
3358 | conditional_and_expression OP_AND inclusive_or_expression
3360 $$ = new Binary (Binary.Operator.LogicalAnd,
3361 (Expression) $1, (Expression) $3);
3365 conditional_or_expression
3366 : conditional_and_expression
3367 | conditional_or_expression OP_OR conditional_and_expression
3369 $$ = new Binary (Binary.Operator.LogicalOr,
3370 (Expression) $1, (Expression) $3);
3374 conditional_expression
3375 : conditional_or_expression
3376 | conditional_or_expression INTERR expression COLON expression
3378 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
3382 assignment_expression
3383 : prefixed_unary_expression ASSIGN expression
3385 $$ = new Assign ((Expression) $1, (Expression) $3);
3387 | prefixed_unary_expression OP_MULT_ASSIGN expression
3389 $$ = new CompoundAssign (
3390 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
3392 | prefixed_unary_expression OP_DIV_ASSIGN expression
3394 $$ = new CompoundAssign (
3395 Binary.Operator.Division, (Expression) $1, (Expression) $3);
3397 | prefixed_unary_expression OP_MOD_ASSIGN expression
3399 $$ = new CompoundAssign (
3400 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
3402 | prefixed_unary_expression OP_ADD_ASSIGN expression
3404 $$ = new CompoundAssign (
3405 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
3407 | prefixed_unary_expression OP_SUB_ASSIGN expression
3409 $$ = new CompoundAssign (
3410 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3412 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3414 $$ = new CompoundAssign (
3415 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
3417 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3419 $$ = new CompoundAssign (
3420 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
3422 | prefixed_unary_expression OP_AND_ASSIGN expression
3424 $$ = new CompoundAssign (
3425 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
3427 | prefixed_unary_expression OP_OR_ASSIGN expression
3429 $$ = new CompoundAssign (
3430 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
3432 | prefixed_unary_expression OP_XOR_ASSIGN expression
3434 $$ = new CompoundAssign (
3435 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
3440 : conditional_expression
3441 | assignment_expression
3461 MemberName name = MakeName ((MemberName) $5);
3462 int mod_flags = (int) $2;
3465 ClassPart part = PartialContainer.CreatePart (
3466 current_namespace, current_class, name, mod_flags,
3467 (Attributes) $1, Kind.Class, (Location) $3);
3469 current_container = part.PartialContainer;
3470 current_class = part;
3472 if ((mod_flags & Modifiers.STATIC) != 0) {
3473 current_class = new StaticClass (
3474 current_namespace, current_class, name,
3475 mod_flags, (Attributes) $1);
3477 current_class = new Class (
3478 current_namespace, current_class, name,
3479 mod_flags, (Attributes) $1);
3482 current_container.AddClassOrStruct (current_class);
3483 current_container = current_class;
3484 RootContext.Tree.RecordDecl (current_namespace.NS, name, current_class);
3490 if (current_class.Name == "System.Object") {
3491 Report.Error (537, current_class.Location,
3492 "The class System.Object cannot have a base " +
3493 "class or implement an interface.");
3495 current_class.Bases = (ArrayList) $7;
3498 if (RootContext.Documentation != null) {
3499 current_class.DocComment = Lexer.consume_doc_comment ();
3500 Lexer.doc_state = XmlCommentState.Allowed;
3505 if (RootContext.Documentation != null)
3506 Lexer.doc_state = XmlCommentState.Allowed;
3510 $$ = pop_current_class ();
3518 { $$ = $1; } // location
3522 : /* empty */ { $$ = (int) 0; }
3528 | modifiers modifier
3533 if ((m1 & m2) != 0) {
3534 Location l = lexer.Location;
3535 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
3537 $$ = (int) (m1 | m2);
3542 : NEW { $$ = Modifiers.NEW; }
3543 | PUBLIC { $$ = Modifiers.PUBLIC; }
3544 | PROTECTED { $$ = Modifiers.PROTECTED; }
3545 | INTERNAL { $$ = Modifiers.INTERNAL; }
3546 | PRIVATE { $$ = Modifiers.PRIVATE; }
3547 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3548 | SEALED { $$ = Modifiers.SEALED; }
3549 | STATIC { $$ = Modifiers.STATIC; }
3550 | READONLY { $$ = Modifiers.READONLY; }
3551 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3552 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3553 | EXTERN { $$ = Modifiers.EXTERN; }
3554 | VOLATILE { $$ = Modifiers.VOLATILE; }
3555 | UNSAFE { $$ = Modifiers.UNSAFE; }
3559 : /* empty */ { $$ = null; }
3560 | class_base { $$ = $1; }
3564 : COLON type_list { $$ = $2; }
3572 // A block is "contained" on the following places:
3574 // property_declaration as part of the accessor body (get/set)
3575 // operator_declaration
3576 // constructor_declaration
3577 // destructor_declaration
3578 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3583 if (current_block == null){
3584 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, (Location) $1);
3585 top_current_block = current_block;
3587 current_block = new Block (current_block, (Location) $1, Location.Null);
3590 opt_statement_list CLOSE_BRACE
3592 while (current_block.Implicit)
3593 current_block = current_block.Parent;
3595 current_block.SetEndLocation ((Location) $4);
3596 current_block = current_block.Parent;
3597 if (current_block == null)
3598 top_current_block = null;
3609 | statement_list statement
3613 : declaration_statement
3615 if ($1 != null && (Block) $1 != current_block){
3616 current_block.AddStatement ((Statement) $1);
3617 current_block = (Block) $1;
3620 | valid_declaration_statement
3622 current_block.AddStatement ((Statement) $1);
3627 valid_declaration_statement
3630 | expression_statement
3631 | selection_statement
3632 | iteration_statement
3636 | unchecked_statement
3644 : valid_declaration_statement
3645 | declaration_statement
3647 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3652 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3660 $$ = EmptyStatement.Value;
3667 LocatedToken lt = (LocatedToken) $1;
3668 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
3670 if (current_block.AddLabel (lt.Value, labeled, lt.Location))
3671 current_block.AddStatement (labeled);
3676 declaration_statement
3677 : local_variable_declaration SEMICOLON
3680 DictionaryEntry de = (DictionaryEntry) $1;
3681 Expression e = (Expression) de.Key;
3683 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
3687 | local_constant_declaration SEMICOLON
3690 DictionaryEntry de = (DictionaryEntry) $1;
3692 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3698 * The following is from Rhys' grammar:
3699 * > Types in local variable declarations must be recognized as
3700 * > expressions to prevent reduce/reduce errors in the grammar.
3701 * > The expressions are converted into types during semantic analysis.
3704 : primary_expression opt_rank_specifier
3706 // FIXME: Do something smart here regarding the composition of the type.
3708 // Ok, the above "primary_expression" is there to get rid of
3709 // both reduce/reduce and shift/reduces in the grammar, it should
3710 // really just be "type_name". If you use type_name, a reduce/reduce
3711 // creeps up. If you use namespace_or_type_name (which is all we need
3712 // really) two shift/reduces appear.
3715 // So the super-trick is that primary_expression
3716 // can only be either a SimpleName or a MemberAccess.
3717 // The MemberAccess case arises when you have a fully qualified type-name like :
3719 // SimpleName is when you have
3722 Expression expr = (Expression) $1;
3723 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3724 Error_ExpectingTypeName (expr);
3728 // So we extract the string corresponding to the SimpleName
3732 if ((string) $2 == "")
3735 $$ = new ComposedCast ((Expression) $1, (string) $2);
3738 | builtin_types opt_rank_specifier
3740 if ((string) $2 == "")
3743 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3747 local_variable_pointer_type
3748 : primary_expression STAR
3750 Expression expr = (Expression) $1;
3752 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3753 Error_ExpectingTypeName (expr);
3757 $$ = new ComposedCast ((Expression) $1, "*");
3759 | builtin_types STAR
3761 $$ = new ComposedCast ((Expression) $1, "*");
3765 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
3767 | local_variable_pointer_type STAR
3769 $$ = new ComposedCast ((Expression) $1, "*");
3773 local_variable_declaration
3774 : local_variable_type variable_declarators
3777 $$ = new DictionaryEntry ($1, $2);
3781 | local_variable_pointer_type opt_rank_specifier variable_declarators
3786 if ((string) $2 == "")
3787 t = (Expression) $1;
3789 t = new ComposedCast ((Expression) $1, (string) $2);
3790 $$ = new DictionaryEntry (t, $3);
3796 local_constant_declaration
3797 : CONST local_variable_type constant_declarators
3800 $$ = new DictionaryEntry ($2, $3);
3806 expression_statement
3807 : statement_expression SEMICOLON
3814 // We have to do the wrapping here and not in the case above,
3815 // because statement_expression is used for example in for_statement
3817 statement_expression
3818 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
3819 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
3820 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
3821 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
3822 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
3823 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
3824 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
3826 Report.Error (1002, GetLocation ($1), "Expecting `;'");
3831 object_creation_expression
3832 : object_or_delegate_creation_expression
3833 { note ("complain if this is a delegate maybe?"); }
3842 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
3845 Location l = (Location) $1;
3847 $$ = new If ((Expression) $3, (Statement) $5, l);
3849 if (RootContext.WarningLevel >= 3){
3850 // FIXME: location for warning should be loc property of $5.
3851 if ($5 == EmptyStatement.Value)
3852 Report.Warning (642, l, "Possible mistaken empty statement");
3856 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
3857 embedded_statement ELSE embedded_statement
3859 Location l = (Location) $1;
3861 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
3863 if (RootContext.WarningLevel >= 3){
3864 // FIXME: location for warning should be loc property of $5 and $7.
3865 if ($5 == EmptyStatement.Value)
3866 Report.Warning (642, l, "Possible mistaken empty statement");
3867 if ($7 == EmptyStatement.Value)
3868 Report.Warning (642, l, "Possible mistaken empty statement");
3874 : SWITCH OPEN_PARENS
3876 switch_stack.Push (current_block);
3878 expression CLOSE_PARENS
3881 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
3882 current_block = (Block) switch_stack.Pop ();
3898 Report.Error (1522, lexer.Location, "Empty switch block");
3906 ArrayList sections = new ArrayList (4);
3911 | switch_sections switch_section
3913 ArrayList sections = (ArrayList) $1;
3923 current_block = current_block.CreateSwitchBlock (lexer.Location);
3927 Block topmost = current_block;
3929 while (topmost.Implicit)
3930 topmost = topmost.Parent;
3931 $$ = new SwitchSection ((ArrayList) $1, topmost);
3938 ArrayList labels = new ArrayList (4);
3943 | switch_labels switch_label
3945 ArrayList labels = (ArrayList) ($1);
3953 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
3954 | DEFAULT COLON { $$ = new SwitchLabel (null, (Location) $1); }
3957 1523, GetLocation ($1),
3958 "The keyword case or default must precede code in switch block");
3970 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
3972 Location l = (Location) $1;
3973 $$ = new While ((Expression) $3, (Statement) $5, l);
3978 : DO embedded_statement
3979 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
3981 Location l = (Location) $1;
3983 $$ = new Do ((Statement) $2, (Expression) $5, l);
3989 opt_for_initializer SEMICOLON
3991 Block assign_block = new Block (current_block);
3992 current_block = assign_block;
3994 if ($3 is DictionaryEntry){
3995 DictionaryEntry de = (DictionaryEntry) $3;
3997 Expression type = (Expression) de.Key;
3998 ArrayList var_declarators = (ArrayList) de.Value;
4000 foreach (VariableDeclaration decl in var_declarators){
4004 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4008 Location l = lexer.Location;
4010 if (decl.expression_or_array_initializer is Expression){
4011 expr = (Expression) decl.expression_or_array_initializer;
4012 } else if (decl.expression_or_array_initializer == null) {
4015 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4016 expr = new ArrayCreation (type, "", init, decl.Location);
4019 LocalVariableReference var;
4020 var = new LocalVariableReference (assign_block, decl.identifier, l);
4023 Assign a = new Assign (var, expr, decl.Location);
4025 assign_block.AddStatement (new StatementExpression (a));
4029 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4030 // This can be referred to as $5 below.
4036 opt_for_condition SEMICOLON
4037 opt_for_iterator CLOSE_PARENS
4040 Location l = (Location) $1;
4042 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4044 current_block.AddStatement (f);
4045 while (current_block.Implicit)
4046 current_block = current_block.Parent;
4048 current_block = current_block.Parent;
4053 : /* empty */ { $$ = EmptyStatement.Value; }
4058 : local_variable_declaration
4059 | statement_expression_list
4063 : /* empty */ { $$ = null; }
4064 | boolean_expression
4068 : /* empty */ { $$ = EmptyStatement.Value; }
4073 : statement_expression_list
4076 statement_expression_list
4077 : statement_expression
4079 // CHANGE: was `null'
4080 Block b = new Block (current_block, Block.Flags.Implicit);
4082 b.AddStatement ((Statement) $1);
4085 | statement_expression_list COMMA statement_expression
4087 Block b = (Block) $1;
4089 b.AddStatement ((Statement) $3);
4095 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4097 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4100 | FOREACH OPEN_PARENS type IDENTIFIER IN
4101 expression CLOSE_PARENS
4103 Block foreach_block = new Block (current_block);
4104 current_block = foreach_block;
4106 LocatedToken lt = (LocatedToken) $4;
4107 Location l = lt.Location;
4110 vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4112 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4114 // Get a writable reference to this read-only variable.
4116 // Note that the $$ here refers to the value of _this_ code block,
4117 // not the value of the LHS non-terminal. This can be referred to as $8 below.
4118 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
4125 LocalVariableReference v = (LocalVariableReference) $8;
4126 Location l = (Location) $1;
4129 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
4130 current_block.AddStatement (f);
4133 while (current_block.Implicit)
4134 current_block = current_block.Parent;
4136 current_block = current_block.Parent;
4142 | continue_statement
4152 $$ = new Break ((Location) $1);
4157 : CONTINUE SEMICOLON
4159 $$ = new Continue ((Location) $1);
4164 : GOTO IDENTIFIER SEMICOLON
4166 LocatedToken lt = (LocatedToken) $2;
4167 $$ = new Goto (lt.Value, lt.Location);
4169 | GOTO CASE constant_expression SEMICOLON
4171 $$ = new GotoCase ((Expression) $3, (Location) $1);
4173 | GOTO DEFAULT SEMICOLON
4175 $$ = new GotoDefault ((Location) $1);
4180 : RETURN opt_expression SEMICOLON
4182 $$ = new Return ((Expression) $2, (Location) $1);
4187 : THROW opt_expression SEMICOLON
4189 $$ = new Throw ((Expression) $2, (Location) $1);
4194 : IDENTIFIER RETURN expression SEMICOLON
4196 LocatedToken lt = (LocatedToken) $1;
4197 string s = lt.Value;
4199 Report.Error (1003, lt.Location, "; expected");
4202 if (RootContext.Version == LanguageVersion.ISO_1){
4203 Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4206 if (iterator_container == null){
4207 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4210 iterator_container.SetYields ();
4211 $$ = new Yield ((Expression) $3, lt.Location);
4214 | IDENTIFIER RETURN SEMICOLON
4216 Report.Error (1627, (Location) $2, "Expression expected after yield return");
4219 | IDENTIFIER BREAK SEMICOLON
4221 LocatedToken lt = (LocatedToken) $1;
4222 string s = lt.Value;
4224 Report.Error (1003, lt.Location, "; expected");
4227 if (RootContext.Version == LanguageVersion.ISO_1){
4228 Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4231 if (iterator_container == null){
4232 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4235 iterator_container.SetYields ();
4236 $$ = new YieldBreak (lt.Location);
4247 : TRY block catch_clauses
4251 ArrayList c = (ArrayList)$3;
4252 for (int i = 0; i < c.Count; ++i) {
4253 Catch cc = (Catch) c [i];
4255 if (i != c.Count - 1)
4256 Report.Error (1017, cc.loc, "Try statement already has an empty catch block");
4263 // Now s contains the list of specific catch clauses
4264 // and g contains the general one.
4266 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4268 | TRY block opt_catch_clauses FINALLY block
4271 ArrayList s = new ArrayList (4);
4272 ArrayList catch_list = (ArrayList) $3;
4274 if (catch_list != null){
4275 foreach (Catch cc in catch_list) {
4283 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4287 Report.Error (1524, (Location) $1, "Expected catch or finally");
4293 : /* empty */ { $$ = null; }
4300 ArrayList l = new ArrayList (4);
4305 | catch_clauses catch_clause
4307 ArrayList l = (ArrayList) $1;
4315 : /* empty */ { $$ = null; }
4320 : CATCH opt_catch_args
4322 Expression type = null;
4325 DictionaryEntry cc = (DictionaryEntry) $2;
4326 type = (Expression) cc.Key;
4327 LocatedToken lt = (LocatedToken) cc.Value;
4330 ArrayList one = new ArrayList (4);
4332 one.Add (new VariableDeclaration (lt, null));
4334 current_block = new Block (current_block);
4335 Block b = declare_local_variables (type, one, lt.Location);
4340 Expression type = null;
4344 DictionaryEntry cc = (DictionaryEntry) $2;
4345 type = (Expression) cc.Key;
4346 LocatedToken lt = (LocatedToken) cc.Value;
4350 while (current_block.Implicit)
4351 current_block = current_block.Parent;
4352 current_block = current_block.Parent;
4356 $$ = new Catch (type, id, (Block) $4, ((Block) $4).loc);
4361 : /* empty */ { $$ = null; }
4366 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4368 $$ = new DictionaryEntry ($2, $3);
4376 $$ = new Checked ((Block) $2);
4383 $$ = new Unchecked ((Block) $2);
4390 RootContext.CheckUnsafeOption ((Location) $1);
4392 $$ = new Unsafe ((Block) $3);
4398 type fixed_pointer_declarators
4401 ArrayList list = (ArrayList) $4;
4402 Expression type = (Expression) $3;
4403 Location l = (Location) $1;
4404 int top = list.Count;
4406 Block assign_block = new Block (current_block);
4407 current_block = assign_block;
4409 for (int i = 0; i < top; i++){
4410 Pair p = (Pair) list [i];
4413 v = current_block.AddVariable (type, (string) p.First, l);
4417 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4425 Location l = (Location) $1;
4427 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4429 current_block.AddStatement (f);
4430 while (current_block.Implicit)
4431 current_block = current_block.Parent;
4433 current_block = current_block.Parent;
4437 fixed_pointer_declarators
4438 : fixed_pointer_declarator {
4439 ArrayList declarators = new ArrayList (4);
4441 declarators.Add ($1);
4444 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4446 ArrayList declarators = (ArrayList) $1;
4448 declarators.Add ($3);
4453 fixed_pointer_declarator
4454 : IDENTIFIER ASSIGN expression
4456 LocatedToken lt = (LocatedToken) $1;
4457 // FIXME: keep location
4458 $$ = new Pair (lt.Value, $3);
4462 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
4468 : LOCK OPEN_PARENS expression CLOSE_PARENS
4474 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
4479 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4481 Block assign_block = new Block (current_block);
4482 current_block = assign_block;
4484 if ($3 is DictionaryEntry){
4485 DictionaryEntry de = (DictionaryEntry) $3;
4486 Location l = (Location) $1;
4488 Expression type = (Expression) de.Key;
4489 ArrayList var_declarators = (ArrayList) de.Value;
4491 ArrayList vars = new ArrayList (4);
4493 foreach (VariableDeclaration decl in var_declarators){
4495 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4498 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
4501 if (decl.expression_or_array_initializer is Expression){
4502 expr = (Expression) decl.expression_or_array_initializer;
4504 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4506 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4509 expr = new ArrayCreation (type, "", init, decl.Location);
4512 LocalVariableReference var;
4514 // Get a writable reference to this read-only variable.
4515 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4517 // This is so that it is not a warning on using variables
4520 vars.Add (new DictionaryEntry (var, expr));
4522 // Assign a = new Assign (var, expr, decl.Location);
4523 // assign_block.AddStatement (new StatementExpression (a));
4526 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4527 // It can be referred to as $5 below.
4528 $$ = new DictionaryEntry (type, vars);
4535 Using u = new Using ($5, (Statement) $6, (Location) $1);
4536 current_block.AddStatement (u);
4537 while (current_block.Implicit)
4538 current_block = current_block.Parent;
4540 current_block = current_block.Parent;
4544 resource_acquisition
4545 : local_variable_declaration
4552 // A class used to pass around variable declarations and constants
4554 public class VariableDeclaration {
4555 public string identifier;
4556 public object expression_or_array_initializer;
4557 public Location Location;
4558 public Attributes OptAttributes;
4559 public string DocComment;
4561 public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
4563 this.identifier = lt.Value;
4564 this.expression_or_array_initializer = eoai;
4565 this.Location = lt.Location;
4566 this.OptAttributes = opt_attrs;
4569 public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
4575 // A class used to hold info about an indexer declarator
4577 public class IndexerDeclaration {
4578 public Expression type;
4579 public MemberName interface_type;
4580 public Parameters param_list;
4581 public Location location;
4583 public IndexerDeclaration (Expression type, MemberName interface_type,
4584 Parameters param_list, Location loc)
4587 this.interface_type = interface_type;
4588 this.param_list = param_list;
4589 this.location = loc;
4594 // We use this when we do not have an object in advance that is an IIteratorContainer
4596 public class SimpleIteratorContainer : IIteratorContainer {
4599 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4604 public static SimpleIteratorContainer GetSimple () {
4605 Simple.Yields = false;
4609 public void SetYields () { Yields = true; }
4613 // A class used to hold info about an operator declarator
4615 public class OperatorDeclaration {
4616 public Operator.OpType optype;
4617 public Expression ret_type, arg1type, arg2type;
4618 public string arg1name, arg2name;
4619 public Location location;
4621 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4622 Expression arg1type, string arg1name,
4623 Expression arg2type, string arg2name, Location location)
4626 this.ret_type = ret_type;
4627 this.arg1type = arg1type;
4628 this.arg1name = arg1name;
4629 this.arg2type = arg2type;
4630 this.arg2name = arg2name;
4631 this.location = location;
4636 void Error_ExpectingTypeName (Expression expr)
4638 if (expr is Invocation){
4639 Report.Error (1002, expr.Location, "Expecting `;'");
4641 Report.Error (-1, expr.Location, "Invalid Type definition");
4645 TypeContainer pop_current_class ()
4647 TypeContainer retval = current_class;
4649 current_class = current_class.Parent;
4650 current_container = current_container.Parent;
4652 if (current_class != current_container) {
4653 if (!(current_class is ClassPart) ||
4654 ((ClassPart) current_class).PartialContainer != current_container)
4655 throw new InternalErrorException ();
4656 } else if (current_container is ClassPart)
4657 current_container = ((ClassPart) current_class).PartialContainer;
4663 // Given the @class_name name, it creates a fully qualified name
4664 // based on the containing declaration space
4667 MakeName (MemberName class_name)
4669 Namespace ns = current_namespace.NS;
4671 if (current_container.Name == ""){
4673 return new MemberName (ns.MemberName, class_name);
4677 return new MemberName (current_container.MemberName, class_name);
4681 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4683 Block implicit_block;
4684 ArrayList inits = null;
4687 // We use the `Used' property to check whether statements
4688 // have been added to the current block. If so, we need
4689 // to create another block to contain the new declaration
4690 // otherwise, as an optimization, we use the same block to
4691 // add the declaration.
4693 // FIXME: A further optimization is to check if the statements
4694 // that were added were added as part of the initialization
4695 // below. In which case, no other statements have been executed
4696 // and we might be able to reduce the number of blocks for
4697 // situations like this:
4699 // int j = 1; int k = j + 1;
4701 if (current_block.Used)
4702 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4704 implicit_block = current_block;
4706 foreach (VariableDeclaration decl in variable_declarators){
4708 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
4709 if (decl.expression_or_array_initializer != null){
4711 inits = new ArrayList (4);
4718 return implicit_block;
4720 foreach (VariableDeclaration decl in inits){
4724 if (decl.expression_or_array_initializer is Expression){
4725 expr = (Expression) decl.expression_or_array_initializer;
4728 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4730 expr = new ArrayCreation (type, "", init, decl.Location);
4733 LocalVariableReference var;
4734 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4736 assign = new Assign (var, expr, decl.Location);
4738 implicit_block.AddStatement (new StatementExpression (assign));
4741 return implicit_block;
4744 Block declare_local_constants (Expression type, ArrayList declarators)
4746 Block implicit_block;
4748 if (current_block.Used)
4749 implicit_block = new Block (current_block, Block.Flags.Implicit);
4751 implicit_block = current_block;
4753 foreach (VariableDeclaration decl in declarators){
4754 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
4757 return implicit_block;
4760 void CheckAttributeTarget (string a, Location l)
4764 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4768 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4774 void CheckUnaryOperator (Operator.OpType op, Location l)
4778 case Operator.OpType.LogicalNot:
4779 case Operator.OpType.OnesComplement:
4780 case Operator.OpType.Increment:
4781 case Operator.OpType.Decrement:
4782 case Operator.OpType.True:
4783 case Operator.OpType.False:
4784 case Operator.OpType.Addition:
4785 case Operator.OpType.Subtraction:
4790 Report.Error (1019, l, "Overloadable unary operator expected");
4796 void CheckBinaryOperator (Operator.OpType op, Location l)
4800 case Operator.OpType.Addition:
4801 case Operator.OpType.Subtraction:
4802 case Operator.OpType.Multiply:
4803 case Operator.OpType.Division:
4804 case Operator.OpType.Modulus:
4805 case Operator.OpType.BitwiseAnd:
4806 case Operator.OpType.BitwiseOr:
4807 case Operator.OpType.ExclusiveOr:
4808 case Operator.OpType.LeftShift:
4809 case Operator.OpType.RightShift:
4810 case Operator.OpType.Equality:
4811 case Operator.OpType.Inequality:
4812 case Operator.OpType.GreaterThan:
4813 case Operator.OpType.LessThan:
4814 case Operator.OpType.GreaterThanOrEqual:
4815 case Operator.OpType.LessThanOrEqual:
4819 Report.Error (1020, l, "Overloadable binary operator expected");
4825 void syntax_error (Location l, string msg)
4827 Report.Error (1003, l, "Syntax error, " + msg);
4830 void note (string s)
4832 // Used to put annotations
4837 public Tokenizer Lexer {
4843 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4845 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4846 this.name = file.Name;
4848 current_container = RootContext.Tree.Types;
4849 // TODO: Make RootContext.Tree.Types a PartialContainer.
4850 current_class = current_container;
4851 current_container.NamespaceEntry = current_namespace;
4852 oob_stack = new Stack ();
4853 switch_stack = new Stack ();
4855 lexer = new Tokenizer (reader, file, defines);
4858 public void parse ()
4861 if (yacc_verbose_flag > 1)
4862 yyparse (lexer, new yydebug.yyDebugSimple ());
4865 Tokenizer tokenizer = lexer as Tokenizer;
4866 tokenizer.cleanup ();
4867 } catch (Exception e){
4869 // Removed for production use, use parser verbose to get the output.
4871 // Console.WriteLine (e);
4872 Report.Error (-25, lexer.Location, "Parsing error");
4873 if (yacc_verbose_flag > 0)
4874 Console.WriteLine (e);
4877 RootContext.Tree.Types.NamespaceEntry = null;
4880 void CheckToken (int error, int yyToken, string msg, Location loc)
4882 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4883 Report.Error (error, loc, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4886 Report.Error (error, loc, msg);
4889 void CheckIdentifierToken (int yyToken, Location loc)
4891 CheckToken (1041, yyToken, "Identifier expected", loc);
4894 string ConsumeStoredComment ()
4896 string s = tmpComment;
4898 Lexer.doc_state = XmlCommentState.Allowed;
4902 Location GetLocation (object obj)
4904 if (obj is MemberCore)
4905 return ((MemberCore) obj).Location;
4906 if (obj is MemberName)
4907 return ((MemberName) obj).Location;
4908 if (obj is LocatedToken)
4909 return ((LocatedToken) obj).Location;
4910 if (obj is Location)
4911 return (Location) obj;
4912 return lexer.Location;