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;
97 /// Current attribute target
98 string current_attr_target;
100 /// assembly and module attribute definition is enabled
101 bool global_attrs_enabled = true;
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 ((string) $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(1518, Lexer.Location, "Attributes cannot be applied to namespaces."
367 + " Expected class, delegate, enum, interface, or struct");
370 MemberName name = (MemberName) $3;
372 if (name.TypeArguments != null)
373 syntax_error (lexer.Location, "namespace name expected");
374 else if ((current_namespace.Parent != null) && (name.Left != null)) {
375 Report.Error (134, lexer.Location,
376 "Cannot use qualified namespace names in nested " +
377 "namespace declarations");
380 current_namespace = new NamespaceEntry (
381 current_namespace, file, name.GetName (), lexer.Location);
383 namespace_body opt_semicolon
385 current_namespace = current_namespace.Parent;
400 : namespace_or_type_name {
401 MemberName name = (MemberName) $1;
403 if (name.TypeArguments != null)
404 syntax_error (lexer.Location, "namespace name expected");
406 $$ = name.GetName ();
413 if (RootContext.Documentation != null)
414 Lexer.doc_state = XmlCommentState.Allowed;
417 opt_namespace_member_declarations
428 opt_namespace_member_declarations
430 | namespace_member_declarations
433 namespace_member_declarations
434 : namespace_member_declaration
435 | namespace_member_declarations namespace_member_declaration
438 namespace_member_declaration
445 Class c = (Class) $1;
446 mod_flags = c.ModFlags;
448 } else if ($1 is Struct){
449 Struct s = (Struct) $1;
450 mod_flags = s.ModFlags;
455 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
457 1527, lexer.Location,
458 "Namespace elements cant be explicitly " +
459 "declared private or protected in `" + name + "'");
461 current_namespace.DeclarationFound = true;
463 | namespace_declaration {
464 current_namespace.DeclarationFound = true;
467 | field_declaration {
468 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
470 | method_declaration {
471 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
478 | interface_declaration
480 | delegate_declaration
482 // Enable this when we have handled all errors, because this acts as a generic fallback
485 // Console.WriteLine ("Token=" + yyToken);
486 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
498 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
506 global_attrs_enabled = false;
511 global_attrs_enabled = false;
520 ArrayList sect = (ArrayList) $1;
522 if (global_attrs_enabled) {
523 if (current_attr_target == "module") {
524 CodeGen.Module.AddAttributes (sect);
526 } else if (current_attr_target == "assembly") {
527 CodeGen.Assembly.AddAttributes (sect);
530 $$ = new Attributes (sect);
533 if (RootContext.Documentation != null) {
534 Lexer.check_incorrect_doc_comment ();
536 XmlCommentState.Allowed;
540 $$ = new Attributes (sect);
542 current_attr_target = null;
544 | attribute_sections attribute_section
546 Attributes attrs = $1 as Attributes;
547 ArrayList sect = (ArrayList) $2;
549 if (global_attrs_enabled) {
550 if (current_attr_target == "module") {
551 CodeGen.Module.AddAttributes (sect);
553 } else if (current_attr_target == "assembly") {
554 CodeGen.Assembly.AddAttributes (sect);
558 attrs = new Attributes (sect);
560 attrs.AddAttributes (sect);
564 attrs = new Attributes (sect);
566 attrs.AddAttributes (sect);
569 current_attr_target = null;
574 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
578 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
584 attribute_target_specifier
585 : attribute_target COLON
587 current_attr_target = (string)$1;
595 CheckAttributeTarget ((string) $1);
598 | EVENT { $$ = "event"; }
599 | RETURN { $$ = "return"; }
605 ArrayList attrs = new ArrayList (4);
611 | attribute_list COMMA attribute
613 ArrayList attrs = (ArrayList) $1;
625 opt_attribute_arguments
627 MemberName mname = (MemberName) $1;
628 if (mname.IsGeneric) {
629 Report.Error (404, lexer.Location,
630 "'<' unexpected: attributes cannot be generic");
633 string name = mname.GetName ();
634 if (current_attr_target == "assembly" || current_attr_target == "module")
635 $$ = new GlobalAttribute (current_container, current_attr_target,
636 name, (ArrayList) $3, (Location) $2);
638 $$ = new Attribute (current_attr_target, name, (ArrayList) $3,
644 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
647 opt_attribute_arguments
648 : /* empty */ { $$ = null; }
649 | OPEN_PARENS attribute_arguments CLOSE_PARENS
657 : opt_positional_argument_list
662 ArrayList args = new ArrayList (4);
668 | positional_argument_list COMMA named_argument_list
670 ArrayList args = new ArrayList (4);
676 | named_argument_list
678 ArrayList args = new ArrayList (4);
687 opt_positional_argument_list
688 : /* empty */ { $$ = null; }
689 | positional_argument_list
692 positional_argument_list
695 ArrayList args = new ArrayList (4);
696 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
700 | positional_argument_list COMMA expression
702 ArrayList args = (ArrayList) $1;
703 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
712 ArrayList args = new ArrayList (4);
717 | named_argument_list COMMA named_argument
719 ArrayList args = (ArrayList) $1;
724 | named_argument_list COMMA expression
726 Report.Error (1016, lexer.Location, "Named attribute argument expected");
732 : IDENTIFIER ASSIGN expression
734 $$ = new DictionaryEntry (
736 new Argument ((Expression) $3, Argument.AType.Expression));
742 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
745 opt_class_member_declarations
747 | class_member_declarations
750 class_member_declarations
751 : class_member_declaration
752 | class_member_declarations
753 class_member_declaration
756 class_member_declaration
757 : constant_declaration // done
758 | field_declaration // done
759 | method_declaration // done
760 | property_declaration // done
761 | event_declaration // done
762 | indexer_declaration // done
763 | operator_declaration // done
764 | constructor_declaration // done
765 | destructor_declaration // done
775 MemberName name = MakeName ((MemberName) $5);
776 bool partial = (bool) $3;
779 ClassPart part = PartialContainer.CreatePart (
780 current_namespace, current_container, name, (int) $2,
781 (Attributes) $1, Kind.Struct, lexer.Location);
783 current_container = part.PartialContainer;
784 current_class = part;
786 current_class = new Struct (
787 current_namespace, current_container, name, (int) $2,
788 (Attributes) $1, lexer.Location);
790 current_container = current_class;
791 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
794 lexer.ConstraintsParsing = true;
797 opt_type_parameter_constraints_clauses
799 lexer.ConstraintsParsing = false;
802 current_class.Bases = (ArrayList) $7;
804 current_class.SetParameterInfo ((ArrayList) $8);
806 if (RootContext.Documentation != null)
807 current_class.DocComment = Lexer.consume_doc_comment ();
809 current_class.Register ();
813 if (RootContext.Documentation != null)
814 Lexer.doc_state = XmlCommentState.Allowed;
820 current_container = current_container.Parent;
821 current_class = current_container;
823 | opt_attributes opt_modifiers opt_partial STRUCT error {
824 CheckIdentifierToken (yyToken);
831 if (RootContext.Documentation != null)
832 Lexer.doc_state = XmlCommentState.Allowed;
834 opt_struct_member_declarations CLOSE_BRACE
837 opt_struct_member_declarations
839 | struct_member_declarations
842 struct_member_declarations
843 : struct_member_declaration
844 | struct_member_declarations struct_member_declaration
847 struct_member_declaration
848 : constant_declaration
851 | property_declaration
853 | indexer_declaration
854 | operator_declaration
855 | constructor_declaration
859 * This is only included so we can flag error 575:
860 * destructors only allowed on class types
862 | destructor_declaration
873 int modflags = (int) $2;
874 foreach (VariableDeclaration constant in (ArrayList) $5){
875 Location l = constant.Location;
876 if ((modflags & Modifiers.STATIC) != 0) {
877 Report.Error (504, l, "The constant '{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
881 Const c = new Const (
882 current_class, (Expression) $4, (string) constant.identifier,
883 (Expression) constant.expression_or_array_initializer, modflags,
886 if (RootContext.Documentation != null) {
887 c.DocComment = Lexer.consume_doc_comment ();
888 Lexer.doc_state = XmlCommentState.Allowed;
890 current_container.AddConstant (c);
896 : constant_declarator
898 ArrayList constants = new ArrayList (4);
903 | constant_declarators COMMA constant_declarator
906 ArrayList constants = (ArrayList) $1;
913 : IDENTIFIER ASSIGN constant_expression
915 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
919 // A const field requires a value to be provided
920 Report.Error (145, lexer.Location, "A const field requires a value to be provided");
932 Expression type = (Expression) $3;
935 foreach (VariableDeclaration var in (ArrayList) $4){
936 Location l = var.Location;
938 Field field = new Field (current_class, type, mod, var.identifier,
939 var.expression_or_array_initializer,
942 if (RootContext.Documentation != null) {
943 field.DocComment = Lexer.consume_doc_comment ();
944 Lexer.doc_state = XmlCommentState.Allowed;
946 current_container.AddField (field);
954 Report.Error (670, lexer.Location, "void type is not allowed for fields");
959 : variable_declarator
961 ArrayList decl = new ArrayList (4);
965 | variable_declarators COMMA variable_declarator
967 ArrayList decls = (ArrayList) $1;
974 : IDENTIFIER ASSIGN variable_initializer
976 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
980 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
993 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
995 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
999 Report.Error (1575, lexer.Location, "A stackalloc expression requires [] after type");
1006 iterator_container = (IIteratorContainer) $1;
1007 if (RootContext.Documentation != null)
1008 Lexer.doc_state = XmlCommentState.NotAllowed;
1012 Method method = (Method) $1;
1013 Block b = (Block) $3;
1014 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
1017 if ((method.ModFlags & extern_abstract) == 0){
1019 501, lexer.Location, current_container.MakeName (method.Name) +
1020 "must declare a body because it is not marked abstract or extern");
1023 if ((method.ModFlags & Modifiers.EXTERN) != 0){
1025 179, lexer.Location, current_container.MakeName (method.Name) +
1026 " is declared extern, but has a body");
1030 method.Block = (ToplevelBlock) $3;
1031 current_container.AddMethod (method);
1033 current_local_parameters = null;
1034 iterator_container = null;
1036 if (RootContext.Documentation != null)
1037 Lexer.doc_state = XmlCommentState.Allowed;
1051 1585, lexer.Location, "Member modifier `" +
1052 Modifiers.Name (i) + "' must precede member type and name");
1063 type namespace_or_type_name
1064 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1066 lexer.ConstraintsParsing = true;
1068 opt_type_parameter_constraints_clauses
1070 lexer.ConstraintsParsing = false;
1072 MemberName name = (MemberName) $4;
1074 if ($9 != null && name.TypeArguments == null)
1075 Report.Error (80, lexer.Location,
1076 "Contraints are not allowed on non-generic declarations");
1080 GenericMethod generic = null;
1081 if (name.TypeArguments != null) {
1082 generic = new GenericMethod (current_namespace, current_class,
1083 name, lexer.Location);
1085 generic.SetParameterInfo ((ArrayList) $9);
1088 method = new Method (current_class, generic, (Expression) $3, (int) $2, false,
1089 name, (Parameters) $6, (Attributes) $1, lexer.Location);
1091 current_local_parameters = (Parameters) $6;
1093 if (RootContext.Documentation != null)
1094 method.DocComment = Lexer.consume_doc_comment ();
1100 VOID namespace_or_type_name
1101 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1103 lexer.ConstraintsParsing = true;
1105 opt_type_parameter_constraints_clauses
1107 lexer.ConstraintsParsing = false;
1109 MemberName name = (MemberName) $4;
1111 if ($9 != null && name.TypeArguments == null)
1112 Report.Error (80, lexer.Location,
1113 "Contraints are not allowed on non-generic declarations");
1116 GenericMethod generic = null;
1117 if (name.TypeArguments != null) {
1118 generic = new GenericMethod (current_namespace, current_class,
1119 name, lexer.Location);
1121 generic.SetParameterInfo ((ArrayList) $9);
1124 method = new Method (current_class, generic, TypeManager.system_void_expr,
1125 (int) $2, false, name, (Parameters) $6, (Attributes) $1,
1128 current_local_parameters = (Parameters) $6;
1130 if (RootContext.Documentation != null)
1131 method.DocComment = Lexer.consume_doc_comment ();
1138 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1140 Report.Error (1585, lexer.Location,
1141 String.Format ("Modifier {0} should appear before type",
1142 Modifiers.Name ((int) $4)));
1143 MemberName name = (MemberName) $4;
1145 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1146 0, false, name, (Parameters) $6, (Attributes) $1,
1149 current_local_parameters = (Parameters) $6;
1151 if (RootContext.Documentation != null)
1152 method.DocComment = Lexer.consume_doc_comment ();
1160 | SEMICOLON { $$ = null; }
1163 opt_formal_parameter_list
1164 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1165 | formal_parameter_list
1168 formal_parameter_list
1171 ArrayList pars_list = (ArrayList) $1;
1173 Parameter [] pars = new Parameter [pars_list.Count];
1174 pars_list.CopyTo (pars);
1176 $$ = new Parameters (pars, null, lexer.Location);
1178 | fixed_parameters COMMA parameter_array
1180 ArrayList pars_list = (ArrayList) $1;
1182 Parameter [] pars = new Parameter [pars_list.Count];
1183 pars_list.CopyTo (pars);
1185 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
1187 | fixed_parameters COMMA ARGLIST
1189 ArrayList pars_list = (ArrayList) $1;
1191 Parameter [] pars = new Parameter [pars_list.Count];
1192 pars_list.CopyTo (pars);
1194 $$ = new Parameters (pars, true, lexer.Location);
1198 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
1202 $$ = new Parameters (null, true, lexer.Location);
1209 ArrayList pars = new ArrayList (4);
1214 | fixed_parameters COMMA fixed_parameter
1216 ArrayList pars = (ArrayList) $1;
1225 opt_parameter_modifier
1229 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
1232 opt_parameter_modifier
1235 Report.Error (1001, lexer.Location, "Identifier expected");
1239 opt_parameter_modifier
1242 CheckIdentifierToken (yyToken);
1246 opt_parameter_modifier
1252 Report.Error (241, lexer.Location, "Default parameter specifiers are not permitted");
1257 opt_parameter_modifier
1258 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1259 | parameter_modifier
1263 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1264 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1268 : opt_attributes PARAMS type IDENTIFIER
1270 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1271 note ("type must be a single-dimension array type");
1273 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1275 Report.Error (1611, lexer.Location, "The params parameter cannot be declared as ref or out");
1278 | opt_attributes PARAMS type error {
1279 CheckIdentifierToken (yyToken);
1284 property_declaration
1288 namespace_or_type_name
1290 if (RootContext.Documentation != null)
1291 tmpComment = Lexer.consume_doc_comment ();
1295 implicit_value_parameter_type = (Expression) $3;
1297 lexer.PropertyParsing = true;
1299 $$ = lexer.Location;
1301 iterator_container = SimpleIteratorContainer.GetSimple ();
1303 accessor_declarations
1305 lexer.PropertyParsing = false;
1310 Pair pair = (Pair) $8;
1311 Accessor get_block = (Accessor) pair.First;
1312 Accessor set_block = (Accessor) pair.Second;
1314 Location loc = (Location) $7;
1315 MemberName name = (MemberName) $4;
1317 if (name.TypeArguments != null)
1318 syntax_error (lexer.Location, "a property can't have type arguments");
1320 prop = new Property (current_container, (Expression) $3, (int) $2, false,
1321 name, (Attributes) $1, get_block, set_block, loc);
1322 if (SimpleIteratorContainer.Simple.Yields)
1325 current_container.AddProperty (prop);
1326 implicit_value_parameter_type = null;
1327 iterator_container = null;
1329 if (RootContext.Documentation != null)
1330 prop.DocComment = ConsumeStoredComment ();
1335 accessor_declarations
1336 : get_accessor_declaration opt_set_accessor_declaration
1338 $$ = new Pair ($1, $2);
1340 | set_accessor_declaration opt_get_accessor_declaration
1342 $$ = new Pair ($2, $1);
1346 opt_get_accessor_declaration
1347 : /* empty */ { $$ = null; }
1348 | get_accessor_declaration
1351 opt_set_accessor_declaration
1352 : /* empty */ { $$ = null; }
1353 | set_accessor_declaration
1356 get_accessor_declaration
1357 : opt_attributes opt_modifiers GET
1359 // If this is not the case, then current_local_parameters has already
1360 // been set in indexer_declaration
1361 if (parsing_indexer == false)
1362 current_local_parameters = null;
1364 current_local_parameters = indexer_parameters;
1365 lexer.PropertyParsing = false;
1369 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1370 current_local_parameters = null;
1371 lexer.PropertyParsing = true;
1373 if (RootContext.Documentation != null)
1374 if (Lexer.doc_state == XmlCommentState.Error)
1375 Lexer.doc_state = XmlCommentState.NotAllowed;
1379 set_accessor_declaration
1380 : opt_attributes opt_modifiers SET
1383 Parameter implicit_value_parameter = new Parameter (
1384 implicit_value_parameter_type, "value",
1385 Parameter.Modifier.NONE, null);
1387 if (parsing_indexer == false) {
1388 args = new Parameter [1];
1389 args [0] = implicit_value_parameter;
1390 current_local_parameters = new Parameters (args, null, lexer.Location);
1392 Parameter [] fpars = indexer_parameters.FixedParameters;
1395 int count = fpars.Length;
1397 args = new Parameter [count + 1];
1398 fpars.CopyTo (args, 0);
1399 args [count] = implicit_value_parameter;
1402 current_local_parameters = new Parameters (
1403 args, indexer_parameters.ArrayParameter, lexer.Location);
1406 lexer.PropertyParsing = false;
1410 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1411 current_local_parameters = null;
1412 lexer.PropertyParsing = true;
1414 if (RootContext.Documentation != null
1415 && Lexer.doc_state == XmlCommentState.Error)
1416 Lexer.doc_state = XmlCommentState.NotAllowed;
1422 | SEMICOLON { $$ = null; }
1425 interface_declaration
1429 INTERFACE member_name
1431 MemberName name = MakeName ((MemberName) $5);
1432 bool partial = (bool) $3;
1435 ClassPart part = PartialContainer.CreatePart (
1436 current_namespace, current_container, name, (int) $2,
1437 (Attributes) $1, Kind.Interface, lexer.Location);
1439 current_container = part.PartialContainer;
1440 current_class = part;
1442 current_class = new Interface (
1443 current_namespace, current_container, name, (int) $2,
1444 (Attributes) $1, lexer.Location);
1446 current_container = current_class;
1447 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
1450 lexer.ConstraintsParsing = true;
1453 opt_type_parameter_constraints_clauses
1455 lexer.ConstraintsParsing = false;
1458 current_class.Bases = (ArrayList) $7;
1460 current_class.SetParameterInfo ((ArrayList) $8);
1462 if (RootContext.Documentation != null) {
1463 current_class.DocComment = Lexer.consume_doc_comment ();
1464 Lexer.doc_state = XmlCommentState.Allowed;
1467 current_class.Register ();
1471 if (RootContext.Documentation != null)
1472 Lexer.doc_state = XmlCommentState.Allowed;
1478 current_container = current_container.Parent;
1479 current_class = current_container;
1481 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1482 CheckIdentifierToken (yyToken);
1488 opt_interface_member_declarations
1492 opt_interface_member_declarations
1494 | interface_member_declarations
1497 interface_member_declarations
1498 : interface_member_declaration
1499 | interface_member_declarations interface_member_declaration
1502 interface_member_declaration
1503 : interface_method_declaration
1505 Method m = (Method) $1;
1507 current_container.AddMethod (m);
1509 if (RootContext.Documentation != null)
1510 Lexer.doc_state = XmlCommentState.Allowed;
1512 | interface_property_declaration
1514 Property p = (Property) $1;
1516 current_container.AddProperty (p);
1518 if (RootContext.Documentation != null)
1519 Lexer.doc_state = XmlCommentState.Allowed;
1521 | interface_event_declaration
1524 Event e = (Event) $1;
1525 current_container.AddEvent (e);
1528 if (RootContext.Documentation != null)
1529 Lexer.doc_state = XmlCommentState.Allowed;
1531 | interface_indexer_declaration
1533 Indexer i = (Indexer) $1;
1535 current_container.AddIndexer (i);
1537 if (RootContext.Documentation != null)
1538 Lexer.doc_state = XmlCommentState.Allowed;
1546 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, lexer.Location);
1551 interface_method_declaration
1552 : opt_attributes opt_new type namespace_or_type_name
1553 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1555 lexer.ConstraintsParsing = true;
1557 opt_type_parameter_constraints_clauses
1560 lexer.ConstraintsParsing = false;
1562 MemberName name = (MemberName) $4;
1564 if ($9 != null && name.TypeArguments == null)
1565 Report.Error (80, lexer.Location,
1566 "Contraints are not allowed on non-generic declarations");
1568 GenericMethod generic = null;
1569 if (name.TypeArguments != null) {
1570 generic = new GenericMethod (current_namespace, current_class,
1571 name, lexer.Location);
1573 generic.SetParameterInfo ((ArrayList) $9);
1576 $$ = new Method (current_class, generic, (Expression) $3, (int) $2, true, name,
1577 (Parameters) $6, (Attributes) $1, lexer.Location);
1578 if (RootContext.Documentation != null)
1579 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1581 | opt_attributes opt_new VOID namespace_or_type_name
1582 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1584 lexer.ConstraintsParsing = true;
1586 opt_type_parameter_constraints_clauses
1589 lexer.ConstraintsParsing = false;
1591 MemberName name = (MemberName) $4;
1593 if ($9 != null && name.TypeArguments == null)
1594 Report.Error (80, lexer.Location,
1595 "Contraints are not allowed on non-generic declarations");
1597 GenericMethod generic = null;
1598 if (name.TypeArguments != null) {
1599 generic = new GenericMethod (current_namespace, current_class,
1600 name, lexer.Location);
1602 generic.SetParameterInfo ((ArrayList) $9);
1605 $$ = new Method (current_class, generic, TypeManager.system_void_expr, (int) $2,
1606 true, name, (Parameters) $6, (Attributes) $1, lexer.Location);
1607 if (RootContext.Documentation != null)
1608 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1612 interface_property_declaration
1617 { lexer.PropertyParsing = true; }
1619 { lexer.PropertyParsing = false; }
1622 InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
1624 $$ = new Property (current_class, (Expression) $3, (int) $2, true,
1625 new MemberName ((string) $4), (Attributes) $1,
1626 pinfo.Get, pinfo.Set, lexer.Location);
1627 if (RootContext.Documentation != null)
1628 ((Property) $$).DocComment = Lexer.consume_doc_comment ();
1633 CheckIdentifierToken (yyToken);
1639 : opt_attributes opt_modifiers GET SEMICOLON
1640 { $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, (int) $2, 0, lexer.Location, lexer.Location); }
1641 | opt_attributes opt_modifiers SET SEMICOLON
1642 { $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1, 0, (int) $2, lexer.Location, lexer.Location); }
1643 | opt_attributes opt_modifiers GET SEMICOLON opt_attributes opt_modifiers SET SEMICOLON
1644 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $5, (int) $2, (int) $6, lexer.Location, lexer.Location); }
1645 | opt_attributes opt_modifiers SET SEMICOLON opt_attributes opt_modifiers GET SEMICOLON
1646 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $5, (Attributes) $1, (int) $6, (int) $2, lexer.Location, lexer.Location); }
1649 interface_event_declaration
1650 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1652 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1653 new MemberName ((string) $5), null,
1654 (Attributes) $1, lexer.Location);
1655 if (RootContext.Documentation != null)
1656 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1658 | opt_attributes opt_new EVENT type error {
1659 CheckIdentifierToken (yyToken);
1662 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1663 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1666 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE event_accessor_declarations CLOSE_BRACE {
1667 Report.Error (69, lexer.Location, "Event accessors not valid on interfaces");
1672 interface_indexer_declaration
1673 : opt_attributes opt_new type THIS
1674 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1676 { lexer.PropertyParsing = true; }
1678 { lexer.PropertyParsing = false; }
1681 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1683 $$ = new Indexer (current_class, (Expression) $3,
1684 new MemberName (TypeContainer.DefaultIndexerName),
1685 (int) $2, true, (Parameters) $6, (Attributes) $1,
1686 info.Get, info.Set, lexer.Location);
1687 if (RootContext.Documentation != null)
1688 ((Indexer) $$).DocComment = ConsumeStoredComment ();
1692 operator_declaration
1693 : opt_attributes opt_modifiers operator_declarator
1695 iterator_container = SimpleIteratorContainer.GetSimple ();
1699 OperatorDeclaration decl = (OperatorDeclaration) $3;
1701 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1703 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null);
1704 if (decl.arg2type != null)
1705 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null);
1707 Operator op = new Operator (
1708 current_class, decl.optype, decl.ret_type, (int) $2,
1709 new Parameters (param_list, null, decl.location),
1710 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1712 if (RootContext.Documentation != null) {
1713 op.DocComment = tmpComment;
1714 Lexer.doc_state = XmlCommentState.Allowed;
1717 if (SimpleIteratorContainer.Simple.Yields)
1720 // Note again, checking is done in semantic analysis
1721 current_container.AddOperator (op);
1723 current_local_parameters = null;
1724 iterator_container = null;
1730 | SEMICOLON { $$ = null; }
1733 : type OPERATOR overloadable_operator
1734 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1736 Operator.OpType op = (Operator.OpType) $3;
1737 CheckUnaryOperator (op);
1739 if (op == Operator.OpType.Addition)
1740 op = Operator.OpType.UnaryPlus;
1742 if (op == Operator.OpType.Subtraction)
1743 op = Operator.OpType.UnaryNegation;
1745 Parameter [] pars = new Parameter [1];
1746 Expression type = (Expression) $5;
1748 pars [0] = new Parameter (type, (string) $6, Parameter.Modifier.NONE, null);
1750 current_local_parameters = new Parameters (pars, null, lexer.Location);
1752 if (RootContext.Documentation != null) {
1753 tmpComment = Lexer.consume_doc_comment ();
1754 Lexer.doc_state = XmlCommentState.NotAllowed;
1757 $$ = new OperatorDeclaration (op, (Expression) $1, type, (string) $6,
1758 null, null, lexer.Location);
1760 | type OPERATOR overloadable_operator
1762 type IDENTIFIER COMMA
1766 CheckBinaryOperator ((Operator.OpType) $3);
1768 Parameter [] pars = new Parameter [2];
1770 Expression typeL = (Expression) $5;
1771 Expression typeR = (Expression) $8;
1773 pars [0] = new Parameter (typeL, (string) $6, Parameter.Modifier.NONE, null);
1774 pars [1] = new Parameter (typeR, (string) $9, Parameter.Modifier.NONE, null);
1776 current_local_parameters = new Parameters (pars, null, lexer.Location);
1778 if (RootContext.Documentation != null) {
1779 tmpComment = Lexer.consume_doc_comment ();
1780 Lexer.doc_state = XmlCommentState.NotAllowed;
1783 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1785 typeR, (string) $9, lexer.Location);
1787 | conversion_operator_declarator
1790 overloadable_operator
1792 : BANG { $$ = Operator.OpType.LogicalNot; }
1793 | TILDE { $$ = Operator.OpType.OnesComplement; }
1794 | OP_INC { $$ = Operator.OpType.Increment; }
1795 | OP_DEC { $$ = Operator.OpType.Decrement; }
1796 | TRUE { $$ = Operator.OpType.True; }
1797 | FALSE { $$ = Operator.OpType.False; }
1798 // Unary and binary:
1799 | PLUS { $$ = Operator.OpType.Addition; }
1800 | MINUS { $$ = Operator.OpType.Subtraction; }
1802 | STAR { $$ = Operator.OpType.Multiply; }
1803 | DIV { $$ = Operator.OpType.Division; }
1804 | PERCENT { $$ = Operator.OpType.Modulus; }
1805 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1806 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1807 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1808 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1809 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1810 | OP_EQ { $$ = Operator.OpType.Equality; }
1811 | OP_NE { $$ = Operator.OpType.Inequality; }
1812 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1813 | OP_LT { $$ = Operator.OpType.LessThan; }
1814 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1815 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1818 conversion_operator_declarator
1819 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1821 Parameter [] pars = new Parameter [1];
1823 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1825 current_local_parameters = new Parameters (pars, null, lexer.Location);
1827 if (RootContext.Documentation != null) {
1828 tmpComment = Lexer.consume_doc_comment ();
1829 Lexer.doc_state = XmlCommentState.NotAllowed;
1832 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1833 null, null, lexer.Location);
1835 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1837 Parameter [] pars = new Parameter [1];
1839 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1841 current_local_parameters = new Parameters (pars, null, lexer.Location);
1843 if (RootContext.Documentation != null) {
1844 tmpComment = Lexer.consume_doc_comment ();
1845 Lexer.doc_state = XmlCommentState.NotAllowed;
1848 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1849 null, null, lexer.Location);
1853 syntax_error (lexer.Location, "'operator' expected");
1857 syntax_error (lexer.Location, "'operator' expected");
1861 constructor_declaration
1864 constructor_declarator
1867 Constructor c = (Constructor) $3;
1868 c.Block = (ToplevelBlock) $4;
1869 c.OptAttributes = (Attributes) $1;
1870 c.ModFlags = (int) $2;
1872 if (RootContext.Documentation != null)
1873 c.DocComment = ConsumeStoredComment ();
1875 if (c.Name == current_container.Basename){
1876 if ((c.ModFlags & Modifiers.STATIC) != 0){
1877 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1879 515, c.Location, String.Format (
1880 "`{0}.{1}': static constructor can not have access modifiers",
1881 c.Name, current_container.Name));
1884 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1886 if (c.Initializer != null){
1889 "Static constructors can not have an explicit this or base " +
1890 "constructor invocations");
1893 if (!c.Parameters.Empty){
1895 132, c.Location, "Static constructors should not have parameters");
1898 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1901 // We let another layer check the validity of the constructor.
1902 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1905 current_container.AddConstructor (c);
1907 current_local_parameters = null;
1908 if (RootContext.Documentation != null)
1909 Lexer.doc_state = XmlCommentState.Allowed;
1913 constructor_declarator
1916 if (RootContext.Documentation != null) {
1917 tmpComment = Lexer.consume_doc_comment ();
1918 Lexer.doc_state = XmlCommentState.Allowed;
1921 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1923 oob_stack.Push (lexer.Location);
1925 current_local_parameters = (Parameters) $4;
1927 opt_constructor_initializer
1929 Location l = (Location) oob_stack.Pop ();
1930 $$ = new Constructor (current_class, (string) $1, 0, (Parameters) $4,
1931 (ConstructorInitializer) $7, l);
1937 | SEMICOLON { $$ = null; }
1940 opt_constructor_initializer
1941 : /* empty */ { $$ = null; }
1942 | constructor_initializer
1945 constructor_initializer
1946 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1948 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1950 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1952 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1955 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1961 : /* EMPTY */ { $$ = 0; }
1962 | UNSAFE { $$ = Modifiers.UNSAFE; }
1963 | EXTERN { $$ = Modifiers.EXTERN; }
1966 destructor_declaration
1967 : opt_attributes opt_finalizer TILDE
1969 if (RootContext.Documentation != null) {
1970 tmpComment = Lexer.consume_doc_comment ();
1971 Lexer.doc_state = XmlCommentState.NotAllowed;
1974 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1976 if ((string) $5 != current_container.Basename){
1977 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1978 } else if (current_container.Kind != Kind.Class){
1979 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1981 Location l = lexer.Location;
1984 if (!RootContext.StdLib && current_container.Name == "System.Object")
1985 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
1987 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
1989 if ((m & Modifiers.UNSAFE) != 0){
1990 if (!RootContext.Unsafe){
1991 Report.Error (227, l,
1992 "Unsafe code requires the -unsafe command " +
1993 "line option to be specified");
1997 Method d = new Destructor (
1998 current_class, TypeManager.system_void_expr, m, "Finalize",
1999 new Parameters (null, null, l), (Attributes) $1, l);
2000 if (RootContext.Documentation != null)
2001 d.DocComment = ConsumeStoredComment ();
2003 d.Block = (ToplevelBlock) $8;
2004 current_container.AddMethod (d);
2012 EVENT type variable_declarators SEMICOLON
2014 foreach (VariableDeclaration var in (ArrayList) $5) {
2016 MemberName name = new MemberName (var.identifier);
2018 Event e = new EventField (
2019 current_class, (Expression) $4, (int) $2, false, name,
2020 var.expression_or_array_initializer, (Attributes) $1,
2023 current_container.AddEvent (e);
2025 if (RootContext.Documentation != null) {
2026 e.DocComment = Lexer.consume_doc_comment ();
2027 Lexer.doc_state = XmlCommentState.Allowed;
2033 EVENT type namespace_or_type_name
2036 implicit_value_parameter_type = (Expression) $4;
2037 lexer.EventParsing = true;
2038 oob_stack.Push (lexer.Location);
2040 event_accessor_declarations
2042 lexer.EventParsing = false;
2046 Location loc = (Location) oob_stack.Pop ();
2049 Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
2052 Pair pair = (Pair) $8;
2054 MemberName name = (MemberName) $5;
2056 if (name.TypeArguments != null)
2057 syntax_error (lexer.Location, "an event can't have type arguments");
2059 Event e = new EventProperty (
2060 current_class, (Expression) $4, (int) $2, false, name, null,
2061 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
2063 if (RootContext.Documentation != null) {
2064 e.DocComment = Lexer.consume_doc_comment ();
2065 Lexer.doc_state = XmlCommentState.Allowed;
2068 current_container.AddEvent (e);
2069 implicit_value_parameter_type = null;
2072 | opt_attributes opt_modifiers EVENT type namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS block {
2073 MemberName mn = (MemberName) $5;
2075 if (mn.Left != null)
2076 Report.Error (71, lexer.Location, "Explicit implementation of events requires property syntax");
2078 Report.Error (71, lexer.Location, "Event declaration should use property syntax");
2080 if (RootContext.Documentation != null)
2081 Lexer.doc_state = XmlCommentState.Allowed;
2085 event_accessor_declarations
2086 : add_accessor_declaration remove_accessor_declaration
2088 $$ = new Pair ($1, $2);
2090 | remove_accessor_declaration add_accessor_declaration
2092 $$ = new Pair ($2, $1);
2094 | add_accessor_declaration { $$ = null; }
2095 | remove_accessor_declaration { $$ = null; }
2098 Report.Error (1055, lexer.Location, "An add or remove accessor expected");
2104 add_accessor_declaration
2105 : opt_attributes ADD
2107 Parameter [] args = new Parameter [1];
2108 Parameter implicit_value_parameter = new Parameter (
2109 implicit_value_parameter_type, "value",
2110 Parameter.Modifier.NONE, null);
2112 args [0] = implicit_value_parameter;
2114 current_local_parameters = new Parameters (args, null, lexer.Location);
2115 lexer.EventParsing = false;
2119 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2120 lexer.EventParsing = true;
2122 | opt_attributes ADD error {
2123 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2128 remove_accessor_declaration
2129 : opt_attributes REMOVE
2131 Parameter [] args = new Parameter [1];
2132 Parameter implicit_value_parameter = new Parameter (
2133 implicit_value_parameter_type, "value",
2134 Parameter.Modifier.NONE, null);
2136 args [0] = implicit_value_parameter;
2138 current_local_parameters = new Parameters (args, null, lexer.Location);
2139 lexer.EventParsing = false;
2143 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2144 lexer.EventParsing = true;
2146 | opt_attributes REMOVE error {
2147 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2153 : opt_attributes opt_modifiers indexer_declarator
2156 IndexerDeclaration decl = (IndexerDeclaration) $3;
2158 implicit_value_parameter_type = decl.type;
2160 lexer.PropertyParsing = true;
2161 parsing_indexer = true;
2163 indexer_parameters = decl.param_list;
2164 oob_stack.Push (lexer.Location);
2166 accessor_declarations
2168 lexer.PropertyParsing = false;
2169 parsing_indexer = false;
2173 // The signature is computed from the signature of the indexer. Look
2174 // at section 3.6 on the spec
2175 Location loc = (Location) oob_stack.Pop ();
2177 IndexerDeclaration decl = (IndexerDeclaration) $3;
2178 Pair pair = (Pair) $6;
2179 Accessor get_block = (Accessor) pair.First;
2180 Accessor set_block = (Accessor) pair.Second;
2183 if (decl.interface_type != null)
2184 name = new MemberName (decl.interface_type,
2185 TypeContainer.DefaultIndexerName, null);
2187 name = new MemberName (TypeContainer.DefaultIndexerName);
2189 indexer = new Indexer (current_class, decl.type, name,
2190 (int) $2, false, decl.param_list, (Attributes) $1,
2191 get_block, set_block, loc);
2192 if (RootContext.Documentation != null)
2193 indexer.DocComment = ConsumeStoredComment ();
2195 current_container.AddIndexer (indexer);
2197 current_local_parameters = null;
2198 implicit_value_parameter_type = null;
2199 indexer_parameters = null;
2204 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2206 Parameters pars = (Parameters) $4;
2207 if (pars.HasArglist) {
2208 // "__arglist is not valid in this context"
2209 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2210 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2211 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2213 if (RootContext.Documentation != null) {
2214 tmpComment = Lexer.consume_doc_comment ();
2215 Lexer.doc_state = XmlCommentState.Allowed;
2218 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
2220 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2222 Parameters pars = (Parameters) $6;
2224 if (pars.HasArglist) {
2225 // "__arglist is not valid in this context"
2226 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2227 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2228 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2231 MemberName name = (MemberName) $2;
2232 if (name.TypeArguments != null)
2233 syntax_error (lexer.Location, "an indexer can't have type arguments");
2235 $$ = new IndexerDeclaration ((Expression) $1, name, pars);
2237 if (RootContext.Documentation != null) {
2238 tmpComment = Lexer.consume_doc_comment ();
2239 Lexer.doc_state = XmlCommentState.Allowed;
2249 if (RootContext.Documentation != null)
2250 enumTypeComment = Lexer.consume_doc_comment ();
2255 Location enum_location = lexer.Location;
2257 MemberName full_name = MakeName (new MemberName ((string) $4));
2258 Enum e = new Enum (current_namespace, current_container, (Expression) $5, (int) $2,
2259 full_name, (Attributes) $1, enum_location);
2261 if (RootContext.Documentation != null)
2262 e.DocComment = enumTypeComment;
2264 foreach (VariableDeclaration ev in (ArrayList) $7) {
2265 e.AddEnumMember (ev.identifier,
2266 (Expression) ev.expression_or_array_initializer,
2267 ev.Location, ev.OptAttributes,
2271 string name = full_name.GetName ();
2272 current_container.AddEnum (e);
2273 RootContext.Tree.RecordDecl (name, e);
2279 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2280 | COLON type { $$ = $2; }
2286 if (RootContext.Documentation != null)
2287 Lexer.doc_state = XmlCommentState.Allowed;
2289 opt_enum_member_declarations
2291 // here will be evaluated after CLOSE_BLACE is consumed.
2292 if (RootContext.Documentation != null)
2293 Lexer.doc_state = XmlCommentState.Allowed;
2301 opt_enum_member_declarations
2302 : /* empty */ { $$ = new ArrayList (4); }
2303 | enum_member_declarations opt_comma { $$ = $1; }
2306 enum_member_declarations
2307 : enum_member_declaration
2309 ArrayList l = new ArrayList (4);
2314 | enum_member_declarations COMMA enum_member_declaration
2316 ArrayList l = (ArrayList) $1;
2324 enum_member_declaration
2325 : opt_attributes IDENTIFIER
2327 VariableDeclaration vd = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
2329 if (RootContext.Documentation != null) {
2330 vd.DocComment = Lexer.consume_doc_comment ();
2331 Lexer.doc_state = XmlCommentState.Allowed;
2336 | opt_attributes IDENTIFIER
2338 $$ = lexer.Location;
2339 if (RootContext.Documentation != null) {
2340 tmpComment = Lexer.consume_doc_comment ();
2341 Lexer.doc_state = XmlCommentState.NotAllowed;
2346 VariableDeclaration vd = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
2348 if (RootContext.Documentation != null)
2349 vd.DocComment = ConsumeStoredComment ();
2355 delegate_declaration
2358 DELEGATE type member_name
2359 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2361 Location l = lexer.Location;
2362 MemberName name = MakeName ((MemberName) $5);
2363 Delegate del = new Delegate (current_namespace, current_container, (Expression) $4,
2364 (int) $2, name, (Parameters) $7, (Attributes) $1, l);
2366 if (RootContext.Documentation != null) {
2367 del.DocComment = Lexer.consume_doc_comment ();
2368 Lexer.doc_state = XmlCommentState.Allowed;
2371 current_container.AddDelegate (del);
2372 RootContext.Tree.RecordDecl (name.GetName (true), del);
2374 current_delegate = del;
2376 lexer.ConstraintsParsing = true;
2378 opt_type_parameter_constraints_clauses
2380 lexer.ConstraintsParsing = false;
2384 current_delegate.SetParameterInfo ((ArrayList) $9);
2386 current_delegate = null;
2390 DELEGATE VOID member_name
2391 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2393 Location l = lexer.Location;
2394 MemberName name = MakeName ((MemberName) $5);
2395 Delegate del = new Delegate (
2396 current_namespace, current_container,
2397 TypeManager.system_void_expr, (int) $2, name,
2398 (Parameters) $7, (Attributes) $1, l);
2400 if (RootContext.Documentation != null) {
2401 del.DocComment = Lexer.consume_doc_comment ();
2402 Lexer.doc_state = XmlCommentState.Allowed;
2405 current_container.AddDelegate (del);
2406 RootContext.Tree.RecordDecl (name.GetName (true), del);
2408 current_delegate = del;
2410 lexer.ConstraintsParsing = true;
2412 opt_type_parameter_constraints_clauses
2414 lexer.ConstraintsParsing = false;
2418 current_delegate.SetParameterInfo ((ArrayList) $9);
2420 current_delegate = null;
2424 namespace_or_type_name
2426 | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list {
2427 $$ = new MemberName ((MemberName) $1, (string) $3, (TypeArguments) $4);
2432 : IDENTIFIER opt_type_argument_list {
2433 $$ = new MemberName ((string) $1, (TypeArguments) $2);
2437 opt_type_argument_list
2438 : /* empty */ { $$ = null; }
2439 | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2445 $$ = new TypeArguments ((int) $1, lexer.Location);
2451 TypeArguments type_args = new TypeArguments (lexer.Location);
2452 type_args.Add ((Expression) $1);
2455 | type_arguments COMMA type {
2456 TypeArguments type_args = (TypeArguments) $1;
2457 type_args.Add ((Expression) $3);
2463 * Before you think of adding a return_type, notice that we have been
2464 * using two rules in the places where it matters (one rule using type
2465 * and another identical one that uses VOID as the return type). This
2466 * gets rid of a shift/reduce couple
2469 : namespace_or_type_name
2471 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2483 // Note that here only unmanaged types are allowed but we
2484 // can't perform checks during this phase - we do it during
2485 // semantic analysis.
2487 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2491 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2497 | non_expression_type rank_specifier
2499 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2501 | non_expression_type STAR
2503 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2505 | expression rank_specifiers
2507 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2511 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2515 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2516 // through this different path
2518 | multiplicative_expression STAR
2520 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2527 ArrayList types = new ArrayList (4);
2532 | type_list COMMA type
2534 ArrayList types = (ArrayList) $1;
2542 * replaces all the productions for isolating the various
2543 * simple types, but we need this to reuse it easily in local_variable_type
2546 : OBJECT { $$ = TypeManager.system_object_expr; }
2547 | STRING { $$ = TypeManager.system_string_expr; }
2548 | BOOL { $$ = TypeManager.system_boolean_expr; }
2549 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2550 | FLOAT { $$ = TypeManager.system_single_expr; }
2551 | DOUBLE { $$ = TypeManager.system_double_expr; }
2556 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2557 | BYTE { $$ = TypeManager.system_byte_expr; }
2558 | SHORT { $$ = TypeManager.system_int16_expr; }
2559 | USHORT { $$ = TypeManager.system_uint16_expr; }
2560 | INT { $$ = TypeManager.system_int32_expr; }
2561 | UINT { $$ = TypeManager.system_uint32_expr; }
2562 | LONG { $$ = TypeManager.system_int64_expr; }
2563 | ULONG { $$ = TypeManager.system_uint64_expr; }
2564 | CHAR { $$ = TypeManager.system_char_expr; }
2565 | VOID { $$ = TypeManager.system_void_expr; }
2569 : type rank_specifiers
2571 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2576 // Expressions, section 7.5
2586 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2588 | parenthesized_expression
2589 | default_value_expression
2591 | invocation_expression
2595 | post_increment_expression
2596 | post_decrement_expression
2600 | checked_expression
2601 | unchecked_expression
2602 | pointer_member_access
2603 | anonymous_method_expression
2610 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2611 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2612 | NULL { $$ = NullLiteral.Null; }
2616 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2617 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2618 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2623 object v = lexer.Value;
2629 $$ = IntLiteral.Zero;
2631 $$ = IntLiteral.One;
2633 $$ = new IntLiteral (i);
2634 } else if (v is uint)
2635 $$ = new UIntLiteral ((UInt32) v);
2637 $$ = new LongLiteral ((Int64) v);
2638 else if (v is ulong)
2639 $$ = new ULongLiteral ((UInt64) v);
2641 Console.WriteLine ("OOPS. Unexpected result from scanner");
2646 : TRUE { $$ = new BoolLiteral (true); }
2647 | FALSE { $$ = new BoolLiteral (false); }
2650 parenthesized_expression_0
2651 : OPEN_PARENS expression CLOSE_PARENS
2654 lexer.Deambiguate_CloseParens ();
2655 // After this, the next token returned is one of
2656 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2657 // or CLOSE_PARENS_MINUS.
2661 parenthesized_expression
2662 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2666 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2668 // If a parenthesized expression is followed by a minus, we need to wrap
2669 // the expression inside a ParenthesizedExpression for the CS0075 check
2670 // in Binary.DoResolve().
2671 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2676 : primary_expression DOT IDENTIFIER opt_type_argument_list
2678 $$ = new MemberAccess ((Expression) $1, (string) $3,
2679 (TypeArguments) $4, lexer.Location);
2681 | predefined_type DOT IDENTIFIER opt_type_argument_list
2683 $$ = new MemberAccess ((Expression) $1, (string) $3,
2684 (TypeArguments) $4, lexer.Location);
2692 invocation_expression
2693 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2696 Location l = lexer.Location;
2697 Report.Error (1, l, "Parse error");
2699 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2701 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2703 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2705 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2707 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2712 : /* empty */ { $$ = null; }
2719 ArrayList list = new ArrayList (4);
2723 | argument_list COMMA argument
2725 ArrayList list = (ArrayList) $1;
2729 | argument_list error {
2730 CheckToken (1026, yyToken, ", or ) expected");
2737 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2739 | REF variable_reference
2741 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2743 | OUT variable_reference
2745 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2747 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2749 ArrayList list = (ArrayList) $3;
2750 Argument[] args = new Argument [list.Count];
2751 list.CopyTo (args, 0);
2753 Expression expr = new Arglist (args, lexer.Location);
2754 $$ = new Argument (expr, Argument.AType.Expression);
2758 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2763 : expression { note ("section 5.4"); $$ = $1; }
2767 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2769 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2771 | primary_expression rank_specifiers
2773 // So the super-trick is that primary_expression
2774 // can only be either a SimpleName or a MemberAccess.
2775 // The MemberAccess case arises when you have a fully qualified type-name like :
2777 // SimpleName is when you have
2780 Expression expr = (Expression) $1;
2781 if (expr is ComposedCast){
2782 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2783 } else if (!(expr is SimpleName || expr is MemberAccess || expr is ConstructedType)){
2784 Error_ExpectingTypeName (lexer.Location, expr);
2785 $$ = TypeManager.system_object_expr;
2788 // So we extract the string corresponding to the SimpleName
2791 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2799 ArrayList list = new ArrayList (4);
2803 | expression_list COMMA expression
2805 ArrayList list = (ArrayList) $1;
2814 $$ = new This (current_block, lexer.Location);
2819 : BASE DOT IDENTIFIER
2821 $$ = new BaseAccess ((string) $3, lexer.Location);
2823 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2825 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2828 Report.Error (175, lexer.Location, "Use of keyword `base' is not valid in this context");
2833 post_increment_expression
2834 : primary_expression OP_INC
2836 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2837 (Expression) $1, lexer.Location);
2841 post_decrement_expression
2842 : primary_expression OP_DEC
2844 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2845 (Expression) $1, lexer.Location);
2850 : object_or_delegate_creation_expression
2851 | array_creation_expression
2854 object_or_delegate_creation_expression
2855 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2857 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2861 array_creation_expression
2862 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2864 opt_array_initializer
2866 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2868 | NEW type rank_specifiers array_initializer
2870 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2874 Report.Error (1031, lexer.Location, "Type expected");
2879 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2895 : rank_specifier opt_rank_specifier
2897 $$ = (string) $2 + (string) $1;
2902 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2904 $$ = "[" + (string) $2 + "]";
2924 | dim_separators COMMA
2926 $$ = (string) $1 + ",";
2930 opt_array_initializer
2942 : OPEN_BRACE CLOSE_BRACE
2944 ArrayList list = new ArrayList (4);
2947 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2949 $$ = (ArrayList) $2;
2953 variable_initializer_list
2954 : variable_initializer
2956 ArrayList list = new ArrayList (4);
2960 | variable_initializer_list COMMA variable_initializer
2962 ArrayList list = (ArrayList) $1;
2968 void_pointer_expression
2969 : void_pointer_expression STAR
2971 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2975 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
2980 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2982 $$ = new TypeOfVoid (lexer.Location);
2984 | TYPEOF OPEN_PARENS void_pointer_expression CLOSE_PARENS
2986 $$ = new TypeOf ((Expression) $3, lexer.Location);
2988 | TYPEOF OPEN_PARENS
2990 lexer.TypeOfParsing = true;
2994 lexer.TypeOfParsing = false;
2995 $$ = new TypeOf ((Expression) $4, lexer.Location);
3000 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3001 $$ = new SizeOf ((Expression) $3, lexer.Location);
3006 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3008 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
3012 unchecked_expression
3013 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3015 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
3019 pointer_member_access
3020 : primary_expression OP_PTR IDENTIFIER
3024 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
3025 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
3029 anonymous_method_expression
3030 : DELEGATE opt_anonymous_method_signature {
3031 oob_stack.Push (current_local_parameters);
3032 current_local_parameters = (Parameters)$2;
3034 // Force the next block to be created as a ToplevelBlock
3035 oob_stack.Push (current_block);
3036 oob_stack.Push (top_current_block);
3037 oob_stack.Push (lexer.Location);
3038 current_block = null;
3040 Location loc = (Location) oob_stack.Pop ();
3041 top_current_block = (Block) oob_stack.Pop ();
3042 current_block = (Block) oob_stack.Pop ();
3043 if (RootContext.Version == LanguageVersion.ISO_1){
3044 Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
3047 ToplevelBlock anon_block = (ToplevelBlock) $4;
3049 anon_block.Parent = current_block;
3050 $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block,
3053 current_local_parameters = (Parameters) oob_stack.Pop ();
3057 opt_anonymous_method_signature
3058 : /* empty */ { $$ = null; }
3059 | anonymous_method_signature
3062 anonymous_method_signature
3063 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3066 $$ = Parameters.EmptyReadOnlyParameters;
3068 ArrayList par_list = (ArrayList) $2;
3069 Parameter [] pars = new Parameter [par_list.Count];
3070 par_list.CopyTo (pars);
3071 $$ = new Parameters (pars, null, lexer.Location);
3076 opt_anonymous_method_parameter_list
3077 : /* empty */ { $$ = null; }
3078 | anonymous_method_parameter_list { $$ = $1; }
3081 anonymous_method_parameter_list
3082 : anonymous_method_parameter
3084 ArrayList a = new ArrayList (4);
3088 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3090 ArrayList a = (ArrayList) $1;
3096 anonymous_method_parameter
3097 : opt_parameter_modifier type IDENTIFIER {
3098 $$ = new Parameter ((Expression) $2, (string) $3, (Parameter.Modifier) $1, null);
3100 | PARAMS type IDENTIFIER {
3101 Report.Error (-221, lexer.Location, "params modifier not allowed in anonymous method declaration");
3106 default_value_expression
3107 : DEFAULT_OPEN_PARENS type CLOSE_PARENS
3109 $$ = new DefaultValueExpression ((Expression) $2, lexer.Location);
3114 : primary_expression
3115 | BANG prefixed_unary_expression
3117 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
3119 | TILDE prefixed_unary_expression
3121 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
3127 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3129 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3131 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3133 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3139 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3141 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3146 // The idea to split this out is from Rhys' grammar
3147 // to solve the problem with casts.
3149 prefixed_unary_expression
3151 | PLUS prefixed_unary_expression
3153 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
3155 | MINUS prefixed_unary_expression
3157 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
3159 | OP_INC prefixed_unary_expression
3161 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3162 (Expression) $2, lexer.Location);
3164 | OP_DEC prefixed_unary_expression
3166 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3167 (Expression) $2, lexer.Location);
3169 | STAR prefixed_unary_expression
3171 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
3173 | BITWISE_AND prefixed_unary_expression
3175 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
3179 pre_increment_expression
3180 : OP_INC prefixed_unary_expression
3182 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3183 (Expression) $2, lexer.Location);
3187 pre_decrement_expression
3188 : OP_DEC prefixed_unary_expression
3190 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3191 (Expression) $2, lexer.Location);
3195 multiplicative_expression
3196 : prefixed_unary_expression
3197 | multiplicative_expression STAR prefixed_unary_expression
3199 $$ = new Binary (Binary.Operator.Multiply,
3200 (Expression) $1, (Expression) $3, lexer.Location);
3202 | multiplicative_expression DIV prefixed_unary_expression
3204 $$ = new Binary (Binary.Operator.Division,
3205 (Expression) $1, (Expression) $3, lexer.Location);
3207 | multiplicative_expression PERCENT prefixed_unary_expression
3209 $$ = new Binary (Binary.Operator.Modulus,
3210 (Expression) $1, (Expression) $3, lexer.Location);
3215 : multiplicative_expression
3216 | additive_expression PLUS multiplicative_expression
3218 $$ = new Binary (Binary.Operator.Addition,
3219 (Expression) $1, (Expression) $3, lexer.Location);
3221 | additive_expression MINUS multiplicative_expression
3223 $$ = new Binary (Binary.Operator.Subtraction,
3224 (Expression) $1, (Expression) $3, lexer.Location);
3229 : additive_expression
3230 | shift_expression OP_SHIFT_LEFT additive_expression
3232 $$ = new Binary (Binary.Operator.LeftShift,
3233 (Expression) $1, (Expression) $3, lexer.Location);
3235 | shift_expression OP_SHIFT_RIGHT additive_expression
3237 $$ = new Binary (Binary.Operator.RightShift,
3238 (Expression) $1, (Expression) $3, lexer.Location);
3242 relational_expression
3244 | relational_expression OP_LT shift_expression
3246 $$ = new Binary (Binary.Operator.LessThan,
3247 (Expression) $1, (Expression) $3, lexer.Location);
3249 | relational_expression OP_GT shift_expression
3251 $$ = new Binary (Binary.Operator.GreaterThan,
3252 (Expression) $1, (Expression) $3, lexer.Location);
3254 | relational_expression OP_LE shift_expression
3256 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3257 (Expression) $1, (Expression) $3, lexer.Location);
3259 | relational_expression OP_GE shift_expression
3261 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3262 (Expression) $1, (Expression) $3, lexer.Location);
3264 | relational_expression IS type
3266 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
3268 | relational_expression AS type
3270 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
3275 : relational_expression
3276 | equality_expression OP_EQ relational_expression
3278 $$ = new Binary (Binary.Operator.Equality,
3279 (Expression) $1, (Expression) $3, lexer.Location);
3281 | equality_expression OP_NE relational_expression
3283 $$ = new Binary (Binary.Operator.Inequality,
3284 (Expression) $1, (Expression) $3, lexer.Location);
3289 : equality_expression
3290 | and_expression BITWISE_AND equality_expression
3292 $$ = new Binary (Binary.Operator.BitwiseAnd,
3293 (Expression) $1, (Expression) $3, lexer.Location);
3297 exclusive_or_expression
3299 | exclusive_or_expression CARRET and_expression
3301 $$ = new Binary (Binary.Operator.ExclusiveOr,
3302 (Expression) $1, (Expression) $3, lexer.Location);
3306 inclusive_or_expression
3307 : exclusive_or_expression
3308 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3310 $$ = new Binary (Binary.Operator.BitwiseOr,
3311 (Expression) $1, (Expression) $3, lexer.Location);
3315 conditional_and_expression
3316 : inclusive_or_expression
3317 | conditional_and_expression OP_AND inclusive_or_expression
3319 $$ = new Binary (Binary.Operator.LogicalAnd,
3320 (Expression) $1, (Expression) $3, lexer.Location);
3324 conditional_or_expression
3325 : conditional_and_expression
3326 | conditional_or_expression OP_OR conditional_and_expression
3328 $$ = new Binary (Binary.Operator.LogicalOr,
3329 (Expression) $1, (Expression) $3, lexer.Location);
3333 conditional_expression
3334 : conditional_or_expression
3335 | conditional_or_expression INTERR expression COLON expression
3337 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
3341 assignment_expression
3342 : prefixed_unary_expression ASSIGN expression
3344 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
3346 | prefixed_unary_expression OP_MULT_ASSIGN expression
3348 Location l = lexer.Location;
3350 $$ = new CompoundAssign (
3351 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
3353 | prefixed_unary_expression OP_DIV_ASSIGN expression
3355 Location l = lexer.Location;
3357 $$ = new CompoundAssign (
3358 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
3360 | prefixed_unary_expression OP_MOD_ASSIGN expression
3362 Location l = lexer.Location;
3364 $$ = new CompoundAssign (
3365 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
3367 | prefixed_unary_expression OP_ADD_ASSIGN expression
3369 Location l = lexer.Location;
3371 $$ = new CompoundAssign (
3372 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
3374 | prefixed_unary_expression OP_SUB_ASSIGN expression
3376 Location l = lexer.Location;
3378 $$ = new CompoundAssign (
3379 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
3381 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3383 Location l = lexer.Location;
3385 $$ = new CompoundAssign (
3386 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
3388 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3390 Location l = lexer.Location;
3392 $$ = new CompoundAssign (
3393 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
3395 | prefixed_unary_expression OP_AND_ASSIGN expression
3397 Location l = lexer.Location;
3399 $$ = new CompoundAssign (
3400 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
3402 | prefixed_unary_expression OP_OR_ASSIGN expression
3404 Location l = lexer.Location;
3406 $$ = new CompoundAssign (
3407 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
3409 | prefixed_unary_expression OP_XOR_ASSIGN expression
3411 Location l = lexer.Location;
3413 $$ = new CompoundAssign (
3414 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
3419 : conditional_expression
3420 | assignment_expression
3440 MemberName name = MakeName ((MemberName) $5);
3441 bool partial = (bool) $3;
3442 int mod_flags = (int) $2;
3445 ClassPart part = PartialContainer.CreatePart (
3446 current_namespace, current_container, name, mod_flags,
3447 (Attributes) $1, Kind.Class, lexer.Location);
3449 current_container = part.PartialContainer;
3450 current_class = part;
3452 if ((mod_flags & Modifiers.STATIC) != 0) {
3453 current_class = new StaticClass (
3454 current_namespace, current_container, name,
3455 mod_flags, (Attributes) $1, lexer.Location);
3457 current_class = new Class (
3458 current_namespace, current_container, name,
3459 mod_flags, (Attributes) $1, lexer.Location);
3462 current_container = current_class;
3463 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
3466 lexer.ConstraintsParsing = true;
3469 opt_type_parameter_constraints_clauses
3471 lexer.ConstraintsParsing = false;
3474 if (current_class.Name == "System.Object") {
3475 Report.Error (537, current_class.Location,
3476 "The class System.Object cannot have a base " +
3477 "class or implement an interface.");
3479 current_class.Bases = (ArrayList) $7;
3482 current_class.SetParameterInfo ((ArrayList) $8);
3484 if (RootContext.Documentation != null) {
3485 current_class.DocComment = Lexer.consume_doc_comment ();
3486 Lexer.doc_state = XmlCommentState.Allowed;
3489 current_class.Register ();
3493 if (RootContext.Documentation != null)
3494 Lexer.doc_state = XmlCommentState.Allowed;
3500 current_container = current_container.Parent;
3501 current_class = current_container;
3507 { $$ = (bool) false; }
3509 { $$ = (bool) true; }
3513 : /* empty */ { $$ = (int) 0; }
3519 | modifiers modifier
3524 if ((m1 & m2) != 0) {
3525 Location l = lexer.Location;
3526 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3528 $$ = (int) (m1 | m2);
3533 : NEW { $$ = Modifiers.NEW; }
3534 | PUBLIC { $$ = Modifiers.PUBLIC; }
3535 | PROTECTED { $$ = Modifiers.PROTECTED; }
3536 | INTERNAL { $$ = Modifiers.INTERNAL; }
3537 | PRIVATE { $$ = Modifiers.PRIVATE; }
3538 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3539 | SEALED { $$ = Modifiers.SEALED; }
3540 | STATIC { $$ = Modifiers.STATIC; }
3541 | READONLY { $$ = Modifiers.READONLY; }
3542 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3543 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3544 | EXTERN { $$ = Modifiers.EXTERN; }
3545 | VOLATILE { $$ = Modifiers.VOLATILE; }
3546 | UNSAFE { $$ = Modifiers.UNSAFE; }
3550 : /* empty */ { $$ = null; }
3551 | class_base { $$ = $1; }
3555 : COLON type_list { $$ = $2; }
3558 opt_type_parameter_constraints_clauses
3559 : /* empty */ { $$ = null; }
3560 | type_parameter_constraints_clauses
3564 type_parameter_constraints_clauses
3565 : type_parameter_constraints_clause {
3566 ArrayList constraints = new ArrayList (1);
3567 constraints.Add ($1);
3570 | type_parameter_constraints_clauses type_parameter_constraints_clause {
3571 ArrayList constraints = (ArrayList) $1;
3573 constraints.Add ($2);
3578 type_parameter_constraints_clause
3579 : WHERE IDENTIFIER COLON type_parameter_constraints {
3580 $$ = new Constraints ((string) $2, (ArrayList) $4, lexer.Location);
3584 type_parameter_constraints
3585 : type_parameter_constraint {
3586 ArrayList constraints = new ArrayList (1);
3587 constraints.Add ($1);
3590 | type_parameter_constraints COMMA type_parameter_constraint {
3591 ArrayList constraints = (ArrayList) $1;
3593 constraints.Add ($3);
3598 type_parameter_constraint
3600 | NEW OPEN_PARENS CLOSE_PARENS {
3601 $$ = SpecialConstraint.Constructor;
3604 $$ = SpecialConstraint.ReferenceType;
3607 $$ = SpecialConstraint.ValueType;
3616 // A block is "contained" on the following places:
3618 // property_declaration as part of the accessor body (get/set)
3619 // operator_declaration
3620 // constructor_declaration
3621 // destructor_declaration
3622 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3627 if (current_block == null){
3628 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, lexer.Location);
3629 top_current_block = current_block;
3631 current_block = new Block (current_block, current_local_parameters,
3632 lexer.Location, Location.Null);
3635 opt_statement_list CLOSE_BRACE
3637 while (current_block.Implicit)
3638 current_block = current_block.Parent;
3640 current_block.SetEndLocation (lexer.Location);
3641 current_block = current_block.Parent;
3642 if (current_block == null)
3643 top_current_block = null;
3654 | statement_list statement
3658 : declaration_statement
3660 if ($1 != null && (Block) $1 != current_block){
3661 current_block.AddStatement ((Statement) $1);
3662 current_block = (Block) $1;
3665 | valid_declaration_statement
3667 current_block.AddStatement ((Statement) $1);
3672 valid_declaration_statement
3675 | expression_statement
3676 | selection_statement
3677 | iteration_statement
3681 | unchecked_statement
3689 : valid_declaration_statement
3690 | declaration_statement
3692 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3697 Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3705 $$ = EmptyStatement.Value;
3712 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3714 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3715 current_block.AddStatement (labeled);
3720 declaration_statement
3721 : local_variable_declaration SEMICOLON
3724 DictionaryEntry de = (DictionaryEntry) $1;
3726 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3730 | local_constant_declaration SEMICOLON
3733 DictionaryEntry de = (DictionaryEntry) $1;
3735 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3741 * The following is from Rhys' grammar:
3742 * > Types in local variable declarations must be recognized as
3743 * > expressions to prevent reduce/reduce errors in the grammar.
3744 * > The expressions are converted into types during semantic analysis.
3747 : primary_expression opt_rank_specifier
3749 // FIXME: Do something smart here regarding the composition of the type.
3751 // Ok, the above "primary_expression" is there to get rid of
3752 // both reduce/reduce and shift/reduces in the grammar, it should
3753 // really just be "type_name". If you use type_name, a reduce/reduce
3754 // creeps up. If you use namespace_or_type_name (which is all we need
3755 // really) two shift/reduces appear.
3758 // So the super-trick is that primary_expression
3759 // can only be either a SimpleName or a MemberAccess.
3760 // The MemberAccess case arises when you have a fully qualified type-name like :
3762 // SimpleName is when you have
3765 Expression expr = (Expression) $1;
3766 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType)) {
3767 Error_ExpectingTypeName (lexer.Location, expr);
3771 // So we extract the string corresponding to the SimpleName
3775 if ((string) $2 == "")
3778 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3781 | builtin_types opt_rank_specifier
3783 if ((string) $2 == "")
3786 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3790 local_variable_pointer_type
3791 : primary_expression STAR
3793 Expression expr = (Expression) $1;
3794 Location l = lexer.Location;
3796 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType)) {
3797 Error_ExpectingTypeName (l, expr);
3801 $$ = new ComposedCast ((Expression) $1, "*", l);
3803 | builtin_types STAR
3805 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3809 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3811 | local_variable_pointer_type STAR
3813 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3817 local_variable_declaration
3818 : local_variable_type variable_declarators
3821 $$ = new DictionaryEntry ($1, $2);
3825 | local_variable_pointer_type opt_rank_specifier variable_declarators
3830 if ((string) $2 == "")
3831 t = (Expression) $1;
3833 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3834 $$ = new DictionaryEntry (t, $3);
3840 local_constant_declaration
3841 : CONST local_variable_type constant_declarators
3844 $$ = new DictionaryEntry ($2, $3);
3850 expression_statement
3851 : statement_expression SEMICOLON
3858 // We have to do the wrapping here and not in the case above,
3859 // because statement_expression is used for example in for_statement
3861 statement_expression
3862 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3863 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3864 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3865 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3866 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3867 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3868 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3870 Report.Error (1002, lexer.Location, "Expecting `;'");
3875 object_creation_expression
3876 : object_or_delegate_creation_expression
3877 { note ("complain if this is a delegate maybe?"); }
3886 : if_statement_open if_statement_rest
3895 oob_stack.Push (lexer.Location);
3900 : boolean_expression CLOSE_PARENS
3903 Location l = (Location) oob_stack.Pop ();
3905 $$ = new If ((Expression) $1, (Statement) $3, l);
3907 if (RootContext.WarningLevel >= 4){
3908 if ($3 == EmptyStatement.Value)
3909 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3913 | boolean_expression CLOSE_PARENS
3914 embedded_statement ELSE embedded_statement
3916 Location l = (Location) oob_stack.Pop ();
3918 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
3923 : SWITCH OPEN_PARENS
3925 oob_stack.Push (lexer.Location);
3926 switch_stack.Push (current_block);
3928 expression CLOSE_PARENS
3931 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
3932 current_block = (Block) switch_stack.Pop ();
3948 Report.Error (1522, lexer.Location, "Empty switch block");
3956 ArrayList sections = new ArrayList (4);
3961 | switch_sections switch_section
3963 ArrayList sections = (ArrayList) $1;
3973 current_block = current_block.CreateSwitchBlock (lexer.Location);
3977 Block topmost = current_block;
3979 while (topmost.Implicit)
3980 topmost = topmost.Parent;
3981 $$ = new SwitchSection ((ArrayList) $1, topmost);
3988 ArrayList labels = new ArrayList (4);
3993 | switch_labels switch_label
3995 ArrayList labels = (ArrayList) ($1);
4003 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
4004 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
4007 1523, lexer.Location,
4008 "The keyword case or default must precede code in switch block");
4022 oob_stack.Push (lexer.Location);
4024 boolean_expression CLOSE_PARENS embedded_statement
4026 Location l = (Location) oob_stack.Pop ();
4027 $$ = new While ((Expression) $4, (Statement) $6, l);
4029 if (RootContext.WarningLevel >= 4){
4030 if ($6 == EmptyStatement.Value)
4031 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4037 : DO embedded_statement
4039 oob_stack.Push (lexer.Location);
4041 boolean_expression CLOSE_PARENS SEMICOLON
4043 Location l = (Location) oob_stack.Pop ();
4045 $$ = new Do ((Statement) $2, (Expression) $6, l);
4051 opt_for_initializer SEMICOLON
4053 Block assign_block = new Block (current_block);
4054 current_block = assign_block;
4056 if ($3 is DictionaryEntry){
4057 DictionaryEntry de = (DictionaryEntry) $3;
4059 Expression type = (Expression) de.Key;
4060 ArrayList var_declarators = (ArrayList) de.Value;
4062 foreach (VariableDeclaration decl in var_declarators){
4066 vi = current_block.AddVariable (
4067 type, decl.identifier, current_local_parameters, decl.Location);
4071 Location l = lexer.Location;
4073 if (decl.expression_or_array_initializer is Expression){
4074 expr = (Expression) decl.expression_or_array_initializer;
4075 } else if (decl.expression_or_array_initializer == null) {
4078 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4079 expr = new ArrayCreation (type, "", init, decl.Location);
4082 LocalVariableReference var;
4083 var = new LocalVariableReference (assign_block, decl.identifier, l);
4086 Assign a = new Assign (var, expr, decl.Location);
4088 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4094 oob_stack.Push (lexer.Location);
4096 opt_for_condition SEMICOLON
4097 opt_for_iterator CLOSE_PARENS
4100 Location l = (Location) oob_stack.Pop ();
4102 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
4104 if (RootContext.WarningLevel >= 4){
4105 if ($10 == EmptyStatement.Value)
4106 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4109 current_block.AddStatement (f);
4110 while (current_block.Implicit)
4111 current_block = current_block.Parent;
4113 current_block = current_block.Parent;
4118 : /* empty */ { $$ = EmptyStatement.Value; }
4123 : local_variable_declaration
4124 | statement_expression_list
4128 : /* empty */ { $$ = null; }
4129 | boolean_expression
4133 : /* empty */ { $$ = EmptyStatement.Value; }
4138 : statement_expression_list
4141 statement_expression_list
4142 : statement_expression
4144 // CHANGE: was `null'
4145 Block b = new Block (current_block, Block.Flags.Implicit);
4147 b.AddStatement ((Statement) $1);
4150 | statement_expression_list COMMA statement_expression
4152 Block b = (Block) $1;
4154 b.AddStatement ((Statement) $3);
4160 : FOREACH OPEN_PARENS type IDENTIFIER IN
4162 oob_stack.Push (lexer.Location);
4164 expression CLOSE_PARENS
4166 oob_stack.Push (current_block);
4168 Block foreach_block = new Block (current_block);
4169 LocalVariableReference v = null;
4170 Location l = lexer.Location;
4173 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
4177 // Get a writable reference to this read-only variable.
4178 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
4180 current_block = foreach_block;
4183 oob_stack.Push (current_block);
4187 Block foreach_block = (Block) oob_stack.Pop ();
4188 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
4189 Block prev_block = (Block) oob_stack.Pop ();
4190 Location l = (Location) oob_stack.Pop ();
4192 current_block = prev_block;
4195 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
4196 foreach_block.AddStatement (f);
4205 | continue_statement
4215 $$ = new Break (lexer.Location);
4220 : CONTINUE SEMICOLON
4222 $$ = new Continue (lexer.Location);
4227 : GOTO IDENTIFIER SEMICOLON
4229 $$ = new Goto (current_block, (string) $2, lexer.Location);
4231 | GOTO CASE constant_expression SEMICOLON
4233 $$ = new GotoCase ((Expression) $3, lexer.Location);
4235 | GOTO DEFAULT SEMICOLON
4237 $$ = new GotoDefault (lexer.Location);
4242 : RETURN opt_expression SEMICOLON
4244 $$ = new Return ((Expression) $2, lexer.Location);
4249 : THROW opt_expression SEMICOLON
4251 $$ = new Throw ((Expression) $2, lexer.Location);
4256 : IDENTIFIER RETURN expression SEMICOLON
4258 string s = (string) $1;
4260 Report.Error (1003, lexer.Location, "; expected");
4263 if (RootContext.Version == LanguageVersion.ISO_1){
4264 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4267 if (iterator_container == null){
4268 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4271 iterator_container.SetYields ();
4272 $$ = new Yield ((Expression) $3, lexer.Location);
4275 | IDENTIFIER BREAK SEMICOLON
4277 string s = (string) $1;
4279 Report.Error (1003, lexer.Location, "; expected");
4282 if (RootContext.Version == LanguageVersion.ISO_1){
4283 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4286 if (iterator_container == null){
4287 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4290 iterator_container.SetYields ();
4291 $$ = new YieldBreak (lexer.Location);
4302 : TRY block catch_clauses
4306 ArrayList c = (ArrayList)$3;
4307 for (int i = 0; i < c.Count; ++i) {
4308 Catch cc = (Catch) c [i];
4310 if (i != c.Count - 1)
4311 Report.Error (1017, cc.loc, "Empty catch block must be the last in a series of catch blocks");
4318 // Now s contains the list of specific catch clauses
4319 // and g contains the general one.
4321 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4323 | TRY block opt_catch_clauses FINALLY block
4326 ArrayList s = new ArrayList (4);
4327 ArrayList catch_list = (ArrayList) $3;
4329 if (catch_list != null){
4330 foreach (Catch cc in catch_list) {
4338 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4342 Report.Error (1524, lexer.Location, "Expected catch or finally");
4347 : /* empty */ { $$ = null; }
4354 ArrayList l = new ArrayList (4);
4359 | catch_clauses catch_clause
4361 ArrayList l = (ArrayList) $1;
4369 : /* empty */ { $$ = null; }
4374 : CATCH opt_catch_args
4376 Expression type = null;
4380 DictionaryEntry cc = (DictionaryEntry) $2;
4381 type = (Expression) cc.Key;
4382 id = (string) cc.Value;
4385 ArrayList one = new ArrayList (4);
4386 Location loc = lexer.Location;
4388 one.Add (new VariableDeclaration (id, null, loc));
4391 current_block = new Block (current_block);
4392 Block b = declare_local_variables (type, one, loc);
4397 Expression type = null;
4401 DictionaryEntry cc = (DictionaryEntry) $2;
4402 type = (Expression) cc.Key;
4403 id = (string) cc.Value;
4407 // FIXME: I can change this for an assignment.
4409 while (current_block != (Block) $1)
4410 current_block = current_block.Parent;
4415 $$ = new Catch (type, id , (Block) $4, ((Block) $4).loc);
4420 : /* empty */ { $$ = null; }
4425 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4427 $$ = new DictionaryEntry ($2, $3);
4434 $$ = new Checked ((Block) $2);
4441 $$ = new Unchecked ((Block) $2);
4448 if (!RootContext.Unsafe){
4449 Report.Error (227, lexer.Location,
4450 "Unsafe code can only be used if --unsafe is used");
4453 $$ = new Unsafe ((Block) $3);
4459 type fixed_pointer_declarators
4462 ArrayList list = (ArrayList) $4;
4463 Expression type = (Expression) $3;
4464 Location l = lexer.Location;
4465 int top = list.Count;
4467 Block assign_block = new Block (current_block);
4468 current_block = assign_block;
4470 for (int i = 0; i < top; i++){
4471 Pair p = (Pair) list [i];
4474 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
4488 Location l = (Location) oob_stack.Pop ();
4490 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4492 if (RootContext.WarningLevel >= 4){
4493 if ($7 == EmptyStatement.Value)
4494 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4497 current_block.AddStatement (f);
4498 while (current_block.Implicit)
4499 current_block = current_block.Parent;
4501 current_block = current_block.Parent;
4505 fixed_pointer_declarators
4506 : fixed_pointer_declarator {
4507 ArrayList declarators = new ArrayList (4);
4509 declarators.Add ($1);
4512 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4514 ArrayList declarators = (ArrayList) $1;
4516 declarators.Add ($3);
4521 fixed_pointer_declarator
4522 : IDENTIFIER ASSIGN expression
4524 $$ = new Pair ($1, $3);
4528 Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
4534 : LOCK OPEN_PARENS expression CLOSE_PARENS
4540 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4545 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4547 Block assign_block = new Block (current_block);
4548 current_block = assign_block;
4550 oob_stack.Push (lexer.Location);
4552 if ($3 is DictionaryEntry){
4553 DictionaryEntry de = (DictionaryEntry) $3;
4554 Location l = lexer.Location;
4556 Expression type = (Expression) de.Key;
4557 ArrayList var_declarators = (ArrayList) de.Value;
4559 ArrayList vars = new ArrayList (4);
4561 foreach (VariableDeclaration decl in var_declarators){
4563 LocalInfo vi = current_block.AddVariable (
4564 type, decl.identifier,
4565 current_local_parameters, decl.Location);
4571 if (decl.expression_or_array_initializer is Expression){
4572 expr = (Expression) decl.expression_or_array_initializer;
4574 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4576 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4579 expr = new ArrayCreation (type, "", init, decl.Location);
4582 LocalVariableReference var;
4584 // Get a writable reference to this read-only variable.
4585 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4587 // This is so that it is not a warning on using variables
4590 vars.Add (new DictionaryEntry (var, expr));
4592 // Assign a = new Assign (var, expr, decl.Location);
4593 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4595 $3 = new DictionaryEntry (type, vars);
4600 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
4601 current_block.AddStatement (u);
4602 while (current_block.Implicit)
4603 current_block = current_block.Parent;
4605 current_block = current_block.Parent;
4609 resource_acquisition
4610 : local_variable_declaration
4617 // A class used to pass around variable declarations and constants
4619 public class VariableDeclaration {
4620 public string identifier;
4621 public object expression_or_array_initializer;
4622 public Location Location;
4623 public Attributes OptAttributes;
4624 public string DocComment;
4626 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4628 this.identifier = id;
4629 this.expression_or_array_initializer = eoai;
4631 this.OptAttributes = opt_attrs;
4634 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4640 /// Used to pass around interface property information
4642 public class InterfaceAccessorInfo {
4644 public readonly Accessor Get, Set;
4646 public InterfaceAccessorInfo (bool has_get, bool has_set,
4647 Attributes get_attrs, Attributes set_attrs, int get_mod, int set_mod, Location get_loc, Location set_loc)
4650 Report.Error (275, get_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4652 Report.Error (275, set_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4655 Get = new Accessor (null, 0, get_attrs, get_loc);
4657 Set = new Accessor (null, 0, set_attrs, set_loc);
4664 // A class used to hold info about an indexer declarator
4666 public class IndexerDeclaration {
4667 public Expression type;
4668 public MemberName interface_type;
4669 public Parameters param_list;
4671 public IndexerDeclaration (Expression type, MemberName interface_type,
4672 Parameters param_list)
4675 this.interface_type = interface_type;
4676 this.param_list = param_list;
4681 // We use this when we do not have an object in advance that is an IIteratorContainer
4683 public class SimpleIteratorContainer : IIteratorContainer {
4686 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4691 public static SimpleIteratorContainer GetSimple () {
4692 Simple.Yields = false;
4696 public void SetYields () { Yields = true; }
4700 // A class used to hold info about an operator declarator
4702 public class OperatorDeclaration {
4703 public Operator.OpType optype;
4704 public Expression ret_type, arg1type, arg2type;
4705 public string arg1name, arg2name;
4706 public Location location;
4708 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4709 Expression arg1type, string arg1name,
4710 Expression arg2type, string arg2name, Location location)
4713 this.ret_type = ret_type;
4714 this.arg1type = arg1type;
4715 this.arg1name = arg1name;
4716 this.arg2type = arg2type;
4717 this.arg2name = arg2name;
4718 this.location = location;
4723 void Error_ExpectingTypeName (Location l, Expression expr)
4725 if (expr is Invocation){
4726 Report.Error (1002, l, "; expected");
4728 Report.Error (-1, l, "Invalid Type definition");
4733 // Given the @class_name name, it creates a fully qualified name
4734 // based on the containing declaration space
4737 MakeName (MemberName class_name)
4739 string ns = current_namespace.FullName;
4741 if (current_container.Name == ""){
4743 return new MemberName (new MemberName (ns), class_name);
4747 return new MemberName (current_container.MemberName, class_name);
4751 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4753 Block implicit_block;
4754 ArrayList inits = null;
4757 // We use the `Used' property to check whether statements
4758 // have been added to the current block. If so, we need
4759 // to create another block to contain the new declaration
4760 // otherwise, as an optimization, we use the same block to
4761 // add the declaration.
4763 // FIXME: A further optimization is to check if the statements
4764 // that were added were added as part of the initialization
4765 // below. In which case, no other statements have been executed
4766 // and we might be able to reduce the number of blocks for
4767 // situations like this:
4769 // int j = 1; int k = j + 1;
4771 implicit_block = current_block;
4773 foreach (VariableDeclaration decl in variable_declarators){
4775 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4776 if (decl.expression_or_array_initializer != null){
4778 inits = new ArrayList (4);
4785 return implicit_block;
4787 foreach (VariableDeclaration decl in inits){
4791 if (decl.expression_or_array_initializer is Expression){
4792 expr = (Expression) decl.expression_or_array_initializer;
4795 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4797 expr = new ArrayCreation (type, "", init, decl.Location);
4800 LocalVariableReference var;
4801 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4803 assign = new Assign (var, expr, decl.Location);
4805 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4808 return implicit_block;
4811 Block declare_local_constants (Expression type, ArrayList declarators)
4813 Block implicit_block;
4815 if (current_block.Used)
4816 implicit_block = new Block (current_block, Block.Flags.Implicit);
4818 implicit_block = current_block;
4820 foreach (VariableDeclaration decl in declarators){
4821 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4822 current_local_parameters, decl.Location);
4825 return implicit_block;
4828 void CheckAttributeTarget (string a)
4832 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4836 Location l = lexer.Location;
4837 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4843 void CheckUnaryOperator (Operator.OpType op)
4847 case Operator.OpType.LogicalNot:
4848 case Operator.OpType.OnesComplement:
4849 case Operator.OpType.Increment:
4850 case Operator.OpType.Decrement:
4851 case Operator.OpType.True:
4852 case Operator.OpType.False:
4853 case Operator.OpType.Addition:
4854 case Operator.OpType.Subtraction:
4859 Location l = lexer.Location;
4860 Report.Error (1019, l, "Overloadable unary operator expected");
4866 void CheckBinaryOperator (Operator.OpType op)
4870 case Operator.OpType.Addition:
4871 case Operator.OpType.Subtraction:
4872 case Operator.OpType.Multiply:
4873 case Operator.OpType.Division:
4874 case Operator.OpType.Modulus:
4875 case Operator.OpType.BitwiseAnd:
4876 case Operator.OpType.BitwiseOr:
4877 case Operator.OpType.ExclusiveOr:
4878 case Operator.OpType.LeftShift:
4879 case Operator.OpType.RightShift:
4880 case Operator.OpType.Equality:
4881 case Operator.OpType.Inequality:
4882 case Operator.OpType.GreaterThan:
4883 case Operator.OpType.LessThan:
4884 case Operator.OpType.GreaterThanOrEqual:
4885 case Operator.OpType.LessThanOrEqual:
4889 Location l = lexer.Location;
4890 Report.Error (1020, l, "Overloadable binary operator expected");
4896 void syntax_error (Location l, string msg)
4898 Report.Error (1003, l, "Syntax error, " + msg);
4901 void output (string s)
4903 Console.WriteLine (s);
4906 void note (string s)
4908 // Used to put annotations
4913 public Tokenizer Lexer {
4919 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4921 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4922 this.name = file.Name;
4924 current_container = RootContext.Tree.Types;
4925 current_container.NamespaceEntry = current_namespace;
4926 oob_stack = new Stack ();
4927 switch_stack = new Stack ();
4929 lexer = new Tokenizer (reader, file, defines);
4932 public void parse ()
4935 if (yacc_verbose_flag > 1)
4936 yyparse (lexer, new yydebug.yyDebugSimple ());
4939 Tokenizer tokenizer = lexer as Tokenizer;
4940 tokenizer.cleanup ();
4941 } catch (Exception e){
4943 // Removed for production use, use parser verbose to get the output.
4945 // Console.WriteLine (e);
4946 Report.Error (-25, lexer.Location, "Parsing error");
4947 if (yacc_verbose_flag > 0)
4948 Console.WriteLine (e);
4952 void CheckToken (int error, int yyToken, string msg)
4954 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4955 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4958 Report.Error (error, lexer.Location, msg);
4961 void CheckIdentifierToken (int yyToken)
4963 CheckToken (1041, yyToken, "Identifier expected");
4966 string ConsumeStoredComment ()
4968 string s = tmpComment;
4970 Lexer.doc_state = XmlCommentState.Allowed;