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 definition is 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. */
235 %token OP_SHIFT_LEFT "<<"
236 %token OP_SHIFT_RIGHT ">>"
243 %token OP_MULT_ASSIGN "*="
244 %token OP_DIV_ASSIGN "/="
245 %token OP_MOD_ASSIGN "%="
246 %token OP_ADD_ASSIGN "+="
247 %token OP_SUB_ASSIGN "-="
248 %token OP_SHIFT_LEFT_ASSIGN "<<="
249 %token OP_SHIFT_RIGHT_ASSIGN ">>="
250 %token OP_AND_ASSIGN "&="
251 %token OP_XOR_ASSIGN "^="
252 %token OP_OR_ASSIGN "|="
256 %token LITERAL_INTEGER "int literal"
257 %token LITERAL_FLOAT "float literal"
258 %token LITERAL_DOUBLE "double literal"
259 %token LITERAL_DECIMAL "decimal literal"
260 %token LITERAL_CHARACTER "character literal"
261 %token LITERAL_STRING "string literal"
264 %token CLOSE_PARENS_CAST
265 %token CLOSE_PARENS_NO_CAST
266 %token CLOSE_PARENS_OPEN_PARENS
267 %token CLOSE_PARENS_MINUS
268 %token DEFAULT_OPEN_PARENS
269 %token GENERIC_DIMENSION
271 /* Add precedence rules to solve dangling else s/r conflict */
280 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
282 %left STAR DIV PERCENT
283 %right BANG CARRET UMINUS
284 %nonassoc OP_INC OP_DEC
286 %left OPEN_BRACKET OPEN_BRACE
290 %start compilation_unit
294 : outer_declarations opt_EOF
295 | outer_declarations global_attributes opt_EOF
296 | global_attributes opt_EOF
297 | opt_EOF /* allow empty files */
303 Lexer.check_incorrect_doc_comment ();
307 Lexer.check_incorrect_doc_comment ();
313 | outer_declarations outer_declaration
318 | namespace_member_declaration
323 | using_directives using_directive
327 : using_alias_directive
329 if (RootContext.Documentation != null)
330 Lexer.doc_state = XmlCommentState.Allowed;
332 | using_namespace_directive
334 if (RootContext.Documentation != null)
335 Lexer.doc_state = XmlCommentState.Allowed;
339 using_alias_directive
340 : USING IDENTIFIER ASSIGN
341 namespace_or_type_name SEMICOLON
343 current_namespace.UsingAlias ((string) $2, (MemberName) $4, lexer.Location);
346 CheckIdentifierToken (yyToken);
350 using_namespace_directive
351 : USING namespace_name SEMICOLON
353 current_namespace.Using ((MemberName) $2, lexer.Location);
358 // Strictly speaking, namespaces don't have attributes but
359 // we parse global attributes along with namespace declarations and then
362 namespace_declaration
363 : opt_attributes NAMESPACE namespace_or_type_name
366 Report.Error(1671, Lexer.Location, "A namespace declaration cannot have modifiers or attributes");
369 MemberName name = (MemberName) $3;
371 if (name.TypeArguments != null)
372 syntax_error (lexer.Location, "namespace name expected");
374 current_namespace = new NamespaceEntry (
375 current_namespace, file, name.GetName (), lexer.Location);
377 namespace_body opt_semicolon
379 current_namespace = current_namespace.Parent;
394 : namespace_or_type_name {
395 MemberName name = (MemberName) $1;
397 if (name.TypeArguments != null)
398 syntax_error (lexer.Location, "namespace name expected");
407 if (RootContext.Documentation != null)
408 Lexer.doc_state = XmlCommentState.Allowed;
411 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, lexer.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, lexer.Location, "A namespace can only contain types and namespace declarations");
452 | method_declaration {
453 Report.Error (116, lexer.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, lexer.Location, "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 == "assembly") {
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 CheckAttributeTarget ((string) $1);
580 | EVENT { $$ = "event"; }
581 | RETURN { $$ = "return"; }
587 ArrayList attrs = new ArrayList (4);
593 | attribute_list COMMA attribute
595 ArrayList attrs = (ArrayList) $1;
607 opt_attribute_arguments
609 Location loc = (Location) $2;
610 MemberName mname = (MemberName) $1;
611 if (mname.IsGeneric) {
612 Report.Error (404, lexer.Location,
613 "'<' unexpected: attributes cannot be generic");
616 MemberName left = mname.Left;
617 string identifier = mname.Name;
619 Expression left_expr = left == null ? null : left.GetTypeExpression (loc);
621 if (current_attr_target == "assembly" || current_attr_target == "module")
622 $$ = new GlobalAttribute (current_class, current_attr_target,
623 left_expr, identifier, (ArrayList) $3, loc);
625 $$ = new Attribute (current_attr_target, left_expr, identifier, (ArrayList) $3, loc);
630 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
633 opt_attribute_arguments
634 : /* empty */ { $$ = null; }
635 | OPEN_PARENS attribute_arguments CLOSE_PARENS
643 : opt_positional_argument_list
648 ArrayList args = new ArrayList (4);
654 | positional_argument_list COMMA named_argument_list
656 ArrayList args = new ArrayList (4);
662 | named_argument_list
664 ArrayList args = new ArrayList (4);
673 opt_positional_argument_list
674 : /* empty */ { $$ = null; }
675 | positional_argument_list
678 positional_argument_list
681 ArrayList args = new ArrayList (4);
682 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
686 | positional_argument_list COMMA expression
688 ArrayList args = (ArrayList) $1;
689 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
698 ArrayList args = new ArrayList (4);
703 | named_argument_list COMMA named_argument
705 ArrayList args = (ArrayList) $1;
710 | named_argument_list COMMA expression
712 Report.Error (1016, lexer.Location, "Named attribute argument expected");
718 : IDENTIFIER ASSIGN expression
720 $$ = new DictionaryEntry (
722 new Argument ((Expression) $3, Argument.AType.Expression));
728 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
731 opt_class_member_declarations
733 | class_member_declarations
736 class_member_declarations
737 : class_member_declaration
738 | class_member_declarations
739 class_member_declaration
742 class_member_declaration
743 : constant_declaration // done
744 | field_declaration // done
745 | method_declaration // done
746 | property_declaration // done
747 | event_declaration // done
748 | indexer_declaration // done
749 | operator_declaration // done
750 | constructor_declaration // done
751 | destructor_declaration // done
761 MemberName name = MakeName ((MemberName) $5);
762 bool partial = (bool) $3;
765 ClassPart part = PartialContainer.CreatePart (
766 current_namespace, current_class, name, (int) $2,
767 (Attributes) $1, Kind.Struct, lexer.Location);
769 current_container = part.PartialContainer;
770 current_class = part;
772 current_class = new Struct (
773 current_namespace, current_class, name, (int) $2,
774 (Attributes) $1, lexer.Location);
776 current_container.AddClassOrStruct (current_class);
777 current_container = current_class;
778 RootContext.Tree.RecordDecl (name, current_class);
781 lexer.ConstraintsParsing = true;
784 opt_type_parameter_constraints_clauses
786 lexer.ConstraintsParsing = false;
789 current_class.Bases = (ArrayList) $7;
791 current_class.SetParameterInfo ((ArrayList) $8);
793 if (RootContext.Documentation != null)
794 current_class.DocComment = Lexer.consume_doc_comment ();
798 if (RootContext.Documentation != null)
799 Lexer.doc_state = XmlCommentState.Allowed;
803 $$ = pop_current_class ();
805 | opt_attributes opt_modifiers opt_partial STRUCT error {
806 CheckIdentifierToken (yyToken);
813 if (RootContext.Documentation != null)
814 Lexer.doc_state = XmlCommentState.Allowed;
816 opt_struct_member_declarations CLOSE_BRACE
819 opt_struct_member_declarations
821 | struct_member_declarations
824 struct_member_declarations
825 : struct_member_declaration
826 | struct_member_declarations struct_member_declaration
829 struct_member_declaration
830 : constant_declaration
833 | property_declaration
835 | indexer_declaration
836 | operator_declaration
837 | constructor_declaration
841 * This is only included so we can flag error 575:
842 * destructors only allowed on class types
844 | destructor_declaration
855 int modflags = (int) $2;
856 foreach (VariableDeclaration constant in (ArrayList) $5){
857 Location l = constant.Location;
858 if ((modflags & Modifiers.STATIC) != 0) {
859 Report.Error (504, l, "The constant '{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
863 Const c = new Const (
864 current_class, (Expression) $4, (string) constant.identifier,
865 (Expression) constant.expression_or_array_initializer, modflags,
868 if (RootContext.Documentation != null) {
869 c.DocComment = Lexer.consume_doc_comment ();
870 Lexer.doc_state = XmlCommentState.Allowed;
872 current_container.AddConstant (c);
878 : constant_declarator
880 ArrayList constants = new ArrayList (4);
885 | constant_declarators COMMA constant_declarator
888 ArrayList constants = (ArrayList) $1;
895 : IDENTIFIER ASSIGN constant_expression
897 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
901 // A const field requires a value to be provided
902 Report.Error (145, lexer.Location, "A const field requires a value to be provided");
914 Expression type = (Expression) $3;
917 foreach (VariableDeclaration var in (ArrayList) $4){
918 Field field = new Field (current_class, type, mod, var.identifier,
919 var.expression_or_array_initializer,
920 (Attributes) $1, var.Location);
922 if (RootContext.Documentation != null) {
923 field.DocComment = Lexer.consume_doc_comment ();
924 Lexer.doc_state = XmlCommentState.Allowed;
926 current_container.AddField (field);
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);
955 Report.Error (670, lexer.Location, "void type is not allowed for fields");
959 fixed_variable_declarators
960 : fixed_variable_declarator
962 ArrayList decl = new ArrayList (2);
966 | fixed_variable_declarators COMMA fixed_variable_declarator
968 ArrayList decls = (ArrayList) $1;
974 fixed_variable_declarator
975 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
977 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
982 : variable_declarator
984 ArrayList decl = new ArrayList (4);
988 | variable_declarators COMMA variable_declarator
990 ArrayList decls = (ArrayList) $1;
997 : IDENTIFIER ASSIGN variable_initializer
999 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
1003 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
1005 | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
1007 Report.Error (650, lexer.Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
1008 "To declare a fixed buffer field, use the fixed keyword before the field type");
1012 variable_initializer
1021 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
1023 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
1027 Report.Error (1575, lexer.Location, "A stackalloc expression requires [] after type");
1034 iterator_container = (IIteratorContainer) $1;
1035 if (RootContext.Documentation != null)
1036 Lexer.doc_state = XmlCommentState.NotAllowed;
1040 Method method = (Method) $1;
1041 Block b = (Block) $3;
1042 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
1045 if ((method.ModFlags & extern_abstract) == 0){
1047 501, lexer.Location, current_container.MakeName (method.Name) +
1048 "must declare a body because it is not marked abstract or extern");
1051 if ((method.ModFlags & Modifiers.EXTERN) != 0){
1053 179, lexer.Location, current_container.MakeName (method.Name) +
1054 " is declared extern, but has a body");
1058 method.Block = (ToplevelBlock) $3;
1059 current_container.AddMethod (method);
1061 current_local_parameters = null;
1062 iterator_container = null;
1064 if (RootContext.Documentation != null)
1065 Lexer.doc_state = XmlCommentState.Allowed;
1072 type namespace_or_type_name
1073 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1075 lexer.ConstraintsParsing = true;
1077 opt_type_parameter_constraints_clauses
1079 lexer.ConstraintsParsing = false;
1081 MemberName name = (MemberName) $4;
1083 if ($9 != null && name.TypeArguments == null)
1084 Report.Error (80, lexer.Location,
1085 "Contraints are not allowed on non-generic declarations");
1089 GenericMethod generic = null;
1090 if (name.TypeArguments != null) {
1091 generic = new GenericMethod (current_namespace, current_class,
1092 name, lexer.Location);
1094 generic.SetParameterInfo ((ArrayList) $9);
1097 method = new Method (current_class, generic, (Expression) $3, (int) $2, false,
1098 name, (Parameters) $6, (Attributes) $1, lexer.Location);
1100 current_local_parameters = (Parameters) $6;
1102 if (RootContext.Documentation != null)
1103 method.DocComment = Lexer.consume_doc_comment ();
1109 VOID namespace_or_type_name
1110 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1112 lexer.ConstraintsParsing = true;
1114 opt_type_parameter_constraints_clauses
1116 lexer.ConstraintsParsing = false;
1118 MemberName name = (MemberName) $4;
1120 if ($9 != null && name.TypeArguments == null)
1121 Report.Error (80, lexer.Location,
1122 "Contraints are not allowed on non-generic declarations");
1125 GenericMethod generic = null;
1126 if (name.TypeArguments != null) {
1127 generic = new GenericMethod (current_namespace, current_class,
1128 name, lexer.Location);
1130 generic.SetParameterInfo ((ArrayList) $9);
1133 method = new Method (current_class, generic, TypeManager.system_void_expr,
1134 (int) $2, false, name, (Parameters) $6, (Attributes) $1,
1137 current_local_parameters = (Parameters) $6;
1139 if (RootContext.Documentation != null)
1140 method.DocComment = Lexer.consume_doc_comment ();
1147 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1149 Report.Error (1585, lexer.Location,
1150 String.Format ("Modifier {0} should appear before type",
1151 Modifiers.Name ((int) $4)));
1152 MemberName name = (MemberName) $4;
1154 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1155 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, lexer.Location);
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, lexer.Location);
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, lexer.Location);
1207 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
1211 $$ = new Parameters (null, true, lexer.Location);
1218 ArrayList pars = new ArrayList (4);
1223 | fixed_parameters COMMA fixed_parameter
1225 ArrayList pars = (ArrayList) $1;
1234 opt_parameter_modifier
1238 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
1241 opt_parameter_modifier
1244 Report.Error (1001, lexer.Location, "Identifier expected");
1248 opt_parameter_modifier
1251 CheckIdentifierToken (yyToken);
1255 opt_parameter_modifier
1261 Report.Error (241, lexer.Location, "Default parameter specifiers are not permitted");
1266 opt_parameter_modifier
1267 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1268 | parameter_modifier
1272 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1273 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1277 : opt_attributes PARAMS type IDENTIFIER
1279 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1280 note ("type must be a single-dimension array type");
1282 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1284 Report.Error (1611, lexer.Location, "The params parameter cannot be declared as ref or out");
1287 | opt_attributes PARAMS type error {
1288 CheckIdentifierToken (yyToken);
1293 property_declaration
1297 namespace_or_type_name
1299 if (RootContext.Documentation != null)
1300 tmpComment = Lexer.consume_doc_comment ();
1304 implicit_value_parameter_type = (Expression) $3;
1306 lexer.PropertyParsing = true;
1308 $$ = lexer.Location;
1310 iterator_container = SimpleIteratorContainer.GetSimple ();
1312 accessor_declarations
1314 lexer.PropertyParsing = false;
1315 has_get = has_set = false;
1323 Pair pair = (Pair) $8;
1324 Accessor get_block = (Accessor) pair.First;
1325 Accessor set_block = (Accessor) pair.Second;
1327 Location loc = (Location) $7;
1328 MemberName name = (MemberName) $4;
1330 if (name.TypeArguments != null)
1331 syntax_error (lexer.Location, "a property can't have type arguments");
1333 prop = new Property (current_container, (Expression) $3, (int) $2, false,
1334 name, (Attributes) $1, get_block, set_block, loc);
1335 if (SimpleIteratorContainer.Simple.Yields)
1338 current_container.AddProperty (prop);
1339 implicit_value_parameter_type = null;
1340 iterator_container = null;
1342 if (RootContext.Documentation != null)
1343 prop.DocComment = ConsumeStoredComment ();
1348 accessor_declarations
1349 : get_accessor_declaration
1351 $$ = new Pair ($1, null);
1353 | get_accessor_declaration accessor_declarations
1355 Pair pair = (Pair) $2;
1359 | set_accessor_declaration
1361 $$ = new Pair (null, $1);
1363 | set_accessor_declaration accessor_declarations
1365 Pair pair = (Pair) $2;
1371 Report.Error (1014, lexer.Location, "A get or set accessor expected");
1376 get_accessor_declaration
1377 : opt_attributes opt_modifiers GET
1379 // If this is not the case, then current_local_parameters has already
1380 // been set in indexer_declaration
1381 if (parsing_indexer == false)
1382 current_local_parameters = null;
1384 current_local_parameters = indexer_parameters;
1385 lexer.PropertyParsing = false;
1390 Report.Error (1007, lexer.Location, "Property accessor already defined");
1393 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1395 current_local_parameters = null;
1396 lexer.PropertyParsing = true;
1398 if (RootContext.Documentation != null)
1399 if (Lexer.doc_state == XmlCommentState.Error)
1400 Lexer.doc_state = XmlCommentState.NotAllowed;
1404 set_accessor_declaration
1405 : opt_attributes opt_modifiers SET
1408 Parameter implicit_value_parameter = new Parameter (
1409 implicit_value_parameter_type, "value",
1410 Parameter.Modifier.NONE, null);
1412 if (parsing_indexer == false) {
1413 args = new Parameter [1];
1414 args [0] = implicit_value_parameter;
1415 current_local_parameters = new Parameters (args, null, lexer.Location);
1417 Parameter [] fpars = indexer_parameters.FixedParameters;
1420 int count = fpars.Length;
1422 args = new Parameter [count + 1];
1423 fpars.CopyTo (args, 0);
1424 args [count] = implicit_value_parameter;
1427 current_local_parameters = new Parameters (
1428 args, indexer_parameters.ArrayParameter, lexer.Location);
1431 lexer.PropertyParsing = false;
1436 Report.Error (1007, lexer.Location, "Property accessor already defined");
1439 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1441 current_local_parameters = null;
1442 lexer.PropertyParsing = true;
1444 if (RootContext.Documentation != null
1445 && Lexer.doc_state == XmlCommentState.Error)
1446 Lexer.doc_state = XmlCommentState.NotAllowed;
1452 | SEMICOLON { $$ = null; }
1455 interface_declaration
1459 INTERFACE member_name
1461 MemberName name = MakeName ((MemberName) $5);
1462 bool partial = (bool) $3;
1465 ClassPart part = PartialContainer.CreatePart (
1466 current_namespace, current_class, name, (int) $2,
1467 (Attributes) $1, Kind.Interface, lexer.Location);
1469 current_container = part.PartialContainer;
1470 current_class = part;
1472 current_class = new Interface (
1473 current_namespace, current_class, name, (int) $2,
1474 (Attributes) $1, lexer.Location);
1476 current_container.AddInterface (current_class);
1477 current_container = current_class;
1478 RootContext.Tree.RecordDecl (name, current_class);
1481 lexer.ConstraintsParsing = true;
1484 opt_type_parameter_constraints_clauses
1486 lexer.ConstraintsParsing = false;
1489 current_class.Bases = (ArrayList) $7;
1491 current_class.SetParameterInfo ((ArrayList) $8);
1493 if (RootContext.Documentation != null) {
1494 current_class.DocComment = Lexer.consume_doc_comment ();
1495 Lexer.doc_state = XmlCommentState.Allowed;
1500 if (RootContext.Documentation != null)
1501 Lexer.doc_state = XmlCommentState.Allowed;
1505 $$ = pop_current_class ();
1507 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1508 CheckIdentifierToken (yyToken);
1514 opt_interface_member_declarations
1518 opt_interface_member_declarations
1520 | interface_member_declarations
1523 interface_member_declarations
1524 : interface_member_declaration
1525 | interface_member_declarations interface_member_declaration
1528 interface_member_declaration
1529 : interface_method_declaration
1534 Method m = (Method) $1;
1536 if (m.IsExplicitImpl)
1537 Report.Error (541, lexer.Location,
1538 "Explicit interface declaration can only be declared in a class or struct");
1540 current_container.AddMethod (m);
1542 if (RootContext.Documentation != null)
1543 Lexer.doc_state = XmlCommentState.Allowed;
1545 | interface_property_declaration
1550 Property p = (Property) $1;
1552 if (p.IsExplicitImpl)
1553 Report.Error (541, lexer.Location,
1554 "Explicit interface declaration can only be declared in a class or struct");
1556 current_container.AddProperty (p);
1558 if (RootContext.Documentation != null)
1559 Lexer.doc_state = XmlCommentState.Allowed;
1561 | interface_event_declaration
1564 Event e = (Event) $1;
1566 if (e.IsExplicitImpl)
1567 Report.Error (541, lexer.Location,
1568 "Explicit interface declaration can only be declared in a class or struct");
1570 current_container.AddEvent (e);
1573 if (RootContext.Documentation != null)
1574 Lexer.doc_state = XmlCommentState.Allowed;
1576 | interface_indexer_declaration
1581 Indexer i = (Indexer) $1;
1583 if (i.IsExplicitImpl)
1584 Report.Error (541, lexer.Location,
1585 "Explicit interface declaration can only be declared in a class or struct");
1587 current_container.AddIndexer (i);
1589 if (RootContext.Documentation != null)
1590 Lexer.doc_state = XmlCommentState.Allowed;
1592 | delegate_declaration
1594 Report.Error (524, lexer.Location, "Interfaces can not declare delegates");
1598 Report.Error (524, lexer.Location, "Interfaces can not declare classes");
1600 | struct_declaration
1602 Report.Error (524, lexer.Location, "Interfaces can not declare structures");
1606 Report.Error (524, lexer.Location, "Interfaces can not declare enumerations");
1608 | interface_declaration
1610 Report.Error (524, lexer.Location, "Interfaces can not declare interfaces");
1612 | constant_declaration
1614 Report.Error (525, lexer.Location, "Interfaces cannot contain constants");
1622 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, lexer.Location);
1627 interface_method_declaration
1628 : opt_attributes opt_new type namespace_or_type_name
1629 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1631 lexer.ConstraintsParsing = true;
1633 opt_type_parameter_constraints_clauses
1636 lexer.ConstraintsParsing = false;
1638 MemberName name = (MemberName) $4;
1640 if ($9 != null && name.TypeArguments == null)
1641 Report.Error (80, lexer.Location,
1642 "Contraints are not allowed on non-generic declarations");
1644 GenericMethod generic = null;
1645 if (name.TypeArguments != null) {
1646 generic = new GenericMethod (current_namespace, current_class,
1647 name, lexer.Location);
1649 generic.SetParameterInfo ((ArrayList) $9);
1652 $$ = new Method (current_class, generic, (Expression) $3, (int) $2, true, name,
1653 (Parameters) $6, (Attributes) $1, lexer.Location);
1654 if (RootContext.Documentation != null)
1655 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1657 | opt_attributes opt_new VOID namespace_or_type_name
1658 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1660 lexer.ConstraintsParsing = true;
1662 opt_type_parameter_constraints_clauses
1665 lexer.ConstraintsParsing = false;
1667 MemberName name = (MemberName) $4;
1669 if ($9 != null && name.TypeArguments == null)
1670 Report.Error (80, lexer.Location,
1671 "Contraints are not allowed on non-generic declarations");
1673 GenericMethod generic = null;
1674 if (name.TypeArguments != null) {
1675 generic = new GenericMethod (current_namespace, current_class,
1676 name, lexer.Location);
1678 generic.SetParameterInfo ((ArrayList) $9);
1681 $$ = new Method (current_class, generic, TypeManager.system_void_expr, (int) $2,
1682 true, name, (Parameters) $6, (Attributes) $1, lexer.Location);
1683 if (RootContext.Documentation != null)
1684 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1686 | opt_attributes opt_new type namespace_or_type_name
1687 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1689 lexer.ConstraintsParsing = true;
1691 opt_type_parameter_constraints_clauses
1692 OPEN_BRACE opt_statement_list CLOSE_BRACE
1694 lexer.ConstraintsParsing = false;
1695 Report.Error (531, lexer.Location, "'{0}': interface members cannot have a definition", $4);
1701 interface_property_declaration
1706 { lexer.PropertyParsing = true; }
1708 { lexer.PropertyParsing = false; }
1711 if ($3 == TypeManager.system_void_expr) {
1712 Report.Error (547, lexer.Location, "'{0}': property cannot have void type", $4);
1719 InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
1721 $$ = new Property (current_class, (Expression) $3, (int) $2, true,
1722 new MemberName ((string) $4), (Attributes) $1,
1723 pinfo.Get, pinfo.Set, lexer.Location);
1724 if (RootContext.Documentation != null)
1725 ((Property) $$).DocComment = Lexer.consume_doc_comment ();
1730 CheckIdentifierToken (yyToken);
1736 : opt_attributes opt_modifiers GET SEMICOLON
1737 { $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, (int) $2, 0, lexer.Location, lexer.Location); }
1738 | opt_attributes opt_modifiers GET OPEN_BRACE
1740 Report.Error (531, lexer.Location, "'{0}': interface members cannot have a definition", ".get");
1743 | opt_attributes opt_modifiers SET SEMICOLON
1744 { $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1, 0, (int) $2, lexer.Location, lexer.Location); }
1745 | opt_attributes opt_modifiers GET SEMICOLON opt_attributes opt_modifiers SET SEMICOLON
1746 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $5, (int) $2, (int) $6, lexer.Location, lexer.Location); }
1747 | opt_attributes opt_modifiers SET SEMICOLON opt_attributes opt_modifiers GET SEMICOLON
1748 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $5, (Attributes) $1, (int) $6, (int) $2, lexer.Location, lexer.Location); }
1751 Report.Error (548, lexer.Location, "'{0}' : property or indexer must have at least one accessor", "");
1755 interface_event_declaration
1756 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1758 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1759 new MemberName ((string) $5), null,
1760 (Attributes) $1, lexer.Location);
1761 if (RootContext.Documentation != null)
1762 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1764 | opt_attributes opt_new EVENT type error {
1765 CheckIdentifierToken (yyToken);
1768 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1769 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1772 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1774 lexer.EventParsing = true;
1776 event_accessor_declarations
1778 lexer.EventParsing = false;
1781 Report.Error (69, lexer.Location, "Event in interface cannot have add or remove accessors");
1786 interface_indexer_declaration
1787 : opt_attributes opt_new type THIS
1788 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1790 { lexer.PropertyParsing = true; }
1792 { lexer.PropertyParsing = false; }
1798 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1800 $$ = new Indexer (current_class, (Expression) $3,
1801 new MemberName (TypeContainer.DefaultIndexerName),
1802 (int) $2, true, (Parameters) $6, (Attributes) $1,
1803 info.Get, info.Set, lexer.Location);
1804 if (RootContext.Documentation != null)
1805 ((Indexer) $$).DocComment = ConsumeStoredComment ();
1809 operator_declaration
1810 : opt_attributes opt_modifiers operator_declarator
1812 iterator_container = SimpleIteratorContainer.GetSimple ();
1819 OperatorDeclaration decl = (OperatorDeclaration) $3;
1821 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1823 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null);
1824 if (decl.arg2type != null)
1825 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null);
1827 Operator op = new Operator (
1828 current_class, decl.optype, decl.ret_type, (int) $2,
1829 new Parameters (param_list, null, decl.location),
1830 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1832 if (RootContext.Documentation != null) {
1833 op.DocComment = tmpComment;
1834 Lexer.doc_state = XmlCommentState.Allowed;
1837 if (SimpleIteratorContainer.Simple.Yields)
1840 // Note again, checking is done in semantic analysis
1841 current_container.AddOperator (op);
1843 current_local_parameters = null;
1844 iterator_container = null;
1850 | SEMICOLON { $$ = null; }
1853 : type OPERATOR overloadable_operator
1854 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1856 Operator.OpType op = (Operator.OpType) $3;
1857 CheckUnaryOperator (op);
1859 if (op == Operator.OpType.Addition)
1860 op = Operator.OpType.UnaryPlus;
1862 if (op == Operator.OpType.Subtraction)
1863 op = Operator.OpType.UnaryNegation;
1865 Parameter [] pars = new Parameter [1];
1866 Expression type = (Expression) $5;
1868 pars [0] = new Parameter (type, (string) $6, Parameter.Modifier.NONE, null);
1870 current_local_parameters = new Parameters (pars, null, lexer.Location);
1872 if (RootContext.Documentation != null) {
1873 tmpComment = Lexer.consume_doc_comment ();
1874 Lexer.doc_state = XmlCommentState.NotAllowed;
1877 $$ = new OperatorDeclaration (op, (Expression) $1, type, (string) $6,
1878 null, null, lexer.Location);
1880 | type OPERATOR overloadable_operator
1882 type IDENTIFIER COMMA
1886 CheckBinaryOperator ((Operator.OpType) $3);
1888 Parameter [] pars = new Parameter [2];
1890 Expression typeL = (Expression) $5;
1891 Expression typeR = (Expression) $8;
1893 pars [0] = new Parameter (typeL, (string) $6, Parameter.Modifier.NONE, null);
1894 pars [1] = new Parameter (typeR, (string) $9, Parameter.Modifier.NONE, null);
1896 current_local_parameters = new Parameters (pars, null, lexer.Location);
1898 if (RootContext.Documentation != null) {
1899 tmpComment = Lexer.consume_doc_comment ();
1900 Lexer.doc_state = XmlCommentState.NotAllowed;
1903 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1905 typeR, (string) $9, lexer.Location);
1907 | conversion_operator_declarator
1908 | type OPERATOR overloadable_operator
1910 type IDENTIFIER COMMA
1911 type IDENTIFIER COMMA
1915 Report.Error (1534, lexer.Location, "Overloaded binary operator '{0}' takes two parameters", $3);
1921 overloadable_operator
1923 : BANG { $$ = Operator.OpType.LogicalNot; }
1924 | TILDE { $$ = Operator.OpType.OnesComplement; }
1925 | OP_INC { $$ = Operator.OpType.Increment; }
1926 | OP_DEC { $$ = Operator.OpType.Decrement; }
1927 | TRUE { $$ = Operator.OpType.True; }
1928 | FALSE { $$ = Operator.OpType.False; }
1929 // Unary and binary:
1930 | PLUS { $$ = Operator.OpType.Addition; }
1931 | MINUS { $$ = Operator.OpType.Subtraction; }
1933 | STAR { $$ = Operator.OpType.Multiply; }
1934 | DIV { $$ = Operator.OpType.Division; }
1935 | PERCENT { $$ = Operator.OpType.Modulus; }
1936 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1937 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1938 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1939 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1940 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1941 | OP_EQ { $$ = Operator.OpType.Equality; }
1942 | OP_NE { $$ = Operator.OpType.Inequality; }
1943 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1944 | OP_LT { $$ = Operator.OpType.LessThan; }
1945 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1946 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1949 conversion_operator_declarator
1950 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1952 Parameter [] pars = new Parameter [1];
1954 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1956 current_local_parameters = new Parameters (pars, null, lexer.Location);
1958 if (RootContext.Documentation != null) {
1959 tmpComment = Lexer.consume_doc_comment ();
1960 Lexer.doc_state = XmlCommentState.NotAllowed;
1963 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1964 null, null, lexer.Location);
1966 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1968 Parameter [] pars = new Parameter [1];
1970 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1972 current_local_parameters = new Parameters (pars, null, lexer.Location);
1974 if (RootContext.Documentation != null) {
1975 tmpComment = Lexer.consume_doc_comment ();
1976 Lexer.doc_state = XmlCommentState.NotAllowed;
1979 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1980 null, null, lexer.Location);
1984 syntax_error (lexer.Location, "'operator' expected");
1988 syntax_error (lexer.Location, "'operator' expected");
1992 constructor_declaration
1995 constructor_declarator
1998 Constructor c = (Constructor) $3;
1999 c.Block = (ToplevelBlock) $4;
2000 c.OptAttributes = (Attributes) $1;
2001 c.ModFlags = (int) $2;
2003 if (RootContext.Documentation != null)
2004 c.DocComment = ConsumeStoredComment ();
2006 if (c.Name == current_container.Basename){
2007 if ((c.ModFlags & Modifiers.STATIC) != 0){
2008 if ((c.ModFlags & Modifiers.Accessibility) != 0){
2010 515, c.Location, String.Format (
2011 "`{0}.{1}': static constructor can not have access modifiers",
2012 c.Name, current_container.Name));
2015 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2017 if (c.Initializer != null){
2020 "Static constructors can not have an explicit this or base " +
2021 "constructor invocations");
2024 if (!c.Parameters.Empty){
2026 132, c.Location, "Static constructors should not have parameters");
2029 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2032 // We let another layer check the validity of the constructor.
2033 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
2036 current_container.AddConstructor (c);
2038 current_local_parameters = null;
2039 if (RootContext.Documentation != null)
2040 Lexer.doc_state = XmlCommentState.Allowed;
2044 constructor_declarator
2047 if (RootContext.Documentation != null) {
2048 tmpComment = Lexer.consume_doc_comment ();
2049 Lexer.doc_state = XmlCommentState.Allowed;
2052 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS _mark_
2054 current_local_parameters = (Parameters) $4;
2056 opt_constructor_initializer
2058 $$ = new Constructor (current_class, (string) $1, 0, (Parameters) $4,
2059 (ConstructorInitializer) $8, (Location) $6);
2065 | SEMICOLON { $$ = null; }
2068 opt_constructor_initializer
2069 : /* empty */ { $$ = null; }
2070 | constructor_initializer
2073 constructor_initializer
2074 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
2076 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
2078 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
2080 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
2083 Report.Error (1018, lexer.Location, "Keyword this or base expected");
2089 : /* EMPTY */ { $$ = 0; }
2090 | UNSAFE { $$ = Modifiers.UNSAFE; }
2091 | EXTERN { $$ = Modifiers.EXTERN; }
2094 destructor_declaration
2095 : opt_attributes opt_finalizer TILDE
2097 if (RootContext.Documentation != null) {
2098 tmpComment = Lexer.consume_doc_comment ();
2099 Lexer.doc_state = XmlCommentState.NotAllowed;
2102 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2104 if ((string) $5 != current_container.MemberName.Name){
2105 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
2106 } else if (current_container.Kind != Kind.Class){
2107 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
2109 Location l = lexer.Location;
2112 if (!RootContext.StdLib && current_container.Name == "System.Object")
2113 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2115 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2117 if ((m & Modifiers.UNSAFE) != 0){
2118 if (!RootContext.Unsafe){
2119 Report.Error (227, l,
2120 "Unsafe code requires the -unsafe command " +
2121 "line option to be specified");
2125 Method d = new Destructor (
2126 current_class, TypeManager.system_void_expr, m, "Finalize",
2127 new Parameters (null, null, l), (Attributes) $1, l);
2128 if (RootContext.Documentation != null)
2129 d.DocComment = ConsumeStoredComment ();
2131 d.Block = (ToplevelBlock) $8;
2132 current_container.AddMethod (d);
2140 EVENT type variable_declarators SEMICOLON
2142 foreach (VariableDeclaration var in (ArrayList) $5) {
2144 MemberName name = new MemberName (var.identifier);
2146 Event e = new EventField (
2147 current_class, (Expression) $4, (int) $2, false, name,
2148 var.expression_or_array_initializer, (Attributes) $1,
2151 current_container.AddEvent (e);
2153 if (RootContext.Documentation != null) {
2154 e.DocComment = Lexer.consume_doc_comment ();
2155 Lexer.doc_state = XmlCommentState.Allowed;
2161 EVENT type namespace_or_type_name
2164 implicit_value_parameter_type = (Expression) $4;
2165 lexer.EventParsing = true;
2167 event_accessor_declarations
2169 lexer.EventParsing = false;
2173 Location loc = (Location) $7;
2176 Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
2179 Pair pair = (Pair) $9;
2181 MemberName name = (MemberName) $5;
2183 if (name.TypeArguments != null)
2184 syntax_error (lexer.Location, "an event can't have type arguments");
2186 Event e = new EventProperty (
2187 current_class, (Expression) $4, (int) $2, false, name, null,
2188 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
2190 if (RootContext.Documentation != null) {
2191 e.DocComment = Lexer.consume_doc_comment ();
2192 Lexer.doc_state = XmlCommentState.Allowed;
2195 current_container.AddEvent (e);
2196 implicit_value_parameter_type = null;
2199 | opt_attributes opt_modifiers EVENT type namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS block {
2200 MemberName mn = (MemberName) $5;
2202 if (mn.Left != null)
2203 Report.Error (71, lexer.Location, "Explicit implementation of events requires property syntax");
2205 Report.Error (71, lexer.Location, "Event declaration should use property syntax");
2207 if (RootContext.Documentation != null)
2208 Lexer.doc_state = XmlCommentState.Allowed;
2212 event_accessor_declarations
2213 : add_accessor_declaration remove_accessor_declaration
2215 $$ = new Pair ($1, $2);
2217 | remove_accessor_declaration add_accessor_declaration
2219 $$ = new Pair ($2, $1);
2221 | add_accessor_declaration { $$ = null; }
2222 | remove_accessor_declaration { $$ = null; }
2225 Report.Error (1055, lexer.Location, "An add or remove accessor expected");
2231 add_accessor_declaration
2232 : opt_attributes ADD
2234 Parameter [] args = new Parameter [1];
2235 Parameter implicit_value_parameter = new Parameter (
2236 implicit_value_parameter_type, "value",
2237 Parameter.Modifier.NONE, null);
2239 args [0] = implicit_value_parameter;
2241 current_local_parameters = new Parameters (args, null, lexer.Location);
2242 lexer.EventParsing = false;
2246 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2247 lexer.EventParsing = true;
2249 | opt_attributes ADD error {
2250 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2253 | opt_attributes modifiers ADD {
2254 Report.Error (1609, lexer.Location, "Modifiers cannot be placed on event accessor declarations");
2259 remove_accessor_declaration
2260 : opt_attributes REMOVE
2262 Parameter [] args = new Parameter [1];
2263 Parameter implicit_value_parameter = new Parameter (
2264 implicit_value_parameter_type, "value",
2265 Parameter.Modifier.NONE, null);
2267 args [0] = implicit_value_parameter;
2269 current_local_parameters = new Parameters (args, null, lexer.Location);
2270 lexer.EventParsing = false;
2274 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2275 lexer.EventParsing = true;
2277 | opt_attributes REMOVE error {
2278 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2281 | opt_attributes modifiers REMOVE {
2282 Report.Error (1609, lexer.Location, "Modifiers cannot be placed on event accessor declarations");
2288 : opt_attributes opt_modifiers indexer_declarator
2291 IndexerDeclaration decl = (IndexerDeclaration) $3;
2293 implicit_value_parameter_type = decl.type;
2295 lexer.PropertyParsing = true;
2296 parsing_indexer = true;
2298 indexer_parameters = decl.param_list;
2300 accessor_declarations
2302 lexer.PropertyParsing = false;
2303 has_get = has_set = false;
2304 parsing_indexer = false;
2311 // The signature is computed from the signature of the indexer. Look
2312 // at section 3.6 on the spec
2313 Location loc = (Location) $5;
2315 IndexerDeclaration decl = (IndexerDeclaration) $3;
2316 Pair pair = (Pair) $7;
2317 Accessor get_block = (Accessor) pair.First;
2318 Accessor set_block = (Accessor) pair.Second;
2321 if (decl.interface_type != null)
2322 name = new MemberName (decl.interface_type,
2323 TypeContainer.DefaultIndexerName, null);
2325 name = new MemberName (TypeContainer.DefaultIndexerName);
2327 indexer = new Indexer (current_class, decl.type, name,
2328 (int) $2, false, decl.param_list, (Attributes) $1,
2329 get_block, set_block, loc);
2330 if (RootContext.Documentation != null)
2331 indexer.DocComment = ConsumeStoredComment ();
2333 current_container.AddIndexer (indexer);
2335 current_local_parameters = null;
2336 implicit_value_parameter_type = null;
2337 indexer_parameters = null;
2342 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2344 Parameters pars = (Parameters) $4;
2345 if (pars.HasArglist) {
2346 // "__arglist is not valid in this context"
2347 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2348 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2349 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2351 if (RootContext.Documentation != null) {
2352 tmpComment = Lexer.consume_doc_comment ();
2353 Lexer.doc_state = XmlCommentState.Allowed;
2356 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
2358 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2360 Parameters pars = (Parameters) $6;
2362 if (pars.HasArglist) {
2363 // "__arglist is not valid in this context"
2364 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2365 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2366 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2369 MemberName name = (MemberName) $2;
2371 $$ = new IndexerDeclaration ((Expression) $1, name, pars);
2373 if (RootContext.Documentation != null) {
2374 tmpComment = Lexer.consume_doc_comment ();
2375 Lexer.doc_state = XmlCommentState.Allowed;
2386 if (RootContext.Documentation != null)
2387 enumTypeComment = Lexer.consume_doc_comment ();
2392 bool partial = (bool) $3;
2395 Report.Error (267, lexer.Location, "The partial modifier can only appear before a 'class', 'struct', or 'interface'");
2396 break; // assumes that the parser put us in a switch
2399 Location enum_location = lexer.Location;
2401 MemberName name = MakeName (new MemberName ((string) $5));
2402 Enum e = new Enum (current_namespace, current_class, (Expression) $6, (int) $2,
2403 name, (Attributes) $1, enum_location);
2405 if (RootContext.Documentation != null)
2406 e.DocComment = enumTypeComment;
2408 foreach (VariableDeclaration ev in (ArrayList) $8) {
2409 e.AddEnumMember (ev.identifier,
2410 (Expression) ev.expression_or_array_initializer,
2411 ev.Location, ev.OptAttributes,
2415 current_container.AddEnum (e);
2416 RootContext.Tree.RecordDecl (name, e);
2423 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2424 | COLON type { $$ = $2; }
2430 if (RootContext.Documentation != null)
2431 Lexer.doc_state = XmlCommentState.Allowed;
2433 opt_enum_member_declarations
2435 // here will be evaluated after CLOSE_BLACE is consumed.
2436 if (RootContext.Documentation != null)
2437 Lexer.doc_state = XmlCommentState.Allowed;
2445 opt_enum_member_declarations
2446 : /* empty */ { $$ = new ArrayList (4); }
2447 | enum_member_declarations opt_comma { $$ = $1; }
2450 enum_member_declarations
2451 : enum_member_declaration
2453 ArrayList l = new ArrayList (4);
2458 | enum_member_declarations COMMA enum_member_declaration
2460 ArrayList l = (ArrayList) $1;
2468 enum_member_declaration
2469 : opt_attributes IDENTIFIER
2471 VariableDeclaration vd = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
2473 if (RootContext.Documentation != null) {
2474 vd.DocComment = Lexer.consume_doc_comment ();
2475 Lexer.doc_state = XmlCommentState.Allowed;
2480 | opt_attributes IDENTIFIER
2482 $$ = lexer.Location;
2483 if (RootContext.Documentation != null) {
2484 tmpComment = Lexer.consume_doc_comment ();
2485 Lexer.doc_state = XmlCommentState.NotAllowed;
2490 VariableDeclaration vd = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
2492 if (RootContext.Documentation != null)
2493 vd.DocComment = ConsumeStoredComment ();
2499 delegate_declaration
2502 DELEGATE type member_name
2503 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2505 Location l = lexer.Location;
2506 MemberName name = MakeName ((MemberName) $5);
2507 Delegate del = new Delegate (current_namespace, current_class, (Expression) $4,
2508 (int) $2, name, (Parameters) $7, (Attributes) $1, l);
2510 if (RootContext.Documentation != null) {
2511 del.DocComment = Lexer.consume_doc_comment ();
2512 Lexer.doc_state = XmlCommentState.Allowed;
2515 current_container.AddDelegate (del);
2516 RootContext.Tree.RecordDecl (name, del);
2518 current_delegate = del;
2520 lexer.ConstraintsParsing = true;
2522 opt_type_parameter_constraints_clauses
2524 lexer.ConstraintsParsing = false;
2528 current_delegate.SetParameterInfo ((ArrayList) $9);
2529 $$ = current_delegate;
2531 current_delegate = null;
2538 lexer.CheckNullable (false);
2543 lexer.CheckNullable (true);
2548 namespace_or_type_name
2550 | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list {
2551 $$ = new MemberName ((MemberName) $1, (string) $3, (TypeArguments) $4);
2556 : IDENTIFIER opt_type_argument_list {
2557 $$ = new MemberName ((string) $1, (TypeArguments) $2);
2561 opt_type_argument_list
2562 : /* empty */ { $$ = null; }
2563 | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2569 $$ = new TypeArguments ((int) $1, lexer.Location);
2575 TypeArguments type_args = new TypeArguments (lexer.Location);
2576 type_args.Add ((Expression) $1);
2579 | type_arguments COMMA type {
2580 TypeArguments type_args = (TypeArguments) $1;
2581 type_args.Add ((Expression) $3);
2587 * Before you think of adding a return_type, notice that we have been
2588 * using two rules in the places where it matters (one rule using type
2589 * and another identical one that uses VOID as the return type). This
2590 * gets rid of a shift/reduce couple
2593 : namespace_or_type_name opt_nullable
2595 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2598 $$ = new ComposedCast ((Expression) $$, "?", lexer.Location);
2600 | builtin_types opt_nullable
2603 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2613 // Note that here only unmanaged types are allowed but we
2614 // can't perform checks during this phase - we do it during
2615 // semantic analysis.
2617 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2621 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2626 : builtin_types opt_nullable
2629 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2631 | non_expression_type rank_specifier
2633 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2635 | non_expression_type STAR
2637 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2639 | expression rank_specifiers
2641 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2645 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2649 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2650 // through this different path
2652 | multiplicative_expression STAR
2654 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2661 ArrayList types = new ArrayList (4);
2666 | type_list COMMA type
2668 ArrayList types = (ArrayList) $1;
2676 * replaces all the productions for isolating the various
2677 * simple types, but we need this to reuse it easily in local_variable_type
2680 : OBJECT { $$ = TypeManager.system_object_expr; }
2681 | STRING { $$ = TypeManager.system_string_expr; }
2682 | BOOL { $$ = TypeManager.system_boolean_expr; }
2683 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2684 | FLOAT { $$ = TypeManager.system_single_expr; }
2685 | DOUBLE { $$ = TypeManager.system_double_expr; }
2690 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2691 | BYTE { $$ = TypeManager.system_byte_expr; }
2692 | SHORT { $$ = TypeManager.system_int16_expr; }
2693 | USHORT { $$ = TypeManager.system_uint16_expr; }
2694 | INT { $$ = TypeManager.system_int32_expr; }
2695 | UINT { $$ = TypeManager.system_uint32_expr; }
2696 | LONG { $$ = TypeManager.system_int64_expr; }
2697 | ULONG { $$ = TypeManager.system_uint64_expr; }
2698 | CHAR { $$ = TypeManager.system_char_expr; }
2699 | VOID { $$ = TypeManager.system_void_expr; }
2703 : type rank_specifiers opt_nullable
2705 string rank_specifiers = (string) $2;
2707 rank_specifiers += "?";
2709 $$ = new ComposedCast ((Expression) $1, rank_specifiers, lexer.Location);
2714 // Expressions, section 7.5
2723 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2725 | parenthesized_expression
2726 | default_value_expression
2728 | invocation_expression
2732 | post_increment_expression
2733 | post_decrement_expression
2737 | checked_expression
2738 | unchecked_expression
2739 | pointer_member_access
2740 | anonymous_method_expression
2747 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2748 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2749 | NULL { $$ = NullLiteral.Null; }
2753 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2754 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2755 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2760 object v = lexer.Value;
2766 $$ = IntLiteral.Zero;
2768 $$ = IntLiteral.One;
2770 $$ = new IntLiteral (i);
2771 } else if (v is uint)
2772 $$ = new UIntLiteral ((UInt32) v);
2774 $$ = new LongLiteral ((Int64) v);
2775 else if (v is ulong)
2776 $$ = new ULongLiteral ((UInt64) v);
2778 Console.WriteLine ("OOPS. Unexpected result from scanner");
2783 : TRUE { $$ = new BoolLiteral (true); }
2784 | FALSE { $$ = new BoolLiteral (false); }
2787 parenthesized_expression_0
2788 : OPEN_PARENS expression CLOSE_PARENS
2791 lexer.Deambiguate_CloseParens ();
2792 // After this, the next token returned is one of
2793 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2794 // or CLOSE_PARENS_MINUS.
2798 parenthesized_expression
2799 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2803 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2805 // If a parenthesized expression is followed by a minus, we need to wrap
2806 // the expression inside a ParenthesizedExpression for the CS0075 check
2807 // in Binary.DoResolve().
2808 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2813 : primary_expression DOT IDENTIFIER opt_type_argument_list
2815 $$ = new MemberAccess ((Expression) $1, (string) $3,
2816 (TypeArguments) $4, lexer.Location);
2818 | predefined_type DOT IDENTIFIER opt_type_argument_list
2820 $$ = new MemberAccess ((Expression) $1, (string) $3,
2821 (TypeArguments) $4, lexer.Location);
2829 invocation_expression
2830 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2833 Location l = lexer.Location;
2834 Report.Error (1, l, "Parse error");
2836 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2838 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2840 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2842 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2844 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2846 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2848 ArrayList args = new ArrayList (1);
2850 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2852 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2854 ArrayList args = ((ArrayList) $4);
2856 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2861 : /* empty */ { $$ = null; }
2868 ArrayList list = new ArrayList (4);
2872 | argument_list COMMA argument
2874 ArrayList list = (ArrayList) $1;
2878 | argument_list error {
2879 CheckToken (1026, yyToken, ", or ) expected");
2886 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2888 | non_simple_argument
2895 : REF variable_reference
2897 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2899 | OUT variable_reference
2901 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2903 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2905 ArrayList list = (ArrayList) $3;
2906 Argument[] args = new Argument [list.Count];
2907 list.CopyTo (args, 0);
2909 Expression expr = new Arglist (args, lexer.Location);
2910 $$ = new Argument (expr, Argument.AType.Expression);
2914 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2919 : expression { note ("section 5.4"); $$ = $1; }
2923 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2925 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2927 | primary_expression rank_specifiers
2929 // So the super-trick is that primary_expression
2930 // can only be either a SimpleName or a MemberAccess.
2931 // The MemberAccess case arises when you have a fully qualified type-name like :
2933 // SimpleName is when you have
2936 Expression expr = (Expression) $1;
2937 if (expr is ComposedCast){
2938 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2939 } else if (!(expr is SimpleName || expr is MemberAccess || expr is ConstructedType)){
2940 Error_ExpectingTypeName (lexer.Location, expr);
2941 $$ = TypeManager.system_object_expr;
2944 // So we extract the string corresponding to the SimpleName
2947 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2955 ArrayList list = new ArrayList (4);
2959 | expression_list COMMA expression
2961 ArrayList list = (ArrayList) $1;
2970 $$ = new This (current_block, lexer.Location);
2975 : BASE DOT IDENTIFIER
2977 $$ = new BaseAccess ((string) $3, lexer.Location);
2979 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2981 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2984 Report.Error (175, lexer.Location, "Use of keyword `base' is not valid in this context");
2989 post_increment_expression
2990 : primary_expression OP_INC
2992 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2993 (Expression) $1, lexer.Location);
2997 post_decrement_expression
2998 : primary_expression OP_DEC
3000 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
3001 (Expression) $1, lexer.Location);
3006 : object_or_delegate_creation_expression
3007 | array_creation_expression
3010 object_or_delegate_creation_expression
3011 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
3013 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
3017 array_creation_expression
3018 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
3020 opt_array_initializer
3022 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
3024 | NEW type rank_specifiers array_initializer
3026 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
3030 Report.Error (1031, lexer.Location, "Type expected");
3035 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
3050 opt_rank_specifier_or_nullable
3059 | opt_nullable rank_specifiers
3066 | opt_nullable rank_specifiers INTERR
3069 $$ = "?" + $2 + "?";
3076 : rank_specifier opt_rank_specifier
3078 $$ = (string) $2 + (string) $1;
3083 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3085 $$ = "[" + (string) $2 + "]";
3105 | dim_separators COMMA
3107 $$ = (string) $1 + ",";
3111 opt_array_initializer
3123 : OPEN_BRACE CLOSE_BRACE
3125 ArrayList list = new ArrayList (4);
3128 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3130 $$ = (ArrayList) $2;
3134 variable_initializer_list
3135 : variable_initializer
3137 ArrayList list = new ArrayList (4);
3141 | variable_initializer_list COMMA variable_initializer
3143 ArrayList list = (ArrayList) $1;
3149 void_pointer_expression
3150 : void_pointer_expression STAR
3152 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3156 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3161 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
3163 $$ = new TypeOfVoid (lexer.Location);
3165 | TYPEOF OPEN_PARENS void_pointer_expression CLOSE_PARENS
3167 $$ = new TypeOf ((Expression) $3, lexer.Location);
3169 | TYPEOF OPEN_PARENS
3171 lexer.TypeOfParsing = true;
3175 lexer.TypeOfParsing = false;
3176 $$ = new TypeOf ((Expression) $4, lexer.Location);
3181 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3182 $$ = new SizeOf ((Expression) $3, lexer.Location);
3187 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3189 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
3193 unchecked_expression
3194 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3196 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
3200 pointer_member_access
3201 : primary_expression OP_PTR IDENTIFIER
3205 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
3206 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
3210 anonymous_method_expression
3211 : DELEGATE opt_anonymous_method_signature _mark_
3213 oob_stack.Push (current_local_parameters);
3214 current_local_parameters = (Parameters)$2;
3216 // Force the next block to be created as a ToplevelBlock
3217 oob_stack.Push (current_block);
3218 oob_stack.Push (top_current_block);
3219 current_block = null;
3223 Location loc = (Location) $3;
3224 top_current_block = (Block) oob_stack.Pop ();
3225 current_block = (Block) oob_stack.Pop ();
3226 if (RootContext.Version == LanguageVersion.ISO_1){
3227 Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
3230 ToplevelBlock anon_block = (ToplevelBlock) $5;
3232 anon_block.Parent = current_block;
3233 $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block,
3236 current_local_parameters = (Parameters) oob_stack.Pop ();
3240 opt_anonymous_method_signature
3241 : /* empty */ { $$ = null; }
3242 | anonymous_method_signature
3245 anonymous_method_signature
3246 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3249 $$ = Parameters.EmptyReadOnlyParameters;
3251 ArrayList par_list = (ArrayList) $2;
3252 Parameter [] pars = new Parameter [par_list.Count];
3253 par_list.CopyTo (pars);
3254 $$ = new Parameters (pars, null, lexer.Location);
3259 opt_anonymous_method_parameter_list
3260 : /* empty */ { $$ = null; }
3261 | anonymous_method_parameter_list { $$ = $1; }
3264 anonymous_method_parameter_list
3265 : anonymous_method_parameter
3267 ArrayList a = new ArrayList (4);
3271 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3273 ArrayList a = (ArrayList) $1;
3279 anonymous_method_parameter
3280 : opt_parameter_modifier type IDENTIFIER {
3281 $$ = new Parameter ((Expression) $2, (string) $3, (Parameter.Modifier) $1, null);
3283 | PARAMS type IDENTIFIER {
3284 Report.Error (1670, lexer.Location, "params modifier not allowed in anonymous method declaration");
3289 default_value_expression
3290 : DEFAULT_OPEN_PARENS type CLOSE_PARENS
3292 $$ = new DefaultValueExpression ((Expression) $2, lexer.Location);
3297 : primary_expression
3298 | BANG prefixed_unary_expression
3300 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
3302 | TILDE prefixed_unary_expression
3304 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
3310 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3312 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3314 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3316 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3322 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3324 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3329 // The idea to split this out is from Rhys' grammar
3330 // to solve the problem with casts.
3332 prefixed_unary_expression
3334 | PLUS prefixed_unary_expression
3336 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
3338 | MINUS prefixed_unary_expression
3340 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
3342 | OP_INC prefixed_unary_expression
3344 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3345 (Expression) $2, lexer.Location);
3347 | OP_DEC prefixed_unary_expression
3349 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3350 (Expression) $2, lexer.Location);
3352 | STAR prefixed_unary_expression
3354 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
3356 | BITWISE_AND prefixed_unary_expression
3358 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
3362 pre_increment_expression
3363 : OP_INC prefixed_unary_expression
3365 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3366 (Expression) $2, lexer.Location);
3370 pre_decrement_expression
3371 : OP_DEC prefixed_unary_expression
3373 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3374 (Expression) $2, lexer.Location);
3378 multiplicative_expression
3379 : prefixed_unary_expression
3380 | multiplicative_expression STAR prefixed_unary_expression
3382 $$ = new Binary (Binary.Operator.Multiply,
3383 (Expression) $1, (Expression) $3, lexer.Location);
3385 | multiplicative_expression DIV prefixed_unary_expression
3387 $$ = new Binary (Binary.Operator.Division,
3388 (Expression) $1, (Expression) $3, lexer.Location);
3390 | multiplicative_expression PERCENT prefixed_unary_expression
3392 $$ = new Binary (Binary.Operator.Modulus,
3393 (Expression) $1, (Expression) $3, lexer.Location);
3398 : multiplicative_expression
3399 | additive_expression PLUS multiplicative_expression
3401 $$ = new Binary (Binary.Operator.Addition,
3402 (Expression) $1, (Expression) $3, lexer.Location);
3404 | additive_expression MINUS multiplicative_expression
3406 $$ = new Binary (Binary.Operator.Subtraction,
3407 (Expression) $1, (Expression) $3, lexer.Location);
3412 : additive_expression
3413 | shift_expression OP_SHIFT_LEFT additive_expression
3415 $$ = new Binary (Binary.Operator.LeftShift,
3416 (Expression) $1, (Expression) $3, lexer.Location);
3418 | shift_expression OP_SHIFT_RIGHT additive_expression
3420 $$ = new Binary (Binary.Operator.RightShift,
3421 (Expression) $1, (Expression) $3, lexer.Location);
3429 lexer.PutbackNullable ();
3433 nullable_type_or_conditional
3440 relational_expression
3442 | relational_expression OP_LT shift_expression
3444 $$ = new Binary (Binary.Operator.LessThan,
3445 (Expression) $1, (Expression) $3, lexer.Location);
3447 | relational_expression OP_GT shift_expression
3449 $$ = new Binary (Binary.Operator.GreaterThan,
3450 (Expression) $1, (Expression) $3, lexer.Location);
3452 | relational_expression OP_LE shift_expression
3454 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3455 (Expression) $1, (Expression) $3, lexer.Location);
3457 | relational_expression OP_GE shift_expression
3459 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3460 (Expression) $1, (Expression) $3, lexer.Location);
3462 | relational_expression IS
3465 } nullable_type_or_conditional
3467 $$ = new Is ((Expression) $1, (Expression) $4, lexer.Location);
3469 | relational_expression AS
3472 } nullable_type_or_conditional
3474 $$ = new As ((Expression) $1, (Expression) $4, lexer.Location);
3479 : relational_expression
3480 | equality_expression OP_EQ relational_expression
3482 $$ = new Binary (Binary.Operator.Equality,
3483 (Expression) $1, (Expression) $3, lexer.Location);
3485 | equality_expression OP_NE relational_expression
3487 $$ = new Binary (Binary.Operator.Inequality,
3488 (Expression) $1, (Expression) $3, lexer.Location);
3493 : equality_expression
3494 | and_expression BITWISE_AND equality_expression
3496 $$ = new Binary (Binary.Operator.BitwiseAnd,
3497 (Expression) $1, (Expression) $3, lexer.Location);
3501 exclusive_or_expression
3503 | exclusive_or_expression CARRET and_expression
3505 $$ = new Binary (Binary.Operator.ExclusiveOr,
3506 (Expression) $1, (Expression) $3, lexer.Location);
3510 inclusive_or_expression
3511 : exclusive_or_expression
3512 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3514 $$ = new Binary (Binary.Operator.BitwiseOr,
3515 (Expression) $1, (Expression) $3, lexer.Location);
3519 conditional_and_expression
3520 : inclusive_or_expression
3521 | conditional_and_expression OP_AND inclusive_or_expression
3523 $$ = new Binary (Binary.Operator.LogicalAnd,
3524 (Expression) $1, (Expression) $3, lexer.Location);
3528 conditional_or_expression
3529 : conditional_and_expression
3530 | conditional_or_expression OP_OR conditional_and_expression
3532 $$ = new Binary (Binary.Operator.LogicalOr,
3533 (Expression) $1, (Expression) $3, lexer.Location);
3537 conditional_expression
3538 : conditional_or_expression
3539 | conditional_or_expression INTERR expression COLON expression
3541 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
3543 | conditional_or_expression INTERR INTERR expression
3545 $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $4, lexer.Location);
3547 // We'll be resolved into a `parenthesized_expression_0' later on.
3548 | conditional_or_expression INTERR CLOSE_PARENS
3550 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
3551 lexer.PutbackCloseParens ();
3555 assignment_expression
3556 : prefixed_unary_expression ASSIGN expression
3558 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
3560 | prefixed_unary_expression OP_MULT_ASSIGN expression
3562 Location l = lexer.Location;
3564 $$ = new CompoundAssign (
3565 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
3567 | prefixed_unary_expression OP_DIV_ASSIGN expression
3569 Location l = lexer.Location;
3571 $$ = new CompoundAssign (
3572 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
3574 | prefixed_unary_expression OP_MOD_ASSIGN expression
3576 Location l = lexer.Location;
3578 $$ = new CompoundAssign (
3579 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
3581 | prefixed_unary_expression OP_ADD_ASSIGN expression
3583 Location l = lexer.Location;
3585 $$ = new CompoundAssign (
3586 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
3588 | prefixed_unary_expression OP_SUB_ASSIGN expression
3590 Location l = lexer.Location;
3592 $$ = new CompoundAssign (
3593 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
3595 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3597 Location l = lexer.Location;
3599 $$ = new CompoundAssign (
3600 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
3602 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3604 Location l = lexer.Location;
3606 $$ = new CompoundAssign (
3607 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
3609 | prefixed_unary_expression OP_AND_ASSIGN expression
3611 Location l = lexer.Location;
3613 $$ = new CompoundAssign (
3614 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
3616 | prefixed_unary_expression OP_OR_ASSIGN expression
3618 Location l = lexer.Location;
3620 $$ = new CompoundAssign (
3621 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
3623 | prefixed_unary_expression OP_XOR_ASSIGN expression
3625 Location l = lexer.Location;
3627 $$ = new CompoundAssign (
3628 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
3633 : conditional_expression
3634 | assignment_expression
3654 MemberName name = MakeName ((MemberName) $5);
3655 bool partial = (bool) $3;
3656 int mod_flags = (int) $2;
3659 ClassPart part = PartialContainer.CreatePart (
3660 current_namespace, current_class, name, mod_flags,
3661 (Attributes) $1, Kind.Class, lexer.Location);
3663 current_container = part.PartialContainer;
3664 current_class = part;
3666 if ((mod_flags & Modifiers.STATIC) != 0) {
3667 current_class = new StaticClass (
3668 current_namespace, current_class, name,
3669 mod_flags, (Attributes) $1, lexer.Location);
3671 current_class = new Class (
3672 current_namespace, current_class, name,
3673 mod_flags, (Attributes) $1, lexer.Location);
3676 current_container.AddClassOrStruct (current_class);
3677 current_container = current_class;
3678 RootContext.Tree.RecordDecl (name, current_class);
3681 lexer.ConstraintsParsing = true;
3684 opt_type_parameter_constraints_clauses
3686 lexer.ConstraintsParsing = false;
3689 if (current_class.Name == "System.Object") {
3690 Report.Error (537, current_class.Location,
3691 "The class System.Object cannot have a base " +
3692 "class or implement an interface.");
3694 current_class.Bases = (ArrayList) $7;
3697 current_class.SetParameterInfo ((ArrayList) $8);
3699 if (RootContext.Documentation != null) {
3700 current_class.DocComment = Lexer.consume_doc_comment ();
3701 Lexer.doc_state = XmlCommentState.Allowed;
3706 if (RootContext.Documentation != null)
3707 Lexer.doc_state = XmlCommentState.Allowed;
3711 $$ = pop_current_class ();
3717 { $$ = (bool) false; }
3719 { $$ = (bool) true; }
3723 : /* empty */ { $$ = (int) 0; }
3729 | modifiers modifier
3734 if ((m1 & m2) != 0) {
3735 Location l = lexer.Location;
3736 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3738 $$ = (int) (m1 | m2);
3743 : NEW { $$ = Modifiers.NEW; }
3744 | PUBLIC { $$ = Modifiers.PUBLIC; }
3745 | PROTECTED { $$ = Modifiers.PROTECTED; }
3746 | INTERNAL { $$ = Modifiers.INTERNAL; }
3747 | PRIVATE { $$ = Modifiers.PRIVATE; }
3748 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3749 | SEALED { $$ = Modifiers.SEALED; }
3750 | STATIC { $$ = Modifiers.STATIC; }
3751 | READONLY { $$ = Modifiers.READONLY; }
3752 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3753 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3754 | EXTERN { $$ = Modifiers.EXTERN; }
3755 | VOLATILE { $$ = Modifiers.VOLATILE; }
3756 | UNSAFE { $$ = Modifiers.UNSAFE; }
3760 : /* empty */ { $$ = null; }
3761 | class_base { $$ = $1; }
3765 : COLON type_list { $$ = $2; }
3768 opt_type_parameter_constraints_clauses
3769 : /* empty */ { $$ = null; }
3770 | type_parameter_constraints_clauses
3774 type_parameter_constraints_clauses
3775 : type_parameter_constraints_clause {
3776 ArrayList constraints = new ArrayList (1);
3777 constraints.Add ($1);
3780 | type_parameter_constraints_clauses type_parameter_constraints_clause {
3781 ArrayList constraints = (ArrayList) $1;
3783 constraints.Add ($2);
3788 type_parameter_constraints_clause
3789 : WHERE IDENTIFIER COLON type_parameter_constraints {
3790 $$ = new Constraints ((string) $2, (ArrayList) $4, lexer.Location);
3794 type_parameter_constraints
3795 : type_parameter_constraint {
3796 ArrayList constraints = new ArrayList (1);
3797 constraints.Add ($1);
3800 | type_parameter_constraints COMMA type_parameter_constraint {
3801 ArrayList constraints = (ArrayList) $1;
3803 constraints.Add ($3);
3808 type_parameter_constraint
3810 | NEW OPEN_PARENS CLOSE_PARENS {
3811 $$ = SpecialConstraint.Constructor;
3814 $$ = SpecialConstraint.ReferenceType;
3817 $$ = SpecialConstraint.ValueType;
3826 // A block is "contained" on the following places:
3828 // property_declaration as part of the accessor body (get/set)
3829 // operator_declaration
3830 // constructor_declaration
3831 // destructor_declaration
3832 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3837 if (current_block == null){
3838 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, lexer.Location);
3839 top_current_block = current_block;
3841 current_block = new Block (current_block, lexer.Location, Location.Null);
3844 opt_statement_list CLOSE_BRACE
3846 while (current_block.Implicit)
3847 current_block = current_block.Parent;
3849 current_block.SetEndLocation (lexer.Location);
3850 current_block = current_block.Parent;
3851 if (current_block == null)
3852 top_current_block = null;
3863 | statement_list statement
3867 : declaration_statement
3869 if ($1 != null && (Block) $1 != current_block){
3870 current_block.AddStatement ((Statement) $1);
3871 current_block = (Block) $1;
3874 | valid_declaration_statement
3876 current_block.AddStatement ((Statement) $1);
3881 valid_declaration_statement
3884 | expression_statement
3885 | selection_statement
3886 | iteration_statement
3890 | unchecked_statement
3898 : valid_declaration_statement
3899 | declaration_statement
3901 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3906 Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3914 $$ = EmptyStatement.Value;
3921 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3923 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3924 current_block.AddStatement (labeled);
3929 declaration_statement
3930 : local_variable_declaration SEMICOLON
3933 DictionaryEntry de = (DictionaryEntry) $1;
3935 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3939 | local_constant_declaration SEMICOLON
3942 DictionaryEntry de = (DictionaryEntry) $1;
3944 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3950 * The following is from Rhys' grammar:
3951 * > Types in local variable declarations must be recognized as
3952 * > expressions to prevent reduce/reduce errors in the grammar.
3953 * > The expressions are converted into types during semantic analysis.
3956 : primary_expression opt_rank_specifier_or_nullable
3958 // FIXME: Do something smart here regarding the composition of the type.
3960 // Ok, the above "primary_expression" is there to get rid of
3961 // both reduce/reduce and shift/reduces in the grammar, it should
3962 // really just be "type_name". If you use type_name, a reduce/reduce
3963 // creeps up. If you use namespace_or_type_name (which is all we need
3964 // really) two shift/reduces appear.
3967 // So the super-trick is that primary_expression
3968 // can only be either a SimpleName or a MemberAccess.
3969 // The MemberAccess case arises when you have a fully qualified type-name like :
3971 // SimpleName is when you have
3974 Expression expr = (Expression) $1;
3975 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType)) {
3976 Error_ExpectingTypeName (lexer.Location, expr);
3980 // So we extract the string corresponding to the SimpleName
3984 if ((string) $2 == "")
3987 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3990 | builtin_types opt_rank_specifier_or_nullable
3992 if ((string) $2 == "")
3995 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3999 local_variable_pointer_type
4000 : primary_expression STAR
4002 Expression expr = (Expression) $1;
4003 Location l = lexer.Location;
4005 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType)) {
4006 Error_ExpectingTypeName (l, expr);
4010 $$ = new ComposedCast ((Expression) $1, "*", l);
4012 | builtin_types STAR
4014 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
4018 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
4020 | local_variable_pointer_type STAR
4022 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
4026 local_variable_declaration
4027 : local_variable_type variable_declarators
4030 $$ = new DictionaryEntry ($1, $2);
4034 | local_variable_pointer_type opt_rank_specifier_or_nullable variable_declarators
4039 if ((string) $2 == "")
4040 t = (Expression) $1;
4042 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
4043 $$ = new DictionaryEntry (t, $3);
4049 local_constant_declaration
4050 : CONST local_variable_type constant_declarators
4053 $$ = new DictionaryEntry ($2, $3);
4059 expression_statement
4060 : statement_expression SEMICOLON
4067 // We have to do the wrapping here and not in the case above,
4068 // because statement_expression is used for example in for_statement
4070 statement_expression
4071 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4072 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4073 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4074 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4075 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4076 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4077 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4079 Report.Error (1002, lexer.Location, "Expecting `;'");
4084 object_creation_expression
4085 : object_or_delegate_creation_expression
4086 { note ("complain if this is a delegate maybe?"); }
4095 : IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS
4098 Location l = (Location) $3;
4100 $$ = new If ((Expression) $4, (Statement) $6, l);
4102 if (RootContext.WarningLevel >= 4){
4103 if ($6 == EmptyStatement.Value)
4104 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4108 | IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS
4109 embedded_statement ELSE embedded_statement
4111 Location l = (Location) $3;
4113 $$ = new If ((Expression) $4, (Statement) $6, (Statement) $8, l);
4118 : SWITCH OPEN_PARENS _mark_
4120 switch_stack.Push (current_block);
4122 expression CLOSE_PARENS
4125 $$ = new Switch ((Expression) $5, (ArrayList) $7, (Location) $3);
4126 current_block = (Block) switch_stack.Pop ();
4142 Report.Error (1522, lexer.Location, "Empty switch block");
4150 ArrayList sections = new ArrayList (4);
4155 | switch_sections switch_section
4157 ArrayList sections = (ArrayList) $1;
4167 current_block = current_block.CreateSwitchBlock (lexer.Location);
4171 Block topmost = current_block;
4173 while (topmost.Implicit)
4174 topmost = topmost.Parent;
4175 $$ = new SwitchSection ((ArrayList) $1, topmost);
4182 ArrayList labels = new ArrayList (4);
4187 | switch_labels switch_label
4189 ArrayList labels = (ArrayList) ($1);
4197 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
4198 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
4201 1523, lexer.Location,
4202 "The keyword case or default must precede code in switch block");
4214 : WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS embedded_statement
4216 Location l = (Location) $3;
4217 $$ = new While ((Expression) $4, (Statement) $6, l);
4219 if (RootContext.WarningLevel >= 4){
4220 if ($6 == EmptyStatement.Value)
4221 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4227 : DO embedded_statement
4228 WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS SEMICOLON
4230 Location l = (Location) $5;
4232 $$ = new Do ((Statement) $2, (Expression) $6, l);
4238 opt_for_initializer SEMICOLON _mark_
4240 Block assign_block = new Block (current_block);
4241 current_block = assign_block;
4243 if ($3 is DictionaryEntry){
4244 DictionaryEntry de = (DictionaryEntry) $3;
4246 Expression type = (Expression) de.Key;
4247 ArrayList var_declarators = (ArrayList) de.Value;
4249 foreach (VariableDeclaration decl in var_declarators){
4253 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4257 Location l = lexer.Location;
4259 if (decl.expression_or_array_initializer is Expression){
4260 expr = (Expression) decl.expression_or_array_initializer;
4261 } else if (decl.expression_or_array_initializer == null) {
4264 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4265 expr = new ArrayCreation (type, "", init, decl.Location);
4268 LocalVariableReference var;
4269 var = new LocalVariableReference (assign_block, decl.identifier, l);
4272 Assign a = new Assign (var, expr, decl.Location);
4274 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4278 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4279 // This can be referred to as $6 below.
4285 opt_for_condition SEMICOLON
4286 opt_for_iterator CLOSE_PARENS
4289 Location l = (Location) $5;
4291 For f = new For ((Statement) $6, (Expression) $7, (Statement) $9, (Statement) $11, l);
4293 if (RootContext.WarningLevel >= 4){
4294 if ($11 == EmptyStatement.Value)
4295 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4298 current_block.AddStatement (f);
4299 while (current_block.Implicit)
4300 current_block = current_block.Parent;
4302 current_block = current_block.Parent;
4307 : /* empty */ { $$ = EmptyStatement.Value; }
4312 : local_variable_declaration
4313 | statement_expression_list
4317 : /* empty */ { $$ = null; }
4318 | boolean_expression
4322 : /* empty */ { $$ = EmptyStatement.Value; }
4327 : statement_expression_list
4330 statement_expression_list
4331 : statement_expression
4333 // CHANGE: was `null'
4334 Block b = new Block (current_block, Block.Flags.Implicit);
4336 b.AddStatement ((Statement) $1);
4339 | statement_expression_list COMMA statement_expression
4341 Block b = (Block) $1;
4343 b.AddStatement ((Statement) $3);
4349 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4351 Report.Error (230, lexer.Location, "Type and identifier are both required in a foreach statement");
4354 | FOREACH OPEN_PARENS type IDENTIFIER IN _mark_
4355 expression CLOSE_PARENS
4357 Block foreach_block = new Block (current_block);
4358 current_block = foreach_block;
4360 Location l = lexer.Location;
4363 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, l);
4367 // Get a writable reference to this read-only variable.
4369 // Note that the $$ here refers to the value of _this_ code block,
4370 // not the value of the LHS non-terminal. This can be referred to as $9 below.
4371 $$ = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
4378 LocalVariableReference v = (LocalVariableReference) $9;
4379 Location l = (Location) $6;
4382 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
4383 current_block.AddStatement (f);
4386 while (current_block.Implicit)
4387 current_block = current_block.Parent;
4389 current_block = current_block.Parent;
4395 | continue_statement
4405 $$ = new Break (lexer.Location);
4410 : CONTINUE SEMICOLON
4412 $$ = new Continue (lexer.Location);
4417 : GOTO IDENTIFIER SEMICOLON
4419 $$ = new Goto ((string) $2, lexer.Location);
4421 | GOTO CASE constant_expression SEMICOLON
4423 $$ = new GotoCase ((Expression) $3, lexer.Location);
4425 | GOTO DEFAULT SEMICOLON
4427 $$ = new GotoDefault (lexer.Location);
4432 : RETURN opt_expression SEMICOLON
4434 $$ = new Return ((Expression) $2, lexer.Location);
4439 : THROW opt_expression SEMICOLON
4441 $$ = new Throw ((Expression) $2, lexer.Location);
4446 : IDENTIFIER RETURN expression SEMICOLON
4448 string s = (string) $1;
4450 Report.Error (1003, lexer.Location, "; expected");
4453 if (RootContext.Version == LanguageVersion.ISO_1){
4454 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4457 if (iterator_container == null){
4458 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4461 iterator_container.SetYields ();
4462 $$ = new Yield ((Expression) $3, lexer.Location);
4465 | IDENTIFIER RETURN SEMICOLON
4467 Report.Error (1627, lexer.Location, "Expression expected after yield return");
4470 | IDENTIFIER BREAK SEMICOLON
4472 string s = (string) $1;
4474 Report.Error (1003, lexer.Location, "; expected");
4477 if (RootContext.Version == LanguageVersion.ISO_1){
4478 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4481 if (iterator_container == null){
4482 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4485 iterator_container.SetYields ();
4486 $$ = new YieldBreak (lexer.Location);
4497 : TRY block catch_clauses
4501 ArrayList c = (ArrayList)$3;
4502 for (int i = 0; i < c.Count; ++i) {
4503 Catch cc = (Catch) c [i];
4505 if (i != c.Count - 1)
4506 Report.Error (1017, cc.loc, "Empty catch block must be the last in a series of catch blocks");
4513 // Now s contains the list of specific catch clauses
4514 // and g contains the general one.
4516 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4518 | TRY block opt_catch_clauses FINALLY block
4521 ArrayList s = new ArrayList (4);
4522 ArrayList catch_list = (ArrayList) $3;
4524 if (catch_list != null){
4525 foreach (Catch cc in catch_list) {
4533 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4537 Report.Error (1524, lexer.Location, "Expected catch or finally");
4542 : /* empty */ { $$ = null; }
4549 ArrayList l = new ArrayList (4);
4554 | catch_clauses catch_clause
4556 ArrayList l = (ArrayList) $1;
4564 : /* empty */ { $$ = null; }
4569 : CATCH opt_catch_args
4571 Expression type = null;
4575 DictionaryEntry cc = (DictionaryEntry) $2;
4576 type = (Expression) cc.Key;
4577 id = (string) cc.Value;
4580 ArrayList one = new ArrayList (4);
4581 Location loc = lexer.Location;
4583 one.Add (new VariableDeclaration (id, null, loc));
4585 current_block = new Block (current_block);
4586 Block b = declare_local_variables (type, one, loc);
4591 Expression type = null;
4595 DictionaryEntry cc = (DictionaryEntry) $2;
4596 type = (Expression) cc.Key;
4597 id = (string) cc.Value;
4600 while (current_block.Implicit)
4601 current_block = current_block.Parent;
4602 current_block = current_block.Parent;
4606 $$ = new Catch (type, id, (Block) $4, ((Block) $4).loc);
4611 : /* empty */ { $$ = null; }
4616 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4618 $$ = new DictionaryEntry ($2, $3);
4625 $$ = new Checked ((Block) $2);
4632 $$ = new Unchecked ((Block) $2);
4639 if (!RootContext.Unsafe){
4640 Report.Error (227, lexer.Location,
4641 "Unsafe code can only be used if -unsafe is used");
4644 $$ = new Unsafe ((Block) $3);
4650 type fixed_pointer_declarators
4653 ArrayList list = (ArrayList) $4;
4654 Expression type = (Expression) $3;
4655 Location l = lexer.Location;
4656 int top = list.Count;
4658 Block assign_block = new Block (current_block);
4659 current_block = assign_block;
4661 for (int i = 0; i < top; i++){
4662 Pair p = (Pair) list [i];
4665 v = current_block.AddVariable (type, (string) p.First, l);
4677 Location l = (Location) $6;
4679 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $8, l);
4681 if (RootContext.WarningLevel >= 4){
4682 if ($8 == EmptyStatement.Value)
4683 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4686 current_block.AddStatement (f);
4687 while (current_block.Implicit)
4688 current_block = current_block.Parent;
4690 current_block = current_block.Parent;
4694 fixed_pointer_declarators
4695 : fixed_pointer_declarator {
4696 ArrayList declarators = new ArrayList (4);
4698 declarators.Add ($1);
4701 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4703 ArrayList declarators = (ArrayList) $1;
4705 declarators.Add ($3);
4710 fixed_pointer_declarator
4711 : IDENTIFIER ASSIGN expression
4713 $$ = new Pair ($1, $3);
4717 Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
4723 : LOCK OPEN_PARENS expression CLOSE_PARENS
4729 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4734 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS _mark_
4736 Block assign_block = new Block (current_block);
4737 current_block = assign_block;
4739 if ($3 is DictionaryEntry){
4740 DictionaryEntry de = (DictionaryEntry) $3;
4741 Location l = lexer.Location;
4743 Expression type = (Expression) de.Key;
4744 ArrayList var_declarators = (ArrayList) de.Value;
4746 ArrayList vars = new ArrayList (4);
4748 foreach (VariableDeclaration decl in var_declarators){
4750 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4756 if (decl.expression_or_array_initializer is Expression){
4757 expr = (Expression) decl.expression_or_array_initializer;
4759 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4761 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4764 expr = new ArrayCreation (type, "", init, decl.Location);
4767 LocalVariableReference var;
4769 // Get a writable reference to this read-only variable.
4770 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4772 // This is so that it is not a warning on using variables
4775 vars.Add (new DictionaryEntry (var, expr));
4777 // Assign a = new Assign (var, expr, decl.Location);
4778 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4781 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4782 // It can be referred to as $6 below.
4783 $$ = new DictionaryEntry (type, vars);
4790 Using u = new Using ($6, (Statement) $7, (Location) $5);
4791 current_block.AddStatement (u);
4792 while (current_block.Implicit)
4793 current_block = current_block.Parent;
4795 current_block = current_block.Parent;
4799 resource_acquisition
4800 : local_variable_declaration
4804 // Utility rule to save location information
4807 { $$ = lexer.Location; }
4812 // A class used to pass around variable declarations and constants
4814 public class VariableDeclaration {
4815 public string identifier;
4816 public object expression_or_array_initializer;
4817 public Location Location;
4818 public Attributes OptAttributes;
4819 public string DocComment;
4821 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4823 this.identifier = id;
4824 this.expression_or_array_initializer = eoai;
4826 this.OptAttributes = opt_attrs;
4829 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4835 /// Used to pass around interface property information
4837 public class InterfaceAccessorInfo {
4839 public readonly Accessor Get, Set;
4841 public InterfaceAccessorInfo (bool has_get, bool has_set,
4842 Attributes get_attrs, Attributes set_attrs, int get_mod, int set_mod, Location get_loc, Location set_loc)
4845 Report.Error (275, get_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4847 Report.Error (275, set_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4850 Get = new Accessor (null, 0, get_attrs, get_loc);
4852 Set = new Accessor (null, 0, set_attrs, set_loc);
4859 // A class used to hold info about an indexer declarator
4861 public class IndexerDeclaration {
4862 public Expression type;
4863 public MemberName interface_type;
4864 public Parameters param_list;
4866 public IndexerDeclaration (Expression type, MemberName interface_type,
4867 Parameters param_list)
4870 this.interface_type = interface_type;
4871 this.param_list = param_list;
4876 // We use this when we do not have an object in advance that is an IIteratorContainer
4878 public class SimpleIteratorContainer : IIteratorContainer {
4881 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4886 public static SimpleIteratorContainer GetSimple () {
4887 Simple.Yields = false;
4891 public void SetYields () { Yields = true; }
4895 // A class used to hold info about an operator declarator
4897 public class OperatorDeclaration {
4898 public Operator.OpType optype;
4899 public Expression ret_type, arg1type, arg2type;
4900 public string arg1name, arg2name;
4901 public Location location;
4903 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4904 Expression arg1type, string arg1name,
4905 Expression arg2type, string arg2name, Location location)
4908 this.ret_type = ret_type;
4909 this.arg1type = arg1type;
4910 this.arg1name = arg1name;
4911 this.arg2type = arg2type;
4912 this.arg2name = arg2name;
4913 this.location = location;
4918 void Error_ExpectingTypeName (Location l, Expression expr)
4920 if (expr is Invocation){
4921 Report.Error (1002, l, "; expected");
4923 Report.Error (-1, l, "Invalid Type definition");
4927 TypeContainer pop_current_class ()
4929 TypeContainer retval = current_class;
4931 current_class = current_class.Parent;
4932 current_container = current_container.Parent;
4934 if (current_class != current_container) {
4935 if (!(current_class is ClassPart) ||
4936 ((ClassPart) current_class).PartialContainer != current_container)
4937 throw new InternalErrorException ();
4938 } else if (current_container is ClassPart)
4939 current_container = ((ClassPart) current_class).PartialContainer;
4945 // Given the @class_name name, it creates a fully qualified name
4946 // based on the containing declaration space
4949 MakeName (MemberName class_name)
4951 Namespace ns = current_namespace.NS;
4953 if (current_container.Name == ""){
4955 return new MemberName (ns.MemberName, class_name);
4959 return new MemberName (current_container.MemberName, class_name);
4963 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4965 Block implicit_block;
4966 ArrayList inits = null;
4969 // We use the `Used' property to check whether statements
4970 // have been added to the current block. If so, we need
4971 // to create another block to contain the new declaration
4972 // otherwise, as an optimization, we use the same block to
4973 // add the declaration.
4975 // FIXME: A further optimization is to check if the statements
4976 // that were added were added as part of the initialization
4977 // below. In which case, no other statements have been executed
4978 // and we might be able to reduce the number of blocks for
4979 // situations like this:
4981 // int j = 1; int k = j + 1;
4983 if (current_block.Used)
4984 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4986 implicit_block = current_block;
4988 foreach (VariableDeclaration decl in variable_declarators){
4990 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
4991 if (decl.expression_or_array_initializer != null){
4993 inits = new ArrayList (4);
5000 return implicit_block;
5002 foreach (VariableDeclaration decl in inits){
5006 if (decl.expression_or_array_initializer is Expression){
5007 expr = (Expression) decl.expression_or_array_initializer;
5010 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
5012 expr = new ArrayCreation (type, "", init, decl.Location);
5015 LocalVariableReference var;
5016 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
5018 assign = new Assign (var, expr, decl.Location);
5020 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
5023 return implicit_block;
5026 Block declare_local_constants (Expression type, ArrayList declarators)
5028 Block implicit_block;
5030 if (current_block.Used)
5031 implicit_block = new Block (current_block, Block.Flags.Implicit);
5033 implicit_block = current_block;
5035 foreach (VariableDeclaration decl in declarators){
5036 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
5039 return implicit_block;
5042 void CheckAttributeTarget (string a)
5046 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
5050 Location l = lexer.Location;
5051 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
5057 void CheckUnaryOperator (Operator.OpType op)
5061 case Operator.OpType.LogicalNot:
5062 case Operator.OpType.OnesComplement:
5063 case Operator.OpType.Increment:
5064 case Operator.OpType.Decrement:
5065 case Operator.OpType.True:
5066 case Operator.OpType.False:
5067 case Operator.OpType.Addition:
5068 case Operator.OpType.Subtraction:
5073 Location l = lexer.Location;
5074 Report.Error (1019, l, "Overloadable unary operator expected");
5080 void CheckBinaryOperator (Operator.OpType op)
5084 case Operator.OpType.Addition:
5085 case Operator.OpType.Subtraction:
5086 case Operator.OpType.Multiply:
5087 case Operator.OpType.Division:
5088 case Operator.OpType.Modulus:
5089 case Operator.OpType.BitwiseAnd:
5090 case Operator.OpType.BitwiseOr:
5091 case Operator.OpType.ExclusiveOr:
5092 case Operator.OpType.LeftShift:
5093 case Operator.OpType.RightShift:
5094 case Operator.OpType.Equality:
5095 case Operator.OpType.Inequality:
5096 case Operator.OpType.GreaterThan:
5097 case Operator.OpType.LessThan:
5098 case Operator.OpType.GreaterThanOrEqual:
5099 case Operator.OpType.LessThanOrEqual:
5103 Location l = lexer.Location;
5104 Report.Error (1020, l, "Overloadable binary operator expected");
5110 void syntax_error (Location l, string msg)
5112 Report.Error (1003, l, "Syntax error, " + msg);
5115 void note (string s)
5117 // Used to put annotations
5122 public Tokenizer Lexer {
5128 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
5130 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
5131 this.name = file.Name;
5133 current_container = RootContext.Tree.Types;
5134 // TODO: Make RootContext.Tree.Types a PartialContainer.
5135 current_class = current_container;
5136 current_container.NamespaceEntry = current_namespace;
5137 oob_stack = new Stack ();
5138 switch_stack = new Stack ();
5140 lexer = new Tokenizer (reader, file, defines);
5143 public void parse ()
5146 if (yacc_verbose_flag > 1)
5147 yyparse (lexer, new yydebug.yyDebugSimple ());
5150 Tokenizer tokenizer = lexer as Tokenizer;
5151 tokenizer.cleanup ();
5152 } catch (Exception e){
5154 // Removed for production use, use parser verbose to get the output.
5156 // Console.WriteLine (e);
5157 Report.Error (-25, lexer.Location, "Parsing error");
5158 if (yacc_verbose_flag > 0)
5159 Console.WriteLine (e);
5162 RootContext.Tree.Types.NamespaceEntry = null;
5165 void CheckToken (int error, int yyToken, string msg)
5167 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
5168 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
5171 Report.Error (error, lexer.Location, msg);
5174 void CheckIdentifierToken (int yyToken)
5176 CheckToken (1041, yyToken, "Identifier expected");
5179 string ConsumeStoredComment ()
5181 string s = tmpComment;
5183 Lexer.doc_state = XmlCommentState.Allowed;