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
420 opt_namespace_member_declarations
422 | namespace_member_declarations
425 namespace_member_declarations
426 : namespace_member_declaration
427 | namespace_member_declarations namespace_member_declaration
430 namespace_member_declaration
434 DeclSpace ds = (DeclSpace)$1;
436 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
437 Report.Error (1527, lexer.Location,
438 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
441 current_namespace.DeclarationFound = true;
443 | namespace_declaration {
444 current_namespace.DeclarationFound = true;
447 | field_declaration {
448 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
450 | method_declaration {
451 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
458 | interface_declaration
460 | delegate_declaration
462 // Enable this when we have handled all errors, because this acts as a generic fallback
465 // Console.WriteLine ("Token=" + yyToken);
466 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
478 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
486 global_attrs_enabled = false;
491 global_attrs_enabled = false;
500 ArrayList sect = (ArrayList) $1;
502 if (global_attrs_enabled) {
503 if (current_attr_target == "module") {
504 CodeGen.Module.AddAttributes (sect);
506 } else if (current_attr_target == "assembly") {
507 CodeGen.Assembly.AddAttributes (sect);
510 $$ = new Attributes (sect);
513 if (RootContext.Documentation != null) {
514 Lexer.check_incorrect_doc_comment ();
516 XmlCommentState.Allowed;
520 $$ = new Attributes (sect);
522 current_attr_target = null;
524 | attribute_sections attribute_section
526 Attributes attrs = $1 as Attributes;
527 ArrayList sect = (ArrayList) $2;
529 if (global_attrs_enabled) {
530 if (current_attr_target == "module") {
531 CodeGen.Module.AddAttributes (sect);
533 } else if (current_attr_target == "assembly") {
534 CodeGen.Assembly.AddAttributes (sect);
538 attrs = new Attributes (sect);
540 attrs.AddAttributes (sect);
544 attrs = new Attributes (sect);
546 attrs.AddAttributes (sect);
549 current_attr_target = null;
554 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
558 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
564 attribute_target_specifier
565 : attribute_target COLON
567 current_attr_target = (string)$1;
575 CheckAttributeTarget ((string) $1);
578 | EVENT { $$ = "event"; }
579 | RETURN { $$ = "return"; }
585 ArrayList attrs = new ArrayList (4);
591 | attribute_list COMMA attribute
593 ArrayList attrs = (ArrayList) $1;
605 opt_attribute_arguments
607 Location loc = (Location) $2;
608 MemberName mname = (MemberName) $1;
609 if (mname.IsGeneric) {
610 Report.Error (404, lexer.Location,
611 "'<' unexpected: attributes cannot be generic");
614 MemberName left = mname.Left;
615 string identifier = mname.Name;
617 Expression left_expr = left == null ? null : left.GetTypeExpression (loc);
619 if (current_attr_target == "assembly" || current_attr_target == "module")
620 $$ = new GlobalAttribute (current_class, current_attr_target,
621 left_expr, identifier, (ArrayList) $3, loc);
623 $$ = new Attribute (current_attr_target, left_expr, identifier, (ArrayList) $3, loc);
628 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
631 opt_attribute_arguments
632 : /* empty */ { $$ = null; }
633 | OPEN_PARENS attribute_arguments CLOSE_PARENS
641 : opt_positional_argument_list
646 ArrayList args = new ArrayList (4);
652 | positional_argument_list COMMA named_argument_list
654 ArrayList args = new ArrayList (4);
660 | named_argument_list
662 ArrayList args = new ArrayList (4);
671 opt_positional_argument_list
672 : /* empty */ { $$ = null; }
673 | positional_argument_list
676 positional_argument_list
679 ArrayList args = new ArrayList (4);
680 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
684 | positional_argument_list COMMA expression
686 ArrayList args = (ArrayList) $1;
687 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
696 ArrayList args = new ArrayList (4);
701 | named_argument_list COMMA named_argument
703 ArrayList args = (ArrayList) $1;
708 | named_argument_list COMMA expression
710 Report.Error (1016, lexer.Location, "Named attribute argument expected");
716 : IDENTIFIER ASSIGN expression
718 $$ = new DictionaryEntry (
720 new Argument ((Expression) $3, Argument.AType.Expression));
726 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
729 opt_class_member_declarations
731 | class_member_declarations
734 class_member_declarations
735 : class_member_declaration
736 | class_member_declarations
737 class_member_declaration
740 class_member_declaration
741 : constant_declaration // done
742 | field_declaration // done
743 | method_declaration // done
744 | property_declaration // done
745 | event_declaration // done
746 | indexer_declaration // done
747 | operator_declaration // done
748 | constructor_declaration // done
749 | destructor_declaration // done
759 lexer.ConstraintsParsing = true;
763 MemberName name = MakeName ((MemberName) $6);
764 bool partial = (bool) $3;
767 ClassPart part = PartialContainer.CreatePart (
768 current_namespace, current_class, name, (int) $2,
769 (Attributes) $1, Kind.Struct, lexer.Location);
771 current_container = part.PartialContainer;
772 current_class = part;
774 current_class = new Struct (
775 current_namespace, current_class, name, (int) $2,
776 (Attributes) $1, lexer.Location);
778 current_container.AddClassOrStruct (current_class);
779 current_container = current_class;
780 RootContext.Tree.RecordDecl (name, current_class);
784 opt_type_parameter_constraints_clauses
786 lexer.ConstraintsParsing = false;
789 current_class.Bases = (ArrayList) $8;
791 current_class.SetParameterInfo ((ArrayList) $9);
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);
989 | variable_declarators COMMA variable_declarator
991 ArrayList decls = (ArrayList) $1;
998 : IDENTIFIER ASSIGN variable_initializer
1000 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
1004 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
1006 | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
1008 Report.Error (650, lexer.Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
1009 "To declare a fixed size buffer field, use the fixed keyword before the field type");
1014 variable_initializer
1023 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
1025 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
1029 Report.Error (1575, lexer.Location, "A stackalloc expression requires [] after type");
1036 iterator_container = (IIteratorContainer) $1;
1037 if (RootContext.Documentation != null)
1038 Lexer.doc_state = XmlCommentState.NotAllowed;
1042 Method method = (Method) $1;
1043 Block b = (Block) $3;
1044 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
1047 if ((method.ModFlags & extern_abstract) == 0){
1049 501, lexer.Location, current_container.MakeName (method.Name) +
1050 "must declare a body because it is not marked abstract or extern");
1053 if ((method.ModFlags & Modifiers.EXTERN) != 0){
1055 179, lexer.Location, current_container.MakeName (method.Name) +
1056 " is declared extern, but has a body");
1060 method.Block = (ToplevelBlock) $3;
1061 current_container.AddMethod (method);
1063 current_local_parameters = null;
1064 iterator_container = null;
1066 if (RootContext.Documentation != null)
1067 Lexer.doc_state = XmlCommentState.Allowed;
1074 type namespace_or_type_name
1075 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1077 lexer.ConstraintsParsing = true;
1079 opt_type_parameter_constraints_clauses
1081 lexer.ConstraintsParsing = false;
1083 MemberName name = (MemberName) $4;
1085 if ($9 != null && name.TypeArguments == null)
1086 Report.Error (80, lexer.Location,
1087 "Contraints are not allowed on non-generic declarations");
1091 GenericMethod generic = null;
1092 if (name.TypeArguments != null) {
1093 generic = new GenericMethod (current_namespace, current_class,
1094 name, lexer.Location);
1096 generic.SetParameterInfo ((ArrayList) $9);
1099 method = new Method (current_class, generic, (Expression) $3, (int) $2, false,
1100 name, (Parameters) $6, (Attributes) $1, lexer.Location);
1102 current_local_parameters = (Parameters) $6;
1104 if (RootContext.Documentation != null)
1105 method.DocComment = Lexer.consume_doc_comment ();
1111 VOID namespace_or_type_name
1112 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1114 lexer.ConstraintsParsing = true;
1116 opt_type_parameter_constraints_clauses
1118 lexer.ConstraintsParsing = false;
1120 MemberName name = (MemberName) $4;
1122 if ($9 != null && name.TypeArguments == null)
1123 Report.Error (80, lexer.Location,
1124 "Contraints are not allowed on non-generic declarations");
1127 GenericMethod generic = null;
1128 if (name.TypeArguments != null) {
1129 generic = new GenericMethod (current_namespace, current_class,
1130 name, lexer.Location);
1132 generic.SetParameterInfo ((ArrayList) $9);
1135 method = new Method (current_class, generic, TypeManager.system_void_expr,
1136 (int) $2, false, name, (Parameters) $6, (Attributes) $1,
1139 current_local_parameters = (Parameters) $6;
1141 if (RootContext.Documentation != null)
1142 method.DocComment = Lexer.consume_doc_comment ();
1149 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1151 Report.Error (1585, lexer.Location,
1152 String.Format ("Modifier {0} should appear before type",
1153 Modifiers.Name ((int) $4)));
1154 MemberName name = (MemberName) $4;
1156 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1157 0, false, name, (Parameters) $6, (Attributes) $1,
1160 current_local_parameters = (Parameters) $6;
1162 if (RootContext.Documentation != null)
1163 method.DocComment = Lexer.consume_doc_comment ();
1171 | SEMICOLON { $$ = null; }
1174 opt_formal_parameter_list
1175 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1176 | formal_parameter_list
1179 formal_parameter_list
1182 ArrayList pars_list = (ArrayList) $1;
1184 Parameter [] pars = new Parameter [pars_list.Count];
1185 pars_list.CopyTo (pars);
1187 $$ = new Parameters (pars, null, lexer.Location);
1189 | fixed_parameters COMMA parameter_array
1191 ArrayList pars_list = (ArrayList) $1;
1193 Parameter [] pars = new Parameter [pars_list.Count];
1194 pars_list.CopyTo (pars);
1196 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
1198 | fixed_parameters COMMA ARGLIST
1200 ArrayList pars_list = (ArrayList) $1;
1202 Parameter [] pars = new Parameter [pars_list.Count];
1203 pars_list.CopyTo (pars);
1205 $$ = new Parameters (pars, true, lexer.Location);
1207 | parameter_array COMMA fixed_parameters
1209 Report.Error (231, lexer.Location, "A params parameter must be the last parameter in a formal parameter list");
1212 | ARGLIST COMMA fixed_parameters
1214 Report.Error (257, lexer.Location, "An __arglist parameter must be the last parameter in a formal parameter list");
1219 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
1223 $$ = new Parameters (null, true, lexer.Location);
1230 ArrayList pars = new ArrayList (4);
1235 | fixed_parameters COMMA fixed_parameter
1237 ArrayList pars = (ArrayList) $1;
1246 opt_parameter_modifier
1250 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
1253 opt_parameter_modifier
1255 IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1257 Report.Error (1552, lexer.Location, "Array type specifier, [], must appear before parameter name");
1261 opt_parameter_modifier
1264 Report.Error (1001, lexer.Location, "Identifier expected");
1268 opt_parameter_modifier
1271 CheckIdentifierToken (yyToken);
1275 opt_parameter_modifier
1281 Report.Error (241, lexer.Location, "Default parameter specifiers are not permitted");
1286 opt_parameter_modifier
1287 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1288 | parameter_modifier
1292 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1293 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1297 : opt_attributes PARAMS type IDENTIFIER
1299 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1300 note ("type must be a single-dimension array type");
1302 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1304 Report.Error (1611, lexer.Location, "The params parameter cannot be declared as ref or out");
1307 | opt_attributes PARAMS type error {
1308 CheckIdentifierToken (yyToken);
1313 property_declaration
1317 namespace_or_type_name
1319 if (RootContext.Documentation != null)
1320 tmpComment = Lexer.consume_doc_comment ();
1324 implicit_value_parameter_type = (Expression) $3;
1326 lexer.PropertyParsing = true;
1328 $$ = lexer.Location;
1330 iterator_container = SimpleIteratorContainer.GetSimple ();
1332 accessor_declarations
1334 lexer.PropertyParsing = false;
1335 has_get = has_set = false;
1343 Pair pair = (Pair) $8;
1344 Accessor get_block = (Accessor) pair.First;
1345 Accessor set_block = (Accessor) pair.Second;
1347 Location loc = (Location) $7;
1348 MemberName name = (MemberName) $4;
1350 if (name.TypeArguments != null)
1351 syntax_error (lexer.Location, "a property can't have type arguments");
1353 prop = new Property (current_class, (Expression) $3, (int) $2, false,
1354 name, (Attributes) $1, get_block, set_block, loc);
1355 if (SimpleIteratorContainer.Simple.Yields)
1358 current_container.AddProperty (prop);
1359 implicit_value_parameter_type = null;
1360 iterator_container = null;
1362 if (RootContext.Documentation != null)
1363 prop.DocComment = ConsumeStoredComment ();
1368 accessor_declarations
1369 : get_accessor_declaration
1371 $$ = new Pair ($1, null);
1373 | get_accessor_declaration accessor_declarations
1375 Pair pair = (Pair) $2;
1379 | set_accessor_declaration
1381 $$ = new Pair (null, $1);
1383 | set_accessor_declaration accessor_declarations
1385 Pair pair = (Pair) $2;
1391 Report.Error (1014, lexer.Location, "A get or set accessor expected");
1396 get_accessor_declaration
1397 : opt_attributes opt_modifiers GET
1399 // If this is not the case, then current_local_parameters has already
1400 // been set in indexer_declaration
1401 if (parsing_indexer == false)
1402 current_local_parameters = null;
1404 current_local_parameters = indexer_parameters;
1405 lexer.PropertyParsing = false;
1410 Report.Error (1007, lexer.Location, "Property accessor already defined");
1413 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1415 current_local_parameters = null;
1416 lexer.PropertyParsing = true;
1418 if (RootContext.Documentation != null)
1419 if (Lexer.doc_state == XmlCommentState.Error)
1420 Lexer.doc_state = XmlCommentState.NotAllowed;
1424 set_accessor_declaration
1425 : opt_attributes opt_modifiers SET
1428 Parameter implicit_value_parameter = new Parameter (
1429 implicit_value_parameter_type, "value",
1430 Parameter.Modifier.NONE, null);
1432 if (parsing_indexer == false) {
1433 args = new Parameter [1];
1434 args [0] = implicit_value_parameter;
1435 current_local_parameters = new Parameters (args, null, lexer.Location);
1437 Parameter [] fpars = indexer_parameters.FixedParameters;
1440 int count = fpars.Length;
1442 args = new Parameter [count + 1];
1443 fpars.CopyTo (args, 0);
1444 args [count] = implicit_value_parameter;
1447 current_local_parameters = new Parameters (
1448 args, indexer_parameters.ArrayParameter, lexer.Location);
1451 lexer.PropertyParsing = false;
1456 Report.Error (1007, lexer.Location, "Property accessor already defined");
1459 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1461 current_local_parameters = null;
1462 lexer.PropertyParsing = true;
1464 if (RootContext.Documentation != null
1465 && Lexer.doc_state == XmlCommentState.Error)
1466 Lexer.doc_state = XmlCommentState.NotAllowed;
1472 | SEMICOLON { $$ = null; }
1475 interface_declaration
1481 lexer.ConstraintsParsing = true;
1485 MemberName name = MakeName ((MemberName) $6);
1486 bool partial = (bool) $3;
1489 ClassPart part = PartialContainer.CreatePart (
1490 current_namespace, current_class, name, (int) $2,
1491 (Attributes) $1, Kind.Interface, lexer.Location);
1493 current_container = part.PartialContainer;
1494 current_class = part;
1496 current_class = new Interface (
1497 current_namespace, current_class, name, (int) $2,
1498 (Attributes) $1, lexer.Location);
1500 current_container.AddInterface (current_class);
1501 current_container = current_class;
1502 RootContext.Tree.RecordDecl (name, current_class);
1506 opt_type_parameter_constraints_clauses
1508 lexer.ConstraintsParsing = false;
1511 current_class.Bases = (ArrayList) $8;
1513 current_class.SetParameterInfo ((ArrayList) $9);
1515 if (RootContext.Documentation != null) {
1516 current_class.DocComment = Lexer.consume_doc_comment ();
1517 Lexer.doc_state = XmlCommentState.Allowed;
1522 if (RootContext.Documentation != null)
1523 Lexer.doc_state = XmlCommentState.Allowed;
1527 $$ = pop_current_class ();
1529 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1530 CheckIdentifierToken (yyToken);
1536 opt_interface_member_declarations
1540 opt_interface_member_declarations
1542 | interface_member_declarations
1545 interface_member_declarations
1546 : interface_member_declaration
1547 | interface_member_declarations interface_member_declaration
1550 interface_member_declaration
1551 : interface_method_declaration
1556 Method m = (Method) $1;
1558 if (m.IsExplicitImpl)
1559 Report.Error (541, lexer.Location,
1560 "Explicit interface declaration can only be declared in a class or struct");
1562 current_container.AddMethod (m);
1564 if (RootContext.Documentation != null)
1565 Lexer.doc_state = XmlCommentState.Allowed;
1567 | interface_property_declaration
1572 Property p = (Property) $1;
1574 if (p.IsExplicitImpl)
1575 Report.Error (541, lexer.Location,
1576 "Explicit interface declaration can only be declared in a class or struct");
1578 current_container.AddProperty (p);
1580 if (RootContext.Documentation != null)
1581 Lexer.doc_state = XmlCommentState.Allowed;
1583 | interface_event_declaration
1586 Event e = (Event) $1;
1588 if (e.IsExplicitImpl)
1589 Report.Error (541, lexer.Location,
1590 "Explicit interface declaration can only be declared in a class or struct");
1592 current_container.AddEvent (e);
1595 if (RootContext.Documentation != null)
1596 Lexer.doc_state = XmlCommentState.Allowed;
1598 | interface_indexer_declaration
1603 Indexer i = (Indexer) $1;
1605 if (i.IsExplicitImpl)
1606 Report.Error (541, lexer.Location,
1607 "Explicit interface declaration can only be declared in a class or struct");
1609 current_container.AddIndexer (i);
1611 if (RootContext.Documentation != null)
1612 Lexer.doc_state = XmlCommentState.Allowed;
1614 | delegate_declaration
1616 Report.Error (524, lexer.Location, "Interfaces can not declare delegates");
1620 Report.Error (524, lexer.Location, "Interfaces can not declare classes");
1622 | struct_declaration
1624 Report.Error (524, lexer.Location, "Interfaces can not declare structures");
1628 Report.Error (524, lexer.Location, "Interfaces can not declare enumerations");
1630 | interface_declaration
1632 Report.Error (524, lexer.Location, "Interfaces can not declare interfaces");
1634 | constant_declaration
1636 Report.Error (525, lexer.Location, "Interfaces cannot contain constants");
1644 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, lexer.Location);
1649 interface_method_declaration
1650 : opt_attributes opt_new type namespace_or_type_name
1651 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1653 lexer.ConstraintsParsing = true;
1655 opt_type_parameter_constraints_clauses
1658 lexer.ConstraintsParsing = false;
1660 MemberName name = (MemberName) $4;
1662 if ($9 != null && name.TypeArguments == null)
1663 Report.Error (80, lexer.Location,
1664 "Contraints are not allowed on non-generic declarations");
1666 GenericMethod generic = null;
1667 if (name.TypeArguments != null) {
1668 generic = new GenericMethod (current_namespace, current_class,
1669 name, lexer.Location);
1671 generic.SetParameterInfo ((ArrayList) $9);
1674 $$ = new Method (current_class, generic, (Expression) $3, (int) $2, true, name,
1675 (Parameters) $6, (Attributes) $1, lexer.Location);
1676 if (RootContext.Documentation != null)
1677 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1679 | opt_attributes opt_new VOID namespace_or_type_name
1680 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1682 lexer.ConstraintsParsing = true;
1684 opt_type_parameter_constraints_clauses
1687 lexer.ConstraintsParsing = false;
1689 MemberName name = (MemberName) $4;
1691 if ($9 != null && name.TypeArguments == null)
1692 Report.Error (80, lexer.Location,
1693 "Contraints are not allowed on non-generic declarations");
1695 GenericMethod generic = null;
1696 if (name.TypeArguments != null) {
1697 generic = new GenericMethod (current_namespace, current_class,
1698 name, lexer.Location);
1700 generic.SetParameterInfo ((ArrayList) $9);
1703 $$ = new Method (current_class, generic, TypeManager.system_void_expr, (int) $2,
1704 true, name, (Parameters) $6, (Attributes) $1, lexer.Location);
1705 if (RootContext.Documentation != null)
1706 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1708 | opt_attributes opt_new type namespace_or_type_name
1709 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1711 lexer.ConstraintsParsing = true;
1713 opt_type_parameter_constraints_clauses
1714 OPEN_BRACE opt_statement_list CLOSE_BRACE
1716 lexer.ConstraintsParsing = false;
1717 Report.Error (531, lexer.Location, "'{0}': interface members cannot have a definition", $4);
1723 interface_property_declaration
1728 { lexer.PropertyParsing = true; }
1730 { lexer.PropertyParsing = false; }
1733 if ($3 == TypeManager.system_void_expr) {
1734 Report.Error (547, lexer.Location, "'{0}': property cannot have void type", $4);
1741 InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
1743 $$ = new Property (current_class, (Expression) $3, (int) $2, true,
1744 new MemberName ((string) $4), (Attributes) $1,
1745 pinfo.Get, pinfo.Set, lexer.Location);
1746 if (RootContext.Documentation != null)
1747 ((Property) $$).DocComment = Lexer.consume_doc_comment ();
1752 CheckIdentifierToken (yyToken);
1758 : opt_attributes opt_modifiers GET SEMICOLON
1759 { $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, (int) $2, 0, lexer.Location, lexer.Location); }
1760 | opt_attributes opt_modifiers GET OPEN_BRACE
1762 Report.Error (531, lexer.Location, "'{0}': interface members cannot have a definition", ".get");
1765 | opt_attributes opt_modifiers SET SEMICOLON
1766 { $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1, 0, (int) $2, lexer.Location, lexer.Location); }
1767 | opt_attributes opt_modifiers GET SEMICOLON opt_attributes opt_modifiers SET SEMICOLON
1768 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $5, (int) $2, (int) $6, lexer.Location, lexer.Location); }
1769 | opt_attributes opt_modifiers SET SEMICOLON opt_attributes opt_modifiers GET SEMICOLON
1770 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $5, (Attributes) $1, (int) $6, (int) $2, lexer.Location, lexer.Location); }
1773 Report.Error (548, lexer.Location, "'{0}' : property or indexer must have at least one accessor", "");
1777 interface_event_declaration
1778 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1780 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1781 new MemberName ((string) $5), null,
1782 (Attributes) $1, lexer.Location);
1783 if (RootContext.Documentation != null)
1784 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1786 | opt_attributes opt_new EVENT type error {
1787 CheckIdentifierToken (yyToken);
1790 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1791 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1794 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1796 lexer.EventParsing = true;
1798 event_accessor_declarations
1800 lexer.EventParsing = false;
1803 Report.Error (69, lexer.Location, "Event in interface cannot have add or remove accessors");
1808 interface_indexer_declaration
1809 : opt_attributes opt_new type THIS
1810 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1812 { lexer.PropertyParsing = true; }
1814 { lexer.PropertyParsing = false; }
1820 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1822 $$ = new Indexer (current_class, (Expression) $3,
1823 new MemberName (TypeContainer.DefaultIndexerName),
1824 (int) $2, true, (Parameters) $6, (Attributes) $1,
1825 info.Get, info.Set, lexer.Location);
1826 if (RootContext.Documentation != null)
1827 ((Indexer) $$).DocComment = ConsumeStoredComment ();
1831 operator_declaration
1832 : opt_attributes opt_modifiers operator_declarator
1834 iterator_container = SimpleIteratorContainer.GetSimple ();
1841 OperatorDeclaration decl = (OperatorDeclaration) $3;
1843 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1845 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null);
1846 if (decl.arg2type != null)
1847 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null);
1849 Operator op = new Operator (
1850 current_class, decl.optype, decl.ret_type, (int) $2,
1851 new Parameters (param_list, null, decl.location),
1852 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1854 if (RootContext.Documentation != null) {
1855 op.DocComment = tmpComment;
1856 Lexer.doc_state = XmlCommentState.Allowed;
1859 if (SimpleIteratorContainer.Simple.Yields)
1862 // Note again, checking is done in semantic analysis
1863 current_container.AddOperator (op);
1865 current_local_parameters = null;
1866 iterator_container = null;
1872 | SEMICOLON { $$ = null; }
1875 : type OPERATOR overloadable_operator
1876 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1878 Operator.OpType op = (Operator.OpType) $3;
1879 CheckUnaryOperator (op);
1881 if (op == Operator.OpType.Addition)
1882 op = Operator.OpType.UnaryPlus;
1884 if (op == Operator.OpType.Subtraction)
1885 op = Operator.OpType.UnaryNegation;
1887 Parameter [] pars = new Parameter [1];
1888 Expression type = (Expression) $5;
1890 pars [0] = new Parameter (type, (string) $6, Parameter.Modifier.NONE, null);
1892 current_local_parameters = new Parameters (pars, null, lexer.Location);
1894 if (RootContext.Documentation != null) {
1895 tmpComment = Lexer.consume_doc_comment ();
1896 Lexer.doc_state = XmlCommentState.NotAllowed;
1899 $$ = new OperatorDeclaration (op, (Expression) $1, type, (string) $6,
1900 null, null, lexer.Location);
1902 | type OPERATOR overloadable_operator
1904 type IDENTIFIER COMMA
1908 CheckBinaryOperator ((Operator.OpType) $3);
1910 Parameter [] pars = new Parameter [2];
1912 Expression typeL = (Expression) $5;
1913 Expression typeR = (Expression) $8;
1915 pars [0] = new Parameter (typeL, (string) $6, Parameter.Modifier.NONE, null);
1916 pars [1] = new Parameter (typeR, (string) $9, Parameter.Modifier.NONE, null);
1918 current_local_parameters = new Parameters (pars, null, lexer.Location);
1920 if (RootContext.Documentation != null) {
1921 tmpComment = Lexer.consume_doc_comment ();
1922 Lexer.doc_state = XmlCommentState.NotAllowed;
1925 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1927 typeR, (string) $9, lexer.Location);
1929 | conversion_operator_declarator
1930 | type OPERATOR overloadable_operator
1932 type IDENTIFIER COMMA
1933 type IDENTIFIER COMMA
1937 Report.Error (1534, lexer.Location, "Overloaded binary operator '{0}' takes two parameters", $3);
1940 | type OPERATOR overloadable_operator
1941 OPEN_PARENS CLOSE_PARENS
1943 Report.Error (1535, lexer.Location, "Overloaded unary operator '{0}' takes one parameter", $3);
1948 overloadable_operator
1950 : BANG { $$ = Operator.OpType.LogicalNot; }
1951 | TILDE { $$ = Operator.OpType.OnesComplement; }
1952 | OP_INC { $$ = Operator.OpType.Increment; }
1953 | OP_DEC { $$ = Operator.OpType.Decrement; }
1954 | TRUE { $$ = Operator.OpType.True; }
1955 | FALSE { $$ = Operator.OpType.False; }
1956 // Unary and binary:
1957 | PLUS { $$ = Operator.OpType.Addition; }
1958 | MINUS { $$ = Operator.OpType.Subtraction; }
1960 | STAR { $$ = Operator.OpType.Multiply; }
1961 | DIV { $$ = Operator.OpType.Division; }
1962 | PERCENT { $$ = Operator.OpType.Modulus; }
1963 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1964 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1965 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1966 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1967 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1968 | OP_EQ { $$ = Operator.OpType.Equality; }
1969 | OP_NE { $$ = Operator.OpType.Inequality; }
1970 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1971 | OP_LT { $$ = Operator.OpType.LessThan; }
1972 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1973 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1976 conversion_operator_declarator
1977 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1979 Parameter [] pars = new Parameter [1];
1981 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1983 current_local_parameters = new Parameters (pars, null, lexer.Location);
1985 if (RootContext.Documentation != null) {
1986 tmpComment = Lexer.consume_doc_comment ();
1987 Lexer.doc_state = XmlCommentState.NotAllowed;
1990 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1991 null, null, lexer.Location);
1993 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1995 Parameter [] pars = new Parameter [1];
1997 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1999 current_local_parameters = new Parameters (pars, null, lexer.Location);
2001 if (RootContext.Documentation != null) {
2002 tmpComment = Lexer.consume_doc_comment ();
2003 Lexer.doc_state = XmlCommentState.NotAllowed;
2006 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
2007 null, null, lexer.Location);
2011 syntax_error (lexer.Location, "'operator' expected");
2015 syntax_error (lexer.Location, "'operator' expected");
2019 constructor_declaration
2022 constructor_declarator
2025 Constructor c = (Constructor) $3;
2026 c.Block = (ToplevelBlock) $4;
2027 c.OptAttributes = (Attributes) $1;
2028 c.ModFlags = (int) $2;
2030 if (RootContext.Documentation != null)
2031 c.DocComment = ConsumeStoredComment ();
2033 if (c.Name == current_container.Basename){
2034 if ((c.ModFlags & Modifiers.STATIC) != 0){
2035 if ((c.ModFlags & Modifiers.Accessibility) != 0){
2037 515, c.Location, String.Format (
2038 "`{0}.{1}': static constructor can not have access modifiers",
2039 c.Name, current_container.Name));
2042 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2044 if (c.Initializer != null){
2047 "Static constructors can not have an explicit this or base " +
2048 "constructor invocations");
2051 if (!c.Parameters.Empty){
2053 132, c.Location, "Static constructors should not have parameters");
2056 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2059 // We let another layer check the validity of the constructor.
2060 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
2063 current_container.AddConstructor (c);
2065 current_local_parameters = null;
2066 if (RootContext.Documentation != null)
2067 Lexer.doc_state = XmlCommentState.Allowed;
2071 constructor_declarator
2074 if (RootContext.Documentation != null) {
2075 tmpComment = Lexer.consume_doc_comment ();
2076 Lexer.doc_state = XmlCommentState.Allowed;
2079 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS _mark_
2081 current_local_parameters = (Parameters) $4;
2083 opt_constructor_initializer
2085 $$ = new Constructor (current_class, (string) $1, 0, (Parameters) $4,
2086 (ConstructorInitializer) $8, (Location) $6);
2092 | SEMICOLON { $$ = null; }
2095 opt_constructor_initializer
2096 : /* empty */ { $$ = null; }
2097 | constructor_initializer
2100 constructor_initializer
2101 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
2103 $$ = new ConstructorBaseInitializer ((ArrayList) $4, lexer.Location);
2105 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
2107 $$ = new ConstructorThisInitializer ((ArrayList) $4, lexer.Location);
2110 Report.Error (1018, lexer.Location, "Keyword this or base expected");
2116 : /* EMPTY */ { $$ = 0; }
2117 | UNSAFE { $$ = Modifiers.UNSAFE; }
2118 | EXTERN { $$ = Modifiers.EXTERN; }
2121 destructor_declaration
2122 : opt_attributes opt_finalizer TILDE
2124 if (RootContext.Documentation != null) {
2125 tmpComment = Lexer.consume_doc_comment ();
2126 Lexer.doc_state = XmlCommentState.NotAllowed;
2129 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2131 if ((string) $5 != current_container.MemberName.Name){
2132 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
2133 } else if (current_container.Kind != Kind.Class){
2134 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
2136 Location l = lexer.Location;
2139 if (!RootContext.StdLib && current_container.Name == "System.Object")
2140 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2142 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2144 if ((m & Modifiers.UNSAFE) != 0){
2145 if (!RootContext.Unsafe){
2146 Report.Error (227, l,
2147 "Unsafe code requires the -unsafe command " +
2148 "line option to be specified");
2152 Method d = new Destructor (
2153 current_class, TypeManager.system_void_expr, m, "Finalize",
2154 new Parameters (null, null, l), (Attributes) $1, l);
2155 if (RootContext.Documentation != null)
2156 d.DocComment = ConsumeStoredComment ();
2158 d.Block = (ToplevelBlock) $8;
2159 current_container.AddMethod (d);
2167 EVENT type variable_declarators SEMICOLON
2169 foreach (VariableDeclaration var in (ArrayList) $5) {
2171 MemberName name = new MemberName (var.identifier);
2173 Event e = new EventField (
2174 current_class, (Expression) $4, (int) $2, false, name,
2175 var.expression_or_array_initializer, (Attributes) $1,
2178 current_container.AddEvent (e);
2180 if (RootContext.Documentation != null) {
2181 e.DocComment = Lexer.consume_doc_comment ();
2182 Lexer.doc_state = XmlCommentState.Allowed;
2188 EVENT type namespace_or_type_name
2191 implicit_value_parameter_type = (Expression) $4;
2192 lexer.EventParsing = true;
2194 event_accessor_declarations
2196 lexer.EventParsing = false;
2200 Location loc = (Location) $7;
2203 Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
2206 Pair pair = (Pair) $9;
2208 MemberName name = (MemberName) $5;
2210 if (name.TypeArguments != null)
2211 syntax_error (lexer.Location, "an event can't have type arguments");
2213 Event e = new EventProperty (
2214 current_class, (Expression) $4, (int) $2, false, name, null,
2215 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
2217 if (RootContext.Documentation != null) {
2218 e.DocComment = Lexer.consume_doc_comment ();
2219 Lexer.doc_state = XmlCommentState.Allowed;
2222 current_container.AddEvent (e);
2223 implicit_value_parameter_type = null;
2226 | opt_attributes opt_modifiers EVENT type namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS block {
2227 MemberName mn = (MemberName) $5;
2229 if (mn.Left != null)
2230 Report.Error (71, lexer.Location, "Explicit implementation of events requires property syntax");
2232 Report.Error (71, lexer.Location, "Event declaration should use property syntax");
2234 if (RootContext.Documentation != null)
2235 Lexer.doc_state = XmlCommentState.Allowed;
2239 event_accessor_declarations
2240 : add_accessor_declaration remove_accessor_declaration
2242 $$ = new Pair ($1, $2);
2244 | remove_accessor_declaration add_accessor_declaration
2246 $$ = new Pair ($2, $1);
2248 | add_accessor_declaration { $$ = null; }
2249 | remove_accessor_declaration { $$ = null; }
2252 Report.Error (1055, lexer.Location, "An add or remove accessor expected");
2258 add_accessor_declaration
2259 : opt_attributes ADD
2261 Parameter [] args = new Parameter [1];
2262 Parameter implicit_value_parameter = new Parameter (
2263 implicit_value_parameter_type, "value",
2264 Parameter.Modifier.NONE, null);
2266 args [0] = implicit_value_parameter;
2268 current_local_parameters = new Parameters (args, null, lexer.Location);
2269 lexer.EventParsing = false;
2273 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2274 lexer.EventParsing = true;
2276 | opt_attributes ADD error {
2277 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2280 | opt_attributes modifiers ADD {
2281 Report.Error (1609, lexer.Location, "Modifiers cannot be placed on event accessor declarations");
2286 remove_accessor_declaration
2287 : opt_attributes REMOVE
2289 Parameter [] args = new Parameter [1];
2290 Parameter implicit_value_parameter = new Parameter (
2291 implicit_value_parameter_type, "value",
2292 Parameter.Modifier.NONE, null);
2294 args [0] = implicit_value_parameter;
2296 current_local_parameters = new Parameters (args, null, lexer.Location);
2297 lexer.EventParsing = false;
2301 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2302 lexer.EventParsing = true;
2304 | opt_attributes REMOVE error {
2305 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2308 | opt_attributes modifiers REMOVE {
2309 Report.Error (1609, lexer.Location, "Modifiers cannot be placed on event accessor declarations");
2315 : opt_attributes opt_modifiers indexer_declarator
2318 IndexerDeclaration decl = (IndexerDeclaration) $3;
2320 implicit_value_parameter_type = decl.type;
2322 lexer.PropertyParsing = true;
2323 parsing_indexer = true;
2325 indexer_parameters = decl.param_list;
2327 accessor_declarations
2329 lexer.PropertyParsing = false;
2330 has_get = has_set = false;
2331 parsing_indexer = false;
2338 // The signature is computed from the signature of the indexer. Look
2339 // at section 3.6 on the spec
2340 Location loc = (Location) $5;
2342 IndexerDeclaration decl = (IndexerDeclaration) $3;
2343 Pair pair = (Pair) $7;
2344 Accessor get_block = (Accessor) pair.First;
2345 Accessor set_block = (Accessor) pair.Second;
2348 if (decl.interface_type != null)
2349 name = new MemberName (decl.interface_type,
2350 TypeContainer.DefaultIndexerName, null);
2352 name = new MemberName (TypeContainer.DefaultIndexerName);
2354 indexer = new Indexer (current_class, decl.type, name,
2355 (int) $2, false, decl.param_list, (Attributes) $1,
2356 get_block, set_block, loc);
2357 if (RootContext.Documentation != null)
2358 indexer.DocComment = ConsumeStoredComment ();
2360 current_container.AddIndexer (indexer);
2362 current_local_parameters = null;
2363 implicit_value_parameter_type = null;
2364 indexer_parameters = null;
2369 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2371 Parameters pars = (Parameters) $4;
2372 if (pars.HasArglist) {
2373 // "__arglist is not valid in this context"
2374 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2375 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2376 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2378 if (RootContext.Documentation != null) {
2379 tmpComment = Lexer.consume_doc_comment ();
2380 Lexer.doc_state = XmlCommentState.Allowed;
2383 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
2385 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2387 Parameters pars = (Parameters) $6;
2389 if (pars.HasArglist) {
2390 // "__arglist is not valid in this context"
2391 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2392 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2393 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2396 MemberName name = (MemberName) $2;
2398 $$ = new IndexerDeclaration ((Expression) $1, name, pars);
2400 if (RootContext.Documentation != null) {
2401 tmpComment = Lexer.consume_doc_comment ();
2402 Lexer.doc_state = XmlCommentState.Allowed;
2413 if (RootContext.Documentation != null)
2414 enumTypeComment = Lexer.consume_doc_comment ();
2419 bool partial = (bool) $3;
2422 Report.Error (267, lexer.Location, "The partial modifier can only appear before a 'class', 'struct', or 'interface'");
2423 break; // assumes that the parser put us in a switch
2426 Location enum_location = lexer.Location;
2428 MemberName name = MakeName (new MemberName ((string) $5));
2429 Enum e = new Enum (current_namespace, current_class, (Expression) $6, (int) $2,
2430 name, (Attributes) $1, enum_location);
2432 if (RootContext.Documentation != null)
2433 e.DocComment = enumTypeComment;
2435 foreach (VariableDeclaration ev in (ArrayList) $8) {
2436 e.AddEnumMember (ev.identifier,
2437 (Expression) ev.expression_or_array_initializer,
2438 ev.Location, ev.OptAttributes,
2442 current_container.AddEnum (e);
2443 RootContext.Tree.RecordDecl (name, e);
2450 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2451 | COLON type { $$ = $2; }
2457 if (RootContext.Documentation != null)
2458 Lexer.doc_state = XmlCommentState.Allowed;
2460 opt_enum_member_declarations
2462 // here will be evaluated after CLOSE_BLACE is consumed.
2463 if (RootContext.Documentation != null)
2464 Lexer.doc_state = XmlCommentState.Allowed;
2472 opt_enum_member_declarations
2473 : /* empty */ { $$ = new ArrayList (4); }
2474 | enum_member_declarations opt_comma { $$ = $1; }
2477 enum_member_declarations
2478 : enum_member_declaration
2480 ArrayList l = new ArrayList (4);
2485 | enum_member_declarations COMMA enum_member_declaration
2487 ArrayList l = (ArrayList) $1;
2495 enum_member_declaration
2496 : opt_attributes IDENTIFIER
2498 VariableDeclaration vd = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
2500 if (RootContext.Documentation != null) {
2501 vd.DocComment = Lexer.consume_doc_comment ();
2502 Lexer.doc_state = XmlCommentState.Allowed;
2507 | opt_attributes IDENTIFIER
2509 $$ = lexer.Location;
2510 if (RootContext.Documentation != null) {
2511 tmpComment = Lexer.consume_doc_comment ();
2512 Lexer.doc_state = XmlCommentState.NotAllowed;
2517 VariableDeclaration vd = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
2519 if (RootContext.Documentation != null)
2520 vd.DocComment = ConsumeStoredComment ();
2526 delegate_declaration
2531 lexer.ConstraintsParsing = true;
2534 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2536 Location l = lexer.Location;
2537 MemberName name = MakeName ((MemberName) $6);
2538 Delegate del = new Delegate (current_namespace, current_class, (Expression) $5,
2539 (int) $2, name, (Parameters) $8, (Attributes) $1, l);
2541 if (RootContext.Documentation != null) {
2542 del.DocComment = Lexer.consume_doc_comment ();
2543 Lexer.doc_state = XmlCommentState.Allowed;
2546 current_container.AddDelegate (del);
2547 RootContext.Tree.RecordDecl (name, del);
2549 current_delegate = del;
2551 opt_type_parameter_constraints_clauses
2553 lexer.ConstraintsParsing = false;
2557 current_delegate.SetParameterInfo ((ArrayList) $10);
2558 $$ = current_delegate;
2560 current_delegate = null;
2567 lexer.CheckNullable (false);
2572 lexer.CheckNullable (true);
2577 namespace_or_type_name
2579 | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list {
2580 $$ = new MemberName ((MemberName) $1, (string) $3, (TypeArguments) $4);
2585 : IDENTIFIER opt_type_argument_list {
2586 $$ = new MemberName ((string) $1, (TypeArguments) $2);
2590 opt_type_argument_list
2591 : /* empty */ { $$ = null; }
2592 | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2598 $$ = new TypeArguments ((int) $1, lexer.Location);
2604 TypeArguments type_args = new TypeArguments (lexer.Location);
2605 type_args.Add ((Expression) $1);
2608 | type_arguments COMMA type {
2609 TypeArguments type_args = (TypeArguments) $1;
2610 type_args.Add ((Expression) $3);
2616 * Before you think of adding a return_type, notice that we have been
2617 * using two rules in the places where it matters (one rule using type
2618 * and another identical one that uses VOID as the return type). This
2619 * gets rid of a shift/reduce couple
2622 : namespace_or_type_name opt_nullable
2624 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2627 $$ = new ComposedCast ((Expression) $$, "?", lexer.Location);
2629 | builtin_types opt_nullable
2632 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2642 // Note that here only unmanaged types are allowed but we
2643 // can't perform checks during this phase - we do it during
2644 // semantic analysis.
2646 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2650 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2655 : builtin_types opt_nullable
2658 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
2660 | non_expression_type rank_specifier
2662 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2664 | non_expression_type STAR
2666 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2668 | expression rank_specifiers
2670 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2674 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2678 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2679 // through this different path
2681 | multiplicative_expression STAR
2683 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2690 ArrayList types = new ArrayList (4);
2695 | type_list COMMA type
2697 ArrayList types = (ArrayList) $1;
2705 * replaces all the productions for isolating the various
2706 * simple types, but we need this to reuse it easily in local_variable_type
2709 : OBJECT { $$ = TypeManager.system_object_expr; }
2710 | STRING { $$ = TypeManager.system_string_expr; }
2711 | BOOL { $$ = TypeManager.system_boolean_expr; }
2712 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2713 | FLOAT { $$ = TypeManager.system_single_expr; }
2714 | DOUBLE { $$ = TypeManager.system_double_expr; }
2719 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2720 | BYTE { $$ = TypeManager.system_byte_expr; }
2721 | SHORT { $$ = TypeManager.system_int16_expr; }
2722 | USHORT { $$ = TypeManager.system_uint16_expr; }
2723 | INT { $$ = TypeManager.system_int32_expr; }
2724 | UINT { $$ = TypeManager.system_uint32_expr; }
2725 | LONG { $$ = TypeManager.system_int64_expr; }
2726 | ULONG { $$ = TypeManager.system_uint64_expr; }
2727 | CHAR { $$ = TypeManager.system_char_expr; }
2728 | VOID { $$ = TypeManager.system_void_expr; }
2732 : type rank_specifiers opt_nullable
2734 string rank_specifiers = (string) $2;
2736 rank_specifiers += "?";
2738 $$ = new ComposedCast ((Expression) $1, rank_specifiers, lexer.Location);
2743 // Expressions, section 7.5
2752 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2754 | parenthesized_expression
2755 | default_value_expression
2757 | invocation_expression
2761 | post_increment_expression
2762 | post_decrement_expression
2766 | checked_expression
2767 | unchecked_expression
2768 | pointer_member_access
2769 | anonymous_method_expression
2776 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2777 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2778 | NULL { $$ = NullLiteral.Null; }
2782 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2783 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2784 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2789 object v = lexer.Value;
2795 $$ = IntLiteral.Zero;
2797 $$ = IntLiteral.One;
2799 $$ = new IntLiteral (i);
2800 } else if (v is uint)
2801 $$ = new UIntLiteral ((UInt32) v);
2803 $$ = new LongLiteral ((Int64) v);
2804 else if (v is ulong)
2805 $$ = new ULongLiteral ((UInt64) v);
2807 Console.WriteLine ("OOPS. Unexpected result from scanner");
2812 : TRUE { $$ = new BoolLiteral (true); }
2813 | FALSE { $$ = new BoolLiteral (false); }
2816 parenthesized_expression_0
2817 : OPEN_PARENS expression CLOSE_PARENS
2820 lexer.Deambiguate_CloseParens ();
2821 // After this, the next token returned is one of
2822 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2823 // or CLOSE_PARENS_MINUS.
2827 parenthesized_expression
2828 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2832 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2834 // If a parenthesized expression is followed by a minus, we need to wrap
2835 // the expression inside a ParenthesizedExpression for the CS0075 check
2836 // in Binary.DoResolve().
2837 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2842 : primary_expression DOT IDENTIFIER opt_type_argument_list
2844 $$ = new MemberAccess ((Expression) $1, (string) $3,
2845 (TypeArguments) $4, lexer.Location);
2847 | predefined_type DOT IDENTIFIER opt_type_argument_list
2849 $$ = new MemberAccess ((Expression) $1, (string) $3,
2850 (TypeArguments) $4, lexer.Location);
2858 invocation_expression
2859 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2862 Location l = lexer.Location;
2863 Report.Error (1, l, "Parse error");
2865 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2867 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2869 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2871 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2873 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2875 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2877 ArrayList args = new ArrayList (1);
2879 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2881 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2883 ArrayList args = ((ArrayList) $4);
2885 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2890 : /* empty */ { $$ = null; }
2897 ArrayList list = new ArrayList (4);
2901 | argument_list COMMA argument
2903 ArrayList list = (ArrayList) $1;
2907 | argument_list error {
2908 CheckToken (1026, yyToken, ", or ) expected");
2915 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2917 | non_simple_argument
2924 : REF variable_reference
2926 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2928 | OUT variable_reference
2930 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2932 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2934 ArrayList list = (ArrayList) $3;
2935 Argument[] args = new Argument [list.Count];
2936 list.CopyTo (args, 0);
2938 Expression expr = new Arglist (args, lexer.Location);
2939 $$ = new Argument (expr, Argument.AType.Expression);
2943 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2948 : expression { note ("section 5.4"); $$ = $1; }
2952 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2954 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2956 | primary_expression rank_specifiers
2958 // So the super-trick is that primary_expression
2959 // can only be either a SimpleName or a MemberAccess.
2960 // The MemberAccess case arises when you have a fully qualified type-name like :
2962 // SimpleName is when you have
2965 Expression expr = (Expression) $1;
2966 if (expr is ComposedCast){
2967 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2968 } else if (!(expr is SimpleName || expr is MemberAccess || expr is ConstructedType)){
2969 Error_ExpectingTypeName (lexer.Location, expr);
2970 $$ = TypeManager.system_object_expr;
2973 // So we extract the string corresponding to the SimpleName
2976 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2984 ArrayList list = new ArrayList (4);
2988 | expression_list COMMA expression
2990 ArrayList list = (ArrayList) $1;
2999 $$ = new This (current_block, lexer.Location);
3004 : BASE DOT IDENTIFIER
3006 $$ = new BaseAccess ((string) $3, lexer.Location);
3008 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
3010 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
3013 Report.Error (175, lexer.Location, "Use of keyword `base' is not valid in this context");
3018 post_increment_expression
3019 : primary_expression OP_INC
3021 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
3022 (Expression) $1, lexer.Location);
3026 post_decrement_expression
3027 : primary_expression OP_DEC
3029 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
3030 (Expression) $1, lexer.Location);
3035 : object_or_delegate_creation_expression
3036 | array_creation_expression
3039 object_or_delegate_creation_expression
3040 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
3042 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
3046 array_creation_expression
3047 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
3049 opt_array_initializer
3051 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
3053 | NEW type rank_specifiers array_initializer
3055 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
3059 Report.Error (1031, lexer.Location, "Type expected");
3064 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
3079 opt_rank_specifier_or_nullable
3088 | opt_nullable rank_specifiers
3095 | opt_nullable rank_specifiers INTERR
3098 $$ = "?" + $2 + "?";
3105 : rank_specifier opt_rank_specifier
3107 $$ = (string) $2 + (string) $1;
3112 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3114 $$ = "[" + (string) $2 + "]";
3134 | dim_separators COMMA
3136 $$ = (string) $1 + ",";
3140 opt_array_initializer
3152 : OPEN_BRACE CLOSE_BRACE
3154 ArrayList list = new ArrayList (4);
3157 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3159 $$ = (ArrayList) $2;
3163 variable_initializer_list
3164 : variable_initializer
3166 ArrayList list = new ArrayList (4);
3170 | variable_initializer_list COMMA variable_initializer
3172 ArrayList list = (ArrayList) $1;
3178 void_pointer_expression
3179 : void_pointer_expression STAR
3181 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3185 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3190 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
3192 $$ = new TypeOfVoid (lexer.Location);
3194 | TYPEOF OPEN_PARENS void_pointer_expression CLOSE_PARENS
3196 $$ = new TypeOf ((Expression) $3, lexer.Location);
3198 | TYPEOF OPEN_PARENS
3200 lexer.TypeOfParsing = true;
3204 lexer.TypeOfParsing = false;
3205 $$ = new TypeOf ((Expression) $4, lexer.Location);
3210 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3211 $$ = new SizeOf ((Expression) $3, lexer.Location);
3216 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3218 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
3222 unchecked_expression
3223 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3225 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
3229 pointer_member_access
3230 : primary_expression OP_PTR IDENTIFIER
3234 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
3235 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
3239 anonymous_method_expression
3240 : DELEGATE opt_anonymous_method_signature _mark_
3242 oob_stack.Push (current_local_parameters);
3243 current_local_parameters = (Parameters)$2;
3245 // Force the next block to be created as a ToplevelBlock
3246 oob_stack.Push (current_block);
3247 oob_stack.Push (top_current_block);
3248 current_block = null;
3252 Location loc = (Location) $3;
3253 top_current_block = (Block) oob_stack.Pop ();
3254 current_block = (Block) oob_stack.Pop ();
3255 if (RootContext.Version == LanguageVersion.ISO_1){
3256 Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
3259 ToplevelBlock anon_block = (ToplevelBlock) $5;
3261 anon_block.Parent = current_block;
3262 $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block,
3265 current_local_parameters = (Parameters) oob_stack.Pop ();
3269 opt_anonymous_method_signature
3270 : /* empty */ { $$ = null; }
3271 | anonymous_method_signature
3274 anonymous_method_signature
3275 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3278 $$ = Parameters.EmptyReadOnlyParameters;
3280 ArrayList par_list = (ArrayList) $2;
3281 Parameter [] pars = new Parameter [par_list.Count];
3282 par_list.CopyTo (pars);
3283 $$ = new Parameters (pars, null, lexer.Location);
3288 opt_anonymous_method_parameter_list
3289 : /* empty */ { $$ = null; }
3290 | anonymous_method_parameter_list { $$ = $1; }
3293 anonymous_method_parameter_list
3294 : anonymous_method_parameter
3296 ArrayList a = new ArrayList (4);
3300 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3302 ArrayList a = (ArrayList) $1;
3308 anonymous_method_parameter
3309 : opt_parameter_modifier type IDENTIFIER {
3310 $$ = new Parameter ((Expression) $2, (string) $3, (Parameter.Modifier) $1, null);
3312 | PARAMS type IDENTIFIER {
3313 Report.Error (1670, lexer.Location, "params modifier not allowed in anonymous method declaration");
3318 default_value_expression
3319 : DEFAULT_OPEN_PARENS type CLOSE_PARENS
3321 $$ = new DefaultValueExpression ((Expression) $2, lexer.Location);
3326 : primary_expression
3327 | BANG prefixed_unary_expression
3329 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
3331 | TILDE prefixed_unary_expression
3333 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
3339 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3341 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3343 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3345 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3351 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3353 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3358 // The idea to split this out is from Rhys' grammar
3359 // to solve the problem with casts.
3361 prefixed_unary_expression
3363 | PLUS prefixed_unary_expression
3365 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
3367 | MINUS prefixed_unary_expression
3369 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
3371 | OP_INC prefixed_unary_expression
3373 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3374 (Expression) $2, lexer.Location);
3376 | OP_DEC prefixed_unary_expression
3378 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3379 (Expression) $2, lexer.Location);
3381 | STAR prefixed_unary_expression
3383 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
3385 | BITWISE_AND prefixed_unary_expression
3387 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
3391 pre_increment_expression
3392 : OP_INC prefixed_unary_expression
3394 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3395 (Expression) $2, lexer.Location);
3399 pre_decrement_expression
3400 : OP_DEC prefixed_unary_expression
3402 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3403 (Expression) $2, lexer.Location);
3407 multiplicative_expression
3408 : prefixed_unary_expression
3409 | multiplicative_expression STAR prefixed_unary_expression
3411 $$ = new Binary (Binary.Operator.Multiply,
3412 (Expression) $1, (Expression) $3, lexer.Location);
3414 | multiplicative_expression DIV prefixed_unary_expression
3416 $$ = new Binary (Binary.Operator.Division,
3417 (Expression) $1, (Expression) $3, lexer.Location);
3419 | multiplicative_expression PERCENT prefixed_unary_expression
3421 $$ = new Binary (Binary.Operator.Modulus,
3422 (Expression) $1, (Expression) $3, lexer.Location);
3427 : multiplicative_expression
3428 | additive_expression PLUS multiplicative_expression
3430 $$ = new Binary (Binary.Operator.Addition,
3431 (Expression) $1, (Expression) $3, lexer.Location);
3433 | additive_expression MINUS multiplicative_expression
3435 $$ = new Binary (Binary.Operator.Subtraction,
3436 (Expression) $1, (Expression) $3, lexer.Location);
3441 : additive_expression
3442 | shift_expression OP_SHIFT_LEFT additive_expression
3444 $$ = new Binary (Binary.Operator.LeftShift,
3445 (Expression) $1, (Expression) $3, lexer.Location);
3447 | shift_expression OP_SHIFT_RIGHT additive_expression
3449 $$ = new Binary (Binary.Operator.RightShift,
3450 (Expression) $1, (Expression) $3, lexer.Location);
3461 lexer.PutbackNullable ();
3466 nullable_type_or_conditional
3469 if (((bool) $2) && ($1 is ComposedCast))
3470 $$ = ((ComposedCast) $1).RemoveNullable ();
3476 relational_expression
3478 | relational_expression OP_LT shift_expression
3480 $$ = new Binary (Binary.Operator.LessThan,
3481 (Expression) $1, (Expression) $3, lexer.Location);
3483 | relational_expression OP_GT shift_expression
3485 $$ = new Binary (Binary.Operator.GreaterThan,
3486 (Expression) $1, (Expression) $3, lexer.Location);
3488 | relational_expression OP_LE shift_expression
3490 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3491 (Expression) $1, (Expression) $3, lexer.Location);
3493 | relational_expression OP_GE shift_expression
3495 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3496 (Expression) $1, (Expression) $3, lexer.Location);
3498 | relational_expression IS
3501 } nullable_type_or_conditional
3503 Report.Debug (64, "PARSE IS", $1, $1.GetType (), $4, $4.GetType (), lexer.Location);
3504 $$ = new Is ((Expression) $1, (Expression) $4, lexer.Location);
3506 | relational_expression AS
3509 } nullable_type_or_conditional
3511 $$ = new As ((Expression) $1, (Expression) $4, lexer.Location);
3516 : relational_expression
3517 | equality_expression OP_EQ relational_expression
3519 $$ = new Binary (Binary.Operator.Equality,
3520 (Expression) $1, (Expression) $3, lexer.Location);
3522 | equality_expression OP_NE relational_expression
3524 $$ = new Binary (Binary.Operator.Inequality,
3525 (Expression) $1, (Expression) $3, lexer.Location);
3530 : equality_expression
3531 | and_expression BITWISE_AND equality_expression
3533 $$ = new Binary (Binary.Operator.BitwiseAnd,
3534 (Expression) $1, (Expression) $3, lexer.Location);
3538 exclusive_or_expression
3540 | exclusive_or_expression CARRET and_expression
3542 $$ = new Binary (Binary.Operator.ExclusiveOr,
3543 (Expression) $1, (Expression) $3, lexer.Location);
3547 inclusive_or_expression
3548 : exclusive_or_expression
3549 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3551 $$ = new Binary (Binary.Operator.BitwiseOr,
3552 (Expression) $1, (Expression) $3, lexer.Location);
3556 conditional_and_expression
3557 : inclusive_or_expression
3558 | conditional_and_expression OP_AND inclusive_or_expression
3560 $$ = new Binary (Binary.Operator.LogicalAnd,
3561 (Expression) $1, (Expression) $3, lexer.Location);
3565 conditional_or_expression
3566 : conditional_and_expression
3567 | conditional_or_expression OP_OR conditional_and_expression
3569 $$ = new Binary (Binary.Operator.LogicalOr,
3570 (Expression) $1, (Expression) $3, lexer.Location);
3574 conditional_expression
3575 : conditional_or_expression
3576 | conditional_or_expression INTERR expression COLON expression
3578 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
3580 | conditional_or_expression INTERR INTERR expression
3582 $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $4, lexer.Location);
3584 // We'll be resolved into a `parenthesized_expression_0' later on.
3585 | conditional_or_expression INTERR CLOSE_PARENS
3587 $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
3588 lexer.PutbackCloseParens ();
3592 assignment_expression
3593 : prefixed_unary_expression ASSIGN expression
3595 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
3597 | prefixed_unary_expression OP_MULT_ASSIGN expression
3599 Location l = lexer.Location;
3601 $$ = new CompoundAssign (
3602 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
3604 | prefixed_unary_expression OP_DIV_ASSIGN expression
3606 Location l = lexer.Location;
3608 $$ = new CompoundAssign (
3609 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
3611 | prefixed_unary_expression OP_MOD_ASSIGN expression
3613 Location l = lexer.Location;
3615 $$ = new CompoundAssign (
3616 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
3618 | prefixed_unary_expression OP_ADD_ASSIGN expression
3620 Location l = lexer.Location;
3622 $$ = new CompoundAssign (
3623 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
3625 | prefixed_unary_expression OP_SUB_ASSIGN expression
3627 Location l = lexer.Location;
3629 $$ = new CompoundAssign (
3630 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
3632 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3634 Location l = lexer.Location;
3636 $$ = new CompoundAssign (
3637 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
3639 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3641 Location l = lexer.Location;
3643 $$ = new CompoundAssign (
3644 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
3646 | prefixed_unary_expression OP_AND_ASSIGN expression
3648 Location l = lexer.Location;
3650 $$ = new CompoundAssign (
3651 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
3653 | prefixed_unary_expression OP_OR_ASSIGN expression
3655 Location l = lexer.Location;
3657 $$ = new CompoundAssign (
3658 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
3660 | prefixed_unary_expression OP_XOR_ASSIGN expression
3662 Location l = lexer.Location;
3664 $$ = new CompoundAssign (
3665 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
3670 : conditional_expression
3671 | assignment_expression
3691 lexer.ConstraintsParsing = true;
3695 MemberName name = MakeName ((MemberName) $6);
3696 bool partial = (bool) $3;
3697 int mod_flags = (int) $2;
3700 ClassPart part = PartialContainer.CreatePart (
3701 current_namespace, current_class, name, mod_flags,
3702 (Attributes) $1, Kind.Class, lexer.Location);
3704 current_container = part.PartialContainer;
3705 current_class = part;
3707 if ((mod_flags & Modifiers.STATIC) != 0) {
3708 current_class = new StaticClass (
3709 current_namespace, current_class, name,
3710 mod_flags, (Attributes) $1, lexer.Location);
3712 current_class = new Class (
3713 current_namespace, current_class, name,
3714 mod_flags, (Attributes) $1, lexer.Location);
3717 current_container.AddClassOrStruct (current_class);
3718 current_container = current_class;
3719 RootContext.Tree.RecordDecl (name, current_class);
3723 opt_type_parameter_constraints_clauses
3725 lexer.ConstraintsParsing = false;
3728 if (current_class.Name == "System.Object") {
3729 Report.Error (537, current_class.Location,
3730 "The class System.Object cannot have a base " +
3731 "class or implement an interface.");
3733 current_class.Bases = (ArrayList) $8;
3736 current_class.SetParameterInfo ((ArrayList) $9);
3738 if (RootContext.Documentation != null) {
3739 current_class.DocComment = Lexer.consume_doc_comment ();
3740 Lexer.doc_state = XmlCommentState.Allowed;
3745 if (RootContext.Documentation != null)
3746 Lexer.doc_state = XmlCommentState.Allowed;
3750 $$ = pop_current_class ();
3756 { $$ = (bool) false; }
3758 { $$ = (bool) true; }
3762 : /* empty */ { $$ = (int) 0; }
3768 | modifiers modifier
3773 if ((m1 & m2) != 0) {
3774 Location l = lexer.Location;
3775 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3777 $$ = (int) (m1 | m2);
3782 : NEW { $$ = Modifiers.NEW; }
3783 | PUBLIC { $$ = Modifiers.PUBLIC; }
3784 | PROTECTED { $$ = Modifiers.PROTECTED; }
3785 | INTERNAL { $$ = Modifiers.INTERNAL; }
3786 | PRIVATE { $$ = Modifiers.PRIVATE; }
3787 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3788 | SEALED { $$ = Modifiers.SEALED; }
3789 | STATIC { $$ = Modifiers.STATIC; }
3790 | READONLY { $$ = Modifiers.READONLY; }
3791 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3792 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3793 | EXTERN { $$ = Modifiers.EXTERN; }
3794 | VOLATILE { $$ = Modifiers.VOLATILE; }
3795 | UNSAFE { $$ = Modifiers.UNSAFE; }
3799 : /* empty */ { $$ = null; }
3800 | class_base { $$ = $1; }
3804 : COLON type_list { $$ = $2; }
3807 opt_type_parameter_constraints_clauses
3808 : /* empty */ { $$ = null; }
3809 | type_parameter_constraints_clauses
3813 type_parameter_constraints_clauses
3814 : type_parameter_constraints_clause {
3815 ArrayList constraints = new ArrayList (1);
3816 constraints.Add ($1);
3819 | type_parameter_constraints_clauses type_parameter_constraints_clause {
3820 ArrayList constraints = (ArrayList) $1;
3822 constraints.Add ($2);
3827 type_parameter_constraints_clause
3828 : WHERE IDENTIFIER COLON type_parameter_constraints {
3829 $$ = new Constraints ((string) $2, (ArrayList) $4, lexer.Location);
3833 type_parameter_constraints
3834 : type_parameter_constraint {
3835 ArrayList constraints = new ArrayList (1);
3836 constraints.Add ($1);
3839 | type_parameter_constraints COMMA type_parameter_constraint {
3840 ArrayList constraints = (ArrayList) $1;
3842 constraints.Add ($3);
3847 type_parameter_constraint
3849 | NEW OPEN_PARENS CLOSE_PARENS {
3850 $$ = SpecialConstraint.Constructor;
3853 $$ = SpecialConstraint.ReferenceType;
3856 $$ = SpecialConstraint.ValueType;
3865 // A block is "contained" on the following places:
3867 // property_declaration as part of the accessor body (get/set)
3868 // operator_declaration
3869 // constructor_declaration
3870 // destructor_declaration
3871 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3876 if (current_block == null){
3877 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, lexer.Location);
3878 top_current_block = current_block;
3880 current_block = new Block (current_block, lexer.Location, Location.Null);
3883 opt_statement_list CLOSE_BRACE
3885 while (current_block.Implicit)
3886 current_block = current_block.Parent;
3888 current_block.SetEndLocation (lexer.Location);
3889 current_block = current_block.Parent;
3890 if (current_block == null)
3891 top_current_block = null;
3902 | statement_list statement
3906 : declaration_statement
3908 if ($1 != null && (Block) $1 != current_block){
3909 current_block.AddStatement ((Statement) $1);
3910 current_block = (Block) $1;
3913 | valid_declaration_statement
3915 current_block.AddStatement ((Statement) $1);
3920 valid_declaration_statement
3923 | expression_statement
3924 | selection_statement
3925 | iteration_statement
3929 | unchecked_statement
3937 : valid_declaration_statement
3938 | declaration_statement
3940 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3945 Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3953 $$ = EmptyStatement.Value;
3960 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3962 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3963 current_block.AddStatement (labeled);
3968 declaration_statement
3969 : local_variable_declaration SEMICOLON
3972 DictionaryEntry de = (DictionaryEntry) $1;
3974 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3978 | local_constant_declaration SEMICOLON
3981 DictionaryEntry de = (DictionaryEntry) $1;
3983 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3989 * The following is from Rhys' grammar:
3990 * > Types in local variable declarations must be recognized as
3991 * > expressions to prevent reduce/reduce errors in the grammar.
3992 * > The expressions are converted into types during semantic analysis.
3995 : primary_expression opt_rank_specifier_or_nullable
3997 // FIXME: Do something smart here regarding the composition of the type.
3999 // Ok, the above "primary_expression" is there to get rid of
4000 // both reduce/reduce and shift/reduces in the grammar, it should
4001 // really just be "type_name". If you use type_name, a reduce/reduce
4002 // creeps up. If you use namespace_or_type_name (which is all we need
4003 // really) two shift/reduces appear.
4006 // So the super-trick is that primary_expression
4007 // can only be either a SimpleName or a MemberAccess.
4008 // The MemberAccess case arises when you have a fully qualified type-name like :
4010 // SimpleName is when you have
4013 Expression expr = (Expression) $1;
4014 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType)) {
4015 Error_ExpectingTypeName (lexer.Location, expr);
4019 // So we extract the string corresponding to the SimpleName
4023 if ((string) $2 == "")
4026 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
4029 | builtin_types opt_rank_specifier_or_nullable
4031 if ((string) $2 == "")
4034 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
4038 local_variable_pointer_type
4039 : primary_expression STAR
4041 Expression expr = (Expression) $1;
4042 Location l = lexer.Location;
4044 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType)) {
4045 Error_ExpectingTypeName (l, expr);
4049 $$ = new ComposedCast ((Expression) $1, "*", l);
4051 | builtin_types STAR
4053 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
4057 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
4059 | local_variable_pointer_type STAR
4061 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
4065 local_variable_declaration
4066 : local_variable_type variable_declarators
4069 $$ = new DictionaryEntry ($1, $2);
4073 | local_variable_pointer_type opt_rank_specifier_or_nullable variable_declarators
4078 if ((string) $2 == "")
4079 t = (Expression) $1;
4081 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
4082 $$ = new DictionaryEntry (t, $3);
4088 local_constant_declaration
4089 : CONST local_variable_type constant_declarators
4092 $$ = new DictionaryEntry ($2, $3);
4098 expression_statement
4099 : statement_expression SEMICOLON
4106 // We have to do the wrapping here and not in the case above,
4107 // because statement_expression is used for example in for_statement
4109 statement_expression
4110 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4111 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4112 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4113 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4114 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4115 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4116 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
4118 Report.Error (1002, lexer.Location, "Expecting `;'");
4123 object_creation_expression
4124 : object_or_delegate_creation_expression
4125 { note ("complain if this is a delegate maybe?"); }
4134 : IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS
4137 Location l = (Location) $3;
4139 $$ = new If ((Expression) $4, (Statement) $6, l);
4141 if (RootContext.WarningLevel >= 4){
4142 if ($6 == EmptyStatement.Value)
4143 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4147 | IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS
4148 embedded_statement ELSE embedded_statement
4150 Location l = (Location) $3;
4152 $$ = new If ((Expression) $4, (Statement) $6, (Statement) $8, l);
4157 : SWITCH OPEN_PARENS _mark_
4159 switch_stack.Push (current_block);
4161 expression CLOSE_PARENS
4164 $$ = new Switch ((Expression) $5, (ArrayList) $7, (Location) $3);
4165 current_block = (Block) switch_stack.Pop ();
4181 Report.Error (1522, lexer.Location, "Empty switch block");
4189 ArrayList sections = new ArrayList (4);
4194 | switch_sections switch_section
4196 ArrayList sections = (ArrayList) $1;
4206 current_block = current_block.CreateSwitchBlock (lexer.Location);
4210 Block topmost = current_block;
4212 while (topmost.Implicit)
4213 topmost = topmost.Parent;
4214 $$ = new SwitchSection ((ArrayList) $1, topmost);
4221 ArrayList labels = new ArrayList (4);
4226 | switch_labels switch_label
4228 ArrayList labels = (ArrayList) ($1);
4236 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
4237 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
4240 1523, lexer.Location,
4241 "The keyword case or default must precede code in switch block");
4253 : WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS embedded_statement
4255 Location l = (Location) $3;
4256 $$ = new While ((Expression) $4, (Statement) $6, l);
4258 if (RootContext.WarningLevel >= 4){
4259 if ($6 == EmptyStatement.Value)
4260 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4266 : DO embedded_statement
4267 WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS SEMICOLON
4269 Location l = (Location) $5;
4271 $$ = new Do ((Statement) $2, (Expression) $6, l);
4277 opt_for_initializer SEMICOLON _mark_
4279 Block assign_block = new Block (current_block);
4280 current_block = assign_block;
4282 if ($3 is DictionaryEntry){
4283 DictionaryEntry de = (DictionaryEntry) $3;
4285 Expression type = (Expression) de.Key;
4286 ArrayList var_declarators = (ArrayList) de.Value;
4288 foreach (VariableDeclaration decl in var_declarators){
4292 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4296 Location l = lexer.Location;
4298 if (decl.expression_or_array_initializer is Expression){
4299 expr = (Expression) decl.expression_or_array_initializer;
4300 } else if (decl.expression_or_array_initializer == null) {
4303 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4304 expr = new ArrayCreation (type, "", init, decl.Location);
4307 LocalVariableReference var;
4308 var = new LocalVariableReference (assign_block, decl.identifier, l);
4311 Assign a = new Assign (var, expr, decl.Location);
4313 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4317 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4318 // This can be referred to as $6 below.
4324 opt_for_condition SEMICOLON
4325 opt_for_iterator CLOSE_PARENS
4328 Location l = (Location) $5;
4330 For f = new For ((Statement) $6, (Expression) $7, (Statement) $9, (Statement) $11, l);
4332 if (RootContext.WarningLevel >= 4){
4333 if ($11 == EmptyStatement.Value)
4334 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4337 current_block.AddStatement (f);
4338 while (current_block.Implicit)
4339 current_block = current_block.Parent;
4341 current_block = current_block.Parent;
4346 : /* empty */ { $$ = EmptyStatement.Value; }
4351 : local_variable_declaration
4352 | statement_expression_list
4356 : /* empty */ { $$ = null; }
4357 | boolean_expression
4361 : /* empty */ { $$ = EmptyStatement.Value; }
4366 : statement_expression_list
4369 statement_expression_list
4370 : statement_expression
4372 // CHANGE: was `null'
4373 Block b = new Block (current_block, Block.Flags.Implicit);
4375 b.AddStatement ((Statement) $1);
4378 | statement_expression_list COMMA statement_expression
4380 Block b = (Block) $1;
4382 b.AddStatement ((Statement) $3);
4388 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4390 Report.Error (230, lexer.Location, "Type and identifier are both required in a foreach statement");
4393 | FOREACH OPEN_PARENS type IDENTIFIER IN _mark_
4394 expression CLOSE_PARENS
4396 Block foreach_block = new Block (current_block);
4397 current_block = foreach_block;
4399 Location l = lexer.Location;
4402 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, l);
4404 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4406 // Get a writable reference to this read-only variable.
4408 // Note that the $$ here refers to the value of _this_ code block,
4409 // not the value of the LHS non-terminal. This can be referred to as $9 below.
4410 $$ = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
4417 LocalVariableReference v = (LocalVariableReference) $9;
4418 Location l = (Location) $6;
4421 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
4422 current_block.AddStatement (f);
4425 while (current_block.Implicit)
4426 current_block = current_block.Parent;
4428 current_block = current_block.Parent;
4434 | continue_statement
4444 $$ = new Break (lexer.Location);
4449 : CONTINUE SEMICOLON
4451 $$ = new Continue (lexer.Location);
4456 : GOTO IDENTIFIER SEMICOLON
4458 $$ = new Goto ((string) $2, lexer.Location);
4460 | GOTO CASE constant_expression SEMICOLON
4462 $$ = new GotoCase ((Expression) $3, lexer.Location);
4464 | GOTO DEFAULT SEMICOLON
4466 $$ = new GotoDefault (lexer.Location);
4471 : RETURN opt_expression SEMICOLON
4473 $$ = new Return ((Expression) $2, lexer.Location);
4478 : THROW opt_expression SEMICOLON
4480 $$ = new Throw ((Expression) $2, lexer.Location);
4485 : IDENTIFIER RETURN expression SEMICOLON
4487 string s = (string) $1;
4489 Report.Error (1003, lexer.Location, "; expected");
4492 if (RootContext.Version == LanguageVersion.ISO_1){
4493 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4496 if (iterator_container == null){
4497 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4500 iterator_container.SetYields ();
4501 $$ = new Yield ((Expression) $3, lexer.Location);
4504 | IDENTIFIER RETURN SEMICOLON
4506 Report.Error (1627, lexer.Location, "Expression expected after yield return");
4509 | IDENTIFIER BREAK SEMICOLON
4511 string s = (string) $1;
4513 Report.Error (1003, lexer.Location, "; expected");
4516 if (RootContext.Version == LanguageVersion.ISO_1){
4517 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4520 if (iterator_container == null){
4521 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4524 iterator_container.SetYields ();
4525 $$ = new YieldBreak (lexer.Location);
4536 : TRY block catch_clauses
4540 ArrayList c = (ArrayList)$3;
4541 for (int i = 0; i < c.Count; ++i) {
4542 Catch cc = (Catch) c [i];
4544 if (i != c.Count - 1)
4545 Report.Error (1017, cc.loc, "Empty catch block must be the last in a series of catch blocks");
4552 // Now s contains the list of specific catch clauses
4553 // and g contains the general one.
4555 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4557 | TRY block opt_catch_clauses FINALLY block
4560 ArrayList s = new ArrayList (4);
4561 ArrayList catch_list = (ArrayList) $3;
4563 if (catch_list != null){
4564 foreach (Catch cc in catch_list) {
4572 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4576 Report.Error (1524, lexer.Location, "Expected catch or finally");
4581 : /* empty */ { $$ = null; }
4588 ArrayList l = new ArrayList (4);
4593 | catch_clauses catch_clause
4595 ArrayList l = (ArrayList) $1;
4603 : /* empty */ { $$ = null; }
4608 : CATCH opt_catch_args
4610 Expression type = null;
4614 DictionaryEntry cc = (DictionaryEntry) $2;
4615 type = (Expression) cc.Key;
4616 id = (string) cc.Value;
4619 ArrayList one = new ArrayList (4);
4620 Location loc = lexer.Location;
4622 one.Add (new VariableDeclaration (id, null, loc));
4624 current_block = new Block (current_block);
4625 Block b = declare_local_variables (type, one, loc);
4630 Expression type = null;
4634 DictionaryEntry cc = (DictionaryEntry) $2;
4635 type = (Expression) cc.Key;
4636 id = (string) cc.Value;
4639 while (current_block.Implicit)
4640 current_block = current_block.Parent;
4641 current_block = current_block.Parent;
4645 $$ = new Catch (type, id, (Block) $4, ((Block) $4).loc);
4650 : /* empty */ { $$ = null; }
4655 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4657 $$ = new DictionaryEntry ($2, $3);
4664 $$ = new Checked ((Block) $2);
4671 $$ = new Unchecked ((Block) $2);
4678 if (!RootContext.Unsafe){
4679 Report.Error (227, lexer.Location,
4680 "Unsafe code can only be used if -unsafe is used");
4683 $$ = new Unsafe ((Block) $3);
4689 type fixed_pointer_declarators
4692 ArrayList list = (ArrayList) $4;
4693 Expression type = (Expression) $3;
4694 Location l = lexer.Location;
4695 int top = list.Count;
4697 Block assign_block = new Block (current_block);
4698 current_block = assign_block;
4700 for (int i = 0; i < top; i++){
4701 Pair p = (Pair) list [i];
4704 v = current_block.AddVariable (type, (string) p.First, l);
4708 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4716 Location l = (Location) $6;
4718 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $8, l);
4720 if (RootContext.WarningLevel >= 4){
4721 if ($8 == EmptyStatement.Value)
4722 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4725 current_block.AddStatement (f);
4726 while (current_block.Implicit)
4727 current_block = current_block.Parent;
4729 current_block = current_block.Parent;
4733 fixed_pointer_declarators
4734 : fixed_pointer_declarator {
4735 ArrayList declarators = new ArrayList (4);
4737 declarators.Add ($1);
4740 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4742 ArrayList declarators = (ArrayList) $1;
4744 declarators.Add ($3);
4749 fixed_pointer_declarator
4750 : IDENTIFIER ASSIGN expression
4752 $$ = new Pair ($1, $3);
4756 Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
4762 : LOCK OPEN_PARENS expression CLOSE_PARENS
4768 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4773 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS _mark_
4775 Block assign_block = new Block (current_block);
4776 current_block = assign_block;
4778 if ($3 is DictionaryEntry){
4779 DictionaryEntry de = (DictionaryEntry) $3;
4780 Location l = lexer.Location;
4782 Expression type = (Expression) de.Key;
4783 ArrayList var_declarators = (ArrayList) de.Value;
4785 ArrayList vars = new ArrayList (4);
4787 foreach (VariableDeclaration decl in var_declarators){
4789 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4792 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
4795 if (decl.expression_or_array_initializer is Expression){
4796 expr = (Expression) decl.expression_or_array_initializer;
4798 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4800 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4803 expr = new ArrayCreation (type, "", init, decl.Location);
4806 LocalVariableReference var;
4808 // Get a writable reference to this read-only variable.
4809 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4811 // This is so that it is not a warning on using variables
4814 vars.Add (new DictionaryEntry (var, expr));
4816 // Assign a = new Assign (var, expr, decl.Location);
4817 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4820 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4821 // It can be referred to as $6 below.
4822 $$ = new DictionaryEntry (type, vars);
4829 Using u = new Using ($6, (Statement) $7, (Location) $5);
4830 current_block.AddStatement (u);
4831 while (current_block.Implicit)
4832 current_block = current_block.Parent;
4834 current_block = current_block.Parent;
4838 resource_acquisition
4839 : local_variable_declaration
4843 // Utility rule to save location information
4846 { $$ = lexer.Location; }
4851 // A class used to pass around variable declarations and constants
4853 public class VariableDeclaration {
4854 public string identifier;
4855 public object expression_or_array_initializer;
4856 public Location Location;
4857 public Attributes OptAttributes;
4858 public string DocComment;
4860 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4862 this.identifier = id;
4863 this.expression_or_array_initializer = eoai;
4865 this.OptAttributes = opt_attrs;
4868 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4874 /// Used to pass around interface property information
4876 public class InterfaceAccessorInfo {
4878 public readonly Accessor Get, Set;
4880 public InterfaceAccessorInfo (bool has_get, bool has_set,
4881 Attributes get_attrs, Attributes set_attrs, int get_mod, int set_mod, Location get_loc, Location set_loc)
4884 Report.Error (275, get_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4886 Report.Error (275, set_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4889 Get = new Accessor (null, 0, get_attrs, get_loc);
4891 Set = new Accessor (null, 0, set_attrs, set_loc);
4898 // A class used to hold info about an indexer declarator
4900 public class IndexerDeclaration {
4901 public Expression type;
4902 public MemberName interface_type;
4903 public Parameters param_list;
4905 public IndexerDeclaration (Expression type, MemberName interface_type,
4906 Parameters param_list)
4909 this.interface_type = interface_type;
4910 this.param_list = param_list;
4915 // We use this when we do not have an object in advance that is an IIteratorContainer
4917 public class SimpleIteratorContainer : IIteratorContainer {
4920 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4925 public static SimpleIteratorContainer GetSimple () {
4926 Simple.Yields = false;
4930 public void SetYields () { Yields = true; }
4934 // A class used to hold info about an operator declarator
4936 public class OperatorDeclaration {
4937 public Operator.OpType optype;
4938 public Expression ret_type, arg1type, arg2type;
4939 public string arg1name, arg2name;
4940 public Location location;
4942 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4943 Expression arg1type, string arg1name,
4944 Expression arg2type, string arg2name, Location location)
4947 this.ret_type = ret_type;
4948 this.arg1type = arg1type;
4949 this.arg1name = arg1name;
4950 this.arg2type = arg2type;
4951 this.arg2name = arg2name;
4952 this.location = location;
4957 void Error_ExpectingTypeName (Location l, Expression expr)
4959 if (expr is Invocation){
4960 Report.Error (1002, l, "; expected");
4962 Report.Error (-1, l, "Invalid Type definition");
4966 TypeContainer pop_current_class ()
4968 TypeContainer retval = current_class;
4970 current_class = current_class.Parent;
4971 current_container = current_container.Parent;
4973 if (current_class != current_container) {
4974 if (!(current_class is ClassPart) ||
4975 ((ClassPart) current_class).PartialContainer != current_container)
4976 throw new InternalErrorException ();
4977 } else if (current_container is ClassPart)
4978 current_container = ((ClassPart) current_class).PartialContainer;
4984 // Given the @class_name name, it creates a fully qualified name
4985 // based on the containing declaration space
4988 MakeName (MemberName class_name)
4990 Namespace ns = current_namespace.NS;
4992 if (current_container.Name == ""){
4994 return new MemberName (ns.MemberName, class_name);
4998 return new MemberName (current_container.MemberName, class_name);
5002 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
5004 Block implicit_block;
5005 ArrayList inits = null;
5008 // We use the `Used' property to check whether statements
5009 // have been added to the current block. If so, we need
5010 // to create another block to contain the new declaration
5011 // otherwise, as an optimization, we use the same block to
5012 // add the declaration.
5014 // FIXME: A further optimization is to check if the statements
5015 // that were added were added as part of the initialization
5016 // below. In which case, no other statements have been executed
5017 // and we might be able to reduce the number of blocks for
5018 // situations like this:
5020 // int j = 1; int k = j + 1;
5022 if (current_block.Used)
5023 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
5025 implicit_block = current_block;
5027 foreach (VariableDeclaration decl in variable_declarators){
5029 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
5030 if (decl.expression_or_array_initializer != null){
5032 inits = new ArrayList (4);
5039 return implicit_block;
5041 foreach (VariableDeclaration decl in inits){
5045 if (decl.expression_or_array_initializer is Expression){
5046 expr = (Expression) decl.expression_or_array_initializer;
5049 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
5051 expr = new ArrayCreation (type, "", init, decl.Location);
5054 LocalVariableReference var;
5055 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
5057 assign = new Assign (var, expr, decl.Location);
5059 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
5062 return implicit_block;
5065 Block declare_local_constants (Expression type, ArrayList declarators)
5067 Block implicit_block;
5069 if (current_block.Used)
5070 implicit_block = new Block (current_block, Block.Flags.Implicit);
5072 implicit_block = current_block;
5074 foreach (VariableDeclaration decl in declarators){
5075 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
5078 return implicit_block;
5081 void CheckAttributeTarget (string a)
5085 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
5089 Location l = lexer.Location;
5090 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
5096 void CheckUnaryOperator (Operator.OpType op)
5100 case Operator.OpType.LogicalNot:
5101 case Operator.OpType.OnesComplement:
5102 case Operator.OpType.Increment:
5103 case Operator.OpType.Decrement:
5104 case Operator.OpType.True:
5105 case Operator.OpType.False:
5106 case Operator.OpType.Addition:
5107 case Operator.OpType.Subtraction:
5112 Location l = lexer.Location;
5113 Report.Error (1019, l, "Overloadable unary operator expected");
5119 void CheckBinaryOperator (Operator.OpType op)
5123 case Operator.OpType.Addition:
5124 case Operator.OpType.Subtraction:
5125 case Operator.OpType.Multiply:
5126 case Operator.OpType.Division:
5127 case Operator.OpType.Modulus:
5128 case Operator.OpType.BitwiseAnd:
5129 case Operator.OpType.BitwiseOr:
5130 case Operator.OpType.ExclusiveOr:
5131 case Operator.OpType.LeftShift:
5132 case Operator.OpType.RightShift:
5133 case Operator.OpType.Equality:
5134 case Operator.OpType.Inequality:
5135 case Operator.OpType.GreaterThan:
5136 case Operator.OpType.LessThan:
5137 case Operator.OpType.GreaterThanOrEqual:
5138 case Operator.OpType.LessThanOrEqual:
5142 Location l = lexer.Location;
5143 Report.Error (1020, l, "Overloadable binary operator expected");
5149 void syntax_error (Location l, string msg)
5151 Report.Error (1003, l, "Syntax error, " + msg);
5154 void note (string s)
5156 // Used to put annotations
5161 public Tokenizer Lexer {
5167 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
5169 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
5170 this.name = file.Name;
5172 current_container = RootContext.Tree.Types;
5173 // TODO: Make RootContext.Tree.Types a PartialContainer.
5174 current_class = current_container;
5175 current_container.NamespaceEntry = current_namespace;
5176 oob_stack = new Stack ();
5177 switch_stack = new Stack ();
5179 lexer = new Tokenizer (reader, file, defines);
5182 public void parse ()
5185 if (yacc_verbose_flag > 1)
5186 yyparse (lexer, new yydebug.yyDebugSimple ());
5189 Tokenizer tokenizer = lexer as Tokenizer;
5190 tokenizer.cleanup ();
5191 } catch (Exception e){
5193 // Removed for production use, use parser verbose to get the output.
5195 // Console.WriteLine (e);
5196 Report.Error (-25, lexer.Location, "Parsing error");
5197 if (yacc_verbose_flag > 0)
5198 Console.WriteLine (e);
5201 RootContext.Tree.Types.NamespaceEntry = null;
5204 void CheckToken (int error, int yyToken, string msg)
5206 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
5207 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
5210 Report.Error (error, lexer.Location, msg);
5213 void CheckIdentifierToken (int yyToken)
5215 CheckToken (1041, yyToken, "Identifier expected");
5218 string ConsumeStoredComment ()
5220 string s = tmpComment;
5222 Lexer.doc_state = XmlCommentState.Allowed;