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; }
1626 { lexer.PropertyParsing = false; }
1629 LocatedToken lt = (LocatedToken) $4;
1630 MemberName name = new MemberName (lt.Value, lt.Location);
1632 if ($3 == TypeManager.system_void_expr) {
1633 Report.Error (547, lt.Location, "`{0}': property or indexer cannot have void type", lt.Value);
1638 Property p = new Property (current_class, (Expression) $3, (int) $2, true,
1639 name, (Attributes) $1,
1642 Report.Error (548, lexer.Location, "`{0}': property or indexer must have at least one accessor", p.GetSignatureForError ());
1646 InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
1648 $$ = new Property (current_class, (Expression) $3, (int) $2, true,
1649 name, (Attributes) $1,
1650 pinfo.Get, pinfo.Set);
1651 if (RootContext.Documentation != null)
1652 ((Property) $$).DocComment = Lexer.consume_doc_comment ();
1657 CheckIdentifierToken (yyToken, GetLocation ($4));
1663 : opt_attributes opt_modifiers GET SEMICOLON
1665 $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, (int) $2, 0, (Location) $3, (Location) $3);
1667 | opt_attributes opt_modifiers GET OPEN_BRACE
1669 Report.Error (531, (Location) $3, "`{0}': interface members cannot have a definition", ".get");
1670 $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, (int) $2, 0, (Location) $3, (Location) $3);
1672 | opt_attributes opt_modifiers SET SEMICOLON
1674 $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1, 0, (int) $2, (Location) $3, (Location) $3);
1676 | opt_attributes opt_modifiers GET SEMICOLON opt_attributes opt_modifiers SET SEMICOLON
1678 $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $5, (int) $2, (int) $6, (Location) $3, (Location) $7);
1680 | opt_attributes opt_modifiers SET SEMICOLON opt_attributes opt_modifiers GET SEMICOLON
1682 $$ = new InterfaceAccessorInfo (true, true, (Attributes) $5, (Attributes) $1, (int) $6, (int) $2, (Location) $3, (Location) $3);
1690 interface_event_declaration
1691 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1693 LocatedToken lt = (LocatedToken) $5;
1694 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1695 new MemberName (lt.Value, lt.Location), null,
1697 if (RootContext.Documentation != null)
1698 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1700 | opt_attributes opt_new EVENT type error {
1701 CheckIdentifierToken (yyToken, GetLocation ($5));
1704 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1705 LocatedToken lt = (LocatedToken) $5;
1706 Report.Error (68, lt.Location, "`{0}.{1}': event in interface cannot have initializer", current_container.Name, lt.Value);
1709 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1711 lexer.EventParsing = true;
1713 event_accessor_declarations
1715 lexer.EventParsing = false;
1718 Report.Error (69, (Location) $3, "Event in interface cannot have add or remove accessors");
1723 interface_indexer_declaration
1724 : opt_attributes opt_new type THIS
1725 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1727 { lexer.PropertyParsing = true; }
1729 { lexer.PropertyParsing = false; }
1733 Indexer i = new Indexer (current_class, (Expression) $3,
1734 new MemberName (TypeContainer.DefaultIndexerName),
1735 (int) $2, true, (Parameters) $6, (Attributes) $1,
1738 Report.Error (548, lexer.Location, "`{0}': property or indexer must have at least one accessor", i.GetSignatureForError ());
1742 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1744 $$ = new Indexer (current_class, (Expression) $3,
1745 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1746 (int) $2, true, (Parameters) $6, (Attributes) $1,
1747 info.Get, info.Set);
1748 if (RootContext.Documentation != null)
1749 ((Indexer) $$).DocComment = ConsumeStoredComment ();
1753 operator_declaration
1754 : opt_attributes opt_modifiers operator_declarator
1756 iterator_container = SimpleIteratorContainer.GetSimple ();
1763 OperatorDeclaration decl = (OperatorDeclaration) $3;
1765 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1767 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null, decl.location);
1768 if (decl.arg2type != null)
1769 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null, decl.location);
1771 Operator op = new Operator (
1772 current_class, decl.optype, decl.ret_type, (int) $2,
1773 new Parameters (param_list, null),
1774 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1776 if (RootContext.Documentation != null) {
1777 op.DocComment = tmpComment;
1778 Lexer.doc_state = XmlCommentState.Allowed;
1781 if (SimpleIteratorContainer.Simple.Yields)
1784 // Note again, checking is done in semantic analysis
1785 current_container.AddOperator (op);
1787 current_local_parameters = null;
1788 iterator_container = null;
1794 | SEMICOLON { $$ = null; }
1797 : type OPERATOR overloadable_operator
1798 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1800 LocatedToken lt = (LocatedToken) $6;
1801 Operator.OpType op = (Operator.OpType) $3;
1802 CheckUnaryOperator (op, lt.Location);
1804 if (op == Operator.OpType.Addition)
1805 op = Operator.OpType.UnaryPlus;
1807 if (op == Operator.OpType.Subtraction)
1808 op = Operator.OpType.UnaryNegation;
1810 Parameter [] pars = new Parameter [1];
1811 Expression type = (Expression) $5;
1813 pars [0] = new Parameter (type, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1815 current_local_parameters = new Parameters (pars, null);
1817 if (RootContext.Documentation != null) {
1818 tmpComment = Lexer.consume_doc_comment ();
1819 Lexer.doc_state = XmlCommentState.NotAllowed;
1822 $$ = new OperatorDeclaration (op, (Expression) $1, type, lt.Value,
1823 null, null, (Location) $2);
1825 | type OPERATOR overloadable_operator
1827 type IDENTIFIER COMMA
1831 LocatedToken ltParam1 = (LocatedToken) $6;
1832 LocatedToken ltParam2 = (LocatedToken) $9;
1833 CheckBinaryOperator ((Operator.OpType) $3, (Location) $2);
1835 Parameter [] pars = new Parameter [2];
1837 Expression typeL = (Expression) $5;
1838 Expression typeR = (Expression) $8;
1840 pars [0] = new Parameter (typeL, ltParam1.Value, Parameter.Modifier.NONE, null, ltParam1.Location);
1841 pars [1] = new Parameter (typeR, ltParam2.Value, Parameter.Modifier.NONE, null, ltParam2.Location);
1843 current_local_parameters = new Parameters (pars, null);
1845 if (RootContext.Documentation != null) {
1846 tmpComment = Lexer.consume_doc_comment ();
1847 Lexer.doc_state = XmlCommentState.NotAllowed;
1850 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1851 typeL, ltParam1.Value,
1852 typeR, ltParam2.Value, (Location) $2);
1854 | conversion_operator_declarator
1855 | type OPERATOR overloadable_operator
1857 type IDENTIFIER COMMA
1858 type IDENTIFIER COMMA
1862 Report.Error (1534, (Location) $2, "Overloaded binary operator `{0}' takes two parameters",
1863 Operator.GetName ((Operator.OpType) $3));
1866 | type OPERATOR overloadable_operator
1867 OPEN_PARENS CLOSE_PARENS
1869 Report.Error (1535, (Location) $2, "Overloaded unary operator `{0}' takes one parameter",
1870 Operator.GetName ((Operator.OpType) $3));
1875 overloadable_operator
1877 : BANG { $$ = Operator.OpType.LogicalNot; }
1878 | TILDE { $$ = Operator.OpType.OnesComplement; }
1879 | OP_INC { $$ = Operator.OpType.Increment; }
1880 | OP_DEC { $$ = Operator.OpType.Decrement; }
1881 | TRUE { $$ = Operator.OpType.True; }
1882 | FALSE { $$ = Operator.OpType.False; }
1883 // Unary and binary:
1884 | PLUS { $$ = Operator.OpType.Addition; }
1885 | MINUS { $$ = Operator.OpType.Subtraction; }
1887 | STAR { $$ = Operator.OpType.Multiply; }
1888 | DIV { $$ = Operator.OpType.Division; }
1889 | PERCENT { $$ = Operator.OpType.Modulus; }
1890 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1891 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1892 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1893 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1894 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1895 | OP_EQ { $$ = Operator.OpType.Equality; }
1896 | OP_NE { $$ = Operator.OpType.Inequality; }
1897 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1898 | OP_LT { $$ = Operator.OpType.LessThan; }
1899 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1900 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1903 conversion_operator_declarator
1904 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1906 LocatedToken lt = (LocatedToken) $6;
1907 Parameter [] pars = new Parameter [1];
1909 pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1911 current_local_parameters = new Parameters (pars, null);
1913 if (RootContext.Documentation != null) {
1914 tmpComment = Lexer.consume_doc_comment ();
1915 Lexer.doc_state = XmlCommentState.NotAllowed;
1918 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, lt.Value,
1919 null, null, (Location) $2);
1921 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1923 LocatedToken lt = (LocatedToken) $6;
1924 Parameter [] pars = new Parameter [1];
1926 pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1928 current_local_parameters = new Parameters (pars, null);
1930 if (RootContext.Documentation != null) {
1931 tmpComment = Lexer.consume_doc_comment ();
1932 Lexer.doc_state = XmlCommentState.NotAllowed;
1935 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, lt.Value,
1936 null, null, (Location) $2);
1940 syntax_error ((Location) $1, "'operator' expected");
1944 syntax_error ((Location) $1, "'operator' expected");
1948 constructor_declaration
1951 constructor_declarator
1954 Constructor c = (Constructor) $3;
1955 c.Block = (ToplevelBlock) $4;
1956 c.OptAttributes = (Attributes) $1;
1957 c.ModFlags = (int) $2;
1959 if (RootContext.Documentation != null)
1960 c.DocComment = ConsumeStoredComment ();
1962 if (c.Name == current_container.Basename){
1963 if ((c.ModFlags & Modifiers.STATIC) != 0){
1964 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1965 Report.Error (515, c.Location,
1966 "`{0}': access modifiers are not allowed on static constructors",
1967 c.GetSignatureForError ());
1970 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1972 if (c.Initializer != null){
1973 Report.Error (514, c.Location,
1974 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
1975 c.GetSignatureForError ());
1978 if (!c.Parameters.Empty){
1979 Report.Error (132, c.Location,
1980 "`{0}': The static constructor must be parameterless", c.GetSignatureForError ());
1983 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1986 // We let another layer check the validity of the constructor.
1987 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1990 current_container.AddConstructor (c);
1992 current_local_parameters = null;
1993 if (RootContext.Documentation != null)
1994 Lexer.doc_state = XmlCommentState.Allowed;
1998 constructor_declarator
2001 if (RootContext.Documentation != null) {
2002 tmpComment = Lexer.consume_doc_comment ();
2003 Lexer.doc_state = XmlCommentState.Allowed;
2006 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2008 current_local_parameters = (Parameters) $4;
2010 opt_constructor_initializer
2012 LocatedToken lt = (LocatedToken) $1;
2013 $$ = new Constructor (current_class, lt.Value, 0, (Parameters) $4,
2014 (ConstructorInitializer) $7, lt.Location);
2020 | SEMICOLON { $$ = null; }
2023 opt_constructor_initializer
2024 : /* empty */ { $$ = null; }
2025 | constructor_initializer
2028 constructor_initializer
2029 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
2031 $$ = new ConstructorBaseInitializer ((ArrayList) $4, (Location) $2);
2033 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
2035 $$ = new ConstructorThisInitializer ((ArrayList) $4, (Location) $2);
2038 Report.Error (1018, (Location) $1, "Keyword this or base expected");
2044 : /* EMPTY */ { $$ = 0; }
2045 | UNSAFE { $$ = Modifiers.UNSAFE; }
2046 | EXTERN { $$ = Modifiers.EXTERN; }
2049 destructor_declaration
2050 : opt_attributes opt_finalizer TILDE
2052 if (RootContext.Documentation != null) {
2053 tmpComment = Lexer.consume_doc_comment ();
2054 Lexer.doc_state = XmlCommentState.NotAllowed;
2057 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2059 LocatedToken lt = (LocatedToken) $5;
2060 if (lt.Value != current_container.Basename){
2061 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2062 } else if (current_container.Kind != Kind.Class){
2063 Report.Error (575, lt.Location, "Only class types can contain destructor");
2065 Location l = lt.Location;
2068 if (!RootContext.StdLib && current_container.Name == "System.Object")
2069 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2071 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2073 Method d = new Destructor (
2074 current_class, TypeManager.system_void_expr, m, "Finalize",
2075 new Parameters (null, null), (Attributes) $1, l);
2076 if (RootContext.Documentation != null)
2077 d.DocComment = ConsumeStoredComment ();
2079 d.Block = (ToplevelBlock) $8;
2080 current_container.AddMethod (d);
2088 EVENT type variable_declarators SEMICOLON
2090 foreach (VariableDeclaration var in (ArrayList) $5) {
2092 MemberName name = new MemberName (var.identifier,
2095 Event e = new EventField (
2096 current_class, (Expression) $4, (int) $2, false, name,
2097 var.expression_or_array_initializer, (Attributes) $1);
2099 current_container.AddEvent (e);
2101 if (RootContext.Documentation != null) {
2102 e.DocComment = Lexer.consume_doc_comment ();
2103 Lexer.doc_state = XmlCommentState.Allowed;
2109 EVENT type namespace_or_type_name
2112 implicit_value_parameter_type = (Expression) $4;
2113 lexer.EventParsing = true;
2115 event_accessor_declarations
2117 lexer.EventParsing = false;
2121 MemberName name = (MemberName) $5;
2124 Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2125 current_container.Name, name);
2128 Pair pair = (Pair) $8;
2129 if (pair.First == null || pair.Second == null)
2130 // CS0073 is already reported, so no CS0065 here.
2133 Event e = new EventProperty (
2134 current_class, (Expression) $4, (int) $2, false, name, null,
2135 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second);
2136 if (RootContext.Documentation != null) {
2137 e.DocComment = Lexer.consume_doc_comment ();
2138 Lexer.doc_state = XmlCommentState.Allowed;
2141 current_container.AddEvent (e);
2142 implicit_value_parameter_type = null;
2146 | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
2147 MemberName mn = (MemberName) $5;
2149 if (mn.Left != null)
2150 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2152 Report.Error (71, mn.Location, "Event declaration should use property syntax");
2154 if (RootContext.Documentation != null)
2155 Lexer.doc_state = XmlCommentState.Allowed;
2159 event_accessor_declarations
2160 : add_accessor_declaration remove_accessor_declaration
2162 $$ = new Pair ($1, $2);
2164 | remove_accessor_declaration add_accessor_declaration
2166 $$ = new Pair ($2, $1);
2168 | add_accessor_declaration { $$ = null; }
2169 | remove_accessor_declaration { $$ = null; }
2172 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2178 add_accessor_declaration
2179 : opt_attributes ADD
2181 Parameter [] args = new Parameter [1];
2182 Parameter implicit_value_parameter = new Parameter (
2183 implicit_value_parameter_type, "value",
2184 Parameter.Modifier.NONE, null, (Location) $2);
2186 args [0] = implicit_value_parameter;
2188 current_local_parameters = new Parameters (args, null);
2189 lexer.EventParsing = false;
2193 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2194 lexer.EventParsing = true;
2196 | opt_attributes ADD error {
2197 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2200 | opt_attributes modifiers ADD {
2201 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2206 remove_accessor_declaration
2207 : opt_attributes REMOVE
2209 Parameter [] args = new Parameter [1];
2210 Parameter implicit_value_parameter = new Parameter (
2211 implicit_value_parameter_type, "value",
2212 Parameter.Modifier.NONE, null, (Location) $2);
2214 args [0] = implicit_value_parameter;
2216 current_local_parameters = new Parameters (args, null);
2217 lexer.EventParsing = false;
2221 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2222 lexer.EventParsing = true;
2224 | opt_attributes REMOVE error {
2225 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2228 | opt_attributes modifiers REMOVE {
2229 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2235 : opt_attributes opt_modifiers indexer_declarator
2238 IndexerDeclaration decl = (IndexerDeclaration) $3;
2240 implicit_value_parameter_type = decl.type;
2242 lexer.PropertyParsing = true;
2243 parsing_indexer = true;
2245 indexer_parameters = decl.param_list;
2247 accessor_declarations
2249 lexer.PropertyParsing = false;
2250 has_get = has_set = false;
2251 parsing_indexer = false;
2258 // The signature is computed from the signature of the indexer. Look
2259 // at section 3.6 on the spec
2261 IndexerDeclaration decl = (IndexerDeclaration) $3;
2262 Location loc = decl.location;
2263 Pair pair = (Pair) $6;
2264 Accessor get_block = (Accessor) pair.First;
2265 Accessor set_block = (Accessor) pair.Second;
2268 if (decl.interface_type != null)
2269 name = new MemberName (decl.interface_type, TypeContainer.DefaultIndexerName, loc);
2271 name = new MemberName (TypeContainer.DefaultIndexerName, loc);
2273 indexer = new Indexer (current_class, decl.type, name,
2274 (int) $2, false, decl.param_list, (Attributes) $1,
2275 get_block, set_block);
2277 if (RootContext.Documentation != null)
2278 indexer.DocComment = ConsumeStoredComment ();
2280 current_container.AddIndexer (indexer);
2282 current_local_parameters = null;
2283 implicit_value_parameter_type = null;
2284 indexer_parameters = null;
2289 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2291 Parameters pars = (Parameters) $4;
2292 if (pars.HasArglist) {
2293 // "__arglist is not valid in this context"
2294 Report.Error (1669, (Location) $2, "__arglist is not valid in this context");
2295 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2296 Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
2298 if (RootContext.Documentation != null) {
2299 tmpComment = Lexer.consume_doc_comment ();
2300 Lexer.doc_state = XmlCommentState.Allowed;
2303 $$ = new IndexerDeclaration ((Expression) $1, null, pars, (Location) $2);
2305 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2307 Parameters pars = (Parameters) $6;
2309 if (pars.HasArglist) {
2310 // "__arglist is not valid in this context"
2311 Report.Error (1669, (Location) $4, "__arglist is not valid in this context");
2312 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2313 Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
2316 MemberName name = (MemberName) $2;
2317 $$ = new IndexerDeclaration ((Expression) $1, name, pars, (Location) $4);
2319 if (RootContext.Documentation != null) {
2320 tmpComment = Lexer.consume_doc_comment ();
2321 Lexer.doc_state = XmlCommentState.Allowed;
2332 if (RootContext.Documentation != null)
2333 enumTypeComment = Lexer.consume_doc_comment ();
2338 LocatedToken lt = (LocatedToken) $5;
2339 Location enum_location = lt.Location;
2342 Report.Error (267, lt.Location, "The partial modifier can only appear immediately before `class', `struct' or `interface'");
2343 break; // assumes that the parser put us in a switch
2346 MemberName name = MakeName (new MemberName (lt.Value, enum_location));
2347 Enum e = new Enum (current_namespace, current_class, (Expression) $6, (int) $2,
2348 name, (Attributes) $1);
2350 if (RootContext.Documentation != null)
2351 e.DocComment = enumTypeComment;
2354 EnumMember em = null;
2355 foreach (VariableDeclaration ev in (ArrayList) $8) {
2356 em = new EnumMember (e, em, (Expression) ev.expression_or_array_initializer,
2357 new MemberName (ev.identifier, ev.Location), ev.OptAttributes);
2359 // if (RootContext.Documentation != null)
2360 em.DocComment = ev.DocComment;
2362 e.AddEnumMember (em);
2365 current_container.AddEnum (e);
2366 RootContext.Tree.RecordDecl (current_namespace.NS, name, e);
2373 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2374 | COLON type { $$ = $2; }
2380 if (RootContext.Documentation != null)
2381 Lexer.doc_state = XmlCommentState.Allowed;
2383 opt_enum_member_declarations
2385 // here will be evaluated after CLOSE_BLACE is consumed.
2386 if (RootContext.Documentation != null)
2387 Lexer.doc_state = XmlCommentState.Allowed;
2395 opt_enum_member_declarations
2396 : /* empty */ { $$ = new ArrayList (4); }
2397 | enum_member_declarations opt_comma { $$ = $1; }
2400 enum_member_declarations
2401 : enum_member_declaration
2403 ArrayList l = new ArrayList (4);
2408 | enum_member_declarations COMMA enum_member_declaration
2410 ArrayList l = (ArrayList) $1;
2418 enum_member_declaration
2419 : opt_attributes IDENTIFIER
2421 VariableDeclaration vd = new VariableDeclaration (
2422 (LocatedToken) $2, null, (Attributes) $1);
2424 if (RootContext.Documentation != null) {
2425 vd.DocComment = Lexer.consume_doc_comment ();
2426 Lexer.doc_state = XmlCommentState.Allowed;
2431 | opt_attributes IDENTIFIER
2433 if (RootContext.Documentation != null) {
2434 tmpComment = Lexer.consume_doc_comment ();
2435 Lexer.doc_state = XmlCommentState.NotAllowed;
2440 VariableDeclaration vd = new VariableDeclaration (
2441 (LocatedToken) $2, $5, (Attributes) $1);
2443 if (RootContext.Documentation != null)
2444 vd.DocComment = ConsumeStoredComment ();
2450 delegate_declaration
2453 DELEGATE type member_name
2454 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2457 MemberName name = MakeName ((MemberName) $5);
2458 Delegate del = new Delegate (current_namespace, current_class, (Expression) $4,
2459 (int) $2, name, (Parameters) $7, (Attributes) $1);
2461 if (RootContext.Documentation != null) {
2462 del.DocComment = Lexer.consume_doc_comment ();
2463 Lexer.doc_state = XmlCommentState.Allowed;
2466 current_container.AddDelegate (del);
2467 RootContext.Tree.RecordDecl (current_namespace.NS, name, del);
2472 namespace_or_type_name
2474 | IDENTIFIER DOUBLE_COLON IDENTIFIER {
2475 LocatedToken lt1 = (LocatedToken) $1;
2476 LocatedToken lt2 = (LocatedToken) $3;
2477 $$ = new MemberName (lt1.Value, lt2.Value, lt2.Location);
2479 | namespace_or_type_name DOT IDENTIFIER {
2480 LocatedToken lt = (LocatedToken) $3;
2481 $$ = new MemberName ((MemberName) $1, lt.Value);
2487 LocatedToken lt = (LocatedToken) $1;
2488 $$ = new MemberName (lt.Value, lt.Location);
2493 * Before you think of adding a return_type, notice that we have been
2494 * using two rules in the places where it matters (one rule using type
2495 * and another identical one that uses VOID as the return type). This
2496 * gets rid of a shift/reduce couple
2499 : namespace_or_type_name
2501 MemberName name = (MemberName) $1;
2502 $$ = name.GetTypeExpression ();
2514 // Note that here only unmanaged types are allowed but we
2515 // can't perform checks during this phase - we do it during
2516 // semantic analysis.
2518 $$ = new ComposedCast ((Expression) $1, "*");
2522 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2528 | non_expression_type rank_specifier
2530 $$ = new ComposedCast ((Expression) $1, (string) $2);
2532 | non_expression_type STAR
2534 $$ = new ComposedCast ((Expression) $1, "*");
2536 | expression rank_specifiers
2538 $$ = new ComposedCast ((Expression) $1, (string) $2);
2542 $$ = new ComposedCast ((Expression) $1, "*");
2546 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2547 // through this different path
2549 | multiplicative_expression STAR
2551 $$ = new ComposedCast ((Expression) $1, "*");
2558 ArrayList types = new ArrayList (4);
2563 | type_list COMMA type
2565 ArrayList types = (ArrayList) $1;
2573 * replaces all the productions for isolating the various
2574 * simple types, but we need this to reuse it easily in local_variable_type
2577 : OBJECT { $$ = TypeManager.system_object_expr; }
2578 | STRING { $$ = TypeManager.system_string_expr; }
2579 | BOOL { $$ = TypeManager.system_boolean_expr; }
2580 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2581 | FLOAT { $$ = TypeManager.system_single_expr; }
2582 | DOUBLE { $$ = TypeManager.system_double_expr; }
2587 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2588 | BYTE { $$ = TypeManager.system_byte_expr; }
2589 | SHORT { $$ = TypeManager.system_int16_expr; }
2590 | USHORT { $$ = TypeManager.system_uint16_expr; }
2591 | INT { $$ = TypeManager.system_int32_expr; }
2592 | UINT { $$ = TypeManager.system_uint32_expr; }
2593 | LONG { $$ = TypeManager.system_int64_expr; }
2594 | ULONG { $$ = TypeManager.system_uint64_expr; }
2595 | CHAR { $$ = TypeManager.system_char_expr; }
2596 | VOID { $$ = TypeManager.system_void_expr; }
2600 : type rank_specifiers
2602 $$ = new ComposedCast ((Expression) $1, (string) $2);
2607 // Expressions, section 7.5
2617 MemberName mn = (MemberName) $1;
2618 $$ = mn.GetTypeExpression ();
2620 | IDENTIFIER DOUBLE_COLON IDENTIFIER
2622 LocatedToken lt1 = (LocatedToken) $1;
2623 LocatedToken lt2 = (LocatedToken) $3;
2624 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, lt2.Location);
2626 | parenthesized_expression
2628 | invocation_expression
2632 | post_increment_expression
2633 | post_decrement_expression
2637 | checked_expression
2638 | unchecked_expression
2639 | pointer_member_access
2640 | anonymous_method_expression
2647 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2648 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2649 | NULL { $$ = NullLiteral.Null; }
2653 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2654 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2655 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2660 object v = lexer.Value;
2666 $$ = IntLiteral.Zero;
2668 $$ = IntLiteral.One;
2670 $$ = new IntLiteral (i);
2671 } else if (v is uint)
2672 $$ = new UIntLiteral ((UInt32) v);
2674 $$ = new LongLiteral ((Int64) v);
2675 else if (v is ulong)
2676 $$ = new ULongLiteral ((UInt64) v);
2678 Console.WriteLine ("OOPS. Unexpected result from scanner");
2683 : TRUE { $$ = new BoolLiteral (true); }
2684 | FALSE { $$ = new BoolLiteral (false); }
2687 parenthesized_expression_0
2688 : OPEN_PARENS expression CLOSE_PARENS
2691 lexer.Deambiguate_CloseParens ();
2692 // After this, the next token returned is one of
2693 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2694 // or CLOSE_PARENS_MINUS.
2698 parenthesized_expression
2699 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2703 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2705 // If a parenthesized expression is followed by a minus, we need to wrap
2706 // the expression inside a ParenthesizedExpression for the CS0075 check
2707 // in Binary.DoResolve().
2708 $$ = new ParenthesizedExpression ((Expression) $1);
2713 : primary_expression DOT IDENTIFIER
2715 LocatedToken lt = (LocatedToken) $3;
2716 $$ = new MemberAccess ((Expression) $1, lt.Value, lt.Location);
2718 | predefined_type DOT IDENTIFIER
2720 LocatedToken lt = (LocatedToken) $3;
2721 $$ = new MemberAccess ((Expression) $1, lt.Value, lt.Location);
2729 invocation_expression
2730 : primary_expression {
2731 $$ = lexer.Location;
2732 } OPEN_PARENS opt_argument_list CLOSE_PARENS
2735 Location l = (Location) $3;
2736 Report.Error (1, l, "Parse error");
2738 $$ = new Invocation ((Expression) $1, (ArrayList) $4);
2740 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2742 $$ = new Invocation ((Expression) $1, new ArrayList ());
2744 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2746 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
2748 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2750 ArrayList args = new ArrayList (1);
2752 $$ = new Invocation ((Expression) $1, args);
2754 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2756 ArrayList args = ((ArrayList) $4);
2758 $$ = new Invocation ((Expression) $1, args);
2763 : /* empty */ { $$ = null; }
2770 ArrayList list = new ArrayList (4);
2774 | argument_list COMMA argument
2776 ArrayList list = (ArrayList) $1;
2780 | argument_list error {
2781 CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
2789 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2791 | non_simple_argument
2798 : REF variable_reference
2800 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2802 | OUT variable_reference
2804 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2806 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2808 ArrayList list = (ArrayList) $3;
2809 Argument[] args = new Argument [list.Count];
2810 list.CopyTo (args, 0);
2812 Expression expr = new Arglist (args, (Location) $1);
2813 $$ = new Argument (expr, Argument.AType.Expression);
2817 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
2822 : expression { note ("section 5.4"); $$ = $1; }
2826 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2828 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
2830 | primary_expression rank_specifiers
2832 // So the super-trick is that primary_expression
2833 // can only be either a SimpleName or a MemberAccess.
2834 // The MemberAccess case arises when you have a fully qualified type-name like :
2836 // SimpleName is when you have
2839 Expression expr = (Expression) $1;
2840 if (expr is ComposedCast){
2841 $$ = new ComposedCast (expr, (string) $2);
2842 } else if (!(expr is SimpleName || expr is MemberAccess || expr is QualifiedAliasMember)){
2843 Error_ExpectingTypeName (expr);
2844 $$ = TypeManager.system_object_expr;
2847 // So we extract the string corresponding to the SimpleName
2850 $$ = new ComposedCast (expr, (string) $2);
2858 ArrayList list = new ArrayList (4);
2862 | expression_list COMMA expression
2864 ArrayList list = (ArrayList) $1;
2873 $$ = new This (current_block, (Location) $1);
2878 : BASE DOT IDENTIFIER
2880 LocatedToken lt = (LocatedToken) $3;
2881 $$ = new BaseAccess (lt.Value, lt.Location);
2883 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2885 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
2888 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
2893 post_increment_expression
2894 : primary_expression OP_INC
2896 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2897 (Expression) $1, (Location) $2);
2901 post_decrement_expression
2902 : primary_expression OP_DEC
2904 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2905 (Expression) $1, (Location) $2);
2910 : object_or_delegate_creation_expression
2911 | array_creation_expression
2914 object_or_delegate_creation_expression
2915 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2917 $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
2921 array_creation_expression
2922 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2924 opt_array_initializer
2926 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
2928 | NEW type rank_specifiers array_initializer
2930 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, (Location) $1);
2934 Report.Error (1031, (Location) $1, "Type expected");
2939 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
2956 : rank_specifier opt_rank_specifier
2958 $$ = (string) $2 + (string) $1;
2963 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2965 $$ = "[" + (string) $2 + "]";
2985 | dim_separators COMMA
2987 $$ = (string) $1 + ",";
2991 opt_array_initializer
3003 : OPEN_BRACE CLOSE_BRACE
3005 ArrayList list = new ArrayList (4);
3008 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3010 $$ = (ArrayList) $2;
3014 variable_initializer_list
3015 : variable_initializer
3017 ArrayList list = new ArrayList (4);
3021 | variable_initializer_list COMMA variable_initializer
3023 ArrayList list = (ArrayList) $1;
3030 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
3032 $$ = new TypeOfVoid ((Location) $1);
3034 | TYPEOF OPEN_PARENS type CLOSE_PARENS
3036 $$ = new TypeOf ((Expression) $3, (Location) $1);
3041 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3042 $$ = new SizeOf ((Expression) $3, (Location) $1);
3047 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3049 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3053 unchecked_expression
3054 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3056 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3060 pointer_member_access
3061 : primary_expression OP_PTR IDENTIFIER
3064 LocatedToken lt = (LocatedToken) $3;
3066 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
3067 $$ = new MemberAccess (deref, lt.Value, lt.Location);
3071 anonymous_method_expression
3072 : DELEGATE opt_anonymous_method_signature
3074 oob_stack.Push (current_local_parameters);
3075 current_local_parameters = (Parameters)$2;
3077 // Force the next block to be created as a ToplevelBlock
3078 oob_stack.Push (current_block);
3079 oob_stack.Push (top_current_block);
3080 current_block = null;
3084 Location loc = (Location) $1;
3085 top_current_block = (Block) oob_stack.Pop ();
3086 current_block = (Block) oob_stack.Pop ();
3087 if (RootContext.Version == LanguageVersion.ISO_1){
3088 Report.FeatureIsNotStandardized (loc, "anonymous methods");
3091 ToplevelBlock anon_block = (ToplevelBlock) $4;
3093 anon_block.Parent = current_block;
3094 $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block,
3097 current_local_parameters = (Parameters) oob_stack.Pop ();
3101 opt_anonymous_method_signature
3102 : /* empty */ { $$ = null; }
3103 | anonymous_method_signature
3106 anonymous_method_signature
3107 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3110 $$ = Parameters.EmptyReadOnlyParameters;
3112 ArrayList par_list = (ArrayList) $2;
3113 Parameter [] pars = new Parameter [par_list.Count];
3114 par_list.CopyTo (pars);
3115 $$ = new Parameters (pars, null);
3120 opt_anonymous_method_parameter_list
3121 : /* empty */ { $$ = null; }
3122 | anonymous_method_parameter_list { $$ = $1; }
3125 anonymous_method_parameter_list
3126 : anonymous_method_parameter
3128 ArrayList a = new ArrayList (4);
3132 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3134 ArrayList a = (ArrayList) $1;
3140 anonymous_method_parameter
3141 : opt_parameter_modifier type IDENTIFIER {
3142 LocatedToken lt = (LocatedToken) $3;
3143 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3145 | PARAMS type IDENTIFIER {
3146 Report.Error (1670, ((LocatedToken) $3).Location, "The `params' modifier is not allowed in anonymous method declaration");
3152 : primary_expression
3153 | BANG prefixed_unary_expression
3155 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3157 | TILDE prefixed_unary_expression
3159 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3165 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3167 $$ = new Cast ((Expression) $1, (Expression) $3);
3169 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3171 $$ = new Cast ((Expression) $1, (Expression) $3);
3177 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3179 $$ = new Cast ((Expression) $2, (Expression) $4);
3184 // The idea to split this out is from Rhys' grammar
3185 // to solve the problem with casts.
3187 prefixed_unary_expression
3189 | PLUS prefixed_unary_expression
3191 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3193 | MINUS prefixed_unary_expression
3195 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3197 | OP_INC prefixed_unary_expression
3199 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3200 (Expression) $2, (Location) $1);
3202 | OP_DEC prefixed_unary_expression
3204 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3205 (Expression) $2, (Location) $1);
3207 | STAR prefixed_unary_expression
3209 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, (Location) $1);
3211 | BITWISE_AND prefixed_unary_expression
3213 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3217 pre_increment_expression
3218 : OP_INC prefixed_unary_expression
3220 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3221 (Expression) $2, (Location) $1);
3225 pre_decrement_expression
3226 : OP_DEC prefixed_unary_expression
3228 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3229 (Expression) $2, (Location) $1);
3233 multiplicative_expression
3234 : prefixed_unary_expression
3235 | multiplicative_expression STAR prefixed_unary_expression
3237 $$ = new Binary (Binary.Operator.Multiply,
3238 (Expression) $1, (Expression) $3);
3240 | multiplicative_expression DIV prefixed_unary_expression
3242 $$ = new Binary (Binary.Operator.Division,
3243 (Expression) $1, (Expression) $3);
3245 | multiplicative_expression PERCENT prefixed_unary_expression
3247 $$ = new Binary (Binary.Operator.Modulus,
3248 (Expression) $1, (Expression) $3);
3253 : multiplicative_expression
3254 | additive_expression PLUS multiplicative_expression
3256 $$ = new Binary (Binary.Operator.Addition,
3257 (Expression) $1, (Expression) $3);
3259 | additive_expression MINUS multiplicative_expression
3261 $$ = new Binary (Binary.Operator.Subtraction,
3262 (Expression) $1, (Expression) $3);
3267 : additive_expression
3268 | shift_expression OP_SHIFT_LEFT additive_expression
3270 $$ = new Binary (Binary.Operator.LeftShift,
3271 (Expression) $1, (Expression) $3);
3273 | shift_expression OP_SHIFT_RIGHT additive_expression
3275 $$ = new Binary (Binary.Operator.RightShift,
3276 (Expression) $1, (Expression) $3);
3280 relational_expression
3282 | relational_expression OP_LT shift_expression
3284 $$ = new Binary (Binary.Operator.LessThan,
3285 (Expression) $1, (Expression) $3);
3287 | relational_expression OP_GT shift_expression
3289 $$ = new Binary (Binary.Operator.GreaterThan,
3290 (Expression) $1, (Expression) $3);
3292 | relational_expression OP_LE shift_expression
3294 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3295 (Expression) $1, (Expression) $3);
3297 | relational_expression OP_GE shift_expression
3299 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3300 (Expression) $1, (Expression) $3);
3302 | relational_expression IS type
3304 $$ = new Is ((Expression) $1, (Expression) $3, (Location) $2);
3306 | relational_expression AS type
3308 $$ = new As ((Expression) $1, (Expression) $3, (Location) $2);
3313 : relational_expression
3314 | equality_expression OP_EQ relational_expression
3316 $$ = new Binary (Binary.Operator.Equality,
3317 (Expression) $1, (Expression) $3);
3319 | equality_expression OP_NE relational_expression
3321 $$ = new Binary (Binary.Operator.Inequality,
3322 (Expression) $1, (Expression) $3);
3327 : equality_expression
3328 | and_expression BITWISE_AND equality_expression
3330 $$ = new Binary (Binary.Operator.BitwiseAnd,
3331 (Expression) $1, (Expression) $3);
3335 exclusive_or_expression
3337 | exclusive_or_expression CARRET and_expression
3339 $$ = new Binary (Binary.Operator.ExclusiveOr,
3340 (Expression) $1, (Expression) $3);
3344 inclusive_or_expression
3345 : exclusive_or_expression
3346 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3348 $$ = new Binary (Binary.Operator.BitwiseOr,
3349 (Expression) $1, (Expression) $3);
3353 conditional_and_expression
3354 : inclusive_or_expression
3355 | conditional_and_expression OP_AND inclusive_or_expression
3357 $$ = new Binary (Binary.Operator.LogicalAnd,
3358 (Expression) $1, (Expression) $3);
3362 conditional_or_expression
3363 : conditional_and_expression
3364 | conditional_or_expression OP_OR conditional_and_expression
3366 $$ = new Binary (Binary.Operator.LogicalOr,
3367 (Expression) $1, (Expression) $3);
3371 conditional_expression
3372 : conditional_or_expression
3373 | conditional_or_expression INTERR expression COLON expression
3375 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
3379 assignment_expression
3380 : prefixed_unary_expression ASSIGN expression
3382 $$ = new Assign ((Expression) $1, (Expression) $3);
3384 | prefixed_unary_expression OP_MULT_ASSIGN expression
3386 $$ = new CompoundAssign (
3387 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
3389 | prefixed_unary_expression OP_DIV_ASSIGN expression
3391 $$ = new CompoundAssign (
3392 Binary.Operator.Division, (Expression) $1, (Expression) $3);
3394 | prefixed_unary_expression OP_MOD_ASSIGN expression
3396 $$ = new CompoundAssign (
3397 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
3399 | prefixed_unary_expression OP_ADD_ASSIGN expression
3401 $$ = new CompoundAssign (
3402 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
3404 | prefixed_unary_expression OP_SUB_ASSIGN expression
3406 $$ = new CompoundAssign (
3407 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3409 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3411 $$ = new CompoundAssign (
3412 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
3414 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3416 $$ = new CompoundAssign (
3417 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
3419 | prefixed_unary_expression OP_AND_ASSIGN expression
3421 $$ = new CompoundAssign (
3422 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
3424 | prefixed_unary_expression OP_OR_ASSIGN expression
3426 $$ = new CompoundAssign (
3427 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
3429 | prefixed_unary_expression OP_XOR_ASSIGN expression
3431 $$ = new CompoundAssign (
3432 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
3437 : conditional_expression
3438 | assignment_expression
3458 MemberName name = MakeName ((MemberName) $5);
3459 int mod_flags = (int) $2;
3462 ClassPart part = PartialContainer.CreatePart (
3463 current_namespace, current_class, name, mod_flags,
3464 (Attributes) $1, Kind.Class, (Location) $3);
3466 current_container = part.PartialContainer;
3467 current_class = part;
3469 if ((mod_flags & Modifiers.STATIC) != 0) {
3470 current_class = new StaticClass (
3471 current_namespace, current_class, name,
3472 mod_flags, (Attributes) $1);
3474 current_class = new Class (
3475 current_namespace, current_class, name,
3476 mod_flags, (Attributes) $1);
3479 current_container.AddClassOrStruct (current_class);
3480 current_container = current_class;
3481 RootContext.Tree.RecordDecl (current_namespace.NS, name, current_class);
3487 if (current_class.Name == "System.Object") {
3488 Report.Error (537, current_class.Location,
3489 "The class System.Object cannot have a base " +
3490 "class or implement an interface.");
3492 current_class.Bases = (ArrayList) $7;
3495 if (RootContext.Documentation != null) {
3496 current_class.DocComment = Lexer.consume_doc_comment ();
3497 Lexer.doc_state = XmlCommentState.Allowed;
3502 if (RootContext.Documentation != null)
3503 Lexer.doc_state = XmlCommentState.Allowed;
3507 $$ = pop_current_class ();
3515 { $$ = $1; } // location
3519 : /* empty */ { $$ = (int) 0; }
3525 | modifiers modifier
3530 if ((m1 & m2) != 0) {
3531 Location l = lexer.Location;
3532 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
3534 $$ = (int) (m1 | m2);
3539 : NEW { $$ = Modifiers.NEW; }
3540 | PUBLIC { $$ = Modifiers.PUBLIC; }
3541 | PROTECTED { $$ = Modifiers.PROTECTED; }
3542 | INTERNAL { $$ = Modifiers.INTERNAL; }
3543 | PRIVATE { $$ = Modifiers.PRIVATE; }
3544 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3545 | SEALED { $$ = Modifiers.SEALED; }
3546 | STATIC { $$ = Modifiers.STATIC; }
3547 | READONLY { $$ = Modifiers.READONLY; }
3548 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3549 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3550 | EXTERN { $$ = Modifiers.EXTERN; }
3551 | VOLATILE { $$ = Modifiers.VOLATILE; }
3552 | UNSAFE { $$ = Modifiers.UNSAFE; }
3556 : /* empty */ { $$ = null; }
3557 | class_base { $$ = $1; }
3561 : COLON type_list { $$ = $2; }
3569 // A block is "contained" on the following places:
3571 // property_declaration as part of the accessor body (get/set)
3572 // operator_declaration
3573 // constructor_declaration
3574 // destructor_declaration
3575 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3580 if (current_block == null){
3581 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, (Location) $1);
3582 top_current_block = current_block;
3584 current_block = new Block (current_block, (Location) $1, Location.Null);
3587 opt_statement_list CLOSE_BRACE
3589 while (current_block.Implicit)
3590 current_block = current_block.Parent;
3592 current_block.SetEndLocation ((Location) $4);
3593 current_block = current_block.Parent;
3594 if (current_block == null)
3595 top_current_block = null;
3606 | statement_list statement
3610 : declaration_statement
3612 if ($1 != null && (Block) $1 != current_block){
3613 current_block.AddStatement ((Statement) $1);
3614 current_block = (Block) $1;
3617 | valid_declaration_statement
3619 current_block.AddStatement ((Statement) $1);
3624 valid_declaration_statement
3627 | expression_statement
3628 | selection_statement
3629 | iteration_statement
3633 | unchecked_statement
3641 : valid_declaration_statement
3642 | declaration_statement
3644 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3649 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3657 $$ = EmptyStatement.Value;
3664 LocatedToken lt = (LocatedToken) $1;
3665 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
3667 if (current_block.AddLabel (lt.Value, labeled, lt.Location))
3668 current_block.AddStatement (labeled);
3673 declaration_statement
3674 : local_variable_declaration SEMICOLON
3677 DictionaryEntry de = (DictionaryEntry) $1;
3678 Expression e = (Expression) de.Key;
3680 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
3684 | local_constant_declaration SEMICOLON
3687 DictionaryEntry de = (DictionaryEntry) $1;
3689 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3695 * The following is from Rhys' grammar:
3696 * > Types in local variable declarations must be recognized as
3697 * > expressions to prevent reduce/reduce errors in the grammar.
3698 * > The expressions are converted into types during semantic analysis.
3701 : primary_expression opt_rank_specifier
3703 // FIXME: Do something smart here regarding the composition of the type.
3705 // Ok, the above "primary_expression" is there to get rid of
3706 // both reduce/reduce and shift/reduces in the grammar, it should
3707 // really just be "type_name". If you use type_name, a reduce/reduce
3708 // creeps up. If you use namespace_or_type_name (which is all we need
3709 // really) two shift/reduces appear.
3712 // So the super-trick is that primary_expression
3713 // can only be either a SimpleName or a MemberAccess.
3714 // The MemberAccess case arises when you have a fully qualified type-name like :
3716 // SimpleName is when you have
3719 Expression expr = (Expression) $1;
3720 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3721 Error_ExpectingTypeName (expr);
3725 // So we extract the string corresponding to the SimpleName
3729 if ((string) $2 == "")
3732 $$ = new ComposedCast ((Expression) $1, (string) $2);
3735 | builtin_types opt_rank_specifier
3737 if ((string) $2 == "")
3740 $$ = new ComposedCast ((Expression) $1, (string) $2);
3744 local_variable_pointer_type
3745 : primary_expression STAR
3747 Expression expr = (Expression) $1;
3749 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3750 Error_ExpectingTypeName (expr);
3754 $$ = new ComposedCast ((Expression) $1, "*");
3756 | builtin_types STAR
3758 $$ = new ComposedCast ((Expression) $1, "*");
3762 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
3764 | local_variable_pointer_type STAR
3766 $$ = new ComposedCast ((Expression) $1, "*");
3770 local_variable_declaration
3771 : local_variable_type variable_declarators
3774 $$ = new DictionaryEntry ($1, $2);
3778 | local_variable_pointer_type opt_rank_specifier variable_declarators
3783 if ((string) $2 == "")
3784 t = (Expression) $1;
3786 t = new ComposedCast ((Expression) $1, (string) $2);
3787 $$ = new DictionaryEntry (t, $3);
3793 local_constant_declaration
3794 : CONST local_variable_type constant_declarators
3797 $$ = new DictionaryEntry ($2, $3);
3803 expression_statement
3804 : statement_expression SEMICOLON
3811 // We have to do the wrapping here and not in the case above,
3812 // because statement_expression is used for example in for_statement
3814 statement_expression
3815 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
3816 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
3817 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
3818 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
3819 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
3820 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
3821 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
3823 Report.Error (1002, GetLocation ($1), "Expecting `;'");
3828 object_creation_expression
3829 : object_or_delegate_creation_expression
3830 { note ("complain if this is a delegate maybe?"); }
3839 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
3842 Location l = (Location) $1;
3844 $$ = new If ((Expression) $3, (Statement) $5, l);
3846 if (RootContext.WarningLevel >= 3){
3847 // FIXME: location for warning should be loc property of $5.
3848 if ($5 == EmptyStatement.Value)
3849 Report.Warning (642, l, "Possible mistaken empty statement");
3853 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
3854 embedded_statement ELSE embedded_statement
3856 Location l = (Location) $1;
3858 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
3860 if (RootContext.WarningLevel >= 3){
3861 // FIXME: location for warning should be loc property of $5 and $7.
3862 if ($5 == EmptyStatement.Value)
3863 Report.Warning (642, l, "Possible mistaken empty statement");
3864 if ($7 == EmptyStatement.Value)
3865 Report.Warning (642, l, "Possible mistaken empty statement");
3871 : SWITCH OPEN_PARENS
3873 switch_stack.Push (current_block);
3875 expression CLOSE_PARENS
3878 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
3879 current_block = (Block) switch_stack.Pop ();
3895 Report.Error (1522, lexer.Location, "Empty switch block");
3903 ArrayList sections = new ArrayList (4);
3908 | switch_sections switch_section
3910 ArrayList sections = (ArrayList) $1;
3920 current_block = current_block.CreateSwitchBlock (lexer.Location);
3924 Block topmost = current_block;
3926 while (topmost.Implicit)
3927 topmost = topmost.Parent;
3928 $$ = new SwitchSection ((ArrayList) $1, topmost);
3935 ArrayList labels = new ArrayList (4);
3940 | switch_labels switch_label
3942 ArrayList labels = (ArrayList) ($1);
3950 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
3951 | DEFAULT COLON { $$ = new SwitchLabel (null, (Location) $1); }
3954 1523, GetLocation ($1),
3955 "The keyword case or default must precede code in switch block");
3967 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
3969 Location l = (Location) $1;
3970 $$ = new While ((Expression) $3, (Statement) $5, l);
3975 : DO embedded_statement
3976 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
3978 Location l = (Location) $1;
3980 $$ = new Do ((Statement) $2, (Expression) $5, l);
3986 opt_for_initializer SEMICOLON
3988 Block assign_block = new Block (current_block);
3989 current_block = assign_block;
3991 if ($3 is DictionaryEntry){
3992 DictionaryEntry de = (DictionaryEntry) $3;
3994 Expression type = (Expression) de.Key;
3995 ArrayList var_declarators = (ArrayList) de.Value;
3997 foreach (VariableDeclaration decl in var_declarators){
4001 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4005 Location l = lexer.Location;
4007 if (decl.expression_or_array_initializer is Expression){
4008 expr = (Expression) decl.expression_or_array_initializer;
4009 } else if (decl.expression_or_array_initializer == null) {
4012 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4013 expr = new ArrayCreation (type, "", init, decl.Location);
4016 LocalVariableReference var;
4017 var = new LocalVariableReference (assign_block, decl.identifier, l);
4020 Assign a = new Assign (var, expr, decl.Location);
4022 assign_block.AddStatement (new StatementExpression (a));
4026 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4027 // This can be referred to as $5 below.
4033 opt_for_condition SEMICOLON
4034 opt_for_iterator CLOSE_PARENS
4037 Location l = (Location) $1;
4039 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4041 current_block.AddStatement (f);
4042 while (current_block.Implicit)
4043 current_block = current_block.Parent;
4045 current_block = current_block.Parent;
4050 : /* empty */ { $$ = EmptyStatement.Value; }
4055 : local_variable_declaration
4056 | statement_expression_list
4060 : /* empty */ { $$ = null; }
4061 | boolean_expression
4065 : /* empty */ { $$ = EmptyStatement.Value; }
4070 : statement_expression_list
4073 statement_expression_list
4074 : statement_expression
4076 // CHANGE: was `null'
4077 Block b = new Block (current_block, Block.Flags.Implicit);
4079 b.AddStatement ((Statement) $1);
4082 | statement_expression_list COMMA statement_expression
4084 Block b = (Block) $1;
4086 b.AddStatement ((Statement) $3);
4092 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4094 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4097 | FOREACH OPEN_PARENS type IDENTIFIER IN
4098 expression CLOSE_PARENS
4100 Block foreach_block = new Block (current_block);
4101 current_block = foreach_block;
4103 LocatedToken lt = (LocatedToken) $4;
4104 Location l = lt.Location;
4107 vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4109 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4111 // Get a writable reference to this read-only variable.
4113 // Note that the $$ here refers to the value of _this_ code block,
4114 // not the value of the LHS non-terminal. This can be referred to as $8 below.
4115 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
4122 LocalVariableReference v = (LocalVariableReference) $8;
4123 Location l = (Location) $1;
4126 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
4127 current_block.AddStatement (f);
4130 while (current_block.Implicit)
4131 current_block = current_block.Parent;
4133 current_block = current_block.Parent;
4139 | continue_statement
4149 $$ = new Break ((Location) $1);
4154 : CONTINUE SEMICOLON
4156 $$ = new Continue ((Location) $1);
4161 : GOTO IDENTIFIER SEMICOLON
4163 LocatedToken lt = (LocatedToken) $2;
4164 $$ = new Goto (lt.Value, lt.Location);
4166 | GOTO CASE constant_expression SEMICOLON
4168 $$ = new GotoCase ((Expression) $3, (Location) $1);
4170 | GOTO DEFAULT SEMICOLON
4172 $$ = new GotoDefault ((Location) $1);
4177 : RETURN opt_expression SEMICOLON
4179 $$ = new Return ((Expression) $2, (Location) $1);
4184 : THROW opt_expression SEMICOLON
4186 $$ = new Throw ((Expression) $2, (Location) $1);
4191 : IDENTIFIER RETURN expression SEMICOLON
4193 LocatedToken lt = (LocatedToken) $1;
4194 string s = lt.Value;
4196 Report.Error (1003, lt.Location, "; expected");
4199 if (RootContext.Version == LanguageVersion.ISO_1){
4200 Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4203 if (iterator_container == null){
4204 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4207 iterator_container.SetYields ();
4208 $$ = new Yield ((Expression) $3, lt.Location);
4211 | IDENTIFIER RETURN SEMICOLON
4213 Report.Error (1627, (Location) $2, "Expression expected after yield return");
4216 | IDENTIFIER BREAK SEMICOLON
4218 LocatedToken lt = (LocatedToken) $1;
4219 string s = lt.Value;
4221 Report.Error (1003, lt.Location, "; expected");
4224 if (RootContext.Version == LanguageVersion.ISO_1){
4225 Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4228 if (iterator_container == null){
4229 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4232 iterator_container.SetYields ();
4233 $$ = new YieldBreak (lt.Location);
4244 : TRY block catch_clauses
4248 ArrayList c = (ArrayList)$3;
4249 for (int i = 0; i < c.Count; ++i) {
4250 Catch cc = (Catch) c [i];
4252 if (i != c.Count - 1)
4253 Report.Error (1017, cc.loc, "Try statement already has an empty catch block");
4260 // Now s contains the list of specific catch clauses
4261 // and g contains the general one.
4263 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4265 | TRY block opt_catch_clauses FINALLY block
4268 ArrayList s = new ArrayList (4);
4269 ArrayList catch_list = (ArrayList) $3;
4271 if (catch_list != null){
4272 foreach (Catch cc in catch_list) {
4280 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4284 Report.Error (1524, (Location) $1, "Expected catch or finally");
4290 : /* empty */ { $$ = null; }
4297 ArrayList l = new ArrayList (4);
4302 | catch_clauses catch_clause
4304 ArrayList l = (ArrayList) $1;
4312 : /* empty */ { $$ = null; }
4317 : CATCH opt_catch_args
4319 Expression type = null;
4322 DictionaryEntry cc = (DictionaryEntry) $2;
4323 type = (Expression) cc.Key;
4324 LocatedToken lt = (LocatedToken) cc.Value;
4327 ArrayList one = new ArrayList (4);
4329 one.Add (new VariableDeclaration (lt, null));
4331 current_block = new Block (current_block);
4332 Block b = declare_local_variables (type, one, lt.Location);
4337 Expression type = null;
4341 DictionaryEntry cc = (DictionaryEntry) $2;
4342 type = (Expression) cc.Key;
4343 LocatedToken lt = (LocatedToken) cc.Value;
4347 while (current_block.Implicit)
4348 current_block = current_block.Parent;
4349 current_block = current_block.Parent;
4353 $$ = new Catch (type, id, (Block) $4, ((Block) $4).loc);
4358 : /* empty */ { $$ = null; }
4363 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4365 $$ = new DictionaryEntry ($2, $3);
4373 $$ = new Checked ((Block) $2);
4380 $$ = new Unchecked ((Block) $2);
4387 RootContext.CheckUnsafeOption ((Location) $1);
4389 $$ = new Unsafe ((Block) $3);
4395 type fixed_pointer_declarators
4398 ArrayList list = (ArrayList) $4;
4399 Expression type = (Expression) $3;
4400 Location l = (Location) $1;
4401 int top = list.Count;
4403 Block assign_block = new Block (current_block);
4404 current_block = assign_block;
4406 for (int i = 0; i < top; i++){
4407 Pair p = (Pair) list [i];
4410 v = current_block.AddVariable (type, (string) p.First, l);
4414 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4422 Location l = (Location) $1;
4424 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4426 current_block.AddStatement (f);
4427 while (current_block.Implicit)
4428 current_block = current_block.Parent;
4430 current_block = current_block.Parent;
4434 fixed_pointer_declarators
4435 : fixed_pointer_declarator {
4436 ArrayList declarators = new ArrayList (4);
4438 declarators.Add ($1);
4441 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4443 ArrayList declarators = (ArrayList) $1;
4445 declarators.Add ($3);
4450 fixed_pointer_declarator
4451 : IDENTIFIER ASSIGN expression
4453 LocatedToken lt = (LocatedToken) $1;
4454 // FIXME: keep location
4455 $$ = new Pair (lt.Value, $3);
4459 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
4465 : LOCK OPEN_PARENS expression CLOSE_PARENS
4471 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
4476 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4478 Block assign_block = new Block (current_block);
4479 current_block = assign_block;
4481 if ($3 is DictionaryEntry){
4482 DictionaryEntry de = (DictionaryEntry) $3;
4483 Location l = (Location) $1;
4485 Expression type = (Expression) de.Key;
4486 ArrayList var_declarators = (ArrayList) de.Value;
4488 ArrayList vars = new ArrayList (4);
4490 foreach (VariableDeclaration decl in var_declarators){
4492 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4495 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
4498 if (decl.expression_or_array_initializer is Expression){
4499 expr = (Expression) decl.expression_or_array_initializer;
4501 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4503 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4506 expr = new ArrayCreation (type, "", init, decl.Location);
4509 LocalVariableReference var;
4511 // Get a writable reference to this read-only variable.
4512 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4514 // This is so that it is not a warning on using variables
4517 vars.Add (new DictionaryEntry (var, expr));
4519 // Assign a = new Assign (var, expr, decl.Location);
4520 // assign_block.AddStatement (new StatementExpression (a));
4523 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4524 // It can be referred to as $5 below.
4525 $$ = new DictionaryEntry (type, vars);
4532 Using u = new Using ($5, (Statement) $6, (Location) $1);
4533 current_block.AddStatement (u);
4534 while (current_block.Implicit)
4535 current_block = current_block.Parent;
4537 current_block = current_block.Parent;
4541 resource_acquisition
4542 : local_variable_declaration
4549 // A class used to pass around variable declarations and constants
4551 public class VariableDeclaration {
4552 public string identifier;
4553 public object expression_or_array_initializer;
4554 public Location Location;
4555 public Attributes OptAttributes;
4556 public string DocComment;
4558 public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
4560 this.identifier = lt.Value;
4561 this.expression_or_array_initializer = eoai;
4562 this.Location = lt.Location;
4563 this.OptAttributes = opt_attrs;
4566 public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
4572 /// Used to pass around interface property information
4574 public class InterfaceAccessorInfo {
4576 public readonly Accessor Get, Set;
4578 public InterfaceAccessorInfo (bool has_get, bool has_set,
4579 Attributes get_attrs, Attributes set_attrs, int get_mod, int set_mod, Location get_loc, Location set_loc)
4582 Get = new Accessor (null, get_mod, get_attrs, get_loc);
4584 Set = new Accessor (null, set_mod, set_attrs, set_loc);
4591 // A class used to hold info about an indexer declarator
4593 public class IndexerDeclaration {
4594 public Expression type;
4595 public MemberName interface_type;
4596 public Parameters param_list;
4597 public Location location;
4599 public IndexerDeclaration (Expression type, MemberName interface_type,
4600 Parameters param_list, Location loc)
4603 this.interface_type = interface_type;
4604 this.param_list = param_list;
4605 this.location = loc;
4610 // We use this when we do not have an object in advance that is an IIteratorContainer
4612 public class SimpleIteratorContainer : IIteratorContainer {
4615 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4620 public static SimpleIteratorContainer GetSimple () {
4621 Simple.Yields = false;
4625 public void SetYields () { Yields = true; }
4629 // A class used to hold info about an operator declarator
4631 public class OperatorDeclaration {
4632 public Operator.OpType optype;
4633 public Expression ret_type, arg1type, arg2type;
4634 public string arg1name, arg2name;
4635 public Location location;
4637 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4638 Expression arg1type, string arg1name,
4639 Expression arg2type, string arg2name, Location location)
4642 this.ret_type = ret_type;
4643 this.arg1type = arg1type;
4644 this.arg1name = arg1name;
4645 this.arg2type = arg2type;
4646 this.arg2name = arg2name;
4647 this.location = location;
4652 void Error_ExpectingTypeName (Expression expr)
4654 if (expr is Invocation){
4655 Report.Error (1002, expr.Location, "Expecting `;'");
4657 Report.Error (-1, expr.Location, "Invalid Type definition");
4661 TypeContainer pop_current_class ()
4663 TypeContainer retval = current_class;
4665 current_class = current_class.Parent;
4666 current_container = current_container.Parent;
4668 if (current_class != current_container) {
4669 if (!(current_class is ClassPart) ||
4670 ((ClassPart) current_class).PartialContainer != current_container)
4671 throw new InternalErrorException ();
4672 } else if (current_container is ClassPart)
4673 current_container = ((ClassPart) current_class).PartialContainer;
4679 // Given the @class_name name, it creates a fully qualified name
4680 // based on the containing declaration space
4683 MakeName (MemberName class_name)
4685 Namespace ns = current_namespace.NS;
4687 if (current_container.Name == ""){
4689 return new MemberName (ns.MemberName, class_name);
4693 return new MemberName (current_container.MemberName, class_name);
4697 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4699 Block implicit_block;
4700 ArrayList inits = null;
4703 // We use the `Used' property to check whether statements
4704 // have been added to the current block. If so, we need
4705 // to create another block to contain the new declaration
4706 // otherwise, as an optimization, we use the same block to
4707 // add the declaration.
4709 // FIXME: A further optimization is to check if the statements
4710 // that were added were added as part of the initialization
4711 // below. In which case, no other statements have been executed
4712 // and we might be able to reduce the number of blocks for
4713 // situations like this:
4715 // int j = 1; int k = j + 1;
4717 if (current_block.Used)
4718 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4720 implicit_block = current_block;
4722 foreach (VariableDeclaration decl in variable_declarators){
4724 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
4725 if (decl.expression_or_array_initializer != null){
4727 inits = new ArrayList (4);
4734 return implicit_block;
4736 foreach (VariableDeclaration decl in inits){
4740 if (decl.expression_or_array_initializer is Expression){
4741 expr = (Expression) decl.expression_or_array_initializer;
4744 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4746 expr = new ArrayCreation (type, "", init, decl.Location);
4749 LocalVariableReference var;
4750 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4752 assign = new Assign (var, expr, decl.Location);
4754 implicit_block.AddStatement (new StatementExpression (assign));
4757 return implicit_block;
4760 Block declare_local_constants (Expression type, ArrayList declarators)
4762 Block implicit_block;
4764 if (current_block.Used)
4765 implicit_block = new Block (current_block, Block.Flags.Implicit);
4767 implicit_block = current_block;
4769 foreach (VariableDeclaration decl in declarators){
4770 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
4773 return implicit_block;
4776 void CheckAttributeTarget (string a, Location l)
4780 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4784 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4790 void CheckUnaryOperator (Operator.OpType op, Location l)
4794 case Operator.OpType.LogicalNot:
4795 case Operator.OpType.OnesComplement:
4796 case Operator.OpType.Increment:
4797 case Operator.OpType.Decrement:
4798 case Operator.OpType.True:
4799 case Operator.OpType.False:
4800 case Operator.OpType.Addition:
4801 case Operator.OpType.Subtraction:
4806 Report.Error (1019, l, "Overloadable unary operator expected");
4812 void CheckBinaryOperator (Operator.OpType op, Location l)
4816 case Operator.OpType.Addition:
4817 case Operator.OpType.Subtraction:
4818 case Operator.OpType.Multiply:
4819 case Operator.OpType.Division:
4820 case Operator.OpType.Modulus:
4821 case Operator.OpType.BitwiseAnd:
4822 case Operator.OpType.BitwiseOr:
4823 case Operator.OpType.ExclusiveOr:
4824 case Operator.OpType.LeftShift:
4825 case Operator.OpType.RightShift:
4826 case Operator.OpType.Equality:
4827 case Operator.OpType.Inequality:
4828 case Operator.OpType.GreaterThan:
4829 case Operator.OpType.LessThan:
4830 case Operator.OpType.GreaterThanOrEqual:
4831 case Operator.OpType.LessThanOrEqual:
4835 Report.Error (1020, l, "Overloadable binary operator expected");
4841 void syntax_error (Location l, string msg)
4843 Report.Error (1003, l, "Syntax error, " + msg);
4846 void note (string s)
4848 // Used to put annotations
4853 public Tokenizer Lexer {
4859 public CSharpParser (StreamReader reader, SourceFile file, ArrayList defines)
4861 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4862 this.name = file.Name;
4864 current_container = RootContext.Tree.Types;
4865 // TODO: Make RootContext.Tree.Types a PartialContainer.
4866 current_class = current_container;
4867 current_container.NamespaceEntry = current_namespace;
4868 oob_stack = new Stack ();
4869 switch_stack = new Stack ();
4871 lexer = new Tokenizer (reader, file, defines);
4874 public void parse ()
4877 if (yacc_verbose_flag > 1)
4878 yyparse (lexer, new yydebug.yyDebugSimple ());
4881 Tokenizer tokenizer = lexer as Tokenizer;
4882 tokenizer.cleanup ();
4883 } catch (Exception e){
4885 // Removed for production use, use parser verbose to get the output.
4887 // Console.WriteLine (e);
4888 Report.Error (-25, lexer.Location, "Parsing error");
4889 if (yacc_verbose_flag > 0)
4890 Console.WriteLine (e);
4893 RootContext.Tree.Types.NamespaceEntry = null;
4896 void CheckToken (int error, int yyToken, string msg, Location loc)
4898 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4899 Report.Error (error, loc, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4902 Report.Error (error, loc, msg);
4905 void CheckIdentifierToken (int yyToken, Location loc)
4907 CheckToken (1041, yyToken, "Identifier expected", loc);
4910 string ConsumeStoredComment ()
4912 string s = tmpComment;
4914 Lexer.doc_state = XmlCommentState.Allowed;
4918 Location GetLocation (object obj)
4920 if (obj is MemberCore)
4921 return ((MemberCore) obj).Location;
4922 if (obj is MemberName)
4923 return ((MemberName) obj).Location;
4924 if (obj is LocatedToken)
4925 return ((LocatedToken) obj).Location;
4926 if (obj is Location)
4927 return (Location) obj;
4928 return lexer.Location;