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 ();
1469 interface_body opt_semicolon
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 = ConsumeStoredComment ();
1715 if (SimpleIteratorContainer.Simple.Yields)
1718 // Note again, checking is done in semantic analysis
1719 current_container.AddOperator (op);
1721 current_local_parameters = null;
1722 iterator_container = null;
1728 | SEMICOLON { $$ = null; }
1731 : type OPERATOR overloadable_operator
1732 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1734 Operator.OpType op = (Operator.OpType) $3;
1735 CheckUnaryOperator (op);
1737 if (op == Operator.OpType.Addition)
1738 op = Operator.OpType.UnaryPlus;
1740 if (op == Operator.OpType.Subtraction)
1741 op = Operator.OpType.UnaryNegation;
1743 Parameter [] pars = new Parameter [1];
1744 Expression type = (Expression) $5;
1746 pars [0] = new Parameter (type, (string) $6, Parameter.Modifier.NONE, null);
1748 current_local_parameters = new Parameters (pars, null, lexer.Location);
1750 if (RootContext.Documentation != null) {
1751 tmpComment = Lexer.consume_doc_comment ();
1752 Lexer.doc_state = XmlCommentState.Allowed;
1755 $$ = new OperatorDeclaration (op, (Expression) $1, type, (string) $6,
1756 null, null, lexer.Location);
1758 | type OPERATOR overloadable_operator
1760 type IDENTIFIER COMMA
1764 CheckBinaryOperator ((Operator.OpType) $3);
1766 Parameter [] pars = new Parameter [2];
1768 Expression typeL = (Expression) $5;
1769 Expression typeR = (Expression) $8;
1771 pars [0] = new Parameter (typeL, (string) $6, Parameter.Modifier.NONE, null);
1772 pars [1] = new Parameter (typeR, (string) $9, Parameter.Modifier.NONE, null);
1774 current_local_parameters = new Parameters (pars, null, lexer.Location);
1776 if (RootContext.Documentation != null) {
1777 tmpComment = Lexer.consume_doc_comment ();
1778 Lexer.doc_state = XmlCommentState.Allowed;
1781 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1783 typeR, (string) $9, lexer.Location);
1785 | conversion_operator_declarator
1788 overloadable_operator
1790 : BANG { $$ = Operator.OpType.LogicalNot; }
1791 | TILDE { $$ = Operator.OpType.OnesComplement; }
1792 | OP_INC { $$ = Operator.OpType.Increment; }
1793 | OP_DEC { $$ = Operator.OpType.Decrement; }
1794 | TRUE { $$ = Operator.OpType.True; }
1795 | FALSE { $$ = Operator.OpType.False; }
1796 // Unary and binary:
1797 | PLUS { $$ = Operator.OpType.Addition; }
1798 | MINUS { $$ = Operator.OpType.Subtraction; }
1800 | STAR { $$ = Operator.OpType.Multiply; }
1801 | DIV { $$ = Operator.OpType.Division; }
1802 | PERCENT { $$ = Operator.OpType.Modulus; }
1803 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1804 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1805 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1806 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1807 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1808 | OP_EQ { $$ = Operator.OpType.Equality; }
1809 | OP_NE { $$ = Operator.OpType.Inequality; }
1810 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1811 | OP_LT { $$ = Operator.OpType.LessThan; }
1812 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1813 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1816 conversion_operator_declarator
1817 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1819 Parameter [] pars = new Parameter [1];
1821 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1823 current_local_parameters = new Parameters (pars, null, lexer.Location);
1825 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1826 null, null, lexer.Location);
1828 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1830 Parameter [] pars = new Parameter [1];
1832 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1834 current_local_parameters = new Parameters (pars, null, lexer.Location);
1836 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1837 null, null, lexer.Location);
1841 syntax_error (lexer.Location, "'operator' expected");
1845 syntax_error (lexer.Location, "'operator' expected");
1849 constructor_declaration
1852 constructor_declarator
1855 Constructor c = (Constructor) $3;
1856 c.Block = (ToplevelBlock) $4;
1857 c.OptAttributes = (Attributes) $1;
1858 c.ModFlags = (int) $2;
1860 if (RootContext.Documentation != null)
1861 c.DocComment = ConsumeStoredComment ();
1863 if (c.Name == current_container.Basename){
1864 if ((c.ModFlags & Modifiers.STATIC) != 0){
1865 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1867 515, c.Location, String.Format (
1868 "`{0}.{1}': static constructor can not have access modifiers",
1869 c.Name, current_container.Name));
1872 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1874 if (c.Initializer != null){
1877 "Static constructors can not have an explicit this or base " +
1878 "constructor invocations");
1881 if (!c.Parameters.Empty){
1883 132, c.Location, "Static constructors should not have parameters");
1886 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1889 // We let another layer check the validity of the constructor.
1890 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1893 current_container.AddConstructor (c);
1895 current_local_parameters = null;
1896 if (RootContext.Documentation != null)
1897 Lexer.doc_state = XmlCommentState.Allowed;
1901 constructor_declarator
1904 if (RootContext.Documentation != null) {
1905 tmpComment = Lexer.consume_doc_comment ();
1906 Lexer.doc_state = XmlCommentState.Allowed;
1909 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1911 oob_stack.Push (lexer.Location);
1913 current_local_parameters = (Parameters) $4;
1915 opt_constructor_initializer
1917 Location l = (Location) oob_stack.Pop ();
1918 $$ = new Constructor (current_class, (string) $1, 0, (Parameters) $4,
1919 (ConstructorInitializer) $7, l);
1925 | SEMICOLON { $$ = null; }
1928 opt_constructor_initializer
1929 : /* empty */ { $$ = null; }
1930 | constructor_initializer
1933 constructor_initializer
1934 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1936 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1938 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1940 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1943 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1949 : /* EMPTY */ { $$ = 0; }
1950 | UNSAFE { $$ = Modifiers.UNSAFE; }
1951 | EXTERN { $$ = Modifiers.EXTERN; }
1954 destructor_declaration
1955 : opt_attributes opt_finalizer TILDE
1957 if (RootContext.Documentation != null) {
1958 tmpComment = Lexer.consume_doc_comment ();
1959 Lexer.doc_state = XmlCommentState.NotAllowed;
1962 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1964 if ((string) $5 != current_container.Basename){
1965 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1966 } else if (!(current_container is Class)){
1967 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1969 Location l = lexer.Location;
1972 if (!RootContext.StdLib && current_container.Name == "System.Object")
1973 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
1975 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
1977 if ((m & Modifiers.UNSAFE) != 0){
1978 if (!RootContext.Unsafe){
1979 Report.Error (227, l,
1980 "Unsafe code requires the -unsafe command " +
1981 "line option to be specified");
1985 Method d = new Destructor (
1986 current_class, TypeManager.system_void_expr, m, "Finalize",
1987 new Parameters (null, null, l), (Attributes) $1, l);
1988 if (RootContext.Documentation != null)
1989 d.DocComment = ConsumeStoredComment ();
1991 d.Block = (ToplevelBlock) $8;
1992 current_container.AddMethod (d);
2000 EVENT type variable_declarators SEMICOLON
2002 foreach (VariableDeclaration var in (ArrayList) $5) {
2004 MemberName name = new MemberName (var.identifier);
2006 Event e = new EventField (
2007 current_class, (Expression) $4, (int) $2, false, name,
2008 var.expression_or_array_initializer, (Attributes) $1,
2011 current_container.AddEvent (e);
2013 if (RootContext.Documentation != null) {
2014 e.DocComment = Lexer.consume_doc_comment ();
2015 Lexer.doc_state = XmlCommentState.Allowed;
2021 EVENT type namespace_or_type_name
2024 implicit_value_parameter_type = (Expression) $4;
2025 lexer.EventParsing = true;
2026 oob_stack.Push (lexer.Location);
2028 event_accessor_declarations
2030 lexer.EventParsing = false;
2034 Location loc = (Location) oob_stack.Pop ();
2037 Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
2040 Pair pair = (Pair) $8;
2042 MemberName name = (MemberName) $5;
2044 if (name.TypeArguments != null)
2045 syntax_error (lexer.Location, "an event can't have type arguments");
2047 Event e = new EventProperty (
2048 current_class, (Expression) $4, (int) $2, false, name, null,
2049 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
2051 if (RootContext.Documentation != null) {
2052 e.DocComment = Lexer.consume_doc_comment ();
2053 Lexer.doc_state = XmlCommentState.Allowed;
2056 current_container.AddEvent (e);
2057 implicit_value_parameter_type = null;
2060 | opt_attributes opt_modifiers EVENT type namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS block {
2061 MemberName mn = (MemberName) $5;
2063 if (mn.Left != null)
2064 Report.Error (71, lexer.Location, "Explicit implementation of events requires property syntax");
2066 Report.Error (71, lexer.Location, "Event declaration should use property syntax");
2068 if (RootContext.Documentation != null)
2069 Lexer.doc_state = XmlCommentState.Allowed;
2073 event_accessor_declarations
2074 : add_accessor_declaration remove_accessor_declaration
2076 $$ = new Pair ($1, $2);
2078 | remove_accessor_declaration add_accessor_declaration
2080 $$ = new Pair ($2, $1);
2082 | add_accessor_declaration { $$ = null; }
2083 | remove_accessor_declaration { $$ = null; }
2086 Report.Error (1055, lexer.Location, "An add or remove accessor expected");
2092 add_accessor_declaration
2093 : opt_attributes ADD
2095 Parameter [] args = new Parameter [1];
2096 Parameter implicit_value_parameter = new Parameter (
2097 implicit_value_parameter_type, "value",
2098 Parameter.Modifier.NONE, null);
2100 args [0] = implicit_value_parameter;
2102 current_local_parameters = new Parameters (args, null, lexer.Location);
2103 lexer.EventParsing = false;
2107 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2108 lexer.EventParsing = true;
2110 | opt_attributes ADD error {
2111 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2116 remove_accessor_declaration
2117 : opt_attributes REMOVE
2119 Parameter [] args = new Parameter [1];
2120 Parameter implicit_value_parameter = new Parameter (
2121 implicit_value_parameter_type, "value",
2122 Parameter.Modifier.NONE, null);
2124 args [0] = implicit_value_parameter;
2126 current_local_parameters = new Parameters (args, null, lexer.Location);
2127 lexer.EventParsing = false;
2131 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2132 lexer.EventParsing = true;
2134 | opt_attributes REMOVE error {
2135 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2141 : opt_attributes opt_modifiers indexer_declarator
2144 IndexerDeclaration decl = (IndexerDeclaration) $3;
2146 implicit_value_parameter_type = decl.type;
2148 lexer.PropertyParsing = true;
2149 parsing_indexer = true;
2151 indexer_parameters = decl.param_list;
2152 oob_stack.Push (lexer.Location);
2154 accessor_declarations
2156 lexer.PropertyParsing = false;
2157 parsing_indexer = false;
2161 // The signature is computed from the signature of the indexer. Look
2162 // at section 3.6 on the spec
2163 Location loc = (Location) oob_stack.Pop ();
2165 IndexerDeclaration decl = (IndexerDeclaration) $3;
2166 Pair pair = (Pair) $6;
2167 Accessor get_block = (Accessor) pair.First;
2168 Accessor set_block = (Accessor) pair.Second;
2171 if (decl.interface_type != null)
2172 name = new MemberName (decl.interface_type,
2173 TypeContainer.DefaultIndexerName, null);
2175 name = new MemberName (TypeContainer.DefaultIndexerName);
2177 indexer = new Indexer (current_class, decl.type, name,
2178 (int) $2, false, decl.param_list, (Attributes) $1,
2179 get_block, set_block, loc);
2180 if (RootContext.Documentation != null)
2181 indexer.DocComment = ConsumeStoredComment ();
2183 current_container.AddIndexer (indexer);
2185 current_local_parameters = null;
2186 implicit_value_parameter_type = null;
2187 indexer_parameters = null;
2192 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2194 Parameters pars = (Parameters) $4;
2195 if (pars.HasArglist) {
2196 // "__arglist is not valid in this context"
2197 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2198 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2199 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2201 if (RootContext.Documentation != null) {
2202 tmpComment = Lexer.consume_doc_comment ();
2203 Lexer.doc_state = XmlCommentState.Allowed;
2206 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
2208 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2210 Parameters pars = (Parameters) $6;
2212 if (pars.HasArglist) {
2213 // "__arglist is not valid in this context"
2214 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2215 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2216 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2219 MemberName name = (MemberName) $2;
2220 if (name.TypeArguments != null)
2221 syntax_error (lexer.Location, "an indexer can't have type arguments");
2223 $$ = new IndexerDeclaration ((Expression) $1, name, pars);
2225 if (RootContext.Documentation != null) {
2226 tmpComment = Lexer.consume_doc_comment ();
2227 Lexer.doc_state = XmlCommentState.Allowed;
2237 if (RootContext.Documentation != null)
2238 enumTypeComment = Lexer.consume_doc_comment ();
2243 Location enum_location = lexer.Location;
2245 MemberName full_name = MakeName (new MemberName ((string) $4));
2246 Enum e = new Enum (current_namespace, current_container, (Expression) $5, (int) $2,
2247 full_name, (Attributes) $1, enum_location);
2249 if (RootContext.Documentation != null)
2250 e.DocComment = enumTypeComment;
2252 foreach (VariableDeclaration ev in (ArrayList) $7) {
2253 e.AddEnumMember (ev.identifier,
2254 (Expression) ev.expression_or_array_initializer,
2255 ev.Location, ev.OptAttributes,
2259 string name = full_name.GetName ();
2260 current_container.AddEnum (e);
2261 RootContext.Tree.RecordDecl (name, e);
2267 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2268 | COLON type { $$ = $2; }
2274 if (RootContext.Documentation != null)
2275 Lexer.doc_state = XmlCommentState.Allowed;
2277 opt_enum_member_declarations
2279 // here will be evaluated after CLOSE_BLACE is consumed.
2280 if (RootContext.Documentation != null)
2281 Lexer.doc_state = XmlCommentState.Allowed;
2289 opt_enum_member_declarations
2290 : /* empty */ { $$ = new ArrayList (4); }
2291 | enum_member_declarations opt_comma { $$ = $1; }
2294 enum_member_declarations
2295 : enum_member_declaration
2297 ArrayList l = new ArrayList (4);
2302 | enum_member_declarations COMMA enum_member_declaration
2304 ArrayList l = (ArrayList) $1;
2312 enum_member_declaration
2313 : opt_attributes IDENTIFIER
2315 VariableDeclaration vd = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
2317 if (RootContext.Documentation != null) {
2318 vd.DocComment = Lexer.consume_doc_comment ();
2319 Lexer.doc_state = XmlCommentState.Allowed;
2324 | opt_attributes IDENTIFIER
2326 $$ = lexer.Location;
2327 if (RootContext.Documentation != null) {
2328 tmpComment = Lexer.consume_doc_comment ();
2329 Lexer.doc_state = XmlCommentState.NotAllowed;
2334 VariableDeclaration vd = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
2336 if (RootContext.Documentation != null)
2337 vd.DocComment = ConsumeStoredComment ();
2343 delegate_declaration
2346 DELEGATE type member_name
2347 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2349 Location l = lexer.Location;
2350 MemberName name = MakeName ((MemberName) $5);
2351 Delegate del = new Delegate (current_namespace, current_container, (Expression) $4,
2352 (int) $2, name, (Parameters) $7, (Attributes) $1, l);
2354 if (RootContext.Documentation != null) {
2355 del.DocComment = Lexer.consume_doc_comment ();
2356 Lexer.doc_state = XmlCommentState.Allowed;
2359 current_container.AddDelegate (del);
2360 RootContext.Tree.RecordDecl (name.GetName (true), del);
2362 current_delegate = del;
2364 lexer.ConstraintsParsing = true;
2366 opt_type_parameter_constraints_clauses
2368 lexer.ConstraintsParsing = false;
2372 current_delegate.SetParameterInfo ((ArrayList) $9);
2374 current_delegate = null;
2378 DELEGATE VOID member_name
2379 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2381 Location l = lexer.Location;
2382 MemberName name = MakeName ((MemberName) $5);
2383 Delegate del = new Delegate (
2384 current_namespace, current_container,
2385 TypeManager.system_void_expr, (int) $2, name,
2386 (Parameters) $7, (Attributes) $1, l);
2388 if (RootContext.Documentation != null) {
2389 del.DocComment = Lexer.consume_doc_comment ();
2390 Lexer.doc_state = XmlCommentState.Allowed;
2393 current_container.AddDelegate (del);
2394 RootContext.Tree.RecordDecl (name.GetName (true), del);
2396 current_delegate = del;
2398 lexer.ConstraintsParsing = true;
2400 opt_type_parameter_constraints_clauses
2402 lexer.ConstraintsParsing = false;
2406 current_delegate.SetParameterInfo ((ArrayList) $9);
2408 current_delegate = null;
2412 namespace_or_type_name
2414 | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list {
2415 $$ = new MemberName ((MemberName) $1, (string) $3, (TypeArguments) $4);
2420 : IDENTIFIER opt_type_argument_list {
2421 $$ = new MemberName ((string) $1, (TypeArguments) $2);
2425 opt_type_argument_list
2426 : /* empty */ { $$ = null; }
2427 | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2433 $$ = new TypeArguments ((int) $1, lexer.Location);
2439 TypeArguments type_args = new TypeArguments (lexer.Location);
2440 type_args.Add ((Expression) $1);
2443 | type_arguments COMMA type {
2444 TypeArguments type_args = (TypeArguments) $1;
2445 type_args.Add ((Expression) $3);
2451 * Before you think of adding a return_type, notice that we have been
2452 * using two rules in the places where it matters (one rule using type
2453 * and another identical one that uses VOID as the return type). This
2454 * gets rid of a shift/reduce couple
2457 : namespace_or_type_name
2459 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2471 // Note that here only unmanaged types are allowed but we
2472 // can't perform checks during this phase - we do it during
2473 // semantic analysis.
2475 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2479 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2485 | non_expression_type rank_specifier
2487 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2489 | non_expression_type STAR
2491 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2493 | expression rank_specifiers
2495 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2499 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2503 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2504 // through this different path
2506 | multiplicative_expression STAR
2508 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2515 ArrayList types = new ArrayList (4);
2520 | type_list COMMA type
2522 ArrayList types = (ArrayList) $1;
2530 * replaces all the productions for isolating the various
2531 * simple types, but we need this to reuse it easily in local_variable_type
2534 : OBJECT { $$ = TypeManager.system_object_expr; }
2535 | STRING { $$ = TypeManager.system_string_expr; }
2536 | BOOL { $$ = TypeManager.system_boolean_expr; }
2537 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2538 | FLOAT { $$ = TypeManager.system_single_expr; }
2539 | DOUBLE { $$ = TypeManager.system_double_expr; }
2544 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2545 | BYTE { $$ = TypeManager.system_byte_expr; }
2546 | SHORT { $$ = TypeManager.system_int16_expr; }
2547 | USHORT { $$ = TypeManager.system_uint16_expr; }
2548 | INT { $$ = TypeManager.system_int32_expr; }
2549 | UINT { $$ = TypeManager.system_uint32_expr; }
2550 | LONG { $$ = TypeManager.system_int64_expr; }
2551 | ULONG { $$ = TypeManager.system_uint64_expr; }
2552 | CHAR { $$ = TypeManager.system_char_expr; }
2553 | VOID { $$ = TypeManager.system_void_expr; }
2557 : type rank_specifiers
2559 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2564 // Expressions, section 7.5
2574 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2576 | parenthesized_expression
2577 | default_value_expression
2579 | invocation_expression
2583 | post_increment_expression
2584 | post_decrement_expression
2588 | checked_expression
2589 | unchecked_expression
2590 | pointer_member_access
2591 | anonymous_method_expression
2598 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2599 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2600 | NULL { $$ = NullLiteral.Null; }
2604 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2605 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2606 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2611 object v = lexer.Value;
2617 $$ = IntLiteral.Zero;
2619 $$ = IntLiteral.One;
2621 $$ = new IntLiteral (i);
2622 } else if (v is uint)
2623 $$ = new UIntLiteral ((UInt32) v);
2625 $$ = new LongLiteral ((Int64) v);
2626 else if (v is ulong)
2627 $$ = new ULongLiteral ((UInt64) v);
2629 Console.WriteLine ("OOPS. Unexpected result from scanner");
2634 : TRUE { $$ = new BoolLiteral (true); }
2635 | FALSE { $$ = new BoolLiteral (false); }
2638 parenthesized_expression_0
2639 : OPEN_PARENS expression CLOSE_PARENS
2642 lexer.Deambiguate_CloseParens ();
2643 // After this, the next token returned is one of
2644 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2645 // or CLOSE_PARENS_MINUS.
2649 parenthesized_expression
2650 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2654 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2656 // If a parenthesized expression is followed by a minus, we need to wrap
2657 // the expression inside a ParenthesizedExpression for the CS0075 check
2658 // in Binary.DoResolve().
2659 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2664 : primary_expression DOT IDENTIFIER opt_type_argument_list
2666 $$ = new MemberAccess ((Expression) $1, (string) $3,
2667 (TypeArguments) $4, lexer.Location);
2669 | predefined_type DOT IDENTIFIER opt_type_argument_list
2671 $$ = new MemberAccess ((Expression) $1, (string) $3,
2672 (TypeArguments) $4, lexer.Location);
2680 invocation_expression
2681 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2684 Location l = lexer.Location;
2685 Report.Error (1, l, "Parse error");
2687 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2689 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2691 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2693 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2695 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2700 : /* empty */ { $$ = null; }
2707 ArrayList list = new ArrayList (4);
2711 | argument_list COMMA argument
2713 ArrayList list = (ArrayList) $1;
2717 | argument_list error {
2718 CheckToken (1026, yyToken, ", or ) expected");
2725 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2727 | REF variable_reference
2729 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2731 | OUT variable_reference
2733 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2735 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2737 ArrayList list = (ArrayList) $3;
2738 Argument[] args = new Argument [list.Count];
2739 list.CopyTo (args, 0);
2741 Expression expr = new Arglist (args, lexer.Location);
2742 $$ = new Argument (expr, Argument.AType.Expression);
2746 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2751 : expression { note ("section 5.4"); $$ = $1; }
2755 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2757 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2759 | primary_expression rank_specifiers
2761 // So the super-trick is that primary_expression
2762 // can only be either a SimpleName or a MemberAccess.
2763 // The MemberAccess case arises when you have a fully qualified type-name like :
2765 // SimpleName is when you have
2768 Expression expr = (Expression) $1;
2769 if (expr is ComposedCast){
2770 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2771 } else if (!(expr is SimpleName || expr is MemberAccess || expr is ConstructedType)){
2772 Error_ExpectingTypeName (lexer.Location, expr);
2773 $$ = TypeManager.system_object_expr;
2776 // So we extract the string corresponding to the SimpleName
2779 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2787 ArrayList list = new ArrayList (4);
2791 | expression_list COMMA expression
2793 ArrayList list = (ArrayList) $1;
2802 $$ = new This (current_block, lexer.Location);
2807 : BASE DOT IDENTIFIER
2809 $$ = new BaseAccess ((string) $3, lexer.Location);
2811 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2813 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2816 Report.Error (175, lexer.Location, "Use of keyword `base' is not valid in this context");
2821 post_increment_expression
2822 : primary_expression OP_INC
2824 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2825 (Expression) $1, lexer.Location);
2829 post_decrement_expression
2830 : primary_expression OP_DEC
2832 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2833 (Expression) $1, lexer.Location);
2838 : object_or_delegate_creation_expression
2839 | array_creation_expression
2842 object_or_delegate_creation_expression
2843 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2845 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2849 array_creation_expression
2850 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2852 opt_array_initializer
2854 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2856 | NEW type rank_specifiers array_initializer
2858 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2862 Report.Error (1031, lexer.Location, "Type expected");
2867 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2883 : rank_specifier opt_rank_specifier
2885 $$ = (string) $2 + (string) $1;
2890 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2892 $$ = "[" + (string) $2 + "]";
2912 | dim_separators COMMA
2914 $$ = (string) $1 + ",";
2918 opt_array_initializer
2930 : OPEN_BRACE CLOSE_BRACE
2932 ArrayList list = new ArrayList (4);
2935 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2937 $$ = (ArrayList) $2;
2941 variable_initializer_list
2942 : variable_initializer
2944 ArrayList list = new ArrayList (4);
2948 | variable_initializer_list COMMA variable_initializer
2950 ArrayList list = (ArrayList) $1;
2957 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2959 $$ = new TypeOfVoid (lexer.Location);
2961 | TYPEOF OPEN_PARENS
2963 lexer.TypeOfParsing = true;
2967 lexer.TypeOfParsing = false;
2968 $$ = new TypeOf ((Expression) $4, lexer.Location);
2973 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2974 $$ = new SizeOf ((Expression) $3, lexer.Location);
2979 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2981 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2985 unchecked_expression
2986 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2988 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2992 pointer_member_access
2993 : primary_expression OP_PTR IDENTIFIER
2997 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2998 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
3002 anonymous_method_expression
3003 : DELEGATE opt_anonymous_method_signature {
3004 oob_stack.Push (current_local_parameters);
3005 current_local_parameters = (Parameters)$2;
3007 // Force the next block to be created as a ToplevelBlock
3008 oob_stack.Push (current_block);
3009 oob_stack.Push (top_current_block);
3010 oob_stack.Push (lexer.Location);
3011 current_block = null;
3013 Location loc = (Location) oob_stack.Pop ();
3014 top_current_block = (Block) oob_stack.Pop ();
3015 current_block = (Block) oob_stack.Pop ();
3016 if (RootContext.Version == LanguageVersion.ISO_1){
3017 Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
3020 ToplevelBlock anon_block = (ToplevelBlock) $4;
3022 anon_block.Parent = current_block;
3023 $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block,
3026 current_local_parameters = (Parameters) oob_stack.Pop ();
3030 opt_anonymous_method_signature
3031 : /* empty */ { $$ = null; }
3032 | anonymous_method_signature
3035 anonymous_method_signature
3036 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3039 $$ = Parameters.EmptyReadOnlyParameters;
3041 ArrayList par_list = (ArrayList) $2;
3042 Parameter [] pars = new Parameter [par_list.Count];
3043 par_list.CopyTo (pars);
3044 $$ = new Parameters (pars, null, lexer.Location);
3049 opt_anonymous_method_parameter_list
3050 : /* empty */ { $$ = null; }
3051 | anonymous_method_parameter_list { $$ = $1; }
3054 anonymous_method_parameter_list
3055 : anonymous_method_parameter
3057 ArrayList a = new ArrayList (4);
3061 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3063 ArrayList a = (ArrayList) $1;
3069 anonymous_method_parameter
3070 : opt_parameter_modifier type IDENTIFIER {
3071 $$ = new Parameter ((Expression) $2, (string) $3, (Parameter.Modifier) $1, null);
3073 | PARAMS type IDENTIFIER {
3074 Report.Error (-221, lexer.Location, "params modifier not allowed in anonymous method declaration");
3079 default_value_expression
3080 : DEFAULT_OPEN_PARENS type CLOSE_PARENS
3082 $$ = new DefaultValueExpression ((Expression) $2, lexer.Location);
3087 : primary_expression
3088 | BANG prefixed_unary_expression
3090 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
3092 | TILDE prefixed_unary_expression
3094 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
3100 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3102 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3104 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3106 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3112 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3114 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3119 // The idea to split this out is from Rhys' grammar
3120 // to solve the problem with casts.
3122 prefixed_unary_expression
3124 | PLUS prefixed_unary_expression
3126 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
3128 | MINUS prefixed_unary_expression
3130 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
3132 | OP_INC prefixed_unary_expression
3134 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3135 (Expression) $2, lexer.Location);
3137 | OP_DEC prefixed_unary_expression
3139 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3140 (Expression) $2, lexer.Location);
3142 | STAR prefixed_unary_expression
3144 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
3146 | BITWISE_AND prefixed_unary_expression
3148 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
3152 pre_increment_expression
3153 : OP_INC prefixed_unary_expression
3155 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3156 (Expression) $2, lexer.Location);
3160 pre_decrement_expression
3161 : OP_DEC prefixed_unary_expression
3163 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3164 (Expression) $2, lexer.Location);
3168 multiplicative_expression
3169 : prefixed_unary_expression
3170 | multiplicative_expression STAR prefixed_unary_expression
3172 $$ = new Binary (Binary.Operator.Multiply,
3173 (Expression) $1, (Expression) $3, lexer.Location);
3175 | multiplicative_expression DIV prefixed_unary_expression
3177 $$ = new Binary (Binary.Operator.Division,
3178 (Expression) $1, (Expression) $3, lexer.Location);
3180 | multiplicative_expression PERCENT prefixed_unary_expression
3182 $$ = new Binary (Binary.Operator.Modulus,
3183 (Expression) $1, (Expression) $3, lexer.Location);
3188 : multiplicative_expression
3189 | additive_expression PLUS multiplicative_expression
3191 $$ = new Binary (Binary.Operator.Addition,
3192 (Expression) $1, (Expression) $3, lexer.Location);
3194 | additive_expression MINUS multiplicative_expression
3196 $$ = new Binary (Binary.Operator.Subtraction,
3197 (Expression) $1, (Expression) $3, lexer.Location);
3202 : additive_expression
3203 | shift_expression OP_SHIFT_LEFT additive_expression
3205 $$ = new Binary (Binary.Operator.LeftShift,
3206 (Expression) $1, (Expression) $3, lexer.Location);
3208 | shift_expression OP_SHIFT_RIGHT additive_expression
3210 $$ = new Binary (Binary.Operator.RightShift,
3211 (Expression) $1, (Expression) $3, lexer.Location);
3215 relational_expression
3217 | relational_expression OP_LT shift_expression
3219 $$ = new Binary (Binary.Operator.LessThan,
3220 (Expression) $1, (Expression) $3, lexer.Location);
3222 | relational_expression OP_GT shift_expression
3224 $$ = new Binary (Binary.Operator.GreaterThan,
3225 (Expression) $1, (Expression) $3, lexer.Location);
3227 | relational_expression OP_LE shift_expression
3229 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3230 (Expression) $1, (Expression) $3, lexer.Location);
3232 | relational_expression OP_GE shift_expression
3234 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3235 (Expression) $1, (Expression) $3, lexer.Location);
3237 | relational_expression IS type
3239 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
3241 | relational_expression AS type
3243 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
3248 : relational_expression
3249 | equality_expression OP_EQ relational_expression
3251 $$ = new Binary (Binary.Operator.Equality,
3252 (Expression) $1, (Expression) $3, lexer.Location);
3254 | equality_expression OP_NE relational_expression
3256 $$ = new Binary (Binary.Operator.Inequality,
3257 (Expression) $1, (Expression) $3, lexer.Location);
3262 : equality_expression
3263 | and_expression BITWISE_AND equality_expression
3265 $$ = new Binary (Binary.Operator.BitwiseAnd,
3266 (Expression) $1, (Expression) $3, lexer.Location);
3270 exclusive_or_expression
3272 | exclusive_or_expression CARRET and_expression
3274 $$ = new Binary (Binary.Operator.ExclusiveOr,
3275 (Expression) $1, (Expression) $3, lexer.Location);
3279 inclusive_or_expression
3280 : exclusive_or_expression
3281 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3283 $$ = new Binary (Binary.Operator.BitwiseOr,
3284 (Expression) $1, (Expression) $3, lexer.Location);
3288 conditional_and_expression
3289 : inclusive_or_expression
3290 | conditional_and_expression OP_AND inclusive_or_expression
3292 $$ = new Binary (Binary.Operator.LogicalAnd,
3293 (Expression) $1, (Expression) $3, lexer.Location);
3297 conditional_or_expression
3298 : conditional_and_expression
3299 | conditional_or_expression OP_OR conditional_and_expression
3301 $$ = new Binary (Binary.Operator.LogicalOr,
3302 (Expression) $1, (Expression) $3, lexer.Location);
3306 conditional_expression
3307 : conditional_or_expression
3308 | conditional_or_expression INTERR expression COLON expression
3310 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
3314 assignment_expression
3315 : prefixed_unary_expression ASSIGN expression
3317 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
3319 | prefixed_unary_expression OP_MULT_ASSIGN expression
3321 Location l = lexer.Location;
3323 $$ = new CompoundAssign (
3324 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
3326 | prefixed_unary_expression OP_DIV_ASSIGN expression
3328 Location l = lexer.Location;
3330 $$ = new CompoundAssign (
3331 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
3333 | prefixed_unary_expression OP_MOD_ASSIGN expression
3335 Location l = lexer.Location;
3337 $$ = new CompoundAssign (
3338 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
3340 | prefixed_unary_expression OP_ADD_ASSIGN expression
3342 Location l = lexer.Location;
3344 $$ = new CompoundAssign (
3345 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
3347 | prefixed_unary_expression OP_SUB_ASSIGN expression
3349 Location l = lexer.Location;
3351 $$ = new CompoundAssign (
3352 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
3354 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3356 Location l = lexer.Location;
3358 $$ = new CompoundAssign (
3359 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
3361 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3363 Location l = lexer.Location;
3365 $$ = new CompoundAssign (
3366 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
3368 | prefixed_unary_expression OP_AND_ASSIGN expression
3370 Location l = lexer.Location;
3372 $$ = new CompoundAssign (
3373 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
3375 | prefixed_unary_expression OP_OR_ASSIGN expression
3377 Location l = lexer.Location;
3379 $$ = new CompoundAssign (
3380 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
3382 | prefixed_unary_expression OP_XOR_ASSIGN expression
3384 Location l = lexer.Location;
3386 $$ = new CompoundAssign (
3387 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
3392 : conditional_expression
3393 | assignment_expression
3413 MemberName name = MakeName ((MemberName) $5);
3414 bool partial = (bool) $3;
3415 int mod_flags = (int) $2;
3418 ClassPart part = PartialContainer.CreatePart (
3419 current_namespace, current_container, name, mod_flags,
3420 (Attributes) $1, Kind.Class, lexer.Location);
3422 current_container = part.PartialContainer;
3423 current_class = part;
3425 if ((mod_flags & Modifiers.STATIC) != 0) {
3426 current_class = new StaticClass (
3427 current_namespace, current_container, name,
3428 mod_flags, (Attributes) $1, lexer.Location);
3430 current_class = new Class (
3431 current_namespace, current_container, name,
3432 mod_flags, (Attributes) $1, lexer.Location);
3435 current_container = current_class;
3436 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
3439 lexer.ConstraintsParsing = true;
3442 opt_type_parameter_constraints_clauses
3444 lexer.ConstraintsParsing = false;
3447 if (current_class.Name == "System.Object") {
3448 Report.Error (537, current_class.Location,
3449 "The class System.Object cannot have a base " +
3450 "class or implement an interface.");
3452 current_class.Bases = (ArrayList) $7;
3455 current_class.SetParameterInfo ((ArrayList) $8);
3457 if (RootContext.Documentation != null) {
3458 current_class.DocComment = Lexer.consume_doc_comment ();
3459 Lexer.doc_state = XmlCommentState.Allowed;
3462 current_class.Register ();
3466 if (RootContext.Documentation != null)
3467 Lexer.doc_state = XmlCommentState.Allowed;
3473 current_container = current_container.Parent;
3474 current_class = current_container;
3480 { $$ = (bool) false; }
3482 { $$ = (bool) true; }
3486 : /* empty */ { $$ = (int) 0; }
3492 | modifiers modifier
3497 if ((m1 & m2) != 0) {
3498 Location l = lexer.Location;
3499 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3501 $$ = (int) (m1 | m2);
3506 : NEW { $$ = Modifiers.NEW; }
3507 | PUBLIC { $$ = Modifiers.PUBLIC; }
3508 | PROTECTED { $$ = Modifiers.PROTECTED; }
3509 | INTERNAL { $$ = Modifiers.INTERNAL; }
3510 | PRIVATE { $$ = Modifiers.PRIVATE; }
3511 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3512 | SEALED { $$ = Modifiers.SEALED; }
3513 | STATIC { $$ = Modifiers.STATIC; }
3514 | READONLY { $$ = Modifiers.READONLY; }
3515 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3516 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3517 | EXTERN { $$ = Modifiers.EXTERN; }
3518 | VOLATILE { $$ = Modifiers.VOLATILE; }
3519 | UNSAFE { $$ = Modifiers.UNSAFE; }
3523 : /* empty */ { $$ = null; }
3524 | class_base { $$ = $1; }
3528 : COLON type_list { $$ = $2; }
3531 opt_type_parameter_constraints_clauses
3532 : /* empty */ { $$ = null; }
3533 | type_parameter_constraints_clauses
3537 type_parameter_constraints_clauses
3538 : type_parameter_constraints_clause {
3539 ArrayList constraints = new ArrayList (1);
3540 constraints.Add ($1);
3543 | type_parameter_constraints_clauses type_parameter_constraints_clause {
3544 ArrayList constraints = (ArrayList) $1;
3546 constraints.Add ($2);
3551 type_parameter_constraints_clause
3552 : WHERE IDENTIFIER COLON type_parameter_constraints {
3553 $$ = new Constraints ((string) $2, (ArrayList) $4, lexer.Location);
3557 type_parameter_constraints
3558 : type_parameter_constraint {
3559 ArrayList constraints = new ArrayList (1);
3560 constraints.Add ($1);
3563 | type_parameter_constraints COMMA type_parameter_constraint {
3564 ArrayList constraints = (ArrayList) $1;
3566 constraints.Add ($3);
3571 type_parameter_constraint
3573 | NEW OPEN_PARENS CLOSE_PARENS {
3574 $$ = SpecialConstraint.Constructor;
3577 $$ = SpecialConstraint.ReferenceType;
3580 $$ = SpecialConstraint.ValueType;
3589 // A block is "contained" on the following places:
3591 // property_declaration as part of the accessor body (get/set)
3592 // operator_declaration
3593 // constructor_declaration
3594 // destructor_declaration
3595 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3600 if (current_block == null){
3601 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, lexer.Location);
3602 top_current_block = current_block;
3604 current_block = new Block (current_block, current_local_parameters,
3605 lexer.Location, Location.Null);
3608 opt_statement_list CLOSE_BRACE
3610 while (current_block.Implicit)
3611 current_block = current_block.Parent;
3613 current_block.SetEndLocation (lexer.Location);
3614 current_block = current_block.Parent;
3615 if (current_block == null)
3616 top_current_block = null;
3627 | statement_list statement
3631 : declaration_statement
3633 if ($1 != null && (Block) $1 != current_block){
3634 current_block.AddStatement ((Statement) $1);
3635 current_block = (Block) $1;
3638 | valid_declaration_statement
3640 current_block.AddStatement ((Statement) $1);
3645 valid_declaration_statement
3648 | expression_statement
3649 | selection_statement
3650 | iteration_statement
3654 | unchecked_statement
3662 : valid_declaration_statement
3663 | declaration_statement
3665 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3670 Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3678 $$ = EmptyStatement.Value;
3685 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3687 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3688 current_block.AddStatement (labeled);
3693 declaration_statement
3694 : local_variable_declaration SEMICOLON
3697 DictionaryEntry de = (DictionaryEntry) $1;
3699 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3703 | local_constant_declaration SEMICOLON
3706 DictionaryEntry de = (DictionaryEntry) $1;
3708 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3714 * The following is from Rhys' grammar:
3715 * > Types in local variable declarations must be recognized as
3716 * > expressions to prevent reduce/reduce errors in the grammar.
3717 * > The expressions are converted into types during semantic analysis.
3720 : primary_expression opt_rank_specifier
3722 // FIXME: Do something smart here regarding the composition of the type.
3724 // Ok, the above "primary_expression" is there to get rid of
3725 // both reduce/reduce and shift/reduces in the grammar, it should
3726 // really just be "type_name". If you use type_name, a reduce/reduce
3727 // creeps up. If you use namespace_or_type_name (which is all we need
3728 // really) two shift/reduces appear.
3731 // So the super-trick is that primary_expression
3732 // can only be either a SimpleName or a MemberAccess.
3733 // The MemberAccess case arises when you have a fully qualified type-name like :
3735 // SimpleName is when you have
3738 Expression expr = (Expression) $1;
3739 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType)) {
3740 Error_ExpectingTypeName (lexer.Location, expr);
3744 // So we extract the string corresponding to the SimpleName
3748 if ((string) $2 == "")
3751 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3754 | builtin_types opt_rank_specifier
3756 if ((string) $2 == "")
3759 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3763 local_variable_pointer_type
3764 : primary_expression STAR
3766 Expression expr = (Expression) $1;
3767 Location l = lexer.Location;
3769 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType)) {
3770 Error_ExpectingTypeName (l, expr);
3774 $$ = new ComposedCast ((Expression) $1, "*", l);
3776 | builtin_types STAR
3778 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3782 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3784 | local_variable_pointer_type STAR
3786 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3790 local_variable_declaration
3791 : local_variable_type variable_declarators
3794 $$ = new DictionaryEntry ($1, $2);
3798 | local_variable_pointer_type opt_rank_specifier variable_declarators
3803 if ((string) $2 == "")
3804 t = (Expression) $1;
3806 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3807 $$ = new DictionaryEntry (t, $3);
3813 local_constant_declaration
3814 : CONST local_variable_type constant_declarators
3817 $$ = new DictionaryEntry ($2, $3);
3823 expression_statement
3824 : statement_expression SEMICOLON
3831 // We have to do the wrapping here and not in the case above,
3832 // because statement_expression is used for example in for_statement
3834 statement_expression
3835 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3836 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3837 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3838 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3839 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3840 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3841 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3843 Report.Error (1002, lexer.Location, "Expecting `;'");
3848 object_creation_expression
3849 : object_or_delegate_creation_expression
3850 { note ("complain if this is a delegate maybe?"); }
3859 : if_statement_open if_statement_rest
3868 oob_stack.Push (lexer.Location);
3873 : boolean_expression CLOSE_PARENS
3876 Location l = (Location) oob_stack.Pop ();
3878 $$ = new If ((Expression) $1, (Statement) $3, l);
3880 if (RootContext.WarningLevel >= 3){
3881 if ($3 == EmptyStatement.Value)
3882 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3886 | boolean_expression CLOSE_PARENS
3887 embedded_statement ELSE embedded_statement
3889 Location l = (Location) oob_stack.Pop ();
3891 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
3896 : SWITCH OPEN_PARENS
3898 oob_stack.Push (lexer.Location);
3899 switch_stack.Push (current_block);
3901 expression CLOSE_PARENS
3904 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
3905 current_block = (Block) switch_stack.Pop ();
3921 Report.Error (1522, lexer.Location, "Empty switch block");
3929 ArrayList sections = new ArrayList (4);
3934 | switch_sections switch_section
3936 ArrayList sections = (ArrayList) $1;
3946 current_block = current_block.CreateSwitchBlock (lexer.Location);
3950 Block topmost = current_block;
3952 while (topmost.Implicit)
3953 topmost = topmost.Parent;
3954 $$ = new SwitchSection ((ArrayList) $1, topmost);
3961 ArrayList labels = new ArrayList (4);
3966 | switch_labels switch_label
3968 ArrayList labels = (ArrayList) ($1);
3976 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3977 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3980 1523, lexer.Location,
3981 "The keyword case or default must precede code in switch block");
3995 oob_stack.Push (lexer.Location);
3997 boolean_expression CLOSE_PARENS embedded_statement
3999 Location l = (Location) oob_stack.Pop ();
4000 $$ = new While ((Expression) $4, (Statement) $6, l);
4002 if (RootContext.WarningLevel >= 3){
4003 if ($6 == EmptyStatement.Value)
4004 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4010 : DO embedded_statement
4012 oob_stack.Push (lexer.Location);
4014 boolean_expression CLOSE_PARENS SEMICOLON
4016 Location l = (Location) oob_stack.Pop ();
4018 $$ = new Do ((Statement) $2, (Expression) $6, l);
4024 opt_for_initializer SEMICOLON
4026 Block assign_block = new Block (current_block);
4027 current_block = assign_block;
4029 if ($3 is DictionaryEntry){
4030 DictionaryEntry de = (DictionaryEntry) $3;
4032 Expression type = (Expression) de.Key;
4033 ArrayList var_declarators = (ArrayList) de.Value;
4035 foreach (VariableDeclaration decl in var_declarators){
4039 vi = current_block.AddVariable (
4040 type, decl.identifier, current_local_parameters, decl.Location);
4044 Location l = lexer.Location;
4046 if (decl.expression_or_array_initializer is Expression){
4047 expr = (Expression) decl.expression_or_array_initializer;
4048 } else if (decl.expression_or_array_initializer == null) {
4051 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4052 expr = new ArrayCreation (type, "", init, decl.Location);
4055 LocalVariableReference var;
4056 var = new LocalVariableReference (assign_block, decl.identifier, l);
4059 Assign a = new Assign (var, expr, decl.Location);
4061 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4067 oob_stack.Push (lexer.Location);
4069 opt_for_condition SEMICOLON
4070 opt_for_iterator CLOSE_PARENS
4073 Location l = (Location) oob_stack.Pop ();
4075 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
4077 if (RootContext.WarningLevel >= 3){
4078 if ($10 == EmptyStatement.Value)
4079 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4082 current_block.AddStatement (f);
4083 while (current_block.Implicit)
4084 current_block = current_block.Parent;
4086 current_block = current_block.Parent;
4091 : /* empty */ { $$ = EmptyStatement.Value; }
4096 : local_variable_declaration
4097 | statement_expression_list
4101 : /* empty */ { $$ = null; }
4102 | boolean_expression
4106 : /* empty */ { $$ = EmptyStatement.Value; }
4111 : statement_expression_list
4114 statement_expression_list
4115 : statement_expression
4117 // CHANGE: was `null'
4118 Block b = new Block (current_block, Block.Flags.Implicit);
4120 b.AddStatement ((Statement) $1);
4123 | statement_expression_list COMMA statement_expression
4125 Block b = (Block) $1;
4127 b.AddStatement ((Statement) $3);
4133 : FOREACH OPEN_PARENS type IDENTIFIER IN
4135 oob_stack.Push (lexer.Location);
4137 expression CLOSE_PARENS
4139 oob_stack.Push (current_block);
4141 Block foreach_block = new Block (current_block);
4142 LocalVariableReference v = null;
4143 Location l = lexer.Location;
4146 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
4150 // Get a writable reference to this read-only variable.
4151 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
4153 current_block = foreach_block;
4156 oob_stack.Push (current_block);
4160 Block foreach_block = (Block) oob_stack.Pop ();
4161 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
4162 Block prev_block = (Block) oob_stack.Pop ();
4163 Location l = (Location) oob_stack.Pop ();
4165 current_block = prev_block;
4168 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
4169 foreach_block.AddStatement (f);
4178 | continue_statement
4188 $$ = new Break (lexer.Location);
4193 : CONTINUE SEMICOLON
4195 $$ = new Continue (lexer.Location);
4200 : GOTO IDENTIFIER SEMICOLON
4202 $$ = new Goto (current_block, (string) $2, lexer.Location);
4204 | GOTO CASE constant_expression SEMICOLON
4206 $$ = new GotoCase ((Expression) $3, lexer.Location);
4208 | GOTO DEFAULT SEMICOLON
4210 $$ = new GotoDefault (lexer.Location);
4215 : RETURN opt_expression SEMICOLON
4217 $$ = new Return ((Expression) $2, lexer.Location);
4222 : THROW opt_expression SEMICOLON
4224 $$ = new Throw ((Expression) $2, lexer.Location);
4229 : IDENTIFIER RETURN expression SEMICOLON
4231 string s = (string) $1;
4233 Report.Error (1003, lexer.Location, "; expected");
4236 if (RootContext.Version == LanguageVersion.ISO_1){
4237 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4240 if (iterator_container == null){
4241 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4244 iterator_container.SetYields ();
4245 $$ = new Yield ((Expression) $3, lexer.Location);
4248 | IDENTIFIER BREAK SEMICOLON
4250 string s = (string) $1;
4252 Report.Error (1003, lexer.Location, "; expected");
4255 if (RootContext.Version == LanguageVersion.ISO_1){
4256 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4259 if (iterator_container == null){
4260 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4263 iterator_container.SetYields ();
4264 $$ = new YieldBreak (lexer.Location);
4275 : TRY block catch_clauses
4279 ArrayList c = (ArrayList)$3;
4280 for (int i = 0; i < c.Count; ++i) {
4281 Catch cc = (Catch) c [i];
4283 if (i != c.Count - 1)
4284 Report.Error (1017, cc.loc, "Empty catch block must be the last in a series of catch blocks");
4291 // Now s contains the list of specific catch clauses
4292 // and g contains the general one.
4294 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4296 | TRY block opt_catch_clauses FINALLY block
4299 ArrayList s = new ArrayList (4);
4300 ArrayList catch_list = (ArrayList) $3;
4302 if (catch_list != null){
4303 foreach (Catch cc in catch_list) {
4311 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4315 Report.Error (1524, lexer.Location, "Expected catch or finally");
4320 : /* empty */ { $$ = null; }
4327 ArrayList l = new ArrayList (4);
4332 | catch_clauses catch_clause
4334 ArrayList l = (ArrayList) $1;
4342 : /* empty */ { $$ = null; }
4347 : CATCH opt_catch_args
4349 Expression type = null;
4353 DictionaryEntry cc = (DictionaryEntry) $2;
4354 type = (Expression) cc.Key;
4355 id = (string) cc.Value;
4358 ArrayList one = new ArrayList (4);
4359 Location loc = lexer.Location;
4361 one.Add (new VariableDeclaration (id, null, loc));
4364 current_block = new Block (current_block);
4365 Block b = declare_local_variables (type, one, loc);
4370 Expression type = null;
4374 DictionaryEntry cc = (DictionaryEntry) $2;
4375 type = (Expression) cc.Key;
4376 id = (string) cc.Value;
4380 // FIXME: I can change this for an assignment.
4382 while (current_block != (Block) $1)
4383 current_block = current_block.Parent;
4388 $$ = new Catch (type, id , (Block) $4, ((Block) $4).loc);
4393 : /* empty */ { $$ = null; }
4398 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4400 $$ = new DictionaryEntry ($2, $3);
4407 $$ = new Checked ((Block) $2);
4414 $$ = new Unchecked ((Block) $2);
4421 if (!RootContext.Unsafe){
4422 Report.Error (227, lexer.Location,
4423 "Unsafe code can only be used if --unsafe is used");
4426 $$ = new Unsafe ((Block) $3);
4432 type fixed_pointer_declarators
4435 ArrayList list = (ArrayList) $4;
4436 Expression type = (Expression) $3;
4437 Location l = lexer.Location;
4438 int top = list.Count;
4440 Block assign_block = new Block (current_block);
4441 current_block = assign_block;
4443 for (int i = 0; i < top; i++){
4444 Pair p = (Pair) list [i];
4447 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
4461 Location l = (Location) oob_stack.Pop ();
4463 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4465 if (RootContext.WarningLevel >= 3){
4466 if ($7 == EmptyStatement.Value)
4467 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4470 current_block.AddStatement (f);
4471 while (current_block.Implicit)
4472 current_block = current_block.Parent;
4474 current_block = current_block.Parent;
4478 fixed_pointer_declarators
4479 : fixed_pointer_declarator {
4480 ArrayList declarators = new ArrayList (4);
4482 declarators.Add ($1);
4485 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4487 ArrayList declarators = (ArrayList) $1;
4489 declarators.Add ($3);
4494 fixed_pointer_declarator
4495 : IDENTIFIER ASSIGN expression
4497 $$ = new Pair ($1, $3);
4501 Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
4507 : LOCK OPEN_PARENS expression CLOSE_PARENS
4513 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4518 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4520 Block assign_block = new Block (current_block);
4521 current_block = assign_block;
4523 oob_stack.Push (lexer.Location);
4525 if ($3 is DictionaryEntry){
4526 DictionaryEntry de = (DictionaryEntry) $3;
4527 Location l = lexer.Location;
4529 Expression type = (Expression) de.Key;
4530 ArrayList var_declarators = (ArrayList) de.Value;
4532 ArrayList vars = new ArrayList (4);
4534 foreach (VariableDeclaration decl in var_declarators){
4536 LocalInfo vi = current_block.AddVariable (
4537 type, decl.identifier,
4538 current_local_parameters, decl.Location);
4544 if (decl.expression_or_array_initializer is Expression){
4545 expr = (Expression) decl.expression_or_array_initializer;
4547 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4549 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4552 expr = new ArrayCreation (type, "", init, decl.Location);
4555 LocalVariableReference var;
4557 // Get a writable reference to this read-only variable.
4558 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4560 // This is so that it is not a warning on using variables
4563 vars.Add (new DictionaryEntry (var, expr));
4565 // Assign a = new Assign (var, expr, decl.Location);
4566 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4568 $3 = new DictionaryEntry (type, vars);
4573 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
4574 current_block.AddStatement (u);
4575 while (current_block.Implicit)
4576 current_block = current_block.Parent;
4578 current_block = current_block.Parent;
4582 resource_acquisition
4583 : local_variable_declaration
4590 // A class used to pass around variable declarations and constants
4592 public class VariableDeclaration {
4593 public string identifier;
4594 public object expression_or_array_initializer;
4595 public Location Location;
4596 public Attributes OptAttributes;
4597 public string DocComment;
4599 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4601 this.identifier = id;
4602 this.expression_or_array_initializer = eoai;
4604 this.OptAttributes = opt_attrs;
4607 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4613 /// Used to pass around interface property information
4615 public class InterfaceAccessorInfo {
4617 public readonly Accessor Get, Set;
4619 public InterfaceAccessorInfo (bool has_get, bool has_set,
4620 Attributes get_attrs, Attributes set_attrs, int get_mod, int set_mod, Location get_loc, Location set_loc)
4623 Report.Error (275, get_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4625 Report.Error (275, set_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4628 Get = new Accessor (null, 0, get_attrs, get_loc);
4630 Set = new Accessor (null, 0, set_attrs, set_loc);
4637 // A class used to hold info about an indexer declarator
4639 public class IndexerDeclaration {
4640 public Expression type;
4641 public MemberName interface_type;
4642 public Parameters param_list;
4644 public IndexerDeclaration (Expression type, MemberName interface_type,
4645 Parameters param_list)
4648 this.interface_type = interface_type;
4649 this.param_list = param_list;
4654 // We use this when we do not have an object in advance that is an IIteratorContainer
4656 public class SimpleIteratorContainer : IIteratorContainer {
4659 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4664 public static SimpleIteratorContainer GetSimple () {
4665 Simple.Yields = false;
4669 public void SetYields () { Yields = true; }
4673 // A class used to hold info about an operator declarator
4675 public class OperatorDeclaration {
4676 public Operator.OpType optype;
4677 public Expression ret_type, arg1type, arg2type;
4678 public string arg1name, arg2name;
4679 public Location location;
4681 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4682 Expression arg1type, string arg1name,
4683 Expression arg2type, string arg2name, Location location)
4686 this.ret_type = ret_type;
4687 this.arg1type = arg1type;
4688 this.arg1name = arg1name;
4689 this.arg2type = arg2type;
4690 this.arg2name = arg2name;
4691 this.location = location;
4696 void Error_ExpectingTypeName (Location l, Expression expr)
4698 if (expr is Invocation){
4699 Report.Error (1002, l, "; expected");
4701 Report.Error (-1, l, "Invalid Type definition");
4706 // Given the @class_name name, it creates a fully qualified name
4707 // based on the containing declaration space
4710 MakeName (MemberName class_name)
4712 string ns = current_namespace.FullName;
4714 if (current_container.Name == ""){
4716 return new MemberName (new MemberName (ns), class_name);
4720 return new MemberName (current_container.MemberName, class_name);
4724 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4726 Block implicit_block;
4727 ArrayList inits = null;
4730 // We use the `Used' property to check whether statements
4731 // have been added to the current block. If so, we need
4732 // to create another block to contain the new declaration
4733 // otherwise, as an optimization, we use the same block to
4734 // add the declaration.
4736 // FIXME: A further optimization is to check if the statements
4737 // that were added were added as part of the initialization
4738 // below. In which case, no other statements have been executed
4739 // and we might be able to reduce the number of blocks for
4740 // situations like this:
4742 // int j = 1; int k = j + 1;
4744 if (current_block.Used)
4745 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4747 implicit_block = current_block;
4749 foreach (VariableDeclaration decl in variable_declarators){
4751 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4752 if (decl.expression_or_array_initializer != null){
4754 inits = new ArrayList (4);
4761 return implicit_block;
4763 foreach (VariableDeclaration decl in inits){
4767 if (decl.expression_or_array_initializer is Expression){
4768 expr = (Expression) decl.expression_or_array_initializer;
4771 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4773 expr = new ArrayCreation (type, "", init, decl.Location);
4776 LocalVariableReference var;
4777 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4779 assign = new Assign (var, expr, decl.Location);
4781 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4784 return implicit_block;
4787 Block declare_local_constants (Expression type, ArrayList declarators)
4789 Block implicit_block;
4791 if (current_block.Used)
4792 implicit_block = new Block (current_block, Block.Flags.Implicit);
4794 implicit_block = current_block;
4796 foreach (VariableDeclaration decl in declarators){
4797 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4798 current_local_parameters, decl.Location);
4801 return implicit_block;
4804 void CheckAttributeTarget (string a)
4808 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4812 Location l = lexer.Location;
4813 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4819 void CheckUnaryOperator (Operator.OpType op)
4823 case Operator.OpType.LogicalNot:
4824 case Operator.OpType.OnesComplement:
4825 case Operator.OpType.Increment:
4826 case Operator.OpType.Decrement:
4827 case Operator.OpType.True:
4828 case Operator.OpType.False:
4829 case Operator.OpType.Addition:
4830 case Operator.OpType.Subtraction:
4835 Location l = lexer.Location;
4836 Report.Error (1019, l, "Overloadable unary operator expected");
4842 void CheckBinaryOperator (Operator.OpType op)
4846 case Operator.OpType.Addition:
4847 case Operator.OpType.Subtraction:
4848 case Operator.OpType.Multiply:
4849 case Operator.OpType.Division:
4850 case Operator.OpType.Modulus:
4851 case Operator.OpType.BitwiseAnd:
4852 case Operator.OpType.BitwiseOr:
4853 case Operator.OpType.ExclusiveOr:
4854 case Operator.OpType.LeftShift:
4855 case Operator.OpType.RightShift:
4856 case Operator.OpType.Equality:
4857 case Operator.OpType.Inequality:
4858 case Operator.OpType.GreaterThan:
4859 case Operator.OpType.LessThan:
4860 case Operator.OpType.GreaterThanOrEqual:
4861 case Operator.OpType.LessThanOrEqual:
4865 Location l = lexer.Location;
4866 Report.Error (1020, l, "Overloadable binary operator expected");
4872 void syntax_error (Location l, string msg)
4874 Report.Error (1003, l, "Syntax error, " + msg);
4877 void output (string s)
4879 Console.WriteLine (s);
4882 void note (string s)
4884 // Used to put annotations
4889 public Tokenizer Lexer {
4895 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4897 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4898 this.name = file.Name;
4900 current_container = RootContext.Tree.Types;
4901 current_container.NamespaceEntry = current_namespace;
4902 oob_stack = new Stack ();
4903 switch_stack = new Stack ();
4905 lexer = new Tokenizer (reader, file, defines);
4908 public void parse ()
4911 if (yacc_verbose_flag > 1)
4912 yyparse (lexer, new yydebug.yyDebugSimple ());
4915 Tokenizer tokenizer = lexer as Tokenizer;
4916 tokenizer.cleanup ();
4917 } catch (Exception e){
4919 // Removed for production use, use parser verbose to get the output.
4921 // Console.WriteLine (e);
4922 Report.Error (-25, lexer.Location, "Parsing error");
4923 if (yacc_verbose_flag > 0)
4924 Console.WriteLine (e);
4928 void CheckToken (int error, int yyToken, string msg)
4930 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4931 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4934 Report.Error (error, lexer.Location, msg);
4937 void CheckIdentifierToken (int yyToken)
4939 CheckToken (1041, yyToken, "Identifier expected");
4942 string ConsumeStoredComment ()
4944 string s = tmpComment;
4946 Lexer.doc_state = XmlCommentState.Allowed;