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;
46 Delegate current_delegate;
49 /// This is used by the unary_expression code to resolve
50 /// a name against a parameter.
52 Parameters current_local_parameters;
55 /// Using during property parsing to describe the implicit
56 /// value parameter that is passed to the "set" and "get"accesor
57 /// methods (properties and indexers).
59 Expression implicit_value_parameter_type;
60 Parameters indexer_parameters;
63 /// Used to determine if we are parsing the get/set pair
64 /// of an indexer or a property
69 /// An out-of-band stack.
78 static public int yacc_verbose_flag;
80 // Name of the file we are parsing
89 /// Temporary Xml documentation cache.
90 /// For enum types, we need one more temporary store.
93 string enumTypeComment;
95 /// Current attribute target
96 string current_attr_target;
98 /// assembly and module attribute definitions are enabled
99 bool global_attrs_enabled = true;
100 bool has_get, has_set;
105 %token NONE /* This token is never returned by our lexer */
106 %token ERROR // This is used not by the parser, but by the tokenizer.
110 *These are the C# keywords
197 /* C# keywords which are not really keywords */
203 /* C# single character operators/punctuation. */
204 %token OPEN_BRACE "{"
205 %token CLOSE_BRACE "}"
206 %token OPEN_BRACKET "["
207 %token CLOSE_BRACKET "]"
208 %token OPEN_PARENS "("
209 %token CLOSE_PARENS ")"
221 %token OP_GENERICS_LT "<"
223 %token OP_GENERICS_GT ">"
224 %token BITWISE_AND "&"
225 %token BITWISE_OR "|"
232 /* C# multi-character operators. */
233 %token DOUBLE_COLON "::"
236 %token OP_SHIFT_LEFT "<<"
237 %token OP_SHIFT_RIGHT ">>"
244 %token OP_MULT_ASSIGN "*="
245 %token OP_DIV_ASSIGN "/="
246 %token OP_MOD_ASSIGN "%="
247 %token OP_ADD_ASSIGN "+="
248 %token OP_SUB_ASSIGN "-="
249 %token OP_SHIFT_LEFT_ASSIGN "<<="
250 %token OP_SHIFT_RIGHT_ASSIGN ">>="
251 %token OP_AND_ASSIGN "&="
252 %token OP_XOR_ASSIGN "^="
253 %token OP_OR_ASSIGN "|="
257 %token LITERAL_INTEGER "int literal"
258 %token LITERAL_FLOAT "float literal"
259 %token LITERAL_DOUBLE "double literal"
260 %token LITERAL_DECIMAL "decimal literal"
261 %token LITERAL_CHARACTER "character literal"
262 %token LITERAL_STRING "string literal"
265 %token CLOSE_PARENS_CAST
266 %token CLOSE_PARENS_NO_CAST
267 %token CLOSE_PARENS_OPEN_PARENS
268 %token CLOSE_PARENS_MINUS
269 %token DEFAULT_OPEN_PARENS
270 %token GENERIC_DIMENSION
272 /* Add precedence rules to solve dangling else s/r conflict */
281 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
283 %left STAR DIV PERCENT
284 %right BANG CARRET UMINUS
285 %nonassoc OP_INC OP_DEC
287 %left OPEN_BRACKET OPEN_BRACE
291 %start compilation_unit
295 : outer_declarations opt_EOF
296 | outer_declarations global_attributes opt_EOF
297 | global_attributes opt_EOF
298 | opt_EOF /* allow empty files */
304 Lexer.check_incorrect_doc_comment ();
308 Lexer.check_incorrect_doc_comment ();
314 | outer_declarations outer_declaration
319 | namespace_member_declaration
324 | using_directives using_directive
328 : using_alias_directive
330 if (RootContext.Documentation != null)
331 Lexer.doc_state = XmlCommentState.Allowed;
333 | using_namespace_directive
335 if (RootContext.Documentation != null)
336 Lexer.doc_state = XmlCommentState.Allowed;
340 using_alias_directive
341 : USING IDENTIFIER ASSIGN
342 namespace_or_type_name SEMICOLON
344 LocatedToken lt = (LocatedToken) $2;
345 current_namespace.UsingAlias (lt.Value, (MemberName) $4, (Location) $1);
348 CheckIdentifierToken (yyToken, GetLocation ($2));
352 using_namespace_directive
353 : USING namespace_name SEMICOLON
355 current_namespace.Using ((MemberName) $2, (Location) $1);
360 // Strictly speaking, namespaces don't have attributes but
361 // we parse global attributes along with namespace declarations and then
364 namespace_declaration
365 : opt_attributes NAMESPACE namespace_or_type_name
367 MemberName name = (MemberName) $3;
370 Report.Error(1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
373 if (name.TypeArguments != null)
374 syntax_error (lexer.Location, "namespace name expected");
376 current_namespace = new NamespaceEntry (
377 current_namespace, file, name.GetName (), name.Location);
379 namespace_body opt_semicolon
381 current_namespace = current_namespace.Parent;
396 : namespace_or_type_name {
397 MemberName name = (MemberName) $1;
399 if (name.TypeArguments != null)
400 syntax_error (lexer.Location, "namespace name expected");
409 if (RootContext.Documentation != null)
410 Lexer.doc_state = XmlCommentState.Allowed;
413 opt_namespace_member_declarations
422 opt_namespace_member_declarations
424 | namespace_member_declarations
427 namespace_member_declarations
428 : namespace_member_declaration
429 | namespace_member_declarations namespace_member_declaration
432 namespace_member_declaration
436 DeclSpace ds = (DeclSpace)$1;
438 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
439 Report.Error (1527, ds.Location,
440 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
443 current_namespace.DeclarationFound = true;
445 | namespace_declaration {
446 current_namespace.DeclarationFound = true;
449 | field_declaration {
450 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
452 | method_declaration {
453 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
460 | interface_declaration
462 | delegate_declaration
464 // Enable this when we have handled all errors, because this acts as a generic fallback
467 // Console.WriteLine ("Token=" + yyToken);
468 // Report.Error (1518, GetLocation ($1), "Expected class, struct, interface, enum or delegate");
480 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
488 global_attrs_enabled = false;
493 global_attrs_enabled = false;
502 ArrayList sect = (ArrayList) $1;
504 if (global_attrs_enabled) {
505 if (current_attr_target == "module") {
506 CodeGen.Module.AddAttributes (sect);
508 } else if (current_attr_target != null && current_attr_target.Length > 0) {
509 CodeGen.Assembly.AddAttributes (sect);
512 $$ = new Attributes (sect);
515 if (RootContext.Documentation != null) {
516 Lexer.check_incorrect_doc_comment ();
518 XmlCommentState.Allowed;
522 $$ = new Attributes (sect);
524 current_attr_target = null;
526 | attribute_sections attribute_section
528 Attributes attrs = $1 as Attributes;
529 ArrayList sect = (ArrayList) $2;
531 if (global_attrs_enabled) {
532 if (current_attr_target == "module") {
533 CodeGen.Module.AddAttributes (sect);
535 } else if (current_attr_target == "assembly") {
536 CodeGen.Assembly.AddAttributes (sect);
540 attrs = new Attributes (sect);
542 attrs.AddAttributes (sect);
546 attrs = new Attributes (sect);
548 attrs.AddAttributes (sect);
551 current_attr_target = null;
556 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
560 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
566 attribute_target_specifier
567 : attribute_target COLON
569 current_attr_target = (string)$1;
577 LocatedToken lt = (LocatedToken) $1;
578 CheckAttributeTarget (lt.Value, lt.Location);
579 $$ = lt.Value; // Location won't be required anymore.
581 | EVENT { $$ = "event"; }
582 | RETURN { $$ = "return"; }
588 ArrayList attrs = new ArrayList (4);
594 | attribute_list COMMA attribute
596 ArrayList attrs = (ArrayList) $1;
604 : attribute_name opt_attribute_arguments
606 MemberName mname = (MemberName) $1;
607 if (mname.IsGeneric) {
608 Report.Error (404, lexer.Location,
609 "'<' unexpected: attributes cannot be generic");
612 ArrayList arguments = (ArrayList) $2;
613 MemberName left = mname.Left;
614 string identifier = mname.Name;
616 Expression left_expr = left == null ? null : left.GetTypeExpression ();
618 if (current_attr_target == "assembly" || current_attr_target == "module")
619 // FIXME: supply "nameEscaped" parameter here.
620 $$ = new GlobalAttribute (current_class, current_attr_target,
621 left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
623 $$ = new Attribute (current_attr_target, left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
628 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
631 opt_attribute_arguments
632 : /* empty */ { $$ = null; }
633 | OPEN_PARENS attribute_arguments CLOSE_PARENS
641 : opt_positional_argument_list
646 ArrayList args = new ArrayList (4);
652 | positional_argument_list COMMA named_argument_list
654 ArrayList args = new ArrayList (4);
660 | named_argument_list
662 ArrayList args = new ArrayList (4);
671 opt_positional_argument_list
672 : /* empty */ { $$ = null; }
673 | positional_argument_list
676 positional_argument_list
679 ArrayList args = new ArrayList (4);
680 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
684 | positional_argument_list COMMA expression
686 ArrayList args = (ArrayList) $1;
687 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
696 ArrayList args = new ArrayList (4);
701 | named_argument_list COMMA named_argument
703 ArrayList args = (ArrayList) $1;
708 | named_argument_list COMMA expression
710 Report.Error (1016, ((Expression) $3).Location, "Named attribute argument expected");
716 : IDENTIFIER ASSIGN expression
718 // FIXME: keep location
719 $$ = new DictionaryEntry (
720 ((LocatedToken) $1).Value,
721 new Argument ((Expression) $3, Argument.AType.Expression));
727 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
730 opt_class_member_declarations
732 | class_member_declarations
735 class_member_declarations
736 : class_member_declaration
737 | class_member_declarations
738 class_member_declaration
741 class_member_declaration
742 : constant_declaration // done
743 | field_declaration // done
744 | method_declaration // done
745 | property_declaration // done
746 | event_declaration // done
747 | indexer_declaration // done
748 | operator_declaration // done
749 | constructor_declaration // done
750 | destructor_declaration // done
760 lexer.ConstraintsParsing = true;
764 MemberName name = MakeName ((MemberName) $6);
766 ClassPart part = PartialContainer.CreatePart (
767 current_namespace, current_class, name, (int) $2,
768 (Attributes) $1, Kind.Struct, (Location) $3);
770 current_container = part.PartialContainer;
771 current_class = part;
773 current_class = new Struct (
774 current_namespace, current_class, name, (int) $2,
777 current_container.AddClassOrStruct (current_class);
778 current_container = current_class;
779 RootContext.Tree.RecordDecl (current_namespace.NS, name, current_class);
783 opt_type_parameter_constraints_clauses
785 lexer.ConstraintsParsing = false;
788 current_class.Bases = (ArrayList) $8;
790 current_class.SetParameterInfo ((ArrayList) $9);
792 if (RootContext.Documentation != null)
793 current_class.DocComment = Lexer.consume_doc_comment ();
797 if (RootContext.Documentation != null)
798 Lexer.doc_state = XmlCommentState.Allowed;
802 $$ = pop_current_class ();
804 | opt_attributes opt_modifiers opt_partial STRUCT error {
805 CheckIdentifierToken (yyToken, GetLocation ($5));
812 if (RootContext.Documentation != null)
813 Lexer.doc_state = XmlCommentState.Allowed;
815 opt_struct_member_declarations CLOSE_BRACE
818 opt_struct_member_declarations
820 | struct_member_declarations
823 struct_member_declarations
824 : struct_member_declaration
825 | struct_member_declarations struct_member_declaration
828 struct_member_declaration
829 : constant_declaration
832 | property_declaration
834 | indexer_declaration
835 | operator_declaration
836 | constructor_declaration
840 * This is only included so we can flag error 575:
841 * destructors only allowed on class types
843 | destructor_declaration
854 int modflags = (int) $2;
855 foreach (VariableDeclaration constant in (ArrayList) $5){
856 Location l = constant.Location;
857 if ((modflags & Modifiers.STATIC) != 0) {
858 Report.Error (504, l, "The constant `{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
862 Const c = new Const (
863 current_class, (Expression) $4, (string) constant.identifier,
864 (Expression) constant.expression_or_array_initializer, modflags,
867 if (RootContext.Documentation != null) {
868 c.DocComment = Lexer.consume_doc_comment ();
869 Lexer.doc_state = XmlCommentState.Allowed;
871 current_container.AddConstant (c);
877 : constant_declarator
879 ArrayList constants = new ArrayList (4);
884 | constant_declarators COMMA constant_declarator
887 ArrayList constants = (ArrayList) $1;
894 : IDENTIFIER ASSIGN constant_expression
896 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
900 // A const field requires a value to be provided
901 Report.Error (145, ((LocatedToken) $1).Location, "A const field requires a value to be provided");
913 Expression type = (Expression) $3;
916 foreach (VariableDeclaration var in (ArrayList) $4){
917 Field field = new Field (current_class, type, mod, var.identifier,
918 var.expression_or_array_initializer,
919 (Attributes) $1, var.Location);
921 if (RootContext.Documentation != null) {
922 field.DocComment = Lexer.consume_doc_comment ();
923 Lexer.doc_state = XmlCommentState.Allowed;
925 current_container.AddField (field);
926 $$ = field; // FIXME: might be better if it points to the top item
933 fixed_variable_declarators
936 Expression type = (Expression) $4;
939 foreach (VariableDeclaration var in (ArrayList) $5) {
940 FixedField field = new FixedField (current_class, type, mod, var.identifier,
941 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
943 if (RootContext.Documentation != null) {
944 field.DocComment = Lexer.consume_doc_comment ();
945 Lexer.doc_state = XmlCommentState.Allowed;
947 current_container.AddField (field);
948 $$ = field; // FIXME: might be better if it points to the top item
956 Report.Error (670, (Location) $3, "Fields cannot have void type");
960 fixed_variable_declarators
961 : fixed_variable_declarator
963 ArrayList decl = new ArrayList (2);
967 | fixed_variable_declarators COMMA fixed_variable_declarator
969 ArrayList decls = (ArrayList) $1;
975 fixed_variable_declarator
976 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
978 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
983 : variable_declarator
985 ArrayList decl = new ArrayList (4);
990 | variable_declarators COMMA variable_declarator
992 ArrayList decls = (ArrayList) $1;
999 : IDENTIFIER ASSIGN variable_initializer
1001 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1005 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1007 | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
1009 Report.Error (650, ((LocatedToken) $1).Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
1010 "To declare a fixed size buffer field, use the fixed keyword before the field type");
1015 variable_initializer
1024 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
1026 $$ = new StackAlloc ((Expression) $2, (Expression) $4, (Location) $1);
1030 Report.Error (1575, (Location) $1, "A stackalloc expression requires [] after type");
1037 iterator_container = (IIteratorContainer) $1;
1038 if (RootContext.Documentation != null)
1039 Lexer.doc_state = XmlCommentState.NotAllowed;
1043 Method method = (Method) $1;
1044 method.Block = (ToplevelBlock) $3;
1045 current_container.AddMethod (method);
1047 current_local_parameters = null;
1048 iterator_container = null;
1050 if (RootContext.Documentation != null)
1051 Lexer.doc_state = XmlCommentState.Allowed;
1064 Report.Error (1585, lexer.Location,
1065 "Member modifier `{0}' must precede the member type and name",
1066 Modifiers.Name (i));
1077 type namespace_or_type_name
1078 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1080 lexer.ConstraintsParsing = true;
1082 opt_type_parameter_constraints_clauses
1084 lexer.ConstraintsParsing = false;
1086 MemberName name = (MemberName) $4;
1088 if ($9 != null && name.TypeArguments == null)
1089 Report.Error (80, lexer.Location,
1090 "Contraints are not allowed on non-generic declarations");
1094 GenericMethod generic = null;
1095 if (name.TypeArguments != null) {
1096 generic = new GenericMethod (current_namespace, current_class, name);
1098 generic.SetParameterInfo ((ArrayList) $9);
1101 method = new Method (current_class, generic, (Expression) $3, (int) $2, false,
1102 name, (Parameters) $6, (Attributes) $1);
1104 current_local_parameters = (Parameters) $6;
1106 if (RootContext.Documentation != null)
1107 method.DocComment = Lexer.consume_doc_comment ();
1113 VOID namespace_or_type_name
1114 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1116 lexer.ConstraintsParsing = true;
1118 opt_type_parameter_constraints_clauses
1120 lexer.ConstraintsParsing = false;
1122 MemberName name = (MemberName) $4;
1124 if ($9 != null && name.TypeArguments == null)
1125 Report.Error (80, lexer.Location,
1126 "Contraints are not allowed on non-generic declarations");
1129 GenericMethod generic = null;
1130 if (name.TypeArguments != null) {
1131 generic = new GenericMethod (current_namespace, current_class, name);
1133 generic.SetParameterInfo ((ArrayList) $9);
1136 method = new Method (current_class, generic, TypeManager.system_void_expr,
1137 (int) $2, false, name, (Parameters) $6, (Attributes) $1);
1139 current_local_parameters = (Parameters) $6;
1141 if (RootContext.Documentation != null)
1142 method.DocComment = Lexer.consume_doc_comment ();
1149 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1151 MemberName name = (MemberName) $5;
1152 Report.Error (1585, name.Location,
1153 "Member modifier `{0}' must precede the member type and name", Modifiers.Name ((int) $4));
1155 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1156 0, false, name, (Parameters) $6, (Attributes) $1);
1158 current_local_parameters = (Parameters) $6;
1160 if (RootContext.Documentation != null)
1161 method.DocComment = Lexer.consume_doc_comment ();
1169 | SEMICOLON { $$ = null; }
1172 opt_formal_parameter_list
1173 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1174 | formal_parameter_list
1177 formal_parameter_list
1180 ArrayList pars_list = (ArrayList) $1;
1182 Parameter [] pars = new Parameter [pars_list.Count];
1183 pars_list.CopyTo (pars);
1185 $$ = new Parameters (pars, null);
1187 | fixed_parameters COMMA parameter_array
1189 ArrayList pars_list = (ArrayList) $1;
1191 Parameter [] pars = new Parameter [pars_list.Count];
1192 pars_list.CopyTo (pars);
1194 $$ = new Parameters (pars, (Parameter) $3);
1196 | fixed_parameters COMMA ARGLIST
1198 ArrayList pars_list = (ArrayList) $1;
1200 Parameter [] pars = new Parameter [pars_list.Count];
1201 pars_list.CopyTo (pars);
1203 $$ = new Parameters (pars, true);
1205 | parameter_array COMMA fixed_parameters
1208 Report.Error (231, ((Parameter) $1).Location, "A params parameter must be the last parameter in a formal parameter list");
1211 | ARGLIST COMMA fixed_parameters
1213 Report.Error (257, (Location) $1, "An __arglist parameter must be the last parameter in a formal parameter list");
1218 $$ = new Parameters (null, (Parameter) $1);
1222 $$ = new Parameters (null, true);
1229 ArrayList pars = new ArrayList (4);
1234 | fixed_parameters COMMA fixed_parameter
1236 ArrayList pars = (ArrayList) $1;
1245 opt_parameter_modifier
1249 LocatedToken lt = (LocatedToken) $4;
1250 $$ = new Parameter ((Expression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1253 opt_parameter_modifier
1255 IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1257 LocatedToken lt = (LocatedToken) $4;
1258 Report.Error (1552, lt.Location, "Array type specifier, [], must appear before parameter name");
1262 opt_parameter_modifier
1265 Report.Error (1001, GetLocation ($3), "Identifier expected");
1269 opt_parameter_modifier
1272 CheckIdentifierToken (yyToken, GetLocation ($4));
1276 opt_parameter_modifier
1282 LocatedToken lt = (LocatedToken) $4;
1283 Report.Error (241, lt.Location, "Default parameter specifiers are not permitted");
1288 opt_parameter_modifier
1289 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1290 | parameter_modifier
1294 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1295 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1299 : opt_attributes PARAMS type IDENTIFIER
1301 LocatedToken lt = (LocatedToken) $4;
1302 $$ = new Parameter ((Expression) $3, lt.Value, Parameter.Modifier.PARAMS, (Attributes) $1, lt.Location);
1303 note ("type must be a single-dimension array type");
1305 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1307 Report.Error (1611, (Location) $2, "The params parameter cannot be declared as ref or out");
1310 | opt_attributes PARAMS type error {
1311 CheckIdentifierToken (yyToken, GetLocation ($4));
1316 property_declaration
1320 namespace_or_type_name
1322 if (RootContext.Documentation != null)
1323 tmpComment = Lexer.consume_doc_comment ();
1327 implicit_value_parameter_type = (Expression) $3;
1329 lexer.PropertyParsing = true;
1331 accessor_declarations
1333 lexer.PropertyParsing = false;
1334 has_get = has_set = false;
1342 Pair pair = (Pair) $8;
1343 Accessor get_block = (Accessor) pair.First;
1344 Accessor set_block = (Accessor) pair.Second;
1346 MemberName name = (MemberName) $4;
1348 if (name.TypeArguments != null)
1349 syntax_error (lexer.Location, "a property can't have type arguments");
1351 prop = new Property (current_class, (Expression) $3, (int) $2, false,
1352 name, (Attributes) $1, get_block, set_block);
1354 current_container.AddProperty (prop);
1355 implicit_value_parameter_type = null;
1357 if (RootContext.Documentation != null)
1358 prop.DocComment = ConsumeStoredComment ();
1363 accessor_declarations
1364 : get_accessor_declaration
1366 $$ = new Pair ($1, null);
1368 | get_accessor_declaration accessor_declarations
1370 Pair pair = (Pair) $2;
1374 | set_accessor_declaration
1376 $$ = new Pair (null, $1);
1378 | set_accessor_declaration accessor_declarations
1380 Pair pair = (Pair) $2;
1386 Report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1391 get_accessor_declaration
1392 : opt_attributes opt_modifiers GET
1394 // If this is not the case, then current_local_parameters has already
1395 // been set in indexer_declaration
1396 if (parsing_indexer == false)
1397 current_local_parameters = null;
1399 current_local_parameters = indexer_parameters;
1400 lexer.PropertyParsing = false;
1402 iterator_container = SimpleIteratorContainer.GetSimple ();
1407 Report.Error (1007, (Location) $3, "Property accessor already defined");
1410 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1412 current_local_parameters = null;
1413 lexer.PropertyParsing = true;
1415 if (SimpleIteratorContainer.Simple.Yields)
1416 accessor.SetYields ();
1418 iterator_container = null;
1420 if (RootContext.Documentation != null)
1421 if (Lexer.doc_state == XmlCommentState.Error)
1422 Lexer.doc_state = XmlCommentState.NotAllowed;
1428 set_accessor_declaration
1429 : opt_attributes opt_modifiers SET
1432 Parameter implicit_value_parameter = new Parameter (
1433 implicit_value_parameter_type, "value",
1434 Parameter.Modifier.NONE, null, (Location) $3);
1436 if (parsing_indexer == false) {
1437 args = new Parameter [1];
1438 args [0] = implicit_value_parameter;
1439 current_local_parameters = new Parameters (args, null);
1441 Parameter [] fpars = indexer_parameters.FixedParameters;
1444 int count = fpars.Length;
1446 args = new Parameter [count + 1];
1447 fpars.CopyTo (args, 0);
1448 args [count] = implicit_value_parameter;
1451 current_local_parameters = new Parameters (
1452 args, indexer_parameters.ArrayParameter);
1455 lexer.PropertyParsing = false;
1457 iterator_container = SimpleIteratorContainer.GetSimple ();
1462 Report.Error (1007, ((LocatedToken) $3).Location, "Property accessor already defined");
1465 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1467 current_local_parameters = null;
1468 lexer.PropertyParsing = true;
1470 if (SimpleIteratorContainer.Simple.Yields)
1471 accessor.SetYields ();
1473 iterator_container = null;
1475 if (RootContext.Documentation != null
1476 && Lexer.doc_state == XmlCommentState.Error)
1477 Lexer.doc_state = XmlCommentState.NotAllowed;
1485 | SEMICOLON { $$ = null; }
1488 interface_declaration
1494 lexer.ConstraintsParsing = true;
1498 MemberName name = MakeName ((MemberName) $6);
1501 ClassPart part = PartialContainer.CreatePart (
1502 current_namespace, current_class, name, (int) $2,
1503 (Attributes) $1, Kind.Interface, (Location) $3);
1505 current_container = part.PartialContainer;
1506 current_class = part;
1508 current_class = new Interface (
1509 current_namespace, current_class, name, (int) $2,
1512 current_container.AddInterface (current_class);
1513 current_container = current_class;
1514 RootContext.Tree.RecordDecl (current_namespace.NS, name, current_class);
1518 opt_type_parameter_constraints_clauses
1520 lexer.ConstraintsParsing = false;
1523 current_class.Bases = (ArrayList) $8;
1525 current_class.SetParameterInfo ((ArrayList) $9);
1527 if (RootContext.Documentation != null) {
1528 current_class.DocComment = Lexer.consume_doc_comment ();
1529 Lexer.doc_state = XmlCommentState.Allowed;
1534 if (RootContext.Documentation != null)
1535 Lexer.doc_state = XmlCommentState.Allowed;
1539 $$ = pop_current_class ();
1541 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1542 CheckIdentifierToken (yyToken, GetLocation ($5));
1548 opt_interface_member_declarations
1552 opt_interface_member_declarations
1554 | interface_member_declarations
1557 interface_member_declarations
1558 : interface_member_declaration
1559 | interface_member_declarations interface_member_declaration
1562 interface_member_declaration
1563 : interface_method_declaration
1568 Method m = (Method) $1;
1570 if (m.IsExplicitImpl)
1571 Report.Error (541, m.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1572 m.GetSignatureForError ());
1574 current_container.AddMethod (m);
1576 if (RootContext.Documentation != null)
1577 Lexer.doc_state = XmlCommentState.Allowed;
1579 | interface_property_declaration
1584 Property p = (Property) $1;
1586 if (p.IsExplicitImpl)
1587 Report.Error (541, p.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1588 p.GetSignatureForError ());
1590 current_container.AddProperty (p);
1592 if (RootContext.Documentation != null)
1593 Lexer.doc_state = XmlCommentState.Allowed;
1595 | interface_event_declaration
1598 Event e = (Event) $1;
1600 if (e.IsExplicitImpl)
1601 Report.Error (541, e.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1602 e.GetSignatureForError ());
1604 current_container.AddEvent (e);
1607 if (RootContext.Documentation != null)
1608 Lexer.doc_state = XmlCommentState.Allowed;
1610 | interface_indexer_declaration
1615 Indexer i = (Indexer) $1;
1617 if (i.IsExplicitImpl)
1618 Report.Error (541, i.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1619 i.GetSignatureForError ());
1621 current_container.AddIndexer (i);
1623 if (RootContext.Documentation != null)
1624 Lexer.doc_state = XmlCommentState.Allowed;
1626 | delegate_declaration
1629 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1630 ((MemberCore)$1).GetSignatureForError ());
1636 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1637 ((MemberCore)$1).GetSignatureForError ());
1640 | struct_declaration
1643 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1644 ((MemberCore)$1).GetSignatureForError ());
1650 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1651 ((MemberCore)$1).GetSignatureForError ());
1654 | interface_declaration
1657 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1658 ((MemberCore)$1).GetSignatureForError ());
1661 | constant_declaration
1663 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1671 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, GetLocation ($1));
1676 interface_method_declaration_body
1679 lexer.ConstraintsParsing = false;
1681 opt_statement_list CLOSE_BRACE
1683 Report.Error (531, lexer.Location,
1684 "'{0}': interface members cannot have a definition", $4);
1690 interface_method_declaration
1691 : opt_attributes opt_new type namespace_or_type_name
1692 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1694 lexer.ConstraintsParsing = true;
1696 opt_type_parameter_constraints_clauses
1697 interface_method_declaration_body
1699 lexer.ConstraintsParsing = false;
1701 MemberName name = (MemberName) $4;
1703 if ($9 != null && name.TypeArguments == null)
1704 Report.Error (80, lexer.Location,
1705 "Contraints are not allowed on non-generic declarations");
1707 GenericMethod generic = null;
1708 if (name.TypeArguments != null) {
1709 generic = new GenericMethod (current_namespace, current_class, name);
1711 generic.SetParameterInfo ((ArrayList) $9);
1714 $$ = new Method (current_class, generic, (Expression) $3, (int) $2, true, name,
1715 (Parameters) $6, (Attributes) $1);
1716 if (RootContext.Documentation != null)
1717 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1719 | opt_attributes opt_new VOID namespace_or_type_name
1720 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1722 lexer.ConstraintsParsing = true;
1724 opt_type_parameter_constraints_clauses
1725 interface_method_declaration_body
1727 lexer.ConstraintsParsing = false;
1729 MemberName name = (MemberName) $4;
1731 if ($9 != null && name.TypeArguments == null)
1732 Report.Error (80, lexer.Location,
1733 "Contraints are not allowed on non-generic declarations");
1735 GenericMethod generic = null;
1736 if (name.TypeArguments != null) {
1737 generic = new GenericMethod (current_namespace, current_class, name);
1739 generic.SetParameterInfo ((ArrayList) $9);
1742 $$ = new Method (current_class, generic, TypeManager.system_void_expr, (int) $2,
1743 true, name, (Parameters) $6, (Attributes) $1);
1744 if (RootContext.Documentation != null)
1745 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1749 interface_property_declaration
1754 { lexer.PropertyParsing = true; }
1756 { lexer.PropertyParsing = false; }
1759 LocatedToken lt = (LocatedToken) $4;
1760 MemberName name = new MemberName (lt.Value, lt.Location);
1762 if ($3 == TypeManager.system_void_expr) {
1763 Report.Error (547, lt.Location, "`{0}': property or indexer cannot have void type", lt.Value);
1768 Property p = new Property (current_class, (Expression) $3, (int) $2, true,
1769 name, (Attributes) $1,
1772 Report.Error (548, lexer.Location, "`{0}': property or indexer must have at least one accessor", p.GetSignatureForError ());
1776 InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
1778 $$ = new Property (current_class, (Expression) $3, (int) $2, true,
1779 name, (Attributes) $1,
1780 pinfo.Get, pinfo.Set);
1781 if (RootContext.Documentation != null)
1782 ((Property) $$).DocComment = Lexer.consume_doc_comment ();
1787 CheckIdentifierToken (yyToken, GetLocation ($4));
1793 : opt_attributes opt_modifiers GET SEMICOLON
1795 $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, (int) $2, 0, (Location) $3, (Location) $3);
1797 | opt_attributes opt_modifiers GET OPEN_BRACE
1799 Report.Error (531, (Location) $3, "`{0}': interface members cannot have a definition", ".get");
1800 $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, (int) $2, 0, (Location) $3, (Location) $3);
1802 | opt_attributes opt_modifiers SET SEMICOLON
1804 $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1, 0, (int) $2, (Location) $3, (Location) $3);
1806 | opt_attributes opt_modifiers GET SEMICOLON opt_attributes opt_modifiers SET SEMICOLON
1808 $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $5, (int) $2, (int) $6, (Location) $3, (Location) $7);
1810 | opt_attributes opt_modifiers SET SEMICOLON opt_attributes opt_modifiers GET SEMICOLON
1812 $$ = new InterfaceAccessorInfo (true, true, (Attributes) $5, (Attributes) $1, (int) $6, (int) $2, (Location) $3, (Location) $3);
1820 interface_event_declaration
1821 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1823 LocatedToken lt = (LocatedToken) $5;
1824 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1825 new MemberName (lt.Value, lt.Location), null,
1827 if (RootContext.Documentation != null)
1828 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1830 | opt_attributes opt_new EVENT type error {
1831 CheckIdentifierToken (yyToken, GetLocation ($5));
1834 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1835 LocatedToken lt = (LocatedToken) $5;
1836 Report.Error (68, lt.Location, "`{0}.{1}': event in interface cannot have initializer", current_container.Name, lt.Value);
1839 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1841 lexer.EventParsing = true;
1843 event_accessor_declarations
1845 lexer.EventParsing = false;
1848 Report.Error (69, (Location) $3, "Event in interface cannot have add or remove accessors");
1853 interface_indexer_declaration
1854 : opt_attributes opt_new type THIS
1855 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1857 { lexer.PropertyParsing = true; }
1859 { lexer.PropertyParsing = false; }
1863 Indexer i = new Indexer (current_class, (Expression) $3,
1864 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1865 (int) $2, true, (Parameters) $6, (Attributes) $1,
1868 Report.Error (548, lexer.Location, "`{0}': property or indexer must have at least one accessor", i.GetSignatureForError ());
1872 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1874 $$ = new Indexer (current_class, (Expression) $3,
1875 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1876 (int) $2, true, (Parameters) $6, (Attributes) $1,
1877 info.Get, info.Set);
1878 if (RootContext.Documentation != null)
1879 ((Indexer) $$).DocComment = ConsumeStoredComment ();
1883 operator_declaration
1884 : opt_attributes opt_modifiers operator_declarator
1886 iterator_container = SimpleIteratorContainer.GetSimple ();
1893 OperatorDeclaration decl = (OperatorDeclaration) $3;
1895 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1897 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null, decl.location);
1898 if (decl.arg2type != null)
1899 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null, decl.location);
1901 Operator op = new Operator (
1902 current_class, decl.optype, decl.ret_type, (int) $2,
1903 new Parameters (param_list, null),
1904 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1906 if (RootContext.Documentation != null) {
1907 op.DocComment = tmpComment;
1908 Lexer.doc_state = XmlCommentState.Allowed;
1911 if (SimpleIteratorContainer.Simple.Yields)
1914 // Note again, checking is done in semantic analysis
1915 current_container.AddOperator (op);
1917 current_local_parameters = null;
1918 iterator_container = null;
1924 | SEMICOLON { $$ = null; }
1927 : type OPERATOR overloadable_operator
1928 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1930 LocatedToken lt = (LocatedToken) $6;
1931 Operator.OpType op = (Operator.OpType) $3;
1932 CheckUnaryOperator (op, lt.Location);
1934 if (op == Operator.OpType.Addition)
1935 op = Operator.OpType.UnaryPlus;
1937 if (op == Operator.OpType.Subtraction)
1938 op = Operator.OpType.UnaryNegation;
1940 Parameter [] pars = new Parameter [1];
1941 Expression type = (Expression) $5;
1943 pars [0] = new Parameter (type, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1945 current_local_parameters = new Parameters (pars, null);
1947 if (RootContext.Documentation != null) {
1948 tmpComment = Lexer.consume_doc_comment ();
1949 Lexer.doc_state = XmlCommentState.NotAllowed;
1952 $$ = new OperatorDeclaration (op, (Expression) $1, type, lt.Value,
1953 null, null, (Location) $2);
1955 | type OPERATOR overloadable_operator
1957 type IDENTIFIER COMMA
1961 LocatedToken ltParam1 = (LocatedToken) $6;
1962 LocatedToken ltParam2 = (LocatedToken) $9;
1963 CheckBinaryOperator ((Operator.OpType) $3, (Location) $2);
1965 Parameter [] pars = new Parameter [2];
1967 Expression typeL = (Expression) $5;
1968 Expression typeR = (Expression) $8;
1970 pars [0] = new Parameter (typeL, ltParam1.Value, Parameter.Modifier.NONE, null, ltParam1.Location);
1971 pars [1] = new Parameter (typeR, ltParam2.Value, Parameter.Modifier.NONE, null, ltParam2.Location);
1973 current_local_parameters = new Parameters (pars, null);
1975 if (RootContext.Documentation != null) {
1976 tmpComment = Lexer.consume_doc_comment ();
1977 Lexer.doc_state = XmlCommentState.NotAllowed;
1980 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1981 typeL, ltParam1.Value,
1982 typeR, ltParam2.Value, (Location) $2);
1984 | conversion_operator_declarator
1985 | type OPERATOR overloadable_operator
1987 type IDENTIFIER COMMA
1988 type IDENTIFIER COMMA
1992 Report.Error (1534, (Location) $2, "Overloaded binary operator `{0}' takes two parameters",
1993 Operator.GetName ((Operator.OpType) $3));
1996 | type OPERATOR overloadable_operator
1997 OPEN_PARENS CLOSE_PARENS
1999 Report.Error (1535, (Location) $2, "Overloaded unary operator `{0}' takes one parameter",
2000 Operator.GetName ((Operator.OpType) $3));
2005 overloadable_operator
2007 : BANG { $$ = Operator.OpType.LogicalNot; }
2008 | TILDE { $$ = Operator.OpType.OnesComplement; }
2009 | OP_INC { $$ = Operator.OpType.Increment; }
2010 | OP_DEC { $$ = Operator.OpType.Decrement; }
2011 | TRUE { $$ = Operator.OpType.True; }
2012 | FALSE { $$ = Operator.OpType.False; }
2013 // Unary and binary:
2014 | PLUS { $$ = Operator.OpType.Addition; }
2015 | MINUS { $$ = Operator.OpType.Subtraction; }
2017 | STAR { $$ = Operator.OpType.Multiply; }
2018 | DIV { $$ = Operator.OpType.Division; }
2019 | PERCENT { $$ = Operator.OpType.Modulus; }
2020 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
2021 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
2022 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
2023 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
2024 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
2025 | OP_EQ { $$ = Operator.OpType.Equality; }
2026 | OP_NE { $$ = Operator.OpType.Inequality; }
2027 | OP_GT { $$ = Operator.OpType.GreaterThan; }
2028 | OP_LT { $$ = Operator.OpType.LessThan; }
2029 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
2030 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
2033 conversion_operator_declarator
2034 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
2036 LocatedToken lt = (LocatedToken) $6;
2037 Parameter [] pars = new Parameter [1];
2039 pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2041 current_local_parameters = new Parameters (pars, null);
2043 if (RootContext.Documentation != null) {
2044 tmpComment = Lexer.consume_doc_comment ();
2045 Lexer.doc_state = XmlCommentState.NotAllowed;
2048 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, lt.Value,
2049 null, null, (Location) $2);
2051 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
2053 LocatedToken lt = (LocatedToken) $6;
2054 Parameter [] pars = new Parameter [1];
2056 pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
2058 current_local_parameters = new Parameters (pars, null);
2060 if (RootContext.Documentation != null) {
2061 tmpComment = Lexer.consume_doc_comment ();
2062 Lexer.doc_state = XmlCommentState.NotAllowed;
2065 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, lt.Value,
2066 null, null, (Location) $2);
2070 syntax_error ((Location) $1, "'operator' expected");
2074 syntax_error ((Location) $1, "'operator' expected");
2078 constructor_declaration
2081 constructor_declarator
2084 Constructor c = (Constructor) $3;
2085 c.Block = (ToplevelBlock) $4;
2086 c.OptAttributes = (Attributes) $1;
2087 c.ModFlags = (int) $2;
2089 if (RootContext.Documentation != null)
2090 c.DocComment = ConsumeStoredComment ();
2092 if (c.Name == current_container.Basename){
2093 if ((c.ModFlags & Modifiers.STATIC) != 0){
2094 if ((c.ModFlags & Modifiers.Accessibility) != 0){
2095 Report.Error (515, c.Location,
2096 "`{0}': access modifiers are not allowed on static constructors",
2097 c.GetSignatureForError ());
2100 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2102 if (c.Initializer != null){
2103 Report.Error (514, c.Location,
2104 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2105 c.GetSignatureForError ());
2108 if (!c.Parameters.Empty){
2109 Report.Error (132, c.Location,
2110 "`{0}': The static constructor must be parameterless", c.GetSignatureForError ());
2113 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2116 // We let another layer check the validity of the constructor.
2117 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
2120 current_container.AddConstructor (c);
2122 current_local_parameters = null;
2123 if (RootContext.Documentation != null)
2124 Lexer.doc_state = XmlCommentState.Allowed;
2128 constructor_declarator
2131 if (RootContext.Documentation != null) {
2132 tmpComment = Lexer.consume_doc_comment ();
2133 Lexer.doc_state = XmlCommentState.Allowed;
2136 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2138 current_local_parameters = (Parameters) $4;
2140 opt_constructor_initializer
2142 LocatedToken lt = (LocatedToken) $1;
2143 $$ = new Constructor (current_class, lt.Value, 0, (Parameters) $4,
2144 (ConstructorInitializer) $7, lt.Location);
2150 | SEMICOLON { $$ = null; }
2153 opt_constructor_initializer
2154 : /* empty */ { $$ = null; }
2155 | constructor_initializer
2158 constructor_initializer
2159 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
2161 $$ = new ConstructorBaseInitializer ((ArrayList) $4, (Location) $2);
2163 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
2165 $$ = new ConstructorThisInitializer ((ArrayList) $4, (Location) $2);
2168 Report.Error (1018, (Location) $1, "Keyword this or base expected");
2174 : /* EMPTY */ { $$ = 0; }
2175 | UNSAFE { $$ = Modifiers.UNSAFE; }
2176 | EXTERN { $$ = Modifiers.EXTERN; }
2179 destructor_declaration
2180 : opt_attributes opt_finalizer TILDE
2182 if (RootContext.Documentation != null) {
2183 tmpComment = Lexer.consume_doc_comment ();
2184 Lexer.doc_state = XmlCommentState.NotAllowed;
2187 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2189 LocatedToken lt = (LocatedToken) $5;
2190 if (lt.Value != current_container.MemberName.Name){
2191 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2192 } else if (current_container.Kind != Kind.Class){
2193 Report.Error (575, lt.Location, "Only class types can contain destructor");
2195 Location l = lt.Location;
2198 if (!RootContext.StdLib && current_container.Name == "System.Object")
2199 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2201 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2203 Method d = new Destructor (
2204 current_class, TypeManager.system_void_expr, m, "Finalize",
2205 new Parameters (null, null), (Attributes) $1, l);
2206 if (RootContext.Documentation != null)
2207 d.DocComment = ConsumeStoredComment ();
2209 d.Block = (ToplevelBlock) $8;
2210 current_container.AddMethod (d);
2218 EVENT type variable_declarators SEMICOLON
2220 foreach (VariableDeclaration var in (ArrayList) $5) {
2222 MemberName name = new MemberName (var.identifier,
2225 Event e = new EventField (
2226 current_class, (Expression) $4, (int) $2, false, name,
2227 var.expression_or_array_initializer, (Attributes) $1);
2229 current_container.AddEvent (e);
2231 if (RootContext.Documentation != null) {
2232 e.DocComment = Lexer.consume_doc_comment ();
2233 Lexer.doc_state = XmlCommentState.Allowed;
2239 EVENT type namespace_or_type_name
2242 implicit_value_parameter_type = (Expression) $4;
2243 lexer.EventParsing = true;
2245 event_accessor_declarations
2247 lexer.EventParsing = false;
2251 MemberName name = (MemberName) $5;
2254 Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2255 current_container.Name, name);
2258 Pair pair = (Pair) $8;
2260 if (name.TypeArguments != null)
2261 syntax_error (lexer.Location, "an event can't have type arguments");
2263 if (pair.First == null || pair.Second == null)
2264 // CS0073 is already reported, so no CS0065 here.
2267 Event e = new EventProperty (
2268 current_class, (Expression) $4, (int) $2, false, name, null,
2269 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second);
2270 if (RootContext.Documentation != null) {
2271 e.DocComment = Lexer.consume_doc_comment ();
2272 Lexer.doc_state = XmlCommentState.Allowed;
2275 current_container.AddEvent (e);
2276 implicit_value_parameter_type = null;
2280 | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
2281 MemberName mn = (MemberName) $5;
2283 if (mn.Left != null)
2284 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2286 Report.Error (71, mn.Location, "Event declaration should use property syntax");
2288 if (RootContext.Documentation != null)
2289 Lexer.doc_state = XmlCommentState.Allowed;
2293 event_accessor_declarations
2294 : add_accessor_declaration remove_accessor_declaration
2296 $$ = new Pair ($1, $2);
2298 | remove_accessor_declaration add_accessor_declaration
2300 $$ = new Pair ($2, $1);
2302 | add_accessor_declaration { $$ = null; }
2303 | remove_accessor_declaration { $$ = null; }
2306 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2312 add_accessor_declaration
2313 : opt_attributes ADD
2315 Parameter [] args = new Parameter [1];
2316 Parameter implicit_value_parameter = new Parameter (
2317 implicit_value_parameter_type, "value",
2318 Parameter.Modifier.NONE, null, (Location) $2);
2320 args [0] = implicit_value_parameter;
2322 current_local_parameters = new Parameters (args, null);
2323 lexer.EventParsing = false;
2327 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2328 lexer.EventParsing = true;
2330 | opt_attributes ADD error {
2331 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2334 | opt_attributes modifiers ADD {
2335 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2340 remove_accessor_declaration
2341 : opt_attributes REMOVE
2343 Parameter [] args = new Parameter [1];
2344 Parameter implicit_value_parameter = new Parameter (
2345 implicit_value_parameter_type, "value",
2346 Parameter.Modifier.NONE, null, (Location) $2);
2348 args [0] = implicit_value_parameter;
2350 current_local_parameters = new Parameters (args, null);
2351 lexer.EventParsing = false;
2355 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2356 lexer.EventParsing = true;
2358 | opt_attributes REMOVE error {
2359 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2362 | opt_attributes modifiers REMOVE {
2363 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2369 : opt_attributes opt_modifiers indexer_declarator
2372 IndexerDeclaration decl = (IndexerDeclaration) $3;
2374 implicit_value_parameter_type = decl.type;
2376 lexer.PropertyParsing = true;
2377 parsing_indexer = true;
2379 indexer_parameters = decl.param_list;
2380 iterator_container = SimpleIteratorContainer.GetSimple ();
2382 accessor_declarations
2384 lexer.PropertyParsing = false;
2385 has_get = has_set = false;
2386 parsing_indexer = false;
2393 // The signature is computed from the signature of the indexer. Look
2394 // at section 3.6 on the spec
2396 IndexerDeclaration decl = (IndexerDeclaration) $3;
2397 Pair pair = (Pair) $6;
2398 Accessor get_block = (Accessor) pair.First;
2399 Accessor set_block = (Accessor) pair.Second;
2402 if (decl.interface_type != null)
2403 name = new MemberName (
2404 decl.interface_type, TypeContainer.DefaultIndexerName, decl.location);
2406 name = new MemberName (TypeContainer.DefaultIndexerName, decl.location);
2408 indexer = new Indexer (current_class, decl.type, name,
2409 (int) $2, false, decl.param_list, (Attributes) $1,
2410 get_block, set_block);
2412 if (RootContext.Documentation != null)
2413 indexer.DocComment = ConsumeStoredComment ();
2415 current_container.AddIndexer (indexer);
2417 current_local_parameters = null;
2418 implicit_value_parameter_type = null;
2419 indexer_parameters = null;
2424 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2426 Parameters pars = (Parameters) $4;
2427 if (pars.HasArglist) {
2428 // "__arglist is not valid in this context"
2429 Report.Error (1669, (Location) $2, "__arglist is not valid in this context");
2430 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2431 Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
2433 if (RootContext.Documentation != null) {
2434 tmpComment = Lexer.consume_doc_comment ();
2435 Lexer.doc_state = XmlCommentState.Allowed;
2438 $$ = new IndexerDeclaration ((Expression) $1, null, pars, (Location) $2);
2440 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2442 Parameters pars = (Parameters) $6;
2444 if (pars.HasArglist) {
2445 // "__arglist is not valid in this context"
2446 Report.Error (1669, (Location) $4, "__arglist is not valid in this context");
2447 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2448 Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
2451 MemberName name = (MemberName) $2;
2452 $$ = new IndexerDeclaration ((Expression) $1, name, pars, (Location) $4);
2454 if (RootContext.Documentation != null) {
2455 tmpComment = Lexer.consume_doc_comment ();
2456 Lexer.doc_state = XmlCommentState.Allowed;
2467 if (RootContext.Documentation != null)
2468 enumTypeComment = Lexer.consume_doc_comment ();
2473 LocatedToken lt = (LocatedToken) $5;
2474 Location enum_location = lt.Location;
2477 Report.Error (267, lt.Location, "The partial modifier can only appear immediately before `class', `struct' or `interface'");
2478 break; // assumes that the parser put us in a switch
2481 MemberName name = MakeName (new MemberName (lt.Value, enum_location));
2482 Enum e = new Enum (current_namespace, current_class, (Expression) $6, (int) $2,
2483 name, (Attributes) $1);
2485 if (RootContext.Documentation != null)
2486 e.DocComment = enumTypeComment;
2489 EnumMember em = null;
2490 foreach (VariableDeclaration ev in (ArrayList) $8) {
2491 em = new EnumMember (e, em, (Expression) ev.expression_or_array_initializer,
2492 new MemberName (ev.identifier, ev.Location), ev.OptAttributes);
2494 // if (RootContext.Documentation != null)
2495 em.DocComment = ev.DocComment;
2497 e.AddEnumMember (em);
2500 current_container.AddEnum (e);
2501 RootContext.Tree.RecordDecl (current_namespace.NS, name, e);
2508 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2509 | COLON type { $$ = $2; }
2515 if (RootContext.Documentation != null)
2516 Lexer.doc_state = XmlCommentState.Allowed;
2518 opt_enum_member_declarations
2520 // here will be evaluated after CLOSE_BLACE is consumed.
2521 if (RootContext.Documentation != null)
2522 Lexer.doc_state = XmlCommentState.Allowed;
2530 opt_enum_member_declarations
2531 : /* empty */ { $$ = new ArrayList (4); }
2532 | enum_member_declarations opt_comma { $$ = $1; }
2535 enum_member_declarations
2536 : enum_member_declaration
2538 ArrayList l = new ArrayList (4);
2543 | enum_member_declarations COMMA enum_member_declaration
2545 ArrayList l = (ArrayList) $1;
2553 enum_member_declaration
2554 : opt_attributes IDENTIFIER
2556 VariableDeclaration vd = new VariableDeclaration (
2557 (LocatedToken) $2, null, (Attributes) $1);
2559 if (RootContext.Documentation != null) {
2560 vd.DocComment = Lexer.consume_doc_comment ();
2561 Lexer.doc_state = XmlCommentState.Allowed;
2566 | opt_attributes IDENTIFIER
2568 if (RootContext.Documentation != null) {
2569 tmpComment = Lexer.consume_doc_comment ();
2570 Lexer.doc_state = XmlCommentState.NotAllowed;
2575 VariableDeclaration vd = new VariableDeclaration (
2576 (LocatedToken) $2, $5, (Attributes) $1);
2578 if (RootContext.Documentation != null)
2579 vd.DocComment = ConsumeStoredComment ();
2585 delegate_declaration
2590 lexer.ConstraintsParsing = true;
2593 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2595 MemberName name = MakeName ((MemberName) $6);
2596 Delegate del = new Delegate (current_namespace, current_class, (Expression) $5,
2597 (int) $2, name, (Parameters) $8, (Attributes) $1);
2599 if (RootContext.Documentation != null) {
2600 del.DocComment = Lexer.consume_doc_comment ();
2601 Lexer.doc_state = XmlCommentState.Allowed;
2604 current_container.AddDelegate (del);
2605 RootContext.Tree.RecordDecl (current_namespace.NS, name, del);
2607 current_delegate = del;
2609 opt_type_parameter_constraints_clauses
2611 lexer.ConstraintsParsing = false;
2615 current_delegate.SetParameterInfo ((ArrayList) $11);
2616 $$ = current_delegate;
2618 current_delegate = null;
2625 lexer.CheckNullable (false);
2630 lexer.CheckNullable (true);
2635 namespace_or_type_name
2637 | IDENTIFIER DOUBLE_COLON IDENTIFIER {
2638 LocatedToken lt1 = (LocatedToken) $1;
2639 LocatedToken lt2 = (LocatedToken) $3;
2640 $$ = new MemberName (lt1.Value, lt2.Value, lt2.Location);
2642 | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list {
2643 LocatedToken lt = (LocatedToken) $3;
2644 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $4, lt.Location);
2649 : IDENTIFIER opt_type_argument_list {
2650 LocatedToken lt = (LocatedToken) $1;
2651 $$ = new MemberName (lt.Value, (TypeArguments) $2, lt.Location);
2655 opt_type_argument_list
2656 : /* empty */ { $$ = null; }
2657 | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2663 $$ = new TypeArguments ((int) $1, lexer.Location);
2669 TypeArguments type_args = new TypeArguments (lexer.Location);
2670 type_args.Add ((Expression) $1);
2673 | type_arguments COMMA type {
2674 TypeArguments type_args = (TypeArguments) $1;
2675 type_args.Add ((Expression) $3);
2681 * Before you think of adding a return_type, notice that we have been
2682 * using two rules in the places where it matters (one rule using type
2683 * and another identical one that uses VOID as the return type). This
2684 * gets rid of a shift/reduce couple
2687 : namespace_or_type_name opt_nullable
2689 MemberName name = (MemberName) $1;
2690 $$ = name.GetTypeExpression ();
2693 $$ = new ComposedCast ((Expression) $$, "?", lexer.Location);
2695 | builtin_types opt_nullable
2698 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2708 // Note that here only unmanaged types are allowed but we
2709 // can't perform checks during this phase - we do it during
2710 // semantic analysis.
2712 $$ = new ComposedCast ((Expression) $1, "*");
2716 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2721 : builtin_types opt_nullable
2724 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2726 | non_expression_type rank_specifier
2728 $$ = new ComposedCast ((Expression) $1, (string) $2);
2730 | non_expression_type STAR
2732 $$ = new ComposedCast ((Expression) $1, "*");
2734 | expression rank_specifiers
2736 $$ = new ComposedCast ((Expression) $1, (string) $2);
2740 $$ = new ComposedCast ((Expression) $1, "*");
2744 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2745 // through this different path
2747 | multiplicative_expression STAR
2749 $$ = new ComposedCast ((Expression) $1, "*");
2756 ArrayList types = new ArrayList (4);
2761 | type_list COMMA type
2763 ArrayList types = (ArrayList) $1;
2771 * replaces all the productions for isolating the various
2772 * simple types, but we need this to reuse it easily in local_variable_type
2775 : OBJECT { $$ = TypeManager.system_object_expr; }
2776 | STRING { $$ = TypeManager.system_string_expr; }
2777 | BOOL { $$ = TypeManager.system_boolean_expr; }
2778 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2779 | FLOAT { $$ = TypeManager.system_single_expr; }
2780 | DOUBLE { $$ = TypeManager.system_double_expr; }
2785 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2786 | BYTE { $$ = TypeManager.system_byte_expr; }
2787 | SHORT { $$ = TypeManager.system_int16_expr; }
2788 | USHORT { $$ = TypeManager.system_uint16_expr; }
2789 | INT { $$ = TypeManager.system_int32_expr; }
2790 | UINT { $$ = TypeManager.system_uint32_expr; }
2791 | LONG { $$ = TypeManager.system_int64_expr; }
2792 | ULONG { $$ = TypeManager.system_uint64_expr; }
2793 | CHAR { $$ = TypeManager.system_char_expr; }
2794 | VOID { $$ = TypeManager.system_void_expr; }
2798 : type rank_specifiers opt_nullable
2800 string rank_specifiers = (string) $2;
2802 rank_specifiers += "?";
2804 $$ = new ComposedCast ((Expression) $1, rank_specifiers);
2809 // Expressions, section 7.5
2818 MemberName mn = (MemberName) $1;
2819 $$ = mn.GetTypeExpression ();
2821 | IDENTIFIER DOUBLE_COLON IDENTIFIER
2823 LocatedToken lt1 = (LocatedToken) $1;
2824 LocatedToken lt2 = (LocatedToken) $3;
2825 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, lt2.Location);
2827 | parenthesized_expression
2828 | default_value_expression
2830 | invocation_expression
2834 | post_increment_expression
2835 | post_decrement_expression
2839 | checked_expression
2840 | unchecked_expression
2841 | pointer_member_access
2842 | anonymous_method_expression
2849 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2850 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2851 | NULL { $$ = NullLiteral.Null; }
2855 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2856 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2857 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2862 object v = lexer.Value;
2868 $$ = IntLiteral.Zero;
2870 $$ = IntLiteral.One;
2872 $$ = new IntLiteral (i);
2873 } else if (v is uint)
2874 $$ = new UIntLiteral ((UInt32) v);
2876 $$ = new LongLiteral ((Int64) v);
2877 else if (v is ulong)
2878 $$ = new ULongLiteral ((UInt64) v);
2880 Console.WriteLine ("OOPS. Unexpected result from scanner");
2885 : TRUE { $$ = new BoolLiteral (true); }
2886 | FALSE { $$ = new BoolLiteral (false); }
2889 parenthesized_expression_0
2890 : OPEN_PARENS expression CLOSE_PARENS
2893 lexer.Deambiguate_CloseParens ();
2894 // After this, the next token returned is one of
2895 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2896 // or CLOSE_PARENS_MINUS.
2900 parenthesized_expression
2901 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2905 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2907 // If a parenthesized expression is followed by a minus, we need to wrap
2908 // the expression inside a ParenthesizedExpression for the CS0075 check
2909 // in Binary.DoResolve().
2910 $$ = new ParenthesizedExpression ((Expression) $1);
2915 : primary_expression DOT IDENTIFIER opt_type_argument_list
2917 LocatedToken lt = (LocatedToken) $3;
2918 $$ = new MemberAccess ((Expression) $1, lt.Value,
2919 (TypeArguments) $4, lt.Location);
2921 | predefined_type DOT IDENTIFIER opt_type_argument_list
2923 LocatedToken lt = (LocatedToken) $3;
2924 $$ = new MemberAccess ((Expression) $1, lt.Value,
2925 (TypeArguments) $4, lt.Location);
2933 invocation_expression
2934 : primary_expression {
2935 $$ = lexer.Location;
2936 } OPEN_PARENS opt_argument_list CLOSE_PARENS
2939 Location l = (Location) $3;
2940 Report.Error (1, l, "Parse error");
2942 $$ = new Invocation ((Expression) $1, (ArrayList) $4);
2944 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2946 $$ = new Invocation ((Expression) $1, new ArrayList ());
2948 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2950 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
2952 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2954 ArrayList args = new ArrayList (1);
2956 $$ = new Invocation ((Expression) $1, args);
2958 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2960 ArrayList args = ((ArrayList) $4);
2962 $$ = new Invocation ((Expression) $1, args);
2967 : /* empty */ { $$ = null; }
2974 ArrayList list = new ArrayList (4);
2978 | argument_list COMMA argument
2980 ArrayList list = (ArrayList) $1;
2984 | argument_list error {
2985 CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
2993 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2995 | non_simple_argument
3002 : REF variable_reference
3004 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
3006 | OUT variable_reference
3008 $$ = new Argument ((Expression) $2, Argument.AType.Out);
3010 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
3012 ArrayList list = (ArrayList) $3;
3013 Argument[] args = new Argument [list.Count];
3014 list.CopyTo (args, 0);
3016 Expression expr = new Arglist (args, (Location) $1);
3017 $$ = new Argument (expr, Argument.AType.Expression);
3021 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
3026 : expression { note ("section 5.4"); $$ = $1; }
3030 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
3032 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
3034 | primary_expression rank_specifiers
3036 // So the super-trick is that primary_expression
3037 // can only be either a SimpleName or a MemberAccess.
3038 // The MemberAccess case arises when you have a fully qualified type-name like :
3040 // SimpleName is when you have
3043 Expression expr = (Expression) $1;
3044 if (expr is ComposedCast){
3045 $$ = new ComposedCast (expr, (string) $2);
3046 } else if (!(expr is SimpleName || expr is MemberAccess || expr is ConstructedType || expr is QualifiedAliasMember)){
3047 Error_ExpectingTypeName (expr);
3048 $$ = TypeManager.system_object_expr;
3051 // So we extract the string corresponding to the SimpleName
3054 $$ = new ComposedCast (expr, (string) $2);
3062 ArrayList list = new ArrayList (4);
3066 | expression_list COMMA expression
3068 ArrayList list = (ArrayList) $1;
3077 $$ = new This (current_block, (Location) $1);
3082 : BASE DOT IDENTIFIER
3084 LocatedToken lt = (LocatedToken) $3;
3085 $$ = new BaseAccess (lt.Value, lt.Location);
3087 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
3089 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
3092 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
3097 post_increment_expression
3098 : primary_expression OP_INC
3100 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
3101 (Expression) $1, (Location) $2);
3105 post_decrement_expression
3106 : primary_expression OP_DEC
3108 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
3109 (Expression) $1, (Location) $2);
3114 : object_or_delegate_creation_expression
3115 | array_creation_expression
3118 object_or_delegate_creation_expression
3119 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
3121 $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
3125 array_creation_expression
3126 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
3128 opt_array_initializer
3130 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
3132 | NEW type rank_specifiers array_initializer
3134 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, (Location) $1);
3138 Report.Error (1031, (Location) $1, "Type expected");
3143 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
3159 opt_rank_specifier_or_nullable
3168 | opt_nullable rank_specifiers
3175 | opt_nullable rank_specifiers INTERR
3178 $$ = "?" + $2 + "?";
3185 : rank_specifier opt_rank_specifier
3187 $$ = (string) $2 + (string) $1;
3192 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3194 $$ = "[" + (string) $2 + "]";
3214 | dim_separators COMMA
3216 $$ = (string) $1 + ",";
3220 opt_array_initializer
3232 : OPEN_BRACE CLOSE_BRACE
3234 ArrayList list = new ArrayList (4);
3237 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3239 $$ = (ArrayList) $2;
3243 variable_initializer_list
3244 : variable_initializer
3246 ArrayList list = new ArrayList (4);
3250 | variable_initializer_list COMMA variable_initializer
3252 ArrayList list = (ArrayList) $1;
3258 void_pointer_expression
3259 : void_pointer_expression STAR
3261 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3265 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3270 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
3272 $$ = new TypeOfVoid ((Location) $1);
3274 | TYPEOF OPEN_PARENS void_pointer_expression CLOSE_PARENS
3276 $$ = new TypeOf ((Expression) $3, (Location) $1);
3278 | TYPEOF OPEN_PARENS
3280 lexer.TypeOfParsing = true;
3284 lexer.TypeOfParsing = false;
3285 $$ = new TypeOf ((Expression) $4, lexer.Location);
3290 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3291 $$ = new SizeOf ((Expression) $3, (Location) $1);
3296 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3298 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3302 unchecked_expression
3303 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3305 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3309 pointer_member_access
3310 : primary_expression OP_PTR IDENTIFIER
3313 LocatedToken lt = (LocatedToken) $3;
3315 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
3316 $$ = new MemberAccess (deref, lt.Value, lt.Location);
3320 anonymous_method_expression
3321 : DELEGATE opt_anonymous_method_signature
3323 oob_stack.Push (current_local_parameters);
3324 current_local_parameters = (Parameters)$2;
3326 // Force the next block to be created as a ToplevelBlock
3327 oob_stack.Push (current_block);
3328 oob_stack.Push (top_current_block);
3329 current_block = null;
3333 Location loc = (Location) $1;
3334 top_current_block = (Block) oob_stack.Pop ();
3335 current_block = (Block) oob_stack.Pop ();
3336 if (RootContext.Version == LanguageVersion.ISO_1){
3337 Report.FeatureIsNotStandardized (loc, "anonymous methods");
3340 ToplevelBlock anon_block = (ToplevelBlock) $4;
3342 anon_block.Parent = current_block;
3343 $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block,
3346 current_local_parameters = (Parameters) oob_stack.Pop ();
3350 opt_anonymous_method_signature
3351 : /* empty */ { $$ = null; }
3352 | anonymous_method_signature
3355 anonymous_method_signature
3356 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3359 $$ = Parameters.EmptyReadOnlyParameters;
3361 ArrayList par_list = (ArrayList) $2;
3362 Parameter [] pars = new Parameter [par_list.Count];
3363 par_list.CopyTo (pars);
3364 $$ = new Parameters (pars, null);
3369 opt_anonymous_method_parameter_list
3370 : /* empty */ { $$ = null; }
3371 | anonymous_method_parameter_list { $$ = $1; }
3374 anonymous_method_parameter_list
3375 : anonymous_method_parameter
3377 ArrayList a = new ArrayList (4);
3381 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3383 ArrayList a = (ArrayList) $1;
3389 anonymous_method_parameter
3390 : opt_parameter_modifier type IDENTIFIER {
3391 LocatedToken lt = (LocatedToken) $3;
3392 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3394 | PARAMS type IDENTIFIER {
3395 Report.Error (1670, ((LocatedToken) $3).Location, "The `params' modifier is not allowed in anonymous method declaration");
3400 default_value_expression
3401 : DEFAULT_OPEN_PARENS type CLOSE_PARENS
3403 $$ = new DefaultValueExpression ((Expression) $2, lexer.Location);
3408 : primary_expression
3409 | BANG prefixed_unary_expression
3411 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3413 | TILDE prefixed_unary_expression
3415 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3421 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3423 $$ = new Cast ((Expression) $1, (Expression) $3);
3425 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3427 $$ = new Cast ((Expression) $1, (Expression) $3);
3433 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3435 $$ = new Cast ((Expression) $2, (Expression) $4);
3440 // The idea to split this out is from Rhys' grammar
3441 // to solve the problem with casts.
3443 prefixed_unary_expression
3445 | PLUS prefixed_unary_expression
3447 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3449 | MINUS prefixed_unary_expression
3451 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3453 | OP_INC prefixed_unary_expression
3455 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3456 (Expression) $2, (Location) $1);
3458 | OP_DEC prefixed_unary_expression
3460 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3461 (Expression) $2, (Location) $1);
3463 | STAR prefixed_unary_expression
3465 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, (Location) $1);
3467 | BITWISE_AND prefixed_unary_expression
3469 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3473 pre_increment_expression
3474 : OP_INC prefixed_unary_expression
3476 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3477 (Expression) $2, (Location) $1);
3481 pre_decrement_expression
3482 : OP_DEC prefixed_unary_expression
3484 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3485 (Expression) $2, (Location) $1);
3489 multiplicative_expression
3490 : prefixed_unary_expression
3491 | multiplicative_expression STAR prefixed_unary_expression
3493 $$ = new Binary (Binary.Operator.Multiply,
3494 (Expression) $1, (Expression) $3);
3496 | multiplicative_expression DIV prefixed_unary_expression
3498 $$ = new Binary (Binary.Operator.Division,
3499 (Expression) $1, (Expression) $3);
3501 | multiplicative_expression PERCENT prefixed_unary_expression
3503 $$ = new Binary (Binary.Operator.Modulus,
3504 (Expression) $1, (Expression) $3);
3509 : multiplicative_expression
3510 | additive_expression PLUS multiplicative_expression
3512 $$ = new Binary (Binary.Operator.Addition,
3513 (Expression) $1, (Expression) $3);
3515 | additive_expression MINUS multiplicative_expression
3517 $$ = new Binary (Binary.Operator.Subtraction,
3518 (Expression) $1, (Expression) $3);
3523 : additive_expression
3524 | shift_expression OP_SHIFT_LEFT additive_expression
3526 $$ = new Binary (Binary.Operator.LeftShift,
3527 (Expression) $1, (Expression) $3);
3529 | shift_expression OP_SHIFT_RIGHT additive_expression
3531 $$ = new Binary (Binary.Operator.RightShift,
3532 (Expression) $1, (Expression) $3);
3543 lexer.PutbackNullable ();
3548 nullable_type_or_conditional
3551 if (((bool) $2) && ($1 is ComposedCast))
3552 $$ = ((ComposedCast) $1).RemoveNullable ();
3558 relational_expression
3560 | relational_expression OP_LT shift_expression
3562 $$ = new Binary (Binary.Operator.LessThan,
3563 (Expression) $1, (Expression) $3);
3565 | relational_expression OP_GT shift_expression
3567 $$ = new Binary (Binary.Operator.GreaterThan,
3568 (Expression) $1, (Expression) $3);
3570 | relational_expression OP_LE shift_expression
3572 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3573 (Expression) $1, (Expression) $3);
3575 | relational_expression OP_GE shift_expression
3577 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3578 (Expression) $1, (Expression) $3);
3580 | relational_expression IS
3583 } nullable_type_or_conditional
3585 $$ = new Is ((Expression) $1, (Expression) $4, (Location) $2);
3587 | relational_expression AS
3590 } nullable_type_or_conditional
3592 $$ = new As ((Expression) $1, (Expression) $4, (Location) $2);
3597 : relational_expression
3598 | equality_expression OP_EQ relational_expression
3600 $$ = new Binary (Binary.Operator.Equality,
3601 (Expression) $1, (Expression) $3);
3603 | equality_expression OP_NE relational_expression
3605 $$ = new Binary (Binary.Operator.Inequality,
3606 (Expression) $1, (Expression) $3);
3611 : equality_expression
3612 | and_expression BITWISE_AND equality_expression
3614 $$ = new Binary (Binary.Operator.BitwiseAnd,
3615 (Expression) $1, (Expression) $3);
3619 exclusive_or_expression
3621 | exclusive_or_expression CARRET and_expression
3623 $$ = new Binary (Binary.Operator.ExclusiveOr,
3624 (Expression) $1, (Expression) $3);
3628 inclusive_or_expression
3629 : exclusive_or_expression
3630 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3632 $$ = new Binary (Binary.Operator.BitwiseOr,
3633 (Expression) $1, (Expression) $3);
3637 conditional_and_expression
3638 : inclusive_or_expression
3639 | conditional_and_expression OP_AND inclusive_or_expression
3641 $$ = new Binary (Binary.Operator.LogicalAnd,
3642 (Expression) $1, (Expression) $3);
3646 conditional_or_expression
3647 : conditional_and_expression
3648 | conditional_or_expression OP_OR conditional_and_expression
3650 $$ = new Binary (Binary.Operator.LogicalOr,
3651 (Expression) $1, (Expression) $3);
3655 conditional_expression
3656 : conditional_or_expression
3657 | conditional_or_expression INTERR expression COLON expression
3659 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
3661 | conditional_or_expression INTERR INTERR expression
3663 $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $4, lexer.Location);
3665 // We'll be resolved into a `parenthesized_expression_0' later on.
3666 | conditional_or_expression INTERR CLOSE_PARENS
3668 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
3669 lexer.PutbackCloseParens ();
3673 assignment_expression
3674 : prefixed_unary_expression ASSIGN expression
3676 $$ = new Assign ((Expression) $1, (Expression) $3);
3678 | prefixed_unary_expression OP_MULT_ASSIGN expression
3680 $$ = new CompoundAssign (
3681 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
3683 | prefixed_unary_expression OP_DIV_ASSIGN expression
3685 $$ = new CompoundAssign (
3686 Binary.Operator.Division, (Expression) $1, (Expression) $3);
3688 | prefixed_unary_expression OP_MOD_ASSIGN expression
3690 $$ = new CompoundAssign (
3691 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
3693 | prefixed_unary_expression OP_ADD_ASSIGN expression
3695 $$ = new CompoundAssign (
3696 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
3698 | prefixed_unary_expression OP_SUB_ASSIGN expression
3700 $$ = new CompoundAssign (
3701 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3703 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3705 $$ = new CompoundAssign (
3706 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
3708 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3710 $$ = new CompoundAssign (
3711 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
3713 | prefixed_unary_expression OP_AND_ASSIGN expression
3715 $$ = new CompoundAssign (
3716 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
3718 | prefixed_unary_expression OP_OR_ASSIGN expression
3720 $$ = new CompoundAssign (
3721 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
3723 | prefixed_unary_expression OP_XOR_ASSIGN expression
3725 $$ = new CompoundAssign (
3726 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
3731 : conditional_expression
3732 | assignment_expression
3752 lexer.ConstraintsParsing = true;
3756 MemberName name = MakeName ((MemberName) $6);
3757 int mod_flags = (int) $2;
3760 ClassPart part = PartialContainer.CreatePart (
3761 current_namespace, current_class, name, mod_flags,
3762 (Attributes) $1, Kind.Class, (Location) $3);
3764 current_container = part.PartialContainer;
3765 current_class = part;
3767 if ((mod_flags & Modifiers.STATIC) != 0) {
3768 current_class = new StaticClass (
3769 current_namespace, current_class, name,
3770 mod_flags, (Attributes) $1);
3772 current_class = new Class (
3773 current_namespace, current_class, name,
3774 mod_flags, (Attributes) $1);
3777 current_container.AddClassOrStruct (current_class);
3778 current_container = current_class;
3779 RootContext.Tree.RecordDecl (current_namespace.NS, name, current_class);
3783 opt_type_parameter_constraints_clauses
3785 lexer.ConstraintsParsing = false;
3788 if (current_class.Name == "System.Object") {
3789 Report.Error (537, current_class.Location,
3790 "The class System.Object cannot have a base " +
3791 "class or implement an interface.");
3793 current_class.Bases = (ArrayList) $8;
3796 current_class.SetParameterInfo ((ArrayList) $9);
3798 if (RootContext.Documentation != null) {
3799 current_class.DocComment = Lexer.consume_doc_comment ();
3800 Lexer.doc_state = XmlCommentState.Allowed;
3805 if (RootContext.Documentation != null)
3806 Lexer.doc_state = XmlCommentState.Allowed;
3810 $$ = pop_current_class ();
3818 { $$ = $1; } // location
3822 : /* empty */ { $$ = (int) 0; }
3828 | modifiers modifier
3833 if ((m1 & m2) != 0) {
3834 Location l = lexer.Location;
3835 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
3837 $$ = (int) (m1 | m2);
3842 : NEW { $$ = Modifiers.NEW; }
3843 | PUBLIC { $$ = Modifiers.PUBLIC; }
3844 | PROTECTED { $$ = Modifiers.PROTECTED; }
3845 | INTERNAL { $$ = Modifiers.INTERNAL; }
3846 | PRIVATE { $$ = Modifiers.PRIVATE; }
3847 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3848 | SEALED { $$ = Modifiers.SEALED; }
3849 | STATIC { $$ = Modifiers.STATIC; }
3850 | READONLY { $$ = Modifiers.READONLY; }
3851 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3852 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3853 | EXTERN { $$ = Modifiers.EXTERN; }
3854 | VOLATILE { $$ = Modifiers.VOLATILE; }
3855 | UNSAFE { $$ = Modifiers.UNSAFE; }
3859 : /* empty */ { $$ = null; }
3860 | class_base { $$ = $1; }
3864 : COLON type_list { $$ = $2; }
3867 opt_type_parameter_constraints_clauses
3868 : /* empty */ { $$ = null; }
3869 | type_parameter_constraints_clauses
3873 type_parameter_constraints_clauses
3874 : type_parameter_constraints_clause {
3875 ArrayList constraints = new ArrayList (1);
3876 constraints.Add ($1);
3879 | type_parameter_constraints_clauses type_parameter_constraints_clause {
3880 ArrayList constraints = (ArrayList) $1;
3882 constraints.Add ($2);
3887 type_parameter_constraints_clause
3888 : WHERE IDENTIFIER COLON type_parameter_constraints {
3889 LocatedToken lt = (LocatedToken) $2;
3890 $$ = new Constraints (lt.Value, (ArrayList) $4, lt.Location);
3894 type_parameter_constraints
3895 : type_parameter_constraint {
3896 ArrayList constraints = new ArrayList (1);
3897 constraints.Add ($1);
3900 | type_parameter_constraints COMMA type_parameter_constraint {
3901 ArrayList constraints = (ArrayList) $1;
3903 constraints.Add ($3);
3908 type_parameter_constraint
3910 | NEW OPEN_PARENS CLOSE_PARENS {
3911 $$ = SpecialConstraint.Constructor;
3914 $$ = SpecialConstraint.ReferenceType;
3917 $$ = SpecialConstraint.ValueType;
3926 // A block is "contained" on the following places:
3928 // property_declaration as part of the accessor body (get/set)
3929 // operator_declaration
3930 // constructor_declaration
3931 // destructor_declaration
3932 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3937 if (current_block == null){
3938 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, (Location) $1);
3939 top_current_block = current_block;
3941 current_block = new Block (current_block, (Location) $1, Location.Null);
3944 opt_statement_list CLOSE_BRACE
3946 while (current_block.Implicit)
3947 current_block = current_block.Parent;
3949 current_block.SetEndLocation ((Location) $4);
3950 current_block = current_block.Parent;
3951 if (current_block == null)
3952 top_current_block = null;
3963 | statement_list statement
3967 : declaration_statement
3969 if ($1 != null && (Block) $1 != current_block){
3970 current_block.AddStatement ((Statement) $1);
3971 current_block = (Block) $1;
3974 | valid_declaration_statement
3976 current_block.AddStatement ((Statement) $1);
3981 valid_declaration_statement
3984 | expression_statement
3985 | selection_statement
3986 | iteration_statement
3990 | unchecked_statement
3998 : valid_declaration_statement
3999 | declaration_statement
4001 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4006 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4014 $$ = EmptyStatement.Value;
4021 LocatedToken lt = (LocatedToken) $1;
4022 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
4024 if (current_block.AddLabel (lt.Value, labeled, lt.Location))
4025 current_block.AddStatement (labeled);
4030 declaration_statement
4031 : local_variable_declaration SEMICOLON
4034 DictionaryEntry de = (DictionaryEntry) $1;
4035 Expression e = (Expression) de.Key;
4037 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
4041 | local_constant_declaration SEMICOLON
4044 DictionaryEntry de = (DictionaryEntry) $1;
4046 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
4052 * The following is from Rhys' grammar:
4053 * > Types in local variable declarations must be recognized as
4054 * > expressions to prevent reduce/reduce errors in the grammar.
4055 * > The expressions are converted into types during semantic analysis.
4058 : primary_expression opt_rank_specifier_or_nullable
4060 // FIXME: Do something smart here regarding the composition of the type.
4062 // Ok, the above "primary_expression" is there to get rid of
4063 // both reduce/reduce and shift/reduces in the grammar, it should
4064 // really just be "type_name". If you use type_name, a reduce/reduce
4065 // creeps up. If you use namespace_or_type_name (which is all we need
4066 // really) two shift/reduces appear.
4069 // So the super-trick is that primary_expression
4070 // can only be either a SimpleName or a MemberAccess.
4071 // The MemberAccess case arises when you have a fully qualified type-name like :
4073 // SimpleName is when you have
4076 Expression expr = (Expression) $1;
4077 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType || expr is QualifiedAliasMember)) {
4078 Error_ExpectingTypeName (expr);
4082 // So we extract the string corresponding to the SimpleName
4086 if ((string) $2 == "")
4089 $$ = new ComposedCast ((Expression) $1, (string) $2);
4092 | builtin_types opt_rank_specifier_or_nullable
4094 if ((string) $2 == "")
4097 $$ = new ComposedCast ((Expression) $1, (string) $2);
4101 local_variable_pointer_type
4102 : primary_expression STAR
4104 Expression expr = (Expression) $1;
4106 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType || expr is QualifiedAliasMember)) {
4107 Error_ExpectingTypeName (expr);
4111 $$ = new ComposedCast ((Expression) $1, "*");
4113 | builtin_types STAR
4115 $$ = new ComposedCast ((Expression) $1, "*");
4119 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
4121 | local_variable_pointer_type STAR
4123 $$ = new ComposedCast ((Expression) $1, "*");
4127 local_variable_declaration
4128 : local_variable_type variable_declarators
4131 $$ = new DictionaryEntry ($1, $2);
4135 | local_variable_pointer_type opt_rank_specifier_or_nullable variable_declarators
4140 if ((string) $2 == "")
4141 t = (Expression) $1;
4143 t = new ComposedCast ((Expression) $1, (string) $2);
4144 $$ = new DictionaryEntry (t, $3);
4150 local_constant_declaration
4151 : CONST local_variable_type constant_declarators
4154 $$ = new DictionaryEntry ($2, $3);
4160 expression_statement
4161 : statement_expression SEMICOLON
4168 // We have to do the wrapping here and not in the case above,
4169 // because statement_expression is used for example in for_statement
4171 statement_expression
4172 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
4173 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
4174 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
4175 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
4176 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
4177 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
4178 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
4180 Report.Error (1002, GetLocation ($1), "Expecting `;'");
4185 object_creation_expression
4186 : object_or_delegate_creation_expression
4187 { note ("complain if this is a delegate maybe?"); }
4196 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
4199 Location l = (Location) $1;
4201 $$ = new If ((Expression) $3, (Statement) $5, l);
4203 if (RootContext.WarningLevel >= 3){
4204 // FIXME: location for warning should be loc property of $5.
4205 if ($5 == EmptyStatement.Value)
4206 Report.Warning (642, l, "Possible mistaken empty statement");
4210 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
4211 embedded_statement ELSE embedded_statement
4213 Location l = (Location) $1;
4215 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
4217 if (RootContext.WarningLevel >= 3){
4218 // FIXME: location for warning should be loc property of $5 and $7.
4219 if ($5 == EmptyStatement.Value)
4220 Report.Warning (642, l, "Possible mistaken empty statement");
4221 if ($7 == EmptyStatement.Value)
4222 Report.Warning (642, l, "Possible mistaken empty statement");
4228 : SWITCH OPEN_PARENS
4230 switch_stack.Push (current_block);
4232 expression CLOSE_PARENS
4235 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
4236 current_block = (Block) switch_stack.Pop ();
4252 Report.Error (1522, lexer.Location, "Empty switch block");
4260 ArrayList sections = new ArrayList (4);
4265 | switch_sections switch_section
4267 ArrayList sections = (ArrayList) $1;
4277 current_block = current_block.CreateSwitchBlock (lexer.Location);
4281 Block topmost = current_block;
4283 while (topmost.Implicit)
4284 topmost = topmost.Parent;
4285 $$ = new SwitchSection ((ArrayList) $1, topmost);
4292 ArrayList labels = new ArrayList (4);
4297 | switch_labels switch_label
4299 ArrayList labels = (ArrayList) ($1);
4307 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
4308 | DEFAULT COLON { $$ = new SwitchLabel (null, (Location) $2); }
4311 1523, GetLocation ($1),
4312 "The keyword case or default must precede code in switch block");
4324 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
4326 Location l = (Location) $1;
4327 $$ = new While ((Expression) $3, (Statement) $5, l);
4332 : DO embedded_statement
4333 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
4335 Location l = (Location) $1;
4337 $$ = new Do ((Statement) $2, (Expression) $5, l);
4343 opt_for_initializer SEMICOLON
4345 Block assign_block = new Block (current_block);
4346 current_block = assign_block;
4348 if ($3 is DictionaryEntry){
4349 DictionaryEntry de = (DictionaryEntry) $3;
4351 Expression type = (Expression) de.Key;
4352 ArrayList var_declarators = (ArrayList) de.Value;
4354 foreach (VariableDeclaration decl in var_declarators){
4358 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4362 Location l = lexer.Location;
4364 if (decl.expression_or_array_initializer is Expression){
4365 expr = (Expression) decl.expression_or_array_initializer;
4366 } else if (decl.expression_or_array_initializer == null) {
4369 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4370 expr = new ArrayCreation (type, "", init, decl.Location);
4373 LocalVariableReference var;
4374 var = new LocalVariableReference (assign_block, decl.identifier, l);
4377 Assign a = new Assign (var, expr, decl.Location);
4379 assign_block.AddStatement (new StatementExpression (a));
4383 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4384 // This can be referred to as $5 below.
4390 opt_for_condition SEMICOLON
4391 opt_for_iterator CLOSE_PARENS
4394 Location l = (Location) $1;
4396 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4398 current_block.AddStatement (f);
4399 while (current_block.Implicit)
4400 current_block = current_block.Parent;
4402 current_block = current_block.Parent;
4407 : /* empty */ { $$ = EmptyStatement.Value; }
4412 : local_variable_declaration
4413 | statement_expression_list
4417 : /* empty */ { $$ = null; }
4418 | boolean_expression
4422 : /* empty */ { $$ = EmptyStatement.Value; }
4427 : statement_expression_list
4430 statement_expression_list
4431 : statement_expression
4433 // CHANGE: was `null'
4434 Block b = new Block (current_block, Block.Flags.Implicit);
4436 b.AddStatement ((Statement) $1);
4439 | statement_expression_list COMMA statement_expression
4441 Block b = (Block) $1;
4443 b.AddStatement ((Statement) $3);
4449 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4451 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4454 | FOREACH OPEN_PARENS type IDENTIFIER IN
4455 expression CLOSE_PARENS
4457 Block foreach_block = new Block (current_block);
4458 current_block = foreach_block;
4460 LocatedToken lt = (LocatedToken) $4;
4461 Location l = lt.Location;
4464 vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4466 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4468 // Get a writable reference to this read-only variable.
4470 // Note that the $$ here refers to the value of _this_ code block,
4471 // not the value of the LHS non-terminal. This can be referred to as $8 below.
4472 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
4479 LocalVariableReference v = (LocalVariableReference) $8;
4480 Location l = (Location) $1;
4483 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
4484 current_block.AddStatement (f);
4487 while (current_block.Implicit)
4488 current_block = current_block.Parent;
4490 current_block = current_block.Parent;
4496 | continue_statement
4506 $$ = new Break ((Location) $1);
4511 : CONTINUE SEMICOLON
4513 $$ = new Continue ((Location) $1);
4518 : GOTO IDENTIFIER SEMICOLON
4520 LocatedToken lt = (LocatedToken) $2;
4521 $$ = new Goto (lt.Value, lt.Location);
4523 | GOTO CASE constant_expression SEMICOLON
4525 $$ = new GotoCase ((Expression) $3, (Location) $1);
4527 | GOTO DEFAULT SEMICOLON
4529 $$ = new GotoDefault ((Location) $1);
4534 : RETURN opt_expression SEMICOLON
4536 $$ = new Return ((Expression) $2, (Location) $1);
4541 : THROW opt_expression SEMICOLON
4543 $$ = new Throw ((Expression) $2, (Location) $1);
4548 : IDENTIFIER RETURN expression SEMICOLON
4550 LocatedToken lt = (LocatedToken) $1;
4551 string s = lt.Value;
4553 Report.Error (1003, lt.Location, "; expected");
4556 if (RootContext.Version == LanguageVersion.ISO_1){
4557 Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4560 if (iterator_container == null){
4561 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4564 iterator_container.SetYields ();
4565 $$ = new Yield ((Expression) $3, lt.Location);
4568 | IDENTIFIER RETURN SEMICOLON
4570 Report.Error (1627, (Location) $2, "Expression expected after yield return");
4573 | IDENTIFIER BREAK SEMICOLON
4575 LocatedToken lt = (LocatedToken) $1;
4576 string s = lt.Value;
4578 Report.Error (1003, lt.Location, "; expected");
4581 if (RootContext.Version == LanguageVersion.ISO_1){
4582 Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4585 if (iterator_container == null){
4586 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4589 iterator_container.SetYields ();
4590 $$ = new YieldBreak (lt.Location);
4601 : TRY block catch_clauses
4605 ArrayList c = (ArrayList)$3;
4606 for (int i = 0; i < c.Count; ++i) {
4607 Catch cc = (Catch) c [i];
4609 if (i != c.Count - 1)
4610 Report.Error (1017, cc.loc, "Try statement already has an empty catch block");
4617 // Now s contains the list of specific catch clauses
4618 // and g contains the general one.
4620 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4622 | TRY block opt_catch_clauses FINALLY block
4625 ArrayList s = new ArrayList (4);
4626 ArrayList catch_list = (ArrayList) $3;
4628 if (catch_list != null){
4629 foreach (Catch cc in catch_list) {
4637 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4641 Report.Error (1524, (Location) $1, "Expected catch or finally");
4647 : /* empty */ { $$ = null; }
4654 ArrayList l = new ArrayList (4);
4659 | catch_clauses catch_clause
4661 ArrayList l = (ArrayList) $1;
4669 : /* empty */ { $$ = null; }
4674 : CATCH opt_catch_args
4676 Expression type = null;
4679 DictionaryEntry cc = (DictionaryEntry) $2;
4680 type = (Expression) cc.Key;
4681 LocatedToken lt = (LocatedToken) cc.Value;
4684 ArrayList one = new ArrayList (4);
4686 one.Add (new VariableDeclaration (lt, null));
4688 current_block = new Block (current_block);
4689 Block b = declare_local_variables (type, one, lt.Location);
4694 Expression type = null;
4698 DictionaryEntry cc = (DictionaryEntry) $2;
4699 type = (Expression) cc.Key;
4700 LocatedToken lt = (LocatedToken) cc.Value;
4704 while (current_block.Implicit)
4705 current_block = current_block.Parent;
4706 current_block = current_block.Parent;
4710 $$ = new Catch (type, id, (Block) $4, ((Block) $4).loc);
4715 : /* empty */ { $$ = null; }
4720 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4722 $$ = new DictionaryEntry ($2, $3);
4730 $$ = new Checked ((Block) $2);
4737 $$ = new Unchecked ((Block) $2);
4744 RootContext.CheckUnsafeOption ((Location) $1);
4746 $$ = new Unsafe ((Block) $3);
4752 type fixed_pointer_declarators
4755 ArrayList list = (ArrayList) $4;
4756 Expression type = (Expression) $3;
4757 Location l = (Location) $1;
4758 int top = list.Count;
4760 Block assign_block = new Block (current_block);
4761 current_block = assign_block;
4763 for (int i = 0; i < top; i++){
4764 Pair p = (Pair) list [i];
4767 v = current_block.AddVariable (type, (string) p.First, l);
4771 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4779 Location l = (Location) $1;
4781 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4783 current_block.AddStatement (f);
4784 while (current_block.Implicit)
4785 current_block = current_block.Parent;
4787 current_block = current_block.Parent;
4791 fixed_pointer_declarators
4792 : fixed_pointer_declarator {
4793 ArrayList declarators = new ArrayList (4);
4795 declarators.Add ($1);
4798 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4800 ArrayList declarators = (ArrayList) $1;
4802 declarators.Add ($3);
4807 fixed_pointer_declarator
4808 : IDENTIFIER ASSIGN expression
4810 LocatedToken lt = (LocatedToken) $1;
4811 // FIXME: keep location
4812 $$ = new Pair (lt.Value, $3);
4816 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
4822 : LOCK OPEN_PARENS expression CLOSE_PARENS
4828 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
4833 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4835 Block assign_block = new Block (current_block);
4836 current_block = assign_block;
4838 if ($3 is DictionaryEntry){
4839 DictionaryEntry de = (DictionaryEntry) $3;
4840 Location l = (Location) $1;
4842 Expression type = (Expression) de.Key;
4843 ArrayList var_declarators = (ArrayList) de.Value;
4845 ArrayList vars = new ArrayList (4);
4847 foreach (VariableDeclaration decl in var_declarators){
4849 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4852 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
4855 if (decl.expression_or_array_initializer is Expression){
4856 expr = (Expression) decl.expression_or_array_initializer;
4858 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4860 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4863 expr = new ArrayCreation (type, "", init, decl.Location);
4866 LocalVariableReference var;
4868 // Get a writable reference to this read-only variable.
4869 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4871 // This is so that it is not a warning on using variables
4874 vars.Add (new DictionaryEntry (var, expr));
4876 // Assign a = new Assign (var, expr, decl.Location);
4877 // assign_block.AddStatement (new StatementExpression (a));
4880 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4881 // It can be referred to as $5 below.
4882 $$ = new DictionaryEntry (type, vars);
4889 Using u = new Using ($5, (Statement) $6, (Location) $1);
4890 current_block.AddStatement (u);
4891 while (current_block.Implicit)
4892 current_block = current_block.Parent;
4894 current_block = current_block.Parent;
4898 resource_acquisition
4899 : local_variable_declaration
4906 // A class used to pass around variable declarations and constants
4908 public class VariableDeclaration {
4909 public string identifier;
4910 public object expression_or_array_initializer;
4911 public Location Location;
4912 public Attributes OptAttributes;
4913 public string DocComment;
4915 public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
4917 this.identifier = lt.Value;
4918 this.expression_or_array_initializer = eoai;
4919 this.Location = lt.Location;
4920 this.OptAttributes = opt_attrs;
4923 public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
4929 /// Used to pass around interface property information
4931 public class InterfaceAccessorInfo {
4933 public readonly Accessor Get, Set;
4935 public InterfaceAccessorInfo (bool has_get, bool has_set,
4936 Attributes get_attrs, Attributes set_attrs, int get_mod, int set_mod, Location get_loc, Location set_loc)
4939 Get = new Accessor (null, get_mod, get_attrs, get_loc);
4941 Set = new Accessor (null, set_mod, set_attrs, set_loc);
4948 // A class used to hold info about an indexer declarator
4950 public class IndexerDeclaration {
4951 public Expression type;
4952 public MemberName interface_type;
4953 public Parameters param_list;
4954 public Location location;
4956 public IndexerDeclaration (Expression type, MemberName interface_type,
4957 Parameters param_list, Location loc)
4960 this.interface_type = interface_type;
4961 this.param_list = param_list;
4962 this.location = loc;
4967 // We use this when we do not have an object in advance that is an IIteratorContainer
4969 public class SimpleIteratorContainer : IIteratorContainer {
4972 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4977 public static SimpleIteratorContainer GetSimple () {
4978 Simple.Yields = false;
4982 public void SetYields () { Yields = true; }
4986 // A class used to hold info about an operator declarator
4988 public class OperatorDeclaration {
4989 public Operator.OpType optype;
4990 public Expression ret_type, arg1type, arg2type;
4991 public string arg1name, arg2name;
4992 public Location location;
4994 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4995 Expression arg1type, string arg1name,
4996 Expression arg2type, string arg2name, Location location)
4999 this.ret_type = ret_type;
5000 this.arg1type = arg1type;
5001 this.arg1name = arg1name;
5002 this.arg2type = arg2type;
5003 this.arg2name = arg2name;
5004 this.location = location;
5009 void Error_ExpectingTypeName (Expression expr)
5011 if (expr is Invocation){
5012 Report.Error (1002, expr.Location, "Expecting `;'");
5014 Report.Error (-1, expr.Location, "Invalid Type definition");
5018 TypeContainer pop_current_class ()
5020 TypeContainer retval = current_class;
5022 current_class = current_class.Parent;
5023 current_container = current_container.Parent;
5025 if (current_class != current_container) {
5026 if (!(current_class is ClassPart) ||
5027 ((ClassPart) current_class).PartialContainer != current_container)
5028 throw new InternalErrorException ();
5029 } else if (current_container is ClassPart)
5030 current_container = ((ClassPart) current_class).PartialContainer;
5036 // Given the @class_name name, it creates a fully qualified name
5037 // based on the containing declaration space
5040 MakeName (MemberName class_name)
5042 Namespace ns = current_namespace.NS;
5044 if (current_container.Name == ""){
5046 return new MemberName (ns.MemberName, class_name);
5050 return new MemberName (current_container.MemberName, class_name);
5054 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
5056 Block implicit_block;
5057 ArrayList inits = null;
5060 // We use the `Used' property to check whether statements
5061 // have been added to the current block. If so, we need
5062 // to create another block to contain the new declaration
5063 // otherwise, as an optimization, we use the same block to
5064 // add the declaration.
5066 // FIXME: A further optimization is to check if the statements
5067 // that were added were added as part of the initialization
5068 // below. In which case, no other statements have been executed
5069 // and we might be able to reduce the number of blocks for
5070 // situations like this:
5072 // int j = 1; int k = j + 1;
5074 if (current_block.Used)
5075 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
5077 implicit_block = current_block;
5079 foreach (VariableDeclaration decl in variable_declarators){
5081 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
5082 if (decl.expression_or_array_initializer != null){
5084 inits = new ArrayList (4);
5091 return implicit_block;
5093 foreach (VariableDeclaration decl in inits){
5097 if (decl.expression_or_array_initializer is Expression){
5098 expr = (Expression) decl.expression_or_array_initializer;
5101 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
5103 expr = new ArrayCreation (type, "", init, decl.Location);
5106 LocalVariableReference var;
5107 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
5109 assign = new Assign (var, expr, decl.Location);
5111 implicit_block.AddStatement (new StatementExpression (assign));
5114 return implicit_block;
5117 Block declare_local_constants (Expression type, ArrayList declarators)
5119 Block implicit_block;
5121 if (current_block.Used)
5122 implicit_block = new Block (current_block, Block.Flags.Implicit);
5124 implicit_block = current_block;
5126 foreach (VariableDeclaration decl in declarators){
5127 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
5130 return implicit_block;
5133 void CheckAttributeTarget (string a, Location l)
5137 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
5141 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
5147 void CheckUnaryOperator (Operator.OpType op, Location l)
5151 case Operator.OpType.LogicalNot:
5152 case Operator.OpType.OnesComplement:
5153 case Operator.OpType.Increment:
5154 case Operator.OpType.Decrement:
5155 case Operator.OpType.True:
5156 case Operator.OpType.False:
5157 case Operator.OpType.Addition:
5158 case Operator.OpType.Subtraction:
5163 Report.Error (1019, l, "Overloadable unary operator expected");
5169 void CheckBinaryOperator (Operator.OpType op, Location l)
5173 case Operator.OpType.Addition:
5174 case Operator.OpType.Subtraction:
5175 case Operator.OpType.Multiply:
5176 case Operator.OpType.Division:
5177 case Operator.OpType.Modulus:
5178 case Operator.OpType.BitwiseAnd:
5179 case Operator.OpType.BitwiseOr:
5180 case Operator.OpType.ExclusiveOr:
5181 case Operator.OpType.LeftShift:
5182 case Operator.OpType.RightShift:
5183 case Operator.OpType.Equality:
5184 case Operator.OpType.Inequality:
5185 case Operator.OpType.GreaterThan:
5186 case Operator.OpType.LessThan:
5187 case Operator.OpType.GreaterThanOrEqual:
5188 case Operator.OpType.LessThanOrEqual:
5192 Report.Error (1020, l, "Overloadable binary operator expected");
5198 void syntax_error (Location l, string msg)
5200 Report.Error (1003, l, "Syntax error, " + msg);
5203 void note (string s)
5205 // Used to put annotations
5210 public Tokenizer Lexer {
5216 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
5218 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
5219 this.name = file.Name;
5221 current_container = RootContext.Tree.Types;
5222 // TODO: Make RootContext.Tree.Types a PartialContainer.
5223 current_class = current_container;
5224 current_container.NamespaceEntry = current_namespace;
5225 oob_stack = new Stack ();
5226 switch_stack = new Stack ();
5228 lexer = new Tokenizer (reader, file, defines);
5231 public void parse ()
5234 if (yacc_verbose_flag > 1)
5235 yyparse (lexer, new yydebug.yyDebugSimple ());
5238 Tokenizer tokenizer = lexer as Tokenizer;
5239 tokenizer.cleanup ();
5240 } catch (Exception e){
5242 // Removed for production use, use parser verbose to get the output.
5244 // Console.WriteLine (e);
5245 Report.Error (-25, lexer.Location, "Parsing error");
5246 if (yacc_verbose_flag > 0)
5247 Console.WriteLine (e);
5250 RootContext.Tree.Types.NamespaceEntry = null;
5253 void CheckToken (int error, int yyToken, string msg, Location loc)
5255 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
5256 Report.Error (error, loc, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
5259 Report.Error (error, loc, msg);
5262 void CheckIdentifierToken (int yyToken, Location loc)
5264 CheckToken (1041, yyToken, "Identifier expected", loc);
5267 string ConsumeStoredComment ()
5269 string s = tmpComment;
5271 Lexer.doc_state = XmlCommentState.Allowed;
5275 Location GetLocation (object obj)
5277 if (obj is MemberCore)
5278 return ((MemberCore) obj).Location;
5279 if (obj is MemberName)
5280 return ((MemberName) obj).Location;
5281 if (obj is LocatedToken)
5282 return ((LocatedToken) obj).Location;
5283 if (obj is Location)
5284 return (Location) obj;
5285 return lexer.Location;